c++: P0847R7 (deducing this) - initial functionality. [PR102609]
[official-gcc.git] / gcc / cp / parser.cc
blob046c48d5552b3619b29865aa2750df1f0a1b42b0
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2024 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #define INCLUDE_MEMORY
23 #include "system.h"
24 #include "coretypes.h"
25 #include "cp-tree.h"
26 #include "c-family/c-common.h"
27 #include "timevar.h"
28 #include "stringpool.h"
29 #include "cgraph.h"
30 #include "print-tree.h"
31 #include "attribs.h"
32 #include "trans-mem.h"
33 #include "intl.h"
34 #include "decl.h"
35 #include "c-family/c-objc.h"
36 #include "plugin.h"
37 #include "tree-pretty-print.h"
38 #include "parser.h"
39 #include "gomp-constants.h"
40 #include "omp-general.h"
41 #include "omp-offload.h"
42 #include "c-family/c-indentation.h"
43 #include "context.h"
44 #include "gcc-rich-location.h"
45 #include "tree-iterator.h"
46 #include "cp-name-hint.h"
47 #include "memmodel.h"
48 #include "c-family/known-headers.h"
49 #include "contracts.h"
50 #include "bitmap.h"
51 #include "builtins.h"
54 /* The lexer. */
56 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
57 and c-lex.cc) and the C++ parser. */
59 /* The various kinds of non integral constant we encounter. */
60 enum non_integral_constant {
61 NIC_NONE,
62 /* floating-point literal */
63 NIC_FLOAT,
64 /* %<this%> */
65 NIC_THIS,
66 /* %<__FUNCTION__%> */
67 NIC_FUNC_NAME,
68 /* %<__PRETTY_FUNCTION__%> */
69 NIC_PRETTY_FUNC,
70 /* %<__func__%> */
71 NIC_C99_FUNC,
72 /* "%<va_arg%> */
73 NIC_VA_ARG,
74 /* a cast */
75 NIC_CAST,
76 /* %<typeid%> operator */
77 NIC_TYPEID,
78 /* non-constant compound literals */
79 NIC_NCC,
80 /* a function call */
81 NIC_FUNC_CALL,
82 /* an increment */
83 NIC_INC,
84 /* an decrement */
85 NIC_DEC,
86 /* an array reference */
87 NIC_ARRAY_REF,
88 /* %<->%> */
89 NIC_ARROW,
90 /* %<.%> */
91 NIC_POINT,
92 /* the address of a label */
93 NIC_ADDR_LABEL,
94 /* %<*%> */
95 NIC_STAR,
96 /* %<&%> */
97 NIC_ADDR,
98 /* %<++%> */
99 NIC_PREINCREMENT,
100 /* %<--%> */
101 NIC_PREDECREMENT,
102 /* %<new%> */
103 NIC_NEW,
104 /* %<delete%> */
105 NIC_DEL,
106 /* calls to overloaded operators */
107 NIC_OVERLOADED,
108 /* an assignment */
109 NIC_ASSIGNMENT,
110 /* a comma operator */
111 NIC_COMMA,
112 /* a call to a constructor */
113 NIC_CONSTRUCTOR,
114 /* a transaction expression */
115 NIC_TRANSACTION
118 /* The various kinds of errors about name-lookup failing. */
119 enum name_lookup_error {
120 /* NULL */
121 NLE_NULL,
122 /* is not a type */
123 NLE_TYPE,
124 /* is not a class or namespace */
125 NLE_CXX98,
126 /* is not a class, namespace, or enumeration */
127 NLE_NOT_CXX98
130 /* The various kinds of required token */
131 enum required_token {
132 RT_NONE,
133 RT_SEMICOLON, /* ';' */
134 RT_OPEN_PAREN, /* '(' */
135 RT_CLOSE_BRACE, /* '}' */
136 RT_OPEN_BRACE, /* '{' */
137 RT_CLOSE_SQUARE, /* ']' */
138 RT_OPEN_SQUARE, /* '[' */
139 RT_COMMA, /* ',' */
140 RT_SCOPE, /* '::' */
141 RT_LESS, /* '<' */
142 RT_GREATER, /* '>' */
143 RT_EQ, /* '=' */
144 RT_ELLIPSIS, /* '...' */
145 RT_MULT, /* '*' */
146 RT_COMPL, /* '~' */
147 RT_COLON, /* ':' */
148 RT_COLON_SCOPE, /* ':' or '::' */
149 RT_CLOSE_PAREN, /* ')' */
150 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
151 RT_PRAGMA_EOL, /* end of line */
152 RT_NAME, /* identifier */
154 /* The type is CPP_KEYWORD */
155 RT_NEW, /* new */
156 RT_DELETE, /* delete */
157 RT_RETURN, /* return */
158 RT_WHILE, /* while */
159 RT_EXTERN, /* extern */
160 RT_STATIC_ASSERT, /* static_assert */
161 RT_DECLTYPE, /* decltype */
162 RT_OPERATOR, /* operator */
163 RT_CLASS, /* class */
164 RT_TEMPLATE, /* template */
165 RT_NAMESPACE, /* namespace */
166 RT_USING, /* using */
167 RT_ASM, /* asm */
168 RT_TRY, /* try */
169 RT_CATCH, /* catch */
170 RT_THROW, /* throw */
171 RT_AUTO, /* auto */
172 RT_LABEL, /* __label__ */
173 RT_AT_TRY, /* @try */
174 RT_AT_SYNCHRONIZED, /* @synchronized */
175 RT_AT_THROW, /* @throw */
177 RT_SELECT, /* selection-statement */
178 RT_ITERATION, /* iteration-statement */
179 RT_JUMP, /* jump-statement */
180 RT_CLASS_KEY, /* class-key */
181 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
182 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
183 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
184 RT_TRANSACTION_CANCEL, /* __transaction_cancel */
186 RT_CO_YIELD /* co_yield */
189 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
190 reverting it on destruction. */
192 class type_id_in_expr_sentinel
194 cp_parser *parser;
195 bool saved;
196 public:
197 type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
198 : parser (parser),
199 saved (parser->in_type_id_in_expr_p)
200 { parser->in_type_id_in_expr_p = set; }
201 ~type_id_in_expr_sentinel ()
202 { parser->in_type_id_in_expr_p = saved; }
205 /* Prototypes. */
207 static cp_lexer *cp_lexer_new_main
208 (void);
209 static cp_lexer *cp_lexer_new_from_tokens
210 (cp_token_cache *tokens);
211 static void cp_lexer_destroy
212 (cp_lexer *);
213 static int cp_lexer_saving_tokens
214 (const cp_lexer *);
215 static cp_token *cp_lexer_token_at
216 (cp_lexer *, cp_token_position);
217 static void cp_lexer_get_preprocessor_token
218 (unsigned, cp_token *);
219 static inline cp_token *cp_lexer_peek_token
220 (cp_lexer *);
221 static cp_token *cp_lexer_peek_nth_token
222 (cp_lexer *, size_t);
223 static inline bool cp_lexer_next_token_is
224 (cp_lexer *, enum cpp_ttype);
225 static bool cp_lexer_next_token_is_not
226 (cp_lexer *, enum cpp_ttype);
227 static bool cp_lexer_next_token_is_keyword
228 (cp_lexer *, enum rid);
229 static cp_token *cp_lexer_consume_token
230 (cp_lexer *);
231 static void cp_lexer_purge_token
232 (cp_lexer *);
233 static void cp_lexer_purge_tokens_after
234 (cp_lexer *, cp_token_position);
235 static void cp_lexer_save_tokens
236 (cp_lexer *);
237 static void cp_lexer_commit_tokens
238 (cp_lexer *);
239 static void cp_lexer_rollback_tokens
240 (cp_lexer *);
241 static void cp_lexer_print_token
242 (FILE *, cp_token *);
243 static inline bool cp_lexer_debugging_p
244 (cp_lexer *);
245 static void cp_lexer_start_debugging
246 (cp_lexer *) ATTRIBUTE_UNUSED;
247 static void cp_lexer_stop_debugging
248 (cp_lexer *) ATTRIBUTE_UNUSED;
249 static const cp_trait *cp_lexer_peek_trait
250 (cp_lexer *);
251 static const cp_trait *cp_lexer_peek_trait_expr
252 (cp_lexer *);
253 static const cp_trait *cp_lexer_peek_trait_type
254 (cp_lexer *);
256 static cp_token_cache *cp_token_cache_new
257 (cp_token *, cp_token *);
258 static tree cp_parser_late_noexcept_specifier
259 (cp_parser *, tree);
260 static void noexcept_override_late_checks
261 (tree);
263 static void cp_parser_initial_pragma
264 (cp_token *);
266 static bool cp_parser_omp_declare_reduction_exprs
267 (tree, cp_parser *);
268 static void cp_finalize_oacc_routine
269 (cp_parser *, tree, bool);
271 static void check_omp_intervening_code
272 (cp_parser *);
275 /* Manifest constants. */
276 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
277 #define CP_SAVED_TOKEN_STACK 5
279 /* Variables. */
281 /* The stream to which debugging output should be written. */
282 static FILE *cp_lexer_debug_stream;
284 /* Nonzero if we are parsing an unevaluated operand: an operand to
285 sizeof, typeof, or alignof. */
286 int cp_unevaluated_operand;
288 /* Dump up to NUM tokens in BUFFER to FILE starting with token
289 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
290 first token in BUFFER. If NUM is 0, dump all the tokens. If
291 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
292 highlighted by surrounding it in [[ ]]. */
294 static void
295 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
296 cp_token *start_token, unsigned num,
297 cp_token *curr_token)
299 unsigned i, nprinted;
300 cp_token *token;
301 bool do_print;
303 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
305 if (buffer == NULL)
306 return;
308 if (num == 0)
309 num = buffer->length ();
311 if (start_token == NULL)
312 start_token = buffer->address ();
314 if (start_token > buffer->address ())
316 cp_lexer_print_token (file, &(*buffer)[0]);
317 fprintf (file, " ... ");
320 do_print = false;
321 nprinted = 0;
322 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
324 if (token == start_token)
325 do_print = true;
327 if (!do_print)
328 continue;
330 nprinted++;
331 if (token == curr_token)
332 fprintf (file, "[[");
334 cp_lexer_print_token (file, token);
336 if (token == curr_token)
337 fprintf (file, "]]");
339 switch (token->type)
341 case CPP_SEMICOLON:
342 case CPP_OPEN_BRACE:
343 case CPP_CLOSE_BRACE:
344 case CPP_EOF:
345 fputc ('\n', file);
346 break;
348 default:
349 fputc (' ', file);
353 if (i == num && i < buffer->length ())
355 fprintf (file, " ... ");
356 cp_lexer_print_token (file, &buffer->last ());
359 fprintf (file, "\n");
363 /* Dump all tokens in BUFFER to stderr. */
365 void
366 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
368 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
371 DEBUG_FUNCTION void
372 debug (vec<cp_token, va_gc> &ref)
374 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
377 DEBUG_FUNCTION void
378 debug (vec<cp_token, va_gc> *ptr)
380 if (ptr)
381 debug (*ptr);
382 else
383 fprintf (stderr, "<nil>\n");
387 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
388 description for T. */
390 static void
391 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
393 if (t)
395 fprintf (file, "%s: ", desc);
396 print_node_brief (file, "", t, 0);
401 /* Dump parser context C to FILE. */
403 static void
404 cp_debug_print_context (FILE *file, cp_parser_context *c)
406 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
407 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
408 print_node_brief (file, "", c->object_type, 0);
409 fprintf (file, "}\n");
413 /* Print the stack of parsing contexts to FILE starting with FIRST. */
415 static void
416 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
418 unsigned i;
419 cp_parser_context *c;
421 fprintf (file, "Parsing context stack:\n");
422 for (i = 0, c = first; c; c = c->next, i++)
424 fprintf (file, "\t#%u: ", i);
425 cp_debug_print_context (file, c);
430 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
432 static void
433 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
435 if (flag)
436 fprintf (file, "%s: true\n", desc);
440 /* Print an unparsed function entry UF to FILE. */
442 static void
443 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
445 unsigned i;
446 cp_default_arg_entry *default_arg_fn;
447 tree fn;
449 fprintf (file, "\tFunctions with default args:\n");
450 for (i = 0;
451 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
452 i++)
454 fprintf (file, "\t\tClass type: ");
455 print_node_brief (file, "", default_arg_fn->class_type, 0);
456 fprintf (file, "\t\tDeclaration: ");
457 print_node_brief (file, "", default_arg_fn->decl, 0);
458 fprintf (file, "\n");
461 fprintf (file, "\n\tFunctions with definitions that require "
462 "post-processing\n\t\t");
463 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
465 print_node_brief (file, "", fn, 0);
466 fprintf (file, " ");
468 fprintf (file, "\n");
470 fprintf (file, "\n\tNon-static data members with initializers that require "
471 "post-processing\n\t\t");
472 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
474 print_node_brief (file, "", fn, 0);
475 fprintf (file, " ");
477 fprintf (file, "\n");
481 /* Print the stack of unparsed member functions S to FILE. */
483 static void
484 cp_debug_print_unparsed_queues (FILE *file,
485 vec<cp_unparsed_functions_entry, va_gc> *s)
487 unsigned i;
488 cp_unparsed_functions_entry *uf;
490 fprintf (file, "Unparsed functions\n");
491 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
493 fprintf (file, "#%u:\n", i);
494 cp_debug_print_unparsed_function (file, uf);
499 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
500 the given PARSER. If FILE is NULL, the output is printed on stderr. */
502 static void
503 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
505 cp_token *next_token, *first_token, *start_token;
507 if (file == NULL)
508 file = stderr;
510 next_token = parser->lexer->next_token;
511 first_token = parser->lexer->buffer->address ();
512 start_token = (next_token > first_token + window_size / 2)
513 ? next_token - window_size / 2
514 : first_token;
515 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
516 next_token);
520 /* Dump debugging information for the given PARSER. If FILE is NULL,
521 the output is printed on stderr. */
523 void
524 cp_debug_parser (FILE *file, cp_parser *parser)
526 const size_t window_size = 20;
527 cp_token *token;
528 expanded_location eloc;
530 if (file == NULL)
531 file = stderr;
533 fprintf (file, "Parser state\n\n");
534 fprintf (file, "Number of tokens: %u\n",
535 vec_safe_length (parser->lexer->buffer));
536 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
537 cp_debug_print_tree_if_set (file, "Object scope",
538 parser->object_scope);
539 cp_debug_print_tree_if_set (file, "Qualifying scope",
540 parser->qualifying_scope);
541 cp_debug_print_context_stack (file, parser->context);
542 cp_debug_print_flag (file, "Allow GNU extensions",
543 parser->allow_gnu_extensions_p);
544 cp_debug_print_flag (file, "'>' token is greater-than",
545 parser->greater_than_is_operator_p);
546 cp_debug_print_flag (file, "Default args allowed in current "
547 "parameter list", parser->default_arg_ok_p);
548 cp_debug_print_flag (file, "Parsing integral constant-expression",
549 parser->integral_constant_expression_p);
550 cp_debug_print_flag (file, "Allow non-constant expression in current "
551 "constant-expression",
552 parser->allow_non_integral_constant_expression_p);
553 cp_debug_print_flag (file, "Seen non-constant expression",
554 parser->non_integral_constant_expression_p);
555 cp_debug_print_flag (file, "Local names forbidden in current context",
556 (parser->local_variables_forbidden_p
557 & LOCAL_VARS_FORBIDDEN));
558 cp_debug_print_flag (file, "'this' forbidden in current context",
559 (parser->local_variables_forbidden_p
560 & THIS_FORBIDDEN));
561 cp_debug_print_flag (file, "In unbraced linkage specification",
562 parser->in_unbraced_linkage_specification_p);
563 cp_debug_print_flag (file, "Parsing a declarator",
564 parser->in_declarator_p);
565 cp_debug_print_flag (file, "In template argument list",
566 parser->in_template_argument_list_p);
567 cp_debug_print_flag (file, "Parsing an iteration statement",
568 parser->in_statement & IN_ITERATION_STMT);
569 cp_debug_print_flag (file, "Parsing a switch statement",
570 parser->in_statement & IN_SWITCH_STMT);
571 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
572 parser->in_statement & IN_OMP_BLOCK);
573 cp_debug_print_flag (file, "Parsing an OpenMP loop",
574 parser->in_statement & IN_OMP_FOR);
575 cp_debug_print_flag (file, "Parsing an if statement",
576 parser->in_statement & IN_IF_STMT);
577 cp_debug_print_flag (file, "Parsing a type-id in an expression "
578 "context", parser->in_type_id_in_expr_p);
579 cp_debug_print_flag (file, "String expressions should be translated "
580 "to execution character set",
581 parser->translate_strings_p);
582 cp_debug_print_flag (file, "Parsing function body outside of a "
583 "local class", parser->in_function_body);
584 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
585 parser->colon_corrects_to_scope_p);
586 cp_debug_print_flag (file, "Colon doesn't start a class definition",
587 parser->colon_doesnt_start_class_def_p);
588 cp_debug_print_flag (file, "Parsing an Objective-C++ message context",
589 parser->objective_c_message_context_p);
590 if (parser->type_definition_forbidden_message)
591 fprintf (file, "Error message for forbidden type definitions: %s %s\n",
592 parser->type_definition_forbidden_message,
593 parser->type_definition_forbidden_message_arg
594 ? parser->type_definition_forbidden_message_arg : "<none>");
595 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
596 fprintf (file, "Number of class definitions in progress: %u\n",
597 parser->num_classes_being_defined);
598 fprintf (file, "Number of template parameter lists for the current "
599 "declaration: %u\n", parser->num_template_parameter_lists);
600 cp_debug_parser_tokens (file, parser, window_size);
601 token = parser->lexer->next_token;
602 fprintf (file, "Next token to parse:\n");
603 fprintf (file, "\tToken: ");
604 cp_lexer_print_token (file, token);
605 eloc = expand_location (token->location);
606 fprintf (file, "\n\tFile: %s\n", eloc.file);
607 fprintf (file, "\tLine: %d\n", eloc.line);
608 fprintf (file, "\tColumn: %d\n", eloc.column);
611 DEBUG_FUNCTION void
612 debug (cp_parser &ref)
614 cp_debug_parser (stderr, &ref);
617 DEBUG_FUNCTION void
618 debug (cp_parser *ptr)
620 if (ptr)
621 debug (*ptr);
622 else
623 fprintf (stderr, "<nil>\n");
626 /* Allocate memory for a new lexer object and return it. */
628 static cp_lexer *
629 cp_lexer_alloc (void)
631 /* Allocate the memory. */
632 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
634 /* Initially we are not debugging. */
635 lexer->debugging_p = false;
637 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
639 /* Create the buffer. */
640 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
642 return lexer;
645 /* Return TRUE if token is the start of a module declaration that will be
646 terminated by a CPP_PRAGMA_EOL token. */
647 static inline bool
648 cp_token_is_module_directive (cp_token *token)
650 return token->keyword == RID__EXPORT
651 || token->keyword == RID__MODULE
652 || token->keyword == RID__IMPORT;
655 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
656 PRAGMA_NONE. */
658 static enum pragma_kind
659 cp_parser_pragma_kind (cp_token *token)
661 if (token->type != CPP_PRAGMA)
662 return PRAGMA_NONE;
663 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
664 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
667 /* Handle early pragmas such as #pragma GCC diagnostic, which needs to be done
668 during preprocessing for the case of preprocessing-related diagnostics. This
669 is called immediately after pushing the CPP_PRAGMA_EOL token onto
670 lexer->buffer. */
672 static void
673 cp_lexer_handle_early_pragma (cp_lexer *lexer)
675 const auto first_token = lexer->buffer->address ();
676 const auto last_token = first_token + lexer->buffer->length () - 1;
678 /* Back up to the start of the pragma so pragma_lex () can parse it when
679 c-pragma lib asks it to. */
680 auto begin = last_token;
681 gcc_assert (begin->type == CPP_PRAGMA_EOL);
682 while (begin->type != CPP_PRAGMA)
684 if (cp_token_is_module_directive (begin))
685 return;
686 gcc_assert (begin != first_token);
687 --begin;
689 gcc_assert (!lexer->next_token);
690 gcc_assert (!lexer->last_token);
691 lexer->next_token = begin;
692 lexer->last_token = last_token;
694 /* Dispatch it. */
695 const unsigned int id
696 = cp_parser_pragma_kind (cp_lexer_consume_token (lexer));
697 if (id >= PRAGMA_FIRST_EXTERNAL)
698 c_invoke_early_pragma_handler (id);
700 /* Reset to normal state. */
701 lexer->next_token = lexer->last_token = nullptr;
704 /* The parser. */
705 static cp_parser *cp_parser_new (cp_lexer *);
706 static GTY (()) cp_parser *the_parser;
708 /* Create a new main C++ lexer, the lexer that gets tokens from the
709 preprocessor, and also create the main parser. */
711 static cp_lexer *
712 cp_lexer_new_main (void)
714 cp_token token;
716 /* It's possible that parsing the first pragma will load a PCH file,
717 which is a GC collection point. So we have to do that before
718 allocating any memory. */
719 cp_lexer_get_preprocessor_token (C_LEX_STRING_NO_JOIN, &token);
720 cp_parser_initial_pragma (&token);
721 c_common_no_more_pch ();
723 cp_lexer *lexer = cp_lexer_alloc ();
724 /* Put the first token in the buffer. */
725 cp_token *tok = lexer->buffer->quick_push (token);
727 uintptr_t filter = 0;
728 if (modules_p ())
729 filter = module_token_cdtor (parse_in, filter);
731 /* Create the parser now, so we can use it to handle early pragmas. */
732 gcc_assert (!the_parser);
733 the_parser = cp_parser_new (lexer);
735 /* Get the remaining tokens from the preprocessor. */
736 while (tok->type != CPP_EOF)
738 if (filter)
739 /* Process the previous token. */
740 module_token_lang (tok->type, tok->keyword, tok->u.value,
741 tok->location, filter);
743 /* Check for early pragmas that need to be handled now. */
744 if (tok->type == CPP_PRAGMA_EOL)
745 cp_lexer_handle_early_pragma (lexer);
747 tok = vec_safe_push (lexer->buffer, cp_token ());
748 cp_lexer_get_preprocessor_token (C_LEX_STRING_NO_JOIN, tok);
751 lexer->next_token = lexer->buffer->address ();
752 lexer->last_token = lexer->next_token
753 + lexer->buffer->length ()
754 - 1;
756 if (lexer->buffer->length () != 1)
758 /* Set the EOF token's location to be the just after the previous
759 token's range. That way 'at-eof' diagnostics point at something
760 meaninful. */
761 auto range = get_range_from_loc (line_table, tok[-1].location);
762 tok[0].location
763 = linemap_position_for_loc_and_offset (line_table, range.m_finish, 1);
766 if (filter)
767 module_token_cdtor (parse_in, filter);
769 /* Subsequent preprocessor diagnostics should use compiler
770 diagnostic functions to get the compiler source location. */
771 override_libcpp_locations = true;
773 maybe_check_all_macros (parse_in);
775 /* If we processed any #pragma GCC target directives, we handled them early so
776 any macros they defined would be effective during preprocessing. Now, we
777 need to reset to the default state to begin compilation, and we will
778 process them again at the correct time as needed. */
779 c_reset_target_pragmas ();
781 gcc_assert (!lexer->next_token->purged_p);
782 return lexer;
785 /* Create a lexer and parser to be used during preprocess-only mode.
786 This will be filled with tokens to parse when needed by pragma_lex (). */
787 void
788 c_init_preprocess ()
790 gcc_assert (!the_parser);
791 the_parser = cp_parser_new (cp_lexer_alloc ());
794 /* Create a new lexer whose token stream is primed with the tokens in
795 CACHE. When these tokens are exhausted, no new tokens will be read. */
797 static cp_lexer *
798 cp_lexer_new_from_tokens (cp_token_cache *cache)
800 cp_token *first = cache->first;
801 cp_token *last = cache->last;
802 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
804 /* We do not own the buffer. */
805 lexer->buffer = NULL;
807 /* Insert an EOF token. */
808 lexer->saved_type = last->type;
809 lexer->saved_keyword = last->keyword;
810 last->type = CPP_EOF;
811 last->keyword = RID_MAX;
813 lexer->next_token = first;
814 lexer->last_token = last;
816 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
818 /* Initially we are not debugging. */
819 lexer->debugging_p = false;
821 gcc_assert (!lexer->next_token->purged_p
822 && !lexer->last_token->purged_p);
823 return lexer;
826 /* Frees all resources associated with LEXER. */
828 static void
829 cp_lexer_destroy (cp_lexer *lexer)
831 if (lexer->buffer)
832 vec_free (lexer->buffer);
833 else
835 /* Restore the token we overwrite with EOF. */
836 lexer->last_token->type = lexer->saved_type;
837 lexer->last_token->keyword = lexer->saved_keyword;
839 lexer->saved_tokens.release ();
840 ggc_free (lexer);
843 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
844 be used. The point of this flag is to help the compiler to fold away calls
845 to cp_lexer_debugging_p within this source file at compile time, when the
846 lexer is not being debugged. */
848 #define LEXER_DEBUGGING_ENABLED_P false
850 /* Returns nonzero if debugging information should be output. */
852 static inline bool
853 cp_lexer_debugging_p (cp_lexer *lexer)
855 if (!LEXER_DEBUGGING_ENABLED_P)
856 return false;
858 return lexer->debugging_p;
862 static inline cp_token_position
863 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
865 return lexer->next_token - previous_p;
868 static inline cp_token *
869 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
871 return pos;
874 static inline void
875 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
877 lexer->next_token = cp_lexer_token_at (lexer, pos);
880 static inline cp_token_position
881 cp_lexer_previous_token_position (cp_lexer *lexer)
883 return cp_lexer_token_position (lexer, true);
886 static inline cp_token *
887 cp_lexer_previous_token (cp_lexer *lexer)
889 cp_token_position tp = cp_lexer_previous_token_position (lexer);
891 /* Skip past purged tokens. */
892 while (tp->purged_p)
894 gcc_assert (tp != vec_safe_address (lexer->buffer));
895 tp--;
898 return cp_lexer_token_at (lexer, tp);
901 /* Same as above, but return NULL when the lexer doesn't own the token
902 buffer or if the next_token is at the start of the token
903 vector or if all previous tokens are purged. */
905 static cp_token *
906 cp_lexer_safe_previous_token (cp_lexer *lexer)
908 if (lexer->buffer
909 && lexer->next_token != lexer->buffer->address ())
911 cp_token_position tp = cp_lexer_previous_token_position (lexer);
913 /* Skip past purged tokens. */
914 while (tp->purged_p)
916 if (tp == lexer->buffer->address ())
917 return NULL;
918 tp--;
920 return cp_lexer_token_at (lexer, tp);
923 return NULL;
926 /* Overload for make_location, taking the lexer to mean the location of the
927 previous token. */
929 static inline location_t
930 make_location (location_t caret, location_t start, cp_lexer *lexer)
932 cp_token *t = cp_lexer_previous_token (lexer);
933 return make_location (caret, start, t->location);
936 /* Overload for make_location taking tokens instead of locations. */
938 static inline location_t
939 make_location (cp_token *caret, cp_token *start, cp_token *end)
941 return make_location (caret->location, start->location, end->location);
944 /* nonzero if we are presently saving tokens. */
946 static inline int
947 cp_lexer_saving_tokens (const cp_lexer* lexer)
949 return lexer->saved_tokens.length () != 0;
952 /* Store the next token from the preprocessor in *TOKEN. Return true
953 if we reach EOF. If LEXER is NULL, assume we are handling an
954 initial #pragma pch_preprocess, and thus want the lexer to return
955 processed strings.
957 Diagnostics issued from this function must have their controlling option (if
958 any) in c.opt annotated as a libcpp option via the CppReason property. */
960 static void
961 cp_lexer_get_preprocessor_token (unsigned flags, cp_token *token)
963 static int is_extern_c = 0;
965 /* Get a new token from the preprocessor. */
966 token->type
967 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
968 flags);
969 token->keyword = RID_MAX;
970 token->purged_p = false;
971 token->error_reported = false;
972 token->tree_check_p = false;
973 /* Usually never see a zero, but just in case ... */
974 token->main_source_p = line_table->depth <= 1;
976 /* On some systems, some header files are surrounded by an
977 implicit extern "C" block. Set a flag in the token if it
978 comes from such a header. */
979 is_extern_c += pending_lang_change;
980 pending_lang_change = 0;
981 token->implicit_extern_c = is_extern_c > 0;
983 /* Check to see if this token is a keyword. */
984 if (token->type == CPP_NAME)
986 if (IDENTIFIER_KEYWORD_P (token->u.value))
988 /* Mark this token as a keyword. */
989 token->type = CPP_KEYWORD;
990 /* Record which keyword. */
991 token->keyword = C_RID_CODE (token->u.value);
993 else
995 if (warn_cxx11_compat
996 && ((C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
997 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
998 /* These are outside the CXX11 range. */
999 || C_RID_CODE (token->u.value) == RID_ALIGNOF
1000 || C_RID_CODE (token->u.value) == RID_ALIGNAS
1001 || C_RID_CODE (token->u.value)== RID_THREAD))
1003 /* Warn about the C++11 keyword (but still treat it as
1004 an identifier). */
1005 warning_at (token->location, OPT_Wc__11_compat,
1006 "identifier %qE is a keyword in C++11",
1007 token->u.value);
1009 /* Clear out the C_RID_CODE so we don't warn about this
1010 particular identifier-turned-keyword again. */
1011 C_SET_RID_CODE (token->u.value, RID_MAX);
1013 if (warn_cxx20_compat
1014 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX20
1015 && C_RID_CODE (token->u.value) <= RID_LAST_CXX20)
1017 /* Warn about the C++20 keyword (but still treat it as
1018 an identifier). */
1019 warning_at (token->location, OPT_Wc__20_compat,
1020 "identifier %qE is a keyword in C++20",
1021 token->u.value);
1023 /* Clear out the C_RID_CODE so we don't warn about this
1024 particular identifier-turned-keyword again. */
1025 C_SET_RID_CODE (token->u.value, RID_MAX);
1028 token->keyword = RID_MAX;
1031 else if (token->type == CPP_AT_NAME)
1033 /* This only happens in Objective-C++; it must be a keyword. */
1034 token->type = CPP_KEYWORD;
1035 switch (C_RID_CODE (token->u.value))
1037 /* Replace 'class' with '@class', 'private' with '@private',
1038 etc. This prevents confusion with the C++ keyword
1039 'class', and makes the tokens consistent with other
1040 Objective-C 'AT' keywords. For example '@class' is
1041 reported as RID_AT_CLASS which is consistent with
1042 '@synchronized', which is reported as
1043 RID_AT_SYNCHRONIZED.
1045 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
1046 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
1047 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
1048 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
1049 case RID_THROW: token->keyword = RID_AT_THROW; break;
1050 case RID_TRY: token->keyword = RID_AT_TRY; break;
1051 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
1052 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
1053 default: token->keyword = C_RID_CODE (token->u.value);
1058 /* Update the globals input_location and the input file stack from TOKEN. */
1059 static inline void
1060 cp_lexer_set_source_position_from_token (cp_token *token)
1062 input_location = token->location;
1065 /* Update the globals input_location and the input file stack from LEXER. */
1066 static inline void
1067 cp_lexer_set_source_position (cp_lexer *lexer)
1069 cp_token *token = cp_lexer_peek_token (lexer);
1070 cp_lexer_set_source_position_from_token (token);
1073 /* Return a pointer to the next token in the token stream, but do not
1074 consume it. */
1076 static inline cp_token *
1077 cp_lexer_peek_token (cp_lexer *lexer)
1079 if (cp_lexer_debugging_p (lexer))
1081 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
1082 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
1083 putc ('\n', cp_lexer_debug_stream);
1085 return lexer->next_token;
1088 /* Return true if the next token has the indicated TYPE. */
1090 static inline bool
1091 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
1093 return cp_lexer_peek_token (lexer)->type == type;
1096 /* Return true if the next token does not have the indicated TYPE. */
1098 static inline bool
1099 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
1101 return !cp_lexer_next_token_is (lexer, type);
1104 /* Return true if the next token is the indicated KEYWORD. */
1106 static inline bool
1107 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
1109 return cp_lexer_peek_token (lexer)->keyword == keyword;
1112 static inline bool
1113 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
1115 return cp_lexer_peek_nth_token (lexer, n)->type == type;
1118 static inline bool
1119 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
1121 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
1124 /* Return true if KEYWORD can start a decl-specifier. */
1126 bool
1127 cp_keyword_starts_decl_specifier_p (enum rid keyword)
1129 switch (keyword)
1131 /* auto specifier: storage-class-specifier in C++,
1132 simple-type-specifier in C++0x. */
1133 case RID_AUTO:
1134 /* Storage classes. */
1135 case RID_REGISTER:
1136 case RID_STATIC:
1137 case RID_EXTERN:
1138 case RID_MUTABLE:
1139 case RID_THREAD:
1140 /* Elaborated type specifiers. */
1141 case RID_ENUM:
1142 case RID_CLASS:
1143 case RID_STRUCT:
1144 case RID_UNION:
1145 case RID_TYPENAME:
1146 /* Simple type specifiers. */
1147 case RID_CHAR:
1148 case RID_CHAR8:
1149 case RID_CHAR16:
1150 case RID_CHAR32:
1151 case RID_WCHAR:
1152 case RID_BOOL:
1153 case RID_SHORT:
1154 case RID_INT:
1155 case RID_LONG:
1156 case RID_SIGNED:
1157 case RID_UNSIGNED:
1158 case RID_FLOAT:
1159 case RID_DOUBLE:
1160 CASE_RID_FLOATN_NX:
1161 case RID_VOID:
1162 /* CV qualifiers. */
1163 case RID_CONST:
1164 case RID_VOLATILE:
1165 /* Function specifiers. */
1166 case RID_EXPLICIT:
1167 case RID_VIRTUAL:
1168 /* friend/typdef/inline specifiers. */
1169 case RID_FRIEND:
1170 case RID_TYPEDEF:
1171 case RID_INLINE:
1172 /* GNU extensions. */
1173 case RID_TYPEOF:
1174 /* C++11 extensions. */
1175 case RID_DECLTYPE:
1176 case RID_CONSTEXPR:
1177 /* C++20 extensions. */
1178 case RID_CONSTINIT:
1179 case RID_CONSTEVAL:
1180 return true;
1182 default:
1183 if (keyword >= RID_FIRST_INT_N
1184 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
1185 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
1186 return true;
1187 return false;
1191 /* Peeks the corresponding built-in trait if the first token is
1192 a built-in trait and the second token is either `(' or `<' depending
1193 on the trait. Otherwise, returns nullptr. */
1195 static const cp_trait *
1196 cp_lexer_peek_trait (cp_lexer *lexer)
1198 const cp_token *token1 = cp_lexer_peek_token (lexer);
1199 if (token1->type == CPP_NAME && IDENTIFIER_TRAIT_P (token1->u.value))
1201 const cp_trait &trait = cp_traits[IDENTIFIER_CP_INDEX (token1->u.value)];
1202 const bool is_pack_element = (trait.kind == CPTK_TYPE_PACK_ELEMENT);
1204 /* Check if the subsequent token is a `<' token to
1205 __type_pack_element or is a `(' token to everything else. */
1206 const cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
1207 if (is_pack_element && token2->type != CPP_LESS)
1208 return nullptr;
1209 if (!is_pack_element && token2->type != CPP_OPEN_PAREN)
1210 return nullptr;
1212 return &trait;
1214 return nullptr;
1217 /* Similarly, but only if the token is an expression-yielding
1218 built-in trait. */
1220 static const cp_trait *
1221 cp_lexer_peek_trait_expr (cp_lexer *lexer)
1223 const cp_trait *trait = cp_lexer_peek_trait (lexer);
1224 if (trait && !trait->type)
1225 return trait;
1227 return nullptr;
1230 /* Similarly, but only if the token is a type-yielding
1231 built-in trait. */
1233 static const cp_trait *
1234 cp_lexer_peek_trait_type (cp_lexer *lexer)
1236 const cp_trait *trait = cp_lexer_peek_trait (lexer);
1237 if (trait && trait->type)
1238 return trait;
1240 return nullptr;
1243 /* Return true if the next token is a keyword for a decl-specifier. */
1245 static bool
1246 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
1248 cp_token *token;
1250 if (cp_lexer_peek_trait_type (lexer))
1251 return true;
1253 token = cp_lexer_peek_token (lexer);
1254 return cp_keyword_starts_decl_specifier_p (token->keyword);
1257 /* Returns TRUE iff the token T begins a decltype type. */
1259 static bool
1260 token_is_decltype (cp_token *t)
1262 return (t->keyword == RID_DECLTYPE
1263 || t->type == CPP_DECLTYPE);
1266 /* Returns TRUE iff the next token begins a decltype type. */
1268 static bool
1269 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1271 cp_token *t = cp_lexer_peek_token (lexer);
1272 return token_is_decltype (t);
1275 /* Called when processing a token with tree_check_value; perform or defer the
1276 associated checks and return the value. */
1278 static tree
1279 saved_checks_value (struct tree_check *check_value)
1281 /* Perform any access checks that were deferred. */
1282 vec<deferred_access_check, va_gc> *checks;
1283 deferred_access_check *chk;
1284 checks = check_value->checks;
1285 if (checks)
1287 int i;
1288 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1289 perform_or_defer_access_check (chk->binfo,
1290 chk->decl,
1291 chk->diag_decl, tf_warning_or_error);
1293 /* Return the stored value. */
1294 return check_value->value;
1297 /* Return a pointer to the Nth token in the token stream. If N is 1,
1298 then this is precisely equivalent to cp_lexer_peek_token (except
1299 that it is not inline). One would like to disallow that case, but
1300 there is one case (cp_parser_nth_token_starts_template_id) where
1301 the caller passes a variable for N and it might be 1. */
1303 static cp_token *
1304 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1306 cp_token *token;
1308 /* N is 1-based, not zero-based. */
1309 gcc_assert (n > 0);
1311 if (cp_lexer_debugging_p (lexer))
1312 fprintf (cp_lexer_debug_stream,
1313 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1315 --n;
1316 token = lexer->next_token;
1317 while (n && token->type != CPP_EOF)
1319 ++token;
1320 if (!token->purged_p)
1321 --n;
1324 if (cp_lexer_debugging_p (lexer))
1326 cp_lexer_print_token (cp_lexer_debug_stream, token);
1327 putc ('\n', cp_lexer_debug_stream);
1330 return token;
1333 /* Return the next token, and advance the lexer's next_token pointer
1334 to point to the next non-purged token. */
1336 static cp_token *
1337 cp_lexer_consume_token (cp_lexer* lexer)
1339 cp_token *token = lexer->next_token;
1343 gcc_assert (token->type != CPP_EOF);
1344 lexer->next_token++;
1346 while (lexer->next_token->purged_p);
1348 cp_lexer_set_source_position_from_token (token);
1350 /* Provide debugging output. */
1351 if (cp_lexer_debugging_p (lexer))
1353 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1354 cp_lexer_print_token (cp_lexer_debug_stream, token);
1355 putc ('\n', cp_lexer_debug_stream);
1358 return token;
1361 /* Permanently remove the next token from the token stream, and
1362 advance the next_token pointer to refer to the next non-purged
1363 token. */
1365 static void
1366 cp_lexer_purge_token (cp_lexer *lexer)
1368 cp_token *tok = lexer->next_token;
1370 gcc_assert (tok->type != CPP_EOF);
1371 tok->purged_p = true;
1372 tok->location = UNKNOWN_LOCATION;
1373 tok->u.value = NULL_TREE;
1374 tok->keyword = RID_MAX;
1377 tok++;
1378 while (tok->purged_p);
1379 lexer->next_token = tok;
1382 /* Permanently remove all tokens after TOK, up to, but not
1383 including, the token that will be returned next by
1384 cp_lexer_peek_token. */
1386 static void
1387 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1389 cp_token *peek = lexer->next_token;
1391 gcc_assert (tok < peek);
1393 for (tok++; tok != peek; tok++)
1395 tok->purged_p = true;
1396 tok->location = UNKNOWN_LOCATION;
1397 tok->u.value = NULL_TREE;
1398 tok->keyword = RID_MAX;
1402 /* Begin saving tokens. All tokens consumed after this point will be
1403 preserved. */
1405 static void
1406 cp_lexer_save_tokens (cp_lexer* lexer)
1408 /* Provide debugging output. */
1409 if (cp_lexer_debugging_p (lexer))
1410 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1412 lexer->saved_tokens.safe_push (lexer->next_token);
1415 /* Commit to the portion of the token stream most recently saved. */
1417 static void
1418 cp_lexer_commit_tokens (cp_lexer* lexer)
1420 /* Provide debugging output. */
1421 if (cp_lexer_debugging_p (lexer))
1422 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1424 lexer->saved_tokens.pop ();
1427 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1428 to the token stream. Stop saving tokens. */
1430 static void
1431 cp_lexer_rollback_tokens (cp_lexer* lexer)
1433 /* Provide debugging output. */
1434 if (cp_lexer_debugging_p (lexer))
1435 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1437 lexer->next_token = lexer->saved_tokens.pop ();
1440 /* Determines what saved_token_sentinel does when going out of scope. */
1442 enum saved_token_sentinel_mode {
1443 STS_COMMIT,
1444 STS_ROLLBACK,
1445 STS_DONOTHING
1448 /* RAII wrapper around the above functions, with sanity checking (the token
1449 stream should be the same at the point of instantiation as it is at the
1450 point of destruction).
1452 Creating a variable saves tokens. MODE determines what happens when the
1453 object is destroyed. STS_COMMIT commits tokens (default),
1454 STS_ROLLBACK rolls-back and STS_DONOTHING does nothing. Calling
1455 rollback() will immediately roll-back tokens and set MODE to
1456 STS_DONOTHING. */
1458 struct saved_token_sentinel
1460 cp_lexer *lexer;
1461 unsigned len;
1462 saved_token_sentinel_mode mode;
1463 saved_token_sentinel (cp_lexer *_lexer,
1464 saved_token_sentinel_mode _mode = STS_COMMIT)
1465 : lexer (_lexer), mode (_mode)
1467 len = lexer->saved_tokens.length ();
1468 cp_lexer_save_tokens (lexer);
1470 void rollback ()
1472 cp_lexer_rollback_tokens (lexer);
1473 cp_lexer_set_source_position_from_token
1474 (cp_lexer_previous_token (lexer));
1475 mode = STS_DONOTHING;
1477 ~saved_token_sentinel ()
1479 if (mode == STS_COMMIT)
1480 cp_lexer_commit_tokens (lexer);
1481 else if (mode == STS_ROLLBACK)
1482 rollback ();
1484 gcc_assert (lexer->saved_tokens.length () == len);
1488 /* Print a representation of the TOKEN on the STREAM. */
1490 static void
1491 cp_lexer_print_token (FILE * stream, cp_token *token)
1493 /* We don't use cpp_type2name here because the parser defines
1494 a few tokens of its own. */
1495 static const char *const token_names[] = {
1496 /* cpplib-defined token types */
1497 #define OP(e, s) #e,
1498 #define TK(e, s) #e,
1499 TTYPE_TABLE
1500 #undef OP
1501 #undef TK
1502 /* C++ parser token types - see "Manifest constants", above. */
1503 "KEYWORD",
1504 "TEMPLATE_ID",
1505 "NESTED_NAME_SPECIFIER",
1508 /* For some tokens, print the associated data. */
1509 switch (token->type)
1511 case CPP_KEYWORD:
1512 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1513 For example, `struct' is mapped to an INTEGER_CST. */
1514 if (!identifier_p (token->u.value))
1515 break;
1516 /* fall through */
1517 case CPP_NAME:
1518 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1519 break;
1521 case CPP_STRING:
1522 case CPP_STRING16:
1523 case CPP_STRING32:
1524 case CPP_WSTRING:
1525 case CPP_UTF8STRING:
1526 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1527 break;
1529 case CPP_NUMBER:
1530 print_generic_expr (stream, token->u.value);
1531 break;
1533 default:
1534 /* If we have a name for the token, print it out. Otherwise, we
1535 simply give the numeric code. */
1536 if (token->type < ARRAY_SIZE(token_names))
1537 fputs (token_names[token->type], stream);
1538 else
1539 fprintf (stream, "[%d]", token->type);
1540 break;
1544 DEBUG_FUNCTION void
1545 debug (cp_token &ref)
1547 cp_lexer_print_token (stderr, &ref);
1548 fprintf (stderr, "\n");
1551 DEBUG_FUNCTION void
1552 debug (cp_token *ptr)
1554 if (ptr)
1555 debug (*ptr);
1556 else
1557 fprintf (stderr, "<nil>\n");
1561 /* Start emitting debugging information. */
1563 static void
1564 cp_lexer_start_debugging (cp_lexer* lexer)
1566 if (!LEXER_DEBUGGING_ENABLED_P)
1567 fatal_error (input_location,
1568 "%<LEXER_DEBUGGING_ENABLED_P%> is not set to true");
1570 lexer->debugging_p = true;
1571 cp_lexer_debug_stream = stderr;
1574 /* Stop emitting debugging information. */
1576 static void
1577 cp_lexer_stop_debugging (cp_lexer* lexer)
1579 if (!LEXER_DEBUGGING_ENABLED_P)
1580 fatal_error (input_location,
1581 "%<LEXER_DEBUGGING_ENABLED_P%> is not set to true");
1583 lexer->debugging_p = false;
1584 cp_lexer_debug_stream = NULL;
1587 /* Create a new cp_token_cache, representing a range of tokens. */
1589 static cp_token_cache *
1590 cp_token_cache_new (cp_token *first, cp_token *last)
1592 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1593 cache->first = first;
1594 cache->last = last;
1595 return cache;
1598 /* Diagnose if #pragma omp declare simd isn't followed immediately
1599 by function declaration or definition. */
1601 static inline void
1602 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1604 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1606 error ("%<#pragma omp declare %s%> not immediately followed by "
1607 "function declaration or definition",
1608 parser->omp_declare_simd->variant_p ? "variant" : "simd");
1609 parser->omp_declare_simd = NULL;
1613 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1614 and put that into "omp declare simd" attribute. */
1616 static inline void
1617 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1619 if (UNLIKELY (parser->omp_declare_simd != NULL))
1621 if (fndecl == error_mark_node)
1623 parser->omp_declare_simd = NULL;
1624 return;
1626 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1628 cp_ensure_no_omp_declare_simd (parser);
1629 return;
1634 /* Similarly, but for use in declaration parsing functions
1635 which call cp_parser_handle_directive_omp_attributes. */
1637 static inline void
1638 cp_finalize_omp_declare_simd (cp_parser *parser, cp_omp_declare_simd_data *data)
1640 if (parser->omp_declare_simd != data)
1641 return;
1643 if (!parser->omp_declare_simd->error_seen
1644 && !parser->omp_declare_simd->fndecl_seen)
1645 error_at (parser->omp_declare_simd->loc,
1646 "%<declare %s%> directive not immediately followed by "
1647 "function declaration or definition",
1648 parser->omp_declare_simd->variant_p ? "variant" : "simd");
1649 parser->omp_declare_simd = NULL;
1652 /* Diagnose if #pragma acc routine isn't followed immediately by function
1653 declaration or definition. */
1655 static inline void
1656 cp_ensure_no_oacc_routine (cp_parser *parser)
1658 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1660 error_at (parser->oacc_routine->loc,
1661 "%<#pragma acc routine%> not immediately followed by "
1662 "function declaration or definition");
1663 parser->oacc_routine = NULL;
1667 /* Decl-specifiers. */
1669 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1671 static void
1672 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1674 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1677 /* Declarators. */
1679 /* Nothing other than the parser should be creating declarators;
1680 declarators are a semi-syntactic representation of C++ entities.
1681 Other parts of the front end that need to create entities (like
1682 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1684 static cp_declarator *make_call_declarator
1685 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier,
1686 tree, tree, tree, tree, tree, location_t);
1687 static cp_declarator *make_array_declarator
1688 (cp_declarator *, tree);
1689 static cp_declarator *make_pointer_declarator
1690 (cp_cv_quals, cp_declarator *, tree);
1691 static cp_declarator *make_reference_declarator
1692 (cp_cv_quals, cp_declarator *, bool, tree);
1693 static cp_declarator *make_ptrmem_declarator
1694 (cp_cv_quals, tree, cp_declarator *, tree);
1696 /* An erroneous declarator. */
1697 static cp_declarator *cp_error_declarator;
1699 /* The obstack on which declarators and related data structures are
1700 allocated. */
1701 static struct obstack declarator_obstack;
1703 /* Alloc BYTES from the declarator memory pool. */
1705 static inline void *
1706 alloc_declarator (size_t bytes)
1708 return obstack_alloc (&declarator_obstack, bytes);
1711 /* Allocate a declarator of the indicated KIND. Clear fields that are
1712 common to all declarators. */
1714 static cp_declarator *
1715 make_declarator (cp_declarator_kind kind)
1717 cp_declarator *declarator;
1719 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1720 declarator->kind = kind;
1721 declarator->parenthesized = UNKNOWN_LOCATION;
1722 declarator->attributes = NULL_TREE;
1723 declarator->std_attributes = NULL_TREE;
1724 declarator->declarator = NULL;
1725 declarator->parameter_pack_p = false;
1726 declarator->id_loc = UNKNOWN_LOCATION;
1727 declarator->init_loc = UNKNOWN_LOCATION;
1729 return declarator;
1732 /* Make a declarator for a generalized identifier. If
1733 QUALIFYING_SCOPE is non-NULL, the identifier is
1734 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1735 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1736 is, if any. */
1738 static cp_declarator *
1739 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1740 special_function_kind sfk, location_t id_location)
1742 cp_declarator *declarator;
1744 /* It is valid to write:
1746 class C { void f(); };
1747 typedef C D;
1748 void D::f();
1750 The standard is not clear about whether `typedef const C D' is
1751 legal; as of 2002-09-15 the committee is considering that
1752 question. EDG 3.0 allows that syntax. Therefore, we do as
1753 well. */
1754 if (qualifying_scope && TYPE_P (qualifying_scope))
1755 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1757 gcc_assert (identifier_p (unqualified_name)
1758 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1759 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1761 declarator = make_declarator (cdk_id);
1762 declarator->u.id.qualifying_scope = qualifying_scope;
1763 declarator->u.id.unqualified_name = unqualified_name;
1764 declarator->u.id.sfk = sfk;
1765 declarator->id_loc = id_location;
1767 return declarator;
1770 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1771 of modifiers such as const or volatile to apply to the pointer
1772 type, represented as identifiers. ATTRIBUTES represent the attributes that
1773 appertain to the pointer or reference. */
1775 cp_declarator *
1776 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1777 tree attributes)
1779 cp_declarator *declarator;
1781 declarator = make_declarator (cdk_pointer);
1782 declarator->declarator = target;
1783 declarator->u.pointer.qualifiers = cv_qualifiers;
1784 declarator->u.pointer.class_type = NULL_TREE;
1785 if (target)
1787 declarator->id_loc = target->id_loc;
1788 declarator->parameter_pack_p = target->parameter_pack_p;
1789 target->parameter_pack_p = false;
1791 else
1792 declarator->parameter_pack_p = false;
1794 declarator->std_attributes = attributes;
1796 return declarator;
1799 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1800 represent the attributes that appertain to the pointer or
1801 reference. */
1803 cp_declarator *
1804 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1805 bool rvalue_ref, tree attributes)
1807 cp_declarator *declarator;
1809 declarator = make_declarator (cdk_reference);
1810 declarator->declarator = target;
1811 declarator->u.reference.qualifiers = cv_qualifiers;
1812 declarator->u.reference.rvalue_ref = rvalue_ref;
1813 if (target)
1815 declarator->id_loc = target->id_loc;
1816 declarator->parameter_pack_p = target->parameter_pack_p;
1817 target->parameter_pack_p = false;
1819 else
1820 declarator->parameter_pack_p = false;
1822 declarator->std_attributes = attributes;
1824 return declarator;
1827 /* Like make_pointer_declarator -- but for a pointer to a non-static
1828 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1829 appertain to the pointer or reference. */
1831 cp_declarator *
1832 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1833 cp_declarator *pointee,
1834 tree attributes)
1836 cp_declarator *declarator;
1838 declarator = make_declarator (cdk_ptrmem);
1839 declarator->declarator = pointee;
1840 declarator->u.pointer.qualifiers = cv_qualifiers;
1841 declarator->u.pointer.class_type = class_type;
1843 if (pointee)
1845 declarator->parameter_pack_p = pointee->parameter_pack_p;
1846 pointee->parameter_pack_p = false;
1848 else
1849 declarator->parameter_pack_p = false;
1851 declarator->std_attributes = attributes;
1853 return declarator;
1856 /* Make a declarator for the function given by TARGET, with the
1857 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1858 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1859 indicates what exceptions can be thrown. STD_ATTRS contains
1860 attributes that appertain to the function type. */
1862 cp_declarator *
1863 make_call_declarator (cp_declarator *target,
1864 tree parms,
1865 cp_cv_quals cv_qualifiers,
1866 cp_virt_specifiers virt_specifiers,
1867 cp_ref_qualifier ref_qualifier,
1868 tree tx_qualifier,
1869 tree exception_specification,
1870 tree late_return_type,
1871 tree requires_clause,
1872 tree std_attrs,
1873 location_t parens_loc)
1875 cp_declarator *declarator;
1877 declarator = make_declarator (cdk_function);
1878 declarator->declarator = target;
1879 declarator->u.function.parameters = parms;
1880 declarator->u.function.qualifiers = cv_qualifiers;
1881 declarator->u.function.virt_specifiers = virt_specifiers;
1882 declarator->u.function.ref_qualifier = ref_qualifier;
1883 declarator->u.function.tx_qualifier = tx_qualifier;
1884 declarator->u.function.exception_specification = exception_specification;
1885 declarator->u.function.late_return_type = late_return_type;
1886 declarator->u.function.requires_clause = requires_clause;
1887 declarator->u.function.parens_loc = parens_loc;
1888 if (target)
1890 declarator->id_loc = target->id_loc;
1891 declarator->parameter_pack_p = target->parameter_pack_p;
1892 target->parameter_pack_p = false;
1894 else
1895 declarator->parameter_pack_p = false;
1897 declarator->std_attributes = std_attrs;
1899 return declarator;
1902 /* Make a declarator for an array of BOUNDS elements, each of which is
1903 defined by ELEMENT. */
1905 cp_declarator *
1906 make_array_declarator (cp_declarator *element, tree bounds)
1908 cp_declarator *declarator;
1910 declarator = make_declarator (cdk_array);
1911 declarator->declarator = element;
1912 declarator->u.array.bounds = bounds;
1913 if (element)
1915 declarator->id_loc = element->id_loc;
1916 declarator->parameter_pack_p = element->parameter_pack_p;
1917 element->parameter_pack_p = false;
1919 else
1920 declarator->parameter_pack_p = false;
1922 return declarator;
1925 /* Determine whether the declarator we've seen so far can be a
1926 parameter pack, when followed by an ellipsis. */
1927 static bool
1928 declarator_can_be_parameter_pack (cp_declarator *declarator)
1930 if (declarator && declarator->parameter_pack_p)
1931 /* We already saw an ellipsis. */
1932 return false;
1934 /* Search for a declarator name, or any other declarator that goes
1935 after the point where the ellipsis could appear in a parameter
1936 pack. If we find any of these, then this declarator cannot be
1937 made into a parameter pack. */
1938 bool found = false;
1939 while (declarator && !found)
1941 switch ((int)declarator->kind)
1943 case cdk_id:
1944 case cdk_array:
1945 case cdk_decomp:
1946 found = true;
1947 break;
1949 case cdk_error:
1950 return true;
1952 default:
1953 declarator = declarator->declarator;
1954 break;
1958 return !found;
1961 cp_parameter_declarator *no_parameters;
1963 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1964 DECLARATOR and DEFAULT_ARGUMENT. */
1966 cp_parameter_declarator *
1967 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1968 cp_declarator *declarator,
1969 tree default_argument,
1970 location_t loc,
1971 bool template_parameter_pack_p = false)
1973 cp_parameter_declarator *parameter;
1975 parameter = ((cp_parameter_declarator *)
1976 alloc_declarator (sizeof (cp_parameter_declarator)));
1977 parameter->next = NULL;
1978 if (decl_specifiers)
1979 parameter->decl_specifiers = *decl_specifiers;
1980 else
1981 clear_decl_specs (&parameter->decl_specifiers);
1982 parameter->declarator = declarator;
1983 parameter->default_argument = default_argument;
1984 parameter->template_parameter_pack_p = template_parameter_pack_p;
1985 parameter->loc = loc;
1987 return parameter;
1990 /* Returns true iff DECLARATOR is a declaration for a function. */
1992 static bool
1993 function_declarator_p (const cp_declarator *declarator)
1995 while (declarator)
1997 if (declarator->kind == cdk_function
1998 && declarator->declarator->kind == cdk_id)
1999 return true;
2000 if (declarator->kind == cdk_id
2001 || declarator->kind == cdk_decomp
2002 || declarator->kind == cdk_error)
2003 return false;
2004 declarator = declarator->declarator;
2006 return false;
2009 /* The parser. */
2011 /* Overview
2012 --------
2014 A cp_parser parses the token stream as specified by the C++
2015 grammar. Its job is purely parsing, not semantic analysis. For
2016 example, the parser breaks the token stream into declarators,
2017 expressions, statements, and other similar syntactic constructs.
2018 It does not check that the types of the expressions on either side
2019 of an assignment-statement are compatible, or that a function is
2020 not declared with a parameter of type `void'.
2022 The parser invokes routines elsewhere in the compiler to perform
2023 semantic analysis and to build up the abstract syntax tree for the
2024 code processed.
2026 The parser (and the template instantiation code, which is, in a
2027 way, a close relative of parsing) are the only parts of the
2028 compiler that should be calling push_scope and pop_scope, or
2029 related functions. The parser (and template instantiation code)
2030 keeps track of what scope is presently active; everything else
2031 should simply honor that. (The code that generates static
2032 initializers may also need to set the scope, in order to check
2033 access control correctly when emitting the initializers.)
2035 Methodology
2036 -----------
2038 The parser is of the standard recursive-descent variety. Upcoming
2039 tokens in the token stream are examined in order to determine which
2040 production to use when parsing a non-terminal. Some C++ constructs
2041 require arbitrary look ahead to disambiguate. For example, it is
2042 impossible, in the general case, to tell whether a statement is an
2043 expression or declaration without scanning the entire statement.
2044 Therefore, the parser is capable of "parsing tentatively." When the
2045 parser is not sure what construct comes next, it enters this mode.
2046 Then, while we attempt to parse the construct, the parser queues up
2047 error messages, rather than issuing them immediately, and saves the
2048 tokens it consumes. If the construct is parsed successfully, the
2049 parser "commits", i.e., it issues any queued error messages and
2050 the tokens that were being preserved are permanently discarded.
2051 If, however, the construct is not parsed successfully, the parser
2052 rolls back its state completely so that it can resume parsing using
2053 a different alternative.
2055 Future Improvements
2056 -------------------
2058 The performance of the parser could probably be improved substantially.
2059 We could often eliminate the need to parse tentatively by looking ahead
2060 a little bit. In some places, this approach might not entirely eliminate
2061 the need to parse tentatively, but it might still speed up the average
2062 case. */
2064 /* Flags that are passed to some parsing functions. These values can
2065 be bitwise-ored together. */
2067 enum
2069 /* No flags. */
2070 CP_PARSER_FLAGS_NONE = 0x0,
2071 /* The construct is optional. If it is not present, then no error
2072 should be issued. */
2073 CP_PARSER_FLAGS_OPTIONAL = 0x1,
2074 /* When parsing a type-specifier, treat user-defined type-names
2075 as non-type identifiers. */
2076 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
2077 /* When parsing a type-specifier, do not try to parse a class-specifier
2078 or enum-specifier. */
2079 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
2080 /* When parsing a decl-specifier-seq, only allow type-specifier or
2081 constexpr. */
2082 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
2083 /* When parsing a decl-specifier-seq, only allow mutable, constexpr or
2084 for C++20 consteval or for C++23 static. */
2085 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10,
2086 /* When parsing a decl-specifier-seq, allow missing typename. */
2087 CP_PARSER_FLAGS_TYPENAME_OPTIONAL = 0x20,
2088 /* When parsing of the noexcept-specifier should be delayed. */
2089 CP_PARSER_FLAGS_DELAY_NOEXCEPT = 0x40,
2090 /* When parsing a consteval declarator. */
2091 CP_PARSER_FLAGS_CONSTEVAL = 0x80
2094 /* This type is used for parameters and variables which hold
2095 combinations of the above flags. */
2096 typedef int cp_parser_flags;
2098 /* The different kinds of declarators we want to parse. */
2100 enum cp_parser_declarator_kind
2102 /* We want an abstract declarator. */
2103 CP_PARSER_DECLARATOR_ABSTRACT,
2104 /* We want a named declarator. */
2105 CP_PARSER_DECLARATOR_NAMED,
2106 /* We don't mind, but the name must be an unqualified-id. */
2107 CP_PARSER_DECLARATOR_EITHER
2110 /* The precedence values used to parse binary expressions. The minimum value
2111 of PREC must be 1, because zero is reserved to quickly discriminate
2112 binary operators from other tokens. */
2114 enum cp_parser_prec
2116 PREC_NOT_OPERATOR,
2117 PREC_LOGICAL_OR_EXPRESSION,
2118 PREC_LOGICAL_AND_EXPRESSION,
2119 PREC_INCLUSIVE_OR_EXPRESSION,
2120 PREC_EXCLUSIVE_OR_EXPRESSION,
2121 PREC_AND_EXPRESSION,
2122 PREC_EQUALITY_EXPRESSION,
2123 PREC_RELATIONAL_EXPRESSION,
2124 PREC_SPACESHIP_EXPRESSION,
2125 PREC_SHIFT_EXPRESSION,
2126 PREC_ADDITIVE_EXPRESSION,
2127 PREC_MULTIPLICATIVE_EXPRESSION,
2128 PREC_PM_EXPRESSION,
2129 NUM_PREC_VALUES = PREC_PM_EXPRESSION
2132 /* A mapping from a token type to a corresponding tree node type, with a
2133 precedence value. */
2135 struct cp_parser_binary_operations_map_node
2137 /* The token type. */
2138 enum cpp_ttype token_type;
2139 /* The corresponding tree code. */
2140 enum tree_code tree_type;
2141 /* The precedence of this operator. */
2142 enum cp_parser_prec prec;
2145 struct cp_parser_expression_stack_entry
2147 /* Left hand side of the binary operation we are currently
2148 parsing. */
2149 cp_expr lhs;
2150 /* Original tree code for left hand side, if it was a binary
2151 expression itself (used for -Wparentheses). */
2152 enum tree_code lhs_type;
2153 /* Tree code for the binary operation we are parsing. */
2154 enum tree_code tree_type;
2155 /* Precedence of the binary operation we are parsing. */
2156 enum cp_parser_prec prec;
2157 /* Location of the binary operation we are parsing. */
2158 location_t loc;
2159 /* Flags from the operator token. */
2160 unsigned char flags;
2163 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
2164 entries because precedence levels on the stack are monotonically
2165 increasing. */
2166 typedef struct cp_parser_expression_stack_entry
2167 cp_parser_expression_stack[NUM_PREC_VALUES];
2169 /* Used for parsing OMP for loops.
2171 Some notes on flags used for context:
2172 parser->omp_for_parse_state is non-null anywhere inside the OMP FOR
2173 construct, except for the final-loop-body.
2174 The want_nested_loop flag is true if inside a {} sequence where
2175 a loop-nest (or another {} sequence containing a loop-nest) is expected,
2176 but has not yet been seen. It's false when parsing intervening code
2177 statements or their substatements that cannot contain a loop-nest.
2178 The in_intervening_code flag is true when parsing any intervening code,
2179 including substatements, and whether or not want_nested_loop is true.
2181 And, about error handling:
2182 The saw_intervening_code flag is set if the loop is not perfectly
2183 nested, even in the usual case where this is not an error.
2184 perfect_nesting_fail is set if an error has been diagnosed because an
2185 imperfectly-nested loop was found where a perfectly-nested one is
2186 required (we diagnose this only once).
2187 fail is set if any kind of structural error in the loop nest
2188 has been found and diagnosed.
2190 struct omp_for_parse_data {
2191 enum tree_code code;
2192 tree declv, condv, incrv, initv;
2193 tree pre_body;
2194 tree orig_declv;
2195 auto_vec<tree, 4> orig_inits;
2196 int count; /* Expected nesting depth. */
2197 int depth; /* Current nesting depth. */
2198 location_t for_loc;
2199 releasing_vec init_blockv;
2200 releasing_vec body_blockv;
2201 releasing_vec init_placeholderv;
2202 releasing_vec body_placeholderv;
2203 bool ordered : 1;
2204 bool inscan : 1;
2205 bool want_nested_loop : 1;
2206 bool in_intervening_code : 1;
2207 bool saw_intervening_code : 1;
2208 bool perfect_nesting_fail : 1;
2209 bool fail : 1;
2210 tree clauses;
2211 tree *cclauses;
2212 tree ordered_cl;
2215 /* Prototypes. */
2217 /* Constructors and destructors. */
2219 static cp_parser_context *cp_parser_context_new
2220 (cp_parser_context *);
2222 /* Class variables. */
2224 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
2226 /* The operator-precedence table used by cp_parser_binary_expression.
2227 Transformed into an associative array (binops_by_token) by
2228 cp_parser_new. */
2230 static const cp_parser_binary_operations_map_node binops[] = {
2231 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
2232 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
2234 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
2235 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
2236 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
2238 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
2239 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
2241 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
2242 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
2244 { CPP_SPACESHIP, SPACESHIP_EXPR, PREC_SPACESHIP_EXPRESSION },
2246 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
2247 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
2248 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
2249 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
2251 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
2252 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
2254 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
2256 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
2258 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
2260 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
2262 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
2265 /* The same as binops, but initialized by cp_parser_new so that
2266 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
2267 for speed. */
2268 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
2270 /* Constructors and destructors. */
2272 /* Construct a new context. The context below this one on the stack
2273 is given by NEXT. */
2275 static cp_parser_context *
2276 cp_parser_context_new (cp_parser_context* next)
2278 cp_parser_context *context;
2280 /* Allocate the storage. */
2281 if (cp_parser_context_free_list != NULL)
2283 /* Pull the first entry from the free list. */
2284 context = cp_parser_context_free_list;
2285 cp_parser_context_free_list = context->next;
2286 memset (context, 0, sizeof (*context));
2288 else
2289 context = ggc_cleared_alloc<cp_parser_context> ();
2291 /* No errors have occurred yet in this context. */
2292 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
2293 /* If this is not the bottommost context, copy information that we
2294 need from the previous context. */
2295 if (next)
2297 /* If, in the NEXT context, we are parsing an `x->' or `x.'
2298 expression, then we are parsing one in this context, too. */
2299 context->object_type = next->object_type;
2300 /* Thread the stack. */
2301 context->next = next;
2304 return context;
2307 /* Managing the unparsed function queues. */
2309 #define unparsed_funs_with_default_args \
2310 parser->unparsed_queues->last ().funs_with_default_args
2311 #define unparsed_funs_with_definitions \
2312 parser->unparsed_queues->last ().funs_with_definitions
2313 #define unparsed_nsdmis \
2314 parser->unparsed_queues->last ().nsdmis
2315 #define unparsed_noexcepts \
2316 parser->unparsed_queues->last ().noexcepts
2317 #define unparsed_contracts \
2318 parser->unparsed_queues->last ().contracts
2320 static void
2321 push_unparsed_function_queues (cp_parser *parser)
2323 cp_unparsed_functions_entry e
2324 = { NULL, make_tree_vector (), NULL, NULL, NULL };
2325 vec_safe_push (parser->unparsed_queues, e);
2328 static void
2329 pop_unparsed_function_queues (cp_parser *parser)
2331 release_tree_vector (unparsed_funs_with_definitions);
2332 parser->unparsed_queues->pop ();
2335 /* Prototypes. */
2337 /* Routines to parse various constructs.
2339 Those that return `tree' will return the error_mark_node (rather
2340 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2341 Sometimes, they will return an ordinary node if error-recovery was
2342 attempted, even though a parse error occurred. So, to check
2343 whether or not a parse error occurred, you should always use
2344 cp_parser_error_occurred. If the construct is optional (indicated
2345 either by an `_opt' in the name of the function that does the
2346 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2347 the construct is not present. */
2349 /* Lexical conventions [gram.lex] */
2351 static tree finish_userdef_string_literal
2352 (tree);
2354 /* Basic concepts [gram.basic] */
2356 static void cp_parser_translation_unit (cp_parser *);
2358 /* Expressions [gram.expr] */
2360 static cp_expr cp_parser_primary_expression
2361 (cp_parser *, bool, bool, bool, cp_id_kind *);
2362 static cp_expr cp_parser_id_expression
2363 (cp_parser *, bool, bool, bool *, bool, bool);
2364 static cp_expr cp_parser_unqualified_id
2365 (cp_parser *, bool, bool, bool, bool);
2366 static tree cp_parser_nested_name_specifier_opt
2367 (cp_parser *, bool, bool, bool, bool, bool = false);
2368 static tree cp_parser_nested_name_specifier
2369 (cp_parser *, bool, bool, bool, bool);
2370 static tree cp_parser_qualifying_entity
2371 (cp_parser *, bool, bool, bool, bool, bool);
2372 static cp_expr cp_parser_postfix_expression
2373 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2374 static tree cp_parser_postfix_open_square_expression
2375 (cp_parser *, tree, bool, bool);
2376 static tree cp_parser_postfix_dot_deref_expression
2377 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2378 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2379 (cp_parser *, int, bool, bool, bool *, location_t * = NULL,
2380 bool = false);
2381 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2382 enum { non_attr = 0, normal_attr = 1, id_attr = 2, assume_attr = 3,
2383 uneval_string_attr = 4 };
2384 static void cp_parser_pseudo_destructor_name
2385 (cp_parser *, tree, tree *, tree *);
2386 static cp_expr cp_parser_unary_expression
2387 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2388 static enum tree_code cp_parser_unary_operator
2389 (cp_token *);
2390 static tree cp_parser_has_attribute_expression
2391 (cp_parser *);
2392 static tree cp_parser_new_expression
2393 (cp_parser *);
2394 static vec<tree, va_gc> *cp_parser_new_placement
2395 (cp_parser *);
2396 static tree cp_parser_new_type_id
2397 (cp_parser *, tree *);
2398 static cp_declarator *cp_parser_new_declarator_opt
2399 (cp_parser *);
2400 static cp_declarator *cp_parser_direct_new_declarator
2401 (cp_parser *);
2402 static vec<tree, va_gc> *cp_parser_new_initializer
2403 (cp_parser *);
2404 static tree cp_parser_delete_expression
2405 (cp_parser *);
2406 static cp_expr cp_parser_cast_expression
2407 (cp_parser *, bool, bool, bool, cp_id_kind *);
2408 static cp_expr cp_parser_binary_expression
2409 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2410 static tree cp_parser_question_colon_clause
2411 (cp_parser *, cp_expr);
2412 static cp_expr cp_parser_conditional_expression (cp_parser *);
2413 static cp_expr cp_parser_assignment_expression
2414 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2415 static enum tree_code cp_parser_assignment_operator_opt
2416 (cp_parser *);
2417 static cp_expr cp_parser_expression
2418 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2419 static cp_expr cp_parser_constant_expression
2420 (cp_parser *, int = 0, bool * = NULL, bool = false);
2421 static cp_expr cp_parser_builtin_offsetof
2422 (cp_parser *);
2423 static cp_expr cp_parser_lambda_expression
2424 (cp_parser *);
2425 static void cp_parser_lambda_introducer
2426 (cp_parser *, tree);
2427 static bool cp_parser_lambda_declarator_opt
2428 (cp_parser *, tree);
2429 static void cp_parser_lambda_body
2430 (cp_parser *, tree);
2432 /* Statements [gram.stmt.stmt] */
2434 static void cp_parser_statement
2435 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2436 static void cp_parser_label_for_labeled_statement
2437 (cp_parser *, tree);
2438 static tree cp_parser_expression_statement
2439 (cp_parser *, tree);
2440 static tree cp_parser_compound_statement
2441 (cp_parser *, tree, int, bool);
2442 static void cp_parser_statement_seq_opt
2443 (cp_parser *, tree);
2444 static tree cp_parser_selection_statement
2445 (cp_parser *, bool *, vec<tree> *);
2446 static tree cp_parser_condition
2447 (cp_parser *);
2448 static tree cp_parser_iteration_statement
2449 (cp_parser *, bool *, bool, tree, bool);
2450 static bool cp_parser_init_statement
2451 (cp_parser *, tree *decl);
2452 static tree cp_parser_for
2453 (cp_parser *, bool, tree, bool);
2454 static tree cp_parser_c_for
2455 (cp_parser *, tree, tree, bool, tree, bool);
2456 static tree cp_parser_range_for
2457 (cp_parser *, tree, tree, tree, bool, tree, bool, bool);
2458 static void do_range_for_auto_deduction
2459 (tree, tree, cp_decomp *);
2460 static tree cp_parser_perform_range_for_lookup
2461 (tree, tree *, tree *);
2462 static tree cp_parser_range_for_member_function
2463 (tree, tree);
2464 static tree cp_parser_jump_statement
2465 (cp_parser *);
2466 static void cp_parser_declaration_statement
2467 (cp_parser *);
2469 static tree cp_parser_implicitly_scoped_statement
2470 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2471 static void cp_parser_already_scoped_statement
2472 (cp_parser *, bool *, const token_indent_info &);
2474 /* State of module-declaration parsing. */
2475 enum module_parse
2477 MP_NOT_MODULE, /* Not a module. */
2479 _MP_UNUSED,
2481 MP_FIRST, /* First declaration of TU. */
2482 MP_GLOBAL, /* Global Module Fragment. */
2484 MP_PURVIEW_IMPORTS, /* Imports of a module. */
2485 MP_PURVIEW, /* Purview of a named module. */
2487 MP_PRIVATE_IMPORTS, /* Imports of a Private Module Fragment. */
2488 MP_PRIVATE, /* Private Module Fragment. */
2491 static module_parse cp_parser_module_declaration
2492 (cp_parser *parser, module_parse, bool exporting);
2493 static void cp_parser_import_declaration
2494 (cp_parser *parser, module_parse, bool exporting);
2496 /* Declarations [gram.dcl.dcl] */
2498 static void cp_parser_declaration_seq_opt
2499 (cp_parser *);
2500 static void cp_parser_declaration
2501 (cp_parser *, tree);
2502 static void cp_parser_toplevel_declaration
2503 (cp_parser *);
2504 static void cp_parser_block_declaration
2505 (cp_parser *, bool);
2506 static void cp_parser_simple_declaration
2507 (cp_parser *, bool, tree *);
2508 static void cp_parser_decl_specifier_seq
2509 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2510 static tree cp_parser_storage_class_specifier_opt
2511 (cp_parser *);
2512 static tree cp_parser_function_specifier_opt
2513 (cp_parser *, cp_decl_specifier_seq *);
2514 static tree cp_parser_type_specifier
2515 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2516 int *, bool *);
2517 static tree cp_parser_simple_type_specifier
2518 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2519 static tree cp_parser_placeholder_type_specifier
2520 (cp_parser *, location_t, tree, bool);
2521 static tree cp_parser_type_name
2522 (cp_parser *, bool);
2523 static tree cp_parser_nonclass_name
2524 (cp_parser* parser);
2525 static tree cp_parser_elaborated_type_specifier
2526 (cp_parser *, bool, bool);
2527 static tree cp_parser_enum_specifier
2528 (cp_parser *);
2529 static void cp_parser_enumerator_list
2530 (cp_parser *, tree);
2531 static void cp_parser_enumerator_definition
2532 (cp_parser *, tree);
2533 static tree cp_parser_namespace_name
2534 (cp_parser *);
2535 static void cp_parser_namespace_definition
2536 (cp_parser *);
2537 static void cp_parser_namespace_body
2538 (cp_parser *);
2539 static tree cp_parser_qualified_namespace_specifier
2540 (cp_parser *);
2541 static void cp_parser_namespace_alias_definition
2542 (cp_parser *);
2543 static bool cp_parser_using_declaration
2544 (cp_parser *, bool);
2545 static void cp_parser_using_directive
2546 (cp_parser *);
2547 static void cp_parser_using_enum
2548 (cp_parser *);
2549 static tree cp_parser_alias_declaration
2550 (cp_parser *);
2551 static void cp_parser_asm_definition
2552 (cp_parser *);
2553 static void cp_parser_linkage_specification
2554 (cp_parser *, tree);
2555 static void cp_parser_static_assert
2556 (cp_parser *, bool);
2557 static tree cp_parser_decltype
2558 (cp_parser *);
2559 static tree cp_parser_decomposition_declaration
2560 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2562 /* Declarators [gram.dcl.decl] */
2564 static tree cp_parser_init_declarator
2565 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *,
2566 vec<deferred_access_check, va_gc> *, bool, bool, int, bool *, tree *,
2567 location_t *, tree *);
2568 static cp_declarator *cp_parser_declarator
2569 (cp_parser *, cp_parser_declarator_kind, cp_parser_flags, int *, bool *,
2570 bool, bool, bool);
2571 static cp_declarator *cp_parser_direct_declarator
2572 (cp_parser *, cp_parser_declarator_kind, cp_parser_flags, int *, bool, bool,
2573 bool);
2574 static enum tree_code cp_parser_ptr_operator
2575 (cp_parser *, tree *, cp_cv_quals *, tree *);
2576 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2577 (cp_parser *);
2578 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2579 (cp_parser *);
2580 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2581 (cp_parser *);
2582 static tree cp_parser_tx_qualifier_opt
2583 (cp_parser *);
2584 static tree cp_parser_late_return_type_opt
2585 (cp_parser *, cp_declarator *, tree &);
2586 static tree cp_parser_declarator_id
2587 (cp_parser *, bool);
2588 static tree cp_parser_type_id
2589 (cp_parser *, cp_parser_flags = CP_PARSER_FLAGS_NONE, location_t * = NULL);
2590 static tree cp_parser_template_type_arg
2591 (cp_parser *);
2592 static tree cp_parser_trailing_type_id (cp_parser *);
2593 static tree cp_parser_type_id_1
2594 (cp_parser *, cp_parser_flags, bool, bool, location_t *);
2595 static void cp_parser_type_specifier_seq
2596 (cp_parser *, cp_parser_flags, bool, bool, cp_decl_specifier_seq *);
2597 static tree cp_parser_parameter_declaration_clause
2598 (cp_parser *, cp_parser_flags);
2599 static tree cp_parser_parameter_declaration_list
2600 (cp_parser *, cp_parser_flags, auto_vec<tree> *);
2601 static cp_parameter_declarator *cp_parser_parameter_declaration
2602 (cp_parser *, cp_parser_flags, bool, bool *);
2603 static tree cp_parser_default_argument
2604 (cp_parser *, bool);
2605 static void cp_parser_function_body
2606 (cp_parser *, bool);
2607 static tree cp_parser_initializer
2608 (cp_parser *, bool * = nullptr, bool * = nullptr, bool = false);
2609 static cp_expr cp_parser_initializer_clause
2610 (cp_parser *, bool * = nullptr);
2611 static cp_expr cp_parser_braced_list
2612 (cp_parser*, bool * = nullptr);
2613 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2614 (cp_parser *, bool *, bool *);
2616 static void cp_parser_ctor_initializer_opt_and_function_body
2617 (cp_parser *, bool);
2619 static tree cp_parser_late_parsing_omp_declare_simd
2620 (cp_parser *, tree);
2622 static tree cp_parser_late_parsing_oacc_routine
2623 (cp_parser *, tree);
2625 static tree synthesize_implicit_template_parm
2626 (cp_parser *, tree);
2627 static tree finish_fully_implicit_template
2628 (cp_parser *, tree);
2629 static void abort_fully_implicit_template
2630 (cp_parser *);
2632 /* Classes [gram.class] */
2634 static tree cp_parser_class_name
2635 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2636 static tree cp_parser_class_specifier
2637 (cp_parser *);
2638 static tree cp_parser_class_head
2639 (cp_parser *, bool *);
2640 static enum tag_types cp_parser_class_key
2641 (cp_parser *);
2642 static void cp_parser_type_parameter_key
2643 (cp_parser* parser);
2644 static void cp_parser_member_specification_opt
2645 (cp_parser *);
2646 static void cp_parser_member_declaration
2647 (cp_parser *);
2648 static tree cp_parser_pure_specifier
2649 (cp_parser *);
2650 static tree cp_parser_constant_initializer
2651 (cp_parser *);
2653 /* Derived classes [gram.class.derived] */
2655 static tree cp_parser_base_clause
2656 (cp_parser *);
2657 static tree cp_parser_base_specifier
2658 (cp_parser *);
2660 /* Special member functions [gram.special] */
2662 static tree cp_parser_conversion_function_id
2663 (cp_parser *);
2664 static tree cp_parser_conversion_type_id
2665 (cp_parser *);
2666 static cp_declarator *cp_parser_conversion_declarator_opt
2667 (cp_parser *);
2668 static void cp_parser_ctor_initializer_opt
2669 (cp_parser *);
2670 static void cp_parser_mem_initializer_list
2671 (cp_parser *);
2672 static tree cp_parser_mem_initializer
2673 (cp_parser *);
2674 static tree cp_parser_mem_initializer_id
2675 (cp_parser *);
2677 /* Overloading [gram.over] */
2679 static cp_expr cp_parser_operator_function_id
2680 (cp_parser *);
2681 static cp_expr cp_parser_operator
2682 (cp_parser *, location_t);
2684 /* Templates [gram.temp] */
2686 static void cp_parser_template_declaration
2687 (cp_parser *, bool);
2688 static tree cp_parser_template_parameter_list
2689 (cp_parser *);
2690 static tree cp_parser_template_parameter
2691 (cp_parser *, bool *, bool *);
2692 static tree cp_parser_type_parameter
2693 (cp_parser *, bool *);
2694 static tree cp_parser_template_id
2695 (cp_parser *, bool, bool, enum tag_types, bool);
2696 static tree cp_parser_template_id_expr
2697 (cp_parser *, bool, bool, bool);
2698 static tree cp_parser_template_name
2699 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2700 static tree cp_parser_template_argument_list
2701 (cp_parser *);
2702 static tree cp_parser_template_argument
2703 (cp_parser *);
2704 static void cp_parser_explicit_instantiation
2705 (cp_parser *);
2706 static void cp_parser_explicit_specialization
2707 (cp_parser *);
2709 /* Exception handling [gram.except] */
2711 static tree cp_parser_try_block
2712 (cp_parser *);
2713 static void cp_parser_function_try_block
2714 (cp_parser *);
2715 static void cp_parser_handler_seq
2716 (cp_parser *);
2717 static void cp_parser_handler
2718 (cp_parser *);
2719 static tree cp_parser_exception_declaration
2720 (cp_parser *);
2721 static tree cp_parser_throw_expression
2722 (cp_parser *);
2723 static tree cp_parser_exception_specification_opt
2724 (cp_parser *, cp_parser_flags);
2725 static tree cp_parser_type_id_list
2726 (cp_parser *);
2727 static tree cp_parser_noexcept_specification_opt
2728 (cp_parser *, cp_parser_flags, bool, bool *, bool);
2730 /* GNU Extensions */
2732 static tree cp_parser_asm_specification_opt
2733 (cp_parser *);
2734 static tree cp_parser_asm_operand_list
2735 (cp_parser *);
2736 static tree cp_parser_asm_clobber_list
2737 (cp_parser *);
2738 static tree cp_parser_asm_label_list
2739 (cp_parser *);
2740 static bool cp_next_tokens_can_be_attribute_p
2741 (cp_parser *);
2742 static bool cp_next_tokens_can_be_gnu_attribute_p
2743 (cp_parser *);
2744 static bool cp_next_tokens_can_be_std_attribute_p
2745 (cp_parser *);
2746 static bool cp_nth_tokens_can_be_std_attribute_p
2747 (cp_parser *, size_t);
2748 static bool cp_nth_tokens_can_be_gnu_attribute_p
2749 (cp_parser *, size_t);
2750 static bool cp_nth_tokens_can_be_attribute_p
2751 (cp_parser *, size_t);
2752 static tree cp_parser_attributes_opt
2753 (cp_parser *);
2754 static tree cp_parser_gnu_attributes_opt
2755 (cp_parser *);
2756 static tree cp_parser_gnu_attribute_list
2757 (cp_parser *, bool = false);
2758 static tree cp_parser_std_attribute
2759 (cp_parser *, tree);
2760 static tree cp_parser_std_attribute_spec
2761 (cp_parser *);
2762 static tree cp_parser_std_attribute_spec_seq
2763 (cp_parser *);
2764 static size_t cp_parser_skip_std_attribute_spec_seq
2765 (cp_parser *, size_t);
2766 static size_t cp_parser_skip_attributes_opt
2767 (cp_parser *, size_t);
2768 static bool cp_parser_extension_opt
2769 (cp_parser *, int *);
2770 static void cp_parser_label_declaration
2771 (cp_parser *);
2773 /* Concept Extensions */
2775 static tree cp_parser_concept_definition
2776 (cp_parser *);
2777 static tree cp_parser_constraint_expression
2778 (cp_parser *);
2779 static tree cp_parser_requires_clause_opt
2780 (cp_parser *, bool);
2781 static tree cp_parser_requires_expression
2782 (cp_parser *);
2783 static tree cp_parser_requirement_parameter_list
2784 (cp_parser *);
2785 static tree cp_parser_requirement_body
2786 (cp_parser *);
2787 static tree cp_parser_requirement_seq
2788 (cp_parser *);
2789 static tree cp_parser_requirement
2790 (cp_parser *);
2791 static tree cp_parser_simple_requirement
2792 (cp_parser *);
2793 static tree cp_parser_compound_requirement
2794 (cp_parser *);
2795 static tree cp_parser_type_requirement
2796 (cp_parser *);
2797 static tree cp_parser_nested_requirement
2798 (cp_parser *);
2800 /* Transactional Memory Extensions */
2802 static tree cp_parser_transaction
2803 (cp_parser *, cp_token *);
2804 static tree cp_parser_transaction_expression
2805 (cp_parser *, enum rid);
2806 static void cp_parser_function_transaction
2807 (cp_parser *, enum rid);
2808 static tree cp_parser_transaction_cancel
2809 (cp_parser *);
2811 /* Coroutine extensions. */
2813 static tree cp_parser_yield_expression
2814 (cp_parser *);
2816 /* Contracts */
2818 static void cp_parser_late_contract_condition
2819 (cp_parser *, tree, tree);
2821 enum pragma_context {
2822 pragma_external,
2823 pragma_member,
2824 pragma_objc_icode,
2825 pragma_stmt,
2826 pragma_compound
2828 static bool cp_parser_pragma
2829 (cp_parser *, enum pragma_context, bool *);
2831 /* Objective-C++ Productions */
2833 static tree cp_parser_objc_message_receiver
2834 (cp_parser *);
2835 static tree cp_parser_objc_message_args
2836 (cp_parser *);
2837 static tree cp_parser_objc_message_expression
2838 (cp_parser *);
2839 static cp_expr cp_parser_objc_encode_expression
2840 (cp_parser *);
2841 static tree cp_parser_objc_defs_expression
2842 (cp_parser *);
2843 static tree cp_parser_objc_protocol_expression
2844 (cp_parser *);
2845 static tree cp_parser_objc_selector_expression
2846 (cp_parser *);
2847 static cp_expr cp_parser_objc_expression
2848 (cp_parser *);
2849 static bool cp_parser_objc_selector_p
2850 (enum cpp_ttype);
2851 static tree cp_parser_objc_selector
2852 (cp_parser *);
2853 static tree cp_parser_objc_protocol_refs_opt
2854 (cp_parser *);
2855 static void cp_parser_objc_declaration
2856 (cp_parser *, tree);
2857 static tree cp_parser_objc_statement
2858 (cp_parser *);
2859 static bool cp_parser_objc_valid_prefix_attributes
2860 (cp_parser *, tree *);
2861 static void cp_parser_objc_at_property_declaration
2862 (cp_parser *) ;
2863 static void cp_parser_objc_at_synthesize_declaration
2864 (cp_parser *) ;
2865 static void cp_parser_objc_at_dynamic_declaration
2866 (cp_parser *) ;
2867 static tree cp_parser_objc_struct_declaration
2868 (cp_parser *) ;
2870 /* Utility Routines */
2872 static cp_expr cp_parser_lookup_name
2873 (cp_parser *, tree, enum tag_types, int, bool, bool, tree *, location_t);
2874 static tree cp_parser_lookup_name_simple
2875 (cp_parser *, tree, location_t);
2876 static tree cp_parser_maybe_treat_template_as_class
2877 (tree, bool);
2878 static bool cp_parser_check_declarator_template_parameters
2879 (cp_parser *, cp_declarator *, location_t);
2880 static bool cp_parser_check_template_parameters
2881 (cp_parser *, unsigned, bool, location_t, cp_declarator *);
2882 static cp_expr cp_parser_simple_cast_expression
2883 (cp_parser *);
2884 static tree cp_parser_global_scope_opt
2885 (cp_parser *, bool);
2886 static bool cp_parser_constructor_declarator_p
2887 (cp_parser *, cp_parser_flags, bool);
2888 static tree cp_parser_function_definition_from_specifiers_and_declarator
2889 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2890 static tree cp_parser_function_definition_after_declarator
2891 (cp_parser *, bool);
2892 static bool cp_parser_template_declaration_after_export
2893 (cp_parser *, bool);
2894 static void cp_parser_perform_template_parameter_access_checks
2895 (vec<deferred_access_check, va_gc> *);
2896 static tree cp_parser_single_declaration
2897 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2898 static cp_expr cp_parser_functional_cast
2899 (cp_parser *, tree);
2900 static tree cp_parser_save_member_function_body
2901 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2902 static tree cp_parser_save_nsdmi
2903 (cp_parser *);
2904 static tree cp_parser_enclosed_template_argument_list
2905 (cp_parser *);
2906 static void cp_parser_save_default_args
2907 (cp_parser *, tree);
2908 static void cp_parser_late_parsing_for_member
2909 (cp_parser *, tree);
2910 static tree cp_parser_late_parse_one_default_arg
2911 (cp_parser *, tree, tree, tree);
2912 static void cp_parser_late_parsing_nsdmi
2913 (cp_parser *, tree);
2914 static void cp_parser_late_parsing_default_args
2915 (cp_parser *, tree);
2916 static tree cp_parser_sizeof_operand
2917 (cp_parser *, enum rid);
2918 static cp_expr cp_parser_trait
2919 (cp_parser *, const cp_trait *);
2920 static bool cp_parser_declares_only_class_p
2921 (cp_parser *);
2922 static void cp_parser_set_storage_class
2923 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2924 static void cp_parser_set_decl_spec_type
2925 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2926 static void set_and_check_decl_spec_loc
2927 (cp_decl_specifier_seq *decl_specs,
2928 cp_decl_spec ds, cp_token *);
2929 static bool cp_parser_friend_p
2930 (const cp_decl_specifier_seq *);
2931 static void cp_parser_required_error
2932 (cp_parser *, required_token, bool, location_t);
2933 static cp_token *cp_parser_require
2934 (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2935 static cp_token *cp_parser_require_keyword
2936 (cp_parser *, enum rid, required_token);
2937 static bool cp_parser_token_starts_function_definition_p
2938 (cp_token *);
2939 static bool cp_parser_next_token_starts_class_definition_p
2940 (cp_parser *);
2941 static bool cp_parser_next_token_ends_template_argument_p
2942 (cp_parser *);
2943 static bool cp_parser_nth_token_starts_template_argument_list_p
2944 (cp_parser *, size_t);
2945 static enum tag_types cp_parser_token_is_class_key
2946 (cp_token *);
2947 static enum tag_types cp_parser_token_is_type_parameter_key
2948 (cp_token *);
2949 static void cp_parser_maybe_warn_enum_key (cp_parser *, location_t, tree, rid);
2950 static void cp_parser_check_class_key
2951 (cp_parser *, location_t, enum tag_types, tree type, bool, bool);
2952 static void cp_parser_check_access_in_redeclaration
2953 (tree type, location_t location);
2954 static bool cp_parser_optional_template_keyword
2955 (cp_parser *);
2956 static void cp_parser_pre_parsed_nested_name_specifier
2957 (cp_parser *);
2958 static bool cp_parser_cache_group
2959 (cp_parser *, enum cpp_ttype, unsigned);
2960 static tree cp_parser_cache_defarg
2961 (cp_parser *parser, bool nsdmi);
2962 static void cp_parser_parse_tentatively
2963 (cp_parser *);
2964 static void cp_parser_commit_to_tentative_parse
2965 (cp_parser *);
2966 static void cp_parser_commit_to_topmost_tentative_parse
2967 (cp_parser *);
2968 static void cp_parser_abort_tentative_parse
2969 (cp_parser *);
2970 static bool cp_parser_parse_definitely
2971 (cp_parser *);
2972 static inline bool cp_parser_parsing_tentatively
2973 (cp_parser *);
2974 static bool cp_parser_uncommitted_to_tentative_parse_p
2975 (cp_parser *);
2976 static void cp_parser_error
2977 (cp_parser *, const char *);
2978 static void cp_parser_name_lookup_error
2979 (cp_parser *, tree, tree, name_lookup_error, location_t);
2980 static bool cp_parser_simulate_error
2981 (cp_parser *);
2982 static bool cp_parser_check_type_definition
2983 (cp_parser *);
2984 static void cp_parser_check_for_definition_in_return_type
2985 (cp_declarator *, tree, location_t type_location);
2986 static void cp_parser_check_for_invalid_template_id
2987 (cp_parser *, tree, enum tag_types, location_t location);
2988 static bool cp_parser_non_integral_constant_expression
2989 (cp_parser *, non_integral_constant);
2990 static void cp_parser_diagnose_invalid_type_name
2991 (cp_parser *, tree, location_t);
2992 static bool cp_parser_parse_and_diagnose_invalid_type_name
2993 (cp_parser *);
2994 static int cp_parser_skip_to_closing_parenthesis
2995 (cp_parser *, bool, bool, bool);
2996 static void cp_parser_skip_to_end_of_statement
2997 (cp_parser *);
2998 static void cp_parser_consume_semicolon_at_end_of_statement
2999 (cp_parser *);
3000 static void cp_parser_skip_to_end_of_block_or_statement
3001 (cp_parser *);
3002 static bool cp_parser_skip_to_closing_brace
3003 (cp_parser *);
3004 static bool cp_parser_skip_entire_template_parameter_list
3005 (cp_parser *);
3006 static void cp_parser_require_end_of_template_parameter_list
3007 (cp_parser *);
3008 static bool cp_parser_skip_to_end_of_template_parameter_list
3009 (cp_parser *);
3010 static void cp_parser_skip_to_pragma_eol
3011 (cp_parser*, cp_token *);
3012 static bool cp_parser_error_occurred
3013 (cp_parser *);
3014 static bool cp_parser_allow_gnu_extensions_p
3015 (cp_parser *);
3016 static bool cp_parser_is_pure_string_literal
3017 (cp_token *);
3018 static bool cp_parser_is_string_literal
3019 (cp_token *);
3020 static bool cp_parser_is_keyword
3021 (cp_token *, enum rid);
3022 static tree cp_parser_make_typename_type
3023 (cp_parser *, tree, location_t location);
3024 static cp_declarator * cp_parser_make_indirect_declarator
3025 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
3026 static bool cp_parser_compound_literal_p
3027 (cp_parser *);
3028 static bool cp_parser_array_designator_p
3029 (cp_parser *);
3030 static bool cp_parser_init_statement_p
3031 (cp_parser *);
3032 static bool cp_parser_skip_up_to_closing_square_bracket
3033 (cp_parser *);
3034 static bool cp_parser_skip_to_closing_square_bracket
3035 (cp_parser *);
3036 static size_t cp_parser_skip_balanced_tokens (cp_parser *, size_t);
3037 static tree cp_parser_omp_loop_nest (cp_parser *, bool *);
3039 // -------------------------------------------------------------------------- //
3040 // Unevaluated Operand Guard
3042 // Implementation of an RAII helper for unevaluated operand parsing.
3043 cp_unevaluated::cp_unevaluated ()
3045 ++cp_unevaluated_operand;
3046 ++c_inhibit_evaluation_warnings;
3049 cp_unevaluated::~cp_unevaluated ()
3051 --c_inhibit_evaluation_warnings;
3052 --cp_unevaluated_operand;
3055 // -------------------------------------------------------------------------- //
3056 // Tentative Parsing
3058 /* Returns nonzero if we are parsing tentatively. */
3060 static inline bool
3061 cp_parser_parsing_tentatively (cp_parser* parser)
3063 return parser->context->next != NULL;
3066 /* Returns nonzero if TOKEN is a string literal. */
3068 static bool
3069 cp_parser_is_pure_string_literal (cp_token* token)
3071 return (token->type == CPP_STRING ||
3072 token->type == CPP_STRING16 ||
3073 token->type == CPP_STRING32 ||
3074 token->type == CPP_WSTRING ||
3075 token->type == CPP_UTF8STRING);
3078 /* Returns nonzero if TOKEN is a string literal
3079 of a user-defined string literal. */
3081 static bool
3082 cp_parser_is_string_literal (cp_token* token)
3084 return (cp_parser_is_pure_string_literal (token) ||
3085 token->type == CPP_STRING_USERDEF ||
3086 token->type == CPP_STRING16_USERDEF ||
3087 token->type == CPP_STRING32_USERDEF ||
3088 token->type == CPP_WSTRING_USERDEF ||
3089 token->type == CPP_UTF8STRING_USERDEF);
3092 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
3094 static bool
3095 cp_parser_is_keyword (cp_token* token, enum rid keyword)
3097 return token->keyword == keyword;
3100 /* Helper function for cp_parser_error.
3101 Having peeked a token of kind TOK1_KIND that might signify
3102 a conflict marker, peek successor tokens to determine
3103 if we actually do have a conflict marker.
3104 Specifically, we consider a run of 7 '<', '=' or '>' characters
3105 at the start of a line as a conflict marker.
3106 These come through the lexer as three pairs and a single,
3107 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
3108 If it returns true, *OUT_LOC is written to with the location/range
3109 of the marker. */
3111 static bool
3112 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
3113 location_t *out_loc)
3115 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
3116 if (token2->type != tok1_kind)
3117 return false;
3118 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
3119 if (token3->type != tok1_kind)
3120 return false;
3121 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
3122 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
3123 return false;
3125 /* It must be at the start of the line. */
3126 location_t start_loc = cp_lexer_peek_token (lexer)->location;
3127 if (LOCATION_COLUMN (start_loc) != 1)
3128 return false;
3130 /* We have a conflict marker. Construct a location of the form:
3131 <<<<<<<
3132 ^~~~~~~
3133 with start == caret, finishing at the end of the marker. */
3134 location_t finish_loc = get_finish (token4->location);
3135 *out_loc = make_location (start_loc, start_loc, finish_loc);
3137 return true;
3140 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
3141 RT_CLOSE_PAREN. */
3143 static const char *
3144 get_matching_symbol (required_token token_desc)
3146 switch (token_desc)
3148 default:
3149 gcc_unreachable ();
3150 return "";
3151 case RT_CLOSE_BRACE:
3152 return "{";
3153 case RT_CLOSE_PAREN:
3154 return "(";
3158 /* Attempt to convert TOKEN_DESC from a required_token to an
3159 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
3161 static enum cpp_ttype
3162 get_required_cpp_ttype (required_token token_desc)
3164 switch (token_desc)
3166 case RT_SEMICOLON:
3167 return CPP_SEMICOLON;
3168 case RT_OPEN_PAREN:
3169 return CPP_OPEN_PAREN;
3170 case RT_CLOSE_BRACE:
3171 return CPP_CLOSE_BRACE;
3172 case RT_OPEN_BRACE:
3173 return CPP_OPEN_BRACE;
3174 case RT_CLOSE_SQUARE:
3175 return CPP_CLOSE_SQUARE;
3176 case RT_OPEN_SQUARE:
3177 return CPP_OPEN_SQUARE;
3178 case RT_COMMA:
3179 return CPP_COMMA;
3180 case RT_COLON:
3181 return CPP_COLON;
3182 case RT_CLOSE_PAREN:
3183 return CPP_CLOSE_PAREN;
3185 default:
3186 /* Use CPP_EOF as a "no completions possible" code. */
3187 return CPP_EOF;
3192 /* Subroutine of cp_parser_error and cp_parser_required_error.
3194 Issue a diagnostic of the form
3195 FILE:LINE: MESSAGE before TOKEN
3196 where TOKEN is the next token in the input stream. MESSAGE
3197 (specified by the caller) is usually of the form "expected
3198 OTHER-TOKEN".
3200 This bypasses the check for tentative passing, and potentially
3201 adds material needed by cp_parser_required_error.
3203 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
3204 suggesting insertion of the missing token.
3206 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
3207 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
3208 location. */
3210 static void
3211 cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
3212 required_token missing_token_desc,
3213 location_t matching_location)
3215 cp_token *token = cp_lexer_peek_token (parser->lexer);
3216 /* This diagnostic makes more sense if it is tagged to the line
3217 of the token we just peeked at. */
3218 cp_lexer_set_source_position_from_token (token);
3220 if (token->type == CPP_PRAGMA)
3222 error_at (token->location,
3223 "%<#pragma%> is not allowed here");
3224 cp_parser_skip_to_pragma_eol (parser, token);
3225 return;
3228 /* If this is actually a conflict marker, report it as such. */
3229 if (token->type == CPP_LSHIFT
3230 || token->type == CPP_RSHIFT
3231 || token->type == CPP_EQ_EQ)
3233 location_t loc;
3234 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
3236 error_at (loc, "version control conflict marker in file");
3237 expanded_location token_exploc = expand_location (token->location);
3238 /* Consume tokens until the end of the source line. */
3239 for (;;)
3241 cp_lexer_consume_token (parser->lexer);
3242 cp_token *next = cp_lexer_peek_token (parser->lexer);
3243 if (next->type == CPP_EOF)
3244 break;
3245 if (next->location == UNKNOWN_LOCATION
3246 || loc == UNKNOWN_LOCATION)
3247 break;
3249 expanded_location next_exploc = expand_location (next->location);
3250 if (next_exploc.file != token_exploc.file)
3251 break;
3252 if (next_exploc.line != token_exploc.line)
3253 break;
3255 return;
3259 auto_diagnostic_group d;
3260 gcc_rich_location richloc (input_location);
3262 bool added_matching_location = false;
3264 if (missing_token_desc != RT_NONE)
3265 if (cp_token *prev_token = cp_lexer_safe_previous_token (parser->lexer))
3267 /* Potentially supply a fix-it hint, suggesting to add the
3268 missing token immediately after the *previous* token.
3269 This may move the primary location within richloc. */
3270 enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
3271 location_t prev_token_loc = prev_token->location;
3272 maybe_suggest_missing_token_insertion (&richloc, ttype,
3273 prev_token_loc);
3275 /* If matching_location != UNKNOWN_LOCATION, highlight it.
3276 Attempt to consolidate diagnostics by printing it as a
3277 secondary range within the main diagnostic. */
3278 if (matching_location != UNKNOWN_LOCATION)
3279 added_matching_location
3280 = richloc.add_location_if_nearby (matching_location);
3283 /* If we were parsing a string-literal and there is an unknown name
3284 token right after, then check to see if that could also have been
3285 a literal string by checking the name against a list of known
3286 standard string literal constants defined in header files. If
3287 there is one, then add that as an hint to the error message. */
3288 name_hint h;
3289 if (token->type == CPP_NAME)
3290 if (cp_token *prev_token = cp_lexer_safe_previous_token (parser->lexer))
3291 if (cp_parser_is_string_literal (prev_token))
3293 tree name = token->u.value;
3294 const char *token_name = IDENTIFIER_POINTER (name);
3295 const char *header_hint
3296 = get_cp_stdlib_header_for_string_macro_name (token_name);
3297 if (header_hint != NULL)
3298 h = name_hint (NULL, new suggest_missing_header (token->location,
3299 token_name,
3300 header_hint));
3303 /* Actually emit the error. */
3304 c_parse_error (gmsgid,
3305 /* Because c_parser_error does not understand
3306 CPP_KEYWORD, keywords are treated like
3307 identifiers. */
3308 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
3309 token->u.value, token->flags, &richloc);
3311 if (missing_token_desc != RT_NONE)
3313 /* If we weren't able to consolidate matching_location, then
3314 print it as a secondary diagnostic. */
3315 if (matching_location != UNKNOWN_LOCATION
3316 && !added_matching_location)
3317 inform (matching_location, "to match this %qs",
3318 get_matching_symbol (missing_token_desc));
3322 /* If not parsing tentatively, issue a diagnostic of the form
3323 FILE:LINE: MESSAGE before TOKEN
3324 where TOKEN is the next token in the input stream. MESSAGE
3325 (specified by the caller) is usually of the form "expected
3326 OTHER-TOKEN". */
3328 static void
3329 cp_parser_error (cp_parser* parser, const char* gmsgid)
3331 if (!cp_parser_simulate_error (parser))
3332 cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
3335 /* Issue an error about name-lookup failing. NAME is the
3336 IDENTIFIER_NODE DECL is the result of
3337 the lookup (as returned from cp_parser_lookup_name). DESIRED is
3338 the thing that we hoped to find. */
3340 static void
3341 cp_parser_name_lookup_error (cp_parser* parser,
3342 tree name,
3343 tree decl,
3344 name_lookup_error desired,
3345 location_t location)
3347 /* If name lookup completely failed, tell the user that NAME was not
3348 declared. */
3349 if (decl == error_mark_node)
3351 if (parser->scope && parser->scope != global_namespace)
3352 error_at (location, "%<%E::%E%> has not been declared",
3353 parser->scope, name);
3354 else if (parser->scope == global_namespace)
3355 error_at (location, "%<::%E%> has not been declared", name);
3356 else if (parser->object_scope
3357 && !CLASS_TYPE_P (parser->object_scope))
3358 error_at (location, "request for member %qE in non-class type %qT",
3359 name, parser->object_scope);
3360 else if (parser->object_scope)
3361 error_at (location, "%<%T::%E%> has not been declared",
3362 parser->object_scope, name);
3363 else
3364 error_at (location, "%qE has not been declared", name);
3366 else if (parser->scope && parser->scope != global_namespace)
3368 switch (desired)
3370 case NLE_TYPE:
3371 error_at (location, "%<%E::%E%> is not a type",
3372 parser->scope, name);
3373 break;
3374 case NLE_CXX98:
3375 error_at (location, "%<%E::%E%> is not a class or namespace",
3376 parser->scope, name);
3377 break;
3378 case NLE_NOT_CXX98:
3379 error_at (location,
3380 "%<%E::%E%> is not a class, namespace, or enumeration",
3381 parser->scope, name);
3382 break;
3383 default:
3384 gcc_unreachable ();
3388 else if (parser->scope == global_namespace)
3390 switch (desired)
3392 case NLE_TYPE:
3393 error_at (location, "%<::%E%> is not a type", name);
3394 break;
3395 case NLE_CXX98:
3396 error_at (location, "%<::%E%> is not a class or namespace", name);
3397 break;
3398 case NLE_NOT_CXX98:
3399 error_at (location,
3400 "%<::%E%> is not a class, namespace, or enumeration",
3401 name);
3402 break;
3403 default:
3404 gcc_unreachable ();
3407 else
3409 switch (desired)
3411 case NLE_TYPE:
3412 error_at (location, "%qE is not a type", name);
3413 break;
3414 case NLE_CXX98:
3415 error_at (location, "%qE is not a class or namespace", name);
3416 break;
3417 case NLE_NOT_CXX98:
3418 error_at (location,
3419 "%qE is not a class, namespace, or enumeration", name);
3420 break;
3421 default:
3422 gcc_unreachable ();
3427 /* If we are parsing tentatively, remember that an error has occurred
3428 during this tentative parse. Returns true if the error was
3429 simulated; false if a message should be issued by the caller. */
3431 static bool
3432 cp_parser_simulate_error (cp_parser* parser)
3434 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3436 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3437 return true;
3439 return false;
3442 /* This function is called when a type is defined. If type
3443 definitions are forbidden at this point, an error message is
3444 issued. */
3446 static bool
3447 cp_parser_check_type_definition (cp_parser* parser)
3449 /* If types are forbidden here, issue a message. */
3450 if (parser->type_definition_forbidden_message)
3452 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3453 or %qs in the message need to be interpreted. */
3454 error (parser->type_definition_forbidden_message,
3455 parser->type_definition_forbidden_message_arg);
3456 return false;
3458 return true;
3461 /* This function is called when the DECLARATOR is processed. The TYPE
3462 was a type defined in the decl-specifiers. If it is invalid to
3463 define a type in the decl-specifiers for DECLARATOR, an error is
3464 issued. TYPE_LOCATION is the location of TYPE and is used
3465 for error reporting. */
3467 static void
3468 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3469 tree type, location_t type_location)
3471 /* [dcl.fct] forbids type definitions in return types.
3472 Unfortunately, it's not easy to know whether or not we are
3473 processing a return type until after the fact. */
3474 while (declarator
3475 && (declarator->kind == cdk_pointer
3476 || declarator->kind == cdk_reference
3477 || declarator->kind == cdk_ptrmem))
3478 declarator = declarator->declarator;
3479 if (declarator
3480 && declarator->kind == cdk_function)
3482 error_at (type_location,
3483 "new types may not be defined in a return type");
3484 inform (type_location,
3485 "(perhaps a semicolon is missing after the definition of %qT)",
3486 type);
3490 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3491 "<" in any valid C++ program. If the next token is indeed "<",
3492 issue a message warning the user about what appears to be an
3493 invalid attempt to form a template-id. LOCATION is the location
3494 of the type-specifier (TYPE) */
3496 static void
3497 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3498 tree type,
3499 enum tag_types tag_type,
3500 location_t location)
3502 cp_token_position start = 0;
3504 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3506 if (TREE_CODE (type) == TYPE_DECL)
3507 type = TREE_TYPE (type);
3508 if (TYPE_P (type) && !template_placeholder_p (type))
3509 error_at (location, "%qT is not a template", type);
3510 else if (identifier_p (type))
3512 if (tag_type != none_type)
3513 error_at (location, "%qE is not a class template", type);
3514 else
3515 error_at (location, "%qE is not a template", type);
3517 else
3518 error_at (location, "invalid template-id");
3519 /* Remember the location of the invalid "<". */
3520 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3521 start = cp_lexer_token_position (parser->lexer, true);
3522 /* Consume the "<". */
3523 cp_lexer_consume_token (parser->lexer);
3524 /* Parse the template arguments. */
3525 cp_parser_enclosed_template_argument_list (parser);
3526 /* Permanently remove the invalid template arguments so that
3527 this error message is not issued again. */
3528 if (start)
3529 cp_lexer_purge_tokens_after (parser->lexer, start);
3533 /* If parsing an integral constant-expression, issue an error message
3534 about the fact that THING appeared and return true. Otherwise,
3535 return false. In either case, set
3536 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3538 static bool
3539 cp_parser_non_integral_constant_expression (cp_parser *parser,
3540 non_integral_constant thing)
3542 parser->non_integral_constant_expression_p = true;
3543 if (parser->integral_constant_expression_p)
3545 if (!parser->allow_non_integral_constant_expression_p)
3547 const char *msg = NULL;
3548 switch (thing)
3550 case NIC_FLOAT:
3551 pedwarn (input_location, OPT_Wpedantic,
3552 "ISO C++ forbids using a floating-point literal "
3553 "in a constant-expression");
3554 return true;
3555 case NIC_CAST:
3556 error ("a cast to a type other than an integral or "
3557 "enumeration type cannot appear in a "
3558 "constant-expression");
3559 return true;
3560 case NIC_TYPEID:
3561 error ("%<typeid%> operator "
3562 "cannot appear in a constant-expression");
3563 return true;
3564 case NIC_NCC:
3565 error ("non-constant compound literals "
3566 "cannot appear in a constant-expression");
3567 return true;
3568 case NIC_FUNC_CALL:
3569 error ("a function call "
3570 "cannot appear in a constant-expression");
3571 return true;
3572 case NIC_INC:
3573 error ("an increment "
3574 "cannot appear in a constant-expression");
3575 return true;
3576 case NIC_DEC:
3577 error ("an decrement "
3578 "cannot appear in a constant-expression");
3579 return true;
3580 case NIC_ARRAY_REF:
3581 error ("an array reference "
3582 "cannot appear in a constant-expression");
3583 return true;
3584 case NIC_ADDR_LABEL:
3585 error ("the address of a label "
3586 "cannot appear in a constant-expression");
3587 return true;
3588 case NIC_OVERLOADED:
3589 error ("calls to overloaded operators "
3590 "cannot appear in a constant-expression");
3591 return true;
3592 case NIC_ASSIGNMENT:
3593 error ("an assignment cannot appear in a constant-expression");
3594 return true;
3595 case NIC_COMMA:
3596 error ("a comma operator "
3597 "cannot appear in a constant-expression");
3598 return true;
3599 case NIC_CONSTRUCTOR:
3600 error ("a call to a constructor "
3601 "cannot appear in a constant-expression");
3602 return true;
3603 case NIC_TRANSACTION:
3604 error ("a transaction expression "
3605 "cannot appear in a constant-expression");
3606 return true;
3607 case NIC_THIS:
3608 msg = "this";
3609 break;
3610 case NIC_FUNC_NAME:
3611 msg = "__FUNCTION__";
3612 break;
3613 case NIC_PRETTY_FUNC:
3614 msg = "__PRETTY_FUNCTION__";
3615 break;
3616 case NIC_C99_FUNC:
3617 msg = "__func__";
3618 break;
3619 case NIC_VA_ARG:
3620 msg = "va_arg";
3621 break;
3622 case NIC_ARROW:
3623 msg = "->";
3624 break;
3625 case NIC_POINT:
3626 msg = ".";
3627 break;
3628 case NIC_STAR:
3629 msg = "*";
3630 break;
3631 case NIC_ADDR:
3632 msg = "&";
3633 break;
3634 case NIC_PREINCREMENT:
3635 msg = "++";
3636 break;
3637 case NIC_PREDECREMENT:
3638 msg = "--";
3639 break;
3640 case NIC_NEW:
3641 msg = "new";
3642 break;
3643 case NIC_DEL:
3644 msg = "delete";
3645 break;
3646 default:
3647 gcc_unreachable ();
3649 if (msg)
3650 error ("%qs cannot appear in a constant-expression", msg);
3651 return true;
3654 return false;
3657 /* Emit a diagnostic for an invalid type name. This function commits
3658 to the current active tentative parse, if any. (Otherwise, the
3659 problematic construct might be encountered again later, resulting
3660 in duplicate error messages.) LOCATION is the location of ID. */
3662 static void
3663 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3664 location_t location)
3666 tree decl, ambiguous_decls;
3667 cp_parser_commit_to_tentative_parse (parser);
3668 /* Try to lookup the identifier. */
3669 decl = cp_parser_lookup_name (parser, id, none_type,
3670 /*is_template=*/false,
3671 /*is_namespace=*/false,
3672 /*check_dependency=*/true,
3673 &ambiguous_decls, location);
3674 if (ambiguous_decls)
3675 /* If the lookup was ambiguous, an error will already have
3676 been issued. */
3677 return;
3678 /* If the lookup found a template-name, it means that the user forgot
3679 to specify an argument list. Emit a useful error message. */
3680 if (DECL_TYPE_TEMPLATE_P (decl))
3682 auto_diagnostic_group d;
3683 error_at (location,
3684 "invalid use of template-name %qE without an argument list",
3685 decl);
3686 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3687 inform (location, "class template argument deduction is only available "
3688 "with %<-std=c++17%> or %<-std=gnu++17%>");
3689 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3691 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3692 error_at (location, "invalid use of destructor %qD as a type", id);
3693 else if (TREE_CODE (decl) == TYPE_DECL)
3694 /* Something like 'unsigned A a;' */
3695 error_at (location, "invalid combination of multiple type-specifiers");
3696 else if (!parser->scope)
3698 /* Issue an error message. */
3699 auto_diagnostic_group d;
3700 name_hint hint;
3701 if (TREE_CODE (id) == IDENTIFIER_NODE)
3702 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3703 if (const char *suggestion = hint.suggestion ())
3705 gcc_rich_location richloc (location);
3706 richloc.add_fixit_replace (suggestion);
3707 error_at (&richloc,
3708 "%qE does not name a type; did you mean %qs?",
3709 id, suggestion);
3711 else
3712 error_at (location, "%qE does not name a type", id);
3713 /* If we're in a template class, it's possible that the user was
3714 referring to a type from a base class. For example:
3716 template <typename T> struct A { typedef T X; };
3717 template <typename T> struct B : public A<T> { X x; };
3719 The user should have said "typename A<T>::X". */
3720 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3721 inform (location, "C++11 %<constexpr%> only available with "
3722 "%<-std=c++11%> or %<-std=gnu++11%>");
3723 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3724 inform (location, "C++11 %<noexcept%> only available with "
3725 "%<-std=c++11%> or %<-std=gnu++11%>");
3726 else if (TREE_CODE (id) == IDENTIFIER_NODE
3727 && (id_equal (id, "module") || id_equal (id, "import")))
3729 if (modules_p ())
3730 inform (location, "%qE is not recognized as a module control-line",
3731 id);
3732 else if (cxx_dialect < cxx20)
3733 inform (location, "C++20 %qE only available with %<-fmodules-ts%>",
3734 id);
3735 else
3736 inform (location, "C++20 %qE only available with %<-fmodules-ts%>"
3737 ", which is not yet enabled with %<-std=c++20%>", id);
3739 else if (cxx_dialect < cxx11
3740 && TREE_CODE (id) == IDENTIFIER_NODE
3741 && id_equal (id, "thread_local"))
3742 inform (location, "C++11 %<thread_local%> only available with "
3743 "%<-std=c++11%> or %<-std=gnu++11%>");
3744 else if (cxx_dialect < cxx20 && id == ridpointers[(int)RID_CONSTINIT])
3745 inform (location, "C++20 %<constinit%> only available with "
3746 "%<-std=c++20%> or %<-std=gnu++20%>");
3747 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3748 inform (location, "%<concept%> only available with %<-std=c++20%> or "
3749 "%<-fconcepts%>");
3750 else if (!flag_concepts && id == ridpointers[(int)RID_REQUIRES])
3751 inform (location, "%<requires%> only available with %<-std=c++20%> or "
3752 "%<-fconcepts%>");
3753 else if (processing_template_decl && current_class_type
3754 && TYPE_BINFO (current_class_type))
3756 for (tree b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3757 b; b = TREE_CHAIN (b))
3759 tree base_type = BINFO_TYPE (b);
3760 if (CLASS_TYPE_P (base_type)
3761 && dependent_type_p (base_type))
3763 /* Go from a particular instantiation of the
3764 template (which will have an empty TYPE_FIELDs),
3765 to the main version. */
3766 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3767 for (tree field = TYPE_FIELDS (base_type);
3768 field; field = DECL_CHAIN (field))
3769 if (TREE_CODE (field) == TYPE_DECL
3770 && DECL_NAME (field) == id)
3772 inform (location,
3773 "(perhaps %<typename %T::%E%> was intended)",
3774 BINFO_TYPE (b), id);
3775 goto found;
3779 found:;
3782 /* Here we diagnose qualified-ids where the scope is actually correct,
3783 but the identifier does not resolve to a valid type name. */
3784 else if (parser->scope != error_mark_node)
3786 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3788 auto_diagnostic_group d;
3789 name_hint hint;
3790 if (decl == error_mark_node)
3791 hint = suggest_alternative_in_explicit_scope (location, id,
3792 parser->scope);
3793 const char *suggestion = hint.suggestion ();
3794 gcc_rich_location richloc (location_of (id));
3795 if (suggestion)
3796 richloc.add_fixit_replace (suggestion);
3797 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3799 if (suggestion)
3800 error_at (&richloc,
3801 "%qE in namespace %qE does not name a template"
3802 " type; did you mean %qs?",
3803 id, parser->scope, suggestion);
3804 else
3805 error_at (&richloc,
3806 "%qE in namespace %qE does not name a template type",
3807 id, parser->scope);
3809 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3811 if (suggestion)
3812 error_at (&richloc,
3813 "%qE in namespace %qE does not name a template"
3814 " type; did you mean %qs?",
3815 TREE_OPERAND (id, 0), parser->scope, suggestion);
3816 else
3817 error_at (&richloc,
3818 "%qE in namespace %qE does not name a template"
3819 " type",
3820 TREE_OPERAND (id, 0), parser->scope);
3822 else
3824 if (suggestion)
3825 error_at (&richloc,
3826 "%qE in namespace %qE does not name a type"
3827 "; did you mean %qs?",
3828 id, parser->scope, suggestion);
3829 else
3830 error_at (&richloc,
3831 "%qE in namespace %qE does not name a type",
3832 id, parser->scope);
3834 if (DECL_P (decl))
3835 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3837 else if (CLASS_TYPE_P (parser->scope)
3838 && constructor_name_p (id, parser->scope))
3840 /* A<T>::A<T>() */
3841 auto_diagnostic_group d;
3842 error_at (location, "%<%T::%E%> names the constructor, not"
3843 " the type", parser->scope, id);
3844 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3845 error_at (location, "and %qT has no template constructors",
3846 parser->scope);
3848 else if (TYPE_P (parser->scope)
3849 && dependent_scope_p (parser->scope))
3851 gcc_rich_location richloc (location);
3852 richloc.add_fixit_insert_before ("typename ");
3853 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3854 error_at (&richloc,
3855 "need %<typename%> before %<%T::%D::%E%> because "
3856 "%<%T::%D%> is a dependent scope",
3857 TYPE_CONTEXT (parser->scope),
3858 TYPENAME_TYPE_FULLNAME (parser->scope),
3860 TYPE_CONTEXT (parser->scope),
3861 TYPENAME_TYPE_FULLNAME (parser->scope));
3862 else
3863 error_at (&richloc, "need %<typename%> before %<%T::%E%> because "
3864 "%qT is a dependent scope",
3865 parser->scope, id, parser->scope);
3867 else if (TYPE_P (parser->scope))
3869 auto_diagnostic_group d;
3870 if (!COMPLETE_TYPE_P (parser->scope))
3871 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3872 parser->scope);
3873 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3874 error_at (location_of (id),
3875 "%qE in %q#T does not name a template type",
3876 id, parser->scope);
3877 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3878 error_at (location_of (id),
3879 "%qE in %q#T does not name a template type",
3880 TREE_OPERAND (id, 0), parser->scope);
3881 else
3882 error_at (location_of (id),
3883 "%qE in %q#T does not name a type",
3884 id, parser->scope);
3885 if (DECL_P (decl))
3886 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3888 else
3889 gcc_unreachable ();
3893 /* Check for a common situation where a type-name should be present,
3894 but is not, and issue a sensible error message. Returns true if an
3895 invalid type-name was detected.
3897 The situation handled by this function are variable declarations of the
3898 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3899 Usually, `ID' should name a type, but if we got here it means that it
3900 does not. We try to emit the best possible error message depending on
3901 how exactly the id-expression looks like. */
3903 static bool
3904 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3906 tree id;
3907 cp_token *token = cp_lexer_peek_token (parser->lexer);
3909 /* Avoid duplicate error about ambiguous lookup. */
3910 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3912 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3913 if (next->type == CPP_NAME && next->error_reported)
3914 goto out;
3917 cp_parser_parse_tentatively (parser);
3918 id = cp_parser_id_expression (parser,
3919 /*template_keyword_p=*/false,
3920 /*check_dependency_p=*/true,
3921 /*template_p=*/NULL,
3922 /*declarator_p=*/true,
3923 /*optional_p=*/false);
3924 /* If the next token is a (, this is a function with no explicit return
3925 type, i.e. constructor, destructor or conversion op. */
3926 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3927 || TREE_CODE (id) == TYPE_DECL)
3929 cp_parser_abort_tentative_parse (parser);
3930 return false;
3932 if (!cp_parser_parse_definitely (parser))
3933 return false;
3935 /* Emit a diagnostic for the invalid type. */
3936 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3937 out:
3938 /* If we aren't in the middle of a declarator (i.e. in a
3939 parameter-declaration-clause), skip to the end of the declaration;
3940 there's no point in trying to process it. */
3941 if (!parser->in_declarator_p)
3942 cp_parser_skip_to_end_of_block_or_statement (parser);
3943 return true;
3946 /* Consume tokens up to, and including, the next non-nested closing `)'.
3947 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3948 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3949 found an unnested token of that type. */
3951 static int
3952 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3953 bool recovering,
3954 cpp_ttype or_ttype,
3955 bool consume_paren)
3957 unsigned paren_depth = 0;
3958 unsigned brace_depth = 0;
3959 unsigned square_depth = 0;
3960 unsigned condop_depth = 0;
3962 if (recovering && or_ttype == CPP_EOF
3963 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3964 return 0;
3966 while (true)
3968 cp_token * token = cp_lexer_peek_token (parser->lexer);
3970 /* Have we found what we're looking for before the closing paren? */
3971 if (token->type == or_ttype && or_ttype != CPP_EOF
3972 && !brace_depth && !paren_depth && !square_depth && !condop_depth)
3973 return -1;
3975 switch (token->type)
3977 case CPP_PRAGMA_EOL:
3978 if (!parser->lexer->in_pragma)
3979 break;
3980 /* FALLTHRU */
3981 case CPP_EOF:
3982 /* If we've run out of tokens, then there is no closing `)'. */
3983 return 0;
3985 /* This is good for lambda expression capture-lists. */
3986 case CPP_OPEN_SQUARE:
3987 ++square_depth;
3988 break;
3989 case CPP_CLOSE_SQUARE:
3990 if (!square_depth--)
3991 return 0;
3992 break;
3994 case CPP_SEMICOLON:
3995 /* This matches the processing in skip_to_end_of_statement. */
3996 if (!brace_depth)
3997 return 0;
3998 break;
4000 case CPP_OPEN_BRACE:
4001 ++brace_depth;
4002 break;
4003 case CPP_CLOSE_BRACE:
4004 if (!brace_depth--)
4005 return 0;
4006 break;
4008 case CPP_OPEN_PAREN:
4009 if (!brace_depth)
4010 ++paren_depth;
4011 break;
4013 case CPP_CLOSE_PAREN:
4014 if (!brace_depth && !paren_depth--)
4016 if (consume_paren)
4017 cp_lexer_consume_token (parser->lexer);
4018 return 1;
4020 break;
4022 case CPP_QUERY:
4023 if (!brace_depth && !paren_depth && !square_depth)
4024 ++condop_depth;
4025 break;
4027 case CPP_COLON:
4028 if (!brace_depth && !paren_depth && !square_depth && condop_depth > 0)
4029 condop_depth--;
4030 break;
4032 case CPP_KEYWORD:
4033 if (!cp_token_is_module_directive (token))
4034 break;
4035 /* FALLTHROUGH */
4037 case CPP_PRAGMA:
4038 /* We fell into a pragma. Skip it, and continue. */
4039 cp_parser_skip_to_pragma_eol (parser, recovering ? token : nullptr);
4040 continue;
4042 default:
4043 break;
4046 /* Consume the token. */
4047 cp_lexer_consume_token (parser->lexer);
4051 /* Consume tokens up to, and including, the next non-nested closing `)'.
4052 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
4053 are doing error recovery. Returns -1 if OR_COMMA is true and we
4054 found an unnested token of that type. */
4056 static int
4057 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
4058 bool recovering,
4059 bool or_comma,
4060 bool consume_paren)
4062 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
4063 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
4064 ttype, consume_paren);
4067 /* Consume tokens until we reach the end of the current statement.
4068 Normally, that will be just before consuming a `;'. However, if a
4069 non-nested `}' comes first, then we stop before consuming that. */
4071 static void
4072 cp_parser_skip_to_end_of_statement (cp_parser* parser)
4074 unsigned nesting_depth = 0;
4076 /* Unwind generic function template scope if necessary. */
4077 if (parser->fully_implicit_function_template_p)
4078 abort_fully_implicit_template (parser);
4080 while (true)
4082 cp_token *token = cp_lexer_peek_token (parser->lexer);
4084 switch (token->type)
4086 case CPP_PRAGMA_EOL:
4087 if (!parser->lexer->in_pragma)
4088 break;
4089 /* FALLTHRU */
4090 case CPP_EOF:
4091 /* If we've run out of tokens, stop. */
4092 return;
4094 case CPP_SEMICOLON:
4095 /* If the next token is a `;', we have reached the end of the
4096 statement. */
4097 if (!nesting_depth)
4098 return;
4099 break;
4101 case CPP_CLOSE_BRACE:
4102 /* If this is a non-nested '}', stop before consuming it.
4103 That way, when confronted with something like:
4105 { 3 + }
4107 we stop before consuming the closing '}', even though we
4108 have not yet reached a `;'. */
4109 if (nesting_depth == 0)
4110 return;
4112 /* If it is the closing '}' for a block that we have
4113 scanned, stop -- but only after consuming the token.
4114 That way given:
4116 void f g () { ... }
4117 typedef int I;
4119 we will stop after the body of the erroneously declared
4120 function, but before consuming the following `typedef'
4121 declaration. */
4122 if (--nesting_depth == 0)
4124 cp_lexer_consume_token (parser->lexer);
4125 return;
4127 break;
4129 case CPP_OPEN_BRACE:
4130 ++nesting_depth;
4131 break;
4133 case CPP_KEYWORD:
4134 if (!cp_token_is_module_directive (token))
4135 break;
4136 /* FALLTHROUGH */
4138 case CPP_PRAGMA:
4139 /* We fell into a pragma. Skip it, and continue or return. */
4140 cp_parser_skip_to_pragma_eol (parser, token);
4141 if (!nesting_depth)
4142 return;
4143 continue;
4145 default:
4146 break;
4149 /* Consume the token. */
4150 cp_lexer_consume_token (parser->lexer);
4154 /* This function is called at the end of a statement or declaration.
4155 If the next token is a semicolon, it is consumed; otherwise, error
4156 recovery is attempted. */
4158 static void
4159 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
4161 /* Look for the trailing `;'. */
4162 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
4164 /* If there is additional (erroneous) input, skip to the end of
4165 the statement. */
4166 cp_parser_skip_to_end_of_statement (parser);
4167 /* If the next token is now a `;', consume it. */
4168 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
4169 cp_lexer_consume_token (parser->lexer);
4173 /* Skip tokens until we have consumed an entire block, or until we
4174 have consumed a non-nested `;'. */
4176 static void
4177 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
4179 int nesting_depth = 0;
4181 /* Unwind generic function template scope if necessary. */
4182 if (parser->fully_implicit_function_template_p)
4183 abort_fully_implicit_template (parser);
4185 while (nesting_depth >= 0)
4187 cp_token *token = cp_lexer_peek_token (parser->lexer);
4189 switch (token->type)
4191 case CPP_PRAGMA_EOL:
4192 if (!parser->lexer->in_pragma)
4193 break;
4194 /* FALLTHRU */
4195 case CPP_EOF:
4196 /* If we've run out of tokens, stop. */
4197 return;
4199 case CPP_SEMICOLON:
4200 /* Stop if this is an unnested ';'. */
4201 if (!nesting_depth)
4202 nesting_depth = -1;
4203 break;
4205 case CPP_CLOSE_BRACE:
4206 /* Stop if this is an unnested '}', or closes the outermost
4207 nesting level. */
4208 nesting_depth--;
4209 if (nesting_depth < 0)
4210 return;
4211 if (!nesting_depth)
4212 nesting_depth = -1;
4213 break;
4215 case CPP_OPEN_BRACE:
4216 /* Nest. */
4217 nesting_depth++;
4218 break;
4220 case CPP_KEYWORD:
4221 if (!cp_token_is_module_directive (token))
4222 break;
4223 /* FALLTHROUGH */
4225 case CPP_PRAGMA:
4226 /* Skip it, and continue or return. */
4227 cp_parser_skip_to_pragma_eol (parser, token);
4228 if (!nesting_depth)
4229 return;
4230 continue;
4232 default:
4233 break;
4236 /* Consume the token. */
4237 cp_lexer_consume_token (parser->lexer);
4241 /* Skip tokens until a non-nested closing curly brace is the next
4242 token, or there are no more tokens. Return true in the first case,
4243 false otherwise. */
4245 static bool
4246 cp_parser_skip_to_closing_brace (cp_parser *parser)
4248 unsigned nesting_depth = 0;
4250 while (true)
4252 cp_token *token = cp_lexer_peek_token (parser->lexer);
4254 switch (token->type)
4256 case CPP_PRAGMA_EOL:
4257 if (!parser->lexer->in_pragma)
4258 break;
4259 /* FALLTHRU */
4260 case CPP_EOF:
4261 /* If we've run out of tokens, stop. */
4262 return false;
4264 case CPP_CLOSE_BRACE:
4265 /* If the next token is a non-nested `}', then we have reached
4266 the end of the current block. */
4267 if (nesting_depth-- == 0)
4268 return true;
4269 break;
4271 case CPP_OPEN_BRACE:
4272 /* If it the next token is a `{', then we are entering a new
4273 block. Consume the entire block. */
4274 ++nesting_depth;
4275 break;
4277 default:
4278 break;
4281 /* Consume the token. */
4282 cp_lexer_consume_token (parser->lexer);
4286 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
4287 parameter is the PRAGMA token, allowing us to purge the entire pragma
4288 sequence. PRAGMA_TOK can be NULL, if we're speculatively scanning
4289 forwards (not error recovery). */
4291 static void
4292 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
4294 cp_token *token;
4298 /* The preprocessor makes sure that a PRAGMA_EOL token appears
4299 before an EOF token, even when the EOF is on the pragma line.
4300 We should never get here without being inside a deferred
4301 pragma. */
4302 gcc_checking_assert (cp_lexer_next_token_is_not (parser->lexer, CPP_EOF));
4303 token = cp_lexer_consume_token (parser->lexer);
4305 while (token->type != CPP_PRAGMA_EOL);
4307 if (pragma_tok)
4309 parser->lexer->in_pragma = false;
4310 if (parser->lexer->in_omp_attribute_pragma
4311 && cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4313 parser->lexer = parser->lexer->next;
4314 /* Put the current source position back where it was before this
4315 lexer was pushed. */
4316 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4321 /* Require pragma end of line, resyncing with it as necessary. The
4322 arguments are as for cp_parser_skip_to_pragma_eol. */
4324 static void
4325 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
4327 parser->lexer->in_pragma = false;
4328 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
4329 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
4330 else if (parser->lexer->in_omp_attribute_pragma
4331 && cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4333 parser->lexer = parser->lexer->next;
4334 /* Put the current source position back where it was before this
4335 lexer was pushed. */
4336 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4340 /* This is a simple wrapper around make_typename_type. When the id is
4341 an unresolved identifier node, we can provide a superior diagnostic
4342 using cp_parser_diagnose_invalid_type_name. */
4344 static tree
4345 cp_parser_make_typename_type (cp_parser *parser, tree id,
4346 location_t id_location)
4348 tree result;
4349 if (identifier_p (id))
4351 result = make_typename_type (parser->scope, id, typename_type,
4352 /*complain=*/tf_none);
4353 if (result == error_mark_node)
4354 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
4355 return result;
4357 return make_typename_type (parser->scope, id, typename_type, tf_error);
4360 /* This is a wrapper around the
4361 make_{pointer,ptrmem,reference}_declarator functions that decides
4362 which one to call based on the CODE and CLASS_TYPE arguments. The
4363 CODE argument should be one of the values returned by
4364 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
4365 appertain to the pointer or reference. */
4367 static cp_declarator *
4368 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
4369 cp_cv_quals cv_qualifiers,
4370 cp_declarator *target,
4371 tree attributes)
4373 if (code == ERROR_MARK || target == cp_error_declarator)
4374 return cp_error_declarator;
4376 if (code == INDIRECT_REF)
4377 if (class_type == NULL_TREE)
4378 return make_pointer_declarator (cv_qualifiers, target, attributes);
4379 else
4380 return make_ptrmem_declarator (cv_qualifiers, class_type,
4381 target, attributes);
4382 else if (code == ADDR_EXPR && class_type == NULL_TREE)
4383 return make_reference_declarator (cv_qualifiers, target,
4384 false, attributes);
4385 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
4386 return make_reference_declarator (cv_qualifiers, target,
4387 true, attributes);
4388 gcc_unreachable ();
4391 /* Create a new C++ parser. */
4393 static cp_parser *
4394 cp_parser_new (cp_lexer *lexer)
4396 /* Initialize the binops_by_token so that we can get the tree
4397 directly from the token. */
4398 for (unsigned i = 0; i < ARRAY_SIZE (binops); i++)
4399 binops_by_token[binops[i].token_type] = binops[i];
4401 cp_parser *parser = ggc_cleared_alloc<cp_parser> ();
4402 parser->lexer = lexer;
4403 parser->context = cp_parser_context_new (NULL);
4405 /* For now, we always accept GNU extensions. */
4406 parser->allow_gnu_extensions_p = 1;
4408 /* The `>' token is a greater-than operator, not the end of a
4409 template-id. */
4410 parser->greater_than_is_operator_p = true;
4412 parser->default_arg_ok_p = true;
4414 /* We are not parsing a constant-expression. */
4415 parser->integral_constant_expression_p = false;
4416 parser->allow_non_integral_constant_expression_p = false;
4417 parser->non_integral_constant_expression_p = false;
4419 /* Local variable names are not forbidden. */
4420 parser->local_variables_forbidden_p = 0;
4422 /* We are not processing an `extern "C"' declaration. */
4423 parser->in_unbraced_linkage_specification_p = false;
4425 /* We are not processing a declarator. */
4426 parser->in_declarator_p = false;
4428 /* We are not processing a template-argument-list. */
4429 parser->in_template_argument_list_p = false;
4431 /* We are not in an iteration statement. */
4432 parser->in_statement = 0;
4434 /* We are not in a switch statement. */
4435 parser->in_switch_statement_p = false;
4437 /* We are not parsing a type-id inside an expression. */
4438 parser->in_type_id_in_expr_p = false;
4440 /* String literals should be translated to the execution character set. */
4441 parser->translate_strings_p = true;
4443 /* We are not parsing a function body. */
4444 parser->in_function_body = false;
4446 /* We can correct until told otherwise. */
4447 parser->colon_corrects_to_scope_p = true;
4449 /* The unparsed function queue is empty. */
4450 push_unparsed_function_queues (parser);
4452 /* There are no classes being defined. */
4453 parser->num_classes_being_defined = 0;
4455 /* No template parameters apply. */
4456 parser->num_template_parameter_lists = 0;
4458 /* Special parsing data structures. */
4459 parser->omp_declare_simd = NULL;
4460 parser->oacc_routine = NULL;
4462 /* Disallow OpenMP array sections in expressions. */
4463 parser->omp_array_section_p = false;
4465 /* Not declaring an implicit function template. */
4466 parser->auto_is_implicit_function_template_parm_p = false;
4467 parser->fully_implicit_function_template_p = false;
4468 parser->implicit_template_parms = 0;
4469 parser->implicit_template_scope = 0;
4471 /* Allow constrained-type-specifiers. */
4472 parser->prevent_constrained_type_specifiers = 0;
4474 /* We haven't yet seen an 'extern "C"'. */
4475 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
4477 return parser;
4480 /* Create a cp_lexer structure which will emit the tokens in CACHE
4481 and push it onto the parser's lexer stack. This is used for delayed
4482 parsing of in-class method bodies and default arguments, and should
4483 not be confused with tentative parsing. */
4484 static void
4485 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
4487 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
4488 lexer->next = parser->lexer;
4489 parser->lexer = lexer;
4491 /* Move the current source position to that of the first token in the
4492 new lexer. */
4493 cp_lexer_set_source_position_from_token (lexer->next_token);
4496 /* Pop the top lexer off the parser stack. This is never used for the
4497 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
4498 static void
4499 cp_parser_pop_lexer (cp_parser *parser)
4501 cp_lexer *lexer = parser->lexer;
4502 parser->lexer = lexer->next;
4503 cp_lexer_destroy (lexer);
4505 /* Put the current source position back where it was before this
4506 lexer was pushed. */
4507 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4510 /* Lexical conventions [gram.lex] */
4512 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
4513 identifier. */
4515 static cp_expr
4516 cp_parser_identifier (cp_parser* parser)
4518 cp_token *token;
4520 /* Look for the identifier. */
4521 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
4522 /* Return the value. */
4523 if (token)
4524 return cp_expr (token->u.value, token->location);
4525 else
4526 return error_mark_node;
4529 /* Worker for cp_parser_string_literal, cp_parser_userdef_string_literal
4530 and cp_parser_unevaluated_string_literal.
4531 Do not call this directly; use either of the above.
4533 Parse a sequence of adjacent string constants. Return a
4534 TREE_STRING representing the combined, nul-terminated string
4535 constant. If TRANSLATE is true, translate the string to the
4536 execution character set. If WIDE_OK is true, a wide string is
4537 valid here. If UDL_OK is true, a string literal with user-defined
4538 suffix can be used in this context. If UNEVAL is true, diagnose
4539 numeric and conditional escape sequences in it if pedantic.
4541 C++98 [lex.string] says that if a narrow string literal token is
4542 adjacent to a wide string literal token, the behavior is undefined.
4543 However, C99 6.4.5p4 says that this results in a wide string literal.
4544 We follow C99 here, for consistency with the C front end.
4546 This code is largely lifted from lex_string() in c-lex.cc.
4548 FUTURE: ObjC++ will need to handle @-strings here. */
4550 static cp_expr
4551 cp_parser_string_literal_common (cp_parser *parser, bool translate,
4552 bool wide_ok, bool udl_ok,
4553 bool lookup_udlit, bool uneval)
4555 tree value;
4556 size_t count;
4557 struct obstack str_ob;
4558 struct obstack loc_ob;
4559 cpp_string str, istr, *strs;
4560 cp_token *tok;
4561 enum cpp_ttype type, curr_type;
4562 int have_suffix_p = 0;
4563 tree string_tree;
4564 tree suffix_id = NULL_TREE;
4565 bool curr_tok_is_userdef_p = false;
4567 tok = cp_lexer_peek_token (parser->lexer);
4568 if (!cp_parser_is_string_literal (tok))
4570 cp_parser_error (parser, "expected string-literal");
4571 return error_mark_node;
4574 location_t loc = tok->location;
4576 if (cpp_userdef_string_p (tok->type))
4578 if (!udl_ok)
4580 error_at (loc, "string literal with user-defined suffix "
4581 "is invalid in this context");
4582 return error_mark_node;
4584 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4585 curr_type = cpp_userdef_string_remove_type (tok->type);
4586 curr_tok_is_userdef_p = true;
4588 else
4590 string_tree = tok->u.value;
4591 curr_type = tok->type;
4593 type = curr_type;
4595 /* Try to avoid the overhead of creating and destroying an obstack
4596 for the common case of just one string. */
4597 if (!cp_parser_is_string_literal
4598 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4600 cp_lexer_consume_token (parser->lexer);
4602 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4603 str.len = TREE_STRING_LENGTH (string_tree);
4604 count = 1;
4606 if (curr_tok_is_userdef_p)
4608 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4609 have_suffix_p = 1;
4610 curr_type = cpp_userdef_string_remove_type (tok->type);
4612 else
4613 curr_type = tok->type;
4615 strs = &str;
4617 else
4619 location_t last_tok_loc = tok->location;
4620 gcc_obstack_init (&str_ob);
4621 gcc_obstack_init (&loc_ob);
4622 count = 0;
4626 cp_lexer_consume_token (parser->lexer);
4627 count++;
4628 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4629 str.len = TREE_STRING_LENGTH (string_tree);
4631 if (curr_tok_is_userdef_p)
4633 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4634 if (have_suffix_p == 0)
4636 suffix_id = curr_suffix_id;
4637 have_suffix_p = 1;
4639 else if (have_suffix_p == 1
4640 && curr_suffix_id != suffix_id)
4642 error ("inconsistent user-defined literal suffixes"
4643 " %qD and %qD in string literal",
4644 suffix_id, curr_suffix_id);
4645 have_suffix_p = -1;
4647 curr_type = cpp_userdef_string_remove_type (tok->type);
4649 else
4650 curr_type = tok->type;
4652 if (type != curr_type)
4654 if (type == CPP_STRING)
4655 type = curr_type;
4656 else if (curr_type != CPP_STRING)
4658 rich_location rich_loc (line_table, tok->location);
4659 rich_loc.add_range (last_tok_loc);
4660 error_at (&rich_loc,
4661 "concatenation of string literals with "
4662 "conflicting encoding prefixes");
4666 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4667 obstack_grow (&loc_ob, &tok->location, sizeof (location_t));
4669 last_tok_loc = tok->location;
4671 tok = cp_lexer_peek_token (parser->lexer);
4672 if (cpp_userdef_string_p (tok->type))
4674 if (!udl_ok)
4676 error_at (loc, "string literal with user-defined suffix "
4677 "is invalid in this context");
4678 return error_mark_node;
4680 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4681 curr_type = cpp_userdef_string_remove_type (tok->type);
4682 curr_tok_is_userdef_p = true;
4684 else
4686 string_tree = tok->u.value;
4687 curr_type = tok->type;
4688 curr_tok_is_userdef_p = false;
4691 while (cp_parser_is_string_literal (tok));
4693 /* A string literal built by concatenation has its caret=start at
4694 the start of the initial string, and its finish at the finish of
4695 the final string literal. */
4696 loc = make_location (loc, loc, get_finish (last_tok_loc));
4698 strs = (cpp_string *) obstack_finish (&str_ob);
4701 if (type != CPP_STRING && !wide_ok)
4703 cp_parser_error (parser, "a wide string is invalid in this context");
4704 type = CPP_STRING;
4706 if (uneval)
4707 type = CPP_UNEVAL_STRING;
4709 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4710 (parse_in, strs, count, &istr, type))
4712 value = build_string (istr.len, (const char *)istr.text);
4713 free (CONST_CAST (unsigned char *, istr.text));
4714 if (count > 1)
4716 location_t *locs = (location_t *)obstack_finish (&loc_ob);
4717 gcc_assert (g_string_concat_db);
4718 g_string_concat_db->record_string_concatenation (count, locs);
4721 switch (type)
4723 default:
4724 case CPP_STRING:
4725 TREE_TYPE (value) = char_array_type_node;
4726 break;
4727 case CPP_UTF8STRING:
4728 if (flag_char8_t)
4729 TREE_TYPE (value) = char8_array_type_node;
4730 else
4731 TREE_TYPE (value) = char_array_type_node;
4732 break;
4733 case CPP_STRING16:
4734 TREE_TYPE (value) = char16_array_type_node;
4735 break;
4736 case CPP_STRING32:
4737 TREE_TYPE (value) = char32_array_type_node;
4738 break;
4739 case CPP_WSTRING:
4740 TREE_TYPE (value) = wchar_array_type_node;
4741 break;
4744 value = fix_string_type (value);
4746 if (have_suffix_p)
4748 tree literal = build_userdef_literal (suffix_id, value,
4749 OT_NONE, NULL_TREE);
4750 if (lookup_udlit)
4751 value = finish_userdef_string_literal (literal);
4752 else
4753 value = literal;
4756 else
4757 /* cpp_interpret_string has issued an error. */
4758 value = error_mark_node;
4760 if (count > 1)
4762 obstack_free (&str_ob, 0);
4763 obstack_free (&loc_ob, 0);
4766 return cp_expr (value, loc);
4769 /* Parse a sequence of adjacent string constants. Return a TREE_STRING
4770 representing the combined, nul-terminated string constant. If
4771 TRANSLATE is true, translate the string to the execution character set.
4772 If WIDE_OK is true, a wide string is valid here.
4774 This function issues an error if a user defined string literal is
4775 encountered; use cp_parser_userdef_string_literal if UDLs are allowed. */
4777 static inline cp_expr
4778 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
4780 return cp_parser_string_literal_common (parser, translate, wide_ok,
4781 /*udl_ok=*/false,
4782 /*lookup_udlit=*/false,
4783 /*uneval=*/false);
4786 /* Parse a string literal or user defined string literal.
4788 user-defined-string-literal :
4789 string-literal ud-suffix
4791 If LOOKUP_UDLIT, perform a lookup for a suitable template function. */
4793 static inline cp_expr
4794 cp_parser_userdef_string_literal (cp_parser *parser, bool lookup_udlit)
4796 return cp_parser_string_literal_common (parser, /*translate=*/true,
4797 /*wide_ok=*/true, /*udl_ok=*/true,
4798 lookup_udlit, /*uneval=*/false);
4801 /* Parse an unevaluated string literal.
4803 unevaluated-string:
4804 string-literal */
4806 static inline cp_expr
4807 cp_parser_unevaluated_string_literal (cp_parser *parser)
4809 return cp_parser_string_literal_common (parser, /*translate=*/false,
4810 /*wide_ok=*/false, /*udl_ok=*/false,
4811 /*lookup_udlit=*/false,
4812 /*uneval=*/true);
4815 /* Look up a literal operator with the name and the exact arguments. */
4817 static tree
4818 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4820 tree decl = lookup_name (name);
4821 if (!decl || !is_overloaded_fn (decl))
4822 return error_mark_node;
4824 for (lkp_iterator iter (decl); iter; ++iter)
4826 tree fn = *iter;
4828 if (tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn)))
4830 unsigned int ix;
4831 bool found = true;
4833 for (ix = 0;
4834 found && ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4835 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4837 tree tparm = TREE_VALUE (parmtypes);
4838 tree targ = TREE_TYPE ((*args)[ix]);
4839 bool ptr = TYPE_PTR_P (tparm);
4840 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4841 if ((ptr || arr || !same_type_p (tparm, targ))
4842 && (!ptr || !arr
4843 || !same_type_p (TREE_TYPE (tparm),
4844 TREE_TYPE (targ))))
4845 found = false;
4848 if (found
4849 && ix == vec_safe_length (args)
4850 /* May be this should be sufficient_parms_p instead,
4851 depending on how exactly should user-defined literals
4852 work in presence of default arguments on the literal
4853 operator parameters. */
4854 && parmtypes == void_list_node)
4855 return decl;
4859 return error_mark_node;
4862 /* Parse a user-defined char constant. Returns a call to a user-defined
4863 literal operator taking the character as an argument. */
4865 static cp_expr
4866 cp_parser_userdef_char_literal (cp_parser *parser)
4868 cp_token *token = cp_lexer_consume_token (parser->lexer);
4869 tree literal = token->u.value;
4870 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4871 tree value = USERDEF_LITERAL_VALUE (literal);
4872 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4873 tree decl, result;
4875 /* Build up a call to the user-defined operator */
4876 /* Lookup the name we got back from the id-expression. */
4877 releasing_vec args;
4878 vec_safe_push (args, value);
4879 decl = lookup_literal_operator (name, args);
4880 if (!decl || decl == error_mark_node)
4882 error ("unable to find character literal operator %qD with %qT argument",
4883 name, TREE_TYPE (value));
4884 return error_mark_node;
4886 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4887 return result;
4890 /* A subroutine of cp_parser_userdef_numeric_literal to
4891 create a char... template parameter pack from a string node. */
4893 static tree
4894 make_char_string_pack (tree value)
4896 tree charvec;
4897 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4898 const unsigned char *str
4899 = (const unsigned char *) TREE_STRING_POINTER (value);
4900 int i, len = TREE_STRING_LENGTH (value) - 1;
4901 tree argvec = make_tree_vec (1);
4903 /* Fill in CHARVEC with all of the parameters. */
4904 charvec = make_tree_vec (len);
4905 for (i = 0; i < len; ++i)
4907 unsigned char s[3] = { '\'', str[i], '\'' };
4908 cpp_string in = { 3, s };
4909 cpp_string out = { 0, 0 };
4910 if (!cpp_interpret_string (parse_in, &in, 1, &out, CPP_STRING))
4911 return NULL_TREE;
4912 gcc_assert (out.len == 2);
4913 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node,
4914 out.text[0]);
4917 /* Build the argument packs. */
4918 ARGUMENT_PACK_ARGS (argpack) = charvec;
4920 TREE_VEC_ELT (argvec, 0) = argpack;
4922 return argvec;
4925 /* A subroutine of cp_parser_userdef_numeric_literal to
4926 create a char... template parameter pack from a string node. */
4928 static tree
4929 make_string_pack (tree value)
4931 tree charvec;
4932 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4933 const unsigned char *str
4934 = (const unsigned char *) TREE_STRING_POINTER (value);
4935 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4936 int len = TREE_STRING_LENGTH (value) / sz - 1;
4937 tree argvec = make_tree_vec (2);
4939 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4940 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4942 /* First template parm is character type. */
4943 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4945 /* Fill in CHARVEC with all of the parameters. */
4946 charvec = make_tree_vec (len);
4947 for (int i = 0; i < len; ++i)
4948 TREE_VEC_ELT (charvec, i)
4949 = double_int_to_tree (str_char_type_node,
4950 double_int::from_buffer (str + i * sz, sz));
4952 /* Build the argument packs. */
4953 ARGUMENT_PACK_ARGS (argpack) = charvec;
4955 TREE_VEC_ELT (argvec, 1) = argpack;
4957 return argvec;
4960 /* Parse a user-defined numeric constant. returns a call to a user-defined
4961 literal operator. */
4963 static cp_expr
4964 cp_parser_userdef_numeric_literal (cp_parser *parser)
4966 cp_token *token = cp_lexer_consume_token (parser->lexer);
4967 tree literal = token->u.value;
4968 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4969 tree value = USERDEF_LITERAL_VALUE (literal);
4970 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4971 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4972 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4973 tree decl, result;
4975 /* Look for a literal operator taking the exact type of numeric argument
4976 as the literal value. */
4977 releasing_vec args;
4978 vec_safe_push (args, value);
4979 decl = lookup_literal_operator (name, args);
4980 if (decl && decl != error_mark_node)
4982 result = finish_call_expr (decl, &args, false, true,
4983 tf_warning_or_error);
4985 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4987 warning_at (token->location, OPT_Woverflow,
4988 "integer literal exceeds range of %qT type",
4989 long_long_unsigned_type_node);
4991 else
4993 if (overflow > 0)
4994 warning_at (token->location, OPT_Woverflow,
4995 "floating literal exceeds range of %qT type",
4996 long_double_type_node);
4997 else if (overflow < 0)
4998 warning_at (token->location, OPT_Woverflow,
4999 "floating literal truncated to zero");
5002 return result;
5005 /* If the numeric argument didn't work, look for a raw literal
5006 operator taking a const char* argument consisting of the number
5007 in string format. */
5008 args->truncate (0);
5009 vec_safe_push (args, num_string);
5010 decl = lookup_literal_operator (name, args);
5011 if (decl && decl != error_mark_node)
5013 result = finish_call_expr (decl, &args, false, true,
5014 tf_warning_or_error);
5015 return result;
5018 /* If the raw literal didn't work, look for a non-type template
5019 function with parameter pack char.... Call the function with
5020 template parameter characters representing the number. */
5021 args->truncate (0);
5022 decl = lookup_literal_operator (name, args);
5023 if (decl && decl != error_mark_node)
5025 tree tmpl_args = make_char_string_pack (num_string);
5026 if (tmpl_args == NULL_TREE)
5028 error ("failed to translate literal to execution character set %qT",
5029 num_string);
5030 return error_mark_node;
5032 decl = lookup_template_function (decl, tmpl_args);
5033 result = finish_call_expr (decl, &args, false, true,
5034 tf_warning_or_error);
5035 return result;
5038 /* In C++14 the standard library defines complex number suffixes that
5039 conflict with GNU extensions. Prefer them if <complex> is #included. */
5040 bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
5041 bool i14 = (cxx_dialect > cxx11
5042 && (id_equal (suffix_id, "i")
5043 || id_equal (suffix_id, "if")
5044 || id_equal (suffix_id, "il")));
5045 diagnostic_t kind = DK_ERROR;
5046 int opt = 0;
5048 if (i14 && ext)
5050 tree cxlit = lookup_qualified_name (std_node, "complex_literals",
5051 LOOK_want::NORMAL, false);
5052 if (cxlit == error_mark_node)
5054 /* No <complex>, so pedwarn and use GNU semantics. */
5055 kind = DK_PEDWARN;
5056 opt = OPT_Wpedantic;
5060 bool complained
5061 = emit_diagnostic (kind, input_location, opt,
5062 "unable to find numeric literal operator %qD", name);
5064 if (!complained)
5065 /* Don't inform either. */;
5066 else if (i14)
5068 inform (token->location, "add %<using namespace std::complex_literals%> "
5069 "(from %<<complex>%>) to enable the C++14 user-defined literal "
5070 "suffixes");
5071 if (ext)
5072 inform (token->location, "or use %<j%> instead of %<i%> for the "
5073 "GNU built-in suffix");
5075 else if (!ext)
5076 inform (token->location, "use %<-fext-numeric-literals%> "
5077 "to enable more built-in suffixes");
5079 if (kind == DK_ERROR)
5080 value = error_mark_node;
5081 else
5083 /* Use the built-in semantics. */
5084 tree type;
5085 if (id_equal (suffix_id, "i"))
5087 if (TREE_CODE (value) == INTEGER_CST)
5088 type = integer_type_node;
5089 else
5090 type = double_type_node;
5092 else if (id_equal (suffix_id, "if"))
5093 type = float_type_node;
5094 else /* if (id_equal (suffix_id, "il")) */
5095 type = long_double_type_node;
5097 value = fold_build2 (COMPLEX_EXPR, build_complex_type (type),
5098 build_zero_cst (type), fold_convert (type, value));
5101 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5102 /* Avoid repeated diagnostics. */
5103 token->u.value = value;
5104 return value;
5107 /* Parse a user-defined string constant. Returns a call to a user-defined
5108 literal operator taking a character pointer and the length of the string
5109 as arguments. */
5111 static tree
5112 finish_userdef_string_literal (tree literal)
5114 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
5115 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
5116 tree value = USERDEF_LITERAL_VALUE (literal);
5117 int len = TREE_STRING_LENGTH (value)
5118 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
5119 tree decl;
5121 /* Build up a call to the user-defined operator. */
5122 /* Lookup the name we got back from the id-expression. */
5123 releasing_vec args;
5124 vec_safe_push (args, value);
5125 vec_safe_push (args, build_int_cst (size_type_node, len));
5126 decl = lookup_literal_operator (name, args);
5128 if (decl && decl != error_mark_node)
5129 return finish_call_expr (decl, &args, false, true,
5130 tf_warning_or_error);
5132 /* Look for a suitable template function, either (C++20) with a single
5133 parameter of class type, or (N3599) with typename parameter CharT and
5134 parameter pack CharT... */
5135 args->truncate (0);
5136 decl = lookup_literal_operator (name, args);
5137 if (decl && decl != error_mark_node)
5139 /* Use resolve_nondeduced_context to try to choose one form of template
5140 or the other. */
5141 tree tmpl_args = make_tree_vec (1);
5142 TREE_VEC_ELT (tmpl_args, 0) = value;
5143 decl = lookup_template_function (decl, tmpl_args);
5144 tree res = resolve_nondeduced_context (decl, tf_none);
5145 if (DECL_P (res))
5146 decl = res;
5147 else
5149 TREE_OPERAND (decl, 1) = make_string_pack (value);
5150 res = resolve_nondeduced_context (decl, tf_none);
5151 if (DECL_P (res))
5152 decl = res;
5154 if (!DECL_P (decl) && cxx_dialect > cxx17)
5155 TREE_OPERAND (decl, 1) = tmpl_args;
5156 return finish_call_expr (decl, &args, false, true,
5157 tf_warning_or_error);
5160 error ("unable to find string literal operator %qD with %qT, %qT arguments",
5161 name, TREE_TYPE (value), size_type_node);
5162 return error_mark_node;
5166 /* Basic concepts [gram.basic] */
5168 /* Parse a translation-unit.
5170 translation-unit:
5171 declaration-seq [opt] */
5173 static void
5174 cp_parser_translation_unit (cp_parser* parser)
5176 gcc_checking_assert (!cp_error_declarator);
5178 /* Create the declarator obstack. */
5179 gcc_obstack_init (&declarator_obstack);
5180 /* Create the error declarator. */
5181 cp_error_declarator = make_declarator (cdk_error);
5182 /* Create the empty parameter list. */
5183 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
5184 UNKNOWN_LOCATION);
5185 /* Remember where the base of the declarator obstack lies. */
5186 void *declarator_obstack_base = obstack_next_free (&declarator_obstack);
5188 push_deferring_access_checks (flag_access_control
5189 ? dk_no_deferred : dk_no_check);
5191 module_parse mp_state = MP_NOT_MODULE;
5192 if (modules_p () && !header_module_p ())
5193 mp_state = MP_FIRST;
5195 bool implicit_extern_c = false;
5197 /* Parse until EOF. */
5198 for (;;)
5200 cp_token *token = cp_lexer_peek_token (parser->lexer);
5202 /* If we're entering or exiting a region that's implicitly
5203 extern "C", modify the lang context appropriately. This is
5204 so horrible. Please die. */
5205 if (implicit_extern_c
5206 != cp_lexer_peek_token (parser->lexer)->implicit_extern_c)
5208 implicit_extern_c = !implicit_extern_c;
5209 if (implicit_extern_c)
5210 push_lang_context (lang_name_c);
5211 else
5212 pop_lang_context ();
5215 if (token->type == CPP_EOF)
5216 break;
5218 if (modules_p ())
5220 /* Top-level module declarations are ok, and change the
5221 portion of file we're in. Top-level import declarations
5222 are significant for the import portions. */
5224 cp_token *next = token;
5225 bool exporting = token->keyword == RID__EXPORT;
5226 if (exporting)
5228 cp_lexer_consume_token (parser->lexer);
5229 next = cp_lexer_peek_token (parser->lexer);
5231 if (next->keyword == RID__MODULE)
5233 mp_state
5234 = cp_parser_module_declaration (parser, mp_state, exporting);
5235 continue;
5237 else if (next->keyword == RID__IMPORT)
5239 if (mp_state == MP_FIRST)
5240 mp_state = MP_NOT_MODULE;
5241 cp_parser_import_declaration (parser, mp_state, exporting);
5242 continue;
5244 else
5245 gcc_checking_assert (!exporting);
5247 if (mp_state == MP_GLOBAL && token->main_source_p)
5249 static bool warned = false;
5250 if (!warned)
5252 warned = true;
5253 error_at (token->location,
5254 "global module fragment contents must be"
5255 " from preprocessor inclusion");
5260 /* This relies on the ordering of module_parse values. */
5261 if (mp_state == MP_PURVIEW_IMPORTS || mp_state == MP_PRIVATE_IMPORTS)
5262 /* We're no longer in the import portion of a named module. */
5263 mp_state = module_parse (mp_state + 1);
5264 else if (mp_state == MP_FIRST)
5265 mp_state = MP_NOT_MODULE;
5267 if (token->type == CPP_CLOSE_BRACE)
5269 cp_parser_error (parser, "expected declaration");
5270 cp_lexer_consume_token (parser->lexer);
5271 /* If the next token is now a `;', consume it. */
5272 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
5273 cp_lexer_consume_token (parser->lexer);
5275 else
5276 cp_parser_toplevel_declaration (parser);
5279 /* Get rid of the token array; we don't need it any more. */
5280 cp_lexer_destroy (parser->lexer);
5281 parser->lexer = NULL;
5283 /* The EOF should have reset this. */
5284 gcc_checking_assert (!implicit_extern_c);
5286 /* Make sure the declarator obstack was fully cleaned up. */
5287 gcc_assert (obstack_next_free (&declarator_obstack)
5288 == declarator_obstack_base);
5291 /* Return the appropriate tsubst flags for parsing, possibly in N3276
5292 decltype context. */
5294 static inline tsubst_flags_t
5295 complain_flags (bool decltype_p)
5297 tsubst_flags_t complain = tf_warning_or_error;
5298 if (decltype_p)
5299 complain |= tf_decltype;
5300 return complain;
5303 /* We're about to parse a collection of statements. If we're currently
5304 parsing tentatively, set up a firewall so that any nested
5305 cp_parser_commit_to_tentative_parse won't affect the current context. */
5307 static cp_token_position
5308 cp_parser_start_tentative_firewall (cp_parser *parser)
5310 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5311 return 0;
5313 cp_parser_parse_tentatively (parser);
5314 cp_parser_commit_to_topmost_tentative_parse (parser);
5315 return cp_lexer_token_position (parser->lexer, false);
5318 /* We've finished parsing the collection of statements. Wrap up the
5319 firewall and replace the relevant tokens with the parsed form. */
5321 static void
5322 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
5323 tree expr)
5325 if (!start)
5326 return;
5328 /* Finish the firewall level. */
5329 cp_parser_parse_definitely (parser);
5330 /* And remember the result of the parse for when we try again. */
5331 cp_token *token = cp_lexer_token_at (parser->lexer, start);
5332 token->type = CPP_PREPARSED_EXPR;
5333 token->u.value = expr;
5334 token->keyword = RID_MAX;
5335 cp_lexer_purge_tokens_after (parser->lexer, start);
5338 /* Like the above functions, but let the user modify the tokens. Used by
5339 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
5340 later parses, so it makes sense to localize the effects of
5341 cp_parser_commit_to_tentative_parse. */
5343 struct tentative_firewall
5345 cp_parser *parser;
5346 bool set;
5348 tentative_firewall (cp_parser *p): parser(p)
5350 /* If we're currently parsing tentatively, start a committed level as a
5351 firewall and then an inner tentative parse. */
5352 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
5354 cp_parser_parse_tentatively (parser);
5355 cp_parser_commit_to_topmost_tentative_parse (parser);
5356 cp_parser_parse_tentatively (parser);
5360 ~tentative_firewall()
5362 if (set)
5364 /* Finish the inner tentative parse and the firewall, propagating any
5365 uncommitted error state to the outer tentative parse. */
5366 bool err = cp_parser_error_occurred (parser);
5367 cp_parser_parse_definitely (parser);
5368 cp_parser_parse_definitely (parser);
5369 if (err)
5370 cp_parser_simulate_error (parser);
5375 /* Some tokens naturally come in pairs e.g.'(' and ')'.
5376 This class is for tracking such a matching pair of symbols.
5377 In particular, it tracks the location of the first token,
5378 so that if the second token is missing, we can highlight the
5379 location of the first token when notifying the user about the
5380 problem. */
5382 template <typename traits_t>
5383 class token_pair
5385 public:
5386 /* token_pair's ctor. */
5387 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
5389 /* If the next token is the opening symbol for this pair, consume it and
5390 return true.
5391 Otherwise, issue an error and return false.
5392 In either case, record the location of the opening token. */
5394 bool require_open (cp_parser *parser)
5396 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
5397 return cp_parser_require (parser, traits_t::open_token_type,
5398 traits_t::required_token_open);
5401 /* Consume the next token from PARSER, recording its location as
5402 that of the opening token within the pair. */
5404 cp_token * consume_open (cp_parser *parser)
5406 cp_token *tok = cp_lexer_consume_token (parser->lexer);
5407 gcc_assert (tok->type == traits_t::open_token_type);
5408 m_open_loc = tok->location;
5409 return tok;
5412 /* If the next token is the closing symbol for this pair, consume it
5413 and return it.
5414 Otherwise, issue an error, highlighting the location of the
5415 corresponding opening token, and return NULL. */
5417 cp_token *require_close (cp_parser *parser) const
5419 return cp_parser_require (parser, traits_t::close_token_type,
5420 traits_t::required_token_close,
5421 m_open_loc);
5424 location_t open_location () const { return m_open_loc; }
5426 private:
5427 location_t m_open_loc;
5430 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
5432 struct matching_paren_traits
5434 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
5435 static const enum required_token required_token_open = RT_OPEN_PAREN;
5436 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
5437 static const enum required_token required_token_close = RT_CLOSE_PAREN;
5440 /* "matching_parens" is a token_pair<T> class for tracking matching
5441 pairs of parentheses. */
5443 typedef token_pair<matching_paren_traits> matching_parens;
5445 /* Traits for token_pair<T> for tracking matching pairs of braces. */
5447 struct matching_brace_traits
5449 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
5450 static const enum required_token required_token_open = RT_OPEN_BRACE;
5451 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
5452 static const enum required_token required_token_close = RT_CLOSE_BRACE;
5455 /* "matching_braces" is a token_pair<T> class for tracking matching
5456 pairs of braces. */
5458 typedef token_pair<matching_brace_traits> matching_braces;
5461 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
5462 enclosing parentheses. */
5464 static cp_expr
5465 cp_parser_statement_expr (cp_parser *parser)
5467 cp_token_position start = cp_parser_start_tentative_firewall (parser);
5468 auto oas = make_temp_override (parser->omp_array_section_p, false);
5470 /* Consume the '('. */
5471 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
5472 matching_parens parens;
5473 parens.consume_open (parser);
5474 /* Start the statement-expression. */
5475 tree expr = begin_stmt_expr ();
5476 /* Parse the compound-statement. */
5477 cp_parser_compound_statement (parser, expr, BCS_STMT_EXPR, false);
5478 /* Finish up. */
5479 expr = finish_stmt_expr (expr, false);
5480 /* Consume the ')'. */
5481 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
5482 if (!parens.require_close (parser))
5483 cp_parser_skip_to_end_of_statement (parser);
5485 cp_parser_end_tentative_firewall (parser, start, expr);
5486 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
5487 return cp_expr (expr, combined_loc);
5490 /* Expressions [gram.expr] */
5492 /* Parse a fold-operator.
5494 fold-operator:
5495 - * / % ^ & | = < > << >>
5496 = -= *= /= %= ^= &= |= <<= >>=
5497 == != <= >= && || , .* ->*
5499 This returns the tree code corresponding to the matched operator
5500 as an int. When the current token matches a compound assignment
5501 operator, the resulting tree code is the negative value of the
5502 non-assignment operator. */
5504 static int
5505 cp_parser_fold_operator (cp_token *token)
5507 switch (token->type)
5509 case CPP_PLUS: return PLUS_EXPR;
5510 case CPP_MINUS: return MINUS_EXPR;
5511 case CPP_MULT: return MULT_EXPR;
5512 case CPP_DIV: return TRUNC_DIV_EXPR;
5513 case CPP_MOD: return TRUNC_MOD_EXPR;
5514 case CPP_XOR: return BIT_XOR_EXPR;
5515 case CPP_AND: return BIT_AND_EXPR;
5516 case CPP_OR: return BIT_IOR_EXPR;
5517 case CPP_LSHIFT: return LSHIFT_EXPR;
5518 case CPP_RSHIFT: return RSHIFT_EXPR;
5520 case CPP_EQ: return -NOP_EXPR;
5521 case CPP_PLUS_EQ: return -PLUS_EXPR;
5522 case CPP_MINUS_EQ: return -MINUS_EXPR;
5523 case CPP_MULT_EQ: return -MULT_EXPR;
5524 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
5525 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
5526 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
5527 case CPP_AND_EQ: return -BIT_AND_EXPR;
5528 case CPP_OR_EQ: return -BIT_IOR_EXPR;
5529 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
5530 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
5532 case CPP_EQ_EQ: return EQ_EXPR;
5533 case CPP_NOT_EQ: return NE_EXPR;
5534 case CPP_LESS: return LT_EXPR;
5535 case CPP_GREATER: return GT_EXPR;
5536 case CPP_LESS_EQ: return LE_EXPR;
5537 case CPP_GREATER_EQ: return GE_EXPR;
5539 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
5540 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
5542 case CPP_COMMA: return COMPOUND_EXPR;
5544 case CPP_DOT_STAR: return DOTSTAR_EXPR;
5545 case CPP_DEREF_STAR: return MEMBER_REF;
5547 default: return ERROR_MARK;
5551 /* Returns true if CODE indicates a binary expression, which is not allowed in
5552 the LHS of a fold-expression. More codes will need to be added to use this
5553 function in other contexts. */
5555 static bool
5556 is_binary_op (tree_code code)
5558 switch (code)
5560 case PLUS_EXPR:
5561 case POINTER_PLUS_EXPR:
5562 case MINUS_EXPR:
5563 case MULT_EXPR:
5564 case TRUNC_DIV_EXPR:
5565 case TRUNC_MOD_EXPR:
5566 case BIT_XOR_EXPR:
5567 case BIT_AND_EXPR:
5568 case BIT_IOR_EXPR:
5569 case LSHIFT_EXPR:
5570 case RSHIFT_EXPR:
5572 case MODOP_EXPR:
5574 case EQ_EXPR:
5575 case NE_EXPR:
5576 case LE_EXPR:
5577 case GE_EXPR:
5578 case LT_EXPR:
5579 case GT_EXPR:
5581 case TRUTH_ANDIF_EXPR:
5582 case TRUTH_ORIF_EXPR:
5584 case COMPOUND_EXPR:
5586 case DOTSTAR_EXPR:
5587 case MEMBER_REF:
5588 return true;
5590 default:
5591 return false;
5595 /* If the next token is a suitable fold operator, consume it and return as
5596 the function above. */
5598 static int
5599 cp_parser_fold_operator (cp_parser *parser)
5601 cp_token* token = cp_lexer_peek_token (parser->lexer);
5602 int code = cp_parser_fold_operator (token);
5603 if (code != ERROR_MARK)
5604 cp_lexer_consume_token (parser->lexer);
5605 return code;
5608 /* Parse a fold-expression.
5610 fold-expression:
5611 ( ... folding-operator cast-expression)
5612 ( cast-expression folding-operator ... )
5613 ( cast-expression folding operator ... folding-operator cast-expression)
5615 Note that the '(' and ')' are matched in primary expression. */
5617 static cp_expr
5618 cp_parser_fold_expression (cp_parser *parser, tree expr1)
5620 cp_id_kind pidk;
5621 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5622 const cp_token *token = nullptr;
5624 // Left fold.
5625 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5627 if (expr1)
5628 return error_mark_node;
5629 cp_lexer_consume_token (parser->lexer);
5630 token = cp_lexer_peek_token (parser->lexer);
5631 int op = cp_parser_fold_operator (parser);
5632 if (op == ERROR_MARK)
5634 cp_parser_error (parser, "expected binary operator");
5635 return error_mark_node;
5638 tree expr = cp_parser_cast_expression (parser, false, false,
5639 false, &pidk);
5640 if (expr == error_mark_node)
5641 return error_mark_node;
5642 loc = make_location (token->location, loc, parser->lexer);
5643 return finish_left_unary_fold_expr (loc, expr, op);
5646 token = cp_lexer_peek_token (parser->lexer);
5647 int op = cp_parser_fold_operator (parser);
5648 if (op == ERROR_MARK)
5650 cp_parser_error (parser, "expected binary operator");
5651 return error_mark_node;
5654 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
5656 cp_parser_error (parser, "expected ...");
5657 return error_mark_node;
5659 cp_lexer_consume_token (parser->lexer);
5661 /* The operands of a fold-expression are cast-expressions, so binary or
5662 conditional expressions are not allowed. We check this here to avoid
5663 tentative parsing. */
5664 if (EXPR_P (expr1) && warning_suppressed_p (expr1, OPT_Wparentheses))
5665 /* OK, the expression was parenthesized. */;
5666 else if (is_binary_op (TREE_CODE (expr1)))
5667 error_at (location_of (expr1),
5668 "binary expression in operand of fold-expression");
5669 else if (TREE_CODE (expr1) == COND_EXPR
5670 || (REFERENCE_REF_P (expr1)
5671 && TREE_CODE (TREE_OPERAND (expr1, 0)) == COND_EXPR))
5672 error_at (location_of (expr1),
5673 "conditional expression in operand of fold-expression");
5675 // Right fold.
5676 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5678 loc = make_location (token->location, loc, parser->lexer);
5679 return finish_right_unary_fold_expr (loc, expr1, op);
5682 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
5684 cp_parser_error (parser, "mismatched operator in fold-expression");
5685 return error_mark_node;
5687 cp_lexer_consume_token (parser->lexer);
5689 // Binary left or right fold.
5690 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
5691 if (expr2 == error_mark_node)
5692 return error_mark_node;
5693 loc = make_location (token->location, loc, parser->lexer);
5694 return finish_binary_fold_expr (loc, expr1, expr2, op);
5697 /* Parse a primary-expression.
5699 primary-expression:
5700 literal
5701 this
5702 ( expression )
5703 id-expression
5704 lambda-expression (C++11)
5706 GNU Extensions:
5708 primary-expression:
5709 ( compound-statement )
5710 __builtin_va_arg ( assignment-expression , type-id )
5711 __builtin_offsetof ( type-id , offsetof-expression )
5713 C++ Extensions:
5714 __has_nothrow_assign ( type-id )
5715 __has_nothrow_constructor ( type-id )
5716 __has_nothrow_copy ( type-id )
5717 __has_trivial_assign ( type-id )
5718 __has_trivial_constructor ( type-id )
5719 __has_trivial_copy ( type-id )
5720 __has_trivial_destructor ( type-id )
5721 __has_virtual_destructor ( type-id )
5722 __is_abstract ( type-id )
5723 __is_base_of ( type-id , type-id )
5724 __is_class ( type-id )
5725 __is_empty ( type-id )
5726 __is_enum ( type-id )
5727 __is_final ( type-id )
5728 __is_literal_type ( type-id )
5729 __is_pod ( type-id )
5730 __is_polymorphic ( type-id )
5731 __is_std_layout ( type-id )
5732 __is_trivial ( type-id )
5733 __is_union ( type-id )
5735 Objective-C++ Extension:
5737 primary-expression:
5738 objc-expression
5740 literal:
5741 __null
5743 ADDRESS_P is true iff this expression was immediately preceded by
5744 "&" and therefore might denote a pointer-to-member. CAST_P is true
5745 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5746 true iff this expression is a template argument.
5748 Returns a representation of the expression. Upon return, *IDK
5749 indicates what kind of id-expression (if any) was present. */
5751 static cp_expr
5752 cp_parser_primary_expression (cp_parser *parser,
5753 bool address_p,
5754 bool cast_p,
5755 bool template_arg_p,
5756 bool decltype_p,
5757 cp_id_kind *idk)
5759 cp_token *token = NULL;
5761 /* Assume the primary expression is not an id-expression. */
5762 *idk = CP_ID_KIND_NONE;
5764 /* Peek at the next token. */
5765 token = cp_lexer_peek_token (parser->lexer);
5766 switch ((int) token->type)
5768 /* literal:
5769 integer-literal
5770 character-literal
5771 floating-literal
5772 string-literal
5773 boolean-literal
5774 pointer-literal
5775 user-defined-literal */
5776 case CPP_CHAR:
5777 case CPP_CHAR16:
5778 case CPP_CHAR32:
5779 case CPP_WCHAR:
5780 case CPP_UTF8CHAR:
5781 case CPP_NUMBER:
5782 case CPP_PREPARSED_EXPR:
5783 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5784 return cp_parser_userdef_numeric_literal (parser);
5785 token = cp_lexer_consume_token (parser->lexer);
5786 if (TREE_CODE (token->u.value) == FIXED_CST)
5788 error_at (token->location,
5789 "fixed-point types not supported in C++");
5790 return error_mark_node;
5792 /* Floating-point literals are only allowed in an integral
5793 constant expression if they are cast to an integral or
5794 enumeration type. */
5795 if ((TREE_CODE (token->u.value) == REAL_CST
5796 || (TREE_CODE (token->u.value) == EXCESS_PRECISION_EXPR
5797 && TREE_CODE (TREE_OPERAND (token->u.value, 0)) == REAL_CST))
5798 && parser->integral_constant_expression_p
5799 && pedantic)
5801 /* CAST_P will be set even in invalid code like "int(2.7 +
5802 ...)". Therefore, we have to check that the next token
5803 is sure to end the cast. */
5804 if (cast_p)
5806 cp_token *next_token;
5808 next_token = cp_lexer_peek_token (parser->lexer);
5809 if (/* The comma at the end of an
5810 enumerator-definition. */
5811 next_token->type != CPP_COMMA
5812 /* The curly brace at the end of an enum-specifier. */
5813 && next_token->type != CPP_CLOSE_BRACE
5814 /* The end of a statement. */
5815 && next_token->type != CPP_SEMICOLON
5816 /* The end of the cast-expression. */
5817 && next_token->type != CPP_CLOSE_PAREN
5818 /* The end of an array bound. */
5819 && next_token->type != CPP_CLOSE_SQUARE
5820 /* The closing ">" in a template-argument-list. */
5821 && (next_token->type != CPP_GREATER
5822 || parser->greater_than_is_operator_p)
5823 /* C++0x only: A ">>" treated like two ">" tokens,
5824 in a template-argument-list. */
5825 && (next_token->type != CPP_RSHIFT
5826 || (cxx_dialect == cxx98)
5827 || parser->greater_than_is_operator_p))
5828 cast_p = false;
5831 /* If we are within a cast, then the constraint that the
5832 cast is to an integral or enumeration type will be
5833 checked at that point. If we are not within a cast, then
5834 this code is invalid. */
5835 if (!cast_p)
5836 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5838 return (cp_expr (token->u.value, token->location, token->flags & DECIMAL_INT)
5839 .maybe_add_location_wrapper ());
5841 case CPP_CHAR_USERDEF:
5842 case CPP_CHAR16_USERDEF:
5843 case CPP_CHAR32_USERDEF:
5844 case CPP_WCHAR_USERDEF:
5845 case CPP_UTF8CHAR_USERDEF:
5846 return cp_parser_userdef_char_literal (parser);
5848 case CPP_STRING:
5849 case CPP_STRING16:
5850 case CPP_STRING32:
5851 case CPP_WSTRING:
5852 case CPP_UTF8STRING:
5853 case CPP_STRING_USERDEF:
5854 case CPP_STRING16_USERDEF:
5855 case CPP_STRING32_USERDEF:
5856 case CPP_WSTRING_USERDEF:
5857 case CPP_UTF8STRING_USERDEF:
5858 /* ??? Should wide strings be allowed when parser->translate_strings_p
5859 is false (i.e. in attributes)? If not, we can kill the third
5860 argument to cp_parser_string_literal. */
5861 if (parser->translate_strings_p)
5862 return (cp_parser_userdef_string_literal (parser,
5863 /*lookup_udlit=*/true)
5864 .maybe_add_location_wrapper ());
5865 else
5866 return (cp_parser_string_literal (parser,
5867 /*translate=*/false,
5868 /*wide_ok=*/true)
5869 .maybe_add_location_wrapper ());
5871 case CPP_OPEN_PAREN:
5872 /* If we see `( { ' then we are looking at the beginning of
5873 a GNU statement-expression. */
5874 if (cp_parser_allow_gnu_extensions_p (parser)
5875 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5877 /* Statement-expressions are not allowed by the standard. */
5878 pedwarn (token->location, OPT_Wpedantic,
5879 "ISO C++ forbids braced-groups within expressions");
5881 /* And they're not allowed outside of a function-body; you
5882 cannot, for example, write:
5884 int i = ({ int j = 3; j + 1; });
5886 at class or namespace scope. */
5887 if (!parser->in_function_body
5888 || parser->in_template_argument_list_p)
5890 error_at (token->location,
5891 "statement-expressions are not allowed outside "
5892 "functions nor in template-argument lists");
5893 cp_parser_skip_to_end_of_block_or_statement (parser);
5894 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5895 cp_lexer_consume_token (parser->lexer);
5896 return error_mark_node;
5898 else
5899 return cp_parser_statement_expr (parser);
5901 /* Otherwise it's a normal parenthesized expression. */
5903 cp_expr expr;
5904 bool saved_greater_than_is_operator_p;
5906 location_t open_paren_loc = token->location;
5908 /* Consume the `('. */
5909 matching_parens parens;
5910 parens.consume_open (parser);
5911 /* Within a parenthesized expression, a `>' token is always
5912 the greater-than operator. */
5913 saved_greater_than_is_operator_p
5914 = parser->greater_than_is_operator_p;
5915 parser->greater_than_is_operator_p = true;
5917 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5918 /* Left fold expression. */
5919 expr = NULL_TREE;
5920 else
5921 /* Parse the parenthesized expression. */
5922 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5924 token = cp_lexer_peek_token (parser->lexer);
5925 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5927 expr = cp_parser_fold_expression (parser, expr);
5928 if (expr != error_mark_node
5929 && cxx_dialect < cxx17)
5930 pedwarn (input_location, OPT_Wc__17_extensions,
5931 "fold-expressions only available with %<-std=c++17%> "
5932 "or %<-std=gnu++17%>");
5934 else
5935 /* Let the front end know that this expression was
5936 enclosed in parentheses. This matters in case, for
5937 example, the expression is of the form `A::B', since
5938 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5939 not. */
5940 expr = finish_parenthesized_expr (expr);
5942 /* DR 705: Wrapping an unqualified name in parentheses
5943 suppresses arg-dependent lookup. We want to pass back
5944 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5945 (c++/37862), but none of the others. */
5946 if (*idk != CP_ID_KIND_QUALIFIED)
5947 *idk = CP_ID_KIND_NONE;
5949 /* The `>' token might be the end of a template-id or
5950 template-parameter-list now. */
5951 parser->greater_than_is_operator_p
5952 = saved_greater_than_is_operator_p;
5954 /* Consume the `)'. */
5955 token = cp_lexer_peek_token (parser->lexer);
5956 location_t close_paren_loc = token->location;
5957 bool no_wparens = warning_suppressed_p (expr, OPT_Wparentheses);
5958 expr.set_range (open_paren_loc, close_paren_loc);
5959 if (no_wparens)
5960 suppress_warning (expr, OPT_Wparentheses);
5961 if (!parens.require_close (parser)
5962 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5963 cp_parser_skip_to_end_of_statement (parser);
5965 return expr;
5968 case CPP_OPEN_SQUARE:
5970 if (c_dialect_objc ())
5972 /* We might have an Objective-C++ message. */
5973 cp_parser_parse_tentatively (parser);
5974 tree msg = cp_parser_objc_message_expression (parser);
5975 /* If that works out, we're done ... */
5976 if (cp_parser_parse_definitely (parser))
5977 return msg;
5978 /* ... else, fall though to see if it's a lambda. */
5980 cp_expr lam = cp_parser_lambda_expression (parser);
5981 /* Don't warn about a failed tentative parse. */
5982 if (cp_parser_error_occurred (parser))
5983 return error_mark_node;
5984 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5985 return lam;
5988 case CPP_OBJC_STRING:
5989 if (c_dialect_objc ())
5990 /* We have an Objective-C++ string literal. */
5991 return cp_parser_objc_expression (parser);
5992 cp_parser_error (parser, "expected primary-expression");
5993 return error_mark_node;
5995 case CPP_KEYWORD:
5996 switch (token->keyword)
5998 /* These two are the boolean literals. */
5999 case RID_TRUE:
6000 cp_lexer_consume_token (parser->lexer);
6001 return cp_expr (boolean_true_node, token->location);
6002 case RID_FALSE:
6003 cp_lexer_consume_token (parser->lexer);
6004 return cp_expr (boolean_false_node, token->location);
6006 /* The `__null' literal. */
6007 case RID_NULL:
6008 cp_lexer_consume_token (parser->lexer);
6009 return cp_expr (null_node, token->location);
6011 /* The `nullptr' literal. */
6012 case RID_NULLPTR:
6013 cp_lexer_consume_token (parser->lexer);
6014 return cp_expr (nullptr_node, token->location);
6016 /* Recognize the `this' keyword. */
6017 case RID_THIS:
6018 cp_lexer_consume_token (parser->lexer);
6019 if (parser->local_variables_forbidden_p & THIS_FORBIDDEN)
6021 error_at (token->location,
6022 "%<this%> may not be used in this context");
6023 return error_mark_node;
6025 /* Pointers cannot appear in constant-expressions. */
6026 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
6027 return error_mark_node;
6028 return cp_expr (finish_this_expr (), token->location);
6030 /* The `operator' keyword can be the beginning of an
6031 id-expression. */
6032 case RID_OPERATOR:
6033 goto id_expression;
6035 case RID_FUNCTION_NAME:
6036 case RID_PRETTY_FUNCTION_NAME:
6037 case RID_C99_FUNCTION_NAME:
6039 non_integral_constant name;
6041 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
6042 __func__ are the names of variables -- but they are
6043 treated specially. Therefore, they are handled here,
6044 rather than relying on the generic id-expression logic
6045 below. Grammatically, these names are id-expressions.
6047 Consume the token. */
6048 token = cp_lexer_consume_token (parser->lexer);
6050 switch (token->keyword)
6052 case RID_FUNCTION_NAME:
6053 name = NIC_FUNC_NAME;
6054 break;
6055 case RID_PRETTY_FUNCTION_NAME:
6056 name = NIC_PRETTY_FUNC;
6057 break;
6058 case RID_C99_FUNCTION_NAME:
6059 name = NIC_C99_FUNC;
6060 break;
6061 default:
6062 gcc_unreachable ();
6065 if (cp_parser_non_integral_constant_expression (parser, name))
6066 return error_mark_node;
6068 /* Look up the name. */
6069 return finish_fname (token->u.value);
6072 case RID_VA_ARG:
6074 tree expression;
6075 tree type;
6076 location_t type_location;
6077 location_t start_loc
6078 = cp_lexer_peek_token (parser->lexer)->location;
6079 /* The `__builtin_va_arg' construct is used to handle
6080 `va_arg'. Consume the `__builtin_va_arg' token. */
6081 cp_lexer_consume_token (parser->lexer);
6082 /* Look for the opening `('. */
6083 matching_parens parens;
6084 parens.require_open (parser);
6085 /* Now, parse the assignment-expression. */
6086 expression = cp_parser_assignment_expression (parser);
6087 /* Look for the `,'. */
6088 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
6089 type_location = cp_lexer_peek_token (parser->lexer)->location;
6090 /* Parse the type-id. */
6092 type_id_in_expr_sentinel s (parser);
6093 type = cp_parser_type_id (parser);
6095 /* Look for the closing `)'. */
6096 location_t finish_loc
6097 = cp_lexer_peek_token (parser->lexer)->location;
6098 parens.require_close (parser);
6099 /* Using `va_arg' in a constant-expression is not
6100 allowed. */
6101 if (cp_parser_non_integral_constant_expression (parser,
6102 NIC_VA_ARG))
6103 return error_mark_node;
6104 /* Construct a location of the form:
6105 __builtin_va_arg (v, int)
6106 ~~~~~~~~~~~~~~~~~~~~~^~~~
6107 with the caret at the type, ranging from the start of the
6108 "__builtin_va_arg" token to the close paren. */
6109 location_t combined_loc
6110 = make_location (type_location, start_loc, finish_loc);
6111 return build_x_va_arg (combined_loc, expression, type);
6114 case RID_OFFSETOF:
6115 return cp_parser_builtin_offsetof (parser);
6117 // C++ concepts
6118 case RID_REQUIRES:
6119 return cp_parser_requires_expression (parser);
6121 /* Objective-C++ expressions. */
6122 case RID_AT_ENCODE:
6123 case RID_AT_PROTOCOL:
6124 case RID_AT_SELECTOR:
6125 return cp_parser_objc_expression (parser);
6127 case RID_OMP_ALL_MEMORY:
6128 gcc_assert (flag_openmp);
6129 cp_lexer_consume_token (parser->lexer);
6130 error_at (token->location,
6131 "%<omp_all_memory%> may only be used in OpenMP "
6132 "%<depend%> clause");
6133 return error_mark_node;
6135 case RID_TEMPLATE:
6136 if (parser->in_function_body
6137 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6138 == CPP_LESS))
6140 error_at (token->location,
6141 "a template declaration cannot appear at block scope");
6142 cp_parser_skip_to_end_of_block_or_statement (parser);
6143 return error_mark_node;
6145 /* FALLTHRU */
6146 default:
6147 cp_parser_error (parser, "expected primary-expression");
6148 return error_mark_node;
6151 /* An id-expression can start with either an identifier, a
6152 `::' as the beginning of a qualified-id, or the "operator"
6153 keyword. */
6154 case CPP_NAME:
6155 if (const cp_trait* trait = cp_lexer_peek_trait_expr (parser->lexer))
6156 return cp_parser_trait (parser, trait);
6157 /* FALLTHRU */
6158 case CPP_SCOPE:
6159 case CPP_TEMPLATE_ID:
6160 case CPP_NESTED_NAME_SPECIFIER:
6162 id_expression:
6163 cp_expr id_expression;
6164 cp_expr decl;
6165 const char *error_msg;
6166 bool template_p;
6167 bool done;
6168 cp_token *id_expr_token;
6170 /* Parse the id-expression. */
6171 id_expression
6172 = cp_parser_id_expression (parser,
6173 /*template_keyword_p=*/false,
6174 /*check_dependency_p=*/true,
6175 &template_p,
6176 /*declarator_p=*/false,
6177 /*optional_p=*/false);
6178 if (id_expression == error_mark_node)
6179 return error_mark_node;
6180 id_expr_token = token;
6181 token = cp_lexer_peek_token (parser->lexer);
6182 done = (token->type != CPP_OPEN_SQUARE
6183 && token->type != CPP_OPEN_PAREN
6184 && token->type != CPP_DOT
6185 && token->type != CPP_DEREF
6186 && token->type != CPP_PLUS_PLUS
6187 && token->type != CPP_MINUS_MINUS);
6188 /* If we have a template-id, then no further lookup is
6189 required. If the template-id was for a template-class, we
6190 will sometimes have a TYPE_DECL at this point. */
6191 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
6192 || TREE_CODE (id_expression) == TYPE_DECL)
6193 decl = id_expression;
6194 /* Look up the name. */
6195 else
6197 tree ambiguous_decls;
6199 /* If we already know that this lookup is ambiguous, then
6200 we've already issued an error message; there's no reason
6201 to check again. */
6202 if (id_expr_token->type == CPP_NAME
6203 && id_expr_token->error_reported)
6205 cp_parser_simulate_error (parser);
6206 return error_mark_node;
6209 decl = cp_parser_lookup_name (parser, id_expression,
6210 none_type,
6211 template_p,
6212 /*is_namespace=*/false,
6213 /*check_dependency=*/true,
6214 &ambiguous_decls,
6215 id_expression.get_location ());
6216 /* If the lookup was ambiguous, an error will already have
6217 been issued. */
6218 if (ambiguous_decls)
6219 return error_mark_node;
6221 /* In Objective-C++, we may have an Objective-C 2.0
6222 dot-syntax for classes here. */
6223 if (c_dialect_objc ()
6224 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
6225 && TREE_CODE (decl) == TYPE_DECL
6226 && objc_is_class_name (decl))
6228 tree component;
6229 cp_lexer_consume_token (parser->lexer);
6230 component = cp_parser_identifier (parser);
6231 if (component == error_mark_node)
6232 return error_mark_node;
6234 tree result = objc_build_class_component_ref (id_expression,
6235 component);
6236 /* Build a location of the form:
6237 expr.component
6238 ~~~~~^~~~~~~~~
6239 with caret at the start of the component name (at
6240 input_location), ranging from the start of the id_expression
6241 to the end of the component name. */
6242 location_t combined_loc
6243 = make_location (input_location, id_expression.get_start (),
6244 get_finish (input_location));
6245 protected_set_expr_location (result, combined_loc);
6246 return result;
6249 /* In Objective-C++, an instance variable (ivar) may be preferred
6250 to whatever cp_parser_lookup_name() found.
6251 Call objc_lookup_ivar. To avoid exposing cp_expr to the
6252 rest of c-family, we have to do a little extra work to preserve
6253 any location information in cp_expr "decl". Given that
6254 objc_lookup_ivar is implemented in "c-family" and "objc", we
6255 have a trip through the pure "tree" type, rather than cp_expr.
6256 Naively copying it back to "decl" would implicitly give the
6257 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
6258 store an EXPR_LOCATION. Hence we only update "decl" (and
6259 hence its location_t) if we get back a different tree node. */
6260 tree decl_tree = objc_lookup_ivar (decl.get_value (),
6261 id_expression);
6262 if (decl_tree != decl.get_value ())
6263 decl = cp_expr (decl_tree);
6265 /* If name lookup gives us a SCOPE_REF, then the
6266 qualifying scope was dependent. */
6267 if (TREE_CODE (decl) == SCOPE_REF)
6269 /* At this point, we do not know if DECL is a valid
6270 integral constant expression. We assume that it is
6271 in fact such an expression, so that code like:
6273 template <int N> struct A {
6274 int a[B<N>::i];
6277 is accepted. At template-instantiation time, we
6278 will check that B<N>::i is actually a constant. */
6279 return decl;
6281 /* Check to see if DECL is a local variable in a context
6282 where that is forbidden. */
6283 if ((parser->local_variables_forbidden_p & LOCAL_VARS_FORBIDDEN)
6284 && local_variable_p (decl)
6285 /* DR 2082 permits local variables in unevaluated contexts
6286 within a default argument. */
6287 && !cp_unevaluated_operand)
6289 const char *msg
6290 = (TREE_CODE (decl) == PARM_DECL
6291 ? G_("parameter %qD may not appear in this context")
6292 : G_("local variable %qD may not appear in this context"));
6293 error_at (id_expression.get_location (), msg,
6294 decl.get_value ());
6295 return error_mark_node;
6299 decl = (finish_id_expression
6300 (id_expression, decl, parser->scope,
6301 idk,
6302 parser->integral_constant_expression_p,
6303 parser->allow_non_integral_constant_expression_p,
6304 &parser->non_integral_constant_expression_p,
6305 template_p, done, address_p,
6306 template_arg_p,
6307 &error_msg,
6308 id_expression.get_location ()));
6309 if (error_msg)
6310 cp_parser_error (parser, error_msg);
6311 /* Build a location for an id-expression of the form:
6312 ::ns::id
6313 ~~~~~~^~
6317 i.e. from the start of the first token to the end of the final
6318 token, with the caret at the start of the unqualified-id. */
6319 location_t caret_loc = get_pure_location (id_expression.get_location ());
6320 location_t start_loc = get_start (id_expr_token->location);
6321 location_t finish_loc = get_finish (id_expression.get_location ());
6322 location_t combined_loc
6323 = make_location (caret_loc, start_loc, finish_loc);
6325 decl.set_location (combined_loc);
6326 return decl;
6329 /* Anything else is an error. */
6330 default:
6331 cp_parser_error (parser, "expected primary-expression");
6332 return error_mark_node;
6336 static inline cp_expr
6337 cp_parser_primary_expression (cp_parser *parser,
6338 bool address_p,
6339 bool cast_p,
6340 bool template_arg_p,
6341 cp_id_kind *idk)
6343 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
6344 /*decltype*/false, idk);
6347 /* Complain about missing template keyword when naming a dependent
6348 member template. */
6350 static void
6351 missing_template_diag (location_t loc, diagnostic_t diag_kind = DK_WARNING)
6353 if (warning_suppressed_at (loc, OPT_Wmissing_template_keyword))
6354 return;
6356 gcc_rich_location richloc (loc);
6357 richloc.add_fixit_insert_before ("template");
6358 emit_diagnostic (diag_kind, &richloc, OPT_Wmissing_template_keyword,
6359 "expected %qs keyword before dependent "
6360 "template name", "template");
6361 suppress_warning_at (loc, OPT_Wmissing_template_keyword);
6364 /* Parse an id-expression.
6366 id-expression:
6367 unqualified-id
6368 qualified-id
6370 qualified-id:
6371 :: [opt] nested-name-specifier template [opt] unqualified-id
6372 :: identifier
6373 :: operator-function-id
6374 :: template-id
6376 Return a representation of the unqualified portion of the
6377 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
6378 a `::' or nested-name-specifier.
6380 Often, if the id-expression was a qualified-id, the caller will
6381 want to make a SCOPE_REF to represent the qualified-id. This
6382 function does not do this in order to avoid wastefully creating
6383 SCOPE_REFs when they are not required.
6385 If TEMPLATE_KEYWORD_P is true, then we have just seen the
6386 `template' keyword.
6388 If CHECK_DEPENDENCY_P is false, then names are looked up inside
6389 uninstantiated templates.
6391 If *TEMPLATE_P is non-NULL, it is set to true iff the
6392 `template' keyword is used to explicitly indicate that the entity
6393 named is a template.
6395 If DECLARATOR_P is true, the id-expression is appearing as part of
6396 a declarator, rather than as part of an expression. */
6398 static cp_expr
6399 cp_parser_id_expression (cp_parser *parser,
6400 bool template_keyword_p,
6401 bool check_dependency_p,
6402 bool *template_p,
6403 bool declarator_p,
6404 bool optional_p)
6406 bool global_scope_p;
6407 bool nested_name_specifier_p;
6409 /* Assume the `template' keyword was not used. */
6410 if (template_p)
6411 *template_p = template_keyword_p;
6413 /* Look for the optional `::' operator. */
6414 global_scope_p
6415 = (!template_keyword_p
6416 && (cp_parser_global_scope_opt (parser,
6417 /*current_scope_valid_p=*/false)
6418 != NULL_TREE));
6420 /* Look for the optional nested-name-specifier. */
6421 nested_name_specifier_p
6422 = (cp_parser_nested_name_specifier_opt (parser,
6423 /*typename_keyword_p=*/false,
6424 check_dependency_p,
6425 /*type_p=*/false,
6426 declarator_p,
6427 template_keyword_p)
6428 != NULL_TREE);
6430 cp_expr id = NULL_TREE;
6431 tree scope = parser->scope;
6433 /* Peek at the next token. */
6434 cp_token *token = cp_lexer_peek_token (parser->lexer);
6436 /* If there is a nested-name-specifier, then we are looking at
6437 the first qualified-id production. */
6438 if (nested_name_specifier_p)
6440 tree saved_object_scope;
6441 tree saved_qualifying_scope;
6443 /* See if the next token is the `template' keyword. */
6444 if (!template_p)
6445 template_p = &template_keyword_p;
6446 *template_p = cp_parser_optional_template_keyword (parser);
6447 /* Name lookup we do during the processing of the
6448 unqualified-id might obliterate SCOPE. */
6449 saved_object_scope = parser->object_scope;
6450 saved_qualifying_scope = parser->qualifying_scope;
6451 /* Process the final unqualified-id. */
6452 id = cp_parser_unqualified_id (parser, *template_p,
6453 check_dependency_p,
6454 declarator_p,
6455 /*optional_p=*/false);
6456 /* Restore the SAVED_SCOPE for our caller. */
6457 parser->scope = scope;
6458 parser->object_scope = saved_object_scope;
6459 parser->qualifying_scope = saved_qualifying_scope;
6461 /* Otherwise, if we are in global scope, then we are looking at one
6462 of the other qualified-id productions. */
6463 else if (global_scope_p)
6465 /* If it's an identifier, and the next token is not a "<", then
6466 we can avoid the template-id case. This is an optimization
6467 for this common case. */
6468 if (token->type == CPP_NAME
6469 && !cp_parser_nth_token_starts_template_argument_list_p
6470 (parser, 2))
6471 return cp_parser_identifier (parser);
6473 cp_parser_parse_tentatively (parser);
6474 /* Try a template-id. */
6475 id = cp_parser_template_id_expr (parser,
6476 /*template_keyword_p=*/false,
6477 /*check_dependency_p=*/true,
6478 declarator_p);
6479 /* If that worked, we're done. */
6480 if (cp_parser_parse_definitely (parser))
6481 return id;
6483 /* Peek at the next token. (Changes in the token buffer may
6484 have invalidated the pointer obtained above.) */
6485 token = cp_lexer_peek_token (parser->lexer);
6487 switch (token->type)
6489 case CPP_NAME:
6490 id = cp_parser_identifier (parser);
6491 break;
6493 case CPP_KEYWORD:
6494 if (token->keyword == RID_OPERATOR)
6496 id = cp_parser_operator_function_id (parser);
6497 break;
6499 /* Fall through. */
6501 default:
6502 cp_parser_error (parser, "expected id-expression");
6503 return error_mark_node;
6506 else
6508 if (!scope)
6509 scope = parser->context->object_type;
6510 id = cp_parser_unqualified_id (parser, template_keyword_p,
6511 /*check_dependency_p=*/true,
6512 declarator_p,
6513 optional_p);
6516 if (id && TREE_CODE (id) == IDENTIFIER_NODE
6517 && warn_missing_template_keyword
6518 && !template_keyword_p
6519 /* Don't warn if we're looking inside templates. */
6520 && check_dependency_p
6521 /* In a template argument list a > could be closing
6522 the enclosing targs. */
6523 && !parser->in_template_argument_list_p
6524 && scope && dependentish_scope_p (scope)
6525 /* Don't confuse an ill-formed constructor declarator for a missing
6526 template keyword in a return type. */
6527 && !(declarator_p && constructor_name_p (id, scope))
6528 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1)
6529 && warning_enabled_at (token->location,
6530 OPT_Wmissing_template_keyword))
6532 saved_token_sentinel toks (parser->lexer, STS_ROLLBACK);
6533 if (cp_parser_skip_entire_template_parameter_list (parser)
6534 /* An operator after the > suggests that the > ends a
6535 template-id; a name or literal suggests that the > is an
6536 operator. */
6537 && (cp_lexer_peek_token (parser->lexer)->type
6538 <= CPP_LAST_PUNCTUATOR))
6539 missing_template_diag (token->location);
6542 return id;
6545 /* Parse an unqualified-id.
6547 unqualified-id:
6548 identifier
6549 operator-function-id
6550 conversion-function-id
6551 ~ class-name
6552 template-id
6554 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
6555 keyword, in a construct like `A::template ...'.
6557 Returns a representation of unqualified-id. For the `identifier'
6558 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
6559 production a BIT_NOT_EXPR is returned; the operand of the
6560 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
6561 other productions, see the documentation accompanying the
6562 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
6563 names are looked up in uninstantiated templates. If DECLARATOR_P
6564 is true, the unqualified-id is appearing as part of a declarator,
6565 rather than as part of an expression. */
6567 static cp_expr
6568 cp_parser_unqualified_id (cp_parser* parser,
6569 bool template_keyword_p,
6570 bool check_dependency_p,
6571 bool declarator_p,
6572 bool optional_p)
6574 cp_token *token;
6576 /* Peek at the next token. */
6577 token = cp_lexer_peek_token (parser->lexer);
6579 switch ((int) token->type)
6581 case CPP_NAME:
6583 tree id;
6585 /* We don't know yet whether or not this will be a
6586 template-id. */
6587 cp_parser_parse_tentatively (parser);
6588 /* Try a template-id. */
6589 id = cp_parser_template_id_expr (parser, template_keyword_p,
6590 check_dependency_p,
6591 declarator_p);
6592 /* If it worked, we're done. */
6593 if (cp_parser_parse_definitely (parser))
6594 return id;
6595 /* Otherwise, it's an ordinary identifier. */
6596 return cp_parser_identifier (parser);
6599 case CPP_TEMPLATE_ID:
6600 return cp_parser_template_id_expr (parser, template_keyword_p,
6601 check_dependency_p,
6602 declarator_p);
6604 case CPP_COMPL:
6606 tree type_decl;
6607 tree qualifying_scope;
6608 tree object_scope;
6609 tree scope;
6610 bool done;
6611 location_t tilde_loc = token->location;
6613 /* Consume the `~' token. */
6614 cp_lexer_consume_token (parser->lexer);
6615 /* Parse the class-name. The standard, as written, seems to
6616 say that:
6618 template <typename T> struct S { ~S (); };
6619 template <typename T> S<T>::~S() {}
6621 is invalid, since `~' must be followed by a class-name, but
6622 `S<T>' is dependent, and so not known to be a class.
6623 That's not right; we need to look in uninstantiated
6624 templates. A further complication arises from:
6626 template <typename T> void f(T t) {
6627 t.T::~T();
6630 Here, it is not possible to look up `T' in the scope of `T'
6631 itself. We must look in both the current scope, and the
6632 scope of the containing complete expression.
6634 Yet another issue is:
6636 struct S {
6637 int S;
6638 ~S();
6641 S::~S() {}
6643 The standard does not seem to say that the `S' in `~S'
6644 should refer to the type `S' and not the data member
6645 `S::S'. */
6647 /* DR 244 says that we look up the name after the "~" in the
6648 same scope as we looked up the qualifying name. That idea
6649 isn't fully worked out; it's more complicated than that. */
6650 scope = parser->scope;
6651 object_scope = parser->object_scope;
6652 qualifying_scope = parser->qualifying_scope;
6654 /* Check for invalid scopes. */
6655 if (scope == error_mark_node)
6657 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
6658 cp_lexer_consume_token (parser->lexer);
6659 return error_mark_node;
6661 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
6663 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6664 error_at (token->location,
6665 "scope %qT before %<~%> is not a class-name",
6666 scope);
6667 cp_parser_simulate_error (parser);
6668 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
6669 cp_lexer_consume_token (parser->lexer);
6670 return error_mark_node;
6672 if (template_keyword_p)
6674 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6675 error_at (tilde_loc, "%<template%> keyword not permitted in "
6676 "destructor name");
6677 cp_parser_simulate_error (parser);
6678 return error_mark_node;
6681 gcc_assert (!scope || TYPE_P (scope));
6683 token = cp_lexer_peek_token (parser->lexer);
6685 /* Create a location with caret == start at the tilde,
6686 finishing at the end of the peeked token, e.g:
6687 ~token
6688 ^~~~~~. */
6689 location_t loc
6690 = make_location (tilde_loc, tilde_loc, token->location);
6692 /* If the name is of the form "X::~X" it's OK even if X is a
6693 typedef. */
6695 if (scope
6696 && token->type == CPP_NAME
6697 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6698 != CPP_LESS)
6699 && (token->u.value == TYPE_IDENTIFIER (scope)
6700 || (CLASS_TYPE_P (scope)
6701 && constructor_name_p (token->u.value, scope))))
6703 cp_lexer_consume_token (parser->lexer);
6704 return build_min_nt_loc (loc, BIT_NOT_EXPR, scope);
6707 /* ~auto means the destructor of whatever the object is. */
6708 if (cp_parser_is_keyword (token, RID_AUTO))
6710 if (cxx_dialect < cxx14)
6711 pedwarn (loc, OPT_Wc__14_extensions,
6712 "%<~auto%> only available with "
6713 "%<-std=c++14%> or %<-std=gnu++14%>");
6714 cp_lexer_consume_token (parser->lexer);
6715 return build_min_nt_loc (loc, BIT_NOT_EXPR, make_auto ());
6718 /* DR 2237 (C++20 only): A simple-template-id is no longer valid as the
6719 declarator-id of a constructor or destructor. */
6720 if (token->type == CPP_TEMPLATE_ID && declarator_p
6721 && cxx_dialect >= cxx20)
6723 if (!cp_parser_simulate_error (parser))
6724 error_at (tilde_loc, "template-id not allowed for destructor");
6725 return error_mark_node;
6728 /* If there was an explicit qualification (S::~T), first look
6729 in the scope given by the qualification (i.e., S).
6731 Note: in the calls to cp_parser_class_name below we pass
6732 typename_type so that lookup finds the injected-class-name
6733 rather than the constructor. */
6734 done = false;
6735 type_decl = NULL_TREE;
6736 if (scope)
6738 cp_parser_parse_tentatively (parser);
6739 type_decl = cp_parser_class_name (parser,
6740 /*typename_keyword_p=*/false,
6741 /*template_keyword_p=*/false,
6742 typename_type,
6743 /*check_dependency=*/false,
6744 /*class_head_p=*/false,
6745 declarator_p);
6746 if (cp_parser_parse_definitely (parser))
6747 done = true;
6749 /* In "N::S::~S", look in "N" as well. */
6750 if (!done && scope && qualifying_scope)
6752 cp_parser_parse_tentatively (parser);
6753 parser->scope = qualifying_scope;
6754 parser->object_scope = NULL_TREE;
6755 parser->qualifying_scope = NULL_TREE;
6756 type_decl
6757 = cp_parser_class_name (parser,
6758 /*typename_keyword_p=*/false,
6759 /*template_keyword_p=*/false,
6760 typename_type,
6761 /*check_dependency=*/false,
6762 /*class_head_p=*/false,
6763 declarator_p);
6764 if (cp_parser_parse_definitely (parser))
6765 done = true;
6767 /* In "p->S::~T", look in the scope given by "*p" as well. */
6768 else if (!done && object_scope)
6770 cp_parser_parse_tentatively (parser);
6771 parser->scope = object_scope;
6772 parser->object_scope = NULL_TREE;
6773 parser->qualifying_scope = NULL_TREE;
6774 type_decl
6775 = cp_parser_class_name (parser,
6776 /*typename_keyword_p=*/false,
6777 /*template_keyword_p=*/false,
6778 typename_type,
6779 /*check_dependency=*/false,
6780 /*class_head_p=*/false,
6781 declarator_p);
6782 if (cp_parser_parse_definitely (parser))
6783 done = true;
6785 /* Look in the surrounding context. */
6786 if (!done)
6788 parser->scope = NULL_TREE;
6789 parser->object_scope = NULL_TREE;
6790 parser->qualifying_scope = NULL_TREE;
6791 if (processing_template_decl)
6792 cp_parser_parse_tentatively (parser);
6793 type_decl
6794 = cp_parser_class_name (parser,
6795 /*typename_keyword_p=*/false,
6796 /*template_keyword_p=*/false,
6797 typename_type,
6798 /*check_dependency=*/false,
6799 /*class_head_p=*/false,
6800 declarator_p);
6801 if (processing_template_decl
6802 && ! cp_parser_parse_definitely (parser))
6804 /* We couldn't find a type with this name. If we're parsing
6805 tentatively, fail and try something else. */
6806 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6808 cp_parser_simulate_error (parser);
6809 return error_mark_node;
6811 /* Otherwise, accept it and check for a match at instantiation
6812 time. */
6813 type_decl = cp_parser_identifier (parser);
6814 if (type_decl != error_mark_node)
6815 type_decl = build_min_nt_loc (loc, BIT_NOT_EXPR, type_decl);
6816 return type_decl;
6819 /* If an error occurred, assume that the name of the
6820 destructor is the same as the name of the qualifying
6821 class. That allows us to keep parsing after running
6822 into ill-formed destructor names. */
6823 if (type_decl == error_mark_node && scope)
6824 return build_min_nt_loc (loc, BIT_NOT_EXPR, scope);
6825 else if (type_decl == error_mark_node)
6826 return error_mark_node;
6828 /* Check that destructor name and scope match. */
6829 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6831 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6832 error_at (loc,
6833 "declaration of %<~%T%> as member of %qT",
6834 type_decl, scope);
6835 cp_parser_simulate_error (parser);
6836 return error_mark_node;
6839 /* [class.dtor]
6841 A typedef-name that names a class shall not be used as the
6842 identifier in the declarator for a destructor declaration. */
6843 if (declarator_p
6844 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6845 && !DECL_SELF_REFERENCE_P (type_decl)
6846 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6847 error_at (loc,
6848 "typedef-name %qD used as destructor declarator",
6849 type_decl);
6851 return build_min_nt_loc (loc, BIT_NOT_EXPR, TREE_TYPE (type_decl));
6854 case CPP_KEYWORD:
6855 if (token->keyword == RID_OPERATOR)
6857 cp_expr id;
6859 /* This could be a template-id, so we try that first. */
6860 cp_parser_parse_tentatively (parser);
6861 /* Try a template-id. */
6862 id = cp_parser_template_id_expr (parser, template_keyword_p,
6863 /*check_dependency_p=*/true,
6864 declarator_p);
6865 /* If that worked, we're done. */
6866 if (cp_parser_parse_definitely (parser))
6867 return id;
6868 /* We still don't know whether we're looking at an
6869 operator-function-id or a conversion-function-id. */
6870 cp_parser_parse_tentatively (parser);
6871 /* Try an operator-function-id. */
6872 id = cp_parser_operator_function_id (parser);
6873 /* If that didn't work, try a conversion-function-id. */
6874 if (!cp_parser_parse_definitely (parser))
6875 id = cp_parser_conversion_function_id (parser);
6877 return id;
6879 /* Fall through. */
6881 default:
6882 if (optional_p)
6883 return NULL_TREE;
6884 cp_parser_error (parser, "expected unqualified-id");
6885 return error_mark_node;
6889 /* Check [temp.names]/5: A name prefixed by the keyword template shall
6890 be a template-id or the name shall refer to a class template or an
6891 alias template. */
6893 static void
6894 check_template_keyword_in_nested_name_spec (tree name)
6896 if (CLASS_TYPE_P (name)
6897 && ((CLASSTYPE_USE_TEMPLATE (name)
6898 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (name)))
6899 || CLASSTYPE_IS_TEMPLATE (name)))
6900 return;
6902 if (TREE_CODE (name) == TYPENAME_TYPE
6903 && TREE_CODE (TYPENAME_TYPE_FULLNAME (name)) == TEMPLATE_ID_EXPR)
6904 return;
6905 /* Alias templates are also OK. */
6906 else if (alias_template_specialization_p (name, nt_opaque))
6907 return;
6909 permerror (input_location, TYPE_P (name)
6910 ? G_("%qT is not a template")
6911 : G_("%qD is not a template"),
6912 name);
6915 /* Parse an (optional) nested-name-specifier.
6917 nested-name-specifier: [C++98]
6918 class-or-namespace-name :: nested-name-specifier [opt]
6919 class-or-namespace-name :: template nested-name-specifier [opt]
6921 nested-name-specifier: [C++0x]
6922 type-name ::
6923 namespace-name ::
6924 nested-name-specifier identifier ::
6925 nested-name-specifier template [opt] simple-template-id ::
6927 PARSER->SCOPE should be set appropriately before this function is
6928 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6929 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6930 in name lookups.
6932 Sets PARSER->SCOPE to the class (TYPE) or namespace
6933 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6934 it unchanged if there is no nested-name-specifier. Returns the new
6935 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6937 If CHECK_DEPENDENCY_P is FALSE, names are looked up in dependent scopes.
6939 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6940 part of a declaration and/or decl-specifier. */
6942 static tree
6943 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6944 bool typename_keyword_p,
6945 bool check_dependency_p,
6946 bool type_p,
6947 bool is_declaration,
6948 bool template_keyword_p /* = false */)
6950 bool success = false;
6951 cp_token_position start = 0;
6952 cp_token *token;
6954 /* Remember where the nested-name-specifier starts. */
6955 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
6956 && cp_lexer_next_token_is_not (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
6958 start = cp_lexer_token_position (parser->lexer, false);
6959 push_deferring_access_checks (dk_deferred);
6962 while (true)
6964 tree new_scope;
6965 tree old_scope;
6966 tree saved_qualifying_scope;
6968 /* Spot cases that cannot be the beginning of a
6969 nested-name-specifier. */
6970 token = cp_lexer_peek_token (parser->lexer);
6972 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6973 the already parsed nested-name-specifier. */
6974 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6976 /* Grab the nested-name-specifier and continue the loop. */
6977 cp_parser_pre_parsed_nested_name_specifier (parser);
6978 /* If we originally encountered this nested-name-specifier
6979 with CHECK_DEPENDENCY_P set to true, we will not have
6980 resolved TYPENAME_TYPEs, so we must do so here. */
6981 if (is_declaration
6982 && !check_dependency_p
6983 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6985 new_scope = resolve_typename_type (parser->scope,
6986 /*only_current_p=*/false);
6987 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6988 parser->scope = new_scope;
6990 success = true;
6991 continue;
6994 /* Spot cases that cannot be the beginning of a
6995 nested-name-specifier. On the second and subsequent times
6996 through the loop, we look for the `template' keyword. */
6997 if (success && token->keyword == RID_TEMPLATE)
6999 /* A template-id can start a nested-name-specifier. */
7000 else if (token->type == CPP_TEMPLATE_ID)
7002 /* DR 743: decltype can be used in a nested-name-specifier. */
7003 else if (token_is_decltype (token))
7005 else
7007 /* If the next token is not an identifier, then it is
7008 definitely not a type-name or namespace-name. */
7009 if (token->type != CPP_NAME)
7010 break;
7011 /* If the following token is neither a `<' (to begin a
7012 template-id), nor a `::', then we are not looking at a
7013 nested-name-specifier. */
7014 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7016 if (token->type == CPP_COLON
7017 && parser->colon_corrects_to_scope_p
7018 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME
7019 /* name:name is a valid sequence in an Objective C message. */
7020 && !parser->objective_c_message_context_p)
7022 gcc_rich_location richloc (token->location);
7023 richloc.add_fixit_replace ("::");
7024 error_at (&richloc,
7025 "found %<:%> in nested-name-specifier, "
7026 "expected %<::%>");
7027 token->type = CPP_SCOPE;
7030 if (token->type != CPP_SCOPE
7031 && !cp_parser_nth_token_starts_template_argument_list_p
7032 (parser, 2))
7033 break;
7036 /* The nested-name-specifier is optional, so we parse
7037 tentatively. */
7038 cp_parser_parse_tentatively (parser);
7040 /* Look for the optional `template' keyword, if this isn't the
7041 first time through the loop. */
7042 if (success)
7044 template_keyword_p = cp_parser_optional_template_keyword (parser);
7045 /* DR1710: "In a qualified-id used as the name in
7046 a typename-specifier, elaborated-type-specifier, using-declaration,
7047 or class-or-decltype, an optional keyword template appearing at
7048 the top level is ignored." */
7049 if (!template_keyword_p
7050 && typename_keyword_p
7051 && cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
7052 template_keyword_p = true;
7055 /* Save the old scope since the name lookup we are about to do
7056 might destroy it. */
7057 old_scope = parser->scope;
7058 saved_qualifying_scope = parser->qualifying_scope;
7059 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
7060 look up names in "X<T>::I" in order to determine that "Y" is
7061 a template. So, if we have a typename at this point, we make
7062 an effort to look through it. */
7063 if (is_declaration
7064 && !check_dependency_p
7065 && !typename_keyword_p
7066 && parser->scope
7067 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
7068 parser->scope = resolve_typename_type (parser->scope,
7069 /*only_current_p=*/false);
7070 /* Parse the qualifying entity. */
7071 new_scope
7072 = cp_parser_qualifying_entity (parser,
7073 typename_keyword_p,
7074 template_keyword_p,
7075 check_dependency_p,
7076 type_p,
7077 is_declaration);
7078 /* Look for the `::' token. */
7079 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7081 /* If we found what we wanted, we keep going; otherwise, we're
7082 done. */
7083 if (!cp_parser_parse_definitely (parser))
7085 bool error_p = false;
7087 /* Restore the OLD_SCOPE since it was valid before the
7088 failed attempt at finding the last
7089 class-or-namespace-name. */
7090 parser->scope = old_scope;
7091 parser->qualifying_scope = saved_qualifying_scope;
7093 /* If the next token is a decltype, and the one after that is a
7094 `::', then the decltype has failed to resolve to a class or
7095 enumeration type. Give this error even when parsing
7096 tentatively since it can't possibly be valid--and we're going
7097 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
7098 won't get another chance.*/
7099 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
7100 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
7101 == CPP_SCOPE))
7103 token = cp_lexer_consume_token (parser->lexer);
7104 tree dtype = token->u.tree_check_value->value;
7105 if (dtype != error_mark_node)
7106 error_at (token->location, "%<decltype%> evaluates to %qT, "
7107 "which is not a class or enumeration type",
7108 dtype);
7109 parser->scope = error_mark_node;
7110 error_p = true;
7111 /* As below. */
7112 success = true;
7113 cp_lexer_consume_token (parser->lexer);
7116 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
7117 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
7119 /* If we have a non-type template-id followed by ::, it can't
7120 possibly be valid. */
7121 token = cp_lexer_peek_token (parser->lexer);
7122 tree tid = token->u.tree_check_value->value;
7123 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
7124 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
7126 tree tmpl = NULL_TREE;
7127 if (is_overloaded_fn (tid))
7129 tree fns = get_fns (tid);
7130 if (OVL_SINGLE_P (fns))
7131 tmpl = OVL_FIRST (fns);
7132 if (function_concept_p (fns))
7133 error_at (token->location, "concept-id %qD "
7134 "in nested-name-specifier", tid);
7135 else
7136 error_at (token->location, "function template-id "
7137 "%qD in nested-name-specifier", tid);
7139 else
7141 tmpl = TREE_OPERAND (tid, 0);
7142 if (variable_concept_p (tmpl)
7143 || standard_concept_p (tmpl))
7144 error_at (token->location, "concept-id %qD "
7145 "in nested-name-specifier", tid);
7146 else
7148 /* Variable template. */
7149 gcc_assert (variable_template_p (tmpl));
7150 error_at (token->location, "variable template-id "
7151 "%qD in nested-name-specifier", tid);
7154 if (tmpl)
7155 inform (DECL_SOURCE_LOCATION (tmpl),
7156 "%qD declared here", tmpl);
7158 parser->scope = error_mark_node;
7159 error_p = true;
7160 /* As below. */
7161 success = true;
7162 cp_lexer_consume_token (parser->lexer);
7163 cp_lexer_consume_token (parser->lexer);
7167 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
7168 break;
7169 /* If the next token is an identifier, and the one after
7170 that is a `::', then any valid interpretation would have
7171 found a class-or-namespace-name. */
7172 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
7173 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
7174 == CPP_SCOPE)
7175 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7176 != CPP_COMPL))
7178 token = cp_lexer_consume_token (parser->lexer);
7179 if (!error_p)
7181 if (!token->error_reported)
7183 tree decl;
7184 tree ambiguous_decls;
7186 decl = cp_parser_lookup_name (parser, token->u.value,
7187 none_type,
7188 /*is_template=*/false,
7189 /*is_namespace=*/false,
7190 /*check_dependency=*/true,
7191 &ambiguous_decls,
7192 token->location);
7193 if (TREE_CODE (decl) == TEMPLATE_DECL)
7194 error_at (token->location,
7195 "%qD used without template arguments",
7196 decl);
7197 else if (ambiguous_decls)
7199 // cp_parser_lookup_name has the same diagnostic,
7200 // thus make sure to emit it at most once.
7201 if (cp_parser_uncommitted_to_tentative_parse_p
7202 (parser))
7204 error_at (token->location,
7205 "reference to %qD is ambiguous",
7206 token->u.value);
7207 print_candidates (ambiguous_decls);
7209 decl = error_mark_node;
7211 else
7213 if (cxx_dialect != cxx98)
7214 cp_parser_name_lookup_error
7215 (parser, token->u.value, decl, NLE_NOT_CXX98,
7216 token->location);
7217 else
7218 cp_parser_name_lookup_error
7219 (parser, token->u.value, decl, NLE_CXX98,
7220 token->location);
7223 parser->scope = error_mark_node;
7224 error_p = true;
7225 /* Treat this as a successful nested-name-specifier
7226 due to:
7228 [basic.lookup.qual]
7230 If the name found is not a class-name (clause
7231 _class_) or namespace-name (_namespace.def_), the
7232 program is ill-formed. */
7233 success = true;
7235 cp_lexer_consume_token (parser->lexer);
7237 break;
7239 /* We've found one valid nested-name-specifier. */
7240 success = true;
7241 /* Name lookup always gives us a DECL. */
7242 if (TREE_CODE (new_scope) == TYPE_DECL)
7243 new_scope = TREE_TYPE (new_scope);
7244 /* Uses of "template" must be followed by actual templates. */
7245 if (template_keyword_p)
7246 check_template_keyword_in_nested_name_spec (new_scope);
7247 /* If it is a class scope, try to complete it; we are about to
7248 be looking up names inside the class. */
7249 if (TYPE_P (new_scope)
7250 /* Since checking types for dependency can be expensive,
7251 avoid doing it if the type is already complete. */
7252 && !COMPLETE_TYPE_P (new_scope)
7253 /* Do not try to complete dependent types. */
7254 && !dependent_type_p (new_scope))
7256 new_scope = complete_type (new_scope);
7257 /* If it is a typedef to current class, use the current
7258 class instead, as the typedef won't have any names inside
7259 it yet. */
7260 if (!COMPLETE_TYPE_P (new_scope)
7261 && currently_open_class (new_scope))
7262 new_scope = TYPE_MAIN_VARIANT (new_scope);
7264 /* Make sure we look in the right scope the next time through
7265 the loop. */
7266 parser->scope = new_scope;
7269 /* If parsing tentatively, replace the sequence of tokens that makes
7270 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
7271 token. That way, should we re-parse the token stream, we will
7272 not have to repeat the effort required to do the parse, nor will
7273 we issue duplicate error messages. */
7274 if (success && start)
7276 cp_token *token;
7278 token = cp_lexer_token_at (parser->lexer, start);
7279 /* Reset the contents of the START token. */
7280 token->type = CPP_NESTED_NAME_SPECIFIER;
7281 /* Retrieve any deferred checks. Do not pop this access checks yet
7282 so the memory will not be reclaimed during token replacing below. */
7283 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
7284 token->tree_check_p = true;
7285 token->u.tree_check_value->value = parser->scope;
7286 token->u.tree_check_value->checks = get_deferred_access_checks ();
7287 token->u.tree_check_value->qualifying_scope =
7288 parser->qualifying_scope;
7289 token->keyword = RID_MAX;
7291 /* Purge all subsequent tokens. */
7292 cp_lexer_purge_tokens_after (parser->lexer, start);
7295 if (start)
7296 pop_to_parent_deferring_access_checks ();
7298 return success ? parser->scope : NULL_TREE;
7301 /* Parse a nested-name-specifier. See
7302 cp_parser_nested_name_specifier_opt for details. This function
7303 behaves identically, except that it will an issue an error if no
7304 nested-name-specifier is present. */
7306 static tree
7307 cp_parser_nested_name_specifier (cp_parser *parser,
7308 bool typename_keyword_p,
7309 bool check_dependency_p,
7310 bool type_p,
7311 bool is_declaration)
7313 tree scope;
7315 /* Look for the nested-name-specifier. */
7316 scope = cp_parser_nested_name_specifier_opt (parser,
7317 typename_keyword_p,
7318 check_dependency_p,
7319 type_p,
7320 is_declaration);
7321 /* If it was not present, issue an error message. */
7322 if (!scope)
7324 cp_parser_error (parser, "expected nested-name-specifier");
7325 parser->scope = NULL_TREE;
7328 return scope;
7331 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
7332 this is either a class-name or a namespace-name (which corresponds
7333 to the class-or-namespace-name production in the grammar). For
7334 C++0x, it can also be a type-name that refers to an enumeration
7335 type or a simple-template-id.
7337 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
7338 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
7339 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
7340 TYPE_P is TRUE iff the next name should be taken as a class-name,
7341 even the same name is declared to be another entity in the same
7342 scope.
7344 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
7345 specified by the class-or-namespace-name. If neither is found the
7346 ERROR_MARK_NODE is returned. */
7348 static tree
7349 cp_parser_qualifying_entity (cp_parser *parser,
7350 bool typename_keyword_p,
7351 bool template_keyword_p,
7352 bool check_dependency_p,
7353 bool type_p,
7354 bool is_declaration)
7356 tree saved_scope;
7357 tree saved_qualifying_scope;
7358 tree saved_object_scope;
7359 tree scope;
7360 bool only_class_p;
7361 bool successful_parse_p;
7363 /* DR 743: decltype can appear in a nested-name-specifier. */
7364 if (cp_lexer_next_token_is_decltype (parser->lexer))
7366 scope = cp_parser_decltype (parser);
7367 if (TREE_CODE (scope) != ENUMERAL_TYPE
7368 && !MAYBE_CLASS_TYPE_P (scope))
7370 cp_parser_simulate_error (parser);
7371 return error_mark_node;
7373 if (TYPE_NAME (scope))
7374 scope = TYPE_NAME (scope);
7375 return scope;
7378 /* Before we try to parse the class-name, we must save away the
7379 current PARSER->SCOPE since cp_parser_class_name will destroy
7380 it. */
7381 saved_scope = parser->scope;
7382 saved_qualifying_scope = parser->qualifying_scope;
7383 saved_object_scope = parser->object_scope;
7384 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
7385 there is no need to look for a namespace-name. */
7386 only_class_p = template_keyword_p
7387 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
7388 if (!only_class_p)
7389 cp_parser_parse_tentatively (parser);
7390 scope = cp_parser_class_name (parser,
7391 typename_keyword_p,
7392 template_keyword_p,
7393 type_p ? class_type : none_type,
7394 check_dependency_p,
7395 /*class_head_p=*/false,
7396 is_declaration,
7397 /*enum_ok=*/cxx_dialect > cxx98);
7398 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
7399 /* If that didn't work, try for a namespace-name. */
7400 if (!only_class_p && !successful_parse_p)
7402 /* Restore the saved scope. */
7403 parser->scope = saved_scope;
7404 parser->qualifying_scope = saved_qualifying_scope;
7405 parser->object_scope = saved_object_scope;
7406 /* If we are not looking at an identifier followed by the scope
7407 resolution operator, then this is not part of a
7408 nested-name-specifier. (Note that this function is only used
7409 to parse the components of a nested-name-specifier.) */
7410 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
7411 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
7412 return error_mark_node;
7413 scope = cp_parser_namespace_name (parser);
7416 return scope;
7419 /* Return true if we are looking at a compound-literal, false otherwise. */
7421 static bool
7422 cp_parser_compound_literal_p (cp_parser *parser)
7424 cp_lexer_save_tokens (parser->lexer);
7426 /* Skip tokens until the next token is a closing parenthesis.
7427 If we find the closing `)', and the next token is a `{', then
7428 we are looking at a compound-literal. */
7429 bool compound_literal_p
7430 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7431 /*consume_paren=*/true)
7432 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
7434 /* Roll back the tokens we skipped. */
7435 cp_lexer_rollback_tokens (parser->lexer);
7437 return compound_literal_p;
7440 /* Return true if EXPR is the integer constant zero or a complex constant
7441 of zero, without any folding, but ignoring location wrappers. */
7443 bool
7444 literal_integer_zerop (const_tree expr)
7446 return (location_wrapper_p (expr)
7447 && integer_zerop (TREE_OPERAND (expr, 0)));
7450 /* Parse a postfix-expression.
7452 postfix-expression:
7453 primary-expression
7454 postfix-expression [ expression ]
7455 postfix-expression ( expression-list [opt] )
7456 simple-type-specifier ( expression-list [opt] )
7457 typename :: [opt] nested-name-specifier identifier
7458 ( expression-list [opt] )
7459 typename :: [opt] nested-name-specifier template [opt] template-id
7460 ( expression-list [opt] )
7461 postfix-expression . template [opt] id-expression
7462 postfix-expression -> template [opt] id-expression
7463 postfix-expression . pseudo-destructor-name
7464 postfix-expression -> pseudo-destructor-name
7465 postfix-expression ++
7466 postfix-expression --
7467 dynamic_cast < type-id > ( expression )
7468 static_cast < type-id > ( expression )
7469 reinterpret_cast < type-id > ( expression )
7470 const_cast < type-id > ( expression )
7471 typeid ( expression )
7472 typeid ( type-id )
7474 GNU Extension:
7476 postfix-expression:
7477 ( type-id ) { initializer-list , [opt] }
7479 This extension is a GNU version of the C99 compound-literal
7480 construct. (The C99 grammar uses `type-name' instead of `type-id',
7481 but they are essentially the same concept.)
7483 If ADDRESS_P is true, the postfix expression is the operand of the
7484 `&' operator. CAST_P is true if this expression is the target of a
7485 cast.
7487 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
7488 class member access expressions [expr.ref].
7490 Returns a representation of the expression. */
7492 static cp_expr
7493 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
7494 bool member_access_only_p, bool decltype_p,
7495 cp_id_kind * pidk_return)
7497 cp_token *token;
7498 location_t loc;
7499 enum rid keyword;
7500 cp_id_kind idk = CP_ID_KIND_NONE;
7501 cp_expr postfix_expression = NULL_TREE;
7502 bool is_member_access = false;
7504 /* Peek at the next token. */
7505 token = cp_lexer_peek_token (parser->lexer);
7506 loc = token->location;
7507 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
7509 /* Some of the productions are determined by keywords. */
7510 keyword = token->keyword;
7511 switch (keyword)
7513 case RID_DYNCAST:
7514 case RID_STATCAST:
7515 case RID_REINTCAST:
7516 case RID_CONSTCAST:
7518 tree type;
7519 cp_expr expression;
7520 const char *saved_message;
7521 bool saved_in_type_id_in_expr_p;
7523 /* All of these can be handled in the same way from the point
7524 of view of parsing. Begin by consuming the token
7525 identifying the cast. */
7526 cp_lexer_consume_token (parser->lexer);
7528 /* New types cannot be defined in the cast. */
7529 saved_message = parser->type_definition_forbidden_message;
7530 parser->type_definition_forbidden_message
7531 = G_("types may not be defined in casts");
7533 /* Look for the opening `<'. */
7534 cp_parser_require (parser, CPP_LESS, RT_LESS);
7535 /* Parse the type to which we are casting. */
7536 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7537 parser->in_type_id_in_expr_p = true;
7538 type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
7539 NULL);
7540 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7541 /* Look for the closing `>'. */
7542 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
7543 /* Restore the old message. */
7544 parser->type_definition_forbidden_message = saved_message;
7546 bool saved_greater_than_is_operator_p
7547 = parser->greater_than_is_operator_p;
7548 parser->greater_than_is_operator_p = true;
7550 /* And the expression which is being cast. */
7551 matching_parens parens;
7552 parens.require_open (parser);
7553 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
7554 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
7555 RT_CLOSE_PAREN);
7556 location_t end_loc = close_paren ?
7557 close_paren->location : UNKNOWN_LOCATION;
7559 parser->greater_than_is_operator_p
7560 = saved_greater_than_is_operator_p;
7562 /* Only type conversions to integral or enumeration types
7563 can be used in constant-expressions. */
7564 if (!cast_valid_in_integral_constant_expression_p (type)
7565 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
7567 postfix_expression = error_mark_node;
7568 break;
7571 /* Construct a location e.g. :
7572 reinterpret_cast <int *> (expr)
7573 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7574 ranging from the start of the "*_cast" token to the final closing
7575 paren, with the caret at the start. */
7576 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
7578 switch (keyword)
7580 case RID_DYNCAST:
7581 postfix_expression
7582 = build_dynamic_cast (cp_cast_loc, type, expression,
7583 tf_warning_or_error);
7584 break;
7585 case RID_STATCAST:
7586 postfix_expression
7587 = build_static_cast (cp_cast_loc, type, expression,
7588 tf_warning_or_error);
7589 break;
7590 case RID_REINTCAST:
7591 postfix_expression
7592 = build_reinterpret_cast (cp_cast_loc, type, expression,
7593 tf_warning_or_error);
7594 break;
7595 case RID_CONSTCAST:
7596 postfix_expression
7597 = build_const_cast (cp_cast_loc, type, expression,
7598 tf_warning_or_error);
7599 break;
7600 default:
7601 gcc_unreachable ();
7604 break;
7606 case RID_TYPEID:
7608 tree type;
7609 const char *saved_message;
7610 bool saved_in_type_id_in_expr_p;
7612 /* Consume the `typeid' token. */
7613 cp_lexer_consume_token (parser->lexer);
7614 /* Look for the `(' token. */
7615 matching_parens parens;
7616 parens.require_open (parser);
7617 /* Types cannot be defined in a `typeid' expression. */
7618 saved_message = parser->type_definition_forbidden_message;
7619 parser->type_definition_forbidden_message
7620 = G_("types may not be defined in a %<typeid%> expression");
7621 /* We can't be sure yet whether we're looking at a type-id or an
7622 expression. */
7623 cp_parser_parse_tentatively (parser);
7624 /* Try a type-id first. */
7625 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7626 parser->in_type_id_in_expr_p = true;
7627 type = cp_parser_type_id (parser);
7628 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7629 /* Look for the `)' token. Otherwise, we can't be sure that
7630 we're not looking at an expression: consider `typeid (int
7631 (3))', for example. */
7632 cp_token *close_paren = parens.require_close (parser);
7633 /* If all went well, simply lookup the type-id. */
7634 if (cp_parser_parse_definitely (parser))
7635 postfix_expression = get_typeid (type, tf_warning_or_error);
7636 /* Otherwise, fall back to the expression variant. */
7637 else
7639 tree expression;
7641 /* Look for an expression. */
7642 expression = cp_parser_expression (parser, & idk);
7643 /* Compute its typeid. */
7644 postfix_expression = build_typeid (expression, tf_warning_or_error);
7645 /* Look for the `)' token. */
7646 close_paren = parens.require_close (parser);
7648 /* Restore the saved message. */
7649 parser->type_definition_forbidden_message = saved_message;
7650 /* `typeid' may not appear in an integral constant expression. */
7651 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
7652 postfix_expression = error_mark_node;
7654 /* Construct a location e.g. :
7655 typeid (expr)
7656 ^~~~~~~~~~~~~
7657 ranging from the start of the "typeid" token to the final closing
7658 paren, with the caret at the start. */
7659 if (close_paren)
7661 location_t typeid_loc
7662 = make_location (start_loc, start_loc, close_paren->location);
7663 postfix_expression.set_location (typeid_loc);
7664 postfix_expression.maybe_add_location_wrapper ();
7667 break;
7669 case RID_TYPENAME:
7671 tree type;
7672 /* The syntax permitted here is the same permitted for an
7673 elaborated-type-specifier. */
7674 ++parser->prevent_constrained_type_specifiers;
7675 type = cp_parser_elaborated_type_specifier (parser,
7676 /*is_friend=*/false,
7677 /*is_declaration=*/false);
7678 --parser->prevent_constrained_type_specifiers;
7679 postfix_expression = cp_parser_functional_cast (parser, type);
7681 break;
7683 case RID_ADDRESSOF:
7684 case RID_BUILTIN_SHUFFLE:
7685 case RID_BUILTIN_SHUFFLEVECTOR:
7686 case RID_BUILTIN_LAUNDER:
7687 case RID_BUILTIN_ASSOC_BARRIER:
7689 vec<tree, va_gc> *vec;
7691 cp_lexer_consume_token (parser->lexer);
7692 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
7693 /*cast_p=*/false, /*allow_expansion_p=*/true,
7694 /*non_constant_p=*/NULL);
7695 if (vec == NULL)
7697 postfix_expression = error_mark_node;
7698 break;
7701 for (tree p : *vec)
7702 mark_exp_read (p);
7704 switch (keyword)
7706 case RID_ADDRESSOF:
7707 if (vec->length () == 1)
7708 postfix_expression
7709 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
7710 else
7712 error_at (loc, "wrong number of arguments to "
7713 "%<__builtin_addressof%>");
7714 postfix_expression = error_mark_node;
7716 break;
7718 case RID_BUILTIN_LAUNDER:
7719 if (vec->length () == 1)
7720 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
7721 tf_warning_or_error);
7722 else
7724 error_at (loc, "wrong number of arguments to "
7725 "%<__builtin_launder%>");
7726 postfix_expression = error_mark_node;
7728 break;
7730 case RID_BUILTIN_ASSOC_BARRIER:
7731 if (vec->length () == 1)
7732 postfix_expression = build1_loc (loc, PAREN_EXPR,
7733 TREE_TYPE ((*vec)[0]),
7734 (*vec)[0]);
7735 else
7737 error_at (loc, "wrong number of arguments to "
7738 "%<__builtin_assoc_barrier%>");
7739 postfix_expression = error_mark_node;
7741 break;
7743 case RID_BUILTIN_SHUFFLE:
7744 if (vec->length () == 2)
7745 postfix_expression
7746 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
7747 (*vec)[1], tf_warning_or_error);
7748 else if (vec->length () == 3)
7749 postfix_expression
7750 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
7751 (*vec)[2], tf_warning_or_error);
7752 else
7754 error_at (loc, "wrong number of arguments to "
7755 "%<__builtin_shuffle%>");
7756 postfix_expression = error_mark_node;
7758 break;
7760 case RID_BUILTIN_SHUFFLEVECTOR:
7761 if (vec->length () < 3)
7763 error_at (loc, "wrong number of arguments to "
7764 "%<__builtin_shufflevector%>");
7765 postfix_expression = error_mark_node;
7767 else
7769 postfix_expression
7770 = build_x_shufflevector (loc, vec, tf_warning_or_error);
7772 break;
7774 default:
7775 gcc_unreachable ();
7777 break;
7780 case RID_BUILTIN_CONVERTVECTOR:
7782 tree expression;
7783 tree type;
7784 /* Consume the `__builtin_convertvector' token. */
7785 cp_lexer_consume_token (parser->lexer);
7786 /* Look for the opening `('. */
7787 matching_parens parens;
7788 parens.require_open (parser);
7789 /* Now, parse the assignment-expression. */
7790 expression = cp_parser_assignment_expression (parser);
7791 /* Look for the `,'. */
7792 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7793 location_t type_location
7794 = cp_lexer_peek_token (parser->lexer)->location;
7795 /* Parse the type-id. */
7797 type_id_in_expr_sentinel s (parser);
7798 type = cp_parser_type_id (parser);
7800 /* Look for the closing `)'. */
7801 parens.require_close (parser);
7802 postfix_expression
7803 = cp_build_vec_convert (expression, type_location, type,
7804 tf_warning_or_error);
7805 break;
7808 case RID_BUILTIN_BIT_CAST:
7810 tree expression;
7811 tree type;
7812 /* Consume the `__builtin_bit_cast' token. */
7813 cp_lexer_consume_token (parser->lexer);
7814 /* Look for the opening `('. */
7815 matching_parens parens;
7816 parens.require_open (parser);
7817 location_t type_location
7818 = cp_lexer_peek_token (parser->lexer)->location;
7819 /* Parse the type-id. */
7821 type_id_in_expr_sentinel s (parser);
7822 type = cp_parser_type_id (parser);
7824 /* Look for the `,'. */
7825 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7826 /* Now, parse the assignment-expression. */
7827 expression = cp_parser_assignment_expression (parser);
7828 /* Look for the closing `)'. */
7829 parens.require_close (parser);
7830 postfix_expression
7831 = cp_build_bit_cast (type_location, type, expression,
7832 tf_warning_or_error);
7833 break;
7836 default:
7838 tree type;
7840 /* If the next thing is a simple-type-specifier, we may be
7841 looking at a functional cast. We could also be looking at
7842 an id-expression. So, we try the functional cast, and if
7843 that doesn't work we fall back to the primary-expression. */
7844 cp_parser_parse_tentatively (parser);
7845 /* Look for the simple-type-specifier. */
7846 ++parser->prevent_constrained_type_specifiers;
7847 type = cp_parser_simple_type_specifier (parser,
7848 /*decl_specs=*/NULL,
7849 CP_PARSER_FLAGS_NONE);
7850 --parser->prevent_constrained_type_specifiers;
7851 /* Parse the cast itself. */
7852 if (!cp_parser_error_occurred (parser))
7853 postfix_expression
7854 = cp_parser_functional_cast (parser, type);
7855 /* If that worked, we're done. */
7856 if (cp_parser_parse_definitely (parser))
7857 break;
7859 /* If the functional-cast didn't work out, try a
7860 compound-literal. */
7861 if (cp_parser_allow_gnu_extensions_p (parser)
7862 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7864 cp_expr initializer = NULL_TREE;
7866 cp_parser_parse_tentatively (parser);
7868 matching_parens parens;
7869 parens.consume_open (parser);
7871 /* Avoid calling cp_parser_type_id pointlessly, see comment
7872 in cp_parser_cast_expression about c++/29234. */
7873 if (!cp_parser_compound_literal_p (parser))
7874 cp_parser_simulate_error (parser);
7875 else
7877 /* Parse the type. */
7878 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7879 parser->in_type_id_in_expr_p = true;
7880 type = cp_parser_type_id (parser);
7881 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7882 parens.require_close (parser);
7885 /* If things aren't going well, there's no need to
7886 keep going. */
7887 if (!cp_parser_error_occurred (parser))
7888 /* Parse the brace-enclosed initializer list. */
7889 initializer = cp_parser_braced_list (parser);
7890 /* If that worked, we're definitely looking at a
7891 compound-literal expression. */
7892 if (cp_parser_parse_definitely (parser))
7894 /* Warn the user that a compound literal is not
7895 allowed in standard C++. */
7896 pedwarn (input_location, OPT_Wpedantic,
7897 "ISO C++ forbids compound-literals");
7898 /* For simplicity, we disallow compound literals in
7899 constant-expressions. We could
7900 allow compound literals of integer type, whose
7901 initializer was a constant, in constant
7902 expressions. Permitting that usage, as a further
7903 extension, would not change the meaning of any
7904 currently accepted programs. (Of course, as
7905 compound literals are not part of ISO C++, the
7906 standard has nothing to say.) */
7907 if (cp_parser_non_integral_constant_expression (parser,
7908 NIC_NCC))
7910 postfix_expression = error_mark_node;
7911 break;
7913 /* Form the representation of the compound-literal. */
7914 postfix_expression
7915 = finish_compound_literal (type, initializer,
7916 tf_warning_or_error, fcl_c99);
7917 postfix_expression.set_location (initializer.get_location ());
7918 break;
7922 /* It must be a primary-expression. */
7923 postfix_expression
7924 = cp_parser_primary_expression (parser, address_p, cast_p,
7925 /*template_arg_p=*/false,
7926 decltype_p,
7927 &idk);
7929 break;
7932 /* Note that we don't need to worry about calling build_cplus_new on a
7933 class-valued CALL_EXPR in decltype when it isn't the end of the
7934 postfix-expression; unary_complex_lvalue will take care of that for
7935 all these cases. */
7937 /* Keep looping until the postfix-expression is complete. */
7938 while (true)
7940 if (idk == CP_ID_KIND_UNQUALIFIED
7941 && identifier_p (postfix_expression)
7942 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7943 /* It is not a Koenig lookup function call. */
7944 postfix_expression
7945 = unqualified_name_lookup_error (postfix_expression);
7947 /* Peek at the next token. */
7948 token = cp_lexer_peek_token (parser->lexer);
7950 switch (token->type)
7952 case CPP_OPEN_SQUARE:
7953 if (cp_next_tokens_can_be_std_attribute_p (parser))
7955 cp_parser_error (parser,
7956 "two consecutive %<[%> shall "
7957 "only introduce an attribute");
7958 return error_mark_node;
7960 postfix_expression
7961 = cp_parser_postfix_open_square_expression (parser,
7962 postfix_expression,
7963 false,
7964 decltype_p);
7965 postfix_expression.set_range (start_loc,
7966 postfix_expression.get_location ());
7968 idk = CP_ID_KIND_NONE;
7969 is_member_access = false;
7970 break;
7972 case CPP_OPEN_PAREN:
7973 /* postfix-expression ( expression-list [opt] ) */
7975 bool koenig_p;
7976 bool is_builtin_constant_p;
7977 bool saved_integral_constant_expression_p = false;
7978 bool saved_non_integral_constant_expression_p = false;
7979 tsubst_flags_t complain = complain_flags (decltype_p);
7980 vec<tree, va_gc> *args;
7981 location_t close_paren_loc = UNKNOWN_LOCATION;
7982 location_t combined_loc = UNKNOWN_LOCATION;
7984 is_member_access = false;
7986 tree stripped_expression
7987 = tree_strip_any_location_wrapper (postfix_expression);
7988 is_builtin_constant_p
7989 = DECL_IS_BUILTIN_CONSTANT_P (stripped_expression);
7990 if (is_builtin_constant_p)
7992 /* The whole point of __builtin_constant_p is to allow
7993 non-constant expressions to appear as arguments. */
7994 saved_integral_constant_expression_p
7995 = parser->integral_constant_expression_p;
7996 saved_non_integral_constant_expression_p
7997 = parser->non_integral_constant_expression_p;
7998 parser->integral_constant_expression_p = false;
8000 else if (TREE_CODE (stripped_expression) == FUNCTION_DECL
8001 && fndecl_built_in_p (stripped_expression,
8002 BUILT_IN_CLASSIFY_TYPE))
8004 /* __builtin_classify_type (type) */
8005 auto cl1 = make_temp_override
8006 (parser->type_definition_forbidden_message,
8007 G_("types may not be defined in "
8008 "%<__builtin_classify_type%> calls"));
8009 auto cl2 = make_temp_override
8010 (parser->type_definition_forbidden_message_arg,
8011 NULL);
8012 auto cl3 = make_temp_override (parser->in_type_id_in_expr_p,
8013 true);
8014 cp_unevaluated uev;
8015 cp_parser_parse_tentatively (parser);
8016 matching_parens parens;
8017 parens.consume_open (parser);
8018 tree type = cp_parser_type_id (parser);
8019 parens.require_close (parser);
8020 if (cp_parser_parse_definitely (parser))
8022 if (dependent_type_p (type))
8024 postfix_expression = build_vl_exp (CALL_EXPR, 4);
8025 CALL_EXPR_FN (postfix_expression)
8026 = stripped_expression;
8027 CALL_EXPR_STATIC_CHAIN (postfix_expression) = type;
8028 CALL_EXPR_ARG (postfix_expression, 0)
8029 = build_min (SIZEOF_EXPR, size_type_node, type);
8030 TREE_TYPE (postfix_expression) = integer_type_node;
8032 else
8034 postfix_expression
8035 = build_int_cst (integer_type_node,
8036 type_to_class (type));
8038 break;
8041 args = (cp_parser_parenthesized_expression_list
8042 (parser, non_attr,
8043 /*cast_p=*/false, /*allow_expansion_p=*/true,
8044 /*non_constant_p=*/NULL,
8045 /*close_paren_loc=*/&close_paren_loc,
8046 /*wrap_locations_p=*/true));
8047 if (is_builtin_constant_p)
8049 parser->integral_constant_expression_p
8050 = saved_integral_constant_expression_p;
8051 parser->non_integral_constant_expression_p
8052 = saved_non_integral_constant_expression_p;
8055 if (args == NULL)
8057 postfix_expression = error_mark_node;
8058 break;
8061 /* Function calls are not permitted in
8062 constant-expressions. */
8063 if (! builtin_valid_in_constant_expr_p (postfix_expression)
8064 && cp_parser_non_integral_constant_expression (parser,
8065 NIC_FUNC_CALL))
8067 postfix_expression = error_mark_node;
8068 release_tree_vector (args);
8069 break;
8072 koenig_p = false;
8073 if (idk == CP_ID_KIND_UNQUALIFIED
8074 || idk == CP_ID_KIND_TEMPLATE_ID)
8076 if (identifier_p (postfix_expression)
8077 /* In C++20, we may need to perform ADL for a template
8078 name. */
8079 || (TREE_CODE (postfix_expression) == TEMPLATE_ID_EXPR
8080 && identifier_p (TREE_OPERAND (postfix_expression, 0))))
8082 if (!args->is_empty ())
8084 koenig_p = true;
8085 if (!any_type_dependent_arguments_p (args))
8086 postfix_expression
8087 = perform_koenig_lookup (postfix_expression, args,
8088 complain);
8090 else
8091 postfix_expression
8092 = unqualified_fn_lookup_error (postfix_expression);
8094 /* We do not perform argument-dependent lookup if
8095 normal lookup finds a non-function, in accordance
8096 with the expected resolution of DR 218. */
8097 else if (!args->is_empty ()
8098 && is_overloaded_fn (postfix_expression))
8100 /* Do not do argument dependent lookup if regular
8101 lookup finds a member function or a block-scope
8102 function declaration. [basic.lookup.argdep]/3 */
8103 bool do_adl_p = true;
8104 tree fns = get_fns (postfix_expression);
8105 for (lkp_iterator iter (fns); iter; ++iter)
8107 tree fn = STRIP_TEMPLATE (*iter);
8108 if ((TREE_CODE (fn) == USING_DECL
8109 && DECL_DEPENDENT_P (fn))
8110 || DECL_FUNCTION_MEMBER_P (fn)
8111 || DECL_LOCAL_DECL_P (fn))
8113 do_adl_p = false;
8114 break;
8118 if (do_adl_p)
8120 koenig_p = true;
8121 if (!any_type_dependent_arguments_p (args))
8122 postfix_expression
8123 = perform_koenig_lookup (postfix_expression, args,
8124 complain);
8129 /* Temporarily set input_location to the combined location
8130 with call expression range, as e.g. build_out_target_exprs
8131 called from convert_default_arg relies on input_location,
8132 so updating it only when the call is fully built results
8133 in inconsistencies between location handling in templates
8134 and outside of templates. */
8135 if (close_paren_loc != UNKNOWN_LOCATION)
8136 combined_loc = make_location (token->location, start_loc,
8137 close_paren_loc);
8138 iloc_sentinel ils (combined_loc);
8140 if (TREE_CODE (postfix_expression) == OFFSET_REF
8141 || TREE_CODE (postfix_expression) == MEMBER_REF
8142 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
8143 postfix_expression = (build_offset_ref_call_from_tree
8144 (postfix_expression, &args,
8145 complain));
8146 else
8147 /* All other function calls. */
8149 if (DECL_P (postfix_expression)
8150 && parser->omp_for_parse_state
8151 && parser->omp_for_parse_state->in_intervening_code
8152 && omp_runtime_api_call (postfix_expression))
8154 error_at (loc, "calls to the OpenMP runtime API are "
8155 "not permitted in intervening code");
8156 parser->omp_for_parse_state->fail = true;
8158 bool disallow_virtual = (idk == CP_ID_KIND_QUALIFIED);
8159 postfix_expression
8160 = finish_call_expr (postfix_expression, &args,
8161 disallow_virtual,
8162 koenig_p,
8163 complain);
8166 if (close_paren_loc != UNKNOWN_LOCATION)
8167 postfix_expression.set_location (combined_loc);
8169 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
8170 idk = CP_ID_KIND_NONE;
8172 release_tree_vector (args);
8174 break;
8176 case CPP_DOT:
8177 case CPP_DEREF:
8178 /* postfix-expression . template [opt] id-expression
8179 postfix-expression . pseudo-destructor-name
8180 postfix-expression -> template [opt] id-expression
8181 postfix-expression -> pseudo-destructor-name */
8183 /* Consume the `.' or `->' operator. */
8184 cp_lexer_consume_token (parser->lexer);
8186 postfix_expression
8187 = cp_parser_postfix_dot_deref_expression (parser, token->type,
8188 postfix_expression,
8189 false, &idk, loc);
8191 is_member_access = true;
8192 break;
8194 case CPP_PLUS_PLUS:
8195 /* postfix-expression ++ */
8196 /* Consume the `++' token. */
8197 cp_lexer_consume_token (parser->lexer);
8198 /* Generate a representation for the complete expression. */
8199 postfix_expression
8200 = finish_increment_expr (postfix_expression,
8201 POSTINCREMENT_EXPR);
8202 /* Increments may not appear in constant-expressions. */
8203 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
8204 postfix_expression = error_mark_node;
8205 idk = CP_ID_KIND_NONE;
8206 is_member_access = false;
8207 break;
8209 case CPP_MINUS_MINUS:
8210 /* postfix-expression -- */
8211 /* Consume the `--' token. */
8212 cp_lexer_consume_token (parser->lexer);
8213 /* Generate a representation for the complete expression. */
8214 postfix_expression
8215 = finish_increment_expr (postfix_expression,
8216 POSTDECREMENT_EXPR);
8217 /* Decrements may not appear in constant-expressions. */
8218 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
8219 postfix_expression = error_mark_node;
8220 idk = CP_ID_KIND_NONE;
8221 is_member_access = false;
8222 break;
8224 default:
8225 if (pidk_return != NULL)
8226 * pidk_return = idk;
8227 if (member_access_only_p)
8228 return is_member_access
8229 ? postfix_expression
8230 : cp_expr (error_mark_node);
8231 else
8232 return postfix_expression;
8237 /* Helper function for cp_parser_parenthesized_expression_list and
8238 cp_parser_postfix_open_square_expression. Parse a single element
8239 of parenthesized expression list. */
8241 static cp_expr
8242 cp_parser_parenthesized_expression_list_elt (cp_parser *parser, bool cast_p,
8243 bool allow_expansion_p,
8244 bool *non_constant_p)
8246 cp_expr expr (NULL_TREE);
8247 bool expr_non_constant_p;
8249 /* Parse the next assignment-expression. */
8250 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8252 /* A braced-init-list. */
8253 cp_lexer_set_source_position (parser->lexer);
8254 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8255 expr = cp_parser_braced_list (parser,
8256 (non_constant_p != nullptr
8257 ? &expr_non_constant_p
8258 : nullptr));
8259 if (non_constant_p && expr_non_constant_p)
8260 *non_constant_p = true;
8262 else if (non_constant_p)
8264 expr = cp_parser_constant_expression (parser,
8265 /*allow_non_constant_p=*/true,
8266 &expr_non_constant_p);
8267 if (expr_non_constant_p)
8268 *non_constant_p = true;
8270 else
8271 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL, cast_p);
8273 /* If we have an ellipsis, then this is an expression expansion. */
8274 if (allow_expansion_p
8275 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8277 /* Consume the `...'. */
8278 cp_lexer_consume_token (parser->lexer);
8280 /* Build the argument pack. */
8281 expr = make_pack_expansion (expr);
8283 return expr;
8286 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
8287 by cp_parser_builtin_offsetof. We're looking for
8289 postfix-expression [ expression ]
8290 postfix-expression [ braced-init-list ] (C++11)
8291 postfix-expression [ expression-list[opt] ] (C++23)
8293 FOR_OFFSETOF is set if we're being called in that context, which
8294 changes how we deal with integer constant expressions. */
8296 static tree
8297 cp_parser_postfix_open_square_expression (cp_parser *parser,
8298 tree postfix_expression,
8299 bool for_offsetof,
8300 bool decltype_p)
8302 tree index = NULL_TREE;
8303 releasing_vec expression_list = NULL;
8304 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8305 bool saved_greater_than_is_operator_p;
8306 bool saved_colon_corrects_to_scope_p;
8308 /* Consume the `[' token. */
8309 cp_lexer_consume_token (parser->lexer);
8311 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
8312 parser->greater_than_is_operator_p = true;
8314 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8315 if (parser->omp_array_section_p)
8316 parser->colon_corrects_to_scope_p = false;
8318 /* Parse the index expression. */
8319 /* ??? For offsetof, there is a question of what to allow here. If
8320 offsetof is not being used in an integral constant expression context,
8321 then we *could* get the right answer by computing the value at runtime.
8322 If we are in an integral constant expression context, then we might
8323 could accept any constant expression; hard to say without analysis.
8324 Rather than open the barn door too wide right away, allow only integer
8325 constant expressions here. */
8326 if (for_offsetof)
8327 index = cp_parser_constant_expression (parser);
8328 else if (!parser->omp_array_section_p
8329 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8331 if (cxx_dialect >= cxx23
8332 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
8333 *&expression_list = make_tree_vector ();
8334 else if (cxx_dialect >= cxx23)
8336 while (true)
8338 cp_expr expr
8339 = cp_parser_parenthesized_expression_list_elt (parser,
8340 /*cast_p=*/
8341 false,
8342 /*allow_exp_p=*/
8343 true,
8344 /*non_cst_p=*/
8345 NULL);
8347 if (expr == error_mark_node)
8348 index = error_mark_node;
8349 else if (expression_list.get () == NULL
8350 && !PACK_EXPANSION_P (expr.get_value ()))
8351 index = expr.get_value ();
8352 else
8353 vec_safe_push (expression_list, expr.get_value ());
8355 /* If the next token isn't a `,', then we are done. */
8356 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8357 break;
8359 if (expression_list.get () == NULL && index != error_mark_node)
8361 *&expression_list = make_tree_vector_single (index);
8362 index = NULL_TREE;
8365 /* Otherwise, consume the `,' and keep going. */
8366 cp_lexer_consume_token (parser->lexer);
8368 if (expression_list.get () && index == error_mark_node)
8369 expression_list.release ();
8371 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8373 cp_lexer_set_source_position (parser->lexer);
8374 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8375 index = cp_parser_braced_list (parser);
8377 else
8378 index = cp_parser_expression (parser, NULL, /*cast_p=*/false,
8379 /*decltype_p=*/false,
8380 /*warn_comma_p=*/warn_comma_subscript);
8383 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
8385 if (cxx_dialect >= cxx23
8386 && parser->omp_array_section_p
8387 && expression_list.get () != NULL
8388 && vec_safe_length (expression_list) > 1)
8390 error_at (loc, "cannot use multidimensional subscript in OpenMP array "
8391 "section");
8392 index = error_mark_node;
8394 if (parser->omp_array_section_p
8395 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
8397 cp_lexer_consume_token (parser->lexer);
8398 tree length = NULL_TREE;
8399 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
8401 if (cxx_dialect >= cxx23)
8403 cp_expr expr
8404 = cp_parser_parenthesized_expression_list_elt (parser,
8405 /*cast_p=*/
8406 false,
8407 /*allow_exp_p=*/
8408 true,
8409 /*non_cst_p=*/
8410 NULL);
8412 if (expr == error_mark_node)
8413 length = error_mark_node;
8414 else
8415 length = expr.get_value ();
8417 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8419 error_at (loc, "cannot use multidimensional subscript in "
8420 "OpenMP array section");
8421 length = error_mark_node;
8424 else
8425 length
8426 = cp_parser_expression (parser, NULL, /*cast_p=*/false,
8427 /*decltype_p=*/false,
8428 /*warn_comma_p=*/warn_comma_subscript);
8431 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8433 if (index == error_mark_node || length == error_mark_node)
8435 cp_parser_skip_to_closing_square_bracket (parser);
8436 return error_mark_node;
8438 else
8439 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8441 return grok_omp_array_section (input_location, postfix_expression, index,
8442 length);
8445 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8447 /* Look for the closing `]'. */
8448 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8450 /* Build the ARRAY_REF. */
8451 postfix_expression = grok_array_decl (loc, postfix_expression,
8452 index, &expression_list,
8453 tf_warning_or_error
8454 | (decltype_p ? tf_decltype : 0));
8456 /* When not doing offsetof, array references are not permitted in
8457 constant-expressions. */
8458 if (!for_offsetof
8459 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
8460 postfix_expression = error_mark_node;
8462 return postfix_expression;
8465 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
8466 dereference of incomplete type, returns true if error_mark_node should
8467 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
8468 and *DEPENDENT_P. */
8470 bool
8471 cp_parser_dot_deref_incomplete (tree *scope, cp_expr *postfix_expression,
8472 bool *dependent_p)
8474 /* In a template, be permissive by treating an object expression
8475 of incomplete type as dependent (after a pedwarn). */
8476 diagnostic_t kind = (processing_template_decl
8477 && MAYBE_CLASS_TYPE_P (*scope) ? DK_PEDWARN : DK_ERROR);
8479 switch (TREE_CODE (*postfix_expression))
8481 case CAST_EXPR:
8482 case REINTERPRET_CAST_EXPR:
8483 case CONST_CAST_EXPR:
8484 case STATIC_CAST_EXPR:
8485 case DYNAMIC_CAST_EXPR:
8486 case IMPLICIT_CONV_EXPR:
8487 case VIEW_CONVERT_EXPR:
8488 case NON_LVALUE_EXPR:
8489 kind = DK_ERROR;
8490 break;
8491 case OVERLOAD:
8492 /* Don't emit any diagnostic for OVERLOADs. */
8493 kind = DK_IGNORED;
8494 break;
8495 default:
8496 /* Avoid clobbering e.g. DECLs. */
8497 if (!EXPR_P (*postfix_expression))
8498 kind = DK_ERROR;
8499 break;
8502 if (kind == DK_IGNORED)
8503 return false;
8505 location_t exploc = location_of (*postfix_expression);
8506 cxx_incomplete_type_diagnostic (exploc, *postfix_expression, *scope, kind);
8507 if (!MAYBE_CLASS_TYPE_P (*scope))
8508 return true;
8509 if (kind == DK_ERROR)
8510 *scope = *postfix_expression = error_mark_node;
8511 else if (processing_template_decl)
8513 *dependent_p = true;
8514 *scope = TREE_TYPE (*postfix_expression) = NULL_TREE;
8516 return false;
8519 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
8520 by cp_parser_builtin_offsetof. We're looking for
8522 postfix-expression . template [opt] id-expression
8523 postfix-expression . pseudo-destructor-name
8524 postfix-expression -> template [opt] id-expression
8525 postfix-expression -> pseudo-destructor-name
8527 FOR_OFFSETOF is set if we're being called in that context. That sorta
8528 limits what of the above we'll actually accept, but nevermind.
8529 TOKEN_TYPE is the "." or "->" token, which will already have been
8530 removed from the stream. */
8532 static tree
8533 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
8534 enum cpp_ttype token_type,
8535 cp_expr postfix_expression,
8536 bool for_offsetof, cp_id_kind *idk,
8537 location_t location)
8539 tree name;
8540 bool dependent_p;
8541 bool pseudo_destructor_p;
8542 tree scope = NULL_TREE;
8543 location_t start_loc = postfix_expression.get_start ();
8545 /* If this is a `->' operator, dereference the pointer. */
8546 if (token_type == CPP_DEREF)
8547 postfix_expression = build_x_arrow (location, postfix_expression,
8548 tf_warning_or_error);
8549 /* Check to see whether or not the expression is type-dependent and
8550 not the current instantiation. */
8551 dependent_p = type_dependent_object_expression_p (postfix_expression);
8552 /* The identifier following the `->' or `.' is not qualified. */
8553 parser->scope = NULL_TREE;
8554 parser->qualifying_scope = NULL_TREE;
8555 parser->object_scope = NULL_TREE;
8556 *idk = CP_ID_KIND_NONE;
8558 /* Enter the scope corresponding to the type of the object
8559 given by the POSTFIX_EXPRESSION. */
8560 if (!dependent_p)
8562 scope = TREE_TYPE (postfix_expression);
8563 /* According to the standard, no expression should ever have
8564 reference type. Unfortunately, we do not currently match
8565 the standard in this respect in that our internal representation
8566 of an expression may have reference type even when the standard
8567 says it does not. Therefore, we have to manually obtain the
8568 underlying type here. */
8569 scope = non_reference (scope);
8570 /* The type of the POSTFIX_EXPRESSION must be complete. */
8571 /* Unlike the object expression in other contexts, *this is not
8572 required to be of complete type for purposes of class member
8573 access (5.2.5) outside the member function body. */
8574 if (postfix_expression != current_class_ref
8575 && scope != error_mark_node
8576 && !currently_open_class (scope))
8578 scope = complete_type (scope);
8579 if (!COMPLETE_TYPE_P (scope)
8580 && cp_parser_dot_deref_incomplete (&scope, &postfix_expression,
8581 &dependent_p))
8582 return error_mark_node;
8585 if (!dependent_p)
8587 /* Let the name lookup machinery know that we are processing a
8588 class member access expression. */
8589 parser->context->object_type = scope;
8590 /* If something went wrong, we want to be able to discern that case,
8591 as opposed to the case where there was no SCOPE due to the type
8592 of expression being dependent. */
8593 if (!scope)
8594 scope = error_mark_node;
8595 /* If the SCOPE was erroneous, make the various semantic analysis
8596 functions exit quickly -- and without issuing additional error
8597 messages. */
8598 if (scope == error_mark_node)
8599 postfix_expression = error_mark_node;
8603 if (dependent_p)
8605 tree type = TREE_TYPE (postfix_expression);
8606 /* If we don't have a (type-dependent) object of class type, use
8607 typeof to figure out the type of the object. */
8608 if (type == NULL_TREE || is_auto (type))
8609 type = finish_typeof (postfix_expression);
8610 parser->context->object_type = type;
8613 /* Assume this expression is not a pseudo-destructor access. */
8614 pseudo_destructor_p = false;
8616 /* If the SCOPE is a scalar type, then, if this is a valid program,
8617 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
8618 is type dependent, it can be pseudo-destructor-name or something else.
8619 Try to parse it as pseudo-destructor-name first. */
8620 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
8622 tree s;
8623 tree type;
8625 cp_parser_parse_tentatively (parser);
8626 /* Parse the pseudo-destructor-name. */
8627 s = NULL_TREE;
8628 cp_parser_pseudo_destructor_name (parser, postfix_expression,
8629 &s, &type);
8630 if (dependent_p
8631 && (cp_parser_error_occurred (parser)
8632 || !SCALAR_TYPE_P (type)))
8633 cp_parser_abort_tentative_parse (parser);
8634 else if (cp_parser_parse_definitely (parser))
8636 pseudo_destructor_p = true;
8637 postfix_expression
8638 = finish_pseudo_destructor_expr (postfix_expression,
8639 s, type, location);
8643 if (!pseudo_destructor_p)
8645 /* If the SCOPE is not a scalar type, we are looking at an
8646 ordinary class member access expression, rather than a
8647 pseudo-destructor-name. */
8648 bool template_p;
8649 cp_token *token = cp_lexer_peek_token (parser->lexer);
8650 /* Parse the id-expression. */
8651 name = (cp_parser_id_expression
8652 (parser,
8653 cp_parser_optional_template_keyword (parser),
8654 /*check_dependency_p=*/true,
8655 &template_p,
8656 /*declarator_p=*/false,
8657 /*optional_p=*/false));
8658 /* In general, build a SCOPE_REF if the member name is qualified.
8659 However, if the name was not dependent and has already been
8660 resolved; there is no need to build the SCOPE_REF. For example;
8662 struct X { void f(); };
8663 template <typename T> void f(T* t) { t->X::f(); }
8665 Even though "t" is dependent, "X::f" is not and has been resolved
8666 to a BASELINK; there is no need to include scope information. */
8668 /* But we do need to remember that there was an explicit scope for
8669 virtual function calls. */
8670 if (parser->scope)
8671 *idk = CP_ID_KIND_QUALIFIED;
8673 /* If the name is a template-id that names a type, we will get a
8674 TYPE_DECL here. That is invalid code. */
8675 if (TREE_CODE (name) == TYPE_DECL)
8677 error_at (token->location, "invalid use of %qD", name);
8678 postfix_expression = error_mark_node;
8680 else
8682 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
8684 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
8686 error_at (token->location, "%<%D::%D%> is not a class member",
8687 parser->scope, name);
8688 postfix_expression = error_mark_node;
8690 else
8691 name = build_qualified_name (/*type=*/NULL_TREE,
8692 parser->scope,
8693 name,
8694 template_p);
8695 parser->scope = NULL_TREE;
8696 parser->qualifying_scope = NULL_TREE;
8697 parser->object_scope = NULL_TREE;
8699 if (parser->scope && name && BASELINK_P (name))
8700 adjust_result_of_qualified_name_lookup
8701 (name, parser->scope, scope);
8702 postfix_expression
8703 = finish_class_member_access_expr (postfix_expression, name,
8704 template_p,
8705 tf_warning_or_error);
8706 /* Build a location e.g.:
8707 ptr->access_expr
8708 ~~~^~~~~~~~~~~~~
8709 where the caret is at the deref token, ranging from
8710 the start of postfix_expression to the end of the access expr. */
8711 location_t combined_loc
8712 = make_location (input_location, start_loc, parser->lexer);
8713 protected_set_expr_location (postfix_expression, combined_loc);
8717 /* We no longer need to look up names in the scope of the object on
8718 the left-hand side of the `.' or `->' operator. */
8719 parser->context->object_type = NULL_TREE;
8721 /* Outside of offsetof, these operators may not appear in
8722 constant-expressions. */
8723 if (!for_offsetof
8724 && (cp_parser_non_integral_constant_expression
8725 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
8726 postfix_expression = error_mark_node;
8728 return postfix_expression;
8731 /* Parse a parenthesized expression-list.
8733 expression-list:
8734 assignment-expression
8735 expression-list, assignment-expression
8737 attribute-list:
8738 expression-list
8739 identifier
8740 identifier, expression-list
8742 CAST_P is true if this expression is the target of a cast.
8744 ALLOW_EXPANSION_P is true if this expression allows expansion of an
8745 argument pack.
8747 WRAP_LOCATIONS_P is true if expressions within this list for which
8748 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
8749 their source locations.
8751 Returns a vector of trees. Each element is a representation of an
8752 assignment-expression. NULL is returned if the ( and or ) are
8753 missing. An empty, but allocated, vector is returned on no
8754 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
8755 if we are parsing an attribute list for an attribute that wants a
8756 plain identifier argument, normal_attr for an attribute that wants
8757 an expression, or non_attr if we aren't parsing an attribute list. If
8758 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
8759 not all of the expressions in the list were constant.
8760 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
8761 will be written to with the location of the closing parenthesis. If
8762 an error occurs, it may or may not be written to. */
8764 static vec<tree, va_gc> *
8765 cp_parser_parenthesized_expression_list (cp_parser* parser,
8766 int is_attribute_list,
8767 bool cast_p,
8768 bool allow_expansion_p,
8769 bool *non_constant_p,
8770 location_t *close_paren_loc,
8771 bool wrap_locations_p)
8773 vec<tree, va_gc> *expression_list;
8774 bool saved_greater_than_is_operator_p;
8775 bool saved_omp_array_section_p;
8777 /* Assume all the expressions will be constant. */
8778 if (non_constant_p)
8779 *non_constant_p = false;
8781 matching_parens parens;
8782 if (!parens.require_open (parser))
8783 return NULL;
8785 expression_list = make_tree_vector ();
8787 /* Within a parenthesized expression, a `>' token is always
8788 the greater-than operator. */
8789 saved_greater_than_is_operator_p
8790 = parser->greater_than_is_operator_p;
8791 parser->greater_than_is_operator_p = true;
8793 saved_omp_array_section_p = parser->omp_array_section_p;
8794 parser->omp_array_section_p = false;
8796 cp_expr expr (NULL_TREE);
8798 /* Consume expressions until there are no more. */
8799 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8800 while (true)
8802 /* At the beginning of attribute lists, check to see if the
8803 next token is an identifier. */
8804 if (is_attribute_list == id_attr
8805 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
8806 expr = cp_lexer_consume_token (parser->lexer)->u.value;
8807 else if (is_attribute_list == assume_attr)
8808 expr = cp_parser_conditional_expression (parser);
8809 else if (is_attribute_list == uneval_string_attr)
8810 expr = cp_parser_unevaluated_string_literal (parser);
8811 else
8812 expr
8813 = cp_parser_parenthesized_expression_list_elt (parser, cast_p,
8814 allow_expansion_p,
8815 non_constant_p);
8817 if (wrap_locations_p)
8818 expr.maybe_add_location_wrapper ();
8820 /* Add it to the list. We add error_mark_node
8821 expressions to the list, so that we can still tell if
8822 the correct form for a parenthesized expression-list
8823 is found. That gives better errors. */
8824 vec_safe_push (expression_list, expr.get_value ());
8826 if (expr == error_mark_node)
8827 goto skip_comma;
8829 /* After the first item, attribute lists look the same as
8830 expression lists. */
8831 is_attribute_list = non_attr;
8833 get_comma:;
8834 /* If the next token isn't a `,', then we are done. */
8835 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8836 break;
8838 /* Otherwise, consume the `,' and keep going. */
8839 cp_lexer_consume_token (parser->lexer);
8842 if (close_paren_loc)
8843 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
8845 if (!parens.require_close (parser))
8847 int ending;
8849 skip_comma:;
8850 /* We try and resync to an unnested comma, as that will give the
8851 user better diagnostics. */
8852 ending = cp_parser_skip_to_closing_parenthesis (parser,
8853 /*recovering=*/true,
8854 /*or_comma=*/true,
8855 /*consume_paren=*/true);
8856 if (ending < 0)
8857 goto get_comma;
8858 if (!ending)
8860 parser->greater_than_is_operator_p
8861 = saved_greater_than_is_operator_p;
8862 parser->omp_array_section_p = saved_omp_array_section_p;
8863 return NULL;
8867 parser->greater_than_is_operator_p
8868 = saved_greater_than_is_operator_p;
8869 parser->omp_array_section_p = saved_omp_array_section_p;
8871 return expression_list;
8874 /* Parse a pseudo-destructor-name.
8876 pseudo-destructor-name:
8877 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
8878 :: [opt] nested-name-specifier template template-id :: ~ type-name
8879 :: [opt] nested-name-specifier [opt] ~ type-name
8881 If either of the first two productions is used, sets *SCOPE to the
8882 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
8883 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
8884 or ERROR_MARK_NODE if the parse fails. */
8886 static void
8887 cp_parser_pseudo_destructor_name (cp_parser* parser,
8888 tree object,
8889 tree* scope,
8890 tree* type)
8892 bool nested_name_specifier_p;
8894 /* Handle ~auto. */
8895 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
8896 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
8897 && !type_dependent_expression_p (object))
8899 if (cxx_dialect < cxx14)
8900 pedwarn (input_location, OPT_Wc__14_extensions,
8901 "%<~auto%> only available with "
8902 "%<-std=c++14%> or %<-std=gnu++14%>");
8903 cp_lexer_consume_token (parser->lexer);
8904 cp_lexer_consume_token (parser->lexer);
8905 *scope = NULL_TREE;
8906 *type = TREE_TYPE (object);
8907 return;
8910 /* Assume that things will not work out. */
8911 *type = error_mark_node;
8913 /* Look for the optional `::' operator. */
8914 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
8915 /* Look for the optional nested-name-specifier. */
8916 nested_name_specifier_p
8917 = (cp_parser_nested_name_specifier_opt (parser,
8918 /*typename_keyword_p=*/false,
8919 /*check_dependency_p=*/true,
8920 /*type_p=*/false,
8921 /*is_declaration=*/false)
8922 != NULL_TREE);
8923 /* Now, if we saw a nested-name-specifier, we might be doing the
8924 second production. */
8925 if (nested_name_specifier_p
8926 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8928 /* Consume the `template' keyword. */
8929 cp_lexer_consume_token (parser->lexer);
8930 /* Parse the template-id. */
8931 cp_parser_template_id (parser,
8932 /*template_keyword_p=*/true,
8933 /*check_dependency_p=*/false,
8934 class_type,
8935 /*is_declaration=*/true);
8936 /* Look for the `::' token. */
8937 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
8939 /* If the next token is not a `~', then there might be some
8940 additional qualification. */
8941 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
8943 /* At this point, we're looking for "type-name :: ~". The type-name
8944 must not be a class-name, since this is a pseudo-destructor. So,
8945 it must be either an enum-name, or a typedef-name -- both of which
8946 are just identifiers. So, we peek ahead to check that the "::"
8947 and "~" tokens are present; if they are not, then we can avoid
8948 calling type_name. */
8949 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
8950 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
8951 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
8953 cp_parser_error (parser, "non-scalar type");
8954 return;
8957 /* Look for the type-name. */
8958 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
8959 if (*scope == error_mark_node)
8960 return;
8962 /* Look for the `::' token. */
8963 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
8965 else
8966 *scope = NULL_TREE;
8968 /* Look for the `~'. */
8969 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
8971 /* Once we see the ~, this has to be a pseudo-destructor. */
8972 if (!processing_template_decl && !cp_parser_error_occurred (parser))
8973 cp_parser_commit_to_topmost_tentative_parse (parser);
8975 /* Look for the type-name again. We are not responsible for
8976 checking that it matches the first type-name. */
8977 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
8980 /* Parse a unary-expression.
8982 unary-expression:
8983 postfix-expression
8984 ++ cast-expression
8985 -- cast-expression
8986 await-expression
8987 unary-operator cast-expression
8988 sizeof unary-expression
8989 sizeof ( type-id )
8990 alignof ( type-id ) [C++0x]
8991 new-expression
8992 delete-expression
8994 GNU Extensions:
8996 unary-expression:
8997 __extension__ cast-expression
8998 __alignof__ unary-expression
8999 __alignof__ ( type-id )
9000 alignof unary-expression [C++0x]
9001 __real__ cast-expression
9002 __imag__ cast-expression
9003 && identifier
9004 sizeof ( type-id ) { initializer-list , [opt] }
9005 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
9006 __alignof__ ( type-id ) { initializer-list , [opt] }
9008 ADDRESS_P is true iff the unary-expression is appearing as the
9009 operand of the `&' operator. CAST_P is true if this expression is
9010 the target of a cast.
9012 Returns a representation of the expression. */
9014 static cp_expr
9015 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
9016 bool address_p, bool cast_p, bool decltype_p)
9018 cp_token *token;
9019 enum tree_code unary_operator;
9021 /* Peek at the next token. */
9022 token = cp_lexer_peek_token (parser->lexer);
9023 /* Some keywords give away the kind of expression. */
9024 if (token->type == CPP_KEYWORD)
9026 enum rid keyword = token->keyword;
9028 switch (keyword)
9030 case RID_ALIGNOF:
9031 case RID_SIZEOF:
9033 tree operand, ret;
9034 enum tree_code op;
9035 location_t start_loc = token->location;
9037 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
9038 bool std_alignof = id_equal (token->u.value, "alignof");
9040 /* Consume the token. */
9041 cp_lexer_consume_token (parser->lexer);
9042 /* Parse the operand. */
9043 operand = cp_parser_sizeof_operand (parser, keyword);
9045 /* Construct a location e.g. :
9046 alignof (expr)
9047 ^~~~~~~~~~~~~~
9048 with start == caret at the start of the "alignof"/"sizeof"
9049 token, with the endpoint at the final closing paren. */
9050 location_t compound_loc
9051 = make_location (start_loc, start_loc, parser->lexer);
9053 if (TYPE_P (operand))
9054 ret = cxx_sizeof_or_alignof_type (compound_loc, operand, op,
9055 std_alignof, true);
9056 else
9058 /* ISO C++ defines alignof only with types, not with
9059 expressions. So pedwarn if alignof is used with a non-
9060 type expression. However, __alignof__ is ok. */
9061 if (std_alignof)
9062 pedwarn (token->location, OPT_Wpedantic,
9063 "ISO C++ does not allow %<alignof%> "
9064 "with a non-type");
9066 ret = cxx_sizeof_or_alignof_expr (compound_loc, operand, op,
9067 std_alignof, true);
9069 /* For SIZEOF_EXPR, just issue diagnostics, but keep
9070 SIZEOF_EXPR with the original operand. */
9071 if (op == SIZEOF_EXPR && ret != error_mark_node)
9073 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
9075 if (!processing_template_decl && TYPE_P (operand))
9077 ret = build_min (SIZEOF_EXPR, size_type_node,
9078 build1 (NOP_EXPR, operand,
9079 error_mark_node));
9080 SIZEOF_EXPR_TYPE_P (ret) = 1;
9082 else
9083 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
9084 TREE_SIDE_EFFECTS (ret) = 0;
9085 TREE_READONLY (ret) = 1;
9086 SET_EXPR_LOCATION (ret, compound_loc);
9090 cp_expr ret_expr (ret, compound_loc);
9091 ret_expr = ret_expr.maybe_add_location_wrapper ();
9092 return ret_expr;
9095 case RID_BUILTIN_HAS_ATTRIBUTE:
9096 return cp_parser_has_attribute_expression (parser);
9098 case RID_NEW:
9099 return cp_parser_new_expression (parser);
9101 case RID_DELETE:
9102 return cp_parser_delete_expression (parser);
9104 case RID_EXTENSION:
9106 /* The saved value of the PEDANTIC flag. */
9107 int saved_pedantic;
9108 tree expr;
9110 /* Save away the PEDANTIC flag. */
9111 cp_parser_extension_opt (parser, &saved_pedantic);
9112 /* Parse the cast-expression. */
9113 expr = cp_parser_simple_cast_expression (parser);
9114 /* Restore the PEDANTIC flag. */
9115 pedantic = saved_pedantic;
9117 return expr;
9120 case RID_REALPART:
9121 case RID_IMAGPART:
9123 tree expression;
9125 /* Consume the `__real__' or `__imag__' token. */
9126 cp_lexer_consume_token (parser->lexer);
9127 /* Parse the cast-expression. */
9128 expression = cp_parser_simple_cast_expression (parser);
9129 /* Create the complete representation. */
9130 return build_x_unary_op (token->location,
9131 (keyword == RID_REALPART
9132 ? REALPART_EXPR : IMAGPART_EXPR),
9133 expression, NULL_TREE,
9134 tf_warning_or_error);
9136 break;
9138 case RID_TRANSACTION_ATOMIC:
9139 case RID_TRANSACTION_RELAXED:
9140 return cp_parser_transaction_expression (parser, keyword);
9142 case RID_NOEXCEPT:
9144 tree expr;
9145 const char *saved_message;
9146 bool saved_integral_constant_expression_p;
9147 bool saved_non_integral_constant_expression_p;
9148 bool saved_greater_than_is_operator_p;
9150 location_t start_loc = token->location;
9152 cp_lexer_consume_token (parser->lexer);
9153 matching_parens parens;
9154 parens.require_open (parser);
9156 saved_message = parser->type_definition_forbidden_message;
9157 parser->type_definition_forbidden_message
9158 = G_("types may not be defined in %<noexcept%> expressions");
9160 saved_integral_constant_expression_p
9161 = parser->integral_constant_expression_p;
9162 saved_non_integral_constant_expression_p
9163 = parser->non_integral_constant_expression_p;
9164 parser->integral_constant_expression_p = false;
9166 saved_greater_than_is_operator_p
9167 = parser->greater_than_is_operator_p;
9168 parser->greater_than_is_operator_p = true;
9170 ++cp_unevaluated_operand;
9171 ++c_inhibit_evaluation_warnings;
9172 ++cp_noexcept_operand;
9173 expr = cp_parser_expression (parser);
9174 --cp_noexcept_operand;
9175 --c_inhibit_evaluation_warnings;
9176 --cp_unevaluated_operand;
9178 parser->greater_than_is_operator_p
9179 = saved_greater_than_is_operator_p;
9181 parser->integral_constant_expression_p
9182 = saved_integral_constant_expression_p;
9183 parser->non_integral_constant_expression_p
9184 = saved_non_integral_constant_expression_p;
9186 parser->type_definition_forbidden_message = saved_message;
9188 parens.require_close (parser);
9190 /* Construct a location of the form:
9191 noexcept (expr)
9192 ^~~~~~~~~~~~~~~
9193 with start == caret, finishing at the close-paren. */
9194 location_t noexcept_loc
9195 = make_location (start_loc, start_loc, parser->lexer);
9197 return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
9198 noexcept_loc);
9201 case RID_CO_AWAIT:
9203 tree expr;
9204 location_t kw_loc = token->location;
9206 /* Consume the `co_await' token. */
9207 cp_lexer_consume_token (parser->lexer);
9208 /* Parse its cast-expression. */
9209 expr = cp_parser_simple_cast_expression (parser);
9210 if (expr == error_mark_node)
9211 return error_mark_node;
9213 /* Handle [expr.await]. */
9214 return cp_expr (finish_co_await_expr (kw_loc, expr));
9217 default:
9218 break;
9222 /* Look for the `:: new' and `:: delete', which also signal the
9223 beginning of a new-expression, or delete-expression,
9224 respectively. If the next token is `::', then it might be one of
9225 these. */
9226 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9228 enum rid keyword;
9230 /* See if the token after the `::' is one of the keywords in
9231 which we're interested. */
9232 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
9233 /* If it's `new', we have a new-expression. */
9234 if (keyword == RID_NEW)
9235 return cp_parser_new_expression (parser);
9236 /* Similarly, for `delete'. */
9237 else if (keyword == RID_DELETE)
9238 return cp_parser_delete_expression (parser);
9241 /* Look for a unary operator. */
9242 unary_operator = cp_parser_unary_operator (token);
9243 /* The `++' and `--' operators can be handled similarly, even though
9244 they are not technically unary-operators in the grammar. */
9245 if (unary_operator == ERROR_MARK)
9247 if (token->type == CPP_PLUS_PLUS)
9248 unary_operator = PREINCREMENT_EXPR;
9249 else if (token->type == CPP_MINUS_MINUS)
9250 unary_operator = PREDECREMENT_EXPR;
9251 /* Handle the GNU address-of-label extension. */
9252 else if (cp_parser_allow_gnu_extensions_p (parser)
9253 && token->type == CPP_AND_AND)
9255 tree identifier;
9256 tree expression;
9257 location_t start_loc = token->location;
9259 /* Consume the '&&' token. */
9260 cp_lexer_consume_token (parser->lexer);
9261 /* Look for the identifier. */
9262 identifier = cp_parser_identifier (parser);
9263 /* Construct a location of the form:
9264 &&label
9265 ^~~~~~~
9266 with caret==start at the "&&", finish at the end of the label. */
9267 location_t combined_loc
9268 = make_location (start_loc, start_loc, parser->lexer);
9269 /* Create an expression representing the address. */
9270 expression = finish_label_address_expr (identifier, combined_loc);
9271 if (TREE_CODE (expression) == ADDR_EXPR)
9272 mark_label_addressed (identifier);
9273 if (cp_parser_non_integral_constant_expression (parser,
9274 NIC_ADDR_LABEL))
9275 expression = error_mark_node;
9276 return expression;
9279 if (unary_operator != ERROR_MARK)
9281 cp_expr cast_expression;
9282 cp_expr expression = error_mark_node;
9283 non_integral_constant non_constant_p = NIC_NONE;
9284 location_t loc = token->location;
9285 tsubst_flags_t complain = complain_flags (decltype_p);
9287 /* Consume the operator token. */
9288 token = cp_lexer_consume_token (parser->lexer);
9289 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
9291 /* Parse the cast-expression. */
9292 cast_expression
9293 = cp_parser_cast_expression (parser,
9294 unary_operator == ADDR_EXPR,
9295 /*cast_p=*/false,
9296 /*decltype*/false,
9297 pidk);
9299 /* Make a location:
9300 OP_TOKEN CAST_EXPRESSION
9301 ^~~~~~~~~~~~~~~~~~~~~~~~~
9302 with start==caret at the operator token, and
9303 extending to the end of the cast_expression. */
9304 loc = make_location (loc, loc, cast_expression.get_finish ());
9306 /* Now, build an appropriate representation. */
9307 switch (unary_operator)
9309 case INDIRECT_REF:
9310 non_constant_p = NIC_STAR;
9311 expression = build_x_indirect_ref (loc, cast_expression,
9312 RO_UNARY_STAR, NULL_TREE,
9313 complain);
9314 /* TODO: build_x_indirect_ref does not always honor the
9315 location, so ensure it is set. */
9316 expression.set_location (loc);
9317 break;
9319 case ADDR_EXPR:
9320 non_constant_p = NIC_ADDR;
9321 /* Fall through. */
9322 case BIT_NOT_EXPR:
9323 expression = build_x_unary_op (loc, unary_operator,
9324 cast_expression,
9325 NULL_TREE, complain);
9326 /* TODO: build_x_unary_op does not always honor the location,
9327 so ensure it is set. */
9328 expression.set_location (loc);
9329 break;
9331 case PREINCREMENT_EXPR:
9332 case PREDECREMENT_EXPR:
9333 non_constant_p = unary_operator == PREINCREMENT_EXPR
9334 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
9335 /* Fall through. */
9336 case NEGATE_EXPR:
9337 /* Immediately fold negation of a constant, unless the constant is 0
9338 (since -0 == 0) or it would overflow. */
9339 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER)
9341 tree stripped_expr
9342 = tree_strip_any_location_wrapper (cast_expression);
9343 if (CONSTANT_CLASS_P (stripped_expr)
9344 && !integer_zerop (stripped_expr)
9345 && !TREE_OVERFLOW (stripped_expr))
9347 tree folded = fold_build1 (unary_operator,
9348 TREE_TYPE (stripped_expr),
9349 stripped_expr);
9350 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
9352 expression = maybe_wrap_with_location (folded, loc);
9353 break;
9357 /* Fall through. */
9358 case UNARY_PLUS_EXPR:
9359 case TRUTH_NOT_EXPR:
9360 expression = finish_unary_op_expr (loc, unary_operator,
9361 cast_expression, complain);
9362 break;
9364 default:
9365 gcc_unreachable ();
9368 if (non_constant_p != NIC_NONE
9369 && cp_parser_non_integral_constant_expression (parser,
9370 non_constant_p))
9371 expression = error_mark_node;
9373 return expression;
9376 return cp_parser_postfix_expression (parser, address_p, cast_p,
9377 /*member_access_only_p=*/false,
9378 decltype_p,
9379 pidk);
9382 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
9383 unary-operator, the corresponding tree code is returned. */
9385 static enum tree_code
9386 cp_parser_unary_operator (cp_token* token)
9388 switch (token->type)
9390 case CPP_MULT:
9391 return INDIRECT_REF;
9393 case CPP_AND:
9394 return ADDR_EXPR;
9396 case CPP_PLUS:
9397 return UNARY_PLUS_EXPR;
9399 case CPP_MINUS:
9400 return NEGATE_EXPR;
9402 case CPP_NOT:
9403 return TRUTH_NOT_EXPR;
9405 case CPP_COMPL:
9406 return BIT_NOT_EXPR;
9408 default:
9409 return ERROR_MARK;
9413 /* Parse a __builtin_has_attribute([expr|type], attribute-spec) expression.
9414 Returns a representation of the expression. */
9416 static tree
9417 cp_parser_has_attribute_expression (cp_parser *parser)
9419 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9421 /* Consume the __builtin_has_attribute token. */
9422 cp_lexer_consume_token (parser->lexer);
9424 matching_parens parens;
9425 if (!parens.require_open (parser))
9426 return error_mark_node;
9428 /* Types cannot be defined in a `sizeof' expression. Save away the
9429 old message. */
9430 const char *saved_message = parser->type_definition_forbidden_message;
9431 const char *saved_message_arg
9432 = parser->type_definition_forbidden_message_arg;
9433 parser->type_definition_forbidden_message
9434 = G_("types may not be defined in %qs expressions");
9435 parser->type_definition_forbidden_message_arg
9436 = IDENTIFIER_POINTER (ridpointers[RID_BUILTIN_HAS_ATTRIBUTE]);
9438 /* The restrictions on constant-expressions do not apply inside
9439 sizeof expressions. */
9440 bool saved_integral_constant_expression_p
9441 = parser->integral_constant_expression_p;
9442 bool saved_non_integral_constant_expression_p
9443 = parser->non_integral_constant_expression_p;
9444 parser->integral_constant_expression_p = false;
9446 /* Do not actually evaluate the expression. */
9447 ++cp_unevaluated_operand;
9448 ++c_inhibit_evaluation_warnings;
9450 tree oper = NULL_TREE;
9452 /* We can't be sure yet whether we're looking at a type-id or an
9453 expression. */
9454 cp_parser_parse_tentatively (parser);
9456 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9457 parser->in_type_id_in_expr_p = true;
9458 /* Look for the type-id. */
9459 oper = cp_parser_type_id (parser);
9460 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9462 cp_parser_parse_definitely (parser);
9464 /* If the type-id production did not work out, then we must be
9465 looking at an expression. */
9466 if (!oper || oper == error_mark_node)
9467 oper = cp_parser_assignment_expression (parser);
9469 STRIP_ANY_LOCATION_WRAPPER (oper);
9471 /* Go back to evaluating expressions. */
9472 --cp_unevaluated_operand;
9473 --c_inhibit_evaluation_warnings;
9475 /* And restore the old one. */
9476 parser->type_definition_forbidden_message = saved_message;
9477 parser->type_definition_forbidden_message_arg = saved_message_arg;
9478 parser->integral_constant_expression_p
9479 = saved_integral_constant_expression_p;
9480 parser->non_integral_constant_expression_p
9481 = saved_non_integral_constant_expression_p;
9483 /* Consume the comma if it's there. */
9484 if (!cp_parser_require (parser, CPP_COMMA, RT_COMMA))
9486 cp_parser_skip_to_closing_parenthesis (parser, false, false,
9487 /*consume_paren=*/true);
9488 return error_mark_node;
9491 /* Parse the attribute specification. */
9492 bool ret = false;
9493 location_t atloc = cp_lexer_peek_token (parser->lexer)->location;
9494 if (tree attr = cp_parser_gnu_attribute_list (parser, /*exactly_one=*/true))
9496 if (oper == error_mark_node)
9497 /* Nothing. */;
9498 else if (processing_template_decl && uses_template_parms (oper))
9499 sorry_at (atloc, "%<__builtin_has_attribute%> with dependent argument "
9500 "not supported yet");
9501 else
9503 /* Fold constant expressions used in attributes first. */
9504 cp_check_const_attributes (attr);
9506 /* Finally, see if OPER has been declared with ATTR. */
9507 ret = has_attribute (atloc, oper, attr, default_conversion);
9510 parens.require_close (parser);
9512 else
9514 error_at (atloc, "expected identifier");
9515 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9518 /* Construct a location e.g. :
9519 __builtin_has_attribute (oper, attr)
9520 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9521 with start == caret at the start of the built-in token,
9522 and with the endpoint at the final closing paren. */
9523 location_t compound_loc
9524 = make_location (start_loc, start_loc, parser->lexer);
9526 cp_expr ret_expr (ret ? boolean_true_node : boolean_false_node);
9527 ret_expr.set_location (compound_loc);
9528 ret_expr = ret_expr.maybe_add_location_wrapper ();
9529 return ret_expr;
9532 /* Parse a new-expression.
9534 new-expression:
9535 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
9536 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
9538 Returns a representation of the expression. */
9540 static tree
9541 cp_parser_new_expression (cp_parser* parser)
9543 bool global_scope_p;
9544 vec<tree, va_gc> *placement;
9545 tree type;
9546 vec<tree, va_gc> *initializer;
9547 tree nelts = NULL_TREE;
9548 tree ret;
9550 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9552 /* Look for the optional `::' operator. */
9553 global_scope_p
9554 = (cp_parser_global_scope_opt (parser,
9555 /*current_scope_valid_p=*/false)
9556 != NULL_TREE);
9557 /* Look for the `new' operator. */
9558 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
9559 /* There's no easy way to tell a new-placement from the
9560 `( type-id )' construct. */
9561 cp_parser_parse_tentatively (parser);
9562 /* Look for a new-placement. */
9563 placement = cp_parser_new_placement (parser);
9564 /* If that didn't work out, there's no new-placement. */
9565 if (!cp_parser_parse_definitely (parser))
9567 if (placement != NULL)
9568 release_tree_vector (placement);
9569 placement = NULL;
9572 /* If the next token is a `(', then we have a parenthesized
9573 type-id. */
9574 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9576 cp_token *token;
9577 const char *saved_message = parser->type_definition_forbidden_message;
9579 /* Consume the `('. */
9580 matching_parens parens;
9581 parens.consume_open (parser);
9583 /* Parse the type-id. */
9584 parser->type_definition_forbidden_message
9585 = G_("types may not be defined in a new-expression");
9587 type_id_in_expr_sentinel s (parser);
9588 type = cp_parser_type_id (parser);
9590 parser->type_definition_forbidden_message = saved_message;
9592 /* Look for the closing `)'. */
9593 parens.require_close (parser);
9594 token = cp_lexer_peek_token (parser->lexer);
9595 /* There should not be a direct-new-declarator in this production,
9596 but GCC used to allowed this, so we check and emit a sensible error
9597 message for this case. */
9598 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
9600 error_at (token->location,
9601 "array bound forbidden after parenthesized type-id");
9602 inform (token->location,
9603 "try removing the parentheses around the type-id");
9604 cp_parser_direct_new_declarator (parser);
9607 /* Otherwise, there must be a new-type-id. */
9608 else
9609 type = cp_parser_new_type_id (parser, &nelts);
9611 /* If the next token is a `(' or '{', then we have a new-initializer. */
9612 cp_token *token = cp_lexer_peek_token (parser->lexer);
9613 if (token->type == CPP_OPEN_PAREN
9614 || token->type == CPP_OPEN_BRACE)
9615 initializer = cp_parser_new_initializer (parser);
9616 else
9617 initializer = NULL;
9619 /* A new-expression may not appear in an integral constant
9620 expression. */
9621 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
9622 ret = error_mark_node;
9623 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
9624 of a new-type-id or type-id of a new-expression, the new-expression shall
9625 contain a new-initializer of the form ( assignment-expression )".
9626 Additionally, consistently with the spirit of DR 1467, we want to accept
9627 'new auto { 2 }' too. */
9628 else if ((ret = type_uses_auto (type))
9629 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
9630 && (vec_safe_length (initializer) != 1
9631 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
9632 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
9634 error_at (token->location,
9635 "initialization of new-expression for type %<auto%> "
9636 "requires exactly one element");
9637 ret = error_mark_node;
9639 else
9641 /* Construct a location e.g.:
9642 ptr = new int[100]
9643 ^~~~~~~~~~~~
9644 with caret == start at the start of the "new" token, and the end
9645 at the end of the final token we consumed. */
9646 location_t combined_loc = make_location (start_loc, start_loc,
9647 parser->lexer);
9648 /* Create a representation of the new-expression. */
9649 ret = build_new (combined_loc, &placement, type, nelts, &initializer,
9650 global_scope_p, tf_warning_or_error);
9653 if (placement != NULL)
9654 release_tree_vector (placement);
9655 if (initializer != NULL)
9656 release_tree_vector (initializer);
9658 return ret;
9661 /* Parse a new-placement.
9663 new-placement:
9664 ( expression-list )
9666 Returns the same representation as for an expression-list. */
9668 static vec<tree, va_gc> *
9669 cp_parser_new_placement (cp_parser* parser)
9671 vec<tree, va_gc> *expression_list;
9673 /* Parse the expression-list. */
9674 expression_list = (cp_parser_parenthesized_expression_list
9675 (parser, non_attr, /*cast_p=*/false,
9676 /*allow_expansion_p=*/true,
9677 /*non_constant_p=*/NULL));
9679 if (expression_list && expression_list->is_empty ())
9680 error ("expected expression-list or type-id");
9682 return expression_list;
9685 /* Parse a new-type-id.
9687 new-type-id:
9688 type-specifier-seq new-declarator [opt]
9690 Returns the TYPE allocated. If the new-type-id indicates an array
9691 type, *NELTS is set to the number of elements in the last array
9692 bound; the TYPE will not include the last array bound. */
9694 static tree
9695 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
9697 cp_decl_specifier_seq type_specifier_seq;
9698 cp_declarator *new_declarator;
9699 cp_declarator *declarator;
9700 cp_declarator *outer_declarator;
9701 const char *saved_message;
9703 /* The type-specifier sequence must not contain type definitions.
9704 (It cannot contain declarations of new types either, but if they
9705 are not definitions we will catch that because they are not
9706 complete.) */
9707 saved_message = parser->type_definition_forbidden_message;
9708 parser->type_definition_forbidden_message
9709 = G_("types may not be defined in a new-type-id");
9710 /* Parse the type-specifier-seq. */
9711 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
9712 /*is_declaration=*/false,
9713 /*is_trailing_return=*/false,
9714 &type_specifier_seq);
9715 /* Restore the old message. */
9716 parser->type_definition_forbidden_message = saved_message;
9718 if (type_specifier_seq.type == error_mark_node)
9719 return error_mark_node;
9721 /* Parse the new-declarator. */
9722 new_declarator = cp_parser_new_declarator_opt (parser);
9724 /* Determine the number of elements in the last array dimension, if
9725 any. */
9726 *nelts = NULL_TREE;
9727 /* Skip down to the last array dimension. */
9728 declarator = new_declarator;
9729 outer_declarator = NULL;
9730 while (declarator && (declarator->kind == cdk_pointer
9731 || declarator->kind == cdk_ptrmem))
9733 outer_declarator = declarator;
9734 declarator = declarator->declarator;
9736 while (declarator
9737 && declarator->kind == cdk_array
9738 && declarator->declarator
9739 && declarator->declarator->kind == cdk_array)
9741 outer_declarator = declarator;
9742 declarator = declarator->declarator;
9745 if (declarator && declarator->kind == cdk_array)
9747 *nelts = declarator->u.array.bounds;
9748 if (*nelts == error_mark_node)
9749 *nelts = integer_one_node;
9751 if (*nelts == NULL_TREE)
9752 /* Leave [] in the declarator. */;
9753 else if (outer_declarator)
9754 outer_declarator->declarator = declarator->declarator;
9755 else
9756 new_declarator = NULL;
9759 return groktypename (&type_specifier_seq, new_declarator, false);
9762 /* Parse an (optional) new-declarator.
9764 new-declarator:
9765 ptr-operator new-declarator [opt]
9766 direct-new-declarator
9768 Returns the declarator. */
9770 static cp_declarator *
9771 cp_parser_new_declarator_opt (cp_parser* parser)
9773 enum tree_code code;
9774 tree type, std_attributes = NULL_TREE;
9775 cp_cv_quals cv_quals;
9777 /* We don't know if there's a ptr-operator next, or not. */
9778 cp_parser_parse_tentatively (parser);
9779 /* Look for a ptr-operator. */
9780 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
9781 /* If that worked, look for more new-declarators. */
9782 if (cp_parser_parse_definitely (parser))
9784 cp_declarator *declarator;
9786 /* Parse another optional declarator. */
9787 declarator = cp_parser_new_declarator_opt (parser);
9789 declarator = cp_parser_make_indirect_declarator
9790 (code, type, cv_quals, declarator, std_attributes);
9792 return declarator;
9795 /* If the next token is a `[', there is a direct-new-declarator. */
9796 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
9797 return cp_parser_direct_new_declarator (parser);
9799 return NULL;
9802 /* Parse a direct-new-declarator.
9804 direct-new-declarator:
9805 [ expression ]
9806 direct-new-declarator [constant-expression]
9810 static cp_declarator *
9811 cp_parser_direct_new_declarator (cp_parser* parser)
9813 cp_declarator *declarator = NULL;
9814 bool first_p = true;
9816 while (true)
9818 tree expression;
9819 cp_token *token;
9821 /* Look for the opening `['. */
9822 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9824 token = cp_lexer_peek_token (parser->lexer);
9825 if (token->type == CPP_CLOSE_SQUARE && first_p)
9826 expression = NULL_TREE;
9827 else
9828 expression = cp_parser_expression (parser);
9829 /* The standard requires that the expression have integral
9830 type. DR 74 adds enumeration types. We believe that the
9831 real intent is that these expressions be handled like the
9832 expression in a `switch' condition, which also allows
9833 classes with a single conversion to integral or
9834 enumeration type. */
9835 if (expression && !processing_template_decl)
9837 expression
9838 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
9839 expression,
9840 /*complain=*/true);
9841 if (!expression)
9843 error_at (token->location,
9844 "expression in new-declarator must have integral "
9845 "or enumeration type");
9846 expression = error_mark_node;
9850 /* Look for the closing `]'. */
9851 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9853 /* Add this bound to the declarator. */
9854 declarator = make_array_declarator (declarator, expression);
9856 /* If the next token is not a `[', then there are no more
9857 bounds. */
9858 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
9859 break;
9860 first_p = false;
9863 return declarator;
9866 /* Parse a new-initializer.
9868 new-initializer:
9869 ( expression-list [opt] )
9870 braced-init-list
9872 Returns a representation of the expression-list. */
9874 static vec<tree, va_gc> *
9875 cp_parser_new_initializer (cp_parser* parser)
9877 vec<tree, va_gc> *expression_list;
9879 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9881 cp_lexer_set_source_position (parser->lexer);
9882 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9883 tree t = cp_parser_braced_list (parser);
9884 CONSTRUCTOR_IS_DIRECT_INIT (t) = true;
9885 expression_list = make_tree_vector_single (t);
9887 else
9888 expression_list = (cp_parser_parenthesized_expression_list
9889 (parser, non_attr, /*cast_p=*/false,
9890 /*allow_expansion_p=*/true,
9891 /*non_constant_p=*/NULL));
9893 return expression_list;
9896 /* Parse a delete-expression.
9898 delete-expression:
9899 :: [opt] delete cast-expression
9900 :: [opt] delete [ ] cast-expression
9902 Returns a representation of the expression. */
9904 static tree
9905 cp_parser_delete_expression (cp_parser* parser)
9907 bool global_scope_p;
9908 bool array_p;
9909 tree expression;
9910 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9912 /* Look for the optional `::' operator. */
9913 global_scope_p
9914 = (cp_parser_global_scope_opt (parser,
9915 /*current_scope_valid_p=*/false)
9916 != NULL_TREE);
9917 /* Look for the `delete' keyword. */
9918 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
9919 /* See if the array syntax is in use. */
9920 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
9922 /* Consume the `[' token. */
9923 cp_lexer_consume_token (parser->lexer);
9924 /* Look for the `]' token. */
9925 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9926 /* Remember that this is the `[]' construct. */
9927 array_p = true;
9929 else
9930 array_p = false;
9932 /* Parse the cast-expression. */
9933 expression = cp_parser_simple_cast_expression (parser);
9935 /* A delete-expression may not appear in an integral constant
9936 expression. */
9937 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
9938 return error_mark_node;
9940 /* Construct a location e.g.:
9941 delete [ ] ptr
9942 ^~~~~~~~~~~~~~
9943 with caret == start at the start of the "delete" token, and
9944 the end at the end of the final token we consumed. */
9945 location_t combined_loc = make_location (start_loc, start_loc,
9946 parser->lexer);
9947 expression = delete_sanity (combined_loc, expression, NULL_TREE, array_p,
9948 global_scope_p, tf_warning_or_error);
9950 return expression;
9953 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
9954 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
9955 0 otherwise. */
9957 static int
9958 cp_parser_tokens_start_cast_expression (cp_parser *parser)
9960 cp_token *token = cp_lexer_peek_token (parser->lexer);
9961 switch (token->type)
9963 case CPP_COMMA:
9964 case CPP_SEMICOLON:
9965 case CPP_QUERY:
9966 case CPP_COLON:
9967 case CPP_CLOSE_SQUARE:
9968 case CPP_CLOSE_PAREN:
9969 case CPP_CLOSE_BRACE:
9970 case CPP_OPEN_BRACE:
9971 case CPP_DOT:
9972 case CPP_DOT_STAR:
9973 case CPP_DEREF:
9974 case CPP_DEREF_STAR:
9975 case CPP_DIV:
9976 case CPP_MOD:
9977 case CPP_LSHIFT:
9978 case CPP_RSHIFT:
9979 case CPP_LESS:
9980 case CPP_GREATER:
9981 case CPP_LESS_EQ:
9982 case CPP_GREATER_EQ:
9983 case CPP_EQ_EQ:
9984 case CPP_NOT_EQ:
9985 case CPP_EQ:
9986 case CPP_MULT_EQ:
9987 case CPP_DIV_EQ:
9988 case CPP_MOD_EQ:
9989 case CPP_PLUS_EQ:
9990 case CPP_MINUS_EQ:
9991 case CPP_RSHIFT_EQ:
9992 case CPP_LSHIFT_EQ:
9993 case CPP_AND_EQ:
9994 case CPP_XOR_EQ:
9995 case CPP_OR_EQ:
9996 case CPP_XOR:
9997 case CPP_OR:
9998 case CPP_OR_OR:
9999 case CPP_EOF:
10000 case CPP_ELLIPSIS:
10001 return 0;
10003 case CPP_OPEN_PAREN:
10004 /* In ((type ()) () the last () isn't a valid cast-expression,
10005 so the whole must be parsed as postfix-expression. */
10006 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
10007 != CPP_CLOSE_PAREN;
10009 case CPP_OPEN_SQUARE:
10010 /* '[' may start a primary-expression in obj-c++ and in C++11,
10011 as a lambda-expression, eg, '(void)[]{}'. */
10012 if (cxx_dialect >= cxx11)
10013 return -1;
10014 return c_dialect_objc ();
10016 case CPP_PLUS_PLUS:
10017 case CPP_MINUS_MINUS:
10018 /* '++' and '--' may or may not start a cast-expression:
10020 struct T { void operator++(int); };
10021 void f() { (T())++; }
10025 int a;
10026 (int)++a; */
10027 return -1;
10029 default:
10030 return 1;
10034 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
10035 in the order: const_cast, static_cast, reinterpret_cast.
10037 Don't suggest dynamic_cast.
10039 Return the first legal cast kind found, or NULL otherwise. */
10041 static const char *
10042 get_cast_suggestion (tree dst_type, tree orig_expr)
10044 tree trial;
10046 /* Reuse the parser logic by attempting to build the various kinds of
10047 cast, with "complain" disabled.
10048 Identify the first such cast that is valid. */
10050 /* Don't attempt to run such logic within template processing. */
10051 if (processing_template_decl)
10052 return NULL;
10054 /* First try const_cast. */
10055 trial = build_const_cast (input_location, dst_type, orig_expr, tf_none);
10056 if (trial != error_mark_node)
10057 return "const_cast";
10059 /* If that fails, try static_cast. */
10060 trial = build_static_cast (input_location, dst_type, orig_expr, tf_none);
10061 if (trial != error_mark_node)
10062 return "static_cast";
10064 /* Finally, try reinterpret_cast. */
10065 trial = build_reinterpret_cast (input_location, dst_type, orig_expr,
10066 tf_none);
10067 if (trial != error_mark_node)
10068 return "reinterpret_cast";
10070 /* No such cast possible. */
10071 return NULL;
10074 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
10075 suggesting how to convert a C-style cast of the form:
10077 (DST_TYPE)ORIG_EXPR
10079 to a C++-style cast.
10081 The primary range of RICHLOC is asssumed to be that of the original
10082 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
10083 of the parens in the C-style cast. */
10085 static void
10086 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
10087 location_t close_paren_loc, tree orig_expr,
10088 tree dst_type)
10090 /* This function is non-trivial, so bail out now if the warning isn't
10091 going to be emitted. */
10092 if (!warn_old_style_cast)
10093 return;
10095 /* Try to find a legal C++ cast, trying them in order:
10096 const_cast, static_cast, reinterpret_cast. */
10097 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
10098 if (!cast_suggestion)
10099 return;
10101 /* Replace the open paren with "CAST_SUGGESTION<". */
10102 pretty_printer pp;
10103 pp_string (&pp, cast_suggestion);
10104 pp_less (&pp);
10105 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
10107 /* Replace the close paren with "> (". */
10108 rich_loc->add_fixit_replace (close_paren_loc, "> (");
10110 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
10111 rich_loc->add_fixit_insert_after (")");
10115 /* Parse a cast-expression.
10117 cast-expression:
10118 unary-expression
10119 ( type-id ) cast-expression
10121 ADDRESS_P is true iff the unary-expression is appearing as the
10122 operand of the `&' operator. CAST_P is true if this expression is
10123 the target of a cast.
10125 Returns a representation of the expression. */
10127 static cp_expr
10128 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
10129 bool decltype_p, cp_id_kind * pidk)
10131 /* If it's a `(', then we might be looking at a cast. */
10132 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10134 tree type = NULL_TREE;
10135 cp_expr expr (NULL_TREE);
10136 int cast_expression = 0;
10137 const char *saved_message;
10139 /* There's no way to know yet whether or not this is a cast.
10140 For example, `(int (3))' is a unary-expression, while `(int)
10141 3' is a cast. So, we resort to parsing tentatively. */
10142 cp_parser_parse_tentatively (parser);
10143 /* Types may not be defined in a cast. */
10144 saved_message = parser->type_definition_forbidden_message;
10145 parser->type_definition_forbidden_message
10146 = G_("types may not be defined in casts");
10147 /* Consume the `('. */
10148 matching_parens parens;
10149 cp_token *open_paren = parens.consume_open (parser);
10150 location_t open_paren_loc = open_paren->location;
10151 location_t close_paren_loc = UNKNOWN_LOCATION;
10153 /* A very tricky bit is that `(struct S) { 3 }' is a
10154 compound-literal (which we permit in C++ as an extension).
10155 But, that construct is not a cast-expression -- it is a
10156 postfix-expression. (The reason is that `(struct S) { 3 }.i'
10157 is legal; if the compound-literal were a cast-expression,
10158 you'd need an extra set of parentheses.) But, if we parse
10159 the type-id, and it happens to be a class-specifier, then we
10160 will commit to the parse at that point, because we cannot
10161 undo the action that is done when creating a new class. So,
10162 then we cannot back up and do a postfix-expression.
10164 Another tricky case is the following (c++/29234):
10166 struct S { void operator () (); };
10168 void foo ()
10170 ( S()() );
10173 As a type-id we parse the parenthesized S()() as a function
10174 returning a function, groktypename complains and we cannot
10175 back up in this case either.
10177 Therefore, we scan ahead to the closing `)', and check to see
10178 if the tokens after the `)' can start a cast-expression. Otherwise
10179 we are dealing with an unary-expression, a postfix-expression
10180 or something else.
10182 Yet another tricky case, in C++11, is the following (c++/54891):
10184 (void)[]{};
10186 The issue is that usually, besides the case of lambda-expressions,
10187 the parenthesized type-id cannot be followed by '[', and, eg, we
10188 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
10189 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
10190 we don't commit, we try a cast-expression, then an unary-expression.
10192 Save tokens so that we can put them back. */
10193 cp_lexer_save_tokens (parser->lexer);
10195 /* We may be looking at a cast-expression. */
10196 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
10197 /*consume_paren=*/true))
10198 cast_expression
10199 = cp_parser_tokens_start_cast_expression (parser);
10201 /* Roll back the tokens we skipped. */
10202 cp_lexer_rollback_tokens (parser->lexer);
10203 /* If we aren't looking at a cast-expression, simulate an error so
10204 that the call to cp_parser_error_occurred below returns true. */
10205 if (!cast_expression)
10206 cp_parser_simulate_error (parser);
10207 else
10209 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
10210 parser->in_type_id_in_expr_p = true;
10211 /* Look for the type-id. */
10212 type = cp_parser_type_id (parser);
10213 /* Look for the closing `)'. */
10214 cp_token *close_paren = parens.require_close (parser);
10215 if (close_paren)
10216 close_paren_loc = close_paren->location;
10217 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
10220 /* Restore the saved message. */
10221 parser->type_definition_forbidden_message = saved_message;
10223 /* At this point this can only be either a cast or a
10224 parenthesized ctor such as `(T ())' that looks like a cast to
10225 function returning T. */
10226 if (!cp_parser_error_occurred (parser))
10228 /* Only commit if the cast-expression doesn't start with
10229 '++', '--', or '[' in C++11. */
10230 if (cast_expression > 0)
10231 cp_parser_commit_to_topmost_tentative_parse (parser);
10233 expr = cp_parser_cast_expression (parser,
10234 /*address_p=*/false,
10235 /*cast_p=*/true,
10236 /*decltype_p=*/false,
10237 pidk);
10239 if (cp_parser_parse_definitely (parser))
10241 /* Warn about old-style casts, if so requested. */
10242 if (warn_old_style_cast
10243 && !in_system_header_at (input_location)
10244 && !VOID_TYPE_P (type)
10245 && current_lang_name != lang_name_c)
10247 gcc_rich_location rich_loc (input_location);
10248 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
10249 expr, type);
10250 warning_at (&rich_loc, OPT_Wold_style_cast,
10251 "use of old-style cast to %q#T", type);
10254 /* Only type conversions to integral or enumeration types
10255 can be used in constant-expressions. */
10256 if (!cast_valid_in_integral_constant_expression_p (type)
10257 && cp_parser_non_integral_constant_expression (parser,
10258 NIC_CAST))
10259 return error_mark_node;
10261 /* Perform the cast. */
10262 /* Make a location:
10263 (TYPE) EXPR
10264 ^~~~~~~~~~~
10265 with start==caret at the open paren, extending to the
10266 end of "expr". */
10267 location_t cast_loc = make_location (open_paren_loc,
10268 open_paren_loc,
10269 expr.get_finish ());
10270 expr = build_c_cast (cast_loc, type, expr);
10271 return expr;
10274 else
10275 cp_parser_abort_tentative_parse (parser);
10278 /* If we get here, then it's not a cast, so it must be a
10279 unary-expression. */
10280 return cp_parser_unary_expression (parser, pidk, address_p,
10281 cast_p, decltype_p);
10284 /* Parse a binary expression of the general form:
10286 pm-expression:
10287 cast-expression
10288 pm-expression .* cast-expression
10289 pm-expression ->* cast-expression
10291 multiplicative-expression:
10292 pm-expression
10293 multiplicative-expression * pm-expression
10294 multiplicative-expression / pm-expression
10295 multiplicative-expression % pm-expression
10297 additive-expression:
10298 multiplicative-expression
10299 additive-expression + multiplicative-expression
10300 additive-expression - multiplicative-expression
10302 shift-expression:
10303 additive-expression
10304 shift-expression << additive-expression
10305 shift-expression >> additive-expression
10307 relational-expression:
10308 shift-expression
10309 relational-expression < shift-expression
10310 relational-expression > shift-expression
10311 relational-expression <= shift-expression
10312 relational-expression >= shift-expression
10314 GNU Extension:
10316 relational-expression:
10317 relational-expression <? shift-expression
10318 relational-expression >? shift-expression
10320 equality-expression:
10321 relational-expression
10322 equality-expression == relational-expression
10323 equality-expression != relational-expression
10325 and-expression:
10326 equality-expression
10327 and-expression & equality-expression
10329 exclusive-or-expression:
10330 and-expression
10331 exclusive-or-expression ^ and-expression
10333 inclusive-or-expression:
10334 exclusive-or-expression
10335 inclusive-or-expression | exclusive-or-expression
10337 logical-and-expression:
10338 inclusive-or-expression
10339 logical-and-expression && inclusive-or-expression
10341 logical-or-expression:
10342 logical-and-expression
10343 logical-or-expression || logical-and-expression
10345 All these are implemented with a single function like:
10347 binary-expression:
10348 simple-cast-expression
10349 binary-expression <token> binary-expression
10351 CAST_P is true if this expression is the target of a cast.
10353 The binops_by_token map is used to get the tree codes for each <token> type.
10354 binary-expressions are associated according to a precedence table. */
10356 #define TOKEN_PRECEDENCE(token) \
10357 (((token->type == CPP_GREATER \
10358 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
10359 && !parser->greater_than_is_operator_p) \
10360 ? PREC_NOT_OPERATOR \
10361 : binops_by_token[token->type].prec)
10363 static cp_expr
10364 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
10365 bool no_toplevel_fold_p,
10366 bool decltype_p,
10367 enum cp_parser_prec prec,
10368 cp_id_kind * pidk)
10370 cp_parser_expression_stack stack;
10371 cp_parser_expression_stack_entry *sp = &stack[0];
10372 cp_parser_expression_stack_entry *disable_warnings_sp = NULL;
10373 cp_parser_expression_stack_entry current;
10374 cp_expr rhs;
10375 cp_token *token;
10376 enum tree_code rhs_type;
10377 enum cp_parser_prec new_prec, lookahead_prec;
10378 tree overload;
10380 /* Parse the first expression. */
10381 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
10382 ? TRUTH_NOT_EXPR : ERROR_MARK);
10383 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
10384 cast_p, decltype_p, pidk);
10385 current.prec = prec;
10387 if (cp_parser_error_occurred (parser))
10388 return error_mark_node;
10390 for (;;)
10392 /* Get an operator token. */
10393 token = cp_lexer_peek_token (parser->lexer);
10395 if (warn_cxx11_compat
10396 && token->type == CPP_RSHIFT
10397 && !parser->greater_than_is_operator_p)
10399 if (warning_at (token->location, OPT_Wc__11_compat,
10400 "%<>>%> operator is treated"
10401 " as two right angle brackets in C++11"))
10402 inform (token->location,
10403 "suggest parentheses around %<>>%> expression");
10406 new_prec = TOKEN_PRECEDENCE (token);
10407 if (new_prec != PREC_NOT_OPERATOR
10408 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10409 /* This is a fold-expression; handle it later. */
10410 new_prec = PREC_NOT_OPERATOR;
10412 /* Popping an entry off the stack means we completed a subexpression:
10413 - either we found a token which is not an operator (`>' where it is not
10414 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
10415 will happen repeatedly;
10416 - or, we found an operator which has lower priority. This is the case
10417 where the recursive descent *ascends*, as in `3 * 4 + 5' after
10418 parsing `3 * 4'. */
10419 if (new_prec <= current.prec)
10421 if (sp == stack)
10422 break;
10423 else
10424 goto pop;
10427 get_rhs:
10428 current.tree_type = binops_by_token[token->type].tree_type;
10429 current.loc = token->location;
10430 current.flags = token->flags;
10432 /* We used the operator token. */
10433 cp_lexer_consume_token (parser->lexer);
10435 /* For "false && x" or "true || x", x will never be executed;
10436 disable warnings while evaluating it. */
10437 if ((current.tree_type == TRUTH_ANDIF_EXPR
10438 && cp_fully_fold (current.lhs) == truthvalue_false_node)
10439 || (current.tree_type == TRUTH_ORIF_EXPR
10440 && cp_fully_fold (current.lhs) == truthvalue_true_node))
10442 disable_warnings_sp = sp;
10443 ++c_inhibit_evaluation_warnings;
10446 /* Extract another operand. It may be the RHS of this expression
10447 or the LHS of a new, higher priority expression. */
10448 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
10449 ? TRUTH_NOT_EXPR : ERROR_MARK);
10450 rhs = cp_parser_simple_cast_expression (parser);
10452 /* Get another operator token. Look up its precedence to avoid
10453 building a useless (immediately popped) stack entry for common
10454 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
10455 token = cp_lexer_peek_token (parser->lexer);
10456 lookahead_prec = TOKEN_PRECEDENCE (token);
10457 if (lookahead_prec != PREC_NOT_OPERATOR
10458 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10459 lookahead_prec = PREC_NOT_OPERATOR;
10460 if (lookahead_prec > new_prec)
10462 /* ... and prepare to parse the RHS of the new, higher priority
10463 expression. Since precedence levels on the stack are
10464 monotonically increasing, we do not have to care about
10465 stack overflows. */
10466 *sp = current;
10467 ++sp;
10468 current.lhs = rhs;
10469 current.lhs_type = rhs_type;
10470 current.prec = new_prec;
10471 new_prec = lookahead_prec;
10472 goto get_rhs;
10474 pop:
10475 lookahead_prec = new_prec;
10476 /* If the stack is not empty, we have parsed into LHS the right side
10477 (`4' in the example above) of an expression we had suspended.
10478 We can use the information on the stack to recover the LHS (`3')
10479 from the stack together with the tree code (`MULT_EXPR'), and
10480 the precedence of the higher level subexpression
10481 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
10482 which will be used to actually build the additive expression. */
10483 rhs = current.lhs;
10484 rhs_type = current.lhs_type;
10485 --sp;
10486 current = *sp;
10489 /* Undo the disabling of warnings done above. */
10490 if (sp == disable_warnings_sp)
10492 disable_warnings_sp = NULL;
10493 --c_inhibit_evaluation_warnings;
10496 if (warn_logical_not_paren
10497 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
10498 && current.lhs_type == TRUTH_NOT_EXPR
10499 /* Avoid warning for !!x == y. */
10500 && (TREE_CODE (current.lhs) != NE_EXPR
10501 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
10502 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
10503 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
10504 /* Avoid warning for !b == y where b is boolean. */
10505 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
10506 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
10507 != BOOLEAN_TYPE))))
10508 /* Avoid warning for !!b == y where b is boolean. */
10509 && (!(DECL_P (tree_strip_any_location_wrapper (current.lhs))
10510 || (TREE_CODE (current.lhs) == NON_LVALUE_EXPR
10511 && DECL_P (tree_strip_any_location_wrapper
10512 (TREE_OPERAND (current.lhs, 0)))))
10513 || TREE_TYPE (current.lhs) == NULL_TREE
10514 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
10515 warn_logical_not_parentheses (current.loc, current.tree_type,
10516 current.lhs, maybe_constant_value (rhs));
10518 if (warn_xor_used_as_pow
10519 && current.tree_type == BIT_XOR_EXPR
10520 /* Don't warn for named "xor" (as opposed to '^'). */
10521 && !(current.flags & NAMED_OP)
10522 && current.lhs.decimal_p ()
10523 && rhs.decimal_p ())
10524 check_for_xor_used_as_pow
10525 (current.lhs.get_location (),
10526 tree_strip_any_location_wrapper (current.lhs),
10527 current.loc,
10528 rhs.get_location (),
10529 tree_strip_any_location_wrapper (rhs));
10531 overload = NULL;
10533 location_t combined_loc = make_location (current.loc,
10534 current.lhs.get_start (),
10535 rhs.get_finish ());
10537 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
10538 ERROR_MARK for everything that is not a binary expression.
10539 This makes warn_about_parentheses miss some warnings that
10540 involve unary operators. For unary expressions we should
10541 pass the correct tree_code unless the unary expression was
10542 surrounded by parentheses.
10544 if (no_toplevel_fold_p
10545 && lookahead_prec <= current.prec
10546 && sp == stack)
10548 if (current.lhs == error_mark_node || rhs == error_mark_node)
10549 current.lhs = error_mark_node;
10550 else
10552 current.lhs.maybe_add_location_wrapper ();
10553 rhs.maybe_add_location_wrapper ();
10554 current.lhs
10555 = build_min (current.tree_type,
10556 TREE_CODE_CLASS (current.tree_type)
10557 == tcc_comparison
10558 ? boolean_type_node : TREE_TYPE (current.lhs),
10559 current.lhs.get_value (), rhs.get_value ());
10560 SET_EXPR_LOCATION (current.lhs, combined_loc);
10563 else
10565 op_location_t op_loc (current.loc, combined_loc);
10566 current.lhs = build_x_binary_op (op_loc, current.tree_type,
10567 current.lhs, current.lhs_type,
10568 rhs, rhs_type, NULL_TREE, &overload,
10569 complain_flags (decltype_p));
10570 /* TODO: build_x_binary_op doesn't always honor the location. */
10571 current.lhs.set_location (combined_loc);
10573 current.lhs_type = current.tree_type;
10575 /* If the binary operator required the use of an overloaded operator,
10576 then this expression cannot be an integral constant-expression.
10577 An overloaded operator can be used even if both operands are
10578 otherwise permissible in an integral constant-expression if at
10579 least one of the operands is of enumeration type. */
10581 if (overload
10582 && cp_parser_non_integral_constant_expression (parser,
10583 NIC_OVERLOADED))
10584 return error_mark_node;
10587 return current.lhs;
10590 static cp_expr
10591 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
10592 bool no_toplevel_fold_p,
10593 enum cp_parser_prec prec,
10594 cp_id_kind * pidk)
10596 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
10597 /*decltype*/false, prec, pidk);
10600 /* Parse the `? expression : assignment-expression' part of a
10601 conditional-expression. The LOGICAL_OR_EXPR is the
10602 logical-or-expression that started the conditional-expression.
10603 Returns a representation of the entire conditional-expression.
10605 This routine is used by cp_parser_assignment_expression
10606 and cp_parser_conditional_expression.
10608 ? expression : assignment-expression
10610 GNU Extensions:
10612 ? : assignment-expression */
10614 static tree
10615 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
10617 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
10618 cp_expr assignment_expr;
10619 struct cp_token *token;
10620 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10622 /* Consume the `?' token. */
10623 cp_lexer_consume_token (parser->lexer);
10624 token = cp_lexer_peek_token (parser->lexer);
10625 if (cp_parser_allow_gnu_extensions_p (parser)
10626 && token->type == CPP_COLON)
10628 pedwarn (token->location, OPT_Wpedantic,
10629 "ISO C++ does not allow %<?:%> with omitted middle operand");
10630 /* Implicit true clause. */
10631 expr = NULL_TREE;
10632 c_inhibit_evaluation_warnings +=
10633 folded_logical_or_expr == truthvalue_true_node;
10634 warn_for_omitted_condop (token->location, logical_or_expr);
10636 else
10638 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10639 parser->colon_corrects_to_scope_p = false;
10640 /* Parse the expression. */
10641 c_inhibit_evaluation_warnings +=
10642 folded_logical_or_expr == truthvalue_false_node;
10643 expr = cp_parser_expression (parser);
10644 c_inhibit_evaluation_warnings +=
10645 ((folded_logical_or_expr == truthvalue_true_node)
10646 - (folded_logical_or_expr == truthvalue_false_node));
10647 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10650 /* The next token should be a `:'. */
10651 cp_parser_require (parser, CPP_COLON, RT_COLON);
10652 /* Parse the assignment-expression. */
10653 assignment_expr = cp_parser_assignment_expression (parser);
10654 c_inhibit_evaluation_warnings -=
10655 folded_logical_or_expr == truthvalue_true_node;
10657 /* Make a location:
10658 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
10659 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
10660 with the caret at the "?", ranging from the start of
10661 the logical_or_expr to the end of the assignment_expr. */
10662 loc = make_location (loc,
10663 logical_or_expr.get_start (),
10664 assignment_expr.get_finish ());
10666 /* Build the conditional-expression. */
10667 return build_x_conditional_expr (loc, logical_or_expr,
10668 expr,
10669 assignment_expr,
10670 tf_warning_or_error);
10673 /* Parse a conditional-expression.
10675 conditional-expression:
10676 logical-or-expression
10677 logical-or-expression ? expression : assignment-expression
10679 GNU Extensions:
10681 logical-or-expression ? : assignment-expression */
10683 static cp_expr
10684 cp_parser_conditional_expression (cp_parser *parser)
10686 cp_expr expr = cp_parser_binary_expression (parser, false, false, false,
10687 PREC_NOT_OPERATOR, NULL);
10688 /* If the next token is a `?' then we're actually looking at
10689 a conditional-expression; otherwise we're done. */
10690 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
10691 return cp_parser_question_colon_clause (parser, expr);
10692 return expr;
10695 /* Parse an assignment-expression.
10697 assignment-expression:
10698 conditional-expression
10699 logical-or-expression assignment-operator assignment_expression
10700 throw-expression
10701 yield-expression
10703 CAST_P is true if this expression is the target of a cast.
10704 DECLTYPE_P is true if this expression is the operand of decltype.
10706 Returns a representation for the expression. */
10708 static cp_expr
10709 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
10710 bool cast_p, bool decltype_p)
10712 cp_expr expr;
10714 /* If the next token is the `throw' keyword, then we're looking at
10715 a throw-expression. */
10716 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
10717 expr = cp_parser_throw_expression (parser);
10718 /* If the next token is the `co_yield' keyword, then we're looking at
10719 a yield-expression. */
10720 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CO_YIELD))
10721 expr = cp_parser_yield_expression (parser);
10722 /* Otherwise, it must be that we are looking at a
10723 logical-or-expression. */
10724 else
10726 /* Parse the binary expressions (logical-or-expression). */
10727 expr = cp_parser_binary_expression (parser, cast_p, false,
10728 decltype_p,
10729 PREC_NOT_OPERATOR, pidk);
10730 /* If the next token is a `?' then we're actually looking at a
10731 conditional-expression. */
10732 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
10733 return cp_parser_question_colon_clause (parser, expr);
10734 else
10736 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10738 /* If it's an assignment-operator, we're using the second
10739 production. */
10740 enum tree_code assignment_operator
10741 = cp_parser_assignment_operator_opt (parser);
10742 if (assignment_operator != ERROR_MARK)
10744 /* Parse the right-hand side of the assignment. */
10745 cp_expr rhs = cp_parser_initializer_clause (parser);
10747 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
10748 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10750 /* An assignment may not appear in a
10751 constant-expression. */
10752 if (cp_parser_non_integral_constant_expression (parser,
10753 NIC_ASSIGNMENT))
10754 return error_mark_node;
10755 /* Build the assignment expression. Its default
10756 location:
10757 LHS = RHS
10758 ~~~~^~~~~
10759 is the location of the '=' token as the
10760 caret, ranging from the start of the lhs to the
10761 end of the rhs. */
10762 loc = make_location (loc,
10763 expr.get_start (),
10764 rhs.get_finish ());
10765 expr = build_x_modify_expr (loc, expr,
10766 assignment_operator,
10767 rhs, NULL_TREE,
10768 complain_flags (decltype_p));
10769 /* TODO: build_x_modify_expr doesn't honor the location,
10770 so we must set it here. */
10771 expr.set_location (loc);
10776 return expr;
10779 /* Parse an (optional) assignment-operator.
10781 assignment-operator: one of
10782 = *= /= %= += -= >>= <<= &= ^= |=
10784 GNU Extension:
10786 assignment-operator: one of
10787 <?= >?=
10789 If the next token is an assignment operator, the corresponding tree
10790 code is returned, and the token is consumed. For example, for
10791 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
10792 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
10793 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
10794 operator, ERROR_MARK is returned. */
10796 static enum tree_code
10797 cp_parser_assignment_operator_opt (cp_parser* parser)
10799 enum tree_code op;
10800 cp_token *token;
10802 /* Peek at the next token. */
10803 token = cp_lexer_peek_token (parser->lexer);
10805 switch (token->type)
10807 case CPP_EQ:
10808 op = NOP_EXPR;
10809 break;
10811 case CPP_MULT_EQ:
10812 op = MULT_EXPR;
10813 break;
10815 case CPP_DIV_EQ:
10816 op = TRUNC_DIV_EXPR;
10817 break;
10819 case CPP_MOD_EQ:
10820 op = TRUNC_MOD_EXPR;
10821 break;
10823 case CPP_PLUS_EQ:
10824 op = PLUS_EXPR;
10825 break;
10827 case CPP_MINUS_EQ:
10828 op = MINUS_EXPR;
10829 break;
10831 case CPP_RSHIFT_EQ:
10832 op = RSHIFT_EXPR;
10833 break;
10835 case CPP_LSHIFT_EQ:
10836 op = LSHIFT_EXPR;
10837 break;
10839 case CPP_AND_EQ:
10840 op = BIT_AND_EXPR;
10841 break;
10843 case CPP_XOR_EQ:
10844 op = BIT_XOR_EXPR;
10845 break;
10847 case CPP_OR_EQ:
10848 op = BIT_IOR_EXPR;
10849 break;
10851 default:
10852 /* Nothing else is an assignment operator. */
10853 op = ERROR_MARK;
10856 /* An operator followed by ... is a fold-expression, handled elsewhere. */
10857 if (op != ERROR_MARK
10858 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10859 op = ERROR_MARK;
10861 /* If it was an assignment operator, consume it. */
10862 if (op != ERROR_MARK)
10863 cp_lexer_consume_token (parser->lexer);
10865 return op;
10868 /* Parse an expression.
10870 expression:
10871 assignment-expression
10872 expression , assignment-expression
10874 CAST_P is true if this expression is the target of a cast.
10875 DECLTYPE_P is true if this expression is the immediate operand of decltype,
10876 except possibly parenthesized or on the RHS of a comma (N3276).
10877 WARN_COMMA_P is true if a comma should be diagnosed.
10879 Returns a representation of the expression. */
10881 static cp_expr
10882 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
10883 bool cast_p, bool decltype_p, bool warn_comma_p)
10885 cp_expr expression = NULL_TREE;
10886 location_t loc = UNKNOWN_LOCATION;
10888 while (true)
10890 cp_expr assignment_expression;
10892 /* Parse the next assignment-expression. */
10893 assignment_expression
10894 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
10896 /* We don't create a temporary for a call that is the immediate operand
10897 of decltype or on the RHS of a comma. But when we see a comma, we
10898 need to create a temporary for a call on the LHS. */
10899 if (decltype_p && !processing_template_decl
10900 && TREE_CODE (assignment_expression) == CALL_EXPR
10901 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
10902 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10903 assignment_expression
10904 = build_cplus_new (TREE_TYPE (assignment_expression),
10905 assignment_expression, tf_warning_or_error);
10907 /* If this is the first assignment-expression, we can just
10908 save it away. */
10909 if (!expression)
10910 expression = assignment_expression;
10911 else
10913 /* Create a location with caret at the comma, ranging
10914 from the start of the LHS to the end of the RHS. */
10915 loc = make_location (loc,
10916 expression.get_start (),
10917 assignment_expression.get_finish ());
10918 expression = build_x_compound_expr (loc, expression,
10919 assignment_expression, NULL_TREE,
10920 complain_flags (decltype_p));
10921 expression.set_location (loc);
10923 /* If the next token is not a comma, or we're in a fold-expression, then
10924 we are done with the expression. */
10925 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
10926 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10927 break;
10928 /* Consume the `,'. */
10929 loc = cp_lexer_peek_token (parser->lexer)->location;
10930 if (warn_comma_p)
10932 /* [depr.comma.subscript]: A comma expression appearing as
10933 the expr-or-braced-init-list of a subscripting expression
10934 is deprecated. A parenthesized comma expression is not
10935 deprecated. */
10936 warning_at (loc, OPT_Wcomma_subscript,
10937 "top-level comma expression in array subscript "
10938 "is deprecated");
10939 warn_comma_p = false;
10941 cp_lexer_consume_token (parser->lexer);
10942 /* A comma operator cannot appear in a constant-expression. */
10943 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
10944 expression = error_mark_node;
10947 return expression;
10950 /* Parse a constant-expression.
10952 constant-expression:
10953 conditional-expression
10955 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
10956 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
10957 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
10958 is false, NON_CONSTANT_P should be NULL. If ALLOW_NON_CONSTANT_P is
10959 greater than 1, this isn't really a constant-expression, only a
10960 potentially constant-evaluated expression. If STRICT_P is true,
10961 only parse a conditional-expression, otherwise parse an
10962 assignment-expression. See below for rationale. */
10964 static cp_expr
10965 cp_parser_constant_expression (cp_parser* parser,
10966 int allow_non_constant_p /* = 0 */,
10967 bool *non_constant_p /* = NULL */,
10968 bool strict_p /* = false */)
10970 /* It might seem that we could simply parse the
10971 conditional-expression, and then check to see if it were
10972 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
10973 one that the compiler can figure out is constant, possibly after
10974 doing some simplifications or optimizations. The standard has a
10975 precise definition of constant-expression, and we must honor
10976 that, even though it is somewhat more restrictive.
10978 For example:
10980 int i[(2, 3)];
10982 is not a legal declaration, because `(2, 3)' is not a
10983 constant-expression. The `,' operator is forbidden in a
10984 constant-expression. However, GCC's constant-folding machinery
10985 will fold this operation to an INTEGER_CST for `3'. */
10987 /* Save the old settings. */
10988 bool saved_integral_constant_expression_p
10989 = parser->integral_constant_expression_p;
10990 bool saved_allow_non_integral_constant_expression_p
10991 = parser->allow_non_integral_constant_expression_p;
10992 bool saved_non_integral_constant_expression_p
10993 = parser->non_integral_constant_expression_p;
10994 /* We are now parsing a constant-expression. */
10995 parser->integral_constant_expression_p = true;
10996 parser->allow_non_integral_constant_expression_p
10997 = (allow_non_constant_p || cxx_dialect >= cxx11);
10998 parser->non_integral_constant_expression_p = false;
11000 /* A manifestly constant-evaluated expression is evaluated even in an
11001 unevaluated operand. */
11002 cp_evaluated ev (/*reset if*/allow_non_constant_p <= 1);
11004 /* Although the grammar says "conditional-expression", when not STRICT_P,
11005 we parse an "assignment-expression", which also permits
11006 "throw-expression" and the use of assignment operators. In the case
11007 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
11008 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
11009 actually essential that we look for an assignment-expression.
11010 For example, cp_parser_initializer_clauses uses this function to
11011 determine whether a particular assignment-expression is in fact
11012 constant. */
11013 cp_expr expression;
11014 if (strict_p)
11015 expression = cp_parser_conditional_expression (parser);
11016 else
11017 expression = cp_parser_assignment_expression (parser);
11018 /* Restore the old settings. */
11019 parser->integral_constant_expression_p
11020 = saved_integral_constant_expression_p;
11021 parser->allow_non_integral_constant_expression_p
11022 = saved_allow_non_integral_constant_expression_p;
11023 if (cxx_dialect >= cxx11
11024 && (!allow_non_constant_p || non_constant_p))
11026 /* Require an rvalue constant expression here; that's what our
11027 callers expect. Reference constant expressions are handled
11028 separately in e.g. cp_parser_template_argument. */
11029 tree decay = expression;
11030 if (TREE_TYPE (expression)
11031 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
11032 decay = build_address (expression);
11033 bool is_const = is_rvalue_constant_expression (decay);
11034 parser->non_integral_constant_expression_p = !is_const;
11035 if (!is_const && !allow_non_constant_p)
11036 require_rvalue_constant_expression (decay);
11038 if (allow_non_constant_p && non_constant_p)
11039 *non_constant_p = parser->non_integral_constant_expression_p;
11040 parser->non_integral_constant_expression_p
11041 = saved_non_integral_constant_expression_p;
11043 return expression;
11046 /* Parse __builtin_offsetof.
11048 offsetof-expression:
11049 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
11051 offsetof-member-designator:
11052 id-expression
11053 | offsetof-member-designator "." id-expression
11054 | offsetof-member-designator "[" expression "]"
11055 | offsetof-member-designator "->" id-expression */
11057 static cp_expr
11058 cp_parser_builtin_offsetof (cp_parser *parser)
11060 int save_ice_p, save_non_ice_p;
11061 tree type;
11062 cp_expr expr;
11063 cp_id_kind dummy;
11064 cp_token *token;
11065 location_t finish_loc;
11067 /* We're about to accept non-integral-constant things, but will
11068 definitely yield an integral constant expression. Save and
11069 restore these values around our local parsing. */
11070 save_ice_p = parser->integral_constant_expression_p;
11071 save_non_ice_p = parser->non_integral_constant_expression_p;
11073 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
11075 /* Consume the "__builtin_offsetof" token. */
11076 cp_lexer_consume_token (parser->lexer);
11077 /* Consume the opening `('. */
11078 matching_parens parens;
11079 parens.require_open (parser);
11080 /* Parse the type-id. */
11081 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11083 const char *saved_message = parser->type_definition_forbidden_message;
11084 parser->type_definition_forbidden_message
11085 = G_("types may not be defined within %<__builtin_offsetof%>");
11086 type = cp_parser_type_id (parser);
11087 parser->type_definition_forbidden_message = saved_message;
11089 /* Look for the `,'. */
11090 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
11091 token = cp_lexer_peek_token (parser->lexer);
11093 /* Build the (type *)null that begins the traditional offsetof macro. */
11094 tree object_ptr
11095 = build_static_cast (input_location, build_pointer_type (type),
11096 null_pointer_node, tf_warning_or_error);
11098 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
11099 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
11100 true, &dummy, token->location);
11101 while (true)
11103 token = cp_lexer_peek_token (parser->lexer);
11104 switch (token->type)
11106 case CPP_OPEN_SQUARE:
11107 /* offsetof-member-designator "[" expression "]" */
11108 expr = cp_parser_postfix_open_square_expression (parser, expr,
11109 true, false);
11110 break;
11112 case CPP_DEREF:
11113 /* offsetof-member-designator "->" identifier */
11114 expr = grok_array_decl (token->location, expr, integer_zero_node,
11115 NULL, tf_warning_or_error);
11116 /* FALLTHRU */
11118 case CPP_DOT:
11119 /* offsetof-member-designator "." identifier */
11120 cp_lexer_consume_token (parser->lexer);
11121 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
11122 expr, true, &dummy,
11123 token->location);
11124 break;
11126 case CPP_CLOSE_PAREN:
11127 /* Consume the ")" token. */
11128 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
11129 cp_lexer_consume_token (parser->lexer);
11130 goto success;
11132 default:
11133 /* Error. We know the following require will fail, but
11134 that gives the proper error message. */
11135 parens.require_close (parser);
11136 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
11137 expr = error_mark_node;
11138 goto failure;
11142 success:
11143 /* Make a location of the form:
11144 __builtin_offsetof (struct s, f)
11145 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
11146 with caret at the type-id, ranging from the start of the
11147 "_builtin_offsetof" token to the close paren. */
11148 loc = make_location (loc, start_loc, finish_loc);
11149 /* The result will be an INTEGER_CST, so we need to explicitly
11150 preserve the location. */
11151 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
11153 failure:
11154 parser->integral_constant_expression_p = save_ice_p;
11155 parser->non_integral_constant_expression_p = save_non_ice_p;
11157 expr = expr.maybe_add_location_wrapper ();
11158 return expr;
11161 /* Parse a builtin trait expression or type. */
11163 static cp_expr
11164 cp_parser_trait (cp_parser* parser, const cp_trait* trait)
11166 const cp_trait_kind kind = trait->kind;
11167 tree type1, type2 = NULL_TREE;
11168 const bool binary = (trait->arity == 2);
11169 const bool variadic = (trait->arity == -1);
11170 const bool type = trait->type;
11172 /* Get location of initial token. */
11173 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
11175 /* Consume the token. */
11176 cp_lexer_consume_token (parser->lexer);
11178 matching_parens parens;
11179 if (kind == CPTK_TYPE_PACK_ELEMENT)
11180 cp_parser_require (parser, CPP_LESS, RT_LESS);
11181 else
11182 parens.require_open (parser);
11184 if (kind == CPTK_IS_DEDUCIBLE)
11186 const cp_token* token = cp_lexer_peek_token (parser->lexer);
11187 type1 = cp_parser_id_expression (parser,
11188 /*template_keyword_p=*/false,
11189 /*check_dependency_p=*/true,
11190 nullptr,
11191 /*declarator_p=*/false,
11192 /*optional_p=*/false);
11193 type1 = cp_parser_lookup_name_simple (parser, type1, token->location);
11195 else if (kind == CPTK_TYPE_PACK_ELEMENT)
11196 /* __type_pack_element takes an expression as its first argument and uses
11197 template-id syntax instead of function call syntax (for consistency
11198 with Clang). We special case these properties of __type_pack_element
11199 here and elsewhere. */
11200 type1 = cp_parser_constant_expression (parser);
11201 else
11203 type_id_in_expr_sentinel s (parser);
11204 type1 = cp_parser_type_id (parser);
11207 if (type1 == error_mark_node)
11208 return error_mark_node;
11210 if (kind == CPTK_TYPE_PACK_ELEMENT)
11212 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
11213 tree trailing = cp_parser_enclosed_template_argument_list (parser);
11214 for (tree elt : tree_vec_range (trailing))
11216 if (!TYPE_P (elt))
11218 error_at (cp_expr_loc_or_input_loc (elt),
11219 "trailing argument to %<__type_pack_element%> "
11220 "is not a type");
11221 return error_mark_node;
11224 type2 = trailing;
11226 else if (binary)
11228 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
11231 type_id_in_expr_sentinel s (parser);
11232 type2 = cp_parser_type_id (parser);
11235 if (type2 == error_mark_node)
11236 return error_mark_node;
11238 else if (variadic)
11240 auto_vec<tree, 4> trailing;
11241 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11243 cp_lexer_consume_token (parser->lexer);
11244 tree elt = cp_parser_type_id (parser);
11245 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11247 cp_lexer_consume_token (parser->lexer);
11248 elt = make_pack_expansion (elt);
11250 if (elt == error_mark_node)
11251 return error_mark_node;
11252 trailing.safe_push (elt);
11254 type2 = make_tree_vec (trailing.length ());
11255 for (int i = 0; i < TREE_VEC_LENGTH (type2); ++i)
11256 TREE_VEC_ELT (type2, i) = trailing[i];
11259 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
11260 if (kind == CPTK_TYPE_PACK_ELEMENT)
11261 /* cp_parser_enclosed_template_argument_list above already took care
11262 of parsing the closing '>'. */;
11263 else
11264 parens.require_close (parser);
11266 /* Construct a location of the form:
11267 __is_trivially_copyable(_Tp)
11268 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
11269 with start == caret, finishing at the close-paren. */
11270 location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
11272 /* Complete the trait expression, which may mean either processing
11273 the trait expr now or saving it for template instantiation. */
11274 switch (kind)
11276 case CPTK_BASES:
11277 return cp_expr (finish_bases (type1, false), trait_loc);
11278 case CPTK_DIRECT_BASES:
11279 return cp_expr (finish_bases (type1, true), trait_loc);
11280 default:
11281 if (type)
11282 return finish_trait_type (kind, type1, type2, tf_warning_or_error);
11283 else
11284 return finish_trait_expr (trait_loc, kind, type1, type2);
11288 /* Parse a lambda expression.
11290 lambda-expression:
11291 lambda-introducer lambda-declarator [opt] compound-statement
11292 lambda-introducer < template-parameter-list > requires-clause [opt]
11293 lambda-declarator [opt] compound-statement
11295 Returns a representation of the expression. */
11297 static cp_expr
11298 cp_parser_lambda_expression (cp_parser* parser)
11300 tree lambda_expr = build_lambda_expr ();
11301 tree type;
11302 bool ok = true;
11303 cp_token *token = cp_lexer_peek_token (parser->lexer);
11304 cp_token_position start = 0;
11306 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
11308 if (cxx_dialect >= cxx20)
11310 /* C++20 allows lambdas in unevaluated context, but one in the type of a
11311 non-type parameter is nonsensical.
11313 Distinguish a lambda in the parameter type from a lambda in the
11314 default argument by looking at local_variables_forbidden_p, which is
11315 only set in default arguments. */
11316 if (processing_template_parmlist && !parser->local_variables_forbidden_p)
11318 error_at (token->location,
11319 "lambda-expression in template parameter type");
11320 token->error_reported = true;
11321 ok = false;
11324 else if (cp_unevaluated_operand)
11326 if (!token->error_reported)
11328 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
11329 "lambda-expression in unevaluated context"
11330 " only available with %<-std=c++20%> or %<-std=gnu++20%>");
11331 token->error_reported = true;
11333 ok = false;
11335 else if (parser->in_template_argument_list_p || processing_template_parmlist)
11337 if (!token->error_reported)
11339 error_at (token->location, "lambda-expression in template-argument"
11340 " only available with %<-std=c++20%> or %<-std=gnu++20%>");
11341 token->error_reported = true;
11343 ok = false;
11346 /* We may be in the middle of deferred access check. Disable
11347 it now. */
11348 push_deferring_access_checks (dk_no_deferred);
11350 cp_parser_lambda_introducer (parser, lambda_expr);
11351 if (cp_parser_error_occurred (parser))
11352 return error_mark_node;
11354 type = begin_lambda_type (lambda_expr);
11355 if (type == error_mark_node)
11356 return error_mark_node;
11358 record_lambda_scope (lambda_expr);
11359 record_lambda_scope_discriminator (lambda_expr);
11361 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
11362 determine_visibility (TYPE_NAME (type));
11364 /* Now that we've started the type, add the capture fields for any
11365 explicit captures. */
11366 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
11369 /* Inside the class, surrounding template-parameter-lists do not apply. */
11370 unsigned int saved_num_template_parameter_lists
11371 = parser->num_template_parameter_lists;
11372 unsigned char in_statement = parser->in_statement;
11373 bool in_switch_statement_p = parser->in_switch_statement_p;
11374 bool fully_implicit_function_template_p
11375 = parser->fully_implicit_function_template_p;
11376 tree implicit_template_parms = parser->implicit_template_parms;
11377 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
11378 bool auto_is_implicit_function_template_parm_p
11379 = parser->auto_is_implicit_function_template_parm_p;
11380 bool saved_omp_array_section_p = parser->omp_array_section_p;
11382 parser->num_template_parameter_lists = 0;
11383 parser->in_statement = 0;
11384 parser->in_switch_statement_p = false;
11385 parser->fully_implicit_function_template_p = false;
11386 parser->implicit_template_parms = 0;
11387 parser->implicit_template_scope = 0;
11388 parser->auto_is_implicit_function_template_parm_p = false;
11389 parser->omp_array_section_p = false;
11391 /* The body of a lambda in a discarded statement is not discarded. */
11392 bool discarded = in_discarded_stmt;
11393 in_discarded_stmt = 0;
11395 /* Similarly the body of a lambda in immediate function context is not
11396 in immediate function context. */
11397 bool save_in_consteval_if_p = in_consteval_if_p;
11398 in_consteval_if_p = false;
11400 /* By virtue of defining a local class, a lambda expression has access to
11401 the private variables of enclosing classes. */
11403 if (cp_parser_start_tentative_firewall (parser))
11404 start = token;
11406 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
11408 if (ok && cp_parser_error_occurred (parser))
11409 ok = false;
11411 if (ok)
11412 cp_parser_lambda_body (parser, lambda_expr);
11413 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
11415 if (cp_parser_skip_to_closing_brace (parser))
11416 cp_lexer_consume_token (parser->lexer);
11419 /* The capture list was built up in reverse order; fix that now. */
11420 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
11421 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
11423 if (ok)
11424 maybe_add_lambda_conv_op (type);
11426 finish_struct (type, /*attributes=*/NULL_TREE);
11428 in_consteval_if_p = save_in_consteval_if_p;
11429 in_discarded_stmt = discarded;
11431 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
11432 parser->in_statement = in_statement;
11433 parser->in_switch_statement_p = in_switch_statement_p;
11434 parser->fully_implicit_function_template_p
11435 = fully_implicit_function_template_p;
11436 parser->implicit_template_parms = implicit_template_parms;
11437 parser->implicit_template_scope = implicit_template_scope;
11438 parser->auto_is_implicit_function_template_parm_p
11439 = auto_is_implicit_function_template_parm_p;
11440 parser->omp_array_section_p = saved_omp_array_section_p;
11443 /* This field is only used during parsing of the lambda. */
11444 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
11446 /* This lambda shouldn't have any proxies left at this point. */
11447 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
11448 /* And now that we're done, push proxies for an enclosing lambda. */
11449 insert_pending_capture_proxies ();
11451 /* Update the lambda expression to a range. */
11452 LAMBDA_EXPR_LOCATION (lambda_expr) = make_location (token->location,
11453 token->location,
11454 parser->lexer);
11456 if (ok)
11457 lambda_expr = build_lambda_object (lambda_expr);
11458 else
11459 lambda_expr = error_mark_node;
11461 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
11463 pop_deferring_access_checks ();
11465 return lambda_expr;
11468 /* Parse the beginning of a lambda expression.
11470 lambda-introducer:
11471 [ lambda-capture [opt] ]
11473 LAMBDA_EXPR is the current representation of the lambda expression. */
11475 static void
11476 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
11478 /* Need commas after the first capture. */
11479 bool first = true;
11481 /* Eat the leading `['. */
11482 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
11484 /* Record default capture mode. "[&" "[=" "[&," "[=," */
11485 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
11486 && !cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS)
11487 && !cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME)
11488 && !cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
11489 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
11490 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11491 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
11493 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
11495 cp_lexer_consume_token (parser->lexer);
11496 first = false;
11498 if (!(at_function_scope_p () || parsing_nsdmi ()))
11499 error ("non-local lambda expression cannot have a capture-default");
11502 hash_set<tree, true> ids;
11503 tree first_capture_id = NULL_TREE;
11504 unsigned name_independent_cnt = 0;
11505 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
11507 cp_token* capture_token;
11508 tree capture_id;
11509 tree capture_init_expr;
11510 cp_id_kind idk = CP_ID_KIND_NONE;
11511 bool explicit_init_p = false;
11513 enum capture_kind_type
11515 BY_COPY,
11516 BY_REFERENCE
11518 enum capture_kind_type capture_kind = BY_COPY;
11520 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
11522 error ("expected end of capture-list");
11523 return;
11526 if (first)
11527 first = false;
11528 else
11529 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
11531 /* Possibly capture `this'. */
11532 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
11534 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11535 if (cxx_dialect < cxx20 && pedantic
11536 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
11537 pedwarn (loc, OPT_Wc__20_extensions,
11538 "explicit by-copy capture of %<this%> "
11539 "with by-copy capture default only available with "
11540 "%<-std=c++20%> or %<-std=gnu++20%>");
11541 cp_lexer_consume_token (parser->lexer);
11542 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
11543 pedwarn (input_location, 0,
11544 "already captured %qD in lambda expression",
11545 this_identifier);
11546 else
11547 add_capture (lambda_expr, /*id=*/this_identifier,
11548 /*initializer=*/finish_this_expr (),
11549 /*by_reference_p=*/true, explicit_init_p, NULL);
11550 continue;
11553 /* Possibly capture `*this'. */
11554 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
11555 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
11557 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11558 if (cxx_dialect < cxx17)
11559 pedwarn (loc, OPT_Wc__17_extensions,
11560 "%<*this%> capture only available with "
11561 "%<-std=c++17%> or %<-std=gnu++17%>");
11562 cp_lexer_consume_token (parser->lexer);
11563 cp_lexer_consume_token (parser->lexer);
11564 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
11565 pedwarn (input_location, 0,
11566 "already captured %qD in lambda expression",
11567 this_identifier);
11568 else
11569 add_capture (lambda_expr, /*id=*/this_identifier,
11570 /*initializer=*/finish_this_expr (),
11571 /*by_reference_p=*/false, explicit_init_p, NULL);
11572 continue;
11575 /* But reject `&this'. */
11576 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
11577 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
11579 error_at (cp_lexer_peek_token (parser->lexer)->location,
11580 "%<this%> cannot be captured by reference");
11581 cp_lexer_consume_token (parser->lexer);
11582 cp_lexer_consume_token (parser->lexer);
11583 continue;
11586 /* Remember whether we want to capture as a reference or not. */
11587 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
11589 capture_kind = BY_REFERENCE;
11590 cp_lexer_consume_token (parser->lexer);
11593 bool init_pack_expansion = false;
11594 location_t ellipsis_loc = UNKNOWN_LOCATION;
11595 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11597 ellipsis_loc = cp_lexer_peek_token (parser->lexer)->location;
11598 if (cxx_dialect < cxx20)
11599 pedwarn (ellipsis_loc, OPT_Wc__20_extensions,
11600 "pack init-capture only available with "
11601 "%<-std=c++20%> or %<-std=gnu++20%>");
11602 cp_lexer_consume_token (parser->lexer);
11603 init_pack_expansion = true;
11606 /* Early C++20 drafts had ...& instead of &...; be forgiving. */
11607 if (init_pack_expansion && capture_kind != BY_REFERENCE
11608 && cp_lexer_next_token_is (parser->lexer, CPP_AND))
11610 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
11611 0, "%<&%> should come before %<...%>");
11612 capture_kind = BY_REFERENCE;
11613 cp_lexer_consume_token (parser->lexer);
11616 /* Get the identifier. */
11617 capture_token = cp_lexer_peek_token (parser->lexer);
11618 capture_id = cp_parser_identifier (parser);
11620 if (capture_id == error_mark_node)
11621 /* Would be nice to have a cp_parser_skip_to_closing_x for general
11622 delimiters, but I modified this to stop on unnested ']' as well. It
11623 was already changed to stop on unnested '}', so the
11624 "closing_parenthesis" name is no more misleading with my change. */
11626 cp_parser_skip_to_closing_parenthesis (parser,
11627 /*recovering=*/true,
11628 /*or_comma=*/true,
11629 /*consume_paren=*/true);
11630 break;
11633 /* Find the initializer for this capture. */
11634 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
11635 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
11636 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11638 /* An explicit initializer exists. */
11639 if (cxx_dialect < cxx14)
11640 pedwarn (input_location, OPT_Wc__14_extensions,
11641 "lambda capture initializers "
11642 "only available with %<-std=c++14%> or %<-std=gnu++14%>");
11643 capture_init_expr = cp_parser_initializer (parser,
11644 /*direct_init=*/nullptr,
11645 /*non_constant=*/nullptr,
11646 /*subexpression_p=*/true);
11647 explicit_init_p = true;
11648 if (capture_init_expr == NULL_TREE)
11650 error ("empty initializer for lambda init-capture");
11651 capture_init_expr = error_mark_node;
11653 if (init_pack_expansion)
11654 capture_init_expr = make_pack_expansion (capture_init_expr);
11656 else
11658 const char* error_msg;
11660 /* Turn the identifier into an id-expression. */
11661 capture_init_expr
11662 = cp_parser_lookup_name_simple (parser, capture_id,
11663 capture_token->location);
11665 if (capture_init_expr == error_mark_node)
11667 unqualified_name_lookup_error (capture_id);
11668 continue;
11670 else if (!VAR_P (capture_init_expr)
11671 && TREE_CODE (capture_init_expr) != PARM_DECL)
11673 error_at (capture_token->location,
11674 "capture of non-variable %qE",
11675 capture_init_expr);
11676 if (DECL_P (capture_init_expr))
11677 inform (DECL_SOURCE_LOCATION (capture_init_expr),
11678 "%q#D declared here", capture_init_expr);
11679 continue;
11681 if (VAR_P (capture_init_expr)
11682 && decl_storage_duration (capture_init_expr) != dk_auto)
11684 if (pedwarn (capture_token->location, 0, "capture of variable "
11685 "%qD with non-automatic storage duration",
11686 capture_init_expr))
11687 inform (DECL_SOURCE_LOCATION (capture_init_expr),
11688 "%q#D declared here", capture_init_expr);
11689 continue;
11692 capture_init_expr
11693 = finish_id_expression
11694 (capture_id,
11695 capture_init_expr,
11696 parser->scope,
11697 &idk,
11698 /*integral_constant_expression_p=*/false,
11699 /*allow_non_integral_constant_expression_p=*/false,
11700 /*non_integral_constant_expression_p=*/NULL,
11701 /*template_p=*/false,
11702 /*done=*/true,
11703 /*address_p=*/false,
11704 /*template_arg_p=*/false,
11705 &error_msg,
11706 capture_token->location);
11708 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11710 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11711 cp_lexer_consume_token (parser->lexer);
11712 capture_init_expr = make_pack_expansion (capture_init_expr);
11713 if (init_pack_expansion)
11715 /* If what follows is an initializer, the second '...' is
11716 invalid. But for cases like [...xs...], the first one
11717 is invalid. */
11718 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
11719 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
11720 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11721 ellipsis_loc = loc;
11722 error_at (ellipsis_loc, "too many %<...%> in lambda capture");
11723 continue;
11728 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
11729 && !explicit_init_p)
11731 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
11732 && capture_kind == BY_COPY)
11733 pedwarn (capture_token->location, 0, "explicit by-copy capture "
11734 "of %qD redundant with by-copy capture default",
11735 capture_id);
11736 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
11737 && capture_kind == BY_REFERENCE)
11738 pedwarn (capture_token->location, 0, "explicit by-reference "
11739 "capture of %qD redundant with by-reference capture "
11740 "default", capture_id);
11743 /* Check for duplicates.
11744 Optimize for the zero or one explicit captures cases and only create
11745 the hash_set after adding second capture. */
11746 bool found = false;
11747 if (!ids.is_empty ())
11748 found = ids.add (capture_id);
11749 else if (first_capture_id == NULL_TREE)
11750 first_capture_id = capture_id;
11751 else if (capture_id == first_capture_id)
11752 found = true;
11753 else
11755 ids.add (first_capture_id);
11756 ids.add (capture_id);
11758 if (found && explicit_init_p && id_equal (capture_id, "_"))
11759 found = false;
11760 if (found)
11761 pedwarn (input_location, 0,
11762 "already captured %qD in lambda expression", capture_id);
11763 else
11764 add_capture (lambda_expr, capture_id, capture_init_expr,
11765 /*by_reference_p=*/capture_kind == BY_REFERENCE,
11766 explicit_init_p, &name_independent_cnt);
11768 /* If there is any qualification still in effect, clear it
11769 now; we will be starting fresh with the next capture. */
11770 parser->scope = NULL_TREE;
11771 parser->qualifying_scope = NULL_TREE;
11772 parser->object_scope = NULL_TREE;
11775 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11778 /* Parse the (optional) middle of a lambda expression.
11780 lambda-declarator:
11781 ( parameter-declaration-clause ) lambda-specifiers requires-clause [opt]
11782 lambda-specifiers (C++23)
11784 lambda-specifiers:
11785 decl-specifier-seq [opt] noexcept-specifier [opt]
11786 attribute-specifier-seq [opt] trailing-return-type [opt]
11788 LAMBDA_EXPR is the current representation of the lambda expression. */
11790 static bool
11791 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
11793 /* 5.1.1.4 of the standard says:
11794 If a lambda-expression does not include a lambda-declarator, it is as if
11795 the lambda-declarator were ().
11796 This means an empty parameter list, no attributes, and no exception
11797 specification. */
11798 tree param_list = void_list_node;
11799 tree std_attrs = NULL_TREE;
11800 tree gnu_attrs = NULL_TREE;
11801 tree exception_spec = NULL_TREE;
11802 tree template_param_list = NULL_TREE;
11803 tree tx_qual = NULL_TREE;
11804 tree return_type = NULL_TREE;
11805 tree trailing_requires_clause = NULL_TREE;
11806 bool has_param_list = false;
11807 location_t omitted_parms_loc = UNKNOWN_LOCATION;
11808 cp_decl_specifier_seq lambda_specs;
11809 clear_decl_specs (&lambda_specs);
11810 /* A lambda op() is const unless explicitly 'mutable'. */
11811 cp_cv_quals quals = TYPE_QUAL_CONST;
11813 /* The template-parameter-list is optional, but must begin with
11814 an opening angle if present. */
11815 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
11817 if (cxx_dialect < cxx20
11818 && (pedantic || cxx_dialect < cxx14))
11819 pedwarn (parser->lexer->next_token->location, OPT_Wc__20_extensions,
11820 "lambda templates are only available with "
11821 "%<-std=c++20%> or %<-std=gnu++20%>");
11823 cp_lexer_consume_token (parser->lexer);
11825 template_param_list = cp_parser_template_parameter_list (parser);
11826 cp_parser_require_end_of_template_parameter_list (parser);
11828 /* We may have a constrained generic lambda; parse the requires-clause
11829 immediately after the template-parameter-list and combine with any
11830 shorthand constraints present. */
11831 tree dreqs = cp_parser_requires_clause_opt (parser, true);
11832 if (flag_concepts)
11834 tree reqs = get_shorthand_constraints (current_template_parms);
11835 if (dreqs)
11836 reqs = combine_constraint_expressions (reqs, dreqs);
11837 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
11840 /* We just processed one more parameter list. */
11841 ++parser->num_template_parameter_lists;
11844 /* Committee discussion supports allowing attributes here. */
11845 lambda_specs.attributes = cp_parser_attributes_opt (parser);
11847 /* The parameter-declaration-clause is optional (unless
11848 template-parameter-list was given), but must begin with an
11849 opening parenthesis if present. */
11850 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
11852 matching_parens parens;
11853 parens.consume_open (parser);
11855 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
11857 /* Parse parameters. */
11858 param_list
11859 = cp_parser_parameter_declaration_clause
11860 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL);
11862 /* Default arguments shall not be specified in the
11863 parameter-declaration-clause of a lambda-declarator. */
11864 if (pedantic && cxx_dialect < cxx14)
11865 for (tree t = param_list; t; t = TREE_CHAIN (t))
11866 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
11867 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
11868 OPT_Wc__14_extensions,
11869 "default argument specified for lambda parameter");
11871 parens.require_close (parser);
11872 has_param_list = true;
11874 else if (cxx_dialect < cxx23)
11875 omitted_parms_loc = cp_lexer_peek_token (parser->lexer)->location;
11877 /* In the decl-specifier-seq of the lambda-declarator, each
11878 decl-specifier shall either be mutable or constexpr. */
11879 int declares_class_or_enum;
11880 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
11881 cp_parser_decl_specifier_seq (parser,
11882 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
11883 &lambda_specs, &declares_class_or_enum);
11885 if (omitted_parms_loc && lambda_specs.any_specifiers_p)
11887 pedwarn (omitted_parms_loc, OPT_Wc__23_extensions,
11888 "parameter declaration before lambda declaration "
11889 "specifiers only optional with %<-std=c++2b%> or "
11890 "%<-std=gnu++2b%>");
11891 omitted_parms_loc = UNKNOWN_LOCATION;
11894 if (lambda_specs.storage_class == sc_mutable)
11896 quals = TYPE_UNQUALIFIED;
11898 else if (lambda_specs.storage_class == sc_static)
11900 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
11901 || LAMBDA_EXPR_CAPTURE_LIST (lambda_expr))
11902 error_at (lambda_specs.locations[ds_storage_class],
11903 "%<static%> lambda specifier with lambda capture");
11904 else
11906 LAMBDA_EXPR_STATIC_P (lambda_expr) = 1;
11907 quals = TYPE_UNQUALIFIED;
11911 tx_qual = cp_parser_tx_qualifier_opt (parser);
11912 if (omitted_parms_loc && tx_qual)
11914 pedwarn (omitted_parms_loc, OPT_Wc__23_extensions,
11915 "parameter declaration before lambda transaction "
11916 "qualifier only optional with %<-std=c++2b%> or "
11917 "%<-std=gnu++2b%>");
11918 omitted_parms_loc = UNKNOWN_LOCATION;
11921 /* Parse optional exception specification. */
11922 exception_spec
11923 = cp_parser_exception_specification_opt (parser, CP_PARSER_FLAGS_NONE);
11925 if (omitted_parms_loc && exception_spec)
11927 pedwarn (omitted_parms_loc, OPT_Wc__23_extensions,
11928 "parameter declaration before lambda exception "
11929 "specification only optional with %<-std=c++2b%> or "
11930 "%<-std=gnu++2b%>");
11931 omitted_parms_loc = UNKNOWN_LOCATION;
11934 /* GCC 8 accepted attributes here, and this is the place for standard C++11
11935 attributes that appertain to the function type. */
11936 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
11937 gnu_attrs = cp_parser_gnu_attributes_opt (parser);
11938 else
11939 std_attrs = cp_parser_std_attribute_spec_seq (parser);
11941 /* Parse optional trailing return type. */
11942 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
11944 if (omitted_parms_loc)
11945 pedwarn (omitted_parms_loc, OPT_Wc__23_extensions,
11946 "parameter declaration before lambda trailing "
11947 "return type only optional with %<-std=c++2b%> or "
11948 "%<-std=gnu++2b%>");
11949 cp_lexer_consume_token (parser->lexer);
11950 return_type = cp_parser_trailing_type_id (parser);
11953 /* Also allow GNU attributes at the very end of the declaration, the usual
11954 place for GNU attributes. */
11955 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
11956 gnu_attrs = chainon (gnu_attrs, cp_parser_gnu_attributes_opt (parser));
11958 if (has_param_list)
11960 /* Parse optional trailing requires clause. */
11961 trailing_requires_clause = cp_parser_requires_clause_opt (parser, false);
11963 /* The function parameters must be in scope all the way until after the
11964 trailing-return-type in case of decltype. */
11965 pop_bindings_and_leave_scope ();
11968 /* Create the function call operator.
11970 Messing with declarators like this is no uglier than building up the
11971 FUNCTION_DECL by hand, and this is less likely to get out of sync with
11972 other code. */
11974 cp_decl_specifier_seq return_type_specs;
11975 cp_declarator* declarator;
11976 tree fco;
11977 void *p;
11979 clear_decl_specs (&return_type_specs);
11980 return_type_specs.type = make_auto ();
11982 if (lambda_specs.locations[ds_constexpr])
11984 if (cxx_dialect >= cxx17)
11985 return_type_specs.locations[ds_constexpr]
11986 = lambda_specs.locations[ds_constexpr];
11987 else
11988 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
11989 "lambda only available with %<-std=c++17%> or "
11990 "%<-std=gnu++17%>");
11992 if (lambda_specs.locations[ds_consteval])
11993 return_type_specs.locations[ds_consteval]
11994 = lambda_specs.locations[ds_consteval];
11995 if (LAMBDA_EXPR_STATIC_P (lambda_expr))
11997 return_type_specs.storage_class = sc_static;
11998 return_type_specs.locations[ds_storage_class]
11999 = lambda_specs.locations[ds_storage_class];
12002 p = obstack_alloc (&declarator_obstack, 0);
12004 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none,
12005 LAMBDA_EXPR_LOCATION (lambda_expr));
12007 declarator = make_call_declarator (declarator, param_list, quals,
12008 VIRT_SPEC_UNSPECIFIED,
12009 REF_QUAL_NONE,
12010 tx_qual,
12011 exception_spec,
12012 return_type,
12013 trailing_requires_clause,
12014 std_attrs,
12015 UNKNOWN_LOCATION);
12017 fco = grokmethod (&return_type_specs,
12018 declarator,
12019 chainon (gnu_attrs, lambda_specs.attributes));
12020 if (fco != error_mark_node)
12022 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
12023 DECL_ARTIFICIAL (fco) = 1;
12024 if (!LAMBDA_EXPR_STATIC_P (lambda_expr))
12025 /* Give the object parameter a different name. */
12026 DECL_NAME (DECL_ARGUMENTS (fco)) = closure_identifier;
12027 DECL_SET_LAMBDA_FUNCTION (fco, true);
12029 if (template_param_list)
12031 fco = finish_member_template_decl (fco);
12032 finish_template_decl (template_param_list);
12033 --parser->num_template_parameter_lists;
12035 else if (parser->fully_implicit_function_template_p)
12036 fco = finish_fully_implicit_template (parser, fco);
12038 finish_member_declaration (fco);
12039 record_lambda_scope_sig_discriminator (lambda_expr, fco);
12041 obstack_free (&declarator_obstack, p);
12043 return (fco != error_mark_node);
12047 /* Parse the body of a lambda expression, which is simply
12049 compound-statement
12051 but which requires special handling.
12052 LAMBDA_EXPR is the current representation of the lambda expression. */
12054 static void
12055 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
12057 bool nested = (current_function_decl != NULL_TREE);
12058 unsigned char local_variables_forbidden_p
12059 = parser->local_variables_forbidden_p;
12060 bool in_function_body = parser->in_function_body;
12062 /* The body of a lambda-expression is not a subexpression of the enclosing
12063 expression. */
12064 cp_evaluated ev;
12066 if (nested)
12067 push_function_context ();
12068 else
12069 /* Still increment function_depth so that we don't GC in the
12070 middle of an expression. */
12071 ++function_depth;
12073 auto odsd = make_temp_override (parser->omp_declare_simd, NULL);
12074 auto ord = make_temp_override (parser->oacc_routine, NULL);
12075 auto oafp = make_temp_override (parser->omp_attrs_forbidden_p, false);
12076 vec<tree> omp_privatization_save;
12077 save_omp_privatization_clauses (omp_privatization_save);
12078 /* Clear this in case we're in the middle of a default argument. */
12079 parser->local_variables_forbidden_p = 0;
12080 parser->in_function_body = true;
12083 local_specialization_stack s (lss_copy);
12084 tree fco = lambda_function (lambda_expr);
12085 tree body = start_lambda_function (fco, lambda_expr);
12087 /* Originally C++11 required us to peek for 'return expr'; and
12088 process it specially here to deduce the return type. N3638
12089 removed the need for that. */
12090 cp_parser_function_body (parser, false);
12092 finish_lambda_function (body);
12095 restore_omp_privatization_clauses (omp_privatization_save);
12096 parser->local_variables_forbidden_p = local_variables_forbidden_p;
12097 parser->in_function_body = in_function_body;
12098 if (nested)
12099 pop_function_context();
12100 else
12101 --function_depth;
12104 /* Statements [gram.stmt.stmt] */
12106 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
12108 static void
12109 add_debug_begin_stmt (location_t loc)
12111 if (!MAY_HAVE_DEBUG_MARKER_STMTS)
12112 return;
12113 if (DECL_DECLARED_CONCEPT_P (current_function_decl))
12114 /* A concept is never expanded normally. */
12115 return;
12117 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
12118 SET_EXPR_LOCATION (stmt, loc);
12119 add_stmt (stmt);
12122 struct cp_omp_attribute_data
12124 cp_token_cache *tokens;
12125 const c_omp_directive *dir;
12126 c_omp_directive_kind kind;
12129 /* Handle omp::directive and omp::sequence attributes in ATTRS
12130 (if any) at the start of a statement or in attribute-declaration. */
12132 static tree
12133 cp_parser_handle_statement_omp_attributes (cp_parser *parser, tree attrs)
12135 if (!flag_openmp && !flag_openmp_simd)
12136 return attrs;
12138 auto_vec<cp_omp_attribute_data, 16> vec;
12139 int cnt = 0;
12140 int tokens = 0;
12141 bool bad = false;
12142 for (tree *pa = &attrs; *pa; )
12143 if (get_attribute_namespace (*pa) == omp_identifier
12144 && is_attribute_p ("directive", get_attribute_name (*pa)))
12146 cnt++;
12147 for (tree a = TREE_VALUE (*pa); a; a = TREE_CHAIN (a))
12149 tree d = TREE_VALUE (a);
12150 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
12151 cp_token *first = DEFPARSE_TOKENS (d)->first;
12152 cp_token *last = DEFPARSE_TOKENS (d)->last;
12153 if (parser->omp_attrs_forbidden_p)
12155 error_at (first->location,
12156 "mixing OpenMP directives with attribute and pragma "
12157 "syntax on the same statement");
12158 parser->omp_attrs_forbidden_p = false;
12159 bad = true;
12161 else if (TREE_PUBLIC (d))
12163 error_at (first->location,
12164 "OpenMP %<omp::decl%> attribute on a statement");
12165 bad = true;
12167 const char *directive[3] = {};
12168 for (int i = 0; i < 3; i++)
12170 tree id = NULL_TREE;
12171 if (first + i == last)
12172 break;
12173 if (first[i].type == CPP_NAME)
12174 id = first[i].u.value;
12175 else if (first[i].type == CPP_KEYWORD)
12176 id = ridpointers[(int) first[i].keyword];
12177 else
12178 break;
12179 directive[i] = IDENTIFIER_POINTER (id);
12181 const c_omp_directive *dir = NULL;
12182 if (directive[0])
12183 dir = c_omp_categorize_directive (directive[0], directive[1],
12184 directive[2]);
12185 if (dir == NULL)
12187 error_at (first->location,
12188 "unknown OpenMP directive name in %qs attribute "
12189 "argument",
12190 TREE_PUBLIC (d) ? "omp::decl" : "omp::directive");
12191 continue;
12193 c_omp_directive_kind kind = dir->kind;
12194 if (dir->id == PRAGMA_OMP_ORDERED)
12196 /* ordered is C_OMP_DIR_CONSTRUCT only if it doesn't contain
12197 depend/doacross clause. */
12198 if (directive[1]
12199 && (strcmp (directive[1], "depend") == 0
12200 || strcmp (directive[1], "doacross") == 0))
12201 kind = C_OMP_DIR_STANDALONE;
12202 else if (first + 2 < last
12203 && first[1].type == CPP_COMMA
12204 && first[2].type == CPP_NAME
12205 && (strcmp (IDENTIFIER_POINTER (first[2].u.value),
12206 "depend") == 0
12207 || strcmp (IDENTIFIER_POINTER (first[2].u.value),
12208 "doacross") == 0))
12209 kind = C_OMP_DIR_STANDALONE;
12211 else if (dir->id == PRAGMA_OMP_ERROR)
12213 /* error with at(execution) clause is C_OMP_DIR_STANDALONE. */
12214 int paren_depth = 0;
12215 for (int i = 1; first + i < last; i++)
12216 if (first[i].type == CPP_OPEN_PAREN)
12217 paren_depth++;
12218 else if (first[i].type == CPP_CLOSE_PAREN)
12219 paren_depth--;
12220 else if (paren_depth == 0
12221 && first + i + 2 < last
12222 && first[i].type == CPP_NAME
12223 && first[i + 1].type == CPP_OPEN_PAREN
12224 && first[i + 2].type == CPP_NAME
12225 && !strcmp (IDENTIFIER_POINTER (first[i].u.value),
12226 "at")
12227 && !strcmp (IDENTIFIER_POINTER (first[i
12228 + 2].u.value),
12229 "execution"))
12231 kind = C_OMP_DIR_STANDALONE;
12232 break;
12235 cp_omp_attribute_data v = { DEFPARSE_TOKENS (d), dir, kind };
12236 vec.safe_push (v);
12237 if (flag_openmp || dir->simd)
12238 tokens += (last - first) + 1;
12240 cp_omp_attribute_data v = {};
12241 vec.safe_push (v);
12242 *pa = TREE_CHAIN (*pa);
12244 else
12245 pa = &TREE_CHAIN (*pa);
12247 if (bad)
12248 return attrs;
12250 unsigned int i;
12251 cp_omp_attribute_data *v;
12252 cp_omp_attribute_data *construct_seen = nullptr;
12253 cp_omp_attribute_data *standalone_seen = nullptr;
12254 cp_omp_attribute_data *prev_standalone_seen = nullptr;
12255 FOR_EACH_VEC_ELT (vec, i, v)
12256 if (v->tokens)
12258 if (v->kind == C_OMP_DIR_CONSTRUCT && !construct_seen)
12259 construct_seen = v;
12260 else if (v->kind == C_OMP_DIR_STANDALONE && !standalone_seen)
12261 standalone_seen = v;
12263 else
12265 if (standalone_seen && !prev_standalone_seen)
12267 prev_standalone_seen = standalone_seen;
12268 standalone_seen = nullptr;
12272 if (cnt > 1 && construct_seen)
12274 error_at (construct_seen->tokens->first->location,
12275 "OpenMP construct among %<omp::directive%> attributes"
12276 " requires all %<omp::directive%> attributes on the"
12277 " same statement to be in the same %<omp::sequence%>");
12278 return attrs;
12280 if (cnt > 1 && standalone_seen && prev_standalone_seen)
12282 error_at (standalone_seen->tokens->first->location,
12283 "multiple OpenMP standalone directives among"
12284 " %<omp::directive%> attributes must be all within the"
12285 " same %<omp::sequence%>");
12286 return attrs;
12289 if (prev_standalone_seen)
12290 standalone_seen = prev_standalone_seen;
12291 if (standalone_seen
12292 && !cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12294 error_at (standalone_seen->tokens->first->location,
12295 "standalone OpenMP directives in %<omp::directive%> attribute"
12296 " can only appear on an empty statement");
12297 return attrs;
12299 if (cnt && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
12301 cp_token *token = cp_lexer_peek_token (parser->lexer);
12302 enum pragma_kind kind = cp_parser_pragma_kind (token);
12303 if (kind >= PRAGMA_OMP__START_ && kind <= PRAGMA_OMP__LAST_)
12305 error_at (token->location,
12306 "mixing OpenMP directives with attribute and pragma "
12307 "syntax on the same statement");
12308 return attrs;
12312 if (!tokens)
12313 return attrs;
12314 tokens++;
12315 cp_lexer *lexer = cp_lexer_alloc ();
12316 lexer->debugging_p = parser->lexer->debugging_p;
12317 vec_safe_reserve (lexer->buffer, tokens, true);
12318 FOR_EACH_VEC_ELT (vec, i, v)
12320 if (!v->tokens)
12321 continue;
12322 if (!flag_openmp && !v->dir->simd)
12323 continue;
12324 cp_token *first = v->tokens->first;
12325 cp_token *last = v->tokens->last;
12326 cp_token tok = {};
12327 tok.type = CPP_PRAGMA;
12328 tok.keyword = RID_MAX;
12329 tok.u.value = build_int_cst (NULL, v->dir->id);
12330 tok.location = first->location;
12331 lexer->buffer->quick_push (tok);
12332 while (++first < last)
12333 lexer->buffer->quick_push (*first);
12334 tok = {};
12335 tok.type = CPP_PRAGMA_EOL;
12336 tok.keyword = RID_MAX;
12337 tok.location = last->location;
12338 lexer->buffer->quick_push (tok);
12340 cp_token tok = {};
12341 tok.type = CPP_EOF;
12342 tok.keyword = RID_MAX;
12343 tok.location = lexer->buffer->last ().location;
12344 lexer->buffer->quick_push (tok);
12345 lexer->next = parser->lexer;
12346 lexer->next_token = lexer->buffer->address ();
12347 lexer->last_token = lexer->next_token
12348 + lexer->buffer->length ()
12349 - 1;
12350 lexer->in_omp_attribute_pragma = true;
12351 parser->lexer = lexer;
12352 /* Move the current source position to that of the first token in the
12353 new lexer. */
12354 cp_lexer_set_source_position_from_token (lexer->next_token);
12355 return attrs;
12358 /* True if and only if the name is one of the contract types. */
12360 static bool
12361 contract_attribute_p (const_tree id)
12363 return is_attribute_p ("assert", id)
12364 || is_attribute_p ("pre", id)
12365 || is_attribute_p ("post", id);
12368 /* Handle omp::directive and omp::sequence attributes in *PATTRS
12369 (if any) at the start or after declaration-id of a declaration. */
12371 static void
12372 cp_parser_handle_directive_omp_attributes (cp_parser *parser, tree *pattrs,
12373 cp_omp_declare_simd_data *data,
12374 bool start)
12376 if (!flag_openmp && !flag_openmp_simd)
12377 return;
12379 int cnt = 0;
12380 bool bad = false;
12381 bool variant_p = false;
12382 location_t loc = UNKNOWN_LOCATION;
12383 for (tree pa = *pattrs; pa; pa = TREE_CHAIN (pa))
12384 if (get_attribute_namespace (pa) == omp_identifier
12385 && is_attribute_p ("directive", get_attribute_name (pa)))
12387 for (tree a = TREE_VALUE (pa); a; a = TREE_CHAIN (a))
12389 tree d = TREE_VALUE (a);
12390 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
12391 cp_token *first = DEFPARSE_TOKENS (d)->first;
12392 cp_token *last = DEFPARSE_TOKENS (d)->last;
12393 const char *directive[3] = {};
12394 for (int i = 0; i < 3; i++)
12396 tree id = NULL_TREE;
12397 if (first + i == last)
12398 break;
12399 if (first[i].type == CPP_NAME)
12400 id = first[i].u.value;
12401 else if (first[i].type == CPP_KEYWORD)
12402 id = ridpointers[(int) first[i].keyword];
12403 else
12404 break;
12405 directive[i] = IDENTIFIER_POINTER (id);
12407 const c_omp_directive *dir = NULL;
12408 if (directive[0])
12409 dir = c_omp_categorize_directive (directive[0], directive[1],
12410 directive[2]);
12411 if (dir == NULL)
12412 continue;
12413 if (dir->id == PRAGMA_OMP_DECLARE
12414 && (strcmp (directive[1], "simd") == 0
12415 || strcmp (directive[1], "variant") == 0))
12417 if (cnt++ == 0)
12419 variant_p = strcmp (directive[1], "variant") == 0;
12420 loc = first->location;
12422 if (start && parser->omp_declare_simd && !bad)
12424 error_at (first->location,
12425 "mixing OpenMP directives with attribute and "
12426 "pragma syntax on the same declaration");
12427 bad = true;
12433 if (bad)
12435 for (tree *pa = pattrs; *pa; )
12436 if (get_attribute_namespace (*pa) == omp_identifier
12437 && is_attribute_p ("directive", get_attribute_name (*pa)))
12438 *pa = TREE_CHAIN (*pa);
12439 else
12440 pa = &TREE_CHAIN (*pa);
12441 return;
12443 if (cnt == 0)
12444 return;
12446 if (parser->omp_declare_simd == NULL)
12448 data->error_seen = false;
12449 data->fndecl_seen = false;
12450 data->variant_p = variant_p;
12451 data->loc = loc;
12452 data->tokens = vNULL;
12453 data->attribs[0] = NULL;
12454 data->attribs[1] = NULL;
12455 parser->omp_declare_simd = data;
12457 parser->omp_declare_simd->attribs[!start] = pattrs;
12460 /* Parse a statement.
12462 statement:
12463 labeled-statement
12464 expression-statement
12465 compound-statement
12466 selection-statement
12467 iteration-statement
12468 jump-statement
12469 declaration-statement
12470 try-block
12472 C++11:
12474 statement:
12475 labeled-statement
12476 attribute-specifier-seq (opt) expression-statement
12477 attribute-specifier-seq (opt) compound-statement
12478 attribute-specifier-seq (opt) selection-statement
12479 attribute-specifier-seq (opt) iteration-statement
12480 attribute-specifier-seq (opt) jump-statement
12481 declaration-statement
12482 attribute-specifier-seq (opt) try-block
12484 init-statement:
12485 expression-statement
12486 simple-declaration
12487 alias-declaration
12489 TM Extension:
12491 statement:
12492 atomic-statement
12494 IN_COMPOUND is true when the statement is nested inside a
12495 cp_parser_compound_statement.
12497 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12498 is a (possibly labeled) if statement which is not enclosed in braces
12499 and has an else clause. This is used to implement -Wparentheses.
12501 CHAIN is a vector of if-else-if conditions.
12503 Note that this version of parsing restricts assertions to be attached to
12504 empty statements. */
12506 static void
12507 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
12508 const bool in_compound, bool *if_p, vec<tree> *chain,
12509 location_t *loc_after_labels)
12511 tree statement, std_attrs = NULL_TREE;
12512 cp_token *token;
12513 location_t statement_location, attrs_loc;
12514 bool in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
12515 bool has_std_attrs;
12516 /* A copy of IN_COMPOUND which is set to false after seeing a label.
12517 This matters for certain pragmas. */
12518 bool in_compound_for_pragma = in_compound;
12520 restart:
12521 if (if_p != NULL)
12522 *if_p = false;
12523 /* There is no statement yet. */
12524 statement = NULL_TREE;
12526 saved_token_sentinel saved_tokens (parser->lexer);
12527 token = cp_lexer_peek_token (parser->lexer);
12528 attrs_loc = token->location;
12529 if (c_dialect_objc ())
12530 /* In obj-c++, seeing '[[' might be the either the beginning of
12531 c++11 attributes, or a nested objc-message-expression. So
12532 let's parse the c++11 attributes tentatively. */
12533 cp_parser_parse_tentatively (parser);
12534 std_attrs = cp_parser_std_attribute_spec_seq (parser);
12535 if (std_attrs)
12536 attrs_loc = make_location (attrs_loc, attrs_loc, parser->lexer);
12537 if (c_dialect_objc ())
12539 if (!cp_parser_parse_definitely (parser))
12540 std_attrs = NULL_TREE;
12542 has_std_attrs = cp_lexer_peek_token (parser->lexer) != token;
12544 /* Peek at the next token. */
12545 token = cp_lexer_peek_token (parser->lexer);
12547 /* If we have contracts, check that they're valid in this context. */
12548 if (std_attrs != error_mark_node)
12550 if (tree pre = lookup_attribute ("pre", std_attrs))
12551 error_at (EXPR_LOCATION (TREE_VALUE (pre)),
12552 "preconditions cannot be statements");
12553 else if (tree post = lookup_attribute ("post", std_attrs))
12554 error_at (EXPR_LOCATION (TREE_VALUE (post)),
12555 "postconditions cannot be statements");
12557 /* Check that assertions are null statements. */
12558 if (cp_contract_assertion_p (std_attrs))
12559 if (token->type != CPP_SEMICOLON)
12560 error_at (token->location, "assertions must be followed by %<;%>");
12563 bool omp_attrs_forbidden_p;
12564 omp_attrs_forbidden_p = parser->omp_attrs_forbidden_p;
12566 if (std_attrs && (flag_openmp || flag_openmp_simd))
12568 bool handle_omp_attribs = false;
12569 if (token->type == CPP_KEYWORD)
12570 switch (token->keyword)
12572 case RID_IF:
12573 case RID_SWITCH:
12574 case RID_WHILE:
12575 case RID_DO:
12576 case RID_FOR:
12577 case RID_BREAK:
12578 case RID_CONTINUE:
12579 case RID_RETURN:
12580 case RID_CO_RETURN:
12581 case RID_GOTO:
12582 case RID_AT_TRY:
12583 case RID_AT_CATCH:
12584 case RID_AT_FINALLY:
12585 case RID_AT_SYNCHRONIZED:
12586 case RID_AT_THROW:
12587 case RID_TRY:
12588 case RID_TRANSACTION_ATOMIC:
12589 case RID_TRANSACTION_RELAXED:
12590 case RID_SYNCHRONIZED:
12591 case RID_ATOMIC_NOEXCEPT:
12592 case RID_ATOMIC_CANCEL:
12593 case RID_TRANSACTION_CANCEL:
12594 handle_omp_attribs = true;
12595 break;
12596 default:
12597 break;
12599 else if (token->type == CPP_SEMICOLON
12600 || token->type == CPP_OPEN_BRACE
12601 || token->type == CPP_PRAGMA)
12602 handle_omp_attribs = true;
12603 if (handle_omp_attribs)
12605 std_attrs = cp_parser_handle_statement_omp_attributes (parser,
12606 std_attrs);
12607 token = cp_lexer_peek_token (parser->lexer);
12610 parser->omp_attrs_forbidden_p = false;
12612 /* Remember the location of the first token in the statement. */
12613 cp_token *statement_token = token;
12614 statement_location = token->location;
12615 add_debug_begin_stmt (statement_location);
12616 /* If this is a keyword, then that will often determine what kind of
12617 statement we have. */
12618 if (token->type == CPP_KEYWORD)
12620 enum rid keyword = token->keyword;
12622 switch (keyword)
12624 case RID_CASE:
12625 case RID_DEFAULT:
12626 /* Looks like a labeled-statement with a case label.
12627 Parse the label, and then use tail recursion to parse
12628 the statement. */
12629 cp_parser_label_for_labeled_statement (parser, std_attrs);
12630 in_compound_for_pragma = false;
12631 in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
12632 goto restart;
12634 case RID_IF:
12635 case RID_SWITCH:
12636 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12637 statement = cp_parser_selection_statement (parser, if_p, chain);
12638 break;
12640 case RID_WHILE:
12641 case RID_DO:
12642 case RID_FOR:
12643 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12644 statement = cp_parser_iteration_statement (parser, if_p, false,
12645 NULL_TREE, false);
12646 break;
12648 case RID_BREAK:
12649 case RID_CONTINUE:
12650 case RID_RETURN:
12651 case RID_CO_RETURN:
12652 case RID_GOTO:
12653 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12654 statement = cp_parser_jump_statement (parser);
12655 break;
12657 /* Objective-C++ exception-handling constructs. */
12658 case RID_AT_TRY:
12659 case RID_AT_CATCH:
12660 case RID_AT_FINALLY:
12661 case RID_AT_SYNCHRONIZED:
12662 case RID_AT_THROW:
12663 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12664 statement = cp_parser_objc_statement (parser);
12665 break;
12667 case RID_TRY:
12668 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12669 statement = cp_parser_try_block (parser);
12670 break;
12672 case RID_NAMESPACE:
12673 /* This must be a namespace alias definition. */
12674 if (has_std_attrs)
12676 /* Attributes should be parsed as part of the
12677 declaration, so let's un-parse them. */
12678 saved_tokens.rollback();
12679 std_attrs = NULL_TREE;
12681 cp_parser_declaration_statement (parser);
12682 return;
12684 case RID_TRANSACTION_ATOMIC:
12685 case RID_TRANSACTION_RELAXED:
12686 case RID_SYNCHRONIZED:
12687 case RID_ATOMIC_NOEXCEPT:
12688 case RID_ATOMIC_CANCEL:
12689 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12690 statement = cp_parser_transaction (parser, token);
12691 break;
12692 case RID_TRANSACTION_CANCEL:
12693 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12694 statement = cp_parser_transaction_cancel (parser);
12695 break;
12697 default:
12698 /* It might be a keyword like `int' that can start a
12699 declaration-statement. */
12700 break;
12703 else if (token->type == CPP_NAME)
12705 /* If the next token is a `:', then we are looking at a
12706 labeled-statement. */
12707 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12708 if (token->type == CPP_COLON)
12710 /* Looks like a labeled-statement with an ordinary label.
12711 Parse the label, and then use tail recursion to parse
12712 the statement. */
12714 cp_parser_label_for_labeled_statement (parser, std_attrs);
12716 /* If there's no statement, it's not a labeled-statement, just
12717 a label. That's allowed in C++23, but only if we're at the
12718 end of a compound-statement. */
12719 if (in_compound
12720 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12722 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
12723 if (cxx_dialect < cxx23)
12724 pedwarn (loc, OPT_Wc__23_extensions,
12725 "label at end of compound statement only available "
12726 "with %<-std=c++2b%> or %<-std=gnu++2b%>");
12727 return;
12729 in_compound_for_pragma = false;
12730 in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
12731 goto restart;
12734 /* Anything that starts with a `{' must be a compound-statement. */
12735 else if (token->type == CPP_OPEN_BRACE)
12737 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12738 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12740 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
12741 a statement all its own. */
12742 else if (token->type == CPP_PRAGMA)
12744 do_pragma:;
12745 cp_lexer *lexer = parser->lexer;
12746 bool do_restart = false;
12747 /* Only certain OpenMP pragmas are attached to statements, and thus
12748 are considered statements themselves. All others are not. In
12749 the context of a compound, accept the pragma as a "statement" and
12750 return so that we can check for a close brace. Otherwise we
12751 require a real statement and must go back and read one. */
12752 if (in_compound_for_pragma)
12754 if (cp_parser_pragma (parser, pragma_compound, if_p)
12755 && parser->omp_for_parse_state)
12756 check_omp_intervening_code (parser);
12758 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
12759 do_restart = true;
12760 else if (parser->omp_for_parse_state)
12761 check_omp_intervening_code (parser);
12762 if (parser->lexer != lexer
12763 && lexer->in_omp_attribute_pragma
12764 && (!in_omp_attribute_pragma || lexer->orphan_p))
12766 if (saved_tokens.lexer == lexer)
12768 if (saved_tokens.mode == STS_COMMIT)
12769 cp_lexer_commit_tokens (lexer);
12770 gcc_assert (lexer->saved_tokens.length () == saved_tokens.len);
12771 saved_tokens.lexer = parser->lexer;
12772 saved_tokens.mode = STS_DONOTHING;
12773 saved_tokens.len = parser->lexer->saved_tokens.length ();
12775 cp_lexer_destroy (lexer);
12776 lexer = parser->lexer;
12778 if (do_restart)
12779 goto restart;
12780 if (parser->lexer == lexer
12781 && lexer->in_omp_attribute_pragma
12782 && !in_omp_attribute_pragma)
12783 parser->lexer->orphan_p = true;
12784 return;
12786 else if (token->type == CPP_EOF)
12788 cp_parser_error (parser, "expected statement");
12789 return;
12792 /* Everything else must be a declaration-statement or an
12793 expression-statement. Try for the declaration-statement
12794 first, unless we are looking at a `;', in which case we know that
12795 we have an expression-statement. */
12796 if (!statement)
12798 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12800 if (has_std_attrs)
12801 /* Attributes should be parsed as part of the declaration,
12802 so let's un-parse them. */
12803 saved_tokens.rollback();
12805 parser->omp_attrs_forbidden_p = omp_attrs_forbidden_p;
12806 cp_parser_parse_tentatively (parser);
12807 /* Try to parse the declaration-statement. */
12808 cp_parser_declaration_statement (parser);
12809 parser->omp_attrs_forbidden_p = false;
12810 /* If that worked, we're done. */
12811 if (cp_parser_parse_definitely (parser))
12812 return;
12813 /* It didn't work, restore the post-attribute position. */
12814 if (has_std_attrs)
12816 cp_lexer_set_token_position (parser->lexer, statement_token);
12817 if (flag_openmp || flag_openmp_simd)
12819 size_t i = 1;
12820 bool handle_omp_attribs = true;
12821 while (cp_lexer_peek_nth_token (parser->lexer, i)->keyword
12822 == RID_EXTENSION)
12823 i++;
12824 switch (cp_lexer_peek_nth_token (parser->lexer, i)->keyword)
12826 case RID_ASM:
12827 case RID_NAMESPACE:
12828 case RID_USING:
12829 case RID_LABEL:
12830 case RID_STATIC_ASSERT:
12831 /* Don't handle OpenMP attribs on keywords that
12832 always start a declaration statement but don't
12833 accept attribute before it and therefore
12834 the tentative cp_parser_declaration_statement
12835 fails to parse because of that. */
12836 handle_omp_attribs = false;
12837 break;
12838 default:
12839 break;
12842 if (handle_omp_attribs)
12844 parser->omp_attrs_forbidden_p = omp_attrs_forbidden_p;
12845 std_attrs
12846 = cp_parser_handle_statement_omp_attributes
12847 (parser, std_attrs);
12848 parser->omp_attrs_forbidden_p = false;
12849 token = cp_lexer_peek_token (parser->lexer);
12850 if (token->type == CPP_PRAGMA)
12851 goto do_pragma;
12856 /* All preceding labels have been parsed at this point. */
12857 if (loc_after_labels != NULL)
12858 *loc_after_labels = statement_location;
12860 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12862 /* Look for an expression-statement instead. */
12863 statement = cp_parser_expression_statement (parser, in_statement_expr);
12865 std_attrs = process_stmt_assume_attribute (std_attrs, statement,
12866 attrs_loc);
12868 /* Handle [[fallthrough]];. */
12869 if (attribute_fallthrough_p (std_attrs))
12871 /* The next token after the fallthrough attribute is ';'. */
12872 if (statement == NULL_TREE)
12874 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
12875 statement = build_call_expr_internal_loc (statement_location,
12876 IFN_FALLTHROUGH,
12877 void_type_node, 0);
12878 finish_expr_stmt (statement);
12880 else
12881 warning_at (statement_location, OPT_Wattributes,
12882 "%<fallthrough%> attribute not followed by %<;%>");
12883 std_attrs = NULL_TREE;
12886 /* Handle [[assert: ...]]; */
12887 if (cp_contract_assertion_p (std_attrs))
12889 /* Add the assertion as a statement in the current block. */
12890 gcc_assert (!statement || statement == error_mark_node);
12891 emit_assertion (std_attrs);
12892 std_attrs = NULL_TREE;
12896 /* Set the line number for the statement. */
12897 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
12898 SET_EXPR_LOCATION (statement, statement_location);
12900 /* Allow "[[fallthrough]];" or "[[assume(cond)]];", but warn otherwise. */
12901 if (std_attrs != NULL_TREE && any_nonignored_attribute_p (std_attrs))
12902 warning_at (attrs_loc, OPT_Wattributes,
12903 "attributes at the beginning of statement are ignored");
12906 /* Append ATTR to attribute list ATTRS. */
12908 tree
12909 attr_chainon (tree attrs, tree attr)
12911 if (attrs == error_mark_node)
12912 return error_mark_node;
12913 if (attr == error_mark_node)
12914 return error_mark_node;
12915 return chainon (attrs, attr);
12918 /* Parse the label for a labeled-statement, i.e.
12920 label:
12921 attribute-specifier-seq[opt] identifier :
12922 attribute-specifier-seq[opt] case constant-expression :
12923 attribute-specifier-seq[opt] default :
12925 labeled-statement:
12926 label statement
12928 GNU Extension:
12929 case constant-expression ... constant-expression : statement
12931 When a label is parsed without errors, the label is added to the
12932 parse tree by the finish_* functions, so this function doesn't
12933 have to return the label. */
12935 static void
12936 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
12938 cp_token *token;
12939 tree label = NULL_TREE;
12940 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12942 /* The next token should be an identifier. */
12943 token = cp_lexer_peek_token (parser->lexer);
12944 if (token->type != CPP_NAME
12945 && token->type != CPP_KEYWORD)
12947 cp_parser_error (parser, "expected labeled-statement");
12948 return;
12951 /* Remember whether this case or a user-defined label is allowed to fall
12952 through to. */
12953 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
12955 parser->colon_corrects_to_scope_p = false;
12956 switch (token->keyword)
12958 case RID_CASE:
12960 tree expr, expr_hi;
12961 cp_token *ellipsis;
12963 /* Consume the `case' token. */
12964 cp_lexer_consume_token (parser->lexer);
12965 /* Parse the constant-expression. */
12966 expr = cp_parser_constant_expression (parser);
12967 if (check_for_bare_parameter_packs (expr))
12968 expr = error_mark_node;
12970 ellipsis = cp_lexer_peek_token (parser->lexer);
12971 if (ellipsis->type == CPP_ELLIPSIS)
12973 /* Consume the `...' token. */
12974 cp_lexer_consume_token (parser->lexer);
12975 expr_hi = cp_parser_constant_expression (parser);
12976 if (check_for_bare_parameter_packs (expr_hi))
12977 expr_hi = error_mark_node;
12979 /* We don't need to emit warnings here, as the common code
12980 will do this for us. */
12982 else
12983 expr_hi = NULL_TREE;
12985 if (parser->in_switch_statement_p)
12987 tree l = finish_case_label (token->location, expr, expr_hi);
12988 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
12990 label = CASE_LABEL (l);
12991 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
12994 else
12995 error_at (token->location,
12996 "case label %qE not within a switch statement",
12997 expr);
12999 break;
13001 case RID_DEFAULT:
13002 /* Consume the `default' token. */
13003 cp_lexer_consume_token (parser->lexer);
13005 if (parser->in_switch_statement_p)
13007 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
13008 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
13010 label = CASE_LABEL (l);
13011 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
13014 else
13015 error_at (token->location, "case label not within a switch statement");
13016 break;
13018 default:
13019 /* Anything else must be an ordinary label. */
13020 label = finish_label_stmt (cp_parser_identifier (parser));
13021 if (label && TREE_CODE (label) == LABEL_DECL)
13022 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
13023 break;
13026 /* Require the `:' token. */
13027 cp_parser_require (parser, CPP_COLON, RT_COLON);
13029 /* An ordinary label may optionally be followed by attributes.
13030 However, this is only permitted if the attributes are then
13031 followed by a semicolon. This is because, for backward
13032 compatibility, when parsing
13033 lab: __attribute__ ((unused)) int i;
13034 we want the attribute to attach to "i", not "lab". */
13035 if (label != NULL_TREE
13036 && cp_next_tokens_can_be_gnu_attribute_p (parser))
13038 tree attrs;
13039 cp_parser_parse_tentatively (parser);
13040 attrs = cp_parser_gnu_attributes_opt (parser);
13041 if (attrs == NULL_TREE
13042 /* And fallthrough always binds to the expression-statement. */
13043 || attribute_fallthrough_p (attrs)
13044 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13045 cp_parser_abort_tentative_parse (parser);
13046 else if (!cp_parser_parse_definitely (parser))
13048 else
13049 attributes = attr_chainon (attributes, attrs);
13052 if (attributes != NULL_TREE)
13053 cplus_decl_attributes (&label, attributes, 0);
13055 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
13058 /* Parse an expression-statement.
13060 expression-statement:
13061 expression [opt] ;
13063 Returns the new EXPR_STMT -- or NULL_TREE if the expression
13064 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
13065 indicates whether this expression-statement is part of an
13066 expression statement. */
13068 static tree
13069 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
13071 tree statement = NULL_TREE;
13072 cp_token *token = cp_lexer_peek_token (parser->lexer);
13073 location_t loc = token->location;
13075 /* There might be attribute fallthrough. */
13076 tree attr = cp_parser_gnu_attributes_opt (parser);
13078 /* If the next token is a ';', then there is no expression
13079 statement. */
13080 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13082 statement = cp_parser_expression (parser);
13083 if (statement == error_mark_node
13084 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
13086 /* If we ran into a problem, make sure we complained. */
13087 gcc_assert (seen_error ());
13089 cp_parser_skip_to_end_of_block_or_statement (parser);
13090 return error_mark_node;
13094 attr = process_stmt_assume_attribute (attr, statement, loc);
13096 /* Handle [[fallthrough]];. */
13097 if (attribute_fallthrough_p (attr))
13099 /* The next token after the fallthrough attribute is ';'. */
13100 if (statement == NULL_TREE)
13101 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
13102 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
13103 void_type_node, 0);
13104 else
13105 warning_at (loc, OPT_Wattributes,
13106 "%<fallthrough%> attribute not followed by %<;%>");
13107 attr = NULL_TREE;
13110 /* Allow "[[fallthrough]];", but warn otherwise. */
13111 if (attr != NULL_TREE && any_nonignored_attribute_p (attr))
13112 warning_at (loc, OPT_Wattributes,
13113 "attributes at the beginning of statement are ignored");
13115 /* Give a helpful message for "A<T>::type t;" and the like. */
13116 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
13117 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
13119 if (TREE_CODE (statement) == SCOPE_REF)
13120 error_at (token->location, "need %<typename%> before %qE because "
13121 "%qT is a dependent scope",
13122 statement, TREE_OPERAND (statement, 0));
13123 else if (is_overloaded_fn (statement)
13124 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
13126 /* A::A a; */
13127 tree fn = get_first_fn (statement);
13128 error_at (token->location,
13129 "%<%T::%D%> names the constructor, not the type",
13130 DECL_CONTEXT (fn), DECL_NAME (fn));
13134 /* Consume the final `;'. */
13135 cp_parser_consume_semicolon_at_end_of_statement (parser);
13137 if (in_statement_expr
13138 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
13139 /* This is the final expression statement of a statement
13140 expression. */
13141 statement = finish_stmt_expr_expr (statement, in_statement_expr);
13142 else if (statement)
13143 statement = finish_expr_stmt (statement);
13145 return statement;
13148 /* Parse a compound-statement.
13150 compound-statement:
13151 { statement-seq [opt] label-seq [opt] }
13153 label-seq:
13154 label
13155 label-seq label
13157 GNU extension:
13159 compound-statement:
13160 { label-declaration-seq [opt] statement-seq [opt] }
13162 label-declaration-seq:
13163 label-declaration
13164 label-declaration-seq label-declaration
13166 Returns a tree representing the statement. */
13168 static tree
13169 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
13170 int bcs_flags, bool function_body)
13172 tree compound_stmt;
13173 matching_braces braces;
13175 /* Consume the `{'. */
13176 if (!braces.require_open (parser))
13177 return error_mark_node;
13178 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
13179 && !function_body && cxx_dialect < cxx14)
13180 pedwarn (input_location, OPT_Wpedantic,
13181 "compound-statement in %<constexpr%> function");
13182 /* Begin the compound-statement. */
13183 compound_stmt = begin_compound_stmt (bcs_flags);
13184 /* If the next keyword is `__label__' we have a label declaration. */
13185 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
13186 cp_parser_label_declaration (parser);
13187 /* Parse an (optional) statement-seq. */
13188 cp_parser_statement_seq_opt (parser, in_statement_expr);
13190 /* Consume the `}'. */
13191 braces.require_close (parser);
13193 /* Finish the compound-statement. */
13194 finish_compound_stmt (compound_stmt);
13196 return compound_stmt;
13199 /* Diagnose errors related to imperfectly nested loops in an OMP
13200 loop construct. This function is called when such code is seen.
13201 Only issue one such diagnostic no matter how much invalid
13202 intervening code there is in the loop.
13203 FIXME: maybe the location associated with the diagnostic should
13204 be the current parser token instead of the location of the outer loop
13205 nest. */
13207 static void
13208 check_omp_intervening_code (cp_parser *parser)
13210 struct omp_for_parse_data *omp_for_parse_state
13211 = parser->omp_for_parse_state;
13212 gcc_assert (omp_for_parse_state);
13214 if (!omp_for_parse_state->in_intervening_code)
13215 return;
13216 omp_for_parse_state->saw_intervening_code = true;
13218 /* Only diagnose errors related to perfect nesting once. */
13219 if (!omp_for_parse_state->perfect_nesting_fail)
13221 if (omp_for_parse_state->code == OACC_LOOP)
13223 error_at (omp_for_parse_state->for_loc,
13224 "inner loops must be perfectly nested in "
13225 "%<#pragma acc loop%>");
13226 omp_for_parse_state->perfect_nesting_fail = true;
13228 else if (omp_for_parse_state->ordered)
13230 error_at (omp_for_parse_state->for_loc,
13231 "inner loops must be perfectly nested with "
13232 "%<ordered%> clause");
13233 omp_for_parse_state->perfect_nesting_fail = true;
13235 else if (omp_for_parse_state->inscan)
13237 error_at (omp_for_parse_state->for_loc,
13238 "inner loops must be perfectly nested with "
13239 "%<reduction%> %<inscan%> clause");
13240 omp_for_parse_state->perfect_nesting_fail = true;
13242 /* TODO: Also reject loops with TILE directive. */
13243 if (omp_for_parse_state->perfect_nesting_fail)
13244 omp_for_parse_state->fail = true;
13248 /* Parse an (optional) statement-seq.
13250 statement-seq:
13251 statement
13252 statement-seq [opt] statement */
13254 static void
13255 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
13257 struct omp_for_parse_data *omp_for_parse_state
13258 = parser->omp_for_parse_state;
13259 bool in_omp_loop_block
13260 = omp_for_parse_state ? omp_for_parse_state->want_nested_loop : false;
13262 /* Scan statements until there aren't any more. */
13263 while (true)
13265 cp_token *token = cp_lexer_peek_token (parser->lexer);
13267 /* If we are looking at a `}', then we have run out of
13268 statements; the same is true if we have reached the end
13269 of file, or have stumbled upon a stray '@end'. */
13270 if (token->type == CPP_CLOSE_BRACE
13271 || token->type == CPP_EOF
13272 || token->type == CPP_PRAGMA_EOL
13273 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
13274 break;
13276 /* If we are in a compound statement and find 'else' then
13277 something went wrong. */
13278 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
13280 if (parser->in_statement & IN_IF_STMT)
13281 break;
13282 else
13284 token = cp_lexer_consume_token (parser->lexer);
13285 error_at (token->location, "%<else%> without a previous %<if%>");
13289 /* Handle special cases for OMP FOR canonical loop syntax. */
13290 else if (in_omp_loop_block)
13292 bool want_nested_loop = omp_for_parse_state->want_nested_loop;
13293 if (want_nested_loop
13294 && token->type == CPP_KEYWORD && token->keyword == RID_FOR)
13296 /* Found the nested loop. */
13297 omp_for_parse_state->depth++;
13298 add_stmt (cp_parser_omp_loop_nest (parser, NULL));
13299 omp_for_parse_state->depth--;
13301 else if (token->type == CPP_SEMICOLON)
13303 /* Prior to implementing the OpenMP 5.1 syntax for canonical
13304 loop form, GCC used to accept an empty statements as not
13305 being intervening code. Continue to do that, as an
13306 extension. */
13307 /* FIXME: Maybe issue a warning or something here? */
13308 cp_lexer_consume_token (parser->lexer);
13310 else if (want_nested_loop && token->type == CPP_OPEN_BRACE)
13311 /* The nested compound statement may contain the next loop, or
13312 it might just be intervening code. */
13314 cp_parser_statement (parser, in_statement_expr, true, NULL);
13315 if (omp_for_parse_state->want_nested_loop)
13316 check_omp_intervening_code (parser);
13318 else
13320 /* This must be intervening code. */
13321 omp_for_parse_state->want_nested_loop = false;
13322 /* Defer calling check_omp_intervening_code on pragmas until
13323 cp_parser_statement, because we can't know until we parse
13324 it whether or not the pragma is a statement. */
13325 if (token->type != CPP_PRAGMA)
13326 check_omp_intervening_code (parser);
13327 cp_parser_statement (parser, in_statement_expr, true, NULL);
13328 omp_for_parse_state->want_nested_loop = want_nested_loop;
13330 continue;
13333 /* Parse the statement. */
13334 cp_parser_statement (parser, in_statement_expr, true, NULL);
13338 /* Return true if this is the C++20 version of range-based-for with
13339 init-statement. */
13341 static bool
13342 cp_parser_range_based_for_with_init_p (cp_parser *parser)
13344 bool r = false;
13346 /* Save tokens so that we can put them back. */
13347 cp_lexer_save_tokens (parser->lexer);
13349 /* There has to be an unnested ; followed by an unnested :. */
13350 if (cp_parser_skip_to_closing_parenthesis_1 (parser,
13351 /*recovering=*/false,
13352 CPP_SEMICOLON,
13353 /*consume_paren=*/false) != -1)
13354 goto out;
13356 /* We found the semicolon, eat it now. */
13357 cp_lexer_consume_token (parser->lexer);
13359 /* Now look for ':' that is not nested in () or {}. */
13360 r = (cp_parser_skip_to_closing_parenthesis_1 (parser,
13361 /*recovering=*/false,
13362 CPP_COLON,
13363 /*consume_paren=*/false) == -1);
13365 out:
13366 /* Roll back the tokens we skipped. */
13367 cp_lexer_rollback_tokens (parser->lexer);
13369 return r;
13372 /* Return true if we're looking at (init; cond), false otherwise. */
13374 static bool
13375 cp_parser_init_statement_p (cp_parser *parser)
13377 /* Save tokens so that we can put them back. */
13378 cp_lexer_save_tokens (parser->lexer);
13380 /* Look for ';' that is not nested in () or {}. */
13381 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
13382 /*recovering=*/false,
13383 CPP_SEMICOLON,
13384 /*consume_paren=*/false);
13386 /* Roll back the tokens we skipped. */
13387 cp_lexer_rollback_tokens (parser->lexer);
13389 return ret == -1;
13392 /* Parse a selection-statement.
13394 selection-statement:
13395 if ( init-statement [opt] condition ) statement
13396 if ( init-statement [opt] condition ) statement else statement
13397 switch ( init-statement [opt] condition ) statement
13399 Returns the new IF_STMT or SWITCH_STMT.
13401 If IF_P is not NULL, *IF_P is set to indicate whether the statement
13402 is a (possibly labeled) if statement which is not enclosed in
13403 braces and has an else clause. This is used to implement
13404 -Wparentheses.
13406 CHAIN is a vector of if-else-if conditions. This is used to implement
13407 -Wduplicated-cond. */
13409 static tree
13410 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
13411 vec<tree> *chain)
13413 cp_token *token;
13414 enum rid keyword;
13415 token_indent_info guard_tinfo;
13417 if (if_p != NULL)
13418 *if_p = false;
13420 /* Peek at the next token. */
13421 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
13422 guard_tinfo = get_token_indent_info (token);
13424 /* See what kind of keyword it is. */
13425 keyword = token->keyword;
13426 switch (keyword)
13428 case RID_IF:
13429 case RID_SWITCH:
13431 tree statement;
13432 tree condition;
13434 bool cx = false;
13435 if (keyword == RID_IF
13436 && cp_lexer_next_token_is_keyword (parser->lexer,
13437 RID_CONSTEXPR))
13439 cx = true;
13440 cp_token *tok = cp_lexer_consume_token (parser->lexer);
13441 if (cxx_dialect < cxx17)
13442 pedwarn (tok->location, OPT_Wc__17_extensions,
13443 "%<if constexpr%> only available with "
13444 "%<-std=c++17%> or %<-std=gnu++17%>");
13446 int ce = 0;
13447 if (keyword == RID_IF && !cx)
13449 if (cp_lexer_next_token_is_keyword (parser->lexer,
13450 RID_CONSTEVAL))
13451 ce = 1;
13452 else if (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
13453 && cp_lexer_nth_token_is_keyword (parser->lexer, 2,
13454 RID_CONSTEVAL))
13456 ce = -1;
13457 cp_lexer_consume_token (parser->lexer);
13460 if (ce)
13462 cp_token *tok = cp_lexer_consume_token (parser->lexer);
13463 if (cxx_dialect < cxx23)
13464 pedwarn (tok->location, OPT_Wc__23_extensions,
13465 "%<if consteval%> only available with "
13466 "%<-std=c++2b%> or %<-std=gnu++2b%>");
13468 bool save_in_consteval_if_p = in_consteval_if_p;
13469 statement = begin_if_stmt ();
13470 IF_STMT_CONSTEVAL_P (statement) = true;
13471 condition = finish_if_stmt_cond (boolean_false_node, statement);
13473 gcc_rich_location richloc (tok->location);
13474 bool non_compound_stmt_p = false;
13475 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13477 non_compound_stmt_p = true;
13478 richloc.add_fixit_insert_after (tok->location, "{");
13481 in_consteval_if_p |= ce > 0;
13482 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
13484 if (non_compound_stmt_p)
13486 location_t before_loc
13487 = cp_lexer_peek_token (parser->lexer)->location;
13488 richloc.add_fixit_insert_before (before_loc, "}");
13489 error_at (&richloc,
13490 "%<if consteval%> requires compound statement");
13491 non_compound_stmt_p = false;
13494 finish_then_clause (statement);
13496 /* If the next token is `else', parse the else-clause. */
13497 if (cp_lexer_next_token_is_keyword (parser->lexer,
13498 RID_ELSE))
13500 cp_token *else_tok = cp_lexer_peek_token (parser->lexer);
13501 gcc_rich_location else_richloc (else_tok->location);
13502 guard_tinfo = get_token_indent_info (else_tok);
13503 /* Consume the `else' keyword. */
13504 cp_lexer_consume_token (parser->lexer);
13506 begin_else_clause (statement);
13508 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13510 non_compound_stmt_p = true;
13511 else_richloc.add_fixit_insert_after (else_tok->location,
13512 "{");
13515 in_consteval_if_p = save_in_consteval_if_p | (ce < 0);
13516 cp_parser_implicitly_scoped_statement (parser, NULL,
13517 guard_tinfo);
13519 if (non_compound_stmt_p)
13521 location_t before_loc
13522 = cp_lexer_peek_token (parser->lexer)->location;
13523 else_richloc.add_fixit_insert_before (before_loc, "}");
13524 error_at (&else_richloc,
13525 "%<if consteval%> requires compound statement");
13528 finish_else_clause (statement);
13531 in_consteval_if_p = save_in_consteval_if_p;
13532 if (ce < 0)
13534 std::swap (THEN_CLAUSE (statement), ELSE_CLAUSE (statement));
13535 if (THEN_CLAUSE (statement) == NULL_TREE)
13536 THEN_CLAUSE (statement) = build_empty_stmt (tok->location);
13539 finish_if_stmt (statement);
13540 return statement;
13543 /* Look for the `('. */
13544 matching_parens parens;
13545 if (!parens.require_open (parser))
13547 cp_parser_skip_to_end_of_statement (parser);
13548 return error_mark_node;
13551 /* Begin the selection-statement. */
13552 if (keyword == RID_IF)
13554 statement = begin_if_stmt ();
13555 IF_STMT_CONSTEXPR_P (statement) = cx;
13557 else
13558 statement = begin_switch_stmt ();
13560 /* Parse the optional init-statement. */
13561 if (cp_parser_init_statement_p (parser))
13563 tree decl;
13564 if (cxx_dialect < cxx17)
13565 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
13566 OPT_Wc__17_extensions,
13567 "init-statement in selection statements only available "
13568 "with %<-std=c++17%> or %<-std=gnu++17%>");
13569 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13570 /* A non-empty init-statement can have arbitrary side
13571 effects. */
13572 vec_free (chain);
13573 cp_parser_init_statement (parser, &decl);
13576 /* Parse the condition. */
13577 condition = cp_parser_condition (parser);
13578 /* Look for the `)'. */
13579 if (!parens.require_close (parser))
13580 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13581 /*consume_paren=*/true);
13583 if (keyword == RID_IF)
13585 bool nested_if;
13586 unsigned char in_statement;
13588 /* Add the condition. */
13589 condition = finish_if_stmt_cond (condition, statement);
13591 if (warn_duplicated_cond)
13592 warn_duplicated_cond_add_or_warn (token->location, condition,
13593 &chain);
13595 /* Parse the then-clause. */
13596 in_statement = parser->in_statement;
13597 parser->in_statement |= IN_IF_STMT;
13599 /* Outside a template, the non-selected branch of a constexpr
13600 if is a 'discarded statement', i.e. unevaluated. */
13601 bool was_discarded = in_discarded_stmt;
13602 bool discard_then = (cx && !processing_template_decl
13603 && integer_zerop (condition));
13604 if (discard_then)
13606 in_discarded_stmt = true;
13607 ++c_inhibit_evaluation_warnings;
13610 cp_parser_implicitly_scoped_statement (parser, &nested_if,
13611 guard_tinfo);
13613 parser->in_statement = in_statement;
13615 finish_then_clause (statement);
13617 if (discard_then)
13619 THEN_CLAUSE (statement) = NULL_TREE;
13620 in_discarded_stmt = was_discarded;
13621 --c_inhibit_evaluation_warnings;
13624 /* If the next token is `else', parse the else-clause. */
13625 if (cp_lexer_next_token_is_keyword (parser->lexer,
13626 RID_ELSE))
13628 bool discard_else = (cx && !processing_template_decl
13629 && integer_nonzerop (condition));
13630 if (discard_else)
13632 in_discarded_stmt = true;
13633 ++c_inhibit_evaluation_warnings;
13636 guard_tinfo
13637 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13638 /* Consume the `else' keyword. */
13639 cp_lexer_consume_token (parser->lexer);
13640 if (warn_duplicated_cond)
13642 if (cp_lexer_next_token_is_keyword (parser->lexer,
13643 RID_IF)
13644 && chain == NULL)
13646 /* We've got "if (COND) else if (COND2)". Start
13647 the condition chain and add COND as the first
13648 element. */
13649 chain = new vec<tree> ();
13650 if (!CONSTANT_CLASS_P (condition)
13651 && !TREE_SIDE_EFFECTS (condition))
13653 /* Wrap it in a NOP_EXPR so that we can set the
13654 location of the condition. */
13655 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
13656 condition);
13657 SET_EXPR_LOCATION (e, token->location);
13658 chain->safe_push (e);
13661 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
13662 RID_IF))
13663 /* This is if-else without subsequent if. Zap the
13664 condition chain; we would have already warned at
13665 this point. */
13666 vec_free (chain);
13668 begin_else_clause (statement);
13669 /* Parse the else-clause. */
13670 cp_parser_implicitly_scoped_statement (parser, NULL,
13671 guard_tinfo, chain);
13673 finish_else_clause (statement);
13675 /* If we are currently parsing a then-clause, then
13676 IF_P will not be NULL. We set it to true to
13677 indicate that this if statement has an else clause.
13678 This may trigger the Wparentheses warning below
13679 when we get back up to the parent if statement. */
13680 if (if_p != NULL)
13681 *if_p = true;
13683 if (discard_else)
13685 ELSE_CLAUSE (statement) = NULL_TREE;
13686 in_discarded_stmt = was_discarded;
13687 --c_inhibit_evaluation_warnings;
13690 else
13692 /* This if statement does not have an else clause. If
13693 NESTED_IF is true, then the then-clause has an if
13694 statement which does have an else clause. We warn
13695 about the potential ambiguity. */
13696 if (nested_if)
13697 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
13698 "suggest explicit braces to avoid ambiguous"
13699 " %<else%>");
13700 if (warn_duplicated_cond)
13701 /* We don't need the condition chain anymore. */
13702 vec_free (chain);
13705 /* Now we're all done with the if-statement. */
13706 finish_if_stmt (statement);
13708 else
13710 bool in_switch_statement_p;
13711 unsigned char in_statement;
13713 /* Add the condition. */
13714 finish_switch_cond (condition, statement);
13716 /* Parse the body of the switch-statement. */
13717 in_switch_statement_p = parser->in_switch_statement_p;
13718 in_statement = parser->in_statement;
13719 parser->in_switch_statement_p = true;
13720 parser->in_statement |= IN_SWITCH_STMT;
13721 cp_parser_implicitly_scoped_statement (parser, if_p,
13722 guard_tinfo);
13723 parser->in_switch_statement_p = in_switch_statement_p;
13724 parser->in_statement = in_statement;
13726 /* Now we're all done with the switch-statement. */
13727 finish_switch_stmt (statement);
13730 return statement;
13732 break;
13734 default:
13735 cp_parser_error (parser, "expected selection-statement");
13736 return error_mark_node;
13740 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
13741 If we have seen at least one decl-specifier, and the next token is not
13742 a parenthesis (after "int (" we might be looking at a functional cast)
13743 neither we are dealing with a concept-check expression then we must be
13744 looking at a declaration. */
13746 static void
13747 cp_parser_maybe_commit_to_declaration (cp_parser* parser,
13748 cp_decl_specifier_seq *decl_specs)
13750 if (decl_specs->any_specifiers_p
13751 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
13752 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
13753 && !cp_parser_error_occurred (parser)
13754 && !(decl_specs->type
13755 && TREE_CODE (decl_specs->type) == TYPE_DECL
13756 && is_constrained_auto (TREE_TYPE (decl_specs->type))))
13757 cp_parser_commit_to_tentative_parse (parser);
13760 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
13761 The declarator shall not specify a function or an array. Returns
13762 TRUE if the declarator is valid, FALSE otherwise. */
13764 static bool
13765 cp_parser_check_condition_declarator (cp_parser* parser,
13766 cp_declarator *declarator,
13767 location_t loc)
13769 if (declarator == cp_error_declarator
13770 || function_declarator_p (declarator)
13771 || declarator->kind == cdk_array)
13773 if (declarator == cp_error_declarator)
13774 /* Already complained. */;
13775 else if (declarator->kind == cdk_array)
13776 error_at (loc, "condition declares an array");
13777 else
13778 error_at (loc, "condition declares a function");
13779 if (parser->fully_implicit_function_template_p)
13780 abort_fully_implicit_template (parser);
13781 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
13782 /*or_comma=*/false,
13783 /*consume_paren=*/false);
13784 return false;
13786 else
13787 return true;
13790 /* Parse a condition.
13792 condition:
13793 expression
13794 type-specifier-seq declarator = initializer-clause
13795 type-specifier-seq declarator braced-init-list
13797 GNU Extension:
13799 condition:
13800 type-specifier-seq declarator asm-specification [opt]
13801 attributes [opt] = assignment-expression
13803 Returns the expression that should be tested. */
13805 static tree
13806 cp_parser_condition (cp_parser* parser)
13808 cp_decl_specifier_seq type_specifiers;
13809 const char *saved_message;
13810 int declares_class_or_enum;
13812 /* Try the declaration first. */
13813 cp_parser_parse_tentatively (parser);
13814 /* New types are not allowed in the type-specifier-seq for a
13815 condition. */
13816 saved_message = parser->type_definition_forbidden_message;
13817 parser->type_definition_forbidden_message
13818 = G_("types may not be defined in conditions");
13819 /* Parse the type-specifier-seq. */
13820 cp_parser_decl_specifier_seq (parser,
13821 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
13822 &type_specifiers,
13823 &declares_class_or_enum);
13824 /* Restore the saved message. */
13825 parser->type_definition_forbidden_message = saved_message;
13827 /* Gather the attributes that were provided with the
13828 decl-specifiers. */
13829 tree prefix_attributes = type_specifiers.attributes;
13831 cp_parser_maybe_commit_to_declaration (parser, &type_specifiers);
13833 /* If all is well, we might be looking at a declaration. */
13834 if (!cp_parser_error_occurred (parser))
13836 tree decl;
13837 tree asm_specification;
13838 tree attributes;
13839 cp_declarator *declarator;
13840 tree initializer = NULL_TREE;
13841 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13843 /* Parse the declarator. */
13844 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13845 CP_PARSER_FLAGS_NONE,
13846 /*ctor_dtor_or_conv_p=*/NULL,
13847 /*parenthesized_p=*/NULL,
13848 /*member_p=*/false,
13849 /*friend_p=*/false,
13850 /*static_p=*/false);
13851 /* Parse the attributes. */
13852 attributes = cp_parser_attributes_opt (parser);
13853 /* Parse the asm-specification. */
13854 asm_specification = cp_parser_asm_specification_opt (parser);
13855 /* If the next token is not an `=' or '{', then we might still be
13856 looking at an expression. For example:
13858 if (A(a).x)
13860 looks like a decl-specifier-seq and a declarator -- but then
13861 there is no `=', so this is an expression. */
13862 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
13863 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13864 cp_parser_simulate_error (parser);
13866 /* If we did see an `=' or '{', then we are looking at a declaration
13867 for sure. */
13868 if (cp_parser_parse_definitely (parser))
13870 tree pushed_scope;
13871 bool non_constant_p = false;
13872 int flags = LOOKUP_ONLYCONVERTING;
13874 if (!cp_parser_check_condition_declarator (parser, declarator, loc))
13875 return error_mark_node;
13877 /* Create the declaration. */
13878 decl = start_decl (declarator, &type_specifiers,
13879 /*initialized_p=*/true,
13880 attributes, prefix_attributes,
13881 &pushed_scope);
13883 declarator->init_loc = cp_lexer_peek_token (parser->lexer)->location;
13884 /* Parse the initializer. */
13885 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13887 initializer = cp_parser_braced_list (parser, &non_constant_p);
13888 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
13889 flags = 0;
13891 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13893 /* Consume the `='. */
13894 cp_lexer_consume_token (parser->lexer);
13895 initializer = cp_parser_initializer_clause (parser,
13896 &non_constant_p);
13898 else
13900 cp_parser_error (parser, "expected initializer");
13901 initializer = error_mark_node;
13903 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
13904 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
13906 /* Process the initializer. */
13907 cp_finish_decl (decl,
13908 initializer, !non_constant_p,
13909 asm_specification,
13910 flags);
13912 if (pushed_scope)
13913 pop_scope (pushed_scope);
13915 return convert_from_reference (decl);
13918 /* If we didn't even get past the declarator successfully, we are
13919 definitely not looking at a declaration. */
13920 else
13921 cp_parser_abort_tentative_parse (parser);
13923 /* Otherwise, we are looking at an expression. */
13924 return cp_parser_expression (parser);
13927 /* Parses a for-statement or range-for-statement until the closing ')',
13928 not included. */
13930 static tree
13931 cp_parser_for (cp_parser *parser, bool ivdep, tree unroll, bool novector)
13933 tree init, scope, decl;
13934 bool is_range_for;
13936 /* Begin the for-statement. */
13937 scope = begin_for_scope (&init);
13939 /* Maybe parse the optional init-statement in a range-based for loop. */
13940 if (cp_parser_range_based_for_with_init_p (parser)
13941 /* Checked for diagnostic purposes only. */
13942 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13944 tree dummy;
13945 cp_parser_init_statement (parser, &dummy);
13946 if (cxx_dialect < cxx20)
13948 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
13949 OPT_Wc__20_extensions,
13950 "range-based %<for%> loops with initializer only "
13951 "available with %<-std=c++20%> or %<-std=gnu++20%>");
13952 decl = error_mark_node;
13956 /* Parse the initialization. */
13957 is_range_for = cp_parser_init_statement (parser, &decl);
13959 if (is_range_for)
13960 return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll,
13961 novector, false);
13962 else
13963 return cp_parser_c_for (parser, scope, init, ivdep, unroll, novector);
13966 static tree
13967 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
13968 tree unroll, bool novector)
13970 /* Normal for loop */
13971 tree condition = NULL_TREE;
13972 tree expression = NULL_TREE;
13973 tree stmt;
13975 stmt = begin_for_stmt (scope, init);
13976 /* The init-statement has already been parsed in
13977 cp_parser_init_statement, so no work is needed here. */
13978 finish_init_stmt (stmt);
13980 /* If there's a condition, process it. */
13981 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13982 condition = cp_parser_condition (parser);
13983 else if (ivdep)
13985 cp_parser_error (parser, "missing loop condition in loop with "
13986 "%<GCC ivdep%> pragma");
13987 condition = error_mark_node;
13989 else if (unroll)
13991 cp_parser_error (parser, "missing loop condition in loop with "
13992 "%<GCC unroll%> pragma");
13993 condition = error_mark_node;
13995 finish_for_cond (condition, stmt, ivdep, unroll, novector);
13996 /* Look for the `;'. */
13997 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13999 /* If there's an expression, process it. */
14000 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
14001 expression = cp_parser_expression (parser);
14002 finish_for_expr (expression, stmt);
14004 return stmt;
14007 /* Tries to parse a range-based for-statement:
14009 range-based-for:
14010 decl-specifier-seq declarator : expression
14012 The decl-specifier-seq declarator and the `:' are already parsed by
14013 cp_parser_init_statement. If processing_template_decl it returns a
14014 newly created RANGE_FOR_STMT; if not, it is converted to a
14015 regular FOR_STMT. */
14017 static tree
14018 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
14019 bool ivdep, tree unroll, bool novector, bool is_omp)
14021 tree stmt, range_expr;
14022 auto_vec <cxx_binding *, 16> bindings;
14023 auto_vec <tree, 16> names;
14024 cp_decomp decomp_d, *decomp = NULL;
14026 /* Get the range declaration momentarily out of the way so that
14027 the range expression doesn't clash with it. */
14028 if (range_decl != error_mark_node)
14030 if (DECL_HAS_VALUE_EXPR_P (range_decl))
14032 tree v = DECL_VALUE_EXPR (range_decl);
14033 /* For decomposition declaration get all of the corresponding
14034 declarations out of the way. */
14035 if (TREE_CODE (v) == ARRAY_REF
14036 && VAR_P (TREE_OPERAND (v, 0))
14037 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
14039 tree d = range_decl;
14040 range_decl = TREE_OPERAND (v, 0);
14041 decomp = &decomp_d;
14042 decomp->count = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
14043 decomp->decl = d;
14044 for (unsigned int i = 0; i < decomp->count;
14045 i++, d = DECL_CHAIN (d))
14047 tree name = DECL_NAME (d);
14048 names.safe_push (name);
14049 bindings.safe_push (IDENTIFIER_BINDING (name));
14050 IDENTIFIER_BINDING (name)
14051 = IDENTIFIER_BINDING (name)->previous;
14055 if (names.is_empty ())
14057 tree name = DECL_NAME (range_decl);
14058 names.safe_push (name);
14059 bindings.safe_push (IDENTIFIER_BINDING (name));
14060 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
14064 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14065 range_expr = cp_parser_braced_list (parser);
14066 else
14067 range_expr = cp_parser_expression (parser);
14069 /* Put the range declaration(s) back into scope. */
14070 for (unsigned int i = 0; i < names.length (); i++)
14072 cxx_binding *binding = bindings[i];
14073 binding->previous = IDENTIFIER_BINDING (names[i]);
14074 IDENTIFIER_BINDING (names[i]) = binding;
14077 /* finish_omp_for has its own code for the following, so just
14078 return the range_expr instead. */
14079 if (is_omp)
14080 return range_expr;
14082 /* If in template, STMT is converted to a normal for-statement
14083 at instantiation. If not, it is done just ahead. */
14084 if (processing_template_decl)
14086 if (check_for_bare_parameter_packs (range_expr))
14087 range_expr = error_mark_node;
14088 stmt = begin_range_for_stmt (scope, init);
14089 if (ivdep)
14090 RANGE_FOR_IVDEP (stmt) = 1;
14091 if (unroll)
14092 RANGE_FOR_UNROLL (stmt) = unroll;
14093 if (novector)
14094 RANGE_FOR_NOVECTOR (stmt) = 1;
14095 finish_range_for_decl (stmt, range_decl, range_expr);
14096 if (!type_dependent_expression_p (range_expr)
14097 /* do_auto_deduction doesn't mess with template init-lists. */
14098 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
14099 do_range_for_auto_deduction (range_decl, range_expr, decomp);
14101 else
14103 stmt = begin_for_stmt (scope, init);
14104 stmt = cp_convert_range_for (stmt, range_decl, range_expr, decomp,
14105 ivdep, unroll, novector);
14107 return stmt;
14110 /* Subroutine of cp_convert_range_for: given the initializer expression,
14111 builds up the range temporary. */
14113 static tree
14114 build_range_temp (tree range_expr)
14116 /* Find out the type deduced by the declaration
14117 `auto &&__range = range_expr'. */
14118 tree auto_node = make_auto ();
14119 tree range_type = cp_build_reference_type (auto_node, true);
14120 range_type = do_auto_deduction (range_type, range_expr, auto_node);
14122 /* Create the __range variable. */
14123 tree range_temp = build_decl (input_location, VAR_DECL,
14124 for_range__identifier, range_type);
14125 TREE_USED (range_temp) = 1;
14126 DECL_ARTIFICIAL (range_temp) = 1;
14128 return range_temp;
14131 /* Used by cp_parser_range_for in template context: we aren't going to
14132 do a full conversion yet, but we still need to resolve auto in the
14133 type of the for-range-declaration if present. This is basically
14134 a shortcut version of cp_convert_range_for. */
14136 static void
14137 do_range_for_auto_deduction (tree decl, tree range_expr, cp_decomp *decomp)
14139 tree auto_node = type_uses_auto (TREE_TYPE (decl));
14140 if (auto_node)
14142 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
14143 range_temp = convert_from_reference (build_range_temp (range_expr));
14144 iter_type = (cp_parser_perform_range_for_lookup
14145 (range_temp, &begin_dummy, &end_dummy));
14146 if (iter_type)
14148 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
14149 iter_type);
14150 iter_decl = build_x_indirect_ref (input_location, iter_decl,
14151 RO_UNARY_STAR, NULL_TREE,
14152 tf_warning_or_error);
14153 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
14154 iter_decl, auto_node,
14155 tf_warning_or_error,
14156 adc_variable_type);
14157 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
14158 cp_finish_decomp (decl, decomp);
14163 /* Warns when the loop variable should be changed to a reference type to
14164 avoid unnecessary copying. I.e., from
14166 for (const auto x : range)
14168 where range returns a reference, to
14170 for (const auto &x : range)
14172 if this version doesn't make a copy.
14174 This function also warns when the loop variable is initialized with
14175 a value of a different type resulting in a copy:
14177 int arr[10];
14178 for (const double &x : arr)
14180 DECL is the RANGE_DECL; EXPR is the *__for_begin expression.
14181 This function is never called when processing_template_decl is on. */
14183 static void
14184 warn_for_range_copy (tree decl, tree expr)
14186 if (!warn_range_loop_construct
14187 || decl == error_mark_node)
14188 return;
14190 location_t loc = DECL_SOURCE_LOCATION (decl);
14191 tree type = TREE_TYPE (decl);
14193 if (from_macro_expansion_at (loc))
14194 return;
14196 if (TYPE_REF_P (type))
14198 if (glvalue_p (expr)
14199 && ref_conv_binds_to_temporary (type, expr).is_true ())
14201 auto_diagnostic_group d;
14202 if (warning_at (loc, OPT_Wrange_loop_construct,
14203 "loop variable %qD of type %qT binds to a temporary "
14204 "constructed from type %qT", decl, type,
14205 TREE_TYPE (expr)))
14207 tree ref = cp_build_qualified_type (TREE_TYPE (expr),
14208 TYPE_QUAL_CONST);
14209 ref = cp_build_reference_type (ref, /*rval*/false);
14210 inform (loc, "use non-reference type %qT to make the copy "
14211 "explicit or %qT to prevent copying",
14212 non_reference (type), ref);
14215 return;
14217 else if (!CP_TYPE_CONST_P (type))
14218 return;
14220 /* Since small trivially copyable types are cheap to copy, we suppress the
14221 warning for them. 64B is a common size of a cache line. */
14222 if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST
14223 || (tree_to_uhwi (TYPE_SIZE_UNIT (type)) <= 64
14224 && trivially_copyable_p (type)))
14225 return;
14227 /* If we can initialize a reference directly, suggest that to avoid the
14228 copy. */
14229 tree rtype = cp_build_reference_type (type, /*rval*/false);
14230 if (ref_conv_binds_to_temporary (rtype, expr).is_false ())
14232 auto_diagnostic_group d;
14233 if (warning_at (loc, OPT_Wrange_loop_construct,
14234 "loop variable %qD creates a copy from type %qT",
14235 decl, type))
14237 gcc_rich_location richloc (loc);
14238 richloc.add_fixit_insert_before ("&");
14239 inform (&richloc, "use reference type to prevent copying");
14244 /* Converts a range-based for-statement into a normal
14245 for-statement, as per the definition.
14247 for (RANGE_DECL : RANGE_EXPR)
14248 BLOCK
14250 should be equivalent to:
14253 auto &&__range = RANGE_EXPR;
14254 for (auto __begin = BEGIN_EXPR, __end = END_EXPR;
14255 __begin != __end;
14256 ++__begin)
14258 RANGE_DECL = *__begin;
14259 BLOCK
14263 If RANGE_EXPR is an array:
14264 BEGIN_EXPR = __range
14265 END_EXPR = __range + ARRAY_SIZE(__range)
14266 Else if RANGE_EXPR has a member 'begin' or 'end':
14267 BEGIN_EXPR = __range.begin()
14268 END_EXPR = __range.end()
14269 Else:
14270 BEGIN_EXPR = begin(__range)
14271 END_EXPR = end(__range);
14273 If __range has a member 'begin' but not 'end', or vice versa, we must
14274 still use the second alternative (it will surely fail, however).
14275 When calling begin()/end() in the third alternative we must use
14276 argument dependent lookup, but always considering 'std' as an associated
14277 namespace. */
14279 tree
14280 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
14281 cp_decomp *decomp, bool ivdep, tree unroll,
14282 bool novector)
14284 tree begin, end;
14285 tree iter_type, begin_expr, end_expr;
14286 tree condition, expression;
14288 range_expr = mark_lvalue_use (range_expr);
14290 if (range_decl == error_mark_node || range_expr == error_mark_node)
14291 /* If an error happened previously do nothing or else a lot of
14292 unhelpful errors would be issued. */
14293 begin_expr = end_expr = iter_type = error_mark_node;
14294 else
14296 tree range_temp;
14298 if (VAR_P (range_expr)
14299 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
14300 /* Can't bind a reference to an array of runtime bound. */
14301 range_temp = range_expr;
14302 else
14304 range_temp = build_range_temp (range_expr);
14305 pushdecl (range_temp);
14306 cp_finish_decl (range_temp, range_expr,
14307 /*is_constant_init*/false, NULL_TREE,
14308 LOOKUP_ONLYCONVERTING);
14309 range_temp = convert_from_reference (range_temp);
14311 iter_type = cp_parser_perform_range_for_lookup (range_temp,
14312 &begin_expr, &end_expr);
14315 /* The new for initialization statement. */
14316 begin = build_decl (input_location, VAR_DECL, for_begin__identifier,
14317 iter_type);
14318 TREE_USED (begin) = 1;
14319 DECL_ARTIFICIAL (begin) = 1;
14320 pushdecl (begin);
14321 cp_finish_decl (begin, begin_expr,
14322 /*is_constant_init*/false, NULL_TREE,
14323 LOOKUP_ONLYCONVERTING);
14325 if (cxx_dialect >= cxx17)
14326 iter_type = cv_unqualified (TREE_TYPE (end_expr));
14327 end = build_decl (input_location, VAR_DECL, for_end__identifier, iter_type);
14328 TREE_USED (end) = 1;
14329 DECL_ARTIFICIAL (end) = 1;
14330 pushdecl (end);
14331 cp_finish_decl (end, end_expr,
14332 /*is_constant_init*/false, NULL_TREE,
14333 LOOKUP_ONLYCONVERTING);
14335 finish_init_stmt (statement);
14337 /* The new for condition. */
14338 condition = build_x_binary_op (input_location, NE_EXPR,
14339 begin, ERROR_MARK,
14340 end, ERROR_MARK,
14341 NULL_TREE, NULL, tf_warning_or_error);
14342 finish_for_cond (condition, statement, ivdep, unroll, novector);
14344 /* The new increment expression. */
14345 expression = finish_unary_op_expr (input_location,
14346 PREINCREMENT_EXPR, begin,
14347 tf_warning_or_error);
14348 finish_for_expr (expression, statement);
14350 /* The declaration is initialized with *__begin inside the loop body. */
14351 tree deref_begin = build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
14352 NULL_TREE, tf_warning_or_error);
14353 cp_finish_decl (range_decl, deref_begin,
14354 /*is_constant_init*/false, NULL_TREE,
14355 LOOKUP_ONLYCONVERTING, decomp);
14356 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
14357 cp_finish_decomp (range_decl, decomp);
14359 warn_for_range_copy (range_decl, deref_begin);
14361 return statement;
14364 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
14365 We need to solve both at the same time because the method used
14366 depends on the existence of members begin or end.
14367 Returns the type deduced for the iterator expression. */
14369 static tree
14370 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
14372 if (error_operand_p (range))
14374 *begin = *end = error_mark_node;
14375 return error_mark_node;
14378 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
14380 error ("range-based %<for%> expression of type %qT "
14381 "has incomplete type", TREE_TYPE (range));
14382 *begin = *end = error_mark_node;
14383 return error_mark_node;
14385 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
14387 /* If RANGE is an array, we will use pointer arithmetic. */
14388 *begin = decay_conversion (range, tf_warning_or_error);
14389 *end = build_binary_op (input_location, PLUS_EXPR,
14390 range,
14391 array_type_nelts_top (TREE_TYPE (range)),
14392 false);
14393 return TREE_TYPE (*begin);
14395 else
14397 /* If it is not an array, we must do a bit of magic. */
14398 tree id_begin, id_end;
14399 tree member_begin, member_end;
14401 *begin = *end = error_mark_node;
14403 id_begin = get_identifier ("begin");
14404 id_end = get_identifier ("end");
14405 member_begin = lookup_member (TREE_TYPE (range), id_begin,
14406 /*protect=*/2, /*want_type=*/false,
14407 tf_warning_or_error);
14408 member_end = lookup_member (TREE_TYPE (range), id_end,
14409 /*protect=*/2, /*want_type=*/false,
14410 tf_warning_or_error);
14412 if (member_begin != NULL_TREE && member_end != NULL_TREE)
14414 /* Use the member functions. */
14415 *begin = cp_parser_range_for_member_function (range, id_begin);
14416 *end = cp_parser_range_for_member_function (range, id_end);
14418 else
14420 /* Use global functions with ADL. */
14421 releasing_vec vec;
14423 vec_safe_push (vec, range);
14425 member_begin = perform_koenig_lookup (id_begin, vec,
14426 tf_warning_or_error);
14427 *begin = finish_call_expr (member_begin, &vec, false, true,
14428 tf_warning_or_error);
14429 member_end = perform_koenig_lookup (id_end, vec,
14430 tf_warning_or_error);
14431 *end = finish_call_expr (member_end, &vec, false, true,
14432 tf_warning_or_error);
14435 /* Last common checks. */
14436 if (*begin == error_mark_node || *end == error_mark_node)
14438 /* If one of the expressions is an error do no more checks. */
14439 *begin = *end = error_mark_node;
14440 return error_mark_node;
14442 else if (type_dependent_expression_p (*begin)
14443 || type_dependent_expression_p (*end))
14444 /* Can happen, when, eg, in a template context, Koenig lookup
14445 can't resolve begin/end (c++/58503). */
14446 return NULL_TREE;
14447 else
14449 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
14450 /* The unqualified type of the __begin and __end temporaries should
14451 be the same, as required by the multiple auto declaration. */
14452 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
14454 if (cxx_dialect >= cxx17
14455 && (build_x_binary_op (input_location, NE_EXPR,
14456 *begin, ERROR_MARK,
14457 *end, ERROR_MARK,
14458 NULL_TREE, NULL, tf_none)
14459 != error_mark_node))
14460 /* P0184R0 allows __begin and __end to have different types,
14461 but make sure they are comparable so we can give a better
14462 diagnostic. */;
14463 else
14464 error ("inconsistent begin/end types in range-based %<for%> "
14465 "statement: %qT and %qT",
14466 TREE_TYPE (*begin), TREE_TYPE (*end));
14468 return iter_type;
14473 /* Helper function for cp_parser_perform_range_for_lookup.
14474 Builds a tree for RANGE.IDENTIFIER(). */
14476 static tree
14477 cp_parser_range_for_member_function (tree range, tree identifier)
14479 tree member, res;
14481 member = finish_class_member_access_expr (range, identifier,
14482 false, tf_warning_or_error);
14483 if (member == error_mark_node)
14484 return error_mark_node;
14486 releasing_vec vec;
14487 res = finish_call_expr (member, &vec,
14488 /*disallow_virtual=*/false,
14489 /*koenig_p=*/false,
14490 tf_warning_or_error);
14491 return res;
14494 /* Parse an iteration-statement.
14496 iteration-statement:
14497 while ( condition ) statement
14498 do statement while ( expression ) ;
14499 for ( init-statement condition [opt] ; expression [opt] )
14500 statement
14502 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
14504 static tree
14505 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
14506 tree unroll, bool novector)
14508 cp_token *token;
14509 enum rid keyword;
14510 tree statement;
14511 unsigned char in_statement;
14512 token_indent_info guard_tinfo;
14514 /* Peek at the next token. */
14515 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
14516 if (!token)
14517 return error_mark_node;
14519 guard_tinfo = get_token_indent_info (token);
14521 /* Remember whether or not we are already within an iteration
14522 statement. */
14523 in_statement = parser->in_statement;
14525 /* Special case for OMP loop intervening code. Parsing of permitted
14526 collapsed loop nests is handled elsewhere. */
14527 if (parser->omp_for_parse_state)
14529 error_at (token->location,
14530 "loop not permitted in intervening code in OpenMP loop body");
14531 parser->omp_for_parse_state->fail = true;
14534 /* See what kind of keyword it is. */
14535 keyword = token->keyword;
14536 switch (keyword)
14538 case RID_WHILE:
14540 tree condition;
14542 /* Begin the while-statement. */
14543 statement = begin_while_stmt ();
14544 /* Look for the `('. */
14545 matching_parens parens;
14546 parens.require_open (parser);
14547 /* Parse the condition. */
14548 condition = cp_parser_condition (parser);
14549 finish_while_stmt_cond (condition, statement, ivdep, unroll, novector);
14550 /* Look for the `)'. */
14551 parens.require_close (parser);
14552 /* Parse the dependent statement. */
14553 parser->in_statement = IN_ITERATION_STMT;
14554 bool prev = note_iteration_stmt_body_start ();
14555 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
14556 note_iteration_stmt_body_end (prev);
14557 parser->in_statement = in_statement;
14558 /* We're done with the while-statement. */
14559 finish_while_stmt (statement);
14561 break;
14563 case RID_DO:
14565 tree expression;
14567 /* Begin the do-statement. */
14568 statement = begin_do_stmt ();
14569 /* Parse the body of the do-statement. */
14570 parser->in_statement = IN_ITERATION_STMT;
14571 bool prev = note_iteration_stmt_body_start ();
14572 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
14573 note_iteration_stmt_body_end (prev);
14574 parser->in_statement = in_statement;
14575 finish_do_body (statement);
14576 /* Look for the `while' keyword. */
14577 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
14578 /* Look for the `('. */
14579 matching_parens parens;
14580 parens.require_open (parser);
14581 /* Parse the expression. */
14582 expression = cp_parser_expression (parser);
14583 /* We're done with the do-statement. */
14584 finish_do_stmt (expression, statement, ivdep, unroll, novector);
14585 /* Look for the `)'. */
14586 parens.require_close (parser);
14587 /* Look for the `;'. */
14588 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14590 break;
14592 case RID_FOR:
14594 /* Look for the `('. */
14595 matching_parens parens;
14596 parens.require_open (parser);
14598 statement = cp_parser_for (parser, ivdep, unroll, novector);
14600 /* Look for the `)'. */
14601 parens.require_close (parser);
14603 /* Parse the body of the for-statement. */
14604 parser->in_statement = IN_ITERATION_STMT;
14605 bool prev = note_iteration_stmt_body_start ();
14606 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
14607 note_iteration_stmt_body_end (prev);
14608 parser->in_statement = in_statement;
14610 /* We're done with the for-statement. */
14611 finish_for_stmt (statement);
14613 break;
14615 default:
14616 cp_parser_error (parser, "expected iteration-statement");
14617 statement = error_mark_node;
14618 break;
14621 return statement;
14624 /* Parse an init-statement or the declarator of a range-based-for.
14625 Returns true if a range-based-for declaration is seen.
14627 init-statement:
14628 expression-statement
14629 simple-declaration
14630 alias-declaration */
14632 static bool
14633 cp_parser_init_statement (cp_parser *parser, tree *decl)
14635 /* If the next token is a `;', then we have an empty
14636 expression-statement. Grammatically, this is also a
14637 simple-declaration, but an invalid one, because it does not
14638 declare anything. Therefore, if we did not handle this case
14639 specially, we would issue an error message about an invalid
14640 declaration. */
14641 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14643 bool is_range_for = false;
14644 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
14646 /* A colon is used in range-based for. */
14647 parser->colon_corrects_to_scope_p = false;
14649 /* We're going to speculatively look for a declaration, falling back
14650 to an expression, if necessary. */
14651 cp_parser_parse_tentatively (parser);
14652 bool expect_semicolon_p = true;
14653 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
14655 cp_parser_alias_declaration (parser);
14656 expect_semicolon_p = false;
14657 if (cxx_dialect < cxx23
14658 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
14659 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
14660 OPT_Wc__23_extensions,
14661 "alias-declaration in init-statement only "
14662 "available with %<-std=c++23%> or %<-std=gnu++23%>");
14664 else
14665 /* Parse the declaration. */
14666 cp_parser_simple_declaration (parser,
14667 /*function_definition_allowed_p=*/false,
14668 decl);
14669 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
14670 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14672 /* It is a range-for, consume the ':'. */
14673 cp_lexer_consume_token (parser->lexer);
14674 is_range_for = true;
14675 if (cxx_dialect < cxx11)
14676 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
14677 OPT_Wc__11_extensions,
14678 "range-based %<for%> loops only available with "
14679 "%<-std=c++11%> or %<-std=gnu++11%>");
14681 else if (expect_semicolon_p)
14682 /* The ';' is not consumed yet because we told
14683 cp_parser_simple_declaration not to. */
14684 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14686 if (cp_parser_parse_definitely (parser))
14687 return is_range_for;
14688 /* If the tentative parse failed, then we shall need to look for an
14689 expression-statement. */
14691 /* If we are here, it is an expression-statement. */
14692 cp_parser_expression_statement (parser, NULL_TREE);
14693 return false;
14696 /* Parse a jump-statement.
14698 jump-statement:
14699 break ;
14700 continue ;
14701 return expression [opt] ;
14702 return braced-init-list ;
14703 coroutine-return-statement;
14704 goto identifier ;
14706 GNU extension:
14708 jump-statement:
14709 goto * expression ;
14711 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
14713 static tree
14714 cp_parser_jump_statement (cp_parser* parser)
14716 tree statement = error_mark_node;
14717 cp_token *token;
14718 enum rid keyword;
14719 unsigned char in_statement;
14721 /* Peek at the next token. */
14722 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
14723 if (!token)
14724 return error_mark_node;
14726 /* See what kind of keyword it is. */
14727 keyword = token->keyword;
14728 switch (keyword)
14730 case RID_BREAK:
14731 in_statement = parser->in_statement & ~IN_IF_STMT;
14732 switch (in_statement)
14734 case 0:
14735 error_at (token->location, "break statement not within loop or switch");
14736 break;
14737 default:
14738 gcc_assert ((in_statement & IN_SWITCH_STMT)
14739 || in_statement == IN_ITERATION_STMT);
14740 statement = finish_break_stmt ();
14741 if (in_statement == IN_ITERATION_STMT)
14742 break_maybe_infinite_loop ();
14743 break;
14744 case IN_OMP_BLOCK:
14745 error_at (token->location, "invalid exit from OpenMP structured block");
14746 break;
14747 case IN_OMP_FOR:
14748 error_at (token->location, "break statement used with OpenMP for loop");
14749 break;
14751 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14752 break;
14754 case RID_CONTINUE:
14755 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
14757 case 0:
14758 error_at (token->location, "continue statement not within a loop");
14759 break;
14760 /* Fall through. */
14761 case IN_ITERATION_STMT:
14762 case IN_OMP_FOR:
14763 statement = finish_continue_stmt ();
14764 break;
14765 case IN_OMP_BLOCK:
14766 error_at (token->location, "invalid exit from OpenMP structured block");
14767 break;
14768 default:
14769 gcc_unreachable ();
14771 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14772 break;
14774 case RID_CO_RETURN:
14775 case RID_RETURN:
14777 tree expr;
14779 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14781 cp_lexer_set_source_position (parser->lexer);
14782 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14783 expr = cp_parser_braced_list (parser);
14785 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14786 expr = cp_parser_expression (parser);
14787 else
14788 /* If the next token is a `;', then there is no
14789 expression. */
14790 expr = NULL_TREE;
14791 /* Build the return-statement, check co-return first, since type
14792 deduction is not valid there. */
14793 if (keyword == RID_CO_RETURN)
14794 statement = finish_co_return_stmt (token->location, expr);
14795 else if (FNDECL_USED_AUTO (current_function_decl) && in_discarded_stmt)
14796 /* Don't deduce from a discarded return statement. */;
14797 else
14798 statement = finish_return_stmt (expr);
14799 /* Look for the final `;'. */
14800 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14802 break;
14804 case RID_GOTO:
14805 if (parser->in_function_body
14806 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
14807 && cxx_dialect < cxx23)
14809 error ("%<goto%> in %<constexpr%> function only available with "
14810 "%<-std=c++2b%> or %<-std=gnu++2b%>");
14811 cp_function_chain->invalid_constexpr = true;
14814 /* Create the goto-statement. */
14815 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
14817 /* Issue a warning about this use of a GNU extension. */
14818 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
14819 /* Consume the '*' token. */
14820 cp_lexer_consume_token (parser->lexer);
14821 /* Parse the dependent expression. */
14822 finish_goto_stmt (cp_parser_expression (parser));
14824 else
14825 finish_goto_stmt (cp_parser_identifier (parser));
14826 /* Look for the final `;'. */
14827 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14828 break;
14830 default:
14831 cp_parser_error (parser, "expected jump-statement");
14832 break;
14835 return statement;
14838 /* Parse a declaration-statement.
14840 declaration-statement:
14841 block-declaration */
14843 static void
14844 cp_parser_declaration_statement (cp_parser* parser)
14846 void *p;
14848 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
14849 p = obstack_alloc (&declarator_obstack, 0);
14851 /* Parse the block-declaration. */
14852 cp_parser_block_declaration (parser, /*statement_p=*/true);
14854 /* Free any declarators allocated. */
14855 obstack_free (&declarator_obstack, p);
14858 /* Some dependent statements (like `if (cond) statement'), are
14859 implicitly in their own scope. In other words, if the statement is
14860 a single statement (as opposed to a compound-statement), it is
14861 none-the-less treated as if it were enclosed in braces. Any
14862 declarations appearing in the dependent statement are out of scope
14863 after control passes that point. This function parses a statement,
14864 but ensures that is in its own scope, even if it is not a
14865 compound-statement.
14867 If IF_P is not NULL, *IF_P is set to indicate whether the statement
14868 is a (possibly labeled) if statement which is not enclosed in
14869 braces and has an else clause. This is used to implement
14870 -Wparentheses.
14872 CHAIN is a vector of if-else-if conditions. This is used to implement
14873 -Wduplicated-cond.
14875 Returns the new statement. */
14877 static tree
14878 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
14879 const token_indent_info &guard_tinfo,
14880 vec<tree> *chain)
14882 tree statement;
14883 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
14884 location_t body_loc_after_labels = UNKNOWN_LOCATION;
14885 token_indent_info body_tinfo
14886 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
14888 if (if_p != NULL)
14889 *if_p = false;
14891 /* Mark if () ; with a special NOP_EXPR. */
14892 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14894 cp_lexer_consume_token (parser->lexer);
14895 statement = add_stmt (build_empty_stmt (body_loc));
14897 if (guard_tinfo.keyword == RID_IF
14898 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
14899 warning_at (body_loc, OPT_Wempty_body,
14900 "suggest braces around empty body in an %<if%> statement");
14901 else if (guard_tinfo.keyword == RID_ELSE)
14902 warning_at (body_loc, OPT_Wempty_body,
14903 "suggest braces around empty body in an %<else%> statement");
14905 /* if a compound is opened, we simply parse the statement directly. */
14906 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14907 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
14908 /* If the token is not a `{', then we must take special action. */
14909 else
14911 /* Create a compound-statement. */
14912 statement = begin_compound_stmt (0);
14913 /* Parse the dependent-statement. */
14914 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
14915 &body_loc_after_labels);
14916 /* Finish the dummy compound-statement. */
14917 finish_compound_stmt (statement);
14920 token_indent_info next_tinfo
14921 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
14922 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
14924 if (body_loc_after_labels != UNKNOWN_LOCATION
14925 && next_tinfo.type != CPP_SEMICOLON)
14926 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
14927 guard_tinfo.location, guard_tinfo.keyword);
14929 /* Return the statement. */
14930 return statement;
14933 /* For some dependent statements (like `while (cond) statement'), we
14934 have already created a scope. Therefore, even if the dependent
14935 statement is a compound-statement, we do not want to create another
14936 scope. */
14938 static void
14939 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
14940 const token_indent_info &guard_tinfo)
14942 /* If the token is a `{', then we must take special action. */
14943 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14945 token_indent_info body_tinfo
14946 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
14947 location_t loc_after_labels = UNKNOWN_LOCATION;
14949 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
14950 &loc_after_labels);
14951 token_indent_info next_tinfo
14952 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
14953 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
14955 if (loc_after_labels != UNKNOWN_LOCATION
14956 && next_tinfo.type != CPP_SEMICOLON)
14957 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
14958 guard_tinfo.location,
14959 guard_tinfo.keyword);
14961 else
14963 /* Avoid calling cp_parser_compound_statement, so that we
14964 don't create a new scope. Do everything else by hand. */
14965 matching_braces braces;
14966 braces.require_open (parser);
14967 /* If the next keyword is `__label__' we have a label declaration. */
14968 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
14969 cp_parser_label_declaration (parser);
14970 /* Parse an (optional) statement-seq. */
14971 cp_parser_statement_seq_opt (parser, NULL_TREE);
14972 braces.require_close (parser);
14976 /* Modules */
14978 /* Parse a module-name or module-partition.
14980 module-name:
14981 module-name-qualifier [opt] identifier
14983 module-partition:
14984 : module-name-qualifier [opt] identifier
14986 module-name-qualifier:
14987 identifier .
14988 module-name-qualifier identifier .
14990 Returns a pointer to the module object, or NULL on failure.
14991 For PARTITION_P, PARENT is the module this is a partition of. */
14993 static module_state *
14994 cp_parser_module_name (cp_parser *parser, bool partition_p = false,
14995 module_state *parent = NULL)
14997 if (partition_p
14998 && cp_lexer_consume_token (parser->lexer)->type != CPP_COLON)
14999 return NULL;
15001 for (;;)
15003 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME)
15005 if (partition_p)
15006 cp_parser_error (parser, "expected module-partition");
15007 else
15008 cp_parser_error (parser, "expected module-name");
15009 return NULL;
15012 tree name = cp_lexer_consume_token (parser->lexer)->u.value;
15013 parent = get_module (name, parent, partition_p);
15014 if (cp_lexer_peek_token (parser->lexer)->type != CPP_DOT)
15015 break;
15017 cp_lexer_consume_token (parser->lexer);
15020 return parent;
15023 /* Parse a module-partition. Defers to cp_parser_module_name. */
15025 static module_state *
15026 cp_parser_module_partition (cp_parser *parser, module_state *parent = NULL)
15028 return cp_parser_module_name (parser, /*partition_p=*/true, parent);
15031 /* Named module-declaration
15032 __module ; PRAGMA_EOL
15033 __module : private ; PRAGMA_EOL (unimplemented)
15034 [__export] __module module-name module-partition [opt]
15035 attr-spec-seq-opt ; PRAGMA_EOL
15038 static module_parse
15039 cp_parser_module_declaration (cp_parser *parser, module_parse mp_state,
15040 bool exporting)
15042 /* We're a pseudo pragma. */
15043 parser->lexer->in_pragma = true;
15044 cp_token *token = cp_lexer_consume_token (parser->lexer);
15046 if (flag_header_unit)
15048 error_at (token->location,
15049 "module-declaration not permitted in header-unit");
15050 goto skip_eol;
15052 else if (mp_state == MP_FIRST && !exporting
15053 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15055 /* Start global module fragment. */
15056 cp_lexer_consume_token (parser->lexer);
15057 module_kind = MK_NAMED;
15058 mp_state = MP_GLOBAL;
15059 cp_parser_require_pragma_eol (parser, token);
15061 else if (!exporting
15062 && cp_lexer_next_token_is (parser->lexer, CPP_COLON)
15063 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_PRIVATE)
15064 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_SEMICOLON))
15066 cp_lexer_consume_token (parser->lexer);
15067 cp_lexer_consume_token (parser->lexer);
15068 cp_lexer_consume_token (parser->lexer);
15069 cp_parser_require_pragma_eol (parser, token);
15071 if ((mp_state == MP_PURVIEW || mp_state == MP_PURVIEW_IMPORTS)
15072 && module_has_cmi_p ())
15074 mp_state = MP_PRIVATE_IMPORTS;
15075 sorry_at (token->location, "private module fragment");
15077 else
15078 error_at (token->location,
15079 "private module fragment only permitted in purview"
15080 " of module interface or partition");
15082 else if (!(mp_state == MP_FIRST || mp_state == MP_GLOBAL))
15084 /* Neither the first declaration, nor in a GMF. */
15085 error_at (token->location, "module-declaration only permitted as first"
15086 " declaration, or ending a global module fragment");
15087 skip_eol:
15088 cp_parser_skip_to_pragma_eol (parser, token);
15090 else
15092 module_state *mod = cp_parser_module_name (parser);
15093 if (mod && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
15094 mod = cp_parser_module_partition (parser, mod);
15095 tree attrs = cp_parser_attributes_opt (parser);
15097 if (mod)
15098 mp_state = MP_PURVIEW_IMPORTS;
15099 if (!mod || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
15100 goto skip_eol;
15102 declare_module (mod, token->location, exporting, attrs, parse_in);
15103 cp_parser_require_pragma_eol (parser, token);
15106 return mp_state;
15109 /* Import-declaration
15110 __import module-name attr-spec-seq-opt ; PRAGMA_EOL
15111 __import module-partition attr-spec-seq-opt ; PRAGMA_EOL
15112 __import header-name attr-spec-seq-opt ; PRAGMA_EOL
15115 static void
15116 cp_parser_import_declaration (cp_parser *parser, module_parse mp_state,
15117 bool exporting)
15119 /* We're a pseudo pragma. */
15120 parser->lexer->in_pragma = true;
15121 cp_token *token = cp_lexer_consume_token (parser->lexer);
15123 if (mp_state == MP_PURVIEW || mp_state == MP_PRIVATE)
15125 error_at (token->location, "post-module-declaration"
15126 " imports must be contiguous");
15127 note_lexer:
15128 inform (token->location, "perhaps insert a line break, or other"
15129 " disambiguation, to prevent this being considered a"
15130 " module control-line");
15131 skip_eol:
15132 cp_parser_skip_to_pragma_eol (parser, token);
15134 else if (current_scope () != global_namespace)
15136 error_at (token->location, "import-declaration must be at global scope");
15137 goto note_lexer;
15139 else
15141 module_state *mod = NULL;
15142 cp_token *next = cp_lexer_peek_token (parser->lexer);
15143 if (next->type == CPP_HEADER_NAME)
15145 cp_lexer_consume_token (parser->lexer);
15146 mod = get_module (next->u.value);
15148 else if (next->type == CPP_COLON)
15150 /* An import specifying a module-partition shall only appear after the
15151 module-declaration in a module unit: [module.import]/4. */
15152 if (named_module_p ()
15153 && (mp_state == MP_PURVIEW_IMPORTS
15154 || mp_state == MP_PRIVATE_IMPORTS))
15155 mod = cp_parser_module_partition (parser);
15156 else
15157 error_at (next->location, "import specifying a module-partition"
15158 " must appear after a named module-declaration");
15160 else
15161 mod = cp_parser_module_name (parser);
15162 tree attrs = cp_parser_attributes_opt (parser);
15164 if (!mod || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
15165 goto skip_eol;
15166 cp_parser_require_pragma_eol (parser, token);
15168 if (parser->in_unbraced_linkage_specification_p)
15169 error_at (token->location, "import cannot appear directly in"
15170 " a linkage-specification");
15172 if (mp_state == MP_PURVIEW_IMPORTS || mp_state == MP_PRIVATE_IMPORTS)
15174 /* Module-purview imports must not be from source inclusion
15175 [cpp.import]/7 */
15176 if (attrs
15177 && private_lookup_attribute ("__translated",
15178 strlen ("__translated"), attrs))
15179 error_at (token->location, "post-module-declaration imports"
15180 " must not be include-translated");
15181 else if (!token->main_source_p)
15182 error_at (token->location, "post-module-declaration imports"
15183 " must not be from header inclusion");
15186 import_module (mod, token->location, exporting, attrs, parse_in);
15190 /* export-declaration.
15192 export declaration
15193 export { declaration-seq-opt } */
15195 static void
15196 cp_parser_module_export (cp_parser *parser)
15198 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT));
15199 cp_token *token = cp_lexer_consume_token (parser->lexer);
15201 if (!module_interface_p ())
15202 error_at (token->location,
15203 "%qE may only occur after a module interface declaration",
15204 token->u.value);
15206 bool braced = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE);
15208 unsigned mk = module_kind;
15209 if (module_exporting_p ())
15210 error_at (token->location,
15211 "%qE may only occur once in an export declaration",
15212 token->u.value);
15213 module_kind |= MK_EXPORTING;
15215 if (braced)
15217 cp_ensure_no_omp_declare_simd (parser);
15218 cp_ensure_no_oacc_routine (parser);
15220 cp_lexer_consume_token (parser->lexer);
15221 cp_parser_declaration_seq_opt (parser);
15222 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15224 else
15226 /* Explicitly check if the next tokens might be a
15227 module-directive line, so we can give a clearer error message
15228 about why the directive will be rejected. */
15229 if (cp_lexer_next_token_is_keyword (parser->lexer, RID__MODULE)
15230 || cp_lexer_next_token_is_keyword (parser->lexer, RID__IMPORT)
15231 || cp_lexer_next_token_is_keyword (parser->lexer, RID__EXPORT))
15232 error_at (token->location, "%<export%> not part of following"
15233 " module-directive");
15234 cp_parser_declaration (parser, NULL_TREE);
15237 module_kind = mk;
15240 /* Declarations [gram.dcl.dcl] */
15242 /* Parse an optional declaration-sequence. TOP_LEVEL is true, if this
15243 is the top-level declaration sequence. That affects whether we
15244 deal with module-preamble.
15246 declaration-seq:
15247 declaration
15248 declaration-seq declaration */
15250 static void
15251 cp_parser_declaration_seq_opt (cp_parser* parser)
15253 while (true)
15255 cp_token *token = cp_lexer_peek_token (parser->lexer);
15257 if (token->type == CPP_CLOSE_BRACE
15258 || token->type == CPP_EOF)
15259 break;
15260 else
15261 cp_parser_toplevel_declaration (parser);
15265 /* Parse a declaration.
15267 declaration:
15268 block-declaration
15269 function-definition
15270 template-declaration
15271 explicit-instantiation
15272 explicit-specialization
15273 linkage-specification
15274 namespace-definition
15276 C++17:
15277 deduction-guide
15279 modules:
15280 (all these are only allowed at the outermost level, check
15281 that semantically, for better diagnostics)
15282 module-declaration
15283 module-export-declaration
15284 module-import-declaration
15285 export-declaration
15287 GNU extension:
15289 declaration:
15290 __extension__ declaration */
15292 static void
15293 cp_parser_declaration (cp_parser* parser, tree prefix_attrs)
15295 int saved_pedantic;
15297 /* Check for the `__extension__' keyword. */
15298 if (cp_parser_extension_opt (parser, &saved_pedantic))
15300 /* Parse the qualified declaration. */
15301 cp_parser_declaration (parser, prefix_attrs);
15302 /* Restore the PEDANTIC flag. */
15303 pedantic = saved_pedantic;
15305 return;
15308 /* Try to figure out what kind of declaration is present. */
15309 cp_token *token1 = cp_lexer_peek_token (parser->lexer);
15310 cp_token *token2 = (token1->type == CPP_EOF
15311 ? token1 : cp_lexer_peek_nth_token (parser->lexer, 2));
15313 if (token1->type == CPP_SEMICOLON)
15315 cp_lexer_consume_token (parser->lexer);
15316 /* A declaration consisting of a single semicolon is invalid
15317 * before C++11. Allow it unless we're being pedantic. */
15318 if (cxx_dialect < cxx11)
15319 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
15320 return;
15322 else if (cp_lexer_nth_token_is (parser->lexer,
15323 cp_parser_skip_std_attribute_spec_seq (parser,
15325 CPP_SEMICOLON))
15327 location_t attrs_loc = token1->location;
15328 tree std_attrs = cp_parser_std_attribute_spec_seq (parser);
15330 if (std_attrs && (flag_openmp || flag_openmp_simd))
15332 gcc_assert (!parser->lexer->in_omp_attribute_pragma);
15333 std_attrs = cp_parser_handle_statement_omp_attributes (parser,
15334 std_attrs);
15335 if (parser->lexer->in_omp_attribute_pragma)
15337 cp_lexer *lexer = parser->lexer;
15338 while (parser->lexer->in_omp_attribute_pragma)
15340 gcc_assert (cp_lexer_next_token_is (parser->lexer,
15341 CPP_PRAGMA));
15342 cp_parser_pragma (parser, pragma_external, NULL);
15344 cp_lexer_destroy (lexer);
15348 if (std_attrs != NULL_TREE && any_nonignored_attribute_p (std_attrs))
15349 warning_at (make_location (attrs_loc, attrs_loc, parser->lexer),
15350 OPT_Wattributes, "attribute ignored");
15351 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15352 cp_lexer_consume_token (parser->lexer);
15353 return;
15356 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
15357 void *p = obstack_alloc (&declarator_obstack, 0);
15359 tree attributes = NULL_TREE;
15361 /* Conditionally, allow attributes to precede a linkage specification. */
15362 if (token1->keyword == RID_ATTRIBUTE)
15364 cp_lexer_save_tokens (parser->lexer);
15365 attributes = cp_parser_attributes_opt (parser);
15366 cp_token *t1 = cp_lexer_peek_token (parser->lexer);
15367 cp_token *t2 = (t1->type == CPP_EOF
15368 ? t1 : cp_lexer_peek_nth_token (parser->lexer, 2));
15369 if (t1->keyword == RID_EXTERN
15370 && cp_parser_is_pure_string_literal (t2))
15372 cp_lexer_commit_tokens (parser->lexer);
15373 /* We might have already been here. */
15374 if (!c_dialect_objc ())
15376 location_t where = get_finish (t2->location);
15377 warning_at (token1->location, OPT_Wattributes, "attributes are"
15378 " not permitted in this position");
15379 where = linemap_position_for_loc_and_offset (line_table,
15380 where, 1);
15381 inform (where, "attributes may be inserted here");
15382 attributes = NULL_TREE;
15384 token1 = t1;
15385 token2 = t2;
15387 else
15389 cp_lexer_rollback_tokens (parser->lexer);
15390 attributes = NULL_TREE;
15393 /* If we already had some attributes, and we've added more, then prepend.
15394 Otherwise attributes just contains any that we just read. */
15395 if (prefix_attrs)
15397 if (attributes)
15398 TREE_CHAIN (prefix_attrs) = attributes;
15399 attributes = prefix_attrs;
15402 /* If the next token is `extern' and the following token is a string
15403 literal, then we have a linkage specification. */
15404 if (token1->keyword == RID_EXTERN
15405 && cp_parser_is_pure_string_literal (token2))
15406 cp_parser_linkage_specification (parser, attributes);
15407 /* If the next token is `template', then we have either a template
15408 declaration, an explicit instantiation, or an explicit
15409 specialization. */
15410 else if (token1->keyword == RID_TEMPLATE)
15412 /* `template <>' indicates a template specialization. */
15413 if (token2->type == CPP_LESS
15414 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
15415 cp_parser_explicit_specialization (parser);
15416 /* `template <' indicates a template declaration. */
15417 else if (token2->type == CPP_LESS)
15418 cp_parser_template_declaration (parser, /*member_p=*/false);
15419 /* Anything else must be an explicit instantiation. */
15420 else
15421 cp_parser_explicit_instantiation (parser);
15423 /* If the next token is `export', it's new-style modules or
15424 old-style template. */
15425 else if (token1->keyword == RID_EXPORT)
15427 if (!modules_p ())
15428 cp_parser_template_declaration (parser, /*member_p=*/false);
15429 else
15430 cp_parser_module_export (parser);
15432 else if (cp_token_is_module_directive (token1))
15434 bool exporting = token1->keyword == RID__EXPORT;
15435 cp_token *next = exporting ? token2 : token1;
15436 if (exporting)
15437 cp_lexer_consume_token (parser->lexer);
15438 // In module purview this will be ill-formed.
15439 auto state = (!named_module_p () ? MP_NOT_MODULE
15440 : module_purview_p () ? MP_PURVIEW
15441 : MP_GLOBAL);
15442 if (next->keyword == RID__MODULE)
15443 cp_parser_module_declaration (parser, state, exporting);
15444 else
15445 cp_parser_import_declaration (parser, state, exporting);
15447 /* If the next token is `extern', 'static' or 'inline' and the one
15448 after that is `template', we have a GNU extended explicit
15449 instantiation directive. */
15450 else if (cp_parser_allow_gnu_extensions_p (parser)
15451 && token2->keyword == RID_TEMPLATE
15452 && (token1->keyword == RID_EXTERN
15453 || token1->keyword == RID_STATIC
15454 || token1->keyword == RID_INLINE))
15455 cp_parser_explicit_instantiation (parser);
15456 /* If the next token is `namespace', check for a named or unnamed
15457 namespace definition. */
15458 else if (token1->keyword == RID_NAMESPACE
15459 && (/* A named namespace definition. */
15460 (token2->type == CPP_NAME
15461 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
15462 != CPP_EQ))
15463 || (token2->type == CPP_OPEN_SQUARE
15464 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
15465 == CPP_OPEN_SQUARE)
15466 /* An unnamed namespace definition. */
15467 || token2->type == CPP_OPEN_BRACE
15468 || token2->keyword == RID_ATTRIBUTE))
15469 cp_parser_namespace_definition (parser);
15470 /* An inline (associated) namespace definition. */
15471 else if (token2->keyword == RID_NAMESPACE
15472 && token1->keyword == RID_INLINE)
15473 cp_parser_namespace_definition (parser);
15474 /* Objective-C++ declaration/definition. */
15475 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1->keyword))
15476 cp_parser_objc_declaration (parser, attributes);
15477 else if (c_dialect_objc ()
15478 && token1->keyword == RID_ATTRIBUTE
15479 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
15480 cp_parser_objc_declaration (parser, attributes);
15481 /* At this point we may have a template declared by a concept
15482 introduction. */
15483 else if (flag_concepts
15484 && cp_parser_template_declaration_after_export (parser,
15485 /*member_p=*/false))
15486 /* We did. */;
15487 else
15488 /* Try to parse a block-declaration, or a function-definition. */
15489 cp_parser_block_declaration (parser, /*statement_p=*/false);
15491 /* Free any declarators allocated. */
15492 obstack_free (&declarator_obstack, p);
15495 /* Parse a namespace-scope declaration. */
15497 static void
15498 cp_parser_toplevel_declaration (cp_parser* parser)
15500 cp_token *token = cp_lexer_peek_token (parser->lexer);
15502 if (token->type == CPP_PRAGMA)
15503 /* A top-level declaration can consist solely of a #pragma. A
15504 nested declaration cannot, so this is done here and not in
15505 cp_parser_declaration. (A #pragma at block scope is
15506 handled in cp_parser_statement.) */
15507 cp_parser_pragma (parser, pragma_external, NULL);
15508 else
15509 /* Parse the declaration itself. */
15510 cp_parser_declaration (parser, NULL_TREE);
15513 /* Parse a block-declaration.
15515 block-declaration:
15516 simple-declaration
15517 asm-definition
15518 namespace-alias-definition
15519 using-declaration
15520 using-directive
15522 GNU Extension:
15524 block-declaration:
15525 __extension__ block-declaration
15527 C++0x Extension:
15529 block-declaration:
15530 static_assert-declaration
15532 If STATEMENT_P is TRUE, then this block-declaration is occurring as
15533 part of a declaration-statement. */
15535 static void
15536 cp_parser_block_declaration (cp_parser *parser,
15537 bool statement_p)
15539 int saved_pedantic;
15541 /* Check for the `__extension__' keyword. */
15542 if (cp_parser_extension_opt (parser, &saved_pedantic))
15544 /* Parse the qualified declaration. */
15545 cp_parser_block_declaration (parser, statement_p);
15546 /* Restore the PEDANTIC flag. */
15547 pedantic = saved_pedantic;
15549 return;
15552 /* Peek at the next token to figure out which kind of declaration is
15553 present. */
15554 cp_token *token1 = cp_lexer_peek_token (parser->lexer);
15556 /* If the next keyword is `asm', we have an asm-definition. */
15557 if (token1->keyword == RID_ASM)
15559 if (statement_p)
15560 cp_parser_commit_to_tentative_parse (parser);
15561 cp_parser_asm_definition (parser);
15563 /* If the next keyword is `namespace', we have a
15564 namespace-alias-definition. */
15565 else if (token1->keyword == RID_NAMESPACE)
15566 cp_parser_namespace_alias_definition (parser);
15567 /* If the next keyword is `using', we have a
15568 using-declaration, a using-directive, or an alias-declaration. */
15569 else if (token1->keyword == RID_USING)
15571 cp_token *token2;
15573 if (statement_p)
15574 cp_parser_commit_to_tentative_parse (parser);
15575 /* If the token after `using' is `namespace', then we have a
15576 using-directive. */
15577 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
15578 if (token2->keyword == RID_NAMESPACE)
15579 cp_parser_using_directive (parser);
15580 else if (token2->keyword == RID_ENUM)
15581 cp_parser_using_enum (parser);
15582 /* If the second token after 'using' is '=', then we have an
15583 alias-declaration. */
15584 else if (cxx_dialect >= cxx11
15585 && token2->type == CPP_NAME
15586 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
15587 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
15588 cp_parser_alias_declaration (parser);
15589 /* Otherwise, it's a using-declaration. */
15590 else
15591 cp_parser_using_declaration (parser,
15592 /*access_declaration_p=*/false);
15594 /* If the next keyword is `__label__' we have a misplaced label
15595 declaration. */
15596 else if (token1->keyword == RID_LABEL)
15598 cp_lexer_consume_token (parser->lexer);
15599 error_at (token1->location, "%<__label__%> not at the beginning of a block");
15600 cp_parser_skip_to_end_of_statement (parser);
15601 /* If the next token is now a `;', consume it. */
15602 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15603 cp_lexer_consume_token (parser->lexer);
15605 /* If the next token is `static_assert' we have a static assertion. */
15606 else if (token1->keyword == RID_STATIC_ASSERT)
15607 cp_parser_static_assert (parser, /*member_p=*/false);
15608 else
15610 size_t attr_idx = cp_parser_skip_std_attribute_spec_seq (parser, 1);
15611 cp_token *after_attr = NULL;
15612 if (attr_idx != 1)
15613 after_attr = cp_lexer_peek_nth_token (parser->lexer, attr_idx);
15614 /* If the next tokens after attributes is `using namespace', then we have
15615 a using-directive. */
15616 if (after_attr
15617 && after_attr->keyword == RID_USING
15618 && cp_lexer_nth_token_is_keyword (parser->lexer, attr_idx + 1,
15619 RID_NAMESPACE))
15621 if (statement_p)
15622 cp_parser_commit_to_tentative_parse (parser);
15623 cp_parser_using_directive (parser);
15625 /* If the next token after attributes is `asm', then we have
15626 an asm-definition. */
15627 else if (after_attr && after_attr->keyword == RID_ASM)
15629 if (statement_p)
15630 cp_parser_commit_to_tentative_parse (parser);
15631 cp_parser_asm_definition (parser);
15633 /* Anything else must be a simple-declaration. */
15634 else
15635 cp_parser_simple_declaration (parser, !statement_p,
15636 /*maybe_range_for_decl*/NULL);
15640 /* Parse a simple-declaration.
15642 simple-declaration:
15643 decl-specifier-seq [opt] init-declarator-list [opt] ;
15644 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
15645 brace-or-equal-initializer ;
15647 init-declarator-list:
15648 init-declarator
15649 init-declarator-list , init-declarator
15651 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
15652 function-definition as a simple-declaration.
15654 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
15655 parsed declaration if it is an uninitialized single declarator not followed
15656 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
15657 if present, will not be consumed. */
15659 static void
15660 cp_parser_simple_declaration (cp_parser* parser,
15661 bool function_definition_allowed_p,
15662 tree *maybe_range_for_decl)
15664 cp_decl_specifier_seq decl_specifiers;
15665 int declares_class_or_enum;
15666 bool saw_declarator;
15667 location_t comma_loc = UNKNOWN_LOCATION;
15668 location_t init_loc = UNKNOWN_LOCATION;
15670 if (maybe_range_for_decl)
15671 *maybe_range_for_decl = NULL_TREE;
15673 /* Defer access checks until we know what is being declared; the
15674 checks for names appearing in the decl-specifier-seq should be
15675 done as if we were in the scope of the thing being declared. */
15676 push_deferring_access_checks (dk_deferred);
15678 /* Parse the decl-specifier-seq. We have to keep track of whether
15679 or not the decl-specifier-seq declares a named class or
15680 enumeration type, since that is the only case in which the
15681 init-declarator-list is allowed to be empty.
15683 [dcl.dcl]
15685 In a simple-declaration, the optional init-declarator-list can be
15686 omitted only when declaring a class or enumeration, that is when
15687 the decl-specifier-seq contains either a class-specifier, an
15688 elaborated-type-specifier, or an enum-specifier. */
15689 cp_parser_decl_specifier_seq (parser,
15690 CP_PARSER_FLAGS_OPTIONAL,
15691 &decl_specifiers,
15692 &declares_class_or_enum);
15693 /* We no longer need to defer access checks. */
15694 stop_deferring_access_checks ();
15696 cp_omp_declare_simd_data odsd;
15697 if (decl_specifiers.attributes && (flag_openmp || flag_openmp_simd))
15698 cp_parser_handle_directive_omp_attributes (parser,
15699 &decl_specifiers.attributes,
15700 &odsd, true);
15702 /* In a block scope, a valid declaration must always have a
15703 decl-specifier-seq. By not trying to parse declarators, we can
15704 resolve the declaration/expression ambiguity more quickly. */
15705 if (!function_definition_allowed_p
15706 && !decl_specifiers.any_specifiers_p)
15708 cp_parser_error (parser, "expected declaration");
15709 goto done;
15712 /* If the next two tokens are both identifiers, the code is
15713 erroneous. The usual cause of this situation is code like:
15715 T t;
15717 where "T" should name a type -- but does not. */
15718 if (!decl_specifiers.any_type_specifiers_p
15719 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15721 /* If parsing tentatively, we should commit; we really are
15722 looking at a declaration. */
15723 cp_parser_commit_to_tentative_parse (parser);
15724 /* Give up. */
15725 goto done;
15728 cp_parser_maybe_commit_to_declaration (parser, &decl_specifiers);
15730 /* Look for C++17 decomposition declaration. */
15731 for (size_t n = 1; ; n++)
15732 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
15733 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
15734 continue;
15735 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
15736 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
15737 && decl_specifiers.any_specifiers_p)
15739 tree decl
15740 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
15741 maybe_range_for_decl,
15742 &init_loc);
15744 /* The next token should be either a `,' or a `;'. */
15745 cp_token *token = cp_lexer_peek_token (parser->lexer);
15746 /* If it's a `;', we are done. */
15747 if (token->type == CPP_SEMICOLON)
15748 goto finish;
15749 else if (maybe_range_for_decl)
15751 if (*maybe_range_for_decl == NULL_TREE)
15752 *maybe_range_for_decl = error_mark_node;
15753 goto finish;
15755 /* Anything else is an error. */
15756 else
15758 /* If we have already issued an error message we don't need
15759 to issue another one. */
15760 if ((decl != error_mark_node
15761 && DECL_INITIAL (decl) != error_mark_node)
15762 || cp_parser_uncommitted_to_tentative_parse_p (parser))
15763 cp_parser_error (parser, "expected %<;%>");
15764 /* Skip tokens until we reach the end of the statement. */
15765 cp_parser_skip_to_end_of_statement (parser);
15766 /* If the next token is now a `;', consume it. */
15767 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15768 cp_lexer_consume_token (parser->lexer);
15769 goto done;
15772 else
15773 break;
15775 tree last_type;
15776 bool auto_specifier_p;
15777 /* NULL_TREE if both variable and function declaration are allowed,
15778 error_mark_node if function declaration are not allowed and
15779 a FUNCTION_DECL that should be diagnosed if it is followed by
15780 variable declarations. */
15781 tree auto_function_declaration;
15783 last_type = NULL_TREE;
15784 auto_specifier_p
15785 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
15786 auto_function_declaration = NULL_TREE;
15788 /* Keep going until we hit the `;' at the end of the simple
15789 declaration. */
15790 saw_declarator = false;
15791 while (cp_lexer_next_token_is_not (parser->lexer,
15792 CPP_SEMICOLON))
15794 cp_token *token;
15795 bool function_definition_p;
15796 tree decl;
15797 tree auto_result = NULL_TREE;
15799 if (saw_declarator)
15801 /* If we are processing next declarator, comma is expected */
15802 token = cp_lexer_peek_token (parser->lexer);
15803 gcc_assert (token->type == CPP_COMMA);
15804 cp_lexer_consume_token (parser->lexer);
15805 if (maybe_range_for_decl)
15807 *maybe_range_for_decl = error_mark_node;
15808 if (comma_loc == UNKNOWN_LOCATION)
15809 comma_loc = token->location;
15812 else
15813 saw_declarator = true;
15815 /* Parse the init-declarator. */
15816 decl = cp_parser_init_declarator (parser,
15817 CP_PARSER_FLAGS_NONE,
15818 &decl_specifiers,
15819 /*checks=*/NULL,
15820 function_definition_allowed_p,
15821 /*member_p=*/false,
15822 declares_class_or_enum,
15823 &function_definition_p,
15824 maybe_range_for_decl,
15825 &init_loc,
15826 &auto_result);
15827 const bool fndecl_p = TREE_CODE (decl) == FUNCTION_DECL;
15828 /* If an error occurred while parsing tentatively, exit quickly.
15829 (That usually happens when in the body of a function; each
15830 statement is treated as a declaration-statement until proven
15831 otherwise.) */
15832 if (cp_parser_error_occurred (parser))
15833 goto done;
15835 if (auto_specifier_p && cxx_dialect >= cxx14)
15837 /* If the init-declarator-list contains more than one
15838 init-declarator, they shall all form declarations of
15839 variables. */
15840 if (auto_function_declaration == NULL_TREE)
15841 auto_function_declaration = fndecl_p ? decl : error_mark_node;
15842 else if (fndecl_p || auto_function_declaration != error_mark_node)
15844 error_at (decl_specifiers.locations[ds_type_spec],
15845 "non-variable %qD in declaration with more than one "
15846 "declarator with placeholder type",
15847 fndecl_p ? decl : auto_function_declaration);
15848 auto_function_declaration = error_mark_node;
15852 if (auto_result
15853 && (!processing_template_decl || !type_uses_auto (auto_result)))
15855 if (last_type
15856 && last_type != error_mark_node
15857 && !same_type_p (auto_result, last_type))
15859 /* If the list of declarators contains more than one declarator,
15860 the type of each declared variable is determined as described
15861 above. If the type deduced for the template parameter U is not
15862 the same in each deduction, the program is ill-formed. */
15863 error_at (decl_specifiers.locations[ds_type_spec],
15864 "inconsistent deduction for %qT: %qT and then %qT",
15865 decl_specifiers.type, last_type, auto_result);
15866 last_type = error_mark_node;
15868 else
15869 last_type = auto_result;
15872 /* Handle function definitions specially. */
15873 if (function_definition_p)
15875 /* If the next token is a `,', then we are probably
15876 processing something like:
15878 void f() {}, *p;
15880 which is erroneous. */
15881 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15883 cp_token *token = cp_lexer_peek_token (parser->lexer);
15884 error_at (token->location,
15885 "mixing"
15886 " declarations and function-definitions is forbidden");
15888 /* Otherwise, we're done with the list of declarators. */
15889 else
15891 pop_deferring_access_checks ();
15892 cp_finalize_omp_declare_simd (parser, &odsd);
15893 return;
15896 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
15897 *maybe_range_for_decl = decl;
15898 /* The next token should be either a `,' or a `;'. */
15899 token = cp_lexer_peek_token (parser->lexer);
15900 /* If it's a `,', there are more declarators to come. */
15901 if (token->type == CPP_COMMA)
15902 /* will be consumed next time around */;
15903 /* If it's a `;', we are done. */
15904 else if (token->type == CPP_SEMICOLON)
15905 break;
15906 else if (maybe_range_for_decl)
15908 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
15909 permerror (decl_specifiers.locations[ds_type_spec],
15910 "types may not be defined in a for-range-declaration");
15911 break;
15913 /* Anything else is an error. */
15914 else
15916 /* If we have already issued an error message we don't need
15917 to issue another one. */
15918 if ((decl != error_mark_node
15919 /* grokfndecl sets DECL_INITIAL to error_mark_node for
15920 functions. */
15921 && (fndecl_p || DECL_INITIAL (decl) != error_mark_node))
15922 || cp_parser_uncommitted_to_tentative_parse_p (parser))
15923 cp_parser_error (parser, "expected %<,%> or %<;%>");
15924 /* Skip tokens until we reach the end of the statement. */
15925 cp_parser_skip_to_end_of_statement (parser);
15926 /* If the next token is now a `;', consume it. */
15927 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15928 cp_lexer_consume_token (parser->lexer);
15929 goto done;
15931 /* After the first time around, a function-definition is not
15932 allowed -- even if it was OK at first. For example:
15934 int i, f() {}
15936 is not valid. */
15937 function_definition_allowed_p = false;
15940 /* Issue an error message if no declarators are present, and the
15941 decl-specifier-seq does not itself declare a class or
15942 enumeration: [dcl.dcl]/3. */
15943 if (!saw_declarator)
15945 if (cp_parser_declares_only_class_p (parser))
15947 if (!declares_class_or_enum
15948 && decl_specifiers.type
15949 && OVERLOAD_TYPE_P (decl_specifiers.type))
15950 /* Ensure an error is issued anyway when finish_decltype_type,
15951 called via cp_parser_decl_specifier_seq, returns a class or
15952 an enumeration (c++/51786). */
15953 decl_specifiers.type = NULL_TREE;
15954 shadow_tag (&decl_specifiers);
15956 /* Perform any deferred access checks. */
15957 perform_deferred_access_checks (tf_warning_or_error);
15960 /* Consume the `;'. */
15961 finish:
15962 if (!maybe_range_for_decl)
15963 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15964 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15966 if (init_loc != UNKNOWN_LOCATION)
15967 error_at (init_loc, "initializer in range-based %<for%> loop");
15968 if (comma_loc != UNKNOWN_LOCATION)
15969 error_at (comma_loc,
15970 "multiple declarations in range-based %<for%> loop");
15973 done:
15974 pop_deferring_access_checks ();
15975 cp_finalize_omp_declare_simd (parser, &odsd);
15978 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
15979 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
15980 initializer ; */
15982 static tree
15983 cp_parser_decomposition_declaration (cp_parser *parser,
15984 cp_decl_specifier_seq *decl_specifiers,
15985 tree *maybe_range_for_decl,
15986 location_t *init_loc)
15988 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
15989 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
15990 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
15992 /* Parse the identifier-list. */
15993 auto_vec<cp_expr, 10> v;
15994 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
15995 while (true)
15997 cp_expr e = cp_parser_identifier (parser);
15998 if (e.get_value () == error_mark_node)
15999 break;
16000 v.safe_push (e);
16001 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16002 break;
16003 cp_lexer_consume_token (parser->lexer);
16006 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
16007 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
16009 end_loc = UNKNOWN_LOCATION;
16010 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
16011 false);
16012 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
16013 cp_lexer_consume_token (parser->lexer);
16014 else
16016 cp_parser_skip_to_end_of_statement (parser);
16017 return error_mark_node;
16021 if (cxx_dialect < cxx17)
16022 pedwarn (loc, OPT_Wc__17_extensions,
16023 "structured bindings only available with "
16024 "%<-std=c++17%> or %<-std=gnu++17%>");
16026 tree pushed_scope;
16027 cp_declarator *declarator = make_declarator (cdk_decomp);
16028 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
16029 declarator->id_loc = loc;
16030 if (ref_qual != REF_QUAL_NONE)
16031 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
16032 ref_qual == REF_QUAL_RVALUE,
16033 NULL_TREE);
16034 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
16035 NULL_TREE, decl_specifiers->attributes,
16036 &pushed_scope);
16037 tree orig_decl = decl;
16039 unsigned int i;
16040 cp_expr e;
16041 cp_decl_specifier_seq decl_specs;
16042 clear_decl_specs (&decl_specs);
16043 decl_specs.type = make_auto ();
16044 if (decl_specifiers->storage_class == sc_static)
16045 decl_specs.storage_class = sc_static;
16046 tree prev = decl;
16047 FOR_EACH_VEC_ELT (v, i, e)
16049 if (i == 0)
16050 declarator = make_id_declarator (NULL_TREE, e.get_value (),
16051 sfk_none, e.get_location ());
16052 else
16054 declarator->u.id.unqualified_name = e.get_value ();
16055 declarator->id_loc = e.get_location ();
16057 tree elt_pushed_scope;
16058 tree decl2 = start_decl (declarator, &decl_specs, SD_DECOMPOSITION,
16059 NULL_TREE, NULL_TREE, &elt_pushed_scope);
16060 if (decl2 == error_mark_node)
16061 decl = error_mark_node;
16062 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
16064 /* Ensure we've diagnosed redeclaration if we aren't creating
16065 a new VAR_DECL. */
16066 gcc_assert (errorcount);
16067 decl = error_mark_node;
16069 else
16070 prev = decl2;
16071 if (elt_pushed_scope)
16072 pop_scope (elt_pushed_scope);
16075 if (v.is_empty ())
16077 error_at (loc, "empty structured binding declaration");
16078 decl = error_mark_node;
16081 if (maybe_range_for_decl == NULL
16082 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16084 bool non_constant_p = false, is_direct_init = false;
16085 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
16086 tree initializer = cp_parser_initializer (parser, &is_direct_init,
16087 &non_constant_p);
16088 if (initializer == NULL_TREE
16089 || (TREE_CODE (initializer) == TREE_LIST
16090 && TREE_CHAIN (initializer))
16091 || (is_direct_init
16092 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
16093 && CONSTRUCTOR_NELTS (initializer) != 1))
16095 error_at (loc, "invalid initializer for structured binding "
16096 "declaration");
16097 initializer = error_mark_node;
16100 if (decl != error_mark_node)
16102 cp_decomp decomp = { prev, v.length () };
16103 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
16104 (is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT),
16105 &decomp);
16106 cp_finish_decomp (decl, &decomp);
16109 else if (decl != error_mark_node)
16111 *maybe_range_for_decl = prev;
16112 cp_decomp decomp = { prev, v.length () };
16113 /* Ensure DECL_VALUE_EXPR is created for all the decls but
16114 the underlying DECL. */
16115 cp_finish_decomp (decl, &decomp);
16118 if (pushed_scope)
16119 pop_scope (pushed_scope);
16121 if (decl == error_mark_node && DECL_P (orig_decl))
16123 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
16124 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
16127 return decl;
16130 /* Names of storage classes. */
16132 static const char *const
16133 cp_storage_class_name[] = {
16134 "", "auto", "register", "static", "extern", "mutable"
16137 /* Parse a decl-specifier-seq.
16139 decl-specifier-seq:
16140 decl-specifier-seq [opt] decl-specifier
16141 decl-specifier attribute-specifier-seq [opt] (C++11)
16143 decl-specifier:
16144 storage-class-specifier
16145 type-specifier
16146 function-specifier
16147 friend
16148 typedef
16150 GNU Extension:
16152 decl-specifier:
16153 attributes
16155 Concepts Extension:
16157 decl-specifier:
16158 concept
16160 Set *DECL_SPECS to a representation of the decl-specifier-seq.
16162 The parser flags FLAGS is used to control type-specifier parsing.
16164 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
16165 flags:
16167 1: one of the decl-specifiers is an elaborated-type-specifier
16168 (i.e., a type declaration)
16169 2: one of the decl-specifiers is an enum-specifier or a
16170 class-specifier (i.e., a type definition)
16174 static void
16175 cp_parser_decl_specifier_seq (cp_parser* parser,
16176 cp_parser_flags flags,
16177 cp_decl_specifier_seq *decl_specs,
16178 int* declares_class_or_enum)
16180 bool constructor_possible_p = !parser->in_declarator_p;
16181 bool found_decl_spec = false;
16182 cp_token *start_token = NULL;
16183 cp_decl_spec ds;
16185 /* Clear DECL_SPECS. */
16186 clear_decl_specs (decl_specs);
16188 /* Assume no class or enumeration type is declared. */
16189 *declares_class_or_enum = 0;
16191 /* Keep reading specifiers until there are no more to read. */
16192 while (true)
16194 bool constructor_p;
16195 cp_token *token;
16196 ds = ds_last;
16198 /* Peek at the next token. */
16199 token = cp_lexer_peek_token (parser->lexer);
16201 /* Save the first token of the decl spec list for error
16202 reporting. */
16203 if (!start_token)
16204 start_token = token;
16205 /* Handle attributes. */
16206 if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) == 0
16207 && cp_next_tokens_can_be_attribute_p (parser))
16209 /* Parse the attributes. */
16210 tree attrs = cp_parser_attributes_opt (parser);
16212 /* In a sequence of declaration specifiers, c++11 attributes
16213 appertain to the type that precede them. In that case
16214 [dcl.spec]/1 says:
16216 The attribute-specifier-seq affects the type only for
16217 the declaration it appears in, not other declarations
16218 involving the same type.
16220 But for now let's force the user to position the
16221 attribute either at the beginning of the declaration or
16222 after the declarator-id, which would clearly mean that it
16223 applies to the declarator. */
16224 if (cxx11_attribute_p (attrs))
16226 if (!found_decl_spec)
16227 /* The c++11 attribute is at the beginning of the
16228 declaration. It appertains to the entity being
16229 declared. */;
16230 else
16232 if (find_contract (attrs))
16234 diagnose_misapplied_contracts (attrs);
16235 attrs = NULL_TREE;
16237 else if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
16239 /* This is an attribute following a
16240 class-specifier. */
16241 if (decl_specs->type_definition_p)
16242 warn_misplaced_attr_for_class_type (token->location,
16243 decl_specs->type);
16244 attrs = NULL_TREE;
16246 else
16248 decl_specs->std_attributes
16249 = attr_chainon (decl_specs->std_attributes, attrs);
16250 if (decl_specs->locations[ds_std_attribute] == 0)
16251 decl_specs->locations[ds_std_attribute] = token->location;
16253 continue;
16257 decl_specs->attributes
16258 = attr_chainon (decl_specs->attributes, attrs);
16259 if (decl_specs->locations[ds_attribute] == 0)
16260 decl_specs->locations[ds_attribute] = token->location;
16261 continue;
16263 /* Special case for "this" specifier, indicating a parm is an xobj parm.
16264 The "this" specifier must be the first specifier in the declaration,
16265 after any attributes. */
16266 if (token->keyword == RID_THIS)
16268 cp_lexer_consume_token (parser->lexer);
16269 set_and_check_decl_spec_loc (decl_specs, ds_this, token);
16270 continue;
16273 /* Assume we will find a decl-specifier keyword. */
16274 found_decl_spec = true;
16275 /* If the next token is an appropriate keyword, we can simply
16276 add it to the list. */
16277 switch (token->keyword)
16279 /* decl-specifier:
16280 friend
16281 constexpr
16282 constinit */
16283 case RID_FRIEND:
16284 if (!at_class_scope_p ())
16286 gcc_rich_location richloc (token->location);
16287 richloc.add_fixit_remove ();
16288 error_at (&richloc, "%<friend%> used outside of class");
16289 cp_lexer_purge_token (parser->lexer);
16291 else
16293 ds = ds_friend;
16294 /* Consume the token. */
16295 cp_lexer_consume_token (parser->lexer);
16297 break;
16299 case RID_CONSTEXPR:
16300 ds = ds_constexpr;
16301 cp_lexer_consume_token (parser->lexer);
16302 break;
16304 case RID_CONSTINIT:
16305 ds = ds_constinit;
16306 cp_lexer_consume_token (parser->lexer);
16307 break;
16309 case RID_CONSTEVAL:
16310 ds = ds_consteval;
16311 cp_lexer_consume_token (parser->lexer);
16312 break;
16314 case RID_CONCEPT:
16315 ds = ds_concept;
16316 cp_lexer_consume_token (parser->lexer);
16318 if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
16319 break;
16321 /* Warn for concept as a decl-specifier. We'll rewrite these as
16322 concept declarations later. */
16323 if (!flag_concepts_ts)
16325 cp_token *next = cp_lexer_peek_token (parser->lexer);
16326 if (next->keyword == RID_BOOL)
16327 permerror (next->location, "the %<bool%> keyword is not "
16328 "allowed in a C++20 concept definition");
16329 else
16330 error_at (token->location, "C++20 concept definition syntax "
16331 "is %<concept <name> = <expr>%>");
16334 /* In C++20 a concept definition is just 'concept name = expr;'
16335 Support that syntax as a TS extension by pretending we've seen
16336 the 'bool' specifier. */
16337 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
16338 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ)
16339 && !decl_specs->any_type_specifiers_p)
16341 cp_parser_set_decl_spec_type (decl_specs, boolean_type_node,
16342 token, /*type_definition*/false);
16343 decl_specs->any_type_specifiers_p = true;
16345 break;
16347 /* function-specifier:
16348 inline
16349 virtual
16350 explicit */
16351 case RID_INLINE:
16352 case RID_VIRTUAL:
16353 case RID_EXPLICIT:
16354 cp_parser_function_specifier_opt (parser, decl_specs);
16355 break;
16357 /* decl-specifier:
16358 typedef */
16359 case RID_TYPEDEF:
16360 ds = ds_typedef;
16361 /* Consume the token. */
16362 cp_lexer_consume_token (parser->lexer);
16364 if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
16365 break;
16367 /* A constructor declarator cannot appear in a typedef. */
16368 constructor_possible_p = false;
16369 /* The "typedef" keyword can only occur in a declaration; we
16370 may as well commit at this point. */
16371 cp_parser_commit_to_tentative_parse (parser);
16373 if (decl_specs->storage_class != sc_none)
16375 if (decl_specs->conflicting_specifiers_p)
16376 break;
16377 gcc_rich_location richloc (token->location);
16378 location_t oloc = decl_specs->locations[ds_storage_class];
16379 richloc.add_location_if_nearby (oloc);
16380 error_at (&richloc,
16381 "%<typedef%> specifier conflicts with %qs",
16382 cp_storage_class_name[decl_specs->storage_class]);
16383 decl_specs->conflicting_specifiers_p = true;
16385 break;
16387 /* storage-class-specifier:
16388 auto
16389 register
16390 static
16391 extern
16392 mutable
16394 GNU Extension:
16395 thread */
16396 case RID_AUTO:
16397 if (cxx_dialect == cxx98)
16399 /* Consume the token. */
16400 cp_lexer_consume_token (parser->lexer);
16402 /* Complain about `auto' as a storage specifier, if
16403 we're complaining about C++0x compatibility. */
16404 gcc_rich_location richloc (token->location);
16405 richloc.add_fixit_remove ();
16406 warning_at (&richloc, OPT_Wc__11_compat,
16407 "%<auto%> changes meaning in C++11; "
16408 "please remove it");
16410 /* Set the storage class anyway. */
16411 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
16412 token);
16414 else
16415 /* C++0x auto type-specifier. */
16416 found_decl_spec = false;
16417 break;
16419 case RID_REGISTER:
16420 case RID_STATIC:
16421 case RID_EXTERN:
16422 case RID_MUTABLE:
16423 /* Consume the token. */
16424 cp_lexer_consume_token (parser->lexer);
16425 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
16426 token);
16427 break;
16428 case RID_THREAD:
16429 /* Consume the token. */
16430 ds = ds_thread;
16431 cp_lexer_consume_token (parser->lexer);
16432 break;
16434 default:
16435 /* We did not yet find a decl-specifier yet. */
16436 found_decl_spec = false;
16437 break;
16440 if (found_decl_spec
16441 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
16442 && token->keyword != RID_CONSTEXPR)
16443 error ("%qD invalid in condition", ridpointers[token->keyword]);
16445 if (found_decl_spec
16446 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
16447 && token->keyword != RID_MUTABLE
16448 && token->keyword != RID_CONSTEXPR
16449 && token->keyword != RID_CONSTEVAL)
16451 if (token->keyword != RID_STATIC)
16452 error_at (token->location, "%qD invalid in lambda",
16453 ridpointers[token->keyword]);
16454 else if (cxx_dialect < cxx23)
16455 pedwarn (token->location, OPT_Wc__23_extensions,
16456 "%qD only valid in lambda with %<-std=c++23%> or "
16457 "%<-std=gnu++23%>", ridpointers[token->keyword]);
16460 if (ds != ds_last)
16461 set_and_check_decl_spec_loc (decl_specs, ds, token);
16463 /* Constructors are a special case. The `S' in `S()' is not a
16464 decl-specifier; it is the beginning of the declarator. */
16465 constructor_p
16466 = (!found_decl_spec
16467 && constructor_possible_p
16468 && (cp_parser_constructor_declarator_p
16469 (parser, flags, decl_spec_seq_has_spec_p (decl_specs,
16470 ds_friend))));
16472 /* If we don't have a DECL_SPEC yet, then we must be looking at
16473 a type-specifier. */
16474 if (!found_decl_spec && !constructor_p)
16476 int decl_spec_declares_class_or_enum;
16477 bool is_cv_qualifier;
16478 tree type_spec;
16480 if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
16481 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
16483 type_spec
16484 = cp_parser_type_specifier (parser, flags,
16485 decl_specs,
16486 /*is_declaration=*/true,
16487 &decl_spec_declares_class_or_enum,
16488 &is_cv_qualifier);
16489 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
16491 /* If this type-specifier referenced a user-defined type
16492 (a typedef, class-name, etc.), then we can't allow any
16493 more such type-specifiers henceforth.
16495 [dcl.spec]
16497 The longest sequence of decl-specifiers that could
16498 possibly be a type name is taken as the
16499 decl-specifier-seq of a declaration. The sequence shall
16500 be self-consistent as described below.
16502 [dcl.type]
16504 As a general rule, at most one type-specifier is allowed
16505 in the complete decl-specifier-seq of a declaration. The
16506 only exceptions are the following:
16508 -- const or volatile can be combined with any other
16509 type-specifier.
16511 -- signed or unsigned can be combined with char, long,
16512 short, or int.
16514 -- ..
16516 Example:
16518 typedef char* Pc;
16519 void g (const int Pc);
16521 Here, Pc is *not* part of the decl-specifier seq; it's
16522 the declarator. Therefore, once we see a type-specifier
16523 (other than a cv-qualifier), we forbid any additional
16524 user-defined types. We *do* still allow things like `int
16525 int' to be considered a decl-specifier-seq, and issue the
16526 error message later. */
16527 if (type_spec && !is_cv_qualifier)
16528 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
16529 /* A constructor declarator cannot follow a type-specifier. */
16530 if (type_spec)
16532 constructor_possible_p = false;
16533 found_decl_spec = true;
16534 if (!is_cv_qualifier)
16535 decl_specs->any_type_specifiers_p = true;
16537 if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) != 0)
16538 error_at (token->location, "type-specifier invalid in lambda");
16542 /* If we still do not have a DECL_SPEC, then there are no more
16543 decl-specifiers. */
16544 if (!found_decl_spec)
16545 break;
16547 if (decl_specs->std_attributes)
16549 error_at (decl_specs->locations[ds_std_attribute],
16550 "standard attributes in middle of decl-specifiers");
16551 inform (decl_specs->locations[ds_std_attribute],
16552 "standard attributes must precede the decl-specifiers to "
16553 "apply to the declaration, or follow them to apply to "
16554 "the type");
16557 decl_specs->any_specifiers_p = true;
16558 /* After we see one decl-specifier, further decl-specifiers are
16559 always optional. */
16560 flags |= CP_PARSER_FLAGS_OPTIONAL;
16563 /* Don't allow a friend specifier with a class definition. */
16564 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
16565 && (*declares_class_or_enum & 2))
16566 error_at (decl_specs->locations[ds_friend],
16567 "class definition may not be declared a friend");
16570 /* Parse an (optional) storage-class-specifier.
16572 storage-class-specifier:
16573 auto
16574 register
16575 static
16576 extern
16577 mutable
16579 GNU Extension:
16581 storage-class-specifier:
16582 thread
16584 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
16586 static tree
16587 cp_parser_storage_class_specifier_opt (cp_parser* parser)
16589 switch (cp_lexer_peek_token (parser->lexer)->keyword)
16591 case RID_AUTO:
16592 if (cxx_dialect != cxx98)
16593 return NULL_TREE;
16594 /* Fall through for C++98. */
16595 gcc_fallthrough ();
16597 case RID_REGISTER:
16598 case RID_STATIC:
16599 case RID_EXTERN:
16600 case RID_MUTABLE:
16601 case RID_THREAD:
16602 /* Consume the token. */
16603 return cp_lexer_consume_token (parser->lexer)->u.value;
16605 default:
16606 return NULL_TREE;
16610 /* Parse an (optional) function-specifier.
16612 function-specifier:
16613 inline
16614 virtual
16615 explicit
16617 C++20 Extension:
16618 explicit(constant-expression)
16620 Returns an IDENTIFIER_NODE corresponding to the keyword used.
16621 Updates DECL_SPECS, if it is non-NULL. */
16623 static tree
16624 cp_parser_function_specifier_opt (cp_parser* parser,
16625 cp_decl_specifier_seq *decl_specs)
16627 cp_token *token = cp_lexer_peek_token (parser->lexer);
16628 switch (token->keyword)
16630 case RID_INLINE:
16631 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
16632 break;
16634 case RID_VIRTUAL:
16635 /* 14.5.2.3 [temp.mem]
16637 A member function template shall not be virtual. */
16638 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
16639 && current_class_type)
16640 error_at (token->location, "templates may not be %<virtual%>");
16641 else
16642 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
16643 break;
16645 case RID_EXPLICIT:
16647 tree id = cp_lexer_consume_token (parser->lexer)->u.value;
16648 /* If we see '(', it's C++20 explicit(bool). */
16649 tree expr;
16650 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16652 matching_parens parens;
16653 parens.consume_open (parser);
16655 /* New types are not allowed in an explicit-specifier. */
16656 const char *saved_message
16657 = parser->type_definition_forbidden_message;
16658 parser->type_definition_forbidden_message
16659 = G_("types may not be defined in explicit-specifier");
16661 if (cxx_dialect < cxx20)
16662 pedwarn (token->location, OPT_Wc__20_extensions,
16663 "%<explicit(bool)%> only available with %<-std=c++20%> "
16664 "or %<-std=gnu++20%>");
16666 /* Parse the constant-expression. */
16667 expr = cp_parser_constant_expression (parser);
16669 /* Restore the saved message. */
16670 parser->type_definition_forbidden_message = saved_message;
16671 parens.require_close (parser);
16673 else
16674 /* The explicit-specifier explicit without a constant-expression is
16675 equivalent to the explicit-specifier explicit(true). */
16676 expr = boolean_true_node;
16678 /* [dcl.fct.spec]
16679 "the constant-expression, if supplied, shall be a contextually
16680 converted constant expression of type bool." */
16681 expr = build_explicit_specifier (expr, tf_warning_or_error);
16682 /* We could evaluate it -- mark the decl as appropriate. */
16683 if (expr == boolean_true_node)
16684 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
16685 else if (expr == boolean_false_node)
16686 /* Don't mark the decl as explicit. */;
16687 else if (decl_specs)
16688 /* The expression was value-dependent. Remember it so that we can
16689 substitute it later. */
16690 decl_specs->explicit_specifier = expr;
16691 return id;
16694 default:
16695 return NULL_TREE;
16698 /* Consume the token. */
16699 return cp_lexer_consume_token (parser->lexer)->u.value;
16702 /* Parse a linkage-specification.
16704 linkage-specification:
16705 extern string-literal { declaration-seq [opt] }
16706 extern string-literal declaration */
16708 static void
16709 cp_parser_linkage_specification (cp_parser* parser, tree prefix_attr)
16711 /* Look for the `extern' keyword. */
16712 cp_token *extern_token
16713 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
16715 /* Look for the string-literal. */
16716 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
16717 tree linkage;
16718 if (cxx_dialect >= cxx26)
16719 linkage = cp_parser_unevaluated_string_literal (parser);
16720 else
16721 linkage = cp_parser_string_literal (parser, /*translate=*/false,
16722 /*wide_ok=*/false);
16724 /* Transform the literal into an identifier. If the literal is a
16725 wide-character string, or contains embedded NULs, then we can't
16726 handle it as the user wants. */
16727 if (linkage == error_mark_node
16728 || strlen (TREE_STRING_POINTER (linkage))
16729 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
16731 cp_parser_error (parser, "invalid linkage-specification");
16732 /* Assume C++ linkage. */
16733 linkage = lang_name_cplusplus;
16735 else
16736 linkage = get_identifier (TREE_STRING_POINTER (linkage));
16738 /* We're now using the new linkage. */
16739 unsigned saved_module = module_kind;
16740 module_kind &= ~MK_ATTACH;
16741 push_lang_context (linkage);
16743 /* Preserve the location of the innermost linkage specification,
16744 tracking the locations of nested specifications via a local. */
16745 location_t saved_location
16746 = parser->innermost_linkage_specification_location;
16747 /* Construct a location ranging from the start of the "extern" to
16748 the end of the string-literal, with the caret at the start, e.g.:
16749 extern "C" {
16750 ^~~~~~~~~~
16752 parser->innermost_linkage_specification_location
16753 = make_location (extern_token->location,
16754 extern_token->location,
16755 get_finish (string_token->location));
16757 /* If the next token is a `{', then we're using the first
16758 production. */
16759 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16761 cp_ensure_no_omp_declare_simd (parser);
16762 cp_ensure_no_oacc_routine (parser);
16764 /* Consume the `{' token. */
16765 matching_braces braces;
16766 braces.consume_open (parser);
16767 /* Parse the declarations. */
16768 cp_parser_declaration_seq_opt (parser);
16769 /* Look for the closing `}'. */
16770 braces.require_close (parser);
16772 /* Otherwise, there's just one declaration. */
16773 else
16775 bool saved_in_unbraced_linkage_specification_p;
16777 saved_in_unbraced_linkage_specification_p
16778 = parser->in_unbraced_linkage_specification_p;
16779 parser->in_unbraced_linkage_specification_p = true;
16780 cp_parser_declaration (parser, prefix_attr);
16781 parser->in_unbraced_linkage_specification_p
16782 = saved_in_unbraced_linkage_specification_p;
16785 /* We're done with the linkage-specification. */
16786 pop_lang_context ();
16787 module_kind = saved_module;
16789 /* Restore location of parent linkage specification, if any. */
16790 parser->innermost_linkage_specification_location = saved_location;
16793 /* Parse a static_assert-declaration.
16795 static_assert-declaration:
16796 static_assert ( constant-expression , string-literal ) ;
16797 static_assert ( constant-expression ) ; (C++17)
16798 static_assert ( constant-expression, conditional-expression ) ; (C++26)
16800 If MEMBER_P, this static_assert is a class member. */
16802 static void
16803 cp_parser_static_assert (cp_parser *parser, bool member_p)
16805 cp_expr condition;
16806 location_t token_loc;
16807 tree message;
16809 /* Peek at the `static_assert' token so we can keep track of exactly
16810 where the static assertion started. */
16811 token_loc = cp_lexer_peek_token (parser->lexer)->location;
16813 /* Look for the `static_assert' keyword. */
16814 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
16815 RT_STATIC_ASSERT))
16816 return;
16818 /* We know we are in a static assertion; commit to any tentative
16819 parse. */
16820 if (cp_parser_parsing_tentatively (parser))
16821 cp_parser_commit_to_tentative_parse (parser);
16823 /* Parse the `(' starting the static assertion condition. */
16824 matching_parens parens;
16825 parens.require_open (parser);
16827 /* Parse the constant-expression. Allow a non-constant expression
16828 here in order to give better diagnostics in finish_static_assert. */
16829 condition
16830 = cp_parser_constant_expression (parser,
16831 /*allow_non_constant_p=*/true,
16832 /*non_constant_p=*/nullptr);
16834 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
16836 if (pedantic && cxx_dialect < cxx17)
16837 pedwarn (input_location, OPT_Wc__17_extensions,
16838 "%<static_assert%> without a message "
16839 "only available with %<-std=c++17%> or %<-std=gnu++17%>");
16840 /* Eat the ')' */
16841 cp_lexer_consume_token (parser->lexer);
16842 message = build_string (1, "");
16843 TREE_TYPE (message) = char_array_type_node;
16844 fix_string_type (message);
16846 else
16848 /* Parse the separating `,'. */
16849 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
16851 /* Parse the message expression. */
16852 bool string_lit = true;
16853 for (unsigned int i = 1; ; ++i)
16855 cp_token *tok = cp_lexer_peek_nth_token (parser->lexer, i);
16856 if (cp_parser_is_pure_string_literal (tok))
16857 continue;
16858 else if (tok->type == CPP_CLOSE_PAREN)
16859 break;
16860 string_lit = false;
16861 break;
16863 if (!string_lit)
16865 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
16866 if (cxx_dialect < cxx26)
16867 pedwarn (loc, OPT_Wc__26_extensions,
16868 "%<static_assert%> with non-string message only "
16869 "available with %<-std=c++2c%> or %<-std=gnu++2c%>");
16871 message = cp_parser_conditional_expression (parser);
16872 if (TREE_CODE (message) == STRING_CST)
16873 message = build1_loc (loc, PAREN_EXPR, TREE_TYPE (message),
16874 message);
16876 else if (cxx_dialect >= cxx26)
16877 message = cp_parser_unevaluated_string_literal (parser);
16878 else
16879 message = cp_parser_string_literal (parser, /*translate=*/false,
16880 /*wide_ok=*/true);
16882 /* A `)' completes the static assertion. */
16883 if (!parens.require_close (parser))
16884 cp_parser_skip_to_closing_parenthesis (parser,
16885 /*recovering=*/true,
16886 /*or_comma=*/false,
16887 /*consume_paren=*/true);
16890 /* A semicolon terminates the declaration. */
16891 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16893 /* Get the location for the static assertion. Use that of the
16894 condition if available, otherwise, use that of the "static_assert"
16895 token. */
16896 location_t assert_loc = condition.get_location ();
16897 if (assert_loc == UNKNOWN_LOCATION)
16898 assert_loc = token_loc;
16900 /* Complete the static assertion, which may mean either processing
16901 the static assert now or saving it for template instantiation. */
16902 finish_static_assert (condition, message, assert_loc, member_p,
16903 /*show_expr_p=*/false);
16906 /* Parse the expression in decltype ( expression ). */
16908 static tree
16909 cp_parser_decltype_expr (cp_parser *parser,
16910 bool &id_expression_or_member_access_p)
16912 cp_token *id_expr_start_token;
16913 tree expr;
16915 /* First, try parsing an id-expression. */
16916 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
16917 cp_parser_parse_tentatively (parser);
16918 expr = cp_parser_id_expression (parser,
16919 /*template_keyword_p=*/false,
16920 /*check_dependency_p=*/true,
16921 /*template_p=*/NULL,
16922 /*declarator_p=*/false,
16923 /*optional_p=*/false);
16925 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
16927 bool non_integral_constant_expression_p = false;
16928 tree id_expression = expr;
16929 cp_id_kind idk;
16930 const char *error_msg;
16932 if (identifier_p (expr))
16933 /* Lookup the name we got back from the id-expression. */
16934 expr = cp_parser_lookup_name_simple (parser, expr,
16935 id_expr_start_token->location);
16937 if (expr
16938 && expr != error_mark_node
16939 && TREE_CODE (expr) != TYPE_DECL
16940 && (TREE_CODE (expr) != BIT_NOT_EXPR
16941 || !TYPE_P (TREE_OPERAND (expr, 0)))
16942 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
16944 /* Complete lookup of the id-expression. */
16945 expr = (finish_id_expression
16946 (id_expression, expr, parser->scope, &idk,
16947 /*integral_constant_expression_p=*/false,
16948 /*allow_non_integral_constant_expression_p=*/true,
16949 &non_integral_constant_expression_p,
16950 /*template_p=*/false,
16951 /*done=*/true,
16952 /*address_p=*/false,
16953 /*template_arg_p=*/false,
16954 &error_msg,
16955 id_expr_start_token->location));
16957 if (error_msg)
16959 /* We found an id-expression, but it was something that we
16960 should not have found. This is an error, not something
16961 we can recover from, so report the error we found and
16962 we'll recover as gracefully as possible. */
16963 cp_parser_parse_definitely (parser);
16964 cp_parser_error (parser, error_msg);
16965 id_expression_or_member_access_p = true;
16966 return error_mark_node;
16970 if (expr
16971 && expr != error_mark_node
16972 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
16973 /* We have an id-expression. */
16974 id_expression_or_member_access_p = true;
16977 if (!id_expression_or_member_access_p)
16979 /* Abort the id-expression parse. */
16980 cp_parser_abort_tentative_parse (parser);
16982 /* Parsing tentatively, again. */
16983 cp_parser_parse_tentatively (parser);
16985 /* Parse a class member access. */
16986 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
16987 /*cast_p=*/false, /*decltype*/true,
16988 /*member_access_only_p=*/true, NULL);
16990 if (expr
16991 && expr != error_mark_node
16992 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
16993 /* We have an id-expression. */
16994 id_expression_or_member_access_p = true;
16997 if (id_expression_or_member_access_p)
16998 /* We have parsed the complete id-expression or member access. */
16999 cp_parser_parse_definitely (parser);
17000 else
17002 /* Abort our attempt to parse an id-expression or member access
17003 expression. */
17004 cp_parser_abort_tentative_parse (parser);
17006 /* Parse a full expression. */
17007 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
17008 /*decltype_p=*/true);
17011 return expr;
17014 /* Parse a `decltype' type. Returns the type.
17016 decltype-specifier:
17017 decltype ( expression )
17018 C++14:
17019 decltype ( auto ) */
17021 static tree
17022 cp_parser_decltype (cp_parser *parser)
17024 bool id_expression_or_member_access_p = false;
17025 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
17027 if (start_token->type == CPP_DECLTYPE)
17029 /* Already parsed. */
17030 cp_lexer_consume_token (parser->lexer);
17031 return saved_checks_value (start_token->u.tree_check_value);
17034 /* Look for the `decltype' token. */
17035 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
17036 return error_mark_node;
17038 /* Parse the opening `('. */
17039 matching_parens parens;
17040 if (!parens.require_open (parser))
17041 return error_mark_node;
17043 /* Since we're going to preserve any side-effects from this parse, set up a
17044 firewall to protect our callers from cp_parser_commit_to_tentative_parse
17045 in the expression. */
17046 tentative_firewall firewall (parser);
17048 /* If in_declarator_p, a reparse as an expression might succeed (60361).
17049 Otherwise, commit now for better diagnostics. */
17050 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
17051 && !parser->in_declarator_p)
17052 cp_parser_commit_to_topmost_tentative_parse (parser);
17054 push_deferring_access_checks (dk_deferred);
17056 tree expr = NULL_TREE;
17058 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO)
17059 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
17061 /* decltype (auto) */
17062 cp_lexer_consume_token (parser->lexer);
17063 if (cxx_dialect < cxx14)
17065 error_at (start_token->location,
17066 "%<decltype(auto)%> type specifier only available with "
17067 "%<-std=c++14%> or %<-std=gnu++14%>");
17068 expr = error_mark_node;
17071 else
17073 /* decltype (expression) */
17075 /* Types cannot be defined in a `decltype' expression. Save away the
17076 old message and set the new one. */
17077 const char *saved_message = parser->type_definition_forbidden_message;
17078 parser->type_definition_forbidden_message
17079 = G_("types may not be defined in %<decltype%> expressions");
17081 /* The restrictions on constant-expressions do not apply inside
17082 decltype expressions. */
17083 bool saved_integral_constant_expression_p
17084 = parser->integral_constant_expression_p;
17085 bool saved_non_integral_constant_expression_p
17086 = parser->non_integral_constant_expression_p;
17087 parser->integral_constant_expression_p = false;
17089 /* Within a parenthesized expression, a `>' token is always
17090 the greater-than operator. */
17091 bool saved_greater_than_is_operator_p
17092 = parser->greater_than_is_operator_p;
17093 parser->greater_than_is_operator_p = true;
17095 /* Don't synthesize an implicit template type parameter here. This
17096 could happen with C++23 code like
17098 void f(decltype(new auto{0}));
17100 where we want to deduce the auto right away so that the parameter
17101 is of type 'int *'. */
17102 auto cleanup = make_temp_override
17103 (parser->auto_is_implicit_function_template_parm_p, false);
17105 /* Do not actually evaluate the expression. */
17106 ++cp_unevaluated_operand;
17108 /* Do not warn about problems with the expression. */
17109 ++c_inhibit_evaluation_warnings;
17111 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
17112 STRIP_ANY_LOCATION_WRAPPER (expr);
17114 /* Go back to evaluating expressions. */
17115 --cp_unevaluated_operand;
17116 --c_inhibit_evaluation_warnings;
17118 /* The `>' token might be the end of a template-id or
17119 template-parameter-list now. */
17120 parser->greater_than_is_operator_p
17121 = saved_greater_than_is_operator_p;
17123 /* Restore the old message and the integral constant expression
17124 flags. */
17125 parser->type_definition_forbidden_message = saved_message;
17126 parser->integral_constant_expression_p
17127 = saved_integral_constant_expression_p;
17128 parser->non_integral_constant_expression_p
17129 = saved_non_integral_constant_expression_p;
17132 /* Parse to the closing `)'. */
17133 if (expr == error_mark_node || !parens.require_close (parser))
17135 cp_parser_skip_to_closing_parenthesis (parser, true, false,
17136 /*consume_paren=*/true);
17137 expr = error_mark_node;
17140 /* If we got a parse error while tentative, bail out now. */
17141 if (cp_parser_error_occurred (parser))
17143 pop_deferring_access_checks ();
17144 return error_mark_node;
17147 if (!expr)
17148 /* Build auto. */
17149 expr = make_decltype_auto ();
17150 else
17151 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
17152 tf_warning_or_error);
17154 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
17155 it again. */
17156 start_token->type = CPP_DECLTYPE;
17157 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
17158 start_token->tree_check_p = true;
17159 start_token->u.tree_check_value->value = expr;
17160 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
17161 start_token->keyword = RID_MAX;
17163 location_t loc = start_token->location;
17164 loc = make_location (loc, loc, parser->lexer);
17165 start_token->location = loc;
17167 cp_lexer_purge_tokens_after (parser->lexer, start_token);
17169 pop_to_parent_deferring_access_checks ();
17171 return expr;
17174 /* Special member functions [gram.special] */
17176 /* Parse a conversion-function-id.
17178 conversion-function-id:
17179 operator conversion-type-id
17181 Returns an IDENTIFIER_NODE representing the operator. */
17183 static tree
17184 cp_parser_conversion_function_id (cp_parser* parser)
17186 tree type;
17187 tree saved_scope;
17188 tree saved_qualifying_scope;
17189 tree saved_object_scope;
17190 tree pushed_scope = NULL_TREE;
17192 /* Look for the `operator' token. */
17193 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
17194 return error_mark_node;
17195 /* When we parse the conversion-type-id, the current scope will be
17196 reset. However, we need that information in able to look up the
17197 conversion function later, so we save it here. */
17198 saved_scope = parser->scope;
17199 saved_qualifying_scope = parser->qualifying_scope;
17200 saved_object_scope = parser->object_scope;
17201 /* We must enter the scope of the class so that the names of
17202 entities declared within the class are available in the
17203 conversion-type-id. For example, consider:
17205 struct S {
17206 typedef int I;
17207 operator I();
17210 S::operator I() { ... }
17212 In order to see that `I' is a type-name in the definition, we
17213 must be in the scope of `S'. */
17214 if (saved_scope)
17215 pushed_scope = push_scope (saved_scope);
17216 /* Parse the conversion-type-id. */
17217 type = cp_parser_conversion_type_id (parser);
17218 /* Leave the scope of the class, if any. */
17219 if (pushed_scope)
17220 pop_scope (pushed_scope);
17221 /* Restore the saved scope. */
17222 parser->scope = saved_scope;
17223 parser->qualifying_scope = saved_qualifying_scope;
17224 parser->object_scope = saved_object_scope;
17225 /* If the TYPE is invalid, indicate failure. */
17226 if (type == error_mark_node)
17227 return error_mark_node;
17228 return make_conv_op_name (type);
17231 /* Parse a conversion-type-id:
17233 conversion-type-id:
17234 type-specifier-seq conversion-declarator [opt]
17236 Returns the TYPE specified. */
17238 static tree
17239 cp_parser_conversion_type_id (cp_parser* parser)
17241 tree attributes;
17242 cp_decl_specifier_seq type_specifiers;
17243 cp_declarator *declarator;
17244 tree type_specified;
17245 const char *saved_message;
17247 /* Parse the attributes. */
17248 attributes = cp_parser_attributes_opt (parser);
17250 saved_message = parser->type_definition_forbidden_message;
17251 parser->type_definition_forbidden_message
17252 = G_("types may not be defined in a conversion-type-id");
17254 /* Parse the type-specifiers. DR 2413 clarifies that `typename' is
17255 optional in conversion-type-id. */
17256 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
17257 /*is_declaration=*/false,
17258 /*is_trailing_return=*/false,
17259 &type_specifiers);
17261 parser->type_definition_forbidden_message = saved_message;
17263 /* If that didn't work, stop. */
17264 if (type_specifiers.type == error_mark_node)
17265 return error_mark_node;
17266 /* Parse the conversion-declarator. */
17267 declarator = cp_parser_conversion_declarator_opt (parser);
17269 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
17270 /*initialized=*/0, &attributes);
17271 if (attributes)
17272 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
17274 /* Don't give this error when parsing tentatively. This happens to
17275 work because we always parse this definitively once. */
17276 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
17277 && type_uses_auto (type_specified))
17279 if (cxx_dialect < cxx14)
17281 error ("invalid use of %<auto%> in conversion operator");
17282 return error_mark_node;
17284 else if (template_parm_scope_p ())
17285 warning (0, "use of %<auto%> in member template "
17286 "conversion operator can never be deduced");
17289 return type_specified;
17292 /* Parse an (optional) conversion-declarator.
17294 conversion-declarator:
17295 ptr-operator conversion-declarator [opt]
17299 static cp_declarator *
17300 cp_parser_conversion_declarator_opt (cp_parser* parser)
17302 enum tree_code code;
17303 tree class_type, std_attributes = NULL_TREE;
17304 cp_cv_quals cv_quals;
17306 /* We don't know if there's a ptr-operator next, or not. */
17307 cp_parser_parse_tentatively (parser);
17308 /* Try the ptr-operator. */
17309 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
17310 &std_attributes);
17311 /* If it worked, look for more conversion-declarators. */
17312 if (cp_parser_parse_definitely (parser))
17314 cp_declarator *declarator;
17316 /* Parse another optional declarator. */
17317 declarator = cp_parser_conversion_declarator_opt (parser);
17319 declarator = cp_parser_make_indirect_declarator
17320 (code, class_type, cv_quals, declarator, std_attributes);
17322 return declarator;
17325 return NULL;
17328 /* Parse an (optional) ctor-initializer.
17330 ctor-initializer:
17331 : mem-initializer-list */
17333 static void
17334 cp_parser_ctor_initializer_opt (cp_parser* parser)
17336 /* If the next token is not a `:', then there is no
17337 ctor-initializer. */
17338 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17340 /* Do default initialization of any bases and members. */
17341 if (DECL_CONSTRUCTOR_P (current_function_decl))
17342 finish_mem_initializers (NULL_TREE);
17343 return;
17346 /* Consume the `:' token. */
17347 cp_lexer_consume_token (parser->lexer);
17348 /* And the mem-initializer-list. */
17349 cp_parser_mem_initializer_list (parser);
17352 /* Parse a mem-initializer-list.
17354 mem-initializer-list:
17355 mem-initializer ... [opt]
17356 mem-initializer ... [opt] , mem-initializer-list */
17358 static void
17359 cp_parser_mem_initializer_list (cp_parser* parser)
17361 tree mem_initializer_list = NULL_TREE;
17362 tree target_ctor = error_mark_node;
17363 cp_token *token = cp_lexer_peek_token (parser->lexer);
17365 /* Let the semantic analysis code know that we are starting the
17366 mem-initializer-list. */
17367 if (!DECL_CONSTRUCTOR_P (current_function_decl))
17368 error_at (token->location,
17369 "only constructors take member initializers");
17371 /* Loop through the list. */
17372 while (true)
17374 tree mem_initializer;
17376 token = cp_lexer_peek_token (parser->lexer);
17377 /* Parse the mem-initializer. */
17378 mem_initializer = cp_parser_mem_initializer (parser);
17379 /* If the next token is a `...', we're expanding member initializers. */
17380 bool ellipsis = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
17381 if (ellipsis
17382 || (mem_initializer != error_mark_node
17383 && check_for_bare_parameter_packs (TREE_PURPOSE
17384 (mem_initializer))))
17386 /* Consume the `...'. */
17387 if (ellipsis)
17388 cp_lexer_consume_token (parser->lexer);
17390 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
17391 can be expanded but members cannot. */
17392 if (mem_initializer != error_mark_node
17393 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
17395 error_at (token->location,
17396 "cannot expand initializer for member %qD",
17397 TREE_PURPOSE (mem_initializer));
17398 mem_initializer = error_mark_node;
17401 /* Construct the pack expansion type. */
17402 if (mem_initializer != error_mark_node)
17403 mem_initializer = make_pack_expansion (mem_initializer);
17405 if (target_ctor != error_mark_node
17406 && mem_initializer != error_mark_node)
17408 error ("mem-initializer for %qD follows constructor delegation",
17409 TREE_PURPOSE (mem_initializer));
17410 mem_initializer = error_mark_node;
17412 /* Look for a target constructor. */
17413 if (mem_initializer != error_mark_node
17414 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
17415 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
17417 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
17418 if (mem_initializer_list)
17420 error ("constructor delegation follows mem-initializer for %qD",
17421 TREE_PURPOSE (mem_initializer_list));
17422 mem_initializer = error_mark_node;
17424 target_ctor = mem_initializer;
17426 /* Add it to the list, unless it was erroneous. */
17427 if (mem_initializer != error_mark_node)
17429 TREE_CHAIN (mem_initializer) = mem_initializer_list;
17430 mem_initializer_list = mem_initializer;
17432 /* If the next token is not a `,', we're done. */
17433 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17434 break;
17435 /* Consume the `,' token. */
17436 cp_lexer_consume_token (parser->lexer);
17439 /* Perform semantic analysis. */
17440 if (DECL_CONSTRUCTOR_P (current_function_decl))
17441 finish_mem_initializers (mem_initializer_list);
17444 /* Parse a mem-initializer.
17446 mem-initializer:
17447 mem-initializer-id ( expression-list [opt] )
17448 mem-initializer-id braced-init-list
17450 GNU extension:
17452 mem-initializer:
17453 ( expression-list [opt] )
17455 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
17456 class) or FIELD_DECL (for a non-static data member) to initialize;
17457 the TREE_VALUE is the expression-list. An empty initialization
17458 list is represented by void_list_node. */
17460 static tree
17461 cp_parser_mem_initializer (cp_parser* parser)
17463 tree mem_initializer_id;
17464 tree expression_list;
17465 tree member;
17466 cp_token *token = cp_lexer_peek_token (parser->lexer);
17468 /* Find out what is being initialized. */
17469 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17471 permerror (token->location,
17472 "anachronistic old-style base class initializer");
17473 mem_initializer_id = NULL_TREE;
17475 else
17477 mem_initializer_id = cp_parser_mem_initializer_id (parser);
17478 if (mem_initializer_id == error_mark_node)
17479 return mem_initializer_id;
17481 member = expand_member_init (mem_initializer_id);
17482 if (member && !DECL_P (member))
17483 in_base_initializer = 1;
17485 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17487 cp_lexer_set_source_position (parser->lexer);
17488 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17489 expression_list = cp_parser_braced_list (parser);
17490 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
17491 expression_list = build_tree_list (NULL_TREE, expression_list);
17493 else
17495 vec<tree, va_gc> *vec;
17496 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
17497 /*cast_p=*/false,
17498 /*allow_expansion_p=*/true,
17499 /*non_constant_p=*/NULL,
17500 /*close_paren_loc=*/NULL,
17501 /*wrap_locations_p=*/true);
17502 if (vec == NULL)
17503 return error_mark_node;
17504 expression_list = build_tree_list_vec (vec);
17505 release_tree_vector (vec);
17508 if (expression_list == error_mark_node)
17509 return error_mark_node;
17510 if (!expression_list)
17511 expression_list = void_type_node;
17513 in_base_initializer = 0;
17515 if (!member)
17516 return error_mark_node;
17517 tree node = build_tree_list (member, expression_list);
17519 /* We can't attach the source location of this initializer directly to
17520 the list node, so we instead attach it to a dummy EMPTY_CLASS_EXPR
17521 within the TREE_TYPE of the list node. */
17522 location_t loc
17523 = make_location (token->location, token->location, parser->lexer);
17524 tree dummy = build0 (EMPTY_CLASS_EXPR, NULL_TREE);
17525 SET_EXPR_LOCATION (dummy, loc);
17526 TREE_TYPE (node) = dummy;
17528 return node;
17531 /* Parse a mem-initializer-id.
17533 mem-initializer-id:
17534 :: [opt] nested-name-specifier [opt] class-name
17535 decltype-specifier (C++11)
17536 identifier
17538 Returns a TYPE indicating the class to be initialized for the first
17539 production (and the second in C++11). Returns an IDENTIFIER_NODE
17540 indicating the data member to be initialized for the last production. */
17542 static tree
17543 cp_parser_mem_initializer_id (cp_parser* parser)
17545 bool global_scope_p;
17546 bool nested_name_specifier_p;
17547 bool template_p = false;
17548 tree id;
17550 cp_token *token = cp_lexer_peek_token (parser->lexer);
17552 /* `typename' is not allowed in this context ([temp.res]). */
17553 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
17555 error_at (token->location,
17556 "keyword %<typename%> not allowed in this context (a qualified "
17557 "member initializer is implicitly a type)");
17558 cp_lexer_consume_token (parser->lexer);
17560 /* Look for the optional `::' operator. */
17561 global_scope_p
17562 = (cp_parser_global_scope_opt (parser,
17563 /*current_scope_valid_p=*/false)
17564 != NULL_TREE);
17565 /* Look for the optional nested-name-specifier. The simplest way to
17566 implement:
17568 [temp.res]
17570 The keyword `typename' is not permitted in a base-specifier or
17571 mem-initializer; in these contexts a qualified name that
17572 depends on a template-parameter is implicitly assumed to be a
17573 type name.
17575 is to assume that we have seen the `typename' keyword at this
17576 point. */
17577 nested_name_specifier_p
17578 = (cp_parser_nested_name_specifier_opt (parser,
17579 /*typename_keyword_p=*/true,
17580 /*check_dependency_p=*/true,
17581 /*type_p=*/true,
17582 /*is_declaration=*/true)
17583 != NULL_TREE);
17584 if (nested_name_specifier_p)
17585 template_p = cp_parser_optional_template_keyword (parser);
17586 /* If there is a `::' operator or a nested-name-specifier, then we
17587 are definitely looking for a class-name. */
17588 if (global_scope_p || nested_name_specifier_p)
17589 return cp_parser_class_name (parser,
17590 /*typename_keyword_p=*/true,
17591 /*template_keyword_p=*/template_p,
17592 typename_type,
17593 /*check_dependency_p=*/true,
17594 /*class_head_p=*/false,
17595 /*is_declaration=*/true);
17596 /* Otherwise, we could also be looking for an ordinary identifier. */
17597 cp_parser_parse_tentatively (parser);
17598 if (cp_lexer_next_token_is_decltype (parser->lexer))
17599 /* Try a decltype-specifier. */
17600 id = cp_parser_decltype (parser);
17601 else
17602 /* Otherwise, try a class-name. */
17603 id = cp_parser_class_name (parser,
17604 /*typename_keyword_p=*/true,
17605 /*template_keyword_p=*/false,
17606 none_type,
17607 /*check_dependency_p=*/true,
17608 /*class_head_p=*/false,
17609 /*is_declaration=*/true);
17610 /* If we found one, we're done. */
17611 if (cp_parser_parse_definitely (parser))
17612 return id;
17613 /* Otherwise, look for an ordinary identifier. */
17614 return cp_parser_identifier (parser);
17617 /* Overloading [gram.over] */
17619 /* Parse an operator-function-id.
17621 operator-function-id:
17622 operator operator
17624 Returns an IDENTIFIER_NODE for the operator which is a
17625 human-readable spelling of the identifier, e.g., `operator +'. */
17627 static cp_expr
17628 cp_parser_operator_function_id (cp_parser* parser)
17630 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
17631 /* Look for the `operator' keyword. */
17632 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
17633 return error_mark_node;
17634 /* And then the name of the operator itself. */
17635 return cp_parser_operator (parser, start_loc);
17638 /* Return an identifier node for a user-defined literal operator.
17639 The suffix identifier is chained to the operator name identifier. */
17641 tree
17642 cp_literal_operator_id (const char* name)
17644 tree identifier;
17645 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
17646 + strlen (name) + 10);
17647 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
17648 identifier = get_identifier (buffer);
17649 XDELETEVEC (buffer);
17651 return identifier;
17654 /* Parse an operator.
17656 operator:
17657 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
17658 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
17659 || ++ -- , ->* -> () []
17661 GNU Extensions:
17663 operator:
17664 <? >? <?= >?=
17666 Returns an IDENTIFIER_NODE for the operator which is a
17667 human-readable spelling of the identifier, e.g., `operator +'. */
17669 static cp_expr
17670 cp_parser_operator (cp_parser* parser, location_t start_loc)
17672 tree id = NULL_TREE;
17673 cp_token *token;
17674 bool utf8 = false;
17676 /* Peek at the next token. */
17677 token = cp_lexer_peek_token (parser->lexer);
17679 location_t end_loc = token->location;
17681 /* Figure out which operator we have. */
17682 enum tree_code op = ERROR_MARK;
17683 bool assop = false;
17684 bool consumed = false;
17685 switch (token->type)
17687 case CPP_KEYWORD:
17689 /* The keyword should be either `new', `delete' or `co_await'. */
17690 if (token->keyword == RID_NEW)
17691 op = NEW_EXPR;
17692 else if (token->keyword == RID_DELETE)
17693 op = DELETE_EXPR;
17694 else if (token->keyword == RID_CO_AWAIT)
17695 op = CO_AWAIT_EXPR;
17696 else
17697 break;
17699 /* Consume the `new', `delete' or co_await token. */
17700 end_loc = cp_lexer_consume_token (parser->lexer)->location;
17702 /* Peek at the next token. */
17703 token = cp_lexer_peek_token (parser->lexer);
17704 /* If it's a `[' token then this is the array variant of the
17705 operator. */
17706 if (token->type == CPP_OPEN_SQUARE
17707 && op != CO_AWAIT_EXPR)
17709 /* Consume the `[' token. */
17710 cp_lexer_consume_token (parser->lexer);
17711 /* Look for the `]' token. */
17712 if (cp_token *close_token
17713 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
17714 end_loc = close_token->location;
17715 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
17717 consumed = true;
17718 break;
17721 case CPP_PLUS:
17722 op = PLUS_EXPR;
17723 break;
17725 case CPP_MINUS:
17726 op = MINUS_EXPR;
17727 break;
17729 case CPP_MULT:
17730 op = MULT_EXPR;
17731 break;
17733 case CPP_DIV:
17734 op = TRUNC_DIV_EXPR;
17735 break;
17737 case CPP_MOD:
17738 op = TRUNC_MOD_EXPR;
17739 break;
17741 case CPP_XOR:
17742 op = BIT_XOR_EXPR;
17743 break;
17745 case CPP_AND:
17746 op = BIT_AND_EXPR;
17747 break;
17749 case CPP_OR:
17750 op = BIT_IOR_EXPR;
17751 break;
17753 case CPP_COMPL:
17754 op = BIT_NOT_EXPR;
17755 break;
17757 case CPP_NOT:
17758 op = TRUTH_NOT_EXPR;
17759 break;
17761 case CPP_EQ:
17762 assop = true;
17763 op = NOP_EXPR;
17764 break;
17766 case CPP_LESS:
17767 op = LT_EXPR;
17768 break;
17770 case CPP_GREATER:
17771 op = GT_EXPR;
17772 break;
17774 case CPP_PLUS_EQ:
17775 assop = true;
17776 op = PLUS_EXPR;
17777 break;
17779 case CPP_MINUS_EQ:
17780 assop = true;
17781 op = MINUS_EXPR;
17782 break;
17784 case CPP_MULT_EQ:
17785 assop = true;
17786 op = MULT_EXPR;
17787 break;
17789 case CPP_DIV_EQ:
17790 assop = true;
17791 op = TRUNC_DIV_EXPR;
17792 break;
17794 case CPP_MOD_EQ:
17795 assop = true;
17796 op = TRUNC_MOD_EXPR;
17797 break;
17799 case CPP_XOR_EQ:
17800 assop = true;
17801 op = BIT_XOR_EXPR;
17802 break;
17804 case CPP_AND_EQ:
17805 assop = true;
17806 op = BIT_AND_EXPR;
17807 break;
17809 case CPP_OR_EQ:
17810 assop = true;
17811 op = BIT_IOR_EXPR;
17812 break;
17814 case CPP_LSHIFT:
17815 op = LSHIFT_EXPR;
17816 break;
17818 case CPP_RSHIFT:
17819 op = RSHIFT_EXPR;
17820 break;
17822 case CPP_LSHIFT_EQ:
17823 assop = true;
17824 op = LSHIFT_EXPR;
17825 break;
17827 case CPP_RSHIFT_EQ:
17828 assop = true;
17829 op = RSHIFT_EXPR;
17830 break;
17832 case CPP_EQ_EQ:
17833 op = EQ_EXPR;
17834 break;
17836 case CPP_NOT_EQ:
17837 op = NE_EXPR;
17838 break;
17840 case CPP_LESS_EQ:
17841 op = LE_EXPR;
17842 break;
17844 case CPP_GREATER_EQ:
17845 op = GE_EXPR;
17846 break;
17848 case CPP_SPACESHIP:
17849 op = SPACESHIP_EXPR;
17850 break;
17852 case CPP_AND_AND:
17853 op = TRUTH_ANDIF_EXPR;
17854 break;
17856 case CPP_OR_OR:
17857 op = TRUTH_ORIF_EXPR;
17858 break;
17860 case CPP_PLUS_PLUS:
17861 op = POSTINCREMENT_EXPR;
17862 break;
17864 case CPP_MINUS_MINUS:
17865 op = PREDECREMENT_EXPR;
17866 break;
17868 case CPP_COMMA:
17869 op = COMPOUND_EXPR;
17870 break;
17872 case CPP_DEREF_STAR:
17873 op = MEMBER_REF;
17874 break;
17876 case CPP_DEREF:
17877 op = COMPONENT_REF;
17878 break;
17880 case CPP_QUERY:
17881 op = COND_EXPR;
17882 /* Consume the `?'. */
17883 cp_lexer_consume_token (parser->lexer);
17884 /* Look for the matching `:'. */
17885 cp_parser_require (parser, CPP_COLON, RT_COLON);
17886 consumed = true;
17887 break;
17889 case CPP_OPEN_PAREN:
17891 /* Consume the `('. */
17892 matching_parens parens;
17893 parens.consume_open (parser);
17894 /* Look for the matching `)'. */
17895 token = parens.require_close (parser);
17896 if (token)
17897 end_loc = token->location;
17898 op = CALL_EXPR;
17899 consumed = true;
17900 break;
17903 case CPP_OPEN_SQUARE:
17904 /* Consume the `['. */
17905 cp_lexer_consume_token (parser->lexer);
17906 /* Look for the matching `]'. */
17907 token = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
17908 if (token)
17909 end_loc = token->location;
17910 op = ARRAY_REF;
17911 consumed = true;
17912 break;
17914 case CPP_UTF8STRING:
17915 case CPP_UTF8STRING_USERDEF:
17916 utf8 = true;
17917 /* FALLTHRU */
17918 case CPP_STRING:
17919 case CPP_WSTRING:
17920 case CPP_STRING16:
17921 case CPP_STRING32:
17922 case CPP_STRING_USERDEF:
17923 case CPP_WSTRING_USERDEF:
17924 case CPP_STRING16_USERDEF:
17925 case CPP_STRING32_USERDEF:
17927 tree string_tree;
17928 int sz, len;
17930 if (cxx_dialect == cxx98)
17931 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
17933 /* Consume the string. */
17934 cp_expr str = cp_parser_userdef_string_literal (parser,
17935 /*lookup_udlit=*/false);
17936 if (str == error_mark_node)
17937 return error_mark_node;
17938 else if (TREE_CODE (str) == USERDEF_LITERAL)
17940 string_tree = USERDEF_LITERAL_VALUE (str.get_value ());
17941 id = USERDEF_LITERAL_SUFFIX_ID (str.get_value ());
17942 end_loc = str.get_location ();
17944 else
17946 string_tree = str;
17947 /* Look for the suffix identifier. */
17948 token = cp_lexer_peek_token (parser->lexer);
17949 if (token->type == CPP_NAME)
17951 id = cp_parser_identifier (parser);
17952 end_loc = token->location;
17954 else if (token->type == CPP_KEYWORD)
17956 error ("unexpected keyword;"
17957 " remove space between quotes and suffix identifier");
17958 return error_mark_node;
17960 else
17962 error ("expected suffix identifier");
17963 return error_mark_node;
17966 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
17967 (TREE_TYPE (TREE_TYPE (string_tree))));
17968 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
17969 if (len != 0)
17971 error ("expected empty string after %<operator%> keyword");
17972 return error_mark_node;
17974 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
17975 != char_type_node)
17977 error ("invalid encoding prefix in literal operator");
17978 return error_mark_node;
17980 if (id != error_mark_node)
17982 const char *name = IDENTIFIER_POINTER (id);
17983 id = cp_literal_operator_id (name);
17985 /* Generate a location of the form:
17986 "" _suffix_identifier
17987 ^~~~~~~~~~~~~~~~~~~~~
17988 with caret == start at the start token, finish at the end of the
17989 suffix identifier. */
17990 location_t combined_loc
17991 = make_location (start_loc, start_loc, parser->lexer);
17992 return cp_expr (id, combined_loc);
17995 default:
17996 /* Anything else is an error. */
17997 break;
18000 /* If we have selected an identifier, we need to consume the
18001 operator token. */
18002 if (op != ERROR_MARK)
18004 id = ovl_op_identifier (assop, op);
18005 if (!consumed)
18006 cp_lexer_consume_token (parser->lexer);
18008 /* Otherwise, no valid operator name was present. */
18009 else
18011 cp_parser_error (parser, "expected operator");
18012 id = error_mark_node;
18015 start_loc = make_location (start_loc, start_loc, get_finish (end_loc));
18016 return cp_expr (id, start_loc);
18019 /* Parse a template-declaration.
18021 template-declaration:
18022 export [opt] template < template-parameter-list > declaration
18024 If MEMBER_P is TRUE, this template-declaration occurs within a
18025 class-specifier.
18027 The grammar rule given by the standard isn't correct. What
18028 is really meant is:
18030 template-declaration:
18031 export [opt] template-parameter-list-seq
18032 decl-specifier-seq [opt] init-declarator [opt] ;
18033 export [opt] template-parameter-list-seq
18034 function-definition
18036 template-parameter-list-seq:
18037 template-parameter-list-seq [opt]
18038 template < template-parameter-list >
18040 Concept Extensions:
18042 template-parameter-list-seq:
18043 template < template-parameter-list > requires-clause [opt]
18045 requires-clause:
18046 requires logical-or-expression */
18048 static void
18049 cp_parser_template_declaration (cp_parser* parser, bool member_p)
18051 /* Check for `export'. */
18052 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
18054 /* Consume the `export' token. */
18055 cp_lexer_consume_token (parser->lexer);
18056 /* Warn that this use of export is deprecated. */
18057 if (cxx_dialect < cxx11)
18058 warning (0, "keyword %<export%> not implemented, and will be ignored");
18059 else if (cxx_dialect < cxx20)
18060 warning (0, "keyword %<export%> is deprecated, and is ignored");
18061 else
18062 warning (0, "keyword %<export%> is enabled with %<-fmodules-ts%>");
18065 cp_parser_template_declaration_after_export (parser, member_p);
18068 /* Parse a template-parameter-list.
18070 template-parameter-list:
18071 template-parameter
18072 template-parameter-list , template-parameter
18074 Returns a TREE_LIST. Each node represents a template parameter.
18075 The nodes are connected via their TREE_CHAINs. */
18077 static tree
18078 cp_parser_template_parameter_list (cp_parser* parser)
18080 tree parameter_list = NULL_TREE;
18082 /* Don't create wrapper nodes within a template-parameter-list,
18083 since we don't want to have different types based on the
18084 spelling location of constants and decls within them. */
18085 auto_suppress_location_wrappers sentinel;
18087 begin_template_parm_list ();
18089 /* The loop below parses the template parms. We first need to know
18090 the total number of template parms to be able to compute proper
18091 canonical types of each dependent type. So after the loop, when
18092 we know the total number of template parms,
18093 end_template_parm_list computes the proper canonical types and
18094 fixes up the dependent types accordingly. */
18095 while (true)
18097 tree parameter;
18098 bool is_non_type;
18099 bool is_parameter_pack;
18100 location_t parm_loc;
18102 /* Parse the template-parameter. */
18103 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
18104 parameter = cp_parser_template_parameter (parser,
18105 &is_non_type,
18106 &is_parameter_pack);
18107 /* Add it to the list. */
18108 if (parameter != error_mark_node)
18109 parameter_list = process_template_parm (parameter_list,
18110 parm_loc,
18111 parameter,
18112 is_non_type,
18113 is_parameter_pack);
18114 else
18116 tree err_parm = build_tree_list (parameter, parameter);
18117 parameter_list = chainon (parameter_list, err_parm);
18120 /* If the next token is not a `,', we're done. */
18121 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18122 break;
18123 /* Otherwise, consume the `,' token. */
18124 cp_lexer_consume_token (parser->lexer);
18127 return end_template_parm_list (parameter_list);
18130 /* Parse a introduction-list.
18132 introduction-list:
18133 introduced-parameter
18134 introduction-list , introduced-parameter
18136 introduced-parameter:
18137 ...[opt] identifier
18139 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
18140 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
18141 WILDCARD_DECL will also have DECL_NAME set and token location in
18142 DECL_SOURCE_LOCATION. */
18144 static tree
18145 cp_parser_introduction_list (cp_parser *parser)
18147 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
18149 while (true)
18151 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
18152 if (is_pack)
18153 cp_lexer_consume_token (parser->lexer);
18155 tree identifier = cp_parser_identifier (parser);
18156 if (identifier == error_mark_node)
18157 break;
18159 /* Build placeholder. */
18160 tree parm = build_nt (WILDCARD_DECL);
18161 DECL_SOURCE_LOCATION (parm)
18162 = cp_lexer_peek_token (parser->lexer)->location;
18163 DECL_NAME (parm) = identifier;
18164 WILDCARD_PACK_P (parm) = is_pack;
18165 vec_safe_push (introduction_vec, parm);
18167 /* If the next token is not a `,', we're done. */
18168 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18169 break;
18170 /* Otherwise, consume the `,' token. */
18171 cp_lexer_consume_token (parser->lexer);
18174 /* Convert the vec into a TREE_VEC. */
18175 tree introduction_list = make_tree_vec (introduction_vec->length ());
18176 unsigned int n;
18177 tree parm;
18178 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
18179 TREE_VEC_ELT (introduction_list, n) = parm;
18181 release_tree_vector (introduction_vec);
18182 return introduction_list;
18185 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
18186 is an abstract declarator. */
18188 static inline cp_declarator*
18189 get_id_declarator (cp_declarator *declarator)
18191 cp_declarator *d = declarator;
18192 while (d && d->kind != cdk_id)
18193 d = d->declarator;
18194 return d;
18197 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
18198 is an abstract declarator. */
18200 static inline tree
18201 get_unqualified_id (cp_declarator *declarator)
18203 declarator = get_id_declarator (declarator);
18204 if (declarator)
18205 return declarator->u.id.unqualified_name;
18206 else
18207 return NULL_TREE;
18210 /* Returns true if TYPE would declare a constrained constrained-parameter. */
18212 static inline bool
18213 is_constrained_parameter (tree type)
18215 return (type
18216 && TREE_CODE (type) == TYPE_DECL
18217 && CONSTRAINED_PARM_CONCEPT (type)
18218 && DECL_P (CONSTRAINED_PARM_CONCEPT (type)));
18221 /* Returns true if PARM declares a constrained-parameter. */
18223 static inline bool
18224 is_constrained_parameter (cp_parameter_declarator *parm)
18226 return is_constrained_parameter (parm->decl_specifiers.type);
18229 /* Check that the type parameter is only a declarator-id, and that its
18230 type is not cv-qualified. */
18232 bool
18233 cp_parser_check_constrained_type_parm (cp_parser *parser,
18234 cp_parameter_declarator *parm)
18236 if (!parm->declarator)
18237 return true;
18239 if (parm->declarator->kind != cdk_id)
18241 cp_parser_error (parser, "invalid constrained type parameter");
18242 return false;
18245 /* Don't allow cv-qualified type parameters. */
18246 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
18247 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
18249 cp_parser_error (parser, "cv-qualified type parameter");
18250 return false;
18253 return true;
18256 /* Finish parsing/processing a template type parameter and checking
18257 various restrictions. */
18259 static inline tree
18260 cp_parser_constrained_type_template_parm (cp_parser *parser,
18261 tree id,
18262 cp_parameter_declarator* parmdecl)
18264 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
18265 return finish_template_type_parm (class_type_node, id);
18266 else
18267 return error_mark_node;
18270 static tree
18271 finish_constrained_template_template_parm (tree proto, tree id)
18273 /* FIXME: This should probably be copied, and we may need to adjust
18274 the template parameter depths. */
18275 tree saved_parms = current_template_parms;
18276 begin_template_parm_list ();
18277 current_template_parms = DECL_TEMPLATE_PARMS (proto);
18278 end_template_parm_list ();
18280 tree parm = finish_template_template_parm (class_type_node, id);
18281 current_template_parms = saved_parms;
18283 return parm;
18286 /* Finish parsing/processing a template template parameter by borrowing
18287 the template parameter list from the prototype parameter. */
18289 static tree
18290 cp_parser_constrained_template_template_parm (cp_parser *parser,
18291 tree proto,
18292 tree id,
18293 cp_parameter_declarator *parmdecl)
18295 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
18296 return error_mark_node;
18297 return finish_constrained_template_template_parm (proto, id);
18300 /* Create a new non-type template parameter from the given PARM
18301 declarator. */
18303 static tree
18304 cp_parser_constrained_non_type_template_parm (bool *is_non_type,
18305 cp_parameter_declarator *parm)
18307 *is_non_type = true;
18308 cp_declarator *decl = parm->declarator;
18309 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
18310 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
18311 return grokdeclarator (decl, specs, TPARM, 0, NULL);
18314 /* Build a constrained template parameter based on the PARMDECL
18315 declarator. The type of PARMDECL is the constrained type, which
18316 refers to the prototype template parameter that ultimately
18317 specifies the type of the declared parameter. */
18319 static tree
18320 finish_constrained_parameter (cp_parser *parser,
18321 cp_parameter_declarator *parmdecl,
18322 bool *is_non_type)
18324 tree decl = parmdecl->decl_specifiers.type;
18325 tree id = get_unqualified_id (parmdecl->declarator);
18326 tree def = parmdecl->default_argument;
18327 tree proto = DECL_INITIAL (decl);
18329 /* Build the parameter. Return an error if the declarator was invalid. */
18330 tree parm;
18331 if (TREE_CODE (proto) == TYPE_DECL)
18332 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
18333 else if (TREE_CODE (proto) == TEMPLATE_DECL)
18334 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
18335 parmdecl);
18336 else
18337 parm = cp_parser_constrained_non_type_template_parm (is_non_type, parmdecl);
18338 if (parm == error_mark_node)
18339 return error_mark_node;
18341 /* Finish the parameter decl and create a node attaching the
18342 default argument and constraint. */
18343 parm = build_tree_list (def, parm);
18344 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
18346 return parm;
18349 /* Returns true if the parsed type actually represents the declaration
18350 of a type template-parameter. */
18352 static bool
18353 declares_constrained_type_template_parameter (tree type)
18355 return (is_constrained_parameter (type)
18356 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
18359 /* Returns true if the parsed type actually represents the declaration of
18360 a template template-parameter. */
18362 static bool
18363 declares_constrained_template_template_parameter (tree type)
18365 return (is_constrained_parameter (type)
18366 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
18369 /* Parse a default argument for a type template-parameter.
18370 Note that diagnostics are handled in cp_parser_template_parameter. */
18372 static tree
18373 cp_parser_default_type_template_argument (cp_parser *parser)
18375 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
18377 /* Consume the `=' token. */
18378 cp_lexer_consume_token (parser->lexer);
18380 cp_token *token = cp_lexer_peek_token (parser->lexer);
18382 /* Tell cp_parser_lambda_expression this is a default argument. */
18383 auto lvf = make_temp_override (parser->local_variables_forbidden_p);
18384 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
18386 /* Parse the default-argument. */
18387 push_deferring_access_checks (dk_no_deferred);
18388 tree default_argument = cp_parser_type_id (parser,
18389 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
18390 NULL);
18391 pop_deferring_access_checks ();
18393 if (flag_concepts && type_uses_auto (default_argument))
18395 error_at (token->location,
18396 "invalid use of %<auto%> in default template argument");
18397 return error_mark_node;
18400 return default_argument;
18403 /* Parse a default argument for a template template-parameter. */
18405 static tree
18406 cp_parser_default_template_template_argument (cp_parser *parser)
18408 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
18410 bool is_template;
18412 /* Consume the `='. */
18413 cp_lexer_consume_token (parser->lexer);
18414 /* Parse the id-expression. */
18415 push_deferring_access_checks (dk_no_deferred);
18416 /* save token before parsing the id-expression, for error
18417 reporting */
18418 const cp_token* token = cp_lexer_peek_token (parser->lexer);
18419 tree default_argument
18420 = cp_parser_id_expression (parser,
18421 /*template_keyword_p=*/false,
18422 /*check_dependency_p=*/true,
18423 /*template_p=*/&is_template,
18424 /*declarator_p=*/false,
18425 /*optional_p=*/false);
18426 if (TREE_CODE (default_argument) == TYPE_DECL)
18427 /* If the id-expression was a template-id that refers to
18428 a template-class, we already have the declaration here,
18429 so no further lookup is needed. */
18431 else
18432 /* Look up the name. */
18433 default_argument
18434 = cp_parser_lookup_name (parser, default_argument,
18435 none_type,
18436 /*is_template=*/is_template,
18437 /*is_namespace=*/false,
18438 /*check_dependency=*/true,
18439 /*ambiguous_decls=*/NULL,
18440 token->location);
18441 /* See if the default argument is valid. */
18442 default_argument = check_template_template_default_arg (default_argument);
18443 pop_deferring_access_checks ();
18444 return default_argument;
18447 /* Parse a template-parameter.
18449 template-parameter:
18450 type-parameter
18451 parameter-declaration
18453 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
18454 the parameter. The TREE_PURPOSE is the default value, if any.
18455 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
18456 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
18457 set to true iff this parameter is a parameter pack. */
18459 static tree
18460 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
18461 bool *is_parameter_pack)
18463 cp_token *token;
18464 cp_parameter_declarator *parameter_declarator;
18465 tree parm;
18467 /* Assume it is a type parameter or a template parameter. */
18468 *is_non_type = false;
18469 /* Assume it not a parameter pack. */
18470 *is_parameter_pack = false;
18471 /* Peek at the next token. */
18472 token = cp_lexer_peek_token (parser->lexer);
18473 /* If it is `template', we have a type-parameter. */
18474 if (token->keyword == RID_TEMPLATE)
18475 return cp_parser_type_parameter (parser, is_parameter_pack);
18476 /* If it is `class' or `typename' we do not know yet whether it is a
18477 type parameter or a non-type parameter. Consider:
18479 template <typename T, typename T::X X> ...
18483 template <class C, class D*> ...
18485 Here, the first parameter is a type parameter, and the second is
18486 a non-type parameter. We can tell by looking at the token after
18487 the identifier -- if it is a `,', `=', or `>' then we have a type
18488 parameter. */
18489 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
18491 /* Peek at the token after `class' or `typename'. */
18492 token = cp_lexer_peek_nth_token (parser->lexer, 2);
18493 /* If it's an ellipsis, we have a template type parameter
18494 pack. */
18495 if (token->type == CPP_ELLIPSIS)
18496 return cp_parser_type_parameter (parser, is_parameter_pack);
18497 /* If it's an identifier, skip it. */
18498 if (token->type == CPP_NAME)
18499 token = cp_lexer_peek_nth_token (parser->lexer, 3);
18500 /* Now, see if the token looks like the end of a template
18501 parameter. */
18502 if (token->type == CPP_COMMA
18503 || token->type == CPP_EQ
18504 || token->type == CPP_GREATER)
18505 return cp_parser_type_parameter (parser, is_parameter_pack);
18508 /* Otherwise, it is a non-type parameter or a constrained parameter.
18510 [temp.param]
18512 When parsing a default template-argument for a non-type
18513 template-parameter, the first non-nested `>' is taken as the end
18514 of the template parameter-list rather than a greater-than
18515 operator. */
18516 parameter_declarator
18517 = cp_parser_parameter_declaration (parser,
18518 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
18519 /*template_parm_p=*/true,
18520 /*parenthesized_p=*/NULL);
18522 if (!parameter_declarator)
18523 return error_mark_node;
18525 /* If the parameter declaration is marked as a parameter pack, set
18526 *IS_PARAMETER_PACK to notify the caller. */
18527 if (parameter_declarator->template_parameter_pack_p)
18528 *is_parameter_pack = true;
18530 if (parameter_declarator->default_argument)
18532 /* Can happen in some cases of erroneous input (c++/34892). */
18533 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18534 /* Consume the `...' for better error recovery. */
18535 cp_lexer_consume_token (parser->lexer);
18538 /* The parameter may have been constrained type parameter. */
18539 if (is_constrained_parameter (parameter_declarator))
18540 return finish_constrained_parameter (parser,
18541 parameter_declarator,
18542 is_non_type);
18544 // Now we're sure that the parameter is a non-type parameter.
18545 *is_non_type = true;
18547 parm = grokdeclarator (parameter_declarator->declarator,
18548 &parameter_declarator->decl_specifiers,
18549 TPARM, /*initialized=*/0,
18550 /*attrlist=*/NULL);
18551 if (parm == error_mark_node)
18552 return error_mark_node;
18554 return build_tree_list (parameter_declarator->default_argument, parm);
18557 /* Parse a type-parameter.
18559 type-parameter:
18560 class identifier [opt]
18561 class identifier [opt] = type-id
18562 typename identifier [opt]
18563 typename identifier [opt] = type-id
18564 template < template-parameter-list > class identifier [opt]
18565 template < template-parameter-list > class identifier [opt]
18566 = id-expression
18568 GNU Extension (variadic templates):
18570 type-parameter:
18571 class ... identifier [opt]
18572 typename ... identifier [opt]
18574 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
18575 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
18576 the declaration of the parameter.
18578 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
18580 static tree
18581 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
18583 cp_token *token;
18584 tree parameter;
18586 /* Look for a keyword to tell us what kind of parameter this is. */
18587 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
18588 if (!token)
18589 return error_mark_node;
18591 switch (token->keyword)
18593 case RID_CLASS:
18594 case RID_TYPENAME:
18596 tree identifier;
18597 tree default_argument;
18599 /* If the next token is an ellipsis, we have a template
18600 argument pack. */
18601 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18603 /* Consume the `...' token. */
18604 cp_lexer_consume_token (parser->lexer);
18605 maybe_warn_variadic_templates ();
18607 *is_parameter_pack = true;
18610 /* If the next token is an identifier, then it names the
18611 parameter. */
18612 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18613 identifier = cp_parser_identifier (parser);
18614 else
18615 identifier = NULL_TREE;
18617 /* Create the parameter. */
18618 parameter = finish_template_type_parm (class_type_node, identifier);
18620 /* If the next token is an `=', we have a default argument. */
18621 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18623 default_argument
18624 = cp_parser_default_type_template_argument (parser);
18626 /* Template parameter packs cannot have default
18627 arguments. */
18628 if (*is_parameter_pack)
18630 if (identifier)
18631 error_at (token->location,
18632 "template parameter pack %qD cannot have a "
18633 "default argument", identifier);
18634 else
18635 error_at (token->location,
18636 "template parameter packs cannot have "
18637 "default arguments");
18638 default_argument = NULL_TREE;
18640 else if (check_for_bare_parameter_packs (default_argument))
18641 default_argument = error_mark_node;
18643 else
18644 default_argument = NULL_TREE;
18646 /* Create the combined representation of the parameter and the
18647 default argument. */
18648 parameter = build_tree_list (default_argument, parameter);
18650 break;
18652 case RID_TEMPLATE:
18654 tree identifier;
18655 tree default_argument;
18657 /* Look for the `<'. */
18658 cp_parser_require (parser, CPP_LESS, RT_LESS);
18659 /* Parse the template-parameter-list. */
18660 cp_parser_template_parameter_list (parser);
18661 /* Look for the `>'. */
18662 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
18664 /* If template requirements are present, parse them. */
18665 if (flag_concepts)
18667 tree reqs = get_shorthand_constraints (current_template_parms);
18668 if (tree dreqs = cp_parser_requires_clause_opt (parser, false))
18669 reqs = combine_constraint_expressions (reqs, dreqs);
18670 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
18673 /* Look for the `class' or 'typename' keywords. */
18674 cp_parser_type_parameter_key (parser);
18675 /* If the next token is an ellipsis, we have a template
18676 argument pack. */
18677 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18679 /* Consume the `...' token. */
18680 cp_lexer_consume_token (parser->lexer);
18681 maybe_warn_variadic_templates ();
18683 *is_parameter_pack = true;
18685 /* If the next token is an `=', then there is a
18686 default-argument. If the next token is a `>', we are at
18687 the end of the parameter-list. If the next token is a `,',
18688 then we are at the end of this parameter. */
18689 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
18690 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
18691 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18693 identifier = cp_parser_identifier (parser);
18694 /* Treat invalid names as if the parameter were nameless. */
18695 if (identifier == error_mark_node)
18696 identifier = NULL_TREE;
18698 else
18699 identifier = NULL_TREE;
18701 /* Create the template parameter. */
18702 parameter = finish_template_template_parm (class_type_node,
18703 identifier);
18705 /* If the next token is an `=', then there is a
18706 default-argument. */
18707 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18709 default_argument
18710 = cp_parser_default_template_template_argument (parser);
18712 /* Template parameter packs cannot have default
18713 arguments. */
18714 if (*is_parameter_pack)
18716 if (identifier)
18717 error_at (token->location,
18718 "template parameter pack %qD cannot "
18719 "have a default argument",
18720 identifier);
18721 else
18722 error_at (token->location, "template parameter packs cannot "
18723 "have default arguments");
18724 default_argument = NULL_TREE;
18727 else
18728 default_argument = NULL_TREE;
18730 /* Create the combined representation of the parameter and the
18731 default argument. */
18732 parameter = build_tree_list (default_argument, parameter);
18734 break;
18736 default:
18737 gcc_unreachable ();
18738 break;
18741 return parameter;
18744 /* Parse a template-id.
18746 template-id:
18747 template-name < template-argument-list [opt] >
18749 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
18750 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
18751 returned. Otherwise, if the template-name names a function, or set
18752 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
18753 names a class, returns a TYPE_DECL for the specialization.
18755 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
18756 uninstantiated templates. */
18758 static tree
18759 cp_parser_template_id (cp_parser *parser,
18760 bool template_keyword_p,
18761 bool check_dependency_p,
18762 enum tag_types tag_type,
18763 bool is_declaration)
18765 tree templ;
18766 tree arguments;
18767 tree template_id;
18768 cp_token_position start_of_id = 0;
18769 cp_token *next_token = NULL, *next_token_2 = NULL;
18770 bool is_identifier;
18772 /* If the next token corresponds to a template-id, there is no need
18773 to reparse it. */
18774 cp_token *token = cp_lexer_peek_token (parser->lexer);
18776 if (token->type == CPP_TEMPLATE_ID)
18778 cp_lexer_consume_token (parser->lexer);
18779 return saved_checks_value (token->u.tree_check_value);
18782 /* Avoid performing name lookup if there is no possibility of
18783 finding a template-id. */
18784 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
18785 || (token->type == CPP_NAME
18786 && !cp_parser_nth_token_starts_template_argument_list_p
18787 (parser, 2)))
18789 cp_parser_error (parser, "expected template-id");
18790 return error_mark_node;
18793 /* Remember where the template-id starts. */
18794 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
18795 start_of_id = cp_lexer_token_position (parser->lexer, false);
18797 push_deferring_access_checks (dk_deferred);
18799 /* Parse the template-name. */
18800 is_identifier = false;
18801 templ = cp_parser_template_name (parser, template_keyword_p,
18802 check_dependency_p,
18803 is_declaration,
18804 tag_type,
18805 &is_identifier);
18807 /* Push any access checks inside the firewall we're about to create. */
18808 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
18809 pop_deferring_access_checks ();
18810 if (templ == error_mark_node || is_identifier)
18811 return templ;
18813 /* Since we're going to preserve any side-effects from this parse, set up a
18814 firewall to protect our callers from cp_parser_commit_to_tentative_parse
18815 in the template arguments. */
18816 tentative_firewall firewall (parser);
18817 reopen_deferring_access_checks (checks);
18819 /* If we find the sequence `[:' after a template-name, it's probably
18820 a digraph-typo for `< ::'. Substitute the tokens and check if we can
18821 parse correctly the argument list. */
18822 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
18823 == CPP_OPEN_SQUARE)
18824 && next_token->flags & DIGRAPH
18825 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
18826 == CPP_COLON)
18827 && !(next_token_2->flags & PREV_WHITE))
18829 cp_parser_parse_tentatively (parser);
18830 /* Change `:' into `::'. */
18831 next_token_2->type = CPP_SCOPE;
18832 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
18833 CPP_LESS. */
18834 cp_lexer_consume_token (parser->lexer);
18836 /* Parse the arguments. */
18837 arguments = cp_parser_enclosed_template_argument_list (parser);
18838 if (!cp_parser_parse_definitely (parser))
18840 /* If we couldn't parse an argument list, then we revert our changes
18841 and return simply an error. Maybe this is not a template-id
18842 after all. */
18843 next_token_2->type = CPP_COLON;
18844 cp_parser_error (parser, "expected %<<%>");
18845 pop_deferring_access_checks ();
18846 return error_mark_node;
18848 /* Otherwise, emit an error about the invalid digraph, but continue
18849 parsing because we got our argument list. */
18850 if (permerror (next_token->location,
18851 "%<<::%> cannot begin a template-argument list"))
18853 static bool hint = false;
18854 inform (next_token->location,
18855 "%<<:%> is an alternate spelling for %<[%>."
18856 " Insert whitespace between %<<%> and %<::%>");
18857 if (!hint && !flag_permissive)
18859 inform (next_token->location, "(if you use %<-fpermissive%> "
18860 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
18861 "accept your code)");
18862 hint = true;
18866 else
18868 /* Look for the `<' that starts the template-argument-list. */
18869 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
18871 pop_deferring_access_checks ();
18872 return error_mark_node;
18874 /* Parse the arguments. */
18875 arguments = cp_parser_enclosed_template_argument_list (parser);
18877 if ((cxx_dialect > cxx17)
18878 && (TREE_CODE (templ) == FUNCTION_DECL || identifier_p (templ))
18879 && !template_keyword_p
18880 && (cp_parser_error_occurred (parser)
18881 || cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)))
18883 /* This didn't go well. */
18884 if (TREE_CODE (templ) == FUNCTION_DECL)
18886 /* C++20 says that "function-name < a;" is now ill-formed. */
18887 if (cp_parser_error_occurred (parser))
18889 error_at (token->location, "invalid template-argument-list");
18890 inform (token->location, "function name as the left hand "
18891 "operand of %<<%> is ill-formed in C++20; wrap the "
18892 "function name in %<()%>");
18894 else
18895 /* We expect "f<targs>" to be followed by "(args)". */
18896 error_at (cp_lexer_peek_token (parser->lexer)->location,
18897 "expected %<(%> after template-argument-list");
18898 if (start_of_id)
18899 /* Purge all subsequent tokens. */
18900 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
18902 else
18903 cp_parser_simulate_error (parser);
18904 pop_deferring_access_checks ();
18905 return error_mark_node;
18909 /* Set the location to be of the form:
18910 template-name < template-argument-list [opt] >
18911 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18912 with caret == start at the start of the template-name,
18913 ranging until the closing '>'. */
18914 location_t combined_loc
18915 = make_location (token->location, token->location, parser->lexer);
18917 /* Check for concepts autos where they don't belong. We could
18918 identify types in some cases of identifier TEMPL, looking ahead
18919 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
18920 types. We reject them in functions, but if what we have is an
18921 identifier, even with none_type we can't conclude it's NOT a
18922 type, we have to wait for template substitution. */
18923 if (flag_concepts && check_auto_in_tmpl_args (templ, arguments))
18924 template_id = error_mark_node;
18925 /* Build a representation of the specialization. */
18926 else if (identifier_p (templ))
18927 template_id = build_min_nt_loc (combined_loc,
18928 TEMPLATE_ID_EXPR,
18929 templ, arguments);
18930 else if (DECL_TYPE_TEMPLATE_P (templ)
18931 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
18933 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
18934 template (rather than some instantiation thereof) only if
18935 is not nested within some other construct. For example, in
18936 "template <typename T> void f(T) { A<T>::", A<T> is just an
18937 instantiation of A. */
18938 bool entering_scope
18939 = (template_parm_scope_p ()
18940 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE));
18941 template_id
18942 = finish_template_type (templ, arguments, entering_scope);
18944 else if (concept_definition_p (templ))
18946 /* The caller will decide whether this is a concept check or type
18947 constraint. */
18948 template_id = build2_loc (combined_loc, TEMPLATE_ID_EXPR,
18949 boolean_type_node, templ, arguments);
18951 else if (variable_template_p (templ))
18953 template_id = lookup_template_variable (templ, arguments, tf_warning_or_error);
18954 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
18955 SET_EXPR_LOCATION (template_id, combined_loc);
18957 else if (TREE_CODE (templ) == TYPE_DECL
18958 && TREE_CODE (TREE_TYPE (templ)) == TYPENAME_TYPE)
18960 /* Some type template in dependent scope. */
18961 tree &name = TYPENAME_TYPE_FULLNAME (TREE_TYPE (templ));
18962 name = build_min_nt_loc (combined_loc,
18963 TEMPLATE_ID_EXPR,
18964 name, arguments);
18965 template_id = templ;
18967 else
18969 /* If it's not a class-template or a template-template, it should be
18970 a function-template. */
18971 gcc_assert (OVL_P (templ) || BASELINK_P (templ));
18973 template_id = lookup_template_function (templ, arguments);
18974 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
18975 SET_EXPR_LOCATION (template_id, combined_loc);
18978 /* If parsing tentatively, replace the sequence of tokens that makes
18979 up the template-id with a CPP_TEMPLATE_ID token. That way,
18980 should we re-parse the token stream, we will not have to repeat
18981 the effort required to do the parse, nor will we issue duplicate
18982 error messages about problems during instantiation of the
18983 template. */
18984 if (start_of_id
18985 /* Don't do this if we had a parse error in a declarator; re-parsing
18986 might succeed if a name changes meaning (60361). */
18987 && !(cp_parser_error_occurred (parser)
18988 && cp_parser_parsing_tentatively (parser)
18989 && parser->in_declarator_p))
18991 /* Reset the contents of the START_OF_ID token. */
18992 token->type = CPP_TEMPLATE_ID;
18993 token->location = combined_loc;
18995 /* Retrieve any deferred checks. Do not pop this access checks yet
18996 so the memory will not be reclaimed during token replacing below. */
18997 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
18998 token->tree_check_p = true;
18999 token->u.tree_check_value->value = template_id;
19000 token->u.tree_check_value->checks = get_deferred_access_checks ();
19001 token->keyword = RID_MAX;
19003 /* Purge all subsequent tokens. */
19004 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
19006 /* ??? Can we actually assume that, if template_id ==
19007 error_mark_node, we will have issued a diagnostic to the
19008 user, as opposed to simply marking the tentative parse as
19009 failed? */
19010 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
19011 error_at (token->location, "parse error in template argument list");
19014 pop_to_parent_deferring_access_checks ();
19015 return template_id;
19018 /* Like cp_parser_template_id, called in non-type context. */
19020 static tree
19021 cp_parser_template_id_expr (cp_parser *parser,
19022 bool template_keyword_p,
19023 bool check_dependency_p,
19024 bool is_declaration)
19026 tree x = cp_parser_template_id (parser, template_keyword_p, check_dependency_p,
19027 none_type, is_declaration);
19028 if (TREE_CODE (x) == TEMPLATE_ID_EXPR
19029 && concept_check_p (x))
19030 /* We didn't check the arguments in cp_parser_template_id; do that now. */
19031 return build_concept_id (x);
19032 return x;
19035 /* Parse a template-name.
19037 template-name:
19038 identifier
19040 The standard should actually say:
19042 template-name:
19043 identifier
19044 operator-function-id
19046 A defect report has been filed about this issue.
19048 A conversion-function-id cannot be a template name because they cannot
19049 be part of a template-id. In fact, looking at this code:
19051 a.operator K<int>()
19053 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
19054 It is impossible to call a templated conversion-function-id with an
19055 explicit argument list, since the only allowed template parameter is
19056 the type to which it is converting.
19058 If TEMPLATE_KEYWORD_P is true, then we have just seen the
19059 `template' keyword, in a construction like:
19061 T::template f<3>()
19063 In that case `f' is taken to be a template-name, even though there
19064 is no way of knowing for sure.
19066 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
19067 name refers to a set of overloaded functions, at least one of which
19068 is a template, or an IDENTIFIER_NODE with the name of the template,
19069 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
19070 names are looked up inside uninstantiated templates. */
19072 static tree
19073 cp_parser_template_name (cp_parser* parser,
19074 bool template_keyword_p,
19075 bool check_dependency_p,
19076 bool is_declaration,
19077 enum tag_types tag_type,
19078 bool *is_identifier)
19080 tree identifier;
19081 tree decl;
19082 cp_token *token = cp_lexer_peek_token (parser->lexer);
19084 /* If the next token is `operator', then we have either an
19085 operator-function-id or a conversion-function-id. */
19086 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
19088 /* We don't know whether we're looking at an
19089 operator-function-id or a conversion-function-id. */
19090 cp_parser_parse_tentatively (parser);
19091 /* Try an operator-function-id. */
19092 identifier = cp_parser_operator_function_id (parser);
19093 /* If that didn't work, try a conversion-function-id. */
19094 if (!cp_parser_parse_definitely (parser))
19096 cp_parser_error (parser, "expected template-name");
19097 return error_mark_node;
19100 /* Look for the identifier. */
19101 else
19102 identifier = cp_parser_identifier (parser);
19104 /* If we didn't find an identifier, we don't have a template-id. */
19105 if (identifier == error_mark_node)
19106 return error_mark_node;
19108 /* If the name immediately followed the `template' keyword, then it
19109 is a template-name. However, if the next token is not `<', then
19110 we do not treat it as a template-name, since it is not being used
19111 as part of a template-id. This enables us to handle constructs
19112 like:
19114 template <typename T> struct S { S(); };
19115 template <typename T> S<T>::S();
19117 correctly. We would treat `S' as a template -- if it were `S<T>'
19118 -- but we do not if there is no `<'. */
19120 if (processing_template_decl
19121 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
19123 /* In a declaration, in a dependent context, we pretend that the
19124 "template" keyword was present in order to improve error
19125 recovery. For example, given:
19127 template <typename T> void f(T::X<int>);
19129 we want to treat "X<int>" as a template-id. */
19130 if (is_declaration
19131 && !template_keyword_p
19132 && parser->scope && TYPE_P (parser->scope)
19133 && check_dependency_p
19134 && dependent_scope_p (parser->scope)
19135 /* Do not do this for dtors (or ctors), since they never
19136 need the template keyword before their name. */
19137 && !constructor_name_p (identifier, parser->scope))
19139 cp_token_position start = 0;
19141 /* Explain what went wrong. */
19142 error_at (token->location, "non-template %qD used as template",
19143 identifier);
19144 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
19145 parser->scope, identifier);
19146 /* If parsing tentatively, find the location of the "<" token. */
19147 if (cp_parser_simulate_error (parser))
19148 start = cp_lexer_token_position (parser->lexer, true);
19149 /* Parse the template arguments so that we can issue error
19150 messages about them. */
19151 cp_lexer_consume_token (parser->lexer);
19152 cp_parser_enclosed_template_argument_list (parser);
19153 /* Skip tokens until we find a good place from which to
19154 continue parsing. */
19155 cp_parser_skip_to_closing_parenthesis (parser,
19156 /*recovering=*/true,
19157 /*or_comma=*/true,
19158 /*consume_paren=*/false);
19159 /* If parsing tentatively, permanently remove the
19160 template argument list. That will prevent duplicate
19161 error messages from being issued about the missing
19162 "template" keyword. */
19163 if (start)
19164 cp_lexer_purge_tokens_after (parser->lexer, start);
19165 if (is_identifier)
19166 *is_identifier = true;
19167 parser->context->object_type = NULL_TREE;
19168 return identifier;
19171 /* If the "template" keyword is present, then there is generally
19172 no point in doing name-lookup, so we just return IDENTIFIER.
19173 But, if the qualifying scope is non-dependent then we can
19174 (and must) do name-lookup normally. */
19175 if (template_keyword_p)
19177 tree scope = (parser->scope ? parser->scope
19178 : parser->context->object_type);
19179 if (scope && TYPE_P (scope)
19180 && (!CLASS_TYPE_P (scope)
19181 || (check_dependency_p && dependent_scope_p (scope))))
19183 /* We're optimizing away the call to cp_parser_lookup_name, but
19184 we still need to do this. */
19185 parser->object_scope = parser->context->object_type;
19186 parser->context->object_type = NULL_TREE;
19187 return identifier;
19192 /* cp_parser_lookup_name clears OBJECT_TYPE. */
19193 tree scope = (parser->scope ? parser->scope
19194 : parser->context->object_type);
19196 /* Look up the name. */
19197 decl = cp_parser_lookup_name (parser, identifier,
19198 tag_type,
19199 /*is_template=*/1 + template_keyword_p,
19200 /*is_namespace=*/false,
19201 check_dependency_p,
19202 /*ambiguous_decls=*/NULL,
19203 token->location);
19205 decl = strip_using_decl (decl);
19207 /* 13.3 [temp.names] A < is interpreted as the delimiter of a
19208 template-argument-list if it follows a name that is not a
19209 conversion-function-id and
19210 - that follows the keyword template or a ~ after a nested-name-specifier or
19211 in a class member access expression, or
19212 - for which name lookup finds the injected-class-name of a class template
19213 or finds any declaration of a template, or
19214 - that is an unqualified name for which name lookup either finds one or
19215 more functions or finds nothing, or
19216 - that is a terminal name in a using-declarator (9.9), in a declarator-id
19217 (9.3.4), or in a type-only context other than a nested-name-specifier
19218 (13.8). */
19220 /* Handle injected-class-name. */
19221 decl = maybe_get_template_decl_from_type_decl (decl);
19223 /* If DECL is a template, then the name was a template-name. */
19224 if (TREE_CODE (decl) == TEMPLATE_DECL)
19226 if ((TREE_DEPRECATED (decl) || TREE_UNAVAILABLE (decl))
19227 && deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
19229 tree d = DECL_TEMPLATE_RESULT (decl);
19230 tree attr;
19231 if (TREE_CODE (d) == TYPE_DECL)
19232 attr = TYPE_ATTRIBUTES (TREE_TYPE (d));
19233 else
19234 attr = DECL_ATTRIBUTES (d);
19235 if (TREE_UNAVAILABLE (decl))
19237 attr = lookup_attribute ("unavailable", attr);
19238 error_unavailable_use (decl, attr);
19240 else if (TREE_DEPRECATED (decl)
19241 && deprecated_state != DEPRECATED_SUPPRESS)
19243 attr = lookup_attribute ("deprecated", attr);
19244 warn_deprecated_use (decl, attr);
19248 else
19250 /* Look through an overload set for any templates. */
19251 bool found = false;
19253 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
19254 !found && iter; ++iter)
19255 if (TREE_CODE (*iter) == TEMPLATE_DECL)
19256 found = true;
19258 /* "an unqualified name for which name lookup either finds one or more
19259 functions or finds nothing". */
19260 if (!found
19261 && (cxx_dialect > cxx17)
19262 && !scope
19263 && cp_lexer_next_token_is (parser->lexer, CPP_LESS)
19264 && tag_type == none_type)
19266 /* The "more functions" case. Just use the OVERLOAD as normally.
19267 We don't use is_overloaded_fn here to avoid considering
19268 BASELINKs. */
19269 if (TREE_CODE (decl) == OVERLOAD
19270 /* Name lookup found one function. */
19271 || TREE_CODE (decl) == FUNCTION_DECL
19272 /* Name lookup found nothing. */
19273 || decl == error_mark_node)
19274 found = true;
19277 /* "that follows the keyword template"..."in a type-only context" */
19278 if (!found && scope
19279 && (template_keyword_p || tag_type != none_type)
19280 && dependentish_scope_p (scope)
19281 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
19282 found = true;
19284 if (!found)
19286 /* The name does not name a template. */
19287 cp_parser_error (parser, "expected template-name");
19288 return error_mark_node;
19290 else if ((!DECL_P (decl) && !is_overloaded_fn (decl))
19291 || TREE_CODE (decl) == USING_DECL
19292 /* cp_parser_template_id can only handle some TYPE_DECLs. */
19293 || (TREE_CODE (decl) == TYPE_DECL
19294 && TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE))
19295 /* Repeat the lookup at instantiation time. */
19296 decl = identifier;
19299 return decl;
19302 /* Parse a template-argument-list.
19304 template-argument-list:
19305 template-argument ... [opt]
19306 template-argument-list , template-argument ... [opt]
19308 Returns a TREE_VEC containing the arguments. */
19310 static tree
19311 cp_parser_template_argument_list (cp_parser* parser)
19313 bool saved_in_template_argument_list_p;
19314 bool saved_ice_p;
19315 bool saved_non_ice_p;
19317 /* Don't create location wrapper nodes within a template-argument-list. */
19318 auto_suppress_location_wrappers sentinel;
19320 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
19321 parser->in_template_argument_list_p = true;
19322 /* Even if the template-id appears in an integral
19323 constant-expression, the contents of the argument list do
19324 not. */
19325 saved_ice_p = parser->integral_constant_expression_p;
19326 parser->integral_constant_expression_p = false;
19327 saved_non_ice_p = parser->non_integral_constant_expression_p;
19328 parser->non_integral_constant_expression_p = false;
19330 /* Parse the arguments. */
19331 auto_vec<tree, 10> args;
19334 if (!args.is_empty ())
19335 /* Consume the comma. */
19336 cp_lexer_consume_token (parser->lexer);
19338 /* Parse the template-argument. */
19339 tree argument = cp_parser_template_argument (parser);
19341 /* If the next token is an ellipsis, we're expanding a template
19342 argument pack. */
19343 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19345 if (argument == error_mark_node)
19347 cp_token *token = cp_lexer_peek_token (parser->lexer);
19348 error_at (token->location,
19349 "expected parameter pack before %<...%>");
19351 /* Consume the `...' token. */
19352 cp_lexer_consume_token (parser->lexer);
19354 /* Make the argument into a TYPE_PACK_EXPANSION or
19355 EXPR_PACK_EXPANSION. */
19356 argument = make_pack_expansion (argument);
19359 args.safe_push (argument);
19361 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
19363 int n_args = args.length ();
19364 tree vec = make_tree_vec (n_args);
19366 for (int i = 0; i < n_args; i++)
19367 TREE_VEC_ELT (vec, i) = args[i];
19369 parser->non_integral_constant_expression_p = saved_non_ice_p;
19370 parser->integral_constant_expression_p = saved_ice_p;
19371 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
19372 if (CHECKING_P)
19373 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
19374 return vec;
19377 /* Parse a template-argument.
19379 template-argument:
19380 assignment-expression
19381 type-id
19382 id-expression
19384 The representation is that of an assignment-expression, type-id, or
19385 id-expression -- except that the qualified id-expression is
19386 evaluated, so that the value returned is either a DECL or an
19387 OVERLOAD.
19389 Although the standard says "assignment-expression", it forbids
19390 throw-expressions or assignments in the template argument.
19391 Therefore, we use "conditional-expression" instead. */
19393 static tree
19394 cp_parser_template_argument (cp_parser* parser)
19396 tree argument;
19397 bool template_p;
19398 bool address_p;
19399 bool maybe_type_id = false;
19400 cp_token *token = NULL, *argument_start_token = NULL;
19401 location_t loc = 0;
19402 cp_id_kind idk;
19404 /* There's really no way to know what we're looking at, so we just
19405 try each alternative in order.
19407 [temp.arg]
19409 In a template-argument, an ambiguity between a type-id and an
19410 expression is resolved to a type-id, regardless of the form of
19411 the corresponding template-parameter.
19413 Therefore, we try a type-id first. */
19414 cp_parser_parse_tentatively (parser);
19415 argument = cp_parser_template_type_arg (parser);
19416 /* If there was no error parsing the type-id but the next token is a
19417 '>>', our behavior depends on which dialect of C++ we're
19418 parsing. In C++98, we probably found a typo for '> >'. But there
19419 are type-id which are also valid expressions. For instance:
19421 struct X { int operator >> (int); };
19422 template <int V> struct Foo {};
19423 Foo<X () >> 5> r;
19425 Here 'X()' is a valid type-id of a function type, but the user just
19426 wanted to write the expression "X() >> 5". Thus, we remember that we
19427 found a valid type-id, but we still try to parse the argument as an
19428 expression to see what happens.
19430 In C++0x, the '>>' will be considered two separate '>'
19431 tokens. */
19432 if (!cp_parser_error_occurred (parser)
19433 && ((cxx_dialect == cxx98
19434 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19435 /* Similarly for >= which
19436 cp_parser_next_token_ends_template_argument_p treats for
19437 diagnostics purposes as mistyped > =, but can be valid
19438 after a type-id. */
19439 || cp_lexer_next_token_is (parser->lexer, CPP_GREATER_EQ)))
19441 maybe_type_id = true;
19442 cp_parser_abort_tentative_parse (parser);
19444 else
19446 /* If the next token isn't a `,' or a `>', then this argument wasn't
19447 really finished. This means that the argument is not a valid
19448 type-id. */
19449 if (!cp_parser_next_token_ends_template_argument_p (parser))
19450 cp_parser_error (parser, "expected template-argument");
19451 /* If that worked, we're done. */
19452 if (cp_parser_parse_definitely (parser))
19453 return argument;
19455 /* We're still not sure what the argument will be. */
19456 cp_parser_parse_tentatively (parser);
19457 /* Try a template. */
19458 argument_start_token = cp_lexer_peek_token (parser->lexer);
19459 argument = cp_parser_id_expression (parser,
19460 /*template_keyword_p=*/false,
19461 /*check_dependency_p=*/true,
19462 &template_p,
19463 /*declarator_p=*/false,
19464 /*optional_p=*/false);
19465 /* If the next token isn't a `,' or a `>', then this argument wasn't
19466 really finished. */
19467 if (!cp_parser_next_token_ends_template_argument_p (parser))
19468 cp_parser_error (parser, "expected template-argument");
19469 if (!cp_parser_error_occurred (parser))
19471 /* Figure out what is being referred to. If the id-expression
19472 was for a class template specialization, then we will have a
19473 TYPE_DECL at this point. There is no need to do name lookup
19474 at this point in that case. */
19475 if (TREE_CODE (argument) != TYPE_DECL)
19476 argument = cp_parser_lookup_name (parser, argument,
19477 none_type,
19478 /*is_template=*/template_p,
19479 /*is_namespace=*/false,
19480 /*check_dependency=*/true,
19481 /*ambiguous_decls=*/NULL,
19482 argument_start_token->location);
19483 if (TREE_CODE (argument) != TEMPLATE_DECL
19484 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
19485 cp_parser_error (parser, "expected template-name");
19487 if (cp_parser_parse_definitely (parser))
19489 if (TREE_UNAVAILABLE (argument))
19490 error_unavailable_use (argument, NULL_TREE);
19491 else if (TREE_DEPRECATED (argument))
19492 warn_deprecated_use (argument, NULL_TREE);
19493 return argument;
19495 /* It must be a non-type argument. In C++17 any constant-expression is
19496 allowed. */
19497 if (cxx_dialect > cxx14)
19498 goto general_expr;
19500 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
19502 -- an integral constant-expression of integral or enumeration
19503 type; or
19505 -- the name of a non-type template-parameter; or
19507 -- the name of an object or function with external linkage...
19509 -- the address of an object or function with external linkage...
19511 -- a pointer to member... */
19512 /* Look for a non-type template parameter. */
19513 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19515 cp_parser_parse_tentatively (parser);
19516 argument = cp_parser_primary_expression (parser,
19517 /*address_p=*/false,
19518 /*cast_p=*/false,
19519 /*template_arg_p=*/true,
19520 &idk);
19521 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
19522 || !cp_parser_next_token_ends_template_argument_p (parser))
19523 cp_parser_simulate_error (parser);
19524 if (cp_parser_parse_definitely (parser))
19525 return argument;
19528 /* If the next token is "&", the argument must be the address of an
19529 object or function with external linkage. */
19530 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
19531 if (address_p)
19533 loc = cp_lexer_peek_token (parser->lexer)->location;
19534 cp_lexer_consume_token (parser->lexer);
19536 /* See if we might have an id-expression. */
19537 token = cp_lexer_peek_token (parser->lexer);
19538 if (token->type == CPP_NAME
19539 || token->keyword == RID_OPERATOR
19540 || token->type == CPP_SCOPE
19541 || token->type == CPP_TEMPLATE_ID
19542 || token->type == CPP_NESTED_NAME_SPECIFIER)
19544 cp_parser_parse_tentatively (parser);
19545 argument = cp_parser_primary_expression (parser,
19546 address_p,
19547 /*cast_p=*/false,
19548 /*template_arg_p=*/true,
19549 &idk);
19550 if (cp_parser_error_occurred (parser)
19551 || !cp_parser_next_token_ends_template_argument_p (parser))
19552 cp_parser_abort_tentative_parse (parser);
19553 else
19555 tree probe;
19557 if (INDIRECT_REF_P (argument))
19559 /* Strip the dereference temporarily. */
19560 gcc_assert (REFERENCE_REF_P (argument));
19561 argument = TREE_OPERAND (argument, 0);
19564 /* If we're in a template, we represent a qualified-id referring
19565 to a static data member as a SCOPE_REF even if the scope isn't
19566 dependent so that we can check access control later. */
19567 probe = argument;
19568 if (TREE_CODE (probe) == SCOPE_REF)
19569 probe = TREE_OPERAND (probe, 1);
19570 if (VAR_P (probe))
19572 /* A variable without external linkage might still be a
19573 valid constant-expression, so no error is issued here
19574 if the external-linkage check fails. */
19575 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
19576 cp_parser_simulate_error (parser);
19578 else if (is_overloaded_fn (argument))
19579 /* All overloaded functions are allowed; if the external
19580 linkage test does not pass, an error will be issued
19581 later. */
19583 else if (address_p
19584 && (TREE_CODE (argument) == OFFSET_REF
19585 || TREE_CODE (argument) == SCOPE_REF))
19586 /* A pointer-to-member. */
19588 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
19590 else
19591 cp_parser_simulate_error (parser);
19593 if (cp_parser_parse_definitely (parser))
19595 if (address_p)
19596 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
19597 NULL_TREE, tf_warning_or_error);
19598 else
19599 argument = convert_from_reference (argument);
19600 return argument;
19604 /* If the argument started with "&", there are no other valid
19605 alternatives at this point. */
19606 if (address_p)
19608 cp_parser_error (parser, "invalid non-type template argument");
19609 return error_mark_node;
19612 general_expr:
19613 /* If the argument wasn't successfully parsed as a type-id followed
19614 by '>>', the argument can only be a constant expression now.
19615 Otherwise, we try parsing the constant-expression tentatively,
19616 because the argument could really be a type-id. */
19617 if (maybe_type_id)
19618 cp_parser_parse_tentatively (parser);
19620 if (cxx_dialect <= cxx14)
19621 argument = cp_parser_constant_expression (parser);
19622 else
19624 /* In C++20, we can encounter a braced-init-list. */
19625 if (cxx_dialect >= cxx20
19626 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19627 return cp_parser_braced_list (parser);
19629 /* With C++17 generalized non-type template arguments we need to handle
19630 lvalue constant expressions, too. */
19631 argument = cp_parser_assignment_expression (parser);
19632 require_potential_constant_expression (argument);
19635 if (!maybe_type_id)
19636 return argument;
19637 if (!cp_parser_next_token_ends_template_argument_p (parser))
19638 cp_parser_error (parser, "expected template-argument");
19639 if (cp_parser_parse_definitely (parser))
19640 return argument;
19641 /* We did our best to parse the argument as a non type-id, but that
19642 was the only alternative that matched (albeit with a '>' after
19643 it). We can assume it's just a typo from the user, and a
19644 diagnostic will then be issued. */
19645 return cp_parser_template_type_arg (parser);
19648 /* Parse an explicit-instantiation.
19650 explicit-instantiation:
19651 template declaration
19653 Although the standard says `declaration', what it really means is:
19655 explicit-instantiation:
19656 template decl-specifier-seq [opt] declarator [opt] ;
19658 Things like `template int S<int>::i = 5, int S<double>::j;' are not
19659 supposed to be allowed. A defect report has been filed about this
19660 issue.
19662 GNU Extension:
19664 explicit-instantiation:
19665 storage-class-specifier template
19666 decl-specifier-seq [opt] declarator [opt] ;
19667 function-specifier template
19668 decl-specifier-seq [opt] declarator [opt] ; */
19670 static void
19671 cp_parser_explicit_instantiation (cp_parser* parser)
19673 int declares_class_or_enum;
19674 cp_decl_specifier_seq decl_specifiers;
19675 tree extension_specifier = NULL_TREE;
19677 auto_timevar tv (TV_TEMPLATE_INST);
19679 /* Look for an (optional) storage-class-specifier or
19680 function-specifier. */
19681 if (cp_parser_allow_gnu_extensions_p (parser))
19683 extension_specifier
19684 = cp_parser_storage_class_specifier_opt (parser);
19685 if (!extension_specifier)
19686 extension_specifier
19687 = cp_parser_function_specifier_opt (parser,
19688 /*decl_specs=*/NULL);
19691 /* Look for the `template' keyword. */
19692 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
19693 /* Let the front end know that we are processing an explicit
19694 instantiation. */
19695 begin_explicit_instantiation ();
19696 /* [temp.explicit] says that we are supposed to ignore access
19697 control while processing explicit instantiation directives. */
19698 push_deferring_access_checks (dk_no_check);
19699 /* Parse a decl-specifier-seq. */
19700 cp_parser_decl_specifier_seq (parser,
19701 CP_PARSER_FLAGS_OPTIONAL,
19702 &decl_specifiers,
19703 &declares_class_or_enum);
19705 cp_omp_declare_simd_data odsd;
19706 if (decl_specifiers.attributes && (flag_openmp || flag_openmp_simd))
19707 cp_parser_handle_directive_omp_attributes (parser,
19708 &decl_specifiers.attributes,
19709 &odsd, true);
19711 /* If there was exactly one decl-specifier, and it declared a class,
19712 and there's no declarator, then we have an explicit type
19713 instantiation. */
19714 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
19716 tree type = check_tag_decl (&decl_specifiers,
19717 /*explicit_type_instantiation_p=*/true);
19718 /* Turn access control back on for names used during
19719 template instantiation. */
19720 pop_deferring_access_checks ();
19721 if (type)
19722 do_type_instantiation (type, extension_specifier,
19723 /*complain=*/tf_error);
19725 else
19727 cp_declarator *declarator;
19728 tree decl;
19730 /* Parse the declarator. */
19731 declarator
19732 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19733 CP_PARSER_FLAGS_NONE,
19734 /*ctor_dtor_or_conv_p=*/NULL,
19735 /*parenthesized_p=*/NULL,
19736 /*member_p=*/false,
19737 /*friend_p=*/false,
19738 /*static_p=*/false);
19739 if (declares_class_or_enum & 2)
19740 cp_parser_check_for_definition_in_return_type (declarator,
19741 decl_specifiers.type,
19742 decl_specifiers.locations[ds_type_spec]);
19743 if (declarator != cp_error_declarator)
19745 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
19746 permerror (decl_specifiers.locations[ds_inline],
19747 "explicit instantiation shall not use"
19748 " %<inline%> specifier");
19749 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
19750 permerror (decl_specifiers.locations[ds_constexpr],
19751 "explicit instantiation shall not use"
19752 " %<constexpr%> specifier");
19753 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_consteval))
19754 permerror (decl_specifiers.locations[ds_consteval],
19755 "explicit instantiation shall not use"
19756 " %<consteval%> specifier");
19758 decl = grokdeclarator (declarator, &decl_specifiers,
19759 NORMAL, 0, &decl_specifiers.attributes);
19760 /* Turn access control back on for names used during
19761 template instantiation. */
19762 pop_deferring_access_checks ();
19763 /* Do the explicit instantiation. */
19764 do_decl_instantiation (decl, extension_specifier);
19766 else
19768 pop_deferring_access_checks ();
19769 /* Skip the body of the explicit instantiation. */
19770 cp_parser_skip_to_end_of_statement (parser);
19773 /* We're done with the instantiation. */
19774 end_explicit_instantiation ();
19776 cp_parser_consume_semicolon_at_end_of_statement (parser);
19778 cp_finalize_omp_declare_simd (parser, &odsd);
19781 /* Parse an explicit-specialization.
19783 explicit-specialization:
19784 template < > declaration
19786 Although the standard says `declaration', what it really means is:
19788 explicit-specialization:
19789 template <> decl-specifier [opt] init-declarator [opt] ;
19790 template <> function-definition
19791 template <> explicit-specialization
19792 template <> template-declaration */
19794 static void
19795 cp_parser_explicit_specialization (cp_parser* parser)
19797 cp_token *token = cp_lexer_peek_token (parser->lexer);
19799 /* Look for the `template' keyword. */
19800 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
19801 /* Look for the `<'. */
19802 cp_parser_require (parser, CPP_LESS, RT_LESS);
19803 /* Look for the `>'. */
19804 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
19805 /* We have processed another parameter list. */
19806 ++parser->num_template_parameter_lists;
19808 /* [temp]
19810 A template ... explicit specialization ... shall not have C
19811 linkage. */
19812 bool need_lang_pop = current_lang_name == lang_name_c;
19813 if (need_lang_pop)
19815 error_at (token->location, "template specialization with C linkage");
19816 maybe_show_extern_c_location ();
19818 /* Give it C++ linkage to avoid confusing other parts of the
19819 front end. */
19820 push_lang_context (lang_name_cplusplus);
19823 /* Let the front end know that we are beginning a specialization. */
19824 if (begin_specialization ())
19826 /* If the next keyword is `template', we need to figure out
19827 whether or not we're looking a template-declaration. */
19828 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19830 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
19831 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
19832 cp_parser_template_declaration_after_export (parser,
19833 /*member_p=*/false);
19834 else
19835 cp_parser_explicit_specialization (parser);
19837 else
19838 /* Parse the dependent declaration. */
19839 cp_parser_single_declaration (parser,
19840 /*checks=*/NULL,
19841 /*member_p=*/false,
19842 /*explicit_specialization_p=*/true,
19843 /*friend_p=*/NULL);
19846 /* We're done with the specialization. */
19847 end_specialization ();
19849 /* For the erroneous case of a template with C linkage, we pushed an
19850 implicit C++ linkage scope; exit that scope now. */
19851 if (need_lang_pop)
19852 pop_lang_context ();
19854 /* We're done with this parameter list. */
19855 --parser->num_template_parameter_lists;
19858 /* Preserve the attributes across a garbage collect (by making it a GC
19859 root), which can occur when parsing a member function. */
19861 static GTY(()) vec<tree, va_gc> *cp_parser_decl_specs_attrs;
19863 /* Parse a type-specifier.
19865 type-specifier:
19866 simple-type-specifier
19867 class-specifier
19868 enum-specifier
19869 elaborated-type-specifier
19870 cv-qualifier
19872 GNU Extension:
19874 type-specifier:
19875 __complex__
19877 Returns a representation of the type-specifier. For a
19878 class-specifier, enum-specifier, or elaborated-type-specifier, a
19879 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
19881 The parser flags FLAGS is used to control type-specifier parsing.
19883 If IS_DECLARATION is TRUE, then this type-specifier is appearing
19884 in a decl-specifier-seq.
19886 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
19887 class-specifier, enum-specifier, or elaborated-type-specifier, then
19888 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
19889 if a type is declared; 2 if it is defined. Otherwise, it is set to
19890 zero.
19892 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
19893 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
19894 is set to FALSE. */
19896 static tree
19897 cp_parser_type_specifier (cp_parser* parser,
19898 cp_parser_flags flags,
19899 cp_decl_specifier_seq *decl_specs,
19900 bool is_declaration,
19901 int* declares_class_or_enum,
19902 bool* is_cv_qualifier)
19904 tree type_spec = NULL_TREE;
19905 cp_token *token;
19906 enum rid keyword;
19907 cp_decl_spec ds = ds_last;
19909 /* Assume this type-specifier does not declare a new type. */
19910 if (declares_class_or_enum)
19911 *declares_class_or_enum = 0;
19912 /* And that it does not specify a cv-qualifier. */
19913 if (is_cv_qualifier)
19914 *is_cv_qualifier = false;
19915 /* Peek at the next token. */
19916 token = cp_lexer_peek_token (parser->lexer);
19918 /* If we're looking at a keyword, we can use that to guide the
19919 production we choose. */
19920 keyword = token->keyword;
19921 switch (keyword)
19923 case RID_ENUM:
19924 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
19925 goto elaborated_type_specifier;
19927 /* Look for the enum-specifier. */
19928 type_spec = cp_parser_enum_specifier (parser);
19929 /* If that worked, we're done. */
19930 if (type_spec)
19932 if (declares_class_or_enum)
19933 *declares_class_or_enum = 2;
19934 if (decl_specs)
19935 cp_parser_set_decl_spec_type (decl_specs,
19936 type_spec,
19937 token,
19938 /*type_definition_p=*/true);
19939 return type_spec;
19941 else
19942 goto elaborated_type_specifier;
19944 /* Any of these indicate either a class-specifier, or an
19945 elaborated-type-specifier. */
19946 case RID_CLASS:
19947 case RID_STRUCT:
19948 case RID_UNION:
19949 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
19950 goto elaborated_type_specifier;
19952 /* Parse tentatively so that we can back up if we don't find a
19953 class-specifier. */
19954 cp_parser_parse_tentatively (parser);
19955 if (decl_specs->attributes)
19956 vec_safe_push (cp_parser_decl_specs_attrs, decl_specs->attributes);
19957 /* Look for the class-specifier. */
19958 type_spec = cp_parser_class_specifier (parser);
19959 if (decl_specs->attributes)
19960 cp_parser_decl_specs_attrs->pop ();
19961 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
19962 /* If that worked, we're done. */
19963 if (cp_parser_parse_definitely (parser))
19965 if (declares_class_or_enum)
19966 *declares_class_or_enum = 2;
19967 if (decl_specs)
19968 cp_parser_set_decl_spec_type (decl_specs,
19969 type_spec,
19970 token,
19971 /*type_definition_p=*/true);
19972 return type_spec;
19975 /* Fall through. */
19976 elaborated_type_specifier:
19977 /* We're declaring (not defining) a class or enum. */
19978 if (declares_class_or_enum)
19979 *declares_class_or_enum = 1;
19981 /* Fall through. */
19982 case RID_TYPENAME:
19983 /* Look for an elaborated-type-specifier. */
19984 type_spec
19985 = (cp_parser_elaborated_type_specifier
19986 (parser,
19987 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
19988 is_declaration));
19989 if (decl_specs)
19990 cp_parser_set_decl_spec_type (decl_specs,
19991 type_spec,
19992 token,
19993 /*type_definition_p=*/false);
19994 return type_spec;
19996 case RID_CONST:
19997 ds = ds_const;
19998 if (is_cv_qualifier)
19999 *is_cv_qualifier = true;
20000 break;
20002 case RID_VOLATILE:
20003 ds = ds_volatile;
20004 if (is_cv_qualifier)
20005 *is_cv_qualifier = true;
20006 break;
20008 case RID_RESTRICT:
20009 ds = ds_restrict;
20010 if (is_cv_qualifier)
20011 *is_cv_qualifier = true;
20012 break;
20014 case RID_COMPLEX:
20015 /* The `__complex__' keyword is a GNU extension. */
20016 ds = ds_complex;
20017 break;
20019 default:
20020 break;
20023 /* Handle simple keywords. */
20024 if (ds != ds_last)
20026 if (decl_specs)
20028 set_and_check_decl_spec_loc (decl_specs, ds, token);
20029 decl_specs->any_specifiers_p = true;
20031 return cp_lexer_consume_token (parser->lexer)->u.value;
20034 /* If we do not already have a type-specifier, assume we are looking
20035 at a simple-type-specifier. */
20036 type_spec = cp_parser_simple_type_specifier (parser,
20037 decl_specs,
20038 flags);
20040 /* If we didn't find a type-specifier, and a type-specifier was not
20041 optional in this context, issue an error message. */
20042 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
20044 cp_parser_error (parser, "expected type specifier");
20045 return error_mark_node;
20048 return type_spec;
20051 /* Parse a simple-type-specifier.
20053 simple-type-specifier:
20054 :: [opt] nested-name-specifier [opt] type-name
20055 :: [opt] nested-name-specifier template template-id
20056 char
20057 wchar_t
20058 bool
20059 short
20061 long
20062 signed
20063 unsigned
20064 float
20065 double
20066 void
20068 C++11 Extension:
20070 simple-type-specifier:
20071 auto
20072 decltype ( expression )
20073 char16_t
20074 char32_t
20075 __underlying_type ( type-id )
20077 C++17 extension:
20079 nested-name-specifier(opt) template-name
20081 GNU Extension:
20083 simple-type-specifier:
20084 __int128
20085 __typeof__ unary-expression
20086 __typeof__ ( type-id )
20087 __typeof__ ( type-id ) { initializer-list , [opt] }
20089 Concepts Extension:
20091 simple-type-specifier:
20092 constrained-type-specifier
20094 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
20095 appropriately updated. */
20097 static tree
20098 cp_parser_simple_type_specifier (cp_parser* parser,
20099 cp_decl_specifier_seq *decl_specs,
20100 cp_parser_flags flags)
20102 tree type = NULL_TREE;
20103 cp_token *token;
20104 int idx;
20106 /* Peek at the next token. */
20107 token = cp_lexer_peek_token (parser->lexer);
20109 /* If we're looking at a keyword, things are easy. */
20110 switch (token->keyword)
20112 case RID_CHAR:
20113 if (decl_specs)
20114 decl_specs->explicit_char_p = true;
20115 type = char_type_node;
20116 break;
20117 case RID_CHAR8:
20118 type = char8_type_node;
20119 break;
20120 case RID_CHAR16:
20121 type = char16_type_node;
20122 break;
20123 case RID_CHAR32:
20124 type = char32_type_node;
20125 break;
20126 case RID_WCHAR:
20127 type = wchar_type_node;
20128 break;
20129 case RID_BOOL:
20130 type = boolean_type_node;
20131 break;
20132 case RID_SHORT:
20133 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
20134 type = short_integer_type_node;
20135 break;
20136 case RID_INT:
20137 if (decl_specs)
20138 decl_specs->explicit_int_p = true;
20139 type = integer_type_node;
20140 break;
20141 case RID_INT_N_0:
20142 case RID_INT_N_1:
20143 case RID_INT_N_2:
20144 case RID_INT_N_3:
20145 idx = token->keyword - RID_INT_N_0;
20146 if (! int_n_enabled_p [idx])
20147 break;
20148 if (decl_specs)
20150 decl_specs->explicit_intN_p = true;
20151 decl_specs->int_n_idx = idx;
20152 /* Check if the alternate "__intN__" form has been used instead of
20153 "__intN". */
20154 if (startswith (IDENTIFIER_POINTER (token->u.value)
20155 + (IDENTIFIER_LENGTH (token->u.value) - 2), "__"))
20156 decl_specs->int_n_alt = true;
20158 type = int_n_trees [idx].signed_type;
20159 break;
20160 case RID_LONG:
20161 if (decl_specs)
20162 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
20163 type = long_integer_type_node;
20164 break;
20165 case RID_SIGNED:
20166 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
20167 type = integer_type_node;
20168 break;
20169 case RID_UNSIGNED:
20170 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
20171 type = unsigned_type_node;
20172 break;
20173 case RID_FLOAT:
20174 type = float_type_node;
20175 break;
20176 case RID_DOUBLE:
20177 type = double_type_node;
20178 break;
20179 CASE_RID_FLOATN_NX:
20180 type = FLOATN_NX_TYPE_NODE (token->keyword - RID_FLOATN_NX_FIRST);
20181 if (type == NULL_TREE)
20182 error ("%<_Float%d%s%> is not supported on this target",
20183 floatn_nx_types[token->keyword - RID_FLOATN_NX_FIRST].n,
20184 floatn_nx_types[token->keyword - RID_FLOATN_NX_FIRST].extended
20185 ? "x" : "");
20186 break;
20187 case RID_VOID:
20188 type = void_type_node;
20189 break;
20191 case RID_AUTO:
20192 maybe_warn_cpp0x (CPP0X_AUTO);
20193 if (parser->auto_is_implicit_function_template_parm_p)
20195 /* The 'auto' might be the placeholder return type for a function decl
20196 with trailing return type. */
20197 bool have_trailing_return_fn_decl = false;
20199 cp_parser_parse_tentatively (parser);
20200 cp_lexer_consume_token (parser->lexer);
20201 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
20202 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
20203 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
20204 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
20206 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20208 cp_lexer_consume_token (parser->lexer);
20209 cp_parser_skip_to_closing_parenthesis (parser,
20210 /*recovering*/false,
20211 /*or_comma*/false,
20212 /*consume_paren*/true);
20213 continue;
20216 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
20218 have_trailing_return_fn_decl = true;
20219 break;
20222 cp_lexer_consume_token (parser->lexer);
20224 cp_parser_abort_tentative_parse (parser);
20226 if (have_trailing_return_fn_decl)
20228 type = make_auto ();
20229 break;
20232 if (cxx_dialect >= cxx14)
20234 type = synthesize_implicit_template_parm (parser, NULL_TREE);
20235 type = TREE_TYPE (type);
20237 else
20238 type = error_mark_node;
20240 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
20242 if (cxx_dialect < cxx14)
20243 error_at (token->location,
20244 "use of %<auto%> in lambda parameter declaration "
20245 "only available with "
20246 "%<-std=c++14%> or %<-std=gnu++14%>");
20248 else if (!flag_concepts_ts && parser->in_template_argument_list_p)
20249 pedwarn (token->location, 0,
20250 "use of %<auto%> in template argument "
20251 "only available with %<-fconcepts-ts%>");
20252 else if (!flag_concepts)
20253 pedwarn (token->location, 0,
20254 "use of %<auto%> in parameter declaration "
20255 "only available with %<-std=c++20%> or %<-fconcepts%>");
20256 else if (cxx_dialect < cxx14)
20257 error_at (token->location,
20258 "use of %<auto%> in parameter declaration "
20259 "only available with "
20260 "%<-std=c++14%> or %<-std=gnu++14%>");
20262 else
20263 type = make_auto ();
20264 break;
20266 case RID_DECLTYPE:
20267 /* Since DR 743, decltype can either be a simple-type-specifier by
20268 itself or begin a nested-name-specifier. Parsing it will replace
20269 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
20270 handling below decide what to do. */
20271 cp_parser_decltype (parser);
20272 cp_lexer_set_token_position (parser->lexer, token);
20273 break;
20275 case RID_TYPEOF:
20276 /* Consume the `typeof' token. */
20277 cp_lexer_consume_token (parser->lexer);
20278 /* Parse the operand to `typeof'. */
20279 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
20280 /* If it is not already a TYPE, take its type. */
20281 if (!TYPE_P (type))
20282 type = finish_typeof (type);
20284 if (decl_specs)
20285 cp_parser_set_decl_spec_type (decl_specs, type,
20286 token,
20287 /*type_definition_p=*/false);
20289 return type;
20291 default:
20292 /* If token is a type-yielding built-in traits, parse it. */
20293 const cp_trait* trait = cp_lexer_peek_trait_type (parser->lexer);
20294 if (trait)
20296 type = cp_parser_trait (parser, trait);
20297 if (decl_specs)
20298 cp_parser_set_decl_spec_type (decl_specs, type,
20299 token,
20300 /*type_definition_p=*/false);
20302 return type;
20305 break;
20308 /* If token is an already-parsed decltype not followed by ::,
20309 it's a simple-type-specifier. */
20310 if (token->type == CPP_DECLTYPE
20311 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
20313 type = saved_checks_value (token->u.tree_check_value);
20314 if (decl_specs)
20316 cp_parser_set_decl_spec_type (decl_specs, type,
20317 token,
20318 /*type_definition_p=*/false);
20319 /* Remember that we are handling a decltype in order to
20320 implement the resolution of DR 1510 when the argument
20321 isn't instantiation dependent. */
20322 decl_specs->decltype_p = true;
20324 cp_lexer_consume_token (parser->lexer);
20325 return type;
20328 /* If the type-specifier was for a built-in type, we're done. */
20329 if (type)
20331 /* Record the type. */
20332 if (decl_specs
20333 && (token->keyword != RID_SIGNED
20334 && token->keyword != RID_UNSIGNED
20335 && token->keyword != RID_SHORT
20336 && token->keyword != RID_LONG))
20337 cp_parser_set_decl_spec_type (decl_specs,
20338 type,
20339 token,
20340 /*type_definition_p=*/false);
20341 if (decl_specs)
20342 decl_specs->any_specifiers_p = true;
20344 /* Consume the token. */
20345 cp_lexer_consume_token (parser->lexer);
20347 if (type == error_mark_node)
20348 return error_mark_node;
20350 /* There is no valid C++ program where a non-template type is
20351 followed by a "<". That usually indicates that the user thought
20352 that the type was a template. */
20353 cp_parser_check_for_invalid_template_id (parser, type, none_type,
20354 token->location);
20356 return TYPE_NAME (type);
20359 /* The type-specifier must be a user-defined type. */
20360 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
20362 bool qualified_p;
20363 bool global_p;
20364 const bool typename_p = (cxx_dialect >= cxx20
20365 && (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
20367 /* Don't gobble tokens or issue error messages if this is an
20368 optional type-specifier. */
20369 if (flags & CP_PARSER_FLAGS_OPTIONAL)
20370 cp_parser_parse_tentatively (parser);
20372 /* Remember current tentative parsing state -- if we know we need
20373 a type, we can give better diagnostics here. */
20374 bool tent = cp_parser_parsing_tentatively (parser);
20376 token = cp_lexer_peek_token (parser->lexer);
20378 /* Look for the optional `::' operator. */
20379 global_p
20380 = (cp_parser_global_scope_opt (parser,
20381 /*current_scope_valid_p=*/false)
20382 != NULL_TREE);
20383 /* Look for the nested-name specifier. */
20384 qualified_p
20385 = (cp_parser_nested_name_specifier_opt (parser,
20386 /*typename_keyword_p=*/false,
20387 /*check_dependency_p=*/true,
20388 /*type_p=*/false,
20389 /*is_declaration=*/false)
20390 != NULL_TREE);
20391 /* If we have seen a nested-name-specifier, and the next token
20392 is `template', then we are using the template-id production. */
20393 if (parser->scope
20394 && cp_parser_optional_template_keyword (parser))
20396 /* Look for the template-id. */
20397 type = cp_parser_template_id (parser,
20398 /*template_keyword_p=*/true,
20399 /*check_dependency_p=*/true,
20400 none_type,
20401 /*is_declaration=*/false);
20402 /* If the template-id did not name a type, we are out of
20403 luck. */
20404 if (TREE_CODE (type) != TYPE_DECL)
20406 /* ...unless we pretend we have seen 'typename'. */
20407 if (typename_p)
20408 type = cp_parser_make_typename_type (parser, type,
20409 token->location);
20410 else
20412 cp_parser_error (parser, "expected template-id for type");
20413 type = error_mark_node;
20417 /* DR 1812: A < following a qualified-id in a typename-specifier
20418 could safely be assumed to begin a template argument list, so
20419 the template keyword should be optional. */
20420 else if (parser->scope
20421 && qualified_p
20422 && typename_p
20423 && cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID))
20425 cp_parser_parse_tentatively (parser);
20427 type = cp_parser_template_id (parser,
20428 /*template_keyword_p=*/true,
20429 /*check_dependency_p=*/true,
20430 none_type,
20431 /*is_declaration=*/false);
20432 /* This is handled below, so back off. */
20433 if (type && concept_check_p (type))
20434 cp_parser_simulate_error (parser);
20436 if (!cp_parser_parse_definitely (parser))
20437 type = NULL_TREE;
20438 else if (TREE_CODE (type) == TEMPLATE_ID_EXPR)
20439 type = make_typename_type (parser->scope, type, typename_type,
20440 /*complain=*/tf_error);
20441 else if (TREE_CODE (type) != TYPE_DECL)
20442 type = NULL_TREE;
20445 /* Otherwise, look for a type-name. */
20446 if (!type)
20448 if (cxx_dialect >= cxx17 || flag_concepts)
20449 cp_parser_parse_tentatively (parser);
20451 type = cp_parser_type_name (parser, (qualified_p && typename_p));
20453 if ((cxx_dialect >= cxx17 || flag_concepts)
20454 && !cp_parser_parse_definitely (parser))
20455 type = NULL_TREE;
20458 if (!type && flag_concepts && decl_specs)
20460 /* Try for a type-constraint with template arguments. We check
20461 decl_specs here to avoid trying this for a functional cast. */
20463 cp_parser_parse_tentatively (parser);
20465 type = cp_parser_template_id (parser,
20466 /*template_keyword_p=*/false,
20467 /*check_dependency_p=*/true,
20468 none_type,
20469 /*is_declaration=*/false);
20470 if (type && concept_check_p (type))
20472 location_t loc = EXPR_LOCATION (type);
20473 type = cp_parser_placeholder_type_specifier (parser, loc,
20474 type, tent);
20475 if (tent && type == error_mark_node)
20476 /* Perhaps it's a concept-check expression. */
20477 cp_parser_simulate_error (parser);
20479 else
20480 cp_parser_simulate_error (parser);
20482 if (!cp_parser_parse_definitely (parser))
20483 type = NULL_TREE;
20486 if (!type && cxx_dialect >= cxx17)
20488 /* Try class template argument deduction or type-constraint without
20489 template arguments. */
20490 tree name = cp_parser_identifier (parser);
20491 if (name && TREE_CODE (name) == IDENTIFIER_NODE
20492 && parser->scope != error_mark_node)
20494 location_t loc
20495 = cp_lexer_previous_token (parser->lexer)->location;
20496 tree tmpl = cp_parser_lookup_name (parser, name,
20497 none_type,
20498 /*is_template=*/false,
20499 /*is_namespace=*/false,
20500 /*check_dependency=*/true,
20501 /*ambiguous_decls=*/NULL,
20502 token->location);
20503 if (tmpl && tmpl != error_mark_node
20504 && ctad_template_p (tmpl))
20505 type = make_template_placeholder (tmpl);
20506 else if (flag_concepts && tmpl && concept_definition_p (tmpl))
20507 type = cp_parser_placeholder_type_specifier (parser, loc,
20508 tmpl, tent);
20509 else
20511 type = error_mark_node;
20512 if (!cp_parser_simulate_error (parser))
20513 cp_parser_name_lookup_error (parser, name, tmpl,
20514 NLE_TYPE, token->location);
20517 else
20518 type = error_mark_node;
20521 /* If it didn't work out, we don't have a TYPE. */
20522 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
20523 && !cp_parser_parse_definitely (parser))
20524 type = NULL_TREE;
20526 /* Keep track of all name-lookups performed in class scopes. */
20527 if (type
20528 && !global_p
20529 && !qualified_p
20530 && TREE_CODE (type) == TYPE_DECL
20531 && identifier_p (DECL_NAME (type)))
20532 maybe_note_name_used_in_class (DECL_NAME (type), type);
20534 if (type && decl_specs)
20535 cp_parser_set_decl_spec_type (decl_specs, type,
20536 token,
20537 /*type_definition_p=*/false);
20540 /* If we didn't get a type-name, issue an error message. */
20541 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
20543 cp_parser_error (parser, "expected type-name");
20544 return error_mark_node;
20547 if (type && type != error_mark_node)
20549 /* See if TYPE is an Objective-C type, and if so, parse and
20550 accept any protocol references following it. Do this before
20551 the cp_parser_check_for_invalid_template_id() call, because
20552 Objective-C types can be followed by '<...>' which would
20553 enclose protocol names rather than template arguments, and so
20554 everything is fine. */
20555 if (c_dialect_objc () && !parser->scope
20556 && (objc_is_id (type) || objc_is_class_name (type)))
20558 tree protos = cp_parser_objc_protocol_refs_opt (parser);
20559 tree qual_type = objc_get_protocol_qualified_type (type, protos);
20561 /* Clobber the "unqualified" type previously entered into
20562 DECL_SPECS with the new, improved protocol-qualified version. */
20563 if (decl_specs)
20564 decl_specs->type = qual_type;
20566 return qual_type;
20569 /* There is no valid C++ program where a non-template type is
20570 followed by a "<". That usually indicates that the user
20571 thought that the type was a template. */
20572 cp_parser_check_for_invalid_template_id (parser, type,
20573 none_type,
20574 token->location);
20577 return type;
20580 /* Parse the remainder of a placholder-type-specifier.
20582 placeholder-type-specifier:
20583 type-constraint_opt auto
20584 type-constraint_opt decltype(auto)
20586 The raw form of the constraint is parsed in cp_parser_simple_type_specifier
20587 and passed as TMPL. This function converts TMPL to an actual type-constraint,
20588 parses the placeholder type, and performs some contextual syntactic analysis.
20590 LOC provides the location of the template name.
20592 TENTATIVE is true if the type-specifier parsing is tentative; in that case,
20593 don't give an error if TMPL isn't a valid type-constraint, as the template-id
20594 might actually be a concept-check,
20596 Note that the Concepts TS allows the auto or decltype(auto) to be
20597 omitted in a constrained-type-specifier. */
20599 static tree
20600 cp_parser_placeholder_type_specifier (cp_parser *parser, location_t loc,
20601 tree tmpl, bool tentative)
20603 if (tmpl == error_mark_node)
20604 return error_mark_node;
20606 tree orig_tmpl = tmpl;
20608 /* Get the arguments as written for subsequent analysis. */
20609 tree args = NULL_TREE;
20610 if (TREE_CODE (tmpl) == TEMPLATE_ID_EXPR)
20612 args = TREE_OPERAND (tmpl, 1);
20613 tmpl = TREE_OPERAND (tmpl, 0);
20615 else
20616 /* A concept-name with no arguments can't be an expression. */
20617 tentative = false;
20619 tsubst_flags_t complain = tentative ? tf_none : tf_warning_or_error;
20621 /* Get the concept and prototype parameter for the constraint. */
20622 tree_pair info = finish_type_constraints (tmpl, args, complain);
20623 tree con = info.first;
20624 tree proto = info.second;
20625 if (con == error_mark_node)
20626 return error_mark_node;
20628 /* As per the standard, require auto or decltype(auto), except in some
20629 cases (template parameter lists, -fconcepts-ts enabled). */
20630 cp_token *placeholder = NULL, *close_paren = NULL;
20631 if (cxx_dialect >= cxx20)
20633 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
20634 placeholder = cp_lexer_consume_token (parser->lexer);
20635 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DECLTYPE))
20637 placeholder = cp_lexer_consume_token (parser->lexer);
20638 matching_parens parens;
20639 parens.require_open (parser);
20640 cp_parser_require_keyword (parser, RID_AUTO, RT_AUTO);
20641 close_paren = parens.require_close (parser);
20645 /* A type constraint constrains a contextually determined type or type
20646 parameter pack. However, the Concepts TS does allow concepts
20647 to introduce non-type and template template parameters. */
20648 if (TREE_CODE (proto) != TYPE_DECL)
20650 if (!flag_concepts_ts
20651 || !processing_template_parmlist)
20653 if (!tentative)
20655 error_at (loc, "%qE does not constrain a type", DECL_NAME (con));
20656 inform (DECL_SOURCE_LOCATION (con), "concept defined here");
20658 return error_mark_node;
20662 /* In a template parameter list, a type-parameter can be introduced
20663 by type-constraints alone. */
20664 if (processing_template_parmlist && !placeholder)
20666 /* In a default argument we may not be creating new parameters. */
20667 if (parser->local_variables_forbidden_p & LOCAL_VARS_FORBIDDEN)
20669 /* If this assert turns out to be false, do error() instead. */
20670 gcc_assert (tentative);
20671 return error_mark_node;
20673 return build_constrained_parameter (con, proto, args);
20676 /* Diagnose issues placeholder issues. */
20677 if (!flag_concepts_ts
20678 && !parser->in_result_type_constraint_p
20679 && !placeholder)
20681 if (tentative)
20682 /* Perhaps it's a concept-check expression (c++/91073). */
20683 return error_mark_node;
20685 tree id = build_nt (TEMPLATE_ID_EXPR, tmpl, args);
20686 tree expr = DECL_P (orig_tmpl) ? DECL_NAME (con) : id;
20687 error_at (input_location,
20688 "expected %<auto%> or %<decltype(auto)%> after %qE", expr);
20689 /* Fall through. This is an error of omission. */
20691 else if (parser->in_result_type_constraint_p && placeholder)
20693 /* A trailing return type only allows type-constraints. */
20694 error_at (input_location,
20695 "unexpected placeholder in constrained result type");
20698 /* In a parameter-declaration-clause, a placeholder-type-specifier
20699 results in an invented template parameter. */
20700 if (parser->auto_is_implicit_function_template_parm_p)
20702 if (close_paren)
20704 location_t loc = make_location (placeholder->location,
20705 placeholder->location,
20706 close_paren->location);
20707 error_at (loc, "cannot declare a parameter with %<decltype(auto)%>");
20708 return error_mark_node;
20710 tree parm = build_constrained_parameter (con, proto, args);
20711 return synthesize_implicit_template_parm (parser, parm);
20714 /* Determine if the type should be deduced using template argument
20715 deduction or decltype deduction. Note that the latter is always
20716 used for type-constraints in trailing return types. */
20717 bool decltype_p = placeholder
20718 ? placeholder->keyword == RID_DECLTYPE
20719 : parser->in_result_type_constraint_p;
20721 /* Otherwise, this is the type of a variable or return type. */
20722 if (decltype_p)
20723 return make_constrained_decltype_auto (con, args);
20724 else
20725 return make_constrained_auto (con, args);
20728 /* Parse a type-name.
20730 type-name:
20731 class-name
20732 enum-name
20733 typedef-name
20734 simple-template-id [in c++0x]
20736 enum-name:
20737 identifier
20739 typedef-name:
20740 identifier
20742 Concepts:
20744 type-name:
20745 concept-name
20746 partial-concept-id
20748 concept-name:
20749 identifier
20751 Returns a TYPE_DECL for the type. */
20753 static tree
20754 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
20756 tree type_decl;
20758 /* We can't know yet whether it is a class-name or not. */
20759 cp_parser_parse_tentatively (parser);
20760 /* Try a class-name. */
20761 type_decl = cp_parser_class_name (parser,
20762 typename_keyword_p,
20763 /*template_keyword_p=*/false,
20764 none_type,
20765 /*check_dependency_p=*/true,
20766 /*class_head_p=*/false,
20767 /*is_declaration=*/false);
20768 /* If it's not a class-name, keep looking. */
20769 if (!cp_parser_parse_definitely (parser))
20771 if (cxx_dialect < cxx11)
20772 /* It must be a typedef-name or an enum-name. */
20773 return cp_parser_nonclass_name (parser);
20775 cp_parser_parse_tentatively (parser);
20776 /* It is either a simple-template-id representing an
20777 instantiation of an alias template... */
20778 type_decl = cp_parser_template_id (parser,
20779 /*template_keyword_p=*/false,
20780 /*check_dependency_p=*/true,
20781 none_type,
20782 /*is_declaration=*/false);
20783 /* Note that this must be an instantiation of an alias template
20784 because [temp.names]/6 says:
20786 A template-id that names an alias template specialization
20787 is a type-name.
20789 Whereas [temp.names]/7 says:
20791 A simple-template-id that names a class template
20792 specialization is a class-name.
20794 With concepts, this could also be a partial-concept-id that
20795 declares a non-type template parameter. */
20796 if (type_decl != NULL_TREE
20797 && TREE_CODE (type_decl) == TYPE_DECL
20798 && TYPE_DECL_ALIAS_P (type_decl))
20799 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
20800 else
20801 cp_parser_simulate_error (parser);
20803 if (!cp_parser_parse_definitely (parser))
20804 /* ... Or a typedef-name or an enum-name. */
20805 return cp_parser_nonclass_name (parser);
20808 return type_decl;
20811 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
20812 or a concept-name.
20814 enum-name:
20815 identifier
20817 typedef-name:
20818 identifier
20820 concept-name:
20821 identifier
20823 Returns a TYPE_DECL for the type. */
20825 static tree
20826 cp_parser_nonclass_name (cp_parser* parser)
20828 tree type_decl;
20829 tree identifier;
20831 cp_token *token = cp_lexer_peek_token (parser->lexer);
20832 identifier = cp_parser_identifier (parser);
20833 if (identifier == error_mark_node)
20834 return error_mark_node;
20836 /* Look up the type-name. */
20837 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
20839 type_decl = strip_using_decl (type_decl);
20841 if (TREE_CODE (type_decl) != TYPE_DECL
20842 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
20844 /* See if this is an Objective-C type. */
20845 tree protos = cp_parser_objc_protocol_refs_opt (parser);
20846 tree type = objc_get_protocol_qualified_type (identifier, protos);
20847 if (type)
20848 type_decl = TYPE_NAME (type);
20851 /* Issue an error if we did not find a type-name. */
20852 if (TREE_CODE (type_decl) != TYPE_DECL
20853 /* In Objective-C, we have the complication that class names are
20854 normally type names and start declarations (eg, the
20855 "NSObject" in "NSObject *object;"), but can be used in an
20856 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
20857 is an expression. So, a classname followed by a dot is not a
20858 valid type-name. */
20859 || (objc_is_class_name (TREE_TYPE (type_decl))
20860 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
20862 if (!cp_parser_simulate_error (parser))
20863 cp_parser_name_lookup_error (parser, identifier, type_decl,
20864 NLE_TYPE, token->location);
20865 return error_mark_node;
20867 /* Remember that the name was used in the definition of the
20868 current class so that we can check later to see if the
20869 meaning would have been different after the class was
20870 entirely defined. */
20871 else if (type_decl != error_mark_node
20872 && !parser->scope)
20873 maybe_note_name_used_in_class (identifier, type_decl);
20875 return type_decl;
20878 /* Parse an elaborated-type-specifier. Note that the grammar given
20879 here incorporates the resolution to DR68.
20881 elaborated-type-specifier:
20882 class-key :: [opt] nested-name-specifier [opt] identifier
20883 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
20884 enum-key :: [opt] nested-name-specifier [opt] identifier
20885 typename :: [opt] nested-name-specifier identifier
20886 typename :: [opt] nested-name-specifier template [opt]
20887 template-id
20889 GNU extension:
20891 elaborated-type-specifier:
20892 class-key attributes :: [opt] nested-name-specifier [opt] identifier
20893 class-key attributes :: [opt] nested-name-specifier [opt]
20894 template [opt] template-id
20895 enum attributes :: [opt] nested-name-specifier [opt] identifier
20897 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
20898 declared `friend'. If IS_DECLARATION is TRUE, then this
20899 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
20900 something is being declared.
20902 Returns the TYPE specified. */
20904 static tree
20905 cp_parser_elaborated_type_specifier (cp_parser* parser,
20906 bool is_friend,
20907 bool is_declaration)
20909 enum tag_types tag_type;
20910 tree identifier;
20911 tree type = NULL_TREE;
20912 tree attributes = NULL_TREE;
20913 tree globalscope;
20914 cp_token *token = NULL;
20916 /* For class and enum types the location of the class-key or enum-key. */
20917 location_t key_loc = cp_lexer_peek_token (parser->lexer)->location;
20918 /* For a scoped enum, the 'class' or 'struct' keyword id. */
20919 rid scoped_key = RID_MAX;
20921 /* See if we're looking at the `enum' keyword. */
20922 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
20924 /* Consume the `enum' token. */
20925 cp_lexer_consume_token (parser->lexer);
20926 /* Remember that it's an enumeration type. */
20927 tag_type = enum_type;
20928 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
20929 enums) is used here. */
20930 cp_token *token = cp_lexer_peek_token (parser->lexer);
20931 if (cp_parser_is_keyword (token, scoped_key = RID_CLASS)
20932 || cp_parser_is_keyword (token, scoped_key = RID_STRUCT))
20934 location_t loc = token->location;
20935 gcc_rich_location richloc (loc);
20936 richloc.add_range (input_location);
20937 richloc.add_fixit_remove ();
20938 pedwarn (&richloc, 0, "elaborated-type-specifier for "
20939 "a scoped enum must not use the %qD keyword",
20940 token->u.value);
20941 /* Consume the `struct' or `class' and parse it anyway. */
20942 cp_lexer_consume_token (parser->lexer);
20943 /* Create a combined location for the whole scoped-enum-key. */
20944 key_loc = make_location (key_loc, key_loc, loc);
20946 else
20947 scoped_key = RID_MAX;
20949 /* Parse the attributes. */
20950 attributes = cp_parser_attributes_opt (parser);
20952 /* Or, it might be `typename'. */
20953 else if (cp_lexer_next_token_is_keyword (parser->lexer,
20954 RID_TYPENAME))
20956 /* Consume the `typename' token. */
20957 cp_lexer_consume_token (parser->lexer);
20958 /* Remember that it's a `typename' type. */
20959 tag_type = typename_type;
20961 /* Otherwise it must be a class-key. */
20962 else
20964 key_loc = cp_lexer_peek_token (parser->lexer)->location;
20965 tag_type = cp_parser_class_key (parser);
20966 if (tag_type == none_type)
20967 return error_mark_node;
20968 /* Parse the attributes. */
20969 attributes = cp_parser_attributes_opt (parser);
20972 /* Look for the `::' operator. */
20973 globalscope = cp_parser_global_scope_opt (parser,
20974 /*current_scope_valid_p=*/false);
20975 /* Look for the nested-name-specifier. */
20976 tree nested_name_specifier;
20977 if (tag_type == typename_type && !globalscope)
20979 nested_name_specifier
20980 = cp_parser_nested_name_specifier (parser,
20981 /*typename_keyword_p=*/true,
20982 /*check_dependency_p=*/true,
20983 /*type_p=*/true,
20984 is_declaration);
20985 if (!nested_name_specifier)
20986 return error_mark_node;
20988 else
20989 /* Even though `typename' is not present, the proposed resolution
20990 to Core Issue 180 says that in `class A<T>::B', `B' should be
20991 considered a type-name, even if `A<T>' is dependent. */
20992 nested_name_specifier
20993 = cp_parser_nested_name_specifier_opt (parser,
20994 /*typename_keyword_p=*/true,
20995 /*check_dependency_p=*/true,
20996 /*type_p=*/true,
20997 is_declaration);
20998 /* For everything but enumeration types, consider a template-id.
20999 For an enumeration type, consider only a plain identifier. */
21000 if (tag_type != enum_type)
21002 bool template_p = false;
21003 tree decl;
21005 /* Allow the `template' keyword. */
21006 template_p = cp_parser_optional_template_keyword (parser);
21007 /* If we didn't see `template', we don't know if there's a
21008 template-id or not. */
21009 if (!template_p)
21010 cp_parser_parse_tentatively (parser);
21011 /* The `template' keyword must follow a nested-name-specifier. */
21012 else if (!nested_name_specifier && !globalscope)
21014 cp_parser_error (parser, "%<template%> must follow a nested-"
21015 "name-specifier");
21016 return error_mark_node;
21019 /* Parse the template-id. */
21020 token = cp_lexer_peek_token (parser->lexer);
21021 decl = cp_parser_template_id (parser, template_p,
21022 /*check_dependency_p=*/true,
21023 tag_type,
21024 is_declaration);
21025 /* If we didn't find a template-id, look for an ordinary
21026 identifier. */
21027 if (!template_p && !cp_parser_parse_definitely (parser))
21029 /* We can get here when cp_parser_template_id, called by
21030 cp_parser_class_name with tag_type == none_type, succeeds
21031 and caches a BASELINK. Then, when called again here,
21032 instead of failing and returning an error_mark_node
21033 returns it (see template/typename17.C in C++11).
21034 ??? Could we diagnose this earlier? */
21035 else if (tag_type == typename_type && BASELINK_P (decl))
21037 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
21038 type = error_mark_node;
21040 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
21041 in effect, then we must assume that, upon instantiation, the
21042 template will correspond to a class. */
21043 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
21044 && tag_type == typename_type)
21045 type = make_typename_type (parser->scope, decl,
21046 typename_type,
21047 /*complain=*/tf_error);
21048 /* If the `typename' keyword is in effect and DECL is not a type
21049 decl, then type is non existent. */
21050 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
21052 else if (TREE_CODE (decl) == TYPE_DECL)
21054 type = check_elaborated_type_specifier (tag_type, decl,
21055 /*allow_template_p=*/true);
21057 /* If the next token is a semicolon, this must be a specialization,
21058 instantiation, or friend declaration. Check the scope while we
21059 still know whether or not we had a nested-name-specifier. */
21060 if (type != error_mark_node
21061 && !nested_name_specifier && !is_friend
21062 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21063 check_unqualified_spec_or_inst (type, token->location);
21065 else if (decl == error_mark_node)
21066 type = error_mark_node;
21069 if (!type)
21071 token = cp_lexer_peek_token (parser->lexer);
21072 identifier = cp_parser_identifier (parser);
21074 if (identifier == error_mark_node)
21076 parser->scope = NULL_TREE;
21077 return error_mark_node;
21080 /* For a `typename', we needn't call xref_tag. */
21081 if (tag_type == typename_type
21082 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
21083 return cp_parser_make_typename_type (parser, identifier,
21084 token->location);
21086 /* Template parameter lists apply only if we are not within a
21087 function parameter list. */
21088 bool template_parm_lists_apply
21089 = parser->num_template_parameter_lists;
21090 if (template_parm_lists_apply)
21091 for (cp_binding_level *s = current_binding_level;
21092 s && s->kind != sk_template_parms;
21093 s = s->level_chain)
21094 if (s->kind == sk_function_parms)
21095 template_parm_lists_apply = false;
21097 /* Look up a qualified name in the usual way. */
21098 if (parser->scope)
21100 tree decl;
21101 tree ambiguous_decls;
21103 decl = cp_parser_lookup_name (parser, identifier,
21104 tag_type,
21105 /*is_template=*/false,
21106 /*is_namespace=*/false,
21107 /*check_dependency=*/true,
21108 &ambiguous_decls,
21109 token->location);
21111 /* If the lookup was ambiguous, an error will already have been
21112 issued. */
21113 if (ambiguous_decls)
21114 return error_mark_node;
21116 /* If we are parsing friend declaration, DECL may be a
21117 TEMPLATE_DECL tree node here. However, we need to check
21118 whether this TEMPLATE_DECL results in valid code. Consider
21119 the following example:
21121 namespace N {
21122 template <class T> class C {};
21124 class X {
21125 template <class T> friend class N::C; // #1, valid code
21127 template <class T> class Y {
21128 friend class N::C; // #2, invalid code
21131 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
21132 name lookup of `N::C'. We see that friend declaration must
21133 be template for the code to be valid. Note that
21134 processing_template_decl does not work here since it is
21135 always 1 for the above two cases. */
21137 decl = (cp_parser_maybe_treat_template_as_class
21138 (decl, /*tag_name_p=*/is_friend
21139 && template_parm_lists_apply));
21141 if (TREE_CODE (decl) != TYPE_DECL)
21143 cp_parser_diagnose_invalid_type_name (parser,
21144 identifier,
21145 token->location);
21146 return error_mark_node;
21149 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
21151 bool allow_template = (template_parm_lists_apply
21152 || DECL_SELF_REFERENCE_P (decl));
21153 type = check_elaborated_type_specifier (tag_type, decl,
21154 allow_template);
21156 if (type == error_mark_node)
21157 return error_mark_node;
21160 /* Forward declarations of nested types, such as
21162 class C1::C2;
21163 class C1::C2::C3;
21165 are invalid unless all components preceding the final '::'
21166 are complete. If all enclosing types are complete, these
21167 declarations become merely pointless.
21169 Invalid forward declarations of nested types are errors
21170 caught elsewhere in parsing. Those that are pointless arrive
21171 here. */
21173 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21174 && !is_friend && is_declaration
21175 && !processing_explicit_instantiation)
21176 warning (0, "declaration %qD does not declare anything", decl);
21178 type = TREE_TYPE (decl);
21180 else
21182 /* An elaborated-type-specifier sometimes introduces a new type and
21183 sometimes names an existing type. Normally, the rule is that it
21184 introduces a new type only if there is not an existing type of
21185 the same name already in scope. For example, given:
21187 struct S {};
21188 void f() { struct S s; }
21190 the `struct S' in the body of `f' is the same `struct S' as in
21191 the global scope; the existing definition is used. However, if
21192 there were no global declaration, this would introduce a new
21193 local class named `S'.
21195 An exception to this rule applies to the following code:
21197 namespace N { struct S; }
21199 Here, the elaborated-type-specifier names a new type
21200 unconditionally; even if there is already an `S' in the
21201 containing scope this declaration names a new type.
21202 This exception only applies if the elaborated-type-specifier
21203 forms the complete declaration:
21205 [class.name]
21207 A declaration consisting solely of `class-key identifier ;' is
21208 either a redeclaration of the name in the current scope or a
21209 forward declaration of the identifier as a class name. It
21210 introduces the name into the current scope.
21212 We are in this situation precisely when the next token is a `;'.
21214 An exception to the exception is that a `friend' declaration does
21215 *not* name a new type; i.e., given:
21217 struct S { friend struct T; };
21219 `T' is not a new type in the scope of `S'.
21221 Also, `new struct S' or `sizeof (struct S)' never results in the
21222 definition of a new type; a new type can only be declared in a
21223 declaration context. */
21225 TAG_how how;
21227 if (is_friend)
21228 /* Friends have special name lookup rules. */
21229 how = TAG_how::HIDDEN_FRIEND;
21230 else if (is_declaration
21231 && cp_lexer_next_token_is (parser->lexer,
21232 CPP_SEMICOLON))
21233 /* This is a `class-key identifier ;' */
21234 how = TAG_how::CURRENT_ONLY;
21235 else
21236 how = TAG_how::GLOBAL;
21238 bool template_p =
21239 (template_parm_lists_apply
21240 && (cp_parser_next_token_starts_class_definition_p (parser)
21241 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
21242 /* An unqualified name was used to reference this type, so
21243 there were no qualifying templates. */
21244 if (template_parm_lists_apply
21245 && !cp_parser_check_template_parameters (parser,
21246 /*num_templates=*/0,
21247 /*template_id*/false,
21248 token->location,
21249 /*declarator=*/NULL))
21250 return error_mark_node;
21252 type = xref_tag (tag_type, identifier, how, template_p);
21256 if (type == error_mark_node)
21257 return error_mark_node;
21259 /* Allow attributes on forward declarations of classes. */
21260 if (attributes)
21262 if (TREE_CODE (type) == TYPENAME_TYPE)
21263 warning (OPT_Wattributes,
21264 "attributes ignored on uninstantiated type");
21265 else if (tag_type != enum_type
21266 && TREE_CODE (type) != BOUND_TEMPLATE_TEMPLATE_PARM
21267 && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
21268 && ! processing_explicit_instantiation)
21269 warning (OPT_Wattributes,
21270 "attributes ignored on template instantiation");
21271 else if (is_friend && cxx11_attribute_p (attributes))
21273 if (warning (OPT_Wattributes, "attribute ignored"))
21274 inform (input_location, "an attribute that appertains to a friend "
21275 "declaration that is not a definition is ignored");
21277 else if (is_declaration && cp_parser_declares_only_class_p (parser))
21278 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
21279 else
21280 warning (OPT_Wattributes,
21281 "attributes ignored on elaborated-type-specifier that is "
21282 "not a forward declaration");
21285 if (tag_type == enum_type)
21286 cp_parser_maybe_warn_enum_key (parser, key_loc, type, scoped_key);
21287 else
21289 /* Diagnose class/struct/union mismatches. IS_DECLARATION is false
21290 for alias definition. */
21291 bool decl_class = (is_declaration
21292 && cp_parser_declares_only_class_p (parser));
21293 cp_parser_check_class_key (parser, key_loc, tag_type, type, false,
21294 decl_class);
21296 /* Indicate whether this class was declared as a `class' or as a
21297 `struct'. */
21298 if (CLASS_TYPE_P (type) && !currently_open_class (type))
21299 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
21302 /* A "<" cannot follow an elaborated type specifier. If that
21303 happens, the user was probably trying to form a template-id. */
21304 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
21305 token->location);
21307 return type;
21310 /* Parse an enum-specifier.
21312 enum-specifier:
21313 enum-head { enumerator-list [opt] }
21314 enum-head { enumerator-list , } [C++0x]
21316 enum-head:
21317 enum-key identifier [opt] enum-base [opt]
21318 enum-key nested-name-specifier identifier enum-base [opt]
21320 enum-key:
21321 enum
21322 enum class [C++0x]
21323 enum struct [C++0x]
21325 enum-base: [C++0x]
21326 : type-specifier-seq
21328 opaque-enum-specifier:
21329 enum-key identifier enum-base [opt] ;
21331 GNU Extensions:
21332 enum-key attributes[opt] identifier [opt] enum-base [opt]
21333 { enumerator-list [opt] }attributes[opt]
21334 enum-key attributes[opt] identifier [opt] enum-base [opt]
21335 { enumerator-list, }attributes[opt] [C++0x]
21337 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
21338 if the token stream isn't an enum-specifier after all. */
21340 static tree
21341 cp_parser_enum_specifier (cp_parser* parser)
21343 tree identifier;
21344 tree type = NULL_TREE;
21345 tree prev_scope;
21346 tree nested_name_specifier = NULL_TREE;
21347 tree attributes;
21348 bool scoped_enum_p = false;
21349 bool has_underlying_type = false;
21350 bool nested_being_defined = false;
21351 bool new_value_list = false;
21352 bool is_new_type = false;
21353 bool is_unnamed = false;
21354 tree underlying_type = NULL_TREE;
21355 cp_token *type_start_token = NULL;
21356 auto cleanup = make_temp_override (parser->colon_corrects_to_scope_p, false);
21358 /* Parse tentatively so that we can back up if we don't find a
21359 enum-specifier. */
21360 cp_parser_parse_tentatively (parser);
21362 /* Caller guarantees that the current token is 'enum', an identifier
21363 possibly follows, and the token after that is an opening brace.
21364 If we don't have an identifier, fabricate an anonymous name for
21365 the enumeration being defined. */
21366 cp_lexer_consume_token (parser->lexer);
21368 /* Parse the "class" or "struct", which indicates a scoped
21369 enumeration type in C++0x. */
21370 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
21371 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
21373 if (cxx_dialect < cxx11)
21374 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
21376 /* Consume the `struct' or `class' token. */
21377 cp_lexer_consume_token (parser->lexer);
21379 scoped_enum_p = true;
21382 attributes = cp_parser_attributes_opt (parser);
21384 /* Clear the qualification. */
21385 parser->scope = NULL_TREE;
21386 parser->qualifying_scope = NULL_TREE;
21387 parser->object_scope = NULL_TREE;
21389 /* Figure out in what scope the declaration is being placed. */
21390 prev_scope = current_scope ();
21392 type_start_token = cp_lexer_peek_token (parser->lexer);
21394 push_deferring_access_checks (dk_no_check);
21395 nested_name_specifier
21396 = cp_parser_nested_name_specifier_opt (parser,
21397 /*typename_keyword_p=*/true,
21398 /*check_dependency_p=*/false,
21399 /*type_p=*/false,
21400 /*is_declaration=*/false);
21402 if (nested_name_specifier)
21404 tree name;
21406 identifier = cp_parser_identifier (parser);
21407 name = cp_parser_lookup_name (parser, identifier,
21408 enum_type,
21409 /*is_template=*/false,
21410 /*is_namespace=*/false,
21411 /*check_dependency=*/true,
21412 /*ambiguous_decls=*/NULL,
21413 input_location);
21414 if (name && name != error_mark_node)
21416 type = TREE_TYPE (name);
21417 if (TREE_CODE (type) == TYPENAME_TYPE)
21419 /* Are template enums allowed in ISO? */
21420 if (template_parm_scope_p ())
21421 pedwarn (type_start_token->location, OPT_Wpedantic,
21422 "%qD is an enumeration template", name);
21423 /* ignore a typename reference, for it will be solved by name
21424 in start_enum. */
21425 type = NULL_TREE;
21428 else if (nested_name_specifier == error_mark_node)
21429 /* We already issued an error. */;
21430 else
21432 error_at (type_start_token->location,
21433 "%qD does not name an enumeration in %qT",
21434 identifier, nested_name_specifier);
21435 nested_name_specifier = error_mark_node;
21438 else
21440 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21441 identifier = cp_parser_identifier (parser);
21442 else
21444 identifier = make_anon_name ();
21445 is_unnamed = true;
21446 if (scoped_enum_p)
21447 error_at (type_start_token->location,
21448 "unnamed scoped enum is not allowed");
21451 pop_deferring_access_checks ();
21453 /* Check for the `:' that denotes a specified underlying type in C++0x.
21454 Note that a ':' could also indicate a bitfield width, however. */
21455 location_t colon_loc = UNKNOWN_LOCATION;
21456 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
21458 cp_decl_specifier_seq type_specifiers;
21460 /* Consume the `:'. */
21461 colon_loc = cp_lexer_peek_token (parser->lexer)->location;
21462 cp_lexer_consume_token (parser->lexer);
21464 auto tdf
21465 = make_temp_override (parser->type_definition_forbidden_message,
21466 G_("types may not be defined in enum-base"));
21468 /* Parse the type-specifier-seq. */
21469 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
21470 /*is_declaration=*/false,
21471 /*is_trailing_return=*/false,
21472 &type_specifiers);
21474 /* At this point this is surely not elaborated type specifier. */
21475 if (!cp_parser_parse_definitely (parser))
21476 return NULL_TREE;
21478 if (cxx_dialect < cxx11)
21479 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
21481 has_underlying_type = true;
21483 /* If that didn't work, stop. */
21484 if (type_specifiers.type != error_mark_node)
21486 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
21487 /*initialized=*/0, NULL);
21488 if (underlying_type == error_mark_node
21489 || check_for_bare_parameter_packs (underlying_type))
21490 underlying_type = NULL_TREE;
21494 /* Look for the `{' but don't consume it yet. */
21495 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21497 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
21499 if (has_underlying_type)
21500 cp_parser_commit_to_tentative_parse (parser);
21501 cp_parser_error (parser, "expected %<{%>");
21502 if (has_underlying_type)
21503 return error_mark_node;
21505 /* An opaque-enum-specifier must have a ';' here. */
21506 if ((scoped_enum_p || underlying_type)
21507 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21509 if (has_underlying_type)
21510 pedwarn (colon_loc,
21511 OPT_Welaborated_enum_base,
21512 "declaration of enumeration with "
21513 "fixed underlying type and no enumerator list is "
21514 "only permitted as a standalone declaration");
21515 else
21516 cp_parser_error (parser, "expected %<;%> or %<{%>");
21520 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
21521 return NULL_TREE;
21523 if (nested_name_specifier)
21525 if (CLASS_TYPE_P (nested_name_specifier))
21527 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
21528 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
21529 push_scope (nested_name_specifier);
21531 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
21532 push_nested_namespace (nested_name_specifier);
21535 /* Issue an error message if type-definitions are forbidden here. */
21536 if (!cp_parser_check_type_definition (parser))
21537 type = error_mark_node;
21538 else
21539 /* Create the new type. We do this before consuming the opening
21540 brace so the enum will be recorded as being on the line of its
21541 tag (or the 'enum' keyword, if there is no tag). */
21542 type = start_enum (identifier, type, underlying_type,
21543 attributes, scoped_enum_p, &is_new_type);
21545 /* If the next token is not '{' it is an opaque-enum-specifier or an
21546 elaborated-type-specifier. */
21547 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21549 auto_timevar tv (TV_PARSE_ENUM);
21551 if (nested_name_specifier
21552 && nested_name_specifier != error_mark_node)
21554 /* The following catches invalid code such as:
21555 enum class S<int>::E { A, B, C }; */
21556 if (!processing_specialization
21557 && CLASS_TYPE_P (nested_name_specifier)
21558 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
21559 error_at (type_start_token->location, "cannot add an enumerator "
21560 "list to a template instantiation");
21562 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
21564 error_at (type_start_token->location,
21565 "%<%T::%E%> has not been declared",
21566 TYPE_CONTEXT (nested_name_specifier),
21567 nested_name_specifier);
21568 type = error_mark_node;
21570 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
21571 && !CLASS_TYPE_P (nested_name_specifier))
21573 error_at (type_start_token->location, "nested name specifier "
21574 "%qT for enum declaration does not name a class "
21575 "or namespace", nested_name_specifier);
21576 type = error_mark_node;
21578 /* If that scope does not contain the scope in which the
21579 class was originally declared, the program is invalid. */
21580 else if (prev_scope && !is_ancestor (prev_scope,
21581 nested_name_specifier))
21583 if (at_namespace_scope_p ())
21584 error_at (type_start_token->location,
21585 "declaration of %qD in namespace %qD which does not "
21586 "enclose %qD",
21587 type, prev_scope, nested_name_specifier);
21588 else
21589 error_at (type_start_token->location,
21590 "declaration of %qD in %qD which does not "
21591 "enclose %qD",
21592 type, prev_scope, nested_name_specifier);
21593 type = error_mark_node;
21595 /* If that scope is the scope where the declaration is being placed
21596 the program is invalid. */
21597 else if (CLASS_TYPE_P (nested_name_specifier)
21598 && CLASS_TYPE_P (prev_scope)
21599 && same_type_p (nested_name_specifier, prev_scope))
21601 permerror (type_start_token->location,
21602 "extra qualification not allowed");
21603 nested_name_specifier = NULL_TREE;
21607 if (scoped_enum_p)
21608 begin_scope (sk_scoped_enum, type);
21610 /* Consume the opening brace. */
21611 matching_braces braces;
21612 braces.consume_open (parser);
21614 if (type == error_mark_node)
21615 ; /* Nothing to add */
21616 else if (OPAQUE_ENUM_P (type)
21617 || (cxx_dialect > cxx98 && processing_specialization))
21619 new_value_list = true;
21620 SET_OPAQUE_ENUM_P (type, false);
21621 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
21623 else
21625 error_at (type_start_token->location,
21626 "multiple definition of %q#T", type);
21627 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
21628 "previous definition here");
21629 type = error_mark_node;
21632 if (type == error_mark_node)
21633 cp_parser_skip_to_end_of_block_or_statement (parser);
21634 /* If the next token is not '}', then there are some enumerators. */
21635 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21637 if (is_unnamed && !scoped_enum_p
21638 /* Don't warn for enum {} a; here. */
21639 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SEMICOLON))
21640 pedwarn (type_start_token->location, OPT_Wpedantic,
21641 "ISO C++ forbids empty unnamed enum");
21643 else
21645 /* We've seen a '{' so we know we're in an enum-specifier.
21646 Commit to any tentative parse to get syntax errors. */
21647 cp_parser_commit_to_tentative_parse (parser);
21648 cp_parser_enumerator_list (parser, type);
21651 /* Consume the final '}'. */
21652 braces.require_close (parser);
21654 if (scoped_enum_p)
21655 finish_scope ();
21657 else
21659 /* If a ';' follows, then it is an opaque-enum-specifier
21660 and additional restrictions apply. */
21661 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21663 if (is_unnamed)
21664 error_at (type_start_token->location,
21665 "opaque-enum-specifier without name");
21666 else if (nested_name_specifier)
21667 error_at (type_start_token->location,
21668 "opaque-enum-specifier must use a simple identifier");
21672 /* Look for trailing attributes to apply to this enumeration, and
21673 apply them if appropriate. */
21674 if (cp_parser_allow_gnu_extensions_p (parser))
21676 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
21677 cplus_decl_attributes (&type,
21678 trailing_attr,
21679 (int) ATTR_FLAG_TYPE_IN_PLACE);
21682 /* Finish up the enumeration. */
21683 if (type != error_mark_node)
21685 if (new_value_list)
21686 finish_enum_value_list (type);
21687 if (is_new_type)
21688 finish_enum (type);
21691 if (nested_name_specifier)
21693 if (CLASS_TYPE_P (nested_name_specifier))
21695 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
21696 pop_scope (nested_name_specifier);
21698 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
21699 pop_nested_namespace (nested_name_specifier);
21701 return type;
21704 /* Parse an enumerator-list. The enumerators all have the indicated
21705 TYPE.
21707 enumerator-list:
21708 enumerator-definition
21709 enumerator-list , enumerator-definition */
21711 static void
21712 cp_parser_enumerator_list (cp_parser* parser, tree type)
21714 while (true)
21716 /* Parse an enumerator-definition. */
21717 cp_parser_enumerator_definition (parser, type);
21719 /* If the next token is not a ',', we've reached the end of
21720 the list. */
21721 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21722 break;
21723 /* Otherwise, consume the `,' and keep going. */
21724 cp_lexer_consume_token (parser->lexer);
21725 /* If the next token is a `}', there is a trailing comma. */
21726 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21728 if (cxx_dialect < cxx11)
21729 pedwarn (input_location, OPT_Wpedantic,
21730 "comma at end of enumerator list");
21731 break;
21736 /* Parse an enumerator-definition. The enumerator has the indicated
21737 TYPE.
21739 enumerator-definition:
21740 enumerator
21741 enumerator = constant-expression
21743 enumerator:
21744 identifier
21746 GNU Extensions:
21748 enumerator-definition:
21749 enumerator attributes [opt]
21750 enumerator attributes [opt] = constant-expression */
21752 static void
21753 cp_parser_enumerator_definition (cp_parser* parser, tree type)
21755 tree identifier;
21756 tree value;
21757 location_t loc;
21759 /* Save the input location because we are interested in the location
21760 of the identifier and not the location of the explicit value. */
21761 loc = cp_lexer_peek_token (parser->lexer)->location;
21763 /* Look for the identifier. */
21764 identifier = cp_parser_identifier (parser);
21765 if (identifier == error_mark_node)
21766 return;
21768 /* Parse any specified attributes. */
21769 tree attrs = cp_parser_attributes_opt (parser);
21771 /* If the next token is an '=', then there is an explicit value. */
21772 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21774 /* Consume the `=' token. */
21775 cp_lexer_consume_token (parser->lexer);
21776 /* Parse the value. */
21777 value = cp_parser_constant_expression (parser);
21779 else
21780 value = NULL_TREE;
21782 /* If we are processing a template, make sure the initializer of the
21783 enumerator doesn't contain any bare template parameter pack. */
21784 if (current_lambda_expr ())
21786 /* In a lambda it should work, but doesn't currently. */
21787 if (uses_parameter_packs (value))
21789 sorry ("unexpanded parameter pack in enumerator in lambda");
21790 value = error_mark_node;
21793 else if (check_for_bare_parameter_packs (value))
21794 value = error_mark_node;
21796 /* Create the enumerator. */
21797 build_enumerator (identifier, value, type, attrs, loc);
21800 /* Parse a namespace-name.
21802 namespace-name:
21803 original-namespace-name
21804 namespace-alias
21806 Returns the NAMESPACE_DECL for the namespace. */
21808 static tree
21809 cp_parser_namespace_name (cp_parser* parser)
21811 tree identifier;
21812 tree namespace_decl;
21814 cp_token *token = cp_lexer_peek_token (parser->lexer);
21816 /* Get the name of the namespace. */
21817 identifier = cp_parser_identifier (parser);
21818 if (identifier == error_mark_node)
21819 return error_mark_node;
21821 /* Look up the identifier in the currently active scope. Look only
21822 for namespaces, due to:
21824 [basic.lookup.udir]
21826 When looking up a namespace-name in a using-directive or alias
21827 definition, only namespace names are considered.
21829 And:
21831 [basic.lookup.qual]
21833 During the lookup of a name preceding the :: scope resolution
21834 operator, object, function, and enumerator names are ignored.
21836 (Note that cp_parser_qualifying_entity only calls this
21837 function if the token after the name is the scope resolution
21838 operator.) */
21839 namespace_decl = cp_parser_lookup_name (parser, identifier,
21840 none_type,
21841 /*is_template=*/false,
21842 /*is_namespace=*/true,
21843 /*check_dependency=*/true,
21844 /*ambiguous_decls=*/NULL,
21845 token->location);
21846 /* If it's not a namespace, issue an error. */
21847 if (namespace_decl == error_mark_node
21848 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
21850 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21852 auto_diagnostic_group d;
21853 name_hint hint;
21854 if (namespace_decl == error_mark_node
21855 && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
21856 hint = suggest_alternative_in_explicit_scope (token->location,
21857 identifier,
21858 parser->scope);
21859 if (const char *suggestion = hint.suggestion ())
21861 gcc_rich_location richloc (token->location);
21862 richloc.add_fixit_replace (suggestion);
21863 error_at (&richloc,
21864 "%qD is not a namespace-name; did you mean %qs?",
21865 identifier, suggestion);
21867 else
21868 error_at (token->location, "%qD is not a namespace-name",
21869 identifier);
21871 else
21872 cp_parser_error (parser, "expected namespace-name");
21873 namespace_decl = error_mark_node;
21876 return namespace_decl;
21879 /* Parse a namespace-definition.
21881 namespace-definition:
21882 named-namespace-definition
21883 unnamed-namespace-definition
21885 named-namespace-definition:
21886 original-namespace-definition
21887 extension-namespace-definition
21889 original-namespace-definition:
21890 namespace identifier { namespace-body }
21892 extension-namespace-definition:
21893 namespace original-namespace-name { namespace-body }
21895 unnamed-namespace-definition:
21896 namespace { namespace-body } */
21898 static void
21899 cp_parser_namespace_definition (cp_parser* parser)
21901 tree identifier;
21902 int nested_definition_count = 0;
21904 cp_ensure_no_omp_declare_simd (parser);
21905 cp_ensure_no_oacc_routine (parser);
21907 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
21908 const bool topmost_inline_p = is_inline;
21910 if (is_inline)
21912 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
21913 cp_lexer_consume_token (parser->lexer);
21916 /* Look for the `namespace' keyword. */
21917 cp_token* token
21918 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
21920 /* Parse any specified attributes before the identifier. */
21921 tree attribs = cp_parser_attributes_opt (parser);
21923 for (;;)
21925 identifier = NULL_TREE;
21927 bool nested_inline_p = cp_lexer_next_token_is_keyword (parser->lexer,
21928 RID_INLINE);
21929 if (nested_inline_p && nested_definition_count != 0)
21931 if (pedantic && cxx_dialect < cxx20)
21932 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
21933 OPT_Wc__20_extensions, "nested inline namespace "
21934 "definitions only available with %<-std=c++20%> or "
21935 "%<-std=gnu++20%>");
21936 cp_lexer_consume_token (parser->lexer);
21939 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21941 identifier = cp_parser_identifier (parser);
21943 if (cp_next_tokens_can_be_std_attribute_p (parser))
21944 pedwarn (input_location, OPT_Wpedantic,
21945 "standard attributes on namespaces must precede "
21946 "the namespace name");
21948 /* Parse any attributes specified after the identifier. */
21949 attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
21952 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
21954 /* Don't forget that the innermost namespace might have been
21955 marked as inline. Use |= because we cannot overwrite
21956 IS_INLINE in case the outermost namespace is inline, but
21957 there are no nested inlines. */
21958 is_inline |= nested_inline_p;
21959 break;
21962 if (!nested_definition_count && pedantic && cxx_dialect < cxx17)
21963 pedwarn (input_location, OPT_Wc__17_extensions,
21964 "nested namespace definitions only available with "
21965 "%<-std=c++17%> or %<-std=gnu++17%>");
21967 /* Nested namespace names can create new namespaces (unlike
21968 other qualified-ids). */
21969 if (int count = (identifier
21970 ? push_namespace (identifier, nested_inline_p)
21971 : 0))
21972 nested_definition_count += count;
21973 else
21974 cp_parser_error (parser, "nested namespace name required");
21975 cp_lexer_consume_token (parser->lexer);
21978 if (nested_definition_count && !identifier)
21979 cp_parser_error (parser, "namespace name required");
21981 if (nested_definition_count && attribs)
21982 error_at (token->location,
21983 "a nested namespace definition cannot have attributes");
21984 if (nested_definition_count && topmost_inline_p)
21985 error_at (token->location,
21986 "a nested namespace definition cannot be inline");
21988 /* Start the namespace. */
21989 nested_definition_count += push_namespace (identifier, is_inline);
21991 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
21993 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
21995 /* Look for the `{' to validate starting the namespace. */
21996 matching_braces braces;
21997 if (braces.require_open (parser))
21999 /* Parse the body of the namespace. */
22000 cp_parser_namespace_body (parser);
22002 /* Look for the final `}'. */
22003 braces.require_close (parser);
22006 if (has_visibility)
22007 pop_visibility (1);
22009 /* Pop the nested namespace definitions. */
22010 while (nested_definition_count--)
22011 pop_namespace ();
22014 /* Parse a namespace-body.
22016 namespace-body:
22017 declaration-seq [opt] */
22019 static void
22020 cp_parser_namespace_body (cp_parser* parser)
22022 cp_parser_declaration_seq_opt (parser);
22025 /* Parse a namespace-alias-definition.
22027 namespace-alias-definition:
22028 namespace identifier = qualified-namespace-specifier ; */
22030 static void
22031 cp_parser_namespace_alias_definition (cp_parser* parser)
22033 tree identifier;
22034 tree namespace_specifier;
22036 cp_token *token = cp_lexer_peek_token (parser->lexer);
22038 /* Look for the `namespace' keyword. */
22039 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
22040 /* Look for the identifier. */
22041 identifier = cp_parser_identifier (parser);
22042 if (identifier == error_mark_node)
22043 return;
22044 /* Look for the `=' token. */
22045 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
22046 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22048 error_at (token->location, "%<namespace%> definition is not allowed here");
22049 /* Skip the definition. */
22050 cp_lexer_consume_token (parser->lexer);
22051 if (cp_parser_skip_to_closing_brace (parser))
22052 cp_lexer_consume_token (parser->lexer);
22053 return;
22055 cp_parser_require (parser, CPP_EQ, RT_EQ);
22056 /* Look for the qualified-namespace-specifier. */
22057 namespace_specifier
22058 = cp_parser_qualified_namespace_specifier (parser);
22059 cp_warn_deprecated_use_scopes (namespace_specifier);
22060 /* Look for the `;' token. */
22061 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22063 /* Register the alias in the symbol table. */
22064 do_namespace_alias (identifier, namespace_specifier);
22067 /* Parse a qualified-namespace-specifier.
22069 qualified-namespace-specifier:
22070 :: [opt] nested-name-specifier [opt] namespace-name
22072 Returns a NAMESPACE_DECL corresponding to the specified
22073 namespace. */
22075 static tree
22076 cp_parser_qualified_namespace_specifier (cp_parser* parser)
22078 /* Look for the optional `::'. */
22079 cp_parser_global_scope_opt (parser,
22080 /*current_scope_valid_p=*/false);
22082 /* Look for the optional nested-name-specifier. */
22083 cp_parser_nested_name_specifier_opt (parser,
22084 /*typename_keyword_p=*/false,
22085 /*check_dependency_p=*/true,
22086 /*type_p=*/false,
22087 /*is_declaration=*/true);
22089 return cp_parser_namespace_name (parser);
22092 /* Subroutine of cp_parser_using_declaration. */
22094 static tree
22095 finish_using_decl (tree qscope, tree identifier, bool typename_p = false)
22097 tree decl = NULL_TREE;
22098 if (at_class_scope_p ())
22100 /* Create the USING_DECL. */
22101 decl = do_class_using_decl (qscope, identifier);
22103 if (check_for_bare_parameter_packs (decl))
22104 return error_mark_node;
22106 if (decl && typename_p)
22107 USING_DECL_TYPENAME_P (decl) = 1;
22109 /* Add it to the list of members in this class. */
22110 finish_member_declaration (decl);
22112 else
22113 finish_nonmember_using_decl (qscope, identifier);
22114 return decl;
22117 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
22118 access declaration.
22120 using-declaration:
22121 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
22122 using :: unqualified-id ;
22124 access-declaration:
22125 qualified-id ;
22129 static bool
22130 cp_parser_using_declaration (cp_parser* parser,
22131 bool access_declaration_p)
22133 cp_token *token;
22134 bool typename_p = false;
22135 bool global_scope_p;
22136 tree identifier;
22137 tree qscope;
22138 int oldcount = errorcount;
22139 cp_token *diag_token = NULL;
22141 if (access_declaration_p)
22143 diag_token = cp_lexer_peek_token (parser->lexer);
22144 cp_parser_parse_tentatively (parser);
22146 else
22148 /* Look for the `using' keyword. */
22149 cp_parser_require_keyword (parser, RID_USING, RT_USING);
22151 again:
22152 /* Peek at the next token. */
22153 token = cp_lexer_peek_token (parser->lexer);
22154 /* See if it's `typename'. */
22155 if (token->keyword == RID_TYPENAME)
22157 /* Remember that we've seen it. */
22158 typename_p = true;
22159 /* Consume the `typename' token. */
22160 cp_lexer_consume_token (parser->lexer);
22164 /* Look for the optional global scope qualification. */
22165 global_scope_p
22166 = (cp_parser_global_scope_opt (parser,
22167 /*current_scope_valid_p=*/false)
22168 != NULL_TREE);
22170 /* If we saw `typename', or didn't see `::', then there must be a
22171 nested-name-specifier present. */
22172 if (typename_p || !global_scope_p)
22174 qscope = cp_parser_nested_name_specifier (parser, typename_p,
22175 /*check_dependency_p=*/true,
22176 /*type_p=*/false,
22177 /*is_declaration=*/true);
22178 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
22180 cp_parser_skip_to_end_of_block_or_statement (parser);
22181 return false;
22184 /* Otherwise, we could be in either of the two productions. In that
22185 case, treat the nested-name-specifier as optional. */
22186 else
22187 qscope = cp_parser_nested_name_specifier_opt (parser,
22188 /*typename_keyword_p=*/false,
22189 /*check_dependency_p=*/true,
22190 /*type_p=*/false,
22191 /*is_declaration=*/true);
22192 if (!qscope)
22193 qscope = global_namespace;
22195 cp_warn_deprecated_use_scopes (qscope);
22197 if (access_declaration_p
22198 && !MAYBE_CLASS_TYPE_P (qscope)
22199 && TREE_CODE (qscope) != ENUMERAL_TYPE)
22200 /* If the qualifying scope of an access-declaration isn't a class
22201 or enumeration type then it can't be valid. */
22202 cp_parser_simulate_error (parser);
22204 if (access_declaration_p && cp_parser_error_occurred (parser))
22205 /* Something has already gone wrong; there's no need to parse
22206 further. Since an error has occurred, the return value of
22207 cp_parser_parse_definitely will be false, as required. */
22208 return cp_parser_parse_definitely (parser);
22210 token = cp_lexer_peek_token (parser->lexer);
22211 /* Parse the unqualified-id. */
22212 identifier = cp_parser_unqualified_id (parser,
22213 /*template_keyword_p=*/false,
22214 /*check_dependency_p=*/true,
22215 /*declarator_p=*/true,
22216 /*optional_p=*/false);
22218 if (access_declaration_p)
22220 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22221 cp_parser_simulate_error (parser);
22222 if (!cp_parser_parse_definitely (parser))
22223 return false;
22225 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22227 cp_token *ell = cp_lexer_consume_token (parser->lexer);
22228 if (cxx_dialect < cxx17)
22229 pedwarn (ell->location, OPT_Wc__17_extensions,
22230 "pack expansion in using-declaration only available "
22231 "with %<-std=c++17%> or %<-std=gnu++17%>");
22233 /* A parameter pack can appear in the qualifying scope, and/or in the
22234 terminal name (if naming a conversion function). Logically they're
22235 part of a single pack expansion of the overall USING_DECL, but we
22236 express them as separate pack expansions within the USING_DECL since
22237 we can't create a pack expansion over a USING_DECL. */
22238 bool saw_parm_pack = false;
22239 if (uses_parameter_packs (qscope))
22241 qscope = make_pack_expansion (qscope);
22242 saw_parm_pack = true;
22244 if (identifier_p (identifier)
22245 && IDENTIFIER_CONV_OP_P (identifier)
22246 && uses_parameter_packs (TREE_TYPE (identifier)))
22248 identifier = make_conv_op_name (make_pack_expansion
22249 (TREE_TYPE (identifier)));
22250 saw_parm_pack = true;
22252 if (!saw_parm_pack)
22254 /* Issue an error in terms using a SCOPE_REF that includes both
22255 components. */
22256 tree name
22257 = build_qualified_name (NULL_TREE, qscope, identifier, false);
22258 make_pack_expansion (name);
22259 gcc_assert (seen_error ());
22260 qscope = identifier = error_mark_node;
22264 /* The function we call to handle a using-declaration is different
22265 depending on what scope we are in. */
22266 if (qscope == error_mark_node || identifier == error_mark_node)
22268 else if (!identifier_p (identifier)
22269 && TREE_CODE (identifier) != BIT_NOT_EXPR)
22270 /* [namespace.udecl]
22272 A using declaration shall not name a template-id. */
22273 error_at (token->location,
22274 "a template-id may not appear in a using-declaration");
22275 else
22277 tree decl = finish_using_decl (qscope, identifier, typename_p);
22279 if (decl == error_mark_node)
22281 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22282 return false;
22286 if (!access_declaration_p
22287 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22289 cp_token *comma = cp_lexer_consume_token (parser->lexer);
22290 if (cxx_dialect < cxx17)
22291 pedwarn (comma->location, OPT_Wc__17_extensions,
22292 "comma-separated list in using-declaration only available "
22293 "with %<-std=c++17%> or %<-std=gnu++17%>");
22294 goto again;
22297 /* Look for the final `;'. */
22298 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22300 if (access_declaration_p && errorcount == oldcount)
22301 warning_at (diag_token->location, OPT_Wdeprecated,
22302 "access declarations are deprecated "
22303 "in favour of using-declarations; "
22304 "suggestion: add the %<using%> keyword");
22306 return true;
22309 /* C++20 using enum declaration.
22311 using-enum-declaration :
22312 using elaborated-enum-specifier ; */
22314 static void
22315 cp_parser_using_enum (cp_parser *parser)
22317 cp_parser_require_keyword (parser, RID_USING, RT_USING);
22319 /* Using cp_parser_elaborated_type_specifier rejects typedef-names, which
22320 breaks one of the motivating examples in using-enum-5.C.
22321 cp_parser_simple_type_specifier seems to be closer to what we actually
22322 want, though that hasn't been properly specified yet. */
22324 /* Consume 'enum'. */
22325 gcc_checking_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM));
22326 cp_lexer_consume_token (parser->lexer);
22328 cp_token *start = cp_lexer_peek_token (parser->lexer);
22330 tree type = (cp_parser_simple_type_specifier
22331 (parser, NULL, CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
22333 cp_token *end = cp_lexer_previous_token (parser->lexer);
22335 if (type == error_mark_node
22336 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
22338 cp_parser_skip_to_end_of_block_or_statement (parser);
22339 return;
22341 if (TREE_CODE (type) == TYPE_DECL)
22342 type = TREE_TYPE (type);
22344 /* The elaborated-enum-specifier shall not name a dependent type and the type
22345 shall have a reachable enum-specifier. */
22346 const char *msg = nullptr;
22347 if (cxx_dialect < cxx20)
22348 msg = G_("%<using enum%> "
22349 "only available with %<-std=c++20%> or %<-std=gnu++20%>");
22350 else if (dependent_type_p (type))
22351 msg = G_("%<using enum%> of dependent type %qT");
22352 else if (TREE_CODE (type) != ENUMERAL_TYPE)
22353 msg = G_("%<using enum%> of non-enumeration type %q#T");
22354 else if (!COMPLETE_TYPE_P (type))
22355 msg = G_("%<using enum%> of incomplete type %qT");
22356 else if (OPAQUE_ENUM_P (type))
22357 msg = G_("%<using enum%> of %qT before its enum-specifier");
22358 if (msg)
22360 location_t loc = make_location (start, start, end);
22361 auto_diagnostic_group g;
22362 error_at (loc, msg, type);
22363 loc = location_of (type);
22364 if (cxx_dialect < cxx20 || loc == input_location)
22366 else if (OPAQUE_ENUM_P (type))
22367 inform (loc, "opaque-enum-declaration here");
22368 else
22369 inform (loc, "declared here");
22372 /* A using-enum-declaration introduces the enumerator names of the named
22373 enumeration as if by a using-declaration for each enumerator. */
22374 if (TREE_CODE (type) == ENUMERAL_TYPE)
22375 for (tree v = TYPE_VALUES (type); v; v = TREE_CHAIN (v))
22376 finish_using_decl (type, DECL_NAME (TREE_VALUE (v)));
22379 /* Parse an alias-declaration.
22381 alias-declaration:
22382 using identifier attribute-specifier-seq [opt] = type-id */
22384 static tree
22385 cp_parser_alias_declaration (cp_parser* parser)
22387 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
22388 location_t id_location, type_location;
22389 cp_declarator *declarator;
22390 cp_decl_specifier_seq decl_specs;
22391 bool member_p;
22392 const char *saved_message = NULL;
22394 /* Look for the `using' keyword. */
22395 cp_token *using_token
22396 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
22397 if (using_token == NULL)
22398 return error_mark_node;
22400 id_location = cp_lexer_peek_token (parser->lexer)->location;
22401 id = cp_parser_identifier (parser);
22402 if (id == error_mark_node)
22403 return error_mark_node;
22405 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
22406 attributes = cp_parser_attributes_opt (parser);
22407 if (attributes == error_mark_node)
22408 return error_mark_node;
22410 cp_parser_require (parser, CPP_EQ, RT_EQ);
22412 if (cp_parser_error_occurred (parser))
22413 return error_mark_node;
22415 cp_parser_commit_to_tentative_parse (parser);
22417 /* Now we are going to parse the type-id of the declaration. */
22420 [dcl.type]/3 says:
22422 "A type-specifier-seq shall not define a class or enumeration
22423 unless it appears in the type-id of an alias-declaration (7.1.3) that
22424 is not the declaration of a template-declaration."
22426 In other words, if we currently are in an alias template, the
22427 type-id should not define a type.
22429 So let's set parser->type_definition_forbidden_message in that
22430 case; cp_parser_check_type_definition (called by
22431 cp_parser_class_specifier) will then emit an error if a type is
22432 defined in the type-id. */
22433 if (parser->num_template_parameter_lists)
22435 saved_message = parser->type_definition_forbidden_message;
22436 parser->type_definition_forbidden_message =
22437 G_("types may not be defined in alias template declarations");
22440 type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
22441 &type_location);
22443 /* Restore the error message if need be. */
22444 if (parser->num_template_parameter_lists)
22445 parser->type_definition_forbidden_message = saved_message;
22447 if (type == error_mark_node
22448 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
22450 cp_parser_skip_to_end_of_block_or_statement (parser);
22451 return error_mark_node;
22454 /* A typedef-name can also be introduced by an alias-declaration. The
22455 identifier following the using keyword becomes a typedef-name. It has
22456 the same semantics as if it were introduced by the typedef
22457 specifier. In particular, it does not define a new type and it shall
22458 not appear in the type-id. */
22460 clear_decl_specs (&decl_specs);
22461 decl_specs.type = type;
22462 if (attributes != NULL_TREE)
22464 decl_specs.attributes = attributes;
22465 set_and_check_decl_spec_loc (&decl_specs,
22466 ds_attribute,
22467 attrs_token);
22469 set_and_check_decl_spec_loc (&decl_specs,
22470 ds_typedef,
22471 using_token);
22472 set_and_check_decl_spec_loc (&decl_specs,
22473 ds_alias,
22474 using_token);
22475 decl_specs.locations[ds_type_spec] = type_location;
22477 if (parser->num_template_parameter_lists
22478 && !cp_parser_check_template_parameters (parser,
22479 /*num_templates=*/0,
22480 /*template_id*/false,
22481 id_location,
22482 /*declarator=*/NULL))
22483 return error_mark_node;
22485 declarator = make_id_declarator (NULL_TREE, id, sfk_none, id_location);
22487 member_p = at_class_scope_p ();
22488 if (member_p)
22489 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
22490 NULL_TREE, attributes);
22491 else
22492 decl = start_decl (declarator, &decl_specs, 0,
22493 attributes, NULL_TREE, &pushed_scope);
22494 if (decl == error_mark_node)
22495 return decl;
22497 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
22499 if (pushed_scope)
22500 pop_scope (pushed_scope);
22502 /* If decl is a template, return its TEMPLATE_DECL so that it gets
22503 added into the symbol table; otherwise, return the TYPE_DECL. */
22504 if (DECL_LANG_SPECIFIC (decl)
22505 && DECL_TEMPLATE_INFO (decl)
22506 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
22508 decl = DECL_TI_TEMPLATE (decl);
22509 if (member_p)
22510 check_member_template (decl);
22513 return decl;
22516 /* Parse a using-directive.
22518 using-directive:
22519 attribute-specifier-seq [opt] using namespace :: [opt]
22520 nested-name-specifier [opt] namespace-name ; */
22522 static void
22523 cp_parser_using_directive (cp_parser* parser)
22525 tree namespace_decl;
22526 tree attribs = cp_parser_std_attribute_spec_seq (parser);
22527 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22529 /* Error during attribute parsing that resulted in skipping
22530 to next semicolon. */
22531 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22532 return;
22535 /* Look for the `using' keyword. */
22536 cp_parser_require_keyword (parser, RID_USING, RT_USING);
22537 /* And the `namespace' keyword. */
22538 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
22539 /* Look for the optional `::' operator. */
22540 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
22541 /* And the optional nested-name-specifier. */
22542 cp_parser_nested_name_specifier_opt (parser,
22543 /*typename_keyword_p=*/false,
22544 /*check_dependency_p=*/true,
22545 /*type_p=*/false,
22546 /*is_declaration=*/true);
22547 /* Get the namespace being used. */
22548 namespace_decl = cp_parser_namespace_name (parser);
22549 cp_warn_deprecated_use_scopes (namespace_decl);
22550 /* And any specified GNU attributes. */
22551 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
22552 attribs = chainon (attribs, cp_parser_gnu_attributes_opt (parser));
22554 /* Update the symbol table. */
22555 finish_using_directive (namespace_decl, attribs);
22557 /* Look for the final `;'. */
22558 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22561 /* Parse an asm-definition.
22563 asm-qualifier:
22564 volatile
22565 inline
22566 goto
22568 asm-qualifier-list:
22569 asm-qualifier
22570 asm-qualifier-list asm-qualifier
22572 asm-definition:
22573 asm ( string-literal ) ;
22575 GNU Extension:
22577 asm-definition:
22578 asm asm-qualifier-list [opt] ( string-literal ) ;
22579 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt] ) ;
22580 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
22581 : asm-operand-list [opt] ) ;
22582 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
22583 : asm-operand-list [opt]
22584 : asm-clobber-list [opt] ) ;
22585 asm asm-qualifier-list [opt] ( string-literal : : asm-operand-list [opt]
22586 : asm-clobber-list [opt]
22587 : asm-goto-list ) ;
22589 The form with asm-goto-list is valid if and only if the asm-qualifier-list
22590 contains goto, and is the only allowed form in that case. No duplicates are
22591 allowed in an asm-qualifier-list. */
22593 static void
22594 cp_parser_asm_definition (cp_parser* parser)
22596 tree outputs = NULL_TREE;
22597 tree inputs = NULL_TREE;
22598 tree clobbers = NULL_TREE;
22599 tree labels = NULL_TREE;
22600 tree asm_stmt;
22601 bool extended_p = false;
22602 bool invalid_inputs_p = false;
22603 bool invalid_outputs_p = false;
22604 required_token missing = RT_NONE;
22605 tree std_attrs = cp_parser_std_attribute_spec_seq (parser);
22606 location_t asm_loc = cp_lexer_peek_token (parser->lexer)->location;
22608 /* Look for the `asm' keyword. */
22609 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
22611 /* In C++20, unevaluated inline assembly is permitted in constexpr
22612 functions. */
22613 if (parser->in_function_body
22614 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
22615 && cxx_dialect < cxx20)
22616 pedwarn (asm_loc, OPT_Wc__20_extensions, "%<asm%> in %<constexpr%> "
22617 "function only available with %<-std=c++20%> or "
22618 "%<-std=gnu++20%>");
22620 /* Handle the asm-qualifier-list. */
22621 location_t volatile_loc = UNKNOWN_LOCATION;
22622 location_t inline_loc = UNKNOWN_LOCATION;
22623 location_t goto_loc = UNKNOWN_LOCATION;
22624 location_t first_loc = UNKNOWN_LOCATION;
22626 if (cp_parser_allow_gnu_extensions_p (parser))
22627 for (;;)
22629 cp_token *token = cp_lexer_peek_token (parser->lexer);
22630 location_t loc = token->location;
22631 switch (cp_lexer_peek_token (parser->lexer)->keyword)
22633 case RID_VOLATILE:
22634 if (volatile_loc)
22636 error_at (loc, "duplicate %<asm%> qualifier %qT",
22637 token->u.value);
22638 inform (volatile_loc, "first seen here");
22640 else
22642 if (!parser->in_function_body)
22643 warning_at (loc, 0, "%<asm%> qualifier %qT ignored "
22644 "outside of function body", token->u.value);
22645 volatile_loc = loc;
22647 cp_lexer_consume_token (parser->lexer);
22648 continue;
22650 case RID_INLINE:
22651 if (inline_loc)
22653 error_at (loc, "duplicate %<asm%> qualifier %qT",
22654 token->u.value);
22655 inform (inline_loc, "first seen here");
22657 else
22658 inline_loc = loc;
22659 if (!first_loc)
22660 first_loc = loc;
22661 cp_lexer_consume_token (parser->lexer);
22662 continue;
22664 case RID_GOTO:
22665 if (goto_loc)
22667 error_at (loc, "duplicate %<asm%> qualifier %qT",
22668 token->u.value);
22669 inform (goto_loc, "first seen here");
22671 else
22672 goto_loc = loc;
22673 if (!first_loc)
22674 first_loc = loc;
22675 cp_lexer_consume_token (parser->lexer);
22676 continue;
22678 case RID_CONST:
22679 case RID_RESTRICT:
22680 error_at (loc, "%qT is not an %<asm%> qualifier", token->u.value);
22681 cp_lexer_consume_token (parser->lexer);
22682 continue;
22684 default:
22685 break;
22687 break;
22690 bool volatile_p = (volatile_loc != UNKNOWN_LOCATION);
22691 bool inline_p = (inline_loc != UNKNOWN_LOCATION);
22692 bool goto_p = (goto_loc != UNKNOWN_LOCATION);
22694 if (!parser->in_function_body && (inline_p || goto_p))
22696 error_at (first_loc, "%<asm%> qualifier outside of function body");
22697 inline_p = goto_p = false;
22700 /* Look for the opening `('. */
22701 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22702 return;
22703 /* Look for the string. */
22704 tree string = cp_parser_string_literal (parser, /*translate=*/false,
22705 /*wide_ok=*/false);
22706 if (string == error_mark_node)
22708 cp_parser_skip_to_closing_parenthesis (parser, true, false,
22709 /*consume_paren=*/true);
22710 return;
22713 /* If we're allowing GNU extensions, check for the extended assembly
22714 syntax. Unfortunately, the `:' tokens need not be separated by
22715 a space in C, and so, for compatibility, we tolerate that here
22716 too. Doing that means that we have to treat the `::' operator as
22717 two `:' tokens. */
22718 if (cp_parser_allow_gnu_extensions_p (parser)
22719 && parser->in_function_body
22720 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
22721 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
22723 bool inputs_p = false;
22724 bool clobbers_p = false;
22725 bool labels_p = false;
22727 /* The extended syntax was used. */
22728 extended_p = true;
22730 /* Look for outputs. */
22731 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22733 /* Consume the `:'. */
22734 cp_lexer_consume_token (parser->lexer);
22735 /* Parse the output-operands. */
22736 if (cp_lexer_next_token_is_not (parser->lexer,
22737 CPP_COLON)
22738 && cp_lexer_next_token_is_not (parser->lexer,
22739 CPP_SCOPE)
22740 && cp_lexer_next_token_is_not (parser->lexer,
22741 CPP_CLOSE_PAREN))
22743 outputs = cp_parser_asm_operand_list (parser);
22744 if (outputs == error_mark_node)
22745 invalid_outputs_p = true;
22748 /* If the next token is `::', there are no outputs, and the
22749 next token is the beginning of the inputs. */
22750 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22751 /* The inputs are coming next. */
22752 inputs_p = true;
22754 /* Look for inputs. */
22755 if (inputs_p
22756 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22758 /* Consume the `:' or `::'. */
22759 cp_lexer_consume_token (parser->lexer);
22760 /* Parse the output-operands. */
22761 if (cp_lexer_next_token_is_not (parser->lexer,
22762 CPP_COLON)
22763 && cp_lexer_next_token_is_not (parser->lexer,
22764 CPP_SCOPE)
22765 && cp_lexer_next_token_is_not (parser->lexer,
22766 CPP_CLOSE_PAREN))
22768 inputs = cp_parser_asm_operand_list (parser);
22769 if (inputs == error_mark_node)
22770 invalid_inputs_p = true;
22773 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22774 /* The clobbers are coming next. */
22775 clobbers_p = true;
22777 /* Look for clobbers. */
22778 if (clobbers_p
22779 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22781 clobbers_p = true;
22782 /* Consume the `:' or `::'. */
22783 cp_lexer_consume_token (parser->lexer);
22784 /* Parse the clobbers. */
22785 if (cp_lexer_next_token_is_not (parser->lexer,
22786 CPP_COLON)
22787 && cp_lexer_next_token_is_not (parser->lexer,
22788 CPP_CLOSE_PAREN))
22789 clobbers = cp_parser_asm_clobber_list (parser);
22791 else if (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22792 /* The labels are coming next. */
22793 labels_p = true;
22795 /* Look for labels. */
22796 if (labels_p
22797 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
22799 labels_p = true;
22800 /* Consume the `:' or `::'. */
22801 cp_lexer_consume_token (parser->lexer);
22802 /* Parse the labels. */
22803 labels = cp_parser_asm_label_list (parser);
22806 if (goto_p && !labels_p)
22807 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
22809 else if (goto_p)
22810 missing = RT_COLON_SCOPE;
22812 /* Look for the closing `)'. */
22813 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
22814 missing ? missing : RT_CLOSE_PAREN))
22815 cp_parser_skip_to_closing_parenthesis (parser, true, false,
22816 /*consume_paren=*/true);
22817 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22819 if (!invalid_inputs_p && !invalid_outputs_p)
22821 /* Create the ASM_EXPR. */
22822 if (parser->in_function_body)
22824 asm_stmt = finish_asm_stmt (asm_loc, volatile_p, string, outputs,
22825 inputs, clobbers, labels, inline_p);
22826 /* If the extended syntax was not used, mark the ASM_EXPR. */
22827 if (!extended_p)
22829 tree temp = asm_stmt;
22830 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
22831 temp = TREE_OPERAND (temp, 0);
22833 ASM_INPUT_P (temp) = 1;
22836 else
22837 symtab->finalize_toplevel_asm (string);
22840 if (std_attrs && any_nonignored_attribute_p (std_attrs))
22841 warning_at (asm_loc, OPT_Wattributes,
22842 "attributes ignored on %<asm%> declaration");
22845 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
22846 type that comes from the decl-specifier-seq. */
22848 static tree
22849 strip_declarator_types (tree type, cp_declarator *declarator)
22851 for (cp_declarator *d = declarator; d;)
22852 switch (d->kind)
22854 case cdk_id:
22855 case cdk_decomp:
22856 case cdk_error:
22857 d = NULL;
22858 break;
22860 default:
22861 if (TYPE_PTRMEMFUNC_P (type))
22862 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
22863 type = TREE_TYPE (type);
22864 d = d->declarator;
22865 break;
22868 return type;
22871 /* Warn about the most vexing parse syntactic ambiguity, i.e., warn when
22872 a construct looks like a variable definition but is actually a function
22873 declaration. DECL_SPECIFIERS is the decl-specifier-seq and DECLARATOR
22874 is the declarator for this function declaration. */
22876 static void
22877 warn_about_ambiguous_parse (const cp_decl_specifier_seq *decl_specifiers,
22878 const cp_declarator *declarator)
22880 /* Only warn if we are declaring a function at block scope. */
22881 if (!at_function_scope_p ())
22882 return;
22884 /* And only if there is no storage class specified. */
22885 if (decl_specifiers->storage_class != sc_none
22886 || decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
22887 return;
22889 if (declarator->kind != cdk_function
22890 || !declarator->declarator
22891 || declarator->declarator->kind != cdk_id
22892 || !identifier_p (get_unqualified_id
22893 (const_cast<cp_declarator *>(declarator))))
22894 return;
22896 /* Don't warn when the whole declarator (not just the declarator-id!)
22897 was parenthesized. That is, don't warn for int(n()) but do warn
22898 for int(f)(). */
22899 if (declarator->parenthesized != UNKNOWN_LOCATION)
22900 return;
22902 tree type;
22903 if (decl_specifiers->type)
22905 type = decl_specifiers->type;
22906 if (TREE_CODE (type) == TYPE_DECL)
22907 type = TREE_TYPE (type);
22909 /* If the return type is void there is no ambiguity. */
22910 if (same_type_p (type, void_type_node))
22911 return;
22913 else if (decl_specifiers->any_type_specifiers_p)
22914 /* Code like long f(); will have null ->type. If we have any
22915 type-specifiers, pretend we've seen int. */
22916 type = integer_type_node;
22917 else
22918 return;
22920 auto_diagnostic_group d;
22921 location_t loc = declarator->u.function.parens_loc;
22922 tree params = declarator->u.function.parameters;
22923 const bool has_list_ctor_p = CLASS_TYPE_P (type) && TYPE_HAS_LIST_CTOR (type);
22925 /* The T t() case. */
22926 if (params == void_list_node)
22928 if (warning_at (loc, OPT_Wvexing_parse,
22929 "empty parentheses were disambiguated as a function "
22930 "declaration"))
22932 /* () means value-initialization (C++03 and up); {} (C++11 and up)
22933 means value-initialization or aggregate-initialization, nothing
22934 means default-initialization. We can only suggest removing the
22935 parentheses/adding {} if T has a default constructor. */
22936 if (!CLASS_TYPE_P (type) || TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
22938 gcc_rich_location iloc (loc);
22939 iloc.add_fixit_remove ();
22940 inform (&iloc, "remove parentheses to default-initialize "
22941 "a variable");
22942 if (cxx_dialect >= cxx11 && !has_list_ctor_p)
22944 if (CP_AGGREGATE_TYPE_P (type))
22945 inform (loc, "or replace parentheses with braces to "
22946 "aggregate-initialize a variable");
22947 else
22948 inform (loc, "or replace parentheses with braces to "
22949 "value-initialize a variable");
22953 return;
22956 /* If we had (...) or the parameter-list wasn't parenthesized,
22957 we're done. */
22958 if (params == NULL_TREE || !PARENTHESIZED_LIST_P (params))
22959 return;
22961 /* The T t(X()) case. */
22962 if (list_length (params) == 2)
22964 if (warning_at (loc, OPT_Wvexing_parse,
22965 "parentheses were disambiguated as a function "
22966 "declaration"))
22968 gcc_rich_location iloc (loc);
22969 /* {}-initialization means that we can use an initializer-list
22970 constructor if no default constructor is available, so don't
22971 suggest using {} for classes that have an initializer_list
22972 constructor. */
22973 if (cxx_dialect >= cxx11 && !has_list_ctor_p)
22975 iloc.add_fixit_replace (get_start (loc), "{");
22976 iloc.add_fixit_replace (get_finish (loc), "}");
22977 inform (&iloc, "replace parentheses with braces to declare a "
22978 "variable");
22980 else
22982 iloc.add_fixit_insert_after (get_start (loc), "(");
22983 iloc.add_fixit_insert_before (get_finish (loc), ")");
22984 inform (&iloc, "add parentheses to declare a variable");
22988 /* The T t(X(), X()) case. */
22989 else if (warning_at (loc, OPT_Wvexing_parse,
22990 "parentheses were disambiguated as a function "
22991 "declaration"))
22993 gcc_rich_location iloc (loc);
22994 if (cxx_dialect >= cxx11 && !has_list_ctor_p)
22996 iloc.add_fixit_replace (get_start (loc), "{");
22997 iloc.add_fixit_replace (get_finish (loc), "}");
22998 inform (&iloc, "replace parentheses with braces to declare a "
22999 "variable");
23004 /* If DECLARATOR with DECL_SPECS is a function declarator that has
23005 the form of a deduction guide, tag it as such. CTOR_DTOR_OR_CONV_P
23006 has the same meaning as in cp_parser_declarator. */
23008 static void
23009 cp_parser_maybe_adjust_declarator_for_dguide (cp_parser *parser,
23010 cp_decl_specifier_seq *decl_specs,
23011 cp_declarator *declarator,
23012 int *ctor_dtor_or_conv_p)
23014 if (cxx_dialect >= cxx17
23015 && *ctor_dtor_or_conv_p <= 0
23016 && !decl_specs->type
23017 && !decl_specs->any_type_specifiers_p
23018 && function_declarator_p (declarator))
23020 cp_declarator *id = get_id_declarator (declarator);
23021 tree name = id->u.id.unqualified_name;
23022 parser->scope = id->u.id.qualifying_scope;
23023 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
23024 if (tmpl
23025 && (DECL_CLASS_TEMPLATE_P (tmpl)
23026 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
23028 id->u.id.unqualified_name = dguide_name (tmpl);
23029 id->u.id.sfk = sfk_deduction_guide;
23030 *ctor_dtor_or_conv_p = 1;
23035 /* Declarators [gram.dcl.decl] */
23037 /* Parse an init-declarator.
23039 init-declarator:
23040 declarator initializer [opt]
23042 GNU Extension:
23044 init-declarator:
23045 declarator asm-specification [opt] attributes [opt] initializer [opt]
23047 function-definition:
23048 decl-specifier-seq [opt] declarator ctor-initializer [opt]
23049 function-body
23050 decl-specifier-seq [opt] declarator function-try-block
23052 GNU Extension:
23054 function-definition:
23055 __extension__ function-definition
23057 TM Extension:
23059 function-definition:
23060 decl-specifier-seq [opt] declarator function-transaction-block
23062 The parser flags FLAGS is used to control type-specifier parsing.
23064 The DECL_SPECIFIERS apply to this declarator. Returns a
23065 representation of the entity declared. If MEMBER_P is TRUE, then
23066 this declarator appears in a class scope. The new DECL created by
23067 this declarator is returned.
23069 The CHECKS are access checks that should be performed once we know
23070 what entity is being declared (and, therefore, what classes have
23071 befriended it).
23073 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
23074 for a function-definition here as well. If the declarator is a
23075 declarator for a function-definition, *FUNCTION_DEFINITION_P will
23076 be TRUE upon return. By that point, the function-definition will
23077 have been completely parsed.
23079 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
23080 is FALSE.
23082 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
23083 parsed declaration if it is an uninitialized single declarator not followed
23084 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
23085 if present, will not be consumed. If returned, this declarator will be
23086 created with SD_INITIALIZED but will not call cp_finish_decl.
23088 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
23089 and there is an initializer, the pointed location_t is set to the
23090 location of the '=' or `(', or '{' in C++11 token introducing the
23091 initializer. */
23093 static tree
23094 cp_parser_init_declarator (cp_parser* parser,
23095 cp_parser_flags flags,
23096 cp_decl_specifier_seq *decl_specifiers,
23097 vec<deferred_access_check, va_gc> *checks,
23098 bool function_definition_allowed_p,
23099 bool member_p,
23100 int declares_class_or_enum,
23101 bool* function_definition_p,
23102 tree* maybe_range_for_decl,
23103 location_t* init_loc,
23104 tree* auto_result)
23106 cp_token *token = NULL, *asm_spec_start_token = NULL,
23107 *attributes_start_token = NULL;
23108 cp_declarator *declarator;
23109 tree prefix_attributes;
23110 tree attributes = NULL;
23111 tree asm_specification;
23112 tree initializer;
23113 tree decl = NULL_TREE;
23114 tree scope;
23115 int is_initialized;
23116 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
23117 initialized with "= ..", CPP_OPEN_PAREN if initialized with
23118 "(...)". */
23119 enum cpp_ttype initialization_kind;
23120 bool is_direct_init = false;
23121 bool is_non_constant_init;
23122 int ctor_dtor_or_conv_p;
23123 bool friend_p = cp_parser_friend_p (decl_specifiers);
23124 bool static_p = decl_specifiers->storage_class == sc_static;
23125 tree pushed_scope = NULL_TREE;
23126 bool range_for_decl_p = false;
23127 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
23128 location_t tmp_init_loc = UNKNOWN_LOCATION;
23130 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_consteval))
23131 flags |= CP_PARSER_FLAGS_CONSTEVAL;
23133 /* Assume that this is not the declarator for a function
23134 definition. */
23135 if (function_definition_p)
23136 *function_definition_p = false;
23138 /* Default arguments are only permitted for function parameters. */
23139 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
23140 parser->default_arg_ok_p = false;
23142 /* Defer access checks while parsing the declarator; we cannot know
23143 what names are accessible until we know what is being
23144 declared. */
23145 resume_deferring_access_checks ();
23147 token = cp_lexer_peek_token (parser->lexer);
23149 /* Parse the declarator. */
23150 declarator
23151 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23152 flags, &ctor_dtor_or_conv_p,
23153 /*parenthesized_p=*/NULL,
23154 member_p, friend_p, static_p);
23155 /* Gather up the deferred checks. */
23156 stop_deferring_access_checks ();
23158 parser->default_arg_ok_p = saved_default_arg_ok_p;
23160 /* If the DECLARATOR was erroneous, there's no need to go
23161 further. */
23162 if (declarator == cp_error_declarator)
23163 return error_mark_node;
23165 /* Check that the number of template-parameter-lists is OK. */
23166 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
23167 token->location))
23168 return error_mark_node;
23170 if (declares_class_or_enum & 2)
23171 cp_parser_check_for_definition_in_return_type (declarator,
23172 decl_specifiers->type,
23173 decl_specifiers->locations[ds_type_spec]);
23175 /* Figure out what scope the entity declared by the DECLARATOR is
23176 located in. `grokdeclarator' sometimes changes the scope, so
23177 we compute it now. */
23178 scope = get_scope_of_declarator (declarator);
23180 /* Perform any lookups in the declared type which were thought to be
23181 dependent, but are not in the scope of the declarator. */
23182 decl_specifiers->type
23183 = maybe_update_decl_type (decl_specifiers->type, scope);
23185 /* If we're allowing GNU extensions, look for an
23186 asm-specification. */
23187 if (cp_parser_allow_gnu_extensions_p (parser))
23189 /* Look for an asm-specification. */
23190 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
23191 asm_specification = cp_parser_asm_specification_opt (parser);
23193 else
23194 asm_specification = NULL_TREE;
23196 /* Gather the attributes that were provided with the
23197 decl-specifiers. */
23198 prefix_attributes = decl_specifiers->attributes;
23200 /* Look for attributes. */
23201 attributes_start_token = cp_lexer_peek_token (parser->lexer);
23202 attributes = cp_parser_attributes_opt (parser);
23204 /* Peek at the next token. */
23205 token = cp_lexer_peek_token (parser->lexer);
23207 bool bogus_implicit_tmpl = false;
23209 if (function_declarator_p (declarator))
23211 /* Handle C++17 deduction guides. Note that class-scope
23212 non-template deduction guides are instead handled in
23213 cp_parser_member_declaration. */
23214 cp_parser_maybe_adjust_declarator_for_dguide (parser,
23215 decl_specifiers,
23216 declarator,
23217 &ctor_dtor_or_conv_p);
23219 if (!member_p && !cp_parser_error_occurred (parser))
23220 warn_about_ambiguous_parse (decl_specifiers, declarator);
23222 /* Check to see if the token indicates the start of a
23223 function-definition. */
23224 if (cp_parser_token_starts_function_definition_p (token))
23226 if (!function_definition_allowed_p)
23228 /* If a function-definition should not appear here, issue an
23229 error message. */
23230 cp_parser_error (parser,
23231 "a function-definition is not allowed here");
23232 return error_mark_node;
23235 location_t func_brace_location
23236 = cp_lexer_peek_token (parser->lexer)->location;
23238 /* Neither attributes nor an asm-specification are allowed
23239 on a function-definition. */
23240 if (asm_specification)
23241 error_at (asm_spec_start_token->location,
23242 "an %<asm%> specification is not allowed "
23243 "on a function-definition");
23244 if (attributes)
23245 error_at (attributes_start_token->location,
23246 "attributes are not allowed "
23247 "on a function-definition");
23248 /* This is a function-definition. */
23249 *function_definition_p = true;
23251 /* Parse the function definition. */
23252 if (member_p)
23253 decl = cp_parser_save_member_function_body (parser,
23254 decl_specifiers,
23255 declarator,
23256 prefix_attributes);
23257 else
23258 decl =
23259 (cp_parser_function_definition_from_specifiers_and_declarator
23260 (parser, decl_specifiers, prefix_attributes, declarator));
23262 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
23264 /* This is where the prologue starts... */
23265 DECL_STRUCT_FUNCTION (decl)->function_start_locus
23266 = func_brace_location;
23269 return decl;
23272 else if (parser->fully_implicit_function_template_p)
23274 /* A non-template declaration involving a function parameter list
23275 containing an implicit template parameter will be made into a
23276 template. If the resulting declaration is not going to be an
23277 actual function then finish the template scope here to prevent it.
23278 An error message will be issued once we have a decl to talk about.
23280 FIXME probably we should do type deduction rather than create an
23281 implicit template, but the standard currently doesn't allow it. */
23282 bogus_implicit_tmpl = true;
23283 finish_fully_implicit_template (parser, NULL_TREE);
23286 /* [dcl.dcl]
23288 Only in function declarations for constructors, destructors, type
23289 conversions, and deduction guides can the decl-specifier-seq be omitted.
23291 We explicitly postpone this check past the point where we handle
23292 function-definitions because we tolerate function-definitions
23293 that are missing their return types in some modes. */
23294 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
23296 cp_parser_error (parser,
23297 "expected constructor, destructor, or type conversion");
23298 return error_mark_node;
23301 /* An `=' or an '{' in C++11, indicate an initializer. An '(' may indicate
23302 an initializer as well. */
23303 if (token->type == CPP_EQ
23304 || token->type == CPP_OPEN_PAREN
23305 || token->type == CPP_OPEN_BRACE)
23307 /* Don't get fooled into thinking that F(i)(1)(2) is an initializer.
23308 It isn't; it's an expression. (Here '(i)' would have already been
23309 parsed as a declarator.) */
23310 if (token->type == CPP_OPEN_PAREN
23311 && cp_parser_uncommitted_to_tentative_parse_p (parser))
23313 cp_lexer_save_tokens (parser->lexer);
23314 cp_lexer_consume_token (parser->lexer);
23315 cp_parser_skip_to_closing_parenthesis (parser,
23316 /*recovering*/false,
23317 /*or_comma*/false,
23318 /*consume_paren*/true);
23319 /* If this is an initializer, only a ',' or ';' can follow: either
23320 we have another init-declarator, or we're at the end of an
23321 init-declarator-list which can only be followed by a ';'. */
23322 bool ok = (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23323 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
23324 cp_lexer_rollback_tokens (parser->lexer);
23325 if (UNLIKELY (!ok))
23326 /* Not an init-declarator. */
23327 return error_mark_node;
23329 is_initialized = SD_INITIALIZED;
23330 initialization_kind = token->type;
23331 declarator->init_loc = token->location;
23332 if (maybe_range_for_decl)
23333 *maybe_range_for_decl = error_mark_node;
23334 tmp_init_loc = token->location;
23335 if (init_loc && *init_loc == UNKNOWN_LOCATION)
23336 *init_loc = tmp_init_loc;
23338 if (token->type == CPP_EQ
23339 && function_declarator_p (declarator))
23341 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
23342 if (t2->keyword == RID_DEFAULT)
23343 is_initialized = SD_DEFAULTED;
23344 else if (t2->keyword == RID_DELETE)
23345 is_initialized = SD_DELETED;
23348 else
23350 /* If the init-declarator isn't initialized and isn't followed by a
23351 `,' or `;', it's not a valid init-declarator. */
23352 if (token->type != CPP_COMMA
23353 && token->type != CPP_SEMICOLON)
23355 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
23356 range_for_decl_p = true;
23357 else
23359 if (!maybe_range_for_decl)
23360 cp_parser_error (parser, "expected initializer");
23361 return error_mark_node;
23364 is_initialized = SD_UNINITIALIZED;
23365 initialization_kind = CPP_EOF;
23368 /* Because start_decl has side-effects, we should only call it if we
23369 know we're going ahead. By this point, we know that we cannot
23370 possibly be looking at any other construct. */
23371 cp_parser_commit_to_tentative_parse (parser);
23373 /* Enter the newly declared entry in the symbol table. If we're
23374 processing a declaration in a class-specifier, we wait until
23375 after processing the initializer. */
23376 if (!member_p)
23378 if (parser->in_unbraced_linkage_specification_p)
23379 decl_specifiers->storage_class = sc_extern;
23380 decl = start_decl (declarator, decl_specifiers,
23381 range_for_decl_p? SD_INITIALIZED : is_initialized,
23382 attributes, prefix_attributes, &pushed_scope);
23383 cp_finalize_omp_declare_simd (parser, decl);
23384 cp_finalize_oacc_routine (parser, decl, false);
23385 /* Adjust location of decl if declarator->id_loc is more appropriate:
23386 set, and decl wasn't merged with another decl, in which case its
23387 location would be different from input_location, and more accurate. */
23388 if (DECL_P (decl)
23389 && declarator->id_loc != UNKNOWN_LOCATION
23390 && DECL_SOURCE_LOCATION (decl) == input_location)
23391 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
23393 else if (scope)
23394 /* Enter the SCOPE. That way unqualified names appearing in the
23395 initializer will be looked up in SCOPE. */
23396 pushed_scope = push_scope (scope);
23398 /* Perform deferred access control checks, now that we know in which
23399 SCOPE the declared entity resides. */
23400 if (!member_p && decl)
23402 tree saved_current_function_decl = NULL_TREE;
23404 /* If the entity being declared is a function, pretend that we
23405 are in its scope. If it is a `friend', it may have access to
23406 things that would not otherwise be accessible. */
23407 if (TREE_CODE (decl) == FUNCTION_DECL)
23409 saved_current_function_decl = current_function_decl;
23410 current_function_decl = decl;
23413 /* Perform access checks for template parameters. */
23414 cp_parser_perform_template_parameter_access_checks (checks);
23416 /* Perform the access control checks for the declarator and the
23417 decl-specifiers. */
23418 perform_deferred_access_checks (tf_warning_or_error);
23420 /* Restore the saved value. */
23421 if (TREE_CODE (decl) == FUNCTION_DECL)
23422 current_function_decl = saved_current_function_decl;
23425 /* Parse the initializer. */
23426 initializer = NULL_TREE;
23427 is_direct_init = false;
23428 is_non_constant_init = true;
23429 if (is_initialized)
23431 if (function_declarator_p (declarator))
23433 if (initialization_kind == CPP_EQ)
23434 initializer = cp_parser_pure_specifier (parser);
23435 else
23437 /* If the declaration was erroneous, we don't really
23438 know what the user intended, so just silently
23439 consume the initializer. */
23440 if (decl != error_mark_node)
23441 error_at (tmp_init_loc, "initializer provided for function");
23442 cp_parser_skip_to_closing_parenthesis (parser,
23443 /*recovering=*/true,
23444 /*or_comma=*/false,
23445 /*consume_paren=*/true);
23448 else
23450 /* We want to record the extra mangling scope for in-class
23451 initializers of class members and initializers of static
23452 data member templates and namespace-scope initializers.
23453 The former involves deferring parsing of the initializer
23454 until end of class as with default arguments. So right
23455 here we only handle the latter two. */
23456 bool has_lambda_scope = false;
23458 if (decl != error_mark_node
23459 && !member_p
23460 && (processing_template_decl || DECL_NAMESPACE_SCOPE_P (decl)))
23461 has_lambda_scope = true;
23463 if (has_lambda_scope)
23464 start_lambda_scope (decl);
23465 initializer = cp_parser_initializer (parser,
23466 &is_direct_init,
23467 &is_non_constant_init);
23468 if (has_lambda_scope)
23469 finish_lambda_scope ();
23470 if (initializer == error_mark_node)
23471 cp_parser_skip_to_end_of_statement (parser);
23475 /* The old parser allows attributes to appear after a parenthesized
23476 initializer. Mark Mitchell proposed removing this functionality
23477 on the GCC mailing lists on 2002-08-13. This parser accepts the
23478 attributes -- but ignores them. Made a permerror in GCC 8. */
23479 if (cp_parser_allow_gnu_extensions_p (parser)
23480 && initialization_kind == CPP_OPEN_PAREN
23481 && cp_parser_attributes_opt (parser)
23482 && permerror (input_location,
23483 "attributes after parenthesized initializer ignored"))
23485 static bool hint;
23486 if (flag_permissive && !hint)
23488 hint = true;
23489 inform (input_location,
23490 "this flexibility is deprecated and will be removed");
23494 /* And now complain about a non-function implicit template. */
23495 if (bogus_implicit_tmpl && decl != error_mark_node)
23496 error_at (DECL_SOURCE_LOCATION (decl),
23497 "non-function %qD declared as implicit template", decl);
23499 /* For an in-class declaration, use `grokfield' to create the
23500 declaration. */
23501 if (member_p)
23503 if (pushed_scope)
23505 pop_scope (pushed_scope);
23506 pushed_scope = NULL_TREE;
23508 decl = grokfield (declarator, decl_specifiers,
23509 initializer, !is_non_constant_init,
23510 /*asmspec=*/NULL_TREE,
23511 attr_chainon (attributes, prefix_attributes));
23512 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
23513 cp_parser_save_default_args (parser, decl);
23514 cp_finalize_omp_declare_simd (parser, decl);
23515 cp_finalize_oacc_routine (parser, decl, false);
23518 /* Finish processing the declaration. But, skip member
23519 declarations. */
23520 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
23522 cp_finish_decl (decl,
23523 initializer, !is_non_constant_init,
23524 asm_specification,
23525 /* If the initializer is in parentheses, then this is
23526 a direct-initialization, which means that an
23527 `explicit' constructor is OK. Otherwise, an
23528 `explicit' constructor cannot be used. */
23529 ((is_direct_init || !is_initialized)
23530 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
23532 else if ((cxx_dialect != cxx98) && friend_p
23533 && decl && TREE_CODE (decl) == FUNCTION_DECL)
23534 /* Core issue #226 (C++0x only): A default template-argument
23535 shall not be specified in a friend class template
23536 declaration. */
23537 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
23538 /*is_partial=*/false, /*is_friend_decl=*/1);
23540 if (!friend_p && pushed_scope)
23541 pop_scope (pushed_scope);
23543 if (function_declarator_p (declarator)
23544 && parser->fully_implicit_function_template_p)
23546 if (member_p)
23547 decl = finish_fully_implicit_template (parser, decl);
23548 else
23549 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
23552 if (auto_result && is_initialized && decl_specifiers->type
23553 && type_uses_auto (decl_specifiers->type))
23554 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
23556 return decl;
23559 /* Parse a declarator.
23561 declarator:
23562 direct-declarator
23563 ptr-operator declarator
23565 abstract-declarator:
23566 ptr-operator abstract-declarator [opt]
23567 direct-abstract-declarator
23569 GNU Extensions:
23571 declarator:
23572 attributes [opt] direct-declarator
23573 attributes [opt] ptr-operator declarator
23575 abstract-declarator:
23576 attributes [opt] ptr-operator abstract-declarator [opt]
23577 attributes [opt] direct-abstract-declarator
23579 The parser flags FLAGS is used to control type-specifier parsing.
23581 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
23582 detect constructors, destructors, deduction guides, or conversion operators.
23583 It is set to -1 if the declarator is a name, and +1 if it is a
23584 function. Otherwise it is set to zero. Usually you just want to
23585 test for >0, but internally the negative value is used.
23587 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
23588 a decl-specifier-seq unless it declares a constructor, destructor,
23589 or conversion. It might seem that we could check this condition in
23590 semantic analysis, rather than parsing, but that makes it difficult
23591 to handle something like `f()'. We want to notice that there are
23592 no decl-specifiers, and therefore realize that this is an
23593 expression, not a declaration.)
23595 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
23596 the declarator is a direct-declarator of the form "(...)".
23598 MEMBER_P is true iff this declarator is a member-declarator.
23600 FRIEND_P is true iff this declarator is a friend.
23602 STATIC_P is true iff the keyword static was seen. */
23604 static cp_declarator *
23605 cp_parser_declarator (cp_parser* parser,
23606 cp_parser_declarator_kind dcl_kind,
23607 cp_parser_flags flags,
23608 int* ctor_dtor_or_conv_p,
23609 bool* parenthesized_p,
23610 bool member_p, bool friend_p, bool static_p)
23612 cp_declarator *declarator;
23613 enum tree_code code;
23614 cp_cv_quals cv_quals;
23615 tree class_type;
23616 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
23618 /* Assume this is not a constructor, destructor, or type-conversion
23619 operator. */
23620 if (ctor_dtor_or_conv_p)
23621 *ctor_dtor_or_conv_p = 0;
23623 if (cp_parser_allow_gnu_extensions_p (parser))
23624 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
23626 /* Check for the ptr-operator production. */
23627 cp_parser_parse_tentatively (parser);
23628 /* Parse the ptr-operator. */
23629 code = cp_parser_ptr_operator (parser,
23630 &class_type,
23631 &cv_quals,
23632 &std_attributes);
23634 /* If that worked, then we have a ptr-operator. */
23635 if (cp_parser_parse_definitely (parser))
23637 /* If a ptr-operator was found, then this declarator was not
23638 parenthesized. */
23639 if (parenthesized_p)
23640 *parenthesized_p = false;
23641 /* The dependent declarator is optional if we are parsing an
23642 abstract-declarator. */
23643 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
23644 cp_parser_parse_tentatively (parser);
23646 /* Parse the dependent declarator. */
23647 declarator = cp_parser_declarator (parser, dcl_kind, flags,
23648 /*ctor_dtor_or_conv_p=*/NULL,
23649 /*parenthesized_p=*/NULL,
23650 member_p, friend_p, static_p);
23652 /* If we are parsing an abstract-declarator, we must handle the
23653 case where the dependent declarator is absent. */
23654 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
23655 && !cp_parser_parse_definitely (parser))
23656 declarator = NULL;
23658 declarator = cp_parser_make_indirect_declarator
23659 (code, class_type, cv_quals, declarator, std_attributes);
23661 /* Everything else is a direct-declarator. */
23662 else
23664 if (parenthesized_p)
23665 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
23666 CPP_OPEN_PAREN);
23667 declarator = cp_parser_direct_declarator (parser, dcl_kind,
23668 flags, ctor_dtor_or_conv_p,
23669 member_p, friend_p, static_p);
23672 if (gnu_attributes && declarator && declarator != cp_error_declarator)
23673 declarator->attributes = gnu_attributes;
23674 return declarator;
23677 /* Parse a direct-declarator or direct-abstract-declarator.
23679 direct-declarator:
23680 declarator-id
23681 direct-declarator ( parameter-declaration-clause )
23682 cv-qualifier-seq [opt]
23683 ref-qualifier [opt]
23684 exception-specification [opt]
23685 direct-declarator [ constant-expression [opt] ]
23686 ( declarator )
23688 direct-abstract-declarator:
23689 direct-abstract-declarator [opt]
23690 ( parameter-declaration-clause )
23691 cv-qualifier-seq [opt]
23692 ref-qualifier [opt]
23693 exception-specification [opt]
23694 direct-abstract-declarator [opt] [ constant-expression [opt] ]
23695 ( abstract-declarator )
23697 Returns a representation of the declarator. DCL_KIND is
23698 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
23699 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
23700 we are parsing a direct-declarator. It is
23701 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
23702 of ambiguity we prefer an abstract declarator, as per
23703 [dcl.ambig.res].
23704 The parser flags FLAGS is used to control type-specifier parsing.
23705 CTOR_DTOR_OR_CONV_P, MEMBER_P, FRIEND_P, and STATIC_P are
23706 as for cp_parser_declarator. */
23708 static cp_declarator *
23709 cp_parser_direct_declarator (cp_parser* parser,
23710 cp_parser_declarator_kind dcl_kind,
23711 cp_parser_flags flags,
23712 int* ctor_dtor_or_conv_p,
23713 bool member_p, bool friend_p, bool static_p)
23715 cp_token *token;
23716 cp_declarator *declarator = NULL;
23717 tree scope = NULL_TREE;
23718 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
23719 bool saved_in_declarator_p = parser->in_declarator_p;
23720 bool first = true;
23721 tree pushed_scope = NULL_TREE;
23722 cp_token *open_paren = NULL, *close_paren = NULL;
23724 while (true)
23726 /* Peek at the next token. */
23727 token = cp_lexer_peek_token (parser->lexer);
23728 if (token->type == CPP_OPEN_PAREN)
23730 /* This is either a parameter-declaration-clause, or a
23731 parenthesized declarator. When we know we are parsing a
23732 named declarator, it must be a parenthesized declarator
23733 if FIRST is true. For instance, `(int)' is a
23734 parameter-declaration-clause, with an omitted
23735 direct-abstract-declarator. But `((*))', is a
23736 parenthesized abstract declarator. Finally, when T is a
23737 template parameter `(T)' is a
23738 parameter-declaration-clause, and not a parenthesized
23739 named declarator.
23741 We first try and parse a parameter-declaration-clause,
23742 and then try a nested declarator (if FIRST is true).
23744 It is not an error for it not to be a
23745 parameter-declaration-clause, even when FIRST is
23746 false. Consider,
23748 int i (int);
23749 int i (3);
23751 The first is the declaration of a function while the
23752 second is the definition of a variable, including its
23753 initializer.
23755 Having seen only the parenthesis, we cannot know which of
23756 these two alternatives should be selected. Even more
23757 complex are examples like:
23759 int i (int (a));
23760 int i (int (3));
23762 The former is a function-declaration; the latter is a
23763 variable initialization.
23765 Thus again, we try a parameter-declaration-clause, and if
23766 that fails, we back out and return. */
23768 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
23770 tree params;
23771 bool is_declarator = false;
23773 open_paren = NULL;
23775 /* In a member-declarator, the only valid interpretation
23776 of a parenthesis is the start of a
23777 parameter-declaration-clause. (It is invalid to
23778 initialize a static data member with a parenthesized
23779 initializer; only the "=" form of initialization is
23780 permitted.) */
23781 if (!member_p)
23782 cp_parser_parse_tentatively (parser);
23784 /* Consume the `('. */
23785 const location_t parens_start = token->location;
23786 matching_parens parens;
23787 parens.consume_open (parser);
23788 if (first)
23790 /* If this is going to be an abstract declarator, we're
23791 in a declarator and we can't have default args. */
23792 parser->default_arg_ok_p = false;
23793 parser->in_declarator_p = true;
23796 begin_scope (sk_function_parms, NULL_TREE);
23798 /* Parse the parameter-declaration-clause. */
23799 params
23800 = cp_parser_parameter_declaration_clause (parser, flags);
23801 const location_t parens_end
23802 = cp_lexer_peek_token (parser->lexer)->location;
23804 /* Consume the `)'. */
23805 parens.require_close (parser);
23807 /* For code like
23808 int x(auto(42));
23809 A a(auto(i), 42);
23810 we have synthesized an implicit template parameter and marked
23811 what we thought was a function as an implicit function template.
23812 But now, having seen the whole parameter list, we know it's not
23813 a function declaration, so undo that. */
23814 if (cp_parser_error_occurred (parser)
23815 && parser->fully_implicit_function_template_p
23816 /* Don't do this for the inner (). */
23817 && parser->default_arg_ok_p)
23818 abort_fully_implicit_template (parser);
23820 /* If all went well, parse the cv-qualifier-seq,
23821 ref-qualifier and the exception-specification. */
23822 if (member_p || cp_parser_parse_definitely (parser))
23824 cp_cv_quals cv_quals;
23825 cp_virt_specifiers virt_specifiers;
23826 cp_ref_qualifier ref_qual;
23827 tree exception_specification;
23828 tree late_return;
23829 tree attrs;
23830 bool memfn = (member_p || (pushed_scope
23831 && CLASS_TYPE_P (pushed_scope)));
23832 unsigned char local_variables_forbidden_p
23833 = parser->local_variables_forbidden_p;
23834 /* 'this' is not allowed in static member functions. */
23835 if (static_p || friend_p)
23836 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
23838 is_declarator = true;
23840 if (ctor_dtor_or_conv_p)
23841 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
23842 first = false;
23844 /* Parse the cv-qualifier-seq. */
23845 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
23846 /* Parse the ref-qualifier. */
23847 ref_qual = cp_parser_ref_qualifier_opt (parser);
23848 /* Parse the tx-qualifier. */
23849 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
23851 tree save_ccp = current_class_ptr;
23852 tree save_ccr = current_class_ref;
23853 if (memfn && !friend_p && !static_p)
23854 /* DR 1207: 'this' is in scope after the cv-quals. */
23855 inject_this_parameter (current_class_type, cv_quals);
23857 /* If it turned out that this is e.g. a pointer to a
23858 function, we don't want to delay noexcept parsing. */
23859 if (declarator == NULL || declarator->kind != cdk_id)
23860 flags &= ~CP_PARSER_FLAGS_DELAY_NOEXCEPT;
23862 /* Parse the exception-specification. */
23863 exception_specification
23864 = cp_parser_exception_specification_opt (parser,
23865 flags);
23867 attrs = cp_parser_std_attribute_spec_seq (parser);
23869 cp_omp_declare_simd_data odsd;
23870 if ((flag_openmp || flag_openmp_simd)
23871 && declarator
23872 && declarator->std_attributes
23873 && declarator->kind == cdk_id)
23875 tree *pa = &declarator->std_attributes;
23876 cp_parser_handle_directive_omp_attributes (parser, pa,
23877 &odsd, false);
23880 /* In here, we handle cases where attribute is used after
23881 the function declaration. For example:
23882 void func (int x) __attribute__((vector(..))); */
23883 tree gnu_attrs = NULL_TREE;
23884 tree requires_clause = NULL_TREE;
23885 late_return
23886 = cp_parser_late_return_type_opt (parser, declarator,
23887 requires_clause);
23889 cp_finalize_omp_declare_simd (parser, &odsd);
23891 /* Parse the virt-specifier-seq. */
23892 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
23894 location_t parens_loc = make_location (parens_start,
23895 parens_start,
23896 parens_end);
23897 /* Create the function-declarator. */
23898 declarator = make_call_declarator (declarator,
23899 params,
23900 cv_quals,
23901 virt_specifiers,
23902 ref_qual,
23903 tx_qual,
23904 exception_specification,
23905 late_return,
23906 requires_clause,
23907 attrs,
23908 parens_loc);
23909 declarator->attributes = gnu_attrs;
23910 /* Any subsequent parameter lists are to do with
23911 return type, so are not those of the declared
23912 function. */
23913 parser->default_arg_ok_p = false;
23915 current_class_ptr = save_ccp;
23916 current_class_ref = save_ccr;
23918 /* Restore the state of local_variables_forbidden_p. */
23919 parser->local_variables_forbidden_p
23920 = local_variables_forbidden_p;
23923 /* Remove the function parms from scope. */
23924 pop_bindings_and_leave_scope ();
23926 if (is_declarator)
23927 /* Repeat the main loop. */
23928 continue;
23931 /* If this is the first, we can try a parenthesized
23932 declarator. */
23933 if (first)
23935 bool saved_in_type_id_in_expr_p;
23937 parser->default_arg_ok_p = saved_default_arg_ok_p;
23938 parser->in_declarator_p = saved_in_declarator_p;
23940 open_paren = token;
23941 /* Consume the `('. */
23942 matching_parens parens;
23943 parens.consume_open (parser);
23944 /* Parse the nested declarator. */
23945 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
23946 parser->in_type_id_in_expr_p = true;
23947 declarator
23948 = cp_parser_declarator (parser, dcl_kind, flags,
23949 ctor_dtor_or_conv_p,
23950 /*parenthesized_p=*/NULL,
23951 member_p, friend_p,
23952 /*static_p=*/false);
23953 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
23954 first = false;
23955 /* Expect a `)'. */
23956 close_paren = cp_lexer_peek_token (parser->lexer);
23957 if (!parens.require_close (parser))
23958 declarator = cp_error_declarator;
23959 if (declarator == cp_error_declarator)
23960 break;
23962 goto handle_declarator;
23964 /* Otherwise, we must be done. */
23965 else
23966 break;
23968 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
23969 && token->type == CPP_OPEN_SQUARE
23970 && !cp_next_tokens_can_be_attribute_p (parser))
23972 /* Parse an array-declarator. */
23973 tree bounds, attrs;
23975 if (ctor_dtor_or_conv_p)
23976 *ctor_dtor_or_conv_p = 0;
23978 open_paren = NULL;
23979 first = false;
23980 parser->default_arg_ok_p = false;
23981 parser->in_declarator_p = true;
23982 /* Consume the `['. */
23983 cp_lexer_consume_token (parser->lexer);
23984 /* Peek at the next token. */
23985 token = cp_lexer_peek_token (parser->lexer);
23986 /* If the next token is `]', then there is no
23987 constant-expression. */
23988 if (token->type != CPP_CLOSE_SQUARE)
23990 bool non_constant_p;
23991 bounds
23992 = cp_parser_constant_expression (parser,
23993 /*allow_non_constant=*/true,
23994 &non_constant_p);
23995 if (!non_constant_p)
23996 /* OK */;
23997 else if (error_operand_p (bounds))
23998 /* Already gave an error. */;
23999 else if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
24000 /* Let compute_array_index_type diagnose this. */;
24001 else if (!parser->in_function_body
24002 || parsing_function_declarator ())
24004 /* Normally, the array bound must be an integral constant
24005 expression. However, as an extension, we allow VLAs
24006 in function scopes as long as they aren't part of a
24007 parameter declaration. */
24008 cp_parser_error (parser,
24009 "array bound is not an integer constant");
24010 bounds = error_mark_node;
24012 else if (processing_template_decl
24013 && !type_dependent_expression_p (bounds))
24015 /* Remember this wasn't a constant-expression. */
24016 bounds = build_nop (TREE_TYPE (bounds), bounds);
24017 TREE_SIDE_EFFECTS (bounds) = 1;
24020 else
24021 bounds = NULL_TREE;
24022 /* Look for the closing `]'. */
24023 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
24025 declarator = cp_error_declarator;
24026 break;
24029 attrs = cp_parser_std_attribute_spec_seq (parser);
24030 declarator = make_array_declarator (declarator, bounds);
24031 declarator->std_attributes = attrs;
24033 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
24036 tree qualifying_scope;
24037 tree unqualified_name;
24038 tree attrs;
24039 special_function_kind sfk;
24040 bool abstract_ok;
24041 bool pack_expansion_p = false;
24042 cp_token *declarator_id_start_token;
24044 /* Parse a declarator-id */
24045 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
24046 if (abstract_ok)
24048 cp_parser_parse_tentatively (parser);
24050 /* If we see an ellipsis, we should be looking at a
24051 parameter pack. */
24052 if (token->type == CPP_ELLIPSIS)
24054 /* Consume the `...' */
24055 cp_lexer_consume_token (parser->lexer);
24057 pack_expansion_p = true;
24061 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
24062 unqualified_name
24063 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
24064 qualifying_scope = parser->scope;
24065 if (abstract_ok)
24067 bool okay = false;
24069 if (!unqualified_name && pack_expansion_p)
24071 /* Check whether an error occurred. */
24072 okay = !cp_parser_error_occurred (parser);
24074 /* We already consumed the ellipsis to mark a
24075 parameter pack, but we have no way to report it,
24076 so abort the tentative parse. We will be exiting
24077 immediately anyway. */
24078 cp_parser_abort_tentative_parse (parser);
24080 else
24081 okay = cp_parser_parse_definitely (parser);
24083 if (!okay)
24084 unqualified_name = error_mark_node;
24085 else if (unqualified_name
24086 && (qualifying_scope
24087 || (!identifier_p (unqualified_name))))
24089 cp_parser_error (parser, "expected unqualified-id");
24090 unqualified_name = error_mark_node;
24094 if (!unqualified_name)
24095 return NULL;
24096 if (unqualified_name == error_mark_node)
24098 declarator = cp_error_declarator;
24099 pack_expansion_p = false;
24100 declarator->parameter_pack_p = false;
24101 break;
24104 attrs = cp_parser_std_attribute_spec_seq (parser);
24106 if (qualifying_scope && at_namespace_scope_p ()
24107 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
24109 /* In the declaration of a member of a template class
24110 outside of the class itself, the SCOPE will sometimes
24111 be a TYPENAME_TYPE. For example, given:
24113 template <typename T>
24114 int S<T>::R::i = 3;
24116 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
24117 this context, we must resolve S<T>::R to an ordinary
24118 type, rather than a typename type.
24120 The reason we normally avoid resolving TYPENAME_TYPEs
24121 is that a specialization of `S' might render
24122 `S<T>::R' not a type. However, if `S' is
24123 specialized, then this `i' will not be used, so there
24124 is no harm in resolving the types here. */
24125 tree type;
24127 /* Resolve the TYPENAME_TYPE. */
24128 type = resolve_typename_type (qualifying_scope,
24129 /*only_current_p=*/false);
24130 /* If that failed, the declarator is invalid. */
24131 if (TREE_CODE (type) == TYPENAME_TYPE)
24133 if (typedef_variant_p (type))
24134 error_at (declarator_id_start_token->location,
24135 "cannot define member of dependent typedef "
24136 "%qT", type);
24137 else
24138 error_at (declarator_id_start_token->location,
24139 "%<%T::%E%> is not a type",
24140 TYPE_CONTEXT (qualifying_scope),
24141 TYPE_IDENTIFIER (qualifying_scope));
24143 qualifying_scope = type;
24146 sfk = sfk_none;
24148 if (unqualified_name)
24150 tree class_type;
24152 if (qualifying_scope
24153 && CLASS_TYPE_P (qualifying_scope))
24154 class_type = qualifying_scope;
24155 else
24156 class_type = current_class_type;
24158 if (TREE_CODE (unqualified_name) == TYPE_DECL)
24160 tree name_type = TREE_TYPE (unqualified_name);
24162 if (!class_type || !same_type_p (name_type, class_type))
24164 /* We do not attempt to print the declarator
24165 here because we do not have enough
24166 information about its original syntactic
24167 form. */
24168 cp_parser_error (parser, "invalid declarator");
24169 declarator = cp_error_declarator;
24170 break;
24172 else if (qualifying_scope
24173 && CLASSTYPE_USE_TEMPLATE (name_type))
24175 error_at (declarator_id_start_token->location,
24176 "invalid use of constructor as a template");
24177 inform (declarator_id_start_token->location,
24178 "use %<%T::%D%> instead of %<%T::%D%> to "
24179 "name the constructor in a qualified name",
24180 class_type,
24181 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
24182 class_type, name_type);
24183 declarator = cp_error_declarator;
24184 break;
24186 unqualified_name = constructor_name (class_type);
24189 if (class_type)
24191 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
24192 sfk = sfk_destructor;
24193 else if (identifier_p (unqualified_name)
24194 && IDENTIFIER_CONV_OP_P (unqualified_name))
24195 sfk = sfk_conversion;
24196 else if (/* There's no way to declare a constructor
24197 for an unnamed type, even if the type
24198 got a name for linkage purposes. */
24199 !TYPE_WAS_UNNAMED (class_type)
24200 /* Handle correctly (c++/19200):
24202 struct S {
24203 struct T{};
24204 friend void S(T);
24207 and also:
24209 namespace N {
24210 void S();
24213 struct S {
24214 friend void N::S();
24215 }; */
24216 && (!friend_p || class_type == qualifying_scope)
24217 && constructor_name_p (unqualified_name,
24218 class_type))
24219 sfk = sfk_constructor;
24220 else if (is_overloaded_fn (unqualified_name)
24221 && DECL_CONSTRUCTOR_P (get_first_fn
24222 (unqualified_name)))
24223 sfk = sfk_constructor;
24225 if (ctor_dtor_or_conv_p && sfk != sfk_none)
24226 *ctor_dtor_or_conv_p = -1;
24229 declarator = make_id_declarator (qualifying_scope,
24230 unqualified_name,
24231 sfk, token->location);
24232 declarator->std_attributes = attrs;
24233 declarator->parameter_pack_p = pack_expansion_p;
24235 if (pack_expansion_p)
24236 maybe_warn_variadic_templates ();
24238 /* We're looking for this case in [temp.res]:
24239 A qualified-id is assumed to name a type if [...]
24240 - it is a decl-specifier of the decl-specifier-seq of a
24241 parameter-declaration in a declarator of a function or
24242 function template declaration, ... */
24243 if (cxx_dialect >= cxx20
24244 && (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL)
24245 && declarator->kind == cdk_id
24246 && !at_class_scope_p ()
24247 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24249 /* ...whose declarator-id is qualified. If it isn't, never
24250 assume the parameters to refer to types. */
24251 if (qualifying_scope == NULL_TREE)
24252 flags &= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
24253 else
24255 /* Now we have something like
24256 template <typename T> int C::x(S::p);
24257 which can be a function template declaration or a
24258 variable template definition. If name lookup for
24259 the declarator-id C::x finds one or more function
24260 templates, assume S::p to name a type. Otherwise,
24261 don't. */
24262 tree decl
24263 = cp_parser_lookup_name (parser, unqualified_name,
24264 none_type,
24265 /*is_template=*/false,
24266 /*is_namespace=*/false,
24267 /*check_dependency=*/false,
24268 /*ambiguous_decls=*/NULL,
24269 token->location);
24271 if (!is_overloaded_fn (decl)
24272 /* Allow
24273 template<typename T>
24274 A<T>::A(T::type) { } */
24275 && !(MAYBE_CLASS_TYPE_P (qualifying_scope)
24276 && constructor_name_p (unqualified_name,
24277 qualifying_scope)))
24278 flags &= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
24283 handle_declarator:;
24284 scope = get_scope_of_declarator (declarator);
24285 if (scope)
24287 /* Any names that appear after the declarator-id for a
24288 member are looked up in the containing scope. */
24289 if (at_function_scope_p ())
24291 /* But declarations with qualified-ids can't appear in a
24292 function. */
24293 cp_parser_error (parser, "qualified-id in declaration");
24294 declarator = cp_error_declarator;
24295 break;
24297 pushed_scope = push_scope (scope);
24299 parser->in_declarator_p = true;
24300 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
24301 || (declarator && declarator->kind == cdk_id))
24302 /* Default args are only allowed on function
24303 declarations. */
24304 parser->default_arg_ok_p = saved_default_arg_ok_p;
24305 else
24306 parser->default_arg_ok_p = false;
24308 first = false;
24310 /* We're done. */
24311 else
24312 break;
24315 /* For an abstract declarator, we might wind up with nothing at this
24316 point. That's an error; the declarator is not optional. */
24317 if (!declarator)
24318 cp_parser_error (parser, "expected declarator");
24319 else if (open_paren)
24321 /* Record overly parenthesized declarator so we can give a
24322 diagnostic about confusing decl/expr disambiguation. */
24323 if (declarator->kind == cdk_array)
24325 /* If the open and close parens are on different lines, this
24326 is probably a formatting thing, so ignore. */
24327 expanded_location open = expand_location (open_paren->location);
24328 expanded_location close = expand_location (close_paren->location);
24329 if (open.line != close.line || open.file != close.file)
24330 open_paren = NULL;
24332 if (open_paren)
24333 declarator->parenthesized = make_location (open_paren->location,
24334 open_paren->location,
24335 close_paren->location);
24338 /* If we entered a scope, we must exit it now. */
24339 if (pushed_scope)
24340 pop_scope (pushed_scope);
24342 parser->default_arg_ok_p = saved_default_arg_ok_p;
24343 parser->in_declarator_p = saved_in_declarator_p;
24345 return declarator;
24348 /* Parse a ptr-operator.
24350 ptr-operator:
24351 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
24352 * cv-qualifier-seq [opt]
24354 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
24355 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
24357 GNU Extension:
24359 ptr-operator:
24360 & cv-qualifier-seq [opt]
24362 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
24363 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
24364 an rvalue reference. In the case of a pointer-to-member, *TYPE is
24365 filled in with the TYPE containing the member. *CV_QUALS is
24366 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
24367 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
24368 Note that the tree codes returned by this function have nothing
24369 to do with the types of trees that will be eventually be created
24370 to represent the pointer or reference type being parsed. They are
24371 just constants with suggestive names. */
24372 static enum tree_code
24373 cp_parser_ptr_operator (cp_parser* parser,
24374 tree* type,
24375 cp_cv_quals *cv_quals,
24376 tree *attributes)
24378 enum tree_code code = ERROR_MARK;
24379 cp_token *token;
24380 tree attrs = NULL_TREE;
24382 /* Assume that it's not a pointer-to-member. */
24383 *type = NULL_TREE;
24384 /* And that there are no cv-qualifiers. */
24385 *cv_quals = TYPE_UNQUALIFIED;
24387 /* Peek at the next token. */
24388 token = cp_lexer_peek_token (parser->lexer);
24390 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
24391 if (token->type == CPP_MULT)
24392 code = INDIRECT_REF;
24393 else if (token->type == CPP_AND)
24394 code = ADDR_EXPR;
24395 else if ((cxx_dialect != cxx98) &&
24396 token->type == CPP_AND_AND) /* C++0x only */
24397 code = NON_LVALUE_EXPR;
24399 if (code != ERROR_MARK)
24401 /* Consume the `*', `&' or `&&'. */
24402 cp_lexer_consume_token (parser->lexer);
24404 /* A `*' can be followed by a cv-qualifier-seq, and so can a
24405 `&', if we are allowing GNU extensions. (The only qualifier
24406 that can legally appear after `&' is `restrict', but that is
24407 enforced during semantic analysis. */
24408 if (code == INDIRECT_REF
24409 || cp_parser_allow_gnu_extensions_p (parser))
24410 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
24412 attrs = cp_parser_std_attribute_spec_seq (parser);
24413 if (attributes != NULL)
24414 *attributes = attrs;
24416 else
24418 /* Try the pointer-to-member case. */
24419 cp_parser_parse_tentatively (parser);
24420 /* Look for the optional `::' operator. */
24421 cp_parser_global_scope_opt (parser,
24422 /*current_scope_valid_p=*/false);
24423 /* Look for the nested-name specifier. */
24424 token = cp_lexer_peek_token (parser->lexer);
24425 cp_parser_nested_name_specifier (parser,
24426 /*typename_keyword_p=*/false,
24427 /*check_dependency_p=*/true,
24428 /*type_p=*/false,
24429 /*is_declaration=*/false);
24430 /* If we found it, and the next token is a `*', then we are
24431 indeed looking at a pointer-to-member operator. */
24432 if (!cp_parser_error_occurred (parser)
24433 && cp_parser_require (parser, CPP_MULT, RT_MULT))
24435 /* Indicate that the `*' operator was used. */
24436 code = INDIRECT_REF;
24438 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
24439 error_at (token->location, "%qD is a namespace", parser->scope);
24440 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
24441 error_at (token->location, "cannot form pointer to member of "
24442 "non-class %q#T", parser->scope);
24443 else
24445 /* The type of which the member is a member is given by the
24446 current SCOPE. */
24447 *type = parser->scope;
24448 /* The next name will not be qualified. */
24449 parser->scope = NULL_TREE;
24450 parser->qualifying_scope = NULL_TREE;
24451 parser->object_scope = NULL_TREE;
24452 /* Look for optional c++11 attributes. */
24453 attrs = cp_parser_std_attribute_spec_seq (parser);
24454 if (attributes != NULL)
24455 *attributes = attrs;
24456 /* Look for the optional cv-qualifier-seq. */
24457 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
24460 /* If that didn't work we don't have a ptr-operator. */
24461 if (!cp_parser_parse_definitely (parser))
24462 cp_parser_error (parser, "expected ptr-operator");
24465 return code;
24468 /* Parse an (optional) cv-qualifier-seq.
24470 cv-qualifier-seq:
24471 cv-qualifier cv-qualifier-seq [opt]
24473 cv-qualifier:
24474 const
24475 volatile
24477 GNU Extension:
24479 cv-qualifier:
24480 __restrict__
24482 Returns a bitmask representing the cv-qualifiers. */
24484 static cp_cv_quals
24485 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
24487 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
24489 while (true)
24491 cp_token *token;
24492 cp_cv_quals cv_qualifier;
24494 /* Peek at the next token. */
24495 token = cp_lexer_peek_token (parser->lexer);
24496 /* See if it's a cv-qualifier. */
24497 switch (token->keyword)
24499 case RID_CONST:
24500 cv_qualifier = TYPE_QUAL_CONST;
24501 break;
24503 case RID_VOLATILE:
24504 cv_qualifier = TYPE_QUAL_VOLATILE;
24505 break;
24507 case RID_RESTRICT:
24508 cv_qualifier = TYPE_QUAL_RESTRICT;
24509 break;
24511 default:
24512 cv_qualifier = TYPE_UNQUALIFIED;
24513 break;
24516 if (!cv_qualifier)
24517 break;
24519 if (cv_quals & cv_qualifier)
24521 gcc_rich_location richloc (token->location);
24522 richloc.add_fixit_remove ();
24523 error_at (&richloc, "duplicate cv-qualifier");
24524 cp_lexer_purge_token (parser->lexer);
24526 else
24528 cp_lexer_consume_token (parser->lexer);
24529 cv_quals |= cv_qualifier;
24533 return cv_quals;
24536 /* Parse an (optional) ref-qualifier
24538 ref-qualifier:
24542 Returns cp_ref_qualifier representing ref-qualifier. */
24544 static cp_ref_qualifier
24545 cp_parser_ref_qualifier_opt (cp_parser* parser)
24547 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
24549 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
24550 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
24551 return ref_qual;
24553 while (true)
24555 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
24556 cp_token *token = cp_lexer_peek_token (parser->lexer);
24558 switch (token->type)
24560 case CPP_AND:
24561 curr_ref_qual = REF_QUAL_LVALUE;
24562 break;
24564 case CPP_AND_AND:
24565 curr_ref_qual = REF_QUAL_RVALUE;
24566 break;
24568 default:
24569 curr_ref_qual = REF_QUAL_NONE;
24570 break;
24573 if (!curr_ref_qual)
24574 break;
24575 else if (ref_qual)
24577 error_at (token->location, "multiple ref-qualifiers");
24578 cp_lexer_purge_token (parser->lexer);
24580 else
24582 ref_qual = curr_ref_qual;
24583 cp_lexer_consume_token (parser->lexer);
24587 return ref_qual;
24590 /* Parse an optional tx-qualifier.
24592 tx-qualifier:
24593 transaction_safe
24594 transaction_safe_dynamic */
24596 static tree
24597 cp_parser_tx_qualifier_opt (cp_parser *parser)
24599 cp_token *token = cp_lexer_peek_token (parser->lexer);
24600 if (token->type == CPP_NAME)
24602 tree name = token->u.value;
24603 const char *p = IDENTIFIER_POINTER (name);
24604 const int len = strlen ("transaction_safe");
24605 if (startswith (p, "transaction_safe"))
24607 p += len;
24608 if (*p == '\0'
24609 || !strcmp (p, "_dynamic"))
24611 cp_lexer_consume_token (parser->lexer);
24612 if (!flag_tm)
24614 error ("%qE requires %<-fgnu-tm%>", name);
24615 return NULL_TREE;
24617 else
24618 return name;
24622 return NULL_TREE;
24625 /* Parse an (optional) virt-specifier-seq.
24627 virt-specifier-seq:
24628 virt-specifier virt-specifier-seq [opt]
24630 virt-specifier:
24631 override
24632 final
24634 Returns a bitmask representing the virt-specifiers. */
24636 static cp_virt_specifiers
24637 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
24639 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
24641 while (true)
24643 cp_token *token;
24644 cp_virt_specifiers virt_specifier;
24646 /* Peek at the next token. */
24647 token = cp_lexer_peek_token (parser->lexer);
24648 /* See if it's a virt-specifier-qualifier. */
24649 if (token->type != CPP_NAME)
24650 break;
24651 if (id_equal (token->u.value, "override"))
24653 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
24654 virt_specifier = VIRT_SPEC_OVERRIDE;
24656 else if (id_equal (token->u.value, "final"))
24658 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
24659 virt_specifier = VIRT_SPEC_FINAL;
24661 else if (id_equal (token->u.value, "__final"))
24663 virt_specifier = VIRT_SPEC_FINAL;
24665 else
24666 break;
24668 if (virt_specifiers & virt_specifier)
24670 gcc_rich_location richloc (token->location);
24671 richloc.add_fixit_remove ();
24672 error_at (&richloc, "duplicate virt-specifier");
24673 cp_lexer_purge_token (parser->lexer);
24675 else
24677 cp_lexer_consume_token (parser->lexer);
24678 virt_specifiers |= virt_specifier;
24681 return virt_specifiers;
24684 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
24685 is in scope even though it isn't real. */
24687 void
24688 inject_this_parameter (tree ctype, cp_cv_quals quals)
24690 tree this_parm;
24692 if (current_class_ptr)
24694 /* We don't clear this between NSDMIs. Is it already what we want? */
24695 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
24696 if (DECL_P (current_class_ptr)
24697 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
24698 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
24699 && cp_type_quals (type) == quals)
24700 return;
24703 this_parm = build_this_parm (NULL_TREE, ctype, quals);
24704 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
24705 current_class_ptr = NULL_TREE;
24706 current_class_ref
24707 = cp_build_fold_indirect_ref (this_parm);
24708 current_class_ptr = this_parm;
24711 /* Return true iff our current scope is a non-static data member
24712 initializer. */
24714 bool
24715 parsing_nsdmi (void)
24717 /* We recognize NSDMI context by the context-less 'this' pointer set up
24718 by the function above. */
24719 if (current_class_ptr
24720 && TREE_CODE (current_class_ptr) == PARM_DECL
24721 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
24722 return true;
24723 return false;
24726 /* True if we're parsing a function declarator. */
24728 bool
24729 parsing_function_declarator ()
24731 /* this_entity is NULL for a function parameter scope while parsing the
24732 declarator; it is set when parsing the body of the function. */
24733 return (current_binding_level->kind == sk_function_parms
24734 && !current_binding_level->this_entity);
24737 /* Parse a late-specified return type, if any. This is not a separate
24738 non-terminal, but part of a function declarator, which looks like
24740 -> trailing-type-specifier-seq abstract-declarator(opt)
24742 Returns the type indicated by the type-id.
24744 In addition to this, parse any queued up #pragma omp declare simd
24745 clauses, and #pragma acc routine clauses.
24747 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
24748 function. */
24750 static tree
24751 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
24752 tree& requires_clause)
24754 cp_token *token;
24755 tree type = NULL_TREE;
24756 bool declare_simd_p = (parser->omp_declare_simd
24757 && declarator
24758 && declarator->kind == cdk_id);
24760 bool oacc_routine_p = (parser->oacc_routine
24761 && declarator
24762 && declarator->kind == cdk_id);
24764 /* Peek at the next token. */
24765 token = cp_lexer_peek_token (parser->lexer);
24766 /* A late-specified return type is indicated by an initial '->'. */
24767 if (token->type != CPP_DEREF
24768 && token->keyword != RID_REQUIRES
24769 && !(token->type == CPP_NAME
24770 && token->u.value == ridpointers[RID_REQUIRES])
24771 && !(declare_simd_p || oacc_routine_p))
24772 return NULL_TREE;
24774 if (token->type == CPP_DEREF)
24776 /* Consume the ->. */
24777 cp_lexer_consume_token (parser->lexer);
24779 type = cp_parser_trailing_type_id (parser);
24782 /* Function declarations may be followed by a trailing
24783 requires-clause. */
24784 requires_clause = cp_parser_requires_clause_opt (parser, false);
24786 if (declare_simd_p)
24787 declarator->attributes
24788 = cp_parser_late_parsing_omp_declare_simd (parser,
24789 declarator->attributes);
24790 if (oacc_routine_p)
24791 declarator->attributes
24792 = cp_parser_late_parsing_oacc_routine (parser,
24793 declarator->attributes);
24795 return type;
24798 /* Parse a declarator-id.
24800 declarator-id:
24801 id-expression
24802 :: [opt] nested-name-specifier [opt] type-name
24804 In the `id-expression' case, the value returned is as for
24805 cp_parser_id_expression if the id-expression was an unqualified-id.
24806 If the id-expression was a qualified-id, then a SCOPE_REF is
24807 returned. The first operand is the scope (either a NAMESPACE_DECL
24808 or TREE_TYPE), but the second is still just a representation of an
24809 unqualified-id. */
24811 static tree
24812 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
24814 tree id;
24815 /* The expression must be an id-expression. Assume that qualified
24816 names are the names of types so that:
24818 template <class T>
24819 int S<T>::R::i = 3;
24821 will work; we must treat `S<T>::R' as the name of a type.
24822 Similarly, assume that qualified names are templates, where
24823 required, so that:
24825 template <class T>
24826 int S<T>::R<T>::i = 3;
24828 will work, too. */
24829 id = cp_parser_id_expression (parser,
24830 /*template_keyword_p=*/false,
24831 /*check_dependency_p=*/false,
24832 /*template_p=*/NULL,
24833 /*declarator_p=*/true,
24834 optional_p);
24835 if (id && BASELINK_P (id))
24836 id = BASELINK_FUNCTIONS (id);
24837 return id;
24840 /* Parse a type-id.
24842 type-id:
24843 type-specifier-seq abstract-declarator [opt]
24845 The parser flags FLAGS is used to control type-specifier parsing.
24847 If IS_TEMPLATE_ARG is true, we are parsing a template argument.
24849 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
24850 i.e. we've just seen "->".
24852 Returns the TYPE specified. */
24854 static tree
24855 cp_parser_type_id_1 (cp_parser *parser, cp_parser_flags flags,
24856 bool is_template_arg, bool is_trailing_return,
24857 location_t *type_location)
24859 cp_decl_specifier_seq type_specifier_seq;
24860 cp_declarator *abstract_declarator;
24862 /* Parse the type-specifier-seq. */
24863 cp_parser_type_specifier_seq (parser, flags,
24864 /*is_declaration=*/false,
24865 is_trailing_return,
24866 &type_specifier_seq);
24867 if (type_location)
24868 *type_location = type_specifier_seq.locations[ds_type_spec];
24870 if (is_template_arg && type_specifier_seq.type
24871 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
24872 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
24873 /* A bare template name as a template argument is a template template
24874 argument, not a placeholder, so fail parsing it as a type argument. */
24876 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
24877 cp_parser_simulate_error (parser);
24878 return error_mark_node;
24880 if (type_specifier_seq.type == error_mark_node)
24881 return error_mark_node;
24883 /* There might or might not be an abstract declarator. */
24884 cp_parser_parse_tentatively (parser);
24885 /* Look for the declarator. */
24886 abstract_declarator
24887 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT,
24888 CP_PARSER_FLAGS_NONE, NULL,
24889 /*parenthesized_p=*/NULL,
24890 /*member_p=*/false,
24891 /*friend_p=*/false,
24892 /*static_p=*/false);
24893 /* Check to see if there really was a declarator. */
24894 if (!cp_parser_parse_definitely (parser))
24895 abstract_declarator = NULL;
24897 bool auto_typeid_ok = false;
24898 /* The concepts TS allows 'auto' as a type-id. */
24899 if (flag_concepts_ts)
24900 auto_typeid_ok = !parser->in_type_id_in_expr_p;
24901 /* DR 625 prohibits use of auto as a template-argument. We allow 'auto'
24902 outside the template-argument-list context here only for the sake of
24903 diagnostic: grokdeclarator then can emit a better error message for
24904 e.g. using T = auto. */
24905 else if (flag_concepts)
24906 auto_typeid_ok = (!parser->in_type_id_in_expr_p
24907 && !parser->in_template_argument_list_p);
24909 if (type_specifier_seq.type
24910 && !auto_typeid_ok
24911 /* None of the valid uses of 'auto' in C++14 involve the type-id
24912 nonterminal, but it is valid in a trailing-return-type. */
24913 && !(cxx_dialect >= cxx14 && is_trailing_return))
24914 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
24916 /* A type-id with type 'auto' is only ok if the abstract declarator
24917 is a function declarator with a late-specified return type.
24919 A type-id with 'auto' is also valid in a trailing-return-type
24920 in a compound-requirement. */
24921 if (abstract_declarator
24922 && abstract_declarator->kind == cdk_function
24923 && abstract_declarator->u.function.late_return_type)
24924 /* OK */;
24925 else if (parser->in_result_type_constraint_p)
24926 /* OK */;
24927 else
24929 if (!cp_parser_simulate_error (parser))
24931 location_t loc = type_specifier_seq.locations[ds_type_spec];
24932 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
24934 auto_diagnostic_group g;
24935 gcc_rich_location richloc (loc);
24936 richloc.add_fixit_insert_after ("<>");
24937 error_at (&richloc, "missing template arguments after %qE",
24938 tmpl);
24939 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
24940 tmpl);
24942 else if (parser->in_template_argument_list_p)
24943 error_at (loc, "%qT not permitted in template argument",
24944 auto_node);
24945 else
24946 error_at (loc, "invalid use of %qT", auto_node);
24948 return error_mark_node;
24952 return groktypename (&type_specifier_seq, abstract_declarator,
24953 is_template_arg);
24956 /* Wrapper for cp_parser_type_id_1. */
24958 static tree
24959 cp_parser_type_id (cp_parser *parser, cp_parser_flags flags,
24960 location_t *type_location)
24962 return cp_parser_type_id_1 (parser, flags, false, false, type_location);
24965 /* Wrapper for cp_parser_type_id_1. */
24967 static tree
24968 cp_parser_template_type_arg (cp_parser *parser)
24970 tree r;
24971 const char *saved_message = parser->type_definition_forbidden_message;
24972 parser->type_definition_forbidden_message
24973 = G_("types may not be defined in template arguments");
24974 r = cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_NONE, true, false, NULL);
24975 parser->type_definition_forbidden_message = saved_message;
24976 return r;
24979 /* Wrapper for cp_parser_type_id_1. */
24981 static tree
24982 cp_parser_trailing_type_id (cp_parser *parser)
24984 return cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
24985 false, true, NULL);
24988 /* Parse a type-specifier-seq.
24990 type-specifier-seq:
24991 type-specifier type-specifier-seq [opt]
24993 GNU extension:
24995 type-specifier-seq:
24996 attributes type-specifier-seq [opt]
24998 The parser flags FLAGS is used to control type-specifier parsing.
25000 If IS_DECLARATION is true, we are at the start of a "condition" or
25001 exception-declaration, so we might be followed by a declarator-id.
25003 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
25004 i.e. we've just seen "->".
25006 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
25008 static void
25009 cp_parser_type_specifier_seq (cp_parser* parser,
25010 cp_parser_flags flags,
25011 bool is_declaration,
25012 bool is_trailing_return,
25013 cp_decl_specifier_seq *type_specifier_seq)
25015 bool seen_type_specifier = false;
25016 cp_token *start_token = NULL;
25018 /* Clear the TYPE_SPECIFIER_SEQ. */
25019 clear_decl_specs (type_specifier_seq);
25021 flags |= CP_PARSER_FLAGS_OPTIONAL;
25022 /* In the context of a trailing return type, enum E { } is an
25023 elaborated-type-specifier followed by a function-body, not an
25024 enum-specifier. */
25025 if (is_trailing_return)
25026 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
25028 /* Parse the type-specifiers and attributes. */
25029 while (true)
25031 tree type_specifier;
25032 bool is_cv_qualifier;
25034 /* Check for attributes first. */
25035 if (cp_next_tokens_can_be_attribute_p (parser))
25037 /* GNU attributes at the end of a declaration apply to the
25038 declaration as a whole, not to the trailing return type. So look
25039 ahead to see if these attributes are at the end. */
25040 if (seen_type_specifier && is_trailing_return
25041 && cp_next_tokens_can_be_gnu_attribute_p (parser))
25043 size_t n = cp_parser_skip_attributes_opt (parser, 1);
25044 cp_token *tok = cp_lexer_peek_nth_token (parser->lexer, n);
25045 if (tok->type == CPP_SEMICOLON || tok->type == CPP_COMMA
25046 || tok->type == CPP_EQ || tok->type == CPP_OPEN_BRACE)
25047 break;
25049 type_specifier_seq->attributes
25050 = attr_chainon (type_specifier_seq->attributes,
25051 cp_parser_attributes_opt (parser));
25052 continue;
25055 /* record the token of the beginning of the type specifier seq,
25056 for error reporting purposes*/
25057 if (!start_token)
25058 start_token = cp_lexer_peek_token (parser->lexer);
25060 /* Look for the type-specifier. */
25061 type_specifier = cp_parser_type_specifier (parser,
25062 flags,
25063 type_specifier_seq,
25064 /*is_declaration=*/false,
25065 NULL,
25066 &is_cv_qualifier);
25067 if (!type_specifier)
25069 /* If the first type-specifier could not be found, this is not a
25070 type-specifier-seq at all. */
25071 if (!seen_type_specifier)
25073 /* Set in_declarator_p to avoid skipping to the semicolon. */
25074 int in_decl = parser->in_declarator_p;
25075 parser->in_declarator_p = true;
25077 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
25078 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
25079 cp_parser_error (parser, "expected type-specifier");
25081 parser->in_declarator_p = in_decl;
25083 type_specifier_seq->type = error_mark_node;
25084 return;
25086 /* If subsequent type-specifiers could not be found, the
25087 type-specifier-seq is complete. */
25088 break;
25091 seen_type_specifier = true;
25092 /* The standard says that a condition can be:
25094 type-specifier-seq declarator = assignment-expression
25096 However, given:
25098 struct S {};
25099 if (int S = ...)
25101 we should treat the "S" as a declarator, not as a
25102 type-specifier. The standard doesn't say that explicitly for
25103 type-specifier-seq, but it does say that for
25104 decl-specifier-seq in an ordinary declaration. Perhaps it
25105 would be clearer just to allow a decl-specifier-seq here, and
25106 then add a semantic restriction that if any decl-specifiers
25107 that are not type-specifiers appear, the program is invalid. */
25108 if (is_declaration && !is_cv_qualifier)
25109 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
25113 /* Return whether the function currently being declared has an associated
25114 template parameter list. */
25116 static bool
25117 function_being_declared_is_template_p (cp_parser* parser)
25119 if (!current_template_parms || processing_template_parmlist)
25120 return false;
25122 if (parser->implicit_template_scope)
25123 return true;
25125 if (at_class_scope_p ()
25126 && TYPE_BEING_DEFINED (current_class_type))
25127 return parser->num_template_parameter_lists != 0;
25129 return ((int) parser->num_template_parameter_lists > template_class_depth
25130 (current_class_type));
25133 /* Parse a parameter-declaration-clause.
25135 parameter-declaration-clause:
25136 parameter-declaration-list [opt] ... [opt]
25137 parameter-declaration-list , ...
25139 The parser flags FLAGS is used to control type-specifier parsing.
25141 Returns a representation for the parameter declarations. A return
25142 value of NULL indicates a parameter-declaration-clause consisting
25143 only of an ellipsis. */
25145 static tree
25146 cp_parser_parameter_declaration_clause (cp_parser* parser,
25147 cp_parser_flags flags)
25149 tree parameters;
25150 cp_token *token;
25151 bool ellipsis_p;
25153 auto cleanup = make_temp_override
25154 (parser->auto_is_implicit_function_template_parm_p);
25156 if (!processing_specialization
25157 && !processing_template_parmlist
25158 && !processing_explicit_instantiation
25159 /* default_arg_ok_p tracks whether this is a parameter-clause for an
25160 actual function or a random abstract declarator. */
25161 && parser->default_arg_ok_p)
25162 if (!current_function_decl
25163 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
25164 parser->auto_is_implicit_function_template_parm_p = true;
25166 /* Peek at the next token. */
25167 token = cp_lexer_peek_token (parser->lexer);
25168 /* Check for trivial parameter-declaration-clauses. */
25169 if (token->type == CPP_ELLIPSIS)
25171 /* Consume the `...' token. */
25172 cp_lexer_consume_token (parser->lexer);
25173 return NULL_TREE;
25175 else if (token->type == CPP_CLOSE_PAREN)
25176 /* There are no parameters. */
25177 return void_list_node;
25178 /* Check for `(void)', too, which is a special case. */
25179 else if (token->keyword == RID_VOID
25180 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
25181 == CPP_CLOSE_PAREN))
25183 /* Consume the `void' token. */
25184 cp_lexer_consume_token (parser->lexer);
25185 /* There are no parameters. */
25186 return explicit_void_list_node;
25189 /* A vector of parameters that haven't been pushed yet. */
25190 auto_vec<tree> pending_decls;
25192 /* Parse the parameter-declaration-list. */
25193 parameters = cp_parser_parameter_declaration_list (parser, flags,
25194 &pending_decls);
25195 /* If a parse error occurred while parsing the
25196 parameter-declaration-list, then the entire
25197 parameter-declaration-clause is erroneous. */
25198 if (parameters == error_mark_node)
25199 return NULL_TREE;
25201 /* Peek at the next token. */
25202 token = cp_lexer_peek_token (parser->lexer);
25203 /* If it's a `,', the clause should terminate with an ellipsis. */
25204 if (token->type == CPP_COMMA)
25206 /* Consume the `,'. */
25207 cp_lexer_consume_token (parser->lexer);
25208 /* Expect an ellipsis. */
25209 ellipsis_p
25210 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
25212 /* It might also be `...' if the optional trailing `,' was
25213 omitted. */
25214 else if (token->type == CPP_ELLIPSIS)
25216 /* Consume the `...' token. */
25217 cp_lexer_consume_token (parser->lexer);
25218 /* And remember that we saw it. */
25219 ellipsis_p = true;
25221 else
25222 ellipsis_p = false;
25224 /* A valid parameter-declaration-clause can only be followed by a ')'.
25225 So it's time to push all the parameters we have seen now that we
25226 know we have a valid declaration. Note that here we may not have
25227 committed yet, nor should we. Pushing here will detect the error
25228 of redefining a parameter. */
25229 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
25231 for (tree p : pending_decls)
25232 pushdecl (p);
25234 /* Delayed checking of auto parameters. */
25235 if (!parser->auto_is_implicit_function_template_parm_p
25236 && cxx_dialect >= cxx14)
25237 for (tree p = parameters; p; p = TREE_CHAIN (p))
25238 if (type_uses_auto (TREE_TYPE (TREE_VALUE (p))))
25240 error_at (location_of (TREE_VALUE (p)),
25241 "%<auto%> parameter not permitted in this context");
25242 TREE_TYPE (TREE_VALUE (p)) = error_mark_node;
25246 /* Finish the parameter list. */
25247 if (!ellipsis_p)
25248 parameters = chainon (parameters, void_list_node);
25250 return parameters;
25253 /* Parse a parameter-declaration-list.
25255 parameter-declaration-list:
25256 parameter-declaration
25257 parameter-declaration-list , parameter-declaration
25259 The parser flags FLAGS is used to control type-specifier parsing.
25260 PENDING_DECLS is a vector of parameters that haven't been pushed yet.
25262 Returns a representation of the parameter-declaration-list, as for
25263 cp_parser_parameter_declaration_clause. However, the
25264 `void_list_node' is never appended to the list. */
25266 static tree
25267 cp_parser_parameter_declaration_list (cp_parser* parser,
25268 cp_parser_flags flags,
25269 auto_vec<tree> *pending_decls)
25271 tree parameters = NULL_TREE;
25272 tree *tail = &parameters;
25273 bool saved_in_unbraced_linkage_specification_p;
25274 int index = 0;
25276 /* The special considerations that apply to a function within an
25277 unbraced linkage specifications do not apply to the parameters
25278 to the function. */
25279 saved_in_unbraced_linkage_specification_p
25280 = parser->in_unbraced_linkage_specification_p;
25281 parser->in_unbraced_linkage_specification_p = false;
25283 /* Look for more parameters. */
25284 while (true)
25286 cp_parameter_declarator *parameter;
25287 tree decl = error_mark_node;
25288 bool parenthesized_p = false;
25290 /* Parse the parameter. */
25291 parameter
25292 = cp_parser_parameter_declaration (parser, flags,
25293 /*template_parm_p=*/false,
25294 &parenthesized_p);
25296 /* We don't know yet if the enclosing context is unavailable or deprecated,
25297 so wait and deal with it in grokparms if appropriate. */
25298 deprecated_state = UNAVAILABLE_DEPRECATED_SUPPRESS;
25300 if (parameter && !cp_parser_error_occurred (parser))
25302 decl = grokdeclarator (parameter->declarator,
25303 &parameter->decl_specifiers,
25304 PARM,
25305 parameter->default_argument != NULL_TREE,
25306 &parameter->decl_specifiers.attributes);
25307 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
25308 DECL_SOURCE_LOCATION (decl) = parameter->loc;
25311 deprecated_state = DEPRECATED_NORMAL;
25313 /* If a parse error occurred parsing the parameter declaration,
25314 then the entire parameter-declaration-list is erroneous. */
25315 if (decl == error_mark_node)
25317 parameters = error_mark_node;
25318 break;
25321 if (parameter->decl_specifiers.attributes)
25322 cplus_decl_attributes (&decl,
25323 parameter->decl_specifiers.attributes,
25325 if (DECL_NAME (decl))
25327 /* We cannot always pushdecl while parsing tentatively because
25328 it may have side effects and we can't be sure yet if we're
25329 parsing a declaration, e.g.:
25331 S foo(int(x), int(x), int{x});
25333 where it's not clear if we're dealing with a constructor call
25334 or a function declaration until we've seen the last argument
25335 which breaks it up.
25336 It's safe to pushdecl so long as it doesn't result in a clash
25337 with an already-pushed parameter. But we don't delay pushing
25338 different parameters to handle
25340 S foo(int(i), decltype(i) j = 42);
25342 which is valid. */
25343 if (pending_decls
25344 && cp_parser_uncommitted_to_tentative_parse_p (parser)
25345 /* See if PARAMETERS already contains a parameter with the same
25346 DECL_NAME as DECL. */
25347 && [parameters, decl] {
25348 for (tree p = parameters; p; p = TREE_CHAIN (p))
25349 if (DECL_NAME (decl) == DECL_NAME (TREE_VALUE (p)))
25350 return true;
25351 return false;
25352 }())
25353 pending_decls->safe_push (decl);
25354 else
25355 decl = pushdecl (decl);
25358 if (decl != error_mark_node)
25360 retrofit_lang_decl (decl);
25361 DECL_PARM_INDEX (decl) = ++index;
25362 DECL_PARM_LEVEL (decl) = function_parm_depth ();
25365 /* Add the new parameter to the list. */
25366 *tail = build_tree_list (parameter->default_argument, decl);
25367 tail = &TREE_CHAIN (*tail);
25369 /* If the parameters were parenthesized, it's the case of
25370 T foo(X(x)) which looks like a variable definition but
25371 is a function declaration. */
25372 if (index == 1 || PARENTHESIZED_LIST_P (parameters))
25373 PARENTHESIZED_LIST_P (parameters) = parenthesized_p;
25375 /* Peek at the next token. */
25376 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
25377 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
25378 /* These are for Objective-C++ */
25379 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
25380 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25381 /* The parameter-declaration-list is complete. */
25382 break;
25383 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25385 cp_token *token;
25387 /* Peek at the next token. */
25388 token = cp_lexer_peek_nth_token (parser->lexer, 2);
25389 /* If it's an ellipsis, then the list is complete. */
25390 if (token->type == CPP_ELLIPSIS)
25391 break;
25392 /* Otherwise, there must be more parameters. Consume the
25393 `,'. */
25394 cp_lexer_consume_token (parser->lexer);
25395 /* When parsing something like:
25397 int i(float f, double d)
25399 we can tell after seeing the declaration for "f" that we
25400 are not looking at an initialization of a variable "i",
25401 but rather at the declaration of a function "i".
25403 Due to the fact that the parsing of template arguments
25404 (as specified to a template-id) requires backtracking we
25405 cannot use this technique when inside a template argument
25406 list. */
25407 if (!parser->in_template_argument_list_p
25408 && !parser->in_type_id_in_expr_p
25409 && cp_parser_uncommitted_to_tentative_parse_p (parser)
25410 /* However, a parameter-declaration of the form
25411 "float(f)" (which is a valid declaration of a
25412 parameter "f") can also be interpreted as an
25413 expression (the conversion of "f" to "float"). */
25414 && !parenthesized_p)
25415 cp_parser_commit_to_tentative_parse (parser);
25417 else
25419 cp_parser_error (parser, "expected %<,%> or %<...%>");
25420 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
25421 cp_parser_skip_to_closing_parenthesis (parser,
25422 /*recovering=*/true,
25423 /*or_comma=*/false,
25424 /*consume_paren=*/false);
25425 break;
25429 parser->in_unbraced_linkage_specification_p
25430 = saved_in_unbraced_linkage_specification_p;
25432 /* Reset implicit_template_scope if we are about to leave the function
25433 parameter list that introduced it. Note that for out-of-line member
25434 definitions, there will be one or more class scopes before we get to
25435 the template parameter scope. */
25437 if (cp_binding_level *its = parser->implicit_template_scope)
25438 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
25440 while (maybe_its->kind == sk_class)
25441 maybe_its = maybe_its->level_chain;
25442 if (maybe_its == its)
25444 parser->implicit_template_parms = 0;
25445 parser->implicit_template_scope = 0;
25449 return parameters;
25452 /* Parse a parameter declaration.
25454 parameter-declaration:
25455 decl-specifier-seq ... [opt] declarator
25456 decl-specifier-seq declarator = assignment-expression
25457 decl-specifier-seq ... [opt] abstract-declarator [opt]
25458 decl-specifier-seq abstract-declarator [opt] = assignment-expression
25460 The parser flags FLAGS is used to control type-specifier parsing.
25462 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
25463 declares a template parameter. (In that case, a non-nested `>'
25464 token encountered during the parsing of the assignment-expression
25465 is not interpreted as a greater-than operator.)
25467 Returns a representation of the parameter, or NULL if an error
25468 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
25469 true iff the declarator is of the form "(p)". */
25471 static cp_parameter_declarator *
25472 cp_parser_parameter_declaration (cp_parser *parser,
25473 cp_parser_flags flags,
25474 bool template_parm_p,
25475 bool *parenthesized_p)
25477 int declares_class_or_enum;
25478 cp_decl_specifier_seq decl_specifiers;
25479 cp_declarator *declarator;
25480 tree default_argument;
25481 cp_token *token = NULL, *declarator_token_start = NULL;
25482 const char *saved_message;
25483 bool template_parameter_pack_p = false;
25485 /* In a template parameter, `>' is not an operator.
25487 [temp.param]
25489 When parsing a default template-argument for a non-type
25490 template-parameter, the first non-nested `>' is taken as the end
25491 of the template parameter-list rather than a greater-than
25492 operator. */
25494 /* Type definitions may not appear in parameter types. */
25495 saved_message = parser->type_definition_forbidden_message;
25496 parser->type_definition_forbidden_message
25497 = G_("types may not be defined in parameter types");
25499 int template_parm_idx = (function_being_declared_is_template_p (parser) ?
25500 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
25501 (current_template_parms)) : 0);
25503 /* Parse the declaration-specifiers. */
25504 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
25505 cp_parser_decl_specifier_seq (parser,
25506 flags,
25507 &decl_specifiers,
25508 &declares_class_or_enum);
25510 /* [dcl.spec.auto.general]: "A placeholder-type-specifier of the form
25511 type-constraint opt auto can be used as a decl-specifier of the
25512 decl-specifier-seq of a parameter-declaration of a function declaration
25513 or lambda-expression..." but we must not synthesize an implicit template
25514 type parameter in its declarator. That is, in "void f(auto[auto{10}]);"
25515 we want to synthesize only the first auto. */
25516 auto cleanup = make_temp_override
25517 (parser->auto_is_implicit_function_template_parm_p, false);
25519 /* Complain about missing 'typename' or other invalid type names. */
25520 if (!decl_specifiers.any_type_specifiers_p
25521 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
25522 decl_specifiers.type = error_mark_node;
25524 /* If an error occurred, there's no reason to attempt to parse the
25525 rest of the declaration. */
25526 if (cp_parser_error_occurred (parser))
25528 parser->type_definition_forbidden_message = saved_message;
25529 return NULL;
25532 /* Peek at the next token. */
25533 token = cp_lexer_peek_token (parser->lexer);
25535 /* If the next token is a `)', `,', `=', `>', or `...', then there
25536 is no declarator. However, when variadic templates are enabled,
25537 there may be a declarator following `...'. */
25538 if (token->type == CPP_CLOSE_PAREN
25539 || token->type == CPP_COMMA
25540 || token->type == CPP_EQ
25541 || token->type == CPP_GREATER)
25543 declarator = NULL;
25544 if (parenthesized_p)
25545 *parenthesized_p = false;
25547 /* Otherwise, there should be a declarator. */
25548 else
25550 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
25551 parser->default_arg_ok_p = false;
25553 /* After seeing a decl-specifier-seq, if the next token is not a
25554 "(" or "{", there is no possibility that the code is a valid
25555 expression. Therefore, if parsing tentatively, we commit at
25556 this point. */
25557 if (!parser->in_template_argument_list_p
25558 /* In an expression context, having seen:
25560 (int((char ...
25562 we cannot be sure whether we are looking at a
25563 function-type (taking a "char" as a parameter) or a cast
25564 of some object of type "char" to "int". */
25565 && !parser->in_type_id_in_expr_p
25566 && cp_parser_uncommitted_to_tentative_parse_p (parser)
25567 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
25569 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25571 if (decl_specifiers.type
25572 && template_placeholder_p (decl_specifiers.type))
25573 /* This is a CTAD expression, not a parameter declaration. */
25574 cp_parser_simulate_error (parser);
25576 else
25577 cp_parser_commit_to_tentative_parse (parser);
25579 /* Parse the declarator. */
25580 declarator_token_start = token;
25581 declarator = cp_parser_declarator (parser,
25582 CP_PARSER_DECLARATOR_EITHER,
25583 CP_PARSER_FLAGS_NONE,
25584 /*ctor_dtor_or_conv_p=*/NULL,
25585 parenthesized_p,
25586 /*member_p=*/false,
25587 /*friend_p=*/false,
25588 /*static_p=*/false);
25589 parser->default_arg_ok_p = saved_default_arg_ok_p;
25590 /* After the declarator, allow more attributes. */
25591 decl_specifiers.attributes
25592 = attr_chainon (decl_specifiers.attributes,
25593 cp_parser_attributes_opt (parser));
25595 /* If the declarator is a template parameter pack, remember that and
25596 clear the flag in the declarator itself so we don't get errors
25597 from grokdeclarator. */
25598 if (template_parm_p && declarator && declarator->parameter_pack_p)
25600 declarator->parameter_pack_p = false;
25601 template_parameter_pack_p = true;
25605 /* If the next token is an ellipsis, and we have not seen a declarator
25606 name, and if either the type of the declarator contains parameter
25607 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
25608 for, eg, abbreviated integral type names), then we actually have a
25609 parameter pack expansion expression. Otherwise, leave the ellipsis
25610 for a C-style variadic function. */
25611 token = cp_lexer_peek_token (parser->lexer);
25613 /* If a function parameter pack was specified and an implicit template
25614 parameter was introduced during cp_parser_parameter_declaration,
25615 change any implicit parameters introduced into packs. */
25616 if (parser->implicit_template_parms
25617 && ((token->type == CPP_ELLIPSIS
25618 && declarator_can_be_parameter_pack (declarator))
25619 || (declarator && declarator->parameter_pack_p)))
25621 int latest_template_parm_idx = TREE_VEC_LENGTH
25622 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
25624 if (latest_template_parm_idx != template_parm_idx)
25625 decl_specifiers.type = convert_generic_types_to_packs
25626 (decl_specifiers.type,
25627 template_parm_idx, latest_template_parm_idx);
25630 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25632 tree type = decl_specifiers.type;
25634 if (type && DECL_P (type))
25635 type = TREE_TYPE (type);
25637 if (((type
25638 && TREE_CODE (type) != TYPE_PACK_EXPANSION
25639 && (template_parm_p || uses_parameter_packs (type)))
25640 || (!type && template_parm_p))
25641 && declarator_can_be_parameter_pack (declarator))
25643 /* Consume the `...'. */
25644 cp_lexer_consume_token (parser->lexer);
25645 maybe_warn_variadic_templates ();
25647 /* Build a pack expansion type */
25648 if (template_parm_p)
25649 template_parameter_pack_p = true;
25650 else if (declarator)
25651 declarator->parameter_pack_p = true;
25652 else
25653 decl_specifiers.type = make_pack_expansion (type);
25657 /* The restriction on defining new types applies only to the type
25658 of the parameter, not to the default argument. */
25659 parser->type_definition_forbidden_message = saved_message;
25661 /* If the next token is `=', then process a default argument. */
25662 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
25664 tree type = decl_specifiers.type;
25665 token = cp_lexer_peek_token (parser->lexer);
25666 if (declarator)
25667 declarator->init_loc = token->location;
25668 /* If we are defining a class, then the tokens that make up the
25669 default argument must be saved and processed later. */
25670 if (!template_parm_p && at_class_scope_p ()
25671 && TYPE_BEING_DEFINED (current_class_type)
25672 && !LAMBDA_TYPE_P (current_class_type))
25673 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
25675 /* A constrained-type-specifier may declare a type
25676 template-parameter. */
25677 else if (declares_constrained_type_template_parameter (type))
25678 default_argument
25679 = cp_parser_default_type_template_argument (parser);
25681 /* A constrained-type-specifier may declare a
25682 template-template-parameter. */
25683 else if (declares_constrained_template_template_parameter (type))
25684 default_argument
25685 = cp_parser_default_template_template_argument (parser);
25687 /* Outside of a class definition, we can just parse the
25688 assignment-expression. */
25689 else
25690 default_argument
25691 = cp_parser_default_argument (parser, template_parm_p);
25693 if (!parser->default_arg_ok_p)
25695 permerror (token->location,
25696 "default arguments are only "
25697 "permitted for function parameters");
25699 else if ((declarator && declarator->parameter_pack_p)
25700 || template_parameter_pack_p
25701 || (decl_specifiers.type
25702 && PACK_EXPANSION_P (decl_specifiers.type)))
25704 /* Find the name of the parameter pack. */
25705 cp_declarator *id_declarator = declarator;
25706 while (id_declarator && id_declarator->kind != cdk_id)
25707 id_declarator = id_declarator->declarator;
25709 if (id_declarator && id_declarator->kind == cdk_id)
25710 error_at (declarator_token_start->location,
25711 template_parm_p
25712 ? G_("template parameter pack %qD "
25713 "cannot have a default argument")
25714 : G_("parameter pack %qD cannot have "
25715 "a default argument"),
25716 id_declarator->u.id.unqualified_name);
25717 else
25718 error_at (declarator_token_start->location,
25719 template_parm_p
25720 ? G_("template parameter pack cannot have "
25721 "a default argument")
25722 : G_("parameter pack cannot have a "
25723 "default argument"));
25725 default_argument = NULL_TREE;
25728 else
25729 default_argument = NULL_TREE;
25731 if (default_argument)
25732 STRIP_ANY_LOCATION_WRAPPER (default_argument);
25734 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_this))
25736 /* Xobj parameters can not have default arguments, thus
25737 we can reuse the default argument field to flag the param as such. */
25738 default_argument = this_identifier;
25741 /* Generate a location for the parameter, ranging from the start of the
25742 initial token to the end of the final token (using input_location for
25743 the latter, set up by cp_lexer_set_source_position_from_token when
25744 consuming tokens).
25746 If we have a identifier, then use it for the caret location, e.g.
25748 extern int callee (int one, int (*two)(int, int), float three);
25749 ~~~~~~^~~~~~~~~~~~~~
25751 otherwise, reuse the start location for the caret location e.g.:
25753 extern int callee (int one, int (*)(int, int), float three);
25754 ^~~~~~~~~~~~~~~~~
25757 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
25758 ? declarator->id_loc
25759 : decl_spec_token_start->location);
25760 location_t param_loc = make_location (caret_loc,
25761 decl_spec_token_start->location,
25762 input_location);
25764 return make_parameter_declarator (&decl_specifiers,
25765 declarator,
25766 default_argument,
25767 param_loc,
25768 template_parameter_pack_p);
25771 /* Parse a default argument and return it.
25773 TEMPLATE_PARM_P is true if this is a default argument for a
25774 non-type template parameter. */
25775 static tree
25776 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
25778 tree default_argument = NULL_TREE;
25779 bool saved_greater_than_is_operator_p;
25780 unsigned char saved_local_variables_forbidden_p;
25782 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
25783 set correctly. */
25784 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
25785 parser->greater_than_is_operator_p = !template_parm_p;
25786 auto odsd = make_temp_override (parser->omp_declare_simd, NULL);
25787 auto ord = make_temp_override (parser->oacc_routine, NULL);
25788 auto oafp = make_temp_override (parser->omp_attrs_forbidden_p, false);
25790 /* Local variable names (and the `this' keyword) may not
25791 appear in a default argument. */
25792 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
25793 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
25794 /* Parse the assignment-expression. */
25795 if (template_parm_p)
25796 push_deferring_access_checks (dk_no_deferred);
25797 tree saved_class_ptr = NULL_TREE;
25798 tree saved_class_ref = NULL_TREE;
25799 /* The "this" pointer is not valid in a default argument. */
25800 if (cfun)
25802 saved_class_ptr = current_class_ptr;
25803 cp_function_chain->x_current_class_ptr = NULL_TREE;
25804 saved_class_ref = current_class_ref;
25805 cp_function_chain->x_current_class_ref = NULL_TREE;
25807 default_argument = cp_parser_initializer (parser);
25808 /* Restore the "this" pointer. */
25809 if (cfun)
25811 cp_function_chain->x_current_class_ptr = saved_class_ptr;
25812 cp_function_chain->x_current_class_ref = saved_class_ref;
25814 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
25815 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
25816 if (template_parm_p)
25817 pop_deferring_access_checks ();
25818 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
25819 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
25821 return default_argument;
25824 /* Parse a function-body.
25826 function-body:
25827 compound_statement */
25829 static void
25830 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
25832 cp_parser_compound_statement (parser, NULL, (in_function_try_block
25833 ? BCS_TRY_BLOCK : BCS_NORMAL),
25834 true);
25837 /* Parse a ctor-initializer-opt followed by a function-body. Return
25838 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
25839 is true we are parsing a function-try-block. */
25841 static void
25842 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
25843 bool in_function_try_block)
25845 tree body, list;
25846 const bool check_body_p
25847 = (DECL_CONSTRUCTOR_P (current_function_decl)
25848 && DECL_DECLARED_CONSTEXPR_P (current_function_decl));
25849 tree last = NULL;
25851 if (in_function_try_block
25852 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
25853 && cxx_dialect < cxx20)
25855 if (DECL_CONSTRUCTOR_P (current_function_decl))
25856 pedwarn (input_location, OPT_Wc__20_extensions,
25857 "function-try-block body of %<constexpr%> constructor only "
25858 "available with %<-std=c++20%> or %<-std=gnu++20%>");
25859 else
25860 pedwarn (input_location, OPT_Wc__20_extensions,
25861 "function-try-block body of %<constexpr%> function only "
25862 "available with %<-std=c++20%> or %<-std=gnu++20%>");
25865 /* Begin the function body. */
25866 body = begin_function_body ();
25867 /* Parse the optional ctor-initializer. */
25868 cp_parser_ctor_initializer_opt (parser);
25870 /* If we're parsing a constexpr constructor definition, we need
25871 to check that the constructor body is indeed empty. However,
25872 before we get to cp_parser_function_body lot of junk has been
25873 generated, so we can't just check that we have an empty block.
25874 Rather we take a snapshot of the outermost block, and check whether
25875 cp_parser_function_body changed its state. */
25876 if (check_body_p)
25878 list = cur_stmt_list;
25879 if (STATEMENT_LIST_TAIL (list))
25880 last = STATEMENT_LIST_TAIL (list)->stmt;
25882 /* Parse the function-body. */
25883 cp_parser_function_body (parser, in_function_try_block);
25884 if (check_body_p)
25885 check_constexpr_ctor_body (last, list, /*complain=*/true);
25886 /* Finish the function body. */
25887 finish_function_body (body);
25890 /* Parse an initializer.
25892 initializer:
25893 = initializer-clause
25894 ( expression-list )
25896 Returns an expression representing the initializer. If no
25897 initializer is present, NULL_TREE is returned.
25899 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
25900 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
25901 set to TRUE if there is no initializer present. If there is an
25902 initializer, and it is not a constant-expression, *NON_CONSTANT_P
25903 is set to true; otherwise it is set to false. */
25905 static tree
25906 cp_parser_initializer (cp_parser *parser, bool *is_direct_init /*=nullptr*/,
25907 bool *non_constant_p /*=nullptr*/, bool subexpression_p)
25909 cp_token *token;
25910 tree init;
25912 /* Peek at the next token. */
25913 token = cp_lexer_peek_token (parser->lexer);
25915 /* Let our caller know whether or not this initializer was
25916 parenthesized. */
25917 if (is_direct_init)
25918 *is_direct_init = (token->type != CPP_EQ);
25919 /* Assume that the initializer is constant. */
25920 if (non_constant_p)
25921 *non_constant_p = false;
25923 if (token->type == CPP_EQ)
25925 /* Consume the `='. */
25926 cp_lexer_consume_token (parser->lexer);
25927 /* Parse the initializer-clause. */
25928 init = cp_parser_initializer_clause (parser, non_constant_p);
25930 else if (token->type == CPP_OPEN_PAREN)
25932 vec<tree, va_gc> *vec;
25933 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
25934 /*cast_p=*/false,
25935 /*allow_expansion_p=*/true,
25936 non_constant_p);
25937 if (vec == NULL)
25938 return error_mark_node;
25939 init = build_tree_list_vec (vec);
25940 release_tree_vector (vec);
25942 else if (token->type == CPP_OPEN_BRACE)
25944 cp_lexer_set_source_position (parser->lexer);
25945 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
25946 init = cp_parser_braced_list (parser, non_constant_p);
25947 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
25949 else
25951 /* Anything else is an error. */
25952 cp_parser_error (parser, "expected initializer");
25953 init = error_mark_node;
25956 if (!subexpression_p && check_for_bare_parameter_packs (init))
25957 init = error_mark_node;
25959 return init;
25962 /* Parse an initializer-clause.
25964 initializer-clause:
25965 assignment-expression
25966 braced-init-list
25968 Returns an expression representing the initializer.
25970 If the `assignment-expression' production is used the value
25971 returned is simply a representation for the expression.
25973 Otherwise, calls cp_parser_braced_list. */
25975 static cp_expr
25976 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
25978 cp_expr initializer;
25980 /* Assume the expression is constant. */
25981 if (non_constant_p)
25982 *non_constant_p = false;
25984 /* If it is not a `{', then we are looking at an
25985 assignment-expression. */
25986 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
25988 initializer
25989 = cp_parser_constant_expression (parser,
25990 /*allow_non_constant_p=*/2,
25991 non_constant_p);
25993 else
25994 initializer = cp_parser_braced_list (parser, non_constant_p);
25996 return initializer;
25999 /* Parse a brace-enclosed initializer list.
26001 braced-init-list:
26002 { initializer-list , [opt] }
26003 { designated-initializer-list , [opt] }
26006 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
26007 the elements of the initializer-list (or NULL, if the last
26008 production is used). The TREE_TYPE for the CONSTRUCTOR will be
26009 NULL_TREE. There is no way to detect whether or not the optional
26010 trailing `,' was provided. NON_CONSTANT_P is as for
26011 cp_parser_initializer. */
26013 static cp_expr
26014 cp_parser_braced_list (cp_parser *parser, bool *non_constant_p /*=nullptr*/)
26016 tree initializer;
26017 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
26018 auto oas = make_temp_override (parser->omp_array_section_p, false);
26020 /* Consume the `{' token. */
26021 matching_braces braces;
26022 braces.require_open (parser);
26023 /* Create a CONSTRUCTOR to represent the braced-initializer. */
26024 initializer = make_node (CONSTRUCTOR);
26025 /* If it's not a `}', then there is a non-trivial initializer. */
26026 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
26028 bool designated;
26029 /* Parse the initializer list. */
26030 CONSTRUCTOR_ELTS (initializer)
26031 = cp_parser_initializer_list (parser, non_constant_p, &designated);
26032 CONSTRUCTOR_IS_DESIGNATED_INIT (initializer) = designated;
26033 /* A trailing `,' token is allowed. */
26034 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26035 cp_lexer_consume_token (parser->lexer);
26037 else if (non_constant_p)
26038 *non_constant_p = false;
26039 /* Now, there should be a trailing `}'. */
26040 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
26041 braces.require_close (parser);
26042 TREE_TYPE (initializer) = init_list_type_node;
26043 recompute_constructor_flags (initializer);
26045 cp_expr result (initializer);
26046 /* Build a location of the form:
26047 { ... }
26048 ^~~~~~~
26049 with caret==start at the open brace, finish at the close brace. */
26050 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
26051 result.set_location (combined_loc);
26052 return result;
26055 /* Consume tokens up to, but not including, the next non-nested closing `]'.
26056 Returns true iff we found a closing `]'. */
26058 static bool
26059 cp_parser_skip_up_to_closing_square_bracket (cp_parser *parser)
26061 unsigned square_depth = 0;
26063 while (true)
26065 cp_token * token = cp_lexer_peek_token (parser->lexer);
26067 switch (token->type)
26069 case CPP_PRAGMA_EOL:
26070 if (!parser->lexer->in_pragma)
26071 break;
26072 /* FALLTHRU */
26074 case CPP_EOF:
26075 /* If we've run out of tokens, then there is no closing `]'. */
26076 return false;
26078 case CPP_OPEN_SQUARE:
26079 ++square_depth;
26080 break;
26082 case CPP_CLOSE_SQUARE:
26083 if (!square_depth--)
26084 return true;
26085 break;
26087 default:
26088 break;
26091 /* Consume the current token, skipping it. */
26092 cp_lexer_consume_token (parser->lexer);
26096 /* Consume tokens up to, and including, the next non-nested closing `]'.
26097 Returns true iff we found a closing `]'. */
26099 static bool
26100 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
26102 bool found = cp_parser_skip_up_to_closing_square_bracket (parser);
26103 if (found)
26104 cp_lexer_consume_token (parser->lexer);
26105 return found;
26108 /* Return true if we are looking at an array-designator, false otherwise. */
26110 static bool
26111 cp_parser_array_designator_p (cp_parser *parser)
26113 /* Consume the `['. */
26114 cp_lexer_consume_token (parser->lexer);
26116 cp_lexer_save_tokens (parser->lexer);
26118 /* Skip tokens until the next token is a closing square bracket.
26119 If we find the closing `]', and the next token is a `=', then
26120 we are looking at an array designator. */
26121 bool array_designator_p
26122 = (cp_parser_skip_to_closing_square_bracket (parser)
26123 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
26125 /* Roll back the tokens we skipped. */
26126 cp_lexer_rollback_tokens (parser->lexer);
26128 return array_designator_p;
26131 /* Parse an initializer-list.
26133 initializer-list:
26134 initializer-clause ... [opt]
26135 initializer-list , initializer-clause ... [opt]
26137 C++20 Extension:
26139 designated-initializer-list:
26140 designated-initializer-clause
26141 designated-initializer-list , designated-initializer-clause
26143 designated-initializer-clause:
26144 designator brace-or-equal-initializer
26146 designator:
26147 . identifier
26149 GNU Extension:
26151 initializer-list:
26152 designation initializer-clause ...[opt]
26153 initializer-list , designation initializer-clause ...[opt]
26155 designation:
26156 . identifier =
26157 identifier :
26158 [ constant-expression ] =
26160 Returns a vec of constructor_elt. The VALUE of each elt is an expression
26161 for the initializer. If the INDEX of the elt is non-NULL, it is the
26162 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
26163 as for cp_parser_initializer. Set *DESIGNATED to a boolean whether there
26164 are any designators. */
26166 static vec<constructor_elt, va_gc> *
26167 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p,
26168 bool *designated)
26170 vec<constructor_elt, va_gc> *v = NULL;
26171 bool first_p = true;
26172 tree first_designator = NULL_TREE;
26174 /* Assume all of the expressions are constant. */
26175 if (non_constant_p)
26176 *non_constant_p = false;
26178 unsigned nelts = 0;
26179 int suppress = suppress_location_wrappers;
26181 /* Parse the rest of the list. */
26182 while (true)
26184 cp_token *token;
26185 tree designator;
26186 tree initializer;
26187 bool clause_non_constant_p;
26188 bool direct_p = false;
26189 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26191 /* Handle the C++20 syntax, '. id ='. */
26192 if ((cxx_dialect >= cxx20
26193 || cp_parser_allow_gnu_extensions_p (parser))
26194 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
26195 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
26196 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
26197 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
26198 == CPP_OPEN_BRACE)))
26200 if (pedantic && cxx_dialect < cxx20)
26201 pedwarn (loc, OPT_Wc__20_extensions,
26202 "C++ designated initializers only available with "
26203 "%<-std=c++20%> or %<-std=gnu++20%>");
26204 /* Consume the `.'. */
26205 cp_lexer_consume_token (parser->lexer);
26206 /* Consume the identifier. */
26207 designator = cp_lexer_consume_token (parser->lexer)->u.value;
26208 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
26209 /* Consume the `='. */
26210 cp_lexer_consume_token (parser->lexer);
26211 else
26212 direct_p = true;
26214 /* Also, if the next token is an identifier and the following one is a
26215 colon, we are looking at the GNU designated-initializer
26216 syntax. */
26217 else if (cp_parser_allow_gnu_extensions_p (parser)
26218 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
26219 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
26220 == CPP_COLON))
26222 /* Warn the user that they are using an extension. */
26223 pedwarn (loc, OPT_Wpedantic,
26224 "ISO C++ does not allow GNU designated initializers");
26225 /* Consume the identifier. */
26226 designator = cp_lexer_consume_token (parser->lexer)->u.value;
26227 /* Consume the `:'. */
26228 cp_lexer_consume_token (parser->lexer);
26230 /* Also handle C99 array designators, '[ const ] ='. */
26231 else if (cp_parser_allow_gnu_extensions_p (parser)
26232 && !c_dialect_objc ()
26233 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
26235 /* In C++11, [ could start a lambda-introducer. */
26236 bool non_const = false;
26238 cp_parser_parse_tentatively (parser);
26240 if (!cp_parser_array_designator_p (parser))
26242 cp_parser_simulate_error (parser);
26243 designator = NULL_TREE;
26245 else
26247 designator = cp_parser_constant_expression (parser, true,
26248 &non_const);
26249 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
26250 cp_parser_require (parser, CPP_EQ, RT_EQ);
26253 if (!cp_parser_parse_definitely (parser))
26254 designator = NULL_TREE;
26255 else if (non_const
26256 && (!require_potential_rvalue_constant_expression
26257 (designator)))
26258 designator = NULL_TREE;
26259 if (designator)
26260 /* Warn the user that they are using an extension. */
26261 pedwarn (loc, OPT_Wpedantic,
26262 "ISO C++ does not allow C99 designated initializers");
26264 else
26265 designator = NULL_TREE;
26267 if (first_p)
26269 first_designator = designator;
26270 first_p = false;
26272 else if (cxx_dialect >= cxx20
26273 && first_designator != error_mark_node
26274 && (!first_designator != !designator))
26276 error_at (loc, "either all initializer clauses should be designated "
26277 "or none of them should be");
26278 first_designator = error_mark_node;
26280 else if (cxx_dialect < cxx20 && !first_designator)
26281 first_designator = designator;
26283 /* Parse the initializer. */
26284 initializer = cp_parser_initializer_clause (parser,
26285 (non_constant_p != nullptr
26286 ? &clause_non_constant_p
26287 : nullptr));
26288 /* If any clause is non-constant, so is the entire initializer. */
26289 if (non_constant_p && clause_non_constant_p)
26290 *non_constant_p = true;
26292 if (TREE_CODE (initializer) == CONSTRUCTOR)
26293 /* This uses |= rather than = because C_I_D_I could have been set in
26294 cp_parser_functional_cast so we must be careful not to clear the
26295 flag. */
26296 CONSTRUCTOR_IS_DIRECT_INIT (initializer) |= direct_p;
26298 /* If we have an ellipsis, this is an initializer pack
26299 expansion. */
26300 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26302 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26304 /* Consume the `...'. */
26305 cp_lexer_consume_token (parser->lexer);
26307 if (designator && cxx_dialect >= cxx20)
26308 error_at (loc,
26309 "%<...%> not allowed in designated initializer list");
26311 /* Turn the initializer into an initializer expansion. */
26312 initializer = make_pack_expansion (initializer);
26315 /* Add it to the vector. */
26316 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
26318 /* If the next token is not a comma, we have reached the end of
26319 the list. */
26320 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
26321 break;
26323 /* Peek at the next token. */
26324 token = cp_lexer_peek_nth_token (parser->lexer, 2);
26325 /* If the next token is a `}', then we're still done. An
26326 initializer-clause can have a trailing `,' after the
26327 initializer-list and before the closing `}'. */
26328 if (token->type == CPP_CLOSE_BRACE)
26329 break;
26331 /* Suppress location wrappers in a long initializer to save memory
26332 (14179). The cutoff is chosen arbitrarily. */
26333 const unsigned loc_max = 256;
26334 unsigned incr = 1;
26335 if (TREE_CODE (initializer) == CONSTRUCTOR)
26336 /* Look one level down because it's easy. Looking deeper would require
26337 passing down a nelts pointer, and I don't think multi-level massive
26338 initializers are common enough to justify this. */
26339 incr = CONSTRUCTOR_NELTS (initializer);
26340 nelts += incr;
26341 if (nelts >= loc_max && (nelts - incr) < loc_max)
26342 ++suppress_location_wrappers;
26344 /* Consume the `,' token. */
26345 cp_lexer_consume_token (parser->lexer);
26348 /* The same identifier shall not appear in multiple designators
26349 of a designated-initializer-list. */
26350 if (first_designator)
26352 unsigned int i;
26353 tree designator, val;
26354 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
26355 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
26357 if (IDENTIFIER_MARKED (designator))
26359 error_at (cp_expr_loc_or_input_loc (val),
26360 "%<.%s%> designator used multiple times in "
26361 "the same initializer list",
26362 IDENTIFIER_POINTER (designator));
26363 (*v)[i].index = error_mark_node;
26365 else
26366 IDENTIFIER_MARKED (designator) = 1;
26368 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
26369 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
26370 IDENTIFIER_MARKED (designator) = 0;
26373 suppress_location_wrappers = suppress;
26375 *designated = first_designator != NULL_TREE;
26376 return v;
26379 /* Classes [gram.class] */
26381 /* Parse a class-name.
26383 class-name:
26384 identifier
26385 template-id
26387 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
26388 to indicate that names looked up in dependent types should be
26389 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
26390 keyword has been used to indicate that the name that appears next
26391 is a template. TAG_TYPE indicates the explicit tag given before
26392 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
26393 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
26394 is the class being defined in a class-head. If ENUM_OK is TRUE,
26395 enum-names are also accepted.
26397 Returns the TYPE_DECL representing the class. */
26399 static tree
26400 cp_parser_class_name (cp_parser *parser,
26401 bool typename_keyword_p,
26402 bool template_keyword_p,
26403 enum tag_types tag_type,
26404 bool check_dependency_p,
26405 bool class_head_p,
26406 bool is_declaration,
26407 bool enum_ok)
26409 tree decl;
26410 tree identifier = NULL_TREE;
26412 /* All class-names start with an identifier. */
26413 cp_token *token = cp_lexer_peek_token (parser->lexer);
26414 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
26416 cp_parser_error (parser, "expected class-name");
26417 return error_mark_node;
26420 /* PARSER->SCOPE can be cleared when parsing the template-arguments
26421 to a template-id, so we save it here. Consider object scope too,
26422 so that make_typename_type below can use it (cp_parser_template_name
26423 considers object scope also). This may happen with code like
26425 p->template A<T>::a()
26427 where we first want to look up A<T>::a in the class of the object
26428 expression, as per [basic.lookup.classref]. */
26429 tree scope = parser->scope ? parser->scope : parser->context->object_type;
26430 /* This only checks parser->scope to avoid duplicate errors; if
26431 ->object_type is erroneous, go on to give a parse error. */
26432 if (parser->scope == error_mark_node)
26433 return error_mark_node;
26435 /* Any name names a type if we're following the `typename' keyword
26436 in a qualified name where the enclosing scope is type-dependent. */
26437 const bool typename_p = (typename_keyword_p
26438 && parser->scope
26439 && TYPE_P (parser->scope)
26440 && dependent_scope_p (parser->scope));
26441 /* Handle the common case (an identifier, but not a template-id)
26442 efficiently. */
26443 if (token->type == CPP_NAME
26444 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
26446 cp_token *identifier_token;
26447 bool ambiguous_p;
26449 /* Look for the identifier. */
26450 identifier_token = cp_lexer_peek_token (parser->lexer);
26451 ambiguous_p = identifier_token->error_reported;
26452 identifier = cp_parser_identifier (parser);
26453 /* If the next token isn't an identifier, we are certainly not
26454 looking at a class-name. */
26455 if (identifier == error_mark_node)
26456 decl = error_mark_node;
26457 /* If we know this is a type-name, there's no need to look it
26458 up. */
26459 else if (typename_p)
26460 decl = identifier;
26461 else
26463 tree ambiguous_decls;
26464 /* If we already know that this lookup is ambiguous, then
26465 we've already issued an error message; there's no reason
26466 to check again. */
26467 if (ambiguous_p)
26469 cp_parser_simulate_error (parser);
26470 return error_mark_node;
26472 /* If the next token is a `::', then the name must be a type
26473 name.
26475 [basic.lookup.qual]
26477 During the lookup for a name preceding the :: scope
26478 resolution operator, object, function, and enumerator
26479 names are ignored. */
26480 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
26481 tag_type = scope_type;
26482 /* Look up the name. */
26483 decl = cp_parser_lookup_name (parser, identifier,
26484 tag_type,
26485 /*is_template=*/false,
26486 /*is_namespace=*/false,
26487 check_dependency_p,
26488 &ambiguous_decls,
26489 identifier_token->location);
26490 if (ambiguous_decls)
26492 if (cp_parser_parsing_tentatively (parser))
26493 cp_parser_simulate_error (parser);
26494 return error_mark_node;
26498 else
26500 /* Try a template-id. */
26501 decl = cp_parser_template_id (parser, template_keyword_p,
26502 check_dependency_p,
26503 tag_type,
26504 is_declaration);
26505 if (decl == error_mark_node)
26506 return error_mark_node;
26509 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
26511 /* If this is a typename, create a TYPENAME_TYPE. */
26512 if (typename_p && decl != error_mark_node)
26514 decl = make_typename_type (scope, decl, typename_type,
26515 /*complain=*/tf_error);
26516 if (decl != error_mark_node)
26517 decl = TYPE_NAME (decl);
26520 decl = strip_using_decl (decl);
26522 /* Check to see that it is really the name of a class. */
26523 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
26524 && identifier_p (TREE_OPERAND (decl, 0))
26525 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
26526 /* Situations like this:
26528 template <typename T> struct A {
26529 typename T::template X<int>::I i;
26532 are problematic. Is `T::template X<int>' a class-name? The
26533 standard does not seem to be definitive, but there is no other
26534 valid interpretation of the following `::'. Therefore, those
26535 names are considered class-names. */
26537 decl = make_typename_type (scope, decl, tag_type, tf_error);
26538 if (decl != error_mark_node)
26539 decl = TYPE_NAME (decl);
26541 else if (TREE_CODE (decl) != TYPE_DECL
26542 || TREE_TYPE (decl) == error_mark_node
26543 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
26544 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
26545 /* In Objective-C 2.0, a classname followed by '.' starts a
26546 dot-syntax expression, and it's not a type-name. */
26547 || (c_dialect_objc ()
26548 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
26549 && objc_is_class_name (decl)))
26550 decl = error_mark_node;
26552 if (decl == error_mark_node)
26553 cp_parser_error (parser, "expected class-name");
26554 else if (identifier && !parser->scope)
26555 maybe_note_name_used_in_class (identifier, decl);
26557 return decl;
26560 /* Make sure that any member-function parameters are in scope.
26561 For instance, a function's noexcept-specifier can use the function's
26562 parameters:
26564 struct S {
26565 void fn (int p) noexcept(noexcept(p));
26568 so we need to make sure name lookup can find them. This is used
26569 when we delay parsing of the noexcept-specifier. */
26571 static void
26572 inject_parm_decls (tree decl)
26574 begin_scope (sk_function_parms, decl);
26575 tree args = DECL_ARGUMENTS (decl);
26577 do_push_parm_decls (decl, args, /*nonparms=*/NULL);
26579 if (args && is_this_parameter (args))
26581 gcc_checking_assert (current_class_ptr == NULL_TREE);
26582 current_class_ref = cp_build_fold_indirect_ref (args);
26583 current_class_ptr = args;
26587 /* Undo the effects of inject_parm_decls. */
26589 static void
26590 pop_injected_parms (void)
26592 pop_bindings_and_leave_scope ();
26593 current_class_ptr = current_class_ref = NULL_TREE;
26596 /* Parse a class-specifier.
26598 class-specifier:
26599 class-head { member-specification [opt] }
26601 Returns the TREE_TYPE representing the class. */
26603 tree
26604 cp_parser_class_specifier (cp_parser* parser)
26606 auto_timevar tv (TV_PARSE_STRUCT);
26608 tree type;
26609 tree attributes = NULL_TREE;
26610 bool nested_name_specifier_p;
26611 unsigned saved_num_template_parameter_lists;
26612 bool saved_in_function_body;
26613 unsigned char in_statement;
26614 bool in_switch_statement_p;
26615 bool saved_in_unbraced_linkage_specification_p;
26616 tree old_scope = NULL_TREE;
26617 tree scope = NULL_TREE;
26618 cp_token *closing_brace;
26620 push_deferring_access_checks (dk_no_deferred);
26622 /* Parse the class-head. */
26623 type = cp_parser_class_head (parser,
26624 &nested_name_specifier_p);
26625 /* If the class-head was a semantic disaster, skip the entire body
26626 of the class. */
26627 if (!type)
26629 cp_parser_skip_to_end_of_block_or_statement (parser);
26630 pop_deferring_access_checks ();
26631 return error_mark_node;
26634 /* Look for the `{'. */
26635 matching_braces braces;
26636 if (!braces.require_open (parser))
26638 pop_deferring_access_checks ();
26639 return error_mark_node;
26642 cp_ensure_no_omp_declare_simd (parser);
26643 cp_ensure_no_oacc_routine (parser);
26645 /* Issue an error message if type-definitions are forbidden here. */
26646 bool type_definition_ok_p = cp_parser_check_type_definition (parser);
26647 /* Remember that we are defining one more class. */
26648 ++parser->num_classes_being_defined;
26649 /* Inside the class, surrounding template-parameter-lists do not
26650 apply. */
26651 saved_num_template_parameter_lists
26652 = parser->num_template_parameter_lists;
26653 parser->num_template_parameter_lists = 0;
26654 /* We are not in a function body. */
26655 saved_in_function_body = parser->in_function_body;
26656 parser->in_function_body = false;
26657 /* Or in a loop. */
26658 in_statement = parser->in_statement;
26659 parser->in_statement = 0;
26660 /* Or in a switch. */
26661 in_switch_statement_p = parser->in_switch_statement_p;
26662 parser->in_switch_statement_p = false;
26663 /* We are not immediately inside an extern "lang" block. */
26664 saved_in_unbraced_linkage_specification_p
26665 = parser->in_unbraced_linkage_specification_p;
26666 parser->in_unbraced_linkage_specification_p = false;
26667 /* 'this' from an enclosing non-static member function is unavailable. */
26668 tree saved_ccp = current_class_ptr;
26669 tree saved_ccr = current_class_ref;
26670 current_class_ptr = NULL_TREE;
26671 current_class_ref = NULL_TREE;
26673 /* Start the class. */
26674 if (nested_name_specifier_p)
26676 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
26677 /* SCOPE must be a scope nested inside current scope. */
26678 if (is_nested_namespace (current_namespace,
26679 decl_namespace_context (scope)))
26680 old_scope = push_inner_scope (scope);
26681 else
26682 nested_name_specifier_p = false;
26684 type = begin_class_definition (type);
26686 if (type == error_mark_node)
26687 /* If the type is erroneous, skip the entire body of the class. */
26688 cp_parser_skip_to_closing_brace (parser);
26689 else
26690 /* Parse the member-specification. */
26691 cp_parser_member_specification_opt (parser);
26693 /* Look for the trailing `}'. */
26694 closing_brace = braces.require_close (parser);
26695 /* Look for trailing attributes to apply to this class. */
26696 if (cp_parser_allow_gnu_extensions_p (parser))
26697 attributes = cp_parser_gnu_attributes_opt (parser);
26698 if (type != error_mark_node)
26699 type = finish_struct (type, attributes);
26700 if (nested_name_specifier_p)
26701 pop_inner_scope (old_scope, scope);
26703 /* We've finished a type definition. Check for the common syntax
26704 error of forgetting a semicolon after the definition. We need to
26705 be careful, as we can't just check for not-a-semicolon and be done
26706 with it; the user might have typed:
26708 class X { } c = ...;
26709 class X { } *p = ...;
26711 and so forth. Instead, enumerate all the possible tokens that
26712 might follow this production; if we don't see one of them, then
26713 complain and silently insert the semicolon. */
26715 cp_token *token = cp_lexer_peek_token (parser->lexer);
26716 bool want_semicolon = true;
26718 if (cp_next_tokens_can_be_std_attribute_p (parser))
26719 /* Don't try to parse c++11 attributes here. As per the
26720 grammar, that should be a task for
26721 cp_parser_decl_specifier_seq. */
26722 want_semicolon = false;
26724 switch (token->type)
26726 case CPP_NAME:
26727 case CPP_SEMICOLON:
26728 case CPP_MULT:
26729 case CPP_AND:
26730 case CPP_OPEN_PAREN:
26731 case CPP_CLOSE_PAREN:
26732 case CPP_COMMA:
26733 case CPP_SCOPE:
26734 want_semicolon = false;
26735 break;
26737 /* While it's legal for type qualifiers and storage class
26738 specifiers to follow type definitions in the grammar, only
26739 compiler testsuites contain code like that. Assume that if
26740 we see such code, then what we're really seeing is a case
26741 like:
26743 class X { }
26744 const <type> var = ...;
26748 class Y { }
26749 static <type> func (...) ...
26751 i.e. the qualifier or specifier applies to the next
26752 declaration. To do so, however, we need to look ahead one
26753 more token to see if *that* token is a type specifier.
26755 This code could be improved to handle:
26757 class Z { }
26758 static const <type> var = ...; */
26759 case CPP_KEYWORD:
26760 if (keyword_is_decl_specifier (token->keyword))
26762 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
26764 /* Handling user-defined types here would be nice, but very
26765 tricky. */
26766 want_semicolon
26767 = (lookahead->type == CPP_KEYWORD
26768 && keyword_begins_type_specifier (lookahead->keyword));
26770 break;
26771 default:
26772 break;
26775 /* If we don't have a type, then something is very wrong and we
26776 shouldn't try to do anything clever. Likewise for not seeing the
26777 closing brace. */
26778 if (closing_brace && TYPE_P (type) && want_semicolon)
26780 /* Locate the closing brace. */
26781 cp_token_position prev
26782 = cp_lexer_previous_token_position (parser->lexer);
26783 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
26784 location_t loc = prev_token->location;
26786 /* We want to suggest insertion of a ';' immediately *after* the
26787 closing brace, so, if we can, offset the location by 1 column. */
26788 location_t next_loc = loc;
26789 if (!linemap_location_from_macro_expansion_p (line_table, loc))
26790 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
26792 rich_location richloc (line_table, next_loc);
26794 /* If we successfully offset the location, suggest the fix-it. */
26795 if (next_loc != loc)
26796 richloc.add_fixit_insert_before (next_loc, ";");
26798 if (CLASSTYPE_DECLARED_CLASS (type))
26799 error_at (&richloc,
26800 "expected %<;%> after class definition");
26801 else if (TREE_CODE (type) == RECORD_TYPE)
26802 error_at (&richloc,
26803 "expected %<;%> after struct definition");
26804 else if (TREE_CODE (type) == UNION_TYPE)
26805 error_at (&richloc,
26806 "expected %<;%> after union definition");
26807 else
26808 gcc_unreachable ();
26810 /* Unget one token and smash it to look as though we encountered
26811 a semicolon in the input stream. */
26812 cp_lexer_set_token_position (parser->lexer, prev);
26813 token = cp_lexer_peek_token (parser->lexer);
26814 token->type = CPP_SEMICOLON;
26815 token->keyword = RID_MAX;
26819 /* If this class is not itself within the scope of another class,
26820 then we need to parse the bodies of all of the queued function
26821 definitions. Note that the queued functions defined in a class
26822 are not always processed immediately following the
26823 class-specifier for that class. Consider:
26825 struct A {
26826 struct B { void f() { sizeof (A); } };
26829 If `f' were processed before the processing of `A' were
26830 completed, there would be no way to compute the size of `A'.
26831 Note that the nesting we are interested in here is lexical --
26832 not the semantic nesting given by TYPE_CONTEXT. In particular,
26833 for:
26835 struct A { struct B; };
26836 struct A::B { void f() { } };
26838 there is no need to delay the parsing of `A::B::f'. */
26839 if (--parser->num_classes_being_defined == 0)
26841 tree decl;
26842 tree class_type = NULL_TREE;
26843 tree pushed_scope = NULL_TREE;
26844 unsigned ix;
26845 cp_default_arg_entry *e;
26847 if (!type_definition_ok_p || any_erroneous_template_args_p (type))
26849 /* Skip default arguments, NSDMIs, etc, in order to improve
26850 error recovery (c++/71169, c++/71832). */
26851 vec_safe_truncate (unparsed_funs_with_default_args, 0);
26852 vec_safe_truncate (unparsed_nsdmis, 0);
26853 vec_safe_truncate (unparsed_funs_with_definitions, 0);
26856 /* In a first pass, parse default arguments to the functions.
26857 Then, in a second pass, parse the bodies of the functions.
26858 This two-phased approach handles cases like:
26860 struct S {
26861 void f() { g(); }
26862 void g(int i = 3);
26866 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
26868 decl = e->decl;
26869 /* If there are default arguments that have not yet been processed,
26870 take care of them now. */
26871 if (class_type != e->class_type)
26873 if (pushed_scope)
26874 pop_scope (pushed_scope);
26875 class_type = e->class_type;
26876 pushed_scope = push_scope (class_type);
26878 /* Make sure that any template parameters are in scope. */
26879 maybe_begin_member_template_processing (decl);
26880 /* Parse the default argument expressions. */
26881 cp_parser_late_parsing_default_args (parser, decl);
26882 /* Remove any template parameters from the symbol table. */
26883 maybe_end_member_template_processing ();
26885 vec_safe_truncate (unparsed_funs_with_default_args, 0);
26887 /* If there are noexcept-specifiers that have not yet been processed,
26888 take care of them now. Do this before processing NSDMIs as they
26889 may depend on noexcept-specifiers already having been processed. */
26890 FOR_EACH_VEC_SAFE_ELT (unparsed_noexcepts, ix, decl)
26892 tree ctx = DECL_CONTEXT (decl);
26893 if (class_type != ctx)
26895 if (pushed_scope)
26896 pop_scope (pushed_scope);
26897 class_type = ctx;
26898 pushed_scope = push_scope (class_type);
26901 tree def_parse = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl));
26902 def_parse = TREE_PURPOSE (def_parse);
26904 /* Make sure that any template parameters are in scope. */
26905 maybe_begin_member_template_processing (decl);
26907 /* Make sure that any member-function parameters are in scope.
26908 This function doesn't expect ccp to be set. */
26909 current_class_ptr = current_class_ref = NULL_TREE;
26910 inject_parm_decls (decl);
26912 /* 'this' is not allowed in static member functions. */
26913 unsigned char local_variables_forbidden_p
26914 = parser->local_variables_forbidden_p;
26915 if (DECL_THIS_STATIC (decl))
26916 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
26918 /* Now we can parse the noexcept-specifier. */
26919 tree spec = cp_parser_late_noexcept_specifier (parser, def_parse);
26921 if (spec == error_mark_node)
26922 spec = NULL_TREE;
26924 /* Update the fn's type directly -- it might have escaped
26925 beyond this decl :( */
26926 fixup_deferred_exception_variants (TREE_TYPE (decl), spec);
26927 /* Update any instantiations we've already created. We must
26928 keep the new noexcept-specifier wrapped in a DEFERRED_NOEXCEPT
26929 so that maybe_instantiate_noexcept can tsubst the NOEXCEPT_EXPR
26930 in the pattern. */
26931 for (tree i : DEFPARSE_INSTANTIATIONS (def_parse))
26932 DEFERRED_NOEXCEPT_PATTERN (TREE_PURPOSE (i))
26933 = spec ? TREE_PURPOSE (spec) : error_mark_node;
26935 /* Restore the state of local_variables_forbidden_p. */
26936 parser->local_variables_forbidden_p = local_variables_forbidden_p;
26938 /* The finish_struct call above performed various override checking,
26939 but it skipped unparsed noexcept-specifier operands. Now that we
26940 have resolved them, check again. */
26941 noexcept_override_late_checks (decl);
26943 /* Remove any member-function parameters from the symbol table. */
26944 pop_injected_parms ();
26946 /* Remove any template parameters from the symbol table. */
26947 maybe_end_member_template_processing ();
26949 vec_safe_truncate (unparsed_noexcepts, 0);
26951 /* Now parse any NSDMIs. */
26952 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
26954 tree ctx = type_context_for_name_lookup (decl);
26955 if (class_type != ctx)
26957 if (pushed_scope)
26958 pop_scope (pushed_scope);
26959 class_type = ctx;
26960 pushed_scope = push_scope (class_type);
26962 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
26963 cp_parser_late_parsing_nsdmi (parser, decl);
26965 vec_safe_truncate (unparsed_nsdmis, 0);
26967 /* Now contract attributes. */
26968 FOR_EACH_VEC_SAFE_ELT (unparsed_contracts, ix, decl)
26970 tree ctx = DECL_CONTEXT (decl);
26971 if (class_type != ctx)
26973 if (pushed_scope)
26974 pop_scope (pushed_scope);
26975 class_type = ctx;
26976 pushed_scope = push_scope (class_type);
26979 temp_override<tree> cfd(current_function_decl, decl);
26981 /* Make sure that any template parameters are in scope. */
26982 maybe_begin_member_template_processing (decl);
26984 /* Make sure that any member-function parameters are in scope.
26985 This function doesn't expect ccp to be set. */
26986 current_class_ptr = current_class_ref = NULL_TREE;
26987 inject_parm_decls (decl);
26989 /* 'this' is not allowed in static member functions. */
26990 unsigned char local_variables_forbidden_p
26991 = parser->local_variables_forbidden_p;
26992 if (DECL_THIS_STATIC (decl))
26993 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
26995 /* Now we can parse contract conditions. */
26996 for (tree a = DECL_ATTRIBUTES (decl); a; a = TREE_CHAIN (a))
26998 if (cxx_contract_attribute_p (a))
26999 cp_parser_late_contract_condition (parser, decl, a);
27002 /* Restore the state of local_variables_forbidden_p. */
27003 parser->local_variables_forbidden_p = local_variables_forbidden_p;
27005 /* Remove any member-function parameters from the symbol table. */
27006 pop_injected_parms ();
27008 /* Remove any template parameters from the symbol table. */
27009 maybe_end_member_template_processing ();
27011 /* Perform any deferred contract matching. */
27012 match_deferred_contracts (decl);
27014 vec_safe_truncate (unparsed_contracts, 0);
27016 current_class_ptr = NULL_TREE;
27017 current_class_ref = NULL_TREE;
27018 if (pushed_scope)
27019 pop_scope (pushed_scope);
27021 /* Now parse the body of the functions. */
27022 if (flag_openmp)
27024 /* OpenMP UDRs need to be parsed before all other functions. */
27025 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
27026 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
27027 cp_parser_late_parsing_for_member (parser, decl);
27028 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
27029 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
27030 cp_parser_late_parsing_for_member (parser, decl);
27032 else
27033 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
27034 cp_parser_late_parsing_for_member (parser, decl);
27035 vec_safe_truncate (unparsed_funs_with_definitions, 0);
27038 /* Put back any saved access checks. */
27039 pop_deferring_access_checks ();
27041 /* Restore saved state. */
27042 parser->in_switch_statement_p = in_switch_statement_p;
27043 parser->in_statement = in_statement;
27044 parser->in_function_body = saved_in_function_body;
27045 parser->num_template_parameter_lists
27046 = saved_num_template_parameter_lists;
27047 parser->in_unbraced_linkage_specification_p
27048 = saved_in_unbraced_linkage_specification_p;
27049 current_class_ptr = saved_ccp;
27050 current_class_ref = saved_ccr;
27052 return type;
27055 /* Parse a class-head.
27057 class-head:
27058 class-key identifier [opt] base-clause [opt]
27059 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
27060 class-key nested-name-specifier [opt] template-id
27061 base-clause [opt]
27063 class-virt-specifier:
27064 final
27066 GNU Extensions:
27067 class-key attributes identifier [opt] base-clause [opt]
27068 class-key attributes nested-name-specifier identifier base-clause [opt]
27069 class-key attributes nested-name-specifier [opt] template-id
27070 base-clause [opt]
27072 Upon return BASES is initialized to the list of base classes (or
27073 NULL, if there are none) in the same form returned by
27074 cp_parser_base_clause.
27076 Returns the TYPE of the indicated class. Sets
27077 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
27078 involving a nested-name-specifier was used, and FALSE otherwise.
27080 Returns error_mark_node if this is not a class-head.
27082 Returns NULL_TREE if the class-head is syntactically valid, but
27083 semantically invalid in a way that means we should skip the entire
27084 body of the class. */
27086 static tree
27087 cp_parser_class_head (cp_parser* parser,
27088 bool* nested_name_specifier_p)
27090 tree nested_name_specifier;
27091 enum tag_types class_key;
27092 tree id = NULL_TREE;
27093 tree type = NULL_TREE;
27094 tree attributes;
27095 tree bases;
27096 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
27097 bool template_id_p = false;
27098 bool qualified_p = false;
27099 bool invalid_nested_name_p = false;
27100 bool invalid_explicit_specialization_p = false;
27101 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27102 tree pushed_scope = NULL_TREE;
27103 unsigned num_templates;
27104 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
27105 /* Assume no nested-name-specifier will be present. */
27106 *nested_name_specifier_p = false;
27107 /* Assume no template parameter lists will be used in defining the
27108 type. */
27109 num_templates = 0;
27110 parser->colon_corrects_to_scope_p = false;
27112 /* Look for the class-key. */
27113 class_key = cp_parser_class_key (parser);
27114 if (class_key == none_type)
27115 return error_mark_node;
27117 location_t class_head_start_location = input_location;
27119 /* Parse the attributes. */
27120 attributes = cp_parser_attributes_opt (parser);
27121 if (find_contract (attributes))
27122 diagnose_misapplied_contracts (attributes);
27124 /* If the next token is `::', that is invalid -- but sometimes
27125 people do try to write:
27127 struct ::S {};
27129 Handle this gracefully by accepting the extra qualifier, and then
27130 issuing an error about it later if this really is a
27131 class-head. If it turns out just to be an elaborated type
27132 specifier, remain silent. */
27133 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
27134 qualified_p = true;
27136 /* It is OK to define an inaccessible class; for example:
27138 class A { class B; };
27139 class A::B {};
27141 So we want to ignore access when parsing the class name.
27142 However, we might be tentatively parsing what is really an
27143 elaborated-type-specifier naming a template-id, e.g.
27145 struct C<&D::m> c;
27147 In this case the tentative parse as a class-head will fail, but not
27148 before cp_parser_template_id splices in a CPP_TEMPLATE_ID token.
27149 Since dk_no_check is sticky, we must instead use dk_deferred so that
27150 any such CPP_TEMPLATE_ID token created during this tentative parse
27151 will correctly capture the access checks imposed by the template-id . */
27152 push_deferring_access_checks (dk_deferred);
27154 /* Determine the name of the class. Begin by looking for an
27155 optional nested-name-specifier. */
27156 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
27157 nested_name_specifier
27158 = cp_parser_nested_name_specifier_opt (parser,
27159 /*typename_keyword_p=*/false,
27160 /*check_dependency_p=*/false,
27161 /*type_p=*/true,
27162 /*is_declaration=*/false);
27163 /* If there was a nested-name-specifier, then there *must* be an
27164 identifier. */
27166 cp_token *bad_template_keyword = NULL;
27168 if (nested_name_specifier)
27170 type_start_token = cp_lexer_peek_token (parser->lexer);
27171 /* Although the grammar says `identifier', it really means
27172 `class-name' or `template-name'. You are only allowed to
27173 define a class that has already been declared with this
27174 syntax.
27176 The proposed resolution for Core Issue 180 says that wherever
27177 you see `class T::X' you should treat `X' as a type-name.
27179 We do not know if we will see a class-name, or a
27180 template-name. We look for a class-name first, in case the
27181 class-name is a template-id; if we looked for the
27182 template-name first we would stop after the template-name. */
27183 cp_parser_parse_tentatively (parser);
27184 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27185 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
27186 type = cp_parser_class_name (parser,
27187 /*typename_keyword_p=*/false,
27188 /*template_keyword_p=*/false,
27189 class_type,
27190 /*check_dependency_p=*/false,
27191 /*class_head_p=*/true,
27192 /*is_declaration=*/false);
27193 /* If that didn't work, ignore the nested-name-specifier. */
27194 if (!cp_parser_parse_definitely (parser))
27196 invalid_nested_name_p = true;
27197 type_start_token = cp_lexer_peek_token (parser->lexer);
27198 id = cp_parser_identifier (parser);
27199 if (id == error_mark_node)
27200 id = NULL_TREE;
27202 /* If we could not find a corresponding TYPE, treat this
27203 declaration like an unqualified declaration. */
27204 if (type == error_mark_node)
27205 nested_name_specifier = NULL_TREE;
27206 /* Otherwise, count the number of templates used in TYPE and its
27207 containing scopes. */
27208 else
27209 num_templates = num_template_headers_for_class (TREE_TYPE (type));
27211 /* Otherwise, the identifier is optional. */
27212 else
27214 /* We don't know whether what comes next is a template-id,
27215 an identifier, or nothing at all. */
27216 cp_parser_parse_tentatively (parser);
27217 /* Check for a template-id. */
27218 type_start_token = cp_lexer_peek_token (parser->lexer);
27219 id = cp_parser_template_id (parser,
27220 /*template_keyword_p=*/false,
27221 /*check_dependency_p=*/true,
27222 class_key,
27223 /*is_declaration=*/true);
27224 /* If that didn't work, it could still be an identifier. */
27225 if (!cp_parser_parse_definitely (parser))
27227 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27229 type_start_token = cp_lexer_peek_token (parser->lexer);
27230 id = cp_parser_identifier (parser);
27232 else
27233 id = NULL_TREE;
27235 else
27237 template_id_p = true;
27238 ++num_templates;
27242 pop_deferring_access_checks ();
27244 if (id)
27246 cp_parser_check_for_invalid_template_id (parser, id,
27247 class_key,
27248 type_start_token->location);
27250 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
27252 /* If it's not a `:' or a `{' then we can't really be looking at a
27253 class-head, since a class-head only appears as part of a
27254 class-specifier. We have to detect this situation before calling
27255 xref_tag, since that has irreversible side-effects. */
27256 if (!cp_parser_next_token_starts_class_definition_p (parser))
27258 cp_parser_error (parser, "expected %<{%> or %<:%>");
27259 type = error_mark_node;
27260 goto out;
27263 /* At this point, we're going ahead with the class-specifier, even
27264 if some other problem occurs. */
27265 cp_parser_commit_to_tentative_parse (parser);
27266 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
27268 cp_parser_error (parser,
27269 "cannot specify %<override%> for a class");
27270 type = error_mark_node;
27271 goto out;
27273 /* Issue the error about the overly-qualified name now. */
27274 if (qualified_p)
27276 cp_parser_error (parser,
27277 "global qualification of class name is invalid");
27278 type = error_mark_node;
27279 goto out;
27281 else if (invalid_nested_name_p)
27283 cp_parser_error (parser,
27284 "qualified name does not name a class");
27285 type = error_mark_node;
27286 goto out;
27288 else if (nested_name_specifier)
27290 tree scope;
27292 if (bad_template_keyword)
27293 /* [temp.names]: in a qualified-id formed by a class-head-name, the
27294 keyword template shall not appear at the top level. */
27295 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
27296 "keyword %<template%> not allowed in class-head-name");
27298 /* Reject typedef-names in class heads. */
27299 if (!DECL_IMPLICIT_TYPEDEF_P (type))
27301 error_at (type_start_token->location,
27302 "invalid class name in declaration of %qD",
27303 type);
27304 type = NULL_TREE;
27305 goto done;
27308 /* Figure out in what scope the declaration is being placed. */
27309 scope = current_scope ();
27310 /* If that scope does not contain the scope in which the
27311 class was originally declared, the program is invalid. */
27312 if (scope && !is_ancestor (scope, nested_name_specifier))
27314 if (at_namespace_scope_p ())
27315 error_at (type_start_token->location,
27316 "declaration of %qD in namespace %qD which does not "
27317 "enclose %qD",
27318 type, scope, nested_name_specifier);
27319 else
27320 error_at (type_start_token->location,
27321 "declaration of %qD in %qD which does not enclose %qD",
27322 type, scope, nested_name_specifier);
27323 type = NULL_TREE;
27324 goto done;
27326 /* [dcl.meaning]
27328 A declarator-id shall not be qualified except for the
27329 definition of a ... nested class outside of its class
27330 ... [or] the definition or explicit instantiation of a
27331 class member of a namespace outside of its namespace. */
27332 if (scope == nested_name_specifier)
27333 permerror (nested_name_specifier_token_start->location,
27334 "extra qualification not allowed");
27336 /* An explicit-specialization must be preceded by "template <>". If
27337 it is not, try to recover gracefully. */
27338 if (at_namespace_scope_p ()
27339 && parser->num_template_parameter_lists == 0
27340 && !processing_template_parmlist
27341 && template_id_p)
27343 /* Build a location of this form:
27344 struct typename <ARGS>
27345 ^~~~~~~~~~~~~~~~~~~~~~
27346 with caret==start at the start token, and
27347 finishing at the end of the type. */
27348 location_t reported_loc
27349 = make_location (class_head_start_location,
27350 class_head_start_location,
27351 get_finish (type_start_token->location));
27352 rich_location richloc (line_table, reported_loc);
27353 richloc.add_fixit_insert_before (class_head_start_location,
27354 "template <> ");
27355 error_at (&richloc,
27356 "an explicit specialization must be preceded by"
27357 " %<template <>%>");
27358 invalid_explicit_specialization_p = true;
27359 /* Take the same action that would have been taken by
27360 cp_parser_explicit_specialization. */
27361 ++parser->num_template_parameter_lists;
27362 begin_specialization ();
27364 /* There must be no "return" statements between this point and the
27365 end of this function; set "type "to the correct return value and
27366 use "goto done;" to return. */
27367 /* Make sure that the right number of template parameters were
27368 present. */
27369 if (!cp_parser_check_template_parameters (parser, num_templates,
27370 template_id_p,
27371 type_start_token->location,
27372 /*declarator=*/NULL))
27374 /* If something went wrong, there is no point in even trying to
27375 process the class-definition. */
27376 type = NULL_TREE;
27377 goto done;
27380 /* Look up the type. */
27381 if (template_id_p)
27383 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
27384 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
27385 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
27387 error_at (type_start_token->location,
27388 "function template %qD redeclared as a class template", id);
27389 type = error_mark_node;
27391 else
27393 type = TREE_TYPE (id);
27394 type = maybe_process_partial_specialization (type);
27396 /* Check the scope while we still know whether or not we had a
27397 nested-name-specifier. */
27398 if (type != error_mark_node)
27399 check_unqualified_spec_or_inst (type, type_start_token->location);
27401 if (nested_name_specifier)
27402 pushed_scope = push_scope (nested_name_specifier);
27404 else if (nested_name_specifier)
27406 type = TREE_TYPE (type);
27408 /* Given:
27410 template <typename T> struct S { struct T };
27411 template <typename T> struct S<T>::T { };
27413 we will get a TYPENAME_TYPE when processing the definition of
27414 `S::T'. We need to resolve it to the actual type before we
27415 try to define it. */
27416 if (TREE_CODE (type) == TYPENAME_TYPE)
27418 type = resolve_typename_type (type, /*only_current_p=*/false);
27419 if (TREE_CODE (type) == TYPENAME_TYPE)
27421 cp_parser_error (parser, "could not resolve typename type");
27422 type = error_mark_node;
27426 type = maybe_process_partial_specialization (type);
27427 if (type == error_mark_node)
27429 type = NULL_TREE;
27430 goto done;
27433 /* Enter the scope indicated by the nested-name-specifier. */
27434 pushed_scope = push_scope (nested_name_specifier);
27435 /* Get the canonical version of this type. */
27436 type = TYPE_MAIN_DECL (type);
27437 /* Call push_template_decl if it seems like we should be defining a
27438 template either from the template headers or the type we're
27439 defining, so that we diagnose both extra and missing headers. */
27440 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
27441 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
27442 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
27444 type = push_template_decl (type);
27445 if (type == error_mark_node)
27447 type = NULL_TREE;
27448 goto done;
27452 type = TREE_TYPE (type);
27453 *nested_name_specifier_p = true;
27455 else /* The name is not a nested name. */
27457 /* If the class was unnamed, create a dummy name. */
27458 if (!id)
27459 id = make_anon_name ();
27460 TAG_how how = (parser->in_type_id_in_expr_p
27461 ? TAG_how::INNERMOST_NON_CLASS
27462 : TAG_how::CURRENT_ONLY);
27463 type = xref_tag (class_key, id, how,
27464 parser->num_template_parameter_lists);
27467 /* Diagnose class/struct/union mismatches. */
27468 cp_parser_check_class_key (parser, UNKNOWN_LOCATION, class_key, type,
27469 true, true);
27471 /* Indicate whether this class was declared as a `class' or as a
27472 `struct'. */
27473 if (TREE_CODE (type) == RECORD_TYPE)
27474 CLASSTYPE_DECLARED_CLASS (type) = class_key == class_type;
27476 /* If this type was already complete, and we see another definition,
27477 that's an error. Likewise if the type is already being defined:
27478 this can happen, eg, when it's defined from within an expression
27479 (c++/84605). */
27480 if (type != error_mark_node
27481 && (COMPLETE_TYPE_P (type) || TYPE_BEING_DEFINED (type)))
27483 error_at (type_start_token->location, "redefinition of %q#T",
27484 type);
27485 inform (location_of (type), "previous definition of %q#T",
27486 type);
27487 type = NULL_TREE;
27488 goto done;
27490 else if (type == error_mark_node)
27491 type = NULL_TREE;
27493 if (type)
27495 if (current_lambda_expr ()
27496 && uses_parameter_packs (attributes))
27498 /* In a lambda this should work, but doesn't currently. */
27499 sorry ("unexpanded parameter pack in local class in lambda");
27500 attributes = NULL_TREE;
27503 /* Apply attributes now, before any use of the class as a template
27504 argument in its base list. */
27505 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
27506 fixup_attribute_variants (type);
27509 /* Associate constraints with the type. */
27510 if (flag_concepts)
27511 type = associate_classtype_constraints (type);
27513 /* We will have entered the scope containing the class; the names of
27514 base classes should be looked up in that context. For example:
27516 struct A { struct B {}; struct C; };
27517 struct A::C : B {};
27519 is valid. */
27521 /* Get the list of base-classes, if there is one. Defer access checking
27522 until the entire list has been seen, as per [class.access.general]. */
27523 push_deferring_access_checks (dk_deferred);
27524 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27526 if (type)
27527 pushclass (type);
27528 bases = cp_parser_base_clause (parser);
27529 if (type)
27530 popclass ();
27532 else
27533 bases = NULL_TREE;
27535 /* If we're really defining a class, process the base classes.
27536 If they're invalid, fail. */
27537 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27538 xref_basetypes (type, bases);
27540 /* Now that all bases have been seen and attached to the class, check
27541 accessibility of the types named in the base-clause. This must be
27542 done relative to the class scope, so that we accept e.g.
27544 struct A { protected: struct B {}; };
27545 struct C : A::B, A {}; // OK: A::B is accessible via base A
27547 as per [class.access.general]. */
27548 if (type)
27549 pushclass (type);
27550 pop_to_parent_deferring_access_checks ();
27551 if (type)
27552 popclass ();
27554 done:
27555 /* Leave the scope given by the nested-name-specifier. We will
27556 enter the class scope itself while processing the members. */
27557 if (pushed_scope)
27558 pop_scope (pushed_scope);
27560 if (invalid_explicit_specialization_p)
27562 end_specialization ();
27563 --parser->num_template_parameter_lists;
27566 if (type)
27567 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
27568 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
27569 CLASSTYPE_FINAL (type) = 1;
27570 out:
27571 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27572 return type;
27575 /* Parse a class-key.
27577 class-key:
27578 class
27579 struct
27580 union
27582 Returns the kind of class-key specified, or none_type to indicate
27583 error. */
27585 static enum tag_types
27586 cp_parser_class_key (cp_parser* parser)
27588 cp_token *token;
27589 enum tag_types tag_type;
27591 /* Look for the class-key. */
27592 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
27593 if (!token)
27594 return none_type;
27596 /* Check to see if the TOKEN is a class-key. */
27597 tag_type = cp_parser_token_is_class_key (token);
27598 if (!tag_type)
27599 cp_parser_error (parser, "expected class-key");
27600 return tag_type;
27603 /* Parse a type-parameter-key.
27605 type-parameter-key:
27606 class
27607 typename
27610 static void
27611 cp_parser_type_parameter_key (cp_parser* parser)
27613 /* Look for the type-parameter-key. */
27614 enum tag_types tag_type = none_type;
27615 cp_token *token = cp_lexer_peek_token (parser->lexer);
27616 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
27618 cp_lexer_consume_token (parser->lexer);
27619 if (pedantic && tag_type == typename_type
27620 && cxx_dialect < cxx17)
27621 /* typename is not allowed in a template template parameter
27622 by the standard until C++17. */
27623 pedwarn (token->location, OPT_Wc__17_extensions,
27624 "ISO C++ forbids typename key in template template parameter;"
27625 " use %<-std=c++17%> or %<-std=gnu++17%>");
27627 else
27628 cp_parser_error (parser, "expected %<class%> or %<typename%>");
27630 return;
27633 /* Parse an (optional) member-specification.
27635 member-specification:
27636 member-declaration member-specification [opt]
27637 access-specifier : member-specification [opt] */
27639 static void
27640 cp_parser_member_specification_opt (cp_parser* parser)
27642 while (true)
27644 cp_token *token;
27645 enum rid keyword;
27647 /* Peek at the next token. */
27648 token = cp_lexer_peek_token (parser->lexer);
27649 /* If it's a `}', or EOF then we've seen all the members. */
27650 if (token->type == CPP_CLOSE_BRACE
27651 || token->type == CPP_EOF
27652 || token->type == CPP_PRAGMA_EOL)
27653 break;
27655 /* See if this token is a keyword. */
27656 keyword = token->keyword;
27657 switch (keyword)
27659 case RID_PUBLIC:
27660 case RID_PROTECTED:
27661 case RID_PRIVATE:
27662 /* Consume the access-specifier. */
27663 cp_lexer_consume_token (parser->lexer);
27664 /* Remember which access-specifier is active. */
27665 current_access_specifier = token->u.value;
27666 /* Look for the `:'. */
27667 cp_parser_require (parser, CPP_COLON, RT_COLON);
27668 break;
27670 default:
27671 /* Accept #pragmas at class scope. */
27672 if (token->type == CPP_PRAGMA)
27674 cp_parser_pragma (parser, pragma_member, NULL);
27675 break;
27678 /* Otherwise, the next construction must be a
27679 member-declaration. */
27680 cp_parser_member_declaration (parser);
27685 /* Parse a member-declaration.
27687 member-declaration:
27688 decl-specifier-seq [opt] member-declarator-list [opt] ;
27689 function-definition ; [opt]
27690 :: [opt] nested-name-specifier template [opt] unqualified-id ;
27691 using-declaration
27692 template-declaration
27693 alias-declaration
27695 member-declarator-list:
27696 member-declarator
27697 member-declarator-list , member-declarator
27699 member-declarator:
27700 declarator pure-specifier [opt]
27701 declarator constant-initializer [opt]
27702 identifier [opt] : constant-expression
27704 GNU Extensions:
27706 member-declaration:
27707 __extension__ member-declaration
27709 member-declarator:
27710 declarator attributes [opt] pure-specifier [opt]
27711 declarator attributes [opt] constant-initializer [opt]
27712 identifier [opt] attributes [opt] : constant-expression
27714 C++0x Extensions:
27716 member-declaration:
27717 static_assert-declaration */
27719 static void
27720 cp_parser_member_declaration (cp_parser* parser)
27722 cp_decl_specifier_seq decl_specifiers;
27723 tree prefix_attributes;
27724 tree decl;
27725 int declares_class_or_enum;
27726 bool friend_p;
27727 cp_token *token = NULL;
27728 cp_token *decl_spec_token_start = NULL;
27729 cp_token *initializer_token_start = NULL;
27730 int saved_pedantic;
27731 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27733 /* Check for the `__extension__' keyword. */
27734 if (cp_parser_extension_opt (parser, &saved_pedantic))
27736 /* Recurse. */
27737 cp_parser_member_declaration (parser);
27738 /* Restore the old value of the PEDANTIC flag. */
27739 pedantic = saved_pedantic;
27741 return;
27744 /* Check for a template-declaration. */
27745 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27747 /* An explicit specialization here is an error condition, and we
27748 expect the specialization handler to detect and report this. */
27749 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
27750 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
27751 cp_parser_explicit_specialization (parser);
27752 else
27753 cp_parser_template_declaration (parser, /*member_p=*/true);
27755 return;
27757 /* Check for a template introduction. */
27758 else if (cp_parser_template_declaration_after_export (parser, true))
27759 return;
27761 /* Check for a using-declaration. */
27762 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
27764 if (cxx_dialect < cxx11)
27765 /* Parse the using-declaration. */
27766 cp_parser_using_declaration (parser, /*access_declaration_p=*/false);
27767 else if (cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_ENUM))
27768 cp_parser_using_enum (parser);
27769 else
27771 tree decl;
27772 bool alias_decl_expected;
27773 cp_parser_parse_tentatively (parser);
27774 decl = cp_parser_alias_declaration (parser);
27775 /* Note that if we actually see the '=' token after the
27776 identifier, cp_parser_alias_declaration commits the
27777 tentative parse. In that case, we really expect an
27778 alias-declaration. Otherwise, we expect a using
27779 declaration. */
27780 alias_decl_expected =
27781 !cp_parser_uncommitted_to_tentative_parse_p (parser);
27782 cp_parser_parse_definitely (parser);
27784 if (alias_decl_expected)
27785 finish_member_declaration (decl);
27786 else
27787 cp_parser_using_declaration (parser,
27788 /*access_declaration_p=*/false);
27790 return;
27793 /* Check for @defs. */
27794 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
27796 tree ivar, member;
27797 tree ivar_chains = cp_parser_objc_defs_expression (parser);
27798 ivar = ivar_chains;
27799 while (ivar)
27801 member = ivar;
27802 ivar = TREE_CHAIN (member);
27803 TREE_CHAIN (member) = NULL_TREE;
27804 finish_member_declaration (member);
27806 return;
27809 /* If the next token is `static_assert' we have a static assertion. */
27810 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
27812 cp_parser_static_assert (parser, /*member_p=*/true);
27813 return;
27816 parser->colon_corrects_to_scope_p = false;
27818 cp_omp_declare_simd_data odsd;
27819 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
27820 goto out;
27822 /* Parse the decl-specifier-seq. */
27823 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
27824 cp_parser_decl_specifier_seq (parser,
27825 (CP_PARSER_FLAGS_OPTIONAL
27826 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
27827 &decl_specifiers,
27828 &declares_class_or_enum);
27830 if (decl_specifiers.attributes && (flag_openmp || flag_openmp_simd))
27831 cp_parser_handle_directive_omp_attributes (parser,
27832 &decl_specifiers.attributes,
27833 &odsd, true);
27835 /* Check for an invalid type-name. */
27836 if (!decl_specifiers.any_type_specifiers_p
27837 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
27838 goto out;
27839 /* If there is no declarator, then the decl-specifier-seq should
27840 specify a type. */
27841 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27843 /* If there was no decl-specifier-seq, and the next token is a
27844 `;', then we have something like:
27846 struct S { ; };
27848 [class.mem]
27850 Each member-declaration shall declare at least one member
27851 name of the class. */
27852 if (!decl_specifiers.any_specifiers_p)
27854 cp_token *token = cp_lexer_peek_token (parser->lexer);
27855 if (!in_system_header_at (token->location))
27857 gcc_rich_location richloc (token->location);
27858 richloc.add_fixit_remove ();
27859 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
27862 else
27864 /* See if this declaration is a friend. */
27865 friend_p = cp_parser_friend_p (&decl_specifiers);
27866 /* If there were decl-specifiers, check to see if there was
27867 a class-declaration. */
27868 tree type = check_tag_decl (&decl_specifiers,
27869 /*explicit_type_instantiation_p=*/false);
27870 /* Nested classes have already been added to the class, but
27871 a `friend' needs to be explicitly registered. */
27872 if (friend_p)
27874 /* If the `friend' keyword was present, the friend must
27875 be introduced with a class-key. */
27876 if (!declares_class_or_enum && cxx_dialect < cxx11)
27877 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
27878 "in C++03 a class-key must be used "
27879 "when declaring a friend");
27880 /* In this case:
27882 template <typename T> struct A {
27883 friend struct A<T>::B;
27886 A<T>::B will be represented by a TYPENAME_TYPE, and
27887 therefore not recognized by check_tag_decl. */
27888 if (!type)
27890 type = decl_specifiers.type;
27891 if (type && TREE_CODE (type) == TYPE_DECL)
27892 type = TREE_TYPE (type);
27894 /* Warn if an attribute cannot appear here, as per
27895 [dcl.attr.grammar]/5. But not when declares_class_or_enum:
27896 we ignore attributes in elaborated-type-specifiers. */
27897 if (!declares_class_or_enum
27898 && cxx11_attribute_p (decl_specifiers.attributes))
27900 decl_specifiers.attributes = NULL_TREE;
27901 if (warning_at (decl_spec_token_start->location,
27902 OPT_Wattributes, "attribute ignored"))
27903 inform (decl_spec_token_start->location, "an attribute "
27904 "that appertains to a friend declaration that "
27905 "is not a definition is ignored");
27907 if (!type || !TYPE_P (type))
27908 error_at (decl_spec_token_start->location,
27909 "friend declaration does not name a class or "
27910 "function");
27911 else
27912 make_friend_class (current_class_type, type,
27913 /*complain=*/true);
27915 /* If there is no TYPE, an error message will already have
27916 been issued. */
27917 else if (!type || type == error_mark_node)
27919 /* An anonymous aggregate has to be handled specially; such
27920 a declaration really declares a data member (with a
27921 particular type), as opposed to a nested class. */
27922 else if (ANON_AGGR_TYPE_P (type))
27924 /* C++11 9.5/6. */
27925 if (decl_specifiers.storage_class != sc_none)
27926 error_at (decl_spec_token_start->location,
27927 "a storage class on an anonymous aggregate "
27928 "in class scope is not allowed");
27930 /* Remove constructors and such from TYPE, now that we
27931 know it is an anonymous aggregate. */
27932 fixup_anonymous_aggr (type);
27933 /* And make the corresponding data member. */
27934 decl = build_decl (decl_spec_token_start->location,
27935 FIELD_DECL, NULL_TREE, type);
27936 /* Add it to the class. */
27937 finish_member_declaration (decl);
27939 else
27940 cp_parser_check_access_in_redeclaration
27941 (TYPE_NAME (type),
27942 decl_spec_token_start->location);
27945 else
27947 bool assume_semicolon = false;
27949 /* Clear attributes from the decl_specifiers but keep them
27950 around as prefix attributes that apply them to the entity
27951 being declared. */
27952 prefix_attributes = decl_specifiers.attributes;
27953 decl_specifiers.attributes = NULL_TREE;
27954 if (parser->omp_declare_simd
27955 && (parser->omp_declare_simd->attribs[0]
27956 == &decl_specifiers.attributes))
27957 parser->omp_declare_simd->attribs[0] = &prefix_attributes;
27959 /* See if these declarations will be friends. */
27960 friend_p = cp_parser_friend_p (&decl_specifiers);
27962 /* Keep going until we hit the `;' at the end of the
27963 declaration. */
27964 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27966 tree attributes = NULL_TREE;
27967 tree first_attribute;
27968 tree initializer;
27969 bool named_bitfld = false;
27971 /* Peek at the next token. */
27972 token = cp_lexer_peek_token (parser->lexer);
27974 /* The following code wants to know early if it is a bit-field
27975 or some other declaration. Attributes can appear before
27976 the `:' token. Skip over them without consuming any tokens
27977 to peek if they are followed by `:'. */
27978 if (cp_next_tokens_can_be_attribute_p (parser)
27979 || (token->type == CPP_NAME
27980 && cp_nth_tokens_can_be_attribute_p (parser, 2)
27981 && (named_bitfld = true)))
27983 size_t n
27984 = cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
27985 token = cp_lexer_peek_nth_token (parser->lexer, n);
27988 /* Check for a bitfield declaration. */
27989 if (token->type == CPP_COLON
27990 || (token->type == CPP_NAME
27991 && token == cp_lexer_peek_token (parser->lexer)
27992 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
27993 && (named_bitfld = true)))
27995 tree identifier;
27996 tree width;
27997 tree late_attributes = NULL_TREE;
27998 location_t id_location
27999 = cp_lexer_peek_token (parser->lexer)->location;
28001 if (named_bitfld)
28002 identifier = cp_parser_identifier (parser);
28003 else
28004 identifier = NULL_TREE;
28006 /* Look for attributes that apply to the bitfield. */
28007 attributes = cp_parser_attributes_opt (parser);
28009 /* Consume the `:' token. */
28010 cp_lexer_consume_token (parser->lexer);
28012 /* Get the width of the bitfield. */
28013 width = cp_parser_constant_expression (parser, false, NULL,
28014 cxx_dialect >= cxx11);
28016 /* In C++20 and as extension for C++11 and above we allow
28017 default member initializers for bit-fields. */
28018 initializer = NULL_TREE;
28019 if (cxx_dialect >= cxx11
28020 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
28021 || cp_lexer_next_token_is (parser->lexer,
28022 CPP_OPEN_BRACE)))
28024 location_t loc
28025 = cp_lexer_peek_token (parser->lexer)->location;
28026 if (cxx_dialect < cxx20
28027 && identifier != NULL_TREE)
28028 pedwarn (loc, OPT_Wc__20_extensions,
28029 "default member initializers for bit-fields "
28030 "only available with %<-std=c++20%> or "
28031 "%<-std=gnu++20%>");
28033 initializer = cp_parser_save_nsdmi (parser);
28034 if (identifier == NULL_TREE)
28036 error_at (loc, "default member initializer for "
28037 "unnamed bit-field");
28038 initializer = NULL_TREE;
28041 else
28043 /* Look for attributes that apply to the bitfield after
28044 the `:' token and width. This is where GCC used to
28045 parse attributes in the past, pedwarn if there is
28046 a std attribute. */
28047 if (cp_next_tokens_can_be_std_attribute_p (parser))
28048 pedwarn (input_location, OPT_Wpedantic,
28049 "ISO C++ allows bit-field attributes only "
28050 "before the %<:%> token");
28052 late_attributes = cp_parser_attributes_opt (parser);
28055 attributes = attr_chainon (attributes, late_attributes);
28057 /* Remember which attributes are prefix attributes and
28058 which are not. */
28059 first_attribute = attributes;
28060 /* Combine the attributes. */
28061 attributes = attr_chainon (prefix_attributes, attributes);
28063 /* Create the bitfield declaration. */
28064 decl = grokbitfield (identifier
28065 ? make_id_declarator (NULL_TREE,
28066 identifier,
28067 sfk_none,
28068 id_location)
28069 : NULL,
28070 &decl_specifiers,
28071 width, initializer,
28072 attributes);
28074 else
28076 cp_declarator *declarator;
28077 tree asm_specification;
28078 int ctor_dtor_or_conv_p;
28079 bool static_p = (decl_specifiers.storage_class == sc_static);
28080 cp_parser_flags flags = CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
28081 /* We can't delay parsing for friends,
28082 alias-declarations, and typedefs, even though the
28083 standard seems to require it. */
28084 if (!friend_p
28085 && !decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
28086 flags |= CP_PARSER_FLAGS_DELAY_NOEXCEPT;
28088 /* Parse the declarator. */
28089 declarator
28090 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28091 flags,
28092 &ctor_dtor_or_conv_p,
28093 /*parenthesized_p=*/NULL,
28094 /*member_p=*/true,
28095 friend_p, static_p);
28097 /* If something went wrong parsing the declarator, make sure
28098 that we at least consume some tokens. */
28099 if (declarator == cp_error_declarator)
28101 /* Skip to the end of the statement. */
28102 cp_parser_skip_to_end_of_statement (parser);
28103 /* If the next token is not a semicolon, that is
28104 probably because we just skipped over the body of
28105 a function. So, we consume a semicolon if
28106 present, but do not issue an error message if it
28107 is not present. */
28108 if (cp_lexer_next_token_is (parser->lexer,
28109 CPP_SEMICOLON))
28110 cp_lexer_consume_token (parser->lexer);
28111 goto out;
28114 /* Handle class-scope non-template C++17 deduction guides. */
28115 cp_parser_maybe_adjust_declarator_for_dguide (parser,
28116 &decl_specifiers,
28117 declarator,
28118 &ctor_dtor_or_conv_p);
28120 if (declares_class_or_enum & 2)
28121 cp_parser_check_for_definition_in_return_type
28122 (declarator, decl_specifiers.type,
28123 decl_specifiers.locations[ds_type_spec]);
28125 /* Look for an asm-specification. */
28126 asm_specification = cp_parser_asm_specification_opt (parser);
28127 /* Look for attributes that apply to the declaration. */
28128 attributes = cp_parser_attributes_opt (parser);
28129 /* Remember which attributes are prefix attributes and
28130 which are not. */
28131 first_attribute = attributes;
28132 /* Combine the attributes. */
28133 attributes = attr_chainon (prefix_attributes, attributes);
28135 /* If it's an `=', then we have a constant-initializer or a
28136 pure-specifier. It is not correct to parse the
28137 initializer before registering the member declaration
28138 since the member declaration should be in scope while
28139 its initializer is processed. However, the rest of the
28140 front end does not yet provide an interface that allows
28141 us to handle this correctly. */
28142 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
28144 /* In [class.mem]:
28146 A pure-specifier shall be used only in the declaration of
28147 a virtual function.
28149 A member-declarator can contain a constant-initializer
28150 only if it declares a static member of integral or
28151 enumeration type.
28153 Therefore, if the DECLARATOR is for a function, we look
28154 for a pure-specifier; otherwise, we look for a
28155 constant-initializer. When we call `grokfield', it will
28156 perform more stringent semantics checks. */
28157 initializer_token_start = cp_lexer_peek_token (parser->lexer);
28158 declarator->init_loc = initializer_token_start->location;
28159 if (function_declarator_p (declarator)
28160 || (decl_specifiers.type
28161 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
28162 && declarator->kind == cdk_id
28163 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
28164 == FUNCTION_TYPE)))
28165 initializer = cp_parser_pure_specifier (parser);
28166 else if (decl_specifiers.storage_class != sc_static)
28167 initializer = cp_parser_save_nsdmi (parser);
28168 else if (cxx_dialect >= cxx11)
28170 /* Don't require a constant rvalue in C++11, since we
28171 might want a reference constant. We'll enforce
28172 constancy later. */
28173 cp_lexer_consume_token (parser->lexer);
28174 /* Parse the initializer. */
28175 initializer = cp_parser_initializer_clause (parser);
28177 else
28178 /* Parse the initializer. */
28179 initializer = cp_parser_constant_initializer (parser);
28181 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
28182 && !function_declarator_p (declarator))
28184 declarator->init_loc
28185 = cp_lexer_peek_token (parser->lexer)->location;
28186 if (decl_specifiers.storage_class != sc_static)
28187 initializer = cp_parser_save_nsdmi (parser);
28188 else
28189 initializer = cp_parser_initializer (parser);
28191 /* Detect invalid bit-field cases such as
28193 int *p : 4;
28194 int &&r : 3;
28196 and similar. */
28197 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
28198 /* If there were no type specifiers, it was a
28199 constructor. */
28200 && decl_specifiers.any_type_specifiers_p)
28202 /* This is called for a decent diagnostic only. */
28203 tree d = grokdeclarator (declarator, &decl_specifiers,
28204 BITFIELD, /*initialized=*/false,
28205 &attributes);
28206 if (!error_operand_p (d))
28207 error_at (DECL_SOURCE_LOCATION (d),
28208 "bit-field %qD has non-integral type %qT",
28209 d, TREE_TYPE (d));
28210 cp_parser_skip_to_end_of_statement (parser);
28211 /* Avoid "extra ;" pedwarns. */
28212 if (cp_lexer_next_token_is (parser->lexer,
28213 CPP_SEMICOLON))
28214 cp_lexer_consume_token (parser->lexer);
28215 goto out;
28217 /* Otherwise, there is no initializer. */
28218 else
28219 initializer = NULL_TREE;
28221 /* See if we are probably looking at a function
28222 definition. We are certainly not looking at a
28223 member-declarator. Calling `grokfield' has
28224 side-effects, so we must not do it unless we are sure
28225 that we are looking at a member-declarator. */
28226 if (cp_parser_token_starts_function_definition_p
28227 (cp_lexer_peek_token (parser->lexer)))
28229 /* The grammar does not allow a pure-specifier to be
28230 used when a member function is defined. (It is
28231 possible that this fact is an oversight in the
28232 standard, since a pure function may be defined
28233 outside of the class-specifier. */
28234 if (initializer && initializer_token_start)
28235 error_at (initializer_token_start->location,
28236 "pure-specifier on function-definition");
28237 decl = cp_parser_save_member_function_body (parser,
28238 &decl_specifiers,
28239 declarator,
28240 attributes);
28242 if (parser->fully_implicit_function_template_p)
28243 decl = finish_fully_implicit_template (parser, decl);
28244 /* If the member was not a friend, declare it here. */
28245 if (!friend_p)
28246 finish_member_declaration (decl);
28247 /* Peek at the next token. */
28248 token = cp_lexer_peek_token (parser->lexer);
28249 /* If the next token is a semicolon, consume it. */
28250 if (token->type == CPP_SEMICOLON)
28252 location_t semicolon_loc
28253 = cp_lexer_consume_token (parser->lexer)->location;
28254 gcc_rich_location richloc (semicolon_loc);
28255 richloc.add_fixit_remove ();
28256 warning_at (&richloc, OPT_Wextra_semi,
28257 "extra %<;%> after in-class "
28258 "function definition");
28260 goto out;
28262 else
28263 if (declarator->kind == cdk_function)
28264 declarator->id_loc = token->location;
28266 /* Create the declaration. */
28267 decl = grokfield (declarator, &decl_specifiers,
28268 initializer, /*init_const_expr_p=*/true,
28269 asm_specification, attributes);
28271 if (parser->fully_implicit_function_template_p)
28273 if (friend_p)
28274 finish_fully_implicit_template (parser, 0);
28275 else
28276 decl = finish_fully_implicit_template (parser, decl);
28280 cp_finalize_omp_declare_simd (parser, decl);
28281 cp_finalize_oacc_routine (parser, decl, false);
28283 /* Reset PREFIX_ATTRIBUTES. */
28284 if (attributes != error_mark_node)
28286 while (attributes && TREE_CHAIN (attributes) != first_attribute)
28287 attributes = TREE_CHAIN (attributes);
28288 if (attributes)
28289 TREE_CHAIN (attributes) = NULL_TREE;
28292 /* If there is any qualification still in effect, clear it
28293 now; we will be starting fresh with the next declarator. */
28294 parser->scope = NULL_TREE;
28295 parser->qualifying_scope = NULL_TREE;
28296 parser->object_scope = NULL_TREE;
28297 /* If it's a `,', then there are more declarators. */
28298 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28300 cp_lexer_consume_token (parser->lexer);
28301 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
28303 cp_token *token = cp_lexer_previous_token (parser->lexer);
28304 gcc_rich_location richloc (token->location);
28305 richloc.add_fixit_remove ();
28306 error_at (&richloc, "stray %<,%> at end of "
28307 "member declaration");
28310 /* If the next token isn't a `;', then we have a parse error. */
28311 else if (cp_lexer_next_token_is_not (parser->lexer,
28312 CPP_SEMICOLON))
28314 /* The next token might be a ways away from where the
28315 actual semicolon is missing. Find the previous token
28316 and use that for our error position. */
28317 cp_token *token = cp_lexer_previous_token (parser->lexer);
28318 gcc_rich_location richloc (token->location);
28319 richloc.add_fixit_insert_after (";");
28320 error_at (&richloc, "expected %<;%> at end of "
28321 "member declaration");
28323 /* Assume that the user meant to provide a semicolon. If
28324 we were to cp_parser_skip_to_end_of_statement, we might
28325 skip to a semicolon inside a member function definition
28326 and issue nonsensical error messages. */
28327 assume_semicolon = true;
28330 if (decl)
28332 /* Add DECL to the list of members. */
28333 if (!friend_p
28334 /* Explicitly include, eg, NSDMIs, for better error
28335 recovery (c++/58650). */
28336 || !DECL_DECLARES_FUNCTION_P (decl))
28337 finish_member_declaration (decl);
28339 if (DECL_DECLARES_FUNCTION_P (decl))
28340 cp_parser_save_default_args (parser, STRIP_TEMPLATE (decl));
28341 else if (TREE_CODE (decl) == FIELD_DECL
28342 && DECL_INITIAL (decl))
28343 /* Add DECL to the queue of NSDMI to be parsed later. */
28344 vec_safe_push (unparsed_nsdmis, decl);
28347 if (assume_semicolon)
28348 goto out;
28352 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
28353 out:
28354 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
28355 cp_finalize_omp_declare_simd (parser, &odsd);
28358 /* Parse a pure-specifier.
28360 pure-specifier:
28363 Returns INTEGER_ZERO_NODE if a pure specifier is found.
28364 Otherwise, ERROR_MARK_NODE is returned. */
28366 static tree
28367 cp_parser_pure_specifier (cp_parser* parser)
28369 cp_token *token;
28371 /* Look for the `=' token. */
28372 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28373 return error_mark_node;
28374 /* Look for the `0' token. */
28375 token = cp_lexer_peek_token (parser->lexer);
28377 if (token->type == CPP_EOF
28378 || token->type == CPP_PRAGMA_EOL)
28379 return error_mark_node;
28381 cp_lexer_consume_token (parser->lexer);
28383 /* Accept = default or = delete in c++0x mode. */
28384 if (token->keyword == RID_DEFAULT
28385 || token->keyword == RID_DELETE)
28387 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
28388 return token->u.value;
28391 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
28392 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
28394 cp_parser_error (parser,
28395 "invalid pure specifier (only %<= 0%> is allowed)");
28396 cp_parser_skip_to_end_of_statement (parser);
28397 return error_mark_node;
28399 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
28401 error_at (token->location, "templates may not be %<virtual%>");
28402 return error_mark_node;
28405 return integer_zero_node;
28408 /* Parse a constant-initializer.
28410 constant-initializer:
28411 = constant-expression
28413 Returns a representation of the constant-expression. */
28415 static tree
28416 cp_parser_constant_initializer (cp_parser* parser)
28418 /* Look for the `=' token. */
28419 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28420 return error_mark_node;
28422 /* It is invalid to write:
28424 struct S { static const int i = { 7 }; };
28427 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28429 cp_parser_error (parser,
28430 "a brace-enclosed initializer is not allowed here");
28431 /* Consume the opening brace. */
28432 matching_braces braces;
28433 braces.consume_open (parser);
28434 /* Skip the initializer. */
28435 cp_parser_skip_to_closing_brace (parser);
28436 /* Look for the trailing `}'. */
28437 braces.require_close (parser);
28439 return error_mark_node;
28442 return cp_parser_constant_expression (parser);
28445 /* Derived classes [gram.class.derived] */
28447 /* Parse a base-clause.
28449 base-clause:
28450 : base-specifier-list
28452 base-specifier-list:
28453 base-specifier ... [opt]
28454 base-specifier-list , base-specifier ... [opt]
28456 Returns a TREE_LIST representing the base-classes, in the order in
28457 which they were declared. The representation of each node is as
28458 described by cp_parser_base_specifier.
28460 In the case that no bases are specified, this function will return
28461 NULL_TREE, not ERROR_MARK_NODE. */
28463 static tree
28464 cp_parser_base_clause (cp_parser* parser)
28466 tree bases = NULL_TREE;
28468 /* Look for the `:' that begins the list. */
28469 cp_parser_require (parser, CPP_COLON, RT_COLON);
28471 /* Scan the base-specifier-list. */
28472 while (true)
28474 cp_token *token;
28475 tree base;
28476 bool pack_expansion_p = false;
28478 /* Look for the base-specifier. */
28479 base = cp_parser_base_specifier (parser);
28480 /* Look for the (optional) ellipsis. */
28481 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28483 /* Consume the `...'. */
28484 cp_lexer_consume_token (parser->lexer);
28486 pack_expansion_p = true;
28489 /* Add BASE to the front of the list. */
28490 if (base && base != error_mark_node)
28492 if (pack_expansion_p)
28493 /* Make this a pack expansion type. */
28494 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
28496 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
28498 TREE_CHAIN (base) = bases;
28499 bases = base;
28502 /* Peek at the next token. */
28503 token = cp_lexer_peek_token (parser->lexer);
28504 /* If it's not a comma, then the list is complete. */
28505 if (token->type != CPP_COMMA)
28506 break;
28507 /* Consume the `,'. */
28508 cp_lexer_consume_token (parser->lexer);
28511 /* PARSER->SCOPE may still be non-NULL at this point, if the last
28512 base class had a qualified name. However, the next name that
28513 appears is certainly not qualified. */
28514 parser->scope = NULL_TREE;
28515 parser->qualifying_scope = NULL_TREE;
28516 parser->object_scope = NULL_TREE;
28518 return nreverse (bases);
28521 /* Parse a base-specifier.
28523 base-specifier:
28524 :: [opt] nested-name-specifier [opt] class-name
28525 virtual access-specifier [opt] :: [opt] nested-name-specifier
28526 [opt] class-name
28527 access-specifier virtual [opt] :: [opt] nested-name-specifier
28528 [opt] class-name
28530 Returns a TREE_LIST. The TREE_PURPOSE will be one of
28531 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
28532 indicate the specifiers provided. The TREE_VALUE will be a TYPE
28533 (or the ERROR_MARK_NODE) indicating the type that was specified. */
28535 static tree
28536 cp_parser_base_specifier (cp_parser* parser)
28538 cp_token *token;
28539 bool done = false;
28540 bool virtual_p = false;
28541 bool duplicate_virtual_error_issued_p = false;
28542 bool duplicate_access_error_issued_p = false;
28543 bool class_scope_p, template_p;
28544 tree access = access_default_node;
28545 tree type;
28547 /* Process the optional `virtual' and `access-specifier'. */
28548 while (!done)
28550 /* Peek at the next token. */
28551 token = cp_lexer_peek_token (parser->lexer);
28552 /* Process `virtual'. */
28553 switch (token->keyword)
28555 case RID_VIRTUAL:
28556 /* If `virtual' appears more than once, issue an error. */
28557 if (virtual_p && !duplicate_virtual_error_issued_p)
28559 cp_parser_error (parser,
28560 "%<virtual%> specified more than once in base-specifier");
28561 duplicate_virtual_error_issued_p = true;
28564 virtual_p = true;
28566 /* Consume the `virtual' token. */
28567 cp_lexer_consume_token (parser->lexer);
28569 break;
28571 case RID_PUBLIC:
28572 case RID_PROTECTED:
28573 case RID_PRIVATE:
28574 /* If more than one access specifier appears, issue an
28575 error. */
28576 if (access != access_default_node
28577 && !duplicate_access_error_issued_p)
28579 cp_parser_error (parser,
28580 "more than one access specifier in base-specifier");
28581 duplicate_access_error_issued_p = true;
28584 access = ridpointers[(int) token->keyword];
28586 /* Consume the access-specifier. */
28587 cp_lexer_consume_token (parser->lexer);
28589 break;
28591 default:
28592 done = true;
28593 break;
28596 /* It is not uncommon to see programs mechanically, erroneously, use
28597 the 'typename' keyword to denote (dependent) qualified types
28598 as base classes. */
28599 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
28601 token = cp_lexer_peek_token (parser->lexer);
28602 if (!processing_template_decl)
28603 error_at (token->location,
28604 "keyword %<typename%> not allowed outside of templates");
28605 else
28606 error_at (token->location,
28607 "keyword %<typename%> not allowed in this context "
28608 "(the base class is implicitly a type)");
28609 cp_lexer_consume_token (parser->lexer);
28612 /* Look for the optional `::' operator. */
28613 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
28614 /* Look for the nested-name-specifier. The simplest way to
28615 implement:
28617 [temp.res]
28619 The keyword `typename' is not permitted in a base-specifier or
28620 mem-initializer; in these contexts a qualified name that
28621 depends on a template-parameter is implicitly assumed to be a
28622 type name.
28624 is to pretend that we have seen the `typename' keyword at this
28625 point. */
28626 cp_parser_nested_name_specifier_opt (parser,
28627 /*typename_keyword_p=*/true,
28628 /*check_dependency_p=*/true,
28629 /*type_p=*/true,
28630 /*is_declaration=*/true);
28631 /* If the base class is given by a qualified name, assume that names
28632 we see are type names or templates, as appropriate. */
28633 class_scope_p = (parser->scope && TYPE_P (parser->scope));
28634 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
28636 if (!parser->scope
28637 && cp_lexer_next_token_is_decltype (parser->lexer))
28638 /* DR 950 allows decltype as a base-specifier. */
28639 type = cp_parser_decltype (parser);
28640 else
28642 /* Otherwise, look for the class-name. */
28643 type = cp_parser_class_name (parser,
28644 class_scope_p,
28645 template_p,
28646 typename_type,
28647 /*check_dependency_p=*/true,
28648 /*class_head_p=*/false,
28649 /*is_declaration=*/true);
28650 type = TREE_TYPE (type);
28653 if (type == error_mark_node)
28654 return error_mark_node;
28656 return finish_base_specifier (type, access, virtual_p);
28659 /* Exception handling [gram.exception] */
28661 /* Save the tokens that make up the noexcept-specifier for a member-function.
28662 Returns a DEFERRED_PARSE. */
28664 static tree
28665 cp_parser_save_noexcept (cp_parser *parser)
28667 cp_token *first = parser->lexer->next_token;
28668 /* We want everything up to, including, the final ')'. */
28669 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0);
28670 cp_token *last = parser->lexer->next_token;
28672 /* As with default arguments and NSDMIs, make use of DEFERRED_PARSE
28673 to carry the information we will need. */
28674 tree expr = make_node (DEFERRED_PARSE);
28675 /* Save away the noexcept-specifier; we will process it when the
28676 class is complete. */
28677 DEFPARSE_TOKENS (expr) = cp_token_cache_new (first, last);
28678 DEFPARSE_INSTANTIATIONS (expr) = nullptr;
28679 expr = build_tree_list (expr, NULL_TREE);
28680 return expr;
28683 /* Used for late processing of noexcept-specifiers of member-functions.
28684 DEFAULT_ARG is the unparsed operand of a noexcept-specifier which
28685 we saved for later; parse it now. DECL is the declaration of the
28686 member function. */
28688 static tree
28689 cp_parser_late_noexcept_specifier (cp_parser *parser, tree default_arg)
28691 /* Make sure we've gotten something that hasn't been parsed yet. */
28692 gcc_assert (TREE_CODE (default_arg) == DEFERRED_PARSE);
28694 push_unparsed_function_queues (parser);
28696 /* Push the saved tokens for the noexcept-specifier onto the parser's
28697 lexer stack. */
28698 cp_token_cache *tokens = DEFPARSE_TOKENS (default_arg);
28699 cp_parser_push_lexer_for_tokens (parser, tokens);
28701 /* Parse the cached noexcept-specifier. */
28702 tree parsed_arg
28703 = cp_parser_noexcept_specification_opt (parser,
28704 CP_PARSER_FLAGS_NONE,
28705 /*require_constexpr=*/true,
28706 /*consumed_expr=*/NULL,
28707 /*return_cond=*/false);
28709 /* Revert to the main lexer. */
28710 cp_parser_pop_lexer (parser);
28712 /* Restore the queue. */
28713 pop_unparsed_function_queues (parser);
28715 /* And we're done. */
28716 return parsed_arg;
28719 /* Perform late checking of overriding function with respect to their
28720 noexcept-specifiers. FNDECL is the member function that potentially
28721 overrides some virtual function with the same signature. */
28723 static void
28724 noexcept_override_late_checks (tree fndecl)
28726 tree binfo = TYPE_BINFO (DECL_CONTEXT (fndecl));
28727 tree base_binfo;
28729 if (DECL_STATIC_FUNCTION_P (fndecl))
28730 return;
28732 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
28734 tree basetype = BINFO_TYPE (base_binfo);
28736 if (!TYPE_POLYMORPHIC_P (basetype))
28737 continue;
28739 tree fn = look_for_overrides_here (basetype, fndecl);
28740 if (fn)
28741 maybe_check_overriding_exception_spec (fndecl, fn);
28745 /* Parse an (optional) noexcept-specification.
28747 noexcept-specification:
28748 noexcept ( constant-expression ) [opt]
28750 If no noexcept-specification is present, returns NULL_TREE.
28751 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
28752 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
28753 there are no parentheses. CONSUMED_EXPR will be set accordingly.
28754 Otherwise, returns a noexcept specification unless RETURN_COND is true,
28755 in which case a boolean condition is returned instead. The parser flags
28756 FLAGS is used to control parsing. QUALS are qualifiers indicating whether
28757 the (member) function is `const'. */
28759 static tree
28760 cp_parser_noexcept_specification_opt (cp_parser* parser,
28761 cp_parser_flags flags,
28762 bool require_constexpr,
28763 bool* consumed_expr,
28764 bool return_cond)
28766 cp_token *token;
28767 const char *saved_message;
28769 /* Peek at the next token. */
28770 token = cp_lexer_peek_token (parser->lexer);
28772 /* Is it a noexcept-specification? */
28773 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
28775 tree expr;
28777 /* [class.mem]/6 says that a noexcept-specifer (within the
28778 member-specification of the class) is a complete-class context of
28779 a class. So, if the noexcept-specifier has the optional expression,
28780 just save the tokens, and reparse this after we're done with the
28781 class. */
28783 if ((flags & CP_PARSER_FLAGS_DELAY_NOEXCEPT)
28784 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN)
28785 /* No need to delay parsing for a number literal or true/false. */
28786 && !((cp_lexer_nth_token_is (parser->lexer, 3, CPP_NUMBER)
28787 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
28788 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_PAREN))
28789 && at_class_scope_p ()
28790 && TYPE_BEING_DEFINED (current_class_type)
28791 && !LAMBDA_TYPE_P (current_class_type))
28792 return cp_parser_save_noexcept (parser);
28794 cp_lexer_consume_token (parser->lexer);
28796 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
28798 matching_parens parens;
28799 parens.consume_open (parser);
28801 if (require_constexpr)
28803 /* Types may not be defined in an exception-specification. */
28804 saved_message = parser->type_definition_forbidden_message;
28805 parser->type_definition_forbidden_message
28806 = G_("types may not be defined in an exception-specification");
28808 bool non_constant_p;
28809 expr
28810 = cp_parser_constant_expression (parser,
28811 /*allow_non_constant=*/true,
28812 &non_constant_p);
28813 if (non_constant_p
28814 && !require_potential_rvalue_constant_expression (expr))
28816 expr = NULL_TREE;
28817 return_cond = true;
28820 /* Restore the saved message. */
28821 parser->type_definition_forbidden_message = saved_message;
28823 else
28825 expr = cp_parser_expression (parser);
28826 *consumed_expr = true;
28829 parens.require_close (parser);
28831 else
28833 expr = boolean_true_node;
28834 if (!require_constexpr)
28835 *consumed_expr = false;
28838 /* We cannot build a noexcept-spec right away because this will check
28839 that expr is a constexpr. */
28840 if (!return_cond)
28841 return build_noexcept_spec (expr, tf_warning_or_error);
28842 else
28843 return expr;
28845 else
28846 return NULL_TREE;
28849 /* Parse an (optional) exception-specification.
28851 exception-specification:
28852 throw ( type-id-list [opt] )
28854 Returns a TREE_LIST representing the exception-specification. The
28855 TREE_VALUE of each node is a type. The parser flags FLAGS is used to
28856 control parsing. QUALS are qualifiers indicating whether the (member)
28857 function is `const'. */
28859 static tree
28860 cp_parser_exception_specification_opt (cp_parser* parser,
28861 cp_parser_flags flags)
28863 cp_token *token;
28864 tree type_id_list;
28865 const char *saved_message;
28867 /* Peek at the next token. */
28868 token = cp_lexer_peek_token (parser->lexer);
28870 /* Is it a noexcept-specification? */
28871 type_id_list
28872 = cp_parser_noexcept_specification_opt (parser, flags,
28873 /*require_constexpr=*/true,
28874 /*consumed_expr=*/NULL,
28875 /*return_cond=*/false);
28876 if (type_id_list != NULL_TREE)
28877 return type_id_list;
28879 /* If it's not `throw', then there's no exception-specification. */
28880 if (!cp_parser_is_keyword (token, RID_THROW))
28881 return NULL_TREE;
28883 location_t loc = token->location;
28885 /* Consume the `throw'. */
28886 cp_lexer_consume_token (parser->lexer);
28888 /* Look for the `('. */
28889 matching_parens parens;
28890 parens.require_open (parser);
28892 /* Peek at the next token. */
28893 token = cp_lexer_peek_token (parser->lexer);
28894 /* If it's not a `)', then there is a type-id-list. */
28895 if (token->type != CPP_CLOSE_PAREN)
28897 /* Types may not be defined in an exception-specification. */
28898 saved_message = parser->type_definition_forbidden_message;
28899 parser->type_definition_forbidden_message
28900 = G_("types may not be defined in an exception-specification");
28901 /* Parse the type-id-list. */
28902 type_id_list = cp_parser_type_id_list (parser);
28903 /* Restore the saved message. */
28904 parser->type_definition_forbidden_message = saved_message;
28906 if (cxx_dialect >= cxx17)
28908 error_at (loc, "ISO C++17 does not allow dynamic exception "
28909 "specifications");
28910 type_id_list = NULL_TREE;
28912 else if (cxx_dialect >= cxx11)
28913 warning_at (loc, OPT_Wdeprecated,
28914 "dynamic exception specifications are deprecated in "
28915 "C++11");
28917 /* In C++17, throw() is equivalent to noexcept (true). throw()
28918 is deprecated in C++11 and above as well, but is still widely used,
28919 so don't warn about it yet. */
28920 else if (cxx_dialect >= cxx17)
28921 type_id_list = noexcept_true_spec;
28922 else
28923 type_id_list = empty_except_spec;
28925 /* Look for the `)'. */
28926 parens.require_close (parser);
28928 return type_id_list;
28931 /* Parse an (optional) type-id-list.
28933 type-id-list:
28934 type-id ... [opt]
28935 type-id-list , type-id ... [opt]
28937 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
28938 in the order that the types were presented. */
28940 static tree
28941 cp_parser_type_id_list (cp_parser* parser)
28943 tree types = NULL_TREE;
28945 while (true)
28947 cp_token *token;
28948 tree type;
28950 token = cp_lexer_peek_token (parser->lexer);
28952 /* Get the next type-id. */
28953 type = cp_parser_type_id (parser);
28954 /* Check for invalid 'auto'. */
28955 if (flag_concepts && type_uses_auto (type))
28957 error_at (token->location,
28958 "invalid use of %<auto%> in exception-specification");
28959 type = error_mark_node;
28961 /* Parse the optional ellipsis. */
28962 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28964 /* Consume the `...'. */
28965 cp_lexer_consume_token (parser->lexer);
28967 /* Turn the type into a pack expansion expression. */
28968 type = make_pack_expansion (type);
28970 /* Add it to the list. */
28971 types = add_exception_specifier (types, type, /*complain=*/1);
28972 /* Peek at the next token. */
28973 token = cp_lexer_peek_token (parser->lexer);
28974 /* If it is not a `,', we are done. */
28975 if (token->type != CPP_COMMA)
28976 break;
28977 /* Consume the `,'. */
28978 cp_lexer_consume_token (parser->lexer);
28981 return nreverse (types);
28984 /* Parse a try-block.
28986 try-block:
28987 try compound-statement handler-seq */
28989 static tree
28990 cp_parser_try_block (cp_parser* parser)
28992 tree try_block;
28994 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
28995 if (parser->in_function_body
28996 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
28997 && cxx_dialect < cxx20)
28998 pedwarn (input_location, OPT_Wc__20_extensions,
28999 "%<try%> in %<constexpr%> function only "
29000 "available with %<-std=c++20%> or %<-std=gnu++20%>");
29002 try_block = begin_try_block ();
29003 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
29004 finish_try_block (try_block);
29005 cp_parser_handler_seq (parser);
29006 finish_handler_sequence (try_block);
29008 return try_block;
29011 /* Parse a function-try-block.
29013 function-try-block:
29014 try ctor-initializer [opt] function-body handler-seq */
29016 static void
29017 cp_parser_function_try_block (cp_parser* parser)
29019 tree compound_stmt;
29020 tree try_block;
29022 /* Look for the `try' keyword. */
29023 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
29024 return;
29025 /* Let the rest of the front end know where we are. */
29026 try_block = begin_function_try_block (&compound_stmt);
29027 /* Parse the function-body. */
29028 cp_parser_ctor_initializer_opt_and_function_body
29029 (parser, /*in_function_try_block=*/true);
29030 /* We're done with the `try' part. */
29031 finish_function_try_block (try_block);
29032 /* Parse the handlers. */
29033 cp_parser_handler_seq (parser);
29034 /* We're done with the handlers. */
29035 finish_function_handler_sequence (try_block, compound_stmt);
29038 /* Parse a handler-seq.
29040 handler-seq:
29041 handler handler-seq [opt] */
29043 static void
29044 cp_parser_handler_seq (cp_parser* parser)
29046 while (true)
29048 cp_token *token;
29050 /* Parse the handler. */
29051 cp_parser_handler (parser);
29052 /* Peek at the next token. */
29053 token = cp_lexer_peek_token (parser->lexer);
29054 /* If it's not `catch' then there are no more handlers. */
29055 if (!cp_parser_is_keyword (token, RID_CATCH))
29056 break;
29060 /* Parse a handler.
29062 handler:
29063 catch ( exception-declaration ) compound-statement */
29065 static void
29066 cp_parser_handler (cp_parser* parser)
29068 tree handler;
29069 tree declaration;
29071 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
29072 handler = begin_handler ();
29073 matching_parens parens;
29074 parens.require_open (parser);
29075 declaration = cp_parser_exception_declaration (parser);
29076 finish_handler_parms (declaration, handler);
29077 parens.require_close (parser);
29078 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29079 finish_handler (handler);
29082 /* Parse an exception-declaration.
29084 exception-declaration:
29085 type-specifier-seq declarator
29086 type-specifier-seq abstract-declarator
29087 type-specifier-seq
29090 Returns a VAR_DECL for the declaration, or NULL_TREE if the
29091 ellipsis variant is used. */
29093 static tree
29094 cp_parser_exception_declaration (cp_parser* parser)
29096 cp_decl_specifier_seq type_specifiers;
29097 cp_declarator *declarator;
29098 const char *saved_message;
29100 /* If it's an ellipsis, it's easy to handle. */
29101 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
29103 /* Consume the `...' token. */
29104 cp_lexer_consume_token (parser->lexer);
29105 return NULL_TREE;
29108 /* Types may not be defined in exception-declarations. */
29109 saved_message = parser->type_definition_forbidden_message;
29110 parser->type_definition_forbidden_message
29111 = G_("types may not be defined in exception-declarations");
29113 /* Parse the type-specifier-seq. */
29114 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
29115 /*is_declaration=*/true,
29116 /*is_trailing_return=*/false,
29117 &type_specifiers);
29118 /* If it's a `)', then there is no declarator. */
29119 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
29120 declarator = NULL;
29121 else
29122 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
29123 CP_PARSER_FLAGS_NONE,
29124 /*ctor_dtor_or_conv_p=*/NULL,
29125 /*parenthesized_p=*/NULL,
29126 /*member_p=*/false,
29127 /*friend_p=*/false,
29128 /*static_p=*/false);
29130 /* Restore the saved message. */
29131 parser->type_definition_forbidden_message = saved_message;
29133 if (!type_specifiers.any_specifiers_p)
29134 return error_mark_node;
29136 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
29139 /* Parse a throw-expression.
29141 throw-expression:
29142 throw assignment-expression [opt]
29144 Returns a THROW_EXPR representing the throw-expression. */
29146 static tree
29147 cp_parser_throw_expression (cp_parser* parser)
29149 tree expression;
29150 cp_token* token;
29151 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29153 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
29154 token = cp_lexer_peek_token (parser->lexer);
29155 /* Figure out whether or not there is an assignment-expression
29156 following the "throw" keyword. */
29157 if (token->type == CPP_COMMA
29158 || token->type == CPP_SEMICOLON
29159 || token->type == CPP_CLOSE_PAREN
29160 || token->type == CPP_CLOSE_SQUARE
29161 || token->type == CPP_CLOSE_BRACE
29162 || token->type == CPP_COLON)
29163 expression = NULL_TREE;
29164 else
29165 expression = cp_parser_assignment_expression (parser);
29167 /* Construct a location e.g.:
29168 throw x
29169 ^~~~~~~
29170 with caret == start at the start of the "throw" token, and
29171 the end at the end of the final token we consumed. */
29172 location_t combined_loc = make_location (start_loc, start_loc,
29173 parser->lexer);
29174 expression = build_throw (combined_loc, expression);
29176 return expression;
29179 /* Parse a yield-expression.
29181 yield-expression:
29182 co_yield assignment-expression
29183 co_yield braced-init-list
29185 Returns a CO_YIELD_EXPR representing the yield-expression. */
29187 static tree
29188 cp_parser_yield_expression (cp_parser* parser)
29190 tree expr;
29192 cp_token *token = cp_lexer_peek_token (parser->lexer);
29193 location_t kw_loc = token->location; /* Save for later. */
29195 cp_parser_require_keyword (parser, RID_CO_YIELD, RT_CO_YIELD);
29197 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29199 cp_lexer_set_source_position (parser->lexer);
29200 /* ??? : probably a moot point? */
29201 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
29202 expr = cp_parser_braced_list (parser);
29204 else
29205 expr = cp_parser_assignment_expression (parser);
29207 if (expr == error_mark_node)
29208 return expr;
29210 return finish_co_yield_expr (kw_loc, expr);
29213 /* GNU Extensions */
29215 /* Parse an (optional) asm-specification.
29217 asm-specification:
29218 asm ( string-literal )
29220 If the asm-specification is present, returns a STRING_CST
29221 corresponding to the string-literal. Otherwise, returns
29222 NULL_TREE. */
29224 static tree
29225 cp_parser_asm_specification_opt (cp_parser* parser)
29227 /* Peek at the next token. */
29228 cp_token *token = cp_lexer_peek_token (parser->lexer);
29229 /* If the next token isn't the `asm' keyword, then there's no
29230 asm-specification. */
29231 if (!cp_parser_is_keyword (token, RID_ASM))
29232 return NULL_TREE;
29234 /* Consume the `asm' token. */
29235 cp_lexer_consume_token (parser->lexer);
29236 /* Look for the `('. */
29237 matching_parens parens;
29238 parens.require_open (parser);
29240 /* Look for the string-literal. */
29241 tree asm_specification = cp_parser_string_literal (parser,
29242 /*translate=*/false,
29243 /*wide_ok=*/false);
29245 /* Look for the `)'. */
29246 parens.require_close (parser);
29248 return asm_specification;
29251 /* Parse an asm-operand-list.
29253 asm-operand-list:
29254 asm-operand
29255 asm-operand-list , asm-operand
29257 asm-operand:
29258 string-literal ( expression )
29259 [ string-literal ] string-literal ( expression )
29261 Returns a TREE_LIST representing the operands. The TREE_VALUE of
29262 each node is the expression. The TREE_PURPOSE is itself a
29263 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
29264 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
29265 is a STRING_CST for the string literal before the parenthesis. Returns
29266 ERROR_MARK_NODE if any of the operands are invalid. */
29268 static tree
29269 cp_parser_asm_operand_list (cp_parser* parser)
29271 tree asm_operands = NULL_TREE;
29272 bool invalid_operands = false;
29274 while (true)
29276 tree name;
29278 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
29280 /* Consume the `[' token. */
29281 cp_lexer_consume_token (parser->lexer);
29282 /* Read the operand name. */
29283 name = cp_parser_identifier (parser);
29284 if (name != error_mark_node)
29285 name = build_string (IDENTIFIER_LENGTH (name),
29286 IDENTIFIER_POINTER (name));
29287 /* Look for the closing `]'. */
29288 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
29290 else
29291 name = NULL_TREE;
29292 /* Look for the string-literal. */
29293 tree string_literal = cp_parser_string_literal (parser,
29294 /*translate=*/false,
29295 /*wide_ok=*/false);
29297 /* Look for the `('. */
29298 matching_parens parens;
29299 parens.require_open (parser);
29300 /* Parse the expression. */
29301 tree expression = cp_parser_expression (parser);
29302 /* Look for the `)'. */
29303 parens.require_close (parser);
29305 if (name == error_mark_node
29306 || string_literal == error_mark_node
29307 || expression == error_mark_node)
29308 invalid_operands = true;
29310 /* Add this operand to the list. */
29311 asm_operands = tree_cons (build_tree_list (name, string_literal),
29312 expression,
29313 asm_operands);
29314 /* If the next token is not a `,', there are no more
29315 operands. */
29316 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
29317 break;
29318 /* Consume the `,'. */
29319 cp_lexer_consume_token (parser->lexer);
29322 return invalid_operands ? error_mark_node : nreverse (asm_operands);
29325 /* Parse an asm-clobber-list.
29327 asm-clobber-list:
29328 string-literal
29329 asm-clobber-list , string-literal
29331 Returns a TREE_LIST, indicating the clobbers in the order that they
29332 appeared. The TREE_VALUE of each node is a STRING_CST. */
29334 static tree
29335 cp_parser_asm_clobber_list (cp_parser* parser)
29337 tree clobbers = NULL_TREE;
29339 while (true)
29341 /* Look for the string literal. */
29342 tree string_literal = cp_parser_string_literal (parser,
29343 /*translate=*/false,
29344 /*wide_ok=*/false);
29345 /* Add it to the list. */
29346 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
29347 /* If the next token is not a `,', then the list is
29348 complete. */
29349 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
29350 break;
29351 /* Consume the `,' token. */
29352 cp_lexer_consume_token (parser->lexer);
29355 return clobbers;
29358 /* Parse an asm-label-list.
29360 asm-label-list:
29361 identifier
29362 asm-label-list , identifier
29364 Returns a TREE_LIST, indicating the labels in the order that they
29365 appeared. The TREE_VALUE of each node is a label. */
29367 static tree
29368 cp_parser_asm_label_list (cp_parser* parser)
29370 tree labels = NULL_TREE;
29372 while (true)
29374 tree identifier, label, name;
29376 /* Look for the identifier. */
29377 identifier = cp_parser_identifier (parser);
29378 if (!error_operand_p (identifier))
29380 label = lookup_label (identifier);
29381 if (TREE_CODE (label) == LABEL_DECL)
29383 TREE_USED (label) = 1;
29384 check_goto (label);
29385 name = build_string (IDENTIFIER_LENGTH (identifier),
29386 IDENTIFIER_POINTER (identifier));
29387 labels = tree_cons (name, label, labels);
29390 /* If the next token is not a `,', then the list is
29391 complete. */
29392 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
29393 break;
29394 /* Consume the `,' token. */
29395 cp_lexer_consume_token (parser->lexer);
29398 return nreverse (labels);
29401 /* Return TRUE iff the next tokens in the stream are possibly the
29402 beginning of a GNU extension attribute. */
29404 static bool
29405 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
29407 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
29410 /* Return TRUE iff the next tokens in the stream are possibly the
29411 beginning of a standard C++-11 attribute specifier. */
29413 static bool
29414 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
29416 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
29419 /* Return TRUE iff the next Nth tokens in the stream are possibly the
29420 beginning of a standard C++-11 attribute specifier. */
29422 static bool
29423 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
29425 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
29427 return (cxx_dialect >= cxx11
29428 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
29429 || (token->type == CPP_OPEN_SQUARE
29430 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
29431 && token->type == CPP_OPEN_SQUARE)));
29434 /* Return TRUE iff the next Nth tokens in the stream are possibly the
29435 beginning of a GNU extension attribute. */
29437 static bool
29438 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
29440 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
29442 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
29445 /* Return true iff the next tokens can be the beginning of either a
29446 GNU attribute list, or a standard C++11 attribute sequence. */
29448 static bool
29449 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
29451 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
29452 || cp_next_tokens_can_be_std_attribute_p (parser));
29455 /* Return true iff the next Nth tokens can be the beginning of either
29456 a GNU attribute list, or a standard C++11 attribute sequence. */
29458 static bool
29459 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
29461 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
29462 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
29465 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
29466 of GNU attributes, or return NULL. */
29468 static tree
29469 cp_parser_attributes_opt (cp_parser *parser)
29471 tree attrs = NULL_TREE;
29472 while (true)
29474 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
29475 attrs = attr_chainon (attrs, cp_parser_gnu_attributes_opt (parser));
29476 else if (cp_next_tokens_can_be_std_attribute_p (parser))
29477 attrs = attr_chainon (attrs, cp_parser_std_attribute_spec_seq (parser));
29478 else
29479 break;
29481 return attrs;
29484 /* Parse an (optional) series of attributes.
29486 attributes:
29487 attributes attribute
29489 attribute:
29490 __attribute__ (( attribute-list [opt] ))
29492 The return value is as for cp_parser_gnu_attribute_list. */
29494 static tree
29495 cp_parser_gnu_attributes_opt (cp_parser* parser)
29497 tree attributes = NULL_TREE;
29499 auto cleanup = make_temp_override
29500 (parser->auto_is_implicit_function_template_parm_p, false);
29502 while (true)
29504 cp_token *token;
29505 tree attribute_list;
29506 bool ok = true;
29508 /* Peek at the next token. */
29509 token = cp_lexer_peek_token (parser->lexer);
29510 /* If it's not `__attribute__', then we're done. */
29511 if (token->keyword != RID_ATTRIBUTE)
29512 break;
29514 /* Consume the `__attribute__' keyword. */
29515 cp_lexer_consume_token (parser->lexer);
29516 /* Look for the two `(' tokens. */
29517 matching_parens outer_parens;
29518 if (!outer_parens.require_open (parser))
29519 ok = false;
29520 matching_parens inner_parens;
29521 if (!inner_parens.require_open (parser))
29522 ok = false;
29524 /* Peek at the next token. */
29525 token = cp_lexer_peek_token (parser->lexer);
29526 if (token->type != CPP_CLOSE_PAREN)
29527 /* Parse the attribute-list. */
29528 attribute_list = cp_parser_gnu_attribute_list (parser);
29529 else
29530 /* If the next token is a `)', then there is no attribute
29531 list. */
29532 attribute_list = NULL;
29534 /* Look for the two `)' tokens. */
29535 if (!inner_parens.require_close (parser))
29536 ok = false;
29537 if (!outer_parens.require_close (parser))
29538 ok = false;
29539 if (!ok)
29540 cp_parser_skip_to_end_of_statement (parser);
29542 /* Add these new attributes to the list. */
29543 attributes = attr_chainon (attributes, attribute_list);
29546 return attributes;
29549 /* Parse a GNU attribute-list.
29551 attribute-list:
29552 attribute
29553 attribute-list , attribute
29555 attribute:
29556 identifier
29557 identifier ( identifier )
29558 identifier ( identifier , expression-list )
29559 identifier ( expression-list )
29561 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
29562 to an attribute. The TREE_PURPOSE of each node is the identifier
29563 indicating which attribute is in use. The TREE_VALUE represents
29564 the arguments, if any. */
29566 static tree
29567 cp_parser_gnu_attribute_list (cp_parser* parser, bool exactly_one /* = false */)
29569 tree attribute_list = NULL_TREE;
29570 bool save_translate_strings_p = parser->translate_strings_p;
29572 /* Don't create wrapper nodes within attributes: the
29573 handlers don't know how to handle them. */
29574 auto_suppress_location_wrappers sentinel;
29576 parser->translate_strings_p = false;
29577 while (true)
29579 cp_token *token;
29580 tree identifier;
29581 tree attribute;
29583 /* Look for the identifier. We also allow keywords here; for
29584 example `__attribute__ ((const))' is legal. */
29585 token = cp_lexer_peek_token (parser->lexer);
29586 if (token->type == CPP_NAME
29587 || token->type == CPP_KEYWORD)
29589 tree arguments = NULL_TREE;
29591 /* Consume the token, but save it since we need it for the
29592 SIMD enabled function parsing. */
29593 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
29595 /* Save away the identifier that indicates which attribute
29596 this is. */
29597 identifier = (token->type == CPP_KEYWORD)
29598 /* For keywords, use the canonical spelling, not the
29599 parsed identifier. */
29600 ? ridpointers[(int) token->keyword]
29601 : id_token->u.value;
29603 identifier = canonicalize_attr_name (identifier);
29604 attribute = build_tree_list (identifier, NULL_TREE);
29606 /* Peek at the next token. */
29607 token = cp_lexer_peek_token (parser->lexer);
29608 /* If it's an `(', then parse the attribute arguments. */
29609 if (token->type == CPP_OPEN_PAREN)
29611 vec<tree, va_gc> *vec;
29612 int attr_flag = (attribute_takes_identifier_p (identifier)
29613 ? id_attr : normal_attr);
29614 if (is_attribute_p ("assume", identifier))
29615 attr_flag = assume_attr;
29616 vec = cp_parser_parenthesized_expression_list
29617 (parser, attr_flag, /*cast_p=*/false,
29618 /*allow_expansion_p=*/false,
29619 /*non_constant_p=*/NULL);
29620 if (vec == NULL)
29621 arguments = error_mark_node;
29622 else
29624 arguments = build_tree_list_vec (vec);
29625 release_tree_vector (vec);
29627 /* Save the arguments away. */
29628 TREE_VALUE (attribute) = arguments;
29631 if (arguments != error_mark_node)
29633 /* Add this attribute to the list. */
29634 TREE_CHAIN (attribute) = attribute_list;
29635 attribute_list = attribute;
29638 token = cp_lexer_peek_token (parser->lexer);
29640 /* Unless EXACTLY_ONE is set look for more attributes.
29641 If the next token isn't a `,', we're done. */
29642 if (exactly_one || token->type != CPP_COMMA)
29643 break;
29645 /* Consume the comma and keep going. */
29646 cp_lexer_consume_token (parser->lexer);
29648 parser->translate_strings_p = save_translate_strings_p;
29650 /* We built up the list in reverse order. */
29651 return nreverse (attribute_list);
29654 /* Parse arguments of omp::directive attribute.
29656 ( directive-name ,[opt] clause-list[opt] )
29658 For directive just remember the first/last tokens for subsequent
29659 parsing. */
29661 static void
29662 cp_parser_omp_directive_args (cp_parser *parser, tree attribute, bool decl_p)
29664 cp_token *first = cp_lexer_peek_nth_token (parser->lexer, 2);
29665 if (first->type == CPP_CLOSE_PAREN)
29667 cp_lexer_consume_token (parser->lexer);
29668 error_at (first->location, "expected OpenMP directive name");
29669 cp_lexer_consume_token (parser->lexer);
29670 TREE_VALUE (attribute) = NULL_TREE;
29671 return;
29673 size_t n = cp_parser_skip_balanced_tokens (parser, 1);
29674 if (n == 1)
29676 cp_lexer_consume_token (parser->lexer);
29677 error_at (first->location, "expected attribute argument as balanced "
29678 "token sequence");
29679 TREE_VALUE (attribute) = NULL_TREE;
29680 return;
29682 for (n = n - 2; n; --n)
29683 cp_lexer_consume_token (parser->lexer);
29684 cp_token *last = cp_lexer_peek_token (parser->lexer);
29685 cp_lexer_consume_token (parser->lexer);
29686 tree arg = make_node (DEFERRED_PARSE);
29687 DEFPARSE_TOKENS (arg) = cp_token_cache_new (first, last);
29688 DEFPARSE_INSTANTIATIONS (arg) = nullptr;
29689 if (decl_p)
29690 TREE_PUBLIC (arg) = 1;
29691 TREE_VALUE (attribute) = tree_cons (NULL_TREE, arg, TREE_VALUE (attribute));
29694 /* Parse arguments of omp::sequence attribute.
29696 ( omp::[opt] directive-attr [ , omp::[opt] directive-attr ]... ) */
29698 static void
29699 cp_parser_omp_sequence_args (cp_parser *parser, tree attribute)
29701 matching_parens parens;
29702 parens.consume_open (parser);
29705 cp_token *token = cp_lexer_peek_token (parser->lexer);
29706 if (token->type == CPP_NAME
29707 && token->u.value == omp_identifier
29708 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
29710 cp_lexer_consume_token (parser->lexer);
29711 cp_lexer_consume_token (parser->lexer);
29712 token = cp_lexer_peek_token (parser->lexer);
29714 bool directive = false;
29715 const char *p;
29716 if (token->type != CPP_NAME)
29717 p = "";
29718 else
29719 p = IDENTIFIER_POINTER (token->u.value);
29720 if (strcmp (p, "directive") == 0)
29721 directive = true;
29722 else if (strcmp (p, "sequence") != 0)
29724 error_at (token->location, "expected %<directive%> or %<sequence%>");
29725 cp_parser_skip_to_closing_parenthesis (parser,
29726 /*recovering=*/true,
29727 /*or_comma=*/true,
29728 /*consume_paren=*/false);
29729 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
29730 break;
29731 cp_lexer_consume_token (parser->lexer);
29733 cp_lexer_consume_token (parser->lexer);
29734 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
29735 cp_parser_required_error (parser, RT_OPEN_PAREN, false,
29736 UNKNOWN_LOCATION);
29737 else if (directive)
29738 cp_parser_omp_directive_args (parser, attribute, false);
29739 else
29740 cp_parser_omp_sequence_args (parser, attribute);
29741 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
29742 break;
29743 cp_lexer_consume_token (parser->lexer);
29745 while (1);
29746 if (!parens.require_close (parser))
29747 cp_parser_skip_to_closing_parenthesis (parser, true, false,
29748 /*consume_paren=*/true);
29751 /* Parse a standard C++11 attribute.
29753 The returned representation is a TREE_LIST which TREE_PURPOSE is
29754 the scoped name of the attribute, and the TREE_VALUE is its
29755 arguments list.
29757 Note that the scoped name of the attribute is itself a TREE_LIST
29758 which TREE_PURPOSE is the namespace of the attribute, and
29759 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
29760 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
29761 and which TREE_PURPOSE is directly the attribute name.
29763 Clients of the attribute code should use get_attribute_namespace
29764 and get_attribute_name to get the actual namespace and name of
29765 attributes, regardless of their being GNU or C++11 attributes.
29767 attribute:
29768 attribute-token attribute-argument-clause [opt]
29770 attribute-token:
29771 identifier
29772 attribute-scoped-token
29774 attribute-scoped-token:
29775 attribute-namespace :: identifier
29777 attribute-namespace:
29778 identifier
29780 attribute-argument-clause:
29781 ( balanced-token-seq )
29783 balanced-token-seq:
29784 balanced-token [opt]
29785 balanced-token-seq balanced-token
29787 balanced-token:
29788 ( balanced-token-seq )
29789 [ balanced-token-seq ]
29790 { balanced-token-seq }. */
29792 static tree
29793 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
29795 tree attribute, attr_id = NULL_TREE, arguments;
29796 cp_token *token;
29798 auto cleanup = make_temp_override
29799 (parser->auto_is_implicit_function_template_parm_p, false);
29801 /* First, parse name of the attribute, a.k.a attribute-token. */
29803 token = cp_lexer_peek_token (parser->lexer);
29804 if (token->type == CPP_NAME)
29805 attr_id = token->u.value;
29806 else if (token->type == CPP_KEYWORD)
29807 attr_id = ridpointers[(int) token->keyword];
29808 else if (token->flags & NAMED_OP)
29809 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
29811 if (attr_id == NULL_TREE)
29812 return NULL_TREE;
29814 cp_lexer_consume_token (parser->lexer);
29816 token = cp_lexer_peek_token (parser->lexer);
29817 if (token->type == CPP_SCOPE)
29819 /* We are seeing a scoped attribute token. */
29821 cp_lexer_consume_token (parser->lexer);
29822 if (attr_ns)
29823 error_at (token->location, "attribute using prefix used together "
29824 "with scoped attribute token");
29825 attr_ns = attr_id;
29827 token = cp_lexer_peek_token (parser->lexer);
29828 if (token->type == CPP_NAME)
29829 attr_id = token->u.value;
29830 else if (token->type == CPP_KEYWORD)
29831 attr_id = ridpointers[(int) token->keyword];
29832 else if (token->flags & NAMED_OP)
29833 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
29834 else
29836 error_at (token->location,
29837 "expected an identifier for the attribute name");
29838 return error_mark_node;
29840 cp_lexer_consume_token (parser->lexer);
29842 attr_ns = canonicalize_attr_name (attr_ns);
29843 attr_id = canonicalize_attr_name (attr_id);
29844 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
29845 NULL_TREE);
29846 token = cp_lexer_peek_token (parser->lexer);
29848 else if (attr_ns)
29850 attr_ns = canonicalize_attr_name (attr_ns);
29851 attr_id = canonicalize_attr_name (attr_id);
29852 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
29853 NULL_TREE);
29855 else
29857 attr_id = canonicalize_attr_name (attr_id);
29858 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
29859 NULL_TREE);
29861 /* We used to treat C++11 noreturn attribute as equivalent to GNU's,
29862 but no longer: we have to be able to tell [[noreturn]] and
29863 __attribute__((noreturn)) apart. */
29864 /* C++14 deprecated attribute is equivalent to GNU's. */
29865 if (is_attribute_p ("deprecated", attr_id))
29866 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
29867 /* C++17 fallthrough attribute is equivalent to GNU's. */
29868 else if (is_attribute_p ("fallthrough", attr_id))
29869 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
29870 /* C++23 assume attribute is equivalent to GNU's. */
29871 else if (is_attribute_p ("assume", attr_id))
29872 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
29873 /* Transactional Memory TS optimize_for_synchronized attribute is
29874 equivalent to GNU transaction_callable. */
29875 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
29876 TREE_PURPOSE (attribute)
29877 = get_identifier ("transaction_callable");
29878 /* Transactional Memory attributes are GNU attributes. */
29879 else if (tm_attr_to_mask (attr_id))
29880 TREE_PURPOSE (attribute) = attr_id;
29883 /* Now parse the optional argument clause of the attribute. */
29885 if (token->type != CPP_OPEN_PAREN)
29887 if ((flag_openmp || flag_openmp_simd)
29888 && attr_ns == omp_identifier
29889 && (is_attribute_p ("directive", attr_id)
29890 || is_attribute_p ("sequence", attr_id)
29891 || is_attribute_p ("decl", attr_id)))
29893 error_at (token->location, "%<omp::%E%> attribute requires argument",
29894 attr_id);
29895 return NULL_TREE;
29897 return attribute;
29901 vec<tree, va_gc> *vec;
29902 int attr_flag = normal_attr;
29904 /* Maybe we don't expect to see any arguments for this attribute. */
29905 const attribute_spec *as
29906 = lookup_attribute_spec (TREE_PURPOSE (attribute));
29907 if (as && as->max_length == 0)
29909 error_at (token->location, "%qE attribute does not take any arguments",
29910 attr_id);
29911 cp_parser_skip_to_closing_parenthesis (parser,
29912 /*recovering=*/true,
29913 /*or_comma=*/false,
29914 /*consume_paren=*/true);
29915 return error_mark_node;
29918 if (is_attribute_p ("assume", attr_id)
29919 && (attr_ns == NULL_TREE || attr_ns == gnu_identifier))
29920 /* The assume attribute needs special handling of the argument. */
29921 attr_flag = assume_attr;
29922 else if (attr_ns == gnu_identifier
29923 && attribute_takes_identifier_p (attr_id))
29924 /* A GNU attribute that takes an identifier in parameter. */
29925 attr_flag = id_attr;
29926 else if (attr_ns == NULL_TREE
29927 && cxx_dialect >= cxx26
29928 && (is_attribute_p ("deprecated", attr_id)
29929 || is_attribute_p ("nodiscard", attr_id)))
29930 attr_flag = uneval_string_attr;
29932 /* If this is a fake attribute created to handle -Wno-attributes,
29933 we must skip parsing the arguments. */
29934 if (as == NULL || attribute_ignored_p (as))
29936 if ((flag_openmp || flag_openmp_simd) && attr_ns == omp_identifier)
29938 if (is_attribute_p ("directive", attr_id))
29940 cp_parser_omp_directive_args (parser, attribute, false);
29941 return attribute;
29943 else if (is_attribute_p ("decl", attr_id))
29945 TREE_VALUE (TREE_PURPOSE (attribute))
29946 = get_identifier ("directive");
29947 cp_parser_omp_directive_args (parser, attribute, true);
29948 return attribute;
29950 else if (is_attribute_p ("sequence", attr_id))
29952 TREE_VALUE (TREE_PURPOSE (attribute))
29953 = get_identifier ("directive");
29954 cp_parser_omp_sequence_args (parser, attribute);
29955 TREE_VALUE (attribute) = nreverse (TREE_VALUE (attribute));
29956 return attribute;
29960 /* For unknown attributes, just skip balanced tokens instead of
29961 trying to parse the arguments. Set TREE_VALUE (attribute) to
29962 error_mark_node to distinguish skipped arguments from attributes
29963 with no arguments. */
29964 for (size_t n = cp_parser_skip_balanced_tokens (parser, 1) - 1; n; --n)
29965 cp_lexer_consume_token (parser->lexer);
29966 TREE_VALUE (attribute) = error_mark_node;
29967 return attribute;
29970 vec = cp_parser_parenthesized_expression_list
29971 (parser, attr_flag, /*cast_p=*/false,
29972 /*allow_expansion_p=*/true,
29973 /*non_constant_p=*/NULL);
29974 if (vec == NULL)
29975 arguments = error_mark_node;
29976 else
29978 if (vec->is_empty ())
29979 /* e.g. [[attr()]]. */
29980 error_at (token->location, "parentheses must be omitted if "
29981 "%qE attribute argument list is empty",
29982 attr_id);
29983 arguments = build_tree_list_vec (vec);
29984 release_tree_vector (vec);
29987 if (arguments == error_mark_node)
29988 attribute = error_mark_node;
29989 else
29990 TREE_VALUE (attribute) = arguments;
29993 return attribute;
29996 /* Warn if the attribute ATTRIBUTE appears more than once in the
29997 attribute-list ATTRIBUTES. This used to be enforced for certain
29998 attributes, but the restriction was removed in P2156.
29999 LOC is the location of ATTRIBUTE. Returns true if ATTRIBUTE was not
30000 found in ATTRIBUTES. */
30002 static bool
30003 cp_parser_check_std_attribute (location_t loc, tree attributes, tree attribute)
30005 static auto alist = { "noreturn", "deprecated", "nodiscard", "maybe_unused",
30006 "likely", "unlikely", "fallthrough",
30007 "no_unique_address", "carries_dependency" };
30008 if (attributes)
30009 for (const auto &a : alist)
30010 if (is_attribute_p (a, get_attribute_name (attribute))
30011 && is_attribute_namespace_p ("", attribute)
30012 && lookup_attribute ("", a, attributes))
30014 if (!from_macro_expansion_at (loc))
30015 warning_at (loc, OPT_Wattributes, "attribute %qs specified "
30016 "multiple times", a);
30017 return false;
30019 return true;
30022 /* Parse a list of standard C++-11 attributes.
30024 attribute-list:
30025 attribute [opt]
30026 attribute-list , attribute[opt]
30027 attribute ...
30028 attribute-list , attribute ...
30031 static tree
30032 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
30034 tree attributes = NULL_TREE, attribute = NULL_TREE;
30035 cp_token *token = NULL;
30037 while (true)
30039 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30040 attribute = cp_parser_std_attribute (parser, attr_ns);
30041 if (attribute == error_mark_node)
30042 break;
30043 if (attribute != NULL_TREE)
30045 if (cp_parser_check_std_attribute (loc, attributes, attribute))
30047 TREE_CHAIN (attribute) = attributes;
30048 attributes = attribute;
30051 token = cp_lexer_peek_token (parser->lexer);
30052 if (token->type == CPP_ELLIPSIS)
30054 cp_lexer_consume_token (parser->lexer);
30055 if (attribute == NULL_TREE)
30056 error_at (token->location,
30057 "expected attribute before %<...%>");
30058 else if (TREE_VALUE (attribute) == NULL_TREE)
30060 error_at (token->location, "attribute with no arguments "
30061 "contains no parameter packs");
30062 return error_mark_node;
30064 else if (TREE_VALUE (attribute) != error_mark_node)
30066 tree pack = make_pack_expansion (TREE_VALUE (attribute));
30067 if (pack == error_mark_node)
30068 return error_mark_node;
30069 TREE_VALUE (attribute) = pack;
30071 token = cp_lexer_peek_token (parser->lexer);
30073 if (token->type != CPP_COMMA)
30074 break;
30075 cp_lexer_consume_token (parser->lexer);
30077 attributes = nreverse (attributes);
30078 return attributes;
30081 /* Optionally parse a C++20 contract role. A NULL return means that no
30082 contract role was specified.
30084 contract-role:
30085 % default
30086 % identifier
30088 If the identifier does not name a known contract role, it will
30089 be assumed to be default. Returns the identifier for the role
30090 token. */
30092 static tree
30093 cp_parser_contract_role (cp_parser *parser)
30095 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_MOD));
30096 cp_lexer_consume_token (parser->lexer);
30098 cp_token *token = cp_lexer_peek_token (parser->lexer);
30099 tree role_id = NULL_TREE;
30100 if (token->type == CPP_NAME)
30101 role_id = token->u.value;
30102 else if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
30103 role_id = get_identifier ("default");
30104 else
30106 error_at (token->location, "expected contract-role");
30107 return error_mark_node;
30109 cp_lexer_consume_token (parser->lexer);
30111 /* FIXME: Warn about invalid/unknown roles? */
30112 return role_id;
30115 /* Parse an optional contract mode.
30117 contract-mode:
30118 contract-semantic
30119 [contract-level] [contract-role]
30121 contract-semantic:
30122 check_never_continue
30123 check_maybe_continue
30124 check_always_continue
30126 contract-level:
30127 default
30128 audit
30129 axiom
30131 contract-role:
30132 default
30133 identifier
30135 This grammar is taken from P1332R0. During parsing, this sets options
30136 on the MODE object to determine the configuration of the contract.
30138 Returns a tree containing the identifiers used in the configuration.
30139 This is either an IDENTIFIER with the literal semantic or a TREE_LIST
30140 whose TREE_VALUE is the contract-level and whose TREE_PURPOSE is the
30141 contract-role, if any. NULL_TREE is returned if no information is
30142 given (i.e., all defaults selected). */
30144 static tree
30145 cp_parser_contract_mode_opt (cp_parser *parser,
30146 bool postcondition_p)
30148 /* The mode is empty; the level and role are default. */
30149 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
30150 return NULL_TREE;
30152 /* There is only a role; the level is default. */
30153 if (cp_lexer_next_token_is (parser->lexer, CPP_MOD))
30155 tree role_id = cp_parser_contract_role (parser);
30156 return build_tree_list (role_id, get_identifier ("default"));
30159 /* Otherwise, match semantic or level. */
30160 cp_token *token = cp_lexer_peek_token (parser->lexer);
30161 contract_level level = CONTRACT_INVALID;
30162 contract_semantic semantic = CCS_INVALID;
30163 tree config_id;
30164 if (token->type == CPP_NAME)
30166 config_id = token->u.value;
30168 /* Either a named level, a concrete semantic, or an identifier
30169 for a postcondition. */
30170 const char *ident = IDENTIFIER_POINTER (token->u.value);
30171 level = map_contract_level (ident);
30172 semantic = map_contract_semantic (ident);
30174 /* The identifier is the return value for a postcondition. */
30175 if (level == CONTRACT_INVALID && semantic == CCS_INVALID
30176 && postcondition_p)
30177 return NULL_TREE;
30179 else if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
30181 config_id = get_identifier ("default");
30182 level = CONTRACT_DEFAULT;
30184 else
30186 /* We got some other token other than a ':'. */
30187 error_at (token->location, "expected contract semantic or level");
30188 return NULL_TREE;
30191 /* Consume the literal semantic or level token. */
30192 cp_lexer_consume_token (parser->lexer);
30194 if (semantic == CCS_INVALID && level == CONTRACT_INVALID)
30196 error_at (token->location,
30197 "expected contract level: "
30198 "%<default%>, %<audit%>, or %<axiom%>");
30199 return NULL_TREE;
30202 /* We matched an explicit semantic. */
30203 if (semantic != CCS_INVALID)
30205 if (cp_lexer_next_token_is (parser->lexer, CPP_MOD))
30207 error ("invalid use of contract role for explicit semantic");
30208 cp_lexer_consume_token (parser->lexer);
30209 cp_lexer_consume_token (parser->lexer);
30211 return config_id;
30214 /* We matched a level, there may be a role; otherwise this is default. */
30215 if (cp_lexer_next_token_is (parser->lexer, CPP_MOD))
30217 tree role_id = cp_parser_contract_role (parser);
30218 return build_tree_list (role_id, config_id);
30221 return build_tree_list (NULL_TREE, config_id);
30224 static tree
30225 find_error (tree *tp, int *, void *)
30227 if (*tp == error_mark_node)
30228 return *tp;
30229 return NULL_TREE;
30232 static bool
30233 contains_error_p (tree t)
30235 return walk_tree (&t, find_error, NULL, NULL);
30238 /* Parse a standard C++20 contract attribute specifier.
30240 contract-attribute-specifier:
30241 [ [ assert contract-level [opt] : conditional-expression ] ]
30242 [ [ pre contract-level [opt] : conditional-expression ] ]
30243 [ [ post contract-level [opt] identifier [opt] : conditional-expression ] ]
30245 For free functions, we cannot determine the type of the postcondition
30246 identifier because the we haven't called grokdeclarator yet. In those
30247 cases we parse the postcondition as if the identifier was declared as
30248 'auto <identifier>'. We then instantiate the postcondition once the
30249 return type is known.
30251 For member functions, contracts are in the complete-class context, so the
30252 parse is deferred. We also have the return type avaialable (unless it's
30253 deduced), so we don't need to parse the postcondition in terms of a
30254 placeholder. */
30256 static tree
30257 cp_parser_contract_attribute_spec (cp_parser *parser, tree attribute)
30259 gcc_assert (contract_attribute_p (attribute));
30260 cp_token *token = cp_lexer_consume_token (parser->lexer);
30261 location_t loc = token->location;
30263 bool assertion_p = is_attribute_p ("assert", attribute);
30264 bool postcondition_p = is_attribute_p ("post", attribute);
30266 /* Parse the optional mode. */
30267 tree mode = cp_parser_contract_mode_opt (parser, postcondition_p);
30269 /* Check for postcondition identifiers. */
30270 cp_expr identifier;
30271 if (postcondition_p && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30272 identifier = cp_parser_identifier (parser);
30273 if (identifier == error_mark_node)
30274 return error_mark_node;
30276 cp_parser_require (parser, CPP_COLON, RT_COLON);
30278 /* Defer the parsing of pre/post contracts inside class definitions. */
30279 tree contract;
30280 if (!assertion_p &&
30281 current_class_type &&
30282 TYPE_BEING_DEFINED (current_class_type))
30284 /* Skip until we reach an unenclose ']'. If we ran into an unnested ']'
30285 that doesn't close the attribute, return an error and let the attribute
30286 handling code emit an error for missing ']]'. */
30287 cp_token *first = cp_lexer_peek_token (parser->lexer);
30288 cp_parser_skip_to_closing_parenthesis_1 (parser,
30289 /*recovering=*/false,
30290 CPP_CLOSE_SQUARE,
30291 /*consume_paren=*/false);
30292 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE
30293 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_CLOSE_SQUARE)
30294 return error_mark_node;
30295 cp_token *last = cp_lexer_peek_token (parser->lexer);
30297 /* Build a deferred-parse node. */
30298 tree condition = make_node (DEFERRED_PARSE);
30299 DEFPARSE_TOKENS (condition) = cp_token_cache_new (first, last);
30300 DEFPARSE_INSTANTIATIONS (condition) = NULL;
30302 /* And its corresponding contract. */
30303 contract = grok_contract (attribute, mode, identifier, condition, loc);
30305 else
30307 /* Enable location wrappers when parsing contracts. */
30308 auto suppression = make_temp_override (suppress_location_wrappers, 0);
30310 /* Build a fake variable for the result identifier. */
30311 tree result = NULL_TREE;
30312 if (identifier)
30314 begin_scope (sk_block, NULL_TREE);
30315 result = make_postcondition_variable (identifier);
30316 ++processing_template_decl;
30319 /* Parse the condition, ensuring that parameters or the return variable
30320 aren't flagged for use outside the body of a function. */
30321 ++processing_contract_condition;
30322 cp_expr condition = cp_parser_conditional_expression (parser);
30323 --processing_contract_condition;
30325 /* Try to recover from errors by scanning up to the end of the
30326 attribute. Sometimes we get partially parsed expressions, so
30327 we need to search the condition for errors. */
30328 if (contains_error_p (condition))
30329 cp_parser_skip_up_to_closing_square_bracket (parser);
30331 /* Build the contract. */
30332 contract = grok_contract (attribute, mode, result, condition, loc);
30334 /* Leave our temporary scope for the postcondition result. */
30335 if (result)
30337 --processing_template_decl;
30338 pop_bindings_and_leave_scope ();
30342 if (!flag_contracts)
30344 error_at (loc, "contracts are only available with %<-fcontracts%>");
30345 return error_mark_node;
30348 return finish_contract_attribute (attribute, contract);
30351 /* Parse a contract condition for a deferred contract. */
30353 void cp_parser_late_contract_condition (cp_parser *parser,
30354 tree fn,
30355 tree attribute)
30357 tree contract = TREE_VALUE (TREE_VALUE (attribute));
30359 /* Make sure we've gotten something that hasn't been parsed yet or that
30360 we're not parsing an invalid contract. */
30361 tree condition = CONTRACT_CONDITION (contract);
30362 if (TREE_CODE (condition) != DEFERRED_PARSE)
30363 return;
30365 tree identifier = NULL_TREE;
30366 if (TREE_CODE (contract) == POSTCONDITION_STMT)
30367 identifier = POSTCONDITION_IDENTIFIER (contract);
30369 /* Build a fake variable for the result identifier. */
30370 tree result = NULL_TREE;
30371 if (identifier)
30373 /* TODO: Can we guarantee that the identifier has a location? */
30374 location_t loc = cp_expr_location (contract);
30375 tree type = TREE_TYPE (TREE_TYPE (fn));
30376 if (!check_postcondition_result (fn, type, loc))
30378 invalidate_contract (contract);
30379 return;
30382 begin_scope (sk_block, NULL_TREE);
30383 result = make_postcondition_variable (identifier, type);
30384 ++processing_template_decl;
30387 /* 'this' is not allowed in preconditions of constructors or in postconditions
30388 of destructors. Note that the previous value of this variable is
30389 established by the calling function, so we need to save it here. */
30390 tree saved_ccr = current_class_ref;
30391 tree saved_ccp = current_class_ptr;
30392 if ((DECL_CONSTRUCTOR_P (fn) && PRECONDITION_P (contract)) ||
30393 (DECL_DESTRUCTOR_P (fn) && POSTCONDITION_P (contract)))
30395 current_class_ref = current_class_ptr = NULL_TREE;
30396 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
30399 push_unparsed_function_queues (parser);
30401 /* Push the saved tokens onto the parser's lexer stack. */
30402 cp_token_cache *tokens = DEFPARSE_TOKENS (condition);
30403 cp_parser_push_lexer_for_tokens (parser, tokens);
30405 /* Parse the condition, ensuring that parameters or the return variable
30406 aren't flagged for use outside the body of a function. */
30407 ++processing_contract_condition;
30408 condition = cp_parser_conditional_expression (parser);
30409 --processing_contract_condition;
30411 /* Revert to the main lexer. */
30412 cp_parser_pop_lexer (parser);
30414 /* Restore the queue. */
30415 pop_unparsed_function_queues (parser);
30417 current_class_ref = saved_ccr;
30418 current_class_ptr = saved_ccp;
30420 /* Commit to changes. */
30421 update_late_contract (contract, result, condition);
30423 /* Leave our temporary scope for the postcondition result. */
30424 if (result)
30426 --processing_template_decl;
30427 pop_bindings_and_leave_scope ();
30431 /* Parse a standard C++-11 attribute specifier.
30433 attribute-specifier:
30434 [ [ attribute-using-prefix [opt] attribute-list ] ]
30435 contract-attribute-specifier
30436 alignment-specifier
30438 attribute-using-prefix:
30439 using attribute-namespace :
30441 alignment-specifier:
30442 alignas ( type-id ... [opt] )
30443 alignas ( alignment-expression ... [opt] ).
30445 Extensions for contracts:
30447 contract-attribute-specifier:
30448 [ [ assert : contract-mode [opt] : conditional-expression ] ]
30449 [ [ pre : contract-mode [opt] : conditional-expression ] ]
30450 [ [ post : contract-mode [opt] identifier [opt] :
30451 conditional-expression ] ]
30453 Return void_list_node if the current token doesn't start an
30454 attribute-specifier to differentiate from NULL_TREE returned e.g.
30455 for [ [ ] ]. */
30457 static tree
30458 cp_parser_std_attribute_spec (cp_parser *parser)
30460 tree attributes = NULL_TREE;
30461 cp_token *token = cp_lexer_peek_token (parser->lexer);
30463 if (token->type == CPP_OPEN_SQUARE
30464 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
30466 tree attr_ns = NULL_TREE;
30467 tree attr_name = NULL_TREE;
30469 cp_lexer_consume_token (parser->lexer);
30470 cp_lexer_consume_token (parser->lexer);
30472 token = cp_lexer_peek_token (parser->lexer);
30473 if (token->type == CPP_NAME)
30475 attr_name = token->u.value;
30476 attr_name = canonicalize_attr_name (attr_name);
30479 /* Handle contract-attribute-specs specially. */
30480 if (attr_name && contract_attribute_p (attr_name))
30482 tree attrs = cp_parser_contract_attribute_spec (parser, attr_name);
30483 if (attrs != error_mark_node)
30484 attributes = attrs;
30485 goto finish_attrs;
30488 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
30490 token = cp_lexer_peek_nth_token (parser->lexer, 2);
30491 if (token->type == CPP_NAME)
30492 attr_ns = token->u.value;
30493 else if (token->type == CPP_KEYWORD)
30494 attr_ns = ridpointers[(int) token->keyword];
30495 else if (token->flags & NAMED_OP)
30496 attr_ns = get_identifier (cpp_type2name (token->type,
30497 token->flags));
30498 if (attr_ns
30499 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
30501 if (cxx_dialect < cxx17)
30502 pedwarn (input_location, OPT_Wc__17_extensions,
30503 "attribute using prefix only available "
30504 "with %<-std=c++17%> or %<-std=gnu++17%>");
30506 cp_lexer_consume_token (parser->lexer);
30507 cp_lexer_consume_token (parser->lexer);
30508 cp_lexer_consume_token (parser->lexer);
30510 else
30511 attr_ns = NULL_TREE;
30514 attributes = cp_parser_std_attribute_list (parser, attr_ns);
30516 finish_attrs:
30517 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
30518 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
30519 cp_parser_skip_to_end_of_statement (parser);
30520 else
30521 /* Warn about parsing c++11 attribute in non-c++11 mode, only
30522 when we are sure that we have actually parsed them. */
30523 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
30525 else
30527 tree alignas_expr;
30529 /* Look for an alignment-specifier. */
30531 token = cp_lexer_peek_token (parser->lexer);
30533 if (token->type != CPP_KEYWORD
30534 || token->keyword != RID_ALIGNAS)
30535 return void_list_node;
30537 cp_lexer_consume_token (parser->lexer);
30538 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
30540 matching_parens parens;
30541 if (!parens.require_open (parser))
30542 return error_mark_node;
30544 cp_parser_parse_tentatively (parser);
30545 alignas_expr = cp_parser_type_id (parser);
30547 if (!cp_parser_parse_definitely (parser))
30549 alignas_expr = cp_parser_assignment_expression (parser);
30550 if (alignas_expr == error_mark_node)
30551 cp_parser_skip_to_end_of_statement (parser);
30552 if (alignas_expr == NULL_TREE
30553 || alignas_expr == error_mark_node)
30554 return alignas_expr;
30557 alignas_expr = cxx_alignas_expr (alignas_expr);
30558 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
30560 /* Handle alignas (pack...). */
30561 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30563 cp_lexer_consume_token (parser->lexer);
30564 alignas_expr = make_pack_expansion (alignas_expr);
30567 /* Something went wrong, so don't build the attribute. */
30568 if (alignas_expr == error_mark_node)
30569 return error_mark_node;
30571 /* Missing ')' means the code cannot possibly be valid; go ahead
30572 and commit to make sure we issue a hard error. */
30573 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
30574 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
30575 cp_parser_commit_to_tentative_parse (parser);
30577 if (!parens.require_close (parser))
30578 return error_mark_node;
30580 /* Build the C++-11 representation of an 'aligned'
30581 attribute. */
30582 attributes
30583 = build_tree_list (build_tree_list (gnu_identifier,
30584 aligned_identifier), alignas_expr);
30587 return attributes;
30590 /* Parse a standard C++-11 attribute-specifier-seq.
30592 attribute-specifier-seq:
30593 attribute-specifier-seq [opt] attribute-specifier */
30595 static tree
30596 cp_parser_std_attribute_spec_seq (cp_parser *parser)
30598 tree attr_specs = NULL_TREE;
30599 tree attr_last = NULL_TREE;
30601 /* Don't create wrapper nodes within attributes: the
30602 handlers don't know how to handle them. */
30603 auto_suppress_location_wrappers sentinel;
30605 while (true)
30607 tree attr_spec = cp_parser_std_attribute_spec (parser);
30608 if (attr_spec == void_list_node)
30609 break;
30610 /* Accept [[]][[]]; for which cp_parser_std_attribute_spec
30611 returns NULL_TREE as there are no attributes. */
30612 if (attr_spec == NULL_TREE)
30613 continue;
30614 if (attr_spec == error_mark_node)
30615 return error_mark_node;
30617 if (attr_last)
30618 TREE_CHAIN (attr_last) = attr_spec;
30619 else
30620 attr_specs = attr_last = attr_spec;
30621 attr_last = tree_last (attr_last);
30624 return attr_specs;
30627 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
30628 return index of the first token after balanced-token, or N on failure. */
30630 static size_t
30631 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
30633 size_t orig_n = n;
30634 int nparens = 0, nbraces = 0, nsquares = 0;
30636 switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
30638 case CPP_PRAGMA_EOL:
30639 if (!parser->lexer->in_pragma)
30640 break;
30641 /* FALLTHRU */
30642 case CPP_EOF:
30643 /* Ran out of tokens. */
30644 return orig_n;
30645 case CPP_OPEN_PAREN:
30646 ++nparens;
30647 break;
30648 case CPP_OPEN_BRACE:
30649 ++nbraces;
30650 break;
30651 case CPP_OPEN_SQUARE:
30652 ++nsquares;
30653 break;
30654 case CPP_CLOSE_PAREN:
30655 --nparens;
30656 break;
30657 case CPP_CLOSE_BRACE:
30658 --nbraces;
30659 break;
30660 case CPP_CLOSE_SQUARE:
30661 --nsquares;
30662 break;
30663 default:
30664 break;
30666 while (nparens || nbraces || nsquares);
30667 return n;
30670 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
30671 return index of the first token after the GNU attribute tokens, or N on
30672 failure. */
30674 static size_t
30675 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
30677 while (true)
30679 if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
30680 || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
30681 || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
30682 break;
30684 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
30685 if (n2 == n + 2)
30686 break;
30687 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
30688 break;
30689 n = n2 + 1;
30691 return n;
30694 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
30695 next token), return index of the first token after the standard C++11
30696 attribute tokens, or N on failure. */
30698 static size_t
30699 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
30701 while (true)
30703 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
30704 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
30706 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
30707 if (n2 == n + 1)
30708 break;
30709 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
30710 break;
30711 n = n2 + 1;
30713 else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
30714 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
30716 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
30717 if (n2 == n + 1)
30718 break;
30719 n = n2;
30721 else
30722 break;
30724 return n;
30727 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
30728 as the next token), return index of the first token after the attribute
30729 tokens, or N on failure. */
30731 static size_t
30732 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
30734 if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
30735 return cp_parser_skip_gnu_attributes_opt (parser, n);
30736 return cp_parser_skip_std_attribute_spec_seq (parser, n);
30739 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
30740 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
30741 current value of the PEDANTIC flag, regardless of whether or not
30742 the `__extension__' keyword is present. The caller is responsible
30743 for restoring the value of the PEDANTIC flag. */
30745 static bool
30746 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
30748 /* Save the old value of the PEDANTIC flag. */
30749 *saved_pedantic = pedantic;
30751 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
30753 /* Consume the `__extension__' token. */
30754 cp_lexer_consume_token (parser->lexer);
30755 /* We're not being pedantic while the `__extension__' keyword is
30756 in effect. */
30757 pedantic = 0;
30759 return true;
30762 return false;
30765 /* Parse a label declaration.
30767 label-declaration:
30768 __label__ label-declarator-seq ;
30770 label-declarator-seq:
30771 identifier , label-declarator-seq
30772 identifier */
30774 static void
30775 cp_parser_label_declaration (cp_parser* parser)
30777 /* Look for the `__label__' keyword. */
30778 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
30780 while (true)
30782 tree identifier;
30784 /* Look for an identifier. */
30785 identifier = cp_parser_identifier (parser);
30786 /* If we failed, stop. */
30787 if (identifier == error_mark_node)
30788 break;
30789 /* Declare it as a label. */
30790 finish_label_decl (identifier);
30791 /* If the next token is a `;', stop. */
30792 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30793 break;
30794 /* Look for the `,' separating the label declarations. */
30795 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
30798 /* Look for the final `;'. */
30799 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
30802 // -------------------------------------------------------------------------- //
30803 // Concept definitions
30805 static tree
30806 cp_parser_concept_definition (cp_parser *parser)
30808 /* A concept definition is an unevaluated context. */
30809 cp_unevaluated u;
30811 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_CONCEPT));
30812 cp_lexer_consume_token (parser->lexer);
30814 cp_expr id = cp_parser_identifier (parser);
30815 if (id == error_mark_node)
30817 cp_parser_skip_to_end_of_statement (parser);
30818 cp_parser_consume_semicolon_at_end_of_statement (parser);
30819 return NULL_TREE;
30822 tree attrs = cp_parser_attributes_opt (parser);
30824 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
30826 cp_parser_skip_to_end_of_statement (parser);
30827 cp_parser_consume_semicolon_at_end_of_statement (parser);
30828 return error_mark_node;
30831 processing_constraint_expression_sentinel parsing_constraint;
30832 tree init = cp_parser_constraint_expression (parser);
30833 if (init == error_mark_node)
30834 cp_parser_skip_to_end_of_statement (parser);
30836 /* Consume the trailing ';'. Diagnose the problem if it isn't there,
30837 but continue as if it were. */
30838 cp_parser_consume_semicolon_at_end_of_statement (parser);
30840 return finish_concept_definition (id, init, attrs);
30843 // -------------------------------------------------------------------------- //
30844 // Requires Clause
30846 /* Diagnose an expression that should appear in ()'s within a requires-clause
30847 and suggest where to place those parentheses. */
30849 static void
30850 cp_parser_diagnose_ungrouped_constraint_plain (location_t loc)
30852 error_at (loc, "expression must be enclosed in parentheses");
30855 static void
30856 cp_parser_diagnose_ungrouped_constraint_rich (location_t loc)
30858 gcc_rich_location richloc (loc);
30859 richloc.add_fixit_insert_before ("(");
30860 richloc.add_fixit_insert_after (")");
30861 error_at (&richloc, "expression must be enclosed in parentheses");
30864 /* Characterizes the likely kind of expression intended by a mis-written
30865 primary constraint. */
30866 enum primary_constraint_error
30868 pce_ok,
30869 pce_maybe_operator,
30870 pce_maybe_postfix
30873 /* Returns true if the token(s) following a primary-expression in a
30874 constraint-logical-* expression would require parentheses. */
30876 static primary_constraint_error
30877 cp_parser_constraint_requires_parens (cp_parser *parser, bool lambda_p)
30879 cp_token *token = cp_lexer_peek_token (parser->lexer);
30880 switch (token->type)
30882 default:
30883 return pce_ok;
30885 case CPP_EQ:
30887 /* An equal sign may be part of the definition of a function,
30888 and not an assignment operator, when parsing the expression
30889 for a trailing requires-clause. For example:
30891 template<typename T>
30892 struct S {
30893 S() requires C<T> = default;
30896 Don't try to reparse this a binary operator. */
30897 if (cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_DELETE)
30898 || cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_DEFAULT))
30899 return pce_ok;
30901 gcc_fallthrough ();
30904 /* Arithmetic operators. */
30905 case CPP_PLUS:
30906 case CPP_MINUS:
30907 case CPP_MULT:
30908 case CPP_DIV:
30909 case CPP_MOD:
30910 /* Bitwise operators. */
30911 case CPP_AND:
30912 case CPP_OR:
30913 case CPP_XOR:
30914 case CPP_RSHIFT:
30915 case CPP_LSHIFT:
30916 /* Relational operators. */
30917 case CPP_EQ_EQ:
30918 case CPP_NOT_EQ:
30919 case CPP_LESS:
30920 case CPP_GREATER:
30921 case CPP_LESS_EQ:
30922 case CPP_GREATER_EQ:
30923 case CPP_SPACESHIP:
30924 /* Pointer-to-member. */
30925 case CPP_DOT_STAR:
30926 case CPP_DEREF_STAR:
30927 /* Assignment operators. */
30928 case CPP_PLUS_EQ:
30929 case CPP_MINUS_EQ:
30930 case CPP_MULT_EQ:
30931 case CPP_DIV_EQ:
30932 case CPP_MOD_EQ:
30933 case CPP_AND_EQ:
30934 case CPP_OR_EQ:
30935 case CPP_XOR_EQ:
30936 case CPP_RSHIFT_EQ:
30937 case CPP_LSHIFT_EQ:
30938 /* Conditional operator */
30939 case CPP_QUERY:
30940 /* Unenclosed binary or conditional operator. */
30941 return pce_maybe_operator;
30943 case CPP_OPEN_PAREN:
30945 /* A primary constraint that precedes the parameter-list of a
30946 lambda expression is followed by an open paren.
30948 []<typename T> requires C (T a, T b) { ... }
30950 Don't try to re-parse this as a postfix expression. */
30951 if (lambda_p)
30952 return pce_ok;
30954 gcc_fallthrough ();
30956 case CPP_OPEN_SQUARE:
30958 /* A primary-constraint-expression followed by a '[[' is not a
30959 postfix expression. */
30960 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE))
30961 return pce_ok;
30963 gcc_fallthrough ();
30965 case CPP_PLUS_PLUS:
30966 case CPP_MINUS_MINUS:
30967 case CPP_DOT:
30968 /* Unenclosed postfix operator. */
30969 return pce_maybe_postfix;
30971 case CPP_DEREF:
30972 /* A primary constraint that precedes the lambda-declarator of a
30973 lambda expression is followed by trailing return type.
30975 []<typename T> requires C -> void {}
30977 Don't try to re-parse this as a postfix expression in
30978 C++23 and later. In C++20 ( needs to come in between but we
30979 allow it to be omitted with pedwarn. */
30980 if (lambda_p)
30981 return pce_ok;
30982 /* Unenclosed postfix operator. */
30983 return pce_maybe_postfix;
30987 /* Returns true if the next token begins a unary expression, preceded by
30988 an operator or keyword. */
30990 static bool
30991 cp_parser_unary_constraint_requires_parens (cp_parser *parser)
30993 cp_token *token = cp_lexer_peek_token (parser->lexer);
30994 switch (token->type)
30996 case CPP_NOT:
30997 case CPP_PLUS:
30998 case CPP_MINUS:
30999 case CPP_MULT:
31000 case CPP_COMPL:
31001 case CPP_PLUS_PLUS:
31002 case CPP_MINUS_MINUS:
31003 return true;
31005 case CPP_KEYWORD:
31007 switch (token->keyword)
31009 case RID_STATCAST:
31010 case RID_DYNCAST:
31011 case RID_REINTCAST:
31012 case RID_CONSTCAST:
31013 case RID_TYPEID:
31014 case RID_SIZEOF:
31015 case RID_ALIGNOF:
31016 case RID_NOEXCEPT:
31017 case RID_NEW:
31018 case RID_DELETE:
31019 case RID_THROW:
31020 return true;
31022 default:
31023 break;
31027 default:
31028 break;
31031 return false;
31034 /* Parse a primary expression within a constraint. */
31036 static cp_expr
31037 cp_parser_constraint_primary_expression (cp_parser *parser, bool lambda_p)
31039 /* If this looks like a unary expression, parse it as such, but diagnose
31040 it as ill-formed; it requires parens. */
31041 if (cp_parser_unary_constraint_requires_parens (parser))
31043 cp_expr e = cp_parser_assignment_expression (parser, NULL, false, false);
31044 cp_parser_diagnose_ungrouped_constraint_rich (e.get_location());
31045 return e;
31048 cp_lexer_save_tokens (parser->lexer);
31049 cp_id_kind idk;
31050 location_t loc = input_location;
31051 cp_expr expr = cp_parser_primary_expression (parser,
31052 /*address_p=*/false,
31053 /*cast_p=*/false,
31054 /*template_arg_p=*/false,
31055 &idk);
31056 expr.maybe_add_location_wrapper ();
31058 primary_constraint_error pce = pce_ok;
31059 if (expr != error_mark_node)
31061 /* The primary-expression could be part of an unenclosed non-logical
31062 compound expression. */
31063 pce = cp_parser_constraint_requires_parens (parser, lambda_p);
31065 if (pce == pce_ok)
31067 cp_lexer_commit_tokens (parser->lexer);
31068 return finish_constraint_primary_expr (expr);
31071 /* Retry the parse at a lower precedence. If that succeeds, diagnose the
31072 error, but return the expression as if it were valid. */
31073 cp_lexer_rollback_tokens (parser->lexer);
31074 cp_parser_parse_tentatively (parser);
31075 if (pce == pce_maybe_operator)
31076 expr = cp_parser_assignment_expression (parser, NULL, false, false);
31077 else
31078 expr = cp_parser_simple_cast_expression (parser);
31079 if (cp_parser_parse_definitely (parser))
31081 cp_parser_diagnose_ungrouped_constraint_rich (expr.get_location());
31082 return expr;
31085 /* Otherwise, something has gone very wrong, and we can't generate a more
31086 meaningful diagnostic or recover. */
31087 cp_parser_diagnose_ungrouped_constraint_plain (loc);
31088 return error_mark_node;
31091 /* Parse a constraint-logical-and-expression.
31093 constraint-logical-and-expression:
31094 primary-expression
31095 constraint-logical-and-expression '&&' primary-expression */
31097 static cp_expr
31098 cp_parser_constraint_logical_and_expression (cp_parser *parser, bool lambda_p)
31100 cp_expr lhs = cp_parser_constraint_primary_expression (parser, lambda_p);
31101 while (cp_lexer_next_token_is (parser->lexer, CPP_AND_AND))
31103 cp_token *op = cp_lexer_consume_token (parser->lexer);
31104 tree rhs = cp_parser_constraint_primary_expression (parser, lambda_p);
31105 lhs = finish_constraint_and_expr (op->location, lhs, rhs);
31107 return lhs;
31110 /* Parse a constraint-logical-or-expression.
31112 constraint-logical-or-expression:
31113 constraint-logical-and-expression
31114 constraint-logical-or-expression '||' constraint-logical-and-expression */
31116 static cp_expr
31117 cp_parser_constraint_logical_or_expression (cp_parser *parser, bool lambda_p)
31119 cp_expr lhs = cp_parser_constraint_logical_and_expression (parser, lambda_p);
31120 while (cp_lexer_next_token_is (parser->lexer, CPP_OR_OR))
31122 cp_token *op = cp_lexer_consume_token (parser->lexer);
31123 cp_expr rhs = cp_parser_constraint_logical_and_expression (parser, lambda_p);
31124 lhs = finish_constraint_or_expr (op->location, lhs, rhs);
31126 return lhs;
31129 /* Parse the expression after a requires-clause. This has a different grammar
31130 than that in the concepts TS. */
31132 static tree
31133 cp_parser_requires_clause_expression (cp_parser *parser, bool lambda_p)
31135 processing_constraint_expression_sentinel parsing_constraint;
31136 ++processing_template_decl;
31137 cp_expr expr = cp_parser_constraint_logical_or_expression (parser, lambda_p);
31138 --processing_template_decl;
31139 if (check_for_bare_parameter_packs (expr))
31140 expr = error_mark_node;
31141 return expr;
31144 /* Parse a expression after a requires clause.
31146 constraint-expression:
31147 logical-or-expression
31149 The required logical-or-expression must be a constant expression. Note
31150 that we don't check that the expression is constepxr here. We defer until
31151 we analyze constraints and then, we only check atomic constraints. */
31153 static tree
31154 cp_parser_constraint_expression (cp_parser *parser)
31156 processing_constraint_expression_sentinel parsing_constraint;
31157 ++processing_template_decl;
31158 cp_expr expr = cp_parser_binary_expression (parser, false, true,
31159 PREC_NOT_OPERATOR, NULL);
31160 --processing_template_decl;
31161 if (check_for_bare_parameter_packs (expr))
31162 expr = error_mark_node;
31163 expr.maybe_add_location_wrapper ();
31164 return expr;
31167 /* Optionally parse a requires clause:
31169 requires-clause:
31170 `requires` constraint-logical-or-expression.
31171 [ConceptsTS]
31172 `requires constraint-expression.
31174 LAMBDA_P is true when the requires-clause is parsed before the
31175 parameter-list of a lambda-declarator. */
31177 static tree
31178 cp_parser_requires_clause_opt (cp_parser *parser, bool lambda_p)
31180 /* A requires clause is an unevaluated context. */
31181 cp_unevaluated u;
31183 cp_token *tok = cp_lexer_peek_token (parser->lexer);
31184 if (tok->keyword != RID_REQUIRES)
31186 if (!flag_concepts && tok->type == CPP_NAME
31187 && tok->u.value == ridpointers[RID_REQUIRES])
31189 error_at (cp_lexer_peek_token (parser->lexer)->location,
31190 "%<requires%> only available with "
31191 "%<-std=c++20%> or %<-fconcepts%>");
31192 /* Parse and discard the requires-clause. */
31193 cp_lexer_consume_token (parser->lexer);
31194 cp_parser_constraint_expression (parser);
31196 return NULL_TREE;
31199 cp_token *tok2 = cp_lexer_peek_nth_token (parser->lexer, 2);
31200 if (tok2->type == CPP_OPEN_BRACE)
31202 /* An opening brace following the start of a requires-clause is
31203 ill-formed; the user likely forgot the second `requires' that
31204 would start a requires-expression. */
31205 gcc_rich_location richloc (tok2->location);
31206 richloc.add_fixit_insert_after (tok->location, " requires");
31207 error_at (&richloc, "missing additional %<requires%> to start "
31208 "a requires-expression");
31209 /* Don't consume the `requires', so that it's reused as the start of a
31210 requires-expression. */
31212 else
31213 cp_lexer_consume_token (parser->lexer);
31215 if (!flag_concepts_ts)
31216 return cp_parser_requires_clause_expression (parser, lambda_p);
31217 else
31218 return cp_parser_constraint_expression (parser);
31221 /*---------------------------------------------------------------------------
31222 Requires expressions
31223 ---------------------------------------------------------------------------*/
31225 /* Parse a requires expression
31227 requirement-expression:
31228 'requires' requirement-parameter-list [opt] requirement-body */
31230 static tree
31231 cp_parser_requires_expression (cp_parser *parser)
31233 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
31234 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
31236 /* Avoid committing to outer tentative parse. */
31237 tentative_firewall firewall (parser);
31239 /* This is definitely a requires-expression. */
31240 cp_parser_commit_to_tentative_parse (parser);
31242 tree parms, reqs;
31244 /* Local parameters are delared as variables within the scope
31245 of the expression. They are not visible past the end of
31246 the expression. Expressions within the requires-expression
31247 are unevaluated. */
31248 struct scope_sentinel
31250 scope_sentinel ()
31252 ++cp_unevaluated_operand;
31253 begin_scope (sk_function_parms, NULL_TREE);
31254 current_binding_level->requires_expression = true;
31257 ~scope_sentinel ()
31259 pop_bindings_and_leave_scope ();
31260 --cp_unevaluated_operand;
31262 } s;
31264 /* Parse the optional parameter list. */
31265 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31267 parms = cp_parser_requirement_parameter_list (parser);
31268 if (parms == error_mark_node)
31269 return error_mark_node;
31271 else
31272 parms = NULL_TREE;
31274 /* Parse the requirement body. */
31275 ++processing_template_decl;
31276 reqs = cp_parser_requirement_body (parser);
31277 --processing_template_decl;
31278 if (reqs == error_mark_node)
31279 return error_mark_node;
31282 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
31283 the parm chain. */
31284 grokparms (parms, &parms);
31285 loc = make_location (loc, loc, parser->lexer);
31286 tree expr = finish_requires_expr (loc, parms, reqs);
31287 if (!processing_template_decl)
31289 /* Perform semantic processing now to diagnose any invalid types and
31290 expressions. */
31291 int saved_errorcount = errorcount;
31292 tsubst_requires_expr (expr, NULL_TREE, tf_warning_or_error, NULL_TREE);
31293 if (errorcount > saved_errorcount)
31294 return error_mark_node;
31296 return expr;
31299 /* Parse a parameterized requirement.
31301 requirement-parameter-list:
31302 '(' parameter-declaration-clause ')' */
31304 static tree
31305 cp_parser_requirement_parameter_list (cp_parser *parser)
31307 matching_parens parens;
31308 if (!parens.require_open (parser))
31309 return error_mark_node;
31311 tree parms = (cp_parser_parameter_declaration_clause
31312 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
31314 if (!parens.require_close (parser))
31315 return error_mark_node;
31317 /* Modify the declared parameters by removing their context
31318 so they don't refer to the enclosing scope and explicitly
31319 indicating that they are constraint variables. */
31320 for (tree parm = parms; parm; parm = TREE_CHAIN (parm))
31322 if (parm == void_list_node || parm == explicit_void_list_node)
31323 break;
31324 tree decl = TREE_VALUE (parm);
31325 if (decl != error_mark_node)
31327 DECL_CONTEXT (decl) = NULL_TREE;
31328 CONSTRAINT_VAR_P (decl) = true;
31332 return parms;
31335 /* Parse the body of a requirement.
31337 requirement-body:
31338 '{' requirement-list '}' */
31339 static tree
31340 cp_parser_requirement_body (cp_parser *parser)
31342 matching_braces braces;
31343 if (!braces.require_open (parser))
31344 return error_mark_node;
31346 tree reqs = cp_parser_requirement_seq (parser);
31348 if (!braces.require_close (parser))
31349 return error_mark_node;
31351 return reqs;
31354 /* Parse a sequence of requirements.
31356 requirement-seq:
31357 requirement
31358 requirement-seq requirement */
31360 static tree
31361 cp_parser_requirement_seq (cp_parser *parser)
31363 tree result = NULL_TREE;
31366 tree req = cp_parser_requirement (parser);
31367 if (req != error_mark_node)
31368 result = tree_cons (NULL_TREE, req, result);
31370 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE)
31371 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF));
31373 /* If there are no valid requirements, this is not a valid expression. */
31374 if (!result)
31375 return error_mark_node;
31377 /* Reverse the order of requirements so they are analyzed in order. */
31378 return nreverse (result);
31381 /* Parse a syntactic requirement or type requirement.
31383 requirement:
31384 simple-requirement
31385 compound-requirement
31386 type-requirement
31387 nested-requirement */
31389 static tree
31390 cp_parser_requirement (cp_parser *parser)
31392 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
31393 return cp_parser_compound_requirement (parser);
31394 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
31396 /* It's probably a type-requirement. */
31397 cp_parser_parse_tentatively (parser);
31398 tree req = cp_parser_type_requirement (parser);
31399 if (cp_parser_parse_definitely (parser))
31400 return req;
31401 /* No, maybe it's something like typename T::type(); */
31402 cp_parser_parse_tentatively (parser);
31403 req = cp_parser_simple_requirement (parser);
31404 if (cp_parser_parse_definitely (parser))
31405 return req;
31406 /* Non-tentative for the error. */
31407 return cp_parser_type_requirement (parser);
31409 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
31410 return cp_parser_nested_requirement (parser);
31411 else
31412 return cp_parser_simple_requirement (parser);
31415 /* Parse a simple requirement.
31417 simple-requirement:
31418 expression ';' */
31420 static tree
31421 cp_parser_simple_requirement (cp_parser *parser)
31423 location_t start = cp_lexer_peek_token (parser->lexer)->location;
31424 cp_expr expr = cp_parser_expression (parser, NULL, false, false);
31425 if (expr == error_mark_node)
31426 cp_parser_skip_to_end_of_statement (parser);
31428 cp_parser_consume_semicolon_at_end_of_statement (parser);
31430 if (!expr || expr == error_mark_node)
31431 return error_mark_node;
31433 /* Sometimes we don't get locations, so use the cached token location
31434 as a reasonable approximation. */
31435 if (expr.get_location() == UNKNOWN_LOCATION)
31436 expr.set_location (start);
31438 for (tree t = expr; ; )
31440 if (TREE_CODE (t) == TRUTH_ANDIF_EXPR
31441 || TREE_CODE (t) == TRUTH_ORIF_EXPR)
31443 t = TREE_OPERAND (t, 0);
31444 continue;
31446 if (concept_check_p (t))
31448 gcc_rich_location richloc (get_start (start));
31449 richloc.add_fixit_insert_before (start, "requires ");
31450 warning_at (&richloc, OPT_Wmissing_requires, "testing "
31451 "if a concept-id is a valid expression; add "
31452 "%<requires%> to check satisfaction");
31454 break;
31457 return finish_simple_requirement (expr.get_location (), expr);
31460 /* Parse a type requirement
31462 type-requirement
31463 nested-name-specifier [opt] required-type-name ';'
31465 required-type-name:
31466 type-name
31467 'template' [opt] simple-template-id */
31469 static tree
31470 cp_parser_type_requirement (cp_parser *parser)
31472 cp_token *start_tok = cp_lexer_consume_token (parser->lexer);
31473 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31475 // Save the scope before parsing name specifiers.
31476 tree saved_scope = parser->scope;
31477 tree saved_object_scope = parser->object_scope;
31478 tree saved_qualifying_scope = parser->qualifying_scope;
31479 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
31480 cp_parser_nested_name_specifier_opt (parser,
31481 /*typename_keyword_p=*/true,
31482 /*check_dependency_p=*/true,
31483 /*type_p=*/true,
31484 /*is_declaration=*/false);
31486 tree type;
31487 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
31489 cp_lexer_consume_token (parser->lexer);
31490 type = cp_parser_template_id (parser,
31491 /*template_keyword_p=*/true,
31492 /*check_dependency_p=*/true,
31493 /*tag_type=*/none_type,
31494 /*is_declaration=*/false);
31495 type = make_typename_type (parser->scope, type, typename_type,
31496 /*complain=*/tf_error);
31498 else
31499 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
31501 if (TREE_CODE (type) == TYPE_DECL)
31502 type = TREE_TYPE (type);
31504 parser->scope = saved_scope;
31505 parser->object_scope = saved_object_scope;
31506 parser->qualifying_scope = saved_qualifying_scope;
31508 if (type == error_mark_node)
31509 cp_parser_skip_to_end_of_statement (parser);
31511 cp_parser_consume_semicolon_at_end_of_statement (parser);
31513 if (type == error_mark_node)
31514 return error_mark_node;
31516 loc = make_location (loc, start_tok->location, parser->lexer);
31517 return finish_type_requirement (loc, type);
31520 /* Parse a compound requirement
31522 compound-requirement:
31523 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
31525 static tree
31526 cp_parser_compound_requirement (cp_parser *parser)
31528 /* Parse an expression enclosed in '{ }'s. */
31529 matching_braces braces;
31530 if (!braces.require_open (parser))
31531 return error_mark_node;
31533 cp_token *expr_token = cp_lexer_peek_token (parser->lexer);
31535 tree expr = cp_parser_expression (parser, NULL, false, false);
31536 if (expr == error_mark_node)
31537 cp_parser_skip_to_closing_brace (parser);
31539 if (!braces.require_close (parser))
31541 cp_parser_skip_to_end_of_statement (parser);
31542 cp_parser_consume_semicolon_at_end_of_statement (parser);
31543 return error_mark_node;
31546 /* If the expression was invalid, skip the remainder of the requirement. */
31547 if (!expr || expr == error_mark_node)
31549 cp_parser_skip_to_end_of_statement (parser);
31550 cp_parser_consume_semicolon_at_end_of_statement (parser);
31551 return error_mark_node;
31554 /* Parse the optional noexcept. */
31555 bool noexcept_p = false;
31556 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
31558 cp_lexer_consume_token (parser->lexer);
31559 noexcept_p = true;
31562 /* Parse the optional trailing return type. */
31563 tree type = NULL_TREE;
31564 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
31566 cp_lexer_consume_token (parser->lexer);
31567 cp_token *tok = cp_lexer_peek_token (parser->lexer);
31569 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
31570 parser->in_result_type_constraint_p = true;
31571 /* C++20 allows either a type-id or a type-constraint. Parsing
31572 a type-id will subsume the parsing for a type-constraint but
31573 allow for more syntactic forms (e.g., const C<T>*). */
31574 type = cp_parser_trailing_type_id (parser);
31575 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
31576 if (type == error_mark_node)
31577 return error_mark_node;
31579 location_t type_loc = make_location (tok->location, tok->location,
31580 parser->lexer);
31582 /* Check that we haven't written something like 'const C<T>*'. */
31583 if (type_uses_auto (type))
31585 if (!is_auto (type))
31587 error_at (type_loc,
31588 "result type is not a plain type-constraint");
31589 cp_parser_consume_semicolon_at_end_of_statement (parser);
31590 return error_mark_node;
31593 else if (!flag_concepts_ts)
31594 /* P1452R2 removed the trailing-return-type option. */
31595 error_at (type_loc,
31596 "return-type-requirement is not a type-constraint");
31599 location_t loc = make_location (expr_token->location,
31600 braces.open_location (),
31601 parser->lexer);
31603 cp_parser_consume_semicolon_at_end_of_statement (parser);
31605 if (expr == error_mark_node || type == error_mark_node)
31606 return error_mark_node;
31608 return finish_compound_requirement (loc, expr, type, noexcept_p);
31611 /* Parse a nested requirement. This is the same as a requires clause.
31613 nested-requirement:
31614 requires-clause */
31616 static tree
31617 cp_parser_nested_requirement (cp_parser *parser)
31619 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
31620 cp_token *tok = cp_lexer_consume_token (parser->lexer);
31621 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31622 tree req = cp_parser_constraint_expression (parser);
31623 if (req == error_mark_node)
31624 cp_parser_skip_to_end_of_statement (parser);
31625 loc = make_location (loc, tok->location, parser->lexer);
31626 cp_parser_consume_semicolon_at_end_of_statement (parser);
31627 if (req == error_mark_node)
31628 return error_mark_node;
31629 return finish_nested_requirement (loc, req);
31632 /* Support Functions */
31634 /* Return the appropriate prefer_type argument for lookup_name based on
31635 tag_type. */
31637 static inline LOOK_want
31638 prefer_type_arg (tag_types tag_type)
31640 switch (tag_type)
31642 case none_type: return LOOK_want::NORMAL; // No preference.
31643 case scope_type: return LOOK_want::TYPE_NAMESPACE; // Type or namespace.
31644 default: return LOOK_want::TYPE; // Type only.
31648 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
31649 NAME should have one of the representations used for an
31650 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
31651 is returned. If PARSER->SCOPE is a dependent type, then a
31652 SCOPE_REF is returned.
31654 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
31655 returned; the name was already resolved when the TEMPLATE_ID_EXPR
31656 was formed. Abstractly, such entities should not be passed to this
31657 function, because they do not need to be looked up, but it is
31658 simpler to check for this special case here, rather than at the
31659 call-sites.
31661 In cases not explicitly covered above, this function returns a
31662 DECL, OVERLOAD, or baselink representing the result of the lookup.
31663 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
31664 is returned.
31666 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
31667 (e.g., "struct") that was used. In that case bindings that do not
31668 refer to types are ignored.
31670 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
31671 ignored. If IS_TEMPLATE IS 2, the 'template' keyword was specified.
31673 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
31674 are ignored.
31676 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
31677 types.
31679 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
31680 TREE_LIST of candidates if name-lookup results in an ambiguity, and
31681 NULL_TREE otherwise. */
31683 static cp_expr
31684 cp_parser_lookup_name (cp_parser *parser, tree name,
31685 enum tag_types tag_type,
31686 int is_template,
31687 bool is_namespace,
31688 bool check_dependency,
31689 tree *ambiguous_decls,
31690 location_t name_location)
31692 tree decl;
31693 tree object_type = parser->context->object_type;
31695 /* Assume that the lookup will be unambiguous. */
31696 if (ambiguous_decls)
31697 *ambiguous_decls = NULL_TREE;
31699 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
31700 no longer valid. Note that if we are parsing tentatively, and
31701 the parse fails, OBJECT_TYPE will be automatically restored. */
31702 parser->context->object_type = NULL_TREE;
31704 if (name == error_mark_node)
31705 return error_mark_node;
31707 /* A template-id has already been resolved; there is no lookup to
31708 do. */
31709 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
31710 return name;
31711 if (BASELINK_P (name))
31713 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
31714 == TEMPLATE_ID_EXPR);
31715 return name;
31718 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
31719 it should already have been checked to make sure that the name
31720 used matches the type being destroyed. */
31721 if (TREE_CODE (name) == BIT_NOT_EXPR)
31723 tree type;
31725 /* Figure out to which type this destructor applies. */
31726 if (parser->scope)
31727 type = parser->scope;
31728 else if (object_type)
31729 type = object_type;
31730 else
31731 type = current_class_type;
31732 /* If that's not a class type, there is no destructor. */
31733 if (!type || !CLASS_TYPE_P (type))
31734 return error_mark_node;
31736 /* In a non-static member function, check implicit this->. */
31737 if (current_class_ref)
31738 return lookup_destructor (current_class_ref, parser->scope, name,
31739 tf_warning_or_error);
31741 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
31742 lazily_declare_fn (sfk_destructor, type);
31744 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
31745 return dtor;
31747 return error_mark_node;
31750 /* By this point, the NAME should be an ordinary identifier. If
31751 the id-expression was a qualified name, the qualifying scope is
31752 stored in PARSER->SCOPE at this point. */
31753 gcc_assert (identifier_p (name));
31755 /* Perform the lookup. */
31756 if (parser->scope)
31758 bool dependent_p;
31760 if (parser->scope == error_mark_node)
31761 return error_mark_node;
31763 /* If the SCOPE is dependent, the lookup must be deferred until
31764 the template is instantiated -- unless we are explicitly
31765 looking up names in uninstantiated templates. Even then, we
31766 cannot look up the name if the scope is not a class type; it
31767 might, for example, be a template type parameter. */
31768 dependent_p = (TYPE_P (parser->scope)
31769 && dependent_scope_p (parser->scope));
31770 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
31771 && dependent_p)
31772 /* Defer lookup. */
31773 decl = error_mark_node;
31774 else
31776 tree pushed_scope = NULL_TREE;
31778 /* If PARSER->SCOPE is a dependent type, then it must be a
31779 class type, and we must not be checking dependencies;
31780 otherwise, we would have processed this lookup above. So
31781 that PARSER->SCOPE is not considered a dependent base by
31782 lookup_member, we must enter the scope here. */
31783 if (dependent_p)
31784 pushed_scope = push_scope (parser->scope);
31786 /* If the PARSER->SCOPE is a template specialization, it
31787 may be instantiated during name lookup. In that case,
31788 errors may be issued. Even if we rollback the current
31789 tentative parse, those errors are valid. */
31790 decl = lookup_qualified_name (parser->scope, name,
31791 prefer_type_arg (tag_type),
31792 /*complain=*/true);
31794 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
31795 lookup result and the nested-name-specifier nominates a class C:
31796 * if the name specified after the nested-name-specifier, when
31797 looked up in C, is the injected-class-name of C (Clause 9), or
31798 * if the name specified after the nested-name-specifier is the
31799 same as the identifier or the simple-template-id's template-
31800 name in the last component of the nested-name-specifier,
31801 the name is instead considered to name the constructor of
31802 class C. [ Note: for example, the constructor is not an
31803 acceptable lookup result in an elaborated-type-specifier so
31804 the constructor would not be used in place of the
31805 injected-class-name. --end note ] Such a constructor name
31806 shall be used only in the declarator-id of a declaration that
31807 names a constructor or in a using-declaration. */
31808 if (tag_type == none_type
31809 && DECL_SELF_REFERENCE_P (decl)
31810 && same_type_p (DECL_CONTEXT (decl), parser->scope))
31811 decl = lookup_qualified_name (parser->scope, ctor_identifier,
31812 prefer_type_arg (tag_type),
31813 /*complain=*/true);
31815 if (pushed_scope)
31816 pop_scope (pushed_scope);
31819 /* If the scope is a dependent type and either we deferred lookup or
31820 we did lookup but didn't find the name, rememeber the name. */
31821 if (decl == error_mark_node && TYPE_P (parser->scope)
31822 && dependent_type_p (parser->scope))
31824 if (tag_type)
31826 tree type;
31828 /* The resolution to Core Issue 180 says that `struct
31829 A::B' should be considered a type-name, even if `A'
31830 is dependent. */
31831 type = make_typename_type (parser->scope, name, tag_type,
31832 /*complain=*/tf_error);
31833 if (type != error_mark_node)
31834 decl = TYPE_NAME (type);
31836 else if (is_template
31837 && (cp_parser_next_token_ends_template_argument_p (parser)
31838 || cp_lexer_next_token_is (parser->lexer,
31839 CPP_CLOSE_PAREN)))
31840 decl = make_unbound_class_template (parser->scope,
31841 name, NULL_TREE,
31842 /*complain=*/tf_error);
31843 else
31844 decl = build_qualified_name (/*type=*/NULL_TREE,
31845 parser->scope, name,
31846 is_template);
31848 parser->qualifying_scope = parser->scope;
31849 parser->object_scope = NULL_TREE;
31851 else if (object_type)
31853 bool dep = dependent_scope_p (object_type);
31855 /* Look up the name in the scope of the OBJECT_TYPE, unless the
31856 OBJECT_TYPE is not a class. */
31857 if (!dep && CLASS_TYPE_P (object_type))
31858 /* If the OBJECT_TYPE is a template specialization, it may
31859 be instantiated during name lookup. In that case, errors
31860 may be issued. Even if we rollback the current tentative
31861 parse, those errors are valid. */
31862 decl = lookup_member (object_type,
31863 name,
31864 /*protect=*/0,
31865 /*prefer_type=*/tag_type != none_type,
31866 tf_warning_or_error);
31867 else
31868 decl = NULL_TREE;
31870 /* If we didn't find a member and have dependent bases, the member lookup
31871 is now dependent. */
31872 if (!dep && !decl && any_dependent_bases_p (object_type))
31873 dep = true;
31875 if (dep && is_template == 2)
31876 /* The template keyword specifies a dependent template. */;
31877 else if (!decl)
31878 /* Look it up in the enclosing context. DR 141: When looking for a
31879 template-name after -> or ., only consider class templates. */
31880 decl = lookup_name (name, is_namespace ? LOOK_want::NAMESPACE
31881 /* DR 141: When looking in the
31882 current enclosing context for a
31883 template-name after -> or ., only
31884 consider class templates. */
31885 : is_template ? LOOK_want::TYPE
31886 : prefer_type_arg (tag_type));
31888 /* If we did unqualified lookup of a dependent member-qualified name and
31889 found something, do we want to use it? P1787 clarified that we need
31890 to look in the object scope first even if it's dependent, but for now
31891 let's still use it in some cases.
31892 FIXME remember unqualified lookup result to use if member lookup fails
31893 at instantiation time. */
31894 if (decl && dep && is_template)
31896 saved_token_sentinel toks (parser->lexer, STS_ROLLBACK);
31897 /* Only use the unqualified class template lookup if we're actually
31898 looking at a template arg list. */
31899 if (!cp_parser_skip_entire_template_parameter_list (parser))
31900 decl = NULL_TREE;
31903 /* If we know we're looking for a type (e.g. A in p->A::x),
31904 mock up a typename. */
31905 if (!decl && dep && tag_type != none_type)
31907 tree type = build_typename_type (object_type, name, name,
31908 typename_type);
31909 decl = TYPE_NAME (type);
31912 parser->object_scope = object_type;
31913 parser->qualifying_scope = NULL_TREE;
31915 else
31917 decl = lookup_name (name, is_namespace ? LOOK_want::NAMESPACE
31918 : prefer_type_arg (tag_type));
31919 parser->qualifying_scope = NULL_TREE;
31920 parser->object_scope = NULL_TREE;
31923 /* If the lookup failed, let our caller know. */
31924 if (!decl || decl == error_mark_node)
31925 return error_mark_node;
31927 /* If we have resolved the name of a member declaration, check to
31928 see if the declaration is accessible. When the name resolves to
31929 set of overloaded functions, accessibility is checked when
31930 overload resolution is done. If we have a TREE_LIST, then the lookup
31931 is either ambiguous or it found multiple injected-class-names, the
31932 accessibility of which is trivially satisfied.
31934 During an explicit instantiation, access is not checked at all,
31935 as per [temp.explicit]. */
31936 if (DECL_P (decl))
31937 check_accessibility_of_qualified_id (decl, object_type, parser->scope,
31938 tf_warning_or_error);
31940 /* Pull out the template from an injected-class-name (or multiple). */
31941 if (is_template)
31942 decl = maybe_get_template_decl_from_type_decl (decl);
31944 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
31945 if (TREE_CODE (decl) == TREE_LIST)
31947 if (ambiguous_decls)
31948 *ambiguous_decls = decl;
31949 /* The error message we have to print is too complicated for
31950 cp_parser_error, so we incorporate its actions directly. */
31951 if (!cp_parser_simulate_error (parser))
31953 error_at (name_location, "reference to %qD is ambiguous",
31954 name);
31955 print_candidates (decl);
31957 return error_mark_node;
31960 gcc_assert (DECL_P (decl)
31961 || TREE_CODE (decl) == OVERLOAD
31962 || TREE_CODE (decl) == SCOPE_REF
31963 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
31964 || BASELINK_P (decl));
31966 maybe_record_typedef_use (decl);
31968 return cp_expr (decl, name_location);
31971 /* Like cp_parser_lookup_name, but for use in the typical case where
31972 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
31973 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
31975 static tree
31976 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
31978 return cp_parser_lookup_name (parser, name,
31979 none_type,
31980 /*is_template=*/false,
31981 /*is_namespace=*/false,
31982 /*check_dependency=*/true,
31983 /*ambiguous_decls=*/NULL,
31984 location);
31987 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
31988 the current context, return the TYPE_DECL. If TAG_NAME_P is
31989 true, the DECL indicates the class being defined in a class-head,
31990 or declared in an elaborated-type-specifier.
31992 Otherwise, return DECL. */
31994 static tree
31995 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
31997 /* If the TEMPLATE_DECL is being declared as part of a class-head,
31998 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
32000 struct A {
32001 template <typename T> struct B;
32004 template <typename T> struct A::B {};
32006 Similarly, in an elaborated-type-specifier:
32008 namespace N { struct X{}; }
32010 struct A {
32011 template <typename T> friend struct N::X;
32014 However, if the DECL refers to a class type, and we are in
32015 the scope of the class, then the name lookup automatically
32016 finds the TYPE_DECL created by build_self_reference rather
32017 than a TEMPLATE_DECL. For example, in:
32019 template <class T> struct S {
32020 S s;
32023 there is no need to handle such case. */
32025 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
32026 return DECL_TEMPLATE_RESULT (decl);
32028 return decl;
32031 /* If too many, or too few, template-parameter lists apply to the
32032 declarator, issue an error message. Returns TRUE if all went well,
32033 and FALSE otherwise. */
32035 static bool
32036 cp_parser_check_declarator_template_parameters (cp_parser* parser,
32037 cp_declarator *declarator,
32038 location_t declarator_location)
32040 switch (declarator->kind)
32042 case cdk_id:
32044 unsigned num_templates = 0;
32045 tree scope = declarator->u.id.qualifying_scope;
32046 bool template_id_p = false;
32048 if (scope)
32049 num_templates = num_template_headers_for_class (scope);
32050 else if (TREE_CODE (declarator->u.id.unqualified_name)
32051 == TEMPLATE_ID_EXPR)
32053 /* If the DECLARATOR has the form `X<y>' then it uses one
32054 additional level of template parameters. */
32055 ++num_templates;
32056 template_id_p = true;
32059 return cp_parser_check_template_parameters
32060 (parser, num_templates, template_id_p, declarator_location,
32061 declarator);
32064 case cdk_function:
32065 case cdk_array:
32066 case cdk_pointer:
32067 case cdk_reference:
32068 case cdk_ptrmem:
32069 return (cp_parser_check_declarator_template_parameters
32070 (parser, declarator->declarator, declarator_location));
32072 case cdk_decomp:
32073 case cdk_error:
32074 return true;
32076 default:
32077 gcc_unreachable ();
32079 return false;
32082 /* NUM_TEMPLATES were used in the current declaration. If that is
32083 invalid, return FALSE and issue an error messages. Otherwise,
32084 return TRUE. If DECLARATOR is non-NULL, then we are checking a
32085 declarator and we can print more accurate diagnostics. */
32087 static bool
32088 cp_parser_check_template_parameters (cp_parser* parser,
32089 unsigned num_templates,
32090 bool template_id_p,
32091 location_t location,
32092 cp_declarator *declarator)
32094 /* If there are the same number of template classes and parameter
32095 lists, that's OK. */
32096 if (parser->num_template_parameter_lists == num_templates)
32097 return true;
32098 /* If there are more, but only one more, and the name ends in an identifier,
32099 then we are declaring a primary template. That's OK too. */
32100 if (!template_id_p
32101 && parser->num_template_parameter_lists == num_templates + 1)
32102 return true;
32104 if (cp_parser_simulate_error (parser))
32105 return false;
32107 /* If there are more template classes than parameter lists, we have
32108 something like:
32110 template <class T> void S<T>::R<T>::f (); */
32111 if (parser->num_template_parameter_lists < num_templates)
32113 if (declarator && !current_function_decl)
32114 error_at (location, "specializing member %<%T::%E%> "
32115 "requires %<template<>%> syntax",
32116 declarator->u.id.qualifying_scope,
32117 declarator->u.id.unqualified_name);
32118 else if (declarator)
32119 error_at (location, "invalid declaration of %<%T::%E%>",
32120 declarator->u.id.qualifying_scope,
32121 declarator->u.id.unqualified_name);
32122 else
32123 error_at (location, "too few template-parameter-lists");
32124 return false;
32126 /* Otherwise, there are too many template parameter lists. We have
32127 something like:
32129 template <class T> template <class U> void S::f(); */
32130 error_at (location, "too many template-parameter-lists");
32131 return false;
32134 /* Parse an optional `::' token indicating that the following name is
32135 from the global namespace. If so, PARSER->SCOPE is set to the
32136 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
32137 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
32138 Returns the new value of PARSER->SCOPE, if the `::' token is
32139 present, and NULL_TREE otherwise. */
32141 static tree
32142 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
32144 cp_token *token;
32146 /* Peek at the next token. */
32147 token = cp_lexer_peek_token (parser->lexer);
32148 /* If we're looking at a `::' token then we're starting from the
32149 global namespace, not our current location. */
32150 if (token->type == CPP_SCOPE)
32152 /* Consume the `::' token. */
32153 cp_lexer_consume_token (parser->lexer);
32154 /* Set the SCOPE so that we know where to start the lookup. */
32155 parser->scope = global_namespace;
32156 parser->qualifying_scope = global_namespace;
32157 parser->object_scope = NULL_TREE;
32159 return parser->scope;
32161 else if (!current_scope_valid_p)
32163 parser->scope = NULL_TREE;
32164 parser->qualifying_scope = NULL_TREE;
32165 parser->object_scope = NULL_TREE;
32168 return NULL_TREE;
32171 /* Returns TRUE if the upcoming token sequence is the start of a
32172 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
32173 declarator is preceded by the `friend' specifier. The parser flags FLAGS
32174 is used to control type-specifier parsing. */
32176 static bool
32177 cp_parser_constructor_declarator_p (cp_parser *parser, cp_parser_flags flags,
32178 bool friend_p)
32180 bool constructor_p;
32181 bool outside_class_specifier_p;
32182 tree nested_name_specifier;
32183 cp_token *next_token;
32185 /* The common case is that this is not a constructor declarator, so
32186 try to avoid doing lots of work if at all possible. It's not
32187 valid declare a constructor at function scope. */
32188 if (parser->in_function_body)
32189 return false;
32190 /* And only certain tokens can begin a constructor declarator. */
32191 next_token = cp_lexer_peek_token (parser->lexer);
32192 if (next_token->type != CPP_NAME
32193 && next_token->type != CPP_SCOPE
32194 && next_token->type != CPP_NESTED_NAME_SPECIFIER
32195 /* DR 2237 (C++20 only): A simple-template-id is no longer valid as the
32196 declarator-id of a constructor or destructor. */
32197 && (next_token->type != CPP_TEMPLATE_ID || cxx_dialect >= cxx20))
32198 return false;
32200 /* Parse tentatively; we are going to roll back all of the tokens
32201 consumed here. */
32202 cp_parser_parse_tentatively (parser);
32203 /* Assume that we are looking at a constructor declarator. */
32204 constructor_p = true;
32206 /* Look for the optional `::' operator. */
32207 cp_parser_global_scope_opt (parser,
32208 /*current_scope_valid_p=*/false);
32209 /* Look for the nested-name-specifier. */
32210 nested_name_specifier
32211 = (cp_parser_nested_name_specifier_opt (parser,
32212 /*typename_keyword_p=*/false,
32213 /*check_dependency_p=*/false,
32214 /*type_p=*/false,
32215 /*is_declaration=*/false));
32217 /* Resolve the TYPENAME_TYPE, because the call above didn't do it. */
32218 if (nested_name_specifier
32219 && TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
32221 tree s = resolve_typename_type (nested_name_specifier,
32222 /*only_current_p=*/false);
32223 if (TREE_CODE (s) != TYPENAME_TYPE)
32224 nested_name_specifier = s;
32227 outside_class_specifier_p = (!at_class_scope_p ()
32228 || !TYPE_BEING_DEFINED (current_class_type)
32229 || friend_p);
32231 /* Outside of a class-specifier, there must be a
32232 nested-name-specifier. Except in C++17 mode, where we
32233 might be declaring a guiding declaration. */
32234 if (!nested_name_specifier && outside_class_specifier_p
32235 && cxx_dialect < cxx17)
32236 constructor_p = false;
32237 else if (nested_name_specifier == error_mark_node)
32238 constructor_p = false;
32240 /* If we have a class scope, this is easy; DR 147 says that S::S always
32241 names the constructor, and no other qualified name could. */
32242 if (constructor_p && nested_name_specifier
32243 && CLASS_TYPE_P (nested_name_specifier))
32245 tree id = cp_parser_unqualified_id (parser,
32246 /*template_keyword_p=*/false,
32247 /*check_dependency_p=*/false,
32248 /*declarator_p=*/true,
32249 /*optional_p=*/false);
32250 if (is_overloaded_fn (id))
32251 id = DECL_NAME (get_first_fn (id));
32252 if (!constructor_name_p (id, nested_name_specifier))
32253 constructor_p = false;
32255 /* If we still think that this might be a constructor-declarator,
32256 look for a class-name. */
32257 else if (constructor_p)
32259 /* If we have:
32261 template <typename T> struct S {
32262 S();
32265 we must recognize that the nested `S' names a class. */
32266 if (cxx_dialect >= cxx17)
32267 cp_parser_parse_tentatively (parser);
32269 tree type_decl;
32270 type_decl = cp_parser_class_name (parser,
32271 /*typename_keyword_p=*/false,
32272 /*template_keyword_p=*/false,
32273 none_type,
32274 /*check_dependency_p=*/false,
32275 /*class_head_p=*/false,
32276 /*is_declaration=*/false);
32278 if (cxx_dialect >= cxx17
32279 && !cp_parser_parse_definitely (parser))
32281 type_decl = NULL_TREE;
32282 tree tmpl = cp_parser_template_name (parser,
32283 /*template_keyword*/false,
32284 /*check_dependency_p*/false,
32285 /*is_declaration*/false,
32286 none_type,
32287 /*is_identifier*/NULL);
32288 if (DECL_CLASS_TEMPLATE_P (tmpl)
32289 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
32290 /* It's a deduction guide, return true. */;
32291 else
32292 cp_parser_simulate_error (parser);
32295 /* If there was no class-name, then this is not a constructor.
32296 Otherwise, if we are in a class-specifier and we aren't
32297 handling a friend declaration, check that its type matches
32298 current_class_type (c++/38313). Note: error_mark_node
32299 is left alone for error recovery purposes. */
32300 constructor_p = (!cp_parser_error_occurred (parser)
32301 && (outside_class_specifier_p
32302 || type_decl == NULL_TREE
32303 || type_decl == error_mark_node
32304 || same_type_p (current_class_type,
32305 TREE_TYPE (type_decl))));
32307 /* If we're still considering a constructor, we have to see a `(',
32308 to begin the parameter-declaration-clause, followed by either a
32309 `)', an `...', or a decl-specifier. We need to check for a
32310 type-specifier to avoid being fooled into thinking that:
32312 S (f) (int);
32314 is a constructor. (It is actually a function named `f' that
32315 takes one parameter (of type `int') and returns a value of type
32316 `S'. */
32317 if (constructor_p
32318 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32319 constructor_p = false;
32321 if (constructor_p
32322 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
32323 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
32324 /* A parameter declaration begins with a decl-specifier,
32325 which is either the "attribute" keyword, a storage class
32326 specifier, or (usually) a type-specifier. */
32327 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer)
32328 /* GNU attributes can actually appear both at the start of
32329 a parameter and parenthesized declarator.
32330 S (__attribute__((unused)) int);
32331 is a constructor, but
32332 S (__attribute__((unused)) foo) (int);
32333 is a function declaration. [[attribute]] can appear in the
32334 first form too, but not in the second form. */
32335 && !cp_next_tokens_can_be_std_attribute_p (parser))
32337 tree type;
32338 tree pushed_scope = NULL_TREE;
32339 unsigned saved_num_template_parameter_lists;
32341 if (cp_parser_allow_gnu_extensions_p (parser)
32342 && cp_next_tokens_can_be_gnu_attribute_p (parser))
32344 unsigned int n = cp_parser_skip_gnu_attributes_opt (parser, 1);
32345 while (--n)
32346 cp_lexer_consume_token (parser->lexer);
32349 /* Names appearing in the type-specifier should be looked up
32350 in the scope of the class. */
32351 if (current_class_type)
32352 type = NULL_TREE;
32353 else if (type_decl)
32355 type = TREE_TYPE (type_decl);
32356 if (TREE_CODE (type) == TYPENAME_TYPE)
32358 type = resolve_typename_type (type,
32359 /*only_current_p=*/false);
32360 if (TREE_CODE (type) == TYPENAME_TYPE)
32362 cp_parser_abort_tentative_parse (parser);
32363 return false;
32366 pushed_scope = push_scope (type);
32369 /* Inside the constructor parameter list, surrounding
32370 template-parameter-lists do not apply. */
32371 saved_num_template_parameter_lists
32372 = parser->num_template_parameter_lists;
32373 parser->num_template_parameter_lists = 0;
32375 /* Look for the type-specifier. It's not optional, but its typename
32376 might be. Unless this is a friend declaration; we don't want to
32377 treat
32379 friend S (T::fn)(int);
32381 as a constructor, but with P0634, we might assume a type when
32382 looking for the type-specifier. It is actually a function named
32383 `T::fn' that takes one parameter (of type `int') and returns a
32384 value of type `S'. Constructors can be friends, but they must
32385 use a qualified name.
32387 Parse with an empty set of declaration specifiers since we're
32388 trying to match a decl-specifier-seq of the first parameter.
32389 This must be non-null so that cp_parser_simple_type_specifier
32390 will recognize a constrained placeholder type such as:
32391 'C<int> auto' where C is a type concept. */
32392 cp_decl_specifier_seq ctor_specs;
32393 clear_decl_specs (&ctor_specs);
32394 cp_parser_type_specifier (parser,
32395 (friend_p ? CP_PARSER_FLAGS_NONE
32396 : (flags & ~CP_PARSER_FLAGS_OPTIONAL)),
32397 /*decl_specs=*/&ctor_specs,
32398 /*is_declarator=*/true,
32399 /*declares_class_or_enum=*/NULL,
32400 /*is_cv_qualifier=*/NULL);
32402 parser->num_template_parameter_lists
32403 = saved_num_template_parameter_lists;
32405 /* Leave the scope of the class. */
32406 if (pushed_scope)
32407 pop_scope (pushed_scope);
32409 constructor_p = !cp_parser_error_occurred (parser);
32413 /* We did not really want to consume any tokens. */
32414 cp_parser_abort_tentative_parse (parser);
32416 return constructor_p;
32419 /* Parse the definition of the function given by the DECL_SPECIFIERS,
32420 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
32421 they must be performed once we are in the scope of the function.
32423 Returns the function defined. */
32425 static tree
32426 cp_parser_function_definition_from_specifiers_and_declarator
32427 (cp_parser* parser,
32428 cp_decl_specifier_seq *decl_specifiers,
32429 tree attributes,
32430 const cp_declarator *declarator)
32432 tree fn;
32433 bool success_p;
32435 /* Begin the function-definition. */
32436 success_p = start_function (decl_specifiers, declarator, attributes);
32438 /* The things we're about to see are not directly qualified by any
32439 template headers we've seen thus far. */
32440 reset_specialization ();
32442 /* If there were names looked up in the decl-specifier-seq that we
32443 did not check, check them now. We must wait until we are in the
32444 scope of the function to perform the checks, since the function
32445 might be a friend. */
32446 perform_deferred_access_checks (tf_warning_or_error);
32448 if (success_p)
32450 cp_finalize_omp_declare_simd (parser, current_function_decl);
32451 parser->omp_declare_simd = NULL;
32452 cp_finalize_oacc_routine (parser, current_function_decl, true);
32453 parser->oacc_routine = NULL;
32456 if (!success_p)
32458 /* Skip the entire function. */
32459 cp_parser_skip_to_end_of_block_or_statement (parser);
32460 fn = error_mark_node;
32462 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
32464 /* Seen already, skip it. An error message has already been output. */
32465 cp_parser_skip_to_end_of_block_or_statement (parser);
32466 fn = current_function_decl;
32467 current_function_decl = NULL_TREE;
32468 /* If this is a function from a class, pop the nested class. */
32469 if (current_class_name)
32470 pop_nested_class ();
32472 else
32474 auto_timevar tv (DECL_DECLARED_INLINE_P (current_function_decl)
32475 ? TV_PARSE_INLINE : TV_PARSE_FUNC);
32476 fn = cp_parser_function_definition_after_declarator (parser,
32477 /*inline_p=*/false);
32480 return fn;
32483 /* Parse the part of a function-definition that follows the
32484 declarator. INLINE_P is TRUE iff this function is an inline
32485 function defined within a class-specifier.
32487 Returns the function defined. */
32489 static tree
32490 cp_parser_function_definition_after_declarator (cp_parser* parser,
32491 bool inline_p)
32493 tree fn;
32494 bool saved_in_unbraced_linkage_specification_p;
32495 bool saved_in_function_body;
32496 unsigned saved_num_template_parameter_lists;
32497 cp_token *token;
32498 bool fully_implicit_function_template_p
32499 = parser->fully_implicit_function_template_p;
32500 parser->fully_implicit_function_template_p = false;
32501 tree implicit_template_parms
32502 = parser->implicit_template_parms;
32503 parser->implicit_template_parms = 0;
32504 cp_binding_level* implicit_template_scope
32505 = parser->implicit_template_scope;
32506 parser->implicit_template_scope = 0;
32508 saved_in_function_body = parser->in_function_body;
32509 parser->in_function_body = true;
32510 /* If the next token is `return', then the code may be trying to
32511 make use of the "named return value" extension that G++ used to
32512 support. */
32513 token = cp_lexer_peek_token (parser->lexer);
32514 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
32516 /* Consume the `return' keyword. */
32517 cp_lexer_consume_token (parser->lexer);
32518 /* Look for the identifier that indicates what value is to be
32519 returned. */
32520 cp_parser_identifier (parser);
32521 /* Issue an error message. */
32522 error_at (token->location,
32523 "named return values are no longer supported");
32524 /* Skip tokens until we reach the start of the function body. */
32525 while (true)
32527 cp_token *token = cp_lexer_peek_token (parser->lexer);
32528 if (token->type == CPP_OPEN_BRACE
32529 || token->type == CPP_EOF
32530 || token->type == CPP_PRAGMA_EOL)
32531 break;
32532 cp_lexer_consume_token (parser->lexer);
32535 /* The `extern' in `extern "C" void f () { ... }' does not apply to
32536 anything declared inside `f'. */
32537 saved_in_unbraced_linkage_specification_p
32538 = parser->in_unbraced_linkage_specification_p;
32539 parser->in_unbraced_linkage_specification_p = false;
32540 /* Inside the function, surrounding template-parameter-lists do not
32541 apply. */
32542 saved_num_template_parameter_lists
32543 = parser->num_template_parameter_lists;
32544 parser->num_template_parameter_lists = 0;
32546 /* If the next token is `try', `__transaction_atomic', or
32547 `__transaction_relaxed`, then we are looking at either function-try-block
32548 or function-transaction-block. Note that all of these include the
32549 function-body. */
32550 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
32551 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
32552 else if (cp_lexer_next_token_is_keyword (parser->lexer,
32553 RID_TRANSACTION_RELAXED))
32554 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
32555 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
32556 cp_parser_function_try_block (parser);
32557 else
32558 cp_parser_ctor_initializer_opt_and_function_body
32559 (parser, /*in_function_try_block=*/false);
32561 /* Finish the function. */
32562 fn = finish_function (inline_p);
32564 if (modules_p ()
32565 && !inline_p
32566 && TYPE_P (DECL_CONTEXT (fn))
32567 && (DECL_DECLARED_INLINE_P (fn)
32568 || processing_template_decl))
32569 set_defining_module (fn);
32571 /* Generate code for it, if necessary. */
32572 expand_or_defer_fn (fn);
32574 /* Restore the saved values. */
32575 parser->in_unbraced_linkage_specification_p
32576 = saved_in_unbraced_linkage_specification_p;
32577 parser->num_template_parameter_lists
32578 = saved_num_template_parameter_lists;
32579 parser->in_function_body = saved_in_function_body;
32581 parser->fully_implicit_function_template_p
32582 = fully_implicit_function_template_p;
32583 parser->implicit_template_parms
32584 = implicit_template_parms;
32585 parser->implicit_template_scope
32586 = implicit_template_scope;
32588 if (parser->fully_implicit_function_template_p)
32589 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
32591 return fn;
32594 /* Parse a template-declaration body (following argument list). */
32596 static void
32597 cp_parser_template_declaration_after_parameters (cp_parser* parser,
32598 tree parameter_list,
32599 bool member_p)
32601 tree decl = NULL_TREE;
32602 bool friend_p = false;
32604 /* We just processed one more parameter list. */
32605 ++parser->num_template_parameter_lists;
32607 /* Get the deferred access checks from the parameter list. These
32608 will be checked once we know what is being declared, as for a
32609 member template the checks must be performed in the scope of the
32610 class containing the member. */
32611 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
32613 /* Tentatively parse for a new template parameter list, which can either be
32614 the template keyword or a template introduction. */
32615 if (cp_parser_template_declaration_after_export (parser, member_p))
32616 /* OK */;
32617 else if (cxx_dialect >= cxx11
32618 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
32619 decl = cp_parser_alias_declaration (parser);
32620 else if (flag_concepts
32621 && cp_lexer_next_token_is_keyword (parser->lexer, RID_CONCEPT)
32622 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
32623 /* -fconcept-ts 'concept bool' syntax is handled below, in
32624 cp_parser_single_declaration. */
32625 decl = cp_parser_concept_definition (parser);
32626 else
32628 cp_token *token = cp_lexer_peek_token (parser->lexer);
32629 decl = cp_parser_single_declaration (parser,
32630 checks,
32631 member_p,
32632 /*explicit_specialization_p=*/false,
32633 &friend_p);
32635 /* If this is a member template declaration, let the front
32636 end know. */
32637 if (member_p && !friend_p && decl)
32639 if (TREE_CODE (decl) == TYPE_DECL)
32640 cp_parser_check_access_in_redeclaration (decl, token->location);
32642 decl = finish_member_template_decl (decl);
32644 else if (friend_p && decl
32645 && DECL_DECLARES_TYPE_P (decl))
32646 make_friend_class (current_class_type, TREE_TYPE (decl),
32647 /*complain=*/true);
32649 /* We are done with the current parameter list. */
32650 --parser->num_template_parameter_lists;
32652 pop_deferring_access_checks ();
32654 /* Finish up. */
32655 finish_template_decl (parameter_list);
32657 /* Check the template arguments for a literal operator template. */
32658 if (decl
32659 && DECL_DECLARES_FUNCTION_P (decl)
32660 && UDLIT_OPER_P (DECL_NAME (decl)))
32662 bool ok = true;
32663 if (parameter_list == NULL_TREE)
32664 ok = false;
32665 else
32667 int num_parms = TREE_VEC_LENGTH (parameter_list);
32668 if (num_parms == 1)
32670 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
32671 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
32672 if (TREE_CODE (parm) != PARM_DECL)
32673 ok = false;
32674 else if (MAYBE_CLASS_TYPE_P (TREE_TYPE (parm))
32675 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
32676 /* OK, C++20 string literal operator template. We don't need
32677 to warn in lower dialects here because we will have already
32678 warned about the template parameter. */;
32679 else if (TREE_TYPE (parm) != char_type_node
32680 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
32681 ok = false;
32683 else if (num_parms == 2 && cxx_dialect >= cxx14)
32685 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
32686 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
32687 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
32688 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
32689 if (TREE_CODE (parm) != PARM_DECL
32690 || TREE_TYPE (parm) != TREE_TYPE (type)
32691 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
32692 ok = false;
32693 else
32694 /* http://cplusplus.github.io/EWG/ewg-active.html#66 */
32695 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
32696 "ISO C++ did not adopt string literal operator templa"
32697 "tes taking an argument pack of characters");
32699 else
32700 ok = false;
32702 if (!ok)
32704 if (cxx_dialect > cxx17)
32705 error_at (DECL_SOURCE_LOCATION (decl), "literal operator "
32706 "template %qD has invalid parameter list; expected "
32707 "non-type template parameter pack %<<char...>%> or "
32708 "single non-type parameter of class type",
32709 decl);
32710 else
32711 error_at (DECL_SOURCE_LOCATION (decl), "literal operator "
32712 "template %qD has invalid parameter list; expected "
32713 "non-type template parameter pack %<<char...>%>",
32714 decl);
32718 /* Register member declarations. */
32719 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
32720 finish_member_declaration (decl);
32721 /* If DECL is a function template, we must return to parse it later.
32722 (Even though there is no definition, there might be default
32723 arguments that need handling.) */
32724 if (member_p && decl
32725 && DECL_DECLARES_FUNCTION_P (decl))
32726 vec_safe_push (unparsed_funs_with_definitions, decl);
32729 /* Parse a template introduction header for a template-declaration. Returns
32730 false if tentative parse fails. */
32732 static bool
32733 cp_parser_template_introduction (cp_parser* parser, bool member_p)
32735 cp_parser_parse_tentatively (parser);
32737 tree saved_scope = parser->scope;
32738 tree saved_object_scope = parser->object_scope;
32739 tree saved_qualifying_scope = parser->qualifying_scope;
32740 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32742 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
32744 /* In classes don't parse valid unnamed bitfields as invalid
32745 template introductions. */
32746 if (member_p)
32747 parser->colon_corrects_to_scope_p = false;
32749 /* Look for the optional `::' operator. */
32750 cp_parser_global_scope_opt (parser,
32751 /*current_scope_valid_p=*/false);
32752 /* Look for the nested-name-specifier. */
32753 cp_parser_nested_name_specifier_opt (parser,
32754 /*typename_keyword_p=*/false,
32755 /*check_dependency_p=*/true,
32756 /*type_p=*/false,
32757 /*is_declaration=*/false);
32759 cp_token *token = cp_lexer_peek_token (parser->lexer);
32760 tree concept_name = cp_parser_identifier (parser);
32762 /* Look up the concept for which we will be matching
32763 template parameters. */
32764 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
32765 token->location);
32766 parser->scope = saved_scope;
32767 parser->object_scope = saved_object_scope;
32768 parser->qualifying_scope = saved_qualifying_scope;
32769 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32771 if (concept_name == error_mark_node
32772 || (seen_error () && !concept_definition_p (tmpl_decl)))
32773 cp_parser_simulate_error (parser);
32775 /* Look for opening brace for introduction. */
32776 matching_braces braces;
32777 braces.require_open (parser);
32778 location_t open_loc = input_location;
32780 if (!cp_parser_parse_definitely (parser))
32781 return false;
32783 push_deferring_access_checks (dk_deferred);
32785 /* Build vector of placeholder parameters and grab
32786 matching identifiers. */
32787 tree introduction_list = cp_parser_introduction_list (parser);
32789 /* Look for closing brace for introduction. */
32790 if (!braces.require_close (parser))
32791 return true;
32793 /* The introduction-list shall not be empty. */
32794 int nargs = TREE_VEC_LENGTH (introduction_list);
32795 if (nargs == 0)
32797 /* In cp_parser_introduction_list we have already issued an error. */
32798 return true;
32801 if (tmpl_decl == error_mark_node)
32803 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
32804 token->location);
32805 return true;
32808 /* Build and associate the constraint. */
32809 location_t introduction_loc = make_location (open_loc,
32810 start_token->location,
32811 parser->lexer);
32812 tree parms = finish_template_introduction (tmpl_decl,
32813 introduction_list,
32814 introduction_loc);
32815 if (parms && parms != error_mark_node)
32817 if (!flag_concepts_ts)
32818 pedwarn (introduction_loc, 0, "template-introductions"
32819 " are not part of C++20 concepts; use %qs to enable",
32820 "-fconcepts-ts");
32822 cp_parser_template_declaration_after_parameters (parser, parms,
32823 member_p);
32824 return true;
32827 if (parms == NULL_TREE)
32828 error_at (token->location, "no matching concept for template-introduction");
32830 return true;
32833 /* Parse a normal template-declaration following the template keyword. */
32835 static void
32836 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
32838 tree parameter_list;
32839 bool need_lang_pop;
32840 location_t location = input_location;
32842 /* Look for the `<' token. */
32843 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
32844 return;
32845 if (at_class_scope_p () && current_function_decl)
32847 /* 14.5.2.2 [temp.mem]
32849 A local class shall not have member templates. */
32850 error_at (location,
32851 "invalid declaration of member template in local class");
32852 cp_parser_skip_to_end_of_block_or_statement (parser);
32853 return;
32855 /* [temp]
32857 A template ... shall not have C linkage. */
32858 if (current_lang_name == lang_name_c)
32860 error_at (location, "template with C linkage");
32861 maybe_show_extern_c_location ();
32862 /* Give it C++ linkage to avoid confusing other parts of the
32863 front end. */
32864 push_lang_context (lang_name_cplusplus);
32865 need_lang_pop = true;
32867 else
32868 need_lang_pop = false;
32870 /* We cannot perform access checks on the template parameter
32871 declarations until we know what is being declared, just as we
32872 cannot check the decl-specifier list. */
32873 push_deferring_access_checks (dk_deferred);
32875 /* If the next token is `>', then we have an invalid
32876 specialization. Rather than complain about an invalid template
32877 parameter, issue an error message here. */
32878 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
32880 cp_parser_error (parser, "invalid explicit specialization");
32881 begin_specialization ();
32882 parameter_list = NULL_TREE;
32884 else
32886 /* Parse the template parameters. */
32887 parameter_list = cp_parser_template_parameter_list (parser);
32890 /* Look for the `>'. */
32891 cp_parser_require_end_of_template_parameter_list (parser);
32893 /* Manage template requirements */
32894 if (flag_concepts)
32896 tree reqs = get_shorthand_constraints (current_template_parms);
32897 if (tree treqs = cp_parser_requires_clause_opt (parser, false))
32898 reqs = combine_constraint_expressions (reqs, treqs);
32899 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
32902 cp_parser_template_declaration_after_parameters (parser, parameter_list,
32903 member_p);
32905 /* For the erroneous case of a template with C linkage, we pushed an
32906 implicit C++ linkage scope; exit that scope now. */
32907 if (need_lang_pop)
32908 pop_lang_context ();
32911 /* Parse a template-declaration, assuming that the `export' (and
32912 `extern') keywords, if present, has already been scanned. MEMBER_P
32913 is as for cp_parser_template_declaration. */
32915 static bool
32916 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
32918 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
32920 cp_lexer_consume_token (parser->lexer);
32921 cp_parser_explicit_template_declaration (parser, member_p);
32922 return true;
32924 else if (flag_concepts)
32925 return cp_parser_template_introduction (parser, member_p);
32927 return false;
32930 /* Perform the deferred access checks from a template-parameter-list.
32931 CHECKS is a TREE_LIST of access checks, as returned by
32932 get_deferred_access_checks. */
32934 static void
32935 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
32937 ++processing_template_parmlist;
32938 perform_access_checks (checks, tf_warning_or_error);
32939 --processing_template_parmlist;
32942 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
32943 `function-definition' sequence that follows a template header.
32944 If MEMBER_P is true, this declaration appears in a class scope.
32946 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
32947 *FRIEND_P is set to TRUE iff the declaration is a friend. */
32949 static tree
32950 cp_parser_single_declaration (cp_parser* parser,
32951 vec<deferred_access_check, va_gc> *checks,
32952 bool member_p,
32953 bool explicit_specialization_p,
32954 bool* friend_p)
32956 int declares_class_or_enum;
32957 tree decl = NULL_TREE;
32958 cp_decl_specifier_seq decl_specifiers;
32959 bool function_definition_p = false;
32960 cp_token *decl_spec_token_start;
32962 /* This function is only used when processing a template
32963 declaration. */
32964 gcc_assert (innermost_scope_kind () == sk_template_parms
32965 || innermost_scope_kind () == sk_template_spec);
32967 /* Defer access checks until we know what is being declared. */
32968 push_deferring_access_checks (dk_deferred);
32970 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
32971 alternative. */
32972 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
32973 cp_parser_decl_specifier_seq (parser,
32974 (CP_PARSER_FLAGS_OPTIONAL
32975 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
32976 &decl_specifiers,
32977 &declares_class_or_enum);
32979 cp_omp_declare_simd_data odsd;
32980 if (decl_specifiers.attributes && (flag_openmp || flag_openmp_simd))
32981 cp_parser_handle_directive_omp_attributes (parser,
32982 &decl_specifiers.attributes,
32983 &odsd, true);
32985 if (friend_p)
32986 *friend_p = cp_parser_friend_p (&decl_specifiers);
32988 /* There are no template typedefs. */
32989 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
32991 error_at (decl_spec_token_start->location,
32992 "template declaration of %<typedef%>");
32993 decl = error_mark_node;
32996 /* Gather up the access checks that occurred the
32997 decl-specifier-seq. */
32998 stop_deferring_access_checks ();
33000 /* Check for the declaration of a template class. */
33001 if (declares_class_or_enum)
33003 if (cp_parser_declares_only_class_p (parser)
33004 || (declares_class_or_enum & 2))
33006 decl = shadow_tag (&decl_specifiers);
33008 /* In this case:
33010 struct C {
33011 friend template <typename T> struct A<T>::B;
33014 A<T>::B will be represented by a TYPENAME_TYPE, and
33015 therefore not recognized by shadow_tag. */
33016 if (friend_p && *friend_p
33017 && !decl
33018 && decl_specifiers.type
33019 && TYPE_P (decl_specifiers.type))
33020 decl = decl_specifiers.type;
33022 if (decl && decl != error_mark_node)
33023 decl = TYPE_NAME (decl);
33024 else
33025 decl = error_mark_node;
33027 /* If this is a declaration, but not a definition, associate
33028 any constraints with the type declaration. Constraints
33029 are associated with definitions in cp_parser_class_specifier. */
33030 if (declares_class_or_enum == 1)
33031 associate_classtype_constraints (TREE_TYPE (decl));
33033 /* Perform access checks for template parameters. */
33034 cp_parser_perform_template_parameter_access_checks (checks);
33036 /* Give a helpful diagnostic for
33037 template <class T> struct A { } a;
33038 if we aren't already recovering from an error. */
33039 if (!cp_parser_declares_only_class_p (parser)
33040 && !seen_error ())
33042 error_at (cp_lexer_peek_token (parser->lexer)->location,
33043 "a class template declaration must not declare "
33044 "anything else");
33045 cp_parser_skip_to_end_of_block_or_statement (parser);
33046 goto out;
33051 /* Complain about missing 'typename' or other invalid type names. */
33052 if (!decl_specifiers.any_type_specifiers_p
33053 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
33055 /* cp_parser_parse_and_diagnose_invalid_type_name calls
33056 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
33057 the rest of this declaration. */
33058 decl = error_mark_node;
33059 goto out;
33062 /* If it's not a template class, try for a template function. If
33063 the next token is a `;', then this declaration does not declare
33064 anything. But, if there were errors in the decl-specifiers, then
33065 the error might well have come from an attempted class-specifier.
33066 In that case, there's no need to warn about a missing declarator. */
33067 if (!decl
33068 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
33069 || decl_specifiers.type != error_mark_node))
33071 int flags = CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
33072 /* We don't delay parsing for friends, though CWG 2510 may change
33073 that. */
33074 if (member_p && !(friend_p && *friend_p))
33075 flags |= CP_PARSER_FLAGS_DELAY_NOEXCEPT;
33076 decl = cp_parser_init_declarator (parser,
33077 flags,
33078 &decl_specifiers,
33079 checks,
33080 /*function_definition_allowed_p=*/true,
33081 member_p,
33082 declares_class_or_enum,
33083 &function_definition_p,
33084 NULL, NULL, NULL);
33086 /* 7.1.1-1 [dcl.stc]
33088 A storage-class-specifier shall not be specified in an explicit
33089 specialization... */
33090 if (decl
33091 && explicit_specialization_p
33092 && decl_specifiers.storage_class != sc_none)
33094 error_at (decl_spec_token_start->location,
33095 "explicit template specialization cannot have a storage class");
33096 decl = error_mark_node;
33099 if (decl && VAR_P (decl))
33100 check_template_variable (decl);
33103 /* Look for a trailing `;' after the declaration. */
33104 if (!function_definition_p
33105 && (decl == error_mark_node
33106 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
33107 cp_parser_skip_to_end_of_block_or_statement (parser);
33109 out:
33110 pop_deferring_access_checks ();
33112 /* Clear any current qualification; whatever comes next is the start
33113 of something new. */
33114 parser->scope = NULL_TREE;
33115 parser->qualifying_scope = NULL_TREE;
33116 parser->object_scope = NULL_TREE;
33118 cp_finalize_omp_declare_simd (parser, &odsd);
33120 return decl;
33123 /* Parse a cast-expression that is not the operand of a unary "&". */
33125 static cp_expr
33126 cp_parser_simple_cast_expression (cp_parser *parser)
33128 return cp_parser_cast_expression (parser, /*address_p=*/false,
33129 /*cast_p=*/false, /*decltype*/false, NULL);
33132 /* Parse a functional cast to TYPE. Returns an expression
33133 representing the cast. */
33135 static cp_expr
33136 cp_parser_functional_cast (cp_parser* parser, tree type)
33138 vec<tree, va_gc> *vec;
33139 tree expression_list;
33140 cp_expr cast;
33142 location_t start_loc = input_location;
33144 if (!type)
33145 type = error_mark_node;
33147 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
33149 cp_lexer_set_source_position (parser->lexer);
33150 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
33151 expression_list = cp_parser_braced_list (parser);
33152 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
33153 if (TREE_CODE (type) == TYPE_DECL)
33154 type = TREE_TYPE (type);
33156 cast = finish_compound_literal (type, expression_list,
33157 tf_warning_or_error, fcl_functional);
33158 /* Create a location of the form:
33159 type_name{i, f}
33160 ^~~~~~~~~~~~~~~
33161 with caret == start at the start of the type name,
33162 finishing at the closing brace. */
33163 location_t combined_loc = make_location (start_loc, start_loc,
33164 parser->lexer);
33165 cast.set_location (combined_loc);
33166 return cast;
33170 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
33171 /*cast_p=*/true,
33172 /*allow_expansion_p=*/true,
33173 /*non_constant_p=*/NULL);
33174 if (vec == NULL)
33175 expression_list = error_mark_node;
33176 else
33178 expression_list = build_tree_list_vec (vec);
33179 release_tree_vector (vec);
33182 /* Create a location of the form:
33183 float(i)
33184 ^~~~~~~~
33185 with caret == start at the start of the type name,
33186 finishing at the closing paren. */
33187 location_t combined_loc = make_location (start_loc, start_loc,
33188 parser->lexer);
33189 cast = build_functional_cast (combined_loc, type, expression_list,
33190 tf_warning_or_error);
33192 /* [expr.const]/1: In an integral constant expression "only type
33193 conversions to integral or enumeration type can be used". */
33194 if (TREE_CODE (type) == TYPE_DECL)
33195 type = TREE_TYPE (type);
33196 if (cast != error_mark_node
33197 && !cast_valid_in_integral_constant_expression_p (type)
33198 && cp_parser_non_integral_constant_expression (parser,
33199 NIC_CONSTRUCTOR))
33200 return error_mark_node;
33202 return cast;
33205 /* Save the tokens that make up the body of a member function defined
33206 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
33207 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
33208 specifiers applied to the declaration. Returns the FUNCTION_DECL
33209 for the member function. */
33211 static tree
33212 cp_parser_save_member_function_body (cp_parser* parser,
33213 cp_decl_specifier_seq *decl_specifiers,
33214 cp_declarator *declarator,
33215 tree attributes)
33217 cp_token *first;
33218 cp_token *last;
33219 tree fn;
33220 bool function_try_block = false;
33222 /* Create the FUNCTION_DECL. */
33223 fn = grokmethod (decl_specifiers, declarator, attributes);
33224 cp_finalize_omp_declare_simd (parser, fn);
33225 cp_finalize_oacc_routine (parser, fn, true);
33226 /* If something went badly wrong, bail out now. */
33227 if (fn == error_mark_node)
33229 /* If there's a function-body, skip it. */
33230 if (cp_parser_token_starts_function_definition_p
33231 (cp_lexer_peek_token (parser->lexer)))
33232 cp_parser_skip_to_end_of_block_or_statement (parser);
33233 return error_mark_node;
33236 /* Remember it, if there are default args to post process. */
33237 cp_parser_save_default_args (parser, fn);
33239 /* Save away the tokens that make up the body of the
33240 function. */
33241 first = parser->lexer->next_token;
33243 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
33244 cp_lexer_consume_token (parser->lexer);
33245 else if (cp_lexer_next_token_is_keyword (parser->lexer,
33246 RID_TRANSACTION_ATOMIC))
33248 cp_lexer_consume_token (parser->lexer);
33249 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
33250 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
33251 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
33252 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
33253 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
33254 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
33255 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
33257 cp_lexer_consume_token (parser->lexer);
33258 cp_lexer_consume_token (parser->lexer);
33259 cp_lexer_consume_token (parser->lexer);
33260 cp_lexer_consume_token (parser->lexer);
33261 cp_lexer_consume_token (parser->lexer);
33263 else
33264 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
33265 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
33267 cp_lexer_consume_token (parser->lexer);
33268 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
33269 break;
33273 /* Handle function try blocks. */
33274 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
33276 cp_lexer_consume_token (parser->lexer);
33277 function_try_block = true;
33279 /* We can have braced-init-list mem-initializers before the fn body. */
33280 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
33282 cp_lexer_consume_token (parser->lexer);
33283 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
33285 /* cache_group will stop after an un-nested { } pair, too. */
33286 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
33287 break;
33289 /* variadic mem-inits have ... after the ')'. */
33290 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
33291 cp_lexer_consume_token (parser->lexer);
33294 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
33295 /* Handle function try blocks. */
33296 if (function_try_block)
33297 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
33298 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
33299 last = parser->lexer->next_token;
33301 /* Save away the inline definition; we will process it when the
33302 class is complete. */
33303 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
33304 DECL_PENDING_INLINE_P (fn) = 1;
33306 /* We need to know that this was defined in the class, so that
33307 friend templates are handled correctly. */
33308 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
33310 /* Add FN to the queue of functions to be parsed later. */
33311 vec_safe_push (unparsed_funs_with_definitions, fn);
33313 return fn;
33316 /* Save the tokens that make up the in-class initializer for a non-static
33317 data member. Returns a DEFERRED_PARSE. */
33319 static tree
33320 cp_parser_save_nsdmi (cp_parser* parser)
33322 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
33325 /* Parse a template-argument-list, as well as the trailing ">" (but
33326 not the opening "<"). See cp_parser_template_argument_list for the
33327 return value. */
33329 static tree
33330 cp_parser_enclosed_template_argument_list (cp_parser* parser)
33332 tree arguments;
33333 tree saved_scope;
33334 tree saved_qualifying_scope;
33335 tree saved_object_scope;
33336 bool saved_greater_than_is_operator_p;
33338 /* [temp.names]
33340 When parsing a template-id, the first non-nested `>' is taken as
33341 the end of the template-argument-list rather than a greater-than
33342 operator. */
33343 saved_greater_than_is_operator_p
33344 = parser->greater_than_is_operator_p;
33345 parser->greater_than_is_operator_p = false;
33346 /* Parsing the argument list may modify SCOPE, so we save it
33347 here. */
33348 saved_scope = parser->scope;
33349 saved_qualifying_scope = parser->qualifying_scope;
33350 saved_object_scope = parser->object_scope;
33351 /* We need to evaluate the template arguments, even though this
33352 template-id may be nested within a "sizeof". */
33353 cp_evaluated ev;
33354 /* Parse the template-argument-list itself. */
33355 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
33356 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT)
33357 || cp_lexer_next_token_is (parser->lexer, CPP_GREATER_EQ)
33358 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT_EQ))
33360 arguments = make_tree_vec (0);
33361 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (arguments, 0);
33363 else
33364 arguments = cp_parser_template_argument_list (parser);
33365 /* Look for the `>' that ends the template-argument-list. If we find
33366 a '>>' instead, it's probably just a typo. */
33367 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
33369 if (cxx_dialect != cxx98)
33371 /* In C++0x, a `>>' in a template argument list or cast
33372 expression is considered to be two separate `>'
33373 tokens. So, change the current token to a `>', but don't
33374 consume it: it will be consumed later when the outer
33375 template argument list (or cast expression) is parsed.
33376 Note that this replacement of `>' for `>>' is necessary
33377 even if we are parsing tentatively: in the tentative
33378 case, after calling
33379 cp_parser_enclosed_template_argument_list we will always
33380 throw away all of the template arguments and the first
33381 closing `>', either because the template argument list
33382 was erroneous or because we are replacing those tokens
33383 with a CPP_TEMPLATE_ID token. The second `>' (which will
33384 not have been thrown away) is needed either to close an
33385 outer template argument list or to complete a new-style
33386 cast. */
33387 cp_token *token = cp_lexer_peek_token (parser->lexer);
33388 token->type = CPP_GREATER;
33390 else if (!saved_greater_than_is_operator_p)
33392 /* If we're in a nested template argument list, the '>>' has
33393 to be a typo for '> >'. We emit the error message, but we
33394 continue parsing and we push a '>' as next token, so that
33395 the argument list will be parsed correctly. Note that the
33396 global source location is still on the token before the
33397 '>>', so we need to say explicitly where we want it. */
33398 cp_token *token = cp_lexer_peek_token (parser->lexer);
33399 gcc_rich_location richloc (token->location);
33400 richloc.add_fixit_replace ("> >");
33401 error_at (&richloc, "%<>>%> should be %<> >%> "
33402 "within a nested template argument list");
33404 token->type = CPP_GREATER;
33406 else
33408 /* If this is not a nested template argument list, the '>>'
33409 is a typo for '>'. Emit an error message and continue.
33410 Same deal about the token location, but here we can get it
33411 right by consuming the '>>' before issuing the diagnostic. */
33412 cp_token *token = cp_lexer_consume_token (parser->lexer);
33413 error_at (token->location,
33414 "spurious %<>>%>, use %<>%> to terminate "
33415 "a template argument list");
33418 /* Similarly for >>= and >=. */
33419 else if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER_EQ)
33420 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT_EQ))
33422 cp_token *token = cp_lexer_consume_token (parser->lexer);
33423 gcc_rich_location richloc (token->location);
33424 enum cpp_ttype new_type;
33425 const char *replacement;
33426 if (token->type == CPP_GREATER_EQ)
33428 replacement = "> =";
33429 new_type = CPP_EQ;
33431 else if (!saved_greater_than_is_operator_p)
33433 if (cxx_dialect != cxx98)
33434 replacement = ">> =";
33435 else
33436 replacement = "> > =";
33437 new_type = CPP_GREATER;
33439 else
33441 replacement = "> >=";
33442 new_type = CPP_GREATER_EQ;
33444 richloc.add_fixit_replace (replacement);
33445 error_at (&richloc, "%qs should be %qs to terminate a template "
33446 "argument list",
33447 cpp_type2name (token->type, token->flags), replacement);
33448 token->type = new_type;
33450 else
33451 cp_parser_require_end_of_template_parameter_list (parser);
33452 /* The `>' token might be a greater-than operator again now. */
33453 parser->greater_than_is_operator_p
33454 = saved_greater_than_is_operator_p;
33455 /* Restore the SAVED_SCOPE. */
33456 parser->scope = saved_scope;
33457 parser->qualifying_scope = saved_qualifying_scope;
33458 parser->object_scope = saved_object_scope;
33460 return arguments;
33463 /* MEMBER_FUNCTION is a member function, or a friend. If default
33464 arguments, or the body of the function have not yet been parsed,
33465 parse them now. */
33467 static void
33468 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
33470 auto_timevar tv (TV_PARSE_INMETH);
33472 /* If this member is a template, get the underlying
33473 FUNCTION_DECL. */
33474 if (DECL_FUNCTION_TEMPLATE_P (member_function))
33475 member_function = DECL_TEMPLATE_RESULT (member_function);
33477 /* There should not be any class definitions in progress at this
33478 point; the bodies of members are only parsed outside of all class
33479 definitions. */
33480 gcc_assert (parser->num_classes_being_defined == 0);
33481 /* While we're parsing the member functions we might encounter more
33482 classes. We want to handle them right away, but we don't want
33483 them getting mixed up with functions that are currently in the
33484 queue. */
33485 push_unparsed_function_queues (parser);
33487 /* Make sure that any template parameters are in scope. */
33488 maybe_begin_member_template_processing (member_function);
33490 /* If the body of the function has not yet been parsed, parse it
33491 now. Except if the tokens have been purged (PR c++/39751). */
33492 if (DECL_PENDING_INLINE_P (member_function)
33493 && !DECL_PENDING_INLINE_INFO (member_function)->first->purged_p)
33495 tree function_scope;
33496 cp_token_cache *tokens;
33498 /* The function is no longer pending; we are processing it. */
33499 tokens = DECL_PENDING_INLINE_INFO (member_function);
33500 DECL_PENDING_INLINE_INFO (member_function) = NULL;
33501 DECL_PENDING_INLINE_P (member_function) = 0;
33503 /* If this is a local class, enter the scope of the containing
33504 function. */
33505 function_scope = current_function_decl;
33506 if (function_scope)
33507 push_function_context ();
33509 /* Push the body of the function onto the lexer stack. */
33510 cp_parser_push_lexer_for_tokens (parser, tokens);
33512 /* Let the front end know that we going to be defining this
33513 function. */
33514 start_preparsed_function (member_function, NULL_TREE,
33515 SF_PRE_PARSED | SF_INCLASS_INLINE);
33517 /* #pragma omp declare reduction needs special parsing. */
33518 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
33520 parser->lexer->in_pragma = true;
33521 cp_parser_omp_declare_reduction_exprs (member_function, parser);
33522 finish_function (/*inline_p=*/true);
33523 cp_check_omp_declare_reduction (member_function);
33525 else
33526 /* Now, parse the body of the function. */
33527 cp_parser_function_definition_after_declarator (parser,
33528 /*inline_p=*/true);
33530 /* Leave the scope of the containing function. */
33531 if (function_scope)
33532 pop_function_context ();
33533 cp_parser_pop_lexer (parser);
33536 /* Remove any template parameters from the symbol table. */
33537 maybe_end_member_template_processing ();
33539 /* Restore the queue. */
33540 pop_unparsed_function_queues (parser);
33543 /* If DECL contains any default args, remember it on the unparsed
33544 functions queue. */
33546 static void
33547 cp_parser_save_default_args (cp_parser* parser, tree decl)
33549 tree probe;
33551 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
33552 probe;
33553 probe = TREE_CHAIN (probe))
33554 if (TREE_PURPOSE (probe))
33556 cp_default_arg_entry entry = {current_class_type, decl};
33557 vec_safe_push (unparsed_funs_with_default_args, entry);
33558 break;
33561 /* Remember if there is a noexcept-specifier to post process. */
33562 tree spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl));
33563 if (UNPARSED_NOEXCEPT_SPEC_P (spec))
33564 vec_safe_push (unparsed_noexcepts, decl);
33566 /* Contracts are deferred. */
33567 for (tree attr = DECL_ATTRIBUTES (decl); attr; attr = TREE_CHAIN (attr))
33568 if (cxx_contract_attribute_p (attr))
33570 vec_safe_push (unparsed_contracts, decl);
33571 break;
33575 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
33576 which is either a FIELD_DECL or PARM_DECL. Parse it and return
33577 the result. For a PARM_DECL, PARMTYPE is the corresponding type
33578 from the parameter-type-list. */
33580 static tree
33581 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
33582 tree default_arg, tree parmtype)
33584 cp_token_cache *tokens;
33585 tree parsed_arg;
33587 if (default_arg == error_mark_node)
33588 return error_mark_node;
33590 /* Push the saved tokens for the default argument onto the parser's
33591 lexer stack. */
33592 tokens = DEFPARSE_TOKENS (default_arg);
33593 cp_parser_push_lexer_for_tokens (parser, tokens);
33595 start_lambda_scope (decl);
33597 /* Parse the default argument. */
33598 parsed_arg = cp_parser_initializer (parser);
33599 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
33600 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
33602 finish_lambda_scope ();
33604 if (parsed_arg == error_mark_node)
33605 cp_parser_skip_to_end_of_statement (parser);
33607 if (!processing_template_decl)
33609 /* In a non-template class, check conversions now. In a template,
33610 we'll wait and instantiate these as needed. */
33611 if (TREE_CODE (decl) == PARM_DECL)
33612 parsed_arg = check_default_argument (parmtype, parsed_arg,
33613 tf_warning_or_error);
33614 else if (maybe_reject_flexarray_init (decl, parsed_arg))
33615 parsed_arg = error_mark_node;
33616 else
33617 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
33620 /* If the token stream has not been completely used up, then
33621 there was extra junk after the end of the default
33622 argument. */
33623 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
33625 if (TREE_CODE (decl) == PARM_DECL)
33626 cp_parser_error (parser, "expected %<,%>");
33627 else
33628 cp_parser_error (parser, "expected %<;%>");
33631 /* Revert to the main lexer. */
33632 cp_parser_pop_lexer (parser);
33634 return parsed_arg;
33637 /* FIELD is a non-static data member with an initializer which we saved for
33638 later; parse it now. */
33640 static void
33641 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
33643 tree def;
33645 maybe_begin_member_template_processing (field);
33647 push_unparsed_function_queues (parser);
33648 def = cp_parser_late_parse_one_default_arg (parser, field,
33649 DECL_INITIAL (field),
33650 NULL_TREE);
33651 pop_unparsed_function_queues (parser);
33653 maybe_end_member_template_processing ();
33655 DECL_INITIAL (field) = def;
33658 /* FN is a FUNCTION_DECL which may contains a parameter with an
33659 unparsed DEFERRED_PARSE. Parse the default args now. This function
33660 assumes that the current scope is the scope in which the default
33661 argument should be processed. */
33663 static void
33664 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
33666 unsigned char saved_local_variables_forbidden_p;
33668 /* While we're parsing the default args, we might (due to the
33669 statement expression extension) encounter more classes. We want
33670 to handle them right away, but we don't want them getting mixed
33671 up with default args that are currently in the queue. */
33672 push_unparsed_function_queues (parser);
33674 /* Local variable names (and the `this' keyword) may not appear
33675 in a default argument. */
33676 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
33677 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
33679 push_defarg_context (fn);
33681 begin_scope (sk_function_parms, fn);
33683 /* Gather the PARM_DECLs into a vec so we can keep track of them when
33684 pushdecl clears DECL_CHAIN. */
33685 releasing_vec parms;
33686 for (tree parmdecl = DECL_ARGUMENTS (fn); parmdecl;
33687 parmdecl = DECL_CHAIN (parmdecl))
33688 vec_safe_push (parms, parmdecl);
33690 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
33691 for (int i = 0;
33692 parm && parm != void_list_node;
33693 parm = TREE_CHAIN (parm),
33694 ++i)
33696 tree default_arg = TREE_PURPOSE (parm);
33697 tree parsed_arg;
33699 tree parmdecl = parms[i];
33700 pushdecl (parmdecl);
33702 if (!default_arg)
33703 continue;
33705 if (TREE_CODE (default_arg) != DEFERRED_PARSE)
33706 /* This can happen for a friend declaration for a function
33707 already declared with default arguments. */
33708 continue;
33710 parsed_arg
33711 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
33712 default_arg,
33713 TREE_VALUE (parm));
33714 TREE_PURPOSE (parm) = parsed_arg;
33716 /* Update any instantiations we've already created. */
33717 for (tree copy : DEFPARSE_INSTANTIATIONS (default_arg))
33718 TREE_PURPOSE (copy) = parsed_arg;
33721 pop_bindings_and_leave_scope ();
33723 /* Restore DECL_CHAINs after clobbering by pushdecl. */
33724 parm = NULL_TREE;
33725 for (int i = parms->length () - 1; i >= 0; --i)
33727 DECL_CHAIN (parms[i]) = parm;
33728 parm = parms[i];
33731 pop_defarg_context ();
33733 /* Make sure no default arg is missing. */
33734 check_default_args (fn);
33736 /* Restore the state of local_variables_forbidden_p. */
33737 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
33739 /* Restore the queue. */
33740 pop_unparsed_function_queues (parser);
33743 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
33745 sizeof ... ( identifier )
33747 where the 'sizeof' token has already been consumed. */
33749 static tree
33750 cp_parser_sizeof_pack (cp_parser *parser)
33752 /* Consume the `...'. */
33753 cp_lexer_consume_token (parser->lexer);
33754 maybe_warn_variadic_templates ();
33756 matching_parens parens;
33757 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
33758 if (paren)
33759 parens.consume_open (parser);
33760 else
33761 permerror (cp_lexer_peek_token (parser->lexer)->location,
33762 "%<sizeof...%> argument must be surrounded by parentheses");
33764 cp_token *token = cp_lexer_peek_token (parser->lexer);
33765 tree name = cp_parser_identifier (parser);
33766 if (name == error_mark_node)
33767 return error_mark_node;
33768 /* The name is not qualified. */
33769 parser->scope = NULL_TREE;
33770 parser->qualifying_scope = NULL_TREE;
33771 parser->object_scope = NULL_TREE;
33772 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
33773 if (expr == error_mark_node)
33774 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
33775 token->location);
33776 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
33777 expr = TREE_TYPE (expr);
33778 else if (TREE_CODE (expr) == CONST_DECL)
33779 expr = DECL_INITIAL (expr);
33780 expr = make_pack_expansion (expr);
33781 if (expr != error_mark_node)
33782 PACK_EXPANSION_SIZEOF_P (expr) = true;
33784 if (paren)
33785 parens.require_close (parser);
33787 return expr;
33790 /* Parse the operand of `sizeof' (or a similar operator). Returns
33791 either a TYPE or an expression, depending on the form of the
33792 input. The KEYWORD indicates which kind of expression we have
33793 encountered. */
33795 static tree
33796 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
33798 tree expr = NULL_TREE;
33799 const char *saved_message;
33800 const char *saved_message_arg;
33801 bool saved_integral_constant_expression_p;
33802 bool saved_non_integral_constant_expression_p;
33804 /* If it's a `...', then we are computing the length of a parameter
33805 pack. */
33806 if (keyword == RID_SIZEOF
33807 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
33808 return cp_parser_sizeof_pack (parser);
33810 /* Types cannot be defined in a `sizeof' expression. Save away the
33811 old message. */
33812 saved_message = parser->type_definition_forbidden_message;
33813 saved_message_arg = parser->type_definition_forbidden_message_arg;
33814 parser->type_definition_forbidden_message
33815 = G_("types may not be defined in %qs expressions");
33816 parser->type_definition_forbidden_message_arg
33817 = IDENTIFIER_POINTER (ridpointers[keyword]);
33819 /* The restrictions on constant-expressions do not apply inside
33820 sizeof expressions. */
33821 saved_integral_constant_expression_p
33822 = parser->integral_constant_expression_p;
33823 saved_non_integral_constant_expression_p
33824 = parser->non_integral_constant_expression_p;
33825 parser->integral_constant_expression_p = false;
33827 auto cleanup = make_temp_override
33828 (parser->auto_is_implicit_function_template_parm_p, false);
33830 /* Do not actually evaluate the expression. */
33831 ++cp_unevaluated_operand;
33832 ++c_inhibit_evaluation_warnings;
33833 /* If it's a `(', then we might be looking at the type-id
33834 construction. */
33835 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33837 tree type = NULL_TREE;
33839 tentative_firewall firewall (parser);
33841 /* We can't be sure yet whether we're looking at a type-id or an
33842 expression. */
33843 cp_parser_parse_tentatively (parser);
33845 matching_parens parens;
33846 parens.consume_open (parser);
33848 /* Note: as a GNU Extension, compound literals are considered
33849 postfix-expressions as they are in C99, so they are valid
33850 arguments to sizeof. See comment in cp_parser_cast_expression
33851 for details. */
33852 if (cp_parser_compound_literal_p (parser))
33853 cp_parser_simulate_error (parser);
33854 else
33856 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
33857 parser->in_type_id_in_expr_p = true;
33858 /* Look for the type-id. */
33859 type = cp_parser_type_id (parser);
33860 /* Look for the closing `)'. */
33861 parens.require_close (parser);
33862 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
33865 /* If all went well, then we're done. */
33866 if (cp_parser_parse_definitely (parser))
33867 expr = type;
33868 else
33870 /* Commit to the tentative_firewall so we get syntax errors. */
33871 cp_parser_commit_to_tentative_parse (parser);
33873 expr = cp_parser_unary_expression (parser);
33876 else
33877 expr = cp_parser_unary_expression (parser);
33879 /* Go back to evaluating expressions. */
33880 --cp_unevaluated_operand;
33881 --c_inhibit_evaluation_warnings;
33883 /* And restore the old one. */
33884 parser->type_definition_forbidden_message = saved_message;
33885 parser->type_definition_forbidden_message_arg = saved_message_arg;
33886 parser->integral_constant_expression_p
33887 = saved_integral_constant_expression_p;
33888 parser->non_integral_constant_expression_p
33889 = saved_non_integral_constant_expression_p;
33891 return expr;
33894 /* If the current declaration has no declarator, return true. */
33896 static bool
33897 cp_parser_declares_only_class_p (cp_parser *parser)
33899 /* If the next token is a `;' or a `,' then there is no
33900 declarator. */
33901 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
33902 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
33905 /* Update the DECL_SPECS to reflect the storage class indicated by
33906 KEYWORD. */
33908 static void
33909 cp_parser_set_storage_class (cp_parser *parser,
33910 cp_decl_specifier_seq *decl_specs,
33911 enum rid keyword,
33912 cp_token *token)
33914 cp_storage_class storage_class;
33916 switch (keyword)
33918 case RID_AUTO:
33919 storage_class = sc_auto;
33920 break;
33921 case RID_REGISTER:
33922 storage_class = sc_register;
33923 break;
33924 case RID_STATIC:
33925 storage_class = sc_static;
33926 break;
33927 case RID_EXTERN:
33928 storage_class = sc_extern;
33929 break;
33930 case RID_MUTABLE:
33931 storage_class = sc_mutable;
33932 break;
33933 default:
33934 gcc_unreachable ();
33937 if (parser->in_unbraced_linkage_specification_p)
33939 error_at (token->location, "invalid use of %qD in linkage specification",
33940 ridpointers[keyword]);
33941 return;
33943 else if (decl_specs->storage_class != sc_none)
33945 if (decl_specs->conflicting_specifiers_p)
33946 return;
33947 gcc_rich_location richloc (token->location);
33948 richloc.add_location_if_nearby (decl_specs->locations[ds_storage_class]);
33949 if (decl_specs->storage_class == storage_class)
33950 error_at (&richloc, "duplicate %qD specifier", ridpointers[keyword]);
33951 else
33952 error_at (&richloc,
33953 "%qD specifier conflicts with %qs",
33954 ridpointers[keyword],
33955 cp_storage_class_name[decl_specs->storage_class]);
33956 decl_specs->conflicting_specifiers_p = true;
33957 return;
33960 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
33961 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
33962 && decl_specs->gnu_thread_keyword_p)
33964 pedwarn (decl_specs->locations[ds_thread], 0,
33965 "%<__thread%> before %qD", ridpointers[keyword]);
33968 decl_specs->storage_class = storage_class;
33969 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
33971 /* A storage class specifier cannot be applied alongside a typedef
33972 specifier. If there is a typedef specifier present then set
33973 conflicting_specifiers_p which will trigger an error later
33974 on in grokdeclarator. */
33975 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
33976 && !decl_specs->conflicting_specifiers_p)
33978 gcc_rich_location richloc (token->location);
33979 richloc.add_location_if_nearby (decl_specs->locations[ds_typedef]);
33980 error_at (&richloc,
33981 "%qD specifier conflicts with %<typedef%>",
33982 ridpointers[keyword]);
33983 decl_specs->conflicting_specifiers_p = true;
33987 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
33988 is true, the type is a class or enum definition. */
33990 static void
33991 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
33992 tree type_spec,
33993 cp_token *token,
33994 bool type_definition_p)
33996 decl_specs->any_specifiers_p = true;
33998 /* If the user tries to redeclare bool, char8_t, char16_t, char32_t, or
33999 wchar_t (with, for example, in "typedef int wchar_t;") we remember that
34000 this is what happened. In system headers, we ignore these
34001 declarations so that G++ can work with system headers that are not
34002 C++-safe. */
34003 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
34004 && !type_definition_p
34005 && TYPE_P (type_spec)
34006 && (type_spec == boolean_type_node
34007 || type_spec == char8_type_node
34008 || type_spec == char16_type_node
34009 || type_spec == char32_type_node
34010 || extended_float_type_p (type_spec)
34011 || type_spec == wchar_type_node)
34012 && (decl_specs->type
34013 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
34014 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
34015 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
34016 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
34018 decl_specs->redefined_builtin_type = type_spec;
34019 set_and_check_decl_spec_loc (decl_specs,
34020 ds_redefined_builtin_type_spec,
34021 token);
34022 if (!decl_specs->type)
34024 decl_specs->type = type_spec;
34025 decl_specs->type_definition_p = false;
34026 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
34029 else if (decl_specs->type)
34030 decl_specs->multiple_types_p = true;
34031 else
34033 decl_specs->type = type_spec;
34034 decl_specs->type_definition_p = type_definition_p;
34035 decl_specs->redefined_builtin_type = NULL_TREE;
34036 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
34040 /* True iff TOKEN is the GNU keyword __thread. */
34042 static bool
34043 token_is__thread (cp_token *token)
34045 gcc_assert (token->keyword == RID_THREAD);
34046 return id_equal (token->u.value, "__thread");
34049 /* Set the location for a declarator specifier and check if it is
34050 duplicated.
34052 DECL_SPECS is the sequence of declarator specifiers onto which to
34053 set the location.
34055 DS is the single declarator specifier to set which location is to
34056 be set onto the existing sequence of declarators.
34058 LOCATION is the location for the declarator specifier to
34059 consider. */
34061 static void
34062 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
34063 cp_decl_spec ds, cp_token *token)
34065 gcc_assert (ds < ds_last);
34067 if (decl_specs == NULL)
34068 return;
34070 location_t location = token->location;
34072 if (decl_specs->locations[ds] == 0)
34074 decl_specs->locations[ds] = location;
34075 if (ds == ds_thread)
34076 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
34078 else
34080 if (ds == ds_long)
34082 if (decl_specs->locations[ds_long_long] != 0)
34083 error_at (location,
34084 "%<long long long%> is too long for GCC");
34085 else
34087 decl_specs->locations[ds_long_long] = location;
34088 pedwarn_cxx98 (location,
34089 OPT_Wlong_long,
34090 "ISO C++ 1998 does not support %<long long%>");
34093 else if (ds == ds_thread)
34095 bool gnu = token_is__thread (token);
34096 gcc_rich_location richloc (location);
34097 if (gnu != decl_specs->gnu_thread_keyword_p)
34099 richloc.add_range (decl_specs->locations[ds_thread]);
34100 error_at (&richloc,
34101 "both %<__thread%> and %<thread_local%> specified");
34103 else
34105 richloc.add_fixit_remove ();
34106 error_at (&richloc, "duplicate %qD", token->u.value);
34109 else
34111 /* These correspond to cp-tree.h:cp_decl_spec,
34112 changes here should also be reflected there. */
34113 static const char *const decl_spec_names[] = {
34114 "signed",
34115 "unsigned",
34116 "short",
34117 "long",
34118 "const",
34119 "volatile",
34120 "restrict",
34121 "inline",
34122 "virtual",
34123 "explicit",
34124 "friend",
34125 "typedef",
34126 "using",
34127 "constexpr",
34128 "__complex",
34129 "constinit",
34130 "consteval",
34131 "this"
34133 gcc_rich_location richloc (location);
34134 richloc.add_fixit_remove ();
34135 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
34140 /* Return true iff the declarator specifier DS is present in the
34141 sequence of declarator specifiers DECL_SPECS. */
34143 bool
34144 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
34145 cp_decl_spec ds)
34147 gcc_assert (ds < ds_last);
34149 if (decl_specs == NULL)
34150 return false;
34152 return decl_specs->locations[ds] != 0;
34155 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
34156 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
34158 static bool
34159 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
34161 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
34164 /* Issue an error message indicating that TOKEN_DESC was expected.
34165 If KEYWORD is true, it indicated this function is called by
34166 cp_parser_require_keword and the required token can only be
34167 a indicated keyword.
34169 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
34170 within any error as the location of an "opening" token matching
34171 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
34172 RT_CLOSE_PAREN). */
34174 static void
34175 cp_parser_required_error (cp_parser *parser,
34176 required_token token_desc,
34177 bool keyword,
34178 location_t matching_location)
34180 if (cp_parser_simulate_error (parser))
34181 return;
34183 const char *gmsgid = NULL;
34184 switch (token_desc)
34186 case RT_NEW:
34187 gmsgid = G_("expected %<new%>");
34188 break;
34189 case RT_DELETE:
34190 gmsgid = G_("expected %<delete%>");
34191 break;
34192 case RT_RETURN:
34193 gmsgid = G_("expected %<return%>");
34194 break;
34195 case RT_WHILE:
34196 gmsgid = G_("expected %<while%>");
34197 break;
34198 case RT_EXTERN:
34199 gmsgid = G_("expected %<extern%>");
34200 break;
34201 case RT_STATIC_ASSERT:
34202 gmsgid = G_("expected %<static_assert%>");
34203 break;
34204 case RT_DECLTYPE:
34205 gmsgid = G_("expected %<decltype%>");
34206 break;
34207 case RT_OPERATOR:
34208 gmsgid = G_("expected %<operator%>");
34209 break;
34210 case RT_CLASS:
34211 gmsgid = G_("expected %<class%>");
34212 break;
34213 case RT_TEMPLATE:
34214 gmsgid = G_("expected %<template%>");
34215 break;
34216 case RT_NAMESPACE:
34217 gmsgid = G_("expected %<namespace%>");
34218 break;
34219 case RT_USING:
34220 gmsgid = G_("expected %<using%>");
34221 break;
34222 case RT_ASM:
34223 gmsgid = G_("expected %<asm%>");
34224 break;
34225 case RT_TRY:
34226 gmsgid = G_("expected %<try%>");
34227 break;
34228 case RT_CATCH:
34229 gmsgid = G_("expected %<catch%>");
34230 break;
34231 case RT_THROW:
34232 gmsgid = G_("expected %<throw%>");
34233 break;
34234 case RT_AUTO:
34235 gmsgid = G_("expected %<auto%>");
34236 break;
34237 case RT_LABEL:
34238 gmsgid = G_("expected %<__label__%>");
34239 break;
34240 case RT_AT_TRY:
34241 gmsgid = G_("expected %<@try%>");
34242 break;
34243 case RT_AT_SYNCHRONIZED:
34244 gmsgid = G_("expected %<@synchronized%>");
34245 break;
34246 case RT_AT_THROW:
34247 gmsgid = G_("expected %<@throw%>");
34248 break;
34249 case RT_TRANSACTION_ATOMIC:
34250 gmsgid = G_("expected %<__transaction_atomic%>");
34251 break;
34252 case RT_TRANSACTION_RELAXED:
34253 gmsgid = G_("expected %<__transaction_relaxed%>");
34254 break;
34255 case RT_CO_YIELD:
34256 gmsgid = G_("expected %<co_yield%>");
34257 break;
34258 default:
34259 break;
34262 if (!gmsgid && !keyword)
34264 switch (token_desc)
34266 case RT_SEMICOLON:
34267 gmsgid = G_("expected %<;%>");
34268 break;
34269 case RT_OPEN_PAREN:
34270 gmsgid = G_("expected %<(%>");
34271 break;
34272 case RT_CLOSE_BRACE:
34273 gmsgid = G_("expected %<}%>");
34274 break;
34275 case RT_OPEN_BRACE:
34276 gmsgid = G_("expected %<{%>");
34277 break;
34278 case RT_CLOSE_SQUARE:
34279 gmsgid = G_("expected %<]%>");
34280 break;
34281 case RT_OPEN_SQUARE:
34282 gmsgid = G_("expected %<[%>");
34283 break;
34284 case RT_COMMA:
34285 gmsgid = G_("expected %<,%>");
34286 break;
34287 case RT_SCOPE:
34288 gmsgid = G_("expected %<::%>");
34289 break;
34290 case RT_LESS:
34291 gmsgid = G_("expected %<<%>");
34292 break;
34293 case RT_GREATER:
34294 gmsgid = G_("expected %<>%>");
34295 break;
34296 case RT_EQ:
34297 gmsgid = G_("expected %<=%>");
34298 break;
34299 case RT_ELLIPSIS:
34300 gmsgid = G_("expected %<...%>");
34301 break;
34302 case RT_MULT:
34303 gmsgid = G_("expected %<*%>");
34304 break;
34305 case RT_COMPL:
34306 gmsgid = G_("expected %<~%>");
34307 break;
34308 case RT_COLON:
34309 gmsgid = G_("expected %<:%>");
34310 break;
34311 case RT_COLON_SCOPE:
34312 gmsgid = G_("expected %<:%> or %<::%>");
34313 break;
34314 case RT_CLOSE_PAREN:
34315 gmsgid = G_("expected %<)%>");
34316 break;
34317 case RT_COMMA_CLOSE_PAREN:
34318 gmsgid = G_("expected %<,%> or %<)%>");
34319 break;
34320 case RT_PRAGMA_EOL:
34321 gmsgid = G_("expected end of line");
34322 break;
34323 case RT_NAME:
34324 gmsgid = G_("expected identifier");
34325 break;
34326 case RT_SELECT:
34327 gmsgid = G_("expected selection-statement");
34328 break;
34329 case RT_ITERATION:
34330 gmsgid = G_("expected iteration-statement");
34331 break;
34332 case RT_JUMP:
34333 gmsgid = G_("expected jump-statement");
34334 break;
34335 case RT_CLASS_KEY:
34336 gmsgid = G_("expected class-key");
34337 break;
34338 case RT_CLASS_TYPENAME_TEMPLATE:
34339 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
34340 break;
34341 default:
34342 gcc_unreachable ();
34346 if (gmsgid)
34347 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
34351 /* If the next token is of the indicated TYPE, consume it. Otherwise,
34352 issue an error message indicating that TOKEN_DESC was expected.
34354 Returns the token consumed, if the token had the appropriate type.
34355 Otherwise, returns NULL.
34357 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
34358 within any error as the location of an "opening" token matching
34359 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
34360 RT_CLOSE_PAREN). */
34362 static cp_token *
34363 cp_parser_require (cp_parser* parser,
34364 enum cpp_ttype type,
34365 required_token token_desc,
34366 location_t matching_location)
34368 if (cp_lexer_next_token_is (parser->lexer, type))
34369 return cp_lexer_consume_token (parser->lexer);
34370 else
34372 /* Output the MESSAGE -- unless we're parsing tentatively. */
34373 if (!cp_parser_simulate_error (parser))
34374 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
34375 matching_location);
34376 return NULL;
34380 /* Skip an entire parameter list from start to finish. The next token must
34381 be the initial "<" of the parameter list. Returns true on success and
34382 false otherwise. */
34384 static bool
34385 cp_parser_skip_entire_template_parameter_list (cp_parser* parser)
34387 /* Consume the "<" because cp_parser_skip_to_end_of_template_parameter_list
34388 requires it. */
34389 cp_lexer_consume_token (parser->lexer);
34390 return cp_parser_skip_to_end_of_template_parameter_list (parser);
34393 /* Ensure we are at the end of a template parameter list. If we are, return.
34394 If we are not, something has gone wrong, in which case issue an error and
34395 skip to end of the parameter list. */
34397 static void
34398 cp_parser_require_end_of_template_parameter_list (cp_parser* parser)
34400 /* Are we ready, yet? If not, issue error message. */
34401 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
34402 return;
34404 cp_parser_skip_to_end_of_template_parameter_list (parser);
34407 /* You should only call this function from inside a template parameter list
34408 (i.e. the current token should at least be the initial "<" of the
34409 parameter list). If you are skipping the entire list, it may be better to
34410 use cp_parser_skip_entire_template_parameter_list.
34412 Tokens are skipped until the final ">" is found, or if we see
34413 '{', '}', ';', or if we find an unbalanced ')' or ']'.
34415 Returns true if we successfully reached the end, and false if
34416 something unexpected happened (e.g. end of file). */
34418 static bool
34419 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
34421 /* Current level of '< ... >'. */
34422 unsigned level = 0;
34423 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
34424 unsigned nesting_depth = 0;
34426 /* Skip tokens until the desired token is found. */
34427 while (true)
34429 /* Peek at the next token. */
34430 switch (cp_lexer_peek_token (parser->lexer)->type)
34432 case CPP_LESS:
34433 if (!nesting_depth)
34434 ++level;
34435 break;
34437 case CPP_RSHIFT:
34438 if (cxx_dialect == cxx98)
34439 /* C++0x views the `>>' operator as two `>' tokens, but
34440 C++98 does not. */
34441 break;
34442 else if (!nesting_depth && level-- == 0)
34444 /* We've hit a `>>' where the first `>' closes the
34445 template argument list, and the second `>' is
34446 spurious. Just consume the `>>' and stop; we've
34447 already produced at least one error. */
34448 cp_lexer_consume_token (parser->lexer);
34449 return false;
34451 /* Fall through for C++0x, so we handle the second `>' in
34452 the `>>'. */
34453 gcc_fallthrough ();
34455 case CPP_GREATER:
34456 if (!nesting_depth && level-- == 0)
34458 /* We've reached the token we want, consume it and stop. */
34459 cp_lexer_consume_token (parser->lexer);
34460 return true;
34462 break;
34464 case CPP_OPEN_PAREN:
34465 case CPP_OPEN_SQUARE:
34466 ++nesting_depth;
34467 break;
34469 case CPP_CLOSE_PAREN:
34470 case CPP_CLOSE_SQUARE:
34471 if (nesting_depth-- == 0)
34472 return false;
34473 break;
34475 case CPP_EOF:
34476 case CPP_PRAGMA_EOL:
34477 case CPP_SEMICOLON:
34478 case CPP_OPEN_BRACE:
34479 case CPP_CLOSE_BRACE:
34480 /* The '>' was probably forgotten, don't look further. */
34481 return false;
34483 default:
34484 break;
34487 /* Consume this token. */
34488 cp_lexer_consume_token (parser->lexer);
34492 /* If the next token is the indicated keyword, consume it. Otherwise,
34493 issue an error message indicating that TOKEN_DESC was expected.
34495 Returns the token consumed, if the token had the appropriate type.
34496 Otherwise, returns NULL. */
34498 static cp_token *
34499 cp_parser_require_keyword (cp_parser* parser,
34500 enum rid keyword,
34501 required_token token_desc)
34503 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
34505 if (token && token->keyword != keyword)
34507 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
34508 UNKNOWN_LOCATION);
34509 return NULL;
34512 return token;
34515 /* Returns TRUE iff TOKEN is a token that can begin the body of a
34516 function-definition. */
34518 static bool
34519 cp_parser_token_starts_function_definition_p (cp_token* token)
34521 return (/* An ordinary function-body begins with an `{'. */
34522 token->type == CPP_OPEN_BRACE
34523 /* A ctor-initializer begins with a `:'. */
34524 || token->type == CPP_COLON
34525 /* A function-try-block begins with `try'. */
34526 || token->keyword == RID_TRY
34527 /* A function-transaction-block begins with `__transaction_atomic'
34528 or `__transaction_relaxed'. */
34529 || token->keyword == RID_TRANSACTION_ATOMIC
34530 || token->keyword == RID_TRANSACTION_RELAXED
34531 /* The named return value extension begins with `return'. */
34532 || token->keyword == RID_RETURN);
34535 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
34536 definition. */
34538 static bool
34539 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
34541 cp_token *token;
34543 token = cp_lexer_peek_token (parser->lexer);
34544 return (token->type == CPP_OPEN_BRACE
34545 || (token->type == CPP_COLON
34546 && !parser->colon_doesnt_start_class_def_p));
34549 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
34550 C++0x) ending a template-argument. */
34552 static bool
34553 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
34555 cp_token *token;
34557 token = cp_lexer_peek_token (parser->lexer);
34558 return (token->type == CPP_COMMA
34559 || token->type == CPP_GREATER
34560 || token->type == CPP_ELLIPSIS
34561 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)
34562 /* For better diagnostics, treat >>= like that too, that
34563 shouldn't appear non-nested in template arguments. */
34564 || token->type == CPP_RSHIFT_EQ);
34567 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
34568 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
34570 static bool
34571 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
34572 size_t n)
34574 cp_token *token;
34576 token = cp_lexer_peek_nth_token (parser->lexer, n);
34577 if (token->type == CPP_LESS)
34578 return true;
34579 /* Check for the sequence `<::' in the original code. It would be lexed as
34580 `[:', where `[' is a digraph, and there is no whitespace before
34581 `:'. */
34582 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
34584 cp_token *token2;
34585 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
34586 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
34587 return true;
34589 return false;
34592 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
34593 or none_type otherwise. */
34595 static enum tag_types
34596 cp_parser_token_is_class_key (cp_token* token)
34598 switch (token->keyword)
34600 case RID_CLASS:
34601 return class_type;
34602 case RID_STRUCT:
34603 return record_type;
34604 case RID_UNION:
34605 return union_type;
34607 default:
34608 return none_type;
34612 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
34613 or none_type otherwise or if the token is null. */
34615 static enum tag_types
34616 cp_parser_token_is_type_parameter_key (cp_token* token)
34618 if (!token)
34619 return none_type;
34621 switch (token->keyword)
34623 case RID_CLASS:
34624 return class_type;
34625 case RID_TYPENAME:
34626 return typename_type;
34628 default:
34629 return none_type;
34633 /* Diagnose redundant enum-keys. */
34635 static void
34636 cp_parser_maybe_warn_enum_key (cp_parser *parser, location_t key_loc,
34637 tree type, rid scoped_key)
34639 if (!warn_redundant_tags)
34640 return;
34642 tree type_decl = TYPE_MAIN_DECL (type);
34643 tree name = DECL_NAME (type_decl);
34644 /* Look up the NAME to see if it unambiguously refers to the TYPE. */
34645 push_deferring_access_checks (dk_no_check);
34646 tree decl = cp_parser_lookup_name_simple (parser, name, input_location);
34647 pop_deferring_access_checks ();
34649 /* The enum-key is redundant for uses of the TYPE that are not
34650 declarations and for which name lookup returns just the type
34651 itself. */
34652 if (decl != type_decl)
34653 return;
34655 if (scoped_key != RID_CLASS
34656 && scoped_key != RID_STRUCT
34657 && current_lang_name != lang_name_cplusplus
34658 && current_namespace == global_namespace)
34660 /* Avoid issuing the diagnostic for apparently redundant (unscoped)
34661 enum tag in shared C/C++ code in files (such as headers) included
34662 in the main source file. */
34663 const line_map_ordinary *map = NULL;
34664 linemap_resolve_location (line_table, key_loc,
34665 LRK_MACRO_DEFINITION_LOCATION,
34666 &map);
34667 if (!MAIN_FILE_P (map))
34668 return;
34671 gcc_rich_location richloc (key_loc);
34672 richloc.add_fixit_remove (key_loc);
34673 warning_at (&richloc, OPT_Wredundant_tags,
34674 "redundant enum-key %<enum%s%> in reference to %q#T",
34675 (scoped_key == RID_CLASS ? " class"
34676 : scoped_key == RID_STRUCT ? " struct" : ""), type);
34679 /* Describes the set of declarations of a struct, class, or class template
34680 or its specializations. Used for -Wmismatched-tags. */
34682 class class_decl_loc_t
34684 public:
34686 class_decl_loc_t ()
34687 : locvec (), idxdef (), def_class_key ()
34689 locvec.create (4);
34692 /* Constructs an object for a single declaration of a class with
34693 CLASS_KEY at the current location in the current function (or
34694 at another scope). KEY_REDUNDANT is true if the class-key may
34695 be omitted in the current context without an ambiguity with
34696 another symbol with the same name.
34697 DEF_P is true for a class declaration that is a definition.
34698 CURLOC is the associated location. */
34699 class_decl_loc_t (tag_types class_key, bool key_redundant, bool def_p,
34700 location_t curloc = input_location)
34701 : locvec (), idxdef (def_p ? 0 : UINT_MAX), def_class_key (class_key)
34703 locvec.create (4);
34704 class_key_loc_t ckl (current_function_decl, curloc, class_key,
34705 key_redundant);
34706 locvec.quick_push (ckl);
34709 /* Copy, assign, and destroy the object. Necessary because LOCVEC
34710 isn't safely copyable and assignable and doesn't release storage
34711 on its own. */
34712 class_decl_loc_t (const class_decl_loc_t &rhs)
34713 : locvec (rhs.locvec.copy ()), idxdef (rhs.idxdef),
34714 def_class_key (rhs.def_class_key)
34717 class_decl_loc_t& operator= (const class_decl_loc_t &rhs)
34719 if (this == &rhs)
34720 return *this;
34721 locvec.release ();
34722 locvec = rhs.locvec.copy ();
34723 idxdef = rhs.idxdef;
34724 def_class_key = rhs.def_class_key;
34725 return *this;
34728 ~class_decl_loc_t ()
34730 locvec.release ();
34733 /* Issues -Wmismatched-tags for a single class. */
34734 void diag_mismatched_tags (tree);
34736 /* Issues -Wmismatched-tags for all classes. */
34737 static void diag_mismatched_tags ();
34739 /* Adds TYPE_DECL to the collection of class decls and diagnoses
34740 redundant tags (if -Wredundant-tags is enabled). */
34741 static void add (cp_parser *, location_t, tag_types, tree, bool, bool);
34743 /* Either adds this decl to the collection of class decls
34744 or diagnoses it, whichever is appropriate. */
34745 void add_or_diag_mismatched_tag (tree, tag_types, bool, bool);
34747 private:
34749 tree function (unsigned i) const
34751 return locvec[i].func;
34754 location_t location (unsigned i) const
34756 return locvec[i].loc;
34759 bool key_redundant (unsigned i) const
34761 return locvec[i].key_redundant;
34764 tag_types class_key (unsigned i) const
34766 return locvec[i].class_key;
34769 /* True if a definition for the class has been seen. */
34770 bool def_p () const
34772 return idxdef < locvec.length ();
34775 /* The location of a single mention of a class type with the given
34776 class-key. */
34777 struct class_key_loc_t
34779 class_key_loc_t (tree func, location_t loc, tag_types key, bool redundant)
34780 : func (func), loc (loc), class_key (key), key_redundant (redundant)
34783 /* The function the type is mentioned in. */
34784 tree func;
34785 /* The exact location. */
34786 location_t loc;
34787 /* The class-key used in the mention of the type. */
34788 tag_types class_key;
34789 /* True when the class-key could be omitted at this location
34790 without an ambiguity with another symbol of the same name. */
34791 bool key_redundant;
34793 /* Avoid using auto_vec here since it's not safe to copy due to pr90904. */
34794 vec <class_key_loc_t> locvec;
34795 /* LOCVEC index of the definition or UINT_MAX if none exists. */
34796 unsigned idxdef;
34797 /* The class-key the class was last declared with or none_type when
34798 it has been declared with a mismatched key. */
34799 tag_types def_class_key;
34801 /* A mapping between a TYPE_DECL for a class and the class_decl_loc_t
34802 description above. */
34803 typedef hash_map<tree_decl_hash, class_decl_loc_t> class_to_loc_map_t;
34804 static class_to_loc_map_t class2loc;
34807 class_decl_loc_t::class_to_loc_map_t class_decl_loc_t::class2loc;
34809 /* Issue an error message if the CLASS_KEY does not match the TYPE.
34810 DEF_P is expected to be set for a definition of class TYPE. DECL_P
34811 is set for a declaration of class TYPE and clear for a reference to
34812 it that is not a declaration of it. */
34814 static void
34815 cp_parser_check_class_key (cp_parser *parser, location_t key_loc,
34816 tag_types class_key, tree type, bool def_p,
34817 bool decl_p)
34819 if (type == error_mark_node)
34820 return;
34822 bool seen_as_union = TREE_CODE (type) == UNION_TYPE;
34823 if (seen_as_union != (class_key == union_type))
34825 if (permerror (input_location, "%qs tag used in naming %q#T",
34826 class_key == union_type ? "union"
34827 : class_key == record_type ? "struct" : "class",
34828 type))
34829 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
34830 "%q#T was previously declared here", type);
34831 return;
34834 if (!warn_mismatched_tags && !warn_redundant_tags)
34835 return;
34837 /* Only consider the true class-keys below and ignore typename_type,
34838 etc. that are not C++ class-keys. */
34839 if (class_key != class_type
34840 && class_key != record_type
34841 && class_key != union_type)
34842 return;
34844 class_decl_loc_t::add (parser, key_loc, class_key, type, def_p, decl_p);
34847 /* Returns the template or specialization of one to which the RECORD_TYPE
34848 TYPE corresponds. */
34850 static tree
34851 specialization_of (tree type)
34853 tree ret = type;
34855 /* Determine the template or its partial specialization to which TYPE
34856 corresponds. */
34857 if (tree ti = most_specialized_partial_spec (type, tf_none))
34858 if (ti != error_mark_node)
34859 ret = TREE_TYPE (TI_TEMPLATE (ti));
34861 if (ret == type)
34862 ret = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (type);
34864 return TYPE_MAIN_DECL (ret);
34868 /* Adds the class TYPE to the collection of class decls and diagnoses
34869 redundant tags (if -Wredundant-tags is enabled).
34870 DEF_P is expected to be set for a definition of class TYPE. DECL_P
34871 is set for a (likely, based on syntactic context) declaration of class
34872 TYPE and clear for a reference to it that is not a declaration of it. */
34874 void
34875 class_decl_loc_t::add (cp_parser *parser, location_t key_loc,
34876 tag_types class_key, tree type, bool def_p, bool decl_p)
34878 tree type_decl = TYPE_MAIN_DECL (type);
34879 tree name = DECL_NAME (type_decl);
34880 /* Look up the NAME to see if it unambiguously refers to the TYPE
34881 and set KEY_REDUNDANT if so. */
34882 push_deferring_access_checks (dk_no_check);
34883 tree decl = cp_parser_lookup_name_simple (parser, name, input_location);
34884 pop_deferring_access_checks ();
34886 /* The class-key is redundant for uses of the CLASS_TYPE that are
34887 neither definitions of it nor declarations, and for which name
34888 lookup returns just the type itself. */
34889 bool key_redundant = (!def_p && !decl_p
34890 && (decl == type_decl
34891 || TREE_CODE (decl) == TEMPLATE_DECL
34892 || (CLASS_TYPE_P (type)
34893 && TYPE_BEING_DEFINED (type))));
34895 if (key_redundant
34896 && class_key != class_type
34897 && current_lang_name != lang_name_cplusplus
34898 && current_namespace == global_namespace)
34900 /* Avoid issuing the diagnostic for apparently redundant struct
34901 and union class-keys in shared C/C++ code in files (such as
34902 headers) included in the main source file. */
34903 const line_map_ordinary *map = NULL;
34904 linemap_resolve_location (line_table, key_loc,
34905 LRK_MACRO_DEFINITION_LOCATION,
34906 &map);
34907 if (!MAIN_FILE_P (map))
34908 key_redundant = false;
34911 /* Set if a declaration of TYPE has previously been seen or if it must
34912 exist in a precompiled header. */
34913 bool exist;
34914 class_decl_loc_t *rdl = &class2loc.get_or_insert (type_decl, &exist);
34915 if (!exist)
34917 tree type = TREE_TYPE (type_decl);
34918 if (def_p || !COMPLETE_TYPE_P (type))
34920 /* TYPE_DECL is the first declaration or definition of the type
34921 (outside precompiled headers -- see below). Just create
34922 a new entry for it and return unless it's a declaration
34923 involving a template that may need to be diagnosed by
34924 -Wredundant-tags. */
34925 *rdl = class_decl_loc_t (class_key, false, def_p);
34926 if (TREE_CODE (decl) != TEMPLATE_DECL)
34927 return;
34929 else
34931 /* TYPE was previously defined in some unknown precompiled header.
34932 Simply add a record of its definition at an unknown location and
34933 proceed below to add a reference to it at the current location.
34934 (Declarations in precompiled headers that are not definitions
34935 are ignored.) */
34936 tag_types def_key
34937 = CLASSTYPE_DECLARED_CLASS (type) ? class_type : record_type;
34938 location_t def_loc = DECL_SOURCE_LOCATION (type_decl);
34939 *rdl = class_decl_loc_t (def_key, false, true, def_loc);
34940 exist = true;
34944 /* A prior declaration of TYPE_DECL has been seen. */
34946 if (key_redundant)
34948 gcc_rich_location richloc (key_loc);
34949 richloc.add_fixit_remove (key_loc);
34950 warning_at (&richloc, OPT_Wredundant_tags,
34951 "redundant class-key %qs in reference to %q#T",
34952 class_key == union_type ? "union"
34953 : class_key == record_type ? "struct" : "class",
34954 type);
34957 if (!exist)
34958 /* Do nothing if this is the first declaration of the type. */
34959 return;
34961 if (rdl->idxdef != UINT_MAX && rdl->def_class_key == class_key)
34962 /* Do nothing if the class-key in this declaration matches
34963 the definition. */
34964 return;
34966 rdl->add_or_diag_mismatched_tag (type_decl, class_key, key_redundant,
34967 def_p);
34970 /* Either adds this DECL corresponding to the TYPE_DECL to the collection
34971 of class decls or diagnoses it, whichever is appropriate. */
34973 void
34974 class_decl_loc_t::add_or_diag_mismatched_tag (tree type_decl,
34975 tag_types class_key,
34976 bool redundant,
34977 bool def_p)
34979 /* Reset the CLASS_KEY associated with this type on mismatch.
34980 This is an optimization that lets the diagnostic code skip
34981 over classes that use the same class-key in all declarations. */
34982 if (def_class_key != class_key)
34983 def_class_key = none_type;
34985 /* Set IDXDEF to the index of the vector corresponding to
34986 the definition. */
34987 if (def_p)
34988 idxdef = locvec.length ();
34990 /* Append a record of this declaration to the vector. */
34991 class_key_loc_t ckl (current_function_decl, input_location, class_key,
34992 redundant);
34993 locvec.safe_push (ckl);
34995 if (idxdef == UINT_MAX)
34996 return;
34998 /* As a space optimization diagnose declarations of a class
34999 whose definition has been seen and purge the LOCVEC of
35000 all entries except the definition. */
35001 diag_mismatched_tags (type_decl);
35002 if (idxdef)
35004 class_decl_loc_t::class_key_loc_t ent = locvec[idxdef];
35005 locvec.release ();
35006 locvec.reserve (2);
35007 locvec.safe_push (ent);
35008 idxdef = 0;
35010 else
35011 /* Pop the entry pushed above for this declaration. */
35012 locvec.pop ();
35015 /* Issues -Wmismatched-tags for a single class. */
35017 void
35018 class_decl_loc_t::diag_mismatched_tags (tree type_decl)
35020 if (!warn_mismatched_tags)
35021 return;
35023 /* Number of uses of the class. */
35024 const unsigned ndecls = locvec.length ();
35026 /* The class (or template) declaration guiding the decisions about
35027 the diagnostic. For ordinary classes it's the same as THIS. For
35028 uses of instantiations of templates other than their declarations
35029 it points to the record for the declaration of the corresponding
35030 primary template or partial specialization. */
35031 class_decl_loc_t *cdlguide = this;
35033 tree type = TREE_TYPE (type_decl);
35034 if (CLASS_TYPE_P (type) && CLASSTYPE_IMPLICIT_INSTANTIATION (type))
35036 /* For implicit instantiations of a primary template look up
35037 the primary or partial specialization and use it as
35038 the expected class-key rather than using the class-key of
35039 the first reference to the instantiation. The primary must
35040 be (and inevitably is) at index zero. */
35041 tree spec = specialization_of (type);
35042 cdlguide = class2loc.get (spec);
35043 /* It's possible that we didn't find SPEC. Consider:
35045 template<typename T> struct A {
35046 template<typename U> struct W { };
35048 struct A<int>::W<int> w; // #1
35050 where while parsing A and #1 we've stashed
35051 A<T>
35052 A<T>::W<U>
35053 A<int>::W<int>
35054 into CLASS2LOC. If TYPE is A<int>::W<int>, specialization_of
35055 will yield A<int>::W<U> which may be in CLASS2LOC if we had
35056 an A<int> class specialization, but otherwise won't be in it.
35057 So try to look up A<T>::W<U>. */
35058 if (!cdlguide)
35060 spec = DECL_TEMPLATE_RESULT (most_general_template (spec));
35061 cdlguide = class2loc.get (spec);
35063 /* Now we really should have found something. */
35064 gcc_assert (cdlguide != NULL);
35066 /* Skip declarations that consistently use the same class-key. */
35067 else if (def_class_key != none_type)
35068 return;
35070 /* Set if a definition for the class has been seen. */
35071 const bool def_p = cdlguide->def_p ();
35073 /* The index of the declaration whose class-key this declaration
35074 is expected to match. It's either the class-key of the class
35075 definition if one exists or the first declaration otherwise. */
35076 const unsigned idxguide = def_p ? cdlguide->idxdef : 0;
35078 /* The class-key the class is expected to be declared with: it's
35079 either the key used in its definition or the first declaration
35080 if no definition has been provided.
35081 For implicit instantiations of a primary template it's
35082 the class-key used to declare the primary with. The primary
35083 must be at index zero. */
35084 const tag_types xpect_key = cdlguide->class_key (idxguide);
35086 unsigned idx = 0;
35087 /* Advance IDX to the first declaration that either is not
35088 a definition or that doesn't match the first declaration
35089 if no definition is provided. */
35090 while (class_key (idx) == xpect_key)
35091 if (++idx == ndecls)
35092 return;
35094 /* Save the current function before changing it below. */
35095 tree save_func = current_function_decl;
35096 /* Set the function declaration to print in diagnostic context. */
35097 current_function_decl = function (idx);
35099 const char *xmatchkstr = xpect_key == record_type ? "class" : "struct";
35100 const char *xpectkstr = xpect_key == record_type ? "struct" : "class";
35102 location_t loc = location (idx);
35103 bool key_redundant_p = key_redundant (idx);
35104 auto_diagnostic_group d;
35105 /* Issue a warning for the first mismatched declaration.
35106 Avoid using "%#qT" since the class-key for the same type will
35107 be the same regardless of which one was used in the declaraion. */
35108 if (warning_at (loc, OPT_Wmismatched_tags,
35109 "%qT declared with a mismatched class-key %qs",
35110 type_decl, xmatchkstr))
35112 /* Suggest how to avoid the warning for each instance since
35113 the guidance may be different depending on context. */
35114 inform (loc,
35115 (key_redundant_p
35116 ? G_("remove the class-key or replace it with %qs")
35117 : G_("replace the class-key with %qs")),
35118 xpectkstr);
35120 /* Also point to the first declaration or definition that guided
35121 the decision to issue the warning above. */
35122 inform (cdlguide->location (idxguide),
35123 (def_p
35124 ? G_("%qT defined as %qs here")
35125 : G_("%qT first declared as %qs here")),
35126 type_decl, xpectkstr);
35129 /* Issue warnings for the remaining inconsistent declarations. */
35130 for (unsigned i = idx + 1; i != ndecls; ++i)
35132 tag_types clskey = class_key (i);
35133 /* Skip over the declarations that match either the definition
35134 if one was provided or the first declaration. */
35135 if (clskey == xpect_key)
35136 continue;
35138 loc = location (i);
35139 key_redundant_p = key_redundant (i);
35140 /* Set the function declaration to print in diagnostic context. */
35141 current_function_decl = function (i);
35142 if (warning_at (loc, OPT_Wmismatched_tags,
35143 "%qT declared with a mismatched class-key %qs",
35144 type_decl, xmatchkstr))
35145 /* Suggest how to avoid the warning for each instance since
35146 the guidance may be different depending on context. */
35147 inform (loc,
35148 (key_redundant_p
35149 ? G_("remove the class-key or replace it with %qs")
35150 : G_("replace the class-key with %qs")),
35151 xpectkstr);
35154 /* Restore the current function in case it was replaced above. */
35155 current_function_decl = save_func;
35158 /* Issues -Wmismatched-tags for all classes. Called at the end
35159 of processing a translation unit, after declarations of all class
35160 types and their uses have been recorded. */
35162 void
35163 class_decl_loc_t::diag_mismatched_tags ()
35165 /* CLASS2LOC should be empty if both -Wmismatched-tags and
35166 -Wredundant-tags are disabled. */
35167 gcc_assert (warn_mismatched_tags
35168 || warn_redundant_tags
35169 || class2loc.is_empty ());
35171 /* Save the current function before changing on return. It should
35172 be null at this point. */
35173 temp_override<tree> cleanup (current_function_decl);
35175 if (warn_mismatched_tags)
35177 /* Iterate over the collected class/struct/template declarations. */
35178 typedef class_to_loc_map_t::iterator iter_t;
35179 for (iter_t it = class2loc.begin (); it != class2loc.end (); ++it)
35181 tree type_decl = (*it).first;
35182 class_decl_loc_t &recloc = (*it).second;
35183 recloc.diag_mismatched_tags (type_decl);
35187 class2loc.empty ();
35190 /* Issue an error message if DECL is redeclared with different
35191 access than its original declaration [class.access.spec/3].
35192 This applies to nested classes, nested class templates and
35193 enumerations [class.mem/1]. */
35195 static void
35196 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
35198 if (!decl
35199 || (!CLASS_TYPE_P (TREE_TYPE (decl))
35200 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
35201 return;
35203 if ((TREE_PRIVATE (decl)
35204 != (current_access_specifier == access_private_node))
35205 || (TREE_PROTECTED (decl)
35206 != (current_access_specifier == access_protected_node)))
35207 error_at (location, "%qD redeclared with different access", decl);
35210 /* Look for the `template' keyword, as a syntactic disambiguator.
35211 Return TRUE iff it is present, in which case it will be
35212 consumed. */
35214 static bool
35215 cp_parser_optional_template_keyword (cp_parser *parser)
35217 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
35219 /* In C++98 the `template' keyword can only be used within templates;
35220 outside templates the parser can always figure out what is a
35221 template and what is not. In C++11, per the resolution of DR 468,
35222 `template' is allowed in cases where it is not strictly necessary. */
35223 if (!processing_template_decl
35224 && pedantic && cxx_dialect == cxx98)
35226 cp_token *token = cp_lexer_peek_token (parser->lexer);
35227 pedwarn (token->location, OPT_Wpedantic,
35228 "in C++98 %<template%> (as a disambiguator) is only "
35229 "allowed within templates");
35230 /* If this part of the token stream is rescanned, the same
35231 error message would be generated. So, we purge the token
35232 from the stream. */
35233 cp_lexer_purge_token (parser->lexer);
35234 return false;
35236 else
35238 /* Consume the `template' keyword. */
35239 cp_lexer_consume_token (parser->lexer);
35240 return true;
35243 return false;
35246 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
35247 set PARSER->SCOPE, and perform other related actions. */
35249 static void
35250 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
35252 struct tree_check *check_value;
35254 /* Get the stored value. */
35255 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
35256 /* Set the scope from the stored value. */
35257 parser->scope = saved_checks_value (check_value);
35258 parser->qualifying_scope = check_value->qualifying_scope;
35259 parser->object_scope = parser->context->object_type;
35260 parser->context->object_type = NULL_TREE;
35263 /* Consume tokens up through a non-nested END token. Returns TRUE if we
35264 encounter the end of a block before what we were looking for. */
35266 static bool
35267 cp_parser_cache_group (cp_parser *parser,
35268 enum cpp_ttype end,
35269 unsigned depth)
35271 while (true)
35273 cp_token *token = cp_lexer_peek_token (parser->lexer);
35275 /* Abort a parenthesized expression if we encounter a semicolon. */
35276 if ((end == CPP_CLOSE_PAREN || depth == 0)
35277 && token->type == CPP_SEMICOLON)
35278 return true;
35279 /* If we've reached the end of the file, stop. */
35280 if (token->type == CPP_EOF
35281 || (end != CPP_PRAGMA_EOL
35282 && token->type == CPP_PRAGMA_EOL))
35283 return true;
35284 if (token->type == CPP_CLOSE_BRACE && depth == 0)
35285 /* We've hit the end of an enclosing block, so there's been some
35286 kind of syntax error. */
35287 return true;
35289 /* Consume the token. */
35290 cp_lexer_consume_token (parser->lexer);
35291 /* See if it starts a new group. */
35292 if (token->type == CPP_OPEN_BRACE)
35294 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
35295 /* In theory this should probably check end == '}', but
35296 cp_parser_save_member_function_body needs it to exit
35297 after either '}' or ')' when called with ')'. */
35298 if (depth == 0)
35299 return false;
35301 else if (token->type == CPP_OPEN_PAREN)
35303 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
35304 if (depth == 0 && end == CPP_CLOSE_PAREN)
35305 return false;
35307 else if (token->type == CPP_PRAGMA)
35308 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
35309 else if (token->type == end)
35310 return false;
35314 /* Like above, for caching a default argument or NSDMI. Both of these are
35315 terminated by a non-nested comma, but it can be unclear whether or not a
35316 comma is nested in a template argument list unless we do more parsing.
35317 In order to handle this ambiguity, when we encounter a ',' after a '<'
35318 we try to parse what follows as a parameter-declaration-list (in the
35319 case of a default argument) or a member-declarator (in the case of an
35320 NSDMI). If that succeeds, then we stop caching. */
35322 static tree
35323 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
35325 unsigned depth = 0;
35326 int maybe_template_id = 0;
35327 cp_token *first_token;
35328 cp_token *token;
35329 tree default_argument;
35331 /* Add tokens until we have processed the entire default
35332 argument. We add the range [first_token, token). */
35333 first_token = cp_lexer_peek_token (parser->lexer);
35334 if (first_token->type == CPP_OPEN_BRACE)
35336 /* For list-initialization, this is straightforward. */
35337 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
35338 token = cp_lexer_peek_token (parser->lexer);
35340 else while (true)
35342 bool done = false;
35344 /* Peek at the next token. */
35345 token = cp_lexer_peek_token (parser->lexer);
35346 /* What we do depends on what token we have. */
35347 switch (token->type)
35349 /* In valid code, a default argument must be
35350 immediately followed by a `,' `)', or `...'. */
35351 case CPP_COMMA:
35352 if (depth == 0 && maybe_template_id)
35354 /* If we've seen a '<', we might be in a
35355 template-argument-list. Until Core issue 325 is
35356 resolved, we don't know how this situation ought
35357 to be handled, so try to DTRT. We check whether
35358 what comes after the comma is a valid parameter
35359 declaration list. If it is, then the comma ends
35360 the default argument; otherwise the default
35361 argument continues. */
35362 bool error = false;
35363 cp_token *peek;
35365 /* Set ITALP so cp_parser_parameter_declaration_list
35366 doesn't decide to commit to this parse. */
35367 bool saved_italp = parser->in_template_argument_list_p;
35368 parser->in_template_argument_list_p = true;
35370 cp_parser_parse_tentatively (parser);
35372 if (nsdmi)
35374 /* Parse declarators until we reach a non-comma or
35375 somthing that cannot be an initializer.
35376 Just checking whether we're looking at a single
35377 declarator is insufficient. Consider:
35378 int var = tuple<T,U>::x;
35379 The template parameter 'U' looks exactly like a
35380 declarator. */
35383 int ctor_dtor_or_conv_p;
35384 cp_lexer_consume_token (parser->lexer);
35385 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
35386 CP_PARSER_FLAGS_NONE,
35387 &ctor_dtor_or_conv_p,
35388 /*parenthesized_p=*/NULL,
35389 /*member_p=*/true,
35390 /*friend_p=*/false,
35391 /*static_p=*/false);
35392 peek = cp_lexer_peek_token (parser->lexer);
35393 if (cp_parser_error_occurred (parser))
35394 break;
35396 while (peek->type == CPP_COMMA);
35397 /* If we met an '=' or ';' then the original comma
35398 was the end of the NSDMI. Otherwise assume
35399 we're still in the NSDMI. */
35400 error = (peek->type != CPP_EQ
35401 && peek->type != CPP_SEMICOLON);
35403 else
35405 cp_lexer_consume_token (parser->lexer);
35406 begin_scope (sk_function_parms, NULL_TREE);
35407 tree t = cp_parser_parameter_declaration_list
35408 (parser, CP_PARSER_FLAGS_NONE,
35409 /*pending_decls*/nullptr);
35410 if (t == error_mark_node)
35411 error = true;
35412 pop_bindings_and_leave_scope ();
35414 if (!cp_parser_error_occurred (parser) && !error)
35415 done = true;
35416 cp_parser_abort_tentative_parse (parser);
35418 parser->in_template_argument_list_p = saved_italp;
35419 break;
35421 /* FALLTHRU */
35422 case CPP_CLOSE_PAREN:
35423 case CPP_ELLIPSIS:
35424 /* If we run into a non-nested `;', `}', or `]',
35425 then the code is invalid -- but the default
35426 argument is certainly over. */
35427 case CPP_SEMICOLON:
35428 case CPP_CLOSE_BRACE:
35429 case CPP_CLOSE_SQUARE:
35430 if (depth == 0
35431 /* Handle correctly int n = sizeof ... ( p ); */
35432 && token->type != CPP_ELLIPSIS)
35433 done = true;
35434 /* Update DEPTH, if necessary. */
35435 else if (token->type == CPP_CLOSE_PAREN
35436 || token->type == CPP_CLOSE_BRACE
35437 || token->type == CPP_CLOSE_SQUARE)
35438 --depth;
35439 break;
35441 case CPP_OPEN_PAREN:
35442 case CPP_OPEN_SQUARE:
35443 case CPP_OPEN_BRACE:
35444 ++depth;
35445 break;
35447 case CPP_LESS:
35448 if (depth == 0)
35449 /* This might be the comparison operator, or it might
35450 start a template argument list. */
35451 ++maybe_template_id;
35452 break;
35454 case CPP_RSHIFT:
35455 if (cxx_dialect == cxx98)
35456 break;
35457 /* Fall through for C++0x, which treats the `>>'
35458 operator like two `>' tokens in certain
35459 cases. */
35460 gcc_fallthrough ();
35462 case CPP_GREATER:
35463 if (depth == 0)
35465 /* This might be an operator, or it might close a
35466 template argument list. But if a previous '<'
35467 started a template argument list, this will have
35468 closed it, so we can't be in one anymore. */
35469 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
35470 if (maybe_template_id < 0)
35471 maybe_template_id = 0;
35473 break;
35475 /* If we run out of tokens, issue an error message. */
35476 case CPP_EOF:
35477 case CPP_PRAGMA_EOL:
35478 error_at (token->location, "file ends in default argument");
35479 return error_mark_node;
35481 case CPP_NAME:
35482 case CPP_SCOPE:
35483 /* In these cases, we should look for template-ids.
35484 For example, if the default argument is
35485 `X<int, double>()', we need to do name lookup to
35486 figure out whether or not `X' is a template; if
35487 so, the `,' does not end the default argument.
35489 That is not yet done. */
35490 break;
35492 default:
35493 break;
35496 /* If we've reached the end, stop. */
35497 if (done)
35498 break;
35500 /* Add the token to the token block. */
35501 token = cp_lexer_consume_token (parser->lexer);
35504 /* Create a DEFERRED_PARSE to represent the unparsed default
35505 argument. */
35506 default_argument = make_node (DEFERRED_PARSE);
35507 DEFPARSE_TOKENS (default_argument)
35508 = cp_token_cache_new (first_token, token);
35509 DEFPARSE_INSTANTIATIONS (default_argument) = NULL;
35511 return default_argument;
35514 /* A location to use for diagnostics about an unparsed DEFERRED_PARSE. */
35516 location_t
35517 defparse_location (tree default_argument)
35519 cp_token_cache *tokens = DEFPARSE_TOKENS (default_argument);
35520 location_t start = tokens->first->location;
35521 location_t end = tokens->last->location;
35522 return make_location (start, start, end);
35525 /* Begin parsing tentatively. We always save tokens while parsing
35526 tentatively so that if the tentative parsing fails we can restore the
35527 tokens. */
35529 static void
35530 cp_parser_parse_tentatively (cp_parser* parser)
35532 /* Enter a new parsing context. */
35533 parser->context = cp_parser_context_new (parser->context);
35534 /* Begin saving tokens. */
35535 cp_lexer_save_tokens (parser->lexer);
35536 /* In order to avoid repetitive access control error messages,
35537 access checks are queued up until we are no longer parsing
35538 tentatively. */
35539 push_deferring_access_checks (dk_deferred);
35542 /* Commit to the currently active tentative parse. */
35544 static void
35545 cp_parser_commit_to_tentative_parse (cp_parser* parser)
35547 cp_parser_context *context;
35548 cp_lexer *lexer;
35550 /* Mark all of the levels as committed. */
35551 lexer = parser->lexer;
35552 for (context = parser->context; context->next; context = context->next)
35554 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
35555 break;
35556 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
35557 while (!cp_lexer_saving_tokens (lexer))
35558 lexer = lexer->next;
35559 cp_lexer_commit_tokens (lexer);
35563 /* Commit to the topmost currently active tentative parse.
35565 Note that this function shouldn't be called when there are
35566 irreversible side-effects while in a tentative state. For
35567 example, we shouldn't create a permanent entry in the symbol
35568 table, or issue an error message that might not apply if the
35569 tentative parse is aborted. */
35571 static void
35572 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
35574 cp_parser_context *context = parser->context;
35575 cp_lexer *lexer = parser->lexer;
35577 if (context)
35579 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
35580 return;
35581 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
35583 while (!cp_lexer_saving_tokens (lexer))
35584 lexer = lexer->next;
35585 cp_lexer_commit_tokens (lexer);
35589 /* Abort the currently active tentative parse. All consumed tokens
35590 will be rolled back, and no diagnostics will be issued. */
35592 static void
35593 cp_parser_abort_tentative_parse (cp_parser* parser)
35595 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
35596 || errorcount > 0);
35597 cp_parser_simulate_error (parser);
35598 /* Now, pretend that we want to see if the construct was
35599 successfully parsed. */
35600 cp_parser_parse_definitely (parser);
35603 /* Stop parsing tentatively. If a parse error has occurred, restore the
35604 token stream. Otherwise, commit to the tokens we have consumed.
35605 Returns true if no error occurred; false otherwise. */
35607 static bool
35608 cp_parser_parse_definitely (cp_parser* parser)
35610 bool error_occurred;
35611 cp_parser_context *context;
35613 /* Remember whether or not an error occurred, since we are about to
35614 destroy that information. */
35615 error_occurred = cp_parser_error_occurred (parser);
35616 /* Remove the topmost context from the stack. */
35617 context = parser->context;
35618 parser->context = context->next;
35619 /* If no parse errors occurred, commit to the tentative parse. */
35620 if (!error_occurred)
35622 /* Commit to the tokens read tentatively, unless that was
35623 already done. */
35624 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
35625 cp_lexer_commit_tokens (parser->lexer);
35627 pop_to_parent_deferring_access_checks ();
35629 /* Otherwise, if errors occurred, roll back our state so that things
35630 are just as they were before we began the tentative parse. */
35631 else
35633 cp_lexer_rollback_tokens (parser->lexer);
35634 pop_deferring_access_checks ();
35636 /* Add the context to the front of the free list. */
35637 context->next = cp_parser_context_free_list;
35638 cp_parser_context_free_list = context;
35640 return !error_occurred;
35643 /* Returns true if we are parsing tentatively and are not committed to
35644 this tentative parse. */
35646 static bool
35647 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
35649 return (cp_parser_parsing_tentatively (parser)
35650 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
35653 /* Returns nonzero iff an error has occurred during the most recent
35654 tentative parse. */
35656 static bool
35657 cp_parser_error_occurred (cp_parser* parser)
35659 return (cp_parser_parsing_tentatively (parser)
35660 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
35663 /* Returns nonzero if GNU extensions are allowed. */
35665 static bool
35666 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
35668 return parser->allow_gnu_extensions_p;
35671 /* Objective-C++ Productions */
35674 /* Parse an Objective-C expression, which feeds into a primary-expression
35675 above.
35677 objc-expression:
35678 objc-message-expression
35679 objc-string-literal
35680 objc-encode-expression
35681 objc-protocol-expression
35682 objc-selector-expression
35684 Returns a tree representation of the expression. */
35686 static cp_expr
35687 cp_parser_objc_expression (cp_parser* parser)
35689 /* Try to figure out what kind of declaration is present. */
35690 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
35692 switch (kwd->type)
35694 case CPP_OPEN_SQUARE:
35695 return cp_parser_objc_message_expression (parser);
35697 case CPP_OBJC_STRING:
35698 kwd = cp_lexer_consume_token (parser->lexer);
35699 return objc_build_string_object (kwd->u.value);
35701 case CPP_KEYWORD:
35702 switch (kwd->keyword)
35704 case RID_AT_ENCODE:
35705 return cp_parser_objc_encode_expression (parser);
35707 case RID_AT_PROTOCOL:
35708 return cp_parser_objc_protocol_expression (parser);
35710 case RID_AT_SELECTOR:
35711 return cp_parser_objc_selector_expression (parser);
35713 default:
35714 break;
35716 /* FALLTHRU */
35717 default:
35718 error_at (kwd->location,
35719 "misplaced %<@%D%> Objective-C++ construct",
35720 kwd->u.value);
35721 cp_parser_skip_to_end_of_block_or_statement (parser);
35724 return error_mark_node;
35727 /* Parse an Objective-C message expression.
35729 objc-message-expression:
35730 [ objc-message-receiver objc-message-args ]
35732 Returns a representation of an Objective-C message. */
35734 static tree
35735 cp_parser_objc_message_expression (cp_parser* parser)
35737 tree receiver, messageargs;
35739 parser->objective_c_message_context_p = true;
35740 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
35741 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
35742 receiver = cp_parser_objc_message_receiver (parser);
35743 messageargs = cp_parser_objc_message_args (parser);
35744 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
35745 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
35747 tree result = objc_build_message_expr (receiver, messageargs);
35749 /* Construct a location e.g.
35750 [self func1:5]
35751 ^~~~~~~~~~~~~~
35752 ranging from the '[' to the ']', with the caret at the start. */
35753 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
35754 protected_set_expr_location (result, combined_loc);
35756 parser->objective_c_message_context_p = false;
35757 return result;
35760 /* Parse an objc-message-receiver.
35762 objc-message-receiver:
35763 expression
35764 simple-type-specifier
35766 Returns a representation of the type or expression. */
35768 static tree
35769 cp_parser_objc_message_receiver (cp_parser* parser)
35771 tree rcv;
35773 /* An Objective-C message receiver may be either (1) a type
35774 or (2) an expression. */
35775 cp_parser_parse_tentatively (parser);
35776 rcv = cp_parser_expression (parser);
35778 /* If that worked out, fine. */
35779 if (cp_parser_parse_definitely (parser))
35780 return rcv;
35782 cp_parser_parse_tentatively (parser);
35783 rcv = cp_parser_simple_type_specifier (parser,
35784 /*decl_specs=*/NULL,
35785 CP_PARSER_FLAGS_NONE);
35787 if (cp_parser_parse_definitely (parser))
35788 return objc_get_class_reference (rcv);
35790 cp_parser_error (parser, "objective-c++ message receiver expected");
35791 return error_mark_node;
35794 /* Parse the arguments and selectors comprising an Objective-C message.
35796 objc-message-args:
35797 objc-selector
35798 objc-selector-args
35799 objc-selector-args , objc-comma-args
35801 objc-selector-args:
35802 objc-selector [opt] : assignment-expression
35803 objc-selector-args objc-selector [opt] : assignment-expression
35805 objc-comma-args:
35806 assignment-expression
35807 objc-comma-args , assignment-expression
35809 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
35810 selector arguments and TREE_VALUE containing a list of comma
35811 arguments. */
35813 static tree
35814 cp_parser_objc_message_args (cp_parser* parser)
35816 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
35817 bool maybe_unary_selector_p = true;
35818 cp_token *token = cp_lexer_peek_token (parser->lexer);
35820 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
35822 tree selector = NULL_TREE, arg;
35824 if (token->type != CPP_COLON)
35825 selector = cp_parser_objc_selector (parser);
35827 /* Detect if we have a unary selector. */
35828 if (maybe_unary_selector_p
35829 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
35830 return build_tree_list (selector, NULL_TREE);
35832 maybe_unary_selector_p = false;
35833 cp_parser_require (parser, CPP_COLON, RT_COLON);
35834 arg = cp_parser_assignment_expression (parser);
35836 sel_args
35837 = chainon (sel_args,
35838 build_tree_list (selector, arg));
35840 token = cp_lexer_peek_token (parser->lexer);
35843 /* Handle non-selector arguments, if any. */
35844 while (token->type == CPP_COMMA)
35846 tree arg;
35848 cp_lexer_consume_token (parser->lexer);
35849 arg = cp_parser_assignment_expression (parser);
35851 addl_args
35852 = chainon (addl_args,
35853 build_tree_list (NULL_TREE, arg));
35855 token = cp_lexer_peek_token (parser->lexer);
35858 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
35860 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
35861 return build_tree_list (error_mark_node, error_mark_node);
35864 return build_tree_list (sel_args, addl_args);
35867 /* Parse an Objective-C encode expression.
35869 objc-encode-expression:
35870 @encode objc-typename
35872 Returns an encoded representation of the type argument. */
35874 static cp_expr
35875 cp_parser_objc_encode_expression (cp_parser* parser)
35877 tree type;
35878 cp_token *token;
35879 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
35881 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
35882 matching_parens parens;
35883 parens.require_open (parser);
35884 token = cp_lexer_peek_token (parser->lexer);
35885 type = complete_type (cp_parser_type_id (parser));
35886 parens.require_close (parser);
35888 if (!type)
35890 error_at (token->location,
35891 "%<@encode%> must specify a type as an argument");
35892 return error_mark_node;
35895 /* This happens if we find @encode(T) (where T is a template
35896 typename or something dependent on a template typename) when
35897 parsing a template. In that case, we can't compile it
35898 immediately, but we rather create an AT_ENCODE_EXPR which will
35899 need to be instantiated when the template is used.
35901 if (dependent_type_p (type))
35903 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
35904 TREE_READONLY (value) = 1;
35905 return value;
35909 /* Build a location of the form:
35910 @encode(int)
35911 ^~~~~~~~~~~~
35912 with caret==start at the @ token, finishing at the close paren. */
35913 location_t combined_loc = make_location (start_loc, start_loc, parser->lexer);
35915 return cp_expr (objc_build_encode_expr (type), combined_loc);
35918 /* Parse an Objective-C @defs expression. */
35920 static tree
35921 cp_parser_objc_defs_expression (cp_parser *parser)
35923 tree name;
35925 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
35926 matching_parens parens;
35927 parens.require_open (parser);
35928 name = cp_parser_identifier (parser);
35929 parens.require_close (parser);
35931 return objc_get_class_ivars (name);
35934 /* Parse an Objective-C protocol expression.
35936 objc-protocol-expression:
35937 @protocol ( identifier )
35939 Returns a representation of the protocol expression. */
35941 static tree
35942 cp_parser_objc_protocol_expression (cp_parser* parser)
35944 tree proto;
35945 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
35947 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
35948 matching_parens parens;
35949 parens.require_open (parser);
35950 proto = cp_parser_identifier (parser);
35951 parens.require_close (parser);
35953 /* Build a location of the form:
35954 @protocol(prot)
35955 ^~~~~~~~~~~~~~~
35956 with caret==start at the @ token, finishing at the close paren. */
35957 location_t combined_loc = make_location (start_loc, start_loc, parser->lexer);
35958 tree result = objc_build_protocol_expr (proto);
35959 protected_set_expr_location (result, combined_loc);
35960 return result;
35963 /* Parse an Objective-C selector expression.
35965 objc-selector-expression:
35966 @selector ( objc-method-signature )
35968 objc-method-signature:
35969 objc-selector
35970 objc-selector-seq
35972 objc-selector-seq:
35973 objc-selector :
35974 objc-selector-seq objc-selector :
35976 Returns a representation of the method selector. */
35978 static tree
35979 cp_parser_objc_selector_expression (cp_parser* parser)
35981 tree sel_seq = NULL_TREE;
35982 bool maybe_unary_selector_p = true;
35983 cp_token *token;
35984 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35986 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
35987 matching_parens parens;
35988 parens.require_open (parser);
35989 token = cp_lexer_peek_token (parser->lexer);
35991 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
35992 || token->type == CPP_SCOPE)
35994 tree selector = NULL_TREE;
35996 if (token->type != CPP_COLON
35997 || token->type == CPP_SCOPE)
35998 selector = cp_parser_objc_selector (parser);
36000 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
36001 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
36003 /* Detect if we have a unary selector. */
36004 if (maybe_unary_selector_p)
36006 sel_seq = selector;
36007 goto finish_selector;
36009 else
36011 cp_parser_error (parser, "expected %<:%>");
36014 maybe_unary_selector_p = false;
36015 token = cp_lexer_consume_token (parser->lexer);
36017 if (token->type == CPP_SCOPE)
36019 sel_seq
36020 = chainon (sel_seq,
36021 build_tree_list (selector, NULL_TREE));
36022 sel_seq
36023 = chainon (sel_seq,
36024 build_tree_list (NULL_TREE, NULL_TREE));
36026 else
36027 sel_seq
36028 = chainon (sel_seq,
36029 build_tree_list (selector, NULL_TREE));
36031 token = cp_lexer_peek_token (parser->lexer);
36034 finish_selector:
36035 parens.require_close (parser);
36038 /* Build a location of the form:
36039 @selector(func)
36040 ^~~~~~~~~~~~~~~
36041 with caret==start at the @ token, finishing at the close paren. */
36042 location_t combined_loc = make_location (loc, loc, parser->lexer);
36043 tree result = objc_build_selector_expr (combined_loc, sel_seq);
36044 /* TODO: objc_build_selector_expr doesn't always honor the location. */
36045 protected_set_expr_location (result, combined_loc);
36046 return result;
36049 /* Parse a list of identifiers.
36051 objc-identifier-list:
36052 identifier
36053 objc-identifier-list , identifier
36055 Returns a TREE_LIST of identifier nodes. */
36057 static tree
36058 cp_parser_objc_identifier_list (cp_parser* parser)
36060 tree identifier;
36061 tree list;
36062 cp_token *sep;
36064 identifier = cp_parser_identifier (parser);
36065 if (identifier == error_mark_node)
36066 return error_mark_node;
36068 list = build_tree_list (NULL_TREE, identifier);
36069 sep = cp_lexer_peek_token (parser->lexer);
36071 while (sep->type == CPP_COMMA)
36073 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
36074 identifier = cp_parser_identifier (parser);
36075 if (identifier == error_mark_node)
36076 return list;
36078 list = chainon (list, build_tree_list (NULL_TREE,
36079 identifier));
36080 sep = cp_lexer_peek_token (parser->lexer);
36083 return list;
36086 /* Parse an Objective-C alias declaration.
36088 objc-alias-declaration:
36089 @compatibility_alias identifier identifier ;
36091 This function registers the alias mapping with the Objective-C front end.
36092 It returns nothing. */
36094 static void
36095 cp_parser_objc_alias_declaration (cp_parser* parser)
36097 tree alias, orig;
36099 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
36100 alias = cp_parser_identifier (parser);
36101 orig = cp_parser_identifier (parser);
36102 objc_declare_alias (alias, orig);
36103 cp_parser_consume_semicolon_at_end_of_statement (parser);
36106 /* Parse an Objective-C class forward-declaration.
36108 objc-class-declaration:
36109 @class objc-identifier-list ;
36111 The function registers the forward declarations with the Objective-C
36112 front end. It returns nothing. */
36114 static void
36115 cp_parser_objc_class_declaration (cp_parser* parser)
36117 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
36118 while (true)
36120 tree id;
36122 id = cp_parser_identifier (parser);
36123 if (id == error_mark_node)
36124 break;
36126 objc_declare_class (id);
36128 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36129 cp_lexer_consume_token (parser->lexer);
36130 else
36131 break;
36133 cp_parser_consume_semicolon_at_end_of_statement (parser);
36136 /* Parse a list of Objective-C protocol references.
36138 objc-protocol-refs-opt:
36139 objc-protocol-refs [opt]
36141 objc-protocol-refs:
36142 < objc-identifier-list >
36144 Returns a TREE_LIST of identifiers, if any. */
36146 static tree
36147 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
36149 tree protorefs = NULL_TREE;
36151 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
36153 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
36154 protorefs = cp_parser_objc_identifier_list (parser);
36155 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
36158 return protorefs;
36161 /* Parse a Objective-C visibility specification. */
36163 static void
36164 cp_parser_objc_visibility_spec (cp_parser* parser)
36166 cp_token *vis = cp_lexer_peek_token (parser->lexer);
36168 switch (vis->keyword)
36170 case RID_AT_PRIVATE:
36171 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
36172 break;
36173 case RID_AT_PROTECTED:
36174 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
36175 break;
36176 case RID_AT_PUBLIC:
36177 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
36178 break;
36179 case RID_AT_PACKAGE:
36180 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
36181 break;
36182 default:
36183 return;
36186 /* Eat '@private'/'@protected'/'@public'. */
36187 cp_lexer_consume_token (parser->lexer);
36190 /* Parse an Objective-C method type. Return 'true' if it is a class
36191 (+) method, and 'false' if it is an instance (-) method. */
36193 static inline bool
36194 cp_parser_objc_method_type (cp_parser* parser)
36196 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
36197 return true;
36198 else
36199 return false;
36202 /* Parse an Objective-C protocol qualifier. */
36204 static tree
36205 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
36207 tree quals = NULL_TREE, node;
36208 cp_token *token = cp_lexer_peek_token (parser->lexer);
36210 node = token->u.value;
36212 while (node && identifier_p (node)
36213 && (node == ridpointers [(int) RID_IN]
36214 || node == ridpointers [(int) RID_OUT]
36215 || node == ridpointers [(int) RID_INOUT]
36216 || node == ridpointers [(int) RID_BYCOPY]
36217 || node == ridpointers [(int) RID_BYREF]
36218 || node == ridpointers [(int) RID_ONEWAY]))
36220 quals = tree_cons (NULL_TREE, node, quals);
36221 cp_lexer_consume_token (parser->lexer);
36222 token = cp_lexer_peek_token (parser->lexer);
36223 node = token->u.value;
36226 return quals;
36229 /* Parse an Objective-C typename. */
36231 static tree
36232 cp_parser_objc_typename (cp_parser* parser)
36234 tree type_name = NULL_TREE;
36236 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36238 tree proto_quals, cp_type = NULL_TREE;
36240 matching_parens parens;
36241 parens.consume_open (parser); /* Eat '('. */
36242 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
36244 /* An ObjC type name may consist of just protocol qualifiers, in which
36245 case the type shall default to 'id'. */
36246 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
36248 cp_type = cp_parser_type_id (parser);
36250 /* If the type could not be parsed, an error has already
36251 been produced. For error recovery, behave as if it had
36252 not been specified, which will use the default type
36253 'id'. */
36254 if (cp_type == error_mark_node)
36256 cp_type = NULL_TREE;
36257 /* We need to skip to the closing parenthesis as
36258 cp_parser_type_id() does not seem to do it for
36259 us. */
36260 cp_parser_skip_to_closing_parenthesis (parser,
36261 /*recovering=*/true,
36262 /*or_comma=*/false,
36263 /*consume_paren=*/false);
36267 parens.require_close (parser);
36268 type_name = build_tree_list (proto_quals, cp_type);
36271 return type_name;
36274 /* Check to see if TYPE refers to an Objective-C selector name. */
36276 static bool
36277 cp_parser_objc_selector_p (enum cpp_ttype type)
36279 return (type == CPP_NAME || type == CPP_KEYWORD
36280 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
36281 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
36282 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
36283 || type == CPP_XOR || type == CPP_XOR_EQ);
36286 /* Parse an Objective-C selector. */
36288 static tree
36289 cp_parser_objc_selector (cp_parser* parser)
36291 cp_token *token = cp_lexer_consume_token (parser->lexer);
36293 if (!cp_parser_objc_selector_p (token->type))
36295 error_at (token->location, "invalid Objective-C++ selector name");
36296 return error_mark_node;
36299 /* C++ operator names are allowed to appear in ObjC selectors. */
36300 switch (token->type)
36302 case CPP_AND_AND: return get_identifier ("and");
36303 case CPP_AND_EQ: return get_identifier ("and_eq");
36304 case CPP_AND: return get_identifier ("bitand");
36305 case CPP_OR: return get_identifier ("bitor");
36306 case CPP_COMPL: return get_identifier ("compl");
36307 case CPP_NOT: return get_identifier ("not");
36308 case CPP_NOT_EQ: return get_identifier ("not_eq");
36309 case CPP_OR_OR: return get_identifier ("or");
36310 case CPP_OR_EQ: return get_identifier ("or_eq");
36311 case CPP_XOR: return get_identifier ("xor");
36312 case CPP_XOR_EQ: return get_identifier ("xor_eq");
36313 default: return token->u.value;
36317 /* Parse an Objective-C params list. */
36319 static tree
36320 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
36322 tree params = NULL_TREE;
36323 bool maybe_unary_selector_p = true;
36324 cp_token *token = cp_lexer_peek_token (parser->lexer);
36326 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
36328 tree selector = NULL_TREE, type_name, identifier;
36329 tree parm_attr = NULL_TREE;
36331 if (token->keyword == RID_ATTRIBUTE)
36332 break;
36334 if (token->type != CPP_COLON)
36335 selector = cp_parser_objc_selector (parser);
36337 /* Detect if we have a unary selector. */
36338 if (maybe_unary_selector_p
36339 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
36341 params = selector; /* Might be followed by attributes. */
36342 break;
36345 maybe_unary_selector_p = false;
36346 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
36348 /* Something went quite wrong. There should be a colon
36349 here, but there is not. Stop parsing parameters. */
36350 break;
36352 type_name = cp_parser_objc_typename (parser);
36353 /* New ObjC allows attributes on parameters too. */
36354 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
36355 parm_attr = cp_parser_attributes_opt (parser);
36356 identifier = cp_parser_identifier (parser);
36358 params
36359 = chainon (params,
36360 objc_build_keyword_decl (selector,
36361 type_name,
36362 identifier,
36363 parm_attr));
36365 token = cp_lexer_peek_token (parser->lexer);
36368 if (params == NULL_TREE)
36370 cp_parser_error (parser, "objective-c++ method declaration is expected");
36371 return error_mark_node;
36374 /* We allow tail attributes for the method. */
36375 if (token->keyword == RID_ATTRIBUTE)
36377 *attributes = cp_parser_attributes_opt (parser);
36378 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
36379 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
36380 return params;
36381 cp_parser_error (parser,
36382 "method attributes must be specified at the end");
36383 return error_mark_node;
36386 if (params == NULL_TREE)
36388 cp_parser_error (parser, "objective-c++ method declaration is expected");
36389 return error_mark_node;
36391 return params;
36394 /* Parse the non-keyword Objective-C params. */
36396 static tree
36397 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
36398 tree* attributes)
36400 tree params = make_node (TREE_LIST);
36401 cp_token *token = cp_lexer_peek_token (parser->lexer);
36402 *ellipsisp = false; /* Initially, assume no ellipsis. */
36404 while (token->type == CPP_COMMA)
36406 cp_parameter_declarator *parmdecl;
36407 tree parm;
36409 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
36410 token = cp_lexer_peek_token (parser->lexer);
36412 if (token->type == CPP_ELLIPSIS)
36414 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
36415 *ellipsisp = true;
36416 token = cp_lexer_peek_token (parser->lexer);
36417 break;
36420 /* TODO: parse attributes for tail parameters. */
36421 parmdecl = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
36422 false, NULL);
36423 parm = grokdeclarator (parmdecl->declarator,
36424 &parmdecl->decl_specifiers,
36425 PARM, /*initialized=*/0,
36426 /*attrlist=*/NULL);
36428 chainon (params, build_tree_list (NULL_TREE, parm));
36429 token = cp_lexer_peek_token (parser->lexer);
36432 /* We allow tail attributes for the method. */
36433 if (token->keyword == RID_ATTRIBUTE)
36435 if (*attributes == NULL_TREE)
36437 *attributes = cp_parser_attributes_opt (parser);
36438 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
36439 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
36440 return params;
36442 else
36443 /* We have an error, but parse the attributes, so that we can
36444 carry on. */
36445 *attributes = cp_parser_attributes_opt (parser);
36447 cp_parser_error (parser,
36448 "method attributes must be specified at the end");
36449 return error_mark_node;
36452 return params;
36455 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
36457 static void
36458 cp_parser_objc_interstitial_code (cp_parser* parser)
36460 cp_token *token = cp_lexer_peek_token (parser->lexer);
36462 /* If the next token is `extern' and the following token is a string
36463 literal, then we have a linkage specification. */
36464 if (token->keyword == RID_EXTERN
36465 && cp_parser_is_pure_string_literal
36466 (cp_lexer_peek_nth_token (parser->lexer, 2)))
36467 cp_parser_linkage_specification (parser, NULL_TREE);
36468 /* Handle #pragma, if any. */
36469 else if (token->type == CPP_PRAGMA)
36470 cp_parser_pragma (parser, pragma_objc_icode, NULL);
36471 /* Allow stray semicolons. */
36472 else if (token->type == CPP_SEMICOLON)
36473 cp_lexer_consume_token (parser->lexer);
36474 /* Mark methods as optional or required, when building protocols. */
36475 else if (token->keyword == RID_AT_OPTIONAL)
36477 cp_lexer_consume_token (parser->lexer);
36478 objc_set_method_opt (true);
36480 else if (token->keyword == RID_AT_REQUIRED)
36482 cp_lexer_consume_token (parser->lexer);
36483 objc_set_method_opt (false);
36485 else if (token->keyword == RID_NAMESPACE)
36486 cp_parser_namespace_definition (parser);
36487 /* Other stray characters must generate errors. */
36488 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
36490 cp_lexer_consume_token (parser->lexer);
36491 error ("stray %qs between Objective-C++ methods",
36492 token->type == CPP_OPEN_BRACE ? "{" : "}");
36494 /* Finally, try to parse a block-declaration, or a function-definition. */
36495 else
36496 cp_parser_block_declaration (parser, /*statement_p=*/false);
36499 /* Parse a method signature. */
36501 static tree
36502 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
36504 tree rettype, kwdparms, optparms;
36505 bool ellipsis = false;
36506 bool is_class_method;
36508 is_class_method = cp_parser_objc_method_type (parser);
36509 rettype = cp_parser_objc_typename (parser);
36510 *attributes = NULL_TREE;
36511 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
36512 if (kwdparms == error_mark_node)
36513 return error_mark_node;
36514 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
36515 if (optparms == error_mark_node)
36516 return error_mark_node;
36518 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
36521 static bool
36522 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
36524 tree tattr;
36525 cp_lexer_save_tokens (parser->lexer);
36526 tattr = cp_parser_attributes_opt (parser);
36527 gcc_assert (tattr) ;
36529 /* If the attributes are followed by a method introducer, this is not allowed.
36530 Dump the attributes and flag the situation. */
36531 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
36532 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
36533 return true;
36535 /* Otherwise, the attributes introduce some interstitial code, possibly so
36536 rewind to allow that check. */
36537 cp_lexer_rollback_tokens (parser->lexer);
36538 return false;
36541 /* Parse an Objective-C method prototype list. */
36543 static void
36544 cp_parser_objc_method_prototype_list (cp_parser* parser)
36546 cp_token *token = cp_lexer_peek_token (parser->lexer);
36548 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
36550 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
36552 tree attributes, sig;
36553 bool is_class_method;
36554 if (token->type == CPP_PLUS)
36555 is_class_method = true;
36556 else
36557 is_class_method = false;
36558 sig = cp_parser_objc_method_signature (parser, &attributes);
36559 if (sig == error_mark_node)
36561 cp_parser_skip_to_end_of_block_or_statement (parser);
36562 token = cp_lexer_peek_token (parser->lexer);
36563 continue;
36565 objc_add_method_declaration (is_class_method, sig, attributes);
36566 cp_parser_consume_semicolon_at_end_of_statement (parser);
36568 else if (token->keyword == RID_AT_PROPERTY)
36569 cp_parser_objc_at_property_declaration (parser);
36570 else if (token->keyword == RID_ATTRIBUTE
36571 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
36572 warning_at (cp_lexer_peek_token (parser->lexer)->location,
36573 OPT_Wattributes,
36574 "prefix attributes are ignored for methods");
36575 else
36576 /* Allow for interspersed non-ObjC++ code. */
36577 cp_parser_objc_interstitial_code (parser);
36579 token = cp_lexer_peek_token (parser->lexer);
36582 if (token->type != CPP_EOF)
36583 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
36584 else
36585 cp_parser_error (parser, "expected %<@end%>");
36587 objc_finish_interface ();
36590 /* Parse an Objective-C method definition list. */
36592 static void
36593 cp_parser_objc_method_definition_list (cp_parser* parser)
36595 for (;;)
36597 cp_token *token = cp_lexer_peek_token (parser->lexer);
36599 if (token->keyword == RID_AT_END)
36601 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
36602 break;
36604 else if (token->type == CPP_EOF)
36606 cp_parser_error (parser, "expected %<@end%>");
36607 break;
36609 else if (token->type == CPP_PLUS || token->type == CPP_MINUS)
36611 bool is_class_method = token->type == CPP_PLUS;
36613 push_deferring_access_checks (dk_deferred);
36614 tree attribute;
36615 tree sig = cp_parser_objc_method_signature (parser, &attribute);
36616 if (sig == error_mark_node)
36617 cp_parser_skip_to_end_of_block_or_statement (parser);
36618 else
36620 objc_start_method_definition (is_class_method, sig,
36621 attribute, NULL_TREE);
36623 /* For historical reasons, we accept an optional semicolon. */
36624 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
36625 cp_lexer_consume_token (parser->lexer);
36627 perform_deferred_access_checks (tf_warning_or_error);
36628 stop_deferring_access_checks ();
36629 tree meth
36630 = cp_parser_function_definition_after_declarator (parser, false);
36631 pop_deferring_access_checks ();
36632 objc_finish_method_definition (meth);
36635 /* The following case will be removed once @synthesize is
36636 completely implemented. */
36637 else if (token->keyword == RID_AT_PROPERTY)
36638 cp_parser_objc_at_property_declaration (parser);
36639 else if (token->keyword == RID_AT_SYNTHESIZE)
36640 cp_parser_objc_at_synthesize_declaration (parser);
36641 else if (token->keyword == RID_AT_DYNAMIC)
36642 cp_parser_objc_at_dynamic_declaration (parser);
36643 else if (token->keyword == RID_ATTRIBUTE
36644 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
36645 warning_at (token->location, OPT_Wattributes,
36646 "prefix attributes are ignored for methods");
36647 else
36648 /* Allow for interspersed non-ObjC++ code. */
36649 cp_parser_objc_interstitial_code (parser);
36652 objc_finish_implementation ();
36655 /* Parse Objective-C ivars. */
36657 static void
36658 cp_parser_objc_class_ivars (cp_parser* parser)
36660 cp_token *token = cp_lexer_peek_token (parser->lexer);
36662 if (token->type != CPP_OPEN_BRACE)
36663 return; /* No ivars specified. */
36665 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
36666 token = cp_lexer_peek_token (parser->lexer);
36668 while (token->type != CPP_CLOSE_BRACE
36669 && token->keyword != RID_AT_END && token->type != CPP_EOF)
36671 cp_decl_specifier_seq declspecs;
36672 int decl_class_or_enum_p;
36673 tree prefix_attributes;
36675 cp_parser_objc_visibility_spec (parser);
36677 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
36678 break;
36680 cp_parser_decl_specifier_seq (parser,
36681 CP_PARSER_FLAGS_OPTIONAL,
36682 &declspecs,
36683 &decl_class_or_enum_p);
36685 /* auto, register, static, extern, mutable. */
36686 if (declspecs.storage_class != sc_none)
36688 cp_parser_error (parser, "invalid type for instance variable");
36689 declspecs.storage_class = sc_none;
36692 /* thread_local. */
36693 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
36695 cp_parser_error (parser, "invalid type for instance variable");
36696 declspecs.locations[ds_thread] = 0;
36699 /* typedef. */
36700 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
36702 cp_parser_error (parser, "invalid type for instance variable");
36703 declspecs.locations[ds_typedef] = 0;
36706 prefix_attributes = declspecs.attributes;
36707 declspecs.attributes = NULL_TREE;
36709 /* Keep going until we hit the `;' at the end of the
36710 declaration. */
36711 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
36713 tree width = NULL_TREE, attributes, first_attribute, decl;
36714 cp_declarator *declarator = NULL;
36715 int ctor_dtor_or_conv_p;
36717 /* Check for a (possibly unnamed) bitfield declaration. */
36718 token = cp_lexer_peek_token (parser->lexer);
36719 if (token->type == CPP_COLON)
36720 goto eat_colon;
36722 if (token->type == CPP_NAME
36723 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
36724 == CPP_COLON))
36726 /* Get the name of the bitfield. */
36727 declarator = make_id_declarator (NULL_TREE,
36728 cp_parser_identifier (parser),
36729 sfk_none, token->location);
36731 eat_colon:
36732 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
36733 /* Get the width of the bitfield. */
36734 width
36735 = cp_parser_constant_expression (parser);
36737 else
36739 /* Parse the declarator. */
36740 declarator
36741 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
36742 CP_PARSER_FLAGS_NONE,
36743 &ctor_dtor_or_conv_p,
36744 /*parenthesized_p=*/NULL,
36745 /*member_p=*/false,
36746 /*friend_p=*/false,
36747 /*static_p=*/false);
36750 /* Look for attributes that apply to the ivar. */
36751 attributes = cp_parser_attributes_opt (parser);
36752 /* Remember which attributes are prefix attributes and
36753 which are not. */
36754 first_attribute = attributes;
36755 /* Combine the attributes. */
36756 attributes = attr_chainon (prefix_attributes, attributes);
36758 if (width)
36759 /* Create the bitfield declaration. */
36760 decl = grokbitfield (declarator, &declspecs,
36761 width, NULL_TREE, attributes);
36762 else
36763 decl = grokfield (declarator, &declspecs,
36764 NULL_TREE, /*init_const_expr_p=*/false,
36765 NULL_TREE, attributes);
36767 /* Add the instance variable. */
36768 if (decl != error_mark_node && decl != NULL_TREE)
36769 objc_add_instance_variable (decl);
36771 /* Reset PREFIX_ATTRIBUTES. */
36772 if (attributes != error_mark_node)
36774 while (attributes && TREE_CHAIN (attributes) != first_attribute)
36775 attributes = TREE_CHAIN (attributes);
36776 if (attributes)
36777 TREE_CHAIN (attributes) = NULL_TREE;
36780 token = cp_lexer_peek_token (parser->lexer);
36782 if (token->type == CPP_COMMA)
36784 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
36785 continue;
36787 break;
36790 cp_parser_consume_semicolon_at_end_of_statement (parser);
36791 token = cp_lexer_peek_token (parser->lexer);
36794 if (token->keyword == RID_AT_END)
36795 cp_parser_error (parser, "expected %<}%>");
36797 /* Do not consume the RID_AT_END, so it will be read again as terminating
36798 the @interface of @implementation. */
36799 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
36800 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
36802 /* For historical reasons, we accept an optional semicolon. */
36803 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
36804 cp_lexer_consume_token (parser->lexer);
36807 /* Parse an Objective-C protocol declaration. */
36809 static void
36810 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
36812 tree proto, protorefs;
36813 cp_token *tok;
36815 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
36816 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
36818 tok = cp_lexer_peek_token (parser->lexer);
36819 error_at (tok->location, "identifier expected after %<@protocol%>");
36820 cp_parser_consume_semicolon_at_end_of_statement (parser);
36821 return;
36824 /* See if we have a forward declaration or a definition. */
36825 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
36827 /* Try a forward declaration first. */
36828 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
36830 while (true)
36832 tree id;
36834 id = cp_parser_identifier (parser);
36835 if (id == error_mark_node)
36836 break;
36838 objc_declare_protocol (id, attributes);
36840 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36841 cp_lexer_consume_token (parser->lexer);
36842 else
36843 break;
36845 cp_parser_consume_semicolon_at_end_of_statement (parser);
36848 /* Ok, we got a full-fledged definition (or at least should). */
36849 else
36851 proto = cp_parser_identifier (parser);
36852 protorefs = cp_parser_objc_protocol_refs_opt (parser);
36853 objc_start_protocol (proto, protorefs, attributes);
36854 cp_parser_objc_method_prototype_list (parser);
36858 /* Parse an Objective-C superclass or category. */
36860 static void
36861 cp_parser_objc_superclass_or_category (cp_parser *parser,
36862 bool iface_p,
36863 tree *super,
36864 tree *categ, bool *is_class_extension)
36866 cp_token *next = cp_lexer_peek_token (parser->lexer);
36868 *super = *categ = NULL_TREE;
36869 *is_class_extension = false;
36870 if (next->type == CPP_COLON)
36872 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
36873 *super = cp_parser_identifier (parser);
36875 else if (next->type == CPP_OPEN_PAREN)
36877 matching_parens parens;
36878 parens.consume_open (parser); /* Eat '('. */
36880 /* If there is no category name, and this is an @interface, we
36881 have a class extension. */
36882 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
36884 *categ = NULL_TREE;
36885 *is_class_extension = true;
36887 else
36888 *categ = cp_parser_identifier (parser);
36890 parens.require_close (parser);
36894 /* Parse an Objective-C class interface. */
36896 static void
36897 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
36899 tree name, super, categ, protos;
36900 bool is_class_extension;
36902 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
36903 location_t nam_loc = cp_lexer_peek_token (parser->lexer)->location;
36904 name = cp_parser_identifier (parser);
36905 if (name == error_mark_node)
36907 /* It's hard to recover because even if valid @interface stuff
36908 is to follow, we can't compile it (or validate it) if we
36909 don't even know which class it refers to. Let's assume this
36910 was a stray '@interface' token in the stream and skip it.
36912 return;
36914 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
36915 &is_class_extension);
36916 protos = cp_parser_objc_protocol_refs_opt (parser);
36918 /* We have either a class or a category on our hands. */
36919 if (categ || is_class_extension)
36920 objc_start_category_interface (name, categ, protos, attributes);
36921 else
36923 objc_start_class_interface (name, nam_loc, super, protos, attributes);
36924 /* Handle instance variable declarations, if any. */
36925 cp_parser_objc_class_ivars (parser);
36926 objc_continue_interface ();
36929 cp_parser_objc_method_prototype_list (parser);
36932 /* Parse an Objective-C class implementation. */
36934 static void
36935 cp_parser_objc_class_implementation (cp_parser* parser)
36937 tree name, super, categ;
36938 bool is_class_extension;
36940 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
36941 name = cp_parser_identifier (parser);
36942 if (name == error_mark_node)
36944 /* It's hard to recover because even if valid @implementation
36945 stuff is to follow, we can't compile it (or validate it) if
36946 we don't even know which class it refers to. Let's assume
36947 this was a stray '@implementation' token in the stream and
36948 skip it.
36950 return;
36952 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
36953 &is_class_extension);
36955 /* We have either a class or a category on our hands. */
36956 if (categ)
36957 objc_start_category_implementation (name, categ);
36958 else
36960 objc_start_class_implementation (name, super);
36961 /* Handle instance variable declarations, if any. */
36962 cp_parser_objc_class_ivars (parser);
36963 objc_continue_implementation ();
36966 cp_parser_objc_method_definition_list (parser);
36969 /* Consume the @end token and finish off the implementation. */
36971 static void
36972 cp_parser_objc_end_implementation (cp_parser* parser)
36974 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
36975 objc_finish_implementation ();
36978 /* Parse an Objective-C declaration. */
36980 static void
36981 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
36983 /* Try to figure out what kind of declaration is present. */
36984 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
36986 if (attributes)
36987 switch (kwd->keyword)
36989 case RID_AT_ALIAS:
36990 case RID_AT_CLASS:
36991 case RID_AT_END:
36992 error_at (kwd->location, "attributes may not be specified before"
36993 " the %<@%D%> Objective-C++ keyword",
36994 kwd->u.value);
36995 attributes = NULL;
36996 break;
36997 case RID_AT_IMPLEMENTATION:
36998 warning_at (kwd->location, OPT_Wattributes,
36999 "prefix attributes are ignored before %<@%D%>",
37000 kwd->u.value);
37001 attributes = NULL;
37002 default:
37003 break;
37006 switch (kwd->keyword)
37008 case RID_AT_ALIAS:
37009 cp_parser_objc_alias_declaration (parser);
37010 break;
37011 case RID_AT_CLASS:
37012 cp_parser_objc_class_declaration (parser);
37013 break;
37014 case RID_AT_PROTOCOL:
37015 cp_parser_objc_protocol_declaration (parser, attributes);
37016 break;
37017 case RID_AT_INTERFACE:
37018 cp_parser_objc_class_interface (parser, attributes);
37019 break;
37020 case RID_AT_IMPLEMENTATION:
37021 cp_parser_objc_class_implementation (parser);
37022 break;
37023 case RID_AT_END:
37024 cp_parser_objc_end_implementation (parser);
37025 break;
37026 default:
37027 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
37028 kwd->u.value);
37029 cp_parser_skip_to_end_of_block_or_statement (parser);
37033 /* Parse an Objective-C try-catch-finally statement.
37035 objc-try-catch-finally-stmt:
37036 @try compound-statement objc-catch-clause-seq [opt]
37037 objc-finally-clause [opt]
37039 objc-catch-clause-seq:
37040 objc-catch-clause objc-catch-clause-seq [opt]
37042 objc-catch-clause:
37043 @catch ( objc-exception-declaration ) compound-statement
37045 objc-finally-clause:
37046 @finally compound-statement
37048 objc-exception-declaration:
37049 parameter-declaration
37050 '...'
37052 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
37054 Returns NULL_TREE.
37056 PS: This function is identical to c_parser_objc_try_catch_finally_statement
37057 for C. Keep them in sync. */
37059 static tree
37060 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
37062 location_t location;
37063 tree stmt;
37065 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
37066 location = cp_lexer_peek_token (parser->lexer)->location;
37067 objc_maybe_warn_exceptions (location);
37068 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
37069 node, lest it get absorbed into the surrounding block. */
37070 stmt = push_stmt_list ();
37071 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
37072 objc_begin_try_stmt (location, pop_stmt_list (stmt));
37074 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
37076 cp_parameter_declarator *parm;
37077 tree parameter_declaration = error_mark_node;
37078 bool seen_open_paren = false;
37079 matching_parens parens;
37081 cp_lexer_consume_token (parser->lexer);
37082 if (parens.require_open (parser))
37083 seen_open_paren = true;
37084 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
37086 /* We have "@catch (...)" (where the '...' are literally
37087 what is in the code). Skip the '...'.
37088 parameter_declaration is set to NULL_TREE, and
37089 objc_being_catch_clauses() knows that that means
37090 '...'. */
37091 cp_lexer_consume_token (parser->lexer);
37092 parameter_declaration = NULL_TREE;
37094 else
37096 /* We have "@catch (NSException *exception)" or something
37097 like that. Parse the parameter declaration. */
37098 parm = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
37099 false, NULL);
37100 if (parm == NULL)
37101 parameter_declaration = error_mark_node;
37102 else
37103 parameter_declaration = grokdeclarator (parm->declarator,
37104 &parm->decl_specifiers,
37105 PARM, /*initialized=*/0,
37106 /*attrlist=*/NULL);
37108 if (seen_open_paren)
37109 parens.require_close (parser);
37110 else
37112 /* If there was no open parenthesis, we are recovering from
37113 an error, and we are trying to figure out what mistake
37114 the user has made. */
37116 /* If there is an immediate closing parenthesis, the user
37117 probably forgot the opening one (ie, they typed "@catch
37118 NSException *e)". Parse the closing parenthesis and keep
37119 going. */
37120 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
37121 cp_lexer_consume_token (parser->lexer);
37123 /* If these is no immediate closing parenthesis, the user
37124 probably doesn't know that parenthesis are required at
37125 all (ie, they typed "@catch NSException *e"). So, just
37126 forget about the closing parenthesis and keep going. */
37128 objc_begin_catch_clause (parameter_declaration);
37129 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
37130 objc_finish_catch_clause ();
37132 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
37134 cp_lexer_consume_token (parser->lexer);
37135 location = cp_lexer_peek_token (parser->lexer)->location;
37136 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
37137 node, lest it get absorbed into the surrounding block. */
37138 stmt = push_stmt_list ();
37139 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
37140 objc_build_finally_clause (location, pop_stmt_list (stmt));
37143 return objc_finish_try_stmt ();
37146 /* Parse an Objective-C synchronized statement.
37148 objc-synchronized-stmt:
37149 @synchronized ( expression ) compound-statement
37151 Returns NULL_TREE. */
37153 static tree
37154 cp_parser_objc_synchronized_statement (cp_parser *parser)
37156 location_t location;
37157 tree lock, stmt;
37159 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
37161 location = cp_lexer_peek_token (parser->lexer)->location;
37162 objc_maybe_warn_exceptions (location);
37163 matching_parens parens;
37164 parens.require_open (parser);
37165 lock = cp_parser_expression (parser);
37166 parens.require_close (parser);
37168 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
37169 node, lest it get absorbed into the surrounding block. */
37170 stmt = push_stmt_list ();
37171 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
37173 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
37176 /* Parse an Objective-C throw statement.
37178 objc-throw-stmt:
37179 @throw assignment-expression [opt] ;
37181 Returns a constructed '@throw' statement. */
37183 static tree
37184 cp_parser_objc_throw_statement (cp_parser *parser)
37186 tree expr = NULL_TREE;
37187 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37189 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
37191 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
37192 expr = cp_parser_expression (parser);
37194 cp_parser_consume_semicolon_at_end_of_statement (parser);
37196 return objc_build_throw_stmt (loc, expr);
37199 /* Parse an Objective-C statement. */
37201 static tree
37202 cp_parser_objc_statement (cp_parser * parser)
37204 /* Try to figure out what kind of declaration is present. */
37205 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
37207 switch (kwd->keyword)
37209 case RID_AT_TRY:
37210 return cp_parser_objc_try_catch_finally_statement (parser);
37211 case RID_AT_SYNCHRONIZED:
37212 return cp_parser_objc_synchronized_statement (parser);
37213 case RID_AT_THROW:
37214 return cp_parser_objc_throw_statement (parser);
37215 default:
37216 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
37217 kwd->u.value);
37218 cp_parser_skip_to_end_of_block_or_statement (parser);
37221 return error_mark_node;
37224 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
37225 look ahead to see if an objc keyword follows the attributes. This
37226 is to detect the use of prefix attributes on ObjC @interface and
37227 @protocol. */
37229 static bool
37230 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
37232 cp_lexer_save_tokens (parser->lexer);
37233 tree addon = cp_parser_attributes_opt (parser);
37234 if (addon
37235 && OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
37237 cp_lexer_commit_tokens (parser->lexer);
37238 if (*attrib)
37239 TREE_CHAIN (*attrib) = addon;
37240 else
37241 *attrib = addon;
37242 return true;
37244 cp_lexer_rollback_tokens (parser->lexer);
37245 return false;
37248 /* This routine is a minimal replacement for
37249 c_parser_struct_declaration () used when parsing the list of
37250 types/names or ObjC++ properties. For example, when parsing the
37251 code
37253 @property (readonly) int a, b, c;
37255 this function is responsible for parsing "int a, int b, int c" and
37256 returning the declarations as CHAIN of DECLs.
37258 TODO: Share this code with cp_parser_objc_class_ivars. It's very
37259 similar parsing. */
37260 static tree
37261 cp_parser_objc_struct_declaration (cp_parser *parser)
37263 tree decls = NULL_TREE;
37264 cp_decl_specifier_seq declspecs;
37265 int decl_class_or_enum_p;
37266 tree prefix_attributes;
37268 cp_parser_decl_specifier_seq (parser,
37269 CP_PARSER_FLAGS_NONE,
37270 &declspecs,
37271 &decl_class_or_enum_p);
37273 if (declspecs.type == error_mark_node)
37274 return error_mark_node;
37276 /* auto, register, static, extern, mutable. */
37277 if (declspecs.storage_class != sc_none)
37279 cp_parser_error (parser, "invalid type for property");
37280 declspecs.storage_class = sc_none;
37283 /* thread_local. */
37284 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
37286 cp_parser_error (parser, "invalid type for property");
37287 declspecs.locations[ds_thread] = 0;
37290 /* typedef. */
37291 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
37293 cp_parser_error (parser, "invalid type for property");
37294 declspecs.locations[ds_typedef] = 0;
37297 prefix_attributes = declspecs.attributes;
37298 declspecs.attributes = NULL_TREE;
37300 /* Keep going until we hit the `;' at the end of the declaration. */
37301 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
37303 tree attributes, first_attribute, decl;
37304 cp_declarator *declarator;
37305 cp_token *token;
37307 /* Parse the declarator. */
37308 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
37309 CP_PARSER_FLAGS_NONE,
37310 NULL, NULL, false, false, false);
37312 /* Look for attributes that apply to the ivar. */
37313 attributes = cp_parser_attributes_opt (parser);
37314 /* Remember which attributes are prefix attributes and
37315 which are not. */
37316 first_attribute = attributes;
37317 /* Combine the attributes. */
37318 attributes = attr_chainon (prefix_attributes, attributes);
37320 decl = grokfield (declarator, &declspecs,
37321 NULL_TREE, /*init_const_expr_p=*/false,
37322 NULL_TREE, attributes);
37324 if (decl == error_mark_node || decl == NULL_TREE)
37325 return error_mark_node;
37327 /* Reset PREFIX_ATTRIBUTES. */
37328 if (attributes != error_mark_node)
37330 while (attributes && TREE_CHAIN (attributes) != first_attribute)
37331 attributes = TREE_CHAIN (attributes);
37332 if (attributes)
37333 TREE_CHAIN (attributes) = NULL_TREE;
37336 DECL_CHAIN (decl) = decls;
37337 decls = decl;
37339 token = cp_lexer_peek_token (parser->lexer);
37340 if (token->type == CPP_COMMA)
37342 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
37343 continue;
37345 else
37346 break;
37348 return decls;
37351 /* Parse an Objective-C @property declaration. The syntax is:
37353 objc-property-declaration:
37354 '@property' objc-property-attributes[opt] struct-declaration ;
37356 objc-property-attributes:
37357 '(' objc-property-attribute-list ')'
37359 objc-property-attribute-list:
37360 objc-property-attribute
37361 objc-property-attribute-list, objc-property-attribute
37363 objc-property-attribute
37364 'getter' = identifier
37365 'setter' = identifier
37366 'readonly'
37367 'readwrite'
37368 'assign'
37369 'retain'
37370 'copy'
37371 'nonatomic'
37373 For example:
37374 @property NSString *name;
37375 @property (readonly) id object;
37376 @property (retain, nonatomic, getter=getTheName) id name;
37377 @property int a, b, c;
37379 PS: This function is identical to
37380 c_parser_objc_at_property_declaration for C. Keep them in sync. */
37381 static void
37382 cp_parser_objc_at_property_declaration (cp_parser *parser)
37384 /* Parse the optional attribute list.
37386 A list of parsed, but not verified, attributes. */
37387 auto_delete_vec<property_attribute_info> prop_attr_list;
37388 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37390 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
37392 /* Parse the optional attribute list... */
37393 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37395 /* Eat the '('. */
37396 matching_parens parens;
37397 location_t attr_start = cp_lexer_peek_token (parser->lexer)->location;
37398 parens.consume_open (parser);
37399 bool syntax_error = false;
37401 /* Allow empty @property attribute lists, but with a warning. */
37402 location_t attr_end = cp_lexer_peek_token (parser->lexer)->location;
37403 location_t attr_comb;
37404 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
37406 attr_comb = make_location (attr_end, attr_start, attr_end);
37407 warning_at (attr_comb, OPT_Wattributes,
37408 "empty property attribute list");
37410 else
37411 while (true)
37413 cp_token *token = cp_lexer_peek_token (parser->lexer);
37414 attr_start = token->location;
37415 attr_end = get_finish (token->location);
37416 attr_comb = make_location (attr_start, attr_start, attr_end);
37418 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_COMMA)
37420 warning_at (attr_comb, OPT_Wattributes,
37421 "missing property attribute");
37422 if (token->type == CPP_CLOSE_PAREN)
37423 break;
37424 cp_lexer_consume_token (parser->lexer);
37425 continue;
37428 tree attr_name = NULL_TREE;
37429 if (identifier_p (token->u.value))
37430 attr_name = token->u.value;
37432 enum rid keyword;
37433 if (token->type == CPP_NAME)
37434 keyword = C_RID_CODE (token->u.value);
37435 else if (token->type == CPP_KEYWORD
37436 && token->keyword == RID_CLASS)
37437 /* Account for accepting the 'class' keyword in this context. */
37438 keyword = RID_CLASS;
37439 else
37440 keyword = RID_MAX; /* By definition, an unknown property. */
37441 cp_lexer_consume_token (parser->lexer);
37443 enum objc_property_attribute_kind prop_kind
37444 = objc_prop_attr_kind_for_rid (keyword);
37445 property_attribute_info *prop
37446 = new property_attribute_info (attr_name, attr_comb, prop_kind);
37447 prop_attr_list.safe_push (prop);
37449 tree meth_name;
37450 switch (prop->prop_kind)
37452 default: break;
37453 case OBJC_PROPERTY_ATTR_UNKNOWN:
37454 if (attr_name)
37455 error_at (attr_start, "unknown property attribute %qE",
37456 attr_name);
37457 else
37458 error_at (attr_start, "unknown property attribute");
37459 prop->parse_error = syntax_error = true;
37460 break;
37462 case OBJC_PROPERTY_ATTR_GETTER:
37463 case OBJC_PROPERTY_ATTR_SETTER:
37464 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
37466 attr_comb = make_location (attr_end, attr_start, attr_end);
37467 error_at (attr_comb, "expected %<=%> after Objective-C %qE",
37468 attr_name);
37469 prop->parse_error = syntax_error = true;
37470 break;
37473 token = cp_lexer_peek_token (parser->lexer);
37474 attr_end = token->location;
37475 cp_lexer_consume_token (parser->lexer); /* eat the = */
37477 if (!cp_parser_objc_selector_p
37478 (cp_lexer_peek_token (parser->lexer)->type))
37480 attr_comb = make_location (attr_end, attr_start, attr_end);
37481 error_at (attr_comb, "expected %qE selector name",
37482 attr_name);
37483 prop->parse_error = syntax_error = true;
37484 break;
37487 /* Get the end of the method name, and consume the name. */
37488 token = cp_lexer_peek_token (parser->lexer);
37489 attr_end = get_finish (token->location);
37490 /* Because method names may contain C++ keywords, we have a
37491 routine to fetch them (this also consumes the token). */
37492 meth_name = cp_parser_objc_selector (parser);
37494 if (prop->prop_kind == OBJC_PROPERTY_ATTR_SETTER)
37496 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
37498 attr_comb = make_location (attr_end, attr_start,
37499 attr_end);
37500 error_at (attr_comb, "setter method names must"
37501 " terminate with %<:%>");
37502 prop->parse_error = syntax_error = true;
37504 else
37506 attr_end = get_finish (cp_lexer_peek_token
37507 (parser->lexer)->location);
37508 cp_lexer_consume_token (parser->lexer);
37510 attr_comb = make_location (attr_start, attr_start,
37511 attr_end);
37513 else
37514 attr_comb = make_location (attr_start, attr_start,
37515 attr_end);
37516 prop->ident = meth_name;
37517 /* Updated location including all that was successfully
37518 parsed. */
37519 prop->prop_loc = attr_comb;
37520 break;
37523 /* If we see a comma here, then keep going - even if we already
37524 saw a syntax error. For simple mistakes e.g. (asign, getter=x)
37525 this makes a more useful output and avoid spurious warnings
37526 about missing attributes that are, in fact, specified after the
37527 one with the syntax error. */
37528 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37529 cp_lexer_consume_token (parser->lexer);
37530 else
37531 break;
37534 if (syntax_error || !parens.require_close (parser))
37535 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37536 /*or_comma=*/false,
37537 /*consume_paren=*/true);
37540 /* 'properties' is the list of properties that we read. Usually a
37541 single one, but maybe more (eg, in "@property int a, b, c;" there
37542 are three).
37543 TODO: Update this parsing so that it accepts (erroneous) bitfields so
37544 that we can issue a meaningful and consistent (between C/C++) error
37545 message from objc_add_property_declaration (). */
37546 tree properties = cp_parser_objc_struct_declaration (parser);
37548 if (properties == error_mark_node)
37549 cp_parser_skip_to_end_of_statement (parser);
37550 else if (properties == NULL_TREE)
37551 cp_parser_error (parser, "expected identifier");
37552 else
37554 /* Comma-separated properties are chained together in reverse order;
37555 add them one by one. */
37556 properties = nreverse (properties);
37557 for (; properties; properties = TREE_CHAIN (properties))
37558 objc_add_property_declaration (loc, copy_node (properties),
37559 prop_attr_list);
37562 cp_parser_consume_semicolon_at_end_of_statement (parser);
37565 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
37567 objc-synthesize-declaration:
37568 @synthesize objc-synthesize-identifier-list ;
37570 objc-synthesize-identifier-list:
37571 objc-synthesize-identifier
37572 objc-synthesize-identifier-list, objc-synthesize-identifier
37574 objc-synthesize-identifier
37575 identifier
37576 identifier = identifier
37578 For example:
37579 @synthesize MyProperty;
37580 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
37582 PS: This function is identical to c_parser_objc_at_synthesize_declaration
37583 for C. Keep them in sync.
37585 static void
37586 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
37588 tree list = NULL_TREE;
37589 location_t loc;
37590 loc = cp_lexer_peek_token (parser->lexer)->location;
37592 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
37593 while (true)
37595 tree property, ivar;
37596 property = cp_parser_identifier (parser);
37597 if (property == error_mark_node)
37599 cp_parser_consume_semicolon_at_end_of_statement (parser);
37600 return;
37602 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
37604 cp_lexer_consume_token (parser->lexer);
37605 ivar = cp_parser_identifier (parser);
37606 if (ivar == error_mark_node)
37608 cp_parser_consume_semicolon_at_end_of_statement (parser);
37609 return;
37612 else
37613 ivar = NULL_TREE;
37614 list = chainon (list, build_tree_list (ivar, property));
37615 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37616 cp_lexer_consume_token (parser->lexer);
37617 else
37618 break;
37620 cp_parser_consume_semicolon_at_end_of_statement (parser);
37621 objc_add_synthesize_declaration (loc, list);
37624 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
37626 objc-dynamic-declaration:
37627 @dynamic identifier-list ;
37629 For example:
37630 @dynamic MyProperty;
37631 @dynamic MyProperty, AnotherProperty;
37633 PS: This function is identical to c_parser_objc_at_dynamic_declaration
37634 for C. Keep them in sync.
37636 static void
37637 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
37639 tree list = NULL_TREE;
37640 location_t loc;
37641 loc = cp_lexer_peek_token (parser->lexer)->location;
37643 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
37644 while (true)
37646 tree property;
37647 property = cp_parser_identifier (parser);
37648 if (property == error_mark_node)
37650 cp_parser_consume_semicolon_at_end_of_statement (parser);
37651 return;
37653 list = chainon (list, build_tree_list (NULL, property));
37654 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37655 cp_lexer_consume_token (parser->lexer);
37656 else
37657 break;
37659 cp_parser_consume_semicolon_at_end_of_statement (parser);
37660 objc_add_dynamic_declaration (loc, list);
37664 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 / 4.5 / 5.0 parsing routines. */
37666 /* Returns name of the next clause.
37667 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
37668 the token is not consumed. Otherwise appropriate pragma_omp_clause is
37669 returned and the token is consumed. */
37671 static pragma_omp_clause
37672 cp_parser_omp_clause_name (cp_parser *parser)
37674 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
37676 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
37677 result = PRAGMA_OACC_CLAUSE_AUTO;
37678 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
37679 result = PRAGMA_OMP_CLAUSE_IF;
37680 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
37681 result = PRAGMA_OMP_CLAUSE_DEFAULT;
37682 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
37683 result = PRAGMA_OACC_CLAUSE_DELETE;
37684 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
37685 result = PRAGMA_OMP_CLAUSE_PRIVATE;
37686 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
37687 result = PRAGMA_OMP_CLAUSE_FOR;
37688 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37690 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37691 const char *p = IDENTIFIER_POINTER (id);
37693 switch (p[0])
37695 case 'a':
37696 if (!strcmp ("affinity", p))
37697 result = PRAGMA_OMP_CLAUSE_AFFINITY;
37698 else if (!strcmp ("aligned", p))
37699 result = PRAGMA_OMP_CLAUSE_ALIGNED;
37700 else if (!strcmp ("allocate", p))
37701 result = PRAGMA_OMP_CLAUSE_ALLOCATE;
37702 else if (!strcmp ("async", p))
37703 result = PRAGMA_OACC_CLAUSE_ASYNC;
37704 else if (!strcmp ("attach", p))
37705 result = PRAGMA_OACC_CLAUSE_ATTACH;
37706 break;
37707 case 'b':
37708 if (!strcmp ("bind", p))
37709 result = PRAGMA_OMP_CLAUSE_BIND;
37710 break;
37711 case 'c':
37712 if (!strcmp ("collapse", p))
37713 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
37714 else if (!strcmp ("copy", p))
37715 result = PRAGMA_OACC_CLAUSE_COPY;
37716 else if (!strcmp ("copyin", p))
37717 result = PRAGMA_OMP_CLAUSE_COPYIN;
37718 else if (!strcmp ("copyout", p))
37719 result = PRAGMA_OACC_CLAUSE_COPYOUT;
37720 else if (!strcmp ("copyprivate", p))
37721 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
37722 else if (!strcmp ("create", p))
37723 result = PRAGMA_OACC_CLAUSE_CREATE;
37724 break;
37725 case 'd':
37726 if (!strcmp ("defaultmap", p))
37727 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
37728 else if (!strcmp ("depend", p))
37729 result = PRAGMA_OMP_CLAUSE_DEPEND;
37730 else if (!strcmp ("detach", p))
37731 result = PRAGMA_OACC_CLAUSE_DETACH;
37732 else if (!strcmp ("device", p))
37733 result = PRAGMA_OMP_CLAUSE_DEVICE;
37734 else if (!strcmp ("deviceptr", p))
37735 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
37736 else if (!strcmp ("device_resident", p))
37737 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
37738 else if (!strcmp ("device_type", p))
37739 result = PRAGMA_OMP_CLAUSE_DEVICE_TYPE;
37740 else if (!strcmp ("dist_schedule", p))
37741 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
37742 else if (!strcmp ("doacross", p))
37743 result = PRAGMA_OMP_CLAUSE_DOACROSS;
37744 break;
37745 case 'e':
37746 if (!strcmp ("enter", p))
37747 result = PRAGMA_OMP_CLAUSE_ENTER;
37748 break;
37749 case 'f':
37750 if (!strcmp ("filter", p))
37751 result = PRAGMA_OMP_CLAUSE_FILTER;
37752 else if (!strcmp ("final", p))
37753 result = PRAGMA_OMP_CLAUSE_FINAL;
37754 else if (!strcmp ("finalize", p))
37755 result = PRAGMA_OACC_CLAUSE_FINALIZE;
37756 else if (!strcmp ("firstprivate", p))
37757 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
37758 else if (!strcmp ("from", p))
37759 result = PRAGMA_OMP_CLAUSE_FROM;
37760 break;
37761 case 'g':
37762 if (!strcmp ("gang", p))
37763 result = PRAGMA_OACC_CLAUSE_GANG;
37764 else if (!strcmp ("grainsize", p))
37765 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
37766 break;
37767 case 'h':
37768 if (!strcmp ("has_device_addr", p))
37769 result = PRAGMA_OMP_CLAUSE_HAS_DEVICE_ADDR;
37770 else if (!strcmp ("hint", p))
37771 result = PRAGMA_OMP_CLAUSE_HINT;
37772 else if (!strcmp ("host", p))
37773 result = PRAGMA_OACC_CLAUSE_HOST;
37774 break;
37775 case 'i':
37776 if (!strcmp ("if_present", p))
37777 result = PRAGMA_OACC_CLAUSE_IF_PRESENT;
37778 else if (!strcmp ("in_reduction", p))
37779 result = PRAGMA_OMP_CLAUSE_IN_REDUCTION;
37780 else if (!strcmp ("inbranch", p))
37781 result = PRAGMA_OMP_CLAUSE_INBRANCH;
37782 else if (!strcmp ("independent", p))
37783 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
37784 else if (!strcmp ("indirect", p))
37785 result = PRAGMA_OMP_CLAUSE_INDIRECT;
37786 else if (!strcmp ("is_device_ptr", p))
37787 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
37788 break;
37789 case 'l':
37790 if (!strcmp ("lastprivate", p))
37791 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
37792 else if (!strcmp ("linear", p))
37793 result = PRAGMA_OMP_CLAUSE_LINEAR;
37794 else if (!strcmp ("link", p))
37795 result = PRAGMA_OMP_CLAUSE_LINK;
37796 break;
37797 case 'm':
37798 if (!strcmp ("map", p))
37799 result = PRAGMA_OMP_CLAUSE_MAP;
37800 else if (!strcmp ("mergeable", p))
37801 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
37802 break;
37803 case 'n':
37804 if (!strcmp ("no_create", p))
37805 result = PRAGMA_OACC_CLAUSE_NO_CREATE;
37806 else if (!strcmp ("nogroup", p))
37807 result = PRAGMA_OMP_CLAUSE_NOGROUP;
37808 else if (!strcmp ("nohost", p))
37809 result = PRAGMA_OACC_CLAUSE_NOHOST;
37810 else if (!strcmp ("nontemporal", p))
37811 result = PRAGMA_OMP_CLAUSE_NONTEMPORAL;
37812 else if (!strcmp ("notinbranch", p))
37813 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
37814 else if (!strcmp ("nowait", p))
37815 result = PRAGMA_OMP_CLAUSE_NOWAIT;
37816 else if (!strcmp ("num_gangs", p))
37817 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
37818 else if (!strcmp ("num_tasks", p))
37819 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
37820 else if (!strcmp ("num_teams", p))
37821 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
37822 else if (!strcmp ("num_threads", p))
37823 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
37824 else if (!strcmp ("num_workers", p))
37825 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
37826 break;
37827 case 'o':
37828 if (!strcmp ("ordered", p))
37829 result = PRAGMA_OMP_CLAUSE_ORDERED;
37830 else if (!strcmp ("order", p))
37831 result = PRAGMA_OMP_CLAUSE_ORDER;
37832 break;
37833 case 'p':
37834 if (!strcmp ("parallel", p))
37835 result = PRAGMA_OMP_CLAUSE_PARALLEL;
37836 else if (!strcmp ("present", p))
37837 result = PRAGMA_OACC_CLAUSE_PRESENT;
37838 else if (!strcmp ("present_or_copy", p)
37839 || !strcmp ("pcopy", p))
37840 result = PRAGMA_OACC_CLAUSE_COPY;
37841 else if (!strcmp ("present_or_copyin", p)
37842 || !strcmp ("pcopyin", p))
37843 result = PRAGMA_OACC_CLAUSE_COPYIN;
37844 else if (!strcmp ("present_or_copyout", p)
37845 || !strcmp ("pcopyout", p))
37846 result = PRAGMA_OACC_CLAUSE_COPYOUT;
37847 else if (!strcmp ("present_or_create", p)
37848 || !strcmp ("pcreate", p))
37849 result = PRAGMA_OACC_CLAUSE_CREATE;
37850 else if (!strcmp ("priority", p))
37851 result = PRAGMA_OMP_CLAUSE_PRIORITY;
37852 else if (!strcmp ("proc_bind", p))
37853 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
37854 break;
37855 case 'r':
37856 if (!strcmp ("reduction", p))
37857 result = PRAGMA_OMP_CLAUSE_REDUCTION;
37858 break;
37859 case 's':
37860 if (!strcmp ("safelen", p))
37861 result = PRAGMA_OMP_CLAUSE_SAFELEN;
37862 else if (!strcmp ("schedule", p))
37863 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
37864 else if (!strcmp ("sections", p))
37865 result = PRAGMA_OMP_CLAUSE_SECTIONS;
37866 else if (!strcmp ("self", p))
37867 result = PRAGMA_OACC_CLAUSE_SELF;
37868 else if (!strcmp ("seq", p))
37869 result = PRAGMA_OACC_CLAUSE_SEQ;
37870 else if (!strcmp ("shared", p))
37871 result = PRAGMA_OMP_CLAUSE_SHARED;
37872 else if (!strcmp ("simd", p))
37873 result = PRAGMA_OMP_CLAUSE_SIMD;
37874 else if (!strcmp ("simdlen", p))
37875 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
37876 break;
37877 case 't':
37878 if (!strcmp ("task_reduction", p))
37879 result = PRAGMA_OMP_CLAUSE_TASK_REDUCTION;
37880 else if (!strcmp ("taskgroup", p))
37881 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
37882 else if (!strcmp ("thread_limit", p))
37883 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
37884 else if (!strcmp ("threads", p))
37885 result = PRAGMA_OMP_CLAUSE_THREADS;
37886 else if (!strcmp ("tile", p))
37887 result = PRAGMA_OACC_CLAUSE_TILE;
37888 else if (!strcmp ("to", p))
37889 result = PRAGMA_OMP_CLAUSE_TO;
37890 break;
37891 case 'u':
37892 if (!strcmp ("uniform", p))
37893 result = PRAGMA_OMP_CLAUSE_UNIFORM;
37894 else if (!strcmp ("untied", p))
37895 result = PRAGMA_OMP_CLAUSE_UNTIED;
37896 else if (!strcmp ("use_device", p))
37897 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
37898 else if (!strcmp ("use_device_addr", p))
37899 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR;
37900 else if (!strcmp ("use_device_ptr", p))
37901 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
37902 break;
37903 case 'v':
37904 if (!strcmp ("vector", p))
37905 result = PRAGMA_OACC_CLAUSE_VECTOR;
37906 else if (!strcmp ("vector_length", p))
37907 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
37908 break;
37909 case 'w':
37910 if (!strcmp ("wait", p))
37911 result = PRAGMA_OACC_CLAUSE_WAIT;
37912 else if (!strcmp ("worker", p))
37913 result = PRAGMA_OACC_CLAUSE_WORKER;
37914 break;
37918 if (result != PRAGMA_OMP_CLAUSE_NONE)
37919 cp_lexer_consume_token (parser->lexer);
37921 return result;
37924 /* Validate that a clause of the given type does not already exist. */
37926 static void
37927 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
37928 const char *name, location_t location)
37930 if (omp_find_clause (clauses, code))
37931 error_at (location, "too many %qs clauses", name);
37934 /* OpenMP 2.5:
37935 variable-list:
37936 identifier
37937 variable-list , identifier
37939 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
37940 colon). An opening parenthesis will have been consumed by the caller.
37942 If KIND is nonzero, create the appropriate node and install the decl
37943 in OMP_CLAUSE_DECL and add the node to the head of the list.
37945 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
37946 return the list created.
37948 COLON can be NULL if only closing parenthesis should end the list,
37949 or pointer to bool which will receive false if the list is terminated
37950 by closing parenthesis or true if the list is terminated by colon.
37952 The optional ALLOW_DEREF argument is true if list items can use the deref
37953 (->) operator. */
37955 struct omp_dim
37957 tree low_bound, length;
37958 location_t loc;
37959 bool no_colon;
37960 omp_dim (tree lb, tree len, location_t lo, bool nc)
37961 : low_bound (lb), length (len), loc (lo), no_colon (nc) {}
37964 static tree
37965 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
37966 tree list, bool *colon,
37967 bool map_lvalue = false)
37969 auto_vec<omp_dim> dims;
37970 bool array_section_p;
37971 cp_token *token;
37972 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37973 if (colon)
37975 parser->colon_corrects_to_scope_p = false;
37976 *colon = false;
37978 while (1)
37980 tree name, decl;
37982 if (kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
37983 cp_parser_parse_tentatively (parser);
37984 /* This condition doesn't include OMP_CLAUSE_DEPEND or
37985 OMP_CLAUSE_AFFINITY since lvalue ("locator list") parsing for those is
37986 handled further down the function. */
37987 else if (map_lvalue
37988 && (kind == OMP_CLAUSE_MAP
37989 || kind == OMP_CLAUSE_TO
37990 || kind == OMP_CLAUSE_FROM))
37992 auto s = make_temp_override (parser->omp_array_section_p, true);
37993 token = cp_lexer_peek_token (parser->lexer);
37994 location_t loc = token->location;
37995 decl = cp_parser_assignment_expression (parser);
37997 /* This code rewrites a parsed expression containing various tree
37998 codes used to represent array accesses into a more uniform nest of
37999 OMP_ARRAY_SECTION nodes before it is processed by
38000 semantics.cc:handle_omp_array_sections_1. It might be more
38001 efficient to move this logic to that function instead, analysing
38002 the parsed expression directly rather than this preprocessed
38003 form. */
38004 dims.truncate (0);
38005 if (TREE_CODE (decl) == OMP_ARRAY_SECTION)
38007 while (TREE_CODE (decl) == OMP_ARRAY_SECTION)
38009 tree low_bound = TREE_OPERAND (decl, 1);
38010 tree length = TREE_OPERAND (decl, 2);
38011 dims.safe_push (omp_dim (low_bound, length, loc, false));
38012 decl = TREE_OPERAND (decl, 0);
38015 while (TREE_CODE (decl) == ARRAY_REF
38016 || TREE_CODE (decl) == INDIRECT_REF
38017 || TREE_CODE (decl) == COMPOUND_EXPR)
38019 if (REFERENCE_REF_P (decl))
38020 break;
38022 if (TREE_CODE (decl) == COMPOUND_EXPR)
38024 decl = TREE_OPERAND (decl, 1);
38025 STRIP_NOPS (decl);
38027 else if (TREE_CODE (decl) == INDIRECT_REF)
38029 dims.safe_push (omp_dim (integer_zero_node,
38030 integer_one_node, loc, true));
38031 decl = TREE_OPERAND (decl, 0);
38033 else /* ARRAY_REF. */
38035 tree index = TREE_OPERAND (decl, 1);
38036 dims.safe_push (omp_dim (index, integer_one_node, loc,
38037 true));
38038 decl = TREE_OPERAND (decl, 0);
38042 /* Bare references have their own special handling, so remove
38043 the explicit dereference added by convert_from_reference. */
38044 if (REFERENCE_REF_P (decl))
38045 decl = TREE_OPERAND (decl, 0);
38047 for (int i = dims.length () - 1; i >= 0; i--)
38048 decl = grok_omp_array_section (loc, decl, dims[i].low_bound,
38049 dims[i].length);
38051 else if (TREE_CODE (decl) == INDIRECT_REF)
38053 bool ref_p = REFERENCE_REF_P (decl);
38055 /* If we have "*foo" and
38056 - it's an indirection of a reference, "unconvert" it, i.e.
38057 strip the indirection (to just "foo").
38058 - it's an indirection of a pointer, turn it into
38059 "foo[0:1]". */
38060 decl = TREE_OPERAND (decl, 0);
38061 STRIP_NOPS (decl);
38063 if (!ref_p)
38064 decl = grok_omp_array_section (loc, decl, integer_zero_node,
38065 integer_one_node);
38067 else if (TREE_CODE (decl) == ARRAY_REF)
38069 tree idx = TREE_OPERAND (decl, 1);
38071 decl = TREE_OPERAND (decl, 0);
38072 STRIP_NOPS (decl);
38074 decl = grok_omp_array_section (loc, decl, idx, integer_one_node);
38076 else if (TREE_CODE (decl) == NON_LVALUE_EXPR
38077 || CONVERT_EXPR_P (decl))
38078 decl = TREE_OPERAND (decl, 0);
38080 goto build_clause;
38082 token = cp_lexer_peek_token (parser->lexer);
38083 if (kind != 0
38084 && cp_parser_is_keyword (token, RID_THIS))
38086 decl = finish_this_expr ();
38087 if (TREE_CODE (decl) == NON_LVALUE_EXPR
38088 || CONVERT_EXPR_P (decl))
38089 decl = TREE_OPERAND (decl, 0);
38090 cp_lexer_consume_token (parser->lexer);
38092 else if (cp_parser_is_keyword (token, RID_FUNCTION_NAME)
38093 || cp_parser_is_keyword (token, RID_PRETTY_FUNCTION_NAME)
38094 || cp_parser_is_keyword (token, RID_C99_FUNCTION_NAME))
38096 cp_id_kind idk;
38097 decl = cp_parser_primary_expression (parser, false, false, false,
38098 &idk);
38100 else if (kind == OMP_CLAUSE_DEPEND
38101 && cp_parser_is_keyword (token, RID_OMP_ALL_MEMORY)
38102 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
38103 || cp_lexer_nth_token_is (parser->lexer, 2,
38104 CPP_CLOSE_PAREN)))
38106 decl = ridpointers[RID_OMP_ALL_MEMORY];
38107 cp_lexer_consume_token (parser->lexer);
38109 else
38111 name = cp_parser_id_expression (parser, /*template_p=*/false,
38112 /*check_dependency_p=*/true,
38113 /*template_p=*/NULL,
38114 /*declarator_p=*/false,
38115 /*optional_p=*/false);
38116 if (name == error_mark_node)
38118 if ((kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
38119 && cp_parser_simulate_error (parser))
38120 goto depend_lvalue;
38121 goto skip_comma;
38124 if (identifier_p (name))
38125 decl = cp_parser_lookup_name_simple (parser, name, token->location);
38126 else
38127 decl = name;
38128 if (decl == error_mark_node)
38130 if ((kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
38131 && cp_parser_simulate_error (parser))
38132 goto depend_lvalue;
38133 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
38134 token->location);
38137 if (outer_automatic_var_p (decl))
38138 decl = process_outer_var_ref (decl, tf_warning_or_error);
38139 if (decl == error_mark_node)
38141 else if (kind != 0)
38143 switch (kind)
38145 case OMP_CLAUSE__CACHE_:
38146 /* The OpenACC cache directive explicitly only allows "array
38147 elements or subarrays". */
38148 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
38150 error_at (token->location, "expected %<[%>");
38151 decl = error_mark_node;
38152 break;
38154 /* FALLTHROUGH. */
38155 case OMP_CLAUSE_MAP:
38156 case OMP_CLAUSE_FROM:
38157 case OMP_CLAUSE_TO:
38158 start_component_ref:
38159 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT)
38160 || cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
38162 cpp_ttype ttype
38163 = cp_lexer_next_token_is (parser->lexer, CPP_DOT)
38164 ? CPP_DOT : CPP_DEREF;
38165 location_t loc
38166 = cp_lexer_peek_token (parser->lexer)->location;
38167 cp_id_kind idk = CP_ID_KIND_NONE;
38168 cp_lexer_consume_token (parser->lexer);
38169 decl = convert_from_reference (decl);
38170 decl = (cp_parser_postfix_dot_deref_expression
38171 (parser, ttype, cp_expr (decl, token->location),
38172 false, &idk, loc));
38174 /* FALLTHROUGH. */
38175 case OMP_CLAUSE_AFFINITY:
38176 case OMP_CLAUSE_DEPEND:
38177 case OMP_CLAUSE_REDUCTION:
38178 case OMP_CLAUSE_IN_REDUCTION:
38179 case OMP_CLAUSE_TASK_REDUCTION:
38180 case OMP_CLAUSE_HAS_DEVICE_ADDR:
38181 array_section_p = false;
38182 dims.truncate (0);
38183 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
38185 location_t loc = UNKNOWN_LOCATION;
38186 tree low_bound = NULL_TREE, length = NULL_TREE;
38187 bool no_colon = false;
38189 parser->colon_corrects_to_scope_p = false;
38190 cp_lexer_consume_token (parser->lexer);
38191 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
38193 loc = cp_lexer_peek_token (parser->lexer)->location;
38194 low_bound = cp_parser_expression (parser);
38195 /* Later handling is not prepared to see through these. */
38196 gcc_checking_assert (!location_wrapper_p (low_bound));
38198 if (!colon)
38199 parser->colon_corrects_to_scope_p
38200 = saved_colon_corrects_to_scope_p;
38201 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
38203 length = integer_one_node;
38204 no_colon = true;
38206 else
38208 /* Look for `:'. */
38209 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
38211 if ((kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
38212 && cp_parser_simulate_error (parser))
38213 goto depend_lvalue;
38214 goto skip_comma;
38216 if (kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
38217 cp_parser_commit_to_tentative_parse (parser);
38218 else
38219 array_section_p = true;
38220 if (!cp_lexer_next_token_is (parser->lexer,
38221 CPP_CLOSE_SQUARE))
38223 length = cp_parser_expression (parser);
38224 /* Later handling is not prepared to see through these. */
38225 gcc_checking_assert (!location_wrapper_p (length));
38228 /* Look for the closing `]'. */
38229 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
38230 RT_CLOSE_SQUARE))
38232 if ((kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
38233 && cp_parser_simulate_error (parser))
38234 goto depend_lvalue;
38235 goto skip_comma;
38238 dims.safe_push (omp_dim (low_bound, length, loc, no_colon));
38241 if ((kind == OMP_CLAUSE_MAP
38242 || kind == OMP_CLAUSE_FROM
38243 || kind == OMP_CLAUSE_TO)
38244 && !array_section_p
38245 && (cp_lexer_next_token_is (parser->lexer, CPP_DOT)
38246 || cp_lexer_next_token_is (parser->lexer, CPP_DEREF)))
38248 for (unsigned i = 0; i < dims.length (); i++)
38250 gcc_assert (dims[i].length == integer_one_node);
38251 decl = build_array_ref (dims[i].loc,
38252 decl, dims[i].low_bound);
38254 goto start_component_ref;
38256 else
38257 for (unsigned i = 0; i < dims.length (); i++)
38258 decl = build_omp_array_section (input_location, decl,
38259 dims[i].low_bound,
38260 dims[i].length);
38261 break;
38262 default:
38263 break;
38266 if (kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
38268 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
38269 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
38270 && cp_parser_simulate_error (parser))
38272 depend_lvalue:
38273 cp_parser_abort_tentative_parse (parser);
38274 decl = cp_parser_assignment_expression (parser, NULL,
38275 false, false);
38277 else
38278 cp_parser_parse_definitely (parser);
38281 build_clause:
38282 tree u = build_omp_clause (token->location, kind);
38283 OMP_CLAUSE_DECL (u) = decl;
38284 OMP_CLAUSE_CHAIN (u) = list;
38285 list = u;
38287 else
38288 list = tree_cons (decl, NULL_TREE, list);
38290 get_comma:
38291 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
38292 break;
38293 cp_lexer_consume_token (parser->lexer);
38296 if (colon)
38297 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
38299 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
38301 *colon = true;
38302 cp_parser_require (parser, CPP_COLON, RT_COLON);
38303 return list;
38306 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
38308 int ending;
38310 /* Try to resync to an unnested comma. Copied from
38311 cp_parser_parenthesized_expression_list. */
38312 skip_comma:
38313 if (colon)
38314 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
38315 ending = cp_parser_skip_to_closing_parenthesis (parser,
38316 /*recovering=*/true,
38317 /*or_comma=*/true,
38318 /*consume_paren=*/true);
38319 if (ending < 0)
38320 goto get_comma;
38323 return list;
38326 /* Similarly, but expect leading and trailing parenthesis. This is a very
38327 common case for omp clauses. */
38329 static tree
38330 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list,
38331 bool map_lvalue = false)
38333 if (parser->lexer->in_omp_decl_attribute)
38335 if (kind)
38337 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38338 tree u = build_omp_clause (loc, kind);
38339 OMP_CLAUSE_DECL (u) = parser->lexer->in_omp_decl_attribute;
38340 OMP_CLAUSE_CHAIN (u) = list;
38341 return u;
38343 else
38344 return tree_cons (parser->lexer->in_omp_decl_attribute, NULL_TREE,
38345 list);
38348 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38349 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL,
38350 map_lvalue);
38351 return list;
38354 /* OpenACC 2.0:
38355 copy ( variable-list )
38356 copyin ( variable-list )
38357 copyout ( variable-list )
38358 create ( variable-list )
38359 delete ( variable-list )
38360 present ( variable-list )
38362 OpenACC 2.6:
38363 no_create ( variable-list )
38364 attach ( variable-list )
38365 detach ( variable-list ) */
38367 static tree
38368 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
38369 tree list)
38371 enum gomp_map_kind kind;
38372 switch (c_kind)
38374 case PRAGMA_OACC_CLAUSE_ATTACH:
38375 kind = GOMP_MAP_ATTACH;
38376 break;
38377 case PRAGMA_OACC_CLAUSE_COPY:
38378 kind = GOMP_MAP_TOFROM;
38379 break;
38380 case PRAGMA_OACC_CLAUSE_COPYIN:
38381 kind = GOMP_MAP_TO;
38382 break;
38383 case PRAGMA_OACC_CLAUSE_COPYOUT:
38384 kind = GOMP_MAP_FROM;
38385 break;
38386 case PRAGMA_OACC_CLAUSE_CREATE:
38387 kind = GOMP_MAP_ALLOC;
38388 break;
38389 case PRAGMA_OACC_CLAUSE_DELETE:
38390 kind = GOMP_MAP_RELEASE;
38391 break;
38392 case PRAGMA_OACC_CLAUSE_DETACH:
38393 kind = GOMP_MAP_DETACH;
38394 break;
38395 case PRAGMA_OACC_CLAUSE_DEVICE:
38396 kind = GOMP_MAP_FORCE_TO;
38397 break;
38398 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
38399 kind = GOMP_MAP_DEVICE_RESIDENT;
38400 break;
38401 case PRAGMA_OACC_CLAUSE_LINK:
38402 kind = GOMP_MAP_LINK;
38403 break;
38404 case PRAGMA_OACC_CLAUSE_NO_CREATE:
38405 kind = GOMP_MAP_IF_PRESENT;
38406 break;
38407 case PRAGMA_OACC_CLAUSE_PRESENT:
38408 kind = GOMP_MAP_FORCE_PRESENT;
38409 break;
38410 case PRAGMA_OACC_CLAUSE_SELF:
38411 /* "The 'host' clause is a synonym for the 'self' clause." */
38412 case PRAGMA_OACC_CLAUSE_HOST:
38413 kind = GOMP_MAP_FORCE_FROM;
38414 break;
38415 default:
38416 gcc_unreachable ();
38418 tree nl, c;
38419 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list, false);
38421 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
38422 OMP_CLAUSE_SET_MAP_KIND (c, kind);
38424 return nl;
38427 /* OpenACC 2.0:
38428 deviceptr ( variable-list ) */
38430 static tree
38431 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
38433 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38434 tree vars, t;
38436 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
38437 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
38438 variable-list must only allow for pointer variables. */
38439 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
38440 for (t = vars; t; t = TREE_CHAIN (t))
38442 tree v = TREE_PURPOSE (t);
38443 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
38444 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
38445 OMP_CLAUSE_DECL (u) = v;
38446 OMP_CLAUSE_CHAIN (u) = list;
38447 list = u;
38450 return list;
38453 /* OpenACC 2.5:
38454 auto
38455 finalize
38456 independent
38457 nohost
38458 seq */
38460 static tree
38461 cp_parser_oacc_simple_clause (location_t loc, enum omp_clause_code code,
38462 tree list)
38464 check_no_duplicate_clause (list, code, omp_clause_code_name[code], loc);
38466 tree c = build_omp_clause (loc, code);
38467 OMP_CLAUSE_CHAIN (c) = list;
38469 return c;
38472 /* OpenACC:
38473 num_gangs ( expression )
38474 num_workers ( expression )
38475 vector_length ( expression ) */
38477 static tree
38478 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
38479 const char *str, tree list)
38481 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38483 matching_parens parens;
38484 if (!parens.require_open (parser))
38485 return list;
38487 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
38489 if (t == error_mark_node
38490 || !parens.require_close (parser))
38492 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38493 /*or_comma=*/false,
38494 /*consume_paren=*/true);
38495 return list;
38498 check_no_duplicate_clause (list, code, str, loc);
38500 tree c = build_omp_clause (loc, code);
38501 OMP_CLAUSE_OPERAND (c, 0) = t;
38502 OMP_CLAUSE_CHAIN (c) = list;
38503 return c;
38506 /* OpenACC:
38508 gang [( gang-arg-list )]
38509 worker [( [num:] int-expr )]
38510 vector [( [length:] int-expr )]
38512 where gang-arg is one of:
38514 [num:] int-expr
38515 static: size-expr
38517 and size-expr may be:
38520 int-expr
38523 static tree
38524 cp_parser_oacc_shape_clause (cp_parser *parser, location_t loc,
38525 omp_clause_code kind,
38526 const char *str, tree list)
38528 const char *id = "num";
38529 cp_lexer *lexer = parser->lexer;
38530 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
38532 if (kind == OMP_CLAUSE_VECTOR)
38533 id = "length";
38535 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
38537 matching_parens parens;
38538 parens.consume_open (parser);
38542 cp_token *next = cp_lexer_peek_token (lexer);
38543 int idx = 0;
38545 /* Gang static argument. */
38546 if (kind == OMP_CLAUSE_GANG
38547 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
38549 cp_lexer_consume_token (lexer);
38551 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
38552 goto cleanup_error;
38554 idx = 1;
38555 if (ops[idx] != NULL)
38557 cp_parser_error (parser, "too many %<static%> arguments");
38558 goto cleanup_error;
38561 /* Check for the '*' argument. */
38562 if (cp_lexer_next_token_is (lexer, CPP_MULT)
38563 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
38564 || cp_lexer_nth_token_is (parser->lexer, 2,
38565 CPP_CLOSE_PAREN)))
38567 cp_lexer_consume_token (lexer);
38568 ops[idx] = integer_minus_one_node;
38570 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
38572 cp_lexer_consume_token (lexer);
38573 continue;
38575 else break;
38578 /* Worker num: argument and vector length: arguments. */
38579 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
38580 && id_equal (next->u.value, id)
38581 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
38583 cp_lexer_consume_token (lexer); /* id */
38584 cp_lexer_consume_token (lexer); /* ':' */
38587 /* Now collect the actual argument. */
38588 if (ops[idx] != NULL_TREE)
38590 cp_parser_error (parser, "unexpected argument");
38591 goto cleanup_error;
38594 tree expr = cp_parser_assignment_expression (parser, NULL, false,
38595 false);
38596 if (expr == error_mark_node)
38597 goto cleanup_error;
38599 mark_exp_read (expr);
38600 ops[idx] = expr;
38602 if (kind == OMP_CLAUSE_GANG
38603 && cp_lexer_next_token_is (lexer, CPP_COMMA))
38605 cp_lexer_consume_token (lexer);
38606 continue;
38608 break;
38610 while (1);
38612 if (!parens.require_close (parser))
38613 goto cleanup_error;
38616 check_no_duplicate_clause (list, kind, str, loc);
38618 c = build_omp_clause (loc, kind);
38620 if (ops[1])
38621 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
38623 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
38624 OMP_CLAUSE_CHAIN (c) = list;
38626 return c;
38628 cleanup_error:
38629 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
38630 return list;
38633 /* OpenACC 2.0:
38634 tile ( size-expr-list ) */
38636 static tree
38637 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
38639 tree c, expr = error_mark_node;
38640 tree tile = NULL_TREE;
38642 /* Collapse and tile are mutually exclusive. (The spec doesn't say
38643 so, but the spec authors never considered such a case and have
38644 differing opinions on what it might mean, including 'not
38645 allowed'.) */
38646 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
38647 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
38648 clause_loc);
38650 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38651 return list;
38655 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
38656 return list;
38658 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
38659 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
38660 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
38662 cp_lexer_consume_token (parser->lexer);
38663 expr = integer_zero_node;
38665 else
38666 expr = cp_parser_constant_expression (parser);
38668 tile = tree_cons (NULL_TREE, expr, tile);
38670 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
38672 /* Consume the trailing ')'. */
38673 cp_lexer_consume_token (parser->lexer);
38675 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
38676 tile = nreverse (tile);
38677 OMP_CLAUSE_TILE_LIST (c) = tile;
38678 OMP_CLAUSE_CHAIN (c) = list;
38679 return c;
38682 /* OpenACC 2.0
38683 Parse wait clause or directive parameters. */
38685 static tree
38686 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
38688 vec<tree, va_gc> *args;
38689 tree t, args_tree;
38691 args = cp_parser_parenthesized_expression_list (parser, non_attr,
38692 /*cast_p=*/false,
38693 /*allow_expansion_p=*/true,
38694 /*non_constant_p=*/NULL);
38696 if (args == NULL || args->length () == 0)
38698 if (args != NULL)
38700 cp_parser_error (parser, "expected integer expression list");
38701 release_tree_vector (args);
38703 return list;
38706 args_tree = build_tree_list_vec (args);
38708 release_tree_vector (args);
38710 for (t = args_tree; t; t = TREE_CHAIN (t))
38712 tree targ = TREE_VALUE (t);
38714 if (targ != error_mark_node)
38716 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
38717 error ("%<wait%> expression must be integral");
38718 else
38720 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
38722 targ = mark_rvalue_use (targ);
38723 OMP_CLAUSE_DECL (c) = targ;
38724 OMP_CLAUSE_CHAIN (c) = list;
38725 list = c;
38730 return list;
38733 /* OpenACC:
38734 wait [( int-expr-list )] */
38736 static tree
38737 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
38739 location_t location = cp_lexer_peek_token (parser->lexer)->location;
38741 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
38742 list = cp_parser_oacc_wait_list (parser, location, list);
38743 else
38745 tree c = build_omp_clause (location, OMP_CLAUSE_WAIT);
38747 OMP_CLAUSE_DECL (c) = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
38748 OMP_CLAUSE_CHAIN (c) = list;
38749 list = c;
38752 return list;
38755 /* OpenMP 3.0:
38756 collapse ( constant-expression ) */
38758 static tree
38759 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
38761 tree c, num;
38762 location_t loc;
38763 HOST_WIDE_INT n;
38765 loc = cp_lexer_peek_token (parser->lexer)->location;
38766 matching_parens parens;
38767 if (!parens.require_open (parser))
38768 return list;
38770 num = cp_parser_constant_expression (parser);
38772 if (!parens.require_close (parser))
38773 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38774 /*or_comma=*/false,
38775 /*consume_paren=*/true);
38777 if (num == error_mark_node)
38778 return list;
38779 num = fold_non_dependent_expr (num);
38780 if (!tree_fits_shwi_p (num)
38781 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
38782 || (n = tree_to_shwi (num)) <= 0
38783 || (int) n != n)
38785 error_at (loc, "collapse argument needs positive constant integer expression");
38786 return list;
38789 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
38790 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
38791 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
38792 OMP_CLAUSE_CHAIN (c) = list;
38793 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
38795 return c;
38798 /* OpenMP 2.5:
38799 default ( none | shared )
38801 OpenMP 5.1:
38802 default ( private | firstprivate )
38804 OpenACC:
38805 default ( none | present ) */
38807 static tree
38808 cp_parser_omp_clause_default (cp_parser *parser, tree list,
38809 location_t location, bool is_oacc)
38811 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
38812 tree c;
38814 matching_parens parens;
38815 if (!parens.require_open (parser))
38816 return list;
38817 if (!is_oacc && cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
38819 kind = OMP_CLAUSE_DEFAULT_PRIVATE;
38820 cp_lexer_consume_token (parser->lexer);
38822 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38824 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38825 const char *p = IDENTIFIER_POINTER (id);
38827 switch (p[0])
38829 case 'n':
38830 if (strcmp ("none", p) != 0)
38831 goto invalid_kind;
38832 kind = OMP_CLAUSE_DEFAULT_NONE;
38833 break;
38835 case 'p':
38836 if (strcmp ("present", p) != 0 || !is_oacc)
38837 goto invalid_kind;
38838 kind = OMP_CLAUSE_DEFAULT_PRESENT;
38839 break;
38841 case 'f':
38842 if (strcmp ("firstprivate", p) != 0 || is_oacc)
38843 goto invalid_kind;
38844 kind = OMP_CLAUSE_DEFAULT_FIRSTPRIVATE;
38845 break;
38847 case 's':
38848 if (strcmp ("shared", p) != 0 || is_oacc)
38849 goto invalid_kind;
38850 kind = OMP_CLAUSE_DEFAULT_SHARED;
38851 break;
38853 default:
38854 goto invalid_kind;
38857 cp_lexer_consume_token (parser->lexer);
38859 else
38861 invalid_kind:
38862 if (is_oacc)
38863 cp_parser_error (parser, "expected %<none%> or %<present%>");
38864 else
38865 cp_parser_error (parser, "expected %<none%>, %<shared%>, "
38866 "%<private%> or %<firstprivate%>");
38869 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
38870 || !parens.require_close (parser))
38871 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38872 /*or_comma=*/false,
38873 /*consume_paren=*/true);
38875 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
38876 return list;
38878 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
38879 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
38880 OMP_CLAUSE_CHAIN (c) = list;
38881 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
38883 return c;
38886 /* OpenMP 3.1:
38887 final ( expression ) */
38889 static tree
38890 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
38892 tree t, c;
38894 matching_parens parens;
38895 if (!parens.require_open (parser))
38896 return list;
38898 t = cp_parser_assignment_expression (parser);
38900 if (t == error_mark_node
38901 || !parens.require_close (parser))
38902 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38903 /*or_comma=*/false,
38904 /*consume_paren=*/true);
38906 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
38908 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
38909 OMP_CLAUSE_FINAL_EXPR (c) = t;
38910 OMP_CLAUSE_CHAIN (c) = list;
38912 return c;
38915 /* OpenMP 5.1:
38916 indirect [( expression )]
38919 static tree
38920 cp_parser_omp_clause_indirect (cp_parser *parser, tree list,
38921 location_t location)
38923 tree t;
38925 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
38927 matching_parens parens;
38928 if (!parens.require_open (parser))
38929 return list;
38931 bool non_constant_p;
38932 t = cp_parser_constant_expression (parser, true, &non_constant_p);
38934 if (t != error_mark_node && non_constant_p)
38935 error_at (location, "expected constant logical expression");
38937 if (t == error_mark_node
38938 || !parens.require_close (parser))
38939 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38940 /*or_comma=*/false,
38941 /*consume_paren=*/true);
38943 else
38944 t = integer_one_node;
38946 check_no_duplicate_clause (list, OMP_CLAUSE_INDIRECT, "indirect", location);
38948 tree c = build_omp_clause (location, OMP_CLAUSE_INDIRECT);
38949 OMP_CLAUSE_INDIRECT_EXPR (c) = t;
38950 OMP_CLAUSE_CHAIN (c) = list;
38952 return c;
38955 /* OpenMP 2.5:
38956 if ( expression )
38958 OpenMP 4.5:
38959 if ( directive-name-modifier : expression )
38961 directive-name-modifier:
38962 parallel | task | taskloop | target data | target | target update
38963 | target enter data | target exit data
38965 OpenMP 5.0:
38966 directive-name-modifier:
38967 ... | simd | cancel */
38969 static tree
38970 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
38971 bool is_omp)
38973 tree t, c;
38974 enum tree_code if_modifier = ERROR_MARK;
38976 matching_parens parens;
38977 if (!parens.require_open (parser))
38978 return list;
38980 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38982 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38983 const char *p = IDENTIFIER_POINTER (id);
38984 int n = 2;
38986 if (strcmp ("cancel", p) == 0)
38987 if_modifier = VOID_CST;
38988 else if (strcmp ("parallel", p) == 0)
38989 if_modifier = OMP_PARALLEL;
38990 else if (strcmp ("simd", p) == 0)
38991 if_modifier = OMP_SIMD;
38992 else if (strcmp ("task", p) == 0)
38993 if_modifier = OMP_TASK;
38994 else if (strcmp ("taskloop", p) == 0)
38995 if_modifier = OMP_TASKLOOP;
38996 else if (strcmp ("target", p) == 0)
38998 if_modifier = OMP_TARGET;
38999 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
39001 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
39002 p = IDENTIFIER_POINTER (id);
39003 if (strcmp ("data", p) == 0)
39004 if_modifier = OMP_TARGET_DATA;
39005 else if (strcmp ("update", p) == 0)
39006 if_modifier = OMP_TARGET_UPDATE;
39007 else if (strcmp ("enter", p) == 0)
39008 if_modifier = OMP_TARGET_ENTER_DATA;
39009 else if (strcmp ("exit", p) == 0)
39010 if_modifier = OMP_TARGET_EXIT_DATA;
39011 if (if_modifier != OMP_TARGET)
39012 n = 3;
39013 else
39015 location_t loc
39016 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
39017 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
39018 "or %<exit%>");
39019 if_modifier = ERROR_MARK;
39021 if (if_modifier == OMP_TARGET_ENTER_DATA
39022 || if_modifier == OMP_TARGET_EXIT_DATA)
39024 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
39026 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
39027 p = IDENTIFIER_POINTER (id);
39028 if (strcmp ("data", p) == 0)
39029 n = 4;
39031 if (n != 4)
39033 location_t loc
39034 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
39035 error_at (loc, "expected %<data%>");
39036 if_modifier = ERROR_MARK;
39041 if (if_modifier != ERROR_MARK)
39043 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
39045 while (n-- > 0)
39046 cp_lexer_consume_token (parser->lexer);
39048 else
39050 if (n > 2)
39052 location_t loc
39053 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
39054 error_at (loc, "expected %<:%>");
39056 if_modifier = ERROR_MARK;
39061 t = cp_parser_assignment_expression (parser);
39063 if (t == error_mark_node
39064 || !parens.require_close (parser))
39065 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39066 /*or_comma=*/false,
39067 /*consume_paren=*/true);
39069 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
39070 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
39072 if (if_modifier != ERROR_MARK
39073 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
39075 const char *p = NULL;
39076 switch (if_modifier)
39078 case VOID_CST: p = "cancel"; break;
39079 case OMP_PARALLEL: p = "parallel"; break;
39080 case OMP_SIMD: p = "simd"; break;
39081 case OMP_TASK: p = "task"; break;
39082 case OMP_TASKLOOP: p = "taskloop"; break;
39083 case OMP_TARGET_DATA: p = "target data"; break;
39084 case OMP_TARGET: p = "target"; break;
39085 case OMP_TARGET_UPDATE: p = "target update"; break;
39086 case OMP_TARGET_ENTER_DATA: p = "target enter data"; break;
39087 case OMP_TARGET_EXIT_DATA: p = "target exit data"; break;
39088 default: gcc_unreachable ();
39090 error_at (location, "too many %<if%> clauses with %qs modifier",
39092 return list;
39094 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
39096 if (!is_omp)
39097 error_at (location, "too many %<if%> clauses");
39098 else
39099 error_at (location, "too many %<if%> clauses without modifier");
39100 return list;
39102 else if (if_modifier == ERROR_MARK
39103 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
39105 error_at (location, "if any %<if%> clause has modifier, then all "
39106 "%<if%> clauses have to use modifier");
39107 return list;
39111 c = build_omp_clause (location, OMP_CLAUSE_IF);
39112 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
39113 OMP_CLAUSE_IF_EXPR (c) = t;
39114 OMP_CLAUSE_CHAIN (c) = list;
39116 return c;
39119 /* OpenMP 3.1:
39120 mergeable */
39122 static tree
39123 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
39124 tree list, location_t location)
39126 tree c;
39128 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
39129 location);
39131 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
39132 OMP_CLAUSE_CHAIN (c) = list;
39133 return c;
39136 /* OpenMP 2.5:
39137 nowait */
39139 static tree
39140 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
39141 tree list, location_t location)
39143 tree c;
39145 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
39147 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
39148 OMP_CLAUSE_CHAIN (c) = list;
39149 return c;
39152 /* OpenMP 2.5:
39153 num_threads ( expression ) */
39155 static tree
39156 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
39157 location_t location)
39159 tree t, c;
39161 matching_parens parens;
39162 if (!parens.require_open (parser))
39163 return list;
39165 t = cp_parser_assignment_expression (parser);
39167 if (t == error_mark_node
39168 || !parens.require_close (parser))
39169 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39170 /*or_comma=*/false,
39171 /*consume_paren=*/true);
39173 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
39174 "num_threads", location);
39176 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
39177 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
39178 OMP_CLAUSE_CHAIN (c) = list;
39180 return c;
39183 /* OpenMP 4.5:
39184 num_tasks ( expression )
39186 OpenMP 5.1:
39187 num_tasks ( strict : expression ) */
39189 static tree
39190 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
39191 location_t location)
39193 tree t, c;
39195 matching_parens parens;
39196 if (!parens.require_open (parser))
39197 return list;
39199 bool strict = false;
39200 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
39201 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
39203 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39204 if (!strcmp (IDENTIFIER_POINTER (id), "strict"))
39206 strict = true;
39207 cp_lexer_consume_token (parser->lexer);
39208 cp_lexer_consume_token (parser->lexer);
39212 t = cp_parser_assignment_expression (parser);
39214 if (t == error_mark_node
39215 || !parens.require_close (parser))
39216 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39217 /*or_comma=*/false,
39218 /*consume_paren=*/true);
39220 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
39221 "num_tasks", location);
39223 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
39224 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
39225 OMP_CLAUSE_NUM_TASKS_STRICT (c) = strict;
39226 OMP_CLAUSE_CHAIN (c) = list;
39228 return c;
39231 /* OpenMP 4.5:
39232 grainsize ( expression )
39234 OpenMP 5.1:
39235 grainsize ( strict : expression ) */
39237 static tree
39238 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
39239 location_t location)
39241 tree t, c;
39243 matching_parens parens;
39244 if (!parens.require_open (parser))
39245 return list;
39247 bool strict = false;
39248 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
39249 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
39251 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39252 if (!strcmp (IDENTIFIER_POINTER (id), "strict"))
39254 strict = true;
39255 cp_lexer_consume_token (parser->lexer);
39256 cp_lexer_consume_token (parser->lexer);
39260 t = cp_parser_assignment_expression (parser);
39262 if (t == error_mark_node
39263 || !parens.require_close (parser))
39264 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39265 /*or_comma=*/false,
39266 /*consume_paren=*/true);
39268 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
39269 "grainsize", location);
39271 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
39272 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
39273 OMP_CLAUSE_GRAINSIZE_STRICT (c) = strict;
39274 OMP_CLAUSE_CHAIN (c) = list;
39276 return c;
39279 /* OpenMP 4.5:
39280 priority ( expression ) */
39282 static tree
39283 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
39284 location_t location)
39286 tree t, c;
39288 matching_parens parens;
39289 if (!parens.require_open (parser))
39290 return list;
39292 t = cp_parser_assignment_expression (parser);
39294 if (t == error_mark_node
39295 || !parens.require_close (parser))
39296 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39297 /*or_comma=*/false,
39298 /*consume_paren=*/true);
39300 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
39301 "priority", location);
39303 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
39304 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
39305 OMP_CLAUSE_CHAIN (c) = list;
39307 return c;
39310 /* OpenMP 4.5:
39311 hint ( expression ) */
39313 static tree
39314 cp_parser_omp_clause_hint (cp_parser *parser, tree list, location_t location)
39316 tree t, c;
39318 matching_parens parens;
39319 if (!parens.require_open (parser))
39320 return list;
39322 t = cp_parser_assignment_expression (parser);
39324 if (t != error_mark_node)
39326 t = fold_non_dependent_expr (t);
39327 if (!value_dependent_expression_p (t)
39328 && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
39329 || !tree_fits_shwi_p (t)
39330 || tree_int_cst_sgn (t) == -1))
39331 error_at (location, "expected constant integer expression with "
39332 "valid sync-hint value");
39334 if (t == error_mark_node
39335 || !parens.require_close (parser))
39336 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39337 /*or_comma=*/false,
39338 /*consume_paren=*/true);
39339 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
39341 c = build_omp_clause (location, OMP_CLAUSE_HINT);
39342 OMP_CLAUSE_HINT_EXPR (c) = t;
39343 OMP_CLAUSE_CHAIN (c) = list;
39345 return c;
39348 /* OpenMP 5.1:
39349 filter ( integer-expression ) */
39351 static tree
39352 cp_parser_omp_clause_filter (cp_parser *parser, tree list, location_t location)
39354 tree t, c;
39356 matching_parens parens;
39357 if (!parens.require_open (parser))
39358 return list;
39360 t = cp_parser_assignment_expression (parser);
39362 if (t == error_mark_node
39363 || !parens.require_close (parser))
39364 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39365 /*or_comma=*/false,
39366 /*consume_paren=*/true);
39367 check_no_duplicate_clause (list, OMP_CLAUSE_FILTER, "filter", location);
39369 c = build_omp_clause (location, OMP_CLAUSE_FILTER);
39370 OMP_CLAUSE_FILTER_EXPR (c) = t;
39371 OMP_CLAUSE_CHAIN (c) = list;
39373 return c;
39376 /* OpenMP 4.5:
39377 defaultmap ( tofrom : scalar )
39379 OpenMP 5.0:
39380 defaultmap ( implicit-behavior [ : variable-category ] ) */
39382 static tree
39383 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
39384 location_t location)
39386 tree c, id;
39387 const char *p;
39388 enum omp_clause_defaultmap_kind behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
39389 enum omp_clause_defaultmap_kind category
39390 = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED;
39392 matching_parens parens;
39393 if (!parens.require_open (parser))
39394 return list;
39396 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
39397 p = "default";
39398 else if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39400 invalid_behavior:
39401 cp_parser_error (parser, "expected %<alloc%>, %<to%>, %<from%>, "
39402 "%<tofrom%>, %<firstprivate%>, %<none%> "
39403 "or %<default%>");
39404 goto out_err;
39406 else
39408 id = cp_lexer_peek_token (parser->lexer)->u.value;
39409 p = IDENTIFIER_POINTER (id);
39412 switch (p[0])
39414 case 'a':
39415 if (strcmp ("alloc", p) == 0)
39416 behavior = OMP_CLAUSE_DEFAULTMAP_ALLOC;
39417 else
39418 goto invalid_behavior;
39419 break;
39421 case 'd':
39422 if (strcmp ("default", p) == 0)
39423 behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
39424 else
39425 goto invalid_behavior;
39426 break;
39428 case 'f':
39429 if (strcmp ("firstprivate", p) == 0)
39430 behavior = OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE;
39431 else if (strcmp ("from", p) == 0)
39432 behavior = OMP_CLAUSE_DEFAULTMAP_FROM;
39433 else
39434 goto invalid_behavior;
39435 break;
39437 case 'n':
39438 if (strcmp ("none", p) == 0)
39439 behavior = OMP_CLAUSE_DEFAULTMAP_NONE;
39440 else
39441 goto invalid_behavior;
39442 break;
39444 case 'p':
39445 if (strcmp ("present", p) == 0)
39446 behavior = OMP_CLAUSE_DEFAULTMAP_PRESENT;
39447 else
39448 goto invalid_behavior;
39449 break;
39451 case 't':
39452 if (strcmp ("tofrom", p) == 0)
39453 behavior = OMP_CLAUSE_DEFAULTMAP_TOFROM;
39454 else if (strcmp ("to", p) == 0)
39455 behavior = OMP_CLAUSE_DEFAULTMAP_TO;
39456 else
39457 goto invalid_behavior;
39458 break;
39460 default:
39461 goto invalid_behavior;
39463 cp_lexer_consume_token (parser->lexer);
39465 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
39467 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
39468 goto out_err;
39470 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39472 invalid_category:
39473 cp_parser_error (parser, "expected %<scalar%>, %<aggregate%>, "
39474 "%<all%>");
39475 goto out_err;
39477 id = cp_lexer_peek_token (parser->lexer)->u.value;
39478 p = IDENTIFIER_POINTER (id);
39480 switch (p[0])
39482 case 'a':
39483 if (strcmp ("aggregate", p) == 0)
39484 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE;
39485 else if (strcmp ("all", p) == 0)
39486 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_ALL;
39487 else
39488 goto invalid_category;
39489 break;
39491 case 'p':
39492 if (strcmp ("pointer", p) == 0)
39493 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER;
39494 else
39495 goto invalid_category;
39496 break;
39498 case 's':
39499 if (strcmp ("scalar", p) == 0)
39500 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR;
39501 else
39502 goto invalid_category;
39503 break;
39505 default:
39506 goto invalid_category;
39509 cp_lexer_consume_token (parser->lexer);
39511 if (!parens.require_close (parser))
39512 goto out_err;
39514 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
39515 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEFAULTMAP
39516 && (category == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
39517 || category == OMP_CLAUSE_DEFAULTMAP_CATEGORY_ALL
39518 || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c) == category
39519 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c)
39520 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)
39521 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c)
39522 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_ALL)))
39524 enum omp_clause_defaultmap_kind cat = category;
39525 location_t loc = OMP_CLAUSE_LOCATION (c);
39526 if (cat == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
39527 || (cat == OMP_CLAUSE_DEFAULTMAP_CATEGORY_ALL
39528 && (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c)
39529 != OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)))
39530 cat = OMP_CLAUSE_DEFAULTMAP_CATEGORY (c);
39531 p = NULL;
39532 switch (cat)
39534 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED:
39535 p = NULL;
39536 break;
39537 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_ALL:
39538 p = "all";
39539 break;
39540 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE:
39541 p = "aggregate";
39542 break;
39543 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER:
39544 p = "pointer";
39545 break;
39546 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR:
39547 p = "scalar";
39548 break;
39549 default:
39550 gcc_unreachable ();
39552 if (p)
39553 error_at (loc, "too many %<defaultmap%> clauses with %qs category",
39555 else
39556 error_at (loc, "too many %<defaultmap%> clauses with unspecified "
39557 "category");
39558 break;
39561 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
39562 OMP_CLAUSE_DEFAULTMAP_SET_KIND (c, behavior, category);
39563 OMP_CLAUSE_CHAIN (c) = list;
39564 return c;
39566 out_err:
39567 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39568 /*or_comma=*/false,
39569 /*consume_paren=*/true);
39570 return list;
39573 /* OpenMP 5.0:
39574 order ( concurrent )
39576 OpenMP 5.1:
39577 order ( order-modifier : concurrent )
39579 order-modifier:
39580 reproducible
39581 unconstrained */
39583 static tree
39584 cp_parser_omp_clause_order (cp_parser *parser, tree list, location_t location)
39586 tree c, id;
39587 const char *p;
39588 bool unconstrained = false;
39589 bool reproducible = false;
39591 matching_parens parens;
39592 if (!parens.require_open (parser))
39593 return list;
39595 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
39596 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
39598 id = cp_lexer_peek_token (parser->lexer)->u.value;
39599 p = IDENTIFIER_POINTER (id);
39600 if (strcmp (p, "unconstrained") == 0)
39601 unconstrained = true;
39602 else if (strcmp (p, "reproducible") == 0)
39603 reproducible = true;
39604 else
39606 cp_parser_error (parser, "expected %<reproducible%> or "
39607 "%<unconstrained%>");
39608 goto out_err;
39610 cp_lexer_consume_token (parser->lexer);
39611 cp_lexer_consume_token (parser->lexer);
39613 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39615 cp_parser_error (parser, "expected %<concurrent%>");
39616 goto out_err;
39618 else
39620 id = cp_lexer_peek_token (parser->lexer)->u.value;
39621 p = IDENTIFIER_POINTER (id);
39623 if (strcmp (p, "concurrent") != 0)
39625 cp_parser_error (parser, "expected %<concurrent%>");
39626 goto out_err;
39628 cp_lexer_consume_token (parser->lexer);
39629 if (!parens.require_close (parser))
39630 goto out_err;
39632 check_no_duplicate_clause (list, OMP_CLAUSE_ORDER, "order", location);
39633 c = build_omp_clause (location, OMP_CLAUSE_ORDER);
39634 OMP_CLAUSE_ORDER_UNCONSTRAINED (c) = unconstrained;
39635 OMP_CLAUSE_ORDER_REPRODUCIBLE (c) = reproducible;
39636 OMP_CLAUSE_CHAIN (c) = list;
39637 return c;
39639 out_err:
39640 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39641 /*or_comma=*/false,
39642 /*consume_paren=*/true);
39643 return list;
39646 /* OpenMP 5.0:
39647 bind ( teams | parallel | thread ) */
39649 static tree
39650 cp_parser_omp_clause_bind (cp_parser *parser, tree list,
39651 location_t location)
39653 tree c;
39654 const char *p;
39655 enum omp_clause_bind_kind kind = OMP_CLAUSE_BIND_THREAD;
39657 matching_parens parens;
39658 if (!parens.require_open (parser))
39659 return list;
39661 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39663 invalid:
39664 cp_parser_error (parser,
39665 "expected %<teams%>, %<parallel%> or %<thread%>");
39666 goto out_err;
39668 else
39670 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39671 p = IDENTIFIER_POINTER (id);
39673 if (strcmp (p, "teams") == 0)
39674 kind = OMP_CLAUSE_BIND_TEAMS;
39675 else if (strcmp (p, "parallel") == 0)
39676 kind = OMP_CLAUSE_BIND_PARALLEL;
39677 else if (strcmp (p, "thread") != 0)
39678 goto invalid;
39679 cp_lexer_consume_token (parser->lexer);
39680 if (!parens.require_close (parser))
39681 goto out_err;
39683 /* check_no_duplicate_clause (list, OMP_CLAUSE_BIND, "bind", location); */
39684 c = build_omp_clause (location, OMP_CLAUSE_BIND);
39685 OMP_CLAUSE_BIND_KIND (c) = kind;
39686 OMP_CLAUSE_CHAIN (c) = list;
39687 return c;
39689 out_err:
39690 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39691 /*or_comma=*/false,
39692 /*consume_paren=*/true);
39693 return list;
39696 /* OpenMP 2.5:
39697 ordered
39699 OpenMP 4.5:
39700 ordered ( constant-expression ) */
39702 static tree
39703 cp_parser_omp_clause_ordered (cp_parser *parser,
39704 tree list, location_t location)
39706 tree c, num = NULL_TREE;
39707 HOST_WIDE_INT n;
39709 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
39710 "ordered", location);
39712 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
39714 matching_parens parens;
39715 parens.consume_open (parser);
39717 num = cp_parser_constant_expression (parser);
39719 if (!parens.require_close (parser))
39720 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39721 /*or_comma=*/false,
39722 /*consume_paren=*/true);
39724 if (num == error_mark_node)
39725 return list;
39726 num = fold_non_dependent_expr (num);
39727 if (!tree_fits_shwi_p (num)
39728 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
39729 || (n = tree_to_shwi (num)) <= 0
39730 || (int) n != n)
39732 error_at (location,
39733 "ordered argument needs positive constant integer "
39734 "expression");
39735 return list;
39739 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
39740 OMP_CLAUSE_ORDERED_EXPR (c) = num;
39741 OMP_CLAUSE_CHAIN (c) = list;
39742 return c;
39745 /* OpenMP 2.5:
39746 reduction ( reduction-operator : variable-list )
39748 reduction-operator:
39749 One of: + * - & ^ | && ||
39751 OpenMP 3.1:
39753 reduction-operator:
39754 One of: + * - & ^ | && || min max
39756 OpenMP 4.0:
39758 reduction-operator:
39759 One of: + * - & ^ | && ||
39760 id-expression
39762 OpenMP 5.0:
39763 reduction ( reduction-modifier, reduction-operator : variable-list )
39764 in_reduction ( reduction-operator : variable-list )
39765 task_reduction ( reduction-operator : variable-list ) */
39767 static tree
39768 cp_parser_omp_clause_reduction (cp_parser *parser, enum omp_clause_code kind,
39769 bool is_omp, tree list)
39771 enum tree_code code = ERROR_MARK;
39772 tree nlist, c, id = NULL_TREE;
39773 bool task = false;
39774 bool inscan = false;
39776 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
39777 return list;
39779 if (kind == OMP_CLAUSE_REDUCTION && is_omp)
39781 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT)
39782 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
39784 cp_lexer_consume_token (parser->lexer);
39785 cp_lexer_consume_token (parser->lexer);
39787 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
39788 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
39790 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39791 const char *p = IDENTIFIER_POINTER (id);
39792 if (strcmp (p, "task") == 0)
39793 task = true;
39794 else if (strcmp (p, "inscan") == 0)
39795 inscan = true;
39796 if (task || inscan)
39798 cp_lexer_consume_token (parser->lexer);
39799 cp_lexer_consume_token (parser->lexer);
39804 switch (cp_lexer_peek_token (parser->lexer)->type)
39806 case CPP_PLUS: code = PLUS_EXPR; break;
39807 case CPP_MULT: code = MULT_EXPR; break;
39808 case CPP_MINUS: code = MINUS_EXPR; break;
39809 case CPP_AND: code = BIT_AND_EXPR; break;
39810 case CPP_XOR: code = BIT_XOR_EXPR; break;
39811 case CPP_OR: code = BIT_IOR_EXPR; break;
39812 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
39813 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
39814 default: break;
39817 if (code != ERROR_MARK)
39818 cp_lexer_consume_token (parser->lexer);
39819 else
39821 bool saved_colon_corrects_to_scope_p;
39822 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
39823 parser->colon_corrects_to_scope_p = false;
39824 id = cp_parser_id_expression (parser, /*template_p=*/false,
39825 /*check_dependency_p=*/true,
39826 /*template_p=*/NULL,
39827 /*declarator_p=*/false,
39828 /*optional_p=*/false);
39829 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
39830 if (identifier_p (id))
39832 const char *p = IDENTIFIER_POINTER (id);
39834 if (strcmp (p, "min") == 0)
39835 code = MIN_EXPR;
39836 else if (strcmp (p, "max") == 0)
39837 code = MAX_EXPR;
39838 else if (id == ovl_op_identifier (false, PLUS_EXPR))
39839 code = PLUS_EXPR;
39840 else if (id == ovl_op_identifier (false, MULT_EXPR))
39841 code = MULT_EXPR;
39842 else if (id == ovl_op_identifier (false, MINUS_EXPR))
39843 code = MINUS_EXPR;
39844 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
39845 code = BIT_AND_EXPR;
39846 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
39847 code = BIT_IOR_EXPR;
39848 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
39849 code = BIT_XOR_EXPR;
39850 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
39851 code = TRUTH_ANDIF_EXPR;
39852 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
39853 code = TRUTH_ORIF_EXPR;
39854 id = omp_reduction_id (code, id, NULL_TREE);
39855 tree scope = parser->scope;
39856 if (scope)
39857 id = build_qualified_name (NULL_TREE, scope, id, false);
39858 parser->scope = NULL_TREE;
39859 parser->qualifying_scope = NULL_TREE;
39860 parser->object_scope = NULL_TREE;
39862 else
39864 error ("invalid reduction-identifier");
39865 resync_fail:
39866 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39867 /*or_comma=*/false,
39868 /*consume_paren=*/true);
39869 return list;
39873 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
39874 goto resync_fail;
39876 nlist = cp_parser_omp_var_list_no_open (parser, kind, list,
39877 NULL);
39878 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
39880 OMP_CLAUSE_REDUCTION_CODE (c) = code;
39881 if (task)
39882 OMP_CLAUSE_REDUCTION_TASK (c) = 1;
39883 else if (inscan)
39884 OMP_CLAUSE_REDUCTION_INSCAN (c) = 1;
39885 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
39888 return nlist;
39891 /* OpenMP 2.5:
39892 schedule ( schedule-kind )
39893 schedule ( schedule-kind , expression )
39895 schedule-kind:
39896 static | dynamic | guided | runtime | auto
39898 OpenMP 4.5:
39899 schedule ( schedule-modifier : schedule-kind )
39900 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
39902 schedule-modifier:
39903 simd
39904 monotonic
39905 nonmonotonic */
39907 static tree
39908 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
39910 tree c, t;
39911 int modifiers = 0, nmodifiers = 0;
39913 matching_parens parens;
39914 if (!parens.require_open (parser))
39915 return list;
39917 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
39919 location_t comma = UNKNOWN_LOCATION;
39920 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39922 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39923 const char *p = IDENTIFIER_POINTER (id);
39924 if (strcmp ("simd", p) == 0)
39925 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
39926 else if (strcmp ("monotonic", p) == 0)
39927 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
39928 else if (strcmp ("nonmonotonic", p) == 0)
39929 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
39930 else
39931 break;
39932 comma = UNKNOWN_LOCATION;
39933 cp_lexer_consume_token (parser->lexer);
39934 if (nmodifiers++ == 0
39935 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39937 comma = cp_lexer_peek_token (parser->lexer)->location;
39938 cp_lexer_consume_token (parser->lexer);
39940 else
39942 cp_parser_require (parser, CPP_COLON, RT_COLON);
39943 break;
39946 if (comma != UNKNOWN_LOCATION)
39947 error_at (comma, "expected %<:%>");
39949 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39951 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39952 const char *p = IDENTIFIER_POINTER (id);
39954 switch (p[0])
39956 case 'd':
39957 if (strcmp ("dynamic", p) != 0)
39958 goto invalid_kind;
39959 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
39960 break;
39962 case 'g':
39963 if (strcmp ("guided", p) != 0)
39964 goto invalid_kind;
39965 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
39966 break;
39968 case 'r':
39969 if (strcmp ("runtime", p) != 0)
39970 goto invalid_kind;
39971 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
39972 break;
39974 default:
39975 goto invalid_kind;
39978 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
39979 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
39980 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
39981 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
39982 else
39983 goto invalid_kind;
39984 cp_lexer_consume_token (parser->lexer);
39986 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
39987 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
39988 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
39989 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
39991 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
39992 "specified");
39993 modifiers = 0;
39996 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39998 cp_token *token;
39999 cp_lexer_consume_token (parser->lexer);
40001 token = cp_lexer_peek_token (parser->lexer);
40002 t = cp_parser_assignment_expression (parser);
40004 if (t == error_mark_node)
40005 goto resync_fail;
40006 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
40007 error_at (token->location, "schedule %<runtime%> does not take "
40008 "a %<chunk_size%> parameter");
40009 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
40010 error_at (token->location, "schedule %<auto%> does not take "
40011 "a %<chunk_size%> parameter");
40012 else
40013 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
40015 if (!parens.require_close (parser))
40016 goto resync_fail;
40018 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
40019 goto resync_fail;
40021 OMP_CLAUSE_SCHEDULE_KIND (c)
40022 = (enum omp_clause_schedule_kind)
40023 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
40025 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
40026 OMP_CLAUSE_CHAIN (c) = list;
40027 return c;
40029 invalid_kind:
40030 cp_parser_error (parser, "invalid schedule kind");
40031 resync_fail:
40032 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40033 /*or_comma=*/false,
40034 /*consume_paren=*/true);
40035 return list;
40038 /* OpenMP 3.0:
40039 untied */
40041 static tree
40042 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
40043 tree list, location_t location)
40045 tree c;
40047 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
40049 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
40050 OMP_CLAUSE_CHAIN (c) = list;
40051 return c;
40054 /* OpenMP 4.0:
40055 inbranch
40056 notinbranch */
40058 static tree
40059 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
40060 tree list, location_t location)
40062 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
40063 tree c = build_omp_clause (location, code);
40064 OMP_CLAUSE_CHAIN (c) = list;
40065 return c;
40068 /* OpenMP 4.0:
40069 parallel
40071 sections
40072 taskgroup */
40074 static tree
40075 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
40076 enum omp_clause_code code,
40077 tree list, location_t location)
40079 tree c = build_omp_clause (location, code);
40080 OMP_CLAUSE_CHAIN (c) = list;
40081 return c;
40084 /* OpenMP 4.5:
40085 nogroup */
40087 static tree
40088 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
40089 tree list, location_t location)
40091 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
40092 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
40093 OMP_CLAUSE_CHAIN (c) = list;
40094 return c;
40097 /* OpenMP 4.5:
40098 simd
40099 threads */
40101 static tree
40102 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
40103 enum omp_clause_code code,
40104 tree list, location_t location)
40106 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
40107 tree c = build_omp_clause (location, code);
40108 OMP_CLAUSE_CHAIN (c) = list;
40109 return c;
40112 /* OpenMP 4.0:
40113 num_teams ( expression )
40115 OpenMP 5.1:
40116 num_teams ( expression : expression ) */
40118 static tree
40119 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
40120 location_t location)
40122 tree upper, lower = NULL_TREE, c;
40124 matching_parens parens;
40125 if (!parens.require_open (parser))
40126 return list;
40128 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
40129 parser->colon_corrects_to_scope_p = false;
40130 upper = cp_parser_assignment_expression (parser);
40131 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
40133 if (upper != error_mark_node
40134 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
40136 lower = upper;
40137 cp_lexer_consume_token (parser->lexer);
40138 upper = cp_parser_assignment_expression (parser);
40141 if (upper == error_mark_node
40142 || !parens.require_close (parser))
40143 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40144 /*or_comma=*/false,
40145 /*consume_paren=*/true);
40147 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
40148 "num_teams", location);
40150 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
40151 OMP_CLAUSE_NUM_TEAMS_UPPER_EXPR (c) = upper;
40152 OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c) = lower;
40153 OMP_CLAUSE_CHAIN (c) = list;
40155 return c;
40158 /* OpenMP 4.0:
40159 thread_limit ( expression ) */
40161 static tree
40162 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
40163 location_t location)
40165 tree t, c;
40167 matching_parens parens;
40168 if (!parens.require_open (parser))
40169 return list;
40171 t = cp_parser_assignment_expression (parser);
40173 if (t == error_mark_node
40174 || !parens.require_close (parser))
40175 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40176 /*or_comma=*/false,
40177 /*consume_paren=*/true);
40179 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
40180 "thread_limit", location);
40182 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
40183 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
40184 OMP_CLAUSE_CHAIN (c) = list;
40186 return c;
40189 /* OpenMP 4.0:
40190 aligned ( variable-list )
40191 aligned ( variable-list : constant-expression ) */
40193 static tree
40194 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
40196 tree nlist, c, alignment = NULL_TREE;
40197 bool colon;
40199 matching_parens parens;
40200 if (!parens.require_open (parser))
40201 return list;
40203 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
40204 &colon);
40206 if (colon)
40208 alignment = cp_parser_constant_expression (parser);
40210 if (!parens.require_close (parser))
40211 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40212 /*or_comma=*/false,
40213 /*consume_paren=*/true);
40215 if (alignment == error_mark_node)
40216 alignment = NULL_TREE;
40219 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
40220 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
40222 return nlist;
40225 /* OpenMP 5.0:
40226 allocate ( variable-list )
40227 allocate ( expression : variable-list )
40229 OpenMP 5.1:
40230 allocate ( allocator-modifier : variable-list )
40231 allocate ( allocator-modifier , allocator-modifier : variable-list )
40233 allocator-modifier:
40234 allocator ( expression )
40235 align ( expression ) */
40237 static tree
40238 cp_parser_omp_clause_allocate (cp_parser *parser, tree list)
40240 tree nlist, c, allocator = NULL_TREE, align = NULL_TREE;
40241 bool colon, has_modifiers = false;
40243 matching_parens parens;
40244 if (!parens.require_open (parser))
40245 return list;
40247 cp_parser_parse_tentatively (parser);
40248 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
40249 parser->colon_corrects_to_scope_p = false;
40250 for (int mod = 0; mod < 2; mod++)
40251 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
40252 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
40254 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40255 const char *p = IDENTIFIER_POINTER (id);
40256 if (strcmp (p, "allocator") != 0 && strcmp (p, "align") != 0)
40257 break;
40258 cp_lexer_consume_token (parser->lexer);
40259 matching_parens parens2;
40260 if (!parens2.require_open (parser))
40261 break;
40262 if (strcmp (p, "allocator") == 0)
40264 if (allocator != NULL_TREE)
40265 break;
40266 allocator = cp_parser_assignment_expression (parser);
40268 else
40270 if (align != NULL_TREE)
40271 break;
40272 align = cp_parser_assignment_expression (parser);
40274 if (!parens2.require_close (parser))
40275 break;
40276 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
40278 has_modifiers = true;
40279 break;
40281 if (mod != 0 || cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
40282 break;
40283 cp_lexer_consume_token (parser->lexer);
40285 else
40286 break;
40287 if (!has_modifiers)
40289 cp_parser_abort_tentative_parse (parser);
40290 align = NULL_TREE;
40291 allocator = NULL_TREE;
40292 cp_parser_parse_tentatively (parser);
40293 allocator = cp_parser_assignment_expression (parser);
40295 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
40296 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
40298 cp_parser_parse_definitely (parser);
40299 cp_lexer_consume_token (parser->lexer);
40300 if (allocator == error_mark_node)
40301 allocator = NULL_TREE;
40302 if (align == error_mark_node)
40303 align = NULL_TREE;
40305 else
40307 cp_parser_abort_tentative_parse (parser);
40308 allocator = NULL_TREE;
40309 align = NULL_TREE;
40312 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALLOCATE, list,
40313 &colon);
40315 if (allocator || align)
40316 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
40318 OMP_CLAUSE_ALLOCATE_ALLOCATOR (c) = allocator;
40319 OMP_CLAUSE_ALLOCATE_ALIGN (c) = align;
40322 return nlist;
40325 /* OpenMP 2.5:
40326 lastprivate ( variable-list )
40328 OpenMP 5.0:
40329 lastprivate ( [ lastprivate-modifier : ] variable-list ) */
40331 static tree
40332 cp_parser_omp_clause_lastprivate (cp_parser *parser, tree list)
40334 bool conditional = false;
40336 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
40337 return list;
40339 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
40340 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
40342 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40343 const char *p = IDENTIFIER_POINTER (id);
40345 if (strcmp ("conditional", p) == 0)
40347 conditional = true;
40348 cp_lexer_consume_token (parser->lexer);
40349 cp_lexer_consume_token (parser->lexer);
40353 tree nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LASTPRIVATE,
40354 list, NULL);
40356 if (conditional)
40357 for (tree c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
40358 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 1;
40359 return nlist;
40362 /* OpenMP 4.0:
40363 linear ( variable-list )
40364 linear ( variable-list : expression )
40366 OpenMP 4.5:
40367 linear ( modifier ( variable-list ) )
40368 linear ( modifier ( variable-list ) : expression )
40370 modifier:
40373 uval
40375 OpenMP 5.2:
40376 linear ( variable-list : modifiers-list )
40378 modifiers:
40381 uval
40382 step ( expression ) */
40384 static tree
40385 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
40386 bool declare_simd)
40388 tree nlist, c, step = integer_one_node;
40389 bool colon;
40390 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
40391 bool old_linear_modifier = false;
40393 matching_parens parens;
40394 if (!parens.require_open (parser))
40395 return list;
40397 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40399 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40400 const char *p = IDENTIFIER_POINTER (id);
40402 if (strcmp ("ref", p) == 0)
40403 kind = OMP_CLAUSE_LINEAR_REF;
40404 else if (strcmp ("val", p) == 0)
40405 kind = OMP_CLAUSE_LINEAR_VAL;
40406 else if (strcmp ("uval", p) == 0)
40407 kind = OMP_CLAUSE_LINEAR_UVAL;
40408 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
40410 cp_lexer_consume_token (parser->lexer);
40411 old_linear_modifier = true;
40413 else
40414 kind = OMP_CLAUSE_LINEAR_DEFAULT;
40417 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
40418 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
40419 &colon);
40420 else
40422 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
40423 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
40424 if (colon)
40425 cp_parser_require (parser, CPP_COLON, RT_COLON);
40426 else if (!parens.require_close (parser))
40427 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40428 /*or_comma=*/false,
40429 /*consume_paren=*/true);
40432 if (colon)
40434 bool has_modifiers = false;
40435 if (kind == OMP_CLAUSE_LINEAR_DEFAULT
40436 && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40438 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40439 const char *p = IDENTIFIER_POINTER (id);
40440 size_t pos = 0;
40441 if (strcmp ("ref", p) == 0
40442 || strcmp ("val", p) == 0
40443 || strcmp ("uval", p) == 0)
40444 pos = 2;
40445 else if (strcmp ("step", p) == 0
40446 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
40448 pos = cp_parser_skip_balanced_tokens (parser, 2);
40449 if (pos == 2)
40450 pos = 0;
40452 if (pos != 0
40453 && (cp_lexer_nth_token_is (parser->lexer, pos, CPP_COMMA)
40454 || cp_lexer_nth_token_is (parser->lexer, pos,
40455 CPP_CLOSE_PAREN)))
40456 has_modifiers = true;
40459 step = NULL_TREE;
40460 if (has_modifiers)
40462 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40464 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40465 const char *p = IDENTIFIER_POINTER (id);
40466 enum omp_clause_linear_kind nkind = OMP_CLAUSE_LINEAR_DEFAULT;
40467 if (strcmp ("ref", p) == 0)
40468 nkind = OMP_CLAUSE_LINEAR_REF;
40469 else if (strcmp ("val", p) == 0)
40470 nkind = OMP_CLAUSE_LINEAR_VAL;
40471 else if (strcmp ("uval", p) == 0)
40472 nkind = OMP_CLAUSE_LINEAR_UVAL;
40473 if (nkind != OMP_CLAUSE_LINEAR_DEFAULT)
40475 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
40476 error_at (cp_lexer_peek_token (parser->lexer)->location,
40477 "multiple linear modifiers");
40478 kind = nkind;
40479 cp_lexer_consume_token (parser->lexer);
40481 else if (strcmp ("step", p) == 0)
40483 location_t step_loc
40484 = cp_lexer_peek_token (parser->lexer)->location;
40485 cp_lexer_consume_token (parser->lexer);
40486 matching_parens parens2;
40487 if (parens2.require_open (parser))
40489 if (step)
40490 error_at (step_loc, "multiple %<step%> modifiers");
40491 if (declare_simd
40492 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
40493 && cp_lexer_nth_token_is (parser->lexer, 2,
40494 CPP_CLOSE_PAREN))
40496 cp_token *token
40497 = cp_lexer_peek_token (parser->lexer);
40498 location_t tok_loc = token->location;
40499 cp_parser_parse_tentatively (parser);
40500 step = cp_parser_id_expression (parser, false, true,
40501 NULL, false, false);
40502 if (step != error_mark_node)
40503 step = cp_parser_lookup_name_simple (parser, step,
40504 tok_loc);
40505 if (step == error_mark_node)
40507 step = NULL_TREE;
40508 cp_parser_abort_tentative_parse (parser);
40510 else if (!cp_parser_parse_definitely (parser))
40511 step = NULL_TREE;
40513 if (!step)
40514 step = cp_parser_assignment_expression (parser);
40515 if (!parens2.require_close (parser))
40516 cp_parser_skip_to_closing_parenthesis (parser, true,
40517 false, true);
40519 else
40520 break;
40522 else
40523 break;
40524 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
40526 cp_lexer_consume_token (parser->lexer);
40527 continue;
40529 break;
40531 if (!step)
40532 step = integer_one_node;
40534 else if (declare_simd
40535 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
40536 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
40538 cp_token *token = cp_lexer_peek_token (parser->lexer);
40539 cp_parser_parse_tentatively (parser);
40540 step = cp_parser_id_expression (parser, /*template_p=*/false,
40541 /*check_dependency_p=*/true,
40542 /*template_p=*/NULL,
40543 /*declarator_p=*/false,
40544 /*optional_p=*/false);
40545 if (step != error_mark_node)
40546 step = cp_parser_lookup_name_simple (parser, step, token->location);
40547 if (step == error_mark_node)
40549 step = NULL_TREE;
40550 cp_parser_abort_tentative_parse (parser);
40552 else if (!cp_parser_parse_definitely (parser))
40553 step = NULL_TREE;
40555 if (!step)
40556 step = cp_parser_assignment_expression (parser);
40558 if (!parens.require_close (parser))
40559 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40560 /*or_comma=*/false,
40561 /*consume_paren=*/true);
40563 if (step == error_mark_node)
40564 return list;
40567 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
40569 OMP_CLAUSE_LINEAR_STEP (c) = step;
40570 OMP_CLAUSE_LINEAR_KIND (c) = kind;
40571 OMP_CLAUSE_LINEAR_OLD_LINEAR_MODIFIER (c) = old_linear_modifier;
40574 return nlist;
40577 /* OpenMP 4.0:
40578 safelen ( constant-expression ) */
40580 static tree
40581 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
40582 location_t location)
40584 tree t, c;
40586 matching_parens parens;
40587 if (!parens.require_open (parser))
40588 return list;
40590 t = cp_parser_constant_expression (parser);
40592 if (t == error_mark_node
40593 || !parens.require_close (parser))
40594 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40595 /*or_comma=*/false,
40596 /*consume_paren=*/true);
40598 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
40600 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
40601 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
40602 OMP_CLAUSE_CHAIN (c) = list;
40604 return c;
40607 /* OpenMP 4.0:
40608 simdlen ( constant-expression ) */
40610 static tree
40611 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
40612 location_t location)
40614 tree t, c;
40616 matching_parens parens;
40617 if (!parens.require_open (parser))
40618 return list;
40620 t = cp_parser_constant_expression (parser);
40622 if (t == error_mark_node
40623 || !parens.require_close (parser))
40624 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40625 /*or_comma=*/false,
40626 /*consume_paren=*/true);
40628 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
40630 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
40631 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
40632 OMP_CLAUSE_CHAIN (c) = list;
40634 return c;
40637 /* OpenMP 4.5:
40638 vec:
40639 identifier [+/- integer]
40640 vec , identifier [+/- integer]
40643 static tree
40644 cp_parser_omp_clause_doacross_sink (cp_parser *parser, location_t clause_loc,
40645 tree list, bool depend_p)
40647 tree vec = NULL;
40649 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
40651 cp_parser_error (parser, "expected identifier");
40652 return list;
40655 if (!depend_p)
40657 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40658 if (strcmp (IDENTIFIER_POINTER (id), "omp_cur_iteration") == 0
40659 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_MINUS)
40660 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_NUMBER)
40661 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_PAREN))
40663 tree val = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
40664 if (integer_onep (val))
40666 cp_lexer_consume_token (parser->lexer);
40667 cp_lexer_consume_token (parser->lexer);
40668 cp_lexer_consume_token (parser->lexer);
40669 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DOACROSS);
40670 OMP_CLAUSE_DOACROSS_KIND (u) = OMP_CLAUSE_DOACROSS_SINK;
40671 OMP_CLAUSE_CHAIN (u) = list;
40672 return u;
40677 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40679 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
40680 tree t, identifier = cp_parser_identifier (parser);
40681 tree addend = NULL;
40683 if (identifier == error_mark_node)
40684 t = error_mark_node;
40685 else
40687 t = cp_parser_lookup_name_simple
40688 (parser, identifier,
40689 cp_lexer_peek_token (parser->lexer)->location);
40690 if (t == error_mark_node)
40691 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
40692 id_loc);
40695 bool neg = false;
40696 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
40697 neg = true;
40698 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
40700 addend = integer_zero_node;
40701 goto add_to_vector;
40703 cp_lexer_consume_token (parser->lexer);
40705 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
40707 cp_parser_error (parser, "expected integer");
40708 return list;
40711 addend = cp_lexer_peek_token (parser->lexer)->u.value;
40712 if (TREE_CODE (addend) != INTEGER_CST)
40714 cp_parser_error (parser, "expected integer");
40715 return list;
40717 cp_lexer_consume_token (parser->lexer);
40719 add_to_vector:
40720 if (t != error_mark_node)
40722 vec = tree_cons (addend, t, vec);
40723 if (neg)
40724 OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (vec) = 1;
40727 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
40728 || !cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
40729 break;
40731 cp_lexer_consume_token (parser->lexer);
40734 if (vec)
40736 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DOACROSS);
40737 OMP_CLAUSE_DOACROSS_KIND (u) = OMP_CLAUSE_DOACROSS_SINK;
40738 OMP_CLAUSE_DOACROSS_DEPEND (u) = depend_p;
40739 OMP_CLAUSE_DECL (u) = nreverse (vec);
40740 OMP_CLAUSE_CHAIN (u) = list;
40741 return u;
40743 return list;
40746 /* OpenMP 5.0:
40747 detach ( event-handle ) */
40749 static tree
40750 cp_parser_omp_clause_detach (cp_parser *parser, tree list)
40752 matching_parens parens;
40754 if (!parens.require_open (parser))
40755 return list;
40757 cp_token *token;
40758 tree name, decl;
40760 token = cp_lexer_peek_token (parser->lexer);
40761 name = cp_parser_id_expression (parser, /*template_p=*/false,
40762 /*check_dependency_p=*/true,
40763 /*template_p=*/NULL,
40764 /*declarator_p=*/false,
40765 /*optional_p=*/false);
40766 if (name == error_mark_node)
40767 decl = error_mark_node;
40768 else
40770 if (identifier_p (name))
40771 decl = cp_parser_lookup_name_simple (parser, name, token->location);
40772 else
40773 decl = name;
40774 if (decl == error_mark_node)
40775 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
40776 token->location);
40779 if (decl == error_mark_node
40780 || !parens.require_close (parser))
40781 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40782 /*or_comma=*/false,
40783 /*consume_paren=*/true);
40785 tree u = build_omp_clause (token->location, OMP_CLAUSE_DETACH);
40786 OMP_CLAUSE_DECL (u) = decl;
40787 OMP_CLAUSE_CHAIN (u) = list;
40789 return u;
40792 /* OpenMP 5.0:
40793 iterators ( iterators-definition )
40795 iterators-definition:
40796 iterator-specifier
40797 iterator-specifier , iterators-definition
40799 iterator-specifier:
40800 identifier = range-specification
40801 iterator-type identifier = range-specification
40803 range-specification:
40804 begin : end
40805 begin : end : step */
40807 static tree
40808 cp_parser_omp_iterators (cp_parser *parser)
40810 tree ret = NULL_TREE, *last = &ret;
40811 cp_lexer_consume_token (parser->lexer);
40813 matching_parens parens;
40814 if (!parens.require_open (parser))
40815 return error_mark_node;
40817 bool saved_colon_corrects_to_scope_p
40818 = parser->colon_corrects_to_scope_p;
40819 bool saved_colon_doesnt_start_class_def_p
40820 = parser->colon_doesnt_start_class_def_p;
40824 tree iter_type;
40825 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
40826 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ))
40827 iter_type = integer_type_node;
40828 else
40830 const char *saved_message
40831 = parser->type_definition_forbidden_message;
40832 parser->type_definition_forbidden_message
40833 = G_("types may not be defined in iterator type");
40835 iter_type = cp_parser_type_id (parser);
40837 parser->type_definition_forbidden_message = saved_message;
40840 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
40841 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
40843 cp_parser_error (parser, "expected identifier");
40844 break;
40847 tree id = cp_parser_identifier (parser);
40848 if (id == error_mark_node)
40849 break;
40851 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
40852 break;
40854 parser->colon_corrects_to_scope_p = false;
40855 parser->colon_doesnt_start_class_def_p = true;
40856 tree begin = cp_parser_assignment_expression (parser);
40858 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
40859 break;
40861 tree end = cp_parser_assignment_expression (parser);
40863 tree step = integer_one_node;
40864 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
40866 cp_lexer_consume_token (parser->lexer);
40867 step = cp_parser_assignment_expression (parser);
40870 tree iter_var = build_decl (loc, VAR_DECL, id, iter_type);
40871 DECL_ARTIFICIAL (iter_var) = 1;
40872 DECL_CONTEXT (iter_var) = current_function_decl;
40873 pushdecl (iter_var);
40875 *last = make_tree_vec (6);
40876 TREE_VEC_ELT (*last, 0) = iter_var;
40877 TREE_VEC_ELT (*last, 1) = begin;
40878 TREE_VEC_ELT (*last, 2) = end;
40879 TREE_VEC_ELT (*last, 3) = step;
40880 last = &TREE_CHAIN (*last);
40882 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
40884 cp_lexer_consume_token (parser->lexer);
40885 continue;
40887 break;
40889 while (1);
40891 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
40892 parser->colon_doesnt_start_class_def_p
40893 = saved_colon_doesnt_start_class_def_p;
40895 if (!parens.require_close (parser))
40896 cp_parser_skip_to_closing_parenthesis (parser,
40897 /*recovering=*/true,
40898 /*or_comma=*/false,
40899 /*consume_paren=*/true);
40901 return ret ? ret : error_mark_node;
40904 /* OpenMP 5.0:
40905 affinity ( [aff-modifier :] variable-list )
40906 aff-modifier:
40907 iterator ( iterators-definition ) */
40909 static tree
40910 cp_parser_omp_clause_affinity (cp_parser *parser, tree list)
40912 tree nlist, c, iterators = NULL_TREE;
40914 matching_parens parens;
40915 if (!parens.require_open (parser))
40916 return list;
40918 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40920 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40921 const char *p = IDENTIFIER_POINTER (id);
40922 bool parse_iter = ((strcmp ("iterator", p) == 0)
40923 && (cp_lexer_nth_token_is (parser->lexer, 2,
40924 CPP_OPEN_PAREN)));
40925 if (parse_iter)
40927 size_t n = cp_parser_skip_balanced_tokens (parser, 2);
40928 parse_iter = cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON);
40930 if (parse_iter)
40932 begin_scope (sk_omp, NULL);
40933 iterators = cp_parser_omp_iterators (parser);
40934 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
40936 if (iterators)
40937 poplevel (0, 1, 0);
40938 cp_parser_skip_to_closing_parenthesis (parser,
40939 /*recovering=*/true,
40940 /*or_comma=*/false,
40941 /*consume_paren=*/true);
40942 return list;
40946 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_AFFINITY,
40947 list, NULL);
40948 if (iterators)
40950 tree block = poplevel (1, 1, 0);
40951 if (iterators != error_mark_node)
40953 TREE_VEC_ELT (iterators, 5) = block;
40954 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
40955 OMP_CLAUSE_DECL (c) = build_tree_list (iterators,
40956 OMP_CLAUSE_DECL (c));
40959 return nlist;
40962 /* OpenMP 4.0:
40963 depend ( depend-kind : variable-list )
40965 depend-kind:
40966 in | out | inout
40968 OpenMP 4.5:
40969 depend ( source )
40971 depend ( sink : vec )
40973 OpenMP 5.0:
40974 depend ( depend-modifier , depend-kind: variable-list )
40976 depend-kind:
40977 in | out | inout | mutexinoutset | depobj
40979 depend-modifier:
40980 iterator ( iterators-definition ) */
40982 static tree
40983 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
40985 tree nlist, c, iterators = NULL_TREE;
40986 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_LAST;
40987 enum omp_clause_doacross_kind dkind = OMP_CLAUSE_DOACROSS_LAST;
40989 matching_parens parens;
40990 if (!parens.require_open (parser))
40991 return list;
40995 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
40996 goto invalid_kind;
40998 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40999 const char *p = IDENTIFIER_POINTER (id);
41001 if (strcmp ("iterator", p) == 0 && iterators == NULL_TREE)
41003 begin_scope (sk_omp, NULL);
41004 iterators = cp_parser_omp_iterators (parser);
41005 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
41006 continue;
41008 if (strcmp ("in", p) == 0)
41009 kind = OMP_CLAUSE_DEPEND_IN;
41010 else if (strcmp ("inout", p) == 0)
41011 kind = OMP_CLAUSE_DEPEND_INOUT;
41012 else if (strcmp ("inoutset", p) == 0)
41013 kind = OMP_CLAUSE_DEPEND_INOUTSET;
41014 else if (strcmp ("mutexinoutset", p) == 0)
41015 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
41016 else if (strcmp ("out", p) == 0)
41017 kind = OMP_CLAUSE_DEPEND_OUT;
41018 else if (strcmp ("depobj", p) == 0)
41019 kind = OMP_CLAUSE_DEPEND_DEPOBJ;
41020 else if (strcmp ("sink", p) == 0)
41021 dkind = OMP_CLAUSE_DOACROSS_SINK;
41022 else if (strcmp ("source", p) == 0)
41023 dkind = OMP_CLAUSE_DOACROSS_SOURCE;
41024 else
41025 goto invalid_kind;
41026 break;
41028 while (1);
41030 cp_lexer_consume_token (parser->lexer);
41032 if (iterators
41033 && (dkind == OMP_CLAUSE_DOACROSS_SOURCE
41034 || dkind == OMP_CLAUSE_DOACROSS_SINK))
41036 poplevel (0, 1, 0);
41037 error_at (loc, "%<iterator%> modifier incompatible with %qs",
41038 dkind == OMP_CLAUSE_DOACROSS_SOURCE ? "source" : "sink");
41039 iterators = NULL_TREE;
41042 if (dkind == OMP_CLAUSE_DOACROSS_SOURCE)
41044 c = build_omp_clause (loc, OMP_CLAUSE_DOACROSS);
41045 OMP_CLAUSE_DOACROSS_KIND (c) = dkind;
41046 OMP_CLAUSE_DOACROSS_DEPEND (c) = 1;
41047 OMP_CLAUSE_DECL (c) = NULL_TREE;
41048 OMP_CLAUSE_CHAIN (c) = list;
41049 if (!parens.require_close (parser))
41050 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41051 /*or_comma=*/false,
41052 /*consume_paren=*/true);
41053 return c;
41056 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
41057 goto resync_fail;
41059 if (dkind == OMP_CLAUSE_DOACROSS_SINK)
41061 nlist = cp_parser_omp_clause_doacross_sink (parser, loc, list, true);
41062 if (!parens.require_close (parser))
41063 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41064 /*or_comma=*/false,
41065 /*consume_paren=*/true);
41067 else
41069 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
41070 list, NULL);
41072 if (iterators)
41074 tree block = poplevel (1, 1, 0);
41075 if (iterators == error_mark_node)
41076 iterators = NULL_TREE;
41077 else
41078 TREE_VEC_ELT (iterators, 5) = block;
41081 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
41083 OMP_CLAUSE_DEPEND_KIND (c) = kind;
41084 if (iterators)
41085 OMP_CLAUSE_DECL (c)
41086 = build_tree_list (iterators, OMP_CLAUSE_DECL (c));
41089 return nlist;
41091 invalid_kind:
41092 cp_parser_error (parser, "invalid depend kind");
41093 resync_fail:
41094 if (iterators)
41095 poplevel (0, 1, 0);
41096 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41097 /*or_comma=*/false,
41098 /*consume_paren=*/true);
41099 return list;
41102 /* OpenMP 5.2:
41103 doacross ( source : )
41104 doacross ( source : omp_cur_iteration )
41106 doacross ( sink : vec )
41107 doacross ( sink : omp_cur_iteration - logical_iteration ) */
41109 static tree
41110 cp_parser_omp_clause_doacross (cp_parser *parser, tree list, location_t loc)
41112 tree nlist;
41113 enum omp_clause_doacross_kind kind = OMP_CLAUSE_DOACROSS_LAST;
41115 matching_parens parens;
41116 if (!parens.require_open (parser))
41117 return list;
41119 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
41121 invalid_kind:
41122 cp_parser_error (parser, "invalid doacross kind");
41123 resync_fail:
41124 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41125 /*or_comma=*/false,
41126 /*consume_paren=*/true);
41127 return list;
41130 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41131 const char *p = IDENTIFIER_POINTER (id);
41133 if (strcmp ("sink", p) == 0)
41134 kind = OMP_CLAUSE_DOACROSS_SINK;
41135 else if (strcmp ("source", p) == 0)
41136 kind = OMP_CLAUSE_DOACROSS_SOURCE;
41137 else
41138 goto invalid_kind;
41140 cp_lexer_consume_token (parser->lexer);
41142 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
41143 goto resync_fail;
41145 if (kind == OMP_CLAUSE_DOACROSS_SOURCE)
41147 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41149 id = cp_lexer_peek_token (parser->lexer)->u.value;
41150 p = IDENTIFIER_POINTER (id);
41151 if (strcmp (p, "omp_cur_iteration") == 0)
41152 cp_lexer_consume_token (parser->lexer);
41154 nlist = build_omp_clause (loc, OMP_CLAUSE_DOACROSS);
41155 OMP_CLAUSE_DOACROSS_KIND (nlist) = OMP_CLAUSE_DOACROSS_SOURCE;
41156 OMP_CLAUSE_DECL (nlist) = NULL_TREE;
41157 OMP_CLAUSE_CHAIN (nlist) = list;
41159 else
41160 nlist = cp_parser_omp_clause_doacross_sink (parser, loc, list, false);
41162 if (!parens.require_close (parser))
41163 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41164 /*or_comma=*/false,
41165 /*consume_paren=*/true);
41166 return nlist;
41169 /* OpenMP 4.0:
41170 from ( variable-list )
41171 to ( variable-list )
41173 OpenMP 5.1:
41174 from ( [present :] variable-list )
41175 to ( [present :] variable-list ) */
41177 static tree
41178 cp_parser_omp_clause_from_to (cp_parser *parser, enum omp_clause_code kind,
41179 tree list)
41181 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
41182 return list;
41184 bool present = false;
41185 cp_token *token = cp_lexer_peek_token (parser->lexer);
41187 if (token->type == CPP_NAME
41188 && strcmp (IDENTIFIER_POINTER (token->u.value), "present") == 0
41189 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
41191 present = true;
41192 cp_lexer_consume_token (parser->lexer);
41193 cp_lexer_consume_token (parser->lexer);
41196 tree nl = cp_parser_omp_var_list_no_open (parser, kind, list, NULL, true);
41197 if (present)
41198 for (tree c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
41199 OMP_CLAUSE_MOTION_PRESENT (c) = 1;
41201 return nl;
41204 /* OpenMP 4.0:
41205 map ( map-kind : variable-list )
41206 map ( variable-list )
41208 map-kind:
41209 alloc | to | from | tofrom
41211 OpenMP 4.5:
41212 map-kind:
41213 alloc | to | from | tofrom | release | delete
41215 map ( always [,] map-kind: variable-list )
41217 OpenMP 5.0:
41218 map ( [map-type-modifier[,] ...] map-kind: variable-list )
41220 map-type-modifier:
41221 always | close */
41223 static tree
41224 cp_parser_omp_clause_map (cp_parser *parser, tree list)
41226 tree nlist, c;
41227 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
41229 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
41230 return list;
41232 int pos = 1;
41233 int map_kind_pos = 0;
41234 while (cp_lexer_peek_nth_token (parser->lexer, pos)->type == CPP_NAME
41235 || cp_lexer_peek_nth_token (parser->lexer, pos)->keyword == RID_DELETE)
41237 if (cp_lexer_peek_nth_token (parser->lexer, pos + 1)->type == CPP_COLON)
41239 map_kind_pos = pos;
41240 break;
41243 if (cp_lexer_peek_nth_token (parser->lexer, pos + 1)->type == CPP_COMMA)
41244 pos++;
41245 pos++;
41248 bool always_modifier = false;
41249 bool close_modifier = false;
41250 bool present_modifier = false;
41251 for (int pos = 1; pos < map_kind_pos; ++pos)
41253 cp_token *tok = cp_lexer_peek_token (parser->lexer);
41254 if (tok->type == CPP_COMMA)
41256 cp_lexer_consume_token (parser->lexer);
41257 continue;
41260 const char *p = IDENTIFIER_POINTER (tok->u.value);
41261 if (strcmp ("always", p) == 0)
41263 if (always_modifier)
41265 cp_parser_error (parser, "too many %<always%> modifiers");
41266 cp_parser_skip_to_closing_parenthesis (parser,
41267 /*recovering=*/true,
41268 /*or_comma=*/false,
41269 /*consume_paren=*/true);
41270 return list;
41272 always_modifier = true;
41274 else if (strcmp ("close", p) == 0)
41276 if (close_modifier)
41278 cp_parser_error (parser, "too many %<close%> modifiers");
41279 cp_parser_skip_to_closing_parenthesis (parser,
41280 /*recovering=*/true,
41281 /*or_comma=*/false,
41282 /*consume_paren=*/true);
41283 return list;
41285 close_modifier = true;
41287 else if (strcmp ("present", p) == 0)
41289 if (present_modifier)
41291 cp_parser_error (parser, "too many %<present%> modifiers");
41292 cp_parser_skip_to_closing_parenthesis (parser,
41293 /*recovering=*/true,
41294 /*or_comma=*/false,
41295 /*consume_paren=*/true);
41296 return list;
41298 present_modifier = true;
41300 else
41302 cp_parser_error (parser, "%<map%> clause with map-type modifier other"
41303 " than %<always%>, %<close%> or %<present%>");
41304 cp_parser_skip_to_closing_parenthesis (parser,
41305 /*recovering=*/true,
41306 /*or_comma=*/false,
41307 /*consume_paren=*/true);
41308 return list;
41311 cp_lexer_consume_token (parser->lexer);
41314 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
41315 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
41317 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41318 const char *p = IDENTIFIER_POINTER (id);
41319 int always_present_modifier = always_modifier && present_modifier;
41321 if (strcmp ("alloc", p) == 0)
41322 kind = present_modifier ? GOMP_MAP_PRESENT_ALLOC : GOMP_MAP_ALLOC;
41323 else if (strcmp ("to", p) == 0)
41324 kind = (always_present_modifier ? GOMP_MAP_ALWAYS_PRESENT_TO
41325 : present_modifier ? GOMP_MAP_PRESENT_TO
41326 : always_modifier ? GOMP_MAP_ALWAYS_TO
41327 : GOMP_MAP_TO);
41328 else if (strcmp ("from", p) == 0)
41329 kind = (always_present_modifier ? GOMP_MAP_ALWAYS_PRESENT_FROM
41330 : present_modifier ? GOMP_MAP_PRESENT_FROM
41331 : always_modifier ? GOMP_MAP_ALWAYS_FROM
41332 : GOMP_MAP_FROM);
41333 else if (strcmp ("tofrom", p) == 0)
41334 kind = (always_present_modifier ? GOMP_MAP_ALWAYS_PRESENT_TOFROM
41335 : present_modifier ? GOMP_MAP_PRESENT_TOFROM
41336 : always_modifier ? GOMP_MAP_ALWAYS_TOFROM
41337 : GOMP_MAP_TOFROM);
41338 else if (strcmp ("release", p) == 0)
41339 kind = GOMP_MAP_RELEASE;
41340 else
41342 cp_parser_error (parser, "invalid map kind");
41343 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41344 /*or_comma=*/false,
41345 /*consume_paren=*/true);
41346 return list;
41348 cp_lexer_consume_token (parser->lexer);
41349 cp_lexer_consume_token (parser->lexer);
41351 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
41352 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
41354 kind = GOMP_MAP_DELETE;
41355 cp_lexer_consume_token (parser->lexer);
41356 cp_lexer_consume_token (parser->lexer);
41359 /* We introduce a scope here so that errors parsing e.g. "always", "close"
41360 tokens do not propagate to later directives that might use them
41361 legally. */
41362 begin_scope (sk_omp, NULL);
41363 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
41364 NULL, true);
41365 finish_scope ();
41367 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
41368 OMP_CLAUSE_SET_MAP_KIND (c, kind);
41370 return nlist;
41373 /* OpenMP 4.0:
41374 device ( expression )
41376 OpenMP 5.0:
41377 device ( [device-modifier :] integer-expression )
41379 device-modifier:
41380 ancestor | device_num */
41382 static tree
41383 cp_parser_omp_clause_device (cp_parser *parser, tree list,
41384 location_t location)
41386 tree t, c;
41387 bool ancestor = false;
41389 matching_parens parens;
41390 if (!parens.require_open (parser))
41391 return list;
41393 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
41394 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
41396 cp_token *tok = cp_lexer_peek_token (parser->lexer);
41397 const char *p = IDENTIFIER_POINTER (tok->u.value);
41398 if (strcmp ("ancestor", p) == 0)
41400 ancestor = true;
41402 /* A requires directive with the reverse_offload clause must be
41403 specified. */
41404 if ((omp_requires_mask & OMP_REQUIRES_REVERSE_OFFLOAD) == 0)
41406 error_at (tok->location, "%<ancestor%> device modifier not "
41407 "preceded by %<requires%> directive "
41408 "with %<reverse_offload%> clause");
41409 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
41410 return list;
41413 else if (strcmp ("device_num", p) == 0)
41415 else
41417 error_at (tok->location, "expected %<ancestor%> or %<device_num%>");
41418 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
41419 return list;
41421 cp_lexer_consume_token (parser->lexer);
41422 cp_lexer_consume_token (parser->lexer);
41425 t = cp_parser_assignment_expression (parser);
41427 if (t == error_mark_node
41428 || !parens.require_close (parser))
41429 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41430 /*or_comma=*/false,
41431 /*consume_paren=*/true);
41433 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
41434 "device", location);
41436 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
41437 OMP_CLAUSE_DEVICE_ID (c) = t;
41438 OMP_CLAUSE_CHAIN (c) = list;
41439 OMP_CLAUSE_DEVICE_ANCESTOR (c) = ancestor;
41441 return c;
41444 /* OpenMP 4.0:
41445 dist_schedule ( static )
41446 dist_schedule ( static , expression ) */
41448 static tree
41449 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
41450 location_t location)
41452 tree c, t;
41454 matching_parens parens;
41455 if (!parens.require_open (parser))
41456 return list;
41458 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
41460 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
41461 goto invalid_kind;
41462 cp_lexer_consume_token (parser->lexer);
41464 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
41466 cp_lexer_consume_token (parser->lexer);
41468 t = cp_parser_assignment_expression (parser);
41470 if (t == error_mark_node)
41471 goto resync_fail;
41472 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
41474 if (!parens.require_close (parser))
41475 goto resync_fail;
41477 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
41478 goto resync_fail;
41480 /* check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE,
41481 "dist_schedule", location); */
41482 if (omp_find_clause (list, OMP_CLAUSE_DIST_SCHEDULE))
41483 warning_at (location, OPT_Wopenmp, "too many %qs clauses", "dist_schedule");
41484 OMP_CLAUSE_CHAIN (c) = list;
41485 return c;
41487 invalid_kind:
41488 cp_parser_error (parser, "invalid dist_schedule kind");
41489 resync_fail:
41490 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41491 /*or_comma=*/false,
41492 /*consume_paren=*/true);
41493 return list;
41496 /* OpenMP 4.0:
41497 proc_bind ( proc-bind-kind )
41499 proc-bind-kind:
41500 primary | master | close | spread
41501 where OpenMP 5.1 added 'primary' and deprecated the alias 'master'. */
41503 static tree
41504 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
41505 location_t location)
41507 tree c;
41508 enum omp_clause_proc_bind_kind kind;
41510 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
41511 return list;
41513 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41515 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41516 const char *p = IDENTIFIER_POINTER (id);
41518 if (strcmp ("primary", p) == 0)
41519 kind = OMP_CLAUSE_PROC_BIND_PRIMARY;
41520 else if (strcmp ("master", p) == 0)
41521 kind = OMP_CLAUSE_PROC_BIND_MASTER;
41522 else if (strcmp ("close", p) == 0)
41523 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
41524 else if (strcmp ("spread", p) == 0)
41525 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
41526 else
41527 goto invalid_kind;
41529 else
41530 goto invalid_kind;
41532 cp_lexer_consume_token (parser->lexer);
41533 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
41534 goto resync_fail;
41536 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
41537 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
41538 location);
41539 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
41540 OMP_CLAUSE_CHAIN (c) = list;
41541 return c;
41543 invalid_kind:
41544 cp_parser_error (parser, "invalid depend kind");
41545 resync_fail:
41546 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41547 /*or_comma=*/false,
41548 /*consume_paren=*/true);
41549 return list;
41552 /* OpenMP 5.0:
41553 device_type ( host | nohost | any ) */
41555 static tree
41556 cp_parser_omp_clause_device_type (cp_parser *parser, tree list,
41557 location_t location)
41559 tree c;
41560 enum omp_clause_device_type_kind kind;
41562 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
41563 return list;
41565 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41567 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41568 const char *p = IDENTIFIER_POINTER (id);
41570 if (strcmp ("host", p) == 0)
41571 kind = OMP_CLAUSE_DEVICE_TYPE_HOST;
41572 else if (strcmp ("nohost", p) == 0)
41573 kind = OMP_CLAUSE_DEVICE_TYPE_NOHOST;
41574 else if (strcmp ("any", p) == 0)
41575 kind = OMP_CLAUSE_DEVICE_TYPE_ANY;
41576 else
41577 goto invalid_kind;
41579 else
41580 goto invalid_kind;
41582 cp_lexer_consume_token (parser->lexer);
41583 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
41584 goto resync_fail;
41586 c = build_omp_clause (location, OMP_CLAUSE_DEVICE_TYPE);
41587 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE_TYPE, "device_type",
41588 location);
41589 OMP_CLAUSE_DEVICE_TYPE_KIND (c) = kind;
41590 OMP_CLAUSE_CHAIN (c) = list;
41591 return c;
41593 invalid_kind:
41594 cp_parser_error (parser, "invalid depend kind");
41595 resync_fail:
41596 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41597 /*or_comma=*/false,
41598 /*consume_paren=*/true);
41599 return list;
41602 /* OpenACC:
41603 async [( int-expr )] */
41605 static tree
41606 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
41608 tree c, t;
41609 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
41611 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
41613 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
41615 matching_parens parens;
41616 parens.consume_open (parser);
41618 t = cp_parser_assignment_expression (parser);
41619 if (t == error_mark_node
41620 || !parens.require_close (parser))
41621 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41622 /*or_comma=*/false,
41623 /*consume_paren=*/true);
41626 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
41628 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
41629 OMP_CLAUSE_ASYNC_EXPR (c) = t;
41630 OMP_CLAUSE_CHAIN (c) = list;
41631 list = c;
41633 return list;
41636 /* OpenACC 2.7:
41637 self [( expression )] */
41639 static tree
41640 cp_parser_oacc_compute_clause_self (cp_parser *parser, tree list)
41642 tree t;
41643 location_t location = cp_lexer_peek_token (parser->lexer)->location;
41644 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
41646 matching_parens parens;
41647 parens.consume_open (parser);
41648 t = cp_parser_assignment_expression (parser);
41649 if (t == error_mark_node
41650 || !parens.require_close (parser))
41652 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41653 /*or_comma=*/false,
41654 /*consume_paren=*/true);
41655 return list;
41658 else
41659 t = truthvalue_true_node;
41661 for (tree c = list; c; c = OMP_CLAUSE_CHAIN (c))
41662 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SELF)
41664 error_at (location, "too many %<self%> clauses");
41665 return list;
41668 tree c = build_omp_clause (location, OMP_CLAUSE_SELF);
41669 OMP_CLAUSE_SELF_EXPR (c) = t;
41670 OMP_CLAUSE_CHAIN (c) = list;
41671 return c;
41674 /* Parse all OpenACC clauses. The set clauses allowed by the directive
41675 is a bitmask in MASK. Return the list of clauses found. */
41677 static tree
41678 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
41679 const char *where, cp_token *pragma_tok,
41680 bool finish_p = true, bool target_p = false)
41682 tree clauses = NULL;
41683 bool first = true;
41685 /* Don't create location wrapper nodes within OpenACC clauses. */
41686 auto_suppress_location_wrappers sentinel;
41688 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
41690 location_t here;
41691 pragma_omp_clause c_kind;
41692 omp_clause_code code;
41693 const char *c_name;
41694 tree prev = clauses;
41696 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
41697 cp_lexer_consume_token (parser->lexer);
41699 here = cp_lexer_peek_token (parser->lexer)->location;
41700 c_kind = cp_parser_omp_clause_name (parser);
41702 switch (c_kind)
41704 case PRAGMA_OACC_CLAUSE_ASYNC:
41705 clauses = cp_parser_oacc_clause_async (parser, clauses);
41706 c_name = "async";
41707 break;
41708 case PRAGMA_OACC_CLAUSE_AUTO:
41709 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_AUTO,
41710 clauses);
41711 c_name = "auto";
41712 break;
41713 case PRAGMA_OACC_CLAUSE_ATTACH:
41714 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41715 c_name = "attach";
41716 break;
41717 case PRAGMA_OACC_CLAUSE_COLLAPSE:
41718 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
41719 c_name = "collapse";
41720 break;
41721 case PRAGMA_OACC_CLAUSE_COPY:
41722 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41723 c_name = "copy";
41724 break;
41725 case PRAGMA_OACC_CLAUSE_COPYIN:
41726 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41727 c_name = "copyin";
41728 break;
41729 case PRAGMA_OACC_CLAUSE_COPYOUT:
41730 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41731 c_name = "copyout";
41732 break;
41733 case PRAGMA_OACC_CLAUSE_CREATE:
41734 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41735 c_name = "create";
41736 break;
41737 case PRAGMA_OACC_CLAUSE_DELETE:
41738 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41739 c_name = "delete";
41740 break;
41741 case PRAGMA_OMP_CLAUSE_DEFAULT:
41742 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
41743 c_name = "default";
41744 break;
41745 case PRAGMA_OACC_CLAUSE_DETACH:
41746 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41747 c_name = "detach";
41748 break;
41749 case PRAGMA_OACC_CLAUSE_DEVICE:
41750 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41751 c_name = "device";
41752 break;
41753 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
41754 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
41755 c_name = "deviceptr";
41756 break;
41757 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
41758 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41759 c_name = "device_resident";
41760 break;
41761 case PRAGMA_OACC_CLAUSE_FINALIZE:
41762 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_FINALIZE,
41763 clauses);
41764 c_name = "finalize";
41765 break;
41766 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
41767 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
41768 clauses);
41769 c_name = "firstprivate";
41770 break;
41771 case PRAGMA_OACC_CLAUSE_GANG:
41772 c_name = "gang";
41773 clauses = cp_parser_oacc_shape_clause (parser, here, OMP_CLAUSE_GANG,
41774 c_name, clauses);
41775 break;
41776 case PRAGMA_OACC_CLAUSE_HOST:
41777 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41778 c_name = "host";
41779 break;
41780 case PRAGMA_OACC_CLAUSE_IF:
41781 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
41782 c_name = "if";
41783 break;
41784 case PRAGMA_OACC_CLAUSE_IF_PRESENT:
41785 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_IF_PRESENT,
41786 clauses);
41787 c_name = "if_present";
41788 break;
41789 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
41790 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_INDEPENDENT,
41791 clauses);
41792 c_name = "independent";
41793 break;
41794 case PRAGMA_OACC_CLAUSE_LINK:
41795 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41796 c_name = "link";
41797 break;
41798 case PRAGMA_OACC_CLAUSE_NO_CREATE:
41799 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41800 c_name = "no_create";
41801 break;
41802 case PRAGMA_OACC_CLAUSE_NOHOST:
41803 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_NOHOST,
41804 clauses);
41805 c_name = "nohost";
41806 break;
41807 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
41808 code = OMP_CLAUSE_NUM_GANGS;
41809 c_name = "num_gangs";
41810 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
41811 clauses);
41812 break;
41813 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
41814 c_name = "num_workers";
41815 code = OMP_CLAUSE_NUM_WORKERS;
41816 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
41817 clauses);
41818 break;
41819 case PRAGMA_OACC_CLAUSE_PRESENT:
41820 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41821 c_name = "present";
41822 break;
41823 case PRAGMA_OACC_CLAUSE_PRIVATE:
41824 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
41825 clauses);
41826 c_name = "private";
41827 break;
41828 case PRAGMA_OACC_CLAUSE_REDUCTION:
41829 clauses
41830 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
41831 false, clauses);
41832 c_name = "reduction";
41833 break;
41834 case PRAGMA_OACC_CLAUSE_SELF:
41835 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST)) == 0)
41836 /* OpenACC compute construct */
41837 clauses = cp_parser_oacc_compute_clause_self (parser, clauses);
41838 else
41839 /* OpenACC 'update' directive */
41840 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41841 c_name = "self";
41842 break;
41843 case PRAGMA_OACC_CLAUSE_SEQ:
41844 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_SEQ,
41845 clauses);
41846 c_name = "seq";
41847 break;
41848 case PRAGMA_OACC_CLAUSE_TILE:
41849 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
41850 c_name = "tile";
41851 break;
41852 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
41853 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
41854 clauses);
41855 c_name = "use_device";
41856 break;
41857 case PRAGMA_OACC_CLAUSE_VECTOR:
41858 c_name = "vector";
41859 clauses = cp_parser_oacc_shape_clause (parser, here,
41860 OMP_CLAUSE_VECTOR,
41861 c_name, clauses);
41862 break;
41863 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
41864 c_name = "vector_length";
41865 code = OMP_CLAUSE_VECTOR_LENGTH;
41866 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
41867 clauses);
41868 break;
41869 case PRAGMA_OACC_CLAUSE_WAIT:
41870 clauses = cp_parser_oacc_clause_wait (parser, clauses);
41871 c_name = "wait";
41872 break;
41873 case PRAGMA_OACC_CLAUSE_WORKER:
41874 c_name = "worker";
41875 clauses = cp_parser_oacc_shape_clause (parser, here,
41876 OMP_CLAUSE_WORKER,
41877 c_name, clauses);
41878 break;
41879 default:
41880 cp_parser_error (parser, "expected an OpenACC clause");
41881 goto saw_error;
41884 first = false;
41886 if (((mask >> c_kind) & 1) == 0)
41888 /* Remove the invalid clause(s) from the list to avoid
41889 confusing the rest of the compiler. */
41890 clauses = prev;
41891 error_at (here, "%qs is not valid for %qs", c_name, where);
41895 saw_error:
41896 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41898 if (finish_p)
41899 return finish_omp_clauses (clauses, target_p ? C_ORT_ACC_TARGET
41900 : C_ORT_ACC);
41902 return clauses;
41905 /* Parse all OpenMP clauses. The set clauses allowed by the directive
41906 is a bitmask in MASK. Return the list of clauses found.
41907 FINISH_P set if finish_omp_clauses should be called.
41908 NESTED non-zero if clauses should be terminated by closing paren instead
41909 of end of pragma. If it is 2, additionally commas are required in between
41910 the clauses. */
41912 static tree
41913 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
41914 const char *where, cp_token *pragma_tok,
41915 bool finish_p = true, int nested = 0)
41917 tree clauses = NULL;
41918 bool first = true;
41919 cp_token *token = NULL;
41921 /* Don't create location wrapper nodes within OpenMP clauses. */
41922 auto_suppress_location_wrappers sentinel;
41924 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
41926 pragma_omp_clause c_kind;
41927 const char *c_name;
41928 tree prev = clauses;
41930 if (nested && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
41931 break;
41933 if (!first || nested != 2)
41935 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
41936 cp_lexer_consume_token (parser->lexer);
41937 else if (nested == 2)
41938 error_at (cp_lexer_peek_token (parser->lexer)->location,
41939 "clauses in %<simd%> trait should be separated "
41940 "by %<,%>");
41943 token = cp_lexer_peek_token (parser->lexer);
41944 c_kind = cp_parser_omp_clause_name (parser);
41946 switch (c_kind)
41948 case PRAGMA_OMP_CLAUSE_BIND:
41949 clauses = cp_parser_omp_clause_bind (parser, clauses,
41950 token->location);
41951 c_name = "bind";
41952 break;
41953 case PRAGMA_OMP_CLAUSE_COLLAPSE:
41954 clauses = cp_parser_omp_clause_collapse (parser, clauses,
41955 token->location);
41956 c_name = "collapse";
41957 break;
41958 case PRAGMA_OMP_CLAUSE_COPYIN:
41959 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
41960 c_name = "copyin";
41961 break;
41962 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
41963 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
41964 clauses);
41965 c_name = "copyprivate";
41966 break;
41967 case PRAGMA_OMP_CLAUSE_DEFAULT:
41968 clauses = cp_parser_omp_clause_default (parser, clauses,
41969 token->location, false);
41970 c_name = "default";
41971 break;
41972 case PRAGMA_OMP_CLAUSE_FILTER:
41973 clauses = cp_parser_omp_clause_filter (parser, clauses,
41974 token->location);
41975 c_name = "filter";
41976 break;
41977 case PRAGMA_OMP_CLAUSE_FINAL:
41978 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
41979 c_name = "final";
41980 break;
41981 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
41982 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
41983 clauses);
41984 c_name = "firstprivate";
41985 break;
41986 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
41987 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
41988 token->location);
41989 c_name = "grainsize";
41990 break;
41991 case PRAGMA_OMP_CLAUSE_HINT:
41992 clauses = cp_parser_omp_clause_hint (parser, clauses,
41993 token->location);
41994 c_name = "hint";
41995 break;
41996 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
41997 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
41998 token->location);
41999 c_name = "defaultmap";
42000 break;
42001 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
42002 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
42003 clauses);
42004 c_name = "use_device_ptr";
42005 break;
42006 case PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR:
42007 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_ADDR,
42008 clauses);
42009 c_name = "use_device_addr";
42010 break;
42011 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
42012 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
42013 clauses);
42014 c_name = "is_device_ptr";
42015 break;
42016 case PRAGMA_OMP_CLAUSE_HAS_DEVICE_ADDR:
42017 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_HAS_DEVICE_ADDR,
42018 clauses);
42019 c_name = "has_device_addr";
42020 break;
42021 case PRAGMA_OMP_CLAUSE_IF:
42022 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
42023 true);
42024 c_name = "if";
42025 break;
42026 case PRAGMA_OMP_CLAUSE_IN_REDUCTION:
42027 clauses
42028 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_IN_REDUCTION,
42029 true, clauses);
42030 c_name = "in_reduction";
42031 break;
42032 case PRAGMA_OMP_CLAUSE_INDIRECT:
42033 clauses = cp_parser_omp_clause_indirect (parser, clauses,
42034 token->location);
42035 c_name = "indirect";
42036 break;
42037 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
42038 clauses = cp_parser_omp_clause_lastprivate (parser, clauses);
42039 c_name = "lastprivate";
42040 break;
42041 case PRAGMA_OMP_CLAUSE_MERGEABLE:
42042 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
42043 token->location);
42044 c_name = "mergeable";
42045 break;
42046 case PRAGMA_OMP_CLAUSE_NOWAIT:
42047 clauses = cp_parser_omp_clause_nowait (parser, clauses,
42048 token->location);
42049 c_name = "nowait";
42050 break;
42051 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
42052 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
42053 token->location);
42054 c_name = "num_tasks";
42055 break;
42056 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
42057 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
42058 token->location);
42059 c_name = "num_threads";
42060 break;
42061 case PRAGMA_OMP_CLAUSE_ORDER:
42062 clauses = cp_parser_omp_clause_order (parser, clauses,
42063 token->location);
42064 c_name = "order";
42065 break;
42066 case PRAGMA_OMP_CLAUSE_ORDERED:
42067 clauses = cp_parser_omp_clause_ordered (parser, clauses,
42068 token->location);
42069 c_name = "ordered";
42070 break;
42071 case PRAGMA_OMP_CLAUSE_PRIORITY:
42072 clauses = cp_parser_omp_clause_priority (parser, clauses,
42073 token->location);
42074 c_name = "priority";
42075 break;
42076 case PRAGMA_OMP_CLAUSE_PRIVATE:
42077 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
42078 clauses);
42079 c_name = "private";
42080 break;
42081 case PRAGMA_OMP_CLAUSE_REDUCTION:
42082 clauses
42083 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
42084 true, clauses);
42085 c_name = "reduction";
42086 break;
42087 case PRAGMA_OMP_CLAUSE_SCHEDULE:
42088 clauses = cp_parser_omp_clause_schedule (parser, clauses,
42089 token->location);
42090 c_name = "schedule";
42091 break;
42092 case PRAGMA_OMP_CLAUSE_SHARED:
42093 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
42094 clauses);
42095 c_name = "shared";
42096 break;
42097 case PRAGMA_OMP_CLAUSE_TASK_REDUCTION:
42098 clauses
42099 = cp_parser_omp_clause_reduction (parser,
42100 OMP_CLAUSE_TASK_REDUCTION,
42101 true, clauses);
42102 c_name = "task_reduction";
42103 break;
42104 case PRAGMA_OMP_CLAUSE_UNTIED:
42105 clauses = cp_parser_omp_clause_untied (parser, clauses,
42106 token->location);
42107 c_name = "untied";
42108 break;
42109 case PRAGMA_OMP_CLAUSE_INBRANCH:
42110 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
42111 clauses, token->location);
42112 c_name = "inbranch";
42113 break;
42114 case PRAGMA_OMP_CLAUSE_NONTEMPORAL:
42115 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_NONTEMPORAL,
42116 clauses);
42117 c_name = "nontemporal";
42118 break;
42119 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
42120 clauses = cp_parser_omp_clause_branch (parser,
42121 OMP_CLAUSE_NOTINBRANCH,
42122 clauses, token->location);
42123 c_name = "notinbranch";
42124 break;
42125 case PRAGMA_OMP_CLAUSE_PARALLEL:
42126 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
42127 clauses, token->location);
42128 c_name = "parallel";
42129 if (!first)
42131 clause_not_first:
42132 error_at (token->location, "%qs must be the first clause of %qs",
42133 c_name, where);
42134 clauses = prev;
42136 break;
42137 case PRAGMA_OMP_CLAUSE_FOR:
42138 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
42139 clauses, token->location);
42140 c_name = "for";
42141 if (!first)
42142 goto clause_not_first;
42143 break;
42144 case PRAGMA_OMP_CLAUSE_SECTIONS:
42145 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
42146 clauses, token->location);
42147 c_name = "sections";
42148 if (!first)
42149 goto clause_not_first;
42150 break;
42151 case PRAGMA_OMP_CLAUSE_TASKGROUP:
42152 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
42153 clauses, token->location);
42154 c_name = "taskgroup";
42155 if (!first)
42156 goto clause_not_first;
42157 break;
42158 case PRAGMA_OMP_CLAUSE_LINK:
42159 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
42160 c_name = "link";
42161 break;
42162 case PRAGMA_OMP_CLAUSE_TO:
42163 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
42165 tree nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_ENTER,
42166 clauses);
42167 for (tree c = nl; c != clauses; c = OMP_CLAUSE_CHAIN (c))
42168 OMP_CLAUSE_ENTER_TO (c) = 1;
42169 clauses = nl;
42171 else
42172 clauses = cp_parser_omp_clause_from_to (parser, OMP_CLAUSE_TO,
42173 clauses);
42174 c_name = "to";
42175 break;
42176 case PRAGMA_OMP_CLAUSE_FROM:
42177 clauses = cp_parser_omp_clause_from_to (parser, OMP_CLAUSE_FROM,
42178 clauses);
42179 c_name = "from";
42180 break;
42181 case PRAGMA_OMP_CLAUSE_UNIFORM:
42182 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
42183 clauses);
42184 c_name = "uniform";
42185 break;
42186 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
42187 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
42188 token->location);
42189 c_name = "num_teams";
42190 break;
42191 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
42192 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
42193 token->location);
42194 c_name = "thread_limit";
42195 break;
42196 case PRAGMA_OMP_CLAUSE_ALIGNED:
42197 clauses = cp_parser_omp_clause_aligned (parser, clauses);
42198 c_name = "aligned";
42199 break;
42200 case PRAGMA_OMP_CLAUSE_ALLOCATE:
42201 clauses = cp_parser_omp_clause_allocate (parser, clauses);
42202 c_name = "allocate";
42203 break;
42204 case PRAGMA_OMP_CLAUSE_LINEAR:
42206 bool declare_simd = false;
42207 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
42208 declare_simd = true;
42209 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
42211 c_name = "linear";
42212 break;
42213 case PRAGMA_OMP_CLAUSE_AFFINITY:
42214 clauses = cp_parser_omp_clause_affinity (parser, clauses);
42215 c_name = "affinity";
42216 break;
42217 case PRAGMA_OMP_CLAUSE_DEPEND:
42218 clauses = cp_parser_omp_clause_depend (parser, clauses,
42219 token->location);
42220 c_name = "depend";
42221 break;
42222 case PRAGMA_OMP_CLAUSE_DOACROSS:
42223 clauses = cp_parser_omp_clause_doacross (parser, clauses,
42224 token->location);
42225 c_name = "doacross";
42226 break;
42227 case PRAGMA_OMP_CLAUSE_DETACH:
42228 clauses = cp_parser_omp_clause_detach (parser, clauses);
42229 c_name = "detach";
42230 break;
42231 case PRAGMA_OMP_CLAUSE_MAP:
42232 clauses = cp_parser_omp_clause_map (parser, clauses);
42233 c_name = "map";
42234 break;
42235 case PRAGMA_OMP_CLAUSE_DEVICE:
42236 clauses = cp_parser_omp_clause_device (parser, clauses,
42237 token->location);
42238 c_name = "device";
42239 break;
42240 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
42241 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
42242 token->location);
42243 c_name = "dist_schedule";
42244 break;
42245 case PRAGMA_OMP_CLAUSE_PROC_BIND:
42246 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
42247 token->location);
42248 c_name = "proc_bind";
42249 break;
42250 case PRAGMA_OMP_CLAUSE_DEVICE_TYPE:
42251 clauses = cp_parser_omp_clause_device_type (parser, clauses,
42252 token->location);
42253 c_name = "device_type";
42254 break;
42255 case PRAGMA_OMP_CLAUSE_SAFELEN:
42256 clauses = cp_parser_omp_clause_safelen (parser, clauses,
42257 token->location);
42258 c_name = "safelen";
42259 break;
42260 case PRAGMA_OMP_CLAUSE_SIMDLEN:
42261 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
42262 token->location);
42263 c_name = "simdlen";
42264 break;
42265 case PRAGMA_OMP_CLAUSE_NOGROUP:
42266 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
42267 token->location);
42268 c_name = "nogroup";
42269 break;
42270 case PRAGMA_OMP_CLAUSE_THREADS:
42271 clauses
42272 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
42273 clauses, token->location);
42274 c_name = "threads";
42275 break;
42276 case PRAGMA_OMP_CLAUSE_SIMD:
42277 clauses
42278 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
42279 clauses, token->location);
42280 c_name = "simd";
42281 break;
42282 case PRAGMA_OMP_CLAUSE_ENTER:
42283 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_ENTER,
42284 clauses);
42285 c_name = "enter";
42286 break;
42287 default:
42288 cp_parser_error (parser, "expected an OpenMP clause");
42289 goto saw_error;
42292 first = false;
42294 if (((mask >> c_kind) & 1) == 0)
42296 /* Remove the invalid clause(s) from the list to avoid
42297 confusing the rest of the compiler. */
42298 clauses = prev;
42299 error_at (token->location, "%qs is not valid for %qs", c_name, where);
42302 saw_error:
42303 if (!nested)
42304 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
42305 if (finish_p)
42307 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
42308 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
42309 else
42310 return finish_omp_clauses (clauses, C_ORT_OMP);
42312 return clauses;
42315 /* OpenMP 2.5:
42316 structured-block:
42317 statement
42319 In practice, we're also interested in adding the statement to an
42320 outer node. So it is convenient if we work around the fact that
42321 cp_parser_statement calls add_stmt. */
42323 static unsigned
42324 cp_parser_begin_omp_structured_block (cp_parser *parser)
42326 unsigned save = parser->in_statement;
42328 /* Only move the values to IN_OMP_BLOCK if they weren't false.
42329 This preserves the "not within loop or switch" style error messages
42330 for nonsense cases like
42331 void foo() {
42332 #pragma omp single
42333 break;
42336 if (parser->in_statement)
42337 parser->in_statement = IN_OMP_BLOCK;
42339 return save;
42342 static void
42343 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
42345 parser->in_statement = save;
42348 static tree
42349 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
42351 tree stmt = begin_omp_structured_block ();
42352 unsigned int save = cp_parser_begin_omp_structured_block (parser);
42354 parser->omp_attrs_forbidden_p = true;
42355 cp_parser_statement (parser, NULL_TREE, false, if_p);
42357 cp_parser_end_omp_structured_block (parser, save);
42358 return finish_omp_structured_block (stmt);
42361 /* OpenMP 5.x:
42362 # pragma omp allocate (list) clauses
42364 OpenMP 5.0 clause:
42365 allocator (omp_allocator_handle_t expression)
42367 OpenMP 5.1 additional clause:
42368 align (constant-expression)] */
42370 static void
42371 cp_parser_omp_allocate (cp_parser *parser, cp_token *pragma_tok)
42373 tree allocator = NULL_TREE;
42374 tree alignment = NULL_TREE;
42375 location_t loc = pragma_tok->location;
42376 tree nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_ALLOCATE, NULL_TREE);
42380 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
42381 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
42382 cp_lexer_consume_token (parser->lexer);
42384 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42385 break;
42386 matching_parens parens;
42387 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
42388 const char *p = IDENTIFIER_POINTER (id);
42389 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
42390 cp_lexer_consume_token (parser->lexer);
42391 if (strcmp (p, "allocator") != 0 && strcmp (p, "align") != 0)
42393 error_at (cloc, "expected %<allocator%> or %<align%>");
42394 break;
42396 if (!parens.require_open (parser))
42397 break;
42398 tree expr = cp_parser_assignment_expression (parser);
42399 if (p[2] == 'i' && alignment)
42401 error_at (cloc, "too many %qs clauses", "align");
42402 break;
42404 else if (p[2] == 'i')
42406 if (expr != error_mark_node)
42407 alignment = expr;
42408 /* FIXME: Remove when adding check to semantics.cc; cf FIXME below. */
42409 if (alignment
42410 && !type_dependent_expression_p (alignment)
42411 && !INTEGRAL_TYPE_P (TREE_TYPE (alignment)))
42413 error_at (cloc, "%<align%> clause argument needs to be "
42414 "positive constant power of two integer "
42415 "expression");
42416 alignment = NULL_TREE;
42418 else if (alignment)
42420 alignment = mark_rvalue_use (alignment);
42421 if (!processing_template_decl)
42423 alignment = maybe_constant_value (alignment);
42424 if (TREE_CODE (alignment) != INTEGER_CST
42425 || !tree_fits_uhwi_p (alignment)
42426 || !integer_pow2p (alignment))
42428 error_at (cloc, "%<align%> clause argument needs to be "
42429 "positive constant power of two integer "
42430 "expression");
42431 alignment = NULL_TREE;
42436 else if (allocator)
42438 error_at (cloc, "too many %qs clauses", "allocator");
42439 break;
42441 else
42443 if (expr != error_mark_node)
42444 allocator = expr;
42446 parens.require_close (parser);
42447 } while (true);
42448 cp_parser_require_pragma_eol (parser, pragma_tok);
42450 if (allocator || alignment)
42451 for (tree c = nl; c != NULL_TREE; c = OMP_CLAUSE_CHAIN (c))
42453 OMP_CLAUSE_ALLOCATE_ALLOCATOR (c) = allocator;
42454 OMP_CLAUSE_ALLOCATE_ALIGN (c) = alignment;
42457 /* FIXME: When implementing properly, delete the align/allocate expr error
42458 check above and add one in semantics.cc (to properly handle templates).
42459 Base this on the allocator/align modifiers check for the 'allocate' clause
42460 in semantics.cc's finish_omp_clauses. */
42461 sorry_at (loc, "%<#pragma omp allocate%> not yet supported");
42464 /* OpenMP 2.5:
42465 # pragma omp atomic new-line
42466 expression-stmt
42468 expression-stmt:
42469 x binop= expr | x++ | ++x | x-- | --x
42470 binop:
42471 +, *, -, /, &, ^, |, <<, >>
42473 where x is an lvalue expression with scalar type.
42475 OpenMP 3.1:
42476 # pragma omp atomic new-line
42477 update-stmt
42479 # pragma omp atomic read new-line
42480 read-stmt
42482 # pragma omp atomic write new-line
42483 write-stmt
42485 # pragma omp atomic update new-line
42486 update-stmt
42488 # pragma omp atomic capture new-line
42489 capture-stmt
42491 # pragma omp atomic capture new-line
42492 capture-block
42494 read-stmt:
42495 v = x
42496 write-stmt:
42497 x = expr
42498 update-stmt:
42499 expression-stmt | x = x binop expr
42500 capture-stmt:
42501 v = expression-stmt
42502 capture-block:
42503 { v = x; update-stmt; } | { update-stmt; v = x; }
42505 OpenMP 4.0:
42506 update-stmt:
42507 expression-stmt | x = x binop expr | x = expr binop x
42508 capture-stmt:
42509 v = update-stmt
42510 capture-block:
42511 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
42513 OpenMP 5.1:
42514 # pragma omp atomic compare new-line
42515 conditional-update-atomic
42517 # pragma omp atomic compare capture new-line
42518 conditional-update-capture-atomic
42520 conditional-update-atomic:
42521 cond-expr-stmt | cond-update-stmt
42522 cond-expr-stmt:
42523 x = expr ordop x ? expr : x;
42524 x = x ordop expr ? expr : x;
42525 x = x == e ? d : x;
42526 cond-update-stmt:
42527 if (expr ordop x) { x = expr; }
42528 if (x ordop expr) { x = expr; }
42529 if (x == e) { x = d; }
42530 ordop:
42531 <, >
42532 conditional-update-capture-atomic:
42533 v = cond-expr-stmt
42534 { v = x; cond-expr-stmt }
42535 { cond-expr-stmt v = x; }
42536 { v = x; cond-update-stmt }
42537 { cond-update-stmt v = x; }
42538 if (x == e) { x = d; } else { v = x; }
42539 { r = x == e; if (r) { x = d; } }
42540 { r = x == e; if (r) { x = d; } else { v = x; } }
42542 where x, r and v are lvalue expressions with scalar type,
42543 expr, e and d are expressions with scalar type and e might be
42544 the same as v. */
42546 static void
42547 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok, bool openacc)
42549 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
42550 tree rhs1 = NULL_TREE, orig_lhs, r = NULL_TREE;
42551 location_t loc = pragma_tok->location;
42552 enum tree_code code = ERROR_MARK, opcode = NOP_EXPR;
42553 enum omp_memory_order memory_order = OMP_MEMORY_ORDER_UNSPECIFIED;
42554 bool structured_block = false;
42555 tree clauses = NULL_TREE;
42556 bool capture = false;
42557 bool compare = false;
42558 bool weak = false;
42559 enum omp_memory_order fail = OMP_MEMORY_ORDER_UNSPECIFIED;
42560 bool no_semicolon = false;
42561 bool extra_scope = false;
42563 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
42565 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
42566 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
42567 cp_lexer_consume_token (parser->lexer);
42569 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42571 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
42572 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
42573 const char *p = IDENTIFIER_POINTER (id);
42574 enum tree_code new_code = ERROR_MARK;
42575 enum omp_memory_order new_memory_order
42576 = OMP_MEMORY_ORDER_UNSPECIFIED;
42577 bool new_capture = false;
42578 bool new_compare = false;
42579 bool new_weak = false;
42580 enum omp_memory_order new_fail = OMP_MEMORY_ORDER_UNSPECIFIED;
42582 if (!strcmp (p, "read"))
42583 new_code = OMP_ATOMIC_READ;
42584 else if (!strcmp (p, "write"))
42585 new_code = NOP_EXPR;
42586 else if (!strcmp (p, "update"))
42587 new_code = OMP_ATOMIC;
42588 else if (openacc && !strcmp (p, "capture"))
42589 new_code = OMP_ATOMIC_CAPTURE_NEW;
42590 else if (openacc)
42592 p = NULL;
42593 error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
42594 "or %<capture%> clause");
42596 else if (!strcmp (p, "capture"))
42597 new_capture = true;
42598 else if (!strcmp (p, "compare"))
42599 new_compare = true;
42600 else if (!strcmp (p, "weak"))
42601 new_weak = true;
42602 else if (!strcmp (p, "fail"))
42604 matching_parens parens;
42606 cp_lexer_consume_token (parser->lexer);
42607 if (!parens.require_open (parser))
42608 continue;
42610 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42612 id = cp_lexer_peek_token (parser->lexer)->u.value;
42613 const char *q = IDENTIFIER_POINTER (id);
42615 if (!strcmp (q, "seq_cst"))
42616 new_fail = OMP_MEMORY_ORDER_SEQ_CST;
42617 else if (!strcmp (q, "acquire"))
42618 new_fail = OMP_MEMORY_ORDER_ACQUIRE;
42619 else if (!strcmp (q, "relaxed"))
42620 new_fail = OMP_MEMORY_ORDER_RELAXED;
42623 if (new_fail != OMP_MEMORY_ORDER_UNSPECIFIED)
42625 cp_lexer_consume_token (parser->lexer);
42626 if (fail != OMP_MEMORY_ORDER_UNSPECIFIED)
42627 error_at (cloc, "too many %qs clauses", "fail");
42628 else
42629 fail = new_fail;
42631 else
42632 cp_parser_error (parser, "expected %<seq_cst%>, %<acquire%> "
42633 "or %<relaxed%>");
42634 if (new_fail == OMP_MEMORY_ORDER_UNSPECIFIED
42635 || !parens.require_close (parser))
42636 cp_parser_skip_to_closing_parenthesis (parser,
42637 /*recovering=*/true,
42638 /*or_comma=*/false,
42639 /*consume_paren=*/true);
42640 continue;
42642 else if (!strcmp (p, "seq_cst"))
42643 new_memory_order = OMP_MEMORY_ORDER_SEQ_CST;
42644 else if (!strcmp (p, "acq_rel"))
42645 new_memory_order = OMP_MEMORY_ORDER_ACQ_REL;
42646 else if (!strcmp (p, "release"))
42647 new_memory_order = OMP_MEMORY_ORDER_RELEASE;
42648 else if (!strcmp (p, "acquire"))
42649 new_memory_order = OMP_MEMORY_ORDER_ACQUIRE;
42650 else if (!strcmp (p, "relaxed"))
42651 new_memory_order = OMP_MEMORY_ORDER_RELAXED;
42652 else if (!strcmp (p, "hint"))
42654 cp_lexer_consume_token (parser->lexer);
42655 clauses = cp_parser_omp_clause_hint (parser, clauses, cloc);
42656 continue;
42658 else
42660 p = NULL;
42661 error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
42662 "%<capture%>, %<compare%>, %<weak%>, %<fail%>, "
42663 "%<seq_cst%>, %<acq_rel%>, %<release%>, "
42664 "%<relaxed%> or %<hint%> clause");
42666 if (p)
42668 if (new_code != ERROR_MARK)
42670 /* OpenACC permits 'update capture'. */
42671 if (openacc
42672 && code == OMP_ATOMIC
42673 && new_code == OMP_ATOMIC_CAPTURE_NEW)
42674 code = new_code;
42675 else if (code != ERROR_MARK)
42676 error_at (cloc, "too many atomic clauses");
42677 else
42678 code = new_code;
42680 else if (new_memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
42682 if (memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
42683 error_at (cloc, "too many memory order clauses");
42684 else
42685 memory_order = new_memory_order;
42687 else if (new_capture)
42689 if (capture)
42690 error_at (cloc, "too many %qs clauses", "capture");
42691 else
42692 capture = true;
42694 else if (new_compare)
42696 if (compare)
42697 error_at (cloc, "too many %qs clauses", "compare");
42698 else
42699 compare = true;
42701 else if (new_weak)
42703 if (weak)
42704 error_at (cloc, "too many %qs clauses", "weak");
42705 else
42706 weak = true;
42708 cp_lexer_consume_token (parser->lexer);
42709 continue;
42712 break;
42714 cp_parser_require_pragma_eol (parser, pragma_tok);
42716 if (code == ERROR_MARK)
42717 code = OMP_ATOMIC;
42718 if (capture)
42720 if (code != OMP_ATOMIC)
42721 error_at (loc, "%qs clause is incompatible with %<read%> or %<write%> "
42722 "clauses", "capture");
42723 else
42724 code = OMP_ATOMIC_CAPTURE_NEW;
42726 if (compare && code != OMP_ATOMIC && code != OMP_ATOMIC_CAPTURE_NEW)
42728 error_at (loc, "%qs clause is incompatible with %<read%> or %<write%> "
42729 "clauses", "compare");
42730 compare = false;
42732 if (fail != OMP_MEMORY_ORDER_UNSPECIFIED && !compare)
42734 error_at (loc, "%qs clause requires %qs clause", "fail", "compare");
42735 fail = OMP_MEMORY_ORDER_UNSPECIFIED;
42737 if (weak && !compare)
42739 error_at (loc, "%qs clause requires %qs clause", "weak", "compare");
42740 weak = false;
42742 if (openacc)
42743 memory_order = OMP_MEMORY_ORDER_RELAXED;
42744 else if (memory_order == OMP_MEMORY_ORDER_UNSPECIFIED)
42746 omp_requires_mask
42747 = (enum omp_requires) (omp_requires_mask
42748 | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED);
42749 switch ((enum omp_memory_order)
42750 (omp_requires_mask & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER))
42752 case OMP_MEMORY_ORDER_UNSPECIFIED:
42753 case OMP_MEMORY_ORDER_RELAXED:
42754 memory_order = OMP_MEMORY_ORDER_RELAXED;
42755 break;
42756 case OMP_MEMORY_ORDER_SEQ_CST:
42757 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
42758 break;
42759 case OMP_MEMORY_ORDER_ACQUIRE:
42760 if (code == NOP_EXPR) /* atomic write */
42762 error_at (loc, "%<#pragma omp atomic write%> incompatible with "
42763 "%<acquire%> clause implicitly provided by a "
42764 "%<requires%> directive");
42765 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
42767 else
42768 memory_order = OMP_MEMORY_ORDER_ACQUIRE;
42769 break;
42770 case OMP_MEMORY_ORDER_RELEASE:
42771 if (code == OMP_ATOMIC_READ)
42773 error_at (loc, "%<#pragma omp atomic read%> incompatible with "
42774 "%<release%> clause implicitly provided by a "
42775 "%<requires%> directive");
42776 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
42778 else
42779 memory_order = OMP_MEMORY_ORDER_RELEASE;
42780 break;
42781 case OMP_MEMORY_ORDER_ACQ_REL:
42782 switch (code)
42784 case OMP_ATOMIC_READ:
42785 memory_order = OMP_MEMORY_ORDER_ACQUIRE;
42786 break;
42787 case NOP_EXPR: /* atomic write */
42788 memory_order = OMP_MEMORY_ORDER_RELEASE;
42789 break;
42790 default:
42791 memory_order = OMP_MEMORY_ORDER_ACQ_REL;
42792 break;
42794 break;
42795 default:
42796 gcc_unreachable ();
42799 else
42800 switch (code)
42802 case OMP_ATOMIC_READ:
42803 if (memory_order == OMP_MEMORY_ORDER_RELEASE)
42805 error_at (loc, "%<#pragma omp atomic read%> incompatible with "
42806 "%<release%> clause");
42807 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
42809 else if (memory_order == OMP_MEMORY_ORDER_ACQ_REL)
42810 memory_order = OMP_MEMORY_ORDER_ACQUIRE;
42811 break;
42812 case NOP_EXPR: /* atomic write */
42813 if (memory_order == OMP_MEMORY_ORDER_ACQUIRE)
42815 error_at (loc, "%<#pragma omp atomic write%> incompatible with "
42816 "%<acquire%> clause");
42817 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
42819 else if (memory_order == OMP_MEMORY_ORDER_ACQ_REL)
42820 memory_order = OMP_MEMORY_ORDER_RELEASE;
42821 break;
42822 default:
42823 break;
42825 if (fail != OMP_MEMORY_ORDER_UNSPECIFIED)
42826 memory_order
42827 = (enum omp_memory_order) (memory_order
42828 | (fail << OMP_FAIL_MEMORY_ORDER_SHIFT));
42830 switch (code)
42832 case OMP_ATOMIC_READ:
42833 case NOP_EXPR: /* atomic write */
42834 v = cp_parser_unary_expression (parser);
42835 if (v == error_mark_node)
42836 goto saw_error;
42837 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
42838 goto saw_error;
42839 if (code == NOP_EXPR)
42840 lhs = cp_parser_expression (parser);
42841 else
42842 lhs = cp_parser_unary_expression (parser);
42843 if (lhs == error_mark_node)
42844 goto saw_error;
42845 if (code == NOP_EXPR)
42847 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
42848 opcode. */
42849 code = OMP_ATOMIC;
42850 rhs = lhs;
42851 lhs = v;
42852 v = NULL_TREE;
42854 goto done;
42855 case OMP_ATOMIC_CAPTURE_NEW:
42856 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
42858 cp_lexer_consume_token (parser->lexer);
42859 structured_block = true;
42861 else if (compare
42862 && cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
42863 break;
42864 else
42866 v = cp_parser_unary_expression (parser);
42867 if (v == error_mark_node)
42868 goto saw_error;
42869 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
42870 goto saw_error;
42871 if (compare
42872 && cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
42874 location_t eloc = cp_lexer_peek_token (parser->lexer)->location;
42875 error_at (eloc, "expected expression");
42876 goto saw_error;
42879 default:
42880 break;
42883 restart:
42884 if (compare && cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
42886 cp_lexer_consume_token (parser->lexer);
42888 matching_parens parens;
42889 if (!parens.require_open (parser))
42890 goto saw_error;
42891 location_t eloc = cp_lexer_peek_token (parser->lexer)->location;
42892 tree cmp_expr;
42893 if (r)
42894 cmp_expr = cp_parser_unary_expression (parser);
42895 else
42896 cmp_expr = cp_parser_binary_expression (parser, false, true,
42897 PREC_NOT_OPERATOR, NULL);
42898 if (!parens.require_close (parser))
42899 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
42900 if (cmp_expr == error_mark_node)
42901 goto saw_error;
42902 if (r)
42904 if (!cp_tree_equal (cmp_expr, r))
42905 goto bad_if;
42906 cmp_expr = rhs;
42907 rhs = NULL_TREE;
42908 gcc_assert (TREE_CODE (cmp_expr) == EQ_EXPR);
42910 if (TREE_CODE (cmp_expr) == EQ_EXPR)
42912 else if (!structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
42914 error_at (EXPR_LOC_OR_LOC (cmp_expr, eloc),
42915 "expected %<==%> comparison in %<if%> condition");
42916 goto saw_error;
42918 else if (TREE_CODE (cmp_expr) != GT_EXPR
42919 && TREE_CODE (cmp_expr) != LT_EXPR)
42921 error_at (EXPR_LOC_OR_LOC (cmp_expr, eloc),
42922 "expected %<==%>, %<<%> or %<>%> comparison in %<if%> "
42923 "condition");
42924 goto saw_error;
42926 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
42927 goto saw_error;
42929 extra_scope = true;
42930 eloc = cp_lexer_peek_token (parser->lexer)->location;
42931 lhs = cp_parser_unary_expression (parser);
42932 orig_lhs = lhs;
42933 if (lhs == error_mark_node)
42934 goto saw_error;
42935 if (!cp_lexer_next_token_is (parser->lexer, CPP_EQ))
42937 cp_parser_error (parser, "expected %<=%>");
42938 goto saw_error;
42940 cp_lexer_consume_token (parser->lexer);
42941 eloc = cp_lexer_peek_token (parser->lexer)->location;
42942 if (TREE_CODE (cmp_expr) == EQ_EXPR)
42943 rhs1 = cp_parser_expression (parser);
42944 else
42945 rhs1 = cp_parser_simple_cast_expression (parser);
42947 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
42948 goto saw_error;
42950 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
42951 goto saw_error;
42953 extra_scope = false;
42954 no_semicolon = true;
42956 if (cp_tree_equal (TREE_OPERAND (cmp_expr, 0), lhs))
42958 if (TREE_CODE (cmp_expr) == EQ_EXPR)
42960 opcode = COND_EXPR;
42961 rhs = TREE_OPERAND (cmp_expr, 1);
42963 else if (cp_tree_equal (TREE_OPERAND (cmp_expr, 1), rhs1))
42965 opcode = (TREE_CODE (cmp_expr) == GT_EXPR
42966 ? MIN_EXPR : MAX_EXPR);
42967 rhs = rhs1;
42968 rhs1 = TREE_OPERAND (cmp_expr, 0);
42970 else
42971 goto bad_if;
42973 else if (TREE_CODE (cmp_expr) == EQ_EXPR)
42974 goto bad_if;
42975 else if (cp_tree_equal (TREE_OPERAND (cmp_expr, 1), lhs)
42976 && cp_tree_equal (TREE_OPERAND (cmp_expr, 0), rhs1))
42978 opcode = (TREE_CODE (cmp_expr) == GT_EXPR
42979 ? MAX_EXPR : MIN_EXPR);
42980 rhs = rhs1;
42981 rhs1 = TREE_OPERAND (cmp_expr, 1);
42983 else
42985 bad_if:
42986 cp_parser_error (parser,
42987 "invalid form of %<#pragma omp atomic compare%>");
42988 goto saw_error;
42991 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
42993 if (code != OMP_ATOMIC_CAPTURE_NEW
42994 || (structured_block && r == NULL_TREE)
42995 || TREE_CODE (cmp_expr) != EQ_EXPR)
42997 eloc = cp_lexer_peek_token (parser->lexer)->location;
42998 error_at (eloc, "unexpected %<else%>");
42999 goto saw_error;
43002 cp_lexer_consume_token (parser->lexer);
43004 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
43005 goto saw_error;
43007 extra_scope = true;
43008 v = cp_parser_unary_expression (parser);
43009 if (v == error_mark_node)
43010 goto saw_error;
43011 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
43012 goto saw_error;
43014 tree expr = cp_parser_simple_cast_expression (parser);
43016 if (!cp_tree_equal (expr, lhs))
43017 goto bad_if;
43019 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
43020 goto saw_error;
43022 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
43023 goto saw_error;
43025 extra_scope = false;
43026 code = OMP_ATOMIC_CAPTURE_OLD;
43027 if (r == NULL_TREE)
43028 /* Signal to c_finish_omp_atomic that in
43029 if (x == e) { x = d; } else { v = x; }
43030 case the store to v should be conditional. */
43031 r = void_list_node;
43033 else if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
43035 cp_parser_error (parser, "expected %<else%>");
43036 goto saw_error;
43038 else if (code == OMP_ATOMIC_CAPTURE_NEW
43039 && r != NULL_TREE
43040 && v == NULL_TREE)
43041 code = OMP_ATOMIC;
43042 goto stmt_done;
43044 lhs = cp_parser_unary_expression (parser);
43045 orig_lhs = lhs;
43046 switch (TREE_CODE (lhs))
43048 case ERROR_MARK:
43049 goto saw_error;
43051 case POSTINCREMENT_EXPR:
43052 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
43053 code = OMP_ATOMIC_CAPTURE_OLD;
43054 /* FALLTHROUGH */
43055 case PREINCREMENT_EXPR:
43056 lhs = TREE_OPERAND (lhs, 0);
43057 opcode = PLUS_EXPR;
43058 rhs = integer_one_node;
43059 if (compare)
43060 goto invalid_compare;
43061 break;
43063 case POSTDECREMENT_EXPR:
43064 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
43065 code = OMP_ATOMIC_CAPTURE_OLD;
43066 /* FALLTHROUGH */
43067 case PREDECREMENT_EXPR:
43068 lhs = TREE_OPERAND (lhs, 0);
43069 opcode = MINUS_EXPR;
43070 rhs = integer_one_node;
43071 if (compare)
43072 goto invalid_compare;
43073 break;
43075 case COMPOUND_EXPR:
43076 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
43077 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
43078 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
43079 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
43080 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
43081 (TREE_OPERAND (lhs, 1), 0), 0)))
43082 == BOOLEAN_TYPE)
43083 /* Undo effects of boolean_increment for post {in,de}crement. */
43084 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
43085 /* FALLTHRU */
43086 case MODIFY_EXPR:
43087 if (TREE_CODE (lhs) == MODIFY_EXPR
43088 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
43090 /* Undo effects of boolean_increment. */
43091 if (integer_onep (TREE_OPERAND (lhs, 1)))
43093 /* This is pre or post increment. */
43094 rhs = TREE_OPERAND (lhs, 1);
43095 lhs = TREE_OPERAND (lhs, 0);
43096 opcode = NOP_EXPR;
43097 if (code == OMP_ATOMIC_CAPTURE_NEW
43098 && !structured_block
43099 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
43100 code = OMP_ATOMIC_CAPTURE_OLD;
43101 if (compare)
43102 goto invalid_compare;
43103 break;
43106 /* FALLTHRU */
43107 default:
43108 if (compare && !cp_lexer_next_token_is (parser->lexer, CPP_EQ))
43110 cp_parser_error (parser, "expected %<=%>");
43111 goto saw_error;
43113 switch (cp_lexer_peek_token (parser->lexer)->type)
43115 case CPP_MULT_EQ:
43116 opcode = MULT_EXPR;
43117 break;
43118 case CPP_DIV_EQ:
43119 opcode = TRUNC_DIV_EXPR;
43120 break;
43121 case CPP_PLUS_EQ:
43122 opcode = PLUS_EXPR;
43123 break;
43124 case CPP_MINUS_EQ:
43125 opcode = MINUS_EXPR;
43126 break;
43127 case CPP_LSHIFT_EQ:
43128 opcode = LSHIFT_EXPR;
43129 break;
43130 case CPP_RSHIFT_EQ:
43131 opcode = RSHIFT_EXPR;
43132 break;
43133 case CPP_AND_EQ:
43134 opcode = BIT_AND_EXPR;
43135 break;
43136 case CPP_OR_EQ:
43137 opcode = BIT_IOR_EXPR;
43138 break;
43139 case CPP_XOR_EQ:
43140 opcode = BIT_XOR_EXPR;
43141 break;
43142 case CPP_EQ:
43143 enum cp_parser_prec oprec;
43144 cp_token *token;
43145 cp_lexer_consume_token (parser->lexer);
43146 cp_parser_parse_tentatively (parser);
43147 rhs1 = cp_parser_simple_cast_expression (parser);
43148 if (rhs1 == error_mark_node)
43150 cp_parser_abort_tentative_parse (parser);
43151 cp_parser_simple_cast_expression (parser);
43152 goto saw_error;
43154 token = cp_lexer_peek_token (parser->lexer);
43155 if (token->type != CPP_SEMICOLON
43156 && (!compare || token->type != CPP_QUERY)
43157 && !cp_tree_equal (lhs, rhs1))
43159 cp_parser_abort_tentative_parse (parser);
43160 cp_parser_parse_tentatively (parser);
43161 rhs = cp_parser_binary_expression (parser, false, true,
43162 PREC_NOT_OPERATOR, NULL);
43163 if (rhs == error_mark_node)
43165 cp_parser_abort_tentative_parse (parser);
43166 cp_parser_binary_expression (parser, false, true,
43167 PREC_NOT_OPERATOR, NULL);
43168 goto saw_error;
43170 switch (TREE_CODE (rhs))
43172 case MULT_EXPR:
43173 case TRUNC_DIV_EXPR:
43174 case RDIV_EXPR:
43175 case PLUS_EXPR:
43176 case MINUS_EXPR:
43177 case LSHIFT_EXPR:
43178 case RSHIFT_EXPR:
43179 case BIT_AND_EXPR:
43180 case BIT_IOR_EXPR:
43181 case BIT_XOR_EXPR:
43182 if (compare)
43183 break;
43184 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
43186 if (cp_parser_parse_definitely (parser))
43188 opcode = TREE_CODE (rhs);
43189 rhs1 = TREE_OPERAND (rhs, 0);
43190 rhs = TREE_OPERAND (rhs, 1);
43191 goto stmt_done;
43193 else
43194 goto saw_error;
43196 break;
43197 case EQ_EXPR:
43198 if (!compare
43199 || code != OMP_ATOMIC_CAPTURE_NEW
43200 || !structured_block
43201 || v
43202 || r)
43203 break;
43204 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
43205 && cp_lexer_nth_token_is_keyword (parser->lexer,
43206 2, RID_IF))
43208 if (cp_parser_parse_definitely (parser))
43210 r = lhs;
43211 lhs = NULL_TREE;
43212 rhs1 = NULL_TREE;
43213 cp_lexer_consume_token (parser->lexer);
43214 goto restart;
43217 break;
43218 case GT_EXPR:
43219 case LT_EXPR:
43220 if (compare
43221 && cp_lexer_next_token_is (parser->lexer, CPP_QUERY)
43222 && cp_tree_equal (lhs, TREE_OPERAND (rhs, 1))
43223 && cp_parser_parse_definitely (parser))
43225 opcode = TREE_CODE (rhs);
43226 rhs1 = TREE_OPERAND (rhs, 0);
43227 rhs = TREE_OPERAND (rhs, 1);
43228 cond_expr:
43229 cp_lexer_consume_token (parser->lexer);
43230 bool saved_colon_corrects_to_scope_p
43231 = parser->colon_corrects_to_scope_p;
43232 parser->colon_corrects_to_scope_p = false;
43233 tree e1 = cp_parser_expression (parser);
43234 parser->colon_corrects_to_scope_p
43235 = saved_colon_corrects_to_scope_p;
43236 cp_parser_require (parser, CPP_COLON, RT_COLON);
43237 tree e2 = cp_parser_simple_cast_expression (parser);
43238 if (cp_tree_equal (lhs, e2))
43240 if (cp_tree_equal (lhs, rhs1))
43242 if (opcode == EQ_EXPR)
43244 opcode = COND_EXPR;
43245 rhs1 = e1;
43246 goto stmt_done;
43248 if (cp_tree_equal (rhs, e1))
43250 opcode
43251 = opcode == GT_EXPR ? MIN_EXPR : MAX_EXPR;
43252 rhs = e1;
43253 goto stmt_done;
43256 else
43258 gcc_assert (opcode != EQ_EXPR);
43259 if (cp_tree_equal (rhs1, e1))
43261 opcode
43262 = opcode == GT_EXPR ? MAX_EXPR : MIN_EXPR;
43263 rhs1 = rhs;
43264 rhs = e1;
43265 goto stmt_done;
43269 cp_parser_error (parser,
43270 "invalid form of "
43271 "%<#pragma omp atomic compare%>");
43272 goto saw_error;
43274 break;
43275 default:
43276 break;
43278 cp_parser_abort_tentative_parse (parser);
43279 if (structured_block
43280 && code == OMP_ATOMIC_CAPTURE_OLD
43281 && !compare)
43283 rhs = cp_parser_expression (parser);
43284 if (rhs == error_mark_node)
43285 goto saw_error;
43286 opcode = NOP_EXPR;
43287 rhs1 = NULL_TREE;
43288 goto stmt_done;
43290 cp_parser_error (parser,
43291 "invalid form of %<#pragma omp atomic%>");
43292 goto saw_error;
43294 if (!cp_parser_parse_definitely (parser))
43295 goto saw_error;
43296 switch (token->type)
43298 case CPP_SEMICOLON:
43299 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
43301 code = OMP_ATOMIC_CAPTURE_OLD;
43302 v = lhs;
43303 lhs = NULL_TREE;
43304 lhs1 = rhs1;
43305 rhs1 = NULL_TREE;
43306 cp_lexer_consume_token (parser->lexer);
43307 goto restart;
43309 else if (structured_block && !compare)
43311 opcode = NOP_EXPR;
43312 rhs = rhs1;
43313 rhs1 = NULL_TREE;
43314 goto stmt_done;
43316 cp_parser_error (parser,
43317 "invalid form of %<#pragma omp atomic%>");
43318 goto saw_error;
43319 case CPP_MULT:
43320 opcode = MULT_EXPR;
43321 break;
43322 case CPP_DIV:
43323 opcode = TRUNC_DIV_EXPR;
43324 break;
43325 case CPP_PLUS:
43326 opcode = PLUS_EXPR;
43327 break;
43328 case CPP_MINUS:
43329 opcode = MINUS_EXPR;
43330 break;
43331 case CPP_LSHIFT:
43332 opcode = LSHIFT_EXPR;
43333 break;
43334 case CPP_RSHIFT:
43335 opcode = RSHIFT_EXPR;
43336 break;
43337 case CPP_AND:
43338 opcode = BIT_AND_EXPR;
43339 break;
43340 case CPP_OR:
43341 opcode = BIT_IOR_EXPR;
43342 break;
43343 case CPP_XOR:
43344 opcode = BIT_XOR_EXPR;
43345 break;
43346 case CPP_EQ_EQ:
43347 opcode = EQ_EXPR;
43348 break;
43349 case CPP_GREATER:
43350 opcode = GT_EXPR;
43351 break;
43352 case CPP_LESS:
43353 opcode = LT_EXPR;
43354 break;
43355 default:
43356 cp_parser_error (parser,
43357 "invalid operator for %<#pragma omp atomic%>");
43358 goto saw_error;
43360 if (compare
43361 && TREE_CODE_CLASS (opcode) != tcc_comparison)
43363 cp_parser_error (parser,
43364 "invalid form of "
43365 "%<#pragma omp atomic compare%>");
43366 goto saw_error;
43368 oprec = TOKEN_PRECEDENCE (token);
43369 gcc_assert (oprec != PREC_NOT_OPERATOR);
43370 if (commutative_tree_code (opcode))
43371 oprec = (enum cp_parser_prec) (oprec - 1);
43372 cp_lexer_consume_token (parser->lexer);
43373 rhs = cp_parser_binary_expression (parser, false, false,
43374 oprec, NULL);
43375 if (rhs == error_mark_node)
43376 goto saw_error;
43377 if (compare)
43379 if (!cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
43381 cp_parser_error (parser,
43382 "invalid form of "
43383 "%<#pragma omp atomic compare%>");
43384 goto saw_error;
43386 goto cond_expr;
43388 goto stmt_done;
43389 default:
43390 cp_parser_error (parser,
43391 "invalid operator for %<#pragma omp atomic%>");
43392 goto saw_error;
43394 cp_lexer_consume_token (parser->lexer);
43396 rhs = cp_parser_expression (parser);
43397 if (rhs == error_mark_node)
43398 goto saw_error;
43399 break;
43401 stmt_done:
43402 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW && r == NULL_TREE)
43404 if (!no_semicolon
43405 && !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
43406 goto saw_error;
43407 no_semicolon = false;
43408 v = cp_parser_unary_expression (parser);
43409 if (v == error_mark_node)
43410 goto saw_error;
43411 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
43412 goto saw_error;
43413 lhs1 = cp_parser_unary_expression (parser);
43414 if (lhs1 == error_mark_node)
43415 goto saw_error;
43417 if (structured_block)
43419 if (!no_semicolon)
43420 cp_parser_consume_semicolon_at_end_of_statement (parser);
43421 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
43423 done:
43424 if (weak && opcode != COND_EXPR)
43426 error_at (loc, "%<weak%> clause requires atomic equality comparison");
43427 weak = false;
43429 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
43430 finish_omp_atomic (pragma_tok->location, code, opcode, lhs, rhs, v, lhs1,
43431 rhs1, r, clauses, memory_order, weak);
43432 if (!structured_block && !no_semicolon)
43433 cp_parser_consume_semicolon_at_end_of_statement (parser);
43434 return;
43436 invalid_compare:
43437 error ("invalid form of %<pragma omp atomic compare%>");
43438 /* FALLTHRU */
43439 saw_error:
43440 cp_parser_skip_to_end_of_block_or_statement (parser);
43441 if (extra_scope && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
43442 cp_lexer_consume_token (parser->lexer);
43443 if (structured_block)
43445 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
43446 cp_lexer_consume_token (parser->lexer);
43447 else if (code == OMP_ATOMIC_CAPTURE_NEW)
43449 cp_parser_skip_to_end_of_block_or_statement (parser);
43450 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
43451 cp_lexer_consume_token (parser->lexer);
43457 /* OpenMP 2.5:
43458 # pragma omp barrier new-line */
43460 static void
43461 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
43463 cp_parser_require_pragma_eol (parser, pragma_tok);
43464 finish_omp_barrier ();
43467 /* OpenMP 2.5:
43468 # pragma omp critical [(name)] new-line
43469 structured-block
43471 OpenMP 4.5:
43472 # pragma omp critical [(name) [hint(expression)]] new-line
43473 structured-block */
43475 #define OMP_CRITICAL_CLAUSE_MASK \
43476 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
43478 static tree
43479 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
43481 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
43483 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
43485 matching_parens parens;
43486 parens.consume_open (parser);
43488 name = cp_parser_identifier (parser);
43490 if (name == error_mark_node
43491 || !parens.require_close (parser))
43492 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
43493 /*or_comma=*/false,
43494 /*consume_paren=*/true);
43495 if (name == error_mark_node)
43496 name = NULL;
43498 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
43499 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
43500 cp_lexer_consume_token (parser->lexer);
43503 clauses = cp_parser_omp_all_clauses (parser, OMP_CRITICAL_CLAUSE_MASK,
43504 "#pragma omp critical", pragma_tok);
43506 stmt = cp_parser_omp_structured_block (parser, if_p);
43507 return c_finish_omp_critical (input_location, stmt, name, clauses);
43510 /* OpenMP 5.0:
43511 # pragma omp depobj ( depobj ) depobj-clause new-line
43513 depobj-clause:
43514 depend (dependence-type : locator)
43515 destroy
43516 update (dependence-type)
43518 OpenMP 5.2 additionally:
43519 destroy ( depobj )
43521 dependence-type:
43524 inout
43525 mutexinout */
43527 static void
43528 cp_parser_omp_depobj (cp_parser *parser, cp_token *pragma_tok)
43530 location_t loc = pragma_tok->location;
43531 matching_parens parens;
43532 if (!parens.require_open (parser))
43534 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43535 return;
43538 tree depobj = cp_parser_assignment_expression (parser);
43540 if (!parens.require_close (parser))
43541 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
43542 /*or_comma=*/false,
43543 /*consume_paren=*/true);
43545 tree clause = NULL_TREE;
43546 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INVALID;
43547 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
43548 cp_lexer_consume_token (parser->lexer);
43549 location_t c_loc = cp_lexer_peek_token (parser->lexer)->location;
43550 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43552 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43553 const char *p = IDENTIFIER_POINTER (id);
43555 cp_lexer_consume_token (parser->lexer);
43556 if (!strcmp ("depend", p))
43558 /* Don't create location wrapper nodes within the depend clause. */
43559 auto_suppress_location_wrappers sentinel;
43560 clause = cp_parser_omp_clause_depend (parser, NULL_TREE, c_loc);
43561 if (clause)
43562 clause = finish_omp_clauses (clause, C_ORT_OMP);
43563 if (!clause)
43564 clause = error_mark_node;
43566 else if (!strcmp ("destroy", p))
43568 kind = OMP_CLAUSE_DEPEND_LAST;
43569 matching_parens c_parens;
43570 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
43571 && c_parens.require_open (parser))
43573 tree destobj = cp_parser_assignment_expression (parser);
43574 if (depobj != error_mark_node
43575 && destobj != error_mark_node
43576 && !operand_equal_p (destobj, depobj, OEP_MATCH_SIDE_EFFECTS
43577 | OEP_LEXICOGRAPHIC))
43578 warning_at (EXPR_LOC_OR_LOC (destobj, c_loc), OPT_Wopenmp,
43579 "the %<destroy%> expression %qE should be the same "
43580 "as the %<depobj%> argument %qE", destobj, depobj);
43581 if (!c_parens.require_close (parser))
43582 cp_parser_skip_to_closing_parenthesis (parser,
43583 /*recovering=*/true,
43584 /*or_comma=*/false,
43585 /*consume_paren=*/true);
43588 else if (!strcmp ("update", p))
43590 matching_parens c_parens;
43591 if (c_parens.require_open (parser))
43593 location_t c2_loc
43594 = cp_lexer_peek_token (parser->lexer)->location;
43595 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43597 tree id2 = cp_lexer_peek_token (parser->lexer)->u.value;
43598 const char *p2 = IDENTIFIER_POINTER (id2);
43600 cp_lexer_consume_token (parser->lexer);
43601 if (!strcmp ("in", p2))
43602 kind = OMP_CLAUSE_DEPEND_IN;
43603 else if (!strcmp ("out", p2))
43604 kind = OMP_CLAUSE_DEPEND_OUT;
43605 else if (!strcmp ("inout", p2))
43606 kind = OMP_CLAUSE_DEPEND_INOUT;
43607 else if (!strcmp ("mutexinoutset", p2))
43608 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
43609 else if (!strcmp ("inoutset", p2))
43610 kind = OMP_CLAUSE_DEPEND_INOUTSET;
43612 if (kind == OMP_CLAUSE_DEPEND_INVALID)
43614 clause = error_mark_node;
43615 error_at (c2_loc, "expected %<in%>, %<out%>, %<inout%>, "
43616 "%<mutexinoutset%> or %<inoutset%>");
43618 if (!c_parens.require_close (parser))
43619 cp_parser_skip_to_closing_parenthesis (parser,
43620 /*recovering=*/true,
43621 /*or_comma=*/false,
43622 /*consume_paren=*/true);
43624 else
43625 clause = error_mark_node;
43628 if (!clause && kind == OMP_CLAUSE_DEPEND_INVALID)
43630 clause = error_mark_node;
43631 error_at (c_loc, "expected %<depend%>, %<destroy%> or %<update%> clause");
43633 cp_parser_require_pragma_eol (parser, pragma_tok);
43635 finish_omp_depobj (loc, depobj, kind, clause);
43639 /* OpenMP 2.5:
43640 # pragma omp flush flush-vars[opt] new-line
43642 flush-vars:
43643 ( variable-list )
43645 OpenMP 5.0:
43646 # pragma omp flush memory-order-clause new-line */
43648 static void
43649 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
43651 enum memmodel mo = MEMMODEL_LAST;
43652 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
43653 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
43654 cp_lexer_consume_token (parser->lexer);
43655 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43657 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43658 const char *p = IDENTIFIER_POINTER (id);
43659 if (!strcmp (p, "seq_cst"))
43660 mo = MEMMODEL_SEQ_CST;
43661 else if (!strcmp (p, "acq_rel"))
43662 mo = MEMMODEL_ACQ_REL;
43663 else if (!strcmp (p, "release"))
43664 mo = MEMMODEL_RELEASE;
43665 else if (!strcmp (p, "acquire"))
43666 mo = MEMMODEL_ACQUIRE;
43667 else
43668 error_at (cp_lexer_peek_token (parser->lexer)->location,
43669 "expected %<seq_cst%>, %<acq_rel%>, %<release%> or "
43670 "%<acquire%>");
43671 cp_lexer_consume_token (parser->lexer);
43673 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
43675 if (mo != MEMMODEL_LAST)
43676 error_at (cp_lexer_peek_token (parser->lexer)->location,
43677 "%<flush%> list specified together with memory order "
43678 "clause");
43679 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
43681 cp_parser_require_pragma_eol (parser, pragma_tok);
43683 finish_omp_flush (mo);
43686 /* Helper function, to parse omp for increment expression. */
43688 static tree
43689 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
43691 tree cond = cp_parser_binary_expression (parser, false, true,
43692 PREC_NOT_OPERATOR, NULL);
43693 if (cond == error_mark_node
43694 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
43696 cp_parser_skip_to_end_of_statement (parser);
43697 return error_mark_node;
43700 switch (TREE_CODE (cond))
43702 case GT_EXPR:
43703 case GE_EXPR:
43704 case LT_EXPR:
43705 case LE_EXPR:
43706 break;
43707 case NE_EXPR:
43708 if (code != OACC_LOOP)
43709 break;
43710 gcc_fallthrough ();
43711 default:
43712 return error_mark_node;
43715 /* If decl is an iterator, preserve LHS and RHS of the relational
43716 expr until finish_omp_for. */
43717 if (decl
43718 && (type_dependent_expression_p (decl)
43719 || CLASS_TYPE_P (TREE_TYPE (decl))))
43720 return cond;
43722 return build_x_binary_op (cp_expr_loc_or_input_loc (cond),
43723 TREE_CODE (cond),
43724 TREE_OPERAND (cond, 0), ERROR_MARK,
43725 TREE_OPERAND (cond, 1), ERROR_MARK,
43726 NULL_TREE, /*overload=*/NULL, tf_warning_or_error);
43729 /* Helper function, to parse omp for increment expression. */
43731 static tree
43732 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
43734 cp_token *token = cp_lexer_peek_token (parser->lexer);
43735 enum tree_code op;
43736 tree lhs, rhs;
43737 cp_id_kind idk;
43738 bool decl_first;
43740 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
43742 op = (token->type == CPP_PLUS_PLUS
43743 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
43744 cp_lexer_consume_token (parser->lexer);
43745 lhs = cp_parser_simple_cast_expression (parser);
43746 if (lhs != decl
43747 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
43748 return error_mark_node;
43749 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
43752 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
43753 if (lhs != decl
43754 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
43755 return error_mark_node;
43757 token = cp_lexer_peek_token (parser->lexer);
43758 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
43760 op = (token->type == CPP_PLUS_PLUS
43761 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
43762 cp_lexer_consume_token (parser->lexer);
43763 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
43766 op = cp_parser_assignment_operator_opt (parser);
43767 if (op == ERROR_MARK)
43768 return error_mark_node;
43770 if (op != NOP_EXPR)
43772 rhs = cp_parser_assignment_expression (parser);
43773 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
43774 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
43777 lhs = cp_parser_binary_expression (parser, false, false,
43778 PREC_ADDITIVE_EXPRESSION, NULL);
43779 token = cp_lexer_peek_token (parser->lexer);
43780 decl_first = (lhs == decl
43781 || (processing_template_decl && cp_tree_equal (lhs, decl)));
43782 if (decl_first)
43783 lhs = NULL_TREE;
43784 if (token->type != CPP_PLUS
43785 && token->type != CPP_MINUS)
43786 return error_mark_node;
43790 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
43791 cp_lexer_consume_token (parser->lexer);
43792 rhs = cp_parser_binary_expression (parser, false, false,
43793 PREC_ADDITIVE_EXPRESSION, NULL);
43794 token = cp_lexer_peek_token (parser->lexer);
43795 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
43797 if (lhs == NULL_TREE)
43799 if (op == PLUS_EXPR)
43800 lhs = rhs;
43801 else
43802 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
43803 NULL_TREE, tf_warning_or_error);
43805 else
43806 lhs = build_x_binary_op (input_location, op,
43807 lhs, ERROR_MARK,
43808 rhs, ERROR_MARK,
43809 NULL_TREE, NULL, tf_warning_or_error);
43812 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
43814 if (!decl_first)
43816 if ((rhs != decl
43817 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
43818 || op == MINUS_EXPR)
43819 return error_mark_node;
43820 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
43822 else
43823 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
43825 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
43828 /* Parse the initialization statement of an OpenMP for loop. Range-for
43829 is handled separately in cp_convert_omp_range_for.
43831 On entry SL is the current statement list. Parsing of some forms
43832 of initialization pops this list and stores its contents in either INIT
43833 or THIS_PRE_BODY, and sets SL to null. Initialization for class
43834 iterators is added directly to SL and it is not popped until later.
43836 On return, DECL is set if the initialization is by binding the
43837 iteration variable. If the initialization is by assignment, REAL_DECL
43838 is set to point to a variable in an outer scope. ORIG_INIT is set
43839 if the iteration variable is of class type; this is a copy saved for
43840 error checking in finish_omp_for.
43842 Return true if the resulting construct should have an
43843 OMP_CLAUSE_PRIVATE added to it. */
43845 static tree
43846 cp_parser_omp_for_loop_init (cp_parser *parser,
43847 tree &this_pre_body,
43848 tree &sl,
43849 tree &init,
43850 tree &orig_init,
43851 tree &decl,
43852 tree &real_decl)
43854 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
43855 return NULL_TREE;
43857 tree add_private_clause = NULL_TREE;
43859 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
43861 init-expr:
43862 var = lb
43863 integer-type var = lb
43864 random-access-iterator-type var = lb
43865 pointer-type var = lb
43867 cp_decl_specifier_seq type_specifiers;
43869 /* First, try to parse as an initialized declaration. See
43870 cp_parser_condition, from whence the bulk of this is copied. */
43872 cp_parser_parse_tentatively (parser);
43873 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
43874 /*is_declaration=*/true,
43875 /*is_trailing_return=*/false,
43876 &type_specifiers);
43877 if (cp_parser_parse_definitely (parser))
43879 /* If parsing a type specifier seq succeeded, then this
43880 MUST be a initialized declaration. */
43881 tree asm_specification, attributes;
43882 cp_declarator *declarator;
43884 declarator = cp_parser_declarator (parser,
43885 CP_PARSER_DECLARATOR_NAMED,
43886 CP_PARSER_FLAGS_NONE,
43887 /*ctor_dtor_or_conv_p=*/NULL,
43888 /*parenthesized_p=*/NULL,
43889 /*member_p=*/false,
43890 /*friend_p=*/false,
43891 /*static_p=*/false);
43892 attributes = cp_parser_attributes_opt (parser);
43893 asm_specification = cp_parser_asm_specification_opt (parser);
43895 if (declarator == cp_error_declarator)
43896 cp_parser_skip_to_end_of_statement (parser);
43898 else
43900 tree pushed_scope, auto_node;
43902 decl = start_decl (declarator, &type_specifiers,
43903 SD_INITIALIZED, attributes,
43904 /*prefix_attributes=*/NULL_TREE,
43905 &pushed_scope);
43907 auto_node = type_uses_auto (TREE_TYPE (decl));
43908 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
43910 if (cp_lexer_next_token_is (parser->lexer,
43911 CPP_OPEN_PAREN))
43912 error ("parenthesized initialization is not allowed in "
43913 "OpenMP %<for%> loop");
43914 else
43915 /* Trigger an error. */
43916 cp_parser_require (parser, CPP_EQ, RT_EQ);
43918 init = error_mark_node;
43919 cp_parser_skip_to_end_of_statement (parser);
43921 else if (CLASS_TYPE_P (TREE_TYPE (decl))
43922 || type_dependent_expression_p (decl)
43923 || auto_node)
43925 bool is_non_constant_init;
43927 init = cp_parser_initializer (parser,
43928 /*is_direct_init=*/nullptr,
43929 &is_non_constant_init);
43931 if (auto_node)
43933 TREE_TYPE (decl)
43934 = do_auto_deduction (TREE_TYPE (decl), init,
43935 auto_node);
43937 if (!CLASS_TYPE_P (TREE_TYPE (decl))
43938 && !type_dependent_expression_p (decl))
43939 goto non_class;
43942 cp_finish_decl (decl, init, !is_non_constant_init,
43943 asm_specification,
43944 LOOKUP_ONLYCONVERTING);
43945 orig_init = init;
43947 /* In the case of a class iterator, do not pop sl here.
43948 Both class initialization and finalization must happen in
43949 the enclosing init block scope. For now set the init
43950 expression to null; it'll be filled in properly in
43951 finish_omp_for before stuffing it in the OMP_FOR. */
43952 if (CLASS_TYPE_P (TREE_TYPE (decl)))
43953 init = NULL_TREE;
43954 else /* It is a parameterized type. */
43956 init = pop_stmt_list (sl);
43957 sl = NULL_TREE;
43958 if (init && TREE_CODE (init) == STATEMENT_LIST)
43960 tree_stmt_iterator i = tsi_start (init);
43961 /* Move lambda DECL_EXPRs to the enclosing block. */
43962 while (!tsi_end_p (i))
43964 tree t = tsi_stmt (i);
43965 if (TREE_CODE (t) == DECL_EXPR
43966 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
43968 tsi_delink (&i);
43969 add_stmt (t);
43970 continue;
43972 break;
43974 if (tsi_one_before_end_p (i))
43976 tree t = tsi_stmt (i);
43977 tsi_delink (&i);
43978 free_stmt_list (init);
43979 init = t;
43984 else
43985 /* This is an initialized declaration of non-class,
43986 non-parameterized type iteration variable. */
43988 /* Consume '='. */
43989 cp_lexer_consume_token (parser->lexer);
43990 init = cp_parser_assignment_expression (parser);
43992 non_class:
43993 if (TYPE_REF_P (TREE_TYPE (decl)))
43994 init = error_mark_node;
43995 else
43996 cp_finish_decl (decl, NULL_TREE,
43997 /*init_const_expr_p=*/false,
43998 asm_specification,
43999 LOOKUP_ONLYCONVERTING);
44000 this_pre_body = pop_stmt_list (sl);
44001 sl = NULL_TREE;
44004 if (pushed_scope)
44005 pop_scope (pushed_scope);
44008 else
44010 cp_id_kind idk;
44011 /* If parsing a type specifier sequence failed, then
44012 this MUST be a simple expression. */
44013 cp_parser_parse_tentatively (parser);
44014 decl = cp_parser_primary_expression (parser, false, false,
44015 false, &idk);
44016 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
44017 if (!cp_parser_error_occurred (parser)
44018 && decl
44019 && (TREE_CODE (decl) == COMPONENT_REF
44020 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
44022 cp_parser_abort_tentative_parse (parser);
44023 cp_parser_parse_tentatively (parser);
44024 cp_token *token = cp_lexer_peek_token (parser->lexer);
44025 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
44026 /*check_dependency_p=*/true,
44027 /*template_p=*/NULL,
44028 /*declarator_p=*/false,
44029 /*optional_p=*/false);
44030 if (name != error_mark_node
44031 && last_tok == cp_lexer_peek_token (parser->lexer))
44033 decl = cp_parser_lookup_name_simple (parser, name,
44034 token->location);
44035 if (TREE_CODE (decl) == FIELD_DECL)
44036 add_private_clause = omp_privatize_field (decl, false);
44038 cp_parser_abort_tentative_parse (parser);
44039 cp_parser_parse_tentatively (parser);
44040 decl = cp_parser_primary_expression (parser, false, false,
44041 false, &idk);
44043 if (!cp_parser_error_occurred (parser)
44044 && decl
44045 && DECL_P (decl)
44046 && CLASS_TYPE_P (TREE_TYPE (decl)))
44048 tree rhs;
44050 cp_parser_parse_definitely (parser);
44051 cp_parser_require (parser, CPP_EQ, RT_EQ);
44052 rhs = cp_parser_assignment_expression (parser);
44053 orig_init = rhs;
44054 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
44055 decl, NOP_EXPR,
44056 rhs, NULL_TREE,
44057 tf_warning_or_error));
44058 if (!add_private_clause)
44059 add_private_clause = decl;
44061 else
44063 decl = NULL;
44064 cp_parser_abort_tentative_parse (parser);
44065 init = cp_parser_expression (parser);
44066 if (init)
44068 if (TREE_CODE (init) == MODIFY_EXPR
44069 || TREE_CODE (init) == MODOP_EXPR)
44070 real_decl = TREE_OPERAND (init, 0);
44073 this_pre_body = pop_stmt_list (sl);
44074 sl = NULL_TREE;
44076 return add_private_clause;
44079 /* Helper for cp_parser_omp_loop_nest, handle one range-for loop
44080 including introducing new temporaries for the range start and end,
44081 doing auto deduction, and processing decomposition variables.
44083 This function is also called from pt.cc during template instantiation.
44084 In that case SL is NULL_TREE, otherwise it is the current statement
44085 list. */
44086 void
44087 cp_convert_omp_range_for (tree &this_pre_body, tree &sl,
44088 tree &decl, tree &orig_decl, tree &init,
44089 tree &orig_init, tree &cond, tree &incr)
44091 tree begin, end, range_temp_decl = NULL_TREE;
44092 tree iter_type, begin_expr, end_expr;
44093 bool clear_has_value_expr = false;
44095 if (processing_template_decl)
44097 if (check_for_bare_parameter_packs (init))
44098 init = error_mark_node;
44099 if (!type_dependent_expression_p (init)
44100 /* do_auto_deduction doesn't mess with template init-lists. */
44101 && !BRACE_ENCLOSED_INITIALIZER_P (init))
44103 tree d = decl;
44104 cp_decomp decomp_d, *decomp = NULL;
44105 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
44107 tree v = DECL_VALUE_EXPR (decl);
44108 if (TREE_CODE (v) == ARRAY_REF
44109 && VAR_P (TREE_OPERAND (v, 0))
44110 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
44112 d = TREE_OPERAND (v, 0);
44113 decomp = &decomp_d;
44114 decomp->count = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
44115 decomp->decl = decl;
44118 do_range_for_auto_deduction (d, init, decomp);
44120 cond = global_namespace;
44121 incr = NULL_TREE;
44122 orig_init = init;
44123 if (sl)
44125 this_pre_body = pop_stmt_list (sl);
44126 sl = NULL_TREE;
44128 return;
44131 init = mark_lvalue_use (init);
44133 if (decl == error_mark_node || init == error_mark_node)
44134 /* If an error happened previously do nothing or else a lot of
44135 unhelpful errors would be issued. */
44136 begin_expr = end_expr = iter_type = error_mark_node;
44137 else
44139 tree range_temp;
44141 if (VAR_P (init)
44142 && array_of_runtime_bound_p (TREE_TYPE (init)))
44143 /* Can't bind a reference to an array of runtime bound. */
44144 range_temp = init;
44145 else
44147 range_temp = build_range_temp (init);
44148 DECL_NAME (range_temp) = NULL_TREE;
44149 pushdecl (range_temp);
44150 cp_finish_decl (range_temp, init,
44151 /*is_constant_init*/false, NULL_TREE,
44152 LOOKUP_ONLYCONVERTING);
44153 range_temp_decl = range_temp;
44154 range_temp = convert_from_reference (range_temp);
44156 iter_type = cp_parser_perform_range_for_lookup (range_temp,
44157 &begin_expr, &end_expr);
44160 tree end_iter_type = iter_type;
44161 if (cxx_dialect >= cxx17)
44162 end_iter_type = cv_unqualified (TREE_TYPE (end_expr));
44163 end = build_decl (input_location, VAR_DECL, NULL_TREE, end_iter_type);
44164 TREE_USED (end) = 1;
44165 DECL_ARTIFICIAL (end) = 1;
44166 pushdecl (end);
44167 cp_finish_decl (end, end_expr,
44168 /*is_constant_init*/false, NULL_TREE,
44169 LOOKUP_ONLYCONVERTING);
44171 /* The new for initialization statement. */
44172 begin = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
44173 TREE_USED (begin) = 1;
44174 DECL_ARTIFICIAL (begin) = 1;
44175 pushdecl (begin);
44176 orig_init = init;
44177 if (CLASS_TYPE_P (iter_type))
44178 init = NULL_TREE;
44179 else
44181 init = begin_expr;
44182 begin_expr = NULL_TREE;
44184 cp_finish_decl (begin, begin_expr,
44185 /*is_constant_init*/false, NULL_TREE,
44186 LOOKUP_ONLYCONVERTING);
44188 /* The new for condition. */
44189 if (CLASS_TYPE_P (iter_type))
44190 cond = build2 (NE_EXPR, boolean_type_node, begin, end);
44191 else
44192 cond = build_x_binary_op (input_location, NE_EXPR,
44193 begin, ERROR_MARK,
44194 end, ERROR_MARK,
44195 NULL_TREE, NULL, tf_warning_or_error);
44197 /* The new increment expression. */
44198 if (CLASS_TYPE_P (iter_type))
44199 incr = build2 (PREINCREMENT_EXPR, iter_type, begin, NULL_TREE);
44200 else
44201 incr = finish_unary_op_expr (input_location,
44202 PREINCREMENT_EXPR, begin,
44203 tf_warning_or_error);
44205 orig_decl = decl;
44206 decl = begin;
44207 /* Defer popping sl here. */
44209 cp_decomp decomp_d, *decomp = NULL;
44210 if (orig_decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (orig_decl))
44212 tree v = DECL_VALUE_EXPR (orig_decl);
44213 if (TREE_CODE (v) == ARRAY_REF
44214 && VAR_P (TREE_OPERAND (v, 0))
44215 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
44217 tree d = orig_decl;
44218 orig_decl = TREE_OPERAND (v, 0);
44219 decomp = &decomp_d;
44220 decomp->count = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
44221 decomp->decl = d;
44225 tree auto_node = type_uses_auto (TREE_TYPE (orig_decl));
44226 if (auto_node)
44228 tree t = build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
44229 NULL_TREE, tf_none);
44230 if (!error_operand_p (t))
44232 TREE_TYPE (orig_decl) = do_auto_deduction (TREE_TYPE (orig_decl),
44233 t, auto_node);
44234 if (decomp)
44236 ++processing_template_decl;
44237 cp_finish_decomp (orig_decl, decomp);
44238 --processing_template_decl;
44239 if (!processing_template_decl)
44240 clear_has_value_expr = true;
44245 /* The output ORIG_DECL is not a decl. Instead, it is a tree structure
44246 that holds decls for variables implementing the iterator, represented
44247 as a TREE_LIST whose TREE_CHAIN is a vector. The first two elements
44248 of the vector are decls of scratch variables for the range start and
44249 end that will eventually be bound in the implicit scope surrounding
44250 the whole loop nest. The remaining elements are decls of derived
44251 decomposition variables that are bound inside the loop body. This
44252 structure is further mangled by finish_omp_for into the form required
44253 for the OMP_FOR_ORIG_DECLS field of the OMP_FOR tree node. */\
44254 unsigned decomp_cnt = decomp ? decomp->count : 0;
44255 tree v = make_tree_vec (decomp_cnt + 3);
44256 TREE_VEC_ELT (v, 0) = range_temp_decl;
44257 TREE_VEC_ELT (v, 1) = end;
44258 TREE_VEC_ELT (v, 2) = orig_decl;
44259 if (clear_has_value_expr)
44260 TREE_PUBLIC (v) = 1;
44261 for (unsigned i = 0; i < decomp_cnt; i++)
44263 if (clear_has_value_expr)
44265 /* If cp_finish_decomp was called with processing_template_decl
44266 temporarily set to 1, then decomp names will have deduced
44267 name but the DECL_VALUE_EXPR will be dependent. Hide those
44268 from folding of other loop initializers e.g. for warning
44269 purposes until cp_finish_omp_range_for. */
44270 gcc_checking_assert (DECL_HAS_VALUE_EXPR_P (decomp->decl)
44271 || (TREE_TYPE (decomp->decl)
44272 == error_mark_node));
44273 DECL_HAS_VALUE_EXPR_P (decomp->decl) = 0;
44275 TREE_VEC_ELT (v, i + 3) = decomp->decl;
44276 decomp->decl = DECL_CHAIN (decomp->decl);
44278 orig_decl = tree_cons (NULL_TREE, NULL_TREE, v);
44281 /* Helper for cp_parser_omp_for_loop, finalize part of range for
44282 inside of the collapsed body. */
44284 void
44285 cp_finish_omp_range_for (tree orig, tree begin)
44287 gcc_assert (TREE_CODE (orig) == TREE_LIST
44288 && TREE_CODE (TREE_CHAIN (orig)) == TREE_VEC);
44289 tree decl = TREE_VEC_ELT (TREE_CHAIN (orig), 2);
44290 cp_decomp decomp_d, *decomp = NULL;
44292 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
44294 decomp = &decomp_d;
44295 decomp_d.decl = TREE_VEC_ELT (TREE_CHAIN (orig), 3);
44296 decomp_d.count = TREE_VEC_LENGTH (TREE_CHAIN (orig)) - 3;
44297 if (TREE_PUBLIC (TREE_CHAIN (orig)))
44299 /* Undo temporary clearing of DECL_HAS_VALUE_EXPR_P done
44300 by cp_convert_omp_range_for above. */
44301 TREE_PUBLIC (TREE_CHAIN (orig)) = 0;
44302 tree d = decomp_d.decl;
44303 for (unsigned i = 0; i < decomp_d.count; i++)
44305 if (TREE_TYPE (d) != error_mark_node)
44306 DECL_HAS_VALUE_EXPR_P (d) = 1;
44307 d = DECL_CHAIN (d);
44312 /* The declaration is initialized with *__begin inside the loop body. */
44313 cp_finish_decl (decl,
44314 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
44315 NULL_TREE, tf_warning_or_error),
44316 /*is_constant_init*/false, NULL_TREE,
44317 LOOKUP_ONLYCONVERTING, decomp);
44318 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
44319 cp_finish_decomp (decl, decomp);
44322 /* Return true if next tokens contain a standard attribute that contains
44323 omp::directive (DIRECTIVE). */
44325 static bool
44326 cp_parser_omp_section_scan (cp_parser *parser, const char *directive,
44327 bool tentative)
44329 size_t n = cp_parser_skip_attributes_opt (parser, 1), i;
44330 if (n < 10)
44331 return false;
44332 for (i = 5; i < n - 4; i++)
44333 if (cp_lexer_nth_token_is (parser->lexer, i, CPP_NAME)
44334 && cp_lexer_nth_token_is (parser->lexer, i + 1, CPP_OPEN_PAREN)
44335 && cp_lexer_nth_token_is (parser->lexer, i + 2, CPP_NAME))
44337 tree first = cp_lexer_peek_nth_token (parser->lexer, i)->u.value;
44338 tree second = cp_lexer_peek_nth_token (parser->lexer, i + 2)->u.value;
44339 if (strcmp (IDENTIFIER_POINTER (first), "directive")
44340 && strcmp (IDENTIFIER_POINTER (first), "__directive__"))
44341 continue;
44342 if (strcmp (IDENTIFIER_POINTER (second), directive) == 0)
44343 break;
44345 if (i == n - 4)
44346 return false;
44347 cp_parser_parse_tentatively (parser);
44348 location_t first_loc = cp_lexer_peek_token (parser->lexer)->location;
44349 location_t last_loc
44350 = cp_lexer_peek_nth_token (parser->lexer, n - 1)->location;
44351 location_t middle_loc = UNKNOWN_LOCATION;
44352 tree std_attrs = cp_parser_std_attribute_spec_seq (parser);
44353 int cnt = 0;
44354 bool seen = false;
44355 for (tree attr = std_attrs; attr; attr = TREE_CHAIN (attr))
44356 if (get_attribute_namespace (attr) == omp_identifier
44357 && is_attribute_p ("directive", get_attribute_name (attr)))
44359 for (tree a = TREE_VALUE (attr); a; a = TREE_CHAIN (a))
44361 tree d = TREE_VALUE (a);
44362 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
44363 cp_token *first = DEFPARSE_TOKENS (d)->first;
44364 cnt++;
44365 if (first->type == CPP_NAME
44366 && strcmp (IDENTIFIER_POINTER (first->u.value),
44367 directive) == 0)
44369 seen = true;
44370 if (middle_loc == UNKNOWN_LOCATION)
44371 middle_loc = first->location;
44375 if (!seen || tentative)
44377 cp_parser_abort_tentative_parse (parser);
44378 return seen;
44380 if (cnt != 1 || TREE_CHAIN (std_attrs))
44382 error_at (make_location (first_loc, last_loc, middle_loc),
44383 "%<[[omp::directive(%s)]]%> must be the only specified "
44384 "attribute on a statement", directive);
44385 cp_parser_abort_tentative_parse (parser);
44386 return false;
44388 if (!cp_parser_parse_definitely (parser))
44389 return false;
44390 cp_parser_handle_statement_omp_attributes (parser, std_attrs);
44391 return true;
44394 /* Parse an OpenMP structured block sequence. KIND is the corresponding
44395 separating directive. */
44397 static tree
44398 cp_parser_omp_structured_block_sequence (cp_parser *parser,
44399 enum pragma_kind kind)
44401 tree stmt = begin_omp_structured_block ();
44402 unsigned int save = cp_parser_begin_omp_structured_block (parser);
44404 cp_parser_statement (parser, NULL_TREE, false, NULL);
44405 while (true)
44407 cp_token *token = cp_lexer_peek_token (parser->lexer);
44409 if (token->type == CPP_CLOSE_BRACE
44410 || token->type == CPP_EOF
44411 || token->type == CPP_PRAGMA_EOL
44412 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END)
44413 || (kind != PRAGMA_NONE
44414 && cp_parser_pragma_kind (token) == kind))
44415 break;
44417 if (kind != PRAGMA_NONE
44418 && cp_parser_omp_section_scan (parser,
44419 kind == PRAGMA_OMP_SCAN
44420 ? "scan" : "section", false))
44421 break;
44423 cp_parser_statement (parser, NULL_TREE, false, NULL);
44426 cp_parser_end_omp_structured_block (parser, save);
44427 return finish_omp_structured_block (stmt);
44431 /* OpenMP 5.0:
44433 scan-loop-body:
44434 { structured-block scan-directive structured-block } */
44436 static void
44437 cp_parser_omp_scan_loop_body (cp_parser *parser)
44439 tree substmt, clauses = NULL_TREE;
44440 bool found_scan = false;
44442 matching_braces braces;
44443 if (!braces.require_open (parser))
44444 return;
44446 cp_token *tok = cp_lexer_peek_token (parser->lexer);
44447 if (cp_parser_pragma_kind (tok) != PRAGMA_OMP_SCAN)
44448 substmt = cp_parser_omp_structured_block_sequence (parser, PRAGMA_OMP_SCAN);
44449 else
44451 warning_at (tok->location, OPT_Wopenmp,
44452 "%<#pragma omp scan%> with zero preceding executable "
44453 "statements");
44454 substmt = build_empty_stmt (tok->location);
44456 substmt = build2 (OMP_SCAN, void_type_node, substmt, NULL_TREE);
44457 add_stmt (substmt);
44459 tok = cp_lexer_peek_token (parser->lexer);
44460 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SCAN)
44462 enum omp_clause_code clause = OMP_CLAUSE_ERROR;
44463 found_scan = true;
44465 cp_lexer_consume_token (parser->lexer);
44467 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
44468 cp_lexer_consume_token (parser->lexer);
44470 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
44472 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
44473 const char *p = IDENTIFIER_POINTER (id);
44474 if (strcmp (p, "inclusive") == 0)
44475 clause = OMP_CLAUSE_INCLUSIVE;
44476 else if (strcmp (p, "exclusive") == 0)
44477 clause = OMP_CLAUSE_EXCLUSIVE;
44479 if (clause != OMP_CLAUSE_ERROR)
44481 cp_lexer_consume_token (parser->lexer);
44482 clauses = cp_parser_omp_var_list (parser, clause, NULL_TREE);
44484 else
44485 cp_parser_error (parser, "expected %<inclusive%> or "
44486 "%<exclusive%> clause");
44488 cp_parser_require_pragma_eol (parser, tok);
44490 else
44491 error ("expected %<#pragma omp scan%>");
44493 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
44494 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
44495 substmt = cp_parser_omp_structured_block_sequence (parser, PRAGMA_NONE);
44496 else
44498 if (found_scan)
44499 warning_at (tok->location, OPT_Wopenmp,
44500 "%<#pragma omp scan%> with zero succeeding executable "
44501 "statements");
44502 substmt = build_empty_stmt (tok->location);
44504 substmt = build2_loc (tok->location, OMP_SCAN, void_type_node, substmt,
44505 clauses);
44506 add_stmt (substmt);
44508 braces.require_close (parser);
44512 /* This function parses a single level of a loop nest, invoking itself
44513 recursively if necessary.
44515 loop-nest :: for (...) loop-body
44516 loop-body :: loop-nest
44517 | { [intervening-code] loop-body [intervening-code] }
44518 | final-loop-body
44519 intervening-code :: structured-block-sequence
44520 final-loop-body :: structured-block
44522 For a collapsed loop nest, only a single OMP_FOR is built, pulling out
44523 all the iterator information from the inner loops into vectors in the
44524 parser->omp_for_parse_state structure.
44526 In the "range for" case, it is transformed into a regular "for" iterator
44527 by introducing some temporary variables for the begin/end,
44528 as well as bindings of the actual iteration variables which are
44529 injected into the body of the loop.
44531 Initialization code for iterator variables may end up either in the
44532 init vector (simple assignments), in omp_for_parse_state->pre_body
44533 (decl_exprs for iterators bound in the for statement), or in the
44534 scope surrounding this level of loop initialization.
44536 The scopes of class iterator variables and their finalizers need to
44537 be adjusted after parsing so that all of the initialization happens
44538 in a scope surrounding all of the intervening and body code. For
44539 this reason we separately store the initialization and body blocks
44540 for each level of loops in the omp_for_parse_state structure and
44541 reassemble/reorder them in cp_parser_omp_for. See additional
44542 comments there about the use of placeholders, etc. */
44544 static tree
44545 cp_parser_omp_loop_nest (cp_parser *parser, bool *if_p)
44547 tree decl, cond, incr, init;
44548 tree orig_init, real_decl, orig_decl;
44549 tree init_block, body_block;
44550 tree init_placeholder, body_placeholder;
44551 tree init_scope;
44552 tree this_pre_body = NULL_TREE;
44553 bool moreloops;
44554 unsigned char save_in_statement;
44555 tree add_private_clause = NULL_TREE;
44556 location_t loc;
44557 bool is_range_for = false;
44558 tree sl = NULL_TREE;
44559 struct omp_for_parse_data *omp_for_parse_state
44560 = parser->omp_for_parse_state;
44561 gcc_assert (omp_for_parse_state);
44562 int depth = omp_for_parse_state->depth;
44564 /* We have already matched the FOR token but not consumed it yet. */
44565 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR));
44566 loc = cp_lexer_consume_token (parser->lexer)->location;
44568 /* Forbid break/continue in the loop initializer, condition, and
44569 increment expressions. */
44570 save_in_statement = parser->in_statement;
44571 parser->in_statement = IN_OMP_BLOCK;
44573 /* We are not in intervening code now. */
44574 omp_for_parse_state->in_intervening_code = false;
44576 /* Don't create location wrapper nodes within an OpenMP "for"
44577 statement. */
44578 auto_suppress_location_wrappers sentinel;
44580 matching_parens parens;
44581 if (!parens.require_open (parser))
44582 return NULL;
44584 init = orig_init = decl = real_decl = orig_decl = NULL_TREE;
44586 init_placeholder = build_stmt (input_location, EXPR_STMT,
44587 integer_zero_node);
44588 vec_safe_push (omp_for_parse_state->init_placeholderv, init_placeholder);
44590 /* The init_block acts as a container for this level of loop goo. */
44591 init_block = push_stmt_list ();
44592 vec_safe_push (omp_for_parse_state->init_blockv, init_block);
44594 /* Wrap a scope around this entire level of loop to hold bindings
44595 of loop iteration variables. We can't insert them directly
44596 in the containing scope because that would cause their visibility to
44597 be incorrect with respect to intervening code after this loop.
44598 We will combine the nested init_scopes in postprocessing after the
44599 entire loop is parsed. */
44600 init_scope = begin_compound_stmt (0);
44602 /* Now we need another level of statement list container to capture the
44603 initialization (and possible finalization) bits. In some cases this
44604 container may be popped off during initializer parsing to store code in
44605 INIT or THIS_PRE_BODY, depending on the form of initialization. If
44606 we have a class iterator we will pop it at the end of parsing this
44607 level, so the cleanups are handled correctly. */
44608 sl = push_stmt_list ();
44610 if (omp_for_parse_state->code != OACC_LOOP && cxx_dialect >= cxx11)
44612 /* Save tokens so that we can put them back. */
44613 cp_lexer_save_tokens (parser->lexer);
44615 /* Look for ':' that is not nested in () or {}. */
44616 is_range_for
44617 = (cp_parser_skip_to_closing_parenthesis_1 (parser,
44618 /*recovering=*/false,
44619 CPP_COLON,
44620 /*consume_paren=*/
44621 false) == -1);
44623 /* Roll back the tokens we skipped. */
44624 cp_lexer_rollback_tokens (parser->lexer);
44626 if (is_range_for)
44628 bool saved_colon_corrects_to_scope_p
44629 = parser->colon_corrects_to_scope_p;
44631 /* A colon is used in range-based for. */
44632 parser->colon_corrects_to_scope_p = false;
44634 /* Parse the declaration. */
44635 cp_parser_simple_declaration (parser,
44636 /*function_definition_allowed_p=*/
44637 false, &decl);
44638 parser->colon_corrects_to_scope_p
44639 = saved_colon_corrects_to_scope_p;
44641 cp_parser_require (parser, CPP_COLON, RT_COLON);
44643 init = cp_parser_range_for (parser, NULL_TREE, NULL_TREE, decl,
44644 false, NULL_TREE, false, true);
44646 cp_convert_omp_range_for (this_pre_body, sl, decl,
44647 orig_decl, init, orig_init,
44648 cond, incr);
44650 if (omp_for_parse_state->ordered_cl)
44651 error_at (OMP_CLAUSE_LOCATION (omp_for_parse_state->ordered_cl),
44652 "%<ordered%> clause with parameter on "
44653 "range-based %<for%> loop");
44655 goto parse_close_paren;
44659 add_private_clause
44660 = cp_parser_omp_for_loop_init (parser, this_pre_body, sl,
44661 init, orig_init, decl, real_decl);
44663 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
44665 /* If the iteration variable was introduced via a declaration in the
44666 for statement, DECL points at it. Otherwise DECL is null and
44667 REAL_DECL is a variable previously declared in an outer scope.
44668 Make REAL_DECL point at the iteration variable no matter where it
44669 was introduced. */
44670 if (decl)
44671 real_decl = decl;
44673 /* Some clauses treat iterator variables specially. */
44674 if (omp_for_parse_state->cclauses != NULL
44675 && omp_for_parse_state->cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
44676 && real_decl != NULL_TREE
44677 && omp_for_parse_state->code != OMP_LOOP)
44679 tree *c;
44680 for (c = &(omp_for_parse_state->cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]);
44681 *c ; )
44682 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
44683 && OMP_CLAUSE_DECL (*c) == real_decl)
44685 error_at (loc, "iteration variable %qD"
44686 " should not be firstprivate", real_decl);
44687 *c = OMP_CLAUSE_CHAIN (*c);
44689 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
44690 && OMP_CLAUSE_DECL (*c) == real_decl)
44692 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
44693 tree l = *c;
44694 *c = OMP_CLAUSE_CHAIN (*c);
44695 if (omp_for_parse_state->code == OMP_SIMD)
44697 OMP_CLAUSE_CHAIN (l)
44698 = omp_for_parse_state->cclauses[C_OMP_CLAUSE_SPLIT_FOR];
44699 omp_for_parse_state->cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
44701 else
44703 OMP_CLAUSE_CHAIN (l) = omp_for_parse_state->clauses;
44704 omp_for_parse_state->clauses = l;
44706 add_private_clause = NULL_TREE;
44708 else
44710 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
44711 && OMP_CLAUSE_DECL (*c) == real_decl)
44712 add_private_clause = NULL_TREE;
44713 c = &OMP_CLAUSE_CHAIN (*c);
44717 if (add_private_clause)
44719 tree c;
44720 for (c = omp_for_parse_state->clauses; c ; c = OMP_CLAUSE_CHAIN (c))
44722 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
44723 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
44724 && OMP_CLAUSE_DECL (c) == decl)
44725 break;
44726 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
44727 && OMP_CLAUSE_DECL (c) == decl)
44728 error_at (loc, "iteration variable %qD "
44729 "should not be firstprivate",
44730 decl);
44731 else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
44732 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
44733 && OMP_CLAUSE_DECL (c) == decl)
44734 error_at (loc, "iteration variable %qD should not be reduction",
44735 decl);
44737 if (c == NULL)
44739 if ((omp_for_parse_state->code == OMP_SIMD
44740 && omp_for_parse_state->count != 1)
44741 || omp_for_parse_state->code == OMP_LOOP)
44742 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
44743 else if (omp_for_parse_state->code != OMP_SIMD)
44744 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
44745 else
44746 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
44747 OMP_CLAUSE_DECL (c) = add_private_clause;
44748 c = finish_omp_clauses (c, C_ORT_OMP);
44749 if (c)
44751 OMP_CLAUSE_CHAIN (c) = omp_for_parse_state->clauses;
44752 omp_for_parse_state->clauses = c;
44753 /* For linear, signal that we need to fill up
44754 the so far unknown linear step. */
44755 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
44756 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
44761 cond = NULL;
44762 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
44763 cond = cp_parser_omp_for_cond (parser, decl, omp_for_parse_state->code);
44764 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
44766 incr = NULL;
44767 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
44769 /* If decl is an iterator, preserve the operator on decl
44770 until finish_omp_for. */
44771 if (real_decl
44772 && ((processing_template_decl
44773 && (TREE_TYPE (real_decl) == NULL_TREE
44774 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl))))
44775 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
44776 incr = cp_parser_omp_for_incr (parser, real_decl);
44777 else
44778 incr = cp_parser_expression (parser);
44779 protected_set_expr_location_if_unset (incr, input_location);
44782 parse_close_paren:
44783 if (!parens.require_close (parser))
44784 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
44785 /*or_comma=*/false,
44786 /*consume_paren=*/true);
44788 /* We've parsed all the for (...) stuff now. Store the bits. */
44789 TREE_VEC_ELT (omp_for_parse_state->declv, depth) = decl;
44790 TREE_VEC_ELT (omp_for_parse_state->initv, depth) = init;
44791 TREE_VEC_ELT (omp_for_parse_state->condv, depth) = cond;
44792 TREE_VEC_ELT (omp_for_parse_state->incrv, depth) = incr;
44793 if (orig_init)
44795 omp_for_parse_state->orig_inits.safe_grow_cleared (depth + 1, true);
44796 omp_for_parse_state->orig_inits[depth] = orig_init;
44798 if (orig_decl)
44800 if (!omp_for_parse_state->orig_declv)
44801 omp_for_parse_state->orig_declv
44802 = copy_node (omp_for_parse_state->declv);
44803 TREE_VEC_ELT (omp_for_parse_state->orig_declv, depth) = orig_decl;
44805 else if (omp_for_parse_state->orig_declv)
44806 TREE_VEC_ELT (omp_for_parse_state->orig_declv, depth) = decl;
44807 if (this_pre_body)
44808 append_to_statement_list_force (this_pre_body,
44809 &(omp_for_parse_state->pre_body));
44811 /* Start a nested block for the loop body. */
44812 body_placeholder = build_stmt (input_location, EXPR_STMT,
44813 integer_zero_node);
44814 vec_safe_push (omp_for_parse_state->body_placeholderv, body_placeholder);
44815 body_block = push_stmt_list ();
44816 vec_safe_push (omp_for_parse_state->body_blockv, body_block);
44818 moreloops = depth < omp_for_parse_state->count - 1;
44819 omp_for_parse_state->want_nested_loop = moreloops;
44820 if (moreloops && cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
44822 omp_for_parse_state->depth++;
44823 add_stmt (cp_parser_omp_loop_nest (parser, if_p));
44824 omp_for_parse_state->depth--;
44826 else if (moreloops
44827 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
44829 /* This is the open brace in the loop-body grammar production. Rather
44830 than trying to special-case braces, just parse it as a compound
44831 statement and handle the nested loop-body case there. Note that
44832 when we see a further open brace inside the compound statement
44833 loop-body, we don't know whether it is the start of intervening
44834 code that is a compound statement, or a level of braces
44835 surrounding a nested loop-body. Use the WANT_NESTED_LOOP state
44836 bit to ensure we have only one nested loop at each level. */
44838 omp_for_parse_state->in_intervening_code = true;
44839 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
44840 omp_for_parse_state->in_intervening_code = false;
44842 if (omp_for_parse_state->want_nested_loop)
44844 /* We have already parsed the whole loop body and not found a
44845 nested loop. */
44846 error_at (omp_for_parse_state->for_loc,
44847 "not enough nested loops");
44848 omp_for_parse_state->fail = true;
44850 if_p = NULL;
44852 else
44854 /* This is the final-loop-body case in the grammar: we have something
44855 that is not a FOR and not an open brace. */
44856 if (moreloops)
44858 /* If we were expecting a nested loop, give an error and mark
44859 that parsing has failed, and try to recover by parsing the
44860 body as regular code without further collapsing. */
44861 error_at (omp_for_parse_state->for_loc,
44862 "not enough nested loops");
44863 omp_for_parse_state->fail = true;
44865 parser->in_statement = IN_OMP_FOR;
44867 /* Generate the parts of range for that belong in the loop body,
44868 to be executed on every iteration. This includes setting the
44869 user-declared decomposition variables from the compiler-generated
44870 temporaries that are the real iteration variables for OMP_FOR.
44871 FIXME: Not sure if this is correct with respect to visibility
44872 of the variables from intervening code. However, putting this
44873 code in each level of loop instead of all around the innermost
44874 body also makes the decomposition variables visible to the
44875 inner for init/bound/step exressions, which is not supposed to
44876 happen and causes test failures. */
44877 if (omp_for_parse_state->orig_declv)
44878 for (int i = 0; i < omp_for_parse_state->count; i++)
44880 tree o = TREE_VEC_ELT (omp_for_parse_state->orig_declv, i);
44881 tree d = TREE_VEC_ELT (omp_for_parse_state->declv, i);
44882 if (o != d)
44883 cp_finish_omp_range_for (o, d);
44886 /* Now parse the final-loop-body for the innermost loop. */
44887 parser->omp_for_parse_state = NULL;
44888 if (omp_for_parse_state->inscan)
44889 cp_parser_omp_scan_loop_body (parser);
44890 else
44891 cp_parser_statement (parser, NULL_TREE, false, if_p);
44892 parser->omp_for_parse_state = omp_for_parse_state;
44894 parser->in_statement = save_in_statement;
44895 omp_for_parse_state->want_nested_loop = false;
44896 omp_for_parse_state->in_intervening_code = true;
44898 /* Pop and remember the body block. Add the body placeholder
44899 to the surrounding statement list instead. This is just a unique
44900 token that will be replaced when we reassemble the generated
44901 code for the entire omp for statement. */
44902 body_block = pop_stmt_list (body_block);
44903 omp_for_parse_state->body_blockv[depth] = body_block;
44904 add_stmt (body_placeholder);
44906 /* Pop and remember the init block. */
44907 if (sl)
44908 add_stmt (pop_stmt_list (sl));
44909 finish_compound_stmt (init_scope);
44910 init_block = pop_stmt_list (init_block);
44911 omp_for_parse_state->init_blockv[depth] = init_block;
44913 /* Return the init placeholder rather than the remembered init block.
44914 Again, this is just a unique cookie that will be used to reassemble
44915 code pieces when the entire omp for statement has been parsed. */
44916 return init_placeholder;
44919 /* Worker for find_structured_blocks. *TP points to a STATEMENT_LIST
44920 and ITER is the element that is or contains a nested loop. This
44921 function moves the statements before and after ITER into
44922 OMP_STRUCTURED_BLOCKs and modifies *TP. */
44923 static void
44924 insert_structured_blocks (tree *tp, tree_stmt_iterator iter)
44926 tree sl = push_stmt_list ();
44927 for (tree_stmt_iterator i = tsi_start (*tp); !tsi_end_p (i); )
44928 if (i == iter)
44930 sl = pop_stmt_list (sl);
44931 if (TREE_CODE (sl) != STATEMENT_LIST || !tsi_end_p (tsi_start (sl)))
44932 tsi_link_before (&i,
44933 build1 (OMP_STRUCTURED_BLOCK, void_type_node, sl),
44934 TSI_SAME_STMT);
44935 i++;
44936 sl = push_stmt_list ();
44938 else
44940 tree s = tsi_stmt (i);
44941 tsi_delink (&i); /* Advances i to next statement. */
44942 add_stmt (s);
44944 sl = pop_stmt_list (sl);
44945 if (TREE_CODE (sl) != STATEMENT_LIST || !tsi_end_p (tsi_start (sl)))
44946 tsi_link_after (&iter,
44947 build1 (OMP_STRUCTURED_BLOCK, void_type_node, sl),
44948 TSI_SAME_STMT);
44951 /* Helper to find and mark structured blocks in intervening code for a
44952 single loop level with markers for later error checking. *TP is the
44953 piece of code to be marked and INNER is the inner loop placeholder.
44954 Returns true if INNER was found (recursively) in *TP. */
44955 static bool
44956 find_structured_blocks (tree *tp, tree inner)
44958 if (*tp == inner)
44959 return true;
44960 else if (TREE_CODE (*tp) == BIND_EXPR)
44961 return find_structured_blocks (&(BIND_EXPR_BODY (*tp)), inner);
44962 else if (TREE_CODE (*tp) == STATEMENT_LIST)
44964 for (tree_stmt_iterator i = tsi_start (*tp); !tsi_end_p (i); ++i)
44966 tree *p = tsi_stmt_ptr (i);
44967 /* The normal case is that there is no intervening code and we
44968 do not have to insert any OMP_STRUCTURED_BLOCK markers. */
44969 if (find_structured_blocks (p, inner))
44971 if (!(i == tsi_start (*tp) && i == tsi_last (*tp)))
44972 insert_structured_blocks (tp, i);
44973 return true;
44976 return false;
44978 else if (TREE_CODE (*tp) == TRY_FINALLY_EXPR)
44979 return find_structured_blocks (&(TREE_OPERAND (*tp, 0)), inner);
44980 else if (TREE_CODE (*tp) == CLEANUP_STMT)
44981 return find_structured_blocks (&(CLEANUP_BODY (*tp)), inner);
44982 else
44983 return false;
44986 /* Helpers used for relinking tree structures: In tree rooted at
44987 CONTEXT, replace ORIG with REPLACEMENT. If FLATTEN is true, try to combine
44988 nested BIND_EXPRs. Gives an assertion if it fails to find ORIG. */
44990 struct sit_data {
44991 tree orig;
44992 tree repl;
44993 bool flatten;
44996 static tree
44997 substitute_in_tree_walker (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
44998 void *dp)
45000 struct sit_data *sit = (struct sit_data *)dp;
45001 if (*tp == sit->orig)
45003 *tp = sit->repl;
45004 return *tp;
45006 /* Remove redundant BIND_EXPRs with no bindings even when not specifically
45007 trying to flatten. */
45008 else if (TREE_CODE (*tp) == BIND_EXPR
45009 && BIND_EXPR_BODY (*tp) == sit->orig
45010 && !BIND_EXPR_VARS (*tp)
45011 && (sit->flatten || TREE_CODE (sit->repl) == BIND_EXPR))
45013 *tp = sit->repl;
45014 return *tp;
45016 else if (sit->flatten
45017 && TREE_CODE (*tp) == BIND_EXPR
45018 && TREE_CODE (sit->repl) == BIND_EXPR)
45020 if (BIND_EXPR_BODY (*tp) == sit->orig)
45022 /* Merge binding lists for two directly nested BIND_EXPRs,
45023 keeping the outer one. */
45024 BIND_EXPR_VARS (*tp) = chainon (BIND_EXPR_VARS (*tp),
45025 BIND_EXPR_VARS (sit->repl));
45026 BIND_EXPR_BODY (*tp) = BIND_EXPR_BODY (sit->repl);
45027 return *tp;
45029 else if (TREE_CODE (BIND_EXPR_BODY (*tp)) == STATEMENT_LIST)
45030 /* There might be a statement list containing cleanup_points
45031 etc between the two levels of BIND_EXPR. We can still merge
45032 them, again keeping the outer BIND_EXPR. */
45033 for (tree_stmt_iterator i = tsi_start (BIND_EXPR_BODY (*tp));
45034 !tsi_end_p (i); ++i)
45036 tree *p = tsi_stmt_ptr (i);
45037 if (*p == sit->orig)
45039 BIND_EXPR_VARS (*tp) = chainon (BIND_EXPR_VARS (*tp),
45040 BIND_EXPR_VARS (sit->repl));
45041 *p = BIND_EXPR_BODY (sit->repl);
45042 return *tp;
45046 return NULL;
45049 static void
45050 substitute_in_tree (tree *context, tree orig, tree repl, bool flatten)
45052 struct sit_data data;
45054 gcc_assert (*context && orig && repl);
45055 if (TREE_CODE (repl) == BIND_EXPR && !BIND_EXPR_VARS (repl))
45056 repl = BIND_EXPR_BODY (repl);
45057 data.orig = orig;
45058 data.repl = repl;
45059 data.flatten = flatten;
45061 tree result = cp_walk_tree (context, substitute_in_tree_walker,
45062 (void *)&data, NULL);
45063 gcc_assert (result != NULL_TREE);
45066 /* Walker to patch up the BLOCK_NODE hierarchy after the above surgery.
45067 *DP is is the parent block. */
45069 static tree
45070 fixup_blocks_walker (tree *tp, int *walk_subtrees, void *dp)
45072 tree superblock = *(tree *)dp;
45074 /* BIND_EXPR_BLOCK may be null if the expression is not a
45075 full-expression; if there's no block, no patching is necessary
45076 for this node. */
45077 if (TREE_CODE (*tp) == BIND_EXPR && BIND_EXPR_BLOCK (*tp))
45079 tree block = BIND_EXPR_BLOCK (*tp);
45080 if (superblock)
45082 BLOCK_SUPERCONTEXT (block) = superblock;
45083 BLOCK_CHAIN (block) = BLOCK_SUBBLOCKS (superblock);
45084 BLOCK_SUBBLOCKS (superblock) = block;
45086 BLOCK_SUBBLOCKS (block) = NULL_TREE;
45087 cp_walk_tree (&BIND_EXPR_BODY (*tp), fixup_blocks_walker,
45088 (void *)&block, NULL);
45089 *walk_subtrees = 0;
45092 return NULL;
45095 /* Parse the restricted form of the for statement allowed by OpenMP. */
45097 static tree
45098 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
45099 tree *cclauses, bool *if_p)
45101 tree ret;
45102 tree cl, ordered_cl = NULL_TREE;
45103 int collapse = 1, ordered = 0;
45104 unsigned int count;
45105 bool tiling = false;
45106 bool inscan = false;
45107 struct omp_for_parse_data data;
45108 struct omp_for_parse_data *save_data = parser->omp_for_parse_state;
45109 tree result;
45110 location_t loc_first = cp_lexer_peek_token (parser->lexer)->location;
45112 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
45113 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
45114 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
45115 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
45117 tiling = true;
45118 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
45120 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
45121 && OMP_CLAUSE_ORDERED_EXPR (cl))
45123 ordered_cl = cl;
45124 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
45126 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_REDUCTION
45127 && OMP_CLAUSE_REDUCTION_INSCAN (cl)
45128 && (code == OMP_SIMD || code == OMP_FOR))
45129 inscan = true;
45131 if (ordered && ordered < collapse)
45133 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
45134 "%<ordered%> clause parameter is less than %<collapse%>");
45135 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
45136 = build_int_cst (NULL_TREE, collapse);
45137 ordered = collapse;
45140 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
45141 count = ordered ? ordered : collapse;
45143 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
45145 cp_parser_error (parser, "for statement expected");
45146 return NULL;
45149 /* Initialize parse state for recursive descent. */
45150 data.declv = make_tree_vec (count);
45151 data.initv = make_tree_vec (count);
45152 data.condv = make_tree_vec (count);
45153 data.incrv = make_tree_vec (count);
45154 data.pre_body = NULL_TREE;
45155 data.for_loc = cp_lexer_peek_token (parser->lexer)->location;
45156 data.count = count;
45157 data.depth = 0;
45158 data.want_nested_loop = true;
45159 data.ordered = ordered > 0;
45160 data.in_intervening_code = false;
45161 data.perfect_nesting_fail = false;
45162 data.fail = false;
45163 data.inscan = inscan;
45164 data.saw_intervening_code = false;
45165 data.code = code;
45166 data.orig_declv = NULL_TREE;
45167 data.clauses = clauses;
45168 data.cclauses = cclauses;
45169 data.ordered_cl = ordered_cl;
45170 parser->omp_for_parse_state = &data;
45172 cp_parser_omp_loop_nest (parser, if_p);
45174 /* Bomb out early if there was an error (not enough loops, etc). */
45175 if (data.fail || data.declv == NULL_TREE)
45177 parser->omp_for_parse_state = save_data;
45178 return NULL_TREE;
45181 /* Relink the init and body blocks that were built during parsing. At
45182 this point we have a structure nested like
45183 init 0
45184 body 0
45185 init 1
45186 body 1
45187 init 2
45188 body 2
45189 and we want to turn it into
45190 init 0
45191 init 1
45192 init 2
45193 omp_for
45194 body 0
45195 body 1
45196 body 2
45197 We also need to flatten the init blocks, as some code for later
45198 processing of combined directives gets confused otherwise. */
45200 gcc_assert (vec_safe_length (data.init_blockv) == count);
45201 gcc_assert (vec_safe_length (data.body_blockv) == count);
45202 gcc_assert (vec_safe_length (data.init_placeholderv) == count);
45203 gcc_assert (vec_safe_length (data.body_placeholderv) == count);
45205 /* First insert markers for structured blocks for intervening code in
45206 the loop bodies. */
45207 for (unsigned int i = 0; i < count - 1; i++)
45209 bool good = find_structured_blocks (&(data.body_blockv[i]),
45210 data.init_placeholderv[i+1]);
45211 gcc_assert (good);
45214 /* Do the substitution from the inside out. */
45215 for (unsigned int i = count - 1; i > 0; i--)
45217 substitute_in_tree (&(data.body_blockv[i-1]),
45218 data.init_placeholderv[i],
45219 data.body_blockv[i], false);
45220 substitute_in_tree (&(data.init_blockv[i-1]),
45221 data.body_placeholderv[i-1],
45222 data.init_blockv[i], true);
45225 /* Generate the OMP_FOR. Note finish_omp_for adds the OMP_FOR
45226 (and possibly other stuff) to the current statement list but
45227 returns a pointer to the OMP_FOR itself, or null in case of error. */
45228 result = push_stmt_list ();
45229 ret = finish_omp_for (loc_first, code, data.declv, data.orig_declv,
45230 data.initv, data.condv, data.incrv,
45231 data.body_blockv[0],
45232 data.pre_body, &data.orig_inits, data.clauses);
45233 result = pop_stmt_list (result);
45235 /* Check for errors involving lb/ub/incr expressions referencing
45236 variables declared in intervening code. */
45237 if (data.saw_intervening_code
45238 && !c_omp_check_loop_binding_exprs (ret, &data.orig_inits))
45239 ret = NULL_TREE;
45241 if (ret)
45243 /* Splice the omp_for into the nest of init blocks. */
45244 substitute_in_tree (&(data.init_blockv[0]),
45245 data.body_placeholderv[count - 1],
45246 result, true);
45248 /* Some later processing for combined directives assumes
45249 that the BIND_EXPR containing range for variables appears
45250 at top level in the OMP_FOR body. Fix that up if it's
45251 not the case, e.g. because there is intervening code. */
45252 if (code != OACC_LOOP)
45253 finish_omp_for_block (data.init_blockv[0], ret);
45255 /* Clean up the block subblock/superblock links. Per comment in
45256 begin_compound_stmt, "we don't build BLOCK nodes when processing
45257 templates", so skip this step in that case. */
45258 if (!processing_template_decl)
45260 tree superblock = NULL_TREE;
45261 cp_walk_tree (&data.init_blockv[0], fixup_blocks_walker,
45262 (void *)&superblock, NULL);
45265 /* Finally record the result. */
45266 add_stmt (data.init_blockv[0]);
45269 parser->omp_for_parse_state = save_data;
45270 return ret;
45273 /* Helper function for OpenMP parsing, split clauses and call
45274 finish_omp_clauses on each of the set of clauses afterwards. */
45276 static void
45277 cp_omp_split_clauses (location_t loc, enum tree_code code,
45278 omp_clause_mask mask, tree clauses, tree *cclauses)
45280 int i;
45281 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
45282 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
45283 if (cclauses[i])
45284 cclauses[i] = finish_omp_clauses (cclauses[i],
45285 i == C_OMP_CLAUSE_SPLIT_TARGET
45286 ? C_ORT_OMP_TARGET : C_ORT_OMP);
45289 /* OpenMP 5.0:
45290 #pragma omp loop loop-clause[optseq] new-line
45291 for-loop */
45293 #define OMP_LOOP_CLAUSE_MASK \
45294 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
45295 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
45296 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
45297 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
45298 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_BIND) \
45299 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
45301 static tree
45302 cp_parser_omp_loop (cp_parser *parser, cp_token *pragma_tok,
45303 char *p_name, omp_clause_mask mask, tree *cclauses,
45304 bool *if_p)
45306 tree clauses, sb, ret;
45307 unsigned int save;
45308 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
45310 strcat (p_name, " loop");
45311 mask |= OMP_LOOP_CLAUSE_MASK;
45313 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
45314 cclauses == NULL);
45315 if (cclauses)
45317 cp_omp_split_clauses (loc, OMP_LOOP, mask, clauses, cclauses);
45318 clauses = cclauses[C_OMP_CLAUSE_SPLIT_LOOP];
45321 keep_next_level (true);
45322 sb = begin_omp_structured_block ();
45323 save = cp_parser_begin_omp_structured_block (parser);
45325 ret = cp_parser_omp_for_loop (parser, OMP_LOOP, clauses, cclauses, if_p);
45327 cp_parser_end_omp_structured_block (parser, save);
45328 add_stmt (finish_omp_structured_block (sb));
45330 return ret;
45333 /* OpenMP 4.0:
45334 #pragma omp simd simd-clause[optseq] new-line
45335 for-loop */
45337 #define OMP_SIMD_CLAUSE_MASK \
45338 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
45339 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
45340 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
45341 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
45342 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
45343 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
45344 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
45345 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
45346 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
45347 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL) \
45348 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
45350 static tree
45351 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
45352 char *p_name, omp_clause_mask mask, tree *cclauses,
45353 bool *if_p)
45355 tree clauses, sb, ret;
45356 unsigned int save;
45357 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
45359 strcat (p_name, " simd");
45360 mask |= OMP_SIMD_CLAUSE_MASK;
45362 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
45363 cclauses == NULL);
45364 if (cclauses)
45366 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
45367 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
45370 keep_next_level (true);
45371 sb = begin_omp_structured_block ();
45372 save = cp_parser_begin_omp_structured_block (parser);
45374 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
45376 cp_parser_end_omp_structured_block (parser, save);
45377 add_stmt (finish_omp_structured_block (sb));
45379 return ret;
45382 /* OpenMP 2.5:
45383 #pragma omp for for-clause[optseq] new-line
45384 for-loop
45386 OpenMP 4.0:
45387 #pragma omp for simd for-simd-clause[optseq] new-line
45388 for-loop */
45390 #define OMP_FOR_CLAUSE_MASK \
45391 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
45392 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
45393 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
45394 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
45395 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
45396 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
45397 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
45398 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
45399 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
45400 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
45401 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
45403 static tree
45404 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
45405 char *p_name, omp_clause_mask mask, tree *cclauses,
45406 bool *if_p)
45408 tree clauses, sb, ret;
45409 unsigned int save;
45410 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
45412 strcat (p_name, " for");
45413 mask |= OMP_FOR_CLAUSE_MASK;
45414 /* parallel for{, simd} disallows nowait clause, but for
45415 target {teams distribute ,}parallel for{, simd} it should be accepted. */
45416 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
45417 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
45418 /* Composite distribute parallel for{, simd} disallows ordered clause. */
45419 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
45420 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
45422 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45424 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
45425 const char *p = IDENTIFIER_POINTER (id);
45427 if (strcmp (p, "simd") == 0)
45429 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
45430 if (cclauses == NULL)
45431 cclauses = cclauses_buf;
45433 cp_lexer_consume_token (parser->lexer);
45434 if (!flag_openmp) /* flag_openmp_simd */
45435 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
45436 cclauses, if_p);
45437 sb = begin_omp_structured_block ();
45438 save = cp_parser_begin_omp_structured_block (parser);
45439 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
45440 cclauses, if_p);
45441 cp_parser_end_omp_structured_block (parser, save);
45442 tree body = finish_omp_structured_block (sb);
45443 if (ret == NULL)
45444 return ret;
45445 ret = make_node (OMP_FOR);
45446 TREE_TYPE (ret) = void_type_node;
45447 OMP_FOR_BODY (ret) = body;
45448 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
45449 SET_EXPR_LOCATION (ret, loc);
45450 add_stmt (ret);
45451 return ret;
45454 if (!flag_openmp) /* flag_openmp_simd */
45456 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45457 return NULL_TREE;
45460 /* Composite distribute parallel for disallows linear clause. */
45461 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
45462 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
45464 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
45465 cclauses == NULL);
45466 if (cclauses)
45468 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
45469 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
45472 keep_next_level (true);
45473 sb = begin_omp_structured_block ();
45474 save = cp_parser_begin_omp_structured_block (parser);
45476 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
45478 cp_parser_end_omp_structured_block (parser, save);
45479 add_stmt (finish_omp_structured_block (sb));
45481 return ret;
45484 static tree cp_parser_omp_taskloop (cp_parser *, cp_token *, char *,
45485 omp_clause_mask, tree *, bool *);
45487 /* OpenMP 2.5:
45488 # pragma omp master new-line
45489 structured-block */
45491 static tree
45492 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok,
45493 char *p_name, omp_clause_mask mask, tree *cclauses,
45494 bool *if_p)
45496 tree clauses, sb, ret;
45497 unsigned int save;
45498 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
45500 strcat (p_name, " master");
45502 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45504 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
45505 const char *p = IDENTIFIER_POINTER (id);
45507 if (strcmp (p, "taskloop") == 0)
45509 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
45510 if (cclauses == NULL)
45511 cclauses = cclauses_buf;
45513 cp_lexer_consume_token (parser->lexer);
45514 if (!flag_openmp) /* flag_openmp_simd */
45515 return cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
45516 cclauses, if_p);
45517 sb = begin_omp_structured_block ();
45518 save = cp_parser_begin_omp_structured_block (parser);
45519 ret = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
45520 cclauses, if_p);
45521 cp_parser_end_omp_structured_block (parser, save);
45522 tree body = finish_omp_structured_block (sb);
45523 if (ret == NULL)
45524 return ret;
45525 ret = c_finish_omp_master (loc, body);
45526 OMP_MASTER_COMBINED (ret) = 1;
45527 return ret;
45530 if (!flag_openmp) /* flag_openmp_simd */
45532 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45533 return NULL_TREE;
45536 if (cclauses)
45538 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
45539 false);
45540 cp_omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
45542 else
45543 cp_parser_require_pragma_eol (parser, pragma_tok);
45545 return c_finish_omp_master (loc,
45546 cp_parser_omp_structured_block (parser, if_p));
45549 /* OpenMP 5.1:
45550 # pragma omp masked masked-clauses new-line
45551 structured-block */
45553 #define OMP_MASKED_CLAUSE_MASK \
45554 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FILTER)
45556 static tree
45557 cp_parser_omp_masked (cp_parser *parser, cp_token *pragma_tok,
45558 char *p_name, omp_clause_mask mask, tree *cclauses,
45559 bool *if_p)
45561 tree clauses, sb, ret;
45562 unsigned int save;
45563 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
45565 strcat (p_name, " masked");
45566 mask |= OMP_MASKED_CLAUSE_MASK;
45568 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45570 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
45571 const char *p = IDENTIFIER_POINTER (id);
45573 if (strcmp (p, "taskloop") == 0)
45575 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
45576 if (cclauses == NULL)
45577 cclauses = cclauses_buf;
45579 cp_lexer_consume_token (parser->lexer);
45580 if (!flag_openmp) /* flag_openmp_simd */
45581 return cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
45582 cclauses, if_p);
45583 sb = begin_omp_structured_block ();
45584 save = cp_parser_begin_omp_structured_block (parser);
45585 ret = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
45586 cclauses, if_p);
45587 cp_parser_end_omp_structured_block (parser, save);
45588 tree body = finish_omp_structured_block (sb);
45589 if (ret == NULL)
45590 return ret;
45591 ret = c_finish_omp_masked (loc, body,
45592 cclauses[C_OMP_CLAUSE_SPLIT_MASKED]);
45593 OMP_MASKED_COMBINED (ret) = 1;
45594 return ret;
45597 if (!flag_openmp) /* flag_openmp_simd */
45599 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45600 return NULL_TREE;
45603 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
45604 cclauses == NULL);
45605 if (cclauses)
45607 cp_omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
45608 clauses = cclauses[C_OMP_CLAUSE_SPLIT_MASKED];
45611 return c_finish_omp_masked (loc,
45612 cp_parser_omp_structured_block (parser, if_p),
45613 clauses);
45616 /* OpenMP 2.5:
45617 # pragma omp ordered new-line
45618 structured-block
45620 OpenMP 4.5:
45621 # pragma omp ordered ordered-clauses new-line
45622 structured-block */
45624 #define OMP_ORDERED_CLAUSE_MASK \
45625 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
45626 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
45628 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
45629 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
45630 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DOACROSS))
45632 static bool
45633 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
45634 enum pragma_context context, bool *if_p)
45636 location_t loc = pragma_tok->location;
45637 int n = 1;
45639 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
45640 n = 2;
45642 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_NAME))
45644 tree id = cp_lexer_peek_nth_token (parser->lexer, n)->u.value;
45645 const char *p = IDENTIFIER_POINTER (id);
45647 if (strcmp (p, "depend") == 0 || strcmp (p, "doacross") == 0)
45649 if (!flag_openmp) /* flag_openmp_simd */
45651 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45652 return false;
45654 if (context == pragma_stmt)
45656 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
45657 "%qs clause may only be used in compound "
45658 "statements", p);
45659 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45660 return true;
45662 tree clauses
45663 = cp_parser_omp_all_clauses (parser,
45664 OMP_ORDERED_DEPEND_CLAUSE_MASK,
45665 "#pragma omp ordered", pragma_tok);
45666 c_finish_omp_ordered (loc, clauses, NULL_TREE);
45667 return false;
45671 tree clauses
45672 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
45673 "#pragma omp ordered", pragma_tok);
45675 if (!flag_openmp /* flag_openmp_simd */
45676 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
45677 return false;
45679 c_finish_omp_ordered (loc, clauses,
45680 cp_parser_omp_structured_block (parser, if_p));
45681 return true;
45684 /* OpenMP 2.5:
45686 section-scope:
45687 { section-sequence }
45689 section-sequence:
45690 section-directive[opt] structured-block
45691 section-sequence section-directive structured-block */
45693 static tree
45694 cp_parser_omp_sections_scope (cp_parser *parser)
45696 tree stmt, substmt;
45697 bool error_suppress = false;
45698 cp_token *tok;
45700 matching_braces braces;
45701 if (!braces.require_open (parser))
45702 return NULL_TREE;
45704 stmt = push_stmt_list ();
45706 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
45707 != PRAGMA_OMP_SECTION
45708 && !cp_parser_omp_section_scan (parser, "section", true))
45710 substmt = cp_parser_omp_structured_block_sequence (parser,
45711 PRAGMA_OMP_SECTION);
45712 substmt = build1 (OMP_SECTION, void_type_node, substmt);
45713 add_stmt (substmt);
45716 while (1)
45718 tok = cp_lexer_peek_token (parser->lexer);
45719 if (tok->type == CPP_CLOSE_BRACE)
45720 break;
45721 if (tok->type == CPP_EOF)
45722 break;
45724 if (cp_parser_omp_section_scan (parser, "section", false))
45725 tok = cp_lexer_peek_token (parser->lexer);
45726 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
45728 cp_lexer_consume_token (parser->lexer);
45729 cp_parser_require_pragma_eol (parser, tok);
45730 error_suppress = false;
45732 else if (!error_suppress)
45734 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
45735 error_suppress = true;
45738 substmt = cp_parser_omp_structured_block_sequence (parser,
45739 PRAGMA_OMP_SECTION);
45740 substmt = build1 (OMP_SECTION, void_type_node, substmt);
45741 add_stmt (substmt);
45743 braces.require_close (parser);
45745 substmt = pop_stmt_list (stmt);
45747 stmt = make_node (OMP_SECTIONS);
45748 TREE_TYPE (stmt) = void_type_node;
45749 OMP_SECTIONS_BODY (stmt) = substmt;
45751 add_stmt (stmt);
45752 return stmt;
45755 /* OpenMP 2.5:
45756 # pragma omp sections sections-clause[optseq] newline
45757 sections-scope */
45759 #define OMP_SECTIONS_CLAUSE_MASK \
45760 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
45761 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
45762 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
45763 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
45764 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
45765 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
45767 static tree
45768 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
45769 char *p_name, omp_clause_mask mask, tree *cclauses)
45771 tree clauses, ret;
45772 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
45774 strcat (p_name, " sections");
45775 mask |= OMP_SECTIONS_CLAUSE_MASK;
45776 if (cclauses)
45777 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
45779 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
45780 cclauses == NULL);
45781 if (cclauses)
45783 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
45784 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
45787 ret = cp_parser_omp_sections_scope (parser);
45788 if (ret)
45789 OMP_SECTIONS_CLAUSES (ret) = clauses;
45791 return ret;
45794 /* OpenMP 2.5:
45795 # pragma omp parallel parallel-clause[optseq] new-line
45796 structured-block
45797 # pragma omp parallel for parallel-for-clause[optseq] new-line
45798 structured-block
45799 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
45800 structured-block
45802 OpenMP 4.0:
45803 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
45804 structured-block */
45806 #define OMP_PARALLEL_CLAUSE_MASK \
45807 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
45808 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
45809 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
45810 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
45811 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
45812 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
45813 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
45814 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
45815 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
45816 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
45818 static tree
45819 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
45820 char *p_name, omp_clause_mask mask, tree *cclauses,
45821 bool *if_p)
45823 tree stmt, clauses, block;
45824 unsigned int save;
45825 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
45827 strcat (p_name, " parallel");
45828 mask |= OMP_PARALLEL_CLAUSE_MASK;
45829 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
45830 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
45831 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
45832 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
45834 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
45836 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
45837 if (cclauses == NULL)
45838 cclauses = cclauses_buf;
45840 cp_lexer_consume_token (parser->lexer);
45841 if (!flag_openmp) /* flag_openmp_simd */
45842 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
45843 if_p);
45844 block = begin_omp_parallel ();
45845 save = cp_parser_begin_omp_structured_block (parser);
45846 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
45847 if_p);
45848 cp_parser_end_omp_structured_block (parser, save);
45849 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
45850 block);
45851 if (ret == NULL_TREE)
45852 return ret;
45853 OMP_PARALLEL_COMBINED (stmt) = 1;
45854 return stmt;
45856 /* When combined with distribute, parallel has to be followed by for.
45857 #pragma omp target parallel is allowed though. */
45858 else if (cclauses
45859 && (mask & (OMP_CLAUSE_MASK_1
45860 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
45862 error_at (loc, "expected %<for%> after %qs", p_name);
45863 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45864 return NULL_TREE;
45866 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45868 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
45869 const char *p = IDENTIFIER_POINTER (id);
45870 if (cclauses == NULL && strcmp (p, "masked") == 0)
45872 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
45873 cclauses = cclauses_buf;
45875 cp_lexer_consume_token (parser->lexer);
45876 if (!flag_openmp) /* flag_openmp_simd */
45877 return cp_parser_omp_masked (parser, pragma_tok, p_name, mask,
45878 cclauses, if_p);
45879 block = begin_omp_parallel ();
45880 save = cp_parser_begin_omp_structured_block (parser);
45881 tree ret = cp_parser_omp_masked (parser, pragma_tok, p_name, mask,
45882 cclauses, if_p);
45883 cp_parser_end_omp_structured_block (parser, save);
45884 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
45885 block);
45886 if (ret == NULL_TREE)
45887 return ret;
45888 /* masked does have just filter clause, but during gimplification
45889 isn't represented by a gimplification omp context, so for
45890 #pragma omp parallel masked don't set OMP_PARALLEL_COMBINED,
45891 so that
45892 #pragma omp parallel masked
45893 #pragma omp taskloop simd lastprivate (x)
45894 isn't confused with
45895 #pragma omp parallel masked taskloop simd lastprivate (x) */
45896 if (OMP_MASKED_COMBINED (ret))
45897 OMP_PARALLEL_COMBINED (stmt) = 1;
45898 return stmt;
45900 else if (cclauses == NULL && strcmp (p, "master") == 0)
45902 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
45903 cclauses = cclauses_buf;
45905 cp_lexer_consume_token (parser->lexer);
45906 if (!flag_openmp) /* flag_openmp_simd */
45907 return cp_parser_omp_master (parser, pragma_tok, p_name, mask,
45908 cclauses, if_p);
45909 block = begin_omp_parallel ();
45910 save = cp_parser_begin_omp_structured_block (parser);
45911 tree ret = cp_parser_omp_master (parser, pragma_tok, p_name, mask,
45912 cclauses, if_p);
45913 cp_parser_end_omp_structured_block (parser, save);
45914 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
45915 block);
45916 if (ret == NULL_TREE)
45917 return ret;
45918 /* master doesn't have any clauses and during gimplification
45919 isn't represented by a gimplification omp context, so for
45920 #pragma omp parallel master don't set OMP_PARALLEL_COMBINED,
45921 so that
45922 #pragma omp parallel master
45923 #pragma omp taskloop simd lastprivate (x)
45924 isn't confused with
45925 #pragma omp parallel master taskloop simd lastprivate (x) */
45926 if (OMP_MASTER_COMBINED (ret))
45927 OMP_PARALLEL_COMBINED (stmt) = 1;
45928 return stmt;
45930 else if (strcmp (p, "loop") == 0)
45932 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
45933 if (cclauses == NULL)
45934 cclauses = cclauses_buf;
45936 cp_lexer_consume_token (parser->lexer);
45937 if (!flag_openmp) /* flag_openmp_simd */
45938 return cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
45939 cclauses, if_p);
45940 block = begin_omp_parallel ();
45941 save = cp_parser_begin_omp_structured_block (parser);
45942 tree ret = cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
45943 cclauses, if_p);
45944 cp_parser_end_omp_structured_block (parser, save);
45945 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
45946 block);
45947 if (ret == NULL_TREE)
45948 return ret;
45949 OMP_PARALLEL_COMBINED (stmt) = 1;
45950 return stmt;
45952 else if (!flag_openmp) /* flag_openmp_simd */
45954 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45955 return NULL_TREE;
45957 else if (cclauses == NULL && strcmp (p, "sections") == 0)
45959 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
45960 cclauses = cclauses_buf;
45962 cp_lexer_consume_token (parser->lexer);
45963 block = begin_omp_parallel ();
45964 save = cp_parser_begin_omp_structured_block (parser);
45965 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
45966 cp_parser_end_omp_structured_block (parser, save);
45967 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
45968 block);
45969 OMP_PARALLEL_COMBINED (stmt) = 1;
45970 return stmt;
45973 else if (!flag_openmp) /* flag_openmp_simd */
45975 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45976 return NULL_TREE;
45979 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
45980 cclauses == NULL);
45981 if (cclauses)
45983 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
45984 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
45987 block = begin_omp_parallel ();
45988 save = cp_parser_begin_omp_structured_block (parser);
45989 parser->omp_attrs_forbidden_p = true;
45990 cp_parser_statement (parser, NULL_TREE, false, if_p);
45991 cp_parser_end_omp_structured_block (parser, save);
45992 stmt = finish_omp_parallel (clauses, block);
45993 return stmt;
45996 /* OpenMP 2.5:
45997 # pragma omp single single-clause[optseq] new-line
45998 structured-block */
46000 #define OMP_SINGLE_CLAUSE_MASK \
46001 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
46002 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
46003 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
46004 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
46005 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
46007 static tree
46008 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
46010 tree stmt = make_node (OMP_SINGLE);
46011 TREE_TYPE (stmt) = void_type_node;
46012 SET_EXPR_LOCATION (stmt, pragma_tok->location);
46014 OMP_SINGLE_CLAUSES (stmt)
46015 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
46016 "#pragma omp single", pragma_tok);
46017 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
46019 return add_stmt (stmt);
46022 /* OpenMP 5.1:
46023 # pragma omp scope scope-clause[optseq] new-line
46024 structured-block */
46026 #define OMP_SCOPE_CLAUSE_MASK \
46027 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
46028 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
46029 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
46030 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
46031 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
46033 static tree
46034 cp_parser_omp_scope (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
46036 tree stmt = make_node (OMP_SCOPE);
46037 TREE_TYPE (stmt) = void_type_node;
46038 SET_EXPR_LOCATION (stmt, pragma_tok->location);
46040 OMP_SCOPE_CLAUSES (stmt)
46041 = cp_parser_omp_all_clauses (parser, OMP_SCOPE_CLAUSE_MASK,
46042 "#pragma omp scope", pragma_tok);
46043 OMP_SCOPE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
46045 return add_stmt (stmt);
46048 /* OpenMP 3.0:
46049 # pragma omp task task-clause[optseq] new-line
46050 structured-block */
46052 #define OMP_TASK_CLAUSE_MASK \
46053 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
46054 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
46055 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
46056 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
46057 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
46058 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
46059 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
46060 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
46061 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
46062 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
46063 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
46064 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION) \
46065 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DETACH) \
46066 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_AFFINITY))
46068 static tree
46069 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
46071 tree clauses, block;
46072 unsigned int save;
46074 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
46075 "#pragma omp task", pragma_tok);
46076 block = begin_omp_task ();
46077 save = cp_parser_begin_omp_structured_block (parser);
46078 parser->omp_attrs_forbidden_p = true;
46079 cp_parser_statement (parser, NULL_TREE, false, if_p);
46080 cp_parser_end_omp_structured_block (parser, save);
46081 return finish_omp_task (clauses, block);
46084 /* OpenMP 3.0:
46085 # pragma omp taskwait new-line
46087 OpenMP 5.0:
46088 # pragma omp taskwait taskwait-clause[opt] new-line */
46090 #define OMP_TASKWAIT_CLAUSE_MASK \
46091 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
46092 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
46094 static void
46095 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
46097 tree clauses
46098 = cp_parser_omp_all_clauses (parser, OMP_TASKWAIT_CLAUSE_MASK,
46099 "#pragma omp taskwait", pragma_tok);
46101 if (clauses)
46103 tree stmt = make_node (OMP_TASK);
46104 TREE_TYPE (stmt) = void_node;
46105 OMP_TASK_CLAUSES (stmt) = clauses;
46106 OMP_TASK_BODY (stmt) = NULL_TREE;
46107 SET_EXPR_LOCATION (stmt, pragma_tok->location);
46108 add_stmt (stmt);
46110 else
46111 finish_omp_taskwait ();
46114 /* OpenMP 3.1:
46115 # pragma omp taskyield new-line */
46117 static void
46118 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
46120 cp_parser_require_pragma_eol (parser, pragma_tok);
46121 finish_omp_taskyield ();
46124 /* OpenMP 4.0:
46125 # pragma omp taskgroup new-line
46126 structured-block
46128 OpenMP 5.0:
46129 # pragma omp taskgroup taskgroup-clause[optseq] new-line */
46131 #define OMP_TASKGROUP_CLAUSE_MASK \
46132 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
46133 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
46135 static tree
46136 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
46138 tree clauses
46139 = cp_parser_omp_all_clauses (parser, OMP_TASKGROUP_CLAUSE_MASK,
46140 "#pragma omp taskgroup", pragma_tok);
46141 return c_finish_omp_taskgroup (input_location,
46142 cp_parser_omp_structured_block (parser,
46143 if_p),
46144 clauses);
46148 /* OpenMP 2.5:
46149 # pragma omp threadprivate (variable-list) */
46151 static void
46152 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
46154 tree vars;
46156 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
46157 cp_parser_require_pragma_eol (parser, pragma_tok);
46159 finish_omp_threadprivate (vars);
46162 /* OpenMP 4.0:
46163 # pragma omp cancel cancel-clause[optseq] new-line */
46165 #define OMP_CANCEL_CLAUSE_MASK \
46166 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
46167 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
46168 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
46169 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
46170 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
46172 static void
46173 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
46175 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
46176 "#pragma omp cancel", pragma_tok);
46177 finish_omp_cancel (clauses);
46180 /* OpenMP 4.0:
46181 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
46183 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
46184 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
46185 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
46186 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
46187 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
46189 static bool
46190 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
46191 enum pragma_context context)
46193 tree clauses;
46194 bool point_seen = false;
46196 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46198 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46199 const char *p = IDENTIFIER_POINTER (id);
46201 if (strcmp (p, "point") == 0)
46203 cp_lexer_consume_token (parser->lexer);
46204 point_seen = true;
46207 if (!point_seen)
46209 cp_parser_error (parser, "expected %<point%>");
46210 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46211 return false;
46214 if (context != pragma_compound)
46216 if (context == pragma_stmt)
46217 error_at (pragma_tok->location,
46218 "%<#pragma %s%> may only be used in compound statements",
46219 "omp cancellation point");
46220 else
46221 cp_parser_error (parser, "expected declaration specifiers");
46222 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46223 return true;
46226 clauses = cp_parser_omp_all_clauses (parser,
46227 OMP_CANCELLATION_POINT_CLAUSE_MASK,
46228 "#pragma omp cancellation point",
46229 pragma_tok);
46230 finish_omp_cancellation_point (clauses);
46231 return true;
46234 /* OpenMP 4.0:
46235 #pragma omp distribute distribute-clause[optseq] new-line
46236 for-loop */
46238 #define OMP_DISTRIBUTE_CLAUSE_MASK \
46239 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
46240 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
46241 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
46242 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
46243 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
46244 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
46245 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
46247 static tree
46248 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
46249 char *p_name, omp_clause_mask mask, tree *cclauses,
46250 bool *if_p)
46252 tree clauses, sb, ret;
46253 unsigned int save;
46254 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
46256 strcat (p_name, " distribute");
46257 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
46259 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46261 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46262 const char *p = IDENTIFIER_POINTER (id);
46263 bool simd = false;
46264 bool parallel = false;
46266 if (strcmp (p, "simd") == 0)
46267 simd = true;
46268 else
46269 parallel = strcmp (p, "parallel") == 0;
46270 if (parallel || simd)
46272 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
46273 if (cclauses == NULL)
46274 cclauses = cclauses_buf;
46275 cp_lexer_consume_token (parser->lexer);
46276 if (!flag_openmp) /* flag_openmp_simd */
46278 if (simd)
46279 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
46280 cclauses, if_p);
46281 else
46282 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
46283 cclauses, if_p);
46285 sb = begin_omp_structured_block ();
46286 save = cp_parser_begin_omp_structured_block (parser);
46287 if (simd)
46288 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
46289 cclauses, if_p);
46290 else
46291 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
46292 cclauses, if_p);
46293 cp_parser_end_omp_structured_block (parser, save);
46294 tree body = finish_omp_structured_block (sb);
46295 if (ret == NULL)
46296 return ret;
46297 ret = make_node (OMP_DISTRIBUTE);
46298 TREE_TYPE (ret) = void_type_node;
46299 OMP_FOR_BODY (ret) = body;
46300 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
46301 SET_EXPR_LOCATION (ret, loc);
46302 add_stmt (ret);
46303 return ret;
46306 if (!flag_openmp) /* flag_openmp_simd */
46308 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46309 return NULL_TREE;
46312 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
46313 cclauses == NULL);
46314 if (cclauses)
46316 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
46317 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
46320 keep_next_level (true);
46321 sb = begin_omp_structured_block ();
46322 save = cp_parser_begin_omp_structured_block (parser);
46324 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
46326 cp_parser_end_omp_structured_block (parser, save);
46327 add_stmt (finish_omp_structured_block (sb));
46329 return ret;
46332 /* OpenMP 4.0:
46333 # pragma omp teams teams-clause[optseq] new-line
46334 structured-block */
46336 #define OMP_TEAMS_CLAUSE_MASK \
46337 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
46338 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
46339 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
46340 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
46341 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
46342 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
46343 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
46344 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
46346 static tree
46347 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
46348 char *p_name, omp_clause_mask mask, tree *cclauses,
46349 bool *if_p)
46351 tree clauses, sb, ret;
46352 unsigned int save;
46353 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
46355 strcat (p_name, " teams");
46356 mask |= OMP_TEAMS_CLAUSE_MASK;
46358 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46360 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46361 const char *p = IDENTIFIER_POINTER (id);
46362 if (strcmp (p, "distribute") == 0)
46364 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
46365 if (cclauses == NULL)
46366 cclauses = cclauses_buf;
46368 cp_lexer_consume_token (parser->lexer);
46369 if (!flag_openmp) /* flag_openmp_simd */
46370 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
46371 cclauses, if_p);
46372 keep_next_level (true);
46373 sb = begin_omp_structured_block ();
46374 save = cp_parser_begin_omp_structured_block (parser);
46375 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
46376 cclauses, if_p);
46377 cp_parser_end_omp_structured_block (parser, save);
46378 tree body = finish_omp_structured_block (sb);
46379 if (ret == NULL)
46380 return ret;
46381 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
46382 ret = make_node (OMP_TEAMS);
46383 TREE_TYPE (ret) = void_type_node;
46384 OMP_TEAMS_CLAUSES (ret) = clauses;
46385 OMP_TEAMS_BODY (ret) = body;
46386 OMP_TEAMS_COMBINED (ret) = 1;
46387 SET_EXPR_LOCATION (ret, loc);
46388 return add_stmt (ret);
46390 else if (strcmp (p, "loop") == 0)
46392 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
46393 if (cclauses == NULL)
46394 cclauses = cclauses_buf;
46396 cp_lexer_consume_token (parser->lexer);
46397 if (!flag_openmp) /* flag_openmp_simd */
46398 return cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
46399 cclauses, if_p);
46400 keep_next_level (true);
46401 sb = begin_omp_structured_block ();
46402 save = cp_parser_begin_omp_structured_block (parser);
46403 ret = cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
46404 cclauses, if_p);
46405 cp_parser_end_omp_structured_block (parser, save);
46406 tree body = finish_omp_structured_block (sb);
46407 if (ret == NULL)
46408 return ret;
46409 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
46410 ret = make_node (OMP_TEAMS);
46411 TREE_TYPE (ret) = void_type_node;
46412 OMP_TEAMS_CLAUSES (ret) = clauses;
46413 OMP_TEAMS_BODY (ret) = body;
46414 OMP_TEAMS_COMBINED (ret) = 1;
46415 SET_EXPR_LOCATION (ret, loc);
46416 return add_stmt (ret);
46419 if (!flag_openmp) /* flag_openmp_simd */
46421 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46422 return NULL_TREE;
46425 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
46426 cclauses == NULL);
46427 if (cclauses)
46429 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
46430 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
46433 tree stmt = make_node (OMP_TEAMS);
46434 TREE_TYPE (stmt) = void_type_node;
46435 OMP_TEAMS_CLAUSES (stmt) = clauses;
46436 keep_next_level (true);
46437 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
46438 SET_EXPR_LOCATION (stmt, loc);
46440 return add_stmt (stmt);
46443 /* OpenMP 4.0:
46444 # pragma omp target data target-data-clause[optseq] new-line
46445 structured-block */
46447 #define OMP_TARGET_DATA_CLAUSE_MASK \
46448 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
46449 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
46450 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
46451 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR) \
46452 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR))
46454 static tree
46455 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
46457 if (flag_openmp)
46458 omp_requires_mask
46459 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
46461 tree clauses
46462 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
46463 "#pragma omp target data", pragma_tok);
46464 c_omp_adjust_map_clauses (clauses, false);
46465 int map_seen = 0;
46466 for (tree *pc = &clauses; *pc;)
46468 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
46469 switch (OMP_CLAUSE_MAP_KIND (*pc))
46471 case GOMP_MAP_TO:
46472 case GOMP_MAP_ALWAYS_TO:
46473 case GOMP_MAP_PRESENT_TO:
46474 case GOMP_MAP_ALWAYS_PRESENT_TO:
46475 case GOMP_MAP_FROM:
46476 case GOMP_MAP_ALWAYS_FROM:
46477 case GOMP_MAP_PRESENT_FROM:
46478 case GOMP_MAP_ALWAYS_PRESENT_FROM:
46479 case GOMP_MAP_TOFROM:
46480 case GOMP_MAP_ALWAYS_TOFROM:
46481 case GOMP_MAP_PRESENT_TOFROM:
46482 case GOMP_MAP_ALWAYS_PRESENT_TOFROM:
46483 case GOMP_MAP_ALLOC:
46484 case GOMP_MAP_PRESENT_ALLOC:
46485 map_seen = 3;
46486 break;
46487 case GOMP_MAP_FIRSTPRIVATE_POINTER:
46488 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
46489 case GOMP_MAP_ALWAYS_POINTER:
46490 case GOMP_MAP_ATTACH_DETACH:
46491 case GOMP_MAP_ATTACH:
46492 break;
46493 default:
46494 map_seen |= 1;
46495 error_at (OMP_CLAUSE_LOCATION (*pc),
46496 "%<#pragma omp target data%> with map-type other "
46497 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
46498 "on %<map%> clause");
46499 *pc = OMP_CLAUSE_CHAIN (*pc);
46500 continue;
46502 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_PTR
46503 || OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_ADDR)
46504 map_seen = 3;
46505 pc = &OMP_CLAUSE_CHAIN (*pc);
46508 if (map_seen != 3)
46510 if (map_seen == 0)
46511 error_at (pragma_tok->location,
46512 "%<#pragma omp target data%> must contain at least "
46513 "one %<map%>, %<use_device_ptr%> or %<use_device_addr%> "
46514 "clause");
46515 return NULL_TREE;
46518 tree stmt = make_node (OMP_TARGET_DATA);
46519 TREE_TYPE (stmt) = void_type_node;
46520 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
46522 keep_next_level (true);
46523 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
46525 SET_EXPR_LOCATION (stmt, pragma_tok->location);
46526 return add_stmt (stmt);
46529 /* OpenMP 4.5:
46530 # pragma omp target enter data target-enter-data-clause[optseq] new-line
46531 structured-block */
46533 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
46534 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
46535 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
46536 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
46537 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
46538 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
46540 static bool
46541 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
46542 enum pragma_context context)
46544 bool data_seen = false;
46545 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46547 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46548 const char *p = IDENTIFIER_POINTER (id);
46550 if (strcmp (p, "data") == 0)
46552 cp_lexer_consume_token (parser->lexer);
46553 data_seen = true;
46556 if (!data_seen)
46558 cp_parser_error (parser, "expected %<data%>");
46559 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46560 return false;
46563 if (context == pragma_stmt)
46565 error_at (pragma_tok->location,
46566 "%<#pragma %s%> may only be used in compound statements",
46567 "omp target enter data");
46568 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46569 return true;
46572 if (flag_openmp)
46573 omp_requires_mask
46574 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
46576 tree clauses
46577 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
46578 "#pragma omp target enter data", pragma_tok);
46579 c_omp_adjust_map_clauses (clauses, false);
46580 int map_seen = 0;
46581 for (tree *pc = &clauses; *pc;)
46583 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
46584 switch (OMP_CLAUSE_MAP_KIND (*pc))
46586 case GOMP_MAP_TO:
46587 case GOMP_MAP_ALWAYS_TO:
46588 case GOMP_MAP_PRESENT_TO:
46589 case GOMP_MAP_ALWAYS_PRESENT_TO:
46590 case GOMP_MAP_ALLOC:
46591 case GOMP_MAP_PRESENT_ALLOC:
46592 map_seen = 3;
46593 break;
46594 case GOMP_MAP_TOFROM:
46595 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_TO);
46596 map_seen = 3;
46597 break;
46598 case GOMP_MAP_ALWAYS_TOFROM:
46599 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_ALWAYS_TO);
46600 map_seen = 3;
46601 break;
46602 case GOMP_MAP_PRESENT_TOFROM:
46603 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_PRESENT_TO);
46604 map_seen = 3;
46605 break;
46606 case GOMP_MAP_ALWAYS_PRESENT_TOFROM:
46607 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_ALWAYS_PRESENT_TO);
46608 map_seen = 3;
46609 break;
46610 case GOMP_MAP_FIRSTPRIVATE_POINTER:
46611 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
46612 case GOMP_MAP_ALWAYS_POINTER:
46613 case GOMP_MAP_ATTACH_DETACH:
46614 case GOMP_MAP_ATTACH:
46615 break;
46616 default:
46617 map_seen |= 1;
46618 error_at (OMP_CLAUSE_LOCATION (*pc),
46619 "%<#pragma omp target enter data%> with map-type other "
46620 "than %<to%>, %<tofrom%> or %<alloc%> on %<map%> clause");
46621 *pc = OMP_CLAUSE_CHAIN (*pc);
46622 continue;
46624 pc = &OMP_CLAUSE_CHAIN (*pc);
46627 if (map_seen != 3)
46629 if (map_seen == 0)
46630 error_at (pragma_tok->location,
46631 "%<#pragma omp target enter data%> must contain at least "
46632 "one %<map%> clause");
46633 return true;
46636 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
46637 TREE_TYPE (stmt) = void_type_node;
46638 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
46639 SET_EXPR_LOCATION (stmt, pragma_tok->location);
46640 add_stmt (stmt);
46641 return true;
46644 /* OpenMP 4.5:
46645 # pragma omp target exit data target-enter-data-clause[optseq] new-line
46646 structured-block */
46648 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
46649 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
46650 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
46651 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
46652 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
46653 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
46655 static bool
46656 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
46657 enum pragma_context context)
46659 bool data_seen = false;
46660 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46662 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46663 const char *p = IDENTIFIER_POINTER (id);
46665 if (strcmp (p, "data") == 0)
46667 cp_lexer_consume_token (parser->lexer);
46668 data_seen = true;
46671 if (!data_seen)
46673 cp_parser_error (parser, "expected %<data%>");
46674 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46675 return false;
46678 if (context == pragma_stmt)
46680 error_at (pragma_tok->location,
46681 "%<#pragma %s%> may only be used in compound statements",
46682 "omp target exit data");
46683 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46684 return true;
46687 if (flag_openmp)
46688 omp_requires_mask
46689 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
46691 tree clauses
46692 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
46693 "#pragma omp target exit data", pragma_tok,
46694 false);
46695 clauses = finish_omp_clauses (clauses, C_ORT_OMP_EXIT_DATA);
46696 c_omp_adjust_map_clauses (clauses, false);
46697 int map_seen = 0;
46698 for (tree *pc = &clauses; *pc;)
46700 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
46701 switch (OMP_CLAUSE_MAP_KIND (*pc))
46703 case GOMP_MAP_FROM:
46704 case GOMP_MAP_ALWAYS_FROM:
46705 case GOMP_MAP_PRESENT_FROM:
46706 case GOMP_MAP_ALWAYS_PRESENT_FROM:
46707 case GOMP_MAP_RELEASE:
46708 case GOMP_MAP_DELETE:
46709 map_seen = 3;
46710 break;
46711 case GOMP_MAP_TOFROM:
46712 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_FROM);
46713 map_seen = 3;
46714 break;
46715 case GOMP_MAP_ALWAYS_TOFROM:
46716 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_ALWAYS_FROM);
46717 map_seen = 3;
46718 break;
46719 case GOMP_MAP_PRESENT_TOFROM:
46720 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_PRESENT_FROM);
46721 map_seen = 3;
46722 break;
46723 case GOMP_MAP_ALWAYS_PRESENT_TOFROM:
46724 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_ALWAYS_PRESENT_FROM);
46725 map_seen = 3;
46726 break;
46727 case GOMP_MAP_FIRSTPRIVATE_POINTER:
46728 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
46729 case GOMP_MAP_ALWAYS_POINTER:
46730 case GOMP_MAP_ATTACH_DETACH:
46731 case GOMP_MAP_DETACH:
46732 break;
46733 default:
46734 map_seen |= 1;
46735 error_at (OMP_CLAUSE_LOCATION (*pc),
46736 "%<#pragma omp target exit data%> with map-type other "
46737 "than %<from%>, %<tofrom%>, %<release%> or %<delete%> "
46738 "on %<map%> clause");
46739 *pc = OMP_CLAUSE_CHAIN (*pc);
46740 continue;
46742 pc = &OMP_CLAUSE_CHAIN (*pc);
46745 if (map_seen != 3)
46747 if (map_seen == 0)
46748 error_at (pragma_tok->location,
46749 "%<#pragma omp target exit data%> must contain at least "
46750 "one %<map%> clause");
46751 return true;
46754 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
46755 TREE_TYPE (stmt) = void_type_node;
46756 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
46757 SET_EXPR_LOCATION (stmt, pragma_tok->location);
46758 add_stmt (stmt);
46759 return true;
46762 /* OpenMP 4.0:
46763 # pragma omp target update target-update-clause[optseq] new-line */
46765 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
46766 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
46767 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
46768 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
46769 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
46770 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
46771 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
46773 static bool
46774 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
46775 enum pragma_context context)
46777 if (context == pragma_stmt)
46779 error_at (pragma_tok->location,
46780 "%<#pragma %s%> may only be used in compound statements",
46781 "omp target update");
46782 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46783 return true;
46786 tree clauses
46787 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
46788 "#pragma omp target update", pragma_tok);
46789 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
46790 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
46792 error_at (pragma_tok->location,
46793 "%<#pragma omp target update%> must contain at least one "
46794 "%<from%> or %<to%> clauses");
46795 return true;
46798 if (flag_openmp)
46799 omp_requires_mask
46800 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
46802 tree stmt = make_node (OMP_TARGET_UPDATE);
46803 TREE_TYPE (stmt) = void_type_node;
46804 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
46805 SET_EXPR_LOCATION (stmt, pragma_tok->location);
46806 add_stmt (stmt);
46807 return true;
46810 /* OpenMP 4.0:
46811 # pragma omp target target-clause[optseq] new-line
46812 structured-block */
46814 #define OMP_TARGET_CLAUSE_MASK \
46815 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
46816 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
46817 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
46818 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
46819 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
46820 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
46821 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
46822 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
46823 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
46824 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION) \
46825 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
46826 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR)\
46827 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HAS_DEVICE_ADDR))
46829 static bool
46830 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
46831 enum pragma_context context, bool *if_p)
46833 if (flag_openmp)
46834 omp_requires_mask
46835 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
46837 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46839 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46840 const char *p = IDENTIFIER_POINTER (id);
46841 enum tree_code ccode = ERROR_MARK;
46843 if (strcmp (p, "teams") == 0)
46844 ccode = OMP_TEAMS;
46845 else if (strcmp (p, "parallel") == 0)
46846 ccode = OMP_PARALLEL;
46847 else if (strcmp (p, "simd") == 0)
46848 ccode = OMP_SIMD;
46849 if (ccode != ERROR_MARK)
46851 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
46852 char p_name[sizeof ("#pragma omp target teams distribute "
46853 "parallel for simd")];
46855 cp_lexer_consume_token (parser->lexer);
46856 strcpy (p_name, "#pragma omp target");
46857 if (!flag_openmp) /* flag_openmp_simd */
46859 tree stmt;
46860 switch (ccode)
46862 case OMP_TEAMS:
46863 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
46864 OMP_TARGET_CLAUSE_MASK,
46865 cclauses, if_p);
46866 break;
46867 case OMP_PARALLEL:
46868 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
46869 OMP_TARGET_CLAUSE_MASK,
46870 cclauses, if_p);
46871 break;
46872 case OMP_SIMD:
46873 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
46874 OMP_TARGET_CLAUSE_MASK,
46875 cclauses, if_p);
46876 break;
46877 default:
46878 gcc_unreachable ();
46880 return stmt != NULL_TREE;
46882 keep_next_level (true);
46883 tree sb = begin_omp_structured_block (), ret;
46884 unsigned save = cp_parser_begin_omp_structured_block (parser);
46885 switch (ccode)
46887 case OMP_TEAMS:
46888 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
46889 OMP_TARGET_CLAUSE_MASK, cclauses,
46890 if_p);
46891 break;
46892 case OMP_PARALLEL:
46893 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
46894 OMP_TARGET_CLAUSE_MASK, cclauses,
46895 if_p);
46896 break;
46897 case OMP_SIMD:
46898 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
46899 OMP_TARGET_CLAUSE_MASK, cclauses,
46900 if_p);
46901 break;
46902 default:
46903 gcc_unreachable ();
46905 cp_parser_end_omp_structured_block (parser, save);
46906 tree body = finish_omp_structured_block (sb);
46907 if (ret == NULL_TREE)
46908 return false;
46909 if (ccode == OMP_TEAMS && !processing_template_decl)
46910 /* For combined target teams, ensure the num_teams and
46911 thread_limit clause expressions are evaluated on the host,
46912 before entering the target construct. */
46913 for (tree c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
46914 c; c = OMP_CLAUSE_CHAIN (c))
46915 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
46916 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
46917 for (int i = 0;
46918 i <= (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS); ++i)
46919 if (OMP_CLAUSE_OPERAND (c, i)
46920 && TREE_CODE (OMP_CLAUSE_OPERAND (c, i)) != INTEGER_CST)
46922 tree expr = OMP_CLAUSE_OPERAND (c, i);
46923 expr = force_target_expr (TREE_TYPE (expr), expr,
46924 tf_none);
46925 if (expr == error_mark_node)
46926 continue;
46927 tree tmp = TARGET_EXPR_SLOT (expr);
46928 add_stmt (expr);
46929 OMP_CLAUSE_OPERAND (c, i) = expr;
46930 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
46931 OMP_CLAUSE_FIRSTPRIVATE);
46932 OMP_CLAUSE_DECL (tc) = tmp;
46933 OMP_CLAUSE_CHAIN (tc)
46934 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
46935 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
46937 c_omp_adjust_map_clauses (cclauses[C_OMP_CLAUSE_SPLIT_TARGET], true);
46938 finish_omp_target (pragma_tok->location,
46939 cclauses[C_OMP_CLAUSE_SPLIT_TARGET], body, true);
46940 return true;
46942 else if (!flag_openmp) /* flag_openmp_simd */
46944 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46945 return false;
46947 else if (strcmp (p, "data") == 0)
46949 cp_lexer_consume_token (parser->lexer);
46950 cp_parser_omp_target_data (parser, pragma_tok, if_p);
46951 return true;
46953 else if (strcmp (p, "enter") == 0)
46955 cp_lexer_consume_token (parser->lexer);
46956 return cp_parser_omp_target_enter_data (parser, pragma_tok, context);
46958 else if (strcmp (p, "exit") == 0)
46960 cp_lexer_consume_token (parser->lexer);
46961 return cp_parser_omp_target_exit_data (parser, pragma_tok, context);
46963 else if (strcmp (p, "update") == 0)
46965 cp_lexer_consume_token (parser->lexer);
46966 return cp_parser_omp_target_update (parser, pragma_tok, context);
46969 if (!flag_openmp) /* flag_openmp_simd */
46971 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46972 return false;
46975 tree clauses = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
46976 "#pragma omp target", pragma_tok,
46977 false);
46978 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
46979 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
46981 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
46982 OMP_CLAUSE_DECL (nc) = OMP_CLAUSE_DECL (c);
46983 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_ALWAYS_TOFROM);
46984 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (c);
46985 OMP_CLAUSE_CHAIN (c) = nc;
46987 clauses = finish_omp_clauses (clauses, C_ORT_OMP_TARGET);
46989 c_omp_adjust_map_clauses (clauses, true);
46990 keep_next_level (true);
46991 tree body = cp_parser_omp_structured_block (parser, if_p);
46993 finish_omp_target (pragma_tok->location, clauses, body, false);
46994 return true;
46997 /* OpenACC 2.0:
46998 # pragma acc cache (variable-list) new-line
47001 static tree
47002 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
47004 /* Don't create location wrapper nodes within 'OMP_CLAUSE__CACHE_'
47005 clauses. */
47006 auto_suppress_location_wrappers sentinel;
47008 tree stmt, clauses;
47010 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
47011 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
47013 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
47015 stmt = make_node (OACC_CACHE);
47016 TREE_TYPE (stmt) = void_type_node;
47017 OACC_CACHE_CLAUSES (stmt) = clauses;
47018 SET_EXPR_LOCATION (stmt, pragma_tok->location);
47019 add_stmt (stmt);
47021 return stmt;
47024 /* OpenACC 2.0:
47025 # pragma acc data oacc-data-clause[optseq] new-line
47026 structured-block */
47028 #define OACC_DATA_CLAUSE_MASK \
47029 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
47030 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
47031 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
47032 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
47033 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
47034 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
47035 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DETACH) \
47036 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
47037 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
47038 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
47039 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
47041 static tree
47042 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
47044 tree stmt, clauses, block;
47045 unsigned int save;
47047 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
47048 "#pragma acc data", pragma_tok);
47050 block = begin_omp_parallel ();
47051 save = cp_parser_begin_omp_structured_block (parser);
47052 cp_parser_statement (parser, NULL_TREE, false, if_p);
47053 cp_parser_end_omp_structured_block (parser, save);
47054 stmt = finish_oacc_data (clauses, block);
47055 return stmt;
47058 /* OpenACC 2.0:
47059 # pragma acc host_data <clauses> new-line
47060 structured-block */
47062 #define OACC_HOST_DATA_CLAUSE_MASK \
47063 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) \
47064 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
47065 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) )
47067 static tree
47068 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
47070 tree stmt, clauses, block;
47071 unsigned int save;
47073 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
47074 "#pragma acc host_data", pragma_tok,
47075 false);
47076 if (!omp_find_clause (clauses, OMP_CLAUSE_USE_DEVICE_PTR))
47078 error_at (pragma_tok->location,
47079 "%<host_data%> construct requires %<use_device%> clause");
47080 return error_mark_node;
47082 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
47083 block = begin_omp_parallel ();
47084 save = cp_parser_begin_omp_structured_block (parser);
47085 cp_parser_statement (parser, NULL_TREE, false, if_p);
47086 cp_parser_end_omp_structured_block (parser, save);
47087 stmt = finish_oacc_host_data (clauses, block);
47088 return stmt;
47091 /* OpenACC 2.0:
47092 # pragma acc declare oacc-data-clause[optseq] new-line
47095 #define OACC_DECLARE_CLAUSE_MASK \
47096 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
47097 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
47098 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
47099 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
47100 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
47101 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
47102 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
47103 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
47105 static tree
47106 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
47108 tree clauses, stmt;
47109 bool error = false;
47110 bool found_in_scope = global_bindings_p ();
47112 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
47113 "#pragma acc declare", pragma_tok);
47116 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
47118 error_at (pragma_tok->location,
47119 "no valid clauses specified in %<#pragma acc declare%>");
47120 return NULL_TREE;
47123 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
47125 location_t loc = OMP_CLAUSE_LOCATION (t);
47126 tree decl = OMP_CLAUSE_DECL (t);
47127 if (!DECL_P (decl))
47129 error_at (loc, "array section in %<#pragma acc declare%>");
47130 error = true;
47131 continue;
47133 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
47134 switch (OMP_CLAUSE_MAP_KIND (t))
47136 case GOMP_MAP_FIRSTPRIVATE_POINTER:
47137 case GOMP_MAP_ALLOC:
47138 case GOMP_MAP_TO:
47139 case GOMP_MAP_FORCE_DEVICEPTR:
47140 case GOMP_MAP_DEVICE_RESIDENT:
47141 break;
47143 case GOMP_MAP_LINK:
47144 if (!global_bindings_p ()
47145 && (TREE_STATIC (decl)
47146 || !DECL_EXTERNAL (decl)))
47148 error_at (loc,
47149 "%qD must be a global variable in "
47150 "%<#pragma acc declare link%>",
47151 decl);
47152 error = true;
47153 continue;
47155 break;
47157 default:
47158 if (global_bindings_p ())
47160 error_at (loc, "invalid OpenACC clause at file scope");
47161 error = true;
47162 continue;
47164 if (DECL_EXTERNAL (decl))
47166 error_at (loc,
47167 "invalid use of %<extern%> variable %qD "
47168 "in %<#pragma acc declare%>", decl);
47169 error = true;
47170 continue;
47172 else if (TREE_PUBLIC (decl))
47174 error_at (loc,
47175 "invalid use of %<global%> variable %qD "
47176 "in %<#pragma acc declare%>", decl);
47177 error = true;
47178 continue;
47180 break;
47183 if (!found_in_scope)
47184 /* This seems to ignore the existence of cleanup scopes?
47185 What is the meaning for local extern decls? The local
47186 extern is in this scope, but it is referring to a decl that
47187 is namespace scope. */
47188 for (tree d = current_binding_level->names; d; d = TREE_CHAIN (d))
47189 if (d == decl)
47191 found_in_scope = true;
47192 break;
47194 if (!found_in_scope)
47196 error_at (loc,
47197 "%qD must be a variable declared in the same scope as "
47198 "%<#pragma acc declare%>", decl);
47199 error = true;
47200 continue;
47203 if (!error)
47205 if (DECL_LOCAL_DECL_P (decl))
47206 /* We need to mark the aliased decl, as that is the entity
47207 that is being referred to. This won't work for
47208 dependent variables, but it didn't work for them before
47209 DECL_LOCAL_DECL_P was a thing either. But then
47210 dependent local extern variable decls are as rare as
47211 hen's teeth. */
47212 if (auto alias = DECL_LOCAL_DECL_ALIAS (decl))
47213 if (alias != error_mark_node)
47214 decl = alias;
47216 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
47217 || lookup_attribute ("omp declare target link",
47218 DECL_ATTRIBUTES (decl)))
47220 error_at (loc, "variable %qD used more than once with "
47221 "%<#pragma acc declare%>", decl);
47222 error = true;
47223 continue;
47226 tree id;
47227 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
47228 id = get_identifier ("omp declare target link");
47229 else
47230 id = get_identifier ("omp declare target");
47232 DECL_ATTRIBUTES (decl)
47233 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
47234 if (current_binding_level->kind == sk_namespace)
47236 symtab_node *node = symtab_node::get (decl);
47237 if (node != NULL)
47239 node->offloadable = 1;
47240 if (ENABLE_OFFLOADING)
47242 g->have_offload = true;
47243 if (is_a <varpool_node *> (node))
47244 vec_safe_push (offload_vars, decl);
47251 if (error || current_binding_level->kind == sk_namespace)
47252 return NULL_TREE;
47254 stmt = make_node (OACC_DECLARE);
47255 TREE_TYPE (stmt) = void_type_node;
47256 OACC_DECLARE_CLAUSES (stmt) = clauses;
47257 SET_EXPR_LOCATION (stmt, pragma_tok->location);
47259 add_stmt (stmt);
47261 return NULL_TREE;
47264 /* OpenACC 2.0:
47265 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
47269 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
47271 LOC is the location of the #pragma token.
47274 #define OACC_ENTER_DATA_CLAUSE_MASK \
47275 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
47276 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
47277 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
47278 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
47279 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
47280 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
47282 #define OACC_EXIT_DATA_CLAUSE_MASK \
47283 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
47284 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
47285 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
47286 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
47287 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DETACH) \
47288 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
47289 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
47291 static tree
47292 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
47293 bool enter)
47295 location_t loc = pragma_tok->location;
47296 tree stmt, clauses;
47297 const char *p = "";
47299 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47300 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
47302 if (strcmp (p, "data") != 0)
47304 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
47305 enter ? "enter" : "exit");
47306 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47307 return NULL_TREE;
47310 cp_lexer_consume_token (parser->lexer);
47312 if (enter)
47313 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
47314 "#pragma acc enter data", pragma_tok);
47315 else
47316 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
47317 "#pragma acc exit data", pragma_tok);
47319 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
47321 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
47322 enter ? "enter" : "exit");
47323 return NULL_TREE;
47326 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
47327 TREE_TYPE (stmt) = void_type_node;
47328 OMP_STANDALONE_CLAUSES (stmt) = clauses;
47329 SET_EXPR_LOCATION (stmt, loc);
47330 add_stmt (stmt);
47331 return stmt;
47334 /* OpenACC 2.0:
47335 # pragma acc loop oacc-loop-clause[optseq] new-line
47336 structured-block */
47338 #define OACC_LOOP_CLAUSE_MASK \
47339 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
47340 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
47341 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
47342 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
47343 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
47344 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
47345 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
47346 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
47347 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
47348 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
47350 static tree
47351 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
47352 omp_clause_mask mask, tree *cclauses, bool *if_p)
47354 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
47356 strcat (p_name, " loop");
47357 mask |= OACC_LOOP_CLAUSE_MASK;
47359 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
47360 /*finish_p=*/cclauses == NULL,
47361 /*target=*/is_parallel);
47362 if (cclauses)
47364 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
47365 if (*cclauses)
47366 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC_TARGET);
47367 if (clauses)
47368 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
47371 tree block = begin_omp_structured_block ();
47372 int save = cp_parser_begin_omp_structured_block (parser);
47373 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
47374 cp_parser_end_omp_structured_block (parser, save);
47376 /* Later processing of combined acc loop constructs gets confused
47377 by an extra level of empty nested BIND_EXPRs, so flatten them. */
47378 block = finish_omp_structured_block (block);
47379 if (TREE_CODE (block) == BIND_EXPR
47380 && TREE_CODE (BIND_EXPR_BODY (block)) == BIND_EXPR
47381 && !BIND_EXPR_VARS (block))
47382 block = BIND_EXPR_BODY (block);
47383 add_stmt (block);
47385 return stmt;
47388 /* OpenACC 2.0:
47389 # pragma acc kernels oacc-kernels-clause[optseq] new-line
47390 structured-block
47394 # pragma acc parallel oacc-parallel-clause[optseq] new-line
47395 structured-block
47397 OpenACC 2.6:
47399 # pragma acc serial oacc-serial-clause[optseq] new-line
47402 #define OACC_KERNELS_CLAUSE_MASK \
47403 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
47404 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
47405 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
47406 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
47407 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
47408 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
47409 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
47410 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
47411 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
47412 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
47413 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
47414 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
47415 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
47416 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
47417 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
47418 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
47420 #define OACC_PARALLEL_CLAUSE_MASK \
47421 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
47422 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
47423 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
47424 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
47425 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
47426 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
47427 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
47428 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
47429 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
47430 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
47431 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
47432 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
47433 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
47434 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
47435 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
47436 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
47437 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
47438 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
47439 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
47441 #define OACC_SERIAL_CLAUSE_MASK \
47442 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
47443 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
47444 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
47445 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
47446 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
47447 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
47448 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
47449 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
47450 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
47451 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
47452 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
47453 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
47454 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
47455 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
47456 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
47457 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
47459 static tree
47460 cp_parser_oacc_compute (cp_parser *parser, cp_token *pragma_tok,
47461 char *p_name, bool *if_p)
47463 omp_clause_mask mask;
47464 enum tree_code code;
47465 switch (cp_parser_pragma_kind (pragma_tok))
47467 case PRAGMA_OACC_KERNELS:
47468 strcat (p_name, " kernels");
47469 mask = OACC_KERNELS_CLAUSE_MASK;
47470 code = OACC_KERNELS;
47471 break;
47472 case PRAGMA_OACC_PARALLEL:
47473 strcat (p_name, " parallel");
47474 mask = OACC_PARALLEL_CLAUSE_MASK;
47475 code = OACC_PARALLEL;
47476 break;
47477 case PRAGMA_OACC_SERIAL:
47478 strcat (p_name, " serial");
47479 mask = OACC_SERIAL_CLAUSE_MASK;
47480 code = OACC_SERIAL;
47481 break;
47482 default:
47483 gcc_unreachable ();
47486 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47488 const char *p
47489 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
47490 if (strcmp (p, "loop") == 0)
47492 cp_lexer_consume_token (parser->lexer);
47493 tree block = begin_omp_parallel ();
47494 tree clauses;
47495 tree stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask,
47496 &clauses, if_p);
47497 protected_set_expr_location (stmt, pragma_tok->location);
47498 return finish_omp_construct (code, block, clauses);
47502 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
47503 /*finish_p=*/true,
47504 /*target=*/true);
47506 tree block = begin_omp_parallel ();
47507 unsigned int save = cp_parser_begin_omp_structured_block (parser);
47508 cp_parser_statement (parser, NULL_TREE, false, if_p);
47509 cp_parser_end_omp_structured_block (parser, save);
47510 return finish_omp_construct (code, block, clauses);
47513 /* OpenACC 2.0:
47514 # pragma acc update oacc-update-clause[optseq] new-line
47517 #define OACC_UPDATE_CLAUSE_MASK \
47518 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
47519 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
47520 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
47521 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
47522 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
47523 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
47524 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
47526 static tree
47527 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
47529 tree stmt, clauses;
47531 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
47532 "#pragma acc update", pragma_tok);
47534 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
47536 error_at (pragma_tok->location,
47537 "%<#pragma acc update%> must contain at least one "
47538 "%<device%> or %<host%> or %<self%> clause");
47539 return NULL_TREE;
47542 stmt = make_node (OACC_UPDATE);
47543 TREE_TYPE (stmt) = void_type_node;
47544 OACC_UPDATE_CLAUSES (stmt) = clauses;
47545 SET_EXPR_LOCATION (stmt, pragma_tok->location);
47546 add_stmt (stmt);
47547 return stmt;
47550 /* OpenACC 2.0:
47551 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
47553 LOC is the location of the #pragma token.
47556 #define OACC_WAIT_CLAUSE_MASK \
47557 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
47559 static tree
47560 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
47562 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
47563 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
47565 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
47566 list = cp_parser_oacc_wait_list (parser, loc, list);
47568 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
47569 "#pragma acc wait", pragma_tok);
47571 stmt = c_finish_oacc_wait (loc, list, clauses);
47572 stmt = finish_expr_stmt (stmt);
47574 return stmt;
47577 /* OpenMP 4.0:
47578 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
47580 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
47581 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
47582 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
47583 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
47584 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
47585 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
47586 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
47588 static void
47589 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
47590 enum pragma_context context,
47591 bool variant_p)
47593 bool first_p = parser->omp_declare_simd == NULL;
47594 cp_omp_declare_simd_data data;
47595 if (first_p)
47597 data.error_seen = false;
47598 data.fndecl_seen = false;
47599 data.variant_p = variant_p;
47600 data.tokens = vNULL;
47601 data.attribs[0] = NULL;
47602 data.attribs[1] = NULL;
47603 data.loc = UNKNOWN_LOCATION;
47604 /* It is safe to take the address of a local variable; it will only be
47605 used while this scope is live. */
47606 parser->omp_declare_simd = &data;
47608 else if (parser->omp_declare_simd->variant_p != variant_p)
47610 error_at (pragma_tok->location,
47611 "%<#pragma omp declare %s%> followed by "
47612 "%<#pragma omp declare %s%>",
47613 parser->omp_declare_simd->variant_p ? "variant" : "simd",
47614 parser->omp_declare_simd->variant_p ? "simd" : "variant");
47615 parser->omp_declare_simd->error_seen = true;
47618 /* Store away all pragma tokens. */
47619 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
47620 cp_lexer_consume_token (parser->lexer);
47621 cp_parser_require_pragma_eol (parser, pragma_tok);
47622 struct cp_token_cache *cp
47623 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
47624 parser->omp_declare_simd->tokens.safe_push (cp);
47626 if (first_p)
47628 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
47629 cp_parser_pragma (parser, context, NULL);
47630 switch (context)
47632 case pragma_external:
47633 cp_parser_declaration (parser, NULL_TREE);
47634 break;
47635 case pragma_member:
47636 cp_parser_member_declaration (parser);
47637 break;
47638 case pragma_objc_icode:
47639 cp_parser_block_declaration (parser, /*statement_p=*/false);
47640 break;
47641 default:
47642 cp_parser_declaration_statement (parser);
47643 break;
47645 if (parser->omp_declare_simd
47646 && !parser->omp_declare_simd->error_seen
47647 && !parser->omp_declare_simd->fndecl_seen)
47648 error_at (pragma_tok->location,
47649 "%<#pragma omp declare %s%> not immediately followed by "
47650 "function declaration or definition",
47651 parser->omp_declare_simd->variant_p ? "variant" : "simd");
47652 data.tokens.release ();
47653 parser->omp_declare_simd = NULL;
47657 /* OpenMP 5.0:
47659 trait-selector:
47660 trait-selector-name[([trait-score:]trait-property[,trait-property[,...]])]
47662 trait-score:
47663 score(score-expression)
47665 Note that this function returns a list of trait selectors for the
47666 trait-selector-set SET. */
47668 static tree
47669 cp_parser_omp_context_selector (cp_parser *parser, enum omp_tss_code set,
47670 bool has_parms_p)
47672 tree ret = NULL_TREE;
47675 tree selector;
47676 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
47677 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47678 selector = cp_lexer_peek_token (parser->lexer)->u.value;
47679 else
47681 cp_parser_error (parser, "expected trait selector name");
47682 return error_mark_node;
47685 enum omp_ts_code sel
47686 = omp_lookup_ts_code (set, IDENTIFIER_POINTER (selector));
47688 if (sel == OMP_TRAIT_INVALID)
47690 /* Per the spec, "Implementations can ignore specified selectors
47691 that are not those described in this section"; however, we
47692 must record such selectors because they cause match failures. */
47693 warning_at (cp_lexer_peek_token (parser->lexer)->location,
47694 OPT_Wopenmp,
47695 "unknown selector %qs for context selector set %qs",
47696 IDENTIFIER_POINTER (selector), omp_tss_map[set]);
47697 cp_lexer_consume_token (parser->lexer);
47698 ret = make_trait_selector (sel, NULL_TREE, NULL_TREE, ret);
47699 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
47700 for (size_t n = cp_parser_skip_balanced_tokens (parser, 1) - 1;
47701 n; --n)
47702 cp_lexer_consume_token (parser->lexer);
47703 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
47705 cp_lexer_consume_token (parser->lexer);
47706 continue;
47708 else
47709 break;
47712 cp_lexer_consume_token (parser->lexer);
47714 tree properties = NULL_TREE;
47715 tree scoreval = NULL_TREE;
47716 enum omp_tp_type property_kind = omp_ts_map[sel].tp_type;
47717 bool allow_score = omp_ts_map[sel].allow_score;
47718 tree t;
47720 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
47722 if (property_kind == OMP_TRAIT_PROPERTY_NONE)
47724 error ("selector %qs does not accept any properties",
47725 IDENTIFIER_POINTER (selector));
47726 return error_mark_node;
47729 matching_parens parens;
47730 parens.consume_open (parser);
47732 cp_token *token = cp_lexer_peek_token (parser->lexer);
47733 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
47734 && strcmp (IDENTIFIER_POINTER (token->u.value), "score") == 0
47735 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
47737 cp_lexer_save_tokens (parser->lexer);
47738 cp_lexer_consume_token (parser->lexer);
47739 cp_lexer_consume_token (parser->lexer);
47740 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
47741 true)
47742 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
47744 cp_lexer_rollback_tokens (parser->lexer);
47745 cp_lexer_consume_token (parser->lexer);
47747 matching_parens parens2;
47748 parens2.require_open (parser);
47749 tree score = cp_parser_constant_expression (parser);
47750 if (!parens2.require_close (parser))
47751 cp_parser_skip_to_closing_parenthesis (parser, true,
47752 false, true);
47753 cp_parser_require (parser, CPP_COLON, RT_COLON);
47754 if (!allow_score)
47755 error_at (token->location,
47756 "%<score%> cannot be specified in traits "
47757 "in the %qs trait-selector-set",
47758 omp_tss_map[set]);
47759 else if (score != error_mark_node)
47761 score = fold_non_dependent_expr (score);
47762 if (value_dependent_expression_p (score))
47763 scoreval = score;
47764 else if (!INTEGRAL_TYPE_P (TREE_TYPE (score))
47765 || TREE_CODE (score) != INTEGER_CST)
47766 error_at (token->location, "%<score%> argument must "
47767 "be constant integer expression");
47768 else if (tree_int_cst_sgn (score) < 0)
47769 error_at (token->location, "%<score%> argument must "
47770 "be non-negative");
47771 else
47772 scoreval = score;
47775 else
47776 cp_lexer_rollback_tokens (parser->lexer);
47778 token = cp_lexer_peek_token (parser->lexer);
47781 switch (property_kind)
47783 case OMP_TRAIT_PROPERTY_ID:
47784 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
47785 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47787 tree prop = cp_lexer_peek_token (parser->lexer)->u.value;
47788 cp_lexer_consume_token (parser->lexer);
47789 properties = make_trait_property (prop, NULL_TREE,
47790 properties);
47792 else
47794 cp_parser_error (parser, "expected identifier");
47795 return error_mark_node;
47797 break;
47798 case OMP_TRAIT_PROPERTY_NAME_LIST:
47801 tree prop = OMP_TP_NAMELIST_NODE;
47802 tree value = NULL_TREE;
47803 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
47804 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47806 value = cp_lexer_peek_token (parser->lexer)->u.value;
47807 cp_lexer_consume_token (parser->lexer);
47809 else if (cp_lexer_next_token_is (parser->lexer, CPP_STRING))
47810 value = cp_parser_string_literal (parser,
47811 /*translate=*/false,
47812 /*wide_ok=*/false);
47813 else
47815 cp_parser_error (parser, "expected identifier or "
47816 "string literal");
47817 return error_mark_node;
47820 properties = make_trait_property (prop, value, properties);
47822 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
47823 cp_lexer_consume_token (parser->lexer);
47824 else
47825 break;
47827 while (1);
47828 break;
47829 case OMP_TRAIT_PROPERTY_EXPR:
47830 /* FIXME: this is bogus, the expression need
47831 not be constant. */
47832 t = cp_parser_constant_expression (parser);
47833 if (t != error_mark_node)
47835 t = fold_non_dependent_expr (t);
47836 if (!value_dependent_expression_p (t)
47837 && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
47838 || !tree_fits_shwi_p (t)))
47839 error_at (token->location, "property must be "
47840 "constant integer expression");
47841 else
47842 properties = make_trait_property (NULL_TREE, t,
47843 properties);
47845 else
47846 return error_mark_node;
47847 break;
47848 case OMP_TRAIT_PROPERTY_CLAUSE_LIST:
47849 if (sel == OMP_TRAIT_CONSTRUCT_SIMD)
47851 if (!has_parms_p)
47853 error_at (token->location, "properties for %<simd%> "
47854 "selector may not be specified in "
47855 "%<metadirective%>");
47856 return error_mark_node;
47858 properties
47859 = cp_parser_omp_all_clauses (parser,
47860 OMP_DECLARE_SIMD_CLAUSE_MASK,
47861 "simd", NULL, true, 2);
47863 else if (sel == OMP_TRAIT_IMPLEMENTATION_REQUIRES)
47865 /* FIXME: The "requires" selector was added in OpenMP 5.1.
47866 Currently only the now-deprecated syntax
47867 from OpenMP 5.0 is supported. */
47868 sorry_at (token->location,
47869 "%<requires%> selector is not supported yet");
47870 return error_mark_node;
47872 else
47873 gcc_unreachable ();
47874 break;
47875 default:
47876 gcc_unreachable ();
47879 if (!parens.require_close (parser))
47880 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
47882 properties = nreverse (properties);
47884 else if (property_kind != OMP_TRAIT_PROPERTY_NONE
47885 && property_kind != OMP_TRAIT_PROPERTY_CLAUSE_LIST
47886 && property_kind != OMP_TRAIT_PROPERTY_EXTENSION)
47888 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
47889 return error_mark_node;
47892 ret = make_trait_selector (sel, scoreval, properties, ret);
47894 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
47895 cp_lexer_consume_token (parser->lexer);
47896 else
47897 break;
47899 while (1);
47901 return nreverse (ret);
47904 /* OpenMP 5.0:
47906 trait-set-selector[,trait-set-selector[,...]]
47908 trait-set-selector:
47909 trait-set-selector-name = { trait-selector[, trait-selector[, ...]] }
47911 trait-set-selector-name:
47912 constructor
47913 device
47914 implementation
47915 user */
47917 static tree
47918 cp_parser_omp_context_selector_specification (cp_parser *parser,
47919 bool has_parms_p)
47921 tree ret = NULL_TREE;
47924 const char *setp = "";
47925 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47926 setp
47927 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
47928 enum omp_tss_code set = omp_lookup_tss_code (setp);
47930 if (set == OMP_TRAIT_SET_INVALID)
47932 cp_parser_error (parser, "expected context selector set name");
47933 return error_mark_node;
47936 cp_lexer_consume_token (parser->lexer);
47938 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
47939 return error_mark_node;
47941 matching_braces braces;
47942 if (!braces.require_open (parser))
47943 return error_mark_node;
47945 tree selectors
47946 = cp_parser_omp_context_selector (parser, set, has_parms_p);
47947 if (selectors == error_mark_node)
47949 cp_parser_skip_to_closing_brace (parser);
47950 ret = error_mark_node;
47952 else if (ret != error_mark_node)
47953 ret = make_trait_set_selector (set, selectors, ret);
47955 braces.require_close (parser);
47957 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
47958 cp_lexer_consume_token (parser->lexer);
47959 else
47960 break;
47962 while (1);
47964 if (ret == error_mark_node)
47965 return ret;
47966 return nreverse (ret);
47969 /* Assumption clauses:
47970 OpenMP 5.1
47971 absent (directive-name-list)
47972 contains (directive-name-list)
47973 holds (expression)
47974 no_openmp
47975 no_openmp_routines
47976 no_parallelism */
47978 static void
47979 cp_parser_omp_assumption_clauses (cp_parser *parser, cp_token *pragma_tok,
47980 bool is_assume)
47982 bool no_openmp = false;
47983 bool no_openmp_routines = false;
47984 bool no_parallelism = false;
47985 bitmap_head absent_head, contains_head;
47987 bitmap_obstack_initialize (NULL);
47988 bitmap_initialize (&absent_head, &bitmap_default_obstack);
47989 bitmap_initialize (&contains_head, &bitmap_default_obstack);
47991 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
47992 error_at (cp_lexer_peek_token (parser->lexer)->location,
47993 "expected at least one assumption clause");
47995 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
47997 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
47998 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
47999 cp_lexer_consume_token (parser->lexer);
48001 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
48002 break;
48004 const char *p
48005 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
48006 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
48008 if (!strcmp (p, "no_openmp"))
48010 cp_lexer_consume_token (parser->lexer);
48011 if (no_openmp)
48012 error_at (cloc, "too many %qs clauses", "no_openmp");
48013 no_openmp = true;
48015 else if (!strcmp (p, "no_openmp_routines"))
48017 cp_lexer_consume_token (parser->lexer);
48018 if (no_openmp_routines)
48019 error_at (cloc, "too many %qs clauses", "no_openmp_routines");
48020 no_openmp_routines = true;
48022 else if (!strcmp (p, "no_parallelism"))
48024 cp_lexer_consume_token (parser->lexer);
48025 if (no_parallelism)
48026 error_at (cloc, "too many %qs clauses", "no_parallelism");
48027 no_parallelism = true;
48029 else if (!strcmp (p, "holds"))
48031 cp_lexer_consume_token (parser->lexer);
48032 matching_parens parens;
48033 if (parens.require_open (parser))
48035 location_t eloc = cp_lexer_peek_token (parser->lexer)->location;
48036 tree t = cp_parser_assignment_expression (parser);
48037 if (!type_dependent_expression_p (t))
48038 t = contextual_conv_bool (t, tf_warning_or_error);
48039 if (is_assume && !error_operand_p (t))
48040 finish_expr_stmt (build_assume_call (eloc, t));
48041 if (!parens.require_close (parser))
48042 cp_parser_skip_to_closing_parenthesis (parser,
48043 /*recovering=*/true,
48044 /*or_comma=*/false,
48045 /*consume_paren=*/true);
48048 else if (!strcmp (p, "absent") || !strcmp (p, "contains"))
48050 cp_lexer_consume_token (parser->lexer);
48051 matching_parens parens;
48052 if (parens.require_open (parser))
48056 const char *directive[3] = {};
48057 int i;
48058 location_t dloc
48059 = cp_lexer_peek_token (parser->lexer)->location;
48060 for (i = 0; i < 3; i++)
48062 tree id;
48063 if (cp_lexer_nth_token_is (parser->lexer, i + 1, CPP_NAME))
48064 id = cp_lexer_peek_nth_token (parser->lexer,
48065 i + 1)->u.value;
48066 else if (cp_lexer_nth_token_is (parser->lexer, i + 1,
48067 CPP_KEYWORD))
48069 enum rid rid
48070 = cp_lexer_peek_nth_token (parser->lexer,
48071 i + 1)->keyword;
48072 id = ridpointers[rid];
48074 else
48075 break;
48076 directive[i] = IDENTIFIER_POINTER (id);
48078 if (i == 0)
48079 error_at (dloc, "expected directive name");
48080 else
48082 const struct c_omp_directive *dir
48083 = c_omp_categorize_directive (directive[0],
48084 directive[1],
48085 directive[2]);
48086 if (dir == NULL
48087 || dir->kind == C_OMP_DIR_DECLARATIVE
48088 || dir->kind == C_OMP_DIR_INFORMATIONAL
48089 || dir->id == PRAGMA_OMP_END
48090 || (!dir->second && directive[1])
48091 || (!dir->third && directive[2]))
48092 error_at (dloc, "unknown OpenMP directive name in "
48093 "%qs clause argument", p);
48094 else
48096 int id = dir - c_omp_directives;
48097 if (bitmap_bit_p (p[0] == 'a' ? &contains_head
48098 : &absent_head, id))
48099 error_at (dloc, "%<%s%s%s%s%s%> directive "
48100 "mentioned in both %<absent%> and "
48101 "%<contains%> clauses",
48102 directive[0],
48103 directive[1] ? " " : "",
48104 directive[1] ? directive[1] : "",
48105 directive[2] ? " " : "",
48106 directive[2] ? directive[2] : "");
48107 else if (!bitmap_set_bit (p[0] == 'a'
48108 ? &absent_head
48109 : &contains_head, id))
48110 error_at (dloc, "%<%s%s%s%s%s%> directive "
48111 "mentioned multiple times in %qs "
48112 "clauses",
48113 directive[0],
48114 directive[1] ? " " : "",
48115 directive[1] ? directive[1] : "",
48116 directive[2] ? " " : "",
48117 directive[2] ? directive[2] : "", p);
48119 for (; i; --i)
48120 cp_lexer_consume_token (parser->lexer);
48122 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
48123 cp_lexer_consume_token (parser->lexer);
48124 else
48125 break;
48127 while (1);
48128 if (!parens.require_close (parser))
48129 cp_parser_skip_to_closing_parenthesis (parser,
48130 /*recovering=*/true,
48131 /*or_comma=*/false,
48132 /*consume_paren=*/true);
48135 else if (startswith (p, "ext_"))
48137 warning_at (cloc, OPT_Wopenmp, "unknown assumption clause %qs", p);
48138 cp_lexer_consume_token (parser->lexer);
48139 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
48140 for (size_t n = cp_parser_skip_balanced_tokens (parser, 1) - 1;
48141 n; --n)
48142 cp_lexer_consume_token (parser->lexer);
48144 else
48146 cp_lexer_consume_token (parser->lexer);
48147 error_at (cloc, "expected assumption clause");
48148 break;
48151 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
48154 /* OpenMP 5.1
48155 # pragma omp assume clauses[optseq] new-line */
48157 static void
48158 cp_parser_omp_assume (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
48160 cp_parser_omp_assumption_clauses (parser, pragma_tok, true);
48161 add_stmt (cp_parser_omp_structured_block (parser, if_p));
48164 /* OpenMP 5.1
48165 # pragma omp assumes clauses[optseq] new-line */
48167 static bool
48168 cp_parser_omp_assumes (cp_parser *parser, cp_token *pragma_tok)
48170 cp_parser_omp_assumption_clauses (parser, pragma_tok, false);
48171 return false;
48174 /* Finalize #pragma omp declare variant after a fndecl has been parsed, and put
48175 that into "omp declare variant base" attribute. */
48177 static tree
48178 cp_finish_omp_declare_variant (cp_parser *parser, cp_token *pragma_tok,
48179 tree attrs)
48181 matching_parens parens;
48182 if (!parens.require_open (parser))
48184 fail:
48185 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
48186 return attrs;
48189 bool template_p;
48190 cp_id_kind idk = CP_ID_KIND_NONE;
48191 cp_token *varid_token = cp_lexer_peek_token (parser->lexer);
48192 cp_expr varid
48193 = cp_parser_id_expression (parser, /*template_keyword_p=*/false,
48194 /*check_dependency_p=*/true,
48195 /*template_p=*/&template_p,
48196 /*declarator_p=*/false,
48197 /*optional_p=*/false);
48198 parens.require_close (parser);
48200 tree variant;
48201 if (TREE_CODE (varid) == TEMPLATE_ID_EXPR
48202 || TREE_CODE (varid) == TYPE_DECL
48203 || varid == error_mark_node)
48204 variant = varid;
48205 else if (varid_token->type == CPP_NAME && varid_token->error_reported)
48206 variant = NULL_TREE;
48207 else
48209 tree ambiguous_decls;
48210 variant = cp_parser_lookup_name (parser, varid, none_type,
48211 template_p, /*is_namespace=*/false,
48212 /*check_dependency=*/true,
48213 &ambiguous_decls,
48214 varid.get_location ());
48215 if (ambiguous_decls)
48216 variant = NULL_TREE;
48218 if (variant == NULL_TREE)
48219 variant = error_mark_node;
48220 else if (TREE_CODE (variant) != SCOPE_REF)
48222 const char *error_msg;
48223 variant
48224 = finish_id_expression (varid, variant, parser->scope,
48225 &idk, false, true,
48226 &parser->non_integral_constant_expression_p,
48227 template_p, true, false, false, &error_msg,
48228 varid.get_location ());
48229 if (error_msg)
48230 cp_parser_error (parser, error_msg);
48232 location_t caret_loc = get_pure_location (varid.get_location ());
48233 location_t start_loc = get_start (varid_token->location);
48234 location_t finish_loc = get_finish (varid.get_location ());
48235 location_t varid_loc = make_location (caret_loc, start_loc, finish_loc);
48237 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
48238 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
48239 cp_lexer_consume_token (parser->lexer);
48241 const char *clause = "";
48242 location_t match_loc = cp_lexer_peek_token (parser->lexer)->location;
48243 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
48244 clause = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
48245 if (strcmp (clause, "match"))
48247 cp_parser_error (parser, "expected %<match%>");
48248 goto fail;
48251 cp_lexer_consume_token (parser->lexer);
48253 if (!parens.require_open (parser))
48254 goto fail;
48256 tree ctx = cp_parser_omp_context_selector_specification (parser, true);
48257 if (ctx == error_mark_node)
48258 goto fail;
48259 ctx = omp_check_context_selector (match_loc, ctx);
48260 if (ctx != error_mark_node && variant != error_mark_node)
48262 tree match_loc_node = maybe_wrap_with_location (integer_zero_node,
48263 match_loc);
48264 tree loc_node = maybe_wrap_with_location (integer_zero_node, varid_loc);
48265 loc_node = tree_cons (match_loc_node,
48266 build_int_cst (integer_type_node, idk),
48267 build_tree_list (loc_node, integer_zero_node));
48268 attrs = tree_cons (get_identifier ("omp declare variant base"),
48269 tree_cons (variant, ctx, loc_node), attrs);
48270 if (processing_template_decl)
48271 ATTR_IS_DEPENDENT (attrs) = 1;
48274 parens.require_close (parser);
48275 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
48276 return attrs;
48280 /* Finalize #pragma omp declare simd clauses after direct declarator has
48281 been parsed, and put that into "omp declare simd" attribute. */
48283 static tree
48284 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
48286 struct cp_token_cache *ce;
48287 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
48288 int i;
48290 if (!data->error_seen && data->fndecl_seen)
48292 error ("%<#pragma omp declare %s%> not immediately followed by "
48293 "a single function declaration or definition",
48294 data->variant_p ? "variant" : "simd");
48295 data->error_seen = true;
48297 if (data->error_seen)
48298 return attrs;
48300 FOR_EACH_VEC_ELT (data->tokens, i, ce)
48302 tree c, cl;
48304 cp_parser_push_lexer_for_tokens (parser, ce);
48305 parser->lexer->in_pragma = true;
48306 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
48307 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
48308 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
48309 const char *kind = IDENTIFIER_POINTER (id);
48310 cp_lexer_consume_token (parser->lexer);
48311 if (strcmp (kind, "simd") == 0)
48313 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
48314 "#pragma omp declare simd",
48315 pragma_tok);
48316 if (cl)
48317 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
48318 c = build_tree_list (get_identifier ("omp declare simd"), cl);
48319 TREE_CHAIN (c) = attrs;
48320 if (processing_template_decl)
48321 ATTR_IS_DEPENDENT (c) = 1;
48322 attrs = c;
48324 else
48326 gcc_assert (strcmp (kind, "variant") == 0);
48327 attrs
48328 = cp_finish_omp_declare_variant (parser, pragma_tok, attrs);
48330 cp_parser_pop_lexer (parser);
48333 cp_lexer *lexer = NULL;
48334 for (int i = 0; i < 2; i++)
48336 if (data->attribs[i] == NULL)
48337 continue;
48338 for (tree *pa = data->attribs[i]; *pa; )
48339 if (get_attribute_namespace (*pa) == omp_identifier
48340 && is_attribute_p ("directive", get_attribute_name (*pa)))
48342 for (tree a = TREE_VALUE (*pa); a; a = TREE_CHAIN (a))
48344 tree d = TREE_VALUE (a);
48345 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
48346 cp_token *first = DEFPARSE_TOKENS (d)->first;
48347 cp_token *last = DEFPARSE_TOKENS (d)->last;
48348 const char *directive[3] = {};
48349 for (int j = 0; j < 3; j++)
48351 tree id = NULL_TREE;
48352 if (first + j == last)
48353 break;
48354 if (first[j].type == CPP_NAME)
48355 id = first[j].u.value;
48356 else if (first[j].type == CPP_KEYWORD)
48357 id = ridpointers[(int) first[j].keyword];
48358 else
48359 break;
48360 directive[j] = IDENTIFIER_POINTER (id);
48362 const c_omp_directive *dir = NULL;
48363 if (directive[0])
48364 dir = c_omp_categorize_directive (directive[0], directive[1],
48365 directive[2]);
48366 if (dir == NULL)
48368 error_at (first->location,
48369 "unknown OpenMP directive name in "
48370 "%qs attribute argument",
48371 TREE_PUBLIC (d)
48372 ? "omp::decl" : "omp::directive");
48373 continue;
48375 if (dir->id != PRAGMA_OMP_DECLARE
48376 || (strcmp (directive[1], "simd") != 0
48377 && strcmp (directive[1], "variant") != 0))
48379 error_at (first->location,
48380 "OpenMP directive other than %<declare simd%> "
48381 "or %<declare variant%> appertains to a "
48382 "declaration");
48383 continue;
48386 if (parser->omp_attrs_forbidden_p)
48388 error_at (first->location,
48389 "mixing OpenMP directives with attribute and "
48390 "pragma syntax on the same statement");
48391 parser->omp_attrs_forbidden_p = false;
48394 if (!flag_openmp && strcmp (directive[1], "simd") != 0)
48395 continue;
48396 if (lexer == NULL)
48398 lexer = cp_lexer_alloc ();
48399 lexer->debugging_p = parser->lexer->debugging_p;
48401 vec_safe_reserve (lexer->buffer, (last - first) + 2);
48402 cp_token tok = {};
48403 tok.type = CPP_PRAGMA;
48404 tok.keyword = RID_MAX;
48405 tok.u.value = build_int_cst (NULL, PRAGMA_OMP_DECLARE);
48406 tok.location = first->location;
48407 lexer->buffer->quick_push (tok);
48408 while (++first < last)
48409 lexer->buffer->quick_push (*first);
48410 tok = {};
48411 tok.type = CPP_PRAGMA_EOL;
48412 tok.keyword = RID_MAX;
48413 tok.location = last->location;
48414 lexer->buffer->quick_push (tok);
48415 tok = {};
48416 tok.type = CPP_EOF;
48417 tok.keyword = RID_MAX;
48418 tok.location = last->location;
48419 lexer->buffer->quick_push (tok);
48420 lexer->next = parser->lexer;
48421 lexer->next_token = lexer->buffer->address ();
48422 lexer->last_token = lexer->next_token
48423 + lexer->buffer->length ()
48424 - 1;
48425 lexer->in_omp_attribute_pragma = true;
48426 parser->lexer = lexer;
48427 /* Move the current source position to that of the first token
48428 in the new lexer. */
48429 cp_lexer_set_source_position_from_token (lexer->next_token);
48431 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
48432 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
48433 const char *kind = IDENTIFIER_POINTER (id);
48434 cp_lexer_consume_token (parser->lexer);
48436 tree c, cl;
48437 if (strcmp (kind, "simd") == 0)
48439 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
48440 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
48441 cp_lexer_consume_token (parser->lexer);
48443 omp_clause_mask mask = OMP_DECLARE_SIMD_CLAUSE_MASK;
48444 cl = cp_parser_omp_all_clauses (parser, mask,
48445 "#pragma omp declare simd",
48446 pragma_tok);
48447 if (cl)
48448 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
48449 c = build_tree_list (get_identifier ("omp declare simd"),
48450 cl);
48451 TREE_CHAIN (c) = attrs;
48452 if (processing_template_decl)
48453 ATTR_IS_DEPENDENT (c) = 1;
48454 attrs = c;
48456 else
48458 gcc_assert (strcmp (kind, "variant") == 0);
48459 attrs
48460 = cp_finish_omp_declare_variant (parser, pragma_tok,
48461 attrs);
48463 gcc_assert (parser->lexer != lexer);
48464 vec_safe_truncate (lexer->buffer, 0);
48466 *pa = TREE_CHAIN (*pa);
48468 else
48469 pa = &TREE_CHAIN (*pa);
48471 if (lexer)
48472 cp_lexer_destroy (lexer);
48474 data->fndecl_seen = true;
48475 return attrs;
48478 /* D should be DEFERRED_PARSE from omp::decl attribute. If it contains
48479 a threadprivate, groupprivate, allocate or declare target directive,
48480 return true and parse it for DECL. */
48482 bool
48483 cp_maybe_parse_omp_decl (tree decl, tree d)
48485 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
48486 cp_token *first = DEFPARSE_TOKENS (d)->first;
48487 cp_token *last = DEFPARSE_TOKENS (d)->last;
48488 const char *directive[3] = {};
48489 for (int j = 0; j < 3; j++)
48491 tree id = NULL_TREE;
48492 if (first + j == last)
48493 break;
48494 if (first[j].type == CPP_NAME)
48495 id = first[j].u.value;
48496 else if (first[j].type == CPP_KEYWORD)
48497 id = ridpointers[(int) first[j].keyword];
48498 else
48499 break;
48500 directive[j] = IDENTIFIER_POINTER (id);
48502 const c_omp_directive *dir = NULL;
48503 if (directive[0])
48504 dir = c_omp_categorize_directive (directive[0], directive[1],
48505 directive[2]);
48506 if (dir == NULL)
48508 error_at (first->location,
48509 "unknown OpenMP directive name in "
48510 "%qs attribute argument", "omp::decl");
48511 return false;
48513 if (dir->id != PRAGMA_OMP_THREADPRIVATE
48514 /* && dir->id != PRAGMA_OMP_GROUPPRIVATE */
48515 && dir->id != PRAGMA_OMP_ALLOCATE
48516 && (dir->id != PRAGMA_OMP_DECLARE
48517 || strcmp (directive[1], "target") != 0))
48518 return false;
48520 if (!flag_openmp && !dir->simd)
48521 return true;
48523 cp_parser *parser = the_parser;
48524 cp_lexer *lexer = cp_lexer_alloc ();
48525 lexer->debugging_p = parser->lexer->debugging_p;
48526 lexer->in_omp_decl_attribute = decl;
48527 vec_safe_reserve (lexer->buffer, last - first + 3, true);
48528 cp_token tok = {};
48529 tok.type = CPP_PRAGMA;
48530 tok.keyword = RID_MAX;
48531 tok.u.value = build_int_cst (NULL, dir->id);
48532 tok.location = first->location;
48533 lexer->buffer->quick_push (tok);
48534 while (++first < last)
48535 lexer->buffer->quick_push (*first);
48536 tok = {};
48537 tok.type = CPP_PRAGMA_EOL;
48538 tok.keyword = RID_MAX;
48539 tok.location = last->location;
48540 lexer->buffer->quick_push (tok);
48541 tok = {};
48542 tok.type = CPP_EOF;
48543 tok.keyword = RID_MAX;
48544 tok.location = last->location;
48545 lexer->buffer->quick_push (tok);
48546 lexer->next = parser->lexer;
48547 lexer->next_token = lexer->buffer->address ();
48548 lexer->last_token = lexer->next_token
48549 + lexer->buffer->length ()
48550 - 1;
48551 lexer->in_omp_attribute_pragma = true;
48552 parser->lexer = lexer;
48553 /* Move the current source position to that of the first token in the
48554 new lexer. */
48555 cp_lexer_set_source_position_from_token (lexer->next_token);
48556 cp_parser_pragma (parser, pragma_external, NULL);
48558 return true;
48561 /* Helper for cp_parser_omp_declare_target, handle one to or link clause
48562 on #pragma omp declare target. Return false if errors were reported. */
48564 static bool
48565 handle_omp_declare_target_clause (tree c, tree t, int device_type,
48566 bool indirect)
48568 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
48569 tree at2 = lookup_attribute ("omp declare target link", DECL_ATTRIBUTES (t));
48570 tree id;
48571 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
48573 id = get_identifier ("omp declare target link");
48574 std::swap (at1, at2);
48576 else
48577 id = get_identifier ("omp declare target");
48578 if (at2)
48580 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
48581 error_at (OMP_CLAUSE_LOCATION (c),
48582 "%qD specified both in declare target %<link%> and %qs"
48583 " clauses", t, OMP_CLAUSE_ENTER_TO (c) ? "to" : "enter");
48584 else
48585 error_at (OMP_CLAUSE_LOCATION (c),
48586 "%qD specified both in declare target %<link%> and "
48587 "%<to%> or %<enter%> clauses", t);
48588 return false;
48590 if (!at1)
48592 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
48593 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
48594 return true;
48596 symtab_node *node = symtab_node::get (t);
48597 if (node != NULL)
48599 node->offloadable = 1;
48600 if (ENABLE_OFFLOADING)
48602 g->have_offload = true;
48603 if (is_a <varpool_node *> (node))
48604 vec_safe_push (offload_vars, t);
48608 if (TREE_CODE (t) != FUNCTION_DECL)
48609 return true;
48610 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_HOST) != 0)
48612 tree at3 = lookup_attribute ("omp declare target host",
48613 DECL_ATTRIBUTES (t));
48614 if (at3 == NULL_TREE)
48616 id = get_identifier ("omp declare target host");
48617 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
48620 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_NOHOST) != 0)
48622 tree at3 = lookup_attribute ("omp declare target nohost",
48623 DECL_ATTRIBUTES (t));
48624 if (at3 == NULL_TREE)
48626 id = get_identifier ("omp declare target nohost");
48627 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
48630 if (indirect)
48632 tree at4 = lookup_attribute ("omp declare target indirect",
48633 DECL_ATTRIBUTES (t));
48634 if (at4 == NULL_TREE)
48636 id = get_identifier ("omp declare target indirect");
48637 DECL_ATTRIBUTES (t)
48638 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
48641 return true;
48644 /* OpenMP 4.0:
48645 # pragma omp declare target new-line
48646 declarations and definitions
48647 # pragma omp end declare target new-line
48649 OpenMP 4.5:
48650 # pragma omp declare target ( extended-list ) new-line
48652 # pragma omp declare target declare-target-clauses[seq] new-line */
48654 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
48655 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
48656 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ENTER) \
48657 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK) \
48658 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE_TYPE) \
48659 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INDIRECT))
48661 static void
48662 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
48664 tree clauses = NULL_TREE;
48665 int device_type = 0;
48666 bool indirect = false;
48667 bool only_device_type_or_indirect = true;
48668 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
48669 || (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
48670 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME)))
48671 clauses
48672 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
48673 "#pragma omp declare target", pragma_tok);
48674 else if (parser->lexer->in_omp_decl_attribute
48675 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
48677 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_ENTER,
48678 clauses);
48679 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
48680 cp_parser_require_pragma_eol (parser, pragma_tok);
48682 else
48684 cp_omp_declare_target_attr a
48685 = { parser->lexer->in_omp_attribute_pragma, -1, false };
48686 vec_safe_push (scope_chain->omp_declare_target_attribute, a);
48687 cp_parser_require_pragma_eol (parser, pragma_tok);
48688 return;
48690 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
48692 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE)
48693 device_type |= OMP_CLAUSE_DEVICE_TYPE_KIND (c);
48694 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_INDIRECT)
48695 indirect |= !integer_zerop (OMP_CLAUSE_INDIRECT_EXPR (c));
48697 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
48699 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE
48700 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_INDIRECT)
48701 continue;
48702 tree t = OMP_CLAUSE_DECL (c);
48703 only_device_type_or_indirect = false;
48704 if (!handle_omp_declare_target_clause (c, t, device_type, indirect))
48705 continue;
48706 if (VAR_OR_FUNCTION_DECL_P (t)
48707 && DECL_LOCAL_DECL_P (t)
48708 && DECL_LANG_SPECIFIC (t)
48709 && DECL_LOCAL_DECL_ALIAS (t)
48710 && DECL_LOCAL_DECL_ALIAS (t) != error_mark_node)
48711 handle_omp_declare_target_clause (c, DECL_LOCAL_DECL_ALIAS (t),
48712 device_type, indirect);
48714 if ((device_type || indirect) && only_device_type_or_indirect)
48715 error_at (OMP_CLAUSE_LOCATION (clauses),
48716 "directive with only %<device_type%> or %<indirect%> clauses");
48717 if (indirect && device_type && device_type != OMP_CLAUSE_DEVICE_TYPE_ANY)
48718 error_at (OMP_CLAUSE_LOCATION (clauses),
48719 "%<device_type%> clause must specify 'any' when used with "
48720 "an %<indirect%> clause");
48723 /* OpenMP 5.1
48724 # pragma omp begin assumes clauses[optseq] new-line
48726 # pragma omp begin declare target clauses[optseq] new-line */
48728 #define OMP_BEGIN_DECLARE_TARGET_CLAUSE_MASK \
48729 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE_TYPE) \
48730 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INDIRECT))
48732 static void
48733 cp_parser_omp_begin (cp_parser *parser, cp_token *pragma_tok)
48735 const char *p = "";
48736 bool in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
48737 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
48739 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
48740 p = IDENTIFIER_POINTER (id);
48742 if (strcmp (p, "declare") == 0)
48744 cp_lexer_consume_token (parser->lexer);
48745 p = "";
48746 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
48748 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
48749 p = IDENTIFIER_POINTER (id);
48751 if (strcmp (p, "target") == 0)
48753 cp_lexer_consume_token (parser->lexer);
48754 tree clauses
48755 = cp_parser_omp_all_clauses (parser,
48756 OMP_BEGIN_DECLARE_TARGET_CLAUSE_MASK,
48757 "#pragma omp begin declare target",
48758 pragma_tok);
48759 int device_type = 0;
48760 bool indirect = 0;
48761 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
48763 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE)
48764 device_type |= OMP_CLAUSE_DEVICE_TYPE_KIND (c);
48765 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_INDIRECT)
48766 indirect |= !integer_zerop (OMP_CLAUSE_INDIRECT_EXPR (c));
48768 cp_omp_declare_target_attr a
48769 = { in_omp_attribute_pragma, device_type, indirect };
48770 vec_safe_push (scope_chain->omp_declare_target_attribute, a);
48772 else
48774 cp_parser_error (parser, "expected %<target%>");
48775 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
48778 else if (strcmp (p, "assumes") == 0)
48780 cp_lexer_consume_token (parser->lexer);
48781 cp_parser_omp_assumption_clauses (parser, pragma_tok, false);
48782 cp_omp_begin_assumes_data a = { in_omp_attribute_pragma };
48783 vec_safe_push (scope_chain->omp_begin_assumes, a);
48785 else
48787 cp_parser_error (parser, "expected %<declare target%> or %<assumes%>");
48788 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
48792 /* OpenMP 4.0:
48793 # pragma omp end declare target new-line
48795 OpenMP 5.1:
48796 # pragma omp end assumes new-line */
48798 static void
48799 cp_parser_omp_end (cp_parser *parser, cp_token *pragma_tok)
48801 const char *p = "";
48802 bool in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
48803 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
48805 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
48806 p = IDENTIFIER_POINTER (id);
48808 if (strcmp (p, "declare") == 0)
48810 cp_lexer_consume_token (parser->lexer);
48811 p = "";
48812 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
48814 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
48815 p = IDENTIFIER_POINTER (id);
48817 if (strcmp (p, "target") == 0)
48818 cp_lexer_consume_token (parser->lexer);
48819 else
48821 cp_parser_error (parser, "expected %<target%>");
48822 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
48823 return;
48825 cp_parser_require_pragma_eol (parser, pragma_tok);
48826 if (!vec_safe_length (scope_chain->omp_declare_target_attribute))
48827 error_at (pragma_tok->location,
48828 "%<#pragma omp end declare target%> without corresponding "
48829 "%<#pragma omp declare target%> or "
48830 "%<#pragma omp begin declare target%>");
48831 else
48833 cp_omp_declare_target_attr
48834 a = scope_chain->omp_declare_target_attribute->pop ();
48835 if (a.attr_syntax != in_omp_attribute_pragma)
48837 if (a.attr_syntax)
48838 error_at (pragma_tok->location,
48839 "%qs in attribute syntax terminated "
48840 "with %qs in pragma syntax",
48841 a.device_type >= 0 ? "begin declare target"
48842 : "declare target",
48843 "end declare target");
48844 else
48845 error_at (pragma_tok->location,
48846 "%qs in pragma syntax terminated "
48847 "with %qs in attribute syntax",
48848 a.device_type >= 0 ? "begin declare target"
48849 : "declare target",
48850 "end declare target");
48854 else if (strcmp (p, "assumes") == 0)
48856 cp_lexer_consume_token (parser->lexer);
48857 cp_parser_require_pragma_eol (parser, pragma_tok);
48858 if (!vec_safe_length (scope_chain->omp_begin_assumes))
48859 error_at (pragma_tok->location,
48860 "%qs without corresponding %qs",
48861 "#pragma omp end assumes", "#pragma omp begin assumes");
48862 else
48864 cp_omp_begin_assumes_data
48865 a = scope_chain->omp_begin_assumes->pop ();
48866 if (a.attr_syntax != in_omp_attribute_pragma)
48868 if (a.attr_syntax)
48869 error_at (pragma_tok->location,
48870 "%qs in attribute syntax terminated "
48871 "with %qs in pragma syntax",
48872 "begin assumes", "end assumes");
48873 else
48874 error_at (pragma_tok->location,
48875 "%qs in pragma syntax terminated "
48876 "with %qs in attribute syntax",
48877 "begin assumes", "end assumes");
48881 else
48883 cp_parser_error (parser, "expected %<declare%> or %<assumes%>");
48884 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
48885 return;
48889 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
48890 expression and optional initializer clause of
48891 #pragma omp declare reduction. We store the expression(s) as
48892 either 3, 6 or 7 special statements inside of the artificial function's
48893 body. The first two statements are DECL_EXPRs for the artificial
48894 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
48895 expression that uses those variables.
48896 If there was any INITIALIZER clause, this is followed by further statements,
48897 the fourth and fifth statements are DECL_EXPRs for the artificial
48898 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
48899 constructor variant (first token after open paren is not omp_priv),
48900 then the sixth statement is a statement with the function call expression
48901 that uses the OMP_PRIV and optionally OMP_ORIG variable.
48902 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
48903 to initialize the OMP_PRIV artificial variable and there is seventh
48904 statement, a DECL_EXPR of the OMP_PRIV statement again. */
48906 static bool
48907 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
48909 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
48910 gcc_assert (TYPE_REF_P (type));
48911 type = TREE_TYPE (type);
48912 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
48913 DECL_ARTIFICIAL (omp_out) = 1;
48914 pushdecl (omp_out);
48915 add_decl_expr (omp_out);
48916 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
48917 DECL_ARTIFICIAL (omp_in) = 1;
48918 pushdecl (omp_in);
48919 add_decl_expr (omp_in);
48920 tree combiner;
48921 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
48923 keep_next_level (true);
48924 tree block = begin_omp_structured_block ();
48925 combiner = cp_parser_expression (parser);
48926 finish_expr_stmt (combiner);
48927 block = finish_omp_structured_block (block);
48928 if (processing_template_decl)
48929 block = build_stmt (input_location, EXPR_STMT, block);
48930 add_stmt (block);
48932 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
48933 return false;
48935 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
48936 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
48937 cp_lexer_consume_token (parser->lexer);
48939 const char *p = "";
48940 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
48942 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
48943 p = IDENTIFIER_POINTER (id);
48946 if (strcmp (p, "initializer") == 0)
48948 cp_lexer_consume_token (parser->lexer);
48949 matching_parens parens;
48950 if (!parens.require_open (parser))
48951 return false;
48953 p = "";
48954 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
48956 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
48957 p = IDENTIFIER_POINTER (id);
48960 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
48961 DECL_ARTIFICIAL (omp_priv) = 1;
48962 pushdecl (omp_priv);
48963 add_decl_expr (omp_priv);
48964 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
48965 DECL_ARTIFICIAL (omp_orig) = 1;
48966 pushdecl (omp_orig);
48967 add_decl_expr (omp_orig);
48969 keep_next_level (true);
48970 block = begin_omp_structured_block ();
48972 bool ctor = false;
48973 if (strcmp (p, "omp_priv") == 0)
48975 bool is_non_constant_init;
48976 ctor = true;
48977 cp_lexer_consume_token (parser->lexer);
48978 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
48979 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
48980 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
48981 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
48982 == CPP_CLOSE_PAREN
48983 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
48984 == CPP_CLOSE_PAREN))
48986 finish_omp_structured_block (block);
48987 error ("invalid initializer clause");
48988 return false;
48990 initializer = cp_parser_initializer (parser,
48991 /*is_direct_init=*/nullptr,
48992 &is_non_constant_init);
48993 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
48994 NULL_TREE, LOOKUP_ONLYCONVERTING);
48996 else
48998 cp_parser_parse_tentatively (parser);
48999 /* Don't create location wrapper nodes here. */
49000 auto_suppress_location_wrappers sentinel;
49001 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
49002 /*check_dependency_p=*/true,
49003 /*template_p=*/NULL,
49004 /*declarator_p=*/false,
49005 /*optional_p=*/false);
49006 vec<tree, va_gc> *args;
49007 if (fn_name == error_mark_node
49008 || cp_parser_error_occurred (parser)
49009 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
49010 || ((args = cp_parser_parenthesized_expression_list
49011 (parser, non_attr, /*cast_p=*/false,
49012 /*allow_expansion_p=*/true,
49013 /*non_constant_p=*/NULL)),
49014 cp_parser_error_occurred (parser)))
49016 finish_omp_structured_block (block);
49017 cp_parser_abort_tentative_parse (parser);
49018 cp_parser_error (parser, "expected id-expression (arguments)");
49019 return false;
49021 unsigned int i;
49022 tree arg;
49023 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
49024 if (arg == omp_priv
49025 || (TREE_CODE (arg) == ADDR_EXPR
49026 && TREE_OPERAND (arg, 0) == omp_priv))
49027 break;
49028 cp_parser_abort_tentative_parse (parser);
49029 if (arg == NULL_TREE)
49030 error ("one of the initializer call arguments should be %<omp_priv%>"
49031 " or %<&omp_priv%>");
49032 initializer = cp_parser_postfix_expression (parser, false, false, false,
49033 false, NULL);
49034 finish_expr_stmt (initializer);
49037 block = finish_omp_structured_block (block);
49038 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
49039 if (processing_template_decl)
49040 block = build_stmt (input_location, EXPR_STMT, block);
49041 add_stmt (block);
49043 if (ctor)
49044 add_decl_expr (omp_orig);
49046 if (!parens.require_close (parser))
49047 return false;
49050 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
49051 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
49052 UNKNOWN_LOCATION);
49054 return true;
49057 /* OpenMP 4.0
49058 #pragma omp declare reduction (reduction-id : typename-list : expression) \
49059 initializer-clause[opt] new-line
49061 initializer-clause:
49062 initializer (omp_priv initializer)
49063 initializer (function-name (argument-list)) */
49065 static void
49066 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
49067 enum pragma_context)
49069 auto_vec<tree> types;
49070 enum tree_code reduc_code = ERROR_MARK;
49071 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
49072 unsigned int i;
49073 cp_token *first_token;
49074 cp_token_cache *cp;
49075 int errs;
49076 void *p;
49078 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
49079 p = obstack_alloc (&declarator_obstack, 0);
49081 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
49082 goto fail;
49084 switch (cp_lexer_peek_token (parser->lexer)->type)
49086 case CPP_PLUS:
49087 reduc_code = PLUS_EXPR;
49088 break;
49089 case CPP_MULT:
49090 reduc_code = MULT_EXPR;
49091 break;
49092 case CPP_MINUS:
49093 reduc_code = MINUS_EXPR;
49094 break;
49095 case CPP_AND:
49096 reduc_code = BIT_AND_EXPR;
49097 break;
49098 case CPP_XOR:
49099 reduc_code = BIT_XOR_EXPR;
49100 break;
49101 case CPP_OR:
49102 reduc_code = BIT_IOR_EXPR;
49103 break;
49104 case CPP_AND_AND:
49105 reduc_code = TRUTH_ANDIF_EXPR;
49106 break;
49107 case CPP_OR_OR:
49108 reduc_code = TRUTH_ORIF_EXPR;
49109 break;
49110 case CPP_NAME:
49111 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
49112 break;
49113 default:
49114 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
49115 "%<|%>, %<&&%>, %<||%> or identifier");
49116 goto fail;
49119 if (reduc_code != ERROR_MARK)
49120 cp_lexer_consume_token (parser->lexer);
49122 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
49123 if (reduc_id == error_mark_node)
49124 goto fail;
49126 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
49127 goto fail;
49129 /* Types may not be defined in declare reduction type list. */
49130 const char *saved_message;
49131 saved_message = parser->type_definition_forbidden_message;
49132 parser->type_definition_forbidden_message
49133 = G_("types may not be defined in declare reduction type list");
49134 bool saved_colon_corrects_to_scope_p;
49135 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
49136 parser->colon_corrects_to_scope_p = false;
49137 bool saved_colon_doesnt_start_class_def_p;
49138 saved_colon_doesnt_start_class_def_p
49139 = parser->colon_doesnt_start_class_def_p;
49140 parser->colon_doesnt_start_class_def_p = true;
49142 while (true)
49144 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
49145 type = cp_parser_type_id (parser);
49146 if (type == error_mark_node)
49148 else if (ARITHMETIC_TYPE_P (type)
49149 && (orig_reduc_id == NULL_TREE
49150 || (TREE_CODE (type) != COMPLEX_TYPE
49151 && (id_equal (orig_reduc_id, "min")
49152 || id_equal (orig_reduc_id, "max")))))
49153 error_at (loc, "predeclared arithmetic type %qT in "
49154 "%<#pragma omp declare reduction%>", type);
49155 else if (FUNC_OR_METHOD_TYPE_P (type)
49156 || TREE_CODE (type) == ARRAY_TYPE)
49157 error_at (loc, "function or array type %qT in "
49158 "%<#pragma omp declare reduction%>", type);
49159 else if (TYPE_REF_P (type))
49160 error_at (loc, "reference type %qT in "
49161 "%<#pragma omp declare reduction%>", type);
49162 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
49163 error_at (loc, "%<const%>, %<volatile%> or %<__restrict%>-qualified "
49164 "type %qT in %<#pragma omp declare reduction%>", type);
49165 else
49166 types.safe_push (type);
49168 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
49169 cp_lexer_consume_token (parser->lexer);
49170 else
49171 break;
49174 /* Restore the saved message. */
49175 parser->type_definition_forbidden_message = saved_message;
49176 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
49177 parser->colon_doesnt_start_class_def_p
49178 = saved_colon_doesnt_start_class_def_p;
49180 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
49181 || types.is_empty ())
49183 fail:
49184 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
49185 goto done;
49188 first_token = cp_lexer_peek_token (parser->lexer);
49189 cp = NULL;
49190 errs = errorcount;
49191 FOR_EACH_VEC_ELT (types, i, type)
49193 tree fntype
49194 = build_function_type_list (void_type_node,
49195 cp_build_reference_type (type, false),
49196 NULL_TREE);
49197 tree this_reduc_id = reduc_id;
49198 if (!dependent_type_p (type))
49199 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
49200 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
49201 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
49202 DECL_ARTIFICIAL (fndecl) = 1;
49203 DECL_EXTERNAL (fndecl) = 1;
49204 DECL_DECLARED_INLINE_P (fndecl) = 1;
49205 DECL_IGNORED_P (fndecl) = 1;
49206 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
49207 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
49208 DECL_ATTRIBUTES (fndecl)
49209 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
49210 DECL_ATTRIBUTES (fndecl));
49211 bool block_scope = false;
49212 if (current_function_decl)
49214 block_scope = true;
49215 DECL_CONTEXT (fndecl) = current_function_decl;
49216 DECL_LOCAL_DECL_P (fndecl) = true;
49219 if (processing_template_decl)
49220 fndecl = push_template_decl (fndecl);
49222 if (block_scope)
49224 if (!processing_template_decl)
49225 pushdecl (fndecl);
49227 else if (current_class_type)
49229 if (cp == NULL)
49231 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
49232 cp_lexer_consume_token (parser->lexer);
49233 cp = cp_token_cache_new (first_token,
49234 cp_lexer_peek_nth_token (parser->lexer,
49235 2));
49237 DECL_STATIC_FUNCTION_P (fndecl) = 1;
49238 finish_member_declaration (fndecl);
49239 DECL_PENDING_INLINE_INFO (fndecl) = cp;
49240 DECL_PENDING_INLINE_P (fndecl) = 1;
49241 vec_safe_push (unparsed_funs_with_definitions, fndecl);
49242 continue;
49244 else
49246 DECL_CONTEXT (fndecl) = current_namespace;
49247 tree d = pushdecl (fndecl);
49248 /* We should never meet a matched duplicate decl. */
49249 gcc_checking_assert (d == error_mark_node || d == fndecl);
49252 tree block = NULL_TREE;
49253 if (!block_scope)
49254 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
49255 else
49256 block = begin_omp_structured_block ();
49257 if (cp)
49259 cp_parser_push_lexer_for_tokens (parser, cp);
49260 parser->lexer->in_pragma = true;
49263 bool ok = cp_parser_omp_declare_reduction_exprs (fndecl, parser);
49265 if (cp)
49266 cp_parser_pop_lexer (parser);
49267 if (!block_scope)
49268 finish_function (/*inline_p=*/false);
49269 else
49271 DECL_CONTEXT (fndecl) = current_function_decl;
49272 if (DECL_TEMPLATE_INFO (fndecl))
49273 DECL_CONTEXT (DECL_TI_TEMPLATE (fndecl)) = current_function_decl;
49275 if (!ok)
49276 goto fail;
49278 if (block_scope)
49280 block = finish_omp_structured_block (block);
49281 if (TREE_CODE (block) == BIND_EXPR)
49282 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
49283 else if (TREE_CODE (block) == STATEMENT_LIST)
49284 DECL_SAVED_TREE (fndecl) = block;
49285 if (processing_template_decl)
49286 add_decl_expr (fndecl);
49289 cp_check_omp_declare_reduction (fndecl);
49290 if (cp == NULL && types.length () > 1)
49291 cp = cp_token_cache_new (first_token,
49292 cp_lexer_peek_nth_token (parser->lexer, 2));
49293 if (errs != errorcount)
49294 break;
49297 cp_parser_require_pragma_eol (parser, pragma_tok);
49299 done:
49300 /* Free any declarators allocated. */
49301 obstack_free (&declarator_obstack, p);
49304 /* OpenMP 4.0
49305 #pragma omp declare simd declare-simd-clauses[optseq] new-line
49306 #pragma omp declare reduction (reduction-id : typename-list : expression) \
49307 initializer-clause[opt] new-line
49308 #pragma omp declare target new-line
49310 OpenMP 5.0
49311 #pragma omp declare variant (identifier) match (context-selector) */
49313 static bool
49314 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
49315 enum pragma_context context)
49317 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
49319 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
49320 const char *p = IDENTIFIER_POINTER (id);
49322 if (strcmp (p, "simd") == 0)
49324 cp_lexer_consume_token (parser->lexer);
49325 cp_parser_omp_declare_simd (parser, pragma_tok,
49326 context, false);
49327 return true;
49329 if (flag_openmp && strcmp (p, "variant") == 0)
49331 cp_lexer_consume_token (parser->lexer);
49332 cp_parser_omp_declare_simd (parser, pragma_tok,
49333 context, true);
49334 return true;
49336 cp_ensure_no_omp_declare_simd (parser);
49337 if (strcmp (p, "reduction") == 0)
49339 cp_lexer_consume_token (parser->lexer);
49340 cp_parser_omp_declare_reduction (parser, pragma_tok,
49341 context);
49342 return false;
49344 if (!flag_openmp) /* flag_openmp_simd */
49346 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
49347 return false;
49349 if (strcmp (p, "target") == 0)
49351 cp_lexer_consume_token (parser->lexer);
49352 cp_parser_omp_declare_target (parser, pragma_tok);
49353 return false;
49356 cp_parser_error (parser, "expected %<simd%>, %<reduction%>, "
49357 "%<target%> or %<variant%>");
49358 cp_parser_require_pragma_eol (parser, pragma_tok);
49359 return false;
49362 /* OpenMP 5.0
49363 #pragma omp requires clauses[optseq] new-line */
49365 static bool
49366 cp_parser_omp_requires (cp_parser *parser, cp_token *pragma_tok)
49368 enum omp_requires new_req = (enum omp_requires) 0;
49370 location_t loc = pragma_tok->location;
49371 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
49373 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
49374 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
49375 cp_lexer_consume_token (parser->lexer);
49377 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
49379 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
49380 const char *p = IDENTIFIER_POINTER (id);
49381 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
49382 enum omp_requires this_req = (enum omp_requires) 0;
49384 if (!strcmp (p, "unified_address"))
49385 this_req = OMP_REQUIRES_UNIFIED_ADDRESS;
49386 else if (!strcmp (p, "unified_shared_memory"))
49387 this_req = OMP_REQUIRES_UNIFIED_SHARED_MEMORY;
49388 else if (!strcmp (p, "dynamic_allocators"))
49389 this_req = OMP_REQUIRES_DYNAMIC_ALLOCATORS;
49390 else if (!strcmp (p, "reverse_offload"))
49391 this_req = OMP_REQUIRES_REVERSE_OFFLOAD;
49392 else if (!strcmp (p, "atomic_default_mem_order"))
49394 cp_lexer_consume_token (parser->lexer);
49396 matching_parens parens;
49397 if (parens.require_open (parser))
49399 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
49401 id = cp_lexer_peek_token (parser->lexer)->u.value;
49402 p = IDENTIFIER_POINTER (id);
49404 if (!strcmp (p, "seq_cst"))
49405 this_req
49406 = (enum omp_requires) OMP_MEMORY_ORDER_SEQ_CST;
49407 else if (!strcmp (p, "relaxed"))
49408 this_req
49409 = (enum omp_requires) OMP_MEMORY_ORDER_RELAXED;
49410 else if (!strcmp (p, "release"))
49411 this_req
49412 = (enum omp_requires) OMP_MEMORY_ORDER_RELEASE;
49413 else if (!strcmp (p, "acq_rel"))
49414 this_req
49415 = (enum omp_requires) OMP_MEMORY_ORDER_ACQ_REL;
49416 else if (!strcmp (p, "acquire"))
49417 this_req
49418 = (enum omp_requires) OMP_MEMORY_ORDER_ACQUIRE;
49420 if (this_req == 0)
49422 error_at (cp_lexer_peek_token (parser->lexer)->location,
49423 "expected %<acq_rel%>, %<acquire%>, "
49424 "%<relaxed%>, %<release%> or %<seq_cst%>");
49425 switch (cp_lexer_peek_token (parser->lexer)->type)
49427 case CPP_EOF:
49428 case CPP_PRAGMA_EOL:
49429 case CPP_CLOSE_PAREN:
49430 break;
49431 default:
49432 if (cp_lexer_nth_token_is (parser->lexer, 2,
49433 CPP_CLOSE_PAREN))
49434 cp_lexer_consume_token (parser->lexer);
49435 break;
49438 else
49439 cp_lexer_consume_token (parser->lexer);
49441 if (!parens.require_close (parser))
49442 cp_parser_skip_to_closing_parenthesis (parser,
49443 /*recovering=*/true,
49444 /*or_comma=*/false,
49445 /*consume_paren=*/
49446 true);
49448 if (this_req == 0)
49450 cp_parser_require_pragma_eol (parser, pragma_tok);
49451 return false;
49454 p = NULL;
49456 else
49458 error_at (cloc, "expected %<unified_address%>, "
49459 "%<unified_shared_memory%>, "
49460 "%<dynamic_allocators%>, "
49461 "%<reverse_offload%> "
49462 "or %<atomic_default_mem_order%> clause");
49463 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
49464 return false;
49466 if (p)
49467 cp_lexer_consume_token (parser->lexer);
49468 if (this_req)
49470 if ((this_req & ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
49472 if ((this_req & new_req) != 0)
49473 error_at (cloc, "too many %qs clauses", p);
49474 if (this_req != OMP_REQUIRES_DYNAMIC_ALLOCATORS
49475 && (omp_requires_mask & OMP_REQUIRES_TARGET_USED) != 0)
49476 error_at (cloc, "%qs clause used lexically after first "
49477 "target construct or offloading API", p);
49479 else if ((new_req & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
49481 error_at (cloc, "too many %qs clauses",
49482 "atomic_default_mem_order");
49483 this_req = (enum omp_requires) 0;
49485 else if ((omp_requires_mask
49486 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
49488 error_at (cloc, "more than one %<atomic_default_mem_order%>"
49489 " clause in a single compilation unit");
49490 this_req
49491 = (enum omp_requires)
49492 (omp_requires_mask
49493 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER);
49495 else if ((omp_requires_mask
49496 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED) != 0)
49497 error_at (cloc, "%<atomic_default_mem_order%> clause used "
49498 "lexically after first %<atomic%> construct "
49499 "without memory order clause");
49500 new_req = (enum omp_requires) (new_req | this_req);
49501 omp_requires_mask
49502 = (enum omp_requires) (omp_requires_mask | this_req);
49503 continue;
49506 break;
49508 cp_parser_require_pragma_eol (parser, pragma_tok);
49510 if (new_req == 0)
49511 error_at (loc, "%<pragma omp requires%> requires at least one clause");
49512 return false;
49516 /* OpenMP 5.1:
49517 #pragma omp nothing new-line */
49519 static void
49520 cp_parser_omp_nothing (cp_parser *parser, cp_token *pragma_tok)
49522 cp_parser_require_pragma_eol (parser, pragma_tok);
49526 /* OpenMP 5.1
49527 #pragma omp error clauses[optseq] new-line */
49529 static bool
49530 cp_parser_omp_error (cp_parser *parser, cp_token *pragma_tok,
49531 enum pragma_context context)
49533 int at_compilation = -1;
49534 int severity_fatal = -1;
49535 tree message = NULL_TREE;
49536 bool bad = false;
49537 location_t loc = pragma_tok->location;
49539 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
49541 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
49542 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
49543 cp_lexer_consume_token (parser->lexer);
49545 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
49546 break;
49548 const char *p
49549 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
49550 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
49551 static const char *args[] = {
49552 "execution", "compilation", "warning", "fatal"
49554 int *v = NULL;
49555 int idx = 0, n = -1;
49556 tree m = NULL_TREE;
49558 if (!strcmp (p, "at"))
49559 v = &at_compilation;
49560 else if (!strcmp (p, "severity"))
49562 v = &severity_fatal;
49563 idx += 2;
49565 else if (strcmp (p, "message"))
49567 error_at (cloc,
49568 "expected %<at%>, %<severity%> or %<message%> clause");
49569 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
49570 return false;
49573 cp_lexer_consume_token (parser->lexer);
49575 matching_parens parens;
49576 if (parens.require_open (parser))
49578 if (v == NULL)
49580 m = cp_parser_assignment_expression (parser);
49581 if (type_dependent_expression_p (m))
49582 m = build1 (IMPLICIT_CONV_EXPR, const_string_type_node, m);
49583 else
49584 m = perform_implicit_conversion_flags (const_string_type_node, m,
49585 tf_warning_or_error,
49586 LOOKUP_NORMAL);
49588 else
49590 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
49592 tree val = cp_lexer_peek_token (parser->lexer)->u.value;
49593 const char *q = IDENTIFIER_POINTER (val);
49595 if (!strcmp (q, args[idx]))
49596 n = 0;
49597 else if (!strcmp (q, args[idx + 1]))
49598 n = 1;
49600 if (n == -1)
49602 error_at (cp_lexer_peek_token (parser->lexer)->location,
49603 "expected %qs or %qs", args[idx], args[idx + 1]);
49604 bad = true;
49605 switch (cp_lexer_peek_token (parser->lexer)->type)
49607 case CPP_EOF:
49608 case CPP_PRAGMA_EOL:
49609 case CPP_CLOSE_PAREN:
49610 break;
49611 default:
49612 if (cp_lexer_nth_token_is (parser->lexer, 2,
49613 CPP_CLOSE_PAREN))
49614 cp_lexer_consume_token (parser->lexer);
49615 break;
49618 else
49619 cp_lexer_consume_token (parser->lexer);
49622 if (!parens.require_close (parser))
49623 cp_parser_skip_to_closing_parenthesis (parser,
49624 /*recovering=*/true,
49625 /*or_comma=*/false,
49626 /*consume_paren=*/
49627 true);
49629 if (v == NULL)
49631 if (message)
49633 error_at (cloc, "too many %qs clauses", p);
49634 bad = true;
49636 else
49637 message = m;
49639 else if (n != -1)
49641 if (*v != -1)
49643 error_at (cloc, "too many %qs clauses", p);
49644 bad = true;
49646 else
49647 *v = n;
49650 else
49651 bad = true;
49653 cp_parser_require_pragma_eol (parser, pragma_tok);
49654 if (bad)
49655 return true;
49657 if (at_compilation == -1)
49658 at_compilation = 1;
49659 if (severity_fatal == -1)
49660 severity_fatal = 1;
49661 if (!at_compilation)
49663 if (context != pragma_compound)
49665 error_at (loc, "%<#pragma omp error%> with %<at(execution)%> clause "
49666 "may only be used in compound statements");
49667 return true;
49669 tree fndecl
49670 = builtin_decl_explicit (severity_fatal ? BUILT_IN_GOMP_ERROR
49671 : BUILT_IN_GOMP_WARNING);
49672 if (!message)
49673 message = build_zero_cst (const_string_type_node);
49674 tree stmt = build_call_expr_loc (loc, fndecl, 2, message,
49675 build_all_ones_cst (size_type_node));
49676 add_stmt (stmt);
49677 return true;
49680 if (in_discarded_stmt)
49681 return false;
49683 const char *msg = NULL;
49684 if (message)
49686 msg = c_getstr (fold_for_warn (message));
49687 if (msg == NULL)
49688 msg = _("<message unknown at compile time>");
49690 if (msg)
49691 emit_diagnostic (severity_fatal ? DK_ERROR : DK_WARNING, loc, 0,
49692 "%<pragma omp error%> encountered: %s", msg);
49693 else
49694 emit_diagnostic (severity_fatal ? DK_ERROR : DK_WARNING, loc, 0,
49695 "%<pragma omp error%> encountered");
49696 return false;
49699 /* OpenMP 4.5:
49700 #pragma omp taskloop taskloop-clause[optseq] new-line
49701 for-loop
49703 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
49704 for-loop */
49706 #define OMP_TASKLOOP_CLAUSE_MASK \
49707 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
49708 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
49709 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
49710 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
49711 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
49712 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
49713 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
49714 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
49715 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
49716 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
49717 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
49718 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
49719 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
49720 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
49721 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
49722 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
49723 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
49725 static tree
49726 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
49727 char *p_name, omp_clause_mask mask, tree *cclauses,
49728 bool *if_p)
49730 tree clauses, sb, ret;
49731 unsigned int save;
49732 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
49734 strcat (p_name, " taskloop");
49735 mask |= OMP_TASKLOOP_CLAUSE_MASK;
49736 /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
49737 clause. */
49738 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)) != 0)
49739 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION);
49741 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
49743 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
49744 const char *p = IDENTIFIER_POINTER (id);
49746 if (strcmp (p, "simd") == 0)
49748 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
49749 if (cclauses == NULL)
49750 cclauses = cclauses_buf;
49752 cp_lexer_consume_token (parser->lexer);
49753 if (!flag_openmp) /* flag_openmp_simd */
49754 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
49755 cclauses, if_p);
49756 sb = begin_omp_structured_block ();
49757 save = cp_parser_begin_omp_structured_block (parser);
49758 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
49759 cclauses, if_p);
49760 cp_parser_end_omp_structured_block (parser, save);
49761 tree body = finish_omp_structured_block (sb);
49762 if (ret == NULL)
49763 return ret;
49764 ret = make_node (OMP_TASKLOOP);
49765 TREE_TYPE (ret) = void_type_node;
49766 OMP_FOR_BODY (ret) = body;
49767 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
49768 SET_EXPR_LOCATION (ret, loc);
49769 add_stmt (ret);
49770 return ret;
49773 if (!flag_openmp) /* flag_openmp_simd */
49775 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
49776 return NULL_TREE;
49779 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
49780 cclauses == NULL);
49781 if (cclauses)
49783 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
49784 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
49787 keep_next_level (true);
49788 sb = begin_omp_structured_block ();
49789 save = cp_parser_begin_omp_structured_block (parser);
49791 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
49792 if_p);
49794 cp_parser_end_omp_structured_block (parser, save);
49795 add_stmt (finish_omp_structured_block (sb));
49797 return ret;
49801 /* OpenACC 2.0:
49802 # pragma acc routine oacc-routine-clause[optseq] new-line
49803 function-definition
49805 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
49808 #define OACC_ROUTINE_CLAUSE_MASK \
49809 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
49810 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
49811 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
49812 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
49813 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NOHOST) )
49815 /* Parse the OpenACC routine pragma. This has an optional '( name )'
49816 component, which must resolve to a declared namespace-scope
49817 function. The clauses are either processed directly (for a named
49818 function), or defered until the immediatley following declaration
49819 is parsed. */
49821 static void
49822 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
49823 enum pragma_context context)
49825 gcc_checking_assert (context == pragma_external);
49826 /* The checking for "another pragma following this one" in the "no optional
49827 '( name )'" case makes sure that we dont re-enter. */
49828 gcc_checking_assert (parser->oacc_routine == NULL);
49830 cp_oacc_routine_data data;
49831 data.error_seen = false;
49832 data.fndecl_seen = false;
49833 data.tokens = vNULL;
49834 data.clauses = NULL_TREE;
49835 data.loc = pragma_tok->location;
49836 /* It is safe to take the address of a local variable; it will only be
49837 used while this scope is live. */
49838 parser->oacc_routine = &data;
49840 /* Look for optional '( name )'. */
49841 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
49843 matching_parens parens;
49844 parens.consume_open (parser); /* '(' */
49846 /* We parse the name as an id-expression. If it resolves to
49847 anything other than a non-overloaded function at namespace
49848 scope, it's an error. */
49849 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
49850 tree name = cp_parser_id_expression (parser,
49851 /*template_keyword_p=*/false,
49852 /*check_dependency_p=*/false,
49853 /*template_p=*/NULL,
49854 /*declarator_p=*/false,
49855 /*optional_p=*/false);
49856 tree decl = (identifier_p (name)
49857 ? cp_parser_lookup_name_simple (parser, name, name_loc)
49858 : name);
49859 if (name != error_mark_node && decl == error_mark_node)
49860 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
49862 if (decl == error_mark_node
49863 || !parens.require_close (parser))
49865 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
49866 parser->oacc_routine = NULL;
49867 return;
49870 data.clauses
49871 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
49872 "#pragma acc routine",
49873 cp_lexer_peek_token (parser->lexer));
49874 /* The clauses are in reverse order; fix that to make later diagnostic
49875 emission easier. */
49876 data.clauses = nreverse (data.clauses);
49878 if (decl && is_overloaded_fn (decl)
49879 && (TREE_CODE (decl) != FUNCTION_DECL
49880 || DECL_FUNCTION_TEMPLATE_P (decl)))
49882 error_at (name_loc,
49883 "%<#pragma acc routine%> names a set of overloads");
49884 parser->oacc_routine = NULL;
49885 return;
49888 /* Perhaps we should use the same rule as declarations in different
49889 namespaces? */
49890 if (!DECL_NAMESPACE_SCOPE_P (decl))
49892 error_at (name_loc,
49893 "%qD does not refer to a namespace scope function", decl);
49894 parser->oacc_routine = NULL;
49895 return;
49898 if (TREE_CODE (decl) != FUNCTION_DECL)
49900 error_at (name_loc, "%qD does not refer to a function", decl);
49901 parser->oacc_routine = NULL;
49902 return;
49905 cp_finalize_oacc_routine (parser, decl, false);
49906 parser->oacc_routine = NULL;
49908 else /* No optional '( name )'. */
49910 /* Store away all pragma tokens. */
49911 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
49912 cp_lexer_consume_token (parser->lexer);
49913 cp_parser_require_pragma_eol (parser, pragma_tok);
49914 struct cp_token_cache *cp
49915 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
49916 parser->oacc_routine->tokens.safe_push (cp);
49918 /* Emit a helpful diagnostic if there's another pragma following this
49919 one. */
49920 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
49922 cp_ensure_no_oacc_routine (parser);
49923 data.tokens.release ();
49924 /* ..., and then just keep going. */
49925 return;
49928 /* We only have to consider the pragma_external case here. */
49929 cp_parser_declaration (parser, NULL_TREE);
49930 if (parser->oacc_routine
49931 && !parser->oacc_routine->fndecl_seen)
49932 cp_ensure_no_oacc_routine (parser);
49933 else
49934 parser->oacc_routine = NULL;
49935 data.tokens.release ();
49939 /* Finalize #pragma acc routine clauses after direct declarator has
49940 been parsed. */
49942 static tree
49943 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
49945 struct cp_token_cache *ce;
49946 cp_oacc_routine_data *data = parser->oacc_routine;
49948 if (!data->error_seen && data->fndecl_seen)
49950 error_at (data->loc,
49951 "%<#pragma acc routine%> not immediately followed by "
49952 "a single function declaration or definition");
49953 data->error_seen = true;
49955 if (data->error_seen)
49956 return attrs;
49958 gcc_checking_assert (data->tokens.length () == 1);
49959 ce = data->tokens[0];
49961 cp_parser_push_lexer_for_tokens (parser, ce);
49962 parser->lexer->in_pragma = true;
49963 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
49965 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
49966 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
49967 parser->oacc_routine->clauses
49968 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
49969 "#pragma acc routine", pragma_tok);
49970 /* The clauses are in reverse order; fix that to make later diagnostic
49971 emission easier. */
49972 parser->oacc_routine->clauses = nreverse (parser->oacc_routine->clauses);
49973 cp_parser_pop_lexer (parser);
49974 /* Later, cp_finalize_oacc_routine will process the clauses. */
49975 parser->oacc_routine->fndecl_seen = true;
49977 return attrs;
49980 /* Apply any saved OpenACC routine clauses to a just-parsed
49981 declaration. */
49983 static void
49984 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
49986 if (UNLIKELY (parser->oacc_routine != NULL))
49988 /* Keep going if we're in error reporting mode. */
49989 if (parser->oacc_routine->error_seen
49990 || fndecl == error_mark_node)
49991 return;
49993 if (TREE_CODE (fndecl) != FUNCTION_DECL)
49995 if (parser->oacc_routine->fndecl_seen)
49997 error_at (parser->oacc_routine->loc,
49998 "%<#pragma acc routine%> not immediately followed by"
49999 " a single function declaration or definition");
50000 parser->oacc_routine = NULL;
50001 return;
50004 cp_ensure_no_oacc_routine (parser);
50005 return;
50008 int compatible
50009 = oacc_verify_routine_clauses (fndecl, &parser->oacc_routine->clauses,
50010 parser->oacc_routine->loc,
50011 "#pragma acc routine");
50012 if (compatible < 0)
50014 parser->oacc_routine = NULL;
50015 return;
50017 if (compatible > 0)
50020 else
50022 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
50024 error_at (parser->oacc_routine->loc,
50025 TREE_USED (fndecl)
50026 ? G_("%<#pragma acc routine%> must be applied before"
50027 " use")
50028 : G_("%<#pragma acc routine%> must be applied before"
50029 " definition"));
50030 parser->oacc_routine = NULL;
50031 return;
50034 /* Set the routine's level of parallelism. */
50035 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
50036 oacc_replace_fn_attrib (fndecl, dims);
50038 /* Add an "omp declare target" attribute. */
50039 DECL_ATTRIBUTES (fndecl)
50040 = tree_cons (get_identifier ("omp declare target"),
50041 parser->oacc_routine->clauses,
50042 DECL_ATTRIBUTES (fndecl));
50047 /* Main entry point to OpenMP statement pragmas. */
50049 static void
50050 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
50052 tree stmt;
50053 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
50054 omp_clause_mask mask (0);
50056 switch (cp_parser_pragma_kind (pragma_tok))
50058 case PRAGMA_OACC_ATOMIC:
50059 cp_parser_omp_atomic (parser, pragma_tok, true);
50060 return;
50061 case PRAGMA_OACC_CACHE:
50062 stmt = cp_parser_oacc_cache (parser, pragma_tok);
50063 break;
50064 case PRAGMA_OACC_DATA:
50065 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
50066 break;
50067 case PRAGMA_OACC_ENTER_DATA:
50068 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
50069 break;
50070 case PRAGMA_OACC_EXIT_DATA:
50071 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
50072 break;
50073 case PRAGMA_OACC_HOST_DATA:
50074 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
50075 break;
50076 case PRAGMA_OACC_KERNELS:
50077 case PRAGMA_OACC_PARALLEL:
50078 case PRAGMA_OACC_SERIAL:
50079 strcpy (p_name, "#pragma acc");
50080 stmt = cp_parser_oacc_compute (parser, pragma_tok, p_name, if_p);
50081 break;
50082 case PRAGMA_OACC_LOOP:
50083 strcpy (p_name, "#pragma acc");
50084 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
50085 if_p);
50086 break;
50087 case PRAGMA_OACC_UPDATE:
50088 stmt = cp_parser_oacc_update (parser, pragma_tok);
50089 break;
50090 case PRAGMA_OACC_WAIT:
50091 stmt = cp_parser_oacc_wait (parser, pragma_tok);
50092 break;
50093 case PRAGMA_OMP_ALLOCATE:
50094 cp_parser_omp_allocate (parser, pragma_tok);
50095 return;
50096 case PRAGMA_OMP_ATOMIC:
50097 cp_parser_omp_atomic (parser, pragma_tok, false);
50098 return;
50099 case PRAGMA_OMP_CRITICAL:
50100 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
50101 break;
50102 case PRAGMA_OMP_DISTRIBUTE:
50103 strcpy (p_name, "#pragma omp");
50104 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
50105 if_p);
50106 break;
50107 case PRAGMA_OMP_FOR:
50108 strcpy (p_name, "#pragma omp");
50109 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
50110 if_p);
50111 break;
50112 case PRAGMA_OMP_LOOP:
50113 strcpy (p_name, "#pragma omp");
50114 stmt = cp_parser_omp_loop (parser, pragma_tok, p_name, mask, NULL,
50115 if_p);
50116 break;
50117 case PRAGMA_OMP_MASKED:
50118 strcpy (p_name, "#pragma omp");
50119 stmt = cp_parser_omp_masked (parser, pragma_tok, p_name, mask, NULL,
50120 if_p);
50121 break;
50122 case PRAGMA_OMP_MASTER:
50123 strcpy (p_name, "#pragma omp");
50124 stmt = cp_parser_omp_master (parser, pragma_tok, p_name, mask, NULL,
50125 if_p);
50126 break;
50127 case PRAGMA_OMP_PARALLEL:
50128 strcpy (p_name, "#pragma omp");
50129 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
50130 if_p);
50131 break;
50132 case PRAGMA_OMP_SCOPE:
50133 stmt = cp_parser_omp_scope (parser, pragma_tok, if_p);
50134 break;
50135 case PRAGMA_OMP_SECTIONS:
50136 strcpy (p_name, "#pragma omp");
50137 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
50138 break;
50139 case PRAGMA_OMP_SIMD:
50140 strcpy (p_name, "#pragma omp");
50141 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
50142 if_p);
50143 break;
50144 case PRAGMA_OMP_SINGLE:
50145 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
50146 break;
50147 case PRAGMA_OMP_TASK:
50148 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
50149 break;
50150 case PRAGMA_OMP_TASKGROUP:
50151 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
50152 break;
50153 case PRAGMA_OMP_TASKLOOP:
50154 strcpy (p_name, "#pragma omp");
50155 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
50156 if_p);
50157 break;
50158 case PRAGMA_OMP_TEAMS:
50159 strcpy (p_name, "#pragma omp");
50160 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
50161 if_p);
50162 break;
50163 case PRAGMA_OMP_ASSUME:
50164 cp_parser_omp_assume (parser, pragma_tok, if_p);
50165 return;
50166 default:
50167 gcc_unreachable ();
50170 protected_set_expr_location (stmt, pragma_tok->location);
50173 /* Transactional Memory parsing routines. */
50175 /* Parse a transaction attribute.
50177 txn-attribute:
50178 attribute
50179 [ [ identifier ] ]
50181 We use this instead of cp_parser_attributes_opt for transactions to avoid
50182 the pedwarn in C++98 mode. */
50184 static tree
50185 cp_parser_txn_attribute_opt (cp_parser *parser)
50187 cp_token *token;
50188 tree attr_name, attr = NULL;
50190 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
50191 return cp_parser_attributes_opt (parser);
50193 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
50194 return NULL_TREE;
50195 cp_lexer_consume_token (parser->lexer);
50196 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
50197 goto error1;
50199 token = cp_lexer_peek_token (parser->lexer);
50200 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
50202 token = cp_lexer_consume_token (parser->lexer);
50204 attr_name = (token->type == CPP_KEYWORD
50205 /* For keywords, use the canonical spelling,
50206 not the parsed identifier. */
50207 ? ridpointers[(int) token->keyword]
50208 : token->u.value);
50209 attr = build_tree_list (attr_name, NULL_TREE);
50211 else
50212 cp_parser_error (parser, "expected identifier");
50214 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
50215 error1:
50216 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
50217 return attr;
50220 /* Parse a __transaction_atomic or __transaction_relaxed statement.
50222 transaction-statement:
50223 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
50224 compound-statement
50225 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
50228 static tree
50229 cp_parser_transaction (cp_parser *parser, cp_token *token)
50231 unsigned char old_in = parser->in_transaction;
50232 unsigned char this_in = 1, new_in;
50233 enum rid keyword = token->keyword;
50234 tree stmt, attrs, noex;
50236 cp_lexer_consume_token (parser->lexer);
50238 if (keyword == RID_TRANSACTION_RELAXED
50239 || keyword == RID_SYNCHRONIZED)
50240 this_in |= TM_STMT_ATTR_RELAXED;
50241 else
50243 attrs = cp_parser_txn_attribute_opt (parser);
50244 if (attrs)
50245 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
50248 /* Parse a noexcept specification. */
50249 if (keyword == RID_ATOMIC_NOEXCEPT)
50250 noex = boolean_true_node;
50251 else if (keyword == RID_ATOMIC_CANCEL)
50253 /* cancel-and-throw is unimplemented. */
50254 sorry ("%<atomic_cancel%>");
50255 noex = NULL_TREE;
50257 else
50258 noex = cp_parser_noexcept_specification_opt (parser,
50259 CP_PARSER_FLAGS_NONE,
50260 /*require_constexpr=*/true,
50261 /*consumed_expr=*/NULL,
50262 /*return_cond=*/true);
50264 /* Keep track if we're in the lexical scope of an outer transaction. */
50265 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
50267 stmt = begin_transaction_stmt (token->location, NULL, this_in);
50269 parser->in_transaction = new_in;
50270 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
50271 parser->in_transaction = old_in;
50273 finish_transaction_stmt (stmt, NULL, this_in, noex);
50275 return stmt;
50278 /* Parse a __transaction_atomic or __transaction_relaxed expression.
50280 transaction-expression:
50281 __transaction_atomic txn-noexcept-spec[opt] ( expression )
50282 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
50285 static tree
50286 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
50288 unsigned char old_in = parser->in_transaction;
50289 unsigned char this_in = 1;
50290 cp_token *token;
50291 tree expr, noex;
50292 bool noex_expr;
50293 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
50295 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
50296 || keyword == RID_TRANSACTION_RELAXED);
50298 if (!flag_tm)
50299 error_at (loc,
50300 keyword == RID_TRANSACTION_RELAXED
50301 ? G_("%<__transaction_relaxed%> without transactional memory "
50302 "support enabled")
50303 : G_("%<__transaction_atomic%> without transactional memory "
50304 "support enabled"));
50306 token = cp_parser_require_keyword (parser, keyword,
50307 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
50308 : RT_TRANSACTION_RELAXED));
50309 gcc_assert (token != NULL);
50311 if (keyword == RID_TRANSACTION_RELAXED)
50312 this_in |= TM_STMT_ATTR_RELAXED;
50314 /* Set this early. This might mean that we allow transaction_cancel in
50315 an expression that we find out later actually has to be a constexpr.
50316 However, we expect that cxx_constant_value will be able to deal with
50317 this; also, if the noexcept has no constexpr, then what we parse next
50318 really is a transaction's body. */
50319 parser->in_transaction = this_in;
50321 /* Parse a noexcept specification. */
50322 noex = cp_parser_noexcept_specification_opt (parser,
50323 CP_PARSER_FLAGS_NONE,
50324 /*require_constexpr=*/false,
50325 &noex_expr,
50326 /*return_cond=*/true);
50328 if (!noex || !noex_expr
50329 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
50331 matching_parens parens;
50332 parens.require_open (parser);
50334 expr = cp_parser_expression (parser);
50335 expr = finish_parenthesized_expr (expr);
50337 parens.require_close (parser);
50339 else
50341 /* The only expression that is available got parsed for the noexcept
50342 already. noexcept is true then. */
50343 expr = noex;
50344 noex = boolean_true_node;
50347 expr = build_transaction_expr (token->location, expr, this_in, noex);
50348 parser->in_transaction = old_in;
50350 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
50351 return error_mark_node;
50353 return (flag_tm ? expr : error_mark_node);
50356 /* Parse a function-transaction-block.
50358 function-transaction-block:
50359 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
50360 function-body
50361 __transaction_atomic txn-attribute[opt] function-try-block
50362 __transaction_relaxed ctor-initializer[opt] function-body
50363 __transaction_relaxed function-try-block
50366 static void
50367 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
50369 unsigned char old_in = parser->in_transaction;
50370 unsigned char new_in = 1;
50371 tree compound_stmt, stmt, attrs;
50372 cp_token *token;
50374 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
50375 || keyword == RID_TRANSACTION_RELAXED);
50376 token = cp_parser_require_keyword (parser, keyword,
50377 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
50378 : RT_TRANSACTION_RELAXED));
50379 gcc_assert (token != NULL);
50381 if (keyword == RID_TRANSACTION_RELAXED)
50382 new_in |= TM_STMT_ATTR_RELAXED;
50383 else
50385 attrs = cp_parser_txn_attribute_opt (parser);
50386 if (attrs)
50387 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
50390 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
50392 parser->in_transaction = new_in;
50394 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
50395 cp_parser_function_try_block (parser);
50396 else
50397 cp_parser_ctor_initializer_opt_and_function_body
50398 (parser, /*in_function_try_block=*/false);
50400 parser->in_transaction = old_in;
50402 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
50405 /* Parse a __transaction_cancel statement.
50407 cancel-statement:
50408 __transaction_cancel txn-attribute[opt] ;
50409 __transaction_cancel txn-attribute[opt] throw-expression ;
50411 ??? Cancel and throw is not yet implemented. */
50413 static tree
50414 cp_parser_transaction_cancel (cp_parser *parser)
50416 cp_token *token;
50417 bool is_outer = false;
50418 tree stmt, attrs;
50420 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
50421 RT_TRANSACTION_CANCEL);
50422 gcc_assert (token != NULL);
50424 attrs = cp_parser_txn_attribute_opt (parser);
50425 if (attrs)
50426 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
50428 /* ??? Parse cancel-and-throw here. */
50430 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
50432 if (!flag_tm)
50434 error_at (token->location, "%<__transaction_cancel%> without "
50435 "transactional memory support enabled");
50436 return error_mark_node;
50438 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
50440 error_at (token->location, "%<__transaction_cancel%> within a "
50441 "%<__transaction_relaxed%>");
50442 return error_mark_node;
50444 else if (is_outer)
50446 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
50447 && !is_tm_may_cancel_outer (current_function_decl))
50449 error_at (token->location, "outer %<__transaction_cancel%> not "
50450 "within outer %<__transaction_atomic%>");
50451 error_at (token->location,
50452 " or a %<transaction_may_cancel_outer%> function");
50453 return error_mark_node;
50456 else if (parser->in_transaction == 0)
50458 error_at (token->location, "%<__transaction_cancel%> not within "
50459 "%<__transaction_atomic%>");
50460 return error_mark_node;
50463 stmt = build_tm_abort_call (token->location, is_outer);
50464 add_stmt (stmt);
50466 return stmt;
50470 /* Special handling for the first token or line in the file. The first
50471 thing in the file might be #pragma GCC pch_preprocess, which loads a
50472 PCH file, which is a GC collection point. So we need to handle this
50473 first pragma without benefit of an existing lexer structure.
50475 Always returns one token to the caller in *FIRST_TOKEN. This is
50476 either the true first token of the file, or the first token after
50477 the initial pragma. */
50479 static void
50480 cp_parser_initial_pragma (cp_token *first_token)
50482 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
50483 return;
50485 cp_lexer_get_preprocessor_token (0, first_token);
50487 tree name = NULL;
50488 if (first_token->type == CPP_STRING)
50490 name = first_token->u.value;
50492 cp_lexer_get_preprocessor_token (0, first_token);
50495 /* Skip to the end of the pragma. */
50496 if (first_token->type != CPP_PRAGMA_EOL)
50498 error_at (first_token->location,
50499 "malformed %<#pragma GCC pch_preprocess%>");
50501 cp_lexer_get_preprocessor_token (0, first_token);
50502 while (first_token->type != CPP_PRAGMA_EOL);
50505 /* Now actually load the PCH file. */
50506 if (name)
50507 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
50509 /* Read one more token to return to our caller. We have to do this
50510 after reading the PCH file in, since its pointers have to be
50511 live. */
50512 cp_lexer_get_preprocessor_token (0, first_token);
50515 /* Parse a pragma GCC ivdep. */
50517 static bool
50518 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
50520 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
50521 return true;
50524 /* Parse a pragma GCC unroll. */
50526 static tree
50527 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
50529 location_t location = cp_lexer_peek_token (parser->lexer)->location;
50530 tree unroll = cp_parser_constant_expression (parser);
50531 unroll = cp_check_pragma_unroll (location, fold_non_dependent_expr (unroll));
50532 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
50533 return unroll;
50536 /* Parse a pragma GCC novector. */
50538 static bool
50539 cp_parser_pragma_novector (cp_parser *parser, cp_token *pragma_tok)
50541 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
50542 return true;
50545 /* Normal parsing of a pragma token. Here we can (and must) use the
50546 regular lexer. */
50548 static bool
50549 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
50551 cp_token *pragma_tok;
50552 unsigned int id;
50553 tree stmt;
50554 bool ret = false;
50556 pragma_tok = cp_lexer_consume_token (parser->lexer);
50557 gcc_assert (pragma_tok->type == CPP_PRAGMA);
50558 parser->lexer->in_pragma = true;
50560 id = cp_parser_pragma_kind (pragma_tok);
50561 if (parser->omp_for_parse_state
50562 && parser->omp_for_parse_state->in_intervening_code
50563 && id >= PRAGMA_OMP__START_
50564 && id <= PRAGMA_OMP__LAST_)
50566 error_at (pragma_tok->location,
50567 "intervening code must not contain OpenMP directives");
50568 parser->omp_for_parse_state->fail = true;
50569 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
50570 return false;
50572 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
50573 cp_ensure_no_omp_declare_simd (parser);
50574 switch (id)
50576 case PRAGMA_GCC_PCH_PREPROCESS:
50577 error_at (pragma_tok->location,
50578 "%<#pragma GCC pch_preprocess%> must be first");
50579 break;
50581 case PRAGMA_OMP_BARRIER:
50582 switch (context)
50584 case pragma_compound:
50585 cp_parser_omp_barrier (parser, pragma_tok);
50586 return false;
50587 case pragma_stmt:
50588 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
50589 "used in compound statements", "omp barrier");
50590 ret = true;
50591 break;
50592 default:
50593 goto bad_stmt;
50595 break;
50597 case PRAGMA_OMP_DEPOBJ:
50598 switch (context)
50600 case pragma_compound:
50601 cp_parser_omp_depobj (parser, pragma_tok);
50602 return false;
50603 case pragma_stmt:
50604 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
50605 "used in compound statements", "omp depobj");
50606 ret = true;
50607 break;
50608 default:
50609 goto bad_stmt;
50611 break;
50613 case PRAGMA_OMP_FLUSH:
50614 switch (context)
50616 case pragma_compound:
50617 cp_parser_omp_flush (parser, pragma_tok);
50618 return false;
50619 case pragma_stmt:
50620 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
50621 "used in compound statements", "omp flush");
50622 ret = true;
50623 break;
50624 default:
50625 goto bad_stmt;
50627 break;
50629 case PRAGMA_OMP_TASKWAIT:
50630 switch (context)
50632 case pragma_compound:
50633 cp_parser_omp_taskwait (parser, pragma_tok);
50634 return false;
50635 case pragma_stmt:
50636 error_at (pragma_tok->location,
50637 "%<#pragma %s%> may only be used in compound statements",
50638 "omp taskwait");
50639 ret = true;
50640 break;
50641 default:
50642 goto bad_stmt;
50644 break;
50646 case PRAGMA_OMP_TASKYIELD:
50647 switch (context)
50649 case pragma_compound:
50650 cp_parser_omp_taskyield (parser, pragma_tok);
50651 return false;
50652 case pragma_stmt:
50653 error_at (pragma_tok->location,
50654 "%<#pragma %s%> may only be used in compound statements",
50655 "omp taskyield");
50656 ret = true;
50657 break;
50658 default:
50659 goto bad_stmt;
50661 break;
50663 case PRAGMA_OMP_CANCEL:
50664 switch (context)
50666 case pragma_compound:
50667 cp_parser_omp_cancel (parser, pragma_tok);
50668 return false;
50669 case pragma_stmt:
50670 error_at (pragma_tok->location,
50671 "%<#pragma %s%> may only be used in compound statements",
50672 "omp cancel");
50673 ret = true;
50674 break;
50675 default:
50676 goto bad_stmt;
50678 break;
50680 case PRAGMA_OMP_CANCELLATION_POINT:
50681 return cp_parser_omp_cancellation_point (parser, pragma_tok, context);
50683 case PRAGMA_OMP_THREADPRIVATE:
50684 cp_parser_omp_threadprivate (parser, pragma_tok);
50685 return false;
50687 case PRAGMA_OMP_DECLARE:
50688 return cp_parser_omp_declare (parser, pragma_tok, context);
50690 case PRAGMA_OACC_DECLARE:
50691 cp_parser_oacc_declare (parser, pragma_tok);
50692 return false;
50694 case PRAGMA_OACC_ENTER_DATA:
50695 if (context == pragma_stmt)
50697 error_at (pragma_tok->location,
50698 "%<#pragma %s%> may only be used in compound statements",
50699 "acc enter data");
50700 ret = true;
50701 break;
50703 else if (context != pragma_compound)
50704 goto bad_stmt;
50705 cp_parser_omp_construct (parser, pragma_tok, if_p);
50706 return true;
50708 case PRAGMA_OACC_EXIT_DATA:
50709 if (context == pragma_stmt)
50711 error_at (pragma_tok->location,
50712 "%<#pragma %s%> may only be used in compound statements",
50713 "acc exit data");
50714 ret = true;
50715 break;
50717 else if (context != pragma_compound)
50718 goto bad_stmt;
50719 cp_parser_omp_construct (parser, pragma_tok, if_p);
50720 return true;
50722 case PRAGMA_OACC_ROUTINE:
50723 if (context != pragma_external)
50725 error_at (pragma_tok->location,
50726 "%<#pragma acc routine%> must be at file scope");
50727 ret = true;
50728 break;
50730 cp_parser_oacc_routine (parser, pragma_tok, context);
50731 return false;
50733 case PRAGMA_OACC_UPDATE:
50734 if (context == pragma_stmt)
50736 error_at (pragma_tok->location,
50737 "%<#pragma %s%> may only be used in compound statements",
50738 "acc update");
50739 ret = true;
50740 break;
50742 else if (context != pragma_compound)
50743 goto bad_stmt;
50744 cp_parser_omp_construct (parser, pragma_tok, if_p);
50745 return true;
50747 case PRAGMA_OACC_WAIT:
50748 if (context == pragma_stmt)
50750 error_at (pragma_tok->location,
50751 "%<#pragma %s%> may only be used in compound statements",
50752 "acc wait");
50753 ret = true;
50754 break;
50756 else if (context != pragma_compound)
50757 goto bad_stmt;
50758 cp_parser_omp_construct (parser, pragma_tok, if_p);
50759 return true;
50760 case PRAGMA_OMP_ALLOCATE:
50761 cp_parser_omp_allocate (parser, pragma_tok);
50762 return false;
50763 case PRAGMA_OACC_ATOMIC:
50764 case PRAGMA_OACC_CACHE:
50765 case PRAGMA_OACC_DATA:
50766 case PRAGMA_OACC_HOST_DATA:
50767 case PRAGMA_OACC_KERNELS:
50768 case PRAGMA_OACC_LOOP:
50769 case PRAGMA_OACC_PARALLEL:
50770 case PRAGMA_OACC_SERIAL:
50771 case PRAGMA_OMP_ASSUME:
50772 case PRAGMA_OMP_ATOMIC:
50773 case PRAGMA_OMP_CRITICAL:
50774 case PRAGMA_OMP_DISTRIBUTE:
50775 case PRAGMA_OMP_FOR:
50776 case PRAGMA_OMP_LOOP:
50777 case PRAGMA_OMP_MASKED:
50778 case PRAGMA_OMP_MASTER:
50779 case PRAGMA_OMP_PARALLEL:
50780 case PRAGMA_OMP_SCOPE:
50781 case PRAGMA_OMP_SECTIONS:
50782 case PRAGMA_OMP_SIMD:
50783 case PRAGMA_OMP_SINGLE:
50784 case PRAGMA_OMP_TASK:
50785 case PRAGMA_OMP_TASKGROUP:
50786 case PRAGMA_OMP_TASKLOOP:
50787 case PRAGMA_OMP_TEAMS:
50788 if (context != pragma_stmt && context != pragma_compound)
50789 goto bad_stmt;
50790 stmt = push_omp_privatization_clauses (false);
50791 cp_parser_omp_construct (parser, pragma_tok, if_p);
50792 pop_omp_privatization_clauses (stmt);
50793 return true;
50795 case PRAGMA_OMP_REQUIRES:
50796 if (context != pragma_external)
50798 error_at (pragma_tok->location,
50799 "%<#pragma omp requires%> may only be used at file or "
50800 "namespace scope");
50801 ret = true;
50802 break;
50804 return cp_parser_omp_requires (parser, pragma_tok);
50806 case PRAGMA_OMP_ASSUMES:
50807 if (context != pragma_external)
50809 error_at (pragma_tok->location,
50810 "%<#pragma omp assumes%> may only be used at file or "
50811 "namespace scope");
50812 ret = true;
50813 break;
50815 return cp_parser_omp_assumes (parser, pragma_tok);
50817 case PRAGMA_OMP_NOTHING:
50818 cp_parser_omp_nothing (parser, pragma_tok);
50819 return false;
50821 case PRAGMA_OMP_ERROR:
50822 return cp_parser_omp_error (parser, pragma_tok, context);
50824 case PRAGMA_OMP_ORDERED:
50825 if (context != pragma_stmt && context != pragma_compound)
50826 goto bad_stmt;
50827 stmt = push_omp_privatization_clauses (false);
50828 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
50829 pop_omp_privatization_clauses (stmt);
50830 return ret;
50832 case PRAGMA_OMP_TARGET:
50833 if (context != pragma_stmt && context != pragma_compound)
50834 goto bad_stmt;
50835 stmt = push_omp_privatization_clauses (false);
50836 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
50837 pop_omp_privatization_clauses (stmt);
50838 return ret;
50840 case PRAGMA_OMP_BEGIN:
50841 cp_parser_omp_begin (parser, pragma_tok);
50842 return false;
50844 case PRAGMA_OMP_END:
50845 cp_parser_omp_end (parser, pragma_tok);
50846 return false;
50848 case PRAGMA_OMP_SCAN:
50849 error_at (pragma_tok->location,
50850 "%<#pragma omp scan%> may only be used in "
50851 "a loop construct with %<inscan%> %<reduction%> clause");
50852 break;
50854 case PRAGMA_OMP_SECTION:
50855 error_at (pragma_tok->location,
50856 "%<#pragma omp section%> may only be used in "
50857 "%<#pragma omp sections%> construct");
50858 break;
50860 case PRAGMA_IVDEP:
50861 case PRAGMA_UNROLL:
50862 case PRAGMA_NOVECTOR:
50864 bool ivdep;
50865 tree unroll = NULL_TREE;
50866 bool novector = false;
50867 const char *pragma_str;
50869 switch (id)
50871 case PRAGMA_IVDEP:
50872 pragma_str = "ivdep";
50873 break;
50874 case PRAGMA_UNROLL:
50875 pragma_str = "unroll";
50876 break;
50877 case PRAGMA_NOVECTOR:
50878 pragma_str = "novector";
50879 break;
50880 default:
50881 gcc_unreachable ();
50884 if (context == pragma_external)
50886 error_at (pragma_tok->location,
50887 "%<#pragma GCC %s%> must be inside a function",
50888 pragma_str);
50889 break;
50892 cp_token *tok = pragma_tok;
50893 bool has_more = true;
50896 switch (cp_parser_pragma_kind (tok))
50898 case PRAGMA_IVDEP:
50900 if (tok != pragma_tok)
50901 tok = cp_lexer_consume_token (parser->lexer);
50902 ivdep = cp_parser_pragma_ivdep (parser, tok);
50903 break;
50905 case PRAGMA_UNROLL:
50907 if (tok != pragma_tok)
50908 tok = cp_lexer_consume_token (parser->lexer);
50909 unroll = cp_parser_pragma_unroll (parser, tok);
50910 break;
50912 case PRAGMA_NOVECTOR:
50914 if (tok != pragma_tok)
50915 tok = cp_lexer_consume_token (parser->lexer);
50916 novector = cp_parser_pragma_novector (parser, tok);
50917 break;
50919 default:
50920 has_more = false;
50921 break;
50923 tok = cp_lexer_peek_token (the_parser->lexer);
50924 has_more = has_more && tok->type == CPP_PRAGMA;
50926 while (has_more);
50928 if (tok->type != CPP_KEYWORD
50929 || (tok->keyword != RID_FOR
50930 && tok->keyword != RID_WHILE
50931 && tok->keyword != RID_DO))
50933 cp_parser_error (parser, "for, while or do statement expected");
50934 return false;
50936 cp_parser_iteration_statement (parser, if_p, ivdep, unroll, novector);
50937 return true;
50940 default:
50941 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
50942 c_invoke_pragma_handler (id);
50943 break;
50945 bad_stmt:
50946 cp_parser_error (parser, "expected declaration specifiers");
50947 break;
50950 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
50951 return ret;
50954 /* Helper for pragma_lex in preprocess-only mode; in this mode, we have not
50955 populated the lexer with any tokens (the tokens rather being read by
50956 c-ppoutput.c's machinery), so we need to read enough tokens now to handle
50957 a pragma. */
50958 static void
50959 maybe_read_tokens_for_pragma_lex ()
50961 const auto lexer = the_parser->lexer;
50962 if (!lexer->buffer->is_empty ())
50963 return;
50965 /* Read the rest of the tokens comprising the pragma line. */
50966 cp_token *tok;
50969 tok = vec_safe_push (lexer->buffer, cp_token ());
50970 cp_lexer_get_preprocessor_token (C_LEX_STRING_NO_JOIN, tok);
50971 gcc_assert (tok->type != CPP_EOF);
50972 } while (tok->type != CPP_PRAGMA_EOL);
50973 lexer->next_token = lexer->buffer->address ();
50974 lexer->last_token = lexer->next_token + lexer->buffer->length () - 1;
50977 /* The interface the pragma parsers have to the lexer. */
50979 enum cpp_ttype
50980 pragma_lex (tree *value, location_t *loc)
50982 if (flag_preprocess_only)
50983 maybe_read_tokens_for_pragma_lex ();
50985 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
50986 enum cpp_ttype ret = tok->type;
50988 *value = tok->u.value;
50989 if (loc)
50990 *loc = tok->location;
50992 if (ret == CPP_PRAGMA_EOL)
50993 ret = CPP_EOF;
50994 else if (ret == CPP_STRING)
50995 *value = cp_parser_string_literal (the_parser, /*translate=*/false,
50996 /*wide_ok=*/false);
50997 else
50999 if (ret == CPP_KEYWORD)
51000 ret = CPP_NAME;
51001 cp_lexer_consume_token (the_parser->lexer);
51004 return ret;
51007 void
51008 pragma_lex_discard_to_eol ()
51010 /* We have already read all the tokens, so we just need to discard
51011 them here. */
51012 const auto lexer = the_parser->lexer;
51013 lexer->next_token = lexer->last_token;
51014 lexer->buffer->truncate (0);
51018 /* External interface. */
51020 /* Parse one entire translation unit. */
51022 void
51023 c_parse_file (void)
51025 static bool already_called = false;
51027 if (already_called)
51028 fatal_error (input_location,
51029 "multi-source compilation not implemented for C++");
51030 already_called = true;
51032 /* cp_lexer_new_main is called before doing any GC allocation
51033 because tokenization might load a PCH file. */
51034 cp_lexer_new_main ();
51036 cp_parser_translation_unit (the_parser);
51037 class_decl_loc_t::diag_mismatched_tags ();
51039 the_parser = NULL;
51041 finish_translation_unit ();
51044 /* Create an identifier for a generic parameter type (a synthesized
51045 template parameter implied by `auto' or a concept identifier). */
51047 static GTY(()) int generic_parm_count;
51048 static tree
51049 make_generic_type_name ()
51051 char buf[32];
51052 sprintf (buf, "auto:%d", ++generic_parm_count);
51053 return get_identifier (buf);
51056 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
51057 (creating a new template parameter list if necessary). Returns the newly
51058 created template type parm. */
51060 static tree
51061 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
51063 /* A requires-clause is not a function and cannot have placeholders. */
51064 if (current_binding_level->requires_expression)
51066 error ("placeholder type not allowed in this context");
51067 return error_mark_node;
51070 gcc_assert (current_binding_level->kind == sk_function_parms);
51072 /* We are either continuing a function template that already contains implicit
51073 template parameters, creating a new fully-implicit function template, or
51074 extending an existing explicit function template with implicit template
51075 parameters. */
51077 cp_binding_level *const entry_scope = current_binding_level;
51079 bool become_template = false;
51080 cp_binding_level *parent_scope = 0;
51082 if (parser->implicit_template_scope)
51084 gcc_assert (parser->implicit_template_parms);
51086 current_binding_level = parser->implicit_template_scope;
51088 else
51090 /* Roll back to the existing template parameter scope (in the case of
51091 extending an explicit function template) or introduce a new template
51092 parameter scope ahead of the function parameter scope (or class scope
51093 in the case of out-of-line member definitions). The function scope is
51094 added back after template parameter synthesis below. */
51096 cp_binding_level *scope = entry_scope;
51098 while (scope->kind == sk_function_parms)
51100 parent_scope = scope;
51101 scope = scope->level_chain;
51103 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
51105 /* If not defining a class, then any class scope is a scope level in
51106 an out-of-line member definition. In this case simply wind back
51107 beyond the first such scope to inject the template parameter list.
51108 Otherwise wind back to the class being defined. The latter can
51109 occur in class member friend declarations such as:
51111 class A {
51112 void foo (auto);
51114 class B {
51115 friend void A::foo (auto);
51118 The template parameter list synthesized for the friend declaration
51119 must be injected in the scope of 'B'. This can also occur in
51120 erroneous cases such as:
51122 struct A {
51123 struct B {
51124 void foo (auto);
51126 void B::foo (auto) {}
51129 Here the attempted definition of 'B::foo' within 'A' is ill-formed
51130 but, nevertheless, the template parameter list synthesized for the
51131 declarator should be injected into the scope of 'A' as if the
51132 ill-formed template was specified explicitly. */
51134 while (scope->kind == sk_class && !scope->defining_class_p)
51136 parent_scope = scope;
51137 scope = scope->level_chain;
51141 current_binding_level = scope;
51143 if (scope->kind != sk_template_parms
51144 || !function_being_declared_is_template_p (parser))
51146 /* Introduce a new template parameter list for implicit template
51147 parameters. */
51149 become_template = true;
51151 parser->implicit_template_scope
51152 = begin_scope (sk_template_parms, NULL);
51154 ++processing_template_decl;
51156 parser->fully_implicit_function_template_p = true;
51157 ++parser->num_template_parameter_lists;
51159 else
51161 /* Synthesize implicit template parameters at the end of the explicit
51162 template parameter list. */
51164 gcc_assert (current_template_parms);
51166 parser->implicit_template_scope = scope;
51168 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
51169 parser->implicit_template_parms
51170 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
51174 /* Synthesize a new template parameter and track the current template
51175 parameter chain with implicit_template_parms. */
51177 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
51178 tree synth_id = make_generic_type_name ();
51179 bool non_type = false;
51181 /* Synthesize the type template parameter. */
51182 gcc_assert(!proto || TREE_CODE (proto) == TYPE_DECL);
51183 tree synth_tmpl_parm = finish_template_type_parm (class_type_node, synth_id);
51185 if (become_template)
51186 current_template_parms = tree_cons (size_int (current_template_depth + 1),
51187 NULL_TREE, current_template_parms);
51189 /* Attach the constraint to the parm before processing. */
51190 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
51191 TREE_TYPE (node) = constr;
51192 tree new_parm
51193 = process_template_parm (parser->implicit_template_parms,
51194 input_location,
51195 node,
51196 /*non_type=*/non_type,
51197 /*param_pack=*/false);
51198 // Process_template_parm returns the list of parms, and
51199 // parser->implicit_template_parms holds the final node of the parm
51200 // list. We really want to manipulate the newly appended element.
51201 gcc_checking_assert (!parser->implicit_template_parms
51202 || parser->implicit_template_parms == new_parm);
51203 if (parser->implicit_template_parms)
51204 new_parm = TREE_CHAIN (new_parm);
51205 gcc_checking_assert (!TREE_CHAIN (new_parm));
51207 // Record the last implicit parm node
51208 parser->implicit_template_parms = new_parm;
51210 /* Mark the synthetic declaration "virtual". This is used when
51211 comparing template-heads to determine if whether an abbreviated
51212 function template is equivalent to an explicit template.
51214 Note that DECL_ARTIFICIAL is used elsewhere for template
51215 parameters. */
51216 if (TREE_VALUE (new_parm) != error_mark_node)
51217 DECL_IMPLICIT_TEMPLATE_PARM_P (TREE_VALUE (new_parm)) = true;
51219 tree new_decl = get_local_decls ();
51220 if (non_type)
51221 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
51222 new_decl = DECL_INITIAL (new_decl);
51224 /* If creating a fully implicit function template, start the new implicit
51225 template parameter list with this synthesized type, otherwise grow the
51226 current template parameter list. */
51228 if (become_template)
51230 parent_scope->level_chain = current_binding_level;
51232 tree new_parms = make_tree_vec (1);
51233 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
51234 TREE_VALUE (current_template_parms) = new_parms;
51236 else
51238 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
51239 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
51240 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
51241 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
51244 /* If the new parameter was constrained, we need to add that to the
51245 constraints in the template parameter list. */
51246 if (tree req = TEMPLATE_PARM_CONSTRAINTS (new_parm))
51248 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
51249 reqs = combine_constraint_expressions (reqs, req);
51250 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
51253 current_binding_level = entry_scope;
51255 return new_decl;
51258 /* Finish the declaration of a fully implicit function template. Such a
51259 template has no explicit template parameter list so has not been through the
51260 normal template head and tail processing. synthesize_implicit_template_parm
51261 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
51262 provided if the declaration is a class member such that its template
51263 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
51264 form is returned. Otherwise NULL_TREE is returned. */
51266 static tree
51267 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
51269 gcc_assert (parser->fully_implicit_function_template_p);
51271 if (member_decl_opt && member_decl_opt != error_mark_node
51272 && DECL_VIRTUAL_P (member_decl_opt))
51274 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
51275 "implicit templates may not be %<virtual%>");
51276 DECL_VIRTUAL_P (member_decl_opt) = false;
51279 if (member_decl_opt)
51280 member_decl_opt = finish_member_template_decl (member_decl_opt);
51281 end_template_decl ();
51283 parser->fully_implicit_function_template_p = false;
51284 parser->implicit_template_parms = 0;
51285 parser->implicit_template_scope = 0;
51286 --parser->num_template_parameter_lists;
51288 return member_decl_opt;
51291 /* Like finish_fully_implicit_template, but to be used in error
51292 recovery, rearranging scopes so that we restore the state we had
51293 before synthesize_implicit_template_parm inserted the implement
51294 template parms scope. */
51296 static void
51297 abort_fully_implicit_template (cp_parser *parser)
51299 cp_binding_level *return_to_scope = current_binding_level;
51301 if (parser->implicit_template_scope
51302 && return_to_scope != parser->implicit_template_scope)
51304 cp_binding_level *child = return_to_scope;
51305 for (cp_binding_level *scope = child->level_chain;
51306 scope != parser->implicit_template_scope;
51307 scope = child->level_chain)
51308 child = scope;
51309 child->level_chain = parser->implicit_template_scope->level_chain;
51310 parser->implicit_template_scope->level_chain = return_to_scope;
51311 current_binding_level = parser->implicit_template_scope;
51313 else
51314 return_to_scope = return_to_scope->level_chain;
51316 finish_fully_implicit_template (parser, NULL);
51318 gcc_assert (current_binding_level == return_to_scope);
51321 /* Helper function for diagnostics that have complained about things
51322 being used with 'extern "C"' linkage.
51324 Attempt to issue a note showing where the 'extern "C"' linkage began. */
51326 void
51327 maybe_show_extern_c_location (void)
51329 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
51330 inform (the_parser->innermost_linkage_specification_location,
51331 "%<extern \"C\"%> linkage started here");
51334 #include "gt-cp-parser.h"