d: Merge upstream dmd, druntime c8ae4adb2e, phobos 792c8b7c1.
[official-gcc.git] / gcc / cp / parser.cc
blob4798aae1fbb3482200d59800acbcb71f65a8da28
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2022 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"
53 /* The lexer. */
55 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
56 and c-lex.cc) and the C++ parser. */
58 /* The various kinds of non integral constant we encounter. */
59 enum non_integral_constant {
60 NIC_NONE,
61 /* floating-point literal */
62 NIC_FLOAT,
63 /* %<this%> */
64 NIC_THIS,
65 /* %<__FUNCTION__%> */
66 NIC_FUNC_NAME,
67 /* %<__PRETTY_FUNCTION__%> */
68 NIC_PRETTY_FUNC,
69 /* %<__func__%> */
70 NIC_C99_FUNC,
71 /* "%<va_arg%> */
72 NIC_VA_ARG,
73 /* a cast */
74 NIC_CAST,
75 /* %<typeid%> operator */
76 NIC_TYPEID,
77 /* non-constant compound literals */
78 NIC_NCC,
79 /* a function call */
80 NIC_FUNC_CALL,
81 /* an increment */
82 NIC_INC,
83 /* an decrement */
84 NIC_DEC,
85 /* an array reference */
86 NIC_ARRAY_REF,
87 /* %<->%> */
88 NIC_ARROW,
89 /* %<.%> */
90 NIC_POINT,
91 /* the address of a label */
92 NIC_ADDR_LABEL,
93 /* %<*%> */
94 NIC_STAR,
95 /* %<&%> */
96 NIC_ADDR,
97 /* %<++%> */
98 NIC_PREINCREMENT,
99 /* %<--%> */
100 NIC_PREDECREMENT,
101 /* %<new%> */
102 NIC_NEW,
103 /* %<delete%> */
104 NIC_DEL,
105 /* calls to overloaded operators */
106 NIC_OVERLOADED,
107 /* an assignment */
108 NIC_ASSIGNMENT,
109 /* a comma operator */
110 NIC_COMMA,
111 /* a call to a constructor */
112 NIC_CONSTRUCTOR,
113 /* a transaction expression */
114 NIC_TRANSACTION
117 /* The various kinds of errors about name-lookup failing. */
118 enum name_lookup_error {
119 /* NULL */
120 NLE_NULL,
121 /* is not a type */
122 NLE_TYPE,
123 /* is not a class or namespace */
124 NLE_CXX98,
125 /* is not a class, namespace, or enumeration */
126 NLE_NOT_CXX98
129 /* The various kinds of required token */
130 enum required_token {
131 RT_NONE,
132 RT_SEMICOLON, /* ';' */
133 RT_OPEN_PAREN, /* '(' */
134 RT_CLOSE_BRACE, /* '}' */
135 RT_OPEN_BRACE, /* '{' */
136 RT_CLOSE_SQUARE, /* ']' */
137 RT_OPEN_SQUARE, /* '[' */
138 RT_COMMA, /* ',' */
139 RT_SCOPE, /* '::' */
140 RT_LESS, /* '<' */
141 RT_GREATER, /* '>' */
142 RT_EQ, /* '=' */
143 RT_ELLIPSIS, /* '...' */
144 RT_MULT, /* '*' */
145 RT_COMPL, /* '~' */
146 RT_COLON, /* ':' */
147 RT_COLON_SCOPE, /* ':' or '::' */
148 RT_CLOSE_PAREN, /* ')' */
149 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
150 RT_PRAGMA_EOL, /* end of line */
151 RT_NAME, /* identifier */
153 /* The type is CPP_KEYWORD */
154 RT_NEW, /* new */
155 RT_DELETE, /* delete */
156 RT_RETURN, /* return */
157 RT_WHILE, /* while */
158 RT_EXTERN, /* extern */
159 RT_STATIC_ASSERT, /* static_assert */
160 RT_DECLTYPE, /* decltype */
161 RT_OPERATOR, /* operator */
162 RT_CLASS, /* class */
163 RT_TEMPLATE, /* template */
164 RT_NAMESPACE, /* namespace */
165 RT_USING, /* using */
166 RT_ASM, /* asm */
167 RT_TRY, /* try */
168 RT_CATCH, /* catch */
169 RT_THROW, /* throw */
170 RT_AUTO, /* auto */
171 RT_LABEL, /* __label__ */
172 RT_AT_TRY, /* @try */
173 RT_AT_SYNCHRONIZED, /* @synchronized */
174 RT_AT_THROW, /* @throw */
176 RT_SELECT, /* selection-statement */
177 RT_ITERATION, /* iteration-statement */
178 RT_JUMP, /* jump-statement */
179 RT_CLASS_KEY, /* class-key */
180 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
181 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
182 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
183 RT_TRANSACTION_CANCEL, /* __transaction_cancel */
185 RT_CO_YIELD /* co_yield */
188 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
189 reverting it on destruction. */
191 class type_id_in_expr_sentinel
193 cp_parser *parser;
194 bool saved;
195 public:
196 type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
197 : parser (parser),
198 saved (parser->in_type_id_in_expr_p)
199 { parser->in_type_id_in_expr_p = set; }
200 ~type_id_in_expr_sentinel ()
201 { parser->in_type_id_in_expr_p = saved; }
204 /* Prototypes. */
206 static cp_lexer *cp_lexer_new_main
207 (void);
208 static cp_lexer *cp_lexer_new_from_tokens
209 (cp_token_cache *tokens);
210 static void cp_lexer_destroy
211 (cp_lexer *);
212 static int cp_lexer_saving_tokens
213 (const cp_lexer *);
214 static cp_token *cp_lexer_token_at
215 (cp_lexer *, cp_token_position);
216 static void cp_lexer_get_preprocessor_token
217 (unsigned, cp_token *);
218 static inline cp_token *cp_lexer_peek_token
219 (cp_lexer *);
220 static cp_token *cp_lexer_peek_nth_token
221 (cp_lexer *, size_t);
222 static inline bool cp_lexer_next_token_is
223 (cp_lexer *, enum cpp_ttype);
224 static bool cp_lexer_next_token_is_not
225 (cp_lexer *, enum cpp_ttype);
226 static bool cp_lexer_next_token_is_keyword
227 (cp_lexer *, enum rid);
228 static cp_token *cp_lexer_consume_token
229 (cp_lexer *);
230 static void cp_lexer_purge_token
231 (cp_lexer *);
232 static void cp_lexer_purge_tokens_after
233 (cp_lexer *, cp_token_position);
234 static void cp_lexer_save_tokens
235 (cp_lexer *);
236 static void cp_lexer_commit_tokens
237 (cp_lexer *);
238 static void cp_lexer_rollback_tokens
239 (cp_lexer *);
240 static void cp_lexer_print_token
241 (FILE *, cp_token *);
242 static inline bool cp_lexer_debugging_p
243 (cp_lexer *);
244 static void cp_lexer_start_debugging
245 (cp_lexer *) ATTRIBUTE_UNUSED;
246 static void cp_lexer_stop_debugging
247 (cp_lexer *) ATTRIBUTE_UNUSED;
249 static cp_token_cache *cp_token_cache_new
250 (cp_token *, cp_token *);
251 static tree cp_parser_late_noexcept_specifier
252 (cp_parser *, tree);
253 static void noexcept_override_late_checks
254 (tree, tree);
256 static void cp_parser_initial_pragma
257 (cp_token *);
259 static bool cp_parser_omp_declare_reduction_exprs
260 (tree, cp_parser *);
261 static void cp_finalize_oacc_routine
262 (cp_parser *, tree, bool);
264 /* Manifest constants. */
265 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
266 #define CP_SAVED_TOKEN_STACK 5
268 /* Variables. */
270 /* The stream to which debugging output should be written. */
271 static FILE *cp_lexer_debug_stream;
273 /* Nonzero if we are parsing an unevaluated operand: an operand to
274 sizeof, typeof, or alignof. */
275 int cp_unevaluated_operand;
277 /* Dump up to NUM tokens in BUFFER to FILE starting with token
278 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
279 first token in BUFFER. If NUM is 0, dump all the tokens. If
280 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
281 highlighted by surrounding it in [[ ]]. */
283 static void
284 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
285 cp_token *start_token, unsigned num,
286 cp_token *curr_token)
288 unsigned i, nprinted;
289 cp_token *token;
290 bool do_print;
292 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
294 if (buffer == NULL)
295 return;
297 if (num == 0)
298 num = buffer->length ();
300 if (start_token == NULL)
301 start_token = buffer->address ();
303 if (start_token > buffer->address ())
305 cp_lexer_print_token (file, &(*buffer)[0]);
306 fprintf (file, " ... ");
309 do_print = false;
310 nprinted = 0;
311 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
313 if (token == start_token)
314 do_print = true;
316 if (!do_print)
317 continue;
319 nprinted++;
320 if (token == curr_token)
321 fprintf (file, "[[");
323 cp_lexer_print_token (file, token);
325 if (token == curr_token)
326 fprintf (file, "]]");
328 switch (token->type)
330 case CPP_SEMICOLON:
331 case CPP_OPEN_BRACE:
332 case CPP_CLOSE_BRACE:
333 case CPP_EOF:
334 fputc ('\n', file);
335 break;
337 default:
338 fputc (' ', file);
342 if (i == num && i < buffer->length ())
344 fprintf (file, " ... ");
345 cp_lexer_print_token (file, &buffer->last ());
348 fprintf (file, "\n");
352 /* Dump all tokens in BUFFER to stderr. */
354 void
355 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
357 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
360 DEBUG_FUNCTION void
361 debug (vec<cp_token, va_gc> &ref)
363 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
366 DEBUG_FUNCTION void
367 debug (vec<cp_token, va_gc> *ptr)
369 if (ptr)
370 debug (*ptr);
371 else
372 fprintf (stderr, "<nil>\n");
376 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
377 description for T. */
379 static void
380 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
382 if (t)
384 fprintf (file, "%s: ", desc);
385 print_node_brief (file, "", t, 0);
390 /* Dump parser context C to FILE. */
392 static void
393 cp_debug_print_context (FILE *file, cp_parser_context *c)
395 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
396 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
397 print_node_brief (file, "", c->object_type, 0);
398 fprintf (file, "}\n");
402 /* Print the stack of parsing contexts to FILE starting with FIRST. */
404 static void
405 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
407 unsigned i;
408 cp_parser_context *c;
410 fprintf (file, "Parsing context stack:\n");
411 for (i = 0, c = first; c; c = c->next, i++)
413 fprintf (file, "\t#%u: ", i);
414 cp_debug_print_context (file, c);
419 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
421 static void
422 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
424 if (flag)
425 fprintf (file, "%s: true\n", desc);
429 /* Print an unparsed function entry UF to FILE. */
431 static void
432 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
434 unsigned i;
435 cp_default_arg_entry *default_arg_fn;
436 tree fn;
438 fprintf (file, "\tFunctions with default args:\n");
439 for (i = 0;
440 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
441 i++)
443 fprintf (file, "\t\tClass type: ");
444 print_node_brief (file, "", default_arg_fn->class_type, 0);
445 fprintf (file, "\t\tDeclaration: ");
446 print_node_brief (file, "", default_arg_fn->decl, 0);
447 fprintf (file, "\n");
450 fprintf (file, "\n\tFunctions with definitions that require "
451 "post-processing\n\t\t");
452 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
454 print_node_brief (file, "", fn, 0);
455 fprintf (file, " ");
457 fprintf (file, "\n");
459 fprintf (file, "\n\tNon-static data members with initializers that require "
460 "post-processing\n\t\t");
461 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
463 print_node_brief (file, "", fn, 0);
464 fprintf (file, " ");
466 fprintf (file, "\n");
470 /* Print the stack of unparsed member functions S to FILE. */
472 static void
473 cp_debug_print_unparsed_queues (FILE *file,
474 vec<cp_unparsed_functions_entry, va_gc> *s)
476 unsigned i;
477 cp_unparsed_functions_entry *uf;
479 fprintf (file, "Unparsed functions\n");
480 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
482 fprintf (file, "#%u:\n", i);
483 cp_debug_print_unparsed_function (file, uf);
488 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
489 the given PARSER. If FILE is NULL, the output is printed on stderr. */
491 static void
492 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
494 cp_token *next_token, *first_token, *start_token;
496 if (file == NULL)
497 file = stderr;
499 next_token = parser->lexer->next_token;
500 first_token = parser->lexer->buffer->address ();
501 start_token = (next_token > first_token + window_size / 2)
502 ? next_token - window_size / 2
503 : first_token;
504 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
505 next_token);
509 /* Dump debugging information for the given PARSER. If FILE is NULL,
510 the output is printed on stderr. */
512 void
513 cp_debug_parser (FILE *file, cp_parser *parser)
515 const size_t window_size = 20;
516 cp_token *token;
517 expanded_location eloc;
519 if (file == NULL)
520 file = stderr;
522 fprintf (file, "Parser state\n\n");
523 fprintf (file, "Number of tokens: %u\n",
524 vec_safe_length (parser->lexer->buffer));
525 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
526 cp_debug_print_tree_if_set (file, "Object scope",
527 parser->object_scope);
528 cp_debug_print_tree_if_set (file, "Qualifying scope",
529 parser->qualifying_scope);
530 cp_debug_print_context_stack (file, parser->context);
531 cp_debug_print_flag (file, "Allow GNU extensions",
532 parser->allow_gnu_extensions_p);
533 cp_debug_print_flag (file, "'>' token is greater-than",
534 parser->greater_than_is_operator_p);
535 cp_debug_print_flag (file, "Default args allowed in current "
536 "parameter list", parser->default_arg_ok_p);
537 cp_debug_print_flag (file, "Parsing integral constant-expression",
538 parser->integral_constant_expression_p);
539 cp_debug_print_flag (file, "Allow non-constant expression in current "
540 "constant-expression",
541 parser->allow_non_integral_constant_expression_p);
542 cp_debug_print_flag (file, "Seen non-constant expression",
543 parser->non_integral_constant_expression_p);
544 cp_debug_print_flag (file, "Local names forbidden in current context",
545 (parser->local_variables_forbidden_p
546 & LOCAL_VARS_FORBIDDEN));
547 cp_debug_print_flag (file, "'this' forbidden in current context",
548 (parser->local_variables_forbidden_p
549 & THIS_FORBIDDEN));
550 cp_debug_print_flag (file, "In unbraced linkage specification",
551 parser->in_unbraced_linkage_specification_p);
552 cp_debug_print_flag (file, "Parsing a declarator",
553 parser->in_declarator_p);
554 cp_debug_print_flag (file, "In template argument list",
555 parser->in_template_argument_list_p);
556 cp_debug_print_flag (file, "Parsing an iteration statement",
557 parser->in_statement & IN_ITERATION_STMT);
558 cp_debug_print_flag (file, "Parsing a switch statement",
559 parser->in_statement & IN_SWITCH_STMT);
560 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
561 parser->in_statement & IN_OMP_BLOCK);
562 cp_debug_print_flag (file, "Parsing an OpenMP loop",
563 parser->in_statement & IN_OMP_FOR);
564 cp_debug_print_flag (file, "Parsing an if statement",
565 parser->in_statement & IN_IF_STMT);
566 cp_debug_print_flag (file, "Parsing a type-id in an expression "
567 "context", parser->in_type_id_in_expr_p);
568 cp_debug_print_flag (file, "String expressions should be translated "
569 "to execution character set",
570 parser->translate_strings_p);
571 cp_debug_print_flag (file, "Parsing function body outside of a "
572 "local class", parser->in_function_body);
573 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
574 parser->colon_corrects_to_scope_p);
575 cp_debug_print_flag (file, "Colon doesn't start a class definition",
576 parser->colon_doesnt_start_class_def_p);
577 cp_debug_print_flag (file, "Parsing an Objective-C++ message context",
578 parser->objective_c_message_context_p);
579 if (parser->type_definition_forbidden_message)
580 fprintf (file, "Error message for forbidden type definitions: %s %s\n",
581 parser->type_definition_forbidden_message,
582 parser->type_definition_forbidden_message_arg
583 ? parser->type_definition_forbidden_message_arg : "<none>");
584 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
585 fprintf (file, "Number of class definitions in progress: %u\n",
586 parser->num_classes_being_defined);
587 fprintf (file, "Number of template parameter lists for the current "
588 "declaration: %u\n", parser->num_template_parameter_lists);
589 cp_debug_parser_tokens (file, parser, window_size);
590 token = parser->lexer->next_token;
591 fprintf (file, "Next token to parse:\n");
592 fprintf (file, "\tToken: ");
593 cp_lexer_print_token (file, token);
594 eloc = expand_location (token->location);
595 fprintf (file, "\n\tFile: %s\n", eloc.file);
596 fprintf (file, "\tLine: %d\n", eloc.line);
597 fprintf (file, "\tColumn: %d\n", eloc.column);
600 DEBUG_FUNCTION void
601 debug (cp_parser &ref)
603 cp_debug_parser (stderr, &ref);
606 DEBUG_FUNCTION void
607 debug (cp_parser *ptr)
609 if (ptr)
610 debug (*ptr);
611 else
612 fprintf (stderr, "<nil>\n");
615 /* Allocate memory for a new lexer object and return it. */
617 static cp_lexer *
618 cp_lexer_alloc (void)
620 /* Allocate the memory. */
621 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
623 /* Initially we are not debugging. */
624 lexer->debugging_p = false;
626 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
628 /* Create the buffer. */
629 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
631 return lexer;
634 /* Return TRUE if token is the start of a module declaration that will be
635 terminated by a CPP_PRAGMA_EOL token. */
636 static inline bool
637 cp_token_is_module_directive (cp_token *token)
639 return token->keyword == RID__EXPORT
640 || token->keyword == RID__MODULE
641 || token->keyword == RID__IMPORT;
644 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
645 PRAGMA_NONE. */
647 static enum pragma_kind
648 cp_parser_pragma_kind (cp_token *token)
650 if (token->type != CPP_PRAGMA)
651 return PRAGMA_NONE;
652 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
653 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
656 /* Handle early pragmas such as #pragma GCC diagnostic, which needs to be done
657 during preprocessing for the case of preprocessing-related diagnostics. This
658 is called immediately after pushing the CPP_PRAGMA_EOL token onto
659 lexer->buffer. */
661 static void
662 cp_lexer_handle_early_pragma (cp_lexer *lexer)
664 const auto first_token = lexer->buffer->address ();
665 const auto last_token = first_token + lexer->buffer->length () - 1;
667 /* Back up to the start of the pragma so pragma_lex () can parse it when
668 c-pragma lib asks it to. */
669 auto begin = last_token;
670 gcc_assert (begin->type == CPP_PRAGMA_EOL);
671 while (begin->type != CPP_PRAGMA)
673 if (cp_token_is_module_directive (begin))
674 return;
675 gcc_assert (begin != first_token);
676 --begin;
678 gcc_assert (!lexer->next_token);
679 gcc_assert (!lexer->last_token);
680 lexer->next_token = begin;
681 lexer->last_token = last_token;
683 /* Dispatch it. */
684 const unsigned int id
685 = cp_parser_pragma_kind (cp_lexer_consume_token (lexer));
686 if (id >= PRAGMA_FIRST_EXTERNAL)
687 c_invoke_early_pragma_handler (id);
689 /* Reset to normal state. */
690 lexer->next_token = lexer->last_token = nullptr;
693 /* The parser. */
694 static cp_parser *cp_parser_new (cp_lexer *);
695 static GTY (()) cp_parser *the_parser;
697 /* Create a new main C++ lexer, the lexer that gets tokens from the
698 preprocessor, and also create the main parser. */
700 static cp_lexer *
701 cp_lexer_new_main (void)
703 cp_token token;
705 /* It's possible that parsing the first pragma will load a PCH file,
706 which is a GC collection point. So we have to do that before
707 allocating any memory. */
708 cp_lexer_get_preprocessor_token (0, &token);
709 cp_parser_initial_pragma (&token);
710 c_common_no_more_pch ();
712 cp_lexer *lexer = cp_lexer_alloc ();
713 /* Put the first token in the buffer. */
714 cp_token *tok = lexer->buffer->quick_push (token);
716 uintptr_t filter = 0;
717 if (modules_p ())
718 filter = module_token_cdtor (parse_in, filter);
720 /* Create the parser now, so we can use it to handle early pragmas. */
721 gcc_assert (!the_parser);
722 the_parser = cp_parser_new (lexer);
724 /* Get the remaining tokens from the preprocessor. */
725 while (tok->type != CPP_EOF)
727 if (filter)
728 /* Process the previous token. */
729 module_token_lang (tok->type, tok->keyword, tok->u.value,
730 tok->location, filter);
732 /* Check for early pragmas that need to be handled now. */
733 if (tok->type == CPP_PRAGMA_EOL)
734 cp_lexer_handle_early_pragma (lexer);
736 tok = vec_safe_push (lexer->buffer, cp_token ());
737 cp_lexer_get_preprocessor_token (C_LEX_STRING_NO_JOIN, tok);
740 lexer->next_token = lexer->buffer->address ();
741 lexer->last_token = lexer->next_token
742 + lexer->buffer->length ()
743 - 1;
745 if (lexer->buffer->length () != 1)
747 /* Set the EOF token's location to be the just after the previous
748 token's range. That way 'at-eof' diagnostics point at something
749 meaninful. */
750 auto range = get_range_from_loc (line_table, tok[-1].location);
751 tok[0].location
752 = linemap_position_for_loc_and_offset (line_table, range.m_finish, 1);
755 if (filter)
756 module_token_cdtor (parse_in, filter);
758 /* Subsequent preprocessor diagnostics should use compiler
759 diagnostic functions to get the compiler source location. */
760 override_libcpp_locations = true;
762 maybe_check_all_macros (parse_in);
764 gcc_assert (!lexer->next_token->purged_p);
765 return lexer;
768 /* Create a new lexer whose token stream is primed with the tokens in
769 CACHE. When these tokens are exhausted, no new tokens will be read. */
771 static cp_lexer *
772 cp_lexer_new_from_tokens (cp_token_cache *cache)
774 cp_token *first = cache->first;
775 cp_token *last = cache->last;
776 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
778 /* We do not own the buffer. */
779 lexer->buffer = NULL;
781 /* Insert an EOF token. */
782 lexer->saved_type = last->type;
783 lexer->saved_keyword = last->keyword;
784 last->type = CPP_EOF;
785 last->keyword = RID_MAX;
787 lexer->next_token = first;
788 lexer->last_token = last;
790 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
792 /* Initially we are not debugging. */
793 lexer->debugging_p = false;
795 gcc_assert (!lexer->next_token->purged_p
796 && !lexer->last_token->purged_p);
797 return lexer;
800 /* Frees all resources associated with LEXER. */
802 static void
803 cp_lexer_destroy (cp_lexer *lexer)
805 if (lexer->buffer)
806 vec_free (lexer->buffer);
807 else
809 /* Restore the token we overwrite with EOF. */
810 lexer->last_token->type = lexer->saved_type;
811 lexer->last_token->keyword = lexer->saved_keyword;
813 lexer->saved_tokens.release ();
814 ggc_free (lexer);
817 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
818 be used. The point of this flag is to help the compiler to fold away calls
819 to cp_lexer_debugging_p within this source file at compile time, when the
820 lexer is not being debugged. */
822 #define LEXER_DEBUGGING_ENABLED_P false
824 /* Returns nonzero if debugging information should be output. */
826 static inline bool
827 cp_lexer_debugging_p (cp_lexer *lexer)
829 if (!LEXER_DEBUGGING_ENABLED_P)
830 return false;
832 return lexer->debugging_p;
836 static inline cp_token_position
837 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
839 return lexer->next_token - previous_p;
842 static inline cp_token *
843 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
845 return pos;
848 static inline void
849 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
851 lexer->next_token = cp_lexer_token_at (lexer, pos);
854 static inline cp_token_position
855 cp_lexer_previous_token_position (cp_lexer *lexer)
857 return cp_lexer_token_position (lexer, true);
860 static inline cp_token *
861 cp_lexer_previous_token (cp_lexer *lexer)
863 cp_token_position tp = cp_lexer_previous_token_position (lexer);
865 /* Skip past purged tokens. */
866 while (tp->purged_p)
868 gcc_assert (tp != vec_safe_address (lexer->buffer));
869 tp--;
872 return cp_lexer_token_at (lexer, tp);
875 /* Same as above, but return NULL when the lexer doesn't own the token
876 buffer or if the next_token is at the start of the token
877 vector or if all previous tokens are purged. */
879 static cp_token *
880 cp_lexer_safe_previous_token (cp_lexer *lexer)
882 if (lexer->buffer
883 && lexer->next_token != lexer->buffer->address ())
885 cp_token_position tp = cp_lexer_previous_token_position (lexer);
887 /* Skip past purged tokens. */
888 while (tp->purged_p)
890 if (tp == lexer->buffer->address ())
891 return NULL;
892 tp--;
894 return cp_lexer_token_at (lexer, tp);
897 return NULL;
900 /* Overload for make_location, taking the lexer to mean the location of the
901 previous token. */
903 static inline location_t
904 make_location (location_t caret, location_t start, cp_lexer *lexer)
906 cp_token *t = cp_lexer_previous_token (lexer);
907 return make_location (caret, start, t->location);
910 /* Overload for make_location taking tokens instead of locations. */
912 static inline location_t
913 make_location (cp_token *caret, cp_token *start, cp_token *end)
915 return make_location (caret->location, start->location, end->location);
918 /* nonzero if we are presently saving tokens. */
920 static inline int
921 cp_lexer_saving_tokens (const cp_lexer* lexer)
923 return lexer->saved_tokens.length () != 0;
926 /* Store the next token from the preprocessor in *TOKEN. Return true
927 if we reach EOF. If LEXER is NULL, assume we are handling an
928 initial #pragma pch_preprocess, and thus want the lexer to return
929 processed strings.
931 Diagnostics issued from this function must have their controlling option (if
932 any) in c.opt annotated as a libcpp option via the CppReason property. */
934 static void
935 cp_lexer_get_preprocessor_token (unsigned flags, cp_token *token)
937 static int is_extern_c = 0;
939 /* Get a new token from the preprocessor. */
940 token->type
941 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
942 flags);
943 token->keyword = RID_MAX;
944 token->purged_p = false;
945 token->error_reported = false;
946 token->tree_check_p = false;
947 /* Usually never see a zero, but just in case ... */
948 token->main_source_p = line_table->depth <= 1;
950 /* On some systems, some header files are surrounded by an
951 implicit extern "C" block. Set a flag in the token if it
952 comes from such a header. */
953 is_extern_c += pending_lang_change;
954 pending_lang_change = 0;
955 token->implicit_extern_c = is_extern_c > 0;
957 /* Check to see if this token is a keyword. */
958 if (token->type == CPP_NAME)
960 if (IDENTIFIER_KEYWORD_P (token->u.value))
962 /* Mark this token as a keyword. */
963 token->type = CPP_KEYWORD;
964 /* Record which keyword. */
965 token->keyword = C_RID_CODE (token->u.value);
967 else
969 if (warn_cxx11_compat
970 && ((C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
971 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
972 /* These are outside the CXX11 range. */
973 || C_RID_CODE (token->u.value) == RID_ALIGNOF
974 || C_RID_CODE (token->u.value) == RID_ALIGNAS
975 || C_RID_CODE (token->u.value)== RID_THREAD))
977 /* Warn about the C++11 keyword (but still treat it as
978 an identifier). */
979 warning_at (token->location, OPT_Wc__11_compat,
980 "identifier %qE is a keyword in C++11",
981 token->u.value);
983 /* Clear out the C_RID_CODE so we don't warn about this
984 particular identifier-turned-keyword again. */
985 C_SET_RID_CODE (token->u.value, RID_MAX);
987 if (warn_cxx20_compat
988 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX20
989 && C_RID_CODE (token->u.value) <= RID_LAST_CXX20)
991 /* Warn about the C++20 keyword (but still treat it as
992 an identifier). */
993 warning_at (token->location, OPT_Wc__20_compat,
994 "identifier %qE is a keyword in C++20",
995 token->u.value);
997 /* Clear out the C_RID_CODE so we don't warn about this
998 particular identifier-turned-keyword again. */
999 C_SET_RID_CODE (token->u.value, RID_MAX);
1002 token->keyword = RID_MAX;
1005 else if (token->type == CPP_AT_NAME)
1007 /* This only happens in Objective-C++; it must be a keyword. */
1008 token->type = CPP_KEYWORD;
1009 switch (C_RID_CODE (token->u.value))
1011 /* Replace 'class' with '@class', 'private' with '@private',
1012 etc. This prevents confusion with the C++ keyword
1013 'class', and makes the tokens consistent with other
1014 Objective-C 'AT' keywords. For example '@class' is
1015 reported as RID_AT_CLASS which is consistent with
1016 '@synchronized', which is reported as
1017 RID_AT_SYNCHRONIZED.
1019 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
1020 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
1021 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
1022 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
1023 case RID_THROW: token->keyword = RID_AT_THROW; break;
1024 case RID_TRY: token->keyword = RID_AT_TRY; break;
1025 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
1026 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
1027 default: token->keyword = C_RID_CODE (token->u.value);
1032 /* Update the globals input_location and the input file stack from TOKEN. */
1033 static inline void
1034 cp_lexer_set_source_position_from_token (cp_token *token)
1036 input_location = token->location;
1039 /* Update the globals input_location and the input file stack from LEXER. */
1040 static inline void
1041 cp_lexer_set_source_position (cp_lexer *lexer)
1043 cp_token *token = cp_lexer_peek_token (lexer);
1044 cp_lexer_set_source_position_from_token (token);
1047 /* Return a pointer to the next token in the token stream, but do not
1048 consume it. */
1050 static inline cp_token *
1051 cp_lexer_peek_token (cp_lexer *lexer)
1053 if (cp_lexer_debugging_p (lexer))
1055 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
1056 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
1057 putc ('\n', cp_lexer_debug_stream);
1059 return lexer->next_token;
1062 /* Return true if the next token has the indicated TYPE. */
1064 static inline bool
1065 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
1067 return cp_lexer_peek_token (lexer)->type == type;
1070 /* Return true if the next token does not have the indicated TYPE. */
1072 static inline bool
1073 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
1075 return !cp_lexer_next_token_is (lexer, type);
1078 /* Return true if the next token is the indicated KEYWORD. */
1080 static inline bool
1081 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
1083 return cp_lexer_peek_token (lexer)->keyword == keyword;
1086 static inline bool
1087 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
1089 return cp_lexer_peek_nth_token (lexer, n)->type == type;
1092 static inline bool
1093 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
1095 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
1098 /* Return true if KEYWORD can start a decl-specifier. */
1100 bool
1101 cp_keyword_starts_decl_specifier_p (enum rid keyword)
1103 switch (keyword)
1105 /* auto specifier: storage-class-specifier in C++,
1106 simple-type-specifier in C++0x. */
1107 case RID_AUTO:
1108 /* Storage classes. */
1109 case RID_REGISTER:
1110 case RID_STATIC:
1111 case RID_EXTERN:
1112 case RID_MUTABLE:
1113 case RID_THREAD:
1114 /* Elaborated type specifiers. */
1115 case RID_ENUM:
1116 case RID_CLASS:
1117 case RID_STRUCT:
1118 case RID_UNION:
1119 case RID_TYPENAME:
1120 /* Simple type specifiers. */
1121 case RID_CHAR:
1122 case RID_CHAR8:
1123 case RID_CHAR16:
1124 case RID_CHAR32:
1125 case RID_WCHAR:
1126 case RID_BOOL:
1127 case RID_SHORT:
1128 case RID_INT:
1129 case RID_LONG:
1130 case RID_SIGNED:
1131 case RID_UNSIGNED:
1132 case RID_FLOAT:
1133 case RID_DOUBLE:
1134 CASE_RID_FLOATN_NX:
1135 case RID_VOID:
1136 /* CV qualifiers. */
1137 case RID_CONST:
1138 case RID_VOLATILE:
1139 /* Function specifiers. */
1140 case RID_EXPLICIT:
1141 case RID_VIRTUAL:
1142 /* friend/typdef/inline specifiers. */
1143 case RID_FRIEND:
1144 case RID_TYPEDEF:
1145 case RID_INLINE:
1146 /* GNU extensions. */
1147 case RID_TYPEOF:
1148 /* C++11 extensions. */
1149 case RID_DECLTYPE:
1150 case RID_CONSTEXPR:
1151 /* C++20 extensions. */
1152 case RID_CONSTINIT:
1153 case RID_CONSTEVAL:
1154 return true;
1156 #define DEFTRAIT_TYPE(CODE, NAME, ARITY) \
1157 case RID_##CODE:
1158 #include "cp-trait.def"
1159 #undef DEFTRAIT_TYPE
1160 return true;
1162 default:
1163 if (keyword >= RID_FIRST_INT_N
1164 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
1165 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
1166 return true;
1167 return false;
1171 /* Return true if the next token is a keyword for a decl-specifier. */
1173 static bool
1174 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
1176 cp_token *token;
1178 token = cp_lexer_peek_token (lexer);
1179 return cp_keyword_starts_decl_specifier_p (token->keyword);
1182 /* Returns TRUE iff the token T begins a decltype type. */
1184 static bool
1185 token_is_decltype (cp_token *t)
1187 return (t->keyword == RID_DECLTYPE
1188 || t->type == CPP_DECLTYPE);
1191 /* Returns TRUE iff the next token begins a decltype type. */
1193 static bool
1194 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1196 cp_token *t = cp_lexer_peek_token (lexer);
1197 return token_is_decltype (t);
1200 /* Called when processing a token with tree_check_value; perform or defer the
1201 associated checks and return the value. */
1203 static tree
1204 saved_checks_value (struct tree_check *check_value)
1206 /* Perform any access checks that were deferred. */
1207 vec<deferred_access_check, va_gc> *checks;
1208 deferred_access_check *chk;
1209 checks = check_value->checks;
1210 if (checks)
1212 int i;
1213 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1214 perform_or_defer_access_check (chk->binfo,
1215 chk->decl,
1216 chk->diag_decl, tf_warning_or_error);
1218 /* Return the stored value. */
1219 return check_value->value;
1222 /* Return a pointer to the Nth token in the token stream. If N is 1,
1223 then this is precisely equivalent to cp_lexer_peek_token (except
1224 that it is not inline). One would like to disallow that case, but
1225 there is one case (cp_parser_nth_token_starts_template_id) where
1226 the caller passes a variable for N and it might be 1. */
1228 static cp_token *
1229 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1231 cp_token *token;
1233 /* N is 1-based, not zero-based. */
1234 gcc_assert (n > 0);
1236 if (cp_lexer_debugging_p (lexer))
1237 fprintf (cp_lexer_debug_stream,
1238 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1240 --n;
1241 token = lexer->next_token;
1242 while (n && token->type != CPP_EOF)
1244 ++token;
1245 if (!token->purged_p)
1246 --n;
1249 if (cp_lexer_debugging_p (lexer))
1251 cp_lexer_print_token (cp_lexer_debug_stream, token);
1252 putc ('\n', cp_lexer_debug_stream);
1255 return token;
1258 /* Return the next token, and advance the lexer's next_token pointer
1259 to point to the next non-purged token. */
1261 static cp_token *
1262 cp_lexer_consume_token (cp_lexer* lexer)
1264 cp_token *token = lexer->next_token;
1268 gcc_assert (token->type != CPP_EOF);
1269 lexer->next_token++;
1271 while (lexer->next_token->purged_p);
1273 cp_lexer_set_source_position_from_token (token);
1275 /* Provide debugging output. */
1276 if (cp_lexer_debugging_p (lexer))
1278 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1279 cp_lexer_print_token (cp_lexer_debug_stream, token);
1280 putc ('\n', cp_lexer_debug_stream);
1283 return token;
1286 /* Permanently remove the next token from the token stream, and
1287 advance the next_token pointer to refer to the next non-purged
1288 token. */
1290 static void
1291 cp_lexer_purge_token (cp_lexer *lexer)
1293 cp_token *tok = lexer->next_token;
1295 gcc_assert (tok->type != CPP_EOF);
1296 tok->purged_p = true;
1297 tok->location = UNKNOWN_LOCATION;
1298 tok->u.value = NULL_TREE;
1299 tok->keyword = RID_MAX;
1302 tok++;
1303 while (tok->purged_p);
1304 lexer->next_token = tok;
1307 /* Permanently remove all tokens after TOK, up to, but not
1308 including, the token that will be returned next by
1309 cp_lexer_peek_token. */
1311 static void
1312 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1314 cp_token *peek = lexer->next_token;
1316 gcc_assert (tok < peek);
1318 for (tok++; tok != peek; tok++)
1320 tok->purged_p = true;
1321 tok->location = UNKNOWN_LOCATION;
1322 tok->u.value = NULL_TREE;
1323 tok->keyword = RID_MAX;
1327 /* Begin saving tokens. All tokens consumed after this point will be
1328 preserved. */
1330 static void
1331 cp_lexer_save_tokens (cp_lexer* lexer)
1333 /* Provide debugging output. */
1334 if (cp_lexer_debugging_p (lexer))
1335 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1337 lexer->saved_tokens.safe_push (lexer->next_token);
1340 /* Commit to the portion of the token stream most recently saved. */
1342 static void
1343 cp_lexer_commit_tokens (cp_lexer* lexer)
1345 /* Provide debugging output. */
1346 if (cp_lexer_debugging_p (lexer))
1347 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1349 lexer->saved_tokens.pop ();
1352 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1353 to the token stream. Stop saving tokens. */
1355 static void
1356 cp_lexer_rollback_tokens (cp_lexer* lexer)
1358 /* Provide debugging output. */
1359 if (cp_lexer_debugging_p (lexer))
1360 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1362 lexer->next_token = lexer->saved_tokens.pop ();
1365 /* Determines what saved_token_sentinel does when going out of scope. */
1367 enum saved_token_sentinel_mode {
1368 STS_COMMIT,
1369 STS_ROLLBACK,
1370 STS_DONOTHING
1373 /* RAII wrapper around the above functions, with sanity checking (the token
1374 stream should be the same at the point of instantiation as it is at the
1375 point of destruction).
1377 Creating a variable saves tokens. MODE determines what happens when the
1378 object is destroyed. STS_COMMIT commits tokens (default),
1379 STS_ROLLBACK rolls-back and STS_DONOTHING does nothing. Calling
1380 rollback() will immediately roll-back tokens and set MODE to
1381 STS_DONOTHING. */
1383 struct saved_token_sentinel
1385 cp_lexer *lexer;
1386 unsigned len;
1387 saved_token_sentinel_mode mode;
1388 saved_token_sentinel (cp_lexer *_lexer,
1389 saved_token_sentinel_mode _mode = STS_COMMIT)
1390 : lexer (_lexer), mode (_mode)
1392 len = lexer->saved_tokens.length ();
1393 cp_lexer_save_tokens (lexer);
1395 void rollback ()
1397 cp_lexer_rollback_tokens (lexer);
1398 cp_lexer_set_source_position_from_token
1399 (cp_lexer_previous_token (lexer));
1400 mode = STS_DONOTHING;
1402 ~saved_token_sentinel ()
1404 if (mode == STS_COMMIT)
1405 cp_lexer_commit_tokens (lexer);
1406 else if (mode == STS_ROLLBACK)
1407 rollback ();
1409 gcc_assert (lexer->saved_tokens.length () == len);
1413 /* Print a representation of the TOKEN on the STREAM. */
1415 static void
1416 cp_lexer_print_token (FILE * stream, cp_token *token)
1418 /* We don't use cpp_type2name here because the parser defines
1419 a few tokens of its own. */
1420 static const char *const token_names[] = {
1421 /* cpplib-defined token types */
1422 #define OP(e, s) #e,
1423 #define TK(e, s) #e,
1424 TTYPE_TABLE
1425 #undef OP
1426 #undef TK
1427 /* C++ parser token types - see "Manifest constants", above. */
1428 "KEYWORD",
1429 "TEMPLATE_ID",
1430 "NESTED_NAME_SPECIFIER",
1433 /* For some tokens, print the associated data. */
1434 switch (token->type)
1436 case CPP_KEYWORD:
1437 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1438 For example, `struct' is mapped to an INTEGER_CST. */
1439 if (!identifier_p (token->u.value))
1440 break;
1441 /* fall through */
1442 case CPP_NAME:
1443 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1444 break;
1446 case CPP_STRING:
1447 case CPP_STRING16:
1448 case CPP_STRING32:
1449 case CPP_WSTRING:
1450 case CPP_UTF8STRING:
1451 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1452 break;
1454 case CPP_NUMBER:
1455 print_generic_expr (stream, token->u.value);
1456 break;
1458 default:
1459 /* If we have a name for the token, print it out. Otherwise, we
1460 simply give the numeric code. */
1461 if (token->type < ARRAY_SIZE(token_names))
1462 fputs (token_names[token->type], stream);
1463 else
1464 fprintf (stream, "[%d]", token->type);
1465 break;
1469 DEBUG_FUNCTION void
1470 debug (cp_token &ref)
1472 cp_lexer_print_token (stderr, &ref);
1473 fprintf (stderr, "\n");
1476 DEBUG_FUNCTION void
1477 debug (cp_token *ptr)
1479 if (ptr)
1480 debug (*ptr);
1481 else
1482 fprintf (stderr, "<nil>\n");
1486 /* Start emitting debugging information. */
1488 static void
1489 cp_lexer_start_debugging (cp_lexer* lexer)
1491 if (!LEXER_DEBUGGING_ENABLED_P)
1492 fatal_error (input_location,
1493 "%<LEXER_DEBUGGING_ENABLED_P%> is not set to true");
1495 lexer->debugging_p = true;
1496 cp_lexer_debug_stream = stderr;
1499 /* Stop emitting debugging information. */
1501 static void
1502 cp_lexer_stop_debugging (cp_lexer* lexer)
1504 if (!LEXER_DEBUGGING_ENABLED_P)
1505 fatal_error (input_location,
1506 "%<LEXER_DEBUGGING_ENABLED_P%> is not set to true");
1508 lexer->debugging_p = false;
1509 cp_lexer_debug_stream = NULL;
1512 /* Create a new cp_token_cache, representing a range of tokens. */
1514 static cp_token_cache *
1515 cp_token_cache_new (cp_token *first, cp_token *last)
1517 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1518 cache->first = first;
1519 cache->last = last;
1520 return cache;
1523 /* Diagnose if #pragma omp declare simd isn't followed immediately
1524 by function declaration or definition. */
1526 static inline void
1527 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1529 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1531 error ("%<#pragma omp declare %s%> not immediately followed by "
1532 "function declaration or definition",
1533 parser->omp_declare_simd->variant_p ? "variant" : "simd");
1534 parser->omp_declare_simd = NULL;
1538 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1539 and put that into "omp declare simd" attribute. */
1541 static inline void
1542 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1544 if (UNLIKELY (parser->omp_declare_simd != NULL))
1546 if (fndecl == error_mark_node)
1548 parser->omp_declare_simd = NULL;
1549 return;
1551 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1553 cp_ensure_no_omp_declare_simd (parser);
1554 return;
1559 /* Similarly, but for use in declaration parsing functions
1560 which call cp_parser_handle_directive_omp_attributes. */
1562 static inline void
1563 cp_finalize_omp_declare_simd (cp_parser *parser, cp_omp_declare_simd_data *data)
1565 if (parser->omp_declare_simd != data)
1566 return;
1568 if (!parser->omp_declare_simd->error_seen
1569 && !parser->omp_declare_simd->fndecl_seen)
1570 error_at (parser->omp_declare_simd->loc,
1571 "%<declare %s%> directive not immediately followed by "
1572 "function declaration or definition",
1573 parser->omp_declare_simd->variant_p ? "variant" : "simd");
1574 parser->omp_declare_simd = NULL;
1577 /* Diagnose if #pragma acc routine isn't followed immediately by function
1578 declaration or definition. */
1580 static inline void
1581 cp_ensure_no_oacc_routine (cp_parser *parser)
1583 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1585 error_at (parser->oacc_routine->loc,
1586 "%<#pragma acc routine%> not immediately followed by "
1587 "function declaration or definition");
1588 parser->oacc_routine = NULL;
1592 /* Decl-specifiers. */
1594 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1596 static void
1597 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1599 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1602 /* Declarators. */
1604 /* Nothing other than the parser should be creating declarators;
1605 declarators are a semi-syntactic representation of C++ entities.
1606 Other parts of the front end that need to create entities (like
1607 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1609 static cp_declarator *make_call_declarator
1610 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier,
1611 tree, tree, tree, tree, tree, location_t);
1612 static cp_declarator *make_array_declarator
1613 (cp_declarator *, tree);
1614 static cp_declarator *make_pointer_declarator
1615 (cp_cv_quals, cp_declarator *, tree);
1616 static cp_declarator *make_reference_declarator
1617 (cp_cv_quals, cp_declarator *, bool, tree);
1618 static cp_declarator *make_ptrmem_declarator
1619 (cp_cv_quals, tree, cp_declarator *, tree);
1621 /* An erroneous declarator. */
1622 static cp_declarator *cp_error_declarator;
1624 /* The obstack on which declarators and related data structures are
1625 allocated. */
1626 static struct obstack declarator_obstack;
1628 /* Alloc BYTES from the declarator memory pool. */
1630 static inline void *
1631 alloc_declarator (size_t bytes)
1633 return obstack_alloc (&declarator_obstack, bytes);
1636 /* Allocate a declarator of the indicated KIND. Clear fields that are
1637 common to all declarators. */
1639 static cp_declarator *
1640 make_declarator (cp_declarator_kind kind)
1642 cp_declarator *declarator;
1644 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1645 declarator->kind = kind;
1646 declarator->parenthesized = UNKNOWN_LOCATION;
1647 declarator->attributes = NULL_TREE;
1648 declarator->std_attributes = NULL_TREE;
1649 declarator->declarator = NULL;
1650 declarator->parameter_pack_p = false;
1651 declarator->id_loc = UNKNOWN_LOCATION;
1652 declarator->init_loc = UNKNOWN_LOCATION;
1654 return declarator;
1657 /* Make a declarator for a generalized identifier. If
1658 QUALIFYING_SCOPE is non-NULL, the identifier is
1659 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1660 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1661 is, if any. */
1663 static cp_declarator *
1664 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1665 special_function_kind sfk, location_t id_location)
1667 cp_declarator *declarator;
1669 /* It is valid to write:
1671 class C { void f(); };
1672 typedef C D;
1673 void D::f();
1675 The standard is not clear about whether `typedef const C D' is
1676 legal; as of 2002-09-15 the committee is considering that
1677 question. EDG 3.0 allows that syntax. Therefore, we do as
1678 well. */
1679 if (qualifying_scope && TYPE_P (qualifying_scope))
1680 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1682 gcc_assert (identifier_p (unqualified_name)
1683 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1684 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1686 declarator = make_declarator (cdk_id);
1687 declarator->u.id.qualifying_scope = qualifying_scope;
1688 declarator->u.id.unqualified_name = unqualified_name;
1689 declarator->u.id.sfk = sfk;
1690 declarator->id_loc = id_location;
1692 return declarator;
1695 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1696 of modifiers such as const or volatile to apply to the pointer
1697 type, represented as identifiers. ATTRIBUTES represent the attributes that
1698 appertain to the pointer or reference. */
1700 cp_declarator *
1701 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1702 tree attributes)
1704 cp_declarator *declarator;
1706 declarator = make_declarator (cdk_pointer);
1707 declarator->declarator = target;
1708 declarator->u.pointer.qualifiers = cv_qualifiers;
1709 declarator->u.pointer.class_type = NULL_TREE;
1710 if (target)
1712 declarator->id_loc = target->id_loc;
1713 declarator->parameter_pack_p = target->parameter_pack_p;
1714 target->parameter_pack_p = false;
1716 else
1717 declarator->parameter_pack_p = false;
1719 declarator->std_attributes = attributes;
1721 return declarator;
1724 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1725 represent the attributes that appertain to the pointer or
1726 reference. */
1728 cp_declarator *
1729 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1730 bool rvalue_ref, tree attributes)
1732 cp_declarator *declarator;
1734 declarator = make_declarator (cdk_reference);
1735 declarator->declarator = target;
1736 declarator->u.reference.qualifiers = cv_qualifiers;
1737 declarator->u.reference.rvalue_ref = rvalue_ref;
1738 if (target)
1740 declarator->id_loc = target->id_loc;
1741 declarator->parameter_pack_p = target->parameter_pack_p;
1742 target->parameter_pack_p = false;
1744 else
1745 declarator->parameter_pack_p = false;
1747 declarator->std_attributes = attributes;
1749 return declarator;
1752 /* Like make_pointer_declarator -- but for a pointer to a non-static
1753 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1754 appertain to the pointer or reference. */
1756 cp_declarator *
1757 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1758 cp_declarator *pointee,
1759 tree attributes)
1761 cp_declarator *declarator;
1763 declarator = make_declarator (cdk_ptrmem);
1764 declarator->declarator = pointee;
1765 declarator->u.pointer.qualifiers = cv_qualifiers;
1766 declarator->u.pointer.class_type = class_type;
1768 if (pointee)
1770 declarator->parameter_pack_p = pointee->parameter_pack_p;
1771 pointee->parameter_pack_p = false;
1773 else
1774 declarator->parameter_pack_p = false;
1776 declarator->std_attributes = attributes;
1778 return declarator;
1781 /* Make a declarator for the function given by TARGET, with the
1782 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1783 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1784 indicates what exceptions can be thrown. STD_ATTRS contains
1785 attributes that appertain to the function type. */
1787 cp_declarator *
1788 make_call_declarator (cp_declarator *target,
1789 tree parms,
1790 cp_cv_quals cv_qualifiers,
1791 cp_virt_specifiers virt_specifiers,
1792 cp_ref_qualifier ref_qualifier,
1793 tree tx_qualifier,
1794 tree exception_specification,
1795 tree late_return_type,
1796 tree requires_clause,
1797 tree std_attrs,
1798 location_t parens_loc)
1800 cp_declarator *declarator;
1802 declarator = make_declarator (cdk_function);
1803 declarator->declarator = target;
1804 declarator->u.function.parameters = parms;
1805 declarator->u.function.qualifiers = cv_qualifiers;
1806 declarator->u.function.virt_specifiers = virt_specifiers;
1807 declarator->u.function.ref_qualifier = ref_qualifier;
1808 declarator->u.function.tx_qualifier = tx_qualifier;
1809 declarator->u.function.exception_specification = exception_specification;
1810 declarator->u.function.late_return_type = late_return_type;
1811 declarator->u.function.requires_clause = requires_clause;
1812 declarator->u.function.parens_loc = parens_loc;
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 = std_attrs;
1824 return declarator;
1827 /* Make a declarator for an array of BOUNDS elements, each of which is
1828 defined by ELEMENT. */
1830 cp_declarator *
1831 make_array_declarator (cp_declarator *element, tree bounds)
1833 cp_declarator *declarator;
1835 declarator = make_declarator (cdk_array);
1836 declarator->declarator = element;
1837 declarator->u.array.bounds = bounds;
1838 if (element)
1840 declarator->id_loc = element->id_loc;
1841 declarator->parameter_pack_p = element->parameter_pack_p;
1842 element->parameter_pack_p = false;
1844 else
1845 declarator->parameter_pack_p = false;
1847 return declarator;
1850 /* Determine whether the declarator we've seen so far can be a
1851 parameter pack, when followed by an ellipsis. */
1852 static bool
1853 declarator_can_be_parameter_pack (cp_declarator *declarator)
1855 if (declarator && declarator->parameter_pack_p)
1856 /* We already saw an ellipsis. */
1857 return false;
1859 /* Search for a declarator name, or any other declarator that goes
1860 after the point where the ellipsis could appear in a parameter
1861 pack. If we find any of these, then this declarator cannot be
1862 made into a parameter pack. */
1863 bool found = false;
1864 while (declarator && !found)
1866 switch ((int)declarator->kind)
1868 case cdk_id:
1869 case cdk_array:
1870 case cdk_decomp:
1871 found = true;
1872 break;
1874 case cdk_error:
1875 return true;
1877 default:
1878 declarator = declarator->declarator;
1879 break;
1883 return !found;
1886 cp_parameter_declarator *no_parameters;
1888 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1889 DECLARATOR and DEFAULT_ARGUMENT. */
1891 cp_parameter_declarator *
1892 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1893 cp_declarator *declarator,
1894 tree default_argument,
1895 location_t loc,
1896 bool template_parameter_pack_p = false)
1898 cp_parameter_declarator *parameter;
1900 parameter = ((cp_parameter_declarator *)
1901 alloc_declarator (sizeof (cp_parameter_declarator)));
1902 parameter->next = NULL;
1903 if (decl_specifiers)
1904 parameter->decl_specifiers = *decl_specifiers;
1905 else
1906 clear_decl_specs (&parameter->decl_specifiers);
1907 parameter->declarator = declarator;
1908 parameter->default_argument = default_argument;
1909 parameter->template_parameter_pack_p = template_parameter_pack_p;
1910 parameter->loc = loc;
1912 return parameter;
1915 /* Returns true iff DECLARATOR is a declaration for a function. */
1917 static bool
1918 function_declarator_p (const cp_declarator *declarator)
1920 while (declarator)
1922 if (declarator->kind == cdk_function
1923 && declarator->declarator->kind == cdk_id)
1924 return true;
1925 if (declarator->kind == cdk_id
1926 || declarator->kind == cdk_decomp
1927 || declarator->kind == cdk_error)
1928 return false;
1929 declarator = declarator->declarator;
1931 return false;
1934 /* The parser. */
1936 /* Overview
1937 --------
1939 A cp_parser parses the token stream as specified by the C++
1940 grammar. Its job is purely parsing, not semantic analysis. For
1941 example, the parser breaks the token stream into declarators,
1942 expressions, statements, and other similar syntactic constructs.
1943 It does not check that the types of the expressions on either side
1944 of an assignment-statement are compatible, or that a function is
1945 not declared with a parameter of type `void'.
1947 The parser invokes routines elsewhere in the compiler to perform
1948 semantic analysis and to build up the abstract syntax tree for the
1949 code processed.
1951 The parser (and the template instantiation code, which is, in a
1952 way, a close relative of parsing) are the only parts of the
1953 compiler that should be calling push_scope and pop_scope, or
1954 related functions. The parser (and template instantiation code)
1955 keeps track of what scope is presently active; everything else
1956 should simply honor that. (The code that generates static
1957 initializers may also need to set the scope, in order to check
1958 access control correctly when emitting the initializers.)
1960 Methodology
1961 -----------
1963 The parser is of the standard recursive-descent variety. Upcoming
1964 tokens in the token stream are examined in order to determine which
1965 production to use when parsing a non-terminal. Some C++ constructs
1966 require arbitrary look ahead to disambiguate. For example, it is
1967 impossible, in the general case, to tell whether a statement is an
1968 expression or declaration without scanning the entire statement.
1969 Therefore, the parser is capable of "parsing tentatively." When the
1970 parser is not sure what construct comes next, it enters this mode.
1971 Then, while we attempt to parse the construct, the parser queues up
1972 error messages, rather than issuing them immediately, and saves the
1973 tokens it consumes. If the construct is parsed successfully, the
1974 parser "commits", i.e., it issues any queued error messages and
1975 the tokens that were being preserved are permanently discarded.
1976 If, however, the construct is not parsed successfully, the parser
1977 rolls back its state completely so that it can resume parsing using
1978 a different alternative.
1980 Future Improvements
1981 -------------------
1983 The performance of the parser could probably be improved substantially.
1984 We could often eliminate the need to parse tentatively by looking ahead
1985 a little bit. In some places, this approach might not entirely eliminate
1986 the need to parse tentatively, but it might still speed up the average
1987 case. */
1989 /* Flags that are passed to some parsing functions. These values can
1990 be bitwise-ored together. */
1992 enum
1994 /* No flags. */
1995 CP_PARSER_FLAGS_NONE = 0x0,
1996 /* The construct is optional. If it is not present, then no error
1997 should be issued. */
1998 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1999 /* When parsing a type-specifier, treat user-defined type-names
2000 as non-type identifiers. */
2001 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
2002 /* When parsing a type-specifier, do not try to parse a class-specifier
2003 or enum-specifier. */
2004 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
2005 /* When parsing a decl-specifier-seq, only allow type-specifier or
2006 constexpr. */
2007 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
2008 /* When parsing a decl-specifier-seq, only allow mutable, constexpr or
2009 for C++20 consteval or for C++23 static. */
2010 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10,
2011 /* When parsing a decl-specifier-seq, allow missing typename. */
2012 CP_PARSER_FLAGS_TYPENAME_OPTIONAL = 0x20,
2013 /* When parsing of the noexcept-specifier should be delayed. */
2014 CP_PARSER_FLAGS_DELAY_NOEXCEPT = 0x40,
2015 /* When parsing a consteval declarator. */
2016 CP_PARSER_FLAGS_CONSTEVAL = 0x80
2019 /* This type is used for parameters and variables which hold
2020 combinations of the above flags. */
2021 typedef int cp_parser_flags;
2023 /* The different kinds of declarators we want to parse. */
2025 enum cp_parser_declarator_kind
2027 /* We want an abstract declarator. */
2028 CP_PARSER_DECLARATOR_ABSTRACT,
2029 /* We want a named declarator. */
2030 CP_PARSER_DECLARATOR_NAMED,
2031 /* We don't mind, but the name must be an unqualified-id. */
2032 CP_PARSER_DECLARATOR_EITHER
2035 /* The precedence values used to parse binary expressions. The minimum value
2036 of PREC must be 1, because zero is reserved to quickly discriminate
2037 binary operators from other tokens. */
2039 enum cp_parser_prec
2041 PREC_NOT_OPERATOR,
2042 PREC_LOGICAL_OR_EXPRESSION,
2043 PREC_LOGICAL_AND_EXPRESSION,
2044 PREC_INCLUSIVE_OR_EXPRESSION,
2045 PREC_EXCLUSIVE_OR_EXPRESSION,
2046 PREC_AND_EXPRESSION,
2047 PREC_EQUALITY_EXPRESSION,
2048 PREC_RELATIONAL_EXPRESSION,
2049 PREC_SPACESHIP_EXPRESSION,
2050 PREC_SHIFT_EXPRESSION,
2051 PREC_ADDITIVE_EXPRESSION,
2052 PREC_MULTIPLICATIVE_EXPRESSION,
2053 PREC_PM_EXPRESSION,
2054 NUM_PREC_VALUES = PREC_PM_EXPRESSION
2057 /* A mapping from a token type to a corresponding tree node type, with a
2058 precedence value. */
2060 struct cp_parser_binary_operations_map_node
2062 /* The token type. */
2063 enum cpp_ttype token_type;
2064 /* The corresponding tree code. */
2065 enum tree_code tree_type;
2066 /* The precedence of this operator. */
2067 enum cp_parser_prec prec;
2070 struct cp_parser_expression_stack_entry
2072 /* Left hand side of the binary operation we are currently
2073 parsing. */
2074 cp_expr lhs;
2075 /* Original tree code for left hand side, if it was a binary
2076 expression itself (used for -Wparentheses). */
2077 enum tree_code lhs_type;
2078 /* Tree code for the binary operation we are parsing. */
2079 enum tree_code tree_type;
2080 /* Precedence of the binary operation we are parsing. */
2081 enum cp_parser_prec prec;
2082 /* Location of the binary operation we are parsing. */
2083 location_t loc;
2084 /* Flags from the operator token. */
2085 unsigned char flags;
2088 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
2089 entries because precedence levels on the stack are monotonically
2090 increasing. */
2091 typedef struct cp_parser_expression_stack_entry
2092 cp_parser_expression_stack[NUM_PREC_VALUES];
2094 /* Prototypes. */
2096 /* Constructors and destructors. */
2098 static cp_parser_context *cp_parser_context_new
2099 (cp_parser_context *);
2101 /* Class variables. */
2103 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
2105 /* The operator-precedence table used by cp_parser_binary_expression.
2106 Transformed into an associative array (binops_by_token) by
2107 cp_parser_new. */
2109 static const cp_parser_binary_operations_map_node binops[] = {
2110 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
2111 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
2113 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
2114 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
2115 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
2117 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
2118 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
2120 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
2121 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
2123 { CPP_SPACESHIP, SPACESHIP_EXPR, PREC_SPACESHIP_EXPRESSION },
2125 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
2126 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
2127 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
2128 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
2130 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
2131 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
2133 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
2135 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
2137 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
2139 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
2141 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
2144 /* The same as binops, but initialized by cp_parser_new so that
2145 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
2146 for speed. */
2147 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
2149 /* Constructors and destructors. */
2151 /* Construct a new context. The context below this one on the stack
2152 is given by NEXT. */
2154 static cp_parser_context *
2155 cp_parser_context_new (cp_parser_context* next)
2157 cp_parser_context *context;
2159 /* Allocate the storage. */
2160 if (cp_parser_context_free_list != NULL)
2162 /* Pull the first entry from the free list. */
2163 context = cp_parser_context_free_list;
2164 cp_parser_context_free_list = context->next;
2165 memset (context, 0, sizeof (*context));
2167 else
2168 context = ggc_cleared_alloc<cp_parser_context> ();
2170 /* No errors have occurred yet in this context. */
2171 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
2172 /* If this is not the bottommost context, copy information that we
2173 need from the previous context. */
2174 if (next)
2176 /* If, in the NEXT context, we are parsing an `x->' or `x.'
2177 expression, then we are parsing one in this context, too. */
2178 context->object_type = next->object_type;
2179 /* Thread the stack. */
2180 context->next = next;
2183 return context;
2186 /* Managing the unparsed function queues. */
2188 #define unparsed_funs_with_default_args \
2189 parser->unparsed_queues->last ().funs_with_default_args
2190 #define unparsed_funs_with_definitions \
2191 parser->unparsed_queues->last ().funs_with_definitions
2192 #define unparsed_nsdmis \
2193 parser->unparsed_queues->last ().nsdmis
2194 #define unparsed_noexcepts \
2195 parser->unparsed_queues->last ().noexcepts
2196 #define unparsed_contracts \
2197 parser->unparsed_queues->last ().contracts
2199 static void
2200 push_unparsed_function_queues (cp_parser *parser)
2202 cp_unparsed_functions_entry e
2203 = { NULL, make_tree_vector (), NULL, NULL, NULL };
2204 vec_safe_push (parser->unparsed_queues, e);
2207 static void
2208 pop_unparsed_function_queues (cp_parser *parser)
2210 release_tree_vector (unparsed_funs_with_definitions);
2211 parser->unparsed_queues->pop ();
2214 /* Prototypes. */
2216 /* Routines to parse various constructs.
2218 Those that return `tree' will return the error_mark_node (rather
2219 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2220 Sometimes, they will return an ordinary node if error-recovery was
2221 attempted, even though a parse error occurred. So, to check
2222 whether or not a parse error occurred, you should always use
2223 cp_parser_error_occurred. If the construct is optional (indicated
2224 either by an `_opt' in the name of the function that does the
2225 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2226 the construct is not present. */
2228 /* Lexical conventions [gram.lex] */
2230 static cp_expr cp_parser_identifier
2231 (cp_parser *);
2232 static cp_expr cp_parser_string_literal
2233 (cp_parser *, bool, bool, bool);
2234 static cp_expr cp_parser_userdef_char_literal
2235 (cp_parser *);
2236 static tree cp_parser_userdef_string_literal
2237 (tree);
2238 static cp_expr cp_parser_userdef_numeric_literal
2239 (cp_parser *);
2241 /* Basic concepts [gram.basic] */
2243 static void cp_parser_translation_unit (cp_parser *);
2245 /* Expressions [gram.expr] */
2247 static cp_expr cp_parser_primary_expression
2248 (cp_parser *, bool, bool, bool, cp_id_kind *);
2249 static cp_expr cp_parser_id_expression
2250 (cp_parser *, bool, bool, bool *, bool, bool);
2251 static cp_expr cp_parser_unqualified_id
2252 (cp_parser *, bool, bool, bool, bool);
2253 static tree cp_parser_nested_name_specifier_opt
2254 (cp_parser *, bool, bool, bool, bool, bool = false);
2255 static tree cp_parser_nested_name_specifier
2256 (cp_parser *, bool, bool, bool, bool);
2257 static tree cp_parser_qualifying_entity
2258 (cp_parser *, bool, bool, bool, bool, bool);
2259 static cp_expr cp_parser_postfix_expression
2260 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2261 static tree cp_parser_postfix_open_square_expression
2262 (cp_parser *, tree, bool, bool);
2263 static tree cp_parser_postfix_dot_deref_expression
2264 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2265 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2266 (cp_parser *, int, bool, bool, bool *, location_t * = NULL,
2267 bool = false);
2268 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2269 enum { non_attr = 0, normal_attr = 1, id_attr = 2, assume_attr = 3 };
2270 static void cp_parser_pseudo_destructor_name
2271 (cp_parser *, tree, tree *, tree *);
2272 static cp_expr cp_parser_unary_expression
2273 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2274 static enum tree_code cp_parser_unary_operator
2275 (cp_token *);
2276 static tree cp_parser_has_attribute_expression
2277 (cp_parser *);
2278 static tree cp_parser_new_expression
2279 (cp_parser *);
2280 static vec<tree, va_gc> *cp_parser_new_placement
2281 (cp_parser *);
2282 static tree cp_parser_new_type_id
2283 (cp_parser *, tree *);
2284 static cp_declarator *cp_parser_new_declarator_opt
2285 (cp_parser *);
2286 static cp_declarator *cp_parser_direct_new_declarator
2287 (cp_parser *);
2288 static vec<tree, va_gc> *cp_parser_new_initializer
2289 (cp_parser *);
2290 static tree cp_parser_delete_expression
2291 (cp_parser *);
2292 static cp_expr cp_parser_cast_expression
2293 (cp_parser *, bool, bool, bool, cp_id_kind *);
2294 static cp_expr cp_parser_binary_expression
2295 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2296 static tree cp_parser_question_colon_clause
2297 (cp_parser *, cp_expr);
2298 static cp_expr cp_parser_conditional_expression (cp_parser *);
2299 static cp_expr cp_parser_assignment_expression
2300 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2301 static enum tree_code cp_parser_assignment_operator_opt
2302 (cp_parser *);
2303 static cp_expr cp_parser_expression
2304 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2305 static cp_expr cp_parser_constant_expression
2306 (cp_parser *, int = 0, bool * = NULL, bool = false);
2307 static cp_expr cp_parser_builtin_offsetof
2308 (cp_parser *);
2309 static cp_expr cp_parser_lambda_expression
2310 (cp_parser *);
2311 static void cp_parser_lambda_introducer
2312 (cp_parser *, tree);
2313 static bool cp_parser_lambda_declarator_opt
2314 (cp_parser *, tree);
2315 static void cp_parser_lambda_body
2316 (cp_parser *, tree);
2318 /* Statements [gram.stmt.stmt] */
2320 static void cp_parser_statement
2321 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2322 static void cp_parser_label_for_labeled_statement
2323 (cp_parser *, tree);
2324 static tree cp_parser_expression_statement
2325 (cp_parser *, tree);
2326 static tree cp_parser_compound_statement
2327 (cp_parser *, tree, int, bool);
2328 static void cp_parser_statement_seq_opt
2329 (cp_parser *, tree);
2330 static tree cp_parser_selection_statement
2331 (cp_parser *, bool *, vec<tree> *);
2332 static tree cp_parser_condition
2333 (cp_parser *);
2334 static tree cp_parser_iteration_statement
2335 (cp_parser *, bool *, bool, unsigned short);
2336 static bool cp_parser_init_statement
2337 (cp_parser *, tree *decl);
2338 static tree cp_parser_for
2339 (cp_parser *, bool, unsigned short);
2340 static tree cp_parser_c_for
2341 (cp_parser *, tree, tree, bool, unsigned short);
2342 static tree cp_parser_range_for
2343 (cp_parser *, tree, tree, tree, bool, unsigned short, bool);
2344 static void do_range_for_auto_deduction
2345 (tree, tree, tree, unsigned int);
2346 static tree cp_parser_perform_range_for_lookup
2347 (tree, tree *, tree *);
2348 static tree cp_parser_range_for_member_function
2349 (tree, tree);
2350 static tree cp_parser_jump_statement
2351 (cp_parser *);
2352 static void cp_parser_declaration_statement
2353 (cp_parser *);
2355 static tree cp_parser_implicitly_scoped_statement
2356 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2357 static void cp_parser_already_scoped_statement
2358 (cp_parser *, bool *, const token_indent_info &);
2360 /* State of module-declaration parsing. */
2361 enum module_parse
2363 MP_NOT_MODULE, /* Not a module. */
2365 _MP_UNUSED,
2367 MP_FIRST, /* First declaration of TU. */
2368 MP_GLOBAL, /* Global Module Fragment. */
2370 MP_PURVIEW_IMPORTS, /* Imports of a module. */
2371 MP_PURVIEW, /* Purview of a named module. */
2373 MP_PRIVATE_IMPORTS, /* Imports of a Private Module Fragment. */
2374 MP_PRIVATE, /* Private Module Fragment. */
2377 static module_parse cp_parser_module_declaration
2378 (cp_parser *parser, module_parse, bool exporting);
2379 static void cp_parser_import_declaration
2380 (cp_parser *parser, module_parse, bool exporting);
2382 /* Declarations [gram.dcl.dcl] */
2384 static void cp_parser_declaration_seq_opt
2385 (cp_parser *);
2386 static void cp_parser_declaration
2387 (cp_parser *, tree);
2388 static void cp_parser_toplevel_declaration
2389 (cp_parser *);
2390 static void cp_parser_block_declaration
2391 (cp_parser *, bool);
2392 static void cp_parser_simple_declaration
2393 (cp_parser *, bool, tree *);
2394 static void cp_parser_decl_specifier_seq
2395 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2396 static tree cp_parser_storage_class_specifier_opt
2397 (cp_parser *);
2398 static tree cp_parser_function_specifier_opt
2399 (cp_parser *, cp_decl_specifier_seq *);
2400 static tree cp_parser_type_specifier
2401 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2402 int *, bool *);
2403 static tree cp_parser_simple_type_specifier
2404 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2405 static tree cp_parser_placeholder_type_specifier
2406 (cp_parser *, location_t, tree, bool);
2407 static tree cp_parser_type_name
2408 (cp_parser *, bool);
2409 static tree cp_parser_nonclass_name
2410 (cp_parser* parser);
2411 static tree cp_parser_elaborated_type_specifier
2412 (cp_parser *, bool, bool);
2413 static tree cp_parser_enum_specifier
2414 (cp_parser *);
2415 static void cp_parser_enumerator_list
2416 (cp_parser *, tree);
2417 static void cp_parser_enumerator_definition
2418 (cp_parser *, tree);
2419 static tree cp_parser_namespace_name
2420 (cp_parser *);
2421 static void cp_parser_namespace_definition
2422 (cp_parser *);
2423 static void cp_parser_namespace_body
2424 (cp_parser *);
2425 static tree cp_parser_qualified_namespace_specifier
2426 (cp_parser *);
2427 static void cp_parser_namespace_alias_definition
2428 (cp_parser *);
2429 static bool cp_parser_using_declaration
2430 (cp_parser *, bool);
2431 static void cp_parser_using_directive
2432 (cp_parser *);
2433 static void cp_parser_using_enum
2434 (cp_parser *);
2435 static tree cp_parser_alias_declaration
2436 (cp_parser *);
2437 static void cp_parser_asm_definition
2438 (cp_parser *);
2439 static void cp_parser_linkage_specification
2440 (cp_parser *, tree);
2441 static void cp_parser_static_assert
2442 (cp_parser *, bool);
2443 static tree cp_parser_decltype
2444 (cp_parser *);
2445 static tree cp_parser_decomposition_declaration
2446 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2448 /* Declarators [gram.dcl.decl] */
2450 static tree cp_parser_init_declarator
2451 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *,
2452 vec<deferred_access_check, va_gc> *, bool, bool, int, bool *, tree *,
2453 location_t *, tree *);
2454 static cp_declarator *cp_parser_declarator
2455 (cp_parser *, cp_parser_declarator_kind, cp_parser_flags, int *, bool *,
2456 bool, bool, bool);
2457 static cp_declarator *cp_parser_direct_declarator
2458 (cp_parser *, cp_parser_declarator_kind, cp_parser_flags, int *, bool, bool,
2459 bool);
2460 static enum tree_code cp_parser_ptr_operator
2461 (cp_parser *, tree *, cp_cv_quals *, tree *);
2462 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2463 (cp_parser *);
2464 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2465 (cp_parser *);
2466 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2467 (cp_parser *);
2468 static tree cp_parser_tx_qualifier_opt
2469 (cp_parser *);
2470 static tree cp_parser_late_return_type_opt
2471 (cp_parser *, cp_declarator *, tree &);
2472 static tree cp_parser_declarator_id
2473 (cp_parser *, bool);
2474 static tree cp_parser_type_id
2475 (cp_parser *, cp_parser_flags = CP_PARSER_FLAGS_NONE, location_t * = NULL);
2476 static tree cp_parser_template_type_arg
2477 (cp_parser *);
2478 static tree cp_parser_trailing_type_id (cp_parser *);
2479 static tree cp_parser_type_id_1
2480 (cp_parser *, cp_parser_flags, bool, bool, location_t *);
2481 static void cp_parser_type_specifier_seq
2482 (cp_parser *, cp_parser_flags, bool, bool, cp_decl_specifier_seq *);
2483 static tree cp_parser_parameter_declaration_clause
2484 (cp_parser *, cp_parser_flags);
2485 static tree cp_parser_parameter_declaration_list
2486 (cp_parser *, cp_parser_flags, auto_vec<tree> *);
2487 static cp_parameter_declarator *cp_parser_parameter_declaration
2488 (cp_parser *, cp_parser_flags, bool, bool *);
2489 static tree cp_parser_default_argument
2490 (cp_parser *, bool);
2491 static void cp_parser_function_body
2492 (cp_parser *, bool);
2493 static tree cp_parser_initializer
2494 (cp_parser *, bool *, bool *, bool = false);
2495 static cp_expr cp_parser_initializer_clause
2496 (cp_parser *, bool *);
2497 static cp_expr cp_parser_braced_list
2498 (cp_parser*, bool*);
2499 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2500 (cp_parser *, bool *, bool *);
2502 static void cp_parser_ctor_initializer_opt_and_function_body
2503 (cp_parser *, bool);
2505 static tree cp_parser_late_parsing_omp_declare_simd
2506 (cp_parser *, tree);
2508 static tree cp_parser_late_parsing_oacc_routine
2509 (cp_parser *, tree);
2511 static tree synthesize_implicit_template_parm
2512 (cp_parser *, tree);
2513 static tree finish_fully_implicit_template
2514 (cp_parser *, tree);
2515 static void abort_fully_implicit_template
2516 (cp_parser *);
2518 /* Classes [gram.class] */
2520 static tree cp_parser_class_name
2521 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2522 static tree cp_parser_class_specifier
2523 (cp_parser *);
2524 static tree cp_parser_class_head
2525 (cp_parser *, bool *);
2526 static enum tag_types cp_parser_class_key
2527 (cp_parser *);
2528 static void cp_parser_type_parameter_key
2529 (cp_parser* parser);
2530 static void cp_parser_member_specification_opt
2531 (cp_parser *);
2532 static void cp_parser_member_declaration
2533 (cp_parser *);
2534 static tree cp_parser_pure_specifier
2535 (cp_parser *);
2536 static tree cp_parser_constant_initializer
2537 (cp_parser *);
2539 /* Derived classes [gram.class.derived] */
2541 static tree cp_parser_base_clause
2542 (cp_parser *);
2543 static tree cp_parser_base_specifier
2544 (cp_parser *);
2546 /* Special member functions [gram.special] */
2548 static tree cp_parser_conversion_function_id
2549 (cp_parser *);
2550 static tree cp_parser_conversion_type_id
2551 (cp_parser *);
2552 static cp_declarator *cp_parser_conversion_declarator_opt
2553 (cp_parser *);
2554 static void cp_parser_ctor_initializer_opt
2555 (cp_parser *);
2556 static void cp_parser_mem_initializer_list
2557 (cp_parser *);
2558 static tree cp_parser_mem_initializer
2559 (cp_parser *);
2560 static tree cp_parser_mem_initializer_id
2561 (cp_parser *);
2563 /* Overloading [gram.over] */
2565 static cp_expr cp_parser_operator_function_id
2566 (cp_parser *);
2567 static cp_expr cp_parser_operator
2568 (cp_parser *, location_t);
2570 /* Templates [gram.temp] */
2572 static void cp_parser_template_declaration
2573 (cp_parser *, bool);
2574 static tree cp_parser_template_parameter_list
2575 (cp_parser *);
2576 static tree cp_parser_template_parameter
2577 (cp_parser *, bool *, bool *);
2578 static tree cp_parser_type_parameter
2579 (cp_parser *, bool *);
2580 static tree cp_parser_template_id
2581 (cp_parser *, bool, bool, enum tag_types, bool);
2582 static tree cp_parser_template_id_expr
2583 (cp_parser *, bool, bool, bool);
2584 static tree cp_parser_template_name
2585 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2586 static tree cp_parser_template_argument_list
2587 (cp_parser *);
2588 static tree cp_parser_template_argument
2589 (cp_parser *);
2590 static void cp_parser_explicit_instantiation
2591 (cp_parser *);
2592 static void cp_parser_explicit_specialization
2593 (cp_parser *);
2595 /* Exception handling [gram.except] */
2597 static tree cp_parser_try_block
2598 (cp_parser *);
2599 static void cp_parser_function_try_block
2600 (cp_parser *);
2601 static void cp_parser_handler_seq
2602 (cp_parser *);
2603 static void cp_parser_handler
2604 (cp_parser *);
2605 static tree cp_parser_exception_declaration
2606 (cp_parser *);
2607 static tree cp_parser_throw_expression
2608 (cp_parser *);
2609 static tree cp_parser_exception_specification_opt
2610 (cp_parser *, cp_parser_flags);
2611 static tree cp_parser_type_id_list
2612 (cp_parser *);
2613 static tree cp_parser_noexcept_specification_opt
2614 (cp_parser *, cp_parser_flags, bool, bool *, bool);
2616 /* GNU Extensions */
2618 static tree cp_parser_asm_specification_opt
2619 (cp_parser *);
2620 static tree cp_parser_asm_operand_list
2621 (cp_parser *);
2622 static tree cp_parser_asm_clobber_list
2623 (cp_parser *);
2624 static tree cp_parser_asm_label_list
2625 (cp_parser *);
2626 static bool cp_next_tokens_can_be_attribute_p
2627 (cp_parser *);
2628 static bool cp_next_tokens_can_be_gnu_attribute_p
2629 (cp_parser *);
2630 static bool cp_next_tokens_can_be_std_attribute_p
2631 (cp_parser *);
2632 static bool cp_nth_tokens_can_be_std_attribute_p
2633 (cp_parser *, size_t);
2634 static bool cp_nth_tokens_can_be_gnu_attribute_p
2635 (cp_parser *, size_t);
2636 static bool cp_nth_tokens_can_be_attribute_p
2637 (cp_parser *, size_t);
2638 static tree cp_parser_attributes_opt
2639 (cp_parser *);
2640 static tree cp_parser_gnu_attributes_opt
2641 (cp_parser *);
2642 static tree cp_parser_gnu_attribute_list
2643 (cp_parser *, bool = false);
2644 static tree cp_parser_std_attribute
2645 (cp_parser *, tree);
2646 static tree cp_parser_std_attribute_spec
2647 (cp_parser *);
2648 static tree cp_parser_std_attribute_spec_seq
2649 (cp_parser *);
2650 static size_t cp_parser_skip_std_attribute_spec_seq
2651 (cp_parser *, size_t);
2652 static size_t cp_parser_skip_attributes_opt
2653 (cp_parser *, size_t);
2654 static bool cp_parser_extension_opt
2655 (cp_parser *, int *);
2656 static void cp_parser_label_declaration
2657 (cp_parser *);
2659 /* Concept Extensions */
2661 static tree cp_parser_concept_definition
2662 (cp_parser *);
2663 static tree cp_parser_constraint_expression
2664 (cp_parser *);
2665 static tree cp_parser_requires_clause_opt
2666 (cp_parser *, bool);
2667 static tree cp_parser_requires_expression
2668 (cp_parser *);
2669 static tree cp_parser_requirement_parameter_list
2670 (cp_parser *);
2671 static tree cp_parser_requirement_body
2672 (cp_parser *);
2673 static tree cp_parser_requirement_seq
2674 (cp_parser *);
2675 static tree cp_parser_requirement
2676 (cp_parser *);
2677 static tree cp_parser_simple_requirement
2678 (cp_parser *);
2679 static tree cp_parser_compound_requirement
2680 (cp_parser *);
2681 static tree cp_parser_type_requirement
2682 (cp_parser *);
2683 static tree cp_parser_nested_requirement
2684 (cp_parser *);
2686 /* Transactional Memory Extensions */
2688 static tree cp_parser_transaction
2689 (cp_parser *, cp_token *);
2690 static tree cp_parser_transaction_expression
2691 (cp_parser *, enum rid);
2692 static void cp_parser_function_transaction
2693 (cp_parser *, enum rid);
2694 static tree cp_parser_transaction_cancel
2695 (cp_parser *);
2697 /* Coroutine extensions. */
2699 static tree cp_parser_yield_expression
2700 (cp_parser *);
2702 /* Contracts */
2704 static void cp_parser_late_contract_condition
2705 (cp_parser *, tree, tree);
2707 enum pragma_context {
2708 pragma_external,
2709 pragma_member,
2710 pragma_objc_icode,
2711 pragma_stmt,
2712 pragma_compound
2714 static bool cp_parser_pragma
2715 (cp_parser *, enum pragma_context, bool *);
2717 /* Objective-C++ Productions */
2719 static tree cp_parser_objc_message_receiver
2720 (cp_parser *);
2721 static tree cp_parser_objc_message_args
2722 (cp_parser *);
2723 static tree cp_parser_objc_message_expression
2724 (cp_parser *);
2725 static cp_expr cp_parser_objc_encode_expression
2726 (cp_parser *);
2727 static tree cp_parser_objc_defs_expression
2728 (cp_parser *);
2729 static tree cp_parser_objc_protocol_expression
2730 (cp_parser *);
2731 static tree cp_parser_objc_selector_expression
2732 (cp_parser *);
2733 static cp_expr cp_parser_objc_expression
2734 (cp_parser *);
2735 static bool cp_parser_objc_selector_p
2736 (enum cpp_ttype);
2737 static tree cp_parser_objc_selector
2738 (cp_parser *);
2739 static tree cp_parser_objc_protocol_refs_opt
2740 (cp_parser *);
2741 static void cp_parser_objc_declaration
2742 (cp_parser *, tree);
2743 static tree cp_parser_objc_statement
2744 (cp_parser *);
2745 static bool cp_parser_objc_valid_prefix_attributes
2746 (cp_parser *, tree *);
2747 static void cp_parser_objc_at_property_declaration
2748 (cp_parser *) ;
2749 static void cp_parser_objc_at_synthesize_declaration
2750 (cp_parser *) ;
2751 static void cp_parser_objc_at_dynamic_declaration
2752 (cp_parser *) ;
2753 static tree cp_parser_objc_struct_declaration
2754 (cp_parser *) ;
2756 /* Utility Routines */
2758 static cp_expr cp_parser_lookup_name
2759 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2760 static tree cp_parser_lookup_name_simple
2761 (cp_parser *, tree, location_t);
2762 static tree cp_parser_maybe_treat_template_as_class
2763 (tree, bool);
2764 static bool cp_parser_check_declarator_template_parameters
2765 (cp_parser *, cp_declarator *, location_t);
2766 static bool cp_parser_check_template_parameters
2767 (cp_parser *, unsigned, bool, location_t, cp_declarator *);
2768 static cp_expr cp_parser_simple_cast_expression
2769 (cp_parser *);
2770 static tree cp_parser_global_scope_opt
2771 (cp_parser *, bool);
2772 static bool cp_parser_constructor_declarator_p
2773 (cp_parser *, cp_parser_flags, bool);
2774 static tree cp_parser_function_definition_from_specifiers_and_declarator
2775 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2776 static tree cp_parser_function_definition_after_declarator
2777 (cp_parser *, bool);
2778 static bool cp_parser_template_declaration_after_export
2779 (cp_parser *, bool);
2780 static void cp_parser_perform_template_parameter_access_checks
2781 (vec<deferred_access_check, va_gc> *);
2782 static tree cp_parser_single_declaration
2783 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2784 static cp_expr cp_parser_functional_cast
2785 (cp_parser *, tree);
2786 static tree cp_parser_save_member_function_body
2787 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2788 static tree cp_parser_save_nsdmi
2789 (cp_parser *);
2790 static tree cp_parser_enclosed_template_argument_list
2791 (cp_parser *);
2792 static void cp_parser_save_default_args
2793 (cp_parser *, tree);
2794 static void cp_parser_late_parsing_for_member
2795 (cp_parser *, tree);
2796 static tree cp_parser_late_parse_one_default_arg
2797 (cp_parser *, tree, tree, tree);
2798 static void cp_parser_late_parsing_nsdmi
2799 (cp_parser *, tree);
2800 static void cp_parser_late_parsing_default_args
2801 (cp_parser *, tree);
2802 static tree cp_parser_sizeof_operand
2803 (cp_parser *, enum rid);
2804 static cp_expr cp_parser_trait
2805 (cp_parser *, enum rid);
2806 static bool cp_parser_declares_only_class_p
2807 (cp_parser *);
2808 static void cp_parser_set_storage_class
2809 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2810 static void cp_parser_set_decl_spec_type
2811 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2812 static void set_and_check_decl_spec_loc
2813 (cp_decl_specifier_seq *decl_specs,
2814 cp_decl_spec ds, cp_token *);
2815 static bool cp_parser_friend_p
2816 (const cp_decl_specifier_seq *);
2817 static void cp_parser_required_error
2818 (cp_parser *, required_token, bool, location_t);
2819 static cp_token *cp_parser_require
2820 (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2821 static cp_token *cp_parser_require_keyword
2822 (cp_parser *, enum rid, required_token);
2823 static bool cp_parser_token_starts_function_definition_p
2824 (cp_token *);
2825 static bool cp_parser_next_token_starts_class_definition_p
2826 (cp_parser *);
2827 static bool cp_parser_next_token_ends_template_argument_p
2828 (cp_parser *);
2829 static bool cp_parser_nth_token_starts_template_argument_list_p
2830 (cp_parser *, size_t);
2831 static enum tag_types cp_parser_token_is_class_key
2832 (cp_token *);
2833 static enum tag_types cp_parser_token_is_type_parameter_key
2834 (cp_token *);
2835 static void cp_parser_maybe_warn_enum_key (cp_parser *, location_t, tree, rid);
2836 static void cp_parser_check_class_key
2837 (cp_parser *, location_t, enum tag_types, tree type, bool, bool);
2838 static void cp_parser_check_access_in_redeclaration
2839 (tree type, location_t location);
2840 static bool cp_parser_optional_template_keyword
2841 (cp_parser *);
2842 static void cp_parser_pre_parsed_nested_name_specifier
2843 (cp_parser *);
2844 static bool cp_parser_cache_group
2845 (cp_parser *, enum cpp_ttype, unsigned);
2846 static tree cp_parser_cache_defarg
2847 (cp_parser *parser, bool nsdmi);
2848 static void cp_parser_parse_tentatively
2849 (cp_parser *);
2850 static void cp_parser_commit_to_tentative_parse
2851 (cp_parser *);
2852 static void cp_parser_commit_to_topmost_tentative_parse
2853 (cp_parser *);
2854 static void cp_parser_abort_tentative_parse
2855 (cp_parser *);
2856 static bool cp_parser_parse_definitely
2857 (cp_parser *);
2858 static inline bool cp_parser_parsing_tentatively
2859 (cp_parser *);
2860 static bool cp_parser_uncommitted_to_tentative_parse_p
2861 (cp_parser *);
2862 static void cp_parser_error
2863 (cp_parser *, const char *);
2864 static void cp_parser_name_lookup_error
2865 (cp_parser *, tree, tree, name_lookup_error, location_t);
2866 static bool cp_parser_simulate_error
2867 (cp_parser *);
2868 static bool cp_parser_check_type_definition
2869 (cp_parser *);
2870 static void cp_parser_check_for_definition_in_return_type
2871 (cp_declarator *, tree, location_t type_location);
2872 static void cp_parser_check_for_invalid_template_id
2873 (cp_parser *, tree, enum tag_types, location_t location);
2874 static bool cp_parser_non_integral_constant_expression
2875 (cp_parser *, non_integral_constant);
2876 static void cp_parser_diagnose_invalid_type_name
2877 (cp_parser *, tree, location_t);
2878 static bool cp_parser_parse_and_diagnose_invalid_type_name
2879 (cp_parser *);
2880 static int cp_parser_skip_to_closing_parenthesis
2881 (cp_parser *, bool, bool, bool);
2882 static void cp_parser_skip_to_end_of_statement
2883 (cp_parser *);
2884 static void cp_parser_consume_semicolon_at_end_of_statement
2885 (cp_parser *);
2886 static void cp_parser_skip_to_end_of_block_or_statement
2887 (cp_parser *);
2888 static bool cp_parser_skip_to_closing_brace
2889 (cp_parser *);
2890 static bool cp_parser_skip_entire_template_parameter_list
2891 (cp_parser *);
2892 static void cp_parser_require_end_of_template_parameter_list
2893 (cp_parser *);
2894 static bool cp_parser_skip_to_end_of_template_parameter_list
2895 (cp_parser *);
2896 static void cp_parser_skip_to_pragma_eol
2897 (cp_parser*, cp_token *);
2898 static bool cp_parser_error_occurred
2899 (cp_parser *);
2900 static bool cp_parser_allow_gnu_extensions_p
2901 (cp_parser *);
2902 static bool cp_parser_is_pure_string_literal
2903 (cp_token *);
2904 static bool cp_parser_is_string_literal
2905 (cp_token *);
2906 static bool cp_parser_is_keyword
2907 (cp_token *, enum rid);
2908 static tree cp_parser_make_typename_type
2909 (cp_parser *, tree, location_t location);
2910 static cp_declarator * cp_parser_make_indirect_declarator
2911 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2912 static bool cp_parser_compound_literal_p
2913 (cp_parser *);
2914 static bool cp_parser_array_designator_p
2915 (cp_parser *);
2916 static bool cp_parser_init_statement_p
2917 (cp_parser *);
2918 static bool cp_parser_skip_up_to_closing_square_bracket
2919 (cp_parser *);
2920 static bool cp_parser_skip_to_closing_square_bracket
2921 (cp_parser *);
2922 static size_t cp_parser_skip_balanced_tokens (cp_parser *, size_t);
2924 // -------------------------------------------------------------------------- //
2925 // Unevaluated Operand Guard
2927 // Implementation of an RAII helper for unevaluated operand parsing.
2928 cp_unevaluated::cp_unevaluated ()
2930 ++cp_unevaluated_operand;
2931 ++c_inhibit_evaluation_warnings;
2934 cp_unevaluated::~cp_unevaluated ()
2936 --c_inhibit_evaluation_warnings;
2937 --cp_unevaluated_operand;
2940 // -------------------------------------------------------------------------- //
2941 // Tentative Parsing
2943 /* Returns nonzero if we are parsing tentatively. */
2945 static inline bool
2946 cp_parser_parsing_tentatively (cp_parser* parser)
2948 return parser->context->next != NULL;
2951 /* Returns nonzero if TOKEN is a string literal. */
2953 static bool
2954 cp_parser_is_pure_string_literal (cp_token* token)
2956 return (token->type == CPP_STRING ||
2957 token->type == CPP_STRING16 ||
2958 token->type == CPP_STRING32 ||
2959 token->type == CPP_WSTRING ||
2960 token->type == CPP_UTF8STRING);
2963 /* Returns nonzero if TOKEN is a string literal
2964 of a user-defined string literal. */
2966 static bool
2967 cp_parser_is_string_literal (cp_token* token)
2969 return (cp_parser_is_pure_string_literal (token) ||
2970 token->type == CPP_STRING_USERDEF ||
2971 token->type == CPP_STRING16_USERDEF ||
2972 token->type == CPP_STRING32_USERDEF ||
2973 token->type == CPP_WSTRING_USERDEF ||
2974 token->type == CPP_UTF8STRING_USERDEF);
2977 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2979 static bool
2980 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2982 return token->keyword == keyword;
2985 /* Helper function for cp_parser_error.
2986 Having peeked a token of kind TOK1_KIND that might signify
2987 a conflict marker, peek successor tokens to determine
2988 if we actually do have a conflict marker.
2989 Specifically, we consider a run of 7 '<', '=' or '>' characters
2990 at the start of a line as a conflict marker.
2991 These come through the lexer as three pairs and a single,
2992 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2993 If it returns true, *OUT_LOC is written to with the location/range
2994 of the marker. */
2996 static bool
2997 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2998 location_t *out_loc)
3000 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
3001 if (token2->type != tok1_kind)
3002 return false;
3003 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
3004 if (token3->type != tok1_kind)
3005 return false;
3006 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
3007 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
3008 return false;
3010 /* It must be at the start of the line. */
3011 location_t start_loc = cp_lexer_peek_token (lexer)->location;
3012 if (LOCATION_COLUMN (start_loc) != 1)
3013 return false;
3015 /* We have a conflict marker. Construct a location of the form:
3016 <<<<<<<
3017 ^~~~~~~
3018 with start == caret, finishing at the end of the marker. */
3019 location_t finish_loc = get_finish (token4->location);
3020 *out_loc = make_location (start_loc, start_loc, finish_loc);
3022 return true;
3025 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
3026 RT_CLOSE_PAREN. */
3028 static const char *
3029 get_matching_symbol (required_token token_desc)
3031 switch (token_desc)
3033 default:
3034 gcc_unreachable ();
3035 return "";
3036 case RT_CLOSE_BRACE:
3037 return "{";
3038 case RT_CLOSE_PAREN:
3039 return "(";
3043 /* Attempt to convert TOKEN_DESC from a required_token to an
3044 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
3046 static enum cpp_ttype
3047 get_required_cpp_ttype (required_token token_desc)
3049 switch (token_desc)
3051 case RT_SEMICOLON:
3052 return CPP_SEMICOLON;
3053 case RT_OPEN_PAREN:
3054 return CPP_OPEN_PAREN;
3055 case RT_CLOSE_BRACE:
3056 return CPP_CLOSE_BRACE;
3057 case RT_OPEN_BRACE:
3058 return CPP_OPEN_BRACE;
3059 case RT_CLOSE_SQUARE:
3060 return CPP_CLOSE_SQUARE;
3061 case RT_OPEN_SQUARE:
3062 return CPP_OPEN_SQUARE;
3063 case RT_COMMA:
3064 return CPP_COMMA;
3065 case RT_COLON:
3066 return CPP_COLON;
3067 case RT_CLOSE_PAREN:
3068 return CPP_CLOSE_PAREN;
3070 default:
3071 /* Use CPP_EOF as a "no completions possible" code. */
3072 return CPP_EOF;
3077 /* Subroutine of cp_parser_error and cp_parser_required_error.
3079 Issue a diagnostic of the form
3080 FILE:LINE: MESSAGE before TOKEN
3081 where TOKEN is the next token in the input stream. MESSAGE
3082 (specified by the caller) is usually of the form "expected
3083 OTHER-TOKEN".
3085 This bypasses the check for tentative passing, and potentially
3086 adds material needed by cp_parser_required_error.
3088 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
3089 suggesting insertion of the missing token.
3091 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
3092 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
3093 location. */
3095 static void
3096 cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
3097 required_token missing_token_desc,
3098 location_t matching_location)
3100 cp_token *token = cp_lexer_peek_token (parser->lexer);
3101 /* This diagnostic makes more sense if it is tagged to the line
3102 of the token we just peeked at. */
3103 cp_lexer_set_source_position_from_token (token);
3105 if (token->type == CPP_PRAGMA)
3107 error_at (token->location,
3108 "%<#pragma%> is not allowed here");
3109 cp_parser_skip_to_pragma_eol (parser, token);
3110 return;
3113 /* If this is actually a conflict marker, report it as such. */
3114 if (token->type == CPP_LSHIFT
3115 || token->type == CPP_RSHIFT
3116 || token->type == CPP_EQ_EQ)
3118 location_t loc;
3119 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
3121 error_at (loc, "version control conflict marker in file");
3122 expanded_location token_exploc = expand_location (token->location);
3123 /* Consume tokens until the end of the source line. */
3124 for (;;)
3126 cp_lexer_consume_token (parser->lexer);
3127 cp_token *next = cp_lexer_peek_token (parser->lexer);
3128 if (next->type == CPP_EOF)
3129 break;
3130 if (next->location == UNKNOWN_LOCATION
3131 || loc == UNKNOWN_LOCATION)
3132 break;
3134 expanded_location next_exploc = expand_location (next->location);
3135 if (next_exploc.file != token_exploc.file)
3136 break;
3137 if (next_exploc.line != token_exploc.line)
3138 break;
3140 return;
3144 auto_diagnostic_group d;
3145 gcc_rich_location richloc (input_location);
3147 bool added_matching_location = false;
3149 if (missing_token_desc != RT_NONE)
3150 if (cp_token *prev_token = cp_lexer_safe_previous_token (parser->lexer))
3152 /* Potentially supply a fix-it hint, suggesting to add the
3153 missing token immediately after the *previous* token.
3154 This may move the primary location within richloc. */
3155 enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
3156 location_t prev_token_loc = prev_token->location;
3157 maybe_suggest_missing_token_insertion (&richloc, ttype,
3158 prev_token_loc);
3160 /* If matching_location != UNKNOWN_LOCATION, highlight it.
3161 Attempt to consolidate diagnostics by printing it as a
3162 secondary range within the main diagnostic. */
3163 if (matching_location != UNKNOWN_LOCATION)
3164 added_matching_location
3165 = richloc.add_location_if_nearby (matching_location);
3168 /* If we were parsing a string-literal and there is an unknown name
3169 token right after, then check to see if that could also have been
3170 a literal string by checking the name against a list of known
3171 standard string literal constants defined in header files. If
3172 there is one, then add that as an hint to the error message. */
3173 name_hint h;
3174 if (token->type == CPP_NAME)
3175 if (cp_token *prev_token = cp_lexer_safe_previous_token (parser->lexer))
3176 if (cp_parser_is_string_literal (prev_token))
3178 tree name = token->u.value;
3179 const char *token_name = IDENTIFIER_POINTER (name);
3180 const char *header_hint
3181 = get_cp_stdlib_header_for_string_macro_name (token_name);
3182 if (header_hint != NULL)
3183 h = name_hint (NULL, new suggest_missing_header (token->location,
3184 token_name,
3185 header_hint));
3188 /* Actually emit the error. */
3189 c_parse_error (gmsgid,
3190 /* Because c_parser_error does not understand
3191 CPP_KEYWORD, keywords are treated like
3192 identifiers. */
3193 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
3194 token->u.value, token->flags, &richloc);
3196 if (missing_token_desc != RT_NONE)
3198 /* If we weren't able to consolidate matching_location, then
3199 print it as a secondary diagnostic. */
3200 if (matching_location != UNKNOWN_LOCATION
3201 && !added_matching_location)
3202 inform (matching_location, "to match this %qs",
3203 get_matching_symbol (missing_token_desc));
3207 /* If not parsing tentatively, issue a diagnostic of the form
3208 FILE:LINE: MESSAGE before TOKEN
3209 where TOKEN is the next token in the input stream. MESSAGE
3210 (specified by the caller) is usually of the form "expected
3211 OTHER-TOKEN". */
3213 static void
3214 cp_parser_error (cp_parser* parser, const char* gmsgid)
3216 if (!cp_parser_simulate_error (parser))
3217 cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
3220 /* Issue an error about name-lookup failing. NAME is the
3221 IDENTIFIER_NODE DECL is the result of
3222 the lookup (as returned from cp_parser_lookup_name). DESIRED is
3223 the thing that we hoped to find. */
3225 static void
3226 cp_parser_name_lookup_error (cp_parser* parser,
3227 tree name,
3228 tree decl,
3229 name_lookup_error desired,
3230 location_t location)
3232 /* If name lookup completely failed, tell the user that NAME was not
3233 declared. */
3234 if (decl == error_mark_node)
3236 if (parser->scope && parser->scope != global_namespace)
3237 error_at (location, "%<%E::%E%> has not been declared",
3238 parser->scope, name);
3239 else if (parser->scope == global_namespace)
3240 error_at (location, "%<::%E%> has not been declared", name);
3241 else if (parser->object_scope
3242 && !CLASS_TYPE_P (parser->object_scope))
3243 error_at (location, "request for member %qE in non-class type %qT",
3244 name, parser->object_scope);
3245 else if (parser->object_scope)
3246 error_at (location, "%<%T::%E%> has not been declared",
3247 parser->object_scope, name);
3248 else
3249 error_at (location, "%qE has not been declared", name);
3251 else if (parser->scope && parser->scope != global_namespace)
3253 switch (desired)
3255 case NLE_TYPE:
3256 error_at (location, "%<%E::%E%> is not a type",
3257 parser->scope, name);
3258 break;
3259 case NLE_CXX98:
3260 error_at (location, "%<%E::%E%> is not a class or namespace",
3261 parser->scope, name);
3262 break;
3263 case NLE_NOT_CXX98:
3264 error_at (location,
3265 "%<%E::%E%> is not a class, namespace, or enumeration",
3266 parser->scope, name);
3267 break;
3268 default:
3269 gcc_unreachable ();
3273 else if (parser->scope == global_namespace)
3275 switch (desired)
3277 case NLE_TYPE:
3278 error_at (location, "%<::%E%> is not a type", name);
3279 break;
3280 case NLE_CXX98:
3281 error_at (location, "%<::%E%> is not a class or namespace", name);
3282 break;
3283 case NLE_NOT_CXX98:
3284 error_at (location,
3285 "%<::%E%> is not a class, namespace, or enumeration",
3286 name);
3287 break;
3288 default:
3289 gcc_unreachable ();
3292 else
3294 switch (desired)
3296 case NLE_TYPE:
3297 error_at (location, "%qE is not a type", name);
3298 break;
3299 case NLE_CXX98:
3300 error_at (location, "%qE is not a class or namespace", name);
3301 break;
3302 case NLE_NOT_CXX98:
3303 error_at (location,
3304 "%qE is not a class, namespace, or enumeration", name);
3305 break;
3306 default:
3307 gcc_unreachable ();
3312 /* If we are parsing tentatively, remember that an error has occurred
3313 during this tentative parse. Returns true if the error was
3314 simulated; false if a message should be issued by the caller. */
3316 static bool
3317 cp_parser_simulate_error (cp_parser* parser)
3319 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3321 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3322 return true;
3324 return false;
3327 /* This function is called when a type is defined. If type
3328 definitions are forbidden at this point, an error message is
3329 issued. */
3331 static bool
3332 cp_parser_check_type_definition (cp_parser* parser)
3334 /* If types are forbidden here, issue a message. */
3335 if (parser->type_definition_forbidden_message)
3337 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3338 or %qs in the message need to be interpreted. */
3339 error (parser->type_definition_forbidden_message,
3340 parser->type_definition_forbidden_message_arg);
3341 return false;
3343 return true;
3346 /* This function is called when the DECLARATOR is processed. The TYPE
3347 was a type defined in the decl-specifiers. If it is invalid to
3348 define a type in the decl-specifiers for DECLARATOR, an error is
3349 issued. TYPE_LOCATION is the location of TYPE and is used
3350 for error reporting. */
3352 static void
3353 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3354 tree type, location_t type_location)
3356 /* [dcl.fct] forbids type definitions in return types.
3357 Unfortunately, it's not easy to know whether or not we are
3358 processing a return type until after the fact. */
3359 while (declarator
3360 && (declarator->kind == cdk_pointer
3361 || declarator->kind == cdk_reference
3362 || declarator->kind == cdk_ptrmem))
3363 declarator = declarator->declarator;
3364 if (declarator
3365 && declarator->kind == cdk_function)
3367 error_at (type_location,
3368 "new types may not be defined in a return type");
3369 inform (type_location,
3370 "(perhaps a semicolon is missing after the definition of %qT)",
3371 type);
3375 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3376 "<" in any valid C++ program. If the next token is indeed "<",
3377 issue a message warning the user about what appears to be an
3378 invalid attempt to form a template-id. LOCATION is the location
3379 of the type-specifier (TYPE) */
3381 static void
3382 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3383 tree type,
3384 enum tag_types tag_type,
3385 location_t location)
3387 cp_token_position start = 0;
3389 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3391 if (TREE_CODE (type) == TYPE_DECL)
3392 type = TREE_TYPE (type);
3393 if (TYPE_P (type) && !template_placeholder_p (type))
3394 error_at (location, "%qT is not a template", type);
3395 else if (identifier_p (type))
3397 if (tag_type != none_type)
3398 error_at (location, "%qE is not a class template", type);
3399 else
3400 error_at (location, "%qE is not a template", type);
3402 else
3403 error_at (location, "invalid template-id");
3404 /* Remember the location of the invalid "<". */
3405 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3406 start = cp_lexer_token_position (parser->lexer, true);
3407 /* Consume the "<". */
3408 cp_lexer_consume_token (parser->lexer);
3409 /* Parse the template arguments. */
3410 cp_parser_enclosed_template_argument_list (parser);
3411 /* Permanently remove the invalid template arguments so that
3412 this error message is not issued again. */
3413 if (start)
3414 cp_lexer_purge_tokens_after (parser->lexer, start);
3418 /* If parsing an integral constant-expression, issue an error message
3419 about the fact that THING appeared and return true. Otherwise,
3420 return false. In either case, set
3421 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3423 static bool
3424 cp_parser_non_integral_constant_expression (cp_parser *parser,
3425 non_integral_constant thing)
3427 parser->non_integral_constant_expression_p = true;
3428 if (parser->integral_constant_expression_p)
3430 if (!parser->allow_non_integral_constant_expression_p)
3432 const char *msg = NULL;
3433 switch (thing)
3435 case NIC_FLOAT:
3436 pedwarn (input_location, OPT_Wpedantic,
3437 "ISO C++ forbids using a floating-point literal "
3438 "in a constant-expression");
3439 return true;
3440 case NIC_CAST:
3441 error ("a cast to a type other than an integral or "
3442 "enumeration type cannot appear in a "
3443 "constant-expression");
3444 return true;
3445 case NIC_TYPEID:
3446 error ("%<typeid%> operator "
3447 "cannot appear in a constant-expression");
3448 return true;
3449 case NIC_NCC:
3450 error ("non-constant compound literals "
3451 "cannot appear in a constant-expression");
3452 return true;
3453 case NIC_FUNC_CALL:
3454 error ("a function call "
3455 "cannot appear in a constant-expression");
3456 return true;
3457 case NIC_INC:
3458 error ("an increment "
3459 "cannot appear in a constant-expression");
3460 return true;
3461 case NIC_DEC:
3462 error ("an decrement "
3463 "cannot appear in a constant-expression");
3464 return true;
3465 case NIC_ARRAY_REF:
3466 error ("an array reference "
3467 "cannot appear in a constant-expression");
3468 return true;
3469 case NIC_ADDR_LABEL:
3470 error ("the address of a label "
3471 "cannot appear in a constant-expression");
3472 return true;
3473 case NIC_OVERLOADED:
3474 error ("calls to overloaded operators "
3475 "cannot appear in a constant-expression");
3476 return true;
3477 case NIC_ASSIGNMENT:
3478 error ("an assignment cannot appear in a constant-expression");
3479 return true;
3480 case NIC_COMMA:
3481 error ("a comma operator "
3482 "cannot appear in a constant-expression");
3483 return true;
3484 case NIC_CONSTRUCTOR:
3485 error ("a call to a constructor "
3486 "cannot appear in a constant-expression");
3487 return true;
3488 case NIC_TRANSACTION:
3489 error ("a transaction expression "
3490 "cannot appear in a constant-expression");
3491 return true;
3492 case NIC_THIS:
3493 msg = "this";
3494 break;
3495 case NIC_FUNC_NAME:
3496 msg = "__FUNCTION__";
3497 break;
3498 case NIC_PRETTY_FUNC:
3499 msg = "__PRETTY_FUNCTION__";
3500 break;
3501 case NIC_C99_FUNC:
3502 msg = "__func__";
3503 break;
3504 case NIC_VA_ARG:
3505 msg = "va_arg";
3506 break;
3507 case NIC_ARROW:
3508 msg = "->";
3509 break;
3510 case NIC_POINT:
3511 msg = ".";
3512 break;
3513 case NIC_STAR:
3514 msg = "*";
3515 break;
3516 case NIC_ADDR:
3517 msg = "&";
3518 break;
3519 case NIC_PREINCREMENT:
3520 msg = "++";
3521 break;
3522 case NIC_PREDECREMENT:
3523 msg = "--";
3524 break;
3525 case NIC_NEW:
3526 msg = "new";
3527 break;
3528 case NIC_DEL:
3529 msg = "delete";
3530 break;
3531 default:
3532 gcc_unreachable ();
3534 if (msg)
3535 error ("%qs cannot appear in a constant-expression", msg);
3536 return true;
3539 return false;
3542 /* Emit a diagnostic for an invalid type name. This function commits
3543 to the current active tentative parse, if any. (Otherwise, the
3544 problematic construct might be encountered again later, resulting
3545 in duplicate error messages.) LOCATION is the location of ID. */
3547 static void
3548 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3549 location_t location)
3551 tree decl, ambiguous_decls;
3552 cp_parser_commit_to_tentative_parse (parser);
3553 /* Try to lookup the identifier. */
3554 decl = cp_parser_lookup_name (parser, id, none_type,
3555 /*is_template=*/false,
3556 /*is_namespace=*/false,
3557 /*check_dependency=*/true,
3558 &ambiguous_decls, location);
3559 if (ambiguous_decls)
3560 /* If the lookup was ambiguous, an error will already have
3561 been issued. */
3562 return;
3563 /* If the lookup found a template-name, it means that the user forgot
3564 to specify an argument list. Emit a useful error message. */
3565 if (DECL_TYPE_TEMPLATE_P (decl))
3567 auto_diagnostic_group d;
3568 error_at (location,
3569 "invalid use of template-name %qE without an argument list",
3570 decl);
3571 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3572 inform (location, "class template argument deduction is only available "
3573 "with %<-std=c++17%> or %<-std=gnu++17%>");
3574 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3576 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3577 error_at (location, "invalid use of destructor %qD as a type", id);
3578 else if (TREE_CODE (decl) == TYPE_DECL)
3579 /* Something like 'unsigned A a;' */
3580 error_at (location, "invalid combination of multiple type-specifiers");
3581 else if (!parser->scope)
3583 /* Issue an error message. */
3584 auto_diagnostic_group d;
3585 name_hint hint;
3586 if (TREE_CODE (id) == IDENTIFIER_NODE)
3587 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3588 if (const char *suggestion = hint.suggestion ())
3590 gcc_rich_location richloc (location);
3591 richloc.add_fixit_replace (suggestion);
3592 error_at (&richloc,
3593 "%qE does not name a type; did you mean %qs?",
3594 id, suggestion);
3596 else
3597 error_at (location, "%qE does not name a type", id);
3598 /* If we're in a template class, it's possible that the user was
3599 referring to a type from a base class. For example:
3601 template <typename T> struct A { typedef T X; };
3602 template <typename T> struct B : public A<T> { X x; };
3604 The user should have said "typename A<T>::X". */
3605 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3606 inform (location, "C++11 %<constexpr%> only available with "
3607 "%<-std=c++11%> or %<-std=gnu++11%>");
3608 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3609 inform (location, "C++11 %<noexcept%> only available with "
3610 "%<-std=c++11%> or %<-std=gnu++11%>");
3611 else if (TREE_CODE (id) == IDENTIFIER_NODE
3612 && (id_equal (id, "module") || id_equal (id, "import")))
3614 if (modules_p ())
3615 inform (location, "%qE is not recognized as a module control-line",
3616 id);
3617 else if (cxx_dialect < cxx20)
3618 inform (location, "C++20 %qE only available with %<-fmodules-ts%>",
3619 id);
3620 else
3621 inform (location, "C++20 %qE only available with %<-fmodules-ts%>"
3622 ", which is not yet enabled with %<-std=c++20%>", id);
3624 else if (cxx_dialect < cxx11
3625 && TREE_CODE (id) == IDENTIFIER_NODE
3626 && id_equal (id, "thread_local"))
3627 inform (location, "C++11 %<thread_local%> only available with "
3628 "%<-std=c++11%> or %<-std=gnu++11%>");
3629 else if (cxx_dialect < cxx20 && id == ridpointers[(int)RID_CONSTINIT])
3630 inform (location, "C++20 %<constinit%> only available with "
3631 "%<-std=c++20%> or %<-std=gnu++20%>");
3632 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3633 inform (location, "%<concept%> only available with %<-std=c++20%> or "
3634 "%<-fconcepts%>");
3635 else if (!flag_concepts && id == ridpointers[(int)RID_REQUIRES])
3636 inform (location, "%<requires%> only available with %<-std=c++20%> or "
3637 "%<-fconcepts%>");
3638 else if (processing_template_decl && current_class_type
3639 && TYPE_BINFO (current_class_type))
3641 for (tree b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3642 b; b = TREE_CHAIN (b))
3644 tree base_type = BINFO_TYPE (b);
3645 if (CLASS_TYPE_P (base_type)
3646 && dependent_type_p (base_type))
3648 /* Go from a particular instantiation of the
3649 template (which will have an empty TYPE_FIELDs),
3650 to the main version. */
3651 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3652 for (tree field = TYPE_FIELDS (base_type);
3653 field; field = DECL_CHAIN (field))
3654 if (TREE_CODE (field) == TYPE_DECL
3655 && DECL_NAME (field) == id)
3657 inform (location,
3658 "(perhaps %<typename %T::%E%> was intended)",
3659 BINFO_TYPE (b), id);
3660 goto found;
3664 found:;
3667 /* Here we diagnose qualified-ids where the scope is actually correct,
3668 but the identifier does not resolve to a valid type name. */
3669 else if (parser->scope != error_mark_node)
3671 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3673 auto_diagnostic_group d;
3674 name_hint hint;
3675 if (decl == error_mark_node)
3676 hint = suggest_alternative_in_explicit_scope (location, id,
3677 parser->scope);
3678 const char *suggestion = hint.suggestion ();
3679 gcc_rich_location richloc (location_of (id));
3680 if (suggestion)
3681 richloc.add_fixit_replace (suggestion);
3682 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3684 if (suggestion)
3685 error_at (&richloc,
3686 "%qE in namespace %qE does not name a template"
3687 " type; did you mean %qs?",
3688 id, parser->scope, suggestion);
3689 else
3690 error_at (&richloc,
3691 "%qE in namespace %qE does not name a template type",
3692 id, parser->scope);
3694 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3696 if (suggestion)
3697 error_at (&richloc,
3698 "%qE in namespace %qE does not name a template"
3699 " type; did you mean %qs?",
3700 TREE_OPERAND (id, 0), parser->scope, suggestion);
3701 else
3702 error_at (&richloc,
3703 "%qE in namespace %qE does not name a template"
3704 " type",
3705 TREE_OPERAND (id, 0), parser->scope);
3707 else
3709 if (suggestion)
3710 error_at (&richloc,
3711 "%qE in namespace %qE does not name a type"
3712 "; did you mean %qs?",
3713 id, parser->scope, suggestion);
3714 else
3715 error_at (&richloc,
3716 "%qE in namespace %qE does not name a type",
3717 id, parser->scope);
3719 if (DECL_P (decl))
3720 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3722 else if (CLASS_TYPE_P (parser->scope)
3723 && constructor_name_p (id, parser->scope))
3725 /* A<T>::A<T>() */
3726 auto_diagnostic_group d;
3727 error_at (location, "%<%T::%E%> names the constructor, not"
3728 " the type", parser->scope, id);
3729 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3730 error_at (location, "and %qT has no template constructors",
3731 parser->scope);
3733 else if (TYPE_P (parser->scope)
3734 && dependent_scope_p (parser->scope))
3736 gcc_rich_location richloc (location);
3737 richloc.add_fixit_insert_before ("typename ");
3738 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3739 error_at (&richloc,
3740 "need %<typename%> before %<%T::%D::%E%> because "
3741 "%<%T::%D%> is a dependent scope",
3742 TYPE_CONTEXT (parser->scope),
3743 TYPENAME_TYPE_FULLNAME (parser->scope),
3745 TYPE_CONTEXT (parser->scope),
3746 TYPENAME_TYPE_FULLNAME (parser->scope));
3747 else
3748 error_at (&richloc, "need %<typename%> before %<%T::%E%> because "
3749 "%qT is a dependent scope",
3750 parser->scope, id, parser->scope);
3752 else if (TYPE_P (parser->scope))
3754 auto_diagnostic_group d;
3755 if (!COMPLETE_TYPE_P (parser->scope))
3756 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3757 parser->scope);
3758 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3759 error_at (location_of (id),
3760 "%qE in %q#T does not name a template type",
3761 id, parser->scope);
3762 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3763 error_at (location_of (id),
3764 "%qE in %q#T does not name a template type",
3765 TREE_OPERAND (id, 0), parser->scope);
3766 else
3767 error_at (location_of (id),
3768 "%qE in %q#T does not name a type",
3769 id, parser->scope);
3770 if (DECL_P (decl))
3771 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3773 else
3774 gcc_unreachable ();
3778 /* Check for a common situation where a type-name should be present,
3779 but is not, and issue a sensible error message. Returns true if an
3780 invalid type-name was detected.
3782 The situation handled by this function are variable declarations of the
3783 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3784 Usually, `ID' should name a type, but if we got here it means that it
3785 does not. We try to emit the best possible error message depending on
3786 how exactly the id-expression looks like. */
3788 static bool
3789 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3791 tree id;
3792 cp_token *token = cp_lexer_peek_token (parser->lexer);
3794 /* Avoid duplicate error about ambiguous lookup. */
3795 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3797 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3798 if (next->type == CPP_NAME && next->error_reported)
3799 goto out;
3802 cp_parser_parse_tentatively (parser);
3803 id = cp_parser_id_expression (parser,
3804 /*template_keyword_p=*/false,
3805 /*check_dependency_p=*/true,
3806 /*template_p=*/NULL,
3807 /*declarator_p=*/true,
3808 /*optional_p=*/false);
3809 /* If the next token is a (, this is a function with no explicit return
3810 type, i.e. constructor, destructor or conversion op. */
3811 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3812 || TREE_CODE (id) == TYPE_DECL)
3814 cp_parser_abort_tentative_parse (parser);
3815 return false;
3817 if (!cp_parser_parse_definitely (parser))
3818 return false;
3820 /* Emit a diagnostic for the invalid type. */
3821 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3822 out:
3823 /* If we aren't in the middle of a declarator (i.e. in a
3824 parameter-declaration-clause), skip to the end of the declaration;
3825 there's no point in trying to process it. */
3826 if (!parser->in_declarator_p)
3827 cp_parser_skip_to_end_of_block_or_statement (parser);
3828 return true;
3831 /* Consume tokens up to, and including, the next non-nested closing `)'.
3832 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3833 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3834 found an unnested token of that type. */
3836 static int
3837 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3838 bool recovering,
3839 cpp_ttype or_ttype,
3840 bool consume_paren)
3842 unsigned paren_depth = 0;
3843 unsigned brace_depth = 0;
3844 unsigned square_depth = 0;
3845 unsigned condop_depth = 0;
3847 if (recovering && or_ttype == CPP_EOF
3848 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3849 return 0;
3851 while (true)
3853 cp_token * token = cp_lexer_peek_token (parser->lexer);
3855 /* Have we found what we're looking for before the closing paren? */
3856 if (token->type == or_ttype && or_ttype != CPP_EOF
3857 && !brace_depth && !paren_depth && !square_depth && !condop_depth)
3858 return -1;
3860 switch (token->type)
3862 case CPP_PRAGMA_EOL:
3863 if (!parser->lexer->in_pragma)
3864 break;
3865 /* FALLTHRU */
3866 case CPP_EOF:
3867 /* If we've run out of tokens, then there is no closing `)'. */
3868 return 0;
3870 /* This is good for lambda expression capture-lists. */
3871 case CPP_OPEN_SQUARE:
3872 ++square_depth;
3873 break;
3874 case CPP_CLOSE_SQUARE:
3875 if (!square_depth--)
3876 return 0;
3877 break;
3879 case CPP_SEMICOLON:
3880 /* This matches the processing in skip_to_end_of_statement. */
3881 if (!brace_depth)
3882 return 0;
3883 break;
3885 case CPP_OPEN_BRACE:
3886 ++brace_depth;
3887 break;
3888 case CPP_CLOSE_BRACE:
3889 if (!brace_depth--)
3890 return 0;
3891 break;
3893 case CPP_OPEN_PAREN:
3894 if (!brace_depth)
3895 ++paren_depth;
3896 break;
3898 case CPP_CLOSE_PAREN:
3899 if (!brace_depth && !paren_depth--)
3901 if (consume_paren)
3902 cp_lexer_consume_token (parser->lexer);
3903 return 1;
3905 break;
3907 case CPP_QUERY:
3908 if (!brace_depth && !paren_depth && !square_depth)
3909 ++condop_depth;
3910 break;
3912 case CPP_COLON:
3913 if (!brace_depth && !paren_depth && !square_depth && condop_depth > 0)
3914 condop_depth--;
3915 break;
3917 case CPP_KEYWORD:
3918 if (!cp_token_is_module_directive (token))
3919 break;
3920 /* FALLTHROUGH */
3922 case CPP_PRAGMA:
3923 /* We fell into a pragma. Skip it, and continue. */
3924 cp_parser_skip_to_pragma_eol (parser, recovering ? token : nullptr);
3925 continue;
3927 default:
3928 break;
3931 /* Consume the token. */
3932 cp_lexer_consume_token (parser->lexer);
3936 /* Consume tokens up to, and including, the next non-nested closing `)'.
3937 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3938 are doing error recovery. Returns -1 if OR_COMMA is true and we
3939 found an unnested token of that type. */
3941 static int
3942 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3943 bool recovering,
3944 bool or_comma,
3945 bool consume_paren)
3947 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3948 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3949 ttype, consume_paren);
3952 /* Consume tokens until we reach the end of the current statement.
3953 Normally, that will be just before consuming a `;'. However, if a
3954 non-nested `}' comes first, then we stop before consuming that. */
3956 static void
3957 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3959 unsigned nesting_depth = 0;
3961 /* Unwind generic function template scope if necessary. */
3962 if (parser->fully_implicit_function_template_p)
3963 abort_fully_implicit_template (parser);
3965 while (true)
3967 cp_token *token = cp_lexer_peek_token (parser->lexer);
3969 switch (token->type)
3971 case CPP_PRAGMA_EOL:
3972 if (!parser->lexer->in_pragma)
3973 break;
3974 /* FALLTHRU */
3975 case CPP_EOF:
3976 /* If we've run out of tokens, stop. */
3977 return;
3979 case CPP_SEMICOLON:
3980 /* If the next token is a `;', we have reached the end of the
3981 statement. */
3982 if (!nesting_depth)
3983 return;
3984 break;
3986 case CPP_CLOSE_BRACE:
3987 /* If this is a non-nested '}', stop before consuming it.
3988 That way, when confronted with something like:
3990 { 3 + }
3992 we stop before consuming the closing '}', even though we
3993 have not yet reached a `;'. */
3994 if (nesting_depth == 0)
3995 return;
3997 /* If it is the closing '}' for a block that we have
3998 scanned, stop -- but only after consuming the token.
3999 That way given:
4001 void f g () { ... }
4002 typedef int I;
4004 we will stop after the body of the erroneously declared
4005 function, but before consuming the following `typedef'
4006 declaration. */
4007 if (--nesting_depth == 0)
4009 cp_lexer_consume_token (parser->lexer);
4010 return;
4012 break;
4014 case CPP_OPEN_BRACE:
4015 ++nesting_depth;
4016 break;
4018 case CPP_KEYWORD:
4019 if (!cp_token_is_module_directive (token))
4020 break;
4021 /* FALLTHROUGH */
4023 case CPP_PRAGMA:
4024 /* We fell into a pragma. Skip it, and continue or return. */
4025 cp_parser_skip_to_pragma_eol (parser, token);
4026 if (!nesting_depth)
4027 return;
4028 continue;
4030 default:
4031 break;
4034 /* Consume the token. */
4035 cp_lexer_consume_token (parser->lexer);
4039 /* This function is called at the end of a statement or declaration.
4040 If the next token is a semicolon, it is consumed; otherwise, error
4041 recovery is attempted. */
4043 static void
4044 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
4046 /* Look for the trailing `;'. */
4047 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
4049 /* If there is additional (erroneous) input, skip to the end of
4050 the statement. */
4051 cp_parser_skip_to_end_of_statement (parser);
4052 /* If the next token is now a `;', consume it. */
4053 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
4054 cp_lexer_consume_token (parser->lexer);
4058 /* Skip tokens until we have consumed an entire block, or until we
4059 have consumed a non-nested `;'. */
4061 static void
4062 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
4064 int nesting_depth = 0;
4066 /* Unwind generic function template scope if necessary. */
4067 if (parser->fully_implicit_function_template_p)
4068 abort_fully_implicit_template (parser);
4070 while (nesting_depth >= 0)
4072 cp_token *token = cp_lexer_peek_token (parser->lexer);
4074 switch (token->type)
4076 case CPP_PRAGMA_EOL:
4077 if (!parser->lexer->in_pragma)
4078 break;
4079 /* FALLTHRU */
4080 case CPP_EOF:
4081 /* If we've run out of tokens, stop. */
4082 return;
4084 case CPP_SEMICOLON:
4085 /* Stop if this is an unnested ';'. */
4086 if (!nesting_depth)
4087 nesting_depth = -1;
4088 break;
4090 case CPP_CLOSE_BRACE:
4091 /* Stop if this is an unnested '}', or closes the outermost
4092 nesting level. */
4093 nesting_depth--;
4094 if (nesting_depth < 0)
4095 return;
4096 if (!nesting_depth)
4097 nesting_depth = -1;
4098 break;
4100 case CPP_OPEN_BRACE:
4101 /* Nest. */
4102 nesting_depth++;
4103 break;
4105 case CPP_KEYWORD:
4106 if (!cp_token_is_module_directive (token))
4107 break;
4108 /* FALLTHROUGH */
4110 case CPP_PRAGMA:
4111 /* Skip it, and continue or return. */
4112 cp_parser_skip_to_pragma_eol (parser, token);
4113 if (!nesting_depth)
4114 return;
4115 continue;
4117 default:
4118 break;
4121 /* Consume the token. */
4122 cp_lexer_consume_token (parser->lexer);
4126 /* Skip tokens until a non-nested closing curly brace is the next
4127 token, or there are no more tokens. Return true in the first case,
4128 false otherwise. */
4130 static bool
4131 cp_parser_skip_to_closing_brace (cp_parser *parser)
4133 unsigned nesting_depth = 0;
4135 while (true)
4137 cp_token *token = cp_lexer_peek_token (parser->lexer);
4139 switch (token->type)
4141 case CPP_PRAGMA_EOL:
4142 if (!parser->lexer->in_pragma)
4143 break;
4144 /* FALLTHRU */
4145 case CPP_EOF:
4146 /* If we've run out of tokens, stop. */
4147 return false;
4149 case CPP_CLOSE_BRACE:
4150 /* If the next token is a non-nested `}', then we have reached
4151 the end of the current block. */
4152 if (nesting_depth-- == 0)
4153 return true;
4154 break;
4156 case CPP_OPEN_BRACE:
4157 /* If it the next token is a `{', then we are entering a new
4158 block. Consume the entire block. */
4159 ++nesting_depth;
4160 break;
4162 default:
4163 break;
4166 /* Consume the token. */
4167 cp_lexer_consume_token (parser->lexer);
4171 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
4172 parameter is the PRAGMA token, allowing us to purge the entire pragma
4173 sequence. PRAGMA_TOK can be NULL, if we're speculatively scanning
4174 forwards (not error recovery). */
4176 static void
4177 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
4179 cp_token *token;
4183 /* The preprocessor makes sure that a PRAGMA_EOL token appears
4184 before an EOF token, even when the EOF is on the pragma line.
4185 We should never get here without being inside a deferred
4186 pragma. */
4187 gcc_checking_assert (cp_lexer_next_token_is_not (parser->lexer, CPP_EOF));
4188 token = cp_lexer_consume_token (parser->lexer);
4190 while (token->type != CPP_PRAGMA_EOL);
4192 if (pragma_tok)
4194 parser->lexer->in_pragma = false;
4195 if (parser->lexer->in_omp_attribute_pragma
4196 && cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4198 parser->lexer = parser->lexer->next;
4199 /* Put the current source position back where it was before this
4200 lexer was pushed. */
4201 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4206 /* Require pragma end of line, resyncing with it as necessary. The
4207 arguments are as for cp_parser_skip_to_pragma_eol. */
4209 static void
4210 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
4212 parser->lexer->in_pragma = false;
4213 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
4214 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
4215 else if (parser->lexer->in_omp_attribute_pragma
4216 && cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4218 parser->lexer = parser->lexer->next;
4219 /* Put the current source position back where it was before this
4220 lexer was pushed. */
4221 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4225 /* This is a simple wrapper around make_typename_type. When the id is
4226 an unresolved identifier node, we can provide a superior diagnostic
4227 using cp_parser_diagnose_invalid_type_name. */
4229 static tree
4230 cp_parser_make_typename_type (cp_parser *parser, tree id,
4231 location_t id_location)
4233 tree result;
4234 if (identifier_p (id))
4236 result = make_typename_type (parser->scope, id, typename_type,
4237 /*complain=*/tf_none);
4238 if (result == error_mark_node)
4239 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
4240 return result;
4242 return make_typename_type (parser->scope, id, typename_type, tf_error);
4245 /* This is a wrapper around the
4246 make_{pointer,ptrmem,reference}_declarator functions that decides
4247 which one to call based on the CODE and CLASS_TYPE arguments. The
4248 CODE argument should be one of the values returned by
4249 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
4250 appertain to the pointer or reference. */
4252 static cp_declarator *
4253 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
4254 cp_cv_quals cv_qualifiers,
4255 cp_declarator *target,
4256 tree attributes)
4258 if (code == ERROR_MARK || target == cp_error_declarator)
4259 return cp_error_declarator;
4261 if (code == INDIRECT_REF)
4262 if (class_type == NULL_TREE)
4263 return make_pointer_declarator (cv_qualifiers, target, attributes);
4264 else
4265 return make_ptrmem_declarator (cv_qualifiers, class_type,
4266 target, attributes);
4267 else if (code == ADDR_EXPR && class_type == NULL_TREE)
4268 return make_reference_declarator (cv_qualifiers, target,
4269 false, attributes);
4270 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
4271 return make_reference_declarator (cv_qualifiers, target,
4272 true, attributes);
4273 gcc_unreachable ();
4276 /* Create a new C++ parser. */
4278 static cp_parser *
4279 cp_parser_new (cp_lexer *lexer)
4281 /* Initialize the binops_by_token so that we can get the tree
4282 directly from the token. */
4283 for (unsigned i = 0; i < ARRAY_SIZE (binops); i++)
4284 binops_by_token[binops[i].token_type] = binops[i];
4286 cp_parser *parser = ggc_cleared_alloc<cp_parser> ();
4287 parser->lexer = lexer;
4288 parser->context = cp_parser_context_new (NULL);
4290 /* For now, we always accept GNU extensions. */
4291 parser->allow_gnu_extensions_p = 1;
4293 /* The `>' token is a greater-than operator, not the end of a
4294 template-id. */
4295 parser->greater_than_is_operator_p = true;
4297 parser->default_arg_ok_p = true;
4299 /* We are not parsing a constant-expression. */
4300 parser->integral_constant_expression_p = false;
4301 parser->allow_non_integral_constant_expression_p = false;
4302 parser->non_integral_constant_expression_p = false;
4304 /* Local variable names are not forbidden. */
4305 parser->local_variables_forbidden_p = 0;
4307 /* We are not processing an `extern "C"' declaration. */
4308 parser->in_unbraced_linkage_specification_p = false;
4310 /* We are not processing a declarator. */
4311 parser->in_declarator_p = false;
4313 /* We are not processing a template-argument-list. */
4314 parser->in_template_argument_list_p = false;
4316 /* We are not in an iteration statement. */
4317 parser->in_statement = 0;
4319 /* We are not in a switch statement. */
4320 parser->in_switch_statement_p = false;
4322 /* We are not parsing a type-id inside an expression. */
4323 parser->in_type_id_in_expr_p = false;
4325 /* String literals should be translated to the execution character set. */
4326 parser->translate_strings_p = true;
4328 /* We are not parsing a function body. */
4329 parser->in_function_body = false;
4331 /* We can correct until told otherwise. */
4332 parser->colon_corrects_to_scope_p = true;
4334 /* The unparsed function queue is empty. */
4335 push_unparsed_function_queues (parser);
4337 /* There are no classes being defined. */
4338 parser->num_classes_being_defined = 0;
4340 /* No template parameters apply. */
4341 parser->num_template_parameter_lists = 0;
4343 /* Special parsing data structures. */
4344 parser->omp_declare_simd = NULL;
4345 parser->oacc_routine = NULL;
4347 /* Not declaring an implicit function template. */
4348 parser->auto_is_implicit_function_template_parm_p = false;
4349 parser->fully_implicit_function_template_p = false;
4350 parser->implicit_template_parms = 0;
4351 parser->implicit_template_scope = 0;
4353 /* Allow constrained-type-specifiers. */
4354 parser->prevent_constrained_type_specifiers = 0;
4356 /* We haven't yet seen an 'extern "C"'. */
4357 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
4359 return parser;
4362 /* Create a cp_lexer structure which will emit the tokens in CACHE
4363 and push it onto the parser's lexer stack. This is used for delayed
4364 parsing of in-class method bodies and default arguments, and should
4365 not be confused with tentative parsing. */
4366 static void
4367 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
4369 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
4370 lexer->next = parser->lexer;
4371 parser->lexer = lexer;
4373 /* Move the current source position to that of the first token in the
4374 new lexer. */
4375 cp_lexer_set_source_position_from_token (lexer->next_token);
4378 /* Pop the top lexer off the parser stack. This is never used for the
4379 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
4380 static void
4381 cp_parser_pop_lexer (cp_parser *parser)
4383 cp_lexer *lexer = parser->lexer;
4384 parser->lexer = lexer->next;
4385 cp_lexer_destroy (lexer);
4387 /* Put the current source position back where it was before this
4388 lexer was pushed. */
4389 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4392 /* Lexical conventions [gram.lex] */
4394 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
4395 identifier. */
4397 static cp_expr
4398 cp_parser_identifier (cp_parser* parser)
4400 cp_token *token;
4402 /* Look for the identifier. */
4403 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
4404 /* Return the value. */
4405 if (token)
4406 return cp_expr (token->u.value, token->location);
4407 else
4408 return error_mark_node;
4411 /* Parse a sequence of adjacent string constants. Returns a
4412 TREE_STRING representing the combined, nul-terminated string
4413 constant. If TRANSLATE is true, translate the string to the
4414 execution character set. If WIDE_OK is true, a wide string is
4415 invalid here.
4417 C++98 [lex.string] says that if a narrow string literal token is
4418 adjacent to a wide string literal token, the behavior is undefined.
4419 However, C99 6.4.5p4 says that this results in a wide string literal.
4420 We follow C99 here, for consistency with the C front end.
4422 This code is largely lifted from lex_string() in c-lex.cc.
4424 FUTURE: ObjC++ will need to handle @-strings here. */
4425 static cp_expr
4426 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
4427 bool lookup_udlit = true)
4429 tree value;
4430 size_t count;
4431 struct obstack str_ob;
4432 struct obstack loc_ob;
4433 cpp_string str, istr, *strs;
4434 cp_token *tok;
4435 enum cpp_ttype type, curr_type;
4436 int have_suffix_p = 0;
4437 tree string_tree;
4438 tree suffix_id = NULL_TREE;
4439 bool curr_tok_is_userdef_p = false;
4441 tok = cp_lexer_peek_token (parser->lexer);
4442 if (!cp_parser_is_string_literal (tok))
4444 cp_parser_error (parser, "expected string-literal");
4445 return error_mark_node;
4448 location_t loc = tok->location;
4450 if (cpp_userdef_string_p (tok->type))
4452 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4453 curr_type = cpp_userdef_string_remove_type (tok->type);
4454 curr_tok_is_userdef_p = true;
4456 else
4458 string_tree = tok->u.value;
4459 curr_type = tok->type;
4461 type = curr_type;
4463 /* Try to avoid the overhead of creating and destroying an obstack
4464 for the common case of just one string. */
4465 if (!cp_parser_is_string_literal
4466 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4468 cp_lexer_consume_token (parser->lexer);
4470 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4471 str.len = TREE_STRING_LENGTH (string_tree);
4472 count = 1;
4474 if (curr_tok_is_userdef_p)
4476 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4477 have_suffix_p = 1;
4478 curr_type = cpp_userdef_string_remove_type (tok->type);
4480 else
4481 curr_type = tok->type;
4483 strs = &str;
4485 else
4487 location_t last_tok_loc = tok->location;
4488 gcc_obstack_init (&str_ob);
4489 gcc_obstack_init (&loc_ob);
4490 count = 0;
4494 cp_lexer_consume_token (parser->lexer);
4495 count++;
4496 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4497 str.len = TREE_STRING_LENGTH (string_tree);
4499 if (curr_tok_is_userdef_p)
4501 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4502 if (have_suffix_p == 0)
4504 suffix_id = curr_suffix_id;
4505 have_suffix_p = 1;
4507 else if (have_suffix_p == 1
4508 && curr_suffix_id != suffix_id)
4510 error ("inconsistent user-defined literal suffixes"
4511 " %qD and %qD in string literal",
4512 suffix_id, curr_suffix_id);
4513 have_suffix_p = -1;
4515 curr_type = cpp_userdef_string_remove_type (tok->type);
4517 else
4518 curr_type = tok->type;
4520 if (type != curr_type)
4522 if (type == CPP_STRING)
4523 type = curr_type;
4524 else if (curr_type != CPP_STRING)
4526 rich_location rich_loc (line_table, tok->location);
4527 rich_loc.add_range (last_tok_loc);
4528 error_at (&rich_loc,
4529 "concatenation of string literals with "
4530 "conflicting encoding prefixes");
4534 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4535 obstack_grow (&loc_ob, &tok->location, sizeof (location_t));
4537 last_tok_loc = tok->location;
4539 tok = cp_lexer_peek_token (parser->lexer);
4540 if (cpp_userdef_string_p (tok->type))
4542 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4543 curr_type = cpp_userdef_string_remove_type (tok->type);
4544 curr_tok_is_userdef_p = true;
4546 else
4548 string_tree = tok->u.value;
4549 curr_type = tok->type;
4550 curr_tok_is_userdef_p = false;
4553 while (cp_parser_is_string_literal (tok));
4555 /* A string literal built by concatenation has its caret=start at
4556 the start of the initial string, and its finish at the finish of
4557 the final string literal. */
4558 loc = make_location (loc, loc, get_finish (last_tok_loc));
4560 strs = (cpp_string *) obstack_finish (&str_ob);
4563 if (type != CPP_STRING && !wide_ok)
4565 cp_parser_error (parser, "a wide string is invalid in this context");
4566 type = CPP_STRING;
4569 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4570 (parse_in, strs, count, &istr, type))
4572 value = build_string (istr.len, (const char *)istr.text);
4573 free (CONST_CAST (unsigned char *, istr.text));
4574 if (count > 1)
4576 location_t *locs = (location_t *)obstack_finish (&loc_ob);
4577 gcc_assert (g_string_concat_db);
4578 g_string_concat_db->record_string_concatenation (count, locs);
4581 switch (type)
4583 default:
4584 case CPP_STRING:
4585 TREE_TYPE (value) = char_array_type_node;
4586 break;
4587 case CPP_UTF8STRING:
4588 if (flag_char8_t)
4589 TREE_TYPE (value) = char8_array_type_node;
4590 else
4591 TREE_TYPE (value) = char_array_type_node;
4592 break;
4593 case CPP_STRING16:
4594 TREE_TYPE (value) = char16_array_type_node;
4595 break;
4596 case CPP_STRING32:
4597 TREE_TYPE (value) = char32_array_type_node;
4598 break;
4599 case CPP_WSTRING:
4600 TREE_TYPE (value) = wchar_array_type_node;
4601 break;
4604 value = fix_string_type (value);
4606 if (have_suffix_p)
4608 tree literal = build_userdef_literal (suffix_id, value,
4609 OT_NONE, NULL_TREE);
4610 if (lookup_udlit)
4611 value = cp_parser_userdef_string_literal (literal);
4612 else
4613 value = literal;
4616 else
4617 /* cpp_interpret_string has issued an error. */
4618 value = error_mark_node;
4620 if (count > 1)
4622 obstack_free (&str_ob, 0);
4623 obstack_free (&loc_ob, 0);
4626 return cp_expr (value, loc);
4629 /* Look up a literal operator with the name and the exact arguments. */
4631 static tree
4632 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4634 tree decl = lookup_name (name);
4635 if (!decl || !is_overloaded_fn (decl))
4636 return error_mark_node;
4638 for (lkp_iterator iter (decl); iter; ++iter)
4640 tree fn = *iter;
4642 if (tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn)))
4644 unsigned int ix;
4645 bool found = true;
4647 for (ix = 0;
4648 found && ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4649 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4651 tree tparm = TREE_VALUE (parmtypes);
4652 tree targ = TREE_TYPE ((*args)[ix]);
4653 bool ptr = TYPE_PTR_P (tparm);
4654 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4655 if ((ptr || arr || !same_type_p (tparm, targ))
4656 && (!ptr || !arr
4657 || !same_type_p (TREE_TYPE (tparm),
4658 TREE_TYPE (targ))))
4659 found = false;
4662 if (found
4663 && ix == vec_safe_length (args)
4664 /* May be this should be sufficient_parms_p instead,
4665 depending on how exactly should user-defined literals
4666 work in presence of default arguments on the literal
4667 operator parameters. */
4668 && parmtypes == void_list_node)
4669 return decl;
4673 return error_mark_node;
4676 /* Parse a user-defined char constant. Returns a call to a user-defined
4677 literal operator taking the character as an argument. */
4679 static cp_expr
4680 cp_parser_userdef_char_literal (cp_parser *parser)
4682 cp_token *token = cp_lexer_consume_token (parser->lexer);
4683 tree literal = token->u.value;
4684 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4685 tree value = USERDEF_LITERAL_VALUE (literal);
4686 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4687 tree decl, result;
4689 /* Build up a call to the user-defined operator */
4690 /* Lookup the name we got back from the id-expression. */
4691 releasing_vec args;
4692 vec_safe_push (args, value);
4693 decl = lookup_literal_operator (name, args);
4694 if (!decl || decl == error_mark_node)
4696 error ("unable to find character literal operator %qD with %qT argument",
4697 name, TREE_TYPE (value));
4698 return error_mark_node;
4700 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4701 return result;
4704 /* A subroutine of cp_parser_userdef_numeric_literal to
4705 create a char... template parameter pack from a string node. */
4707 static tree
4708 make_char_string_pack (tree value)
4710 tree charvec;
4711 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4712 const unsigned char *str
4713 = (const unsigned char *) TREE_STRING_POINTER (value);
4714 int i, len = TREE_STRING_LENGTH (value) - 1;
4715 tree argvec = make_tree_vec (1);
4717 /* Fill in CHARVEC with all of the parameters. */
4718 charvec = make_tree_vec (len);
4719 for (i = 0; i < len; ++i)
4721 unsigned char s[3] = { '\'', str[i], '\'' };
4722 cpp_string in = { 3, s };
4723 cpp_string out = { 0, 0 };
4724 if (!cpp_interpret_string (parse_in, &in, 1, &out, CPP_STRING))
4725 return NULL_TREE;
4726 gcc_assert (out.len == 2);
4727 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node,
4728 out.text[0]);
4731 /* Build the argument packs. */
4732 ARGUMENT_PACK_ARGS (argpack) = charvec;
4734 TREE_VEC_ELT (argvec, 0) = argpack;
4736 return argvec;
4739 /* A subroutine of cp_parser_userdef_numeric_literal to
4740 create a char... template parameter pack from a string node. */
4742 static tree
4743 make_string_pack (tree value)
4745 tree charvec;
4746 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4747 const unsigned char *str
4748 = (const unsigned char *) TREE_STRING_POINTER (value);
4749 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4750 int len = TREE_STRING_LENGTH (value) / sz - 1;
4751 tree argvec = make_tree_vec (2);
4753 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4754 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4756 /* First template parm is character type. */
4757 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4759 /* Fill in CHARVEC with all of the parameters. */
4760 charvec = make_tree_vec (len);
4761 for (int i = 0; i < len; ++i)
4762 TREE_VEC_ELT (charvec, i)
4763 = double_int_to_tree (str_char_type_node,
4764 double_int::from_buffer (str + i * sz, sz));
4766 /* Build the argument packs. */
4767 ARGUMENT_PACK_ARGS (argpack) = charvec;
4769 TREE_VEC_ELT (argvec, 1) = argpack;
4771 return argvec;
4774 /* Parse a user-defined numeric constant. returns a call to a user-defined
4775 literal operator. */
4777 static cp_expr
4778 cp_parser_userdef_numeric_literal (cp_parser *parser)
4780 cp_token *token = cp_lexer_consume_token (parser->lexer);
4781 tree literal = token->u.value;
4782 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4783 tree value = USERDEF_LITERAL_VALUE (literal);
4784 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4785 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4786 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4787 tree decl, result;
4789 /* Look for a literal operator taking the exact type of numeric argument
4790 as the literal value. */
4791 releasing_vec args;
4792 vec_safe_push (args, value);
4793 decl = lookup_literal_operator (name, args);
4794 if (decl && decl != error_mark_node)
4796 result = finish_call_expr (decl, &args, false, true,
4797 tf_warning_or_error);
4799 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4801 warning_at (token->location, OPT_Woverflow,
4802 "integer literal exceeds range of %qT type",
4803 long_long_unsigned_type_node);
4805 else
4807 if (overflow > 0)
4808 warning_at (token->location, OPT_Woverflow,
4809 "floating literal exceeds range of %qT type",
4810 long_double_type_node);
4811 else if (overflow < 0)
4812 warning_at (token->location, OPT_Woverflow,
4813 "floating literal truncated to zero");
4816 return result;
4819 /* If the numeric argument didn't work, look for a raw literal
4820 operator taking a const char* argument consisting of the number
4821 in string format. */
4822 args->truncate (0);
4823 vec_safe_push (args, num_string);
4824 decl = lookup_literal_operator (name, args);
4825 if (decl && decl != error_mark_node)
4827 result = finish_call_expr (decl, &args, false, true,
4828 tf_warning_or_error);
4829 return result;
4832 /* If the raw literal didn't work, look for a non-type template
4833 function with parameter pack char.... Call the function with
4834 template parameter characters representing the number. */
4835 args->truncate (0);
4836 decl = lookup_literal_operator (name, args);
4837 if (decl && decl != error_mark_node)
4839 tree tmpl_args = make_char_string_pack (num_string);
4840 if (tmpl_args == NULL_TREE)
4842 error ("failed to translate literal to execution character set %qT",
4843 num_string);
4844 return error_mark_node;
4846 decl = lookup_template_function (decl, tmpl_args);
4847 result = finish_call_expr (decl, &args, false, true,
4848 tf_warning_or_error);
4849 return result;
4852 /* In C++14 the standard library defines complex number suffixes that
4853 conflict with GNU extensions. Prefer them if <complex> is #included. */
4854 bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
4855 bool i14 = (cxx_dialect > cxx11
4856 && (id_equal (suffix_id, "i")
4857 || id_equal (suffix_id, "if")
4858 || id_equal (suffix_id, "il")));
4859 diagnostic_t kind = DK_ERROR;
4860 int opt = 0;
4862 if (i14 && ext)
4864 tree cxlit = lookup_qualified_name (std_node, "complex_literals",
4865 LOOK_want::NORMAL, false);
4866 if (cxlit == error_mark_node)
4868 /* No <complex>, so pedwarn and use GNU semantics. */
4869 kind = DK_PEDWARN;
4870 opt = OPT_Wpedantic;
4874 bool complained
4875 = emit_diagnostic (kind, input_location, opt,
4876 "unable to find numeric literal operator %qD", name);
4878 if (!complained)
4879 /* Don't inform either. */;
4880 else if (i14)
4882 inform (token->location, "add %<using namespace std::complex_literals%> "
4883 "(from %<<complex>%>) to enable the C++14 user-defined literal "
4884 "suffixes");
4885 if (ext)
4886 inform (token->location, "or use %<j%> instead of %<i%> for the "
4887 "GNU built-in suffix");
4889 else if (!ext)
4890 inform (token->location, "use %<-fext-numeric-literals%> "
4891 "to enable more built-in suffixes");
4893 if (kind == DK_ERROR)
4894 value = error_mark_node;
4895 else
4897 /* Use the built-in semantics. */
4898 tree type;
4899 if (id_equal (suffix_id, "i"))
4901 if (TREE_CODE (value) == INTEGER_CST)
4902 type = integer_type_node;
4903 else
4904 type = double_type_node;
4906 else if (id_equal (suffix_id, "if"))
4907 type = float_type_node;
4908 else /* if (id_equal (suffix_id, "il")) */
4909 type = long_double_type_node;
4911 value = fold_build2 (COMPLEX_EXPR, build_complex_type (type),
4912 build_zero_cst (type), fold_convert (type, value));
4915 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4916 /* Avoid repeated diagnostics. */
4917 token->u.value = value;
4918 return value;
4921 /* Parse a user-defined string constant. Returns a call to a user-defined
4922 literal operator taking a character pointer and the length of the string
4923 as arguments. */
4925 static tree
4926 cp_parser_userdef_string_literal (tree literal)
4928 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4929 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4930 tree value = USERDEF_LITERAL_VALUE (literal);
4931 int len = TREE_STRING_LENGTH (value)
4932 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4933 tree decl;
4935 /* Build up a call to the user-defined operator. */
4936 /* Lookup the name we got back from the id-expression. */
4937 releasing_vec args;
4938 vec_safe_push (args, value);
4939 vec_safe_push (args, build_int_cst (size_type_node, len));
4940 decl = lookup_literal_operator (name, args);
4942 if (decl && decl != error_mark_node)
4943 return finish_call_expr (decl, &args, false, true,
4944 tf_warning_or_error);
4946 /* Look for a suitable template function, either (C++20) with a single
4947 parameter of class type, or (N3599) with typename parameter CharT and
4948 parameter pack CharT... */
4949 args->truncate (0);
4950 decl = lookup_literal_operator (name, args);
4951 if (decl && decl != error_mark_node)
4953 /* Use resolve_nondeduced_context to try to choose one form of template
4954 or the other. */
4955 tree tmpl_args = make_tree_vec (1);
4956 TREE_VEC_ELT (tmpl_args, 0) = value;
4957 decl = lookup_template_function (decl, tmpl_args);
4958 tree res = resolve_nondeduced_context (decl, tf_none);
4959 if (DECL_P (res))
4960 decl = res;
4961 else
4963 TREE_OPERAND (decl, 1) = make_string_pack (value);
4964 res = resolve_nondeduced_context (decl, tf_none);
4965 if (DECL_P (res))
4966 decl = res;
4968 if (!DECL_P (decl) && cxx_dialect > cxx17)
4969 TREE_OPERAND (decl, 1) = tmpl_args;
4970 return finish_call_expr (decl, &args, false, true,
4971 tf_warning_or_error);
4974 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4975 name, TREE_TYPE (value), size_type_node);
4976 return error_mark_node;
4980 /* Basic concepts [gram.basic] */
4982 /* Parse a translation-unit.
4984 translation-unit:
4985 declaration-seq [opt] */
4987 static void
4988 cp_parser_translation_unit (cp_parser* parser)
4990 gcc_checking_assert (!cp_error_declarator);
4992 /* Create the declarator obstack. */
4993 gcc_obstack_init (&declarator_obstack);
4994 /* Create the error declarator. */
4995 cp_error_declarator = make_declarator (cdk_error);
4996 /* Create the empty parameter list. */
4997 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
4998 UNKNOWN_LOCATION);
4999 /* Remember where the base of the declarator obstack lies. */
5000 void *declarator_obstack_base = obstack_next_free (&declarator_obstack);
5002 push_deferring_access_checks (flag_access_control
5003 ? dk_no_deferred : dk_no_check);
5005 module_parse mp_state = MP_NOT_MODULE;
5006 if (modules_p () && !header_module_p ())
5007 mp_state = MP_FIRST;
5009 bool implicit_extern_c = false;
5011 /* Parse until EOF. */
5012 for (;;)
5014 cp_token *token = cp_lexer_peek_token (parser->lexer);
5016 /* If we're entering or exiting a region that's implicitly
5017 extern "C", modify the lang context appropriately. This is
5018 so horrible. Please die. */
5019 if (implicit_extern_c
5020 != cp_lexer_peek_token (parser->lexer)->implicit_extern_c)
5022 implicit_extern_c = !implicit_extern_c;
5023 if (implicit_extern_c)
5024 push_lang_context (lang_name_c);
5025 else
5026 pop_lang_context ();
5029 if (token->type == CPP_EOF)
5030 break;
5032 if (modules_p ())
5034 /* Top-level module declarations are ok, and change the
5035 portion of file we're in. Top-level import declarations
5036 are significant for the import portions. */
5038 cp_token *next = token;
5039 bool exporting = token->keyword == RID__EXPORT;
5040 if (exporting)
5042 cp_lexer_consume_token (parser->lexer);
5043 next = cp_lexer_peek_token (parser->lexer);
5045 if (next->keyword == RID__MODULE)
5047 mp_state
5048 = cp_parser_module_declaration (parser, mp_state, exporting);
5049 continue;
5051 else if (next->keyword == RID__IMPORT)
5053 if (mp_state == MP_FIRST)
5054 mp_state = MP_NOT_MODULE;
5055 cp_parser_import_declaration (parser, mp_state, exporting);
5056 continue;
5058 else
5059 gcc_checking_assert (!exporting);
5061 if (mp_state == MP_GLOBAL && token->main_source_p)
5063 static bool warned = false;
5064 if (!warned)
5066 warned = true;
5067 error_at (token->location,
5068 "global module fragment contents must be"
5069 " from preprocessor inclusion");
5074 /* This relies on the ordering of module_parse values. */
5075 if (mp_state == MP_PURVIEW_IMPORTS || mp_state == MP_PRIVATE_IMPORTS)
5076 /* We're no longer in the import portion of a named module. */
5077 mp_state = module_parse (mp_state + 1);
5078 else if (mp_state == MP_FIRST)
5079 mp_state = MP_NOT_MODULE;
5081 if (token->type == CPP_CLOSE_BRACE)
5083 cp_parser_error (parser, "expected declaration");
5084 cp_lexer_consume_token (parser->lexer);
5085 /* If the next token is now a `;', consume it. */
5086 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
5087 cp_lexer_consume_token (parser->lexer);
5089 else
5090 cp_parser_toplevel_declaration (parser);
5093 /* Get rid of the token array; we don't need it any more. */
5094 cp_lexer_destroy (parser->lexer);
5095 parser->lexer = NULL;
5097 /* The EOF should have reset this. */
5098 gcc_checking_assert (!implicit_extern_c);
5100 /* Make sure the declarator obstack was fully cleaned up. */
5101 gcc_assert (obstack_next_free (&declarator_obstack)
5102 == declarator_obstack_base);
5105 /* Return the appropriate tsubst flags for parsing, possibly in N3276
5106 decltype context. */
5108 static inline tsubst_flags_t
5109 complain_flags (bool decltype_p)
5111 tsubst_flags_t complain = tf_warning_or_error;
5112 if (decltype_p)
5113 complain |= tf_decltype;
5114 return complain;
5117 /* We're about to parse a collection of statements. If we're currently
5118 parsing tentatively, set up a firewall so that any nested
5119 cp_parser_commit_to_tentative_parse won't affect the current context. */
5121 static cp_token_position
5122 cp_parser_start_tentative_firewall (cp_parser *parser)
5124 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5125 return 0;
5127 cp_parser_parse_tentatively (parser);
5128 cp_parser_commit_to_topmost_tentative_parse (parser);
5129 return cp_lexer_token_position (parser->lexer, false);
5132 /* We've finished parsing the collection of statements. Wrap up the
5133 firewall and replace the relevant tokens with the parsed form. */
5135 static void
5136 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
5137 tree expr)
5139 if (!start)
5140 return;
5142 /* Finish the firewall level. */
5143 cp_parser_parse_definitely (parser);
5144 /* And remember the result of the parse for when we try again. */
5145 cp_token *token = cp_lexer_token_at (parser->lexer, start);
5146 token->type = CPP_PREPARSED_EXPR;
5147 token->u.value = expr;
5148 token->keyword = RID_MAX;
5149 cp_lexer_purge_tokens_after (parser->lexer, start);
5152 /* Like the above functions, but let the user modify the tokens. Used by
5153 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
5154 later parses, so it makes sense to localize the effects of
5155 cp_parser_commit_to_tentative_parse. */
5157 struct tentative_firewall
5159 cp_parser *parser;
5160 bool set;
5162 tentative_firewall (cp_parser *p): parser(p)
5164 /* If we're currently parsing tentatively, start a committed level as a
5165 firewall and then an inner tentative parse. */
5166 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
5168 cp_parser_parse_tentatively (parser);
5169 cp_parser_commit_to_topmost_tentative_parse (parser);
5170 cp_parser_parse_tentatively (parser);
5174 ~tentative_firewall()
5176 if (set)
5178 /* Finish the inner tentative parse and the firewall, propagating any
5179 uncommitted error state to the outer tentative parse. */
5180 bool err = cp_parser_error_occurred (parser);
5181 cp_parser_parse_definitely (parser);
5182 cp_parser_parse_definitely (parser);
5183 if (err)
5184 cp_parser_simulate_error (parser);
5189 /* Some tokens naturally come in pairs e.g.'(' and ')'.
5190 This class is for tracking such a matching pair of symbols.
5191 In particular, it tracks the location of the first token,
5192 so that if the second token is missing, we can highlight the
5193 location of the first token when notifying the user about the
5194 problem. */
5196 template <typename traits_t>
5197 class token_pair
5199 public:
5200 /* token_pair's ctor. */
5201 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
5203 /* If the next token is the opening symbol for this pair, consume it and
5204 return true.
5205 Otherwise, issue an error and return false.
5206 In either case, record the location of the opening token. */
5208 bool require_open (cp_parser *parser)
5210 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
5211 return cp_parser_require (parser, traits_t::open_token_type,
5212 traits_t::required_token_open);
5215 /* Consume the next token from PARSER, recording its location as
5216 that of the opening token within the pair. */
5218 cp_token * consume_open (cp_parser *parser)
5220 cp_token *tok = cp_lexer_consume_token (parser->lexer);
5221 gcc_assert (tok->type == traits_t::open_token_type);
5222 m_open_loc = tok->location;
5223 return tok;
5226 /* If the next token is the closing symbol for this pair, consume it
5227 and return it.
5228 Otherwise, issue an error, highlighting the location of the
5229 corresponding opening token, and return NULL. */
5231 cp_token *require_close (cp_parser *parser) const
5233 return cp_parser_require (parser, traits_t::close_token_type,
5234 traits_t::required_token_close,
5235 m_open_loc);
5238 location_t open_location () const { return m_open_loc; }
5240 private:
5241 location_t m_open_loc;
5244 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
5246 struct matching_paren_traits
5248 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
5249 static const enum required_token required_token_open = RT_OPEN_PAREN;
5250 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
5251 static const enum required_token required_token_close = RT_CLOSE_PAREN;
5254 /* "matching_parens" is a token_pair<T> class for tracking matching
5255 pairs of parentheses. */
5257 typedef token_pair<matching_paren_traits> matching_parens;
5259 /* Traits for token_pair<T> for tracking matching pairs of braces. */
5261 struct matching_brace_traits
5263 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
5264 static const enum required_token required_token_open = RT_OPEN_BRACE;
5265 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
5266 static const enum required_token required_token_close = RT_CLOSE_BRACE;
5269 /* "matching_braces" is a token_pair<T> class for tracking matching
5270 pairs of braces. */
5272 typedef token_pair<matching_brace_traits> matching_braces;
5275 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
5276 enclosing parentheses. */
5278 static cp_expr
5279 cp_parser_statement_expr (cp_parser *parser)
5281 cp_token_position start = cp_parser_start_tentative_firewall (parser);
5283 /* Consume the '('. */
5284 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
5285 matching_parens parens;
5286 parens.consume_open (parser);
5287 /* Start the statement-expression. */
5288 tree expr = begin_stmt_expr ();
5289 /* Parse the compound-statement. */
5290 cp_parser_compound_statement (parser, expr, BCS_STMT_EXPR, false);
5291 /* Finish up. */
5292 expr = finish_stmt_expr (expr, false);
5293 /* Consume the ')'. */
5294 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
5295 if (!parens.require_close (parser))
5296 cp_parser_skip_to_end_of_statement (parser);
5298 cp_parser_end_tentative_firewall (parser, start, expr);
5299 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
5300 return cp_expr (expr, combined_loc);
5303 /* Expressions [gram.expr] */
5305 /* Parse a fold-operator.
5307 fold-operator:
5308 - * / % ^ & | = < > << >>
5309 = -= *= /= %= ^= &= |= <<= >>=
5310 == != <= >= && || , .* ->*
5312 This returns the tree code corresponding to the matched operator
5313 as an int. When the current token matches a compound assignment
5314 operator, the resulting tree code is the negative value of the
5315 non-assignment operator. */
5317 static int
5318 cp_parser_fold_operator (cp_token *token)
5320 switch (token->type)
5322 case CPP_PLUS: return PLUS_EXPR;
5323 case CPP_MINUS: return MINUS_EXPR;
5324 case CPP_MULT: return MULT_EXPR;
5325 case CPP_DIV: return TRUNC_DIV_EXPR;
5326 case CPP_MOD: return TRUNC_MOD_EXPR;
5327 case CPP_XOR: return BIT_XOR_EXPR;
5328 case CPP_AND: return BIT_AND_EXPR;
5329 case CPP_OR: return BIT_IOR_EXPR;
5330 case CPP_LSHIFT: return LSHIFT_EXPR;
5331 case CPP_RSHIFT: return RSHIFT_EXPR;
5333 case CPP_EQ: return -NOP_EXPR;
5334 case CPP_PLUS_EQ: return -PLUS_EXPR;
5335 case CPP_MINUS_EQ: return -MINUS_EXPR;
5336 case CPP_MULT_EQ: return -MULT_EXPR;
5337 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
5338 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
5339 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
5340 case CPP_AND_EQ: return -BIT_AND_EXPR;
5341 case CPP_OR_EQ: return -BIT_IOR_EXPR;
5342 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
5343 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
5345 case CPP_EQ_EQ: return EQ_EXPR;
5346 case CPP_NOT_EQ: return NE_EXPR;
5347 case CPP_LESS: return LT_EXPR;
5348 case CPP_GREATER: return GT_EXPR;
5349 case CPP_LESS_EQ: return LE_EXPR;
5350 case CPP_GREATER_EQ: return GE_EXPR;
5352 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
5353 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
5355 case CPP_COMMA: return COMPOUND_EXPR;
5357 case CPP_DOT_STAR: return DOTSTAR_EXPR;
5358 case CPP_DEREF_STAR: return MEMBER_REF;
5360 default: return ERROR_MARK;
5364 /* Returns true if CODE indicates a binary expression, which is not allowed in
5365 the LHS of a fold-expression. More codes will need to be added to use this
5366 function in other contexts. */
5368 static bool
5369 is_binary_op (tree_code code)
5371 switch (code)
5373 case PLUS_EXPR:
5374 case POINTER_PLUS_EXPR:
5375 case MINUS_EXPR:
5376 case MULT_EXPR:
5377 case TRUNC_DIV_EXPR:
5378 case TRUNC_MOD_EXPR:
5379 case BIT_XOR_EXPR:
5380 case BIT_AND_EXPR:
5381 case BIT_IOR_EXPR:
5382 case LSHIFT_EXPR:
5383 case RSHIFT_EXPR:
5385 case MODOP_EXPR:
5387 case EQ_EXPR:
5388 case NE_EXPR:
5389 case LE_EXPR:
5390 case GE_EXPR:
5391 case LT_EXPR:
5392 case GT_EXPR:
5394 case TRUTH_ANDIF_EXPR:
5395 case TRUTH_ORIF_EXPR:
5397 case COMPOUND_EXPR:
5399 case DOTSTAR_EXPR:
5400 case MEMBER_REF:
5401 return true;
5403 default:
5404 return false;
5408 /* If the next token is a suitable fold operator, consume it and return as
5409 the function above. */
5411 static int
5412 cp_parser_fold_operator (cp_parser *parser)
5414 cp_token* token = cp_lexer_peek_token (parser->lexer);
5415 int code = cp_parser_fold_operator (token);
5416 if (code != ERROR_MARK)
5417 cp_lexer_consume_token (parser->lexer);
5418 return code;
5421 /* Parse a fold-expression.
5423 fold-expression:
5424 ( ... folding-operator cast-expression)
5425 ( cast-expression folding-operator ... )
5426 ( cast-expression folding operator ... folding-operator cast-expression)
5428 Note that the '(' and ')' are matched in primary expression. */
5430 static cp_expr
5431 cp_parser_fold_expression (cp_parser *parser, tree expr1)
5433 cp_id_kind pidk;
5435 // Left fold.
5436 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5438 if (expr1)
5439 return error_mark_node;
5440 cp_lexer_consume_token (parser->lexer);
5441 int op = cp_parser_fold_operator (parser);
5442 if (op == ERROR_MARK)
5444 cp_parser_error (parser, "expected binary operator");
5445 return error_mark_node;
5448 tree expr = cp_parser_cast_expression (parser, false, false,
5449 false, &pidk);
5450 if (expr == error_mark_node)
5451 return error_mark_node;
5452 return finish_left_unary_fold_expr (expr, op);
5455 const cp_token* token = cp_lexer_peek_token (parser->lexer);
5456 int op = cp_parser_fold_operator (parser);
5457 if (op == ERROR_MARK)
5459 cp_parser_error (parser, "expected binary operator");
5460 return error_mark_node;
5463 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
5465 cp_parser_error (parser, "expected ...");
5466 return error_mark_node;
5468 cp_lexer_consume_token (parser->lexer);
5470 /* The operands of a fold-expression are cast-expressions, so binary or
5471 conditional expressions are not allowed. We check this here to avoid
5472 tentative parsing. */
5473 if (EXPR_P (expr1) && warning_suppressed_p (expr1, OPT_Wparentheses))
5474 /* OK, the expression was parenthesized. */;
5475 else if (is_binary_op (TREE_CODE (expr1)))
5476 error_at (location_of (expr1),
5477 "binary expression in operand of fold-expression");
5478 else if (TREE_CODE (expr1) == COND_EXPR
5479 || (REFERENCE_REF_P (expr1)
5480 && TREE_CODE (TREE_OPERAND (expr1, 0)) == COND_EXPR))
5481 error_at (location_of (expr1),
5482 "conditional expression in operand of fold-expression");
5484 // Right fold.
5485 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5486 return finish_right_unary_fold_expr (expr1, op);
5488 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
5490 cp_parser_error (parser, "mismatched operator in fold-expression");
5491 return error_mark_node;
5493 cp_lexer_consume_token (parser->lexer);
5495 // Binary left or right fold.
5496 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
5497 if (expr2 == error_mark_node)
5498 return error_mark_node;
5499 return finish_binary_fold_expr (expr1, expr2, op);
5502 /* Parse a primary-expression.
5504 primary-expression:
5505 literal
5506 this
5507 ( expression )
5508 id-expression
5509 lambda-expression (C++11)
5511 GNU Extensions:
5513 primary-expression:
5514 ( compound-statement )
5515 __builtin_va_arg ( assignment-expression , type-id )
5516 __builtin_offsetof ( type-id , offsetof-expression )
5518 C++ Extensions:
5519 __has_nothrow_assign ( type-id )
5520 __has_nothrow_constructor ( type-id )
5521 __has_nothrow_copy ( type-id )
5522 __has_trivial_assign ( type-id )
5523 __has_trivial_constructor ( type-id )
5524 __has_trivial_copy ( type-id )
5525 __has_trivial_destructor ( type-id )
5526 __has_virtual_destructor ( type-id )
5527 __is_abstract ( type-id )
5528 __is_base_of ( type-id , type-id )
5529 __is_class ( type-id )
5530 __is_empty ( type-id )
5531 __is_enum ( type-id )
5532 __is_final ( type-id )
5533 __is_literal_type ( type-id )
5534 __is_pod ( type-id )
5535 __is_polymorphic ( type-id )
5536 __is_std_layout ( type-id )
5537 __is_trivial ( type-id )
5538 __is_union ( type-id )
5540 Objective-C++ Extension:
5542 primary-expression:
5543 objc-expression
5545 literal:
5546 __null
5548 ADDRESS_P is true iff this expression was immediately preceded by
5549 "&" and therefore might denote a pointer-to-member. CAST_P is true
5550 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5551 true iff this expression is a template argument.
5553 Returns a representation of the expression. Upon return, *IDK
5554 indicates what kind of id-expression (if any) was present. */
5556 static cp_expr
5557 cp_parser_primary_expression (cp_parser *parser,
5558 bool address_p,
5559 bool cast_p,
5560 bool template_arg_p,
5561 bool decltype_p,
5562 cp_id_kind *idk)
5564 cp_token *token = NULL;
5566 /* Assume the primary expression is not an id-expression. */
5567 *idk = CP_ID_KIND_NONE;
5569 /* Peek at the next token. */
5570 token = cp_lexer_peek_token (parser->lexer);
5571 switch ((int) token->type)
5573 /* literal:
5574 integer-literal
5575 character-literal
5576 floating-literal
5577 string-literal
5578 boolean-literal
5579 pointer-literal
5580 user-defined-literal */
5581 case CPP_CHAR:
5582 case CPP_CHAR16:
5583 case CPP_CHAR32:
5584 case CPP_WCHAR:
5585 case CPP_UTF8CHAR:
5586 case CPP_NUMBER:
5587 case CPP_PREPARSED_EXPR:
5588 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5589 return cp_parser_userdef_numeric_literal (parser);
5590 token = cp_lexer_consume_token (parser->lexer);
5591 if (TREE_CODE (token->u.value) == FIXED_CST)
5593 error_at (token->location,
5594 "fixed-point types not supported in C++");
5595 return error_mark_node;
5597 /* Floating-point literals are only allowed in an integral
5598 constant expression if they are cast to an integral or
5599 enumeration type. */
5600 if ((TREE_CODE (token->u.value) == REAL_CST
5601 || (TREE_CODE (token->u.value) == EXCESS_PRECISION_EXPR
5602 && TREE_CODE (TREE_OPERAND (token->u.value, 0)) == REAL_CST))
5603 && parser->integral_constant_expression_p
5604 && pedantic)
5606 /* CAST_P will be set even in invalid code like "int(2.7 +
5607 ...)". Therefore, we have to check that the next token
5608 is sure to end the cast. */
5609 if (cast_p)
5611 cp_token *next_token;
5613 next_token = cp_lexer_peek_token (parser->lexer);
5614 if (/* The comma at the end of an
5615 enumerator-definition. */
5616 next_token->type != CPP_COMMA
5617 /* The curly brace at the end of an enum-specifier. */
5618 && next_token->type != CPP_CLOSE_BRACE
5619 /* The end of a statement. */
5620 && next_token->type != CPP_SEMICOLON
5621 /* The end of the cast-expression. */
5622 && next_token->type != CPP_CLOSE_PAREN
5623 /* The end of an array bound. */
5624 && next_token->type != CPP_CLOSE_SQUARE
5625 /* The closing ">" in a template-argument-list. */
5626 && (next_token->type != CPP_GREATER
5627 || parser->greater_than_is_operator_p)
5628 /* C++0x only: A ">>" treated like two ">" tokens,
5629 in a template-argument-list. */
5630 && (next_token->type != CPP_RSHIFT
5631 || (cxx_dialect == cxx98)
5632 || parser->greater_than_is_operator_p))
5633 cast_p = false;
5636 /* If we are within a cast, then the constraint that the
5637 cast is to an integral or enumeration type will be
5638 checked at that point. If we are not within a cast, then
5639 this code is invalid. */
5640 if (!cast_p)
5641 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5643 return (cp_expr (token->u.value, token->location, token->flags & DECIMAL_INT)
5644 .maybe_add_location_wrapper ());
5646 case CPP_CHAR_USERDEF:
5647 case CPP_CHAR16_USERDEF:
5648 case CPP_CHAR32_USERDEF:
5649 case CPP_WCHAR_USERDEF:
5650 case CPP_UTF8CHAR_USERDEF:
5651 return cp_parser_userdef_char_literal (parser);
5653 case CPP_STRING:
5654 case CPP_STRING16:
5655 case CPP_STRING32:
5656 case CPP_WSTRING:
5657 case CPP_UTF8STRING:
5658 case CPP_STRING_USERDEF:
5659 case CPP_STRING16_USERDEF:
5660 case CPP_STRING32_USERDEF:
5661 case CPP_WSTRING_USERDEF:
5662 case CPP_UTF8STRING_USERDEF:
5663 /* ??? Should wide strings be allowed when parser->translate_strings_p
5664 is false (i.e. in attributes)? If not, we can kill the third
5665 argument to cp_parser_string_literal. */
5666 return (cp_parser_string_literal (parser,
5667 parser->translate_strings_p,
5668 true)
5669 .maybe_add_location_wrapper ());
5671 case CPP_OPEN_PAREN:
5672 /* If we see `( { ' then we are looking at the beginning of
5673 a GNU statement-expression. */
5674 if (cp_parser_allow_gnu_extensions_p (parser)
5675 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5677 /* Statement-expressions are not allowed by the standard. */
5678 pedwarn (token->location, OPT_Wpedantic,
5679 "ISO C++ forbids braced-groups within expressions");
5681 /* And they're not allowed outside of a function-body; you
5682 cannot, for example, write:
5684 int i = ({ int j = 3; j + 1; });
5686 at class or namespace scope. */
5687 if (!parser->in_function_body
5688 || parser->in_template_argument_list_p)
5690 error_at (token->location,
5691 "statement-expressions are not allowed outside "
5692 "functions nor in template-argument lists");
5693 cp_parser_skip_to_end_of_block_or_statement (parser);
5694 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5695 cp_lexer_consume_token (parser->lexer);
5696 return error_mark_node;
5698 else
5699 return cp_parser_statement_expr (parser);
5701 /* Otherwise it's a normal parenthesized expression. */
5703 cp_expr expr;
5704 bool saved_greater_than_is_operator_p;
5706 location_t open_paren_loc = token->location;
5708 /* Consume the `('. */
5709 matching_parens parens;
5710 parens.consume_open (parser);
5711 /* Within a parenthesized expression, a `>' token is always
5712 the greater-than operator. */
5713 saved_greater_than_is_operator_p
5714 = parser->greater_than_is_operator_p;
5715 parser->greater_than_is_operator_p = true;
5717 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5718 /* Left fold expression. */
5719 expr = NULL_TREE;
5720 else
5721 /* Parse the parenthesized expression. */
5722 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5724 token = cp_lexer_peek_token (parser->lexer);
5725 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5727 expr = cp_parser_fold_expression (parser, expr);
5728 if (expr != error_mark_node
5729 && cxx_dialect < cxx17)
5730 pedwarn (input_location, OPT_Wc__17_extensions,
5731 "fold-expressions only available with %<-std=c++17%> "
5732 "or %<-std=gnu++17%>");
5734 else
5735 /* Let the front end know that this expression was
5736 enclosed in parentheses. This matters in case, for
5737 example, the expression is of the form `A::B', since
5738 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5739 not. */
5740 expr = finish_parenthesized_expr (expr);
5742 /* DR 705: Wrapping an unqualified name in parentheses
5743 suppresses arg-dependent lookup. We want to pass back
5744 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5745 (c++/37862), but none of the others. */
5746 if (*idk != CP_ID_KIND_QUALIFIED)
5747 *idk = CP_ID_KIND_NONE;
5749 /* The `>' token might be the end of a template-id or
5750 template-parameter-list now. */
5751 parser->greater_than_is_operator_p
5752 = saved_greater_than_is_operator_p;
5754 /* Consume the `)'. */
5755 token = cp_lexer_peek_token (parser->lexer);
5756 location_t close_paren_loc = token->location;
5757 bool no_wparens = warning_suppressed_p (expr, OPT_Wparentheses);
5758 expr.set_range (open_paren_loc, close_paren_loc);
5759 if (no_wparens)
5760 suppress_warning (expr, OPT_Wparentheses);
5761 if (!parens.require_close (parser)
5762 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5763 cp_parser_skip_to_end_of_statement (parser);
5765 return expr;
5768 case CPP_OPEN_SQUARE:
5770 if (c_dialect_objc ())
5772 /* We might have an Objective-C++ message. */
5773 cp_parser_parse_tentatively (parser);
5774 tree msg = cp_parser_objc_message_expression (parser);
5775 /* If that works out, we're done ... */
5776 if (cp_parser_parse_definitely (parser))
5777 return msg;
5778 /* ... else, fall though to see if it's a lambda. */
5780 cp_expr lam = cp_parser_lambda_expression (parser);
5781 /* Don't warn about a failed tentative parse. */
5782 if (cp_parser_error_occurred (parser))
5783 return error_mark_node;
5784 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5785 return lam;
5788 case CPP_OBJC_STRING:
5789 if (c_dialect_objc ())
5790 /* We have an Objective-C++ string literal. */
5791 return cp_parser_objc_expression (parser);
5792 cp_parser_error (parser, "expected primary-expression");
5793 return error_mark_node;
5795 case CPP_KEYWORD:
5796 switch (token->keyword)
5798 /* These two are the boolean literals. */
5799 case RID_TRUE:
5800 cp_lexer_consume_token (parser->lexer);
5801 return cp_expr (boolean_true_node, token->location);
5802 case RID_FALSE:
5803 cp_lexer_consume_token (parser->lexer);
5804 return cp_expr (boolean_false_node, token->location);
5806 /* The `__null' literal. */
5807 case RID_NULL:
5808 cp_lexer_consume_token (parser->lexer);
5809 return cp_expr (null_node, token->location);
5811 /* The `nullptr' literal. */
5812 case RID_NULLPTR:
5813 cp_lexer_consume_token (parser->lexer);
5814 return cp_expr (nullptr_node, token->location);
5816 /* Recognize the `this' keyword. */
5817 case RID_THIS:
5818 cp_lexer_consume_token (parser->lexer);
5819 if (parser->local_variables_forbidden_p & THIS_FORBIDDEN)
5821 error_at (token->location,
5822 "%<this%> may not be used in this context");
5823 return error_mark_node;
5825 /* Pointers cannot appear in constant-expressions. */
5826 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5827 return error_mark_node;
5828 return cp_expr (finish_this_expr (), token->location);
5830 /* The `operator' keyword can be the beginning of an
5831 id-expression. */
5832 case RID_OPERATOR:
5833 goto id_expression;
5835 case RID_FUNCTION_NAME:
5836 case RID_PRETTY_FUNCTION_NAME:
5837 case RID_C99_FUNCTION_NAME:
5839 non_integral_constant name;
5841 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5842 __func__ are the names of variables -- but they are
5843 treated specially. Therefore, they are handled here,
5844 rather than relying on the generic id-expression logic
5845 below. Grammatically, these names are id-expressions.
5847 Consume the token. */
5848 token = cp_lexer_consume_token (parser->lexer);
5850 switch (token->keyword)
5852 case RID_FUNCTION_NAME:
5853 name = NIC_FUNC_NAME;
5854 break;
5855 case RID_PRETTY_FUNCTION_NAME:
5856 name = NIC_PRETTY_FUNC;
5857 break;
5858 case RID_C99_FUNCTION_NAME:
5859 name = NIC_C99_FUNC;
5860 break;
5861 default:
5862 gcc_unreachable ();
5865 if (cp_parser_non_integral_constant_expression (parser, name))
5866 return error_mark_node;
5868 /* Look up the name. */
5869 return finish_fname (token->u.value);
5872 case RID_VA_ARG:
5874 tree expression;
5875 tree type;
5876 location_t type_location;
5877 location_t start_loc
5878 = cp_lexer_peek_token (parser->lexer)->location;
5879 /* The `__builtin_va_arg' construct is used to handle
5880 `va_arg'. Consume the `__builtin_va_arg' token. */
5881 cp_lexer_consume_token (parser->lexer);
5882 /* Look for the opening `('. */
5883 matching_parens parens;
5884 parens.require_open (parser);
5885 /* Now, parse the assignment-expression. */
5886 expression = cp_parser_assignment_expression (parser);
5887 /* Look for the `,'. */
5888 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5889 type_location = cp_lexer_peek_token (parser->lexer)->location;
5890 /* Parse the type-id. */
5892 type_id_in_expr_sentinel s (parser);
5893 type = cp_parser_type_id (parser);
5895 /* Look for the closing `)'. */
5896 location_t finish_loc
5897 = cp_lexer_peek_token (parser->lexer)->location;
5898 parens.require_close (parser);
5899 /* Using `va_arg' in a constant-expression is not
5900 allowed. */
5901 if (cp_parser_non_integral_constant_expression (parser,
5902 NIC_VA_ARG))
5903 return error_mark_node;
5904 /* Construct a location of the form:
5905 __builtin_va_arg (v, int)
5906 ~~~~~~~~~~~~~~~~~~~~~^~~~
5907 with the caret at the type, ranging from the start of the
5908 "__builtin_va_arg" token to the close paren. */
5909 location_t combined_loc
5910 = make_location (type_location, start_loc, finish_loc);
5911 return build_x_va_arg (combined_loc, expression, type);
5914 case RID_OFFSETOF:
5915 return cp_parser_builtin_offsetof (parser);
5917 #define DEFTRAIT_EXPR(CODE, NAME, ARITY) \
5918 case RID_##CODE:
5919 #include "cp-trait.def"
5920 #undef DEFTRAIT_EXPR
5921 return cp_parser_trait (parser, token->keyword);
5923 // C++ concepts
5924 case RID_REQUIRES:
5925 return cp_parser_requires_expression (parser);
5927 /* Objective-C++ expressions. */
5928 case RID_AT_ENCODE:
5929 case RID_AT_PROTOCOL:
5930 case RID_AT_SELECTOR:
5931 return cp_parser_objc_expression (parser);
5933 case RID_OMP_ALL_MEMORY:
5934 gcc_assert (flag_openmp);
5935 cp_lexer_consume_token (parser->lexer);
5936 error_at (token->location,
5937 "%<omp_all_memory%> may only be used in OpenMP "
5938 "%<depend%> clause");
5939 return error_mark_node;
5941 case RID_TEMPLATE:
5942 if (parser->in_function_body
5943 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5944 == CPP_LESS))
5946 error_at (token->location,
5947 "a template declaration cannot appear at block scope");
5948 cp_parser_skip_to_end_of_block_or_statement (parser);
5949 return error_mark_node;
5951 /* FALLTHRU */
5952 default:
5953 cp_parser_error (parser, "expected primary-expression");
5954 return error_mark_node;
5957 /* An id-expression can start with either an identifier, a
5958 `::' as the beginning of a qualified-id, or the "operator"
5959 keyword. */
5960 case CPP_NAME:
5961 case CPP_SCOPE:
5962 case CPP_TEMPLATE_ID:
5963 case CPP_NESTED_NAME_SPECIFIER:
5965 id_expression:
5966 cp_expr id_expression;
5967 cp_expr decl;
5968 const char *error_msg;
5969 bool template_p;
5970 bool done;
5971 cp_token *id_expr_token;
5973 /* Parse the id-expression. */
5974 id_expression
5975 = cp_parser_id_expression (parser,
5976 /*template_keyword_p=*/false,
5977 /*check_dependency_p=*/true,
5978 &template_p,
5979 /*declarator_p=*/false,
5980 /*optional_p=*/false);
5981 if (id_expression == error_mark_node)
5982 return error_mark_node;
5983 id_expr_token = token;
5984 token = cp_lexer_peek_token (parser->lexer);
5985 done = (token->type != CPP_OPEN_SQUARE
5986 && token->type != CPP_OPEN_PAREN
5987 && token->type != CPP_DOT
5988 && token->type != CPP_DEREF
5989 && token->type != CPP_PLUS_PLUS
5990 && token->type != CPP_MINUS_MINUS);
5991 /* If we have a template-id, then no further lookup is
5992 required. If the template-id was for a template-class, we
5993 will sometimes have a TYPE_DECL at this point. */
5994 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5995 || TREE_CODE (id_expression) == TYPE_DECL)
5996 decl = id_expression;
5997 /* Look up the name. */
5998 else
6000 tree ambiguous_decls;
6002 /* If we already know that this lookup is ambiguous, then
6003 we've already issued an error message; there's no reason
6004 to check again. */
6005 if (id_expr_token->type == CPP_NAME
6006 && id_expr_token->error_reported)
6008 cp_parser_simulate_error (parser);
6009 return error_mark_node;
6012 decl = cp_parser_lookup_name (parser, id_expression,
6013 none_type,
6014 template_p,
6015 /*is_namespace=*/false,
6016 /*check_dependency=*/true,
6017 &ambiguous_decls,
6018 id_expression.get_location ());
6019 /* If the lookup was ambiguous, an error will already have
6020 been issued. */
6021 if (ambiguous_decls)
6022 return error_mark_node;
6024 /* In Objective-C++, we may have an Objective-C 2.0
6025 dot-syntax for classes here. */
6026 if (c_dialect_objc ()
6027 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
6028 && TREE_CODE (decl) == TYPE_DECL
6029 && objc_is_class_name (decl))
6031 tree component;
6032 cp_lexer_consume_token (parser->lexer);
6033 component = cp_parser_identifier (parser);
6034 if (component == error_mark_node)
6035 return error_mark_node;
6037 tree result = objc_build_class_component_ref (id_expression,
6038 component);
6039 /* Build a location of the form:
6040 expr.component
6041 ~~~~~^~~~~~~~~
6042 with caret at the start of the component name (at
6043 input_location), ranging from the start of the id_expression
6044 to the end of the component name. */
6045 location_t combined_loc
6046 = make_location (input_location, id_expression.get_start (),
6047 get_finish (input_location));
6048 protected_set_expr_location (result, combined_loc);
6049 return result;
6052 /* In Objective-C++, an instance variable (ivar) may be preferred
6053 to whatever cp_parser_lookup_name() found.
6054 Call objc_lookup_ivar. To avoid exposing cp_expr to the
6055 rest of c-family, we have to do a little extra work to preserve
6056 any location information in cp_expr "decl". Given that
6057 objc_lookup_ivar is implemented in "c-family" and "objc", we
6058 have a trip through the pure "tree" type, rather than cp_expr.
6059 Naively copying it back to "decl" would implicitly give the
6060 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
6061 store an EXPR_LOCATION. Hence we only update "decl" (and
6062 hence its location_t) if we get back a different tree node. */
6063 tree decl_tree = objc_lookup_ivar (decl.get_value (),
6064 id_expression);
6065 if (decl_tree != decl.get_value ())
6066 decl = cp_expr (decl_tree);
6068 /* If name lookup gives us a SCOPE_REF, then the
6069 qualifying scope was dependent. */
6070 if (TREE_CODE (decl) == SCOPE_REF)
6072 /* At this point, we do not know if DECL is a valid
6073 integral constant expression. We assume that it is
6074 in fact such an expression, so that code like:
6076 template <int N> struct A {
6077 int a[B<N>::i];
6080 is accepted. At template-instantiation time, we
6081 will check that B<N>::i is actually a constant. */
6082 return decl;
6084 /* Check to see if DECL is a local variable in a context
6085 where that is forbidden. */
6086 if ((parser->local_variables_forbidden_p & LOCAL_VARS_FORBIDDEN)
6087 && local_variable_p (decl)
6088 /* DR 2082 permits local variables in unevaluated contexts
6089 within a default argument. */
6090 && !cp_unevaluated_operand)
6092 const char *msg
6093 = (TREE_CODE (decl) == PARM_DECL
6094 ? _("parameter %qD may not appear in this context")
6095 : _("local variable %qD may not appear in this context"));
6096 error_at (id_expression.get_location (), msg,
6097 decl.get_value ());
6098 return error_mark_node;
6102 decl = (finish_id_expression
6103 (id_expression, decl, parser->scope,
6104 idk,
6105 parser->integral_constant_expression_p,
6106 parser->allow_non_integral_constant_expression_p,
6107 &parser->non_integral_constant_expression_p,
6108 template_p, done, address_p,
6109 template_arg_p,
6110 &error_msg,
6111 id_expression.get_location ()));
6112 if (error_msg)
6113 cp_parser_error (parser, error_msg);
6114 /* Build a location for an id-expression of the form:
6115 ::ns::id
6116 ~~~~~~^~
6120 i.e. from the start of the first token to the end of the final
6121 token, with the caret at the start of the unqualified-id. */
6122 location_t caret_loc = get_pure_location (id_expression.get_location ());
6123 location_t start_loc = get_start (id_expr_token->location);
6124 location_t finish_loc = get_finish (id_expression.get_location ());
6125 location_t combined_loc
6126 = make_location (caret_loc, start_loc, finish_loc);
6128 decl.set_location (combined_loc);
6129 return decl;
6132 /* Anything else is an error. */
6133 default:
6134 cp_parser_error (parser, "expected primary-expression");
6135 return error_mark_node;
6139 static inline cp_expr
6140 cp_parser_primary_expression (cp_parser *parser,
6141 bool address_p,
6142 bool cast_p,
6143 bool template_arg_p,
6144 cp_id_kind *idk)
6146 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
6147 /*decltype*/false, idk);
6150 /* Complain about missing template keyword when naming a dependent
6151 member template. */
6153 static void
6154 missing_template_diag (location_t loc, diagnostic_t diag_kind = DK_WARNING)
6156 if (warning_suppressed_at (loc, OPT_Wmissing_template_keyword))
6157 return;
6159 gcc_rich_location richloc (loc);
6160 richloc.add_fixit_insert_before ("template");
6161 emit_diagnostic (diag_kind, &richloc, OPT_Wmissing_template_keyword,
6162 "expected %qs keyword before dependent "
6163 "template name", "template");
6164 suppress_warning_at (loc, OPT_Wmissing_template_keyword);
6167 /* Parse an id-expression.
6169 id-expression:
6170 unqualified-id
6171 qualified-id
6173 qualified-id:
6174 :: [opt] nested-name-specifier template [opt] unqualified-id
6175 :: identifier
6176 :: operator-function-id
6177 :: template-id
6179 Return a representation of the unqualified portion of the
6180 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
6181 a `::' or nested-name-specifier.
6183 Often, if the id-expression was a qualified-id, the caller will
6184 want to make a SCOPE_REF to represent the qualified-id. This
6185 function does not do this in order to avoid wastefully creating
6186 SCOPE_REFs when they are not required.
6188 If TEMPLATE_KEYWORD_P is true, then we have just seen the
6189 `template' keyword.
6191 If CHECK_DEPENDENCY_P is false, then names are looked up inside
6192 uninstantiated templates.
6194 If *TEMPLATE_P is non-NULL, it is set to true iff the
6195 `template' keyword is used to explicitly indicate that the entity
6196 named is a template.
6198 If DECLARATOR_P is true, the id-expression is appearing as part of
6199 a declarator, rather than as part of an expression. */
6201 static cp_expr
6202 cp_parser_id_expression (cp_parser *parser,
6203 bool template_keyword_p,
6204 bool check_dependency_p,
6205 bool *template_p,
6206 bool declarator_p,
6207 bool optional_p)
6209 bool global_scope_p;
6210 bool nested_name_specifier_p;
6212 /* Assume the `template' keyword was not used. */
6213 if (template_p)
6214 *template_p = template_keyword_p;
6216 /* Look for the optional `::' operator. */
6217 global_scope_p
6218 = (!template_keyword_p
6219 && (cp_parser_global_scope_opt (parser,
6220 /*current_scope_valid_p=*/false)
6221 != NULL_TREE));
6223 /* Look for the optional nested-name-specifier. */
6224 nested_name_specifier_p
6225 = (cp_parser_nested_name_specifier_opt (parser,
6226 /*typename_keyword_p=*/false,
6227 check_dependency_p,
6228 /*type_p=*/false,
6229 declarator_p,
6230 template_keyword_p)
6231 != NULL_TREE);
6233 cp_expr id = NULL_TREE;
6234 tree scope = parser->scope;
6236 /* Peek at the next token. */
6237 cp_token *token = cp_lexer_peek_token (parser->lexer);
6239 /* If there is a nested-name-specifier, then we are looking at
6240 the first qualified-id production. */
6241 if (nested_name_specifier_p)
6243 tree saved_object_scope;
6244 tree saved_qualifying_scope;
6246 /* See if the next token is the `template' keyword. */
6247 if (!template_p)
6248 template_p = &template_keyword_p;
6249 *template_p = cp_parser_optional_template_keyword (parser);
6250 /* Name lookup we do during the processing of the
6251 unqualified-id might obliterate SCOPE. */
6252 saved_object_scope = parser->object_scope;
6253 saved_qualifying_scope = parser->qualifying_scope;
6254 /* Process the final unqualified-id. */
6255 id = cp_parser_unqualified_id (parser, *template_p,
6256 check_dependency_p,
6257 declarator_p,
6258 /*optional_p=*/false);
6259 /* Restore the SAVED_SCOPE for our caller. */
6260 parser->scope = scope;
6261 parser->object_scope = saved_object_scope;
6262 parser->qualifying_scope = saved_qualifying_scope;
6264 /* Otherwise, if we are in global scope, then we are looking at one
6265 of the other qualified-id productions. */
6266 else if (global_scope_p)
6268 /* If it's an identifier, and the next token is not a "<", then
6269 we can avoid the template-id case. This is an optimization
6270 for this common case. */
6271 if (token->type == CPP_NAME
6272 && !cp_parser_nth_token_starts_template_argument_list_p
6273 (parser, 2))
6274 return cp_parser_identifier (parser);
6276 cp_parser_parse_tentatively (parser);
6277 /* Try a template-id. */
6278 id = cp_parser_template_id_expr (parser,
6279 /*template_keyword_p=*/false,
6280 /*check_dependency_p=*/true,
6281 declarator_p);
6282 /* If that worked, we're done. */
6283 if (cp_parser_parse_definitely (parser))
6284 return id;
6286 /* Peek at the next token. (Changes in the token buffer may
6287 have invalidated the pointer obtained above.) */
6288 token = cp_lexer_peek_token (parser->lexer);
6290 switch (token->type)
6292 case CPP_NAME:
6293 id = cp_parser_identifier (parser);
6294 break;
6296 case CPP_KEYWORD:
6297 if (token->keyword == RID_OPERATOR)
6299 id = cp_parser_operator_function_id (parser);
6300 break;
6302 /* Fall through. */
6304 default:
6305 cp_parser_error (parser, "expected id-expression");
6306 return error_mark_node;
6309 else
6311 if (!scope)
6312 scope = parser->context->object_type;
6313 id = cp_parser_unqualified_id (parser, template_keyword_p,
6314 /*check_dependency_p=*/true,
6315 declarator_p,
6316 optional_p);
6319 if (id && TREE_CODE (id) == IDENTIFIER_NODE
6320 && warn_missing_template_keyword
6321 && !template_keyword_p
6322 /* Don't warn if we're looking inside templates. */
6323 && check_dependency_p
6324 /* In a template argument list a > could be closing
6325 the enclosing targs. */
6326 && !parser->in_template_argument_list_p
6327 && scope && dependentish_scope_p (scope)
6328 /* Don't confuse an ill-formed constructor declarator for a missing
6329 template keyword in a return type. */
6330 && !(declarator_p && constructor_name_p (id, scope))
6331 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1)
6332 && warning_enabled_at (token->location,
6333 OPT_Wmissing_template_keyword))
6335 saved_token_sentinel toks (parser->lexer, STS_ROLLBACK);
6336 if (cp_parser_skip_entire_template_parameter_list (parser)
6337 /* An operator after the > suggests that the > ends a
6338 template-id; a name or literal suggests that the > is an
6339 operator. */
6340 && (cp_lexer_peek_token (parser->lexer)->type
6341 <= CPP_LAST_PUNCTUATOR))
6342 missing_template_diag (token->location);
6345 return id;
6348 /* Parse an unqualified-id.
6350 unqualified-id:
6351 identifier
6352 operator-function-id
6353 conversion-function-id
6354 ~ class-name
6355 template-id
6357 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
6358 keyword, in a construct like `A::template ...'.
6360 Returns a representation of unqualified-id. For the `identifier'
6361 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
6362 production a BIT_NOT_EXPR is returned; the operand of the
6363 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
6364 other productions, see the documentation accompanying the
6365 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
6366 names are looked up in uninstantiated templates. If DECLARATOR_P
6367 is true, the unqualified-id is appearing as part of a declarator,
6368 rather than as part of an expression. */
6370 static cp_expr
6371 cp_parser_unqualified_id (cp_parser* parser,
6372 bool template_keyword_p,
6373 bool check_dependency_p,
6374 bool declarator_p,
6375 bool optional_p)
6377 cp_token *token;
6379 /* Peek at the next token. */
6380 token = cp_lexer_peek_token (parser->lexer);
6382 switch ((int) token->type)
6384 case CPP_NAME:
6386 tree id;
6388 /* We don't know yet whether or not this will be a
6389 template-id. */
6390 cp_parser_parse_tentatively (parser);
6391 /* Try a template-id. */
6392 id = cp_parser_template_id_expr (parser, template_keyword_p,
6393 check_dependency_p,
6394 declarator_p);
6395 /* If it worked, we're done. */
6396 if (cp_parser_parse_definitely (parser))
6397 return id;
6398 /* Otherwise, it's an ordinary identifier. */
6399 return cp_parser_identifier (parser);
6402 case CPP_TEMPLATE_ID:
6403 return cp_parser_template_id_expr (parser, template_keyword_p,
6404 check_dependency_p,
6405 declarator_p);
6407 case CPP_COMPL:
6409 tree type_decl;
6410 tree qualifying_scope;
6411 tree object_scope;
6412 tree scope;
6413 bool done;
6414 location_t tilde_loc = token->location;
6416 /* Consume the `~' token. */
6417 cp_lexer_consume_token (parser->lexer);
6418 /* Parse the class-name. The standard, as written, seems to
6419 say that:
6421 template <typename T> struct S { ~S (); };
6422 template <typename T> S<T>::~S() {}
6424 is invalid, since `~' must be followed by a class-name, but
6425 `S<T>' is dependent, and so not known to be a class.
6426 That's not right; we need to look in uninstantiated
6427 templates. A further complication arises from:
6429 template <typename T> void f(T t) {
6430 t.T::~T();
6433 Here, it is not possible to look up `T' in the scope of `T'
6434 itself. We must look in both the current scope, and the
6435 scope of the containing complete expression.
6437 Yet another issue is:
6439 struct S {
6440 int S;
6441 ~S();
6444 S::~S() {}
6446 The standard does not seem to say that the `S' in `~S'
6447 should refer to the type `S' and not the data member
6448 `S::S'. */
6450 /* DR 244 says that we look up the name after the "~" in the
6451 same scope as we looked up the qualifying name. That idea
6452 isn't fully worked out; it's more complicated than that. */
6453 scope = parser->scope;
6454 object_scope = parser->object_scope;
6455 qualifying_scope = parser->qualifying_scope;
6457 /* Check for invalid scopes. */
6458 if (scope == error_mark_node)
6460 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
6461 cp_lexer_consume_token (parser->lexer);
6462 return error_mark_node;
6464 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
6466 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6467 error_at (token->location,
6468 "scope %qT before %<~%> is not a class-name",
6469 scope);
6470 cp_parser_simulate_error (parser);
6471 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
6472 cp_lexer_consume_token (parser->lexer);
6473 return error_mark_node;
6475 if (template_keyword_p)
6477 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6478 error_at (tilde_loc, "%<template%> keyword not permitted in "
6479 "destructor name");
6480 cp_parser_simulate_error (parser);
6481 return error_mark_node;
6484 gcc_assert (!scope || TYPE_P (scope));
6486 token = cp_lexer_peek_token (parser->lexer);
6488 /* Create a location with caret == start at the tilde,
6489 finishing at the end of the peeked token, e.g:
6490 ~token
6491 ^~~~~~. */
6492 location_t loc
6493 = make_location (tilde_loc, tilde_loc, token->location);
6495 /* If the name is of the form "X::~X" it's OK even if X is a
6496 typedef. */
6498 if (scope
6499 && token->type == CPP_NAME
6500 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6501 != CPP_LESS)
6502 && (token->u.value == TYPE_IDENTIFIER (scope)
6503 || (CLASS_TYPE_P (scope)
6504 && constructor_name_p (token->u.value, scope))))
6506 cp_lexer_consume_token (parser->lexer);
6507 return build_min_nt_loc (loc, BIT_NOT_EXPR, scope);
6510 /* ~auto means the destructor of whatever the object is. */
6511 if (cp_parser_is_keyword (token, RID_AUTO))
6513 if (cxx_dialect < cxx14)
6514 pedwarn (loc, OPT_Wc__14_extensions,
6515 "%<~auto%> only available with "
6516 "%<-std=c++14%> or %<-std=gnu++14%>");
6517 cp_lexer_consume_token (parser->lexer);
6518 return build_min_nt_loc (loc, BIT_NOT_EXPR, make_auto ());
6521 /* DR 2237 (C++20 only): A simple-template-id is no longer valid as the
6522 declarator-id of a constructor or destructor. */
6523 if (token->type == CPP_TEMPLATE_ID && declarator_p
6524 && cxx_dialect >= cxx20)
6526 if (!cp_parser_simulate_error (parser))
6527 error_at (tilde_loc, "template-id not allowed for destructor");
6528 return error_mark_node;
6531 /* If there was an explicit qualification (S::~T), first look
6532 in the scope given by the qualification (i.e., S).
6534 Note: in the calls to cp_parser_class_name below we pass
6535 typename_type so that lookup finds the injected-class-name
6536 rather than the constructor. */
6537 done = false;
6538 type_decl = NULL_TREE;
6539 if (scope)
6541 cp_parser_parse_tentatively (parser);
6542 type_decl = cp_parser_class_name (parser,
6543 /*typename_keyword_p=*/false,
6544 /*template_keyword_p=*/false,
6545 typename_type,
6546 /*check_dependency=*/false,
6547 /*class_head_p=*/false,
6548 declarator_p);
6549 if (cp_parser_parse_definitely (parser))
6550 done = true;
6552 /* In "N::S::~S", look in "N" as well. */
6553 if (!done && scope && qualifying_scope)
6555 cp_parser_parse_tentatively (parser);
6556 parser->scope = qualifying_scope;
6557 parser->object_scope = NULL_TREE;
6558 parser->qualifying_scope = NULL_TREE;
6559 type_decl
6560 = cp_parser_class_name (parser,
6561 /*typename_keyword_p=*/false,
6562 /*template_keyword_p=*/false,
6563 typename_type,
6564 /*check_dependency=*/false,
6565 /*class_head_p=*/false,
6566 declarator_p);
6567 if (cp_parser_parse_definitely (parser))
6568 done = true;
6570 /* In "p->S::~T", look in the scope given by "*p" as well. */
6571 else if (!done && object_scope)
6573 cp_parser_parse_tentatively (parser);
6574 parser->scope = object_scope;
6575 parser->object_scope = NULL_TREE;
6576 parser->qualifying_scope = NULL_TREE;
6577 type_decl
6578 = cp_parser_class_name (parser,
6579 /*typename_keyword_p=*/false,
6580 /*template_keyword_p=*/false,
6581 typename_type,
6582 /*check_dependency=*/false,
6583 /*class_head_p=*/false,
6584 declarator_p);
6585 if (cp_parser_parse_definitely (parser))
6586 done = true;
6588 /* Look in the surrounding context. */
6589 if (!done)
6591 parser->scope = NULL_TREE;
6592 parser->object_scope = NULL_TREE;
6593 parser->qualifying_scope = NULL_TREE;
6594 if (processing_template_decl)
6595 cp_parser_parse_tentatively (parser);
6596 type_decl
6597 = cp_parser_class_name (parser,
6598 /*typename_keyword_p=*/false,
6599 /*template_keyword_p=*/false,
6600 typename_type,
6601 /*check_dependency=*/false,
6602 /*class_head_p=*/false,
6603 declarator_p);
6604 if (processing_template_decl
6605 && ! cp_parser_parse_definitely (parser))
6607 /* We couldn't find a type with this name. If we're parsing
6608 tentatively, fail and try something else. */
6609 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6611 cp_parser_simulate_error (parser);
6612 return error_mark_node;
6614 /* Otherwise, accept it and check for a match at instantiation
6615 time. */
6616 type_decl = cp_parser_identifier (parser);
6617 if (type_decl != error_mark_node)
6618 type_decl = build_min_nt_loc (loc, BIT_NOT_EXPR, type_decl);
6619 return type_decl;
6622 /* If an error occurred, assume that the name of the
6623 destructor is the same as the name of the qualifying
6624 class. That allows us to keep parsing after running
6625 into ill-formed destructor names. */
6626 if (type_decl == error_mark_node && scope)
6627 return build_min_nt_loc (loc, BIT_NOT_EXPR, scope);
6628 else if (type_decl == error_mark_node)
6629 return error_mark_node;
6631 /* Check that destructor name and scope match. */
6632 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6634 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6635 error_at (loc,
6636 "declaration of %<~%T%> as member of %qT",
6637 type_decl, scope);
6638 cp_parser_simulate_error (parser);
6639 return error_mark_node;
6642 /* [class.dtor]
6644 A typedef-name that names a class shall not be used as the
6645 identifier in the declarator for a destructor declaration. */
6646 if (declarator_p
6647 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6648 && !DECL_SELF_REFERENCE_P (type_decl)
6649 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6650 error_at (loc,
6651 "typedef-name %qD used as destructor declarator",
6652 type_decl);
6654 return build_min_nt_loc (loc, BIT_NOT_EXPR, TREE_TYPE (type_decl));
6657 case CPP_KEYWORD:
6658 if (token->keyword == RID_OPERATOR)
6660 cp_expr id;
6662 /* This could be a template-id, so we try that first. */
6663 cp_parser_parse_tentatively (parser);
6664 /* Try a template-id. */
6665 id = cp_parser_template_id_expr (parser, template_keyword_p,
6666 /*check_dependency_p=*/true,
6667 declarator_p);
6668 /* If that worked, we're done. */
6669 if (cp_parser_parse_definitely (parser))
6670 return id;
6671 /* We still don't know whether we're looking at an
6672 operator-function-id or a conversion-function-id. */
6673 cp_parser_parse_tentatively (parser);
6674 /* Try an operator-function-id. */
6675 id = cp_parser_operator_function_id (parser);
6676 /* If that didn't work, try a conversion-function-id. */
6677 if (!cp_parser_parse_definitely (parser))
6678 id = cp_parser_conversion_function_id (parser);
6680 return id;
6682 /* Fall through. */
6684 default:
6685 if (optional_p)
6686 return NULL_TREE;
6687 cp_parser_error (parser, "expected unqualified-id");
6688 return error_mark_node;
6692 /* Check [temp.names]/5: A name prefixed by the keyword template shall
6693 be a template-id or the name shall refer to a class template or an
6694 alias template. */
6696 static void
6697 check_template_keyword_in_nested_name_spec (tree name)
6699 if (CLASS_TYPE_P (name)
6700 && ((CLASSTYPE_USE_TEMPLATE (name)
6701 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (name)))
6702 || CLASSTYPE_IS_TEMPLATE (name)))
6703 return;
6705 if (TREE_CODE (name) == TYPENAME_TYPE
6706 && TREE_CODE (TYPENAME_TYPE_FULLNAME (name)) == TEMPLATE_ID_EXPR)
6707 return;
6708 /* Alias templates are also OK. */
6709 else if (alias_template_specialization_p (name, nt_opaque))
6710 return;
6712 permerror (input_location, TYPE_P (name)
6713 ? G_("%qT is not a template")
6714 : G_("%qD is not a template"),
6715 name);
6718 /* Parse an (optional) nested-name-specifier.
6720 nested-name-specifier: [C++98]
6721 class-or-namespace-name :: nested-name-specifier [opt]
6722 class-or-namespace-name :: template nested-name-specifier [opt]
6724 nested-name-specifier: [C++0x]
6725 type-name ::
6726 namespace-name ::
6727 nested-name-specifier identifier ::
6728 nested-name-specifier template [opt] simple-template-id ::
6730 PARSER->SCOPE should be set appropriately before this function is
6731 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6732 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6733 in name lookups.
6735 Sets PARSER->SCOPE to the class (TYPE) or namespace
6736 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6737 it unchanged if there is no nested-name-specifier. Returns the new
6738 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6740 If CHECK_DEPENDENCY_P is FALSE, names are looked up in dependent scopes.
6742 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6743 part of a declaration and/or decl-specifier. */
6745 static tree
6746 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6747 bool typename_keyword_p,
6748 bool check_dependency_p,
6749 bool type_p,
6750 bool is_declaration,
6751 bool template_keyword_p /* = false */)
6753 bool success = false;
6754 cp_token_position start = 0;
6755 cp_token *token;
6757 /* Remember where the nested-name-specifier starts. */
6758 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
6759 && cp_lexer_next_token_is_not (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
6761 start = cp_lexer_token_position (parser->lexer, false);
6762 push_deferring_access_checks (dk_deferred);
6765 while (true)
6767 tree new_scope;
6768 tree old_scope;
6769 tree saved_qualifying_scope;
6771 /* Spot cases that cannot be the beginning of a
6772 nested-name-specifier. */
6773 token = cp_lexer_peek_token (parser->lexer);
6775 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6776 the already parsed nested-name-specifier. */
6777 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6779 /* Grab the nested-name-specifier and continue the loop. */
6780 cp_parser_pre_parsed_nested_name_specifier (parser);
6781 /* If we originally encountered this nested-name-specifier
6782 with CHECK_DEPENDENCY_P set to true, we will not have
6783 resolved TYPENAME_TYPEs, so we must do so here. */
6784 if (is_declaration
6785 && !check_dependency_p
6786 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6788 new_scope = resolve_typename_type (parser->scope,
6789 /*only_current_p=*/false);
6790 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6791 parser->scope = new_scope;
6793 success = true;
6794 continue;
6797 /* Spot cases that cannot be the beginning of a
6798 nested-name-specifier. On the second and subsequent times
6799 through the loop, we look for the `template' keyword. */
6800 if (success && token->keyword == RID_TEMPLATE)
6802 /* A template-id can start a nested-name-specifier. */
6803 else if (token->type == CPP_TEMPLATE_ID)
6805 /* DR 743: decltype can be used in a nested-name-specifier. */
6806 else if (token_is_decltype (token))
6808 else
6810 /* If the next token is not an identifier, then it is
6811 definitely not a type-name or namespace-name. */
6812 if (token->type != CPP_NAME)
6813 break;
6814 /* If the following token is neither a `<' (to begin a
6815 template-id), nor a `::', then we are not looking at a
6816 nested-name-specifier. */
6817 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6819 if (token->type == CPP_COLON
6820 && parser->colon_corrects_to_scope_p
6821 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME
6822 /* name:name is a valid sequence in an Objective C message. */
6823 && !parser->objective_c_message_context_p)
6825 gcc_rich_location richloc (token->location);
6826 richloc.add_fixit_replace ("::");
6827 error_at (&richloc,
6828 "found %<:%> in nested-name-specifier, "
6829 "expected %<::%>");
6830 token->type = CPP_SCOPE;
6833 if (token->type != CPP_SCOPE
6834 && !cp_parser_nth_token_starts_template_argument_list_p
6835 (parser, 2))
6836 break;
6839 /* The nested-name-specifier is optional, so we parse
6840 tentatively. */
6841 cp_parser_parse_tentatively (parser);
6843 /* Look for the optional `template' keyword, if this isn't the
6844 first time through the loop. */
6845 if (success)
6847 template_keyword_p = cp_parser_optional_template_keyword (parser);
6848 /* DR1710: "In a qualified-id used as the name in
6849 a typename-specifier, elaborated-type-specifier, using-declaration,
6850 or class-or-decltype, an optional keyword template appearing at
6851 the top level is ignored." */
6852 if (!template_keyword_p
6853 && typename_keyword_p
6854 && cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
6855 template_keyword_p = true;
6858 /* Save the old scope since the name lookup we are about to do
6859 might destroy it. */
6860 old_scope = parser->scope;
6861 saved_qualifying_scope = parser->qualifying_scope;
6862 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6863 look up names in "X<T>::I" in order to determine that "Y" is
6864 a template. So, if we have a typename at this point, we make
6865 an effort to look through it. */
6866 if (is_declaration
6867 && !check_dependency_p
6868 && !typename_keyword_p
6869 && parser->scope
6870 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6871 parser->scope = resolve_typename_type (parser->scope,
6872 /*only_current_p=*/false);
6873 /* Parse the qualifying entity. */
6874 new_scope
6875 = cp_parser_qualifying_entity (parser,
6876 typename_keyword_p,
6877 template_keyword_p,
6878 check_dependency_p,
6879 type_p,
6880 is_declaration);
6881 /* Look for the `::' token. */
6882 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6884 /* If we found what we wanted, we keep going; otherwise, we're
6885 done. */
6886 if (!cp_parser_parse_definitely (parser))
6888 bool error_p = false;
6890 /* Restore the OLD_SCOPE since it was valid before the
6891 failed attempt at finding the last
6892 class-or-namespace-name. */
6893 parser->scope = old_scope;
6894 parser->qualifying_scope = saved_qualifying_scope;
6896 /* If the next token is a decltype, and the one after that is a
6897 `::', then the decltype has failed to resolve to a class or
6898 enumeration type. Give this error even when parsing
6899 tentatively since it can't possibly be valid--and we're going
6900 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6901 won't get another chance.*/
6902 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6903 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6904 == CPP_SCOPE))
6906 token = cp_lexer_consume_token (parser->lexer);
6907 tree dtype = token->u.tree_check_value->value;
6908 if (dtype != error_mark_node)
6909 error_at (token->location, "%<decltype%> evaluates to %qT, "
6910 "which is not a class or enumeration type",
6911 dtype);
6912 parser->scope = error_mark_node;
6913 error_p = true;
6914 /* As below. */
6915 success = true;
6916 cp_lexer_consume_token (parser->lexer);
6919 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6920 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6922 /* If we have a non-type template-id followed by ::, it can't
6923 possibly be valid. */
6924 token = cp_lexer_peek_token (parser->lexer);
6925 tree tid = token->u.tree_check_value->value;
6926 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6927 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6929 tree tmpl = NULL_TREE;
6930 if (is_overloaded_fn (tid))
6932 tree fns = get_fns (tid);
6933 if (OVL_SINGLE_P (fns))
6934 tmpl = OVL_FIRST (fns);
6935 if (function_concept_p (fns))
6936 error_at (token->location, "concept-id %qD "
6937 "in nested-name-specifier", tid);
6938 else
6939 error_at (token->location, "function template-id "
6940 "%qD in nested-name-specifier", tid);
6942 else
6944 tmpl = TREE_OPERAND (tid, 0);
6945 if (variable_concept_p (tmpl)
6946 || standard_concept_p (tmpl))
6947 error_at (token->location, "concept-id %qD "
6948 "in nested-name-specifier", tid);
6949 else
6951 /* Variable template. */
6952 gcc_assert (variable_template_p (tmpl));
6953 error_at (token->location, "variable template-id "
6954 "%qD in nested-name-specifier", tid);
6957 if (tmpl)
6958 inform (DECL_SOURCE_LOCATION (tmpl),
6959 "%qD declared here", tmpl);
6961 parser->scope = error_mark_node;
6962 error_p = true;
6963 /* As below. */
6964 success = true;
6965 cp_lexer_consume_token (parser->lexer);
6966 cp_lexer_consume_token (parser->lexer);
6970 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6971 break;
6972 /* If the next token is an identifier, and the one after
6973 that is a `::', then any valid interpretation would have
6974 found a class-or-namespace-name. */
6975 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6976 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6977 == CPP_SCOPE)
6978 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6979 != CPP_COMPL))
6981 token = cp_lexer_consume_token (parser->lexer);
6982 if (!error_p)
6984 if (!token->error_reported)
6986 tree decl;
6987 tree ambiguous_decls;
6989 decl = cp_parser_lookup_name (parser, token->u.value,
6990 none_type,
6991 /*is_template=*/false,
6992 /*is_namespace=*/false,
6993 /*check_dependency=*/true,
6994 &ambiguous_decls,
6995 token->location);
6996 if (TREE_CODE (decl) == TEMPLATE_DECL)
6997 error_at (token->location,
6998 "%qD used without template arguments",
6999 decl);
7000 else if (ambiguous_decls)
7002 // cp_parser_lookup_name has the same diagnostic,
7003 // thus make sure to emit it at most once.
7004 if (cp_parser_uncommitted_to_tentative_parse_p
7005 (parser))
7007 error_at (token->location,
7008 "reference to %qD is ambiguous",
7009 token->u.value);
7010 print_candidates (ambiguous_decls);
7012 decl = error_mark_node;
7014 else
7016 if (cxx_dialect != cxx98)
7017 cp_parser_name_lookup_error
7018 (parser, token->u.value, decl, NLE_NOT_CXX98,
7019 token->location);
7020 else
7021 cp_parser_name_lookup_error
7022 (parser, token->u.value, decl, NLE_CXX98,
7023 token->location);
7026 parser->scope = error_mark_node;
7027 error_p = true;
7028 /* Treat this as a successful nested-name-specifier
7029 due to:
7031 [basic.lookup.qual]
7033 If the name found is not a class-name (clause
7034 _class_) or namespace-name (_namespace.def_), the
7035 program is ill-formed. */
7036 success = true;
7038 cp_lexer_consume_token (parser->lexer);
7040 break;
7042 /* We've found one valid nested-name-specifier. */
7043 success = true;
7044 /* Name lookup always gives us a DECL. */
7045 if (TREE_CODE (new_scope) == TYPE_DECL)
7046 new_scope = TREE_TYPE (new_scope);
7047 /* Uses of "template" must be followed by actual templates. */
7048 if (template_keyword_p)
7049 check_template_keyword_in_nested_name_spec (new_scope);
7050 /* If it is a class scope, try to complete it; we are about to
7051 be looking up names inside the class. */
7052 if (TYPE_P (new_scope)
7053 /* Since checking types for dependency can be expensive,
7054 avoid doing it if the type is already complete. */
7055 && !COMPLETE_TYPE_P (new_scope)
7056 /* Do not try to complete dependent types. */
7057 && !dependent_type_p (new_scope))
7059 new_scope = complete_type (new_scope);
7060 /* If it is a typedef to current class, use the current
7061 class instead, as the typedef won't have any names inside
7062 it yet. */
7063 if (!COMPLETE_TYPE_P (new_scope)
7064 && currently_open_class (new_scope))
7065 new_scope = TYPE_MAIN_VARIANT (new_scope);
7067 /* Make sure we look in the right scope the next time through
7068 the loop. */
7069 parser->scope = new_scope;
7072 /* If parsing tentatively, replace the sequence of tokens that makes
7073 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
7074 token. That way, should we re-parse the token stream, we will
7075 not have to repeat the effort required to do the parse, nor will
7076 we issue duplicate error messages. */
7077 if (success && start)
7079 cp_token *token;
7081 token = cp_lexer_token_at (parser->lexer, start);
7082 /* Reset the contents of the START token. */
7083 token->type = CPP_NESTED_NAME_SPECIFIER;
7084 /* Retrieve any deferred checks. Do not pop this access checks yet
7085 so the memory will not be reclaimed during token replacing below. */
7086 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
7087 token->tree_check_p = true;
7088 token->u.tree_check_value->value = parser->scope;
7089 token->u.tree_check_value->checks = get_deferred_access_checks ();
7090 token->u.tree_check_value->qualifying_scope =
7091 parser->qualifying_scope;
7092 token->keyword = RID_MAX;
7094 /* Purge all subsequent tokens. */
7095 cp_lexer_purge_tokens_after (parser->lexer, start);
7098 if (start)
7099 pop_to_parent_deferring_access_checks ();
7101 return success ? parser->scope : NULL_TREE;
7104 /* Parse a nested-name-specifier. See
7105 cp_parser_nested_name_specifier_opt for details. This function
7106 behaves identically, except that it will an issue an error if no
7107 nested-name-specifier is present. */
7109 static tree
7110 cp_parser_nested_name_specifier (cp_parser *parser,
7111 bool typename_keyword_p,
7112 bool check_dependency_p,
7113 bool type_p,
7114 bool is_declaration)
7116 tree scope;
7118 /* Look for the nested-name-specifier. */
7119 scope = cp_parser_nested_name_specifier_opt (parser,
7120 typename_keyword_p,
7121 check_dependency_p,
7122 type_p,
7123 is_declaration);
7124 /* If it was not present, issue an error message. */
7125 if (!scope)
7127 cp_parser_error (parser, "expected nested-name-specifier");
7128 parser->scope = NULL_TREE;
7131 return scope;
7134 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
7135 this is either a class-name or a namespace-name (which corresponds
7136 to the class-or-namespace-name production in the grammar). For
7137 C++0x, it can also be a type-name that refers to an enumeration
7138 type or a simple-template-id.
7140 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
7141 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
7142 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
7143 TYPE_P is TRUE iff the next name should be taken as a class-name,
7144 even the same name is declared to be another entity in the same
7145 scope.
7147 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
7148 specified by the class-or-namespace-name. If neither is found the
7149 ERROR_MARK_NODE is returned. */
7151 static tree
7152 cp_parser_qualifying_entity (cp_parser *parser,
7153 bool typename_keyword_p,
7154 bool template_keyword_p,
7155 bool check_dependency_p,
7156 bool type_p,
7157 bool is_declaration)
7159 tree saved_scope;
7160 tree saved_qualifying_scope;
7161 tree saved_object_scope;
7162 tree scope;
7163 bool only_class_p;
7164 bool successful_parse_p;
7166 /* DR 743: decltype can appear in a nested-name-specifier. */
7167 if (cp_lexer_next_token_is_decltype (parser->lexer))
7169 scope = cp_parser_decltype (parser);
7170 if (TREE_CODE (scope) != ENUMERAL_TYPE
7171 && !MAYBE_CLASS_TYPE_P (scope))
7173 cp_parser_simulate_error (parser);
7174 return error_mark_node;
7176 if (TYPE_NAME (scope))
7177 scope = TYPE_NAME (scope);
7178 return scope;
7181 /* Before we try to parse the class-name, we must save away the
7182 current PARSER->SCOPE since cp_parser_class_name will destroy
7183 it. */
7184 saved_scope = parser->scope;
7185 saved_qualifying_scope = parser->qualifying_scope;
7186 saved_object_scope = parser->object_scope;
7187 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
7188 there is no need to look for a namespace-name. */
7189 only_class_p = template_keyword_p
7190 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
7191 if (!only_class_p)
7192 cp_parser_parse_tentatively (parser);
7193 scope = cp_parser_class_name (parser,
7194 typename_keyword_p,
7195 template_keyword_p,
7196 type_p ? class_type : none_type,
7197 check_dependency_p,
7198 /*class_head_p=*/false,
7199 is_declaration,
7200 /*enum_ok=*/cxx_dialect > cxx98);
7201 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
7202 /* If that didn't work, try for a namespace-name. */
7203 if (!only_class_p && !successful_parse_p)
7205 /* Restore the saved scope. */
7206 parser->scope = saved_scope;
7207 parser->qualifying_scope = saved_qualifying_scope;
7208 parser->object_scope = saved_object_scope;
7209 /* If we are not looking at an identifier followed by the scope
7210 resolution operator, then this is not part of a
7211 nested-name-specifier. (Note that this function is only used
7212 to parse the components of a nested-name-specifier.) */
7213 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
7214 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
7215 return error_mark_node;
7216 scope = cp_parser_namespace_name (parser);
7219 return scope;
7222 /* Return true if we are looking at a compound-literal, false otherwise. */
7224 static bool
7225 cp_parser_compound_literal_p (cp_parser *parser)
7227 cp_lexer_save_tokens (parser->lexer);
7229 /* Skip tokens until the next token is a closing parenthesis.
7230 If we find the closing `)', and the next token is a `{', then
7231 we are looking at a compound-literal. */
7232 bool compound_literal_p
7233 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7234 /*consume_paren=*/true)
7235 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
7237 /* Roll back the tokens we skipped. */
7238 cp_lexer_rollback_tokens (parser->lexer);
7240 return compound_literal_p;
7243 /* Return true if EXPR is the integer constant zero or a complex constant
7244 of zero, without any folding, but ignoring location wrappers. */
7246 bool
7247 literal_integer_zerop (const_tree expr)
7249 return (location_wrapper_p (expr)
7250 && integer_zerop (TREE_OPERAND (expr, 0)));
7253 /* Parse a postfix-expression.
7255 postfix-expression:
7256 primary-expression
7257 postfix-expression [ expression ]
7258 postfix-expression ( expression-list [opt] )
7259 simple-type-specifier ( expression-list [opt] )
7260 typename :: [opt] nested-name-specifier identifier
7261 ( expression-list [opt] )
7262 typename :: [opt] nested-name-specifier template [opt] template-id
7263 ( expression-list [opt] )
7264 postfix-expression . template [opt] id-expression
7265 postfix-expression -> template [opt] id-expression
7266 postfix-expression . pseudo-destructor-name
7267 postfix-expression -> pseudo-destructor-name
7268 postfix-expression ++
7269 postfix-expression --
7270 dynamic_cast < type-id > ( expression )
7271 static_cast < type-id > ( expression )
7272 reinterpret_cast < type-id > ( expression )
7273 const_cast < type-id > ( expression )
7274 typeid ( expression )
7275 typeid ( type-id )
7277 GNU Extension:
7279 postfix-expression:
7280 ( type-id ) { initializer-list , [opt] }
7282 This extension is a GNU version of the C99 compound-literal
7283 construct. (The C99 grammar uses `type-name' instead of `type-id',
7284 but they are essentially the same concept.)
7286 If ADDRESS_P is true, the postfix expression is the operand of the
7287 `&' operator. CAST_P is true if this expression is the target of a
7288 cast.
7290 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
7291 class member access expressions [expr.ref].
7293 Returns a representation of the expression. */
7295 static cp_expr
7296 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
7297 bool member_access_only_p, bool decltype_p,
7298 cp_id_kind * pidk_return)
7300 cp_token *token;
7301 location_t loc;
7302 enum rid keyword;
7303 cp_id_kind idk = CP_ID_KIND_NONE;
7304 cp_expr postfix_expression = NULL_TREE;
7305 bool is_member_access = false;
7307 /* Peek at the next token. */
7308 token = cp_lexer_peek_token (parser->lexer);
7309 loc = token->location;
7310 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
7312 /* Some of the productions are determined by keywords. */
7313 keyword = token->keyword;
7314 switch (keyword)
7316 case RID_DYNCAST:
7317 case RID_STATCAST:
7318 case RID_REINTCAST:
7319 case RID_CONSTCAST:
7321 tree type;
7322 cp_expr expression;
7323 const char *saved_message;
7324 bool saved_in_type_id_in_expr_p;
7326 /* All of these can be handled in the same way from the point
7327 of view of parsing. Begin by consuming the token
7328 identifying the cast. */
7329 cp_lexer_consume_token (parser->lexer);
7331 /* New types cannot be defined in the cast. */
7332 saved_message = parser->type_definition_forbidden_message;
7333 parser->type_definition_forbidden_message
7334 = G_("types may not be defined in casts");
7336 /* Look for the opening `<'. */
7337 cp_parser_require (parser, CPP_LESS, RT_LESS);
7338 /* Parse the type to which we are casting. */
7339 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7340 parser->in_type_id_in_expr_p = true;
7341 type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
7342 NULL);
7343 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7344 /* Look for the closing `>'. */
7345 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
7346 /* Restore the old message. */
7347 parser->type_definition_forbidden_message = saved_message;
7349 bool saved_greater_than_is_operator_p
7350 = parser->greater_than_is_operator_p;
7351 parser->greater_than_is_operator_p = true;
7353 /* And the expression which is being cast. */
7354 matching_parens parens;
7355 parens.require_open (parser);
7356 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
7357 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
7358 RT_CLOSE_PAREN);
7359 location_t end_loc = close_paren ?
7360 close_paren->location : UNKNOWN_LOCATION;
7362 parser->greater_than_is_operator_p
7363 = saved_greater_than_is_operator_p;
7365 /* Only type conversions to integral or enumeration types
7366 can be used in constant-expressions. */
7367 if (!cast_valid_in_integral_constant_expression_p (type)
7368 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
7370 postfix_expression = error_mark_node;
7371 break;
7374 /* Construct a location e.g. :
7375 reinterpret_cast <int *> (expr)
7376 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7377 ranging from the start of the "*_cast" token to the final closing
7378 paren, with the caret at the start. */
7379 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
7381 switch (keyword)
7383 case RID_DYNCAST:
7384 postfix_expression
7385 = build_dynamic_cast (cp_cast_loc, type, expression,
7386 tf_warning_or_error);
7387 break;
7388 case RID_STATCAST:
7389 postfix_expression
7390 = build_static_cast (cp_cast_loc, type, expression,
7391 tf_warning_or_error);
7392 break;
7393 case RID_REINTCAST:
7394 postfix_expression
7395 = build_reinterpret_cast (cp_cast_loc, type, expression,
7396 tf_warning_or_error);
7397 break;
7398 case RID_CONSTCAST:
7399 postfix_expression
7400 = build_const_cast (cp_cast_loc, type, expression,
7401 tf_warning_or_error);
7402 break;
7403 default:
7404 gcc_unreachable ();
7407 break;
7409 case RID_TYPEID:
7411 tree type;
7412 const char *saved_message;
7413 bool saved_in_type_id_in_expr_p;
7415 /* Consume the `typeid' token. */
7416 cp_lexer_consume_token (parser->lexer);
7417 /* Look for the `(' token. */
7418 matching_parens parens;
7419 parens.require_open (parser);
7420 /* Types cannot be defined in a `typeid' expression. */
7421 saved_message = parser->type_definition_forbidden_message;
7422 parser->type_definition_forbidden_message
7423 = G_("types may not be defined in a %<typeid%> expression");
7424 /* We can't be sure yet whether we're looking at a type-id or an
7425 expression. */
7426 cp_parser_parse_tentatively (parser);
7427 /* Try a type-id first. */
7428 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7429 parser->in_type_id_in_expr_p = true;
7430 type = cp_parser_type_id (parser);
7431 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7432 /* Look for the `)' token. Otherwise, we can't be sure that
7433 we're not looking at an expression: consider `typeid (int
7434 (3))', for example. */
7435 cp_token *close_paren = parens.require_close (parser);
7436 /* If all went well, simply lookup the type-id. */
7437 if (cp_parser_parse_definitely (parser))
7438 postfix_expression = get_typeid (type, tf_warning_or_error);
7439 /* Otherwise, fall back to the expression variant. */
7440 else
7442 tree expression;
7444 /* Look for an expression. */
7445 expression = cp_parser_expression (parser, & idk);
7446 /* Compute its typeid. */
7447 postfix_expression = build_typeid (expression, tf_warning_or_error);
7448 /* Look for the `)' token. */
7449 close_paren = parens.require_close (parser);
7451 /* Restore the saved message. */
7452 parser->type_definition_forbidden_message = saved_message;
7453 /* `typeid' may not appear in an integral constant expression. */
7454 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
7455 postfix_expression = error_mark_node;
7457 /* Construct a location e.g. :
7458 typeid (expr)
7459 ^~~~~~~~~~~~~
7460 ranging from the start of the "typeid" token to the final closing
7461 paren, with the caret at the start. */
7462 if (close_paren)
7464 location_t typeid_loc
7465 = make_location (start_loc, start_loc, close_paren->location);
7466 postfix_expression.set_location (typeid_loc);
7467 postfix_expression.maybe_add_location_wrapper ();
7470 break;
7472 case RID_TYPENAME:
7474 tree type;
7475 /* The syntax permitted here is the same permitted for an
7476 elaborated-type-specifier. */
7477 ++parser->prevent_constrained_type_specifiers;
7478 type = cp_parser_elaborated_type_specifier (parser,
7479 /*is_friend=*/false,
7480 /*is_declaration=*/false);
7481 --parser->prevent_constrained_type_specifiers;
7482 postfix_expression = cp_parser_functional_cast (parser, type);
7484 break;
7486 case RID_ADDRESSOF:
7487 case RID_BUILTIN_SHUFFLE:
7488 case RID_BUILTIN_SHUFFLEVECTOR:
7489 case RID_BUILTIN_LAUNDER:
7490 case RID_BUILTIN_ASSOC_BARRIER:
7492 vec<tree, va_gc> *vec;
7494 cp_lexer_consume_token (parser->lexer);
7495 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
7496 /*cast_p=*/false, /*allow_expansion_p=*/true,
7497 /*non_constant_p=*/NULL);
7498 if (vec == NULL)
7500 postfix_expression = error_mark_node;
7501 break;
7504 for (tree p : *vec)
7505 mark_exp_read (p);
7507 switch (keyword)
7509 case RID_ADDRESSOF:
7510 if (vec->length () == 1)
7511 postfix_expression
7512 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
7513 else
7515 error_at (loc, "wrong number of arguments to "
7516 "%<__builtin_addressof%>");
7517 postfix_expression = error_mark_node;
7519 break;
7521 case RID_BUILTIN_LAUNDER:
7522 if (vec->length () == 1)
7523 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
7524 tf_warning_or_error);
7525 else
7527 error_at (loc, "wrong number of arguments to "
7528 "%<__builtin_launder%>");
7529 postfix_expression = error_mark_node;
7531 break;
7533 case RID_BUILTIN_ASSOC_BARRIER:
7534 if (vec->length () == 1)
7535 postfix_expression = build1_loc (loc, PAREN_EXPR,
7536 TREE_TYPE ((*vec)[0]),
7537 (*vec)[0]);
7538 else
7540 error_at (loc, "wrong number of arguments to "
7541 "%<__builtin_assoc_barrier%>");
7542 postfix_expression = error_mark_node;
7544 break;
7546 case RID_BUILTIN_SHUFFLE:
7547 if (vec->length () == 2)
7548 postfix_expression
7549 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
7550 (*vec)[1], tf_warning_or_error);
7551 else if (vec->length () == 3)
7552 postfix_expression
7553 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
7554 (*vec)[2], tf_warning_or_error);
7555 else
7557 error_at (loc, "wrong number of arguments to "
7558 "%<__builtin_shuffle%>");
7559 postfix_expression = error_mark_node;
7561 break;
7563 case RID_BUILTIN_SHUFFLEVECTOR:
7564 if (vec->length () < 3)
7566 error_at (loc, "wrong number of arguments to "
7567 "%<__builtin_shufflevector%>");
7568 postfix_expression = error_mark_node;
7570 else
7572 postfix_expression
7573 = build_x_shufflevector (loc, vec, tf_warning_or_error);
7575 break;
7577 default:
7578 gcc_unreachable ();
7580 break;
7583 case RID_BUILTIN_CONVERTVECTOR:
7585 tree expression;
7586 tree type;
7587 /* Consume the `__builtin_convertvector' token. */
7588 cp_lexer_consume_token (parser->lexer);
7589 /* Look for the opening `('. */
7590 matching_parens parens;
7591 parens.require_open (parser);
7592 /* Now, parse the assignment-expression. */
7593 expression = cp_parser_assignment_expression (parser);
7594 /* Look for the `,'. */
7595 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7596 location_t type_location
7597 = cp_lexer_peek_token (parser->lexer)->location;
7598 /* Parse the type-id. */
7600 type_id_in_expr_sentinel s (parser);
7601 type = cp_parser_type_id (parser);
7603 /* Look for the closing `)'. */
7604 parens.require_close (parser);
7605 postfix_expression
7606 = cp_build_vec_convert (expression, type_location, type,
7607 tf_warning_or_error);
7608 break;
7611 case RID_BUILTIN_BIT_CAST:
7613 tree expression;
7614 tree type;
7615 /* Consume the `__builtin_bit_cast' token. */
7616 cp_lexer_consume_token (parser->lexer);
7617 /* Look for the opening `('. */
7618 matching_parens parens;
7619 parens.require_open (parser);
7620 location_t type_location
7621 = cp_lexer_peek_token (parser->lexer)->location;
7622 /* Parse the type-id. */
7624 type_id_in_expr_sentinel s (parser);
7625 type = cp_parser_type_id (parser);
7627 /* Look for the `,'. */
7628 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7629 /* Now, parse the assignment-expression. */
7630 expression = cp_parser_assignment_expression (parser);
7631 /* Look for the closing `)'. */
7632 parens.require_close (parser);
7633 postfix_expression
7634 = cp_build_bit_cast (type_location, type, expression,
7635 tf_warning_or_error);
7636 break;
7639 default:
7641 tree type;
7643 /* If the next thing is a simple-type-specifier, we may be
7644 looking at a functional cast. We could also be looking at
7645 an id-expression. So, we try the functional cast, and if
7646 that doesn't work we fall back to the primary-expression. */
7647 cp_parser_parse_tentatively (parser);
7648 /* Look for the simple-type-specifier. */
7649 ++parser->prevent_constrained_type_specifiers;
7650 type = cp_parser_simple_type_specifier (parser,
7651 /*decl_specs=*/NULL,
7652 CP_PARSER_FLAGS_NONE);
7653 --parser->prevent_constrained_type_specifiers;
7654 /* Parse the cast itself. */
7655 if (!cp_parser_error_occurred (parser))
7656 postfix_expression
7657 = cp_parser_functional_cast (parser, type);
7658 /* If that worked, we're done. */
7659 if (cp_parser_parse_definitely (parser))
7660 break;
7662 /* If the functional-cast didn't work out, try a
7663 compound-literal. */
7664 if (cp_parser_allow_gnu_extensions_p (parser)
7665 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7667 cp_expr initializer = NULL_TREE;
7669 cp_parser_parse_tentatively (parser);
7671 matching_parens parens;
7672 parens.consume_open (parser);
7674 /* Avoid calling cp_parser_type_id pointlessly, see comment
7675 in cp_parser_cast_expression about c++/29234. */
7676 if (!cp_parser_compound_literal_p (parser))
7677 cp_parser_simulate_error (parser);
7678 else
7680 /* Parse the type. */
7681 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7682 parser->in_type_id_in_expr_p = true;
7683 type = cp_parser_type_id (parser);
7684 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7685 parens.require_close (parser);
7688 /* If things aren't going well, there's no need to
7689 keep going. */
7690 if (!cp_parser_error_occurred (parser))
7692 bool non_constant_p;
7693 /* Parse the brace-enclosed initializer list. */
7694 initializer = cp_parser_braced_list (parser,
7695 &non_constant_p);
7697 /* If that worked, we're definitely looking at a
7698 compound-literal expression. */
7699 if (cp_parser_parse_definitely (parser))
7701 /* Warn the user that a compound literal is not
7702 allowed in standard C++. */
7703 pedwarn (input_location, OPT_Wpedantic,
7704 "ISO C++ forbids compound-literals");
7705 /* For simplicity, we disallow compound literals in
7706 constant-expressions. We could
7707 allow compound literals of integer type, whose
7708 initializer was a constant, in constant
7709 expressions. Permitting that usage, as a further
7710 extension, would not change the meaning of any
7711 currently accepted programs. (Of course, as
7712 compound literals are not part of ISO C++, the
7713 standard has nothing to say.) */
7714 if (cp_parser_non_integral_constant_expression (parser,
7715 NIC_NCC))
7717 postfix_expression = error_mark_node;
7718 break;
7720 /* Form the representation of the compound-literal. */
7721 postfix_expression
7722 = finish_compound_literal (type, initializer,
7723 tf_warning_or_error, fcl_c99);
7724 postfix_expression.set_location (initializer.get_location ());
7725 break;
7729 /* It must be a primary-expression. */
7730 postfix_expression
7731 = cp_parser_primary_expression (parser, address_p, cast_p,
7732 /*template_arg_p=*/false,
7733 decltype_p,
7734 &idk);
7736 break;
7739 /* Note that we don't need to worry about calling build_cplus_new on a
7740 class-valued CALL_EXPR in decltype when it isn't the end of the
7741 postfix-expression; unary_complex_lvalue will take care of that for
7742 all these cases. */
7744 /* Keep looping until the postfix-expression is complete. */
7745 while (true)
7747 if (idk == CP_ID_KIND_UNQUALIFIED
7748 && identifier_p (postfix_expression)
7749 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7750 /* It is not a Koenig lookup function call. */
7751 postfix_expression
7752 = unqualified_name_lookup_error (postfix_expression);
7754 /* Peek at the next token. */
7755 token = cp_lexer_peek_token (parser->lexer);
7757 switch (token->type)
7759 case CPP_OPEN_SQUARE:
7760 if (cp_next_tokens_can_be_std_attribute_p (parser))
7762 cp_parser_error (parser,
7763 "two consecutive %<[%> shall "
7764 "only introduce an attribute");
7765 return error_mark_node;
7767 postfix_expression
7768 = cp_parser_postfix_open_square_expression (parser,
7769 postfix_expression,
7770 false,
7771 decltype_p);
7772 postfix_expression.set_range (start_loc,
7773 postfix_expression.get_location ());
7775 idk = CP_ID_KIND_NONE;
7776 is_member_access = false;
7777 break;
7779 case CPP_OPEN_PAREN:
7780 /* postfix-expression ( expression-list [opt] ) */
7782 bool koenig_p;
7783 bool is_builtin_constant_p;
7784 bool saved_integral_constant_expression_p = false;
7785 bool saved_non_integral_constant_expression_p = false;
7786 tsubst_flags_t complain = complain_flags (decltype_p);
7787 vec<tree, va_gc> *args;
7788 location_t close_paren_loc = UNKNOWN_LOCATION;
7789 location_t combined_loc = UNKNOWN_LOCATION;
7791 is_member_access = false;
7793 tree stripped_expression
7794 = tree_strip_any_location_wrapper (postfix_expression);
7795 is_builtin_constant_p
7796 = DECL_IS_BUILTIN_CONSTANT_P (stripped_expression);
7797 if (is_builtin_constant_p)
7799 /* The whole point of __builtin_constant_p is to allow
7800 non-constant expressions to appear as arguments. */
7801 saved_integral_constant_expression_p
7802 = parser->integral_constant_expression_p;
7803 saved_non_integral_constant_expression_p
7804 = parser->non_integral_constant_expression_p;
7805 parser->integral_constant_expression_p = false;
7807 args = (cp_parser_parenthesized_expression_list
7808 (parser, non_attr,
7809 /*cast_p=*/false, /*allow_expansion_p=*/true,
7810 /*non_constant_p=*/NULL,
7811 /*close_paren_loc=*/&close_paren_loc,
7812 /*wrap_locations_p=*/true));
7813 if (is_builtin_constant_p)
7815 parser->integral_constant_expression_p
7816 = saved_integral_constant_expression_p;
7817 parser->non_integral_constant_expression_p
7818 = saved_non_integral_constant_expression_p;
7821 if (args == NULL)
7823 postfix_expression = error_mark_node;
7824 break;
7827 /* Function calls are not permitted in
7828 constant-expressions. */
7829 if (! builtin_valid_in_constant_expr_p (postfix_expression)
7830 && cp_parser_non_integral_constant_expression (parser,
7831 NIC_FUNC_CALL))
7833 postfix_expression = error_mark_node;
7834 release_tree_vector (args);
7835 break;
7838 koenig_p = false;
7839 if (idk == CP_ID_KIND_UNQUALIFIED
7840 || idk == CP_ID_KIND_TEMPLATE_ID)
7842 if (identifier_p (postfix_expression)
7843 /* In C++20, we may need to perform ADL for a template
7844 name. */
7845 || (TREE_CODE (postfix_expression) == TEMPLATE_ID_EXPR
7846 && identifier_p (TREE_OPERAND (postfix_expression, 0))))
7848 if (!args->is_empty ())
7850 koenig_p = true;
7851 if (!any_type_dependent_arguments_p (args))
7852 postfix_expression
7853 = perform_koenig_lookup (postfix_expression, args,
7854 complain);
7856 else
7857 postfix_expression
7858 = unqualified_fn_lookup_error (postfix_expression);
7860 /* We do not perform argument-dependent lookup if
7861 normal lookup finds a non-function, in accordance
7862 with the expected resolution of DR 218. */
7863 else if (!args->is_empty ()
7864 && is_overloaded_fn (postfix_expression))
7866 /* Do not do argument dependent lookup if regular
7867 lookup finds a member function or a block-scope
7868 function declaration. [basic.lookup.argdep]/3 */
7869 bool do_adl_p = true;
7870 tree fns = get_fns (postfix_expression);
7871 for (lkp_iterator iter (fns); iter; ++iter)
7873 tree fn = STRIP_TEMPLATE (*iter);
7874 if ((TREE_CODE (fn) == USING_DECL
7875 && DECL_DEPENDENT_P (fn))
7876 || DECL_FUNCTION_MEMBER_P (fn)
7877 || DECL_LOCAL_DECL_P (fn))
7879 do_adl_p = false;
7880 break;
7884 if (do_adl_p)
7886 koenig_p = true;
7887 if (!any_type_dependent_arguments_p (args))
7888 postfix_expression
7889 = perform_koenig_lookup (postfix_expression, args,
7890 complain);
7895 /* Temporarily set input_location to the combined location
7896 with call expression range, as e.g. build_out_target_exprs
7897 called from convert_default_arg relies on input_location,
7898 so updating it only when the call is fully built results
7899 in inconsistencies between location handling in templates
7900 and outside of templates. */
7901 if (close_paren_loc != UNKNOWN_LOCATION)
7902 combined_loc = make_location (token->location, start_loc,
7903 close_paren_loc);
7904 iloc_sentinel ils (combined_loc);
7906 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7908 tree instance = TREE_OPERAND (postfix_expression, 0);
7909 tree fn = TREE_OPERAND (postfix_expression, 1);
7911 if (processing_template_decl
7912 && (type_dependent_object_expression_p (instance)
7913 || (!BASELINK_P (fn)
7914 && TREE_CODE (fn) != FIELD_DECL)
7915 || type_dependent_expression_p (fn)
7916 || any_type_dependent_arguments_p (args)))
7918 maybe_generic_this_capture (instance, fn);
7919 postfix_expression
7920 = build_min_nt_call_vec (postfix_expression, args);
7922 else if (BASELINK_P (fn))
7924 postfix_expression
7925 = (build_new_method_call
7926 (instance, fn, &args, NULL_TREE,
7927 (idk == CP_ID_KIND_QUALIFIED
7928 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7929 : LOOKUP_NORMAL),
7930 /*fn_p=*/NULL,
7931 complain));
7933 else
7934 postfix_expression
7935 = finish_call_expr (postfix_expression, &args,
7936 /*disallow_virtual=*/false,
7937 /*koenig_p=*/false,
7938 complain);
7940 else if (TREE_CODE (postfix_expression) == OFFSET_REF
7941 || TREE_CODE (postfix_expression) == MEMBER_REF
7942 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7943 postfix_expression = (build_offset_ref_call_from_tree
7944 (postfix_expression, &args,
7945 complain));
7946 else if (idk == CP_ID_KIND_QUALIFIED)
7947 /* A call to a static class member, or a namespace-scope
7948 function. */
7949 postfix_expression
7950 = finish_call_expr (postfix_expression, &args,
7951 /*disallow_virtual=*/true,
7952 koenig_p,
7953 complain);
7954 else
7955 /* All other function calls. */
7956 postfix_expression
7957 = finish_call_expr (postfix_expression, &args,
7958 /*disallow_virtual=*/false,
7959 koenig_p,
7960 complain);
7962 if (close_paren_loc != UNKNOWN_LOCATION)
7963 postfix_expression.set_location (combined_loc);
7965 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7966 idk = CP_ID_KIND_NONE;
7968 release_tree_vector (args);
7970 break;
7972 case CPP_DOT:
7973 case CPP_DEREF:
7974 /* postfix-expression . template [opt] id-expression
7975 postfix-expression . pseudo-destructor-name
7976 postfix-expression -> template [opt] id-expression
7977 postfix-expression -> pseudo-destructor-name */
7979 /* Consume the `.' or `->' operator. */
7980 cp_lexer_consume_token (parser->lexer);
7982 postfix_expression
7983 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7984 postfix_expression,
7985 false, &idk, loc);
7987 is_member_access = true;
7988 break;
7990 case CPP_PLUS_PLUS:
7991 /* postfix-expression ++ */
7992 /* Consume the `++' token. */
7993 cp_lexer_consume_token (parser->lexer);
7994 /* Generate a representation for the complete expression. */
7995 postfix_expression
7996 = finish_increment_expr (postfix_expression,
7997 POSTINCREMENT_EXPR);
7998 /* Increments may not appear in constant-expressions. */
7999 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
8000 postfix_expression = error_mark_node;
8001 idk = CP_ID_KIND_NONE;
8002 is_member_access = false;
8003 break;
8005 case CPP_MINUS_MINUS:
8006 /* postfix-expression -- */
8007 /* Consume the `--' token. */
8008 cp_lexer_consume_token (parser->lexer);
8009 /* Generate a representation for the complete expression. */
8010 postfix_expression
8011 = finish_increment_expr (postfix_expression,
8012 POSTDECREMENT_EXPR);
8013 /* Decrements may not appear in constant-expressions. */
8014 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
8015 postfix_expression = error_mark_node;
8016 idk = CP_ID_KIND_NONE;
8017 is_member_access = false;
8018 break;
8020 default:
8021 if (pidk_return != NULL)
8022 * pidk_return = idk;
8023 if (member_access_only_p)
8024 return is_member_access
8025 ? postfix_expression
8026 : cp_expr (error_mark_node);
8027 else
8028 return postfix_expression;
8033 /* Helper function for cp_parser_parenthesized_expression_list and
8034 cp_parser_postfix_open_square_expression. Parse a single element
8035 of parenthesized expression list. */
8037 static cp_expr
8038 cp_parser_parenthesized_expression_list_elt (cp_parser *parser, bool cast_p,
8039 bool allow_expansion_p,
8040 bool *non_constant_p)
8042 cp_expr expr (NULL_TREE);
8043 bool expr_non_constant_p;
8045 /* Parse the next assignment-expression. */
8046 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8048 /* A braced-init-list. */
8049 cp_lexer_set_source_position (parser->lexer);
8050 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8051 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8052 if (non_constant_p && expr_non_constant_p)
8053 *non_constant_p = true;
8055 else if (non_constant_p)
8057 expr = cp_parser_constant_expression (parser,
8058 /*allow_non_constant_p=*/true,
8059 &expr_non_constant_p);
8060 if (expr_non_constant_p)
8061 *non_constant_p = true;
8063 else
8064 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL, cast_p);
8066 /* If we have an ellipsis, then this is an expression expansion. */
8067 if (allow_expansion_p
8068 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8070 /* Consume the `...'. */
8071 cp_lexer_consume_token (parser->lexer);
8073 /* Build the argument pack. */
8074 expr = make_pack_expansion (expr);
8076 return expr;
8079 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
8080 by cp_parser_builtin_offsetof. We're looking for
8082 postfix-expression [ expression ]
8083 postfix-expression [ braced-init-list ] (C++11)
8084 postfix-expression [ expression-list[opt] ] (C++23)
8086 FOR_OFFSETOF is set if we're being called in that context, which
8087 changes how we deal with integer constant expressions. */
8089 static tree
8090 cp_parser_postfix_open_square_expression (cp_parser *parser,
8091 tree postfix_expression,
8092 bool for_offsetof,
8093 bool decltype_p)
8095 tree index = NULL_TREE;
8096 releasing_vec expression_list = NULL;
8097 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8098 bool saved_greater_than_is_operator_p;
8100 /* Consume the `[' token. */
8101 cp_lexer_consume_token (parser->lexer);
8103 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
8104 parser->greater_than_is_operator_p = true;
8106 /* Parse the index expression. */
8107 /* ??? For offsetof, there is a question of what to allow here. If
8108 offsetof is not being used in an integral constant expression context,
8109 then we *could* get the right answer by computing the value at runtime.
8110 If we are in an integral constant expression context, then we might
8111 could accept any constant expression; hard to say without analysis.
8112 Rather than open the barn door too wide right away, allow only integer
8113 constant expressions here. */
8114 if (for_offsetof)
8115 index = cp_parser_constant_expression (parser);
8116 else
8118 if (cxx_dialect >= cxx23
8119 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
8120 *&expression_list = make_tree_vector ();
8121 else if (cxx_dialect >= cxx23)
8123 while (true)
8125 cp_expr expr
8126 = cp_parser_parenthesized_expression_list_elt (parser,
8127 /*cast_p=*/
8128 false,
8129 /*allow_exp_p=*/
8130 true,
8131 /*non_cst_p=*/
8132 NULL);
8134 if (expr == error_mark_node)
8135 index = error_mark_node;
8136 else if (expression_list.get () == NULL
8137 && !PACK_EXPANSION_P (expr.get_value ()))
8138 index = expr.get_value ();
8139 else
8140 vec_safe_push (expression_list, expr.get_value ());
8142 /* If the next token isn't a `,', then we are done. */
8143 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8144 break;
8146 if (expression_list.get () == NULL && index != error_mark_node)
8148 *&expression_list = make_tree_vector_single (index);
8149 index = NULL_TREE;
8152 /* Otherwise, consume the `,' and keep going. */
8153 cp_lexer_consume_token (parser->lexer);
8155 if (expression_list.get () && index == error_mark_node)
8156 expression_list.release ();
8158 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8160 bool expr_nonconst_p;
8161 cp_lexer_set_source_position (parser->lexer);
8162 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8163 index = cp_parser_braced_list (parser, &expr_nonconst_p);
8165 else
8166 index = cp_parser_expression (parser, NULL, /*cast_p=*/false,
8167 /*decltype_p=*/false,
8168 /*warn_comma_p=*/warn_comma_subscript);
8171 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
8173 /* Look for the closing `]'. */
8174 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8176 /* Build the ARRAY_REF. */
8177 postfix_expression = grok_array_decl (loc, postfix_expression,
8178 index, &expression_list,
8179 tf_warning_or_error
8180 | (decltype_p ? tf_decltype : 0));
8182 /* When not doing offsetof, array references are not permitted in
8183 constant-expressions. */
8184 if (!for_offsetof
8185 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
8186 postfix_expression = error_mark_node;
8188 return postfix_expression;
8191 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
8192 dereference of incomplete type, returns true if error_mark_node should
8193 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
8194 and *DEPENDENT_P. */
8196 bool
8197 cp_parser_dot_deref_incomplete (tree *scope, cp_expr *postfix_expression,
8198 bool *dependent_p)
8200 /* In a template, be permissive by treating an object expression
8201 of incomplete type as dependent (after a pedwarn). */
8202 diagnostic_t kind = (processing_template_decl
8203 && MAYBE_CLASS_TYPE_P (*scope) ? DK_PEDWARN : DK_ERROR);
8205 switch (TREE_CODE (*postfix_expression))
8207 case CAST_EXPR:
8208 case REINTERPRET_CAST_EXPR:
8209 case CONST_CAST_EXPR:
8210 case STATIC_CAST_EXPR:
8211 case DYNAMIC_CAST_EXPR:
8212 case IMPLICIT_CONV_EXPR:
8213 case VIEW_CONVERT_EXPR:
8214 case NON_LVALUE_EXPR:
8215 kind = DK_ERROR;
8216 break;
8217 case OVERLOAD:
8218 /* Don't emit any diagnostic for OVERLOADs. */
8219 kind = DK_IGNORED;
8220 break;
8221 default:
8222 /* Avoid clobbering e.g. DECLs. */
8223 if (!EXPR_P (*postfix_expression))
8224 kind = DK_ERROR;
8225 break;
8228 if (kind == DK_IGNORED)
8229 return false;
8231 location_t exploc = location_of (*postfix_expression);
8232 cxx_incomplete_type_diagnostic (exploc, *postfix_expression, *scope, kind);
8233 if (!MAYBE_CLASS_TYPE_P (*scope))
8234 return true;
8235 if (kind == DK_ERROR)
8236 *scope = *postfix_expression = error_mark_node;
8237 else if (processing_template_decl)
8239 *dependent_p = true;
8240 *scope = TREE_TYPE (*postfix_expression) = NULL_TREE;
8242 return false;
8245 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
8246 by cp_parser_builtin_offsetof. We're looking for
8248 postfix-expression . template [opt] id-expression
8249 postfix-expression . pseudo-destructor-name
8250 postfix-expression -> template [opt] id-expression
8251 postfix-expression -> pseudo-destructor-name
8253 FOR_OFFSETOF is set if we're being called in that context. That sorta
8254 limits what of the above we'll actually accept, but nevermind.
8255 TOKEN_TYPE is the "." or "->" token, which will already have been
8256 removed from the stream. */
8258 static tree
8259 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
8260 enum cpp_ttype token_type,
8261 cp_expr postfix_expression,
8262 bool for_offsetof, cp_id_kind *idk,
8263 location_t location)
8265 tree name;
8266 bool dependent_p;
8267 bool pseudo_destructor_p;
8268 tree scope = NULL_TREE;
8269 location_t start_loc = postfix_expression.get_start ();
8271 /* If this is a `->' operator, dereference the pointer. */
8272 if (token_type == CPP_DEREF)
8273 postfix_expression = build_x_arrow (location, postfix_expression,
8274 tf_warning_or_error);
8275 /* Check to see whether or not the expression is type-dependent and
8276 not the current instantiation. */
8277 dependent_p = type_dependent_object_expression_p (postfix_expression);
8278 /* The identifier following the `->' or `.' is not qualified. */
8279 parser->scope = NULL_TREE;
8280 parser->qualifying_scope = NULL_TREE;
8281 parser->object_scope = NULL_TREE;
8282 *idk = CP_ID_KIND_NONE;
8284 /* Enter the scope corresponding to the type of the object
8285 given by the POSTFIX_EXPRESSION. */
8286 if (!dependent_p)
8288 scope = TREE_TYPE (postfix_expression);
8289 /* According to the standard, no expression should ever have
8290 reference type. Unfortunately, we do not currently match
8291 the standard in this respect in that our internal representation
8292 of an expression may have reference type even when the standard
8293 says it does not. Therefore, we have to manually obtain the
8294 underlying type here. */
8295 scope = non_reference (scope);
8296 /* The type of the POSTFIX_EXPRESSION must be complete. */
8297 /* Unlike the object expression in other contexts, *this is not
8298 required to be of complete type for purposes of class member
8299 access (5.2.5) outside the member function body. */
8300 if (postfix_expression != current_class_ref
8301 && scope != error_mark_node
8302 && !currently_open_class (scope))
8304 scope = complete_type (scope);
8305 if (!COMPLETE_TYPE_P (scope)
8306 && cp_parser_dot_deref_incomplete (&scope, &postfix_expression,
8307 &dependent_p))
8308 return error_mark_node;
8311 if (!dependent_p)
8313 /* Let the name lookup machinery know that we are processing a
8314 class member access expression. */
8315 parser->context->object_type = scope;
8316 /* If something went wrong, we want to be able to discern that case,
8317 as opposed to the case where there was no SCOPE due to the type
8318 of expression being dependent. */
8319 if (!scope)
8320 scope = error_mark_node;
8321 /* If the SCOPE was erroneous, make the various semantic analysis
8322 functions exit quickly -- and without issuing additional error
8323 messages. */
8324 if (scope == error_mark_node)
8325 postfix_expression = error_mark_node;
8329 if (dependent_p)
8331 tree type = TREE_TYPE (postfix_expression);
8332 /* If we don't have a (type-dependent) object of class type, use
8333 typeof to figure out the type of the object. */
8334 if (type == NULL_TREE || is_auto (type))
8335 type = finish_typeof (postfix_expression);
8336 parser->context->object_type = type;
8339 /* Assume this expression is not a pseudo-destructor access. */
8340 pseudo_destructor_p = false;
8342 /* If the SCOPE is a scalar type, then, if this is a valid program,
8343 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
8344 is type dependent, it can be pseudo-destructor-name or something else.
8345 Try to parse it as pseudo-destructor-name first. */
8346 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
8348 tree s;
8349 tree type;
8351 cp_parser_parse_tentatively (parser);
8352 /* Parse the pseudo-destructor-name. */
8353 s = NULL_TREE;
8354 cp_parser_pseudo_destructor_name (parser, postfix_expression,
8355 &s, &type);
8356 if (dependent_p
8357 && (cp_parser_error_occurred (parser)
8358 || !SCALAR_TYPE_P (type)))
8359 cp_parser_abort_tentative_parse (parser);
8360 else if (cp_parser_parse_definitely (parser))
8362 pseudo_destructor_p = true;
8363 postfix_expression
8364 = finish_pseudo_destructor_expr (postfix_expression,
8365 s, type, location);
8369 if (!pseudo_destructor_p)
8371 /* If the SCOPE is not a scalar type, we are looking at an
8372 ordinary class member access expression, rather than a
8373 pseudo-destructor-name. */
8374 bool template_p;
8375 cp_token *token = cp_lexer_peek_token (parser->lexer);
8376 /* Parse the id-expression. */
8377 name = (cp_parser_id_expression
8378 (parser,
8379 cp_parser_optional_template_keyword (parser),
8380 /*check_dependency_p=*/true,
8381 &template_p,
8382 /*declarator_p=*/false,
8383 /*optional_p=*/false));
8384 /* In general, build a SCOPE_REF if the member name is qualified.
8385 However, if the name was not dependent and has already been
8386 resolved; there is no need to build the SCOPE_REF. For example;
8388 struct X { void f(); };
8389 template <typename T> void f(T* t) { t->X::f(); }
8391 Even though "t" is dependent, "X::f" is not and has been resolved
8392 to a BASELINK; there is no need to include scope information. */
8394 /* But we do need to remember that there was an explicit scope for
8395 virtual function calls. */
8396 if (parser->scope)
8397 *idk = CP_ID_KIND_QUALIFIED;
8399 /* If the name is a template-id that names a type, we will get a
8400 TYPE_DECL here. That is invalid code. */
8401 if (TREE_CODE (name) == TYPE_DECL)
8403 error_at (token->location, "invalid use of %qD", name);
8404 postfix_expression = error_mark_node;
8406 else
8408 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
8410 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
8412 error_at (token->location, "%<%D::%D%> is not a class member",
8413 parser->scope, name);
8414 postfix_expression = error_mark_node;
8416 else
8417 name = build_qualified_name (/*type=*/NULL_TREE,
8418 parser->scope,
8419 name,
8420 template_p);
8421 parser->scope = NULL_TREE;
8422 parser->qualifying_scope = NULL_TREE;
8423 parser->object_scope = NULL_TREE;
8425 if (parser->scope && name && BASELINK_P (name))
8426 adjust_result_of_qualified_name_lookup
8427 (name, parser->scope, scope);
8428 postfix_expression
8429 = finish_class_member_access_expr (postfix_expression, name,
8430 template_p,
8431 tf_warning_or_error);
8432 /* Build a location e.g.:
8433 ptr->access_expr
8434 ~~~^~~~~~~~~~~~~
8435 where the caret is at the deref token, ranging from
8436 the start of postfix_expression to the end of the access expr. */
8437 location_t combined_loc
8438 = make_location (input_location, start_loc, parser->lexer);
8439 protected_set_expr_location (postfix_expression, combined_loc);
8443 /* We no longer need to look up names in the scope of the object on
8444 the left-hand side of the `.' or `->' operator. */
8445 parser->context->object_type = NULL_TREE;
8447 /* Outside of offsetof, these operators may not appear in
8448 constant-expressions. */
8449 if (!for_offsetof
8450 && (cp_parser_non_integral_constant_expression
8451 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
8452 postfix_expression = error_mark_node;
8454 return postfix_expression;
8457 /* Parse a parenthesized expression-list.
8459 expression-list:
8460 assignment-expression
8461 expression-list, assignment-expression
8463 attribute-list:
8464 expression-list
8465 identifier
8466 identifier, expression-list
8468 CAST_P is true if this expression is the target of a cast.
8470 ALLOW_EXPANSION_P is true if this expression allows expansion of an
8471 argument pack.
8473 WRAP_LOCATIONS_P is true if expressions within this list for which
8474 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
8475 their source locations.
8477 Returns a vector of trees. Each element is a representation of an
8478 assignment-expression. NULL is returned if the ( and or ) are
8479 missing. An empty, but allocated, vector is returned on no
8480 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
8481 if we are parsing an attribute list for an attribute that wants a
8482 plain identifier argument, normal_attr for an attribute that wants
8483 an expression, or non_attr if we aren't parsing an attribute list. If
8484 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
8485 not all of the expressions in the list were constant.
8486 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
8487 will be written to with the location of the closing parenthesis. If
8488 an error occurs, it may or may not be written to. */
8490 static vec<tree, va_gc> *
8491 cp_parser_parenthesized_expression_list (cp_parser* parser,
8492 int is_attribute_list,
8493 bool cast_p,
8494 bool allow_expansion_p,
8495 bool *non_constant_p,
8496 location_t *close_paren_loc,
8497 bool wrap_locations_p)
8499 vec<tree, va_gc> *expression_list;
8500 bool saved_greater_than_is_operator_p;
8502 /* Assume all the expressions will be constant. */
8503 if (non_constant_p)
8504 *non_constant_p = false;
8506 matching_parens parens;
8507 if (!parens.require_open (parser))
8508 return NULL;
8510 expression_list = make_tree_vector ();
8512 /* Within a parenthesized expression, a `>' token is always
8513 the greater-than operator. */
8514 saved_greater_than_is_operator_p
8515 = parser->greater_than_is_operator_p;
8516 parser->greater_than_is_operator_p = true;
8518 cp_expr expr (NULL_TREE);
8520 /* Consume expressions until there are no more. */
8521 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8522 while (true)
8524 /* At the beginning of attribute lists, check to see if the
8525 next token is an identifier. */
8526 if (is_attribute_list == id_attr
8527 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
8528 expr = cp_lexer_consume_token (parser->lexer)->u.value;
8529 else if (is_attribute_list == assume_attr)
8530 expr = cp_parser_conditional_expression (parser);
8531 else
8532 expr
8533 = cp_parser_parenthesized_expression_list_elt (parser, cast_p,
8534 allow_expansion_p,
8535 non_constant_p);
8537 if (wrap_locations_p)
8538 expr.maybe_add_location_wrapper ();
8540 /* Add it to the list. We add error_mark_node
8541 expressions to the list, so that we can still tell if
8542 the correct form for a parenthesized expression-list
8543 is found. That gives better errors. */
8544 vec_safe_push (expression_list, expr.get_value ());
8546 if (expr == error_mark_node)
8547 goto skip_comma;
8549 /* After the first item, attribute lists look the same as
8550 expression lists. */
8551 is_attribute_list = non_attr;
8553 get_comma:;
8554 /* If the next token isn't a `,', then we are done. */
8555 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8556 break;
8558 /* Otherwise, consume the `,' and keep going. */
8559 cp_lexer_consume_token (parser->lexer);
8562 if (close_paren_loc)
8563 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
8565 if (!parens.require_close (parser))
8567 int ending;
8569 skip_comma:;
8570 /* We try and resync to an unnested comma, as that will give the
8571 user better diagnostics. */
8572 ending = cp_parser_skip_to_closing_parenthesis (parser,
8573 /*recovering=*/true,
8574 /*or_comma=*/true,
8575 /*consume_paren=*/true);
8576 if (ending < 0)
8577 goto get_comma;
8578 if (!ending)
8580 parser->greater_than_is_operator_p
8581 = saved_greater_than_is_operator_p;
8582 return NULL;
8586 parser->greater_than_is_operator_p
8587 = saved_greater_than_is_operator_p;
8589 return expression_list;
8592 /* Parse a pseudo-destructor-name.
8594 pseudo-destructor-name:
8595 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
8596 :: [opt] nested-name-specifier template template-id :: ~ type-name
8597 :: [opt] nested-name-specifier [opt] ~ type-name
8599 If either of the first two productions is used, sets *SCOPE to the
8600 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
8601 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
8602 or ERROR_MARK_NODE if the parse fails. */
8604 static void
8605 cp_parser_pseudo_destructor_name (cp_parser* parser,
8606 tree object,
8607 tree* scope,
8608 tree* type)
8610 bool nested_name_specifier_p;
8612 /* Handle ~auto. */
8613 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
8614 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
8615 && !type_dependent_expression_p (object))
8617 if (cxx_dialect < cxx14)
8618 pedwarn (input_location, OPT_Wc__14_extensions,
8619 "%<~auto%> only available with "
8620 "%<-std=c++14%> or %<-std=gnu++14%>");
8621 cp_lexer_consume_token (parser->lexer);
8622 cp_lexer_consume_token (parser->lexer);
8623 *scope = NULL_TREE;
8624 *type = TREE_TYPE (object);
8625 return;
8628 /* Assume that things will not work out. */
8629 *type = error_mark_node;
8631 /* Look for the optional `::' operator. */
8632 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
8633 /* Look for the optional nested-name-specifier. */
8634 nested_name_specifier_p
8635 = (cp_parser_nested_name_specifier_opt (parser,
8636 /*typename_keyword_p=*/false,
8637 /*check_dependency_p=*/true,
8638 /*type_p=*/false,
8639 /*is_declaration=*/false)
8640 != NULL_TREE);
8641 /* Now, if we saw a nested-name-specifier, we might be doing the
8642 second production. */
8643 if (nested_name_specifier_p
8644 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8646 /* Consume the `template' keyword. */
8647 cp_lexer_consume_token (parser->lexer);
8648 /* Parse the template-id. */
8649 cp_parser_template_id (parser,
8650 /*template_keyword_p=*/true,
8651 /*check_dependency_p=*/false,
8652 class_type,
8653 /*is_declaration=*/true);
8654 /* Look for the `::' token. */
8655 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
8657 /* If the next token is not a `~', then there might be some
8658 additional qualification. */
8659 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
8661 /* At this point, we're looking for "type-name :: ~". The type-name
8662 must not be a class-name, since this is a pseudo-destructor. So,
8663 it must be either an enum-name, or a typedef-name -- both of which
8664 are just identifiers. So, we peek ahead to check that the "::"
8665 and "~" tokens are present; if they are not, then we can avoid
8666 calling type_name. */
8667 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
8668 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
8669 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
8671 cp_parser_error (parser, "non-scalar type");
8672 return;
8675 /* Look for the type-name. */
8676 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
8677 if (*scope == error_mark_node)
8678 return;
8680 /* Look for the `::' token. */
8681 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
8683 else
8684 *scope = NULL_TREE;
8686 /* Look for the `~'. */
8687 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
8689 /* Once we see the ~, this has to be a pseudo-destructor. */
8690 if (!processing_template_decl && !cp_parser_error_occurred (parser))
8691 cp_parser_commit_to_topmost_tentative_parse (parser);
8693 /* Look for the type-name again. We are not responsible for
8694 checking that it matches the first type-name. */
8695 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
8698 /* Parse a unary-expression.
8700 unary-expression:
8701 postfix-expression
8702 ++ cast-expression
8703 -- cast-expression
8704 await-expression
8705 unary-operator cast-expression
8706 sizeof unary-expression
8707 sizeof ( type-id )
8708 alignof ( type-id ) [C++0x]
8709 new-expression
8710 delete-expression
8712 GNU Extensions:
8714 unary-expression:
8715 __extension__ cast-expression
8716 __alignof__ unary-expression
8717 __alignof__ ( type-id )
8718 alignof unary-expression [C++0x]
8719 __real__ cast-expression
8720 __imag__ cast-expression
8721 && identifier
8722 sizeof ( type-id ) { initializer-list , [opt] }
8723 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
8724 __alignof__ ( type-id ) { initializer-list , [opt] }
8726 ADDRESS_P is true iff the unary-expression is appearing as the
8727 operand of the `&' operator. CAST_P is true if this expression is
8728 the target of a cast.
8730 Returns a representation of the expression. */
8732 static cp_expr
8733 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
8734 bool address_p, bool cast_p, bool decltype_p)
8736 cp_token *token;
8737 enum tree_code unary_operator;
8739 /* Peek at the next token. */
8740 token = cp_lexer_peek_token (parser->lexer);
8741 /* Some keywords give away the kind of expression. */
8742 if (token->type == CPP_KEYWORD)
8744 enum rid keyword = token->keyword;
8746 switch (keyword)
8748 case RID_ALIGNOF:
8749 case RID_SIZEOF:
8751 tree operand, ret;
8752 enum tree_code op;
8753 location_t start_loc = token->location;
8755 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
8756 bool std_alignof = id_equal (token->u.value, "alignof");
8758 /* Consume the token. */
8759 cp_lexer_consume_token (parser->lexer);
8760 /* Parse the operand. */
8761 operand = cp_parser_sizeof_operand (parser, keyword);
8763 /* Construct a location e.g. :
8764 alignof (expr)
8765 ^~~~~~~~~~~~~~
8766 with start == caret at the start of the "alignof"/"sizeof"
8767 token, with the endpoint at the final closing paren. */
8768 location_t compound_loc
8769 = make_location (start_loc, start_loc, parser->lexer);
8771 if (TYPE_P (operand))
8772 ret = cxx_sizeof_or_alignof_type (compound_loc, operand, op,
8773 std_alignof, true);
8774 else
8776 /* ISO C++ defines alignof only with types, not with
8777 expressions. So pedwarn if alignof is used with a non-
8778 type expression. However, __alignof__ is ok. */
8779 if (std_alignof)
8780 pedwarn (token->location, OPT_Wpedantic,
8781 "ISO C++ does not allow %<alignof%> "
8782 "with a non-type");
8784 ret = cxx_sizeof_or_alignof_expr (compound_loc, operand, op,
8785 std_alignof, true);
8787 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8788 SIZEOF_EXPR with the original operand. */
8789 if (op == SIZEOF_EXPR && ret != error_mark_node)
8791 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
8793 if (!processing_template_decl && TYPE_P (operand))
8795 ret = build_min (SIZEOF_EXPR, size_type_node,
8796 build1 (NOP_EXPR, operand,
8797 error_mark_node));
8798 SIZEOF_EXPR_TYPE_P (ret) = 1;
8800 else
8801 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
8802 TREE_SIDE_EFFECTS (ret) = 0;
8803 TREE_READONLY (ret) = 1;
8804 SET_EXPR_LOCATION (ret, compound_loc);
8808 cp_expr ret_expr (ret, compound_loc);
8809 ret_expr = ret_expr.maybe_add_location_wrapper ();
8810 return ret_expr;
8813 case RID_BUILTIN_HAS_ATTRIBUTE:
8814 return cp_parser_has_attribute_expression (parser);
8816 case RID_NEW:
8817 return cp_parser_new_expression (parser);
8819 case RID_DELETE:
8820 return cp_parser_delete_expression (parser);
8822 case RID_EXTENSION:
8824 /* The saved value of the PEDANTIC flag. */
8825 int saved_pedantic;
8826 tree expr;
8828 /* Save away the PEDANTIC flag. */
8829 cp_parser_extension_opt (parser, &saved_pedantic);
8830 /* Parse the cast-expression. */
8831 expr = cp_parser_simple_cast_expression (parser);
8832 /* Restore the PEDANTIC flag. */
8833 pedantic = saved_pedantic;
8835 return expr;
8838 case RID_REALPART:
8839 case RID_IMAGPART:
8841 tree expression;
8843 /* Consume the `__real__' or `__imag__' token. */
8844 cp_lexer_consume_token (parser->lexer);
8845 /* Parse the cast-expression. */
8846 expression = cp_parser_simple_cast_expression (parser);
8847 /* Create the complete representation. */
8848 return build_x_unary_op (token->location,
8849 (keyword == RID_REALPART
8850 ? REALPART_EXPR : IMAGPART_EXPR),
8851 expression, NULL_TREE,
8852 tf_warning_or_error);
8854 break;
8856 case RID_TRANSACTION_ATOMIC:
8857 case RID_TRANSACTION_RELAXED:
8858 return cp_parser_transaction_expression (parser, keyword);
8860 case RID_NOEXCEPT:
8862 tree expr;
8863 const char *saved_message;
8864 bool saved_integral_constant_expression_p;
8865 bool saved_non_integral_constant_expression_p;
8866 bool saved_greater_than_is_operator_p;
8868 location_t start_loc = token->location;
8870 cp_lexer_consume_token (parser->lexer);
8871 matching_parens parens;
8872 parens.require_open (parser);
8874 saved_message = parser->type_definition_forbidden_message;
8875 parser->type_definition_forbidden_message
8876 = G_("types may not be defined in %<noexcept%> expressions");
8878 saved_integral_constant_expression_p
8879 = parser->integral_constant_expression_p;
8880 saved_non_integral_constant_expression_p
8881 = parser->non_integral_constant_expression_p;
8882 parser->integral_constant_expression_p = false;
8884 saved_greater_than_is_operator_p
8885 = parser->greater_than_is_operator_p;
8886 parser->greater_than_is_operator_p = true;
8888 ++cp_unevaluated_operand;
8889 ++c_inhibit_evaluation_warnings;
8890 ++cp_noexcept_operand;
8891 expr = cp_parser_expression (parser);
8892 --cp_noexcept_operand;
8893 --c_inhibit_evaluation_warnings;
8894 --cp_unevaluated_operand;
8896 parser->greater_than_is_operator_p
8897 = saved_greater_than_is_operator_p;
8899 parser->integral_constant_expression_p
8900 = saved_integral_constant_expression_p;
8901 parser->non_integral_constant_expression_p
8902 = saved_non_integral_constant_expression_p;
8904 parser->type_definition_forbidden_message = saved_message;
8906 parens.require_close (parser);
8908 /* Construct a location of the form:
8909 noexcept (expr)
8910 ^~~~~~~~~~~~~~~
8911 with start == caret, finishing at the close-paren. */
8912 location_t noexcept_loc
8913 = make_location (start_loc, start_loc, parser->lexer);
8915 return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
8916 noexcept_loc);
8919 case RID_CO_AWAIT:
8921 tree expr;
8922 location_t kw_loc = token->location;
8924 /* Consume the `co_await' token. */
8925 cp_lexer_consume_token (parser->lexer);
8926 /* Parse its cast-expression. */
8927 expr = cp_parser_simple_cast_expression (parser);
8928 if (expr == error_mark_node)
8929 return error_mark_node;
8931 /* Handle [expr.await]. */
8932 return cp_expr (finish_co_await_expr (kw_loc, expr));
8935 default:
8936 break;
8940 /* Look for the `:: new' and `:: delete', which also signal the
8941 beginning of a new-expression, or delete-expression,
8942 respectively. If the next token is `::', then it might be one of
8943 these. */
8944 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8946 enum rid keyword;
8948 /* See if the token after the `::' is one of the keywords in
8949 which we're interested. */
8950 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8951 /* If it's `new', we have a new-expression. */
8952 if (keyword == RID_NEW)
8953 return cp_parser_new_expression (parser);
8954 /* Similarly, for `delete'. */
8955 else if (keyword == RID_DELETE)
8956 return cp_parser_delete_expression (parser);
8959 /* Look for a unary operator. */
8960 unary_operator = cp_parser_unary_operator (token);
8961 /* The `++' and `--' operators can be handled similarly, even though
8962 they are not technically unary-operators in the grammar. */
8963 if (unary_operator == ERROR_MARK)
8965 if (token->type == CPP_PLUS_PLUS)
8966 unary_operator = PREINCREMENT_EXPR;
8967 else if (token->type == CPP_MINUS_MINUS)
8968 unary_operator = PREDECREMENT_EXPR;
8969 /* Handle the GNU address-of-label extension. */
8970 else if (cp_parser_allow_gnu_extensions_p (parser)
8971 && token->type == CPP_AND_AND)
8973 tree identifier;
8974 tree expression;
8975 location_t start_loc = token->location;
8977 /* Consume the '&&' token. */
8978 cp_lexer_consume_token (parser->lexer);
8979 /* Look for the identifier. */
8980 identifier = cp_parser_identifier (parser);
8981 /* Construct a location of the form:
8982 &&label
8983 ^~~~~~~
8984 with caret==start at the "&&", finish at the end of the label. */
8985 location_t combined_loc
8986 = make_location (start_loc, start_loc, parser->lexer);
8987 /* Create an expression representing the address. */
8988 expression = finish_label_address_expr (identifier, combined_loc);
8989 if (cp_parser_non_integral_constant_expression (parser,
8990 NIC_ADDR_LABEL))
8991 expression = error_mark_node;
8992 return expression;
8995 if (unary_operator != ERROR_MARK)
8997 cp_expr cast_expression;
8998 cp_expr expression = error_mark_node;
8999 non_integral_constant non_constant_p = NIC_NONE;
9000 location_t loc = token->location;
9001 tsubst_flags_t complain = complain_flags (decltype_p);
9003 /* Consume the operator token. */
9004 token = cp_lexer_consume_token (parser->lexer);
9005 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
9007 /* Parse the cast-expression. */
9008 cast_expression
9009 = cp_parser_cast_expression (parser,
9010 unary_operator == ADDR_EXPR,
9011 /*cast_p=*/false,
9012 /*decltype*/false,
9013 pidk);
9015 /* Make a location:
9016 OP_TOKEN CAST_EXPRESSION
9017 ^~~~~~~~~~~~~~~~~~~~~~~~~
9018 with start==caret at the operator token, and
9019 extending to the end of the cast_expression. */
9020 loc = make_location (loc, loc, cast_expression.get_finish ());
9022 /* Now, build an appropriate representation. */
9023 switch (unary_operator)
9025 case INDIRECT_REF:
9026 non_constant_p = NIC_STAR;
9027 expression = build_x_indirect_ref (loc, cast_expression,
9028 RO_UNARY_STAR, NULL_TREE,
9029 complain);
9030 /* TODO: build_x_indirect_ref does not always honor the
9031 location, so ensure it is set. */
9032 expression.set_location (loc);
9033 break;
9035 case ADDR_EXPR:
9036 non_constant_p = NIC_ADDR;
9037 /* Fall through. */
9038 case BIT_NOT_EXPR:
9039 expression = build_x_unary_op (loc, unary_operator,
9040 cast_expression,
9041 NULL_TREE, complain);
9042 /* TODO: build_x_unary_op does not always honor the location,
9043 so ensure it is set. */
9044 expression.set_location (loc);
9045 break;
9047 case PREINCREMENT_EXPR:
9048 case PREDECREMENT_EXPR:
9049 non_constant_p = unary_operator == PREINCREMENT_EXPR
9050 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
9051 /* Fall through. */
9052 case NEGATE_EXPR:
9053 /* Immediately fold negation of a constant, unless the constant is 0
9054 (since -0 == 0) or it would overflow. */
9055 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER)
9057 tree stripped_expr
9058 = tree_strip_any_location_wrapper (cast_expression);
9059 if (CONSTANT_CLASS_P (stripped_expr)
9060 && !integer_zerop (stripped_expr)
9061 && !TREE_OVERFLOW (stripped_expr))
9063 tree folded = fold_build1 (unary_operator,
9064 TREE_TYPE (stripped_expr),
9065 stripped_expr);
9066 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
9068 expression = maybe_wrap_with_location (folded, loc);
9069 break;
9073 /* Fall through. */
9074 case UNARY_PLUS_EXPR:
9075 case TRUTH_NOT_EXPR:
9076 expression = finish_unary_op_expr (loc, unary_operator,
9077 cast_expression, complain);
9078 break;
9080 default:
9081 gcc_unreachable ();
9084 if (non_constant_p != NIC_NONE
9085 && cp_parser_non_integral_constant_expression (parser,
9086 non_constant_p))
9087 expression = error_mark_node;
9089 return expression;
9092 return cp_parser_postfix_expression (parser, address_p, cast_p,
9093 /*member_access_only_p=*/false,
9094 decltype_p,
9095 pidk);
9098 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
9099 unary-operator, the corresponding tree code is returned. */
9101 static enum tree_code
9102 cp_parser_unary_operator (cp_token* token)
9104 switch (token->type)
9106 case CPP_MULT:
9107 return INDIRECT_REF;
9109 case CPP_AND:
9110 return ADDR_EXPR;
9112 case CPP_PLUS:
9113 return UNARY_PLUS_EXPR;
9115 case CPP_MINUS:
9116 return NEGATE_EXPR;
9118 case CPP_NOT:
9119 return TRUTH_NOT_EXPR;
9121 case CPP_COMPL:
9122 return BIT_NOT_EXPR;
9124 default:
9125 return ERROR_MARK;
9129 /* Parse a __builtin_has_attribute([expr|type], attribute-spec) expression.
9130 Returns a representation of the expression. */
9132 static tree
9133 cp_parser_has_attribute_expression (cp_parser *parser)
9135 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9137 /* Consume the __builtin_has_attribute token. */
9138 cp_lexer_consume_token (parser->lexer);
9140 matching_parens parens;
9141 if (!parens.require_open (parser))
9142 return error_mark_node;
9144 /* Types cannot be defined in a `sizeof' expression. Save away the
9145 old message. */
9146 const char *saved_message = parser->type_definition_forbidden_message;
9147 const char *saved_message_arg
9148 = parser->type_definition_forbidden_message_arg;
9149 parser->type_definition_forbidden_message
9150 = G_("types may not be defined in %qs expressions");
9151 parser->type_definition_forbidden_message_arg
9152 = IDENTIFIER_POINTER (ridpointers[RID_BUILTIN_HAS_ATTRIBUTE]);
9154 /* The restrictions on constant-expressions do not apply inside
9155 sizeof expressions. */
9156 bool saved_integral_constant_expression_p
9157 = parser->integral_constant_expression_p;
9158 bool saved_non_integral_constant_expression_p
9159 = parser->non_integral_constant_expression_p;
9160 parser->integral_constant_expression_p = false;
9162 /* Do not actually evaluate the expression. */
9163 ++cp_unevaluated_operand;
9164 ++c_inhibit_evaluation_warnings;
9166 tree oper = NULL_TREE;
9168 /* We can't be sure yet whether we're looking at a type-id or an
9169 expression. */
9170 cp_parser_parse_tentatively (parser);
9172 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9173 parser->in_type_id_in_expr_p = true;
9174 /* Look for the type-id. */
9175 oper = cp_parser_type_id (parser);
9176 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9178 cp_parser_parse_definitely (parser);
9180 /* If the type-id production did not work out, then we must be
9181 looking at an expression. */
9182 if (!oper || oper == error_mark_node)
9183 oper = cp_parser_assignment_expression (parser);
9185 STRIP_ANY_LOCATION_WRAPPER (oper);
9187 /* Go back to evaluating expressions. */
9188 --cp_unevaluated_operand;
9189 --c_inhibit_evaluation_warnings;
9191 /* And restore the old one. */
9192 parser->type_definition_forbidden_message = saved_message;
9193 parser->type_definition_forbidden_message_arg = saved_message_arg;
9194 parser->integral_constant_expression_p
9195 = saved_integral_constant_expression_p;
9196 parser->non_integral_constant_expression_p
9197 = saved_non_integral_constant_expression_p;
9199 /* Consume the comma if it's there. */
9200 if (!cp_parser_require (parser, CPP_COMMA, RT_COMMA))
9202 cp_parser_skip_to_closing_parenthesis (parser, false, false,
9203 /*consume_paren=*/true);
9204 return error_mark_node;
9207 /* Parse the attribute specification. */
9208 bool ret = false;
9209 location_t atloc = cp_lexer_peek_token (parser->lexer)->location;
9210 if (tree attr = cp_parser_gnu_attribute_list (parser, /*exactly_one=*/true))
9212 if (oper == error_mark_node)
9213 /* Nothing. */;
9214 else if (processing_template_decl && uses_template_parms (oper))
9215 sorry_at (atloc, "%<__builtin_has_attribute%> with dependent argument "
9216 "not supported yet");
9217 else
9219 /* Fold constant expressions used in attributes first. */
9220 cp_check_const_attributes (attr);
9222 /* Finally, see if OPER has been declared with ATTR. */
9223 ret = has_attribute (atloc, oper, attr, default_conversion);
9226 parens.require_close (parser);
9228 else
9230 error_at (atloc, "expected identifier");
9231 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9234 /* Construct a location e.g. :
9235 __builtin_has_attribute (oper, attr)
9236 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9237 with start == caret at the start of the built-in token,
9238 and with the endpoint at the final closing paren. */
9239 location_t compound_loc
9240 = make_location (start_loc, start_loc, parser->lexer);
9242 cp_expr ret_expr (ret ? boolean_true_node : boolean_false_node);
9243 ret_expr.set_location (compound_loc);
9244 ret_expr = ret_expr.maybe_add_location_wrapper ();
9245 return ret_expr;
9248 /* Parse a new-expression.
9250 new-expression:
9251 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
9252 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
9254 Returns a representation of the expression. */
9256 static tree
9257 cp_parser_new_expression (cp_parser* parser)
9259 bool global_scope_p;
9260 vec<tree, va_gc> *placement;
9261 tree type;
9262 vec<tree, va_gc> *initializer;
9263 tree nelts = NULL_TREE;
9264 tree ret;
9266 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9268 /* Look for the optional `::' operator. */
9269 global_scope_p
9270 = (cp_parser_global_scope_opt (parser,
9271 /*current_scope_valid_p=*/false)
9272 != NULL_TREE);
9273 /* Look for the `new' operator. */
9274 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
9275 /* There's no easy way to tell a new-placement from the
9276 `( type-id )' construct. */
9277 cp_parser_parse_tentatively (parser);
9278 /* Look for a new-placement. */
9279 placement = cp_parser_new_placement (parser);
9280 /* If that didn't work out, there's no new-placement. */
9281 if (!cp_parser_parse_definitely (parser))
9283 if (placement != NULL)
9284 release_tree_vector (placement);
9285 placement = NULL;
9288 /* If the next token is a `(', then we have a parenthesized
9289 type-id. */
9290 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9292 cp_token *token;
9293 const char *saved_message = parser->type_definition_forbidden_message;
9295 /* Consume the `('. */
9296 matching_parens parens;
9297 parens.consume_open (parser);
9299 /* Parse the type-id. */
9300 parser->type_definition_forbidden_message
9301 = G_("types may not be defined in a new-expression");
9303 type_id_in_expr_sentinel s (parser);
9304 type = cp_parser_type_id (parser);
9306 parser->type_definition_forbidden_message = saved_message;
9308 /* Look for the closing `)'. */
9309 parens.require_close (parser);
9310 token = cp_lexer_peek_token (parser->lexer);
9311 /* There should not be a direct-new-declarator in this production,
9312 but GCC used to allowed this, so we check and emit a sensible error
9313 message for this case. */
9314 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
9316 error_at (token->location,
9317 "array bound forbidden after parenthesized type-id");
9318 inform (token->location,
9319 "try removing the parentheses around the type-id");
9320 cp_parser_direct_new_declarator (parser);
9323 /* Otherwise, there must be a new-type-id. */
9324 else
9325 type = cp_parser_new_type_id (parser, &nelts);
9327 /* If the next token is a `(' or '{', then we have a new-initializer. */
9328 cp_token *token = cp_lexer_peek_token (parser->lexer);
9329 if (token->type == CPP_OPEN_PAREN
9330 || token->type == CPP_OPEN_BRACE)
9331 initializer = cp_parser_new_initializer (parser);
9332 else
9333 initializer = NULL;
9335 /* A new-expression may not appear in an integral constant
9336 expression. */
9337 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
9338 ret = error_mark_node;
9339 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
9340 of a new-type-id or type-id of a new-expression, the new-expression shall
9341 contain a new-initializer of the form ( assignment-expression )".
9342 Additionally, consistently with the spirit of DR 1467, we want to accept
9343 'new auto { 2 }' too. */
9344 else if ((ret = type_uses_auto (type))
9345 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
9346 && (vec_safe_length (initializer) != 1
9347 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
9348 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
9350 error_at (token->location,
9351 "initialization of new-expression for type %<auto%> "
9352 "requires exactly one element");
9353 ret = error_mark_node;
9355 else
9357 /* Construct a location e.g.:
9358 ptr = new int[100]
9359 ^~~~~~~~~~~~
9360 with caret == start at the start of the "new" token, and the end
9361 at the end of the final token we consumed. */
9362 location_t combined_loc = make_location (start_loc, start_loc,
9363 parser->lexer);
9364 /* Create a representation of the new-expression. */
9365 ret = build_new (combined_loc, &placement, type, nelts, &initializer,
9366 global_scope_p, tf_warning_or_error);
9369 if (placement != NULL)
9370 release_tree_vector (placement);
9371 if (initializer != NULL)
9372 release_tree_vector (initializer);
9374 return ret;
9377 /* Parse a new-placement.
9379 new-placement:
9380 ( expression-list )
9382 Returns the same representation as for an expression-list. */
9384 static vec<tree, va_gc> *
9385 cp_parser_new_placement (cp_parser* parser)
9387 vec<tree, va_gc> *expression_list;
9389 /* Parse the expression-list. */
9390 expression_list = (cp_parser_parenthesized_expression_list
9391 (parser, non_attr, /*cast_p=*/false,
9392 /*allow_expansion_p=*/true,
9393 /*non_constant_p=*/NULL));
9395 if (expression_list && expression_list->is_empty ())
9396 error ("expected expression-list or type-id");
9398 return expression_list;
9401 /* Parse a new-type-id.
9403 new-type-id:
9404 type-specifier-seq new-declarator [opt]
9406 Returns the TYPE allocated. If the new-type-id indicates an array
9407 type, *NELTS is set to the number of elements in the last array
9408 bound; the TYPE will not include the last array bound. */
9410 static tree
9411 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
9413 cp_decl_specifier_seq type_specifier_seq;
9414 cp_declarator *new_declarator;
9415 cp_declarator *declarator;
9416 cp_declarator *outer_declarator;
9417 const char *saved_message;
9419 /* The type-specifier sequence must not contain type definitions.
9420 (It cannot contain declarations of new types either, but if they
9421 are not definitions we will catch that because they are not
9422 complete.) */
9423 saved_message = parser->type_definition_forbidden_message;
9424 parser->type_definition_forbidden_message
9425 = G_("types may not be defined in a new-type-id");
9426 /* Parse the type-specifier-seq. */
9427 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
9428 /*is_declaration=*/false,
9429 /*is_trailing_return=*/false,
9430 &type_specifier_seq);
9431 /* Restore the old message. */
9432 parser->type_definition_forbidden_message = saved_message;
9434 if (type_specifier_seq.type == error_mark_node)
9435 return error_mark_node;
9437 /* Parse the new-declarator. */
9438 new_declarator = cp_parser_new_declarator_opt (parser);
9440 /* Determine the number of elements in the last array dimension, if
9441 any. */
9442 *nelts = NULL_TREE;
9443 /* Skip down to the last array dimension. */
9444 declarator = new_declarator;
9445 outer_declarator = NULL;
9446 while (declarator && (declarator->kind == cdk_pointer
9447 || declarator->kind == cdk_ptrmem))
9449 outer_declarator = declarator;
9450 declarator = declarator->declarator;
9452 while (declarator
9453 && declarator->kind == cdk_array
9454 && declarator->declarator
9455 && declarator->declarator->kind == cdk_array)
9457 outer_declarator = declarator;
9458 declarator = declarator->declarator;
9461 if (declarator && declarator->kind == cdk_array)
9463 *nelts = declarator->u.array.bounds;
9464 if (*nelts == error_mark_node)
9465 *nelts = integer_one_node;
9467 if (*nelts == NULL_TREE)
9468 /* Leave [] in the declarator. */;
9469 else if (outer_declarator)
9470 outer_declarator->declarator = declarator->declarator;
9471 else
9472 new_declarator = NULL;
9475 return groktypename (&type_specifier_seq, new_declarator, false);
9478 /* Parse an (optional) new-declarator.
9480 new-declarator:
9481 ptr-operator new-declarator [opt]
9482 direct-new-declarator
9484 Returns the declarator. */
9486 static cp_declarator *
9487 cp_parser_new_declarator_opt (cp_parser* parser)
9489 enum tree_code code;
9490 tree type, std_attributes = NULL_TREE;
9491 cp_cv_quals cv_quals;
9493 /* We don't know if there's a ptr-operator next, or not. */
9494 cp_parser_parse_tentatively (parser);
9495 /* Look for a ptr-operator. */
9496 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
9497 /* If that worked, look for more new-declarators. */
9498 if (cp_parser_parse_definitely (parser))
9500 cp_declarator *declarator;
9502 /* Parse another optional declarator. */
9503 declarator = cp_parser_new_declarator_opt (parser);
9505 declarator = cp_parser_make_indirect_declarator
9506 (code, type, cv_quals, declarator, std_attributes);
9508 return declarator;
9511 /* If the next token is a `[', there is a direct-new-declarator. */
9512 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
9513 return cp_parser_direct_new_declarator (parser);
9515 return NULL;
9518 /* Parse a direct-new-declarator.
9520 direct-new-declarator:
9521 [ expression ]
9522 direct-new-declarator [constant-expression]
9526 static cp_declarator *
9527 cp_parser_direct_new_declarator (cp_parser* parser)
9529 cp_declarator *declarator = NULL;
9530 bool first_p = true;
9532 while (true)
9534 tree expression;
9535 cp_token *token;
9537 /* Look for the opening `['. */
9538 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9540 token = cp_lexer_peek_token (parser->lexer);
9541 if (token->type == CPP_CLOSE_SQUARE && first_p)
9542 expression = NULL_TREE;
9543 else
9544 expression = cp_parser_expression (parser);
9545 /* The standard requires that the expression have integral
9546 type. DR 74 adds enumeration types. We believe that the
9547 real intent is that these expressions be handled like the
9548 expression in a `switch' condition, which also allows
9549 classes with a single conversion to integral or
9550 enumeration type. */
9551 if (expression && !processing_template_decl)
9553 expression
9554 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
9555 expression,
9556 /*complain=*/true);
9557 if (!expression)
9559 error_at (token->location,
9560 "expression in new-declarator must have integral "
9561 "or enumeration type");
9562 expression = error_mark_node;
9566 /* Look for the closing `]'. */
9567 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9569 /* Add this bound to the declarator. */
9570 declarator = make_array_declarator (declarator, expression);
9572 /* If the next token is not a `[', then there are no more
9573 bounds. */
9574 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
9575 break;
9576 first_p = false;
9579 return declarator;
9582 /* Parse a new-initializer.
9584 new-initializer:
9585 ( expression-list [opt] )
9586 braced-init-list
9588 Returns a representation of the expression-list. */
9590 static vec<tree, va_gc> *
9591 cp_parser_new_initializer (cp_parser* parser)
9593 vec<tree, va_gc> *expression_list;
9595 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9597 tree t;
9598 bool expr_non_constant_p;
9599 cp_lexer_set_source_position (parser->lexer);
9600 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9601 t = cp_parser_braced_list (parser, &expr_non_constant_p);
9602 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
9603 expression_list = make_tree_vector_single (t);
9605 else
9606 expression_list = (cp_parser_parenthesized_expression_list
9607 (parser, non_attr, /*cast_p=*/false,
9608 /*allow_expansion_p=*/true,
9609 /*non_constant_p=*/NULL));
9611 return expression_list;
9614 /* Parse a delete-expression.
9616 delete-expression:
9617 :: [opt] delete cast-expression
9618 :: [opt] delete [ ] cast-expression
9620 Returns a representation of the expression. */
9622 static tree
9623 cp_parser_delete_expression (cp_parser* parser)
9625 bool global_scope_p;
9626 bool array_p;
9627 tree expression;
9628 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9630 /* Look for the optional `::' operator. */
9631 global_scope_p
9632 = (cp_parser_global_scope_opt (parser,
9633 /*current_scope_valid_p=*/false)
9634 != NULL_TREE);
9635 /* Look for the `delete' keyword. */
9636 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
9637 /* See if the array syntax is in use. */
9638 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
9640 /* Consume the `[' token. */
9641 cp_lexer_consume_token (parser->lexer);
9642 /* Look for the `]' token. */
9643 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9644 /* Remember that this is the `[]' construct. */
9645 array_p = true;
9647 else
9648 array_p = false;
9650 /* Parse the cast-expression. */
9651 expression = cp_parser_simple_cast_expression (parser);
9653 /* A delete-expression may not appear in an integral constant
9654 expression. */
9655 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
9656 return error_mark_node;
9658 /* Construct a location e.g.:
9659 delete [ ] ptr
9660 ^~~~~~~~~~~~~~
9661 with caret == start at the start of the "delete" token, and
9662 the end at the end of the final token we consumed. */
9663 location_t combined_loc = make_location (start_loc, start_loc,
9664 parser->lexer);
9665 expression = delete_sanity (combined_loc, expression, NULL_TREE, array_p,
9666 global_scope_p, tf_warning_or_error);
9668 return expression;
9671 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
9672 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
9673 0 otherwise. */
9675 static int
9676 cp_parser_tokens_start_cast_expression (cp_parser *parser)
9678 cp_token *token = cp_lexer_peek_token (parser->lexer);
9679 switch (token->type)
9681 case CPP_COMMA:
9682 case CPP_SEMICOLON:
9683 case CPP_QUERY:
9684 case CPP_COLON:
9685 case CPP_CLOSE_SQUARE:
9686 case CPP_CLOSE_PAREN:
9687 case CPP_CLOSE_BRACE:
9688 case CPP_OPEN_BRACE:
9689 case CPP_DOT:
9690 case CPP_DOT_STAR:
9691 case CPP_DEREF:
9692 case CPP_DEREF_STAR:
9693 case CPP_DIV:
9694 case CPP_MOD:
9695 case CPP_LSHIFT:
9696 case CPP_RSHIFT:
9697 case CPP_LESS:
9698 case CPP_GREATER:
9699 case CPP_LESS_EQ:
9700 case CPP_GREATER_EQ:
9701 case CPP_EQ_EQ:
9702 case CPP_NOT_EQ:
9703 case CPP_EQ:
9704 case CPP_MULT_EQ:
9705 case CPP_DIV_EQ:
9706 case CPP_MOD_EQ:
9707 case CPP_PLUS_EQ:
9708 case CPP_MINUS_EQ:
9709 case CPP_RSHIFT_EQ:
9710 case CPP_LSHIFT_EQ:
9711 case CPP_AND_EQ:
9712 case CPP_XOR_EQ:
9713 case CPP_OR_EQ:
9714 case CPP_XOR:
9715 case CPP_OR:
9716 case CPP_OR_OR:
9717 case CPP_EOF:
9718 case CPP_ELLIPSIS:
9719 return 0;
9721 case CPP_OPEN_PAREN:
9722 /* In ((type ()) () the last () isn't a valid cast-expression,
9723 so the whole must be parsed as postfix-expression. */
9724 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
9725 != CPP_CLOSE_PAREN;
9727 case CPP_OPEN_SQUARE:
9728 /* '[' may start a primary-expression in obj-c++ and in C++11,
9729 as a lambda-expression, eg, '(void)[]{}'. */
9730 if (cxx_dialect >= cxx11)
9731 return -1;
9732 return c_dialect_objc ();
9734 case CPP_PLUS_PLUS:
9735 case CPP_MINUS_MINUS:
9736 /* '++' and '--' may or may not start a cast-expression:
9738 struct T { void operator++(int); };
9739 void f() { (T())++; }
9743 int a;
9744 (int)++a; */
9745 return -1;
9747 default:
9748 return 1;
9752 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
9753 in the order: const_cast, static_cast, reinterpret_cast.
9755 Don't suggest dynamic_cast.
9757 Return the first legal cast kind found, or NULL otherwise. */
9759 static const char *
9760 get_cast_suggestion (tree dst_type, tree orig_expr)
9762 tree trial;
9764 /* Reuse the parser logic by attempting to build the various kinds of
9765 cast, with "complain" disabled.
9766 Identify the first such cast that is valid. */
9768 /* Don't attempt to run such logic within template processing. */
9769 if (processing_template_decl)
9770 return NULL;
9772 /* First try const_cast. */
9773 trial = build_const_cast (input_location, dst_type, orig_expr, tf_none);
9774 if (trial != error_mark_node)
9775 return "const_cast";
9777 /* If that fails, try static_cast. */
9778 trial = build_static_cast (input_location, dst_type, orig_expr, tf_none);
9779 if (trial != error_mark_node)
9780 return "static_cast";
9782 /* Finally, try reinterpret_cast. */
9783 trial = build_reinterpret_cast (input_location, dst_type, orig_expr,
9784 tf_none);
9785 if (trial != error_mark_node)
9786 return "reinterpret_cast";
9788 /* No such cast possible. */
9789 return NULL;
9792 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
9793 suggesting how to convert a C-style cast of the form:
9795 (DST_TYPE)ORIG_EXPR
9797 to a C++-style cast.
9799 The primary range of RICHLOC is asssumed to be that of the original
9800 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
9801 of the parens in the C-style cast. */
9803 static void
9804 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
9805 location_t close_paren_loc, tree orig_expr,
9806 tree dst_type)
9808 /* This function is non-trivial, so bail out now if the warning isn't
9809 going to be emitted. */
9810 if (!warn_old_style_cast)
9811 return;
9813 /* Try to find a legal C++ cast, trying them in order:
9814 const_cast, static_cast, reinterpret_cast. */
9815 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
9816 if (!cast_suggestion)
9817 return;
9819 /* Replace the open paren with "CAST_SUGGESTION<". */
9820 pretty_printer pp;
9821 pp_string (&pp, cast_suggestion);
9822 pp_less (&pp);
9823 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
9825 /* Replace the close paren with "> (". */
9826 rich_loc->add_fixit_replace (close_paren_loc, "> (");
9828 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
9829 rich_loc->add_fixit_insert_after (")");
9833 /* Parse a cast-expression.
9835 cast-expression:
9836 unary-expression
9837 ( type-id ) cast-expression
9839 ADDRESS_P is true iff the unary-expression is appearing as the
9840 operand of the `&' operator. CAST_P is true if this expression is
9841 the target of a cast.
9843 Returns a representation of the expression. */
9845 static cp_expr
9846 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
9847 bool decltype_p, cp_id_kind * pidk)
9849 /* If it's a `(', then we might be looking at a cast. */
9850 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9852 tree type = NULL_TREE;
9853 cp_expr expr (NULL_TREE);
9854 int cast_expression = 0;
9855 const char *saved_message;
9857 /* There's no way to know yet whether or not this is a cast.
9858 For example, `(int (3))' is a unary-expression, while `(int)
9859 3' is a cast. So, we resort to parsing tentatively. */
9860 cp_parser_parse_tentatively (parser);
9861 /* Types may not be defined in a cast. */
9862 saved_message = parser->type_definition_forbidden_message;
9863 parser->type_definition_forbidden_message
9864 = G_("types may not be defined in casts");
9865 /* Consume the `('. */
9866 matching_parens parens;
9867 cp_token *open_paren = parens.consume_open (parser);
9868 location_t open_paren_loc = open_paren->location;
9869 location_t close_paren_loc = UNKNOWN_LOCATION;
9871 /* A very tricky bit is that `(struct S) { 3 }' is a
9872 compound-literal (which we permit in C++ as an extension).
9873 But, that construct is not a cast-expression -- it is a
9874 postfix-expression. (The reason is that `(struct S) { 3 }.i'
9875 is legal; if the compound-literal were a cast-expression,
9876 you'd need an extra set of parentheses.) But, if we parse
9877 the type-id, and it happens to be a class-specifier, then we
9878 will commit to the parse at that point, because we cannot
9879 undo the action that is done when creating a new class. So,
9880 then we cannot back up and do a postfix-expression.
9882 Another tricky case is the following (c++/29234):
9884 struct S { void operator () (); };
9886 void foo ()
9888 ( S()() );
9891 As a type-id we parse the parenthesized S()() as a function
9892 returning a function, groktypename complains and we cannot
9893 back up in this case either.
9895 Therefore, we scan ahead to the closing `)', and check to see
9896 if the tokens after the `)' can start a cast-expression. Otherwise
9897 we are dealing with an unary-expression, a postfix-expression
9898 or something else.
9900 Yet another tricky case, in C++11, is the following (c++/54891):
9902 (void)[]{};
9904 The issue is that usually, besides the case of lambda-expressions,
9905 the parenthesized type-id cannot be followed by '[', and, eg, we
9906 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9907 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9908 we don't commit, we try a cast-expression, then an unary-expression.
9910 Save tokens so that we can put them back. */
9911 cp_lexer_save_tokens (parser->lexer);
9913 /* We may be looking at a cast-expression. */
9914 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
9915 /*consume_paren=*/true))
9916 cast_expression
9917 = cp_parser_tokens_start_cast_expression (parser);
9919 /* Roll back the tokens we skipped. */
9920 cp_lexer_rollback_tokens (parser->lexer);
9921 /* If we aren't looking at a cast-expression, simulate an error so
9922 that the call to cp_parser_error_occurred below returns true. */
9923 if (!cast_expression)
9924 cp_parser_simulate_error (parser);
9925 else
9927 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9928 parser->in_type_id_in_expr_p = true;
9929 /* Look for the type-id. */
9930 type = cp_parser_type_id (parser);
9931 /* Look for the closing `)'. */
9932 cp_token *close_paren = parens.require_close (parser);
9933 if (close_paren)
9934 close_paren_loc = close_paren->location;
9935 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9938 /* Restore the saved message. */
9939 parser->type_definition_forbidden_message = saved_message;
9941 /* At this point this can only be either a cast or a
9942 parenthesized ctor such as `(T ())' that looks like a cast to
9943 function returning T. */
9944 if (!cp_parser_error_occurred (parser))
9946 /* Only commit if the cast-expression doesn't start with
9947 '++', '--', or '[' in C++11. */
9948 if (cast_expression > 0)
9949 cp_parser_commit_to_topmost_tentative_parse (parser);
9951 expr = cp_parser_cast_expression (parser,
9952 /*address_p=*/false,
9953 /*cast_p=*/true,
9954 /*decltype_p=*/false,
9955 pidk);
9957 if (cp_parser_parse_definitely (parser))
9959 /* Warn about old-style casts, if so requested. */
9960 if (warn_old_style_cast
9961 && !in_system_header_at (input_location)
9962 && !VOID_TYPE_P (type)
9963 && current_lang_name != lang_name_c)
9965 gcc_rich_location rich_loc (input_location);
9966 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
9967 expr, type);
9968 warning_at (&rich_loc, OPT_Wold_style_cast,
9969 "use of old-style cast to %q#T", type);
9972 /* Only type conversions to integral or enumeration types
9973 can be used in constant-expressions. */
9974 if (!cast_valid_in_integral_constant_expression_p (type)
9975 && cp_parser_non_integral_constant_expression (parser,
9976 NIC_CAST))
9977 return error_mark_node;
9979 /* Perform the cast. */
9980 /* Make a location:
9981 (TYPE) EXPR
9982 ^~~~~~~~~~~
9983 with start==caret at the open paren, extending to the
9984 end of "expr". */
9985 location_t cast_loc = make_location (open_paren_loc,
9986 open_paren_loc,
9987 expr.get_finish ());
9988 expr = build_c_cast (cast_loc, type, expr);
9989 return expr;
9992 else
9993 cp_parser_abort_tentative_parse (parser);
9996 /* If we get here, then it's not a cast, so it must be a
9997 unary-expression. */
9998 return cp_parser_unary_expression (parser, pidk, address_p,
9999 cast_p, decltype_p);
10002 /* Parse a binary expression of the general form:
10004 pm-expression:
10005 cast-expression
10006 pm-expression .* cast-expression
10007 pm-expression ->* cast-expression
10009 multiplicative-expression:
10010 pm-expression
10011 multiplicative-expression * pm-expression
10012 multiplicative-expression / pm-expression
10013 multiplicative-expression % pm-expression
10015 additive-expression:
10016 multiplicative-expression
10017 additive-expression + multiplicative-expression
10018 additive-expression - multiplicative-expression
10020 shift-expression:
10021 additive-expression
10022 shift-expression << additive-expression
10023 shift-expression >> additive-expression
10025 relational-expression:
10026 shift-expression
10027 relational-expression < shift-expression
10028 relational-expression > shift-expression
10029 relational-expression <= shift-expression
10030 relational-expression >= shift-expression
10032 GNU Extension:
10034 relational-expression:
10035 relational-expression <? shift-expression
10036 relational-expression >? shift-expression
10038 equality-expression:
10039 relational-expression
10040 equality-expression == relational-expression
10041 equality-expression != relational-expression
10043 and-expression:
10044 equality-expression
10045 and-expression & equality-expression
10047 exclusive-or-expression:
10048 and-expression
10049 exclusive-or-expression ^ and-expression
10051 inclusive-or-expression:
10052 exclusive-or-expression
10053 inclusive-or-expression | exclusive-or-expression
10055 logical-and-expression:
10056 inclusive-or-expression
10057 logical-and-expression && inclusive-or-expression
10059 logical-or-expression:
10060 logical-and-expression
10061 logical-or-expression || logical-and-expression
10063 All these are implemented with a single function like:
10065 binary-expression:
10066 simple-cast-expression
10067 binary-expression <token> binary-expression
10069 CAST_P is true if this expression is the target of a cast.
10071 The binops_by_token map is used to get the tree codes for each <token> type.
10072 binary-expressions are associated according to a precedence table. */
10074 #define TOKEN_PRECEDENCE(token) \
10075 (((token->type == CPP_GREATER \
10076 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
10077 && !parser->greater_than_is_operator_p) \
10078 ? PREC_NOT_OPERATOR \
10079 : binops_by_token[token->type].prec)
10081 static cp_expr
10082 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
10083 bool no_toplevel_fold_p,
10084 bool decltype_p,
10085 enum cp_parser_prec prec,
10086 cp_id_kind * pidk)
10088 cp_parser_expression_stack stack;
10089 cp_parser_expression_stack_entry *sp = &stack[0];
10090 cp_parser_expression_stack_entry *disable_warnings_sp = NULL;
10091 cp_parser_expression_stack_entry current;
10092 cp_expr rhs;
10093 cp_token *token;
10094 enum tree_code rhs_type;
10095 enum cp_parser_prec new_prec, lookahead_prec;
10096 tree overload;
10098 /* Parse the first expression. */
10099 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
10100 ? TRUTH_NOT_EXPR : ERROR_MARK);
10101 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
10102 cast_p, decltype_p, pidk);
10103 current.prec = prec;
10105 if (cp_parser_error_occurred (parser))
10106 return error_mark_node;
10108 for (;;)
10110 /* Get an operator token. */
10111 token = cp_lexer_peek_token (parser->lexer);
10113 if (warn_cxx11_compat
10114 && token->type == CPP_RSHIFT
10115 && !parser->greater_than_is_operator_p)
10117 if (warning_at (token->location, OPT_Wc__11_compat,
10118 "%<>>%> operator is treated"
10119 " as two right angle brackets in C++11"))
10120 inform (token->location,
10121 "suggest parentheses around %<>>%> expression");
10124 new_prec = TOKEN_PRECEDENCE (token);
10125 if (new_prec != PREC_NOT_OPERATOR
10126 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10127 /* This is a fold-expression; handle it later. */
10128 new_prec = PREC_NOT_OPERATOR;
10130 /* Popping an entry off the stack means we completed a subexpression:
10131 - either we found a token which is not an operator (`>' where it is not
10132 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
10133 will happen repeatedly;
10134 - or, we found an operator which has lower priority. This is the case
10135 where the recursive descent *ascends*, as in `3 * 4 + 5' after
10136 parsing `3 * 4'. */
10137 if (new_prec <= current.prec)
10139 if (sp == stack)
10140 break;
10141 else
10142 goto pop;
10145 get_rhs:
10146 current.tree_type = binops_by_token[token->type].tree_type;
10147 current.loc = token->location;
10148 current.flags = token->flags;
10150 /* We used the operator token. */
10151 cp_lexer_consume_token (parser->lexer);
10153 /* For "false && x" or "true || x", x will never be executed;
10154 disable warnings while evaluating it. */
10155 if ((current.tree_type == TRUTH_ANDIF_EXPR
10156 && cp_fully_fold (current.lhs) == truthvalue_false_node)
10157 || (current.tree_type == TRUTH_ORIF_EXPR
10158 && cp_fully_fold (current.lhs) == truthvalue_true_node))
10160 disable_warnings_sp = sp;
10161 ++c_inhibit_evaluation_warnings;
10164 /* Extract another operand. It may be the RHS of this expression
10165 or the LHS of a new, higher priority expression. */
10166 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
10167 ? TRUTH_NOT_EXPR : ERROR_MARK);
10168 rhs = cp_parser_simple_cast_expression (parser);
10170 /* Get another operator token. Look up its precedence to avoid
10171 building a useless (immediately popped) stack entry for common
10172 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
10173 token = cp_lexer_peek_token (parser->lexer);
10174 lookahead_prec = TOKEN_PRECEDENCE (token);
10175 if (lookahead_prec != PREC_NOT_OPERATOR
10176 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10177 lookahead_prec = PREC_NOT_OPERATOR;
10178 if (lookahead_prec > new_prec)
10180 /* ... and prepare to parse the RHS of the new, higher priority
10181 expression. Since precedence levels on the stack are
10182 monotonically increasing, we do not have to care about
10183 stack overflows. */
10184 *sp = current;
10185 ++sp;
10186 current.lhs = rhs;
10187 current.lhs_type = rhs_type;
10188 current.prec = new_prec;
10189 new_prec = lookahead_prec;
10190 goto get_rhs;
10192 pop:
10193 lookahead_prec = new_prec;
10194 /* If the stack is not empty, we have parsed into LHS the right side
10195 (`4' in the example above) of an expression we had suspended.
10196 We can use the information on the stack to recover the LHS (`3')
10197 from the stack together with the tree code (`MULT_EXPR'), and
10198 the precedence of the higher level subexpression
10199 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
10200 which will be used to actually build the additive expression. */
10201 rhs = current.lhs;
10202 rhs_type = current.lhs_type;
10203 --sp;
10204 current = *sp;
10207 /* Undo the disabling of warnings done above. */
10208 if (sp == disable_warnings_sp)
10210 disable_warnings_sp = NULL;
10211 --c_inhibit_evaluation_warnings;
10214 if (warn_logical_not_paren
10215 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
10216 && current.lhs_type == TRUTH_NOT_EXPR
10217 /* Avoid warning for !!x == y. */
10218 && (TREE_CODE (current.lhs) != NE_EXPR
10219 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
10220 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
10221 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
10222 /* Avoid warning for !b == y where b is boolean. */
10223 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
10224 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
10225 != BOOLEAN_TYPE))))
10226 /* Avoid warning for !!b == y where b is boolean. */
10227 && (!DECL_P (tree_strip_any_location_wrapper (current.lhs))
10228 || TREE_TYPE (current.lhs) == NULL_TREE
10229 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
10230 warn_logical_not_parentheses (current.loc, current.tree_type,
10231 current.lhs, maybe_constant_value (rhs));
10233 if (warn_xor_used_as_pow
10234 && current.tree_type == BIT_XOR_EXPR
10235 /* Don't warn for named "xor" (as opposed to '^'). */
10236 && !(current.flags & NAMED_OP)
10237 && current.lhs.decimal_p ()
10238 && rhs.decimal_p ())
10239 check_for_xor_used_as_pow
10240 (current.lhs.get_location (),
10241 tree_strip_any_location_wrapper (current.lhs),
10242 current.loc,
10243 tree_strip_any_location_wrapper (rhs));
10245 overload = NULL;
10247 location_t combined_loc = make_location (current.loc,
10248 current.lhs.get_start (),
10249 rhs.get_finish ());
10251 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
10252 ERROR_MARK for everything that is not a binary expression.
10253 This makes warn_about_parentheses miss some warnings that
10254 involve unary operators. For unary expressions we should
10255 pass the correct tree_code unless the unary expression was
10256 surrounded by parentheses.
10258 if (no_toplevel_fold_p
10259 && lookahead_prec <= current.prec
10260 && sp == stack)
10262 if (current.lhs == error_mark_node || rhs == error_mark_node)
10263 current.lhs = error_mark_node;
10264 else
10266 current.lhs.maybe_add_location_wrapper ();
10267 rhs.maybe_add_location_wrapper ();
10268 current.lhs
10269 = build_min (current.tree_type,
10270 TREE_CODE_CLASS (current.tree_type)
10271 == tcc_comparison
10272 ? boolean_type_node : TREE_TYPE (current.lhs),
10273 current.lhs.get_value (), rhs.get_value ());
10274 SET_EXPR_LOCATION (current.lhs, combined_loc);
10277 else
10279 op_location_t op_loc (current.loc, combined_loc);
10280 current.lhs = build_x_binary_op (op_loc, current.tree_type,
10281 current.lhs, current.lhs_type,
10282 rhs, rhs_type, NULL_TREE, &overload,
10283 complain_flags (decltype_p));
10284 /* TODO: build_x_binary_op doesn't always honor the location. */
10285 current.lhs.set_location (combined_loc);
10287 current.lhs_type = current.tree_type;
10289 /* If the binary operator required the use of an overloaded operator,
10290 then this expression cannot be an integral constant-expression.
10291 An overloaded operator can be used even if both operands are
10292 otherwise permissible in an integral constant-expression if at
10293 least one of the operands is of enumeration type. */
10295 if (overload
10296 && cp_parser_non_integral_constant_expression (parser,
10297 NIC_OVERLOADED))
10298 return error_mark_node;
10301 return current.lhs;
10304 static cp_expr
10305 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
10306 bool no_toplevel_fold_p,
10307 enum cp_parser_prec prec,
10308 cp_id_kind * pidk)
10310 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
10311 /*decltype*/false, prec, pidk);
10314 /* Parse the `? expression : assignment-expression' part of a
10315 conditional-expression. The LOGICAL_OR_EXPR is the
10316 logical-or-expression that started the conditional-expression.
10317 Returns a representation of the entire conditional-expression.
10319 This routine is used by cp_parser_assignment_expression
10320 and cp_parser_conditional_expression.
10322 ? expression : assignment-expression
10324 GNU Extensions:
10326 ? : assignment-expression */
10328 static tree
10329 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
10331 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
10332 cp_expr assignment_expr;
10333 struct cp_token *token;
10334 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10336 /* Consume the `?' token. */
10337 cp_lexer_consume_token (parser->lexer);
10338 token = cp_lexer_peek_token (parser->lexer);
10339 if (cp_parser_allow_gnu_extensions_p (parser)
10340 && token->type == CPP_COLON)
10342 pedwarn (token->location, OPT_Wpedantic,
10343 "ISO C++ does not allow %<?:%> with omitted middle operand");
10344 /* Implicit true clause. */
10345 expr = NULL_TREE;
10346 c_inhibit_evaluation_warnings +=
10347 folded_logical_or_expr == truthvalue_true_node;
10348 warn_for_omitted_condop (token->location, logical_or_expr);
10350 else
10352 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10353 parser->colon_corrects_to_scope_p = false;
10354 /* Parse the expression. */
10355 c_inhibit_evaluation_warnings +=
10356 folded_logical_or_expr == truthvalue_false_node;
10357 expr = cp_parser_expression (parser);
10358 c_inhibit_evaluation_warnings +=
10359 ((folded_logical_or_expr == truthvalue_true_node)
10360 - (folded_logical_or_expr == truthvalue_false_node));
10361 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10364 /* The next token should be a `:'. */
10365 cp_parser_require (parser, CPP_COLON, RT_COLON);
10366 /* Parse the assignment-expression. */
10367 assignment_expr = cp_parser_assignment_expression (parser);
10368 c_inhibit_evaluation_warnings -=
10369 folded_logical_or_expr == truthvalue_true_node;
10371 /* Make a location:
10372 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
10373 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
10374 with the caret at the "?", ranging from the start of
10375 the logical_or_expr to the end of the assignment_expr. */
10376 loc = make_location (loc,
10377 logical_or_expr.get_start (),
10378 assignment_expr.get_finish ());
10380 /* Build the conditional-expression. */
10381 return build_x_conditional_expr (loc, logical_or_expr,
10382 expr,
10383 assignment_expr,
10384 tf_warning_or_error);
10387 /* Parse a conditional-expression.
10389 conditional-expression:
10390 logical-or-expression
10391 logical-or-expression ? expression : assignment-expression
10393 GNU Extensions:
10395 logical-or-expression ? : assignment-expression */
10397 static cp_expr
10398 cp_parser_conditional_expression (cp_parser *parser)
10400 cp_expr expr = cp_parser_binary_expression (parser, false, false, false,
10401 PREC_NOT_OPERATOR, NULL);
10402 /* If the next token is a `?' then we're actually looking at
10403 a conditional-expression; otherwise we're done. */
10404 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
10405 return cp_parser_question_colon_clause (parser, expr);
10406 return expr;
10409 /* Parse an assignment-expression.
10411 assignment-expression:
10412 conditional-expression
10413 logical-or-expression assignment-operator assignment_expression
10414 throw-expression
10415 yield-expression
10417 CAST_P is true if this expression is the target of a cast.
10418 DECLTYPE_P is true if this expression is the operand of decltype.
10420 Returns a representation for the expression. */
10422 static cp_expr
10423 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
10424 bool cast_p, bool decltype_p)
10426 cp_expr expr;
10428 /* If the next token is the `throw' keyword, then we're looking at
10429 a throw-expression. */
10430 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
10431 expr = cp_parser_throw_expression (parser);
10432 /* If the next token is the `co_yield' keyword, then we're looking at
10433 a yield-expression. */
10434 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CO_YIELD))
10435 expr = cp_parser_yield_expression (parser);
10436 /* Otherwise, it must be that we are looking at a
10437 logical-or-expression. */
10438 else
10440 /* Parse the binary expressions (logical-or-expression). */
10441 expr = cp_parser_binary_expression (parser, cast_p, false,
10442 decltype_p,
10443 PREC_NOT_OPERATOR, pidk);
10444 /* If the next token is a `?' then we're actually looking at a
10445 conditional-expression. */
10446 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
10447 return cp_parser_question_colon_clause (parser, expr);
10448 else
10450 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10452 /* If it's an assignment-operator, we're using the second
10453 production. */
10454 enum tree_code assignment_operator
10455 = cp_parser_assignment_operator_opt (parser);
10456 if (assignment_operator != ERROR_MARK)
10458 bool non_constant_p;
10460 /* Parse the right-hand side of the assignment. */
10461 cp_expr rhs = cp_parser_initializer_clause (parser,
10462 &non_constant_p);
10464 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
10465 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10467 /* An assignment may not appear in a
10468 constant-expression. */
10469 if (cp_parser_non_integral_constant_expression (parser,
10470 NIC_ASSIGNMENT))
10471 return error_mark_node;
10472 /* Build the assignment expression. Its default
10473 location:
10474 LHS = RHS
10475 ~~~~^~~~~
10476 is the location of the '=' token as the
10477 caret, ranging from the start of the lhs to the
10478 end of the rhs. */
10479 loc = make_location (loc,
10480 expr.get_start (),
10481 rhs.get_finish ());
10482 expr = build_x_modify_expr (loc, expr,
10483 assignment_operator,
10484 rhs, NULL_TREE,
10485 complain_flags (decltype_p));
10486 /* TODO: build_x_modify_expr doesn't honor the location,
10487 so we must set it here. */
10488 expr.set_location (loc);
10493 return expr;
10496 /* Parse an (optional) assignment-operator.
10498 assignment-operator: one of
10499 = *= /= %= += -= >>= <<= &= ^= |=
10501 GNU Extension:
10503 assignment-operator: one of
10504 <?= >?=
10506 If the next token is an assignment operator, the corresponding tree
10507 code is returned, and the token is consumed. For example, for
10508 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
10509 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
10510 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
10511 operator, ERROR_MARK is returned. */
10513 static enum tree_code
10514 cp_parser_assignment_operator_opt (cp_parser* parser)
10516 enum tree_code op;
10517 cp_token *token;
10519 /* Peek at the next token. */
10520 token = cp_lexer_peek_token (parser->lexer);
10522 switch (token->type)
10524 case CPP_EQ:
10525 op = NOP_EXPR;
10526 break;
10528 case CPP_MULT_EQ:
10529 op = MULT_EXPR;
10530 break;
10532 case CPP_DIV_EQ:
10533 op = TRUNC_DIV_EXPR;
10534 break;
10536 case CPP_MOD_EQ:
10537 op = TRUNC_MOD_EXPR;
10538 break;
10540 case CPP_PLUS_EQ:
10541 op = PLUS_EXPR;
10542 break;
10544 case CPP_MINUS_EQ:
10545 op = MINUS_EXPR;
10546 break;
10548 case CPP_RSHIFT_EQ:
10549 op = RSHIFT_EXPR;
10550 break;
10552 case CPP_LSHIFT_EQ:
10553 op = LSHIFT_EXPR;
10554 break;
10556 case CPP_AND_EQ:
10557 op = BIT_AND_EXPR;
10558 break;
10560 case CPP_XOR_EQ:
10561 op = BIT_XOR_EXPR;
10562 break;
10564 case CPP_OR_EQ:
10565 op = BIT_IOR_EXPR;
10566 break;
10568 default:
10569 /* Nothing else is an assignment operator. */
10570 op = ERROR_MARK;
10573 /* An operator followed by ... is a fold-expression, handled elsewhere. */
10574 if (op != ERROR_MARK
10575 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10576 op = ERROR_MARK;
10578 /* If it was an assignment operator, consume it. */
10579 if (op != ERROR_MARK)
10580 cp_lexer_consume_token (parser->lexer);
10582 return op;
10585 /* Parse an expression.
10587 expression:
10588 assignment-expression
10589 expression , assignment-expression
10591 CAST_P is true if this expression is the target of a cast.
10592 DECLTYPE_P is true if this expression is the immediate operand of decltype,
10593 except possibly parenthesized or on the RHS of a comma (N3276).
10594 WARN_COMMA_P is true if a comma should be diagnosed.
10596 Returns a representation of the expression. */
10598 static cp_expr
10599 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
10600 bool cast_p, bool decltype_p, bool warn_comma_p)
10602 cp_expr expression = NULL_TREE;
10603 location_t loc = UNKNOWN_LOCATION;
10605 while (true)
10607 cp_expr assignment_expression;
10609 /* Parse the next assignment-expression. */
10610 assignment_expression
10611 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
10613 /* We don't create a temporary for a call that is the immediate operand
10614 of decltype or on the RHS of a comma. But when we see a comma, we
10615 need to create a temporary for a call on the LHS. */
10616 if (decltype_p && !processing_template_decl
10617 && TREE_CODE (assignment_expression) == CALL_EXPR
10618 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
10619 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10620 assignment_expression
10621 = build_cplus_new (TREE_TYPE (assignment_expression),
10622 assignment_expression, tf_warning_or_error);
10624 /* If this is the first assignment-expression, we can just
10625 save it away. */
10626 if (!expression)
10627 expression = assignment_expression;
10628 else
10630 /* Create a location with caret at the comma, ranging
10631 from the start of the LHS to the end of the RHS. */
10632 loc = make_location (loc,
10633 expression.get_start (),
10634 assignment_expression.get_finish ());
10635 expression = build_x_compound_expr (loc, expression,
10636 assignment_expression, NULL_TREE,
10637 complain_flags (decltype_p));
10638 expression.set_location (loc);
10640 /* If the next token is not a comma, or we're in a fold-expression, then
10641 we are done with the expression. */
10642 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
10643 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10644 break;
10645 /* Consume the `,'. */
10646 loc = cp_lexer_peek_token (parser->lexer)->location;
10647 if (warn_comma_p)
10649 /* [depr.comma.subscript]: A comma expression appearing as
10650 the expr-or-braced-init-list of a subscripting expression
10651 is deprecated. A parenthesized comma expression is not
10652 deprecated. */
10653 warning_at (loc, OPT_Wcomma_subscript,
10654 "top-level comma expression in array subscript "
10655 "is deprecated");
10656 warn_comma_p = false;
10658 cp_lexer_consume_token (parser->lexer);
10659 /* A comma operator cannot appear in a constant-expression. */
10660 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
10661 expression = error_mark_node;
10664 return expression;
10667 /* Parse a constant-expression.
10669 constant-expression:
10670 conditional-expression
10672 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
10673 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
10674 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
10675 is false, NON_CONSTANT_P should be NULL. If ALLOW_NON_CONSTANT_P is
10676 greater than 1, this isn't really a constant-expression, only a
10677 potentially constant-evaluated expression. If STRICT_P is true,
10678 only parse a conditional-expression, otherwise parse an
10679 assignment-expression. See below for rationale. */
10681 static cp_expr
10682 cp_parser_constant_expression (cp_parser* parser,
10683 int allow_non_constant_p,
10684 bool *non_constant_p,
10685 bool strict_p)
10687 bool saved_integral_constant_expression_p;
10688 bool saved_allow_non_integral_constant_expression_p;
10689 bool saved_non_integral_constant_expression_p;
10690 cp_expr expression;
10692 /* It might seem that we could simply parse the
10693 conditional-expression, and then check to see if it were
10694 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
10695 one that the compiler can figure out is constant, possibly after
10696 doing some simplifications or optimizations. The standard has a
10697 precise definition of constant-expression, and we must honor
10698 that, even though it is somewhat more restrictive.
10700 For example:
10702 int i[(2, 3)];
10704 is not a legal declaration, because `(2, 3)' is not a
10705 constant-expression. The `,' operator is forbidden in a
10706 constant-expression. However, GCC's constant-folding machinery
10707 will fold this operation to an INTEGER_CST for `3'. */
10709 /* Save the old settings. */
10710 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
10711 saved_allow_non_integral_constant_expression_p
10712 = parser->allow_non_integral_constant_expression_p;
10713 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
10714 /* We are now parsing a constant-expression. */
10715 parser->integral_constant_expression_p = true;
10716 parser->allow_non_integral_constant_expression_p
10717 = (allow_non_constant_p || cxx_dialect >= cxx11);
10718 parser->non_integral_constant_expression_p = false;
10720 /* A manifestly constant-evaluated expression is evaluated even in an
10721 unevaluated operand. */
10722 cp_evaluated ev (/*reset if*/allow_non_constant_p <= 1);
10724 /* Although the grammar says "conditional-expression", when not STRICT_P,
10725 we parse an "assignment-expression", which also permits
10726 "throw-expression" and the use of assignment operators. In the case
10727 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
10728 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
10729 actually essential that we look for an assignment-expression.
10730 For example, cp_parser_initializer_clauses uses this function to
10731 determine whether a particular assignment-expression is in fact
10732 constant. */
10733 if (strict_p)
10734 expression = cp_parser_conditional_expression (parser);
10735 else
10736 expression = cp_parser_assignment_expression (parser);
10737 /* Restore the old settings. */
10738 parser->integral_constant_expression_p
10739 = saved_integral_constant_expression_p;
10740 parser->allow_non_integral_constant_expression_p
10741 = saved_allow_non_integral_constant_expression_p;
10742 if (cxx_dialect >= cxx11)
10744 /* Require an rvalue constant expression here; that's what our
10745 callers expect. Reference constant expressions are handled
10746 separately in e.g. cp_parser_template_argument. */
10747 tree decay = expression;
10748 if (TREE_TYPE (expression)
10749 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
10750 decay = build_address (expression);
10751 bool is_const = is_rvalue_constant_expression (decay);
10752 parser->non_integral_constant_expression_p = !is_const;
10753 if (!is_const && !allow_non_constant_p)
10754 require_rvalue_constant_expression (decay);
10756 if (allow_non_constant_p)
10757 *non_constant_p = parser->non_integral_constant_expression_p;
10758 parser->non_integral_constant_expression_p
10759 = saved_non_integral_constant_expression_p;
10761 return expression;
10764 /* Parse __builtin_offsetof.
10766 offsetof-expression:
10767 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
10769 offsetof-member-designator:
10770 id-expression
10771 | offsetof-member-designator "." id-expression
10772 | offsetof-member-designator "[" expression "]"
10773 | offsetof-member-designator "->" id-expression */
10775 static cp_expr
10776 cp_parser_builtin_offsetof (cp_parser *parser)
10778 int save_ice_p, save_non_ice_p;
10779 tree type;
10780 cp_expr expr;
10781 cp_id_kind dummy;
10782 cp_token *token;
10783 location_t finish_loc;
10785 /* We're about to accept non-integral-constant things, but will
10786 definitely yield an integral constant expression. Save and
10787 restore these values around our local parsing. */
10788 save_ice_p = parser->integral_constant_expression_p;
10789 save_non_ice_p = parser->non_integral_constant_expression_p;
10791 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10793 /* Consume the "__builtin_offsetof" token. */
10794 cp_lexer_consume_token (parser->lexer);
10795 /* Consume the opening `('. */
10796 matching_parens parens;
10797 parens.require_open (parser);
10798 /* Parse the type-id. */
10799 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10801 const char *saved_message = parser->type_definition_forbidden_message;
10802 parser->type_definition_forbidden_message
10803 = G_("types may not be defined within %<__builtin_offsetof%>");
10804 type = cp_parser_type_id (parser);
10805 parser->type_definition_forbidden_message = saved_message;
10807 /* Look for the `,'. */
10808 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10809 token = cp_lexer_peek_token (parser->lexer);
10811 /* Build the (type *)null that begins the traditional offsetof macro. */
10812 tree object_ptr
10813 = build_static_cast (input_location, build_pointer_type (type),
10814 null_pointer_node, tf_warning_or_error);
10816 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
10817 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
10818 true, &dummy, token->location);
10819 while (true)
10821 token = cp_lexer_peek_token (parser->lexer);
10822 switch (token->type)
10824 case CPP_OPEN_SQUARE:
10825 /* offsetof-member-designator "[" expression "]" */
10826 expr = cp_parser_postfix_open_square_expression (parser, expr,
10827 true, false);
10828 break;
10830 case CPP_DEREF:
10831 /* offsetof-member-designator "->" identifier */
10832 expr = grok_array_decl (token->location, expr, integer_zero_node,
10833 NULL, tf_warning_or_error);
10834 /* FALLTHRU */
10836 case CPP_DOT:
10837 /* offsetof-member-designator "." identifier */
10838 cp_lexer_consume_token (parser->lexer);
10839 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
10840 expr, true, &dummy,
10841 token->location);
10842 break;
10844 case CPP_CLOSE_PAREN:
10845 /* Consume the ")" token. */
10846 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10847 cp_lexer_consume_token (parser->lexer);
10848 goto success;
10850 default:
10851 /* Error. We know the following require will fail, but
10852 that gives the proper error message. */
10853 parens.require_close (parser);
10854 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
10855 expr = error_mark_node;
10856 goto failure;
10860 success:
10861 /* Make a location of the form:
10862 __builtin_offsetof (struct s, f)
10863 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
10864 with caret at the type-id, ranging from the start of the
10865 "_builtin_offsetof" token to the close paren. */
10866 loc = make_location (loc, start_loc, finish_loc);
10867 /* The result will be an INTEGER_CST, so we need to explicitly
10868 preserve the location. */
10869 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
10871 failure:
10872 parser->integral_constant_expression_p = save_ice_p;
10873 parser->non_integral_constant_expression_p = save_non_ice_p;
10875 expr = expr.maybe_add_location_wrapper ();
10876 return expr;
10879 /* Parse a builtin trait expression or type. */
10881 static cp_expr
10882 cp_parser_trait (cp_parser* parser, enum rid keyword)
10884 cp_trait_kind kind;
10885 tree type1, type2 = NULL_TREE;
10886 bool binary = false;
10887 bool variadic = false;
10888 bool type = false;
10890 switch (keyword)
10892 #define DEFTRAIT(TCC, CODE, NAME, ARITY) \
10893 case RID_##CODE: \
10894 kind = CPTK_##CODE; \
10895 binary = (ARITY == 2); \
10896 variadic = (ARITY == -1); \
10897 type = (TCC == tcc_type); \
10898 break;
10899 #include "cp-trait.def"
10900 #undef DEFTRAIT
10901 default:
10902 gcc_unreachable ();
10905 /* Get location of initial token. */
10906 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10908 /* Consume the token. */
10909 cp_lexer_consume_token (parser->lexer);
10911 matching_parens parens;
10912 parens.require_open (parser);
10915 type_id_in_expr_sentinel s (parser);
10916 type1 = cp_parser_type_id (parser);
10919 if (type1 == error_mark_node)
10920 return error_mark_node;
10922 if (binary)
10924 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10927 type_id_in_expr_sentinel s (parser);
10928 type2 = cp_parser_type_id (parser);
10931 if (type2 == error_mark_node)
10932 return error_mark_node;
10934 else if (variadic)
10936 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10938 cp_lexer_consume_token (parser->lexer);
10939 tree elt = cp_parser_type_id (parser);
10940 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10942 cp_lexer_consume_token (parser->lexer);
10943 elt = make_pack_expansion (elt);
10945 if (elt == error_mark_node)
10946 return error_mark_node;
10947 type2 = tree_cons (NULL_TREE, elt, type2);
10949 type2 = nreverse (type2);
10952 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10953 parens.require_close (parser);
10955 /* Construct a location of the form:
10956 __is_trivially_copyable(_Tp)
10957 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10958 with start == caret, finishing at the close-paren. */
10959 location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
10961 /* Complete the trait expression, which may mean either processing
10962 the trait expr now or saving it for template instantiation. */
10963 switch (kind)
10965 case CPTK_BASES:
10966 return cp_expr (finish_bases (type1, false), trait_loc);
10967 case CPTK_DIRECT_BASES:
10968 return cp_expr (finish_bases (type1, true), trait_loc);
10969 default:
10970 if (type)
10971 return finish_trait_type (kind, type1, type2);
10972 else
10973 return finish_trait_expr (trait_loc, kind, type1, type2);
10977 /* Parse a lambda expression.
10979 lambda-expression:
10980 lambda-introducer lambda-declarator [opt] compound-statement
10981 lambda-introducer < template-parameter-list > requires-clause [opt]
10982 lambda-declarator [opt] compound-statement
10984 Returns a representation of the expression. */
10986 static cp_expr
10987 cp_parser_lambda_expression (cp_parser* parser)
10989 tree lambda_expr = build_lambda_expr ();
10990 tree type;
10991 bool ok = true;
10992 cp_token *token = cp_lexer_peek_token (parser->lexer);
10993 cp_token_position start = 0;
10995 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
10997 if (cxx_dialect >= cxx20)
10999 /* C++20 allows lambdas in unevaluated context, but one in the type of a
11000 non-type parameter is nonsensical.
11002 Distinguish a lambda in the parameter type from a lambda in the
11003 default argument by looking at local_variables_forbidden_p, which is
11004 only set in default arguments. */
11005 if (processing_template_parmlist && !parser->local_variables_forbidden_p)
11007 error_at (token->location,
11008 "lambda-expression in template parameter type");
11009 token->error_reported = true;
11010 ok = false;
11013 else if (cp_unevaluated_operand)
11015 if (!token->error_reported)
11017 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
11018 "lambda-expression in unevaluated context"
11019 " only available with %<-std=c++20%> or %<-std=gnu++20%>");
11020 token->error_reported = true;
11022 ok = false;
11024 else if (parser->in_template_argument_list_p || processing_template_parmlist)
11026 if (!token->error_reported)
11028 error_at (token->location, "lambda-expression in template-argument"
11029 " only available with %<-std=c++20%> or %<-std=gnu++20%>");
11030 token->error_reported = true;
11032 ok = false;
11035 /* We may be in the middle of deferred access check. Disable
11036 it now. */
11037 push_deferring_access_checks (dk_no_deferred);
11039 cp_parser_lambda_introducer (parser, lambda_expr);
11040 if (cp_parser_error_occurred (parser))
11041 return error_mark_node;
11043 type = begin_lambda_type (lambda_expr);
11044 if (type == error_mark_node)
11045 return error_mark_node;
11047 record_lambda_scope (lambda_expr);
11048 record_lambda_scope_discriminator (lambda_expr);
11050 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
11051 determine_visibility (TYPE_NAME (type));
11053 /* Now that we've started the type, add the capture fields for any
11054 explicit captures. */
11055 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
11058 /* Inside the class, surrounding template-parameter-lists do not apply. */
11059 unsigned int saved_num_template_parameter_lists
11060 = parser->num_template_parameter_lists;
11061 unsigned char in_statement = parser->in_statement;
11062 bool in_switch_statement_p = parser->in_switch_statement_p;
11063 bool fully_implicit_function_template_p
11064 = parser->fully_implicit_function_template_p;
11065 tree implicit_template_parms = parser->implicit_template_parms;
11066 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
11067 bool auto_is_implicit_function_template_parm_p
11068 = parser->auto_is_implicit_function_template_parm_p;
11070 parser->num_template_parameter_lists = 0;
11071 parser->in_statement = 0;
11072 parser->in_switch_statement_p = false;
11073 parser->fully_implicit_function_template_p = false;
11074 parser->implicit_template_parms = 0;
11075 parser->implicit_template_scope = 0;
11076 parser->auto_is_implicit_function_template_parm_p = false;
11078 /* The body of a lambda in a discarded statement is not discarded. */
11079 bool discarded = in_discarded_stmt;
11080 in_discarded_stmt = 0;
11082 /* Similarly the body of a lambda in immediate function context is not
11083 in immediate function context. */
11084 bool save_in_consteval_if_p = in_consteval_if_p;
11085 in_consteval_if_p = false;
11087 /* By virtue of defining a local class, a lambda expression has access to
11088 the private variables of enclosing classes. */
11090 if (cp_parser_start_tentative_firewall (parser))
11091 start = token;
11093 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
11095 if (ok && cp_parser_error_occurred (parser))
11096 ok = false;
11098 if (ok)
11099 cp_parser_lambda_body (parser, lambda_expr);
11100 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
11102 if (cp_parser_skip_to_closing_brace (parser))
11103 cp_lexer_consume_token (parser->lexer);
11106 /* The capture list was built up in reverse order; fix that now. */
11107 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
11108 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
11110 if (ok)
11111 maybe_add_lambda_conv_op (type);
11113 finish_struct (type, /*attributes=*/NULL_TREE);
11115 in_consteval_if_p = save_in_consteval_if_p;
11116 in_discarded_stmt = discarded;
11118 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
11119 parser->in_statement = in_statement;
11120 parser->in_switch_statement_p = in_switch_statement_p;
11121 parser->fully_implicit_function_template_p
11122 = fully_implicit_function_template_p;
11123 parser->implicit_template_parms = implicit_template_parms;
11124 parser->implicit_template_scope = implicit_template_scope;
11125 parser->auto_is_implicit_function_template_parm_p
11126 = auto_is_implicit_function_template_parm_p;
11129 /* This field is only used during parsing of the lambda. */
11130 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
11132 /* This lambda shouldn't have any proxies left at this point. */
11133 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
11134 /* And now that we're done, push proxies for an enclosing lambda. */
11135 insert_pending_capture_proxies ();
11137 /* Update the lambda expression to a range. */
11138 LAMBDA_EXPR_LOCATION (lambda_expr) = make_location (token->location,
11139 token->location,
11140 parser->lexer);
11142 if (ok)
11143 lambda_expr = build_lambda_object (lambda_expr);
11144 else
11145 lambda_expr = error_mark_node;
11147 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
11149 pop_deferring_access_checks ();
11151 return lambda_expr;
11154 /* Parse the beginning of a lambda expression.
11156 lambda-introducer:
11157 [ lambda-capture [opt] ]
11159 LAMBDA_EXPR is the current representation of the lambda expression. */
11161 static void
11162 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
11164 /* Need commas after the first capture. */
11165 bool first = true;
11167 /* Eat the leading `['. */
11168 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
11170 /* Record default capture mode. "[&" "[=" "[&," "[=," */
11171 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
11172 && !cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS)
11173 && !cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME)
11174 && !cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
11175 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
11176 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11177 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
11179 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
11181 cp_lexer_consume_token (parser->lexer);
11182 first = false;
11184 if (!(at_function_scope_p () || parsing_nsdmi ()))
11185 error ("non-local lambda expression cannot have a capture-default");
11188 hash_set<tree, true> ids;
11189 tree first_capture_id = NULL_TREE;
11190 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
11192 cp_token* capture_token;
11193 tree capture_id;
11194 tree capture_init_expr;
11195 cp_id_kind idk = CP_ID_KIND_NONE;
11196 bool explicit_init_p = false;
11198 enum capture_kind_type
11200 BY_COPY,
11201 BY_REFERENCE
11203 enum capture_kind_type capture_kind = BY_COPY;
11205 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
11207 error ("expected end of capture-list");
11208 return;
11211 if (first)
11212 first = false;
11213 else
11214 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
11216 /* Possibly capture `this'. */
11217 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
11219 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11220 if (cxx_dialect < cxx20 && pedantic
11221 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
11222 pedwarn (loc, OPT_Wc__20_extensions,
11223 "explicit by-copy capture of %<this%> "
11224 "with by-copy capture default only available with "
11225 "%<-std=c++20%> or %<-std=gnu++20%>");
11226 cp_lexer_consume_token (parser->lexer);
11227 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
11228 pedwarn (input_location, 0,
11229 "already captured %qD in lambda expression",
11230 this_identifier);
11231 else
11232 add_capture (lambda_expr, /*id=*/this_identifier,
11233 /*initializer=*/finish_this_expr (),
11234 /*by_reference_p=*/true, explicit_init_p);
11235 continue;
11238 /* Possibly capture `*this'. */
11239 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
11240 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
11242 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11243 if (cxx_dialect < cxx17)
11244 pedwarn (loc, OPT_Wc__17_extensions,
11245 "%<*this%> capture only available with "
11246 "%<-std=c++17%> or %<-std=gnu++17%>");
11247 cp_lexer_consume_token (parser->lexer);
11248 cp_lexer_consume_token (parser->lexer);
11249 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
11250 pedwarn (input_location, 0,
11251 "already captured %qD in lambda expression",
11252 this_identifier);
11253 else
11254 add_capture (lambda_expr, /*id=*/this_identifier,
11255 /*initializer=*/finish_this_expr (),
11256 /*by_reference_p=*/false, explicit_init_p);
11257 continue;
11260 /* But reject `&this'. */
11261 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
11262 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
11264 error_at (cp_lexer_peek_token (parser->lexer)->location,
11265 "%<this%> cannot be captured by reference");
11266 cp_lexer_consume_token (parser->lexer);
11267 cp_lexer_consume_token (parser->lexer);
11268 continue;
11271 /* Remember whether we want to capture as a reference or not. */
11272 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
11274 capture_kind = BY_REFERENCE;
11275 cp_lexer_consume_token (parser->lexer);
11278 bool init_pack_expansion = false;
11279 location_t ellipsis_loc = UNKNOWN_LOCATION;
11280 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11282 ellipsis_loc = cp_lexer_peek_token (parser->lexer)->location;
11283 if (cxx_dialect < cxx20)
11284 pedwarn (ellipsis_loc, OPT_Wc__20_extensions,
11285 "pack init-capture only available with "
11286 "%<-std=c++20%> or %<-std=gnu++20%>");
11287 cp_lexer_consume_token (parser->lexer);
11288 init_pack_expansion = true;
11291 /* Early C++20 drafts had ...& instead of &...; be forgiving. */
11292 if (init_pack_expansion && capture_kind != BY_REFERENCE
11293 && cp_lexer_next_token_is (parser->lexer, CPP_AND))
11295 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
11296 0, "%<&%> should come before %<...%>");
11297 capture_kind = BY_REFERENCE;
11298 cp_lexer_consume_token (parser->lexer);
11301 /* Get the identifier. */
11302 capture_token = cp_lexer_peek_token (parser->lexer);
11303 capture_id = cp_parser_identifier (parser);
11305 if (capture_id == error_mark_node)
11306 /* Would be nice to have a cp_parser_skip_to_closing_x for general
11307 delimiters, but I modified this to stop on unnested ']' as well. It
11308 was already changed to stop on unnested '}', so the
11309 "closing_parenthesis" name is no more misleading with my change. */
11311 cp_parser_skip_to_closing_parenthesis (parser,
11312 /*recovering=*/true,
11313 /*or_comma=*/true,
11314 /*consume_paren=*/true);
11315 break;
11318 /* Find the initializer for this capture. */
11319 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
11320 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
11321 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11323 bool direct, non_constant;
11324 /* An explicit initializer exists. */
11325 if (cxx_dialect < cxx14)
11326 pedwarn (input_location, OPT_Wc__14_extensions,
11327 "lambda capture initializers "
11328 "only available with %<-std=c++14%> or %<-std=gnu++14%>");
11329 capture_init_expr = cp_parser_initializer (parser, &direct,
11330 &non_constant, true);
11331 explicit_init_p = true;
11332 if (capture_init_expr == NULL_TREE)
11334 error ("empty initializer for lambda init-capture");
11335 capture_init_expr = error_mark_node;
11337 if (init_pack_expansion)
11338 capture_init_expr = make_pack_expansion (capture_init_expr);
11340 else
11342 const char* error_msg;
11344 /* Turn the identifier into an id-expression. */
11345 capture_init_expr
11346 = cp_parser_lookup_name_simple (parser, capture_id,
11347 capture_token->location);
11349 if (capture_init_expr == error_mark_node)
11351 unqualified_name_lookup_error (capture_id);
11352 continue;
11354 else if (!VAR_P (capture_init_expr)
11355 && TREE_CODE (capture_init_expr) != PARM_DECL)
11357 error_at (capture_token->location,
11358 "capture of non-variable %qE",
11359 capture_init_expr);
11360 if (DECL_P (capture_init_expr))
11361 inform (DECL_SOURCE_LOCATION (capture_init_expr),
11362 "%q#D declared here", capture_init_expr);
11363 continue;
11365 if (VAR_P (capture_init_expr)
11366 && decl_storage_duration (capture_init_expr) != dk_auto)
11368 if (pedwarn (capture_token->location, 0, "capture of variable "
11369 "%qD with non-automatic storage duration",
11370 capture_init_expr))
11371 inform (DECL_SOURCE_LOCATION (capture_init_expr),
11372 "%q#D declared here", capture_init_expr);
11373 continue;
11376 capture_init_expr
11377 = finish_id_expression
11378 (capture_id,
11379 capture_init_expr,
11380 parser->scope,
11381 &idk,
11382 /*integral_constant_expression_p=*/false,
11383 /*allow_non_integral_constant_expression_p=*/false,
11384 /*non_integral_constant_expression_p=*/NULL,
11385 /*template_p=*/false,
11386 /*done=*/true,
11387 /*address_p=*/false,
11388 /*template_arg_p=*/false,
11389 &error_msg,
11390 capture_token->location);
11392 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11394 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11395 cp_lexer_consume_token (parser->lexer);
11396 capture_init_expr = make_pack_expansion (capture_init_expr);
11397 if (init_pack_expansion)
11399 /* If what follows is an initializer, the second '...' is
11400 invalid. But for cases like [...xs...], the first one
11401 is invalid. */
11402 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
11403 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
11404 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11405 ellipsis_loc = loc;
11406 error_at (ellipsis_loc, "too many %<...%> in lambda capture");
11407 continue;
11412 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
11413 && !explicit_init_p)
11415 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
11416 && capture_kind == BY_COPY)
11417 pedwarn (capture_token->location, 0, "explicit by-copy capture "
11418 "of %qD redundant with by-copy capture default",
11419 capture_id);
11420 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
11421 && capture_kind == BY_REFERENCE)
11422 pedwarn (capture_token->location, 0, "explicit by-reference "
11423 "capture of %qD redundant with by-reference capture "
11424 "default", capture_id);
11427 /* Check for duplicates.
11428 Optimize for the zero or one explicit captures cases and only create
11429 the hash_set after adding second capture. */
11430 bool found = false;
11431 if (!ids.is_empty ())
11432 found = ids.add (capture_id);
11433 else if (first_capture_id == NULL_TREE)
11434 first_capture_id = capture_id;
11435 else if (capture_id == first_capture_id)
11436 found = true;
11437 else
11439 ids.add (first_capture_id);
11440 ids.add (capture_id);
11442 if (found)
11443 pedwarn (input_location, 0,
11444 "already captured %qD in lambda expression", capture_id);
11445 else
11446 add_capture (lambda_expr, capture_id, capture_init_expr,
11447 /*by_reference_p=*/capture_kind == BY_REFERENCE,
11448 explicit_init_p);
11450 /* If there is any qualification still in effect, clear it
11451 now; we will be starting fresh with the next capture. */
11452 parser->scope = NULL_TREE;
11453 parser->qualifying_scope = NULL_TREE;
11454 parser->object_scope = NULL_TREE;
11457 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11460 /* Parse the (optional) middle of a lambda expression.
11462 lambda-declarator:
11463 ( parameter-declaration-clause ) lambda-specifiers requires-clause [opt]
11464 lambda-specifiers (C++23)
11466 lambda-specifiers:
11467 decl-specifier-seq [opt] noexcept-specifier [opt]
11468 attribute-specifier-seq [opt] trailing-return-type [opt]
11470 LAMBDA_EXPR is the current representation of the lambda expression. */
11472 static bool
11473 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
11475 /* 5.1.1.4 of the standard says:
11476 If a lambda-expression does not include a lambda-declarator, it is as if
11477 the lambda-declarator were ().
11478 This means an empty parameter list, no attributes, and no exception
11479 specification. */
11480 tree param_list = void_list_node;
11481 tree std_attrs = NULL_TREE;
11482 tree gnu_attrs = NULL_TREE;
11483 tree exception_spec = NULL_TREE;
11484 tree template_param_list = NULL_TREE;
11485 tree tx_qual = NULL_TREE;
11486 tree return_type = NULL_TREE;
11487 tree trailing_requires_clause = NULL_TREE;
11488 bool has_param_list = false;
11489 location_t omitted_parms_loc = UNKNOWN_LOCATION;
11490 cp_decl_specifier_seq lambda_specs;
11491 clear_decl_specs (&lambda_specs);
11492 /* A lambda op() is const unless explicitly 'mutable'. */
11493 cp_cv_quals quals = TYPE_QUAL_CONST;
11495 /* The template-parameter-list is optional, but must begin with
11496 an opening angle if present. */
11497 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
11499 if (cxx_dialect < cxx14)
11500 pedwarn (parser->lexer->next_token->location, OPT_Wc__14_extensions,
11501 "lambda templates are only available with "
11502 "%<-std=c++14%> or %<-std=gnu++14%>");
11503 else if (pedantic && cxx_dialect < cxx20)
11504 pedwarn (parser->lexer->next_token->location, OPT_Wc__20_extensions,
11505 "lambda templates are only available with "
11506 "%<-std=c++20%> or %<-std=gnu++20%>");
11508 cp_lexer_consume_token (parser->lexer);
11510 template_param_list = cp_parser_template_parameter_list (parser);
11511 cp_parser_require_end_of_template_parameter_list (parser);
11513 /* We may have a constrained generic lambda; parse the requires-clause
11514 immediately after the template-parameter-list and combine with any
11515 shorthand constraints present. */
11516 tree dreqs = cp_parser_requires_clause_opt (parser, true);
11517 if (flag_concepts)
11519 tree reqs = get_shorthand_constraints (current_template_parms);
11520 if (dreqs)
11521 reqs = combine_constraint_expressions (reqs, dreqs);
11522 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
11525 /* We just processed one more parameter list. */
11526 ++parser->num_template_parameter_lists;
11529 /* Committee discussion supports allowing attributes here. */
11530 lambda_specs.attributes = cp_parser_attributes_opt (parser);
11532 /* The parameter-declaration-clause is optional (unless
11533 template-parameter-list was given), but must begin with an
11534 opening parenthesis if present. */
11535 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
11537 matching_parens parens;
11538 parens.consume_open (parser);
11540 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
11542 /* Parse parameters. */
11543 param_list
11544 = cp_parser_parameter_declaration_clause
11545 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL);
11547 /* Default arguments shall not be specified in the
11548 parameter-declaration-clause of a lambda-declarator. */
11549 if (pedantic && cxx_dialect < cxx14)
11550 for (tree t = param_list; t; t = TREE_CHAIN (t))
11551 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
11552 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
11553 OPT_Wc__14_extensions,
11554 "default argument specified for lambda parameter");
11556 parens.require_close (parser);
11557 has_param_list = true;
11559 else if (cxx_dialect < cxx23)
11560 omitted_parms_loc = cp_lexer_peek_token (parser->lexer)->location;
11562 /* In the decl-specifier-seq of the lambda-declarator, each
11563 decl-specifier shall either be mutable or constexpr. */
11564 int declares_class_or_enum;
11565 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
11566 cp_parser_decl_specifier_seq (parser,
11567 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
11568 &lambda_specs, &declares_class_or_enum);
11570 if (omitted_parms_loc && lambda_specs.any_specifiers_p)
11572 pedwarn (omitted_parms_loc, OPT_Wc__23_extensions,
11573 "parameter declaration before lambda declaration "
11574 "specifiers only optional with %<-std=c++2b%> or "
11575 "%<-std=gnu++2b%>");
11576 omitted_parms_loc = UNKNOWN_LOCATION;
11579 if (lambda_specs.storage_class == sc_mutable)
11581 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
11582 quals = TYPE_UNQUALIFIED;
11584 else if (lambda_specs.storage_class == sc_static)
11586 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
11587 || LAMBDA_EXPR_CAPTURE_LIST (lambda_expr))
11588 error_at (lambda_specs.locations[ds_storage_class],
11589 "%<static%> lambda specifier with lambda capture");
11590 else
11592 LAMBDA_EXPR_STATIC_P (lambda_expr) = 1;
11593 quals = TYPE_UNQUALIFIED;
11597 tx_qual = cp_parser_tx_qualifier_opt (parser);
11598 if (omitted_parms_loc && tx_qual)
11600 pedwarn (omitted_parms_loc, OPT_Wc__23_extensions,
11601 "parameter declaration before lambda transaction "
11602 "qualifier only optional with %<-std=c++2b%> or "
11603 "%<-std=gnu++2b%>");
11604 omitted_parms_loc = UNKNOWN_LOCATION;
11607 /* Parse optional exception specification. */
11608 exception_spec
11609 = cp_parser_exception_specification_opt (parser, CP_PARSER_FLAGS_NONE);
11611 if (omitted_parms_loc && exception_spec)
11613 pedwarn (omitted_parms_loc, OPT_Wc__23_extensions,
11614 "parameter declaration before lambda exception "
11615 "specification only optional with %<-std=c++2b%> or "
11616 "%<-std=gnu++2b%>");
11617 omitted_parms_loc = UNKNOWN_LOCATION;
11620 /* GCC 8 accepted attributes here, and this is the place for standard C++11
11621 attributes that appertain to the function type. */
11622 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
11623 gnu_attrs = cp_parser_gnu_attributes_opt (parser);
11624 else
11625 std_attrs = cp_parser_std_attribute_spec_seq (parser);
11627 /* Parse optional trailing return type. */
11628 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
11630 if (omitted_parms_loc)
11631 pedwarn (omitted_parms_loc, OPT_Wc__23_extensions,
11632 "parameter declaration before lambda trailing "
11633 "return type only optional with %<-std=c++2b%> or "
11634 "%<-std=gnu++2b%>");
11635 cp_lexer_consume_token (parser->lexer);
11636 return_type = cp_parser_trailing_type_id (parser);
11639 /* Also allow GNU attributes at the very end of the declaration, the usual
11640 place for GNU attributes. */
11641 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
11642 gnu_attrs = chainon (gnu_attrs, cp_parser_gnu_attributes_opt (parser));
11644 if (has_param_list)
11646 /* Parse optional trailing requires clause. */
11647 trailing_requires_clause = cp_parser_requires_clause_opt (parser, false);
11649 /* The function parameters must be in scope all the way until after the
11650 trailing-return-type in case of decltype. */
11651 pop_bindings_and_leave_scope ();
11654 /* Create the function call operator.
11656 Messing with declarators like this is no uglier than building up the
11657 FUNCTION_DECL by hand, and this is less likely to get out of sync with
11658 other code. */
11660 cp_decl_specifier_seq return_type_specs;
11661 cp_declarator* declarator;
11662 tree fco;
11663 void *p;
11665 clear_decl_specs (&return_type_specs);
11666 return_type_specs.type = make_auto ();
11668 if (lambda_specs.locations[ds_constexpr])
11670 if (cxx_dialect >= cxx17)
11671 return_type_specs.locations[ds_constexpr]
11672 = lambda_specs.locations[ds_constexpr];
11673 else
11674 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
11675 "lambda only available with %<-std=c++17%> or "
11676 "%<-std=gnu++17%>");
11678 if (lambda_specs.locations[ds_consteval])
11679 return_type_specs.locations[ds_consteval]
11680 = lambda_specs.locations[ds_consteval];
11681 if (LAMBDA_EXPR_STATIC_P (lambda_expr))
11683 return_type_specs.storage_class = sc_static;
11684 return_type_specs.locations[ds_storage_class]
11685 = lambda_specs.locations[ds_storage_class];
11688 p = obstack_alloc (&declarator_obstack, 0);
11690 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none,
11691 LAMBDA_EXPR_LOCATION (lambda_expr));
11693 declarator = make_call_declarator (declarator, param_list, quals,
11694 VIRT_SPEC_UNSPECIFIED,
11695 REF_QUAL_NONE,
11696 tx_qual,
11697 exception_spec,
11698 return_type,
11699 trailing_requires_clause,
11700 std_attrs,
11701 UNKNOWN_LOCATION);
11703 fco = grokmethod (&return_type_specs,
11704 declarator,
11705 chainon (gnu_attrs, lambda_specs.attributes));
11706 if (fco != error_mark_node)
11708 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
11709 DECL_ARTIFICIAL (fco) = 1;
11710 if (!LAMBDA_EXPR_STATIC_P (lambda_expr))
11711 /* Give the object parameter a different name. */
11712 DECL_NAME (DECL_ARGUMENTS (fco)) = closure_identifier;
11713 DECL_SET_LAMBDA_FUNCTION (fco, true);
11715 if (template_param_list)
11717 fco = finish_member_template_decl (fco);
11718 finish_template_decl (template_param_list);
11719 --parser->num_template_parameter_lists;
11721 else if (parser->fully_implicit_function_template_p)
11722 fco = finish_fully_implicit_template (parser, fco);
11724 finish_member_declaration (fco);
11725 record_lambda_scope_sig_discriminator (lambda_expr, fco);
11727 obstack_free (&declarator_obstack, p);
11729 return (fco != error_mark_node);
11733 /* Parse the body of a lambda expression, which is simply
11735 compound-statement
11737 but which requires special handling.
11738 LAMBDA_EXPR is the current representation of the lambda expression. */
11740 static void
11741 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
11743 bool nested = (current_function_decl != NULL_TREE);
11744 unsigned char local_variables_forbidden_p
11745 = parser->local_variables_forbidden_p;
11746 bool in_function_body = parser->in_function_body;
11748 /* The body of a lambda-expression is not a subexpression of the enclosing
11749 expression. */
11750 cp_evaluated ev;
11752 if (nested)
11753 push_function_context ();
11754 else
11755 /* Still increment function_depth so that we don't GC in the
11756 middle of an expression. */
11757 ++function_depth;
11759 auto odsd = make_temp_override (parser->omp_declare_simd, NULL);
11760 auto ord = make_temp_override (parser->oacc_routine, NULL);
11761 auto oafp = make_temp_override (parser->omp_attrs_forbidden_p, false);
11762 vec<tree> omp_privatization_save;
11763 save_omp_privatization_clauses (omp_privatization_save);
11764 /* Clear this in case we're in the middle of a default argument. */
11765 parser->local_variables_forbidden_p = 0;
11766 parser->in_function_body = true;
11769 local_specialization_stack s (lss_copy);
11770 tree fco = lambda_function (lambda_expr);
11771 tree body = start_lambda_function (fco, lambda_expr);
11773 /* Originally C++11 required us to peek for 'return expr'; and
11774 process it specially here to deduce the return type. N3638
11775 removed the need for that. */
11776 cp_parser_function_body (parser, false);
11778 finish_lambda_function (body);
11781 restore_omp_privatization_clauses (omp_privatization_save);
11782 parser->local_variables_forbidden_p = local_variables_forbidden_p;
11783 parser->in_function_body = in_function_body;
11784 if (nested)
11785 pop_function_context();
11786 else
11787 --function_depth;
11790 /* Statements [gram.stmt.stmt] */
11792 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
11794 static void
11795 add_debug_begin_stmt (location_t loc)
11797 if (!MAY_HAVE_DEBUG_MARKER_STMTS)
11798 return;
11799 if (DECL_DECLARED_CONCEPT_P (current_function_decl))
11800 /* A concept is never expanded normally. */
11801 return;
11803 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
11804 SET_EXPR_LOCATION (stmt, loc);
11805 add_stmt (stmt);
11808 struct cp_omp_attribute_data
11810 cp_token_cache *tokens;
11811 const c_omp_directive *dir;
11812 c_omp_directive_kind kind;
11815 /* Handle omp::directive and omp::sequence attributes in ATTRS
11816 (if any) at the start of a statement or in attribute-declaration. */
11818 static tree
11819 cp_parser_handle_statement_omp_attributes (cp_parser *parser, tree attrs)
11821 if (!flag_openmp && !flag_openmp_simd)
11822 return attrs;
11824 auto_vec<cp_omp_attribute_data, 16> vec;
11825 int cnt = 0;
11826 int tokens = 0;
11827 bool bad = false;
11828 for (tree *pa = &attrs; *pa; )
11829 if (get_attribute_namespace (*pa) == omp_identifier
11830 && is_attribute_p ("directive", get_attribute_name (*pa)))
11832 cnt++;
11833 for (tree a = TREE_VALUE (*pa); a; a = TREE_CHAIN (a))
11835 tree d = TREE_VALUE (a);
11836 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
11837 cp_token *first = DEFPARSE_TOKENS (d)->first;
11838 cp_token *last = DEFPARSE_TOKENS (d)->last;
11839 if (parser->omp_attrs_forbidden_p)
11841 error_at (first->location,
11842 "mixing OpenMP directives with attribute and pragma "
11843 "syntax on the same statement");
11844 parser->omp_attrs_forbidden_p = false;
11845 bad = true;
11847 const char *directive[3] = {};
11848 for (int i = 0; i < 3; i++)
11850 tree id = NULL_TREE;
11851 if (first + i == last)
11852 break;
11853 if (first[i].type == CPP_NAME)
11854 id = first[i].u.value;
11855 else if (first[i].type == CPP_KEYWORD)
11856 id = ridpointers[(int) first[i].keyword];
11857 else
11858 break;
11859 directive[i] = IDENTIFIER_POINTER (id);
11861 const c_omp_directive *dir = NULL;
11862 if (directive[0])
11863 dir = c_omp_categorize_directive (directive[0], directive[1],
11864 directive[2]);
11865 if (dir == NULL)
11867 error_at (first->location,
11868 "unknown OpenMP directive name in %<omp::directive%>"
11869 " attribute argument");
11870 continue;
11872 c_omp_directive_kind kind = dir->kind;
11873 if (dir->id == PRAGMA_OMP_ORDERED)
11875 /* ordered is C_OMP_DIR_CONSTRUCT only if it doesn't contain
11876 depend/doacross clause. */
11877 if (directive[1]
11878 && (strcmp (directive[1], "depend") == 0
11879 || strcmp (directive[1], "doacross") == 0))
11880 kind = C_OMP_DIR_STANDALONE;
11881 else if (first + 2 < last
11882 && first[1].type == CPP_COMMA
11883 && first[2].type == CPP_NAME
11884 && (strcmp (IDENTIFIER_POINTER (first[2].u.value),
11885 "depend") == 0
11886 || strcmp (IDENTIFIER_POINTER (first[2].u.value),
11887 "doacross") == 0))
11888 kind = C_OMP_DIR_STANDALONE;
11890 else if (dir->id == PRAGMA_OMP_ERROR)
11892 /* error with at(execution) clause is C_OMP_DIR_STANDALONE. */
11893 int paren_depth = 0;
11894 for (int i = 1; first + i < last; i++)
11895 if (first[i].type == CPP_OPEN_PAREN)
11896 paren_depth++;
11897 else if (first[i].type == CPP_CLOSE_PAREN)
11898 paren_depth--;
11899 else if (paren_depth == 0
11900 && first + i + 2 < last
11901 && first[i].type == CPP_NAME
11902 && first[i + 1].type == CPP_OPEN_PAREN
11903 && first[i + 2].type == CPP_NAME
11904 && !strcmp (IDENTIFIER_POINTER (first[i].u.value),
11905 "at")
11906 && !strcmp (IDENTIFIER_POINTER (first[i
11907 + 2].u.value),
11908 "execution"))
11910 kind = C_OMP_DIR_STANDALONE;
11911 break;
11914 cp_omp_attribute_data v = { DEFPARSE_TOKENS (d), dir, kind };
11915 vec.safe_push (v);
11916 if (flag_openmp || dir->simd)
11917 tokens += (last - first) + 1;
11919 cp_omp_attribute_data v = {};
11920 vec.safe_push (v);
11921 *pa = TREE_CHAIN (*pa);
11923 else
11924 pa = &TREE_CHAIN (*pa);
11926 if (bad)
11927 return attrs;
11929 unsigned int i;
11930 cp_omp_attribute_data *v;
11931 cp_omp_attribute_data *construct_seen = nullptr;
11932 cp_omp_attribute_data *standalone_seen = nullptr;
11933 cp_omp_attribute_data *prev_standalone_seen = nullptr;
11934 FOR_EACH_VEC_ELT (vec, i, v)
11935 if (v->tokens)
11937 if (v->kind == C_OMP_DIR_CONSTRUCT && !construct_seen)
11938 construct_seen = v;
11939 else if (v->kind == C_OMP_DIR_STANDALONE && !standalone_seen)
11940 standalone_seen = v;
11942 else
11944 if (standalone_seen && !prev_standalone_seen)
11946 prev_standalone_seen = standalone_seen;
11947 standalone_seen = nullptr;
11951 if (cnt > 1 && construct_seen)
11953 error_at (construct_seen->tokens->first->location,
11954 "OpenMP construct among %<omp::directive%> attributes"
11955 " requires all %<omp::directive%> attributes on the"
11956 " same statement to be in the same %<omp::sequence%>");
11957 return attrs;
11959 if (cnt > 1 && standalone_seen && prev_standalone_seen)
11961 error_at (standalone_seen->tokens->first->location,
11962 "multiple OpenMP standalone directives among"
11963 " %<omp::directive%> attributes must be all within the"
11964 " same %<omp::sequence%>");
11965 return attrs;
11968 if (prev_standalone_seen)
11969 standalone_seen = prev_standalone_seen;
11970 if (standalone_seen
11971 && !cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11973 error_at (standalone_seen->tokens->first->location,
11974 "standalone OpenMP directives in %<omp::directive%> attribute"
11975 " can only appear on an empty statement");
11976 return attrs;
11978 if (cnt && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
11980 cp_token *token = cp_lexer_peek_token (parser->lexer);
11981 enum pragma_kind kind = cp_parser_pragma_kind (token);
11982 if (kind >= PRAGMA_OMP__START_ && kind <= PRAGMA_OMP__LAST_)
11984 error_at (token->location,
11985 "mixing OpenMP directives with attribute and pragma "
11986 "syntax on the same statement");
11987 return attrs;
11991 if (!tokens)
11992 return attrs;
11993 tokens++;
11994 cp_lexer *lexer = cp_lexer_alloc ();
11995 lexer->debugging_p = parser->lexer->debugging_p;
11996 vec_safe_reserve (lexer->buffer, tokens, true);
11997 FOR_EACH_VEC_ELT (vec, i, v)
11999 if (!v->tokens)
12000 continue;
12001 if (!flag_openmp && !v->dir->simd)
12002 continue;
12003 cp_token *first = v->tokens->first;
12004 cp_token *last = v->tokens->last;
12005 cp_token tok = {};
12006 tok.type = CPP_PRAGMA;
12007 tok.keyword = RID_MAX;
12008 tok.u.value = build_int_cst (NULL, v->dir->id);
12009 tok.location = first->location;
12010 lexer->buffer->quick_push (tok);
12011 while (++first < last)
12012 lexer->buffer->quick_push (*first);
12013 tok = {};
12014 tok.type = CPP_PRAGMA_EOL;
12015 tok.keyword = RID_MAX;
12016 tok.location = last->location;
12017 lexer->buffer->quick_push (tok);
12019 cp_token tok = {};
12020 tok.type = CPP_EOF;
12021 tok.keyword = RID_MAX;
12022 tok.location = lexer->buffer->last ().location;
12023 lexer->buffer->quick_push (tok);
12024 lexer->next = parser->lexer;
12025 lexer->next_token = lexer->buffer->address ();
12026 lexer->last_token = lexer->next_token
12027 + lexer->buffer->length ()
12028 - 1;
12029 lexer->in_omp_attribute_pragma = true;
12030 parser->lexer = lexer;
12031 /* Move the current source position to that of the first token in the
12032 new lexer. */
12033 cp_lexer_set_source_position_from_token (lexer->next_token);
12034 return attrs;
12037 /* True if and only if the name is one of the contract types. */
12039 static bool
12040 contract_attribute_p (const_tree id)
12042 return is_attribute_p ("assert", id)
12043 || is_attribute_p ("pre", id)
12044 || is_attribute_p ("post", id);
12047 /* Handle omp::directive and omp::sequence attributes in *PATTRS
12048 (if any) at the start or after declaration-id of a declaration. */
12050 static void
12051 cp_parser_handle_directive_omp_attributes (cp_parser *parser, tree *pattrs,
12052 cp_omp_declare_simd_data *data,
12053 bool start)
12055 if (!flag_openmp && !flag_openmp_simd)
12056 return;
12058 int cnt = 0;
12059 bool bad = false;
12060 bool variant_p = false;
12061 location_t loc = UNKNOWN_LOCATION;
12062 for (tree pa = *pattrs; pa; pa = TREE_CHAIN (pa))
12063 if (get_attribute_namespace (pa) == omp_identifier
12064 && is_attribute_p ("directive", get_attribute_name (pa)))
12066 for (tree a = TREE_VALUE (pa); a; a = TREE_CHAIN (a))
12068 tree d = TREE_VALUE (a);
12069 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
12070 cp_token *first = DEFPARSE_TOKENS (d)->first;
12071 cp_token *last = DEFPARSE_TOKENS (d)->last;
12072 const char *directive[3] = {};
12073 for (int i = 0; i < 3; i++)
12075 tree id = NULL_TREE;
12076 if (first + i == last)
12077 break;
12078 if (first[i].type == CPP_NAME)
12079 id = first[i].u.value;
12080 else if (first[i].type == CPP_KEYWORD)
12081 id = ridpointers[(int) first[i].keyword];
12082 else
12083 break;
12084 directive[i] = IDENTIFIER_POINTER (id);
12086 const c_omp_directive *dir = NULL;
12087 if (directive[0])
12088 dir = c_omp_categorize_directive (directive[0], directive[1],
12089 directive[2]);
12090 if (dir == NULL)
12091 continue;
12092 if (dir->id == PRAGMA_OMP_DECLARE
12093 && (strcmp (directive[1], "simd") == 0
12094 || strcmp (directive[1], "variant") == 0))
12096 if (cnt++ == 0)
12098 variant_p = strcmp (directive[1], "variant") == 0;
12099 loc = first->location;
12101 if (start && parser->omp_declare_simd && !bad)
12103 error_at (first->location,
12104 "mixing OpenMP directives with attribute and "
12105 "pragma syntax on the same declaration");
12106 bad = true;
12112 if (bad)
12114 for (tree *pa = pattrs; *pa; )
12115 if (get_attribute_namespace (*pa) == omp_identifier
12116 && is_attribute_p ("directive", get_attribute_name (*pa)))
12117 *pa = TREE_CHAIN (*pa);
12118 else
12119 pa = &TREE_CHAIN (*pa);
12120 return;
12122 if (cnt == 0)
12123 return;
12125 if (parser->omp_declare_simd == NULL)
12127 data->error_seen = false;
12128 data->fndecl_seen = false;
12129 data->variant_p = variant_p;
12130 data->loc = loc;
12131 data->tokens = vNULL;
12132 data->attribs[0] = NULL;
12133 data->attribs[1] = NULL;
12134 parser->omp_declare_simd = data;
12136 parser->omp_declare_simd->attribs[!start] = pattrs;
12139 /* Parse a statement.
12141 statement:
12142 labeled-statement
12143 expression-statement
12144 compound-statement
12145 selection-statement
12146 iteration-statement
12147 jump-statement
12148 declaration-statement
12149 try-block
12151 C++11:
12153 statement:
12154 labeled-statement
12155 attribute-specifier-seq (opt) expression-statement
12156 attribute-specifier-seq (opt) compound-statement
12157 attribute-specifier-seq (opt) selection-statement
12158 attribute-specifier-seq (opt) iteration-statement
12159 attribute-specifier-seq (opt) jump-statement
12160 declaration-statement
12161 attribute-specifier-seq (opt) try-block
12163 init-statement:
12164 expression-statement
12165 simple-declaration
12166 alias-declaration
12168 TM Extension:
12170 statement:
12171 atomic-statement
12173 IN_COMPOUND is true when the statement is nested inside a
12174 cp_parser_compound_statement.
12176 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12177 is a (possibly labeled) if statement which is not enclosed in braces
12178 and has an else clause. This is used to implement -Wparentheses.
12180 CHAIN is a vector of if-else-if conditions.
12182 Note that this version of parsing restricts assertions to be attached to
12183 empty statements. */
12185 static void
12186 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
12187 const bool in_compound, bool *if_p, vec<tree> *chain,
12188 location_t *loc_after_labels)
12190 tree statement, std_attrs = NULL_TREE;
12191 cp_token *token;
12192 location_t statement_location, attrs_loc;
12193 bool in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
12194 bool has_std_attrs;
12195 /* A copy of IN_COMPOUND which is set to false after seeing a label.
12196 This matters for certain pragmas. */
12197 bool in_compound_for_pragma = in_compound;
12199 restart:
12200 if (if_p != NULL)
12201 *if_p = false;
12202 /* There is no statement yet. */
12203 statement = NULL_TREE;
12205 saved_token_sentinel saved_tokens (parser->lexer);
12206 token = cp_lexer_peek_token (parser->lexer);
12207 attrs_loc = token->location;
12208 if (c_dialect_objc ())
12209 /* In obj-c++, seeing '[[' might be the either the beginning of
12210 c++11 attributes, or a nested objc-message-expression. So
12211 let's parse the c++11 attributes tentatively. */
12212 cp_parser_parse_tentatively (parser);
12213 std_attrs = cp_parser_std_attribute_spec_seq (parser);
12214 if (std_attrs)
12215 attrs_loc = make_location (attrs_loc, attrs_loc, parser->lexer);
12216 if (c_dialect_objc ())
12218 if (!cp_parser_parse_definitely (parser))
12219 std_attrs = NULL_TREE;
12221 has_std_attrs = cp_lexer_peek_token (parser->lexer) != token;
12223 /* Peek at the next token. */
12224 token = cp_lexer_peek_token (parser->lexer);
12226 /* If we have contracts, check that they're valid in this context. */
12227 if (std_attrs != error_mark_node)
12229 if (tree pre = lookup_attribute ("pre", std_attrs))
12230 error_at (EXPR_LOCATION (TREE_VALUE (pre)),
12231 "preconditions cannot be statements");
12232 else if (tree post = lookup_attribute ("post", std_attrs))
12233 error_at (EXPR_LOCATION (TREE_VALUE (post)),
12234 "postconditions cannot be statements");
12236 /* Check that assertions are null statements. */
12237 if (cp_contract_assertion_p (std_attrs))
12238 if (token->type != CPP_SEMICOLON)
12239 error_at (token->location, "assertions must be followed by %<;%>");
12242 bool omp_attrs_forbidden_p;
12243 omp_attrs_forbidden_p = parser->omp_attrs_forbidden_p;
12245 if (std_attrs && (flag_openmp || flag_openmp_simd))
12247 bool handle_omp_attribs = false;
12248 if (token->type == CPP_KEYWORD)
12249 switch (token->keyword)
12251 case RID_IF:
12252 case RID_SWITCH:
12253 case RID_WHILE:
12254 case RID_DO:
12255 case RID_FOR:
12256 case RID_BREAK:
12257 case RID_CONTINUE:
12258 case RID_RETURN:
12259 case RID_CO_RETURN:
12260 case RID_GOTO:
12261 case RID_AT_TRY:
12262 case RID_AT_CATCH:
12263 case RID_AT_FINALLY:
12264 case RID_AT_SYNCHRONIZED:
12265 case RID_AT_THROW:
12266 case RID_TRY:
12267 case RID_TRANSACTION_ATOMIC:
12268 case RID_TRANSACTION_RELAXED:
12269 case RID_SYNCHRONIZED:
12270 case RID_ATOMIC_NOEXCEPT:
12271 case RID_ATOMIC_CANCEL:
12272 case RID_TRANSACTION_CANCEL:
12273 handle_omp_attribs = true;
12274 break;
12275 default:
12276 break;
12278 else if (token->type == CPP_SEMICOLON
12279 || token->type == CPP_OPEN_BRACE
12280 || token->type == CPP_PRAGMA)
12281 handle_omp_attribs = true;
12282 if (handle_omp_attribs)
12284 std_attrs = cp_parser_handle_statement_omp_attributes (parser,
12285 std_attrs);
12286 token = cp_lexer_peek_token (parser->lexer);
12289 parser->omp_attrs_forbidden_p = false;
12291 /* Remember the location of the first token in the statement. */
12292 cp_token *statement_token = token;
12293 statement_location = token->location;
12294 add_debug_begin_stmt (statement_location);
12295 /* If this is a keyword, then that will often determine what kind of
12296 statement we have. */
12297 if (token->type == CPP_KEYWORD)
12299 enum rid keyword = token->keyword;
12301 switch (keyword)
12303 case RID_CASE:
12304 case RID_DEFAULT:
12305 /* Looks like a labeled-statement with a case label.
12306 Parse the label, and then use tail recursion to parse
12307 the statement. */
12308 cp_parser_label_for_labeled_statement (parser, std_attrs);
12309 in_compound_for_pragma = false;
12310 in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
12311 goto restart;
12313 case RID_IF:
12314 case RID_SWITCH:
12315 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12316 statement = cp_parser_selection_statement (parser, if_p, chain);
12317 break;
12319 case RID_WHILE:
12320 case RID_DO:
12321 case RID_FOR:
12322 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12323 statement = cp_parser_iteration_statement (parser, if_p, false, 0);
12324 break;
12326 case RID_BREAK:
12327 case RID_CONTINUE:
12328 case RID_RETURN:
12329 case RID_CO_RETURN:
12330 case RID_GOTO:
12331 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12332 statement = cp_parser_jump_statement (parser);
12333 break;
12335 /* Objective-C++ exception-handling constructs. */
12336 case RID_AT_TRY:
12337 case RID_AT_CATCH:
12338 case RID_AT_FINALLY:
12339 case RID_AT_SYNCHRONIZED:
12340 case RID_AT_THROW:
12341 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12342 statement = cp_parser_objc_statement (parser);
12343 break;
12345 case RID_TRY:
12346 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12347 statement = cp_parser_try_block (parser);
12348 break;
12350 case RID_NAMESPACE:
12351 /* This must be a namespace alias definition. */
12352 if (has_std_attrs)
12354 /* Attributes should be parsed as part of the
12355 declaration, so let's un-parse them. */
12356 saved_tokens.rollback();
12357 std_attrs = NULL_TREE;
12359 cp_parser_declaration_statement (parser);
12360 return;
12362 case RID_TRANSACTION_ATOMIC:
12363 case RID_TRANSACTION_RELAXED:
12364 case RID_SYNCHRONIZED:
12365 case RID_ATOMIC_NOEXCEPT:
12366 case RID_ATOMIC_CANCEL:
12367 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12368 statement = cp_parser_transaction (parser, token);
12369 break;
12370 case RID_TRANSACTION_CANCEL:
12371 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12372 statement = cp_parser_transaction_cancel (parser);
12373 break;
12375 default:
12376 /* It might be a keyword like `int' that can start a
12377 declaration-statement. */
12378 break;
12381 else if (token->type == CPP_NAME)
12383 /* If the next token is a `:', then we are looking at a
12384 labeled-statement. */
12385 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12386 if (token->type == CPP_COLON)
12388 /* Looks like a labeled-statement with an ordinary label.
12389 Parse the label, and then use tail recursion to parse
12390 the statement. */
12392 cp_parser_label_for_labeled_statement (parser, std_attrs);
12394 /* If there's no statement, it's not a labeled-statement, just
12395 a label. That's allowed in C++23, but only if we're at the
12396 end of a compound-statement. */
12397 if (in_compound
12398 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12400 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
12401 if (cxx_dialect < cxx23)
12402 pedwarn (loc, OPT_Wc__23_extensions,
12403 "label at end of compound statement only available "
12404 "with %<-std=c++2b%> or %<-std=gnu++2b%>");
12405 return;
12407 in_compound_for_pragma = false;
12408 in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
12409 goto restart;
12412 /* Anything that starts with a `{' must be a compound-statement. */
12413 else if (token->type == CPP_OPEN_BRACE)
12415 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12416 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12418 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
12419 a statement all its own. */
12420 else if (token->type == CPP_PRAGMA)
12422 do_pragma:;
12423 cp_lexer *lexer = parser->lexer;
12424 bool do_restart = false;
12425 /* Only certain OpenMP pragmas are attached to statements, and thus
12426 are considered statements themselves. All others are not. In
12427 the context of a compound, accept the pragma as a "statement" and
12428 return so that we can check for a close brace. Otherwise we
12429 require a real statement and must go back and read one. */
12430 if (in_compound_for_pragma)
12431 cp_parser_pragma (parser, pragma_compound, if_p);
12432 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
12433 do_restart = true;
12434 if (parser->lexer != lexer
12435 && lexer->in_omp_attribute_pragma
12436 && (!in_omp_attribute_pragma || lexer->orphan_p))
12438 if (saved_tokens.lexer == lexer)
12440 if (saved_tokens.mode == STS_COMMIT)
12441 cp_lexer_commit_tokens (lexer);
12442 gcc_assert (lexer->saved_tokens.length () == saved_tokens.len);
12443 saved_tokens.lexer = parser->lexer;
12444 saved_tokens.mode = STS_DONOTHING;
12445 saved_tokens.len = parser->lexer->saved_tokens.length ();
12447 cp_lexer_destroy (lexer);
12448 lexer = parser->lexer;
12450 if (do_restart)
12451 goto restart;
12452 if (parser->lexer == lexer
12453 && lexer->in_omp_attribute_pragma
12454 && !in_omp_attribute_pragma)
12455 parser->lexer->orphan_p = true;
12456 return;
12458 else if (token->type == CPP_EOF)
12460 cp_parser_error (parser, "expected statement");
12461 return;
12464 /* Everything else must be a declaration-statement or an
12465 expression-statement. Try for the declaration-statement
12466 first, unless we are looking at a `;', in which case we know that
12467 we have an expression-statement. */
12468 if (!statement)
12470 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12472 if (has_std_attrs)
12473 /* Attributes should be parsed as part of the declaration,
12474 so let's un-parse them. */
12475 saved_tokens.rollback();
12477 parser->omp_attrs_forbidden_p = omp_attrs_forbidden_p;
12478 cp_parser_parse_tentatively (parser);
12479 /* Try to parse the declaration-statement. */
12480 cp_parser_declaration_statement (parser);
12481 parser->omp_attrs_forbidden_p = false;
12482 /* If that worked, we're done. */
12483 if (cp_parser_parse_definitely (parser))
12484 return;
12485 /* It didn't work, restore the post-attribute position. */
12486 if (has_std_attrs)
12488 cp_lexer_set_token_position (parser->lexer, statement_token);
12489 if (flag_openmp || flag_openmp_simd)
12491 size_t i = 1;
12492 bool handle_omp_attribs = true;
12493 while (cp_lexer_peek_nth_token (parser->lexer, i)->keyword
12494 == RID_EXTENSION)
12495 i++;
12496 switch (cp_lexer_peek_nth_token (parser->lexer, i)->keyword)
12498 case RID_ASM:
12499 case RID_NAMESPACE:
12500 case RID_USING:
12501 case RID_LABEL:
12502 case RID_STATIC_ASSERT:
12503 /* Don't handle OpenMP attribs on keywords that
12504 always start a declaration statement but don't
12505 accept attribute before it and therefore
12506 the tentative cp_parser_declaration_statement
12507 fails to parse because of that. */
12508 handle_omp_attribs = false;
12509 break;
12510 default:
12511 break;
12514 if (handle_omp_attribs)
12516 parser->omp_attrs_forbidden_p = omp_attrs_forbidden_p;
12517 std_attrs
12518 = cp_parser_handle_statement_omp_attributes
12519 (parser, std_attrs);
12520 parser->omp_attrs_forbidden_p = false;
12521 token = cp_lexer_peek_token (parser->lexer);
12522 if (token->type == CPP_PRAGMA)
12523 goto do_pragma;
12528 /* All preceding labels have been parsed at this point. */
12529 if (loc_after_labels != NULL)
12530 *loc_after_labels = statement_location;
12532 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12534 /* Look for an expression-statement instead. */
12535 statement = cp_parser_expression_statement (parser, in_statement_expr);
12537 std_attrs = process_stmt_assume_attribute (std_attrs, statement,
12538 attrs_loc);
12540 /* Handle [[fallthrough]];. */
12541 if (attribute_fallthrough_p (std_attrs))
12543 /* The next token after the fallthrough attribute is ';'. */
12544 if (statement == NULL_TREE)
12546 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
12547 statement = build_call_expr_internal_loc (statement_location,
12548 IFN_FALLTHROUGH,
12549 void_type_node, 0);
12550 finish_expr_stmt (statement);
12552 else
12553 warning_at (statement_location, OPT_Wattributes,
12554 "%<fallthrough%> attribute not followed by %<;%>");
12555 std_attrs = NULL_TREE;
12558 /* Handle [[assert: ...]]; */
12559 if (cp_contract_assertion_p (std_attrs))
12561 /* Add the assertion as a statement in the current block. */
12562 gcc_assert (!statement || statement == error_mark_node);
12563 emit_assertion (std_attrs);
12564 std_attrs = NULL_TREE;
12568 /* Set the line number for the statement. */
12569 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
12570 SET_EXPR_LOCATION (statement, statement_location);
12572 /* Allow "[[fallthrough]];" or "[[assume(cond)]];", but warn otherwise. */
12573 if (std_attrs != NULL_TREE)
12574 warning_at (attrs_loc,
12575 OPT_Wattributes,
12576 "attributes at the beginning of statement are ignored");
12579 /* Append ATTR to attribute list ATTRS. */
12581 tree
12582 attr_chainon (tree attrs, tree attr)
12584 if (attrs == error_mark_node)
12585 return error_mark_node;
12586 if (attr == error_mark_node)
12587 return error_mark_node;
12588 return chainon (attrs, attr);
12591 /* Parse the label for a labeled-statement, i.e.
12593 label:
12594 attribute-specifier-seq[opt] identifier :
12595 attribute-specifier-seq[opt] case constant-expression :
12596 attribute-specifier-seq[opt] default :
12598 labeled-statement:
12599 label statement
12601 GNU Extension:
12602 case constant-expression ... constant-expression : statement
12604 When a label is parsed without errors, the label is added to the
12605 parse tree by the finish_* functions, so this function doesn't
12606 have to return the label. */
12608 static void
12609 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
12611 cp_token *token;
12612 tree label = NULL_TREE;
12613 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12615 /* The next token should be an identifier. */
12616 token = cp_lexer_peek_token (parser->lexer);
12617 if (token->type != CPP_NAME
12618 && token->type != CPP_KEYWORD)
12620 cp_parser_error (parser, "expected labeled-statement");
12621 return;
12624 /* Remember whether this case or a user-defined label is allowed to fall
12625 through to. */
12626 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
12628 parser->colon_corrects_to_scope_p = false;
12629 switch (token->keyword)
12631 case RID_CASE:
12633 tree expr, expr_hi;
12634 cp_token *ellipsis;
12636 /* Consume the `case' token. */
12637 cp_lexer_consume_token (parser->lexer);
12638 /* Parse the constant-expression. */
12639 expr = cp_parser_constant_expression (parser);
12640 if (check_for_bare_parameter_packs (expr))
12641 expr = error_mark_node;
12643 ellipsis = cp_lexer_peek_token (parser->lexer);
12644 if (ellipsis->type == CPP_ELLIPSIS)
12646 /* Consume the `...' token. */
12647 cp_lexer_consume_token (parser->lexer);
12648 expr_hi = cp_parser_constant_expression (parser);
12649 if (check_for_bare_parameter_packs (expr_hi))
12650 expr_hi = error_mark_node;
12652 /* We don't need to emit warnings here, as the common code
12653 will do this for us. */
12655 else
12656 expr_hi = NULL_TREE;
12658 if (parser->in_switch_statement_p)
12660 tree l = finish_case_label (token->location, expr, expr_hi);
12661 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
12663 label = CASE_LABEL (l);
12664 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
12667 else
12668 error_at (token->location,
12669 "case label %qE not within a switch statement",
12670 expr);
12672 break;
12674 case RID_DEFAULT:
12675 /* Consume the `default' token. */
12676 cp_lexer_consume_token (parser->lexer);
12678 if (parser->in_switch_statement_p)
12680 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
12681 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
12683 label = CASE_LABEL (l);
12684 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
12687 else
12688 error_at (token->location, "case label not within a switch statement");
12689 break;
12691 default:
12692 /* Anything else must be an ordinary label. */
12693 label = finish_label_stmt (cp_parser_identifier (parser));
12694 if (label && TREE_CODE (label) == LABEL_DECL)
12695 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
12696 break;
12699 /* Require the `:' token. */
12700 cp_parser_require (parser, CPP_COLON, RT_COLON);
12702 /* An ordinary label may optionally be followed by attributes.
12703 However, this is only permitted if the attributes are then
12704 followed by a semicolon. This is because, for backward
12705 compatibility, when parsing
12706 lab: __attribute__ ((unused)) int i;
12707 we want the attribute to attach to "i", not "lab". */
12708 if (label != NULL_TREE
12709 && cp_next_tokens_can_be_gnu_attribute_p (parser))
12711 tree attrs;
12712 cp_parser_parse_tentatively (parser);
12713 attrs = cp_parser_gnu_attributes_opt (parser);
12714 if (attrs == NULL_TREE
12715 /* And fallthrough always binds to the expression-statement. */
12716 || attribute_fallthrough_p (attrs)
12717 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12718 cp_parser_abort_tentative_parse (parser);
12719 else if (!cp_parser_parse_definitely (parser))
12721 else
12722 attributes = attr_chainon (attributes, attrs);
12725 if (attributes != NULL_TREE)
12726 cplus_decl_attributes (&label, attributes, 0);
12728 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12731 /* Parse an expression-statement.
12733 expression-statement:
12734 expression [opt] ;
12736 Returns the new EXPR_STMT -- or NULL_TREE if the expression
12737 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
12738 indicates whether this expression-statement is part of an
12739 expression statement. */
12741 static tree
12742 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
12744 tree statement = NULL_TREE;
12745 cp_token *token = cp_lexer_peek_token (parser->lexer);
12746 location_t loc = token->location;
12748 /* There might be attribute fallthrough. */
12749 tree attr = cp_parser_gnu_attributes_opt (parser);
12751 /* If the next token is a ';', then there is no expression
12752 statement. */
12753 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12755 statement = cp_parser_expression (parser);
12756 if (statement == error_mark_node
12757 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
12759 cp_parser_skip_to_end_of_block_or_statement (parser);
12760 return error_mark_node;
12764 attr = process_stmt_assume_attribute (attr, statement, loc);
12766 /* Handle [[fallthrough]];. */
12767 if (attribute_fallthrough_p (attr))
12769 /* The next token after the fallthrough attribute is ';'. */
12770 if (statement == NULL_TREE)
12771 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
12772 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
12773 void_type_node, 0);
12774 else
12775 warning_at (loc, OPT_Wattributes,
12776 "%<fallthrough%> attribute not followed by %<;%>");
12777 attr = NULL_TREE;
12780 /* Allow "[[fallthrough]];", but warn otherwise. */
12781 if (attr != NULL_TREE)
12782 warning_at (loc, OPT_Wattributes,
12783 "attributes at the beginning of statement are ignored");
12785 /* Give a helpful message for "A<T>::type t;" and the like. */
12786 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
12787 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
12789 if (TREE_CODE (statement) == SCOPE_REF)
12790 error_at (token->location, "need %<typename%> before %qE because "
12791 "%qT is a dependent scope",
12792 statement, TREE_OPERAND (statement, 0));
12793 else if (is_overloaded_fn (statement)
12794 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
12796 /* A::A a; */
12797 tree fn = get_first_fn (statement);
12798 error_at (token->location,
12799 "%<%T::%D%> names the constructor, not the type",
12800 DECL_CONTEXT (fn), DECL_NAME (fn));
12804 /* Consume the final `;'. */
12805 cp_parser_consume_semicolon_at_end_of_statement (parser);
12807 if (in_statement_expr
12808 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12809 /* This is the final expression statement of a statement
12810 expression. */
12811 statement = finish_stmt_expr_expr (statement, in_statement_expr);
12812 else if (statement)
12813 statement = finish_expr_stmt (statement);
12815 return statement;
12818 /* Parse a compound-statement.
12820 compound-statement:
12821 { statement-seq [opt] label-seq [opt] }
12823 label-seq:
12824 label
12825 label-seq label
12827 GNU extension:
12829 compound-statement:
12830 { label-declaration-seq [opt] statement-seq [opt] }
12832 label-declaration-seq:
12833 label-declaration
12834 label-declaration-seq label-declaration
12836 Returns a tree representing the statement. */
12838 static tree
12839 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
12840 int bcs_flags, bool function_body)
12842 tree compound_stmt;
12843 matching_braces braces;
12845 /* Consume the `{'. */
12846 if (!braces.require_open (parser))
12847 return error_mark_node;
12848 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
12849 && !function_body && cxx_dialect < cxx14)
12850 pedwarn (input_location, OPT_Wpedantic,
12851 "compound-statement in %<constexpr%> function");
12852 /* Begin the compound-statement. */
12853 compound_stmt = begin_compound_stmt (bcs_flags);
12854 /* If the next keyword is `__label__' we have a label declaration. */
12855 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12856 cp_parser_label_declaration (parser);
12857 /* Parse an (optional) statement-seq. */
12858 cp_parser_statement_seq_opt (parser, in_statement_expr);
12860 /* Consume the `}'. */
12861 braces.require_close (parser);
12863 /* Finish the compound-statement. */
12864 finish_compound_stmt (compound_stmt);
12866 return compound_stmt;
12869 /* Parse an (optional) statement-seq.
12871 statement-seq:
12872 statement
12873 statement-seq [opt] statement */
12875 static void
12876 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
12878 /* Scan statements until there aren't any more. */
12879 while (true)
12881 cp_token *token = cp_lexer_peek_token (parser->lexer);
12883 /* If we are looking at a `}', then we have run out of
12884 statements; the same is true if we have reached the end
12885 of file, or have stumbled upon a stray '@end'. */
12886 if (token->type == CPP_CLOSE_BRACE
12887 || token->type == CPP_EOF
12888 || token->type == CPP_PRAGMA_EOL
12889 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
12890 break;
12892 /* If we are in a compound statement and find 'else' then
12893 something went wrong. */
12894 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
12896 if (parser->in_statement & IN_IF_STMT)
12897 break;
12898 else
12900 token = cp_lexer_consume_token (parser->lexer);
12901 error_at (token->location, "%<else%> without a previous %<if%>");
12905 /* Parse the statement. */
12906 cp_parser_statement (parser, in_statement_expr, true, NULL);
12910 /* Return true if this is the C++20 version of range-based-for with
12911 init-statement. */
12913 static bool
12914 cp_parser_range_based_for_with_init_p (cp_parser *parser)
12916 bool r = false;
12918 /* Save tokens so that we can put them back. */
12919 cp_lexer_save_tokens (parser->lexer);
12921 /* There has to be an unnested ; followed by an unnested :. */
12922 if (cp_parser_skip_to_closing_parenthesis_1 (parser,
12923 /*recovering=*/false,
12924 CPP_SEMICOLON,
12925 /*consume_paren=*/false) != -1)
12926 goto out;
12928 /* We found the semicolon, eat it now. */
12929 cp_lexer_consume_token (parser->lexer);
12931 /* Now look for ':' that is not nested in () or {}. */
12932 r = (cp_parser_skip_to_closing_parenthesis_1 (parser,
12933 /*recovering=*/false,
12934 CPP_COLON,
12935 /*consume_paren=*/false) == -1);
12937 out:
12938 /* Roll back the tokens we skipped. */
12939 cp_lexer_rollback_tokens (parser->lexer);
12941 return r;
12944 /* Return true if we're looking at (init; cond), false otherwise. */
12946 static bool
12947 cp_parser_init_statement_p (cp_parser *parser)
12949 /* Save tokens so that we can put them back. */
12950 cp_lexer_save_tokens (parser->lexer);
12952 /* Look for ';' that is not nested in () or {}. */
12953 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
12954 /*recovering=*/false,
12955 CPP_SEMICOLON,
12956 /*consume_paren=*/false);
12958 /* Roll back the tokens we skipped. */
12959 cp_lexer_rollback_tokens (parser->lexer);
12961 return ret == -1;
12964 /* Parse a selection-statement.
12966 selection-statement:
12967 if ( init-statement [opt] condition ) statement
12968 if ( init-statement [opt] condition ) statement else statement
12969 switch ( init-statement [opt] condition ) statement
12971 Returns the new IF_STMT or SWITCH_STMT.
12973 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12974 is a (possibly labeled) if statement which is not enclosed in
12975 braces and has an else clause. This is used to implement
12976 -Wparentheses.
12978 CHAIN is a vector of if-else-if conditions. This is used to implement
12979 -Wduplicated-cond. */
12981 static tree
12982 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
12983 vec<tree> *chain)
12985 cp_token *token;
12986 enum rid keyword;
12987 token_indent_info guard_tinfo;
12989 if (if_p != NULL)
12990 *if_p = false;
12992 /* Peek at the next token. */
12993 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
12994 guard_tinfo = get_token_indent_info (token);
12996 /* See what kind of keyword it is. */
12997 keyword = token->keyword;
12998 switch (keyword)
13000 case RID_IF:
13001 case RID_SWITCH:
13003 tree statement;
13004 tree condition;
13006 bool cx = false;
13007 if (keyword == RID_IF
13008 && cp_lexer_next_token_is_keyword (parser->lexer,
13009 RID_CONSTEXPR))
13011 cx = true;
13012 cp_token *tok = cp_lexer_consume_token (parser->lexer);
13013 if (cxx_dialect < cxx17)
13014 pedwarn (tok->location, OPT_Wc__17_extensions,
13015 "%<if constexpr%> only available with "
13016 "%<-std=c++17%> or %<-std=gnu++17%>");
13018 int ce = 0;
13019 if (keyword == RID_IF && !cx)
13021 if (cp_lexer_next_token_is_keyword (parser->lexer,
13022 RID_CONSTEVAL))
13023 ce = 1;
13024 else if (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
13025 && cp_lexer_nth_token_is_keyword (parser->lexer, 2,
13026 RID_CONSTEVAL))
13028 ce = -1;
13029 cp_lexer_consume_token (parser->lexer);
13032 if (ce)
13034 cp_token *tok = cp_lexer_consume_token (parser->lexer);
13035 if (cxx_dialect < cxx23)
13036 pedwarn (tok->location, OPT_Wc__23_extensions,
13037 "%<if consteval%> only available with "
13038 "%<-std=c++2b%> or %<-std=gnu++2b%>");
13040 bool save_in_consteval_if_p = in_consteval_if_p;
13041 statement = begin_if_stmt ();
13042 IF_STMT_CONSTEVAL_P (statement) = true;
13043 condition = finish_if_stmt_cond (boolean_false_node, statement);
13045 gcc_rich_location richloc (tok->location);
13046 bool non_compound_stmt_p = false;
13047 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13049 non_compound_stmt_p = true;
13050 richloc.add_fixit_insert_after (tok->location, "{");
13053 in_consteval_if_p |= ce > 0;
13054 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
13056 if (non_compound_stmt_p)
13058 location_t before_loc
13059 = cp_lexer_peek_token (parser->lexer)->location;
13060 richloc.add_fixit_insert_before (before_loc, "}");
13061 error_at (&richloc,
13062 "%<if consteval%> requires compound statement");
13063 non_compound_stmt_p = false;
13066 finish_then_clause (statement);
13068 /* If the next token is `else', parse the else-clause. */
13069 if (cp_lexer_next_token_is_keyword (parser->lexer,
13070 RID_ELSE))
13072 cp_token *else_tok = cp_lexer_peek_token (parser->lexer);
13073 gcc_rich_location else_richloc (else_tok->location);
13074 guard_tinfo = get_token_indent_info (else_tok);
13075 /* Consume the `else' keyword. */
13076 cp_lexer_consume_token (parser->lexer);
13078 begin_else_clause (statement);
13080 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13082 non_compound_stmt_p = true;
13083 else_richloc.add_fixit_insert_after (else_tok->location,
13084 "{");
13087 in_consteval_if_p = save_in_consteval_if_p | (ce < 0);
13088 cp_parser_implicitly_scoped_statement (parser, NULL,
13089 guard_tinfo);
13091 if (non_compound_stmt_p)
13093 location_t before_loc
13094 = cp_lexer_peek_token (parser->lexer)->location;
13095 else_richloc.add_fixit_insert_before (before_loc, "}");
13096 error_at (&else_richloc,
13097 "%<if consteval%> requires compound statement");
13100 finish_else_clause (statement);
13103 in_consteval_if_p = save_in_consteval_if_p;
13104 if (ce < 0)
13106 std::swap (THEN_CLAUSE (statement), ELSE_CLAUSE (statement));
13107 if (THEN_CLAUSE (statement) == NULL_TREE)
13108 THEN_CLAUSE (statement) = build_empty_stmt (tok->location);
13111 finish_if_stmt (statement);
13112 return statement;
13115 /* Look for the `('. */
13116 matching_parens parens;
13117 if (!parens.require_open (parser))
13119 cp_parser_skip_to_end_of_statement (parser);
13120 return error_mark_node;
13123 /* Begin the selection-statement. */
13124 if (keyword == RID_IF)
13126 statement = begin_if_stmt ();
13127 IF_STMT_CONSTEXPR_P (statement) = cx;
13129 else
13130 statement = begin_switch_stmt ();
13132 /* Parse the optional init-statement. */
13133 if (cp_parser_init_statement_p (parser))
13135 tree decl;
13136 if (cxx_dialect < cxx17)
13137 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
13138 OPT_Wc__17_extensions,
13139 "init-statement in selection statements only available "
13140 "with %<-std=c++17%> or %<-std=gnu++17%>");
13141 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13142 /* A non-empty init-statement can have arbitrary side
13143 effects. */
13144 vec_free (chain);
13145 cp_parser_init_statement (parser, &decl);
13148 /* Parse the condition. */
13149 condition = cp_parser_condition (parser);
13150 /* Look for the `)'. */
13151 if (!parens.require_close (parser))
13152 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13153 /*consume_paren=*/true);
13155 if (keyword == RID_IF)
13157 bool nested_if;
13158 unsigned char in_statement;
13160 /* Add the condition. */
13161 condition = finish_if_stmt_cond (condition, statement);
13163 if (warn_duplicated_cond)
13164 warn_duplicated_cond_add_or_warn (token->location, condition,
13165 &chain);
13167 /* Parse the then-clause. */
13168 in_statement = parser->in_statement;
13169 parser->in_statement |= IN_IF_STMT;
13171 /* Outside a template, the non-selected branch of a constexpr
13172 if is a 'discarded statement', i.e. unevaluated. */
13173 bool was_discarded = in_discarded_stmt;
13174 bool discard_then = (cx && !processing_template_decl
13175 && integer_zerop (condition));
13176 if (discard_then)
13178 in_discarded_stmt = true;
13179 ++c_inhibit_evaluation_warnings;
13182 cp_parser_implicitly_scoped_statement (parser, &nested_if,
13183 guard_tinfo);
13185 parser->in_statement = in_statement;
13187 finish_then_clause (statement);
13189 if (discard_then)
13191 THEN_CLAUSE (statement) = NULL_TREE;
13192 in_discarded_stmt = was_discarded;
13193 --c_inhibit_evaluation_warnings;
13196 /* If the next token is `else', parse the else-clause. */
13197 if (cp_lexer_next_token_is_keyword (parser->lexer,
13198 RID_ELSE))
13200 bool discard_else = (cx && !processing_template_decl
13201 && integer_nonzerop (condition));
13202 if (discard_else)
13204 in_discarded_stmt = true;
13205 ++c_inhibit_evaluation_warnings;
13208 guard_tinfo
13209 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13210 /* Consume the `else' keyword. */
13211 cp_lexer_consume_token (parser->lexer);
13212 if (warn_duplicated_cond)
13214 if (cp_lexer_next_token_is_keyword (parser->lexer,
13215 RID_IF)
13216 && chain == NULL)
13218 /* We've got "if (COND) else if (COND2)". Start
13219 the condition chain and add COND as the first
13220 element. */
13221 chain = new vec<tree> ();
13222 if (!CONSTANT_CLASS_P (condition)
13223 && !TREE_SIDE_EFFECTS (condition))
13225 /* Wrap it in a NOP_EXPR so that we can set the
13226 location of the condition. */
13227 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
13228 condition);
13229 SET_EXPR_LOCATION (e, token->location);
13230 chain->safe_push (e);
13233 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
13234 RID_IF))
13235 /* This is if-else without subsequent if. Zap the
13236 condition chain; we would have already warned at
13237 this point. */
13238 vec_free (chain);
13240 begin_else_clause (statement);
13241 /* Parse the else-clause. */
13242 cp_parser_implicitly_scoped_statement (parser, NULL,
13243 guard_tinfo, chain);
13245 finish_else_clause (statement);
13247 /* If we are currently parsing a then-clause, then
13248 IF_P will not be NULL. We set it to true to
13249 indicate that this if statement has an else clause.
13250 This may trigger the Wparentheses warning below
13251 when we get back up to the parent if statement. */
13252 if (if_p != NULL)
13253 *if_p = true;
13255 if (discard_else)
13257 ELSE_CLAUSE (statement) = NULL_TREE;
13258 in_discarded_stmt = was_discarded;
13259 --c_inhibit_evaluation_warnings;
13262 else
13264 /* This if statement does not have an else clause. If
13265 NESTED_IF is true, then the then-clause has an if
13266 statement which does have an else clause. We warn
13267 about the potential ambiguity. */
13268 if (nested_if)
13269 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
13270 "suggest explicit braces to avoid ambiguous"
13271 " %<else%>");
13272 if (warn_duplicated_cond)
13273 /* We don't need the condition chain anymore. */
13274 vec_free (chain);
13277 /* Now we're all done with the if-statement. */
13278 finish_if_stmt (statement);
13280 else
13282 bool in_switch_statement_p;
13283 unsigned char in_statement;
13285 /* Add the condition. */
13286 finish_switch_cond (condition, statement);
13288 /* Parse the body of the switch-statement. */
13289 in_switch_statement_p = parser->in_switch_statement_p;
13290 in_statement = parser->in_statement;
13291 parser->in_switch_statement_p = true;
13292 parser->in_statement |= IN_SWITCH_STMT;
13293 cp_parser_implicitly_scoped_statement (parser, if_p,
13294 guard_tinfo);
13295 parser->in_switch_statement_p = in_switch_statement_p;
13296 parser->in_statement = in_statement;
13298 /* Now we're all done with the switch-statement. */
13299 finish_switch_stmt (statement);
13302 return statement;
13304 break;
13306 default:
13307 cp_parser_error (parser, "expected selection-statement");
13308 return error_mark_node;
13312 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
13313 If we have seen at least one decl-specifier, and the next token is not
13314 a parenthesis (after "int (" we might be looking at a functional cast)
13315 neither we are dealing with a concept-check expression then we must be
13316 looking at a declaration. */
13318 static void
13319 cp_parser_maybe_commit_to_declaration (cp_parser* parser,
13320 cp_decl_specifier_seq *decl_specs)
13322 if (decl_specs->any_specifiers_p
13323 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
13324 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
13325 && !cp_parser_error_occurred (parser)
13326 && !(decl_specs->type
13327 && TREE_CODE (decl_specs->type) == TYPE_DECL
13328 && is_constrained_auto (TREE_TYPE (decl_specs->type))))
13329 cp_parser_commit_to_tentative_parse (parser);
13332 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
13333 The declarator shall not specify a function or an array. Returns
13334 TRUE if the declarator is valid, FALSE otherwise. */
13336 static bool
13337 cp_parser_check_condition_declarator (cp_parser* parser,
13338 cp_declarator *declarator,
13339 location_t loc)
13341 if (declarator == cp_error_declarator
13342 || function_declarator_p (declarator)
13343 || declarator->kind == cdk_array)
13345 if (declarator == cp_error_declarator)
13346 /* Already complained. */;
13347 else if (declarator->kind == cdk_array)
13348 error_at (loc, "condition declares an array");
13349 else
13350 error_at (loc, "condition declares a function");
13351 if (parser->fully_implicit_function_template_p)
13352 abort_fully_implicit_template (parser);
13353 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
13354 /*or_comma=*/false,
13355 /*consume_paren=*/false);
13356 return false;
13358 else
13359 return true;
13362 /* Parse a condition.
13364 condition:
13365 expression
13366 type-specifier-seq declarator = initializer-clause
13367 type-specifier-seq declarator braced-init-list
13369 GNU Extension:
13371 condition:
13372 type-specifier-seq declarator asm-specification [opt]
13373 attributes [opt] = assignment-expression
13375 Returns the expression that should be tested. */
13377 static tree
13378 cp_parser_condition (cp_parser* parser)
13380 cp_decl_specifier_seq type_specifiers;
13381 const char *saved_message;
13382 int declares_class_or_enum;
13384 /* Try the declaration first. */
13385 cp_parser_parse_tentatively (parser);
13386 /* New types are not allowed in the type-specifier-seq for a
13387 condition. */
13388 saved_message = parser->type_definition_forbidden_message;
13389 parser->type_definition_forbidden_message
13390 = G_("types may not be defined in conditions");
13391 /* Parse the type-specifier-seq. */
13392 cp_parser_decl_specifier_seq (parser,
13393 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
13394 &type_specifiers,
13395 &declares_class_or_enum);
13396 /* Restore the saved message. */
13397 parser->type_definition_forbidden_message = saved_message;
13399 /* Gather the attributes that were provided with the
13400 decl-specifiers. */
13401 tree prefix_attributes = type_specifiers.attributes;
13403 cp_parser_maybe_commit_to_declaration (parser, &type_specifiers);
13405 /* If all is well, we might be looking at a declaration. */
13406 if (!cp_parser_error_occurred (parser))
13408 tree decl;
13409 tree asm_specification;
13410 tree attributes;
13411 cp_declarator *declarator;
13412 tree initializer = NULL_TREE;
13413 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13415 /* Parse the declarator. */
13416 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13417 CP_PARSER_FLAGS_NONE,
13418 /*ctor_dtor_or_conv_p=*/NULL,
13419 /*parenthesized_p=*/NULL,
13420 /*member_p=*/false,
13421 /*friend_p=*/false,
13422 /*static_p=*/false);
13423 /* Parse the attributes. */
13424 attributes = cp_parser_attributes_opt (parser);
13425 /* Parse the asm-specification. */
13426 asm_specification = cp_parser_asm_specification_opt (parser);
13427 /* If the next token is not an `=' or '{', then we might still be
13428 looking at an expression. For example:
13430 if (A(a).x)
13432 looks like a decl-specifier-seq and a declarator -- but then
13433 there is no `=', so this is an expression. */
13434 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
13435 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13436 cp_parser_simulate_error (parser);
13438 /* If we did see an `=' or '{', then we are looking at a declaration
13439 for sure. */
13440 if (cp_parser_parse_definitely (parser))
13442 tree pushed_scope;
13443 bool non_constant_p = false;
13444 int flags = LOOKUP_ONLYCONVERTING;
13446 if (!cp_parser_check_condition_declarator (parser, declarator, loc))
13447 return error_mark_node;
13449 /* Create the declaration. */
13450 decl = start_decl (declarator, &type_specifiers,
13451 /*initialized_p=*/true,
13452 attributes, prefix_attributes,
13453 &pushed_scope);
13455 declarator->init_loc = cp_lexer_peek_token (parser->lexer)->location;
13456 /* Parse the initializer. */
13457 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13459 initializer = cp_parser_braced_list (parser, &non_constant_p);
13460 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
13461 flags = 0;
13463 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13465 /* Consume the `='. */
13466 cp_lexer_consume_token (parser->lexer);
13467 initializer = cp_parser_initializer_clause (parser,
13468 &non_constant_p);
13470 else
13472 cp_parser_error (parser, "expected initializer");
13473 initializer = error_mark_node;
13475 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
13476 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
13478 /* Process the initializer. */
13479 cp_finish_decl (decl,
13480 initializer, !non_constant_p,
13481 asm_specification,
13482 flags);
13484 if (pushed_scope)
13485 pop_scope (pushed_scope);
13487 return convert_from_reference (decl);
13490 /* If we didn't even get past the declarator successfully, we are
13491 definitely not looking at a declaration. */
13492 else
13493 cp_parser_abort_tentative_parse (parser);
13495 /* Otherwise, we are looking at an expression. */
13496 return cp_parser_expression (parser);
13499 /* Parses a for-statement or range-for-statement until the closing ')',
13500 not included. */
13502 static tree
13503 cp_parser_for (cp_parser *parser, bool ivdep, unsigned short unroll)
13505 tree init, scope, decl;
13506 bool is_range_for;
13508 /* Begin the for-statement. */
13509 scope = begin_for_scope (&init);
13511 /* Maybe parse the optional init-statement in a range-based for loop. */
13512 if (cp_parser_range_based_for_with_init_p (parser)
13513 /* Checked for diagnostic purposes only. */
13514 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13516 tree dummy;
13517 cp_parser_init_statement (parser, &dummy);
13518 if (cxx_dialect < cxx20)
13520 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
13521 OPT_Wc__20_extensions,
13522 "range-based %<for%> loops with initializer only "
13523 "available with %<-std=c++20%> or %<-std=gnu++20%>");
13524 decl = error_mark_node;
13528 /* Parse the initialization. */
13529 is_range_for = cp_parser_init_statement (parser, &decl);
13531 if (is_range_for)
13532 return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll,
13533 false);
13534 else
13535 return cp_parser_c_for (parser, scope, init, ivdep, unroll);
13538 static tree
13539 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
13540 unsigned short unroll)
13542 /* Normal for loop */
13543 tree condition = NULL_TREE;
13544 tree expression = NULL_TREE;
13545 tree stmt;
13547 stmt = begin_for_stmt (scope, init);
13548 /* The init-statement has already been parsed in
13549 cp_parser_init_statement, so no work is needed here. */
13550 finish_init_stmt (stmt);
13552 /* If there's a condition, process it. */
13553 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13554 condition = cp_parser_condition (parser);
13555 else if (ivdep)
13557 cp_parser_error (parser, "missing loop condition in loop with "
13558 "%<GCC ivdep%> pragma");
13559 condition = error_mark_node;
13561 else if (unroll)
13563 cp_parser_error (parser, "missing loop condition in loop with "
13564 "%<GCC unroll%> pragma");
13565 condition = error_mark_node;
13567 finish_for_cond (condition, stmt, ivdep, unroll);
13568 /* Look for the `;'. */
13569 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13571 /* If there's an expression, process it. */
13572 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
13573 expression = cp_parser_expression (parser);
13574 finish_for_expr (expression, stmt);
13576 return stmt;
13579 /* Tries to parse a range-based for-statement:
13581 range-based-for:
13582 decl-specifier-seq declarator : expression
13584 The decl-specifier-seq declarator and the `:' are already parsed by
13585 cp_parser_init_statement. If processing_template_decl it returns a
13586 newly created RANGE_FOR_STMT; if not, it is converted to a
13587 regular FOR_STMT. */
13589 static tree
13590 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
13591 bool ivdep, unsigned short unroll, bool is_omp)
13593 tree stmt, range_expr;
13594 auto_vec <cxx_binding *, 16> bindings;
13595 auto_vec <tree, 16> names;
13596 tree decomp_first_name = NULL_TREE;
13597 unsigned int decomp_cnt = 0;
13599 /* Get the range declaration momentarily out of the way so that
13600 the range expression doesn't clash with it. */
13601 if (range_decl != error_mark_node)
13603 if (DECL_HAS_VALUE_EXPR_P (range_decl))
13605 tree v = DECL_VALUE_EXPR (range_decl);
13606 /* For decomposition declaration get all of the corresponding
13607 declarations out of the way. */
13608 if (TREE_CODE (v) == ARRAY_REF
13609 && VAR_P (TREE_OPERAND (v, 0))
13610 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
13612 tree d = range_decl;
13613 range_decl = TREE_OPERAND (v, 0);
13614 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
13615 decomp_first_name = d;
13616 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
13618 tree name = DECL_NAME (d);
13619 names.safe_push (name);
13620 bindings.safe_push (IDENTIFIER_BINDING (name));
13621 IDENTIFIER_BINDING (name)
13622 = IDENTIFIER_BINDING (name)->previous;
13626 if (names.is_empty ())
13628 tree name = DECL_NAME (range_decl);
13629 names.safe_push (name);
13630 bindings.safe_push (IDENTIFIER_BINDING (name));
13631 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
13635 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13637 bool expr_non_constant_p;
13638 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
13640 else
13641 range_expr = cp_parser_expression (parser);
13643 /* Put the range declaration(s) back into scope. */
13644 for (unsigned int i = 0; i < names.length (); i++)
13646 cxx_binding *binding = bindings[i];
13647 binding->previous = IDENTIFIER_BINDING (names[i]);
13648 IDENTIFIER_BINDING (names[i]) = binding;
13651 /* finish_omp_for has its own code for the following, so just
13652 return the range_expr instead. */
13653 if (is_omp)
13654 return range_expr;
13656 /* If in template, STMT is converted to a normal for-statement
13657 at instantiation. If not, it is done just ahead. */
13658 if (processing_template_decl)
13660 if (check_for_bare_parameter_packs (range_expr))
13661 range_expr = error_mark_node;
13662 stmt = begin_range_for_stmt (scope, init);
13663 if (ivdep)
13664 RANGE_FOR_IVDEP (stmt) = 1;
13665 if (unroll)
13666 RANGE_FOR_UNROLL (stmt) = build_int_cst (integer_type_node, unroll);
13667 finish_range_for_decl (stmt, range_decl, range_expr);
13668 if (!type_dependent_expression_p (range_expr)
13669 /* do_auto_deduction doesn't mess with template init-lists. */
13670 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
13671 do_range_for_auto_deduction (range_decl, range_expr, decomp_first_name,
13672 decomp_cnt);
13674 else
13676 stmt = begin_for_stmt (scope, init);
13677 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
13678 decomp_first_name, decomp_cnt, ivdep,
13679 unroll);
13681 return stmt;
13684 /* Subroutine of cp_convert_range_for: given the initializer expression,
13685 builds up the range temporary. */
13687 static tree
13688 build_range_temp (tree range_expr)
13690 /* Find out the type deduced by the declaration
13691 `auto &&__range = range_expr'. */
13692 tree auto_node = make_auto ();
13693 tree range_type = cp_build_reference_type (auto_node, true);
13694 range_type = do_auto_deduction (range_type, range_expr, auto_node);
13696 /* Create the __range variable. */
13697 tree range_temp = build_decl (input_location, VAR_DECL,
13698 for_range__identifier, range_type);
13699 TREE_USED (range_temp) = 1;
13700 DECL_ARTIFICIAL (range_temp) = 1;
13702 return range_temp;
13705 /* Used by cp_parser_range_for in template context: we aren't going to
13706 do a full conversion yet, but we still need to resolve auto in the
13707 type of the for-range-declaration if present. This is basically
13708 a shortcut version of cp_convert_range_for. */
13710 static void
13711 do_range_for_auto_deduction (tree decl, tree range_expr,
13712 tree decomp_first_name, unsigned int decomp_cnt)
13714 tree auto_node = type_uses_auto (TREE_TYPE (decl));
13715 if (auto_node)
13717 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
13718 range_temp = convert_from_reference (build_range_temp (range_expr));
13719 iter_type = (cp_parser_perform_range_for_lookup
13720 (range_temp, &begin_dummy, &end_dummy));
13721 if (iter_type)
13723 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
13724 iter_type);
13725 iter_decl = build_x_indirect_ref (input_location, iter_decl,
13726 RO_UNARY_STAR, NULL_TREE,
13727 tf_warning_or_error);
13728 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
13729 iter_decl, auto_node,
13730 tf_warning_or_error,
13731 adc_variable_type);
13732 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
13733 cp_finish_decomp (decl, decomp_first_name, decomp_cnt);
13738 /* Warns when the loop variable should be changed to a reference type to
13739 avoid unnecessary copying. I.e., from
13741 for (const auto x : range)
13743 where range returns a reference, to
13745 for (const auto &x : range)
13747 if this version doesn't make a copy.
13749 This function also warns when the loop variable is initialized with
13750 a value of a different type resulting in a copy:
13752 int arr[10];
13753 for (const double &x : arr)
13755 DECL is the RANGE_DECL; EXPR is the *__for_begin expression.
13756 This function is never called when processing_template_decl is on. */
13758 static void
13759 warn_for_range_copy (tree decl, tree expr)
13761 if (!warn_range_loop_construct
13762 || decl == error_mark_node)
13763 return;
13765 location_t loc = DECL_SOURCE_LOCATION (decl);
13766 tree type = TREE_TYPE (decl);
13768 if (from_macro_expansion_at (loc))
13769 return;
13771 if (TYPE_REF_P (type))
13773 if (glvalue_p (expr)
13774 && ref_conv_binds_to_temporary (type, expr).is_true ())
13776 auto_diagnostic_group d;
13777 if (warning_at (loc, OPT_Wrange_loop_construct,
13778 "loop variable %qD of type %qT binds to a temporary "
13779 "constructed from type %qT", decl, type,
13780 TREE_TYPE (expr)))
13782 tree ref = cp_build_qualified_type (TREE_TYPE (expr),
13783 TYPE_QUAL_CONST);
13784 ref = cp_build_reference_type (ref, /*rval*/false);
13785 inform (loc, "use non-reference type %qT to make the copy "
13786 "explicit or %qT to prevent copying",
13787 non_reference (type), ref);
13790 return;
13792 else if (!CP_TYPE_CONST_P (type))
13793 return;
13795 /* Since small trivially copyable types are cheap to copy, we suppress the
13796 warning for them. 64B is a common size of a cache line. */
13797 if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST
13798 || (tree_to_uhwi (TYPE_SIZE_UNIT (type)) <= 64
13799 && trivially_copyable_p (type)))
13800 return;
13802 /* If we can initialize a reference directly, suggest that to avoid the
13803 copy. */
13804 tree rtype = cp_build_reference_type (type, /*rval*/false);
13805 if (ref_conv_binds_to_temporary (rtype, expr).is_false ())
13807 auto_diagnostic_group d;
13808 if (warning_at (loc, OPT_Wrange_loop_construct,
13809 "loop variable %qD creates a copy from type %qT",
13810 decl, type))
13812 gcc_rich_location richloc (loc);
13813 richloc.add_fixit_insert_before ("&");
13814 inform (&richloc, "use reference type to prevent copying");
13819 /* Converts a range-based for-statement into a normal
13820 for-statement, as per the definition.
13822 for (RANGE_DECL : RANGE_EXPR)
13823 BLOCK
13825 should be equivalent to:
13828 auto &&__range = RANGE_EXPR;
13829 for (auto __begin = BEGIN_EXPR, __end = END_EXPR;
13830 __begin != __end;
13831 ++__begin)
13833 RANGE_DECL = *__begin;
13834 BLOCK
13838 If RANGE_EXPR is an array:
13839 BEGIN_EXPR = __range
13840 END_EXPR = __range + ARRAY_SIZE(__range)
13841 Else if RANGE_EXPR has a member 'begin' or 'end':
13842 BEGIN_EXPR = __range.begin()
13843 END_EXPR = __range.end()
13844 Else:
13845 BEGIN_EXPR = begin(__range)
13846 END_EXPR = end(__range);
13848 If __range has a member 'begin' but not 'end', or vice versa, we must
13849 still use the second alternative (it will surely fail, however).
13850 When calling begin()/end() in the third alternative we must use
13851 argument dependent lookup, but always considering 'std' as an associated
13852 namespace. */
13854 tree
13855 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
13856 tree decomp_first_name, unsigned int decomp_cnt,
13857 bool ivdep, unsigned short unroll)
13859 tree begin, end;
13860 tree iter_type, begin_expr, end_expr;
13861 tree condition, expression;
13863 range_expr = mark_lvalue_use (range_expr);
13865 if (range_decl == error_mark_node || range_expr == error_mark_node)
13866 /* If an error happened previously do nothing or else a lot of
13867 unhelpful errors would be issued. */
13868 begin_expr = end_expr = iter_type = error_mark_node;
13869 else
13871 tree range_temp;
13873 if (VAR_P (range_expr)
13874 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
13875 /* Can't bind a reference to an array of runtime bound. */
13876 range_temp = range_expr;
13877 else
13879 range_temp = build_range_temp (range_expr);
13880 pushdecl (range_temp);
13881 cp_finish_decl (range_temp, range_expr,
13882 /*is_constant_init*/false, NULL_TREE,
13883 LOOKUP_ONLYCONVERTING);
13884 range_temp = convert_from_reference (range_temp);
13886 iter_type = cp_parser_perform_range_for_lookup (range_temp,
13887 &begin_expr, &end_expr);
13890 /* The new for initialization statement. */
13891 begin = build_decl (input_location, VAR_DECL, for_begin__identifier,
13892 iter_type);
13893 TREE_USED (begin) = 1;
13894 DECL_ARTIFICIAL (begin) = 1;
13895 pushdecl (begin);
13896 cp_finish_decl (begin, begin_expr,
13897 /*is_constant_init*/false, NULL_TREE,
13898 LOOKUP_ONLYCONVERTING);
13900 if (cxx_dialect >= cxx17)
13901 iter_type = cv_unqualified (TREE_TYPE (end_expr));
13902 end = build_decl (input_location, VAR_DECL, for_end__identifier, iter_type);
13903 TREE_USED (end) = 1;
13904 DECL_ARTIFICIAL (end) = 1;
13905 pushdecl (end);
13906 cp_finish_decl (end, end_expr,
13907 /*is_constant_init*/false, NULL_TREE,
13908 LOOKUP_ONLYCONVERTING);
13910 finish_init_stmt (statement);
13912 /* The new for condition. */
13913 condition = build_x_binary_op (input_location, NE_EXPR,
13914 begin, ERROR_MARK,
13915 end, ERROR_MARK,
13916 NULL_TREE, NULL, tf_warning_or_error);
13917 finish_for_cond (condition, statement, ivdep, unroll);
13919 /* The new increment expression. */
13920 expression = finish_unary_op_expr (input_location,
13921 PREINCREMENT_EXPR, begin,
13922 tf_warning_or_error);
13923 finish_for_expr (expression, statement);
13925 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
13926 cp_maybe_mangle_decomp (range_decl, decomp_first_name, decomp_cnt);
13928 /* The declaration is initialized with *__begin inside the loop body. */
13929 tree deref_begin = build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
13930 NULL_TREE, tf_warning_or_error);
13931 cp_finish_decl (range_decl, deref_begin,
13932 /*is_constant_init*/false, NULL_TREE,
13933 LOOKUP_ONLYCONVERTING);
13934 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
13935 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
13937 warn_for_range_copy (range_decl, deref_begin);
13939 return statement;
13942 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
13943 We need to solve both at the same time because the method used
13944 depends on the existence of members begin or end.
13945 Returns the type deduced for the iterator expression. */
13947 static tree
13948 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
13950 if (error_operand_p (range))
13952 *begin = *end = error_mark_node;
13953 return error_mark_node;
13956 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
13958 error ("range-based %<for%> expression of type %qT "
13959 "has incomplete type", TREE_TYPE (range));
13960 *begin = *end = error_mark_node;
13961 return error_mark_node;
13963 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
13965 /* If RANGE is an array, we will use pointer arithmetic. */
13966 *begin = decay_conversion (range, tf_warning_or_error);
13967 *end = build_binary_op (input_location, PLUS_EXPR,
13968 range,
13969 array_type_nelts_top (TREE_TYPE (range)),
13970 false);
13971 return TREE_TYPE (*begin);
13973 else
13975 /* If it is not an array, we must do a bit of magic. */
13976 tree id_begin, id_end;
13977 tree member_begin, member_end;
13979 *begin = *end = error_mark_node;
13981 id_begin = get_identifier ("begin");
13982 id_end = get_identifier ("end");
13983 member_begin = lookup_member (TREE_TYPE (range), id_begin,
13984 /*protect=*/2, /*want_type=*/false,
13985 tf_warning_or_error);
13986 member_end = lookup_member (TREE_TYPE (range), id_end,
13987 /*protect=*/2, /*want_type=*/false,
13988 tf_warning_or_error);
13990 if (member_begin != NULL_TREE && member_end != NULL_TREE)
13992 /* Use the member functions. */
13993 *begin = cp_parser_range_for_member_function (range, id_begin);
13994 *end = cp_parser_range_for_member_function (range, id_end);
13996 else
13998 /* Use global functions with ADL. */
13999 releasing_vec vec;
14001 vec_safe_push (vec, range);
14003 member_begin = perform_koenig_lookup (id_begin, vec,
14004 tf_warning_or_error);
14005 *begin = finish_call_expr (member_begin, &vec, false, true,
14006 tf_warning_or_error);
14007 member_end = perform_koenig_lookup (id_end, vec,
14008 tf_warning_or_error);
14009 *end = finish_call_expr (member_end, &vec, false, true,
14010 tf_warning_or_error);
14013 /* Last common checks. */
14014 if (*begin == error_mark_node || *end == error_mark_node)
14016 /* If one of the expressions is an error do no more checks. */
14017 *begin = *end = error_mark_node;
14018 return error_mark_node;
14020 else if (type_dependent_expression_p (*begin)
14021 || type_dependent_expression_p (*end))
14022 /* Can happen, when, eg, in a template context, Koenig lookup
14023 can't resolve begin/end (c++/58503). */
14024 return NULL_TREE;
14025 else
14027 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
14028 /* The unqualified type of the __begin and __end temporaries should
14029 be the same, as required by the multiple auto declaration. */
14030 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
14032 if (cxx_dialect >= cxx17
14033 && (build_x_binary_op (input_location, NE_EXPR,
14034 *begin, ERROR_MARK,
14035 *end, ERROR_MARK,
14036 NULL_TREE, NULL, tf_none)
14037 != error_mark_node))
14038 /* P0184R0 allows __begin and __end to have different types,
14039 but make sure they are comparable so we can give a better
14040 diagnostic. */;
14041 else
14042 error ("inconsistent begin/end types in range-based %<for%> "
14043 "statement: %qT and %qT",
14044 TREE_TYPE (*begin), TREE_TYPE (*end));
14046 return iter_type;
14051 /* Helper function for cp_parser_perform_range_for_lookup.
14052 Builds a tree for RANGE.IDENTIFIER(). */
14054 static tree
14055 cp_parser_range_for_member_function (tree range, tree identifier)
14057 tree member, res;
14059 member = finish_class_member_access_expr (range, identifier,
14060 false, tf_warning_or_error);
14061 if (member == error_mark_node)
14062 return error_mark_node;
14064 releasing_vec vec;
14065 res = finish_call_expr (member, &vec,
14066 /*disallow_virtual=*/false,
14067 /*koenig_p=*/false,
14068 tf_warning_or_error);
14069 return res;
14072 /* Parse an iteration-statement.
14074 iteration-statement:
14075 while ( condition ) statement
14076 do statement while ( expression ) ;
14077 for ( init-statement condition [opt] ; expression [opt] )
14078 statement
14080 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
14082 static tree
14083 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
14084 unsigned short unroll)
14086 cp_token *token;
14087 enum rid keyword;
14088 tree statement;
14089 unsigned char in_statement;
14090 token_indent_info guard_tinfo;
14092 /* Peek at the next token. */
14093 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
14094 if (!token)
14095 return error_mark_node;
14097 guard_tinfo = get_token_indent_info (token);
14099 /* Remember whether or not we are already within an iteration
14100 statement. */
14101 in_statement = parser->in_statement;
14103 /* See what kind of keyword it is. */
14104 keyword = token->keyword;
14105 switch (keyword)
14107 case RID_WHILE:
14109 tree condition;
14111 /* Begin the while-statement. */
14112 statement = begin_while_stmt ();
14113 /* Look for the `('. */
14114 matching_parens parens;
14115 parens.require_open (parser);
14116 /* Parse the condition. */
14117 condition = cp_parser_condition (parser);
14118 finish_while_stmt_cond (condition, statement, ivdep, unroll);
14119 /* Look for the `)'. */
14120 parens.require_close (parser);
14121 /* Parse the dependent statement. */
14122 parser->in_statement = IN_ITERATION_STMT;
14123 bool prev = note_iteration_stmt_body_start ();
14124 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
14125 note_iteration_stmt_body_end (prev);
14126 parser->in_statement = in_statement;
14127 /* We're done with the while-statement. */
14128 finish_while_stmt (statement);
14130 break;
14132 case RID_DO:
14134 tree expression;
14136 /* Begin the do-statement. */
14137 statement = begin_do_stmt ();
14138 /* Parse the body of the do-statement. */
14139 parser->in_statement = IN_ITERATION_STMT;
14140 bool prev = note_iteration_stmt_body_start ();
14141 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
14142 note_iteration_stmt_body_end (prev);
14143 parser->in_statement = in_statement;
14144 finish_do_body (statement);
14145 /* Look for the `while' keyword. */
14146 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
14147 /* Look for the `('. */
14148 matching_parens parens;
14149 parens.require_open (parser);
14150 /* Parse the expression. */
14151 expression = cp_parser_expression (parser);
14152 /* We're done with the do-statement. */
14153 finish_do_stmt (expression, statement, ivdep, unroll);
14154 /* Look for the `)'. */
14155 parens.require_close (parser);
14156 /* Look for the `;'. */
14157 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14159 break;
14161 case RID_FOR:
14163 /* Look for the `('. */
14164 matching_parens parens;
14165 parens.require_open (parser);
14167 statement = cp_parser_for (parser, ivdep, unroll);
14169 /* Look for the `)'. */
14170 parens.require_close (parser);
14172 /* Parse the body of the for-statement. */
14173 parser->in_statement = IN_ITERATION_STMT;
14174 bool prev = note_iteration_stmt_body_start ();
14175 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
14176 note_iteration_stmt_body_end (prev);
14177 parser->in_statement = in_statement;
14179 /* We're done with the for-statement. */
14180 finish_for_stmt (statement);
14182 break;
14184 default:
14185 cp_parser_error (parser, "expected iteration-statement");
14186 statement = error_mark_node;
14187 break;
14190 return statement;
14193 /* Parse an init-statement or the declarator of a range-based-for.
14194 Returns true if a range-based-for declaration is seen.
14196 init-statement:
14197 expression-statement
14198 simple-declaration
14199 alias-declaration */
14201 static bool
14202 cp_parser_init_statement (cp_parser *parser, tree *decl)
14204 /* If the next token is a `;', then we have an empty
14205 expression-statement. Grammatically, this is also a
14206 simple-declaration, but an invalid one, because it does not
14207 declare anything. Therefore, if we did not handle this case
14208 specially, we would issue an error message about an invalid
14209 declaration. */
14210 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14212 bool is_range_for = false;
14213 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
14215 /* A colon is used in range-based for. */
14216 parser->colon_corrects_to_scope_p = false;
14218 /* We're going to speculatively look for a declaration, falling back
14219 to an expression, if necessary. */
14220 cp_parser_parse_tentatively (parser);
14221 bool expect_semicolon_p = true;
14222 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
14224 cp_parser_alias_declaration (parser);
14225 expect_semicolon_p = false;
14226 if (cxx_dialect < cxx23
14227 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
14228 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
14229 OPT_Wc__23_extensions,
14230 "alias-declaration in init-statement only "
14231 "available with %<-std=c++23%> or %<-std=gnu++23%>");
14233 else
14234 /* Parse the declaration. */
14235 cp_parser_simple_declaration (parser,
14236 /*function_definition_allowed_p=*/false,
14237 decl);
14238 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
14239 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14241 /* It is a range-for, consume the ':'. */
14242 cp_lexer_consume_token (parser->lexer);
14243 is_range_for = true;
14244 if (cxx_dialect < cxx11)
14245 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
14246 OPT_Wc__11_extensions,
14247 "range-based %<for%> loops only available with "
14248 "%<-std=c++11%> or %<-std=gnu++11%>");
14250 else if (expect_semicolon_p)
14251 /* The ';' is not consumed yet because we told
14252 cp_parser_simple_declaration not to. */
14253 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14255 if (cp_parser_parse_definitely (parser))
14256 return is_range_for;
14257 /* If the tentative parse failed, then we shall need to look for an
14258 expression-statement. */
14260 /* If we are here, it is an expression-statement. */
14261 cp_parser_expression_statement (parser, NULL_TREE);
14262 return false;
14265 /* Parse a jump-statement.
14267 jump-statement:
14268 break ;
14269 continue ;
14270 return expression [opt] ;
14271 return braced-init-list ;
14272 coroutine-return-statement;
14273 goto identifier ;
14275 GNU extension:
14277 jump-statement:
14278 goto * expression ;
14280 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
14282 static tree
14283 cp_parser_jump_statement (cp_parser* parser)
14285 tree statement = error_mark_node;
14286 cp_token *token;
14287 enum rid keyword;
14288 unsigned char in_statement;
14290 /* Peek at the next token. */
14291 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
14292 if (!token)
14293 return error_mark_node;
14295 /* See what kind of keyword it is. */
14296 keyword = token->keyword;
14297 switch (keyword)
14299 case RID_BREAK:
14300 in_statement = parser->in_statement & ~IN_IF_STMT;
14301 switch (in_statement)
14303 case 0:
14304 error_at (token->location, "break statement not within loop or switch");
14305 break;
14306 default:
14307 gcc_assert ((in_statement & IN_SWITCH_STMT)
14308 || in_statement == IN_ITERATION_STMT);
14309 statement = finish_break_stmt ();
14310 if (in_statement == IN_ITERATION_STMT)
14311 break_maybe_infinite_loop ();
14312 break;
14313 case IN_OMP_BLOCK:
14314 error_at (token->location, "invalid exit from OpenMP structured block");
14315 break;
14316 case IN_OMP_FOR:
14317 error_at (token->location, "break statement used with OpenMP for loop");
14318 break;
14320 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14321 break;
14323 case RID_CONTINUE:
14324 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
14326 case 0:
14327 error_at (token->location, "continue statement not within a loop");
14328 break;
14329 /* Fall through. */
14330 case IN_ITERATION_STMT:
14331 case IN_OMP_FOR:
14332 statement = finish_continue_stmt ();
14333 break;
14334 case IN_OMP_BLOCK:
14335 error_at (token->location, "invalid exit from OpenMP structured block");
14336 break;
14337 default:
14338 gcc_unreachable ();
14340 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14341 break;
14343 case RID_CO_RETURN:
14344 case RID_RETURN:
14346 tree expr;
14347 bool expr_non_constant_p;
14349 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14351 cp_lexer_set_source_position (parser->lexer);
14352 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14353 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
14355 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14356 expr = cp_parser_expression (parser);
14357 else
14358 /* If the next token is a `;', then there is no
14359 expression. */
14360 expr = NULL_TREE;
14361 /* Build the return-statement, check co-return first, since type
14362 deduction is not valid there. */
14363 if (keyword == RID_CO_RETURN)
14364 statement = finish_co_return_stmt (token->location, expr);
14365 else if (FNDECL_USED_AUTO (current_function_decl) && in_discarded_stmt)
14366 /* Don't deduce from a discarded return statement. */;
14367 else
14368 statement = finish_return_stmt (expr);
14369 /* Look for the final `;'. */
14370 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14372 break;
14374 case RID_GOTO:
14375 if (parser->in_function_body
14376 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
14377 && cxx_dialect < cxx23)
14379 error ("%<goto%> in %<constexpr%> function only available with "
14380 "%<-std=c++2b%> or %<-std=gnu++2b%>");
14381 cp_function_chain->invalid_constexpr = true;
14384 /* Create the goto-statement. */
14385 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
14387 /* Issue a warning about this use of a GNU extension. */
14388 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
14389 /* Consume the '*' token. */
14390 cp_lexer_consume_token (parser->lexer);
14391 /* Parse the dependent expression. */
14392 finish_goto_stmt (cp_parser_expression (parser));
14394 else
14395 finish_goto_stmt (cp_parser_identifier (parser));
14396 /* Look for the final `;'. */
14397 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14398 break;
14400 default:
14401 cp_parser_error (parser, "expected jump-statement");
14402 break;
14405 return statement;
14408 /* Parse a declaration-statement.
14410 declaration-statement:
14411 block-declaration */
14413 static void
14414 cp_parser_declaration_statement (cp_parser* parser)
14416 void *p;
14418 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
14419 p = obstack_alloc (&declarator_obstack, 0);
14421 /* Parse the block-declaration. */
14422 cp_parser_block_declaration (parser, /*statement_p=*/true);
14424 /* Free any declarators allocated. */
14425 obstack_free (&declarator_obstack, p);
14428 /* Some dependent statements (like `if (cond) statement'), are
14429 implicitly in their own scope. In other words, if the statement is
14430 a single statement (as opposed to a compound-statement), it is
14431 none-the-less treated as if it were enclosed in braces. Any
14432 declarations appearing in the dependent statement are out of scope
14433 after control passes that point. This function parses a statement,
14434 but ensures that is in its own scope, even if it is not a
14435 compound-statement.
14437 If IF_P is not NULL, *IF_P is set to indicate whether the statement
14438 is a (possibly labeled) if statement which is not enclosed in
14439 braces and has an else clause. This is used to implement
14440 -Wparentheses.
14442 CHAIN is a vector of if-else-if conditions. This is used to implement
14443 -Wduplicated-cond.
14445 Returns the new statement. */
14447 static tree
14448 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
14449 const token_indent_info &guard_tinfo,
14450 vec<tree> *chain)
14452 tree statement;
14453 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
14454 location_t body_loc_after_labels = UNKNOWN_LOCATION;
14455 token_indent_info body_tinfo
14456 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
14458 if (if_p != NULL)
14459 *if_p = false;
14461 /* Mark if () ; with a special NOP_EXPR. */
14462 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14464 cp_lexer_consume_token (parser->lexer);
14465 statement = add_stmt (build_empty_stmt (body_loc));
14467 if (guard_tinfo.keyword == RID_IF
14468 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
14469 warning_at (body_loc, OPT_Wempty_body,
14470 "suggest braces around empty body in an %<if%> statement");
14471 else if (guard_tinfo.keyword == RID_ELSE)
14472 warning_at (body_loc, OPT_Wempty_body,
14473 "suggest braces around empty body in an %<else%> statement");
14475 /* if a compound is opened, we simply parse the statement directly. */
14476 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14477 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
14478 /* If the token is not a `{', then we must take special action. */
14479 else
14481 /* Create a compound-statement. */
14482 statement = begin_compound_stmt (0);
14483 /* Parse the dependent-statement. */
14484 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
14485 &body_loc_after_labels);
14486 /* Finish the dummy compound-statement. */
14487 finish_compound_stmt (statement);
14490 token_indent_info next_tinfo
14491 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
14492 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
14494 if (body_loc_after_labels != UNKNOWN_LOCATION
14495 && next_tinfo.type != CPP_SEMICOLON)
14496 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
14497 guard_tinfo.location, guard_tinfo.keyword);
14499 /* Return the statement. */
14500 return statement;
14503 /* For some dependent statements (like `while (cond) statement'), we
14504 have already created a scope. Therefore, even if the dependent
14505 statement is a compound-statement, we do not want to create another
14506 scope. */
14508 static void
14509 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
14510 const token_indent_info &guard_tinfo)
14512 /* If the token is a `{', then we must take special action. */
14513 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14515 token_indent_info body_tinfo
14516 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
14517 location_t loc_after_labels = UNKNOWN_LOCATION;
14519 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
14520 &loc_after_labels);
14521 token_indent_info next_tinfo
14522 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
14523 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
14525 if (loc_after_labels != UNKNOWN_LOCATION
14526 && next_tinfo.type != CPP_SEMICOLON)
14527 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
14528 guard_tinfo.location,
14529 guard_tinfo.keyword);
14531 else
14533 /* Avoid calling cp_parser_compound_statement, so that we
14534 don't create a new scope. Do everything else by hand. */
14535 matching_braces braces;
14536 braces.require_open (parser);
14537 /* If the next keyword is `__label__' we have a label declaration. */
14538 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
14539 cp_parser_label_declaration (parser);
14540 /* Parse an (optional) statement-seq. */
14541 cp_parser_statement_seq_opt (parser, NULL_TREE);
14542 braces.require_close (parser);
14546 /* Modules */
14548 /* Parse a module-name,
14549 identifier
14550 module-name . identifier
14551 header-name
14553 Returns a pointer to module object, NULL. */
14555 static module_state *
14556 cp_parser_module_name (cp_parser *parser)
14558 cp_token *token = cp_lexer_peek_token (parser->lexer);
14559 if (token->type == CPP_HEADER_NAME)
14561 cp_lexer_consume_token (parser->lexer);
14563 return get_module (token->u.value);
14566 module_state *parent = NULL;
14567 bool partitioned = false;
14568 if (token->type == CPP_COLON && named_module_p ())
14570 partitioned = true;
14571 cp_lexer_consume_token (parser->lexer);
14574 for (;;)
14576 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME)
14578 cp_parser_error (parser, "expected module-name");
14579 break;
14582 tree name = cp_lexer_consume_token (parser->lexer)->u.value;
14583 parent = get_module (name, parent, partitioned);
14584 token = cp_lexer_peek_token (parser->lexer);
14585 if (!partitioned && token->type == CPP_COLON)
14586 partitioned = true;
14587 else if (token->type != CPP_DOT)
14588 break;
14590 cp_lexer_consume_token (parser->lexer);
14593 return parent;
14596 /* Named module-declaration
14597 __module ; PRAGMA_EOL
14598 __module private ; PRAGMA_EOL (unimplemented)
14599 [__export] __module module-name attr-spec-seq-opt ; PRAGMA_EOL
14602 static module_parse
14603 cp_parser_module_declaration (cp_parser *parser, module_parse mp_state,
14604 bool exporting)
14606 /* We're a pseudo pragma. */
14607 parser->lexer->in_pragma = true;
14608 cp_token *token = cp_lexer_consume_token (parser->lexer);
14610 if (flag_header_unit)
14612 error_at (token->location,
14613 "module-declaration not permitted in header-unit");
14614 goto skip_eol;
14616 else if (mp_state == MP_FIRST && !exporting
14617 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14619 /* Start global module fragment. */
14620 cp_lexer_consume_token (parser->lexer);
14621 module_kind = MK_NAMED;
14622 mp_state = MP_GLOBAL;
14623 cp_parser_require_pragma_eol (parser, token);
14625 else if (!exporting
14626 && cp_lexer_next_token_is (parser->lexer, CPP_COLON)
14627 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_PRIVATE)
14628 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_SEMICOLON))
14630 cp_lexer_consume_token (parser->lexer);
14631 cp_lexer_consume_token (parser->lexer);
14632 cp_lexer_consume_token (parser->lexer);
14633 cp_parser_require_pragma_eol (parser, token);
14635 if ((mp_state == MP_PURVIEW || mp_state == MP_PURVIEW_IMPORTS)
14636 && module_has_cmi_p ())
14638 mp_state = MP_PRIVATE_IMPORTS;
14639 sorry_at (token->location, "private module fragment");
14641 else
14642 error_at (token->location,
14643 "private module fragment only permitted in purview"
14644 " of module interface or partition");
14646 else if (!(mp_state == MP_FIRST || mp_state == MP_GLOBAL))
14648 /* Neither the first declaration, nor in a GMF. */
14649 error_at (token->location, "module-declaration only permitted as first"
14650 " declaration, or ending a global module fragment");
14651 skip_eol:
14652 cp_parser_skip_to_pragma_eol (parser, token);
14654 else
14656 module_state *mod = cp_parser_module_name (parser);
14657 tree attrs = cp_parser_attributes_opt (parser);
14659 mp_state = MP_PURVIEW_IMPORTS;
14660 if (!mod || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
14661 goto skip_eol;
14663 declare_module (mod, token->location, exporting, attrs, parse_in);
14664 cp_parser_require_pragma_eol (parser, token);
14667 return mp_state;
14670 /* Import-declaration
14671 [__export] __import module-name attr-spec-seq-opt ; PRAGMA_EOL */
14673 static void
14674 cp_parser_import_declaration (cp_parser *parser, module_parse mp_state,
14675 bool exporting)
14677 /* We're a pseudo pragma. */
14678 parser->lexer->in_pragma = true;
14679 cp_token *token = cp_lexer_consume_token (parser->lexer);
14681 if (mp_state == MP_PURVIEW || mp_state == MP_PRIVATE)
14683 error_at (token->location, "post-module-declaration"
14684 " imports must be contiguous");
14685 note_lexer:
14686 inform (token->location, "perhaps insert a line break, or other"
14687 " disambiguation, to prevent this being considered a"
14688 " module control-line");
14689 skip_eol:
14690 cp_parser_skip_to_pragma_eol (parser, token);
14692 else if (current_scope () != global_namespace)
14694 error_at (token->location, "import-declaration must be at global scope");
14695 goto note_lexer;
14697 else
14699 module_state *mod = cp_parser_module_name (parser);
14700 tree attrs = cp_parser_attributes_opt (parser);
14702 if (!mod || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
14703 goto skip_eol;
14704 cp_parser_require_pragma_eol (parser, token);
14706 if (parser->in_unbraced_linkage_specification_p)
14707 error_at (token->location, "import cannot appear directly in"
14708 " a linkage-specification");
14710 if (mp_state == MP_PURVIEW_IMPORTS || mp_state == MP_PRIVATE_IMPORTS)
14712 /* Module-purview imports must not be from source inclusion
14713 [cpp.import]/7 */
14714 if (attrs
14715 && private_lookup_attribute ("__translated",
14716 strlen ("__translated"), attrs))
14717 error_at (token->location, "post-module-declaration imports"
14718 " must not be include-translated");
14719 else if (!token->main_source_p)
14720 error_at (token->location, "post-module-declaration imports"
14721 " must not be from header inclusion");
14724 import_module (mod, token->location, exporting, attrs, parse_in);
14728 /* export-declaration.
14730 export declaration
14731 export { declaration-seq-opt } */
14733 static void
14734 cp_parser_module_export (cp_parser *parser)
14736 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT));
14737 cp_token *token = cp_lexer_consume_token (parser->lexer);
14739 if (!module_interface_p ())
14740 error_at (token->location,
14741 "%qE may only occur after a module interface declaration",
14742 token->u.value);
14744 bool braced = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE);
14746 unsigned mk = module_kind;
14747 if (module_exporting_p ())
14748 error_at (token->location,
14749 "%qE may only occur once in an export declaration",
14750 token->u.value);
14751 module_kind |= MK_EXPORTING;
14753 if (braced)
14755 cp_ensure_no_omp_declare_simd (parser);
14756 cp_ensure_no_oacc_routine (parser);
14758 cp_lexer_consume_token (parser->lexer);
14759 cp_parser_declaration_seq_opt (parser);
14760 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14762 else
14764 /* Explicitly check if the next tokens might be a
14765 module-directive line, so we can give a clearer error message
14766 about why the directive will be rejected. */
14767 if (cp_lexer_next_token_is_keyword (parser->lexer, RID__MODULE)
14768 || cp_lexer_next_token_is_keyword (parser->lexer, RID__IMPORT)
14769 || cp_lexer_next_token_is_keyword (parser->lexer, RID__EXPORT))
14770 error_at (token->location, "%<export%> not part of following"
14771 " module-directive");
14772 cp_parser_declaration (parser, NULL_TREE);
14775 module_kind = mk;
14778 /* Declarations [gram.dcl.dcl] */
14780 /* Parse an optional declaration-sequence. TOP_LEVEL is true, if this
14781 is the top-level declaration sequence. That affects whether we
14782 deal with module-preamble.
14784 declaration-seq:
14785 declaration
14786 declaration-seq declaration */
14788 static void
14789 cp_parser_declaration_seq_opt (cp_parser* parser)
14791 while (true)
14793 cp_token *token = cp_lexer_peek_token (parser->lexer);
14795 if (token->type == CPP_CLOSE_BRACE
14796 || token->type == CPP_EOF)
14797 break;
14798 else
14799 cp_parser_toplevel_declaration (parser);
14803 /* Parse a declaration.
14805 declaration:
14806 block-declaration
14807 function-definition
14808 template-declaration
14809 explicit-instantiation
14810 explicit-specialization
14811 linkage-specification
14812 namespace-definition
14814 C++17:
14815 deduction-guide
14817 modules:
14818 (all these are only allowed at the outermost level, check
14819 that semantically, for better diagnostics)
14820 module-declaration
14821 module-export-declaration
14822 module-import-declaration
14823 export-declaration
14825 GNU extension:
14827 declaration:
14828 __extension__ declaration */
14830 static void
14831 cp_parser_declaration (cp_parser* parser, tree prefix_attrs)
14833 int saved_pedantic;
14835 /* Check for the `__extension__' keyword. */
14836 if (cp_parser_extension_opt (parser, &saved_pedantic))
14838 /* Parse the qualified declaration. */
14839 cp_parser_declaration (parser, prefix_attrs);
14840 /* Restore the PEDANTIC flag. */
14841 pedantic = saved_pedantic;
14843 return;
14846 /* Try to figure out what kind of declaration is present. */
14847 cp_token *token1 = cp_lexer_peek_token (parser->lexer);
14848 cp_token *token2 = (token1->type == CPP_EOF
14849 ? token1 : cp_lexer_peek_nth_token (parser->lexer, 2));
14851 if (token1->type == CPP_SEMICOLON)
14853 cp_lexer_consume_token (parser->lexer);
14854 /* A declaration consisting of a single semicolon is invalid
14855 * before C++11. Allow it unless we're being pedantic. */
14856 if (cxx_dialect < cxx11)
14857 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
14858 return;
14860 else if (cp_lexer_nth_token_is (parser->lexer,
14861 cp_parser_skip_std_attribute_spec_seq (parser,
14863 CPP_SEMICOLON))
14865 location_t attrs_loc = token1->location;
14866 tree std_attrs = cp_parser_std_attribute_spec_seq (parser);
14868 if (std_attrs && (flag_openmp || flag_openmp_simd))
14870 gcc_assert (!parser->lexer->in_omp_attribute_pragma);
14871 std_attrs = cp_parser_handle_statement_omp_attributes (parser,
14872 std_attrs);
14873 if (parser->lexer->in_omp_attribute_pragma)
14875 cp_lexer *lexer = parser->lexer;
14876 while (parser->lexer->in_omp_attribute_pragma)
14878 gcc_assert (cp_lexer_next_token_is (parser->lexer,
14879 CPP_PRAGMA));
14880 cp_parser_pragma (parser, pragma_external, NULL);
14882 cp_lexer_destroy (lexer);
14886 if (std_attrs != NULL_TREE && !attribute_ignored_p (std_attrs))
14887 warning_at (make_location (attrs_loc, attrs_loc, parser->lexer),
14888 OPT_Wattributes, "attribute ignored");
14889 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14890 cp_lexer_consume_token (parser->lexer);
14891 return;
14894 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
14895 void *p = obstack_alloc (&declarator_obstack, 0);
14897 tree attributes = NULL_TREE;
14899 /* Conditionally, allow attributes to precede a linkage specification. */
14900 if (token1->keyword == RID_ATTRIBUTE)
14902 cp_lexer_save_tokens (parser->lexer);
14903 attributes = cp_parser_attributes_opt (parser);
14904 cp_token *t1 = cp_lexer_peek_token (parser->lexer);
14905 cp_token *t2 = (t1->type == CPP_EOF
14906 ? t1 : cp_lexer_peek_nth_token (parser->lexer, 2));
14907 if (t1->keyword == RID_EXTERN
14908 && cp_parser_is_pure_string_literal (t2))
14910 cp_lexer_commit_tokens (parser->lexer);
14911 /* We might have already been here. */
14912 if (!c_dialect_objc ())
14914 location_t where = get_finish (t2->location);
14915 warning_at (token1->location, OPT_Wattributes, "attributes are"
14916 " not permitted in this position");
14917 where = linemap_position_for_loc_and_offset (line_table,
14918 where, 1);
14919 inform (where, "attributes may be inserted here");
14920 attributes = NULL_TREE;
14922 token1 = t1;
14923 token2 = t2;
14925 else
14927 cp_lexer_rollback_tokens (parser->lexer);
14928 attributes = NULL_TREE;
14931 /* If we already had some attributes, and we've added more, then prepend.
14932 Otherwise attributes just contains any that we just read. */
14933 if (prefix_attrs)
14935 if (attributes)
14936 TREE_CHAIN (prefix_attrs) = attributes;
14937 attributes = prefix_attrs;
14940 /* If the next token is `extern' and the following token is a string
14941 literal, then we have a linkage specification. */
14942 if (token1->keyword == RID_EXTERN
14943 && cp_parser_is_pure_string_literal (token2))
14944 cp_parser_linkage_specification (parser, attributes);
14945 /* If the next token is `template', then we have either a template
14946 declaration, an explicit instantiation, or an explicit
14947 specialization. */
14948 else if (token1->keyword == RID_TEMPLATE)
14950 /* `template <>' indicates a template specialization. */
14951 if (token2->type == CPP_LESS
14952 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
14953 cp_parser_explicit_specialization (parser);
14954 /* `template <' indicates a template declaration. */
14955 else if (token2->type == CPP_LESS)
14956 cp_parser_template_declaration (parser, /*member_p=*/false);
14957 /* Anything else must be an explicit instantiation. */
14958 else
14959 cp_parser_explicit_instantiation (parser);
14961 /* If the next token is `export', it's new-style modules or
14962 old-style template. */
14963 else if (token1->keyword == RID_EXPORT)
14965 if (!modules_p ())
14966 cp_parser_template_declaration (parser, /*member_p=*/false);
14967 else
14968 cp_parser_module_export (parser);
14970 else if (cp_token_is_module_directive (token1))
14972 bool exporting = token1->keyword == RID__EXPORT;
14973 cp_token *next = exporting ? token2 : token1;
14974 if (exporting)
14975 cp_lexer_consume_token (parser->lexer);
14976 // In module purview this will be ill-formed.
14977 auto state = (!named_module_p () ? MP_NOT_MODULE
14978 : module_purview_p () ? MP_PURVIEW
14979 : MP_GLOBAL);
14980 if (next->keyword == RID__MODULE)
14981 cp_parser_module_declaration (parser, state, exporting);
14982 else
14983 cp_parser_import_declaration (parser, state, exporting);
14985 /* If the next token is `extern', 'static' or 'inline' and the one
14986 after that is `template', we have a GNU extended explicit
14987 instantiation directive. */
14988 else if (cp_parser_allow_gnu_extensions_p (parser)
14989 && token2->keyword == RID_TEMPLATE
14990 && (token1->keyword == RID_EXTERN
14991 || token1->keyword == RID_STATIC
14992 || token1->keyword == RID_INLINE))
14993 cp_parser_explicit_instantiation (parser);
14994 /* If the next token is `namespace', check for a named or unnamed
14995 namespace definition. */
14996 else if (token1->keyword == RID_NAMESPACE
14997 && (/* A named namespace definition. */
14998 (token2->type == CPP_NAME
14999 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
15000 != CPP_EQ))
15001 || (token2->type == CPP_OPEN_SQUARE
15002 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
15003 == CPP_OPEN_SQUARE)
15004 /* An unnamed namespace definition. */
15005 || token2->type == CPP_OPEN_BRACE
15006 || token2->keyword == RID_ATTRIBUTE))
15007 cp_parser_namespace_definition (parser);
15008 /* An inline (associated) namespace definition. */
15009 else if (token2->keyword == RID_NAMESPACE
15010 && token1->keyword == RID_INLINE)
15011 cp_parser_namespace_definition (parser);
15012 /* Objective-C++ declaration/definition. */
15013 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1->keyword))
15014 cp_parser_objc_declaration (parser, attributes);
15015 else if (c_dialect_objc ()
15016 && token1->keyword == RID_ATTRIBUTE
15017 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
15018 cp_parser_objc_declaration (parser, attributes);
15019 /* At this point we may have a template declared by a concept
15020 introduction. */
15021 else if (flag_concepts
15022 && cp_parser_template_declaration_after_export (parser,
15023 /*member_p=*/false))
15024 /* We did. */;
15025 else
15026 /* Try to parse a block-declaration, or a function-definition. */
15027 cp_parser_block_declaration (parser, /*statement_p=*/false);
15029 /* Free any declarators allocated. */
15030 obstack_free (&declarator_obstack, p);
15033 /* Parse a namespace-scope declaration. */
15035 static void
15036 cp_parser_toplevel_declaration (cp_parser* parser)
15038 cp_token *token = cp_lexer_peek_token (parser->lexer);
15040 if (token->type == CPP_PRAGMA)
15041 /* A top-level declaration can consist solely of a #pragma. A
15042 nested declaration cannot, so this is done here and not in
15043 cp_parser_declaration. (A #pragma at block scope is
15044 handled in cp_parser_statement.) */
15045 cp_parser_pragma (parser, pragma_external, NULL);
15046 else
15047 /* Parse the declaration itself. */
15048 cp_parser_declaration (parser, NULL_TREE);
15051 /* Parse a block-declaration.
15053 block-declaration:
15054 simple-declaration
15055 asm-definition
15056 namespace-alias-definition
15057 using-declaration
15058 using-directive
15060 GNU Extension:
15062 block-declaration:
15063 __extension__ block-declaration
15065 C++0x Extension:
15067 block-declaration:
15068 static_assert-declaration
15070 If STATEMENT_P is TRUE, then this block-declaration is occurring as
15071 part of a declaration-statement. */
15073 static void
15074 cp_parser_block_declaration (cp_parser *parser,
15075 bool statement_p)
15077 int saved_pedantic;
15079 /* Check for the `__extension__' keyword. */
15080 if (cp_parser_extension_opt (parser, &saved_pedantic))
15082 /* Parse the qualified declaration. */
15083 cp_parser_block_declaration (parser, statement_p);
15084 /* Restore the PEDANTIC flag. */
15085 pedantic = saved_pedantic;
15087 return;
15090 /* Peek at the next token to figure out which kind of declaration is
15091 present. */
15092 cp_token *token1 = cp_lexer_peek_token (parser->lexer);
15093 size_t attr_idx;
15095 /* If the next keyword is `asm', we have an asm-definition. */
15096 if (token1->keyword == RID_ASM)
15098 if (statement_p)
15099 cp_parser_commit_to_tentative_parse (parser);
15100 cp_parser_asm_definition (parser);
15102 /* If the next keyword is `namespace', we have a
15103 namespace-alias-definition. */
15104 else if (token1->keyword == RID_NAMESPACE)
15105 cp_parser_namespace_alias_definition (parser);
15106 /* If the next keyword is `using', we have a
15107 using-declaration, a using-directive, or an alias-declaration. */
15108 else if (token1->keyword == RID_USING)
15110 cp_token *token2;
15112 if (statement_p)
15113 cp_parser_commit_to_tentative_parse (parser);
15114 /* If the token after `using' is `namespace', then we have a
15115 using-directive. */
15116 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
15117 if (token2->keyword == RID_NAMESPACE)
15118 cp_parser_using_directive (parser);
15119 else if (token2->keyword == RID_ENUM)
15120 cp_parser_using_enum (parser);
15121 /* If the second token after 'using' is '=', then we have an
15122 alias-declaration. */
15123 else if (cxx_dialect >= cxx11
15124 && token2->type == CPP_NAME
15125 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
15126 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
15127 cp_parser_alias_declaration (parser);
15128 /* Otherwise, it's a using-declaration. */
15129 else
15130 cp_parser_using_declaration (parser,
15131 /*access_declaration_p=*/false);
15133 /* If the next keyword is `__label__' we have a misplaced label
15134 declaration. */
15135 else if (token1->keyword == RID_LABEL)
15137 cp_lexer_consume_token (parser->lexer);
15138 error_at (token1->location, "%<__label__%> not at the beginning of a block");
15139 cp_parser_skip_to_end_of_statement (parser);
15140 /* If the next token is now a `;', consume it. */
15141 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15142 cp_lexer_consume_token (parser->lexer);
15144 /* If the next token is `static_assert' we have a static assertion. */
15145 else if (token1->keyword == RID_STATIC_ASSERT)
15146 cp_parser_static_assert (parser, /*member_p=*/false);
15147 /* If the next tokens after attributes is `using namespace', then we have
15148 a using-directive. */
15149 else if ((attr_idx = cp_parser_skip_std_attribute_spec_seq (parser, 1)) != 1
15150 && cp_lexer_nth_token_is_keyword (parser->lexer, attr_idx,
15151 RID_USING)
15152 && cp_lexer_nth_token_is_keyword (parser->lexer, attr_idx + 1,
15153 RID_NAMESPACE))
15155 if (statement_p)
15156 cp_parser_commit_to_tentative_parse (parser);
15157 cp_parser_using_directive (parser);
15159 /* Anything else must be a simple-declaration. */
15160 else
15161 cp_parser_simple_declaration (parser, !statement_p,
15162 /*maybe_range_for_decl*/NULL);
15165 /* Parse a simple-declaration.
15167 simple-declaration:
15168 decl-specifier-seq [opt] init-declarator-list [opt] ;
15169 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
15170 brace-or-equal-initializer ;
15172 init-declarator-list:
15173 init-declarator
15174 init-declarator-list , init-declarator
15176 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
15177 function-definition as a simple-declaration.
15179 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
15180 parsed declaration if it is an uninitialized single declarator not followed
15181 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
15182 if present, will not be consumed. */
15184 static void
15185 cp_parser_simple_declaration (cp_parser* parser,
15186 bool function_definition_allowed_p,
15187 tree *maybe_range_for_decl)
15189 cp_decl_specifier_seq decl_specifiers;
15190 int declares_class_or_enum;
15191 bool saw_declarator;
15192 location_t comma_loc = UNKNOWN_LOCATION;
15193 location_t init_loc = UNKNOWN_LOCATION;
15195 if (maybe_range_for_decl)
15196 *maybe_range_for_decl = NULL_TREE;
15198 /* Defer access checks until we know what is being declared; the
15199 checks for names appearing in the decl-specifier-seq should be
15200 done as if we were in the scope of the thing being declared. */
15201 push_deferring_access_checks (dk_deferred);
15203 /* Parse the decl-specifier-seq. We have to keep track of whether
15204 or not the decl-specifier-seq declares a named class or
15205 enumeration type, since that is the only case in which the
15206 init-declarator-list is allowed to be empty.
15208 [dcl.dcl]
15210 In a simple-declaration, the optional init-declarator-list can be
15211 omitted only when declaring a class or enumeration, that is when
15212 the decl-specifier-seq contains either a class-specifier, an
15213 elaborated-type-specifier, or an enum-specifier. */
15214 cp_parser_decl_specifier_seq (parser,
15215 CP_PARSER_FLAGS_OPTIONAL,
15216 &decl_specifiers,
15217 &declares_class_or_enum);
15218 /* We no longer need to defer access checks. */
15219 stop_deferring_access_checks ();
15221 cp_omp_declare_simd_data odsd;
15222 if (decl_specifiers.attributes && (flag_openmp || flag_openmp_simd))
15223 cp_parser_handle_directive_omp_attributes (parser,
15224 &decl_specifiers.attributes,
15225 &odsd, true);
15227 /* In a block scope, a valid declaration must always have a
15228 decl-specifier-seq. By not trying to parse declarators, we can
15229 resolve the declaration/expression ambiguity more quickly. */
15230 if (!function_definition_allowed_p
15231 && !decl_specifiers.any_specifiers_p)
15233 cp_parser_error (parser, "expected declaration");
15234 goto done;
15237 /* If the next two tokens are both identifiers, the code is
15238 erroneous. The usual cause of this situation is code like:
15240 T t;
15242 where "T" should name a type -- but does not. */
15243 if (!decl_specifiers.any_type_specifiers_p
15244 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15246 /* If parsing tentatively, we should commit; we really are
15247 looking at a declaration. */
15248 cp_parser_commit_to_tentative_parse (parser);
15249 /* Give up. */
15250 goto done;
15253 cp_parser_maybe_commit_to_declaration (parser, &decl_specifiers);
15255 /* Look for C++17 decomposition declaration. */
15256 for (size_t n = 1; ; n++)
15257 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
15258 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
15259 continue;
15260 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
15261 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
15262 && decl_specifiers.any_specifiers_p)
15264 tree decl
15265 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
15266 maybe_range_for_decl,
15267 &init_loc);
15269 /* The next token should be either a `,' or a `;'. */
15270 cp_token *token = cp_lexer_peek_token (parser->lexer);
15271 /* If it's a `;', we are done. */
15272 if (token->type == CPP_SEMICOLON)
15273 goto finish;
15274 else if (maybe_range_for_decl)
15276 if (*maybe_range_for_decl == NULL_TREE)
15277 *maybe_range_for_decl = error_mark_node;
15278 goto finish;
15280 /* Anything else is an error. */
15281 else
15283 /* If we have already issued an error message we don't need
15284 to issue another one. */
15285 if ((decl != error_mark_node
15286 && DECL_INITIAL (decl) != error_mark_node)
15287 || cp_parser_uncommitted_to_tentative_parse_p (parser))
15288 cp_parser_error (parser, "expected %<;%>");
15289 /* Skip tokens until we reach the end of the statement. */
15290 cp_parser_skip_to_end_of_statement (parser);
15291 /* If the next token is now a `;', consume it. */
15292 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15293 cp_lexer_consume_token (parser->lexer);
15294 goto done;
15297 else
15298 break;
15300 tree last_type;
15301 bool auto_specifier_p;
15302 /* NULL_TREE if both variable and function declaration are allowed,
15303 error_mark_node if function declaration are not allowed and
15304 a FUNCTION_DECL that should be diagnosed if it is followed by
15305 variable declarations. */
15306 tree auto_function_declaration;
15308 last_type = NULL_TREE;
15309 auto_specifier_p
15310 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
15311 auto_function_declaration = NULL_TREE;
15313 /* Keep going until we hit the `;' at the end of the simple
15314 declaration. */
15315 saw_declarator = false;
15316 while (cp_lexer_next_token_is_not (parser->lexer,
15317 CPP_SEMICOLON))
15319 cp_token *token;
15320 bool function_definition_p;
15321 tree decl;
15322 tree auto_result = NULL_TREE;
15324 if (saw_declarator)
15326 /* If we are processing next declarator, comma is expected */
15327 token = cp_lexer_peek_token (parser->lexer);
15328 gcc_assert (token->type == CPP_COMMA);
15329 cp_lexer_consume_token (parser->lexer);
15330 if (maybe_range_for_decl)
15332 *maybe_range_for_decl = error_mark_node;
15333 if (comma_loc == UNKNOWN_LOCATION)
15334 comma_loc = token->location;
15337 else
15338 saw_declarator = true;
15340 /* Parse the init-declarator. */
15341 decl = cp_parser_init_declarator (parser,
15342 CP_PARSER_FLAGS_NONE,
15343 &decl_specifiers,
15344 /*checks=*/NULL,
15345 function_definition_allowed_p,
15346 /*member_p=*/false,
15347 declares_class_or_enum,
15348 &function_definition_p,
15349 maybe_range_for_decl,
15350 &init_loc,
15351 &auto_result);
15352 /* If an error occurred while parsing tentatively, exit quickly.
15353 (That usually happens when in the body of a function; each
15354 statement is treated as a declaration-statement until proven
15355 otherwise.) */
15356 if (cp_parser_error_occurred (parser))
15357 goto done;
15359 if (auto_specifier_p && cxx_dialect >= cxx14)
15361 /* If the init-declarator-list contains more than one
15362 init-declarator, they shall all form declarations of
15363 variables. */
15364 if (auto_function_declaration == NULL_TREE)
15365 auto_function_declaration
15366 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
15367 else if (TREE_CODE (decl) == FUNCTION_DECL
15368 || auto_function_declaration != error_mark_node)
15370 error_at (decl_specifiers.locations[ds_type_spec],
15371 "non-variable %qD in declaration with more than one "
15372 "declarator with placeholder type",
15373 TREE_CODE (decl) == FUNCTION_DECL
15374 ? decl : auto_function_declaration);
15375 auto_function_declaration = error_mark_node;
15379 if (auto_result
15380 && (!processing_template_decl || !type_uses_auto (auto_result)))
15382 if (last_type
15383 && last_type != error_mark_node
15384 && !same_type_p (auto_result, last_type))
15386 /* If the list of declarators contains more than one declarator,
15387 the type of each declared variable is determined as described
15388 above. If the type deduced for the template parameter U is not
15389 the same in each deduction, the program is ill-formed. */
15390 error_at (decl_specifiers.locations[ds_type_spec],
15391 "inconsistent deduction for %qT: %qT and then %qT",
15392 decl_specifiers.type, last_type, auto_result);
15393 last_type = error_mark_node;
15395 else
15396 last_type = auto_result;
15399 /* Handle function definitions specially. */
15400 if (function_definition_p)
15402 /* If the next token is a `,', then we are probably
15403 processing something like:
15405 void f() {}, *p;
15407 which is erroneous. */
15408 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15410 cp_token *token = cp_lexer_peek_token (parser->lexer);
15411 error_at (token->location,
15412 "mixing"
15413 " declarations and function-definitions is forbidden");
15415 /* Otherwise, we're done with the list of declarators. */
15416 else
15418 pop_deferring_access_checks ();
15419 cp_finalize_omp_declare_simd (parser, &odsd);
15420 return;
15423 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
15424 *maybe_range_for_decl = decl;
15425 /* The next token should be either a `,' or a `;'. */
15426 token = cp_lexer_peek_token (parser->lexer);
15427 /* If it's a `,', there are more declarators to come. */
15428 if (token->type == CPP_COMMA)
15429 /* will be consumed next time around */;
15430 /* If it's a `;', we are done. */
15431 else if (token->type == CPP_SEMICOLON)
15432 break;
15433 else if (maybe_range_for_decl)
15435 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
15436 permerror (decl_specifiers.locations[ds_type_spec],
15437 "types may not be defined in a for-range-declaration");
15438 break;
15440 /* Anything else is an error. */
15441 else
15443 /* If we have already issued an error message we don't need
15444 to issue another one. */
15445 if ((decl != error_mark_node
15446 && DECL_INITIAL (decl) != error_mark_node)
15447 || cp_parser_uncommitted_to_tentative_parse_p (parser))
15448 cp_parser_error (parser, "expected %<,%> or %<;%>");
15449 /* Skip tokens until we reach the end of the statement. */
15450 cp_parser_skip_to_end_of_statement (parser);
15451 /* If the next token is now a `;', consume it. */
15452 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15453 cp_lexer_consume_token (parser->lexer);
15454 goto done;
15456 /* After the first time around, a function-definition is not
15457 allowed -- even if it was OK at first. For example:
15459 int i, f() {}
15461 is not valid. */
15462 function_definition_allowed_p = false;
15465 /* Issue an error message if no declarators are present, and the
15466 decl-specifier-seq does not itself declare a class or
15467 enumeration: [dcl.dcl]/3. */
15468 if (!saw_declarator)
15470 if (cp_parser_declares_only_class_p (parser))
15472 if (!declares_class_or_enum
15473 && decl_specifiers.type
15474 && OVERLOAD_TYPE_P (decl_specifiers.type))
15475 /* Ensure an error is issued anyway when finish_decltype_type,
15476 called via cp_parser_decl_specifier_seq, returns a class or
15477 an enumeration (c++/51786). */
15478 decl_specifiers.type = NULL_TREE;
15479 shadow_tag (&decl_specifiers);
15481 /* Perform any deferred access checks. */
15482 perform_deferred_access_checks (tf_warning_or_error);
15485 /* Consume the `;'. */
15486 finish:
15487 if (!maybe_range_for_decl)
15488 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15489 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15491 if (init_loc != UNKNOWN_LOCATION)
15492 error_at (init_loc, "initializer in range-based %<for%> loop");
15493 if (comma_loc != UNKNOWN_LOCATION)
15494 error_at (comma_loc,
15495 "multiple declarations in range-based %<for%> loop");
15498 done:
15499 pop_deferring_access_checks ();
15500 cp_finalize_omp_declare_simd (parser, &odsd);
15503 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
15504 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
15505 initializer ; */
15507 static tree
15508 cp_parser_decomposition_declaration (cp_parser *parser,
15509 cp_decl_specifier_seq *decl_specifiers,
15510 tree *maybe_range_for_decl,
15511 location_t *init_loc)
15513 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
15514 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
15515 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
15517 /* Parse the identifier-list. */
15518 auto_vec<cp_expr, 10> v;
15519 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
15520 while (true)
15522 cp_expr e = cp_parser_identifier (parser);
15523 if (e.get_value () == error_mark_node)
15524 break;
15525 v.safe_push (e);
15526 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15527 break;
15528 cp_lexer_consume_token (parser->lexer);
15531 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
15532 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15534 end_loc = UNKNOWN_LOCATION;
15535 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
15536 false);
15537 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
15538 cp_lexer_consume_token (parser->lexer);
15539 else
15541 cp_parser_skip_to_end_of_statement (parser);
15542 return error_mark_node;
15546 if (cxx_dialect < cxx17)
15547 pedwarn (loc, OPT_Wc__17_extensions,
15548 "structured bindings only available with "
15549 "%<-std=c++17%> or %<-std=gnu++17%>");
15551 tree pushed_scope;
15552 cp_declarator *declarator = make_declarator (cdk_decomp);
15553 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
15554 declarator->id_loc = loc;
15555 if (ref_qual != REF_QUAL_NONE)
15556 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
15557 ref_qual == REF_QUAL_RVALUE,
15558 NULL_TREE);
15559 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
15560 NULL_TREE, decl_specifiers->attributes,
15561 &pushed_scope);
15562 tree orig_decl = decl;
15564 unsigned int i;
15565 cp_expr e;
15566 cp_decl_specifier_seq decl_specs;
15567 clear_decl_specs (&decl_specs);
15568 decl_specs.type = make_auto ();
15569 tree prev = decl;
15570 FOR_EACH_VEC_ELT (v, i, e)
15572 if (i == 0)
15573 declarator = make_id_declarator (NULL_TREE, e.get_value (),
15574 sfk_none, e.get_location ());
15575 else
15577 declarator->u.id.unqualified_name = e.get_value ();
15578 declarator->id_loc = e.get_location ();
15580 tree elt_pushed_scope;
15581 tree decl2 = start_decl (declarator, &decl_specs, SD_DECOMPOSITION,
15582 NULL_TREE, NULL_TREE, &elt_pushed_scope);
15583 if (decl2 == error_mark_node)
15584 decl = error_mark_node;
15585 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
15587 /* Ensure we've diagnosed redeclaration if we aren't creating
15588 a new VAR_DECL. */
15589 gcc_assert (errorcount);
15590 decl = error_mark_node;
15592 else
15593 prev = decl2;
15594 if (elt_pushed_scope)
15595 pop_scope (elt_pushed_scope);
15598 if (v.is_empty ())
15600 error_at (loc, "empty structured binding declaration");
15601 decl = error_mark_node;
15604 if (maybe_range_for_decl == NULL
15605 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
15607 bool non_constant_p = false, is_direct_init = false;
15608 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
15609 tree initializer = cp_parser_initializer (parser, &is_direct_init,
15610 &non_constant_p);
15611 if (initializer == NULL_TREE
15612 || (TREE_CODE (initializer) == TREE_LIST
15613 && TREE_CHAIN (initializer))
15614 || (is_direct_init
15615 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
15616 && CONSTRUCTOR_NELTS (initializer) != 1))
15618 error_at (loc, "invalid initializer for structured binding "
15619 "declaration");
15620 initializer = error_mark_node;
15623 if (decl != error_mark_node)
15625 cp_maybe_mangle_decomp (decl, prev, v.length ());
15626 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
15627 (is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
15628 cp_finish_decomp (decl, prev, v.length ());
15631 else if (decl != error_mark_node)
15633 *maybe_range_for_decl = prev;
15634 /* Ensure DECL_VALUE_EXPR is created for all the decls but
15635 the underlying DECL. */
15636 cp_finish_decomp (decl, prev, v.length ());
15639 if (pushed_scope)
15640 pop_scope (pushed_scope);
15642 if (decl == error_mark_node && DECL_P (orig_decl))
15644 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
15645 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
15648 return decl;
15651 /* Names of storage classes. */
15653 static const char *const
15654 cp_storage_class_name[] = {
15655 "", "auto", "register", "static", "extern", "mutable"
15658 /* Parse a decl-specifier-seq.
15660 decl-specifier-seq:
15661 decl-specifier-seq [opt] decl-specifier
15662 decl-specifier attribute-specifier-seq [opt] (C++11)
15664 decl-specifier:
15665 storage-class-specifier
15666 type-specifier
15667 function-specifier
15668 friend
15669 typedef
15671 GNU Extension:
15673 decl-specifier:
15674 attributes
15676 Concepts Extension:
15678 decl-specifier:
15679 concept
15681 Set *DECL_SPECS to a representation of the decl-specifier-seq.
15683 The parser flags FLAGS is used to control type-specifier parsing.
15685 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
15686 flags:
15688 1: one of the decl-specifiers is an elaborated-type-specifier
15689 (i.e., a type declaration)
15690 2: one of the decl-specifiers is an enum-specifier or a
15691 class-specifier (i.e., a type definition)
15695 static void
15696 cp_parser_decl_specifier_seq (cp_parser* parser,
15697 cp_parser_flags flags,
15698 cp_decl_specifier_seq *decl_specs,
15699 int* declares_class_or_enum)
15701 bool constructor_possible_p = !parser->in_declarator_p;
15702 bool found_decl_spec = false;
15703 cp_token *start_token = NULL;
15704 cp_decl_spec ds;
15706 /* Clear DECL_SPECS. */
15707 clear_decl_specs (decl_specs);
15709 /* Assume no class or enumeration type is declared. */
15710 *declares_class_or_enum = 0;
15712 /* Keep reading specifiers until there are no more to read. */
15713 while (true)
15715 bool constructor_p;
15716 cp_token *token;
15717 ds = ds_last;
15719 /* Peek at the next token. */
15720 token = cp_lexer_peek_token (parser->lexer);
15722 /* Save the first token of the decl spec list for error
15723 reporting. */
15724 if (!start_token)
15725 start_token = token;
15726 /* Handle attributes. */
15727 if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) == 0
15728 && cp_next_tokens_can_be_attribute_p (parser))
15730 /* Parse the attributes. */
15731 tree attrs = cp_parser_attributes_opt (parser);
15733 /* In a sequence of declaration specifiers, c++11 attributes
15734 appertain to the type that precede them. In that case
15735 [dcl.spec]/1 says:
15737 The attribute-specifier-seq affects the type only for
15738 the declaration it appears in, not other declarations
15739 involving the same type.
15741 But for now let's force the user to position the
15742 attribute either at the beginning of the declaration or
15743 after the declarator-id, which would clearly mean that it
15744 applies to the declarator. */
15745 if (cxx11_attribute_p (attrs))
15747 if (!found_decl_spec)
15748 /* The c++11 attribute is at the beginning of the
15749 declaration. It appertains to the entity being
15750 declared. */;
15751 else
15753 if (find_contract (attrs))
15755 diagnose_misapplied_contracts (attrs);
15756 attrs = NULL_TREE;
15758 else if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
15760 /* This is an attribute following a
15761 class-specifier. */
15762 if (decl_specs->type_definition_p)
15763 warn_misplaced_attr_for_class_type (token->location,
15764 decl_specs->type);
15765 attrs = NULL_TREE;
15767 else
15769 decl_specs->std_attributes
15770 = attr_chainon (decl_specs->std_attributes, attrs);
15771 if (decl_specs->locations[ds_std_attribute] == 0)
15772 decl_specs->locations[ds_std_attribute] = token->location;
15774 continue;
15778 decl_specs->attributes
15779 = attr_chainon (decl_specs->attributes, attrs);
15780 if (decl_specs->locations[ds_attribute] == 0)
15781 decl_specs->locations[ds_attribute] = token->location;
15782 continue;
15784 /* Assume we will find a decl-specifier keyword. */
15785 found_decl_spec = true;
15786 /* If the next token is an appropriate keyword, we can simply
15787 add it to the list. */
15788 switch (token->keyword)
15790 /* decl-specifier:
15791 friend
15792 constexpr
15793 constinit */
15794 case RID_FRIEND:
15795 if (!at_class_scope_p ())
15797 gcc_rich_location richloc (token->location);
15798 richloc.add_fixit_remove ();
15799 error_at (&richloc, "%<friend%> used outside of class");
15800 cp_lexer_purge_token (parser->lexer);
15802 else
15804 ds = ds_friend;
15805 /* Consume the token. */
15806 cp_lexer_consume_token (parser->lexer);
15808 break;
15810 case RID_CONSTEXPR:
15811 ds = ds_constexpr;
15812 cp_lexer_consume_token (parser->lexer);
15813 break;
15815 case RID_CONSTINIT:
15816 ds = ds_constinit;
15817 cp_lexer_consume_token (parser->lexer);
15818 break;
15820 case RID_CONSTEVAL:
15821 ds = ds_consteval;
15822 cp_lexer_consume_token (parser->lexer);
15823 break;
15825 case RID_CONCEPT:
15826 ds = ds_concept;
15827 cp_lexer_consume_token (parser->lexer);
15829 if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
15830 break;
15832 /* Warn for concept as a decl-specifier. We'll rewrite these as
15833 concept declarations later. */
15834 if (!flag_concepts_ts)
15836 cp_token *next = cp_lexer_peek_token (parser->lexer);
15837 if (next->keyword == RID_BOOL)
15838 permerror (next->location, "the %<bool%> keyword is not "
15839 "allowed in a C++20 concept definition");
15840 else
15841 error_at (token->location, "C++20 concept definition syntax "
15842 "is %<concept <name> = <expr>%>");
15845 /* In C++20 a concept definition is just 'concept name = expr;'
15846 Support that syntax as a TS extension by pretending we've seen
15847 the 'bool' specifier. */
15848 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
15849 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ)
15850 && !decl_specs->any_type_specifiers_p)
15852 cp_parser_set_decl_spec_type (decl_specs, boolean_type_node,
15853 token, /*type_definition*/false);
15854 decl_specs->any_type_specifiers_p = true;
15856 break;
15858 /* function-specifier:
15859 inline
15860 virtual
15861 explicit */
15862 case RID_INLINE:
15863 case RID_VIRTUAL:
15864 case RID_EXPLICIT:
15865 cp_parser_function_specifier_opt (parser, decl_specs);
15866 break;
15868 /* decl-specifier:
15869 typedef */
15870 case RID_TYPEDEF:
15871 ds = ds_typedef;
15872 /* Consume the token. */
15873 cp_lexer_consume_token (parser->lexer);
15875 if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
15876 break;
15878 /* A constructor declarator cannot appear in a typedef. */
15879 constructor_possible_p = false;
15880 /* The "typedef" keyword can only occur in a declaration; we
15881 may as well commit at this point. */
15882 cp_parser_commit_to_tentative_parse (parser);
15884 if (decl_specs->storage_class != sc_none)
15886 if (decl_specs->conflicting_specifiers_p)
15887 break;
15888 gcc_rich_location richloc (token->location);
15889 location_t oloc = decl_specs->locations[ds_storage_class];
15890 richloc.add_location_if_nearby (oloc);
15891 error_at (&richloc,
15892 "%<typedef%> specifier conflicts with %qs",
15893 cp_storage_class_name[decl_specs->storage_class]);
15894 decl_specs->conflicting_specifiers_p = true;
15896 break;
15898 /* storage-class-specifier:
15899 auto
15900 register
15901 static
15902 extern
15903 mutable
15905 GNU Extension:
15906 thread */
15907 case RID_AUTO:
15908 if (cxx_dialect == cxx98)
15910 /* Consume the token. */
15911 cp_lexer_consume_token (parser->lexer);
15913 /* Complain about `auto' as a storage specifier, if
15914 we're complaining about C++0x compatibility. */
15915 gcc_rich_location richloc (token->location);
15916 richloc.add_fixit_remove ();
15917 warning_at (&richloc, OPT_Wc__11_compat,
15918 "%<auto%> changes meaning in C++11; "
15919 "please remove it");
15921 /* Set the storage class anyway. */
15922 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
15923 token);
15925 else
15926 /* C++0x auto type-specifier. */
15927 found_decl_spec = false;
15928 break;
15930 case RID_REGISTER:
15931 case RID_STATIC:
15932 case RID_EXTERN:
15933 case RID_MUTABLE:
15934 /* Consume the token. */
15935 cp_lexer_consume_token (parser->lexer);
15936 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
15937 token);
15938 break;
15939 case RID_THREAD:
15940 /* Consume the token. */
15941 ds = ds_thread;
15942 cp_lexer_consume_token (parser->lexer);
15943 break;
15945 default:
15946 /* We did not yet find a decl-specifier yet. */
15947 found_decl_spec = false;
15948 break;
15951 if (found_decl_spec
15952 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
15953 && token->keyword != RID_CONSTEXPR)
15954 error ("%qD invalid in condition", ridpointers[token->keyword]);
15956 if (found_decl_spec
15957 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
15958 && token->keyword != RID_MUTABLE
15959 && token->keyword != RID_CONSTEXPR
15960 && token->keyword != RID_CONSTEVAL)
15962 if (token->keyword != RID_STATIC)
15963 error_at (token->location, "%qD invalid in lambda",
15964 ridpointers[token->keyword]);
15965 else if (cxx_dialect < cxx23)
15966 pedwarn (token->location, OPT_Wc__23_extensions,
15967 "%qD only valid in lambda with %<-std=c++23%> or "
15968 "%<-std=gnu++23%>", ridpointers[token->keyword]);
15971 if (ds != ds_last)
15972 set_and_check_decl_spec_loc (decl_specs, ds, token);
15974 /* Constructors are a special case. The `S' in `S()' is not a
15975 decl-specifier; it is the beginning of the declarator. */
15976 constructor_p
15977 = (!found_decl_spec
15978 && constructor_possible_p
15979 && (cp_parser_constructor_declarator_p
15980 (parser, flags, decl_spec_seq_has_spec_p (decl_specs,
15981 ds_friend))));
15983 /* If we don't have a DECL_SPEC yet, then we must be looking at
15984 a type-specifier. */
15985 if (!found_decl_spec && !constructor_p)
15987 int decl_spec_declares_class_or_enum;
15988 bool is_cv_qualifier;
15989 tree type_spec;
15991 if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
15992 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
15994 type_spec
15995 = cp_parser_type_specifier (parser, flags,
15996 decl_specs,
15997 /*is_declaration=*/true,
15998 &decl_spec_declares_class_or_enum,
15999 &is_cv_qualifier);
16000 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
16002 /* If this type-specifier referenced a user-defined type
16003 (a typedef, class-name, etc.), then we can't allow any
16004 more such type-specifiers henceforth.
16006 [dcl.spec]
16008 The longest sequence of decl-specifiers that could
16009 possibly be a type name is taken as the
16010 decl-specifier-seq of a declaration. The sequence shall
16011 be self-consistent as described below.
16013 [dcl.type]
16015 As a general rule, at most one type-specifier is allowed
16016 in the complete decl-specifier-seq of a declaration. The
16017 only exceptions are the following:
16019 -- const or volatile can be combined with any other
16020 type-specifier.
16022 -- signed or unsigned can be combined with char, long,
16023 short, or int.
16025 -- ..
16027 Example:
16029 typedef char* Pc;
16030 void g (const int Pc);
16032 Here, Pc is *not* part of the decl-specifier seq; it's
16033 the declarator. Therefore, once we see a type-specifier
16034 (other than a cv-qualifier), we forbid any additional
16035 user-defined types. We *do* still allow things like `int
16036 int' to be considered a decl-specifier-seq, and issue the
16037 error message later. */
16038 if (type_spec && !is_cv_qualifier)
16039 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
16040 /* A constructor declarator cannot follow a type-specifier. */
16041 if (type_spec)
16043 constructor_possible_p = false;
16044 found_decl_spec = true;
16045 if (!is_cv_qualifier)
16046 decl_specs->any_type_specifiers_p = true;
16048 if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) != 0)
16049 error_at (token->location, "type-specifier invalid in lambda");
16053 /* If we still do not have a DECL_SPEC, then there are no more
16054 decl-specifiers. */
16055 if (!found_decl_spec)
16056 break;
16058 if (decl_specs->std_attributes)
16060 error_at (decl_specs->locations[ds_std_attribute],
16061 "standard attributes in middle of decl-specifiers");
16062 inform (decl_specs->locations[ds_std_attribute],
16063 "standard attributes must precede the decl-specifiers to "
16064 "apply to the declaration, or follow them to apply to "
16065 "the type");
16068 decl_specs->any_specifiers_p = true;
16069 /* After we see one decl-specifier, further decl-specifiers are
16070 always optional. */
16071 flags |= CP_PARSER_FLAGS_OPTIONAL;
16074 /* Don't allow a friend specifier with a class definition. */
16075 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
16076 && (*declares_class_or_enum & 2))
16077 error_at (decl_specs->locations[ds_friend],
16078 "class definition may not be declared a friend");
16081 /* Parse an (optional) storage-class-specifier.
16083 storage-class-specifier:
16084 auto
16085 register
16086 static
16087 extern
16088 mutable
16090 GNU Extension:
16092 storage-class-specifier:
16093 thread
16095 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
16097 static tree
16098 cp_parser_storage_class_specifier_opt (cp_parser* parser)
16100 switch (cp_lexer_peek_token (parser->lexer)->keyword)
16102 case RID_AUTO:
16103 if (cxx_dialect != cxx98)
16104 return NULL_TREE;
16105 /* Fall through for C++98. */
16106 gcc_fallthrough ();
16108 case RID_REGISTER:
16109 case RID_STATIC:
16110 case RID_EXTERN:
16111 case RID_MUTABLE:
16112 case RID_THREAD:
16113 /* Consume the token. */
16114 return cp_lexer_consume_token (parser->lexer)->u.value;
16116 default:
16117 return NULL_TREE;
16121 /* Parse an (optional) function-specifier.
16123 function-specifier:
16124 inline
16125 virtual
16126 explicit
16128 C++20 Extension:
16129 explicit(constant-expression)
16131 Returns an IDENTIFIER_NODE corresponding to the keyword used.
16132 Updates DECL_SPECS, if it is non-NULL. */
16134 static tree
16135 cp_parser_function_specifier_opt (cp_parser* parser,
16136 cp_decl_specifier_seq *decl_specs)
16138 cp_token *token = cp_lexer_peek_token (parser->lexer);
16139 switch (token->keyword)
16141 case RID_INLINE:
16142 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
16143 break;
16145 case RID_VIRTUAL:
16146 /* 14.5.2.3 [temp.mem]
16148 A member function template shall not be virtual. */
16149 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
16150 && current_class_type)
16151 error_at (token->location, "templates may not be %<virtual%>");
16152 else
16153 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
16154 break;
16156 case RID_EXPLICIT:
16158 tree id = cp_lexer_consume_token (parser->lexer)->u.value;
16159 /* If we see '(', it's C++20 explicit(bool). */
16160 tree expr;
16161 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16163 matching_parens parens;
16164 parens.consume_open (parser);
16166 /* New types are not allowed in an explicit-specifier. */
16167 const char *saved_message
16168 = parser->type_definition_forbidden_message;
16169 parser->type_definition_forbidden_message
16170 = G_("types may not be defined in explicit-specifier");
16172 if (cxx_dialect < cxx20)
16173 pedwarn (token->location, OPT_Wc__20_extensions,
16174 "%<explicit(bool)%> only available with %<-std=c++20%> "
16175 "or %<-std=gnu++20%>");
16177 /* Parse the constant-expression. */
16178 expr = cp_parser_constant_expression (parser);
16180 /* Restore the saved message. */
16181 parser->type_definition_forbidden_message = saved_message;
16182 parens.require_close (parser);
16184 else
16185 /* The explicit-specifier explicit without a constant-expression is
16186 equivalent to the explicit-specifier explicit(true). */
16187 expr = boolean_true_node;
16189 /* [dcl.fct.spec]
16190 "the constant-expression, if supplied, shall be a contextually
16191 converted constant expression of type bool." */
16192 expr = build_explicit_specifier (expr, tf_warning_or_error);
16193 /* We could evaluate it -- mark the decl as appropriate. */
16194 if (expr == boolean_true_node)
16195 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
16196 else if (expr == boolean_false_node)
16197 /* Don't mark the decl as explicit. */;
16198 else if (decl_specs)
16199 /* The expression was value-dependent. Remember it so that we can
16200 substitute it later. */
16201 decl_specs->explicit_specifier = expr;
16202 return id;
16205 default:
16206 return NULL_TREE;
16209 /* Consume the token. */
16210 return cp_lexer_consume_token (parser->lexer)->u.value;
16213 /* Parse a linkage-specification.
16215 linkage-specification:
16216 extern string-literal { declaration-seq [opt] }
16217 extern string-literal declaration */
16219 static void
16220 cp_parser_linkage_specification (cp_parser* parser, tree prefix_attr)
16222 tree linkage;
16224 /* Look for the `extern' keyword. */
16225 cp_token *extern_token
16226 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
16228 /* Look for the string-literal. */
16229 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
16230 linkage = cp_parser_string_literal (parser, false, false);
16232 /* Transform the literal into an identifier. If the literal is a
16233 wide-character string, or contains embedded NULs, then we can't
16234 handle it as the user wants. */
16235 if (linkage == error_mark_node
16236 || strlen (TREE_STRING_POINTER (linkage))
16237 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
16239 cp_parser_error (parser, "invalid linkage-specification");
16240 /* Assume C++ linkage. */
16241 linkage = lang_name_cplusplus;
16243 else
16244 linkage = get_identifier (TREE_STRING_POINTER (linkage));
16246 /* We're now using the new linkage. */
16247 unsigned saved_module = module_kind;
16248 module_kind &= ~MK_ATTACH;
16249 push_lang_context (linkage);
16251 /* Preserve the location of the innermost linkage specification,
16252 tracking the locations of nested specifications via a local. */
16253 location_t saved_location
16254 = parser->innermost_linkage_specification_location;
16255 /* Construct a location ranging from the start of the "extern" to
16256 the end of the string-literal, with the caret at the start, e.g.:
16257 extern "C" {
16258 ^~~~~~~~~~
16260 parser->innermost_linkage_specification_location
16261 = make_location (extern_token->location,
16262 extern_token->location,
16263 get_finish (string_token->location));
16265 /* If the next token is a `{', then we're using the first
16266 production. */
16267 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16269 cp_ensure_no_omp_declare_simd (parser);
16270 cp_ensure_no_oacc_routine (parser);
16272 /* Consume the `{' token. */
16273 matching_braces braces;
16274 braces.consume_open (parser);
16275 /* Parse the declarations. */
16276 cp_parser_declaration_seq_opt (parser);
16277 /* Look for the closing `}'. */
16278 braces.require_close (parser);
16280 /* Otherwise, there's just one declaration. */
16281 else
16283 bool saved_in_unbraced_linkage_specification_p;
16285 saved_in_unbraced_linkage_specification_p
16286 = parser->in_unbraced_linkage_specification_p;
16287 parser->in_unbraced_linkage_specification_p = true;
16288 cp_parser_declaration (parser, prefix_attr);
16289 parser->in_unbraced_linkage_specification_p
16290 = saved_in_unbraced_linkage_specification_p;
16293 /* We're done with the linkage-specification. */
16294 pop_lang_context ();
16295 module_kind = saved_module;
16297 /* Restore location of parent linkage specification, if any. */
16298 parser->innermost_linkage_specification_location = saved_location;
16301 /* Parse a static_assert-declaration.
16303 static_assert-declaration:
16304 static_assert ( constant-expression , string-literal ) ;
16305 static_assert ( constant-expression ) ; (C++17)
16307 If MEMBER_P, this static_assert is a class member. */
16309 static void
16310 cp_parser_static_assert(cp_parser *parser, bool member_p)
16312 cp_expr condition;
16313 location_t token_loc;
16314 tree message;
16315 bool dummy;
16317 /* Peek at the `static_assert' token so we can keep track of exactly
16318 where the static assertion started. */
16319 token_loc = cp_lexer_peek_token (parser->lexer)->location;
16321 /* Look for the `static_assert' keyword. */
16322 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
16323 RT_STATIC_ASSERT))
16324 return;
16326 /* We know we are in a static assertion; commit to any tentative
16327 parse. */
16328 if (cp_parser_parsing_tentatively (parser))
16329 cp_parser_commit_to_tentative_parse (parser);
16331 /* Parse the `(' starting the static assertion condition. */
16332 matching_parens parens;
16333 parens.require_open (parser);
16335 /* Parse the constant-expression. Allow a non-constant expression
16336 here in order to give better diagnostics in finish_static_assert. */
16337 condition =
16338 cp_parser_constant_expression (parser,
16339 /*allow_non_constant_p=*/true,
16340 /*non_constant_p=*/&dummy);
16342 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
16344 if (pedantic && cxx_dialect < cxx17)
16345 pedwarn (input_location, OPT_Wc__17_extensions,
16346 "%<static_assert%> without a message "
16347 "only available with %<-std=c++17%> or %<-std=gnu++17%>");
16348 /* Eat the ')' */
16349 cp_lexer_consume_token (parser->lexer);
16350 message = build_string (1, "");
16351 TREE_TYPE (message) = char_array_type_node;
16352 fix_string_type (message);
16354 else
16356 /* Parse the separating `,'. */
16357 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
16359 /* Parse the string-literal message. */
16360 message = cp_parser_string_literal (parser,
16361 /*translate=*/false,
16362 /*wide_ok=*/true);
16364 /* A `)' completes the static assertion. */
16365 if (!parens.require_close (parser))
16366 cp_parser_skip_to_closing_parenthesis (parser,
16367 /*recovering=*/true,
16368 /*or_comma=*/false,
16369 /*consume_paren=*/true);
16372 /* A semicolon terminates the declaration. */
16373 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16375 /* Get the location for the static assertion. Use that of the
16376 condition if available, otherwise, use that of the "static_assert"
16377 token. */
16378 location_t assert_loc = condition.get_location ();
16379 if (assert_loc == UNKNOWN_LOCATION)
16380 assert_loc = token_loc;
16382 /* Complete the static assertion, which may mean either processing
16383 the static assert now or saving it for template instantiation. */
16384 finish_static_assert (condition, message, assert_loc, member_p,
16385 /*show_expr_p=*/false);
16388 /* Parse the expression in decltype ( expression ). */
16390 static tree
16391 cp_parser_decltype_expr (cp_parser *parser,
16392 bool &id_expression_or_member_access_p)
16394 cp_token *id_expr_start_token;
16395 tree expr;
16397 /* First, try parsing an id-expression. */
16398 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
16399 cp_parser_parse_tentatively (parser);
16400 expr = cp_parser_id_expression (parser,
16401 /*template_keyword_p=*/false,
16402 /*check_dependency_p=*/true,
16403 /*template_p=*/NULL,
16404 /*declarator_p=*/false,
16405 /*optional_p=*/false);
16407 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
16409 bool non_integral_constant_expression_p = false;
16410 tree id_expression = expr;
16411 cp_id_kind idk;
16412 const char *error_msg;
16414 if (identifier_p (expr))
16415 /* Lookup the name we got back from the id-expression. */
16416 expr = cp_parser_lookup_name_simple (parser, expr,
16417 id_expr_start_token->location);
16419 if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
16420 /* A template without args is not a complete id-expression. */
16421 expr = error_mark_node;
16423 if (expr
16424 && expr != error_mark_node
16425 && TREE_CODE (expr) != TYPE_DECL
16426 && (TREE_CODE (expr) != BIT_NOT_EXPR
16427 || !TYPE_P (TREE_OPERAND (expr, 0)))
16428 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
16430 /* Complete lookup of the id-expression. */
16431 expr = (finish_id_expression
16432 (id_expression, expr, parser->scope, &idk,
16433 /*integral_constant_expression_p=*/false,
16434 /*allow_non_integral_constant_expression_p=*/true,
16435 &non_integral_constant_expression_p,
16436 /*template_p=*/false,
16437 /*done=*/true,
16438 /*address_p=*/false,
16439 /*template_arg_p=*/false,
16440 &error_msg,
16441 id_expr_start_token->location));
16443 if (expr == error_mark_node)
16444 /* We found an id-expression, but it was something that we
16445 should not have found. This is an error, not something
16446 we can recover from, so note that we found an
16447 id-expression and we'll recover as gracefully as
16448 possible. */
16449 id_expression_or_member_access_p = true;
16452 if (expr
16453 && expr != error_mark_node
16454 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
16455 /* We have an id-expression. */
16456 id_expression_or_member_access_p = true;
16459 if (!id_expression_or_member_access_p)
16461 /* Abort the id-expression parse. */
16462 cp_parser_abort_tentative_parse (parser);
16464 /* Parsing tentatively, again. */
16465 cp_parser_parse_tentatively (parser);
16467 /* Parse a class member access. */
16468 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
16469 /*cast_p=*/false, /*decltype*/true,
16470 /*member_access_only_p=*/true, NULL);
16472 if (expr
16473 && expr != error_mark_node
16474 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
16475 /* We have an id-expression. */
16476 id_expression_or_member_access_p = true;
16479 if (id_expression_or_member_access_p)
16480 /* We have parsed the complete id-expression or member access. */
16481 cp_parser_parse_definitely (parser);
16482 else
16484 /* Abort our attempt to parse an id-expression or member access
16485 expression. */
16486 cp_parser_abort_tentative_parse (parser);
16488 /* Parse a full expression. */
16489 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
16490 /*decltype_p=*/true);
16493 return expr;
16496 /* Parse a `decltype' type. Returns the type.
16498 decltype-specifier:
16499 decltype ( expression )
16500 C++14:
16501 decltype ( auto ) */
16503 static tree
16504 cp_parser_decltype (cp_parser *parser)
16506 bool id_expression_or_member_access_p = false;
16507 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
16509 if (start_token->type == CPP_DECLTYPE)
16511 /* Already parsed. */
16512 cp_lexer_consume_token (parser->lexer);
16513 return saved_checks_value (start_token->u.tree_check_value);
16516 /* Look for the `decltype' token. */
16517 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
16518 return error_mark_node;
16520 /* Parse the opening `('. */
16521 matching_parens parens;
16522 if (!parens.require_open (parser))
16523 return error_mark_node;
16525 /* Since we're going to preserve any side-effects from this parse, set up a
16526 firewall to protect our callers from cp_parser_commit_to_tentative_parse
16527 in the expression. */
16528 tentative_firewall firewall (parser);
16530 /* If in_declarator_p, a reparse as an expression might succeed (60361).
16531 Otherwise, commit now for better diagnostics. */
16532 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
16533 && !parser->in_declarator_p)
16534 cp_parser_commit_to_topmost_tentative_parse (parser);
16536 push_deferring_access_checks (dk_deferred);
16538 tree expr = NULL_TREE;
16540 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO)
16541 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
16543 /* decltype (auto) */
16544 cp_lexer_consume_token (parser->lexer);
16545 if (cxx_dialect < cxx14)
16547 error_at (start_token->location,
16548 "%<decltype(auto)%> type specifier only available with "
16549 "%<-std=c++14%> or %<-std=gnu++14%>");
16550 expr = error_mark_node;
16553 else
16555 /* decltype (expression) */
16557 /* Types cannot be defined in a `decltype' expression. Save away the
16558 old message and set the new one. */
16559 const char *saved_message = parser->type_definition_forbidden_message;
16560 parser->type_definition_forbidden_message
16561 = G_("types may not be defined in %<decltype%> expressions");
16563 /* The restrictions on constant-expressions do not apply inside
16564 decltype expressions. */
16565 bool saved_integral_constant_expression_p
16566 = parser->integral_constant_expression_p;
16567 bool saved_non_integral_constant_expression_p
16568 = parser->non_integral_constant_expression_p;
16569 parser->integral_constant_expression_p = false;
16571 /* Within a parenthesized expression, a `>' token is always
16572 the greater-than operator. */
16573 bool saved_greater_than_is_operator_p
16574 = parser->greater_than_is_operator_p;
16575 parser->greater_than_is_operator_p = true;
16577 /* Don't synthesize an implicit template type parameter here. This
16578 could happen with C++23 code like
16580 void f(decltype(new auto{0}));
16582 where we want to deduce the auto right away so that the parameter
16583 is of type 'int *'. */
16584 auto cleanup = make_temp_override
16585 (parser->auto_is_implicit_function_template_parm_p, false);
16587 /* Do not actually evaluate the expression. */
16588 ++cp_unevaluated_operand;
16590 /* Do not warn about problems with the expression. */
16591 ++c_inhibit_evaluation_warnings;
16593 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
16594 STRIP_ANY_LOCATION_WRAPPER (expr);
16596 /* Go back to evaluating expressions. */
16597 --cp_unevaluated_operand;
16598 --c_inhibit_evaluation_warnings;
16600 /* The `>' token might be the end of a template-id or
16601 template-parameter-list now. */
16602 parser->greater_than_is_operator_p
16603 = saved_greater_than_is_operator_p;
16605 /* Restore the old message and the integral constant expression
16606 flags. */
16607 parser->type_definition_forbidden_message = saved_message;
16608 parser->integral_constant_expression_p
16609 = saved_integral_constant_expression_p;
16610 parser->non_integral_constant_expression_p
16611 = saved_non_integral_constant_expression_p;
16614 /* Parse to the closing `)'. */
16615 if (expr == error_mark_node || !parens.require_close (parser))
16617 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16618 /*consume_paren=*/true);
16619 expr = error_mark_node;
16622 /* If we got a parse error while tentative, bail out now. */
16623 if (cp_parser_error_occurred (parser))
16625 pop_deferring_access_checks ();
16626 return error_mark_node;
16629 if (!expr)
16630 /* Build auto. */
16631 expr = make_decltype_auto ();
16632 else
16633 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
16634 tf_warning_or_error);
16636 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
16637 it again. */
16638 start_token->type = CPP_DECLTYPE;
16639 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
16640 start_token->tree_check_p = true;
16641 start_token->u.tree_check_value->value = expr;
16642 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
16643 start_token->keyword = RID_MAX;
16645 location_t loc = start_token->location;
16646 loc = make_location (loc, loc, parser->lexer);
16647 start_token->location = loc;
16649 cp_lexer_purge_tokens_after (parser->lexer, start_token);
16651 pop_to_parent_deferring_access_checks ();
16653 return expr;
16656 /* Special member functions [gram.special] */
16658 /* Parse a conversion-function-id.
16660 conversion-function-id:
16661 operator conversion-type-id
16663 Returns an IDENTIFIER_NODE representing the operator. */
16665 static tree
16666 cp_parser_conversion_function_id (cp_parser* parser)
16668 tree type;
16669 tree saved_scope;
16670 tree saved_qualifying_scope;
16671 tree saved_object_scope;
16672 tree pushed_scope = NULL_TREE;
16674 /* Look for the `operator' token. */
16675 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
16676 return error_mark_node;
16677 /* When we parse the conversion-type-id, the current scope will be
16678 reset. However, we need that information in able to look up the
16679 conversion function later, so we save it here. */
16680 saved_scope = parser->scope;
16681 saved_qualifying_scope = parser->qualifying_scope;
16682 saved_object_scope = parser->object_scope;
16683 /* We must enter the scope of the class so that the names of
16684 entities declared within the class are available in the
16685 conversion-type-id. For example, consider:
16687 struct S {
16688 typedef int I;
16689 operator I();
16692 S::operator I() { ... }
16694 In order to see that `I' is a type-name in the definition, we
16695 must be in the scope of `S'. */
16696 if (saved_scope)
16697 pushed_scope = push_scope (saved_scope);
16698 /* Parse the conversion-type-id. */
16699 type = cp_parser_conversion_type_id (parser);
16700 /* Leave the scope of the class, if any. */
16701 if (pushed_scope)
16702 pop_scope (pushed_scope);
16703 /* Restore the saved scope. */
16704 parser->scope = saved_scope;
16705 parser->qualifying_scope = saved_qualifying_scope;
16706 parser->object_scope = saved_object_scope;
16707 /* If the TYPE is invalid, indicate failure. */
16708 if (type == error_mark_node)
16709 return error_mark_node;
16710 return make_conv_op_name (type);
16713 /* Parse a conversion-type-id:
16715 conversion-type-id:
16716 type-specifier-seq conversion-declarator [opt]
16718 Returns the TYPE specified. */
16720 static tree
16721 cp_parser_conversion_type_id (cp_parser* parser)
16723 tree attributes;
16724 cp_decl_specifier_seq type_specifiers;
16725 cp_declarator *declarator;
16726 tree type_specified;
16727 const char *saved_message;
16729 /* Parse the attributes. */
16730 attributes = cp_parser_attributes_opt (parser);
16732 saved_message = parser->type_definition_forbidden_message;
16733 parser->type_definition_forbidden_message
16734 = G_("types may not be defined in a conversion-type-id");
16736 /* Parse the type-specifiers. DR 2413 clarifies that `typename' is
16737 optional in conversion-type-id. */
16738 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
16739 /*is_declaration=*/false,
16740 /*is_trailing_return=*/false,
16741 &type_specifiers);
16743 parser->type_definition_forbidden_message = saved_message;
16745 /* If that didn't work, stop. */
16746 if (type_specifiers.type == error_mark_node)
16747 return error_mark_node;
16748 /* Parse the conversion-declarator. */
16749 declarator = cp_parser_conversion_declarator_opt (parser);
16751 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
16752 /*initialized=*/0, &attributes);
16753 if (attributes)
16754 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
16756 /* Don't give this error when parsing tentatively. This happens to
16757 work because we always parse this definitively once. */
16758 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
16759 && type_uses_auto (type_specified))
16761 if (cxx_dialect < cxx14)
16763 error ("invalid use of %<auto%> in conversion operator");
16764 return error_mark_node;
16766 else if (template_parm_scope_p ())
16767 warning (0, "use of %<auto%> in member template "
16768 "conversion operator can never be deduced");
16771 return type_specified;
16774 /* Parse an (optional) conversion-declarator.
16776 conversion-declarator:
16777 ptr-operator conversion-declarator [opt]
16781 static cp_declarator *
16782 cp_parser_conversion_declarator_opt (cp_parser* parser)
16784 enum tree_code code;
16785 tree class_type, std_attributes = NULL_TREE;
16786 cp_cv_quals cv_quals;
16788 /* We don't know if there's a ptr-operator next, or not. */
16789 cp_parser_parse_tentatively (parser);
16790 /* Try the ptr-operator. */
16791 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
16792 &std_attributes);
16793 /* If it worked, look for more conversion-declarators. */
16794 if (cp_parser_parse_definitely (parser))
16796 cp_declarator *declarator;
16798 /* Parse another optional declarator. */
16799 declarator = cp_parser_conversion_declarator_opt (parser);
16801 declarator = cp_parser_make_indirect_declarator
16802 (code, class_type, cv_quals, declarator, std_attributes);
16804 return declarator;
16807 return NULL;
16810 /* Parse an (optional) ctor-initializer.
16812 ctor-initializer:
16813 : mem-initializer-list */
16815 static void
16816 cp_parser_ctor_initializer_opt (cp_parser* parser)
16818 /* If the next token is not a `:', then there is no
16819 ctor-initializer. */
16820 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16822 /* Do default initialization of any bases and members. */
16823 if (DECL_CONSTRUCTOR_P (current_function_decl))
16824 finish_mem_initializers (NULL_TREE);
16825 return;
16828 /* Consume the `:' token. */
16829 cp_lexer_consume_token (parser->lexer);
16830 /* And the mem-initializer-list. */
16831 cp_parser_mem_initializer_list (parser);
16834 /* Parse a mem-initializer-list.
16836 mem-initializer-list:
16837 mem-initializer ... [opt]
16838 mem-initializer ... [opt] , mem-initializer-list */
16840 static void
16841 cp_parser_mem_initializer_list (cp_parser* parser)
16843 tree mem_initializer_list = NULL_TREE;
16844 tree target_ctor = error_mark_node;
16845 cp_token *token = cp_lexer_peek_token (parser->lexer);
16847 /* Let the semantic analysis code know that we are starting the
16848 mem-initializer-list. */
16849 if (!DECL_CONSTRUCTOR_P (current_function_decl))
16850 error_at (token->location,
16851 "only constructors take member initializers");
16853 /* Loop through the list. */
16854 while (true)
16856 tree mem_initializer;
16858 token = cp_lexer_peek_token (parser->lexer);
16859 /* Parse the mem-initializer. */
16860 mem_initializer = cp_parser_mem_initializer (parser);
16861 /* If the next token is a `...', we're expanding member initializers. */
16862 bool ellipsis = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
16863 if (ellipsis
16864 || (mem_initializer != error_mark_node
16865 && check_for_bare_parameter_packs (TREE_PURPOSE
16866 (mem_initializer))))
16868 /* Consume the `...'. */
16869 if (ellipsis)
16870 cp_lexer_consume_token (parser->lexer);
16872 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
16873 can be expanded but members cannot. */
16874 if (mem_initializer != error_mark_node
16875 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
16877 error_at (token->location,
16878 "cannot expand initializer for member %qD",
16879 TREE_PURPOSE (mem_initializer));
16880 mem_initializer = error_mark_node;
16883 /* Construct the pack expansion type. */
16884 if (mem_initializer != error_mark_node)
16885 mem_initializer = make_pack_expansion (mem_initializer);
16887 if (target_ctor != error_mark_node
16888 && mem_initializer != error_mark_node)
16890 error ("mem-initializer for %qD follows constructor delegation",
16891 TREE_PURPOSE (mem_initializer));
16892 mem_initializer = error_mark_node;
16894 /* Look for a target constructor. */
16895 if (mem_initializer != error_mark_node
16896 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
16897 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
16899 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
16900 if (mem_initializer_list)
16902 error ("constructor delegation follows mem-initializer for %qD",
16903 TREE_PURPOSE (mem_initializer_list));
16904 mem_initializer = error_mark_node;
16906 target_ctor = mem_initializer;
16908 /* Add it to the list, unless it was erroneous. */
16909 if (mem_initializer != error_mark_node)
16911 TREE_CHAIN (mem_initializer) = mem_initializer_list;
16912 mem_initializer_list = mem_initializer;
16914 /* If the next token is not a `,', we're done. */
16915 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16916 break;
16917 /* Consume the `,' token. */
16918 cp_lexer_consume_token (parser->lexer);
16921 /* Perform semantic analysis. */
16922 if (DECL_CONSTRUCTOR_P (current_function_decl))
16923 finish_mem_initializers (mem_initializer_list);
16926 /* Parse a mem-initializer.
16928 mem-initializer:
16929 mem-initializer-id ( expression-list [opt] )
16930 mem-initializer-id braced-init-list
16932 GNU extension:
16934 mem-initializer:
16935 ( expression-list [opt] )
16937 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
16938 class) or FIELD_DECL (for a non-static data member) to initialize;
16939 the TREE_VALUE is the expression-list. An empty initialization
16940 list is represented by void_list_node. */
16942 static tree
16943 cp_parser_mem_initializer (cp_parser* parser)
16945 tree mem_initializer_id;
16946 tree expression_list;
16947 tree member;
16948 cp_token *token = cp_lexer_peek_token (parser->lexer);
16950 /* Find out what is being initialized. */
16951 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16953 permerror (token->location,
16954 "anachronistic old-style base class initializer");
16955 mem_initializer_id = NULL_TREE;
16957 else
16959 mem_initializer_id = cp_parser_mem_initializer_id (parser);
16960 if (mem_initializer_id == error_mark_node)
16961 return mem_initializer_id;
16963 member = expand_member_init (mem_initializer_id);
16964 if (member && !DECL_P (member))
16965 in_base_initializer = 1;
16967 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16969 bool expr_non_constant_p;
16970 cp_lexer_set_source_position (parser->lexer);
16971 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
16972 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
16973 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
16974 expression_list = build_tree_list (NULL_TREE, expression_list);
16976 else
16978 vec<tree, va_gc> *vec;
16979 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
16980 /*cast_p=*/false,
16981 /*allow_expansion_p=*/true,
16982 /*non_constant_p=*/NULL,
16983 /*close_paren_loc=*/NULL,
16984 /*wrap_locations_p=*/true);
16985 if (vec == NULL)
16986 return error_mark_node;
16987 expression_list = build_tree_list_vec (vec);
16988 release_tree_vector (vec);
16991 if (expression_list == error_mark_node)
16992 return error_mark_node;
16993 if (!expression_list)
16994 expression_list = void_type_node;
16996 in_base_initializer = 0;
16998 if (!member)
16999 return error_mark_node;
17000 tree node = build_tree_list (member, expression_list);
17002 /* We can't attach the source location of this initializer directly to
17003 the list node, so we instead attach it to a dummy EMPTY_CLASS_EXPR
17004 within the TREE_TYPE of the list node. */
17005 location_t loc
17006 = make_location (token->location, token->location, parser->lexer);
17007 tree dummy = build0 (EMPTY_CLASS_EXPR, NULL_TREE);
17008 SET_EXPR_LOCATION (dummy, loc);
17009 TREE_TYPE (node) = dummy;
17011 return node;
17014 /* Parse a mem-initializer-id.
17016 mem-initializer-id:
17017 :: [opt] nested-name-specifier [opt] class-name
17018 decltype-specifier (C++11)
17019 identifier
17021 Returns a TYPE indicating the class to be initialized for the first
17022 production (and the second in C++11). Returns an IDENTIFIER_NODE
17023 indicating the data member to be initialized for the last production. */
17025 static tree
17026 cp_parser_mem_initializer_id (cp_parser* parser)
17028 bool global_scope_p;
17029 bool nested_name_specifier_p;
17030 bool template_p = false;
17031 tree id;
17033 cp_token *token = cp_lexer_peek_token (parser->lexer);
17035 /* `typename' is not allowed in this context ([temp.res]). */
17036 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
17038 error_at (token->location,
17039 "keyword %<typename%> not allowed in this context (a qualified "
17040 "member initializer is implicitly a type)");
17041 cp_lexer_consume_token (parser->lexer);
17043 /* Look for the optional `::' operator. */
17044 global_scope_p
17045 = (cp_parser_global_scope_opt (parser,
17046 /*current_scope_valid_p=*/false)
17047 != NULL_TREE);
17048 /* Look for the optional nested-name-specifier. The simplest way to
17049 implement:
17051 [temp.res]
17053 The keyword `typename' is not permitted in a base-specifier or
17054 mem-initializer; in these contexts a qualified name that
17055 depends on a template-parameter is implicitly assumed to be a
17056 type name.
17058 is to assume that we have seen the `typename' keyword at this
17059 point. */
17060 nested_name_specifier_p
17061 = (cp_parser_nested_name_specifier_opt (parser,
17062 /*typename_keyword_p=*/true,
17063 /*check_dependency_p=*/true,
17064 /*type_p=*/true,
17065 /*is_declaration=*/true)
17066 != NULL_TREE);
17067 if (nested_name_specifier_p)
17068 template_p = cp_parser_optional_template_keyword (parser);
17069 /* If there is a `::' operator or a nested-name-specifier, then we
17070 are definitely looking for a class-name. */
17071 if (global_scope_p || nested_name_specifier_p)
17072 return cp_parser_class_name (parser,
17073 /*typename_keyword_p=*/true,
17074 /*template_keyword_p=*/template_p,
17075 typename_type,
17076 /*check_dependency_p=*/true,
17077 /*class_head_p=*/false,
17078 /*is_declaration=*/true);
17079 /* Otherwise, we could also be looking for an ordinary identifier. */
17080 cp_parser_parse_tentatively (parser);
17081 if (cp_lexer_next_token_is_decltype (parser->lexer))
17082 /* Try a decltype-specifier. */
17083 id = cp_parser_decltype (parser);
17084 else
17085 /* Otherwise, try a class-name. */
17086 id = cp_parser_class_name (parser,
17087 /*typename_keyword_p=*/true,
17088 /*template_keyword_p=*/false,
17089 none_type,
17090 /*check_dependency_p=*/true,
17091 /*class_head_p=*/false,
17092 /*is_declaration=*/true);
17093 /* If we found one, we're done. */
17094 if (cp_parser_parse_definitely (parser))
17095 return id;
17096 /* Otherwise, look for an ordinary identifier. */
17097 return cp_parser_identifier (parser);
17100 /* Overloading [gram.over] */
17102 /* Parse an operator-function-id.
17104 operator-function-id:
17105 operator operator
17107 Returns an IDENTIFIER_NODE for the operator which is a
17108 human-readable spelling of the identifier, e.g., `operator +'. */
17110 static cp_expr
17111 cp_parser_operator_function_id (cp_parser* parser)
17113 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
17114 /* Look for the `operator' keyword. */
17115 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
17116 return error_mark_node;
17117 /* And then the name of the operator itself. */
17118 return cp_parser_operator (parser, start_loc);
17121 /* Return an identifier node for a user-defined literal operator.
17122 The suffix identifier is chained to the operator name identifier. */
17124 tree
17125 cp_literal_operator_id (const char* name)
17127 tree identifier;
17128 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
17129 + strlen (name) + 10);
17130 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
17131 identifier = get_identifier (buffer);
17132 XDELETEVEC (buffer);
17134 return identifier;
17137 /* Parse an operator.
17139 operator:
17140 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
17141 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
17142 || ++ -- , ->* -> () []
17144 GNU Extensions:
17146 operator:
17147 <? >? <?= >?=
17149 Returns an IDENTIFIER_NODE for the operator which is a
17150 human-readable spelling of the identifier, e.g., `operator +'. */
17152 static cp_expr
17153 cp_parser_operator (cp_parser* parser, location_t start_loc)
17155 tree id = NULL_TREE;
17156 cp_token *token;
17157 bool utf8 = false;
17159 /* Peek at the next token. */
17160 token = cp_lexer_peek_token (parser->lexer);
17162 location_t end_loc = token->location;
17164 /* Figure out which operator we have. */
17165 enum tree_code op = ERROR_MARK;
17166 bool assop = false;
17167 bool consumed = false;
17168 switch (token->type)
17170 case CPP_KEYWORD:
17172 /* The keyword should be either `new', `delete' or `co_await'. */
17173 if (token->keyword == RID_NEW)
17174 op = NEW_EXPR;
17175 else if (token->keyword == RID_DELETE)
17176 op = DELETE_EXPR;
17177 else if (token->keyword == RID_CO_AWAIT)
17178 op = CO_AWAIT_EXPR;
17179 else
17180 break;
17182 /* Consume the `new', `delete' or co_await token. */
17183 end_loc = cp_lexer_consume_token (parser->lexer)->location;
17185 /* Peek at the next token. */
17186 token = cp_lexer_peek_token (parser->lexer);
17187 /* If it's a `[' token then this is the array variant of the
17188 operator. */
17189 if (token->type == CPP_OPEN_SQUARE
17190 && op != CO_AWAIT_EXPR)
17192 /* Consume the `[' token. */
17193 cp_lexer_consume_token (parser->lexer);
17194 /* Look for the `]' token. */
17195 if (cp_token *close_token
17196 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
17197 end_loc = close_token->location;
17198 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
17200 consumed = true;
17201 break;
17204 case CPP_PLUS:
17205 op = PLUS_EXPR;
17206 break;
17208 case CPP_MINUS:
17209 op = MINUS_EXPR;
17210 break;
17212 case CPP_MULT:
17213 op = MULT_EXPR;
17214 break;
17216 case CPP_DIV:
17217 op = TRUNC_DIV_EXPR;
17218 break;
17220 case CPP_MOD:
17221 op = TRUNC_MOD_EXPR;
17222 break;
17224 case CPP_XOR:
17225 op = BIT_XOR_EXPR;
17226 break;
17228 case CPP_AND:
17229 op = BIT_AND_EXPR;
17230 break;
17232 case CPP_OR:
17233 op = BIT_IOR_EXPR;
17234 break;
17236 case CPP_COMPL:
17237 op = BIT_NOT_EXPR;
17238 break;
17240 case CPP_NOT:
17241 op = TRUTH_NOT_EXPR;
17242 break;
17244 case CPP_EQ:
17245 assop = true;
17246 op = NOP_EXPR;
17247 break;
17249 case CPP_LESS:
17250 op = LT_EXPR;
17251 break;
17253 case CPP_GREATER:
17254 op = GT_EXPR;
17255 break;
17257 case CPP_PLUS_EQ:
17258 assop = true;
17259 op = PLUS_EXPR;
17260 break;
17262 case CPP_MINUS_EQ:
17263 assop = true;
17264 op = MINUS_EXPR;
17265 break;
17267 case CPP_MULT_EQ:
17268 assop = true;
17269 op = MULT_EXPR;
17270 break;
17272 case CPP_DIV_EQ:
17273 assop = true;
17274 op = TRUNC_DIV_EXPR;
17275 break;
17277 case CPP_MOD_EQ:
17278 assop = true;
17279 op = TRUNC_MOD_EXPR;
17280 break;
17282 case CPP_XOR_EQ:
17283 assop = true;
17284 op = BIT_XOR_EXPR;
17285 break;
17287 case CPP_AND_EQ:
17288 assop = true;
17289 op = BIT_AND_EXPR;
17290 break;
17292 case CPP_OR_EQ:
17293 assop = true;
17294 op = BIT_IOR_EXPR;
17295 break;
17297 case CPP_LSHIFT:
17298 op = LSHIFT_EXPR;
17299 break;
17301 case CPP_RSHIFT:
17302 op = RSHIFT_EXPR;
17303 break;
17305 case CPP_LSHIFT_EQ:
17306 assop = true;
17307 op = LSHIFT_EXPR;
17308 break;
17310 case CPP_RSHIFT_EQ:
17311 assop = true;
17312 op = RSHIFT_EXPR;
17313 break;
17315 case CPP_EQ_EQ:
17316 op = EQ_EXPR;
17317 break;
17319 case CPP_NOT_EQ:
17320 op = NE_EXPR;
17321 break;
17323 case CPP_LESS_EQ:
17324 op = LE_EXPR;
17325 break;
17327 case CPP_GREATER_EQ:
17328 op = GE_EXPR;
17329 break;
17331 case CPP_SPACESHIP:
17332 op = SPACESHIP_EXPR;
17333 break;
17335 case CPP_AND_AND:
17336 op = TRUTH_ANDIF_EXPR;
17337 break;
17339 case CPP_OR_OR:
17340 op = TRUTH_ORIF_EXPR;
17341 break;
17343 case CPP_PLUS_PLUS:
17344 op = POSTINCREMENT_EXPR;
17345 break;
17347 case CPP_MINUS_MINUS:
17348 op = PREDECREMENT_EXPR;
17349 break;
17351 case CPP_COMMA:
17352 op = COMPOUND_EXPR;
17353 break;
17355 case CPP_DEREF_STAR:
17356 op = MEMBER_REF;
17357 break;
17359 case CPP_DEREF:
17360 op = COMPONENT_REF;
17361 break;
17363 case CPP_QUERY:
17364 op = COND_EXPR;
17365 /* Consume the `?'. */
17366 cp_lexer_consume_token (parser->lexer);
17367 /* Look for the matching `:'. */
17368 cp_parser_require (parser, CPP_COLON, RT_COLON);
17369 consumed = true;
17370 break;
17372 case CPP_OPEN_PAREN:
17374 /* Consume the `('. */
17375 matching_parens parens;
17376 parens.consume_open (parser);
17377 /* Look for the matching `)'. */
17378 token = parens.require_close (parser);
17379 if (token)
17380 end_loc = token->location;
17381 op = CALL_EXPR;
17382 consumed = true;
17383 break;
17386 case CPP_OPEN_SQUARE:
17387 /* Consume the `['. */
17388 cp_lexer_consume_token (parser->lexer);
17389 /* Look for the matching `]'. */
17390 token = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
17391 if (token)
17392 end_loc = token->location;
17393 op = ARRAY_REF;
17394 consumed = true;
17395 break;
17397 case CPP_UTF8STRING:
17398 case CPP_UTF8STRING_USERDEF:
17399 utf8 = true;
17400 /* FALLTHRU */
17401 case CPP_STRING:
17402 case CPP_WSTRING:
17403 case CPP_STRING16:
17404 case CPP_STRING32:
17405 case CPP_STRING_USERDEF:
17406 case CPP_WSTRING_USERDEF:
17407 case CPP_STRING16_USERDEF:
17408 case CPP_STRING32_USERDEF:
17410 cp_expr str;
17411 tree string_tree;
17412 int sz, len;
17414 if (cxx_dialect == cxx98)
17415 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
17417 /* Consume the string. */
17418 str = cp_parser_string_literal (parser, /*translate=*/true,
17419 /*wide_ok=*/true, /*lookup_udlit=*/false);
17420 if (str == error_mark_node)
17421 return error_mark_node;
17422 else if (TREE_CODE (str) == USERDEF_LITERAL)
17424 string_tree = USERDEF_LITERAL_VALUE (str.get_value ());
17425 id = USERDEF_LITERAL_SUFFIX_ID (str.get_value ());
17426 end_loc = str.get_location ();
17428 else
17430 string_tree = str;
17431 /* Look for the suffix identifier. */
17432 token = cp_lexer_peek_token (parser->lexer);
17433 if (token->type == CPP_NAME)
17435 id = cp_parser_identifier (parser);
17436 end_loc = token->location;
17438 else if (token->type == CPP_KEYWORD)
17440 error ("unexpected keyword;"
17441 " remove space between quotes and suffix identifier");
17442 return error_mark_node;
17444 else
17446 error ("expected suffix identifier");
17447 return error_mark_node;
17450 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
17451 (TREE_TYPE (TREE_TYPE (string_tree))));
17452 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
17453 if (len != 0)
17455 error ("expected empty string after %<operator%> keyword");
17456 return error_mark_node;
17458 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
17459 != char_type_node)
17461 error ("invalid encoding prefix in literal operator");
17462 return error_mark_node;
17464 if (id != error_mark_node)
17466 const char *name = IDENTIFIER_POINTER (id);
17467 id = cp_literal_operator_id (name);
17469 /* Generate a location of the form:
17470 "" _suffix_identifier
17471 ^~~~~~~~~~~~~~~~~~~~~
17472 with caret == start at the start token, finish at the end of the
17473 suffix identifier. */
17474 location_t combined_loc
17475 = make_location (start_loc, start_loc, parser->lexer);
17476 return cp_expr (id, combined_loc);
17479 default:
17480 /* Anything else is an error. */
17481 break;
17484 /* If we have selected an identifier, we need to consume the
17485 operator token. */
17486 if (op != ERROR_MARK)
17488 id = ovl_op_identifier (assop, op);
17489 if (!consumed)
17490 cp_lexer_consume_token (parser->lexer);
17492 /* Otherwise, no valid operator name was present. */
17493 else
17495 cp_parser_error (parser, "expected operator");
17496 id = error_mark_node;
17499 start_loc = make_location (start_loc, start_loc, get_finish (end_loc));
17500 return cp_expr (id, start_loc);
17503 /* Parse a template-declaration.
17505 template-declaration:
17506 export [opt] template < template-parameter-list > declaration
17508 If MEMBER_P is TRUE, this template-declaration occurs within a
17509 class-specifier.
17511 The grammar rule given by the standard isn't correct. What
17512 is really meant is:
17514 template-declaration:
17515 export [opt] template-parameter-list-seq
17516 decl-specifier-seq [opt] init-declarator [opt] ;
17517 export [opt] template-parameter-list-seq
17518 function-definition
17520 template-parameter-list-seq:
17521 template-parameter-list-seq [opt]
17522 template < template-parameter-list >
17524 Concept Extensions:
17526 template-parameter-list-seq:
17527 template < template-parameter-list > requires-clause [opt]
17529 requires-clause:
17530 requires logical-or-expression */
17532 static void
17533 cp_parser_template_declaration (cp_parser* parser, bool member_p)
17535 /* Check for `export'. */
17536 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
17538 /* Consume the `export' token. */
17539 cp_lexer_consume_token (parser->lexer);
17540 /* Warn that this use of export is deprecated. */
17541 if (cxx_dialect < cxx11)
17542 warning (0, "keyword %<export%> not implemented, and will be ignored");
17543 else if (cxx_dialect < cxx20)
17544 warning (0, "keyword %<export%> is deprecated, and is ignored");
17545 else
17546 warning (0, "keyword %<export%> is enabled with %<-fmodules-ts%>");
17549 cp_parser_template_declaration_after_export (parser, member_p);
17552 /* Parse a template-parameter-list.
17554 template-parameter-list:
17555 template-parameter
17556 template-parameter-list , template-parameter
17558 Returns a TREE_LIST. Each node represents a template parameter.
17559 The nodes are connected via their TREE_CHAINs. */
17561 static tree
17562 cp_parser_template_parameter_list (cp_parser* parser)
17564 tree parameter_list = NULL_TREE;
17566 /* Don't create wrapper nodes within a template-parameter-list,
17567 since we don't want to have different types based on the
17568 spelling location of constants and decls within them. */
17569 auto_suppress_location_wrappers sentinel;
17571 begin_template_parm_list ();
17573 /* The loop below parses the template parms. We first need to know
17574 the total number of template parms to be able to compute proper
17575 canonical types of each dependent type. So after the loop, when
17576 we know the total number of template parms,
17577 end_template_parm_list computes the proper canonical types and
17578 fixes up the dependent types accordingly. */
17579 while (true)
17581 tree parameter;
17582 bool is_non_type;
17583 bool is_parameter_pack;
17584 location_t parm_loc;
17586 /* Parse the template-parameter. */
17587 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
17588 parameter = cp_parser_template_parameter (parser,
17589 &is_non_type,
17590 &is_parameter_pack);
17591 /* Add it to the list. */
17592 if (parameter != error_mark_node)
17593 parameter_list = process_template_parm (parameter_list,
17594 parm_loc,
17595 parameter,
17596 is_non_type,
17597 is_parameter_pack);
17598 else
17600 tree err_parm = build_tree_list (parameter, parameter);
17601 parameter_list = chainon (parameter_list, err_parm);
17604 /* If the next token is not a `,', we're done. */
17605 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17606 break;
17607 /* Otherwise, consume the `,' token. */
17608 cp_lexer_consume_token (parser->lexer);
17611 return end_template_parm_list (parameter_list);
17614 /* Parse a introduction-list.
17616 introduction-list:
17617 introduced-parameter
17618 introduction-list , introduced-parameter
17620 introduced-parameter:
17621 ...[opt] identifier
17623 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
17624 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
17625 WILDCARD_DECL will also have DECL_NAME set and token location in
17626 DECL_SOURCE_LOCATION. */
17628 static tree
17629 cp_parser_introduction_list (cp_parser *parser)
17631 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
17633 while (true)
17635 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
17636 if (is_pack)
17637 cp_lexer_consume_token (parser->lexer);
17639 tree identifier = cp_parser_identifier (parser);
17640 if (identifier == error_mark_node)
17641 break;
17643 /* Build placeholder. */
17644 tree parm = build_nt (WILDCARD_DECL);
17645 DECL_SOURCE_LOCATION (parm)
17646 = cp_lexer_peek_token (parser->lexer)->location;
17647 DECL_NAME (parm) = identifier;
17648 WILDCARD_PACK_P (parm) = is_pack;
17649 vec_safe_push (introduction_vec, parm);
17651 /* If the next token is not a `,', we're done. */
17652 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17653 break;
17654 /* Otherwise, consume the `,' token. */
17655 cp_lexer_consume_token (parser->lexer);
17658 /* Convert the vec into a TREE_VEC. */
17659 tree introduction_list = make_tree_vec (introduction_vec->length ());
17660 unsigned int n;
17661 tree parm;
17662 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
17663 TREE_VEC_ELT (introduction_list, n) = parm;
17665 release_tree_vector (introduction_vec);
17666 return introduction_list;
17669 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
17670 is an abstract declarator. */
17672 static inline cp_declarator*
17673 get_id_declarator (cp_declarator *declarator)
17675 cp_declarator *d = declarator;
17676 while (d && d->kind != cdk_id)
17677 d = d->declarator;
17678 return d;
17681 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
17682 is an abstract declarator. */
17684 static inline tree
17685 get_unqualified_id (cp_declarator *declarator)
17687 declarator = get_id_declarator (declarator);
17688 if (declarator)
17689 return declarator->u.id.unqualified_name;
17690 else
17691 return NULL_TREE;
17694 /* Returns true if TYPE would declare a constrained constrained-parameter. */
17696 static inline bool
17697 is_constrained_parameter (tree type)
17699 return (type
17700 && TREE_CODE (type) == TYPE_DECL
17701 && CONSTRAINED_PARM_CONCEPT (type)
17702 && DECL_P (CONSTRAINED_PARM_CONCEPT (type)));
17705 /* Returns true if PARM declares a constrained-parameter. */
17707 static inline bool
17708 is_constrained_parameter (cp_parameter_declarator *parm)
17710 return is_constrained_parameter (parm->decl_specifiers.type);
17713 /* Check that the type parameter is only a declarator-id, and that its
17714 type is not cv-qualified. */
17716 bool
17717 cp_parser_check_constrained_type_parm (cp_parser *parser,
17718 cp_parameter_declarator *parm)
17720 if (!parm->declarator)
17721 return true;
17723 if (parm->declarator->kind != cdk_id)
17725 cp_parser_error (parser, "invalid constrained type parameter");
17726 return false;
17729 /* Don't allow cv-qualified type parameters. */
17730 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
17731 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
17733 cp_parser_error (parser, "cv-qualified type parameter");
17734 return false;
17737 return true;
17740 /* Finish parsing/processing a template type parameter and checking
17741 various restrictions. */
17743 static inline tree
17744 cp_parser_constrained_type_template_parm (cp_parser *parser,
17745 tree id,
17746 cp_parameter_declarator* parmdecl)
17748 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
17749 return finish_template_type_parm (class_type_node, id);
17750 else
17751 return error_mark_node;
17754 static tree
17755 finish_constrained_template_template_parm (tree proto, tree id)
17757 /* FIXME: This should probably be copied, and we may need to adjust
17758 the template parameter depths. */
17759 tree saved_parms = current_template_parms;
17760 begin_template_parm_list ();
17761 current_template_parms = DECL_TEMPLATE_PARMS (proto);
17762 end_template_parm_list ();
17764 tree parm = finish_template_template_parm (class_type_node, id);
17765 current_template_parms = saved_parms;
17767 return parm;
17770 /* Finish parsing/processing a template template parameter by borrowing
17771 the template parameter list from the prototype parameter. */
17773 static tree
17774 cp_parser_constrained_template_template_parm (cp_parser *parser,
17775 tree proto,
17776 tree id,
17777 cp_parameter_declarator *parmdecl)
17779 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
17780 return error_mark_node;
17781 return finish_constrained_template_template_parm (proto, id);
17784 /* Create a new non-type template parameter from the given PARM
17785 declarator. */
17787 static tree
17788 cp_parser_constrained_non_type_template_parm (bool *is_non_type,
17789 cp_parameter_declarator *parm)
17791 *is_non_type = true;
17792 cp_declarator *decl = parm->declarator;
17793 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
17794 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
17795 return grokdeclarator (decl, specs, TPARM, 0, NULL);
17798 /* Build a constrained template parameter based on the PARMDECL
17799 declarator. The type of PARMDECL is the constrained type, which
17800 refers to the prototype template parameter that ultimately
17801 specifies the type of the declared parameter. */
17803 static tree
17804 finish_constrained_parameter (cp_parser *parser,
17805 cp_parameter_declarator *parmdecl,
17806 bool *is_non_type)
17808 tree decl = parmdecl->decl_specifiers.type;
17809 tree id = get_unqualified_id (parmdecl->declarator);
17810 tree def = parmdecl->default_argument;
17811 tree proto = DECL_INITIAL (decl);
17813 /* Build the parameter. Return an error if the declarator was invalid. */
17814 tree parm;
17815 if (TREE_CODE (proto) == TYPE_DECL)
17816 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
17817 else if (TREE_CODE (proto) == TEMPLATE_DECL)
17818 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
17819 parmdecl);
17820 else
17821 parm = cp_parser_constrained_non_type_template_parm (is_non_type, parmdecl);
17822 if (parm == error_mark_node)
17823 return error_mark_node;
17825 /* Finish the parameter decl and create a node attaching the
17826 default argument and constraint. */
17827 parm = build_tree_list (def, parm);
17828 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
17830 return parm;
17833 /* Returns true if the parsed type actually represents the declaration
17834 of a type template-parameter. */
17836 static bool
17837 declares_constrained_type_template_parameter (tree type)
17839 return (is_constrained_parameter (type)
17840 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
17843 /* Returns true if the parsed type actually represents the declaration of
17844 a template template-parameter. */
17846 static bool
17847 declares_constrained_template_template_parameter (tree type)
17849 return (is_constrained_parameter (type)
17850 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
17853 /* Parse a default argument for a type template-parameter.
17854 Note that diagnostics are handled in cp_parser_template_parameter. */
17856 static tree
17857 cp_parser_default_type_template_argument (cp_parser *parser)
17859 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
17861 /* Consume the `=' token. */
17862 cp_lexer_consume_token (parser->lexer);
17864 cp_token *token = cp_lexer_peek_token (parser->lexer);
17866 /* Tell cp_parser_lambda_expression this is a default argument. */
17867 auto lvf = make_temp_override (parser->local_variables_forbidden_p);
17868 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
17870 /* Parse the default-argument. */
17871 push_deferring_access_checks (dk_no_deferred);
17872 tree default_argument = cp_parser_type_id (parser,
17873 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
17874 NULL);
17875 pop_deferring_access_checks ();
17877 if (flag_concepts && type_uses_auto (default_argument))
17879 error_at (token->location,
17880 "invalid use of %<auto%> in default template argument");
17881 return error_mark_node;
17884 return default_argument;
17887 /* Parse a default argument for a template template-parameter. */
17889 static tree
17890 cp_parser_default_template_template_argument (cp_parser *parser)
17892 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
17894 bool is_template;
17896 /* Consume the `='. */
17897 cp_lexer_consume_token (parser->lexer);
17898 /* Parse the id-expression. */
17899 push_deferring_access_checks (dk_no_deferred);
17900 /* save token before parsing the id-expression, for error
17901 reporting */
17902 const cp_token* token = cp_lexer_peek_token (parser->lexer);
17903 tree default_argument
17904 = cp_parser_id_expression (parser,
17905 /*template_keyword_p=*/false,
17906 /*check_dependency_p=*/true,
17907 /*template_p=*/&is_template,
17908 /*declarator_p=*/false,
17909 /*optional_p=*/false);
17910 if (TREE_CODE (default_argument) == TYPE_DECL)
17911 /* If the id-expression was a template-id that refers to
17912 a template-class, we already have the declaration here,
17913 so no further lookup is needed. */
17915 else
17916 /* Look up the name. */
17917 default_argument
17918 = cp_parser_lookup_name (parser, default_argument,
17919 none_type,
17920 /*is_template=*/is_template,
17921 /*is_namespace=*/false,
17922 /*check_dependency=*/true,
17923 /*ambiguous_decls=*/NULL,
17924 token->location);
17925 /* See if the default argument is valid. */
17926 default_argument = check_template_template_default_arg (default_argument);
17927 pop_deferring_access_checks ();
17928 return default_argument;
17931 /* Parse a template-parameter.
17933 template-parameter:
17934 type-parameter
17935 parameter-declaration
17937 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
17938 the parameter. The TREE_PURPOSE is the default value, if any.
17939 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
17940 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
17941 set to true iff this parameter is a parameter pack. */
17943 static tree
17944 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
17945 bool *is_parameter_pack)
17947 cp_token *token;
17948 cp_parameter_declarator *parameter_declarator;
17949 tree parm;
17951 /* Assume it is a type parameter or a template parameter. */
17952 *is_non_type = false;
17953 /* Assume it not a parameter pack. */
17954 *is_parameter_pack = false;
17955 /* Peek at the next token. */
17956 token = cp_lexer_peek_token (parser->lexer);
17957 /* If it is `template', we have a type-parameter. */
17958 if (token->keyword == RID_TEMPLATE)
17959 return cp_parser_type_parameter (parser, is_parameter_pack);
17960 /* If it is `class' or `typename' we do not know yet whether it is a
17961 type parameter or a non-type parameter. Consider:
17963 template <typename T, typename T::X X> ...
17967 template <class C, class D*> ...
17969 Here, the first parameter is a type parameter, and the second is
17970 a non-type parameter. We can tell by looking at the token after
17971 the identifier -- if it is a `,', `=', or `>' then we have a type
17972 parameter. */
17973 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
17975 /* Peek at the token after `class' or `typename'. */
17976 token = cp_lexer_peek_nth_token (parser->lexer, 2);
17977 /* If it's an ellipsis, we have a template type parameter
17978 pack. */
17979 if (token->type == CPP_ELLIPSIS)
17980 return cp_parser_type_parameter (parser, is_parameter_pack);
17981 /* If it's an identifier, skip it. */
17982 if (token->type == CPP_NAME)
17983 token = cp_lexer_peek_nth_token (parser->lexer, 3);
17984 /* Now, see if the token looks like the end of a template
17985 parameter. */
17986 if (token->type == CPP_COMMA
17987 || token->type == CPP_EQ
17988 || token->type == CPP_GREATER)
17989 return cp_parser_type_parameter (parser, is_parameter_pack);
17992 /* Otherwise, it is a non-type parameter or a constrained parameter.
17994 [temp.param]
17996 When parsing a default template-argument for a non-type
17997 template-parameter, the first non-nested `>' is taken as the end
17998 of the template parameter-list rather than a greater-than
17999 operator. */
18000 parameter_declarator
18001 = cp_parser_parameter_declaration (parser,
18002 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
18003 /*template_parm_p=*/true,
18004 /*parenthesized_p=*/NULL);
18006 if (!parameter_declarator)
18007 return error_mark_node;
18009 /* If the parameter declaration is marked as a parameter pack, set
18010 *IS_PARAMETER_PACK to notify the caller. */
18011 if (parameter_declarator->template_parameter_pack_p)
18012 *is_parameter_pack = true;
18014 if (parameter_declarator->default_argument)
18016 /* Can happen in some cases of erroneous input (c++/34892). */
18017 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18018 /* Consume the `...' for better error recovery. */
18019 cp_lexer_consume_token (parser->lexer);
18022 /* The parameter may have been constrained type parameter. */
18023 if (is_constrained_parameter (parameter_declarator))
18024 return finish_constrained_parameter (parser,
18025 parameter_declarator,
18026 is_non_type);
18028 // Now we're sure that the parameter is a non-type parameter.
18029 *is_non_type = true;
18031 parm = grokdeclarator (parameter_declarator->declarator,
18032 &parameter_declarator->decl_specifiers,
18033 TPARM, /*initialized=*/0,
18034 /*attrlist=*/NULL);
18035 if (parm == error_mark_node)
18036 return error_mark_node;
18038 return build_tree_list (parameter_declarator->default_argument, parm);
18041 /* Parse a type-parameter.
18043 type-parameter:
18044 class identifier [opt]
18045 class identifier [opt] = type-id
18046 typename identifier [opt]
18047 typename identifier [opt] = type-id
18048 template < template-parameter-list > class identifier [opt]
18049 template < template-parameter-list > class identifier [opt]
18050 = id-expression
18052 GNU Extension (variadic templates):
18054 type-parameter:
18055 class ... identifier [opt]
18056 typename ... identifier [opt]
18058 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
18059 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
18060 the declaration of the parameter.
18062 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
18064 static tree
18065 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
18067 cp_token *token;
18068 tree parameter;
18070 /* Look for a keyword to tell us what kind of parameter this is. */
18071 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
18072 if (!token)
18073 return error_mark_node;
18075 switch (token->keyword)
18077 case RID_CLASS:
18078 case RID_TYPENAME:
18080 tree identifier;
18081 tree default_argument;
18083 /* If the next token is an ellipsis, we have a template
18084 argument pack. */
18085 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18087 /* Consume the `...' token. */
18088 cp_lexer_consume_token (parser->lexer);
18089 maybe_warn_variadic_templates ();
18091 *is_parameter_pack = true;
18094 /* If the next token is an identifier, then it names the
18095 parameter. */
18096 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18097 identifier = cp_parser_identifier (parser);
18098 else
18099 identifier = NULL_TREE;
18101 /* Create the parameter. */
18102 parameter = finish_template_type_parm (class_type_node, identifier);
18104 /* If the next token is an `=', we have a default argument. */
18105 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18107 default_argument
18108 = cp_parser_default_type_template_argument (parser);
18110 /* Template parameter packs cannot have default
18111 arguments. */
18112 if (*is_parameter_pack)
18114 if (identifier)
18115 error_at (token->location,
18116 "template parameter pack %qD cannot have a "
18117 "default argument", identifier);
18118 else
18119 error_at (token->location,
18120 "template parameter packs cannot have "
18121 "default arguments");
18122 default_argument = NULL_TREE;
18124 else if (check_for_bare_parameter_packs (default_argument))
18125 default_argument = error_mark_node;
18127 else
18128 default_argument = NULL_TREE;
18130 /* Create the combined representation of the parameter and the
18131 default argument. */
18132 parameter = build_tree_list (default_argument, parameter);
18134 break;
18136 case RID_TEMPLATE:
18138 tree identifier;
18139 tree default_argument;
18141 /* Look for the `<'. */
18142 cp_parser_require (parser, CPP_LESS, RT_LESS);
18143 /* Parse the template-parameter-list. */
18144 cp_parser_template_parameter_list (parser);
18145 /* Look for the `>'. */
18146 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
18148 /* If template requirements are present, parse them. */
18149 if (flag_concepts)
18151 tree reqs = get_shorthand_constraints (current_template_parms);
18152 if (tree dreqs = cp_parser_requires_clause_opt (parser, false))
18153 reqs = combine_constraint_expressions (reqs, dreqs);
18154 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
18157 /* Look for the `class' or 'typename' keywords. */
18158 cp_parser_type_parameter_key (parser);
18159 /* If the next token is an ellipsis, we have a template
18160 argument pack. */
18161 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18163 /* Consume the `...' token. */
18164 cp_lexer_consume_token (parser->lexer);
18165 maybe_warn_variadic_templates ();
18167 *is_parameter_pack = true;
18169 /* If the next token is an `=', then there is a
18170 default-argument. If the next token is a `>', we are at
18171 the end of the parameter-list. If the next token is a `,',
18172 then we are at the end of this parameter. */
18173 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
18174 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
18175 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18177 identifier = cp_parser_identifier (parser);
18178 /* Treat invalid names as if the parameter were nameless. */
18179 if (identifier == error_mark_node)
18180 identifier = NULL_TREE;
18182 else
18183 identifier = NULL_TREE;
18185 /* Create the template parameter. */
18186 parameter = finish_template_template_parm (class_type_node,
18187 identifier);
18189 /* If the next token is an `=', then there is a
18190 default-argument. */
18191 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18193 default_argument
18194 = cp_parser_default_template_template_argument (parser);
18196 /* Template parameter packs cannot have default
18197 arguments. */
18198 if (*is_parameter_pack)
18200 if (identifier)
18201 error_at (token->location,
18202 "template parameter pack %qD cannot "
18203 "have a default argument",
18204 identifier);
18205 else
18206 error_at (token->location, "template parameter packs cannot "
18207 "have default arguments");
18208 default_argument = NULL_TREE;
18211 else
18212 default_argument = NULL_TREE;
18214 /* Create the combined representation of the parameter and the
18215 default argument. */
18216 parameter = build_tree_list (default_argument, parameter);
18218 break;
18220 default:
18221 gcc_unreachable ();
18222 break;
18225 return parameter;
18228 /* Parse a template-id.
18230 template-id:
18231 template-name < template-argument-list [opt] >
18233 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
18234 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
18235 returned. Otherwise, if the template-name names a function, or set
18236 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
18237 names a class, returns a TYPE_DECL for the specialization.
18239 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
18240 uninstantiated templates. */
18242 static tree
18243 cp_parser_template_id (cp_parser *parser,
18244 bool template_keyword_p,
18245 bool check_dependency_p,
18246 enum tag_types tag_type,
18247 bool is_declaration)
18249 tree templ;
18250 tree arguments;
18251 tree template_id;
18252 cp_token_position start_of_id = 0;
18253 cp_token *next_token = NULL, *next_token_2 = NULL;
18254 bool is_identifier;
18256 /* If the next token corresponds to a template-id, there is no need
18257 to reparse it. */
18258 cp_token *token = cp_lexer_peek_token (parser->lexer);
18260 if (token->type == CPP_TEMPLATE_ID)
18262 cp_lexer_consume_token (parser->lexer);
18263 return saved_checks_value (token->u.tree_check_value);
18266 /* Avoid performing name lookup if there is no possibility of
18267 finding a template-id. */
18268 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
18269 || (token->type == CPP_NAME
18270 && !cp_parser_nth_token_starts_template_argument_list_p
18271 (parser, 2)))
18273 cp_parser_error (parser, "expected template-id");
18274 return error_mark_node;
18277 /* Remember where the template-id starts. */
18278 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
18279 start_of_id = cp_lexer_token_position (parser->lexer, false);
18281 push_deferring_access_checks (dk_deferred);
18283 /* Parse the template-name. */
18284 is_identifier = false;
18285 templ = cp_parser_template_name (parser, template_keyword_p,
18286 check_dependency_p,
18287 is_declaration,
18288 tag_type,
18289 &is_identifier);
18291 /* Push any access checks inside the firewall we're about to create. */
18292 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
18293 pop_deferring_access_checks ();
18294 if (templ == error_mark_node || is_identifier)
18295 return templ;
18297 /* Since we're going to preserve any side-effects from this parse, set up a
18298 firewall to protect our callers from cp_parser_commit_to_tentative_parse
18299 in the template arguments. */
18300 tentative_firewall firewall (parser);
18301 reopen_deferring_access_checks (checks);
18303 /* If we find the sequence `[:' after a template-name, it's probably
18304 a digraph-typo for `< ::'. Substitute the tokens and check if we can
18305 parse correctly the argument list. */
18306 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
18307 == CPP_OPEN_SQUARE)
18308 && next_token->flags & DIGRAPH
18309 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
18310 == CPP_COLON)
18311 && !(next_token_2->flags & PREV_WHITE))
18313 cp_parser_parse_tentatively (parser);
18314 /* Change `:' into `::'. */
18315 next_token_2->type = CPP_SCOPE;
18316 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
18317 CPP_LESS. */
18318 cp_lexer_consume_token (parser->lexer);
18320 /* Parse the arguments. */
18321 arguments = cp_parser_enclosed_template_argument_list (parser);
18322 if (!cp_parser_parse_definitely (parser))
18324 /* If we couldn't parse an argument list, then we revert our changes
18325 and return simply an error. Maybe this is not a template-id
18326 after all. */
18327 next_token_2->type = CPP_COLON;
18328 cp_parser_error (parser, "expected %<<%>");
18329 pop_deferring_access_checks ();
18330 return error_mark_node;
18332 /* Otherwise, emit an error about the invalid digraph, but continue
18333 parsing because we got our argument list. */
18334 if (permerror (next_token->location,
18335 "%<<::%> cannot begin a template-argument list"))
18337 static bool hint = false;
18338 inform (next_token->location,
18339 "%<<:%> is an alternate spelling for %<[%>."
18340 " Insert whitespace between %<<%> and %<::%>");
18341 if (!hint && !flag_permissive)
18343 inform (next_token->location, "(if you use %<-fpermissive%> "
18344 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
18345 "accept your code)");
18346 hint = true;
18350 else
18352 /* Look for the `<' that starts the template-argument-list. */
18353 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
18355 pop_deferring_access_checks ();
18356 return error_mark_node;
18358 /* Parse the arguments. */
18359 arguments = cp_parser_enclosed_template_argument_list (parser);
18361 if ((cxx_dialect > cxx17)
18362 && (TREE_CODE (templ) == FUNCTION_DECL || identifier_p (templ))
18363 && !template_keyword_p
18364 && (cp_parser_error_occurred (parser)
18365 || cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)))
18367 /* This didn't go well. */
18368 if (TREE_CODE (templ) == FUNCTION_DECL)
18370 /* C++20 says that "function-name < a;" is now ill-formed. */
18371 if (cp_parser_error_occurred (parser))
18373 error_at (token->location, "invalid template-argument-list");
18374 inform (token->location, "function name as the left hand "
18375 "operand of %<<%> is ill-formed in C++20; wrap the "
18376 "function name in %<()%>");
18378 else
18379 /* We expect "f<targs>" to be followed by "(args)". */
18380 error_at (cp_lexer_peek_token (parser->lexer)->location,
18381 "expected %<(%> after template-argument-list");
18382 if (start_of_id)
18383 /* Purge all subsequent tokens. */
18384 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
18386 else
18387 cp_parser_simulate_error (parser);
18388 pop_deferring_access_checks ();
18389 return error_mark_node;
18393 /* Set the location to be of the form:
18394 template-name < template-argument-list [opt] >
18395 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18396 with caret == start at the start of the template-name,
18397 ranging until the closing '>'. */
18398 location_t combined_loc
18399 = make_location (token->location, token->location, parser->lexer);
18401 /* Check for concepts autos where they don't belong. We could
18402 identify types in some cases of identifier TEMPL, looking ahead
18403 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
18404 types. We reject them in functions, but if what we have is an
18405 identifier, even with none_type we can't conclude it's NOT a
18406 type, we have to wait for template substitution. */
18407 if (flag_concepts && check_auto_in_tmpl_args (templ, arguments))
18408 template_id = error_mark_node;
18409 /* Build a representation of the specialization. */
18410 else if (identifier_p (templ))
18411 template_id = build_min_nt_loc (combined_loc,
18412 TEMPLATE_ID_EXPR,
18413 templ, arguments);
18414 else if (DECL_TYPE_TEMPLATE_P (templ)
18415 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
18417 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
18418 template (rather than some instantiation thereof) only if
18419 is not nested within some other construct. For example, in
18420 "template <typename T> void f(T) { A<T>::", A<T> is just an
18421 instantiation of A. */
18422 bool entering_scope
18423 = (template_parm_scope_p ()
18424 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE));
18425 template_id
18426 = finish_template_type (templ, arguments, entering_scope);
18428 else if (concept_definition_p (templ))
18430 /* The caller will decide whether this is a concept check or type
18431 constraint. */
18432 template_id = build2_loc (combined_loc, TEMPLATE_ID_EXPR,
18433 boolean_type_node, templ, arguments);
18435 else if (variable_template_p (templ))
18437 template_id = lookup_template_variable (templ, arguments);
18438 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
18439 SET_EXPR_LOCATION (template_id, combined_loc);
18441 else if (TREE_CODE (templ) == TYPE_DECL
18442 && TREE_CODE (TREE_TYPE (templ)) == TYPENAME_TYPE)
18444 /* Some type template in dependent scope. */
18445 tree &name = TYPENAME_TYPE_FULLNAME (TREE_TYPE (templ));
18446 name = build_min_nt_loc (combined_loc,
18447 TEMPLATE_ID_EXPR,
18448 name, arguments);
18449 template_id = templ;
18451 else
18453 /* If it's not a class-template or a template-template, it should be
18454 a function-template. */
18455 gcc_assert (OVL_P (templ) || BASELINK_P (templ));
18457 template_id = lookup_template_function (templ, arguments);
18458 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
18459 SET_EXPR_LOCATION (template_id, combined_loc);
18462 /* If parsing tentatively, replace the sequence of tokens that makes
18463 up the template-id with a CPP_TEMPLATE_ID token. That way,
18464 should we re-parse the token stream, we will not have to repeat
18465 the effort required to do the parse, nor will we issue duplicate
18466 error messages about problems during instantiation of the
18467 template. */
18468 if (start_of_id
18469 /* Don't do this if we had a parse error in a declarator; re-parsing
18470 might succeed if a name changes meaning (60361). */
18471 && !(cp_parser_error_occurred (parser)
18472 && cp_parser_parsing_tentatively (parser)
18473 && parser->in_declarator_p))
18475 /* Reset the contents of the START_OF_ID token. */
18476 token->type = CPP_TEMPLATE_ID;
18477 token->location = combined_loc;
18479 /* Retrieve any deferred checks. Do not pop this access checks yet
18480 so the memory will not be reclaimed during token replacing below. */
18481 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
18482 token->tree_check_p = true;
18483 token->u.tree_check_value->value = template_id;
18484 token->u.tree_check_value->checks = get_deferred_access_checks ();
18485 token->keyword = RID_MAX;
18487 /* Purge all subsequent tokens. */
18488 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
18490 /* ??? Can we actually assume that, if template_id ==
18491 error_mark_node, we will have issued a diagnostic to the
18492 user, as opposed to simply marking the tentative parse as
18493 failed? */
18494 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
18495 error_at (token->location, "parse error in template argument list");
18498 pop_to_parent_deferring_access_checks ();
18499 return template_id;
18502 /* Like cp_parser_template_id, called in non-type context. */
18504 static tree
18505 cp_parser_template_id_expr (cp_parser *parser,
18506 bool template_keyword_p,
18507 bool check_dependency_p,
18508 bool is_declaration)
18510 tree x = cp_parser_template_id (parser, template_keyword_p, check_dependency_p,
18511 none_type, is_declaration);
18512 if (TREE_CODE (x) == TEMPLATE_ID_EXPR
18513 && concept_check_p (x))
18514 /* We didn't check the arguments in cp_parser_template_id; do that now. */
18515 return build_concept_id (x);
18516 return x;
18519 /* Parse a template-name.
18521 template-name:
18522 identifier
18524 The standard should actually say:
18526 template-name:
18527 identifier
18528 operator-function-id
18530 A defect report has been filed about this issue.
18532 A conversion-function-id cannot be a template name because they cannot
18533 be part of a template-id. In fact, looking at this code:
18535 a.operator K<int>()
18537 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
18538 It is impossible to call a templated conversion-function-id with an
18539 explicit argument list, since the only allowed template parameter is
18540 the type to which it is converting.
18542 If TEMPLATE_KEYWORD_P is true, then we have just seen the
18543 `template' keyword, in a construction like:
18545 T::template f<3>()
18547 In that case `f' is taken to be a template-name, even though there
18548 is no way of knowing for sure.
18550 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
18551 name refers to a set of overloaded functions, at least one of which
18552 is a template, or an IDENTIFIER_NODE with the name of the template,
18553 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
18554 names are looked up inside uninstantiated templates. */
18556 static tree
18557 cp_parser_template_name (cp_parser* parser,
18558 bool template_keyword_p,
18559 bool check_dependency_p,
18560 bool is_declaration,
18561 enum tag_types tag_type,
18562 bool *is_identifier)
18564 tree identifier;
18565 tree decl;
18566 cp_token *token = cp_lexer_peek_token (parser->lexer);
18568 /* If the next token is `operator', then we have either an
18569 operator-function-id or a conversion-function-id. */
18570 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
18572 /* We don't know whether we're looking at an
18573 operator-function-id or a conversion-function-id. */
18574 cp_parser_parse_tentatively (parser);
18575 /* Try an operator-function-id. */
18576 identifier = cp_parser_operator_function_id (parser);
18577 /* If that didn't work, try a conversion-function-id. */
18578 if (!cp_parser_parse_definitely (parser))
18580 cp_parser_error (parser, "expected template-name");
18581 return error_mark_node;
18584 /* Look for the identifier. */
18585 else
18586 identifier = cp_parser_identifier (parser);
18588 /* If we didn't find an identifier, we don't have a template-id. */
18589 if (identifier == error_mark_node)
18590 return error_mark_node;
18592 /* If the name immediately followed the `template' keyword, then it
18593 is a template-name. However, if the next token is not `<', then
18594 we do not treat it as a template-name, since it is not being used
18595 as part of a template-id. This enables us to handle constructs
18596 like:
18598 template <typename T> struct S { S(); };
18599 template <typename T> S<T>::S();
18601 correctly. We would treat `S' as a template -- if it were `S<T>'
18602 -- but we do not if there is no `<'. */
18604 if (processing_template_decl
18605 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
18607 /* In a declaration, in a dependent context, we pretend that the
18608 "template" keyword was present in order to improve error
18609 recovery. For example, given:
18611 template <typename T> void f(T::X<int>);
18613 we want to treat "X<int>" as a template-id. */
18614 if (is_declaration
18615 && !template_keyword_p
18616 && parser->scope && TYPE_P (parser->scope)
18617 && check_dependency_p
18618 && dependent_scope_p (parser->scope)
18619 /* Do not do this for dtors (or ctors), since they never
18620 need the template keyword before their name. */
18621 && !constructor_name_p (identifier, parser->scope))
18623 cp_token_position start = 0;
18625 /* Explain what went wrong. */
18626 error_at (token->location, "non-template %qD used as template",
18627 identifier);
18628 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
18629 parser->scope, identifier);
18630 /* If parsing tentatively, find the location of the "<" token. */
18631 if (cp_parser_simulate_error (parser))
18632 start = cp_lexer_token_position (parser->lexer, true);
18633 /* Parse the template arguments so that we can issue error
18634 messages about them. */
18635 cp_lexer_consume_token (parser->lexer);
18636 cp_parser_enclosed_template_argument_list (parser);
18637 /* Skip tokens until we find a good place from which to
18638 continue parsing. */
18639 cp_parser_skip_to_closing_parenthesis (parser,
18640 /*recovering=*/true,
18641 /*or_comma=*/true,
18642 /*consume_paren=*/false);
18643 /* If parsing tentatively, permanently remove the
18644 template argument list. That will prevent duplicate
18645 error messages from being issued about the missing
18646 "template" keyword. */
18647 if (start)
18648 cp_lexer_purge_tokens_after (parser->lexer, start);
18649 if (is_identifier)
18650 *is_identifier = true;
18651 parser->context->object_type = NULL_TREE;
18652 return identifier;
18655 /* If the "template" keyword is present, then there is generally
18656 no point in doing name-lookup, so we just return IDENTIFIER.
18657 But, if the qualifying scope is non-dependent then we can
18658 (and must) do name-lookup normally. */
18659 if (template_keyword_p)
18661 tree scope = (parser->scope ? parser->scope
18662 : parser->context->object_type);
18663 if (scope && TYPE_P (scope)
18664 && (!CLASS_TYPE_P (scope)
18665 || (check_dependency_p && dependent_scope_p (scope))))
18667 /* We're optimizing away the call to cp_parser_lookup_name, but
18668 we still need to do this. */
18669 parser->object_scope = parser->context->object_type;
18670 parser->context->object_type = NULL_TREE;
18671 return identifier;
18676 /* cp_parser_lookup_name clears OBJECT_TYPE. */
18677 tree scope = (parser->scope ? parser->scope
18678 : parser->context->object_type);
18680 /* Look up the name. */
18681 decl = cp_parser_lookup_name (parser, identifier,
18682 tag_type,
18683 /*is_template=*/true,
18684 /*is_namespace=*/false,
18685 check_dependency_p,
18686 /*ambiguous_decls=*/NULL,
18687 token->location);
18689 decl = strip_using_decl (decl);
18691 /* 13.3 [temp.names] A < is interpreted as the delimiter of a
18692 template-argument-list if it follows a name that is not a
18693 conversion-function-id and
18694 - that follows the keyword template or a ~ after a nested-name-specifier or
18695 in a class member access expression, or
18696 - for which name lookup finds the injected-class-name of a class template
18697 or finds any declaration of a template, or
18698 - that is an unqualified name for which name lookup either finds one or
18699 more functions or finds nothing, or
18700 - that is a terminal name in a using-declarator (9.9), in a declarator-id
18701 (9.3.4), or in a type-only context other than a nested-name-specifier
18702 (13.8). */
18704 /* Handle injected-class-name. */
18705 decl = maybe_get_template_decl_from_type_decl (decl);
18707 /* If DECL is a template, then the name was a template-name. */
18708 if (TREE_CODE (decl) == TEMPLATE_DECL)
18710 if ((TREE_DEPRECATED (decl) || TREE_UNAVAILABLE (decl))
18711 && deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
18713 tree d = DECL_TEMPLATE_RESULT (decl);
18714 tree attr;
18715 if (TREE_CODE (d) == TYPE_DECL)
18716 attr = TYPE_ATTRIBUTES (TREE_TYPE (d));
18717 else
18718 attr = DECL_ATTRIBUTES (d);
18719 if (TREE_UNAVAILABLE (decl))
18721 attr = lookup_attribute ("unavailable", attr);
18722 error_unavailable_use (decl, attr);
18724 else if (TREE_DEPRECATED (decl)
18725 && deprecated_state != DEPRECATED_SUPPRESS)
18727 attr = lookup_attribute ("deprecated", attr);
18728 warn_deprecated_use (decl, attr);
18732 else
18734 /* Look through an overload set for any templates. */
18735 bool found = false;
18737 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
18738 !found && iter; ++iter)
18739 if (TREE_CODE (*iter) == TEMPLATE_DECL)
18740 found = true;
18742 /* "an unqualified name for which name lookup either finds one or more
18743 functions or finds nothing". */
18744 if (!found
18745 && (cxx_dialect > cxx17)
18746 && !scope
18747 && cp_lexer_next_token_is (parser->lexer, CPP_LESS)
18748 && tag_type == none_type)
18750 /* The "more functions" case. Just use the OVERLOAD as normally.
18751 We don't use is_overloaded_fn here to avoid considering
18752 BASELINKs. */
18753 if (TREE_CODE (decl) == OVERLOAD
18754 /* Name lookup found one function. */
18755 || TREE_CODE (decl) == FUNCTION_DECL
18756 /* Name lookup found nothing. */
18757 || decl == error_mark_node)
18758 found = true;
18761 /* "that follows the keyword template"..."in a type-only context" */
18762 if (!found && scope
18763 && (template_keyword_p || tag_type != none_type)
18764 && dependentish_scope_p (scope)
18765 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
18766 found = true;
18768 if (!found)
18770 /* The name does not name a template. */
18771 cp_parser_error (parser, "expected template-name");
18772 return error_mark_node;
18774 else if ((!DECL_P (decl) && !is_overloaded_fn (decl))
18775 || TREE_CODE (decl) == USING_DECL
18776 /* cp_parser_template_id can only handle some TYPE_DECLs. */
18777 || (TREE_CODE (decl) == TYPE_DECL
18778 && TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE))
18779 /* Repeat the lookup at instantiation time. */
18780 decl = identifier;
18783 return decl;
18786 /* Parse a template-argument-list.
18788 template-argument-list:
18789 template-argument ... [opt]
18790 template-argument-list , template-argument ... [opt]
18792 Returns a TREE_VEC containing the arguments. */
18794 static tree
18795 cp_parser_template_argument_list (cp_parser* parser)
18797 bool saved_in_template_argument_list_p;
18798 bool saved_ice_p;
18799 bool saved_non_ice_p;
18801 /* Don't create location wrapper nodes within a template-argument-list. */
18802 auto_suppress_location_wrappers sentinel;
18804 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
18805 parser->in_template_argument_list_p = true;
18806 /* Even if the template-id appears in an integral
18807 constant-expression, the contents of the argument list do
18808 not. */
18809 saved_ice_p = parser->integral_constant_expression_p;
18810 parser->integral_constant_expression_p = false;
18811 saved_non_ice_p = parser->non_integral_constant_expression_p;
18812 parser->non_integral_constant_expression_p = false;
18814 /* Parse the arguments. */
18815 auto_vec<tree, 10> args;
18818 if (!args.is_empty ())
18819 /* Consume the comma. */
18820 cp_lexer_consume_token (parser->lexer);
18822 /* Parse the template-argument. */
18823 tree argument = cp_parser_template_argument (parser);
18825 /* If the next token is an ellipsis, we're expanding a template
18826 argument pack. */
18827 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18829 if (argument == error_mark_node)
18831 cp_token *token = cp_lexer_peek_token (parser->lexer);
18832 error_at (token->location,
18833 "expected parameter pack before %<...%>");
18835 /* Consume the `...' token. */
18836 cp_lexer_consume_token (parser->lexer);
18838 /* Make the argument into a TYPE_PACK_EXPANSION or
18839 EXPR_PACK_EXPANSION. */
18840 argument = make_pack_expansion (argument);
18843 args.safe_push (argument);
18845 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
18847 int n_args = args.length ();
18848 tree vec = make_tree_vec (n_args);
18850 for (int i = 0; i < n_args; i++)
18851 TREE_VEC_ELT (vec, i) = args[i];
18853 parser->non_integral_constant_expression_p = saved_non_ice_p;
18854 parser->integral_constant_expression_p = saved_ice_p;
18855 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
18856 if (CHECKING_P)
18857 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
18858 return vec;
18861 /* Parse a template-argument.
18863 template-argument:
18864 assignment-expression
18865 type-id
18866 id-expression
18868 The representation is that of an assignment-expression, type-id, or
18869 id-expression -- except that the qualified id-expression is
18870 evaluated, so that the value returned is either a DECL or an
18871 OVERLOAD.
18873 Although the standard says "assignment-expression", it forbids
18874 throw-expressions or assignments in the template argument.
18875 Therefore, we use "conditional-expression" instead. */
18877 static tree
18878 cp_parser_template_argument (cp_parser* parser)
18880 tree argument;
18881 bool template_p;
18882 bool address_p;
18883 bool maybe_type_id = false;
18884 cp_token *token = NULL, *argument_start_token = NULL;
18885 location_t loc = 0;
18886 cp_id_kind idk;
18888 /* There's really no way to know what we're looking at, so we just
18889 try each alternative in order.
18891 [temp.arg]
18893 In a template-argument, an ambiguity between a type-id and an
18894 expression is resolved to a type-id, regardless of the form of
18895 the corresponding template-parameter.
18897 Therefore, we try a type-id first. */
18898 cp_parser_parse_tentatively (parser);
18899 argument = cp_parser_template_type_arg (parser);
18900 /* If there was no error parsing the type-id but the next token is a
18901 '>>', our behavior depends on which dialect of C++ we're
18902 parsing. In C++98, we probably found a typo for '> >'. But there
18903 are type-id which are also valid expressions. For instance:
18905 struct X { int operator >> (int); };
18906 template <int V> struct Foo {};
18907 Foo<X () >> 5> r;
18909 Here 'X()' is a valid type-id of a function type, but the user just
18910 wanted to write the expression "X() >> 5". Thus, we remember that we
18911 found a valid type-id, but we still try to parse the argument as an
18912 expression to see what happens.
18914 In C++0x, the '>>' will be considered two separate '>'
18915 tokens. */
18916 if (!cp_parser_error_occurred (parser)
18917 && ((cxx_dialect == cxx98
18918 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18919 /* Similarly for >= which
18920 cp_parser_next_token_ends_template_argument_p treats for
18921 diagnostics purposes as mistyped > =, but can be valid
18922 after a type-id. */
18923 || cp_lexer_next_token_is (parser->lexer, CPP_GREATER_EQ)))
18925 maybe_type_id = true;
18926 cp_parser_abort_tentative_parse (parser);
18928 else
18930 /* If the next token isn't a `,' or a `>', then this argument wasn't
18931 really finished. This means that the argument is not a valid
18932 type-id. */
18933 if (!cp_parser_next_token_ends_template_argument_p (parser))
18934 cp_parser_error (parser, "expected template-argument");
18935 /* If that worked, we're done. */
18936 if (cp_parser_parse_definitely (parser))
18937 return argument;
18939 /* We're still not sure what the argument will be. */
18940 cp_parser_parse_tentatively (parser);
18941 /* Try a template. */
18942 argument_start_token = cp_lexer_peek_token (parser->lexer);
18943 argument = cp_parser_id_expression (parser,
18944 /*template_keyword_p=*/false,
18945 /*check_dependency_p=*/true,
18946 &template_p,
18947 /*declarator_p=*/false,
18948 /*optional_p=*/false);
18949 /* If the next token isn't a `,' or a `>', then this argument wasn't
18950 really finished. */
18951 if (!cp_parser_next_token_ends_template_argument_p (parser))
18952 cp_parser_error (parser, "expected template-argument");
18953 if (!cp_parser_error_occurred (parser))
18955 /* Figure out what is being referred to. If the id-expression
18956 was for a class template specialization, then we will have a
18957 TYPE_DECL at this point. There is no need to do name lookup
18958 at this point in that case. */
18959 if (TREE_CODE (argument) != TYPE_DECL)
18960 argument = cp_parser_lookup_name (parser, argument,
18961 none_type,
18962 /*is_template=*/template_p,
18963 /*is_namespace=*/false,
18964 /*check_dependency=*/true,
18965 /*ambiguous_decls=*/NULL,
18966 argument_start_token->location);
18967 if (TREE_CODE (argument) != TEMPLATE_DECL
18968 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
18969 cp_parser_error (parser, "expected template-name");
18971 if (cp_parser_parse_definitely (parser))
18973 if (TREE_UNAVAILABLE (argument))
18974 error_unavailable_use (argument, NULL_TREE);
18975 else if (TREE_DEPRECATED (argument))
18976 warn_deprecated_use (argument, NULL_TREE);
18977 return argument;
18979 /* It must be a non-type argument. In C++17 any constant-expression is
18980 allowed. */
18981 if (cxx_dialect > cxx14)
18982 goto general_expr;
18984 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
18986 -- an integral constant-expression of integral or enumeration
18987 type; or
18989 -- the name of a non-type template-parameter; or
18991 -- the name of an object or function with external linkage...
18993 -- the address of an object or function with external linkage...
18995 -- a pointer to member... */
18996 /* Look for a non-type template parameter. */
18997 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18999 cp_parser_parse_tentatively (parser);
19000 argument = cp_parser_primary_expression (parser,
19001 /*address_p=*/false,
19002 /*cast_p=*/false,
19003 /*template_arg_p=*/true,
19004 &idk);
19005 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
19006 || !cp_parser_next_token_ends_template_argument_p (parser))
19007 cp_parser_simulate_error (parser);
19008 if (cp_parser_parse_definitely (parser))
19009 return argument;
19012 /* If the next token is "&", the argument must be the address of an
19013 object or function with external linkage. */
19014 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
19015 if (address_p)
19017 loc = cp_lexer_peek_token (parser->lexer)->location;
19018 cp_lexer_consume_token (parser->lexer);
19020 /* See if we might have an id-expression. */
19021 token = cp_lexer_peek_token (parser->lexer);
19022 if (token->type == CPP_NAME
19023 || token->keyword == RID_OPERATOR
19024 || token->type == CPP_SCOPE
19025 || token->type == CPP_TEMPLATE_ID
19026 || token->type == CPP_NESTED_NAME_SPECIFIER)
19028 cp_parser_parse_tentatively (parser);
19029 argument = cp_parser_primary_expression (parser,
19030 address_p,
19031 /*cast_p=*/false,
19032 /*template_arg_p=*/true,
19033 &idk);
19034 if (cp_parser_error_occurred (parser)
19035 || !cp_parser_next_token_ends_template_argument_p (parser))
19036 cp_parser_abort_tentative_parse (parser);
19037 else
19039 tree probe;
19041 if (INDIRECT_REF_P (argument))
19043 /* Strip the dereference temporarily. */
19044 gcc_assert (REFERENCE_REF_P (argument));
19045 argument = TREE_OPERAND (argument, 0);
19048 /* If we're in a template, we represent a qualified-id referring
19049 to a static data member as a SCOPE_REF even if the scope isn't
19050 dependent so that we can check access control later. */
19051 probe = argument;
19052 if (TREE_CODE (probe) == SCOPE_REF)
19053 probe = TREE_OPERAND (probe, 1);
19054 if (VAR_P (probe))
19056 /* A variable without external linkage might still be a
19057 valid constant-expression, so no error is issued here
19058 if the external-linkage check fails. */
19059 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
19060 cp_parser_simulate_error (parser);
19062 else if (is_overloaded_fn (argument))
19063 /* All overloaded functions are allowed; if the external
19064 linkage test does not pass, an error will be issued
19065 later. */
19067 else if (address_p
19068 && (TREE_CODE (argument) == OFFSET_REF
19069 || TREE_CODE (argument) == SCOPE_REF))
19070 /* A pointer-to-member. */
19072 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
19074 else
19075 cp_parser_simulate_error (parser);
19077 if (cp_parser_parse_definitely (parser))
19079 if (address_p)
19080 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
19081 NULL_TREE, tf_warning_or_error);
19082 else
19083 argument = convert_from_reference (argument);
19084 return argument;
19088 /* If the argument started with "&", there are no other valid
19089 alternatives at this point. */
19090 if (address_p)
19092 cp_parser_error (parser, "invalid non-type template argument");
19093 return error_mark_node;
19096 general_expr:
19097 /* If the argument wasn't successfully parsed as a type-id followed
19098 by '>>', the argument can only be a constant expression now.
19099 Otherwise, we try parsing the constant-expression tentatively,
19100 because the argument could really be a type-id. */
19101 if (maybe_type_id)
19102 cp_parser_parse_tentatively (parser);
19104 if (cxx_dialect <= cxx14)
19105 argument = cp_parser_constant_expression (parser);
19106 else
19108 /* In C++20, we can encounter a braced-init-list. */
19109 if (cxx_dialect >= cxx20
19110 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19112 bool expr_non_constant_p;
19113 return cp_parser_braced_list (parser, &expr_non_constant_p);
19116 /* With C++17 generalized non-type template arguments we need to handle
19117 lvalue constant expressions, too. */
19118 argument = cp_parser_assignment_expression (parser);
19119 require_potential_constant_expression (argument);
19122 if (!maybe_type_id)
19123 return argument;
19124 if (!cp_parser_next_token_ends_template_argument_p (parser))
19125 cp_parser_error (parser, "expected template-argument");
19126 if (cp_parser_parse_definitely (parser))
19127 return argument;
19128 /* We did our best to parse the argument as a non type-id, but that
19129 was the only alternative that matched (albeit with a '>' after
19130 it). We can assume it's just a typo from the user, and a
19131 diagnostic will then be issued. */
19132 return cp_parser_template_type_arg (parser);
19135 /* Parse an explicit-instantiation.
19137 explicit-instantiation:
19138 template declaration
19140 Although the standard says `declaration', what it really means is:
19142 explicit-instantiation:
19143 template decl-specifier-seq [opt] declarator [opt] ;
19145 Things like `template int S<int>::i = 5, int S<double>::j;' are not
19146 supposed to be allowed. A defect report has been filed about this
19147 issue.
19149 GNU Extension:
19151 explicit-instantiation:
19152 storage-class-specifier template
19153 decl-specifier-seq [opt] declarator [opt] ;
19154 function-specifier template
19155 decl-specifier-seq [opt] declarator [opt] ; */
19157 static void
19158 cp_parser_explicit_instantiation (cp_parser* parser)
19160 int declares_class_or_enum;
19161 cp_decl_specifier_seq decl_specifiers;
19162 tree extension_specifier = NULL_TREE;
19164 auto_timevar tv (TV_TEMPLATE_INST);
19166 /* Look for an (optional) storage-class-specifier or
19167 function-specifier. */
19168 if (cp_parser_allow_gnu_extensions_p (parser))
19170 extension_specifier
19171 = cp_parser_storage_class_specifier_opt (parser);
19172 if (!extension_specifier)
19173 extension_specifier
19174 = cp_parser_function_specifier_opt (parser,
19175 /*decl_specs=*/NULL);
19178 /* Look for the `template' keyword. */
19179 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
19180 /* Let the front end know that we are processing an explicit
19181 instantiation. */
19182 begin_explicit_instantiation ();
19183 /* [temp.explicit] says that we are supposed to ignore access
19184 control while processing explicit instantiation directives. */
19185 push_deferring_access_checks (dk_no_check);
19186 /* Parse a decl-specifier-seq. */
19187 cp_parser_decl_specifier_seq (parser,
19188 CP_PARSER_FLAGS_OPTIONAL,
19189 &decl_specifiers,
19190 &declares_class_or_enum);
19192 cp_omp_declare_simd_data odsd;
19193 if (decl_specifiers.attributes && (flag_openmp || flag_openmp_simd))
19194 cp_parser_handle_directive_omp_attributes (parser,
19195 &decl_specifiers.attributes,
19196 &odsd, true);
19198 /* If there was exactly one decl-specifier, and it declared a class,
19199 and there's no declarator, then we have an explicit type
19200 instantiation. */
19201 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
19203 tree type = check_tag_decl (&decl_specifiers,
19204 /*explicit_type_instantiation_p=*/true);
19205 /* Turn access control back on for names used during
19206 template instantiation. */
19207 pop_deferring_access_checks ();
19208 if (type)
19209 do_type_instantiation (type, extension_specifier,
19210 /*complain=*/tf_error);
19212 else
19214 cp_declarator *declarator;
19215 tree decl;
19217 /* Parse the declarator. */
19218 declarator
19219 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19220 CP_PARSER_FLAGS_NONE,
19221 /*ctor_dtor_or_conv_p=*/NULL,
19222 /*parenthesized_p=*/NULL,
19223 /*member_p=*/false,
19224 /*friend_p=*/false,
19225 /*static_p=*/false);
19226 if (declares_class_or_enum & 2)
19227 cp_parser_check_for_definition_in_return_type (declarator,
19228 decl_specifiers.type,
19229 decl_specifiers.locations[ds_type_spec]);
19230 if (declarator != cp_error_declarator)
19232 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
19233 permerror (decl_specifiers.locations[ds_inline],
19234 "explicit instantiation shall not use"
19235 " %<inline%> specifier");
19236 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
19237 permerror (decl_specifiers.locations[ds_constexpr],
19238 "explicit instantiation shall not use"
19239 " %<constexpr%> specifier");
19240 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_consteval))
19241 permerror (decl_specifiers.locations[ds_consteval],
19242 "explicit instantiation shall not use"
19243 " %<consteval%> specifier");
19245 decl = grokdeclarator (declarator, &decl_specifiers,
19246 NORMAL, 0, &decl_specifiers.attributes);
19247 /* Turn access control back on for names used during
19248 template instantiation. */
19249 pop_deferring_access_checks ();
19250 /* Do the explicit instantiation. */
19251 do_decl_instantiation (decl, extension_specifier);
19253 else
19255 pop_deferring_access_checks ();
19256 /* Skip the body of the explicit instantiation. */
19257 cp_parser_skip_to_end_of_statement (parser);
19260 /* We're done with the instantiation. */
19261 end_explicit_instantiation ();
19263 cp_parser_consume_semicolon_at_end_of_statement (parser);
19265 cp_finalize_omp_declare_simd (parser, &odsd);
19268 /* Parse an explicit-specialization.
19270 explicit-specialization:
19271 template < > declaration
19273 Although the standard says `declaration', what it really means is:
19275 explicit-specialization:
19276 template <> decl-specifier [opt] init-declarator [opt] ;
19277 template <> function-definition
19278 template <> explicit-specialization
19279 template <> template-declaration */
19281 static void
19282 cp_parser_explicit_specialization (cp_parser* parser)
19284 cp_token *token = cp_lexer_peek_token (parser->lexer);
19286 /* Look for the `template' keyword. */
19287 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
19288 /* Look for the `<'. */
19289 cp_parser_require (parser, CPP_LESS, RT_LESS);
19290 /* Look for the `>'. */
19291 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
19292 /* We have processed another parameter list. */
19293 ++parser->num_template_parameter_lists;
19295 /* [temp]
19297 A template ... explicit specialization ... shall not have C
19298 linkage. */
19299 bool need_lang_pop = current_lang_name == lang_name_c;
19300 if (need_lang_pop)
19302 error_at (token->location, "template specialization with C linkage");
19303 maybe_show_extern_c_location ();
19305 /* Give it C++ linkage to avoid confusing other parts of the
19306 front end. */
19307 push_lang_context (lang_name_cplusplus);
19310 /* Let the front end know that we are beginning a specialization. */
19311 if (begin_specialization ())
19313 /* If the next keyword is `template', we need to figure out
19314 whether or not we're looking a template-declaration. */
19315 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19317 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
19318 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
19319 cp_parser_template_declaration_after_export (parser,
19320 /*member_p=*/false);
19321 else
19322 cp_parser_explicit_specialization (parser);
19324 else
19325 /* Parse the dependent declaration. */
19326 cp_parser_single_declaration (parser,
19327 /*checks=*/NULL,
19328 /*member_p=*/false,
19329 /*explicit_specialization_p=*/true,
19330 /*friend_p=*/NULL);
19333 /* We're done with the specialization. */
19334 end_specialization ();
19336 /* For the erroneous case of a template with C linkage, we pushed an
19337 implicit C++ linkage scope; exit that scope now. */
19338 if (need_lang_pop)
19339 pop_lang_context ();
19341 /* We're done with this parameter list. */
19342 --parser->num_template_parameter_lists;
19345 /* Preserve the attributes across a garbage collect (by making it a GC
19346 root), which can occur when parsing a member function. */
19348 static GTY(()) vec<tree, va_gc> *cp_parser_decl_specs_attrs;
19350 /* Parse a type-specifier.
19352 type-specifier:
19353 simple-type-specifier
19354 class-specifier
19355 enum-specifier
19356 elaborated-type-specifier
19357 cv-qualifier
19359 GNU Extension:
19361 type-specifier:
19362 __complex__
19364 Returns a representation of the type-specifier. For a
19365 class-specifier, enum-specifier, or elaborated-type-specifier, a
19366 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
19368 The parser flags FLAGS is used to control type-specifier parsing.
19370 If IS_DECLARATION is TRUE, then this type-specifier is appearing
19371 in a decl-specifier-seq.
19373 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
19374 class-specifier, enum-specifier, or elaborated-type-specifier, then
19375 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
19376 if a type is declared; 2 if it is defined. Otherwise, it is set to
19377 zero.
19379 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
19380 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
19381 is set to FALSE. */
19383 static tree
19384 cp_parser_type_specifier (cp_parser* parser,
19385 cp_parser_flags flags,
19386 cp_decl_specifier_seq *decl_specs,
19387 bool is_declaration,
19388 int* declares_class_or_enum,
19389 bool* is_cv_qualifier)
19391 tree type_spec = NULL_TREE;
19392 cp_token *token;
19393 enum rid keyword;
19394 cp_decl_spec ds = ds_last;
19396 /* Assume this type-specifier does not declare a new type. */
19397 if (declares_class_or_enum)
19398 *declares_class_or_enum = 0;
19399 /* And that it does not specify a cv-qualifier. */
19400 if (is_cv_qualifier)
19401 *is_cv_qualifier = false;
19402 /* Peek at the next token. */
19403 token = cp_lexer_peek_token (parser->lexer);
19405 /* If we're looking at a keyword, we can use that to guide the
19406 production we choose. */
19407 keyword = token->keyword;
19408 switch (keyword)
19410 case RID_ENUM:
19411 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
19412 goto elaborated_type_specifier;
19414 /* Look for the enum-specifier. */
19415 type_spec = cp_parser_enum_specifier (parser);
19416 /* If that worked, we're done. */
19417 if (type_spec)
19419 if (declares_class_or_enum)
19420 *declares_class_or_enum = 2;
19421 if (decl_specs)
19422 cp_parser_set_decl_spec_type (decl_specs,
19423 type_spec,
19424 token,
19425 /*type_definition_p=*/true);
19426 return type_spec;
19428 else
19429 goto elaborated_type_specifier;
19431 /* Any of these indicate either a class-specifier, or an
19432 elaborated-type-specifier. */
19433 case RID_CLASS:
19434 case RID_STRUCT:
19435 case RID_UNION:
19436 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
19437 goto elaborated_type_specifier;
19439 /* Parse tentatively so that we can back up if we don't find a
19440 class-specifier. */
19441 cp_parser_parse_tentatively (parser);
19442 if (decl_specs->attributes)
19443 vec_safe_push (cp_parser_decl_specs_attrs, decl_specs->attributes);
19444 /* Look for the class-specifier. */
19445 type_spec = cp_parser_class_specifier (parser);
19446 if (decl_specs->attributes)
19447 cp_parser_decl_specs_attrs->pop ();
19448 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
19449 /* If that worked, we're done. */
19450 if (cp_parser_parse_definitely (parser))
19452 if (declares_class_or_enum)
19453 *declares_class_or_enum = 2;
19454 if (decl_specs)
19455 cp_parser_set_decl_spec_type (decl_specs,
19456 type_spec,
19457 token,
19458 /*type_definition_p=*/true);
19459 return type_spec;
19462 /* Fall through. */
19463 elaborated_type_specifier:
19464 /* We're declaring (not defining) a class or enum. */
19465 if (declares_class_or_enum)
19466 *declares_class_or_enum = 1;
19468 /* Fall through. */
19469 case RID_TYPENAME:
19470 /* Look for an elaborated-type-specifier. */
19471 type_spec
19472 = (cp_parser_elaborated_type_specifier
19473 (parser,
19474 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
19475 is_declaration));
19476 if (decl_specs)
19477 cp_parser_set_decl_spec_type (decl_specs,
19478 type_spec,
19479 token,
19480 /*type_definition_p=*/false);
19481 return type_spec;
19483 case RID_CONST:
19484 ds = ds_const;
19485 if (is_cv_qualifier)
19486 *is_cv_qualifier = true;
19487 break;
19489 case RID_VOLATILE:
19490 ds = ds_volatile;
19491 if (is_cv_qualifier)
19492 *is_cv_qualifier = true;
19493 break;
19495 case RID_RESTRICT:
19496 ds = ds_restrict;
19497 if (is_cv_qualifier)
19498 *is_cv_qualifier = true;
19499 break;
19501 case RID_COMPLEX:
19502 /* The `__complex__' keyword is a GNU extension. */
19503 ds = ds_complex;
19504 break;
19506 default:
19507 break;
19510 /* Handle simple keywords. */
19511 if (ds != ds_last)
19513 if (decl_specs)
19515 set_and_check_decl_spec_loc (decl_specs, ds, token);
19516 decl_specs->any_specifiers_p = true;
19518 return cp_lexer_consume_token (parser->lexer)->u.value;
19521 /* If we do not already have a type-specifier, assume we are looking
19522 at a simple-type-specifier. */
19523 type_spec = cp_parser_simple_type_specifier (parser,
19524 decl_specs,
19525 flags);
19527 /* If we didn't find a type-specifier, and a type-specifier was not
19528 optional in this context, issue an error message. */
19529 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
19531 cp_parser_error (parser, "expected type specifier");
19532 return error_mark_node;
19535 return type_spec;
19538 /* Parse a simple-type-specifier.
19540 simple-type-specifier:
19541 :: [opt] nested-name-specifier [opt] type-name
19542 :: [opt] nested-name-specifier template template-id
19543 char
19544 wchar_t
19545 bool
19546 short
19548 long
19549 signed
19550 unsigned
19551 float
19552 double
19553 void
19555 C++11 Extension:
19557 simple-type-specifier:
19558 auto
19559 decltype ( expression )
19560 char16_t
19561 char32_t
19562 __underlying_type ( type-id )
19564 C++17 extension:
19566 nested-name-specifier(opt) template-name
19568 GNU Extension:
19570 simple-type-specifier:
19571 __int128
19572 __typeof__ unary-expression
19573 __typeof__ ( type-id )
19574 __typeof__ ( type-id ) { initializer-list , [opt] }
19576 Concepts Extension:
19578 simple-type-specifier:
19579 constrained-type-specifier
19581 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
19582 appropriately updated. */
19584 static tree
19585 cp_parser_simple_type_specifier (cp_parser* parser,
19586 cp_decl_specifier_seq *decl_specs,
19587 cp_parser_flags flags)
19589 tree type = NULL_TREE;
19590 cp_token *token;
19591 int idx;
19593 /* Peek at the next token. */
19594 token = cp_lexer_peek_token (parser->lexer);
19596 /* If we're looking at a keyword, things are easy. */
19597 switch (token->keyword)
19599 case RID_CHAR:
19600 if (decl_specs)
19601 decl_specs->explicit_char_p = true;
19602 type = char_type_node;
19603 break;
19604 case RID_CHAR8:
19605 type = char8_type_node;
19606 break;
19607 case RID_CHAR16:
19608 type = char16_type_node;
19609 break;
19610 case RID_CHAR32:
19611 type = char32_type_node;
19612 break;
19613 case RID_WCHAR:
19614 type = wchar_type_node;
19615 break;
19616 case RID_BOOL:
19617 type = boolean_type_node;
19618 break;
19619 case RID_SHORT:
19620 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
19621 type = short_integer_type_node;
19622 break;
19623 case RID_INT:
19624 if (decl_specs)
19625 decl_specs->explicit_int_p = true;
19626 type = integer_type_node;
19627 break;
19628 case RID_INT_N_0:
19629 case RID_INT_N_1:
19630 case RID_INT_N_2:
19631 case RID_INT_N_3:
19632 idx = token->keyword - RID_INT_N_0;
19633 if (! int_n_enabled_p [idx])
19634 break;
19635 if (decl_specs)
19637 decl_specs->explicit_intN_p = true;
19638 decl_specs->int_n_idx = idx;
19639 /* Check if the alternate "__intN__" form has been used instead of
19640 "__intN". */
19641 if (startswith (IDENTIFIER_POINTER (token->u.value)
19642 + (IDENTIFIER_LENGTH (token->u.value) - 2), "__"))
19643 decl_specs->int_n_alt = true;
19645 type = int_n_trees [idx].signed_type;
19646 break;
19647 case RID_LONG:
19648 if (decl_specs)
19649 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
19650 type = long_integer_type_node;
19651 break;
19652 case RID_SIGNED:
19653 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
19654 type = integer_type_node;
19655 break;
19656 case RID_UNSIGNED:
19657 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
19658 type = unsigned_type_node;
19659 break;
19660 case RID_FLOAT:
19661 type = float_type_node;
19662 break;
19663 case RID_DOUBLE:
19664 type = double_type_node;
19665 break;
19666 CASE_RID_FLOATN_NX:
19667 type = FLOATN_NX_TYPE_NODE (token->keyword - RID_FLOATN_NX_FIRST);
19668 if (type == NULL_TREE)
19669 error ("%<_Float%d%s%> is not supported on this target",
19670 floatn_nx_types[token->keyword - RID_FLOATN_NX_FIRST].n,
19671 floatn_nx_types[token->keyword - RID_FLOATN_NX_FIRST].extended
19672 ? "x" : "");
19673 break;
19674 case RID_VOID:
19675 type = void_type_node;
19676 break;
19678 case RID_AUTO:
19679 maybe_warn_cpp0x (CPP0X_AUTO);
19680 if (parser->auto_is_implicit_function_template_parm_p)
19682 /* The 'auto' might be the placeholder return type for a function decl
19683 with trailing return type. */
19684 bool have_trailing_return_fn_decl = false;
19686 cp_parser_parse_tentatively (parser);
19687 cp_lexer_consume_token (parser->lexer);
19688 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
19689 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
19690 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
19691 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
19693 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19695 cp_lexer_consume_token (parser->lexer);
19696 cp_parser_skip_to_closing_parenthesis (parser,
19697 /*recovering*/false,
19698 /*or_comma*/false,
19699 /*consume_paren*/true);
19700 continue;
19703 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
19705 have_trailing_return_fn_decl = true;
19706 break;
19709 cp_lexer_consume_token (parser->lexer);
19711 cp_parser_abort_tentative_parse (parser);
19713 if (have_trailing_return_fn_decl)
19715 type = make_auto ();
19716 break;
19719 if (cxx_dialect >= cxx14)
19721 type = synthesize_implicit_template_parm (parser, NULL_TREE);
19722 type = TREE_TYPE (type);
19724 else
19725 type = error_mark_node;
19727 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
19729 if (cxx_dialect < cxx14)
19730 error_at (token->location,
19731 "use of %<auto%> in lambda parameter declaration "
19732 "only available with "
19733 "%<-std=c++14%> or %<-std=gnu++14%>");
19735 else if (cxx_dialect < cxx14)
19736 error_at (token->location,
19737 "use of %<auto%> in parameter declaration "
19738 "only available with "
19739 "%<-std=c++14%> or %<-std=gnu++14%>");
19740 else if (!flag_concepts)
19741 pedwarn (token->location, 0,
19742 "use of %<auto%> in parameter declaration "
19743 "only available with %<-std=c++20%> or %<-fconcepts%>");
19745 else
19746 type = make_auto ();
19747 break;
19749 case RID_DECLTYPE:
19750 /* Since DR 743, decltype can either be a simple-type-specifier by
19751 itself or begin a nested-name-specifier. Parsing it will replace
19752 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
19753 handling below decide what to do. */
19754 cp_parser_decltype (parser);
19755 cp_lexer_set_token_position (parser->lexer, token);
19756 break;
19758 case RID_TYPEOF:
19759 /* Consume the `typeof' token. */
19760 cp_lexer_consume_token (parser->lexer);
19761 /* Parse the operand to `typeof'. */
19762 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
19763 /* If it is not already a TYPE, take its type. */
19764 if (!TYPE_P (type))
19765 type = finish_typeof (type);
19767 if (decl_specs)
19768 cp_parser_set_decl_spec_type (decl_specs, type,
19769 token,
19770 /*type_definition_p=*/false);
19772 return type;
19774 #define DEFTRAIT_TYPE(CODE, NAME, ARITY) \
19775 case RID_##CODE:
19776 #include "cp-trait.def"
19777 #undef DEFTRAIT_TYPE
19778 type = cp_parser_trait (parser, token->keyword);
19779 if (decl_specs)
19780 cp_parser_set_decl_spec_type (decl_specs, type,
19781 token,
19782 /*type_definition_p=*/false);
19784 return type;
19786 default:
19787 break;
19790 /* If token is an already-parsed decltype not followed by ::,
19791 it's a simple-type-specifier. */
19792 if (token->type == CPP_DECLTYPE
19793 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
19795 type = saved_checks_value (token->u.tree_check_value);
19796 if (decl_specs)
19798 cp_parser_set_decl_spec_type (decl_specs, type,
19799 token,
19800 /*type_definition_p=*/false);
19801 /* Remember that we are handling a decltype in order to
19802 implement the resolution of DR 1510 when the argument
19803 isn't instantiation dependent. */
19804 decl_specs->decltype_p = true;
19806 cp_lexer_consume_token (parser->lexer);
19807 return type;
19810 /* If the type-specifier was for a built-in type, we're done. */
19811 if (type)
19813 /* Record the type. */
19814 if (decl_specs
19815 && (token->keyword != RID_SIGNED
19816 && token->keyword != RID_UNSIGNED
19817 && token->keyword != RID_SHORT
19818 && token->keyword != RID_LONG))
19819 cp_parser_set_decl_spec_type (decl_specs,
19820 type,
19821 token,
19822 /*type_definition_p=*/false);
19823 if (decl_specs)
19824 decl_specs->any_specifiers_p = true;
19826 /* Consume the token. */
19827 cp_lexer_consume_token (parser->lexer);
19829 if (type == error_mark_node)
19830 return error_mark_node;
19832 /* There is no valid C++ program where a non-template type is
19833 followed by a "<". That usually indicates that the user thought
19834 that the type was a template. */
19835 cp_parser_check_for_invalid_template_id (parser, type, none_type,
19836 token->location);
19838 return TYPE_NAME (type);
19841 /* The type-specifier must be a user-defined type. */
19842 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
19844 bool qualified_p;
19845 bool global_p;
19846 const bool typename_p = (cxx_dialect >= cxx20
19847 && (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
19849 /* Don't gobble tokens or issue error messages if this is an
19850 optional type-specifier. */
19851 if (flags & CP_PARSER_FLAGS_OPTIONAL)
19852 cp_parser_parse_tentatively (parser);
19854 /* Remember current tentative parsing state -- if we know we need
19855 a type, we can give better diagnostics here. */
19856 bool tent = cp_parser_parsing_tentatively (parser);
19858 token = cp_lexer_peek_token (parser->lexer);
19860 /* Look for the optional `::' operator. */
19861 global_p
19862 = (cp_parser_global_scope_opt (parser,
19863 /*current_scope_valid_p=*/false)
19864 != NULL_TREE);
19865 /* Look for the nested-name specifier. */
19866 qualified_p
19867 = (cp_parser_nested_name_specifier_opt (parser,
19868 /*typename_keyword_p=*/false,
19869 /*check_dependency_p=*/true,
19870 /*type_p=*/false,
19871 /*is_declaration=*/false)
19872 != NULL_TREE);
19873 /* If we have seen a nested-name-specifier, and the next token
19874 is `template', then we are using the template-id production. */
19875 if (parser->scope
19876 && cp_parser_optional_template_keyword (parser))
19878 /* Look for the template-id. */
19879 type = cp_parser_template_id (parser,
19880 /*template_keyword_p=*/true,
19881 /*check_dependency_p=*/true,
19882 none_type,
19883 /*is_declaration=*/false);
19884 /* If the template-id did not name a type, we are out of
19885 luck. */
19886 if (TREE_CODE (type) != TYPE_DECL)
19888 /* ...unless we pretend we have seen 'typename'. */
19889 if (typename_p)
19890 type = cp_parser_make_typename_type (parser, type,
19891 token->location);
19892 else
19894 cp_parser_error (parser, "expected template-id for type");
19895 type = error_mark_node;
19899 /* DR 1812: A < following a qualified-id in a typename-specifier
19900 could safely be assumed to begin a template argument list, so
19901 the template keyword should be optional. */
19902 else if (parser->scope
19903 && qualified_p
19904 && typename_p
19905 && cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID))
19907 cp_parser_parse_tentatively (parser);
19909 type = cp_parser_template_id (parser,
19910 /*template_keyword_p=*/true,
19911 /*check_dependency_p=*/true,
19912 none_type,
19913 /*is_declaration=*/false);
19914 /* This is handled below, so back off. */
19915 if (type && concept_check_p (type))
19916 cp_parser_simulate_error (parser);
19918 if (!cp_parser_parse_definitely (parser))
19919 type = NULL_TREE;
19920 else if (TREE_CODE (type) == TEMPLATE_ID_EXPR)
19921 type = make_typename_type (parser->scope, type, typename_type,
19922 /*complain=*/tf_error);
19923 else if (TREE_CODE (type) != TYPE_DECL)
19924 type = NULL_TREE;
19927 /* Otherwise, look for a type-name. */
19928 if (!type)
19930 if (cxx_dialect >= cxx17)
19931 cp_parser_parse_tentatively (parser);
19933 type = cp_parser_type_name (parser, (qualified_p && typename_p));
19935 if (cxx_dialect >= cxx17 && !cp_parser_parse_definitely (parser))
19936 type = NULL_TREE;
19939 if (!type && flag_concepts && decl_specs)
19941 /* Try for a type-constraint with template arguments. We check
19942 decl_specs here to avoid trying this for a functional cast. */
19944 cp_parser_parse_tentatively (parser);
19946 type = cp_parser_template_id (parser,
19947 /*template_keyword_p=*/false,
19948 /*check_dependency_p=*/true,
19949 none_type,
19950 /*is_declaration=*/false);
19951 if (type && concept_check_p (type))
19953 location_t loc = EXPR_LOCATION (type);
19954 type = cp_parser_placeholder_type_specifier (parser, loc,
19955 type, tent);
19956 if (tent && type == error_mark_node)
19957 /* Perhaps it's a concept-check expression. */
19958 cp_parser_simulate_error (parser);
19960 else
19961 cp_parser_simulate_error (parser);
19963 if (!cp_parser_parse_definitely (parser))
19964 type = NULL_TREE;
19967 if (!type && cxx_dialect >= cxx17)
19969 /* Try class template argument deduction or type-constraint without
19970 template arguments. */
19971 tree name = cp_parser_identifier (parser);
19972 if (name && TREE_CODE (name) == IDENTIFIER_NODE
19973 && parser->scope != error_mark_node)
19975 location_t loc
19976 = cp_lexer_previous_token (parser->lexer)->location;
19977 tree tmpl = cp_parser_lookup_name (parser, name,
19978 none_type,
19979 /*is_template=*/false,
19980 /*is_namespace=*/false,
19981 /*check_dependency=*/true,
19982 /*ambiguous_decls=*/NULL,
19983 token->location);
19984 if (tmpl && tmpl != error_mark_node
19985 && ctad_template_p (tmpl))
19986 type = make_template_placeholder (tmpl);
19987 else if (flag_concepts && tmpl && concept_definition_p (tmpl))
19988 type = cp_parser_placeholder_type_specifier (parser, loc,
19989 tmpl, tent);
19990 else
19992 type = error_mark_node;
19993 if (!cp_parser_simulate_error (parser))
19994 cp_parser_name_lookup_error (parser, name, tmpl,
19995 NLE_TYPE, token->location);
19998 else
19999 type = error_mark_node;
20002 /* If it didn't work out, we don't have a TYPE. */
20003 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
20004 && !cp_parser_parse_definitely (parser))
20005 type = NULL_TREE;
20007 /* Keep track of all name-lookups performed in class scopes. */
20008 if (type
20009 && !global_p
20010 && !qualified_p
20011 && TREE_CODE (type) == TYPE_DECL
20012 && identifier_p (DECL_NAME (type)))
20013 maybe_note_name_used_in_class (DECL_NAME (type), type);
20015 if (type && decl_specs)
20016 cp_parser_set_decl_spec_type (decl_specs, type,
20017 token,
20018 /*type_definition_p=*/false);
20021 /* If we didn't get a type-name, issue an error message. */
20022 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
20024 cp_parser_error (parser, "expected type-name");
20025 return error_mark_node;
20028 if (type && type != error_mark_node)
20030 /* See if TYPE is an Objective-C type, and if so, parse and
20031 accept any protocol references following it. Do this before
20032 the cp_parser_check_for_invalid_template_id() call, because
20033 Objective-C types can be followed by '<...>' which would
20034 enclose protocol names rather than template arguments, and so
20035 everything is fine. */
20036 if (c_dialect_objc () && !parser->scope
20037 && (objc_is_id (type) || objc_is_class_name (type)))
20039 tree protos = cp_parser_objc_protocol_refs_opt (parser);
20040 tree qual_type = objc_get_protocol_qualified_type (type, protos);
20042 /* Clobber the "unqualified" type previously entered into
20043 DECL_SPECS with the new, improved protocol-qualified version. */
20044 if (decl_specs)
20045 decl_specs->type = qual_type;
20047 return qual_type;
20050 /* There is no valid C++ program where a non-template type is
20051 followed by a "<". That usually indicates that the user
20052 thought that the type was a template. */
20053 cp_parser_check_for_invalid_template_id (parser, type,
20054 none_type,
20055 token->location);
20058 return type;
20061 /* Parse the remainder of a placholder-type-specifier.
20063 placeholder-type-specifier:
20064 type-constraint_opt auto
20065 type-constraint_opt decltype(auto)
20067 The raw form of the constraint is parsed in cp_parser_simple_type_specifier
20068 and passed as TMPL. This function converts TMPL to an actual type-constraint,
20069 parses the placeholder type, and performs some contextual syntactic analysis.
20071 LOC provides the location of the template name.
20073 TENTATIVE is true if the type-specifier parsing is tentative; in that case,
20074 don't give an error if TMPL isn't a valid type-constraint, as the template-id
20075 might actually be a concept-check,
20077 Note that the Concepts TS allows the auto or decltype(auto) to be
20078 omitted in a constrained-type-specifier. */
20080 static tree
20081 cp_parser_placeholder_type_specifier (cp_parser *parser, location_t loc,
20082 tree tmpl, bool tentative)
20084 if (tmpl == error_mark_node)
20085 return error_mark_node;
20087 tree orig_tmpl = tmpl;
20089 /* Get the arguments as written for subsequent analysis. */
20090 tree args = NULL_TREE;
20091 if (TREE_CODE (tmpl) == TEMPLATE_ID_EXPR)
20093 args = TREE_OPERAND (tmpl, 1);
20094 tmpl = TREE_OPERAND (tmpl, 0);
20096 else
20097 /* A concept-name with no arguments can't be an expression. */
20098 tentative = false;
20100 tsubst_flags_t complain = tentative ? tf_none : tf_warning_or_error;
20102 /* Get the concept and prototype parameter for the constraint. */
20103 tree_pair info = finish_type_constraints (tmpl, args, complain);
20104 tree con = info.first;
20105 tree proto = info.second;
20106 if (con == error_mark_node)
20107 return error_mark_node;
20109 /* As per the standard, require auto or decltype(auto), except in some
20110 cases (template parameter lists, -fconcepts-ts enabled). */
20111 cp_token *placeholder = NULL, *close_paren = NULL;
20112 if (cxx_dialect >= cxx20)
20114 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
20115 placeholder = cp_lexer_consume_token (parser->lexer);
20116 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DECLTYPE))
20118 placeholder = cp_lexer_consume_token (parser->lexer);
20119 matching_parens parens;
20120 parens.require_open (parser);
20121 cp_parser_require_keyword (parser, RID_AUTO, RT_AUTO);
20122 close_paren = parens.require_close (parser);
20126 /* A type constraint constrains a contextually determined type or type
20127 parameter pack. However, the Concepts TS does allow concepts
20128 to introduce non-type and template template parameters. */
20129 if (TREE_CODE (proto) != TYPE_DECL)
20131 if (!flag_concepts_ts
20132 || !processing_template_parmlist)
20134 if (!tentative)
20136 error_at (loc, "%qE does not constrain a type", DECL_NAME (con));
20137 inform (DECL_SOURCE_LOCATION (con), "concept defined here");
20139 return error_mark_node;
20143 /* In a template parameter list, a type-parameter can be introduced
20144 by type-constraints alone. */
20145 if (processing_template_parmlist && !placeholder)
20147 /* In a default argument we may not be creating new parameters. */
20148 if (parser->local_variables_forbidden_p & LOCAL_VARS_FORBIDDEN)
20150 /* If this assert turns out to be false, do error() instead. */
20151 gcc_assert (tentative);
20152 return error_mark_node;
20154 return build_constrained_parameter (con, proto, args);
20157 /* Diagnose issues placeholder issues. */
20158 if (!flag_concepts_ts
20159 && !parser->in_result_type_constraint_p
20160 && !placeholder)
20162 if (tentative)
20163 /* Perhaps it's a concept-check expression (c++/91073). */
20164 return error_mark_node;
20166 tree id = build_nt (TEMPLATE_ID_EXPR, tmpl, args);
20167 tree expr = DECL_P (orig_tmpl) ? DECL_NAME (con) : id;
20168 error_at (input_location,
20169 "expected %<auto%> or %<decltype(auto)%> after %qE", expr);
20170 /* Fall through. This is an error of omission. */
20172 else if (parser->in_result_type_constraint_p && placeholder)
20174 /* A trailing return type only allows type-constraints. */
20175 error_at (input_location,
20176 "unexpected placeholder in constrained result type");
20179 /* In a parameter-declaration-clause, a placeholder-type-specifier
20180 results in an invented template parameter. */
20181 if (parser->auto_is_implicit_function_template_parm_p)
20183 if (close_paren)
20185 location_t loc = make_location (placeholder->location,
20186 placeholder->location,
20187 close_paren->location);
20188 error_at (loc, "cannot declare a parameter with %<decltype(auto)%>");
20189 return error_mark_node;
20191 tree parm = build_constrained_parameter (con, proto, args);
20192 return synthesize_implicit_template_parm (parser, parm);
20195 /* Determine if the type should be deduced using template argument
20196 deduction or decltype deduction. Note that the latter is always
20197 used for type-constraints in trailing return types. */
20198 bool decltype_p = placeholder
20199 ? placeholder->keyword == RID_DECLTYPE
20200 : parser->in_result_type_constraint_p;
20202 /* Otherwise, this is the type of a variable or return type. */
20203 if (decltype_p)
20204 return make_constrained_decltype_auto (con, args);
20205 else
20206 return make_constrained_auto (con, args);
20209 /* Parse a type-name.
20211 type-name:
20212 class-name
20213 enum-name
20214 typedef-name
20215 simple-template-id [in c++0x]
20217 enum-name:
20218 identifier
20220 typedef-name:
20221 identifier
20223 Concepts:
20225 type-name:
20226 concept-name
20227 partial-concept-id
20229 concept-name:
20230 identifier
20232 Returns a TYPE_DECL for the type. */
20234 static tree
20235 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
20237 tree type_decl;
20239 /* We can't know yet whether it is a class-name or not. */
20240 cp_parser_parse_tentatively (parser);
20241 /* Try a class-name. */
20242 type_decl = cp_parser_class_name (parser,
20243 typename_keyword_p,
20244 /*template_keyword_p=*/false,
20245 none_type,
20246 /*check_dependency_p=*/true,
20247 /*class_head_p=*/false,
20248 /*is_declaration=*/false);
20249 /* If it's not a class-name, keep looking. */
20250 if (!cp_parser_parse_definitely (parser))
20252 if (cxx_dialect < cxx11)
20253 /* It must be a typedef-name or an enum-name. */
20254 return cp_parser_nonclass_name (parser);
20256 cp_parser_parse_tentatively (parser);
20257 /* It is either a simple-template-id representing an
20258 instantiation of an alias template... */
20259 type_decl = cp_parser_template_id (parser,
20260 /*template_keyword_p=*/false,
20261 /*check_dependency_p=*/true,
20262 none_type,
20263 /*is_declaration=*/false);
20264 /* Note that this must be an instantiation of an alias template
20265 because [temp.names]/6 says:
20267 A template-id that names an alias template specialization
20268 is a type-name.
20270 Whereas [temp.names]/7 says:
20272 A simple-template-id that names a class template
20273 specialization is a class-name.
20275 With concepts, this could also be a partial-concept-id that
20276 declares a non-type template parameter. */
20277 if (type_decl != NULL_TREE
20278 && TREE_CODE (type_decl) == TYPE_DECL
20279 && TYPE_DECL_ALIAS_P (type_decl))
20280 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
20281 else
20282 cp_parser_simulate_error (parser);
20284 if (!cp_parser_parse_definitely (parser))
20285 /* ... Or a typedef-name or an enum-name. */
20286 return cp_parser_nonclass_name (parser);
20289 return type_decl;
20292 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
20293 or a concept-name.
20295 enum-name:
20296 identifier
20298 typedef-name:
20299 identifier
20301 concept-name:
20302 identifier
20304 Returns a TYPE_DECL for the type. */
20306 static tree
20307 cp_parser_nonclass_name (cp_parser* parser)
20309 tree type_decl;
20310 tree identifier;
20312 cp_token *token = cp_lexer_peek_token (parser->lexer);
20313 identifier = cp_parser_identifier (parser);
20314 if (identifier == error_mark_node)
20315 return error_mark_node;
20317 /* Look up the type-name. */
20318 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
20320 type_decl = strip_using_decl (type_decl);
20322 if (TREE_CODE (type_decl) != TYPE_DECL
20323 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
20325 /* See if this is an Objective-C type. */
20326 tree protos = cp_parser_objc_protocol_refs_opt (parser);
20327 tree type = objc_get_protocol_qualified_type (identifier, protos);
20328 if (type)
20329 type_decl = TYPE_NAME (type);
20332 /* Issue an error if we did not find a type-name. */
20333 if (TREE_CODE (type_decl) != TYPE_DECL
20334 /* In Objective-C, we have the complication that class names are
20335 normally type names and start declarations (eg, the
20336 "NSObject" in "NSObject *object;"), but can be used in an
20337 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
20338 is an expression. So, a classname followed by a dot is not a
20339 valid type-name. */
20340 || (objc_is_class_name (TREE_TYPE (type_decl))
20341 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
20343 if (!cp_parser_simulate_error (parser))
20344 cp_parser_name_lookup_error (parser, identifier, type_decl,
20345 NLE_TYPE, token->location);
20346 return error_mark_node;
20348 /* Remember that the name was used in the definition of the
20349 current class so that we can check later to see if the
20350 meaning would have been different after the class was
20351 entirely defined. */
20352 else if (type_decl != error_mark_node
20353 && !parser->scope)
20354 maybe_note_name_used_in_class (identifier, type_decl);
20356 return type_decl;
20359 /* Parse an elaborated-type-specifier. Note that the grammar given
20360 here incorporates the resolution to DR68.
20362 elaborated-type-specifier:
20363 class-key :: [opt] nested-name-specifier [opt] identifier
20364 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
20365 enum-key :: [opt] nested-name-specifier [opt] identifier
20366 typename :: [opt] nested-name-specifier identifier
20367 typename :: [opt] nested-name-specifier template [opt]
20368 template-id
20370 GNU extension:
20372 elaborated-type-specifier:
20373 class-key attributes :: [opt] nested-name-specifier [opt] identifier
20374 class-key attributes :: [opt] nested-name-specifier [opt]
20375 template [opt] template-id
20376 enum attributes :: [opt] nested-name-specifier [opt] identifier
20378 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
20379 declared `friend'. If IS_DECLARATION is TRUE, then this
20380 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
20381 something is being declared.
20383 Returns the TYPE specified. */
20385 static tree
20386 cp_parser_elaborated_type_specifier (cp_parser* parser,
20387 bool is_friend,
20388 bool is_declaration)
20390 enum tag_types tag_type;
20391 tree identifier;
20392 tree type = NULL_TREE;
20393 tree attributes = NULL_TREE;
20394 tree globalscope;
20395 cp_token *token = NULL;
20397 /* For class and enum types the location of the class-key or enum-key. */
20398 location_t key_loc = cp_lexer_peek_token (parser->lexer)->location;
20399 /* For a scoped enum, the 'class' or 'struct' keyword id. */
20400 rid scoped_key = RID_MAX;
20402 /* See if we're looking at the `enum' keyword. */
20403 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
20405 /* Consume the `enum' token. */
20406 cp_lexer_consume_token (parser->lexer);
20407 /* Remember that it's an enumeration type. */
20408 tag_type = enum_type;
20409 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
20410 enums) is used here. */
20411 cp_token *token = cp_lexer_peek_token (parser->lexer);
20412 if (cp_parser_is_keyword (token, scoped_key = RID_CLASS)
20413 || cp_parser_is_keyword (token, scoped_key = RID_STRUCT))
20415 location_t loc = token->location;
20416 gcc_rich_location richloc (loc);
20417 richloc.add_range (input_location);
20418 richloc.add_fixit_remove ();
20419 pedwarn (&richloc, 0, "elaborated-type-specifier for "
20420 "a scoped enum must not use the %qD keyword",
20421 token->u.value);
20422 /* Consume the `struct' or `class' and parse it anyway. */
20423 cp_lexer_consume_token (parser->lexer);
20424 /* Create a combined location for the whole scoped-enum-key. */
20425 key_loc = make_location (key_loc, key_loc, loc);
20427 else
20428 scoped_key = RID_MAX;
20430 /* Parse the attributes. */
20431 attributes = cp_parser_attributes_opt (parser);
20433 /* Or, it might be `typename'. */
20434 else if (cp_lexer_next_token_is_keyword (parser->lexer,
20435 RID_TYPENAME))
20437 /* Consume the `typename' token. */
20438 cp_lexer_consume_token (parser->lexer);
20439 /* Remember that it's a `typename' type. */
20440 tag_type = typename_type;
20442 /* Otherwise it must be a class-key. */
20443 else
20445 key_loc = cp_lexer_peek_token (parser->lexer)->location;
20446 tag_type = cp_parser_class_key (parser);
20447 if (tag_type == none_type)
20448 return error_mark_node;
20449 /* Parse the attributes. */
20450 attributes = cp_parser_attributes_opt (parser);
20453 /* Look for the `::' operator. */
20454 globalscope = cp_parser_global_scope_opt (parser,
20455 /*current_scope_valid_p=*/false);
20456 /* Look for the nested-name-specifier. */
20457 tree nested_name_specifier;
20458 if (tag_type == typename_type && !globalscope)
20460 nested_name_specifier
20461 = cp_parser_nested_name_specifier (parser,
20462 /*typename_keyword_p=*/true,
20463 /*check_dependency_p=*/true,
20464 /*type_p=*/true,
20465 is_declaration);
20466 if (!nested_name_specifier)
20467 return error_mark_node;
20469 else
20470 /* Even though `typename' is not present, the proposed resolution
20471 to Core Issue 180 says that in `class A<T>::B', `B' should be
20472 considered a type-name, even if `A<T>' is dependent. */
20473 nested_name_specifier
20474 = cp_parser_nested_name_specifier_opt (parser,
20475 /*typename_keyword_p=*/true,
20476 /*check_dependency_p=*/true,
20477 /*type_p=*/true,
20478 is_declaration);
20479 /* For everything but enumeration types, consider a template-id.
20480 For an enumeration type, consider only a plain identifier. */
20481 if (tag_type != enum_type)
20483 bool template_p = false;
20484 tree decl;
20486 /* Allow the `template' keyword. */
20487 template_p = cp_parser_optional_template_keyword (parser);
20488 /* If we didn't see `template', we don't know if there's a
20489 template-id or not. */
20490 if (!template_p)
20491 cp_parser_parse_tentatively (parser);
20492 /* The `template' keyword must follow a nested-name-specifier. */
20493 else if (!nested_name_specifier && !globalscope)
20495 cp_parser_error (parser, "%<template%> must follow a nested-"
20496 "name-specifier");
20497 return error_mark_node;
20500 /* Parse the template-id. */
20501 token = cp_lexer_peek_token (parser->lexer);
20502 decl = cp_parser_template_id (parser, template_p,
20503 /*check_dependency_p=*/true,
20504 tag_type,
20505 is_declaration);
20506 /* If we didn't find a template-id, look for an ordinary
20507 identifier. */
20508 if (!template_p && !cp_parser_parse_definitely (parser))
20510 /* We can get here when cp_parser_template_id, called by
20511 cp_parser_class_name with tag_type == none_type, succeeds
20512 and caches a BASELINK. Then, when called again here,
20513 instead of failing and returning an error_mark_node
20514 returns it (see template/typename17.C in C++11).
20515 ??? Could we diagnose this earlier? */
20516 else if (tag_type == typename_type && BASELINK_P (decl))
20518 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
20519 type = error_mark_node;
20521 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
20522 in effect, then we must assume that, upon instantiation, the
20523 template will correspond to a class. */
20524 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
20525 && tag_type == typename_type)
20526 type = make_typename_type (parser->scope, decl,
20527 typename_type,
20528 /*complain=*/tf_error);
20529 /* If the `typename' keyword is in effect and DECL is not a type
20530 decl, then type is non existent. */
20531 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
20533 else if (TREE_CODE (decl) == TYPE_DECL)
20535 type = check_elaborated_type_specifier (tag_type, decl,
20536 /*allow_template_p=*/true);
20538 /* If the next token is a semicolon, this must be a specialization,
20539 instantiation, or friend declaration. Check the scope while we
20540 still know whether or not we had a nested-name-specifier. */
20541 if (type != error_mark_node
20542 && !nested_name_specifier && !is_friend
20543 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20544 check_unqualified_spec_or_inst (type, token->location);
20546 else if (decl == error_mark_node)
20547 type = error_mark_node;
20550 if (!type)
20552 token = cp_lexer_peek_token (parser->lexer);
20553 identifier = cp_parser_identifier (parser);
20555 if (identifier == error_mark_node)
20557 parser->scope = NULL_TREE;
20558 return error_mark_node;
20561 /* For a `typename', we needn't call xref_tag. */
20562 if (tag_type == typename_type
20563 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
20564 return cp_parser_make_typename_type (parser, identifier,
20565 token->location);
20567 /* Template parameter lists apply only if we are not within a
20568 function parameter list. */
20569 bool template_parm_lists_apply
20570 = parser->num_template_parameter_lists;
20571 if (template_parm_lists_apply)
20572 for (cp_binding_level *s = current_binding_level;
20573 s && s->kind != sk_template_parms;
20574 s = s->level_chain)
20575 if (s->kind == sk_function_parms)
20576 template_parm_lists_apply = false;
20578 /* Look up a qualified name in the usual way. */
20579 if (parser->scope)
20581 tree decl;
20582 tree ambiguous_decls;
20584 decl = cp_parser_lookup_name (parser, identifier,
20585 tag_type,
20586 /*is_template=*/false,
20587 /*is_namespace=*/false,
20588 /*check_dependency=*/true,
20589 &ambiguous_decls,
20590 token->location);
20592 /* If the lookup was ambiguous, an error will already have been
20593 issued. */
20594 if (ambiguous_decls)
20595 return error_mark_node;
20597 /* If we are parsing friend declaration, DECL may be a
20598 TEMPLATE_DECL tree node here. However, we need to check
20599 whether this TEMPLATE_DECL results in valid code. Consider
20600 the following example:
20602 namespace N {
20603 template <class T> class C {};
20605 class X {
20606 template <class T> friend class N::C; // #1, valid code
20608 template <class T> class Y {
20609 friend class N::C; // #2, invalid code
20612 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
20613 name lookup of `N::C'. We see that friend declaration must
20614 be template for the code to be valid. Note that
20615 processing_template_decl does not work here since it is
20616 always 1 for the above two cases. */
20618 decl = (cp_parser_maybe_treat_template_as_class
20619 (decl, /*tag_name_p=*/is_friend
20620 && template_parm_lists_apply));
20622 if (TREE_CODE (decl) != TYPE_DECL)
20624 cp_parser_diagnose_invalid_type_name (parser,
20625 identifier,
20626 token->location);
20627 return error_mark_node;
20630 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
20632 bool allow_template = (template_parm_lists_apply
20633 || DECL_SELF_REFERENCE_P (decl));
20634 type = check_elaborated_type_specifier (tag_type, decl,
20635 allow_template);
20637 if (type == error_mark_node)
20638 return error_mark_node;
20641 /* Forward declarations of nested types, such as
20643 class C1::C2;
20644 class C1::C2::C3;
20646 are invalid unless all components preceding the final '::'
20647 are complete. If all enclosing types are complete, these
20648 declarations become merely pointless.
20650 Invalid forward declarations of nested types are errors
20651 caught elsewhere in parsing. Those that are pointless arrive
20652 here. */
20654 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20655 && !is_friend && is_declaration
20656 && !processing_explicit_instantiation)
20657 warning (0, "declaration %qD does not declare anything", decl);
20659 type = TREE_TYPE (decl);
20661 else
20663 /* An elaborated-type-specifier sometimes introduces a new type and
20664 sometimes names an existing type. Normally, the rule is that it
20665 introduces a new type only if there is not an existing type of
20666 the same name already in scope. For example, given:
20668 struct S {};
20669 void f() { struct S s; }
20671 the `struct S' in the body of `f' is the same `struct S' as in
20672 the global scope; the existing definition is used. However, if
20673 there were no global declaration, this would introduce a new
20674 local class named `S'.
20676 An exception to this rule applies to the following code:
20678 namespace N { struct S; }
20680 Here, the elaborated-type-specifier names a new type
20681 unconditionally; even if there is already an `S' in the
20682 containing scope this declaration names a new type.
20683 This exception only applies if the elaborated-type-specifier
20684 forms the complete declaration:
20686 [class.name]
20688 A declaration consisting solely of `class-key identifier ;' is
20689 either a redeclaration of the name in the current scope or a
20690 forward declaration of the identifier as a class name. It
20691 introduces the name into the current scope.
20693 We are in this situation precisely when the next token is a `;'.
20695 An exception to the exception is that a `friend' declaration does
20696 *not* name a new type; i.e., given:
20698 struct S { friend struct T; };
20700 `T' is not a new type in the scope of `S'.
20702 Also, `new struct S' or `sizeof (struct S)' never results in the
20703 definition of a new type; a new type can only be declared in a
20704 declaration context. */
20706 TAG_how how;
20708 if (is_friend)
20709 /* Friends have special name lookup rules. */
20710 how = TAG_how::HIDDEN_FRIEND;
20711 else if (is_declaration
20712 && cp_lexer_next_token_is (parser->lexer,
20713 CPP_SEMICOLON))
20714 /* This is a `class-key identifier ;' */
20715 how = TAG_how::CURRENT_ONLY;
20716 else
20717 how = TAG_how::GLOBAL;
20719 bool template_p =
20720 (template_parm_lists_apply
20721 && (cp_parser_next_token_starts_class_definition_p (parser)
20722 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
20723 /* An unqualified name was used to reference this type, so
20724 there were no qualifying templates. */
20725 if (template_parm_lists_apply
20726 && !cp_parser_check_template_parameters (parser,
20727 /*num_templates=*/0,
20728 /*template_id*/false,
20729 token->location,
20730 /*declarator=*/NULL))
20731 return error_mark_node;
20733 type = xref_tag (tag_type, identifier, how, template_p);
20737 if (type == error_mark_node)
20738 return error_mark_node;
20740 /* Allow attributes on forward declarations of classes. */
20741 if (attributes)
20743 if (TREE_CODE (type) == TYPENAME_TYPE)
20744 warning (OPT_Wattributes,
20745 "attributes ignored on uninstantiated type");
20746 else if (tag_type != enum_type
20747 && TREE_CODE (type) != BOUND_TEMPLATE_TEMPLATE_PARM
20748 && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
20749 && ! processing_explicit_instantiation)
20750 warning (OPT_Wattributes,
20751 "attributes ignored on template instantiation");
20752 else if (is_friend && cxx11_attribute_p (attributes))
20754 if (warning (OPT_Wattributes, "attribute ignored"))
20755 inform (input_location, "an attribute that appertains to a friend "
20756 "declaration that is not a definition is ignored");
20758 else if (is_declaration && cp_parser_declares_only_class_p (parser))
20759 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
20760 else
20761 warning (OPT_Wattributes,
20762 "attributes ignored on elaborated-type-specifier that is "
20763 "not a forward declaration");
20766 if (tag_type == enum_type)
20767 cp_parser_maybe_warn_enum_key (parser, key_loc, type, scoped_key);
20768 else
20770 /* Diagnose class/struct/union mismatches. IS_DECLARATION is false
20771 for alias definition. */
20772 bool decl_class = (is_declaration
20773 && cp_parser_declares_only_class_p (parser));
20774 cp_parser_check_class_key (parser, key_loc, tag_type, type, false,
20775 decl_class);
20777 /* Indicate whether this class was declared as a `class' or as a
20778 `struct'. */
20779 if (CLASS_TYPE_P (type) && !currently_open_class (type))
20780 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
20783 /* A "<" cannot follow an elaborated type specifier. If that
20784 happens, the user was probably trying to form a template-id. */
20785 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
20786 token->location);
20788 return type;
20791 /* Parse an enum-specifier.
20793 enum-specifier:
20794 enum-head { enumerator-list [opt] }
20795 enum-head { enumerator-list , } [C++0x]
20797 enum-head:
20798 enum-key identifier [opt] enum-base [opt]
20799 enum-key nested-name-specifier identifier enum-base [opt]
20801 enum-key:
20802 enum
20803 enum class [C++0x]
20804 enum struct [C++0x]
20806 enum-base: [C++0x]
20807 : type-specifier-seq
20809 opaque-enum-specifier:
20810 enum-key identifier enum-base [opt] ;
20812 GNU Extensions:
20813 enum-key attributes[opt] identifier [opt] enum-base [opt]
20814 { enumerator-list [opt] }attributes[opt]
20815 enum-key attributes[opt] identifier [opt] enum-base [opt]
20816 { enumerator-list, }attributes[opt] [C++0x]
20818 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
20819 if the token stream isn't an enum-specifier after all. */
20821 static tree
20822 cp_parser_enum_specifier (cp_parser* parser)
20824 tree identifier;
20825 tree type = NULL_TREE;
20826 tree prev_scope;
20827 tree nested_name_specifier = NULL_TREE;
20828 tree attributes;
20829 bool scoped_enum_p = false;
20830 bool has_underlying_type = false;
20831 bool nested_being_defined = false;
20832 bool new_value_list = false;
20833 bool is_new_type = false;
20834 bool is_unnamed = false;
20835 tree underlying_type = NULL_TREE;
20836 cp_token *type_start_token = NULL;
20837 auto cleanup = make_temp_override (parser->colon_corrects_to_scope_p, false);
20839 /* Parse tentatively so that we can back up if we don't find a
20840 enum-specifier. */
20841 cp_parser_parse_tentatively (parser);
20843 /* Caller guarantees that the current token is 'enum', an identifier
20844 possibly follows, and the token after that is an opening brace.
20845 If we don't have an identifier, fabricate an anonymous name for
20846 the enumeration being defined. */
20847 cp_lexer_consume_token (parser->lexer);
20849 /* Parse the "class" or "struct", which indicates a scoped
20850 enumeration type in C++0x. */
20851 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
20852 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
20854 if (cxx_dialect < cxx11)
20855 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
20857 /* Consume the `struct' or `class' token. */
20858 cp_lexer_consume_token (parser->lexer);
20860 scoped_enum_p = true;
20863 attributes = cp_parser_attributes_opt (parser);
20865 /* Clear the qualification. */
20866 parser->scope = NULL_TREE;
20867 parser->qualifying_scope = NULL_TREE;
20868 parser->object_scope = NULL_TREE;
20870 /* Figure out in what scope the declaration is being placed. */
20871 prev_scope = current_scope ();
20873 type_start_token = cp_lexer_peek_token (parser->lexer);
20875 push_deferring_access_checks (dk_no_check);
20876 nested_name_specifier
20877 = cp_parser_nested_name_specifier_opt (parser,
20878 /*typename_keyword_p=*/true,
20879 /*check_dependency_p=*/false,
20880 /*type_p=*/false,
20881 /*is_declaration=*/false);
20883 if (nested_name_specifier)
20885 tree name;
20887 identifier = cp_parser_identifier (parser);
20888 name = cp_parser_lookup_name (parser, identifier,
20889 enum_type,
20890 /*is_template=*/false,
20891 /*is_namespace=*/false,
20892 /*check_dependency=*/true,
20893 /*ambiguous_decls=*/NULL,
20894 input_location);
20895 if (name && name != error_mark_node)
20897 type = TREE_TYPE (name);
20898 if (TREE_CODE (type) == TYPENAME_TYPE)
20900 /* Are template enums allowed in ISO? */
20901 if (template_parm_scope_p ())
20902 pedwarn (type_start_token->location, OPT_Wpedantic,
20903 "%qD is an enumeration template", name);
20904 /* ignore a typename reference, for it will be solved by name
20905 in start_enum. */
20906 type = NULL_TREE;
20909 else if (nested_name_specifier == error_mark_node)
20910 /* We already issued an error. */;
20911 else
20913 error_at (type_start_token->location,
20914 "%qD does not name an enumeration in %qT",
20915 identifier, nested_name_specifier);
20916 nested_name_specifier = error_mark_node;
20919 else
20921 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20922 identifier = cp_parser_identifier (parser);
20923 else
20925 identifier = make_anon_name ();
20926 is_unnamed = true;
20927 if (scoped_enum_p)
20928 error_at (type_start_token->location,
20929 "unnamed scoped enum is not allowed");
20932 pop_deferring_access_checks ();
20934 /* Check for the `:' that denotes a specified underlying type in C++0x.
20935 Note that a ':' could also indicate a bitfield width, however. */
20936 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20938 cp_decl_specifier_seq type_specifiers;
20940 /* Consume the `:'. */
20941 cp_lexer_consume_token (parser->lexer);
20943 auto tdf
20944 = make_temp_override (parser->type_definition_forbidden_message,
20945 G_("types may not be defined in enum-base"));
20947 /* Parse the type-specifier-seq. */
20948 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
20949 /*is_declaration=*/false,
20950 /*is_trailing_return=*/false,
20951 &type_specifiers);
20953 /* At this point this is surely not elaborated type specifier. */
20954 if (!cp_parser_parse_definitely (parser))
20955 return NULL_TREE;
20957 if (cxx_dialect < cxx11)
20958 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
20960 has_underlying_type = true;
20962 /* If that didn't work, stop. */
20963 if (type_specifiers.type != error_mark_node)
20965 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
20966 /*initialized=*/0, NULL);
20967 if (underlying_type == error_mark_node
20968 || check_for_bare_parameter_packs (underlying_type))
20969 underlying_type = NULL_TREE;
20973 /* Look for the `{' but don't consume it yet. */
20974 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20976 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
20978 if (has_underlying_type)
20979 cp_parser_commit_to_tentative_parse (parser);
20980 cp_parser_error (parser, "expected %<{%>");
20981 if (has_underlying_type)
20982 return error_mark_node;
20984 /* An opaque-enum-specifier must have a ';' here. */
20985 if ((scoped_enum_p || underlying_type)
20986 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20988 if (has_underlying_type)
20989 cp_parser_commit_to_tentative_parse (parser);
20990 cp_parser_error (parser, "expected %<;%> or %<{%>");
20991 if (has_underlying_type)
20992 return error_mark_node;
20996 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
20997 return NULL_TREE;
20999 if (nested_name_specifier)
21001 if (CLASS_TYPE_P (nested_name_specifier))
21003 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
21004 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
21005 push_scope (nested_name_specifier);
21007 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
21008 push_nested_namespace (nested_name_specifier);
21011 /* Issue an error message if type-definitions are forbidden here. */
21012 if (!cp_parser_check_type_definition (parser))
21013 type = error_mark_node;
21014 else
21015 /* Create the new type. We do this before consuming the opening
21016 brace so the enum will be recorded as being on the line of its
21017 tag (or the 'enum' keyword, if there is no tag). */
21018 type = start_enum (identifier, type, underlying_type,
21019 attributes, scoped_enum_p, &is_new_type);
21021 /* If the next token is not '{' it is an opaque-enum-specifier or an
21022 elaborated-type-specifier. */
21023 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21025 auto_timevar tv (TV_PARSE_ENUM);
21027 if (nested_name_specifier
21028 && nested_name_specifier != error_mark_node)
21030 /* The following catches invalid code such as:
21031 enum class S<int>::E { A, B, C }; */
21032 if (!processing_specialization
21033 && CLASS_TYPE_P (nested_name_specifier)
21034 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
21035 error_at (type_start_token->location, "cannot add an enumerator "
21036 "list to a template instantiation");
21038 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
21040 error_at (type_start_token->location,
21041 "%<%T::%E%> has not been declared",
21042 TYPE_CONTEXT (nested_name_specifier),
21043 nested_name_specifier);
21044 type = error_mark_node;
21046 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
21047 && !CLASS_TYPE_P (nested_name_specifier))
21049 error_at (type_start_token->location, "nested name specifier "
21050 "%qT for enum declaration does not name a class "
21051 "or namespace", nested_name_specifier);
21052 type = error_mark_node;
21054 /* If that scope does not contain the scope in which the
21055 class was originally declared, the program is invalid. */
21056 else if (prev_scope && !is_ancestor (prev_scope,
21057 nested_name_specifier))
21059 if (at_namespace_scope_p ())
21060 error_at (type_start_token->location,
21061 "declaration of %qD in namespace %qD which does not "
21062 "enclose %qD",
21063 type, prev_scope, nested_name_specifier);
21064 else
21065 error_at (type_start_token->location,
21066 "declaration of %qD in %qD which does not "
21067 "enclose %qD",
21068 type, prev_scope, nested_name_specifier);
21069 type = error_mark_node;
21071 /* If that scope is the scope where the declaration is being placed
21072 the program is invalid. */
21073 else if (CLASS_TYPE_P (nested_name_specifier)
21074 && CLASS_TYPE_P (prev_scope)
21075 && same_type_p (nested_name_specifier, prev_scope))
21077 permerror (type_start_token->location,
21078 "extra qualification not allowed");
21079 nested_name_specifier = NULL_TREE;
21083 if (scoped_enum_p)
21084 begin_scope (sk_scoped_enum, type);
21086 /* Consume the opening brace. */
21087 matching_braces braces;
21088 braces.consume_open (parser);
21090 if (type == error_mark_node)
21091 ; /* Nothing to add */
21092 else if (OPAQUE_ENUM_P (type)
21093 || (cxx_dialect > cxx98 && processing_specialization))
21095 new_value_list = true;
21096 SET_OPAQUE_ENUM_P (type, false);
21097 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
21099 else
21101 error_at (type_start_token->location,
21102 "multiple definition of %q#T", type);
21103 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
21104 "previous definition here");
21105 type = error_mark_node;
21108 if (type == error_mark_node)
21109 cp_parser_skip_to_end_of_block_or_statement (parser);
21110 /* If the next token is not '}', then there are some enumerators. */
21111 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21113 if (is_unnamed && !scoped_enum_p
21114 /* Don't warn for enum {} a; here. */
21115 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SEMICOLON))
21116 pedwarn (type_start_token->location, OPT_Wpedantic,
21117 "ISO C++ forbids empty unnamed enum");
21119 else
21121 /* We've seen a '{' so we know we're in an enum-specifier.
21122 Commit to any tentative parse to get syntax errors. */
21123 cp_parser_commit_to_tentative_parse (parser);
21124 cp_parser_enumerator_list (parser, type);
21127 /* Consume the final '}'. */
21128 braces.require_close (parser);
21130 if (scoped_enum_p)
21131 finish_scope ();
21133 else
21135 /* If a ';' follows, then it is an opaque-enum-specifier
21136 and additional restrictions apply. */
21137 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21139 if (is_unnamed)
21140 error_at (type_start_token->location,
21141 "opaque-enum-specifier without name");
21142 else if (nested_name_specifier)
21143 error_at (type_start_token->location,
21144 "opaque-enum-specifier must use a simple identifier");
21148 /* Look for trailing attributes to apply to this enumeration, and
21149 apply them if appropriate. */
21150 if (cp_parser_allow_gnu_extensions_p (parser))
21152 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
21153 cplus_decl_attributes (&type,
21154 trailing_attr,
21155 (int) ATTR_FLAG_TYPE_IN_PLACE);
21158 /* Finish up the enumeration. */
21159 if (type != error_mark_node)
21161 if (new_value_list)
21162 finish_enum_value_list (type);
21163 if (is_new_type)
21164 finish_enum (type);
21167 if (nested_name_specifier)
21169 if (CLASS_TYPE_P (nested_name_specifier))
21171 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
21172 pop_scope (nested_name_specifier);
21174 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
21175 pop_nested_namespace (nested_name_specifier);
21177 return type;
21180 /* Parse an enumerator-list. The enumerators all have the indicated
21181 TYPE.
21183 enumerator-list:
21184 enumerator-definition
21185 enumerator-list , enumerator-definition */
21187 static void
21188 cp_parser_enumerator_list (cp_parser* parser, tree type)
21190 while (true)
21192 /* Parse an enumerator-definition. */
21193 cp_parser_enumerator_definition (parser, type);
21195 /* If the next token is not a ',', we've reached the end of
21196 the list. */
21197 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21198 break;
21199 /* Otherwise, consume the `,' and keep going. */
21200 cp_lexer_consume_token (parser->lexer);
21201 /* If the next token is a `}', there is a trailing comma. */
21202 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21204 if (cxx_dialect < cxx11)
21205 pedwarn (input_location, OPT_Wpedantic,
21206 "comma at end of enumerator list");
21207 break;
21212 /* Parse an enumerator-definition. The enumerator has the indicated
21213 TYPE.
21215 enumerator-definition:
21216 enumerator
21217 enumerator = constant-expression
21219 enumerator:
21220 identifier
21222 GNU Extensions:
21224 enumerator-definition:
21225 enumerator attributes [opt]
21226 enumerator attributes [opt] = constant-expression */
21228 static void
21229 cp_parser_enumerator_definition (cp_parser* parser, tree type)
21231 tree identifier;
21232 tree value;
21233 location_t loc;
21235 /* Save the input location because we are interested in the location
21236 of the identifier and not the location of the explicit value. */
21237 loc = cp_lexer_peek_token (parser->lexer)->location;
21239 /* Look for the identifier. */
21240 identifier = cp_parser_identifier (parser);
21241 if (identifier == error_mark_node)
21242 return;
21244 /* Parse any specified attributes. */
21245 tree attrs = cp_parser_attributes_opt (parser);
21247 /* If the next token is an '=', then there is an explicit value. */
21248 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21250 /* Consume the `=' token. */
21251 cp_lexer_consume_token (parser->lexer);
21252 /* Parse the value. */
21253 value = cp_parser_constant_expression (parser);
21255 else
21256 value = NULL_TREE;
21258 /* If we are processing a template, make sure the initializer of the
21259 enumerator doesn't contain any bare template parameter pack. */
21260 if (current_lambda_expr ())
21262 /* In a lambda it should work, but doesn't currently. */
21263 if (uses_parameter_packs (value))
21265 sorry ("unexpanded parameter pack in enumerator in lambda");
21266 value = error_mark_node;
21269 else if (check_for_bare_parameter_packs (value))
21270 value = error_mark_node;
21272 /* Create the enumerator. */
21273 build_enumerator (identifier, value, type, attrs, loc);
21276 /* Parse a namespace-name.
21278 namespace-name:
21279 original-namespace-name
21280 namespace-alias
21282 Returns the NAMESPACE_DECL for the namespace. */
21284 static tree
21285 cp_parser_namespace_name (cp_parser* parser)
21287 tree identifier;
21288 tree namespace_decl;
21290 cp_token *token = cp_lexer_peek_token (parser->lexer);
21292 /* Get the name of the namespace. */
21293 identifier = cp_parser_identifier (parser);
21294 if (identifier == error_mark_node)
21295 return error_mark_node;
21297 /* Look up the identifier in the currently active scope. Look only
21298 for namespaces, due to:
21300 [basic.lookup.udir]
21302 When looking up a namespace-name in a using-directive or alias
21303 definition, only namespace names are considered.
21305 And:
21307 [basic.lookup.qual]
21309 During the lookup of a name preceding the :: scope resolution
21310 operator, object, function, and enumerator names are ignored.
21312 (Note that cp_parser_qualifying_entity only calls this
21313 function if the token after the name is the scope resolution
21314 operator.) */
21315 namespace_decl = cp_parser_lookup_name (parser, identifier,
21316 none_type,
21317 /*is_template=*/false,
21318 /*is_namespace=*/true,
21319 /*check_dependency=*/true,
21320 /*ambiguous_decls=*/NULL,
21321 token->location);
21322 /* If it's not a namespace, issue an error. */
21323 if (namespace_decl == error_mark_node
21324 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
21326 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21328 auto_diagnostic_group d;
21329 name_hint hint;
21330 if (namespace_decl == error_mark_node
21331 && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
21332 hint = suggest_alternative_in_explicit_scope (token->location,
21333 identifier,
21334 parser->scope);
21335 if (const char *suggestion = hint.suggestion ())
21337 gcc_rich_location richloc (token->location);
21338 richloc.add_fixit_replace (suggestion);
21339 error_at (&richloc,
21340 "%qD is not a namespace-name; did you mean %qs?",
21341 identifier, suggestion);
21343 else
21344 error_at (token->location, "%qD is not a namespace-name",
21345 identifier);
21347 else
21348 cp_parser_error (parser, "expected namespace-name");
21349 namespace_decl = error_mark_node;
21352 return namespace_decl;
21355 /* Parse a namespace-definition.
21357 namespace-definition:
21358 named-namespace-definition
21359 unnamed-namespace-definition
21361 named-namespace-definition:
21362 original-namespace-definition
21363 extension-namespace-definition
21365 original-namespace-definition:
21366 namespace identifier { namespace-body }
21368 extension-namespace-definition:
21369 namespace original-namespace-name { namespace-body }
21371 unnamed-namespace-definition:
21372 namespace { namespace-body } */
21374 static void
21375 cp_parser_namespace_definition (cp_parser* parser)
21377 tree identifier;
21378 int nested_definition_count = 0;
21380 cp_ensure_no_omp_declare_simd (parser);
21381 cp_ensure_no_oacc_routine (parser);
21383 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
21384 const bool topmost_inline_p = is_inline;
21386 if (is_inline)
21388 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
21389 cp_lexer_consume_token (parser->lexer);
21392 /* Look for the `namespace' keyword. */
21393 cp_token* token
21394 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
21396 /* Parse any specified attributes before the identifier. */
21397 tree attribs = cp_parser_attributes_opt (parser);
21399 for (;;)
21401 identifier = NULL_TREE;
21403 bool nested_inline_p = cp_lexer_next_token_is_keyword (parser->lexer,
21404 RID_INLINE);
21405 if (nested_inline_p && nested_definition_count != 0)
21407 if (pedantic && cxx_dialect < cxx20)
21408 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
21409 OPT_Wc__20_extensions, "nested inline namespace "
21410 "definitions only available with %<-std=c++20%> or "
21411 "%<-std=gnu++20%>");
21412 cp_lexer_consume_token (parser->lexer);
21415 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21417 identifier = cp_parser_identifier (parser);
21419 if (cp_next_tokens_can_be_std_attribute_p (parser))
21420 pedwarn (input_location, OPT_Wpedantic,
21421 "standard attributes on namespaces must precede "
21422 "the namespace name");
21424 /* Parse any attributes specified after the identifier. */
21425 attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
21428 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
21430 /* Don't forget that the innermost namespace might have been
21431 marked as inline. Use |= because we cannot overwrite
21432 IS_INLINE in case the outermost namespace is inline, but
21433 there are no nested inlines. */
21434 is_inline |= nested_inline_p;
21435 break;
21438 if (!nested_definition_count && pedantic && cxx_dialect < cxx17)
21439 pedwarn (input_location, OPT_Wc__17_extensions,
21440 "nested namespace definitions only available with "
21441 "%<-std=c++17%> or %<-std=gnu++17%>");
21443 /* Nested namespace names can create new namespaces (unlike
21444 other qualified-ids). */
21445 if (int count = (identifier
21446 ? push_namespace (identifier, nested_inline_p)
21447 : 0))
21448 nested_definition_count += count;
21449 else
21450 cp_parser_error (parser, "nested namespace name required");
21451 cp_lexer_consume_token (parser->lexer);
21454 if (nested_definition_count && !identifier)
21455 cp_parser_error (parser, "namespace name required");
21457 if (nested_definition_count && attribs)
21458 error_at (token->location,
21459 "a nested namespace definition cannot have attributes");
21460 if (nested_definition_count && topmost_inline_p)
21461 error_at (token->location,
21462 "a nested namespace definition cannot be inline");
21464 /* Start the namespace. */
21465 nested_definition_count += push_namespace (identifier, is_inline);
21467 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
21469 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
21471 /* Look for the `{' to validate starting the namespace. */
21472 matching_braces braces;
21473 if (braces.require_open (parser))
21475 /* Parse the body of the namespace. */
21476 cp_parser_namespace_body (parser);
21478 /* Look for the final `}'. */
21479 braces.require_close (parser);
21482 if (has_visibility)
21483 pop_visibility (1);
21485 /* Pop the nested namespace definitions. */
21486 while (nested_definition_count--)
21487 pop_namespace ();
21490 /* Parse a namespace-body.
21492 namespace-body:
21493 declaration-seq [opt] */
21495 static void
21496 cp_parser_namespace_body (cp_parser* parser)
21498 cp_parser_declaration_seq_opt (parser);
21501 /* Parse a namespace-alias-definition.
21503 namespace-alias-definition:
21504 namespace identifier = qualified-namespace-specifier ; */
21506 static void
21507 cp_parser_namespace_alias_definition (cp_parser* parser)
21509 tree identifier;
21510 tree namespace_specifier;
21512 cp_token *token = cp_lexer_peek_token (parser->lexer);
21514 /* Look for the `namespace' keyword. */
21515 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
21516 /* Look for the identifier. */
21517 identifier = cp_parser_identifier (parser);
21518 if (identifier == error_mark_node)
21519 return;
21520 /* Look for the `=' token. */
21521 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
21522 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21524 error_at (token->location, "%<namespace%> definition is not allowed here");
21525 /* Skip the definition. */
21526 cp_lexer_consume_token (parser->lexer);
21527 if (cp_parser_skip_to_closing_brace (parser))
21528 cp_lexer_consume_token (parser->lexer);
21529 return;
21531 cp_parser_require (parser, CPP_EQ, RT_EQ);
21532 /* Look for the qualified-namespace-specifier. */
21533 namespace_specifier
21534 = cp_parser_qualified_namespace_specifier (parser);
21535 cp_warn_deprecated_use_scopes (namespace_specifier);
21536 /* Look for the `;' token. */
21537 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21539 /* Register the alias in the symbol table. */
21540 do_namespace_alias (identifier, namespace_specifier);
21543 /* Parse a qualified-namespace-specifier.
21545 qualified-namespace-specifier:
21546 :: [opt] nested-name-specifier [opt] namespace-name
21548 Returns a NAMESPACE_DECL corresponding to the specified
21549 namespace. */
21551 static tree
21552 cp_parser_qualified_namespace_specifier (cp_parser* parser)
21554 /* Look for the optional `::'. */
21555 cp_parser_global_scope_opt (parser,
21556 /*current_scope_valid_p=*/false);
21558 /* Look for the optional nested-name-specifier. */
21559 cp_parser_nested_name_specifier_opt (parser,
21560 /*typename_keyword_p=*/false,
21561 /*check_dependency_p=*/true,
21562 /*type_p=*/false,
21563 /*is_declaration=*/true);
21565 return cp_parser_namespace_name (parser);
21568 /* Subroutine of cp_parser_using_declaration. */
21570 static tree
21571 finish_using_decl (tree qscope, tree identifier, bool typename_p = false)
21573 tree decl = NULL_TREE;
21574 if (at_class_scope_p ())
21576 /* Create the USING_DECL. */
21577 decl = do_class_using_decl (qscope, identifier);
21579 if (check_for_bare_parameter_packs (decl))
21580 return error_mark_node;
21582 if (decl && typename_p)
21583 USING_DECL_TYPENAME_P (decl) = 1;
21585 /* Add it to the list of members in this class. */
21586 finish_member_declaration (decl);
21588 else
21589 finish_nonmember_using_decl (qscope, identifier);
21590 return decl;
21593 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
21594 access declaration.
21596 using-declaration:
21597 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
21598 using :: unqualified-id ;
21600 access-declaration:
21601 qualified-id ;
21605 static bool
21606 cp_parser_using_declaration (cp_parser* parser,
21607 bool access_declaration_p)
21609 cp_token *token;
21610 bool typename_p = false;
21611 bool global_scope_p;
21612 tree identifier;
21613 tree qscope;
21614 int oldcount = errorcount;
21615 cp_token *diag_token = NULL;
21617 if (access_declaration_p)
21619 diag_token = cp_lexer_peek_token (parser->lexer);
21620 cp_parser_parse_tentatively (parser);
21622 else
21624 /* Look for the `using' keyword. */
21625 cp_parser_require_keyword (parser, RID_USING, RT_USING);
21627 again:
21628 /* Peek at the next token. */
21629 token = cp_lexer_peek_token (parser->lexer);
21630 /* See if it's `typename'. */
21631 if (token->keyword == RID_TYPENAME)
21633 /* Remember that we've seen it. */
21634 typename_p = true;
21635 /* Consume the `typename' token. */
21636 cp_lexer_consume_token (parser->lexer);
21640 /* Look for the optional global scope qualification. */
21641 global_scope_p
21642 = (cp_parser_global_scope_opt (parser,
21643 /*current_scope_valid_p=*/false)
21644 != NULL_TREE);
21646 /* If we saw `typename', or didn't see `::', then there must be a
21647 nested-name-specifier present. */
21648 if (typename_p || !global_scope_p)
21650 qscope = cp_parser_nested_name_specifier (parser, typename_p,
21651 /*check_dependency_p=*/true,
21652 /*type_p=*/false,
21653 /*is_declaration=*/true);
21654 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
21656 cp_parser_skip_to_end_of_block_or_statement (parser);
21657 return false;
21660 /* Otherwise, we could be in either of the two productions. In that
21661 case, treat the nested-name-specifier as optional. */
21662 else
21663 qscope = cp_parser_nested_name_specifier_opt (parser,
21664 /*typename_keyword_p=*/false,
21665 /*check_dependency_p=*/true,
21666 /*type_p=*/false,
21667 /*is_declaration=*/true);
21668 if (!qscope)
21669 qscope = global_namespace;
21671 cp_warn_deprecated_use_scopes (qscope);
21673 if (access_declaration_p && cp_parser_error_occurred (parser))
21674 /* Something has already gone wrong; there's no need to parse
21675 further. Since an error has occurred, the return value of
21676 cp_parser_parse_definitely will be false, as required. */
21677 return cp_parser_parse_definitely (parser);
21679 token = cp_lexer_peek_token (parser->lexer);
21680 /* Parse the unqualified-id. */
21681 identifier = cp_parser_unqualified_id (parser,
21682 /*template_keyword_p=*/false,
21683 /*check_dependency_p=*/true,
21684 /*declarator_p=*/true,
21685 /*optional_p=*/false);
21687 if (access_declaration_p)
21689 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21690 cp_parser_simulate_error (parser);
21691 if (!cp_parser_parse_definitely (parser))
21692 return false;
21694 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21696 cp_token *ell = cp_lexer_consume_token (parser->lexer);
21697 if (cxx_dialect < cxx17)
21698 pedwarn (ell->location, OPT_Wc__17_extensions,
21699 "pack expansion in using-declaration only available "
21700 "with %<-std=c++17%> or %<-std=gnu++17%>");
21701 qscope = make_pack_expansion (qscope);
21704 /* The function we call to handle a using-declaration is different
21705 depending on what scope we are in. */
21706 if (qscope == error_mark_node || identifier == error_mark_node)
21708 else if (!identifier_p (identifier)
21709 && TREE_CODE (identifier) != BIT_NOT_EXPR)
21710 /* [namespace.udecl]
21712 A using declaration shall not name a template-id. */
21713 error_at (token->location,
21714 "a template-id may not appear in a using-declaration");
21715 else
21717 tree decl = finish_using_decl (qscope, identifier, typename_p);
21719 if (decl == error_mark_node)
21721 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21722 return false;
21726 if (!access_declaration_p
21727 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21729 cp_token *comma = cp_lexer_consume_token (parser->lexer);
21730 if (cxx_dialect < cxx17)
21731 pedwarn (comma->location, OPT_Wc__17_extensions,
21732 "comma-separated list in using-declaration only available "
21733 "with %<-std=c++17%> or %<-std=gnu++17%>");
21734 goto again;
21737 /* Look for the final `;'. */
21738 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21740 if (access_declaration_p && errorcount == oldcount)
21741 warning_at (diag_token->location, OPT_Wdeprecated,
21742 "access declarations are deprecated "
21743 "in favour of using-declarations; "
21744 "suggestion: add the %<using%> keyword");
21746 return true;
21749 /* C++20 using enum declaration.
21751 using-enum-declaration :
21752 using elaborated-enum-specifier ; */
21754 static void
21755 cp_parser_using_enum (cp_parser *parser)
21757 cp_parser_require_keyword (parser, RID_USING, RT_USING);
21759 /* Using cp_parser_elaborated_type_specifier rejects typedef-names, which
21760 breaks one of the motivating examples in using-enum-5.C.
21761 cp_parser_simple_type_specifier seems to be closer to what we actually
21762 want, though that hasn't been properly specified yet. */
21764 /* Consume 'enum'. */
21765 gcc_checking_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM));
21766 cp_lexer_consume_token (parser->lexer);
21768 cp_token *start = cp_lexer_peek_token (parser->lexer);
21770 tree type = (cp_parser_simple_type_specifier
21771 (parser, NULL, CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
21773 cp_token *end = cp_lexer_previous_token (parser->lexer);
21775 if (type == error_mark_node
21776 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
21778 cp_parser_skip_to_end_of_block_or_statement (parser);
21779 return;
21781 if (TREE_CODE (type) == TYPE_DECL)
21782 type = TREE_TYPE (type);
21784 /* The elaborated-enum-specifier shall not name a dependent type and the type
21785 shall have a reachable enum-specifier. */
21786 const char *msg = nullptr;
21787 if (cxx_dialect < cxx20)
21788 msg = _("%<using enum%> "
21789 "only available with %<-std=c++20%> or %<-std=gnu++20%>");
21790 else if (dependent_type_p (type))
21791 msg = _("%<using enum%> of dependent type %qT");
21792 else if (TREE_CODE (type) != ENUMERAL_TYPE)
21793 msg = _("%<using enum%> of non-enumeration type %q#T");
21794 else if (!COMPLETE_TYPE_P (type))
21795 msg = _("%<using enum%> of incomplete type %qT");
21796 else if (OPAQUE_ENUM_P (type))
21797 msg = _("%<using enum%> of %qT before its enum-specifier");
21798 if (msg)
21800 location_t loc = make_location (start, start, end);
21801 auto_diagnostic_group g;
21802 error_at (loc, msg, type);
21803 loc = location_of (type);
21804 if (cxx_dialect < cxx20 || loc == input_location)
21806 else if (OPAQUE_ENUM_P (type))
21807 inform (loc, "opaque-enum-declaration here");
21808 else
21809 inform (loc, "declared here");
21812 /* A using-enum-declaration introduces the enumerator names of the named
21813 enumeration as if by a using-declaration for each enumerator. */
21814 if (TREE_CODE (type) == ENUMERAL_TYPE)
21815 for (tree v = TYPE_VALUES (type); v; v = TREE_CHAIN (v))
21816 finish_using_decl (type, DECL_NAME (TREE_VALUE (v)));
21819 /* Parse an alias-declaration.
21821 alias-declaration:
21822 using identifier attribute-specifier-seq [opt] = type-id */
21824 static tree
21825 cp_parser_alias_declaration (cp_parser* parser)
21827 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
21828 location_t id_location, type_location;
21829 cp_declarator *declarator;
21830 cp_decl_specifier_seq decl_specs;
21831 bool member_p;
21832 const char *saved_message = NULL;
21834 /* Look for the `using' keyword. */
21835 cp_token *using_token
21836 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
21837 if (using_token == NULL)
21838 return error_mark_node;
21840 id_location = cp_lexer_peek_token (parser->lexer)->location;
21841 id = cp_parser_identifier (parser);
21842 if (id == error_mark_node)
21843 return error_mark_node;
21845 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
21846 attributes = cp_parser_attributes_opt (parser);
21847 if (attributes == error_mark_node)
21848 return error_mark_node;
21850 cp_parser_require (parser, CPP_EQ, RT_EQ);
21852 if (cp_parser_error_occurred (parser))
21853 return error_mark_node;
21855 cp_parser_commit_to_tentative_parse (parser);
21857 /* Now we are going to parse the type-id of the declaration. */
21860 [dcl.type]/3 says:
21862 "A type-specifier-seq shall not define a class or enumeration
21863 unless it appears in the type-id of an alias-declaration (7.1.3) that
21864 is not the declaration of a template-declaration."
21866 In other words, if we currently are in an alias template, the
21867 type-id should not define a type.
21869 So let's set parser->type_definition_forbidden_message in that
21870 case; cp_parser_check_type_definition (called by
21871 cp_parser_class_specifier) will then emit an error if a type is
21872 defined in the type-id. */
21873 if (parser->num_template_parameter_lists)
21875 saved_message = parser->type_definition_forbidden_message;
21876 parser->type_definition_forbidden_message =
21877 G_("types may not be defined in alias template declarations");
21880 type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
21881 &type_location);
21883 /* Restore the error message if need be. */
21884 if (parser->num_template_parameter_lists)
21885 parser->type_definition_forbidden_message = saved_message;
21887 if (type == error_mark_node
21888 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
21890 cp_parser_skip_to_end_of_block_or_statement (parser);
21891 return error_mark_node;
21894 /* A typedef-name can also be introduced by an alias-declaration. The
21895 identifier following the using keyword becomes a typedef-name. It has
21896 the same semantics as if it were introduced by the typedef
21897 specifier. In particular, it does not define a new type and it shall
21898 not appear in the type-id. */
21900 clear_decl_specs (&decl_specs);
21901 decl_specs.type = type;
21902 if (attributes != NULL_TREE)
21904 decl_specs.attributes = attributes;
21905 set_and_check_decl_spec_loc (&decl_specs,
21906 ds_attribute,
21907 attrs_token);
21909 set_and_check_decl_spec_loc (&decl_specs,
21910 ds_typedef,
21911 using_token);
21912 set_and_check_decl_spec_loc (&decl_specs,
21913 ds_alias,
21914 using_token);
21915 decl_specs.locations[ds_type_spec] = type_location;
21917 if (parser->num_template_parameter_lists
21918 && !cp_parser_check_template_parameters (parser,
21919 /*num_templates=*/0,
21920 /*template_id*/false,
21921 id_location,
21922 /*declarator=*/NULL))
21923 return error_mark_node;
21925 declarator = make_id_declarator (NULL_TREE, id, sfk_none, id_location);
21927 member_p = at_class_scope_p ();
21928 if (member_p)
21929 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
21930 NULL_TREE, attributes);
21931 else
21932 decl = start_decl (declarator, &decl_specs, 0,
21933 attributes, NULL_TREE, &pushed_scope);
21934 if (decl == error_mark_node)
21935 return decl;
21937 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
21939 if (pushed_scope)
21940 pop_scope (pushed_scope);
21942 /* If decl is a template, return its TEMPLATE_DECL so that it gets
21943 added into the symbol table; otherwise, return the TYPE_DECL. */
21944 if (DECL_LANG_SPECIFIC (decl)
21945 && DECL_TEMPLATE_INFO (decl)
21946 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
21948 decl = DECL_TI_TEMPLATE (decl);
21949 if (member_p)
21950 check_member_template (decl);
21953 return decl;
21956 /* Parse a using-directive.
21958 using-directive:
21959 attribute-specifier-seq [opt] using namespace :: [opt]
21960 nested-name-specifier [opt] namespace-name ; */
21962 static void
21963 cp_parser_using_directive (cp_parser* parser)
21965 tree namespace_decl;
21966 tree attribs = cp_parser_std_attribute_spec_seq (parser);
21967 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21969 /* Error during attribute parsing that resulted in skipping
21970 to next semicolon. */
21971 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21972 return;
21975 /* Look for the `using' keyword. */
21976 cp_parser_require_keyword (parser, RID_USING, RT_USING);
21977 /* And the `namespace' keyword. */
21978 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
21979 /* Look for the optional `::' operator. */
21980 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
21981 /* And the optional nested-name-specifier. */
21982 cp_parser_nested_name_specifier_opt (parser,
21983 /*typename_keyword_p=*/false,
21984 /*check_dependency_p=*/true,
21985 /*type_p=*/false,
21986 /*is_declaration=*/true);
21987 /* Get the namespace being used. */
21988 namespace_decl = cp_parser_namespace_name (parser);
21989 cp_warn_deprecated_use_scopes (namespace_decl);
21990 /* And any specified GNU attributes. */
21991 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
21992 attribs = chainon (attribs, cp_parser_gnu_attributes_opt (parser));
21994 /* Update the symbol table. */
21995 finish_using_directive (namespace_decl, attribs);
21997 /* Look for the final `;'. */
21998 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22001 /* Parse an asm-definition.
22003 asm-qualifier:
22004 volatile
22005 inline
22006 goto
22008 asm-qualifier-list:
22009 asm-qualifier
22010 asm-qualifier-list asm-qualifier
22012 asm-definition:
22013 asm ( string-literal ) ;
22015 GNU Extension:
22017 asm-definition:
22018 asm asm-qualifier-list [opt] ( string-literal ) ;
22019 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt] ) ;
22020 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
22021 : asm-operand-list [opt] ) ;
22022 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
22023 : asm-operand-list [opt]
22024 : asm-clobber-list [opt] ) ;
22025 asm asm-qualifier-list [opt] ( string-literal : : asm-operand-list [opt]
22026 : asm-clobber-list [opt]
22027 : asm-goto-list ) ;
22029 The form with asm-goto-list is valid if and only if the asm-qualifier-list
22030 contains goto, and is the only allowed form in that case. No duplicates are
22031 allowed in an asm-qualifier-list. */
22033 static void
22034 cp_parser_asm_definition (cp_parser* parser)
22036 tree string;
22037 tree outputs = NULL_TREE;
22038 tree inputs = NULL_TREE;
22039 tree clobbers = NULL_TREE;
22040 tree labels = NULL_TREE;
22041 tree asm_stmt;
22042 bool extended_p = false;
22043 bool invalid_inputs_p = false;
22044 bool invalid_outputs_p = false;
22045 required_token missing = RT_NONE;
22046 location_t asm_loc = cp_lexer_peek_token (parser->lexer)->location;
22048 /* Look for the `asm' keyword. */
22049 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
22051 /* In C++20, unevaluated inline assembly is permitted in constexpr
22052 functions. */
22053 if (parser->in_function_body
22054 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
22055 && cxx_dialect < cxx20)
22056 pedwarn (asm_loc, OPT_Wc__20_extensions, "%<asm%> in %<constexpr%> "
22057 "function only available with %<-std=c++20%> or "
22058 "%<-std=gnu++20%>");
22060 /* Handle the asm-qualifier-list. */
22061 location_t volatile_loc = UNKNOWN_LOCATION;
22062 location_t inline_loc = UNKNOWN_LOCATION;
22063 location_t goto_loc = UNKNOWN_LOCATION;
22064 location_t first_loc = UNKNOWN_LOCATION;
22066 if (cp_parser_allow_gnu_extensions_p (parser))
22067 for (;;)
22069 cp_token *token = cp_lexer_peek_token (parser->lexer);
22070 location_t loc = token->location;
22071 switch (cp_lexer_peek_token (parser->lexer)->keyword)
22073 case RID_VOLATILE:
22074 if (volatile_loc)
22076 error_at (loc, "duplicate %<asm%> qualifier %qT",
22077 token->u.value);
22078 inform (volatile_loc, "first seen here");
22080 else
22082 if (!parser->in_function_body)
22083 warning_at (loc, 0, "%<asm%> qualifier %qT ignored "
22084 "outside of function body", token->u.value);
22085 volatile_loc = loc;
22087 cp_lexer_consume_token (parser->lexer);
22088 continue;
22090 case RID_INLINE:
22091 if (inline_loc)
22093 error_at (loc, "duplicate %<asm%> qualifier %qT",
22094 token->u.value);
22095 inform (inline_loc, "first seen here");
22097 else
22098 inline_loc = loc;
22099 if (!first_loc)
22100 first_loc = loc;
22101 cp_lexer_consume_token (parser->lexer);
22102 continue;
22104 case RID_GOTO:
22105 if (goto_loc)
22107 error_at (loc, "duplicate %<asm%> qualifier %qT",
22108 token->u.value);
22109 inform (goto_loc, "first seen here");
22111 else
22112 goto_loc = loc;
22113 if (!first_loc)
22114 first_loc = loc;
22115 cp_lexer_consume_token (parser->lexer);
22116 continue;
22118 case RID_CONST:
22119 case RID_RESTRICT:
22120 error_at (loc, "%qT is not an %<asm%> qualifier", token->u.value);
22121 cp_lexer_consume_token (parser->lexer);
22122 continue;
22124 default:
22125 break;
22127 break;
22130 bool volatile_p = (volatile_loc != UNKNOWN_LOCATION);
22131 bool inline_p = (inline_loc != UNKNOWN_LOCATION);
22132 bool goto_p = (goto_loc != UNKNOWN_LOCATION);
22134 if (!parser->in_function_body && (inline_p || goto_p))
22136 error_at (first_loc, "%<asm%> qualifier outside of function body");
22137 inline_p = goto_p = false;
22140 /* Look for the opening `('. */
22141 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22142 return;
22143 /* Look for the string. */
22144 string = cp_parser_string_literal (parser, false, false);
22145 if (string == error_mark_node)
22147 cp_parser_skip_to_closing_parenthesis (parser, true, false,
22148 /*consume_paren=*/true);
22149 return;
22152 /* If we're allowing GNU extensions, check for the extended assembly
22153 syntax. Unfortunately, the `:' tokens need not be separated by
22154 a space in C, and so, for compatibility, we tolerate that here
22155 too. Doing that means that we have to treat the `::' operator as
22156 two `:' tokens. */
22157 if (cp_parser_allow_gnu_extensions_p (parser)
22158 && parser->in_function_body
22159 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
22160 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
22162 bool inputs_p = false;
22163 bool clobbers_p = false;
22164 bool labels_p = false;
22166 /* The extended syntax was used. */
22167 extended_p = true;
22169 /* Look for outputs. */
22170 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22172 /* Consume the `:'. */
22173 cp_lexer_consume_token (parser->lexer);
22174 /* Parse the output-operands. */
22175 if (cp_lexer_next_token_is_not (parser->lexer,
22176 CPP_COLON)
22177 && cp_lexer_next_token_is_not (parser->lexer,
22178 CPP_SCOPE)
22179 && cp_lexer_next_token_is_not (parser->lexer,
22180 CPP_CLOSE_PAREN))
22182 outputs = cp_parser_asm_operand_list (parser);
22183 if (outputs == error_mark_node)
22184 invalid_outputs_p = true;
22187 /* If the next token is `::', there are no outputs, and the
22188 next token is the beginning of the inputs. */
22189 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22190 /* The inputs are coming next. */
22191 inputs_p = true;
22193 /* Look for inputs. */
22194 if (inputs_p
22195 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22197 /* Consume the `:' or `::'. */
22198 cp_lexer_consume_token (parser->lexer);
22199 /* Parse the output-operands. */
22200 if (cp_lexer_next_token_is_not (parser->lexer,
22201 CPP_COLON)
22202 && cp_lexer_next_token_is_not (parser->lexer,
22203 CPP_SCOPE)
22204 && cp_lexer_next_token_is_not (parser->lexer,
22205 CPP_CLOSE_PAREN))
22207 inputs = cp_parser_asm_operand_list (parser);
22208 if (inputs == error_mark_node)
22209 invalid_inputs_p = true;
22212 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22213 /* The clobbers are coming next. */
22214 clobbers_p = true;
22216 /* Look for clobbers. */
22217 if (clobbers_p
22218 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22220 clobbers_p = true;
22221 /* Consume the `:' or `::'. */
22222 cp_lexer_consume_token (parser->lexer);
22223 /* Parse the clobbers. */
22224 if (cp_lexer_next_token_is_not (parser->lexer,
22225 CPP_COLON)
22226 && cp_lexer_next_token_is_not (parser->lexer,
22227 CPP_CLOSE_PAREN))
22228 clobbers = cp_parser_asm_clobber_list (parser);
22230 else if (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22231 /* The labels are coming next. */
22232 labels_p = true;
22234 /* Look for labels. */
22235 if (labels_p
22236 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
22238 labels_p = true;
22239 /* Consume the `:' or `::'. */
22240 cp_lexer_consume_token (parser->lexer);
22241 /* Parse the labels. */
22242 labels = cp_parser_asm_label_list (parser);
22245 if (goto_p && !labels_p)
22246 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
22248 else if (goto_p)
22249 missing = RT_COLON_SCOPE;
22251 /* Look for the closing `)'. */
22252 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
22253 missing ? missing : RT_CLOSE_PAREN))
22254 cp_parser_skip_to_closing_parenthesis (parser, true, false,
22255 /*consume_paren=*/true);
22256 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22258 if (!invalid_inputs_p && !invalid_outputs_p)
22260 /* Create the ASM_EXPR. */
22261 if (parser->in_function_body)
22263 asm_stmt = finish_asm_stmt (asm_loc, volatile_p, string, outputs,
22264 inputs, clobbers, labels, inline_p);
22265 /* If the extended syntax was not used, mark the ASM_EXPR. */
22266 if (!extended_p)
22268 tree temp = asm_stmt;
22269 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
22270 temp = TREE_OPERAND (temp, 0);
22272 ASM_INPUT_P (temp) = 1;
22275 else
22276 symtab->finalize_toplevel_asm (string);
22280 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
22281 type that comes from the decl-specifier-seq. */
22283 static tree
22284 strip_declarator_types (tree type, cp_declarator *declarator)
22286 for (cp_declarator *d = declarator; d;)
22287 switch (d->kind)
22289 case cdk_id:
22290 case cdk_decomp:
22291 case cdk_error:
22292 d = NULL;
22293 break;
22295 default:
22296 if (TYPE_PTRMEMFUNC_P (type))
22297 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
22298 type = TREE_TYPE (type);
22299 d = d->declarator;
22300 break;
22303 return type;
22306 /* Warn about the most vexing parse syntactic ambiguity, i.e., warn when
22307 a construct looks like a variable definition but is actually a function
22308 declaration. DECL_SPECIFIERS is the decl-specifier-seq and DECLARATOR
22309 is the declarator for this function declaration. */
22311 static void
22312 warn_about_ambiguous_parse (const cp_decl_specifier_seq *decl_specifiers,
22313 const cp_declarator *declarator)
22315 /* Only warn if we are declaring a function at block scope. */
22316 if (!at_function_scope_p ())
22317 return;
22319 /* And only if there is no storage class specified. */
22320 if (decl_specifiers->storage_class != sc_none
22321 || decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
22322 return;
22324 if (declarator->kind != cdk_function
22325 || !declarator->declarator
22326 || declarator->declarator->kind != cdk_id
22327 || !identifier_p (get_unqualified_id
22328 (const_cast<cp_declarator *>(declarator))))
22329 return;
22331 /* Don't warn when the whole declarator (not just the declarator-id!)
22332 was parenthesized. That is, don't warn for int(n()) but do warn
22333 for int(f)(). */
22334 if (declarator->parenthesized != UNKNOWN_LOCATION)
22335 return;
22337 tree type;
22338 if (decl_specifiers->type)
22340 type = decl_specifiers->type;
22341 if (TREE_CODE (type) == TYPE_DECL)
22342 type = TREE_TYPE (type);
22344 /* If the return type is void there is no ambiguity. */
22345 if (same_type_p (type, void_type_node))
22346 return;
22348 else if (decl_specifiers->any_type_specifiers_p)
22349 /* Code like long f(); will have null ->type. If we have any
22350 type-specifiers, pretend we've seen int. */
22351 type = integer_type_node;
22352 else
22353 return;
22355 auto_diagnostic_group d;
22356 location_t loc = declarator->u.function.parens_loc;
22357 tree params = declarator->u.function.parameters;
22358 const bool has_list_ctor_p = CLASS_TYPE_P (type) && TYPE_HAS_LIST_CTOR (type);
22360 /* The T t() case. */
22361 if (params == void_list_node)
22363 if (warning_at (loc, OPT_Wvexing_parse,
22364 "empty parentheses were disambiguated as a function "
22365 "declaration"))
22367 /* () means value-initialization (C++03 and up); {} (C++11 and up)
22368 means value-initialization or aggregate-initialization, nothing
22369 means default-initialization. We can only suggest removing the
22370 parentheses/adding {} if T has a default constructor. */
22371 if (!CLASS_TYPE_P (type) || TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
22373 gcc_rich_location iloc (loc);
22374 iloc.add_fixit_remove ();
22375 inform (&iloc, "remove parentheses to default-initialize "
22376 "a variable");
22377 if (cxx_dialect >= cxx11 && !has_list_ctor_p)
22379 if (CP_AGGREGATE_TYPE_P (type))
22380 inform (loc, "or replace parentheses with braces to "
22381 "aggregate-initialize a variable");
22382 else
22383 inform (loc, "or replace parentheses with braces to "
22384 "value-initialize a variable");
22388 return;
22391 /* If we had (...) or the parameter-list wasn't parenthesized,
22392 we're done. */
22393 if (params == NULL_TREE || !PARENTHESIZED_LIST_P (params))
22394 return;
22396 /* The T t(X()) case. */
22397 if (list_length (params) == 2)
22399 if (warning_at (loc, OPT_Wvexing_parse,
22400 "parentheses were disambiguated as a function "
22401 "declaration"))
22403 gcc_rich_location iloc (loc);
22404 /* {}-initialization means that we can use an initializer-list
22405 constructor if no default constructor is available, so don't
22406 suggest using {} for classes that have an initializer_list
22407 constructor. */
22408 if (cxx_dialect >= cxx11 && !has_list_ctor_p)
22410 iloc.add_fixit_replace (get_start (loc), "{");
22411 iloc.add_fixit_replace (get_finish (loc), "}");
22412 inform (&iloc, "replace parentheses with braces to declare a "
22413 "variable");
22415 else
22417 iloc.add_fixit_insert_after (get_start (loc), "(");
22418 iloc.add_fixit_insert_before (get_finish (loc), ")");
22419 inform (&iloc, "add parentheses to declare a variable");
22423 /* The T t(X(), X()) case. */
22424 else if (warning_at (loc, OPT_Wvexing_parse,
22425 "parentheses were disambiguated as a function "
22426 "declaration"))
22428 gcc_rich_location iloc (loc);
22429 if (cxx_dialect >= cxx11 && !has_list_ctor_p)
22431 iloc.add_fixit_replace (get_start (loc), "{");
22432 iloc.add_fixit_replace (get_finish (loc), "}");
22433 inform (&iloc, "replace parentheses with braces to declare a "
22434 "variable");
22439 /* If DECLARATOR with DECL_SPECS is a function declarator that has
22440 the form of a deduction guide, tag it as such. CTOR_DTOR_OR_CONV_P
22441 has the same meaning as in cp_parser_declarator. */
22443 static void
22444 cp_parser_maybe_adjust_declarator_for_dguide (cp_parser *parser,
22445 cp_decl_specifier_seq *decl_specs,
22446 cp_declarator *declarator,
22447 int *ctor_dtor_or_conv_p)
22449 if (cxx_dialect >= cxx17
22450 && *ctor_dtor_or_conv_p <= 0
22451 && !decl_specs->type
22452 && !decl_specs->any_type_specifiers_p
22453 && function_declarator_p (declarator))
22455 cp_declarator *id = get_id_declarator (declarator);
22456 tree name = id->u.id.unqualified_name;
22457 parser->scope = id->u.id.qualifying_scope;
22458 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
22459 if (tmpl
22460 && (DECL_CLASS_TEMPLATE_P (tmpl)
22461 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
22463 id->u.id.unqualified_name = dguide_name (tmpl);
22464 id->u.id.sfk = sfk_deduction_guide;
22465 *ctor_dtor_or_conv_p = 1;
22470 /* Declarators [gram.dcl.decl] */
22472 /* Parse an init-declarator.
22474 init-declarator:
22475 declarator initializer [opt]
22477 GNU Extension:
22479 init-declarator:
22480 declarator asm-specification [opt] attributes [opt] initializer [opt]
22482 function-definition:
22483 decl-specifier-seq [opt] declarator ctor-initializer [opt]
22484 function-body
22485 decl-specifier-seq [opt] declarator function-try-block
22487 GNU Extension:
22489 function-definition:
22490 __extension__ function-definition
22492 TM Extension:
22494 function-definition:
22495 decl-specifier-seq [opt] declarator function-transaction-block
22497 The parser flags FLAGS is used to control type-specifier parsing.
22499 The DECL_SPECIFIERS apply to this declarator. Returns a
22500 representation of the entity declared. If MEMBER_P is TRUE, then
22501 this declarator appears in a class scope. The new DECL created by
22502 this declarator is returned.
22504 The CHECKS are access checks that should be performed once we know
22505 what entity is being declared (and, therefore, what classes have
22506 befriended it).
22508 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
22509 for a function-definition here as well. If the declarator is a
22510 declarator for a function-definition, *FUNCTION_DEFINITION_P will
22511 be TRUE upon return. By that point, the function-definition will
22512 have been completely parsed.
22514 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
22515 is FALSE.
22517 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
22518 parsed declaration if it is an uninitialized single declarator not followed
22519 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
22520 if present, will not be consumed. If returned, this declarator will be
22521 created with SD_INITIALIZED but will not call cp_finish_decl.
22523 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
22524 and there is an initializer, the pointed location_t is set to the
22525 location of the '=' or `(', or '{' in C++11 token introducing the
22526 initializer. */
22528 static tree
22529 cp_parser_init_declarator (cp_parser* parser,
22530 cp_parser_flags flags,
22531 cp_decl_specifier_seq *decl_specifiers,
22532 vec<deferred_access_check, va_gc> *checks,
22533 bool function_definition_allowed_p,
22534 bool member_p,
22535 int declares_class_or_enum,
22536 bool* function_definition_p,
22537 tree* maybe_range_for_decl,
22538 location_t* init_loc,
22539 tree* auto_result)
22541 cp_token *token = NULL, *asm_spec_start_token = NULL,
22542 *attributes_start_token = NULL;
22543 cp_declarator *declarator;
22544 tree prefix_attributes;
22545 tree attributes = NULL;
22546 tree asm_specification;
22547 tree initializer;
22548 tree decl = NULL_TREE;
22549 tree scope;
22550 int is_initialized;
22551 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
22552 initialized with "= ..", CPP_OPEN_PAREN if initialized with
22553 "(...)". */
22554 enum cpp_ttype initialization_kind;
22555 bool is_direct_init = false;
22556 bool is_non_constant_init;
22557 int ctor_dtor_or_conv_p;
22558 bool friend_p = cp_parser_friend_p (decl_specifiers);
22559 bool static_p = decl_specifiers->storage_class == sc_static;
22560 tree pushed_scope = NULL_TREE;
22561 bool range_for_decl_p = false;
22562 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
22563 location_t tmp_init_loc = UNKNOWN_LOCATION;
22565 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_consteval))
22566 flags |= CP_PARSER_FLAGS_CONSTEVAL;
22568 /* Assume that this is not the declarator for a function
22569 definition. */
22570 if (function_definition_p)
22571 *function_definition_p = false;
22573 /* Default arguments are only permitted for function parameters. */
22574 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
22575 parser->default_arg_ok_p = false;
22577 /* Defer access checks while parsing the declarator; we cannot know
22578 what names are accessible until we know what is being
22579 declared. */
22580 resume_deferring_access_checks ();
22582 token = cp_lexer_peek_token (parser->lexer);
22584 /* Parse the declarator. */
22585 declarator
22586 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22587 flags, &ctor_dtor_or_conv_p,
22588 /*parenthesized_p=*/NULL,
22589 member_p, friend_p, static_p);
22590 /* Gather up the deferred checks. */
22591 stop_deferring_access_checks ();
22593 parser->default_arg_ok_p = saved_default_arg_ok_p;
22595 /* If the DECLARATOR was erroneous, there's no need to go
22596 further. */
22597 if (declarator == cp_error_declarator)
22598 return error_mark_node;
22600 /* Check that the number of template-parameter-lists is OK. */
22601 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
22602 token->location))
22603 return error_mark_node;
22605 if (declares_class_or_enum & 2)
22606 cp_parser_check_for_definition_in_return_type (declarator,
22607 decl_specifiers->type,
22608 decl_specifiers->locations[ds_type_spec]);
22610 /* Figure out what scope the entity declared by the DECLARATOR is
22611 located in. `grokdeclarator' sometimes changes the scope, so
22612 we compute it now. */
22613 scope = get_scope_of_declarator (declarator);
22615 /* Perform any lookups in the declared type which were thought to be
22616 dependent, but are not in the scope of the declarator. */
22617 decl_specifiers->type
22618 = maybe_update_decl_type (decl_specifiers->type, scope);
22620 /* If we're allowing GNU extensions, look for an
22621 asm-specification. */
22622 if (cp_parser_allow_gnu_extensions_p (parser))
22624 /* Look for an asm-specification. */
22625 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
22626 asm_specification = cp_parser_asm_specification_opt (parser);
22628 else
22629 asm_specification = NULL_TREE;
22631 /* Gather the attributes that were provided with the
22632 decl-specifiers. */
22633 prefix_attributes = decl_specifiers->attributes;
22635 /* Look for attributes. */
22636 attributes_start_token = cp_lexer_peek_token (parser->lexer);
22637 attributes = cp_parser_attributes_opt (parser);
22639 /* Peek at the next token. */
22640 token = cp_lexer_peek_token (parser->lexer);
22642 bool bogus_implicit_tmpl = false;
22644 if (function_declarator_p (declarator))
22646 /* Handle C++17 deduction guides. Note that class-scope
22647 non-template deduction guides are instead handled in
22648 cp_parser_member_declaration. */
22649 cp_parser_maybe_adjust_declarator_for_dguide (parser,
22650 decl_specifiers,
22651 declarator,
22652 &ctor_dtor_or_conv_p);
22654 if (!member_p && !cp_parser_error_occurred (parser))
22655 warn_about_ambiguous_parse (decl_specifiers, declarator);
22657 /* Check to see if the token indicates the start of a
22658 function-definition. */
22659 if (cp_parser_token_starts_function_definition_p (token))
22661 if (!function_definition_allowed_p)
22663 /* If a function-definition should not appear here, issue an
22664 error message. */
22665 cp_parser_error (parser,
22666 "a function-definition is not allowed here");
22667 return error_mark_node;
22670 location_t func_brace_location
22671 = cp_lexer_peek_token (parser->lexer)->location;
22673 /* Neither attributes nor an asm-specification are allowed
22674 on a function-definition. */
22675 if (asm_specification)
22676 error_at (asm_spec_start_token->location,
22677 "an %<asm%> specification is not allowed "
22678 "on a function-definition");
22679 if (attributes)
22680 error_at (attributes_start_token->location,
22681 "attributes are not allowed "
22682 "on a function-definition");
22683 /* This is a function-definition. */
22684 *function_definition_p = true;
22686 /* Parse the function definition. */
22687 if (member_p)
22688 decl = cp_parser_save_member_function_body (parser,
22689 decl_specifiers,
22690 declarator,
22691 prefix_attributes);
22692 else
22693 decl =
22694 (cp_parser_function_definition_from_specifiers_and_declarator
22695 (parser, decl_specifiers, prefix_attributes, declarator));
22697 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
22699 /* This is where the prologue starts... */
22700 DECL_STRUCT_FUNCTION (decl)->function_start_locus
22701 = func_brace_location;
22704 return decl;
22707 else if (parser->fully_implicit_function_template_p)
22709 /* A non-template declaration involving a function parameter list
22710 containing an implicit template parameter will be made into a
22711 template. If the resulting declaration is not going to be an
22712 actual function then finish the template scope here to prevent it.
22713 An error message will be issued once we have a decl to talk about.
22715 FIXME probably we should do type deduction rather than create an
22716 implicit template, but the standard currently doesn't allow it. */
22717 bogus_implicit_tmpl = true;
22718 finish_fully_implicit_template (parser, NULL_TREE);
22721 /* [dcl.dcl]
22723 Only in function declarations for constructors, destructors, type
22724 conversions, and deduction guides can the decl-specifier-seq be omitted.
22726 We explicitly postpone this check past the point where we handle
22727 function-definitions because we tolerate function-definitions
22728 that are missing their return types in some modes. */
22729 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
22731 cp_parser_error (parser,
22732 "expected constructor, destructor, or type conversion");
22733 return error_mark_node;
22736 /* An `=' or an '{' in C++11, indicate an initializer. An '(' may indicate
22737 an initializer as well. */
22738 if (token->type == CPP_EQ
22739 || token->type == CPP_OPEN_PAREN
22740 || token->type == CPP_OPEN_BRACE)
22742 /* Don't get fooled into thinking that F(i)(1)(2) is an initializer.
22743 It isn't; it's an expression. (Here '(i)' would have already been
22744 parsed as a declarator.) */
22745 if (token->type == CPP_OPEN_PAREN
22746 && cp_parser_uncommitted_to_tentative_parse_p (parser))
22748 cp_lexer_save_tokens (parser->lexer);
22749 cp_lexer_consume_token (parser->lexer);
22750 cp_parser_skip_to_closing_parenthesis (parser,
22751 /*recovering*/false,
22752 /*or_comma*/false,
22753 /*consume_paren*/true);
22754 /* If this is an initializer, only a ',' or ';' can follow: either
22755 we have another init-declarator, or we're at the end of an
22756 init-declarator-list which can only be followed by a ';'. */
22757 bool ok = (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22758 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
22759 cp_lexer_rollback_tokens (parser->lexer);
22760 if (UNLIKELY (!ok))
22761 /* Not an init-declarator. */
22762 return error_mark_node;
22764 is_initialized = SD_INITIALIZED;
22765 initialization_kind = token->type;
22766 declarator->init_loc = token->location;
22767 if (maybe_range_for_decl)
22768 *maybe_range_for_decl = error_mark_node;
22769 tmp_init_loc = token->location;
22770 if (init_loc && *init_loc == UNKNOWN_LOCATION)
22771 *init_loc = tmp_init_loc;
22773 if (token->type == CPP_EQ
22774 && function_declarator_p (declarator))
22776 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
22777 if (t2->keyword == RID_DEFAULT)
22778 is_initialized = SD_DEFAULTED;
22779 else if (t2->keyword == RID_DELETE)
22780 is_initialized = SD_DELETED;
22783 else
22785 /* If the init-declarator isn't initialized and isn't followed by a
22786 `,' or `;', it's not a valid init-declarator. */
22787 if (token->type != CPP_COMMA
22788 && token->type != CPP_SEMICOLON)
22790 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
22791 range_for_decl_p = true;
22792 else
22794 if (!maybe_range_for_decl)
22795 cp_parser_error (parser, "expected initializer");
22796 return error_mark_node;
22799 is_initialized = SD_UNINITIALIZED;
22800 initialization_kind = CPP_EOF;
22803 /* Because start_decl has side-effects, we should only call it if we
22804 know we're going ahead. By this point, we know that we cannot
22805 possibly be looking at any other construct. */
22806 cp_parser_commit_to_tentative_parse (parser);
22808 /* Enter the newly declared entry in the symbol table. If we're
22809 processing a declaration in a class-specifier, we wait until
22810 after processing the initializer. */
22811 if (!member_p)
22813 if (parser->in_unbraced_linkage_specification_p)
22814 decl_specifiers->storage_class = sc_extern;
22815 decl = start_decl (declarator, decl_specifiers,
22816 range_for_decl_p? SD_INITIALIZED : is_initialized,
22817 attributes, prefix_attributes, &pushed_scope);
22818 cp_finalize_omp_declare_simd (parser, decl);
22819 cp_finalize_oacc_routine (parser, decl, false);
22820 /* Adjust location of decl if declarator->id_loc is more appropriate:
22821 set, and decl wasn't merged with another decl, in which case its
22822 location would be different from input_location, and more accurate. */
22823 if (DECL_P (decl)
22824 && declarator->id_loc != UNKNOWN_LOCATION
22825 && DECL_SOURCE_LOCATION (decl) == input_location)
22826 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
22828 else if (scope)
22829 /* Enter the SCOPE. That way unqualified names appearing in the
22830 initializer will be looked up in SCOPE. */
22831 pushed_scope = push_scope (scope);
22833 /* Perform deferred access control checks, now that we know in which
22834 SCOPE the declared entity resides. */
22835 if (!member_p && decl)
22837 tree saved_current_function_decl = NULL_TREE;
22839 /* If the entity being declared is a function, pretend that we
22840 are in its scope. If it is a `friend', it may have access to
22841 things that would not otherwise be accessible. */
22842 if (TREE_CODE (decl) == FUNCTION_DECL)
22844 saved_current_function_decl = current_function_decl;
22845 current_function_decl = decl;
22848 /* Perform access checks for template parameters. */
22849 cp_parser_perform_template_parameter_access_checks (checks);
22851 /* Perform the access control checks for the declarator and the
22852 decl-specifiers. */
22853 perform_deferred_access_checks (tf_warning_or_error);
22855 /* Restore the saved value. */
22856 if (TREE_CODE (decl) == FUNCTION_DECL)
22857 current_function_decl = saved_current_function_decl;
22860 /* Parse the initializer. */
22861 initializer = NULL_TREE;
22862 is_direct_init = false;
22863 is_non_constant_init = true;
22864 if (is_initialized)
22866 if (function_declarator_p (declarator))
22868 if (initialization_kind == CPP_EQ)
22869 initializer = cp_parser_pure_specifier (parser);
22870 else
22872 /* If the declaration was erroneous, we don't really
22873 know what the user intended, so just silently
22874 consume the initializer. */
22875 if (decl != error_mark_node)
22876 error_at (tmp_init_loc, "initializer provided for function");
22877 cp_parser_skip_to_closing_parenthesis (parser,
22878 /*recovering=*/true,
22879 /*or_comma=*/false,
22880 /*consume_paren=*/true);
22883 else
22885 /* We want to record the extra mangling scope for in-class
22886 initializers of class members and initializers of static
22887 data member templates and namespace-scope initializers.
22888 The former involves deferring parsing of the initializer
22889 until end of class as with default arguments. So right
22890 here we only handle the latter two. */
22891 bool has_lambda_scope = false;
22893 if (decl != error_mark_node
22894 && !member_p
22895 && (processing_template_decl || DECL_NAMESPACE_SCOPE_P (decl)))
22896 has_lambda_scope = true;
22898 if (has_lambda_scope)
22899 start_lambda_scope (decl);
22900 initializer = cp_parser_initializer (parser,
22901 &is_direct_init,
22902 &is_non_constant_init);
22903 if (has_lambda_scope)
22904 finish_lambda_scope ();
22905 if (initializer == error_mark_node)
22906 cp_parser_skip_to_end_of_statement (parser);
22910 /* The old parser allows attributes to appear after a parenthesized
22911 initializer. Mark Mitchell proposed removing this functionality
22912 on the GCC mailing lists on 2002-08-13. This parser accepts the
22913 attributes -- but ignores them. Made a permerror in GCC 8. */
22914 if (cp_parser_allow_gnu_extensions_p (parser)
22915 && initialization_kind == CPP_OPEN_PAREN
22916 && cp_parser_attributes_opt (parser)
22917 && permerror (input_location,
22918 "attributes after parenthesized initializer ignored"))
22920 static bool hint;
22921 if (flag_permissive && !hint)
22923 hint = true;
22924 inform (input_location,
22925 "this flexibility is deprecated and will be removed");
22929 /* And now complain about a non-function implicit template. */
22930 if (bogus_implicit_tmpl && decl != error_mark_node)
22931 error_at (DECL_SOURCE_LOCATION (decl),
22932 "non-function %qD declared as implicit template", decl);
22934 /* For an in-class declaration, use `grokfield' to create the
22935 declaration. */
22936 if (member_p)
22938 if (pushed_scope)
22940 pop_scope (pushed_scope);
22941 pushed_scope = NULL_TREE;
22943 decl = grokfield (declarator, decl_specifiers,
22944 initializer, !is_non_constant_init,
22945 /*asmspec=*/NULL_TREE,
22946 attr_chainon (attributes, prefix_attributes));
22947 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
22948 cp_parser_save_default_args (parser, decl);
22949 cp_finalize_omp_declare_simd (parser, decl);
22950 cp_finalize_oacc_routine (parser, decl, false);
22953 /* Finish processing the declaration. But, skip member
22954 declarations. */
22955 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
22957 cp_finish_decl (decl,
22958 initializer, !is_non_constant_init,
22959 asm_specification,
22960 /* If the initializer is in parentheses, then this is
22961 a direct-initialization, which means that an
22962 `explicit' constructor is OK. Otherwise, an
22963 `explicit' constructor cannot be used. */
22964 ((is_direct_init || !is_initialized)
22965 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
22967 else if ((cxx_dialect != cxx98) && friend_p
22968 && decl && TREE_CODE (decl) == FUNCTION_DECL)
22969 /* Core issue #226 (C++0x only): A default template-argument
22970 shall not be specified in a friend class template
22971 declaration. */
22972 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
22973 /*is_partial=*/false, /*is_friend_decl=*/1);
22975 if (!friend_p && pushed_scope)
22976 pop_scope (pushed_scope);
22978 if (function_declarator_p (declarator)
22979 && parser->fully_implicit_function_template_p)
22981 if (member_p)
22982 decl = finish_fully_implicit_template (parser, decl);
22983 else
22984 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
22987 if (auto_result && is_initialized && decl_specifiers->type
22988 && type_uses_auto (decl_specifiers->type))
22989 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
22991 return decl;
22994 /* Parse a declarator.
22996 declarator:
22997 direct-declarator
22998 ptr-operator declarator
23000 abstract-declarator:
23001 ptr-operator abstract-declarator [opt]
23002 direct-abstract-declarator
23004 GNU Extensions:
23006 declarator:
23007 attributes [opt] direct-declarator
23008 attributes [opt] ptr-operator declarator
23010 abstract-declarator:
23011 attributes [opt] ptr-operator abstract-declarator [opt]
23012 attributes [opt] direct-abstract-declarator
23014 The parser flags FLAGS is used to control type-specifier parsing.
23016 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
23017 detect constructors, destructors, deduction guides, or conversion operators.
23018 It is set to -1 if the declarator is a name, and +1 if it is a
23019 function. Otherwise it is set to zero. Usually you just want to
23020 test for >0, but internally the negative value is used.
23022 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
23023 a decl-specifier-seq unless it declares a constructor, destructor,
23024 or conversion. It might seem that we could check this condition in
23025 semantic analysis, rather than parsing, but that makes it difficult
23026 to handle something like `f()'. We want to notice that there are
23027 no decl-specifiers, and therefore realize that this is an
23028 expression, not a declaration.)
23030 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
23031 the declarator is a direct-declarator of the form "(...)".
23033 MEMBER_P is true iff this declarator is a member-declarator.
23035 FRIEND_P is true iff this declarator is a friend.
23037 STATIC_P is true iff the keyword static was seen. */
23039 static cp_declarator *
23040 cp_parser_declarator (cp_parser* parser,
23041 cp_parser_declarator_kind dcl_kind,
23042 cp_parser_flags flags,
23043 int* ctor_dtor_or_conv_p,
23044 bool* parenthesized_p,
23045 bool member_p, bool friend_p, bool static_p)
23047 cp_declarator *declarator;
23048 enum tree_code code;
23049 cp_cv_quals cv_quals;
23050 tree class_type;
23051 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
23053 /* Assume this is not a constructor, destructor, or type-conversion
23054 operator. */
23055 if (ctor_dtor_or_conv_p)
23056 *ctor_dtor_or_conv_p = 0;
23058 if (cp_parser_allow_gnu_extensions_p (parser))
23059 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
23061 /* Check for the ptr-operator production. */
23062 cp_parser_parse_tentatively (parser);
23063 /* Parse the ptr-operator. */
23064 code = cp_parser_ptr_operator (parser,
23065 &class_type,
23066 &cv_quals,
23067 &std_attributes);
23069 /* If that worked, then we have a ptr-operator. */
23070 if (cp_parser_parse_definitely (parser))
23072 /* If a ptr-operator was found, then this declarator was not
23073 parenthesized. */
23074 if (parenthesized_p)
23075 *parenthesized_p = false;
23076 /* The dependent declarator is optional if we are parsing an
23077 abstract-declarator. */
23078 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
23079 cp_parser_parse_tentatively (parser);
23081 /* Parse the dependent declarator. */
23082 declarator = cp_parser_declarator (parser, dcl_kind, flags,
23083 /*ctor_dtor_or_conv_p=*/NULL,
23084 /*parenthesized_p=*/NULL,
23085 member_p, friend_p, static_p);
23087 /* If we are parsing an abstract-declarator, we must handle the
23088 case where the dependent declarator is absent. */
23089 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
23090 && !cp_parser_parse_definitely (parser))
23091 declarator = NULL;
23093 declarator = cp_parser_make_indirect_declarator
23094 (code, class_type, cv_quals, declarator, std_attributes);
23096 /* Everything else is a direct-declarator. */
23097 else
23099 if (parenthesized_p)
23100 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
23101 CPP_OPEN_PAREN);
23102 declarator = cp_parser_direct_declarator (parser, dcl_kind,
23103 flags, ctor_dtor_or_conv_p,
23104 member_p, friend_p, static_p);
23107 if (gnu_attributes && declarator && declarator != cp_error_declarator)
23108 declarator->attributes = gnu_attributes;
23109 return declarator;
23112 /* Parse a direct-declarator or direct-abstract-declarator.
23114 direct-declarator:
23115 declarator-id
23116 direct-declarator ( parameter-declaration-clause )
23117 cv-qualifier-seq [opt]
23118 ref-qualifier [opt]
23119 exception-specification [opt]
23120 direct-declarator [ constant-expression [opt] ]
23121 ( declarator )
23123 direct-abstract-declarator:
23124 direct-abstract-declarator [opt]
23125 ( parameter-declaration-clause )
23126 cv-qualifier-seq [opt]
23127 ref-qualifier [opt]
23128 exception-specification [opt]
23129 direct-abstract-declarator [opt] [ constant-expression [opt] ]
23130 ( abstract-declarator )
23132 Returns a representation of the declarator. DCL_KIND is
23133 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
23134 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
23135 we are parsing a direct-declarator. It is
23136 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
23137 of ambiguity we prefer an abstract declarator, as per
23138 [dcl.ambig.res].
23139 The parser flags FLAGS is used to control type-specifier parsing.
23140 CTOR_DTOR_OR_CONV_P, MEMBER_P, FRIEND_P, and STATIC_P are
23141 as for cp_parser_declarator. */
23143 static cp_declarator *
23144 cp_parser_direct_declarator (cp_parser* parser,
23145 cp_parser_declarator_kind dcl_kind,
23146 cp_parser_flags flags,
23147 int* ctor_dtor_or_conv_p,
23148 bool member_p, bool friend_p, bool static_p)
23150 cp_token *token;
23151 cp_declarator *declarator = NULL;
23152 tree scope = NULL_TREE;
23153 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
23154 bool saved_in_declarator_p = parser->in_declarator_p;
23155 bool first = true;
23156 tree pushed_scope = NULL_TREE;
23157 cp_token *open_paren = NULL, *close_paren = NULL;
23159 while (true)
23161 /* Peek at the next token. */
23162 token = cp_lexer_peek_token (parser->lexer);
23163 if (token->type == CPP_OPEN_PAREN)
23165 /* This is either a parameter-declaration-clause, or a
23166 parenthesized declarator. When we know we are parsing a
23167 named declarator, it must be a parenthesized declarator
23168 if FIRST is true. For instance, `(int)' is a
23169 parameter-declaration-clause, with an omitted
23170 direct-abstract-declarator. But `((*))', is a
23171 parenthesized abstract declarator. Finally, when T is a
23172 template parameter `(T)' is a
23173 parameter-declaration-clause, and not a parenthesized
23174 named declarator.
23176 We first try and parse a parameter-declaration-clause,
23177 and then try a nested declarator (if FIRST is true).
23179 It is not an error for it not to be a
23180 parameter-declaration-clause, even when FIRST is
23181 false. Consider,
23183 int i (int);
23184 int i (3);
23186 The first is the declaration of a function while the
23187 second is the definition of a variable, including its
23188 initializer.
23190 Having seen only the parenthesis, we cannot know which of
23191 these two alternatives should be selected. Even more
23192 complex are examples like:
23194 int i (int (a));
23195 int i (int (3));
23197 The former is a function-declaration; the latter is a
23198 variable initialization.
23200 Thus again, we try a parameter-declaration-clause, and if
23201 that fails, we back out and return. */
23203 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
23205 tree params;
23206 bool is_declarator = false;
23208 open_paren = NULL;
23210 /* In a member-declarator, the only valid interpretation
23211 of a parenthesis is the start of a
23212 parameter-declaration-clause. (It is invalid to
23213 initialize a static data member with a parenthesized
23214 initializer; only the "=" form of initialization is
23215 permitted.) */
23216 if (!member_p)
23217 cp_parser_parse_tentatively (parser);
23219 /* Consume the `('. */
23220 const location_t parens_start = token->location;
23221 matching_parens parens;
23222 parens.consume_open (parser);
23223 if (first)
23225 /* If this is going to be an abstract declarator, we're
23226 in a declarator and we can't have default args. */
23227 parser->default_arg_ok_p = false;
23228 parser->in_declarator_p = true;
23231 begin_scope (sk_function_parms, NULL_TREE);
23233 /* Parse the parameter-declaration-clause. */
23234 params
23235 = cp_parser_parameter_declaration_clause (parser, flags);
23236 const location_t parens_end
23237 = cp_lexer_peek_token (parser->lexer)->location;
23239 /* Consume the `)'. */
23240 parens.require_close (parser);
23242 /* If all went well, parse the cv-qualifier-seq,
23243 ref-qualifier and the exception-specification. */
23244 if (member_p || cp_parser_parse_definitely (parser))
23246 cp_cv_quals cv_quals;
23247 cp_virt_specifiers virt_specifiers;
23248 cp_ref_qualifier ref_qual;
23249 tree exception_specification;
23250 tree late_return;
23251 tree attrs;
23252 bool memfn = (member_p || (pushed_scope
23253 && CLASS_TYPE_P (pushed_scope)));
23254 unsigned char local_variables_forbidden_p
23255 = parser->local_variables_forbidden_p;
23256 /* 'this' is not allowed in static member functions. */
23257 if (static_p || friend_p)
23258 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
23260 is_declarator = true;
23262 if (ctor_dtor_or_conv_p)
23263 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
23264 first = false;
23266 /* Parse the cv-qualifier-seq. */
23267 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
23268 /* Parse the ref-qualifier. */
23269 ref_qual = cp_parser_ref_qualifier_opt (parser);
23270 /* Parse the tx-qualifier. */
23271 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
23273 tree save_ccp = current_class_ptr;
23274 tree save_ccr = current_class_ref;
23275 if (memfn && !friend_p && !static_p)
23276 /* DR 1207: 'this' is in scope after the cv-quals. */
23277 inject_this_parameter (current_class_type, cv_quals);
23279 /* If it turned out that this is e.g. a pointer to a
23280 function, we don't want to delay noexcept parsing. */
23281 if (declarator == NULL || declarator->kind != cdk_id)
23282 flags &= ~CP_PARSER_FLAGS_DELAY_NOEXCEPT;
23284 /* Parse the exception-specification. */
23285 exception_specification
23286 = cp_parser_exception_specification_opt (parser,
23287 flags);
23289 attrs = cp_parser_std_attribute_spec_seq (parser);
23291 cp_omp_declare_simd_data odsd;
23292 if ((flag_openmp || flag_openmp_simd)
23293 && declarator
23294 && declarator->std_attributes
23295 && declarator->kind == cdk_id)
23297 tree *pa = &declarator->std_attributes;
23298 cp_parser_handle_directive_omp_attributes (parser, pa,
23299 &odsd, false);
23302 /* In here, we handle cases where attribute is used after
23303 the function declaration. For example:
23304 void func (int x) __attribute__((vector(..))); */
23305 tree gnu_attrs = NULL_TREE;
23306 tree requires_clause = NULL_TREE;
23307 late_return
23308 = cp_parser_late_return_type_opt (parser, declarator,
23309 requires_clause);
23311 cp_finalize_omp_declare_simd (parser, &odsd);
23313 /* Parse the virt-specifier-seq. */
23314 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
23316 location_t parens_loc = make_location (parens_start,
23317 parens_start,
23318 parens_end);
23319 /* Create the function-declarator. */
23320 declarator = make_call_declarator (declarator,
23321 params,
23322 cv_quals,
23323 virt_specifiers,
23324 ref_qual,
23325 tx_qual,
23326 exception_specification,
23327 late_return,
23328 requires_clause,
23329 attrs,
23330 parens_loc);
23331 declarator->attributes = gnu_attrs;
23332 /* Any subsequent parameter lists are to do with
23333 return type, so are not those of the declared
23334 function. */
23335 parser->default_arg_ok_p = false;
23337 current_class_ptr = save_ccp;
23338 current_class_ref = save_ccr;
23340 /* Restore the state of local_variables_forbidden_p. */
23341 parser->local_variables_forbidden_p
23342 = local_variables_forbidden_p;
23345 /* Remove the function parms from scope. */
23346 pop_bindings_and_leave_scope ();
23348 if (is_declarator)
23349 /* Repeat the main loop. */
23350 continue;
23353 /* If this is the first, we can try a parenthesized
23354 declarator. */
23355 if (first)
23357 bool saved_in_type_id_in_expr_p;
23359 parser->default_arg_ok_p = saved_default_arg_ok_p;
23360 parser->in_declarator_p = saved_in_declarator_p;
23362 open_paren = token;
23363 /* Consume the `('. */
23364 matching_parens parens;
23365 parens.consume_open (parser);
23366 /* Parse the nested declarator. */
23367 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
23368 parser->in_type_id_in_expr_p = true;
23369 declarator
23370 = cp_parser_declarator (parser, dcl_kind, flags,
23371 ctor_dtor_or_conv_p,
23372 /*parenthesized_p=*/NULL,
23373 member_p, friend_p,
23374 /*static_p=*/false);
23375 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
23376 first = false;
23377 /* Expect a `)'. */
23378 close_paren = cp_lexer_peek_token (parser->lexer);
23379 if (!parens.require_close (parser))
23380 declarator = cp_error_declarator;
23381 if (declarator == cp_error_declarator)
23382 break;
23384 goto handle_declarator;
23386 /* Otherwise, we must be done. */
23387 else
23388 break;
23390 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
23391 && token->type == CPP_OPEN_SQUARE
23392 && !cp_next_tokens_can_be_attribute_p (parser))
23394 /* Parse an array-declarator. */
23395 tree bounds, attrs;
23397 if (ctor_dtor_or_conv_p)
23398 *ctor_dtor_or_conv_p = 0;
23400 open_paren = NULL;
23401 first = false;
23402 parser->default_arg_ok_p = false;
23403 parser->in_declarator_p = true;
23404 /* Consume the `['. */
23405 cp_lexer_consume_token (parser->lexer);
23406 /* Peek at the next token. */
23407 token = cp_lexer_peek_token (parser->lexer);
23408 /* If the next token is `]', then there is no
23409 constant-expression. */
23410 if (token->type != CPP_CLOSE_SQUARE)
23412 bool non_constant_p;
23413 bounds
23414 = cp_parser_constant_expression (parser,
23415 /*allow_non_constant=*/true,
23416 &non_constant_p);
23417 if (!non_constant_p)
23418 /* OK */;
23419 else if (error_operand_p (bounds))
23420 /* Already gave an error. */;
23421 else if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
23422 /* Let compute_array_index_type diagnose this. */;
23423 else if (!parser->in_function_body
23424 || parsing_function_declarator ())
23426 /* Normally, the array bound must be an integral constant
23427 expression. However, as an extension, we allow VLAs
23428 in function scopes as long as they aren't part of a
23429 parameter declaration. */
23430 cp_parser_error (parser,
23431 "array bound is not an integer constant");
23432 bounds = error_mark_node;
23434 else if (processing_template_decl
23435 && !type_dependent_expression_p (bounds))
23437 /* Remember this wasn't a constant-expression. */
23438 bounds = build_nop (TREE_TYPE (bounds), bounds);
23439 TREE_SIDE_EFFECTS (bounds) = 1;
23442 else
23443 bounds = NULL_TREE;
23444 /* Look for the closing `]'. */
23445 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
23447 declarator = cp_error_declarator;
23448 break;
23451 attrs = cp_parser_std_attribute_spec_seq (parser);
23452 declarator = make_array_declarator (declarator, bounds);
23453 declarator->std_attributes = attrs;
23455 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
23458 tree qualifying_scope;
23459 tree unqualified_name;
23460 tree attrs;
23461 special_function_kind sfk;
23462 bool abstract_ok;
23463 bool pack_expansion_p = false;
23464 cp_token *declarator_id_start_token;
23466 /* Parse a declarator-id */
23467 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
23468 if (abstract_ok)
23470 cp_parser_parse_tentatively (parser);
23472 /* If we see an ellipsis, we should be looking at a
23473 parameter pack. */
23474 if (token->type == CPP_ELLIPSIS)
23476 /* Consume the `...' */
23477 cp_lexer_consume_token (parser->lexer);
23479 pack_expansion_p = true;
23483 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
23484 unqualified_name
23485 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
23486 qualifying_scope = parser->scope;
23487 if (abstract_ok)
23489 bool okay = false;
23491 if (!unqualified_name && pack_expansion_p)
23493 /* Check whether an error occurred. */
23494 okay = !cp_parser_error_occurred (parser);
23496 /* We already consumed the ellipsis to mark a
23497 parameter pack, but we have no way to report it,
23498 so abort the tentative parse. We will be exiting
23499 immediately anyway. */
23500 cp_parser_abort_tentative_parse (parser);
23502 else
23503 okay = cp_parser_parse_definitely (parser);
23505 if (!okay)
23506 unqualified_name = error_mark_node;
23507 else if (unqualified_name
23508 && (qualifying_scope
23509 || (!identifier_p (unqualified_name))))
23511 cp_parser_error (parser, "expected unqualified-id");
23512 unqualified_name = error_mark_node;
23516 if (!unqualified_name)
23517 return NULL;
23518 if (unqualified_name == error_mark_node)
23520 declarator = cp_error_declarator;
23521 pack_expansion_p = false;
23522 declarator->parameter_pack_p = false;
23523 break;
23526 attrs = cp_parser_std_attribute_spec_seq (parser);
23528 if (qualifying_scope && at_namespace_scope_p ()
23529 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
23531 /* In the declaration of a member of a template class
23532 outside of the class itself, the SCOPE will sometimes
23533 be a TYPENAME_TYPE. For example, given:
23535 template <typename T>
23536 int S<T>::R::i = 3;
23538 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
23539 this context, we must resolve S<T>::R to an ordinary
23540 type, rather than a typename type.
23542 The reason we normally avoid resolving TYPENAME_TYPEs
23543 is that a specialization of `S' might render
23544 `S<T>::R' not a type. However, if `S' is
23545 specialized, then this `i' will not be used, so there
23546 is no harm in resolving the types here. */
23547 tree type;
23549 /* Resolve the TYPENAME_TYPE. */
23550 type = resolve_typename_type (qualifying_scope,
23551 /*only_current_p=*/false);
23552 /* If that failed, the declarator is invalid. */
23553 if (TREE_CODE (type) == TYPENAME_TYPE)
23555 if (typedef_variant_p (type))
23556 error_at (declarator_id_start_token->location,
23557 "cannot define member of dependent typedef "
23558 "%qT", type);
23559 else
23560 error_at (declarator_id_start_token->location,
23561 "%<%T::%E%> is not a type",
23562 TYPE_CONTEXT (qualifying_scope),
23563 TYPE_IDENTIFIER (qualifying_scope));
23565 qualifying_scope = type;
23568 sfk = sfk_none;
23570 if (unqualified_name)
23572 tree class_type;
23574 if (qualifying_scope
23575 && CLASS_TYPE_P (qualifying_scope))
23576 class_type = qualifying_scope;
23577 else
23578 class_type = current_class_type;
23580 if (TREE_CODE (unqualified_name) == TYPE_DECL)
23582 tree name_type = TREE_TYPE (unqualified_name);
23584 if (!class_type || !same_type_p (name_type, class_type))
23586 /* We do not attempt to print the declarator
23587 here because we do not have enough
23588 information about its original syntactic
23589 form. */
23590 cp_parser_error (parser, "invalid declarator");
23591 declarator = cp_error_declarator;
23592 break;
23594 else if (qualifying_scope
23595 && CLASSTYPE_USE_TEMPLATE (name_type))
23597 error_at (declarator_id_start_token->location,
23598 "invalid use of constructor as a template");
23599 inform (declarator_id_start_token->location,
23600 "use %<%T::%D%> instead of %<%T::%D%> to "
23601 "name the constructor in a qualified name",
23602 class_type,
23603 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
23604 class_type, name_type);
23605 declarator = cp_error_declarator;
23606 break;
23608 unqualified_name = constructor_name (class_type);
23611 if (class_type)
23613 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
23614 sfk = sfk_destructor;
23615 else if (identifier_p (unqualified_name)
23616 && IDENTIFIER_CONV_OP_P (unqualified_name))
23617 sfk = sfk_conversion;
23618 else if (/* There's no way to declare a constructor
23619 for an unnamed type, even if the type
23620 got a name for linkage purposes. */
23621 !TYPE_WAS_UNNAMED (class_type)
23622 /* Handle correctly (c++/19200):
23624 struct S {
23625 struct T{};
23626 friend void S(T);
23629 and also:
23631 namespace N {
23632 void S();
23635 struct S {
23636 friend void N::S();
23637 }; */
23638 && (!friend_p || class_type == qualifying_scope)
23639 && constructor_name_p (unqualified_name,
23640 class_type))
23641 sfk = sfk_constructor;
23642 else if (is_overloaded_fn (unqualified_name)
23643 && DECL_CONSTRUCTOR_P (get_first_fn
23644 (unqualified_name)))
23645 sfk = sfk_constructor;
23647 if (ctor_dtor_or_conv_p && sfk != sfk_none)
23648 *ctor_dtor_or_conv_p = -1;
23651 declarator = make_id_declarator (qualifying_scope,
23652 unqualified_name,
23653 sfk, token->location);
23654 declarator->std_attributes = attrs;
23655 declarator->parameter_pack_p = pack_expansion_p;
23657 if (pack_expansion_p)
23658 maybe_warn_variadic_templates ();
23660 /* We're looking for this case in [temp.res]:
23661 A qualified-id is assumed to name a type if [...]
23662 - it is a decl-specifier of the decl-specifier-seq of a
23663 parameter-declaration in a declarator of a function or
23664 function template declaration, ... */
23665 if (cxx_dialect >= cxx20
23666 && (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL)
23667 && declarator->kind == cdk_id
23668 && !at_class_scope_p ()
23669 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23671 /* ...whose declarator-id is qualified. If it isn't, never
23672 assume the parameters to refer to types. */
23673 if (qualifying_scope == NULL_TREE)
23674 flags &= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
23675 else
23677 /* Now we have something like
23678 template <typename T> int C::x(S::p);
23679 which can be a function template declaration or a
23680 variable template definition. If name lookup for
23681 the declarator-id C::x finds one or more function
23682 templates, assume S::p to name a type. Otherwise,
23683 don't. */
23684 tree decl
23685 = cp_parser_lookup_name (parser, unqualified_name,
23686 none_type,
23687 /*is_template=*/false,
23688 /*is_namespace=*/false,
23689 /*check_dependency=*/false,
23690 /*ambiguous_decls=*/NULL,
23691 token->location);
23693 if (!is_overloaded_fn (decl)
23694 /* Allow
23695 template<typename T>
23696 A<T>::A(T::type) { } */
23697 && !(MAYBE_CLASS_TYPE_P (qualifying_scope)
23698 && constructor_name_p (unqualified_name,
23699 qualifying_scope)))
23700 flags &= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
23705 handle_declarator:;
23706 scope = get_scope_of_declarator (declarator);
23707 if (scope)
23709 /* Any names that appear after the declarator-id for a
23710 member are looked up in the containing scope. */
23711 if (at_function_scope_p ())
23713 /* But declarations with qualified-ids can't appear in a
23714 function. */
23715 cp_parser_error (parser, "qualified-id in declaration");
23716 declarator = cp_error_declarator;
23717 break;
23719 pushed_scope = push_scope (scope);
23721 parser->in_declarator_p = true;
23722 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
23723 || (declarator && declarator->kind == cdk_id))
23724 /* Default args are only allowed on function
23725 declarations. */
23726 parser->default_arg_ok_p = saved_default_arg_ok_p;
23727 else
23728 parser->default_arg_ok_p = false;
23730 first = false;
23732 /* We're done. */
23733 else
23734 break;
23737 /* For an abstract declarator, we might wind up with nothing at this
23738 point. That's an error; the declarator is not optional. */
23739 if (!declarator)
23740 cp_parser_error (parser, "expected declarator");
23741 else if (open_paren)
23743 /* Record overly parenthesized declarator so we can give a
23744 diagnostic about confusing decl/expr disambiguation. */
23745 if (declarator->kind == cdk_array)
23747 /* If the open and close parens are on different lines, this
23748 is probably a formatting thing, so ignore. */
23749 expanded_location open = expand_location (open_paren->location);
23750 expanded_location close = expand_location (close_paren->location);
23751 if (open.line != close.line || open.file != close.file)
23752 open_paren = NULL;
23754 if (open_paren)
23755 declarator->parenthesized = make_location (open_paren->location,
23756 open_paren->location,
23757 close_paren->location);
23760 /* If we entered a scope, we must exit it now. */
23761 if (pushed_scope)
23762 pop_scope (pushed_scope);
23764 parser->default_arg_ok_p = saved_default_arg_ok_p;
23765 parser->in_declarator_p = saved_in_declarator_p;
23767 return declarator;
23770 /* Parse a ptr-operator.
23772 ptr-operator:
23773 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
23774 * cv-qualifier-seq [opt]
23776 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
23777 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
23779 GNU Extension:
23781 ptr-operator:
23782 & cv-qualifier-seq [opt]
23784 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
23785 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
23786 an rvalue reference. In the case of a pointer-to-member, *TYPE is
23787 filled in with the TYPE containing the member. *CV_QUALS is
23788 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
23789 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
23790 Note that the tree codes returned by this function have nothing
23791 to do with the types of trees that will be eventually be created
23792 to represent the pointer or reference type being parsed. They are
23793 just constants with suggestive names. */
23794 static enum tree_code
23795 cp_parser_ptr_operator (cp_parser* parser,
23796 tree* type,
23797 cp_cv_quals *cv_quals,
23798 tree *attributes)
23800 enum tree_code code = ERROR_MARK;
23801 cp_token *token;
23802 tree attrs = NULL_TREE;
23804 /* Assume that it's not a pointer-to-member. */
23805 *type = NULL_TREE;
23806 /* And that there are no cv-qualifiers. */
23807 *cv_quals = TYPE_UNQUALIFIED;
23809 /* Peek at the next token. */
23810 token = cp_lexer_peek_token (parser->lexer);
23812 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
23813 if (token->type == CPP_MULT)
23814 code = INDIRECT_REF;
23815 else if (token->type == CPP_AND)
23816 code = ADDR_EXPR;
23817 else if ((cxx_dialect != cxx98) &&
23818 token->type == CPP_AND_AND) /* C++0x only */
23819 code = NON_LVALUE_EXPR;
23821 if (code != ERROR_MARK)
23823 /* Consume the `*', `&' or `&&'. */
23824 cp_lexer_consume_token (parser->lexer);
23826 /* A `*' can be followed by a cv-qualifier-seq, and so can a
23827 `&', if we are allowing GNU extensions. (The only qualifier
23828 that can legally appear after `&' is `restrict', but that is
23829 enforced during semantic analysis. */
23830 if (code == INDIRECT_REF
23831 || cp_parser_allow_gnu_extensions_p (parser))
23832 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
23834 attrs = cp_parser_std_attribute_spec_seq (parser);
23835 if (attributes != NULL)
23836 *attributes = attrs;
23838 else
23840 /* Try the pointer-to-member case. */
23841 cp_parser_parse_tentatively (parser);
23842 /* Look for the optional `::' operator. */
23843 cp_parser_global_scope_opt (parser,
23844 /*current_scope_valid_p=*/false);
23845 /* Look for the nested-name specifier. */
23846 token = cp_lexer_peek_token (parser->lexer);
23847 cp_parser_nested_name_specifier (parser,
23848 /*typename_keyword_p=*/false,
23849 /*check_dependency_p=*/true,
23850 /*type_p=*/false,
23851 /*is_declaration=*/false);
23852 /* If we found it, and the next token is a `*', then we are
23853 indeed looking at a pointer-to-member operator. */
23854 if (!cp_parser_error_occurred (parser)
23855 && cp_parser_require (parser, CPP_MULT, RT_MULT))
23857 /* Indicate that the `*' operator was used. */
23858 code = INDIRECT_REF;
23860 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
23861 error_at (token->location, "%qD is a namespace", parser->scope);
23862 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
23863 error_at (token->location, "cannot form pointer to member of "
23864 "non-class %q#T", parser->scope);
23865 else
23867 /* The type of which the member is a member is given by the
23868 current SCOPE. */
23869 *type = parser->scope;
23870 /* The next name will not be qualified. */
23871 parser->scope = NULL_TREE;
23872 parser->qualifying_scope = NULL_TREE;
23873 parser->object_scope = NULL_TREE;
23874 /* Look for optional c++11 attributes. */
23875 attrs = cp_parser_std_attribute_spec_seq (parser);
23876 if (attributes != NULL)
23877 *attributes = attrs;
23878 /* Look for the optional cv-qualifier-seq. */
23879 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
23882 /* If that didn't work we don't have a ptr-operator. */
23883 if (!cp_parser_parse_definitely (parser))
23884 cp_parser_error (parser, "expected ptr-operator");
23887 return code;
23890 /* Parse an (optional) cv-qualifier-seq.
23892 cv-qualifier-seq:
23893 cv-qualifier cv-qualifier-seq [opt]
23895 cv-qualifier:
23896 const
23897 volatile
23899 GNU Extension:
23901 cv-qualifier:
23902 __restrict__
23904 Returns a bitmask representing the cv-qualifiers. */
23906 static cp_cv_quals
23907 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
23909 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
23911 while (true)
23913 cp_token *token;
23914 cp_cv_quals cv_qualifier;
23916 /* Peek at the next token. */
23917 token = cp_lexer_peek_token (parser->lexer);
23918 /* See if it's a cv-qualifier. */
23919 switch (token->keyword)
23921 case RID_CONST:
23922 cv_qualifier = TYPE_QUAL_CONST;
23923 break;
23925 case RID_VOLATILE:
23926 cv_qualifier = TYPE_QUAL_VOLATILE;
23927 break;
23929 case RID_RESTRICT:
23930 cv_qualifier = TYPE_QUAL_RESTRICT;
23931 break;
23933 default:
23934 cv_qualifier = TYPE_UNQUALIFIED;
23935 break;
23938 if (!cv_qualifier)
23939 break;
23941 if (cv_quals & cv_qualifier)
23943 gcc_rich_location richloc (token->location);
23944 richloc.add_fixit_remove ();
23945 error_at (&richloc, "duplicate cv-qualifier");
23946 cp_lexer_purge_token (parser->lexer);
23948 else
23950 cp_lexer_consume_token (parser->lexer);
23951 cv_quals |= cv_qualifier;
23955 return cv_quals;
23958 /* Parse an (optional) ref-qualifier
23960 ref-qualifier:
23964 Returns cp_ref_qualifier representing ref-qualifier. */
23966 static cp_ref_qualifier
23967 cp_parser_ref_qualifier_opt (cp_parser* parser)
23969 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
23971 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
23972 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
23973 return ref_qual;
23975 while (true)
23977 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
23978 cp_token *token = cp_lexer_peek_token (parser->lexer);
23980 switch (token->type)
23982 case CPP_AND:
23983 curr_ref_qual = REF_QUAL_LVALUE;
23984 break;
23986 case CPP_AND_AND:
23987 curr_ref_qual = REF_QUAL_RVALUE;
23988 break;
23990 default:
23991 curr_ref_qual = REF_QUAL_NONE;
23992 break;
23995 if (!curr_ref_qual)
23996 break;
23997 else if (ref_qual)
23999 error_at (token->location, "multiple ref-qualifiers");
24000 cp_lexer_purge_token (parser->lexer);
24002 else
24004 ref_qual = curr_ref_qual;
24005 cp_lexer_consume_token (parser->lexer);
24009 return ref_qual;
24012 /* Parse an optional tx-qualifier.
24014 tx-qualifier:
24015 transaction_safe
24016 transaction_safe_dynamic */
24018 static tree
24019 cp_parser_tx_qualifier_opt (cp_parser *parser)
24021 cp_token *token = cp_lexer_peek_token (parser->lexer);
24022 if (token->type == CPP_NAME)
24024 tree name = token->u.value;
24025 const char *p = IDENTIFIER_POINTER (name);
24026 const int len = strlen ("transaction_safe");
24027 if (startswith (p, "transaction_safe"))
24029 p += len;
24030 if (*p == '\0'
24031 || !strcmp (p, "_dynamic"))
24033 cp_lexer_consume_token (parser->lexer);
24034 if (!flag_tm)
24036 error ("%qE requires %<-fgnu-tm%>", name);
24037 return NULL_TREE;
24039 else
24040 return name;
24044 return NULL_TREE;
24047 /* Parse an (optional) virt-specifier-seq.
24049 virt-specifier-seq:
24050 virt-specifier virt-specifier-seq [opt]
24052 virt-specifier:
24053 override
24054 final
24056 Returns a bitmask representing the virt-specifiers. */
24058 static cp_virt_specifiers
24059 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
24061 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
24063 while (true)
24065 cp_token *token;
24066 cp_virt_specifiers virt_specifier;
24068 /* Peek at the next token. */
24069 token = cp_lexer_peek_token (parser->lexer);
24070 /* See if it's a virt-specifier-qualifier. */
24071 if (token->type != CPP_NAME)
24072 break;
24073 if (id_equal (token->u.value, "override"))
24075 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
24076 virt_specifier = VIRT_SPEC_OVERRIDE;
24078 else if (id_equal (token->u.value, "final"))
24080 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
24081 virt_specifier = VIRT_SPEC_FINAL;
24083 else if (id_equal (token->u.value, "__final"))
24085 virt_specifier = VIRT_SPEC_FINAL;
24087 else
24088 break;
24090 if (virt_specifiers & virt_specifier)
24092 gcc_rich_location richloc (token->location);
24093 richloc.add_fixit_remove ();
24094 error_at (&richloc, "duplicate virt-specifier");
24095 cp_lexer_purge_token (parser->lexer);
24097 else
24099 cp_lexer_consume_token (parser->lexer);
24100 virt_specifiers |= virt_specifier;
24103 return virt_specifiers;
24106 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
24107 is in scope even though it isn't real. */
24109 void
24110 inject_this_parameter (tree ctype, cp_cv_quals quals)
24112 tree this_parm;
24114 if (current_class_ptr)
24116 /* We don't clear this between NSDMIs. Is it already what we want? */
24117 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
24118 if (DECL_P (current_class_ptr)
24119 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
24120 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
24121 && cp_type_quals (type) == quals)
24122 return;
24125 this_parm = build_this_parm (NULL_TREE, ctype, quals);
24126 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
24127 current_class_ptr = NULL_TREE;
24128 current_class_ref
24129 = cp_build_fold_indirect_ref (this_parm);
24130 current_class_ptr = this_parm;
24133 /* Return true iff our current scope is a non-static data member
24134 initializer. */
24136 bool
24137 parsing_nsdmi (void)
24139 /* We recognize NSDMI context by the context-less 'this' pointer set up
24140 by the function above. */
24141 if (current_class_ptr
24142 && TREE_CODE (current_class_ptr) == PARM_DECL
24143 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
24144 return true;
24145 return false;
24148 /* True if we're parsing a function declarator. */
24150 bool
24151 parsing_function_declarator ()
24153 /* this_entity is NULL for a function parameter scope while parsing the
24154 declarator; it is set when parsing the body of the function. */
24155 return (current_binding_level->kind == sk_function_parms
24156 && !current_binding_level->this_entity);
24159 /* Parse a late-specified return type, if any. This is not a separate
24160 non-terminal, but part of a function declarator, which looks like
24162 -> trailing-type-specifier-seq abstract-declarator(opt)
24164 Returns the type indicated by the type-id.
24166 In addition to this, parse any queued up #pragma omp declare simd
24167 clauses, and #pragma acc routine clauses.
24169 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
24170 function. */
24172 static tree
24173 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
24174 tree& requires_clause)
24176 cp_token *token;
24177 tree type = NULL_TREE;
24178 bool declare_simd_p = (parser->omp_declare_simd
24179 && declarator
24180 && declarator->kind == cdk_id);
24182 bool oacc_routine_p = (parser->oacc_routine
24183 && declarator
24184 && declarator->kind == cdk_id);
24186 /* Peek at the next token. */
24187 token = cp_lexer_peek_token (parser->lexer);
24188 /* A late-specified return type is indicated by an initial '->'. */
24189 if (token->type != CPP_DEREF
24190 && token->keyword != RID_REQUIRES
24191 && !(token->type == CPP_NAME
24192 && token->u.value == ridpointers[RID_REQUIRES])
24193 && !(declare_simd_p || oacc_routine_p))
24194 return NULL_TREE;
24196 if (token->type == CPP_DEREF)
24198 /* Consume the ->. */
24199 cp_lexer_consume_token (parser->lexer);
24201 type = cp_parser_trailing_type_id (parser);
24204 /* Function declarations may be followed by a trailing
24205 requires-clause. */
24206 requires_clause = cp_parser_requires_clause_opt (parser, false);
24208 if (declare_simd_p)
24209 declarator->attributes
24210 = cp_parser_late_parsing_omp_declare_simd (parser,
24211 declarator->attributes);
24212 if (oacc_routine_p)
24213 declarator->attributes
24214 = cp_parser_late_parsing_oacc_routine (parser,
24215 declarator->attributes);
24217 return type;
24220 /* Parse a declarator-id.
24222 declarator-id:
24223 id-expression
24224 :: [opt] nested-name-specifier [opt] type-name
24226 In the `id-expression' case, the value returned is as for
24227 cp_parser_id_expression if the id-expression was an unqualified-id.
24228 If the id-expression was a qualified-id, then a SCOPE_REF is
24229 returned. The first operand is the scope (either a NAMESPACE_DECL
24230 or TREE_TYPE), but the second is still just a representation of an
24231 unqualified-id. */
24233 static tree
24234 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
24236 tree id;
24237 /* The expression must be an id-expression. Assume that qualified
24238 names are the names of types so that:
24240 template <class T>
24241 int S<T>::R::i = 3;
24243 will work; we must treat `S<T>::R' as the name of a type.
24244 Similarly, assume that qualified names are templates, where
24245 required, so that:
24247 template <class T>
24248 int S<T>::R<T>::i = 3;
24250 will work, too. */
24251 id = cp_parser_id_expression (parser,
24252 /*template_keyword_p=*/false,
24253 /*check_dependency_p=*/false,
24254 /*template_p=*/NULL,
24255 /*declarator_p=*/true,
24256 optional_p);
24257 if (id && BASELINK_P (id))
24258 id = BASELINK_FUNCTIONS (id);
24259 return id;
24262 /* Parse a type-id.
24264 type-id:
24265 type-specifier-seq abstract-declarator [opt]
24267 The parser flags FLAGS is used to control type-specifier parsing.
24269 If IS_TEMPLATE_ARG is true, we are parsing a template argument.
24271 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
24272 i.e. we've just seen "->".
24274 Returns the TYPE specified. */
24276 static tree
24277 cp_parser_type_id_1 (cp_parser *parser, cp_parser_flags flags,
24278 bool is_template_arg, bool is_trailing_return,
24279 location_t *type_location)
24281 cp_decl_specifier_seq type_specifier_seq;
24282 cp_declarator *abstract_declarator;
24284 /* Parse the type-specifier-seq. */
24285 cp_parser_type_specifier_seq (parser, flags,
24286 /*is_declaration=*/false,
24287 is_trailing_return,
24288 &type_specifier_seq);
24289 if (type_location)
24290 *type_location = type_specifier_seq.locations[ds_type_spec];
24292 if (is_template_arg && type_specifier_seq.type
24293 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
24294 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
24295 /* A bare template name as a template argument is a template template
24296 argument, not a placeholder, so fail parsing it as a type argument. */
24298 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
24299 cp_parser_simulate_error (parser);
24300 return error_mark_node;
24302 if (type_specifier_seq.type == error_mark_node)
24303 return error_mark_node;
24305 /* There might or might not be an abstract declarator. */
24306 cp_parser_parse_tentatively (parser);
24307 /* Look for the declarator. */
24308 abstract_declarator
24309 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT,
24310 CP_PARSER_FLAGS_NONE, NULL,
24311 /*parenthesized_p=*/NULL,
24312 /*member_p=*/false,
24313 /*friend_p=*/false,
24314 /*static_p=*/false);
24315 /* Check to see if there really was a declarator. */
24316 if (!cp_parser_parse_definitely (parser))
24317 abstract_declarator = NULL;
24319 bool auto_typeid_ok = false;
24320 /* The concepts TS allows 'auto' as a type-id. */
24321 if (flag_concepts_ts)
24322 auto_typeid_ok = !parser->in_type_id_in_expr_p;
24323 /* DR 625 prohibits use of auto as a template-argument. We allow 'auto'
24324 outside the template-argument-list context here only for the sake of
24325 diagnostic: grokdeclarator then can emit a better error message for
24326 e.g. using T = auto. */
24327 else if (flag_concepts)
24328 auto_typeid_ok = (!parser->in_type_id_in_expr_p
24329 && !parser->in_template_argument_list_p);
24331 if (type_specifier_seq.type
24332 && !auto_typeid_ok
24333 /* None of the valid uses of 'auto' in C++14 involve the type-id
24334 nonterminal, but it is valid in a trailing-return-type. */
24335 && !(cxx_dialect >= cxx14 && is_trailing_return))
24336 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
24338 /* A type-id with type 'auto' is only ok if the abstract declarator
24339 is a function declarator with a late-specified return type.
24341 A type-id with 'auto' is also valid in a trailing-return-type
24342 in a compound-requirement. */
24343 if (abstract_declarator
24344 && abstract_declarator->kind == cdk_function
24345 && abstract_declarator->u.function.late_return_type)
24346 /* OK */;
24347 else if (parser->in_result_type_constraint_p)
24348 /* OK */;
24349 else
24351 if (!cp_parser_simulate_error (parser))
24353 location_t loc = type_specifier_seq.locations[ds_type_spec];
24354 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
24356 auto_diagnostic_group g;
24357 gcc_rich_location richloc (loc);
24358 richloc.add_fixit_insert_after ("<>");
24359 error_at (&richloc, "missing template arguments after %qE",
24360 tmpl);
24361 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
24362 tmpl);
24364 else if (parser->in_template_argument_list_p)
24365 error_at (loc, "%qT not permitted in template argument",
24366 auto_node);
24367 else
24368 error_at (loc, "invalid use of %qT", auto_node);
24370 return error_mark_node;
24374 return groktypename (&type_specifier_seq, abstract_declarator,
24375 is_template_arg);
24378 /* Wrapper for cp_parser_type_id_1. */
24380 static tree
24381 cp_parser_type_id (cp_parser *parser, cp_parser_flags flags,
24382 location_t *type_location)
24384 return cp_parser_type_id_1 (parser, flags, false, false, type_location);
24387 /* Wrapper for cp_parser_type_id_1. */
24389 static tree
24390 cp_parser_template_type_arg (cp_parser *parser)
24392 tree r;
24393 const char *saved_message = parser->type_definition_forbidden_message;
24394 parser->type_definition_forbidden_message
24395 = G_("types may not be defined in template arguments");
24396 r = cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_NONE, true, false, NULL);
24397 parser->type_definition_forbidden_message = saved_message;
24398 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
24400 error ("invalid use of %<auto%> in template argument");
24401 r = error_mark_node;
24403 return r;
24406 /* Wrapper for cp_parser_type_id_1. */
24408 static tree
24409 cp_parser_trailing_type_id (cp_parser *parser)
24411 return cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
24412 false, true, NULL);
24415 /* Parse a type-specifier-seq.
24417 type-specifier-seq:
24418 type-specifier type-specifier-seq [opt]
24420 GNU extension:
24422 type-specifier-seq:
24423 attributes type-specifier-seq [opt]
24425 The parser flags FLAGS is used to control type-specifier parsing.
24427 If IS_DECLARATION is true, we are at the start of a "condition" or
24428 exception-declaration, so we might be followed by a declarator-id.
24430 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
24431 i.e. we've just seen "->".
24433 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
24435 static void
24436 cp_parser_type_specifier_seq (cp_parser* parser,
24437 cp_parser_flags flags,
24438 bool is_declaration,
24439 bool is_trailing_return,
24440 cp_decl_specifier_seq *type_specifier_seq)
24442 bool seen_type_specifier = false;
24443 cp_token *start_token = NULL;
24445 /* Clear the TYPE_SPECIFIER_SEQ. */
24446 clear_decl_specs (type_specifier_seq);
24448 flags |= CP_PARSER_FLAGS_OPTIONAL;
24449 /* In the context of a trailing return type, enum E { } is an
24450 elaborated-type-specifier followed by a function-body, not an
24451 enum-specifier. */
24452 if (is_trailing_return)
24453 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
24455 /* Parse the type-specifiers and attributes. */
24456 while (true)
24458 tree type_specifier;
24459 bool is_cv_qualifier;
24461 /* Check for attributes first. */
24462 if (cp_next_tokens_can_be_attribute_p (parser))
24464 /* GNU attributes at the end of a declaration apply to the
24465 declaration as a whole, not to the trailing return type. So look
24466 ahead to see if these attributes are at the end. */
24467 if (seen_type_specifier && is_trailing_return
24468 && cp_next_tokens_can_be_gnu_attribute_p (parser))
24470 size_t n = cp_parser_skip_attributes_opt (parser, 1);
24471 cp_token *tok = cp_lexer_peek_nth_token (parser->lexer, n);
24472 if (tok->type == CPP_SEMICOLON || tok->type == CPP_COMMA
24473 || tok->type == CPP_EQ || tok->type == CPP_OPEN_BRACE)
24474 break;
24476 type_specifier_seq->attributes
24477 = attr_chainon (type_specifier_seq->attributes,
24478 cp_parser_attributes_opt (parser));
24479 continue;
24482 /* record the token of the beginning of the type specifier seq,
24483 for error reporting purposes*/
24484 if (!start_token)
24485 start_token = cp_lexer_peek_token (parser->lexer);
24487 /* Look for the type-specifier. */
24488 type_specifier = cp_parser_type_specifier (parser,
24489 flags,
24490 type_specifier_seq,
24491 /*is_declaration=*/false,
24492 NULL,
24493 &is_cv_qualifier);
24494 if (!type_specifier)
24496 /* If the first type-specifier could not be found, this is not a
24497 type-specifier-seq at all. */
24498 if (!seen_type_specifier)
24500 /* Set in_declarator_p to avoid skipping to the semicolon. */
24501 int in_decl = parser->in_declarator_p;
24502 parser->in_declarator_p = true;
24504 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
24505 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
24506 cp_parser_error (parser, "expected type-specifier");
24508 parser->in_declarator_p = in_decl;
24510 type_specifier_seq->type = error_mark_node;
24511 return;
24513 /* If subsequent type-specifiers could not be found, the
24514 type-specifier-seq is complete. */
24515 break;
24518 seen_type_specifier = true;
24519 /* The standard says that a condition can be:
24521 type-specifier-seq declarator = assignment-expression
24523 However, given:
24525 struct S {};
24526 if (int S = ...)
24528 we should treat the "S" as a declarator, not as a
24529 type-specifier. The standard doesn't say that explicitly for
24530 type-specifier-seq, but it does say that for
24531 decl-specifier-seq in an ordinary declaration. Perhaps it
24532 would be clearer just to allow a decl-specifier-seq here, and
24533 then add a semantic restriction that if any decl-specifiers
24534 that are not type-specifiers appear, the program is invalid. */
24535 if (is_declaration && !is_cv_qualifier)
24536 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
24540 /* Return whether the function currently being declared has an associated
24541 template parameter list. */
24543 static bool
24544 function_being_declared_is_template_p (cp_parser* parser)
24546 if (!current_template_parms || processing_template_parmlist)
24547 return false;
24549 if (parser->implicit_template_scope)
24550 return true;
24552 if (at_class_scope_p ()
24553 && TYPE_BEING_DEFINED (current_class_type))
24554 return parser->num_template_parameter_lists != 0;
24556 return ((int) parser->num_template_parameter_lists > template_class_depth
24557 (current_class_type));
24560 /* Parse a parameter-declaration-clause.
24562 parameter-declaration-clause:
24563 parameter-declaration-list [opt] ... [opt]
24564 parameter-declaration-list , ...
24566 The parser flags FLAGS is used to control type-specifier parsing.
24568 Returns a representation for the parameter declarations. A return
24569 value of NULL indicates a parameter-declaration-clause consisting
24570 only of an ellipsis. */
24572 static tree
24573 cp_parser_parameter_declaration_clause (cp_parser* parser,
24574 cp_parser_flags flags)
24576 tree parameters;
24577 cp_token *token;
24578 bool ellipsis_p;
24580 auto cleanup = make_temp_override
24581 (parser->auto_is_implicit_function_template_parm_p);
24583 if (!processing_specialization
24584 && !processing_template_parmlist
24585 && !processing_explicit_instantiation
24586 /* default_arg_ok_p tracks whether this is a parameter-clause for an
24587 actual function or a random abstract declarator. */
24588 && parser->default_arg_ok_p)
24589 if (!current_function_decl
24590 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
24591 parser->auto_is_implicit_function_template_parm_p = true;
24593 /* Peek at the next token. */
24594 token = cp_lexer_peek_token (parser->lexer);
24595 /* Check for trivial parameter-declaration-clauses. */
24596 if (token->type == CPP_ELLIPSIS)
24598 /* Consume the `...' token. */
24599 cp_lexer_consume_token (parser->lexer);
24600 return NULL_TREE;
24602 else if (token->type == CPP_CLOSE_PAREN)
24603 /* There are no parameters. */
24604 return void_list_node;
24605 /* Check for `(void)', too, which is a special case. */
24606 else if (token->keyword == RID_VOID
24607 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
24608 == CPP_CLOSE_PAREN))
24610 /* Consume the `void' token. */
24611 cp_lexer_consume_token (parser->lexer);
24612 /* There are no parameters. */
24613 return explicit_void_list_node;
24616 /* A vector of parameters that haven't been pushed yet. */
24617 auto_vec<tree> pending_decls;
24619 /* Parse the parameter-declaration-list. */
24620 parameters = cp_parser_parameter_declaration_list (parser, flags,
24621 &pending_decls);
24622 /* If a parse error occurred while parsing the
24623 parameter-declaration-list, then the entire
24624 parameter-declaration-clause is erroneous. */
24625 if (parameters == error_mark_node)
24626 return NULL_TREE;
24628 /* Peek at the next token. */
24629 token = cp_lexer_peek_token (parser->lexer);
24630 /* If it's a `,', the clause should terminate with an ellipsis. */
24631 if (token->type == CPP_COMMA)
24633 /* Consume the `,'. */
24634 cp_lexer_consume_token (parser->lexer);
24635 /* Expect an ellipsis. */
24636 ellipsis_p
24637 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
24639 /* It might also be `...' if the optional trailing `,' was
24640 omitted. */
24641 else if (token->type == CPP_ELLIPSIS)
24643 /* Consume the `...' token. */
24644 cp_lexer_consume_token (parser->lexer);
24645 /* And remember that we saw it. */
24646 ellipsis_p = true;
24648 else
24649 ellipsis_p = false;
24651 /* A valid parameter-declaration-clause can only be followed by a ')'.
24652 So it's time to push all the parameters we have seen now that we
24653 know we have a valid declaration. Note that here we may not have
24654 committed yet, nor should we. Pushing here will detect the error
24655 of redefining a parameter. */
24656 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24657 for (tree p : pending_decls)
24658 pushdecl (p);
24660 /* Finish the parameter list. */
24661 if (!ellipsis_p)
24662 parameters = chainon (parameters, void_list_node);
24664 return parameters;
24667 /* Parse a parameter-declaration-list.
24669 parameter-declaration-list:
24670 parameter-declaration
24671 parameter-declaration-list , parameter-declaration
24673 The parser flags FLAGS is used to control type-specifier parsing.
24674 PENDING_DECLS is a vector of parameters that haven't been pushed yet.
24676 Returns a representation of the parameter-declaration-list, as for
24677 cp_parser_parameter_declaration_clause. However, the
24678 `void_list_node' is never appended to the list. */
24680 static tree
24681 cp_parser_parameter_declaration_list (cp_parser* parser,
24682 cp_parser_flags flags,
24683 auto_vec<tree> *pending_decls)
24685 tree parameters = NULL_TREE;
24686 tree *tail = &parameters;
24687 bool saved_in_unbraced_linkage_specification_p;
24688 int index = 0;
24690 /* The special considerations that apply to a function within an
24691 unbraced linkage specifications do not apply to the parameters
24692 to the function. */
24693 saved_in_unbraced_linkage_specification_p
24694 = parser->in_unbraced_linkage_specification_p;
24695 parser->in_unbraced_linkage_specification_p = false;
24697 /* Look for more parameters. */
24698 while (true)
24700 cp_parameter_declarator *parameter;
24701 tree decl = error_mark_node;
24702 bool parenthesized_p = false;
24704 /* Parse the parameter. */
24705 parameter
24706 = cp_parser_parameter_declaration (parser, flags,
24707 /*template_parm_p=*/false,
24708 &parenthesized_p);
24710 /* We don't know yet if the enclosing context is unavailable or deprecated,
24711 so wait and deal with it in grokparms if appropriate. */
24712 deprecated_state = UNAVAILABLE_DEPRECATED_SUPPRESS;
24714 if (parameter && !cp_parser_error_occurred (parser))
24716 decl = grokdeclarator (parameter->declarator,
24717 &parameter->decl_specifiers,
24718 PARM,
24719 parameter->default_argument != NULL_TREE,
24720 &parameter->decl_specifiers.attributes);
24721 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
24722 DECL_SOURCE_LOCATION (decl) = parameter->loc;
24725 deprecated_state = DEPRECATED_NORMAL;
24727 /* If a parse error occurred parsing the parameter declaration,
24728 then the entire parameter-declaration-list is erroneous. */
24729 if (decl == error_mark_node)
24731 parameters = error_mark_node;
24732 break;
24735 if (parameter->decl_specifiers.attributes)
24736 cplus_decl_attributes (&decl,
24737 parameter->decl_specifiers.attributes,
24739 if (DECL_NAME (decl))
24741 /* We cannot always pushdecl while parsing tentatively because
24742 it may have side effects and we can't be sure yet if we're
24743 parsing a declaration, e.g.:
24745 S foo(int(x), int(x), int{x});
24747 where it's not clear if we're dealing with a constructor call
24748 or a function declaration until we've seen the last argument
24749 which breaks it up.
24750 It's safe to pushdecl so long as it doesn't result in a clash
24751 with an already-pushed parameter. But we don't delay pushing
24752 different parameters to handle
24754 S foo(int(i), decltype(i) j = 42);
24756 which is valid. */
24757 if (pending_decls
24758 && cp_parser_uncommitted_to_tentative_parse_p (parser)
24759 /* See if PARAMETERS already contains a parameter with the same
24760 DECL_NAME as DECL. */
24761 && [parameters, decl] {
24762 for (tree p = parameters; p; p = TREE_CHAIN (p))
24763 if (DECL_NAME (decl) == DECL_NAME (TREE_VALUE (p)))
24764 return true;
24765 return false;
24766 }())
24767 pending_decls->safe_push (decl);
24768 else
24769 decl = pushdecl (decl);
24772 if (decl != error_mark_node)
24774 retrofit_lang_decl (decl);
24775 DECL_PARM_INDEX (decl) = ++index;
24776 DECL_PARM_LEVEL (decl) = function_parm_depth ();
24779 /* Add the new parameter to the list. */
24780 *tail = build_tree_list (parameter->default_argument, decl);
24781 tail = &TREE_CHAIN (*tail);
24783 /* If the parameters were parenthesized, it's the case of
24784 T foo(X(x)) which looks like a variable definition but
24785 is a function declaration. */
24786 if (index == 1 || PARENTHESIZED_LIST_P (parameters))
24787 PARENTHESIZED_LIST_P (parameters) = parenthesized_p;
24789 /* Peek at the next token. */
24790 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
24791 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
24792 /* These are for Objective-C++ */
24793 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
24794 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24795 /* The parameter-declaration-list is complete. */
24796 break;
24797 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24799 cp_token *token;
24801 /* Peek at the next token. */
24802 token = cp_lexer_peek_nth_token (parser->lexer, 2);
24803 /* If it's an ellipsis, then the list is complete. */
24804 if (token->type == CPP_ELLIPSIS)
24805 break;
24806 /* Otherwise, there must be more parameters. Consume the
24807 `,'. */
24808 cp_lexer_consume_token (parser->lexer);
24809 /* When parsing something like:
24811 int i(float f, double d)
24813 we can tell after seeing the declaration for "f" that we
24814 are not looking at an initialization of a variable "i",
24815 but rather at the declaration of a function "i".
24817 Due to the fact that the parsing of template arguments
24818 (as specified to a template-id) requires backtracking we
24819 cannot use this technique when inside a template argument
24820 list. */
24821 if (!parser->in_template_argument_list_p
24822 && !parser->in_type_id_in_expr_p
24823 && cp_parser_uncommitted_to_tentative_parse_p (parser)
24824 /* However, a parameter-declaration of the form
24825 "float(f)" (which is a valid declaration of a
24826 parameter "f") can also be interpreted as an
24827 expression (the conversion of "f" to "float"). */
24828 && !parenthesized_p)
24829 cp_parser_commit_to_tentative_parse (parser);
24831 else
24833 cp_parser_error (parser, "expected %<,%> or %<...%>");
24834 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
24835 cp_parser_skip_to_closing_parenthesis (parser,
24836 /*recovering=*/true,
24837 /*or_comma=*/false,
24838 /*consume_paren=*/false);
24839 break;
24843 parser->in_unbraced_linkage_specification_p
24844 = saved_in_unbraced_linkage_specification_p;
24846 /* Reset implicit_template_scope if we are about to leave the function
24847 parameter list that introduced it. Note that for out-of-line member
24848 definitions, there will be one or more class scopes before we get to
24849 the template parameter scope. */
24851 if (cp_binding_level *its = parser->implicit_template_scope)
24852 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
24854 while (maybe_its->kind == sk_class)
24855 maybe_its = maybe_its->level_chain;
24856 if (maybe_its == its)
24858 parser->implicit_template_parms = 0;
24859 parser->implicit_template_scope = 0;
24863 return parameters;
24866 /* Parse a parameter declaration.
24868 parameter-declaration:
24869 decl-specifier-seq ... [opt] declarator
24870 decl-specifier-seq declarator = assignment-expression
24871 decl-specifier-seq ... [opt] abstract-declarator [opt]
24872 decl-specifier-seq abstract-declarator [opt] = assignment-expression
24874 The parser flags FLAGS is used to control type-specifier parsing.
24876 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
24877 declares a template parameter. (In that case, a non-nested `>'
24878 token encountered during the parsing of the assignment-expression
24879 is not interpreted as a greater-than operator.)
24881 Returns a representation of the parameter, or NULL if an error
24882 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
24883 true iff the declarator is of the form "(p)". */
24885 static cp_parameter_declarator *
24886 cp_parser_parameter_declaration (cp_parser *parser,
24887 cp_parser_flags flags,
24888 bool template_parm_p,
24889 bool *parenthesized_p)
24891 int declares_class_or_enum;
24892 cp_decl_specifier_seq decl_specifiers;
24893 cp_declarator *declarator;
24894 tree default_argument;
24895 cp_token *token = NULL, *declarator_token_start = NULL;
24896 const char *saved_message;
24897 bool template_parameter_pack_p = false;
24899 /* In a template parameter, `>' is not an operator.
24901 [temp.param]
24903 When parsing a default template-argument for a non-type
24904 template-parameter, the first non-nested `>' is taken as the end
24905 of the template parameter-list rather than a greater-than
24906 operator. */
24908 /* Type definitions may not appear in parameter types. */
24909 saved_message = parser->type_definition_forbidden_message;
24910 parser->type_definition_forbidden_message
24911 = G_("types may not be defined in parameter types");
24913 int template_parm_idx = (function_being_declared_is_template_p (parser) ?
24914 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
24915 (current_template_parms)) : 0);
24917 /* Parse the declaration-specifiers. */
24918 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
24919 cp_parser_decl_specifier_seq (parser,
24920 flags,
24921 &decl_specifiers,
24922 &declares_class_or_enum);
24924 /* [dcl.spec.auto.general]: "A placeholder-type-specifier of the form
24925 type-constraint opt auto can be used as a decl-specifier of the
24926 decl-specifier-seq of a parameter-declaration of a function declaration
24927 or lambda-expression..." but we must not synthesize an implicit template
24928 type parameter in its declarator. That is, in "void f(auto[auto{10}]);"
24929 we want to synthesize only the first auto. */
24930 auto cleanup = make_temp_override
24931 (parser->auto_is_implicit_function_template_parm_p, false);
24933 /* Complain about missing 'typename' or other invalid type names. */
24934 if (!decl_specifiers.any_type_specifiers_p
24935 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
24936 decl_specifiers.type = error_mark_node;
24938 /* If an error occurred, there's no reason to attempt to parse the
24939 rest of the declaration. */
24940 if (cp_parser_error_occurred (parser))
24942 parser->type_definition_forbidden_message = saved_message;
24943 return NULL;
24946 /* Peek at the next token. */
24947 token = cp_lexer_peek_token (parser->lexer);
24949 /* If the next token is a `)', `,', `=', `>', or `...', then there
24950 is no declarator. However, when variadic templates are enabled,
24951 there may be a declarator following `...'. */
24952 if (token->type == CPP_CLOSE_PAREN
24953 || token->type == CPP_COMMA
24954 || token->type == CPP_EQ
24955 || token->type == CPP_GREATER)
24957 declarator = NULL;
24958 if (parenthesized_p)
24959 *parenthesized_p = false;
24961 /* Otherwise, there should be a declarator. */
24962 else
24964 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
24965 parser->default_arg_ok_p = false;
24967 /* After seeing a decl-specifier-seq, if the next token is not a
24968 "(" or "{", there is no possibility that the code is a valid
24969 expression. Therefore, if parsing tentatively, we commit at
24970 this point. */
24971 if (!parser->in_template_argument_list_p
24972 /* In an expression context, having seen:
24974 (int((char ...
24976 we cannot be sure whether we are looking at a
24977 function-type (taking a "char" as a parameter) or a cast
24978 of some object of type "char" to "int". */
24979 && !parser->in_type_id_in_expr_p
24980 && cp_parser_uncommitted_to_tentative_parse_p (parser)
24981 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
24983 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24985 if (decl_specifiers.type
24986 && template_placeholder_p (decl_specifiers.type))
24987 /* This is a CTAD expression, not a parameter declaration. */
24988 cp_parser_simulate_error (parser);
24990 else
24991 cp_parser_commit_to_tentative_parse (parser);
24993 /* Parse the declarator. */
24994 declarator_token_start = token;
24995 declarator = cp_parser_declarator (parser,
24996 CP_PARSER_DECLARATOR_EITHER,
24997 CP_PARSER_FLAGS_NONE,
24998 /*ctor_dtor_or_conv_p=*/NULL,
24999 parenthesized_p,
25000 /*member_p=*/false,
25001 /*friend_p=*/false,
25002 /*static_p=*/false);
25003 parser->default_arg_ok_p = saved_default_arg_ok_p;
25004 /* After the declarator, allow more attributes. */
25005 decl_specifiers.attributes
25006 = attr_chainon (decl_specifiers.attributes,
25007 cp_parser_attributes_opt (parser));
25009 /* If the declarator is a template parameter pack, remember that and
25010 clear the flag in the declarator itself so we don't get errors
25011 from grokdeclarator. */
25012 if (template_parm_p && declarator && declarator->parameter_pack_p)
25014 declarator->parameter_pack_p = false;
25015 template_parameter_pack_p = true;
25019 /* If the next token is an ellipsis, and we have not seen a declarator
25020 name, and if either the type of the declarator contains parameter
25021 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
25022 for, eg, abbreviated integral type names), then we actually have a
25023 parameter pack expansion expression. Otherwise, leave the ellipsis
25024 for a C-style variadic function. */
25025 token = cp_lexer_peek_token (parser->lexer);
25027 /* If a function parameter pack was specified and an implicit template
25028 parameter was introduced during cp_parser_parameter_declaration,
25029 change any implicit parameters introduced into packs. */
25030 if (parser->implicit_template_parms
25031 && ((token->type == CPP_ELLIPSIS
25032 && declarator_can_be_parameter_pack (declarator))
25033 || (declarator && declarator->parameter_pack_p)))
25035 int latest_template_parm_idx = TREE_VEC_LENGTH
25036 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
25038 if (latest_template_parm_idx != template_parm_idx)
25039 decl_specifiers.type = convert_generic_types_to_packs
25040 (decl_specifiers.type,
25041 template_parm_idx, latest_template_parm_idx);
25044 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25046 tree type = decl_specifiers.type;
25048 if (type && DECL_P (type))
25049 type = TREE_TYPE (type);
25051 if (((type
25052 && TREE_CODE (type) != TYPE_PACK_EXPANSION
25053 && (template_parm_p || uses_parameter_packs (type)))
25054 || (!type && template_parm_p))
25055 && declarator_can_be_parameter_pack (declarator))
25057 /* Consume the `...'. */
25058 cp_lexer_consume_token (parser->lexer);
25059 maybe_warn_variadic_templates ();
25061 /* Build a pack expansion type */
25062 if (template_parm_p)
25063 template_parameter_pack_p = true;
25064 else if (declarator)
25065 declarator->parameter_pack_p = true;
25066 else
25067 decl_specifiers.type = make_pack_expansion (type);
25071 /* The restriction on defining new types applies only to the type
25072 of the parameter, not to the default argument. */
25073 parser->type_definition_forbidden_message = saved_message;
25075 /* If the next token is `=', then process a default argument. */
25076 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
25078 tree type = decl_specifiers.type;
25079 token = cp_lexer_peek_token (parser->lexer);
25080 if (declarator)
25081 declarator->init_loc = token->location;
25082 /* If we are defining a class, then the tokens that make up the
25083 default argument must be saved and processed later. */
25084 if (!template_parm_p && at_class_scope_p ()
25085 && TYPE_BEING_DEFINED (current_class_type)
25086 && !LAMBDA_TYPE_P (current_class_type))
25087 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
25089 /* A constrained-type-specifier may declare a type
25090 template-parameter. */
25091 else if (declares_constrained_type_template_parameter (type))
25092 default_argument
25093 = cp_parser_default_type_template_argument (parser);
25095 /* A constrained-type-specifier may declare a
25096 template-template-parameter. */
25097 else if (declares_constrained_template_template_parameter (type))
25098 default_argument
25099 = cp_parser_default_template_template_argument (parser);
25101 /* Outside of a class definition, we can just parse the
25102 assignment-expression. */
25103 else
25104 default_argument
25105 = cp_parser_default_argument (parser, template_parm_p);
25107 if (!parser->default_arg_ok_p)
25109 permerror (token->location,
25110 "default arguments are only "
25111 "permitted for function parameters");
25113 else if ((declarator && declarator->parameter_pack_p)
25114 || template_parameter_pack_p
25115 || (decl_specifiers.type
25116 && PACK_EXPANSION_P (decl_specifiers.type)))
25118 /* Find the name of the parameter pack. */
25119 cp_declarator *id_declarator = declarator;
25120 while (id_declarator && id_declarator->kind != cdk_id)
25121 id_declarator = id_declarator->declarator;
25123 if (id_declarator && id_declarator->kind == cdk_id)
25124 error_at (declarator_token_start->location,
25125 template_parm_p
25126 ? G_("template parameter pack %qD "
25127 "cannot have a default argument")
25128 : G_("parameter pack %qD cannot have "
25129 "a default argument"),
25130 id_declarator->u.id.unqualified_name);
25131 else
25132 error_at (declarator_token_start->location,
25133 template_parm_p
25134 ? G_("template parameter pack cannot have "
25135 "a default argument")
25136 : G_("parameter pack cannot have a "
25137 "default argument"));
25139 default_argument = NULL_TREE;
25142 else
25143 default_argument = NULL_TREE;
25145 if (default_argument)
25146 STRIP_ANY_LOCATION_WRAPPER (default_argument);
25148 /* Generate a location for the parameter, ranging from the start of the
25149 initial token to the end of the final token (using input_location for
25150 the latter, set up by cp_lexer_set_source_position_from_token when
25151 consuming tokens).
25153 If we have a identifier, then use it for the caret location, e.g.
25155 extern int callee (int one, int (*two)(int, int), float three);
25156 ~~~~~~^~~~~~~~~~~~~~
25158 otherwise, reuse the start location for the caret location e.g.:
25160 extern int callee (int one, int (*)(int, int), float three);
25161 ^~~~~~~~~~~~~~~~~
25164 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
25165 ? declarator->id_loc
25166 : decl_spec_token_start->location);
25167 location_t param_loc = make_location (caret_loc,
25168 decl_spec_token_start->location,
25169 input_location);
25171 return make_parameter_declarator (&decl_specifiers,
25172 declarator,
25173 default_argument,
25174 param_loc,
25175 template_parameter_pack_p);
25178 /* Parse a default argument and return it.
25180 TEMPLATE_PARM_P is true if this is a default argument for a
25181 non-type template parameter. */
25182 static tree
25183 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
25185 tree default_argument = NULL_TREE;
25186 bool saved_greater_than_is_operator_p;
25187 unsigned char saved_local_variables_forbidden_p;
25188 bool non_constant_p, is_direct_init;
25190 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
25191 set correctly. */
25192 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
25193 parser->greater_than_is_operator_p = !template_parm_p;
25194 auto odsd = make_temp_override (parser->omp_declare_simd, NULL);
25195 auto ord = make_temp_override (parser->oacc_routine, NULL);
25196 auto oafp = make_temp_override (parser->omp_attrs_forbidden_p, false);
25198 /* Local variable names (and the `this' keyword) may not
25199 appear in a default argument. */
25200 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
25201 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
25202 /* Parse the assignment-expression. */
25203 if (template_parm_p)
25204 push_deferring_access_checks (dk_no_deferred);
25205 tree saved_class_ptr = NULL_TREE;
25206 tree saved_class_ref = NULL_TREE;
25207 /* The "this" pointer is not valid in a default argument. */
25208 if (cfun)
25210 saved_class_ptr = current_class_ptr;
25211 cp_function_chain->x_current_class_ptr = NULL_TREE;
25212 saved_class_ref = current_class_ref;
25213 cp_function_chain->x_current_class_ref = NULL_TREE;
25215 default_argument
25216 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
25217 /* Restore the "this" pointer. */
25218 if (cfun)
25220 cp_function_chain->x_current_class_ptr = saved_class_ptr;
25221 cp_function_chain->x_current_class_ref = saved_class_ref;
25223 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
25224 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
25225 if (template_parm_p)
25226 pop_deferring_access_checks ();
25227 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
25228 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
25230 return default_argument;
25233 /* Parse a function-body.
25235 function-body:
25236 compound_statement */
25238 static void
25239 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
25241 cp_parser_compound_statement (parser, NULL, (in_function_try_block
25242 ? BCS_TRY_BLOCK : BCS_NORMAL),
25243 true);
25246 /* Parse a ctor-initializer-opt followed by a function-body. Return
25247 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
25248 is true we are parsing a function-try-block. */
25250 static void
25251 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
25252 bool in_function_try_block)
25254 tree body, list;
25255 const bool check_body_p
25256 = (DECL_CONSTRUCTOR_P (current_function_decl)
25257 && DECL_DECLARED_CONSTEXPR_P (current_function_decl));
25258 tree last = NULL;
25260 if (in_function_try_block
25261 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
25262 && cxx_dialect < cxx20)
25264 if (DECL_CONSTRUCTOR_P (current_function_decl))
25265 pedwarn (input_location, OPT_Wc__20_extensions,
25266 "function-try-block body of %<constexpr%> constructor only "
25267 "available with %<-std=c++20%> or %<-std=gnu++20%>");
25268 else
25269 pedwarn (input_location, OPT_Wc__20_extensions,
25270 "function-try-block body of %<constexpr%> function only "
25271 "available with %<-std=c++20%> or %<-std=gnu++20%>");
25274 /* Begin the function body. */
25275 body = begin_function_body ();
25276 /* Parse the optional ctor-initializer. */
25277 cp_parser_ctor_initializer_opt (parser);
25279 /* If we're parsing a constexpr constructor definition, we need
25280 to check that the constructor body is indeed empty. However,
25281 before we get to cp_parser_function_body lot of junk has been
25282 generated, so we can't just check that we have an empty block.
25283 Rather we take a snapshot of the outermost block, and check whether
25284 cp_parser_function_body changed its state. */
25285 if (check_body_p)
25287 list = cur_stmt_list;
25288 if (STATEMENT_LIST_TAIL (list))
25289 last = STATEMENT_LIST_TAIL (list)->stmt;
25291 /* Parse the function-body. */
25292 cp_parser_function_body (parser, in_function_try_block);
25293 if (check_body_p)
25294 check_constexpr_ctor_body (last, list, /*complain=*/true);
25295 /* Finish the function body. */
25296 finish_function_body (body);
25299 /* Parse an initializer.
25301 initializer:
25302 = initializer-clause
25303 ( expression-list )
25305 Returns an expression representing the initializer. If no
25306 initializer is present, NULL_TREE is returned.
25308 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
25309 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
25310 set to TRUE if there is no initializer present. If there is an
25311 initializer, and it is not a constant-expression, *NON_CONSTANT_P
25312 is set to true; otherwise it is set to false. */
25314 static tree
25315 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
25316 bool* non_constant_p, bool subexpression_p)
25318 cp_token *token;
25319 tree init;
25321 /* Peek at the next token. */
25322 token = cp_lexer_peek_token (parser->lexer);
25324 /* Let our caller know whether or not this initializer was
25325 parenthesized. */
25326 *is_direct_init = (token->type != CPP_EQ);
25327 /* Assume that the initializer is constant. */
25328 *non_constant_p = false;
25330 if (token->type == CPP_EQ)
25332 /* Consume the `='. */
25333 cp_lexer_consume_token (parser->lexer);
25334 /* Parse the initializer-clause. */
25335 init = cp_parser_initializer_clause (parser, non_constant_p);
25337 else if (token->type == CPP_OPEN_PAREN)
25339 vec<tree, va_gc> *vec;
25340 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
25341 /*cast_p=*/false,
25342 /*allow_expansion_p=*/true,
25343 non_constant_p);
25344 if (vec == NULL)
25345 return error_mark_node;
25346 init = build_tree_list_vec (vec);
25347 release_tree_vector (vec);
25349 else if (token->type == CPP_OPEN_BRACE)
25351 cp_lexer_set_source_position (parser->lexer);
25352 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
25353 init = cp_parser_braced_list (parser, non_constant_p);
25354 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
25356 else
25358 /* Anything else is an error. */
25359 cp_parser_error (parser, "expected initializer");
25360 init = error_mark_node;
25363 if (!subexpression_p && check_for_bare_parameter_packs (init))
25364 init = error_mark_node;
25366 return init;
25369 /* Parse an initializer-clause.
25371 initializer-clause:
25372 assignment-expression
25373 braced-init-list
25375 Returns an expression representing the initializer.
25377 If the `assignment-expression' production is used the value
25378 returned is simply a representation for the expression.
25380 Otherwise, calls cp_parser_braced_list. */
25382 static cp_expr
25383 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
25385 cp_expr initializer;
25387 /* Assume the expression is constant. */
25388 *non_constant_p = false;
25390 /* If it is not a `{', then we are looking at an
25391 assignment-expression. */
25392 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
25394 initializer
25395 = cp_parser_constant_expression (parser,
25396 /*allow_non_constant_p=*/2,
25397 non_constant_p);
25399 else
25400 initializer = cp_parser_braced_list (parser, non_constant_p);
25402 return initializer;
25405 /* Parse a brace-enclosed initializer list.
25407 braced-init-list:
25408 { initializer-list , [opt] }
25409 { designated-initializer-list , [opt] }
25412 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
25413 the elements of the initializer-list (or NULL, if the last
25414 production is used). The TREE_TYPE for the CONSTRUCTOR will be
25415 NULL_TREE. There is no way to detect whether or not the optional
25416 trailing `,' was provided. NON_CONSTANT_P is as for
25417 cp_parser_initializer. */
25419 static cp_expr
25420 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
25422 tree initializer;
25423 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
25425 /* Consume the `{' token. */
25426 matching_braces braces;
25427 braces.require_open (parser);
25428 /* Create a CONSTRUCTOR to represent the braced-initializer. */
25429 initializer = make_node (CONSTRUCTOR);
25430 /* If it's not a `}', then there is a non-trivial initializer. */
25431 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
25433 bool designated;
25434 /* Parse the initializer list. */
25435 CONSTRUCTOR_ELTS (initializer)
25436 = cp_parser_initializer_list (parser, non_constant_p, &designated);
25437 CONSTRUCTOR_IS_DESIGNATED_INIT (initializer) = designated;
25438 /* A trailing `,' token is allowed. */
25439 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25440 cp_lexer_consume_token (parser->lexer);
25442 else
25443 *non_constant_p = false;
25444 /* Now, there should be a trailing `}'. */
25445 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
25446 braces.require_close (parser);
25447 TREE_TYPE (initializer) = init_list_type_node;
25448 recompute_constructor_flags (initializer);
25450 cp_expr result (initializer);
25451 /* Build a location of the form:
25452 { ... }
25453 ^~~~~~~
25454 with caret==start at the open brace, finish at the close brace. */
25455 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
25456 result.set_location (combined_loc);
25457 return result;
25460 /* Consume tokens up to, but not including, the next non-nested closing `]'.
25461 Returns true iff we found a closing `]'. */
25463 static bool
25464 cp_parser_skip_up_to_closing_square_bracket (cp_parser *parser)
25466 unsigned square_depth = 0;
25468 while (true)
25470 cp_token * token = cp_lexer_peek_token (parser->lexer);
25472 switch (token->type)
25474 case CPP_PRAGMA_EOL:
25475 if (!parser->lexer->in_pragma)
25476 break;
25477 /* FALLTHRU */
25479 case CPP_EOF:
25480 /* If we've run out of tokens, then there is no closing `]'. */
25481 return false;
25483 case CPP_OPEN_SQUARE:
25484 ++square_depth;
25485 break;
25487 case CPP_CLOSE_SQUARE:
25488 if (!square_depth--)
25489 return true;
25490 break;
25492 default:
25493 break;
25496 /* Consume the current token, skipping it. */
25497 cp_lexer_consume_token (parser->lexer);
25501 /* Consume tokens up to, and including, the next non-nested closing `]'.
25502 Returns true iff we found a closing `]'. */
25504 static bool
25505 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
25507 bool found = cp_parser_skip_up_to_closing_square_bracket (parser);
25508 if (found)
25509 cp_lexer_consume_token (parser->lexer);
25510 return found;
25513 /* Return true if we are looking at an array-designator, false otherwise. */
25515 static bool
25516 cp_parser_array_designator_p (cp_parser *parser)
25518 /* Consume the `['. */
25519 cp_lexer_consume_token (parser->lexer);
25521 cp_lexer_save_tokens (parser->lexer);
25523 /* Skip tokens until the next token is a closing square bracket.
25524 If we find the closing `]', and the next token is a `=', then
25525 we are looking at an array designator. */
25526 bool array_designator_p
25527 = (cp_parser_skip_to_closing_square_bracket (parser)
25528 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
25530 /* Roll back the tokens we skipped. */
25531 cp_lexer_rollback_tokens (parser->lexer);
25533 return array_designator_p;
25536 /* Parse an initializer-list.
25538 initializer-list:
25539 initializer-clause ... [opt]
25540 initializer-list , initializer-clause ... [opt]
25542 C++20 Extension:
25544 designated-initializer-list:
25545 designated-initializer-clause
25546 designated-initializer-list , designated-initializer-clause
25548 designated-initializer-clause:
25549 designator brace-or-equal-initializer
25551 designator:
25552 . identifier
25554 GNU Extension:
25556 initializer-list:
25557 designation initializer-clause ...[opt]
25558 initializer-list , designation initializer-clause ...[opt]
25560 designation:
25561 . identifier =
25562 identifier :
25563 [ constant-expression ] =
25565 Returns a vec of constructor_elt. The VALUE of each elt is an expression
25566 for the initializer. If the INDEX of the elt is non-NULL, it is the
25567 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
25568 as for cp_parser_initializer. Set *DESIGNATED to a boolean whether there
25569 are any designators. */
25571 static vec<constructor_elt, va_gc> *
25572 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p,
25573 bool *designated)
25575 vec<constructor_elt, va_gc> *v = NULL;
25576 bool first_p = true;
25577 tree first_designator = NULL_TREE;
25579 /* Assume all of the expressions are constant. */
25580 *non_constant_p = false;
25582 unsigned nelts = 0;
25583 int suppress = suppress_location_wrappers;
25585 /* Parse the rest of the list. */
25586 while (true)
25588 cp_token *token;
25589 tree designator;
25590 tree initializer;
25591 bool clause_non_constant_p;
25592 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25594 /* Handle the C++20 syntax, '. id ='. */
25595 if ((cxx_dialect >= cxx20
25596 || cp_parser_allow_gnu_extensions_p (parser))
25597 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
25598 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
25599 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
25600 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
25601 == CPP_OPEN_BRACE)))
25603 if (pedantic && cxx_dialect < cxx20)
25604 pedwarn (loc, OPT_Wc__20_extensions,
25605 "C++ designated initializers only available with "
25606 "%<-std=c++20%> or %<-std=gnu++20%>");
25607 /* Consume the `.'. */
25608 cp_lexer_consume_token (parser->lexer);
25609 /* Consume the identifier. */
25610 designator = cp_lexer_consume_token (parser->lexer)->u.value;
25611 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
25612 /* Consume the `='. */
25613 cp_lexer_consume_token (parser->lexer);
25615 /* Also, if the next token is an identifier and the following one is a
25616 colon, we are looking at the GNU designated-initializer
25617 syntax. */
25618 else if (cp_parser_allow_gnu_extensions_p (parser)
25619 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
25620 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
25621 == CPP_COLON))
25623 /* Warn the user that they are using an extension. */
25624 pedwarn (loc, OPT_Wpedantic,
25625 "ISO C++ does not allow GNU designated initializers");
25626 /* Consume the identifier. */
25627 designator = cp_lexer_consume_token (parser->lexer)->u.value;
25628 /* Consume the `:'. */
25629 cp_lexer_consume_token (parser->lexer);
25631 /* Also handle C99 array designators, '[ const ] ='. */
25632 else if (cp_parser_allow_gnu_extensions_p (parser)
25633 && !c_dialect_objc ()
25634 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
25636 /* In C++11, [ could start a lambda-introducer. */
25637 bool non_const = false;
25639 cp_parser_parse_tentatively (parser);
25641 if (!cp_parser_array_designator_p (parser))
25643 cp_parser_simulate_error (parser);
25644 designator = NULL_TREE;
25646 else
25648 designator = cp_parser_constant_expression (parser, true,
25649 &non_const);
25650 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25651 cp_parser_require (parser, CPP_EQ, RT_EQ);
25654 if (!cp_parser_parse_definitely (parser))
25655 designator = NULL_TREE;
25656 else if (non_const
25657 && (!require_potential_rvalue_constant_expression
25658 (designator)))
25659 designator = NULL_TREE;
25660 if (designator)
25661 /* Warn the user that they are using an extension. */
25662 pedwarn (loc, OPT_Wpedantic,
25663 "ISO C++ does not allow C99 designated initializers");
25665 else
25666 designator = NULL_TREE;
25668 if (first_p)
25670 first_designator = designator;
25671 first_p = false;
25673 else if (cxx_dialect >= cxx20
25674 && first_designator != error_mark_node
25675 && (!first_designator != !designator))
25677 error_at (loc, "either all initializer clauses should be designated "
25678 "or none of them should be");
25679 first_designator = error_mark_node;
25681 else if (cxx_dialect < cxx20 && !first_designator)
25682 first_designator = designator;
25684 /* Parse the initializer. */
25685 initializer = cp_parser_initializer_clause (parser,
25686 &clause_non_constant_p);
25687 /* If any clause is non-constant, so is the entire initializer. */
25688 if (clause_non_constant_p)
25689 *non_constant_p = true;
25691 /* If we have an ellipsis, this is an initializer pack
25692 expansion. */
25693 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25695 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25697 /* Consume the `...'. */
25698 cp_lexer_consume_token (parser->lexer);
25700 if (designator && cxx_dialect >= cxx20)
25701 error_at (loc,
25702 "%<...%> not allowed in designated initializer list");
25704 /* Turn the initializer into an initializer expansion. */
25705 initializer = make_pack_expansion (initializer);
25708 /* Add it to the vector. */
25709 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
25711 /* If the next token is not a comma, we have reached the end of
25712 the list. */
25713 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25714 break;
25716 /* Peek at the next token. */
25717 token = cp_lexer_peek_nth_token (parser->lexer, 2);
25718 /* If the next token is a `}', then we're still done. An
25719 initializer-clause can have a trailing `,' after the
25720 initializer-list and before the closing `}'. */
25721 if (token->type == CPP_CLOSE_BRACE)
25722 break;
25724 /* Suppress location wrappers in a long initializer to save memory
25725 (14179). The cutoff is chosen arbitrarily. */
25726 const unsigned loc_max = 256;
25727 unsigned incr = 1;
25728 if (TREE_CODE (initializer) == CONSTRUCTOR)
25729 /* Look one level down because it's easy. Looking deeper would require
25730 passing down a nelts pointer, and I don't think multi-level massive
25731 initializers are common enough to justify this. */
25732 incr = CONSTRUCTOR_NELTS (initializer);
25733 nelts += incr;
25734 if (nelts >= loc_max && (nelts - incr) < loc_max)
25735 ++suppress_location_wrappers;
25737 /* Consume the `,' token. */
25738 cp_lexer_consume_token (parser->lexer);
25741 /* The same identifier shall not appear in multiple designators
25742 of a designated-initializer-list. */
25743 if (first_designator)
25745 unsigned int i;
25746 tree designator, val;
25747 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
25748 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
25750 if (IDENTIFIER_MARKED (designator))
25752 error_at (cp_expr_loc_or_input_loc (val),
25753 "%<.%s%> designator used multiple times in "
25754 "the same initializer list",
25755 IDENTIFIER_POINTER (designator));
25756 (*v)[i].index = error_mark_node;
25758 else
25759 IDENTIFIER_MARKED (designator) = 1;
25761 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
25762 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
25763 IDENTIFIER_MARKED (designator) = 0;
25766 suppress_location_wrappers = suppress;
25768 *designated = first_designator != NULL_TREE;
25769 return v;
25772 /* Classes [gram.class] */
25774 /* Parse a class-name.
25776 class-name:
25777 identifier
25778 template-id
25780 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
25781 to indicate that names looked up in dependent types should be
25782 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
25783 keyword has been used to indicate that the name that appears next
25784 is a template. TAG_TYPE indicates the explicit tag given before
25785 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
25786 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
25787 is the class being defined in a class-head. If ENUM_OK is TRUE,
25788 enum-names are also accepted.
25790 Returns the TYPE_DECL representing the class. */
25792 static tree
25793 cp_parser_class_name (cp_parser *parser,
25794 bool typename_keyword_p,
25795 bool template_keyword_p,
25796 enum tag_types tag_type,
25797 bool check_dependency_p,
25798 bool class_head_p,
25799 bool is_declaration,
25800 bool enum_ok)
25802 tree decl;
25803 tree identifier = NULL_TREE;
25805 /* All class-names start with an identifier. */
25806 cp_token *token = cp_lexer_peek_token (parser->lexer);
25807 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
25809 cp_parser_error (parser, "expected class-name");
25810 return error_mark_node;
25813 /* PARSER->SCOPE can be cleared when parsing the template-arguments
25814 to a template-id, so we save it here. Consider object scope too,
25815 so that make_typename_type below can use it (cp_parser_template_name
25816 considers object scope also). This may happen with code like
25818 p->template A<T>::a()
25820 where we first want to look up A<T>::a in the class of the object
25821 expression, as per [basic.lookup.classref]. */
25822 tree scope = parser->scope ? parser->scope : parser->context->object_type;
25823 /* This only checks parser->scope to avoid duplicate errors; if
25824 ->object_type is erroneous, go on to give a parse error. */
25825 if (parser->scope == error_mark_node)
25826 return error_mark_node;
25828 /* Any name names a type if we're following the `typename' keyword
25829 in a qualified name where the enclosing scope is type-dependent. */
25830 const bool typename_p = (typename_keyword_p
25831 && parser->scope
25832 && TYPE_P (parser->scope)
25833 && dependent_scope_p (parser->scope));
25834 /* Handle the common case (an identifier, but not a template-id)
25835 efficiently. */
25836 if (token->type == CPP_NAME
25837 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
25839 cp_token *identifier_token;
25840 bool ambiguous_p;
25842 /* Look for the identifier. */
25843 identifier_token = cp_lexer_peek_token (parser->lexer);
25844 ambiguous_p = identifier_token->error_reported;
25845 identifier = cp_parser_identifier (parser);
25846 /* If the next token isn't an identifier, we are certainly not
25847 looking at a class-name. */
25848 if (identifier == error_mark_node)
25849 decl = error_mark_node;
25850 /* If we know this is a type-name, there's no need to look it
25851 up. */
25852 else if (typename_p)
25853 decl = identifier;
25854 else
25856 tree ambiguous_decls;
25857 /* If we already know that this lookup is ambiguous, then
25858 we've already issued an error message; there's no reason
25859 to check again. */
25860 if (ambiguous_p)
25862 cp_parser_simulate_error (parser);
25863 return error_mark_node;
25865 /* If the next token is a `::', then the name must be a type
25866 name.
25868 [basic.lookup.qual]
25870 During the lookup for a name preceding the :: scope
25871 resolution operator, object, function, and enumerator
25872 names are ignored. */
25873 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
25874 tag_type = scope_type;
25875 /* Look up the name. */
25876 decl = cp_parser_lookup_name (parser, identifier,
25877 tag_type,
25878 /*is_template=*/false,
25879 /*is_namespace=*/false,
25880 check_dependency_p,
25881 &ambiguous_decls,
25882 identifier_token->location);
25883 if (ambiguous_decls)
25885 if (cp_parser_parsing_tentatively (parser))
25886 cp_parser_simulate_error (parser);
25887 return error_mark_node;
25891 else
25893 /* Try a template-id. */
25894 decl = cp_parser_template_id (parser, template_keyword_p,
25895 check_dependency_p,
25896 tag_type,
25897 is_declaration);
25898 if (decl == error_mark_node)
25899 return error_mark_node;
25902 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
25904 /* If this is a typename, create a TYPENAME_TYPE. */
25905 if (typename_p && decl != error_mark_node)
25907 decl = make_typename_type (scope, decl, typename_type,
25908 /*complain=*/tf_error);
25909 if (decl != error_mark_node)
25910 decl = TYPE_NAME (decl);
25913 decl = strip_using_decl (decl);
25915 /* Check to see that it is really the name of a class. */
25916 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
25917 && identifier_p (TREE_OPERAND (decl, 0))
25918 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
25919 /* Situations like this:
25921 template <typename T> struct A {
25922 typename T::template X<int>::I i;
25925 are problematic. Is `T::template X<int>' a class-name? The
25926 standard does not seem to be definitive, but there is no other
25927 valid interpretation of the following `::'. Therefore, those
25928 names are considered class-names. */
25930 decl = make_typename_type (scope, decl, tag_type, tf_error);
25931 if (decl != error_mark_node)
25932 decl = TYPE_NAME (decl);
25934 else if (TREE_CODE (decl) != TYPE_DECL
25935 || TREE_TYPE (decl) == error_mark_node
25936 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
25937 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
25938 /* In Objective-C 2.0, a classname followed by '.' starts a
25939 dot-syntax expression, and it's not a type-name. */
25940 || (c_dialect_objc ()
25941 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
25942 && objc_is_class_name (decl)))
25943 decl = error_mark_node;
25945 if (decl == error_mark_node)
25946 cp_parser_error (parser, "expected class-name");
25947 else if (identifier && !parser->scope)
25948 maybe_note_name_used_in_class (identifier, decl);
25950 return decl;
25953 /* Make sure that any member-function parameters are in scope.
25954 For instance, a function's noexcept-specifier can use the function's
25955 parameters:
25957 struct S {
25958 void fn (int p) noexcept(noexcept(p));
25961 so we need to make sure name lookup can find them. This is used
25962 when we delay parsing of the noexcept-specifier. */
25964 static void
25965 inject_parm_decls (tree decl)
25967 begin_scope (sk_function_parms, decl);
25968 tree args = DECL_ARGUMENTS (decl);
25970 do_push_parm_decls (decl, args, /*nonparms=*/NULL);
25972 if (args && is_this_parameter (args))
25974 gcc_checking_assert (current_class_ptr == NULL_TREE);
25975 current_class_ref = cp_build_fold_indirect_ref (args);
25976 current_class_ptr = args;
25980 /* Undo the effects of inject_parm_decls. */
25982 static void
25983 pop_injected_parms (void)
25985 pop_bindings_and_leave_scope ();
25986 current_class_ptr = current_class_ref = NULL_TREE;
25989 /* Parse a class-specifier.
25991 class-specifier:
25992 class-head { member-specification [opt] }
25994 Returns the TREE_TYPE representing the class. */
25996 tree
25997 cp_parser_class_specifier (cp_parser* parser)
25999 auto_timevar tv (TV_PARSE_STRUCT);
26001 tree type;
26002 tree attributes = NULL_TREE;
26003 bool nested_name_specifier_p;
26004 unsigned saved_num_template_parameter_lists;
26005 bool saved_in_function_body;
26006 unsigned char in_statement;
26007 bool in_switch_statement_p;
26008 bool saved_in_unbraced_linkage_specification_p;
26009 tree old_scope = NULL_TREE;
26010 tree scope = NULL_TREE;
26011 cp_token *closing_brace;
26013 push_deferring_access_checks (dk_no_deferred);
26015 /* Parse the class-head. */
26016 type = cp_parser_class_head (parser,
26017 &nested_name_specifier_p);
26018 /* If the class-head was a semantic disaster, skip the entire body
26019 of the class. */
26020 if (!type)
26022 cp_parser_skip_to_end_of_block_or_statement (parser);
26023 pop_deferring_access_checks ();
26024 return error_mark_node;
26027 /* Look for the `{'. */
26028 matching_braces braces;
26029 if (!braces.require_open (parser))
26031 pop_deferring_access_checks ();
26032 return error_mark_node;
26035 cp_ensure_no_omp_declare_simd (parser);
26036 cp_ensure_no_oacc_routine (parser);
26038 /* Issue an error message if type-definitions are forbidden here. */
26039 bool type_definition_ok_p = cp_parser_check_type_definition (parser);
26040 /* Remember that we are defining one more class. */
26041 ++parser->num_classes_being_defined;
26042 /* Inside the class, surrounding template-parameter-lists do not
26043 apply. */
26044 saved_num_template_parameter_lists
26045 = parser->num_template_parameter_lists;
26046 parser->num_template_parameter_lists = 0;
26047 /* We are not in a function body. */
26048 saved_in_function_body = parser->in_function_body;
26049 parser->in_function_body = false;
26050 /* Or in a loop. */
26051 in_statement = parser->in_statement;
26052 parser->in_statement = 0;
26053 /* Or in a switch. */
26054 in_switch_statement_p = parser->in_switch_statement_p;
26055 parser->in_switch_statement_p = false;
26056 /* We are not immediately inside an extern "lang" block. */
26057 saved_in_unbraced_linkage_specification_p
26058 = parser->in_unbraced_linkage_specification_p;
26059 parser->in_unbraced_linkage_specification_p = false;
26061 /* Start the class. */
26062 if (nested_name_specifier_p)
26064 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
26065 /* SCOPE must be a scope nested inside current scope. */
26066 if (is_nested_namespace (current_namespace,
26067 decl_namespace_context (scope)))
26068 old_scope = push_inner_scope (scope);
26069 else
26070 nested_name_specifier_p = false;
26072 type = begin_class_definition (type);
26074 if (type == error_mark_node)
26075 /* If the type is erroneous, skip the entire body of the class. */
26076 cp_parser_skip_to_closing_brace (parser);
26077 else
26078 /* Parse the member-specification. */
26079 cp_parser_member_specification_opt (parser);
26081 /* Look for the trailing `}'. */
26082 closing_brace = braces.require_close (parser);
26083 /* Look for trailing attributes to apply to this class. */
26084 if (cp_parser_allow_gnu_extensions_p (parser))
26085 attributes = cp_parser_gnu_attributes_opt (parser);
26086 if (type != error_mark_node)
26087 type = finish_struct (type, attributes);
26088 if (nested_name_specifier_p)
26089 pop_inner_scope (old_scope, scope);
26091 /* We've finished a type definition. Check for the common syntax
26092 error of forgetting a semicolon after the definition. We need to
26093 be careful, as we can't just check for not-a-semicolon and be done
26094 with it; the user might have typed:
26096 class X { } c = ...;
26097 class X { } *p = ...;
26099 and so forth. Instead, enumerate all the possible tokens that
26100 might follow this production; if we don't see one of them, then
26101 complain and silently insert the semicolon. */
26103 cp_token *token = cp_lexer_peek_token (parser->lexer);
26104 bool want_semicolon = true;
26106 if (cp_next_tokens_can_be_std_attribute_p (parser))
26107 /* Don't try to parse c++11 attributes here. As per the
26108 grammar, that should be a task for
26109 cp_parser_decl_specifier_seq. */
26110 want_semicolon = false;
26112 switch (token->type)
26114 case CPP_NAME:
26115 case CPP_SEMICOLON:
26116 case CPP_MULT:
26117 case CPP_AND:
26118 case CPP_OPEN_PAREN:
26119 case CPP_CLOSE_PAREN:
26120 case CPP_COMMA:
26121 case CPP_SCOPE:
26122 want_semicolon = false;
26123 break;
26125 /* While it's legal for type qualifiers and storage class
26126 specifiers to follow type definitions in the grammar, only
26127 compiler testsuites contain code like that. Assume that if
26128 we see such code, then what we're really seeing is a case
26129 like:
26131 class X { }
26132 const <type> var = ...;
26136 class Y { }
26137 static <type> func (...) ...
26139 i.e. the qualifier or specifier applies to the next
26140 declaration. To do so, however, we need to look ahead one
26141 more token to see if *that* token is a type specifier.
26143 This code could be improved to handle:
26145 class Z { }
26146 static const <type> var = ...; */
26147 case CPP_KEYWORD:
26148 if (keyword_is_decl_specifier (token->keyword))
26150 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
26152 /* Handling user-defined types here would be nice, but very
26153 tricky. */
26154 want_semicolon
26155 = (lookahead->type == CPP_KEYWORD
26156 && keyword_begins_type_specifier (lookahead->keyword));
26158 break;
26159 default:
26160 break;
26163 /* If we don't have a type, then something is very wrong and we
26164 shouldn't try to do anything clever. Likewise for not seeing the
26165 closing brace. */
26166 if (closing_brace && TYPE_P (type) && want_semicolon)
26168 /* Locate the closing brace. */
26169 cp_token_position prev
26170 = cp_lexer_previous_token_position (parser->lexer);
26171 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
26172 location_t loc = prev_token->location;
26174 /* We want to suggest insertion of a ';' immediately *after* the
26175 closing brace, so, if we can, offset the location by 1 column. */
26176 location_t next_loc = loc;
26177 if (!linemap_location_from_macro_expansion_p (line_table, loc))
26178 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
26180 rich_location richloc (line_table, next_loc);
26182 /* If we successfully offset the location, suggest the fix-it. */
26183 if (next_loc != loc)
26184 richloc.add_fixit_insert_before (next_loc, ";");
26186 if (CLASSTYPE_DECLARED_CLASS (type))
26187 error_at (&richloc,
26188 "expected %<;%> after class definition");
26189 else if (TREE_CODE (type) == RECORD_TYPE)
26190 error_at (&richloc,
26191 "expected %<;%> after struct definition");
26192 else if (TREE_CODE (type) == UNION_TYPE)
26193 error_at (&richloc,
26194 "expected %<;%> after union definition");
26195 else
26196 gcc_unreachable ();
26198 /* Unget one token and smash it to look as though we encountered
26199 a semicolon in the input stream. */
26200 cp_lexer_set_token_position (parser->lexer, prev);
26201 token = cp_lexer_peek_token (parser->lexer);
26202 token->type = CPP_SEMICOLON;
26203 token->keyword = RID_MAX;
26207 /* If this class is not itself within the scope of another class,
26208 then we need to parse the bodies of all of the queued function
26209 definitions. Note that the queued functions defined in a class
26210 are not always processed immediately following the
26211 class-specifier for that class. Consider:
26213 struct A {
26214 struct B { void f() { sizeof (A); } };
26217 If `f' were processed before the processing of `A' were
26218 completed, there would be no way to compute the size of `A'.
26219 Note that the nesting we are interested in here is lexical --
26220 not the semantic nesting given by TYPE_CONTEXT. In particular,
26221 for:
26223 struct A { struct B; };
26224 struct A::B { void f() { } };
26226 there is no need to delay the parsing of `A::B::f'. */
26227 if (--parser->num_classes_being_defined == 0)
26229 tree decl;
26230 tree class_type = NULL_TREE;
26231 tree pushed_scope = NULL_TREE;
26232 unsigned ix;
26233 cp_default_arg_entry *e;
26235 if (!type_definition_ok_p || any_erroneous_template_args_p (type))
26237 /* Skip default arguments, NSDMIs, etc, in order to improve
26238 error recovery (c++/71169, c++/71832). */
26239 vec_safe_truncate (unparsed_funs_with_default_args, 0);
26240 vec_safe_truncate (unparsed_nsdmis, 0);
26241 vec_safe_truncate (unparsed_funs_with_definitions, 0);
26244 /* In a first pass, parse default arguments to the functions.
26245 Then, in a second pass, parse the bodies of the functions.
26246 This two-phased approach handles cases like:
26248 struct S {
26249 void f() { g(); }
26250 void g(int i = 3);
26254 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
26256 decl = e->decl;
26257 /* If there are default arguments that have not yet been processed,
26258 take care of them now. */
26259 if (class_type != e->class_type)
26261 if (pushed_scope)
26262 pop_scope (pushed_scope);
26263 class_type = e->class_type;
26264 pushed_scope = push_scope (class_type);
26266 /* Make sure that any template parameters are in scope. */
26267 maybe_begin_member_template_processing (decl);
26268 /* Parse the default argument expressions. */
26269 cp_parser_late_parsing_default_args (parser, decl);
26270 /* Remove any template parameters from the symbol table. */
26271 maybe_end_member_template_processing ();
26273 vec_safe_truncate (unparsed_funs_with_default_args, 0);
26275 /* If there are noexcept-specifiers that have not yet been processed,
26276 take care of them now. Do this before processing NSDMIs as they
26277 may depend on noexcept-specifiers already having been processed. */
26278 tree save_ccp = current_class_ptr;
26279 tree save_ccr = current_class_ref;
26280 FOR_EACH_VEC_SAFE_ELT (unparsed_noexcepts, ix, decl)
26282 tree ctx = DECL_CONTEXT (decl);
26283 if (class_type != ctx)
26285 if (pushed_scope)
26286 pop_scope (pushed_scope);
26287 class_type = ctx;
26288 pushed_scope = push_scope (class_type);
26291 tree def_parse = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl));
26292 def_parse = TREE_PURPOSE (def_parse);
26294 /* Make sure that any template parameters are in scope. */
26295 maybe_begin_member_template_processing (decl);
26297 /* Make sure that any member-function parameters are in scope.
26298 This function doesn't expect ccp to be set. */
26299 current_class_ptr = current_class_ref = NULL_TREE;
26300 inject_parm_decls (decl);
26302 /* 'this' is not allowed in static member functions. */
26303 unsigned char local_variables_forbidden_p
26304 = parser->local_variables_forbidden_p;
26305 if (DECL_THIS_STATIC (decl))
26306 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
26308 /* Now we can parse the noexcept-specifier. */
26309 tree spec = cp_parser_late_noexcept_specifier (parser, def_parse);
26311 if (spec == error_mark_node)
26312 spec = NULL_TREE;
26314 /* Update the fn's type directly -- it might have escaped
26315 beyond this decl :( */
26316 fixup_deferred_exception_variants (TREE_TYPE (decl), spec);
26317 /* Update any instantiations we've already created. We must
26318 keep the new noexcept-specifier wrapped in a DEFERRED_NOEXCEPT
26319 so that maybe_instantiate_noexcept can tsubst the NOEXCEPT_EXPR
26320 in the pattern. */
26321 for (tree i : DEFPARSE_INSTANTIATIONS (def_parse))
26322 DEFERRED_NOEXCEPT_PATTERN (TREE_PURPOSE (i))
26323 = spec ? TREE_PURPOSE (spec) : error_mark_node;
26325 /* Restore the state of local_variables_forbidden_p. */
26326 parser->local_variables_forbidden_p = local_variables_forbidden_p;
26328 /* The finish_struct call above performed various override checking,
26329 but it skipped unparsed noexcept-specifier operands. Now that we
26330 have resolved them, check again. */
26331 noexcept_override_late_checks (type, decl);
26333 /* Remove any member-function parameters from the symbol table. */
26334 pop_injected_parms ();
26336 /* Remove any template parameters from the symbol table. */
26337 maybe_end_member_template_processing ();
26339 vec_safe_truncate (unparsed_noexcepts, 0);
26341 /* Now parse any NSDMIs. */
26342 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
26344 if (class_type != DECL_CONTEXT (decl))
26346 if (pushed_scope)
26347 pop_scope (pushed_scope);
26348 class_type = DECL_CONTEXT (decl);
26349 pushed_scope = push_scope (class_type);
26351 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
26352 cp_parser_late_parsing_nsdmi (parser, decl);
26354 vec_safe_truncate (unparsed_nsdmis, 0);
26356 /* Now contract attributes. */
26357 FOR_EACH_VEC_SAFE_ELT (unparsed_contracts, ix, decl)
26359 tree ctx = DECL_CONTEXT (decl);
26360 if (class_type != ctx)
26362 if (pushed_scope)
26363 pop_scope (pushed_scope);
26364 class_type = ctx;
26365 pushed_scope = push_scope (class_type);
26368 temp_override<tree> cfd(current_function_decl, decl);
26370 /* Make sure that any template parameters are in scope. */
26371 maybe_begin_member_template_processing (decl);
26373 /* Make sure that any member-function parameters are in scope.
26374 This function doesn't expect ccp to be set. */
26375 current_class_ptr = current_class_ref = NULL_TREE;
26376 inject_parm_decls (decl);
26378 /* 'this' is not allowed in static member functions. */
26379 unsigned char local_variables_forbidden_p
26380 = parser->local_variables_forbidden_p;
26381 if (DECL_THIS_STATIC (decl))
26382 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
26384 /* Now we can parse contract conditions. */
26385 for (tree a = DECL_ATTRIBUTES (decl); a; a = TREE_CHAIN (a))
26387 if (cxx_contract_attribute_p (a))
26388 cp_parser_late_contract_condition (parser, decl, a);
26391 /* Restore the state of local_variables_forbidden_p. */
26392 parser->local_variables_forbidden_p = local_variables_forbidden_p;
26394 /* Remove any member-function parameters from the symbol table. */
26395 pop_injected_parms ();
26397 /* Remove any template parameters from the symbol table. */
26398 maybe_end_member_template_processing ();
26400 /* Perform any deferred contract matching. */
26401 match_deferred_contracts (decl);
26403 vec_safe_truncate (unparsed_contracts, 0);
26405 current_class_ptr = save_ccp;
26406 current_class_ref = save_ccr;
26407 if (pushed_scope)
26408 pop_scope (pushed_scope);
26410 /* Now parse the body of the functions. */
26411 if (flag_openmp)
26413 /* OpenMP UDRs need to be parsed before all other functions. */
26414 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
26415 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
26416 cp_parser_late_parsing_for_member (parser, decl);
26417 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
26418 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
26419 cp_parser_late_parsing_for_member (parser, decl);
26421 else
26422 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
26423 cp_parser_late_parsing_for_member (parser, decl);
26424 vec_safe_truncate (unparsed_funs_with_definitions, 0);
26427 /* Put back any saved access checks. */
26428 pop_deferring_access_checks ();
26430 /* Restore saved state. */
26431 parser->in_switch_statement_p = in_switch_statement_p;
26432 parser->in_statement = in_statement;
26433 parser->in_function_body = saved_in_function_body;
26434 parser->num_template_parameter_lists
26435 = saved_num_template_parameter_lists;
26436 parser->in_unbraced_linkage_specification_p
26437 = saved_in_unbraced_linkage_specification_p;
26439 return type;
26442 /* Parse a class-head.
26444 class-head:
26445 class-key identifier [opt] base-clause [opt]
26446 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
26447 class-key nested-name-specifier [opt] template-id
26448 base-clause [opt]
26450 class-virt-specifier:
26451 final
26453 GNU Extensions:
26454 class-key attributes identifier [opt] base-clause [opt]
26455 class-key attributes nested-name-specifier identifier base-clause [opt]
26456 class-key attributes nested-name-specifier [opt] template-id
26457 base-clause [opt]
26459 Upon return BASES is initialized to the list of base classes (or
26460 NULL, if there are none) in the same form returned by
26461 cp_parser_base_clause.
26463 Returns the TYPE of the indicated class. Sets
26464 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
26465 involving a nested-name-specifier was used, and FALSE otherwise.
26467 Returns error_mark_node if this is not a class-head.
26469 Returns NULL_TREE if the class-head is syntactically valid, but
26470 semantically invalid in a way that means we should skip the entire
26471 body of the class. */
26473 static tree
26474 cp_parser_class_head (cp_parser* parser,
26475 bool* nested_name_specifier_p)
26477 tree nested_name_specifier;
26478 enum tag_types class_key;
26479 tree id = NULL_TREE;
26480 tree type = NULL_TREE;
26481 tree attributes;
26482 tree bases;
26483 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
26484 bool template_id_p = false;
26485 bool qualified_p = false;
26486 bool invalid_nested_name_p = false;
26487 bool invalid_explicit_specialization_p = false;
26488 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
26489 tree pushed_scope = NULL_TREE;
26490 unsigned num_templates;
26491 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
26492 /* Assume no nested-name-specifier will be present. */
26493 *nested_name_specifier_p = false;
26494 /* Assume no template parameter lists will be used in defining the
26495 type. */
26496 num_templates = 0;
26497 parser->colon_corrects_to_scope_p = false;
26499 /* Look for the class-key. */
26500 class_key = cp_parser_class_key (parser);
26501 if (class_key == none_type)
26502 return error_mark_node;
26504 location_t class_head_start_location = input_location;
26506 /* Parse the attributes. */
26507 attributes = cp_parser_attributes_opt (parser);
26508 if (find_contract (attributes))
26509 diagnose_misapplied_contracts (attributes);
26511 /* If the next token is `::', that is invalid -- but sometimes
26512 people do try to write:
26514 struct ::S {};
26516 Handle this gracefully by accepting the extra qualifier, and then
26517 issuing an error about it later if this really is a
26518 class-head. If it turns out just to be an elaborated type
26519 specifier, remain silent. */
26520 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
26521 qualified_p = true;
26523 push_deferring_access_checks (dk_no_check);
26525 /* Determine the name of the class. Begin by looking for an
26526 optional nested-name-specifier. */
26527 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
26528 nested_name_specifier
26529 = cp_parser_nested_name_specifier_opt (parser,
26530 /*typename_keyword_p=*/false,
26531 /*check_dependency_p=*/false,
26532 /*type_p=*/true,
26533 /*is_declaration=*/false);
26534 /* If there was a nested-name-specifier, then there *must* be an
26535 identifier. */
26537 cp_token *bad_template_keyword = NULL;
26539 if (nested_name_specifier)
26541 type_start_token = cp_lexer_peek_token (parser->lexer);
26542 /* Although the grammar says `identifier', it really means
26543 `class-name' or `template-name'. You are only allowed to
26544 define a class that has already been declared with this
26545 syntax.
26547 The proposed resolution for Core Issue 180 says that wherever
26548 you see `class T::X' you should treat `X' as a type-name.
26550 It is OK to define an inaccessible class; for example:
26552 class A { class B; };
26553 class A::B {};
26555 We do not know if we will see a class-name, or a
26556 template-name. We look for a class-name first, in case the
26557 class-name is a template-id; if we looked for the
26558 template-name first we would stop after the template-name. */
26559 cp_parser_parse_tentatively (parser);
26560 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26561 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
26562 type = cp_parser_class_name (parser,
26563 /*typename_keyword_p=*/false,
26564 /*template_keyword_p=*/false,
26565 class_type,
26566 /*check_dependency_p=*/false,
26567 /*class_head_p=*/true,
26568 /*is_declaration=*/false);
26569 /* If that didn't work, ignore the nested-name-specifier. */
26570 if (!cp_parser_parse_definitely (parser))
26572 invalid_nested_name_p = true;
26573 type_start_token = cp_lexer_peek_token (parser->lexer);
26574 id = cp_parser_identifier (parser);
26575 if (id == error_mark_node)
26576 id = NULL_TREE;
26578 /* If we could not find a corresponding TYPE, treat this
26579 declaration like an unqualified declaration. */
26580 if (type == error_mark_node)
26581 nested_name_specifier = NULL_TREE;
26582 /* Otherwise, count the number of templates used in TYPE and its
26583 containing scopes. */
26584 else
26585 num_templates = num_template_headers_for_class (TREE_TYPE (type));
26587 /* Otherwise, the identifier is optional. */
26588 else
26590 /* We don't know whether what comes next is a template-id,
26591 an identifier, or nothing at all. */
26592 cp_parser_parse_tentatively (parser);
26593 /* Check for a template-id. */
26594 type_start_token = cp_lexer_peek_token (parser->lexer);
26595 id = cp_parser_template_id (parser,
26596 /*template_keyword_p=*/false,
26597 /*check_dependency_p=*/true,
26598 class_key,
26599 /*is_declaration=*/true);
26600 /* If that didn't work, it could still be an identifier. */
26601 if (!cp_parser_parse_definitely (parser))
26603 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26605 type_start_token = cp_lexer_peek_token (parser->lexer);
26606 id = cp_parser_identifier (parser);
26608 else
26609 id = NULL_TREE;
26611 else
26613 template_id_p = true;
26614 ++num_templates;
26618 pop_deferring_access_checks ();
26620 if (id)
26622 cp_parser_check_for_invalid_template_id (parser, id,
26623 class_key,
26624 type_start_token->location);
26626 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
26628 /* If it's not a `:' or a `{' then we can't really be looking at a
26629 class-head, since a class-head only appears as part of a
26630 class-specifier. We have to detect this situation before calling
26631 xref_tag, since that has irreversible side-effects. */
26632 if (!cp_parser_next_token_starts_class_definition_p (parser))
26634 cp_parser_error (parser, "expected %<{%> or %<:%>");
26635 type = error_mark_node;
26636 goto out;
26639 /* At this point, we're going ahead with the class-specifier, even
26640 if some other problem occurs. */
26641 cp_parser_commit_to_tentative_parse (parser);
26642 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
26644 cp_parser_error (parser,
26645 "cannot specify %<override%> for a class");
26646 type = error_mark_node;
26647 goto out;
26649 /* Issue the error about the overly-qualified name now. */
26650 if (qualified_p)
26652 cp_parser_error (parser,
26653 "global qualification of class name is invalid");
26654 type = error_mark_node;
26655 goto out;
26657 else if (invalid_nested_name_p)
26659 cp_parser_error (parser,
26660 "qualified name does not name a class");
26661 type = error_mark_node;
26662 goto out;
26664 else if (nested_name_specifier)
26666 tree scope;
26668 if (bad_template_keyword)
26669 /* [temp.names]: in a qualified-id formed by a class-head-name, the
26670 keyword template shall not appear at the top level. */
26671 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
26672 "keyword %<template%> not allowed in class-head-name");
26674 /* Reject typedef-names in class heads. */
26675 if (!DECL_IMPLICIT_TYPEDEF_P (type))
26677 error_at (type_start_token->location,
26678 "invalid class name in declaration of %qD",
26679 type);
26680 type = NULL_TREE;
26681 goto done;
26684 /* Figure out in what scope the declaration is being placed. */
26685 scope = current_scope ();
26686 /* If that scope does not contain the scope in which the
26687 class was originally declared, the program is invalid. */
26688 if (scope && !is_ancestor (scope, nested_name_specifier))
26690 if (at_namespace_scope_p ())
26691 error_at (type_start_token->location,
26692 "declaration of %qD in namespace %qD which does not "
26693 "enclose %qD",
26694 type, scope, nested_name_specifier);
26695 else
26696 error_at (type_start_token->location,
26697 "declaration of %qD in %qD which does not enclose %qD",
26698 type, scope, nested_name_specifier);
26699 type = NULL_TREE;
26700 goto done;
26702 /* [dcl.meaning]
26704 A declarator-id shall not be qualified except for the
26705 definition of a ... nested class outside of its class
26706 ... [or] the definition or explicit instantiation of a
26707 class member of a namespace outside of its namespace. */
26708 if (scope == nested_name_specifier)
26709 permerror (nested_name_specifier_token_start->location,
26710 "extra qualification not allowed");
26712 /* An explicit-specialization must be preceded by "template <>". If
26713 it is not, try to recover gracefully. */
26714 if (at_namespace_scope_p ()
26715 && parser->num_template_parameter_lists == 0
26716 && !processing_template_parmlist
26717 && template_id_p)
26719 /* Build a location of this form:
26720 struct typename <ARGS>
26721 ^~~~~~~~~~~~~~~~~~~~~~
26722 with caret==start at the start token, and
26723 finishing at the end of the type. */
26724 location_t reported_loc
26725 = make_location (class_head_start_location,
26726 class_head_start_location,
26727 get_finish (type_start_token->location));
26728 rich_location richloc (line_table, reported_loc);
26729 richloc.add_fixit_insert_before (class_head_start_location,
26730 "template <> ");
26731 error_at (&richloc,
26732 "an explicit specialization must be preceded by"
26733 " %<template <>%>");
26734 invalid_explicit_specialization_p = true;
26735 /* Take the same action that would have been taken by
26736 cp_parser_explicit_specialization. */
26737 ++parser->num_template_parameter_lists;
26738 begin_specialization ();
26740 /* There must be no "return" statements between this point and the
26741 end of this function; set "type "to the correct return value and
26742 use "goto done;" to return. */
26743 /* Make sure that the right number of template parameters were
26744 present. */
26745 if (!cp_parser_check_template_parameters (parser, num_templates,
26746 template_id_p,
26747 type_start_token->location,
26748 /*declarator=*/NULL))
26750 /* If something went wrong, there is no point in even trying to
26751 process the class-definition. */
26752 type = NULL_TREE;
26753 goto done;
26756 /* Look up the type. */
26757 if (template_id_p)
26759 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
26760 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
26761 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
26763 error_at (type_start_token->location,
26764 "function template %qD redeclared as a class template", id);
26765 type = error_mark_node;
26767 else
26769 type = TREE_TYPE (id);
26770 type = maybe_process_partial_specialization (type);
26772 /* Check the scope while we still know whether or not we had a
26773 nested-name-specifier. */
26774 if (type != error_mark_node)
26775 check_unqualified_spec_or_inst (type, type_start_token->location);
26777 if (nested_name_specifier)
26778 pushed_scope = push_scope (nested_name_specifier);
26780 else if (nested_name_specifier)
26782 type = TREE_TYPE (type);
26784 /* Given:
26786 template <typename T> struct S { struct T };
26787 template <typename T> struct S<T>::T { };
26789 we will get a TYPENAME_TYPE when processing the definition of
26790 `S::T'. We need to resolve it to the actual type before we
26791 try to define it. */
26792 if (TREE_CODE (type) == TYPENAME_TYPE)
26794 type = resolve_typename_type (type, /*only_current_p=*/false);
26795 if (TREE_CODE (type) == TYPENAME_TYPE)
26797 cp_parser_error (parser, "could not resolve typename type");
26798 type = error_mark_node;
26802 type = maybe_process_partial_specialization (type);
26803 if (type == error_mark_node)
26805 type = NULL_TREE;
26806 goto done;
26809 /* Enter the scope indicated by the nested-name-specifier. */
26810 pushed_scope = push_scope (nested_name_specifier);
26811 /* Get the canonical version of this type. */
26812 type = TYPE_MAIN_DECL (type);
26813 /* Call push_template_decl if it seems like we should be defining a
26814 template either from the template headers or the type we're
26815 defining, so that we diagnose both extra and missing headers. */
26816 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
26817 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
26818 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
26820 type = push_template_decl (type);
26821 if (type == error_mark_node)
26823 type = NULL_TREE;
26824 goto done;
26828 type = TREE_TYPE (type);
26829 *nested_name_specifier_p = true;
26831 else /* The name is not a nested name. */
26833 /* If the class was unnamed, create a dummy name. */
26834 if (!id)
26835 id = make_anon_name ();
26836 TAG_how how = (parser->in_type_id_in_expr_p
26837 ? TAG_how::INNERMOST_NON_CLASS
26838 : TAG_how::CURRENT_ONLY);
26839 type = xref_tag (class_key, id, how,
26840 parser->num_template_parameter_lists);
26843 /* Diagnose class/struct/union mismatches. */
26844 cp_parser_check_class_key (parser, UNKNOWN_LOCATION, class_key, type,
26845 true, true);
26847 /* Indicate whether this class was declared as a `class' or as a
26848 `struct'. */
26849 if (TREE_CODE (type) == RECORD_TYPE)
26850 CLASSTYPE_DECLARED_CLASS (type) = class_key == class_type;
26852 /* If this type was already complete, and we see another definition,
26853 that's an error. Likewise if the type is already being defined:
26854 this can happen, eg, when it's defined from within an expression
26855 (c++/84605). */
26856 if (type != error_mark_node
26857 && (COMPLETE_TYPE_P (type) || TYPE_BEING_DEFINED (type)))
26859 error_at (type_start_token->location, "redefinition of %q#T",
26860 type);
26861 inform (location_of (type), "previous definition of %q#T",
26862 type);
26863 type = NULL_TREE;
26864 goto done;
26866 else if (type == error_mark_node)
26867 type = NULL_TREE;
26869 if (type)
26871 if (current_lambda_expr ()
26872 && uses_parameter_packs (attributes))
26874 /* In a lambda this should work, but doesn't currently. */
26875 sorry ("unexpanded parameter pack in local class in lambda");
26876 attributes = NULL_TREE;
26879 /* Apply attributes now, before any use of the class as a template
26880 argument in its base list. */
26881 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
26882 fixup_attribute_variants (type);
26885 /* Associate constraints with the type. */
26886 if (flag_concepts)
26887 type = associate_classtype_constraints (type);
26889 /* We will have entered the scope containing the class; the names of
26890 base classes should be looked up in that context. For example:
26892 struct A { struct B {}; struct C; };
26893 struct A::C : B {};
26895 is valid. */
26897 /* Get the list of base-classes, if there is one. Defer access checking
26898 until the entire list has been seen, as per [class.access.general]. */
26899 push_deferring_access_checks (dk_deferred);
26900 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
26902 if (type)
26903 pushclass (type);
26904 bases = cp_parser_base_clause (parser);
26905 if (type)
26906 popclass ();
26908 else
26909 bases = NULL_TREE;
26911 /* If we're really defining a class, process the base classes.
26912 If they're invalid, fail. */
26913 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26914 xref_basetypes (type, bases);
26916 /* Now that all bases have been seen and attached to the class, check
26917 accessibility of the types named in the base-clause. This must be
26918 done relative to the class scope, so that we accept e.g.
26920 struct A { protected: struct B {}; };
26921 struct C : A::B, A {}; // OK: A::B is accessible via base A
26923 as per [class.access.general]. */
26924 if (type)
26925 pushclass (type);
26926 pop_to_parent_deferring_access_checks ();
26927 if (type)
26928 popclass ();
26930 done:
26931 /* Leave the scope given by the nested-name-specifier. We will
26932 enter the class scope itself while processing the members. */
26933 if (pushed_scope)
26934 pop_scope (pushed_scope);
26936 if (invalid_explicit_specialization_p)
26938 end_specialization ();
26939 --parser->num_template_parameter_lists;
26942 if (type)
26943 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
26944 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
26945 CLASSTYPE_FINAL (type) = 1;
26946 out:
26947 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
26948 return type;
26951 /* Parse a class-key.
26953 class-key:
26954 class
26955 struct
26956 union
26958 Returns the kind of class-key specified, or none_type to indicate
26959 error. */
26961 static enum tag_types
26962 cp_parser_class_key (cp_parser* parser)
26964 cp_token *token;
26965 enum tag_types tag_type;
26967 /* Look for the class-key. */
26968 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
26969 if (!token)
26970 return none_type;
26972 /* Check to see if the TOKEN is a class-key. */
26973 tag_type = cp_parser_token_is_class_key (token);
26974 if (!tag_type)
26975 cp_parser_error (parser, "expected class-key");
26976 return tag_type;
26979 /* Parse a type-parameter-key.
26981 type-parameter-key:
26982 class
26983 typename
26986 static void
26987 cp_parser_type_parameter_key (cp_parser* parser)
26989 /* Look for the type-parameter-key. */
26990 enum tag_types tag_type = none_type;
26991 cp_token *token = cp_lexer_peek_token (parser->lexer);
26992 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
26994 cp_lexer_consume_token (parser->lexer);
26995 if (pedantic && tag_type == typename_type
26996 && cxx_dialect < cxx17)
26997 /* typename is not allowed in a template template parameter
26998 by the standard until C++17. */
26999 pedwarn (token->location, OPT_Wc__17_extensions,
27000 "ISO C++ forbids typename key in template template parameter;"
27001 " use %<-std=c++17%> or %<-std=gnu++17%>");
27003 else
27004 cp_parser_error (parser, "expected %<class%> or %<typename%>");
27006 return;
27009 /* Parse an (optional) member-specification.
27011 member-specification:
27012 member-declaration member-specification [opt]
27013 access-specifier : member-specification [opt] */
27015 static void
27016 cp_parser_member_specification_opt (cp_parser* parser)
27018 while (true)
27020 cp_token *token;
27021 enum rid keyword;
27023 /* Peek at the next token. */
27024 token = cp_lexer_peek_token (parser->lexer);
27025 /* If it's a `}', or EOF then we've seen all the members. */
27026 if (token->type == CPP_CLOSE_BRACE
27027 || token->type == CPP_EOF
27028 || token->type == CPP_PRAGMA_EOL)
27029 break;
27031 /* See if this token is a keyword. */
27032 keyword = token->keyword;
27033 switch (keyword)
27035 case RID_PUBLIC:
27036 case RID_PROTECTED:
27037 case RID_PRIVATE:
27038 /* Consume the access-specifier. */
27039 cp_lexer_consume_token (parser->lexer);
27040 /* Remember which access-specifier is active. */
27041 current_access_specifier = token->u.value;
27042 /* Look for the `:'. */
27043 cp_parser_require (parser, CPP_COLON, RT_COLON);
27044 break;
27046 default:
27047 /* Accept #pragmas at class scope. */
27048 if (token->type == CPP_PRAGMA)
27050 cp_parser_pragma (parser, pragma_member, NULL);
27051 break;
27054 /* Otherwise, the next construction must be a
27055 member-declaration. */
27056 cp_parser_member_declaration (parser);
27061 /* Parse a member-declaration.
27063 member-declaration:
27064 decl-specifier-seq [opt] member-declarator-list [opt] ;
27065 function-definition ; [opt]
27066 :: [opt] nested-name-specifier template [opt] unqualified-id ;
27067 using-declaration
27068 template-declaration
27069 alias-declaration
27071 member-declarator-list:
27072 member-declarator
27073 member-declarator-list , member-declarator
27075 member-declarator:
27076 declarator pure-specifier [opt]
27077 declarator constant-initializer [opt]
27078 identifier [opt] : constant-expression
27080 GNU Extensions:
27082 member-declaration:
27083 __extension__ member-declaration
27085 member-declarator:
27086 declarator attributes [opt] pure-specifier [opt]
27087 declarator attributes [opt] constant-initializer [opt]
27088 identifier [opt] attributes [opt] : constant-expression
27090 C++0x Extensions:
27092 member-declaration:
27093 static_assert-declaration */
27095 static void
27096 cp_parser_member_declaration (cp_parser* parser)
27098 cp_decl_specifier_seq decl_specifiers;
27099 tree prefix_attributes;
27100 tree decl;
27101 int declares_class_or_enum;
27102 bool friend_p;
27103 cp_token *token = NULL;
27104 cp_token *decl_spec_token_start = NULL;
27105 cp_token *initializer_token_start = NULL;
27106 int saved_pedantic;
27107 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27109 /* Check for the `__extension__' keyword. */
27110 if (cp_parser_extension_opt (parser, &saved_pedantic))
27112 /* Recurse. */
27113 cp_parser_member_declaration (parser);
27114 /* Restore the old value of the PEDANTIC flag. */
27115 pedantic = saved_pedantic;
27117 return;
27120 /* Check for a template-declaration. */
27121 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27123 /* An explicit specialization here is an error condition, and we
27124 expect the specialization handler to detect and report this. */
27125 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
27126 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
27127 cp_parser_explicit_specialization (parser);
27128 else
27129 cp_parser_template_declaration (parser, /*member_p=*/true);
27131 return;
27133 /* Check for a template introduction. */
27134 else if (cp_parser_template_declaration_after_export (parser, true))
27135 return;
27137 /* Check for a using-declaration. */
27138 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
27140 if (cxx_dialect < cxx11)
27141 /* Parse the using-declaration. */
27142 cp_parser_using_declaration (parser, /*access_declaration_p=*/false);
27143 else if (cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_ENUM))
27144 cp_parser_using_enum (parser);
27145 else
27147 tree decl;
27148 bool alias_decl_expected;
27149 cp_parser_parse_tentatively (parser);
27150 decl = cp_parser_alias_declaration (parser);
27151 /* Note that if we actually see the '=' token after the
27152 identifier, cp_parser_alias_declaration commits the
27153 tentative parse. In that case, we really expect an
27154 alias-declaration. Otherwise, we expect a using
27155 declaration. */
27156 alias_decl_expected =
27157 !cp_parser_uncommitted_to_tentative_parse_p (parser);
27158 cp_parser_parse_definitely (parser);
27160 if (alias_decl_expected)
27161 finish_member_declaration (decl);
27162 else
27163 cp_parser_using_declaration (parser,
27164 /*access_declaration_p=*/false);
27166 return;
27169 /* Check for @defs. */
27170 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
27172 tree ivar, member;
27173 tree ivar_chains = cp_parser_objc_defs_expression (parser);
27174 ivar = ivar_chains;
27175 while (ivar)
27177 member = ivar;
27178 ivar = TREE_CHAIN (member);
27179 TREE_CHAIN (member) = NULL_TREE;
27180 finish_member_declaration (member);
27182 return;
27185 /* If the next token is `static_assert' we have a static assertion. */
27186 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
27188 cp_parser_static_assert (parser, /*member_p=*/true);
27189 return;
27192 parser->colon_corrects_to_scope_p = false;
27194 cp_omp_declare_simd_data odsd;
27195 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
27196 goto out;
27198 /* Parse the decl-specifier-seq. */
27199 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
27200 cp_parser_decl_specifier_seq (parser,
27201 (CP_PARSER_FLAGS_OPTIONAL
27202 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
27203 &decl_specifiers,
27204 &declares_class_or_enum);
27206 if (decl_specifiers.attributes && (flag_openmp || flag_openmp_simd))
27207 cp_parser_handle_directive_omp_attributes (parser,
27208 &decl_specifiers.attributes,
27209 &odsd, true);
27211 /* Check for an invalid type-name. */
27212 if (!decl_specifiers.any_type_specifiers_p
27213 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
27214 goto out;
27215 /* If there is no declarator, then the decl-specifier-seq should
27216 specify a type. */
27217 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27219 /* If there was no decl-specifier-seq, and the next token is a
27220 `;', then we have something like:
27222 struct S { ; };
27224 [class.mem]
27226 Each member-declaration shall declare at least one member
27227 name of the class. */
27228 if (!decl_specifiers.any_specifiers_p)
27230 cp_token *token = cp_lexer_peek_token (parser->lexer);
27231 if (!in_system_header_at (token->location))
27233 gcc_rich_location richloc (token->location);
27234 richloc.add_fixit_remove ();
27235 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
27238 else
27240 /* See if this declaration is a friend. */
27241 friend_p = cp_parser_friend_p (&decl_specifiers);
27242 /* If there were decl-specifiers, check to see if there was
27243 a class-declaration. */
27244 tree type = check_tag_decl (&decl_specifiers,
27245 /*explicit_type_instantiation_p=*/false);
27246 /* Nested classes have already been added to the class, but
27247 a `friend' needs to be explicitly registered. */
27248 if (friend_p)
27250 /* If the `friend' keyword was present, the friend must
27251 be introduced with a class-key. */
27252 if (!declares_class_or_enum && cxx_dialect < cxx11)
27253 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
27254 "in C++03 a class-key must be used "
27255 "when declaring a friend");
27256 /* In this case:
27258 template <typename T> struct A {
27259 friend struct A<T>::B;
27262 A<T>::B will be represented by a TYPENAME_TYPE, and
27263 therefore not recognized by check_tag_decl. */
27264 if (!type)
27266 type = decl_specifiers.type;
27267 if (type && TREE_CODE (type) == TYPE_DECL)
27268 type = TREE_TYPE (type);
27270 /* Warn if an attribute cannot appear here, as per
27271 [dcl.attr.grammar]/5. But not when declares_class_or_enum:
27272 we ignore attributes in elaborated-type-specifiers. */
27273 if (!declares_class_or_enum
27274 && cxx11_attribute_p (decl_specifiers.attributes))
27276 decl_specifiers.attributes = NULL_TREE;
27277 if (warning_at (decl_spec_token_start->location,
27278 OPT_Wattributes, "attribute ignored"))
27279 inform (decl_spec_token_start->location, "an attribute "
27280 "that appertains to a friend declaration that "
27281 "is not a definition is ignored");
27283 if (!type || !TYPE_P (type))
27284 error_at (decl_spec_token_start->location,
27285 "friend declaration does not name a class or "
27286 "function");
27287 else
27288 make_friend_class (current_class_type, type,
27289 /*complain=*/true);
27291 /* If there is no TYPE, an error message will already have
27292 been issued. */
27293 else if (!type || type == error_mark_node)
27295 /* An anonymous aggregate has to be handled specially; such
27296 a declaration really declares a data member (with a
27297 particular type), as opposed to a nested class. */
27298 else if (ANON_AGGR_TYPE_P (type))
27300 /* C++11 9.5/6. */
27301 if (decl_specifiers.storage_class != sc_none)
27302 error_at (decl_spec_token_start->location,
27303 "a storage class on an anonymous aggregate "
27304 "in class scope is not allowed");
27306 /* Remove constructors and such from TYPE, now that we
27307 know it is an anonymous aggregate. */
27308 fixup_anonymous_aggr (type);
27309 /* And make the corresponding data member. */
27310 decl = build_decl (decl_spec_token_start->location,
27311 FIELD_DECL, NULL_TREE, type);
27312 /* Add it to the class. */
27313 finish_member_declaration (decl);
27315 else
27316 cp_parser_check_access_in_redeclaration
27317 (TYPE_NAME (type),
27318 decl_spec_token_start->location);
27321 else
27323 bool assume_semicolon = false;
27325 /* Clear attributes from the decl_specifiers but keep them
27326 around as prefix attributes that apply them to the entity
27327 being declared. */
27328 prefix_attributes = decl_specifiers.attributes;
27329 decl_specifiers.attributes = NULL_TREE;
27330 if (parser->omp_declare_simd
27331 && (parser->omp_declare_simd->attribs[0]
27332 == &decl_specifiers.attributes))
27333 parser->omp_declare_simd->attribs[0] = &prefix_attributes;
27335 /* See if these declarations will be friends. */
27336 friend_p = cp_parser_friend_p (&decl_specifiers);
27338 /* Keep going until we hit the `;' at the end of the
27339 declaration. */
27340 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27342 tree attributes = NULL_TREE;
27343 tree first_attribute;
27344 tree initializer;
27345 bool named_bitfld = false;
27347 /* Peek at the next token. */
27348 token = cp_lexer_peek_token (parser->lexer);
27350 /* The following code wants to know early if it is a bit-field
27351 or some other declaration. Attributes can appear before
27352 the `:' token. Skip over them without consuming any tokens
27353 to peek if they are followed by `:'. */
27354 if (cp_next_tokens_can_be_attribute_p (parser)
27355 || (token->type == CPP_NAME
27356 && cp_nth_tokens_can_be_attribute_p (parser, 2)
27357 && (named_bitfld = true)))
27359 size_t n
27360 = cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
27361 token = cp_lexer_peek_nth_token (parser->lexer, n);
27364 /* Check for a bitfield declaration. */
27365 if (token->type == CPP_COLON
27366 || (token->type == CPP_NAME
27367 && token == cp_lexer_peek_token (parser->lexer)
27368 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
27369 && (named_bitfld = true)))
27371 tree identifier;
27372 tree width;
27373 tree late_attributes = NULL_TREE;
27374 location_t id_location
27375 = cp_lexer_peek_token (parser->lexer)->location;
27377 if (named_bitfld)
27378 identifier = cp_parser_identifier (parser);
27379 else
27380 identifier = NULL_TREE;
27382 /* Look for attributes that apply to the bitfield. */
27383 attributes = cp_parser_attributes_opt (parser);
27385 /* Consume the `:' token. */
27386 cp_lexer_consume_token (parser->lexer);
27388 /* Get the width of the bitfield. */
27389 width = cp_parser_constant_expression (parser, false, NULL,
27390 cxx_dialect >= cxx11);
27392 /* In C++20 and as extension for C++11 and above we allow
27393 default member initializers for bit-fields. */
27394 initializer = NULL_TREE;
27395 if (cxx_dialect >= cxx11
27396 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
27397 || cp_lexer_next_token_is (parser->lexer,
27398 CPP_OPEN_BRACE)))
27400 location_t loc
27401 = cp_lexer_peek_token (parser->lexer)->location;
27402 if (cxx_dialect < cxx20
27403 && identifier != NULL_TREE)
27404 pedwarn (loc, OPT_Wc__20_extensions,
27405 "default member initializers for bit-fields "
27406 "only available with %<-std=c++20%> or "
27407 "%<-std=gnu++20%>");
27409 initializer = cp_parser_save_nsdmi (parser);
27410 if (identifier == NULL_TREE)
27412 error_at (loc, "default member initializer for "
27413 "unnamed bit-field");
27414 initializer = NULL_TREE;
27417 else
27419 /* Look for attributes that apply to the bitfield after
27420 the `:' token and width. This is where GCC used to
27421 parse attributes in the past, pedwarn if there is
27422 a std attribute. */
27423 if (cp_next_tokens_can_be_std_attribute_p (parser))
27424 pedwarn (input_location, OPT_Wpedantic,
27425 "ISO C++ allows bit-field attributes only "
27426 "before the %<:%> token");
27428 late_attributes = cp_parser_attributes_opt (parser);
27431 attributes = attr_chainon (attributes, late_attributes);
27433 /* Remember which attributes are prefix attributes and
27434 which are not. */
27435 first_attribute = attributes;
27436 /* Combine the attributes. */
27437 attributes = attr_chainon (prefix_attributes, attributes);
27439 /* Create the bitfield declaration. */
27440 decl = grokbitfield (identifier
27441 ? make_id_declarator (NULL_TREE,
27442 identifier,
27443 sfk_none,
27444 id_location)
27445 : NULL,
27446 &decl_specifiers,
27447 width, initializer,
27448 attributes);
27450 else
27452 cp_declarator *declarator;
27453 tree asm_specification;
27454 int ctor_dtor_or_conv_p;
27455 bool static_p = (decl_specifiers.storage_class == sc_static);
27456 cp_parser_flags flags = CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
27457 /* We can't delay parsing for friends,
27458 alias-declarations, and typedefs, even though the
27459 standard seems to require it. */
27460 if (!friend_p
27461 && !decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
27462 flags |= CP_PARSER_FLAGS_DELAY_NOEXCEPT;
27464 /* Parse the declarator. */
27465 declarator
27466 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
27467 flags,
27468 &ctor_dtor_or_conv_p,
27469 /*parenthesized_p=*/NULL,
27470 /*member_p=*/true,
27471 friend_p, static_p);
27473 /* If something went wrong parsing the declarator, make sure
27474 that we at least consume some tokens. */
27475 if (declarator == cp_error_declarator)
27477 /* Skip to the end of the statement. */
27478 cp_parser_skip_to_end_of_statement (parser);
27479 /* If the next token is not a semicolon, that is
27480 probably because we just skipped over the body of
27481 a function. So, we consume a semicolon if
27482 present, but do not issue an error message if it
27483 is not present. */
27484 if (cp_lexer_next_token_is (parser->lexer,
27485 CPP_SEMICOLON))
27486 cp_lexer_consume_token (parser->lexer);
27487 goto out;
27490 /* Handle class-scope non-template C++17 deduction guides. */
27491 cp_parser_maybe_adjust_declarator_for_dguide (parser,
27492 &decl_specifiers,
27493 declarator,
27494 &ctor_dtor_or_conv_p);
27496 if (declares_class_or_enum & 2)
27497 cp_parser_check_for_definition_in_return_type
27498 (declarator, decl_specifiers.type,
27499 decl_specifiers.locations[ds_type_spec]);
27501 /* Look for an asm-specification. */
27502 asm_specification = cp_parser_asm_specification_opt (parser);
27503 /* Look for attributes that apply to the declaration. */
27504 attributes = cp_parser_attributes_opt (parser);
27505 /* Remember which attributes are prefix attributes and
27506 which are not. */
27507 first_attribute = attributes;
27508 /* Combine the attributes. */
27509 attributes = attr_chainon (prefix_attributes, attributes);
27511 /* If it's an `=', then we have a constant-initializer or a
27512 pure-specifier. It is not correct to parse the
27513 initializer before registering the member declaration
27514 since the member declaration should be in scope while
27515 its initializer is processed. However, the rest of the
27516 front end does not yet provide an interface that allows
27517 us to handle this correctly. */
27518 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
27520 /* In [class.mem]:
27522 A pure-specifier shall be used only in the declaration of
27523 a virtual function.
27525 A member-declarator can contain a constant-initializer
27526 only if it declares a static member of integral or
27527 enumeration type.
27529 Therefore, if the DECLARATOR is for a function, we look
27530 for a pure-specifier; otherwise, we look for a
27531 constant-initializer. When we call `grokfield', it will
27532 perform more stringent semantics checks. */
27533 initializer_token_start = cp_lexer_peek_token (parser->lexer);
27534 declarator->init_loc = initializer_token_start->location;
27535 if (function_declarator_p (declarator)
27536 || (decl_specifiers.type
27537 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
27538 && declarator->kind == cdk_id
27539 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
27540 == FUNCTION_TYPE)))
27541 initializer = cp_parser_pure_specifier (parser);
27542 else if (decl_specifiers.storage_class != sc_static)
27543 initializer = cp_parser_save_nsdmi (parser);
27544 else if (cxx_dialect >= cxx11)
27546 bool nonconst;
27547 /* Don't require a constant rvalue in C++11, since we
27548 might want a reference constant. We'll enforce
27549 constancy later. */
27550 cp_lexer_consume_token (parser->lexer);
27551 /* Parse the initializer. */
27552 initializer = cp_parser_initializer_clause (parser,
27553 &nonconst);
27555 else
27556 /* Parse the initializer. */
27557 initializer = cp_parser_constant_initializer (parser);
27559 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
27560 && !function_declarator_p (declarator))
27562 bool x;
27563 declarator->init_loc
27564 = cp_lexer_peek_token (parser->lexer)->location;
27565 if (decl_specifiers.storage_class != sc_static)
27566 initializer = cp_parser_save_nsdmi (parser);
27567 else
27568 initializer = cp_parser_initializer (parser, &x, &x);
27570 /* Detect invalid bit-field cases such as
27572 int *p : 4;
27573 int &&r : 3;
27575 and similar. */
27576 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
27577 /* If there were no type specifiers, it was a
27578 constructor. */
27579 && decl_specifiers.any_type_specifiers_p)
27581 /* This is called for a decent diagnostic only. */
27582 tree d = grokdeclarator (declarator, &decl_specifiers,
27583 BITFIELD, /*initialized=*/false,
27584 &attributes);
27585 if (!error_operand_p (d))
27586 error_at (DECL_SOURCE_LOCATION (d),
27587 "bit-field %qD has non-integral type %qT",
27588 d, TREE_TYPE (d));
27589 cp_parser_skip_to_end_of_statement (parser);
27590 /* Avoid "extra ;" pedwarns. */
27591 if (cp_lexer_next_token_is (parser->lexer,
27592 CPP_SEMICOLON))
27593 cp_lexer_consume_token (parser->lexer);
27594 goto out;
27596 /* Otherwise, there is no initializer. */
27597 else
27598 initializer = NULL_TREE;
27600 /* See if we are probably looking at a function
27601 definition. We are certainly not looking at a
27602 member-declarator. Calling `grokfield' has
27603 side-effects, so we must not do it unless we are sure
27604 that we are looking at a member-declarator. */
27605 if (cp_parser_token_starts_function_definition_p
27606 (cp_lexer_peek_token (parser->lexer)))
27608 /* The grammar does not allow a pure-specifier to be
27609 used when a member function is defined. (It is
27610 possible that this fact is an oversight in the
27611 standard, since a pure function may be defined
27612 outside of the class-specifier. */
27613 if (initializer && initializer_token_start)
27614 error_at (initializer_token_start->location,
27615 "pure-specifier on function-definition");
27616 decl = cp_parser_save_member_function_body (parser,
27617 &decl_specifiers,
27618 declarator,
27619 attributes);
27621 if (parser->fully_implicit_function_template_p)
27622 decl = finish_fully_implicit_template (parser, decl);
27623 /* If the member was not a friend, declare it here. */
27624 if (!friend_p)
27625 finish_member_declaration (decl);
27626 /* Peek at the next token. */
27627 token = cp_lexer_peek_token (parser->lexer);
27628 /* If the next token is a semicolon, consume it. */
27629 if (token->type == CPP_SEMICOLON)
27631 location_t semicolon_loc
27632 = cp_lexer_consume_token (parser->lexer)->location;
27633 gcc_rich_location richloc (semicolon_loc);
27634 richloc.add_fixit_remove ();
27635 warning_at (&richloc, OPT_Wextra_semi,
27636 "extra %<;%> after in-class "
27637 "function definition");
27639 goto out;
27641 else
27642 if (declarator->kind == cdk_function)
27643 declarator->id_loc = token->location;
27645 /* Create the declaration. */
27646 decl = grokfield (declarator, &decl_specifiers,
27647 initializer, /*init_const_expr_p=*/true,
27648 asm_specification, attributes);
27650 if (parser->fully_implicit_function_template_p)
27652 if (friend_p)
27653 finish_fully_implicit_template (parser, 0);
27654 else
27655 decl = finish_fully_implicit_template (parser, decl);
27659 cp_finalize_omp_declare_simd (parser, decl);
27660 cp_finalize_oacc_routine (parser, decl, false);
27662 /* Reset PREFIX_ATTRIBUTES. */
27663 if (attributes != error_mark_node)
27665 while (attributes && TREE_CHAIN (attributes) != first_attribute)
27666 attributes = TREE_CHAIN (attributes);
27667 if (attributes)
27668 TREE_CHAIN (attributes) = NULL_TREE;
27671 /* If there is any qualification still in effect, clear it
27672 now; we will be starting fresh with the next declarator. */
27673 parser->scope = NULL_TREE;
27674 parser->qualifying_scope = NULL_TREE;
27675 parser->object_scope = NULL_TREE;
27676 /* If it's a `,', then there are more declarators. */
27677 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27679 cp_lexer_consume_token (parser->lexer);
27680 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27682 cp_token *token = cp_lexer_previous_token (parser->lexer);
27683 gcc_rich_location richloc (token->location);
27684 richloc.add_fixit_remove ();
27685 error_at (&richloc, "stray %<,%> at end of "
27686 "member declaration");
27689 /* If the next token isn't a `;', then we have a parse error. */
27690 else if (cp_lexer_next_token_is_not (parser->lexer,
27691 CPP_SEMICOLON))
27693 /* The next token might be a ways away from where the
27694 actual semicolon is missing. Find the previous token
27695 and use that for our error position. */
27696 cp_token *token = cp_lexer_previous_token (parser->lexer);
27697 gcc_rich_location richloc (token->location);
27698 richloc.add_fixit_insert_after (";");
27699 error_at (&richloc, "expected %<;%> at end of "
27700 "member declaration");
27702 /* Assume that the user meant to provide a semicolon. If
27703 we were to cp_parser_skip_to_end_of_statement, we might
27704 skip to a semicolon inside a member function definition
27705 and issue nonsensical error messages. */
27706 assume_semicolon = true;
27709 if (decl)
27711 /* Add DECL to the list of members. */
27712 if (!friend_p
27713 /* Explicitly include, eg, NSDMIs, for better error
27714 recovery (c++/58650). */
27715 || !DECL_DECLARES_FUNCTION_P (decl))
27716 finish_member_declaration (decl);
27718 if (DECL_DECLARES_FUNCTION_P (decl))
27719 cp_parser_save_default_args (parser, STRIP_TEMPLATE (decl));
27720 else if (TREE_CODE (decl) == FIELD_DECL
27721 && DECL_INITIAL (decl))
27722 /* Add DECL to the queue of NSDMI to be parsed later. */
27723 vec_safe_push (unparsed_nsdmis, decl);
27726 if (assume_semicolon)
27727 goto out;
27731 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
27732 out:
27733 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27734 cp_finalize_omp_declare_simd (parser, &odsd);
27737 /* Parse a pure-specifier.
27739 pure-specifier:
27742 Returns INTEGER_ZERO_NODE if a pure specifier is found.
27743 Otherwise, ERROR_MARK_NODE is returned. */
27745 static tree
27746 cp_parser_pure_specifier (cp_parser* parser)
27748 cp_token *token;
27750 /* Look for the `=' token. */
27751 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
27752 return error_mark_node;
27753 /* Look for the `0' token. */
27754 token = cp_lexer_peek_token (parser->lexer);
27756 if (token->type == CPP_EOF
27757 || token->type == CPP_PRAGMA_EOL)
27758 return error_mark_node;
27760 cp_lexer_consume_token (parser->lexer);
27762 /* Accept = default or = delete in c++0x mode. */
27763 if (token->keyword == RID_DEFAULT
27764 || token->keyword == RID_DELETE)
27766 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
27767 return token->u.value;
27770 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
27771 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
27773 cp_parser_error (parser,
27774 "invalid pure specifier (only %<= 0%> is allowed)");
27775 cp_parser_skip_to_end_of_statement (parser);
27776 return error_mark_node;
27778 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
27780 error_at (token->location, "templates may not be %<virtual%>");
27781 return error_mark_node;
27784 return integer_zero_node;
27787 /* Parse a constant-initializer.
27789 constant-initializer:
27790 = constant-expression
27792 Returns a representation of the constant-expression. */
27794 static tree
27795 cp_parser_constant_initializer (cp_parser* parser)
27797 /* Look for the `=' token. */
27798 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
27799 return error_mark_node;
27801 /* It is invalid to write:
27803 struct S { static const int i = { 7 }; };
27806 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27808 cp_parser_error (parser,
27809 "a brace-enclosed initializer is not allowed here");
27810 /* Consume the opening brace. */
27811 matching_braces braces;
27812 braces.consume_open (parser);
27813 /* Skip the initializer. */
27814 cp_parser_skip_to_closing_brace (parser);
27815 /* Look for the trailing `}'. */
27816 braces.require_close (parser);
27818 return error_mark_node;
27821 return cp_parser_constant_expression (parser);
27824 /* Derived classes [gram.class.derived] */
27826 /* Parse a base-clause.
27828 base-clause:
27829 : base-specifier-list
27831 base-specifier-list:
27832 base-specifier ... [opt]
27833 base-specifier-list , base-specifier ... [opt]
27835 Returns a TREE_LIST representing the base-classes, in the order in
27836 which they were declared. The representation of each node is as
27837 described by cp_parser_base_specifier.
27839 In the case that no bases are specified, this function will return
27840 NULL_TREE, not ERROR_MARK_NODE. */
27842 static tree
27843 cp_parser_base_clause (cp_parser* parser)
27845 tree bases = NULL_TREE;
27847 /* Look for the `:' that begins the list. */
27848 cp_parser_require (parser, CPP_COLON, RT_COLON);
27850 /* Scan the base-specifier-list. */
27851 while (true)
27853 cp_token *token;
27854 tree base;
27855 bool pack_expansion_p = false;
27857 /* Look for the base-specifier. */
27858 base = cp_parser_base_specifier (parser);
27859 /* Look for the (optional) ellipsis. */
27860 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27862 /* Consume the `...'. */
27863 cp_lexer_consume_token (parser->lexer);
27865 pack_expansion_p = true;
27868 /* Add BASE to the front of the list. */
27869 if (base && base != error_mark_node)
27871 if (pack_expansion_p)
27872 /* Make this a pack expansion type. */
27873 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
27875 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
27877 TREE_CHAIN (base) = bases;
27878 bases = base;
27881 /* Peek at the next token. */
27882 token = cp_lexer_peek_token (parser->lexer);
27883 /* If it's not a comma, then the list is complete. */
27884 if (token->type != CPP_COMMA)
27885 break;
27886 /* Consume the `,'. */
27887 cp_lexer_consume_token (parser->lexer);
27890 /* PARSER->SCOPE may still be non-NULL at this point, if the last
27891 base class had a qualified name. However, the next name that
27892 appears is certainly not qualified. */
27893 parser->scope = NULL_TREE;
27894 parser->qualifying_scope = NULL_TREE;
27895 parser->object_scope = NULL_TREE;
27897 return nreverse (bases);
27900 /* Parse a base-specifier.
27902 base-specifier:
27903 :: [opt] nested-name-specifier [opt] class-name
27904 virtual access-specifier [opt] :: [opt] nested-name-specifier
27905 [opt] class-name
27906 access-specifier virtual [opt] :: [opt] nested-name-specifier
27907 [opt] class-name
27909 Returns a TREE_LIST. The TREE_PURPOSE will be one of
27910 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
27911 indicate the specifiers provided. The TREE_VALUE will be a TYPE
27912 (or the ERROR_MARK_NODE) indicating the type that was specified. */
27914 static tree
27915 cp_parser_base_specifier (cp_parser* parser)
27917 cp_token *token;
27918 bool done = false;
27919 bool virtual_p = false;
27920 bool duplicate_virtual_error_issued_p = false;
27921 bool duplicate_access_error_issued_p = false;
27922 bool class_scope_p, template_p;
27923 tree access = access_default_node;
27924 tree type;
27926 /* Process the optional `virtual' and `access-specifier'. */
27927 while (!done)
27929 /* Peek at the next token. */
27930 token = cp_lexer_peek_token (parser->lexer);
27931 /* Process `virtual'. */
27932 switch (token->keyword)
27934 case RID_VIRTUAL:
27935 /* If `virtual' appears more than once, issue an error. */
27936 if (virtual_p && !duplicate_virtual_error_issued_p)
27938 cp_parser_error (parser,
27939 "%<virtual%> specified more than once in base-specifier");
27940 duplicate_virtual_error_issued_p = true;
27943 virtual_p = true;
27945 /* Consume the `virtual' token. */
27946 cp_lexer_consume_token (parser->lexer);
27948 break;
27950 case RID_PUBLIC:
27951 case RID_PROTECTED:
27952 case RID_PRIVATE:
27953 /* If more than one access specifier appears, issue an
27954 error. */
27955 if (access != access_default_node
27956 && !duplicate_access_error_issued_p)
27958 cp_parser_error (parser,
27959 "more than one access specifier in base-specifier");
27960 duplicate_access_error_issued_p = true;
27963 access = ridpointers[(int) token->keyword];
27965 /* Consume the access-specifier. */
27966 cp_lexer_consume_token (parser->lexer);
27968 break;
27970 default:
27971 done = true;
27972 break;
27975 /* It is not uncommon to see programs mechanically, erroneously, use
27976 the 'typename' keyword to denote (dependent) qualified types
27977 as base classes. */
27978 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
27980 token = cp_lexer_peek_token (parser->lexer);
27981 if (!processing_template_decl)
27982 error_at (token->location,
27983 "keyword %<typename%> not allowed outside of templates");
27984 else
27985 error_at (token->location,
27986 "keyword %<typename%> not allowed in this context "
27987 "(the base class is implicitly a type)");
27988 cp_lexer_consume_token (parser->lexer);
27991 /* Look for the optional `::' operator. */
27992 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
27993 /* Look for the nested-name-specifier. The simplest way to
27994 implement:
27996 [temp.res]
27998 The keyword `typename' is not permitted in a base-specifier or
27999 mem-initializer; in these contexts a qualified name that
28000 depends on a template-parameter is implicitly assumed to be a
28001 type name.
28003 is to pretend that we have seen the `typename' keyword at this
28004 point. */
28005 cp_parser_nested_name_specifier_opt (parser,
28006 /*typename_keyword_p=*/true,
28007 /*check_dependency_p=*/true,
28008 /*type_p=*/true,
28009 /*is_declaration=*/true);
28010 /* If the base class is given by a qualified name, assume that names
28011 we see are type names or templates, as appropriate. */
28012 class_scope_p = (parser->scope && TYPE_P (parser->scope));
28013 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
28015 if (!parser->scope
28016 && cp_lexer_next_token_is_decltype (parser->lexer))
28017 /* DR 950 allows decltype as a base-specifier. */
28018 type = cp_parser_decltype (parser);
28019 else
28021 /* Otherwise, look for the class-name. */
28022 type = cp_parser_class_name (parser,
28023 class_scope_p,
28024 template_p,
28025 typename_type,
28026 /*check_dependency_p=*/true,
28027 /*class_head_p=*/false,
28028 /*is_declaration=*/true);
28029 type = TREE_TYPE (type);
28032 if (type == error_mark_node)
28033 return error_mark_node;
28035 return finish_base_specifier (type, access, virtual_p);
28038 /* Exception handling [gram.exception] */
28040 /* Save the tokens that make up the noexcept-specifier for a member-function.
28041 Returns a DEFERRED_PARSE. */
28043 static tree
28044 cp_parser_save_noexcept (cp_parser *parser)
28046 cp_token *first = parser->lexer->next_token;
28047 /* We want everything up to, including, the final ')'. */
28048 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0);
28049 cp_token *last = parser->lexer->next_token;
28051 /* As with default arguments and NSDMIs, make use of DEFERRED_PARSE
28052 to carry the information we will need. */
28053 tree expr = make_node (DEFERRED_PARSE);
28054 /* Save away the noexcept-specifier; we will process it when the
28055 class is complete. */
28056 DEFPARSE_TOKENS (expr) = cp_token_cache_new (first, last);
28057 DEFPARSE_INSTANTIATIONS (expr) = nullptr;
28058 expr = build_tree_list (expr, NULL_TREE);
28059 return expr;
28062 /* Used for late processing of noexcept-specifiers of member-functions.
28063 DEFAULT_ARG is the unparsed operand of a noexcept-specifier which
28064 we saved for later; parse it now. DECL is the declaration of the
28065 member function. */
28067 static tree
28068 cp_parser_late_noexcept_specifier (cp_parser *parser, tree default_arg)
28070 /* Make sure we've gotten something that hasn't been parsed yet. */
28071 gcc_assert (TREE_CODE (default_arg) == DEFERRED_PARSE);
28073 push_unparsed_function_queues (parser);
28075 /* Push the saved tokens for the noexcept-specifier onto the parser's
28076 lexer stack. */
28077 cp_token_cache *tokens = DEFPARSE_TOKENS (default_arg);
28078 cp_parser_push_lexer_for_tokens (parser, tokens);
28080 /* Parse the cached noexcept-specifier. */
28081 tree parsed_arg
28082 = cp_parser_noexcept_specification_opt (parser,
28083 CP_PARSER_FLAGS_NONE,
28084 /*require_constexpr=*/true,
28085 /*consumed_expr=*/NULL,
28086 /*return_cond=*/false);
28088 /* Revert to the main lexer. */
28089 cp_parser_pop_lexer (parser);
28091 /* Restore the queue. */
28092 pop_unparsed_function_queues (parser);
28094 /* And we're done. */
28095 return parsed_arg;
28098 /* Perform late checking of overriding function with respect to their
28099 noexcept-specifiers. TYPE is the class and FNDECL is the function
28100 that potentially overrides some virtual function with the same
28101 signature. */
28103 static void
28104 noexcept_override_late_checks (tree type, tree fndecl)
28106 tree binfo = TYPE_BINFO (type);
28107 tree base_binfo;
28109 if (DECL_STATIC_FUNCTION_P (fndecl))
28110 return;
28112 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
28114 tree basetype = BINFO_TYPE (base_binfo);
28116 if (!TYPE_POLYMORPHIC_P (basetype))
28117 continue;
28119 tree fn = look_for_overrides_here (basetype, fndecl);
28120 if (fn)
28121 maybe_check_overriding_exception_spec (fndecl, fn);
28125 /* Parse an (optional) noexcept-specification.
28127 noexcept-specification:
28128 noexcept ( constant-expression ) [opt]
28130 If no noexcept-specification is present, returns NULL_TREE.
28131 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
28132 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
28133 there are no parentheses. CONSUMED_EXPR will be set accordingly.
28134 Otherwise, returns a noexcept specification unless RETURN_COND is true,
28135 in which case a boolean condition is returned instead. The parser flags
28136 FLAGS is used to control parsing. QUALS are qualifiers indicating whether
28137 the (member) function is `const'. */
28139 static tree
28140 cp_parser_noexcept_specification_opt (cp_parser* parser,
28141 cp_parser_flags flags,
28142 bool require_constexpr,
28143 bool* consumed_expr,
28144 bool return_cond)
28146 cp_token *token;
28147 const char *saved_message;
28149 /* Peek at the next token. */
28150 token = cp_lexer_peek_token (parser->lexer);
28152 /* Is it a noexcept-specification? */
28153 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
28155 tree expr;
28157 /* [class.mem]/6 says that a noexcept-specifer (within the
28158 member-specification of the class) is a complete-class context of
28159 a class. So, if the noexcept-specifier has the optional expression,
28160 just save the tokens, and reparse this after we're done with the
28161 class. */
28163 if ((flags & CP_PARSER_FLAGS_DELAY_NOEXCEPT)
28164 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN)
28165 /* No need to delay parsing for a number literal or true/false. */
28166 && !((cp_lexer_nth_token_is (parser->lexer, 3, CPP_NUMBER)
28167 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
28168 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_PAREN))
28169 && at_class_scope_p ()
28170 && TYPE_BEING_DEFINED (current_class_type)
28171 && !LAMBDA_TYPE_P (current_class_type))
28172 return cp_parser_save_noexcept (parser);
28174 cp_lexer_consume_token (parser->lexer);
28176 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
28178 matching_parens parens;
28179 parens.consume_open (parser);
28181 if (require_constexpr)
28183 /* Types may not be defined in an exception-specification. */
28184 saved_message = parser->type_definition_forbidden_message;
28185 parser->type_definition_forbidden_message
28186 = G_("types may not be defined in an exception-specification");
28188 bool non_constant_p;
28189 expr
28190 = cp_parser_constant_expression (parser,
28191 /*allow_non_constant=*/true,
28192 &non_constant_p);
28193 if (non_constant_p
28194 && !require_potential_rvalue_constant_expression (expr))
28196 expr = NULL_TREE;
28197 return_cond = true;
28200 /* Restore the saved message. */
28201 parser->type_definition_forbidden_message = saved_message;
28203 else
28205 expr = cp_parser_expression (parser);
28206 *consumed_expr = true;
28209 parens.require_close (parser);
28211 else
28213 expr = boolean_true_node;
28214 if (!require_constexpr)
28215 *consumed_expr = false;
28218 /* We cannot build a noexcept-spec right away because this will check
28219 that expr is a constexpr. */
28220 if (!return_cond)
28221 return build_noexcept_spec (expr, tf_warning_or_error);
28222 else
28223 return expr;
28225 else
28226 return NULL_TREE;
28229 /* Parse an (optional) exception-specification.
28231 exception-specification:
28232 throw ( type-id-list [opt] )
28234 Returns a TREE_LIST representing the exception-specification. The
28235 TREE_VALUE of each node is a type. The parser flags FLAGS is used to
28236 control parsing. QUALS are qualifiers indicating whether the (member)
28237 function is `const'. */
28239 static tree
28240 cp_parser_exception_specification_opt (cp_parser* parser,
28241 cp_parser_flags flags)
28243 cp_token *token;
28244 tree type_id_list;
28245 const char *saved_message;
28247 /* Peek at the next token. */
28248 token = cp_lexer_peek_token (parser->lexer);
28250 /* Is it a noexcept-specification? */
28251 type_id_list
28252 = cp_parser_noexcept_specification_opt (parser, flags,
28253 /*require_constexpr=*/true,
28254 /*consumed_expr=*/NULL,
28255 /*return_cond=*/false);
28256 if (type_id_list != NULL_TREE)
28257 return type_id_list;
28259 /* If it's not `throw', then there's no exception-specification. */
28260 if (!cp_parser_is_keyword (token, RID_THROW))
28261 return NULL_TREE;
28263 location_t loc = token->location;
28265 /* Consume the `throw'. */
28266 cp_lexer_consume_token (parser->lexer);
28268 /* Look for the `('. */
28269 matching_parens parens;
28270 parens.require_open (parser);
28272 /* Peek at the next token. */
28273 token = cp_lexer_peek_token (parser->lexer);
28274 /* If it's not a `)', then there is a type-id-list. */
28275 if (token->type != CPP_CLOSE_PAREN)
28277 /* Types may not be defined in an exception-specification. */
28278 saved_message = parser->type_definition_forbidden_message;
28279 parser->type_definition_forbidden_message
28280 = G_("types may not be defined in an exception-specification");
28281 /* Parse the type-id-list. */
28282 type_id_list = cp_parser_type_id_list (parser);
28283 /* Restore the saved message. */
28284 parser->type_definition_forbidden_message = saved_message;
28286 if (cxx_dialect >= cxx17)
28288 error_at (loc, "ISO C++17 does not allow dynamic exception "
28289 "specifications");
28290 type_id_list = NULL_TREE;
28292 else if (cxx_dialect >= cxx11)
28293 warning_at (loc, OPT_Wdeprecated,
28294 "dynamic exception specifications are deprecated in "
28295 "C++11");
28297 /* In C++17, throw() is equivalent to noexcept (true). throw()
28298 is deprecated in C++11 and above as well, but is still widely used,
28299 so don't warn about it yet. */
28300 else if (cxx_dialect >= cxx17)
28301 type_id_list = noexcept_true_spec;
28302 else
28303 type_id_list = empty_except_spec;
28305 /* Look for the `)'. */
28306 parens.require_close (parser);
28308 return type_id_list;
28311 /* Parse an (optional) type-id-list.
28313 type-id-list:
28314 type-id ... [opt]
28315 type-id-list , type-id ... [opt]
28317 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
28318 in the order that the types were presented. */
28320 static tree
28321 cp_parser_type_id_list (cp_parser* parser)
28323 tree types = NULL_TREE;
28325 while (true)
28327 cp_token *token;
28328 tree type;
28330 token = cp_lexer_peek_token (parser->lexer);
28332 /* Get the next type-id. */
28333 type = cp_parser_type_id (parser);
28334 /* Check for invalid 'auto'. */
28335 if (flag_concepts && type_uses_auto (type))
28337 error_at (token->location,
28338 "invalid use of %<auto%> in exception-specification");
28339 type = error_mark_node;
28341 /* Parse the optional ellipsis. */
28342 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28344 /* Consume the `...'. */
28345 cp_lexer_consume_token (parser->lexer);
28347 /* Turn the type into a pack expansion expression. */
28348 type = make_pack_expansion (type);
28350 /* Add it to the list. */
28351 types = add_exception_specifier (types, type, /*complain=*/1);
28352 /* Peek at the next token. */
28353 token = cp_lexer_peek_token (parser->lexer);
28354 /* If it is not a `,', we are done. */
28355 if (token->type != CPP_COMMA)
28356 break;
28357 /* Consume the `,'. */
28358 cp_lexer_consume_token (parser->lexer);
28361 return nreverse (types);
28364 /* Parse a try-block.
28366 try-block:
28367 try compound-statement handler-seq */
28369 static tree
28370 cp_parser_try_block (cp_parser* parser)
28372 tree try_block;
28374 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
28375 if (parser->in_function_body
28376 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
28377 && cxx_dialect < cxx20)
28378 pedwarn (input_location, OPT_Wc__20_extensions,
28379 "%<try%> in %<constexpr%> function only "
28380 "available with %<-std=c++20%> or %<-std=gnu++20%>");
28382 try_block = begin_try_block ();
28383 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
28384 finish_try_block (try_block);
28385 cp_parser_handler_seq (parser);
28386 finish_handler_sequence (try_block);
28388 return try_block;
28391 /* Parse a function-try-block.
28393 function-try-block:
28394 try ctor-initializer [opt] function-body handler-seq */
28396 static void
28397 cp_parser_function_try_block (cp_parser* parser)
28399 tree compound_stmt;
28400 tree try_block;
28402 /* Look for the `try' keyword. */
28403 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
28404 return;
28405 /* Let the rest of the front end know where we are. */
28406 try_block = begin_function_try_block (&compound_stmt);
28407 /* Parse the function-body. */
28408 cp_parser_ctor_initializer_opt_and_function_body
28409 (parser, /*in_function_try_block=*/true);
28410 /* We're done with the `try' part. */
28411 finish_function_try_block (try_block);
28412 /* Parse the handlers. */
28413 cp_parser_handler_seq (parser);
28414 /* We're done with the handlers. */
28415 finish_function_handler_sequence (try_block, compound_stmt);
28418 /* Parse a handler-seq.
28420 handler-seq:
28421 handler handler-seq [opt] */
28423 static void
28424 cp_parser_handler_seq (cp_parser* parser)
28426 while (true)
28428 cp_token *token;
28430 /* Parse the handler. */
28431 cp_parser_handler (parser);
28432 /* Peek at the next token. */
28433 token = cp_lexer_peek_token (parser->lexer);
28434 /* If it's not `catch' then there are no more handlers. */
28435 if (!cp_parser_is_keyword (token, RID_CATCH))
28436 break;
28440 /* Parse a handler.
28442 handler:
28443 catch ( exception-declaration ) compound-statement */
28445 static void
28446 cp_parser_handler (cp_parser* parser)
28448 tree handler;
28449 tree declaration;
28451 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
28452 handler = begin_handler ();
28453 matching_parens parens;
28454 parens.require_open (parser);
28455 declaration = cp_parser_exception_declaration (parser);
28456 finish_handler_parms (declaration, handler);
28457 parens.require_close (parser);
28458 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
28459 finish_handler (handler);
28462 /* Parse an exception-declaration.
28464 exception-declaration:
28465 type-specifier-seq declarator
28466 type-specifier-seq abstract-declarator
28467 type-specifier-seq
28470 Returns a VAR_DECL for the declaration, or NULL_TREE if the
28471 ellipsis variant is used. */
28473 static tree
28474 cp_parser_exception_declaration (cp_parser* parser)
28476 cp_decl_specifier_seq type_specifiers;
28477 cp_declarator *declarator;
28478 const char *saved_message;
28480 /* If it's an ellipsis, it's easy to handle. */
28481 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28483 /* Consume the `...' token. */
28484 cp_lexer_consume_token (parser->lexer);
28485 return NULL_TREE;
28488 /* Types may not be defined in exception-declarations. */
28489 saved_message = parser->type_definition_forbidden_message;
28490 parser->type_definition_forbidden_message
28491 = G_("types may not be defined in exception-declarations");
28493 /* Parse the type-specifier-seq. */
28494 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
28495 /*is_declaration=*/true,
28496 /*is_trailing_return=*/false,
28497 &type_specifiers);
28498 /* If it's a `)', then there is no declarator. */
28499 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
28500 declarator = NULL;
28501 else
28502 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
28503 CP_PARSER_FLAGS_NONE,
28504 /*ctor_dtor_or_conv_p=*/NULL,
28505 /*parenthesized_p=*/NULL,
28506 /*member_p=*/false,
28507 /*friend_p=*/false,
28508 /*static_p=*/false);
28510 /* Restore the saved message. */
28511 parser->type_definition_forbidden_message = saved_message;
28513 if (!type_specifiers.any_specifiers_p)
28514 return error_mark_node;
28516 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
28519 /* Parse a throw-expression.
28521 throw-expression:
28522 throw assignment-expression [opt]
28524 Returns a THROW_EXPR representing the throw-expression. */
28526 static tree
28527 cp_parser_throw_expression (cp_parser* parser)
28529 tree expression;
28530 cp_token* token;
28531 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28533 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
28534 token = cp_lexer_peek_token (parser->lexer);
28535 /* Figure out whether or not there is an assignment-expression
28536 following the "throw" keyword. */
28537 if (token->type == CPP_COMMA
28538 || token->type == CPP_SEMICOLON
28539 || token->type == CPP_CLOSE_PAREN
28540 || token->type == CPP_CLOSE_SQUARE
28541 || token->type == CPP_CLOSE_BRACE
28542 || token->type == CPP_COLON)
28543 expression = NULL_TREE;
28544 else
28545 expression = cp_parser_assignment_expression (parser);
28547 /* Construct a location e.g.:
28548 throw x
28549 ^~~~~~~
28550 with caret == start at the start of the "throw" token, and
28551 the end at the end of the final token we consumed. */
28552 location_t combined_loc = make_location (start_loc, start_loc,
28553 parser->lexer);
28554 expression = build_throw (combined_loc, expression);
28556 return expression;
28559 /* Parse a yield-expression.
28561 yield-expression:
28562 co_yield assignment-expression
28563 co_yield braced-init-list
28565 Returns a CO_YIELD_EXPR representing the yield-expression. */
28567 static tree
28568 cp_parser_yield_expression (cp_parser* parser)
28570 tree expr;
28572 cp_token *token = cp_lexer_peek_token (parser->lexer);
28573 location_t kw_loc = token->location; /* Save for later. */
28575 cp_parser_require_keyword (parser, RID_CO_YIELD, RT_CO_YIELD);
28577 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28579 bool expr_non_constant_p;
28580 cp_lexer_set_source_position (parser->lexer);
28581 /* ??? : probably a moot point? */
28582 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
28583 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
28585 else
28586 expr = cp_parser_assignment_expression (parser);
28588 if (expr == error_mark_node)
28589 return expr;
28591 return finish_co_yield_expr (kw_loc, expr);
28594 /* GNU Extensions */
28596 /* Parse an (optional) asm-specification.
28598 asm-specification:
28599 asm ( string-literal )
28601 If the asm-specification is present, returns a STRING_CST
28602 corresponding to the string-literal. Otherwise, returns
28603 NULL_TREE. */
28605 static tree
28606 cp_parser_asm_specification_opt (cp_parser* parser)
28608 cp_token *token;
28609 tree asm_specification;
28611 /* Peek at the next token. */
28612 token = cp_lexer_peek_token (parser->lexer);
28613 /* If the next token isn't the `asm' keyword, then there's no
28614 asm-specification. */
28615 if (!cp_parser_is_keyword (token, RID_ASM))
28616 return NULL_TREE;
28618 /* Consume the `asm' token. */
28619 cp_lexer_consume_token (parser->lexer);
28620 /* Look for the `('. */
28621 matching_parens parens;
28622 parens.require_open (parser);
28624 /* Look for the string-literal. */
28625 asm_specification = cp_parser_string_literal (parser, false, false);
28627 /* Look for the `)'. */
28628 parens.require_close (parser);
28630 return asm_specification;
28633 /* Parse an asm-operand-list.
28635 asm-operand-list:
28636 asm-operand
28637 asm-operand-list , asm-operand
28639 asm-operand:
28640 string-literal ( expression )
28641 [ string-literal ] string-literal ( expression )
28643 Returns a TREE_LIST representing the operands. The TREE_VALUE of
28644 each node is the expression. The TREE_PURPOSE is itself a
28645 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
28646 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
28647 is a STRING_CST for the string literal before the parenthesis. Returns
28648 ERROR_MARK_NODE if any of the operands are invalid. */
28650 static tree
28651 cp_parser_asm_operand_list (cp_parser* parser)
28653 tree asm_operands = NULL_TREE;
28654 bool invalid_operands = false;
28656 while (true)
28658 tree string_literal;
28659 tree expression;
28660 tree name;
28662 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
28664 /* Consume the `[' token. */
28665 cp_lexer_consume_token (parser->lexer);
28666 /* Read the operand name. */
28667 name = cp_parser_identifier (parser);
28668 if (name != error_mark_node)
28669 name = build_string (IDENTIFIER_LENGTH (name),
28670 IDENTIFIER_POINTER (name));
28671 /* Look for the closing `]'. */
28672 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
28674 else
28675 name = NULL_TREE;
28676 /* Look for the string-literal. */
28677 string_literal = cp_parser_string_literal (parser, false, false);
28679 /* Look for the `('. */
28680 matching_parens parens;
28681 parens.require_open (parser);
28682 /* Parse the expression. */
28683 expression = cp_parser_expression (parser);
28684 /* Look for the `)'. */
28685 parens.require_close (parser);
28687 if (name == error_mark_node
28688 || string_literal == error_mark_node
28689 || expression == error_mark_node)
28690 invalid_operands = true;
28692 /* Add this operand to the list. */
28693 asm_operands = tree_cons (build_tree_list (name, string_literal),
28694 expression,
28695 asm_operands);
28696 /* If the next token is not a `,', there are no more
28697 operands. */
28698 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
28699 break;
28700 /* Consume the `,'. */
28701 cp_lexer_consume_token (parser->lexer);
28704 return invalid_operands ? error_mark_node : nreverse (asm_operands);
28707 /* Parse an asm-clobber-list.
28709 asm-clobber-list:
28710 string-literal
28711 asm-clobber-list , string-literal
28713 Returns a TREE_LIST, indicating the clobbers in the order that they
28714 appeared. The TREE_VALUE of each node is a STRING_CST. */
28716 static tree
28717 cp_parser_asm_clobber_list (cp_parser* parser)
28719 tree clobbers = NULL_TREE;
28721 while (true)
28723 tree string_literal;
28725 /* Look for the string literal. */
28726 string_literal = cp_parser_string_literal (parser, false, false);
28727 /* Add it to the list. */
28728 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
28729 /* If the next token is not a `,', then the list is
28730 complete. */
28731 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
28732 break;
28733 /* Consume the `,' token. */
28734 cp_lexer_consume_token (parser->lexer);
28737 return clobbers;
28740 /* Parse an asm-label-list.
28742 asm-label-list:
28743 identifier
28744 asm-label-list , identifier
28746 Returns a TREE_LIST, indicating the labels in the order that they
28747 appeared. The TREE_VALUE of each node is a label. */
28749 static tree
28750 cp_parser_asm_label_list (cp_parser* parser)
28752 tree labels = NULL_TREE;
28754 while (true)
28756 tree identifier, label, name;
28758 /* Look for the identifier. */
28759 identifier = cp_parser_identifier (parser);
28760 if (!error_operand_p (identifier))
28762 label = lookup_label (identifier);
28763 if (TREE_CODE (label) == LABEL_DECL)
28765 TREE_USED (label) = 1;
28766 check_goto (label);
28767 name = build_string (IDENTIFIER_LENGTH (identifier),
28768 IDENTIFIER_POINTER (identifier));
28769 labels = tree_cons (name, label, labels);
28772 /* If the next token is not a `,', then the list is
28773 complete. */
28774 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
28775 break;
28776 /* Consume the `,' token. */
28777 cp_lexer_consume_token (parser->lexer);
28780 return nreverse (labels);
28783 /* Return TRUE iff the next tokens in the stream are possibly the
28784 beginning of a GNU extension attribute. */
28786 static bool
28787 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
28789 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
28792 /* Return TRUE iff the next tokens in the stream are possibly the
28793 beginning of a standard C++-11 attribute specifier. */
28795 static bool
28796 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
28798 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
28801 /* Return TRUE iff the next Nth tokens in the stream are possibly the
28802 beginning of a standard C++-11 attribute specifier. */
28804 static bool
28805 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
28807 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
28809 return (cxx_dialect >= cxx11
28810 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
28811 || (token->type == CPP_OPEN_SQUARE
28812 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
28813 && token->type == CPP_OPEN_SQUARE)));
28816 /* Return TRUE iff the next Nth tokens in the stream are possibly the
28817 beginning of a GNU extension attribute. */
28819 static bool
28820 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
28822 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
28824 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
28827 /* Return true iff the next tokens can be the beginning of either a
28828 GNU attribute list, or a standard C++11 attribute sequence. */
28830 static bool
28831 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
28833 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
28834 || cp_next_tokens_can_be_std_attribute_p (parser));
28837 /* Return true iff the next Nth tokens can be the beginning of either
28838 a GNU attribute list, or a standard C++11 attribute sequence. */
28840 static bool
28841 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
28843 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
28844 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
28847 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
28848 of GNU attributes, or return NULL. */
28850 static tree
28851 cp_parser_attributes_opt (cp_parser *parser)
28853 tree attrs = NULL_TREE;
28854 while (true)
28856 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
28857 attrs = attr_chainon (attrs, cp_parser_gnu_attributes_opt (parser));
28858 else if (cp_next_tokens_can_be_std_attribute_p (parser))
28859 attrs = attr_chainon (attrs, cp_parser_std_attribute_spec_seq (parser));
28860 else
28861 break;
28863 return attrs;
28866 /* Parse an (optional) series of attributes.
28868 attributes:
28869 attributes attribute
28871 attribute:
28872 __attribute__ (( attribute-list [opt] ))
28874 The return value is as for cp_parser_gnu_attribute_list. */
28876 static tree
28877 cp_parser_gnu_attributes_opt (cp_parser* parser)
28879 tree attributes = NULL_TREE;
28881 auto cleanup = make_temp_override
28882 (parser->auto_is_implicit_function_template_parm_p, false);
28884 while (true)
28886 cp_token *token;
28887 tree attribute_list;
28888 bool ok = true;
28890 /* Peek at the next token. */
28891 token = cp_lexer_peek_token (parser->lexer);
28892 /* If it's not `__attribute__', then we're done. */
28893 if (token->keyword != RID_ATTRIBUTE)
28894 break;
28896 /* Consume the `__attribute__' keyword. */
28897 cp_lexer_consume_token (parser->lexer);
28898 /* Look for the two `(' tokens. */
28899 matching_parens outer_parens;
28900 if (!outer_parens.require_open (parser))
28901 ok = false;
28902 matching_parens inner_parens;
28903 if (!inner_parens.require_open (parser))
28904 ok = false;
28906 /* Peek at the next token. */
28907 token = cp_lexer_peek_token (parser->lexer);
28908 if (token->type != CPP_CLOSE_PAREN)
28909 /* Parse the attribute-list. */
28910 attribute_list = cp_parser_gnu_attribute_list (parser);
28911 else
28912 /* If the next token is a `)', then there is no attribute
28913 list. */
28914 attribute_list = NULL;
28916 /* Look for the two `)' tokens. */
28917 if (!inner_parens.require_close (parser))
28918 ok = false;
28919 if (!outer_parens.require_close (parser))
28920 ok = false;
28921 if (!ok)
28922 cp_parser_skip_to_end_of_statement (parser);
28924 /* Add these new attributes to the list. */
28925 attributes = attr_chainon (attributes, attribute_list);
28928 return attributes;
28931 /* Parse a GNU attribute-list.
28933 attribute-list:
28934 attribute
28935 attribute-list , attribute
28937 attribute:
28938 identifier
28939 identifier ( identifier )
28940 identifier ( identifier , expression-list )
28941 identifier ( expression-list )
28943 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
28944 to an attribute. The TREE_PURPOSE of each node is the identifier
28945 indicating which attribute is in use. The TREE_VALUE represents
28946 the arguments, if any. */
28948 static tree
28949 cp_parser_gnu_attribute_list (cp_parser* parser, bool exactly_one /* = false */)
28951 tree attribute_list = NULL_TREE;
28952 bool save_translate_strings_p = parser->translate_strings_p;
28954 /* Don't create wrapper nodes within attributes: the
28955 handlers don't know how to handle them. */
28956 auto_suppress_location_wrappers sentinel;
28958 parser->translate_strings_p = false;
28959 while (true)
28961 cp_token *token;
28962 tree identifier;
28963 tree attribute;
28965 /* Look for the identifier. We also allow keywords here; for
28966 example `__attribute__ ((const))' is legal. */
28967 token = cp_lexer_peek_token (parser->lexer);
28968 if (token->type == CPP_NAME
28969 || token->type == CPP_KEYWORD)
28971 tree arguments = NULL_TREE;
28973 /* Consume the token, but save it since we need it for the
28974 SIMD enabled function parsing. */
28975 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
28977 /* Save away the identifier that indicates which attribute
28978 this is. */
28979 identifier = (token->type == CPP_KEYWORD)
28980 /* For keywords, use the canonical spelling, not the
28981 parsed identifier. */
28982 ? ridpointers[(int) token->keyword]
28983 : id_token->u.value;
28985 identifier = canonicalize_attr_name (identifier);
28986 attribute = build_tree_list (identifier, NULL_TREE);
28988 /* Peek at the next token. */
28989 token = cp_lexer_peek_token (parser->lexer);
28990 /* If it's an `(', then parse the attribute arguments. */
28991 if (token->type == CPP_OPEN_PAREN)
28993 vec<tree, va_gc> *vec;
28994 int attr_flag = (attribute_takes_identifier_p (identifier)
28995 ? id_attr : normal_attr);
28996 if (is_attribute_p ("assume", identifier))
28997 attr_flag = assume_attr;
28998 vec = cp_parser_parenthesized_expression_list
28999 (parser, attr_flag, /*cast_p=*/false,
29000 /*allow_expansion_p=*/false,
29001 /*non_constant_p=*/NULL);
29002 if (vec == NULL)
29003 arguments = error_mark_node;
29004 else
29006 arguments = build_tree_list_vec (vec);
29007 release_tree_vector (vec);
29009 /* Save the arguments away. */
29010 TREE_VALUE (attribute) = arguments;
29013 if (arguments != error_mark_node)
29015 /* Add this attribute to the list. */
29016 TREE_CHAIN (attribute) = attribute_list;
29017 attribute_list = attribute;
29020 token = cp_lexer_peek_token (parser->lexer);
29022 /* Unless EXACTLY_ONE is set look for more attributes.
29023 If the next token isn't a `,', we're done. */
29024 if (exactly_one || token->type != CPP_COMMA)
29025 break;
29027 /* Consume the comma and keep going. */
29028 cp_lexer_consume_token (parser->lexer);
29030 parser->translate_strings_p = save_translate_strings_p;
29032 /* We built up the list in reverse order. */
29033 return nreverse (attribute_list);
29036 /* Parse arguments of omp::directive attribute.
29038 ( directive-name ,[opt] clause-list[opt] )
29040 For directive just remember the first/last tokens for subsequent
29041 parsing. */
29043 static void
29044 cp_parser_omp_directive_args (cp_parser *parser, tree attribute)
29046 cp_token *first = cp_lexer_peek_nth_token (parser->lexer, 2);
29047 if (first->type == CPP_CLOSE_PAREN)
29049 cp_lexer_consume_token (parser->lexer);
29050 error_at (first->location, "expected OpenMP directive name");
29051 cp_lexer_consume_token (parser->lexer);
29052 TREE_VALUE (attribute) = NULL_TREE;
29053 return;
29055 size_t n = cp_parser_skip_balanced_tokens (parser, 1);
29056 if (n == 1)
29058 cp_lexer_consume_token (parser->lexer);
29059 error_at (first->location, "expected attribute argument as balanced "
29060 "token sequence");
29061 TREE_VALUE (attribute) = NULL_TREE;
29062 return;
29064 for (n = n - 2; n; --n)
29065 cp_lexer_consume_token (parser->lexer);
29066 cp_token *last = cp_lexer_peek_token (parser->lexer);
29067 cp_lexer_consume_token (parser->lexer);
29068 tree arg = make_node (DEFERRED_PARSE);
29069 DEFPARSE_TOKENS (arg) = cp_token_cache_new (first, last);
29070 DEFPARSE_INSTANTIATIONS (arg) = nullptr;
29071 TREE_VALUE (attribute) = tree_cons (NULL_TREE, arg, TREE_VALUE (attribute));
29074 /* Parse arguments of omp::sequence attribute.
29076 ( omp::[opt] directive-attr [ , omp::[opt] directive-attr ]... ) */
29078 static void
29079 cp_parser_omp_sequence_args (cp_parser *parser, tree attribute)
29081 matching_parens parens;
29082 parens.consume_open (parser);
29085 cp_token *token = cp_lexer_peek_token (parser->lexer);
29086 if (token->type == CPP_NAME
29087 && token->u.value == omp_identifier
29088 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
29090 cp_lexer_consume_token (parser->lexer);
29091 cp_lexer_consume_token (parser->lexer);
29092 token = cp_lexer_peek_token (parser->lexer);
29094 bool directive = false;
29095 const char *p;
29096 if (token->type != CPP_NAME)
29097 p = "";
29098 else
29099 p = IDENTIFIER_POINTER (token->u.value);
29100 if (strcmp (p, "directive") == 0)
29101 directive = true;
29102 else if (strcmp (p, "sequence") != 0)
29104 error_at (token->location, "expected %<directive%> or %<sequence%>");
29105 cp_parser_skip_to_closing_parenthesis (parser,
29106 /*recovering=*/true,
29107 /*or_comma=*/true,
29108 /*consume_paren=*/false);
29109 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
29110 break;
29111 cp_lexer_consume_token (parser->lexer);
29113 cp_lexer_consume_token (parser->lexer);
29114 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
29115 cp_parser_required_error (parser, RT_OPEN_PAREN, false,
29116 UNKNOWN_LOCATION);
29117 else if (directive)
29118 cp_parser_omp_directive_args (parser, attribute);
29119 else
29120 cp_parser_omp_sequence_args (parser, attribute);
29121 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
29122 break;
29123 cp_lexer_consume_token (parser->lexer);
29125 while (1);
29126 if (!parens.require_close (parser))
29127 cp_parser_skip_to_closing_parenthesis (parser, true, false,
29128 /*consume_paren=*/true);
29131 /* Parse a standard C++11 attribute.
29133 The returned representation is a TREE_LIST which TREE_PURPOSE is
29134 the scoped name of the attribute, and the TREE_VALUE is its
29135 arguments list.
29137 Note that the scoped name of the attribute is itself a TREE_LIST
29138 which TREE_PURPOSE is the namespace of the attribute, and
29139 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
29140 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
29141 and which TREE_PURPOSE is directly the attribute name.
29143 Clients of the attribute code should use get_attribute_namespace
29144 and get_attribute_name to get the actual namespace and name of
29145 attributes, regardless of their being GNU or C++11 attributes.
29147 attribute:
29148 attribute-token attribute-argument-clause [opt]
29150 attribute-token:
29151 identifier
29152 attribute-scoped-token
29154 attribute-scoped-token:
29155 attribute-namespace :: identifier
29157 attribute-namespace:
29158 identifier
29160 attribute-argument-clause:
29161 ( balanced-token-seq )
29163 balanced-token-seq:
29164 balanced-token [opt]
29165 balanced-token-seq balanced-token
29167 balanced-token:
29168 ( balanced-token-seq )
29169 [ balanced-token-seq ]
29170 { balanced-token-seq }. */
29172 static tree
29173 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
29175 tree attribute, attr_id = NULL_TREE, arguments;
29176 cp_token *token;
29178 auto cleanup = make_temp_override
29179 (parser->auto_is_implicit_function_template_parm_p, false);
29181 /* First, parse name of the attribute, a.k.a attribute-token. */
29183 token = cp_lexer_peek_token (parser->lexer);
29184 if (token->type == CPP_NAME)
29185 attr_id = token->u.value;
29186 else if (token->type == CPP_KEYWORD)
29187 attr_id = ridpointers[(int) token->keyword];
29188 else if (token->flags & NAMED_OP)
29189 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
29191 if (attr_id == NULL_TREE)
29192 return NULL_TREE;
29194 cp_lexer_consume_token (parser->lexer);
29196 token = cp_lexer_peek_token (parser->lexer);
29197 if (token->type == CPP_SCOPE)
29199 /* We are seeing a scoped attribute token. */
29201 cp_lexer_consume_token (parser->lexer);
29202 if (attr_ns)
29203 error_at (token->location, "attribute using prefix used together "
29204 "with scoped attribute token");
29205 attr_ns = attr_id;
29207 token = cp_lexer_peek_token (parser->lexer);
29208 if (token->type == CPP_NAME)
29209 attr_id = token->u.value;
29210 else if (token->type == CPP_KEYWORD)
29211 attr_id = ridpointers[(int) token->keyword];
29212 else if (token->flags & NAMED_OP)
29213 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
29214 else
29216 error_at (token->location,
29217 "expected an identifier for the attribute name");
29218 return error_mark_node;
29220 cp_lexer_consume_token (parser->lexer);
29222 attr_ns = canonicalize_attr_name (attr_ns);
29223 attr_id = canonicalize_attr_name (attr_id);
29224 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
29225 NULL_TREE);
29226 token = cp_lexer_peek_token (parser->lexer);
29228 else if (attr_ns)
29230 attr_ns = canonicalize_attr_name (attr_ns);
29231 attr_id = canonicalize_attr_name (attr_id);
29232 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
29233 NULL_TREE);
29235 else
29237 attr_id = canonicalize_attr_name (attr_id);
29238 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
29239 NULL_TREE);
29241 /* We used to treat C++11 noreturn attribute as equivalent to GNU's,
29242 but no longer: we have to be able to tell [[noreturn]] and
29243 __attribute__((noreturn)) apart. */
29244 /* C++14 deprecated attribute is equivalent to GNU's. */
29245 if (is_attribute_p ("deprecated", attr_id))
29246 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
29247 /* C++17 fallthrough attribute is equivalent to GNU's. */
29248 else if (is_attribute_p ("fallthrough", attr_id))
29249 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
29250 /* C++23 assume attribute is equivalent to GNU's. */
29251 else if (is_attribute_p ("assume", attr_id))
29252 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
29253 /* Transactional Memory TS optimize_for_synchronized attribute is
29254 equivalent to GNU transaction_callable. */
29255 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
29256 TREE_PURPOSE (attribute)
29257 = get_identifier ("transaction_callable");
29258 /* Transactional Memory attributes are GNU attributes. */
29259 else if (tm_attr_to_mask (attr_id))
29260 TREE_PURPOSE (attribute) = attr_id;
29263 /* Now parse the optional argument clause of the attribute. */
29265 if (token->type != CPP_OPEN_PAREN)
29267 if ((flag_openmp || flag_openmp_simd)
29268 && attr_ns == omp_identifier
29269 && (is_attribute_p ("directive", attr_id)
29270 || is_attribute_p ("sequence", attr_id)))
29272 error_at (token->location, "%<omp::%E%> attribute requires argument",
29273 attr_id);
29274 return NULL_TREE;
29276 return attribute;
29280 vec<tree, va_gc> *vec;
29281 int attr_flag = normal_attr;
29283 /* Maybe we don't expect to see any arguments for this attribute. */
29284 const attribute_spec *as
29285 = lookup_attribute_spec (TREE_PURPOSE (attribute));
29286 if (as && as->max_length == 0)
29288 error_at (token->location, "%qE attribute does not take any arguments",
29289 attr_id);
29290 cp_parser_skip_to_closing_parenthesis (parser,
29291 /*recovering=*/true,
29292 /*or_comma=*/false,
29293 /*consume_paren=*/true);
29294 return error_mark_node;
29297 if (is_attribute_p ("assume", attr_id)
29298 && (attr_ns == NULL_TREE || attr_ns == gnu_identifier))
29299 /* The assume attribute needs special handling of the argument. */
29300 attr_flag = assume_attr;
29301 else if (attr_ns == gnu_identifier
29302 && attribute_takes_identifier_p (attr_id))
29303 /* A GNU attribute that takes an identifier in parameter. */
29304 attr_flag = id_attr;
29306 /* If this is a fake attribute created to handle -Wno-attributes,
29307 we must skip parsing the arguments. */
29308 if (as == NULL || attribute_ignored_p (as))
29310 if ((flag_openmp || flag_openmp_simd) && attr_ns == omp_identifier)
29312 if (is_attribute_p ("directive", attr_id))
29314 cp_parser_omp_directive_args (parser, attribute);
29315 return attribute;
29317 else if (is_attribute_p ("sequence", attr_id))
29319 TREE_VALUE (TREE_PURPOSE (attribute))
29320 = get_identifier ("directive");
29321 cp_parser_omp_sequence_args (parser, attribute);
29322 TREE_VALUE (attribute) = nreverse (TREE_VALUE (attribute));
29323 return attribute;
29327 /* For unknown attributes, just skip balanced tokens instead of
29328 trying to parse the arguments. */
29329 for (size_t n = cp_parser_skip_balanced_tokens (parser, 1) - 1; n; --n)
29330 cp_lexer_consume_token (parser->lexer);
29331 return attribute;
29334 vec = cp_parser_parenthesized_expression_list
29335 (parser, attr_flag, /*cast_p=*/false,
29336 /*allow_expansion_p=*/true,
29337 /*non_constant_p=*/NULL);
29338 if (vec == NULL)
29339 arguments = error_mark_node;
29340 else
29342 if (vec->is_empty ())
29343 /* e.g. [[attr()]]. */
29344 error_at (token->location, "parentheses must be omitted if "
29345 "%qE attribute argument list is empty",
29346 attr_id);
29347 arguments = build_tree_list_vec (vec);
29348 release_tree_vector (vec);
29351 if (arguments == error_mark_node)
29352 attribute = error_mark_node;
29353 else
29354 TREE_VALUE (attribute) = arguments;
29357 return attribute;
29360 /* Warn if the attribute ATTRIBUTE appears more than once in the
29361 attribute-list ATTRIBUTES. This used to be enforced for certain
29362 attributes, but the restriction was removed in P2156.
29363 LOC is the location of ATTRIBUTE. Returns true if ATTRIBUTE was not
29364 found in ATTRIBUTES. */
29366 static bool
29367 cp_parser_check_std_attribute (location_t loc, tree attributes, tree attribute)
29369 static auto alist = { "noreturn", "deprecated", "nodiscard", "maybe_unused",
29370 "likely", "unlikely", "fallthrough",
29371 "no_unique_address", "carries_dependency" };
29372 if (attributes)
29373 for (const auto &a : alist)
29374 if (is_attribute_p (a, get_attribute_name (attribute))
29375 && is_attribute_namespace_p ("", attribute)
29376 && lookup_attribute ("", a, attributes))
29378 if (!from_macro_expansion_at (loc))
29379 warning_at (loc, OPT_Wattributes, "attribute %qs specified "
29380 "multiple times", a);
29381 return false;
29383 return true;
29386 /* Parse a list of standard C++-11 attributes.
29388 attribute-list:
29389 attribute [opt]
29390 attribute-list , attribute[opt]
29391 attribute ...
29392 attribute-list , attribute ...
29395 static tree
29396 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
29398 tree attributes = NULL_TREE, attribute = NULL_TREE;
29399 cp_token *token = NULL;
29401 while (true)
29403 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29404 attribute = cp_parser_std_attribute (parser, attr_ns);
29405 if (attribute == error_mark_node)
29406 break;
29407 if (attribute != NULL_TREE)
29409 if (cp_parser_check_std_attribute (loc, attributes, attribute))
29411 TREE_CHAIN (attribute) = attributes;
29412 attributes = attribute;
29415 token = cp_lexer_peek_token (parser->lexer);
29416 if (token->type == CPP_ELLIPSIS)
29418 cp_lexer_consume_token (parser->lexer);
29419 if (attribute == NULL_TREE)
29420 error_at (token->location,
29421 "expected attribute before %<...%>");
29422 else
29424 tree pack = make_pack_expansion (TREE_VALUE (attribute));
29425 if (pack == error_mark_node)
29426 return error_mark_node;
29427 TREE_VALUE (attribute) = pack;
29429 token = cp_lexer_peek_token (parser->lexer);
29431 if (token->type != CPP_COMMA)
29432 break;
29433 cp_lexer_consume_token (parser->lexer);
29435 attributes = nreverse (attributes);
29436 return attributes;
29439 /* Optionally parse a C++20 contract role. A NULL return means that no
29440 contract role was specified.
29442 contract-role:
29443 % default
29444 % identifier
29446 If the identifier does not name a known contract role, it will
29447 be assumed to be default. Returns the identifier for the role
29448 token. */
29450 static tree
29451 cp_parser_contract_role (cp_parser *parser)
29453 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_MOD));
29454 cp_lexer_consume_token (parser->lexer);
29456 cp_token *token = cp_lexer_peek_token (parser->lexer);
29457 tree role_id = NULL_TREE;
29458 if (token->type == CPP_NAME)
29459 role_id = token->u.value;
29460 else if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
29461 role_id = get_identifier ("default");
29462 else
29464 error_at (token->location, "expected contract-role");
29465 return error_mark_node;
29467 cp_lexer_consume_token (parser->lexer);
29469 /* FIXME: Warn about invalid/unknown roles? */
29470 return role_id;
29473 /* Parse an optional contract mode.
29475 contract-mode:
29476 contract-semantic
29477 [contract-level] [contract-role]
29479 contract-semantic:
29480 check_never_continue
29481 check_maybe_continue
29482 check_always_continue
29484 contract-level:
29485 default
29486 audit
29487 axiom
29489 contract-role:
29490 default
29491 identifier
29493 This grammar is taken from P1332R0. During parsing, this sets options
29494 on the MODE object to determine the configuration of the contract.
29496 Returns a tree containing the identifiers used in the configuration.
29497 This is either an IDENTIFIER with the literal semantic or a TREE_LIST
29498 whose TREE_VALUE is the contract-level and whose TREE_PURPOSE is the
29499 contract-role, if any. NULL_TREE is returned if no information is
29500 given (i.e., all defaults selected). */
29502 static tree
29503 cp_parser_contract_mode_opt (cp_parser *parser,
29504 bool postcondition_p)
29506 /* The mode is empty; the level and role are default. */
29507 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
29508 return NULL_TREE;
29510 /* There is only a role; the level is default. */
29511 if (cp_lexer_next_token_is (parser->lexer, CPP_MOD))
29513 tree role_id = cp_parser_contract_role (parser);
29514 return build_tree_list (role_id, get_identifier ("default"));
29517 /* Otherwise, match semantic or level. */
29518 cp_token *token = cp_lexer_peek_token (parser->lexer);
29519 contract_level level = CONTRACT_INVALID;
29520 contract_semantic semantic = CCS_INVALID;
29521 tree config_id;
29522 if (token->type == CPP_NAME)
29524 config_id = token->u.value;
29526 /* Either a named level, a concrete semantic, or an identifier
29527 for a postcondition. */
29528 const char *ident = IDENTIFIER_POINTER (token->u.value);
29529 level = map_contract_level (ident);
29530 semantic = map_contract_semantic (ident);
29532 /* The identifier is the return value for a postcondition. */
29533 if (level == CONTRACT_INVALID && semantic == CCS_INVALID
29534 && postcondition_p)
29535 return NULL_TREE;
29537 else if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
29539 config_id = get_identifier ("default");
29540 level = CONTRACT_DEFAULT;
29542 else
29544 /* We got some other token other than a ':'. */
29545 error_at (token->location, "expected contract semantic or level");
29546 return NULL_TREE;
29549 /* Consume the literal semantic or level token. */
29550 cp_lexer_consume_token (parser->lexer);
29552 if (semantic == CCS_INVALID && level == CONTRACT_INVALID)
29554 error_at (token->location,
29555 "expected contract level: "
29556 "%<default%>, %<audit%>, or %<axiom%>");
29557 return NULL_TREE;
29560 /* We matched an explicit semantic. */
29561 if (semantic != CCS_INVALID)
29563 if (cp_lexer_next_token_is (parser->lexer, CPP_MOD))
29565 error ("invalid use of contract role for explicit semantic");
29566 cp_lexer_consume_token (parser->lexer);
29567 cp_lexer_consume_token (parser->lexer);
29569 return config_id;
29572 /* We matched a level, there may be a role; otherwise this is default. */
29573 if (cp_lexer_next_token_is (parser->lexer, CPP_MOD))
29575 tree role_id = cp_parser_contract_role (parser);
29576 return build_tree_list (role_id, config_id);
29579 return build_tree_list (NULL_TREE, config_id);
29582 static tree
29583 find_error (tree *tp, int *, void *)
29585 if (*tp == error_mark_node)
29586 return *tp;
29587 return NULL_TREE;
29590 static bool
29591 contains_error_p (tree t)
29593 return walk_tree (&t, find_error, NULL, NULL);
29596 /* Parse a standard C++20 contract attribute specifier.
29598 contract-attribute-specifier:
29599 [ [ assert contract-level [opt] : conditional-expression ] ]
29600 [ [ pre contract-level [opt] : conditional-expression ] ]
29601 [ [ post contract-level [opt] identifier [opt] : conditional-expression ] ]
29603 For free functions, we cannot determine the type of the postcondition
29604 identifier because the we haven't called grokdeclarator yet. In those
29605 cases we parse the postcondition as if the identifier was declared as
29606 'auto <identifier>'. We then instantiate the postcondition once the
29607 return type is known.
29609 For member functions, contracts are in the complete-class context, so the
29610 parse is deferred. We also have the return type avaialable (unless it's
29611 deduced), so we don't need to parse the postcondition in terms of a
29612 placeholder. */
29614 static tree
29615 cp_parser_contract_attribute_spec (cp_parser *parser, tree attribute)
29617 gcc_assert (contract_attribute_p (attribute));
29618 cp_token *token = cp_lexer_consume_token (parser->lexer);
29619 location_t loc = token->location;
29621 bool assertion_p = is_attribute_p ("assert", attribute);
29622 bool postcondition_p = is_attribute_p ("post", attribute);
29624 /* Parse the optional mode. */
29625 tree mode = cp_parser_contract_mode_opt (parser, postcondition_p);
29627 /* Check for postcondition identifiers. */
29628 cp_expr identifier;
29629 if (postcondition_p && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29630 identifier = cp_parser_identifier (parser);
29631 if (identifier == error_mark_node)
29632 return error_mark_node;
29634 cp_parser_require (parser, CPP_COLON, RT_COLON);
29636 /* Defer the parsing of pre/post contracts inside class definitions. */
29637 tree contract;
29638 if (!assertion_p &&
29639 current_class_type &&
29640 TYPE_BEING_DEFINED (current_class_type))
29642 /* Skip until we reach an unenclose ']'. If we ran into an unnested ']'
29643 that doesn't close the attribute, return an error and let the attribute
29644 handling code emit an error for missing ']]'. */
29645 cp_token *first = cp_lexer_peek_token (parser->lexer);
29646 cp_parser_skip_to_closing_parenthesis_1 (parser,
29647 /*recovering=*/false,
29648 CPP_CLOSE_SQUARE,
29649 /*consume_paren=*/false);
29650 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE
29651 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_CLOSE_SQUARE)
29652 return error_mark_node;
29653 cp_token *last = cp_lexer_peek_token (parser->lexer);
29655 /* Build a deferred-parse node. */
29656 tree condition = make_node (DEFERRED_PARSE);
29657 DEFPARSE_TOKENS (condition) = cp_token_cache_new (first, last);
29658 DEFPARSE_INSTANTIATIONS (condition) = NULL;
29660 /* And its corresponding contract. */
29661 contract = grok_contract (attribute, mode, identifier, condition, loc);
29663 else
29665 /* Enable location wrappers when parsing contracts. */
29666 auto suppression = make_temp_override (suppress_location_wrappers, 0);
29668 /* Build a fake variable for the result identifier. */
29669 tree result = NULL_TREE;
29670 if (identifier)
29672 begin_scope (sk_block, NULL_TREE);
29673 result = make_postcondition_variable (identifier);
29674 ++processing_template_decl;
29677 /* Parse the condition, ensuring that parameters or the return variable
29678 aren't flagged for use outside the body of a function. */
29679 ++processing_contract_condition;
29680 cp_expr condition = cp_parser_conditional_expression (parser);
29681 --processing_contract_condition;
29683 /* Try to recover from errors by scanning up to the end of the
29684 attribute. Sometimes we get partially parsed expressions, so
29685 we need to search the condition for errors. */
29686 if (contains_error_p (condition))
29687 cp_parser_skip_up_to_closing_square_bracket (parser);
29689 /* Build the contract. */
29690 contract = grok_contract (attribute, mode, result, condition, loc);
29692 /* Leave our temporary scope for the postcondition result. */
29693 if (result)
29695 --processing_template_decl;
29696 pop_bindings_and_leave_scope ();
29700 if (!flag_contracts)
29702 error_at (loc, "contracts are only available with %<-fcontracts%>");
29703 return error_mark_node;
29706 return finish_contract_attribute (attribute, contract);
29709 /* Parse a contract condition for a deferred contract. */
29711 void cp_parser_late_contract_condition (cp_parser *parser,
29712 tree fn,
29713 tree attribute)
29715 tree contract = TREE_VALUE (TREE_VALUE (attribute));
29717 /* Make sure we've gotten something that hasn't been parsed yet or that
29718 we're not parsing an invalid contract. */
29719 tree condition = CONTRACT_CONDITION (contract);
29720 if (TREE_CODE (condition) != DEFERRED_PARSE)
29721 return;
29723 tree identifier = NULL_TREE;
29724 if (TREE_CODE (contract) == POSTCONDITION_STMT)
29725 identifier = POSTCONDITION_IDENTIFIER (contract);
29727 /* Build a fake variable for the result identifier. */
29728 tree result = NULL_TREE;
29729 if (identifier)
29731 /* TODO: Can we guarantee that the identifier has a location? */
29732 location_t loc = cp_expr_location (contract);
29733 tree type = TREE_TYPE (TREE_TYPE (fn));
29734 if (!check_postcondition_result (fn, type, loc))
29736 invalidate_contract (contract);
29737 return;
29740 begin_scope (sk_block, NULL_TREE);
29741 result = make_postcondition_variable (identifier, type);
29742 ++processing_template_decl;
29745 /* 'this' is not allowed in preconditions of constructors or in postconditions
29746 of destructors. Note that the previous value of this variable is
29747 established by the calling function, so we need to save it here. */
29748 tree saved_ccr = current_class_ref;
29749 tree saved_ccp = current_class_ptr;
29750 if ((DECL_CONSTRUCTOR_P (fn) && PRECONDITION_P (contract)) ||
29751 (DECL_DESTRUCTOR_P (fn) && POSTCONDITION_P (contract)))
29753 current_class_ref = current_class_ptr = NULL_TREE;
29754 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
29757 push_unparsed_function_queues (parser);
29759 /* Push the saved tokens onto the parser's lexer stack. */
29760 cp_token_cache *tokens = DEFPARSE_TOKENS (condition);
29761 cp_parser_push_lexer_for_tokens (parser, tokens);
29763 /* Parse the condition, ensuring that parameters or the return variable
29764 aren't flagged for use outside the body of a function. */
29765 ++processing_contract_condition;
29766 condition = cp_parser_conditional_expression (parser);
29767 --processing_contract_condition;
29769 /* Revert to the main lexer. */
29770 cp_parser_pop_lexer (parser);
29772 /* Restore the queue. */
29773 pop_unparsed_function_queues (parser);
29775 current_class_ref = saved_ccr;
29776 current_class_ptr = saved_ccp;
29778 /* Commit to changes. */
29779 update_late_contract (contract, result, condition);
29781 /* Leave our temporary scope for the postcondition result. */
29782 if (result)
29784 --processing_template_decl;
29785 pop_bindings_and_leave_scope ();
29789 /* Parse a standard C++-11 attribute specifier.
29791 attribute-specifier:
29792 [ [ attribute-using-prefix [opt] attribute-list ] ]
29793 contract-attribute-specifier
29794 alignment-specifier
29796 attribute-using-prefix:
29797 using attribute-namespace :
29799 alignment-specifier:
29800 alignas ( type-id ... [opt] )
29801 alignas ( alignment-expression ... [opt] ).
29803 Extensions for contracts:
29805 contract-attribute-specifier:
29806 [ [ assert : contract-mode [opt] : conditional-expression ] ]
29807 [ [ pre : contract-mode [opt] : conditional-expression ] ]
29808 [ [ post : contract-mode [opt] identifier [opt] :
29809 conditional-expression ] ] */
29811 static tree
29812 cp_parser_std_attribute_spec (cp_parser *parser)
29814 tree attributes = NULL_TREE;
29815 cp_token *token = cp_lexer_peek_token (parser->lexer);
29817 if (token->type == CPP_OPEN_SQUARE
29818 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
29820 tree attr_ns = NULL_TREE;
29821 tree attr_name = NULL_TREE;
29823 cp_lexer_consume_token (parser->lexer);
29824 cp_lexer_consume_token (parser->lexer);
29826 token = cp_lexer_peek_token (parser->lexer);
29827 if (token->type == CPP_NAME)
29829 attr_name = token->u.value;
29830 attr_name = canonicalize_attr_name (attr_name);
29833 /* Handle contract-attribute-specs specially. */
29834 if (attr_name && contract_attribute_p (attr_name))
29836 tree attrs = cp_parser_contract_attribute_spec (parser, attr_name);
29837 if (attrs != error_mark_node)
29838 attributes = attrs;
29839 goto finish_attrs;
29842 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
29844 token = cp_lexer_peek_nth_token (parser->lexer, 2);
29845 if (token->type == CPP_NAME)
29846 attr_ns = token->u.value;
29847 else if (token->type == CPP_KEYWORD)
29848 attr_ns = ridpointers[(int) token->keyword];
29849 else if (token->flags & NAMED_OP)
29850 attr_ns = get_identifier (cpp_type2name (token->type,
29851 token->flags));
29852 if (attr_ns
29853 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
29855 if (cxx_dialect < cxx17)
29856 pedwarn (input_location, OPT_Wc__17_extensions,
29857 "attribute using prefix only available "
29858 "with %<-std=c++17%> or %<-std=gnu++17%>");
29860 cp_lexer_consume_token (parser->lexer);
29861 cp_lexer_consume_token (parser->lexer);
29862 cp_lexer_consume_token (parser->lexer);
29864 else
29865 attr_ns = NULL_TREE;
29868 attributes = cp_parser_std_attribute_list (parser, attr_ns);
29870 finish_attrs:
29871 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
29872 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
29873 cp_parser_skip_to_end_of_statement (parser);
29874 else
29875 /* Warn about parsing c++11 attribute in non-c++11 mode, only
29876 when we are sure that we have actually parsed them. */
29877 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
29879 else
29881 tree alignas_expr;
29883 /* Look for an alignment-specifier. */
29885 token = cp_lexer_peek_token (parser->lexer);
29887 if (token->type != CPP_KEYWORD
29888 || token->keyword != RID_ALIGNAS)
29889 return NULL_TREE;
29891 cp_lexer_consume_token (parser->lexer);
29892 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
29894 matching_parens parens;
29895 if (!parens.require_open (parser))
29896 return error_mark_node;
29898 cp_parser_parse_tentatively (parser);
29899 alignas_expr = cp_parser_type_id (parser);
29901 if (!cp_parser_parse_definitely (parser))
29903 alignas_expr = cp_parser_assignment_expression (parser);
29904 if (alignas_expr == error_mark_node)
29905 cp_parser_skip_to_end_of_statement (parser);
29906 if (alignas_expr == NULL_TREE
29907 || alignas_expr == error_mark_node)
29908 return alignas_expr;
29911 alignas_expr = cxx_alignas_expr (alignas_expr);
29912 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
29914 /* Handle alignas (pack...). */
29915 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
29917 cp_lexer_consume_token (parser->lexer);
29918 alignas_expr = make_pack_expansion (alignas_expr);
29921 /* Something went wrong, so don't build the attribute. */
29922 if (alignas_expr == error_mark_node)
29923 return error_mark_node;
29925 /* Missing ')' means the code cannot possibly be valid; go ahead
29926 and commit to make sure we issue a hard error. */
29927 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
29928 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29929 cp_parser_commit_to_tentative_parse (parser);
29931 if (!parens.require_close (parser))
29932 return error_mark_node;
29934 /* Build the C++-11 representation of an 'aligned'
29935 attribute. */
29936 attributes
29937 = build_tree_list (build_tree_list (gnu_identifier,
29938 aligned_identifier), alignas_expr);
29941 return attributes;
29944 /* Parse a standard C++-11 attribute-specifier-seq.
29946 attribute-specifier-seq:
29947 attribute-specifier-seq [opt] attribute-specifier */
29949 static tree
29950 cp_parser_std_attribute_spec_seq (cp_parser *parser)
29952 tree attr_specs = NULL_TREE;
29953 tree attr_last = NULL_TREE;
29955 /* Don't create wrapper nodes within attributes: the
29956 handlers don't know how to handle them. */
29957 auto_suppress_location_wrappers sentinel;
29959 while (true)
29961 tree attr_spec = cp_parser_std_attribute_spec (parser);
29962 if (attr_spec == NULL_TREE)
29963 break;
29964 if (attr_spec == error_mark_node)
29965 return error_mark_node;
29967 if (attr_last)
29968 TREE_CHAIN (attr_last) = attr_spec;
29969 else
29970 attr_specs = attr_last = attr_spec;
29971 attr_last = tree_last (attr_last);
29974 return attr_specs;
29977 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
29978 return index of the first token after balanced-token, or N on failure. */
29980 static size_t
29981 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
29983 size_t orig_n = n;
29984 int nparens = 0, nbraces = 0, nsquares = 0;
29986 switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
29988 case CPP_PRAGMA_EOL:
29989 if (!parser->lexer->in_pragma)
29990 break;
29991 /* FALLTHRU */
29992 case CPP_EOF:
29993 /* Ran out of tokens. */
29994 return orig_n;
29995 case CPP_OPEN_PAREN:
29996 ++nparens;
29997 break;
29998 case CPP_OPEN_BRACE:
29999 ++nbraces;
30000 break;
30001 case CPP_OPEN_SQUARE:
30002 ++nsquares;
30003 break;
30004 case CPP_CLOSE_PAREN:
30005 --nparens;
30006 break;
30007 case CPP_CLOSE_BRACE:
30008 --nbraces;
30009 break;
30010 case CPP_CLOSE_SQUARE:
30011 --nsquares;
30012 break;
30013 default:
30014 break;
30016 while (nparens || nbraces || nsquares);
30017 return n;
30020 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
30021 return index of the first token after the GNU attribute tokens, or N on
30022 failure. */
30024 static size_t
30025 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
30027 while (true)
30029 if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
30030 || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
30031 || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
30032 break;
30034 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
30035 if (n2 == n + 2)
30036 break;
30037 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
30038 break;
30039 n = n2 + 1;
30041 return n;
30044 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
30045 next token), return index of the first token after the standard C++11
30046 attribute tokens, or N on failure. */
30048 static size_t
30049 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
30051 while (true)
30053 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
30054 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
30056 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
30057 if (n2 == n + 1)
30058 break;
30059 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
30060 break;
30061 n = n2 + 1;
30063 else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
30064 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
30066 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
30067 if (n2 == n + 1)
30068 break;
30069 n = n2;
30071 else
30072 break;
30074 return n;
30077 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
30078 as the next token), return index of the first token after the attribute
30079 tokens, or N on failure. */
30081 static size_t
30082 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
30084 if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
30085 return cp_parser_skip_gnu_attributes_opt (parser, n);
30086 return cp_parser_skip_std_attribute_spec_seq (parser, n);
30089 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
30090 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
30091 current value of the PEDANTIC flag, regardless of whether or not
30092 the `__extension__' keyword is present. The caller is responsible
30093 for restoring the value of the PEDANTIC flag. */
30095 static bool
30096 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
30098 /* Save the old value of the PEDANTIC flag. */
30099 *saved_pedantic = pedantic;
30101 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
30103 /* Consume the `__extension__' token. */
30104 cp_lexer_consume_token (parser->lexer);
30105 /* We're not being pedantic while the `__extension__' keyword is
30106 in effect. */
30107 pedantic = 0;
30109 return true;
30112 return false;
30115 /* Parse a label declaration.
30117 label-declaration:
30118 __label__ label-declarator-seq ;
30120 label-declarator-seq:
30121 identifier , label-declarator-seq
30122 identifier */
30124 static void
30125 cp_parser_label_declaration (cp_parser* parser)
30127 /* Look for the `__label__' keyword. */
30128 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
30130 while (true)
30132 tree identifier;
30134 /* Look for an identifier. */
30135 identifier = cp_parser_identifier (parser);
30136 /* If we failed, stop. */
30137 if (identifier == error_mark_node)
30138 break;
30139 /* Declare it as a label. */
30140 finish_label_decl (identifier);
30141 /* If the next token is a `;', stop. */
30142 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30143 break;
30144 /* Look for the `,' separating the label declarations. */
30145 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
30148 /* Look for the final `;'. */
30149 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
30152 // -------------------------------------------------------------------------- //
30153 // Concept definitions
30155 static tree
30156 cp_parser_concept_definition (cp_parser *parser)
30158 /* A concept definition is an unevaluated context. */
30159 cp_unevaluated u;
30161 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_CONCEPT));
30162 cp_lexer_consume_token (parser->lexer);
30164 cp_expr id = cp_parser_identifier (parser);
30165 if (id == error_mark_node)
30167 cp_parser_skip_to_end_of_statement (parser);
30168 cp_parser_consume_semicolon_at_end_of_statement (parser);
30169 return NULL_TREE;
30172 tree attrs = cp_parser_attributes_opt (parser);
30174 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
30176 cp_parser_skip_to_end_of_statement (parser);
30177 cp_parser_consume_semicolon_at_end_of_statement (parser);
30178 return error_mark_node;
30181 processing_constraint_expression_sentinel parsing_constraint;
30182 tree init = cp_parser_constraint_expression (parser);
30183 if (init == error_mark_node)
30184 cp_parser_skip_to_end_of_statement (parser);
30186 /* Consume the trailing ';'. Diagnose the problem if it isn't there,
30187 but continue as if it were. */
30188 cp_parser_consume_semicolon_at_end_of_statement (parser);
30190 return finish_concept_definition (id, init, attrs);
30193 // -------------------------------------------------------------------------- //
30194 // Requires Clause
30196 /* Diagnose an expression that should appear in ()'s within a requires-clause
30197 and suggest where to place those parentheses. */
30199 static void
30200 cp_parser_diagnose_ungrouped_constraint_plain (location_t loc)
30202 error_at (loc, "expression must be enclosed in parentheses");
30205 static void
30206 cp_parser_diagnose_ungrouped_constraint_rich (location_t loc)
30208 gcc_rich_location richloc (loc);
30209 richloc.add_fixit_insert_before ("(");
30210 richloc.add_fixit_insert_after (")");
30211 error_at (&richloc, "expression must be enclosed in parentheses");
30214 /* Characterizes the likely kind of expression intended by a mis-written
30215 primary constraint. */
30216 enum primary_constraint_error
30218 pce_ok,
30219 pce_maybe_operator,
30220 pce_maybe_postfix
30223 /* Returns true if the token(s) following a primary-expression in a
30224 constraint-logical-* expression would require parentheses. */
30226 static primary_constraint_error
30227 cp_parser_constraint_requires_parens (cp_parser *parser, bool lambda_p)
30229 cp_token *token = cp_lexer_peek_token (parser->lexer);
30230 switch (token->type)
30232 default:
30233 return pce_ok;
30235 case CPP_EQ:
30237 /* An equal sign may be part of the definition of a function,
30238 and not an assignment operator, when parsing the expression
30239 for a trailing requires-clause. For example:
30241 template<typename T>
30242 struct S {
30243 S() requires C<T> = default;
30246 Don't try to reparse this a binary operator. */
30247 if (cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_DELETE)
30248 || cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_DEFAULT))
30249 return pce_ok;
30251 gcc_fallthrough ();
30254 /* Arithmetic operators. */
30255 case CPP_PLUS:
30256 case CPP_MINUS:
30257 case CPP_MULT:
30258 case CPP_DIV:
30259 case CPP_MOD:
30260 /* Bitwise operators. */
30261 case CPP_AND:
30262 case CPP_OR:
30263 case CPP_XOR:
30264 case CPP_RSHIFT:
30265 case CPP_LSHIFT:
30266 /* Relational operators. */
30267 case CPP_EQ_EQ:
30268 case CPP_NOT_EQ:
30269 case CPP_LESS:
30270 case CPP_GREATER:
30271 case CPP_LESS_EQ:
30272 case CPP_GREATER_EQ:
30273 case CPP_SPACESHIP:
30274 /* Pointer-to-member. */
30275 case CPP_DOT_STAR:
30276 case CPP_DEREF_STAR:
30277 /* Assignment operators. */
30278 case CPP_PLUS_EQ:
30279 case CPP_MINUS_EQ:
30280 case CPP_MULT_EQ:
30281 case CPP_DIV_EQ:
30282 case CPP_MOD_EQ:
30283 case CPP_AND_EQ:
30284 case CPP_OR_EQ:
30285 case CPP_XOR_EQ:
30286 case CPP_RSHIFT_EQ:
30287 case CPP_LSHIFT_EQ:
30288 /* Conditional operator */
30289 case CPP_QUERY:
30290 /* Unenclosed binary or conditional operator. */
30291 return pce_maybe_operator;
30293 case CPP_OPEN_PAREN:
30295 /* A primary constraint that precedes the parameter-list of a
30296 lambda expression is followed by an open paren.
30298 []<typename T> requires C (T a, T b) { ... }
30300 Don't try to re-parse this as a postfix expression. */
30301 if (lambda_p)
30302 return pce_ok;
30304 gcc_fallthrough ();
30306 case CPP_OPEN_SQUARE:
30308 /* A primary-constraint-expression followed by a '[[' is not a
30309 postfix expression. */
30310 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE))
30311 return pce_ok;
30313 gcc_fallthrough ();
30315 case CPP_PLUS_PLUS:
30316 case CPP_MINUS_MINUS:
30317 case CPP_DOT:
30318 /* Unenclosed postfix operator. */
30319 return pce_maybe_postfix;
30321 case CPP_DEREF:
30322 /* A primary constraint that precedes the lambda-declarator of a
30323 lambda expression is followed by trailing return type.
30325 []<typename T> requires C -> void {}
30327 Don't try to re-parse this as a postfix expression in
30328 C++23 and later. In C++20 ( needs to come in between but we
30329 allow it to be omitted with pedwarn. */
30330 if (lambda_p)
30331 return pce_ok;
30332 /* Unenclosed postfix operator. */
30333 return pce_maybe_postfix;
30337 /* Returns true if the next token begins a unary expression, preceded by
30338 an operator or keyword. */
30340 static bool
30341 cp_parser_unary_constraint_requires_parens (cp_parser *parser)
30343 cp_token *token = cp_lexer_peek_token (parser->lexer);
30344 switch (token->type)
30346 case CPP_NOT:
30347 case CPP_PLUS:
30348 case CPP_MINUS:
30349 case CPP_MULT:
30350 case CPP_COMPL:
30351 case CPP_PLUS_PLUS:
30352 case CPP_MINUS_MINUS:
30353 return true;
30355 case CPP_KEYWORD:
30357 switch (token->keyword)
30359 case RID_STATCAST:
30360 case RID_DYNCAST:
30361 case RID_REINTCAST:
30362 case RID_CONSTCAST:
30363 case RID_TYPEID:
30364 case RID_SIZEOF:
30365 case RID_ALIGNOF:
30366 case RID_NOEXCEPT:
30367 case RID_NEW:
30368 case RID_DELETE:
30369 case RID_THROW:
30370 return true;
30372 default:
30373 break;
30377 default:
30378 break;
30381 return false;
30384 /* Parse a primary expression within a constraint. */
30386 static cp_expr
30387 cp_parser_constraint_primary_expression (cp_parser *parser, bool lambda_p)
30389 /* If this looks like a unary expression, parse it as such, but diagnose
30390 it as ill-formed; it requires parens. */
30391 if (cp_parser_unary_constraint_requires_parens (parser))
30393 cp_expr e = cp_parser_assignment_expression (parser, NULL, false, false);
30394 cp_parser_diagnose_ungrouped_constraint_rich (e.get_location());
30395 return e;
30398 cp_lexer_save_tokens (parser->lexer);
30399 cp_id_kind idk;
30400 location_t loc = input_location;
30401 cp_expr expr = cp_parser_primary_expression (parser,
30402 /*address_p=*/false,
30403 /*cast_p=*/false,
30404 /*template_arg_p=*/false,
30405 &idk);
30406 expr.maybe_add_location_wrapper ();
30408 primary_constraint_error pce = pce_ok;
30409 if (expr != error_mark_node)
30411 /* The primary-expression could be part of an unenclosed non-logical
30412 compound expression. */
30413 pce = cp_parser_constraint_requires_parens (parser, lambda_p);
30415 if (pce == pce_ok)
30417 cp_lexer_commit_tokens (parser->lexer);
30418 return finish_constraint_primary_expr (expr);
30421 /* Retry the parse at a lower precedence. If that succeeds, diagnose the
30422 error, but return the expression as if it were valid. */
30423 cp_lexer_rollback_tokens (parser->lexer);
30424 cp_parser_parse_tentatively (parser);
30425 if (pce == pce_maybe_operator)
30426 expr = cp_parser_assignment_expression (parser, NULL, false, false);
30427 else
30428 expr = cp_parser_simple_cast_expression (parser);
30429 if (cp_parser_parse_definitely (parser))
30431 cp_parser_diagnose_ungrouped_constraint_rich (expr.get_location());
30432 return expr;
30435 /* Otherwise, something has gone very wrong, and we can't generate a more
30436 meaningful diagnostic or recover. */
30437 cp_parser_diagnose_ungrouped_constraint_plain (loc);
30438 return error_mark_node;
30441 /* Parse a constraint-logical-and-expression.
30443 constraint-logical-and-expression:
30444 primary-expression
30445 constraint-logical-and-expression '&&' primary-expression */
30447 static cp_expr
30448 cp_parser_constraint_logical_and_expression (cp_parser *parser, bool lambda_p)
30450 cp_expr lhs = cp_parser_constraint_primary_expression (parser, lambda_p);
30451 while (cp_lexer_next_token_is (parser->lexer, CPP_AND_AND))
30453 cp_token *op = cp_lexer_consume_token (parser->lexer);
30454 tree rhs = cp_parser_constraint_primary_expression (parser, lambda_p);
30455 lhs = finish_constraint_and_expr (op->location, lhs, rhs);
30457 return lhs;
30460 /* Parse a constraint-logical-or-expression.
30462 constraint-logical-or-expression:
30463 constraint-logical-and-expression
30464 constraint-logical-or-expression '||' constraint-logical-and-expression */
30466 static cp_expr
30467 cp_parser_constraint_logical_or_expression (cp_parser *parser, bool lambda_p)
30469 cp_expr lhs = cp_parser_constraint_logical_and_expression (parser, lambda_p);
30470 while (cp_lexer_next_token_is (parser->lexer, CPP_OR_OR))
30472 cp_token *op = cp_lexer_consume_token (parser->lexer);
30473 cp_expr rhs = cp_parser_constraint_logical_and_expression (parser, lambda_p);
30474 lhs = finish_constraint_or_expr (op->location, lhs, rhs);
30476 return lhs;
30479 /* Parse the expression after a requires-clause. This has a different grammar
30480 than that in the concepts TS. */
30482 static tree
30483 cp_parser_requires_clause_expression (cp_parser *parser, bool lambda_p)
30485 processing_constraint_expression_sentinel parsing_constraint;
30486 ++processing_template_decl;
30487 cp_expr expr = cp_parser_constraint_logical_or_expression (parser, lambda_p);
30488 --processing_template_decl;
30489 if (check_for_bare_parameter_packs (expr))
30490 expr = error_mark_node;
30491 return expr;
30494 /* Parse a expression after a requires clause.
30496 constraint-expression:
30497 logical-or-expression
30499 The required logical-or-expression must be a constant expression. Note
30500 that we don't check that the expression is constepxr here. We defer until
30501 we analyze constraints and then, we only check atomic constraints. */
30503 static tree
30504 cp_parser_constraint_expression (cp_parser *parser)
30506 processing_constraint_expression_sentinel parsing_constraint;
30507 ++processing_template_decl;
30508 cp_expr expr = cp_parser_binary_expression (parser, false, true,
30509 PREC_NOT_OPERATOR, NULL);
30510 --processing_template_decl;
30511 if (check_for_bare_parameter_packs (expr))
30512 expr = error_mark_node;
30513 expr.maybe_add_location_wrapper ();
30514 return expr;
30517 /* Optionally parse a requires clause:
30519 requires-clause:
30520 `requires` constraint-logical-or-expression.
30521 [ConceptsTS]
30522 `requires constraint-expression.
30524 LAMBDA_P is true when the requires-clause is parsed before the
30525 parameter-list of a lambda-declarator. */
30527 static tree
30528 cp_parser_requires_clause_opt (cp_parser *parser, bool lambda_p)
30530 /* A requires clause is an unevaluated context. */
30531 cp_unevaluated u;
30533 cp_token *tok = cp_lexer_peek_token (parser->lexer);
30534 if (tok->keyword != RID_REQUIRES)
30536 if (!flag_concepts && tok->type == CPP_NAME
30537 && tok->u.value == ridpointers[RID_REQUIRES])
30539 error_at (cp_lexer_peek_token (parser->lexer)->location,
30540 "%<requires%> only available with "
30541 "%<-std=c++20%> or %<-fconcepts%>");
30542 /* Parse and discard the requires-clause. */
30543 cp_lexer_consume_token (parser->lexer);
30544 cp_parser_constraint_expression (parser);
30546 return NULL_TREE;
30549 cp_token *tok2 = cp_lexer_peek_nth_token (parser->lexer, 2);
30550 if (tok2->type == CPP_OPEN_BRACE)
30552 /* An opening brace following the start of a requires-clause is
30553 ill-formed; the user likely forgot the second `requires' that
30554 would start a requires-expression. */
30555 gcc_rich_location richloc (tok2->location);
30556 richloc.add_fixit_insert_after (tok->location, " requires");
30557 error_at (&richloc, "missing additional %<requires%> to start "
30558 "a requires-expression");
30559 /* Don't consume the `requires', so that it's reused as the start of a
30560 requires-expression. */
30562 else
30563 cp_lexer_consume_token (parser->lexer);
30565 if (!flag_concepts_ts)
30566 return cp_parser_requires_clause_expression (parser, lambda_p);
30567 else
30568 return cp_parser_constraint_expression (parser);
30571 /*---------------------------------------------------------------------------
30572 Requires expressions
30573 ---------------------------------------------------------------------------*/
30575 /* Parse a requires expression
30577 requirement-expression:
30578 'requires' requirement-parameter-list [opt] requirement-body */
30580 static tree
30581 cp_parser_requires_expression (cp_parser *parser)
30583 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
30584 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
30586 /* Avoid committing to outer tentative parse. */
30587 tentative_firewall firewall (parser);
30589 /* This is definitely a requires-expression. */
30590 cp_parser_commit_to_tentative_parse (parser);
30592 tree parms, reqs;
30594 /* Local parameters are delared as variables within the scope
30595 of the expression. They are not visible past the end of
30596 the expression. Expressions within the requires-expression
30597 are unevaluated. */
30598 struct scope_sentinel
30600 scope_sentinel ()
30602 ++cp_unevaluated_operand;
30603 begin_scope (sk_function_parms, NULL_TREE);
30604 current_binding_level->requires_expression = true;
30607 ~scope_sentinel ()
30609 pop_bindings_and_leave_scope ();
30610 --cp_unevaluated_operand;
30612 } s;
30614 /* Parse the optional parameter list. */
30615 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30617 parms = cp_parser_requirement_parameter_list (parser);
30618 if (parms == error_mark_node)
30619 return error_mark_node;
30621 else
30622 parms = NULL_TREE;
30624 /* Parse the requirement body. */
30625 ++processing_template_decl;
30626 reqs = cp_parser_requirement_body (parser);
30627 --processing_template_decl;
30628 if (reqs == error_mark_node)
30629 return error_mark_node;
30632 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
30633 the parm chain. */
30634 grokparms (parms, &parms);
30635 loc = make_location (loc, loc, parser->lexer);
30636 tree expr = finish_requires_expr (loc, parms, reqs);
30637 if (!processing_template_decl)
30639 /* Perform semantic processing now to diagnose any invalid types and
30640 expressions. */
30641 int saved_errorcount = errorcount;
30642 tsubst_requires_expr (expr, NULL_TREE, tf_warning_or_error, NULL_TREE);
30643 if (errorcount > saved_errorcount)
30644 return error_mark_node;
30646 return expr;
30649 /* Parse a parameterized requirement.
30651 requirement-parameter-list:
30652 '(' parameter-declaration-clause ')' */
30654 static tree
30655 cp_parser_requirement_parameter_list (cp_parser *parser)
30657 matching_parens parens;
30658 if (!parens.require_open (parser))
30659 return error_mark_node;
30661 tree parms = (cp_parser_parameter_declaration_clause
30662 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
30664 if (!parens.require_close (parser))
30665 return error_mark_node;
30667 /* Modify the declared parameters by removing their context
30668 so they don't refer to the enclosing scope and explicitly
30669 indicating that they are constraint variables. */
30670 for (tree parm = parms; parm; parm = TREE_CHAIN (parm))
30672 if (parm == void_list_node || parm == explicit_void_list_node)
30673 break;
30674 tree decl = TREE_VALUE (parm);
30675 if (decl != error_mark_node)
30677 DECL_CONTEXT (decl) = NULL_TREE;
30678 CONSTRAINT_VAR_P (decl) = true;
30682 return parms;
30685 /* Parse the body of a requirement.
30687 requirement-body:
30688 '{' requirement-list '}' */
30689 static tree
30690 cp_parser_requirement_body (cp_parser *parser)
30692 matching_braces braces;
30693 if (!braces.require_open (parser))
30694 return error_mark_node;
30696 tree reqs = cp_parser_requirement_seq (parser);
30698 if (!braces.require_close (parser))
30699 return error_mark_node;
30701 return reqs;
30704 /* Parse a sequence of requirements.
30706 requirement-seq:
30707 requirement
30708 requirement-seq requirement */
30710 static tree
30711 cp_parser_requirement_seq (cp_parser *parser)
30713 tree result = NULL_TREE;
30716 tree req = cp_parser_requirement (parser);
30717 if (req != error_mark_node)
30718 result = tree_cons (NULL_TREE, req, result);
30720 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE)
30721 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF));
30723 /* If there are no valid requirements, this is not a valid expression. */
30724 if (!result)
30725 return error_mark_node;
30727 /* Reverse the order of requirements so they are analyzed in order. */
30728 return nreverse (result);
30731 /* Parse a syntactic requirement or type requirement.
30733 requirement:
30734 simple-requirement
30735 compound-requirement
30736 type-requirement
30737 nested-requirement */
30739 static tree
30740 cp_parser_requirement (cp_parser *parser)
30742 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30743 return cp_parser_compound_requirement (parser);
30744 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
30746 /* It's probably a type-requirement. */
30747 cp_parser_parse_tentatively (parser);
30748 tree req = cp_parser_type_requirement (parser);
30749 if (cp_parser_parse_definitely (parser))
30750 return req;
30751 /* No, maybe it's something like typename T::type(); */
30752 cp_parser_parse_tentatively (parser);
30753 req = cp_parser_simple_requirement (parser);
30754 if (cp_parser_parse_definitely (parser))
30755 return req;
30756 /* Non-tentative for the error. */
30757 return cp_parser_type_requirement (parser);
30759 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
30760 return cp_parser_nested_requirement (parser);
30761 else
30762 return cp_parser_simple_requirement (parser);
30765 /* Parse a simple requirement.
30767 simple-requirement:
30768 expression ';' */
30770 static tree
30771 cp_parser_simple_requirement (cp_parser *parser)
30773 location_t start = cp_lexer_peek_token (parser->lexer)->location;
30774 cp_expr expr = cp_parser_expression (parser, NULL, false, false);
30775 if (expr == error_mark_node)
30776 cp_parser_skip_to_end_of_statement (parser);
30778 cp_parser_consume_semicolon_at_end_of_statement (parser);
30780 if (!expr || expr == error_mark_node)
30781 return error_mark_node;
30783 /* Sometimes we don't get locations, so use the cached token location
30784 as a reasonable approximation. */
30785 if (expr.get_location() == UNKNOWN_LOCATION)
30786 expr.set_location (start);
30788 for (tree t = expr; ; )
30790 if (TREE_CODE (t) == TRUTH_ANDIF_EXPR
30791 || TREE_CODE (t) == TRUTH_ORIF_EXPR)
30793 t = TREE_OPERAND (t, 0);
30794 continue;
30796 if (concept_check_p (t))
30798 gcc_rich_location richloc (get_start (start));
30799 richloc.add_fixit_insert_before (start, "requires ");
30800 warning_at (&richloc, OPT_Wmissing_requires, "testing "
30801 "if a concept-id is a valid expression; add "
30802 "%<requires%> to check satisfaction");
30804 break;
30807 return finish_simple_requirement (expr.get_location (), expr);
30810 /* Parse a type requirement
30812 type-requirement
30813 nested-name-specifier [opt] required-type-name ';'
30815 required-type-name:
30816 type-name
30817 'template' [opt] simple-template-id */
30819 static tree
30820 cp_parser_type_requirement (cp_parser *parser)
30822 cp_token *start_tok = cp_lexer_consume_token (parser->lexer);
30823 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30825 // Save the scope before parsing name specifiers.
30826 tree saved_scope = parser->scope;
30827 tree saved_object_scope = parser->object_scope;
30828 tree saved_qualifying_scope = parser->qualifying_scope;
30829 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
30830 cp_parser_nested_name_specifier_opt (parser,
30831 /*typename_keyword_p=*/true,
30832 /*check_dependency_p=*/false,
30833 /*type_p=*/true,
30834 /*is_declaration=*/false);
30836 tree type;
30837 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
30839 cp_lexer_consume_token (parser->lexer);
30840 type = cp_parser_template_id (parser,
30841 /*template_keyword_p=*/true,
30842 /*check_dependency=*/false,
30843 /*tag_type=*/none_type,
30844 /*is_declaration=*/false);
30845 type = make_typename_type (parser->scope, type, typename_type,
30846 /*complain=*/tf_error);
30848 else
30849 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
30851 if (TREE_CODE (type) == TYPE_DECL)
30852 type = TREE_TYPE (type);
30854 parser->scope = saved_scope;
30855 parser->object_scope = saved_object_scope;
30856 parser->qualifying_scope = saved_qualifying_scope;
30858 if (type == error_mark_node)
30859 cp_parser_skip_to_end_of_statement (parser);
30861 cp_parser_consume_semicolon_at_end_of_statement (parser);
30863 if (type == error_mark_node)
30864 return error_mark_node;
30866 loc = make_location (loc, start_tok->location, parser->lexer);
30867 return finish_type_requirement (loc, type);
30870 /* Parse a compound requirement
30872 compound-requirement:
30873 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
30875 static tree
30876 cp_parser_compound_requirement (cp_parser *parser)
30878 /* Parse an expression enclosed in '{ }'s. */
30879 matching_braces braces;
30880 if (!braces.require_open (parser))
30881 return error_mark_node;
30883 cp_token *expr_token = cp_lexer_peek_token (parser->lexer);
30885 tree expr = cp_parser_expression (parser, NULL, false, false);
30886 if (expr == error_mark_node)
30887 cp_parser_skip_to_closing_brace (parser);
30889 if (!braces.require_close (parser))
30891 cp_parser_skip_to_end_of_statement (parser);
30892 cp_parser_consume_semicolon_at_end_of_statement (parser);
30893 return error_mark_node;
30896 /* If the expression was invalid, skip the remainder of the requirement. */
30897 if (!expr || expr == error_mark_node)
30899 cp_parser_skip_to_end_of_statement (parser);
30900 cp_parser_consume_semicolon_at_end_of_statement (parser);
30901 return error_mark_node;
30904 /* Parse the optional noexcept. */
30905 bool noexcept_p = false;
30906 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
30908 cp_lexer_consume_token (parser->lexer);
30909 noexcept_p = true;
30912 /* Parse the optional trailing return type. */
30913 tree type = NULL_TREE;
30914 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
30916 cp_lexer_consume_token (parser->lexer);
30917 cp_token *tok = cp_lexer_peek_token (parser->lexer);
30919 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
30920 parser->in_result_type_constraint_p = true;
30921 /* C++20 allows either a type-id or a type-constraint. Parsing
30922 a type-id will subsume the parsing for a type-constraint but
30923 allow for more syntactic forms (e.g., const C<T>*). */
30924 type = cp_parser_trailing_type_id (parser);
30925 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
30926 if (type == error_mark_node)
30927 return error_mark_node;
30929 location_t type_loc = make_location (tok->location, tok->location,
30930 parser->lexer);
30932 /* Check that we haven't written something like 'const C<T>*'. */
30933 if (type_uses_auto (type))
30935 if (!is_auto (type))
30937 error_at (type_loc,
30938 "result type is not a plain type-constraint");
30939 cp_parser_consume_semicolon_at_end_of_statement (parser);
30940 return error_mark_node;
30943 else if (!flag_concepts_ts)
30944 /* P1452R2 removed the trailing-return-type option. */
30945 error_at (type_loc,
30946 "return-type-requirement is not a type-constraint");
30949 location_t loc = make_location (expr_token->location,
30950 braces.open_location (),
30951 parser->lexer);
30953 cp_parser_consume_semicolon_at_end_of_statement (parser);
30955 if (expr == error_mark_node || type == error_mark_node)
30956 return error_mark_node;
30958 return finish_compound_requirement (loc, expr, type, noexcept_p);
30961 /* Parse a nested requirement. This is the same as a requires clause.
30963 nested-requirement:
30964 requires-clause */
30966 static tree
30967 cp_parser_nested_requirement (cp_parser *parser)
30969 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
30970 cp_token *tok = cp_lexer_consume_token (parser->lexer);
30971 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30972 tree req = cp_parser_constraint_expression (parser);
30973 if (req == error_mark_node)
30974 cp_parser_skip_to_end_of_statement (parser);
30975 loc = make_location (loc, tok->location, parser->lexer);
30976 cp_parser_consume_semicolon_at_end_of_statement (parser);
30977 if (req == error_mark_node)
30978 return error_mark_node;
30979 return finish_nested_requirement (loc, req);
30982 /* Support Functions */
30984 /* Return the appropriate prefer_type argument for lookup_name based on
30985 tag_type. */
30987 static inline LOOK_want
30988 prefer_type_arg (tag_types tag_type)
30990 switch (tag_type)
30992 case none_type: return LOOK_want::NORMAL; // No preference.
30993 case scope_type: return LOOK_want::TYPE_NAMESPACE; // Type or namespace.
30994 default: return LOOK_want::TYPE; // Type only.
30998 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
30999 NAME should have one of the representations used for an
31000 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
31001 is returned. If PARSER->SCOPE is a dependent type, then a
31002 SCOPE_REF is returned.
31004 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
31005 returned; the name was already resolved when the TEMPLATE_ID_EXPR
31006 was formed. Abstractly, such entities should not be passed to this
31007 function, because they do not need to be looked up, but it is
31008 simpler to check for this special case here, rather than at the
31009 call-sites.
31011 In cases not explicitly covered above, this function returns a
31012 DECL, OVERLOAD, or baselink representing the result of the lookup.
31013 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
31014 is returned.
31016 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
31017 (e.g., "struct") that was used. In that case bindings that do not
31018 refer to types are ignored.
31020 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
31021 ignored.
31023 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
31024 are ignored.
31026 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
31027 types.
31029 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
31030 TREE_LIST of candidates if name-lookup results in an ambiguity, and
31031 NULL_TREE otherwise. */
31033 static cp_expr
31034 cp_parser_lookup_name (cp_parser *parser, tree name,
31035 enum tag_types tag_type,
31036 bool is_template,
31037 bool is_namespace,
31038 bool check_dependency,
31039 tree *ambiguous_decls,
31040 location_t name_location)
31042 tree decl;
31043 tree object_type = parser->context->object_type;
31045 /* Assume that the lookup will be unambiguous. */
31046 if (ambiguous_decls)
31047 *ambiguous_decls = NULL_TREE;
31049 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
31050 no longer valid. Note that if we are parsing tentatively, and
31051 the parse fails, OBJECT_TYPE will be automatically restored. */
31052 parser->context->object_type = NULL_TREE;
31054 if (name == error_mark_node)
31055 return error_mark_node;
31057 /* A template-id has already been resolved; there is no lookup to
31058 do. */
31059 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
31060 return name;
31061 if (BASELINK_P (name))
31063 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
31064 == TEMPLATE_ID_EXPR);
31065 return name;
31068 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
31069 it should already have been checked to make sure that the name
31070 used matches the type being destroyed. */
31071 if (TREE_CODE (name) == BIT_NOT_EXPR)
31073 tree type;
31075 /* Figure out to which type this destructor applies. */
31076 if (parser->scope)
31077 type = parser->scope;
31078 else if (object_type)
31079 type = object_type;
31080 else
31081 type = current_class_type;
31082 /* If that's not a class type, there is no destructor. */
31083 if (!type || !CLASS_TYPE_P (type))
31084 return error_mark_node;
31086 /* In a non-static member function, check implicit this->. */
31087 if (current_class_ref)
31088 return lookup_destructor (current_class_ref, parser->scope, name,
31089 tf_warning_or_error);
31091 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
31092 lazily_declare_fn (sfk_destructor, type);
31094 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
31095 return dtor;
31097 return error_mark_node;
31100 /* By this point, the NAME should be an ordinary identifier. If
31101 the id-expression was a qualified name, the qualifying scope is
31102 stored in PARSER->SCOPE at this point. */
31103 gcc_assert (identifier_p (name));
31105 /* Perform the lookup. */
31106 if (parser->scope)
31108 bool dependent_p;
31110 if (parser->scope == error_mark_node)
31111 return error_mark_node;
31113 /* If the SCOPE is dependent, the lookup must be deferred until
31114 the template is instantiated -- unless we are explicitly
31115 looking up names in uninstantiated templates. Even then, we
31116 cannot look up the name if the scope is not a class type; it
31117 might, for example, be a template type parameter. */
31118 dependent_p = (TYPE_P (parser->scope)
31119 && dependent_scope_p (parser->scope));
31120 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
31121 && dependent_p)
31122 /* Defer lookup. */
31123 decl = error_mark_node;
31124 else
31126 tree pushed_scope = NULL_TREE;
31128 /* If PARSER->SCOPE is a dependent type, then it must be a
31129 class type, and we must not be checking dependencies;
31130 otherwise, we would have processed this lookup above. So
31131 that PARSER->SCOPE is not considered a dependent base by
31132 lookup_member, we must enter the scope here. */
31133 if (dependent_p)
31134 pushed_scope = push_scope (parser->scope);
31136 /* If the PARSER->SCOPE is a template specialization, it
31137 may be instantiated during name lookup. In that case,
31138 errors may be issued. Even if we rollback the current
31139 tentative parse, those errors are valid. */
31140 decl = lookup_qualified_name (parser->scope, name,
31141 prefer_type_arg (tag_type),
31142 /*complain=*/true);
31144 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
31145 lookup result and the nested-name-specifier nominates a class C:
31146 * if the name specified after the nested-name-specifier, when
31147 looked up in C, is the injected-class-name of C (Clause 9), or
31148 * if the name specified after the nested-name-specifier is the
31149 same as the identifier or the simple-template-id's template-
31150 name in the last component of the nested-name-specifier,
31151 the name is instead considered to name the constructor of
31152 class C. [ Note: for example, the constructor is not an
31153 acceptable lookup result in an elaborated-type-specifier so
31154 the constructor would not be used in place of the
31155 injected-class-name. --end note ] Such a constructor name
31156 shall be used only in the declarator-id of a declaration that
31157 names a constructor or in a using-declaration. */
31158 if (tag_type == none_type
31159 && DECL_SELF_REFERENCE_P (decl)
31160 && same_type_p (DECL_CONTEXT (decl), parser->scope))
31161 decl = lookup_qualified_name (parser->scope, ctor_identifier,
31162 prefer_type_arg (tag_type),
31163 /*complain=*/true);
31165 if (pushed_scope)
31166 pop_scope (pushed_scope);
31169 /* If the scope is a dependent type and either we deferred lookup or
31170 we did lookup but didn't find the name, rememeber the name. */
31171 if (decl == error_mark_node && TYPE_P (parser->scope)
31172 && dependent_type_p (parser->scope))
31174 if (tag_type)
31176 tree type;
31178 /* The resolution to Core Issue 180 says that `struct
31179 A::B' should be considered a type-name, even if `A'
31180 is dependent. */
31181 type = make_typename_type (parser->scope, name, tag_type,
31182 /*complain=*/tf_error);
31183 if (type != error_mark_node)
31184 decl = TYPE_NAME (type);
31186 else if (is_template
31187 && (cp_parser_next_token_ends_template_argument_p (parser)
31188 || cp_lexer_next_token_is (parser->lexer,
31189 CPP_CLOSE_PAREN)))
31190 decl = make_unbound_class_template (parser->scope,
31191 name, NULL_TREE,
31192 /*complain=*/tf_error);
31193 else
31194 decl = build_qualified_name (/*type=*/NULL_TREE,
31195 parser->scope, name,
31196 is_template);
31198 parser->qualifying_scope = parser->scope;
31199 parser->object_scope = NULL_TREE;
31201 else if (object_type)
31203 bool dep = dependent_scope_p (object_type);
31205 /* Look up the name in the scope of the OBJECT_TYPE, unless the
31206 OBJECT_TYPE is not a class. */
31207 if (!dep && CLASS_TYPE_P (object_type))
31208 /* If the OBJECT_TYPE is a template specialization, it may
31209 be instantiated during name lookup. In that case, errors
31210 may be issued. Even if we rollback the current tentative
31211 parse, those errors are valid. */
31212 decl = lookup_member (object_type,
31213 name,
31214 /*protect=*/0,
31215 /*prefer_type=*/tag_type != none_type,
31216 tf_warning_or_error);
31217 else
31218 decl = NULL_TREE;
31220 if (!decl)
31221 /* Look it up in the enclosing context. DR 141: When looking for a
31222 template-name after -> or ., only consider class templates. */
31223 decl = lookup_name (name, is_namespace ? LOOK_want::NAMESPACE
31224 /* DR 141: When looking in the
31225 current enclosing context for a
31226 template-name after -> or ., only
31227 consider class templates. */
31228 : is_template ? LOOK_want::TYPE
31229 : prefer_type_arg (tag_type));
31231 /* If we did unqualified lookup of a dependent member-qualified name and
31232 found something, do we want to use it? P1787 clarified that we need
31233 to look in the object scope first even if it's dependent, but for now
31234 let's still use it in some cases.
31235 FIXME remember unqualified lookup result to use if member lookup fails
31236 at instantiation time. */
31237 if (decl && dep && is_template)
31239 saved_token_sentinel toks (parser->lexer, STS_ROLLBACK);
31240 /* Only use the unqualified class template lookup if we're actually
31241 looking at a template arg list. */
31242 if (!cp_parser_skip_entire_template_parameter_list (parser))
31243 decl = NULL_TREE;
31246 /* If we know we're looking for a type (e.g. A in p->A::x),
31247 mock up a typename. */
31248 if (!decl && object_type && tag_type != none_type
31249 && dependentish_scope_p (object_type))
31251 tree type = build_typename_type (object_type, name, name,
31252 typename_type);
31253 decl = TYPE_NAME (type);
31256 parser->object_scope = object_type;
31257 parser->qualifying_scope = NULL_TREE;
31259 else
31261 decl = lookup_name (name, is_namespace ? LOOK_want::NAMESPACE
31262 : prefer_type_arg (tag_type));
31263 parser->qualifying_scope = NULL_TREE;
31264 parser->object_scope = NULL_TREE;
31267 /* If the lookup failed, let our caller know. */
31268 if (!decl || decl == error_mark_node)
31269 return error_mark_node;
31271 /* If we have resolved the name of a member declaration, check to
31272 see if the declaration is accessible. When the name resolves to
31273 set of overloaded functions, accessibility is checked when
31274 overload resolution is done. If we have a TREE_LIST, then the lookup
31275 is either ambiguous or it found multiple injected-class-names, the
31276 accessibility of which is trivially satisfied.
31278 During an explicit instantiation, access is not checked at all,
31279 as per [temp.explicit]. */
31280 if (DECL_P (decl))
31281 check_accessibility_of_qualified_id (decl, object_type, parser->scope,
31282 tf_warning_or_error);
31284 /* Pull out the template from an injected-class-name (or multiple). */
31285 if (is_template)
31286 decl = maybe_get_template_decl_from_type_decl (decl);
31288 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
31289 if (TREE_CODE (decl) == TREE_LIST)
31291 if (ambiguous_decls)
31292 *ambiguous_decls = decl;
31293 /* The error message we have to print is too complicated for
31294 cp_parser_error, so we incorporate its actions directly. */
31295 if (!cp_parser_simulate_error (parser))
31297 error_at (name_location, "reference to %qD is ambiguous",
31298 name);
31299 print_candidates (decl);
31301 return error_mark_node;
31304 gcc_assert (DECL_P (decl)
31305 || TREE_CODE (decl) == OVERLOAD
31306 || TREE_CODE (decl) == SCOPE_REF
31307 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
31308 || BASELINK_P (decl));
31310 maybe_record_typedef_use (decl);
31312 return cp_expr (decl, name_location);
31315 /* Like cp_parser_lookup_name, but for use in the typical case where
31316 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
31317 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
31319 static tree
31320 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
31322 return cp_parser_lookup_name (parser, name,
31323 none_type,
31324 /*is_template=*/false,
31325 /*is_namespace=*/false,
31326 /*check_dependency=*/true,
31327 /*ambiguous_decls=*/NULL,
31328 location);
31331 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
31332 the current context, return the TYPE_DECL. If TAG_NAME_P is
31333 true, the DECL indicates the class being defined in a class-head,
31334 or declared in an elaborated-type-specifier.
31336 Otherwise, return DECL. */
31338 static tree
31339 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
31341 /* If the TEMPLATE_DECL is being declared as part of a class-head,
31342 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
31344 struct A {
31345 template <typename T> struct B;
31348 template <typename T> struct A::B {};
31350 Similarly, in an elaborated-type-specifier:
31352 namespace N { struct X{}; }
31354 struct A {
31355 template <typename T> friend struct N::X;
31358 However, if the DECL refers to a class type, and we are in
31359 the scope of the class, then the name lookup automatically
31360 finds the TYPE_DECL created by build_self_reference rather
31361 than a TEMPLATE_DECL. For example, in:
31363 template <class T> struct S {
31364 S s;
31367 there is no need to handle such case. */
31369 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
31370 return DECL_TEMPLATE_RESULT (decl);
31372 return decl;
31375 /* If too many, or too few, template-parameter lists apply to the
31376 declarator, issue an error message. Returns TRUE if all went well,
31377 and FALSE otherwise. */
31379 static bool
31380 cp_parser_check_declarator_template_parameters (cp_parser* parser,
31381 cp_declarator *declarator,
31382 location_t declarator_location)
31384 switch (declarator->kind)
31386 case cdk_id:
31388 unsigned num_templates = 0;
31389 tree scope = declarator->u.id.qualifying_scope;
31390 bool template_id_p = false;
31392 if (scope)
31393 num_templates = num_template_headers_for_class (scope);
31394 else if (TREE_CODE (declarator->u.id.unqualified_name)
31395 == TEMPLATE_ID_EXPR)
31397 /* If the DECLARATOR has the form `X<y>' then it uses one
31398 additional level of template parameters. */
31399 ++num_templates;
31400 template_id_p = true;
31403 return cp_parser_check_template_parameters
31404 (parser, num_templates, template_id_p, declarator_location,
31405 declarator);
31408 case cdk_function:
31409 case cdk_array:
31410 case cdk_pointer:
31411 case cdk_reference:
31412 case cdk_ptrmem:
31413 return (cp_parser_check_declarator_template_parameters
31414 (parser, declarator->declarator, declarator_location));
31416 case cdk_decomp:
31417 case cdk_error:
31418 return true;
31420 default:
31421 gcc_unreachable ();
31423 return false;
31426 /* NUM_TEMPLATES were used in the current declaration. If that is
31427 invalid, return FALSE and issue an error messages. Otherwise,
31428 return TRUE. If DECLARATOR is non-NULL, then we are checking a
31429 declarator and we can print more accurate diagnostics. */
31431 static bool
31432 cp_parser_check_template_parameters (cp_parser* parser,
31433 unsigned num_templates,
31434 bool template_id_p,
31435 location_t location,
31436 cp_declarator *declarator)
31438 /* If there are the same number of template classes and parameter
31439 lists, that's OK. */
31440 if (parser->num_template_parameter_lists == num_templates)
31441 return true;
31442 /* If there are more, but only one more, and the name ends in an identifier,
31443 then we are declaring a primary template. That's OK too. */
31444 if (!template_id_p
31445 && parser->num_template_parameter_lists == num_templates + 1)
31446 return true;
31448 if (cp_parser_simulate_error (parser))
31449 return false;
31451 /* If there are more template classes than parameter lists, we have
31452 something like:
31454 template <class T> void S<T>::R<T>::f (); */
31455 if (parser->num_template_parameter_lists < num_templates)
31457 if (declarator && !current_function_decl)
31458 error_at (location, "specializing member %<%T::%E%> "
31459 "requires %<template<>%> syntax",
31460 declarator->u.id.qualifying_scope,
31461 declarator->u.id.unqualified_name);
31462 else if (declarator)
31463 error_at (location, "invalid declaration of %<%T::%E%>",
31464 declarator->u.id.qualifying_scope,
31465 declarator->u.id.unqualified_name);
31466 else
31467 error_at (location, "too few template-parameter-lists");
31468 return false;
31470 /* Otherwise, there are too many template parameter lists. We have
31471 something like:
31473 template <class T> template <class U> void S::f(); */
31474 error_at (location, "too many template-parameter-lists");
31475 return false;
31478 /* Parse an optional `::' token indicating that the following name is
31479 from the global namespace. If so, PARSER->SCOPE is set to the
31480 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
31481 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
31482 Returns the new value of PARSER->SCOPE, if the `::' token is
31483 present, and NULL_TREE otherwise. */
31485 static tree
31486 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
31488 cp_token *token;
31490 /* Peek at the next token. */
31491 token = cp_lexer_peek_token (parser->lexer);
31492 /* If we're looking at a `::' token then we're starting from the
31493 global namespace, not our current location. */
31494 if (token->type == CPP_SCOPE)
31496 /* Consume the `::' token. */
31497 cp_lexer_consume_token (parser->lexer);
31498 /* Set the SCOPE so that we know where to start the lookup. */
31499 parser->scope = global_namespace;
31500 parser->qualifying_scope = global_namespace;
31501 parser->object_scope = NULL_TREE;
31503 return parser->scope;
31505 else if (!current_scope_valid_p)
31507 parser->scope = NULL_TREE;
31508 parser->qualifying_scope = NULL_TREE;
31509 parser->object_scope = NULL_TREE;
31512 return NULL_TREE;
31515 /* Returns TRUE if the upcoming token sequence is the start of a
31516 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
31517 declarator is preceded by the `friend' specifier. The parser flags FLAGS
31518 is used to control type-specifier parsing. */
31520 static bool
31521 cp_parser_constructor_declarator_p (cp_parser *parser, cp_parser_flags flags,
31522 bool friend_p)
31524 bool constructor_p;
31525 bool outside_class_specifier_p;
31526 tree nested_name_specifier;
31527 cp_token *next_token;
31529 /* The common case is that this is not a constructor declarator, so
31530 try to avoid doing lots of work if at all possible. It's not
31531 valid declare a constructor at function scope. */
31532 if (parser->in_function_body)
31533 return false;
31534 /* And only certain tokens can begin a constructor declarator. */
31535 next_token = cp_lexer_peek_token (parser->lexer);
31536 if (next_token->type != CPP_NAME
31537 && next_token->type != CPP_SCOPE
31538 && next_token->type != CPP_NESTED_NAME_SPECIFIER
31539 /* DR 2237 (C++20 only): A simple-template-id is no longer valid as the
31540 declarator-id of a constructor or destructor. */
31541 && (next_token->type != CPP_TEMPLATE_ID || cxx_dialect >= cxx20))
31542 return false;
31544 /* Parse tentatively; we are going to roll back all of the tokens
31545 consumed here. */
31546 cp_parser_parse_tentatively (parser);
31547 /* Assume that we are looking at a constructor declarator. */
31548 constructor_p = true;
31550 /* Look for the optional `::' operator. */
31551 cp_parser_global_scope_opt (parser,
31552 /*current_scope_valid_p=*/false);
31553 /* Look for the nested-name-specifier. */
31554 nested_name_specifier
31555 = (cp_parser_nested_name_specifier_opt (parser,
31556 /*typename_keyword_p=*/false,
31557 /*check_dependency_p=*/false,
31558 /*type_p=*/false,
31559 /*is_declaration=*/false));
31561 /* Resolve the TYPENAME_TYPE, because the call above didn't do it. */
31562 if (nested_name_specifier
31563 && TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
31565 tree s = resolve_typename_type (nested_name_specifier,
31566 /*only_current_p=*/false);
31567 if (TREE_CODE (s) != TYPENAME_TYPE)
31568 nested_name_specifier = s;
31571 outside_class_specifier_p = (!at_class_scope_p ()
31572 || !TYPE_BEING_DEFINED (current_class_type)
31573 || friend_p);
31575 /* Outside of a class-specifier, there must be a
31576 nested-name-specifier. Except in C++17 mode, where we
31577 might be declaring a guiding declaration. */
31578 if (!nested_name_specifier && outside_class_specifier_p
31579 && cxx_dialect < cxx17)
31580 constructor_p = false;
31581 else if (nested_name_specifier == error_mark_node)
31582 constructor_p = false;
31584 /* If we have a class scope, this is easy; DR 147 says that S::S always
31585 names the constructor, and no other qualified name could. */
31586 if (constructor_p && nested_name_specifier
31587 && CLASS_TYPE_P (nested_name_specifier))
31589 tree id = cp_parser_unqualified_id (parser,
31590 /*template_keyword_p=*/false,
31591 /*check_dependency_p=*/false,
31592 /*declarator_p=*/true,
31593 /*optional_p=*/false);
31594 if (is_overloaded_fn (id))
31595 id = DECL_NAME (get_first_fn (id));
31596 if (!constructor_name_p (id, nested_name_specifier))
31597 constructor_p = false;
31599 /* If we still think that this might be a constructor-declarator,
31600 look for a class-name. */
31601 else if (constructor_p)
31603 /* If we have:
31605 template <typename T> struct S {
31606 S();
31609 we must recognize that the nested `S' names a class. */
31610 if (cxx_dialect >= cxx17)
31611 cp_parser_parse_tentatively (parser);
31613 tree type_decl;
31614 type_decl = cp_parser_class_name (parser,
31615 /*typename_keyword_p=*/false,
31616 /*template_keyword_p=*/false,
31617 none_type,
31618 /*check_dependency_p=*/false,
31619 /*class_head_p=*/false,
31620 /*is_declaration=*/false);
31622 if (cxx_dialect >= cxx17
31623 && !cp_parser_parse_definitely (parser))
31625 type_decl = NULL_TREE;
31626 tree tmpl = cp_parser_template_name (parser,
31627 /*template_keyword*/false,
31628 /*check_dependency_p*/false,
31629 /*is_declaration*/false,
31630 none_type,
31631 /*is_identifier*/NULL);
31632 if (DECL_CLASS_TEMPLATE_P (tmpl)
31633 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
31634 /* It's a deduction guide, return true. */;
31635 else
31636 cp_parser_simulate_error (parser);
31639 /* If there was no class-name, then this is not a constructor.
31640 Otherwise, if we are in a class-specifier and we aren't
31641 handling a friend declaration, check that its type matches
31642 current_class_type (c++/38313). Note: error_mark_node
31643 is left alone for error recovery purposes. */
31644 constructor_p = (!cp_parser_error_occurred (parser)
31645 && (outside_class_specifier_p
31646 || type_decl == NULL_TREE
31647 || type_decl == error_mark_node
31648 || same_type_p (current_class_type,
31649 TREE_TYPE (type_decl))));
31651 /* If we're still considering a constructor, we have to see a `(',
31652 to begin the parameter-declaration-clause, followed by either a
31653 `)', an `...', or a decl-specifier. We need to check for a
31654 type-specifier to avoid being fooled into thinking that:
31656 S (f) (int);
31658 is a constructor. (It is actually a function named `f' that
31659 takes one parameter (of type `int') and returns a value of type
31660 `S'. */
31661 if (constructor_p
31662 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31663 constructor_p = false;
31665 if (constructor_p
31666 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
31667 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
31668 /* A parameter declaration begins with a decl-specifier,
31669 which is either the "attribute" keyword, a storage class
31670 specifier, or (usually) a type-specifier. */
31671 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer)
31672 /* GNU attributes can actually appear both at the start of
31673 a parameter and parenthesized declarator.
31674 S (__attribute__((unused)) int);
31675 is a constructor, but
31676 S (__attribute__((unused)) foo) (int);
31677 is a function declaration. [[attribute]] can appear in the
31678 first form too, but not in the second form. */
31679 && !cp_next_tokens_can_be_std_attribute_p (parser))
31681 tree type;
31682 tree pushed_scope = NULL_TREE;
31683 unsigned saved_num_template_parameter_lists;
31685 if (cp_parser_allow_gnu_extensions_p (parser)
31686 && cp_next_tokens_can_be_gnu_attribute_p (parser))
31688 unsigned int n = cp_parser_skip_gnu_attributes_opt (parser, 1);
31689 while (--n)
31690 cp_lexer_consume_token (parser->lexer);
31693 /* Names appearing in the type-specifier should be looked up
31694 in the scope of the class. */
31695 if (current_class_type)
31696 type = NULL_TREE;
31697 else if (type_decl)
31699 type = TREE_TYPE (type_decl);
31700 if (TREE_CODE (type) == TYPENAME_TYPE)
31702 type = resolve_typename_type (type,
31703 /*only_current_p=*/false);
31704 if (TREE_CODE (type) == TYPENAME_TYPE)
31706 cp_parser_abort_tentative_parse (parser);
31707 return false;
31710 pushed_scope = push_scope (type);
31713 /* Inside the constructor parameter list, surrounding
31714 template-parameter-lists do not apply. */
31715 saved_num_template_parameter_lists
31716 = parser->num_template_parameter_lists;
31717 parser->num_template_parameter_lists = 0;
31719 /* Look for the type-specifier. It's not optional, but its typename
31720 might be. Unless this is a friend declaration; we don't want to
31721 treat
31723 friend S (T::fn)(int);
31725 as a constructor, but with P0634, we might assume a type when
31726 looking for the type-specifier. It is actually a function named
31727 `T::fn' that takes one parameter (of type `int') and returns a
31728 value of type `S'. Constructors can be friends, but they must
31729 use a qualified name.
31731 Parse with an empty set of declaration specifiers since we're
31732 trying to match a decl-specifier-seq of the first parameter.
31733 This must be non-null so that cp_parser_simple_type_specifier
31734 will recognize a constrained placeholder type such as:
31735 'C<int> auto' where C is a type concept. */
31736 cp_decl_specifier_seq ctor_specs;
31737 clear_decl_specs (&ctor_specs);
31738 cp_parser_type_specifier (parser,
31739 (friend_p ? CP_PARSER_FLAGS_NONE
31740 : (flags & ~CP_PARSER_FLAGS_OPTIONAL)),
31741 /*decl_specs=*/&ctor_specs,
31742 /*is_declarator=*/true,
31743 /*declares_class_or_enum=*/NULL,
31744 /*is_cv_qualifier=*/NULL);
31746 parser->num_template_parameter_lists
31747 = saved_num_template_parameter_lists;
31749 /* Leave the scope of the class. */
31750 if (pushed_scope)
31751 pop_scope (pushed_scope);
31753 constructor_p = !cp_parser_error_occurred (parser);
31757 /* We did not really want to consume any tokens. */
31758 cp_parser_abort_tentative_parse (parser);
31760 return constructor_p;
31763 /* Parse the definition of the function given by the DECL_SPECIFIERS,
31764 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
31765 they must be performed once we are in the scope of the function.
31767 Returns the function defined. */
31769 static tree
31770 cp_parser_function_definition_from_specifiers_and_declarator
31771 (cp_parser* parser,
31772 cp_decl_specifier_seq *decl_specifiers,
31773 tree attributes,
31774 const cp_declarator *declarator)
31776 tree fn;
31777 bool success_p;
31779 /* Begin the function-definition. */
31780 success_p = start_function (decl_specifiers, declarator, attributes);
31782 /* The things we're about to see are not directly qualified by any
31783 template headers we've seen thus far. */
31784 reset_specialization ();
31786 /* If there were names looked up in the decl-specifier-seq that we
31787 did not check, check them now. We must wait until we are in the
31788 scope of the function to perform the checks, since the function
31789 might be a friend. */
31790 perform_deferred_access_checks (tf_warning_or_error);
31792 if (success_p)
31794 cp_finalize_omp_declare_simd (parser, current_function_decl);
31795 parser->omp_declare_simd = NULL;
31796 cp_finalize_oacc_routine (parser, current_function_decl, true);
31797 parser->oacc_routine = NULL;
31800 if (!success_p)
31802 /* Skip the entire function. */
31803 cp_parser_skip_to_end_of_block_or_statement (parser);
31804 fn = error_mark_node;
31806 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
31808 /* Seen already, skip it. An error message has already been output. */
31809 cp_parser_skip_to_end_of_block_or_statement (parser);
31810 fn = current_function_decl;
31811 current_function_decl = NULL_TREE;
31812 /* If this is a function from a class, pop the nested class. */
31813 if (current_class_name)
31814 pop_nested_class ();
31816 else
31818 auto_timevar tv (DECL_DECLARED_INLINE_P (current_function_decl)
31819 ? TV_PARSE_INLINE : TV_PARSE_FUNC);
31820 fn = cp_parser_function_definition_after_declarator (parser,
31821 /*inline_p=*/false);
31824 return fn;
31827 /* Parse the part of a function-definition that follows the
31828 declarator. INLINE_P is TRUE iff this function is an inline
31829 function defined within a class-specifier.
31831 Returns the function defined. */
31833 static tree
31834 cp_parser_function_definition_after_declarator (cp_parser* parser,
31835 bool inline_p)
31837 tree fn;
31838 bool saved_in_unbraced_linkage_specification_p;
31839 bool saved_in_function_body;
31840 unsigned saved_num_template_parameter_lists;
31841 cp_token *token;
31842 bool fully_implicit_function_template_p
31843 = parser->fully_implicit_function_template_p;
31844 parser->fully_implicit_function_template_p = false;
31845 tree implicit_template_parms
31846 = parser->implicit_template_parms;
31847 parser->implicit_template_parms = 0;
31848 cp_binding_level* implicit_template_scope
31849 = parser->implicit_template_scope;
31850 parser->implicit_template_scope = 0;
31852 saved_in_function_body = parser->in_function_body;
31853 parser->in_function_body = true;
31854 /* If the next token is `return', then the code may be trying to
31855 make use of the "named return value" extension that G++ used to
31856 support. */
31857 token = cp_lexer_peek_token (parser->lexer);
31858 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
31860 /* Consume the `return' keyword. */
31861 cp_lexer_consume_token (parser->lexer);
31862 /* Look for the identifier that indicates what value is to be
31863 returned. */
31864 cp_parser_identifier (parser);
31865 /* Issue an error message. */
31866 error_at (token->location,
31867 "named return values are no longer supported");
31868 /* Skip tokens until we reach the start of the function body. */
31869 while (true)
31871 cp_token *token = cp_lexer_peek_token (parser->lexer);
31872 if (token->type == CPP_OPEN_BRACE
31873 || token->type == CPP_EOF
31874 || token->type == CPP_PRAGMA_EOL)
31875 break;
31876 cp_lexer_consume_token (parser->lexer);
31879 /* The `extern' in `extern "C" void f () { ... }' does not apply to
31880 anything declared inside `f'. */
31881 saved_in_unbraced_linkage_specification_p
31882 = parser->in_unbraced_linkage_specification_p;
31883 parser->in_unbraced_linkage_specification_p = false;
31884 /* Inside the function, surrounding template-parameter-lists do not
31885 apply. */
31886 saved_num_template_parameter_lists
31887 = parser->num_template_parameter_lists;
31888 parser->num_template_parameter_lists = 0;
31890 /* If the next token is `try', `__transaction_atomic', or
31891 `__transaction_relaxed`, then we are looking at either function-try-block
31892 or function-transaction-block. Note that all of these include the
31893 function-body. */
31894 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
31895 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
31896 else if (cp_lexer_next_token_is_keyword (parser->lexer,
31897 RID_TRANSACTION_RELAXED))
31898 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
31899 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
31900 cp_parser_function_try_block (parser);
31901 else
31902 cp_parser_ctor_initializer_opt_and_function_body
31903 (parser, /*in_function_try_block=*/false);
31905 /* Finish the function. */
31906 fn = finish_function (inline_p);
31908 if (modules_p ()
31909 && !inline_p
31910 && TYPE_P (DECL_CONTEXT (fn))
31911 && (DECL_DECLARED_INLINE_P (fn)
31912 || processing_template_decl))
31913 set_defining_module (fn);
31915 /* Generate code for it, if necessary. */
31916 expand_or_defer_fn (fn);
31918 /* Restore the saved values. */
31919 parser->in_unbraced_linkage_specification_p
31920 = saved_in_unbraced_linkage_specification_p;
31921 parser->num_template_parameter_lists
31922 = saved_num_template_parameter_lists;
31923 parser->in_function_body = saved_in_function_body;
31925 parser->fully_implicit_function_template_p
31926 = fully_implicit_function_template_p;
31927 parser->implicit_template_parms
31928 = implicit_template_parms;
31929 parser->implicit_template_scope
31930 = implicit_template_scope;
31932 if (parser->fully_implicit_function_template_p)
31933 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
31935 return fn;
31938 /* Parse a template-declaration body (following argument list). */
31940 static void
31941 cp_parser_template_declaration_after_parameters (cp_parser* parser,
31942 tree parameter_list,
31943 bool member_p)
31945 tree decl = NULL_TREE;
31946 bool friend_p = false;
31948 /* We just processed one more parameter list. */
31949 ++parser->num_template_parameter_lists;
31951 /* Get the deferred access checks from the parameter list. These
31952 will be checked once we know what is being declared, as for a
31953 member template the checks must be performed in the scope of the
31954 class containing the member. */
31955 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
31957 /* Tentatively parse for a new template parameter list, which can either be
31958 the template keyword or a template introduction. */
31959 if (cp_parser_template_declaration_after_export (parser, member_p))
31960 /* OK */;
31961 else if (cxx_dialect >= cxx11
31962 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
31963 decl = cp_parser_alias_declaration (parser);
31964 else if (flag_concepts
31965 && cp_lexer_next_token_is_keyword (parser->lexer, RID_CONCEPT)
31966 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
31967 /* -fconcept-ts 'concept bool' syntax is handled below, in
31968 cp_parser_single_declaration. */
31969 decl = cp_parser_concept_definition (parser);
31970 else
31972 cp_token *token = cp_lexer_peek_token (parser->lexer);
31973 decl = cp_parser_single_declaration (parser,
31974 checks,
31975 member_p,
31976 /*explicit_specialization_p=*/false,
31977 &friend_p);
31979 /* If this is a member template declaration, let the front
31980 end know. */
31981 if (member_p && !friend_p && decl)
31983 if (TREE_CODE (decl) == TYPE_DECL)
31984 cp_parser_check_access_in_redeclaration (decl, token->location);
31986 decl = finish_member_template_decl (decl);
31988 else if (friend_p && decl
31989 && DECL_DECLARES_TYPE_P (decl))
31990 make_friend_class (current_class_type, TREE_TYPE (decl),
31991 /*complain=*/true);
31993 /* We are done with the current parameter list. */
31994 --parser->num_template_parameter_lists;
31996 pop_deferring_access_checks ();
31998 /* Finish up. */
31999 finish_template_decl (parameter_list);
32001 /* Check the template arguments for a literal operator template. */
32002 if (decl
32003 && DECL_DECLARES_FUNCTION_P (decl)
32004 && UDLIT_OPER_P (DECL_NAME (decl)))
32006 bool ok = true;
32007 if (parameter_list == NULL_TREE)
32008 ok = false;
32009 else
32011 int num_parms = TREE_VEC_LENGTH (parameter_list);
32012 if (num_parms == 1)
32014 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
32015 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
32016 if (TREE_CODE (parm) != PARM_DECL)
32017 ok = false;
32018 else if (MAYBE_CLASS_TYPE_P (TREE_TYPE (parm))
32019 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
32020 /* OK, C++20 string literal operator template. We don't need
32021 to warn in lower dialects here because we will have already
32022 warned about the template parameter. */;
32023 else if (TREE_TYPE (parm) != char_type_node
32024 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
32025 ok = false;
32027 else if (num_parms == 2 && cxx_dialect >= cxx14)
32029 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
32030 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
32031 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
32032 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
32033 if (TREE_CODE (parm) != PARM_DECL
32034 || TREE_TYPE (parm) != TREE_TYPE (type)
32035 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
32036 ok = false;
32037 else
32038 /* http://cplusplus.github.io/EWG/ewg-active.html#66 */
32039 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
32040 "ISO C++ did not adopt string literal operator templa"
32041 "tes taking an argument pack of characters");
32043 else
32044 ok = false;
32046 if (!ok)
32048 if (cxx_dialect > cxx17)
32049 error_at (DECL_SOURCE_LOCATION (decl), "literal operator "
32050 "template %qD has invalid parameter list; expected "
32051 "non-type template parameter pack %<<char...>%> or "
32052 "single non-type parameter of class type",
32053 decl);
32054 else
32055 error_at (DECL_SOURCE_LOCATION (decl), "literal operator "
32056 "template %qD has invalid parameter list; expected "
32057 "non-type template parameter pack %<<char...>%>",
32058 decl);
32062 /* Register member declarations. */
32063 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
32064 finish_member_declaration (decl);
32065 /* If DECL is a function template, we must return to parse it later.
32066 (Even though there is no definition, there might be default
32067 arguments that need handling.) */
32068 if (member_p && decl
32069 && DECL_DECLARES_FUNCTION_P (decl))
32070 vec_safe_push (unparsed_funs_with_definitions, decl);
32073 /* Parse a template introduction header for a template-declaration. Returns
32074 false if tentative parse fails. */
32076 static bool
32077 cp_parser_template_introduction (cp_parser* parser, bool member_p)
32079 cp_parser_parse_tentatively (parser);
32081 tree saved_scope = parser->scope;
32082 tree saved_object_scope = parser->object_scope;
32083 tree saved_qualifying_scope = parser->qualifying_scope;
32084 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32086 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
32088 /* In classes don't parse valid unnamed bitfields as invalid
32089 template introductions. */
32090 if (member_p)
32091 parser->colon_corrects_to_scope_p = false;
32093 /* Look for the optional `::' operator. */
32094 cp_parser_global_scope_opt (parser,
32095 /*current_scope_valid_p=*/false);
32096 /* Look for the nested-name-specifier. */
32097 cp_parser_nested_name_specifier_opt (parser,
32098 /*typename_keyword_p=*/false,
32099 /*check_dependency_p=*/true,
32100 /*type_p=*/false,
32101 /*is_declaration=*/false);
32103 cp_token *token = cp_lexer_peek_token (parser->lexer);
32104 tree concept_name = cp_parser_identifier (parser);
32106 /* Look up the concept for which we will be matching
32107 template parameters. */
32108 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
32109 token->location);
32110 parser->scope = saved_scope;
32111 parser->object_scope = saved_object_scope;
32112 parser->qualifying_scope = saved_qualifying_scope;
32113 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32115 if (concept_name == error_mark_node
32116 || (seen_error () && !concept_definition_p (tmpl_decl)))
32117 cp_parser_simulate_error (parser);
32119 /* Look for opening brace for introduction. */
32120 matching_braces braces;
32121 braces.require_open (parser);
32122 location_t open_loc = input_location;
32124 if (!cp_parser_parse_definitely (parser))
32125 return false;
32127 push_deferring_access_checks (dk_deferred);
32129 /* Build vector of placeholder parameters and grab
32130 matching identifiers. */
32131 tree introduction_list = cp_parser_introduction_list (parser);
32133 /* Look for closing brace for introduction. */
32134 if (!braces.require_close (parser))
32135 return true;
32137 /* The introduction-list shall not be empty. */
32138 int nargs = TREE_VEC_LENGTH (introduction_list);
32139 if (nargs == 0)
32141 /* In cp_parser_introduction_list we have already issued an error. */
32142 return true;
32145 if (tmpl_decl == error_mark_node)
32147 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
32148 token->location);
32149 return true;
32152 /* Build and associate the constraint. */
32153 location_t introduction_loc = make_location (open_loc,
32154 start_token->location,
32155 parser->lexer);
32156 tree parms = finish_template_introduction (tmpl_decl,
32157 introduction_list,
32158 introduction_loc);
32159 if (parms && parms != error_mark_node)
32161 if (!flag_concepts_ts)
32162 pedwarn (introduction_loc, 0, "template-introductions"
32163 " are not part of C++20 concepts; use %qs to enable",
32164 "-fconcepts-ts");
32166 cp_parser_template_declaration_after_parameters (parser, parms,
32167 member_p);
32168 return true;
32171 if (parms == NULL_TREE)
32172 error_at (token->location, "no matching concept for template-introduction");
32174 return true;
32177 /* Parse a normal template-declaration following the template keyword. */
32179 static void
32180 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
32182 tree parameter_list;
32183 bool need_lang_pop;
32184 location_t location = input_location;
32186 /* Look for the `<' token. */
32187 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
32188 return;
32189 if (at_class_scope_p () && current_function_decl)
32191 /* 14.5.2.2 [temp.mem]
32193 A local class shall not have member templates. */
32194 error_at (location,
32195 "invalid declaration of member template in local class");
32196 cp_parser_skip_to_end_of_block_or_statement (parser);
32197 return;
32199 /* [temp]
32201 A template ... shall not have C linkage. */
32202 if (current_lang_name == lang_name_c)
32204 error_at (location, "template with C linkage");
32205 maybe_show_extern_c_location ();
32206 /* Give it C++ linkage to avoid confusing other parts of the
32207 front end. */
32208 push_lang_context (lang_name_cplusplus);
32209 need_lang_pop = true;
32211 else
32212 need_lang_pop = false;
32214 /* We cannot perform access checks on the template parameter
32215 declarations until we know what is being declared, just as we
32216 cannot check the decl-specifier list. */
32217 push_deferring_access_checks (dk_deferred);
32219 /* If the next token is `>', then we have an invalid
32220 specialization. Rather than complain about an invalid template
32221 parameter, issue an error message here. */
32222 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
32224 cp_parser_error (parser, "invalid explicit specialization");
32225 begin_specialization ();
32226 parameter_list = NULL_TREE;
32228 else
32230 /* Parse the template parameters. */
32231 parameter_list = cp_parser_template_parameter_list (parser);
32234 /* Look for the `>'. */
32235 cp_parser_require_end_of_template_parameter_list (parser);
32237 /* Manage template requirements */
32238 if (flag_concepts)
32240 tree reqs = get_shorthand_constraints (current_template_parms);
32241 if (tree treqs = cp_parser_requires_clause_opt (parser, false))
32242 reqs = combine_constraint_expressions (reqs, treqs);
32243 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
32246 cp_parser_template_declaration_after_parameters (parser, parameter_list,
32247 member_p);
32249 /* For the erroneous case of a template with C linkage, we pushed an
32250 implicit C++ linkage scope; exit that scope now. */
32251 if (need_lang_pop)
32252 pop_lang_context ();
32255 /* Parse a template-declaration, assuming that the `export' (and
32256 `extern') keywords, if present, has already been scanned. MEMBER_P
32257 is as for cp_parser_template_declaration. */
32259 static bool
32260 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
32262 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
32264 cp_lexer_consume_token (parser->lexer);
32265 cp_parser_explicit_template_declaration (parser, member_p);
32266 return true;
32268 else if (flag_concepts)
32269 return cp_parser_template_introduction (parser, member_p);
32271 return false;
32274 /* Perform the deferred access checks from a template-parameter-list.
32275 CHECKS is a TREE_LIST of access checks, as returned by
32276 get_deferred_access_checks. */
32278 static void
32279 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
32281 ++processing_template_parmlist;
32282 perform_access_checks (checks, tf_warning_or_error);
32283 --processing_template_parmlist;
32286 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
32287 `function-definition' sequence that follows a template header.
32288 If MEMBER_P is true, this declaration appears in a class scope.
32290 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
32291 *FRIEND_P is set to TRUE iff the declaration is a friend. */
32293 static tree
32294 cp_parser_single_declaration (cp_parser* parser,
32295 vec<deferred_access_check, va_gc> *checks,
32296 bool member_p,
32297 bool explicit_specialization_p,
32298 bool* friend_p)
32300 int declares_class_or_enum;
32301 tree decl = NULL_TREE;
32302 cp_decl_specifier_seq decl_specifiers;
32303 bool function_definition_p = false;
32304 cp_token *decl_spec_token_start;
32306 /* This function is only used when processing a template
32307 declaration. */
32308 gcc_assert (innermost_scope_kind () == sk_template_parms
32309 || innermost_scope_kind () == sk_template_spec);
32311 /* Defer access checks until we know what is being declared. */
32312 push_deferring_access_checks (dk_deferred);
32314 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
32315 alternative. */
32316 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
32317 cp_parser_decl_specifier_seq (parser,
32318 (CP_PARSER_FLAGS_OPTIONAL
32319 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
32320 &decl_specifiers,
32321 &declares_class_or_enum);
32323 cp_omp_declare_simd_data odsd;
32324 if (decl_specifiers.attributes && (flag_openmp || flag_openmp_simd))
32325 cp_parser_handle_directive_omp_attributes (parser,
32326 &decl_specifiers.attributes,
32327 &odsd, true);
32329 if (friend_p)
32330 *friend_p = cp_parser_friend_p (&decl_specifiers);
32332 /* There are no template typedefs. */
32333 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
32335 error_at (decl_spec_token_start->location,
32336 "template declaration of %<typedef%>");
32337 decl = error_mark_node;
32340 /* Gather up the access checks that occurred the
32341 decl-specifier-seq. */
32342 stop_deferring_access_checks ();
32344 /* Check for the declaration of a template class. */
32345 if (declares_class_or_enum)
32347 if (cp_parser_declares_only_class_p (parser)
32348 || (declares_class_or_enum & 2))
32350 decl = shadow_tag (&decl_specifiers);
32352 /* In this case:
32354 struct C {
32355 friend template <typename T> struct A<T>::B;
32358 A<T>::B will be represented by a TYPENAME_TYPE, and
32359 therefore not recognized by shadow_tag. */
32360 if (friend_p && *friend_p
32361 && !decl
32362 && decl_specifiers.type
32363 && TYPE_P (decl_specifiers.type))
32364 decl = decl_specifiers.type;
32366 if (decl && decl != error_mark_node)
32367 decl = TYPE_NAME (decl);
32368 else
32369 decl = error_mark_node;
32371 /* If this is a declaration, but not a definition, associate
32372 any constraints with the type declaration. Constraints
32373 are associated with definitions in cp_parser_class_specifier. */
32374 if (declares_class_or_enum == 1)
32375 associate_classtype_constraints (TREE_TYPE (decl));
32377 /* Perform access checks for template parameters. */
32378 cp_parser_perform_template_parameter_access_checks (checks);
32380 /* Give a helpful diagnostic for
32381 template <class T> struct A { } a;
32382 if we aren't already recovering from an error. */
32383 if (!cp_parser_declares_only_class_p (parser)
32384 && !seen_error ())
32386 error_at (cp_lexer_peek_token (parser->lexer)->location,
32387 "a class template declaration must not declare "
32388 "anything else");
32389 cp_parser_skip_to_end_of_block_or_statement (parser);
32390 goto out;
32395 /* Complain about missing 'typename' or other invalid type names. */
32396 if (!decl_specifiers.any_type_specifiers_p
32397 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
32399 /* cp_parser_parse_and_diagnose_invalid_type_name calls
32400 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
32401 the rest of this declaration. */
32402 decl = error_mark_node;
32403 goto out;
32406 /* If it's not a template class, try for a template function. If
32407 the next token is a `;', then this declaration does not declare
32408 anything. But, if there were errors in the decl-specifiers, then
32409 the error might well have come from an attempted class-specifier.
32410 In that case, there's no need to warn about a missing declarator. */
32411 if (!decl
32412 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
32413 || decl_specifiers.type != error_mark_node))
32415 int flags = CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
32416 /* We don't delay parsing for friends, though CWG 2510 may change
32417 that. */
32418 if (member_p && !(friend_p && *friend_p))
32419 flags |= CP_PARSER_FLAGS_DELAY_NOEXCEPT;
32420 decl = cp_parser_init_declarator (parser,
32421 flags,
32422 &decl_specifiers,
32423 checks,
32424 /*function_definition_allowed_p=*/true,
32425 member_p,
32426 declares_class_or_enum,
32427 &function_definition_p,
32428 NULL, NULL, NULL);
32430 /* 7.1.1-1 [dcl.stc]
32432 A storage-class-specifier shall not be specified in an explicit
32433 specialization... */
32434 if (decl
32435 && explicit_specialization_p
32436 && decl_specifiers.storage_class != sc_none)
32438 error_at (decl_spec_token_start->location,
32439 "explicit template specialization cannot have a storage class");
32440 decl = error_mark_node;
32443 if (decl && VAR_P (decl))
32444 check_template_variable (decl);
32447 /* Look for a trailing `;' after the declaration. */
32448 if (!function_definition_p
32449 && (decl == error_mark_node
32450 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
32451 cp_parser_skip_to_end_of_block_or_statement (parser);
32453 out:
32454 pop_deferring_access_checks ();
32456 /* Clear any current qualification; whatever comes next is the start
32457 of something new. */
32458 parser->scope = NULL_TREE;
32459 parser->qualifying_scope = NULL_TREE;
32460 parser->object_scope = NULL_TREE;
32462 cp_finalize_omp_declare_simd (parser, &odsd);
32464 return decl;
32467 /* Parse a cast-expression that is not the operand of a unary "&". */
32469 static cp_expr
32470 cp_parser_simple_cast_expression (cp_parser *parser)
32472 return cp_parser_cast_expression (parser, /*address_p=*/false,
32473 /*cast_p=*/false, /*decltype*/false, NULL);
32476 /* Parse a functional cast to TYPE. Returns an expression
32477 representing the cast. */
32479 static cp_expr
32480 cp_parser_functional_cast (cp_parser* parser, tree type)
32482 vec<tree, va_gc> *vec;
32483 tree expression_list;
32484 cp_expr cast;
32485 bool nonconst_p;
32487 location_t start_loc = input_location;
32489 if (!type)
32490 type = error_mark_node;
32492 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
32494 cp_lexer_set_source_position (parser->lexer);
32495 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
32496 expression_list = cp_parser_braced_list (parser, &nonconst_p);
32497 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
32498 if (TREE_CODE (type) == TYPE_DECL)
32499 type = TREE_TYPE (type);
32501 cast = finish_compound_literal (type, expression_list,
32502 tf_warning_or_error, fcl_functional);
32503 /* Create a location of the form:
32504 type_name{i, f}
32505 ^~~~~~~~~~~~~~~
32506 with caret == start at the start of the type name,
32507 finishing at the closing brace. */
32508 location_t combined_loc = make_location (start_loc, start_loc,
32509 parser->lexer);
32510 cast.set_location (combined_loc);
32511 return cast;
32515 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
32516 /*cast_p=*/true,
32517 /*allow_expansion_p=*/true,
32518 /*non_constant_p=*/NULL);
32519 if (vec == NULL)
32520 expression_list = error_mark_node;
32521 else
32523 expression_list = build_tree_list_vec (vec);
32524 release_tree_vector (vec);
32527 /* Create a location of the form:
32528 float(i)
32529 ^~~~~~~~
32530 with caret == start at the start of the type name,
32531 finishing at the closing paren. */
32532 location_t combined_loc = make_location (start_loc, start_loc,
32533 parser->lexer);
32534 cast = build_functional_cast (combined_loc, type, expression_list,
32535 tf_warning_or_error);
32537 /* [expr.const]/1: In an integral constant expression "only type
32538 conversions to integral or enumeration type can be used". */
32539 if (TREE_CODE (type) == TYPE_DECL)
32540 type = TREE_TYPE (type);
32541 if (cast != error_mark_node
32542 && !cast_valid_in_integral_constant_expression_p (type)
32543 && cp_parser_non_integral_constant_expression (parser,
32544 NIC_CONSTRUCTOR))
32545 return error_mark_node;
32547 return cast;
32550 /* Save the tokens that make up the body of a member function defined
32551 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
32552 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
32553 specifiers applied to the declaration. Returns the FUNCTION_DECL
32554 for the member function. */
32556 static tree
32557 cp_parser_save_member_function_body (cp_parser* parser,
32558 cp_decl_specifier_seq *decl_specifiers,
32559 cp_declarator *declarator,
32560 tree attributes)
32562 cp_token *first;
32563 cp_token *last;
32564 tree fn;
32565 bool function_try_block = false;
32567 /* Create the FUNCTION_DECL. */
32568 fn = grokmethod (decl_specifiers, declarator, attributes);
32569 cp_finalize_omp_declare_simd (parser, fn);
32570 cp_finalize_oacc_routine (parser, fn, true);
32571 /* If something went badly wrong, bail out now. */
32572 if (fn == error_mark_node)
32574 /* If there's a function-body, skip it. */
32575 if (cp_parser_token_starts_function_definition_p
32576 (cp_lexer_peek_token (parser->lexer)))
32577 cp_parser_skip_to_end_of_block_or_statement (parser);
32578 return error_mark_node;
32581 /* Remember it, if there are default args to post process. */
32582 cp_parser_save_default_args (parser, fn);
32584 /* Save away the tokens that make up the body of the
32585 function. */
32586 first = parser->lexer->next_token;
32588 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
32589 cp_lexer_consume_token (parser->lexer);
32590 else if (cp_lexer_next_token_is_keyword (parser->lexer,
32591 RID_TRANSACTION_ATOMIC))
32593 cp_lexer_consume_token (parser->lexer);
32594 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
32595 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
32596 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
32597 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
32598 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
32599 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
32600 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
32602 cp_lexer_consume_token (parser->lexer);
32603 cp_lexer_consume_token (parser->lexer);
32604 cp_lexer_consume_token (parser->lexer);
32605 cp_lexer_consume_token (parser->lexer);
32606 cp_lexer_consume_token (parser->lexer);
32608 else
32609 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
32610 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
32612 cp_lexer_consume_token (parser->lexer);
32613 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
32614 break;
32618 /* Handle function try blocks. */
32619 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
32621 cp_lexer_consume_token (parser->lexer);
32622 function_try_block = true;
32624 /* We can have braced-init-list mem-initializers before the fn body. */
32625 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
32627 cp_lexer_consume_token (parser->lexer);
32628 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
32630 /* cache_group will stop after an un-nested { } pair, too. */
32631 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
32632 break;
32634 /* variadic mem-inits have ... after the ')'. */
32635 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
32636 cp_lexer_consume_token (parser->lexer);
32639 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
32640 /* Handle function try blocks. */
32641 if (function_try_block)
32642 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
32643 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
32644 last = parser->lexer->next_token;
32646 /* Save away the inline definition; we will process it when the
32647 class is complete. */
32648 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
32649 DECL_PENDING_INLINE_P (fn) = 1;
32651 /* We need to know that this was defined in the class, so that
32652 friend templates are handled correctly. */
32653 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
32655 /* Add FN to the queue of functions to be parsed later. */
32656 vec_safe_push (unparsed_funs_with_definitions, fn);
32658 return fn;
32661 /* Save the tokens that make up the in-class initializer for a non-static
32662 data member. Returns a DEFERRED_PARSE. */
32664 static tree
32665 cp_parser_save_nsdmi (cp_parser* parser)
32667 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
32670 /* Parse a template-argument-list, as well as the trailing ">" (but
32671 not the opening "<"). See cp_parser_template_argument_list for the
32672 return value. */
32674 static tree
32675 cp_parser_enclosed_template_argument_list (cp_parser* parser)
32677 tree arguments;
32678 tree saved_scope;
32679 tree saved_qualifying_scope;
32680 tree saved_object_scope;
32681 bool saved_greater_than_is_operator_p;
32683 /* [temp.names]
32685 When parsing a template-id, the first non-nested `>' is taken as
32686 the end of the template-argument-list rather than a greater-than
32687 operator. */
32688 saved_greater_than_is_operator_p
32689 = parser->greater_than_is_operator_p;
32690 parser->greater_than_is_operator_p = false;
32691 /* Parsing the argument list may modify SCOPE, so we save it
32692 here. */
32693 saved_scope = parser->scope;
32694 saved_qualifying_scope = parser->qualifying_scope;
32695 saved_object_scope = parser->object_scope;
32696 /* We need to evaluate the template arguments, even though this
32697 template-id may be nested within a "sizeof". */
32698 cp_evaluated ev;
32699 /* Parse the template-argument-list itself. */
32700 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
32701 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT)
32702 || cp_lexer_next_token_is (parser->lexer, CPP_GREATER_EQ)
32703 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT_EQ))
32705 arguments = make_tree_vec (0);
32706 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (arguments, 0);
32708 else
32709 arguments = cp_parser_template_argument_list (parser);
32710 /* Look for the `>' that ends the template-argument-list. If we find
32711 a '>>' instead, it's probably just a typo. */
32712 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
32714 if (cxx_dialect != cxx98)
32716 /* In C++0x, a `>>' in a template argument list or cast
32717 expression is considered to be two separate `>'
32718 tokens. So, change the current token to a `>', but don't
32719 consume it: it will be consumed later when the outer
32720 template argument list (or cast expression) is parsed.
32721 Note that this replacement of `>' for `>>' is necessary
32722 even if we are parsing tentatively: in the tentative
32723 case, after calling
32724 cp_parser_enclosed_template_argument_list we will always
32725 throw away all of the template arguments and the first
32726 closing `>', either because the template argument list
32727 was erroneous or because we are replacing those tokens
32728 with a CPP_TEMPLATE_ID token. The second `>' (which will
32729 not have been thrown away) is needed either to close an
32730 outer template argument list or to complete a new-style
32731 cast. */
32732 cp_token *token = cp_lexer_peek_token (parser->lexer);
32733 token->type = CPP_GREATER;
32735 else if (!saved_greater_than_is_operator_p)
32737 /* If we're in a nested template argument list, the '>>' has
32738 to be a typo for '> >'. We emit the error message, but we
32739 continue parsing and we push a '>' as next token, so that
32740 the argument list will be parsed correctly. Note that the
32741 global source location is still on the token before the
32742 '>>', so we need to say explicitly where we want it. */
32743 cp_token *token = cp_lexer_peek_token (parser->lexer);
32744 gcc_rich_location richloc (token->location);
32745 richloc.add_fixit_replace ("> >");
32746 error_at (&richloc, "%<>>%> should be %<> >%> "
32747 "within a nested template argument list");
32749 token->type = CPP_GREATER;
32751 else
32753 /* If this is not a nested template argument list, the '>>'
32754 is a typo for '>'. Emit an error message and continue.
32755 Same deal about the token location, but here we can get it
32756 right by consuming the '>>' before issuing the diagnostic. */
32757 cp_token *token = cp_lexer_consume_token (parser->lexer);
32758 error_at (token->location,
32759 "spurious %<>>%>, use %<>%> to terminate "
32760 "a template argument list");
32763 /* Similarly for >>= and >=. */
32764 else if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER_EQ)
32765 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT_EQ))
32767 cp_token *token = cp_lexer_consume_token (parser->lexer);
32768 gcc_rich_location richloc (token->location);
32769 enum cpp_ttype new_type;
32770 const char *replacement;
32771 if (token->type == CPP_GREATER_EQ)
32773 replacement = "> =";
32774 new_type = CPP_EQ;
32776 else if (!saved_greater_than_is_operator_p)
32778 if (cxx_dialect != cxx98)
32779 replacement = ">> =";
32780 else
32781 replacement = "> > =";
32782 new_type = CPP_GREATER;
32784 else
32786 replacement = "> >=";
32787 new_type = CPP_GREATER_EQ;
32789 richloc.add_fixit_replace (replacement);
32790 error_at (&richloc, "%qs should be %qs to terminate a template "
32791 "argument list",
32792 cpp_type2name (token->type, token->flags), replacement);
32793 token->type = new_type;
32795 else
32796 cp_parser_require_end_of_template_parameter_list (parser);
32797 /* The `>' token might be a greater-than operator again now. */
32798 parser->greater_than_is_operator_p
32799 = saved_greater_than_is_operator_p;
32800 /* Restore the SAVED_SCOPE. */
32801 parser->scope = saved_scope;
32802 parser->qualifying_scope = saved_qualifying_scope;
32803 parser->object_scope = saved_object_scope;
32805 return arguments;
32808 /* MEMBER_FUNCTION is a member function, or a friend. If default
32809 arguments, or the body of the function have not yet been parsed,
32810 parse them now. */
32812 static void
32813 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
32815 auto_timevar tv (TV_PARSE_INMETH);
32817 /* If this member is a template, get the underlying
32818 FUNCTION_DECL. */
32819 if (DECL_FUNCTION_TEMPLATE_P (member_function))
32820 member_function = DECL_TEMPLATE_RESULT (member_function);
32822 /* There should not be any class definitions in progress at this
32823 point; the bodies of members are only parsed outside of all class
32824 definitions. */
32825 gcc_assert (parser->num_classes_being_defined == 0);
32826 /* While we're parsing the member functions we might encounter more
32827 classes. We want to handle them right away, but we don't want
32828 them getting mixed up with functions that are currently in the
32829 queue. */
32830 push_unparsed_function_queues (parser);
32832 /* Make sure that any template parameters are in scope. */
32833 maybe_begin_member_template_processing (member_function);
32835 /* If the body of the function has not yet been parsed, parse it
32836 now. Except if the tokens have been purged (PR c++/39751). */
32837 if (DECL_PENDING_INLINE_P (member_function)
32838 && !DECL_PENDING_INLINE_INFO (member_function)->first->purged_p)
32840 tree function_scope;
32841 cp_token_cache *tokens;
32843 /* The function is no longer pending; we are processing it. */
32844 tokens = DECL_PENDING_INLINE_INFO (member_function);
32845 DECL_PENDING_INLINE_INFO (member_function) = NULL;
32846 DECL_PENDING_INLINE_P (member_function) = 0;
32848 /* If this is a local class, enter the scope of the containing
32849 function. */
32850 function_scope = current_function_decl;
32851 if (function_scope)
32852 push_function_context ();
32854 /* Push the body of the function onto the lexer stack. */
32855 cp_parser_push_lexer_for_tokens (parser, tokens);
32857 /* Let the front end know that we going to be defining this
32858 function. */
32859 start_preparsed_function (member_function, NULL_TREE,
32860 SF_PRE_PARSED | SF_INCLASS_INLINE);
32862 /* #pragma omp declare reduction needs special parsing. */
32863 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
32865 parser->lexer->in_pragma = true;
32866 cp_parser_omp_declare_reduction_exprs (member_function, parser);
32867 finish_function (/*inline_p=*/true);
32868 cp_check_omp_declare_reduction (member_function);
32870 else
32871 /* Now, parse the body of the function. */
32872 cp_parser_function_definition_after_declarator (parser,
32873 /*inline_p=*/true);
32875 /* Leave the scope of the containing function. */
32876 if (function_scope)
32877 pop_function_context ();
32878 cp_parser_pop_lexer (parser);
32881 /* Remove any template parameters from the symbol table. */
32882 maybe_end_member_template_processing ();
32884 /* Restore the queue. */
32885 pop_unparsed_function_queues (parser);
32888 /* If DECL contains any default args, remember it on the unparsed
32889 functions queue. */
32891 static void
32892 cp_parser_save_default_args (cp_parser* parser, tree decl)
32894 tree probe;
32896 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
32897 probe;
32898 probe = TREE_CHAIN (probe))
32899 if (TREE_PURPOSE (probe))
32901 cp_default_arg_entry entry = {current_class_type, decl};
32902 vec_safe_push (unparsed_funs_with_default_args, entry);
32903 break;
32906 /* Remember if there is a noexcept-specifier to post process. */
32907 tree spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl));
32908 if (UNPARSED_NOEXCEPT_SPEC_P (spec))
32909 vec_safe_push (unparsed_noexcepts, decl);
32911 /* Contracts are deferred. */
32912 for (tree attr = DECL_ATTRIBUTES (decl); attr; attr = TREE_CHAIN (attr))
32913 if (cxx_contract_attribute_p (attr))
32915 vec_safe_push (unparsed_contracts, decl);
32916 break;
32920 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
32921 which is either a FIELD_DECL or PARM_DECL. Parse it and return
32922 the result. For a PARM_DECL, PARMTYPE is the corresponding type
32923 from the parameter-type-list. */
32925 static tree
32926 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
32927 tree default_arg, tree parmtype)
32929 cp_token_cache *tokens;
32930 tree parsed_arg;
32931 bool dummy;
32933 if (default_arg == error_mark_node)
32934 return error_mark_node;
32936 /* Push the saved tokens for the default argument onto the parser's
32937 lexer stack. */
32938 tokens = DEFPARSE_TOKENS (default_arg);
32939 cp_parser_push_lexer_for_tokens (parser, tokens);
32941 start_lambda_scope (decl);
32943 /* Parse the default argument. */
32944 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
32945 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
32946 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
32948 finish_lambda_scope ();
32950 if (parsed_arg == error_mark_node)
32951 cp_parser_skip_to_end_of_statement (parser);
32953 if (!processing_template_decl)
32955 /* In a non-template class, check conversions now. In a template,
32956 we'll wait and instantiate these as needed. */
32957 if (TREE_CODE (decl) == PARM_DECL)
32958 parsed_arg = check_default_argument (parmtype, parsed_arg,
32959 tf_warning_or_error);
32960 else if (maybe_reject_flexarray_init (decl, parsed_arg))
32961 parsed_arg = error_mark_node;
32962 else
32963 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
32966 /* If the token stream has not been completely used up, then
32967 there was extra junk after the end of the default
32968 argument. */
32969 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
32971 if (TREE_CODE (decl) == PARM_DECL)
32972 cp_parser_error (parser, "expected %<,%>");
32973 else
32974 cp_parser_error (parser, "expected %<;%>");
32977 /* Revert to the main lexer. */
32978 cp_parser_pop_lexer (parser);
32980 return parsed_arg;
32983 /* FIELD is a non-static data member with an initializer which we saved for
32984 later; parse it now. */
32986 static void
32987 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
32989 tree def;
32991 maybe_begin_member_template_processing (field);
32993 push_unparsed_function_queues (parser);
32994 def = cp_parser_late_parse_one_default_arg (parser, field,
32995 DECL_INITIAL (field),
32996 NULL_TREE);
32997 pop_unparsed_function_queues (parser);
32999 maybe_end_member_template_processing ();
33001 DECL_INITIAL (field) = def;
33004 /* FN is a FUNCTION_DECL which may contains a parameter with an
33005 unparsed DEFERRED_PARSE. Parse the default args now. This function
33006 assumes that the current scope is the scope in which the default
33007 argument should be processed. */
33009 static void
33010 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
33012 unsigned char saved_local_variables_forbidden_p;
33014 /* While we're parsing the default args, we might (due to the
33015 statement expression extension) encounter more classes. We want
33016 to handle them right away, but we don't want them getting mixed
33017 up with default args that are currently in the queue. */
33018 push_unparsed_function_queues (parser);
33020 /* Local variable names (and the `this' keyword) may not appear
33021 in a default argument. */
33022 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
33023 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
33025 push_defarg_context (fn);
33027 begin_scope (sk_function_parms, fn);
33029 /* Gather the PARM_DECLs into a vec so we can keep track of them when
33030 pushdecl clears DECL_CHAIN. */
33031 releasing_vec parms;
33032 for (tree parmdecl = DECL_ARGUMENTS (fn); parmdecl;
33033 parmdecl = DECL_CHAIN (parmdecl))
33034 vec_safe_push (parms, parmdecl);
33036 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
33037 for (int i = 0;
33038 parm && parm != void_list_node;
33039 parm = TREE_CHAIN (parm),
33040 ++i)
33042 tree default_arg = TREE_PURPOSE (parm);
33043 tree parsed_arg;
33045 tree parmdecl = parms[i];
33046 pushdecl (parmdecl);
33048 if (!default_arg)
33049 continue;
33051 if (TREE_CODE (default_arg) != DEFERRED_PARSE)
33052 /* This can happen for a friend declaration for a function
33053 already declared with default arguments. */
33054 continue;
33056 parsed_arg
33057 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
33058 default_arg,
33059 TREE_VALUE (parm));
33060 TREE_PURPOSE (parm) = parsed_arg;
33062 /* Update any instantiations we've already created. */
33063 for (tree copy : DEFPARSE_INSTANTIATIONS (default_arg))
33064 TREE_PURPOSE (copy) = parsed_arg;
33067 pop_bindings_and_leave_scope ();
33069 /* Restore DECL_CHAINs after clobbering by pushdecl. */
33070 parm = NULL_TREE;
33071 for (int i = parms->length () - 1; i >= 0; --i)
33073 DECL_CHAIN (parms[i]) = parm;
33074 parm = parms[i];
33077 pop_defarg_context ();
33079 /* Make sure no default arg is missing. */
33080 check_default_args (fn);
33082 /* Restore the state of local_variables_forbidden_p. */
33083 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
33085 /* Restore the queue. */
33086 pop_unparsed_function_queues (parser);
33089 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
33091 sizeof ... ( identifier )
33093 where the 'sizeof' token has already been consumed. */
33095 static tree
33096 cp_parser_sizeof_pack (cp_parser *parser)
33098 /* Consume the `...'. */
33099 cp_lexer_consume_token (parser->lexer);
33100 maybe_warn_variadic_templates ();
33102 matching_parens parens;
33103 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
33104 if (paren)
33105 parens.consume_open (parser);
33106 else
33107 permerror (cp_lexer_peek_token (parser->lexer)->location,
33108 "%<sizeof...%> argument must be surrounded by parentheses");
33110 cp_token *token = cp_lexer_peek_token (parser->lexer);
33111 tree name = cp_parser_identifier (parser);
33112 if (name == error_mark_node)
33113 return error_mark_node;
33114 /* The name is not qualified. */
33115 parser->scope = NULL_TREE;
33116 parser->qualifying_scope = NULL_TREE;
33117 parser->object_scope = NULL_TREE;
33118 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
33119 if (expr == error_mark_node)
33120 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
33121 token->location);
33122 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
33123 expr = TREE_TYPE (expr);
33124 else if (TREE_CODE (expr) == CONST_DECL)
33125 expr = DECL_INITIAL (expr);
33126 expr = make_pack_expansion (expr);
33127 if (expr != error_mark_node)
33128 PACK_EXPANSION_SIZEOF_P (expr) = true;
33130 if (paren)
33131 parens.require_close (parser);
33133 return expr;
33136 /* Parse the operand of `sizeof' (or a similar operator). Returns
33137 either a TYPE or an expression, depending on the form of the
33138 input. The KEYWORD indicates which kind of expression we have
33139 encountered. */
33141 static tree
33142 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
33144 tree expr = NULL_TREE;
33145 const char *saved_message;
33146 const char *saved_message_arg;
33147 bool saved_integral_constant_expression_p;
33148 bool saved_non_integral_constant_expression_p;
33150 /* If it's a `...', then we are computing the length of a parameter
33151 pack. */
33152 if (keyword == RID_SIZEOF
33153 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
33154 return cp_parser_sizeof_pack (parser);
33156 /* Types cannot be defined in a `sizeof' expression. Save away the
33157 old message. */
33158 saved_message = parser->type_definition_forbidden_message;
33159 saved_message_arg = parser->type_definition_forbidden_message_arg;
33160 parser->type_definition_forbidden_message
33161 = G_("types may not be defined in %qs expressions");
33162 parser->type_definition_forbidden_message_arg
33163 = IDENTIFIER_POINTER (ridpointers[keyword]);
33165 /* The restrictions on constant-expressions do not apply inside
33166 sizeof expressions. */
33167 saved_integral_constant_expression_p
33168 = parser->integral_constant_expression_p;
33169 saved_non_integral_constant_expression_p
33170 = parser->non_integral_constant_expression_p;
33171 parser->integral_constant_expression_p = false;
33173 auto cleanup = make_temp_override
33174 (parser->auto_is_implicit_function_template_parm_p, false);
33176 /* Do not actually evaluate the expression. */
33177 ++cp_unevaluated_operand;
33178 ++c_inhibit_evaluation_warnings;
33179 /* If it's a `(', then we might be looking at the type-id
33180 construction. */
33181 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33183 tree type = NULL_TREE;
33185 tentative_firewall firewall (parser);
33187 /* We can't be sure yet whether we're looking at a type-id or an
33188 expression. */
33189 cp_parser_parse_tentatively (parser);
33191 matching_parens parens;
33192 parens.consume_open (parser);
33194 /* Note: as a GNU Extension, compound literals are considered
33195 postfix-expressions as they are in C99, so they are valid
33196 arguments to sizeof. See comment in cp_parser_cast_expression
33197 for details. */
33198 if (cp_parser_compound_literal_p (parser))
33199 cp_parser_simulate_error (parser);
33200 else
33202 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
33203 parser->in_type_id_in_expr_p = true;
33204 /* Look for the type-id. */
33205 type = cp_parser_type_id (parser);
33206 /* Look for the closing `)'. */
33207 parens.require_close (parser);
33208 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
33211 /* If all went well, then we're done. */
33212 if (cp_parser_parse_definitely (parser))
33213 expr = type;
33214 else
33216 /* Commit to the tentative_firewall so we get syntax errors. */
33217 cp_parser_commit_to_tentative_parse (parser);
33219 expr = cp_parser_unary_expression (parser);
33222 else
33223 expr = cp_parser_unary_expression (parser);
33225 /* Go back to evaluating expressions. */
33226 --cp_unevaluated_operand;
33227 --c_inhibit_evaluation_warnings;
33229 /* And restore the old one. */
33230 parser->type_definition_forbidden_message = saved_message;
33231 parser->type_definition_forbidden_message_arg = saved_message_arg;
33232 parser->integral_constant_expression_p
33233 = saved_integral_constant_expression_p;
33234 parser->non_integral_constant_expression_p
33235 = saved_non_integral_constant_expression_p;
33237 return expr;
33240 /* If the current declaration has no declarator, return true. */
33242 static bool
33243 cp_parser_declares_only_class_p (cp_parser *parser)
33245 /* If the next token is a `;' or a `,' then there is no
33246 declarator. */
33247 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
33248 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
33251 /* Update the DECL_SPECS to reflect the storage class indicated by
33252 KEYWORD. */
33254 static void
33255 cp_parser_set_storage_class (cp_parser *parser,
33256 cp_decl_specifier_seq *decl_specs,
33257 enum rid keyword,
33258 cp_token *token)
33260 cp_storage_class storage_class;
33262 switch (keyword)
33264 case RID_AUTO:
33265 storage_class = sc_auto;
33266 break;
33267 case RID_REGISTER:
33268 storage_class = sc_register;
33269 break;
33270 case RID_STATIC:
33271 storage_class = sc_static;
33272 break;
33273 case RID_EXTERN:
33274 storage_class = sc_extern;
33275 break;
33276 case RID_MUTABLE:
33277 storage_class = sc_mutable;
33278 break;
33279 default:
33280 gcc_unreachable ();
33283 if (parser->in_unbraced_linkage_specification_p)
33285 error_at (token->location, "invalid use of %qD in linkage specification",
33286 ridpointers[keyword]);
33287 return;
33289 else if (decl_specs->storage_class != sc_none)
33291 if (decl_specs->conflicting_specifiers_p)
33292 return;
33293 gcc_rich_location richloc (token->location);
33294 richloc.add_location_if_nearby (decl_specs->locations[ds_storage_class]);
33295 if (decl_specs->storage_class == storage_class)
33296 error_at (&richloc, "duplicate %qD specifier", ridpointers[keyword]);
33297 else
33298 error_at (&richloc,
33299 "%qD specifier conflicts with %qs",
33300 ridpointers[keyword],
33301 cp_storage_class_name[decl_specs->storage_class]);
33302 decl_specs->conflicting_specifiers_p = true;
33303 return;
33306 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
33307 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
33308 && decl_specs->gnu_thread_keyword_p)
33310 pedwarn (decl_specs->locations[ds_thread], 0,
33311 "%<__thread%> before %qD", ridpointers[keyword]);
33314 decl_specs->storage_class = storage_class;
33315 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
33317 /* A storage class specifier cannot be applied alongside a typedef
33318 specifier. If there is a typedef specifier present then set
33319 conflicting_specifiers_p which will trigger an error later
33320 on in grokdeclarator. */
33321 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
33322 && !decl_specs->conflicting_specifiers_p)
33324 gcc_rich_location richloc (token->location);
33325 richloc.add_location_if_nearby (decl_specs->locations[ds_typedef]);
33326 error_at (&richloc,
33327 "%qD specifier conflicts with %<typedef%>",
33328 ridpointers[keyword]);
33329 decl_specs->conflicting_specifiers_p = true;
33333 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
33334 is true, the type is a class or enum definition. */
33336 static void
33337 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
33338 tree type_spec,
33339 cp_token *token,
33340 bool type_definition_p)
33342 decl_specs->any_specifiers_p = true;
33344 /* If the user tries to redeclare bool, char8_t, char16_t, char32_t, or
33345 wchar_t (with, for example, in "typedef int wchar_t;") we remember that
33346 this is what happened. In system headers, we ignore these
33347 declarations so that G++ can work with system headers that are not
33348 C++-safe. */
33349 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
33350 && !type_definition_p
33351 && (type_spec == boolean_type_node
33352 || type_spec == char8_type_node
33353 || type_spec == char16_type_node
33354 || type_spec == char32_type_node
33355 || type_spec == wchar_type_node)
33356 && (decl_specs->type
33357 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
33358 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
33359 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
33360 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
33362 decl_specs->redefined_builtin_type = type_spec;
33363 set_and_check_decl_spec_loc (decl_specs,
33364 ds_redefined_builtin_type_spec,
33365 token);
33366 if (!decl_specs->type)
33368 decl_specs->type = type_spec;
33369 decl_specs->type_definition_p = false;
33370 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
33373 else if (decl_specs->type)
33374 decl_specs->multiple_types_p = true;
33375 else
33377 decl_specs->type = type_spec;
33378 decl_specs->type_definition_p = type_definition_p;
33379 decl_specs->redefined_builtin_type = NULL_TREE;
33380 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
33384 /* True iff TOKEN is the GNU keyword __thread. */
33386 static bool
33387 token_is__thread (cp_token *token)
33389 gcc_assert (token->keyword == RID_THREAD);
33390 return id_equal (token->u.value, "__thread");
33393 /* Set the location for a declarator specifier and check if it is
33394 duplicated.
33396 DECL_SPECS is the sequence of declarator specifiers onto which to
33397 set the location.
33399 DS is the single declarator specifier to set which location is to
33400 be set onto the existing sequence of declarators.
33402 LOCATION is the location for the declarator specifier to
33403 consider. */
33405 static void
33406 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
33407 cp_decl_spec ds, cp_token *token)
33409 gcc_assert (ds < ds_last);
33411 if (decl_specs == NULL)
33412 return;
33414 location_t location = token->location;
33416 if (decl_specs->locations[ds] == 0)
33418 decl_specs->locations[ds] = location;
33419 if (ds == ds_thread)
33420 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
33422 else
33424 if (ds == ds_long)
33426 if (decl_specs->locations[ds_long_long] != 0)
33427 error_at (location,
33428 "%<long long long%> is too long for GCC");
33429 else
33431 decl_specs->locations[ds_long_long] = location;
33432 pedwarn_cxx98 (location,
33433 OPT_Wlong_long,
33434 "ISO C++ 1998 does not support %<long long%>");
33437 else if (ds == ds_thread)
33439 bool gnu = token_is__thread (token);
33440 gcc_rich_location richloc (location);
33441 if (gnu != decl_specs->gnu_thread_keyword_p)
33443 richloc.add_range (decl_specs->locations[ds_thread]);
33444 error_at (&richloc,
33445 "both %<__thread%> and %<thread_local%> specified");
33447 else
33449 richloc.add_fixit_remove ();
33450 error_at (&richloc, "duplicate %qD", token->u.value);
33453 else
33455 static const char *const decl_spec_names[] = {
33456 "signed",
33457 "unsigned",
33458 "short",
33459 "long",
33460 "const",
33461 "volatile",
33462 "restrict",
33463 "inline",
33464 "virtual",
33465 "explicit",
33466 "friend",
33467 "typedef",
33468 "using",
33469 "constexpr",
33470 "__complex",
33471 "constinit",
33472 "consteval"
33474 gcc_rich_location richloc (location);
33475 richloc.add_fixit_remove ();
33476 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
33481 /* Return true iff the declarator specifier DS is present in the
33482 sequence of declarator specifiers DECL_SPECS. */
33484 bool
33485 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
33486 cp_decl_spec ds)
33488 gcc_assert (ds < ds_last);
33490 if (decl_specs == NULL)
33491 return false;
33493 return decl_specs->locations[ds] != 0;
33496 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
33497 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
33499 static bool
33500 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
33502 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
33505 /* Issue an error message indicating that TOKEN_DESC was expected.
33506 If KEYWORD is true, it indicated this function is called by
33507 cp_parser_require_keword and the required token can only be
33508 a indicated keyword.
33510 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
33511 within any error as the location of an "opening" token matching
33512 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
33513 RT_CLOSE_PAREN). */
33515 static void
33516 cp_parser_required_error (cp_parser *parser,
33517 required_token token_desc,
33518 bool keyword,
33519 location_t matching_location)
33521 if (cp_parser_simulate_error (parser))
33522 return;
33524 const char *gmsgid = NULL;
33525 switch (token_desc)
33527 case RT_NEW:
33528 gmsgid = G_("expected %<new%>");
33529 break;
33530 case RT_DELETE:
33531 gmsgid = G_("expected %<delete%>");
33532 break;
33533 case RT_RETURN:
33534 gmsgid = G_("expected %<return%>");
33535 break;
33536 case RT_WHILE:
33537 gmsgid = G_("expected %<while%>");
33538 break;
33539 case RT_EXTERN:
33540 gmsgid = G_("expected %<extern%>");
33541 break;
33542 case RT_STATIC_ASSERT:
33543 gmsgid = G_("expected %<static_assert%>");
33544 break;
33545 case RT_DECLTYPE:
33546 gmsgid = G_("expected %<decltype%>");
33547 break;
33548 case RT_OPERATOR:
33549 gmsgid = G_("expected %<operator%>");
33550 break;
33551 case RT_CLASS:
33552 gmsgid = G_("expected %<class%>");
33553 break;
33554 case RT_TEMPLATE:
33555 gmsgid = G_("expected %<template%>");
33556 break;
33557 case RT_NAMESPACE:
33558 gmsgid = G_("expected %<namespace%>");
33559 break;
33560 case RT_USING:
33561 gmsgid = G_("expected %<using%>");
33562 break;
33563 case RT_ASM:
33564 gmsgid = G_("expected %<asm%>");
33565 break;
33566 case RT_TRY:
33567 gmsgid = G_("expected %<try%>");
33568 break;
33569 case RT_CATCH:
33570 gmsgid = G_("expected %<catch%>");
33571 break;
33572 case RT_THROW:
33573 gmsgid = G_("expected %<throw%>");
33574 break;
33575 case RT_AUTO:
33576 gmsgid = G_("expected %<auto%>");
33577 break;
33578 case RT_LABEL:
33579 gmsgid = G_("expected %<__label__%>");
33580 break;
33581 case RT_AT_TRY:
33582 gmsgid = G_("expected %<@try%>");
33583 break;
33584 case RT_AT_SYNCHRONIZED:
33585 gmsgid = G_("expected %<@synchronized%>");
33586 break;
33587 case RT_AT_THROW:
33588 gmsgid = G_("expected %<@throw%>");
33589 break;
33590 case RT_TRANSACTION_ATOMIC:
33591 gmsgid = G_("expected %<__transaction_atomic%>");
33592 break;
33593 case RT_TRANSACTION_RELAXED:
33594 gmsgid = G_("expected %<__transaction_relaxed%>");
33595 break;
33596 case RT_CO_YIELD:
33597 gmsgid = G_("expected %<co_yield%>");
33598 break;
33599 default:
33600 break;
33603 if (!gmsgid && !keyword)
33605 switch (token_desc)
33607 case RT_SEMICOLON:
33608 gmsgid = G_("expected %<;%>");
33609 break;
33610 case RT_OPEN_PAREN:
33611 gmsgid = G_("expected %<(%>");
33612 break;
33613 case RT_CLOSE_BRACE:
33614 gmsgid = G_("expected %<}%>");
33615 break;
33616 case RT_OPEN_BRACE:
33617 gmsgid = G_("expected %<{%>");
33618 break;
33619 case RT_CLOSE_SQUARE:
33620 gmsgid = G_("expected %<]%>");
33621 break;
33622 case RT_OPEN_SQUARE:
33623 gmsgid = G_("expected %<[%>");
33624 break;
33625 case RT_COMMA:
33626 gmsgid = G_("expected %<,%>");
33627 break;
33628 case RT_SCOPE:
33629 gmsgid = G_("expected %<::%>");
33630 break;
33631 case RT_LESS:
33632 gmsgid = G_("expected %<<%>");
33633 break;
33634 case RT_GREATER:
33635 gmsgid = G_("expected %<>%>");
33636 break;
33637 case RT_EQ:
33638 gmsgid = G_("expected %<=%>");
33639 break;
33640 case RT_ELLIPSIS:
33641 gmsgid = G_("expected %<...%>");
33642 break;
33643 case RT_MULT:
33644 gmsgid = G_("expected %<*%>");
33645 break;
33646 case RT_COMPL:
33647 gmsgid = G_("expected %<~%>");
33648 break;
33649 case RT_COLON:
33650 gmsgid = G_("expected %<:%>");
33651 break;
33652 case RT_COLON_SCOPE:
33653 gmsgid = G_("expected %<:%> or %<::%>");
33654 break;
33655 case RT_CLOSE_PAREN:
33656 gmsgid = G_("expected %<)%>");
33657 break;
33658 case RT_COMMA_CLOSE_PAREN:
33659 gmsgid = G_("expected %<,%> or %<)%>");
33660 break;
33661 case RT_PRAGMA_EOL:
33662 gmsgid = G_("expected end of line");
33663 break;
33664 case RT_NAME:
33665 gmsgid = G_("expected identifier");
33666 break;
33667 case RT_SELECT:
33668 gmsgid = G_("expected selection-statement");
33669 break;
33670 case RT_ITERATION:
33671 gmsgid = G_("expected iteration-statement");
33672 break;
33673 case RT_JUMP:
33674 gmsgid = G_("expected jump-statement");
33675 break;
33676 case RT_CLASS_KEY:
33677 gmsgid = G_("expected class-key");
33678 break;
33679 case RT_CLASS_TYPENAME_TEMPLATE:
33680 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
33681 break;
33682 default:
33683 gcc_unreachable ();
33687 if (gmsgid)
33688 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
33692 /* If the next token is of the indicated TYPE, consume it. Otherwise,
33693 issue an error message indicating that TOKEN_DESC was expected.
33695 Returns the token consumed, if the token had the appropriate type.
33696 Otherwise, returns NULL.
33698 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
33699 within any error as the location of an "opening" token matching
33700 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
33701 RT_CLOSE_PAREN). */
33703 static cp_token *
33704 cp_parser_require (cp_parser* parser,
33705 enum cpp_ttype type,
33706 required_token token_desc,
33707 location_t matching_location)
33709 if (cp_lexer_next_token_is (parser->lexer, type))
33710 return cp_lexer_consume_token (parser->lexer);
33711 else
33713 /* Output the MESSAGE -- unless we're parsing tentatively. */
33714 if (!cp_parser_simulate_error (parser))
33715 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
33716 matching_location);
33717 return NULL;
33721 /* Skip an entire parameter list from start to finish. The next token must
33722 be the initial "<" of the parameter list. Returns true on success and
33723 false otherwise. */
33725 static bool
33726 cp_parser_skip_entire_template_parameter_list (cp_parser* parser)
33728 /* Consume the "<" because cp_parser_skip_to_end_of_template_parameter_list
33729 requires it. */
33730 cp_lexer_consume_token (parser->lexer);
33731 return cp_parser_skip_to_end_of_template_parameter_list (parser);
33734 /* Ensure we are at the end of a template parameter list. If we are, return.
33735 If we are not, something has gone wrong, in which case issue an error and
33736 skip to end of the parameter list. */
33738 static void
33739 cp_parser_require_end_of_template_parameter_list (cp_parser* parser)
33741 /* Are we ready, yet? If not, issue error message. */
33742 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
33743 return;
33745 cp_parser_skip_to_end_of_template_parameter_list (parser);
33748 /* You should only call this function from inside a template parameter list
33749 (i.e. the current token should at least be the initial "<" of the
33750 parameter list). If you are skipping the entire list, it may be better to
33751 use cp_parser_skip_entire_template_parameter_list.
33753 Tokens are skipped until the final ">" is found, or if we see
33754 '{', '}', ';', or if we find an unbalanced ')' or ']'.
33756 Returns true if we successfully reached the end, and false if
33757 something unexpected happened (e.g. end of file). */
33759 static bool
33760 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
33762 /* Current level of '< ... >'. */
33763 unsigned level = 0;
33764 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
33765 unsigned nesting_depth = 0;
33767 /* Skip tokens until the desired token is found. */
33768 while (true)
33770 /* Peek at the next token. */
33771 switch (cp_lexer_peek_token (parser->lexer)->type)
33773 case CPP_LESS:
33774 if (!nesting_depth)
33775 ++level;
33776 break;
33778 case CPP_RSHIFT:
33779 if (cxx_dialect == cxx98)
33780 /* C++0x views the `>>' operator as two `>' tokens, but
33781 C++98 does not. */
33782 break;
33783 else if (!nesting_depth && level-- == 0)
33785 /* We've hit a `>>' where the first `>' closes the
33786 template argument list, and the second `>' is
33787 spurious. Just consume the `>>' and stop; we've
33788 already produced at least one error. */
33789 cp_lexer_consume_token (parser->lexer);
33790 return false;
33792 /* Fall through for C++0x, so we handle the second `>' in
33793 the `>>'. */
33794 gcc_fallthrough ();
33796 case CPP_GREATER:
33797 if (!nesting_depth && level-- == 0)
33799 /* We've reached the token we want, consume it and stop. */
33800 cp_lexer_consume_token (parser->lexer);
33801 return true;
33803 break;
33805 case CPP_OPEN_PAREN:
33806 case CPP_OPEN_SQUARE:
33807 ++nesting_depth;
33808 break;
33810 case CPP_CLOSE_PAREN:
33811 case CPP_CLOSE_SQUARE:
33812 if (nesting_depth-- == 0)
33813 return false;
33814 break;
33816 case CPP_EOF:
33817 case CPP_PRAGMA_EOL:
33818 case CPP_SEMICOLON:
33819 case CPP_OPEN_BRACE:
33820 case CPP_CLOSE_BRACE:
33821 /* The '>' was probably forgotten, don't look further. */
33822 return false;
33824 default:
33825 break;
33828 /* Consume this token. */
33829 cp_lexer_consume_token (parser->lexer);
33833 /* If the next token is the indicated keyword, consume it. Otherwise,
33834 issue an error message indicating that TOKEN_DESC was expected.
33836 Returns the token consumed, if the token had the appropriate type.
33837 Otherwise, returns NULL. */
33839 static cp_token *
33840 cp_parser_require_keyword (cp_parser* parser,
33841 enum rid keyword,
33842 required_token token_desc)
33844 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
33846 if (token && token->keyword != keyword)
33848 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
33849 UNKNOWN_LOCATION);
33850 return NULL;
33853 return token;
33856 /* Returns TRUE iff TOKEN is a token that can begin the body of a
33857 function-definition. */
33859 static bool
33860 cp_parser_token_starts_function_definition_p (cp_token* token)
33862 return (/* An ordinary function-body begins with an `{'. */
33863 token->type == CPP_OPEN_BRACE
33864 /* A ctor-initializer begins with a `:'. */
33865 || token->type == CPP_COLON
33866 /* A function-try-block begins with `try'. */
33867 || token->keyword == RID_TRY
33868 /* A function-transaction-block begins with `__transaction_atomic'
33869 or `__transaction_relaxed'. */
33870 || token->keyword == RID_TRANSACTION_ATOMIC
33871 || token->keyword == RID_TRANSACTION_RELAXED
33872 /* The named return value extension begins with `return'. */
33873 || token->keyword == RID_RETURN);
33876 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
33877 definition. */
33879 static bool
33880 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
33882 cp_token *token;
33884 token = cp_lexer_peek_token (parser->lexer);
33885 return (token->type == CPP_OPEN_BRACE
33886 || (token->type == CPP_COLON
33887 && !parser->colon_doesnt_start_class_def_p));
33890 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
33891 C++0x) ending a template-argument. */
33893 static bool
33894 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
33896 cp_token *token;
33898 token = cp_lexer_peek_token (parser->lexer);
33899 return (token->type == CPP_COMMA
33900 || token->type == CPP_GREATER
33901 || token->type == CPP_ELLIPSIS
33902 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)
33903 /* For better diagnostics, treat >>= like that too, that
33904 shouldn't appear non-nested in template arguments. */
33905 || token->type == CPP_RSHIFT_EQ);
33908 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
33909 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
33911 static bool
33912 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
33913 size_t n)
33915 cp_token *token;
33917 token = cp_lexer_peek_nth_token (parser->lexer, n);
33918 if (token->type == CPP_LESS)
33919 return true;
33920 /* Check for the sequence `<::' in the original code. It would be lexed as
33921 `[:', where `[' is a digraph, and there is no whitespace before
33922 `:'. */
33923 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
33925 cp_token *token2;
33926 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
33927 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
33928 return true;
33930 return false;
33933 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
33934 or none_type otherwise. */
33936 static enum tag_types
33937 cp_parser_token_is_class_key (cp_token* token)
33939 switch (token->keyword)
33941 case RID_CLASS:
33942 return class_type;
33943 case RID_STRUCT:
33944 return record_type;
33945 case RID_UNION:
33946 return union_type;
33948 default:
33949 return none_type;
33953 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
33954 or none_type otherwise or if the token is null. */
33956 static enum tag_types
33957 cp_parser_token_is_type_parameter_key (cp_token* token)
33959 if (!token)
33960 return none_type;
33962 switch (token->keyword)
33964 case RID_CLASS:
33965 return class_type;
33966 case RID_TYPENAME:
33967 return typename_type;
33969 default:
33970 return none_type;
33974 /* Diagnose redundant enum-keys. */
33976 static void
33977 cp_parser_maybe_warn_enum_key (cp_parser *parser, location_t key_loc,
33978 tree type, rid scoped_key)
33980 if (!warn_redundant_tags)
33981 return;
33983 tree type_decl = TYPE_MAIN_DECL (type);
33984 tree name = DECL_NAME (type_decl);
33985 /* Look up the NAME to see if it unambiguously refers to the TYPE. */
33986 push_deferring_access_checks (dk_no_check);
33987 tree decl = cp_parser_lookup_name_simple (parser, name, input_location);
33988 pop_deferring_access_checks ();
33990 /* The enum-key is redundant for uses of the TYPE that are not
33991 declarations and for which name lookup returns just the type
33992 itself. */
33993 if (decl != type_decl)
33994 return;
33996 if (scoped_key != RID_CLASS
33997 && scoped_key != RID_STRUCT
33998 && current_lang_name != lang_name_cplusplus
33999 && current_namespace == global_namespace)
34001 /* Avoid issuing the diagnostic for apparently redundant (unscoped)
34002 enum tag in shared C/C++ code in files (such as headers) included
34003 in the main source file. */
34004 const line_map_ordinary *map = NULL;
34005 linemap_resolve_location (line_table, key_loc,
34006 LRK_MACRO_DEFINITION_LOCATION,
34007 &map);
34008 if (!MAIN_FILE_P (map))
34009 return;
34012 gcc_rich_location richloc (key_loc);
34013 richloc.add_fixit_remove (key_loc);
34014 warning_at (&richloc, OPT_Wredundant_tags,
34015 "redundant enum-key %<enum%s%> in reference to %q#T",
34016 (scoped_key == RID_CLASS ? " class"
34017 : scoped_key == RID_STRUCT ? " struct" : ""), type);
34020 /* Describes the set of declarations of a struct, class, or class template
34021 or its specializations. Used for -Wmismatched-tags. */
34023 class class_decl_loc_t
34025 public:
34027 class_decl_loc_t ()
34028 : locvec (), idxdef (), def_class_key ()
34030 locvec.create (4);
34033 /* Constructs an object for a single declaration of a class with
34034 CLASS_KEY at the current location in the current function (or
34035 at another scope). KEY_REDUNDANT is true if the class-key may
34036 be omitted in the current context without an ambiguity with
34037 another symbol with the same name.
34038 DEF_P is true for a class declaration that is a definition.
34039 CURLOC is the associated location. */
34040 class_decl_loc_t (tag_types class_key, bool key_redundant, bool def_p,
34041 location_t curloc = input_location)
34042 : locvec (), idxdef (def_p ? 0 : UINT_MAX), def_class_key (class_key)
34044 locvec.create (4);
34045 class_key_loc_t ckl (current_function_decl, curloc, class_key,
34046 key_redundant);
34047 locvec.quick_push (ckl);
34050 /* Copy, assign, and destroy the object. Necessary because LOCVEC
34051 isn't safely copyable and assignable and doesn't release storage
34052 on its own. */
34053 class_decl_loc_t (const class_decl_loc_t &rhs)
34054 : locvec (rhs.locvec.copy ()), idxdef (rhs.idxdef),
34055 def_class_key (rhs.def_class_key)
34058 class_decl_loc_t& operator= (const class_decl_loc_t &rhs)
34060 if (this == &rhs)
34061 return *this;
34062 locvec.release ();
34063 locvec = rhs.locvec.copy ();
34064 idxdef = rhs.idxdef;
34065 def_class_key = rhs.def_class_key;
34066 return *this;
34069 ~class_decl_loc_t ()
34071 locvec.release ();
34074 /* Issues -Wmismatched-tags for a single class. */
34075 void diag_mismatched_tags (tree);
34077 /* Issues -Wmismatched-tags for all classes. */
34078 static void diag_mismatched_tags ();
34080 /* Adds TYPE_DECL to the collection of class decls and diagnoses
34081 redundant tags (if -Wredundant-tags is enabled). */
34082 static void add (cp_parser *, location_t, tag_types, tree, bool, bool);
34084 /* Either adds this decl to the collection of class decls
34085 or diagnoses it, whichever is appropriate. */
34086 void add_or_diag_mismatched_tag (tree, tag_types, bool, bool);
34088 private:
34090 tree function (unsigned i) const
34092 return locvec[i].func;
34095 location_t location (unsigned i) const
34097 return locvec[i].loc;
34100 bool key_redundant (unsigned i) const
34102 return locvec[i].key_redundant;
34105 tag_types class_key (unsigned i) const
34107 return locvec[i].class_key;
34110 /* True if a definition for the class has been seen. */
34111 bool def_p () const
34113 return idxdef < locvec.length ();
34116 /* The location of a single mention of a class type with the given
34117 class-key. */
34118 struct class_key_loc_t
34120 class_key_loc_t (tree func, location_t loc, tag_types key, bool redundant)
34121 : func (func), loc (loc), class_key (key), key_redundant (redundant)
34124 /* The function the type is mentioned in. */
34125 tree func;
34126 /* The exact location. */
34127 location_t loc;
34128 /* The class-key used in the mention of the type. */
34129 tag_types class_key;
34130 /* True when the class-key could be omitted at this location
34131 without an ambiguity with another symbol of the same name. */
34132 bool key_redundant;
34134 /* Avoid using auto_vec here since it's not safe to copy due to pr90904. */
34135 vec <class_key_loc_t> locvec;
34136 /* LOCVEC index of the definition or UINT_MAX if none exists. */
34137 unsigned idxdef;
34138 /* The class-key the class was last declared with or none_type when
34139 it has been declared with a mismatched key. */
34140 tag_types def_class_key;
34142 /* A mapping between a TYPE_DECL for a class and the class_decl_loc_t
34143 description above. */
34144 typedef hash_map<tree_decl_hash, class_decl_loc_t> class_to_loc_map_t;
34145 static class_to_loc_map_t class2loc;
34148 class_decl_loc_t::class_to_loc_map_t class_decl_loc_t::class2loc;
34150 /* Issue an error message if the CLASS_KEY does not match the TYPE.
34151 DEF_P is expected to be set for a definition of class TYPE. DECL_P
34152 is set for a declaration of class TYPE and clear for a reference to
34153 it that is not a declaration of it. */
34155 static void
34156 cp_parser_check_class_key (cp_parser *parser, location_t key_loc,
34157 tag_types class_key, tree type, bool def_p,
34158 bool decl_p)
34160 if (type == error_mark_node)
34161 return;
34163 bool seen_as_union = TREE_CODE (type) == UNION_TYPE;
34164 if (seen_as_union != (class_key == union_type))
34166 if (permerror (input_location, "%qs tag used in naming %q#T",
34167 class_key == union_type ? "union"
34168 : class_key == record_type ? "struct" : "class",
34169 type))
34170 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
34171 "%q#T was previously declared here", type);
34172 return;
34175 if (!warn_mismatched_tags && !warn_redundant_tags)
34176 return;
34178 /* Only consider the true class-keys below and ignore typename_type,
34179 etc. that are not C++ class-keys. */
34180 if (class_key != class_type
34181 && class_key != record_type
34182 && class_key != union_type)
34183 return;
34185 class_decl_loc_t::add (parser, key_loc, class_key, type, def_p, decl_p);
34188 /* Returns the template or specialization of one to which the RECORD_TYPE
34189 TYPE corresponds. */
34191 static tree
34192 specialization_of (tree type)
34194 tree ret = type;
34196 /* Determine the template or its partial specialization to which TYPE
34197 corresponds. */
34198 if (tree spec = most_specialized_partial_spec (type, tf_none))
34199 if (spec != error_mark_node)
34200 ret = TREE_TYPE (TREE_VALUE (spec));
34202 if (ret == type)
34203 ret = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (type);
34205 return TYPE_MAIN_DECL (ret);
34209 /* Adds the class TYPE to the collection of class decls and diagnoses
34210 redundant tags (if -Wredundant-tags is enabled).
34211 DEF_P is expected to be set for a definition of class TYPE. DECL_P
34212 is set for a (likely, based on syntactic context) declaration of class
34213 TYPE and clear for a reference to it that is not a declaration of it. */
34215 void
34216 class_decl_loc_t::add (cp_parser *parser, location_t key_loc,
34217 tag_types class_key, tree type, bool def_p, bool decl_p)
34219 tree type_decl = TYPE_MAIN_DECL (type);
34220 tree name = DECL_NAME (type_decl);
34221 /* Look up the NAME to see if it unambiguously refers to the TYPE
34222 and set KEY_REDUNDANT if so. */
34223 push_deferring_access_checks (dk_no_check);
34224 tree decl = cp_parser_lookup_name_simple (parser, name, input_location);
34225 pop_deferring_access_checks ();
34227 /* The class-key is redundant for uses of the CLASS_TYPE that are
34228 neither definitions of it nor declarations, and for which name
34229 lookup returns just the type itself. */
34230 bool key_redundant = (!def_p && !decl_p
34231 && (decl == type_decl
34232 || TREE_CODE (decl) == TEMPLATE_DECL
34233 || (CLASS_TYPE_P (type)
34234 && TYPE_BEING_DEFINED (type))));
34236 if (key_redundant
34237 && class_key != class_type
34238 && current_lang_name != lang_name_cplusplus
34239 && current_namespace == global_namespace)
34241 /* Avoid issuing the diagnostic for apparently redundant struct
34242 and union class-keys in shared C/C++ code in files (such as
34243 headers) included in the main source file. */
34244 const line_map_ordinary *map = NULL;
34245 linemap_resolve_location (line_table, key_loc,
34246 LRK_MACRO_DEFINITION_LOCATION,
34247 &map);
34248 if (!MAIN_FILE_P (map))
34249 key_redundant = false;
34252 /* Set if a declaration of TYPE has previously been seen or if it must
34253 exist in a precompiled header. */
34254 bool exist;
34255 class_decl_loc_t *rdl = &class2loc.get_or_insert (type_decl, &exist);
34256 if (!exist)
34258 tree type = TREE_TYPE (type_decl);
34259 if (def_p || !COMPLETE_TYPE_P (type))
34261 /* TYPE_DECL is the first declaration or definition of the type
34262 (outside precompiled headers -- see below). Just create
34263 a new entry for it and return unless it's a declaration
34264 involving a template that may need to be diagnosed by
34265 -Wredundant-tags. */
34266 *rdl = class_decl_loc_t (class_key, false, def_p);
34267 if (TREE_CODE (decl) != TEMPLATE_DECL)
34268 return;
34270 else
34272 /* TYPE was previously defined in some unknown precompiled header.
34273 Simply add a record of its definition at an unknown location and
34274 proceed below to add a reference to it at the current location.
34275 (Declarations in precompiled headers that are not definitions
34276 are ignored.) */
34277 tag_types def_key
34278 = CLASSTYPE_DECLARED_CLASS (type) ? class_type : record_type;
34279 location_t def_loc = DECL_SOURCE_LOCATION (type_decl);
34280 *rdl = class_decl_loc_t (def_key, false, true, def_loc);
34281 exist = true;
34285 /* A prior declaration of TYPE_DECL has been seen. */
34287 if (key_redundant)
34289 gcc_rich_location richloc (key_loc);
34290 richloc.add_fixit_remove (key_loc);
34291 warning_at (&richloc, OPT_Wredundant_tags,
34292 "redundant class-key %qs in reference to %q#T",
34293 class_key == union_type ? "union"
34294 : class_key == record_type ? "struct" : "class",
34295 type);
34298 if (!exist)
34299 /* Do nothing if this is the first declaration of the type. */
34300 return;
34302 if (rdl->idxdef != UINT_MAX && rdl->def_class_key == class_key)
34303 /* Do nothing if the class-key in this declaration matches
34304 the definition. */
34305 return;
34307 rdl->add_or_diag_mismatched_tag (type_decl, class_key, key_redundant,
34308 def_p);
34311 /* Either adds this DECL corresponding to the TYPE_DECL to the collection
34312 of class decls or diagnoses it, whichever is appropriate. */
34314 void
34315 class_decl_loc_t::add_or_diag_mismatched_tag (tree type_decl,
34316 tag_types class_key,
34317 bool redundant,
34318 bool def_p)
34320 /* Reset the CLASS_KEY associated with this type on mismatch.
34321 This is an optimization that lets the diagnostic code skip
34322 over classes that use the same class-key in all declarations. */
34323 if (def_class_key != class_key)
34324 def_class_key = none_type;
34326 /* Set IDXDEF to the index of the vector corresponding to
34327 the definition. */
34328 if (def_p)
34329 idxdef = locvec.length ();
34331 /* Append a record of this declaration to the vector. */
34332 class_key_loc_t ckl (current_function_decl, input_location, class_key,
34333 redundant);
34334 locvec.safe_push (ckl);
34336 if (idxdef == UINT_MAX)
34337 return;
34339 /* As a space optimization diagnose declarations of a class
34340 whose definition has been seen and purge the LOCVEC of
34341 all entries except the definition. */
34342 diag_mismatched_tags (type_decl);
34343 if (idxdef)
34345 class_decl_loc_t::class_key_loc_t ent = locvec[idxdef];
34346 locvec.release ();
34347 locvec.reserve (2);
34348 locvec.safe_push (ent);
34349 idxdef = 0;
34351 else
34352 /* Pop the entry pushed above for this declaration. */
34353 locvec.pop ();
34356 /* Issues -Wmismatched-tags for a single class. */
34358 void
34359 class_decl_loc_t::diag_mismatched_tags (tree type_decl)
34361 if (!warn_mismatched_tags)
34362 return;
34364 /* Number of uses of the class. */
34365 const unsigned ndecls = locvec.length ();
34367 /* The class (or template) declaration guiding the decisions about
34368 the diagnostic. For ordinary classes it's the same as THIS. For
34369 uses of instantiations of templates other than their declarations
34370 it points to the record for the declaration of the corresponding
34371 primary template or partial specialization. */
34372 class_decl_loc_t *cdlguide = this;
34374 tree type = TREE_TYPE (type_decl);
34375 if (CLASS_TYPE_P (type) && CLASSTYPE_IMPLICIT_INSTANTIATION (type))
34377 /* For implicit instantiations of a primary template look up
34378 the primary or partial specialization and use it as
34379 the expected class-key rather than using the class-key of
34380 the first reference to the instantiation. The primary must
34381 be (and inevitably is) at index zero. */
34382 tree spec = specialization_of (type);
34383 cdlguide = class2loc.get (spec);
34384 gcc_assert (cdlguide != NULL);
34386 else
34388 /* Skip declarations that consistently use the same class-key. */
34389 if (def_class_key != none_type)
34390 return;
34393 /* Set if a definition for the class has been seen. */
34394 const bool def_p = cdlguide->def_p ();
34396 /* The index of the declaration whose class-key this declaration
34397 is expected to match. It's either the class-key of the class
34398 definition if one exists or the first declaration otherwise. */
34399 const unsigned idxguide = def_p ? cdlguide->idxdef : 0;
34401 /* The class-key the class is expected to be declared with: it's
34402 either the key used in its definition or the first declaration
34403 if no definition has been provided.
34404 For implicit instantiations of a primary template it's
34405 the class-key used to declare the primary with. The primary
34406 must be at index zero. */
34407 const tag_types xpect_key = cdlguide->class_key (idxguide);
34409 unsigned idx = 0;
34410 /* Advance IDX to the first declaration that either is not
34411 a definition or that doesn't match the first declaration
34412 if no definition is provided. */
34413 while (class_key (idx) == xpect_key)
34414 if (++idx == ndecls)
34415 return;
34417 /* Save the current function before changing it below. */
34418 tree save_func = current_function_decl;
34419 /* Set the function declaration to print in diagnostic context. */
34420 current_function_decl = function (idx);
34422 const char *xmatchkstr = xpect_key == record_type ? "class" : "struct";
34423 const char *xpectkstr = xpect_key == record_type ? "struct" : "class";
34425 location_t loc = location (idx);
34426 bool key_redundant_p = key_redundant (idx);
34427 auto_diagnostic_group d;
34428 /* Issue a warning for the first mismatched declaration.
34429 Avoid using "%#qT" since the class-key for the same type will
34430 be the same regardless of which one was used in the declaraion. */
34431 if (warning_at (loc, OPT_Wmismatched_tags,
34432 "%qT declared with a mismatched class-key %qs",
34433 type_decl, xmatchkstr))
34435 /* Suggest how to avoid the warning for each instance since
34436 the guidance may be different depending on context. */
34437 inform (loc,
34438 (key_redundant_p
34439 ? G_("remove the class-key or replace it with %qs")
34440 : G_("replace the class-key with %qs")),
34441 xpectkstr);
34443 /* Also point to the first declaration or definition that guided
34444 the decision to issue the warning above. */
34445 inform (cdlguide->location (idxguide),
34446 (def_p
34447 ? G_("%qT defined as %qs here")
34448 : G_("%qT first declared as %qs here")),
34449 type_decl, xpectkstr);
34452 /* Issue warnings for the remaining inconsistent declarations. */
34453 for (unsigned i = idx + 1; i != ndecls; ++i)
34455 tag_types clskey = class_key (i);
34456 /* Skip over the declarations that match either the definition
34457 if one was provided or the first declaration. */
34458 if (clskey == xpect_key)
34459 continue;
34461 loc = location (i);
34462 key_redundant_p = key_redundant (i);
34463 /* Set the function declaration to print in diagnostic context. */
34464 current_function_decl = function (i);
34465 if (warning_at (loc, OPT_Wmismatched_tags,
34466 "%qT declared with a mismatched class-key %qs",
34467 type_decl, xmatchkstr))
34468 /* Suggest how to avoid the warning for each instance since
34469 the guidance may be different depending on context. */
34470 inform (loc,
34471 (key_redundant_p
34472 ? G_("remove the class-key or replace it with %qs")
34473 : G_("replace the class-key with %qs")),
34474 xpectkstr);
34477 /* Restore the current function in case it was replaced above. */
34478 current_function_decl = save_func;
34481 /* Issues -Wmismatched-tags for all classes. Called at the end
34482 of processing a translation unit, after declarations of all class
34483 types and their uses have been recorded. */
34485 void
34486 class_decl_loc_t::diag_mismatched_tags ()
34488 /* CLASS2LOC should be empty if both -Wmismatched-tags and
34489 -Wredundant-tags are disabled. */
34490 gcc_assert (warn_mismatched_tags
34491 || warn_redundant_tags
34492 || class2loc.is_empty ());
34494 /* Save the current function before changing on return. It should
34495 be null at this point. */
34496 temp_override<tree> cleanup (current_function_decl);
34498 if (warn_mismatched_tags)
34500 /* Iterate over the collected class/struct/template declarations. */
34501 typedef class_to_loc_map_t::iterator iter_t;
34502 for (iter_t it = class2loc.begin (); it != class2loc.end (); ++it)
34504 tree type_decl = (*it).first;
34505 class_decl_loc_t &recloc = (*it).second;
34506 recloc.diag_mismatched_tags (type_decl);
34510 class2loc.empty ();
34513 /* Issue an error message if DECL is redeclared with different
34514 access than its original declaration [class.access.spec/3].
34515 This applies to nested classes, nested class templates and
34516 enumerations [class.mem/1]. */
34518 static void
34519 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
34521 if (!decl
34522 || (!CLASS_TYPE_P (TREE_TYPE (decl))
34523 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
34524 return;
34526 if ((TREE_PRIVATE (decl)
34527 != (current_access_specifier == access_private_node))
34528 || (TREE_PROTECTED (decl)
34529 != (current_access_specifier == access_protected_node)))
34530 error_at (location, "%qD redeclared with different access", decl);
34533 /* Look for the `template' keyword, as a syntactic disambiguator.
34534 Return TRUE iff it is present, in which case it will be
34535 consumed. */
34537 static bool
34538 cp_parser_optional_template_keyword (cp_parser *parser)
34540 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
34542 /* In C++98 the `template' keyword can only be used within templates;
34543 outside templates the parser can always figure out what is a
34544 template and what is not. In C++11, per the resolution of DR 468,
34545 `template' is allowed in cases where it is not strictly necessary. */
34546 if (!processing_template_decl
34547 && pedantic && cxx_dialect == cxx98)
34549 cp_token *token = cp_lexer_peek_token (parser->lexer);
34550 pedwarn (token->location, OPT_Wpedantic,
34551 "in C++98 %<template%> (as a disambiguator) is only "
34552 "allowed within templates");
34553 /* If this part of the token stream is rescanned, the same
34554 error message would be generated. So, we purge the token
34555 from the stream. */
34556 cp_lexer_purge_token (parser->lexer);
34557 return false;
34559 else
34561 /* Consume the `template' keyword. */
34562 cp_lexer_consume_token (parser->lexer);
34563 return true;
34566 return false;
34569 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
34570 set PARSER->SCOPE, and perform other related actions. */
34572 static void
34573 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
34575 struct tree_check *check_value;
34577 /* Get the stored value. */
34578 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
34579 /* Set the scope from the stored value. */
34580 parser->scope = saved_checks_value (check_value);
34581 parser->qualifying_scope = check_value->qualifying_scope;
34582 parser->object_scope = parser->context->object_type;
34583 parser->context->object_type = NULL_TREE;
34586 /* Consume tokens up through a non-nested END token. Returns TRUE if we
34587 encounter the end of a block before what we were looking for. */
34589 static bool
34590 cp_parser_cache_group (cp_parser *parser,
34591 enum cpp_ttype end,
34592 unsigned depth)
34594 while (true)
34596 cp_token *token = cp_lexer_peek_token (parser->lexer);
34598 /* Abort a parenthesized expression if we encounter a semicolon. */
34599 if ((end == CPP_CLOSE_PAREN || depth == 0)
34600 && token->type == CPP_SEMICOLON)
34601 return true;
34602 /* If we've reached the end of the file, stop. */
34603 if (token->type == CPP_EOF
34604 || (end != CPP_PRAGMA_EOL
34605 && token->type == CPP_PRAGMA_EOL))
34606 return true;
34607 if (token->type == CPP_CLOSE_BRACE && depth == 0)
34608 /* We've hit the end of an enclosing block, so there's been some
34609 kind of syntax error. */
34610 return true;
34612 /* Consume the token. */
34613 cp_lexer_consume_token (parser->lexer);
34614 /* See if it starts a new group. */
34615 if (token->type == CPP_OPEN_BRACE)
34617 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
34618 /* In theory this should probably check end == '}', but
34619 cp_parser_save_member_function_body needs it to exit
34620 after either '}' or ')' when called with ')'. */
34621 if (depth == 0)
34622 return false;
34624 else if (token->type == CPP_OPEN_PAREN)
34626 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
34627 if (depth == 0 && end == CPP_CLOSE_PAREN)
34628 return false;
34630 else if (token->type == CPP_PRAGMA)
34631 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
34632 else if (token->type == end)
34633 return false;
34637 /* Like above, for caching a default argument or NSDMI. Both of these are
34638 terminated by a non-nested comma, but it can be unclear whether or not a
34639 comma is nested in a template argument list unless we do more parsing.
34640 In order to handle this ambiguity, when we encounter a ',' after a '<'
34641 we try to parse what follows as a parameter-declaration-list (in the
34642 case of a default argument) or a member-declarator (in the case of an
34643 NSDMI). If that succeeds, then we stop caching. */
34645 static tree
34646 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
34648 unsigned depth = 0;
34649 int maybe_template_id = 0;
34650 cp_token *first_token;
34651 cp_token *token;
34652 tree default_argument;
34654 /* Add tokens until we have processed the entire default
34655 argument. We add the range [first_token, token). */
34656 first_token = cp_lexer_peek_token (parser->lexer);
34657 if (first_token->type == CPP_OPEN_BRACE)
34659 /* For list-initialization, this is straightforward. */
34660 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
34661 token = cp_lexer_peek_token (parser->lexer);
34663 else while (true)
34665 bool done = false;
34667 /* Peek at the next token. */
34668 token = cp_lexer_peek_token (parser->lexer);
34669 /* What we do depends on what token we have. */
34670 switch (token->type)
34672 /* In valid code, a default argument must be
34673 immediately followed by a `,' `)', or `...'. */
34674 case CPP_COMMA:
34675 if (depth == 0 && maybe_template_id)
34677 /* If we've seen a '<', we might be in a
34678 template-argument-list. Until Core issue 325 is
34679 resolved, we don't know how this situation ought
34680 to be handled, so try to DTRT. We check whether
34681 what comes after the comma is a valid parameter
34682 declaration list. If it is, then the comma ends
34683 the default argument; otherwise the default
34684 argument continues. */
34685 bool error = false;
34686 cp_token *peek;
34688 /* Set ITALP so cp_parser_parameter_declaration_list
34689 doesn't decide to commit to this parse. */
34690 bool saved_italp = parser->in_template_argument_list_p;
34691 parser->in_template_argument_list_p = true;
34693 cp_parser_parse_tentatively (parser);
34695 if (nsdmi)
34697 /* Parse declarators until we reach a non-comma or
34698 somthing that cannot be an initializer.
34699 Just checking whether we're looking at a single
34700 declarator is insufficient. Consider:
34701 int var = tuple<T,U>::x;
34702 The template parameter 'U' looks exactly like a
34703 declarator. */
34706 int ctor_dtor_or_conv_p;
34707 cp_lexer_consume_token (parser->lexer);
34708 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
34709 CP_PARSER_FLAGS_NONE,
34710 &ctor_dtor_or_conv_p,
34711 /*parenthesized_p=*/NULL,
34712 /*member_p=*/true,
34713 /*friend_p=*/false,
34714 /*static_p=*/false);
34715 peek = cp_lexer_peek_token (parser->lexer);
34716 if (cp_parser_error_occurred (parser))
34717 break;
34719 while (peek->type == CPP_COMMA);
34720 /* If we met an '=' or ';' then the original comma
34721 was the end of the NSDMI. Otherwise assume
34722 we're still in the NSDMI. */
34723 error = (peek->type != CPP_EQ
34724 && peek->type != CPP_SEMICOLON);
34726 else
34728 cp_lexer_consume_token (parser->lexer);
34729 begin_scope (sk_function_parms, NULL_TREE);
34730 tree t = cp_parser_parameter_declaration_list
34731 (parser, CP_PARSER_FLAGS_NONE,
34732 /*pending_decls*/nullptr);
34733 if (t == error_mark_node)
34734 error = true;
34735 pop_bindings_and_leave_scope ();
34737 if (!cp_parser_error_occurred (parser) && !error)
34738 done = true;
34739 cp_parser_abort_tentative_parse (parser);
34741 parser->in_template_argument_list_p = saved_italp;
34742 break;
34744 /* FALLTHRU */
34745 case CPP_CLOSE_PAREN:
34746 case CPP_ELLIPSIS:
34747 /* If we run into a non-nested `;', `}', or `]',
34748 then the code is invalid -- but the default
34749 argument is certainly over. */
34750 case CPP_SEMICOLON:
34751 case CPP_CLOSE_BRACE:
34752 case CPP_CLOSE_SQUARE:
34753 if (depth == 0
34754 /* Handle correctly int n = sizeof ... ( p ); */
34755 && token->type != CPP_ELLIPSIS)
34756 done = true;
34757 /* Update DEPTH, if necessary. */
34758 else if (token->type == CPP_CLOSE_PAREN
34759 || token->type == CPP_CLOSE_BRACE
34760 || token->type == CPP_CLOSE_SQUARE)
34761 --depth;
34762 break;
34764 case CPP_OPEN_PAREN:
34765 case CPP_OPEN_SQUARE:
34766 case CPP_OPEN_BRACE:
34767 ++depth;
34768 break;
34770 case CPP_LESS:
34771 if (depth == 0)
34772 /* This might be the comparison operator, or it might
34773 start a template argument list. */
34774 ++maybe_template_id;
34775 break;
34777 case CPP_RSHIFT:
34778 if (cxx_dialect == cxx98)
34779 break;
34780 /* Fall through for C++0x, which treats the `>>'
34781 operator like two `>' tokens in certain
34782 cases. */
34783 gcc_fallthrough ();
34785 case CPP_GREATER:
34786 if (depth == 0)
34788 /* This might be an operator, or it might close a
34789 template argument list. But if a previous '<'
34790 started a template argument list, this will have
34791 closed it, so we can't be in one anymore. */
34792 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
34793 if (maybe_template_id < 0)
34794 maybe_template_id = 0;
34796 break;
34798 /* If we run out of tokens, issue an error message. */
34799 case CPP_EOF:
34800 case CPP_PRAGMA_EOL:
34801 error_at (token->location, "file ends in default argument");
34802 return error_mark_node;
34804 case CPP_NAME:
34805 case CPP_SCOPE:
34806 /* In these cases, we should look for template-ids.
34807 For example, if the default argument is
34808 `X<int, double>()', we need to do name lookup to
34809 figure out whether or not `X' is a template; if
34810 so, the `,' does not end the default argument.
34812 That is not yet done. */
34813 break;
34815 default:
34816 break;
34819 /* If we've reached the end, stop. */
34820 if (done)
34821 break;
34823 /* Add the token to the token block. */
34824 token = cp_lexer_consume_token (parser->lexer);
34827 /* Create a DEFERRED_PARSE to represent the unparsed default
34828 argument. */
34829 default_argument = make_node (DEFERRED_PARSE);
34830 DEFPARSE_TOKENS (default_argument)
34831 = cp_token_cache_new (first_token, token);
34832 DEFPARSE_INSTANTIATIONS (default_argument) = NULL;
34834 return default_argument;
34837 /* A location to use for diagnostics about an unparsed DEFERRED_PARSE. */
34839 location_t
34840 defparse_location (tree default_argument)
34842 cp_token_cache *tokens = DEFPARSE_TOKENS (default_argument);
34843 location_t start = tokens->first->location;
34844 location_t end = tokens->last->location;
34845 return make_location (start, start, end);
34848 /* Begin parsing tentatively. We always save tokens while parsing
34849 tentatively so that if the tentative parsing fails we can restore the
34850 tokens. */
34852 static void
34853 cp_parser_parse_tentatively (cp_parser* parser)
34855 /* Enter a new parsing context. */
34856 parser->context = cp_parser_context_new (parser->context);
34857 /* Begin saving tokens. */
34858 cp_lexer_save_tokens (parser->lexer);
34859 /* In order to avoid repetitive access control error messages,
34860 access checks are queued up until we are no longer parsing
34861 tentatively. */
34862 push_deferring_access_checks (dk_deferred);
34865 /* Commit to the currently active tentative parse. */
34867 static void
34868 cp_parser_commit_to_tentative_parse (cp_parser* parser)
34870 cp_parser_context *context;
34871 cp_lexer *lexer;
34873 /* Mark all of the levels as committed. */
34874 lexer = parser->lexer;
34875 for (context = parser->context; context->next; context = context->next)
34877 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
34878 break;
34879 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
34880 while (!cp_lexer_saving_tokens (lexer))
34881 lexer = lexer->next;
34882 cp_lexer_commit_tokens (lexer);
34886 /* Commit to the topmost currently active tentative parse.
34888 Note that this function shouldn't be called when there are
34889 irreversible side-effects while in a tentative state. For
34890 example, we shouldn't create a permanent entry in the symbol
34891 table, or issue an error message that might not apply if the
34892 tentative parse is aborted. */
34894 static void
34895 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
34897 cp_parser_context *context = parser->context;
34898 cp_lexer *lexer = parser->lexer;
34900 if (context)
34902 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
34903 return;
34904 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
34906 while (!cp_lexer_saving_tokens (lexer))
34907 lexer = lexer->next;
34908 cp_lexer_commit_tokens (lexer);
34912 /* Abort the currently active tentative parse. All consumed tokens
34913 will be rolled back, and no diagnostics will be issued. */
34915 static void
34916 cp_parser_abort_tentative_parse (cp_parser* parser)
34918 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
34919 || errorcount > 0);
34920 cp_parser_simulate_error (parser);
34921 /* Now, pretend that we want to see if the construct was
34922 successfully parsed. */
34923 cp_parser_parse_definitely (parser);
34926 /* Stop parsing tentatively. If a parse error has occurred, restore the
34927 token stream. Otherwise, commit to the tokens we have consumed.
34928 Returns true if no error occurred; false otherwise. */
34930 static bool
34931 cp_parser_parse_definitely (cp_parser* parser)
34933 bool error_occurred;
34934 cp_parser_context *context;
34936 /* Remember whether or not an error occurred, since we are about to
34937 destroy that information. */
34938 error_occurred = cp_parser_error_occurred (parser);
34939 /* Remove the topmost context from the stack. */
34940 context = parser->context;
34941 parser->context = context->next;
34942 /* If no parse errors occurred, commit to the tentative parse. */
34943 if (!error_occurred)
34945 /* Commit to the tokens read tentatively, unless that was
34946 already done. */
34947 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
34948 cp_lexer_commit_tokens (parser->lexer);
34950 pop_to_parent_deferring_access_checks ();
34952 /* Otherwise, if errors occurred, roll back our state so that things
34953 are just as they were before we began the tentative parse. */
34954 else
34956 cp_lexer_rollback_tokens (parser->lexer);
34957 pop_deferring_access_checks ();
34959 /* Add the context to the front of the free list. */
34960 context->next = cp_parser_context_free_list;
34961 cp_parser_context_free_list = context;
34963 return !error_occurred;
34966 /* Returns true if we are parsing tentatively and are not committed to
34967 this tentative parse. */
34969 static bool
34970 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
34972 return (cp_parser_parsing_tentatively (parser)
34973 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
34976 /* Returns nonzero iff an error has occurred during the most recent
34977 tentative parse. */
34979 static bool
34980 cp_parser_error_occurred (cp_parser* parser)
34982 return (cp_parser_parsing_tentatively (parser)
34983 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
34986 /* Returns nonzero if GNU extensions are allowed. */
34988 static bool
34989 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
34991 return parser->allow_gnu_extensions_p;
34994 /* Objective-C++ Productions */
34997 /* Parse an Objective-C expression, which feeds into a primary-expression
34998 above.
35000 objc-expression:
35001 objc-message-expression
35002 objc-string-literal
35003 objc-encode-expression
35004 objc-protocol-expression
35005 objc-selector-expression
35007 Returns a tree representation of the expression. */
35009 static cp_expr
35010 cp_parser_objc_expression (cp_parser* parser)
35012 /* Try to figure out what kind of declaration is present. */
35013 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
35015 switch (kwd->type)
35017 case CPP_OPEN_SQUARE:
35018 return cp_parser_objc_message_expression (parser);
35020 case CPP_OBJC_STRING:
35021 kwd = cp_lexer_consume_token (parser->lexer);
35022 return objc_build_string_object (kwd->u.value);
35024 case CPP_KEYWORD:
35025 switch (kwd->keyword)
35027 case RID_AT_ENCODE:
35028 return cp_parser_objc_encode_expression (parser);
35030 case RID_AT_PROTOCOL:
35031 return cp_parser_objc_protocol_expression (parser);
35033 case RID_AT_SELECTOR:
35034 return cp_parser_objc_selector_expression (parser);
35036 default:
35037 break;
35039 /* FALLTHRU */
35040 default:
35041 error_at (kwd->location,
35042 "misplaced %<@%D%> Objective-C++ construct",
35043 kwd->u.value);
35044 cp_parser_skip_to_end_of_block_or_statement (parser);
35047 return error_mark_node;
35050 /* Parse an Objective-C message expression.
35052 objc-message-expression:
35053 [ objc-message-receiver objc-message-args ]
35055 Returns a representation of an Objective-C message. */
35057 static tree
35058 cp_parser_objc_message_expression (cp_parser* parser)
35060 tree receiver, messageargs;
35062 parser->objective_c_message_context_p = true;
35063 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
35064 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
35065 receiver = cp_parser_objc_message_receiver (parser);
35066 messageargs = cp_parser_objc_message_args (parser);
35067 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
35068 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
35070 tree result = objc_build_message_expr (receiver, messageargs);
35072 /* Construct a location e.g.
35073 [self func1:5]
35074 ^~~~~~~~~~~~~~
35075 ranging from the '[' to the ']', with the caret at the start. */
35076 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
35077 protected_set_expr_location (result, combined_loc);
35079 parser->objective_c_message_context_p = false;
35080 return result;
35083 /* Parse an objc-message-receiver.
35085 objc-message-receiver:
35086 expression
35087 simple-type-specifier
35089 Returns a representation of the type or expression. */
35091 static tree
35092 cp_parser_objc_message_receiver (cp_parser* parser)
35094 tree rcv;
35096 /* An Objective-C message receiver may be either (1) a type
35097 or (2) an expression. */
35098 cp_parser_parse_tentatively (parser);
35099 rcv = cp_parser_expression (parser);
35101 /* If that worked out, fine. */
35102 if (cp_parser_parse_definitely (parser))
35103 return rcv;
35105 cp_parser_parse_tentatively (parser);
35106 rcv = cp_parser_simple_type_specifier (parser,
35107 /*decl_specs=*/NULL,
35108 CP_PARSER_FLAGS_NONE);
35110 if (cp_parser_parse_definitely (parser))
35111 return objc_get_class_reference (rcv);
35113 cp_parser_error (parser, "objective-c++ message receiver expected");
35114 return error_mark_node;
35117 /* Parse the arguments and selectors comprising an Objective-C message.
35119 objc-message-args:
35120 objc-selector
35121 objc-selector-args
35122 objc-selector-args , objc-comma-args
35124 objc-selector-args:
35125 objc-selector [opt] : assignment-expression
35126 objc-selector-args objc-selector [opt] : assignment-expression
35128 objc-comma-args:
35129 assignment-expression
35130 objc-comma-args , assignment-expression
35132 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
35133 selector arguments and TREE_VALUE containing a list of comma
35134 arguments. */
35136 static tree
35137 cp_parser_objc_message_args (cp_parser* parser)
35139 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
35140 bool maybe_unary_selector_p = true;
35141 cp_token *token = cp_lexer_peek_token (parser->lexer);
35143 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
35145 tree selector = NULL_TREE, arg;
35147 if (token->type != CPP_COLON)
35148 selector = cp_parser_objc_selector (parser);
35150 /* Detect if we have a unary selector. */
35151 if (maybe_unary_selector_p
35152 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
35153 return build_tree_list (selector, NULL_TREE);
35155 maybe_unary_selector_p = false;
35156 cp_parser_require (parser, CPP_COLON, RT_COLON);
35157 arg = cp_parser_assignment_expression (parser);
35159 sel_args
35160 = chainon (sel_args,
35161 build_tree_list (selector, arg));
35163 token = cp_lexer_peek_token (parser->lexer);
35166 /* Handle non-selector arguments, if any. */
35167 while (token->type == CPP_COMMA)
35169 tree arg;
35171 cp_lexer_consume_token (parser->lexer);
35172 arg = cp_parser_assignment_expression (parser);
35174 addl_args
35175 = chainon (addl_args,
35176 build_tree_list (NULL_TREE, arg));
35178 token = cp_lexer_peek_token (parser->lexer);
35181 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
35183 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
35184 return build_tree_list (error_mark_node, error_mark_node);
35187 return build_tree_list (sel_args, addl_args);
35190 /* Parse an Objective-C encode expression.
35192 objc-encode-expression:
35193 @encode objc-typename
35195 Returns an encoded representation of the type argument. */
35197 static cp_expr
35198 cp_parser_objc_encode_expression (cp_parser* parser)
35200 tree type;
35201 cp_token *token;
35202 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
35204 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
35205 matching_parens parens;
35206 parens.require_open (parser);
35207 token = cp_lexer_peek_token (parser->lexer);
35208 type = complete_type (cp_parser_type_id (parser));
35209 parens.require_close (parser);
35211 if (!type)
35213 error_at (token->location,
35214 "%<@encode%> must specify a type as an argument");
35215 return error_mark_node;
35218 /* This happens if we find @encode(T) (where T is a template
35219 typename or something dependent on a template typename) when
35220 parsing a template. In that case, we can't compile it
35221 immediately, but we rather create an AT_ENCODE_EXPR which will
35222 need to be instantiated when the template is used.
35224 if (dependent_type_p (type))
35226 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
35227 TREE_READONLY (value) = 1;
35228 return value;
35232 /* Build a location of the form:
35233 @encode(int)
35234 ^~~~~~~~~~~~
35235 with caret==start at the @ token, finishing at the close paren. */
35236 location_t combined_loc = make_location (start_loc, start_loc, parser->lexer);
35238 return cp_expr (objc_build_encode_expr (type), combined_loc);
35241 /* Parse an Objective-C @defs expression. */
35243 static tree
35244 cp_parser_objc_defs_expression (cp_parser *parser)
35246 tree name;
35248 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
35249 matching_parens parens;
35250 parens.require_open (parser);
35251 name = cp_parser_identifier (parser);
35252 parens.require_close (parser);
35254 return objc_get_class_ivars (name);
35257 /* Parse an Objective-C protocol expression.
35259 objc-protocol-expression:
35260 @protocol ( identifier )
35262 Returns a representation of the protocol expression. */
35264 static tree
35265 cp_parser_objc_protocol_expression (cp_parser* parser)
35267 tree proto;
35268 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
35270 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
35271 matching_parens parens;
35272 parens.require_open (parser);
35273 proto = cp_parser_identifier (parser);
35274 parens.require_close (parser);
35276 /* Build a location of the form:
35277 @protocol(prot)
35278 ^~~~~~~~~~~~~~~
35279 with caret==start at the @ token, finishing at the close paren. */
35280 location_t combined_loc = make_location (start_loc, start_loc, parser->lexer);
35281 tree result = objc_build_protocol_expr (proto);
35282 protected_set_expr_location (result, combined_loc);
35283 return result;
35286 /* Parse an Objective-C selector expression.
35288 objc-selector-expression:
35289 @selector ( objc-method-signature )
35291 objc-method-signature:
35292 objc-selector
35293 objc-selector-seq
35295 objc-selector-seq:
35296 objc-selector :
35297 objc-selector-seq objc-selector :
35299 Returns a representation of the method selector. */
35301 static tree
35302 cp_parser_objc_selector_expression (cp_parser* parser)
35304 tree sel_seq = NULL_TREE;
35305 bool maybe_unary_selector_p = true;
35306 cp_token *token;
35307 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35309 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
35310 matching_parens parens;
35311 parens.require_open (parser);
35312 token = cp_lexer_peek_token (parser->lexer);
35314 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
35315 || token->type == CPP_SCOPE)
35317 tree selector = NULL_TREE;
35319 if (token->type != CPP_COLON
35320 || token->type == CPP_SCOPE)
35321 selector = cp_parser_objc_selector (parser);
35323 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
35324 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
35326 /* Detect if we have a unary selector. */
35327 if (maybe_unary_selector_p)
35329 sel_seq = selector;
35330 goto finish_selector;
35332 else
35334 cp_parser_error (parser, "expected %<:%>");
35337 maybe_unary_selector_p = false;
35338 token = cp_lexer_consume_token (parser->lexer);
35340 if (token->type == CPP_SCOPE)
35342 sel_seq
35343 = chainon (sel_seq,
35344 build_tree_list (selector, NULL_TREE));
35345 sel_seq
35346 = chainon (sel_seq,
35347 build_tree_list (NULL_TREE, NULL_TREE));
35349 else
35350 sel_seq
35351 = chainon (sel_seq,
35352 build_tree_list (selector, NULL_TREE));
35354 token = cp_lexer_peek_token (parser->lexer);
35357 finish_selector:
35358 parens.require_close (parser);
35361 /* Build a location of the form:
35362 @selector(func)
35363 ^~~~~~~~~~~~~~~
35364 with caret==start at the @ token, finishing at the close paren. */
35365 location_t combined_loc = make_location (loc, loc, parser->lexer);
35366 tree result = objc_build_selector_expr (combined_loc, sel_seq);
35367 /* TODO: objc_build_selector_expr doesn't always honor the location. */
35368 protected_set_expr_location (result, combined_loc);
35369 return result;
35372 /* Parse a list of identifiers.
35374 objc-identifier-list:
35375 identifier
35376 objc-identifier-list , identifier
35378 Returns a TREE_LIST of identifier nodes. */
35380 static tree
35381 cp_parser_objc_identifier_list (cp_parser* parser)
35383 tree identifier;
35384 tree list;
35385 cp_token *sep;
35387 identifier = cp_parser_identifier (parser);
35388 if (identifier == error_mark_node)
35389 return error_mark_node;
35391 list = build_tree_list (NULL_TREE, identifier);
35392 sep = cp_lexer_peek_token (parser->lexer);
35394 while (sep->type == CPP_COMMA)
35396 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
35397 identifier = cp_parser_identifier (parser);
35398 if (identifier == error_mark_node)
35399 return list;
35401 list = chainon (list, build_tree_list (NULL_TREE,
35402 identifier));
35403 sep = cp_lexer_peek_token (parser->lexer);
35406 return list;
35409 /* Parse an Objective-C alias declaration.
35411 objc-alias-declaration:
35412 @compatibility_alias identifier identifier ;
35414 This function registers the alias mapping with the Objective-C front end.
35415 It returns nothing. */
35417 static void
35418 cp_parser_objc_alias_declaration (cp_parser* parser)
35420 tree alias, orig;
35422 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
35423 alias = cp_parser_identifier (parser);
35424 orig = cp_parser_identifier (parser);
35425 objc_declare_alias (alias, orig);
35426 cp_parser_consume_semicolon_at_end_of_statement (parser);
35429 /* Parse an Objective-C class forward-declaration.
35431 objc-class-declaration:
35432 @class objc-identifier-list ;
35434 The function registers the forward declarations with the Objective-C
35435 front end. It returns nothing. */
35437 static void
35438 cp_parser_objc_class_declaration (cp_parser* parser)
35440 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
35441 while (true)
35443 tree id;
35445 id = cp_parser_identifier (parser);
35446 if (id == error_mark_node)
35447 break;
35449 objc_declare_class (id);
35451 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
35452 cp_lexer_consume_token (parser->lexer);
35453 else
35454 break;
35456 cp_parser_consume_semicolon_at_end_of_statement (parser);
35459 /* Parse a list of Objective-C protocol references.
35461 objc-protocol-refs-opt:
35462 objc-protocol-refs [opt]
35464 objc-protocol-refs:
35465 < objc-identifier-list >
35467 Returns a TREE_LIST of identifiers, if any. */
35469 static tree
35470 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
35472 tree protorefs = NULL_TREE;
35474 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
35476 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
35477 protorefs = cp_parser_objc_identifier_list (parser);
35478 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
35481 return protorefs;
35484 /* Parse a Objective-C visibility specification. */
35486 static void
35487 cp_parser_objc_visibility_spec (cp_parser* parser)
35489 cp_token *vis = cp_lexer_peek_token (parser->lexer);
35491 switch (vis->keyword)
35493 case RID_AT_PRIVATE:
35494 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
35495 break;
35496 case RID_AT_PROTECTED:
35497 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
35498 break;
35499 case RID_AT_PUBLIC:
35500 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
35501 break;
35502 case RID_AT_PACKAGE:
35503 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
35504 break;
35505 default:
35506 return;
35509 /* Eat '@private'/'@protected'/'@public'. */
35510 cp_lexer_consume_token (parser->lexer);
35513 /* Parse an Objective-C method type. Return 'true' if it is a class
35514 (+) method, and 'false' if it is an instance (-) method. */
35516 static inline bool
35517 cp_parser_objc_method_type (cp_parser* parser)
35519 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
35520 return true;
35521 else
35522 return false;
35525 /* Parse an Objective-C protocol qualifier. */
35527 static tree
35528 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
35530 tree quals = NULL_TREE, node;
35531 cp_token *token = cp_lexer_peek_token (parser->lexer);
35533 node = token->u.value;
35535 while (node && identifier_p (node)
35536 && (node == ridpointers [(int) RID_IN]
35537 || node == ridpointers [(int) RID_OUT]
35538 || node == ridpointers [(int) RID_INOUT]
35539 || node == ridpointers [(int) RID_BYCOPY]
35540 || node == ridpointers [(int) RID_BYREF]
35541 || node == ridpointers [(int) RID_ONEWAY]))
35543 quals = tree_cons (NULL_TREE, node, quals);
35544 cp_lexer_consume_token (parser->lexer);
35545 token = cp_lexer_peek_token (parser->lexer);
35546 node = token->u.value;
35549 return quals;
35552 /* Parse an Objective-C typename. */
35554 static tree
35555 cp_parser_objc_typename (cp_parser* parser)
35557 tree type_name = NULL_TREE;
35559 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
35561 tree proto_quals, cp_type = NULL_TREE;
35563 matching_parens parens;
35564 parens.consume_open (parser); /* Eat '('. */
35565 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
35567 /* An ObjC type name may consist of just protocol qualifiers, in which
35568 case the type shall default to 'id'. */
35569 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
35571 cp_type = cp_parser_type_id (parser);
35573 /* If the type could not be parsed, an error has already
35574 been produced. For error recovery, behave as if it had
35575 not been specified, which will use the default type
35576 'id'. */
35577 if (cp_type == error_mark_node)
35579 cp_type = NULL_TREE;
35580 /* We need to skip to the closing parenthesis as
35581 cp_parser_type_id() does not seem to do it for
35582 us. */
35583 cp_parser_skip_to_closing_parenthesis (parser,
35584 /*recovering=*/true,
35585 /*or_comma=*/false,
35586 /*consume_paren=*/false);
35590 parens.require_close (parser);
35591 type_name = build_tree_list (proto_quals, cp_type);
35594 return type_name;
35597 /* Check to see if TYPE refers to an Objective-C selector name. */
35599 static bool
35600 cp_parser_objc_selector_p (enum cpp_ttype type)
35602 return (type == CPP_NAME || type == CPP_KEYWORD
35603 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
35604 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
35605 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
35606 || type == CPP_XOR || type == CPP_XOR_EQ);
35609 /* Parse an Objective-C selector. */
35611 static tree
35612 cp_parser_objc_selector (cp_parser* parser)
35614 cp_token *token = cp_lexer_consume_token (parser->lexer);
35616 if (!cp_parser_objc_selector_p (token->type))
35618 error_at (token->location, "invalid Objective-C++ selector name");
35619 return error_mark_node;
35622 /* C++ operator names are allowed to appear in ObjC selectors. */
35623 switch (token->type)
35625 case CPP_AND_AND: return get_identifier ("and");
35626 case CPP_AND_EQ: return get_identifier ("and_eq");
35627 case CPP_AND: return get_identifier ("bitand");
35628 case CPP_OR: return get_identifier ("bitor");
35629 case CPP_COMPL: return get_identifier ("compl");
35630 case CPP_NOT: return get_identifier ("not");
35631 case CPP_NOT_EQ: return get_identifier ("not_eq");
35632 case CPP_OR_OR: return get_identifier ("or");
35633 case CPP_OR_EQ: return get_identifier ("or_eq");
35634 case CPP_XOR: return get_identifier ("xor");
35635 case CPP_XOR_EQ: return get_identifier ("xor_eq");
35636 default: return token->u.value;
35640 /* Parse an Objective-C params list. */
35642 static tree
35643 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
35645 tree params = NULL_TREE;
35646 bool maybe_unary_selector_p = true;
35647 cp_token *token = cp_lexer_peek_token (parser->lexer);
35649 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
35651 tree selector = NULL_TREE, type_name, identifier;
35652 tree parm_attr = NULL_TREE;
35654 if (token->keyword == RID_ATTRIBUTE)
35655 break;
35657 if (token->type != CPP_COLON)
35658 selector = cp_parser_objc_selector (parser);
35660 /* Detect if we have a unary selector. */
35661 if (maybe_unary_selector_p
35662 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
35664 params = selector; /* Might be followed by attributes. */
35665 break;
35668 maybe_unary_selector_p = false;
35669 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
35671 /* Something went quite wrong. There should be a colon
35672 here, but there is not. Stop parsing parameters. */
35673 break;
35675 type_name = cp_parser_objc_typename (parser);
35676 /* New ObjC allows attributes on parameters too. */
35677 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
35678 parm_attr = cp_parser_attributes_opt (parser);
35679 identifier = cp_parser_identifier (parser);
35681 params
35682 = chainon (params,
35683 objc_build_keyword_decl (selector,
35684 type_name,
35685 identifier,
35686 parm_attr));
35688 token = cp_lexer_peek_token (parser->lexer);
35691 if (params == NULL_TREE)
35693 cp_parser_error (parser, "objective-c++ method declaration is expected");
35694 return error_mark_node;
35697 /* We allow tail attributes for the method. */
35698 if (token->keyword == RID_ATTRIBUTE)
35700 *attributes = cp_parser_attributes_opt (parser);
35701 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
35702 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35703 return params;
35704 cp_parser_error (parser,
35705 "method attributes must be specified at the end");
35706 return error_mark_node;
35709 if (params == NULL_TREE)
35711 cp_parser_error (parser, "objective-c++ method declaration is expected");
35712 return error_mark_node;
35714 return params;
35717 /* Parse the non-keyword Objective-C params. */
35719 static tree
35720 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
35721 tree* attributes)
35723 tree params = make_node (TREE_LIST);
35724 cp_token *token = cp_lexer_peek_token (parser->lexer);
35725 *ellipsisp = false; /* Initially, assume no ellipsis. */
35727 while (token->type == CPP_COMMA)
35729 cp_parameter_declarator *parmdecl;
35730 tree parm;
35732 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
35733 token = cp_lexer_peek_token (parser->lexer);
35735 if (token->type == CPP_ELLIPSIS)
35737 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
35738 *ellipsisp = true;
35739 token = cp_lexer_peek_token (parser->lexer);
35740 break;
35743 /* TODO: parse attributes for tail parameters. */
35744 parmdecl = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
35745 false, NULL);
35746 parm = grokdeclarator (parmdecl->declarator,
35747 &parmdecl->decl_specifiers,
35748 PARM, /*initialized=*/0,
35749 /*attrlist=*/NULL);
35751 chainon (params, build_tree_list (NULL_TREE, parm));
35752 token = cp_lexer_peek_token (parser->lexer);
35755 /* We allow tail attributes for the method. */
35756 if (token->keyword == RID_ATTRIBUTE)
35758 if (*attributes == NULL_TREE)
35760 *attributes = cp_parser_attributes_opt (parser);
35761 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
35762 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35763 return params;
35765 else
35766 /* We have an error, but parse the attributes, so that we can
35767 carry on. */
35768 *attributes = cp_parser_attributes_opt (parser);
35770 cp_parser_error (parser,
35771 "method attributes must be specified at the end");
35772 return error_mark_node;
35775 return params;
35778 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
35780 static void
35781 cp_parser_objc_interstitial_code (cp_parser* parser)
35783 cp_token *token = cp_lexer_peek_token (parser->lexer);
35785 /* If the next token is `extern' and the following token is a string
35786 literal, then we have a linkage specification. */
35787 if (token->keyword == RID_EXTERN
35788 && cp_parser_is_pure_string_literal
35789 (cp_lexer_peek_nth_token (parser->lexer, 2)))
35790 cp_parser_linkage_specification (parser, NULL_TREE);
35791 /* Handle #pragma, if any. */
35792 else if (token->type == CPP_PRAGMA)
35793 cp_parser_pragma (parser, pragma_objc_icode, NULL);
35794 /* Allow stray semicolons. */
35795 else if (token->type == CPP_SEMICOLON)
35796 cp_lexer_consume_token (parser->lexer);
35797 /* Mark methods as optional or required, when building protocols. */
35798 else if (token->keyword == RID_AT_OPTIONAL)
35800 cp_lexer_consume_token (parser->lexer);
35801 objc_set_method_opt (true);
35803 else if (token->keyword == RID_AT_REQUIRED)
35805 cp_lexer_consume_token (parser->lexer);
35806 objc_set_method_opt (false);
35808 else if (token->keyword == RID_NAMESPACE)
35809 cp_parser_namespace_definition (parser);
35810 /* Other stray characters must generate errors. */
35811 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
35813 cp_lexer_consume_token (parser->lexer);
35814 error ("stray %qs between Objective-C++ methods",
35815 token->type == CPP_OPEN_BRACE ? "{" : "}");
35817 /* Finally, try to parse a block-declaration, or a function-definition. */
35818 else
35819 cp_parser_block_declaration (parser, /*statement_p=*/false);
35822 /* Parse a method signature. */
35824 static tree
35825 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
35827 tree rettype, kwdparms, optparms;
35828 bool ellipsis = false;
35829 bool is_class_method;
35831 is_class_method = cp_parser_objc_method_type (parser);
35832 rettype = cp_parser_objc_typename (parser);
35833 *attributes = NULL_TREE;
35834 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
35835 if (kwdparms == error_mark_node)
35836 return error_mark_node;
35837 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
35838 if (optparms == error_mark_node)
35839 return error_mark_node;
35841 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
35844 static bool
35845 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
35847 tree tattr;
35848 cp_lexer_save_tokens (parser->lexer);
35849 tattr = cp_parser_attributes_opt (parser);
35850 gcc_assert (tattr) ;
35852 /* If the attributes are followed by a method introducer, this is not allowed.
35853 Dump the attributes and flag the situation. */
35854 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
35855 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
35856 return true;
35858 /* Otherwise, the attributes introduce some interstitial code, possibly so
35859 rewind to allow that check. */
35860 cp_lexer_rollback_tokens (parser->lexer);
35861 return false;
35864 /* Parse an Objective-C method prototype list. */
35866 static void
35867 cp_parser_objc_method_prototype_list (cp_parser* parser)
35869 cp_token *token = cp_lexer_peek_token (parser->lexer);
35871 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
35873 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
35875 tree attributes, sig;
35876 bool is_class_method;
35877 if (token->type == CPP_PLUS)
35878 is_class_method = true;
35879 else
35880 is_class_method = false;
35881 sig = cp_parser_objc_method_signature (parser, &attributes);
35882 if (sig == error_mark_node)
35884 cp_parser_skip_to_end_of_block_or_statement (parser);
35885 token = cp_lexer_peek_token (parser->lexer);
35886 continue;
35888 objc_add_method_declaration (is_class_method, sig, attributes);
35889 cp_parser_consume_semicolon_at_end_of_statement (parser);
35891 else if (token->keyword == RID_AT_PROPERTY)
35892 cp_parser_objc_at_property_declaration (parser);
35893 else if (token->keyword == RID_ATTRIBUTE
35894 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
35895 warning_at (cp_lexer_peek_token (parser->lexer)->location,
35896 OPT_Wattributes,
35897 "prefix attributes are ignored for methods");
35898 else
35899 /* Allow for interspersed non-ObjC++ code. */
35900 cp_parser_objc_interstitial_code (parser);
35902 token = cp_lexer_peek_token (parser->lexer);
35905 if (token->type != CPP_EOF)
35906 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
35907 else
35908 cp_parser_error (parser, "expected %<@end%>");
35910 objc_finish_interface ();
35913 /* Parse an Objective-C method definition list. */
35915 static void
35916 cp_parser_objc_method_definition_list (cp_parser* parser)
35918 for (;;)
35920 cp_token *token = cp_lexer_peek_token (parser->lexer);
35922 if (token->keyword == RID_AT_END)
35924 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
35925 break;
35927 else if (token->type == CPP_EOF)
35929 cp_parser_error (parser, "expected %<@end%>");
35930 break;
35932 else if (token->type == CPP_PLUS || token->type == CPP_MINUS)
35934 bool is_class_method = token->type == CPP_PLUS;
35936 push_deferring_access_checks (dk_deferred);
35937 tree attribute;
35938 tree sig = cp_parser_objc_method_signature (parser, &attribute);
35939 if (sig == error_mark_node)
35940 cp_parser_skip_to_end_of_block_or_statement (parser);
35941 else
35943 objc_start_method_definition (is_class_method, sig,
35944 attribute, NULL_TREE);
35946 /* For historical reasons, we accept an optional semicolon. */
35947 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35948 cp_lexer_consume_token (parser->lexer);
35950 perform_deferred_access_checks (tf_warning_or_error);
35951 stop_deferring_access_checks ();
35952 tree meth
35953 = cp_parser_function_definition_after_declarator (parser, false);
35954 pop_deferring_access_checks ();
35955 objc_finish_method_definition (meth);
35958 /* The following case will be removed once @synthesize is
35959 completely implemented. */
35960 else if (token->keyword == RID_AT_PROPERTY)
35961 cp_parser_objc_at_property_declaration (parser);
35962 else if (token->keyword == RID_AT_SYNTHESIZE)
35963 cp_parser_objc_at_synthesize_declaration (parser);
35964 else if (token->keyword == RID_AT_DYNAMIC)
35965 cp_parser_objc_at_dynamic_declaration (parser);
35966 else if (token->keyword == RID_ATTRIBUTE
35967 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
35968 warning_at (token->location, OPT_Wattributes,
35969 "prefix attributes are ignored for methods");
35970 else
35971 /* Allow for interspersed non-ObjC++ code. */
35972 cp_parser_objc_interstitial_code (parser);
35975 objc_finish_implementation ();
35978 /* Parse Objective-C ivars. */
35980 static void
35981 cp_parser_objc_class_ivars (cp_parser* parser)
35983 cp_token *token = cp_lexer_peek_token (parser->lexer);
35985 if (token->type != CPP_OPEN_BRACE)
35986 return; /* No ivars specified. */
35988 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
35989 token = cp_lexer_peek_token (parser->lexer);
35991 while (token->type != CPP_CLOSE_BRACE
35992 && token->keyword != RID_AT_END && token->type != CPP_EOF)
35994 cp_decl_specifier_seq declspecs;
35995 int decl_class_or_enum_p;
35996 tree prefix_attributes;
35998 cp_parser_objc_visibility_spec (parser);
36000 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
36001 break;
36003 cp_parser_decl_specifier_seq (parser,
36004 CP_PARSER_FLAGS_OPTIONAL,
36005 &declspecs,
36006 &decl_class_or_enum_p);
36008 /* auto, register, static, extern, mutable. */
36009 if (declspecs.storage_class != sc_none)
36011 cp_parser_error (parser, "invalid type for instance variable");
36012 declspecs.storage_class = sc_none;
36015 /* thread_local. */
36016 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
36018 cp_parser_error (parser, "invalid type for instance variable");
36019 declspecs.locations[ds_thread] = 0;
36022 /* typedef. */
36023 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
36025 cp_parser_error (parser, "invalid type for instance variable");
36026 declspecs.locations[ds_typedef] = 0;
36029 prefix_attributes = declspecs.attributes;
36030 declspecs.attributes = NULL_TREE;
36032 /* Keep going until we hit the `;' at the end of the
36033 declaration. */
36034 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
36036 tree width = NULL_TREE, attributes, first_attribute, decl;
36037 cp_declarator *declarator = NULL;
36038 int ctor_dtor_or_conv_p;
36040 /* Check for a (possibly unnamed) bitfield declaration. */
36041 token = cp_lexer_peek_token (parser->lexer);
36042 if (token->type == CPP_COLON)
36043 goto eat_colon;
36045 if (token->type == CPP_NAME
36046 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
36047 == CPP_COLON))
36049 /* Get the name of the bitfield. */
36050 declarator = make_id_declarator (NULL_TREE,
36051 cp_parser_identifier (parser),
36052 sfk_none, token->location);
36054 eat_colon:
36055 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
36056 /* Get the width of the bitfield. */
36057 width
36058 = cp_parser_constant_expression (parser);
36060 else
36062 /* Parse the declarator. */
36063 declarator
36064 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
36065 CP_PARSER_FLAGS_NONE,
36066 &ctor_dtor_or_conv_p,
36067 /*parenthesized_p=*/NULL,
36068 /*member_p=*/false,
36069 /*friend_p=*/false,
36070 /*static_p=*/false);
36073 /* Look for attributes that apply to the ivar. */
36074 attributes = cp_parser_attributes_opt (parser);
36075 /* Remember which attributes are prefix attributes and
36076 which are not. */
36077 first_attribute = attributes;
36078 /* Combine the attributes. */
36079 attributes = attr_chainon (prefix_attributes, attributes);
36081 if (width)
36082 /* Create the bitfield declaration. */
36083 decl = grokbitfield (declarator, &declspecs,
36084 width, NULL_TREE, attributes);
36085 else
36086 decl = grokfield (declarator, &declspecs,
36087 NULL_TREE, /*init_const_expr_p=*/false,
36088 NULL_TREE, attributes);
36090 /* Add the instance variable. */
36091 if (decl != error_mark_node && decl != NULL_TREE)
36092 objc_add_instance_variable (decl);
36094 /* Reset PREFIX_ATTRIBUTES. */
36095 if (attributes != error_mark_node)
36097 while (attributes && TREE_CHAIN (attributes) != first_attribute)
36098 attributes = TREE_CHAIN (attributes);
36099 if (attributes)
36100 TREE_CHAIN (attributes) = NULL_TREE;
36103 token = cp_lexer_peek_token (parser->lexer);
36105 if (token->type == CPP_COMMA)
36107 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
36108 continue;
36110 break;
36113 cp_parser_consume_semicolon_at_end_of_statement (parser);
36114 token = cp_lexer_peek_token (parser->lexer);
36117 if (token->keyword == RID_AT_END)
36118 cp_parser_error (parser, "expected %<}%>");
36120 /* Do not consume the RID_AT_END, so it will be read again as terminating
36121 the @interface of @implementation. */
36122 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
36123 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
36125 /* For historical reasons, we accept an optional semicolon. */
36126 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
36127 cp_lexer_consume_token (parser->lexer);
36130 /* Parse an Objective-C protocol declaration. */
36132 static void
36133 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
36135 tree proto, protorefs;
36136 cp_token *tok;
36138 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
36139 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
36141 tok = cp_lexer_peek_token (parser->lexer);
36142 error_at (tok->location, "identifier expected after %<@protocol%>");
36143 cp_parser_consume_semicolon_at_end_of_statement (parser);
36144 return;
36147 /* See if we have a forward declaration or a definition. */
36148 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
36150 /* Try a forward declaration first. */
36151 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
36153 while (true)
36155 tree id;
36157 id = cp_parser_identifier (parser);
36158 if (id == error_mark_node)
36159 break;
36161 objc_declare_protocol (id, attributes);
36163 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36164 cp_lexer_consume_token (parser->lexer);
36165 else
36166 break;
36168 cp_parser_consume_semicolon_at_end_of_statement (parser);
36171 /* Ok, we got a full-fledged definition (or at least should). */
36172 else
36174 proto = cp_parser_identifier (parser);
36175 protorefs = cp_parser_objc_protocol_refs_opt (parser);
36176 objc_start_protocol (proto, protorefs, attributes);
36177 cp_parser_objc_method_prototype_list (parser);
36181 /* Parse an Objective-C superclass or category. */
36183 static void
36184 cp_parser_objc_superclass_or_category (cp_parser *parser,
36185 bool iface_p,
36186 tree *super,
36187 tree *categ, bool *is_class_extension)
36189 cp_token *next = cp_lexer_peek_token (parser->lexer);
36191 *super = *categ = NULL_TREE;
36192 *is_class_extension = false;
36193 if (next->type == CPP_COLON)
36195 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
36196 *super = cp_parser_identifier (parser);
36198 else if (next->type == CPP_OPEN_PAREN)
36200 matching_parens parens;
36201 parens.consume_open (parser); /* Eat '('. */
36203 /* If there is no category name, and this is an @interface, we
36204 have a class extension. */
36205 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
36207 *categ = NULL_TREE;
36208 *is_class_extension = true;
36210 else
36211 *categ = cp_parser_identifier (parser);
36213 parens.require_close (parser);
36217 /* Parse an Objective-C class interface. */
36219 static void
36220 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
36222 tree name, super, categ, protos;
36223 bool is_class_extension;
36225 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
36226 location_t nam_loc = cp_lexer_peek_token (parser->lexer)->location;
36227 name = cp_parser_identifier (parser);
36228 if (name == error_mark_node)
36230 /* It's hard to recover because even if valid @interface stuff
36231 is to follow, we can't compile it (or validate it) if we
36232 don't even know which class it refers to. Let's assume this
36233 was a stray '@interface' token in the stream and skip it.
36235 return;
36237 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
36238 &is_class_extension);
36239 protos = cp_parser_objc_protocol_refs_opt (parser);
36241 /* We have either a class or a category on our hands. */
36242 if (categ || is_class_extension)
36243 objc_start_category_interface (name, categ, protos, attributes);
36244 else
36246 objc_start_class_interface (name, nam_loc, super, protos, attributes);
36247 /* Handle instance variable declarations, if any. */
36248 cp_parser_objc_class_ivars (parser);
36249 objc_continue_interface ();
36252 cp_parser_objc_method_prototype_list (parser);
36255 /* Parse an Objective-C class implementation. */
36257 static void
36258 cp_parser_objc_class_implementation (cp_parser* parser)
36260 tree name, super, categ;
36261 bool is_class_extension;
36263 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
36264 name = cp_parser_identifier (parser);
36265 if (name == error_mark_node)
36267 /* It's hard to recover because even if valid @implementation
36268 stuff is to follow, we can't compile it (or validate it) if
36269 we don't even know which class it refers to. Let's assume
36270 this was a stray '@implementation' token in the stream and
36271 skip it.
36273 return;
36275 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
36276 &is_class_extension);
36278 /* We have either a class or a category on our hands. */
36279 if (categ)
36280 objc_start_category_implementation (name, categ);
36281 else
36283 objc_start_class_implementation (name, super);
36284 /* Handle instance variable declarations, if any. */
36285 cp_parser_objc_class_ivars (parser);
36286 objc_continue_implementation ();
36289 cp_parser_objc_method_definition_list (parser);
36292 /* Consume the @end token and finish off the implementation. */
36294 static void
36295 cp_parser_objc_end_implementation (cp_parser* parser)
36297 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
36298 objc_finish_implementation ();
36301 /* Parse an Objective-C declaration. */
36303 static void
36304 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
36306 /* Try to figure out what kind of declaration is present. */
36307 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
36309 if (attributes)
36310 switch (kwd->keyword)
36312 case RID_AT_ALIAS:
36313 case RID_AT_CLASS:
36314 case RID_AT_END:
36315 error_at (kwd->location, "attributes may not be specified before"
36316 " the %<@%D%> Objective-C++ keyword",
36317 kwd->u.value);
36318 attributes = NULL;
36319 break;
36320 case RID_AT_IMPLEMENTATION:
36321 warning_at (kwd->location, OPT_Wattributes,
36322 "prefix attributes are ignored before %<@%D%>",
36323 kwd->u.value);
36324 attributes = NULL;
36325 default:
36326 break;
36329 switch (kwd->keyword)
36331 case RID_AT_ALIAS:
36332 cp_parser_objc_alias_declaration (parser);
36333 break;
36334 case RID_AT_CLASS:
36335 cp_parser_objc_class_declaration (parser);
36336 break;
36337 case RID_AT_PROTOCOL:
36338 cp_parser_objc_protocol_declaration (parser, attributes);
36339 break;
36340 case RID_AT_INTERFACE:
36341 cp_parser_objc_class_interface (parser, attributes);
36342 break;
36343 case RID_AT_IMPLEMENTATION:
36344 cp_parser_objc_class_implementation (parser);
36345 break;
36346 case RID_AT_END:
36347 cp_parser_objc_end_implementation (parser);
36348 break;
36349 default:
36350 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
36351 kwd->u.value);
36352 cp_parser_skip_to_end_of_block_or_statement (parser);
36356 /* Parse an Objective-C try-catch-finally statement.
36358 objc-try-catch-finally-stmt:
36359 @try compound-statement objc-catch-clause-seq [opt]
36360 objc-finally-clause [opt]
36362 objc-catch-clause-seq:
36363 objc-catch-clause objc-catch-clause-seq [opt]
36365 objc-catch-clause:
36366 @catch ( objc-exception-declaration ) compound-statement
36368 objc-finally-clause:
36369 @finally compound-statement
36371 objc-exception-declaration:
36372 parameter-declaration
36373 '...'
36375 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
36377 Returns NULL_TREE.
36379 PS: This function is identical to c_parser_objc_try_catch_finally_statement
36380 for C. Keep them in sync. */
36382 static tree
36383 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
36385 location_t location;
36386 tree stmt;
36388 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
36389 location = cp_lexer_peek_token (parser->lexer)->location;
36390 objc_maybe_warn_exceptions (location);
36391 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
36392 node, lest it get absorbed into the surrounding block. */
36393 stmt = push_stmt_list ();
36394 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
36395 objc_begin_try_stmt (location, pop_stmt_list (stmt));
36397 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
36399 cp_parameter_declarator *parm;
36400 tree parameter_declaration = error_mark_node;
36401 bool seen_open_paren = false;
36402 matching_parens parens;
36404 cp_lexer_consume_token (parser->lexer);
36405 if (parens.require_open (parser))
36406 seen_open_paren = true;
36407 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
36409 /* We have "@catch (...)" (where the '...' are literally
36410 what is in the code). Skip the '...'.
36411 parameter_declaration is set to NULL_TREE, and
36412 objc_being_catch_clauses() knows that that means
36413 '...'. */
36414 cp_lexer_consume_token (parser->lexer);
36415 parameter_declaration = NULL_TREE;
36417 else
36419 /* We have "@catch (NSException *exception)" or something
36420 like that. Parse the parameter declaration. */
36421 parm = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
36422 false, NULL);
36423 if (parm == NULL)
36424 parameter_declaration = error_mark_node;
36425 else
36426 parameter_declaration = grokdeclarator (parm->declarator,
36427 &parm->decl_specifiers,
36428 PARM, /*initialized=*/0,
36429 /*attrlist=*/NULL);
36431 if (seen_open_paren)
36432 parens.require_close (parser);
36433 else
36435 /* If there was no open parenthesis, we are recovering from
36436 an error, and we are trying to figure out what mistake
36437 the user has made. */
36439 /* If there is an immediate closing parenthesis, the user
36440 probably forgot the opening one (ie, they typed "@catch
36441 NSException *e)". Parse the closing parenthesis and keep
36442 going. */
36443 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
36444 cp_lexer_consume_token (parser->lexer);
36446 /* If these is no immediate closing parenthesis, the user
36447 probably doesn't know that parenthesis are required at
36448 all (ie, they typed "@catch NSException *e"). So, just
36449 forget about the closing parenthesis and keep going. */
36451 objc_begin_catch_clause (parameter_declaration);
36452 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
36453 objc_finish_catch_clause ();
36455 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
36457 cp_lexer_consume_token (parser->lexer);
36458 location = cp_lexer_peek_token (parser->lexer)->location;
36459 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
36460 node, lest it get absorbed into the surrounding block. */
36461 stmt = push_stmt_list ();
36462 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
36463 objc_build_finally_clause (location, pop_stmt_list (stmt));
36466 return objc_finish_try_stmt ();
36469 /* Parse an Objective-C synchronized statement.
36471 objc-synchronized-stmt:
36472 @synchronized ( expression ) compound-statement
36474 Returns NULL_TREE. */
36476 static tree
36477 cp_parser_objc_synchronized_statement (cp_parser *parser)
36479 location_t location;
36480 tree lock, stmt;
36482 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
36484 location = cp_lexer_peek_token (parser->lexer)->location;
36485 objc_maybe_warn_exceptions (location);
36486 matching_parens parens;
36487 parens.require_open (parser);
36488 lock = cp_parser_expression (parser);
36489 parens.require_close (parser);
36491 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
36492 node, lest it get absorbed into the surrounding block. */
36493 stmt = push_stmt_list ();
36494 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
36496 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
36499 /* Parse an Objective-C throw statement.
36501 objc-throw-stmt:
36502 @throw assignment-expression [opt] ;
36504 Returns a constructed '@throw' statement. */
36506 static tree
36507 cp_parser_objc_throw_statement (cp_parser *parser)
36509 tree expr = NULL_TREE;
36510 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36512 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
36514 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
36515 expr = cp_parser_expression (parser);
36517 cp_parser_consume_semicolon_at_end_of_statement (parser);
36519 return objc_build_throw_stmt (loc, expr);
36522 /* Parse an Objective-C statement. */
36524 static tree
36525 cp_parser_objc_statement (cp_parser * parser)
36527 /* Try to figure out what kind of declaration is present. */
36528 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
36530 switch (kwd->keyword)
36532 case RID_AT_TRY:
36533 return cp_parser_objc_try_catch_finally_statement (parser);
36534 case RID_AT_SYNCHRONIZED:
36535 return cp_parser_objc_synchronized_statement (parser);
36536 case RID_AT_THROW:
36537 return cp_parser_objc_throw_statement (parser);
36538 default:
36539 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
36540 kwd->u.value);
36541 cp_parser_skip_to_end_of_block_or_statement (parser);
36544 return error_mark_node;
36547 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
36548 look ahead to see if an objc keyword follows the attributes. This
36549 is to detect the use of prefix attributes on ObjC @interface and
36550 @protocol. */
36552 static bool
36553 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
36555 cp_lexer_save_tokens (parser->lexer);
36556 tree addon = cp_parser_attributes_opt (parser);
36557 if (addon
36558 && OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
36560 cp_lexer_commit_tokens (parser->lexer);
36561 if (*attrib)
36562 TREE_CHAIN (*attrib) = addon;
36563 else
36564 *attrib = addon;
36565 return true;
36567 cp_lexer_rollback_tokens (parser->lexer);
36568 return false;
36571 /* This routine is a minimal replacement for
36572 c_parser_struct_declaration () used when parsing the list of
36573 types/names or ObjC++ properties. For example, when parsing the
36574 code
36576 @property (readonly) int a, b, c;
36578 this function is responsible for parsing "int a, int b, int c" and
36579 returning the declarations as CHAIN of DECLs.
36581 TODO: Share this code with cp_parser_objc_class_ivars. It's very
36582 similar parsing. */
36583 static tree
36584 cp_parser_objc_struct_declaration (cp_parser *parser)
36586 tree decls = NULL_TREE;
36587 cp_decl_specifier_seq declspecs;
36588 int decl_class_or_enum_p;
36589 tree prefix_attributes;
36591 cp_parser_decl_specifier_seq (parser,
36592 CP_PARSER_FLAGS_NONE,
36593 &declspecs,
36594 &decl_class_or_enum_p);
36596 if (declspecs.type == error_mark_node)
36597 return error_mark_node;
36599 /* auto, register, static, extern, mutable. */
36600 if (declspecs.storage_class != sc_none)
36602 cp_parser_error (parser, "invalid type for property");
36603 declspecs.storage_class = sc_none;
36606 /* thread_local. */
36607 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
36609 cp_parser_error (parser, "invalid type for property");
36610 declspecs.locations[ds_thread] = 0;
36613 /* typedef. */
36614 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
36616 cp_parser_error (parser, "invalid type for property");
36617 declspecs.locations[ds_typedef] = 0;
36620 prefix_attributes = declspecs.attributes;
36621 declspecs.attributes = NULL_TREE;
36623 /* Keep going until we hit the `;' at the end of the declaration. */
36624 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
36626 tree attributes, first_attribute, decl;
36627 cp_declarator *declarator;
36628 cp_token *token;
36630 /* Parse the declarator. */
36631 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
36632 CP_PARSER_FLAGS_NONE,
36633 NULL, NULL, false, false, false);
36635 /* Look for attributes that apply to the ivar. */
36636 attributes = cp_parser_attributes_opt (parser);
36637 /* Remember which attributes are prefix attributes and
36638 which are not. */
36639 first_attribute = attributes;
36640 /* Combine the attributes. */
36641 attributes = attr_chainon (prefix_attributes, attributes);
36643 decl = grokfield (declarator, &declspecs,
36644 NULL_TREE, /*init_const_expr_p=*/false,
36645 NULL_TREE, attributes);
36647 if (decl == error_mark_node || decl == NULL_TREE)
36648 return error_mark_node;
36650 /* Reset PREFIX_ATTRIBUTES. */
36651 if (attributes != error_mark_node)
36653 while (attributes && TREE_CHAIN (attributes) != first_attribute)
36654 attributes = TREE_CHAIN (attributes);
36655 if (attributes)
36656 TREE_CHAIN (attributes) = NULL_TREE;
36659 DECL_CHAIN (decl) = decls;
36660 decls = decl;
36662 token = cp_lexer_peek_token (parser->lexer);
36663 if (token->type == CPP_COMMA)
36665 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
36666 continue;
36668 else
36669 break;
36671 return decls;
36674 /* Parse an Objective-C @property declaration. The syntax is:
36676 objc-property-declaration:
36677 '@property' objc-property-attributes[opt] struct-declaration ;
36679 objc-property-attributes:
36680 '(' objc-property-attribute-list ')'
36682 objc-property-attribute-list:
36683 objc-property-attribute
36684 objc-property-attribute-list, objc-property-attribute
36686 objc-property-attribute
36687 'getter' = identifier
36688 'setter' = identifier
36689 'readonly'
36690 'readwrite'
36691 'assign'
36692 'retain'
36693 'copy'
36694 'nonatomic'
36696 For example:
36697 @property NSString *name;
36698 @property (readonly) id object;
36699 @property (retain, nonatomic, getter=getTheName) id name;
36700 @property int a, b, c;
36702 PS: This function is identical to
36703 c_parser_objc_at_property_declaration for C. Keep them in sync. */
36704 static void
36705 cp_parser_objc_at_property_declaration (cp_parser *parser)
36707 /* Parse the optional attribute list.
36709 A list of parsed, but not verified, attributes. */
36710 auto_delete_vec<property_attribute_info> prop_attr_list;
36711 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36713 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
36715 /* Parse the optional attribute list... */
36716 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36718 /* Eat the '('. */
36719 matching_parens parens;
36720 location_t attr_start = cp_lexer_peek_token (parser->lexer)->location;
36721 parens.consume_open (parser);
36722 bool syntax_error = false;
36724 /* Allow empty @property attribute lists, but with a warning. */
36725 location_t attr_end = cp_lexer_peek_token (parser->lexer)->location;
36726 location_t attr_comb;
36727 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
36729 attr_comb = make_location (attr_end, attr_start, attr_end);
36730 warning_at (attr_comb, OPT_Wattributes,
36731 "empty property attribute list");
36733 else
36734 while (true)
36736 cp_token *token = cp_lexer_peek_token (parser->lexer);
36737 attr_start = token->location;
36738 attr_end = get_finish (token->location);
36739 attr_comb = make_location (attr_start, attr_start, attr_end);
36741 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_COMMA)
36743 warning_at (attr_comb, OPT_Wattributes,
36744 "missing property attribute");
36745 if (token->type == CPP_CLOSE_PAREN)
36746 break;
36747 cp_lexer_consume_token (parser->lexer);
36748 continue;
36751 tree attr_name = NULL_TREE;
36752 if (identifier_p (token->u.value))
36753 attr_name = token->u.value;
36755 enum rid keyword;
36756 if (token->type == CPP_NAME)
36757 keyword = C_RID_CODE (token->u.value);
36758 else if (token->type == CPP_KEYWORD
36759 && token->keyword == RID_CLASS)
36760 /* Account for accepting the 'class' keyword in this context. */
36761 keyword = RID_CLASS;
36762 else
36763 keyword = RID_MAX; /* By definition, an unknown property. */
36764 cp_lexer_consume_token (parser->lexer);
36766 enum objc_property_attribute_kind prop_kind
36767 = objc_prop_attr_kind_for_rid (keyword);
36768 property_attribute_info *prop
36769 = new property_attribute_info (attr_name, attr_comb, prop_kind);
36770 prop_attr_list.safe_push (prop);
36772 tree meth_name;
36773 switch (prop->prop_kind)
36775 default: break;
36776 case OBJC_PROPERTY_ATTR_UNKNOWN:
36777 if (attr_name)
36778 error_at (attr_start, "unknown property attribute %qE",
36779 attr_name);
36780 else
36781 error_at (attr_start, "unknown property attribute");
36782 prop->parse_error = syntax_error = true;
36783 break;
36785 case OBJC_PROPERTY_ATTR_GETTER:
36786 case OBJC_PROPERTY_ATTR_SETTER:
36787 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
36789 attr_comb = make_location (attr_end, attr_start, attr_end);
36790 error_at (attr_comb, "expected %<=%> after Objective-C %qE",
36791 attr_name);
36792 prop->parse_error = syntax_error = true;
36793 break;
36796 token = cp_lexer_peek_token (parser->lexer);
36797 attr_end = token->location;
36798 cp_lexer_consume_token (parser->lexer); /* eat the = */
36800 if (!cp_parser_objc_selector_p
36801 (cp_lexer_peek_token (parser->lexer)->type))
36803 attr_comb = make_location (attr_end, attr_start, attr_end);
36804 error_at (attr_comb, "expected %qE selector name",
36805 attr_name);
36806 prop->parse_error = syntax_error = true;
36807 break;
36810 /* Get the end of the method name, and consume the name. */
36811 token = cp_lexer_peek_token (parser->lexer);
36812 attr_end = get_finish (token->location);
36813 /* Because method names may contain C++ keywords, we have a
36814 routine to fetch them (this also consumes the token). */
36815 meth_name = cp_parser_objc_selector (parser);
36817 if (prop->prop_kind == OBJC_PROPERTY_ATTR_SETTER)
36819 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
36821 attr_comb = make_location (attr_end, attr_start,
36822 attr_end);
36823 error_at (attr_comb, "setter method names must"
36824 " terminate with %<:%>");
36825 prop->parse_error = syntax_error = true;
36827 else
36829 attr_end = get_finish (cp_lexer_peek_token
36830 (parser->lexer)->location);
36831 cp_lexer_consume_token (parser->lexer);
36833 attr_comb = make_location (attr_start, attr_start,
36834 attr_end);
36836 else
36837 attr_comb = make_location (attr_start, attr_start,
36838 attr_end);
36839 prop->ident = meth_name;
36840 /* Updated location including all that was successfully
36841 parsed. */
36842 prop->prop_loc = attr_comb;
36843 break;
36846 /* If we see a comma here, then keep going - even if we already
36847 saw a syntax error. For simple mistakes e.g. (asign, getter=x)
36848 this makes a more useful output and avoid spurious warnings
36849 about missing attributes that are, in fact, specified after the
36850 one with the syntax error. */
36851 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36852 cp_lexer_consume_token (parser->lexer);
36853 else
36854 break;
36857 if (syntax_error || !parens.require_close (parser))
36858 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36859 /*or_comma=*/false,
36860 /*consume_paren=*/true);
36863 /* 'properties' is the list of properties that we read. Usually a
36864 single one, but maybe more (eg, in "@property int a, b, c;" there
36865 are three).
36866 TODO: Update this parsing so that it accepts (erroneous) bitfields so
36867 that we can issue a meaningful and consistent (between C/C++) error
36868 message from objc_add_property_declaration (). */
36869 tree properties = cp_parser_objc_struct_declaration (parser);
36871 if (properties == error_mark_node)
36872 cp_parser_skip_to_end_of_statement (parser);
36873 else if (properties == NULL_TREE)
36874 cp_parser_error (parser, "expected identifier");
36875 else
36877 /* Comma-separated properties are chained together in reverse order;
36878 add them one by one. */
36879 properties = nreverse (properties);
36880 for (; properties; properties = TREE_CHAIN (properties))
36881 objc_add_property_declaration (loc, copy_node (properties),
36882 prop_attr_list);
36885 cp_parser_consume_semicolon_at_end_of_statement (parser);
36888 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
36890 objc-synthesize-declaration:
36891 @synthesize objc-synthesize-identifier-list ;
36893 objc-synthesize-identifier-list:
36894 objc-synthesize-identifier
36895 objc-synthesize-identifier-list, objc-synthesize-identifier
36897 objc-synthesize-identifier
36898 identifier
36899 identifier = identifier
36901 For example:
36902 @synthesize MyProperty;
36903 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
36905 PS: This function is identical to c_parser_objc_at_synthesize_declaration
36906 for C. Keep them in sync.
36908 static void
36909 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
36911 tree list = NULL_TREE;
36912 location_t loc;
36913 loc = cp_lexer_peek_token (parser->lexer)->location;
36915 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
36916 while (true)
36918 tree property, ivar;
36919 property = cp_parser_identifier (parser);
36920 if (property == error_mark_node)
36922 cp_parser_consume_semicolon_at_end_of_statement (parser);
36923 return;
36925 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
36927 cp_lexer_consume_token (parser->lexer);
36928 ivar = cp_parser_identifier (parser);
36929 if (ivar == error_mark_node)
36931 cp_parser_consume_semicolon_at_end_of_statement (parser);
36932 return;
36935 else
36936 ivar = NULL_TREE;
36937 list = chainon (list, build_tree_list (ivar, property));
36938 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36939 cp_lexer_consume_token (parser->lexer);
36940 else
36941 break;
36943 cp_parser_consume_semicolon_at_end_of_statement (parser);
36944 objc_add_synthesize_declaration (loc, list);
36947 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
36949 objc-dynamic-declaration:
36950 @dynamic identifier-list ;
36952 For example:
36953 @dynamic MyProperty;
36954 @dynamic MyProperty, AnotherProperty;
36956 PS: This function is identical to c_parser_objc_at_dynamic_declaration
36957 for C. Keep them in sync.
36959 static void
36960 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
36962 tree list = NULL_TREE;
36963 location_t loc;
36964 loc = cp_lexer_peek_token (parser->lexer)->location;
36966 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
36967 while (true)
36969 tree property;
36970 property = cp_parser_identifier (parser);
36971 if (property == error_mark_node)
36973 cp_parser_consume_semicolon_at_end_of_statement (parser);
36974 return;
36976 list = chainon (list, build_tree_list (NULL, property));
36977 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36978 cp_lexer_consume_token (parser->lexer);
36979 else
36980 break;
36982 cp_parser_consume_semicolon_at_end_of_statement (parser);
36983 objc_add_dynamic_declaration (loc, list);
36987 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 / 4.5 / 5.0 parsing routines. */
36989 /* Returns name of the next clause.
36990 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
36991 the token is not consumed. Otherwise appropriate pragma_omp_clause is
36992 returned and the token is consumed. */
36994 static pragma_omp_clause
36995 cp_parser_omp_clause_name (cp_parser *parser)
36997 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
36999 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
37000 result = PRAGMA_OACC_CLAUSE_AUTO;
37001 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
37002 result = PRAGMA_OMP_CLAUSE_IF;
37003 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
37004 result = PRAGMA_OMP_CLAUSE_DEFAULT;
37005 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
37006 result = PRAGMA_OACC_CLAUSE_DELETE;
37007 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
37008 result = PRAGMA_OMP_CLAUSE_PRIVATE;
37009 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
37010 result = PRAGMA_OMP_CLAUSE_FOR;
37011 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37013 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37014 const char *p = IDENTIFIER_POINTER (id);
37016 switch (p[0])
37018 case 'a':
37019 if (!strcmp ("affinity", p))
37020 result = PRAGMA_OMP_CLAUSE_AFFINITY;
37021 else if (!strcmp ("aligned", p))
37022 result = PRAGMA_OMP_CLAUSE_ALIGNED;
37023 else if (!strcmp ("allocate", p))
37024 result = PRAGMA_OMP_CLAUSE_ALLOCATE;
37025 else if (!strcmp ("async", p))
37026 result = PRAGMA_OACC_CLAUSE_ASYNC;
37027 else if (!strcmp ("attach", p))
37028 result = PRAGMA_OACC_CLAUSE_ATTACH;
37029 break;
37030 case 'b':
37031 if (!strcmp ("bind", p))
37032 result = PRAGMA_OMP_CLAUSE_BIND;
37033 break;
37034 case 'c':
37035 if (!strcmp ("collapse", p))
37036 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
37037 else if (!strcmp ("copy", p))
37038 result = PRAGMA_OACC_CLAUSE_COPY;
37039 else if (!strcmp ("copyin", p))
37040 result = PRAGMA_OMP_CLAUSE_COPYIN;
37041 else if (!strcmp ("copyout", p))
37042 result = PRAGMA_OACC_CLAUSE_COPYOUT;
37043 else if (!strcmp ("copyprivate", p))
37044 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
37045 else if (!strcmp ("create", p))
37046 result = PRAGMA_OACC_CLAUSE_CREATE;
37047 break;
37048 case 'd':
37049 if (!strcmp ("defaultmap", p))
37050 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
37051 else if (!strcmp ("depend", p))
37052 result = PRAGMA_OMP_CLAUSE_DEPEND;
37053 else if (!strcmp ("detach", p))
37054 result = PRAGMA_OACC_CLAUSE_DETACH;
37055 else if (!strcmp ("device", p))
37056 result = PRAGMA_OMP_CLAUSE_DEVICE;
37057 else if (!strcmp ("deviceptr", p))
37058 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
37059 else if (!strcmp ("device_resident", p))
37060 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
37061 else if (!strcmp ("device_type", p))
37062 result = PRAGMA_OMP_CLAUSE_DEVICE_TYPE;
37063 else if (!strcmp ("dist_schedule", p))
37064 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
37065 else if (!strcmp ("doacross", p))
37066 result = PRAGMA_OMP_CLAUSE_DOACROSS;
37067 break;
37068 case 'e':
37069 if (!strcmp ("enter", p))
37070 result = PRAGMA_OMP_CLAUSE_ENTER;
37071 break;
37072 case 'f':
37073 if (!strcmp ("filter", p))
37074 result = PRAGMA_OMP_CLAUSE_FILTER;
37075 else if (!strcmp ("final", p))
37076 result = PRAGMA_OMP_CLAUSE_FINAL;
37077 else if (!strcmp ("finalize", p))
37078 result = PRAGMA_OACC_CLAUSE_FINALIZE;
37079 else if (!strcmp ("firstprivate", p))
37080 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
37081 else if (!strcmp ("from", p))
37082 result = PRAGMA_OMP_CLAUSE_FROM;
37083 break;
37084 case 'g':
37085 if (!strcmp ("gang", p))
37086 result = PRAGMA_OACC_CLAUSE_GANG;
37087 else if (!strcmp ("grainsize", p))
37088 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
37089 break;
37090 case 'h':
37091 if (!strcmp ("has_device_addr", p))
37092 result = PRAGMA_OMP_CLAUSE_HAS_DEVICE_ADDR;
37093 else if (!strcmp ("hint", p))
37094 result = PRAGMA_OMP_CLAUSE_HINT;
37095 else if (!strcmp ("host", p))
37096 result = PRAGMA_OACC_CLAUSE_HOST;
37097 break;
37098 case 'i':
37099 if (!strcmp ("if_present", p))
37100 result = PRAGMA_OACC_CLAUSE_IF_PRESENT;
37101 else if (!strcmp ("in_reduction", p))
37102 result = PRAGMA_OMP_CLAUSE_IN_REDUCTION;
37103 else if (!strcmp ("inbranch", p))
37104 result = PRAGMA_OMP_CLAUSE_INBRANCH;
37105 else if (!strcmp ("independent", p))
37106 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
37107 else if (!strcmp ("is_device_ptr", p))
37108 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
37109 break;
37110 case 'l':
37111 if (!strcmp ("lastprivate", p))
37112 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
37113 else if (!strcmp ("linear", p))
37114 result = PRAGMA_OMP_CLAUSE_LINEAR;
37115 else if (!strcmp ("link", p))
37116 result = PRAGMA_OMP_CLAUSE_LINK;
37117 break;
37118 case 'm':
37119 if (!strcmp ("map", p))
37120 result = PRAGMA_OMP_CLAUSE_MAP;
37121 else if (!strcmp ("mergeable", p))
37122 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
37123 break;
37124 case 'n':
37125 if (!strcmp ("no_create", p))
37126 result = PRAGMA_OACC_CLAUSE_NO_CREATE;
37127 else if (!strcmp ("nogroup", p))
37128 result = PRAGMA_OMP_CLAUSE_NOGROUP;
37129 else if (!strcmp ("nohost", p))
37130 result = PRAGMA_OACC_CLAUSE_NOHOST;
37131 else if (!strcmp ("nontemporal", p))
37132 result = PRAGMA_OMP_CLAUSE_NONTEMPORAL;
37133 else if (!strcmp ("notinbranch", p))
37134 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
37135 else if (!strcmp ("nowait", p))
37136 result = PRAGMA_OMP_CLAUSE_NOWAIT;
37137 else if (!strcmp ("num_gangs", p))
37138 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
37139 else if (!strcmp ("num_tasks", p))
37140 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
37141 else if (!strcmp ("num_teams", p))
37142 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
37143 else if (!strcmp ("num_threads", p))
37144 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
37145 else if (!strcmp ("num_workers", p))
37146 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
37147 break;
37148 case 'o':
37149 if (!strcmp ("ordered", p))
37150 result = PRAGMA_OMP_CLAUSE_ORDERED;
37151 else if (!strcmp ("order", p))
37152 result = PRAGMA_OMP_CLAUSE_ORDER;
37153 break;
37154 case 'p':
37155 if (!strcmp ("parallel", p))
37156 result = PRAGMA_OMP_CLAUSE_PARALLEL;
37157 else if (!strcmp ("present", p))
37158 result = PRAGMA_OACC_CLAUSE_PRESENT;
37159 else if (!strcmp ("present_or_copy", p)
37160 || !strcmp ("pcopy", p))
37161 result = PRAGMA_OACC_CLAUSE_COPY;
37162 else if (!strcmp ("present_or_copyin", p)
37163 || !strcmp ("pcopyin", p))
37164 result = PRAGMA_OACC_CLAUSE_COPYIN;
37165 else if (!strcmp ("present_or_copyout", p)
37166 || !strcmp ("pcopyout", p))
37167 result = PRAGMA_OACC_CLAUSE_COPYOUT;
37168 else if (!strcmp ("present_or_create", p)
37169 || !strcmp ("pcreate", p))
37170 result = PRAGMA_OACC_CLAUSE_CREATE;
37171 else if (!strcmp ("priority", p))
37172 result = PRAGMA_OMP_CLAUSE_PRIORITY;
37173 else if (!strcmp ("proc_bind", p))
37174 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
37175 break;
37176 case 'r':
37177 if (!strcmp ("reduction", p))
37178 result = PRAGMA_OMP_CLAUSE_REDUCTION;
37179 break;
37180 case 's':
37181 if (!strcmp ("safelen", p))
37182 result = PRAGMA_OMP_CLAUSE_SAFELEN;
37183 else if (!strcmp ("schedule", p))
37184 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
37185 else if (!strcmp ("sections", p))
37186 result = PRAGMA_OMP_CLAUSE_SECTIONS;
37187 else if (!strcmp ("self", p)) /* "self" is a synonym for "host". */
37188 result = PRAGMA_OACC_CLAUSE_HOST;
37189 else if (!strcmp ("seq", p))
37190 result = PRAGMA_OACC_CLAUSE_SEQ;
37191 else if (!strcmp ("shared", p))
37192 result = PRAGMA_OMP_CLAUSE_SHARED;
37193 else if (!strcmp ("simd", p))
37194 result = PRAGMA_OMP_CLAUSE_SIMD;
37195 else if (!strcmp ("simdlen", p))
37196 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
37197 break;
37198 case 't':
37199 if (!strcmp ("task_reduction", p))
37200 result = PRAGMA_OMP_CLAUSE_TASK_REDUCTION;
37201 else if (!strcmp ("taskgroup", p))
37202 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
37203 else if (!strcmp ("thread_limit", p))
37204 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
37205 else if (!strcmp ("threads", p))
37206 result = PRAGMA_OMP_CLAUSE_THREADS;
37207 else if (!strcmp ("tile", p))
37208 result = PRAGMA_OACC_CLAUSE_TILE;
37209 else if (!strcmp ("to", p))
37210 result = PRAGMA_OMP_CLAUSE_TO;
37211 break;
37212 case 'u':
37213 if (!strcmp ("uniform", p))
37214 result = PRAGMA_OMP_CLAUSE_UNIFORM;
37215 else if (!strcmp ("untied", p))
37216 result = PRAGMA_OMP_CLAUSE_UNTIED;
37217 else if (!strcmp ("use_device", p))
37218 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
37219 else if (!strcmp ("use_device_addr", p))
37220 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR;
37221 else if (!strcmp ("use_device_ptr", p))
37222 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
37223 break;
37224 case 'v':
37225 if (!strcmp ("vector", p))
37226 result = PRAGMA_OACC_CLAUSE_VECTOR;
37227 else if (!strcmp ("vector_length", p))
37228 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
37229 break;
37230 case 'w':
37231 if (!strcmp ("wait", p))
37232 result = PRAGMA_OACC_CLAUSE_WAIT;
37233 else if (!strcmp ("worker", p))
37234 result = PRAGMA_OACC_CLAUSE_WORKER;
37235 break;
37239 if (result != PRAGMA_OMP_CLAUSE_NONE)
37240 cp_lexer_consume_token (parser->lexer);
37242 return result;
37245 /* Validate that a clause of the given type does not already exist. */
37247 static void
37248 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
37249 const char *name, location_t location)
37251 if (omp_find_clause (clauses, code))
37252 error_at (location, "too many %qs clauses", name);
37255 /* OpenMP 2.5:
37256 variable-list:
37257 identifier
37258 variable-list , identifier
37260 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
37261 colon). An opening parenthesis will have been consumed by the caller.
37263 If KIND is nonzero, create the appropriate node and install the decl
37264 in OMP_CLAUSE_DECL and add the node to the head of the list.
37266 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
37267 return the list created.
37269 COLON can be NULL if only closing parenthesis should end the list,
37270 or pointer to bool which will receive false if the list is terminated
37271 by closing parenthesis or true if the list is terminated by colon.
37273 The optional ALLOW_DEREF argument is true if list items can use the deref
37274 (->) operator. */
37276 struct omp_dim
37278 tree low_bound, length;
37279 location_t loc;
37280 bool no_colon;
37281 omp_dim (tree lb, tree len, location_t lo, bool nc)
37282 : low_bound (lb), length (len), loc (lo), no_colon (nc) {}
37285 static tree
37286 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
37287 tree list, bool *colon,
37288 bool allow_deref = false)
37290 auto_vec<omp_dim> dims;
37291 bool array_section_p;
37292 cp_token *token;
37293 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37294 if (colon)
37296 parser->colon_corrects_to_scope_p = false;
37297 *colon = false;
37299 while (1)
37301 tree name, decl;
37303 if (kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
37304 cp_parser_parse_tentatively (parser);
37305 token = cp_lexer_peek_token (parser->lexer);
37306 if (kind != 0
37307 && cp_parser_is_keyword (token, RID_THIS))
37309 decl = finish_this_expr ();
37310 if (TREE_CODE (decl) == NON_LVALUE_EXPR
37311 || CONVERT_EXPR_P (decl))
37312 decl = TREE_OPERAND (decl, 0);
37313 cp_lexer_consume_token (parser->lexer);
37315 else if (cp_parser_is_keyword (token, RID_FUNCTION_NAME)
37316 || cp_parser_is_keyword (token, RID_PRETTY_FUNCTION_NAME)
37317 || cp_parser_is_keyword (token, RID_C99_FUNCTION_NAME))
37319 cp_id_kind idk;
37320 decl = cp_parser_primary_expression (parser, false, false, false,
37321 &idk);
37323 else if (kind == OMP_CLAUSE_DEPEND
37324 && cp_parser_is_keyword (token, RID_OMP_ALL_MEMORY)
37325 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
37326 || cp_lexer_nth_token_is (parser->lexer, 2,
37327 CPP_CLOSE_PAREN)))
37329 decl = ridpointers[RID_OMP_ALL_MEMORY];
37330 cp_lexer_consume_token (parser->lexer);
37332 else
37334 name = cp_parser_id_expression (parser, /*template_p=*/false,
37335 /*check_dependency_p=*/true,
37336 /*template_p=*/NULL,
37337 /*declarator_p=*/false,
37338 /*optional_p=*/false);
37339 if (name == error_mark_node)
37341 if ((kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
37342 && cp_parser_simulate_error (parser))
37343 goto depend_lvalue;
37344 goto skip_comma;
37347 if (identifier_p (name))
37348 decl = cp_parser_lookup_name_simple (parser, name, token->location);
37349 else
37350 decl = name;
37351 if (decl == error_mark_node)
37353 if ((kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
37354 && cp_parser_simulate_error (parser))
37355 goto depend_lvalue;
37356 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
37357 token->location);
37360 if (outer_automatic_var_p (decl))
37361 decl = process_outer_var_ref (decl, tf_warning_or_error);
37362 if (decl == error_mark_node)
37364 else if (kind != 0)
37366 switch (kind)
37368 case OMP_CLAUSE__CACHE_:
37369 /* The OpenACC cache directive explicitly only allows "array
37370 elements or subarrays". */
37371 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
37373 error_at (token->location, "expected %<[%>");
37374 decl = error_mark_node;
37375 break;
37377 /* FALLTHROUGH. */
37378 case OMP_CLAUSE_MAP:
37379 case OMP_CLAUSE_FROM:
37380 case OMP_CLAUSE_TO:
37381 start_component_ref:
37382 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT)
37383 || (allow_deref
37384 && cp_lexer_next_token_is (parser->lexer, CPP_DEREF)))
37386 cpp_ttype ttype
37387 = cp_lexer_next_token_is (parser->lexer, CPP_DOT)
37388 ? CPP_DOT : CPP_DEREF;
37389 location_t loc
37390 = cp_lexer_peek_token (parser->lexer)->location;
37391 cp_id_kind idk = CP_ID_KIND_NONE;
37392 cp_lexer_consume_token (parser->lexer);
37393 decl = convert_from_reference (decl);
37394 decl = (cp_parser_postfix_dot_deref_expression
37395 (parser, ttype, cp_expr (decl, token->location),
37396 false, &idk, loc));
37398 /* FALLTHROUGH. */
37399 case OMP_CLAUSE_AFFINITY:
37400 case OMP_CLAUSE_DEPEND:
37401 case OMP_CLAUSE_REDUCTION:
37402 case OMP_CLAUSE_IN_REDUCTION:
37403 case OMP_CLAUSE_TASK_REDUCTION:
37404 case OMP_CLAUSE_HAS_DEVICE_ADDR:
37405 array_section_p = false;
37406 dims.truncate (0);
37407 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
37409 location_t loc = UNKNOWN_LOCATION;
37410 tree low_bound = NULL_TREE, length = NULL_TREE;
37411 bool no_colon = false;
37413 parser->colon_corrects_to_scope_p = false;
37414 cp_lexer_consume_token (parser->lexer);
37415 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
37417 loc = cp_lexer_peek_token (parser->lexer)->location;
37418 low_bound = cp_parser_expression (parser);
37419 /* Later handling is not prepared to see through these. */
37420 gcc_checking_assert (!location_wrapper_p (low_bound));
37422 if (!colon)
37423 parser->colon_corrects_to_scope_p
37424 = saved_colon_corrects_to_scope_p;
37425 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
37427 length = integer_one_node;
37428 no_colon = true;
37430 else
37432 /* Look for `:'. */
37433 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37435 if ((kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
37436 && cp_parser_simulate_error (parser))
37437 goto depend_lvalue;
37438 goto skip_comma;
37440 if (kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
37441 cp_parser_commit_to_tentative_parse (parser);
37442 else
37443 array_section_p = true;
37444 if (!cp_lexer_next_token_is (parser->lexer,
37445 CPP_CLOSE_SQUARE))
37447 length = cp_parser_expression (parser);
37448 /* Later handling is not prepared to see through these. */
37449 gcc_checking_assert (!location_wrapper_p (length));
37452 /* Look for the closing `]'. */
37453 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
37454 RT_CLOSE_SQUARE))
37456 if ((kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
37457 && cp_parser_simulate_error (parser))
37458 goto depend_lvalue;
37459 goto skip_comma;
37462 dims.safe_push (omp_dim (low_bound, length, loc, no_colon));
37465 if ((kind == OMP_CLAUSE_MAP
37466 || kind == OMP_CLAUSE_FROM
37467 || kind == OMP_CLAUSE_TO)
37468 && !array_section_p
37469 && (cp_lexer_next_token_is (parser->lexer, CPP_DOT)
37470 || (allow_deref
37471 && cp_lexer_next_token_is (parser->lexer,
37472 CPP_DEREF))))
37474 for (unsigned i = 0; i < dims.length (); i++)
37476 gcc_assert (dims[i].length == integer_one_node);
37477 decl = build_array_ref (dims[i].loc,
37478 decl, dims[i].low_bound);
37480 goto start_component_ref;
37482 else
37483 for (unsigned i = 0; i < dims.length (); i++)
37484 decl = tree_cons (dims[i].low_bound, dims[i].length, decl);
37486 break;
37487 default:
37488 break;
37491 if (kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
37493 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
37494 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
37495 && cp_parser_simulate_error (parser))
37497 depend_lvalue:
37498 cp_parser_abort_tentative_parse (parser);
37499 decl = cp_parser_assignment_expression (parser, NULL,
37500 false, false);
37502 else
37503 cp_parser_parse_definitely (parser);
37506 tree u = build_omp_clause (token->location, kind);
37507 OMP_CLAUSE_DECL (u) = decl;
37508 OMP_CLAUSE_CHAIN (u) = list;
37509 list = u;
37511 else
37512 list = tree_cons (decl, NULL_TREE, list);
37514 get_comma:
37515 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
37516 break;
37517 cp_lexer_consume_token (parser->lexer);
37520 if (colon)
37521 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37523 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
37525 *colon = true;
37526 cp_parser_require (parser, CPP_COLON, RT_COLON);
37527 return list;
37530 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37532 int ending;
37534 /* Try to resync to an unnested comma. Copied from
37535 cp_parser_parenthesized_expression_list. */
37536 skip_comma:
37537 if (colon)
37538 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37539 ending = cp_parser_skip_to_closing_parenthesis (parser,
37540 /*recovering=*/true,
37541 /*or_comma=*/true,
37542 /*consume_paren=*/true);
37543 if (ending < 0)
37544 goto get_comma;
37547 return list;
37550 /* Similarly, but expect leading and trailing parenthesis. This is a very
37551 common case for omp clauses. */
37553 static tree
37554 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list,
37555 bool allow_deref = false)
37557 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37558 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL,
37559 allow_deref);
37560 return list;
37563 /* OpenACC 2.0:
37564 copy ( variable-list )
37565 copyin ( variable-list )
37566 copyout ( variable-list )
37567 create ( variable-list )
37568 delete ( variable-list )
37569 present ( variable-list )
37571 OpenACC 2.6:
37572 no_create ( variable-list )
37573 attach ( variable-list )
37574 detach ( variable-list ) */
37576 static tree
37577 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
37578 tree list)
37580 enum gomp_map_kind kind;
37581 switch (c_kind)
37583 case PRAGMA_OACC_CLAUSE_ATTACH:
37584 kind = GOMP_MAP_ATTACH;
37585 break;
37586 case PRAGMA_OACC_CLAUSE_COPY:
37587 kind = GOMP_MAP_TOFROM;
37588 break;
37589 case PRAGMA_OACC_CLAUSE_COPYIN:
37590 kind = GOMP_MAP_TO;
37591 break;
37592 case PRAGMA_OACC_CLAUSE_COPYOUT:
37593 kind = GOMP_MAP_FROM;
37594 break;
37595 case PRAGMA_OACC_CLAUSE_CREATE:
37596 kind = GOMP_MAP_ALLOC;
37597 break;
37598 case PRAGMA_OACC_CLAUSE_DELETE:
37599 kind = GOMP_MAP_RELEASE;
37600 break;
37601 case PRAGMA_OACC_CLAUSE_DETACH:
37602 kind = GOMP_MAP_DETACH;
37603 break;
37604 case PRAGMA_OACC_CLAUSE_DEVICE:
37605 kind = GOMP_MAP_FORCE_TO;
37606 break;
37607 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
37608 kind = GOMP_MAP_DEVICE_RESIDENT;
37609 break;
37610 case PRAGMA_OACC_CLAUSE_HOST:
37611 kind = GOMP_MAP_FORCE_FROM;
37612 break;
37613 case PRAGMA_OACC_CLAUSE_LINK:
37614 kind = GOMP_MAP_LINK;
37615 break;
37616 case PRAGMA_OACC_CLAUSE_NO_CREATE:
37617 kind = GOMP_MAP_IF_PRESENT;
37618 break;
37619 case PRAGMA_OACC_CLAUSE_PRESENT:
37620 kind = GOMP_MAP_FORCE_PRESENT;
37621 break;
37622 default:
37623 gcc_unreachable ();
37625 tree nl, c;
37626 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list, true);
37628 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
37629 OMP_CLAUSE_SET_MAP_KIND (c, kind);
37631 return nl;
37634 /* OpenACC 2.0:
37635 deviceptr ( variable-list ) */
37637 static tree
37638 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
37640 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37641 tree vars, t;
37643 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
37644 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
37645 variable-list must only allow for pointer variables. */
37646 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
37647 for (t = vars; t; t = TREE_CHAIN (t))
37649 tree v = TREE_PURPOSE (t);
37650 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
37651 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
37652 OMP_CLAUSE_DECL (u) = v;
37653 OMP_CLAUSE_CHAIN (u) = list;
37654 list = u;
37657 return list;
37660 /* OpenACC 2.5:
37661 auto
37662 finalize
37663 independent
37664 nohost
37665 seq */
37667 static tree
37668 cp_parser_oacc_simple_clause (location_t loc, enum omp_clause_code code,
37669 tree list)
37671 check_no_duplicate_clause (list, code, omp_clause_code_name[code], loc);
37673 tree c = build_omp_clause (loc, code);
37674 OMP_CLAUSE_CHAIN (c) = list;
37676 return c;
37679 /* OpenACC:
37680 num_gangs ( expression )
37681 num_workers ( expression )
37682 vector_length ( expression ) */
37684 static tree
37685 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
37686 const char *str, tree list)
37688 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37690 matching_parens parens;
37691 if (!parens.require_open (parser))
37692 return list;
37694 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
37696 if (t == error_mark_node
37697 || !parens.require_close (parser))
37699 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37700 /*or_comma=*/false,
37701 /*consume_paren=*/true);
37702 return list;
37705 check_no_duplicate_clause (list, code, str, loc);
37707 tree c = build_omp_clause (loc, code);
37708 OMP_CLAUSE_OPERAND (c, 0) = t;
37709 OMP_CLAUSE_CHAIN (c) = list;
37710 return c;
37713 /* OpenACC:
37715 gang [( gang-arg-list )]
37716 worker [( [num:] int-expr )]
37717 vector [( [length:] int-expr )]
37719 where gang-arg is one of:
37721 [num:] int-expr
37722 static: size-expr
37724 and size-expr may be:
37727 int-expr
37730 static tree
37731 cp_parser_oacc_shape_clause (cp_parser *parser, location_t loc,
37732 omp_clause_code kind,
37733 const char *str, tree list)
37735 const char *id = "num";
37736 cp_lexer *lexer = parser->lexer;
37737 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
37739 if (kind == OMP_CLAUSE_VECTOR)
37740 id = "length";
37742 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
37744 matching_parens parens;
37745 parens.consume_open (parser);
37749 cp_token *next = cp_lexer_peek_token (lexer);
37750 int idx = 0;
37752 /* Gang static argument. */
37753 if (kind == OMP_CLAUSE_GANG
37754 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
37756 cp_lexer_consume_token (lexer);
37758 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37759 goto cleanup_error;
37761 idx = 1;
37762 if (ops[idx] != NULL)
37764 cp_parser_error (parser, "too many %<static%> arguments");
37765 goto cleanup_error;
37768 /* Check for the '*' argument. */
37769 if (cp_lexer_next_token_is (lexer, CPP_MULT)
37770 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
37771 || cp_lexer_nth_token_is (parser->lexer, 2,
37772 CPP_CLOSE_PAREN)))
37774 cp_lexer_consume_token (lexer);
37775 ops[idx] = integer_minus_one_node;
37777 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
37779 cp_lexer_consume_token (lexer);
37780 continue;
37782 else break;
37785 /* Worker num: argument and vector length: arguments. */
37786 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
37787 && id_equal (next->u.value, id)
37788 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
37790 cp_lexer_consume_token (lexer); /* id */
37791 cp_lexer_consume_token (lexer); /* ':' */
37794 /* Now collect the actual argument. */
37795 if (ops[idx] != NULL_TREE)
37797 cp_parser_error (parser, "unexpected argument");
37798 goto cleanup_error;
37801 tree expr = cp_parser_assignment_expression (parser, NULL, false,
37802 false);
37803 if (expr == error_mark_node)
37804 goto cleanup_error;
37806 mark_exp_read (expr);
37807 ops[idx] = expr;
37809 if (kind == OMP_CLAUSE_GANG
37810 && cp_lexer_next_token_is (lexer, CPP_COMMA))
37812 cp_lexer_consume_token (lexer);
37813 continue;
37815 break;
37817 while (1);
37819 if (!parens.require_close (parser))
37820 goto cleanup_error;
37823 check_no_duplicate_clause (list, kind, str, loc);
37825 c = build_omp_clause (loc, kind);
37827 if (ops[1])
37828 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
37830 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
37831 OMP_CLAUSE_CHAIN (c) = list;
37833 return c;
37835 cleanup_error:
37836 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
37837 return list;
37840 /* OpenACC 2.0:
37841 tile ( size-expr-list ) */
37843 static tree
37844 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
37846 tree c, expr = error_mark_node;
37847 tree tile = NULL_TREE;
37849 /* Collapse and tile are mutually exclusive. (The spec doesn't say
37850 so, but the spec authors never considered such a case and have
37851 differing opinions on what it might mean, including 'not
37852 allowed'.) */
37853 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
37854 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
37855 clause_loc);
37857 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37858 return list;
37862 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
37863 return list;
37865 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
37866 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
37867 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
37869 cp_lexer_consume_token (parser->lexer);
37870 expr = integer_zero_node;
37872 else
37873 expr = cp_parser_constant_expression (parser);
37875 tile = tree_cons (NULL_TREE, expr, tile);
37877 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
37879 /* Consume the trailing ')'. */
37880 cp_lexer_consume_token (parser->lexer);
37882 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
37883 tile = nreverse (tile);
37884 OMP_CLAUSE_TILE_LIST (c) = tile;
37885 OMP_CLAUSE_CHAIN (c) = list;
37886 return c;
37889 /* OpenACC 2.0
37890 Parse wait clause or directive parameters. */
37892 static tree
37893 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
37895 vec<tree, va_gc> *args;
37896 tree t, args_tree;
37898 args = cp_parser_parenthesized_expression_list (parser, non_attr,
37899 /*cast_p=*/false,
37900 /*allow_expansion_p=*/true,
37901 /*non_constant_p=*/NULL);
37903 if (args == NULL || args->length () == 0)
37905 if (args != NULL)
37907 cp_parser_error (parser, "expected integer expression list");
37908 release_tree_vector (args);
37910 return list;
37913 args_tree = build_tree_list_vec (args);
37915 release_tree_vector (args);
37917 for (t = args_tree; t; t = TREE_CHAIN (t))
37919 tree targ = TREE_VALUE (t);
37921 if (targ != error_mark_node)
37923 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
37924 error ("%<wait%> expression must be integral");
37925 else
37927 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
37929 targ = mark_rvalue_use (targ);
37930 OMP_CLAUSE_DECL (c) = targ;
37931 OMP_CLAUSE_CHAIN (c) = list;
37932 list = c;
37937 return list;
37940 /* OpenACC:
37941 wait [( int-expr-list )] */
37943 static tree
37944 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
37946 location_t location = cp_lexer_peek_token (parser->lexer)->location;
37948 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
37949 list = cp_parser_oacc_wait_list (parser, location, list);
37950 else
37952 tree c = build_omp_clause (location, OMP_CLAUSE_WAIT);
37954 OMP_CLAUSE_DECL (c) = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
37955 OMP_CLAUSE_CHAIN (c) = list;
37956 list = c;
37959 return list;
37962 /* OpenMP 3.0:
37963 collapse ( constant-expression ) */
37965 static tree
37966 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
37968 tree c, num;
37969 location_t loc;
37970 HOST_WIDE_INT n;
37972 loc = cp_lexer_peek_token (parser->lexer)->location;
37973 matching_parens parens;
37974 if (!parens.require_open (parser))
37975 return list;
37977 num = cp_parser_constant_expression (parser);
37979 if (!parens.require_close (parser))
37980 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37981 /*or_comma=*/false,
37982 /*consume_paren=*/true);
37984 if (num == error_mark_node)
37985 return list;
37986 num = fold_non_dependent_expr (num);
37987 if (!tree_fits_shwi_p (num)
37988 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
37989 || (n = tree_to_shwi (num)) <= 0
37990 || (int) n != n)
37992 error_at (loc, "collapse argument needs positive constant integer expression");
37993 return list;
37996 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
37997 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
37998 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
37999 OMP_CLAUSE_CHAIN (c) = list;
38000 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
38002 return c;
38005 /* OpenMP 2.5:
38006 default ( none | shared )
38008 OpenMP 5.1:
38009 default ( private | firstprivate )
38011 OpenACC:
38012 default ( none | present ) */
38014 static tree
38015 cp_parser_omp_clause_default (cp_parser *parser, tree list,
38016 location_t location, bool is_oacc)
38018 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
38019 tree c;
38021 matching_parens parens;
38022 if (!parens.require_open (parser))
38023 return list;
38024 if (!is_oacc && cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
38026 kind = OMP_CLAUSE_DEFAULT_PRIVATE;
38027 cp_lexer_consume_token (parser->lexer);
38029 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38031 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38032 const char *p = IDENTIFIER_POINTER (id);
38034 switch (p[0])
38036 case 'n':
38037 if (strcmp ("none", p) != 0)
38038 goto invalid_kind;
38039 kind = OMP_CLAUSE_DEFAULT_NONE;
38040 break;
38042 case 'p':
38043 if (strcmp ("present", p) != 0 || !is_oacc)
38044 goto invalid_kind;
38045 kind = OMP_CLAUSE_DEFAULT_PRESENT;
38046 break;
38048 case 'f':
38049 if (strcmp ("firstprivate", p) != 0 || is_oacc)
38050 goto invalid_kind;
38051 kind = OMP_CLAUSE_DEFAULT_FIRSTPRIVATE;
38052 break;
38054 case 's':
38055 if (strcmp ("shared", p) != 0 || is_oacc)
38056 goto invalid_kind;
38057 kind = OMP_CLAUSE_DEFAULT_SHARED;
38058 break;
38060 default:
38061 goto invalid_kind;
38064 cp_lexer_consume_token (parser->lexer);
38066 else
38068 invalid_kind:
38069 if (is_oacc)
38070 cp_parser_error (parser, "expected %<none%> or %<present%>");
38071 else
38072 cp_parser_error (parser, "expected %<none%>, %<shared%>, "
38073 "%<private%> or %<firstprivate%>");
38076 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
38077 || !parens.require_close (parser))
38078 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38079 /*or_comma=*/false,
38080 /*consume_paren=*/true);
38082 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
38083 return list;
38085 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
38086 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
38087 OMP_CLAUSE_CHAIN (c) = list;
38088 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
38090 return c;
38093 /* OpenMP 3.1:
38094 final ( expression ) */
38096 static tree
38097 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
38099 tree t, c;
38101 matching_parens parens;
38102 if (!parens.require_open (parser))
38103 return list;
38105 t = cp_parser_assignment_expression (parser);
38107 if (t == error_mark_node
38108 || !parens.require_close (parser))
38109 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38110 /*or_comma=*/false,
38111 /*consume_paren=*/true);
38113 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
38115 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
38116 OMP_CLAUSE_FINAL_EXPR (c) = t;
38117 OMP_CLAUSE_CHAIN (c) = list;
38119 return c;
38122 /* OpenMP 2.5:
38123 if ( expression )
38125 OpenMP 4.5:
38126 if ( directive-name-modifier : expression )
38128 directive-name-modifier:
38129 parallel | task | taskloop | target data | target | target update
38130 | target enter data | target exit data
38132 OpenMP 5.0:
38133 directive-name-modifier:
38134 ... | simd | cancel */
38136 static tree
38137 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
38138 bool is_omp)
38140 tree t, c;
38141 enum tree_code if_modifier = ERROR_MARK;
38143 matching_parens parens;
38144 if (!parens.require_open (parser))
38145 return list;
38147 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38149 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38150 const char *p = IDENTIFIER_POINTER (id);
38151 int n = 2;
38153 if (strcmp ("cancel", p) == 0)
38154 if_modifier = VOID_CST;
38155 else if (strcmp ("parallel", p) == 0)
38156 if_modifier = OMP_PARALLEL;
38157 else if (strcmp ("simd", p) == 0)
38158 if_modifier = OMP_SIMD;
38159 else if (strcmp ("task", p) == 0)
38160 if_modifier = OMP_TASK;
38161 else if (strcmp ("taskloop", p) == 0)
38162 if_modifier = OMP_TASKLOOP;
38163 else if (strcmp ("target", p) == 0)
38165 if_modifier = OMP_TARGET;
38166 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
38168 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
38169 p = IDENTIFIER_POINTER (id);
38170 if (strcmp ("data", p) == 0)
38171 if_modifier = OMP_TARGET_DATA;
38172 else if (strcmp ("update", p) == 0)
38173 if_modifier = OMP_TARGET_UPDATE;
38174 else if (strcmp ("enter", p) == 0)
38175 if_modifier = OMP_TARGET_ENTER_DATA;
38176 else if (strcmp ("exit", p) == 0)
38177 if_modifier = OMP_TARGET_EXIT_DATA;
38178 if (if_modifier != OMP_TARGET)
38179 n = 3;
38180 else
38182 location_t loc
38183 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
38184 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
38185 "or %<exit%>");
38186 if_modifier = ERROR_MARK;
38188 if (if_modifier == OMP_TARGET_ENTER_DATA
38189 || if_modifier == OMP_TARGET_EXIT_DATA)
38191 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
38193 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
38194 p = IDENTIFIER_POINTER (id);
38195 if (strcmp ("data", p) == 0)
38196 n = 4;
38198 if (n != 4)
38200 location_t loc
38201 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
38202 error_at (loc, "expected %<data%>");
38203 if_modifier = ERROR_MARK;
38208 if (if_modifier != ERROR_MARK)
38210 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
38212 while (n-- > 0)
38213 cp_lexer_consume_token (parser->lexer);
38215 else
38217 if (n > 2)
38219 location_t loc
38220 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
38221 error_at (loc, "expected %<:%>");
38223 if_modifier = ERROR_MARK;
38228 t = cp_parser_assignment_expression (parser);
38230 if (t == error_mark_node
38231 || !parens.require_close (parser))
38232 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38233 /*or_comma=*/false,
38234 /*consume_paren=*/true);
38236 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
38237 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
38239 if (if_modifier != ERROR_MARK
38240 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
38242 const char *p = NULL;
38243 switch (if_modifier)
38245 case VOID_CST: p = "cancel"; break;
38246 case OMP_PARALLEL: p = "parallel"; break;
38247 case OMP_SIMD: p = "simd"; break;
38248 case OMP_TASK: p = "task"; break;
38249 case OMP_TASKLOOP: p = "taskloop"; break;
38250 case OMP_TARGET_DATA: p = "target data"; break;
38251 case OMP_TARGET: p = "target"; break;
38252 case OMP_TARGET_UPDATE: p = "target update"; break;
38253 case OMP_TARGET_ENTER_DATA: p = "target enter data"; break;
38254 case OMP_TARGET_EXIT_DATA: p = "target exit data"; break;
38255 default: gcc_unreachable ();
38257 error_at (location, "too many %<if%> clauses with %qs modifier",
38259 return list;
38261 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
38263 if (!is_omp)
38264 error_at (location, "too many %<if%> clauses");
38265 else
38266 error_at (location, "too many %<if%> clauses without modifier");
38267 return list;
38269 else if (if_modifier == ERROR_MARK
38270 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
38272 error_at (location, "if any %<if%> clause has modifier, then all "
38273 "%<if%> clauses have to use modifier");
38274 return list;
38278 c = build_omp_clause (location, OMP_CLAUSE_IF);
38279 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
38280 OMP_CLAUSE_IF_EXPR (c) = t;
38281 OMP_CLAUSE_CHAIN (c) = list;
38283 return c;
38286 /* OpenMP 3.1:
38287 mergeable */
38289 static tree
38290 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
38291 tree list, location_t location)
38293 tree c;
38295 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
38296 location);
38298 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
38299 OMP_CLAUSE_CHAIN (c) = list;
38300 return c;
38303 /* OpenMP 2.5:
38304 nowait */
38306 static tree
38307 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
38308 tree list, location_t location)
38310 tree c;
38312 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
38314 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
38315 OMP_CLAUSE_CHAIN (c) = list;
38316 return c;
38319 /* OpenMP 2.5:
38320 num_threads ( expression ) */
38322 static tree
38323 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
38324 location_t location)
38326 tree t, c;
38328 matching_parens parens;
38329 if (!parens.require_open (parser))
38330 return list;
38332 t = cp_parser_assignment_expression (parser);
38334 if (t == error_mark_node
38335 || !parens.require_close (parser))
38336 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38337 /*or_comma=*/false,
38338 /*consume_paren=*/true);
38340 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
38341 "num_threads", location);
38343 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
38344 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
38345 OMP_CLAUSE_CHAIN (c) = list;
38347 return c;
38350 /* OpenMP 4.5:
38351 num_tasks ( expression )
38353 OpenMP 5.1:
38354 num_tasks ( strict : expression ) */
38356 static tree
38357 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
38358 location_t location)
38360 tree t, c;
38362 matching_parens parens;
38363 if (!parens.require_open (parser))
38364 return list;
38366 bool strict = false;
38367 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
38368 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
38370 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38371 if (!strcmp (IDENTIFIER_POINTER (id), "strict"))
38373 strict = true;
38374 cp_lexer_consume_token (parser->lexer);
38375 cp_lexer_consume_token (parser->lexer);
38379 t = cp_parser_assignment_expression (parser);
38381 if (t == error_mark_node
38382 || !parens.require_close (parser))
38383 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38384 /*or_comma=*/false,
38385 /*consume_paren=*/true);
38387 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
38388 "num_tasks", location);
38390 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
38391 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
38392 OMP_CLAUSE_NUM_TASKS_STRICT (c) = strict;
38393 OMP_CLAUSE_CHAIN (c) = list;
38395 return c;
38398 /* OpenMP 4.5:
38399 grainsize ( expression )
38401 OpenMP 5.1:
38402 grainsize ( strict : expression ) */
38404 static tree
38405 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
38406 location_t location)
38408 tree t, c;
38410 matching_parens parens;
38411 if (!parens.require_open (parser))
38412 return list;
38414 bool strict = false;
38415 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
38416 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
38418 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38419 if (!strcmp (IDENTIFIER_POINTER (id), "strict"))
38421 strict = true;
38422 cp_lexer_consume_token (parser->lexer);
38423 cp_lexer_consume_token (parser->lexer);
38427 t = cp_parser_assignment_expression (parser);
38429 if (t == error_mark_node
38430 || !parens.require_close (parser))
38431 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38432 /*or_comma=*/false,
38433 /*consume_paren=*/true);
38435 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
38436 "grainsize", location);
38438 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
38439 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
38440 OMP_CLAUSE_GRAINSIZE_STRICT (c) = strict;
38441 OMP_CLAUSE_CHAIN (c) = list;
38443 return c;
38446 /* OpenMP 4.5:
38447 priority ( expression ) */
38449 static tree
38450 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
38451 location_t location)
38453 tree t, c;
38455 matching_parens parens;
38456 if (!parens.require_open (parser))
38457 return list;
38459 t = cp_parser_assignment_expression (parser);
38461 if (t == error_mark_node
38462 || !parens.require_close (parser))
38463 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38464 /*or_comma=*/false,
38465 /*consume_paren=*/true);
38467 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
38468 "priority", location);
38470 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
38471 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
38472 OMP_CLAUSE_CHAIN (c) = list;
38474 return c;
38477 /* OpenMP 4.5:
38478 hint ( expression ) */
38480 static tree
38481 cp_parser_omp_clause_hint (cp_parser *parser, tree list, location_t location)
38483 tree t, c;
38485 matching_parens parens;
38486 if (!parens.require_open (parser))
38487 return list;
38489 t = cp_parser_assignment_expression (parser);
38491 if (t != error_mark_node)
38493 t = fold_non_dependent_expr (t);
38494 if (!value_dependent_expression_p (t)
38495 && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
38496 || !tree_fits_shwi_p (t)
38497 || tree_int_cst_sgn (t) == -1))
38498 error_at (location, "expected constant integer expression with "
38499 "valid sync-hint value");
38501 if (t == error_mark_node
38502 || !parens.require_close (parser))
38503 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38504 /*or_comma=*/false,
38505 /*consume_paren=*/true);
38506 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
38508 c = build_omp_clause (location, OMP_CLAUSE_HINT);
38509 OMP_CLAUSE_HINT_EXPR (c) = t;
38510 OMP_CLAUSE_CHAIN (c) = list;
38512 return c;
38515 /* OpenMP 5.1:
38516 filter ( integer-expression ) */
38518 static tree
38519 cp_parser_omp_clause_filter (cp_parser *parser, tree list, location_t location)
38521 tree t, c;
38523 matching_parens parens;
38524 if (!parens.require_open (parser))
38525 return list;
38527 t = cp_parser_assignment_expression (parser);
38529 if (t == error_mark_node
38530 || !parens.require_close (parser))
38531 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38532 /*or_comma=*/false,
38533 /*consume_paren=*/true);
38534 check_no_duplicate_clause (list, OMP_CLAUSE_FILTER, "filter", location);
38536 c = build_omp_clause (location, OMP_CLAUSE_FILTER);
38537 OMP_CLAUSE_FILTER_EXPR (c) = t;
38538 OMP_CLAUSE_CHAIN (c) = list;
38540 return c;
38543 /* OpenMP 4.5:
38544 defaultmap ( tofrom : scalar )
38546 OpenMP 5.0:
38547 defaultmap ( implicit-behavior [ : variable-category ] ) */
38549 static tree
38550 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
38551 location_t location)
38553 tree c, id;
38554 const char *p;
38555 enum omp_clause_defaultmap_kind behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
38556 enum omp_clause_defaultmap_kind category
38557 = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED;
38559 matching_parens parens;
38560 if (!parens.require_open (parser))
38561 return list;
38563 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
38564 p = "default";
38565 else if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38567 invalid_behavior:
38568 cp_parser_error (parser, "expected %<alloc%>, %<to%>, %<from%>, "
38569 "%<tofrom%>, %<firstprivate%>, %<none%> "
38570 "or %<default%>");
38571 goto out_err;
38573 else
38575 id = cp_lexer_peek_token (parser->lexer)->u.value;
38576 p = IDENTIFIER_POINTER (id);
38579 switch (p[0])
38581 case 'a':
38582 if (strcmp ("alloc", p) == 0)
38583 behavior = OMP_CLAUSE_DEFAULTMAP_ALLOC;
38584 else
38585 goto invalid_behavior;
38586 break;
38588 case 'd':
38589 if (strcmp ("default", p) == 0)
38590 behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
38591 else
38592 goto invalid_behavior;
38593 break;
38595 case 'f':
38596 if (strcmp ("firstprivate", p) == 0)
38597 behavior = OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE;
38598 else if (strcmp ("from", p) == 0)
38599 behavior = OMP_CLAUSE_DEFAULTMAP_FROM;
38600 else
38601 goto invalid_behavior;
38602 break;
38604 case 'n':
38605 if (strcmp ("none", p) == 0)
38606 behavior = OMP_CLAUSE_DEFAULTMAP_NONE;
38607 else
38608 goto invalid_behavior;
38609 break;
38611 case 't':
38612 if (strcmp ("tofrom", p) == 0)
38613 behavior = OMP_CLAUSE_DEFAULTMAP_TOFROM;
38614 else if (strcmp ("to", p) == 0)
38615 behavior = OMP_CLAUSE_DEFAULTMAP_TO;
38616 else
38617 goto invalid_behavior;
38618 break;
38620 default:
38621 goto invalid_behavior;
38623 cp_lexer_consume_token (parser->lexer);
38625 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
38627 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
38628 goto out_err;
38630 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38632 invalid_category:
38633 cp_parser_error (parser, "expected %<scalar%>, %<aggregate%> or "
38634 "%<pointer%>");
38635 goto out_err;
38637 id = cp_lexer_peek_token (parser->lexer)->u.value;
38638 p = IDENTIFIER_POINTER (id);
38640 switch (p[0])
38642 case 'a':
38643 if (strcmp ("aggregate", p) == 0)
38644 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE;
38645 else
38646 goto invalid_category;
38647 break;
38649 case 'p':
38650 if (strcmp ("pointer", p) == 0)
38651 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER;
38652 else
38653 goto invalid_category;
38654 break;
38656 case 's':
38657 if (strcmp ("scalar", p) == 0)
38658 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR;
38659 else
38660 goto invalid_category;
38661 break;
38663 default:
38664 goto invalid_category;
38667 cp_lexer_consume_token (parser->lexer);
38669 if (!parens.require_close (parser))
38670 goto out_err;
38672 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
38673 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEFAULTMAP
38674 && (category == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
38675 || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c) == category
38676 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c)
38677 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)))
38679 enum omp_clause_defaultmap_kind cat = category;
38680 location_t loc = OMP_CLAUSE_LOCATION (c);
38681 if (cat == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)
38682 cat = OMP_CLAUSE_DEFAULTMAP_CATEGORY (c);
38683 p = NULL;
38684 switch (cat)
38686 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED:
38687 p = NULL;
38688 break;
38689 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE:
38690 p = "aggregate";
38691 break;
38692 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER:
38693 p = "pointer";
38694 break;
38695 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR:
38696 p = "scalar";
38697 break;
38698 default:
38699 gcc_unreachable ();
38701 if (p)
38702 error_at (loc, "too many %<defaultmap%> clauses with %qs category",
38704 else
38705 error_at (loc, "too many %<defaultmap%> clauses with unspecified "
38706 "category");
38707 break;
38710 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
38711 OMP_CLAUSE_DEFAULTMAP_SET_KIND (c, behavior, category);
38712 OMP_CLAUSE_CHAIN (c) = list;
38713 return c;
38715 out_err:
38716 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38717 /*or_comma=*/false,
38718 /*consume_paren=*/true);
38719 return list;
38722 /* OpenMP 5.0:
38723 order ( concurrent )
38725 OpenMP 5.1:
38726 order ( order-modifier : concurrent )
38728 order-modifier:
38729 reproducible
38730 unconstrained */
38732 static tree
38733 cp_parser_omp_clause_order (cp_parser *parser, tree list, location_t location)
38735 tree c, id;
38736 const char *p;
38737 bool unconstrained = false;
38738 bool reproducible = false;
38740 matching_parens parens;
38741 if (!parens.require_open (parser))
38742 return list;
38744 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
38745 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
38747 id = cp_lexer_peek_token (parser->lexer)->u.value;
38748 p = IDENTIFIER_POINTER (id);
38749 if (strcmp (p, "unconstrained") == 0)
38750 unconstrained = true;
38751 else if (strcmp (p, "reproducible") == 0)
38752 reproducible = true;
38753 else
38755 cp_parser_error (parser, "expected %<reproducible%> or "
38756 "%<unconstrained%>");
38757 goto out_err;
38759 cp_lexer_consume_token (parser->lexer);
38760 cp_lexer_consume_token (parser->lexer);
38762 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38764 cp_parser_error (parser, "expected %<concurrent%>");
38765 goto out_err;
38767 else
38769 id = cp_lexer_peek_token (parser->lexer)->u.value;
38770 p = IDENTIFIER_POINTER (id);
38772 if (strcmp (p, "concurrent") != 0)
38774 cp_parser_error (parser, "expected %<concurrent%>");
38775 goto out_err;
38777 cp_lexer_consume_token (parser->lexer);
38778 if (!parens.require_close (parser))
38779 goto out_err;
38781 check_no_duplicate_clause (list, OMP_CLAUSE_ORDER, "order", location);
38782 c = build_omp_clause (location, OMP_CLAUSE_ORDER);
38783 OMP_CLAUSE_ORDER_UNCONSTRAINED (c) = unconstrained;
38784 OMP_CLAUSE_ORDER_REPRODUCIBLE (c) = reproducible;
38785 OMP_CLAUSE_CHAIN (c) = list;
38786 return c;
38788 out_err:
38789 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38790 /*or_comma=*/false,
38791 /*consume_paren=*/true);
38792 return list;
38795 /* OpenMP 5.0:
38796 bind ( teams | parallel | thread ) */
38798 static tree
38799 cp_parser_omp_clause_bind (cp_parser *parser, tree list,
38800 location_t location)
38802 tree c;
38803 const char *p;
38804 enum omp_clause_bind_kind kind = OMP_CLAUSE_BIND_THREAD;
38806 matching_parens parens;
38807 if (!parens.require_open (parser))
38808 return list;
38810 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38812 invalid:
38813 cp_parser_error (parser,
38814 "expected %<teams%>, %<parallel%> or %<thread%>");
38815 goto out_err;
38817 else
38819 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38820 p = IDENTIFIER_POINTER (id);
38822 if (strcmp (p, "teams") == 0)
38823 kind = OMP_CLAUSE_BIND_TEAMS;
38824 else if (strcmp (p, "parallel") == 0)
38825 kind = OMP_CLAUSE_BIND_PARALLEL;
38826 else if (strcmp (p, "thread") != 0)
38827 goto invalid;
38828 cp_lexer_consume_token (parser->lexer);
38829 if (!parens.require_close (parser))
38830 goto out_err;
38832 /* check_no_duplicate_clause (list, OMP_CLAUSE_BIND, "bind", location); */
38833 c = build_omp_clause (location, OMP_CLAUSE_BIND);
38834 OMP_CLAUSE_BIND_KIND (c) = kind;
38835 OMP_CLAUSE_CHAIN (c) = list;
38836 return c;
38838 out_err:
38839 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38840 /*or_comma=*/false,
38841 /*consume_paren=*/true);
38842 return list;
38845 /* OpenMP 2.5:
38846 ordered
38848 OpenMP 4.5:
38849 ordered ( constant-expression ) */
38851 static tree
38852 cp_parser_omp_clause_ordered (cp_parser *parser,
38853 tree list, location_t location)
38855 tree c, num = NULL_TREE;
38856 HOST_WIDE_INT n;
38858 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
38859 "ordered", location);
38861 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
38863 matching_parens parens;
38864 parens.consume_open (parser);
38866 num = cp_parser_constant_expression (parser);
38868 if (!parens.require_close (parser))
38869 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38870 /*or_comma=*/false,
38871 /*consume_paren=*/true);
38873 if (num == error_mark_node)
38874 return list;
38875 num = fold_non_dependent_expr (num);
38876 if (!tree_fits_shwi_p (num)
38877 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
38878 || (n = tree_to_shwi (num)) <= 0
38879 || (int) n != n)
38881 error_at (location,
38882 "ordered argument needs positive constant integer "
38883 "expression");
38884 return list;
38888 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
38889 OMP_CLAUSE_ORDERED_EXPR (c) = num;
38890 OMP_CLAUSE_CHAIN (c) = list;
38891 return c;
38894 /* OpenMP 2.5:
38895 reduction ( reduction-operator : variable-list )
38897 reduction-operator:
38898 One of: + * - & ^ | && ||
38900 OpenMP 3.1:
38902 reduction-operator:
38903 One of: + * - & ^ | && || min max
38905 OpenMP 4.0:
38907 reduction-operator:
38908 One of: + * - & ^ | && ||
38909 id-expression
38911 OpenMP 5.0:
38912 reduction ( reduction-modifier, reduction-operator : variable-list )
38913 in_reduction ( reduction-operator : variable-list )
38914 task_reduction ( reduction-operator : variable-list ) */
38916 static tree
38917 cp_parser_omp_clause_reduction (cp_parser *parser, enum omp_clause_code kind,
38918 bool is_omp, tree list)
38920 enum tree_code code = ERROR_MARK;
38921 tree nlist, c, id = NULL_TREE;
38922 bool task = false;
38923 bool inscan = false;
38925 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38926 return list;
38928 if (kind == OMP_CLAUSE_REDUCTION && is_omp)
38930 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT)
38931 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
38933 cp_lexer_consume_token (parser->lexer);
38934 cp_lexer_consume_token (parser->lexer);
38936 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
38937 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
38939 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38940 const char *p = IDENTIFIER_POINTER (id);
38941 if (strcmp (p, "task") == 0)
38942 task = true;
38943 else if (strcmp (p, "inscan") == 0)
38944 inscan = true;
38945 if (task || inscan)
38947 cp_lexer_consume_token (parser->lexer);
38948 cp_lexer_consume_token (parser->lexer);
38953 switch (cp_lexer_peek_token (parser->lexer)->type)
38955 case CPP_PLUS: code = PLUS_EXPR; break;
38956 case CPP_MULT: code = MULT_EXPR; break;
38957 case CPP_MINUS: code = MINUS_EXPR; break;
38958 case CPP_AND: code = BIT_AND_EXPR; break;
38959 case CPP_XOR: code = BIT_XOR_EXPR; break;
38960 case CPP_OR: code = BIT_IOR_EXPR; break;
38961 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
38962 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
38963 default: break;
38966 if (code != ERROR_MARK)
38967 cp_lexer_consume_token (parser->lexer);
38968 else
38970 bool saved_colon_corrects_to_scope_p;
38971 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
38972 parser->colon_corrects_to_scope_p = false;
38973 id = cp_parser_id_expression (parser, /*template_p=*/false,
38974 /*check_dependency_p=*/true,
38975 /*template_p=*/NULL,
38976 /*declarator_p=*/false,
38977 /*optional_p=*/false);
38978 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
38979 if (identifier_p (id))
38981 const char *p = IDENTIFIER_POINTER (id);
38983 if (strcmp (p, "min") == 0)
38984 code = MIN_EXPR;
38985 else if (strcmp (p, "max") == 0)
38986 code = MAX_EXPR;
38987 else if (id == ovl_op_identifier (false, PLUS_EXPR))
38988 code = PLUS_EXPR;
38989 else if (id == ovl_op_identifier (false, MULT_EXPR))
38990 code = MULT_EXPR;
38991 else if (id == ovl_op_identifier (false, MINUS_EXPR))
38992 code = MINUS_EXPR;
38993 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
38994 code = BIT_AND_EXPR;
38995 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
38996 code = BIT_IOR_EXPR;
38997 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
38998 code = BIT_XOR_EXPR;
38999 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
39000 code = TRUTH_ANDIF_EXPR;
39001 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
39002 code = TRUTH_ORIF_EXPR;
39003 id = omp_reduction_id (code, id, NULL_TREE);
39004 tree scope = parser->scope;
39005 if (scope)
39006 id = build_qualified_name (NULL_TREE, scope, id, false);
39007 parser->scope = NULL_TREE;
39008 parser->qualifying_scope = NULL_TREE;
39009 parser->object_scope = NULL_TREE;
39011 else
39013 error ("invalid reduction-identifier");
39014 resync_fail:
39015 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39016 /*or_comma=*/false,
39017 /*consume_paren=*/true);
39018 return list;
39022 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
39023 goto resync_fail;
39025 nlist = cp_parser_omp_var_list_no_open (parser, kind, list,
39026 NULL);
39027 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
39029 OMP_CLAUSE_REDUCTION_CODE (c) = code;
39030 if (task)
39031 OMP_CLAUSE_REDUCTION_TASK (c) = 1;
39032 else if (inscan)
39033 OMP_CLAUSE_REDUCTION_INSCAN (c) = 1;
39034 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
39037 return nlist;
39040 /* OpenMP 2.5:
39041 schedule ( schedule-kind )
39042 schedule ( schedule-kind , expression )
39044 schedule-kind:
39045 static | dynamic | guided | runtime | auto
39047 OpenMP 4.5:
39048 schedule ( schedule-modifier : schedule-kind )
39049 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
39051 schedule-modifier:
39052 simd
39053 monotonic
39054 nonmonotonic */
39056 static tree
39057 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
39059 tree c, t;
39060 int modifiers = 0, nmodifiers = 0;
39062 matching_parens parens;
39063 if (!parens.require_open (parser))
39064 return list;
39066 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
39068 location_t comma = UNKNOWN_LOCATION;
39069 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39071 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39072 const char *p = IDENTIFIER_POINTER (id);
39073 if (strcmp ("simd", p) == 0)
39074 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
39075 else if (strcmp ("monotonic", p) == 0)
39076 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
39077 else if (strcmp ("nonmonotonic", p) == 0)
39078 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
39079 else
39080 break;
39081 comma = UNKNOWN_LOCATION;
39082 cp_lexer_consume_token (parser->lexer);
39083 if (nmodifiers++ == 0
39084 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39086 comma = cp_lexer_peek_token (parser->lexer)->location;
39087 cp_lexer_consume_token (parser->lexer);
39089 else
39091 cp_parser_require (parser, CPP_COLON, RT_COLON);
39092 break;
39095 if (comma != UNKNOWN_LOCATION)
39096 error_at (comma, "expected %<:%>");
39098 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39100 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39101 const char *p = IDENTIFIER_POINTER (id);
39103 switch (p[0])
39105 case 'd':
39106 if (strcmp ("dynamic", p) != 0)
39107 goto invalid_kind;
39108 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
39109 break;
39111 case 'g':
39112 if (strcmp ("guided", p) != 0)
39113 goto invalid_kind;
39114 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
39115 break;
39117 case 'r':
39118 if (strcmp ("runtime", p) != 0)
39119 goto invalid_kind;
39120 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
39121 break;
39123 default:
39124 goto invalid_kind;
39127 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
39128 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
39129 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
39130 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
39131 else
39132 goto invalid_kind;
39133 cp_lexer_consume_token (parser->lexer);
39135 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
39136 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
39137 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
39138 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
39140 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
39141 "specified");
39142 modifiers = 0;
39145 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39147 cp_token *token;
39148 cp_lexer_consume_token (parser->lexer);
39150 token = cp_lexer_peek_token (parser->lexer);
39151 t = cp_parser_assignment_expression (parser);
39153 if (t == error_mark_node)
39154 goto resync_fail;
39155 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
39156 error_at (token->location, "schedule %<runtime%> does not take "
39157 "a %<chunk_size%> parameter");
39158 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
39159 error_at (token->location, "schedule %<auto%> does not take "
39160 "a %<chunk_size%> parameter");
39161 else
39162 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
39164 if (!parens.require_close (parser))
39165 goto resync_fail;
39167 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
39168 goto resync_fail;
39170 OMP_CLAUSE_SCHEDULE_KIND (c)
39171 = (enum omp_clause_schedule_kind)
39172 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
39174 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
39175 OMP_CLAUSE_CHAIN (c) = list;
39176 return c;
39178 invalid_kind:
39179 cp_parser_error (parser, "invalid schedule kind");
39180 resync_fail:
39181 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39182 /*or_comma=*/false,
39183 /*consume_paren=*/true);
39184 return list;
39187 /* OpenMP 3.0:
39188 untied */
39190 static tree
39191 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
39192 tree list, location_t location)
39194 tree c;
39196 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
39198 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
39199 OMP_CLAUSE_CHAIN (c) = list;
39200 return c;
39203 /* OpenMP 4.0:
39204 inbranch
39205 notinbranch */
39207 static tree
39208 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
39209 tree list, location_t location)
39211 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
39212 tree c = build_omp_clause (location, code);
39213 OMP_CLAUSE_CHAIN (c) = list;
39214 return c;
39217 /* OpenMP 4.0:
39218 parallel
39220 sections
39221 taskgroup */
39223 static tree
39224 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
39225 enum omp_clause_code code,
39226 tree list, location_t location)
39228 tree c = build_omp_clause (location, code);
39229 OMP_CLAUSE_CHAIN (c) = list;
39230 return c;
39233 /* OpenMP 4.5:
39234 nogroup */
39236 static tree
39237 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
39238 tree list, location_t location)
39240 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
39241 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
39242 OMP_CLAUSE_CHAIN (c) = list;
39243 return c;
39246 /* OpenMP 4.5:
39247 simd
39248 threads */
39250 static tree
39251 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
39252 enum omp_clause_code code,
39253 tree list, location_t location)
39255 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
39256 tree c = build_omp_clause (location, code);
39257 OMP_CLAUSE_CHAIN (c) = list;
39258 return c;
39261 /* OpenMP 4.0:
39262 num_teams ( expression )
39264 OpenMP 5.1:
39265 num_teams ( expression : expression ) */
39267 static tree
39268 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
39269 location_t location)
39271 tree upper, lower = NULL_TREE, c;
39273 matching_parens parens;
39274 if (!parens.require_open (parser))
39275 return list;
39277 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
39278 parser->colon_corrects_to_scope_p = false;
39279 upper = cp_parser_assignment_expression (parser);
39280 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
39282 if (upper != error_mark_node
39283 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
39285 lower = upper;
39286 cp_lexer_consume_token (parser->lexer);
39287 upper = cp_parser_assignment_expression (parser);
39290 if (upper == error_mark_node
39291 || !parens.require_close (parser))
39292 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39293 /*or_comma=*/false,
39294 /*consume_paren=*/true);
39296 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
39297 "num_teams", location);
39299 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
39300 OMP_CLAUSE_NUM_TEAMS_UPPER_EXPR (c) = upper;
39301 OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c) = lower;
39302 OMP_CLAUSE_CHAIN (c) = list;
39304 return c;
39307 /* OpenMP 4.0:
39308 thread_limit ( expression ) */
39310 static tree
39311 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
39312 location_t location)
39314 tree t, c;
39316 matching_parens parens;
39317 if (!parens.require_open (parser))
39318 return list;
39320 t = cp_parser_assignment_expression (parser);
39322 if (t == error_mark_node
39323 || !parens.require_close (parser))
39324 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39325 /*or_comma=*/false,
39326 /*consume_paren=*/true);
39328 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
39329 "thread_limit", location);
39331 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
39332 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
39333 OMP_CLAUSE_CHAIN (c) = list;
39335 return c;
39338 /* OpenMP 4.0:
39339 aligned ( variable-list )
39340 aligned ( variable-list : constant-expression ) */
39342 static tree
39343 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
39345 tree nlist, c, alignment = NULL_TREE;
39346 bool colon;
39348 matching_parens parens;
39349 if (!parens.require_open (parser))
39350 return list;
39352 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
39353 &colon);
39355 if (colon)
39357 alignment = cp_parser_constant_expression (parser);
39359 if (!parens.require_close (parser))
39360 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39361 /*or_comma=*/false,
39362 /*consume_paren=*/true);
39364 if (alignment == error_mark_node)
39365 alignment = NULL_TREE;
39368 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
39369 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
39371 return nlist;
39374 /* OpenMP 5.0:
39375 allocate ( variable-list )
39376 allocate ( expression : variable-list )
39378 OpenMP 5.1:
39379 allocate ( allocator-modifier : variable-list )
39380 allocate ( allocator-modifier , allocator-modifier : variable-list )
39382 allocator-modifier:
39383 allocator ( expression )
39384 align ( expression ) */
39386 static tree
39387 cp_parser_omp_clause_allocate (cp_parser *parser, tree list)
39389 tree nlist, c, allocator = NULL_TREE, align = NULL_TREE;
39390 bool colon, has_modifiers = false;
39392 matching_parens parens;
39393 if (!parens.require_open (parser))
39394 return list;
39396 cp_parser_parse_tentatively (parser);
39397 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
39398 parser->colon_corrects_to_scope_p = false;
39399 for (int mod = 0; mod < 2; mod++)
39400 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
39401 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
39403 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39404 const char *p = IDENTIFIER_POINTER (id);
39405 if (strcmp (p, "allocator") != 0 && strcmp (p, "align") != 0)
39406 break;
39407 cp_lexer_consume_token (parser->lexer);
39408 matching_parens parens2;
39409 if (!parens2.require_open (parser))
39410 break;
39411 if (strcmp (p, "allocator") == 0)
39413 if (allocator != NULL_TREE)
39414 break;
39415 allocator = cp_parser_assignment_expression (parser);
39417 else
39419 if (align != NULL_TREE)
39420 break;
39421 align = cp_parser_assignment_expression (parser);
39423 if (!parens2.require_close (parser))
39424 break;
39425 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
39427 has_modifiers = true;
39428 break;
39430 if (mod != 0 || cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
39431 break;
39432 cp_lexer_consume_token (parser->lexer);
39434 else
39435 break;
39436 if (!has_modifiers)
39438 cp_parser_abort_tentative_parse (parser);
39439 align = NULL_TREE;
39440 allocator = NULL_TREE;
39441 cp_parser_parse_tentatively (parser);
39442 allocator = cp_parser_assignment_expression (parser);
39444 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
39445 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
39447 cp_parser_parse_definitely (parser);
39448 cp_lexer_consume_token (parser->lexer);
39449 if (allocator == error_mark_node)
39450 allocator = NULL_TREE;
39451 if (align == error_mark_node)
39452 align = NULL_TREE;
39454 else
39456 cp_parser_abort_tentative_parse (parser);
39457 allocator = NULL_TREE;
39458 align = NULL_TREE;
39461 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALLOCATE, list,
39462 &colon);
39464 if (allocator || align)
39465 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
39467 OMP_CLAUSE_ALLOCATE_ALLOCATOR (c) = allocator;
39468 OMP_CLAUSE_ALLOCATE_ALIGN (c) = align;
39471 return nlist;
39474 /* OpenMP 2.5:
39475 lastprivate ( variable-list )
39477 OpenMP 5.0:
39478 lastprivate ( [ lastprivate-modifier : ] variable-list ) */
39480 static tree
39481 cp_parser_omp_clause_lastprivate (cp_parser *parser, tree list)
39483 bool conditional = false;
39485 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
39486 return list;
39488 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
39489 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
39491 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39492 const char *p = IDENTIFIER_POINTER (id);
39494 if (strcmp ("conditional", p) == 0)
39496 conditional = true;
39497 cp_lexer_consume_token (parser->lexer);
39498 cp_lexer_consume_token (parser->lexer);
39502 tree nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LASTPRIVATE,
39503 list, NULL);
39505 if (conditional)
39506 for (tree c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
39507 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 1;
39508 return nlist;
39511 /* OpenMP 4.0:
39512 linear ( variable-list )
39513 linear ( variable-list : expression )
39515 OpenMP 4.5:
39516 linear ( modifier ( variable-list ) )
39517 linear ( modifier ( variable-list ) : expression )
39519 modifier:
39522 uval
39524 OpenMP 5.2:
39525 linear ( variable-list : modifiers-list )
39527 modifiers:
39530 uval
39531 step ( expression ) */
39533 static tree
39534 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
39535 bool declare_simd)
39537 tree nlist, c, step = integer_one_node;
39538 bool colon;
39539 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
39540 bool old_linear_modifier = false;
39542 matching_parens parens;
39543 if (!parens.require_open (parser))
39544 return list;
39546 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39548 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39549 const char *p = IDENTIFIER_POINTER (id);
39551 if (strcmp ("ref", p) == 0)
39552 kind = OMP_CLAUSE_LINEAR_REF;
39553 else if (strcmp ("val", p) == 0)
39554 kind = OMP_CLAUSE_LINEAR_VAL;
39555 else if (strcmp ("uval", p) == 0)
39556 kind = OMP_CLAUSE_LINEAR_UVAL;
39557 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
39559 cp_lexer_consume_token (parser->lexer);
39560 old_linear_modifier = true;
39562 else
39563 kind = OMP_CLAUSE_LINEAR_DEFAULT;
39566 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
39567 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
39568 &colon);
39569 else
39571 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
39572 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
39573 if (colon)
39574 cp_parser_require (parser, CPP_COLON, RT_COLON);
39575 else if (!parens.require_close (parser))
39576 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39577 /*or_comma=*/false,
39578 /*consume_paren=*/true);
39581 if (colon)
39583 bool has_modifiers = false;
39584 if (kind == OMP_CLAUSE_LINEAR_DEFAULT
39585 && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39587 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39588 const char *p = IDENTIFIER_POINTER (id);
39589 size_t pos = 0;
39590 if (strcmp ("ref", p) == 0
39591 || strcmp ("val", p) == 0
39592 || strcmp ("uval", p) == 0)
39593 pos = 2;
39594 else if (strcmp ("step", p) == 0
39595 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
39597 pos = cp_parser_skip_balanced_tokens (parser, 2);
39598 if (pos == 2)
39599 pos = 0;
39601 if (pos != 0
39602 && (cp_lexer_nth_token_is (parser->lexer, pos, CPP_COMMA)
39603 || cp_lexer_nth_token_is (parser->lexer, pos,
39604 CPP_CLOSE_PAREN)))
39605 has_modifiers = true;
39608 step = NULL_TREE;
39609 if (has_modifiers)
39611 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39613 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39614 const char *p = IDENTIFIER_POINTER (id);
39615 enum omp_clause_linear_kind nkind = OMP_CLAUSE_LINEAR_DEFAULT;
39616 if (strcmp ("ref", p) == 0)
39617 nkind = OMP_CLAUSE_LINEAR_REF;
39618 else if (strcmp ("val", p) == 0)
39619 nkind = OMP_CLAUSE_LINEAR_VAL;
39620 else if (strcmp ("uval", p) == 0)
39621 nkind = OMP_CLAUSE_LINEAR_UVAL;
39622 if (nkind != OMP_CLAUSE_LINEAR_DEFAULT)
39624 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
39625 error_at (cp_lexer_peek_token (parser->lexer)->location,
39626 "multiple linear modifiers");
39627 kind = nkind;
39628 cp_lexer_consume_token (parser->lexer);
39630 else if (strcmp ("step", p) == 0)
39632 location_t step_loc
39633 = cp_lexer_peek_token (parser->lexer)->location;
39634 cp_lexer_consume_token (parser->lexer);
39635 matching_parens parens2;
39636 if (parens2.require_open (parser))
39638 if (step)
39639 error_at (step_loc, "multiple %<step%> modifiers");
39640 if (declare_simd
39641 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
39642 && cp_lexer_nth_token_is (parser->lexer, 2,
39643 CPP_CLOSE_PAREN))
39645 cp_token *token
39646 = cp_lexer_peek_token (parser->lexer);
39647 location_t tok_loc = token->location;
39648 cp_parser_parse_tentatively (parser);
39649 step = cp_parser_id_expression (parser, false, true,
39650 NULL, false, false);
39651 if (step != error_mark_node)
39652 step = cp_parser_lookup_name_simple (parser, step,
39653 tok_loc);
39654 if (step == error_mark_node)
39656 step = NULL_TREE;
39657 cp_parser_abort_tentative_parse (parser);
39659 else if (!cp_parser_parse_definitely (parser))
39660 step = NULL_TREE;
39662 if (!step)
39663 step = cp_parser_assignment_expression (parser);
39664 if (!parens2.require_close (parser))
39665 cp_parser_skip_to_closing_parenthesis (parser, true,
39666 false, true);
39668 else
39669 break;
39671 else
39672 break;
39673 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39675 cp_lexer_consume_token (parser->lexer);
39676 continue;
39678 break;
39680 if (!step)
39681 step = integer_one_node;
39683 else if (declare_simd
39684 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
39685 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
39687 cp_token *token = cp_lexer_peek_token (parser->lexer);
39688 cp_parser_parse_tentatively (parser);
39689 step = cp_parser_id_expression (parser, /*template_p=*/false,
39690 /*check_dependency_p=*/true,
39691 /*template_p=*/NULL,
39692 /*declarator_p=*/false,
39693 /*optional_p=*/false);
39694 if (step != error_mark_node)
39695 step = cp_parser_lookup_name_simple (parser, step, token->location);
39696 if (step == error_mark_node)
39698 step = NULL_TREE;
39699 cp_parser_abort_tentative_parse (parser);
39701 else if (!cp_parser_parse_definitely (parser))
39702 step = NULL_TREE;
39704 if (!step)
39705 step = cp_parser_assignment_expression (parser);
39707 if (!parens.require_close (parser))
39708 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39709 /*or_comma=*/false,
39710 /*consume_paren=*/true);
39712 if (step == error_mark_node)
39713 return list;
39716 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
39718 OMP_CLAUSE_LINEAR_STEP (c) = step;
39719 OMP_CLAUSE_LINEAR_KIND (c) = kind;
39720 OMP_CLAUSE_LINEAR_OLD_LINEAR_MODIFIER (c) = old_linear_modifier;
39723 return nlist;
39726 /* OpenMP 4.0:
39727 safelen ( constant-expression ) */
39729 static tree
39730 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
39731 location_t location)
39733 tree t, c;
39735 matching_parens parens;
39736 if (!parens.require_open (parser))
39737 return list;
39739 t = cp_parser_constant_expression (parser);
39741 if (t == error_mark_node
39742 || !parens.require_close (parser))
39743 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39744 /*or_comma=*/false,
39745 /*consume_paren=*/true);
39747 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
39749 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
39750 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
39751 OMP_CLAUSE_CHAIN (c) = list;
39753 return c;
39756 /* OpenMP 4.0:
39757 simdlen ( constant-expression ) */
39759 static tree
39760 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
39761 location_t location)
39763 tree t, c;
39765 matching_parens parens;
39766 if (!parens.require_open (parser))
39767 return list;
39769 t = cp_parser_constant_expression (parser);
39771 if (t == error_mark_node
39772 || !parens.require_close (parser))
39773 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39774 /*or_comma=*/false,
39775 /*consume_paren=*/true);
39777 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
39779 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
39780 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
39781 OMP_CLAUSE_CHAIN (c) = list;
39783 return c;
39786 /* OpenMP 4.5:
39787 vec:
39788 identifier [+/- integer]
39789 vec , identifier [+/- integer]
39792 static tree
39793 cp_parser_omp_clause_doacross_sink (cp_parser *parser, location_t clause_loc,
39794 tree list, bool depend_p)
39796 tree vec = NULL;
39798 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
39800 cp_parser_error (parser, "expected identifier");
39801 return list;
39804 if (!depend_p)
39806 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39807 if (strcmp (IDENTIFIER_POINTER (id), "omp_cur_iteration") == 0
39808 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_MINUS)
39809 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_NUMBER)
39810 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_PAREN))
39812 tree val = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
39813 if (integer_onep (val))
39815 cp_lexer_consume_token (parser->lexer);
39816 cp_lexer_consume_token (parser->lexer);
39817 cp_lexer_consume_token (parser->lexer);
39818 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DOACROSS);
39819 OMP_CLAUSE_DOACROSS_KIND (u) = OMP_CLAUSE_DOACROSS_SINK;
39820 OMP_CLAUSE_CHAIN (u) = list;
39821 return u;
39826 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39828 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
39829 tree t, identifier = cp_parser_identifier (parser);
39830 tree addend = NULL;
39832 if (identifier == error_mark_node)
39833 t = error_mark_node;
39834 else
39836 t = cp_parser_lookup_name_simple
39837 (parser, identifier,
39838 cp_lexer_peek_token (parser->lexer)->location);
39839 if (t == error_mark_node)
39840 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
39841 id_loc);
39844 bool neg = false;
39845 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
39846 neg = true;
39847 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
39849 addend = integer_zero_node;
39850 goto add_to_vector;
39852 cp_lexer_consume_token (parser->lexer);
39854 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
39856 cp_parser_error (parser, "expected integer");
39857 return list;
39860 addend = cp_lexer_peek_token (parser->lexer)->u.value;
39861 if (TREE_CODE (addend) != INTEGER_CST)
39863 cp_parser_error (parser, "expected integer");
39864 return list;
39866 cp_lexer_consume_token (parser->lexer);
39868 add_to_vector:
39869 if (t != error_mark_node)
39871 vec = tree_cons (addend, t, vec);
39872 if (neg)
39873 OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (vec) = 1;
39876 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
39877 || !cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
39878 break;
39880 cp_lexer_consume_token (parser->lexer);
39883 if (vec)
39885 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DOACROSS);
39886 OMP_CLAUSE_DOACROSS_KIND (u) = OMP_CLAUSE_DOACROSS_SINK;
39887 OMP_CLAUSE_DOACROSS_DEPEND (u) = depend_p;
39888 OMP_CLAUSE_DECL (u) = nreverse (vec);
39889 OMP_CLAUSE_CHAIN (u) = list;
39890 return u;
39892 return list;
39895 /* OpenMP 5.0:
39896 detach ( event-handle ) */
39898 static tree
39899 cp_parser_omp_clause_detach (cp_parser *parser, tree list)
39901 matching_parens parens;
39903 if (!parens.require_open (parser))
39904 return list;
39906 cp_token *token;
39907 tree name, decl;
39909 token = cp_lexer_peek_token (parser->lexer);
39910 name = cp_parser_id_expression (parser, /*template_p=*/false,
39911 /*check_dependency_p=*/true,
39912 /*template_p=*/NULL,
39913 /*declarator_p=*/false,
39914 /*optional_p=*/false);
39915 if (name == error_mark_node)
39916 decl = error_mark_node;
39917 else
39919 if (identifier_p (name))
39920 decl = cp_parser_lookup_name_simple (parser, name, token->location);
39921 else
39922 decl = name;
39923 if (decl == error_mark_node)
39924 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
39925 token->location);
39928 if (decl == error_mark_node
39929 || !parens.require_close (parser))
39930 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39931 /*or_comma=*/false,
39932 /*consume_paren=*/true);
39934 tree u = build_omp_clause (token->location, OMP_CLAUSE_DETACH);
39935 OMP_CLAUSE_DECL (u) = decl;
39936 OMP_CLAUSE_CHAIN (u) = list;
39938 return u;
39941 /* OpenMP 5.0:
39942 iterators ( iterators-definition )
39944 iterators-definition:
39945 iterator-specifier
39946 iterator-specifier , iterators-definition
39948 iterator-specifier:
39949 identifier = range-specification
39950 iterator-type identifier = range-specification
39952 range-specification:
39953 begin : end
39954 begin : end : step */
39956 static tree
39957 cp_parser_omp_iterators (cp_parser *parser)
39959 tree ret = NULL_TREE, *last = &ret;
39960 cp_lexer_consume_token (parser->lexer);
39962 matching_parens parens;
39963 if (!parens.require_open (parser))
39964 return error_mark_node;
39966 bool saved_colon_corrects_to_scope_p
39967 = parser->colon_corrects_to_scope_p;
39968 bool saved_colon_doesnt_start_class_def_p
39969 = parser->colon_doesnt_start_class_def_p;
39973 tree iter_type;
39974 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
39975 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ))
39976 iter_type = integer_type_node;
39977 else
39979 const char *saved_message
39980 = parser->type_definition_forbidden_message;
39981 parser->type_definition_forbidden_message
39982 = G_("types may not be defined in iterator type");
39984 iter_type = cp_parser_type_id (parser);
39986 parser->type_definition_forbidden_message = saved_message;
39989 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39990 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
39992 cp_parser_error (parser, "expected identifier");
39993 break;
39996 tree id = cp_parser_identifier (parser);
39997 if (id == error_mark_node)
39998 break;
40000 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
40001 break;
40003 parser->colon_corrects_to_scope_p = false;
40004 parser->colon_doesnt_start_class_def_p = true;
40005 tree begin = cp_parser_assignment_expression (parser);
40007 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
40008 break;
40010 tree end = cp_parser_assignment_expression (parser);
40012 tree step = integer_one_node;
40013 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
40015 cp_lexer_consume_token (parser->lexer);
40016 step = cp_parser_assignment_expression (parser);
40019 tree iter_var = build_decl (loc, VAR_DECL, id, iter_type);
40020 DECL_ARTIFICIAL (iter_var) = 1;
40021 DECL_CONTEXT (iter_var) = current_function_decl;
40022 pushdecl (iter_var);
40024 *last = make_tree_vec (6);
40025 TREE_VEC_ELT (*last, 0) = iter_var;
40026 TREE_VEC_ELT (*last, 1) = begin;
40027 TREE_VEC_ELT (*last, 2) = end;
40028 TREE_VEC_ELT (*last, 3) = step;
40029 last = &TREE_CHAIN (*last);
40031 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
40033 cp_lexer_consume_token (parser->lexer);
40034 continue;
40036 break;
40038 while (1);
40040 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
40041 parser->colon_doesnt_start_class_def_p
40042 = saved_colon_doesnt_start_class_def_p;
40044 if (!parens.require_close (parser))
40045 cp_parser_skip_to_closing_parenthesis (parser,
40046 /*recovering=*/true,
40047 /*or_comma=*/false,
40048 /*consume_paren=*/true);
40050 return ret ? ret : error_mark_node;
40053 /* OpenMP 5.0:
40054 affinity ( [aff-modifier :] variable-list )
40055 aff-modifier:
40056 iterator ( iterators-definition ) */
40058 static tree
40059 cp_parser_omp_clause_affinity (cp_parser *parser, tree list)
40061 tree nlist, c, iterators = NULL_TREE;
40063 matching_parens parens;
40064 if (!parens.require_open (parser))
40065 return list;
40067 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40069 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40070 const char *p = IDENTIFIER_POINTER (id);
40071 bool parse_iter = ((strcmp ("iterator", p) == 0)
40072 && (cp_lexer_nth_token_is (parser->lexer, 2,
40073 CPP_OPEN_PAREN)));
40074 if (parse_iter)
40076 size_t n = cp_parser_skip_balanced_tokens (parser, 2);
40077 parse_iter = cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON);
40079 if (parse_iter)
40081 begin_scope (sk_omp, NULL);
40082 iterators = cp_parser_omp_iterators (parser);
40083 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
40085 if (iterators)
40086 poplevel (0, 1, 0);
40087 cp_parser_skip_to_closing_parenthesis (parser,
40088 /*recovering=*/true,
40089 /*or_comma=*/false,
40090 /*consume_paren=*/true);
40091 return list;
40095 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_AFFINITY,
40096 list, NULL);
40097 if (iterators)
40099 tree block = poplevel (1, 1, 0);
40100 if (iterators != error_mark_node)
40102 TREE_VEC_ELT (iterators, 5) = block;
40103 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
40104 OMP_CLAUSE_DECL (c) = build_tree_list (iterators,
40105 OMP_CLAUSE_DECL (c));
40108 return nlist;
40111 /* OpenMP 4.0:
40112 depend ( depend-kind : variable-list )
40114 depend-kind:
40115 in | out | inout
40117 OpenMP 4.5:
40118 depend ( source )
40120 depend ( sink : vec )
40122 OpenMP 5.0:
40123 depend ( depend-modifier , depend-kind: variable-list )
40125 depend-kind:
40126 in | out | inout | mutexinoutset | depobj
40128 depend-modifier:
40129 iterator ( iterators-definition ) */
40131 static tree
40132 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
40134 tree nlist, c, iterators = NULL_TREE;
40135 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_LAST;
40136 enum omp_clause_doacross_kind dkind = OMP_CLAUSE_DOACROSS_LAST;
40138 matching_parens parens;
40139 if (!parens.require_open (parser))
40140 return list;
40144 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
40145 goto invalid_kind;
40147 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40148 const char *p = IDENTIFIER_POINTER (id);
40150 if (strcmp ("iterator", p) == 0 && iterators == NULL_TREE)
40152 begin_scope (sk_omp, NULL);
40153 iterators = cp_parser_omp_iterators (parser);
40154 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
40155 continue;
40157 if (strcmp ("in", p) == 0)
40158 kind = OMP_CLAUSE_DEPEND_IN;
40159 else if (strcmp ("inout", p) == 0)
40160 kind = OMP_CLAUSE_DEPEND_INOUT;
40161 else if (strcmp ("inoutset", p) == 0)
40162 kind = OMP_CLAUSE_DEPEND_INOUTSET;
40163 else if (strcmp ("mutexinoutset", p) == 0)
40164 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
40165 else if (strcmp ("out", p) == 0)
40166 kind = OMP_CLAUSE_DEPEND_OUT;
40167 else if (strcmp ("depobj", p) == 0)
40168 kind = OMP_CLAUSE_DEPEND_DEPOBJ;
40169 else if (strcmp ("sink", p) == 0)
40170 dkind = OMP_CLAUSE_DOACROSS_SINK;
40171 else if (strcmp ("source", p) == 0)
40172 dkind = OMP_CLAUSE_DOACROSS_SOURCE;
40173 else
40174 goto invalid_kind;
40175 break;
40177 while (1);
40179 cp_lexer_consume_token (parser->lexer);
40181 if (iterators
40182 && (dkind == OMP_CLAUSE_DOACROSS_SOURCE
40183 || dkind == OMP_CLAUSE_DOACROSS_SINK))
40185 poplevel (0, 1, 0);
40186 error_at (loc, "%<iterator%> modifier incompatible with %qs",
40187 dkind == OMP_CLAUSE_DOACROSS_SOURCE ? "source" : "sink");
40188 iterators = NULL_TREE;
40191 if (dkind == OMP_CLAUSE_DOACROSS_SOURCE)
40193 c = build_omp_clause (loc, OMP_CLAUSE_DOACROSS);
40194 OMP_CLAUSE_DOACROSS_KIND (c) = dkind;
40195 OMP_CLAUSE_DOACROSS_DEPEND (c) = 1;
40196 OMP_CLAUSE_DECL (c) = NULL_TREE;
40197 OMP_CLAUSE_CHAIN (c) = list;
40198 if (!parens.require_close (parser))
40199 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40200 /*or_comma=*/false,
40201 /*consume_paren=*/true);
40202 return c;
40205 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
40206 goto resync_fail;
40208 if (dkind == OMP_CLAUSE_DOACROSS_SINK)
40210 nlist = cp_parser_omp_clause_doacross_sink (parser, loc, list, true);
40211 if (!parens.require_close (parser))
40212 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40213 /*or_comma=*/false,
40214 /*consume_paren=*/true);
40216 else
40218 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
40219 list, NULL);
40221 if (iterators)
40223 tree block = poplevel (1, 1, 0);
40224 if (iterators == error_mark_node)
40225 iterators = NULL_TREE;
40226 else
40227 TREE_VEC_ELT (iterators, 5) = block;
40230 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
40232 OMP_CLAUSE_DEPEND_KIND (c) = kind;
40233 if (iterators)
40234 OMP_CLAUSE_DECL (c)
40235 = build_tree_list (iterators, OMP_CLAUSE_DECL (c));
40238 return nlist;
40240 invalid_kind:
40241 cp_parser_error (parser, "invalid depend kind");
40242 resync_fail:
40243 if (iterators)
40244 poplevel (0, 1, 0);
40245 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40246 /*or_comma=*/false,
40247 /*consume_paren=*/true);
40248 return list;
40251 /* OpenMP 5.2:
40252 doacross ( source : )
40253 doacross ( source : omp_cur_iteration )
40255 doacross ( sink : vec )
40256 doacross ( sink : omp_cur_iteration - logical_iteration ) */
40258 static tree
40259 cp_parser_omp_clause_doacross (cp_parser *parser, tree list, location_t loc)
40261 tree nlist;
40262 enum omp_clause_doacross_kind kind = OMP_CLAUSE_DOACROSS_LAST;
40264 matching_parens parens;
40265 if (!parens.require_open (parser))
40266 return list;
40268 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
40270 invalid_kind:
40271 cp_parser_error (parser, "invalid doacross kind");
40272 resync_fail:
40273 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40274 /*or_comma=*/false,
40275 /*consume_paren=*/true);
40276 return list;
40279 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40280 const char *p = IDENTIFIER_POINTER (id);
40282 if (strcmp ("sink", p) == 0)
40283 kind = OMP_CLAUSE_DOACROSS_SINK;
40284 else if (strcmp ("source", p) == 0)
40285 kind = OMP_CLAUSE_DOACROSS_SOURCE;
40286 else
40287 goto invalid_kind;
40289 cp_lexer_consume_token (parser->lexer);
40291 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
40292 goto resync_fail;
40294 if (kind == OMP_CLAUSE_DOACROSS_SOURCE)
40296 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40298 id = cp_lexer_peek_token (parser->lexer)->u.value;
40299 p = IDENTIFIER_POINTER (id);
40300 if (strcmp (p, "omp_cur_iteration") == 0)
40301 cp_lexer_consume_token (parser->lexer);
40303 nlist = build_omp_clause (loc, OMP_CLAUSE_DOACROSS);
40304 OMP_CLAUSE_DOACROSS_KIND (nlist) = OMP_CLAUSE_DOACROSS_SOURCE;
40305 OMP_CLAUSE_DECL (nlist) = NULL_TREE;
40306 OMP_CLAUSE_CHAIN (nlist) = list;
40308 else
40309 nlist = cp_parser_omp_clause_doacross_sink (parser, loc, list, false);
40311 if (!parens.require_close (parser))
40312 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40313 /*or_comma=*/false,
40314 /*consume_paren=*/true);
40315 return nlist;
40318 /* OpenMP 4.0:
40319 map ( map-kind : variable-list )
40320 map ( variable-list )
40322 map-kind:
40323 alloc | to | from | tofrom
40325 OpenMP 4.5:
40326 map-kind:
40327 alloc | to | from | tofrom | release | delete
40329 map ( always [,] map-kind: variable-list )
40331 OpenMP 5.0:
40332 map ( [map-type-modifier[,] ...] map-kind: variable-list )
40334 map-type-modifier:
40335 always | close */
40337 static tree
40338 cp_parser_omp_clause_map (cp_parser *parser, tree list)
40340 tree nlist, c;
40341 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
40343 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
40344 return list;
40346 int pos = 1;
40347 int map_kind_pos = 0;
40348 while (cp_lexer_peek_nth_token (parser->lexer, pos)->type == CPP_NAME
40349 || cp_lexer_peek_nth_token (parser->lexer, pos)->keyword == RID_DELETE)
40351 if (cp_lexer_peek_nth_token (parser->lexer, pos + 1)->type == CPP_COLON)
40353 map_kind_pos = pos;
40354 break;
40357 if (cp_lexer_peek_nth_token (parser->lexer, pos + 1)->type == CPP_COMMA)
40358 pos++;
40359 pos++;
40362 bool always_modifier = false;
40363 bool close_modifier = false;
40364 for (int pos = 1; pos < map_kind_pos; ++pos)
40366 cp_token *tok = cp_lexer_peek_token (parser->lexer);
40367 if (tok->type == CPP_COMMA)
40369 cp_lexer_consume_token (parser->lexer);
40370 continue;
40373 const char *p = IDENTIFIER_POINTER (tok->u.value);
40374 if (strcmp ("always", p) == 0)
40376 if (always_modifier)
40378 cp_parser_error (parser, "too many %<always%> modifiers");
40379 cp_parser_skip_to_closing_parenthesis (parser,
40380 /*recovering=*/true,
40381 /*or_comma=*/false,
40382 /*consume_paren=*/true);
40383 return list;
40385 always_modifier = true;
40387 else if (strcmp ("close", p) == 0)
40389 if (close_modifier)
40391 cp_parser_error (parser, "too many %<close%> modifiers");
40392 cp_parser_skip_to_closing_parenthesis (parser,
40393 /*recovering=*/true,
40394 /*or_comma=*/false,
40395 /*consume_paren=*/true);
40396 return list;
40398 close_modifier = true;
40400 else
40402 cp_parser_error (parser, "%<#pragma omp target%> with "
40403 "modifier other than %<always%> or "
40404 "%<close%> on %<map%> clause");
40405 cp_parser_skip_to_closing_parenthesis (parser,
40406 /*recovering=*/true,
40407 /*or_comma=*/false,
40408 /*consume_paren=*/true);
40409 return list;
40412 cp_lexer_consume_token (parser->lexer);
40415 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
40416 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
40418 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40419 const char *p = IDENTIFIER_POINTER (id);
40421 if (strcmp ("alloc", p) == 0)
40422 kind = GOMP_MAP_ALLOC;
40423 else if (strcmp ("to", p) == 0)
40424 kind = always_modifier ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
40425 else if (strcmp ("from", p) == 0)
40426 kind = always_modifier ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
40427 else if (strcmp ("tofrom", p) == 0)
40428 kind = always_modifier ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
40429 else if (strcmp ("release", p) == 0)
40430 kind = GOMP_MAP_RELEASE;
40431 else
40433 cp_parser_error (parser, "invalid map kind");
40434 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40435 /*or_comma=*/false,
40436 /*consume_paren=*/true);
40437 return list;
40439 cp_lexer_consume_token (parser->lexer);
40440 cp_lexer_consume_token (parser->lexer);
40442 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
40443 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
40445 kind = GOMP_MAP_DELETE;
40446 cp_lexer_consume_token (parser->lexer);
40447 cp_lexer_consume_token (parser->lexer);
40450 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
40451 NULL, true);
40453 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
40454 OMP_CLAUSE_SET_MAP_KIND (c, kind);
40456 return nlist;
40459 /* OpenMP 4.0:
40460 device ( expression )
40462 OpenMP 5.0:
40463 device ( [device-modifier :] integer-expression )
40465 device-modifier:
40466 ancestor | device_num */
40468 static tree
40469 cp_parser_omp_clause_device (cp_parser *parser, tree list,
40470 location_t location)
40472 tree t, c;
40473 bool ancestor = false;
40475 matching_parens parens;
40476 if (!parens.require_open (parser))
40477 return list;
40479 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
40480 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
40482 cp_token *tok = cp_lexer_peek_token (parser->lexer);
40483 const char *p = IDENTIFIER_POINTER (tok->u.value);
40484 if (strcmp ("ancestor", p) == 0)
40486 ancestor = true;
40488 /* A requires directive with the reverse_offload clause must be
40489 specified. */
40490 if ((omp_requires_mask & OMP_REQUIRES_REVERSE_OFFLOAD) == 0)
40492 error_at (tok->location, "%<ancestor%> device modifier not "
40493 "preceded by %<requires%> directive "
40494 "with %<reverse_offload%> clause");
40495 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
40496 return list;
40499 else if (strcmp ("device_num", p) == 0)
40501 else
40503 error_at (tok->location, "expected %<ancestor%> or %<device_num%>");
40504 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
40505 return list;
40507 cp_lexer_consume_token (parser->lexer);
40508 cp_lexer_consume_token (parser->lexer);
40511 t = cp_parser_assignment_expression (parser);
40513 if (t == error_mark_node
40514 || !parens.require_close (parser))
40515 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40516 /*or_comma=*/false,
40517 /*consume_paren=*/true);
40519 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
40520 "device", location);
40522 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
40523 OMP_CLAUSE_DEVICE_ID (c) = t;
40524 OMP_CLAUSE_CHAIN (c) = list;
40525 OMP_CLAUSE_DEVICE_ANCESTOR (c) = ancestor;
40527 return c;
40530 /* OpenMP 4.0:
40531 dist_schedule ( static )
40532 dist_schedule ( static , expression ) */
40534 static tree
40535 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
40536 location_t location)
40538 tree c, t;
40540 matching_parens parens;
40541 if (!parens.require_open (parser))
40542 return list;
40544 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
40546 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
40547 goto invalid_kind;
40548 cp_lexer_consume_token (parser->lexer);
40550 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
40552 cp_lexer_consume_token (parser->lexer);
40554 t = cp_parser_assignment_expression (parser);
40556 if (t == error_mark_node)
40557 goto resync_fail;
40558 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
40560 if (!parens.require_close (parser))
40561 goto resync_fail;
40563 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
40564 goto resync_fail;
40566 /* check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE,
40567 "dist_schedule", location); */
40568 if (omp_find_clause (list, OMP_CLAUSE_DIST_SCHEDULE))
40569 warning_at (location, 0, "too many %qs clauses", "dist_schedule");
40570 OMP_CLAUSE_CHAIN (c) = list;
40571 return c;
40573 invalid_kind:
40574 cp_parser_error (parser, "invalid dist_schedule kind");
40575 resync_fail:
40576 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40577 /*or_comma=*/false,
40578 /*consume_paren=*/true);
40579 return list;
40582 /* OpenMP 4.0:
40583 proc_bind ( proc-bind-kind )
40585 proc-bind-kind:
40586 primary | master | close | spread
40587 where OpenMP 5.1 added 'primary' and deprecated the alias 'master'. */
40589 static tree
40590 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
40591 location_t location)
40593 tree c;
40594 enum omp_clause_proc_bind_kind kind;
40596 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
40597 return list;
40599 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40601 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40602 const char *p = IDENTIFIER_POINTER (id);
40604 if (strcmp ("primary", p) == 0)
40605 kind = OMP_CLAUSE_PROC_BIND_PRIMARY;
40606 else if (strcmp ("master", p) == 0)
40607 kind = OMP_CLAUSE_PROC_BIND_MASTER;
40608 else if (strcmp ("close", p) == 0)
40609 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
40610 else if (strcmp ("spread", p) == 0)
40611 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
40612 else
40613 goto invalid_kind;
40615 else
40616 goto invalid_kind;
40618 cp_lexer_consume_token (parser->lexer);
40619 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
40620 goto resync_fail;
40622 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
40623 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
40624 location);
40625 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
40626 OMP_CLAUSE_CHAIN (c) = list;
40627 return c;
40629 invalid_kind:
40630 cp_parser_error (parser, "invalid depend kind");
40631 resync_fail:
40632 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40633 /*or_comma=*/false,
40634 /*consume_paren=*/true);
40635 return list;
40638 /* OpenMP 5.0:
40639 device_type ( host | nohost | any ) */
40641 static tree
40642 cp_parser_omp_clause_device_type (cp_parser *parser, tree list,
40643 location_t location)
40645 tree c;
40646 enum omp_clause_device_type_kind kind;
40648 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
40649 return list;
40651 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40653 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40654 const char *p = IDENTIFIER_POINTER (id);
40656 if (strcmp ("host", p) == 0)
40657 kind = OMP_CLAUSE_DEVICE_TYPE_HOST;
40658 else if (strcmp ("nohost", p) == 0)
40659 kind = OMP_CLAUSE_DEVICE_TYPE_NOHOST;
40660 else if (strcmp ("any", p) == 0)
40661 kind = OMP_CLAUSE_DEVICE_TYPE_ANY;
40662 else
40663 goto invalid_kind;
40665 else
40666 goto invalid_kind;
40668 cp_lexer_consume_token (parser->lexer);
40669 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
40670 goto resync_fail;
40672 c = build_omp_clause (location, OMP_CLAUSE_DEVICE_TYPE);
40673 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE_TYPE, "device_type",
40674 location);
40675 OMP_CLAUSE_DEVICE_TYPE_KIND (c) = kind;
40676 OMP_CLAUSE_CHAIN (c) = list;
40677 return c;
40679 invalid_kind:
40680 cp_parser_error (parser, "invalid depend kind");
40681 resync_fail:
40682 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40683 /*or_comma=*/false,
40684 /*consume_paren=*/true);
40685 return list;
40688 /* OpenACC:
40689 async [( int-expr )] */
40691 static tree
40692 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
40694 tree c, t;
40695 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
40697 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
40699 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
40701 matching_parens parens;
40702 parens.consume_open (parser);
40704 t = cp_parser_assignment_expression (parser);
40705 if (t == error_mark_node
40706 || !parens.require_close (parser))
40707 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40708 /*or_comma=*/false,
40709 /*consume_paren=*/true);
40712 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
40714 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
40715 OMP_CLAUSE_ASYNC_EXPR (c) = t;
40716 OMP_CLAUSE_CHAIN (c) = list;
40717 list = c;
40719 return list;
40722 /* Parse all OpenACC clauses. The set clauses allowed by the directive
40723 is a bitmask in MASK. Return the list of clauses found. */
40725 static tree
40726 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
40727 const char *where, cp_token *pragma_tok,
40728 bool finish_p = true)
40730 tree clauses = NULL;
40731 bool first = true;
40733 /* Don't create location wrapper nodes within OpenACC clauses. */
40734 auto_suppress_location_wrappers sentinel;
40736 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
40738 location_t here;
40739 pragma_omp_clause c_kind;
40740 omp_clause_code code;
40741 const char *c_name;
40742 tree prev = clauses;
40744 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
40745 cp_lexer_consume_token (parser->lexer);
40747 here = cp_lexer_peek_token (parser->lexer)->location;
40748 c_kind = cp_parser_omp_clause_name (parser);
40750 switch (c_kind)
40752 case PRAGMA_OACC_CLAUSE_ASYNC:
40753 clauses = cp_parser_oacc_clause_async (parser, clauses);
40754 c_name = "async";
40755 break;
40756 case PRAGMA_OACC_CLAUSE_AUTO:
40757 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_AUTO,
40758 clauses);
40759 c_name = "auto";
40760 break;
40761 case PRAGMA_OACC_CLAUSE_ATTACH:
40762 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
40763 c_name = "attach";
40764 break;
40765 case PRAGMA_OACC_CLAUSE_COLLAPSE:
40766 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
40767 c_name = "collapse";
40768 break;
40769 case PRAGMA_OACC_CLAUSE_COPY:
40770 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
40771 c_name = "copy";
40772 break;
40773 case PRAGMA_OACC_CLAUSE_COPYIN:
40774 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
40775 c_name = "copyin";
40776 break;
40777 case PRAGMA_OACC_CLAUSE_COPYOUT:
40778 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
40779 c_name = "copyout";
40780 break;
40781 case PRAGMA_OACC_CLAUSE_CREATE:
40782 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
40783 c_name = "create";
40784 break;
40785 case PRAGMA_OACC_CLAUSE_DELETE:
40786 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
40787 c_name = "delete";
40788 break;
40789 case PRAGMA_OMP_CLAUSE_DEFAULT:
40790 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
40791 c_name = "default";
40792 break;
40793 case PRAGMA_OACC_CLAUSE_DETACH:
40794 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
40795 c_name = "detach";
40796 break;
40797 case PRAGMA_OACC_CLAUSE_DEVICE:
40798 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
40799 c_name = "device";
40800 break;
40801 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
40802 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
40803 c_name = "deviceptr";
40804 break;
40805 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
40806 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
40807 c_name = "device_resident";
40808 break;
40809 case PRAGMA_OACC_CLAUSE_FINALIZE:
40810 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_FINALIZE,
40811 clauses);
40812 c_name = "finalize";
40813 break;
40814 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
40815 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
40816 clauses);
40817 c_name = "firstprivate";
40818 break;
40819 case PRAGMA_OACC_CLAUSE_GANG:
40820 c_name = "gang";
40821 clauses = cp_parser_oacc_shape_clause (parser, here, OMP_CLAUSE_GANG,
40822 c_name, clauses);
40823 break;
40824 case PRAGMA_OACC_CLAUSE_HOST:
40825 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
40826 c_name = "host";
40827 break;
40828 case PRAGMA_OACC_CLAUSE_IF:
40829 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
40830 c_name = "if";
40831 break;
40832 case PRAGMA_OACC_CLAUSE_IF_PRESENT:
40833 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_IF_PRESENT,
40834 clauses);
40835 c_name = "if_present";
40836 break;
40837 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
40838 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_INDEPENDENT,
40839 clauses);
40840 c_name = "independent";
40841 break;
40842 case PRAGMA_OACC_CLAUSE_LINK:
40843 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
40844 c_name = "link";
40845 break;
40846 case PRAGMA_OACC_CLAUSE_NO_CREATE:
40847 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
40848 c_name = "no_create";
40849 break;
40850 case PRAGMA_OACC_CLAUSE_NOHOST:
40851 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_NOHOST,
40852 clauses);
40853 c_name = "nohost";
40854 break;
40855 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
40856 code = OMP_CLAUSE_NUM_GANGS;
40857 c_name = "num_gangs";
40858 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
40859 clauses);
40860 break;
40861 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
40862 c_name = "num_workers";
40863 code = OMP_CLAUSE_NUM_WORKERS;
40864 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
40865 clauses);
40866 break;
40867 case PRAGMA_OACC_CLAUSE_PRESENT:
40868 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
40869 c_name = "present";
40870 break;
40871 case PRAGMA_OACC_CLAUSE_PRIVATE:
40872 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
40873 clauses);
40874 c_name = "private";
40875 break;
40876 case PRAGMA_OACC_CLAUSE_REDUCTION:
40877 clauses
40878 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
40879 false, clauses);
40880 c_name = "reduction";
40881 break;
40882 case PRAGMA_OACC_CLAUSE_SEQ:
40883 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_SEQ,
40884 clauses);
40885 c_name = "seq";
40886 break;
40887 case PRAGMA_OACC_CLAUSE_TILE:
40888 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
40889 c_name = "tile";
40890 break;
40891 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
40892 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
40893 clauses);
40894 c_name = "use_device";
40895 break;
40896 case PRAGMA_OACC_CLAUSE_VECTOR:
40897 c_name = "vector";
40898 clauses = cp_parser_oacc_shape_clause (parser, here,
40899 OMP_CLAUSE_VECTOR,
40900 c_name, clauses);
40901 break;
40902 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
40903 c_name = "vector_length";
40904 code = OMP_CLAUSE_VECTOR_LENGTH;
40905 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
40906 clauses);
40907 break;
40908 case PRAGMA_OACC_CLAUSE_WAIT:
40909 clauses = cp_parser_oacc_clause_wait (parser, clauses);
40910 c_name = "wait";
40911 break;
40912 case PRAGMA_OACC_CLAUSE_WORKER:
40913 c_name = "worker";
40914 clauses = cp_parser_oacc_shape_clause (parser, here,
40915 OMP_CLAUSE_WORKER,
40916 c_name, clauses);
40917 break;
40918 default:
40919 cp_parser_error (parser, "expected %<#pragma acc%> clause");
40920 goto saw_error;
40923 first = false;
40925 if (((mask >> c_kind) & 1) == 0)
40927 /* Remove the invalid clause(s) from the list to avoid
40928 confusing the rest of the compiler. */
40929 clauses = prev;
40930 error_at (here, "%qs is not valid for %qs", c_name, where);
40934 saw_error:
40935 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40937 if (finish_p)
40938 return finish_omp_clauses (clauses, C_ORT_ACC);
40940 return clauses;
40943 /* Parse all OpenMP clauses. The set clauses allowed by the directive
40944 is a bitmask in MASK. Return the list of clauses found.
40945 FINISH_P set if finish_omp_clauses should be called.
40946 NESTED non-zero if clauses should be terminated by closing paren instead
40947 of end of pragma. If it is 2, additionally commas are required in between
40948 the clauses. */
40950 static tree
40951 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
40952 const char *where, cp_token *pragma_tok,
40953 bool finish_p = true, int nested = 0)
40955 tree clauses = NULL;
40956 bool first = true;
40957 cp_token *token = NULL;
40959 /* Don't create location wrapper nodes within OpenMP clauses. */
40960 auto_suppress_location_wrappers sentinel;
40962 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
40964 pragma_omp_clause c_kind;
40965 const char *c_name;
40966 tree prev = clauses;
40968 if (nested && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
40969 break;
40971 if (!first || nested != 2)
40973 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
40974 cp_lexer_consume_token (parser->lexer);
40975 else if (nested == 2)
40976 error_at (cp_lexer_peek_token (parser->lexer)->location,
40977 "clauses in %<simd%> trait should be separated "
40978 "by %<,%>");
40981 token = cp_lexer_peek_token (parser->lexer);
40982 c_kind = cp_parser_omp_clause_name (parser);
40984 switch (c_kind)
40986 case PRAGMA_OMP_CLAUSE_BIND:
40987 clauses = cp_parser_omp_clause_bind (parser, clauses,
40988 token->location);
40989 c_name = "bind";
40990 break;
40991 case PRAGMA_OMP_CLAUSE_COLLAPSE:
40992 clauses = cp_parser_omp_clause_collapse (parser, clauses,
40993 token->location);
40994 c_name = "collapse";
40995 break;
40996 case PRAGMA_OMP_CLAUSE_COPYIN:
40997 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
40998 c_name = "copyin";
40999 break;
41000 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
41001 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
41002 clauses);
41003 c_name = "copyprivate";
41004 break;
41005 case PRAGMA_OMP_CLAUSE_DEFAULT:
41006 clauses = cp_parser_omp_clause_default (parser, clauses,
41007 token->location, false);
41008 c_name = "default";
41009 break;
41010 case PRAGMA_OMP_CLAUSE_FILTER:
41011 clauses = cp_parser_omp_clause_filter (parser, clauses,
41012 token->location);
41013 c_name = "filter";
41014 break;
41015 case PRAGMA_OMP_CLAUSE_FINAL:
41016 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
41017 c_name = "final";
41018 break;
41019 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
41020 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
41021 clauses);
41022 c_name = "firstprivate";
41023 break;
41024 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
41025 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
41026 token->location);
41027 c_name = "grainsize";
41028 break;
41029 case PRAGMA_OMP_CLAUSE_HINT:
41030 clauses = cp_parser_omp_clause_hint (parser, clauses,
41031 token->location);
41032 c_name = "hint";
41033 break;
41034 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
41035 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
41036 token->location);
41037 c_name = "defaultmap";
41038 break;
41039 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
41040 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
41041 clauses);
41042 c_name = "use_device_ptr";
41043 break;
41044 case PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR:
41045 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_ADDR,
41046 clauses);
41047 c_name = "use_device_addr";
41048 break;
41049 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
41050 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
41051 clauses);
41052 c_name = "is_device_ptr";
41053 break;
41054 case PRAGMA_OMP_CLAUSE_HAS_DEVICE_ADDR:
41055 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_HAS_DEVICE_ADDR,
41056 clauses);
41057 c_name = "has_device_addr";
41058 break;
41059 case PRAGMA_OMP_CLAUSE_IF:
41060 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
41061 true);
41062 c_name = "if";
41063 break;
41064 case PRAGMA_OMP_CLAUSE_IN_REDUCTION:
41065 clauses
41066 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_IN_REDUCTION,
41067 true, clauses);
41068 c_name = "in_reduction";
41069 break;
41070 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
41071 clauses = cp_parser_omp_clause_lastprivate (parser, clauses);
41072 c_name = "lastprivate";
41073 break;
41074 case PRAGMA_OMP_CLAUSE_MERGEABLE:
41075 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
41076 token->location);
41077 c_name = "mergeable";
41078 break;
41079 case PRAGMA_OMP_CLAUSE_NOWAIT:
41080 clauses = cp_parser_omp_clause_nowait (parser, clauses,
41081 token->location);
41082 c_name = "nowait";
41083 break;
41084 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
41085 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
41086 token->location);
41087 c_name = "num_tasks";
41088 break;
41089 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
41090 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
41091 token->location);
41092 c_name = "num_threads";
41093 break;
41094 case PRAGMA_OMP_CLAUSE_ORDER:
41095 clauses = cp_parser_omp_clause_order (parser, clauses,
41096 token->location);
41097 c_name = "order";
41098 break;
41099 case PRAGMA_OMP_CLAUSE_ORDERED:
41100 clauses = cp_parser_omp_clause_ordered (parser, clauses,
41101 token->location);
41102 c_name = "ordered";
41103 break;
41104 case PRAGMA_OMP_CLAUSE_PRIORITY:
41105 clauses = cp_parser_omp_clause_priority (parser, clauses,
41106 token->location);
41107 c_name = "priority";
41108 break;
41109 case PRAGMA_OMP_CLAUSE_PRIVATE:
41110 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
41111 clauses);
41112 c_name = "private";
41113 break;
41114 case PRAGMA_OMP_CLAUSE_REDUCTION:
41115 clauses
41116 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
41117 true, clauses);
41118 c_name = "reduction";
41119 break;
41120 case PRAGMA_OMP_CLAUSE_SCHEDULE:
41121 clauses = cp_parser_omp_clause_schedule (parser, clauses,
41122 token->location);
41123 c_name = "schedule";
41124 break;
41125 case PRAGMA_OMP_CLAUSE_SHARED:
41126 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
41127 clauses);
41128 c_name = "shared";
41129 break;
41130 case PRAGMA_OMP_CLAUSE_TASK_REDUCTION:
41131 clauses
41132 = cp_parser_omp_clause_reduction (parser,
41133 OMP_CLAUSE_TASK_REDUCTION,
41134 true, clauses);
41135 c_name = "task_reduction";
41136 break;
41137 case PRAGMA_OMP_CLAUSE_UNTIED:
41138 clauses = cp_parser_omp_clause_untied (parser, clauses,
41139 token->location);
41140 c_name = "untied";
41141 break;
41142 case PRAGMA_OMP_CLAUSE_INBRANCH:
41143 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
41144 clauses, token->location);
41145 c_name = "inbranch";
41146 break;
41147 case PRAGMA_OMP_CLAUSE_NONTEMPORAL:
41148 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_NONTEMPORAL,
41149 clauses);
41150 c_name = "nontemporal";
41151 break;
41152 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
41153 clauses = cp_parser_omp_clause_branch (parser,
41154 OMP_CLAUSE_NOTINBRANCH,
41155 clauses, token->location);
41156 c_name = "notinbranch";
41157 break;
41158 case PRAGMA_OMP_CLAUSE_PARALLEL:
41159 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
41160 clauses, token->location);
41161 c_name = "parallel";
41162 if (!first)
41164 clause_not_first:
41165 error_at (token->location, "%qs must be the first clause of %qs",
41166 c_name, where);
41167 clauses = prev;
41169 break;
41170 case PRAGMA_OMP_CLAUSE_FOR:
41171 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
41172 clauses, token->location);
41173 c_name = "for";
41174 if (!first)
41175 goto clause_not_first;
41176 break;
41177 case PRAGMA_OMP_CLAUSE_SECTIONS:
41178 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
41179 clauses, token->location);
41180 c_name = "sections";
41181 if (!first)
41182 goto clause_not_first;
41183 break;
41184 case PRAGMA_OMP_CLAUSE_TASKGROUP:
41185 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
41186 clauses, token->location);
41187 c_name = "taskgroup";
41188 if (!first)
41189 goto clause_not_first;
41190 break;
41191 case PRAGMA_OMP_CLAUSE_LINK:
41192 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
41193 c_name = "link";
41194 break;
41195 case PRAGMA_OMP_CLAUSE_TO:
41196 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
41198 tree nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_ENTER,
41199 clauses);
41200 for (tree c = nl; c != clauses; c = OMP_CLAUSE_CHAIN (c))
41201 OMP_CLAUSE_ENTER_TO (c) = 1;
41202 clauses = nl;
41204 else
41205 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses,
41206 true);
41207 c_name = "to";
41208 break;
41209 case PRAGMA_OMP_CLAUSE_FROM:
41210 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses,
41211 true);
41212 c_name = "from";
41213 break;
41214 case PRAGMA_OMP_CLAUSE_UNIFORM:
41215 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
41216 clauses);
41217 c_name = "uniform";
41218 break;
41219 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
41220 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
41221 token->location);
41222 c_name = "num_teams";
41223 break;
41224 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
41225 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
41226 token->location);
41227 c_name = "thread_limit";
41228 break;
41229 case PRAGMA_OMP_CLAUSE_ALIGNED:
41230 clauses = cp_parser_omp_clause_aligned (parser, clauses);
41231 c_name = "aligned";
41232 break;
41233 case PRAGMA_OMP_CLAUSE_ALLOCATE:
41234 clauses = cp_parser_omp_clause_allocate (parser, clauses);
41235 c_name = "allocate";
41236 break;
41237 case PRAGMA_OMP_CLAUSE_LINEAR:
41239 bool declare_simd = false;
41240 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
41241 declare_simd = true;
41242 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
41244 c_name = "linear";
41245 break;
41246 case PRAGMA_OMP_CLAUSE_AFFINITY:
41247 clauses = cp_parser_omp_clause_affinity (parser, clauses);
41248 c_name = "affinity";
41249 break;
41250 case PRAGMA_OMP_CLAUSE_DEPEND:
41251 clauses = cp_parser_omp_clause_depend (parser, clauses,
41252 token->location);
41253 c_name = "depend";
41254 break;
41255 case PRAGMA_OMP_CLAUSE_DOACROSS:
41256 clauses = cp_parser_omp_clause_doacross (parser, clauses,
41257 token->location);
41258 c_name = "doacross";
41259 break;
41260 case PRAGMA_OMP_CLAUSE_DETACH:
41261 clauses = cp_parser_omp_clause_detach (parser, clauses);
41262 c_name = "detach";
41263 break;
41264 case PRAGMA_OMP_CLAUSE_MAP:
41265 clauses = cp_parser_omp_clause_map (parser, clauses);
41266 c_name = "map";
41267 break;
41268 case PRAGMA_OMP_CLAUSE_DEVICE:
41269 clauses = cp_parser_omp_clause_device (parser, clauses,
41270 token->location);
41271 c_name = "device";
41272 break;
41273 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
41274 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
41275 token->location);
41276 c_name = "dist_schedule";
41277 break;
41278 case PRAGMA_OMP_CLAUSE_PROC_BIND:
41279 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
41280 token->location);
41281 c_name = "proc_bind";
41282 break;
41283 case PRAGMA_OMP_CLAUSE_DEVICE_TYPE:
41284 clauses = cp_parser_omp_clause_device_type (parser, clauses,
41285 token->location);
41286 c_name = "device_type";
41287 break;
41288 case PRAGMA_OMP_CLAUSE_SAFELEN:
41289 clauses = cp_parser_omp_clause_safelen (parser, clauses,
41290 token->location);
41291 c_name = "safelen";
41292 break;
41293 case PRAGMA_OMP_CLAUSE_SIMDLEN:
41294 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
41295 token->location);
41296 c_name = "simdlen";
41297 break;
41298 case PRAGMA_OMP_CLAUSE_NOGROUP:
41299 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
41300 token->location);
41301 c_name = "nogroup";
41302 break;
41303 case PRAGMA_OMP_CLAUSE_THREADS:
41304 clauses
41305 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
41306 clauses, token->location);
41307 c_name = "threads";
41308 break;
41309 case PRAGMA_OMP_CLAUSE_SIMD:
41310 clauses
41311 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
41312 clauses, token->location);
41313 c_name = "simd";
41314 break;
41315 case PRAGMA_OMP_CLAUSE_ENTER:
41316 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_ENTER,
41317 clauses);
41318 c_name = "enter";
41319 break;
41320 default:
41321 cp_parser_error (parser, "expected %<#pragma omp%> clause");
41322 goto saw_error;
41325 first = false;
41327 if (((mask >> c_kind) & 1) == 0)
41329 /* Remove the invalid clause(s) from the list to avoid
41330 confusing the rest of the compiler. */
41331 clauses = prev;
41332 error_at (token->location, "%qs is not valid for %qs", c_name, where);
41335 saw_error:
41336 if (!nested)
41337 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41338 if (finish_p)
41340 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
41341 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
41342 else
41343 return finish_omp_clauses (clauses, C_ORT_OMP);
41345 return clauses;
41348 /* OpenMP 2.5:
41349 structured-block:
41350 statement
41352 In practice, we're also interested in adding the statement to an
41353 outer node. So it is convenient if we work around the fact that
41354 cp_parser_statement calls add_stmt. */
41356 static unsigned
41357 cp_parser_begin_omp_structured_block (cp_parser *parser)
41359 unsigned save = parser->in_statement;
41361 /* Only move the values to IN_OMP_BLOCK if they weren't false.
41362 This preserves the "not within loop or switch" style error messages
41363 for nonsense cases like
41364 void foo() {
41365 #pragma omp single
41366 break;
41369 if (parser->in_statement)
41370 parser->in_statement = IN_OMP_BLOCK;
41372 return save;
41375 static void
41376 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
41378 parser->in_statement = save;
41381 static tree
41382 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
41384 tree stmt = begin_omp_structured_block ();
41385 unsigned int save = cp_parser_begin_omp_structured_block (parser);
41387 parser->omp_attrs_forbidden_p = true;
41388 cp_parser_statement (parser, NULL_TREE, false, if_p);
41390 cp_parser_end_omp_structured_block (parser, save);
41391 return finish_omp_structured_block (stmt);
41394 /* OpenMP 5.0:
41395 # pragma omp allocate (list) [allocator(allocator)] */
41397 static void
41398 cp_parser_omp_allocate (cp_parser *parser, cp_token *pragma_tok)
41400 tree allocator = NULL_TREE;
41401 location_t loc = pragma_tok->location;
41402 tree nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_ALLOCATE, NULL_TREE);
41404 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
41405 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
41406 cp_lexer_consume_token (parser->lexer);
41408 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41410 matching_parens parens;
41411 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41412 const char *p = IDENTIFIER_POINTER (id);
41413 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
41414 cp_lexer_consume_token (parser->lexer);
41415 if (strcmp (p, "allocator") != 0)
41416 error_at (cloc, "expected %<allocator%>");
41417 else if (parens.require_open (parser))
41419 allocator = cp_parser_assignment_expression (parser);
41420 if (allocator == error_mark_node)
41421 allocator = NULL_TREE;
41422 parens.require_close (parser);
41425 cp_parser_require_pragma_eol (parser, pragma_tok);
41427 if (allocator)
41428 for (tree c = nl; c != NULL_TREE; c = OMP_CLAUSE_CHAIN (c))
41429 OMP_CLAUSE_ALLOCATE_ALLOCATOR (c) = allocator;
41431 sorry_at (loc, "%<#pragma omp allocate%> not yet supported");
41434 /* OpenMP 2.5:
41435 # pragma omp atomic new-line
41436 expression-stmt
41438 expression-stmt:
41439 x binop= expr | x++ | ++x | x-- | --x
41440 binop:
41441 +, *, -, /, &, ^, |, <<, >>
41443 where x is an lvalue expression with scalar type.
41445 OpenMP 3.1:
41446 # pragma omp atomic new-line
41447 update-stmt
41449 # pragma omp atomic read new-line
41450 read-stmt
41452 # pragma omp atomic write new-line
41453 write-stmt
41455 # pragma omp atomic update new-line
41456 update-stmt
41458 # pragma omp atomic capture new-line
41459 capture-stmt
41461 # pragma omp atomic capture new-line
41462 capture-block
41464 read-stmt:
41465 v = x
41466 write-stmt:
41467 x = expr
41468 update-stmt:
41469 expression-stmt | x = x binop expr
41470 capture-stmt:
41471 v = expression-stmt
41472 capture-block:
41473 { v = x; update-stmt; } | { update-stmt; v = x; }
41475 OpenMP 4.0:
41476 update-stmt:
41477 expression-stmt | x = x binop expr | x = expr binop x
41478 capture-stmt:
41479 v = update-stmt
41480 capture-block:
41481 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
41483 OpenMP 5.1:
41484 # pragma omp atomic compare new-line
41485 conditional-update-atomic
41487 # pragma omp atomic compare capture new-line
41488 conditional-update-capture-atomic
41490 conditional-update-atomic:
41491 cond-expr-stmt | cond-update-stmt
41492 cond-expr-stmt:
41493 x = expr ordop x ? expr : x;
41494 x = x ordop expr ? expr : x;
41495 x = x == e ? d : x;
41496 cond-update-stmt:
41497 if (expr ordop x) { x = expr; }
41498 if (x ordop expr) { x = expr; }
41499 if (x == e) { x = d; }
41500 ordop:
41501 <, >
41502 conditional-update-capture-atomic:
41503 v = cond-expr-stmt
41504 { v = x; cond-expr-stmt }
41505 { cond-expr-stmt v = x; }
41506 { v = x; cond-update-stmt }
41507 { cond-update-stmt v = x; }
41508 if (x == e) { x = d; } else { v = x; }
41509 { r = x == e; if (r) { x = d; } }
41510 { r = x == e; if (r) { x = d; } else { v = x; } }
41512 where x, r and v are lvalue expressions with scalar type,
41513 expr, e and d are expressions with scalar type and e might be
41514 the same as v. */
41516 static void
41517 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok, bool openacc)
41519 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
41520 tree rhs1 = NULL_TREE, orig_lhs, r = NULL_TREE;
41521 location_t loc = pragma_tok->location;
41522 enum tree_code code = ERROR_MARK, opcode = NOP_EXPR;
41523 enum omp_memory_order memory_order = OMP_MEMORY_ORDER_UNSPECIFIED;
41524 bool structured_block = false;
41525 tree clauses = NULL_TREE;
41526 bool capture = false;
41527 bool compare = false;
41528 bool weak = false;
41529 enum omp_memory_order fail = OMP_MEMORY_ORDER_UNSPECIFIED;
41530 bool no_semicolon = false;
41531 bool extra_scope = false;
41533 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
41535 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
41536 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
41537 cp_lexer_consume_token (parser->lexer);
41539 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41541 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41542 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
41543 const char *p = IDENTIFIER_POINTER (id);
41544 enum tree_code new_code = ERROR_MARK;
41545 enum omp_memory_order new_memory_order
41546 = OMP_MEMORY_ORDER_UNSPECIFIED;
41547 bool new_capture = false;
41548 bool new_compare = false;
41549 bool new_weak = false;
41550 enum omp_memory_order new_fail = OMP_MEMORY_ORDER_UNSPECIFIED;
41552 if (!strcmp (p, "read"))
41553 new_code = OMP_ATOMIC_READ;
41554 else if (!strcmp (p, "write"))
41555 new_code = NOP_EXPR;
41556 else if (!strcmp (p, "update"))
41557 new_code = OMP_ATOMIC;
41558 else if (openacc && !strcmp (p, "capture"))
41559 new_code = OMP_ATOMIC_CAPTURE_NEW;
41560 else if (openacc)
41562 p = NULL;
41563 error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
41564 "or %<capture%> clause");
41566 else if (!strcmp (p, "capture"))
41567 new_capture = true;
41568 else if (!strcmp (p, "compare"))
41569 new_compare = true;
41570 else if (!strcmp (p, "weak"))
41571 new_weak = true;
41572 else if (!strcmp (p, "fail"))
41574 matching_parens parens;
41576 cp_lexer_consume_token (parser->lexer);
41577 if (!parens.require_open (parser))
41578 continue;
41580 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41582 id = cp_lexer_peek_token (parser->lexer)->u.value;
41583 const char *q = IDENTIFIER_POINTER (id);
41585 if (!strcmp (q, "seq_cst"))
41586 new_fail = OMP_MEMORY_ORDER_SEQ_CST;
41587 else if (!strcmp (q, "acquire"))
41588 new_fail = OMP_MEMORY_ORDER_ACQUIRE;
41589 else if (!strcmp (q, "relaxed"))
41590 new_fail = OMP_MEMORY_ORDER_RELAXED;
41593 if (new_fail != OMP_MEMORY_ORDER_UNSPECIFIED)
41595 cp_lexer_consume_token (parser->lexer);
41596 if (fail != OMP_MEMORY_ORDER_UNSPECIFIED)
41597 error_at (cloc, "too many %qs clauses", "fail");
41598 else
41599 fail = new_fail;
41601 else
41602 cp_parser_error (parser, "expected %<seq_cst%>, %<acquire%> "
41603 "or %<relaxed%>");
41604 if (new_fail == OMP_MEMORY_ORDER_UNSPECIFIED
41605 || !parens.require_close (parser))
41606 cp_parser_skip_to_closing_parenthesis (parser,
41607 /*recovering=*/true,
41608 /*or_comma=*/false,
41609 /*consume_paren=*/true);
41610 continue;
41612 else if (!strcmp (p, "seq_cst"))
41613 new_memory_order = OMP_MEMORY_ORDER_SEQ_CST;
41614 else if (!strcmp (p, "acq_rel"))
41615 new_memory_order = OMP_MEMORY_ORDER_ACQ_REL;
41616 else if (!strcmp (p, "release"))
41617 new_memory_order = OMP_MEMORY_ORDER_RELEASE;
41618 else if (!strcmp (p, "acquire"))
41619 new_memory_order = OMP_MEMORY_ORDER_ACQUIRE;
41620 else if (!strcmp (p, "relaxed"))
41621 new_memory_order = OMP_MEMORY_ORDER_RELAXED;
41622 else if (!strcmp (p, "hint"))
41624 cp_lexer_consume_token (parser->lexer);
41625 clauses = cp_parser_omp_clause_hint (parser, clauses, cloc);
41626 continue;
41628 else
41630 p = NULL;
41631 error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
41632 "%<capture%>, %<compare%>, %<weak%>, %<fail%>, "
41633 "%<seq_cst%>, %<acq_rel%>, %<release%>, "
41634 "%<relaxed%> or %<hint%> clause");
41636 if (p)
41638 if (new_code != ERROR_MARK)
41640 /* OpenACC permits 'update capture'. */
41641 if (openacc
41642 && code == OMP_ATOMIC
41643 && new_code == OMP_ATOMIC_CAPTURE_NEW)
41644 code = new_code;
41645 else if (code != ERROR_MARK)
41646 error_at (cloc, "too many atomic clauses");
41647 else
41648 code = new_code;
41650 else if (new_memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
41652 if (memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
41653 error_at (cloc, "too many memory order clauses");
41654 else
41655 memory_order = new_memory_order;
41657 else if (new_capture)
41659 if (capture)
41660 error_at (cloc, "too many %qs clauses", "capture");
41661 else
41662 capture = true;
41664 else if (new_compare)
41666 if (compare)
41667 error_at (cloc, "too many %qs clauses", "compare");
41668 else
41669 compare = true;
41671 else if (new_weak)
41673 if (weak)
41674 error_at (cloc, "too many %qs clauses", "weak");
41675 else
41676 weak = true;
41678 cp_lexer_consume_token (parser->lexer);
41679 continue;
41682 break;
41684 cp_parser_require_pragma_eol (parser, pragma_tok);
41686 if (code == ERROR_MARK)
41687 code = OMP_ATOMIC;
41688 if (capture)
41690 if (code != OMP_ATOMIC)
41691 error_at (loc, "%qs clause is incompatible with %<read%> or %<write%> "
41692 "clauses", "capture");
41693 else
41694 code = OMP_ATOMIC_CAPTURE_NEW;
41696 if (compare && code != OMP_ATOMIC && code != OMP_ATOMIC_CAPTURE_NEW)
41698 error_at (loc, "%qs clause is incompatible with %<read%> or %<write%> "
41699 "clauses", "compare");
41700 compare = false;
41702 if (fail != OMP_MEMORY_ORDER_UNSPECIFIED && !compare)
41704 error_at (loc, "%qs clause requires %qs clause", "fail", "compare");
41705 fail = OMP_MEMORY_ORDER_UNSPECIFIED;
41707 if (weak && !compare)
41709 error_at (loc, "%qs clause requires %qs clause", "weak", "compare");
41710 weak = false;
41712 if (openacc)
41713 memory_order = OMP_MEMORY_ORDER_RELAXED;
41714 else if (memory_order == OMP_MEMORY_ORDER_UNSPECIFIED)
41716 omp_requires_mask
41717 = (enum omp_requires) (omp_requires_mask
41718 | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED);
41719 switch ((enum omp_memory_order)
41720 (omp_requires_mask & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER))
41722 case OMP_MEMORY_ORDER_UNSPECIFIED:
41723 case OMP_MEMORY_ORDER_RELAXED:
41724 memory_order = OMP_MEMORY_ORDER_RELAXED;
41725 break;
41726 case OMP_MEMORY_ORDER_SEQ_CST:
41727 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
41728 break;
41729 case OMP_MEMORY_ORDER_ACQ_REL:
41730 switch (code)
41732 case OMP_ATOMIC_READ:
41733 memory_order = OMP_MEMORY_ORDER_ACQUIRE;
41734 break;
41735 case NOP_EXPR: /* atomic write */
41736 memory_order = OMP_MEMORY_ORDER_RELEASE;
41737 break;
41738 default:
41739 memory_order = OMP_MEMORY_ORDER_ACQ_REL;
41740 break;
41742 break;
41743 default:
41744 gcc_unreachable ();
41747 else
41748 switch (code)
41750 case OMP_ATOMIC_READ:
41751 if (memory_order == OMP_MEMORY_ORDER_RELEASE)
41753 error_at (loc, "%<#pragma omp atomic read%> incompatible with "
41754 "%<release%> clause");
41755 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
41757 else if (memory_order == OMP_MEMORY_ORDER_ACQ_REL)
41758 memory_order = OMP_MEMORY_ORDER_ACQUIRE;
41759 break;
41760 case NOP_EXPR: /* atomic write */
41761 if (memory_order == OMP_MEMORY_ORDER_ACQUIRE)
41763 error_at (loc, "%<#pragma omp atomic write%> incompatible with "
41764 "%<acquire%> clause");
41765 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
41767 else if (memory_order == OMP_MEMORY_ORDER_ACQ_REL)
41768 memory_order = OMP_MEMORY_ORDER_RELEASE;
41769 break;
41770 default:
41771 break;
41773 if (fail != OMP_MEMORY_ORDER_UNSPECIFIED)
41774 memory_order
41775 = (enum omp_memory_order) (memory_order
41776 | (fail << OMP_FAIL_MEMORY_ORDER_SHIFT));
41778 switch (code)
41780 case OMP_ATOMIC_READ:
41781 case NOP_EXPR: /* atomic write */
41782 v = cp_parser_unary_expression (parser);
41783 if (v == error_mark_node)
41784 goto saw_error;
41785 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
41786 goto saw_error;
41787 if (code == NOP_EXPR)
41788 lhs = cp_parser_expression (parser);
41789 else
41790 lhs = cp_parser_unary_expression (parser);
41791 if (lhs == error_mark_node)
41792 goto saw_error;
41793 if (code == NOP_EXPR)
41795 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
41796 opcode. */
41797 code = OMP_ATOMIC;
41798 rhs = lhs;
41799 lhs = v;
41800 v = NULL_TREE;
41802 goto done;
41803 case OMP_ATOMIC_CAPTURE_NEW:
41804 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
41806 cp_lexer_consume_token (parser->lexer);
41807 structured_block = true;
41809 else if (compare
41810 && cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
41811 break;
41812 else
41814 v = cp_parser_unary_expression (parser);
41815 if (v == error_mark_node)
41816 goto saw_error;
41817 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
41818 goto saw_error;
41819 if (compare
41820 && cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
41822 location_t eloc = cp_lexer_peek_token (parser->lexer)->location;
41823 error_at (eloc, "expected expression");
41824 goto saw_error;
41827 default:
41828 break;
41831 restart:
41832 if (compare && cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
41834 cp_lexer_consume_token (parser->lexer);
41836 matching_parens parens;
41837 if (!parens.require_open (parser))
41838 goto saw_error;
41839 location_t eloc = cp_lexer_peek_token (parser->lexer)->location;
41840 tree cmp_expr;
41841 if (r)
41842 cmp_expr = cp_parser_unary_expression (parser);
41843 else
41844 cmp_expr = cp_parser_binary_expression (parser, false, true,
41845 PREC_NOT_OPERATOR, NULL);
41846 if (!parens.require_close (parser))
41847 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
41848 if (cmp_expr == error_mark_node)
41849 goto saw_error;
41850 if (r)
41852 if (!cp_tree_equal (cmp_expr, r))
41853 goto bad_if;
41854 cmp_expr = rhs;
41855 rhs = NULL_TREE;
41856 gcc_assert (TREE_CODE (cmp_expr) == EQ_EXPR);
41858 if (TREE_CODE (cmp_expr) == EQ_EXPR)
41860 else if (!structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
41862 error_at (EXPR_LOC_OR_LOC (cmp_expr, eloc),
41863 "expected %<==%> comparison in %<if%> condition");
41864 goto saw_error;
41866 else if (TREE_CODE (cmp_expr) != GT_EXPR
41867 && TREE_CODE (cmp_expr) != LT_EXPR)
41869 error_at (EXPR_LOC_OR_LOC (cmp_expr, eloc),
41870 "expected %<==%>, %<<%> or %<>%> comparison in %<if%> "
41871 "condition");
41872 goto saw_error;
41874 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
41875 goto saw_error;
41877 extra_scope = true;
41878 eloc = cp_lexer_peek_token (parser->lexer)->location;
41879 lhs = cp_parser_unary_expression (parser);
41880 orig_lhs = lhs;
41881 if (lhs == error_mark_node)
41882 goto saw_error;
41883 if (!cp_lexer_next_token_is (parser->lexer, CPP_EQ))
41885 cp_parser_error (parser, "expected %<=%>");
41886 goto saw_error;
41888 cp_lexer_consume_token (parser->lexer);
41889 eloc = cp_lexer_peek_token (parser->lexer)->location;
41890 if (TREE_CODE (cmp_expr) == EQ_EXPR)
41891 rhs1 = cp_parser_expression (parser);
41892 else
41893 rhs1 = cp_parser_simple_cast_expression (parser);
41895 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
41896 goto saw_error;
41898 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
41899 goto saw_error;
41901 extra_scope = false;
41902 no_semicolon = true;
41904 if (cp_tree_equal (TREE_OPERAND (cmp_expr, 0), lhs))
41906 if (TREE_CODE (cmp_expr) == EQ_EXPR)
41908 opcode = COND_EXPR;
41909 rhs = TREE_OPERAND (cmp_expr, 1);
41911 else if (cp_tree_equal (TREE_OPERAND (cmp_expr, 1), rhs1))
41913 opcode = (TREE_CODE (cmp_expr) == GT_EXPR
41914 ? MIN_EXPR : MAX_EXPR);
41915 rhs = rhs1;
41916 rhs1 = TREE_OPERAND (cmp_expr, 0);
41918 else
41919 goto bad_if;
41921 else if (TREE_CODE (cmp_expr) == EQ_EXPR)
41922 goto bad_if;
41923 else if (cp_tree_equal (TREE_OPERAND (cmp_expr, 1), lhs)
41924 && cp_tree_equal (TREE_OPERAND (cmp_expr, 0), rhs1))
41926 opcode = (TREE_CODE (cmp_expr) == GT_EXPR
41927 ? MAX_EXPR : MIN_EXPR);
41928 rhs = rhs1;
41929 rhs1 = TREE_OPERAND (cmp_expr, 1);
41931 else
41933 bad_if:
41934 cp_parser_error (parser,
41935 "invalid form of %<#pragma omp atomic compare%>");
41936 goto saw_error;
41939 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
41941 if (code != OMP_ATOMIC_CAPTURE_NEW
41942 || (structured_block && r == NULL_TREE)
41943 || TREE_CODE (cmp_expr) != EQ_EXPR)
41945 eloc = cp_lexer_peek_token (parser->lexer)->location;
41946 error_at (eloc, "unexpected %<else%>");
41947 goto saw_error;
41950 cp_lexer_consume_token (parser->lexer);
41952 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
41953 goto saw_error;
41955 extra_scope = true;
41956 v = cp_parser_unary_expression (parser);
41957 if (v == error_mark_node)
41958 goto saw_error;
41959 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
41960 goto saw_error;
41962 tree expr = cp_parser_simple_cast_expression (parser);
41964 if (!cp_tree_equal (expr, lhs))
41965 goto bad_if;
41967 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
41968 goto saw_error;
41970 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
41971 goto saw_error;
41973 extra_scope = false;
41974 code = OMP_ATOMIC_CAPTURE_OLD;
41975 if (r == NULL_TREE)
41976 /* Signal to c_finish_omp_atomic that in
41977 if (x == e) { x = d; } else { v = x; }
41978 case the store to v should be conditional. */
41979 r = void_list_node;
41981 else if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
41983 cp_parser_error (parser, "expected %<else%>");
41984 goto saw_error;
41986 else if (code == OMP_ATOMIC_CAPTURE_NEW
41987 && r != NULL_TREE
41988 && v == NULL_TREE)
41989 code = OMP_ATOMIC;
41990 goto stmt_done;
41992 lhs = cp_parser_unary_expression (parser);
41993 orig_lhs = lhs;
41994 switch (TREE_CODE (lhs))
41996 case ERROR_MARK:
41997 goto saw_error;
41999 case POSTINCREMENT_EXPR:
42000 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
42001 code = OMP_ATOMIC_CAPTURE_OLD;
42002 /* FALLTHROUGH */
42003 case PREINCREMENT_EXPR:
42004 lhs = TREE_OPERAND (lhs, 0);
42005 opcode = PLUS_EXPR;
42006 rhs = integer_one_node;
42007 if (compare)
42008 goto invalid_compare;
42009 break;
42011 case POSTDECREMENT_EXPR:
42012 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
42013 code = OMP_ATOMIC_CAPTURE_OLD;
42014 /* FALLTHROUGH */
42015 case PREDECREMENT_EXPR:
42016 lhs = TREE_OPERAND (lhs, 0);
42017 opcode = MINUS_EXPR;
42018 rhs = integer_one_node;
42019 if (compare)
42020 goto invalid_compare;
42021 break;
42023 case COMPOUND_EXPR:
42024 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
42025 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
42026 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
42027 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
42028 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
42029 (TREE_OPERAND (lhs, 1), 0), 0)))
42030 == BOOLEAN_TYPE)
42031 /* Undo effects of boolean_increment for post {in,de}crement. */
42032 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
42033 /* FALLTHRU */
42034 case MODIFY_EXPR:
42035 if (TREE_CODE (lhs) == MODIFY_EXPR
42036 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
42038 /* Undo effects of boolean_increment. */
42039 if (integer_onep (TREE_OPERAND (lhs, 1)))
42041 /* This is pre or post increment. */
42042 rhs = TREE_OPERAND (lhs, 1);
42043 lhs = TREE_OPERAND (lhs, 0);
42044 opcode = NOP_EXPR;
42045 if (code == OMP_ATOMIC_CAPTURE_NEW
42046 && !structured_block
42047 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
42048 code = OMP_ATOMIC_CAPTURE_OLD;
42049 if (compare)
42050 goto invalid_compare;
42051 break;
42054 /* FALLTHRU */
42055 default:
42056 if (compare && !cp_lexer_next_token_is (parser->lexer, CPP_EQ))
42058 cp_parser_error (parser, "expected %<=%>");
42059 goto saw_error;
42061 switch (cp_lexer_peek_token (parser->lexer)->type)
42063 case CPP_MULT_EQ:
42064 opcode = MULT_EXPR;
42065 break;
42066 case CPP_DIV_EQ:
42067 opcode = TRUNC_DIV_EXPR;
42068 break;
42069 case CPP_PLUS_EQ:
42070 opcode = PLUS_EXPR;
42071 break;
42072 case CPP_MINUS_EQ:
42073 opcode = MINUS_EXPR;
42074 break;
42075 case CPP_LSHIFT_EQ:
42076 opcode = LSHIFT_EXPR;
42077 break;
42078 case CPP_RSHIFT_EQ:
42079 opcode = RSHIFT_EXPR;
42080 break;
42081 case CPP_AND_EQ:
42082 opcode = BIT_AND_EXPR;
42083 break;
42084 case CPP_OR_EQ:
42085 opcode = BIT_IOR_EXPR;
42086 break;
42087 case CPP_XOR_EQ:
42088 opcode = BIT_XOR_EXPR;
42089 break;
42090 case CPP_EQ:
42091 enum cp_parser_prec oprec;
42092 cp_token *token;
42093 cp_lexer_consume_token (parser->lexer);
42094 cp_parser_parse_tentatively (parser);
42095 rhs1 = cp_parser_simple_cast_expression (parser);
42096 if (rhs1 == error_mark_node)
42098 cp_parser_abort_tentative_parse (parser);
42099 cp_parser_simple_cast_expression (parser);
42100 goto saw_error;
42102 token = cp_lexer_peek_token (parser->lexer);
42103 if (token->type != CPP_SEMICOLON
42104 && (!compare || token->type != CPP_QUERY)
42105 && !cp_tree_equal (lhs, rhs1))
42107 cp_parser_abort_tentative_parse (parser);
42108 cp_parser_parse_tentatively (parser);
42109 rhs = cp_parser_binary_expression (parser, false, true,
42110 PREC_NOT_OPERATOR, NULL);
42111 if (rhs == error_mark_node)
42113 cp_parser_abort_tentative_parse (parser);
42114 cp_parser_binary_expression (parser, false, true,
42115 PREC_NOT_OPERATOR, NULL);
42116 goto saw_error;
42118 switch (TREE_CODE (rhs))
42120 case MULT_EXPR:
42121 case TRUNC_DIV_EXPR:
42122 case RDIV_EXPR:
42123 case PLUS_EXPR:
42124 case MINUS_EXPR:
42125 case LSHIFT_EXPR:
42126 case RSHIFT_EXPR:
42127 case BIT_AND_EXPR:
42128 case BIT_IOR_EXPR:
42129 case BIT_XOR_EXPR:
42130 if (compare)
42131 break;
42132 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
42134 if (cp_parser_parse_definitely (parser))
42136 opcode = TREE_CODE (rhs);
42137 rhs1 = TREE_OPERAND (rhs, 0);
42138 rhs = TREE_OPERAND (rhs, 1);
42139 goto stmt_done;
42141 else
42142 goto saw_error;
42144 break;
42145 case EQ_EXPR:
42146 if (!compare
42147 || code != OMP_ATOMIC_CAPTURE_NEW
42148 || !structured_block
42149 || v
42150 || r)
42151 break;
42152 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
42153 && cp_lexer_nth_token_is_keyword (parser->lexer,
42154 2, RID_IF))
42156 if (cp_parser_parse_definitely (parser))
42158 r = lhs;
42159 lhs = NULL_TREE;
42160 rhs1 = NULL_TREE;
42161 cp_lexer_consume_token (parser->lexer);
42162 goto restart;
42165 break;
42166 case GT_EXPR:
42167 case LT_EXPR:
42168 if (compare
42169 && cp_lexer_next_token_is (parser->lexer, CPP_QUERY)
42170 && cp_tree_equal (lhs, TREE_OPERAND (rhs, 1))
42171 && cp_parser_parse_definitely (parser))
42173 opcode = TREE_CODE (rhs);
42174 rhs1 = TREE_OPERAND (rhs, 0);
42175 rhs = TREE_OPERAND (rhs, 1);
42176 cond_expr:
42177 cp_lexer_consume_token (parser->lexer);
42178 bool saved_colon_corrects_to_scope_p
42179 = parser->colon_corrects_to_scope_p;
42180 parser->colon_corrects_to_scope_p = false;
42181 tree e1 = cp_parser_expression (parser);
42182 parser->colon_corrects_to_scope_p
42183 = saved_colon_corrects_to_scope_p;
42184 cp_parser_require (parser, CPP_COLON, RT_COLON);
42185 tree e2 = cp_parser_simple_cast_expression (parser);
42186 if (cp_tree_equal (lhs, e2))
42188 if (cp_tree_equal (lhs, rhs1))
42190 if (opcode == EQ_EXPR)
42192 opcode = COND_EXPR;
42193 rhs1 = e1;
42194 goto stmt_done;
42196 if (cp_tree_equal (rhs, e1))
42198 opcode
42199 = opcode == GT_EXPR ? MIN_EXPR : MAX_EXPR;
42200 rhs = e1;
42201 goto stmt_done;
42204 else
42206 gcc_assert (opcode != EQ_EXPR);
42207 if (cp_tree_equal (rhs1, e1))
42209 opcode
42210 = opcode == GT_EXPR ? MAX_EXPR : MIN_EXPR;
42211 rhs1 = rhs;
42212 rhs = e1;
42213 goto stmt_done;
42217 cp_parser_error (parser,
42218 "invalid form of "
42219 "%<#pragma omp atomic compare%>");
42220 goto saw_error;
42222 break;
42223 default:
42224 break;
42226 cp_parser_abort_tentative_parse (parser);
42227 if (structured_block
42228 && code == OMP_ATOMIC_CAPTURE_OLD
42229 && !compare)
42231 rhs = cp_parser_expression (parser);
42232 if (rhs == error_mark_node)
42233 goto saw_error;
42234 opcode = NOP_EXPR;
42235 rhs1 = NULL_TREE;
42236 goto stmt_done;
42238 cp_parser_error (parser,
42239 "invalid form of %<#pragma omp atomic%>");
42240 goto saw_error;
42242 if (!cp_parser_parse_definitely (parser))
42243 goto saw_error;
42244 switch (token->type)
42246 case CPP_SEMICOLON:
42247 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
42249 code = OMP_ATOMIC_CAPTURE_OLD;
42250 v = lhs;
42251 lhs = NULL_TREE;
42252 lhs1 = rhs1;
42253 rhs1 = NULL_TREE;
42254 cp_lexer_consume_token (parser->lexer);
42255 goto restart;
42257 else if (structured_block && !compare)
42259 opcode = NOP_EXPR;
42260 rhs = rhs1;
42261 rhs1 = NULL_TREE;
42262 goto stmt_done;
42264 cp_parser_error (parser,
42265 "invalid form of %<#pragma omp atomic%>");
42266 goto saw_error;
42267 case CPP_MULT:
42268 opcode = MULT_EXPR;
42269 break;
42270 case CPP_DIV:
42271 opcode = TRUNC_DIV_EXPR;
42272 break;
42273 case CPP_PLUS:
42274 opcode = PLUS_EXPR;
42275 break;
42276 case CPP_MINUS:
42277 opcode = MINUS_EXPR;
42278 break;
42279 case CPP_LSHIFT:
42280 opcode = LSHIFT_EXPR;
42281 break;
42282 case CPP_RSHIFT:
42283 opcode = RSHIFT_EXPR;
42284 break;
42285 case CPP_AND:
42286 opcode = BIT_AND_EXPR;
42287 break;
42288 case CPP_OR:
42289 opcode = BIT_IOR_EXPR;
42290 break;
42291 case CPP_XOR:
42292 opcode = BIT_XOR_EXPR;
42293 break;
42294 case CPP_EQ_EQ:
42295 opcode = EQ_EXPR;
42296 break;
42297 case CPP_GREATER:
42298 opcode = GT_EXPR;
42299 break;
42300 case CPP_LESS:
42301 opcode = LT_EXPR;
42302 break;
42303 default:
42304 cp_parser_error (parser,
42305 "invalid operator for %<#pragma omp atomic%>");
42306 goto saw_error;
42308 if (compare
42309 && TREE_CODE_CLASS (opcode) != tcc_comparison)
42311 cp_parser_error (parser,
42312 "invalid form of "
42313 "%<#pragma omp atomic compare%>");
42314 goto saw_error;
42316 oprec = TOKEN_PRECEDENCE (token);
42317 gcc_assert (oprec != PREC_NOT_OPERATOR);
42318 if (commutative_tree_code (opcode))
42319 oprec = (enum cp_parser_prec) (oprec - 1);
42320 cp_lexer_consume_token (parser->lexer);
42321 rhs = cp_parser_binary_expression (parser, false, false,
42322 oprec, NULL);
42323 if (rhs == error_mark_node)
42324 goto saw_error;
42325 if (compare)
42327 if (!cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
42329 cp_parser_error (parser,
42330 "invalid form of "
42331 "%<#pragma omp atomic compare%>");
42332 goto saw_error;
42334 goto cond_expr;
42336 goto stmt_done;
42337 default:
42338 cp_parser_error (parser,
42339 "invalid operator for %<#pragma omp atomic%>");
42340 goto saw_error;
42342 cp_lexer_consume_token (parser->lexer);
42344 rhs = cp_parser_expression (parser);
42345 if (rhs == error_mark_node)
42346 goto saw_error;
42347 break;
42349 stmt_done:
42350 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW && r == NULL_TREE)
42352 if (!no_semicolon
42353 && !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
42354 goto saw_error;
42355 no_semicolon = false;
42356 v = cp_parser_unary_expression (parser);
42357 if (v == error_mark_node)
42358 goto saw_error;
42359 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
42360 goto saw_error;
42361 lhs1 = cp_parser_unary_expression (parser);
42362 if (lhs1 == error_mark_node)
42363 goto saw_error;
42365 if (structured_block)
42367 if (!no_semicolon)
42368 cp_parser_consume_semicolon_at_end_of_statement (parser);
42369 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
42371 done:
42372 if (weak && opcode != COND_EXPR)
42374 error_at (loc, "%<weak%> clause requires atomic equality comparison");
42375 weak = false;
42377 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
42378 finish_omp_atomic (pragma_tok->location, code, opcode, lhs, rhs, v, lhs1,
42379 rhs1, r, clauses, memory_order, weak);
42380 if (!structured_block && !no_semicolon)
42381 cp_parser_consume_semicolon_at_end_of_statement (parser);
42382 return;
42384 invalid_compare:
42385 error ("invalid form of %<pragma omp atomic compare%>");
42386 /* FALLTHRU */
42387 saw_error:
42388 cp_parser_skip_to_end_of_block_or_statement (parser);
42389 if (extra_scope && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
42390 cp_lexer_consume_token (parser->lexer);
42391 if (structured_block)
42393 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
42394 cp_lexer_consume_token (parser->lexer);
42395 else if (code == OMP_ATOMIC_CAPTURE_NEW)
42397 cp_parser_skip_to_end_of_block_or_statement (parser);
42398 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
42399 cp_lexer_consume_token (parser->lexer);
42405 /* OpenMP 2.5:
42406 # pragma omp barrier new-line */
42408 static void
42409 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
42411 cp_parser_require_pragma_eol (parser, pragma_tok);
42412 finish_omp_barrier ();
42415 /* OpenMP 2.5:
42416 # pragma omp critical [(name)] new-line
42417 structured-block
42419 OpenMP 4.5:
42420 # pragma omp critical [(name) [hint(expression)]] new-line
42421 structured-block */
42423 #define OMP_CRITICAL_CLAUSE_MASK \
42424 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
42426 static tree
42427 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
42429 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
42431 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
42433 matching_parens parens;
42434 parens.consume_open (parser);
42436 name = cp_parser_identifier (parser);
42438 if (name == error_mark_node
42439 || !parens.require_close (parser))
42440 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
42441 /*or_comma=*/false,
42442 /*consume_paren=*/true);
42443 if (name == error_mark_node)
42444 name = NULL;
42446 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
42447 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
42448 cp_lexer_consume_token (parser->lexer);
42451 clauses = cp_parser_omp_all_clauses (parser, OMP_CRITICAL_CLAUSE_MASK,
42452 "#pragma omp critical", pragma_tok);
42454 stmt = cp_parser_omp_structured_block (parser, if_p);
42455 return c_finish_omp_critical (input_location, stmt, name, clauses);
42458 /* OpenMP 5.0:
42459 # pragma omp depobj ( depobj ) depobj-clause new-line
42461 depobj-clause:
42462 depend (dependence-type : locator)
42463 destroy
42464 update (dependence-type)
42466 dependence-type:
42469 inout
42470 mutexinout */
42472 static void
42473 cp_parser_omp_depobj (cp_parser *parser, cp_token *pragma_tok)
42475 location_t loc = pragma_tok->location;
42476 matching_parens parens;
42477 if (!parens.require_open (parser))
42479 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
42480 return;
42483 tree depobj = cp_parser_assignment_expression (parser);
42485 if (!parens.require_close (parser))
42486 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
42487 /*or_comma=*/false,
42488 /*consume_paren=*/true);
42490 tree clause = NULL_TREE;
42491 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INVALID;
42492 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
42493 cp_lexer_consume_token (parser->lexer);
42494 location_t c_loc = cp_lexer_peek_token (parser->lexer)->location;
42495 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42497 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
42498 const char *p = IDENTIFIER_POINTER (id);
42500 cp_lexer_consume_token (parser->lexer);
42501 if (!strcmp ("depend", p))
42503 /* Don't create location wrapper nodes within the depend clause. */
42504 auto_suppress_location_wrappers sentinel;
42505 clause = cp_parser_omp_clause_depend (parser, NULL_TREE, c_loc);
42506 if (clause)
42507 clause = finish_omp_clauses (clause, C_ORT_OMP);
42508 if (!clause)
42509 clause = error_mark_node;
42511 else if (!strcmp ("destroy", p))
42512 kind = OMP_CLAUSE_DEPEND_LAST;
42513 else if (!strcmp ("update", p))
42515 matching_parens c_parens;
42516 if (c_parens.require_open (parser))
42518 location_t c2_loc
42519 = cp_lexer_peek_token (parser->lexer)->location;
42520 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42522 tree id2 = cp_lexer_peek_token (parser->lexer)->u.value;
42523 const char *p2 = IDENTIFIER_POINTER (id2);
42525 cp_lexer_consume_token (parser->lexer);
42526 if (!strcmp ("in", p2))
42527 kind = OMP_CLAUSE_DEPEND_IN;
42528 else if (!strcmp ("out", p2))
42529 kind = OMP_CLAUSE_DEPEND_OUT;
42530 else if (!strcmp ("inout", p2))
42531 kind = OMP_CLAUSE_DEPEND_INOUT;
42532 else if (!strcmp ("mutexinoutset", p2))
42533 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
42534 else if (!strcmp ("inoutset", p2))
42535 kind = OMP_CLAUSE_DEPEND_INOUTSET;
42537 if (kind == OMP_CLAUSE_DEPEND_INVALID)
42539 clause = error_mark_node;
42540 error_at (c2_loc, "expected %<in%>, %<out%>, %<inout%>, "
42541 "%<mutexinoutset%> or %<inoutset%>");
42543 if (!c_parens.require_close (parser))
42544 cp_parser_skip_to_closing_parenthesis (parser,
42545 /*recovering=*/true,
42546 /*or_comma=*/false,
42547 /*consume_paren=*/true);
42549 else
42550 clause = error_mark_node;
42553 if (!clause && kind == OMP_CLAUSE_DEPEND_INVALID)
42555 clause = error_mark_node;
42556 error_at (c_loc, "expected %<depend%>, %<destroy%> or %<update%> clause");
42558 cp_parser_require_pragma_eol (parser, pragma_tok);
42560 finish_omp_depobj (loc, depobj, kind, clause);
42564 /* OpenMP 2.5:
42565 # pragma omp flush flush-vars[opt] new-line
42567 flush-vars:
42568 ( variable-list )
42570 OpenMP 5.0:
42571 # pragma omp flush memory-order-clause new-line */
42573 static void
42574 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
42576 enum memmodel mo = MEMMODEL_LAST;
42577 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
42578 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
42579 cp_lexer_consume_token (parser->lexer);
42580 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42582 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
42583 const char *p = IDENTIFIER_POINTER (id);
42584 if (!strcmp (p, "seq_cst"))
42585 mo = MEMMODEL_SEQ_CST;
42586 else if (!strcmp (p, "acq_rel"))
42587 mo = MEMMODEL_ACQ_REL;
42588 else if (!strcmp (p, "release"))
42589 mo = MEMMODEL_RELEASE;
42590 else if (!strcmp (p, "acquire"))
42591 mo = MEMMODEL_ACQUIRE;
42592 else
42593 error_at (cp_lexer_peek_token (parser->lexer)->location,
42594 "expected %<seq_cst%>, %<acq_rel%>, %<release%> or "
42595 "%<acquire%>");
42596 cp_lexer_consume_token (parser->lexer);
42598 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
42600 if (mo != MEMMODEL_LAST)
42601 error_at (cp_lexer_peek_token (parser->lexer)->location,
42602 "%<flush%> list specified together with memory order "
42603 "clause");
42604 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
42606 cp_parser_require_pragma_eol (parser, pragma_tok);
42608 finish_omp_flush (mo);
42611 /* Helper function, to parse omp for increment expression. */
42613 static tree
42614 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
42616 tree cond = cp_parser_binary_expression (parser, false, true,
42617 PREC_NOT_OPERATOR, NULL);
42618 if (cond == error_mark_node
42619 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
42621 cp_parser_skip_to_end_of_statement (parser);
42622 return error_mark_node;
42625 switch (TREE_CODE (cond))
42627 case GT_EXPR:
42628 case GE_EXPR:
42629 case LT_EXPR:
42630 case LE_EXPR:
42631 break;
42632 case NE_EXPR:
42633 if (code != OACC_LOOP)
42634 break;
42635 gcc_fallthrough ();
42636 default:
42637 return error_mark_node;
42640 /* If decl is an iterator, preserve LHS and RHS of the relational
42641 expr until finish_omp_for. */
42642 if (decl
42643 && (type_dependent_expression_p (decl)
42644 || CLASS_TYPE_P (TREE_TYPE (decl))))
42645 return cond;
42647 return build_x_binary_op (cp_expr_loc_or_input_loc (cond),
42648 TREE_CODE (cond),
42649 TREE_OPERAND (cond, 0), ERROR_MARK,
42650 TREE_OPERAND (cond, 1), ERROR_MARK,
42651 NULL_TREE, /*overload=*/NULL, tf_warning_or_error);
42654 /* Helper function, to parse omp for increment expression. */
42656 static tree
42657 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
42659 cp_token *token = cp_lexer_peek_token (parser->lexer);
42660 enum tree_code op;
42661 tree lhs, rhs;
42662 cp_id_kind idk;
42663 bool decl_first;
42665 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
42667 op = (token->type == CPP_PLUS_PLUS
42668 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
42669 cp_lexer_consume_token (parser->lexer);
42670 lhs = cp_parser_simple_cast_expression (parser);
42671 if (lhs != decl
42672 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
42673 return error_mark_node;
42674 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
42677 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
42678 if (lhs != decl
42679 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
42680 return error_mark_node;
42682 token = cp_lexer_peek_token (parser->lexer);
42683 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
42685 op = (token->type == CPP_PLUS_PLUS
42686 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
42687 cp_lexer_consume_token (parser->lexer);
42688 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
42691 op = cp_parser_assignment_operator_opt (parser);
42692 if (op == ERROR_MARK)
42693 return error_mark_node;
42695 if (op != NOP_EXPR)
42697 rhs = cp_parser_assignment_expression (parser);
42698 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
42699 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
42702 lhs = cp_parser_binary_expression (parser, false, false,
42703 PREC_ADDITIVE_EXPRESSION, NULL);
42704 token = cp_lexer_peek_token (parser->lexer);
42705 decl_first = (lhs == decl
42706 || (processing_template_decl && cp_tree_equal (lhs, decl)));
42707 if (decl_first)
42708 lhs = NULL_TREE;
42709 if (token->type != CPP_PLUS
42710 && token->type != CPP_MINUS)
42711 return error_mark_node;
42715 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
42716 cp_lexer_consume_token (parser->lexer);
42717 rhs = cp_parser_binary_expression (parser, false, false,
42718 PREC_ADDITIVE_EXPRESSION, NULL);
42719 token = cp_lexer_peek_token (parser->lexer);
42720 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
42722 if (lhs == NULL_TREE)
42724 if (op == PLUS_EXPR)
42725 lhs = rhs;
42726 else
42727 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
42728 NULL_TREE, tf_warning_or_error);
42730 else
42731 lhs = build_x_binary_op (input_location, op,
42732 lhs, ERROR_MARK,
42733 rhs, ERROR_MARK,
42734 NULL_TREE, NULL, tf_warning_or_error);
42737 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
42739 if (!decl_first)
42741 if ((rhs != decl
42742 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
42743 || op == MINUS_EXPR)
42744 return error_mark_node;
42745 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
42747 else
42748 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
42750 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
42753 /* Parse the initialization statement of an OpenMP for loop.
42755 Return true if the resulting construct should have an
42756 OMP_CLAUSE_PRIVATE added to it. */
42758 static tree
42759 cp_parser_omp_for_loop_init (cp_parser *parser,
42760 tree &this_pre_body,
42761 releasing_vec &for_block,
42762 tree &init,
42763 tree &orig_init,
42764 tree &decl,
42765 tree &real_decl)
42767 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
42768 return NULL_TREE;
42770 tree add_private_clause = NULL_TREE;
42772 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
42774 init-expr:
42775 var = lb
42776 integer-type var = lb
42777 random-access-iterator-type var = lb
42778 pointer-type var = lb
42780 cp_decl_specifier_seq type_specifiers;
42782 /* First, try to parse as an initialized declaration. See
42783 cp_parser_condition, from whence the bulk of this is copied. */
42785 cp_parser_parse_tentatively (parser);
42786 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
42787 /*is_declaration=*/true,
42788 /*is_trailing_return=*/false,
42789 &type_specifiers);
42790 if (cp_parser_parse_definitely (parser))
42792 /* If parsing a type specifier seq succeeded, then this
42793 MUST be a initialized declaration. */
42794 tree asm_specification, attributes;
42795 cp_declarator *declarator;
42797 declarator = cp_parser_declarator (parser,
42798 CP_PARSER_DECLARATOR_NAMED,
42799 CP_PARSER_FLAGS_NONE,
42800 /*ctor_dtor_or_conv_p=*/NULL,
42801 /*parenthesized_p=*/NULL,
42802 /*member_p=*/false,
42803 /*friend_p=*/false,
42804 /*static_p=*/false);
42805 attributes = cp_parser_attributes_opt (parser);
42806 asm_specification = cp_parser_asm_specification_opt (parser);
42808 if (declarator == cp_error_declarator)
42809 cp_parser_skip_to_end_of_statement (parser);
42811 else
42813 tree pushed_scope, auto_node;
42815 decl = start_decl (declarator, &type_specifiers,
42816 SD_INITIALIZED, attributes,
42817 /*prefix_attributes=*/NULL_TREE,
42818 &pushed_scope);
42820 auto_node = type_uses_auto (TREE_TYPE (decl));
42821 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
42823 if (cp_lexer_next_token_is (parser->lexer,
42824 CPP_OPEN_PAREN))
42825 error ("parenthesized initialization is not allowed in "
42826 "OpenMP %<for%> loop");
42827 else
42828 /* Trigger an error. */
42829 cp_parser_require (parser, CPP_EQ, RT_EQ);
42831 init = error_mark_node;
42832 cp_parser_skip_to_end_of_statement (parser);
42834 else if (CLASS_TYPE_P (TREE_TYPE (decl))
42835 || type_dependent_expression_p (decl)
42836 || auto_node)
42838 bool is_direct_init, is_non_constant_init;
42840 init = cp_parser_initializer (parser,
42841 &is_direct_init,
42842 &is_non_constant_init);
42844 if (auto_node)
42846 TREE_TYPE (decl)
42847 = do_auto_deduction (TREE_TYPE (decl), init,
42848 auto_node);
42850 if (!CLASS_TYPE_P (TREE_TYPE (decl))
42851 && !type_dependent_expression_p (decl))
42852 goto non_class;
42855 cp_finish_decl (decl, init, !is_non_constant_init,
42856 asm_specification,
42857 LOOKUP_ONLYCONVERTING);
42858 orig_init = init;
42859 if (CLASS_TYPE_P (TREE_TYPE (decl)))
42861 vec_safe_push (for_block, this_pre_body);
42862 init = NULL_TREE;
42864 else
42866 init = pop_stmt_list (this_pre_body);
42867 if (init && TREE_CODE (init) == STATEMENT_LIST)
42869 tree_stmt_iterator i = tsi_start (init);
42870 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
42871 while (!tsi_end_p (i))
42873 tree t = tsi_stmt (i);
42874 if (TREE_CODE (t) == DECL_EXPR
42875 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
42877 tsi_delink (&i);
42878 vec_safe_push (for_block, t);
42879 continue;
42881 break;
42883 if (tsi_one_before_end_p (i))
42885 tree t = tsi_stmt (i);
42886 tsi_delink (&i);
42887 free_stmt_list (init);
42888 init = t;
42892 this_pre_body = NULL_TREE;
42894 else
42896 /* Consume '='. */
42897 cp_lexer_consume_token (parser->lexer);
42898 init = cp_parser_assignment_expression (parser);
42900 non_class:
42901 if (TYPE_REF_P (TREE_TYPE (decl)))
42902 init = error_mark_node;
42903 else
42904 cp_finish_decl (decl, NULL_TREE,
42905 /*init_const_expr_p=*/false,
42906 asm_specification,
42907 LOOKUP_ONLYCONVERTING);
42910 if (pushed_scope)
42911 pop_scope (pushed_scope);
42914 else
42916 cp_id_kind idk;
42917 /* If parsing a type specifier sequence failed, then
42918 this MUST be a simple expression. */
42919 cp_parser_parse_tentatively (parser);
42920 decl = cp_parser_primary_expression (parser, false, false,
42921 false, &idk);
42922 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
42923 if (!cp_parser_error_occurred (parser)
42924 && decl
42925 && (TREE_CODE (decl) == COMPONENT_REF
42926 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
42928 cp_parser_abort_tentative_parse (parser);
42929 cp_parser_parse_tentatively (parser);
42930 cp_token *token = cp_lexer_peek_token (parser->lexer);
42931 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
42932 /*check_dependency_p=*/true,
42933 /*template_p=*/NULL,
42934 /*declarator_p=*/false,
42935 /*optional_p=*/false);
42936 if (name != error_mark_node
42937 && last_tok == cp_lexer_peek_token (parser->lexer))
42939 decl = cp_parser_lookup_name_simple (parser, name,
42940 token->location);
42941 if (TREE_CODE (decl) == FIELD_DECL)
42942 add_private_clause = omp_privatize_field (decl, false);
42944 cp_parser_abort_tentative_parse (parser);
42945 cp_parser_parse_tentatively (parser);
42946 decl = cp_parser_primary_expression (parser, false, false,
42947 false, &idk);
42949 if (!cp_parser_error_occurred (parser)
42950 && decl
42951 && DECL_P (decl)
42952 && CLASS_TYPE_P (TREE_TYPE (decl)))
42954 tree rhs;
42956 cp_parser_parse_definitely (parser);
42957 cp_parser_require (parser, CPP_EQ, RT_EQ);
42958 rhs = cp_parser_assignment_expression (parser);
42959 orig_init = rhs;
42960 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
42961 decl, NOP_EXPR,
42962 rhs, NULL_TREE,
42963 tf_warning_or_error));
42964 if (!add_private_clause)
42965 add_private_clause = decl;
42967 else
42969 decl = NULL;
42970 cp_parser_abort_tentative_parse (parser);
42971 init = cp_parser_expression (parser);
42972 if (init)
42974 if (TREE_CODE (init) == MODIFY_EXPR
42975 || TREE_CODE (init) == MODOP_EXPR)
42976 real_decl = TREE_OPERAND (init, 0);
42980 return add_private_clause;
42983 /* Helper for cp_parser_omp_for_loop, handle one range-for loop. */
42985 void
42986 cp_convert_omp_range_for (tree &this_pre_body, vec<tree, va_gc> *for_block,
42987 tree &decl, tree &orig_decl, tree &init,
42988 tree &orig_init, tree &cond, tree &incr)
42990 tree begin, end, range_temp_decl = NULL_TREE;
42991 tree iter_type, begin_expr, end_expr;
42993 if (processing_template_decl)
42995 if (check_for_bare_parameter_packs (init))
42996 init = error_mark_node;
42997 if (!type_dependent_expression_p (init)
42998 /* do_auto_deduction doesn't mess with template init-lists. */
42999 && !BRACE_ENCLOSED_INITIALIZER_P (init))
43001 tree d = decl;
43002 tree decomp_first_name = NULL_TREE;
43003 unsigned decomp_cnt = 0;
43004 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
43006 tree v = DECL_VALUE_EXPR (decl);
43007 if (TREE_CODE (v) == ARRAY_REF
43008 && VAR_P (TREE_OPERAND (v, 0))
43009 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
43011 d = TREE_OPERAND (v, 0);
43012 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
43013 decomp_first_name = decl;
43016 do_range_for_auto_deduction (d, init, decomp_first_name, decomp_cnt);
43018 cond = global_namespace;
43019 incr = NULL_TREE;
43020 orig_init = init;
43021 if (this_pre_body)
43022 this_pre_body = pop_stmt_list (this_pre_body);
43023 return;
43026 init = mark_lvalue_use (init);
43028 if (decl == error_mark_node || init == error_mark_node)
43029 /* If an error happened previously do nothing or else a lot of
43030 unhelpful errors would be issued. */
43031 begin_expr = end_expr = iter_type = error_mark_node;
43032 else
43034 tree range_temp;
43036 if (VAR_P (init)
43037 && array_of_runtime_bound_p (TREE_TYPE (init)))
43038 /* Can't bind a reference to an array of runtime bound. */
43039 range_temp = init;
43040 else
43042 range_temp = build_range_temp (init);
43043 DECL_NAME (range_temp) = NULL_TREE;
43044 pushdecl (range_temp);
43045 cp_finish_decl (range_temp, init,
43046 /*is_constant_init*/false, NULL_TREE,
43047 LOOKUP_ONLYCONVERTING);
43048 range_temp_decl = range_temp;
43049 range_temp = convert_from_reference (range_temp);
43051 iter_type = cp_parser_perform_range_for_lookup (range_temp,
43052 &begin_expr, &end_expr);
43055 tree end_iter_type = iter_type;
43056 if (cxx_dialect >= cxx17)
43057 end_iter_type = cv_unqualified (TREE_TYPE (end_expr));
43058 end = build_decl (input_location, VAR_DECL, NULL_TREE, end_iter_type);
43059 TREE_USED (end) = 1;
43060 DECL_ARTIFICIAL (end) = 1;
43061 pushdecl (end);
43062 cp_finish_decl (end, end_expr,
43063 /*is_constant_init*/false, NULL_TREE,
43064 LOOKUP_ONLYCONVERTING);
43066 /* The new for initialization statement. */
43067 begin = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
43068 TREE_USED (begin) = 1;
43069 DECL_ARTIFICIAL (begin) = 1;
43070 pushdecl (begin);
43071 orig_init = init;
43072 if (CLASS_TYPE_P (iter_type))
43073 init = NULL_TREE;
43074 else
43076 init = begin_expr;
43077 begin_expr = NULL_TREE;
43079 cp_finish_decl (begin, begin_expr,
43080 /*is_constant_init*/false, NULL_TREE,
43081 LOOKUP_ONLYCONVERTING);
43083 /* The new for condition. */
43084 if (CLASS_TYPE_P (iter_type))
43085 cond = build2 (NE_EXPR, boolean_type_node, begin, end);
43086 else
43087 cond = build_x_binary_op (input_location, NE_EXPR,
43088 begin, ERROR_MARK,
43089 end, ERROR_MARK,
43090 NULL_TREE, NULL, tf_warning_or_error);
43092 /* The new increment expression. */
43093 if (CLASS_TYPE_P (iter_type))
43094 incr = build2 (PREINCREMENT_EXPR, iter_type, begin, NULL_TREE);
43095 else
43096 incr = finish_unary_op_expr (input_location,
43097 PREINCREMENT_EXPR, begin,
43098 tf_warning_or_error);
43100 orig_decl = decl;
43101 decl = begin;
43102 if (for_block)
43104 vec_safe_push (for_block, this_pre_body);
43105 this_pre_body = NULL_TREE;
43108 tree decomp_first_name = NULL_TREE;
43109 unsigned decomp_cnt = 0;
43110 if (orig_decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (orig_decl))
43112 tree v = DECL_VALUE_EXPR (orig_decl);
43113 if (TREE_CODE (v) == ARRAY_REF
43114 && VAR_P (TREE_OPERAND (v, 0))
43115 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
43117 tree d = orig_decl;
43118 orig_decl = TREE_OPERAND (v, 0);
43119 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
43120 decomp_first_name = d;
43124 tree auto_node = type_uses_auto (TREE_TYPE (orig_decl));
43125 if (auto_node)
43127 tree t = build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
43128 NULL_TREE, tf_none);
43129 if (!error_operand_p (t))
43131 TREE_TYPE (orig_decl) = do_auto_deduction (TREE_TYPE (orig_decl),
43132 t, auto_node);
43133 if (decomp_first_name)
43135 ++processing_template_decl;
43136 cp_finish_decomp (orig_decl, decomp_first_name, decomp_cnt);
43137 --processing_template_decl;
43142 tree v = make_tree_vec (decomp_cnt + 3);
43143 TREE_VEC_ELT (v, 0) = range_temp_decl;
43144 TREE_VEC_ELT (v, 1) = end;
43145 TREE_VEC_ELT (v, 2) = orig_decl;
43146 for (unsigned i = 0; i < decomp_cnt; i++)
43148 TREE_VEC_ELT (v, i + 3) = decomp_first_name;
43149 decomp_first_name = DECL_CHAIN (decomp_first_name);
43151 orig_decl = tree_cons (NULL_TREE, NULL_TREE, v);
43154 /* Helper for cp_parser_omp_for_loop, finalize part of range for
43155 inside of the collapsed body. */
43157 void
43158 cp_finish_omp_range_for (tree orig, tree begin)
43160 gcc_assert (TREE_CODE (orig) == TREE_LIST
43161 && TREE_CODE (TREE_CHAIN (orig)) == TREE_VEC);
43162 tree decl = TREE_VEC_ELT (TREE_CHAIN (orig), 2);
43163 tree decomp_first_name = NULL_TREE;
43164 unsigned int decomp_cnt = 0;
43166 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
43168 decomp_first_name = TREE_VEC_ELT (TREE_CHAIN (orig), 3);
43169 decomp_cnt = TREE_VEC_LENGTH (TREE_CHAIN (orig)) - 3;
43170 cp_maybe_mangle_decomp (decl, decomp_first_name, decomp_cnt);
43173 /* The declaration is initialized with *__begin inside the loop body. */
43174 cp_finish_decl (decl,
43175 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
43176 NULL_TREE, tf_warning_or_error),
43177 /*is_constant_init*/false, NULL_TREE,
43178 LOOKUP_ONLYCONVERTING);
43179 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
43180 cp_finish_decomp (decl, decomp_first_name, decomp_cnt);
43183 /* Return true if next tokens contain a standard attribute that contains
43184 omp::directive (DIRECTIVE). */
43186 static bool
43187 cp_parser_omp_section_scan (cp_parser *parser, const char *directive,
43188 bool tentative)
43190 size_t n = cp_parser_skip_attributes_opt (parser, 1), i;
43191 if (n < 10)
43192 return false;
43193 for (i = 5; i < n - 4; i++)
43194 if (cp_lexer_nth_token_is (parser->lexer, i, CPP_NAME)
43195 && cp_lexer_nth_token_is (parser->lexer, i + 1, CPP_OPEN_PAREN)
43196 && cp_lexer_nth_token_is (parser->lexer, i + 2, CPP_NAME))
43198 tree first = cp_lexer_peek_nth_token (parser->lexer, i)->u.value;
43199 tree second = cp_lexer_peek_nth_token (parser->lexer, i + 2)->u.value;
43200 if (strcmp (IDENTIFIER_POINTER (first), "directive"))
43201 continue;
43202 if (strcmp (IDENTIFIER_POINTER (second), directive) == 0)
43203 break;
43205 if (i == n - 4)
43206 return false;
43207 cp_parser_parse_tentatively (parser);
43208 location_t first_loc = cp_lexer_peek_token (parser->lexer)->location;
43209 location_t last_loc
43210 = cp_lexer_peek_nth_token (parser->lexer, n - 1)->location;
43211 location_t middle_loc = UNKNOWN_LOCATION;
43212 tree std_attrs = cp_parser_std_attribute_spec_seq (parser);
43213 int cnt = 0;
43214 bool seen = false;
43215 for (tree attr = std_attrs; attr; attr = TREE_CHAIN (attr))
43216 if (get_attribute_namespace (attr) == omp_identifier
43217 && is_attribute_p ("directive", get_attribute_name (attr)))
43219 for (tree a = TREE_VALUE (attr); a; a = TREE_CHAIN (a))
43221 tree d = TREE_VALUE (a);
43222 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
43223 cp_token *first = DEFPARSE_TOKENS (d)->first;
43224 cnt++;
43225 if (first->type == CPP_NAME
43226 && strcmp (IDENTIFIER_POINTER (first->u.value),
43227 directive) == 0)
43229 seen = true;
43230 if (middle_loc == UNKNOWN_LOCATION)
43231 middle_loc = first->location;
43235 if (!seen || tentative)
43237 cp_parser_abort_tentative_parse (parser);
43238 return seen;
43240 if (cnt != 1 || TREE_CHAIN (std_attrs))
43242 error_at (make_location (first_loc, last_loc, middle_loc),
43243 "%<[[omp::directive(%s)]]%> must be the only specified "
43244 "attribute on a statement", directive);
43245 cp_parser_abort_tentative_parse (parser);
43246 return false;
43248 if (!cp_parser_parse_definitely (parser))
43249 return false;
43250 cp_parser_handle_statement_omp_attributes (parser, std_attrs);
43251 return true;
43254 /* Parse an OpenMP structured block sequence. KIND is the corresponding
43255 separating directive. */
43257 static tree
43258 cp_parser_omp_structured_block_sequence (cp_parser *parser,
43259 enum pragma_kind kind)
43261 tree stmt = begin_omp_structured_block ();
43262 unsigned int save = cp_parser_begin_omp_structured_block (parser);
43264 cp_parser_statement (parser, NULL_TREE, false, NULL);
43265 while (true)
43267 cp_token *token = cp_lexer_peek_token (parser->lexer);
43269 if (token->type == CPP_CLOSE_BRACE
43270 || token->type == CPP_EOF
43271 || token->type == CPP_PRAGMA_EOL
43272 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END)
43273 || (kind != PRAGMA_NONE
43274 && cp_parser_pragma_kind (token) == kind))
43275 break;
43277 if (kind != PRAGMA_NONE
43278 && cp_parser_omp_section_scan (parser,
43279 kind == PRAGMA_OMP_SCAN
43280 ? "scan" : "section", false))
43281 break;
43283 cp_parser_statement (parser, NULL_TREE, false, NULL);
43286 cp_parser_end_omp_structured_block (parser, save);
43287 return finish_omp_structured_block (stmt);
43291 /* OpenMP 5.0:
43293 scan-loop-body:
43294 { structured-block scan-directive structured-block } */
43296 static void
43297 cp_parser_omp_scan_loop_body (cp_parser *parser)
43299 tree substmt, clauses = NULL_TREE;
43301 matching_braces braces;
43302 if (!braces.require_open (parser))
43303 return;
43305 substmt = cp_parser_omp_structured_block_sequence (parser, PRAGMA_OMP_SCAN);
43306 substmt = build2 (OMP_SCAN, void_type_node, substmt, NULL_TREE);
43307 add_stmt (substmt);
43309 cp_token *tok = cp_lexer_peek_token (parser->lexer);
43310 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SCAN)
43312 enum omp_clause_code clause = OMP_CLAUSE_ERROR;
43314 cp_lexer_consume_token (parser->lexer);
43316 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
43317 cp_lexer_consume_token (parser->lexer);
43319 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43321 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43322 const char *p = IDENTIFIER_POINTER (id);
43323 if (strcmp (p, "inclusive") == 0)
43324 clause = OMP_CLAUSE_INCLUSIVE;
43325 else if (strcmp (p, "exclusive") == 0)
43326 clause = OMP_CLAUSE_EXCLUSIVE;
43328 if (clause != OMP_CLAUSE_ERROR)
43330 cp_lexer_consume_token (parser->lexer);
43331 clauses = cp_parser_omp_var_list (parser, clause, NULL_TREE);
43333 else
43334 cp_parser_error (parser, "expected %<inclusive%> or "
43335 "%<exclusive%> clause");
43337 cp_parser_require_pragma_eol (parser, tok);
43339 else
43340 error ("expected %<#pragma omp scan%>");
43342 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
43343 substmt = cp_parser_omp_structured_block_sequence (parser, PRAGMA_NONE);
43344 substmt = build2_loc (tok->location, OMP_SCAN, void_type_node, substmt,
43345 clauses);
43346 add_stmt (substmt);
43348 braces.require_close (parser);
43351 /* Parse the restricted form of the for statement allowed by OpenMP. */
43353 static tree
43354 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
43355 tree *cclauses, bool *if_p)
43357 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
43358 tree orig_decl;
43359 tree real_decl, initv, condv, incrv, declv, orig_declv;
43360 tree this_pre_body, cl, ordered_cl = NULL_TREE;
43361 location_t loc_first;
43362 bool collapse_err = false;
43363 int i, collapse = 1, ordered = 0, count, nbraces = 0;
43364 releasing_vec for_block;
43365 auto_vec<tree, 4> orig_inits;
43366 bool tiling = false;
43367 bool inscan = false;
43369 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
43370 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
43371 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
43372 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
43374 tiling = true;
43375 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
43377 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
43378 && OMP_CLAUSE_ORDERED_EXPR (cl))
43380 ordered_cl = cl;
43381 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
43383 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_REDUCTION
43384 && OMP_CLAUSE_REDUCTION_INSCAN (cl)
43385 && (code == OMP_SIMD || code == OMP_FOR))
43386 inscan = true;
43388 if (ordered && ordered < collapse)
43390 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
43391 "%<ordered%> clause parameter is less than %<collapse%>");
43392 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
43393 = build_int_cst (NULL_TREE, collapse);
43394 ordered = collapse;
43397 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
43398 count = ordered ? ordered : collapse;
43400 declv = make_tree_vec (count);
43401 initv = make_tree_vec (count);
43402 condv = make_tree_vec (count);
43403 incrv = make_tree_vec (count);
43404 orig_declv = NULL_TREE;
43406 loc_first = cp_lexer_peek_token (parser->lexer)->location;
43408 for (i = 0; i < count; i++)
43410 int bracecount = 0;
43411 tree add_private_clause = NULL_TREE;
43412 location_t loc;
43414 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
43416 if (!collapse_err)
43417 cp_parser_error (parser, "for statement expected");
43418 return NULL;
43420 loc = cp_lexer_consume_token (parser->lexer)->location;
43422 /* Don't create location wrapper nodes within an OpenMP "for"
43423 statement. */
43424 auto_suppress_location_wrappers sentinel;
43426 matching_parens parens;
43427 if (!parens.require_open (parser))
43428 return NULL;
43430 init = orig_init = decl = real_decl = orig_decl = NULL_TREE;
43431 this_pre_body = push_stmt_list ();
43433 if (code != OACC_LOOP && cxx_dialect >= cxx11)
43435 /* Save tokens so that we can put them back. */
43436 cp_lexer_save_tokens (parser->lexer);
43438 /* Look for ':' that is not nested in () or {}. */
43439 bool is_range_for
43440 = (cp_parser_skip_to_closing_parenthesis_1 (parser,
43441 /*recovering=*/false,
43442 CPP_COLON,
43443 /*consume_paren=*/
43444 false) == -1);
43446 /* Roll back the tokens we skipped. */
43447 cp_lexer_rollback_tokens (parser->lexer);
43449 if (is_range_for)
43451 bool saved_colon_corrects_to_scope_p
43452 = parser->colon_corrects_to_scope_p;
43454 /* A colon is used in range-based for. */
43455 parser->colon_corrects_to_scope_p = false;
43457 /* Parse the declaration. */
43458 cp_parser_simple_declaration (parser,
43459 /*function_definition_allowed_p=*/
43460 false, &decl);
43461 parser->colon_corrects_to_scope_p
43462 = saved_colon_corrects_to_scope_p;
43464 cp_parser_require (parser, CPP_COLON, RT_COLON);
43466 init = cp_parser_range_for (parser, NULL_TREE, NULL_TREE, decl,
43467 false, 0, true);
43469 cp_convert_omp_range_for (this_pre_body, for_block, decl,
43470 orig_decl, init, orig_init,
43471 cond, incr);
43472 if (this_pre_body)
43474 if (pre_body)
43476 tree t = pre_body;
43477 pre_body = push_stmt_list ();
43478 add_stmt (t);
43479 add_stmt (this_pre_body);
43480 pre_body = pop_stmt_list (pre_body);
43482 else
43483 pre_body = this_pre_body;
43486 if (ordered_cl)
43487 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
43488 "%<ordered%> clause with parameter on "
43489 "range-based %<for%> loop");
43491 goto parse_close_paren;
43495 add_private_clause
43496 = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
43497 init, orig_init, decl, real_decl);
43499 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
43500 if (this_pre_body)
43502 this_pre_body = pop_stmt_list (this_pre_body);
43503 if (pre_body)
43505 tree t = pre_body;
43506 pre_body = push_stmt_list ();
43507 add_stmt (t);
43508 add_stmt (this_pre_body);
43509 pre_body = pop_stmt_list (pre_body);
43511 else
43512 pre_body = this_pre_body;
43515 if (decl)
43516 real_decl = decl;
43517 if (cclauses != NULL
43518 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
43519 && real_decl != NULL_TREE
43520 && code != OMP_LOOP)
43522 tree *c;
43523 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
43524 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
43525 && OMP_CLAUSE_DECL (*c) == real_decl)
43527 error_at (loc, "iteration variable %qD"
43528 " should not be firstprivate", real_decl);
43529 *c = OMP_CLAUSE_CHAIN (*c);
43531 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
43532 && OMP_CLAUSE_DECL (*c) == real_decl)
43534 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
43535 tree l = *c;
43536 *c = OMP_CLAUSE_CHAIN (*c);
43537 if (code == OMP_SIMD)
43539 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
43540 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
43542 else
43544 OMP_CLAUSE_CHAIN (l) = clauses;
43545 clauses = l;
43547 add_private_clause = NULL_TREE;
43549 else
43551 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
43552 && OMP_CLAUSE_DECL (*c) == real_decl)
43553 add_private_clause = NULL_TREE;
43554 c = &OMP_CLAUSE_CHAIN (*c);
43558 if (add_private_clause)
43560 tree c;
43561 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
43563 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
43564 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
43565 && OMP_CLAUSE_DECL (c) == decl)
43566 break;
43567 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
43568 && OMP_CLAUSE_DECL (c) == decl)
43569 error_at (loc, "iteration variable %qD "
43570 "should not be firstprivate",
43571 decl);
43572 else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
43573 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
43574 && OMP_CLAUSE_DECL (c) == decl)
43575 error_at (loc, "iteration variable %qD should not be reduction",
43576 decl);
43578 if (c == NULL)
43580 if ((code == OMP_SIMD && collapse != 1) || code == OMP_LOOP)
43581 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
43582 else if (code != OMP_SIMD)
43583 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
43584 else
43585 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
43586 OMP_CLAUSE_DECL (c) = add_private_clause;
43587 c = finish_omp_clauses (c, C_ORT_OMP);
43588 if (c)
43590 OMP_CLAUSE_CHAIN (c) = clauses;
43591 clauses = c;
43592 /* For linear, signal that we need to fill up
43593 the so far unknown linear step. */
43594 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
43595 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
43600 cond = NULL;
43601 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
43602 cond = cp_parser_omp_for_cond (parser, decl, code);
43603 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
43605 incr = NULL;
43606 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
43608 /* If decl is an iterator, preserve the operator on decl
43609 until finish_omp_for. */
43610 if (real_decl
43611 && ((processing_template_decl
43612 && (TREE_TYPE (real_decl) == NULL_TREE
43613 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl))))
43614 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
43615 incr = cp_parser_omp_for_incr (parser, real_decl);
43616 else
43617 incr = cp_parser_expression (parser);
43618 protected_set_expr_location_if_unset (incr, input_location);
43621 parse_close_paren:
43622 if (!parens.require_close (parser))
43623 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
43624 /*or_comma=*/false,
43625 /*consume_paren=*/true);
43627 TREE_VEC_ELT (declv, i) = decl;
43628 TREE_VEC_ELT (initv, i) = init;
43629 TREE_VEC_ELT (condv, i) = cond;
43630 TREE_VEC_ELT (incrv, i) = incr;
43631 if (orig_init)
43633 orig_inits.safe_grow_cleared (i + 1, true);
43634 orig_inits[i] = orig_init;
43636 if (orig_decl)
43638 if (!orig_declv)
43639 orig_declv = copy_node (declv);
43640 TREE_VEC_ELT (orig_declv, i) = orig_decl;
43642 else if (orig_declv)
43643 TREE_VEC_ELT (orig_declv, i) = decl;
43645 if (i == count - 1)
43646 break;
43648 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
43649 in between the collapsed for loops to be still considered perfectly
43650 nested. Hopefully the final version clarifies this.
43651 For now handle (multiple) {'s and empty statements. */
43652 cp_parser_parse_tentatively (parser);
43653 for (;;)
43655 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
43656 break;
43657 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
43659 cp_lexer_consume_token (parser->lexer);
43660 bracecount++;
43662 else if (bracecount
43663 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
43664 cp_lexer_consume_token (parser->lexer);
43665 else
43667 loc = cp_lexer_peek_token (parser->lexer)->location;
43668 error_at (loc, "not enough for loops to collapse");
43669 collapse_err = true;
43670 cp_parser_abort_tentative_parse (parser);
43671 declv = NULL_TREE;
43672 break;
43676 if (declv)
43678 cp_parser_parse_definitely (parser);
43679 nbraces += bracecount;
43683 if (nbraces)
43684 if_p = NULL;
43686 /* Note that we saved the original contents of this flag when we entered
43687 the structured block, and so we don't need to re-save it here. */
43688 parser->in_statement = IN_OMP_FOR;
43690 /* Note that the grammar doesn't call for a structured block here,
43691 though the loop as a whole is a structured block. */
43692 if (orig_declv)
43694 body = begin_omp_structured_block ();
43695 for (i = 0; i < count; i++)
43696 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i))
43697 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
43698 TREE_VEC_ELT (declv, i));
43700 else
43701 body = push_stmt_list ();
43702 if (inscan)
43703 cp_parser_omp_scan_loop_body (parser);
43704 else
43705 cp_parser_statement (parser, NULL_TREE, false, if_p);
43706 if (orig_declv)
43707 body = finish_omp_structured_block (body);
43708 else
43709 body = pop_stmt_list (body);
43711 if (declv == NULL_TREE)
43712 ret = NULL_TREE;
43713 else
43714 ret = finish_omp_for (loc_first, code, declv, orig_declv, initv, condv,
43715 incrv, body, pre_body, &orig_inits, clauses);
43717 while (nbraces)
43719 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
43721 cp_lexer_consume_token (parser->lexer);
43722 nbraces--;
43724 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
43725 cp_lexer_consume_token (parser->lexer);
43726 else
43728 if (!collapse_err)
43730 error_at (cp_lexer_peek_token (parser->lexer)->location,
43731 "collapsed loops not perfectly nested");
43733 collapse_err = true;
43734 cp_parser_statement_seq_opt (parser, NULL);
43735 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
43736 break;
43740 while (!for_block->is_empty ())
43742 tree t = for_block->pop ();
43743 if (TREE_CODE (t) == STATEMENT_LIST)
43744 add_stmt (pop_stmt_list (t));
43745 else
43746 add_stmt (t);
43749 return ret;
43752 /* Helper function for OpenMP parsing, split clauses and call
43753 finish_omp_clauses on each of the set of clauses afterwards. */
43755 static void
43756 cp_omp_split_clauses (location_t loc, enum tree_code code,
43757 omp_clause_mask mask, tree clauses, tree *cclauses)
43759 int i;
43760 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
43761 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
43762 if (cclauses[i])
43763 cclauses[i] = finish_omp_clauses (cclauses[i],
43764 i == C_OMP_CLAUSE_SPLIT_TARGET
43765 ? C_ORT_OMP_TARGET : C_ORT_OMP);
43768 /* OpenMP 5.0:
43769 #pragma omp loop loop-clause[optseq] new-line
43770 for-loop */
43772 #define OMP_LOOP_CLAUSE_MASK \
43773 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
43774 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
43775 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
43776 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
43777 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_BIND) \
43778 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
43780 static tree
43781 cp_parser_omp_loop (cp_parser *parser, cp_token *pragma_tok,
43782 char *p_name, omp_clause_mask mask, tree *cclauses,
43783 bool *if_p)
43785 tree clauses, sb, ret;
43786 unsigned int save;
43787 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
43789 strcat (p_name, " loop");
43790 mask |= OMP_LOOP_CLAUSE_MASK;
43792 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
43793 cclauses == NULL);
43794 if (cclauses)
43796 cp_omp_split_clauses (loc, OMP_LOOP, mask, clauses, cclauses);
43797 clauses = cclauses[C_OMP_CLAUSE_SPLIT_LOOP];
43800 keep_next_level (true);
43801 sb = begin_omp_structured_block ();
43802 save = cp_parser_begin_omp_structured_block (parser);
43804 ret = cp_parser_omp_for_loop (parser, OMP_LOOP, clauses, cclauses, if_p);
43806 cp_parser_end_omp_structured_block (parser, save);
43807 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
43809 return ret;
43812 /* OpenMP 4.0:
43813 #pragma omp simd simd-clause[optseq] new-line
43814 for-loop */
43816 #define OMP_SIMD_CLAUSE_MASK \
43817 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
43818 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
43819 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
43820 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
43821 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
43822 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
43823 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
43824 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
43825 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
43826 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL) \
43827 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
43829 static tree
43830 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
43831 char *p_name, omp_clause_mask mask, tree *cclauses,
43832 bool *if_p)
43834 tree clauses, sb, ret;
43835 unsigned int save;
43836 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
43838 strcat (p_name, " simd");
43839 mask |= OMP_SIMD_CLAUSE_MASK;
43841 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
43842 cclauses == NULL);
43843 if (cclauses)
43845 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
43846 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
43849 keep_next_level (true);
43850 sb = begin_omp_structured_block ();
43851 save = cp_parser_begin_omp_structured_block (parser);
43853 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
43855 cp_parser_end_omp_structured_block (parser, save);
43856 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
43858 return ret;
43861 /* OpenMP 2.5:
43862 #pragma omp for for-clause[optseq] new-line
43863 for-loop
43865 OpenMP 4.0:
43866 #pragma omp for simd for-simd-clause[optseq] new-line
43867 for-loop */
43869 #define OMP_FOR_CLAUSE_MASK \
43870 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
43871 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
43872 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
43873 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
43874 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
43875 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
43876 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
43877 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
43878 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
43879 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
43880 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
43882 static tree
43883 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
43884 char *p_name, omp_clause_mask mask, tree *cclauses,
43885 bool *if_p)
43887 tree clauses, sb, ret;
43888 unsigned int save;
43889 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
43891 strcat (p_name, " for");
43892 mask |= OMP_FOR_CLAUSE_MASK;
43893 /* parallel for{, simd} disallows nowait clause, but for
43894 target {teams distribute ,}parallel for{, simd} it should be accepted. */
43895 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
43896 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
43897 /* Composite distribute parallel for{, simd} disallows ordered clause. */
43898 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
43899 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
43901 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43903 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43904 const char *p = IDENTIFIER_POINTER (id);
43906 if (strcmp (p, "simd") == 0)
43908 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
43909 if (cclauses == NULL)
43910 cclauses = cclauses_buf;
43912 cp_lexer_consume_token (parser->lexer);
43913 if (!flag_openmp) /* flag_openmp_simd */
43914 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
43915 cclauses, if_p);
43916 sb = begin_omp_structured_block ();
43917 save = cp_parser_begin_omp_structured_block (parser);
43918 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
43919 cclauses, if_p);
43920 cp_parser_end_omp_structured_block (parser, save);
43921 tree body = finish_omp_structured_block (sb);
43922 if (ret == NULL)
43923 return ret;
43924 ret = make_node (OMP_FOR);
43925 TREE_TYPE (ret) = void_type_node;
43926 OMP_FOR_BODY (ret) = body;
43927 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
43928 SET_EXPR_LOCATION (ret, loc);
43929 add_stmt (ret);
43930 return ret;
43933 if (!flag_openmp) /* flag_openmp_simd */
43935 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43936 return NULL_TREE;
43939 /* Composite distribute parallel for disallows linear clause. */
43940 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
43941 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
43943 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
43944 cclauses == NULL);
43945 if (cclauses)
43947 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
43948 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
43951 keep_next_level (true);
43952 sb = begin_omp_structured_block ();
43953 save = cp_parser_begin_omp_structured_block (parser);
43955 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
43957 cp_parser_end_omp_structured_block (parser, save);
43958 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
43960 return ret;
43963 static tree cp_parser_omp_taskloop (cp_parser *, cp_token *, char *,
43964 omp_clause_mask, tree *, bool *);
43966 /* OpenMP 2.5:
43967 # pragma omp master new-line
43968 structured-block */
43970 static tree
43971 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok,
43972 char *p_name, omp_clause_mask mask, tree *cclauses,
43973 bool *if_p)
43975 tree clauses, sb, ret;
43976 unsigned int save;
43977 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
43979 strcat (p_name, " master");
43981 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43983 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43984 const char *p = IDENTIFIER_POINTER (id);
43986 if (strcmp (p, "taskloop") == 0)
43988 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
43989 if (cclauses == NULL)
43990 cclauses = cclauses_buf;
43992 cp_lexer_consume_token (parser->lexer);
43993 if (!flag_openmp) /* flag_openmp_simd */
43994 return cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
43995 cclauses, if_p);
43996 sb = begin_omp_structured_block ();
43997 save = cp_parser_begin_omp_structured_block (parser);
43998 ret = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
43999 cclauses, if_p);
44000 cp_parser_end_omp_structured_block (parser, save);
44001 tree body = finish_omp_structured_block (sb);
44002 if (ret == NULL)
44003 return ret;
44004 ret = c_finish_omp_master (loc, body);
44005 OMP_MASTER_COMBINED (ret) = 1;
44006 return ret;
44009 if (!flag_openmp) /* flag_openmp_simd */
44011 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44012 return NULL_TREE;
44015 if (cclauses)
44017 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
44018 false);
44019 cp_omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
44021 else
44022 cp_parser_require_pragma_eol (parser, pragma_tok);
44024 return c_finish_omp_master (loc,
44025 cp_parser_omp_structured_block (parser, if_p));
44028 /* OpenMP 5.1:
44029 # pragma omp masked masked-clauses new-line
44030 structured-block */
44032 #define OMP_MASKED_CLAUSE_MASK \
44033 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FILTER)
44035 static tree
44036 cp_parser_omp_masked (cp_parser *parser, cp_token *pragma_tok,
44037 char *p_name, omp_clause_mask mask, tree *cclauses,
44038 bool *if_p)
44040 tree clauses, sb, ret;
44041 unsigned int save;
44042 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
44044 strcat (p_name, " masked");
44045 mask |= OMP_MASKED_CLAUSE_MASK;
44047 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
44049 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
44050 const char *p = IDENTIFIER_POINTER (id);
44052 if (strcmp (p, "taskloop") == 0)
44054 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
44055 if (cclauses == NULL)
44056 cclauses = cclauses_buf;
44058 cp_lexer_consume_token (parser->lexer);
44059 if (!flag_openmp) /* flag_openmp_simd */
44060 return cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
44061 cclauses, if_p);
44062 sb = begin_omp_structured_block ();
44063 save = cp_parser_begin_omp_structured_block (parser);
44064 ret = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
44065 cclauses, if_p);
44066 cp_parser_end_omp_structured_block (parser, save);
44067 tree body = finish_omp_structured_block (sb);
44068 if (ret == NULL)
44069 return ret;
44070 ret = c_finish_omp_masked (loc, body,
44071 cclauses[C_OMP_CLAUSE_SPLIT_MASKED]);
44072 OMP_MASKED_COMBINED (ret) = 1;
44073 return ret;
44076 if (!flag_openmp) /* flag_openmp_simd */
44078 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44079 return NULL_TREE;
44082 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
44083 cclauses == NULL);
44084 if (cclauses)
44086 cp_omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
44087 clauses = cclauses[C_OMP_CLAUSE_SPLIT_MASKED];
44090 return c_finish_omp_masked (loc,
44091 cp_parser_omp_structured_block (parser, if_p),
44092 clauses);
44095 /* OpenMP 2.5:
44096 # pragma omp ordered new-line
44097 structured-block
44099 OpenMP 4.5:
44100 # pragma omp ordered ordered-clauses new-line
44101 structured-block */
44103 #define OMP_ORDERED_CLAUSE_MASK \
44104 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
44105 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
44107 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
44108 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
44109 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DOACROSS))
44111 static bool
44112 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
44113 enum pragma_context context, bool *if_p)
44115 location_t loc = pragma_tok->location;
44116 int n = 1;
44118 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
44119 n = 2;
44121 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_NAME))
44123 tree id = cp_lexer_peek_nth_token (parser->lexer, n)->u.value;
44124 const char *p = IDENTIFIER_POINTER (id);
44126 if (strcmp (p, "depend") == 0 || strcmp (p, "doacross") == 0)
44128 if (!flag_openmp) /* flag_openmp_simd */
44130 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44131 return false;
44133 if (context == pragma_stmt)
44135 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
44136 "%qs clause may only be used in compound "
44137 "statements", p);
44138 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44139 return true;
44141 tree clauses
44142 = cp_parser_omp_all_clauses (parser,
44143 OMP_ORDERED_DEPEND_CLAUSE_MASK,
44144 "#pragma omp ordered", pragma_tok);
44145 c_finish_omp_ordered (loc, clauses, NULL_TREE);
44146 return false;
44150 tree clauses
44151 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
44152 "#pragma omp ordered", pragma_tok);
44154 if (!flag_openmp /* flag_openmp_simd */
44155 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
44156 return false;
44158 c_finish_omp_ordered (loc, clauses,
44159 cp_parser_omp_structured_block (parser, if_p));
44160 return true;
44163 /* OpenMP 2.5:
44165 section-scope:
44166 { section-sequence }
44168 section-sequence:
44169 section-directive[opt] structured-block
44170 section-sequence section-directive structured-block */
44172 static tree
44173 cp_parser_omp_sections_scope (cp_parser *parser)
44175 tree stmt, substmt;
44176 bool error_suppress = false;
44177 cp_token *tok;
44179 matching_braces braces;
44180 if (!braces.require_open (parser))
44181 return NULL_TREE;
44183 stmt = push_stmt_list ();
44185 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
44186 != PRAGMA_OMP_SECTION
44187 && !cp_parser_omp_section_scan (parser, "section", true))
44189 substmt = cp_parser_omp_structured_block_sequence (parser,
44190 PRAGMA_OMP_SECTION);
44191 substmt = build1 (OMP_SECTION, void_type_node, substmt);
44192 add_stmt (substmt);
44195 while (1)
44197 tok = cp_lexer_peek_token (parser->lexer);
44198 if (tok->type == CPP_CLOSE_BRACE)
44199 break;
44200 if (tok->type == CPP_EOF)
44201 break;
44203 if (cp_parser_omp_section_scan (parser, "section", false))
44204 tok = cp_lexer_peek_token (parser->lexer);
44205 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
44207 cp_lexer_consume_token (parser->lexer);
44208 cp_parser_require_pragma_eol (parser, tok);
44209 error_suppress = false;
44211 else if (!error_suppress)
44213 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
44214 error_suppress = true;
44217 substmt = cp_parser_omp_structured_block_sequence (parser,
44218 PRAGMA_OMP_SECTION);
44219 substmt = build1 (OMP_SECTION, void_type_node, substmt);
44220 add_stmt (substmt);
44222 braces.require_close (parser);
44224 substmt = pop_stmt_list (stmt);
44226 stmt = make_node (OMP_SECTIONS);
44227 TREE_TYPE (stmt) = void_type_node;
44228 OMP_SECTIONS_BODY (stmt) = substmt;
44230 add_stmt (stmt);
44231 return stmt;
44234 /* OpenMP 2.5:
44235 # pragma omp sections sections-clause[optseq] newline
44236 sections-scope */
44238 #define OMP_SECTIONS_CLAUSE_MASK \
44239 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
44240 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
44241 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
44242 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
44243 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
44244 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
44246 static tree
44247 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
44248 char *p_name, omp_clause_mask mask, tree *cclauses)
44250 tree clauses, ret;
44251 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
44253 strcat (p_name, " sections");
44254 mask |= OMP_SECTIONS_CLAUSE_MASK;
44255 if (cclauses)
44256 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
44258 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
44259 cclauses == NULL);
44260 if (cclauses)
44262 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
44263 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
44266 ret = cp_parser_omp_sections_scope (parser);
44267 if (ret)
44268 OMP_SECTIONS_CLAUSES (ret) = clauses;
44270 return ret;
44273 /* OpenMP 2.5:
44274 # pragma omp parallel parallel-clause[optseq] new-line
44275 structured-block
44276 # pragma omp parallel for parallel-for-clause[optseq] new-line
44277 structured-block
44278 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
44279 structured-block
44281 OpenMP 4.0:
44282 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
44283 structured-block */
44285 #define OMP_PARALLEL_CLAUSE_MASK \
44286 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
44287 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
44288 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
44289 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
44290 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
44291 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
44292 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
44293 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
44294 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
44295 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
44297 static tree
44298 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
44299 char *p_name, omp_clause_mask mask, tree *cclauses,
44300 bool *if_p)
44302 tree stmt, clauses, block;
44303 unsigned int save;
44304 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
44306 strcat (p_name, " parallel");
44307 mask |= OMP_PARALLEL_CLAUSE_MASK;
44308 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
44309 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
44310 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
44311 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
44313 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
44315 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
44316 if (cclauses == NULL)
44317 cclauses = cclauses_buf;
44319 cp_lexer_consume_token (parser->lexer);
44320 if (!flag_openmp) /* flag_openmp_simd */
44321 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
44322 if_p);
44323 block = begin_omp_parallel ();
44324 save = cp_parser_begin_omp_structured_block (parser);
44325 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
44326 if_p);
44327 cp_parser_end_omp_structured_block (parser, save);
44328 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
44329 block);
44330 if (ret == NULL_TREE)
44331 return ret;
44332 OMP_PARALLEL_COMBINED (stmt) = 1;
44333 return stmt;
44335 /* When combined with distribute, parallel has to be followed by for.
44336 #pragma omp target parallel is allowed though. */
44337 else if (cclauses
44338 && (mask & (OMP_CLAUSE_MASK_1
44339 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
44341 error_at (loc, "expected %<for%> after %qs", p_name);
44342 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44343 return NULL_TREE;
44345 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
44347 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
44348 const char *p = IDENTIFIER_POINTER (id);
44349 if (cclauses == NULL && strcmp (p, "masked") == 0)
44351 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
44352 cclauses = cclauses_buf;
44354 cp_lexer_consume_token (parser->lexer);
44355 if (!flag_openmp) /* flag_openmp_simd */
44356 return cp_parser_omp_masked (parser, pragma_tok, p_name, mask,
44357 cclauses, if_p);
44358 block = begin_omp_parallel ();
44359 save = cp_parser_begin_omp_structured_block (parser);
44360 tree ret = cp_parser_omp_masked (parser, pragma_tok, p_name, mask,
44361 cclauses, if_p);
44362 cp_parser_end_omp_structured_block (parser, save);
44363 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
44364 block);
44365 if (ret == NULL_TREE)
44366 return ret;
44367 /* masked does have just filter clause, but during gimplification
44368 isn't represented by a gimplification omp context, so for
44369 #pragma omp parallel masked don't set OMP_PARALLEL_COMBINED,
44370 so that
44371 #pragma omp parallel masked
44372 #pragma omp taskloop simd lastprivate (x)
44373 isn't confused with
44374 #pragma omp parallel masked taskloop simd lastprivate (x) */
44375 if (OMP_MASKED_COMBINED (ret))
44376 OMP_PARALLEL_COMBINED (stmt) = 1;
44377 return stmt;
44379 else if (cclauses == NULL && strcmp (p, "master") == 0)
44381 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
44382 cclauses = cclauses_buf;
44384 cp_lexer_consume_token (parser->lexer);
44385 if (!flag_openmp) /* flag_openmp_simd */
44386 return cp_parser_omp_master (parser, pragma_tok, p_name, mask,
44387 cclauses, if_p);
44388 block = begin_omp_parallel ();
44389 save = cp_parser_begin_omp_structured_block (parser);
44390 tree ret = cp_parser_omp_master (parser, pragma_tok, p_name, mask,
44391 cclauses, if_p);
44392 cp_parser_end_omp_structured_block (parser, save);
44393 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
44394 block);
44395 if (ret == NULL_TREE)
44396 return ret;
44397 /* master doesn't have any clauses and during gimplification
44398 isn't represented by a gimplification omp context, so for
44399 #pragma omp parallel master don't set OMP_PARALLEL_COMBINED,
44400 so that
44401 #pragma omp parallel master
44402 #pragma omp taskloop simd lastprivate (x)
44403 isn't confused with
44404 #pragma omp parallel master taskloop simd lastprivate (x) */
44405 if (OMP_MASTER_COMBINED (ret))
44406 OMP_PARALLEL_COMBINED (stmt) = 1;
44407 return stmt;
44409 else if (strcmp (p, "loop") == 0)
44411 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
44412 if (cclauses == NULL)
44413 cclauses = cclauses_buf;
44415 cp_lexer_consume_token (parser->lexer);
44416 if (!flag_openmp) /* flag_openmp_simd */
44417 return cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
44418 cclauses, if_p);
44419 block = begin_omp_parallel ();
44420 save = cp_parser_begin_omp_structured_block (parser);
44421 tree ret = cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
44422 cclauses, if_p);
44423 cp_parser_end_omp_structured_block (parser, save);
44424 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
44425 block);
44426 if (ret == NULL_TREE)
44427 return ret;
44428 OMP_PARALLEL_COMBINED (stmt) = 1;
44429 return stmt;
44431 else if (!flag_openmp) /* flag_openmp_simd */
44433 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44434 return NULL_TREE;
44436 else if (cclauses == NULL && strcmp (p, "sections") == 0)
44438 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
44439 cclauses = cclauses_buf;
44441 cp_lexer_consume_token (parser->lexer);
44442 block = begin_omp_parallel ();
44443 save = cp_parser_begin_omp_structured_block (parser);
44444 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
44445 cp_parser_end_omp_structured_block (parser, save);
44446 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
44447 block);
44448 OMP_PARALLEL_COMBINED (stmt) = 1;
44449 return stmt;
44452 else if (!flag_openmp) /* flag_openmp_simd */
44454 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44455 return NULL_TREE;
44458 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
44459 cclauses == NULL);
44460 if (cclauses)
44462 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
44463 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
44466 block = begin_omp_parallel ();
44467 save = cp_parser_begin_omp_structured_block (parser);
44468 parser->omp_attrs_forbidden_p = true;
44469 cp_parser_statement (parser, NULL_TREE, false, if_p);
44470 cp_parser_end_omp_structured_block (parser, save);
44471 stmt = finish_omp_parallel (clauses, block);
44472 return stmt;
44475 /* OpenMP 2.5:
44476 # pragma omp single single-clause[optseq] new-line
44477 structured-block */
44479 #define OMP_SINGLE_CLAUSE_MASK \
44480 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
44481 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
44482 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
44483 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
44484 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
44486 static tree
44487 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
44489 tree stmt = make_node (OMP_SINGLE);
44490 TREE_TYPE (stmt) = void_type_node;
44491 SET_EXPR_LOCATION (stmt, pragma_tok->location);
44493 OMP_SINGLE_CLAUSES (stmt)
44494 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
44495 "#pragma omp single", pragma_tok);
44496 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
44498 return add_stmt (stmt);
44501 /* OpenMP 5.1:
44502 # pragma omp scope scope-clause[optseq] new-line
44503 structured-block */
44505 #define OMP_SCOPE_CLAUSE_MASK \
44506 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
44507 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
44508 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
44509 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
44510 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
44512 static tree
44513 cp_parser_omp_scope (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
44515 tree stmt = make_node (OMP_SCOPE);
44516 TREE_TYPE (stmt) = void_type_node;
44517 SET_EXPR_LOCATION (stmt, pragma_tok->location);
44519 OMP_SCOPE_CLAUSES (stmt)
44520 = cp_parser_omp_all_clauses (parser, OMP_SCOPE_CLAUSE_MASK,
44521 "#pragma omp scope", pragma_tok);
44522 OMP_SCOPE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
44524 return add_stmt (stmt);
44527 /* OpenMP 3.0:
44528 # pragma omp task task-clause[optseq] new-line
44529 structured-block */
44531 #define OMP_TASK_CLAUSE_MASK \
44532 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
44533 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
44534 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
44535 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
44536 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
44537 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
44538 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
44539 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
44540 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
44541 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
44542 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
44543 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION) \
44544 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DETACH) \
44545 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_AFFINITY))
44547 static tree
44548 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
44550 tree clauses, block;
44551 unsigned int save;
44553 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
44554 "#pragma omp task", pragma_tok);
44555 block = begin_omp_task ();
44556 save = cp_parser_begin_omp_structured_block (parser);
44557 parser->omp_attrs_forbidden_p = true;
44558 cp_parser_statement (parser, NULL_TREE, false, if_p);
44559 cp_parser_end_omp_structured_block (parser, save);
44560 return finish_omp_task (clauses, block);
44563 /* OpenMP 3.0:
44564 # pragma omp taskwait new-line
44566 OpenMP 5.0:
44567 # pragma omp taskwait taskwait-clause[opt] new-line */
44569 #define OMP_TASKWAIT_CLAUSE_MASK \
44570 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
44571 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
44573 static void
44574 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
44576 tree clauses
44577 = cp_parser_omp_all_clauses (parser, OMP_TASKWAIT_CLAUSE_MASK,
44578 "#pragma omp taskwait", pragma_tok);
44580 if (clauses)
44582 tree stmt = make_node (OMP_TASK);
44583 TREE_TYPE (stmt) = void_node;
44584 OMP_TASK_CLAUSES (stmt) = clauses;
44585 OMP_TASK_BODY (stmt) = NULL_TREE;
44586 SET_EXPR_LOCATION (stmt, pragma_tok->location);
44587 add_stmt (stmt);
44589 else
44590 finish_omp_taskwait ();
44593 /* OpenMP 3.1:
44594 # pragma omp taskyield new-line */
44596 static void
44597 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
44599 cp_parser_require_pragma_eol (parser, pragma_tok);
44600 finish_omp_taskyield ();
44603 /* OpenMP 4.0:
44604 # pragma omp taskgroup new-line
44605 structured-block
44607 OpenMP 5.0:
44608 # pragma omp taskgroup taskgroup-clause[optseq] new-line */
44610 #define OMP_TASKGROUP_CLAUSE_MASK \
44611 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
44612 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
44614 static tree
44615 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
44617 tree clauses
44618 = cp_parser_omp_all_clauses (parser, OMP_TASKGROUP_CLAUSE_MASK,
44619 "#pragma omp taskgroup", pragma_tok);
44620 return c_finish_omp_taskgroup (input_location,
44621 cp_parser_omp_structured_block (parser,
44622 if_p),
44623 clauses);
44627 /* OpenMP 2.5:
44628 # pragma omp threadprivate (variable-list) */
44630 static void
44631 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
44633 tree vars;
44635 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
44636 cp_parser_require_pragma_eol (parser, pragma_tok);
44638 finish_omp_threadprivate (vars);
44641 /* OpenMP 4.0:
44642 # pragma omp cancel cancel-clause[optseq] new-line */
44644 #define OMP_CANCEL_CLAUSE_MASK \
44645 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
44646 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
44647 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
44648 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
44649 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
44651 static void
44652 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
44654 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
44655 "#pragma omp cancel", pragma_tok);
44656 finish_omp_cancel (clauses);
44659 /* OpenMP 4.0:
44660 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
44662 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
44663 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
44664 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
44665 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
44666 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
44668 static bool
44669 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
44670 enum pragma_context context)
44672 tree clauses;
44673 bool point_seen = false;
44675 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
44677 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
44678 const char *p = IDENTIFIER_POINTER (id);
44680 if (strcmp (p, "point") == 0)
44682 cp_lexer_consume_token (parser->lexer);
44683 point_seen = true;
44686 if (!point_seen)
44688 cp_parser_error (parser, "expected %<point%>");
44689 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44690 return false;
44693 if (context != pragma_compound)
44695 if (context == pragma_stmt)
44696 error_at (pragma_tok->location,
44697 "%<#pragma %s%> may only be used in compound statements",
44698 "omp cancellation point");
44699 else
44700 cp_parser_error (parser, "expected declaration specifiers");
44701 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44702 return true;
44705 clauses = cp_parser_omp_all_clauses (parser,
44706 OMP_CANCELLATION_POINT_CLAUSE_MASK,
44707 "#pragma omp cancellation point",
44708 pragma_tok);
44709 finish_omp_cancellation_point (clauses);
44710 return true;
44713 /* OpenMP 4.0:
44714 #pragma omp distribute distribute-clause[optseq] new-line
44715 for-loop */
44717 #define OMP_DISTRIBUTE_CLAUSE_MASK \
44718 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
44719 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
44720 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
44721 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
44722 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
44723 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
44724 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
44726 static tree
44727 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
44728 char *p_name, omp_clause_mask mask, tree *cclauses,
44729 bool *if_p)
44731 tree clauses, sb, ret;
44732 unsigned int save;
44733 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
44735 strcat (p_name, " distribute");
44736 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
44738 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
44740 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
44741 const char *p = IDENTIFIER_POINTER (id);
44742 bool simd = false;
44743 bool parallel = false;
44745 if (strcmp (p, "simd") == 0)
44746 simd = true;
44747 else
44748 parallel = strcmp (p, "parallel") == 0;
44749 if (parallel || simd)
44751 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
44752 if (cclauses == NULL)
44753 cclauses = cclauses_buf;
44754 cp_lexer_consume_token (parser->lexer);
44755 if (!flag_openmp) /* flag_openmp_simd */
44757 if (simd)
44758 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
44759 cclauses, if_p);
44760 else
44761 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
44762 cclauses, if_p);
44764 sb = begin_omp_structured_block ();
44765 save = cp_parser_begin_omp_structured_block (parser);
44766 if (simd)
44767 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
44768 cclauses, if_p);
44769 else
44770 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
44771 cclauses, if_p);
44772 cp_parser_end_omp_structured_block (parser, save);
44773 tree body = finish_omp_structured_block (sb);
44774 if (ret == NULL)
44775 return ret;
44776 ret = make_node (OMP_DISTRIBUTE);
44777 TREE_TYPE (ret) = void_type_node;
44778 OMP_FOR_BODY (ret) = body;
44779 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
44780 SET_EXPR_LOCATION (ret, loc);
44781 add_stmt (ret);
44782 return ret;
44785 if (!flag_openmp) /* flag_openmp_simd */
44787 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44788 return NULL_TREE;
44791 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
44792 cclauses == NULL);
44793 if (cclauses)
44795 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
44796 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
44799 keep_next_level (true);
44800 sb = begin_omp_structured_block ();
44801 save = cp_parser_begin_omp_structured_block (parser);
44803 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
44805 cp_parser_end_omp_structured_block (parser, save);
44806 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
44808 return ret;
44811 /* OpenMP 4.0:
44812 # pragma omp teams teams-clause[optseq] new-line
44813 structured-block */
44815 #define OMP_TEAMS_CLAUSE_MASK \
44816 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
44817 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
44818 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
44819 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
44820 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
44821 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
44822 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
44823 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
44825 static tree
44826 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
44827 char *p_name, omp_clause_mask mask, tree *cclauses,
44828 bool *if_p)
44830 tree clauses, sb, ret;
44831 unsigned int save;
44832 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
44834 strcat (p_name, " teams");
44835 mask |= OMP_TEAMS_CLAUSE_MASK;
44837 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
44839 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
44840 const char *p = IDENTIFIER_POINTER (id);
44841 if (strcmp (p, "distribute") == 0)
44843 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
44844 if (cclauses == NULL)
44845 cclauses = cclauses_buf;
44847 cp_lexer_consume_token (parser->lexer);
44848 if (!flag_openmp) /* flag_openmp_simd */
44849 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
44850 cclauses, if_p);
44851 keep_next_level (true);
44852 sb = begin_omp_structured_block ();
44853 save = cp_parser_begin_omp_structured_block (parser);
44854 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
44855 cclauses, if_p);
44856 cp_parser_end_omp_structured_block (parser, save);
44857 tree body = finish_omp_structured_block (sb);
44858 if (ret == NULL)
44859 return ret;
44860 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
44861 ret = make_node (OMP_TEAMS);
44862 TREE_TYPE (ret) = void_type_node;
44863 OMP_TEAMS_CLAUSES (ret) = clauses;
44864 OMP_TEAMS_BODY (ret) = body;
44865 OMP_TEAMS_COMBINED (ret) = 1;
44866 SET_EXPR_LOCATION (ret, loc);
44867 return add_stmt (ret);
44869 else if (strcmp (p, "loop") == 0)
44871 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
44872 if (cclauses == NULL)
44873 cclauses = cclauses_buf;
44875 cp_lexer_consume_token (parser->lexer);
44876 if (!flag_openmp) /* flag_openmp_simd */
44877 return cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
44878 cclauses, if_p);
44879 keep_next_level (true);
44880 sb = begin_omp_structured_block ();
44881 save = cp_parser_begin_omp_structured_block (parser);
44882 ret = cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
44883 cclauses, if_p);
44884 cp_parser_end_omp_structured_block (parser, save);
44885 tree body = finish_omp_structured_block (sb);
44886 if (ret == NULL)
44887 return ret;
44888 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
44889 ret = make_node (OMP_TEAMS);
44890 TREE_TYPE (ret) = void_type_node;
44891 OMP_TEAMS_CLAUSES (ret) = clauses;
44892 OMP_TEAMS_BODY (ret) = body;
44893 OMP_TEAMS_COMBINED (ret) = 1;
44894 SET_EXPR_LOCATION (ret, loc);
44895 return add_stmt (ret);
44898 if (!flag_openmp) /* flag_openmp_simd */
44900 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44901 return NULL_TREE;
44904 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
44905 cclauses == NULL);
44906 if (cclauses)
44908 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
44909 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
44912 tree stmt = make_node (OMP_TEAMS);
44913 TREE_TYPE (stmt) = void_type_node;
44914 OMP_TEAMS_CLAUSES (stmt) = clauses;
44915 keep_next_level (true);
44916 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
44917 SET_EXPR_LOCATION (stmt, loc);
44919 return add_stmt (stmt);
44922 /* OpenMP 4.0:
44923 # pragma omp target data target-data-clause[optseq] new-line
44924 structured-block */
44926 #define OMP_TARGET_DATA_CLAUSE_MASK \
44927 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
44928 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
44929 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
44930 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR) \
44931 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR))
44933 static tree
44934 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
44936 if (flag_openmp)
44937 omp_requires_mask
44938 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
44940 tree clauses
44941 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
44942 "#pragma omp target data", pragma_tok);
44943 c_omp_adjust_map_clauses (clauses, false);
44944 int map_seen = 0;
44945 for (tree *pc = &clauses; *pc;)
44947 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
44948 switch (OMP_CLAUSE_MAP_KIND (*pc))
44950 case GOMP_MAP_TO:
44951 case GOMP_MAP_ALWAYS_TO:
44952 case GOMP_MAP_FROM:
44953 case GOMP_MAP_ALWAYS_FROM:
44954 case GOMP_MAP_TOFROM:
44955 case GOMP_MAP_ALWAYS_TOFROM:
44956 case GOMP_MAP_ALLOC:
44957 map_seen = 3;
44958 break;
44959 case GOMP_MAP_FIRSTPRIVATE_POINTER:
44960 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
44961 case GOMP_MAP_ALWAYS_POINTER:
44962 case GOMP_MAP_ATTACH_DETACH:
44963 break;
44964 default:
44965 map_seen |= 1;
44966 error_at (OMP_CLAUSE_LOCATION (*pc),
44967 "%<#pragma omp target data%> with map-type other "
44968 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
44969 "on %<map%> clause");
44970 *pc = OMP_CLAUSE_CHAIN (*pc);
44971 continue;
44973 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_PTR
44974 || OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_ADDR)
44975 map_seen = 3;
44976 pc = &OMP_CLAUSE_CHAIN (*pc);
44979 if (map_seen != 3)
44981 if (map_seen == 0)
44982 error_at (pragma_tok->location,
44983 "%<#pragma omp target data%> must contain at least "
44984 "one %<map%>, %<use_device_ptr%> or %<use_device_addr%> "
44985 "clause");
44986 return NULL_TREE;
44989 tree stmt = make_node (OMP_TARGET_DATA);
44990 TREE_TYPE (stmt) = void_type_node;
44991 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
44993 keep_next_level (true);
44994 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
44996 SET_EXPR_LOCATION (stmt, pragma_tok->location);
44997 return add_stmt (stmt);
45000 /* OpenMP 4.5:
45001 # pragma omp target enter data target-enter-data-clause[optseq] new-line
45002 structured-block */
45004 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
45005 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
45006 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
45007 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
45008 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
45009 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
45011 static bool
45012 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
45013 enum pragma_context context)
45015 bool data_seen = false;
45016 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45018 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
45019 const char *p = IDENTIFIER_POINTER (id);
45021 if (strcmp (p, "data") == 0)
45023 cp_lexer_consume_token (parser->lexer);
45024 data_seen = true;
45027 if (!data_seen)
45029 cp_parser_error (parser, "expected %<data%>");
45030 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45031 return false;
45034 if (context == pragma_stmt)
45036 error_at (pragma_tok->location,
45037 "%<#pragma %s%> may only be used in compound statements",
45038 "omp target enter data");
45039 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45040 return true;
45043 if (flag_openmp)
45044 omp_requires_mask
45045 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
45047 tree clauses
45048 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
45049 "#pragma omp target enter data", pragma_tok);
45050 c_omp_adjust_map_clauses (clauses, false);
45051 int map_seen = 0;
45052 for (tree *pc = &clauses; *pc;)
45054 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
45055 switch (OMP_CLAUSE_MAP_KIND (*pc))
45057 case GOMP_MAP_TO:
45058 case GOMP_MAP_ALWAYS_TO:
45059 case GOMP_MAP_ALLOC:
45060 map_seen = 3;
45061 break;
45062 case GOMP_MAP_TOFROM:
45063 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_TO);
45064 map_seen = 3;
45065 break;
45066 case GOMP_MAP_ALWAYS_TOFROM:
45067 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_ALWAYS_TO);
45068 map_seen = 3;
45069 break;
45070 case GOMP_MAP_FIRSTPRIVATE_POINTER:
45071 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
45072 case GOMP_MAP_ALWAYS_POINTER:
45073 case GOMP_MAP_ATTACH_DETACH:
45074 break;
45075 default:
45076 map_seen |= 1;
45077 error_at (OMP_CLAUSE_LOCATION (*pc),
45078 "%<#pragma omp target enter data%> with map-type other "
45079 "than %<to%>, %<tofrom%> or %<alloc%> on %<map%> clause");
45080 *pc = OMP_CLAUSE_CHAIN (*pc);
45081 continue;
45083 pc = &OMP_CLAUSE_CHAIN (*pc);
45086 if (map_seen != 3)
45088 if (map_seen == 0)
45089 error_at (pragma_tok->location,
45090 "%<#pragma omp target enter data%> must contain at least "
45091 "one %<map%> clause");
45092 return true;
45095 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
45096 TREE_TYPE (stmt) = void_type_node;
45097 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
45098 SET_EXPR_LOCATION (stmt, pragma_tok->location);
45099 add_stmt (stmt);
45100 return true;
45103 /* OpenMP 4.5:
45104 # pragma omp target exit data target-enter-data-clause[optseq] new-line
45105 structured-block */
45107 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
45108 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
45109 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
45110 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
45111 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
45112 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
45114 static bool
45115 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
45116 enum pragma_context context)
45118 bool data_seen = false;
45119 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45121 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
45122 const char *p = IDENTIFIER_POINTER (id);
45124 if (strcmp (p, "data") == 0)
45126 cp_lexer_consume_token (parser->lexer);
45127 data_seen = true;
45130 if (!data_seen)
45132 cp_parser_error (parser, "expected %<data%>");
45133 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45134 return false;
45137 if (context == pragma_stmt)
45139 error_at (pragma_tok->location,
45140 "%<#pragma %s%> may only be used in compound statements",
45141 "omp target exit data");
45142 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45143 return true;
45146 if (flag_openmp)
45147 omp_requires_mask
45148 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
45150 tree clauses
45151 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
45152 "#pragma omp target exit data", pragma_tok);
45153 c_omp_adjust_map_clauses (clauses, false);
45154 int map_seen = 0;
45155 for (tree *pc = &clauses; *pc;)
45157 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
45158 switch (OMP_CLAUSE_MAP_KIND (*pc))
45160 case GOMP_MAP_FROM:
45161 case GOMP_MAP_ALWAYS_FROM:
45162 case GOMP_MAP_RELEASE:
45163 case GOMP_MAP_DELETE:
45164 map_seen = 3;
45165 break;
45166 case GOMP_MAP_TOFROM:
45167 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_FROM);
45168 map_seen = 3;
45169 break;
45170 case GOMP_MAP_ALWAYS_TOFROM:
45171 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_ALWAYS_FROM);
45172 map_seen = 3;
45173 break;
45174 case GOMP_MAP_FIRSTPRIVATE_POINTER:
45175 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
45176 case GOMP_MAP_ALWAYS_POINTER:
45177 case GOMP_MAP_ATTACH_DETACH:
45178 break;
45179 default:
45180 map_seen |= 1;
45181 error_at (OMP_CLAUSE_LOCATION (*pc),
45182 "%<#pragma omp target exit data%> with map-type other "
45183 "than %<from%>, %<tofrom%>, %<release%> or %<delete%> "
45184 "on %<map%> clause");
45185 *pc = OMP_CLAUSE_CHAIN (*pc);
45186 continue;
45188 pc = &OMP_CLAUSE_CHAIN (*pc);
45191 if (map_seen != 3)
45193 if (map_seen == 0)
45194 error_at (pragma_tok->location,
45195 "%<#pragma omp target exit data%> must contain at least "
45196 "one %<map%> clause");
45197 return true;
45200 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
45201 TREE_TYPE (stmt) = void_type_node;
45202 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
45203 SET_EXPR_LOCATION (stmt, pragma_tok->location);
45204 add_stmt (stmt);
45205 return true;
45208 /* OpenMP 4.0:
45209 # pragma omp target update target-update-clause[optseq] new-line */
45211 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
45212 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
45213 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
45214 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
45215 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
45216 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
45217 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
45219 static bool
45220 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
45221 enum pragma_context context)
45223 if (context == pragma_stmt)
45225 error_at (pragma_tok->location,
45226 "%<#pragma %s%> may only be used in compound statements",
45227 "omp target update");
45228 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45229 return true;
45232 tree clauses
45233 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
45234 "#pragma omp target update", pragma_tok);
45235 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
45236 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
45238 error_at (pragma_tok->location,
45239 "%<#pragma omp target update%> must contain at least one "
45240 "%<from%> or %<to%> clauses");
45241 return true;
45244 if (flag_openmp)
45245 omp_requires_mask
45246 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
45248 tree stmt = make_node (OMP_TARGET_UPDATE);
45249 TREE_TYPE (stmt) = void_type_node;
45250 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
45251 SET_EXPR_LOCATION (stmt, pragma_tok->location);
45252 add_stmt (stmt);
45253 return true;
45256 /* OpenMP 4.0:
45257 # pragma omp target target-clause[optseq] new-line
45258 structured-block */
45260 #define OMP_TARGET_CLAUSE_MASK \
45261 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
45262 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
45263 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
45264 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
45265 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
45266 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
45267 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
45268 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
45269 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
45270 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION) \
45271 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
45272 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR)\
45273 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HAS_DEVICE_ADDR))
45275 static bool
45276 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
45277 enum pragma_context context, bool *if_p)
45279 if (flag_openmp)
45280 omp_requires_mask
45281 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
45283 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45285 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
45286 const char *p = IDENTIFIER_POINTER (id);
45287 enum tree_code ccode = ERROR_MARK;
45289 if (strcmp (p, "teams") == 0)
45290 ccode = OMP_TEAMS;
45291 else if (strcmp (p, "parallel") == 0)
45292 ccode = OMP_PARALLEL;
45293 else if (strcmp (p, "simd") == 0)
45294 ccode = OMP_SIMD;
45295 if (ccode != ERROR_MARK)
45297 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
45298 char p_name[sizeof ("#pragma omp target teams distribute "
45299 "parallel for simd")];
45301 cp_lexer_consume_token (parser->lexer);
45302 strcpy (p_name, "#pragma omp target");
45303 if (!flag_openmp) /* flag_openmp_simd */
45305 tree stmt;
45306 switch (ccode)
45308 case OMP_TEAMS:
45309 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
45310 OMP_TARGET_CLAUSE_MASK,
45311 cclauses, if_p);
45312 break;
45313 case OMP_PARALLEL:
45314 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
45315 OMP_TARGET_CLAUSE_MASK,
45316 cclauses, if_p);
45317 break;
45318 case OMP_SIMD:
45319 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
45320 OMP_TARGET_CLAUSE_MASK,
45321 cclauses, if_p);
45322 break;
45323 default:
45324 gcc_unreachable ();
45326 return stmt != NULL_TREE;
45328 keep_next_level (true);
45329 tree sb = begin_omp_structured_block (), ret;
45330 unsigned save = cp_parser_begin_omp_structured_block (parser);
45331 switch (ccode)
45333 case OMP_TEAMS:
45334 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
45335 OMP_TARGET_CLAUSE_MASK, cclauses,
45336 if_p);
45337 break;
45338 case OMP_PARALLEL:
45339 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
45340 OMP_TARGET_CLAUSE_MASK, cclauses,
45341 if_p);
45342 break;
45343 case OMP_SIMD:
45344 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
45345 OMP_TARGET_CLAUSE_MASK, cclauses,
45346 if_p);
45347 break;
45348 default:
45349 gcc_unreachable ();
45351 cp_parser_end_omp_structured_block (parser, save);
45352 tree body = finish_omp_structured_block (sb);
45353 if (ret == NULL_TREE)
45354 return false;
45355 if (ccode == OMP_TEAMS && !processing_template_decl)
45356 /* For combined target teams, ensure the num_teams and
45357 thread_limit clause expressions are evaluated on the host,
45358 before entering the target construct. */
45359 for (tree c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
45360 c; c = OMP_CLAUSE_CHAIN (c))
45361 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
45362 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
45363 for (int i = 0;
45364 i <= (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS); ++i)
45365 if (OMP_CLAUSE_OPERAND (c, i)
45366 && TREE_CODE (OMP_CLAUSE_OPERAND (c, i)) != INTEGER_CST)
45368 tree expr = OMP_CLAUSE_OPERAND (c, i);
45369 expr = force_target_expr (TREE_TYPE (expr), expr,
45370 tf_none);
45371 if (expr == error_mark_node)
45372 continue;
45373 tree tmp = TARGET_EXPR_SLOT (expr);
45374 add_stmt (expr);
45375 OMP_CLAUSE_OPERAND (c, i) = expr;
45376 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
45377 OMP_CLAUSE_FIRSTPRIVATE);
45378 OMP_CLAUSE_DECL (tc) = tmp;
45379 OMP_CLAUSE_CHAIN (tc)
45380 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
45381 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
45383 c_omp_adjust_map_clauses (cclauses[C_OMP_CLAUSE_SPLIT_TARGET], true);
45384 finish_omp_target (pragma_tok->location,
45385 cclauses[C_OMP_CLAUSE_SPLIT_TARGET], body, true);
45386 return true;
45388 else if (!flag_openmp) /* flag_openmp_simd */
45390 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45391 return false;
45393 else if (strcmp (p, "data") == 0)
45395 cp_lexer_consume_token (parser->lexer);
45396 cp_parser_omp_target_data (parser, pragma_tok, if_p);
45397 return true;
45399 else if (strcmp (p, "enter") == 0)
45401 cp_lexer_consume_token (parser->lexer);
45402 return cp_parser_omp_target_enter_data (parser, pragma_tok, context);
45404 else if (strcmp (p, "exit") == 0)
45406 cp_lexer_consume_token (parser->lexer);
45407 return cp_parser_omp_target_exit_data (parser, pragma_tok, context);
45409 else if (strcmp (p, "update") == 0)
45411 cp_lexer_consume_token (parser->lexer);
45412 return cp_parser_omp_target_update (parser, pragma_tok, context);
45415 if (!flag_openmp) /* flag_openmp_simd */
45417 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45418 return false;
45421 tree clauses = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
45422 "#pragma omp target", pragma_tok,
45423 false);
45424 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
45425 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
45427 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
45428 OMP_CLAUSE_DECL (nc) = OMP_CLAUSE_DECL (c);
45429 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_ALWAYS_TOFROM);
45430 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (c);
45431 OMP_CLAUSE_CHAIN (c) = nc;
45433 clauses = finish_omp_clauses (clauses, C_ORT_OMP_TARGET);
45435 c_omp_adjust_map_clauses (clauses, true);
45436 keep_next_level (true);
45437 tree body = cp_parser_omp_structured_block (parser, if_p);
45439 finish_omp_target (pragma_tok->location, clauses, body, false);
45440 return true;
45443 /* OpenACC 2.0:
45444 # pragma acc cache (variable-list) new-line
45447 static tree
45448 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
45450 /* Don't create location wrapper nodes within 'OMP_CLAUSE__CACHE_'
45451 clauses. */
45452 auto_suppress_location_wrappers sentinel;
45454 tree stmt, clauses;
45456 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
45457 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
45459 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
45461 stmt = make_node (OACC_CACHE);
45462 TREE_TYPE (stmt) = void_type_node;
45463 OACC_CACHE_CLAUSES (stmt) = clauses;
45464 SET_EXPR_LOCATION (stmt, pragma_tok->location);
45465 add_stmt (stmt);
45467 return stmt;
45470 /* OpenACC 2.0:
45471 # pragma acc data oacc-data-clause[optseq] new-line
45472 structured-block */
45474 #define OACC_DATA_CLAUSE_MASK \
45475 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
45476 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
45477 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
45478 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
45479 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
45480 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DETACH) \
45481 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
45482 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
45483 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
45484 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
45486 static tree
45487 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
45489 tree stmt, clauses, block;
45490 unsigned int save;
45492 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
45493 "#pragma acc data", pragma_tok);
45495 block = begin_omp_parallel ();
45496 save = cp_parser_begin_omp_structured_block (parser);
45497 cp_parser_statement (parser, NULL_TREE, false, if_p);
45498 cp_parser_end_omp_structured_block (parser, save);
45499 stmt = finish_oacc_data (clauses, block);
45500 return stmt;
45503 /* OpenACC 2.0:
45504 # pragma acc host_data <clauses> new-line
45505 structured-block */
45507 #define OACC_HOST_DATA_CLAUSE_MASK \
45508 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) \
45509 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
45510 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) )
45512 static tree
45513 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
45515 tree stmt, clauses, block;
45516 unsigned int save;
45518 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
45519 "#pragma acc host_data", pragma_tok);
45521 block = begin_omp_parallel ();
45522 save = cp_parser_begin_omp_structured_block (parser);
45523 cp_parser_statement (parser, NULL_TREE, false, if_p);
45524 cp_parser_end_omp_structured_block (parser, save);
45525 stmt = finish_oacc_host_data (clauses, block);
45526 return stmt;
45529 /* OpenACC 2.0:
45530 # pragma acc declare oacc-data-clause[optseq] new-line
45533 #define OACC_DECLARE_CLAUSE_MASK \
45534 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
45535 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
45536 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
45537 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
45538 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
45539 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
45540 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
45541 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
45543 static tree
45544 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
45546 tree clauses, stmt;
45547 bool error = false;
45548 bool found_in_scope = global_bindings_p ();
45550 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
45551 "#pragma acc declare", pragma_tok, true);
45554 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
45556 error_at (pragma_tok->location,
45557 "no valid clauses specified in %<#pragma acc declare%>");
45558 return NULL_TREE;
45561 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
45563 location_t loc = OMP_CLAUSE_LOCATION (t);
45564 tree decl = OMP_CLAUSE_DECL (t);
45565 if (!DECL_P (decl))
45567 error_at (loc, "array section in %<#pragma acc declare%>");
45568 error = true;
45569 continue;
45571 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
45572 switch (OMP_CLAUSE_MAP_KIND (t))
45574 case GOMP_MAP_FIRSTPRIVATE_POINTER:
45575 case GOMP_MAP_ALLOC:
45576 case GOMP_MAP_TO:
45577 case GOMP_MAP_FORCE_DEVICEPTR:
45578 case GOMP_MAP_DEVICE_RESIDENT:
45579 break;
45581 case GOMP_MAP_LINK:
45582 if (!global_bindings_p ()
45583 && (TREE_STATIC (decl)
45584 || !DECL_EXTERNAL (decl)))
45586 error_at (loc,
45587 "%qD must be a global variable in "
45588 "%<#pragma acc declare link%>",
45589 decl);
45590 error = true;
45591 continue;
45593 break;
45595 default:
45596 if (global_bindings_p ())
45598 error_at (loc, "invalid OpenACC clause at file scope");
45599 error = true;
45600 continue;
45602 if (DECL_EXTERNAL (decl))
45604 error_at (loc,
45605 "invalid use of %<extern%> variable %qD "
45606 "in %<#pragma acc declare%>", decl);
45607 error = true;
45608 continue;
45610 else if (TREE_PUBLIC (decl))
45612 error_at (loc,
45613 "invalid use of %<global%> variable %qD "
45614 "in %<#pragma acc declare%>", decl);
45615 error = true;
45616 continue;
45618 break;
45621 if (!found_in_scope)
45622 /* This seems to ignore the existence of cleanup scopes?
45623 What is the meaning for local extern decls? The local
45624 extern is in this scope, but it is referring to a decl that
45625 is namespace scope. */
45626 for (tree d = current_binding_level->names; d; d = TREE_CHAIN (d))
45627 if (d == decl)
45629 found_in_scope = true;
45630 break;
45632 if (!found_in_scope)
45634 error_at (loc,
45635 "%qD must be a variable declared in the same scope as "
45636 "%<#pragma acc declare%>", decl);
45637 error = true;
45638 continue;
45641 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
45642 || lookup_attribute ("omp declare target link",
45643 DECL_ATTRIBUTES (decl)))
45645 error_at (loc, "variable %qD used more than once with "
45646 "%<#pragma acc declare%>", decl);
45647 error = true;
45648 continue;
45651 if (!error)
45653 tree id;
45655 if (DECL_LOCAL_DECL_P (decl))
45656 /* We need to mark the aliased decl, as that is the entity
45657 that is being referred to. This won't work for
45658 dependent variables, but it didn't work for them before
45659 DECL_LOCAL_DECL_P was a thing either. But then
45660 dependent local extern variable decls are as rare as
45661 hen's teeth. */
45662 if (auto alias = DECL_LOCAL_DECL_ALIAS (decl))
45663 if (alias != error_mark_node)
45664 decl = alias;
45666 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
45667 id = get_identifier ("omp declare target link");
45668 else
45669 id = get_identifier ("omp declare target");
45671 DECL_ATTRIBUTES (decl)
45672 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
45673 if (current_binding_level->kind == sk_namespace)
45675 symtab_node *node = symtab_node::get (decl);
45676 if (node != NULL)
45678 node->offloadable = 1;
45679 if (ENABLE_OFFLOADING)
45681 g->have_offload = true;
45682 if (is_a <varpool_node *> (node))
45683 vec_safe_push (offload_vars, decl);
45690 if (error || current_binding_level->kind == sk_namespace)
45691 return NULL_TREE;
45693 stmt = make_node (OACC_DECLARE);
45694 TREE_TYPE (stmt) = void_type_node;
45695 OACC_DECLARE_CLAUSES (stmt) = clauses;
45696 SET_EXPR_LOCATION (stmt, pragma_tok->location);
45698 add_stmt (stmt);
45700 return NULL_TREE;
45703 /* OpenACC 2.0:
45704 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
45708 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
45710 LOC is the location of the #pragma token.
45713 #define OACC_ENTER_DATA_CLAUSE_MASK \
45714 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
45715 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
45716 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
45717 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
45718 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
45719 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
45721 #define OACC_EXIT_DATA_CLAUSE_MASK \
45722 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
45723 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
45724 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
45725 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
45726 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DETACH) \
45727 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
45728 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
45730 static tree
45731 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
45732 bool enter)
45734 location_t loc = pragma_tok->location;
45735 tree stmt, clauses;
45736 const char *p = "";
45738 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45739 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
45741 if (strcmp (p, "data") != 0)
45743 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
45744 enter ? "enter" : "exit");
45745 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45746 return NULL_TREE;
45749 cp_lexer_consume_token (parser->lexer);
45751 if (enter)
45752 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
45753 "#pragma acc enter data", pragma_tok);
45754 else
45755 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
45756 "#pragma acc exit data", pragma_tok);
45758 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
45760 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
45761 enter ? "enter" : "exit");
45762 return NULL_TREE;
45765 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
45766 TREE_TYPE (stmt) = void_type_node;
45767 OMP_STANDALONE_CLAUSES (stmt) = clauses;
45768 SET_EXPR_LOCATION (stmt, loc);
45769 add_stmt (stmt);
45770 return stmt;
45773 /* OpenACC 2.0:
45774 # pragma acc loop oacc-loop-clause[optseq] new-line
45775 structured-block */
45777 #define OACC_LOOP_CLAUSE_MASK \
45778 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
45779 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
45780 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
45781 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
45782 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
45783 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
45784 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
45785 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
45786 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
45787 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
45789 static tree
45790 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
45791 omp_clause_mask mask, tree *cclauses, bool *if_p)
45793 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
45795 strcat (p_name, " loop");
45796 mask |= OACC_LOOP_CLAUSE_MASK;
45798 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
45799 cclauses == NULL);
45800 if (cclauses)
45802 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
45803 if (*cclauses)
45804 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
45805 if (clauses)
45806 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
45809 tree block = begin_omp_structured_block ();
45810 int save = cp_parser_begin_omp_structured_block (parser);
45811 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
45812 cp_parser_end_omp_structured_block (parser, save);
45813 add_stmt (finish_omp_structured_block (block));
45815 return stmt;
45818 /* OpenACC 2.0:
45819 # pragma acc kernels oacc-kernels-clause[optseq] new-line
45820 structured-block
45824 # pragma acc parallel oacc-parallel-clause[optseq] new-line
45825 structured-block
45827 OpenACC 2.6:
45829 # pragma acc serial oacc-serial-clause[optseq] new-line
45832 #define OACC_KERNELS_CLAUSE_MASK \
45833 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
45834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
45835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
45836 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
45837 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
45838 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
45839 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
45840 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
45841 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
45842 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
45843 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
45844 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
45845 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
45846 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
45847 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
45849 #define OACC_PARALLEL_CLAUSE_MASK \
45850 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
45851 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
45852 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
45853 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
45854 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
45855 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
45856 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
45857 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
45858 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
45859 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
45860 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
45861 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
45862 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
45863 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
45864 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
45865 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
45866 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
45867 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
45869 #define OACC_SERIAL_CLAUSE_MASK \
45870 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
45871 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
45872 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
45873 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
45874 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
45875 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
45876 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
45877 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
45878 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
45879 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
45880 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
45881 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
45882 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
45883 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
45884 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
45886 static tree
45887 cp_parser_oacc_compute (cp_parser *parser, cp_token *pragma_tok,
45888 char *p_name, bool *if_p)
45890 omp_clause_mask mask;
45891 enum tree_code code;
45892 switch (cp_parser_pragma_kind (pragma_tok))
45894 case PRAGMA_OACC_KERNELS:
45895 strcat (p_name, " kernels");
45896 mask = OACC_KERNELS_CLAUSE_MASK;
45897 code = OACC_KERNELS;
45898 break;
45899 case PRAGMA_OACC_PARALLEL:
45900 strcat (p_name, " parallel");
45901 mask = OACC_PARALLEL_CLAUSE_MASK;
45902 code = OACC_PARALLEL;
45903 break;
45904 case PRAGMA_OACC_SERIAL:
45905 strcat (p_name, " serial");
45906 mask = OACC_SERIAL_CLAUSE_MASK;
45907 code = OACC_SERIAL;
45908 break;
45909 default:
45910 gcc_unreachable ();
45913 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45915 const char *p
45916 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
45917 if (strcmp (p, "loop") == 0)
45919 cp_lexer_consume_token (parser->lexer);
45920 tree block = begin_omp_parallel ();
45921 tree clauses;
45922 tree stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask,
45923 &clauses, if_p);
45924 protected_set_expr_location (stmt, pragma_tok->location);
45925 return finish_omp_construct (code, block, clauses);
45929 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
45931 tree block = begin_omp_parallel ();
45932 unsigned int save = cp_parser_begin_omp_structured_block (parser);
45933 cp_parser_statement (parser, NULL_TREE, false, if_p);
45934 cp_parser_end_omp_structured_block (parser, save);
45935 return finish_omp_construct (code, block, clauses);
45938 /* OpenACC 2.0:
45939 # pragma acc update oacc-update-clause[optseq] new-line
45942 #define OACC_UPDATE_CLAUSE_MASK \
45943 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
45944 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
45945 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
45946 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
45947 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
45948 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
45950 static tree
45951 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
45953 tree stmt, clauses;
45955 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
45956 "#pragma acc update", pragma_tok);
45958 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
45960 error_at (pragma_tok->location,
45961 "%<#pragma acc update%> must contain at least one "
45962 "%<device%> or %<host%> or %<self%> clause");
45963 return NULL_TREE;
45966 stmt = make_node (OACC_UPDATE);
45967 TREE_TYPE (stmt) = void_type_node;
45968 OACC_UPDATE_CLAUSES (stmt) = clauses;
45969 SET_EXPR_LOCATION (stmt, pragma_tok->location);
45970 add_stmt (stmt);
45971 return stmt;
45974 /* OpenACC 2.0:
45975 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
45977 LOC is the location of the #pragma token.
45980 #define OACC_WAIT_CLAUSE_MASK \
45981 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
45983 static tree
45984 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
45986 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
45987 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
45989 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
45990 list = cp_parser_oacc_wait_list (parser, loc, list);
45992 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
45993 "#pragma acc wait", pragma_tok);
45995 stmt = c_finish_oacc_wait (loc, list, clauses);
45996 stmt = finish_expr_stmt (stmt);
45998 return stmt;
46001 /* OpenMP 4.0:
46002 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
46004 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
46005 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
46006 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
46007 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
46008 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
46009 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
46010 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
46012 static void
46013 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
46014 enum pragma_context context,
46015 bool variant_p)
46017 bool first_p = parser->omp_declare_simd == NULL;
46018 cp_omp_declare_simd_data data;
46019 if (first_p)
46021 data.error_seen = false;
46022 data.fndecl_seen = false;
46023 data.variant_p = variant_p;
46024 data.tokens = vNULL;
46025 data.attribs[0] = NULL;
46026 data.attribs[1] = NULL;
46027 data.loc = UNKNOWN_LOCATION;
46028 /* It is safe to take the address of a local variable; it will only be
46029 used while this scope is live. */
46030 parser->omp_declare_simd = &data;
46032 else if (parser->omp_declare_simd->variant_p != variant_p)
46034 error_at (pragma_tok->location,
46035 "%<#pragma omp declare %s%> followed by "
46036 "%<#pragma omp declare %s%>",
46037 parser->omp_declare_simd->variant_p ? "variant" : "simd",
46038 parser->omp_declare_simd->variant_p ? "simd" : "variant");
46039 parser->omp_declare_simd->error_seen = true;
46042 /* Store away all pragma tokens. */
46043 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
46044 cp_lexer_consume_token (parser->lexer);
46045 cp_parser_require_pragma_eol (parser, pragma_tok);
46046 struct cp_token_cache *cp
46047 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
46048 parser->omp_declare_simd->tokens.safe_push (cp);
46050 if (first_p)
46052 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
46053 cp_parser_pragma (parser, context, NULL);
46054 switch (context)
46056 case pragma_external:
46057 cp_parser_declaration (parser, NULL_TREE);
46058 break;
46059 case pragma_member:
46060 cp_parser_member_declaration (parser);
46061 break;
46062 case pragma_objc_icode:
46063 cp_parser_block_declaration (parser, /*statement_p=*/false);
46064 break;
46065 default:
46066 cp_parser_declaration_statement (parser);
46067 break;
46069 if (parser->omp_declare_simd
46070 && !parser->omp_declare_simd->error_seen
46071 && !parser->omp_declare_simd->fndecl_seen)
46072 error_at (pragma_tok->location,
46073 "%<#pragma omp declare %s%> not immediately followed by "
46074 "function declaration or definition",
46075 parser->omp_declare_simd->variant_p ? "variant" : "simd");
46076 data.tokens.release ();
46077 parser->omp_declare_simd = NULL;
46081 static const char *const omp_construct_selectors[] = {
46082 "simd", "target", "teams", "parallel", "for", NULL };
46083 static const char *const omp_device_selectors[] = {
46084 "kind", "isa", "arch", NULL };
46085 static const char *const omp_implementation_selectors[] = {
46086 "vendor", "extension", "atomic_default_mem_order", "unified_address",
46087 "unified_shared_memory", "dynamic_allocators", "reverse_offload", NULL };
46088 static const char *const omp_user_selectors[] = {
46089 "condition", NULL };
46091 /* OpenMP 5.0:
46093 trait-selector:
46094 trait-selector-name[([trait-score:]trait-property[,trait-property[,...]])]
46096 trait-score:
46097 score(score-expression) */
46099 static tree
46100 cp_parser_omp_context_selector (cp_parser *parser, tree set, bool has_parms_p)
46102 tree ret = NULL_TREE;
46105 tree selector;
46106 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
46107 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46108 selector = cp_lexer_peek_token (parser->lexer)->u.value;
46109 else
46111 cp_parser_error (parser, "expected trait selector name");
46112 return error_mark_node;
46115 tree properties = NULL_TREE;
46116 const char *const *selectors = NULL;
46117 bool allow_score = true;
46118 bool allow_user = false;
46119 int property_limit = 0;
46120 enum { CTX_PROPERTY_NONE, CTX_PROPERTY_USER, CTX_PROPERTY_NAME_LIST,
46121 CTX_PROPERTY_ID, CTX_PROPERTY_EXPR,
46122 CTX_PROPERTY_SIMD } property_kind = CTX_PROPERTY_NONE;
46123 switch (IDENTIFIER_POINTER (set)[0])
46125 case 'c': /* construct */
46126 selectors = omp_construct_selectors;
46127 allow_score = false;
46128 property_limit = 1;
46129 property_kind = CTX_PROPERTY_SIMD;
46130 break;
46131 case 'd': /* device */
46132 selectors = omp_device_selectors;
46133 allow_score = false;
46134 allow_user = true;
46135 property_limit = 3;
46136 property_kind = CTX_PROPERTY_NAME_LIST;
46137 break;
46138 case 'i': /* implementation */
46139 selectors = omp_implementation_selectors;
46140 allow_user = true;
46141 property_limit = 3;
46142 property_kind = CTX_PROPERTY_NAME_LIST;
46143 break;
46144 case 'u': /* user */
46145 selectors = omp_user_selectors;
46146 property_limit = 1;
46147 property_kind = CTX_PROPERTY_EXPR;
46148 break;
46149 default:
46150 gcc_unreachable ();
46152 for (int i = 0; ; i++)
46154 if (selectors[i] == NULL)
46156 if (allow_user)
46158 property_kind = CTX_PROPERTY_USER;
46159 break;
46161 else
46163 error ("selector %qs not allowed for context selector "
46164 "set %qs", IDENTIFIER_POINTER (selector),
46165 IDENTIFIER_POINTER (set));
46166 cp_lexer_consume_token (parser->lexer);
46167 return error_mark_node;
46170 if (i == property_limit)
46171 property_kind = CTX_PROPERTY_NONE;
46172 if (strcmp (selectors[i], IDENTIFIER_POINTER (selector)) == 0)
46173 break;
46175 if (property_kind == CTX_PROPERTY_NAME_LIST
46176 && IDENTIFIER_POINTER (set)[0] == 'i'
46177 && strcmp (IDENTIFIER_POINTER (selector),
46178 "atomic_default_mem_order") == 0)
46179 property_kind = CTX_PROPERTY_ID;
46181 cp_lexer_consume_token (parser->lexer);
46183 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
46185 if (property_kind == CTX_PROPERTY_NONE)
46187 error ("selector %qs does not accept any properties",
46188 IDENTIFIER_POINTER (selector));
46189 return error_mark_node;
46192 matching_parens parens;
46193 parens.consume_open (parser);
46195 cp_token *token = cp_lexer_peek_token (parser->lexer);
46196 if (allow_score
46197 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
46198 && strcmp (IDENTIFIER_POINTER (token->u.value), "score") == 0
46199 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
46201 cp_lexer_save_tokens (parser->lexer);
46202 cp_lexer_consume_token (parser->lexer);
46203 cp_lexer_consume_token (parser->lexer);
46204 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
46205 true)
46206 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
46208 cp_lexer_rollback_tokens (parser->lexer);
46209 cp_lexer_consume_token (parser->lexer);
46211 matching_parens parens2;
46212 parens2.require_open (parser);
46213 tree score = cp_parser_constant_expression (parser);
46214 if (!parens2.require_close (parser))
46215 cp_parser_skip_to_closing_parenthesis (parser, true,
46216 false, true);
46217 cp_parser_require (parser, CPP_COLON, RT_COLON);
46218 if (score != error_mark_node)
46220 score = fold_non_dependent_expr (score);
46221 if (value_dependent_expression_p (score))
46222 properties = tree_cons (get_identifier (" score"),
46223 score, properties);
46224 else if (!INTEGRAL_TYPE_P (TREE_TYPE (score))
46225 || TREE_CODE (score) != INTEGER_CST)
46226 error_at (token->location, "score argument must be "
46227 "constant integer expression");
46228 else if (tree_int_cst_sgn (score) < 0)
46229 error_at (token->location, "score argument must be "
46230 "non-negative");
46231 else
46232 properties = tree_cons (get_identifier (" score"),
46233 score, properties);
46236 else
46237 cp_lexer_rollback_tokens (parser->lexer);
46239 token = cp_lexer_peek_token (parser->lexer);
46242 switch (property_kind)
46244 tree t;
46245 case CTX_PROPERTY_USER:
46248 t = cp_parser_constant_expression (parser);
46249 if (t != error_mark_node)
46251 t = fold_non_dependent_expr (t);
46252 if (TREE_CODE (t) == STRING_CST)
46253 properties = tree_cons (NULL_TREE, t, properties);
46254 else if (!value_dependent_expression_p (t)
46255 && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
46256 || !tree_fits_shwi_p (t)))
46257 error_at (token->location, "property must be "
46258 "constant integer expression or string "
46259 "literal");
46260 else
46261 properties = tree_cons (NULL_TREE, t, properties);
46263 else
46264 return error_mark_node;
46266 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
46267 cp_lexer_consume_token (parser->lexer);
46268 else
46269 break;
46271 while (1);
46272 break;
46273 case CTX_PROPERTY_ID:
46274 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
46275 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46277 tree prop = cp_lexer_peek_token (parser->lexer)->u.value;
46278 cp_lexer_consume_token (parser->lexer);
46279 properties = tree_cons (prop, NULL_TREE, properties);
46281 else
46283 cp_parser_error (parser, "expected identifier");
46284 return error_mark_node;
46286 break;
46287 case CTX_PROPERTY_NAME_LIST:
46290 tree prop = NULL_TREE, value = NULL_TREE;
46291 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
46292 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46294 prop = cp_lexer_peek_token (parser->lexer)->u.value;
46295 cp_lexer_consume_token (parser->lexer);
46297 else if (cp_lexer_next_token_is (parser->lexer, CPP_STRING))
46298 value = cp_parser_string_literal (parser, false, false);
46299 else
46301 cp_parser_error (parser, "expected identifier or "
46302 "string literal");
46303 return error_mark_node;
46306 properties = tree_cons (prop, value, properties);
46308 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
46309 cp_lexer_consume_token (parser->lexer);
46310 else
46311 break;
46313 while (1);
46314 break;
46315 case CTX_PROPERTY_EXPR:
46316 t = cp_parser_constant_expression (parser);
46317 if (t != error_mark_node)
46319 t = fold_non_dependent_expr (t);
46320 if (!value_dependent_expression_p (t)
46321 && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
46322 || !tree_fits_shwi_p (t)))
46323 error_at (token->location, "property must be "
46324 "constant integer expression");
46325 else
46326 properties = tree_cons (NULL_TREE, t, properties);
46328 else
46329 return error_mark_node;
46330 break;
46331 case CTX_PROPERTY_SIMD:
46332 if (!has_parms_p)
46334 error_at (token->location, "properties for %<simd%> "
46335 "selector may not be specified in "
46336 "%<metadirective%>");
46337 return error_mark_node;
46339 properties
46340 = cp_parser_omp_all_clauses (parser,
46341 OMP_DECLARE_SIMD_CLAUSE_MASK,
46342 "simd", NULL, true, 2);
46343 break;
46344 default:
46345 gcc_unreachable ();
46348 if (!parens.require_close (parser))
46349 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
46351 properties = nreverse (properties);
46353 else if (property_kind == CTX_PROPERTY_NAME_LIST
46354 || property_kind == CTX_PROPERTY_ID
46355 || property_kind == CTX_PROPERTY_EXPR)
46357 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
46358 return error_mark_node;
46361 ret = tree_cons (selector, properties, ret);
46363 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
46364 cp_lexer_consume_token (parser->lexer);
46365 else
46366 break;
46368 while (1);
46370 return nreverse (ret);
46373 /* OpenMP 5.0:
46375 trait-set-selector[,trait-set-selector[,...]]
46377 trait-set-selector:
46378 trait-set-selector-name = { trait-selector[, trait-selector[, ...]] }
46380 trait-set-selector-name:
46381 constructor
46382 device
46383 implementation
46384 user */
46386 static tree
46387 cp_parser_omp_context_selector_specification (cp_parser *parser,
46388 bool has_parms_p)
46390 tree ret = NULL_TREE;
46393 const char *setp = "";
46394 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46395 setp
46396 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
46397 switch (setp[0])
46399 case 'c':
46400 if (strcmp (setp, "construct") == 0)
46401 setp = NULL;
46402 break;
46403 case 'd':
46404 if (strcmp (setp, "device") == 0)
46405 setp = NULL;
46406 break;
46407 case 'i':
46408 if (strcmp (setp, "implementation") == 0)
46409 setp = NULL;
46410 break;
46411 case 'u':
46412 if (strcmp (setp, "user") == 0)
46413 setp = NULL;
46414 break;
46415 default:
46416 break;
46418 if (setp)
46420 cp_parser_error (parser, "expected %<construct%>, %<device%>, "
46421 "%<implementation%> or %<user%>");
46422 return error_mark_node;
46425 tree set = cp_lexer_peek_token (parser->lexer)->u.value;
46426 cp_lexer_consume_token (parser->lexer);
46428 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
46429 return error_mark_node;
46431 matching_braces braces;
46432 if (!braces.require_open (parser))
46433 return error_mark_node;
46435 tree selectors
46436 = cp_parser_omp_context_selector (parser, set, has_parms_p);
46437 if (selectors == error_mark_node)
46439 cp_parser_skip_to_closing_brace (parser);
46440 ret = error_mark_node;
46442 else if (ret != error_mark_node)
46443 ret = tree_cons (set, selectors, ret);
46445 braces.require_close (parser);
46447 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
46448 cp_lexer_consume_token (parser->lexer);
46449 else
46450 break;
46452 while (1);
46454 if (ret == error_mark_node)
46455 return ret;
46456 return nreverse (ret);
46459 /* Assumption clauses:
46460 OpenMP 5.1
46461 absent (directive-name-list)
46462 contains (directive-name-list)
46463 holds (expression)
46464 no_openmp
46465 no_openmp_routines
46466 no_parallelism */
46468 static void
46469 cp_parser_omp_assumption_clauses (cp_parser *parser, cp_token *pragma_tok,
46470 bool is_assume)
46472 bool no_openmp = false;
46473 bool no_openmp_routines = false;
46474 bool no_parallelism = false;
46475 bitmap_head absent_head, contains_head;
46477 bitmap_obstack_initialize (NULL);
46478 bitmap_initialize (&absent_head, &bitmap_default_obstack);
46479 bitmap_initialize (&contains_head, &bitmap_default_obstack);
46481 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
46482 error_at (cp_lexer_peek_token (parser->lexer)->location,
46483 "expected at least one assumption clause");
46485 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
46487 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
46488 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
46489 cp_lexer_consume_token (parser->lexer);
46491 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46492 break;
46494 const char *p
46495 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
46496 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
46498 if (!strcmp (p, "no_openmp"))
46500 cp_lexer_consume_token (parser->lexer);
46501 if (no_openmp)
46502 error_at (cloc, "too many %qs clauses", "no_openmp");
46503 no_openmp = true;
46505 else if (!strcmp (p, "no_openmp_routines"))
46507 cp_lexer_consume_token (parser->lexer);
46508 if (no_openmp_routines)
46509 error_at (cloc, "too many %qs clauses", "no_openmp_routines");
46510 no_openmp_routines = true;
46512 else if (!strcmp (p, "no_parallelism"))
46514 cp_lexer_consume_token (parser->lexer);
46515 if (no_parallelism)
46516 error_at (cloc, "too many %qs clauses", "no_parallelism");
46517 no_parallelism = true;
46519 else if (!strcmp (p, "holds"))
46521 cp_lexer_consume_token (parser->lexer);
46522 matching_parens parens;
46523 if (parens.require_open (parser))
46525 location_t eloc = cp_lexer_peek_token (parser->lexer)->location;
46526 tree t = cp_parser_assignment_expression (parser);
46527 if (!type_dependent_expression_p (t))
46528 t = contextual_conv_bool (t, tf_warning_or_error);
46529 if (is_assume && !error_operand_p (t))
46530 finish_expr_stmt (build_assume_call (eloc, t));
46531 if (!parens.require_close (parser))
46532 cp_parser_skip_to_closing_parenthesis (parser,
46533 /*recovering=*/true,
46534 /*or_comma=*/false,
46535 /*consume_paren=*/true);
46538 else if (!strcmp (p, "absent") || !strcmp (p, "contains"))
46540 cp_lexer_consume_token (parser->lexer);
46541 matching_parens parens;
46542 if (parens.require_open (parser))
46546 const char *directive[3] = {};
46547 int i;
46548 location_t dloc
46549 = cp_lexer_peek_token (parser->lexer)->location;
46550 for (i = 0; i < 3; i++)
46552 tree id;
46553 if (cp_lexer_nth_token_is (parser->lexer, i + 1, CPP_NAME))
46554 id = cp_lexer_peek_nth_token (parser->lexer,
46555 i + 1)->u.value;
46556 else if (cp_lexer_nth_token_is (parser->lexer, i + 1,
46557 CPP_KEYWORD))
46559 enum rid rid
46560 = cp_lexer_peek_nth_token (parser->lexer,
46561 i + 1)->keyword;
46562 id = ridpointers[rid];
46564 else
46565 break;
46566 directive[i] = IDENTIFIER_POINTER (id);
46568 if (i == 0)
46569 error_at (dloc, "expected directive name");
46570 else
46572 const struct c_omp_directive *dir
46573 = c_omp_categorize_directive (directive[0],
46574 directive[1],
46575 directive[2]);
46576 if (dir == NULL
46577 || dir->kind == C_OMP_DIR_DECLARATIVE
46578 || dir->kind == C_OMP_DIR_INFORMATIONAL
46579 || dir->id == PRAGMA_OMP_END
46580 || (!dir->second && directive[1])
46581 || (!dir->third && directive[2]))
46582 error_at (dloc, "unknown OpenMP directive name in "
46583 "%qs clause argument", p);
46584 else
46586 int id = dir - c_omp_directives;
46587 if (bitmap_bit_p (p[0] == 'a' ? &contains_head
46588 : &absent_head, id))
46589 error_at (dloc, "%<%s%s%s%s%s%> directive "
46590 "mentioned in both %<absent%> and "
46591 "%<contains%> clauses",
46592 directive[0],
46593 directive[1] ? " " : "",
46594 directive[1] ? directive[1] : "",
46595 directive[2] ? " " : "",
46596 directive[2] ? directive[2] : "");
46597 else if (!bitmap_set_bit (p[0] == 'a'
46598 ? &absent_head
46599 : &contains_head, id))
46600 error_at (dloc, "%<%s%s%s%s%s%> directive "
46601 "mentioned multiple times in %qs "
46602 "clauses",
46603 directive[0],
46604 directive[1] ? " " : "",
46605 directive[1] ? directive[1] : "",
46606 directive[2] ? " " : "",
46607 directive[2] ? directive[2] : "", p);
46609 for (; i; --i)
46610 cp_lexer_consume_token (parser->lexer);
46612 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
46613 cp_lexer_consume_token (parser->lexer);
46614 else
46615 break;
46617 while (1);
46618 if (!parens.require_close (parser))
46619 cp_parser_skip_to_closing_parenthesis (parser,
46620 /*recovering=*/true,
46621 /*or_comma=*/false,
46622 /*consume_paren=*/true);
46625 else if (startswith (p, "ext_"))
46627 warning_at (cloc, 0, "unknown assumption clause %qs", p);
46628 cp_lexer_consume_token (parser->lexer);
46629 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
46630 for (size_t n = cp_parser_skip_balanced_tokens (parser, 1) - 1;
46631 n; --n)
46632 cp_lexer_consume_token (parser->lexer);
46634 else
46636 cp_lexer_consume_token (parser->lexer);
46637 error_at (cloc, "expected assumption clause");
46638 break;
46641 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46644 /* OpenMP 5.1
46645 # pragma omp assume clauses[optseq] new-line */
46647 static void
46648 cp_parser_omp_assume (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
46650 cp_parser_omp_assumption_clauses (parser, pragma_tok, true);
46651 add_stmt (cp_parser_omp_structured_block (parser, if_p));
46654 /* OpenMP 5.1
46655 # pragma omp assumes clauses[optseq] new-line */
46657 static bool
46658 cp_parser_omp_assumes (cp_parser *parser, cp_token *pragma_tok)
46660 cp_parser_omp_assumption_clauses (parser, pragma_tok, false);
46661 return false;
46664 /* Finalize #pragma omp declare variant after a fndecl has been parsed, and put
46665 that into "omp declare variant base" attribute. */
46667 static tree
46668 cp_finish_omp_declare_variant (cp_parser *parser, cp_token *pragma_tok,
46669 tree attrs)
46671 matching_parens parens;
46672 if (!parens.require_open (parser))
46674 fail:
46675 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46676 return attrs;
46679 bool template_p;
46680 cp_id_kind idk = CP_ID_KIND_NONE;
46681 cp_token *varid_token = cp_lexer_peek_token (parser->lexer);
46682 cp_expr varid
46683 = cp_parser_id_expression (parser, /*template_keyword_p=*/false,
46684 /*check_dependency_p=*/true,
46685 /*template_p=*/&template_p,
46686 /*declarator_p=*/false,
46687 /*optional_p=*/false);
46688 parens.require_close (parser);
46690 tree variant;
46691 if (TREE_CODE (varid) == TEMPLATE_ID_EXPR
46692 || TREE_CODE (varid) == TYPE_DECL
46693 || varid == error_mark_node)
46694 variant = varid;
46695 else if (varid_token->type == CPP_NAME && varid_token->error_reported)
46696 variant = NULL_TREE;
46697 else
46699 tree ambiguous_decls;
46700 variant = cp_parser_lookup_name (parser, varid, none_type,
46701 template_p, /*is_namespace=*/false,
46702 /*check_dependency=*/true,
46703 &ambiguous_decls,
46704 varid.get_location ());
46705 if (ambiguous_decls)
46706 variant = NULL_TREE;
46708 if (variant == NULL_TREE)
46709 variant = error_mark_node;
46710 else if (TREE_CODE (variant) != SCOPE_REF)
46712 const char *error_msg;
46713 variant
46714 = finish_id_expression (varid, variant, parser->scope,
46715 &idk, false, true,
46716 &parser->non_integral_constant_expression_p,
46717 template_p, true, false, false, &error_msg,
46718 varid.get_location ());
46719 if (error_msg)
46720 cp_parser_error (parser, error_msg);
46722 location_t caret_loc = get_pure_location (varid.get_location ());
46723 location_t start_loc = get_start (varid_token->location);
46724 location_t finish_loc = get_finish (varid.get_location ());
46725 location_t varid_loc = make_location (caret_loc, start_loc, finish_loc);
46727 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
46728 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
46729 cp_lexer_consume_token (parser->lexer);
46731 const char *clause = "";
46732 location_t match_loc = cp_lexer_peek_token (parser->lexer)->location;
46733 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46734 clause = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
46735 if (strcmp (clause, "match"))
46737 cp_parser_error (parser, "expected %<match%>");
46738 goto fail;
46741 cp_lexer_consume_token (parser->lexer);
46743 if (!parens.require_open (parser))
46744 goto fail;
46746 tree ctx = cp_parser_omp_context_selector_specification (parser, true);
46747 if (ctx == error_mark_node)
46748 goto fail;
46749 ctx = omp_check_context_selector (match_loc, ctx);
46750 if (ctx != error_mark_node && variant != error_mark_node)
46752 tree match_loc_node = maybe_wrap_with_location (integer_zero_node,
46753 match_loc);
46754 tree loc_node = maybe_wrap_with_location (integer_zero_node, varid_loc);
46755 loc_node = tree_cons (match_loc_node,
46756 build_int_cst (integer_type_node, idk),
46757 build_tree_list (loc_node, integer_zero_node));
46758 attrs = tree_cons (get_identifier ("omp declare variant base"),
46759 tree_cons (variant, ctx, loc_node), attrs);
46760 if (processing_template_decl)
46761 ATTR_IS_DEPENDENT (attrs) = 1;
46764 parens.require_close (parser);
46765 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46766 return attrs;
46770 /* Finalize #pragma omp declare simd clauses after direct declarator has
46771 been parsed, and put that into "omp declare simd" attribute. */
46773 static tree
46774 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
46776 struct cp_token_cache *ce;
46777 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
46778 int i;
46780 if (!data->error_seen && data->fndecl_seen)
46782 error ("%<#pragma omp declare %s%> not immediately followed by "
46783 "a single function declaration or definition",
46784 data->variant_p ? "variant" : "simd");
46785 data->error_seen = true;
46787 if (data->error_seen)
46788 return attrs;
46790 FOR_EACH_VEC_ELT (data->tokens, i, ce)
46792 tree c, cl;
46794 cp_parser_push_lexer_for_tokens (parser, ce);
46795 parser->lexer->in_pragma = true;
46796 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
46797 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
46798 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46799 const char *kind = IDENTIFIER_POINTER (id);
46800 cp_lexer_consume_token (parser->lexer);
46801 if (strcmp (kind, "simd") == 0)
46803 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
46804 "#pragma omp declare simd",
46805 pragma_tok);
46806 if (cl)
46807 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
46808 c = build_tree_list (get_identifier ("omp declare simd"), cl);
46809 TREE_CHAIN (c) = attrs;
46810 if (processing_template_decl)
46811 ATTR_IS_DEPENDENT (c) = 1;
46812 attrs = c;
46814 else
46816 gcc_assert (strcmp (kind, "variant") == 0);
46817 attrs
46818 = cp_finish_omp_declare_variant (parser, pragma_tok, attrs);
46820 cp_parser_pop_lexer (parser);
46823 cp_lexer *lexer = NULL;
46824 for (int i = 0; i < 2; i++)
46826 if (data->attribs[i] == NULL)
46827 continue;
46828 for (tree *pa = data->attribs[i]; *pa; )
46829 if (get_attribute_namespace (*pa) == omp_identifier
46830 && is_attribute_p ("directive", get_attribute_name (*pa)))
46832 for (tree a = TREE_VALUE (*pa); a; a = TREE_CHAIN (a))
46834 tree d = TREE_VALUE (a);
46835 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
46836 cp_token *first = DEFPARSE_TOKENS (d)->first;
46837 cp_token *last = DEFPARSE_TOKENS (d)->last;
46838 const char *directive[3] = {};
46839 for (int j = 0; j < 3; j++)
46841 tree id = NULL_TREE;
46842 if (first + j == last)
46843 break;
46844 if (first[j].type == CPP_NAME)
46845 id = first[j].u.value;
46846 else if (first[j].type == CPP_KEYWORD)
46847 id = ridpointers[(int) first[j].keyword];
46848 else
46849 break;
46850 directive[j] = IDENTIFIER_POINTER (id);
46852 const c_omp_directive *dir = NULL;
46853 if (directive[0])
46854 dir = c_omp_categorize_directive (directive[0], directive[1],
46855 directive[2]);
46856 if (dir == NULL)
46858 error_at (first->location,
46859 "unknown OpenMP directive name in "
46860 "%<omp::directive%> attribute argument");
46861 continue;
46863 if (dir->id != PRAGMA_OMP_DECLARE
46864 || (strcmp (directive[1], "simd") != 0
46865 && strcmp (directive[1], "variant") != 0))
46867 error_at (first->location,
46868 "OpenMP directive other than %<declare simd%> "
46869 "or %<declare variant%> appertains to a "
46870 "declaration");
46871 continue;
46874 if (parser->omp_attrs_forbidden_p)
46876 error_at (first->location,
46877 "mixing OpenMP directives with attribute and "
46878 "pragma syntax on the same statement");
46879 parser->omp_attrs_forbidden_p = false;
46882 if (!flag_openmp && strcmp (directive[1], "simd") != 0)
46883 continue;
46884 if (lexer == NULL)
46886 lexer = cp_lexer_alloc ();
46887 lexer->debugging_p = parser->lexer->debugging_p;
46889 vec_safe_reserve (lexer->buffer, (last - first) + 2);
46890 cp_token tok = {};
46891 tok.type = CPP_PRAGMA;
46892 tok.keyword = RID_MAX;
46893 tok.u.value = build_int_cst (NULL, PRAGMA_OMP_DECLARE);
46894 tok.location = first->location;
46895 lexer->buffer->quick_push (tok);
46896 while (++first < last)
46897 lexer->buffer->quick_push (*first);
46898 tok = {};
46899 tok.type = CPP_PRAGMA_EOL;
46900 tok.keyword = RID_MAX;
46901 tok.location = last->location;
46902 lexer->buffer->quick_push (tok);
46903 tok = {};
46904 tok.type = CPP_EOF;
46905 tok.keyword = RID_MAX;
46906 tok.location = last->location;
46907 lexer->buffer->quick_push (tok);
46908 lexer->next = parser->lexer;
46909 lexer->next_token = lexer->buffer->address ();
46910 lexer->last_token = lexer->next_token
46911 + lexer->buffer->length ()
46912 - 1;
46913 lexer->in_omp_attribute_pragma = true;
46914 parser->lexer = lexer;
46915 /* Move the current source position to that of the first token
46916 in the new lexer. */
46917 cp_lexer_set_source_position_from_token (lexer->next_token);
46919 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
46920 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46921 const char *kind = IDENTIFIER_POINTER (id);
46922 cp_lexer_consume_token (parser->lexer);
46924 tree c, cl;
46925 if (strcmp (kind, "simd") == 0)
46927 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
46928 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
46929 cp_lexer_consume_token (parser->lexer);
46931 omp_clause_mask mask = OMP_DECLARE_SIMD_CLAUSE_MASK;
46932 cl = cp_parser_omp_all_clauses (parser, mask,
46933 "#pragma omp declare simd",
46934 pragma_tok);
46935 if (cl)
46936 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
46937 c = build_tree_list (get_identifier ("omp declare simd"),
46938 cl);
46939 TREE_CHAIN (c) = attrs;
46940 if (processing_template_decl)
46941 ATTR_IS_DEPENDENT (c) = 1;
46942 attrs = c;
46944 else
46946 gcc_assert (strcmp (kind, "variant") == 0);
46947 attrs
46948 = cp_finish_omp_declare_variant (parser, pragma_tok,
46949 attrs);
46951 gcc_assert (parser->lexer != lexer);
46952 vec_safe_truncate (lexer->buffer, 0);
46954 *pa = TREE_CHAIN (*pa);
46956 else
46957 pa = &TREE_CHAIN (*pa);
46959 if (lexer)
46960 cp_lexer_destroy (lexer);
46962 data->fndecl_seen = true;
46963 return attrs;
46966 /* Helper for cp_parser_omp_declare_target, handle one to or link clause
46967 on #pragma omp declare target. Return false if errors were reported. */
46969 static bool
46970 handle_omp_declare_target_clause (tree c, tree t, int device_type)
46972 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
46973 tree at2 = lookup_attribute ("omp declare target link", DECL_ATTRIBUTES (t));
46974 tree id;
46975 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
46977 id = get_identifier ("omp declare target link");
46978 std::swap (at1, at2);
46980 else
46981 id = get_identifier ("omp declare target");
46982 if (at2)
46984 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
46985 error_at (OMP_CLAUSE_LOCATION (c),
46986 "%qD specified both in declare target %<link%> and %qs"
46987 " clauses", t, OMP_CLAUSE_ENTER_TO (c) ? "to" : "enter");
46988 else
46989 error_at (OMP_CLAUSE_LOCATION (c),
46990 "%qD specified both in declare target %<link%> and "
46991 "%<to%> or %<enter%> clauses", t);
46992 return false;
46994 if (!at1)
46996 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
46997 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
46998 return true;
47000 symtab_node *node = symtab_node::get (t);
47001 if (node != NULL)
47003 node->offloadable = 1;
47004 if (ENABLE_OFFLOADING)
47006 g->have_offload = true;
47007 if (is_a <varpool_node *> (node))
47008 vec_safe_push (offload_vars, t);
47012 if (TREE_CODE (t) != FUNCTION_DECL)
47013 return true;
47014 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_HOST) != 0)
47016 tree at3 = lookup_attribute ("omp declare target host",
47017 DECL_ATTRIBUTES (t));
47018 if (at3 == NULL_TREE)
47020 id = get_identifier ("omp declare target host");
47021 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
47024 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_NOHOST) != 0)
47026 tree at3 = lookup_attribute ("omp declare target nohost",
47027 DECL_ATTRIBUTES (t));
47028 if (at3 == NULL_TREE)
47030 id = get_identifier ("omp declare target nohost");
47031 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
47034 return true;
47037 /* OpenMP 4.0:
47038 # pragma omp declare target new-line
47039 declarations and definitions
47040 # pragma omp end declare target new-line
47042 OpenMP 4.5:
47043 # pragma omp declare target ( extended-list ) new-line
47045 # pragma omp declare target declare-target-clauses[seq] new-line */
47047 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
47048 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
47049 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ENTER) \
47050 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK) \
47051 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE_TYPE))
47053 static void
47054 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
47056 tree clauses = NULL_TREE;
47057 int device_type = 0;
47058 bool only_device_type = true;
47059 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
47060 || (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
47061 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME)))
47062 clauses
47063 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
47064 "#pragma omp declare target", pragma_tok);
47065 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
47067 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_ENTER,
47068 clauses);
47069 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
47070 cp_parser_require_pragma_eol (parser, pragma_tok);
47072 else
47074 cp_omp_declare_target_attr a
47075 = { parser->lexer->in_omp_attribute_pragma, -1 };
47076 vec_safe_push (scope_chain->omp_declare_target_attribute, a);
47077 cp_parser_require_pragma_eol (parser, pragma_tok);
47078 return;
47080 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
47081 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE)
47082 device_type |= OMP_CLAUSE_DEVICE_TYPE_KIND (c);
47083 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
47085 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE)
47086 continue;
47087 tree t = OMP_CLAUSE_DECL (c);
47088 only_device_type = false;
47089 if (!handle_omp_declare_target_clause (c, t, device_type))
47090 continue;
47091 if (VAR_OR_FUNCTION_DECL_P (t)
47092 && DECL_LOCAL_DECL_P (t)
47093 && DECL_LANG_SPECIFIC (t)
47094 && DECL_LOCAL_DECL_ALIAS (t)
47095 && DECL_LOCAL_DECL_ALIAS (t) != error_mark_node)
47096 handle_omp_declare_target_clause (c, DECL_LOCAL_DECL_ALIAS (t),
47097 device_type);
47099 if (device_type && only_device_type)
47100 error_at (OMP_CLAUSE_LOCATION (clauses),
47101 "directive with only %<device_type%> clause");
47104 /* OpenMP 5.1
47105 # pragma omp begin assumes clauses[optseq] new-line
47107 # pragma omp begin declare target clauses[optseq] new-line */
47109 #define OMP_BEGIN_DECLARE_TARGET_CLAUSE_MASK \
47110 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE_TYPE)
47112 static void
47113 cp_parser_omp_begin (cp_parser *parser, cp_token *pragma_tok)
47115 const char *p = "";
47116 bool in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
47117 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47119 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
47120 p = IDENTIFIER_POINTER (id);
47122 if (strcmp (p, "declare") == 0)
47124 cp_lexer_consume_token (parser->lexer);
47125 p = "";
47126 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47128 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
47129 p = IDENTIFIER_POINTER (id);
47131 if (strcmp (p, "target") == 0)
47133 cp_lexer_consume_token (parser->lexer);
47134 tree clauses
47135 = cp_parser_omp_all_clauses (parser,
47136 OMP_BEGIN_DECLARE_TARGET_CLAUSE_MASK,
47137 "#pragma omp begin declare target",
47138 pragma_tok);
47139 int device_type = 0;
47140 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
47141 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE)
47142 device_type |= OMP_CLAUSE_DEVICE_TYPE_KIND (c);
47143 cp_omp_declare_target_attr a
47144 = { in_omp_attribute_pragma, device_type };
47145 vec_safe_push (scope_chain->omp_declare_target_attribute, a);
47147 else
47149 cp_parser_error (parser, "expected %<target%>");
47150 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47153 else if (strcmp (p, "assumes") == 0)
47155 cp_lexer_consume_token (parser->lexer);
47156 cp_parser_omp_assumption_clauses (parser, pragma_tok, false);
47157 cp_omp_begin_assumes_data a = { in_omp_attribute_pragma };
47158 vec_safe_push (scope_chain->omp_begin_assumes, a);
47160 else
47162 cp_parser_error (parser, "expected %<declare target%> or %<assumes%>");
47163 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47167 /* OpenMP 4.0:
47168 # pragma omp end declare target new-line
47170 OpenMP 5.1:
47171 # pragma omp end assumes new-line */
47173 static void
47174 cp_parser_omp_end (cp_parser *parser, cp_token *pragma_tok)
47176 const char *p = "";
47177 bool in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
47178 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47180 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
47181 p = IDENTIFIER_POINTER (id);
47183 if (strcmp (p, "declare") == 0)
47185 cp_lexer_consume_token (parser->lexer);
47186 p = "";
47187 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47189 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
47190 p = IDENTIFIER_POINTER (id);
47192 if (strcmp (p, "target") == 0)
47193 cp_lexer_consume_token (parser->lexer);
47194 else
47196 cp_parser_error (parser, "expected %<target%>");
47197 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47198 return;
47200 cp_parser_require_pragma_eol (parser, pragma_tok);
47201 if (!vec_safe_length (scope_chain->omp_declare_target_attribute))
47202 error_at (pragma_tok->location,
47203 "%<#pragma omp end declare target%> without corresponding "
47204 "%<#pragma omp declare target%> or "
47205 "%<#pragma omp begin declare target%>");
47206 else
47208 cp_omp_declare_target_attr
47209 a = scope_chain->omp_declare_target_attribute->pop ();
47210 if (a.attr_syntax != in_omp_attribute_pragma)
47212 if (a.attr_syntax)
47213 error_at (pragma_tok->location,
47214 "%qs in attribute syntax terminated "
47215 "with %qs in pragma syntax",
47216 a.device_type >= 0 ? "begin declare target"
47217 : "declare target",
47218 "end declare target");
47219 else
47220 error_at (pragma_tok->location,
47221 "%qs in pragma syntax terminated "
47222 "with %qs in attribute syntax",
47223 a.device_type >= 0 ? "begin declare target"
47224 : "declare target",
47225 "end declare target");
47229 else if (strcmp (p, "assumes") == 0)
47231 cp_lexer_consume_token (parser->lexer);
47232 cp_parser_require_pragma_eol (parser, pragma_tok);
47233 if (!vec_safe_length (scope_chain->omp_begin_assumes))
47234 error_at (pragma_tok->location,
47235 "%qs without corresponding %qs",
47236 "#pragma omp end assumes", "#pragma omp begin assumes");
47237 else
47239 cp_omp_begin_assumes_data
47240 a = scope_chain->omp_begin_assumes->pop ();
47241 if (a.attr_syntax != in_omp_attribute_pragma)
47243 if (a.attr_syntax)
47244 error_at (pragma_tok->location,
47245 "%qs in attribute syntax terminated "
47246 "with %qs in pragma syntax",
47247 "begin assumes", "end assumes");
47248 else
47249 error_at (pragma_tok->location,
47250 "%qs in pragma syntax terminated "
47251 "with %qs in attribute syntax",
47252 "begin assumes", "end assumes");
47256 else
47258 cp_parser_error (parser, "expected %<declare%> or %<assumes%>");
47259 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47260 return;
47264 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
47265 expression and optional initializer clause of
47266 #pragma omp declare reduction. We store the expression(s) as
47267 either 3, 6 or 7 special statements inside of the artificial function's
47268 body. The first two statements are DECL_EXPRs for the artificial
47269 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
47270 expression that uses those variables.
47271 If there was any INITIALIZER clause, this is followed by further statements,
47272 the fourth and fifth statements are DECL_EXPRs for the artificial
47273 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
47274 constructor variant (first token after open paren is not omp_priv),
47275 then the sixth statement is a statement with the function call expression
47276 that uses the OMP_PRIV and optionally OMP_ORIG variable.
47277 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
47278 to initialize the OMP_PRIV artificial variable and there is seventh
47279 statement, a DECL_EXPR of the OMP_PRIV statement again. */
47281 static bool
47282 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
47284 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
47285 gcc_assert (TYPE_REF_P (type));
47286 type = TREE_TYPE (type);
47287 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
47288 DECL_ARTIFICIAL (omp_out) = 1;
47289 pushdecl (omp_out);
47290 add_decl_expr (omp_out);
47291 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
47292 DECL_ARTIFICIAL (omp_in) = 1;
47293 pushdecl (omp_in);
47294 add_decl_expr (omp_in);
47295 tree combiner;
47296 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
47298 keep_next_level (true);
47299 tree block = begin_omp_structured_block ();
47300 combiner = cp_parser_expression (parser);
47301 finish_expr_stmt (combiner);
47302 block = finish_omp_structured_block (block);
47303 if (processing_template_decl)
47304 block = build_stmt (input_location, EXPR_STMT, block);
47305 add_stmt (block);
47307 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
47308 return false;
47310 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
47311 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
47312 cp_lexer_consume_token (parser->lexer);
47314 const char *p = "";
47315 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47317 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
47318 p = IDENTIFIER_POINTER (id);
47321 if (strcmp (p, "initializer") == 0)
47323 cp_lexer_consume_token (parser->lexer);
47324 matching_parens parens;
47325 if (!parens.require_open (parser))
47326 return false;
47328 p = "";
47329 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47331 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
47332 p = IDENTIFIER_POINTER (id);
47335 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
47336 DECL_ARTIFICIAL (omp_priv) = 1;
47337 pushdecl (omp_priv);
47338 add_decl_expr (omp_priv);
47339 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
47340 DECL_ARTIFICIAL (omp_orig) = 1;
47341 pushdecl (omp_orig);
47342 add_decl_expr (omp_orig);
47344 keep_next_level (true);
47345 block = begin_omp_structured_block ();
47347 bool ctor = false;
47348 if (strcmp (p, "omp_priv") == 0)
47350 bool is_direct_init, is_non_constant_init;
47351 ctor = true;
47352 cp_lexer_consume_token (parser->lexer);
47353 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
47354 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
47355 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
47356 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
47357 == CPP_CLOSE_PAREN
47358 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
47359 == CPP_CLOSE_PAREN))
47361 finish_omp_structured_block (block);
47362 error ("invalid initializer clause");
47363 return false;
47365 initializer = cp_parser_initializer (parser, &is_direct_init,
47366 &is_non_constant_init);
47367 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
47368 NULL_TREE, LOOKUP_ONLYCONVERTING);
47370 else
47372 cp_parser_parse_tentatively (parser);
47373 /* Don't create location wrapper nodes here. */
47374 auto_suppress_location_wrappers sentinel;
47375 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
47376 /*check_dependency_p=*/true,
47377 /*template_p=*/NULL,
47378 /*declarator_p=*/false,
47379 /*optional_p=*/false);
47380 vec<tree, va_gc> *args;
47381 if (fn_name == error_mark_node
47382 || cp_parser_error_occurred (parser)
47383 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
47384 || ((args = cp_parser_parenthesized_expression_list
47385 (parser, non_attr, /*cast_p=*/false,
47386 /*allow_expansion_p=*/true,
47387 /*non_constant_p=*/NULL)),
47388 cp_parser_error_occurred (parser)))
47390 finish_omp_structured_block (block);
47391 cp_parser_abort_tentative_parse (parser);
47392 cp_parser_error (parser, "expected id-expression (arguments)");
47393 return false;
47395 unsigned int i;
47396 tree arg;
47397 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
47398 if (arg == omp_priv
47399 || (TREE_CODE (arg) == ADDR_EXPR
47400 && TREE_OPERAND (arg, 0) == omp_priv))
47401 break;
47402 cp_parser_abort_tentative_parse (parser);
47403 if (arg == NULL_TREE)
47404 error ("one of the initializer call arguments should be %<omp_priv%>"
47405 " or %<&omp_priv%>");
47406 initializer = cp_parser_postfix_expression (parser, false, false, false,
47407 false, NULL);
47408 finish_expr_stmt (initializer);
47411 block = finish_omp_structured_block (block);
47412 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
47413 if (processing_template_decl)
47414 block = build_stmt (input_location, EXPR_STMT, block);
47415 add_stmt (block);
47417 if (ctor)
47418 add_decl_expr (omp_orig);
47420 if (!parens.require_close (parser))
47421 return false;
47424 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
47425 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
47426 UNKNOWN_LOCATION);
47428 return true;
47431 /* OpenMP 4.0
47432 #pragma omp declare reduction (reduction-id : typename-list : expression) \
47433 initializer-clause[opt] new-line
47435 initializer-clause:
47436 initializer (omp_priv initializer)
47437 initializer (function-name (argument-list)) */
47439 static void
47440 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
47441 enum pragma_context)
47443 auto_vec<tree> types;
47444 enum tree_code reduc_code = ERROR_MARK;
47445 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
47446 unsigned int i;
47447 cp_token *first_token;
47448 cp_token_cache *cp;
47449 int errs;
47450 void *p;
47452 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
47453 p = obstack_alloc (&declarator_obstack, 0);
47455 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
47456 goto fail;
47458 switch (cp_lexer_peek_token (parser->lexer)->type)
47460 case CPP_PLUS:
47461 reduc_code = PLUS_EXPR;
47462 break;
47463 case CPP_MULT:
47464 reduc_code = MULT_EXPR;
47465 break;
47466 case CPP_MINUS:
47467 reduc_code = MINUS_EXPR;
47468 break;
47469 case CPP_AND:
47470 reduc_code = BIT_AND_EXPR;
47471 break;
47472 case CPP_XOR:
47473 reduc_code = BIT_XOR_EXPR;
47474 break;
47475 case CPP_OR:
47476 reduc_code = BIT_IOR_EXPR;
47477 break;
47478 case CPP_AND_AND:
47479 reduc_code = TRUTH_ANDIF_EXPR;
47480 break;
47481 case CPP_OR_OR:
47482 reduc_code = TRUTH_ORIF_EXPR;
47483 break;
47484 case CPP_NAME:
47485 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
47486 break;
47487 default:
47488 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
47489 "%<|%>, %<&&%>, %<||%> or identifier");
47490 goto fail;
47493 if (reduc_code != ERROR_MARK)
47494 cp_lexer_consume_token (parser->lexer);
47496 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
47497 if (reduc_id == error_mark_node)
47498 goto fail;
47500 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
47501 goto fail;
47503 /* Types may not be defined in declare reduction type list. */
47504 const char *saved_message;
47505 saved_message = parser->type_definition_forbidden_message;
47506 parser->type_definition_forbidden_message
47507 = G_("types may not be defined in declare reduction type list");
47508 bool saved_colon_corrects_to_scope_p;
47509 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
47510 parser->colon_corrects_to_scope_p = false;
47511 bool saved_colon_doesnt_start_class_def_p;
47512 saved_colon_doesnt_start_class_def_p
47513 = parser->colon_doesnt_start_class_def_p;
47514 parser->colon_doesnt_start_class_def_p = true;
47516 while (true)
47518 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
47519 type = cp_parser_type_id (parser);
47520 if (type == error_mark_node)
47522 else if (ARITHMETIC_TYPE_P (type)
47523 && (orig_reduc_id == NULL_TREE
47524 || (TREE_CODE (type) != COMPLEX_TYPE
47525 && (id_equal (orig_reduc_id, "min")
47526 || id_equal (orig_reduc_id, "max")))))
47527 error_at (loc, "predeclared arithmetic type %qT in "
47528 "%<#pragma omp declare reduction%>", type);
47529 else if (FUNC_OR_METHOD_TYPE_P (type)
47530 || TREE_CODE (type) == ARRAY_TYPE)
47531 error_at (loc, "function or array type %qT in "
47532 "%<#pragma omp declare reduction%>", type);
47533 else if (TYPE_REF_P (type))
47534 error_at (loc, "reference type %qT in "
47535 "%<#pragma omp declare reduction%>", type);
47536 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
47537 error_at (loc, "%<const%>, %<volatile%> or %<__restrict%>-qualified "
47538 "type %qT in %<#pragma omp declare reduction%>", type);
47539 else
47540 types.safe_push (type);
47542 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
47543 cp_lexer_consume_token (parser->lexer);
47544 else
47545 break;
47548 /* Restore the saved message. */
47549 parser->type_definition_forbidden_message = saved_message;
47550 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
47551 parser->colon_doesnt_start_class_def_p
47552 = saved_colon_doesnt_start_class_def_p;
47554 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
47555 || types.is_empty ())
47557 fail:
47558 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47559 goto done;
47562 first_token = cp_lexer_peek_token (parser->lexer);
47563 cp = NULL;
47564 errs = errorcount;
47565 FOR_EACH_VEC_ELT (types, i, type)
47567 tree fntype
47568 = build_function_type_list (void_type_node,
47569 cp_build_reference_type (type, false),
47570 NULL_TREE);
47571 tree this_reduc_id = reduc_id;
47572 if (!dependent_type_p (type))
47573 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
47574 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
47575 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
47576 DECL_ARTIFICIAL (fndecl) = 1;
47577 DECL_EXTERNAL (fndecl) = 1;
47578 DECL_DECLARED_INLINE_P (fndecl) = 1;
47579 DECL_IGNORED_P (fndecl) = 1;
47580 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
47581 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
47582 DECL_ATTRIBUTES (fndecl)
47583 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
47584 DECL_ATTRIBUTES (fndecl));
47585 bool block_scope = false;
47586 if (current_function_decl)
47588 block_scope = true;
47589 DECL_CONTEXT (fndecl) = current_function_decl;
47590 DECL_LOCAL_DECL_P (fndecl) = true;
47593 if (processing_template_decl)
47594 fndecl = push_template_decl (fndecl);
47596 if (block_scope)
47598 if (!processing_template_decl)
47599 pushdecl (fndecl);
47601 else if (current_class_type)
47603 if (cp == NULL)
47605 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
47606 cp_lexer_consume_token (parser->lexer);
47607 cp = cp_token_cache_new (first_token,
47608 cp_lexer_peek_nth_token (parser->lexer,
47609 2));
47611 DECL_STATIC_FUNCTION_P (fndecl) = 1;
47612 finish_member_declaration (fndecl);
47613 DECL_PENDING_INLINE_INFO (fndecl) = cp;
47614 DECL_PENDING_INLINE_P (fndecl) = 1;
47615 vec_safe_push (unparsed_funs_with_definitions, fndecl);
47616 continue;
47618 else
47620 DECL_CONTEXT (fndecl) = current_namespace;
47621 tree d = pushdecl (fndecl);
47622 /* We should never meet a matched duplicate decl. */
47623 gcc_checking_assert (d == error_mark_node || d == fndecl);
47626 tree block = NULL_TREE;
47627 if (!block_scope)
47628 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
47629 else
47630 block = begin_omp_structured_block ();
47631 if (cp)
47633 cp_parser_push_lexer_for_tokens (parser, cp);
47634 parser->lexer->in_pragma = true;
47637 bool ok = cp_parser_omp_declare_reduction_exprs (fndecl, parser);
47639 if (cp)
47640 cp_parser_pop_lexer (parser);
47641 if (!block_scope)
47642 finish_function (/*inline_p=*/false);
47643 else
47645 DECL_CONTEXT (fndecl) = current_function_decl;
47646 if (DECL_TEMPLATE_INFO (fndecl))
47647 DECL_CONTEXT (DECL_TI_TEMPLATE (fndecl)) = current_function_decl;
47649 if (!ok)
47650 goto fail;
47652 if (block_scope)
47654 block = finish_omp_structured_block (block);
47655 if (TREE_CODE (block) == BIND_EXPR)
47656 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
47657 else if (TREE_CODE (block) == STATEMENT_LIST)
47658 DECL_SAVED_TREE (fndecl) = block;
47659 if (processing_template_decl)
47660 add_decl_expr (fndecl);
47663 cp_check_omp_declare_reduction (fndecl);
47664 if (cp == NULL && types.length () > 1)
47665 cp = cp_token_cache_new (first_token,
47666 cp_lexer_peek_nth_token (parser->lexer, 2));
47667 if (errs != errorcount)
47668 break;
47671 cp_parser_require_pragma_eol (parser, pragma_tok);
47673 done:
47674 /* Free any declarators allocated. */
47675 obstack_free (&declarator_obstack, p);
47678 /* OpenMP 4.0
47679 #pragma omp declare simd declare-simd-clauses[optseq] new-line
47680 #pragma omp declare reduction (reduction-id : typename-list : expression) \
47681 initializer-clause[opt] new-line
47682 #pragma omp declare target new-line
47684 OpenMP 5.0
47685 #pragma omp declare variant (identifier) match (context-selector) */
47687 static bool
47688 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
47689 enum pragma_context context)
47691 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47693 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
47694 const char *p = IDENTIFIER_POINTER (id);
47696 if (strcmp (p, "simd") == 0)
47698 cp_lexer_consume_token (parser->lexer);
47699 cp_parser_omp_declare_simd (parser, pragma_tok,
47700 context, false);
47701 return true;
47703 if (flag_openmp && strcmp (p, "variant") == 0)
47705 cp_lexer_consume_token (parser->lexer);
47706 cp_parser_omp_declare_simd (parser, pragma_tok,
47707 context, true);
47708 return true;
47710 cp_ensure_no_omp_declare_simd (parser);
47711 if (strcmp (p, "reduction") == 0)
47713 cp_lexer_consume_token (parser->lexer);
47714 cp_parser_omp_declare_reduction (parser, pragma_tok,
47715 context);
47716 return false;
47718 if (!flag_openmp) /* flag_openmp_simd */
47720 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47721 return false;
47723 if (strcmp (p, "target") == 0)
47725 cp_lexer_consume_token (parser->lexer);
47726 cp_parser_omp_declare_target (parser, pragma_tok);
47727 return false;
47730 cp_parser_error (parser, "expected %<simd%>, %<reduction%>, "
47731 "%<target%> or %<variant%>");
47732 cp_parser_require_pragma_eol (parser, pragma_tok);
47733 return false;
47736 /* OpenMP 5.0
47737 #pragma omp requires clauses[optseq] new-line */
47739 static bool
47740 cp_parser_omp_requires (cp_parser *parser, cp_token *pragma_tok)
47742 enum omp_requires new_req = (enum omp_requires) 0;
47744 location_t loc = pragma_tok->location;
47745 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
47747 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
47748 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
47749 cp_lexer_consume_token (parser->lexer);
47751 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47753 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
47754 const char *p = IDENTIFIER_POINTER (id);
47755 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
47756 enum omp_requires this_req = (enum omp_requires) 0;
47758 if (!strcmp (p, "unified_address"))
47759 this_req = OMP_REQUIRES_UNIFIED_ADDRESS;
47760 else if (!strcmp (p, "unified_shared_memory"))
47761 this_req = OMP_REQUIRES_UNIFIED_SHARED_MEMORY;
47762 else if (!strcmp (p, "dynamic_allocators"))
47763 this_req = OMP_REQUIRES_DYNAMIC_ALLOCATORS;
47764 else if (!strcmp (p, "reverse_offload"))
47765 this_req = OMP_REQUIRES_REVERSE_OFFLOAD;
47766 else if (!strcmp (p, "atomic_default_mem_order"))
47768 cp_lexer_consume_token (parser->lexer);
47770 matching_parens parens;
47771 if (parens.require_open (parser))
47773 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47775 id = cp_lexer_peek_token (parser->lexer)->u.value;
47776 p = IDENTIFIER_POINTER (id);
47778 if (!strcmp (p, "seq_cst"))
47779 this_req
47780 = (enum omp_requires) OMP_MEMORY_ORDER_SEQ_CST;
47781 else if (!strcmp (p, "relaxed"))
47782 this_req
47783 = (enum omp_requires) OMP_MEMORY_ORDER_RELAXED;
47784 else if (!strcmp (p, "acq_rel"))
47785 this_req
47786 = (enum omp_requires) OMP_MEMORY_ORDER_ACQ_REL;
47788 if (this_req == 0)
47790 error_at (cp_lexer_peek_token (parser->lexer)->location,
47791 "expected %<seq_cst%>, %<relaxed%> or "
47792 "%<acq_rel%>");
47793 switch (cp_lexer_peek_token (parser->lexer)->type)
47795 case CPP_EOF:
47796 case CPP_PRAGMA_EOL:
47797 case CPP_CLOSE_PAREN:
47798 break;
47799 default:
47800 if (cp_lexer_nth_token_is (parser->lexer, 2,
47801 CPP_CLOSE_PAREN))
47802 cp_lexer_consume_token (parser->lexer);
47803 break;
47806 else
47807 cp_lexer_consume_token (parser->lexer);
47809 if (!parens.require_close (parser))
47810 cp_parser_skip_to_closing_parenthesis (parser,
47811 /*recovering=*/true,
47812 /*or_comma=*/false,
47813 /*consume_paren=*/
47814 true);
47816 if (this_req == 0)
47818 cp_parser_require_pragma_eol (parser, pragma_tok);
47819 return false;
47822 p = NULL;
47824 else
47826 error_at (cloc, "expected %<unified_address%>, "
47827 "%<unified_shared_memory%>, "
47828 "%<dynamic_allocators%>, "
47829 "%<reverse_offload%> "
47830 "or %<atomic_default_mem_order%> clause");
47831 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47832 return false;
47834 if (p)
47835 cp_lexer_consume_token (parser->lexer);
47836 if (this_req)
47838 if ((this_req & ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
47840 if ((this_req & new_req) != 0)
47841 error_at (cloc, "too many %qs clauses", p);
47842 if (this_req != OMP_REQUIRES_DYNAMIC_ALLOCATORS
47843 && (omp_requires_mask & OMP_REQUIRES_TARGET_USED) != 0)
47844 error_at (cloc, "%qs clause used lexically after first "
47845 "target construct or offloading API", p);
47847 else if ((new_req & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
47849 error_at (cloc, "too many %qs clauses",
47850 "atomic_default_mem_order");
47851 this_req = (enum omp_requires) 0;
47853 else if ((omp_requires_mask
47854 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
47856 error_at (cloc, "more than one %<atomic_default_mem_order%>"
47857 " clause in a single compilation unit");
47858 this_req
47859 = (enum omp_requires)
47860 (omp_requires_mask
47861 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER);
47863 else if ((omp_requires_mask
47864 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED) != 0)
47865 error_at (cloc, "%<atomic_default_mem_order%> clause used "
47866 "lexically after first %<atomic%> construct "
47867 "without memory order clause");
47868 new_req = (enum omp_requires) (new_req | this_req);
47869 omp_requires_mask
47870 = (enum omp_requires) (omp_requires_mask | this_req);
47871 continue;
47874 break;
47876 cp_parser_require_pragma_eol (parser, pragma_tok);
47878 if (new_req == 0)
47879 error_at (loc, "%<pragma omp requires%> requires at least one clause");
47880 return false;
47884 /* OpenMP 5.1:
47885 #pragma omp nothing new-line */
47887 static void
47888 cp_parser_omp_nothing (cp_parser *parser, cp_token *pragma_tok)
47890 cp_parser_require_pragma_eol (parser, pragma_tok);
47894 /* OpenMP 5.1
47895 #pragma omp error clauses[optseq] new-line */
47897 static bool
47898 cp_parser_omp_error (cp_parser *parser, cp_token *pragma_tok,
47899 enum pragma_context context)
47901 int at_compilation = -1;
47902 int severity_fatal = -1;
47903 tree message = NULL_TREE;
47904 bool bad = false;
47905 location_t loc = pragma_tok->location;
47907 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
47909 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
47910 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
47911 cp_lexer_consume_token (parser->lexer);
47913 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
47914 break;
47916 const char *p
47917 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
47918 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
47919 static const char *args[] = {
47920 "execution", "compilation", "warning", "fatal"
47922 int *v = NULL;
47923 int idx = 0, n = -1;
47924 tree m = NULL_TREE;
47926 if (!strcmp (p, "at"))
47927 v = &at_compilation;
47928 else if (!strcmp (p, "severity"))
47930 v = &severity_fatal;
47931 idx += 2;
47933 else if (strcmp (p, "message"))
47935 error_at (cloc,
47936 "expected %<at%>, %<severity%> or %<message%> clause");
47937 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47938 return false;
47941 cp_lexer_consume_token (parser->lexer);
47943 matching_parens parens;
47944 if (parens.require_open (parser))
47946 if (v == NULL)
47948 m = cp_parser_assignment_expression (parser);
47949 if (type_dependent_expression_p (m))
47950 m = build1 (IMPLICIT_CONV_EXPR, const_string_type_node, m);
47951 else
47952 m = perform_implicit_conversion_flags (const_string_type_node, m,
47953 tf_warning_or_error,
47954 LOOKUP_NORMAL);
47956 else
47958 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47960 tree val = cp_lexer_peek_token (parser->lexer)->u.value;
47961 const char *q = IDENTIFIER_POINTER (val);
47963 if (!strcmp (q, args[idx]))
47964 n = 0;
47965 else if (!strcmp (q, args[idx + 1]))
47966 n = 1;
47968 if (n == -1)
47970 error_at (cp_lexer_peek_token (parser->lexer)->location,
47971 "expected %qs or %qs", args[idx], args[idx + 1]);
47972 bad = true;
47973 switch (cp_lexer_peek_token (parser->lexer)->type)
47975 case CPP_EOF:
47976 case CPP_PRAGMA_EOL:
47977 case CPP_CLOSE_PAREN:
47978 break;
47979 default:
47980 if (cp_lexer_nth_token_is (parser->lexer, 2,
47981 CPP_CLOSE_PAREN))
47982 cp_lexer_consume_token (parser->lexer);
47983 break;
47986 else
47987 cp_lexer_consume_token (parser->lexer);
47990 if (!parens.require_close (parser))
47991 cp_parser_skip_to_closing_parenthesis (parser,
47992 /*recovering=*/true,
47993 /*or_comma=*/false,
47994 /*consume_paren=*/
47995 true);
47997 if (v == NULL)
47999 if (message)
48001 error_at (cloc, "too many %qs clauses", p);
48002 bad = true;
48004 else
48005 message = m;
48007 else if (n != -1)
48009 if (*v != -1)
48011 error_at (cloc, "too many %qs clauses", p);
48012 bad = true;
48014 else
48015 *v = n;
48018 else
48019 bad = true;
48021 cp_parser_require_pragma_eol (parser, pragma_tok);
48022 if (bad)
48023 return true;
48025 if (at_compilation == -1)
48026 at_compilation = 1;
48027 if (severity_fatal == -1)
48028 severity_fatal = 1;
48029 if (!at_compilation)
48031 if (context != pragma_compound)
48033 error_at (loc, "%<#pragma omp error%> with %<at(execution)%> clause "
48034 "may only be used in compound statements");
48035 return true;
48037 tree fndecl
48038 = builtin_decl_explicit (severity_fatal ? BUILT_IN_GOMP_ERROR
48039 : BUILT_IN_GOMP_WARNING);
48040 if (!message)
48041 message = build_zero_cst (const_string_type_node);
48042 tree stmt = build_call_expr_loc (loc, fndecl, 2, message,
48043 build_all_ones_cst (size_type_node));
48044 add_stmt (stmt);
48045 return true;
48048 if (in_discarded_stmt)
48049 return false;
48051 const char *msg = NULL;
48052 if (message)
48054 msg = c_getstr (fold_for_warn (message));
48055 if (msg == NULL)
48056 msg = _("<message unknown at compile time>");
48058 if (msg)
48059 emit_diagnostic (severity_fatal ? DK_ERROR : DK_WARNING, loc, 0,
48060 "%<pragma omp error%> encountered: %s", msg);
48061 else
48062 emit_diagnostic (severity_fatal ? DK_ERROR : DK_WARNING, loc, 0,
48063 "%<pragma omp error%> encountered");
48064 return false;
48067 /* OpenMP 4.5:
48068 #pragma omp taskloop taskloop-clause[optseq] new-line
48069 for-loop
48071 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
48072 for-loop */
48074 #define OMP_TASKLOOP_CLAUSE_MASK \
48075 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
48076 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
48077 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
48078 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
48079 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
48080 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
48081 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
48082 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
48083 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
48084 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
48085 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
48086 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
48087 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
48088 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
48089 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
48090 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
48091 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
48093 static tree
48094 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
48095 char *p_name, omp_clause_mask mask, tree *cclauses,
48096 bool *if_p)
48098 tree clauses, sb, ret;
48099 unsigned int save;
48100 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
48102 strcat (p_name, " taskloop");
48103 mask |= OMP_TASKLOOP_CLAUSE_MASK;
48104 /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
48105 clause. */
48106 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)) != 0)
48107 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION);
48109 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
48111 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
48112 const char *p = IDENTIFIER_POINTER (id);
48114 if (strcmp (p, "simd") == 0)
48116 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
48117 if (cclauses == NULL)
48118 cclauses = cclauses_buf;
48120 cp_lexer_consume_token (parser->lexer);
48121 if (!flag_openmp) /* flag_openmp_simd */
48122 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
48123 cclauses, if_p);
48124 sb = begin_omp_structured_block ();
48125 save = cp_parser_begin_omp_structured_block (parser);
48126 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
48127 cclauses, if_p);
48128 cp_parser_end_omp_structured_block (parser, save);
48129 tree body = finish_omp_structured_block (sb);
48130 if (ret == NULL)
48131 return ret;
48132 ret = make_node (OMP_TASKLOOP);
48133 TREE_TYPE (ret) = void_type_node;
48134 OMP_FOR_BODY (ret) = body;
48135 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
48136 SET_EXPR_LOCATION (ret, loc);
48137 add_stmt (ret);
48138 return ret;
48141 if (!flag_openmp) /* flag_openmp_simd */
48143 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
48144 return NULL_TREE;
48147 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
48148 cclauses == NULL);
48149 if (cclauses)
48151 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
48152 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
48155 keep_next_level (true);
48156 sb = begin_omp_structured_block ();
48157 save = cp_parser_begin_omp_structured_block (parser);
48159 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
48160 if_p);
48162 cp_parser_end_omp_structured_block (parser, save);
48163 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
48165 return ret;
48169 /* OpenACC 2.0:
48170 # pragma acc routine oacc-routine-clause[optseq] new-line
48171 function-definition
48173 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
48176 #define OACC_ROUTINE_CLAUSE_MASK \
48177 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
48178 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
48179 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
48180 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
48181 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NOHOST) )
48183 /* Parse the OpenACC routine pragma. This has an optional '( name )'
48184 component, which must resolve to a declared namespace-scope
48185 function. The clauses are either processed directly (for a named
48186 function), or defered until the immediatley following declaration
48187 is parsed. */
48189 static void
48190 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
48191 enum pragma_context context)
48193 gcc_checking_assert (context == pragma_external);
48194 /* The checking for "another pragma following this one" in the "no optional
48195 '( name )'" case makes sure that we dont re-enter. */
48196 gcc_checking_assert (parser->oacc_routine == NULL);
48198 cp_oacc_routine_data data;
48199 data.error_seen = false;
48200 data.fndecl_seen = false;
48201 data.tokens = vNULL;
48202 data.clauses = NULL_TREE;
48203 data.loc = pragma_tok->location;
48204 /* It is safe to take the address of a local variable; it will only be
48205 used while this scope is live. */
48206 parser->oacc_routine = &data;
48208 /* Look for optional '( name )'. */
48209 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
48211 matching_parens parens;
48212 parens.consume_open (parser); /* '(' */
48214 /* We parse the name as an id-expression. If it resolves to
48215 anything other than a non-overloaded function at namespace
48216 scope, it's an error. */
48217 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
48218 tree name = cp_parser_id_expression (parser,
48219 /*template_keyword_p=*/false,
48220 /*check_dependency_p=*/false,
48221 /*template_p=*/NULL,
48222 /*declarator_p=*/false,
48223 /*optional_p=*/false);
48224 tree decl = (identifier_p (name)
48225 ? cp_parser_lookup_name_simple (parser, name, name_loc)
48226 : name);
48227 if (name != error_mark_node && decl == error_mark_node)
48228 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
48230 if (decl == error_mark_node
48231 || !parens.require_close (parser))
48233 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
48234 parser->oacc_routine = NULL;
48235 return;
48238 data.clauses
48239 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
48240 "#pragma acc routine",
48241 cp_lexer_peek_token (parser->lexer));
48242 /* The clauses are in reverse order; fix that to make later diagnostic
48243 emission easier. */
48244 data.clauses = nreverse (data.clauses);
48246 if (decl && is_overloaded_fn (decl)
48247 && (TREE_CODE (decl) != FUNCTION_DECL
48248 || DECL_FUNCTION_TEMPLATE_P (decl)))
48250 error_at (name_loc,
48251 "%<#pragma acc routine%> names a set of overloads");
48252 parser->oacc_routine = NULL;
48253 return;
48256 /* Perhaps we should use the same rule as declarations in different
48257 namespaces? */
48258 if (!DECL_NAMESPACE_SCOPE_P (decl))
48260 error_at (name_loc,
48261 "%qD does not refer to a namespace scope function", decl);
48262 parser->oacc_routine = NULL;
48263 return;
48266 if (TREE_CODE (decl) != FUNCTION_DECL)
48268 error_at (name_loc, "%qD does not refer to a function", decl);
48269 parser->oacc_routine = NULL;
48270 return;
48273 cp_finalize_oacc_routine (parser, decl, false);
48274 parser->oacc_routine = NULL;
48276 else /* No optional '( name )'. */
48278 /* Store away all pragma tokens. */
48279 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
48280 cp_lexer_consume_token (parser->lexer);
48281 cp_parser_require_pragma_eol (parser, pragma_tok);
48282 struct cp_token_cache *cp
48283 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
48284 parser->oacc_routine->tokens.safe_push (cp);
48286 /* Emit a helpful diagnostic if there's another pragma following this
48287 one. */
48288 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
48290 cp_ensure_no_oacc_routine (parser);
48291 data.tokens.release ();
48292 /* ..., and then just keep going. */
48293 return;
48296 /* We only have to consider the pragma_external case here. */
48297 cp_parser_declaration (parser, NULL_TREE);
48298 if (parser->oacc_routine
48299 && !parser->oacc_routine->fndecl_seen)
48300 cp_ensure_no_oacc_routine (parser);
48301 else
48302 parser->oacc_routine = NULL;
48303 data.tokens.release ();
48307 /* Finalize #pragma acc routine clauses after direct declarator has
48308 been parsed. */
48310 static tree
48311 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
48313 struct cp_token_cache *ce;
48314 cp_oacc_routine_data *data = parser->oacc_routine;
48316 if (!data->error_seen && data->fndecl_seen)
48318 error_at (data->loc,
48319 "%<#pragma acc routine%> not immediately followed by "
48320 "a single function declaration or definition");
48321 data->error_seen = true;
48323 if (data->error_seen)
48324 return attrs;
48326 gcc_checking_assert (data->tokens.length () == 1);
48327 ce = data->tokens[0];
48329 cp_parser_push_lexer_for_tokens (parser, ce);
48330 parser->lexer->in_pragma = true;
48331 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
48333 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
48334 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
48335 parser->oacc_routine->clauses
48336 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
48337 "#pragma acc routine", pragma_tok);
48338 /* The clauses are in reverse order; fix that to make later diagnostic
48339 emission easier. */
48340 parser->oacc_routine->clauses = nreverse (parser->oacc_routine->clauses);
48341 cp_parser_pop_lexer (parser);
48342 /* Later, cp_finalize_oacc_routine will process the clauses. */
48343 parser->oacc_routine->fndecl_seen = true;
48345 return attrs;
48348 /* Apply any saved OpenACC routine clauses to a just-parsed
48349 declaration. */
48351 static void
48352 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
48354 if (UNLIKELY (parser->oacc_routine != NULL))
48356 /* Keep going if we're in error reporting mode. */
48357 if (parser->oacc_routine->error_seen
48358 || fndecl == error_mark_node)
48359 return;
48361 if (TREE_CODE (fndecl) != FUNCTION_DECL)
48363 if (parser->oacc_routine->fndecl_seen)
48365 error_at (parser->oacc_routine->loc,
48366 "%<#pragma acc routine%> not immediately followed by"
48367 " a single function declaration or definition");
48368 parser->oacc_routine = NULL;
48369 return;
48372 cp_ensure_no_oacc_routine (parser);
48373 return;
48376 int compatible
48377 = oacc_verify_routine_clauses (fndecl, &parser->oacc_routine->clauses,
48378 parser->oacc_routine->loc,
48379 "#pragma acc routine");
48380 if (compatible < 0)
48382 parser->oacc_routine = NULL;
48383 return;
48385 if (compatible > 0)
48388 else
48390 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
48392 error_at (parser->oacc_routine->loc,
48393 TREE_USED (fndecl)
48394 ? G_("%<#pragma acc routine%> must be applied before"
48395 " use")
48396 : G_("%<#pragma acc routine%> must be applied before"
48397 " definition"));
48398 parser->oacc_routine = NULL;
48399 return;
48402 /* Set the routine's level of parallelism. */
48403 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
48404 oacc_replace_fn_attrib (fndecl, dims);
48406 /* Add an "omp declare target" attribute. */
48407 DECL_ATTRIBUTES (fndecl)
48408 = tree_cons (get_identifier ("omp declare target"),
48409 parser->oacc_routine->clauses,
48410 DECL_ATTRIBUTES (fndecl));
48415 /* Main entry point to OpenMP statement pragmas. */
48417 static void
48418 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
48420 tree stmt;
48421 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
48422 omp_clause_mask mask (0);
48424 switch (cp_parser_pragma_kind (pragma_tok))
48426 case PRAGMA_OACC_ATOMIC:
48427 cp_parser_omp_atomic (parser, pragma_tok, true);
48428 return;
48429 case PRAGMA_OACC_CACHE:
48430 stmt = cp_parser_oacc_cache (parser, pragma_tok);
48431 break;
48432 case PRAGMA_OACC_DATA:
48433 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
48434 break;
48435 case PRAGMA_OACC_ENTER_DATA:
48436 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
48437 break;
48438 case PRAGMA_OACC_EXIT_DATA:
48439 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
48440 break;
48441 case PRAGMA_OACC_HOST_DATA:
48442 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
48443 break;
48444 case PRAGMA_OACC_KERNELS:
48445 case PRAGMA_OACC_PARALLEL:
48446 case PRAGMA_OACC_SERIAL:
48447 strcpy (p_name, "#pragma acc");
48448 stmt = cp_parser_oacc_compute (parser, pragma_tok, p_name, if_p);
48449 break;
48450 case PRAGMA_OACC_LOOP:
48451 strcpy (p_name, "#pragma acc");
48452 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
48453 if_p);
48454 break;
48455 case PRAGMA_OACC_UPDATE:
48456 stmt = cp_parser_oacc_update (parser, pragma_tok);
48457 break;
48458 case PRAGMA_OACC_WAIT:
48459 stmt = cp_parser_oacc_wait (parser, pragma_tok);
48460 break;
48461 case PRAGMA_OMP_ALLOCATE:
48462 cp_parser_omp_allocate (parser, pragma_tok);
48463 return;
48464 case PRAGMA_OMP_ATOMIC:
48465 cp_parser_omp_atomic (parser, pragma_tok, false);
48466 return;
48467 case PRAGMA_OMP_CRITICAL:
48468 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
48469 break;
48470 case PRAGMA_OMP_DISTRIBUTE:
48471 strcpy (p_name, "#pragma omp");
48472 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
48473 if_p);
48474 break;
48475 case PRAGMA_OMP_FOR:
48476 strcpy (p_name, "#pragma omp");
48477 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
48478 if_p);
48479 break;
48480 case PRAGMA_OMP_LOOP:
48481 strcpy (p_name, "#pragma omp");
48482 stmt = cp_parser_omp_loop (parser, pragma_tok, p_name, mask, NULL,
48483 if_p);
48484 break;
48485 case PRAGMA_OMP_MASKED:
48486 strcpy (p_name, "#pragma omp");
48487 stmt = cp_parser_omp_masked (parser, pragma_tok, p_name, mask, NULL,
48488 if_p);
48489 break;
48490 case PRAGMA_OMP_MASTER:
48491 strcpy (p_name, "#pragma omp");
48492 stmt = cp_parser_omp_master (parser, pragma_tok, p_name, mask, NULL,
48493 if_p);
48494 break;
48495 case PRAGMA_OMP_PARALLEL:
48496 strcpy (p_name, "#pragma omp");
48497 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
48498 if_p);
48499 break;
48500 case PRAGMA_OMP_SCOPE:
48501 stmt = cp_parser_omp_scope (parser, pragma_tok, if_p);
48502 break;
48503 case PRAGMA_OMP_SECTIONS:
48504 strcpy (p_name, "#pragma omp");
48505 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
48506 break;
48507 case PRAGMA_OMP_SIMD:
48508 strcpy (p_name, "#pragma omp");
48509 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
48510 if_p);
48511 break;
48512 case PRAGMA_OMP_SINGLE:
48513 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
48514 break;
48515 case PRAGMA_OMP_TASK:
48516 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
48517 break;
48518 case PRAGMA_OMP_TASKGROUP:
48519 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
48520 break;
48521 case PRAGMA_OMP_TASKLOOP:
48522 strcpy (p_name, "#pragma omp");
48523 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
48524 if_p);
48525 break;
48526 case PRAGMA_OMP_TEAMS:
48527 strcpy (p_name, "#pragma omp");
48528 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
48529 if_p);
48530 break;
48531 case PRAGMA_OMP_ASSUME:
48532 cp_parser_omp_assume (parser, pragma_tok, if_p);
48533 return;
48534 default:
48535 gcc_unreachable ();
48538 protected_set_expr_location (stmt, pragma_tok->location);
48541 /* Transactional Memory parsing routines. */
48543 /* Parse a transaction attribute.
48545 txn-attribute:
48546 attribute
48547 [ [ identifier ] ]
48549 We use this instead of cp_parser_attributes_opt for transactions to avoid
48550 the pedwarn in C++98 mode. */
48552 static tree
48553 cp_parser_txn_attribute_opt (cp_parser *parser)
48555 cp_token *token;
48556 tree attr_name, attr = NULL;
48558 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
48559 return cp_parser_attributes_opt (parser);
48561 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
48562 return NULL_TREE;
48563 cp_lexer_consume_token (parser->lexer);
48564 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
48565 goto error1;
48567 token = cp_lexer_peek_token (parser->lexer);
48568 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
48570 token = cp_lexer_consume_token (parser->lexer);
48572 attr_name = (token->type == CPP_KEYWORD
48573 /* For keywords, use the canonical spelling,
48574 not the parsed identifier. */
48575 ? ridpointers[(int) token->keyword]
48576 : token->u.value);
48577 attr = build_tree_list (attr_name, NULL_TREE);
48579 else
48580 cp_parser_error (parser, "expected identifier");
48582 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
48583 error1:
48584 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
48585 return attr;
48588 /* Parse a __transaction_atomic or __transaction_relaxed statement.
48590 transaction-statement:
48591 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
48592 compound-statement
48593 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
48596 static tree
48597 cp_parser_transaction (cp_parser *parser, cp_token *token)
48599 unsigned char old_in = parser->in_transaction;
48600 unsigned char this_in = 1, new_in;
48601 enum rid keyword = token->keyword;
48602 tree stmt, attrs, noex;
48604 cp_lexer_consume_token (parser->lexer);
48606 if (keyword == RID_TRANSACTION_RELAXED
48607 || keyword == RID_SYNCHRONIZED)
48608 this_in |= TM_STMT_ATTR_RELAXED;
48609 else
48611 attrs = cp_parser_txn_attribute_opt (parser);
48612 if (attrs)
48613 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
48616 /* Parse a noexcept specification. */
48617 if (keyword == RID_ATOMIC_NOEXCEPT)
48618 noex = boolean_true_node;
48619 else if (keyword == RID_ATOMIC_CANCEL)
48621 /* cancel-and-throw is unimplemented. */
48622 sorry ("%<atomic_cancel%>");
48623 noex = NULL_TREE;
48625 else
48626 noex = cp_parser_noexcept_specification_opt (parser,
48627 CP_PARSER_FLAGS_NONE,
48628 /*require_constexpr=*/true,
48629 /*consumed_expr=*/NULL,
48630 /*return_cond=*/true);
48632 /* Keep track if we're in the lexical scope of an outer transaction. */
48633 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
48635 stmt = begin_transaction_stmt (token->location, NULL, this_in);
48637 parser->in_transaction = new_in;
48638 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
48639 parser->in_transaction = old_in;
48641 finish_transaction_stmt (stmt, NULL, this_in, noex);
48643 return stmt;
48646 /* Parse a __transaction_atomic or __transaction_relaxed expression.
48648 transaction-expression:
48649 __transaction_atomic txn-noexcept-spec[opt] ( expression )
48650 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
48653 static tree
48654 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
48656 unsigned char old_in = parser->in_transaction;
48657 unsigned char this_in = 1;
48658 cp_token *token;
48659 tree expr, noex;
48660 bool noex_expr;
48661 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
48663 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
48664 || keyword == RID_TRANSACTION_RELAXED);
48666 if (!flag_tm)
48667 error_at (loc,
48668 keyword == RID_TRANSACTION_RELAXED
48669 ? G_("%<__transaction_relaxed%> without transactional memory "
48670 "support enabled")
48671 : G_("%<__transaction_atomic%> without transactional memory "
48672 "support enabled"));
48674 token = cp_parser_require_keyword (parser, keyword,
48675 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
48676 : RT_TRANSACTION_RELAXED));
48677 gcc_assert (token != NULL);
48679 if (keyword == RID_TRANSACTION_RELAXED)
48680 this_in |= TM_STMT_ATTR_RELAXED;
48682 /* Set this early. This might mean that we allow transaction_cancel in
48683 an expression that we find out later actually has to be a constexpr.
48684 However, we expect that cxx_constant_value will be able to deal with
48685 this; also, if the noexcept has no constexpr, then what we parse next
48686 really is a transaction's body. */
48687 parser->in_transaction = this_in;
48689 /* Parse a noexcept specification. */
48690 noex = cp_parser_noexcept_specification_opt (parser,
48691 CP_PARSER_FLAGS_NONE,
48692 /*require_constexpr=*/false,
48693 &noex_expr,
48694 /*return_cond=*/true);
48696 if (!noex || !noex_expr
48697 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
48699 matching_parens parens;
48700 parens.require_open (parser);
48702 expr = cp_parser_expression (parser);
48703 expr = finish_parenthesized_expr (expr);
48705 parens.require_close (parser);
48707 else
48709 /* The only expression that is available got parsed for the noexcept
48710 already. noexcept is true then. */
48711 expr = noex;
48712 noex = boolean_true_node;
48715 expr = build_transaction_expr (token->location, expr, this_in, noex);
48716 parser->in_transaction = old_in;
48718 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
48719 return error_mark_node;
48721 return (flag_tm ? expr : error_mark_node);
48724 /* Parse a function-transaction-block.
48726 function-transaction-block:
48727 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
48728 function-body
48729 __transaction_atomic txn-attribute[opt] function-try-block
48730 __transaction_relaxed ctor-initializer[opt] function-body
48731 __transaction_relaxed function-try-block
48734 static void
48735 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
48737 unsigned char old_in = parser->in_transaction;
48738 unsigned char new_in = 1;
48739 tree compound_stmt, stmt, attrs;
48740 cp_token *token;
48742 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
48743 || keyword == RID_TRANSACTION_RELAXED);
48744 token = cp_parser_require_keyword (parser, keyword,
48745 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
48746 : RT_TRANSACTION_RELAXED));
48747 gcc_assert (token != NULL);
48749 if (keyword == RID_TRANSACTION_RELAXED)
48750 new_in |= TM_STMT_ATTR_RELAXED;
48751 else
48753 attrs = cp_parser_txn_attribute_opt (parser);
48754 if (attrs)
48755 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
48758 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
48760 parser->in_transaction = new_in;
48762 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
48763 cp_parser_function_try_block (parser);
48764 else
48765 cp_parser_ctor_initializer_opt_and_function_body
48766 (parser, /*in_function_try_block=*/false);
48768 parser->in_transaction = old_in;
48770 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
48773 /* Parse a __transaction_cancel statement.
48775 cancel-statement:
48776 __transaction_cancel txn-attribute[opt] ;
48777 __transaction_cancel txn-attribute[opt] throw-expression ;
48779 ??? Cancel and throw is not yet implemented. */
48781 static tree
48782 cp_parser_transaction_cancel (cp_parser *parser)
48784 cp_token *token;
48785 bool is_outer = false;
48786 tree stmt, attrs;
48788 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
48789 RT_TRANSACTION_CANCEL);
48790 gcc_assert (token != NULL);
48792 attrs = cp_parser_txn_attribute_opt (parser);
48793 if (attrs)
48794 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
48796 /* ??? Parse cancel-and-throw here. */
48798 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
48800 if (!flag_tm)
48802 error_at (token->location, "%<__transaction_cancel%> without "
48803 "transactional memory support enabled");
48804 return error_mark_node;
48806 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
48808 error_at (token->location, "%<__transaction_cancel%> within a "
48809 "%<__transaction_relaxed%>");
48810 return error_mark_node;
48812 else if (is_outer)
48814 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
48815 && !is_tm_may_cancel_outer (current_function_decl))
48817 error_at (token->location, "outer %<__transaction_cancel%> not "
48818 "within outer %<__transaction_atomic%>");
48819 error_at (token->location,
48820 " or a %<transaction_may_cancel_outer%> function");
48821 return error_mark_node;
48824 else if (parser->in_transaction == 0)
48826 error_at (token->location, "%<__transaction_cancel%> not within "
48827 "%<__transaction_atomic%>");
48828 return error_mark_node;
48831 stmt = build_tm_abort_call (token->location, is_outer);
48832 add_stmt (stmt);
48834 return stmt;
48838 /* Special handling for the first token or line in the file. The first
48839 thing in the file might be #pragma GCC pch_preprocess, which loads a
48840 PCH file, which is a GC collection point. So we need to handle this
48841 first pragma without benefit of an existing lexer structure.
48843 Always returns one token to the caller in *FIRST_TOKEN. This is
48844 either the true first token of the file, or the first token after
48845 the initial pragma. */
48847 static void
48848 cp_parser_initial_pragma (cp_token *first_token)
48850 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
48851 return;
48853 cp_lexer_get_preprocessor_token (0, first_token);
48855 tree name = NULL;
48856 if (first_token->type == CPP_STRING)
48858 name = first_token->u.value;
48860 cp_lexer_get_preprocessor_token (0, first_token);
48863 /* Skip to the end of the pragma. */
48864 if (first_token->type != CPP_PRAGMA_EOL)
48866 error_at (first_token->location,
48867 "malformed %<#pragma GCC pch_preprocess%>");
48869 cp_lexer_get_preprocessor_token (0, first_token);
48870 while (first_token->type != CPP_PRAGMA_EOL);
48873 /* Now actually load the PCH file. */
48874 if (name)
48875 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
48877 /* Read one more token to return to our caller. We have to do this
48878 after reading the PCH file in, since its pointers have to be
48879 live. */
48880 cp_lexer_get_preprocessor_token (0, first_token);
48883 /* Parse a pragma GCC ivdep. */
48885 static bool
48886 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
48888 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
48889 return true;
48892 /* Parse a pragma GCC unroll. */
48894 static unsigned short
48895 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
48897 location_t location = cp_lexer_peek_token (parser->lexer)->location;
48898 tree expr = cp_parser_constant_expression (parser);
48899 unsigned short unroll;
48900 expr = maybe_constant_value (expr);
48901 HOST_WIDE_INT lunroll = 0;
48902 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
48903 || TREE_CODE (expr) != INTEGER_CST
48904 || (lunroll = tree_to_shwi (expr)) < 0
48905 || lunroll >= USHRT_MAX)
48907 error_at (location, "%<#pragma GCC unroll%> requires an"
48908 " assignment-expression that evaluates to a non-negative"
48909 " integral constant less than %u", USHRT_MAX);
48910 unroll = 0;
48912 else
48914 unroll = (unsigned short)lunroll;
48915 if (unroll == 0)
48916 unroll = 1;
48918 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
48919 return unroll;
48922 /* Normal parsing of a pragma token. Here we can (and must) use the
48923 regular lexer. */
48925 static bool
48926 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
48928 cp_token *pragma_tok;
48929 unsigned int id;
48930 tree stmt;
48931 bool ret = false;
48933 pragma_tok = cp_lexer_consume_token (parser->lexer);
48934 gcc_assert (pragma_tok->type == CPP_PRAGMA);
48935 parser->lexer->in_pragma = true;
48937 id = cp_parser_pragma_kind (pragma_tok);
48938 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
48939 cp_ensure_no_omp_declare_simd (parser);
48940 switch (id)
48942 case PRAGMA_GCC_PCH_PREPROCESS:
48943 error_at (pragma_tok->location,
48944 "%<#pragma GCC pch_preprocess%> must be first");
48945 break;
48947 case PRAGMA_OMP_BARRIER:
48948 switch (context)
48950 case pragma_compound:
48951 cp_parser_omp_barrier (parser, pragma_tok);
48952 return false;
48953 case pragma_stmt:
48954 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
48955 "used in compound statements", "omp barrier");
48956 ret = true;
48957 break;
48958 default:
48959 goto bad_stmt;
48961 break;
48963 case PRAGMA_OMP_DEPOBJ:
48964 switch (context)
48966 case pragma_compound:
48967 cp_parser_omp_depobj (parser, pragma_tok);
48968 return false;
48969 case pragma_stmt:
48970 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
48971 "used in compound statements", "omp depobj");
48972 ret = true;
48973 break;
48974 default:
48975 goto bad_stmt;
48977 break;
48979 case PRAGMA_OMP_FLUSH:
48980 switch (context)
48982 case pragma_compound:
48983 cp_parser_omp_flush (parser, pragma_tok);
48984 return false;
48985 case pragma_stmt:
48986 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
48987 "used in compound statements", "omp flush");
48988 ret = true;
48989 break;
48990 default:
48991 goto bad_stmt;
48993 break;
48995 case PRAGMA_OMP_TASKWAIT:
48996 switch (context)
48998 case pragma_compound:
48999 cp_parser_omp_taskwait (parser, pragma_tok);
49000 return false;
49001 case pragma_stmt:
49002 error_at (pragma_tok->location,
49003 "%<#pragma %s%> may only be used in compound statements",
49004 "omp taskwait");
49005 ret = true;
49006 break;
49007 default:
49008 goto bad_stmt;
49010 break;
49012 case PRAGMA_OMP_TASKYIELD:
49013 switch (context)
49015 case pragma_compound:
49016 cp_parser_omp_taskyield (parser, pragma_tok);
49017 return false;
49018 case pragma_stmt:
49019 error_at (pragma_tok->location,
49020 "%<#pragma %s%> may only be used in compound statements",
49021 "omp taskyield");
49022 ret = true;
49023 break;
49024 default:
49025 goto bad_stmt;
49027 break;
49029 case PRAGMA_OMP_CANCEL:
49030 switch (context)
49032 case pragma_compound:
49033 cp_parser_omp_cancel (parser, pragma_tok);
49034 return false;
49035 case pragma_stmt:
49036 error_at (pragma_tok->location,
49037 "%<#pragma %s%> may only be used in compound statements",
49038 "omp cancel");
49039 ret = true;
49040 break;
49041 default:
49042 goto bad_stmt;
49044 break;
49046 case PRAGMA_OMP_CANCELLATION_POINT:
49047 return cp_parser_omp_cancellation_point (parser, pragma_tok, context);
49049 case PRAGMA_OMP_THREADPRIVATE:
49050 cp_parser_omp_threadprivate (parser, pragma_tok);
49051 return false;
49053 case PRAGMA_OMP_DECLARE:
49054 return cp_parser_omp_declare (parser, pragma_tok, context);
49056 case PRAGMA_OACC_DECLARE:
49057 cp_parser_oacc_declare (parser, pragma_tok);
49058 return false;
49060 case PRAGMA_OACC_ENTER_DATA:
49061 if (context == pragma_stmt)
49063 error_at (pragma_tok->location,
49064 "%<#pragma %s%> may only be used in compound statements",
49065 "acc enter data");
49066 ret = true;
49067 break;
49069 else if (context != pragma_compound)
49070 goto bad_stmt;
49071 cp_parser_omp_construct (parser, pragma_tok, if_p);
49072 return true;
49074 case PRAGMA_OACC_EXIT_DATA:
49075 if (context == pragma_stmt)
49077 error_at (pragma_tok->location,
49078 "%<#pragma %s%> may only be used in compound statements",
49079 "acc exit data");
49080 ret = true;
49081 break;
49083 else if (context != pragma_compound)
49084 goto bad_stmt;
49085 cp_parser_omp_construct (parser, pragma_tok, if_p);
49086 return true;
49088 case PRAGMA_OACC_ROUTINE:
49089 if (context != pragma_external)
49091 error_at (pragma_tok->location,
49092 "%<#pragma acc routine%> must be at file scope");
49093 ret = true;
49094 break;
49096 cp_parser_oacc_routine (parser, pragma_tok, context);
49097 return false;
49099 case PRAGMA_OACC_UPDATE:
49100 if (context == pragma_stmt)
49102 error_at (pragma_tok->location,
49103 "%<#pragma %s%> may only be used in compound statements",
49104 "acc update");
49105 ret = true;
49106 break;
49108 else if (context != pragma_compound)
49109 goto bad_stmt;
49110 cp_parser_omp_construct (parser, pragma_tok, if_p);
49111 return true;
49113 case PRAGMA_OACC_WAIT:
49114 if (context == pragma_stmt)
49116 error_at (pragma_tok->location,
49117 "%<#pragma %s%> may only be used in compound statements",
49118 "acc wait");
49119 ret = true;
49120 break;
49122 else if (context != pragma_compound)
49123 goto bad_stmt;
49124 cp_parser_omp_construct (parser, pragma_tok, if_p);
49125 return true;
49126 case PRAGMA_OMP_ALLOCATE:
49127 cp_parser_omp_allocate (parser, pragma_tok);
49128 return false;
49129 case PRAGMA_OACC_ATOMIC:
49130 case PRAGMA_OACC_CACHE:
49131 case PRAGMA_OACC_DATA:
49132 case PRAGMA_OACC_HOST_DATA:
49133 case PRAGMA_OACC_KERNELS:
49134 case PRAGMA_OACC_LOOP:
49135 case PRAGMA_OACC_PARALLEL:
49136 case PRAGMA_OACC_SERIAL:
49137 case PRAGMA_OMP_ASSUME:
49138 case PRAGMA_OMP_ATOMIC:
49139 case PRAGMA_OMP_CRITICAL:
49140 case PRAGMA_OMP_DISTRIBUTE:
49141 case PRAGMA_OMP_FOR:
49142 case PRAGMA_OMP_LOOP:
49143 case PRAGMA_OMP_MASKED:
49144 case PRAGMA_OMP_MASTER:
49145 case PRAGMA_OMP_PARALLEL:
49146 case PRAGMA_OMP_SCOPE:
49147 case PRAGMA_OMP_SECTIONS:
49148 case PRAGMA_OMP_SIMD:
49149 case PRAGMA_OMP_SINGLE:
49150 case PRAGMA_OMP_TASK:
49151 case PRAGMA_OMP_TASKGROUP:
49152 case PRAGMA_OMP_TASKLOOP:
49153 case PRAGMA_OMP_TEAMS:
49154 if (context != pragma_stmt && context != pragma_compound)
49155 goto bad_stmt;
49156 stmt = push_omp_privatization_clauses (false);
49157 cp_parser_omp_construct (parser, pragma_tok, if_p);
49158 pop_omp_privatization_clauses (stmt);
49159 return true;
49161 case PRAGMA_OMP_REQUIRES:
49162 if (context != pragma_external)
49164 error_at (pragma_tok->location,
49165 "%<#pragma omp requires%> may only be used at file or "
49166 "namespace scope");
49167 ret = true;
49168 break;
49170 return cp_parser_omp_requires (parser, pragma_tok);
49172 case PRAGMA_OMP_ASSUMES:
49173 if (context != pragma_external)
49175 error_at (pragma_tok->location,
49176 "%<#pragma omp assumes%> may only be used at file or "
49177 "namespace scope");
49178 ret = true;
49179 break;
49181 return cp_parser_omp_assumes (parser, pragma_tok);
49183 case PRAGMA_OMP_NOTHING:
49184 cp_parser_omp_nothing (parser, pragma_tok);
49185 return false;
49187 case PRAGMA_OMP_ERROR:
49188 return cp_parser_omp_error (parser, pragma_tok, context);
49190 case PRAGMA_OMP_ORDERED:
49191 if (context != pragma_stmt && context != pragma_compound)
49192 goto bad_stmt;
49193 stmt = push_omp_privatization_clauses (false);
49194 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
49195 pop_omp_privatization_clauses (stmt);
49196 return ret;
49198 case PRAGMA_OMP_TARGET:
49199 if (context != pragma_stmt && context != pragma_compound)
49200 goto bad_stmt;
49201 stmt = push_omp_privatization_clauses (false);
49202 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
49203 pop_omp_privatization_clauses (stmt);
49204 return ret;
49206 case PRAGMA_OMP_BEGIN:
49207 cp_parser_omp_begin (parser, pragma_tok);
49208 return false;
49210 case PRAGMA_OMP_END:
49211 cp_parser_omp_end (parser, pragma_tok);
49212 return false;
49214 case PRAGMA_OMP_SCAN:
49215 error_at (pragma_tok->location,
49216 "%<#pragma omp scan%> may only be used in "
49217 "a loop construct with %<inscan%> %<reduction%> clause");
49218 break;
49220 case PRAGMA_OMP_SECTION:
49221 error_at (pragma_tok->location,
49222 "%<#pragma omp section%> may only be used in "
49223 "%<#pragma omp sections%> construct");
49224 break;
49226 case PRAGMA_IVDEP:
49228 if (context == pragma_external)
49230 error_at (pragma_tok->location,
49231 "%<#pragma GCC ivdep%> must be inside a function");
49232 break;
49234 const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
49235 unsigned short unroll;
49236 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
49237 if (tok->type == CPP_PRAGMA
49238 && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
49240 tok = cp_lexer_consume_token (parser->lexer);
49241 unroll = cp_parser_pragma_unroll (parser, tok);
49242 tok = cp_lexer_peek_token (the_parser->lexer);
49244 else
49245 unroll = 0;
49246 if (tok->type != CPP_KEYWORD
49247 || (tok->keyword != RID_FOR
49248 && tok->keyword != RID_WHILE
49249 && tok->keyword != RID_DO))
49251 cp_parser_error (parser, "for, while or do statement expected");
49252 return false;
49254 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
49255 return true;
49258 case PRAGMA_UNROLL:
49260 if (context == pragma_external)
49262 error_at (pragma_tok->location,
49263 "%<#pragma GCC unroll%> must be inside a function");
49264 break;
49266 const unsigned short unroll
49267 = cp_parser_pragma_unroll (parser, pragma_tok);
49268 bool ivdep;
49269 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
49270 if (tok->type == CPP_PRAGMA
49271 && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
49273 tok = cp_lexer_consume_token (parser->lexer);
49274 ivdep = cp_parser_pragma_ivdep (parser, tok);
49275 tok = cp_lexer_peek_token (the_parser->lexer);
49277 else
49278 ivdep = false;
49279 if (tok->type != CPP_KEYWORD
49280 || (tok->keyword != RID_FOR
49281 && tok->keyword != RID_WHILE
49282 && tok->keyword != RID_DO))
49284 cp_parser_error (parser, "for, while or do statement expected");
49285 return false;
49287 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
49288 return true;
49291 default:
49292 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
49293 c_invoke_pragma_handler (id);
49294 break;
49296 bad_stmt:
49297 cp_parser_error (parser, "expected declaration specifiers");
49298 break;
49301 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
49302 return ret;
49305 /* The interface the pragma parsers have to the lexer. */
49307 enum cpp_ttype
49308 pragma_lex (tree *value, location_t *loc)
49310 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
49311 enum cpp_ttype ret = tok->type;
49313 *value = tok->u.value;
49314 if (loc)
49315 *loc = tok->location;
49317 if (ret == CPP_PRAGMA_EOL)
49318 ret = CPP_EOF;
49319 else if (ret == CPP_STRING)
49320 *value = cp_parser_string_literal (the_parser, false, false);
49321 else
49323 if (ret == CPP_KEYWORD)
49324 ret = CPP_NAME;
49325 cp_lexer_consume_token (the_parser->lexer);
49328 return ret;
49332 /* External interface. */
49334 /* Parse one entire translation unit. */
49336 void
49337 c_parse_file (void)
49339 static bool already_called = false;
49341 if (already_called)
49342 fatal_error (input_location,
49343 "multi-source compilation not implemented for C++");
49344 already_called = true;
49346 /* cp_lexer_new_main is called before doing any GC allocation
49347 because tokenization might load a PCH file. */
49348 cp_lexer_new_main ();
49350 cp_parser_translation_unit (the_parser);
49351 class_decl_loc_t::diag_mismatched_tags ();
49353 the_parser = NULL;
49355 finish_translation_unit ();
49358 /* Create an identifier for a generic parameter type (a synthesized
49359 template parameter implied by `auto' or a concept identifier). */
49361 static GTY(()) int generic_parm_count;
49362 static tree
49363 make_generic_type_name ()
49365 char buf[32];
49366 sprintf (buf, "auto:%d", ++generic_parm_count);
49367 return get_identifier (buf);
49370 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
49371 (creating a new template parameter list if necessary). Returns the newly
49372 created template type parm. */
49374 static tree
49375 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
49377 /* A requires-clause is not a function and cannot have placeholders. */
49378 if (current_binding_level->requires_expression)
49380 error ("placeholder type not allowed in this context");
49381 return error_mark_node;
49384 gcc_assert (current_binding_level->kind == sk_function_parms);
49386 /* We are either continuing a function template that already contains implicit
49387 template parameters, creating a new fully-implicit function template, or
49388 extending an existing explicit function template with implicit template
49389 parameters. */
49391 cp_binding_level *const entry_scope = current_binding_level;
49393 bool become_template = false;
49394 cp_binding_level *parent_scope = 0;
49396 if (parser->implicit_template_scope)
49398 gcc_assert (parser->implicit_template_parms);
49400 current_binding_level = parser->implicit_template_scope;
49402 else
49404 /* Roll back to the existing template parameter scope (in the case of
49405 extending an explicit function template) or introduce a new template
49406 parameter scope ahead of the function parameter scope (or class scope
49407 in the case of out-of-line member definitions). The function scope is
49408 added back after template parameter synthesis below. */
49410 cp_binding_level *scope = entry_scope;
49412 while (scope->kind == sk_function_parms)
49414 parent_scope = scope;
49415 scope = scope->level_chain;
49417 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
49419 /* If not defining a class, then any class scope is a scope level in
49420 an out-of-line member definition. In this case simply wind back
49421 beyond the first such scope to inject the template parameter list.
49422 Otherwise wind back to the class being defined. The latter can
49423 occur in class member friend declarations such as:
49425 class A {
49426 void foo (auto);
49428 class B {
49429 friend void A::foo (auto);
49432 The template parameter list synthesized for the friend declaration
49433 must be injected in the scope of 'B'. This can also occur in
49434 erroneous cases such as:
49436 struct A {
49437 struct B {
49438 void foo (auto);
49440 void B::foo (auto) {}
49443 Here the attempted definition of 'B::foo' within 'A' is ill-formed
49444 but, nevertheless, the template parameter list synthesized for the
49445 declarator should be injected into the scope of 'A' as if the
49446 ill-formed template was specified explicitly. */
49448 while (scope->kind == sk_class && !scope->defining_class_p)
49450 parent_scope = scope;
49451 scope = scope->level_chain;
49455 current_binding_level = scope;
49457 if (scope->kind != sk_template_parms
49458 || !function_being_declared_is_template_p (parser))
49460 /* Introduce a new template parameter list for implicit template
49461 parameters. */
49463 become_template = true;
49465 parser->implicit_template_scope
49466 = begin_scope (sk_template_parms, NULL);
49468 ++processing_template_decl;
49470 parser->fully_implicit_function_template_p = true;
49471 ++parser->num_template_parameter_lists;
49473 else
49475 /* Synthesize implicit template parameters at the end of the explicit
49476 template parameter list. */
49478 gcc_assert (current_template_parms);
49480 parser->implicit_template_scope = scope;
49482 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
49483 parser->implicit_template_parms
49484 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
49488 /* Synthesize a new template parameter and track the current template
49489 parameter chain with implicit_template_parms. */
49491 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
49492 tree synth_id = make_generic_type_name ();
49493 bool non_type = false;
49495 /* Synthesize the type template parameter. */
49496 gcc_assert(!proto || TREE_CODE (proto) == TYPE_DECL);
49497 tree synth_tmpl_parm = finish_template_type_parm (class_type_node, synth_id);
49499 if (become_template)
49500 current_template_parms = tree_cons (size_int (current_template_depth + 1),
49501 NULL_TREE, current_template_parms);
49503 /* Attach the constraint to the parm before processing. */
49504 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
49505 TREE_TYPE (node) = constr;
49506 tree new_parm
49507 = process_template_parm (parser->implicit_template_parms,
49508 input_location,
49509 node,
49510 /*non_type=*/non_type,
49511 /*param_pack=*/false);
49512 // Process_template_parm returns the list of parms, and
49513 // parser->implicit_template_parms holds the final node of the parm
49514 // list. We really want to manipulate the newly appended element.
49515 gcc_checking_assert (!parser->implicit_template_parms
49516 || parser->implicit_template_parms == new_parm);
49517 if (parser->implicit_template_parms)
49518 new_parm = TREE_CHAIN (new_parm);
49519 gcc_checking_assert (!TREE_CHAIN (new_parm));
49521 // Record the last implicit parm node
49522 parser->implicit_template_parms = new_parm;
49524 /* Mark the synthetic declaration "virtual". This is used when
49525 comparing template-heads to determine if whether an abbreviated
49526 function template is equivalent to an explicit template.
49528 Note that DECL_ARTIFICIAL is used elsewhere for template
49529 parameters. */
49530 if (TREE_VALUE (new_parm) != error_mark_node)
49531 DECL_VIRTUAL_P (TREE_VALUE (new_parm)) = true;
49533 tree new_decl = get_local_decls ();
49534 if (non_type)
49535 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
49536 new_decl = DECL_INITIAL (new_decl);
49538 /* If creating a fully implicit function template, start the new implicit
49539 template parameter list with this synthesized type, otherwise grow the
49540 current template parameter list. */
49542 if (become_template)
49544 parent_scope->level_chain = current_binding_level;
49546 tree new_parms = make_tree_vec (1);
49547 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
49548 TREE_VALUE (current_template_parms) = new_parms;
49550 else
49552 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
49553 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
49554 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
49555 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
49558 /* If the new parameter was constrained, we need to add that to the
49559 constraints in the template parameter list. */
49560 if (tree req = TEMPLATE_PARM_CONSTRAINTS (new_parm))
49562 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
49563 reqs = combine_constraint_expressions (reqs, req);
49564 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
49567 current_binding_level = entry_scope;
49569 return new_decl;
49572 /* Finish the declaration of a fully implicit function template. Such a
49573 template has no explicit template parameter list so has not been through the
49574 normal template head and tail processing. synthesize_implicit_template_parm
49575 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
49576 provided if the declaration is a class member such that its template
49577 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
49578 form is returned. Otherwise NULL_TREE is returned. */
49580 static tree
49581 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
49583 gcc_assert (parser->fully_implicit_function_template_p);
49585 if (member_decl_opt && member_decl_opt != error_mark_node
49586 && DECL_VIRTUAL_P (member_decl_opt))
49588 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
49589 "implicit templates may not be %<virtual%>");
49590 DECL_VIRTUAL_P (member_decl_opt) = false;
49593 if (member_decl_opt)
49594 member_decl_opt = finish_member_template_decl (member_decl_opt);
49595 end_template_decl ();
49597 parser->fully_implicit_function_template_p = false;
49598 parser->implicit_template_parms = 0;
49599 parser->implicit_template_scope = 0;
49600 --parser->num_template_parameter_lists;
49602 return member_decl_opt;
49605 /* Like finish_fully_implicit_template, but to be used in error
49606 recovery, rearranging scopes so that we restore the state we had
49607 before synthesize_implicit_template_parm inserted the implement
49608 template parms scope. */
49610 static void
49611 abort_fully_implicit_template (cp_parser *parser)
49613 cp_binding_level *return_to_scope = current_binding_level;
49615 if (parser->implicit_template_scope
49616 && return_to_scope != parser->implicit_template_scope)
49618 cp_binding_level *child = return_to_scope;
49619 for (cp_binding_level *scope = child->level_chain;
49620 scope != parser->implicit_template_scope;
49621 scope = child->level_chain)
49622 child = scope;
49623 child->level_chain = parser->implicit_template_scope->level_chain;
49624 parser->implicit_template_scope->level_chain = return_to_scope;
49625 current_binding_level = parser->implicit_template_scope;
49627 else
49628 return_to_scope = return_to_scope->level_chain;
49630 finish_fully_implicit_template (parser, NULL);
49632 gcc_assert (current_binding_level == return_to_scope);
49635 /* Helper function for diagnostics that have complained about things
49636 being used with 'extern "C"' linkage.
49638 Attempt to issue a note showing where the 'extern "C"' linkage began. */
49640 void
49641 maybe_show_extern_c_location (void)
49643 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
49644 inform (the_parser->innermost_linkage_specification_location,
49645 "%<extern \"C\"%> linkage started here");
49648 #include "gt-cp-parser.h"