c++: Fix up mangling of function/block scope static structured bindings [PR111069]
[official-gcc.git] / gcc / cp / parser.cc
blob7811d582b072a66e683dcf547e4437a20b5503fa
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2023 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);
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 static void check_omp_intervening_code
265 (cp_parser *);
268 /* Manifest constants. */
269 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
270 #define CP_SAVED_TOKEN_STACK 5
272 /* Variables. */
274 /* The stream to which debugging output should be written. */
275 static FILE *cp_lexer_debug_stream;
277 /* Nonzero if we are parsing an unevaluated operand: an operand to
278 sizeof, typeof, or alignof. */
279 int cp_unevaluated_operand;
281 /* Dump up to NUM tokens in BUFFER to FILE starting with token
282 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
283 first token in BUFFER. If NUM is 0, dump all the tokens. If
284 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
285 highlighted by surrounding it in [[ ]]. */
287 static void
288 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
289 cp_token *start_token, unsigned num,
290 cp_token *curr_token)
292 unsigned i, nprinted;
293 cp_token *token;
294 bool do_print;
296 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
298 if (buffer == NULL)
299 return;
301 if (num == 0)
302 num = buffer->length ();
304 if (start_token == NULL)
305 start_token = buffer->address ();
307 if (start_token > buffer->address ())
309 cp_lexer_print_token (file, &(*buffer)[0]);
310 fprintf (file, " ... ");
313 do_print = false;
314 nprinted = 0;
315 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
317 if (token == start_token)
318 do_print = true;
320 if (!do_print)
321 continue;
323 nprinted++;
324 if (token == curr_token)
325 fprintf (file, "[[");
327 cp_lexer_print_token (file, token);
329 if (token == curr_token)
330 fprintf (file, "]]");
332 switch (token->type)
334 case CPP_SEMICOLON:
335 case CPP_OPEN_BRACE:
336 case CPP_CLOSE_BRACE:
337 case CPP_EOF:
338 fputc ('\n', file);
339 break;
341 default:
342 fputc (' ', file);
346 if (i == num && i < buffer->length ())
348 fprintf (file, " ... ");
349 cp_lexer_print_token (file, &buffer->last ());
352 fprintf (file, "\n");
356 /* Dump all tokens in BUFFER to stderr. */
358 void
359 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
361 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
364 DEBUG_FUNCTION void
365 debug (vec<cp_token, va_gc> &ref)
367 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
370 DEBUG_FUNCTION void
371 debug (vec<cp_token, va_gc> *ptr)
373 if (ptr)
374 debug (*ptr);
375 else
376 fprintf (stderr, "<nil>\n");
380 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
381 description for T. */
383 static void
384 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
386 if (t)
388 fprintf (file, "%s: ", desc);
389 print_node_brief (file, "", t, 0);
394 /* Dump parser context C to FILE. */
396 static void
397 cp_debug_print_context (FILE *file, cp_parser_context *c)
399 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
400 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
401 print_node_brief (file, "", c->object_type, 0);
402 fprintf (file, "}\n");
406 /* Print the stack of parsing contexts to FILE starting with FIRST. */
408 static void
409 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
411 unsigned i;
412 cp_parser_context *c;
414 fprintf (file, "Parsing context stack:\n");
415 for (i = 0, c = first; c; c = c->next, i++)
417 fprintf (file, "\t#%u: ", i);
418 cp_debug_print_context (file, c);
423 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
425 static void
426 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
428 if (flag)
429 fprintf (file, "%s: true\n", desc);
433 /* Print an unparsed function entry UF to FILE. */
435 static void
436 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
438 unsigned i;
439 cp_default_arg_entry *default_arg_fn;
440 tree fn;
442 fprintf (file, "\tFunctions with default args:\n");
443 for (i = 0;
444 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
445 i++)
447 fprintf (file, "\t\tClass type: ");
448 print_node_brief (file, "", default_arg_fn->class_type, 0);
449 fprintf (file, "\t\tDeclaration: ");
450 print_node_brief (file, "", default_arg_fn->decl, 0);
451 fprintf (file, "\n");
454 fprintf (file, "\n\tFunctions with definitions that require "
455 "post-processing\n\t\t");
456 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
458 print_node_brief (file, "", fn, 0);
459 fprintf (file, " ");
461 fprintf (file, "\n");
463 fprintf (file, "\n\tNon-static data members with initializers that require "
464 "post-processing\n\t\t");
465 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
467 print_node_brief (file, "", fn, 0);
468 fprintf (file, " ");
470 fprintf (file, "\n");
474 /* Print the stack of unparsed member functions S to FILE. */
476 static void
477 cp_debug_print_unparsed_queues (FILE *file,
478 vec<cp_unparsed_functions_entry, va_gc> *s)
480 unsigned i;
481 cp_unparsed_functions_entry *uf;
483 fprintf (file, "Unparsed functions\n");
484 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
486 fprintf (file, "#%u:\n", i);
487 cp_debug_print_unparsed_function (file, uf);
492 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
493 the given PARSER. If FILE is NULL, the output is printed on stderr. */
495 static void
496 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
498 cp_token *next_token, *first_token, *start_token;
500 if (file == NULL)
501 file = stderr;
503 next_token = parser->lexer->next_token;
504 first_token = parser->lexer->buffer->address ();
505 start_token = (next_token > first_token + window_size / 2)
506 ? next_token - window_size / 2
507 : first_token;
508 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
509 next_token);
513 /* Dump debugging information for the given PARSER. If FILE is NULL,
514 the output is printed on stderr. */
516 void
517 cp_debug_parser (FILE *file, cp_parser *parser)
519 const size_t window_size = 20;
520 cp_token *token;
521 expanded_location eloc;
523 if (file == NULL)
524 file = stderr;
526 fprintf (file, "Parser state\n\n");
527 fprintf (file, "Number of tokens: %u\n",
528 vec_safe_length (parser->lexer->buffer));
529 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
530 cp_debug_print_tree_if_set (file, "Object scope",
531 parser->object_scope);
532 cp_debug_print_tree_if_set (file, "Qualifying scope",
533 parser->qualifying_scope);
534 cp_debug_print_context_stack (file, parser->context);
535 cp_debug_print_flag (file, "Allow GNU extensions",
536 parser->allow_gnu_extensions_p);
537 cp_debug_print_flag (file, "'>' token is greater-than",
538 parser->greater_than_is_operator_p);
539 cp_debug_print_flag (file, "Default args allowed in current "
540 "parameter list", parser->default_arg_ok_p);
541 cp_debug_print_flag (file, "Parsing integral constant-expression",
542 parser->integral_constant_expression_p);
543 cp_debug_print_flag (file, "Allow non-constant expression in current "
544 "constant-expression",
545 parser->allow_non_integral_constant_expression_p);
546 cp_debug_print_flag (file, "Seen non-constant expression",
547 parser->non_integral_constant_expression_p);
548 cp_debug_print_flag (file, "Local names forbidden in current context",
549 (parser->local_variables_forbidden_p
550 & LOCAL_VARS_FORBIDDEN));
551 cp_debug_print_flag (file, "'this' forbidden in current context",
552 (parser->local_variables_forbidden_p
553 & THIS_FORBIDDEN));
554 cp_debug_print_flag (file, "In unbraced linkage specification",
555 parser->in_unbraced_linkage_specification_p);
556 cp_debug_print_flag (file, "Parsing a declarator",
557 parser->in_declarator_p);
558 cp_debug_print_flag (file, "In template argument list",
559 parser->in_template_argument_list_p);
560 cp_debug_print_flag (file, "Parsing an iteration statement",
561 parser->in_statement & IN_ITERATION_STMT);
562 cp_debug_print_flag (file, "Parsing a switch statement",
563 parser->in_statement & IN_SWITCH_STMT);
564 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
565 parser->in_statement & IN_OMP_BLOCK);
566 cp_debug_print_flag (file, "Parsing an OpenMP loop",
567 parser->in_statement & IN_OMP_FOR);
568 cp_debug_print_flag (file, "Parsing an if statement",
569 parser->in_statement & IN_IF_STMT);
570 cp_debug_print_flag (file, "Parsing a type-id in an expression "
571 "context", parser->in_type_id_in_expr_p);
572 cp_debug_print_flag (file, "String expressions should be translated "
573 "to execution character set",
574 parser->translate_strings_p);
575 cp_debug_print_flag (file, "Parsing function body outside of a "
576 "local class", parser->in_function_body);
577 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
578 parser->colon_corrects_to_scope_p);
579 cp_debug_print_flag (file, "Colon doesn't start a class definition",
580 parser->colon_doesnt_start_class_def_p);
581 cp_debug_print_flag (file, "Parsing an Objective-C++ message context",
582 parser->objective_c_message_context_p);
583 if (parser->type_definition_forbidden_message)
584 fprintf (file, "Error message for forbidden type definitions: %s %s\n",
585 parser->type_definition_forbidden_message,
586 parser->type_definition_forbidden_message_arg
587 ? parser->type_definition_forbidden_message_arg : "<none>");
588 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
589 fprintf (file, "Number of class definitions in progress: %u\n",
590 parser->num_classes_being_defined);
591 fprintf (file, "Number of template parameter lists for the current "
592 "declaration: %u\n", parser->num_template_parameter_lists);
593 cp_debug_parser_tokens (file, parser, window_size);
594 token = parser->lexer->next_token;
595 fprintf (file, "Next token to parse:\n");
596 fprintf (file, "\tToken: ");
597 cp_lexer_print_token (file, token);
598 eloc = expand_location (token->location);
599 fprintf (file, "\n\tFile: %s\n", eloc.file);
600 fprintf (file, "\tLine: %d\n", eloc.line);
601 fprintf (file, "\tColumn: %d\n", eloc.column);
604 DEBUG_FUNCTION void
605 debug (cp_parser &ref)
607 cp_debug_parser (stderr, &ref);
610 DEBUG_FUNCTION void
611 debug (cp_parser *ptr)
613 if (ptr)
614 debug (*ptr);
615 else
616 fprintf (stderr, "<nil>\n");
619 /* Allocate memory for a new lexer object and return it. */
621 static cp_lexer *
622 cp_lexer_alloc (void)
624 /* Allocate the memory. */
625 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
627 /* Initially we are not debugging. */
628 lexer->debugging_p = false;
630 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
632 /* Create the buffer. */
633 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
635 return lexer;
638 /* Return TRUE if token is the start of a module declaration that will be
639 terminated by a CPP_PRAGMA_EOL token. */
640 static inline bool
641 cp_token_is_module_directive (cp_token *token)
643 return token->keyword == RID__EXPORT
644 || token->keyword == RID__MODULE
645 || token->keyword == RID__IMPORT;
648 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
649 PRAGMA_NONE. */
651 static enum pragma_kind
652 cp_parser_pragma_kind (cp_token *token)
654 if (token->type != CPP_PRAGMA)
655 return PRAGMA_NONE;
656 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
657 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
660 /* Handle early pragmas such as #pragma GCC diagnostic, which needs to be done
661 during preprocessing for the case of preprocessing-related diagnostics. This
662 is called immediately after pushing the CPP_PRAGMA_EOL token onto
663 lexer->buffer. */
665 static void
666 cp_lexer_handle_early_pragma (cp_lexer *lexer)
668 const auto first_token = lexer->buffer->address ();
669 const auto last_token = first_token + lexer->buffer->length () - 1;
671 /* Back up to the start of the pragma so pragma_lex () can parse it when
672 c-pragma lib asks it to. */
673 auto begin = last_token;
674 gcc_assert (begin->type == CPP_PRAGMA_EOL);
675 while (begin->type != CPP_PRAGMA)
677 if (cp_token_is_module_directive (begin))
678 return;
679 gcc_assert (begin != first_token);
680 --begin;
682 gcc_assert (!lexer->next_token);
683 gcc_assert (!lexer->last_token);
684 lexer->next_token = begin;
685 lexer->last_token = last_token;
687 /* Dispatch it. */
688 const unsigned int id
689 = cp_parser_pragma_kind (cp_lexer_consume_token (lexer));
690 if (id >= PRAGMA_FIRST_EXTERNAL)
691 c_invoke_early_pragma_handler (id);
693 /* Reset to normal state. */
694 lexer->next_token = lexer->last_token = nullptr;
697 /* The parser. */
698 static cp_parser *cp_parser_new (cp_lexer *);
699 static GTY (()) cp_parser *the_parser;
701 /* Create a new main C++ lexer, the lexer that gets tokens from the
702 preprocessor, and also create the main parser. */
704 static cp_lexer *
705 cp_lexer_new_main (void)
707 cp_token token;
709 /* It's possible that parsing the first pragma will load a PCH file,
710 which is a GC collection point. So we have to do that before
711 allocating any memory. */
712 cp_lexer_get_preprocessor_token (C_LEX_STRING_NO_JOIN, &token);
713 cp_parser_initial_pragma (&token);
714 c_common_no_more_pch ();
716 cp_lexer *lexer = cp_lexer_alloc ();
717 /* Put the first token in the buffer. */
718 cp_token *tok = lexer->buffer->quick_push (token);
720 uintptr_t filter = 0;
721 if (modules_p ())
722 filter = module_token_cdtor (parse_in, filter);
724 /* Create the parser now, so we can use it to handle early pragmas. */
725 gcc_assert (!the_parser);
726 the_parser = cp_parser_new (lexer);
728 /* Get the remaining tokens from the preprocessor. */
729 while (tok->type != CPP_EOF)
731 if (filter)
732 /* Process the previous token. */
733 module_token_lang (tok->type, tok->keyword, tok->u.value,
734 tok->location, filter);
736 /* Check for early pragmas that need to be handled now. */
737 if (tok->type == CPP_PRAGMA_EOL)
738 cp_lexer_handle_early_pragma (lexer);
740 tok = vec_safe_push (lexer->buffer, cp_token ());
741 cp_lexer_get_preprocessor_token (C_LEX_STRING_NO_JOIN, tok);
744 lexer->next_token = lexer->buffer->address ();
745 lexer->last_token = lexer->next_token
746 + lexer->buffer->length ()
747 - 1;
749 if (lexer->buffer->length () != 1)
751 /* Set the EOF token's location to be the just after the previous
752 token's range. That way 'at-eof' diagnostics point at something
753 meaninful. */
754 auto range = get_range_from_loc (line_table, tok[-1].location);
755 tok[0].location
756 = linemap_position_for_loc_and_offset (line_table, range.m_finish, 1);
759 if (filter)
760 module_token_cdtor (parse_in, filter);
762 /* Subsequent preprocessor diagnostics should use compiler
763 diagnostic functions to get the compiler source location. */
764 override_libcpp_locations = true;
766 maybe_check_all_macros (parse_in);
768 gcc_assert (!lexer->next_token->purged_p);
769 return lexer;
772 /* Create a lexer and parser to be used during preprocess-only mode.
773 This will be filled with tokens to parse when needed by pragma_lex (). */
774 void
775 c_init_preprocess ()
777 gcc_assert (!the_parser);
778 the_parser = cp_parser_new (cp_lexer_alloc ());
781 /* Create a new lexer whose token stream is primed with the tokens in
782 CACHE. When these tokens are exhausted, no new tokens will be read. */
784 static cp_lexer *
785 cp_lexer_new_from_tokens (cp_token_cache *cache)
787 cp_token *first = cache->first;
788 cp_token *last = cache->last;
789 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
791 /* We do not own the buffer. */
792 lexer->buffer = NULL;
794 /* Insert an EOF token. */
795 lexer->saved_type = last->type;
796 lexer->saved_keyword = last->keyword;
797 last->type = CPP_EOF;
798 last->keyword = RID_MAX;
800 lexer->next_token = first;
801 lexer->last_token = last;
803 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
805 /* Initially we are not debugging. */
806 lexer->debugging_p = false;
808 gcc_assert (!lexer->next_token->purged_p
809 && !lexer->last_token->purged_p);
810 return lexer;
813 /* Frees all resources associated with LEXER. */
815 static void
816 cp_lexer_destroy (cp_lexer *lexer)
818 if (lexer->buffer)
819 vec_free (lexer->buffer);
820 else
822 /* Restore the token we overwrite with EOF. */
823 lexer->last_token->type = lexer->saved_type;
824 lexer->last_token->keyword = lexer->saved_keyword;
826 lexer->saved_tokens.release ();
827 ggc_free (lexer);
830 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
831 be used. The point of this flag is to help the compiler to fold away calls
832 to cp_lexer_debugging_p within this source file at compile time, when the
833 lexer is not being debugged. */
835 #define LEXER_DEBUGGING_ENABLED_P false
837 /* Returns nonzero if debugging information should be output. */
839 static inline bool
840 cp_lexer_debugging_p (cp_lexer *lexer)
842 if (!LEXER_DEBUGGING_ENABLED_P)
843 return false;
845 return lexer->debugging_p;
849 static inline cp_token_position
850 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
852 return lexer->next_token - previous_p;
855 static inline cp_token *
856 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
858 return pos;
861 static inline void
862 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
864 lexer->next_token = cp_lexer_token_at (lexer, pos);
867 static inline cp_token_position
868 cp_lexer_previous_token_position (cp_lexer *lexer)
870 return cp_lexer_token_position (lexer, true);
873 static inline cp_token *
874 cp_lexer_previous_token (cp_lexer *lexer)
876 cp_token_position tp = cp_lexer_previous_token_position (lexer);
878 /* Skip past purged tokens. */
879 while (tp->purged_p)
881 gcc_assert (tp != vec_safe_address (lexer->buffer));
882 tp--;
885 return cp_lexer_token_at (lexer, tp);
888 /* Same as above, but return NULL when the lexer doesn't own the token
889 buffer or if the next_token is at the start of the token
890 vector or if all previous tokens are purged. */
892 static cp_token *
893 cp_lexer_safe_previous_token (cp_lexer *lexer)
895 if (lexer->buffer
896 && lexer->next_token != lexer->buffer->address ())
898 cp_token_position tp = cp_lexer_previous_token_position (lexer);
900 /* Skip past purged tokens. */
901 while (tp->purged_p)
903 if (tp == lexer->buffer->address ())
904 return NULL;
905 tp--;
907 return cp_lexer_token_at (lexer, tp);
910 return NULL;
913 /* Overload for make_location, taking the lexer to mean the location of the
914 previous token. */
916 static inline location_t
917 make_location (location_t caret, location_t start, cp_lexer *lexer)
919 cp_token *t = cp_lexer_previous_token (lexer);
920 return make_location (caret, start, t->location);
923 /* Overload for make_location taking tokens instead of locations. */
925 static inline location_t
926 make_location (cp_token *caret, cp_token *start, cp_token *end)
928 return make_location (caret->location, start->location, end->location);
931 /* nonzero if we are presently saving tokens. */
933 static inline int
934 cp_lexer_saving_tokens (const cp_lexer* lexer)
936 return lexer->saved_tokens.length () != 0;
939 /* Store the next token from the preprocessor in *TOKEN. Return true
940 if we reach EOF. If LEXER is NULL, assume we are handling an
941 initial #pragma pch_preprocess, and thus want the lexer to return
942 processed strings.
944 Diagnostics issued from this function must have their controlling option (if
945 any) in c.opt annotated as a libcpp option via the CppReason property. */
947 static void
948 cp_lexer_get_preprocessor_token (unsigned flags, cp_token *token)
950 static int is_extern_c = 0;
952 /* Get a new token from the preprocessor. */
953 token->type
954 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
955 flags);
956 token->keyword = RID_MAX;
957 token->purged_p = false;
958 token->error_reported = false;
959 token->tree_check_p = false;
960 /* Usually never see a zero, but just in case ... */
961 token->main_source_p = line_table->depth <= 1;
963 /* On some systems, some header files are surrounded by an
964 implicit extern "C" block. Set a flag in the token if it
965 comes from such a header. */
966 is_extern_c += pending_lang_change;
967 pending_lang_change = 0;
968 token->implicit_extern_c = is_extern_c > 0;
970 /* Check to see if this token is a keyword. */
971 if (token->type == CPP_NAME)
973 if (IDENTIFIER_KEYWORD_P (token->u.value))
975 /* Mark this token as a keyword. */
976 token->type = CPP_KEYWORD;
977 /* Record which keyword. */
978 token->keyword = C_RID_CODE (token->u.value);
980 else
982 if (warn_cxx11_compat
983 && ((C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
984 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
985 /* These are outside the CXX11 range. */
986 || C_RID_CODE (token->u.value) == RID_ALIGNOF
987 || C_RID_CODE (token->u.value) == RID_ALIGNAS
988 || C_RID_CODE (token->u.value)== RID_THREAD))
990 /* Warn about the C++11 keyword (but still treat it as
991 an identifier). */
992 warning_at (token->location, OPT_Wc__11_compat,
993 "identifier %qE is a keyword in C++11",
994 token->u.value);
996 /* Clear out the C_RID_CODE so we don't warn about this
997 particular identifier-turned-keyword again. */
998 C_SET_RID_CODE (token->u.value, RID_MAX);
1000 if (warn_cxx20_compat
1001 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX20
1002 && C_RID_CODE (token->u.value) <= RID_LAST_CXX20)
1004 /* Warn about the C++20 keyword (but still treat it as
1005 an identifier). */
1006 warning_at (token->location, OPT_Wc__20_compat,
1007 "identifier %qE is a keyword in C++20",
1008 token->u.value);
1010 /* Clear out the C_RID_CODE so we don't warn about this
1011 particular identifier-turned-keyword again. */
1012 C_SET_RID_CODE (token->u.value, RID_MAX);
1015 token->keyword = RID_MAX;
1018 else if (token->type == CPP_AT_NAME)
1020 /* This only happens in Objective-C++; it must be a keyword. */
1021 token->type = CPP_KEYWORD;
1022 switch (C_RID_CODE (token->u.value))
1024 /* Replace 'class' with '@class', 'private' with '@private',
1025 etc. This prevents confusion with the C++ keyword
1026 'class', and makes the tokens consistent with other
1027 Objective-C 'AT' keywords. For example '@class' is
1028 reported as RID_AT_CLASS which is consistent with
1029 '@synchronized', which is reported as
1030 RID_AT_SYNCHRONIZED.
1032 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
1033 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
1034 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
1035 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
1036 case RID_THROW: token->keyword = RID_AT_THROW; break;
1037 case RID_TRY: token->keyword = RID_AT_TRY; break;
1038 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
1039 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
1040 default: token->keyword = C_RID_CODE (token->u.value);
1045 /* Update the globals input_location and the input file stack from TOKEN. */
1046 static inline void
1047 cp_lexer_set_source_position_from_token (cp_token *token)
1049 input_location = token->location;
1052 /* Update the globals input_location and the input file stack from LEXER. */
1053 static inline void
1054 cp_lexer_set_source_position (cp_lexer *lexer)
1056 cp_token *token = cp_lexer_peek_token (lexer);
1057 cp_lexer_set_source_position_from_token (token);
1060 /* Return a pointer to the next token in the token stream, but do not
1061 consume it. */
1063 static inline cp_token *
1064 cp_lexer_peek_token (cp_lexer *lexer)
1066 if (cp_lexer_debugging_p (lexer))
1068 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
1069 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
1070 putc ('\n', cp_lexer_debug_stream);
1072 return lexer->next_token;
1075 /* Return true if the next token has the indicated TYPE. */
1077 static inline bool
1078 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
1080 return cp_lexer_peek_token (lexer)->type == type;
1083 /* Return true if the next token does not have the indicated TYPE. */
1085 static inline bool
1086 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
1088 return !cp_lexer_next_token_is (lexer, type);
1091 /* Return true if the next token is the indicated KEYWORD. */
1093 static inline bool
1094 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
1096 return cp_lexer_peek_token (lexer)->keyword == keyword;
1099 static inline bool
1100 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
1102 return cp_lexer_peek_nth_token (lexer, n)->type == type;
1105 static inline bool
1106 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
1108 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
1111 /* Return true if KEYWORD can start a decl-specifier. */
1113 bool
1114 cp_keyword_starts_decl_specifier_p (enum rid keyword)
1116 switch (keyword)
1118 /* auto specifier: storage-class-specifier in C++,
1119 simple-type-specifier in C++0x. */
1120 case RID_AUTO:
1121 /* Storage classes. */
1122 case RID_REGISTER:
1123 case RID_STATIC:
1124 case RID_EXTERN:
1125 case RID_MUTABLE:
1126 case RID_THREAD:
1127 /* Elaborated type specifiers. */
1128 case RID_ENUM:
1129 case RID_CLASS:
1130 case RID_STRUCT:
1131 case RID_UNION:
1132 case RID_TYPENAME:
1133 /* Simple type specifiers. */
1134 case RID_CHAR:
1135 case RID_CHAR8:
1136 case RID_CHAR16:
1137 case RID_CHAR32:
1138 case RID_WCHAR:
1139 case RID_BOOL:
1140 case RID_SHORT:
1141 case RID_INT:
1142 case RID_LONG:
1143 case RID_SIGNED:
1144 case RID_UNSIGNED:
1145 case RID_FLOAT:
1146 case RID_DOUBLE:
1147 CASE_RID_FLOATN_NX:
1148 case RID_VOID:
1149 /* CV qualifiers. */
1150 case RID_CONST:
1151 case RID_VOLATILE:
1152 /* Function specifiers. */
1153 case RID_EXPLICIT:
1154 case RID_VIRTUAL:
1155 /* friend/typdef/inline specifiers. */
1156 case RID_FRIEND:
1157 case RID_TYPEDEF:
1158 case RID_INLINE:
1159 /* GNU extensions. */
1160 case RID_TYPEOF:
1161 /* C++11 extensions. */
1162 case RID_DECLTYPE:
1163 case RID_CONSTEXPR:
1164 /* C++20 extensions. */
1165 case RID_CONSTINIT:
1166 case RID_CONSTEVAL:
1167 return true;
1169 #define DEFTRAIT_TYPE(CODE, NAME, ARITY) \
1170 case RID_##CODE:
1171 #include "cp-trait.def"
1172 #undef DEFTRAIT_TYPE
1173 return true;
1175 default:
1176 if (keyword >= RID_FIRST_INT_N
1177 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
1178 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
1179 return true;
1180 return false;
1184 /* Return true if the next token is a keyword for a decl-specifier. */
1186 static bool
1187 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
1189 cp_token *token;
1191 token = cp_lexer_peek_token (lexer);
1192 return cp_keyword_starts_decl_specifier_p (token->keyword);
1195 /* Returns TRUE iff the token T begins a decltype type. */
1197 static bool
1198 token_is_decltype (cp_token *t)
1200 return (t->keyword == RID_DECLTYPE
1201 || t->type == CPP_DECLTYPE);
1204 /* Returns TRUE iff the next token begins a decltype type. */
1206 static bool
1207 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1209 cp_token *t = cp_lexer_peek_token (lexer);
1210 return token_is_decltype (t);
1213 /* Called when processing a token with tree_check_value; perform or defer the
1214 associated checks and return the value. */
1216 static tree
1217 saved_checks_value (struct tree_check *check_value)
1219 /* Perform any access checks that were deferred. */
1220 vec<deferred_access_check, va_gc> *checks;
1221 deferred_access_check *chk;
1222 checks = check_value->checks;
1223 if (checks)
1225 int i;
1226 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1227 perform_or_defer_access_check (chk->binfo,
1228 chk->decl,
1229 chk->diag_decl, tf_warning_or_error);
1231 /* Return the stored value. */
1232 return check_value->value;
1235 /* Return a pointer to the Nth token in the token stream. If N is 1,
1236 then this is precisely equivalent to cp_lexer_peek_token (except
1237 that it is not inline). One would like to disallow that case, but
1238 there is one case (cp_parser_nth_token_starts_template_id) where
1239 the caller passes a variable for N and it might be 1. */
1241 static cp_token *
1242 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1244 cp_token *token;
1246 /* N is 1-based, not zero-based. */
1247 gcc_assert (n > 0);
1249 if (cp_lexer_debugging_p (lexer))
1250 fprintf (cp_lexer_debug_stream,
1251 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1253 --n;
1254 token = lexer->next_token;
1255 while (n && token->type != CPP_EOF)
1257 ++token;
1258 if (!token->purged_p)
1259 --n;
1262 if (cp_lexer_debugging_p (lexer))
1264 cp_lexer_print_token (cp_lexer_debug_stream, token);
1265 putc ('\n', cp_lexer_debug_stream);
1268 return token;
1271 /* Return the next token, and advance the lexer's next_token pointer
1272 to point to the next non-purged token. */
1274 static cp_token *
1275 cp_lexer_consume_token (cp_lexer* lexer)
1277 cp_token *token = lexer->next_token;
1281 gcc_assert (token->type != CPP_EOF);
1282 lexer->next_token++;
1284 while (lexer->next_token->purged_p);
1286 cp_lexer_set_source_position_from_token (token);
1288 /* Provide debugging output. */
1289 if (cp_lexer_debugging_p (lexer))
1291 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1292 cp_lexer_print_token (cp_lexer_debug_stream, token);
1293 putc ('\n', cp_lexer_debug_stream);
1296 return token;
1299 /* Permanently remove the next token from the token stream, and
1300 advance the next_token pointer to refer to the next non-purged
1301 token. */
1303 static void
1304 cp_lexer_purge_token (cp_lexer *lexer)
1306 cp_token *tok = lexer->next_token;
1308 gcc_assert (tok->type != CPP_EOF);
1309 tok->purged_p = true;
1310 tok->location = UNKNOWN_LOCATION;
1311 tok->u.value = NULL_TREE;
1312 tok->keyword = RID_MAX;
1315 tok++;
1316 while (tok->purged_p);
1317 lexer->next_token = tok;
1320 /* Permanently remove all tokens after TOK, up to, but not
1321 including, the token that will be returned next by
1322 cp_lexer_peek_token. */
1324 static void
1325 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1327 cp_token *peek = lexer->next_token;
1329 gcc_assert (tok < peek);
1331 for (tok++; tok != peek; tok++)
1333 tok->purged_p = true;
1334 tok->location = UNKNOWN_LOCATION;
1335 tok->u.value = NULL_TREE;
1336 tok->keyword = RID_MAX;
1340 /* Begin saving tokens. All tokens consumed after this point will be
1341 preserved. */
1343 static void
1344 cp_lexer_save_tokens (cp_lexer* lexer)
1346 /* Provide debugging output. */
1347 if (cp_lexer_debugging_p (lexer))
1348 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1350 lexer->saved_tokens.safe_push (lexer->next_token);
1353 /* Commit to the portion of the token stream most recently saved. */
1355 static void
1356 cp_lexer_commit_tokens (cp_lexer* lexer)
1358 /* Provide debugging output. */
1359 if (cp_lexer_debugging_p (lexer))
1360 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1362 lexer->saved_tokens.pop ();
1365 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1366 to the token stream. Stop saving tokens. */
1368 static void
1369 cp_lexer_rollback_tokens (cp_lexer* lexer)
1371 /* Provide debugging output. */
1372 if (cp_lexer_debugging_p (lexer))
1373 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1375 lexer->next_token = lexer->saved_tokens.pop ();
1378 /* Determines what saved_token_sentinel does when going out of scope. */
1380 enum saved_token_sentinel_mode {
1381 STS_COMMIT,
1382 STS_ROLLBACK,
1383 STS_DONOTHING
1386 /* RAII wrapper around the above functions, with sanity checking (the token
1387 stream should be the same at the point of instantiation as it is at the
1388 point of destruction).
1390 Creating a variable saves tokens. MODE determines what happens when the
1391 object is destroyed. STS_COMMIT commits tokens (default),
1392 STS_ROLLBACK rolls-back and STS_DONOTHING does nothing. Calling
1393 rollback() will immediately roll-back tokens and set MODE to
1394 STS_DONOTHING. */
1396 struct saved_token_sentinel
1398 cp_lexer *lexer;
1399 unsigned len;
1400 saved_token_sentinel_mode mode;
1401 saved_token_sentinel (cp_lexer *_lexer,
1402 saved_token_sentinel_mode _mode = STS_COMMIT)
1403 : lexer (_lexer), mode (_mode)
1405 len = lexer->saved_tokens.length ();
1406 cp_lexer_save_tokens (lexer);
1408 void rollback ()
1410 cp_lexer_rollback_tokens (lexer);
1411 cp_lexer_set_source_position_from_token
1412 (cp_lexer_previous_token (lexer));
1413 mode = STS_DONOTHING;
1415 ~saved_token_sentinel ()
1417 if (mode == STS_COMMIT)
1418 cp_lexer_commit_tokens (lexer);
1419 else if (mode == STS_ROLLBACK)
1420 rollback ();
1422 gcc_assert (lexer->saved_tokens.length () == len);
1426 /* Print a representation of the TOKEN on the STREAM. */
1428 static void
1429 cp_lexer_print_token (FILE * stream, cp_token *token)
1431 /* We don't use cpp_type2name here because the parser defines
1432 a few tokens of its own. */
1433 static const char *const token_names[] = {
1434 /* cpplib-defined token types */
1435 #define OP(e, s) #e,
1436 #define TK(e, s) #e,
1437 TTYPE_TABLE
1438 #undef OP
1439 #undef TK
1440 /* C++ parser token types - see "Manifest constants", above. */
1441 "KEYWORD",
1442 "TEMPLATE_ID",
1443 "NESTED_NAME_SPECIFIER",
1446 /* For some tokens, print the associated data. */
1447 switch (token->type)
1449 case CPP_KEYWORD:
1450 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1451 For example, `struct' is mapped to an INTEGER_CST. */
1452 if (!identifier_p (token->u.value))
1453 break;
1454 /* fall through */
1455 case CPP_NAME:
1456 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1457 break;
1459 case CPP_STRING:
1460 case CPP_STRING16:
1461 case CPP_STRING32:
1462 case CPP_WSTRING:
1463 case CPP_UTF8STRING:
1464 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1465 break;
1467 case CPP_NUMBER:
1468 print_generic_expr (stream, token->u.value);
1469 break;
1471 default:
1472 /* If we have a name for the token, print it out. Otherwise, we
1473 simply give the numeric code. */
1474 if (token->type < ARRAY_SIZE(token_names))
1475 fputs (token_names[token->type], stream);
1476 else
1477 fprintf (stream, "[%d]", token->type);
1478 break;
1482 DEBUG_FUNCTION void
1483 debug (cp_token &ref)
1485 cp_lexer_print_token (stderr, &ref);
1486 fprintf (stderr, "\n");
1489 DEBUG_FUNCTION void
1490 debug (cp_token *ptr)
1492 if (ptr)
1493 debug (*ptr);
1494 else
1495 fprintf (stderr, "<nil>\n");
1499 /* Start emitting debugging information. */
1501 static void
1502 cp_lexer_start_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 = true;
1509 cp_lexer_debug_stream = stderr;
1512 /* Stop emitting debugging information. */
1514 static void
1515 cp_lexer_stop_debugging (cp_lexer* lexer)
1517 if (!LEXER_DEBUGGING_ENABLED_P)
1518 fatal_error (input_location,
1519 "%<LEXER_DEBUGGING_ENABLED_P%> is not set to true");
1521 lexer->debugging_p = false;
1522 cp_lexer_debug_stream = NULL;
1525 /* Create a new cp_token_cache, representing a range of tokens. */
1527 static cp_token_cache *
1528 cp_token_cache_new (cp_token *first, cp_token *last)
1530 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1531 cache->first = first;
1532 cache->last = last;
1533 return cache;
1536 /* Diagnose if #pragma omp declare simd isn't followed immediately
1537 by function declaration or definition. */
1539 static inline void
1540 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1542 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1544 error ("%<#pragma omp declare %s%> not immediately followed by "
1545 "function declaration or definition",
1546 parser->omp_declare_simd->variant_p ? "variant" : "simd");
1547 parser->omp_declare_simd = NULL;
1551 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1552 and put that into "omp declare simd" attribute. */
1554 static inline void
1555 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1557 if (UNLIKELY (parser->omp_declare_simd != NULL))
1559 if (fndecl == error_mark_node)
1561 parser->omp_declare_simd = NULL;
1562 return;
1564 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1566 cp_ensure_no_omp_declare_simd (parser);
1567 return;
1572 /* Similarly, but for use in declaration parsing functions
1573 which call cp_parser_handle_directive_omp_attributes. */
1575 static inline void
1576 cp_finalize_omp_declare_simd (cp_parser *parser, cp_omp_declare_simd_data *data)
1578 if (parser->omp_declare_simd != data)
1579 return;
1581 if (!parser->omp_declare_simd->error_seen
1582 && !parser->omp_declare_simd->fndecl_seen)
1583 error_at (parser->omp_declare_simd->loc,
1584 "%<declare %s%> directive not immediately followed by "
1585 "function declaration or definition",
1586 parser->omp_declare_simd->variant_p ? "variant" : "simd");
1587 parser->omp_declare_simd = NULL;
1590 /* Diagnose if #pragma acc routine isn't followed immediately by function
1591 declaration or definition. */
1593 static inline void
1594 cp_ensure_no_oacc_routine (cp_parser *parser)
1596 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1598 error_at (parser->oacc_routine->loc,
1599 "%<#pragma acc routine%> not immediately followed by "
1600 "function declaration or definition");
1601 parser->oacc_routine = NULL;
1605 /* Decl-specifiers. */
1607 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1609 static void
1610 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1612 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1615 /* Declarators. */
1617 /* Nothing other than the parser should be creating declarators;
1618 declarators are a semi-syntactic representation of C++ entities.
1619 Other parts of the front end that need to create entities (like
1620 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1622 static cp_declarator *make_call_declarator
1623 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier,
1624 tree, tree, tree, tree, tree, location_t);
1625 static cp_declarator *make_array_declarator
1626 (cp_declarator *, tree);
1627 static cp_declarator *make_pointer_declarator
1628 (cp_cv_quals, cp_declarator *, tree);
1629 static cp_declarator *make_reference_declarator
1630 (cp_cv_quals, cp_declarator *, bool, tree);
1631 static cp_declarator *make_ptrmem_declarator
1632 (cp_cv_quals, tree, cp_declarator *, tree);
1634 /* An erroneous declarator. */
1635 static cp_declarator *cp_error_declarator;
1637 /* The obstack on which declarators and related data structures are
1638 allocated. */
1639 static struct obstack declarator_obstack;
1641 /* Alloc BYTES from the declarator memory pool. */
1643 static inline void *
1644 alloc_declarator (size_t bytes)
1646 return obstack_alloc (&declarator_obstack, bytes);
1649 /* Allocate a declarator of the indicated KIND. Clear fields that are
1650 common to all declarators. */
1652 static cp_declarator *
1653 make_declarator (cp_declarator_kind kind)
1655 cp_declarator *declarator;
1657 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1658 declarator->kind = kind;
1659 declarator->parenthesized = UNKNOWN_LOCATION;
1660 declarator->attributes = NULL_TREE;
1661 declarator->std_attributes = NULL_TREE;
1662 declarator->declarator = NULL;
1663 declarator->parameter_pack_p = false;
1664 declarator->id_loc = UNKNOWN_LOCATION;
1665 declarator->init_loc = UNKNOWN_LOCATION;
1667 return declarator;
1670 /* Make a declarator for a generalized identifier. If
1671 QUALIFYING_SCOPE is non-NULL, the identifier is
1672 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1673 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1674 is, if any. */
1676 static cp_declarator *
1677 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1678 special_function_kind sfk, location_t id_location)
1680 cp_declarator *declarator;
1682 /* It is valid to write:
1684 class C { void f(); };
1685 typedef C D;
1686 void D::f();
1688 The standard is not clear about whether `typedef const C D' is
1689 legal; as of 2002-09-15 the committee is considering that
1690 question. EDG 3.0 allows that syntax. Therefore, we do as
1691 well. */
1692 if (qualifying_scope && TYPE_P (qualifying_scope))
1693 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1695 gcc_assert (identifier_p (unqualified_name)
1696 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1697 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1699 declarator = make_declarator (cdk_id);
1700 declarator->u.id.qualifying_scope = qualifying_scope;
1701 declarator->u.id.unqualified_name = unqualified_name;
1702 declarator->u.id.sfk = sfk;
1703 declarator->id_loc = id_location;
1705 return declarator;
1708 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1709 of modifiers such as const or volatile to apply to the pointer
1710 type, represented as identifiers. ATTRIBUTES represent the attributes that
1711 appertain to the pointer or reference. */
1713 cp_declarator *
1714 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1715 tree attributes)
1717 cp_declarator *declarator;
1719 declarator = make_declarator (cdk_pointer);
1720 declarator->declarator = target;
1721 declarator->u.pointer.qualifiers = cv_qualifiers;
1722 declarator->u.pointer.class_type = NULL_TREE;
1723 if (target)
1725 declarator->id_loc = target->id_loc;
1726 declarator->parameter_pack_p = target->parameter_pack_p;
1727 target->parameter_pack_p = false;
1729 else
1730 declarator->parameter_pack_p = false;
1732 declarator->std_attributes = attributes;
1734 return declarator;
1737 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1738 represent the attributes that appertain to the pointer or
1739 reference. */
1741 cp_declarator *
1742 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1743 bool rvalue_ref, tree attributes)
1745 cp_declarator *declarator;
1747 declarator = make_declarator (cdk_reference);
1748 declarator->declarator = target;
1749 declarator->u.reference.qualifiers = cv_qualifiers;
1750 declarator->u.reference.rvalue_ref = rvalue_ref;
1751 if (target)
1753 declarator->id_loc = target->id_loc;
1754 declarator->parameter_pack_p = target->parameter_pack_p;
1755 target->parameter_pack_p = false;
1757 else
1758 declarator->parameter_pack_p = false;
1760 declarator->std_attributes = attributes;
1762 return declarator;
1765 /* Like make_pointer_declarator -- but for a pointer to a non-static
1766 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1767 appertain to the pointer or reference. */
1769 cp_declarator *
1770 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1771 cp_declarator *pointee,
1772 tree attributes)
1774 cp_declarator *declarator;
1776 declarator = make_declarator (cdk_ptrmem);
1777 declarator->declarator = pointee;
1778 declarator->u.pointer.qualifiers = cv_qualifiers;
1779 declarator->u.pointer.class_type = class_type;
1781 if (pointee)
1783 declarator->parameter_pack_p = pointee->parameter_pack_p;
1784 pointee->parameter_pack_p = false;
1786 else
1787 declarator->parameter_pack_p = false;
1789 declarator->std_attributes = attributes;
1791 return declarator;
1794 /* Make a declarator for the function given by TARGET, with the
1795 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1796 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1797 indicates what exceptions can be thrown. STD_ATTRS contains
1798 attributes that appertain to the function type. */
1800 cp_declarator *
1801 make_call_declarator (cp_declarator *target,
1802 tree parms,
1803 cp_cv_quals cv_qualifiers,
1804 cp_virt_specifiers virt_specifiers,
1805 cp_ref_qualifier ref_qualifier,
1806 tree tx_qualifier,
1807 tree exception_specification,
1808 tree late_return_type,
1809 tree requires_clause,
1810 tree std_attrs,
1811 location_t parens_loc)
1813 cp_declarator *declarator;
1815 declarator = make_declarator (cdk_function);
1816 declarator->declarator = target;
1817 declarator->u.function.parameters = parms;
1818 declarator->u.function.qualifiers = cv_qualifiers;
1819 declarator->u.function.virt_specifiers = virt_specifiers;
1820 declarator->u.function.ref_qualifier = ref_qualifier;
1821 declarator->u.function.tx_qualifier = tx_qualifier;
1822 declarator->u.function.exception_specification = exception_specification;
1823 declarator->u.function.late_return_type = late_return_type;
1824 declarator->u.function.requires_clause = requires_clause;
1825 declarator->u.function.parens_loc = parens_loc;
1826 if (target)
1828 declarator->id_loc = target->id_loc;
1829 declarator->parameter_pack_p = target->parameter_pack_p;
1830 target->parameter_pack_p = false;
1832 else
1833 declarator->parameter_pack_p = false;
1835 declarator->std_attributes = std_attrs;
1837 return declarator;
1840 /* Make a declarator for an array of BOUNDS elements, each of which is
1841 defined by ELEMENT. */
1843 cp_declarator *
1844 make_array_declarator (cp_declarator *element, tree bounds)
1846 cp_declarator *declarator;
1848 declarator = make_declarator (cdk_array);
1849 declarator->declarator = element;
1850 declarator->u.array.bounds = bounds;
1851 if (element)
1853 declarator->id_loc = element->id_loc;
1854 declarator->parameter_pack_p = element->parameter_pack_p;
1855 element->parameter_pack_p = false;
1857 else
1858 declarator->parameter_pack_p = false;
1860 return declarator;
1863 /* Determine whether the declarator we've seen so far can be a
1864 parameter pack, when followed by an ellipsis. */
1865 static bool
1866 declarator_can_be_parameter_pack (cp_declarator *declarator)
1868 if (declarator && declarator->parameter_pack_p)
1869 /* We already saw an ellipsis. */
1870 return false;
1872 /* Search for a declarator name, or any other declarator that goes
1873 after the point where the ellipsis could appear in a parameter
1874 pack. If we find any of these, then this declarator cannot be
1875 made into a parameter pack. */
1876 bool found = false;
1877 while (declarator && !found)
1879 switch ((int)declarator->kind)
1881 case cdk_id:
1882 case cdk_array:
1883 case cdk_decomp:
1884 found = true;
1885 break;
1887 case cdk_error:
1888 return true;
1890 default:
1891 declarator = declarator->declarator;
1892 break;
1896 return !found;
1899 cp_parameter_declarator *no_parameters;
1901 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1902 DECLARATOR and DEFAULT_ARGUMENT. */
1904 cp_parameter_declarator *
1905 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1906 cp_declarator *declarator,
1907 tree default_argument,
1908 location_t loc,
1909 bool template_parameter_pack_p = false)
1911 cp_parameter_declarator *parameter;
1913 parameter = ((cp_parameter_declarator *)
1914 alloc_declarator (sizeof (cp_parameter_declarator)));
1915 parameter->next = NULL;
1916 if (decl_specifiers)
1917 parameter->decl_specifiers = *decl_specifiers;
1918 else
1919 clear_decl_specs (&parameter->decl_specifiers);
1920 parameter->declarator = declarator;
1921 parameter->default_argument = default_argument;
1922 parameter->template_parameter_pack_p = template_parameter_pack_p;
1923 parameter->loc = loc;
1925 return parameter;
1928 /* Returns true iff DECLARATOR is a declaration for a function. */
1930 static bool
1931 function_declarator_p (const cp_declarator *declarator)
1933 while (declarator)
1935 if (declarator->kind == cdk_function
1936 && declarator->declarator->kind == cdk_id)
1937 return true;
1938 if (declarator->kind == cdk_id
1939 || declarator->kind == cdk_decomp
1940 || declarator->kind == cdk_error)
1941 return false;
1942 declarator = declarator->declarator;
1944 return false;
1947 /* The parser. */
1949 /* Overview
1950 --------
1952 A cp_parser parses the token stream as specified by the C++
1953 grammar. Its job is purely parsing, not semantic analysis. For
1954 example, the parser breaks the token stream into declarators,
1955 expressions, statements, and other similar syntactic constructs.
1956 It does not check that the types of the expressions on either side
1957 of an assignment-statement are compatible, or that a function is
1958 not declared with a parameter of type `void'.
1960 The parser invokes routines elsewhere in the compiler to perform
1961 semantic analysis and to build up the abstract syntax tree for the
1962 code processed.
1964 The parser (and the template instantiation code, which is, in a
1965 way, a close relative of parsing) are the only parts of the
1966 compiler that should be calling push_scope and pop_scope, or
1967 related functions. The parser (and template instantiation code)
1968 keeps track of what scope is presently active; everything else
1969 should simply honor that. (The code that generates static
1970 initializers may also need to set the scope, in order to check
1971 access control correctly when emitting the initializers.)
1973 Methodology
1974 -----------
1976 The parser is of the standard recursive-descent variety. Upcoming
1977 tokens in the token stream are examined in order to determine which
1978 production to use when parsing a non-terminal. Some C++ constructs
1979 require arbitrary look ahead to disambiguate. For example, it is
1980 impossible, in the general case, to tell whether a statement is an
1981 expression or declaration without scanning the entire statement.
1982 Therefore, the parser is capable of "parsing tentatively." When the
1983 parser is not sure what construct comes next, it enters this mode.
1984 Then, while we attempt to parse the construct, the parser queues up
1985 error messages, rather than issuing them immediately, and saves the
1986 tokens it consumes. If the construct is parsed successfully, the
1987 parser "commits", i.e., it issues any queued error messages and
1988 the tokens that were being preserved are permanently discarded.
1989 If, however, the construct is not parsed successfully, the parser
1990 rolls back its state completely so that it can resume parsing using
1991 a different alternative.
1993 Future Improvements
1994 -------------------
1996 The performance of the parser could probably be improved substantially.
1997 We could often eliminate the need to parse tentatively by looking ahead
1998 a little bit. In some places, this approach might not entirely eliminate
1999 the need to parse tentatively, but it might still speed up the average
2000 case. */
2002 /* Flags that are passed to some parsing functions. These values can
2003 be bitwise-ored together. */
2005 enum
2007 /* No flags. */
2008 CP_PARSER_FLAGS_NONE = 0x0,
2009 /* The construct is optional. If it is not present, then no error
2010 should be issued. */
2011 CP_PARSER_FLAGS_OPTIONAL = 0x1,
2012 /* When parsing a type-specifier, treat user-defined type-names
2013 as non-type identifiers. */
2014 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
2015 /* When parsing a type-specifier, do not try to parse a class-specifier
2016 or enum-specifier. */
2017 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
2018 /* When parsing a decl-specifier-seq, only allow type-specifier or
2019 constexpr. */
2020 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
2021 /* When parsing a decl-specifier-seq, only allow mutable, constexpr or
2022 for C++20 consteval or for C++23 static. */
2023 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10,
2024 /* When parsing a decl-specifier-seq, allow missing typename. */
2025 CP_PARSER_FLAGS_TYPENAME_OPTIONAL = 0x20,
2026 /* When parsing of the noexcept-specifier should be delayed. */
2027 CP_PARSER_FLAGS_DELAY_NOEXCEPT = 0x40,
2028 /* When parsing a consteval declarator. */
2029 CP_PARSER_FLAGS_CONSTEVAL = 0x80
2032 /* This type is used for parameters and variables which hold
2033 combinations of the above flags. */
2034 typedef int cp_parser_flags;
2036 /* The different kinds of declarators we want to parse. */
2038 enum cp_parser_declarator_kind
2040 /* We want an abstract declarator. */
2041 CP_PARSER_DECLARATOR_ABSTRACT,
2042 /* We want a named declarator. */
2043 CP_PARSER_DECLARATOR_NAMED,
2044 /* We don't mind, but the name must be an unqualified-id. */
2045 CP_PARSER_DECLARATOR_EITHER
2048 /* The precedence values used to parse binary expressions. The minimum value
2049 of PREC must be 1, because zero is reserved to quickly discriminate
2050 binary operators from other tokens. */
2052 enum cp_parser_prec
2054 PREC_NOT_OPERATOR,
2055 PREC_LOGICAL_OR_EXPRESSION,
2056 PREC_LOGICAL_AND_EXPRESSION,
2057 PREC_INCLUSIVE_OR_EXPRESSION,
2058 PREC_EXCLUSIVE_OR_EXPRESSION,
2059 PREC_AND_EXPRESSION,
2060 PREC_EQUALITY_EXPRESSION,
2061 PREC_RELATIONAL_EXPRESSION,
2062 PREC_SPACESHIP_EXPRESSION,
2063 PREC_SHIFT_EXPRESSION,
2064 PREC_ADDITIVE_EXPRESSION,
2065 PREC_MULTIPLICATIVE_EXPRESSION,
2066 PREC_PM_EXPRESSION,
2067 NUM_PREC_VALUES = PREC_PM_EXPRESSION
2070 /* A mapping from a token type to a corresponding tree node type, with a
2071 precedence value. */
2073 struct cp_parser_binary_operations_map_node
2075 /* The token type. */
2076 enum cpp_ttype token_type;
2077 /* The corresponding tree code. */
2078 enum tree_code tree_type;
2079 /* The precedence of this operator. */
2080 enum cp_parser_prec prec;
2083 struct cp_parser_expression_stack_entry
2085 /* Left hand side of the binary operation we are currently
2086 parsing. */
2087 cp_expr lhs;
2088 /* Original tree code for left hand side, if it was a binary
2089 expression itself (used for -Wparentheses). */
2090 enum tree_code lhs_type;
2091 /* Tree code for the binary operation we are parsing. */
2092 enum tree_code tree_type;
2093 /* Precedence of the binary operation we are parsing. */
2094 enum cp_parser_prec prec;
2095 /* Location of the binary operation we are parsing. */
2096 location_t loc;
2097 /* Flags from the operator token. */
2098 unsigned char flags;
2101 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
2102 entries because precedence levels on the stack are monotonically
2103 increasing. */
2104 typedef struct cp_parser_expression_stack_entry
2105 cp_parser_expression_stack[NUM_PREC_VALUES];
2107 /* Used for parsing OMP for loops.
2109 Some notes on flags used for context:
2110 parser->omp_for_parse_state is non-null anywhere inside the OMP FOR
2111 construct, except for the final-loop-body.
2112 The want_nested_loop flag is true if inside a {} sequence where
2113 a loop-nest (or another {} sequence containing a loop-nest) is expected,
2114 but has not yet been seen. It's false when parsing intervening code
2115 statements or their substatements that cannot contain a loop-nest.
2116 The in_intervening_code flag is true when parsing any intervening code,
2117 including substatements, and whether or not want_nested_loop is true.
2119 And, about error handling:
2120 The saw_intervening_code flag is set if the loop is not perfectly
2121 nested, even in the usual case where this is not an error.
2122 perfect_nesting_fail is set if an error has been diagnosed because an
2123 imperfectly-nested loop was found where a perfectly-nested one is
2124 required (we diagnose this only once).
2125 fail is set if any kind of structural error in the loop nest
2126 has been found and diagnosed.
2128 struct omp_for_parse_data {
2129 enum tree_code code;
2130 tree declv, condv, incrv, initv;
2131 tree pre_body;
2132 tree orig_declv;
2133 auto_vec<tree, 4> orig_inits;
2134 int count; /* Expected nesting depth. */
2135 int depth; /* Current nesting depth. */
2136 location_t for_loc;
2137 releasing_vec init_blockv;
2138 releasing_vec body_blockv;
2139 releasing_vec init_placeholderv;
2140 releasing_vec body_placeholderv;
2141 bool ordered : 1;
2142 bool inscan : 1;
2143 bool want_nested_loop : 1;
2144 bool in_intervening_code : 1;
2145 bool saw_intervening_code : 1;
2146 bool perfect_nesting_fail : 1;
2147 bool fail : 1;
2148 tree clauses;
2149 tree *cclauses;
2150 tree ordered_cl;
2153 /* Prototypes. */
2155 /* Constructors and destructors. */
2157 static cp_parser_context *cp_parser_context_new
2158 (cp_parser_context *);
2160 /* Class variables. */
2162 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
2164 /* The operator-precedence table used by cp_parser_binary_expression.
2165 Transformed into an associative array (binops_by_token) by
2166 cp_parser_new. */
2168 static const cp_parser_binary_operations_map_node binops[] = {
2169 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
2170 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
2172 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
2173 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
2174 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
2176 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
2177 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
2179 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
2180 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
2182 { CPP_SPACESHIP, SPACESHIP_EXPR, PREC_SPACESHIP_EXPRESSION },
2184 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
2185 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
2186 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
2187 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
2189 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
2190 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
2192 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
2194 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
2196 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
2198 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
2200 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
2203 /* The same as binops, but initialized by cp_parser_new so that
2204 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
2205 for speed. */
2206 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
2208 /* Constructors and destructors. */
2210 /* Construct a new context. The context below this one on the stack
2211 is given by NEXT. */
2213 static cp_parser_context *
2214 cp_parser_context_new (cp_parser_context* next)
2216 cp_parser_context *context;
2218 /* Allocate the storage. */
2219 if (cp_parser_context_free_list != NULL)
2221 /* Pull the first entry from the free list. */
2222 context = cp_parser_context_free_list;
2223 cp_parser_context_free_list = context->next;
2224 memset (context, 0, sizeof (*context));
2226 else
2227 context = ggc_cleared_alloc<cp_parser_context> ();
2229 /* No errors have occurred yet in this context. */
2230 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
2231 /* If this is not the bottommost context, copy information that we
2232 need from the previous context. */
2233 if (next)
2235 /* If, in the NEXT context, we are parsing an `x->' or `x.'
2236 expression, then we are parsing one in this context, too. */
2237 context->object_type = next->object_type;
2238 /* Thread the stack. */
2239 context->next = next;
2242 return context;
2245 /* Managing the unparsed function queues. */
2247 #define unparsed_funs_with_default_args \
2248 parser->unparsed_queues->last ().funs_with_default_args
2249 #define unparsed_funs_with_definitions \
2250 parser->unparsed_queues->last ().funs_with_definitions
2251 #define unparsed_nsdmis \
2252 parser->unparsed_queues->last ().nsdmis
2253 #define unparsed_noexcepts \
2254 parser->unparsed_queues->last ().noexcepts
2255 #define unparsed_contracts \
2256 parser->unparsed_queues->last ().contracts
2258 static void
2259 push_unparsed_function_queues (cp_parser *parser)
2261 cp_unparsed_functions_entry e
2262 = { NULL, make_tree_vector (), NULL, NULL, NULL };
2263 vec_safe_push (parser->unparsed_queues, e);
2266 static void
2267 pop_unparsed_function_queues (cp_parser *parser)
2269 release_tree_vector (unparsed_funs_with_definitions);
2270 parser->unparsed_queues->pop ();
2273 /* Prototypes. */
2275 /* Routines to parse various constructs.
2277 Those that return `tree' will return the error_mark_node (rather
2278 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2279 Sometimes, they will return an ordinary node if error-recovery was
2280 attempted, even though a parse error occurred. So, to check
2281 whether or not a parse error occurred, you should always use
2282 cp_parser_error_occurred. If the construct is optional (indicated
2283 either by an `_opt' in the name of the function that does the
2284 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2285 the construct is not present. */
2287 /* Lexical conventions [gram.lex] */
2289 static tree finish_userdef_string_literal
2290 (tree);
2292 /* Basic concepts [gram.basic] */
2294 static void cp_parser_translation_unit (cp_parser *);
2296 /* Expressions [gram.expr] */
2298 static cp_expr cp_parser_primary_expression
2299 (cp_parser *, bool, bool, bool, cp_id_kind *);
2300 static cp_expr cp_parser_id_expression
2301 (cp_parser *, bool, bool, bool *, bool, bool);
2302 static cp_expr cp_parser_unqualified_id
2303 (cp_parser *, bool, bool, bool, bool);
2304 static tree cp_parser_nested_name_specifier_opt
2305 (cp_parser *, bool, bool, bool, bool, bool = false);
2306 static tree cp_parser_nested_name_specifier
2307 (cp_parser *, bool, bool, bool, bool);
2308 static tree cp_parser_qualifying_entity
2309 (cp_parser *, bool, bool, bool, bool, bool);
2310 static cp_expr cp_parser_postfix_expression
2311 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2312 static tree cp_parser_postfix_open_square_expression
2313 (cp_parser *, tree, bool, bool);
2314 static tree cp_parser_postfix_dot_deref_expression
2315 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2316 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2317 (cp_parser *, int, bool, bool, bool *, location_t * = NULL,
2318 bool = false);
2319 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2320 enum { non_attr = 0, normal_attr = 1, id_attr = 2, assume_attr = 3 };
2321 static void cp_parser_pseudo_destructor_name
2322 (cp_parser *, tree, tree *, tree *);
2323 static cp_expr cp_parser_unary_expression
2324 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2325 static enum tree_code cp_parser_unary_operator
2326 (cp_token *);
2327 static tree cp_parser_has_attribute_expression
2328 (cp_parser *);
2329 static tree cp_parser_new_expression
2330 (cp_parser *);
2331 static vec<tree, va_gc> *cp_parser_new_placement
2332 (cp_parser *);
2333 static tree cp_parser_new_type_id
2334 (cp_parser *, tree *);
2335 static cp_declarator *cp_parser_new_declarator_opt
2336 (cp_parser *);
2337 static cp_declarator *cp_parser_direct_new_declarator
2338 (cp_parser *);
2339 static vec<tree, va_gc> *cp_parser_new_initializer
2340 (cp_parser *);
2341 static tree cp_parser_delete_expression
2342 (cp_parser *);
2343 static cp_expr cp_parser_cast_expression
2344 (cp_parser *, bool, bool, bool, cp_id_kind *);
2345 static cp_expr cp_parser_binary_expression
2346 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2347 static tree cp_parser_question_colon_clause
2348 (cp_parser *, cp_expr);
2349 static cp_expr cp_parser_conditional_expression (cp_parser *);
2350 static cp_expr cp_parser_assignment_expression
2351 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2352 static enum tree_code cp_parser_assignment_operator_opt
2353 (cp_parser *);
2354 static cp_expr cp_parser_expression
2355 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2356 static cp_expr cp_parser_constant_expression
2357 (cp_parser *, int = 0, bool * = NULL, bool = false);
2358 static cp_expr cp_parser_builtin_offsetof
2359 (cp_parser *);
2360 static cp_expr cp_parser_lambda_expression
2361 (cp_parser *);
2362 static void cp_parser_lambda_introducer
2363 (cp_parser *, tree);
2364 static bool cp_parser_lambda_declarator_opt
2365 (cp_parser *, tree);
2366 static void cp_parser_lambda_body
2367 (cp_parser *, tree);
2369 /* Statements [gram.stmt.stmt] */
2371 static void cp_parser_statement
2372 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2373 static void cp_parser_label_for_labeled_statement
2374 (cp_parser *, tree);
2375 static tree cp_parser_expression_statement
2376 (cp_parser *, tree);
2377 static tree cp_parser_compound_statement
2378 (cp_parser *, tree, int, bool);
2379 static void cp_parser_statement_seq_opt
2380 (cp_parser *, tree);
2381 static tree cp_parser_selection_statement
2382 (cp_parser *, bool *, vec<tree> *);
2383 static tree cp_parser_condition
2384 (cp_parser *);
2385 static tree cp_parser_iteration_statement
2386 (cp_parser *, bool *, bool, unsigned short, bool);
2387 static bool cp_parser_init_statement
2388 (cp_parser *, tree *decl);
2389 static tree cp_parser_for
2390 (cp_parser *, bool, unsigned short, bool);
2391 static tree cp_parser_c_for
2392 (cp_parser *, tree, tree, bool, unsigned short, bool);
2393 static tree cp_parser_range_for
2394 (cp_parser *, tree, tree, tree, bool, unsigned short, bool, bool);
2395 static void do_range_for_auto_deduction
2396 (tree, tree, cp_decomp *);
2397 static tree cp_parser_perform_range_for_lookup
2398 (tree, tree *, tree *);
2399 static tree cp_parser_range_for_member_function
2400 (tree, tree);
2401 static tree cp_parser_jump_statement
2402 (cp_parser *);
2403 static void cp_parser_declaration_statement
2404 (cp_parser *);
2406 static tree cp_parser_implicitly_scoped_statement
2407 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2408 static void cp_parser_already_scoped_statement
2409 (cp_parser *, bool *, const token_indent_info &);
2411 /* State of module-declaration parsing. */
2412 enum module_parse
2414 MP_NOT_MODULE, /* Not a module. */
2416 _MP_UNUSED,
2418 MP_FIRST, /* First declaration of TU. */
2419 MP_GLOBAL, /* Global Module Fragment. */
2421 MP_PURVIEW_IMPORTS, /* Imports of a module. */
2422 MP_PURVIEW, /* Purview of a named module. */
2424 MP_PRIVATE_IMPORTS, /* Imports of a Private Module Fragment. */
2425 MP_PRIVATE, /* Private Module Fragment. */
2428 static module_parse cp_parser_module_declaration
2429 (cp_parser *parser, module_parse, bool exporting);
2430 static void cp_parser_import_declaration
2431 (cp_parser *parser, module_parse, bool exporting);
2433 /* Declarations [gram.dcl.dcl] */
2435 static void cp_parser_declaration_seq_opt
2436 (cp_parser *);
2437 static void cp_parser_declaration
2438 (cp_parser *, tree);
2439 static void cp_parser_toplevel_declaration
2440 (cp_parser *);
2441 static void cp_parser_block_declaration
2442 (cp_parser *, bool);
2443 static void cp_parser_simple_declaration
2444 (cp_parser *, bool, tree *);
2445 static void cp_parser_decl_specifier_seq
2446 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2447 static tree cp_parser_storage_class_specifier_opt
2448 (cp_parser *);
2449 static tree cp_parser_function_specifier_opt
2450 (cp_parser *, cp_decl_specifier_seq *);
2451 static tree cp_parser_type_specifier
2452 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2453 int *, bool *);
2454 static tree cp_parser_simple_type_specifier
2455 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2456 static tree cp_parser_placeholder_type_specifier
2457 (cp_parser *, location_t, tree, bool);
2458 static tree cp_parser_type_name
2459 (cp_parser *, bool);
2460 static tree cp_parser_nonclass_name
2461 (cp_parser* parser);
2462 static tree cp_parser_elaborated_type_specifier
2463 (cp_parser *, bool, bool);
2464 static tree cp_parser_enum_specifier
2465 (cp_parser *);
2466 static void cp_parser_enumerator_list
2467 (cp_parser *, tree);
2468 static void cp_parser_enumerator_definition
2469 (cp_parser *, tree);
2470 static tree cp_parser_namespace_name
2471 (cp_parser *);
2472 static void cp_parser_namespace_definition
2473 (cp_parser *);
2474 static void cp_parser_namespace_body
2475 (cp_parser *);
2476 static tree cp_parser_qualified_namespace_specifier
2477 (cp_parser *);
2478 static void cp_parser_namespace_alias_definition
2479 (cp_parser *);
2480 static bool cp_parser_using_declaration
2481 (cp_parser *, bool);
2482 static void cp_parser_using_directive
2483 (cp_parser *);
2484 static void cp_parser_using_enum
2485 (cp_parser *);
2486 static tree cp_parser_alias_declaration
2487 (cp_parser *);
2488 static void cp_parser_asm_definition
2489 (cp_parser *);
2490 static void cp_parser_linkage_specification
2491 (cp_parser *, tree);
2492 static void cp_parser_static_assert
2493 (cp_parser *, bool);
2494 static tree cp_parser_decltype
2495 (cp_parser *);
2496 static tree cp_parser_decomposition_declaration
2497 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2499 /* Declarators [gram.dcl.decl] */
2501 static tree cp_parser_init_declarator
2502 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *,
2503 vec<deferred_access_check, va_gc> *, bool, bool, int, bool *, tree *,
2504 location_t *, tree *);
2505 static cp_declarator *cp_parser_declarator
2506 (cp_parser *, cp_parser_declarator_kind, cp_parser_flags, int *, bool *,
2507 bool, bool, bool);
2508 static cp_declarator *cp_parser_direct_declarator
2509 (cp_parser *, cp_parser_declarator_kind, cp_parser_flags, int *, bool, bool,
2510 bool);
2511 static enum tree_code cp_parser_ptr_operator
2512 (cp_parser *, tree *, cp_cv_quals *, tree *);
2513 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2514 (cp_parser *);
2515 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2516 (cp_parser *);
2517 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2518 (cp_parser *);
2519 static tree cp_parser_tx_qualifier_opt
2520 (cp_parser *);
2521 static tree cp_parser_late_return_type_opt
2522 (cp_parser *, cp_declarator *, tree &);
2523 static tree cp_parser_declarator_id
2524 (cp_parser *, bool);
2525 static tree cp_parser_type_id
2526 (cp_parser *, cp_parser_flags = CP_PARSER_FLAGS_NONE, location_t * = NULL);
2527 static tree cp_parser_template_type_arg
2528 (cp_parser *);
2529 static tree cp_parser_trailing_type_id (cp_parser *);
2530 static tree cp_parser_type_id_1
2531 (cp_parser *, cp_parser_flags, bool, bool, location_t *);
2532 static void cp_parser_type_specifier_seq
2533 (cp_parser *, cp_parser_flags, bool, bool, cp_decl_specifier_seq *);
2534 static tree cp_parser_parameter_declaration_clause
2535 (cp_parser *, cp_parser_flags);
2536 static tree cp_parser_parameter_declaration_list
2537 (cp_parser *, cp_parser_flags, auto_vec<tree> *);
2538 static cp_parameter_declarator *cp_parser_parameter_declaration
2539 (cp_parser *, cp_parser_flags, bool, bool *);
2540 static tree cp_parser_default_argument
2541 (cp_parser *, bool);
2542 static void cp_parser_function_body
2543 (cp_parser *, bool);
2544 static tree cp_parser_initializer
2545 (cp_parser *, bool * = nullptr, bool * = nullptr, bool = false);
2546 static cp_expr cp_parser_initializer_clause
2547 (cp_parser *, bool * = nullptr);
2548 static cp_expr cp_parser_braced_list
2549 (cp_parser*, bool * = nullptr);
2550 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2551 (cp_parser *, bool *, bool *);
2553 static void cp_parser_ctor_initializer_opt_and_function_body
2554 (cp_parser *, bool);
2556 static tree cp_parser_late_parsing_omp_declare_simd
2557 (cp_parser *, tree);
2559 static tree cp_parser_late_parsing_oacc_routine
2560 (cp_parser *, tree);
2562 static tree synthesize_implicit_template_parm
2563 (cp_parser *, tree);
2564 static tree finish_fully_implicit_template
2565 (cp_parser *, tree);
2566 static void abort_fully_implicit_template
2567 (cp_parser *);
2569 /* Classes [gram.class] */
2571 static tree cp_parser_class_name
2572 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2573 static tree cp_parser_class_specifier
2574 (cp_parser *);
2575 static tree cp_parser_class_head
2576 (cp_parser *, bool *);
2577 static enum tag_types cp_parser_class_key
2578 (cp_parser *);
2579 static void cp_parser_type_parameter_key
2580 (cp_parser* parser);
2581 static void cp_parser_member_specification_opt
2582 (cp_parser *);
2583 static void cp_parser_member_declaration
2584 (cp_parser *);
2585 static tree cp_parser_pure_specifier
2586 (cp_parser *);
2587 static tree cp_parser_constant_initializer
2588 (cp_parser *);
2590 /* Derived classes [gram.class.derived] */
2592 static tree cp_parser_base_clause
2593 (cp_parser *);
2594 static tree cp_parser_base_specifier
2595 (cp_parser *);
2597 /* Special member functions [gram.special] */
2599 static tree cp_parser_conversion_function_id
2600 (cp_parser *);
2601 static tree cp_parser_conversion_type_id
2602 (cp_parser *);
2603 static cp_declarator *cp_parser_conversion_declarator_opt
2604 (cp_parser *);
2605 static void cp_parser_ctor_initializer_opt
2606 (cp_parser *);
2607 static void cp_parser_mem_initializer_list
2608 (cp_parser *);
2609 static tree cp_parser_mem_initializer
2610 (cp_parser *);
2611 static tree cp_parser_mem_initializer_id
2612 (cp_parser *);
2614 /* Overloading [gram.over] */
2616 static cp_expr cp_parser_operator_function_id
2617 (cp_parser *);
2618 static cp_expr cp_parser_operator
2619 (cp_parser *, location_t);
2621 /* Templates [gram.temp] */
2623 static void cp_parser_template_declaration
2624 (cp_parser *, bool);
2625 static tree cp_parser_template_parameter_list
2626 (cp_parser *);
2627 static tree cp_parser_template_parameter
2628 (cp_parser *, bool *, bool *);
2629 static tree cp_parser_type_parameter
2630 (cp_parser *, bool *);
2631 static tree cp_parser_template_id
2632 (cp_parser *, bool, bool, enum tag_types, bool);
2633 static tree cp_parser_template_id_expr
2634 (cp_parser *, bool, bool, bool);
2635 static tree cp_parser_template_name
2636 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2637 static tree cp_parser_template_argument_list
2638 (cp_parser *);
2639 static tree cp_parser_template_argument
2640 (cp_parser *);
2641 static void cp_parser_explicit_instantiation
2642 (cp_parser *);
2643 static void cp_parser_explicit_specialization
2644 (cp_parser *);
2646 /* Exception handling [gram.except] */
2648 static tree cp_parser_try_block
2649 (cp_parser *);
2650 static void cp_parser_function_try_block
2651 (cp_parser *);
2652 static void cp_parser_handler_seq
2653 (cp_parser *);
2654 static void cp_parser_handler
2655 (cp_parser *);
2656 static tree cp_parser_exception_declaration
2657 (cp_parser *);
2658 static tree cp_parser_throw_expression
2659 (cp_parser *);
2660 static tree cp_parser_exception_specification_opt
2661 (cp_parser *, cp_parser_flags);
2662 static tree cp_parser_type_id_list
2663 (cp_parser *);
2664 static tree cp_parser_noexcept_specification_opt
2665 (cp_parser *, cp_parser_flags, bool, bool *, bool);
2667 /* GNU Extensions */
2669 static tree cp_parser_asm_specification_opt
2670 (cp_parser *);
2671 static tree cp_parser_asm_operand_list
2672 (cp_parser *);
2673 static tree cp_parser_asm_clobber_list
2674 (cp_parser *);
2675 static tree cp_parser_asm_label_list
2676 (cp_parser *);
2677 static bool cp_next_tokens_can_be_attribute_p
2678 (cp_parser *);
2679 static bool cp_next_tokens_can_be_gnu_attribute_p
2680 (cp_parser *);
2681 static bool cp_next_tokens_can_be_std_attribute_p
2682 (cp_parser *);
2683 static bool cp_nth_tokens_can_be_std_attribute_p
2684 (cp_parser *, size_t);
2685 static bool cp_nth_tokens_can_be_gnu_attribute_p
2686 (cp_parser *, size_t);
2687 static bool cp_nth_tokens_can_be_attribute_p
2688 (cp_parser *, size_t);
2689 static tree cp_parser_attributes_opt
2690 (cp_parser *);
2691 static tree cp_parser_gnu_attributes_opt
2692 (cp_parser *);
2693 static tree cp_parser_gnu_attribute_list
2694 (cp_parser *, bool = false);
2695 static tree cp_parser_std_attribute
2696 (cp_parser *, tree);
2697 static tree cp_parser_std_attribute_spec
2698 (cp_parser *);
2699 static tree cp_parser_std_attribute_spec_seq
2700 (cp_parser *);
2701 static size_t cp_parser_skip_std_attribute_spec_seq
2702 (cp_parser *, size_t);
2703 static size_t cp_parser_skip_attributes_opt
2704 (cp_parser *, size_t);
2705 static bool cp_parser_extension_opt
2706 (cp_parser *, int *);
2707 static void cp_parser_label_declaration
2708 (cp_parser *);
2710 /* Concept Extensions */
2712 static tree cp_parser_concept_definition
2713 (cp_parser *);
2714 static tree cp_parser_constraint_expression
2715 (cp_parser *);
2716 static tree cp_parser_requires_clause_opt
2717 (cp_parser *, bool);
2718 static tree cp_parser_requires_expression
2719 (cp_parser *);
2720 static tree cp_parser_requirement_parameter_list
2721 (cp_parser *);
2722 static tree cp_parser_requirement_body
2723 (cp_parser *);
2724 static tree cp_parser_requirement_seq
2725 (cp_parser *);
2726 static tree cp_parser_requirement
2727 (cp_parser *);
2728 static tree cp_parser_simple_requirement
2729 (cp_parser *);
2730 static tree cp_parser_compound_requirement
2731 (cp_parser *);
2732 static tree cp_parser_type_requirement
2733 (cp_parser *);
2734 static tree cp_parser_nested_requirement
2735 (cp_parser *);
2737 /* Transactional Memory Extensions */
2739 static tree cp_parser_transaction
2740 (cp_parser *, cp_token *);
2741 static tree cp_parser_transaction_expression
2742 (cp_parser *, enum rid);
2743 static void cp_parser_function_transaction
2744 (cp_parser *, enum rid);
2745 static tree cp_parser_transaction_cancel
2746 (cp_parser *);
2748 /* Coroutine extensions. */
2750 static tree cp_parser_yield_expression
2751 (cp_parser *);
2753 /* Contracts */
2755 static void cp_parser_late_contract_condition
2756 (cp_parser *, tree, tree);
2758 enum pragma_context {
2759 pragma_external,
2760 pragma_member,
2761 pragma_objc_icode,
2762 pragma_stmt,
2763 pragma_compound
2765 static bool cp_parser_pragma
2766 (cp_parser *, enum pragma_context, bool *);
2768 /* Objective-C++ Productions */
2770 static tree cp_parser_objc_message_receiver
2771 (cp_parser *);
2772 static tree cp_parser_objc_message_args
2773 (cp_parser *);
2774 static tree cp_parser_objc_message_expression
2775 (cp_parser *);
2776 static cp_expr cp_parser_objc_encode_expression
2777 (cp_parser *);
2778 static tree cp_parser_objc_defs_expression
2779 (cp_parser *);
2780 static tree cp_parser_objc_protocol_expression
2781 (cp_parser *);
2782 static tree cp_parser_objc_selector_expression
2783 (cp_parser *);
2784 static cp_expr cp_parser_objc_expression
2785 (cp_parser *);
2786 static bool cp_parser_objc_selector_p
2787 (enum cpp_ttype);
2788 static tree cp_parser_objc_selector
2789 (cp_parser *);
2790 static tree cp_parser_objc_protocol_refs_opt
2791 (cp_parser *);
2792 static void cp_parser_objc_declaration
2793 (cp_parser *, tree);
2794 static tree cp_parser_objc_statement
2795 (cp_parser *);
2796 static bool cp_parser_objc_valid_prefix_attributes
2797 (cp_parser *, tree *);
2798 static void cp_parser_objc_at_property_declaration
2799 (cp_parser *) ;
2800 static void cp_parser_objc_at_synthesize_declaration
2801 (cp_parser *) ;
2802 static void cp_parser_objc_at_dynamic_declaration
2803 (cp_parser *) ;
2804 static tree cp_parser_objc_struct_declaration
2805 (cp_parser *) ;
2807 /* Utility Routines */
2809 static cp_expr cp_parser_lookup_name
2810 (cp_parser *, tree, enum tag_types, int, bool, bool, tree *, location_t);
2811 static tree cp_parser_lookup_name_simple
2812 (cp_parser *, tree, location_t);
2813 static tree cp_parser_maybe_treat_template_as_class
2814 (tree, bool);
2815 static bool cp_parser_check_declarator_template_parameters
2816 (cp_parser *, cp_declarator *, location_t);
2817 static bool cp_parser_check_template_parameters
2818 (cp_parser *, unsigned, bool, location_t, cp_declarator *);
2819 static cp_expr cp_parser_simple_cast_expression
2820 (cp_parser *);
2821 static tree cp_parser_global_scope_opt
2822 (cp_parser *, bool);
2823 static bool cp_parser_constructor_declarator_p
2824 (cp_parser *, cp_parser_flags, bool);
2825 static tree cp_parser_function_definition_from_specifiers_and_declarator
2826 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2827 static tree cp_parser_function_definition_after_declarator
2828 (cp_parser *, bool);
2829 static bool cp_parser_template_declaration_after_export
2830 (cp_parser *, bool);
2831 static void cp_parser_perform_template_parameter_access_checks
2832 (vec<deferred_access_check, va_gc> *);
2833 static tree cp_parser_single_declaration
2834 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2835 static cp_expr cp_parser_functional_cast
2836 (cp_parser *, tree);
2837 static tree cp_parser_save_member_function_body
2838 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2839 static tree cp_parser_save_nsdmi
2840 (cp_parser *);
2841 static tree cp_parser_enclosed_template_argument_list
2842 (cp_parser *);
2843 static void cp_parser_save_default_args
2844 (cp_parser *, tree);
2845 static void cp_parser_late_parsing_for_member
2846 (cp_parser *, tree);
2847 static tree cp_parser_late_parse_one_default_arg
2848 (cp_parser *, tree, tree, tree);
2849 static void cp_parser_late_parsing_nsdmi
2850 (cp_parser *, tree);
2851 static void cp_parser_late_parsing_default_args
2852 (cp_parser *, tree);
2853 static tree cp_parser_sizeof_operand
2854 (cp_parser *, enum rid);
2855 static cp_expr cp_parser_trait
2856 (cp_parser *, enum rid);
2857 static bool cp_parser_declares_only_class_p
2858 (cp_parser *);
2859 static void cp_parser_set_storage_class
2860 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2861 static void cp_parser_set_decl_spec_type
2862 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2863 static void set_and_check_decl_spec_loc
2864 (cp_decl_specifier_seq *decl_specs,
2865 cp_decl_spec ds, cp_token *);
2866 static bool cp_parser_friend_p
2867 (const cp_decl_specifier_seq *);
2868 static void cp_parser_required_error
2869 (cp_parser *, required_token, bool, location_t);
2870 static cp_token *cp_parser_require
2871 (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2872 static cp_token *cp_parser_require_keyword
2873 (cp_parser *, enum rid, required_token);
2874 static bool cp_parser_token_starts_function_definition_p
2875 (cp_token *);
2876 static bool cp_parser_next_token_starts_class_definition_p
2877 (cp_parser *);
2878 static bool cp_parser_next_token_ends_template_argument_p
2879 (cp_parser *);
2880 static bool cp_parser_nth_token_starts_template_argument_list_p
2881 (cp_parser *, size_t);
2882 static enum tag_types cp_parser_token_is_class_key
2883 (cp_token *);
2884 static enum tag_types cp_parser_token_is_type_parameter_key
2885 (cp_token *);
2886 static void cp_parser_maybe_warn_enum_key (cp_parser *, location_t, tree, rid);
2887 static void cp_parser_check_class_key
2888 (cp_parser *, location_t, enum tag_types, tree type, bool, bool);
2889 static void cp_parser_check_access_in_redeclaration
2890 (tree type, location_t location);
2891 static bool cp_parser_optional_template_keyword
2892 (cp_parser *);
2893 static void cp_parser_pre_parsed_nested_name_specifier
2894 (cp_parser *);
2895 static bool cp_parser_cache_group
2896 (cp_parser *, enum cpp_ttype, unsigned);
2897 static tree cp_parser_cache_defarg
2898 (cp_parser *parser, bool nsdmi);
2899 static void cp_parser_parse_tentatively
2900 (cp_parser *);
2901 static void cp_parser_commit_to_tentative_parse
2902 (cp_parser *);
2903 static void cp_parser_commit_to_topmost_tentative_parse
2904 (cp_parser *);
2905 static void cp_parser_abort_tentative_parse
2906 (cp_parser *);
2907 static bool cp_parser_parse_definitely
2908 (cp_parser *);
2909 static inline bool cp_parser_parsing_tentatively
2910 (cp_parser *);
2911 static bool cp_parser_uncommitted_to_tentative_parse_p
2912 (cp_parser *);
2913 static void cp_parser_error
2914 (cp_parser *, const char *);
2915 static void cp_parser_name_lookup_error
2916 (cp_parser *, tree, tree, name_lookup_error, location_t);
2917 static bool cp_parser_simulate_error
2918 (cp_parser *);
2919 static bool cp_parser_check_type_definition
2920 (cp_parser *);
2921 static void cp_parser_check_for_definition_in_return_type
2922 (cp_declarator *, tree, location_t type_location);
2923 static void cp_parser_check_for_invalid_template_id
2924 (cp_parser *, tree, enum tag_types, location_t location);
2925 static bool cp_parser_non_integral_constant_expression
2926 (cp_parser *, non_integral_constant);
2927 static void cp_parser_diagnose_invalid_type_name
2928 (cp_parser *, tree, location_t);
2929 static bool cp_parser_parse_and_diagnose_invalid_type_name
2930 (cp_parser *);
2931 static int cp_parser_skip_to_closing_parenthesis
2932 (cp_parser *, bool, bool, bool);
2933 static void cp_parser_skip_to_end_of_statement
2934 (cp_parser *);
2935 static void cp_parser_consume_semicolon_at_end_of_statement
2936 (cp_parser *);
2937 static void cp_parser_skip_to_end_of_block_or_statement
2938 (cp_parser *);
2939 static bool cp_parser_skip_to_closing_brace
2940 (cp_parser *);
2941 static bool cp_parser_skip_entire_template_parameter_list
2942 (cp_parser *);
2943 static void cp_parser_require_end_of_template_parameter_list
2944 (cp_parser *);
2945 static bool cp_parser_skip_to_end_of_template_parameter_list
2946 (cp_parser *);
2947 static void cp_parser_skip_to_pragma_eol
2948 (cp_parser*, cp_token *);
2949 static bool cp_parser_error_occurred
2950 (cp_parser *);
2951 static bool cp_parser_allow_gnu_extensions_p
2952 (cp_parser *);
2953 static bool cp_parser_is_pure_string_literal
2954 (cp_token *);
2955 static bool cp_parser_is_string_literal
2956 (cp_token *);
2957 static bool cp_parser_is_keyword
2958 (cp_token *, enum rid);
2959 static tree cp_parser_make_typename_type
2960 (cp_parser *, tree, location_t location);
2961 static cp_declarator * cp_parser_make_indirect_declarator
2962 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2963 static bool cp_parser_compound_literal_p
2964 (cp_parser *);
2965 static bool cp_parser_array_designator_p
2966 (cp_parser *);
2967 static bool cp_parser_init_statement_p
2968 (cp_parser *);
2969 static bool cp_parser_skip_up_to_closing_square_bracket
2970 (cp_parser *);
2971 static bool cp_parser_skip_to_closing_square_bracket
2972 (cp_parser *);
2973 static size_t cp_parser_skip_balanced_tokens (cp_parser *, size_t);
2974 static tree cp_parser_omp_loop_nest (cp_parser *, bool *);
2976 // -------------------------------------------------------------------------- //
2977 // Unevaluated Operand Guard
2979 // Implementation of an RAII helper for unevaluated operand parsing.
2980 cp_unevaluated::cp_unevaluated ()
2982 ++cp_unevaluated_operand;
2983 ++c_inhibit_evaluation_warnings;
2986 cp_unevaluated::~cp_unevaluated ()
2988 --c_inhibit_evaluation_warnings;
2989 --cp_unevaluated_operand;
2992 // -------------------------------------------------------------------------- //
2993 // Tentative Parsing
2995 /* Returns nonzero if we are parsing tentatively. */
2997 static inline bool
2998 cp_parser_parsing_tentatively (cp_parser* parser)
3000 return parser->context->next != NULL;
3003 /* Returns nonzero if TOKEN is a string literal. */
3005 static bool
3006 cp_parser_is_pure_string_literal (cp_token* token)
3008 return (token->type == CPP_STRING ||
3009 token->type == CPP_STRING16 ||
3010 token->type == CPP_STRING32 ||
3011 token->type == CPP_WSTRING ||
3012 token->type == CPP_UTF8STRING);
3015 /* Returns nonzero if TOKEN is a string literal
3016 of a user-defined string literal. */
3018 static bool
3019 cp_parser_is_string_literal (cp_token* token)
3021 return (cp_parser_is_pure_string_literal (token) ||
3022 token->type == CPP_STRING_USERDEF ||
3023 token->type == CPP_STRING16_USERDEF ||
3024 token->type == CPP_STRING32_USERDEF ||
3025 token->type == CPP_WSTRING_USERDEF ||
3026 token->type == CPP_UTF8STRING_USERDEF);
3029 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
3031 static bool
3032 cp_parser_is_keyword (cp_token* token, enum rid keyword)
3034 return token->keyword == keyword;
3037 /* Helper function for cp_parser_error.
3038 Having peeked a token of kind TOK1_KIND that might signify
3039 a conflict marker, peek successor tokens to determine
3040 if we actually do have a conflict marker.
3041 Specifically, we consider a run of 7 '<', '=' or '>' characters
3042 at the start of a line as a conflict marker.
3043 These come through the lexer as three pairs and a single,
3044 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
3045 If it returns true, *OUT_LOC is written to with the location/range
3046 of the marker. */
3048 static bool
3049 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
3050 location_t *out_loc)
3052 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
3053 if (token2->type != tok1_kind)
3054 return false;
3055 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
3056 if (token3->type != tok1_kind)
3057 return false;
3058 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
3059 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
3060 return false;
3062 /* It must be at the start of the line. */
3063 location_t start_loc = cp_lexer_peek_token (lexer)->location;
3064 if (LOCATION_COLUMN (start_loc) != 1)
3065 return false;
3067 /* We have a conflict marker. Construct a location of the form:
3068 <<<<<<<
3069 ^~~~~~~
3070 with start == caret, finishing at the end of the marker. */
3071 location_t finish_loc = get_finish (token4->location);
3072 *out_loc = make_location (start_loc, start_loc, finish_loc);
3074 return true;
3077 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
3078 RT_CLOSE_PAREN. */
3080 static const char *
3081 get_matching_symbol (required_token token_desc)
3083 switch (token_desc)
3085 default:
3086 gcc_unreachable ();
3087 return "";
3088 case RT_CLOSE_BRACE:
3089 return "{";
3090 case RT_CLOSE_PAREN:
3091 return "(";
3095 /* Attempt to convert TOKEN_DESC from a required_token to an
3096 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
3098 static enum cpp_ttype
3099 get_required_cpp_ttype (required_token token_desc)
3101 switch (token_desc)
3103 case RT_SEMICOLON:
3104 return CPP_SEMICOLON;
3105 case RT_OPEN_PAREN:
3106 return CPP_OPEN_PAREN;
3107 case RT_CLOSE_BRACE:
3108 return CPP_CLOSE_BRACE;
3109 case RT_OPEN_BRACE:
3110 return CPP_OPEN_BRACE;
3111 case RT_CLOSE_SQUARE:
3112 return CPP_CLOSE_SQUARE;
3113 case RT_OPEN_SQUARE:
3114 return CPP_OPEN_SQUARE;
3115 case RT_COMMA:
3116 return CPP_COMMA;
3117 case RT_COLON:
3118 return CPP_COLON;
3119 case RT_CLOSE_PAREN:
3120 return CPP_CLOSE_PAREN;
3122 default:
3123 /* Use CPP_EOF as a "no completions possible" code. */
3124 return CPP_EOF;
3129 /* Subroutine of cp_parser_error and cp_parser_required_error.
3131 Issue a diagnostic of the form
3132 FILE:LINE: MESSAGE before TOKEN
3133 where TOKEN is the next token in the input stream. MESSAGE
3134 (specified by the caller) is usually of the form "expected
3135 OTHER-TOKEN".
3137 This bypasses the check for tentative passing, and potentially
3138 adds material needed by cp_parser_required_error.
3140 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
3141 suggesting insertion of the missing token.
3143 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
3144 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
3145 location. */
3147 static void
3148 cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
3149 required_token missing_token_desc,
3150 location_t matching_location)
3152 cp_token *token = cp_lexer_peek_token (parser->lexer);
3153 /* This diagnostic makes more sense if it is tagged to the line
3154 of the token we just peeked at. */
3155 cp_lexer_set_source_position_from_token (token);
3157 if (token->type == CPP_PRAGMA)
3159 error_at (token->location,
3160 "%<#pragma%> is not allowed here");
3161 cp_parser_skip_to_pragma_eol (parser, token);
3162 return;
3165 /* If this is actually a conflict marker, report it as such. */
3166 if (token->type == CPP_LSHIFT
3167 || token->type == CPP_RSHIFT
3168 || token->type == CPP_EQ_EQ)
3170 location_t loc;
3171 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
3173 error_at (loc, "version control conflict marker in file");
3174 expanded_location token_exploc = expand_location (token->location);
3175 /* Consume tokens until the end of the source line. */
3176 for (;;)
3178 cp_lexer_consume_token (parser->lexer);
3179 cp_token *next = cp_lexer_peek_token (parser->lexer);
3180 if (next->type == CPP_EOF)
3181 break;
3182 if (next->location == UNKNOWN_LOCATION
3183 || loc == UNKNOWN_LOCATION)
3184 break;
3186 expanded_location next_exploc = expand_location (next->location);
3187 if (next_exploc.file != token_exploc.file)
3188 break;
3189 if (next_exploc.line != token_exploc.line)
3190 break;
3192 return;
3196 auto_diagnostic_group d;
3197 gcc_rich_location richloc (input_location);
3199 bool added_matching_location = false;
3201 if (missing_token_desc != RT_NONE)
3202 if (cp_token *prev_token = cp_lexer_safe_previous_token (parser->lexer))
3204 /* Potentially supply a fix-it hint, suggesting to add the
3205 missing token immediately after the *previous* token.
3206 This may move the primary location within richloc. */
3207 enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
3208 location_t prev_token_loc = prev_token->location;
3209 maybe_suggest_missing_token_insertion (&richloc, ttype,
3210 prev_token_loc);
3212 /* If matching_location != UNKNOWN_LOCATION, highlight it.
3213 Attempt to consolidate diagnostics by printing it as a
3214 secondary range within the main diagnostic. */
3215 if (matching_location != UNKNOWN_LOCATION)
3216 added_matching_location
3217 = richloc.add_location_if_nearby (matching_location);
3220 /* If we were parsing a string-literal and there is an unknown name
3221 token right after, then check to see if that could also have been
3222 a literal string by checking the name against a list of known
3223 standard string literal constants defined in header files. If
3224 there is one, then add that as an hint to the error message. */
3225 name_hint h;
3226 if (token->type == CPP_NAME)
3227 if (cp_token *prev_token = cp_lexer_safe_previous_token (parser->lexer))
3228 if (cp_parser_is_string_literal (prev_token))
3230 tree name = token->u.value;
3231 const char *token_name = IDENTIFIER_POINTER (name);
3232 const char *header_hint
3233 = get_cp_stdlib_header_for_string_macro_name (token_name);
3234 if (header_hint != NULL)
3235 h = name_hint (NULL, new suggest_missing_header (token->location,
3236 token_name,
3237 header_hint));
3240 /* Actually emit the error. */
3241 c_parse_error (gmsgid,
3242 /* Because c_parser_error does not understand
3243 CPP_KEYWORD, keywords are treated like
3244 identifiers. */
3245 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
3246 token->u.value, token->flags, &richloc);
3248 if (missing_token_desc != RT_NONE)
3250 /* If we weren't able to consolidate matching_location, then
3251 print it as a secondary diagnostic. */
3252 if (matching_location != UNKNOWN_LOCATION
3253 && !added_matching_location)
3254 inform (matching_location, "to match this %qs",
3255 get_matching_symbol (missing_token_desc));
3259 /* If not parsing tentatively, issue a diagnostic of the form
3260 FILE:LINE: MESSAGE before TOKEN
3261 where TOKEN is the next token in the input stream. MESSAGE
3262 (specified by the caller) is usually of the form "expected
3263 OTHER-TOKEN". */
3265 static void
3266 cp_parser_error (cp_parser* parser, const char* gmsgid)
3268 if (!cp_parser_simulate_error (parser))
3269 cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
3272 /* Issue an error about name-lookup failing. NAME is the
3273 IDENTIFIER_NODE DECL is the result of
3274 the lookup (as returned from cp_parser_lookup_name). DESIRED is
3275 the thing that we hoped to find. */
3277 static void
3278 cp_parser_name_lookup_error (cp_parser* parser,
3279 tree name,
3280 tree decl,
3281 name_lookup_error desired,
3282 location_t location)
3284 /* If name lookup completely failed, tell the user that NAME was not
3285 declared. */
3286 if (decl == error_mark_node)
3288 if (parser->scope && parser->scope != global_namespace)
3289 error_at (location, "%<%E::%E%> has not been declared",
3290 parser->scope, name);
3291 else if (parser->scope == global_namespace)
3292 error_at (location, "%<::%E%> has not been declared", name);
3293 else if (parser->object_scope
3294 && !CLASS_TYPE_P (parser->object_scope))
3295 error_at (location, "request for member %qE in non-class type %qT",
3296 name, parser->object_scope);
3297 else if (parser->object_scope)
3298 error_at (location, "%<%T::%E%> has not been declared",
3299 parser->object_scope, name);
3300 else
3301 error_at (location, "%qE has not been declared", name);
3303 else if (parser->scope && parser->scope != global_namespace)
3305 switch (desired)
3307 case NLE_TYPE:
3308 error_at (location, "%<%E::%E%> is not a type",
3309 parser->scope, name);
3310 break;
3311 case NLE_CXX98:
3312 error_at (location, "%<%E::%E%> is not a class or namespace",
3313 parser->scope, name);
3314 break;
3315 case NLE_NOT_CXX98:
3316 error_at (location,
3317 "%<%E::%E%> is not a class, namespace, or enumeration",
3318 parser->scope, name);
3319 break;
3320 default:
3321 gcc_unreachable ();
3325 else if (parser->scope == global_namespace)
3327 switch (desired)
3329 case NLE_TYPE:
3330 error_at (location, "%<::%E%> is not a type", name);
3331 break;
3332 case NLE_CXX98:
3333 error_at (location, "%<::%E%> is not a class or namespace", name);
3334 break;
3335 case NLE_NOT_CXX98:
3336 error_at (location,
3337 "%<::%E%> is not a class, namespace, or enumeration",
3338 name);
3339 break;
3340 default:
3341 gcc_unreachable ();
3344 else
3346 switch (desired)
3348 case NLE_TYPE:
3349 error_at (location, "%qE is not a type", name);
3350 break;
3351 case NLE_CXX98:
3352 error_at (location, "%qE is not a class or namespace", name);
3353 break;
3354 case NLE_NOT_CXX98:
3355 error_at (location,
3356 "%qE is not a class, namespace, or enumeration", name);
3357 break;
3358 default:
3359 gcc_unreachable ();
3364 /* If we are parsing tentatively, remember that an error has occurred
3365 during this tentative parse. Returns true if the error was
3366 simulated; false if a message should be issued by the caller. */
3368 static bool
3369 cp_parser_simulate_error (cp_parser* parser)
3371 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3373 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3374 return true;
3376 return false;
3379 /* This function is called when a type is defined. If type
3380 definitions are forbidden at this point, an error message is
3381 issued. */
3383 static bool
3384 cp_parser_check_type_definition (cp_parser* parser)
3386 /* If types are forbidden here, issue a message. */
3387 if (parser->type_definition_forbidden_message)
3389 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3390 or %qs in the message need to be interpreted. */
3391 error (parser->type_definition_forbidden_message,
3392 parser->type_definition_forbidden_message_arg);
3393 return false;
3395 return true;
3398 /* This function is called when the DECLARATOR is processed. The TYPE
3399 was a type defined in the decl-specifiers. If it is invalid to
3400 define a type in the decl-specifiers for DECLARATOR, an error is
3401 issued. TYPE_LOCATION is the location of TYPE and is used
3402 for error reporting. */
3404 static void
3405 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3406 tree type, location_t type_location)
3408 /* [dcl.fct] forbids type definitions in return types.
3409 Unfortunately, it's not easy to know whether or not we are
3410 processing a return type until after the fact. */
3411 while (declarator
3412 && (declarator->kind == cdk_pointer
3413 || declarator->kind == cdk_reference
3414 || declarator->kind == cdk_ptrmem))
3415 declarator = declarator->declarator;
3416 if (declarator
3417 && declarator->kind == cdk_function)
3419 error_at (type_location,
3420 "new types may not be defined in a return type");
3421 inform (type_location,
3422 "(perhaps a semicolon is missing after the definition of %qT)",
3423 type);
3427 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3428 "<" in any valid C++ program. If the next token is indeed "<",
3429 issue a message warning the user about what appears to be an
3430 invalid attempt to form a template-id. LOCATION is the location
3431 of the type-specifier (TYPE) */
3433 static void
3434 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3435 tree type,
3436 enum tag_types tag_type,
3437 location_t location)
3439 cp_token_position start = 0;
3441 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3443 if (TREE_CODE (type) == TYPE_DECL)
3444 type = TREE_TYPE (type);
3445 if (TYPE_P (type) && !template_placeholder_p (type))
3446 error_at (location, "%qT is not a template", type);
3447 else if (identifier_p (type))
3449 if (tag_type != none_type)
3450 error_at (location, "%qE is not a class template", type);
3451 else
3452 error_at (location, "%qE is not a template", type);
3454 else
3455 error_at (location, "invalid template-id");
3456 /* Remember the location of the invalid "<". */
3457 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3458 start = cp_lexer_token_position (parser->lexer, true);
3459 /* Consume the "<". */
3460 cp_lexer_consume_token (parser->lexer);
3461 /* Parse the template arguments. */
3462 cp_parser_enclosed_template_argument_list (parser);
3463 /* Permanently remove the invalid template arguments so that
3464 this error message is not issued again. */
3465 if (start)
3466 cp_lexer_purge_tokens_after (parser->lexer, start);
3470 /* If parsing an integral constant-expression, issue an error message
3471 about the fact that THING appeared and return true. Otherwise,
3472 return false. In either case, set
3473 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3475 static bool
3476 cp_parser_non_integral_constant_expression (cp_parser *parser,
3477 non_integral_constant thing)
3479 parser->non_integral_constant_expression_p = true;
3480 if (parser->integral_constant_expression_p)
3482 if (!parser->allow_non_integral_constant_expression_p)
3484 const char *msg = NULL;
3485 switch (thing)
3487 case NIC_FLOAT:
3488 pedwarn (input_location, OPT_Wpedantic,
3489 "ISO C++ forbids using a floating-point literal "
3490 "in a constant-expression");
3491 return true;
3492 case NIC_CAST:
3493 error ("a cast to a type other than an integral or "
3494 "enumeration type cannot appear in a "
3495 "constant-expression");
3496 return true;
3497 case NIC_TYPEID:
3498 error ("%<typeid%> operator "
3499 "cannot appear in a constant-expression");
3500 return true;
3501 case NIC_NCC:
3502 error ("non-constant compound literals "
3503 "cannot appear in a constant-expression");
3504 return true;
3505 case NIC_FUNC_CALL:
3506 error ("a function call "
3507 "cannot appear in a constant-expression");
3508 return true;
3509 case NIC_INC:
3510 error ("an increment "
3511 "cannot appear in a constant-expression");
3512 return true;
3513 case NIC_DEC:
3514 error ("an decrement "
3515 "cannot appear in a constant-expression");
3516 return true;
3517 case NIC_ARRAY_REF:
3518 error ("an array reference "
3519 "cannot appear in a constant-expression");
3520 return true;
3521 case NIC_ADDR_LABEL:
3522 error ("the address of a label "
3523 "cannot appear in a constant-expression");
3524 return true;
3525 case NIC_OVERLOADED:
3526 error ("calls to overloaded operators "
3527 "cannot appear in a constant-expression");
3528 return true;
3529 case NIC_ASSIGNMENT:
3530 error ("an assignment cannot appear in a constant-expression");
3531 return true;
3532 case NIC_COMMA:
3533 error ("a comma operator "
3534 "cannot appear in a constant-expression");
3535 return true;
3536 case NIC_CONSTRUCTOR:
3537 error ("a call to a constructor "
3538 "cannot appear in a constant-expression");
3539 return true;
3540 case NIC_TRANSACTION:
3541 error ("a transaction expression "
3542 "cannot appear in a constant-expression");
3543 return true;
3544 case NIC_THIS:
3545 msg = "this";
3546 break;
3547 case NIC_FUNC_NAME:
3548 msg = "__FUNCTION__";
3549 break;
3550 case NIC_PRETTY_FUNC:
3551 msg = "__PRETTY_FUNCTION__";
3552 break;
3553 case NIC_C99_FUNC:
3554 msg = "__func__";
3555 break;
3556 case NIC_VA_ARG:
3557 msg = "va_arg";
3558 break;
3559 case NIC_ARROW:
3560 msg = "->";
3561 break;
3562 case NIC_POINT:
3563 msg = ".";
3564 break;
3565 case NIC_STAR:
3566 msg = "*";
3567 break;
3568 case NIC_ADDR:
3569 msg = "&";
3570 break;
3571 case NIC_PREINCREMENT:
3572 msg = "++";
3573 break;
3574 case NIC_PREDECREMENT:
3575 msg = "--";
3576 break;
3577 case NIC_NEW:
3578 msg = "new";
3579 break;
3580 case NIC_DEL:
3581 msg = "delete";
3582 break;
3583 default:
3584 gcc_unreachable ();
3586 if (msg)
3587 error ("%qs cannot appear in a constant-expression", msg);
3588 return true;
3591 return false;
3594 /* Emit a diagnostic for an invalid type name. This function commits
3595 to the current active tentative parse, if any. (Otherwise, the
3596 problematic construct might be encountered again later, resulting
3597 in duplicate error messages.) LOCATION is the location of ID. */
3599 static void
3600 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3601 location_t location)
3603 tree decl, ambiguous_decls;
3604 cp_parser_commit_to_tentative_parse (parser);
3605 /* Try to lookup the identifier. */
3606 decl = cp_parser_lookup_name (parser, id, none_type,
3607 /*is_template=*/false,
3608 /*is_namespace=*/false,
3609 /*check_dependency=*/true,
3610 &ambiguous_decls, location);
3611 if (ambiguous_decls)
3612 /* If the lookup was ambiguous, an error will already have
3613 been issued. */
3614 return;
3615 /* If the lookup found a template-name, it means that the user forgot
3616 to specify an argument list. Emit a useful error message. */
3617 if (DECL_TYPE_TEMPLATE_P (decl))
3619 auto_diagnostic_group d;
3620 error_at (location,
3621 "invalid use of template-name %qE without an argument list",
3622 decl);
3623 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3624 inform (location, "class template argument deduction is only available "
3625 "with %<-std=c++17%> or %<-std=gnu++17%>");
3626 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3628 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3629 error_at (location, "invalid use of destructor %qD as a type", id);
3630 else if (TREE_CODE (decl) == TYPE_DECL)
3631 /* Something like 'unsigned A a;' */
3632 error_at (location, "invalid combination of multiple type-specifiers");
3633 else if (!parser->scope)
3635 /* Issue an error message. */
3636 auto_diagnostic_group d;
3637 name_hint hint;
3638 if (TREE_CODE (id) == IDENTIFIER_NODE)
3639 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3640 if (const char *suggestion = hint.suggestion ())
3642 gcc_rich_location richloc (location);
3643 richloc.add_fixit_replace (suggestion);
3644 error_at (&richloc,
3645 "%qE does not name a type; did you mean %qs?",
3646 id, suggestion);
3648 else
3649 error_at (location, "%qE does not name a type", id);
3650 /* If we're in a template class, it's possible that the user was
3651 referring to a type from a base class. For example:
3653 template <typename T> struct A { typedef T X; };
3654 template <typename T> struct B : public A<T> { X x; };
3656 The user should have said "typename A<T>::X". */
3657 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3658 inform (location, "C++11 %<constexpr%> only available with "
3659 "%<-std=c++11%> or %<-std=gnu++11%>");
3660 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3661 inform (location, "C++11 %<noexcept%> only available with "
3662 "%<-std=c++11%> or %<-std=gnu++11%>");
3663 else if (TREE_CODE (id) == IDENTIFIER_NODE
3664 && (id_equal (id, "module") || id_equal (id, "import")))
3666 if (modules_p ())
3667 inform (location, "%qE is not recognized as a module control-line",
3668 id);
3669 else if (cxx_dialect < cxx20)
3670 inform (location, "C++20 %qE only available with %<-fmodules-ts%>",
3671 id);
3672 else
3673 inform (location, "C++20 %qE only available with %<-fmodules-ts%>"
3674 ", which is not yet enabled with %<-std=c++20%>", id);
3676 else if (cxx_dialect < cxx11
3677 && TREE_CODE (id) == IDENTIFIER_NODE
3678 && id_equal (id, "thread_local"))
3679 inform (location, "C++11 %<thread_local%> only available with "
3680 "%<-std=c++11%> or %<-std=gnu++11%>");
3681 else if (cxx_dialect < cxx20 && id == ridpointers[(int)RID_CONSTINIT])
3682 inform (location, "C++20 %<constinit%> only available with "
3683 "%<-std=c++20%> or %<-std=gnu++20%>");
3684 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3685 inform (location, "%<concept%> only available with %<-std=c++20%> or "
3686 "%<-fconcepts%>");
3687 else if (!flag_concepts && id == ridpointers[(int)RID_REQUIRES])
3688 inform (location, "%<requires%> only available with %<-std=c++20%> or "
3689 "%<-fconcepts%>");
3690 else if (processing_template_decl && current_class_type
3691 && TYPE_BINFO (current_class_type))
3693 for (tree b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3694 b; b = TREE_CHAIN (b))
3696 tree base_type = BINFO_TYPE (b);
3697 if (CLASS_TYPE_P (base_type)
3698 && dependent_type_p (base_type))
3700 /* Go from a particular instantiation of the
3701 template (which will have an empty TYPE_FIELDs),
3702 to the main version. */
3703 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3704 for (tree field = TYPE_FIELDS (base_type);
3705 field; field = DECL_CHAIN (field))
3706 if (TREE_CODE (field) == TYPE_DECL
3707 && DECL_NAME (field) == id)
3709 inform (location,
3710 "(perhaps %<typename %T::%E%> was intended)",
3711 BINFO_TYPE (b), id);
3712 goto found;
3716 found:;
3719 /* Here we diagnose qualified-ids where the scope is actually correct,
3720 but the identifier does not resolve to a valid type name. */
3721 else if (parser->scope != error_mark_node)
3723 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3725 auto_diagnostic_group d;
3726 name_hint hint;
3727 if (decl == error_mark_node)
3728 hint = suggest_alternative_in_explicit_scope (location, id,
3729 parser->scope);
3730 const char *suggestion = hint.suggestion ();
3731 gcc_rich_location richloc (location_of (id));
3732 if (suggestion)
3733 richloc.add_fixit_replace (suggestion);
3734 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3736 if (suggestion)
3737 error_at (&richloc,
3738 "%qE in namespace %qE does not name a template"
3739 " type; did you mean %qs?",
3740 id, parser->scope, suggestion);
3741 else
3742 error_at (&richloc,
3743 "%qE in namespace %qE does not name a template type",
3744 id, parser->scope);
3746 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3748 if (suggestion)
3749 error_at (&richloc,
3750 "%qE in namespace %qE does not name a template"
3751 " type; did you mean %qs?",
3752 TREE_OPERAND (id, 0), parser->scope, suggestion);
3753 else
3754 error_at (&richloc,
3755 "%qE in namespace %qE does not name a template"
3756 " type",
3757 TREE_OPERAND (id, 0), parser->scope);
3759 else
3761 if (suggestion)
3762 error_at (&richloc,
3763 "%qE in namespace %qE does not name a type"
3764 "; did you mean %qs?",
3765 id, parser->scope, suggestion);
3766 else
3767 error_at (&richloc,
3768 "%qE in namespace %qE does not name a type",
3769 id, parser->scope);
3771 if (DECL_P (decl))
3772 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3774 else if (CLASS_TYPE_P (parser->scope)
3775 && constructor_name_p (id, parser->scope))
3777 /* A<T>::A<T>() */
3778 auto_diagnostic_group d;
3779 error_at (location, "%<%T::%E%> names the constructor, not"
3780 " the type", parser->scope, id);
3781 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3782 error_at (location, "and %qT has no template constructors",
3783 parser->scope);
3785 else if (TYPE_P (parser->scope)
3786 && dependent_scope_p (parser->scope))
3788 gcc_rich_location richloc (location);
3789 richloc.add_fixit_insert_before ("typename ");
3790 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3791 error_at (&richloc,
3792 "need %<typename%> before %<%T::%D::%E%> because "
3793 "%<%T::%D%> is a dependent scope",
3794 TYPE_CONTEXT (parser->scope),
3795 TYPENAME_TYPE_FULLNAME (parser->scope),
3797 TYPE_CONTEXT (parser->scope),
3798 TYPENAME_TYPE_FULLNAME (parser->scope));
3799 else
3800 error_at (&richloc, "need %<typename%> before %<%T::%E%> because "
3801 "%qT is a dependent scope",
3802 parser->scope, id, parser->scope);
3804 else if (TYPE_P (parser->scope))
3806 auto_diagnostic_group d;
3807 if (!COMPLETE_TYPE_P (parser->scope))
3808 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3809 parser->scope);
3810 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3811 error_at (location_of (id),
3812 "%qE in %q#T does not name a template type",
3813 id, parser->scope);
3814 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3815 error_at (location_of (id),
3816 "%qE in %q#T does not name a template type",
3817 TREE_OPERAND (id, 0), parser->scope);
3818 else
3819 error_at (location_of (id),
3820 "%qE in %q#T does not name a type",
3821 id, parser->scope);
3822 if (DECL_P (decl))
3823 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3825 else
3826 gcc_unreachable ();
3830 /* Check for a common situation where a type-name should be present,
3831 but is not, and issue a sensible error message. Returns true if an
3832 invalid type-name was detected.
3834 The situation handled by this function are variable declarations of the
3835 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3836 Usually, `ID' should name a type, but if we got here it means that it
3837 does not. We try to emit the best possible error message depending on
3838 how exactly the id-expression looks like. */
3840 static bool
3841 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3843 tree id;
3844 cp_token *token = cp_lexer_peek_token (parser->lexer);
3846 /* Avoid duplicate error about ambiguous lookup. */
3847 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3849 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3850 if (next->type == CPP_NAME && next->error_reported)
3851 goto out;
3854 cp_parser_parse_tentatively (parser);
3855 id = cp_parser_id_expression (parser,
3856 /*template_keyword_p=*/false,
3857 /*check_dependency_p=*/true,
3858 /*template_p=*/NULL,
3859 /*declarator_p=*/true,
3860 /*optional_p=*/false);
3861 /* If the next token is a (, this is a function with no explicit return
3862 type, i.e. constructor, destructor or conversion op. */
3863 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3864 || TREE_CODE (id) == TYPE_DECL)
3866 cp_parser_abort_tentative_parse (parser);
3867 return false;
3869 if (!cp_parser_parse_definitely (parser))
3870 return false;
3872 /* Emit a diagnostic for the invalid type. */
3873 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3874 out:
3875 /* If we aren't in the middle of a declarator (i.e. in a
3876 parameter-declaration-clause), skip to the end of the declaration;
3877 there's no point in trying to process it. */
3878 if (!parser->in_declarator_p)
3879 cp_parser_skip_to_end_of_block_or_statement (parser);
3880 return true;
3883 /* Consume tokens up to, and including, the next non-nested closing `)'.
3884 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3885 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3886 found an unnested token of that type. */
3888 static int
3889 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3890 bool recovering,
3891 cpp_ttype or_ttype,
3892 bool consume_paren)
3894 unsigned paren_depth = 0;
3895 unsigned brace_depth = 0;
3896 unsigned square_depth = 0;
3897 unsigned condop_depth = 0;
3899 if (recovering && or_ttype == CPP_EOF
3900 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3901 return 0;
3903 while (true)
3905 cp_token * token = cp_lexer_peek_token (parser->lexer);
3907 /* Have we found what we're looking for before the closing paren? */
3908 if (token->type == or_ttype && or_ttype != CPP_EOF
3909 && !brace_depth && !paren_depth && !square_depth && !condop_depth)
3910 return -1;
3912 switch (token->type)
3914 case CPP_PRAGMA_EOL:
3915 if (!parser->lexer->in_pragma)
3916 break;
3917 /* FALLTHRU */
3918 case CPP_EOF:
3919 /* If we've run out of tokens, then there is no closing `)'. */
3920 return 0;
3922 /* This is good for lambda expression capture-lists. */
3923 case CPP_OPEN_SQUARE:
3924 ++square_depth;
3925 break;
3926 case CPP_CLOSE_SQUARE:
3927 if (!square_depth--)
3928 return 0;
3929 break;
3931 case CPP_SEMICOLON:
3932 /* This matches the processing in skip_to_end_of_statement. */
3933 if (!brace_depth)
3934 return 0;
3935 break;
3937 case CPP_OPEN_BRACE:
3938 ++brace_depth;
3939 break;
3940 case CPP_CLOSE_BRACE:
3941 if (!brace_depth--)
3942 return 0;
3943 break;
3945 case CPP_OPEN_PAREN:
3946 if (!brace_depth)
3947 ++paren_depth;
3948 break;
3950 case CPP_CLOSE_PAREN:
3951 if (!brace_depth && !paren_depth--)
3953 if (consume_paren)
3954 cp_lexer_consume_token (parser->lexer);
3955 return 1;
3957 break;
3959 case CPP_QUERY:
3960 if (!brace_depth && !paren_depth && !square_depth)
3961 ++condop_depth;
3962 break;
3964 case CPP_COLON:
3965 if (!brace_depth && !paren_depth && !square_depth && condop_depth > 0)
3966 condop_depth--;
3967 break;
3969 case CPP_KEYWORD:
3970 if (!cp_token_is_module_directive (token))
3971 break;
3972 /* FALLTHROUGH */
3974 case CPP_PRAGMA:
3975 /* We fell into a pragma. Skip it, and continue. */
3976 cp_parser_skip_to_pragma_eol (parser, recovering ? token : nullptr);
3977 continue;
3979 default:
3980 break;
3983 /* Consume the token. */
3984 cp_lexer_consume_token (parser->lexer);
3988 /* Consume tokens up to, and including, the next non-nested closing `)'.
3989 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3990 are doing error recovery. Returns -1 if OR_COMMA is true and we
3991 found an unnested token of that type. */
3993 static int
3994 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3995 bool recovering,
3996 bool or_comma,
3997 bool consume_paren)
3999 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
4000 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
4001 ttype, consume_paren);
4004 /* Consume tokens until we reach the end of the current statement.
4005 Normally, that will be just before consuming a `;'. However, if a
4006 non-nested `}' comes first, then we stop before consuming that. */
4008 static void
4009 cp_parser_skip_to_end_of_statement (cp_parser* parser)
4011 unsigned nesting_depth = 0;
4013 /* Unwind generic function template scope if necessary. */
4014 if (parser->fully_implicit_function_template_p)
4015 abort_fully_implicit_template (parser);
4017 while (true)
4019 cp_token *token = cp_lexer_peek_token (parser->lexer);
4021 switch (token->type)
4023 case CPP_PRAGMA_EOL:
4024 if (!parser->lexer->in_pragma)
4025 break;
4026 /* FALLTHRU */
4027 case CPP_EOF:
4028 /* If we've run out of tokens, stop. */
4029 return;
4031 case CPP_SEMICOLON:
4032 /* If the next token is a `;', we have reached the end of the
4033 statement. */
4034 if (!nesting_depth)
4035 return;
4036 break;
4038 case CPP_CLOSE_BRACE:
4039 /* If this is a non-nested '}', stop before consuming it.
4040 That way, when confronted with something like:
4042 { 3 + }
4044 we stop before consuming the closing '}', even though we
4045 have not yet reached a `;'. */
4046 if (nesting_depth == 0)
4047 return;
4049 /* If it is the closing '}' for a block that we have
4050 scanned, stop -- but only after consuming the token.
4051 That way given:
4053 void f g () { ... }
4054 typedef int I;
4056 we will stop after the body of the erroneously declared
4057 function, but before consuming the following `typedef'
4058 declaration. */
4059 if (--nesting_depth == 0)
4061 cp_lexer_consume_token (parser->lexer);
4062 return;
4064 break;
4066 case CPP_OPEN_BRACE:
4067 ++nesting_depth;
4068 break;
4070 case CPP_KEYWORD:
4071 if (!cp_token_is_module_directive (token))
4072 break;
4073 /* FALLTHROUGH */
4075 case CPP_PRAGMA:
4076 /* We fell into a pragma. Skip it, and continue or return. */
4077 cp_parser_skip_to_pragma_eol (parser, token);
4078 if (!nesting_depth)
4079 return;
4080 continue;
4082 default:
4083 break;
4086 /* Consume the token. */
4087 cp_lexer_consume_token (parser->lexer);
4091 /* This function is called at the end of a statement or declaration.
4092 If the next token is a semicolon, it is consumed; otherwise, error
4093 recovery is attempted. */
4095 static void
4096 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
4098 /* Look for the trailing `;'. */
4099 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
4101 /* If there is additional (erroneous) input, skip to the end of
4102 the statement. */
4103 cp_parser_skip_to_end_of_statement (parser);
4104 /* If the next token is now a `;', consume it. */
4105 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
4106 cp_lexer_consume_token (parser->lexer);
4110 /* Skip tokens until we have consumed an entire block, or until we
4111 have consumed a non-nested `;'. */
4113 static void
4114 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
4116 int nesting_depth = 0;
4118 /* Unwind generic function template scope if necessary. */
4119 if (parser->fully_implicit_function_template_p)
4120 abort_fully_implicit_template (parser);
4122 while (nesting_depth >= 0)
4124 cp_token *token = cp_lexer_peek_token (parser->lexer);
4126 switch (token->type)
4128 case CPP_PRAGMA_EOL:
4129 if (!parser->lexer->in_pragma)
4130 break;
4131 /* FALLTHRU */
4132 case CPP_EOF:
4133 /* If we've run out of tokens, stop. */
4134 return;
4136 case CPP_SEMICOLON:
4137 /* Stop if this is an unnested ';'. */
4138 if (!nesting_depth)
4139 nesting_depth = -1;
4140 break;
4142 case CPP_CLOSE_BRACE:
4143 /* Stop if this is an unnested '}', or closes the outermost
4144 nesting level. */
4145 nesting_depth--;
4146 if (nesting_depth < 0)
4147 return;
4148 if (!nesting_depth)
4149 nesting_depth = -1;
4150 break;
4152 case CPP_OPEN_BRACE:
4153 /* Nest. */
4154 nesting_depth++;
4155 break;
4157 case CPP_KEYWORD:
4158 if (!cp_token_is_module_directive (token))
4159 break;
4160 /* FALLTHROUGH */
4162 case CPP_PRAGMA:
4163 /* Skip it, and continue or return. */
4164 cp_parser_skip_to_pragma_eol (parser, token);
4165 if (!nesting_depth)
4166 return;
4167 continue;
4169 default:
4170 break;
4173 /* Consume the token. */
4174 cp_lexer_consume_token (parser->lexer);
4178 /* Skip tokens until a non-nested closing curly brace is the next
4179 token, or there are no more tokens. Return true in the first case,
4180 false otherwise. */
4182 static bool
4183 cp_parser_skip_to_closing_brace (cp_parser *parser)
4185 unsigned nesting_depth = 0;
4187 while (true)
4189 cp_token *token = cp_lexer_peek_token (parser->lexer);
4191 switch (token->type)
4193 case CPP_PRAGMA_EOL:
4194 if (!parser->lexer->in_pragma)
4195 break;
4196 /* FALLTHRU */
4197 case CPP_EOF:
4198 /* If we've run out of tokens, stop. */
4199 return false;
4201 case CPP_CLOSE_BRACE:
4202 /* If the next token is a non-nested `}', then we have reached
4203 the end of the current block. */
4204 if (nesting_depth-- == 0)
4205 return true;
4206 break;
4208 case CPP_OPEN_BRACE:
4209 /* If it the next token is a `{', then we are entering a new
4210 block. Consume the entire block. */
4211 ++nesting_depth;
4212 break;
4214 default:
4215 break;
4218 /* Consume the token. */
4219 cp_lexer_consume_token (parser->lexer);
4223 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
4224 parameter is the PRAGMA token, allowing us to purge the entire pragma
4225 sequence. PRAGMA_TOK can be NULL, if we're speculatively scanning
4226 forwards (not error recovery). */
4228 static void
4229 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
4231 cp_token *token;
4235 /* The preprocessor makes sure that a PRAGMA_EOL token appears
4236 before an EOF token, even when the EOF is on the pragma line.
4237 We should never get here without being inside a deferred
4238 pragma. */
4239 gcc_checking_assert (cp_lexer_next_token_is_not (parser->lexer, CPP_EOF));
4240 token = cp_lexer_consume_token (parser->lexer);
4242 while (token->type != CPP_PRAGMA_EOL);
4244 if (pragma_tok)
4246 parser->lexer->in_pragma = false;
4247 if (parser->lexer->in_omp_attribute_pragma
4248 && cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4250 parser->lexer = parser->lexer->next;
4251 /* Put the current source position back where it was before this
4252 lexer was pushed. */
4253 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4258 /* Require pragma end of line, resyncing with it as necessary. The
4259 arguments are as for cp_parser_skip_to_pragma_eol. */
4261 static void
4262 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
4264 parser->lexer->in_pragma = false;
4265 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
4266 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
4267 else if (parser->lexer->in_omp_attribute_pragma
4268 && cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4270 parser->lexer = parser->lexer->next;
4271 /* Put the current source position back where it was before this
4272 lexer was pushed. */
4273 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4277 /* This is a simple wrapper around make_typename_type. When the id is
4278 an unresolved identifier node, we can provide a superior diagnostic
4279 using cp_parser_diagnose_invalid_type_name. */
4281 static tree
4282 cp_parser_make_typename_type (cp_parser *parser, tree id,
4283 location_t id_location)
4285 tree result;
4286 if (identifier_p (id))
4288 result = make_typename_type (parser->scope, id, typename_type,
4289 /*complain=*/tf_none);
4290 if (result == error_mark_node)
4291 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
4292 return result;
4294 return make_typename_type (parser->scope, id, typename_type, tf_error);
4297 /* This is a wrapper around the
4298 make_{pointer,ptrmem,reference}_declarator functions that decides
4299 which one to call based on the CODE and CLASS_TYPE arguments. The
4300 CODE argument should be one of the values returned by
4301 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
4302 appertain to the pointer or reference. */
4304 static cp_declarator *
4305 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
4306 cp_cv_quals cv_qualifiers,
4307 cp_declarator *target,
4308 tree attributes)
4310 if (code == ERROR_MARK || target == cp_error_declarator)
4311 return cp_error_declarator;
4313 if (code == INDIRECT_REF)
4314 if (class_type == NULL_TREE)
4315 return make_pointer_declarator (cv_qualifiers, target, attributes);
4316 else
4317 return make_ptrmem_declarator (cv_qualifiers, class_type,
4318 target, attributes);
4319 else if (code == ADDR_EXPR && class_type == NULL_TREE)
4320 return make_reference_declarator (cv_qualifiers, target,
4321 false, attributes);
4322 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
4323 return make_reference_declarator (cv_qualifiers, target,
4324 true, attributes);
4325 gcc_unreachable ();
4328 /* Create a new C++ parser. */
4330 static cp_parser *
4331 cp_parser_new (cp_lexer *lexer)
4333 /* Initialize the binops_by_token so that we can get the tree
4334 directly from the token. */
4335 for (unsigned i = 0; i < ARRAY_SIZE (binops); i++)
4336 binops_by_token[binops[i].token_type] = binops[i];
4338 cp_parser *parser = ggc_cleared_alloc<cp_parser> ();
4339 parser->lexer = lexer;
4340 parser->context = cp_parser_context_new (NULL);
4342 /* For now, we always accept GNU extensions. */
4343 parser->allow_gnu_extensions_p = 1;
4345 /* The `>' token is a greater-than operator, not the end of a
4346 template-id. */
4347 parser->greater_than_is_operator_p = true;
4349 parser->default_arg_ok_p = true;
4351 /* We are not parsing a constant-expression. */
4352 parser->integral_constant_expression_p = false;
4353 parser->allow_non_integral_constant_expression_p = false;
4354 parser->non_integral_constant_expression_p = false;
4356 /* Local variable names are not forbidden. */
4357 parser->local_variables_forbidden_p = 0;
4359 /* We are not processing an `extern "C"' declaration. */
4360 parser->in_unbraced_linkage_specification_p = false;
4362 /* We are not processing a declarator. */
4363 parser->in_declarator_p = false;
4365 /* We are not processing a template-argument-list. */
4366 parser->in_template_argument_list_p = false;
4368 /* We are not in an iteration statement. */
4369 parser->in_statement = 0;
4371 /* We are not in a switch statement. */
4372 parser->in_switch_statement_p = false;
4374 /* We are not parsing a type-id inside an expression. */
4375 parser->in_type_id_in_expr_p = false;
4377 /* String literals should be translated to the execution character set. */
4378 parser->translate_strings_p = true;
4380 /* We are not parsing a function body. */
4381 parser->in_function_body = false;
4383 /* We can correct until told otherwise. */
4384 parser->colon_corrects_to_scope_p = true;
4386 /* The unparsed function queue is empty. */
4387 push_unparsed_function_queues (parser);
4389 /* There are no classes being defined. */
4390 parser->num_classes_being_defined = 0;
4392 /* No template parameters apply. */
4393 parser->num_template_parameter_lists = 0;
4395 /* Special parsing data structures. */
4396 parser->omp_declare_simd = NULL;
4397 parser->oacc_routine = NULL;
4399 /* Not declaring an implicit function template. */
4400 parser->auto_is_implicit_function_template_parm_p = false;
4401 parser->fully_implicit_function_template_p = false;
4402 parser->implicit_template_parms = 0;
4403 parser->implicit_template_scope = 0;
4405 /* Allow constrained-type-specifiers. */
4406 parser->prevent_constrained_type_specifiers = 0;
4408 /* We haven't yet seen an 'extern "C"'. */
4409 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
4411 return parser;
4414 /* Create a cp_lexer structure which will emit the tokens in CACHE
4415 and push it onto the parser's lexer stack. This is used for delayed
4416 parsing of in-class method bodies and default arguments, and should
4417 not be confused with tentative parsing. */
4418 static void
4419 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
4421 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
4422 lexer->next = parser->lexer;
4423 parser->lexer = lexer;
4425 /* Move the current source position to that of the first token in the
4426 new lexer. */
4427 cp_lexer_set_source_position_from_token (lexer->next_token);
4430 /* Pop the top lexer off the parser stack. This is never used for the
4431 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
4432 static void
4433 cp_parser_pop_lexer (cp_parser *parser)
4435 cp_lexer *lexer = parser->lexer;
4436 parser->lexer = lexer->next;
4437 cp_lexer_destroy (lexer);
4439 /* Put the current source position back where it was before this
4440 lexer was pushed. */
4441 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4444 /* Lexical conventions [gram.lex] */
4446 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
4447 identifier. */
4449 static cp_expr
4450 cp_parser_identifier (cp_parser* parser)
4452 cp_token *token;
4454 /* Look for the identifier. */
4455 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
4456 /* Return the value. */
4457 if (token)
4458 return cp_expr (token->u.value, token->location);
4459 else
4460 return error_mark_node;
4463 /* Worker for cp_parser_string_literal and cp_parser_userdef_string_literal.
4464 Do not call this directly; use either of the above.
4466 Parse a sequence of adjacent string constants. Return a
4467 TREE_STRING representing the combined, nul-terminated string
4468 constant. If TRANSLATE is true, translate the string to the
4469 execution character set. If WIDE_OK is true, a wide string is
4470 valid here. If UDL_OK is true, a string literal with user-defined
4471 suffix can be used in this context.
4473 C++98 [lex.string] says that if a narrow string literal token is
4474 adjacent to a wide string literal token, the behavior is undefined.
4475 However, C99 6.4.5p4 says that this results in a wide string literal.
4476 We follow C99 here, for consistency with the C front end.
4478 This code is largely lifted from lex_string() in c-lex.cc.
4480 FUTURE: ObjC++ will need to handle @-strings here. */
4482 static cp_expr
4483 cp_parser_string_literal_common (cp_parser *parser, bool translate,
4484 bool wide_ok, bool udl_ok,
4485 bool lookup_udlit)
4487 tree value;
4488 size_t count;
4489 struct obstack str_ob;
4490 struct obstack loc_ob;
4491 cpp_string str, istr, *strs;
4492 cp_token *tok;
4493 enum cpp_ttype type, curr_type;
4494 int have_suffix_p = 0;
4495 tree string_tree;
4496 tree suffix_id = NULL_TREE;
4497 bool curr_tok_is_userdef_p = false;
4499 tok = cp_lexer_peek_token (parser->lexer);
4500 if (!cp_parser_is_string_literal (tok))
4502 cp_parser_error (parser, "expected string-literal");
4503 return error_mark_node;
4506 location_t loc = tok->location;
4508 if (cpp_userdef_string_p (tok->type))
4510 if (!udl_ok)
4512 error_at (loc, "string literal with user-defined suffix "
4513 "is invalid in this context");
4514 return error_mark_node;
4516 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4517 curr_type = cpp_userdef_string_remove_type (tok->type);
4518 curr_tok_is_userdef_p = true;
4520 else
4522 string_tree = tok->u.value;
4523 curr_type = tok->type;
4525 type = curr_type;
4527 /* Try to avoid the overhead of creating and destroying an obstack
4528 for the common case of just one string. */
4529 if (!cp_parser_is_string_literal
4530 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4532 cp_lexer_consume_token (parser->lexer);
4534 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4535 str.len = TREE_STRING_LENGTH (string_tree);
4536 count = 1;
4538 if (curr_tok_is_userdef_p)
4540 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4541 have_suffix_p = 1;
4542 curr_type = cpp_userdef_string_remove_type (tok->type);
4544 else
4545 curr_type = tok->type;
4547 strs = &str;
4549 else
4551 location_t last_tok_loc = tok->location;
4552 gcc_obstack_init (&str_ob);
4553 gcc_obstack_init (&loc_ob);
4554 count = 0;
4558 cp_lexer_consume_token (parser->lexer);
4559 count++;
4560 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4561 str.len = TREE_STRING_LENGTH (string_tree);
4563 if (curr_tok_is_userdef_p)
4565 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4566 if (have_suffix_p == 0)
4568 suffix_id = curr_suffix_id;
4569 have_suffix_p = 1;
4571 else if (have_suffix_p == 1
4572 && curr_suffix_id != suffix_id)
4574 error ("inconsistent user-defined literal suffixes"
4575 " %qD and %qD in string literal",
4576 suffix_id, curr_suffix_id);
4577 have_suffix_p = -1;
4579 curr_type = cpp_userdef_string_remove_type (tok->type);
4581 else
4582 curr_type = tok->type;
4584 if (type != curr_type)
4586 if (type == CPP_STRING)
4587 type = curr_type;
4588 else if (curr_type != CPP_STRING)
4590 rich_location rich_loc (line_table, tok->location);
4591 rich_loc.add_range (last_tok_loc);
4592 error_at (&rich_loc,
4593 "concatenation of string literals with "
4594 "conflicting encoding prefixes");
4598 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4599 obstack_grow (&loc_ob, &tok->location, sizeof (location_t));
4601 last_tok_loc = tok->location;
4603 tok = cp_lexer_peek_token (parser->lexer);
4604 if (cpp_userdef_string_p (tok->type))
4606 if (!udl_ok)
4608 error_at (loc, "string literal with user-defined suffix "
4609 "is invalid in this context");
4610 return error_mark_node;
4612 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4613 curr_type = cpp_userdef_string_remove_type (tok->type);
4614 curr_tok_is_userdef_p = true;
4616 else
4618 string_tree = tok->u.value;
4619 curr_type = tok->type;
4620 curr_tok_is_userdef_p = false;
4623 while (cp_parser_is_string_literal (tok));
4625 /* A string literal built by concatenation has its caret=start at
4626 the start of the initial string, and its finish at the finish of
4627 the final string literal. */
4628 loc = make_location (loc, loc, get_finish (last_tok_loc));
4630 strs = (cpp_string *) obstack_finish (&str_ob);
4633 if (type != CPP_STRING && !wide_ok)
4635 cp_parser_error (parser, "a wide string is invalid in this context");
4636 type = CPP_STRING;
4639 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4640 (parse_in, strs, count, &istr, type))
4642 value = build_string (istr.len, (const char *)istr.text);
4643 free (CONST_CAST (unsigned char *, istr.text));
4644 if (count > 1)
4646 location_t *locs = (location_t *)obstack_finish (&loc_ob);
4647 gcc_assert (g_string_concat_db);
4648 g_string_concat_db->record_string_concatenation (count, locs);
4651 switch (type)
4653 default:
4654 case CPP_STRING:
4655 TREE_TYPE (value) = char_array_type_node;
4656 break;
4657 case CPP_UTF8STRING:
4658 if (flag_char8_t)
4659 TREE_TYPE (value) = char8_array_type_node;
4660 else
4661 TREE_TYPE (value) = char_array_type_node;
4662 break;
4663 case CPP_STRING16:
4664 TREE_TYPE (value) = char16_array_type_node;
4665 break;
4666 case CPP_STRING32:
4667 TREE_TYPE (value) = char32_array_type_node;
4668 break;
4669 case CPP_WSTRING:
4670 TREE_TYPE (value) = wchar_array_type_node;
4671 break;
4674 value = fix_string_type (value);
4676 if (have_suffix_p)
4678 tree literal = build_userdef_literal (suffix_id, value,
4679 OT_NONE, NULL_TREE);
4680 if (lookup_udlit)
4681 value = finish_userdef_string_literal (literal);
4682 else
4683 value = literal;
4686 else
4687 /* cpp_interpret_string has issued an error. */
4688 value = error_mark_node;
4690 if (count > 1)
4692 obstack_free (&str_ob, 0);
4693 obstack_free (&loc_ob, 0);
4696 return cp_expr (value, loc);
4699 /* Parse a sequence of adjacent string constants. Return a TREE_STRING
4700 representing the combined, nul-terminated string constant. If
4701 TRANSLATE is true, translate the string to the execution character set.
4702 If WIDE_OK is true, a wide string is valid here.
4704 This function issues an error if a user defined string literal is
4705 encountered; use cp_parser_userdef_string_literal if UDLs are allowed. */
4707 static inline cp_expr
4708 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
4710 return cp_parser_string_literal_common (parser, translate, wide_ok,
4711 /*udl_ok=*/false,
4712 /*lookup_udlit=*/false);
4715 /* Parse a string literal or user defined string literal.
4717 user-defined-string-literal :
4718 string-literal ud-suffix
4720 If LOOKUP_UDLIT, perform a lookup for a suitable template function. */
4722 static inline cp_expr
4723 cp_parser_userdef_string_literal (cp_parser *parser, bool lookup_udlit)
4725 return cp_parser_string_literal_common (parser, /*translate=*/true,
4726 /*wide_ok=*/true, /*udl_ok=*/true,
4727 lookup_udlit);
4730 /* Look up a literal operator with the name and the exact arguments. */
4732 static tree
4733 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4735 tree decl = lookup_name (name);
4736 if (!decl || !is_overloaded_fn (decl))
4737 return error_mark_node;
4739 for (lkp_iterator iter (decl); iter; ++iter)
4741 tree fn = *iter;
4743 if (tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn)))
4745 unsigned int ix;
4746 bool found = true;
4748 for (ix = 0;
4749 found && ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4750 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4752 tree tparm = TREE_VALUE (parmtypes);
4753 tree targ = TREE_TYPE ((*args)[ix]);
4754 bool ptr = TYPE_PTR_P (tparm);
4755 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4756 if ((ptr || arr || !same_type_p (tparm, targ))
4757 && (!ptr || !arr
4758 || !same_type_p (TREE_TYPE (tparm),
4759 TREE_TYPE (targ))))
4760 found = false;
4763 if (found
4764 && ix == vec_safe_length (args)
4765 /* May be this should be sufficient_parms_p instead,
4766 depending on how exactly should user-defined literals
4767 work in presence of default arguments on the literal
4768 operator parameters. */
4769 && parmtypes == void_list_node)
4770 return decl;
4774 return error_mark_node;
4777 /* Parse a user-defined char constant. Returns a call to a user-defined
4778 literal operator taking the character as an argument. */
4780 static cp_expr
4781 cp_parser_userdef_char_literal (cp_parser *parser)
4783 cp_token *token = cp_lexer_consume_token (parser->lexer);
4784 tree literal = token->u.value;
4785 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4786 tree value = USERDEF_LITERAL_VALUE (literal);
4787 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4788 tree decl, result;
4790 /* Build up a call to the user-defined operator */
4791 /* Lookup the name we got back from the id-expression. */
4792 releasing_vec args;
4793 vec_safe_push (args, value);
4794 decl = lookup_literal_operator (name, args);
4795 if (!decl || decl == error_mark_node)
4797 error ("unable to find character literal operator %qD with %qT argument",
4798 name, TREE_TYPE (value));
4799 return error_mark_node;
4801 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4802 return result;
4805 /* A subroutine of cp_parser_userdef_numeric_literal to
4806 create a char... template parameter pack from a string node. */
4808 static tree
4809 make_char_string_pack (tree value)
4811 tree charvec;
4812 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4813 const unsigned char *str
4814 = (const unsigned char *) TREE_STRING_POINTER (value);
4815 int i, len = TREE_STRING_LENGTH (value) - 1;
4816 tree argvec = make_tree_vec (1);
4818 /* Fill in CHARVEC with all of the parameters. */
4819 charvec = make_tree_vec (len);
4820 for (i = 0; i < len; ++i)
4822 unsigned char s[3] = { '\'', str[i], '\'' };
4823 cpp_string in = { 3, s };
4824 cpp_string out = { 0, 0 };
4825 if (!cpp_interpret_string (parse_in, &in, 1, &out, CPP_STRING))
4826 return NULL_TREE;
4827 gcc_assert (out.len == 2);
4828 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node,
4829 out.text[0]);
4832 /* Build the argument packs. */
4833 ARGUMENT_PACK_ARGS (argpack) = charvec;
4835 TREE_VEC_ELT (argvec, 0) = argpack;
4837 return argvec;
4840 /* A subroutine of cp_parser_userdef_numeric_literal to
4841 create a char... template parameter pack from a string node. */
4843 static tree
4844 make_string_pack (tree value)
4846 tree charvec;
4847 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4848 const unsigned char *str
4849 = (const unsigned char *) TREE_STRING_POINTER (value);
4850 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4851 int len = TREE_STRING_LENGTH (value) / sz - 1;
4852 tree argvec = make_tree_vec (2);
4854 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4855 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4857 /* First template parm is character type. */
4858 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4860 /* Fill in CHARVEC with all of the parameters. */
4861 charvec = make_tree_vec (len);
4862 for (int i = 0; i < len; ++i)
4863 TREE_VEC_ELT (charvec, i)
4864 = double_int_to_tree (str_char_type_node,
4865 double_int::from_buffer (str + i * sz, sz));
4867 /* Build the argument packs. */
4868 ARGUMENT_PACK_ARGS (argpack) = charvec;
4870 TREE_VEC_ELT (argvec, 1) = argpack;
4872 return argvec;
4875 /* Parse a user-defined numeric constant. returns a call to a user-defined
4876 literal operator. */
4878 static cp_expr
4879 cp_parser_userdef_numeric_literal (cp_parser *parser)
4881 cp_token *token = cp_lexer_consume_token (parser->lexer);
4882 tree literal = token->u.value;
4883 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4884 tree value = USERDEF_LITERAL_VALUE (literal);
4885 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4886 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4887 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4888 tree decl, result;
4890 /* Look for a literal operator taking the exact type of numeric argument
4891 as the literal value. */
4892 releasing_vec args;
4893 vec_safe_push (args, value);
4894 decl = lookup_literal_operator (name, args);
4895 if (decl && decl != error_mark_node)
4897 result = finish_call_expr (decl, &args, false, true,
4898 tf_warning_or_error);
4900 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4902 warning_at (token->location, OPT_Woverflow,
4903 "integer literal exceeds range of %qT type",
4904 long_long_unsigned_type_node);
4906 else
4908 if (overflow > 0)
4909 warning_at (token->location, OPT_Woverflow,
4910 "floating literal exceeds range of %qT type",
4911 long_double_type_node);
4912 else if (overflow < 0)
4913 warning_at (token->location, OPT_Woverflow,
4914 "floating literal truncated to zero");
4917 return result;
4920 /* If the numeric argument didn't work, look for a raw literal
4921 operator taking a const char* argument consisting of the number
4922 in string format. */
4923 args->truncate (0);
4924 vec_safe_push (args, num_string);
4925 decl = lookup_literal_operator (name, args);
4926 if (decl && decl != error_mark_node)
4928 result = finish_call_expr (decl, &args, false, true,
4929 tf_warning_or_error);
4930 return result;
4933 /* If the raw literal didn't work, look for a non-type template
4934 function with parameter pack char.... Call the function with
4935 template parameter characters representing the number. */
4936 args->truncate (0);
4937 decl = lookup_literal_operator (name, args);
4938 if (decl && decl != error_mark_node)
4940 tree tmpl_args = make_char_string_pack (num_string);
4941 if (tmpl_args == NULL_TREE)
4943 error ("failed to translate literal to execution character set %qT",
4944 num_string);
4945 return error_mark_node;
4947 decl = lookup_template_function (decl, tmpl_args);
4948 result = finish_call_expr (decl, &args, false, true,
4949 tf_warning_or_error);
4950 return result;
4953 /* In C++14 the standard library defines complex number suffixes that
4954 conflict with GNU extensions. Prefer them if <complex> is #included. */
4955 bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
4956 bool i14 = (cxx_dialect > cxx11
4957 && (id_equal (suffix_id, "i")
4958 || id_equal (suffix_id, "if")
4959 || id_equal (suffix_id, "il")));
4960 diagnostic_t kind = DK_ERROR;
4961 int opt = 0;
4963 if (i14 && ext)
4965 tree cxlit = lookup_qualified_name (std_node, "complex_literals",
4966 LOOK_want::NORMAL, false);
4967 if (cxlit == error_mark_node)
4969 /* No <complex>, so pedwarn and use GNU semantics. */
4970 kind = DK_PEDWARN;
4971 opt = OPT_Wpedantic;
4975 bool complained
4976 = emit_diagnostic (kind, input_location, opt,
4977 "unable to find numeric literal operator %qD", name);
4979 if (!complained)
4980 /* Don't inform either. */;
4981 else if (i14)
4983 inform (token->location, "add %<using namespace std::complex_literals%> "
4984 "(from %<<complex>%>) to enable the C++14 user-defined literal "
4985 "suffixes");
4986 if (ext)
4987 inform (token->location, "or use %<j%> instead of %<i%> for the "
4988 "GNU built-in suffix");
4990 else if (!ext)
4991 inform (token->location, "use %<-fext-numeric-literals%> "
4992 "to enable more built-in suffixes");
4994 if (kind == DK_ERROR)
4995 value = error_mark_node;
4996 else
4998 /* Use the built-in semantics. */
4999 tree type;
5000 if (id_equal (suffix_id, "i"))
5002 if (TREE_CODE (value) == INTEGER_CST)
5003 type = integer_type_node;
5004 else
5005 type = double_type_node;
5007 else if (id_equal (suffix_id, "if"))
5008 type = float_type_node;
5009 else /* if (id_equal (suffix_id, "il")) */
5010 type = long_double_type_node;
5012 value = fold_build2 (COMPLEX_EXPR, build_complex_type (type),
5013 build_zero_cst (type), fold_convert (type, value));
5016 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5017 /* Avoid repeated diagnostics. */
5018 token->u.value = value;
5019 return value;
5022 /* Parse a user-defined string constant. Returns a call to a user-defined
5023 literal operator taking a character pointer and the length of the string
5024 as arguments. */
5026 static tree
5027 finish_userdef_string_literal (tree literal)
5029 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
5030 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
5031 tree value = USERDEF_LITERAL_VALUE (literal);
5032 int len = TREE_STRING_LENGTH (value)
5033 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
5034 tree decl;
5036 /* Build up a call to the user-defined operator. */
5037 /* Lookup the name we got back from the id-expression. */
5038 releasing_vec args;
5039 vec_safe_push (args, value);
5040 vec_safe_push (args, build_int_cst (size_type_node, len));
5041 decl = lookup_literal_operator (name, args);
5043 if (decl && decl != error_mark_node)
5044 return finish_call_expr (decl, &args, false, true,
5045 tf_warning_or_error);
5047 /* Look for a suitable template function, either (C++20) with a single
5048 parameter of class type, or (N3599) with typename parameter CharT and
5049 parameter pack CharT... */
5050 args->truncate (0);
5051 decl = lookup_literal_operator (name, args);
5052 if (decl && decl != error_mark_node)
5054 /* Use resolve_nondeduced_context to try to choose one form of template
5055 or the other. */
5056 tree tmpl_args = make_tree_vec (1);
5057 TREE_VEC_ELT (tmpl_args, 0) = value;
5058 decl = lookup_template_function (decl, tmpl_args);
5059 tree res = resolve_nondeduced_context (decl, tf_none);
5060 if (DECL_P (res))
5061 decl = res;
5062 else
5064 TREE_OPERAND (decl, 1) = make_string_pack (value);
5065 res = resolve_nondeduced_context (decl, tf_none);
5066 if (DECL_P (res))
5067 decl = res;
5069 if (!DECL_P (decl) && cxx_dialect > cxx17)
5070 TREE_OPERAND (decl, 1) = tmpl_args;
5071 return finish_call_expr (decl, &args, false, true,
5072 tf_warning_or_error);
5075 error ("unable to find string literal operator %qD with %qT, %qT arguments",
5076 name, TREE_TYPE (value), size_type_node);
5077 return error_mark_node;
5081 /* Basic concepts [gram.basic] */
5083 /* Parse a translation-unit.
5085 translation-unit:
5086 declaration-seq [opt] */
5088 static void
5089 cp_parser_translation_unit (cp_parser* parser)
5091 gcc_checking_assert (!cp_error_declarator);
5093 /* Create the declarator obstack. */
5094 gcc_obstack_init (&declarator_obstack);
5095 /* Create the error declarator. */
5096 cp_error_declarator = make_declarator (cdk_error);
5097 /* Create the empty parameter list. */
5098 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
5099 UNKNOWN_LOCATION);
5100 /* Remember where the base of the declarator obstack lies. */
5101 void *declarator_obstack_base = obstack_next_free (&declarator_obstack);
5103 push_deferring_access_checks (flag_access_control
5104 ? dk_no_deferred : dk_no_check);
5106 module_parse mp_state = MP_NOT_MODULE;
5107 if (modules_p () && !header_module_p ())
5108 mp_state = MP_FIRST;
5110 bool implicit_extern_c = false;
5112 /* Parse until EOF. */
5113 for (;;)
5115 cp_token *token = cp_lexer_peek_token (parser->lexer);
5117 /* If we're entering or exiting a region that's implicitly
5118 extern "C", modify the lang context appropriately. This is
5119 so horrible. Please die. */
5120 if (implicit_extern_c
5121 != cp_lexer_peek_token (parser->lexer)->implicit_extern_c)
5123 implicit_extern_c = !implicit_extern_c;
5124 if (implicit_extern_c)
5125 push_lang_context (lang_name_c);
5126 else
5127 pop_lang_context ();
5130 if (token->type == CPP_EOF)
5131 break;
5133 if (modules_p ())
5135 /* Top-level module declarations are ok, and change the
5136 portion of file we're in. Top-level import declarations
5137 are significant for the import portions. */
5139 cp_token *next = token;
5140 bool exporting = token->keyword == RID__EXPORT;
5141 if (exporting)
5143 cp_lexer_consume_token (parser->lexer);
5144 next = cp_lexer_peek_token (parser->lexer);
5146 if (next->keyword == RID__MODULE)
5148 mp_state
5149 = cp_parser_module_declaration (parser, mp_state, exporting);
5150 continue;
5152 else if (next->keyword == RID__IMPORT)
5154 if (mp_state == MP_FIRST)
5155 mp_state = MP_NOT_MODULE;
5156 cp_parser_import_declaration (parser, mp_state, exporting);
5157 continue;
5159 else
5160 gcc_checking_assert (!exporting);
5162 if (mp_state == MP_GLOBAL && token->main_source_p)
5164 static bool warned = false;
5165 if (!warned)
5167 warned = true;
5168 error_at (token->location,
5169 "global module fragment contents must be"
5170 " from preprocessor inclusion");
5175 /* This relies on the ordering of module_parse values. */
5176 if (mp_state == MP_PURVIEW_IMPORTS || mp_state == MP_PRIVATE_IMPORTS)
5177 /* We're no longer in the import portion of a named module. */
5178 mp_state = module_parse (mp_state + 1);
5179 else if (mp_state == MP_FIRST)
5180 mp_state = MP_NOT_MODULE;
5182 if (token->type == CPP_CLOSE_BRACE)
5184 cp_parser_error (parser, "expected declaration");
5185 cp_lexer_consume_token (parser->lexer);
5186 /* If the next token is now a `;', consume it. */
5187 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
5188 cp_lexer_consume_token (parser->lexer);
5190 else
5191 cp_parser_toplevel_declaration (parser);
5194 /* Get rid of the token array; we don't need it any more. */
5195 cp_lexer_destroy (parser->lexer);
5196 parser->lexer = NULL;
5198 /* The EOF should have reset this. */
5199 gcc_checking_assert (!implicit_extern_c);
5201 /* Make sure the declarator obstack was fully cleaned up. */
5202 gcc_assert (obstack_next_free (&declarator_obstack)
5203 == declarator_obstack_base);
5206 /* Return the appropriate tsubst flags for parsing, possibly in N3276
5207 decltype context. */
5209 static inline tsubst_flags_t
5210 complain_flags (bool decltype_p)
5212 tsubst_flags_t complain = tf_warning_or_error;
5213 if (decltype_p)
5214 complain |= tf_decltype;
5215 return complain;
5218 /* We're about to parse a collection of statements. If we're currently
5219 parsing tentatively, set up a firewall so that any nested
5220 cp_parser_commit_to_tentative_parse won't affect the current context. */
5222 static cp_token_position
5223 cp_parser_start_tentative_firewall (cp_parser *parser)
5225 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5226 return 0;
5228 cp_parser_parse_tentatively (parser);
5229 cp_parser_commit_to_topmost_tentative_parse (parser);
5230 return cp_lexer_token_position (parser->lexer, false);
5233 /* We've finished parsing the collection of statements. Wrap up the
5234 firewall and replace the relevant tokens with the parsed form. */
5236 static void
5237 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
5238 tree expr)
5240 if (!start)
5241 return;
5243 /* Finish the firewall level. */
5244 cp_parser_parse_definitely (parser);
5245 /* And remember the result of the parse for when we try again. */
5246 cp_token *token = cp_lexer_token_at (parser->lexer, start);
5247 token->type = CPP_PREPARSED_EXPR;
5248 token->u.value = expr;
5249 token->keyword = RID_MAX;
5250 cp_lexer_purge_tokens_after (parser->lexer, start);
5253 /* Like the above functions, but let the user modify the tokens. Used by
5254 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
5255 later parses, so it makes sense to localize the effects of
5256 cp_parser_commit_to_tentative_parse. */
5258 struct tentative_firewall
5260 cp_parser *parser;
5261 bool set;
5263 tentative_firewall (cp_parser *p): parser(p)
5265 /* If we're currently parsing tentatively, start a committed level as a
5266 firewall and then an inner tentative parse. */
5267 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
5269 cp_parser_parse_tentatively (parser);
5270 cp_parser_commit_to_topmost_tentative_parse (parser);
5271 cp_parser_parse_tentatively (parser);
5275 ~tentative_firewall()
5277 if (set)
5279 /* Finish the inner tentative parse and the firewall, propagating any
5280 uncommitted error state to the outer tentative parse. */
5281 bool err = cp_parser_error_occurred (parser);
5282 cp_parser_parse_definitely (parser);
5283 cp_parser_parse_definitely (parser);
5284 if (err)
5285 cp_parser_simulate_error (parser);
5290 /* Some tokens naturally come in pairs e.g.'(' and ')'.
5291 This class is for tracking such a matching pair of symbols.
5292 In particular, it tracks the location of the first token,
5293 so that if the second token is missing, we can highlight the
5294 location of the first token when notifying the user about the
5295 problem. */
5297 template <typename traits_t>
5298 class token_pair
5300 public:
5301 /* token_pair's ctor. */
5302 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
5304 /* If the next token is the opening symbol for this pair, consume it and
5305 return true.
5306 Otherwise, issue an error and return false.
5307 In either case, record the location of the opening token. */
5309 bool require_open (cp_parser *parser)
5311 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
5312 return cp_parser_require (parser, traits_t::open_token_type,
5313 traits_t::required_token_open);
5316 /* Consume the next token from PARSER, recording its location as
5317 that of the opening token within the pair. */
5319 cp_token * consume_open (cp_parser *parser)
5321 cp_token *tok = cp_lexer_consume_token (parser->lexer);
5322 gcc_assert (tok->type == traits_t::open_token_type);
5323 m_open_loc = tok->location;
5324 return tok;
5327 /* If the next token is the closing symbol for this pair, consume it
5328 and return it.
5329 Otherwise, issue an error, highlighting the location of the
5330 corresponding opening token, and return NULL. */
5332 cp_token *require_close (cp_parser *parser) const
5334 return cp_parser_require (parser, traits_t::close_token_type,
5335 traits_t::required_token_close,
5336 m_open_loc);
5339 location_t open_location () const { return m_open_loc; }
5341 private:
5342 location_t m_open_loc;
5345 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
5347 struct matching_paren_traits
5349 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
5350 static const enum required_token required_token_open = RT_OPEN_PAREN;
5351 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
5352 static const enum required_token required_token_close = RT_CLOSE_PAREN;
5355 /* "matching_parens" is a token_pair<T> class for tracking matching
5356 pairs of parentheses. */
5358 typedef token_pair<matching_paren_traits> matching_parens;
5360 /* Traits for token_pair<T> for tracking matching pairs of braces. */
5362 struct matching_brace_traits
5364 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
5365 static const enum required_token required_token_open = RT_OPEN_BRACE;
5366 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
5367 static const enum required_token required_token_close = RT_CLOSE_BRACE;
5370 /* "matching_braces" is a token_pair<T> class for tracking matching
5371 pairs of braces. */
5373 typedef token_pair<matching_brace_traits> matching_braces;
5376 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
5377 enclosing parentheses. */
5379 static cp_expr
5380 cp_parser_statement_expr (cp_parser *parser)
5382 cp_token_position start = cp_parser_start_tentative_firewall (parser);
5384 /* Consume the '('. */
5385 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
5386 matching_parens parens;
5387 parens.consume_open (parser);
5388 /* Start the statement-expression. */
5389 tree expr = begin_stmt_expr ();
5390 /* Parse the compound-statement. */
5391 cp_parser_compound_statement (parser, expr, BCS_STMT_EXPR, false);
5392 /* Finish up. */
5393 expr = finish_stmt_expr (expr, false);
5394 /* Consume the ')'. */
5395 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
5396 if (!parens.require_close (parser))
5397 cp_parser_skip_to_end_of_statement (parser);
5399 cp_parser_end_tentative_firewall (parser, start, expr);
5400 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
5401 return cp_expr (expr, combined_loc);
5404 /* Expressions [gram.expr] */
5406 /* Parse a fold-operator.
5408 fold-operator:
5409 - * / % ^ & | = < > << >>
5410 = -= *= /= %= ^= &= |= <<= >>=
5411 == != <= >= && || , .* ->*
5413 This returns the tree code corresponding to the matched operator
5414 as an int. When the current token matches a compound assignment
5415 operator, the resulting tree code is the negative value of the
5416 non-assignment operator. */
5418 static int
5419 cp_parser_fold_operator (cp_token *token)
5421 switch (token->type)
5423 case CPP_PLUS: return PLUS_EXPR;
5424 case CPP_MINUS: return MINUS_EXPR;
5425 case CPP_MULT: return MULT_EXPR;
5426 case CPP_DIV: return TRUNC_DIV_EXPR;
5427 case CPP_MOD: return TRUNC_MOD_EXPR;
5428 case CPP_XOR: return BIT_XOR_EXPR;
5429 case CPP_AND: return BIT_AND_EXPR;
5430 case CPP_OR: return BIT_IOR_EXPR;
5431 case CPP_LSHIFT: return LSHIFT_EXPR;
5432 case CPP_RSHIFT: return RSHIFT_EXPR;
5434 case CPP_EQ: return -NOP_EXPR;
5435 case CPP_PLUS_EQ: return -PLUS_EXPR;
5436 case CPP_MINUS_EQ: return -MINUS_EXPR;
5437 case CPP_MULT_EQ: return -MULT_EXPR;
5438 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
5439 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
5440 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
5441 case CPP_AND_EQ: return -BIT_AND_EXPR;
5442 case CPP_OR_EQ: return -BIT_IOR_EXPR;
5443 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
5444 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
5446 case CPP_EQ_EQ: return EQ_EXPR;
5447 case CPP_NOT_EQ: return NE_EXPR;
5448 case CPP_LESS: return LT_EXPR;
5449 case CPP_GREATER: return GT_EXPR;
5450 case CPP_LESS_EQ: return LE_EXPR;
5451 case CPP_GREATER_EQ: return GE_EXPR;
5453 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
5454 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
5456 case CPP_COMMA: return COMPOUND_EXPR;
5458 case CPP_DOT_STAR: return DOTSTAR_EXPR;
5459 case CPP_DEREF_STAR: return MEMBER_REF;
5461 default: return ERROR_MARK;
5465 /* Returns true if CODE indicates a binary expression, which is not allowed in
5466 the LHS of a fold-expression. More codes will need to be added to use this
5467 function in other contexts. */
5469 static bool
5470 is_binary_op (tree_code code)
5472 switch (code)
5474 case PLUS_EXPR:
5475 case POINTER_PLUS_EXPR:
5476 case MINUS_EXPR:
5477 case MULT_EXPR:
5478 case TRUNC_DIV_EXPR:
5479 case TRUNC_MOD_EXPR:
5480 case BIT_XOR_EXPR:
5481 case BIT_AND_EXPR:
5482 case BIT_IOR_EXPR:
5483 case LSHIFT_EXPR:
5484 case RSHIFT_EXPR:
5486 case MODOP_EXPR:
5488 case EQ_EXPR:
5489 case NE_EXPR:
5490 case LE_EXPR:
5491 case GE_EXPR:
5492 case LT_EXPR:
5493 case GT_EXPR:
5495 case TRUTH_ANDIF_EXPR:
5496 case TRUTH_ORIF_EXPR:
5498 case COMPOUND_EXPR:
5500 case DOTSTAR_EXPR:
5501 case MEMBER_REF:
5502 return true;
5504 default:
5505 return false;
5509 /* If the next token is a suitable fold operator, consume it and return as
5510 the function above. */
5512 static int
5513 cp_parser_fold_operator (cp_parser *parser)
5515 cp_token* token = cp_lexer_peek_token (parser->lexer);
5516 int code = cp_parser_fold_operator (token);
5517 if (code != ERROR_MARK)
5518 cp_lexer_consume_token (parser->lexer);
5519 return code;
5522 /* Parse a fold-expression.
5524 fold-expression:
5525 ( ... folding-operator cast-expression)
5526 ( cast-expression folding-operator ... )
5527 ( cast-expression folding operator ... folding-operator cast-expression)
5529 Note that the '(' and ')' are matched in primary expression. */
5531 static cp_expr
5532 cp_parser_fold_expression (cp_parser *parser, tree expr1)
5534 cp_id_kind pidk;
5536 // Left fold.
5537 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5539 if (expr1)
5540 return error_mark_node;
5541 cp_lexer_consume_token (parser->lexer);
5542 int op = cp_parser_fold_operator (parser);
5543 if (op == ERROR_MARK)
5545 cp_parser_error (parser, "expected binary operator");
5546 return error_mark_node;
5549 tree expr = cp_parser_cast_expression (parser, false, false,
5550 false, &pidk);
5551 if (expr == error_mark_node)
5552 return error_mark_node;
5553 return finish_left_unary_fold_expr (expr, op);
5556 const cp_token* token = cp_lexer_peek_token (parser->lexer);
5557 int op = cp_parser_fold_operator (parser);
5558 if (op == ERROR_MARK)
5560 cp_parser_error (parser, "expected binary operator");
5561 return error_mark_node;
5564 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
5566 cp_parser_error (parser, "expected ...");
5567 return error_mark_node;
5569 cp_lexer_consume_token (parser->lexer);
5571 /* The operands of a fold-expression are cast-expressions, so binary or
5572 conditional expressions are not allowed. We check this here to avoid
5573 tentative parsing. */
5574 if (EXPR_P (expr1) && warning_suppressed_p (expr1, OPT_Wparentheses))
5575 /* OK, the expression was parenthesized. */;
5576 else if (is_binary_op (TREE_CODE (expr1)))
5577 error_at (location_of (expr1),
5578 "binary expression in operand of fold-expression");
5579 else if (TREE_CODE (expr1) == COND_EXPR
5580 || (REFERENCE_REF_P (expr1)
5581 && TREE_CODE (TREE_OPERAND (expr1, 0)) == COND_EXPR))
5582 error_at (location_of (expr1),
5583 "conditional expression in operand of fold-expression");
5585 // Right fold.
5586 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5587 return finish_right_unary_fold_expr (expr1, op);
5589 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
5591 cp_parser_error (parser, "mismatched operator in fold-expression");
5592 return error_mark_node;
5594 cp_lexer_consume_token (parser->lexer);
5596 // Binary left or right fold.
5597 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
5598 if (expr2 == error_mark_node)
5599 return error_mark_node;
5600 return finish_binary_fold_expr (expr1, expr2, op);
5603 /* Parse a primary-expression.
5605 primary-expression:
5606 literal
5607 this
5608 ( expression )
5609 id-expression
5610 lambda-expression (C++11)
5612 GNU Extensions:
5614 primary-expression:
5615 ( compound-statement )
5616 __builtin_va_arg ( assignment-expression , type-id )
5617 __builtin_offsetof ( type-id , offsetof-expression )
5619 C++ Extensions:
5620 __has_nothrow_assign ( type-id )
5621 __has_nothrow_constructor ( type-id )
5622 __has_nothrow_copy ( type-id )
5623 __has_trivial_assign ( type-id )
5624 __has_trivial_constructor ( type-id )
5625 __has_trivial_copy ( type-id )
5626 __has_trivial_destructor ( type-id )
5627 __has_virtual_destructor ( type-id )
5628 __is_abstract ( type-id )
5629 __is_base_of ( type-id , type-id )
5630 __is_class ( type-id )
5631 __is_empty ( type-id )
5632 __is_enum ( type-id )
5633 __is_final ( type-id )
5634 __is_literal_type ( type-id )
5635 __is_pod ( type-id )
5636 __is_polymorphic ( type-id )
5637 __is_std_layout ( type-id )
5638 __is_trivial ( type-id )
5639 __is_union ( type-id )
5641 Objective-C++ Extension:
5643 primary-expression:
5644 objc-expression
5646 literal:
5647 __null
5649 ADDRESS_P is true iff this expression was immediately preceded by
5650 "&" and therefore might denote a pointer-to-member. CAST_P is true
5651 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5652 true iff this expression is a template argument.
5654 Returns a representation of the expression. Upon return, *IDK
5655 indicates what kind of id-expression (if any) was present. */
5657 static cp_expr
5658 cp_parser_primary_expression (cp_parser *parser,
5659 bool address_p,
5660 bool cast_p,
5661 bool template_arg_p,
5662 bool decltype_p,
5663 cp_id_kind *idk)
5665 cp_token *token = NULL;
5667 /* Assume the primary expression is not an id-expression. */
5668 *idk = CP_ID_KIND_NONE;
5670 /* Peek at the next token. */
5671 token = cp_lexer_peek_token (parser->lexer);
5672 switch ((int) token->type)
5674 /* literal:
5675 integer-literal
5676 character-literal
5677 floating-literal
5678 string-literal
5679 boolean-literal
5680 pointer-literal
5681 user-defined-literal */
5682 case CPP_CHAR:
5683 case CPP_CHAR16:
5684 case CPP_CHAR32:
5685 case CPP_WCHAR:
5686 case CPP_UTF8CHAR:
5687 case CPP_NUMBER:
5688 case CPP_PREPARSED_EXPR:
5689 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5690 return cp_parser_userdef_numeric_literal (parser);
5691 token = cp_lexer_consume_token (parser->lexer);
5692 if (TREE_CODE (token->u.value) == FIXED_CST)
5694 error_at (token->location,
5695 "fixed-point types not supported in C++");
5696 return error_mark_node;
5698 /* Floating-point literals are only allowed in an integral
5699 constant expression if they are cast to an integral or
5700 enumeration type. */
5701 if ((TREE_CODE (token->u.value) == REAL_CST
5702 || (TREE_CODE (token->u.value) == EXCESS_PRECISION_EXPR
5703 && TREE_CODE (TREE_OPERAND (token->u.value, 0)) == REAL_CST))
5704 && parser->integral_constant_expression_p
5705 && pedantic)
5707 /* CAST_P will be set even in invalid code like "int(2.7 +
5708 ...)". Therefore, we have to check that the next token
5709 is sure to end the cast. */
5710 if (cast_p)
5712 cp_token *next_token;
5714 next_token = cp_lexer_peek_token (parser->lexer);
5715 if (/* The comma at the end of an
5716 enumerator-definition. */
5717 next_token->type != CPP_COMMA
5718 /* The curly brace at the end of an enum-specifier. */
5719 && next_token->type != CPP_CLOSE_BRACE
5720 /* The end of a statement. */
5721 && next_token->type != CPP_SEMICOLON
5722 /* The end of the cast-expression. */
5723 && next_token->type != CPP_CLOSE_PAREN
5724 /* The end of an array bound. */
5725 && next_token->type != CPP_CLOSE_SQUARE
5726 /* The closing ">" in a template-argument-list. */
5727 && (next_token->type != CPP_GREATER
5728 || parser->greater_than_is_operator_p)
5729 /* C++0x only: A ">>" treated like two ">" tokens,
5730 in a template-argument-list. */
5731 && (next_token->type != CPP_RSHIFT
5732 || (cxx_dialect == cxx98)
5733 || parser->greater_than_is_operator_p))
5734 cast_p = false;
5737 /* If we are within a cast, then the constraint that the
5738 cast is to an integral or enumeration type will be
5739 checked at that point. If we are not within a cast, then
5740 this code is invalid. */
5741 if (!cast_p)
5742 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5744 return (cp_expr (token->u.value, token->location, token->flags & DECIMAL_INT)
5745 .maybe_add_location_wrapper ());
5747 case CPP_CHAR_USERDEF:
5748 case CPP_CHAR16_USERDEF:
5749 case CPP_CHAR32_USERDEF:
5750 case CPP_WCHAR_USERDEF:
5751 case CPP_UTF8CHAR_USERDEF:
5752 return cp_parser_userdef_char_literal (parser);
5754 case CPP_STRING:
5755 case CPP_STRING16:
5756 case CPP_STRING32:
5757 case CPP_WSTRING:
5758 case CPP_UTF8STRING:
5759 case CPP_STRING_USERDEF:
5760 case CPP_STRING16_USERDEF:
5761 case CPP_STRING32_USERDEF:
5762 case CPP_WSTRING_USERDEF:
5763 case CPP_UTF8STRING_USERDEF:
5764 /* ??? Should wide strings be allowed when parser->translate_strings_p
5765 is false (i.e. in attributes)? If not, we can kill the third
5766 argument to cp_parser_string_literal. */
5767 if (parser->translate_strings_p)
5768 return (cp_parser_userdef_string_literal (parser,
5769 /*lookup_udlit=*/true)
5770 .maybe_add_location_wrapper ());
5771 else
5772 return (cp_parser_string_literal (parser,
5773 /*translate=*/false,
5774 /*wide_ok=*/true)
5775 .maybe_add_location_wrapper ());
5777 case CPP_OPEN_PAREN:
5778 /* If we see `( { ' then we are looking at the beginning of
5779 a GNU statement-expression. */
5780 if (cp_parser_allow_gnu_extensions_p (parser)
5781 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5783 /* Statement-expressions are not allowed by the standard. */
5784 pedwarn (token->location, OPT_Wpedantic,
5785 "ISO C++ forbids braced-groups within expressions");
5787 /* And they're not allowed outside of a function-body; you
5788 cannot, for example, write:
5790 int i = ({ int j = 3; j + 1; });
5792 at class or namespace scope. */
5793 if (!parser->in_function_body
5794 || parser->in_template_argument_list_p)
5796 error_at (token->location,
5797 "statement-expressions are not allowed outside "
5798 "functions nor in template-argument lists");
5799 cp_parser_skip_to_end_of_block_or_statement (parser);
5800 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5801 cp_lexer_consume_token (parser->lexer);
5802 return error_mark_node;
5804 else
5805 return cp_parser_statement_expr (parser);
5807 /* Otherwise it's a normal parenthesized expression. */
5809 cp_expr expr;
5810 bool saved_greater_than_is_operator_p;
5812 location_t open_paren_loc = token->location;
5814 /* Consume the `('. */
5815 matching_parens parens;
5816 parens.consume_open (parser);
5817 /* Within a parenthesized expression, a `>' token is always
5818 the greater-than operator. */
5819 saved_greater_than_is_operator_p
5820 = parser->greater_than_is_operator_p;
5821 parser->greater_than_is_operator_p = true;
5823 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5824 /* Left fold expression. */
5825 expr = NULL_TREE;
5826 else
5827 /* Parse the parenthesized expression. */
5828 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5830 token = cp_lexer_peek_token (parser->lexer);
5831 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5833 expr = cp_parser_fold_expression (parser, expr);
5834 if (expr != error_mark_node
5835 && cxx_dialect < cxx17)
5836 pedwarn (input_location, OPT_Wc__17_extensions,
5837 "fold-expressions only available with %<-std=c++17%> "
5838 "or %<-std=gnu++17%>");
5840 else
5841 /* Let the front end know that this expression was
5842 enclosed in parentheses. This matters in case, for
5843 example, the expression is of the form `A::B', since
5844 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5845 not. */
5846 expr = finish_parenthesized_expr (expr);
5848 /* DR 705: Wrapping an unqualified name in parentheses
5849 suppresses arg-dependent lookup. We want to pass back
5850 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5851 (c++/37862), but none of the others. */
5852 if (*idk != CP_ID_KIND_QUALIFIED)
5853 *idk = CP_ID_KIND_NONE;
5855 /* The `>' token might be the end of a template-id or
5856 template-parameter-list now. */
5857 parser->greater_than_is_operator_p
5858 = saved_greater_than_is_operator_p;
5860 /* Consume the `)'. */
5861 token = cp_lexer_peek_token (parser->lexer);
5862 location_t close_paren_loc = token->location;
5863 bool no_wparens = warning_suppressed_p (expr, OPT_Wparentheses);
5864 expr.set_range (open_paren_loc, close_paren_loc);
5865 if (no_wparens)
5866 suppress_warning (expr, OPT_Wparentheses);
5867 if (!parens.require_close (parser)
5868 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5869 cp_parser_skip_to_end_of_statement (parser);
5871 return expr;
5874 case CPP_OPEN_SQUARE:
5876 if (c_dialect_objc ())
5878 /* We might have an Objective-C++ message. */
5879 cp_parser_parse_tentatively (parser);
5880 tree msg = cp_parser_objc_message_expression (parser);
5881 /* If that works out, we're done ... */
5882 if (cp_parser_parse_definitely (parser))
5883 return msg;
5884 /* ... else, fall though to see if it's a lambda. */
5886 cp_expr lam = cp_parser_lambda_expression (parser);
5887 /* Don't warn about a failed tentative parse. */
5888 if (cp_parser_error_occurred (parser))
5889 return error_mark_node;
5890 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5891 return lam;
5894 case CPP_OBJC_STRING:
5895 if (c_dialect_objc ())
5896 /* We have an Objective-C++ string literal. */
5897 return cp_parser_objc_expression (parser);
5898 cp_parser_error (parser, "expected primary-expression");
5899 return error_mark_node;
5901 case CPP_KEYWORD:
5902 switch (token->keyword)
5904 /* These two are the boolean literals. */
5905 case RID_TRUE:
5906 cp_lexer_consume_token (parser->lexer);
5907 return cp_expr (boolean_true_node, token->location);
5908 case RID_FALSE:
5909 cp_lexer_consume_token (parser->lexer);
5910 return cp_expr (boolean_false_node, token->location);
5912 /* The `__null' literal. */
5913 case RID_NULL:
5914 cp_lexer_consume_token (parser->lexer);
5915 return cp_expr (null_node, token->location);
5917 /* The `nullptr' literal. */
5918 case RID_NULLPTR:
5919 cp_lexer_consume_token (parser->lexer);
5920 return cp_expr (nullptr_node, token->location);
5922 /* Recognize the `this' keyword. */
5923 case RID_THIS:
5924 cp_lexer_consume_token (parser->lexer);
5925 if (parser->local_variables_forbidden_p & THIS_FORBIDDEN)
5927 error_at (token->location,
5928 "%<this%> may not be used in this context");
5929 return error_mark_node;
5931 /* Pointers cannot appear in constant-expressions. */
5932 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5933 return error_mark_node;
5934 return cp_expr (finish_this_expr (), token->location);
5936 /* The `operator' keyword can be the beginning of an
5937 id-expression. */
5938 case RID_OPERATOR:
5939 goto id_expression;
5941 case RID_FUNCTION_NAME:
5942 case RID_PRETTY_FUNCTION_NAME:
5943 case RID_C99_FUNCTION_NAME:
5945 non_integral_constant name;
5947 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5948 __func__ are the names of variables -- but they are
5949 treated specially. Therefore, they are handled here,
5950 rather than relying on the generic id-expression logic
5951 below. Grammatically, these names are id-expressions.
5953 Consume the token. */
5954 token = cp_lexer_consume_token (parser->lexer);
5956 switch (token->keyword)
5958 case RID_FUNCTION_NAME:
5959 name = NIC_FUNC_NAME;
5960 break;
5961 case RID_PRETTY_FUNCTION_NAME:
5962 name = NIC_PRETTY_FUNC;
5963 break;
5964 case RID_C99_FUNCTION_NAME:
5965 name = NIC_C99_FUNC;
5966 break;
5967 default:
5968 gcc_unreachable ();
5971 if (cp_parser_non_integral_constant_expression (parser, name))
5972 return error_mark_node;
5974 /* Look up the name. */
5975 return finish_fname (token->u.value);
5978 case RID_VA_ARG:
5980 tree expression;
5981 tree type;
5982 location_t type_location;
5983 location_t start_loc
5984 = cp_lexer_peek_token (parser->lexer)->location;
5985 /* The `__builtin_va_arg' construct is used to handle
5986 `va_arg'. Consume the `__builtin_va_arg' token. */
5987 cp_lexer_consume_token (parser->lexer);
5988 /* Look for the opening `('. */
5989 matching_parens parens;
5990 parens.require_open (parser);
5991 /* Now, parse the assignment-expression. */
5992 expression = cp_parser_assignment_expression (parser);
5993 /* Look for the `,'. */
5994 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5995 type_location = cp_lexer_peek_token (parser->lexer)->location;
5996 /* Parse the type-id. */
5998 type_id_in_expr_sentinel s (parser);
5999 type = cp_parser_type_id (parser);
6001 /* Look for the closing `)'. */
6002 location_t finish_loc
6003 = cp_lexer_peek_token (parser->lexer)->location;
6004 parens.require_close (parser);
6005 /* Using `va_arg' in a constant-expression is not
6006 allowed. */
6007 if (cp_parser_non_integral_constant_expression (parser,
6008 NIC_VA_ARG))
6009 return error_mark_node;
6010 /* Construct a location of the form:
6011 __builtin_va_arg (v, int)
6012 ~~~~~~~~~~~~~~~~~~~~~^~~~
6013 with the caret at the type, ranging from the start of the
6014 "__builtin_va_arg" token to the close paren. */
6015 location_t combined_loc
6016 = make_location (type_location, start_loc, finish_loc);
6017 return build_x_va_arg (combined_loc, expression, type);
6020 case RID_OFFSETOF:
6021 return cp_parser_builtin_offsetof (parser);
6023 #define DEFTRAIT_EXPR(CODE, NAME, ARITY) \
6024 case RID_##CODE:
6025 #include "cp-trait.def"
6026 #undef DEFTRAIT_EXPR
6027 return cp_parser_trait (parser, token->keyword);
6029 // C++ concepts
6030 case RID_REQUIRES:
6031 return cp_parser_requires_expression (parser);
6033 /* Objective-C++ expressions. */
6034 case RID_AT_ENCODE:
6035 case RID_AT_PROTOCOL:
6036 case RID_AT_SELECTOR:
6037 return cp_parser_objc_expression (parser);
6039 case RID_OMP_ALL_MEMORY:
6040 gcc_assert (flag_openmp);
6041 cp_lexer_consume_token (parser->lexer);
6042 error_at (token->location,
6043 "%<omp_all_memory%> may only be used in OpenMP "
6044 "%<depend%> clause");
6045 return error_mark_node;
6047 case RID_TEMPLATE:
6048 if (parser->in_function_body
6049 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6050 == CPP_LESS))
6052 error_at (token->location,
6053 "a template declaration cannot appear at block scope");
6054 cp_parser_skip_to_end_of_block_or_statement (parser);
6055 return error_mark_node;
6057 /* FALLTHRU */
6058 default:
6059 cp_parser_error (parser, "expected primary-expression");
6060 return error_mark_node;
6063 /* An id-expression can start with either an identifier, a
6064 `::' as the beginning of a qualified-id, or the "operator"
6065 keyword. */
6066 case CPP_NAME:
6067 case CPP_SCOPE:
6068 case CPP_TEMPLATE_ID:
6069 case CPP_NESTED_NAME_SPECIFIER:
6071 id_expression:
6072 cp_expr id_expression;
6073 cp_expr decl;
6074 const char *error_msg;
6075 bool template_p;
6076 bool done;
6077 cp_token *id_expr_token;
6079 /* Parse the id-expression. */
6080 id_expression
6081 = cp_parser_id_expression (parser,
6082 /*template_keyword_p=*/false,
6083 /*check_dependency_p=*/true,
6084 &template_p,
6085 /*declarator_p=*/false,
6086 /*optional_p=*/false);
6087 if (id_expression == error_mark_node)
6088 return error_mark_node;
6089 id_expr_token = token;
6090 token = cp_lexer_peek_token (parser->lexer);
6091 done = (token->type != CPP_OPEN_SQUARE
6092 && token->type != CPP_OPEN_PAREN
6093 && token->type != CPP_DOT
6094 && token->type != CPP_DEREF
6095 && token->type != CPP_PLUS_PLUS
6096 && token->type != CPP_MINUS_MINUS);
6097 /* If we have a template-id, then no further lookup is
6098 required. If the template-id was for a template-class, we
6099 will sometimes have a TYPE_DECL at this point. */
6100 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
6101 || TREE_CODE (id_expression) == TYPE_DECL)
6102 decl = id_expression;
6103 /* Look up the name. */
6104 else
6106 tree ambiguous_decls;
6108 /* If we already know that this lookup is ambiguous, then
6109 we've already issued an error message; there's no reason
6110 to check again. */
6111 if (id_expr_token->type == CPP_NAME
6112 && id_expr_token->error_reported)
6114 cp_parser_simulate_error (parser);
6115 return error_mark_node;
6118 decl = cp_parser_lookup_name (parser, id_expression,
6119 none_type,
6120 template_p,
6121 /*is_namespace=*/false,
6122 /*check_dependency=*/true,
6123 &ambiguous_decls,
6124 id_expression.get_location ());
6125 /* If the lookup was ambiguous, an error will already have
6126 been issued. */
6127 if (ambiguous_decls)
6128 return error_mark_node;
6130 /* In Objective-C++, we may have an Objective-C 2.0
6131 dot-syntax for classes here. */
6132 if (c_dialect_objc ()
6133 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
6134 && TREE_CODE (decl) == TYPE_DECL
6135 && objc_is_class_name (decl))
6137 tree component;
6138 cp_lexer_consume_token (parser->lexer);
6139 component = cp_parser_identifier (parser);
6140 if (component == error_mark_node)
6141 return error_mark_node;
6143 tree result = objc_build_class_component_ref (id_expression,
6144 component);
6145 /* Build a location of the form:
6146 expr.component
6147 ~~~~~^~~~~~~~~
6148 with caret at the start of the component name (at
6149 input_location), ranging from the start of the id_expression
6150 to the end of the component name. */
6151 location_t combined_loc
6152 = make_location (input_location, id_expression.get_start (),
6153 get_finish (input_location));
6154 protected_set_expr_location (result, combined_loc);
6155 return result;
6158 /* In Objective-C++, an instance variable (ivar) may be preferred
6159 to whatever cp_parser_lookup_name() found.
6160 Call objc_lookup_ivar. To avoid exposing cp_expr to the
6161 rest of c-family, we have to do a little extra work to preserve
6162 any location information in cp_expr "decl". Given that
6163 objc_lookup_ivar is implemented in "c-family" and "objc", we
6164 have a trip through the pure "tree" type, rather than cp_expr.
6165 Naively copying it back to "decl" would implicitly give the
6166 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
6167 store an EXPR_LOCATION. Hence we only update "decl" (and
6168 hence its location_t) if we get back a different tree node. */
6169 tree decl_tree = objc_lookup_ivar (decl.get_value (),
6170 id_expression);
6171 if (decl_tree != decl.get_value ())
6172 decl = cp_expr (decl_tree);
6174 /* If name lookup gives us a SCOPE_REF, then the
6175 qualifying scope was dependent. */
6176 if (TREE_CODE (decl) == SCOPE_REF)
6178 /* At this point, we do not know if DECL is a valid
6179 integral constant expression. We assume that it is
6180 in fact such an expression, so that code like:
6182 template <int N> struct A {
6183 int a[B<N>::i];
6186 is accepted. At template-instantiation time, we
6187 will check that B<N>::i is actually a constant. */
6188 return decl;
6190 /* Check to see if DECL is a local variable in a context
6191 where that is forbidden. */
6192 if ((parser->local_variables_forbidden_p & LOCAL_VARS_FORBIDDEN)
6193 && local_variable_p (decl)
6194 /* DR 2082 permits local variables in unevaluated contexts
6195 within a default argument. */
6196 && !cp_unevaluated_operand)
6198 const char *msg
6199 = (TREE_CODE (decl) == PARM_DECL
6200 ? _("parameter %qD may not appear in this context")
6201 : _("local variable %qD may not appear in this context"));
6202 error_at (id_expression.get_location (), msg,
6203 decl.get_value ());
6204 return error_mark_node;
6208 decl = (finish_id_expression
6209 (id_expression, decl, parser->scope,
6210 idk,
6211 parser->integral_constant_expression_p,
6212 parser->allow_non_integral_constant_expression_p,
6213 &parser->non_integral_constant_expression_p,
6214 template_p, done, address_p,
6215 template_arg_p,
6216 &error_msg,
6217 id_expression.get_location ()));
6218 if (error_msg)
6219 cp_parser_error (parser, error_msg);
6220 /* Build a location for an id-expression of the form:
6221 ::ns::id
6222 ~~~~~~^~
6226 i.e. from the start of the first token to the end of the final
6227 token, with the caret at the start of the unqualified-id. */
6228 location_t caret_loc = get_pure_location (id_expression.get_location ());
6229 location_t start_loc = get_start (id_expr_token->location);
6230 location_t finish_loc = get_finish (id_expression.get_location ());
6231 location_t combined_loc
6232 = make_location (caret_loc, start_loc, finish_loc);
6234 decl.set_location (combined_loc);
6235 return decl;
6238 /* Anything else is an error. */
6239 default:
6240 cp_parser_error (parser, "expected primary-expression");
6241 return error_mark_node;
6245 static inline cp_expr
6246 cp_parser_primary_expression (cp_parser *parser,
6247 bool address_p,
6248 bool cast_p,
6249 bool template_arg_p,
6250 cp_id_kind *idk)
6252 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
6253 /*decltype*/false, idk);
6256 /* Complain about missing template keyword when naming a dependent
6257 member template. */
6259 static void
6260 missing_template_diag (location_t loc, diagnostic_t diag_kind = DK_WARNING)
6262 if (warning_suppressed_at (loc, OPT_Wmissing_template_keyword))
6263 return;
6265 gcc_rich_location richloc (loc);
6266 richloc.add_fixit_insert_before ("template");
6267 emit_diagnostic (diag_kind, &richloc, OPT_Wmissing_template_keyword,
6268 "expected %qs keyword before dependent "
6269 "template name", "template");
6270 suppress_warning_at (loc, OPT_Wmissing_template_keyword);
6273 /* Parse an id-expression.
6275 id-expression:
6276 unqualified-id
6277 qualified-id
6279 qualified-id:
6280 :: [opt] nested-name-specifier template [opt] unqualified-id
6281 :: identifier
6282 :: operator-function-id
6283 :: template-id
6285 Return a representation of the unqualified portion of the
6286 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
6287 a `::' or nested-name-specifier.
6289 Often, if the id-expression was a qualified-id, the caller will
6290 want to make a SCOPE_REF to represent the qualified-id. This
6291 function does not do this in order to avoid wastefully creating
6292 SCOPE_REFs when they are not required.
6294 If TEMPLATE_KEYWORD_P is true, then we have just seen the
6295 `template' keyword.
6297 If CHECK_DEPENDENCY_P is false, then names are looked up inside
6298 uninstantiated templates.
6300 If *TEMPLATE_P is non-NULL, it is set to true iff the
6301 `template' keyword is used to explicitly indicate that the entity
6302 named is a template.
6304 If DECLARATOR_P is true, the id-expression is appearing as part of
6305 a declarator, rather than as part of an expression. */
6307 static cp_expr
6308 cp_parser_id_expression (cp_parser *parser,
6309 bool template_keyword_p,
6310 bool check_dependency_p,
6311 bool *template_p,
6312 bool declarator_p,
6313 bool optional_p)
6315 bool global_scope_p;
6316 bool nested_name_specifier_p;
6318 /* Assume the `template' keyword was not used. */
6319 if (template_p)
6320 *template_p = template_keyword_p;
6322 /* Look for the optional `::' operator. */
6323 global_scope_p
6324 = (!template_keyword_p
6325 && (cp_parser_global_scope_opt (parser,
6326 /*current_scope_valid_p=*/false)
6327 != NULL_TREE));
6329 /* Look for the optional nested-name-specifier. */
6330 nested_name_specifier_p
6331 = (cp_parser_nested_name_specifier_opt (parser,
6332 /*typename_keyword_p=*/false,
6333 check_dependency_p,
6334 /*type_p=*/false,
6335 declarator_p,
6336 template_keyword_p)
6337 != NULL_TREE);
6339 cp_expr id = NULL_TREE;
6340 tree scope = parser->scope;
6342 /* Peek at the next token. */
6343 cp_token *token = cp_lexer_peek_token (parser->lexer);
6345 /* If there is a nested-name-specifier, then we are looking at
6346 the first qualified-id production. */
6347 if (nested_name_specifier_p)
6349 tree saved_object_scope;
6350 tree saved_qualifying_scope;
6352 /* See if the next token is the `template' keyword. */
6353 if (!template_p)
6354 template_p = &template_keyword_p;
6355 *template_p = cp_parser_optional_template_keyword (parser);
6356 /* Name lookup we do during the processing of the
6357 unqualified-id might obliterate SCOPE. */
6358 saved_object_scope = parser->object_scope;
6359 saved_qualifying_scope = parser->qualifying_scope;
6360 /* Process the final unqualified-id. */
6361 id = cp_parser_unqualified_id (parser, *template_p,
6362 check_dependency_p,
6363 declarator_p,
6364 /*optional_p=*/false);
6365 /* Restore the SAVED_SCOPE for our caller. */
6366 parser->scope = scope;
6367 parser->object_scope = saved_object_scope;
6368 parser->qualifying_scope = saved_qualifying_scope;
6370 /* Otherwise, if we are in global scope, then we are looking at one
6371 of the other qualified-id productions. */
6372 else if (global_scope_p)
6374 /* If it's an identifier, and the next token is not a "<", then
6375 we can avoid the template-id case. This is an optimization
6376 for this common case. */
6377 if (token->type == CPP_NAME
6378 && !cp_parser_nth_token_starts_template_argument_list_p
6379 (parser, 2))
6380 return cp_parser_identifier (parser);
6382 cp_parser_parse_tentatively (parser);
6383 /* Try a template-id. */
6384 id = cp_parser_template_id_expr (parser,
6385 /*template_keyword_p=*/false,
6386 /*check_dependency_p=*/true,
6387 declarator_p);
6388 /* If that worked, we're done. */
6389 if (cp_parser_parse_definitely (parser))
6390 return id;
6392 /* Peek at the next token. (Changes in the token buffer may
6393 have invalidated the pointer obtained above.) */
6394 token = cp_lexer_peek_token (parser->lexer);
6396 switch (token->type)
6398 case CPP_NAME:
6399 id = cp_parser_identifier (parser);
6400 break;
6402 case CPP_KEYWORD:
6403 if (token->keyword == RID_OPERATOR)
6405 id = cp_parser_operator_function_id (parser);
6406 break;
6408 /* Fall through. */
6410 default:
6411 cp_parser_error (parser, "expected id-expression");
6412 return error_mark_node;
6415 else
6417 if (!scope)
6418 scope = parser->context->object_type;
6419 id = cp_parser_unqualified_id (parser, template_keyword_p,
6420 /*check_dependency_p=*/true,
6421 declarator_p,
6422 optional_p);
6425 if (id && TREE_CODE (id) == IDENTIFIER_NODE
6426 && warn_missing_template_keyword
6427 && !template_keyword_p
6428 /* Don't warn if we're looking inside templates. */
6429 && check_dependency_p
6430 /* In a template argument list a > could be closing
6431 the enclosing targs. */
6432 && !parser->in_template_argument_list_p
6433 && scope && dependentish_scope_p (scope)
6434 /* Don't confuse an ill-formed constructor declarator for a missing
6435 template keyword in a return type. */
6436 && !(declarator_p && constructor_name_p (id, scope))
6437 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1)
6438 && warning_enabled_at (token->location,
6439 OPT_Wmissing_template_keyword))
6441 saved_token_sentinel toks (parser->lexer, STS_ROLLBACK);
6442 if (cp_parser_skip_entire_template_parameter_list (parser)
6443 /* An operator after the > suggests that the > ends a
6444 template-id; a name or literal suggests that the > is an
6445 operator. */
6446 && (cp_lexer_peek_token (parser->lexer)->type
6447 <= CPP_LAST_PUNCTUATOR))
6448 missing_template_diag (token->location);
6451 return id;
6454 /* Parse an unqualified-id.
6456 unqualified-id:
6457 identifier
6458 operator-function-id
6459 conversion-function-id
6460 ~ class-name
6461 template-id
6463 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
6464 keyword, in a construct like `A::template ...'.
6466 Returns a representation of unqualified-id. For the `identifier'
6467 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
6468 production a BIT_NOT_EXPR is returned; the operand of the
6469 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
6470 other productions, see the documentation accompanying the
6471 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
6472 names are looked up in uninstantiated templates. If DECLARATOR_P
6473 is true, the unqualified-id is appearing as part of a declarator,
6474 rather than as part of an expression. */
6476 static cp_expr
6477 cp_parser_unqualified_id (cp_parser* parser,
6478 bool template_keyword_p,
6479 bool check_dependency_p,
6480 bool declarator_p,
6481 bool optional_p)
6483 cp_token *token;
6485 /* Peek at the next token. */
6486 token = cp_lexer_peek_token (parser->lexer);
6488 switch ((int) token->type)
6490 case CPP_NAME:
6492 tree id;
6494 /* We don't know yet whether or not this will be a
6495 template-id. */
6496 cp_parser_parse_tentatively (parser);
6497 /* Try a template-id. */
6498 id = cp_parser_template_id_expr (parser, template_keyword_p,
6499 check_dependency_p,
6500 declarator_p);
6501 /* If it worked, we're done. */
6502 if (cp_parser_parse_definitely (parser))
6503 return id;
6504 /* Otherwise, it's an ordinary identifier. */
6505 return cp_parser_identifier (parser);
6508 case CPP_TEMPLATE_ID:
6509 return cp_parser_template_id_expr (parser, template_keyword_p,
6510 check_dependency_p,
6511 declarator_p);
6513 case CPP_COMPL:
6515 tree type_decl;
6516 tree qualifying_scope;
6517 tree object_scope;
6518 tree scope;
6519 bool done;
6520 location_t tilde_loc = token->location;
6522 /* Consume the `~' token. */
6523 cp_lexer_consume_token (parser->lexer);
6524 /* Parse the class-name. The standard, as written, seems to
6525 say that:
6527 template <typename T> struct S { ~S (); };
6528 template <typename T> S<T>::~S() {}
6530 is invalid, since `~' must be followed by a class-name, but
6531 `S<T>' is dependent, and so not known to be a class.
6532 That's not right; we need to look in uninstantiated
6533 templates. A further complication arises from:
6535 template <typename T> void f(T t) {
6536 t.T::~T();
6539 Here, it is not possible to look up `T' in the scope of `T'
6540 itself. We must look in both the current scope, and the
6541 scope of the containing complete expression.
6543 Yet another issue is:
6545 struct S {
6546 int S;
6547 ~S();
6550 S::~S() {}
6552 The standard does not seem to say that the `S' in `~S'
6553 should refer to the type `S' and not the data member
6554 `S::S'. */
6556 /* DR 244 says that we look up the name after the "~" in the
6557 same scope as we looked up the qualifying name. That idea
6558 isn't fully worked out; it's more complicated than that. */
6559 scope = parser->scope;
6560 object_scope = parser->object_scope;
6561 qualifying_scope = parser->qualifying_scope;
6563 /* Check for invalid scopes. */
6564 if (scope == error_mark_node)
6566 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
6567 cp_lexer_consume_token (parser->lexer);
6568 return error_mark_node;
6570 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
6572 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6573 error_at (token->location,
6574 "scope %qT before %<~%> is not a class-name",
6575 scope);
6576 cp_parser_simulate_error (parser);
6577 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
6578 cp_lexer_consume_token (parser->lexer);
6579 return error_mark_node;
6581 if (template_keyword_p)
6583 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6584 error_at (tilde_loc, "%<template%> keyword not permitted in "
6585 "destructor name");
6586 cp_parser_simulate_error (parser);
6587 return error_mark_node;
6590 gcc_assert (!scope || TYPE_P (scope));
6592 token = cp_lexer_peek_token (parser->lexer);
6594 /* Create a location with caret == start at the tilde,
6595 finishing at the end of the peeked token, e.g:
6596 ~token
6597 ^~~~~~. */
6598 location_t loc
6599 = make_location (tilde_loc, tilde_loc, token->location);
6601 /* If the name is of the form "X::~X" it's OK even if X is a
6602 typedef. */
6604 if (scope
6605 && token->type == CPP_NAME
6606 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6607 != CPP_LESS)
6608 && (token->u.value == TYPE_IDENTIFIER (scope)
6609 || (CLASS_TYPE_P (scope)
6610 && constructor_name_p (token->u.value, scope))))
6612 cp_lexer_consume_token (parser->lexer);
6613 return build_min_nt_loc (loc, BIT_NOT_EXPR, scope);
6616 /* ~auto means the destructor of whatever the object is. */
6617 if (cp_parser_is_keyword (token, RID_AUTO))
6619 if (cxx_dialect < cxx14)
6620 pedwarn (loc, OPT_Wc__14_extensions,
6621 "%<~auto%> only available with "
6622 "%<-std=c++14%> or %<-std=gnu++14%>");
6623 cp_lexer_consume_token (parser->lexer);
6624 return build_min_nt_loc (loc, BIT_NOT_EXPR, make_auto ());
6627 /* DR 2237 (C++20 only): A simple-template-id is no longer valid as the
6628 declarator-id of a constructor or destructor. */
6629 if (token->type == CPP_TEMPLATE_ID && declarator_p
6630 && cxx_dialect >= cxx20)
6632 if (!cp_parser_simulate_error (parser))
6633 error_at (tilde_loc, "template-id not allowed for destructor");
6634 return error_mark_node;
6637 /* If there was an explicit qualification (S::~T), first look
6638 in the scope given by the qualification (i.e., S).
6640 Note: in the calls to cp_parser_class_name below we pass
6641 typename_type so that lookup finds the injected-class-name
6642 rather than the constructor. */
6643 done = false;
6644 type_decl = NULL_TREE;
6645 if (scope)
6647 cp_parser_parse_tentatively (parser);
6648 type_decl = cp_parser_class_name (parser,
6649 /*typename_keyword_p=*/false,
6650 /*template_keyword_p=*/false,
6651 typename_type,
6652 /*check_dependency=*/false,
6653 /*class_head_p=*/false,
6654 declarator_p);
6655 if (cp_parser_parse_definitely (parser))
6656 done = true;
6658 /* In "N::S::~S", look in "N" as well. */
6659 if (!done && scope && qualifying_scope)
6661 cp_parser_parse_tentatively (parser);
6662 parser->scope = qualifying_scope;
6663 parser->object_scope = NULL_TREE;
6664 parser->qualifying_scope = NULL_TREE;
6665 type_decl
6666 = cp_parser_class_name (parser,
6667 /*typename_keyword_p=*/false,
6668 /*template_keyword_p=*/false,
6669 typename_type,
6670 /*check_dependency=*/false,
6671 /*class_head_p=*/false,
6672 declarator_p);
6673 if (cp_parser_parse_definitely (parser))
6674 done = true;
6676 /* In "p->S::~T", look in the scope given by "*p" as well. */
6677 else if (!done && object_scope)
6679 cp_parser_parse_tentatively (parser);
6680 parser->scope = object_scope;
6681 parser->object_scope = NULL_TREE;
6682 parser->qualifying_scope = NULL_TREE;
6683 type_decl
6684 = cp_parser_class_name (parser,
6685 /*typename_keyword_p=*/false,
6686 /*template_keyword_p=*/false,
6687 typename_type,
6688 /*check_dependency=*/false,
6689 /*class_head_p=*/false,
6690 declarator_p);
6691 if (cp_parser_parse_definitely (parser))
6692 done = true;
6694 /* Look in the surrounding context. */
6695 if (!done)
6697 parser->scope = NULL_TREE;
6698 parser->object_scope = NULL_TREE;
6699 parser->qualifying_scope = NULL_TREE;
6700 if (processing_template_decl)
6701 cp_parser_parse_tentatively (parser);
6702 type_decl
6703 = cp_parser_class_name (parser,
6704 /*typename_keyword_p=*/false,
6705 /*template_keyword_p=*/false,
6706 typename_type,
6707 /*check_dependency=*/false,
6708 /*class_head_p=*/false,
6709 declarator_p);
6710 if (processing_template_decl
6711 && ! cp_parser_parse_definitely (parser))
6713 /* We couldn't find a type with this name. If we're parsing
6714 tentatively, fail and try something else. */
6715 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6717 cp_parser_simulate_error (parser);
6718 return error_mark_node;
6720 /* Otherwise, accept it and check for a match at instantiation
6721 time. */
6722 type_decl = cp_parser_identifier (parser);
6723 if (type_decl != error_mark_node)
6724 type_decl = build_min_nt_loc (loc, BIT_NOT_EXPR, type_decl);
6725 return type_decl;
6728 /* If an error occurred, assume that the name of the
6729 destructor is the same as the name of the qualifying
6730 class. That allows us to keep parsing after running
6731 into ill-formed destructor names. */
6732 if (type_decl == error_mark_node && scope)
6733 return build_min_nt_loc (loc, BIT_NOT_EXPR, scope);
6734 else if (type_decl == error_mark_node)
6735 return error_mark_node;
6737 /* Check that destructor name and scope match. */
6738 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6740 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6741 error_at (loc,
6742 "declaration of %<~%T%> as member of %qT",
6743 type_decl, scope);
6744 cp_parser_simulate_error (parser);
6745 return error_mark_node;
6748 /* [class.dtor]
6750 A typedef-name that names a class shall not be used as the
6751 identifier in the declarator for a destructor declaration. */
6752 if (declarator_p
6753 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6754 && !DECL_SELF_REFERENCE_P (type_decl)
6755 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6756 error_at (loc,
6757 "typedef-name %qD used as destructor declarator",
6758 type_decl);
6760 return build_min_nt_loc (loc, BIT_NOT_EXPR, TREE_TYPE (type_decl));
6763 case CPP_KEYWORD:
6764 if (token->keyword == RID_OPERATOR)
6766 cp_expr id;
6768 /* This could be a template-id, so we try that first. */
6769 cp_parser_parse_tentatively (parser);
6770 /* Try a template-id. */
6771 id = cp_parser_template_id_expr (parser, template_keyword_p,
6772 /*check_dependency_p=*/true,
6773 declarator_p);
6774 /* If that worked, we're done. */
6775 if (cp_parser_parse_definitely (parser))
6776 return id;
6777 /* We still don't know whether we're looking at an
6778 operator-function-id or a conversion-function-id. */
6779 cp_parser_parse_tentatively (parser);
6780 /* Try an operator-function-id. */
6781 id = cp_parser_operator_function_id (parser);
6782 /* If that didn't work, try a conversion-function-id. */
6783 if (!cp_parser_parse_definitely (parser))
6784 id = cp_parser_conversion_function_id (parser);
6786 return id;
6788 /* Fall through. */
6790 default:
6791 if (optional_p)
6792 return NULL_TREE;
6793 cp_parser_error (parser, "expected unqualified-id");
6794 return error_mark_node;
6798 /* Check [temp.names]/5: A name prefixed by the keyword template shall
6799 be a template-id or the name shall refer to a class template or an
6800 alias template. */
6802 static void
6803 check_template_keyword_in_nested_name_spec (tree name)
6805 if (CLASS_TYPE_P (name)
6806 && ((CLASSTYPE_USE_TEMPLATE (name)
6807 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (name)))
6808 || CLASSTYPE_IS_TEMPLATE (name)))
6809 return;
6811 if (TREE_CODE (name) == TYPENAME_TYPE
6812 && TREE_CODE (TYPENAME_TYPE_FULLNAME (name)) == TEMPLATE_ID_EXPR)
6813 return;
6814 /* Alias templates are also OK. */
6815 else if (alias_template_specialization_p (name, nt_opaque))
6816 return;
6818 permerror (input_location, TYPE_P (name)
6819 ? G_("%qT is not a template")
6820 : G_("%qD is not a template"),
6821 name);
6824 /* Parse an (optional) nested-name-specifier.
6826 nested-name-specifier: [C++98]
6827 class-or-namespace-name :: nested-name-specifier [opt]
6828 class-or-namespace-name :: template nested-name-specifier [opt]
6830 nested-name-specifier: [C++0x]
6831 type-name ::
6832 namespace-name ::
6833 nested-name-specifier identifier ::
6834 nested-name-specifier template [opt] simple-template-id ::
6836 PARSER->SCOPE should be set appropriately before this function is
6837 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6838 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6839 in name lookups.
6841 Sets PARSER->SCOPE to the class (TYPE) or namespace
6842 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6843 it unchanged if there is no nested-name-specifier. Returns the new
6844 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6846 If CHECK_DEPENDENCY_P is FALSE, names are looked up in dependent scopes.
6848 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6849 part of a declaration and/or decl-specifier. */
6851 static tree
6852 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6853 bool typename_keyword_p,
6854 bool check_dependency_p,
6855 bool type_p,
6856 bool is_declaration,
6857 bool template_keyword_p /* = false */)
6859 bool success = false;
6860 cp_token_position start = 0;
6861 cp_token *token;
6863 /* Remember where the nested-name-specifier starts. */
6864 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
6865 && cp_lexer_next_token_is_not (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
6867 start = cp_lexer_token_position (parser->lexer, false);
6868 push_deferring_access_checks (dk_deferred);
6871 while (true)
6873 tree new_scope;
6874 tree old_scope;
6875 tree saved_qualifying_scope;
6877 /* Spot cases that cannot be the beginning of a
6878 nested-name-specifier. */
6879 token = cp_lexer_peek_token (parser->lexer);
6881 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6882 the already parsed nested-name-specifier. */
6883 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6885 /* Grab the nested-name-specifier and continue the loop. */
6886 cp_parser_pre_parsed_nested_name_specifier (parser);
6887 /* If we originally encountered this nested-name-specifier
6888 with CHECK_DEPENDENCY_P set to true, we will not have
6889 resolved TYPENAME_TYPEs, so we must do so here. */
6890 if (is_declaration
6891 && !check_dependency_p
6892 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6894 new_scope = resolve_typename_type (parser->scope,
6895 /*only_current_p=*/false);
6896 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6897 parser->scope = new_scope;
6899 success = true;
6900 continue;
6903 /* Spot cases that cannot be the beginning of a
6904 nested-name-specifier. On the second and subsequent times
6905 through the loop, we look for the `template' keyword. */
6906 if (success && token->keyword == RID_TEMPLATE)
6908 /* A template-id can start a nested-name-specifier. */
6909 else if (token->type == CPP_TEMPLATE_ID)
6911 /* DR 743: decltype can be used in a nested-name-specifier. */
6912 else if (token_is_decltype (token))
6914 else
6916 /* If the next token is not an identifier, then it is
6917 definitely not a type-name or namespace-name. */
6918 if (token->type != CPP_NAME)
6919 break;
6920 /* If the following token is neither a `<' (to begin a
6921 template-id), nor a `::', then we are not looking at a
6922 nested-name-specifier. */
6923 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6925 if (token->type == CPP_COLON
6926 && parser->colon_corrects_to_scope_p
6927 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME
6928 /* name:name is a valid sequence in an Objective C message. */
6929 && !parser->objective_c_message_context_p)
6931 gcc_rich_location richloc (token->location);
6932 richloc.add_fixit_replace ("::");
6933 error_at (&richloc,
6934 "found %<:%> in nested-name-specifier, "
6935 "expected %<::%>");
6936 token->type = CPP_SCOPE;
6939 if (token->type != CPP_SCOPE
6940 && !cp_parser_nth_token_starts_template_argument_list_p
6941 (parser, 2))
6942 break;
6945 /* The nested-name-specifier is optional, so we parse
6946 tentatively. */
6947 cp_parser_parse_tentatively (parser);
6949 /* Look for the optional `template' keyword, if this isn't the
6950 first time through the loop. */
6951 if (success)
6953 template_keyword_p = cp_parser_optional_template_keyword (parser);
6954 /* DR1710: "In a qualified-id used as the name in
6955 a typename-specifier, elaborated-type-specifier, using-declaration,
6956 or class-or-decltype, an optional keyword template appearing at
6957 the top level is ignored." */
6958 if (!template_keyword_p
6959 && typename_keyword_p
6960 && cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
6961 template_keyword_p = true;
6964 /* Save the old scope since the name lookup we are about to do
6965 might destroy it. */
6966 old_scope = parser->scope;
6967 saved_qualifying_scope = parser->qualifying_scope;
6968 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6969 look up names in "X<T>::I" in order to determine that "Y" is
6970 a template. So, if we have a typename at this point, we make
6971 an effort to look through it. */
6972 if (is_declaration
6973 && !check_dependency_p
6974 && !typename_keyword_p
6975 && parser->scope
6976 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6977 parser->scope = resolve_typename_type (parser->scope,
6978 /*only_current_p=*/false);
6979 /* Parse the qualifying entity. */
6980 new_scope
6981 = cp_parser_qualifying_entity (parser,
6982 typename_keyword_p,
6983 template_keyword_p,
6984 check_dependency_p,
6985 type_p,
6986 is_declaration);
6987 /* Look for the `::' token. */
6988 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6990 /* If we found what we wanted, we keep going; otherwise, we're
6991 done. */
6992 if (!cp_parser_parse_definitely (parser))
6994 bool error_p = false;
6996 /* Restore the OLD_SCOPE since it was valid before the
6997 failed attempt at finding the last
6998 class-or-namespace-name. */
6999 parser->scope = old_scope;
7000 parser->qualifying_scope = saved_qualifying_scope;
7002 /* If the next token is a decltype, and the one after that is a
7003 `::', then the decltype has failed to resolve to a class or
7004 enumeration type. Give this error even when parsing
7005 tentatively since it can't possibly be valid--and we're going
7006 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
7007 won't get another chance.*/
7008 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
7009 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
7010 == CPP_SCOPE))
7012 token = cp_lexer_consume_token (parser->lexer);
7013 tree dtype = token->u.tree_check_value->value;
7014 if (dtype != error_mark_node)
7015 error_at (token->location, "%<decltype%> evaluates to %qT, "
7016 "which is not a class or enumeration type",
7017 dtype);
7018 parser->scope = error_mark_node;
7019 error_p = true;
7020 /* As below. */
7021 success = true;
7022 cp_lexer_consume_token (parser->lexer);
7025 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
7026 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
7028 /* If we have a non-type template-id followed by ::, it can't
7029 possibly be valid. */
7030 token = cp_lexer_peek_token (parser->lexer);
7031 tree tid = token->u.tree_check_value->value;
7032 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
7033 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
7035 tree tmpl = NULL_TREE;
7036 if (is_overloaded_fn (tid))
7038 tree fns = get_fns (tid);
7039 if (OVL_SINGLE_P (fns))
7040 tmpl = OVL_FIRST (fns);
7041 if (function_concept_p (fns))
7042 error_at (token->location, "concept-id %qD "
7043 "in nested-name-specifier", tid);
7044 else
7045 error_at (token->location, "function template-id "
7046 "%qD in nested-name-specifier", tid);
7048 else
7050 tmpl = TREE_OPERAND (tid, 0);
7051 if (variable_concept_p (tmpl)
7052 || standard_concept_p (tmpl))
7053 error_at (token->location, "concept-id %qD "
7054 "in nested-name-specifier", tid);
7055 else
7057 /* Variable template. */
7058 gcc_assert (variable_template_p (tmpl));
7059 error_at (token->location, "variable template-id "
7060 "%qD in nested-name-specifier", tid);
7063 if (tmpl)
7064 inform (DECL_SOURCE_LOCATION (tmpl),
7065 "%qD declared here", tmpl);
7067 parser->scope = error_mark_node;
7068 error_p = true;
7069 /* As below. */
7070 success = true;
7071 cp_lexer_consume_token (parser->lexer);
7072 cp_lexer_consume_token (parser->lexer);
7076 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
7077 break;
7078 /* If the next token is an identifier, and the one after
7079 that is a `::', then any valid interpretation would have
7080 found a class-or-namespace-name. */
7081 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
7082 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
7083 == CPP_SCOPE)
7084 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7085 != CPP_COMPL))
7087 token = cp_lexer_consume_token (parser->lexer);
7088 if (!error_p)
7090 if (!token->error_reported)
7092 tree decl;
7093 tree ambiguous_decls;
7095 decl = cp_parser_lookup_name (parser, token->u.value,
7096 none_type,
7097 /*is_template=*/false,
7098 /*is_namespace=*/false,
7099 /*check_dependency=*/true,
7100 &ambiguous_decls,
7101 token->location);
7102 if (TREE_CODE (decl) == TEMPLATE_DECL)
7103 error_at (token->location,
7104 "%qD used without template arguments",
7105 decl);
7106 else if (ambiguous_decls)
7108 // cp_parser_lookup_name has the same diagnostic,
7109 // thus make sure to emit it at most once.
7110 if (cp_parser_uncommitted_to_tentative_parse_p
7111 (parser))
7113 error_at (token->location,
7114 "reference to %qD is ambiguous",
7115 token->u.value);
7116 print_candidates (ambiguous_decls);
7118 decl = error_mark_node;
7120 else
7122 if (cxx_dialect != cxx98)
7123 cp_parser_name_lookup_error
7124 (parser, token->u.value, decl, NLE_NOT_CXX98,
7125 token->location);
7126 else
7127 cp_parser_name_lookup_error
7128 (parser, token->u.value, decl, NLE_CXX98,
7129 token->location);
7132 parser->scope = error_mark_node;
7133 error_p = true;
7134 /* Treat this as a successful nested-name-specifier
7135 due to:
7137 [basic.lookup.qual]
7139 If the name found is not a class-name (clause
7140 _class_) or namespace-name (_namespace.def_), the
7141 program is ill-formed. */
7142 success = true;
7144 cp_lexer_consume_token (parser->lexer);
7146 break;
7148 /* We've found one valid nested-name-specifier. */
7149 success = true;
7150 /* Name lookup always gives us a DECL. */
7151 if (TREE_CODE (new_scope) == TYPE_DECL)
7152 new_scope = TREE_TYPE (new_scope);
7153 /* Uses of "template" must be followed by actual templates. */
7154 if (template_keyword_p)
7155 check_template_keyword_in_nested_name_spec (new_scope);
7156 /* If it is a class scope, try to complete it; we are about to
7157 be looking up names inside the class. */
7158 if (TYPE_P (new_scope)
7159 /* Since checking types for dependency can be expensive,
7160 avoid doing it if the type is already complete. */
7161 && !COMPLETE_TYPE_P (new_scope)
7162 /* Do not try to complete dependent types. */
7163 && !dependent_type_p (new_scope))
7165 new_scope = complete_type (new_scope);
7166 /* If it is a typedef to current class, use the current
7167 class instead, as the typedef won't have any names inside
7168 it yet. */
7169 if (!COMPLETE_TYPE_P (new_scope)
7170 && currently_open_class (new_scope))
7171 new_scope = TYPE_MAIN_VARIANT (new_scope);
7173 /* Make sure we look in the right scope the next time through
7174 the loop. */
7175 parser->scope = new_scope;
7178 /* If parsing tentatively, replace the sequence of tokens that makes
7179 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
7180 token. That way, should we re-parse the token stream, we will
7181 not have to repeat the effort required to do the parse, nor will
7182 we issue duplicate error messages. */
7183 if (success && start)
7185 cp_token *token;
7187 token = cp_lexer_token_at (parser->lexer, start);
7188 /* Reset the contents of the START token. */
7189 token->type = CPP_NESTED_NAME_SPECIFIER;
7190 /* Retrieve any deferred checks. Do not pop this access checks yet
7191 so the memory will not be reclaimed during token replacing below. */
7192 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
7193 token->tree_check_p = true;
7194 token->u.tree_check_value->value = parser->scope;
7195 token->u.tree_check_value->checks = get_deferred_access_checks ();
7196 token->u.tree_check_value->qualifying_scope =
7197 parser->qualifying_scope;
7198 token->keyword = RID_MAX;
7200 /* Purge all subsequent tokens. */
7201 cp_lexer_purge_tokens_after (parser->lexer, start);
7204 if (start)
7205 pop_to_parent_deferring_access_checks ();
7207 return success ? parser->scope : NULL_TREE;
7210 /* Parse a nested-name-specifier. See
7211 cp_parser_nested_name_specifier_opt for details. This function
7212 behaves identically, except that it will an issue an error if no
7213 nested-name-specifier is present. */
7215 static tree
7216 cp_parser_nested_name_specifier (cp_parser *parser,
7217 bool typename_keyword_p,
7218 bool check_dependency_p,
7219 bool type_p,
7220 bool is_declaration)
7222 tree scope;
7224 /* Look for the nested-name-specifier. */
7225 scope = cp_parser_nested_name_specifier_opt (parser,
7226 typename_keyword_p,
7227 check_dependency_p,
7228 type_p,
7229 is_declaration);
7230 /* If it was not present, issue an error message. */
7231 if (!scope)
7233 cp_parser_error (parser, "expected nested-name-specifier");
7234 parser->scope = NULL_TREE;
7237 return scope;
7240 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
7241 this is either a class-name or a namespace-name (which corresponds
7242 to the class-or-namespace-name production in the grammar). For
7243 C++0x, it can also be a type-name that refers to an enumeration
7244 type or a simple-template-id.
7246 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
7247 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
7248 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
7249 TYPE_P is TRUE iff the next name should be taken as a class-name,
7250 even the same name is declared to be another entity in the same
7251 scope.
7253 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
7254 specified by the class-or-namespace-name. If neither is found the
7255 ERROR_MARK_NODE is returned. */
7257 static tree
7258 cp_parser_qualifying_entity (cp_parser *parser,
7259 bool typename_keyword_p,
7260 bool template_keyword_p,
7261 bool check_dependency_p,
7262 bool type_p,
7263 bool is_declaration)
7265 tree saved_scope;
7266 tree saved_qualifying_scope;
7267 tree saved_object_scope;
7268 tree scope;
7269 bool only_class_p;
7270 bool successful_parse_p;
7272 /* DR 743: decltype can appear in a nested-name-specifier. */
7273 if (cp_lexer_next_token_is_decltype (parser->lexer))
7275 scope = cp_parser_decltype (parser);
7276 if (TREE_CODE (scope) != ENUMERAL_TYPE
7277 && !MAYBE_CLASS_TYPE_P (scope))
7279 cp_parser_simulate_error (parser);
7280 return error_mark_node;
7282 if (TYPE_NAME (scope))
7283 scope = TYPE_NAME (scope);
7284 return scope;
7287 /* Before we try to parse the class-name, we must save away the
7288 current PARSER->SCOPE since cp_parser_class_name will destroy
7289 it. */
7290 saved_scope = parser->scope;
7291 saved_qualifying_scope = parser->qualifying_scope;
7292 saved_object_scope = parser->object_scope;
7293 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
7294 there is no need to look for a namespace-name. */
7295 only_class_p = template_keyword_p
7296 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
7297 if (!only_class_p)
7298 cp_parser_parse_tentatively (parser);
7299 scope = cp_parser_class_name (parser,
7300 typename_keyword_p,
7301 template_keyword_p,
7302 type_p ? class_type : none_type,
7303 check_dependency_p,
7304 /*class_head_p=*/false,
7305 is_declaration,
7306 /*enum_ok=*/cxx_dialect > cxx98);
7307 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
7308 /* If that didn't work, try for a namespace-name. */
7309 if (!only_class_p && !successful_parse_p)
7311 /* Restore the saved scope. */
7312 parser->scope = saved_scope;
7313 parser->qualifying_scope = saved_qualifying_scope;
7314 parser->object_scope = saved_object_scope;
7315 /* If we are not looking at an identifier followed by the scope
7316 resolution operator, then this is not part of a
7317 nested-name-specifier. (Note that this function is only used
7318 to parse the components of a nested-name-specifier.) */
7319 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
7320 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
7321 return error_mark_node;
7322 scope = cp_parser_namespace_name (parser);
7325 return scope;
7328 /* Return true if we are looking at a compound-literal, false otherwise. */
7330 static bool
7331 cp_parser_compound_literal_p (cp_parser *parser)
7333 cp_lexer_save_tokens (parser->lexer);
7335 /* Skip tokens until the next token is a closing parenthesis.
7336 If we find the closing `)', and the next token is a `{', then
7337 we are looking at a compound-literal. */
7338 bool compound_literal_p
7339 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7340 /*consume_paren=*/true)
7341 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
7343 /* Roll back the tokens we skipped. */
7344 cp_lexer_rollback_tokens (parser->lexer);
7346 return compound_literal_p;
7349 /* Return true if EXPR is the integer constant zero or a complex constant
7350 of zero, without any folding, but ignoring location wrappers. */
7352 bool
7353 literal_integer_zerop (const_tree expr)
7355 return (location_wrapper_p (expr)
7356 && integer_zerop (TREE_OPERAND (expr, 0)));
7359 /* Parse a postfix-expression.
7361 postfix-expression:
7362 primary-expression
7363 postfix-expression [ expression ]
7364 postfix-expression ( expression-list [opt] )
7365 simple-type-specifier ( expression-list [opt] )
7366 typename :: [opt] nested-name-specifier identifier
7367 ( expression-list [opt] )
7368 typename :: [opt] nested-name-specifier template [opt] template-id
7369 ( expression-list [opt] )
7370 postfix-expression . template [opt] id-expression
7371 postfix-expression -> template [opt] id-expression
7372 postfix-expression . pseudo-destructor-name
7373 postfix-expression -> pseudo-destructor-name
7374 postfix-expression ++
7375 postfix-expression --
7376 dynamic_cast < type-id > ( expression )
7377 static_cast < type-id > ( expression )
7378 reinterpret_cast < type-id > ( expression )
7379 const_cast < type-id > ( expression )
7380 typeid ( expression )
7381 typeid ( type-id )
7383 GNU Extension:
7385 postfix-expression:
7386 ( type-id ) { initializer-list , [opt] }
7388 This extension is a GNU version of the C99 compound-literal
7389 construct. (The C99 grammar uses `type-name' instead of `type-id',
7390 but they are essentially the same concept.)
7392 If ADDRESS_P is true, the postfix expression is the operand of the
7393 `&' operator. CAST_P is true if this expression is the target of a
7394 cast.
7396 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
7397 class member access expressions [expr.ref].
7399 Returns a representation of the expression. */
7401 static cp_expr
7402 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
7403 bool member_access_only_p, bool decltype_p,
7404 cp_id_kind * pidk_return)
7406 cp_token *token;
7407 location_t loc;
7408 enum rid keyword;
7409 cp_id_kind idk = CP_ID_KIND_NONE;
7410 cp_expr postfix_expression = NULL_TREE;
7411 bool is_member_access = false;
7413 /* Peek at the next token. */
7414 token = cp_lexer_peek_token (parser->lexer);
7415 loc = token->location;
7416 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
7418 /* Some of the productions are determined by keywords. */
7419 keyword = token->keyword;
7420 switch (keyword)
7422 case RID_DYNCAST:
7423 case RID_STATCAST:
7424 case RID_REINTCAST:
7425 case RID_CONSTCAST:
7427 tree type;
7428 cp_expr expression;
7429 const char *saved_message;
7430 bool saved_in_type_id_in_expr_p;
7432 /* All of these can be handled in the same way from the point
7433 of view of parsing. Begin by consuming the token
7434 identifying the cast. */
7435 cp_lexer_consume_token (parser->lexer);
7437 /* New types cannot be defined in the cast. */
7438 saved_message = parser->type_definition_forbidden_message;
7439 parser->type_definition_forbidden_message
7440 = G_("types may not be defined in casts");
7442 /* Look for the opening `<'. */
7443 cp_parser_require (parser, CPP_LESS, RT_LESS);
7444 /* Parse the type to which we are casting. */
7445 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7446 parser->in_type_id_in_expr_p = true;
7447 type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
7448 NULL);
7449 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7450 /* Look for the closing `>'. */
7451 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
7452 /* Restore the old message. */
7453 parser->type_definition_forbidden_message = saved_message;
7455 bool saved_greater_than_is_operator_p
7456 = parser->greater_than_is_operator_p;
7457 parser->greater_than_is_operator_p = true;
7459 /* And the expression which is being cast. */
7460 matching_parens parens;
7461 parens.require_open (parser);
7462 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
7463 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
7464 RT_CLOSE_PAREN);
7465 location_t end_loc = close_paren ?
7466 close_paren->location : UNKNOWN_LOCATION;
7468 parser->greater_than_is_operator_p
7469 = saved_greater_than_is_operator_p;
7471 /* Only type conversions to integral or enumeration types
7472 can be used in constant-expressions. */
7473 if (!cast_valid_in_integral_constant_expression_p (type)
7474 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
7476 postfix_expression = error_mark_node;
7477 break;
7480 /* Construct a location e.g. :
7481 reinterpret_cast <int *> (expr)
7482 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7483 ranging from the start of the "*_cast" token to the final closing
7484 paren, with the caret at the start. */
7485 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
7487 switch (keyword)
7489 case RID_DYNCAST:
7490 postfix_expression
7491 = build_dynamic_cast (cp_cast_loc, type, expression,
7492 tf_warning_or_error);
7493 break;
7494 case RID_STATCAST:
7495 postfix_expression
7496 = build_static_cast (cp_cast_loc, type, expression,
7497 tf_warning_or_error);
7498 break;
7499 case RID_REINTCAST:
7500 postfix_expression
7501 = build_reinterpret_cast (cp_cast_loc, type, expression,
7502 tf_warning_or_error);
7503 break;
7504 case RID_CONSTCAST:
7505 postfix_expression
7506 = build_const_cast (cp_cast_loc, type, expression,
7507 tf_warning_or_error);
7508 break;
7509 default:
7510 gcc_unreachable ();
7513 break;
7515 case RID_TYPEID:
7517 tree type;
7518 const char *saved_message;
7519 bool saved_in_type_id_in_expr_p;
7521 /* Consume the `typeid' token. */
7522 cp_lexer_consume_token (parser->lexer);
7523 /* Look for the `(' token. */
7524 matching_parens parens;
7525 parens.require_open (parser);
7526 /* Types cannot be defined in a `typeid' expression. */
7527 saved_message = parser->type_definition_forbidden_message;
7528 parser->type_definition_forbidden_message
7529 = G_("types may not be defined in a %<typeid%> expression");
7530 /* We can't be sure yet whether we're looking at a type-id or an
7531 expression. */
7532 cp_parser_parse_tentatively (parser);
7533 /* Try a type-id first. */
7534 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7535 parser->in_type_id_in_expr_p = true;
7536 type = cp_parser_type_id (parser);
7537 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7538 /* Look for the `)' token. Otherwise, we can't be sure that
7539 we're not looking at an expression: consider `typeid (int
7540 (3))', for example. */
7541 cp_token *close_paren = parens.require_close (parser);
7542 /* If all went well, simply lookup the type-id. */
7543 if (cp_parser_parse_definitely (parser))
7544 postfix_expression = get_typeid (type, tf_warning_or_error);
7545 /* Otherwise, fall back to the expression variant. */
7546 else
7548 tree expression;
7550 /* Look for an expression. */
7551 expression = cp_parser_expression (parser, & idk);
7552 /* Compute its typeid. */
7553 postfix_expression = build_typeid (expression, tf_warning_or_error);
7554 /* Look for the `)' token. */
7555 close_paren = parens.require_close (parser);
7557 /* Restore the saved message. */
7558 parser->type_definition_forbidden_message = saved_message;
7559 /* `typeid' may not appear in an integral constant expression. */
7560 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
7561 postfix_expression = error_mark_node;
7563 /* Construct a location e.g. :
7564 typeid (expr)
7565 ^~~~~~~~~~~~~
7566 ranging from the start of the "typeid" token to the final closing
7567 paren, with the caret at the start. */
7568 if (close_paren)
7570 location_t typeid_loc
7571 = make_location (start_loc, start_loc, close_paren->location);
7572 postfix_expression.set_location (typeid_loc);
7573 postfix_expression.maybe_add_location_wrapper ();
7576 break;
7578 case RID_TYPENAME:
7580 tree type;
7581 /* The syntax permitted here is the same permitted for an
7582 elaborated-type-specifier. */
7583 ++parser->prevent_constrained_type_specifiers;
7584 type = cp_parser_elaborated_type_specifier (parser,
7585 /*is_friend=*/false,
7586 /*is_declaration=*/false);
7587 --parser->prevent_constrained_type_specifiers;
7588 postfix_expression = cp_parser_functional_cast (parser, type);
7590 break;
7592 case RID_ADDRESSOF:
7593 case RID_BUILTIN_SHUFFLE:
7594 case RID_BUILTIN_SHUFFLEVECTOR:
7595 case RID_BUILTIN_LAUNDER:
7596 case RID_BUILTIN_ASSOC_BARRIER:
7598 vec<tree, va_gc> *vec;
7600 cp_lexer_consume_token (parser->lexer);
7601 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
7602 /*cast_p=*/false, /*allow_expansion_p=*/true,
7603 /*non_constant_p=*/NULL);
7604 if (vec == NULL)
7606 postfix_expression = error_mark_node;
7607 break;
7610 for (tree p : *vec)
7611 mark_exp_read (p);
7613 switch (keyword)
7615 case RID_ADDRESSOF:
7616 if (vec->length () == 1)
7617 postfix_expression
7618 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
7619 else
7621 error_at (loc, "wrong number of arguments to "
7622 "%<__builtin_addressof%>");
7623 postfix_expression = error_mark_node;
7625 break;
7627 case RID_BUILTIN_LAUNDER:
7628 if (vec->length () == 1)
7629 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
7630 tf_warning_or_error);
7631 else
7633 error_at (loc, "wrong number of arguments to "
7634 "%<__builtin_launder%>");
7635 postfix_expression = error_mark_node;
7637 break;
7639 case RID_BUILTIN_ASSOC_BARRIER:
7640 if (vec->length () == 1)
7641 postfix_expression = build1_loc (loc, PAREN_EXPR,
7642 TREE_TYPE ((*vec)[0]),
7643 (*vec)[0]);
7644 else
7646 error_at (loc, "wrong number of arguments to "
7647 "%<__builtin_assoc_barrier%>");
7648 postfix_expression = error_mark_node;
7650 break;
7652 case RID_BUILTIN_SHUFFLE:
7653 if (vec->length () == 2)
7654 postfix_expression
7655 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
7656 (*vec)[1], tf_warning_or_error);
7657 else if (vec->length () == 3)
7658 postfix_expression
7659 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
7660 (*vec)[2], tf_warning_or_error);
7661 else
7663 error_at (loc, "wrong number of arguments to "
7664 "%<__builtin_shuffle%>");
7665 postfix_expression = error_mark_node;
7667 break;
7669 case RID_BUILTIN_SHUFFLEVECTOR:
7670 if (vec->length () < 3)
7672 error_at (loc, "wrong number of arguments to "
7673 "%<__builtin_shufflevector%>");
7674 postfix_expression = error_mark_node;
7676 else
7678 postfix_expression
7679 = build_x_shufflevector (loc, vec, tf_warning_or_error);
7681 break;
7683 default:
7684 gcc_unreachable ();
7686 break;
7689 case RID_BUILTIN_CONVERTVECTOR:
7691 tree expression;
7692 tree type;
7693 /* Consume the `__builtin_convertvector' token. */
7694 cp_lexer_consume_token (parser->lexer);
7695 /* Look for the opening `('. */
7696 matching_parens parens;
7697 parens.require_open (parser);
7698 /* Now, parse the assignment-expression. */
7699 expression = cp_parser_assignment_expression (parser);
7700 /* Look for the `,'. */
7701 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7702 location_t type_location
7703 = cp_lexer_peek_token (parser->lexer)->location;
7704 /* Parse the type-id. */
7706 type_id_in_expr_sentinel s (parser);
7707 type = cp_parser_type_id (parser);
7709 /* Look for the closing `)'. */
7710 parens.require_close (parser);
7711 postfix_expression
7712 = cp_build_vec_convert (expression, type_location, type,
7713 tf_warning_or_error);
7714 break;
7717 case RID_BUILTIN_BIT_CAST:
7719 tree expression;
7720 tree type;
7721 /* Consume the `__builtin_bit_cast' token. */
7722 cp_lexer_consume_token (parser->lexer);
7723 /* Look for the opening `('. */
7724 matching_parens parens;
7725 parens.require_open (parser);
7726 location_t type_location
7727 = cp_lexer_peek_token (parser->lexer)->location;
7728 /* Parse the type-id. */
7730 type_id_in_expr_sentinel s (parser);
7731 type = cp_parser_type_id (parser);
7733 /* Look for the `,'. */
7734 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7735 /* Now, parse the assignment-expression. */
7736 expression = cp_parser_assignment_expression (parser);
7737 /* Look for the closing `)'. */
7738 parens.require_close (parser);
7739 postfix_expression
7740 = cp_build_bit_cast (type_location, type, expression,
7741 tf_warning_or_error);
7742 break;
7745 default:
7747 tree type;
7749 /* If the next thing is a simple-type-specifier, we may be
7750 looking at a functional cast. We could also be looking at
7751 an id-expression. So, we try the functional cast, and if
7752 that doesn't work we fall back to the primary-expression. */
7753 cp_parser_parse_tentatively (parser);
7754 /* Look for the simple-type-specifier. */
7755 ++parser->prevent_constrained_type_specifiers;
7756 type = cp_parser_simple_type_specifier (parser,
7757 /*decl_specs=*/NULL,
7758 CP_PARSER_FLAGS_NONE);
7759 --parser->prevent_constrained_type_specifiers;
7760 /* Parse the cast itself. */
7761 if (!cp_parser_error_occurred (parser))
7762 postfix_expression
7763 = cp_parser_functional_cast (parser, type);
7764 /* If that worked, we're done. */
7765 if (cp_parser_parse_definitely (parser))
7766 break;
7768 /* If the functional-cast didn't work out, try a
7769 compound-literal. */
7770 if (cp_parser_allow_gnu_extensions_p (parser)
7771 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7773 cp_expr initializer = NULL_TREE;
7775 cp_parser_parse_tentatively (parser);
7777 matching_parens parens;
7778 parens.consume_open (parser);
7780 /* Avoid calling cp_parser_type_id pointlessly, see comment
7781 in cp_parser_cast_expression about c++/29234. */
7782 if (!cp_parser_compound_literal_p (parser))
7783 cp_parser_simulate_error (parser);
7784 else
7786 /* Parse the type. */
7787 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7788 parser->in_type_id_in_expr_p = true;
7789 type = cp_parser_type_id (parser);
7790 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7791 parens.require_close (parser);
7794 /* If things aren't going well, there's no need to
7795 keep going. */
7796 if (!cp_parser_error_occurred (parser))
7797 /* Parse the brace-enclosed initializer list. */
7798 initializer = cp_parser_braced_list (parser);
7799 /* If that worked, we're definitely looking at a
7800 compound-literal expression. */
7801 if (cp_parser_parse_definitely (parser))
7803 /* Warn the user that a compound literal is not
7804 allowed in standard C++. */
7805 pedwarn (input_location, OPT_Wpedantic,
7806 "ISO C++ forbids compound-literals");
7807 /* For simplicity, we disallow compound literals in
7808 constant-expressions. We could
7809 allow compound literals of integer type, whose
7810 initializer was a constant, in constant
7811 expressions. Permitting that usage, as a further
7812 extension, would not change the meaning of any
7813 currently accepted programs. (Of course, as
7814 compound literals are not part of ISO C++, the
7815 standard has nothing to say.) */
7816 if (cp_parser_non_integral_constant_expression (parser,
7817 NIC_NCC))
7819 postfix_expression = error_mark_node;
7820 break;
7822 /* Form the representation of the compound-literal. */
7823 postfix_expression
7824 = finish_compound_literal (type, initializer,
7825 tf_warning_or_error, fcl_c99);
7826 postfix_expression.set_location (initializer.get_location ());
7827 break;
7831 /* It must be a primary-expression. */
7832 postfix_expression
7833 = cp_parser_primary_expression (parser, address_p, cast_p,
7834 /*template_arg_p=*/false,
7835 decltype_p,
7836 &idk);
7838 break;
7841 /* Note that we don't need to worry about calling build_cplus_new on a
7842 class-valued CALL_EXPR in decltype when it isn't the end of the
7843 postfix-expression; unary_complex_lvalue will take care of that for
7844 all these cases. */
7846 /* Keep looping until the postfix-expression is complete. */
7847 while (true)
7849 if (idk == CP_ID_KIND_UNQUALIFIED
7850 && identifier_p (postfix_expression)
7851 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7852 /* It is not a Koenig lookup function call. */
7853 postfix_expression
7854 = unqualified_name_lookup_error (postfix_expression);
7856 /* Peek at the next token. */
7857 token = cp_lexer_peek_token (parser->lexer);
7859 switch (token->type)
7861 case CPP_OPEN_SQUARE:
7862 if (cp_next_tokens_can_be_std_attribute_p (parser))
7864 cp_parser_error (parser,
7865 "two consecutive %<[%> shall "
7866 "only introduce an attribute");
7867 return error_mark_node;
7869 postfix_expression
7870 = cp_parser_postfix_open_square_expression (parser,
7871 postfix_expression,
7872 false,
7873 decltype_p);
7874 postfix_expression.set_range (start_loc,
7875 postfix_expression.get_location ());
7877 idk = CP_ID_KIND_NONE;
7878 is_member_access = false;
7879 break;
7881 case CPP_OPEN_PAREN:
7882 /* postfix-expression ( expression-list [opt] ) */
7884 bool koenig_p;
7885 bool is_builtin_constant_p;
7886 bool saved_integral_constant_expression_p = false;
7887 bool saved_non_integral_constant_expression_p = false;
7888 tsubst_flags_t complain = complain_flags (decltype_p);
7889 vec<tree, va_gc> *args;
7890 location_t close_paren_loc = UNKNOWN_LOCATION;
7891 location_t combined_loc = UNKNOWN_LOCATION;
7893 is_member_access = false;
7895 tree stripped_expression
7896 = tree_strip_any_location_wrapper (postfix_expression);
7897 is_builtin_constant_p
7898 = DECL_IS_BUILTIN_CONSTANT_P (stripped_expression);
7899 if (is_builtin_constant_p)
7901 /* The whole point of __builtin_constant_p is to allow
7902 non-constant expressions to appear as arguments. */
7903 saved_integral_constant_expression_p
7904 = parser->integral_constant_expression_p;
7905 saved_non_integral_constant_expression_p
7906 = parser->non_integral_constant_expression_p;
7907 parser->integral_constant_expression_p = false;
7909 args = (cp_parser_parenthesized_expression_list
7910 (parser, non_attr,
7911 /*cast_p=*/false, /*allow_expansion_p=*/true,
7912 /*non_constant_p=*/NULL,
7913 /*close_paren_loc=*/&close_paren_loc,
7914 /*wrap_locations_p=*/true));
7915 if (is_builtin_constant_p)
7917 parser->integral_constant_expression_p
7918 = saved_integral_constant_expression_p;
7919 parser->non_integral_constant_expression_p
7920 = saved_non_integral_constant_expression_p;
7923 if (args == NULL)
7925 postfix_expression = error_mark_node;
7926 break;
7929 /* Function calls are not permitted in
7930 constant-expressions. */
7931 if (! builtin_valid_in_constant_expr_p (postfix_expression)
7932 && cp_parser_non_integral_constant_expression (parser,
7933 NIC_FUNC_CALL))
7935 postfix_expression = error_mark_node;
7936 release_tree_vector (args);
7937 break;
7940 koenig_p = false;
7941 if (idk == CP_ID_KIND_UNQUALIFIED
7942 || idk == CP_ID_KIND_TEMPLATE_ID)
7944 if (identifier_p (postfix_expression)
7945 /* In C++20, we may need to perform ADL for a template
7946 name. */
7947 || (TREE_CODE (postfix_expression) == TEMPLATE_ID_EXPR
7948 && identifier_p (TREE_OPERAND (postfix_expression, 0))))
7950 if (!args->is_empty ())
7952 koenig_p = true;
7953 if (!any_type_dependent_arguments_p (args))
7954 postfix_expression
7955 = perform_koenig_lookup (postfix_expression, args,
7956 complain);
7958 else
7959 postfix_expression
7960 = unqualified_fn_lookup_error (postfix_expression);
7962 /* We do not perform argument-dependent lookup if
7963 normal lookup finds a non-function, in accordance
7964 with the expected resolution of DR 218. */
7965 else if (!args->is_empty ()
7966 && is_overloaded_fn (postfix_expression))
7968 /* Do not do argument dependent lookup if regular
7969 lookup finds a member function or a block-scope
7970 function declaration. [basic.lookup.argdep]/3 */
7971 bool do_adl_p = true;
7972 tree fns = get_fns (postfix_expression);
7973 for (lkp_iterator iter (fns); iter; ++iter)
7975 tree fn = STRIP_TEMPLATE (*iter);
7976 if ((TREE_CODE (fn) == USING_DECL
7977 && DECL_DEPENDENT_P (fn))
7978 || DECL_FUNCTION_MEMBER_P (fn)
7979 || DECL_LOCAL_DECL_P (fn))
7981 do_adl_p = false;
7982 break;
7986 if (do_adl_p)
7988 koenig_p = true;
7989 if (!any_type_dependent_arguments_p (args))
7990 postfix_expression
7991 = perform_koenig_lookup (postfix_expression, args,
7992 complain);
7997 /* Temporarily set input_location to the combined location
7998 with call expression range, as e.g. build_out_target_exprs
7999 called from convert_default_arg relies on input_location,
8000 so updating it only when the call is fully built results
8001 in inconsistencies between location handling in templates
8002 and outside of templates. */
8003 if (close_paren_loc != UNKNOWN_LOCATION)
8004 combined_loc = make_location (token->location, start_loc,
8005 close_paren_loc);
8006 iloc_sentinel ils (combined_loc);
8008 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
8010 tree instance = TREE_OPERAND (postfix_expression, 0);
8011 tree fn = TREE_OPERAND (postfix_expression, 1);
8013 if (processing_template_decl
8014 && (type_dependent_object_expression_p (instance)
8015 || (!BASELINK_P (fn)
8016 && TREE_CODE (fn) != FIELD_DECL)
8017 || type_dependent_expression_p (fn)
8018 || any_type_dependent_arguments_p (args)))
8020 maybe_generic_this_capture (instance, fn);
8021 postfix_expression
8022 = build_min_nt_call_vec (postfix_expression, args);
8024 else if (BASELINK_P (fn))
8026 postfix_expression
8027 = (build_new_method_call
8028 (instance, fn, &args, NULL_TREE,
8029 (idk == CP_ID_KIND_QUALIFIED
8030 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
8031 : LOOKUP_NORMAL),
8032 /*fn_p=*/NULL,
8033 complain));
8035 else
8036 postfix_expression
8037 = finish_call_expr (postfix_expression, &args,
8038 /*disallow_virtual=*/false,
8039 /*koenig_p=*/false,
8040 complain);
8042 else if (TREE_CODE (postfix_expression) == OFFSET_REF
8043 || TREE_CODE (postfix_expression) == MEMBER_REF
8044 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
8045 postfix_expression = (build_offset_ref_call_from_tree
8046 (postfix_expression, &args,
8047 complain));
8048 else if (idk == CP_ID_KIND_QUALIFIED)
8049 /* A call to a static class member, or a namespace-scope
8050 function. */
8051 postfix_expression
8052 = finish_call_expr (postfix_expression, &args,
8053 /*disallow_virtual=*/true,
8054 koenig_p,
8055 complain);
8056 else
8057 /* All other function calls. */
8059 if (DECL_P (postfix_expression)
8060 && parser->omp_for_parse_state
8061 && parser->omp_for_parse_state->in_intervening_code
8062 && omp_runtime_api_call (postfix_expression))
8064 error_at (loc, "calls to the OpenMP runtime API are "
8065 "not permitted in intervening code");
8066 parser->omp_for_parse_state->fail = true;
8068 postfix_expression
8069 = finish_call_expr (postfix_expression, &args,
8070 /*disallow_virtual=*/false,
8071 koenig_p,
8072 complain);
8074 if (close_paren_loc != UNKNOWN_LOCATION)
8075 postfix_expression.set_location (combined_loc);
8077 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
8078 idk = CP_ID_KIND_NONE;
8080 release_tree_vector (args);
8082 break;
8084 case CPP_DOT:
8085 case CPP_DEREF:
8086 /* postfix-expression . template [opt] id-expression
8087 postfix-expression . pseudo-destructor-name
8088 postfix-expression -> template [opt] id-expression
8089 postfix-expression -> pseudo-destructor-name */
8091 /* Consume the `.' or `->' operator. */
8092 cp_lexer_consume_token (parser->lexer);
8094 postfix_expression
8095 = cp_parser_postfix_dot_deref_expression (parser, token->type,
8096 postfix_expression,
8097 false, &idk, loc);
8099 is_member_access = true;
8100 break;
8102 case CPP_PLUS_PLUS:
8103 /* postfix-expression ++ */
8104 /* Consume the `++' token. */
8105 cp_lexer_consume_token (parser->lexer);
8106 /* Generate a representation for the complete expression. */
8107 postfix_expression
8108 = finish_increment_expr (postfix_expression,
8109 POSTINCREMENT_EXPR);
8110 /* Increments may not appear in constant-expressions. */
8111 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
8112 postfix_expression = error_mark_node;
8113 idk = CP_ID_KIND_NONE;
8114 is_member_access = false;
8115 break;
8117 case CPP_MINUS_MINUS:
8118 /* postfix-expression -- */
8119 /* Consume the `--' token. */
8120 cp_lexer_consume_token (parser->lexer);
8121 /* Generate a representation for the complete expression. */
8122 postfix_expression
8123 = finish_increment_expr (postfix_expression,
8124 POSTDECREMENT_EXPR);
8125 /* Decrements may not appear in constant-expressions. */
8126 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
8127 postfix_expression = error_mark_node;
8128 idk = CP_ID_KIND_NONE;
8129 is_member_access = false;
8130 break;
8132 default:
8133 if (pidk_return != NULL)
8134 * pidk_return = idk;
8135 if (member_access_only_p)
8136 return is_member_access
8137 ? postfix_expression
8138 : cp_expr (error_mark_node);
8139 else
8140 return postfix_expression;
8145 /* Helper function for cp_parser_parenthesized_expression_list and
8146 cp_parser_postfix_open_square_expression. Parse a single element
8147 of parenthesized expression list. */
8149 static cp_expr
8150 cp_parser_parenthesized_expression_list_elt (cp_parser *parser, bool cast_p,
8151 bool allow_expansion_p,
8152 bool *non_constant_p)
8154 cp_expr expr (NULL_TREE);
8155 bool expr_non_constant_p;
8157 /* Parse the next assignment-expression. */
8158 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8160 /* A braced-init-list. */
8161 cp_lexer_set_source_position (parser->lexer);
8162 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8163 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8164 if (non_constant_p && expr_non_constant_p)
8165 *non_constant_p = true;
8167 else if (non_constant_p)
8169 expr = cp_parser_constant_expression (parser,
8170 /*allow_non_constant_p=*/true,
8171 &expr_non_constant_p);
8172 if (expr_non_constant_p)
8173 *non_constant_p = true;
8175 else
8176 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL, cast_p);
8178 /* If we have an ellipsis, then this is an expression expansion. */
8179 if (allow_expansion_p
8180 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8182 /* Consume the `...'. */
8183 cp_lexer_consume_token (parser->lexer);
8185 /* Build the argument pack. */
8186 expr = make_pack_expansion (expr);
8188 return expr;
8191 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
8192 by cp_parser_builtin_offsetof. We're looking for
8194 postfix-expression [ expression ]
8195 postfix-expression [ braced-init-list ] (C++11)
8196 postfix-expression [ expression-list[opt] ] (C++23)
8198 FOR_OFFSETOF is set if we're being called in that context, which
8199 changes how we deal with integer constant expressions. */
8201 static tree
8202 cp_parser_postfix_open_square_expression (cp_parser *parser,
8203 tree postfix_expression,
8204 bool for_offsetof,
8205 bool decltype_p)
8207 tree index = NULL_TREE;
8208 releasing_vec expression_list = NULL;
8209 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8210 bool saved_greater_than_is_operator_p;
8212 /* Consume the `[' token. */
8213 cp_lexer_consume_token (parser->lexer);
8215 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
8216 parser->greater_than_is_operator_p = true;
8218 /* Parse the index expression. */
8219 /* ??? For offsetof, there is a question of what to allow here. If
8220 offsetof is not being used in an integral constant expression context,
8221 then we *could* get the right answer by computing the value at runtime.
8222 If we are in an integral constant expression context, then we might
8223 could accept any constant expression; hard to say without analysis.
8224 Rather than open the barn door too wide right away, allow only integer
8225 constant expressions here. */
8226 if (for_offsetof)
8227 index = cp_parser_constant_expression (parser);
8228 else
8230 if (cxx_dialect >= cxx23
8231 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
8232 *&expression_list = make_tree_vector ();
8233 else if (cxx_dialect >= cxx23)
8235 while (true)
8237 cp_expr expr
8238 = cp_parser_parenthesized_expression_list_elt (parser,
8239 /*cast_p=*/
8240 false,
8241 /*allow_exp_p=*/
8242 true,
8243 /*non_cst_p=*/
8244 NULL);
8246 if (expr == error_mark_node)
8247 index = error_mark_node;
8248 else if (expression_list.get () == NULL
8249 && !PACK_EXPANSION_P (expr.get_value ()))
8250 index = expr.get_value ();
8251 else
8252 vec_safe_push (expression_list, expr.get_value ());
8254 /* If the next token isn't a `,', then we are done. */
8255 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8256 break;
8258 if (expression_list.get () == NULL && index != error_mark_node)
8260 *&expression_list = make_tree_vector_single (index);
8261 index = NULL_TREE;
8264 /* Otherwise, consume the `,' and keep going. */
8265 cp_lexer_consume_token (parser->lexer);
8267 if (expression_list.get () && index == error_mark_node)
8268 expression_list.release ();
8270 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8272 cp_lexer_set_source_position (parser->lexer);
8273 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8274 index = cp_parser_braced_list (parser);
8276 else
8277 index = cp_parser_expression (parser, NULL, /*cast_p=*/false,
8278 /*decltype_p=*/false,
8279 /*warn_comma_p=*/warn_comma_subscript);
8282 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
8284 /* Look for the closing `]'. */
8285 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8287 /* Build the ARRAY_REF. */
8288 postfix_expression = grok_array_decl (loc, postfix_expression,
8289 index, &expression_list,
8290 tf_warning_or_error
8291 | (decltype_p ? tf_decltype : 0));
8293 /* When not doing offsetof, array references are not permitted in
8294 constant-expressions. */
8295 if (!for_offsetof
8296 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
8297 postfix_expression = error_mark_node;
8299 return postfix_expression;
8302 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
8303 dereference of incomplete type, returns true if error_mark_node should
8304 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
8305 and *DEPENDENT_P. */
8307 bool
8308 cp_parser_dot_deref_incomplete (tree *scope, cp_expr *postfix_expression,
8309 bool *dependent_p)
8311 /* In a template, be permissive by treating an object expression
8312 of incomplete type as dependent (after a pedwarn). */
8313 diagnostic_t kind = (processing_template_decl
8314 && MAYBE_CLASS_TYPE_P (*scope) ? DK_PEDWARN : DK_ERROR);
8316 switch (TREE_CODE (*postfix_expression))
8318 case CAST_EXPR:
8319 case REINTERPRET_CAST_EXPR:
8320 case CONST_CAST_EXPR:
8321 case STATIC_CAST_EXPR:
8322 case DYNAMIC_CAST_EXPR:
8323 case IMPLICIT_CONV_EXPR:
8324 case VIEW_CONVERT_EXPR:
8325 case NON_LVALUE_EXPR:
8326 kind = DK_ERROR;
8327 break;
8328 case OVERLOAD:
8329 /* Don't emit any diagnostic for OVERLOADs. */
8330 kind = DK_IGNORED;
8331 break;
8332 default:
8333 /* Avoid clobbering e.g. DECLs. */
8334 if (!EXPR_P (*postfix_expression))
8335 kind = DK_ERROR;
8336 break;
8339 if (kind == DK_IGNORED)
8340 return false;
8342 location_t exploc = location_of (*postfix_expression);
8343 cxx_incomplete_type_diagnostic (exploc, *postfix_expression, *scope, kind);
8344 if (!MAYBE_CLASS_TYPE_P (*scope))
8345 return true;
8346 if (kind == DK_ERROR)
8347 *scope = *postfix_expression = error_mark_node;
8348 else if (processing_template_decl)
8350 *dependent_p = true;
8351 *scope = TREE_TYPE (*postfix_expression) = NULL_TREE;
8353 return false;
8356 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
8357 by cp_parser_builtin_offsetof. We're looking for
8359 postfix-expression . template [opt] id-expression
8360 postfix-expression . pseudo-destructor-name
8361 postfix-expression -> template [opt] id-expression
8362 postfix-expression -> pseudo-destructor-name
8364 FOR_OFFSETOF is set if we're being called in that context. That sorta
8365 limits what of the above we'll actually accept, but nevermind.
8366 TOKEN_TYPE is the "." or "->" token, which will already have been
8367 removed from the stream. */
8369 static tree
8370 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
8371 enum cpp_ttype token_type,
8372 cp_expr postfix_expression,
8373 bool for_offsetof, cp_id_kind *idk,
8374 location_t location)
8376 tree name;
8377 bool dependent_p;
8378 bool pseudo_destructor_p;
8379 tree scope = NULL_TREE;
8380 location_t start_loc = postfix_expression.get_start ();
8382 /* If this is a `->' operator, dereference the pointer. */
8383 if (token_type == CPP_DEREF)
8384 postfix_expression = build_x_arrow (location, postfix_expression,
8385 tf_warning_or_error);
8386 /* Check to see whether or not the expression is type-dependent and
8387 not the current instantiation. */
8388 dependent_p = type_dependent_object_expression_p (postfix_expression);
8389 /* The identifier following the `->' or `.' is not qualified. */
8390 parser->scope = NULL_TREE;
8391 parser->qualifying_scope = NULL_TREE;
8392 parser->object_scope = NULL_TREE;
8393 *idk = CP_ID_KIND_NONE;
8395 /* Enter the scope corresponding to the type of the object
8396 given by the POSTFIX_EXPRESSION. */
8397 if (!dependent_p)
8399 scope = TREE_TYPE (postfix_expression);
8400 /* According to the standard, no expression should ever have
8401 reference type. Unfortunately, we do not currently match
8402 the standard in this respect in that our internal representation
8403 of an expression may have reference type even when the standard
8404 says it does not. Therefore, we have to manually obtain the
8405 underlying type here. */
8406 scope = non_reference (scope);
8407 /* The type of the POSTFIX_EXPRESSION must be complete. */
8408 /* Unlike the object expression in other contexts, *this is not
8409 required to be of complete type for purposes of class member
8410 access (5.2.5) outside the member function body. */
8411 if (postfix_expression != current_class_ref
8412 && scope != error_mark_node
8413 && !currently_open_class (scope))
8415 scope = complete_type (scope);
8416 if (!COMPLETE_TYPE_P (scope)
8417 && cp_parser_dot_deref_incomplete (&scope, &postfix_expression,
8418 &dependent_p))
8419 return error_mark_node;
8422 if (!dependent_p)
8424 /* Let the name lookup machinery know that we are processing a
8425 class member access expression. */
8426 parser->context->object_type = scope;
8427 /* If something went wrong, we want to be able to discern that case,
8428 as opposed to the case where there was no SCOPE due to the type
8429 of expression being dependent. */
8430 if (!scope)
8431 scope = error_mark_node;
8432 /* If the SCOPE was erroneous, make the various semantic analysis
8433 functions exit quickly -- and without issuing additional error
8434 messages. */
8435 if (scope == error_mark_node)
8436 postfix_expression = error_mark_node;
8440 if (dependent_p)
8442 tree type = TREE_TYPE (postfix_expression);
8443 /* If we don't have a (type-dependent) object of class type, use
8444 typeof to figure out the type of the object. */
8445 if (type == NULL_TREE || is_auto (type))
8446 type = finish_typeof (postfix_expression);
8447 parser->context->object_type = type;
8450 /* Assume this expression is not a pseudo-destructor access. */
8451 pseudo_destructor_p = false;
8453 /* If the SCOPE is a scalar type, then, if this is a valid program,
8454 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
8455 is type dependent, it can be pseudo-destructor-name or something else.
8456 Try to parse it as pseudo-destructor-name first. */
8457 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
8459 tree s;
8460 tree type;
8462 cp_parser_parse_tentatively (parser);
8463 /* Parse the pseudo-destructor-name. */
8464 s = NULL_TREE;
8465 cp_parser_pseudo_destructor_name (parser, postfix_expression,
8466 &s, &type);
8467 if (dependent_p
8468 && (cp_parser_error_occurred (parser)
8469 || !SCALAR_TYPE_P (type)))
8470 cp_parser_abort_tentative_parse (parser);
8471 else if (cp_parser_parse_definitely (parser))
8473 pseudo_destructor_p = true;
8474 postfix_expression
8475 = finish_pseudo_destructor_expr (postfix_expression,
8476 s, type, location);
8480 if (!pseudo_destructor_p)
8482 /* If the SCOPE is not a scalar type, we are looking at an
8483 ordinary class member access expression, rather than a
8484 pseudo-destructor-name. */
8485 bool template_p;
8486 cp_token *token = cp_lexer_peek_token (parser->lexer);
8487 /* Parse the id-expression. */
8488 name = (cp_parser_id_expression
8489 (parser,
8490 cp_parser_optional_template_keyword (parser),
8491 /*check_dependency_p=*/true,
8492 &template_p,
8493 /*declarator_p=*/false,
8494 /*optional_p=*/false));
8495 /* In general, build a SCOPE_REF if the member name is qualified.
8496 However, if the name was not dependent and has already been
8497 resolved; there is no need to build the SCOPE_REF. For example;
8499 struct X { void f(); };
8500 template <typename T> void f(T* t) { t->X::f(); }
8502 Even though "t" is dependent, "X::f" is not and has been resolved
8503 to a BASELINK; there is no need to include scope information. */
8505 /* But we do need to remember that there was an explicit scope for
8506 virtual function calls. */
8507 if (parser->scope)
8508 *idk = CP_ID_KIND_QUALIFIED;
8510 /* If the name is a template-id that names a type, we will get a
8511 TYPE_DECL here. That is invalid code. */
8512 if (TREE_CODE (name) == TYPE_DECL)
8514 error_at (token->location, "invalid use of %qD", name);
8515 postfix_expression = error_mark_node;
8517 else
8519 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
8521 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
8523 error_at (token->location, "%<%D::%D%> is not a class member",
8524 parser->scope, name);
8525 postfix_expression = error_mark_node;
8527 else
8528 name = build_qualified_name (/*type=*/NULL_TREE,
8529 parser->scope,
8530 name,
8531 template_p);
8532 parser->scope = NULL_TREE;
8533 parser->qualifying_scope = NULL_TREE;
8534 parser->object_scope = NULL_TREE;
8536 if (parser->scope && name && BASELINK_P (name))
8537 adjust_result_of_qualified_name_lookup
8538 (name, parser->scope, scope);
8539 postfix_expression
8540 = finish_class_member_access_expr (postfix_expression, name,
8541 template_p,
8542 tf_warning_or_error);
8543 /* Build a location e.g.:
8544 ptr->access_expr
8545 ~~~^~~~~~~~~~~~~
8546 where the caret is at the deref token, ranging from
8547 the start of postfix_expression to the end of the access expr. */
8548 location_t combined_loc
8549 = make_location (input_location, start_loc, parser->lexer);
8550 protected_set_expr_location (postfix_expression, combined_loc);
8554 /* We no longer need to look up names in the scope of the object on
8555 the left-hand side of the `.' or `->' operator. */
8556 parser->context->object_type = NULL_TREE;
8558 /* Outside of offsetof, these operators may not appear in
8559 constant-expressions. */
8560 if (!for_offsetof
8561 && (cp_parser_non_integral_constant_expression
8562 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
8563 postfix_expression = error_mark_node;
8565 return postfix_expression;
8568 /* Parse a parenthesized expression-list.
8570 expression-list:
8571 assignment-expression
8572 expression-list, assignment-expression
8574 attribute-list:
8575 expression-list
8576 identifier
8577 identifier, expression-list
8579 CAST_P is true if this expression is the target of a cast.
8581 ALLOW_EXPANSION_P is true if this expression allows expansion of an
8582 argument pack.
8584 WRAP_LOCATIONS_P is true if expressions within this list for which
8585 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
8586 their source locations.
8588 Returns a vector of trees. Each element is a representation of an
8589 assignment-expression. NULL is returned if the ( and or ) are
8590 missing. An empty, but allocated, vector is returned on no
8591 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
8592 if we are parsing an attribute list for an attribute that wants a
8593 plain identifier argument, normal_attr for an attribute that wants
8594 an expression, or non_attr if we aren't parsing an attribute list. If
8595 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
8596 not all of the expressions in the list were constant.
8597 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
8598 will be written to with the location of the closing parenthesis. If
8599 an error occurs, it may or may not be written to. */
8601 static vec<tree, va_gc> *
8602 cp_parser_parenthesized_expression_list (cp_parser* parser,
8603 int is_attribute_list,
8604 bool cast_p,
8605 bool allow_expansion_p,
8606 bool *non_constant_p,
8607 location_t *close_paren_loc,
8608 bool wrap_locations_p)
8610 vec<tree, va_gc> *expression_list;
8611 bool saved_greater_than_is_operator_p;
8613 /* Assume all the expressions will be constant. */
8614 if (non_constant_p)
8615 *non_constant_p = false;
8617 matching_parens parens;
8618 if (!parens.require_open (parser))
8619 return NULL;
8621 expression_list = make_tree_vector ();
8623 /* Within a parenthesized expression, a `>' token is always
8624 the greater-than operator. */
8625 saved_greater_than_is_operator_p
8626 = parser->greater_than_is_operator_p;
8627 parser->greater_than_is_operator_p = true;
8629 cp_expr expr (NULL_TREE);
8631 /* Consume expressions until there are no more. */
8632 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8633 while (true)
8635 /* At the beginning of attribute lists, check to see if the
8636 next token is an identifier. */
8637 if (is_attribute_list == id_attr
8638 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
8639 expr = cp_lexer_consume_token (parser->lexer)->u.value;
8640 else if (is_attribute_list == assume_attr)
8641 expr = cp_parser_conditional_expression (parser);
8642 else
8643 expr
8644 = cp_parser_parenthesized_expression_list_elt (parser, cast_p,
8645 allow_expansion_p,
8646 non_constant_p);
8648 if (wrap_locations_p)
8649 expr.maybe_add_location_wrapper ();
8651 /* Add it to the list. We add error_mark_node
8652 expressions to the list, so that we can still tell if
8653 the correct form for a parenthesized expression-list
8654 is found. That gives better errors. */
8655 vec_safe_push (expression_list, expr.get_value ());
8657 if (expr == error_mark_node)
8658 goto skip_comma;
8660 /* After the first item, attribute lists look the same as
8661 expression lists. */
8662 is_attribute_list = non_attr;
8664 get_comma:;
8665 /* If the next token isn't a `,', then we are done. */
8666 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8667 break;
8669 /* Otherwise, consume the `,' and keep going. */
8670 cp_lexer_consume_token (parser->lexer);
8673 if (close_paren_loc)
8674 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
8676 if (!parens.require_close (parser))
8678 int ending;
8680 skip_comma:;
8681 /* We try and resync to an unnested comma, as that will give the
8682 user better diagnostics. */
8683 ending = cp_parser_skip_to_closing_parenthesis (parser,
8684 /*recovering=*/true,
8685 /*or_comma=*/true,
8686 /*consume_paren=*/true);
8687 if (ending < 0)
8688 goto get_comma;
8689 if (!ending)
8691 parser->greater_than_is_operator_p
8692 = saved_greater_than_is_operator_p;
8693 return NULL;
8697 parser->greater_than_is_operator_p
8698 = saved_greater_than_is_operator_p;
8700 return expression_list;
8703 /* Parse a pseudo-destructor-name.
8705 pseudo-destructor-name:
8706 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
8707 :: [opt] nested-name-specifier template template-id :: ~ type-name
8708 :: [opt] nested-name-specifier [opt] ~ type-name
8710 If either of the first two productions is used, sets *SCOPE to the
8711 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
8712 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
8713 or ERROR_MARK_NODE if the parse fails. */
8715 static void
8716 cp_parser_pseudo_destructor_name (cp_parser* parser,
8717 tree object,
8718 tree* scope,
8719 tree* type)
8721 bool nested_name_specifier_p;
8723 /* Handle ~auto. */
8724 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
8725 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
8726 && !type_dependent_expression_p (object))
8728 if (cxx_dialect < cxx14)
8729 pedwarn (input_location, OPT_Wc__14_extensions,
8730 "%<~auto%> only available with "
8731 "%<-std=c++14%> or %<-std=gnu++14%>");
8732 cp_lexer_consume_token (parser->lexer);
8733 cp_lexer_consume_token (parser->lexer);
8734 *scope = NULL_TREE;
8735 *type = TREE_TYPE (object);
8736 return;
8739 /* Assume that things will not work out. */
8740 *type = error_mark_node;
8742 /* Look for the optional `::' operator. */
8743 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
8744 /* Look for the optional nested-name-specifier. */
8745 nested_name_specifier_p
8746 = (cp_parser_nested_name_specifier_opt (parser,
8747 /*typename_keyword_p=*/false,
8748 /*check_dependency_p=*/true,
8749 /*type_p=*/false,
8750 /*is_declaration=*/false)
8751 != NULL_TREE);
8752 /* Now, if we saw a nested-name-specifier, we might be doing the
8753 second production. */
8754 if (nested_name_specifier_p
8755 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8757 /* Consume the `template' keyword. */
8758 cp_lexer_consume_token (parser->lexer);
8759 /* Parse the template-id. */
8760 cp_parser_template_id (parser,
8761 /*template_keyword_p=*/true,
8762 /*check_dependency_p=*/false,
8763 class_type,
8764 /*is_declaration=*/true);
8765 /* Look for the `::' token. */
8766 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
8768 /* If the next token is not a `~', then there might be some
8769 additional qualification. */
8770 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
8772 /* At this point, we're looking for "type-name :: ~". The type-name
8773 must not be a class-name, since this is a pseudo-destructor. So,
8774 it must be either an enum-name, or a typedef-name -- both of which
8775 are just identifiers. So, we peek ahead to check that the "::"
8776 and "~" tokens are present; if they are not, then we can avoid
8777 calling type_name. */
8778 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
8779 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
8780 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
8782 cp_parser_error (parser, "non-scalar type");
8783 return;
8786 /* Look for the type-name. */
8787 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
8788 if (*scope == error_mark_node)
8789 return;
8791 /* Look for the `::' token. */
8792 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
8794 else
8795 *scope = NULL_TREE;
8797 /* Look for the `~'. */
8798 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
8800 /* Once we see the ~, this has to be a pseudo-destructor. */
8801 if (!processing_template_decl && !cp_parser_error_occurred (parser))
8802 cp_parser_commit_to_topmost_tentative_parse (parser);
8804 /* Look for the type-name again. We are not responsible for
8805 checking that it matches the first type-name. */
8806 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
8809 /* Parse a unary-expression.
8811 unary-expression:
8812 postfix-expression
8813 ++ cast-expression
8814 -- cast-expression
8815 await-expression
8816 unary-operator cast-expression
8817 sizeof unary-expression
8818 sizeof ( type-id )
8819 alignof ( type-id ) [C++0x]
8820 new-expression
8821 delete-expression
8823 GNU Extensions:
8825 unary-expression:
8826 __extension__ cast-expression
8827 __alignof__ unary-expression
8828 __alignof__ ( type-id )
8829 alignof unary-expression [C++0x]
8830 __real__ cast-expression
8831 __imag__ cast-expression
8832 && identifier
8833 sizeof ( type-id ) { initializer-list , [opt] }
8834 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
8835 __alignof__ ( type-id ) { initializer-list , [opt] }
8837 ADDRESS_P is true iff the unary-expression is appearing as the
8838 operand of the `&' operator. CAST_P is true if this expression is
8839 the target of a cast.
8841 Returns a representation of the expression. */
8843 static cp_expr
8844 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
8845 bool address_p, bool cast_p, bool decltype_p)
8847 cp_token *token;
8848 enum tree_code unary_operator;
8850 /* Peek at the next token. */
8851 token = cp_lexer_peek_token (parser->lexer);
8852 /* Some keywords give away the kind of expression. */
8853 if (token->type == CPP_KEYWORD)
8855 enum rid keyword = token->keyword;
8857 switch (keyword)
8859 case RID_ALIGNOF:
8860 case RID_SIZEOF:
8862 tree operand, ret;
8863 enum tree_code op;
8864 location_t start_loc = token->location;
8866 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
8867 bool std_alignof = id_equal (token->u.value, "alignof");
8869 /* Consume the token. */
8870 cp_lexer_consume_token (parser->lexer);
8871 /* Parse the operand. */
8872 operand = cp_parser_sizeof_operand (parser, keyword);
8874 /* Construct a location e.g. :
8875 alignof (expr)
8876 ^~~~~~~~~~~~~~
8877 with start == caret at the start of the "alignof"/"sizeof"
8878 token, with the endpoint at the final closing paren. */
8879 location_t compound_loc
8880 = make_location (start_loc, start_loc, parser->lexer);
8882 if (TYPE_P (operand))
8883 ret = cxx_sizeof_or_alignof_type (compound_loc, operand, op,
8884 std_alignof, true);
8885 else
8887 /* ISO C++ defines alignof only with types, not with
8888 expressions. So pedwarn if alignof is used with a non-
8889 type expression. However, __alignof__ is ok. */
8890 if (std_alignof)
8891 pedwarn (token->location, OPT_Wpedantic,
8892 "ISO C++ does not allow %<alignof%> "
8893 "with a non-type");
8895 ret = cxx_sizeof_or_alignof_expr (compound_loc, operand, op,
8896 std_alignof, true);
8898 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8899 SIZEOF_EXPR with the original operand. */
8900 if (op == SIZEOF_EXPR && ret != error_mark_node)
8902 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
8904 if (!processing_template_decl && TYPE_P (operand))
8906 ret = build_min (SIZEOF_EXPR, size_type_node,
8907 build1 (NOP_EXPR, operand,
8908 error_mark_node));
8909 SIZEOF_EXPR_TYPE_P (ret) = 1;
8911 else
8912 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
8913 TREE_SIDE_EFFECTS (ret) = 0;
8914 TREE_READONLY (ret) = 1;
8915 SET_EXPR_LOCATION (ret, compound_loc);
8919 cp_expr ret_expr (ret, compound_loc);
8920 ret_expr = ret_expr.maybe_add_location_wrapper ();
8921 return ret_expr;
8924 case RID_BUILTIN_HAS_ATTRIBUTE:
8925 return cp_parser_has_attribute_expression (parser);
8927 case RID_NEW:
8928 return cp_parser_new_expression (parser);
8930 case RID_DELETE:
8931 return cp_parser_delete_expression (parser);
8933 case RID_EXTENSION:
8935 /* The saved value of the PEDANTIC flag. */
8936 int saved_pedantic;
8937 tree expr;
8939 /* Save away the PEDANTIC flag. */
8940 cp_parser_extension_opt (parser, &saved_pedantic);
8941 /* Parse the cast-expression. */
8942 expr = cp_parser_simple_cast_expression (parser);
8943 /* Restore the PEDANTIC flag. */
8944 pedantic = saved_pedantic;
8946 return expr;
8949 case RID_REALPART:
8950 case RID_IMAGPART:
8952 tree expression;
8954 /* Consume the `__real__' or `__imag__' token. */
8955 cp_lexer_consume_token (parser->lexer);
8956 /* Parse the cast-expression. */
8957 expression = cp_parser_simple_cast_expression (parser);
8958 /* Create the complete representation. */
8959 return build_x_unary_op (token->location,
8960 (keyword == RID_REALPART
8961 ? REALPART_EXPR : IMAGPART_EXPR),
8962 expression, NULL_TREE,
8963 tf_warning_or_error);
8965 break;
8967 case RID_TRANSACTION_ATOMIC:
8968 case RID_TRANSACTION_RELAXED:
8969 return cp_parser_transaction_expression (parser, keyword);
8971 case RID_NOEXCEPT:
8973 tree expr;
8974 const char *saved_message;
8975 bool saved_integral_constant_expression_p;
8976 bool saved_non_integral_constant_expression_p;
8977 bool saved_greater_than_is_operator_p;
8979 location_t start_loc = token->location;
8981 cp_lexer_consume_token (parser->lexer);
8982 matching_parens parens;
8983 parens.require_open (parser);
8985 saved_message = parser->type_definition_forbidden_message;
8986 parser->type_definition_forbidden_message
8987 = G_("types may not be defined in %<noexcept%> expressions");
8989 saved_integral_constant_expression_p
8990 = parser->integral_constant_expression_p;
8991 saved_non_integral_constant_expression_p
8992 = parser->non_integral_constant_expression_p;
8993 parser->integral_constant_expression_p = false;
8995 saved_greater_than_is_operator_p
8996 = parser->greater_than_is_operator_p;
8997 parser->greater_than_is_operator_p = true;
8999 ++cp_unevaluated_operand;
9000 ++c_inhibit_evaluation_warnings;
9001 ++cp_noexcept_operand;
9002 expr = cp_parser_expression (parser);
9003 --cp_noexcept_operand;
9004 --c_inhibit_evaluation_warnings;
9005 --cp_unevaluated_operand;
9007 parser->greater_than_is_operator_p
9008 = saved_greater_than_is_operator_p;
9010 parser->integral_constant_expression_p
9011 = saved_integral_constant_expression_p;
9012 parser->non_integral_constant_expression_p
9013 = saved_non_integral_constant_expression_p;
9015 parser->type_definition_forbidden_message = saved_message;
9017 parens.require_close (parser);
9019 /* Construct a location of the form:
9020 noexcept (expr)
9021 ^~~~~~~~~~~~~~~
9022 with start == caret, finishing at the close-paren. */
9023 location_t noexcept_loc
9024 = make_location (start_loc, start_loc, parser->lexer);
9026 return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
9027 noexcept_loc);
9030 case RID_CO_AWAIT:
9032 tree expr;
9033 location_t kw_loc = token->location;
9035 /* Consume the `co_await' token. */
9036 cp_lexer_consume_token (parser->lexer);
9037 /* Parse its cast-expression. */
9038 expr = cp_parser_simple_cast_expression (parser);
9039 if (expr == error_mark_node)
9040 return error_mark_node;
9042 /* Handle [expr.await]. */
9043 return cp_expr (finish_co_await_expr (kw_loc, expr));
9046 default:
9047 break;
9051 /* Look for the `:: new' and `:: delete', which also signal the
9052 beginning of a new-expression, or delete-expression,
9053 respectively. If the next token is `::', then it might be one of
9054 these. */
9055 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9057 enum rid keyword;
9059 /* See if the token after the `::' is one of the keywords in
9060 which we're interested. */
9061 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
9062 /* If it's `new', we have a new-expression. */
9063 if (keyword == RID_NEW)
9064 return cp_parser_new_expression (parser);
9065 /* Similarly, for `delete'. */
9066 else if (keyword == RID_DELETE)
9067 return cp_parser_delete_expression (parser);
9070 /* Look for a unary operator. */
9071 unary_operator = cp_parser_unary_operator (token);
9072 /* The `++' and `--' operators can be handled similarly, even though
9073 they are not technically unary-operators in the grammar. */
9074 if (unary_operator == ERROR_MARK)
9076 if (token->type == CPP_PLUS_PLUS)
9077 unary_operator = PREINCREMENT_EXPR;
9078 else if (token->type == CPP_MINUS_MINUS)
9079 unary_operator = PREDECREMENT_EXPR;
9080 /* Handle the GNU address-of-label extension. */
9081 else if (cp_parser_allow_gnu_extensions_p (parser)
9082 && token->type == CPP_AND_AND)
9084 tree identifier;
9085 tree expression;
9086 location_t start_loc = token->location;
9088 /* Consume the '&&' token. */
9089 cp_lexer_consume_token (parser->lexer);
9090 /* Look for the identifier. */
9091 identifier = cp_parser_identifier (parser);
9092 /* Construct a location of the form:
9093 &&label
9094 ^~~~~~~
9095 with caret==start at the "&&", finish at the end of the label. */
9096 location_t combined_loc
9097 = make_location (start_loc, start_loc, parser->lexer);
9098 /* Create an expression representing the address. */
9099 expression = finish_label_address_expr (identifier, combined_loc);
9100 if (cp_parser_non_integral_constant_expression (parser,
9101 NIC_ADDR_LABEL))
9102 expression = error_mark_node;
9103 return expression;
9106 if (unary_operator != ERROR_MARK)
9108 cp_expr cast_expression;
9109 cp_expr expression = error_mark_node;
9110 non_integral_constant non_constant_p = NIC_NONE;
9111 location_t loc = token->location;
9112 tsubst_flags_t complain = complain_flags (decltype_p);
9114 /* Consume the operator token. */
9115 token = cp_lexer_consume_token (parser->lexer);
9116 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
9118 /* Parse the cast-expression. */
9119 cast_expression
9120 = cp_parser_cast_expression (parser,
9121 unary_operator == ADDR_EXPR,
9122 /*cast_p=*/false,
9123 /*decltype*/false,
9124 pidk);
9126 /* Make a location:
9127 OP_TOKEN CAST_EXPRESSION
9128 ^~~~~~~~~~~~~~~~~~~~~~~~~
9129 with start==caret at the operator token, and
9130 extending to the end of the cast_expression. */
9131 loc = make_location (loc, loc, cast_expression.get_finish ());
9133 /* Now, build an appropriate representation. */
9134 switch (unary_operator)
9136 case INDIRECT_REF:
9137 non_constant_p = NIC_STAR;
9138 expression = build_x_indirect_ref (loc, cast_expression,
9139 RO_UNARY_STAR, NULL_TREE,
9140 complain);
9141 /* TODO: build_x_indirect_ref does not always honor the
9142 location, so ensure it is set. */
9143 expression.set_location (loc);
9144 break;
9146 case ADDR_EXPR:
9147 non_constant_p = NIC_ADDR;
9148 /* Fall through. */
9149 case BIT_NOT_EXPR:
9150 expression = build_x_unary_op (loc, unary_operator,
9151 cast_expression,
9152 NULL_TREE, complain);
9153 /* TODO: build_x_unary_op does not always honor the location,
9154 so ensure it is set. */
9155 expression.set_location (loc);
9156 break;
9158 case PREINCREMENT_EXPR:
9159 case PREDECREMENT_EXPR:
9160 non_constant_p = unary_operator == PREINCREMENT_EXPR
9161 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
9162 /* Fall through. */
9163 case NEGATE_EXPR:
9164 /* Immediately fold negation of a constant, unless the constant is 0
9165 (since -0 == 0) or it would overflow. */
9166 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER)
9168 tree stripped_expr
9169 = tree_strip_any_location_wrapper (cast_expression);
9170 if (CONSTANT_CLASS_P (stripped_expr)
9171 && !integer_zerop (stripped_expr)
9172 && !TREE_OVERFLOW (stripped_expr))
9174 tree folded = fold_build1 (unary_operator,
9175 TREE_TYPE (stripped_expr),
9176 stripped_expr);
9177 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
9179 expression = maybe_wrap_with_location (folded, loc);
9180 break;
9184 /* Fall through. */
9185 case UNARY_PLUS_EXPR:
9186 case TRUTH_NOT_EXPR:
9187 expression = finish_unary_op_expr (loc, unary_operator,
9188 cast_expression, complain);
9189 break;
9191 default:
9192 gcc_unreachable ();
9195 if (non_constant_p != NIC_NONE
9196 && cp_parser_non_integral_constant_expression (parser,
9197 non_constant_p))
9198 expression = error_mark_node;
9200 return expression;
9203 return cp_parser_postfix_expression (parser, address_p, cast_p,
9204 /*member_access_only_p=*/false,
9205 decltype_p,
9206 pidk);
9209 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
9210 unary-operator, the corresponding tree code is returned. */
9212 static enum tree_code
9213 cp_parser_unary_operator (cp_token* token)
9215 switch (token->type)
9217 case CPP_MULT:
9218 return INDIRECT_REF;
9220 case CPP_AND:
9221 return ADDR_EXPR;
9223 case CPP_PLUS:
9224 return UNARY_PLUS_EXPR;
9226 case CPP_MINUS:
9227 return NEGATE_EXPR;
9229 case CPP_NOT:
9230 return TRUTH_NOT_EXPR;
9232 case CPP_COMPL:
9233 return BIT_NOT_EXPR;
9235 default:
9236 return ERROR_MARK;
9240 /* Parse a __builtin_has_attribute([expr|type], attribute-spec) expression.
9241 Returns a representation of the expression. */
9243 static tree
9244 cp_parser_has_attribute_expression (cp_parser *parser)
9246 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9248 /* Consume the __builtin_has_attribute token. */
9249 cp_lexer_consume_token (parser->lexer);
9251 matching_parens parens;
9252 if (!parens.require_open (parser))
9253 return error_mark_node;
9255 /* Types cannot be defined in a `sizeof' expression. Save away the
9256 old message. */
9257 const char *saved_message = parser->type_definition_forbidden_message;
9258 const char *saved_message_arg
9259 = parser->type_definition_forbidden_message_arg;
9260 parser->type_definition_forbidden_message
9261 = G_("types may not be defined in %qs expressions");
9262 parser->type_definition_forbidden_message_arg
9263 = IDENTIFIER_POINTER (ridpointers[RID_BUILTIN_HAS_ATTRIBUTE]);
9265 /* The restrictions on constant-expressions do not apply inside
9266 sizeof expressions. */
9267 bool saved_integral_constant_expression_p
9268 = parser->integral_constant_expression_p;
9269 bool saved_non_integral_constant_expression_p
9270 = parser->non_integral_constant_expression_p;
9271 parser->integral_constant_expression_p = false;
9273 /* Do not actually evaluate the expression. */
9274 ++cp_unevaluated_operand;
9275 ++c_inhibit_evaluation_warnings;
9277 tree oper = NULL_TREE;
9279 /* We can't be sure yet whether we're looking at a type-id or an
9280 expression. */
9281 cp_parser_parse_tentatively (parser);
9283 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9284 parser->in_type_id_in_expr_p = true;
9285 /* Look for the type-id. */
9286 oper = cp_parser_type_id (parser);
9287 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9289 cp_parser_parse_definitely (parser);
9291 /* If the type-id production did not work out, then we must be
9292 looking at an expression. */
9293 if (!oper || oper == error_mark_node)
9294 oper = cp_parser_assignment_expression (parser);
9296 STRIP_ANY_LOCATION_WRAPPER (oper);
9298 /* Go back to evaluating expressions. */
9299 --cp_unevaluated_operand;
9300 --c_inhibit_evaluation_warnings;
9302 /* And restore the old one. */
9303 parser->type_definition_forbidden_message = saved_message;
9304 parser->type_definition_forbidden_message_arg = saved_message_arg;
9305 parser->integral_constant_expression_p
9306 = saved_integral_constant_expression_p;
9307 parser->non_integral_constant_expression_p
9308 = saved_non_integral_constant_expression_p;
9310 /* Consume the comma if it's there. */
9311 if (!cp_parser_require (parser, CPP_COMMA, RT_COMMA))
9313 cp_parser_skip_to_closing_parenthesis (parser, false, false,
9314 /*consume_paren=*/true);
9315 return error_mark_node;
9318 /* Parse the attribute specification. */
9319 bool ret = false;
9320 location_t atloc = cp_lexer_peek_token (parser->lexer)->location;
9321 if (tree attr = cp_parser_gnu_attribute_list (parser, /*exactly_one=*/true))
9323 if (oper == error_mark_node)
9324 /* Nothing. */;
9325 else if (processing_template_decl && uses_template_parms (oper))
9326 sorry_at (atloc, "%<__builtin_has_attribute%> with dependent argument "
9327 "not supported yet");
9328 else
9330 /* Fold constant expressions used in attributes first. */
9331 cp_check_const_attributes (attr);
9333 /* Finally, see if OPER has been declared with ATTR. */
9334 ret = has_attribute (atloc, oper, attr, default_conversion);
9337 parens.require_close (parser);
9339 else
9341 error_at (atloc, "expected identifier");
9342 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9345 /* Construct a location e.g. :
9346 __builtin_has_attribute (oper, attr)
9347 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9348 with start == caret at the start of the built-in token,
9349 and with the endpoint at the final closing paren. */
9350 location_t compound_loc
9351 = make_location (start_loc, start_loc, parser->lexer);
9353 cp_expr ret_expr (ret ? boolean_true_node : boolean_false_node);
9354 ret_expr.set_location (compound_loc);
9355 ret_expr = ret_expr.maybe_add_location_wrapper ();
9356 return ret_expr;
9359 /* Parse a new-expression.
9361 new-expression:
9362 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
9363 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
9365 Returns a representation of the expression. */
9367 static tree
9368 cp_parser_new_expression (cp_parser* parser)
9370 bool global_scope_p;
9371 vec<tree, va_gc> *placement;
9372 tree type;
9373 vec<tree, va_gc> *initializer;
9374 tree nelts = NULL_TREE;
9375 tree ret;
9377 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9379 /* Look for the optional `::' operator. */
9380 global_scope_p
9381 = (cp_parser_global_scope_opt (parser,
9382 /*current_scope_valid_p=*/false)
9383 != NULL_TREE);
9384 /* Look for the `new' operator. */
9385 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
9386 /* There's no easy way to tell a new-placement from the
9387 `( type-id )' construct. */
9388 cp_parser_parse_tentatively (parser);
9389 /* Look for a new-placement. */
9390 placement = cp_parser_new_placement (parser);
9391 /* If that didn't work out, there's no new-placement. */
9392 if (!cp_parser_parse_definitely (parser))
9394 if (placement != NULL)
9395 release_tree_vector (placement);
9396 placement = NULL;
9399 /* If the next token is a `(', then we have a parenthesized
9400 type-id. */
9401 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9403 cp_token *token;
9404 const char *saved_message = parser->type_definition_forbidden_message;
9406 /* Consume the `('. */
9407 matching_parens parens;
9408 parens.consume_open (parser);
9410 /* Parse the type-id. */
9411 parser->type_definition_forbidden_message
9412 = G_("types may not be defined in a new-expression");
9414 type_id_in_expr_sentinel s (parser);
9415 type = cp_parser_type_id (parser);
9417 parser->type_definition_forbidden_message = saved_message;
9419 /* Look for the closing `)'. */
9420 parens.require_close (parser);
9421 token = cp_lexer_peek_token (parser->lexer);
9422 /* There should not be a direct-new-declarator in this production,
9423 but GCC used to allowed this, so we check and emit a sensible error
9424 message for this case. */
9425 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
9427 error_at (token->location,
9428 "array bound forbidden after parenthesized type-id");
9429 inform (token->location,
9430 "try removing the parentheses around the type-id");
9431 cp_parser_direct_new_declarator (parser);
9434 /* Otherwise, there must be a new-type-id. */
9435 else
9436 type = cp_parser_new_type_id (parser, &nelts);
9438 /* If the next token is a `(' or '{', then we have a new-initializer. */
9439 cp_token *token = cp_lexer_peek_token (parser->lexer);
9440 if (token->type == CPP_OPEN_PAREN
9441 || token->type == CPP_OPEN_BRACE)
9442 initializer = cp_parser_new_initializer (parser);
9443 else
9444 initializer = NULL;
9446 /* A new-expression may not appear in an integral constant
9447 expression. */
9448 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
9449 ret = error_mark_node;
9450 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
9451 of a new-type-id or type-id of a new-expression, the new-expression shall
9452 contain a new-initializer of the form ( assignment-expression )".
9453 Additionally, consistently with the spirit of DR 1467, we want to accept
9454 'new auto { 2 }' too. */
9455 else if ((ret = type_uses_auto (type))
9456 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
9457 && (vec_safe_length (initializer) != 1
9458 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
9459 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
9461 error_at (token->location,
9462 "initialization of new-expression for type %<auto%> "
9463 "requires exactly one element");
9464 ret = error_mark_node;
9466 else
9468 /* Construct a location e.g.:
9469 ptr = new int[100]
9470 ^~~~~~~~~~~~
9471 with caret == start at the start of the "new" token, and the end
9472 at the end of the final token we consumed. */
9473 location_t combined_loc = make_location (start_loc, start_loc,
9474 parser->lexer);
9475 /* Create a representation of the new-expression. */
9476 ret = build_new (combined_loc, &placement, type, nelts, &initializer,
9477 global_scope_p, tf_warning_or_error);
9480 if (placement != NULL)
9481 release_tree_vector (placement);
9482 if (initializer != NULL)
9483 release_tree_vector (initializer);
9485 return ret;
9488 /* Parse a new-placement.
9490 new-placement:
9491 ( expression-list )
9493 Returns the same representation as for an expression-list. */
9495 static vec<tree, va_gc> *
9496 cp_parser_new_placement (cp_parser* parser)
9498 vec<tree, va_gc> *expression_list;
9500 /* Parse the expression-list. */
9501 expression_list = (cp_parser_parenthesized_expression_list
9502 (parser, non_attr, /*cast_p=*/false,
9503 /*allow_expansion_p=*/true,
9504 /*non_constant_p=*/NULL));
9506 if (expression_list && expression_list->is_empty ())
9507 error ("expected expression-list or type-id");
9509 return expression_list;
9512 /* Parse a new-type-id.
9514 new-type-id:
9515 type-specifier-seq new-declarator [opt]
9517 Returns the TYPE allocated. If the new-type-id indicates an array
9518 type, *NELTS is set to the number of elements in the last array
9519 bound; the TYPE will not include the last array bound. */
9521 static tree
9522 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
9524 cp_decl_specifier_seq type_specifier_seq;
9525 cp_declarator *new_declarator;
9526 cp_declarator *declarator;
9527 cp_declarator *outer_declarator;
9528 const char *saved_message;
9530 /* The type-specifier sequence must not contain type definitions.
9531 (It cannot contain declarations of new types either, but if they
9532 are not definitions we will catch that because they are not
9533 complete.) */
9534 saved_message = parser->type_definition_forbidden_message;
9535 parser->type_definition_forbidden_message
9536 = G_("types may not be defined in a new-type-id");
9537 /* Parse the type-specifier-seq. */
9538 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
9539 /*is_declaration=*/false,
9540 /*is_trailing_return=*/false,
9541 &type_specifier_seq);
9542 /* Restore the old message. */
9543 parser->type_definition_forbidden_message = saved_message;
9545 if (type_specifier_seq.type == error_mark_node)
9546 return error_mark_node;
9548 /* Parse the new-declarator. */
9549 new_declarator = cp_parser_new_declarator_opt (parser);
9551 /* Determine the number of elements in the last array dimension, if
9552 any. */
9553 *nelts = NULL_TREE;
9554 /* Skip down to the last array dimension. */
9555 declarator = new_declarator;
9556 outer_declarator = NULL;
9557 while (declarator && (declarator->kind == cdk_pointer
9558 || declarator->kind == cdk_ptrmem))
9560 outer_declarator = declarator;
9561 declarator = declarator->declarator;
9563 while (declarator
9564 && declarator->kind == cdk_array
9565 && declarator->declarator
9566 && declarator->declarator->kind == cdk_array)
9568 outer_declarator = declarator;
9569 declarator = declarator->declarator;
9572 if (declarator && declarator->kind == cdk_array)
9574 *nelts = declarator->u.array.bounds;
9575 if (*nelts == error_mark_node)
9576 *nelts = integer_one_node;
9578 if (*nelts == NULL_TREE)
9579 /* Leave [] in the declarator. */;
9580 else if (outer_declarator)
9581 outer_declarator->declarator = declarator->declarator;
9582 else
9583 new_declarator = NULL;
9586 return groktypename (&type_specifier_seq, new_declarator, false);
9589 /* Parse an (optional) new-declarator.
9591 new-declarator:
9592 ptr-operator new-declarator [opt]
9593 direct-new-declarator
9595 Returns the declarator. */
9597 static cp_declarator *
9598 cp_parser_new_declarator_opt (cp_parser* parser)
9600 enum tree_code code;
9601 tree type, std_attributes = NULL_TREE;
9602 cp_cv_quals cv_quals;
9604 /* We don't know if there's a ptr-operator next, or not. */
9605 cp_parser_parse_tentatively (parser);
9606 /* Look for a ptr-operator. */
9607 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
9608 /* If that worked, look for more new-declarators. */
9609 if (cp_parser_parse_definitely (parser))
9611 cp_declarator *declarator;
9613 /* Parse another optional declarator. */
9614 declarator = cp_parser_new_declarator_opt (parser);
9616 declarator = cp_parser_make_indirect_declarator
9617 (code, type, cv_quals, declarator, std_attributes);
9619 return declarator;
9622 /* If the next token is a `[', there is a direct-new-declarator. */
9623 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
9624 return cp_parser_direct_new_declarator (parser);
9626 return NULL;
9629 /* Parse a direct-new-declarator.
9631 direct-new-declarator:
9632 [ expression ]
9633 direct-new-declarator [constant-expression]
9637 static cp_declarator *
9638 cp_parser_direct_new_declarator (cp_parser* parser)
9640 cp_declarator *declarator = NULL;
9641 bool first_p = true;
9643 while (true)
9645 tree expression;
9646 cp_token *token;
9648 /* Look for the opening `['. */
9649 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9651 token = cp_lexer_peek_token (parser->lexer);
9652 if (token->type == CPP_CLOSE_SQUARE && first_p)
9653 expression = NULL_TREE;
9654 else
9655 expression = cp_parser_expression (parser);
9656 /* The standard requires that the expression have integral
9657 type. DR 74 adds enumeration types. We believe that the
9658 real intent is that these expressions be handled like the
9659 expression in a `switch' condition, which also allows
9660 classes with a single conversion to integral or
9661 enumeration type. */
9662 if (expression && !processing_template_decl)
9664 expression
9665 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
9666 expression,
9667 /*complain=*/true);
9668 if (!expression)
9670 error_at (token->location,
9671 "expression in new-declarator must have integral "
9672 "or enumeration type");
9673 expression = error_mark_node;
9677 /* Look for the closing `]'. */
9678 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9680 /* Add this bound to the declarator. */
9681 declarator = make_array_declarator (declarator, expression);
9683 /* If the next token is not a `[', then there are no more
9684 bounds. */
9685 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
9686 break;
9687 first_p = false;
9690 return declarator;
9693 /* Parse a new-initializer.
9695 new-initializer:
9696 ( expression-list [opt] )
9697 braced-init-list
9699 Returns a representation of the expression-list. */
9701 static vec<tree, va_gc> *
9702 cp_parser_new_initializer (cp_parser* parser)
9704 vec<tree, va_gc> *expression_list;
9706 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9708 cp_lexer_set_source_position (parser->lexer);
9709 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9710 tree t = cp_parser_braced_list (parser);
9711 CONSTRUCTOR_IS_DIRECT_INIT (t) = true;
9712 expression_list = make_tree_vector_single (t);
9714 else
9715 expression_list = (cp_parser_parenthesized_expression_list
9716 (parser, non_attr, /*cast_p=*/false,
9717 /*allow_expansion_p=*/true,
9718 /*non_constant_p=*/NULL));
9720 return expression_list;
9723 /* Parse a delete-expression.
9725 delete-expression:
9726 :: [opt] delete cast-expression
9727 :: [opt] delete [ ] cast-expression
9729 Returns a representation of the expression. */
9731 static tree
9732 cp_parser_delete_expression (cp_parser* parser)
9734 bool global_scope_p;
9735 bool array_p;
9736 tree expression;
9737 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9739 /* Look for the optional `::' operator. */
9740 global_scope_p
9741 = (cp_parser_global_scope_opt (parser,
9742 /*current_scope_valid_p=*/false)
9743 != NULL_TREE);
9744 /* Look for the `delete' keyword. */
9745 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
9746 /* See if the array syntax is in use. */
9747 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
9749 /* Consume the `[' token. */
9750 cp_lexer_consume_token (parser->lexer);
9751 /* Look for the `]' token. */
9752 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9753 /* Remember that this is the `[]' construct. */
9754 array_p = true;
9756 else
9757 array_p = false;
9759 /* Parse the cast-expression. */
9760 expression = cp_parser_simple_cast_expression (parser);
9762 /* A delete-expression may not appear in an integral constant
9763 expression. */
9764 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
9765 return error_mark_node;
9767 /* Construct a location e.g.:
9768 delete [ ] ptr
9769 ^~~~~~~~~~~~~~
9770 with caret == start at the start of the "delete" token, and
9771 the end at the end of the final token we consumed. */
9772 location_t combined_loc = make_location (start_loc, start_loc,
9773 parser->lexer);
9774 expression = delete_sanity (combined_loc, expression, NULL_TREE, array_p,
9775 global_scope_p, tf_warning_or_error);
9777 return expression;
9780 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
9781 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
9782 0 otherwise. */
9784 static int
9785 cp_parser_tokens_start_cast_expression (cp_parser *parser)
9787 cp_token *token = cp_lexer_peek_token (parser->lexer);
9788 switch (token->type)
9790 case CPP_COMMA:
9791 case CPP_SEMICOLON:
9792 case CPP_QUERY:
9793 case CPP_COLON:
9794 case CPP_CLOSE_SQUARE:
9795 case CPP_CLOSE_PAREN:
9796 case CPP_CLOSE_BRACE:
9797 case CPP_OPEN_BRACE:
9798 case CPP_DOT:
9799 case CPP_DOT_STAR:
9800 case CPP_DEREF:
9801 case CPP_DEREF_STAR:
9802 case CPP_DIV:
9803 case CPP_MOD:
9804 case CPP_LSHIFT:
9805 case CPP_RSHIFT:
9806 case CPP_LESS:
9807 case CPP_GREATER:
9808 case CPP_LESS_EQ:
9809 case CPP_GREATER_EQ:
9810 case CPP_EQ_EQ:
9811 case CPP_NOT_EQ:
9812 case CPP_EQ:
9813 case CPP_MULT_EQ:
9814 case CPP_DIV_EQ:
9815 case CPP_MOD_EQ:
9816 case CPP_PLUS_EQ:
9817 case CPP_MINUS_EQ:
9818 case CPP_RSHIFT_EQ:
9819 case CPP_LSHIFT_EQ:
9820 case CPP_AND_EQ:
9821 case CPP_XOR_EQ:
9822 case CPP_OR_EQ:
9823 case CPP_XOR:
9824 case CPP_OR:
9825 case CPP_OR_OR:
9826 case CPP_EOF:
9827 case CPP_ELLIPSIS:
9828 return 0;
9830 case CPP_OPEN_PAREN:
9831 /* In ((type ()) () the last () isn't a valid cast-expression,
9832 so the whole must be parsed as postfix-expression. */
9833 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
9834 != CPP_CLOSE_PAREN;
9836 case CPP_OPEN_SQUARE:
9837 /* '[' may start a primary-expression in obj-c++ and in C++11,
9838 as a lambda-expression, eg, '(void)[]{}'. */
9839 if (cxx_dialect >= cxx11)
9840 return -1;
9841 return c_dialect_objc ();
9843 case CPP_PLUS_PLUS:
9844 case CPP_MINUS_MINUS:
9845 /* '++' and '--' may or may not start a cast-expression:
9847 struct T { void operator++(int); };
9848 void f() { (T())++; }
9852 int a;
9853 (int)++a; */
9854 return -1;
9856 default:
9857 return 1;
9861 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
9862 in the order: const_cast, static_cast, reinterpret_cast.
9864 Don't suggest dynamic_cast.
9866 Return the first legal cast kind found, or NULL otherwise. */
9868 static const char *
9869 get_cast_suggestion (tree dst_type, tree orig_expr)
9871 tree trial;
9873 /* Reuse the parser logic by attempting to build the various kinds of
9874 cast, with "complain" disabled.
9875 Identify the first such cast that is valid. */
9877 /* Don't attempt to run such logic within template processing. */
9878 if (processing_template_decl)
9879 return NULL;
9881 /* First try const_cast. */
9882 trial = build_const_cast (input_location, dst_type, orig_expr, tf_none);
9883 if (trial != error_mark_node)
9884 return "const_cast";
9886 /* If that fails, try static_cast. */
9887 trial = build_static_cast (input_location, dst_type, orig_expr, tf_none);
9888 if (trial != error_mark_node)
9889 return "static_cast";
9891 /* Finally, try reinterpret_cast. */
9892 trial = build_reinterpret_cast (input_location, dst_type, orig_expr,
9893 tf_none);
9894 if (trial != error_mark_node)
9895 return "reinterpret_cast";
9897 /* No such cast possible. */
9898 return NULL;
9901 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
9902 suggesting how to convert a C-style cast of the form:
9904 (DST_TYPE)ORIG_EXPR
9906 to a C++-style cast.
9908 The primary range of RICHLOC is asssumed to be that of the original
9909 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
9910 of the parens in the C-style cast. */
9912 static void
9913 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
9914 location_t close_paren_loc, tree orig_expr,
9915 tree dst_type)
9917 /* This function is non-trivial, so bail out now if the warning isn't
9918 going to be emitted. */
9919 if (!warn_old_style_cast)
9920 return;
9922 /* Try to find a legal C++ cast, trying them in order:
9923 const_cast, static_cast, reinterpret_cast. */
9924 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
9925 if (!cast_suggestion)
9926 return;
9928 /* Replace the open paren with "CAST_SUGGESTION<". */
9929 pretty_printer pp;
9930 pp_string (&pp, cast_suggestion);
9931 pp_less (&pp);
9932 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
9934 /* Replace the close paren with "> (". */
9935 rich_loc->add_fixit_replace (close_paren_loc, "> (");
9937 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
9938 rich_loc->add_fixit_insert_after (")");
9942 /* Parse a cast-expression.
9944 cast-expression:
9945 unary-expression
9946 ( type-id ) cast-expression
9948 ADDRESS_P is true iff the unary-expression is appearing as the
9949 operand of the `&' operator. CAST_P is true if this expression is
9950 the target of a cast.
9952 Returns a representation of the expression. */
9954 static cp_expr
9955 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
9956 bool decltype_p, cp_id_kind * pidk)
9958 /* If it's a `(', then we might be looking at a cast. */
9959 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9961 tree type = NULL_TREE;
9962 cp_expr expr (NULL_TREE);
9963 int cast_expression = 0;
9964 const char *saved_message;
9966 /* There's no way to know yet whether or not this is a cast.
9967 For example, `(int (3))' is a unary-expression, while `(int)
9968 3' is a cast. So, we resort to parsing tentatively. */
9969 cp_parser_parse_tentatively (parser);
9970 /* Types may not be defined in a cast. */
9971 saved_message = parser->type_definition_forbidden_message;
9972 parser->type_definition_forbidden_message
9973 = G_("types may not be defined in casts");
9974 /* Consume the `('. */
9975 matching_parens parens;
9976 cp_token *open_paren = parens.consume_open (parser);
9977 location_t open_paren_loc = open_paren->location;
9978 location_t close_paren_loc = UNKNOWN_LOCATION;
9980 /* A very tricky bit is that `(struct S) { 3 }' is a
9981 compound-literal (which we permit in C++ as an extension).
9982 But, that construct is not a cast-expression -- it is a
9983 postfix-expression. (The reason is that `(struct S) { 3 }.i'
9984 is legal; if the compound-literal were a cast-expression,
9985 you'd need an extra set of parentheses.) But, if we parse
9986 the type-id, and it happens to be a class-specifier, then we
9987 will commit to the parse at that point, because we cannot
9988 undo the action that is done when creating a new class. So,
9989 then we cannot back up and do a postfix-expression.
9991 Another tricky case is the following (c++/29234):
9993 struct S { void operator () (); };
9995 void foo ()
9997 ( S()() );
10000 As a type-id we parse the parenthesized S()() as a function
10001 returning a function, groktypename complains and we cannot
10002 back up in this case either.
10004 Therefore, we scan ahead to the closing `)', and check to see
10005 if the tokens after the `)' can start a cast-expression. Otherwise
10006 we are dealing with an unary-expression, a postfix-expression
10007 or something else.
10009 Yet another tricky case, in C++11, is the following (c++/54891):
10011 (void)[]{};
10013 The issue is that usually, besides the case of lambda-expressions,
10014 the parenthesized type-id cannot be followed by '[', and, eg, we
10015 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
10016 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
10017 we don't commit, we try a cast-expression, then an unary-expression.
10019 Save tokens so that we can put them back. */
10020 cp_lexer_save_tokens (parser->lexer);
10022 /* We may be looking at a cast-expression. */
10023 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
10024 /*consume_paren=*/true))
10025 cast_expression
10026 = cp_parser_tokens_start_cast_expression (parser);
10028 /* Roll back the tokens we skipped. */
10029 cp_lexer_rollback_tokens (parser->lexer);
10030 /* If we aren't looking at a cast-expression, simulate an error so
10031 that the call to cp_parser_error_occurred below returns true. */
10032 if (!cast_expression)
10033 cp_parser_simulate_error (parser);
10034 else
10036 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
10037 parser->in_type_id_in_expr_p = true;
10038 /* Look for the type-id. */
10039 type = cp_parser_type_id (parser);
10040 /* Look for the closing `)'. */
10041 cp_token *close_paren = parens.require_close (parser);
10042 if (close_paren)
10043 close_paren_loc = close_paren->location;
10044 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
10047 /* Restore the saved message. */
10048 parser->type_definition_forbidden_message = saved_message;
10050 /* At this point this can only be either a cast or a
10051 parenthesized ctor such as `(T ())' that looks like a cast to
10052 function returning T. */
10053 if (!cp_parser_error_occurred (parser))
10055 /* Only commit if the cast-expression doesn't start with
10056 '++', '--', or '[' in C++11. */
10057 if (cast_expression > 0)
10058 cp_parser_commit_to_topmost_tentative_parse (parser);
10060 expr = cp_parser_cast_expression (parser,
10061 /*address_p=*/false,
10062 /*cast_p=*/true,
10063 /*decltype_p=*/false,
10064 pidk);
10066 if (cp_parser_parse_definitely (parser))
10068 /* Warn about old-style casts, if so requested. */
10069 if (warn_old_style_cast
10070 && !in_system_header_at (input_location)
10071 && !VOID_TYPE_P (type)
10072 && current_lang_name != lang_name_c)
10074 gcc_rich_location rich_loc (input_location);
10075 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
10076 expr, type);
10077 warning_at (&rich_loc, OPT_Wold_style_cast,
10078 "use of old-style cast to %q#T", type);
10081 /* Only type conversions to integral or enumeration types
10082 can be used in constant-expressions. */
10083 if (!cast_valid_in_integral_constant_expression_p (type)
10084 && cp_parser_non_integral_constant_expression (parser,
10085 NIC_CAST))
10086 return error_mark_node;
10088 /* Perform the cast. */
10089 /* Make a location:
10090 (TYPE) EXPR
10091 ^~~~~~~~~~~
10092 with start==caret at the open paren, extending to the
10093 end of "expr". */
10094 location_t cast_loc = make_location (open_paren_loc,
10095 open_paren_loc,
10096 expr.get_finish ());
10097 expr = build_c_cast (cast_loc, type, expr);
10098 return expr;
10101 else
10102 cp_parser_abort_tentative_parse (parser);
10105 /* If we get here, then it's not a cast, so it must be a
10106 unary-expression. */
10107 return cp_parser_unary_expression (parser, pidk, address_p,
10108 cast_p, decltype_p);
10111 /* Parse a binary expression of the general form:
10113 pm-expression:
10114 cast-expression
10115 pm-expression .* cast-expression
10116 pm-expression ->* cast-expression
10118 multiplicative-expression:
10119 pm-expression
10120 multiplicative-expression * pm-expression
10121 multiplicative-expression / pm-expression
10122 multiplicative-expression % pm-expression
10124 additive-expression:
10125 multiplicative-expression
10126 additive-expression + multiplicative-expression
10127 additive-expression - multiplicative-expression
10129 shift-expression:
10130 additive-expression
10131 shift-expression << additive-expression
10132 shift-expression >> additive-expression
10134 relational-expression:
10135 shift-expression
10136 relational-expression < shift-expression
10137 relational-expression > shift-expression
10138 relational-expression <= shift-expression
10139 relational-expression >= shift-expression
10141 GNU Extension:
10143 relational-expression:
10144 relational-expression <? shift-expression
10145 relational-expression >? shift-expression
10147 equality-expression:
10148 relational-expression
10149 equality-expression == relational-expression
10150 equality-expression != relational-expression
10152 and-expression:
10153 equality-expression
10154 and-expression & equality-expression
10156 exclusive-or-expression:
10157 and-expression
10158 exclusive-or-expression ^ and-expression
10160 inclusive-or-expression:
10161 exclusive-or-expression
10162 inclusive-or-expression | exclusive-or-expression
10164 logical-and-expression:
10165 inclusive-or-expression
10166 logical-and-expression && inclusive-or-expression
10168 logical-or-expression:
10169 logical-and-expression
10170 logical-or-expression || logical-and-expression
10172 All these are implemented with a single function like:
10174 binary-expression:
10175 simple-cast-expression
10176 binary-expression <token> binary-expression
10178 CAST_P is true if this expression is the target of a cast.
10180 The binops_by_token map is used to get the tree codes for each <token> type.
10181 binary-expressions are associated according to a precedence table. */
10183 #define TOKEN_PRECEDENCE(token) \
10184 (((token->type == CPP_GREATER \
10185 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
10186 && !parser->greater_than_is_operator_p) \
10187 ? PREC_NOT_OPERATOR \
10188 : binops_by_token[token->type].prec)
10190 static cp_expr
10191 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
10192 bool no_toplevel_fold_p,
10193 bool decltype_p,
10194 enum cp_parser_prec prec,
10195 cp_id_kind * pidk)
10197 cp_parser_expression_stack stack;
10198 cp_parser_expression_stack_entry *sp = &stack[0];
10199 cp_parser_expression_stack_entry *disable_warnings_sp = NULL;
10200 cp_parser_expression_stack_entry current;
10201 cp_expr rhs;
10202 cp_token *token;
10203 enum tree_code rhs_type;
10204 enum cp_parser_prec new_prec, lookahead_prec;
10205 tree overload;
10207 /* Parse the first expression. */
10208 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
10209 ? TRUTH_NOT_EXPR : ERROR_MARK);
10210 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
10211 cast_p, decltype_p, pidk);
10212 current.prec = prec;
10214 if (cp_parser_error_occurred (parser))
10215 return error_mark_node;
10217 for (;;)
10219 /* Get an operator token. */
10220 token = cp_lexer_peek_token (parser->lexer);
10222 if (warn_cxx11_compat
10223 && token->type == CPP_RSHIFT
10224 && !parser->greater_than_is_operator_p)
10226 if (warning_at (token->location, OPT_Wc__11_compat,
10227 "%<>>%> operator is treated"
10228 " as two right angle brackets in C++11"))
10229 inform (token->location,
10230 "suggest parentheses around %<>>%> expression");
10233 new_prec = TOKEN_PRECEDENCE (token);
10234 if (new_prec != PREC_NOT_OPERATOR
10235 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10236 /* This is a fold-expression; handle it later. */
10237 new_prec = PREC_NOT_OPERATOR;
10239 /* Popping an entry off the stack means we completed a subexpression:
10240 - either we found a token which is not an operator (`>' where it is not
10241 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
10242 will happen repeatedly;
10243 - or, we found an operator which has lower priority. This is the case
10244 where the recursive descent *ascends*, as in `3 * 4 + 5' after
10245 parsing `3 * 4'. */
10246 if (new_prec <= current.prec)
10248 if (sp == stack)
10249 break;
10250 else
10251 goto pop;
10254 get_rhs:
10255 current.tree_type = binops_by_token[token->type].tree_type;
10256 current.loc = token->location;
10257 current.flags = token->flags;
10259 /* We used the operator token. */
10260 cp_lexer_consume_token (parser->lexer);
10262 /* For "false && x" or "true || x", x will never be executed;
10263 disable warnings while evaluating it. */
10264 if ((current.tree_type == TRUTH_ANDIF_EXPR
10265 && cp_fully_fold (current.lhs) == truthvalue_false_node)
10266 || (current.tree_type == TRUTH_ORIF_EXPR
10267 && cp_fully_fold (current.lhs) == truthvalue_true_node))
10269 disable_warnings_sp = sp;
10270 ++c_inhibit_evaluation_warnings;
10273 /* Extract another operand. It may be the RHS of this expression
10274 or the LHS of a new, higher priority expression. */
10275 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
10276 ? TRUTH_NOT_EXPR : ERROR_MARK);
10277 rhs = cp_parser_simple_cast_expression (parser);
10279 /* Get another operator token. Look up its precedence to avoid
10280 building a useless (immediately popped) stack entry for common
10281 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
10282 token = cp_lexer_peek_token (parser->lexer);
10283 lookahead_prec = TOKEN_PRECEDENCE (token);
10284 if (lookahead_prec != PREC_NOT_OPERATOR
10285 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10286 lookahead_prec = PREC_NOT_OPERATOR;
10287 if (lookahead_prec > new_prec)
10289 /* ... and prepare to parse the RHS of the new, higher priority
10290 expression. Since precedence levels on the stack are
10291 monotonically increasing, we do not have to care about
10292 stack overflows. */
10293 *sp = current;
10294 ++sp;
10295 current.lhs = rhs;
10296 current.lhs_type = rhs_type;
10297 current.prec = new_prec;
10298 new_prec = lookahead_prec;
10299 goto get_rhs;
10301 pop:
10302 lookahead_prec = new_prec;
10303 /* If the stack is not empty, we have parsed into LHS the right side
10304 (`4' in the example above) of an expression we had suspended.
10305 We can use the information on the stack to recover the LHS (`3')
10306 from the stack together with the tree code (`MULT_EXPR'), and
10307 the precedence of the higher level subexpression
10308 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
10309 which will be used to actually build the additive expression. */
10310 rhs = current.lhs;
10311 rhs_type = current.lhs_type;
10312 --sp;
10313 current = *sp;
10316 /* Undo the disabling of warnings done above. */
10317 if (sp == disable_warnings_sp)
10319 disable_warnings_sp = NULL;
10320 --c_inhibit_evaluation_warnings;
10323 if (warn_logical_not_paren
10324 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
10325 && current.lhs_type == TRUTH_NOT_EXPR
10326 /* Avoid warning for !!x == y. */
10327 && (TREE_CODE (current.lhs) != NE_EXPR
10328 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
10329 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
10330 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
10331 /* Avoid warning for !b == y where b is boolean. */
10332 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
10333 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
10334 != BOOLEAN_TYPE))))
10335 /* Avoid warning for !!b == y where b is boolean. */
10336 && (!(DECL_P (tree_strip_any_location_wrapper (current.lhs))
10337 || (TREE_CODE (current.lhs) == NON_LVALUE_EXPR
10338 && DECL_P (tree_strip_any_location_wrapper
10339 (TREE_OPERAND (current.lhs, 0)))))
10340 || TREE_TYPE (current.lhs) == NULL_TREE
10341 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
10342 warn_logical_not_parentheses (current.loc, current.tree_type,
10343 current.lhs, maybe_constant_value (rhs));
10345 if (warn_xor_used_as_pow
10346 && current.tree_type == BIT_XOR_EXPR
10347 /* Don't warn for named "xor" (as opposed to '^'). */
10348 && !(current.flags & NAMED_OP)
10349 && current.lhs.decimal_p ()
10350 && rhs.decimal_p ())
10351 check_for_xor_used_as_pow
10352 (current.lhs.get_location (),
10353 tree_strip_any_location_wrapper (current.lhs),
10354 current.loc,
10355 rhs.get_location (),
10356 tree_strip_any_location_wrapper (rhs));
10358 overload = NULL;
10360 location_t combined_loc = make_location (current.loc,
10361 current.lhs.get_start (),
10362 rhs.get_finish ());
10364 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
10365 ERROR_MARK for everything that is not a binary expression.
10366 This makes warn_about_parentheses miss some warnings that
10367 involve unary operators. For unary expressions we should
10368 pass the correct tree_code unless the unary expression was
10369 surrounded by parentheses.
10371 if (no_toplevel_fold_p
10372 && lookahead_prec <= current.prec
10373 && sp == stack)
10375 if (current.lhs == error_mark_node || rhs == error_mark_node)
10376 current.lhs = error_mark_node;
10377 else
10379 current.lhs.maybe_add_location_wrapper ();
10380 rhs.maybe_add_location_wrapper ();
10381 current.lhs
10382 = build_min (current.tree_type,
10383 TREE_CODE_CLASS (current.tree_type)
10384 == tcc_comparison
10385 ? boolean_type_node : TREE_TYPE (current.lhs),
10386 current.lhs.get_value (), rhs.get_value ());
10387 SET_EXPR_LOCATION (current.lhs, combined_loc);
10390 else
10392 op_location_t op_loc (current.loc, combined_loc);
10393 current.lhs = build_x_binary_op (op_loc, current.tree_type,
10394 current.lhs, current.lhs_type,
10395 rhs, rhs_type, NULL_TREE, &overload,
10396 complain_flags (decltype_p));
10397 /* TODO: build_x_binary_op doesn't always honor the location. */
10398 current.lhs.set_location (combined_loc);
10400 current.lhs_type = current.tree_type;
10402 /* If the binary operator required the use of an overloaded operator,
10403 then this expression cannot be an integral constant-expression.
10404 An overloaded operator can be used even if both operands are
10405 otherwise permissible in an integral constant-expression if at
10406 least one of the operands is of enumeration type. */
10408 if (overload
10409 && cp_parser_non_integral_constant_expression (parser,
10410 NIC_OVERLOADED))
10411 return error_mark_node;
10414 return current.lhs;
10417 static cp_expr
10418 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
10419 bool no_toplevel_fold_p,
10420 enum cp_parser_prec prec,
10421 cp_id_kind * pidk)
10423 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
10424 /*decltype*/false, prec, pidk);
10427 /* Parse the `? expression : assignment-expression' part of a
10428 conditional-expression. The LOGICAL_OR_EXPR is the
10429 logical-or-expression that started the conditional-expression.
10430 Returns a representation of the entire conditional-expression.
10432 This routine is used by cp_parser_assignment_expression
10433 and cp_parser_conditional_expression.
10435 ? expression : assignment-expression
10437 GNU Extensions:
10439 ? : assignment-expression */
10441 static tree
10442 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
10444 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
10445 cp_expr assignment_expr;
10446 struct cp_token *token;
10447 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10449 /* Consume the `?' token. */
10450 cp_lexer_consume_token (parser->lexer);
10451 token = cp_lexer_peek_token (parser->lexer);
10452 if (cp_parser_allow_gnu_extensions_p (parser)
10453 && token->type == CPP_COLON)
10455 pedwarn (token->location, OPT_Wpedantic,
10456 "ISO C++ does not allow %<?:%> with omitted middle operand");
10457 /* Implicit true clause. */
10458 expr = NULL_TREE;
10459 c_inhibit_evaluation_warnings +=
10460 folded_logical_or_expr == truthvalue_true_node;
10461 warn_for_omitted_condop (token->location, logical_or_expr);
10463 else
10465 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10466 parser->colon_corrects_to_scope_p = false;
10467 /* Parse the expression. */
10468 c_inhibit_evaluation_warnings +=
10469 folded_logical_or_expr == truthvalue_false_node;
10470 expr = cp_parser_expression (parser);
10471 c_inhibit_evaluation_warnings +=
10472 ((folded_logical_or_expr == truthvalue_true_node)
10473 - (folded_logical_or_expr == truthvalue_false_node));
10474 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10477 /* The next token should be a `:'. */
10478 cp_parser_require (parser, CPP_COLON, RT_COLON);
10479 /* Parse the assignment-expression. */
10480 assignment_expr = cp_parser_assignment_expression (parser);
10481 c_inhibit_evaluation_warnings -=
10482 folded_logical_or_expr == truthvalue_true_node;
10484 /* Make a location:
10485 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
10486 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
10487 with the caret at the "?", ranging from the start of
10488 the logical_or_expr to the end of the assignment_expr. */
10489 loc = make_location (loc,
10490 logical_or_expr.get_start (),
10491 assignment_expr.get_finish ());
10493 /* Build the conditional-expression. */
10494 return build_x_conditional_expr (loc, logical_or_expr,
10495 expr,
10496 assignment_expr,
10497 tf_warning_or_error);
10500 /* Parse a conditional-expression.
10502 conditional-expression:
10503 logical-or-expression
10504 logical-or-expression ? expression : assignment-expression
10506 GNU Extensions:
10508 logical-or-expression ? : assignment-expression */
10510 static cp_expr
10511 cp_parser_conditional_expression (cp_parser *parser)
10513 cp_expr expr = cp_parser_binary_expression (parser, false, false, false,
10514 PREC_NOT_OPERATOR, NULL);
10515 /* If the next token is a `?' then we're actually looking at
10516 a conditional-expression; otherwise we're done. */
10517 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
10518 return cp_parser_question_colon_clause (parser, expr);
10519 return expr;
10522 /* Parse an assignment-expression.
10524 assignment-expression:
10525 conditional-expression
10526 logical-or-expression assignment-operator assignment_expression
10527 throw-expression
10528 yield-expression
10530 CAST_P is true if this expression is the target of a cast.
10531 DECLTYPE_P is true if this expression is the operand of decltype.
10533 Returns a representation for the expression. */
10535 static cp_expr
10536 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
10537 bool cast_p, bool decltype_p)
10539 cp_expr expr;
10541 /* If the next token is the `throw' keyword, then we're looking at
10542 a throw-expression. */
10543 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
10544 expr = cp_parser_throw_expression (parser);
10545 /* If the next token is the `co_yield' keyword, then we're looking at
10546 a yield-expression. */
10547 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CO_YIELD))
10548 expr = cp_parser_yield_expression (parser);
10549 /* Otherwise, it must be that we are looking at a
10550 logical-or-expression. */
10551 else
10553 /* Parse the binary expressions (logical-or-expression). */
10554 expr = cp_parser_binary_expression (parser, cast_p, false,
10555 decltype_p,
10556 PREC_NOT_OPERATOR, pidk);
10557 /* If the next token is a `?' then we're actually looking at a
10558 conditional-expression. */
10559 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
10560 return cp_parser_question_colon_clause (parser, expr);
10561 else
10563 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10565 /* If it's an assignment-operator, we're using the second
10566 production. */
10567 enum tree_code assignment_operator
10568 = cp_parser_assignment_operator_opt (parser);
10569 if (assignment_operator != ERROR_MARK)
10571 /* Parse the right-hand side of the assignment. */
10572 cp_expr rhs = cp_parser_initializer_clause (parser);
10574 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
10575 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10577 /* An assignment may not appear in a
10578 constant-expression. */
10579 if (cp_parser_non_integral_constant_expression (parser,
10580 NIC_ASSIGNMENT))
10581 return error_mark_node;
10582 /* Build the assignment expression. Its default
10583 location:
10584 LHS = RHS
10585 ~~~~^~~~~
10586 is the location of the '=' token as the
10587 caret, ranging from the start of the lhs to the
10588 end of the rhs. */
10589 loc = make_location (loc,
10590 expr.get_start (),
10591 rhs.get_finish ());
10592 expr = build_x_modify_expr (loc, expr,
10593 assignment_operator,
10594 rhs, NULL_TREE,
10595 complain_flags (decltype_p));
10596 /* TODO: build_x_modify_expr doesn't honor the location,
10597 so we must set it here. */
10598 expr.set_location (loc);
10603 return expr;
10606 /* Parse an (optional) assignment-operator.
10608 assignment-operator: one of
10609 = *= /= %= += -= >>= <<= &= ^= |=
10611 GNU Extension:
10613 assignment-operator: one of
10614 <?= >?=
10616 If the next token is an assignment operator, the corresponding tree
10617 code is returned, and the token is consumed. For example, for
10618 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
10619 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
10620 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
10621 operator, ERROR_MARK is returned. */
10623 static enum tree_code
10624 cp_parser_assignment_operator_opt (cp_parser* parser)
10626 enum tree_code op;
10627 cp_token *token;
10629 /* Peek at the next token. */
10630 token = cp_lexer_peek_token (parser->lexer);
10632 switch (token->type)
10634 case CPP_EQ:
10635 op = NOP_EXPR;
10636 break;
10638 case CPP_MULT_EQ:
10639 op = MULT_EXPR;
10640 break;
10642 case CPP_DIV_EQ:
10643 op = TRUNC_DIV_EXPR;
10644 break;
10646 case CPP_MOD_EQ:
10647 op = TRUNC_MOD_EXPR;
10648 break;
10650 case CPP_PLUS_EQ:
10651 op = PLUS_EXPR;
10652 break;
10654 case CPP_MINUS_EQ:
10655 op = MINUS_EXPR;
10656 break;
10658 case CPP_RSHIFT_EQ:
10659 op = RSHIFT_EXPR;
10660 break;
10662 case CPP_LSHIFT_EQ:
10663 op = LSHIFT_EXPR;
10664 break;
10666 case CPP_AND_EQ:
10667 op = BIT_AND_EXPR;
10668 break;
10670 case CPP_XOR_EQ:
10671 op = BIT_XOR_EXPR;
10672 break;
10674 case CPP_OR_EQ:
10675 op = BIT_IOR_EXPR;
10676 break;
10678 default:
10679 /* Nothing else is an assignment operator. */
10680 op = ERROR_MARK;
10683 /* An operator followed by ... is a fold-expression, handled elsewhere. */
10684 if (op != ERROR_MARK
10685 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10686 op = ERROR_MARK;
10688 /* If it was an assignment operator, consume it. */
10689 if (op != ERROR_MARK)
10690 cp_lexer_consume_token (parser->lexer);
10692 return op;
10695 /* Parse an expression.
10697 expression:
10698 assignment-expression
10699 expression , assignment-expression
10701 CAST_P is true if this expression is the target of a cast.
10702 DECLTYPE_P is true if this expression is the immediate operand of decltype,
10703 except possibly parenthesized or on the RHS of a comma (N3276).
10704 WARN_COMMA_P is true if a comma should be diagnosed.
10706 Returns a representation of the expression. */
10708 static cp_expr
10709 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
10710 bool cast_p, bool decltype_p, bool warn_comma_p)
10712 cp_expr expression = NULL_TREE;
10713 location_t loc = UNKNOWN_LOCATION;
10715 while (true)
10717 cp_expr assignment_expression;
10719 /* Parse the next assignment-expression. */
10720 assignment_expression
10721 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
10723 /* We don't create a temporary for a call that is the immediate operand
10724 of decltype or on the RHS of a comma. But when we see a comma, we
10725 need to create a temporary for a call on the LHS. */
10726 if (decltype_p && !processing_template_decl
10727 && TREE_CODE (assignment_expression) == CALL_EXPR
10728 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
10729 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10730 assignment_expression
10731 = build_cplus_new (TREE_TYPE (assignment_expression),
10732 assignment_expression, tf_warning_or_error);
10734 /* If this is the first assignment-expression, we can just
10735 save it away. */
10736 if (!expression)
10737 expression = assignment_expression;
10738 else
10740 /* Create a location with caret at the comma, ranging
10741 from the start of the LHS to the end of the RHS. */
10742 loc = make_location (loc,
10743 expression.get_start (),
10744 assignment_expression.get_finish ());
10745 expression = build_x_compound_expr (loc, expression,
10746 assignment_expression, NULL_TREE,
10747 complain_flags (decltype_p));
10748 expression.set_location (loc);
10750 /* If the next token is not a comma, or we're in a fold-expression, then
10751 we are done with the expression. */
10752 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
10753 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10754 break;
10755 /* Consume the `,'. */
10756 loc = cp_lexer_peek_token (parser->lexer)->location;
10757 if (warn_comma_p)
10759 /* [depr.comma.subscript]: A comma expression appearing as
10760 the expr-or-braced-init-list of a subscripting expression
10761 is deprecated. A parenthesized comma expression is not
10762 deprecated. */
10763 warning_at (loc, OPT_Wcomma_subscript,
10764 "top-level comma expression in array subscript "
10765 "is deprecated");
10766 warn_comma_p = false;
10768 cp_lexer_consume_token (parser->lexer);
10769 /* A comma operator cannot appear in a constant-expression. */
10770 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
10771 expression = error_mark_node;
10774 return expression;
10777 /* Parse a constant-expression.
10779 constant-expression:
10780 conditional-expression
10782 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
10783 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
10784 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
10785 is false, NON_CONSTANT_P should be NULL. If ALLOW_NON_CONSTANT_P is
10786 greater than 1, this isn't really a constant-expression, only a
10787 potentially constant-evaluated expression. If STRICT_P is true,
10788 only parse a conditional-expression, otherwise parse an
10789 assignment-expression. See below for rationale. */
10791 static cp_expr
10792 cp_parser_constant_expression (cp_parser* parser,
10793 int allow_non_constant_p /* = 0 */,
10794 bool *non_constant_p /* = NULL */,
10795 bool strict_p /* = false */)
10797 /* It might seem that we could simply parse the
10798 conditional-expression, and then check to see if it were
10799 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
10800 one that the compiler can figure out is constant, possibly after
10801 doing some simplifications or optimizations. The standard has a
10802 precise definition of constant-expression, and we must honor
10803 that, even though it is somewhat more restrictive.
10805 For example:
10807 int i[(2, 3)];
10809 is not a legal declaration, because `(2, 3)' is not a
10810 constant-expression. The `,' operator is forbidden in a
10811 constant-expression. However, GCC's constant-folding machinery
10812 will fold this operation to an INTEGER_CST for `3'. */
10814 /* Save the old settings. */
10815 bool saved_integral_constant_expression_p
10816 = parser->integral_constant_expression_p;
10817 bool saved_allow_non_integral_constant_expression_p
10818 = parser->allow_non_integral_constant_expression_p;
10819 bool saved_non_integral_constant_expression_p
10820 = parser->non_integral_constant_expression_p;
10821 /* We are now parsing a constant-expression. */
10822 parser->integral_constant_expression_p = true;
10823 parser->allow_non_integral_constant_expression_p
10824 = (allow_non_constant_p || cxx_dialect >= cxx11);
10825 parser->non_integral_constant_expression_p = false;
10827 /* A manifestly constant-evaluated expression is evaluated even in an
10828 unevaluated operand. */
10829 cp_evaluated ev (/*reset if*/allow_non_constant_p <= 1);
10831 /* Although the grammar says "conditional-expression", when not STRICT_P,
10832 we parse an "assignment-expression", which also permits
10833 "throw-expression" and the use of assignment operators. In the case
10834 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
10835 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
10836 actually essential that we look for an assignment-expression.
10837 For example, cp_parser_initializer_clauses uses this function to
10838 determine whether a particular assignment-expression is in fact
10839 constant. */
10840 cp_expr expression;
10841 if (strict_p)
10842 expression = cp_parser_conditional_expression (parser);
10843 else
10844 expression = cp_parser_assignment_expression (parser);
10845 /* Restore the old settings. */
10846 parser->integral_constant_expression_p
10847 = saved_integral_constant_expression_p;
10848 parser->allow_non_integral_constant_expression_p
10849 = saved_allow_non_integral_constant_expression_p;
10850 if (cxx_dialect >= cxx11
10851 && (!allow_non_constant_p || non_constant_p))
10853 /* Require an rvalue constant expression here; that's what our
10854 callers expect. Reference constant expressions are handled
10855 separately in e.g. cp_parser_template_argument. */
10856 tree decay = expression;
10857 if (TREE_TYPE (expression)
10858 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
10859 decay = build_address (expression);
10860 bool is_const = is_rvalue_constant_expression (decay);
10861 parser->non_integral_constant_expression_p = !is_const;
10862 if (!is_const && !allow_non_constant_p)
10863 require_rvalue_constant_expression (decay);
10865 if (allow_non_constant_p && non_constant_p)
10866 *non_constant_p = parser->non_integral_constant_expression_p;
10867 parser->non_integral_constant_expression_p
10868 = saved_non_integral_constant_expression_p;
10870 return expression;
10873 /* Parse __builtin_offsetof.
10875 offsetof-expression:
10876 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
10878 offsetof-member-designator:
10879 id-expression
10880 | offsetof-member-designator "." id-expression
10881 | offsetof-member-designator "[" expression "]"
10882 | offsetof-member-designator "->" id-expression */
10884 static cp_expr
10885 cp_parser_builtin_offsetof (cp_parser *parser)
10887 int save_ice_p, save_non_ice_p;
10888 tree type;
10889 cp_expr expr;
10890 cp_id_kind dummy;
10891 cp_token *token;
10892 location_t finish_loc;
10894 /* We're about to accept non-integral-constant things, but will
10895 definitely yield an integral constant expression. Save and
10896 restore these values around our local parsing. */
10897 save_ice_p = parser->integral_constant_expression_p;
10898 save_non_ice_p = parser->non_integral_constant_expression_p;
10900 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10902 /* Consume the "__builtin_offsetof" token. */
10903 cp_lexer_consume_token (parser->lexer);
10904 /* Consume the opening `('. */
10905 matching_parens parens;
10906 parens.require_open (parser);
10907 /* Parse the type-id. */
10908 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10910 const char *saved_message = parser->type_definition_forbidden_message;
10911 parser->type_definition_forbidden_message
10912 = G_("types may not be defined within %<__builtin_offsetof%>");
10913 type = cp_parser_type_id (parser);
10914 parser->type_definition_forbidden_message = saved_message;
10916 /* Look for the `,'. */
10917 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10918 token = cp_lexer_peek_token (parser->lexer);
10920 /* Build the (type *)null that begins the traditional offsetof macro. */
10921 tree object_ptr
10922 = build_static_cast (input_location, build_pointer_type (type),
10923 null_pointer_node, tf_warning_or_error);
10925 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
10926 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
10927 true, &dummy, token->location);
10928 while (true)
10930 token = cp_lexer_peek_token (parser->lexer);
10931 switch (token->type)
10933 case CPP_OPEN_SQUARE:
10934 /* offsetof-member-designator "[" expression "]" */
10935 expr = cp_parser_postfix_open_square_expression (parser, expr,
10936 true, false);
10937 break;
10939 case CPP_DEREF:
10940 /* offsetof-member-designator "->" identifier */
10941 expr = grok_array_decl (token->location, expr, integer_zero_node,
10942 NULL, tf_warning_or_error);
10943 /* FALLTHRU */
10945 case CPP_DOT:
10946 /* offsetof-member-designator "." identifier */
10947 cp_lexer_consume_token (parser->lexer);
10948 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
10949 expr, true, &dummy,
10950 token->location);
10951 break;
10953 case CPP_CLOSE_PAREN:
10954 /* Consume the ")" token. */
10955 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10956 cp_lexer_consume_token (parser->lexer);
10957 goto success;
10959 default:
10960 /* Error. We know the following require will fail, but
10961 that gives the proper error message. */
10962 parens.require_close (parser);
10963 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
10964 expr = error_mark_node;
10965 goto failure;
10969 success:
10970 /* Make a location of the form:
10971 __builtin_offsetof (struct s, f)
10972 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
10973 with caret at the type-id, ranging from the start of the
10974 "_builtin_offsetof" token to the close paren. */
10975 loc = make_location (loc, start_loc, finish_loc);
10976 /* The result will be an INTEGER_CST, so we need to explicitly
10977 preserve the location. */
10978 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
10980 failure:
10981 parser->integral_constant_expression_p = save_ice_p;
10982 parser->non_integral_constant_expression_p = save_non_ice_p;
10984 expr = expr.maybe_add_location_wrapper ();
10985 return expr;
10988 /* Parse a builtin trait expression or type. */
10990 static cp_expr
10991 cp_parser_trait (cp_parser* parser, enum rid keyword)
10993 cp_trait_kind kind;
10994 tree type1, type2 = NULL_TREE;
10995 bool binary = false;
10996 bool variadic = false;
10997 bool type = false;
10999 switch (keyword)
11001 #define DEFTRAIT(TCC, CODE, NAME, ARITY) \
11002 case RID_##CODE: \
11003 kind = CPTK_##CODE; \
11004 binary = (ARITY == 2); \
11005 variadic = (ARITY == -1); \
11006 type = (TCC == tcc_type); \
11007 break;
11008 #include "cp-trait.def"
11009 #undef DEFTRAIT
11010 default:
11011 gcc_unreachable ();
11014 /* Get location of initial token. */
11015 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
11017 /* Consume the token. */
11018 cp_lexer_consume_token (parser->lexer);
11020 matching_parens parens;
11021 if (kind == CPTK_TYPE_PACK_ELEMENT)
11022 cp_parser_require (parser, CPP_LESS, RT_LESS);
11023 else
11024 parens.require_open (parser);
11026 if (kind == CPTK_IS_DEDUCIBLE)
11028 const cp_token* token = cp_lexer_peek_token (parser->lexer);
11029 type1 = cp_parser_id_expression (parser,
11030 /*template_keyword_p=*/false,
11031 /*check_dependency_p=*/true,
11032 nullptr,
11033 /*declarator_p=*/false,
11034 /*optional_p=*/false);
11035 type1 = cp_parser_lookup_name_simple (parser, type1, token->location);
11037 else if (kind == CPTK_TYPE_PACK_ELEMENT)
11038 /* __type_pack_element takes an expression as its first argument and uses
11039 template-id syntax instead of function call syntax (for consistency
11040 with Clang). We special case these properties of __type_pack_element
11041 here and elsewhere. */
11042 type1 = cp_parser_constant_expression (parser);
11043 else
11045 type_id_in_expr_sentinel s (parser);
11046 type1 = cp_parser_type_id (parser);
11049 if (type1 == error_mark_node)
11050 return error_mark_node;
11052 if (kind == CPTK_TYPE_PACK_ELEMENT)
11054 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
11055 tree trailing = cp_parser_enclosed_template_argument_list (parser);
11056 for (tree elt : tree_vec_range (trailing))
11058 if (!TYPE_P (elt))
11060 error_at (cp_expr_loc_or_input_loc (elt),
11061 "trailing argument to %<__type_pack_element%> "
11062 "is not a type");
11063 return error_mark_node;
11066 type2 = trailing;
11068 else if (binary)
11070 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
11073 type_id_in_expr_sentinel s (parser);
11074 type2 = cp_parser_type_id (parser);
11077 if (type2 == error_mark_node)
11078 return error_mark_node;
11080 else if (variadic)
11082 auto_vec<tree, 4> trailing;
11083 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11085 cp_lexer_consume_token (parser->lexer);
11086 tree elt = cp_parser_type_id (parser);
11087 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11089 cp_lexer_consume_token (parser->lexer);
11090 elt = make_pack_expansion (elt);
11092 if (elt == error_mark_node)
11093 return error_mark_node;
11094 trailing.safe_push (elt);
11096 type2 = make_tree_vec (trailing.length ());
11097 for (int i = 0; i < TREE_VEC_LENGTH (type2); ++i)
11098 TREE_VEC_ELT (type2, i) = trailing[i];
11101 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
11102 if (kind == CPTK_TYPE_PACK_ELEMENT)
11103 /* cp_parser_enclosed_template_argument_list above already took care
11104 of parsing the closing '>'. */;
11105 else
11106 parens.require_close (parser);
11108 /* Construct a location of the form:
11109 __is_trivially_copyable(_Tp)
11110 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
11111 with start == caret, finishing at the close-paren. */
11112 location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
11114 /* Complete the trait expression, which may mean either processing
11115 the trait expr now or saving it for template instantiation. */
11116 switch (kind)
11118 case CPTK_BASES:
11119 return cp_expr (finish_bases (type1, false), trait_loc);
11120 case CPTK_DIRECT_BASES:
11121 return cp_expr (finish_bases (type1, true), trait_loc);
11122 default:
11123 if (type)
11124 return finish_trait_type (kind, type1, type2, tf_warning_or_error);
11125 else
11126 return finish_trait_expr (trait_loc, kind, type1, type2);
11130 /* Parse a lambda expression.
11132 lambda-expression:
11133 lambda-introducer lambda-declarator [opt] compound-statement
11134 lambda-introducer < template-parameter-list > requires-clause [opt]
11135 lambda-declarator [opt] compound-statement
11137 Returns a representation of the expression. */
11139 static cp_expr
11140 cp_parser_lambda_expression (cp_parser* parser)
11142 tree lambda_expr = build_lambda_expr ();
11143 tree type;
11144 bool ok = true;
11145 cp_token *token = cp_lexer_peek_token (parser->lexer);
11146 cp_token_position start = 0;
11148 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
11150 if (cxx_dialect >= cxx20)
11152 /* C++20 allows lambdas in unevaluated context, but one in the type of a
11153 non-type parameter is nonsensical.
11155 Distinguish a lambda in the parameter type from a lambda in the
11156 default argument by looking at local_variables_forbidden_p, which is
11157 only set in default arguments. */
11158 if (processing_template_parmlist && !parser->local_variables_forbidden_p)
11160 error_at (token->location,
11161 "lambda-expression in template parameter type");
11162 token->error_reported = true;
11163 ok = false;
11166 else if (cp_unevaluated_operand)
11168 if (!token->error_reported)
11170 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
11171 "lambda-expression in unevaluated context"
11172 " only available with %<-std=c++20%> or %<-std=gnu++20%>");
11173 token->error_reported = true;
11175 ok = false;
11177 else if (parser->in_template_argument_list_p || processing_template_parmlist)
11179 if (!token->error_reported)
11181 error_at (token->location, "lambda-expression in template-argument"
11182 " only available with %<-std=c++20%> or %<-std=gnu++20%>");
11183 token->error_reported = true;
11185 ok = false;
11188 /* We may be in the middle of deferred access check. Disable
11189 it now. */
11190 push_deferring_access_checks (dk_no_deferred);
11192 cp_parser_lambda_introducer (parser, lambda_expr);
11193 if (cp_parser_error_occurred (parser))
11194 return error_mark_node;
11196 type = begin_lambda_type (lambda_expr);
11197 if (type == error_mark_node)
11198 return error_mark_node;
11200 record_lambda_scope (lambda_expr);
11201 record_lambda_scope_discriminator (lambda_expr);
11203 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
11204 determine_visibility (TYPE_NAME (type));
11206 /* Now that we've started the type, add the capture fields for any
11207 explicit captures. */
11208 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
11211 /* Inside the class, surrounding template-parameter-lists do not apply. */
11212 unsigned int saved_num_template_parameter_lists
11213 = parser->num_template_parameter_lists;
11214 unsigned char in_statement = parser->in_statement;
11215 bool in_switch_statement_p = parser->in_switch_statement_p;
11216 bool fully_implicit_function_template_p
11217 = parser->fully_implicit_function_template_p;
11218 tree implicit_template_parms = parser->implicit_template_parms;
11219 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
11220 bool auto_is_implicit_function_template_parm_p
11221 = parser->auto_is_implicit_function_template_parm_p;
11223 parser->num_template_parameter_lists = 0;
11224 parser->in_statement = 0;
11225 parser->in_switch_statement_p = false;
11226 parser->fully_implicit_function_template_p = false;
11227 parser->implicit_template_parms = 0;
11228 parser->implicit_template_scope = 0;
11229 parser->auto_is_implicit_function_template_parm_p = false;
11231 /* The body of a lambda in a discarded statement is not discarded. */
11232 bool discarded = in_discarded_stmt;
11233 in_discarded_stmt = 0;
11235 /* Similarly the body of a lambda in immediate function context is not
11236 in immediate function context. */
11237 bool save_in_consteval_if_p = in_consteval_if_p;
11238 in_consteval_if_p = false;
11240 /* By virtue of defining a local class, a lambda expression has access to
11241 the private variables of enclosing classes. */
11243 if (cp_parser_start_tentative_firewall (parser))
11244 start = token;
11246 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
11248 if (ok && cp_parser_error_occurred (parser))
11249 ok = false;
11251 if (ok)
11252 cp_parser_lambda_body (parser, lambda_expr);
11253 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
11255 if (cp_parser_skip_to_closing_brace (parser))
11256 cp_lexer_consume_token (parser->lexer);
11259 /* The capture list was built up in reverse order; fix that now. */
11260 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
11261 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
11263 if (ok)
11264 maybe_add_lambda_conv_op (type);
11266 finish_struct (type, /*attributes=*/NULL_TREE);
11268 in_consteval_if_p = save_in_consteval_if_p;
11269 in_discarded_stmt = discarded;
11271 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
11272 parser->in_statement = in_statement;
11273 parser->in_switch_statement_p = in_switch_statement_p;
11274 parser->fully_implicit_function_template_p
11275 = fully_implicit_function_template_p;
11276 parser->implicit_template_parms = implicit_template_parms;
11277 parser->implicit_template_scope = implicit_template_scope;
11278 parser->auto_is_implicit_function_template_parm_p
11279 = auto_is_implicit_function_template_parm_p;
11282 /* This field is only used during parsing of the lambda. */
11283 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
11285 /* This lambda shouldn't have any proxies left at this point. */
11286 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
11287 /* And now that we're done, push proxies for an enclosing lambda. */
11288 insert_pending_capture_proxies ();
11290 /* Update the lambda expression to a range. */
11291 LAMBDA_EXPR_LOCATION (lambda_expr) = make_location (token->location,
11292 token->location,
11293 parser->lexer);
11295 if (ok)
11296 lambda_expr = build_lambda_object (lambda_expr);
11297 else
11298 lambda_expr = error_mark_node;
11300 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
11302 pop_deferring_access_checks ();
11304 return lambda_expr;
11307 /* Parse the beginning of a lambda expression.
11309 lambda-introducer:
11310 [ lambda-capture [opt] ]
11312 LAMBDA_EXPR is the current representation of the lambda expression. */
11314 static void
11315 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
11317 /* Need commas after the first capture. */
11318 bool first = true;
11320 /* Eat the leading `['. */
11321 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
11323 /* Record default capture mode. "[&" "[=" "[&," "[=," */
11324 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
11325 && !cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS)
11326 && !cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME)
11327 && !cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
11328 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
11329 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11330 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
11332 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
11334 cp_lexer_consume_token (parser->lexer);
11335 first = false;
11337 if (!(at_function_scope_p () || parsing_nsdmi ()))
11338 error ("non-local lambda expression cannot have a capture-default");
11341 hash_set<tree, true> ids;
11342 tree first_capture_id = NULL_TREE;
11343 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
11345 cp_token* capture_token;
11346 tree capture_id;
11347 tree capture_init_expr;
11348 cp_id_kind idk = CP_ID_KIND_NONE;
11349 bool explicit_init_p = false;
11351 enum capture_kind_type
11353 BY_COPY,
11354 BY_REFERENCE
11356 enum capture_kind_type capture_kind = BY_COPY;
11358 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
11360 error ("expected end of capture-list");
11361 return;
11364 if (first)
11365 first = false;
11366 else
11367 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
11369 /* Possibly capture `this'. */
11370 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
11372 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11373 if (cxx_dialect < cxx20 && pedantic
11374 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
11375 pedwarn (loc, OPT_Wc__20_extensions,
11376 "explicit by-copy capture of %<this%> "
11377 "with by-copy capture default only available with "
11378 "%<-std=c++20%> or %<-std=gnu++20%>");
11379 cp_lexer_consume_token (parser->lexer);
11380 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
11381 pedwarn (input_location, 0,
11382 "already captured %qD in lambda expression",
11383 this_identifier);
11384 else
11385 add_capture (lambda_expr, /*id=*/this_identifier,
11386 /*initializer=*/finish_this_expr (),
11387 /*by_reference_p=*/true, explicit_init_p);
11388 continue;
11391 /* Possibly capture `*this'. */
11392 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
11393 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
11395 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11396 if (cxx_dialect < cxx17)
11397 pedwarn (loc, OPT_Wc__17_extensions,
11398 "%<*this%> capture only available with "
11399 "%<-std=c++17%> or %<-std=gnu++17%>");
11400 cp_lexer_consume_token (parser->lexer);
11401 cp_lexer_consume_token (parser->lexer);
11402 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
11403 pedwarn (input_location, 0,
11404 "already captured %qD in lambda expression",
11405 this_identifier);
11406 else
11407 add_capture (lambda_expr, /*id=*/this_identifier,
11408 /*initializer=*/finish_this_expr (),
11409 /*by_reference_p=*/false, explicit_init_p);
11410 continue;
11413 /* But reject `&this'. */
11414 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
11415 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
11417 error_at (cp_lexer_peek_token (parser->lexer)->location,
11418 "%<this%> cannot be captured by reference");
11419 cp_lexer_consume_token (parser->lexer);
11420 cp_lexer_consume_token (parser->lexer);
11421 continue;
11424 /* Remember whether we want to capture as a reference or not. */
11425 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
11427 capture_kind = BY_REFERENCE;
11428 cp_lexer_consume_token (parser->lexer);
11431 bool init_pack_expansion = false;
11432 location_t ellipsis_loc = UNKNOWN_LOCATION;
11433 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11435 ellipsis_loc = cp_lexer_peek_token (parser->lexer)->location;
11436 if (cxx_dialect < cxx20)
11437 pedwarn (ellipsis_loc, OPT_Wc__20_extensions,
11438 "pack init-capture only available with "
11439 "%<-std=c++20%> or %<-std=gnu++20%>");
11440 cp_lexer_consume_token (parser->lexer);
11441 init_pack_expansion = true;
11444 /* Early C++20 drafts had ...& instead of &...; be forgiving. */
11445 if (init_pack_expansion && capture_kind != BY_REFERENCE
11446 && cp_lexer_next_token_is (parser->lexer, CPP_AND))
11448 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
11449 0, "%<&%> should come before %<...%>");
11450 capture_kind = BY_REFERENCE;
11451 cp_lexer_consume_token (parser->lexer);
11454 /* Get the identifier. */
11455 capture_token = cp_lexer_peek_token (parser->lexer);
11456 capture_id = cp_parser_identifier (parser);
11458 if (capture_id == error_mark_node)
11459 /* Would be nice to have a cp_parser_skip_to_closing_x for general
11460 delimiters, but I modified this to stop on unnested ']' as well. It
11461 was already changed to stop on unnested '}', so the
11462 "closing_parenthesis" name is no more misleading with my change. */
11464 cp_parser_skip_to_closing_parenthesis (parser,
11465 /*recovering=*/true,
11466 /*or_comma=*/true,
11467 /*consume_paren=*/true);
11468 break;
11471 /* Find the initializer for this capture. */
11472 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
11473 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
11474 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11476 /* An explicit initializer exists. */
11477 if (cxx_dialect < cxx14)
11478 pedwarn (input_location, OPT_Wc__14_extensions,
11479 "lambda capture initializers "
11480 "only available with %<-std=c++14%> or %<-std=gnu++14%>");
11481 capture_init_expr = cp_parser_initializer (parser,
11482 /*direct_init=*/nullptr,
11483 /*non_constant=*/nullptr,
11484 /*subexpression_p=*/true);
11485 explicit_init_p = true;
11486 if (capture_init_expr == NULL_TREE)
11488 error ("empty initializer for lambda init-capture");
11489 capture_init_expr = error_mark_node;
11491 if (init_pack_expansion)
11492 capture_init_expr = make_pack_expansion (capture_init_expr);
11494 else
11496 const char* error_msg;
11498 /* Turn the identifier into an id-expression. */
11499 capture_init_expr
11500 = cp_parser_lookup_name_simple (parser, capture_id,
11501 capture_token->location);
11503 if (capture_init_expr == error_mark_node)
11505 unqualified_name_lookup_error (capture_id);
11506 continue;
11508 else if (!VAR_P (capture_init_expr)
11509 && TREE_CODE (capture_init_expr) != PARM_DECL)
11511 error_at (capture_token->location,
11512 "capture of non-variable %qE",
11513 capture_init_expr);
11514 if (DECL_P (capture_init_expr))
11515 inform (DECL_SOURCE_LOCATION (capture_init_expr),
11516 "%q#D declared here", capture_init_expr);
11517 continue;
11519 if (VAR_P (capture_init_expr)
11520 && decl_storage_duration (capture_init_expr) != dk_auto)
11522 if (pedwarn (capture_token->location, 0, "capture of variable "
11523 "%qD with non-automatic storage duration",
11524 capture_init_expr))
11525 inform (DECL_SOURCE_LOCATION (capture_init_expr),
11526 "%q#D declared here", capture_init_expr);
11527 continue;
11530 capture_init_expr
11531 = finish_id_expression
11532 (capture_id,
11533 capture_init_expr,
11534 parser->scope,
11535 &idk,
11536 /*integral_constant_expression_p=*/false,
11537 /*allow_non_integral_constant_expression_p=*/false,
11538 /*non_integral_constant_expression_p=*/NULL,
11539 /*template_p=*/false,
11540 /*done=*/true,
11541 /*address_p=*/false,
11542 /*template_arg_p=*/false,
11543 &error_msg,
11544 capture_token->location);
11546 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11548 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11549 cp_lexer_consume_token (parser->lexer);
11550 capture_init_expr = make_pack_expansion (capture_init_expr);
11551 if (init_pack_expansion)
11553 /* If what follows is an initializer, the second '...' is
11554 invalid. But for cases like [...xs...], the first one
11555 is invalid. */
11556 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
11557 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
11558 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11559 ellipsis_loc = loc;
11560 error_at (ellipsis_loc, "too many %<...%> in lambda capture");
11561 continue;
11566 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
11567 && !explicit_init_p)
11569 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
11570 && capture_kind == BY_COPY)
11571 pedwarn (capture_token->location, 0, "explicit by-copy capture "
11572 "of %qD redundant with by-copy capture default",
11573 capture_id);
11574 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
11575 && capture_kind == BY_REFERENCE)
11576 pedwarn (capture_token->location, 0, "explicit by-reference "
11577 "capture of %qD redundant with by-reference capture "
11578 "default", capture_id);
11581 /* Check for duplicates.
11582 Optimize for the zero or one explicit captures cases and only create
11583 the hash_set after adding second capture. */
11584 bool found = false;
11585 if (!ids.is_empty ())
11586 found = ids.add (capture_id);
11587 else if (first_capture_id == NULL_TREE)
11588 first_capture_id = capture_id;
11589 else if (capture_id == first_capture_id)
11590 found = true;
11591 else
11593 ids.add (first_capture_id);
11594 ids.add (capture_id);
11596 if (found)
11597 pedwarn (input_location, 0,
11598 "already captured %qD in lambda expression", capture_id);
11599 else
11600 add_capture (lambda_expr, capture_id, capture_init_expr,
11601 /*by_reference_p=*/capture_kind == BY_REFERENCE,
11602 explicit_init_p);
11604 /* If there is any qualification still in effect, clear it
11605 now; we will be starting fresh with the next capture. */
11606 parser->scope = NULL_TREE;
11607 parser->qualifying_scope = NULL_TREE;
11608 parser->object_scope = NULL_TREE;
11611 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11614 /* Parse the (optional) middle of a lambda expression.
11616 lambda-declarator:
11617 ( parameter-declaration-clause ) lambda-specifiers requires-clause [opt]
11618 lambda-specifiers (C++23)
11620 lambda-specifiers:
11621 decl-specifier-seq [opt] noexcept-specifier [opt]
11622 attribute-specifier-seq [opt] trailing-return-type [opt]
11624 LAMBDA_EXPR is the current representation of the lambda expression. */
11626 static bool
11627 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
11629 /* 5.1.1.4 of the standard says:
11630 If a lambda-expression does not include a lambda-declarator, it is as if
11631 the lambda-declarator were ().
11632 This means an empty parameter list, no attributes, and no exception
11633 specification. */
11634 tree param_list = void_list_node;
11635 tree std_attrs = NULL_TREE;
11636 tree gnu_attrs = NULL_TREE;
11637 tree exception_spec = NULL_TREE;
11638 tree template_param_list = NULL_TREE;
11639 tree tx_qual = NULL_TREE;
11640 tree return_type = NULL_TREE;
11641 tree trailing_requires_clause = NULL_TREE;
11642 bool has_param_list = false;
11643 location_t omitted_parms_loc = UNKNOWN_LOCATION;
11644 cp_decl_specifier_seq lambda_specs;
11645 clear_decl_specs (&lambda_specs);
11646 /* A lambda op() is const unless explicitly 'mutable'. */
11647 cp_cv_quals quals = TYPE_QUAL_CONST;
11649 /* The template-parameter-list is optional, but must begin with
11650 an opening angle if present. */
11651 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
11653 if (cxx_dialect < cxx14)
11654 pedwarn (parser->lexer->next_token->location, OPT_Wc__14_extensions,
11655 "lambda templates are only available with "
11656 "%<-std=c++14%> or %<-std=gnu++14%>");
11657 else if (pedantic && cxx_dialect < cxx20)
11658 pedwarn (parser->lexer->next_token->location, OPT_Wc__20_extensions,
11659 "lambda templates are only available with "
11660 "%<-std=c++20%> or %<-std=gnu++20%>");
11662 cp_lexer_consume_token (parser->lexer);
11664 template_param_list = cp_parser_template_parameter_list (parser);
11665 cp_parser_require_end_of_template_parameter_list (parser);
11667 /* We may have a constrained generic lambda; parse the requires-clause
11668 immediately after the template-parameter-list and combine with any
11669 shorthand constraints present. */
11670 tree dreqs = cp_parser_requires_clause_opt (parser, true);
11671 if (flag_concepts)
11673 tree reqs = get_shorthand_constraints (current_template_parms);
11674 if (dreqs)
11675 reqs = combine_constraint_expressions (reqs, dreqs);
11676 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
11679 /* We just processed one more parameter list. */
11680 ++parser->num_template_parameter_lists;
11683 /* Committee discussion supports allowing attributes here. */
11684 lambda_specs.attributes = cp_parser_attributes_opt (parser);
11686 /* The parameter-declaration-clause is optional (unless
11687 template-parameter-list was given), but must begin with an
11688 opening parenthesis if present. */
11689 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
11691 matching_parens parens;
11692 parens.consume_open (parser);
11694 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
11696 /* Parse parameters. */
11697 param_list
11698 = cp_parser_parameter_declaration_clause
11699 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL);
11701 /* Default arguments shall not be specified in the
11702 parameter-declaration-clause of a lambda-declarator. */
11703 if (pedantic && cxx_dialect < cxx14)
11704 for (tree t = param_list; t; t = TREE_CHAIN (t))
11705 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
11706 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
11707 OPT_Wc__14_extensions,
11708 "default argument specified for lambda parameter");
11710 parens.require_close (parser);
11711 has_param_list = true;
11713 else if (cxx_dialect < cxx23)
11714 omitted_parms_loc = cp_lexer_peek_token (parser->lexer)->location;
11716 /* In the decl-specifier-seq of the lambda-declarator, each
11717 decl-specifier shall either be mutable or constexpr. */
11718 int declares_class_or_enum;
11719 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
11720 cp_parser_decl_specifier_seq (parser,
11721 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
11722 &lambda_specs, &declares_class_or_enum);
11724 if (omitted_parms_loc && lambda_specs.any_specifiers_p)
11726 pedwarn (omitted_parms_loc, OPT_Wc__23_extensions,
11727 "parameter declaration before lambda declaration "
11728 "specifiers only optional with %<-std=c++2b%> or "
11729 "%<-std=gnu++2b%>");
11730 omitted_parms_loc = UNKNOWN_LOCATION;
11733 if (lambda_specs.storage_class == sc_mutable)
11735 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
11736 quals = TYPE_UNQUALIFIED;
11738 else if (lambda_specs.storage_class == sc_static)
11740 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
11741 || LAMBDA_EXPR_CAPTURE_LIST (lambda_expr))
11742 error_at (lambda_specs.locations[ds_storage_class],
11743 "%<static%> lambda specifier with lambda capture");
11744 else
11746 LAMBDA_EXPR_STATIC_P (lambda_expr) = 1;
11747 quals = TYPE_UNQUALIFIED;
11751 tx_qual = cp_parser_tx_qualifier_opt (parser);
11752 if (omitted_parms_loc && tx_qual)
11754 pedwarn (omitted_parms_loc, OPT_Wc__23_extensions,
11755 "parameter declaration before lambda transaction "
11756 "qualifier only optional with %<-std=c++2b%> or "
11757 "%<-std=gnu++2b%>");
11758 omitted_parms_loc = UNKNOWN_LOCATION;
11761 /* Parse optional exception specification. */
11762 exception_spec
11763 = cp_parser_exception_specification_opt (parser, CP_PARSER_FLAGS_NONE);
11765 if (omitted_parms_loc && exception_spec)
11767 pedwarn (omitted_parms_loc, OPT_Wc__23_extensions,
11768 "parameter declaration before lambda exception "
11769 "specification only optional with %<-std=c++2b%> or "
11770 "%<-std=gnu++2b%>");
11771 omitted_parms_loc = UNKNOWN_LOCATION;
11774 /* GCC 8 accepted attributes here, and this is the place for standard C++11
11775 attributes that appertain to the function type. */
11776 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
11777 gnu_attrs = cp_parser_gnu_attributes_opt (parser);
11778 else
11779 std_attrs = cp_parser_std_attribute_spec_seq (parser);
11781 /* Parse optional trailing return type. */
11782 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
11784 if (omitted_parms_loc)
11785 pedwarn (omitted_parms_loc, OPT_Wc__23_extensions,
11786 "parameter declaration before lambda trailing "
11787 "return type only optional with %<-std=c++2b%> or "
11788 "%<-std=gnu++2b%>");
11789 cp_lexer_consume_token (parser->lexer);
11790 return_type = cp_parser_trailing_type_id (parser);
11793 /* Also allow GNU attributes at the very end of the declaration, the usual
11794 place for GNU attributes. */
11795 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
11796 gnu_attrs = chainon (gnu_attrs, cp_parser_gnu_attributes_opt (parser));
11798 if (has_param_list)
11800 /* Parse optional trailing requires clause. */
11801 trailing_requires_clause = cp_parser_requires_clause_opt (parser, false);
11803 /* The function parameters must be in scope all the way until after the
11804 trailing-return-type in case of decltype. */
11805 pop_bindings_and_leave_scope ();
11808 /* Create the function call operator.
11810 Messing with declarators like this is no uglier than building up the
11811 FUNCTION_DECL by hand, and this is less likely to get out of sync with
11812 other code. */
11814 cp_decl_specifier_seq return_type_specs;
11815 cp_declarator* declarator;
11816 tree fco;
11817 void *p;
11819 clear_decl_specs (&return_type_specs);
11820 return_type_specs.type = make_auto ();
11822 if (lambda_specs.locations[ds_constexpr])
11824 if (cxx_dialect >= cxx17)
11825 return_type_specs.locations[ds_constexpr]
11826 = lambda_specs.locations[ds_constexpr];
11827 else
11828 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
11829 "lambda only available with %<-std=c++17%> or "
11830 "%<-std=gnu++17%>");
11832 if (lambda_specs.locations[ds_consteval])
11833 return_type_specs.locations[ds_consteval]
11834 = lambda_specs.locations[ds_consteval];
11835 if (LAMBDA_EXPR_STATIC_P (lambda_expr))
11837 return_type_specs.storage_class = sc_static;
11838 return_type_specs.locations[ds_storage_class]
11839 = lambda_specs.locations[ds_storage_class];
11842 p = obstack_alloc (&declarator_obstack, 0);
11844 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none,
11845 LAMBDA_EXPR_LOCATION (lambda_expr));
11847 declarator = make_call_declarator (declarator, param_list, quals,
11848 VIRT_SPEC_UNSPECIFIED,
11849 REF_QUAL_NONE,
11850 tx_qual,
11851 exception_spec,
11852 return_type,
11853 trailing_requires_clause,
11854 std_attrs,
11855 UNKNOWN_LOCATION);
11857 fco = grokmethod (&return_type_specs,
11858 declarator,
11859 chainon (gnu_attrs, lambda_specs.attributes));
11860 if (fco != error_mark_node)
11862 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
11863 DECL_ARTIFICIAL (fco) = 1;
11864 if (!LAMBDA_EXPR_STATIC_P (lambda_expr))
11865 /* Give the object parameter a different name. */
11866 DECL_NAME (DECL_ARGUMENTS (fco)) = closure_identifier;
11867 DECL_SET_LAMBDA_FUNCTION (fco, true);
11869 if (template_param_list)
11871 fco = finish_member_template_decl (fco);
11872 finish_template_decl (template_param_list);
11873 --parser->num_template_parameter_lists;
11875 else if (parser->fully_implicit_function_template_p)
11876 fco = finish_fully_implicit_template (parser, fco);
11878 finish_member_declaration (fco);
11879 record_lambda_scope_sig_discriminator (lambda_expr, fco);
11881 obstack_free (&declarator_obstack, p);
11883 return (fco != error_mark_node);
11887 /* Parse the body of a lambda expression, which is simply
11889 compound-statement
11891 but which requires special handling.
11892 LAMBDA_EXPR is the current representation of the lambda expression. */
11894 static void
11895 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
11897 bool nested = (current_function_decl != NULL_TREE);
11898 unsigned char local_variables_forbidden_p
11899 = parser->local_variables_forbidden_p;
11900 bool in_function_body = parser->in_function_body;
11902 /* The body of a lambda-expression is not a subexpression of the enclosing
11903 expression. */
11904 cp_evaluated ev;
11906 if (nested)
11907 push_function_context ();
11908 else
11909 /* Still increment function_depth so that we don't GC in the
11910 middle of an expression. */
11911 ++function_depth;
11913 auto odsd = make_temp_override (parser->omp_declare_simd, NULL);
11914 auto ord = make_temp_override (parser->oacc_routine, NULL);
11915 auto oafp = make_temp_override (parser->omp_attrs_forbidden_p, false);
11916 vec<tree> omp_privatization_save;
11917 save_omp_privatization_clauses (omp_privatization_save);
11918 /* Clear this in case we're in the middle of a default argument. */
11919 parser->local_variables_forbidden_p = 0;
11920 parser->in_function_body = true;
11923 local_specialization_stack s (lss_copy);
11924 tree fco = lambda_function (lambda_expr);
11925 tree body = start_lambda_function (fco, lambda_expr);
11927 /* Originally C++11 required us to peek for 'return expr'; and
11928 process it specially here to deduce the return type. N3638
11929 removed the need for that. */
11930 cp_parser_function_body (parser, false);
11932 finish_lambda_function (body);
11935 restore_omp_privatization_clauses (omp_privatization_save);
11936 parser->local_variables_forbidden_p = local_variables_forbidden_p;
11937 parser->in_function_body = in_function_body;
11938 if (nested)
11939 pop_function_context();
11940 else
11941 --function_depth;
11944 /* Statements [gram.stmt.stmt] */
11946 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
11948 static void
11949 add_debug_begin_stmt (location_t loc)
11951 if (!MAY_HAVE_DEBUG_MARKER_STMTS)
11952 return;
11953 if (DECL_DECLARED_CONCEPT_P (current_function_decl))
11954 /* A concept is never expanded normally. */
11955 return;
11957 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
11958 SET_EXPR_LOCATION (stmt, loc);
11959 add_stmt (stmt);
11962 struct cp_omp_attribute_data
11964 cp_token_cache *tokens;
11965 const c_omp_directive *dir;
11966 c_omp_directive_kind kind;
11969 /* Handle omp::directive and omp::sequence attributes in ATTRS
11970 (if any) at the start of a statement or in attribute-declaration. */
11972 static tree
11973 cp_parser_handle_statement_omp_attributes (cp_parser *parser, tree attrs)
11975 if (!flag_openmp && !flag_openmp_simd)
11976 return attrs;
11978 auto_vec<cp_omp_attribute_data, 16> vec;
11979 int cnt = 0;
11980 int tokens = 0;
11981 bool bad = false;
11982 for (tree *pa = &attrs; *pa; )
11983 if (get_attribute_namespace (*pa) == omp_identifier
11984 && is_attribute_p ("directive", get_attribute_name (*pa)))
11986 cnt++;
11987 for (tree a = TREE_VALUE (*pa); a; a = TREE_CHAIN (a))
11989 tree d = TREE_VALUE (a);
11990 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
11991 cp_token *first = DEFPARSE_TOKENS (d)->first;
11992 cp_token *last = DEFPARSE_TOKENS (d)->last;
11993 if (parser->omp_attrs_forbidden_p)
11995 error_at (first->location,
11996 "mixing OpenMP directives with attribute and pragma "
11997 "syntax on the same statement");
11998 parser->omp_attrs_forbidden_p = false;
11999 bad = true;
12001 const char *directive[3] = {};
12002 for (int i = 0; i < 3; i++)
12004 tree id = NULL_TREE;
12005 if (first + i == last)
12006 break;
12007 if (first[i].type == CPP_NAME)
12008 id = first[i].u.value;
12009 else if (first[i].type == CPP_KEYWORD)
12010 id = ridpointers[(int) first[i].keyword];
12011 else
12012 break;
12013 directive[i] = IDENTIFIER_POINTER (id);
12015 const c_omp_directive *dir = NULL;
12016 if (directive[0])
12017 dir = c_omp_categorize_directive (directive[0], directive[1],
12018 directive[2]);
12019 if (dir == NULL)
12021 error_at (first->location,
12022 "unknown OpenMP directive name in %<omp::directive%>"
12023 " attribute argument");
12024 continue;
12026 c_omp_directive_kind kind = dir->kind;
12027 if (dir->id == PRAGMA_OMP_ORDERED)
12029 /* ordered is C_OMP_DIR_CONSTRUCT only if it doesn't contain
12030 depend/doacross clause. */
12031 if (directive[1]
12032 && (strcmp (directive[1], "depend") == 0
12033 || strcmp (directive[1], "doacross") == 0))
12034 kind = C_OMP_DIR_STANDALONE;
12035 else if (first + 2 < last
12036 && first[1].type == CPP_COMMA
12037 && first[2].type == CPP_NAME
12038 && (strcmp (IDENTIFIER_POINTER (first[2].u.value),
12039 "depend") == 0
12040 || strcmp (IDENTIFIER_POINTER (first[2].u.value),
12041 "doacross") == 0))
12042 kind = C_OMP_DIR_STANDALONE;
12044 else if (dir->id == PRAGMA_OMP_ERROR)
12046 /* error with at(execution) clause is C_OMP_DIR_STANDALONE. */
12047 int paren_depth = 0;
12048 for (int i = 1; first + i < last; i++)
12049 if (first[i].type == CPP_OPEN_PAREN)
12050 paren_depth++;
12051 else if (first[i].type == CPP_CLOSE_PAREN)
12052 paren_depth--;
12053 else if (paren_depth == 0
12054 && first + i + 2 < last
12055 && first[i].type == CPP_NAME
12056 && first[i + 1].type == CPP_OPEN_PAREN
12057 && first[i + 2].type == CPP_NAME
12058 && !strcmp (IDENTIFIER_POINTER (first[i].u.value),
12059 "at")
12060 && !strcmp (IDENTIFIER_POINTER (first[i
12061 + 2].u.value),
12062 "execution"))
12064 kind = C_OMP_DIR_STANDALONE;
12065 break;
12068 cp_omp_attribute_data v = { DEFPARSE_TOKENS (d), dir, kind };
12069 vec.safe_push (v);
12070 if (flag_openmp || dir->simd)
12071 tokens += (last - first) + 1;
12073 cp_omp_attribute_data v = {};
12074 vec.safe_push (v);
12075 *pa = TREE_CHAIN (*pa);
12077 else
12078 pa = &TREE_CHAIN (*pa);
12080 if (bad)
12081 return attrs;
12083 unsigned int i;
12084 cp_omp_attribute_data *v;
12085 cp_omp_attribute_data *construct_seen = nullptr;
12086 cp_omp_attribute_data *standalone_seen = nullptr;
12087 cp_omp_attribute_data *prev_standalone_seen = nullptr;
12088 FOR_EACH_VEC_ELT (vec, i, v)
12089 if (v->tokens)
12091 if (v->kind == C_OMP_DIR_CONSTRUCT && !construct_seen)
12092 construct_seen = v;
12093 else if (v->kind == C_OMP_DIR_STANDALONE && !standalone_seen)
12094 standalone_seen = v;
12096 else
12098 if (standalone_seen && !prev_standalone_seen)
12100 prev_standalone_seen = standalone_seen;
12101 standalone_seen = nullptr;
12105 if (cnt > 1 && construct_seen)
12107 error_at (construct_seen->tokens->first->location,
12108 "OpenMP construct among %<omp::directive%> attributes"
12109 " requires all %<omp::directive%> attributes on the"
12110 " same statement to be in the same %<omp::sequence%>");
12111 return attrs;
12113 if (cnt > 1 && standalone_seen && prev_standalone_seen)
12115 error_at (standalone_seen->tokens->first->location,
12116 "multiple OpenMP standalone directives among"
12117 " %<omp::directive%> attributes must be all within the"
12118 " same %<omp::sequence%>");
12119 return attrs;
12122 if (prev_standalone_seen)
12123 standalone_seen = prev_standalone_seen;
12124 if (standalone_seen
12125 && !cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12127 error_at (standalone_seen->tokens->first->location,
12128 "standalone OpenMP directives in %<omp::directive%> attribute"
12129 " can only appear on an empty statement");
12130 return attrs;
12132 if (cnt && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
12134 cp_token *token = cp_lexer_peek_token (parser->lexer);
12135 enum pragma_kind kind = cp_parser_pragma_kind (token);
12136 if (kind >= PRAGMA_OMP__START_ && kind <= PRAGMA_OMP__LAST_)
12138 error_at (token->location,
12139 "mixing OpenMP directives with attribute and pragma "
12140 "syntax on the same statement");
12141 return attrs;
12145 if (!tokens)
12146 return attrs;
12147 tokens++;
12148 cp_lexer *lexer = cp_lexer_alloc ();
12149 lexer->debugging_p = parser->lexer->debugging_p;
12150 vec_safe_reserve (lexer->buffer, tokens, true);
12151 FOR_EACH_VEC_ELT (vec, i, v)
12153 if (!v->tokens)
12154 continue;
12155 if (!flag_openmp && !v->dir->simd)
12156 continue;
12157 cp_token *first = v->tokens->first;
12158 cp_token *last = v->tokens->last;
12159 cp_token tok = {};
12160 tok.type = CPP_PRAGMA;
12161 tok.keyword = RID_MAX;
12162 tok.u.value = build_int_cst (NULL, v->dir->id);
12163 tok.location = first->location;
12164 lexer->buffer->quick_push (tok);
12165 while (++first < last)
12166 lexer->buffer->quick_push (*first);
12167 tok = {};
12168 tok.type = CPP_PRAGMA_EOL;
12169 tok.keyword = RID_MAX;
12170 tok.location = last->location;
12171 lexer->buffer->quick_push (tok);
12173 cp_token tok = {};
12174 tok.type = CPP_EOF;
12175 tok.keyword = RID_MAX;
12176 tok.location = lexer->buffer->last ().location;
12177 lexer->buffer->quick_push (tok);
12178 lexer->next = parser->lexer;
12179 lexer->next_token = lexer->buffer->address ();
12180 lexer->last_token = lexer->next_token
12181 + lexer->buffer->length ()
12182 - 1;
12183 lexer->in_omp_attribute_pragma = true;
12184 parser->lexer = lexer;
12185 /* Move the current source position to that of the first token in the
12186 new lexer. */
12187 cp_lexer_set_source_position_from_token (lexer->next_token);
12188 return attrs;
12191 /* True if and only if the name is one of the contract types. */
12193 static bool
12194 contract_attribute_p (const_tree id)
12196 return is_attribute_p ("assert", id)
12197 || is_attribute_p ("pre", id)
12198 || is_attribute_p ("post", id);
12201 /* Handle omp::directive and omp::sequence attributes in *PATTRS
12202 (if any) at the start or after declaration-id of a declaration. */
12204 static void
12205 cp_parser_handle_directive_omp_attributes (cp_parser *parser, tree *pattrs,
12206 cp_omp_declare_simd_data *data,
12207 bool start)
12209 if (!flag_openmp && !flag_openmp_simd)
12210 return;
12212 int cnt = 0;
12213 bool bad = false;
12214 bool variant_p = false;
12215 location_t loc = UNKNOWN_LOCATION;
12216 for (tree pa = *pattrs; pa; pa = TREE_CHAIN (pa))
12217 if (get_attribute_namespace (pa) == omp_identifier
12218 && is_attribute_p ("directive", get_attribute_name (pa)))
12220 for (tree a = TREE_VALUE (pa); a; a = TREE_CHAIN (a))
12222 tree d = TREE_VALUE (a);
12223 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
12224 cp_token *first = DEFPARSE_TOKENS (d)->first;
12225 cp_token *last = DEFPARSE_TOKENS (d)->last;
12226 const char *directive[3] = {};
12227 for (int i = 0; i < 3; i++)
12229 tree id = NULL_TREE;
12230 if (first + i == last)
12231 break;
12232 if (first[i].type == CPP_NAME)
12233 id = first[i].u.value;
12234 else if (first[i].type == CPP_KEYWORD)
12235 id = ridpointers[(int) first[i].keyword];
12236 else
12237 break;
12238 directive[i] = IDENTIFIER_POINTER (id);
12240 const c_omp_directive *dir = NULL;
12241 if (directive[0])
12242 dir = c_omp_categorize_directive (directive[0], directive[1],
12243 directive[2]);
12244 if (dir == NULL)
12245 continue;
12246 if (dir->id == PRAGMA_OMP_DECLARE
12247 && (strcmp (directive[1], "simd") == 0
12248 || strcmp (directive[1], "variant") == 0))
12250 if (cnt++ == 0)
12252 variant_p = strcmp (directive[1], "variant") == 0;
12253 loc = first->location;
12255 if (start && parser->omp_declare_simd && !bad)
12257 error_at (first->location,
12258 "mixing OpenMP directives with attribute and "
12259 "pragma syntax on the same declaration");
12260 bad = true;
12266 if (bad)
12268 for (tree *pa = pattrs; *pa; )
12269 if (get_attribute_namespace (*pa) == omp_identifier
12270 && is_attribute_p ("directive", get_attribute_name (*pa)))
12271 *pa = TREE_CHAIN (*pa);
12272 else
12273 pa = &TREE_CHAIN (*pa);
12274 return;
12276 if (cnt == 0)
12277 return;
12279 if (parser->omp_declare_simd == NULL)
12281 data->error_seen = false;
12282 data->fndecl_seen = false;
12283 data->variant_p = variant_p;
12284 data->loc = loc;
12285 data->tokens = vNULL;
12286 data->attribs[0] = NULL;
12287 data->attribs[1] = NULL;
12288 parser->omp_declare_simd = data;
12290 parser->omp_declare_simd->attribs[!start] = pattrs;
12293 /* Parse a statement.
12295 statement:
12296 labeled-statement
12297 expression-statement
12298 compound-statement
12299 selection-statement
12300 iteration-statement
12301 jump-statement
12302 declaration-statement
12303 try-block
12305 C++11:
12307 statement:
12308 labeled-statement
12309 attribute-specifier-seq (opt) expression-statement
12310 attribute-specifier-seq (opt) compound-statement
12311 attribute-specifier-seq (opt) selection-statement
12312 attribute-specifier-seq (opt) iteration-statement
12313 attribute-specifier-seq (opt) jump-statement
12314 declaration-statement
12315 attribute-specifier-seq (opt) try-block
12317 init-statement:
12318 expression-statement
12319 simple-declaration
12320 alias-declaration
12322 TM Extension:
12324 statement:
12325 atomic-statement
12327 IN_COMPOUND is true when the statement is nested inside a
12328 cp_parser_compound_statement.
12330 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12331 is a (possibly labeled) if statement which is not enclosed in braces
12332 and has an else clause. This is used to implement -Wparentheses.
12334 CHAIN is a vector of if-else-if conditions.
12336 Note that this version of parsing restricts assertions to be attached to
12337 empty statements. */
12339 static void
12340 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
12341 const bool in_compound, bool *if_p, vec<tree> *chain,
12342 location_t *loc_after_labels)
12344 tree statement, std_attrs = NULL_TREE;
12345 cp_token *token;
12346 location_t statement_location, attrs_loc;
12347 bool in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
12348 bool has_std_attrs;
12349 /* A copy of IN_COMPOUND which is set to false after seeing a label.
12350 This matters for certain pragmas. */
12351 bool in_compound_for_pragma = in_compound;
12353 restart:
12354 if (if_p != NULL)
12355 *if_p = false;
12356 /* There is no statement yet. */
12357 statement = NULL_TREE;
12359 saved_token_sentinel saved_tokens (parser->lexer);
12360 token = cp_lexer_peek_token (parser->lexer);
12361 attrs_loc = token->location;
12362 if (c_dialect_objc ())
12363 /* In obj-c++, seeing '[[' might be the either the beginning of
12364 c++11 attributes, or a nested objc-message-expression. So
12365 let's parse the c++11 attributes tentatively. */
12366 cp_parser_parse_tentatively (parser);
12367 std_attrs = cp_parser_std_attribute_spec_seq (parser);
12368 if (std_attrs)
12369 attrs_loc = make_location (attrs_loc, attrs_loc, parser->lexer);
12370 if (c_dialect_objc ())
12372 if (!cp_parser_parse_definitely (parser))
12373 std_attrs = NULL_TREE;
12375 has_std_attrs = cp_lexer_peek_token (parser->lexer) != token;
12377 /* Peek at the next token. */
12378 token = cp_lexer_peek_token (parser->lexer);
12380 /* If we have contracts, check that they're valid in this context. */
12381 if (std_attrs != error_mark_node)
12383 if (tree pre = lookup_attribute ("pre", std_attrs))
12384 error_at (EXPR_LOCATION (TREE_VALUE (pre)),
12385 "preconditions cannot be statements");
12386 else if (tree post = lookup_attribute ("post", std_attrs))
12387 error_at (EXPR_LOCATION (TREE_VALUE (post)),
12388 "postconditions cannot be statements");
12390 /* Check that assertions are null statements. */
12391 if (cp_contract_assertion_p (std_attrs))
12392 if (token->type != CPP_SEMICOLON)
12393 error_at (token->location, "assertions must be followed by %<;%>");
12396 bool omp_attrs_forbidden_p;
12397 omp_attrs_forbidden_p = parser->omp_attrs_forbidden_p;
12399 if (std_attrs && (flag_openmp || flag_openmp_simd))
12401 bool handle_omp_attribs = false;
12402 if (token->type == CPP_KEYWORD)
12403 switch (token->keyword)
12405 case RID_IF:
12406 case RID_SWITCH:
12407 case RID_WHILE:
12408 case RID_DO:
12409 case RID_FOR:
12410 case RID_BREAK:
12411 case RID_CONTINUE:
12412 case RID_RETURN:
12413 case RID_CO_RETURN:
12414 case RID_GOTO:
12415 case RID_AT_TRY:
12416 case RID_AT_CATCH:
12417 case RID_AT_FINALLY:
12418 case RID_AT_SYNCHRONIZED:
12419 case RID_AT_THROW:
12420 case RID_TRY:
12421 case RID_TRANSACTION_ATOMIC:
12422 case RID_TRANSACTION_RELAXED:
12423 case RID_SYNCHRONIZED:
12424 case RID_ATOMIC_NOEXCEPT:
12425 case RID_ATOMIC_CANCEL:
12426 case RID_TRANSACTION_CANCEL:
12427 handle_omp_attribs = true;
12428 break;
12429 default:
12430 break;
12432 else if (token->type == CPP_SEMICOLON
12433 || token->type == CPP_OPEN_BRACE
12434 || token->type == CPP_PRAGMA)
12435 handle_omp_attribs = true;
12436 if (handle_omp_attribs)
12438 std_attrs = cp_parser_handle_statement_omp_attributes (parser,
12439 std_attrs);
12440 token = cp_lexer_peek_token (parser->lexer);
12443 parser->omp_attrs_forbidden_p = false;
12445 /* Remember the location of the first token in the statement. */
12446 cp_token *statement_token = token;
12447 statement_location = token->location;
12448 add_debug_begin_stmt (statement_location);
12449 /* If this is a keyword, then that will often determine what kind of
12450 statement we have. */
12451 if (token->type == CPP_KEYWORD)
12453 enum rid keyword = token->keyword;
12455 switch (keyword)
12457 case RID_CASE:
12458 case RID_DEFAULT:
12459 /* Looks like a labeled-statement with a case label.
12460 Parse the label, and then use tail recursion to parse
12461 the statement. */
12462 cp_parser_label_for_labeled_statement (parser, std_attrs);
12463 in_compound_for_pragma = false;
12464 in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
12465 goto restart;
12467 case RID_IF:
12468 case RID_SWITCH:
12469 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12470 statement = cp_parser_selection_statement (parser, if_p, chain);
12471 break;
12473 case RID_WHILE:
12474 case RID_DO:
12475 case RID_FOR:
12476 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12477 statement = cp_parser_iteration_statement (parser, if_p, false, 0,
12478 false);
12479 break;
12481 case RID_BREAK:
12482 case RID_CONTINUE:
12483 case RID_RETURN:
12484 case RID_CO_RETURN:
12485 case RID_GOTO:
12486 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12487 statement = cp_parser_jump_statement (parser);
12488 break;
12490 /* Objective-C++ exception-handling constructs. */
12491 case RID_AT_TRY:
12492 case RID_AT_CATCH:
12493 case RID_AT_FINALLY:
12494 case RID_AT_SYNCHRONIZED:
12495 case RID_AT_THROW:
12496 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12497 statement = cp_parser_objc_statement (parser);
12498 break;
12500 case RID_TRY:
12501 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12502 statement = cp_parser_try_block (parser);
12503 break;
12505 case RID_NAMESPACE:
12506 /* This must be a namespace alias definition. */
12507 if (has_std_attrs)
12509 /* Attributes should be parsed as part of the
12510 declaration, so let's un-parse them. */
12511 saved_tokens.rollback();
12512 std_attrs = NULL_TREE;
12514 cp_parser_declaration_statement (parser);
12515 return;
12517 case RID_TRANSACTION_ATOMIC:
12518 case RID_TRANSACTION_RELAXED:
12519 case RID_SYNCHRONIZED:
12520 case RID_ATOMIC_NOEXCEPT:
12521 case RID_ATOMIC_CANCEL:
12522 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12523 statement = cp_parser_transaction (parser, token);
12524 break;
12525 case RID_TRANSACTION_CANCEL:
12526 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12527 statement = cp_parser_transaction_cancel (parser);
12528 break;
12530 default:
12531 /* It might be a keyword like `int' that can start a
12532 declaration-statement. */
12533 break;
12536 else if (token->type == CPP_NAME)
12538 /* If the next token is a `:', then we are looking at a
12539 labeled-statement. */
12540 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12541 if (token->type == CPP_COLON)
12543 /* Looks like a labeled-statement with an ordinary label.
12544 Parse the label, and then use tail recursion to parse
12545 the statement. */
12547 cp_parser_label_for_labeled_statement (parser, std_attrs);
12549 /* If there's no statement, it's not a labeled-statement, just
12550 a label. That's allowed in C++23, but only if we're at the
12551 end of a compound-statement. */
12552 if (in_compound
12553 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12555 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
12556 if (cxx_dialect < cxx23)
12557 pedwarn (loc, OPT_Wc__23_extensions,
12558 "label at end of compound statement only available "
12559 "with %<-std=c++2b%> or %<-std=gnu++2b%>");
12560 return;
12562 in_compound_for_pragma = false;
12563 in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
12564 goto restart;
12567 /* Anything that starts with a `{' must be a compound-statement. */
12568 else if (token->type == CPP_OPEN_BRACE)
12570 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12571 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12573 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
12574 a statement all its own. */
12575 else if (token->type == CPP_PRAGMA)
12577 do_pragma:;
12578 cp_lexer *lexer = parser->lexer;
12579 bool do_restart = false;
12580 /* Only certain OpenMP pragmas are attached to statements, and thus
12581 are considered statements themselves. All others are not. In
12582 the context of a compound, accept the pragma as a "statement" and
12583 return so that we can check for a close brace. Otherwise we
12584 require a real statement and must go back and read one. */
12585 if (in_compound_for_pragma)
12587 if (cp_parser_pragma (parser, pragma_compound, if_p)
12588 && parser->omp_for_parse_state)
12589 check_omp_intervening_code (parser);
12591 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
12592 do_restart = true;
12593 else if (parser->omp_for_parse_state)
12594 check_omp_intervening_code (parser);
12595 if (parser->lexer != lexer
12596 && lexer->in_omp_attribute_pragma
12597 && (!in_omp_attribute_pragma || lexer->orphan_p))
12599 if (saved_tokens.lexer == lexer)
12601 if (saved_tokens.mode == STS_COMMIT)
12602 cp_lexer_commit_tokens (lexer);
12603 gcc_assert (lexer->saved_tokens.length () == saved_tokens.len);
12604 saved_tokens.lexer = parser->lexer;
12605 saved_tokens.mode = STS_DONOTHING;
12606 saved_tokens.len = parser->lexer->saved_tokens.length ();
12608 cp_lexer_destroy (lexer);
12609 lexer = parser->lexer;
12611 if (do_restart)
12612 goto restart;
12613 if (parser->lexer == lexer
12614 && lexer->in_omp_attribute_pragma
12615 && !in_omp_attribute_pragma)
12616 parser->lexer->orphan_p = true;
12617 return;
12619 else if (token->type == CPP_EOF)
12621 cp_parser_error (parser, "expected statement");
12622 return;
12625 /* Everything else must be a declaration-statement or an
12626 expression-statement. Try for the declaration-statement
12627 first, unless we are looking at a `;', in which case we know that
12628 we have an expression-statement. */
12629 if (!statement)
12631 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12633 if (has_std_attrs)
12634 /* Attributes should be parsed as part of the declaration,
12635 so let's un-parse them. */
12636 saved_tokens.rollback();
12638 parser->omp_attrs_forbidden_p = omp_attrs_forbidden_p;
12639 cp_parser_parse_tentatively (parser);
12640 /* Try to parse the declaration-statement. */
12641 cp_parser_declaration_statement (parser);
12642 parser->omp_attrs_forbidden_p = false;
12643 /* If that worked, we're done. */
12644 if (cp_parser_parse_definitely (parser))
12645 return;
12646 /* It didn't work, restore the post-attribute position. */
12647 if (has_std_attrs)
12649 cp_lexer_set_token_position (parser->lexer, statement_token);
12650 if (flag_openmp || flag_openmp_simd)
12652 size_t i = 1;
12653 bool handle_omp_attribs = true;
12654 while (cp_lexer_peek_nth_token (parser->lexer, i)->keyword
12655 == RID_EXTENSION)
12656 i++;
12657 switch (cp_lexer_peek_nth_token (parser->lexer, i)->keyword)
12659 case RID_ASM:
12660 case RID_NAMESPACE:
12661 case RID_USING:
12662 case RID_LABEL:
12663 case RID_STATIC_ASSERT:
12664 /* Don't handle OpenMP attribs on keywords that
12665 always start a declaration statement but don't
12666 accept attribute before it and therefore
12667 the tentative cp_parser_declaration_statement
12668 fails to parse because of that. */
12669 handle_omp_attribs = false;
12670 break;
12671 default:
12672 break;
12675 if (handle_omp_attribs)
12677 parser->omp_attrs_forbidden_p = omp_attrs_forbidden_p;
12678 std_attrs
12679 = cp_parser_handle_statement_omp_attributes
12680 (parser, std_attrs);
12681 parser->omp_attrs_forbidden_p = false;
12682 token = cp_lexer_peek_token (parser->lexer);
12683 if (token->type == CPP_PRAGMA)
12684 goto do_pragma;
12689 /* All preceding labels have been parsed at this point. */
12690 if (loc_after_labels != NULL)
12691 *loc_after_labels = statement_location;
12693 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12695 /* Look for an expression-statement instead. */
12696 statement = cp_parser_expression_statement (parser, in_statement_expr);
12698 std_attrs = process_stmt_assume_attribute (std_attrs, statement,
12699 attrs_loc);
12701 /* Handle [[fallthrough]];. */
12702 if (attribute_fallthrough_p (std_attrs))
12704 /* The next token after the fallthrough attribute is ';'. */
12705 if (statement == NULL_TREE)
12707 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
12708 statement = build_call_expr_internal_loc (statement_location,
12709 IFN_FALLTHROUGH,
12710 void_type_node, 0);
12711 finish_expr_stmt (statement);
12713 else
12714 warning_at (statement_location, OPT_Wattributes,
12715 "%<fallthrough%> attribute not followed by %<;%>");
12716 std_attrs = NULL_TREE;
12719 /* Handle [[assert: ...]]; */
12720 if (cp_contract_assertion_p (std_attrs))
12722 /* Add the assertion as a statement in the current block. */
12723 gcc_assert (!statement || statement == error_mark_node);
12724 emit_assertion (std_attrs);
12725 std_attrs = NULL_TREE;
12729 /* Set the line number for the statement. */
12730 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
12731 SET_EXPR_LOCATION (statement, statement_location);
12733 /* Allow "[[fallthrough]];" or "[[assume(cond)]];", but warn otherwise. */
12734 if (std_attrs != NULL_TREE)
12735 warning_at (attrs_loc,
12736 OPT_Wattributes,
12737 "attributes at the beginning of statement are ignored");
12740 /* Append ATTR to attribute list ATTRS. */
12742 tree
12743 attr_chainon (tree attrs, tree attr)
12745 if (attrs == error_mark_node)
12746 return error_mark_node;
12747 if (attr == error_mark_node)
12748 return error_mark_node;
12749 return chainon (attrs, attr);
12752 /* Parse the label for a labeled-statement, i.e.
12754 label:
12755 attribute-specifier-seq[opt] identifier :
12756 attribute-specifier-seq[opt] case constant-expression :
12757 attribute-specifier-seq[opt] default :
12759 labeled-statement:
12760 label statement
12762 GNU Extension:
12763 case constant-expression ... constant-expression : statement
12765 When a label is parsed without errors, the label is added to the
12766 parse tree by the finish_* functions, so this function doesn't
12767 have to return the label. */
12769 static void
12770 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
12772 cp_token *token;
12773 tree label = NULL_TREE;
12774 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12776 /* The next token should be an identifier. */
12777 token = cp_lexer_peek_token (parser->lexer);
12778 if (token->type != CPP_NAME
12779 && token->type != CPP_KEYWORD)
12781 cp_parser_error (parser, "expected labeled-statement");
12782 return;
12785 /* Remember whether this case or a user-defined label is allowed to fall
12786 through to. */
12787 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
12789 parser->colon_corrects_to_scope_p = false;
12790 switch (token->keyword)
12792 case RID_CASE:
12794 tree expr, expr_hi;
12795 cp_token *ellipsis;
12797 /* Consume the `case' token. */
12798 cp_lexer_consume_token (parser->lexer);
12799 /* Parse the constant-expression. */
12800 expr = cp_parser_constant_expression (parser);
12801 if (check_for_bare_parameter_packs (expr))
12802 expr = error_mark_node;
12804 ellipsis = cp_lexer_peek_token (parser->lexer);
12805 if (ellipsis->type == CPP_ELLIPSIS)
12807 /* Consume the `...' token. */
12808 cp_lexer_consume_token (parser->lexer);
12809 expr_hi = cp_parser_constant_expression (parser);
12810 if (check_for_bare_parameter_packs (expr_hi))
12811 expr_hi = error_mark_node;
12813 /* We don't need to emit warnings here, as the common code
12814 will do this for us. */
12816 else
12817 expr_hi = NULL_TREE;
12819 if (parser->in_switch_statement_p)
12821 tree l = finish_case_label (token->location, expr, expr_hi);
12822 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
12824 label = CASE_LABEL (l);
12825 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
12828 else
12829 error_at (token->location,
12830 "case label %qE not within a switch statement",
12831 expr);
12833 break;
12835 case RID_DEFAULT:
12836 /* Consume the `default' token. */
12837 cp_lexer_consume_token (parser->lexer);
12839 if (parser->in_switch_statement_p)
12841 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
12842 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
12844 label = CASE_LABEL (l);
12845 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
12848 else
12849 error_at (token->location, "case label not within a switch statement");
12850 break;
12852 default:
12853 /* Anything else must be an ordinary label. */
12854 label = finish_label_stmt (cp_parser_identifier (parser));
12855 if (label && TREE_CODE (label) == LABEL_DECL)
12856 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
12857 break;
12860 /* Require the `:' token. */
12861 cp_parser_require (parser, CPP_COLON, RT_COLON);
12863 /* An ordinary label may optionally be followed by attributes.
12864 However, this is only permitted if the attributes are then
12865 followed by a semicolon. This is because, for backward
12866 compatibility, when parsing
12867 lab: __attribute__ ((unused)) int i;
12868 we want the attribute to attach to "i", not "lab". */
12869 if (label != NULL_TREE
12870 && cp_next_tokens_can_be_gnu_attribute_p (parser))
12872 tree attrs;
12873 cp_parser_parse_tentatively (parser);
12874 attrs = cp_parser_gnu_attributes_opt (parser);
12875 if (attrs == NULL_TREE
12876 /* And fallthrough always binds to the expression-statement. */
12877 || attribute_fallthrough_p (attrs)
12878 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12879 cp_parser_abort_tentative_parse (parser);
12880 else if (!cp_parser_parse_definitely (parser))
12882 else
12883 attributes = attr_chainon (attributes, attrs);
12886 if (attributes != NULL_TREE)
12887 cplus_decl_attributes (&label, attributes, 0);
12889 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12892 /* Parse an expression-statement.
12894 expression-statement:
12895 expression [opt] ;
12897 Returns the new EXPR_STMT -- or NULL_TREE if the expression
12898 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
12899 indicates whether this expression-statement is part of an
12900 expression statement. */
12902 static tree
12903 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
12905 tree statement = NULL_TREE;
12906 cp_token *token = cp_lexer_peek_token (parser->lexer);
12907 location_t loc = token->location;
12909 /* There might be attribute fallthrough. */
12910 tree attr = cp_parser_gnu_attributes_opt (parser);
12912 /* If the next token is a ';', then there is no expression
12913 statement. */
12914 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12916 statement = cp_parser_expression (parser);
12917 if (statement == error_mark_node
12918 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
12920 cp_parser_skip_to_end_of_block_or_statement (parser);
12921 return error_mark_node;
12925 attr = process_stmt_assume_attribute (attr, statement, loc);
12927 /* Handle [[fallthrough]];. */
12928 if (attribute_fallthrough_p (attr))
12930 /* The next token after the fallthrough attribute is ';'. */
12931 if (statement == NULL_TREE)
12932 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
12933 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
12934 void_type_node, 0);
12935 else
12936 warning_at (loc, OPT_Wattributes,
12937 "%<fallthrough%> attribute not followed by %<;%>");
12938 attr = NULL_TREE;
12941 /* Allow "[[fallthrough]];", but warn otherwise. */
12942 if (attr != NULL_TREE)
12943 warning_at (loc, OPT_Wattributes,
12944 "attributes at the beginning of statement are ignored");
12946 /* Give a helpful message for "A<T>::type t;" and the like. */
12947 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
12948 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
12950 if (TREE_CODE (statement) == SCOPE_REF)
12951 error_at (token->location, "need %<typename%> before %qE because "
12952 "%qT is a dependent scope",
12953 statement, TREE_OPERAND (statement, 0));
12954 else if (is_overloaded_fn (statement)
12955 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
12957 /* A::A a; */
12958 tree fn = get_first_fn (statement);
12959 error_at (token->location,
12960 "%<%T::%D%> names the constructor, not the type",
12961 DECL_CONTEXT (fn), DECL_NAME (fn));
12965 /* Consume the final `;'. */
12966 cp_parser_consume_semicolon_at_end_of_statement (parser);
12968 if (in_statement_expr
12969 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12970 /* This is the final expression statement of a statement
12971 expression. */
12972 statement = finish_stmt_expr_expr (statement, in_statement_expr);
12973 else if (statement)
12974 statement = finish_expr_stmt (statement);
12976 return statement;
12979 /* Parse a compound-statement.
12981 compound-statement:
12982 { statement-seq [opt] label-seq [opt] }
12984 label-seq:
12985 label
12986 label-seq label
12988 GNU extension:
12990 compound-statement:
12991 { label-declaration-seq [opt] statement-seq [opt] }
12993 label-declaration-seq:
12994 label-declaration
12995 label-declaration-seq label-declaration
12997 Returns a tree representing the statement. */
12999 static tree
13000 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
13001 int bcs_flags, bool function_body)
13003 tree compound_stmt;
13004 matching_braces braces;
13006 /* Consume the `{'. */
13007 if (!braces.require_open (parser))
13008 return error_mark_node;
13009 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
13010 && !function_body && cxx_dialect < cxx14)
13011 pedwarn (input_location, OPT_Wpedantic,
13012 "compound-statement in %<constexpr%> function");
13013 /* Begin the compound-statement. */
13014 compound_stmt = begin_compound_stmt (bcs_flags);
13015 /* If the next keyword is `__label__' we have a label declaration. */
13016 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
13017 cp_parser_label_declaration (parser);
13018 /* Parse an (optional) statement-seq. */
13019 cp_parser_statement_seq_opt (parser, in_statement_expr);
13021 /* Consume the `}'. */
13022 braces.require_close (parser);
13024 /* Finish the compound-statement. */
13025 finish_compound_stmt (compound_stmt);
13027 return compound_stmt;
13030 /* Diagnose errors related to imperfectly nested loops in an OMP
13031 loop construct. This function is called when such code is seen.
13032 Only issue one such diagnostic no matter how much invalid
13033 intervening code there is in the loop.
13034 FIXME: maybe the location associated with the diagnostic should
13035 be the current parser token instead of the location of the outer loop
13036 nest. */
13038 static void
13039 check_omp_intervening_code (cp_parser *parser)
13041 struct omp_for_parse_data *omp_for_parse_state
13042 = parser->omp_for_parse_state;
13043 gcc_assert (omp_for_parse_state);
13045 if (!omp_for_parse_state->in_intervening_code)
13046 return;
13047 omp_for_parse_state->saw_intervening_code = true;
13049 /* Only diagnose errors related to perfect nesting once. */
13050 if (!omp_for_parse_state->perfect_nesting_fail)
13052 if (omp_for_parse_state->code == OACC_LOOP)
13054 error_at (omp_for_parse_state->for_loc,
13055 "inner loops must be perfectly nested in "
13056 "%<#pragma acc loop%>");
13057 omp_for_parse_state->perfect_nesting_fail = true;
13059 else if (omp_for_parse_state->ordered)
13061 error_at (omp_for_parse_state->for_loc,
13062 "inner loops must be perfectly nested with "
13063 "%<ordered%> clause");
13064 omp_for_parse_state->perfect_nesting_fail = true;
13066 else if (omp_for_parse_state->inscan)
13068 error_at (omp_for_parse_state->for_loc,
13069 "inner loops must be perfectly nested with "
13070 "%<reduction%> %<inscan%> clause");
13071 omp_for_parse_state->perfect_nesting_fail = true;
13073 /* TODO: Also reject loops with TILE directive. */
13074 if (omp_for_parse_state->perfect_nesting_fail)
13075 omp_for_parse_state->fail = true;
13079 /* Parse an (optional) statement-seq.
13081 statement-seq:
13082 statement
13083 statement-seq [opt] statement */
13085 static void
13086 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
13088 struct omp_for_parse_data *omp_for_parse_state
13089 = parser->omp_for_parse_state;
13090 bool in_omp_loop_block
13091 = omp_for_parse_state ? omp_for_parse_state->want_nested_loop : false;
13093 /* Scan statements until there aren't any more. */
13094 while (true)
13096 cp_token *token = cp_lexer_peek_token (parser->lexer);
13098 /* If we are looking at a `}', then we have run out of
13099 statements; the same is true if we have reached the end
13100 of file, or have stumbled upon a stray '@end'. */
13101 if (token->type == CPP_CLOSE_BRACE
13102 || token->type == CPP_EOF
13103 || token->type == CPP_PRAGMA_EOL
13104 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
13105 break;
13107 /* If we are in a compound statement and find 'else' then
13108 something went wrong. */
13109 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
13111 if (parser->in_statement & IN_IF_STMT)
13112 break;
13113 else
13115 token = cp_lexer_consume_token (parser->lexer);
13116 error_at (token->location, "%<else%> without a previous %<if%>");
13120 /* Handle special cases for OMP FOR canonical loop syntax. */
13121 else if (in_omp_loop_block)
13123 bool want_nested_loop = omp_for_parse_state->want_nested_loop;
13124 if (want_nested_loop
13125 && token->type == CPP_KEYWORD && token->keyword == RID_FOR)
13127 /* Found the nested loop. */
13128 omp_for_parse_state->depth++;
13129 add_stmt (cp_parser_omp_loop_nest (parser, NULL));
13130 omp_for_parse_state->depth--;
13132 else if (token->type == CPP_SEMICOLON)
13134 /* Prior to implementing the OpenMP 5.1 syntax for canonical
13135 loop form, GCC used to accept an empty statements as not
13136 being intervening code. Continue to do that, as an
13137 extension. */
13138 /* FIXME: Maybe issue a warning or something here? */
13139 cp_lexer_consume_token (parser->lexer);
13141 else if (want_nested_loop && token->type == CPP_OPEN_BRACE)
13142 /* The nested compound statement may contain the next loop, or
13143 it might just be intervening code. */
13145 cp_parser_statement (parser, in_statement_expr, true, NULL);
13146 if (omp_for_parse_state->want_nested_loop)
13147 check_omp_intervening_code (parser);
13149 else
13151 /* This must be intervening code. */
13152 omp_for_parse_state->want_nested_loop = false;
13153 /* Defer calling check_omp_intervening_code on pragmas until
13154 cp_parser_statement, because we can't know until we parse
13155 it whether or not the pragma is a statement. */
13156 if (token->type != CPP_PRAGMA)
13157 check_omp_intervening_code (parser);
13158 cp_parser_statement (parser, in_statement_expr, true, NULL);
13159 omp_for_parse_state->want_nested_loop = want_nested_loop;
13161 continue;
13164 /* Parse the statement. */
13165 cp_parser_statement (parser, in_statement_expr, true, NULL);
13169 /* Return true if this is the C++20 version of range-based-for with
13170 init-statement. */
13172 static bool
13173 cp_parser_range_based_for_with_init_p (cp_parser *parser)
13175 bool r = false;
13177 /* Save tokens so that we can put them back. */
13178 cp_lexer_save_tokens (parser->lexer);
13180 /* There has to be an unnested ; followed by an unnested :. */
13181 if (cp_parser_skip_to_closing_parenthesis_1 (parser,
13182 /*recovering=*/false,
13183 CPP_SEMICOLON,
13184 /*consume_paren=*/false) != -1)
13185 goto out;
13187 /* We found the semicolon, eat it now. */
13188 cp_lexer_consume_token (parser->lexer);
13190 /* Now look for ':' that is not nested in () or {}. */
13191 r = (cp_parser_skip_to_closing_parenthesis_1 (parser,
13192 /*recovering=*/false,
13193 CPP_COLON,
13194 /*consume_paren=*/false) == -1);
13196 out:
13197 /* Roll back the tokens we skipped. */
13198 cp_lexer_rollback_tokens (parser->lexer);
13200 return r;
13203 /* Return true if we're looking at (init; cond), false otherwise. */
13205 static bool
13206 cp_parser_init_statement_p (cp_parser *parser)
13208 /* Save tokens so that we can put them back. */
13209 cp_lexer_save_tokens (parser->lexer);
13211 /* Look for ';' that is not nested in () or {}. */
13212 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
13213 /*recovering=*/false,
13214 CPP_SEMICOLON,
13215 /*consume_paren=*/false);
13217 /* Roll back the tokens we skipped. */
13218 cp_lexer_rollback_tokens (parser->lexer);
13220 return ret == -1;
13223 /* Parse a selection-statement.
13225 selection-statement:
13226 if ( init-statement [opt] condition ) statement
13227 if ( init-statement [opt] condition ) statement else statement
13228 switch ( init-statement [opt] condition ) statement
13230 Returns the new IF_STMT or SWITCH_STMT.
13232 If IF_P is not NULL, *IF_P is set to indicate whether the statement
13233 is a (possibly labeled) if statement which is not enclosed in
13234 braces and has an else clause. This is used to implement
13235 -Wparentheses.
13237 CHAIN is a vector of if-else-if conditions. This is used to implement
13238 -Wduplicated-cond. */
13240 static tree
13241 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
13242 vec<tree> *chain)
13244 cp_token *token;
13245 enum rid keyword;
13246 token_indent_info guard_tinfo;
13248 if (if_p != NULL)
13249 *if_p = false;
13251 /* Peek at the next token. */
13252 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
13253 guard_tinfo = get_token_indent_info (token);
13255 /* See what kind of keyword it is. */
13256 keyword = token->keyword;
13257 switch (keyword)
13259 case RID_IF:
13260 case RID_SWITCH:
13262 tree statement;
13263 tree condition;
13265 bool cx = false;
13266 if (keyword == RID_IF
13267 && cp_lexer_next_token_is_keyword (parser->lexer,
13268 RID_CONSTEXPR))
13270 cx = true;
13271 cp_token *tok = cp_lexer_consume_token (parser->lexer);
13272 if (cxx_dialect < cxx17)
13273 pedwarn (tok->location, OPT_Wc__17_extensions,
13274 "%<if constexpr%> only available with "
13275 "%<-std=c++17%> or %<-std=gnu++17%>");
13277 int ce = 0;
13278 if (keyword == RID_IF && !cx)
13280 if (cp_lexer_next_token_is_keyword (parser->lexer,
13281 RID_CONSTEVAL))
13282 ce = 1;
13283 else if (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
13284 && cp_lexer_nth_token_is_keyword (parser->lexer, 2,
13285 RID_CONSTEVAL))
13287 ce = -1;
13288 cp_lexer_consume_token (parser->lexer);
13291 if (ce)
13293 cp_token *tok = cp_lexer_consume_token (parser->lexer);
13294 if (cxx_dialect < cxx23)
13295 pedwarn (tok->location, OPT_Wc__23_extensions,
13296 "%<if consteval%> only available with "
13297 "%<-std=c++2b%> or %<-std=gnu++2b%>");
13299 bool save_in_consteval_if_p = in_consteval_if_p;
13300 statement = begin_if_stmt ();
13301 IF_STMT_CONSTEVAL_P (statement) = true;
13302 condition = finish_if_stmt_cond (boolean_false_node, statement);
13304 gcc_rich_location richloc (tok->location);
13305 bool non_compound_stmt_p = false;
13306 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13308 non_compound_stmt_p = true;
13309 richloc.add_fixit_insert_after (tok->location, "{");
13312 in_consteval_if_p |= ce > 0;
13313 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
13315 if (non_compound_stmt_p)
13317 location_t before_loc
13318 = cp_lexer_peek_token (parser->lexer)->location;
13319 richloc.add_fixit_insert_before (before_loc, "}");
13320 error_at (&richloc,
13321 "%<if consteval%> requires compound statement");
13322 non_compound_stmt_p = false;
13325 finish_then_clause (statement);
13327 /* If the next token is `else', parse the else-clause. */
13328 if (cp_lexer_next_token_is_keyword (parser->lexer,
13329 RID_ELSE))
13331 cp_token *else_tok = cp_lexer_peek_token (parser->lexer);
13332 gcc_rich_location else_richloc (else_tok->location);
13333 guard_tinfo = get_token_indent_info (else_tok);
13334 /* Consume the `else' keyword. */
13335 cp_lexer_consume_token (parser->lexer);
13337 begin_else_clause (statement);
13339 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13341 non_compound_stmt_p = true;
13342 else_richloc.add_fixit_insert_after (else_tok->location,
13343 "{");
13346 in_consteval_if_p = save_in_consteval_if_p | (ce < 0);
13347 cp_parser_implicitly_scoped_statement (parser, NULL,
13348 guard_tinfo);
13350 if (non_compound_stmt_p)
13352 location_t before_loc
13353 = cp_lexer_peek_token (parser->lexer)->location;
13354 else_richloc.add_fixit_insert_before (before_loc, "}");
13355 error_at (&else_richloc,
13356 "%<if consteval%> requires compound statement");
13359 finish_else_clause (statement);
13362 in_consteval_if_p = save_in_consteval_if_p;
13363 if (ce < 0)
13365 std::swap (THEN_CLAUSE (statement), ELSE_CLAUSE (statement));
13366 if (THEN_CLAUSE (statement) == NULL_TREE)
13367 THEN_CLAUSE (statement) = build_empty_stmt (tok->location);
13370 finish_if_stmt (statement);
13371 return statement;
13374 /* Look for the `('. */
13375 matching_parens parens;
13376 if (!parens.require_open (parser))
13378 cp_parser_skip_to_end_of_statement (parser);
13379 return error_mark_node;
13382 /* Begin the selection-statement. */
13383 if (keyword == RID_IF)
13385 statement = begin_if_stmt ();
13386 IF_STMT_CONSTEXPR_P (statement) = cx;
13388 else
13389 statement = begin_switch_stmt ();
13391 /* Parse the optional init-statement. */
13392 if (cp_parser_init_statement_p (parser))
13394 tree decl;
13395 if (cxx_dialect < cxx17)
13396 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
13397 OPT_Wc__17_extensions,
13398 "init-statement in selection statements only available "
13399 "with %<-std=c++17%> or %<-std=gnu++17%>");
13400 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13401 /* A non-empty init-statement can have arbitrary side
13402 effects. */
13403 vec_free (chain);
13404 cp_parser_init_statement (parser, &decl);
13407 /* Parse the condition. */
13408 condition = cp_parser_condition (parser);
13409 /* Look for the `)'. */
13410 if (!parens.require_close (parser))
13411 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13412 /*consume_paren=*/true);
13414 if (keyword == RID_IF)
13416 bool nested_if;
13417 unsigned char in_statement;
13419 /* Add the condition. */
13420 condition = finish_if_stmt_cond (condition, statement);
13422 if (warn_duplicated_cond)
13423 warn_duplicated_cond_add_or_warn (token->location, condition,
13424 &chain);
13426 /* Parse the then-clause. */
13427 in_statement = parser->in_statement;
13428 parser->in_statement |= IN_IF_STMT;
13430 /* Outside a template, the non-selected branch of a constexpr
13431 if is a 'discarded statement', i.e. unevaluated. */
13432 bool was_discarded = in_discarded_stmt;
13433 bool discard_then = (cx && !processing_template_decl
13434 && integer_zerop (condition));
13435 if (discard_then)
13437 in_discarded_stmt = true;
13438 ++c_inhibit_evaluation_warnings;
13441 cp_parser_implicitly_scoped_statement (parser, &nested_if,
13442 guard_tinfo);
13444 parser->in_statement = in_statement;
13446 finish_then_clause (statement);
13448 if (discard_then)
13450 THEN_CLAUSE (statement) = NULL_TREE;
13451 in_discarded_stmt = was_discarded;
13452 --c_inhibit_evaluation_warnings;
13455 /* If the next token is `else', parse the else-clause. */
13456 if (cp_lexer_next_token_is_keyword (parser->lexer,
13457 RID_ELSE))
13459 bool discard_else = (cx && !processing_template_decl
13460 && integer_nonzerop (condition));
13461 if (discard_else)
13463 in_discarded_stmt = true;
13464 ++c_inhibit_evaluation_warnings;
13467 guard_tinfo
13468 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13469 /* Consume the `else' keyword. */
13470 cp_lexer_consume_token (parser->lexer);
13471 if (warn_duplicated_cond)
13473 if (cp_lexer_next_token_is_keyword (parser->lexer,
13474 RID_IF)
13475 && chain == NULL)
13477 /* We've got "if (COND) else if (COND2)". Start
13478 the condition chain and add COND as the first
13479 element. */
13480 chain = new vec<tree> ();
13481 if (!CONSTANT_CLASS_P (condition)
13482 && !TREE_SIDE_EFFECTS (condition))
13484 /* Wrap it in a NOP_EXPR so that we can set the
13485 location of the condition. */
13486 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
13487 condition);
13488 SET_EXPR_LOCATION (e, token->location);
13489 chain->safe_push (e);
13492 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
13493 RID_IF))
13494 /* This is if-else without subsequent if. Zap the
13495 condition chain; we would have already warned at
13496 this point. */
13497 vec_free (chain);
13499 begin_else_clause (statement);
13500 /* Parse the else-clause. */
13501 cp_parser_implicitly_scoped_statement (parser, NULL,
13502 guard_tinfo, chain);
13504 finish_else_clause (statement);
13506 /* If we are currently parsing a then-clause, then
13507 IF_P will not be NULL. We set it to true to
13508 indicate that this if statement has an else clause.
13509 This may trigger the Wparentheses warning below
13510 when we get back up to the parent if statement. */
13511 if (if_p != NULL)
13512 *if_p = true;
13514 if (discard_else)
13516 ELSE_CLAUSE (statement) = NULL_TREE;
13517 in_discarded_stmt = was_discarded;
13518 --c_inhibit_evaluation_warnings;
13521 else
13523 /* This if statement does not have an else clause. If
13524 NESTED_IF is true, then the then-clause has an if
13525 statement which does have an else clause. We warn
13526 about the potential ambiguity. */
13527 if (nested_if)
13528 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
13529 "suggest explicit braces to avoid ambiguous"
13530 " %<else%>");
13531 if (warn_duplicated_cond)
13532 /* We don't need the condition chain anymore. */
13533 vec_free (chain);
13536 /* Now we're all done with the if-statement. */
13537 finish_if_stmt (statement);
13539 else
13541 bool in_switch_statement_p;
13542 unsigned char in_statement;
13544 /* Add the condition. */
13545 finish_switch_cond (condition, statement);
13547 /* Parse the body of the switch-statement. */
13548 in_switch_statement_p = parser->in_switch_statement_p;
13549 in_statement = parser->in_statement;
13550 parser->in_switch_statement_p = true;
13551 parser->in_statement |= IN_SWITCH_STMT;
13552 cp_parser_implicitly_scoped_statement (parser, if_p,
13553 guard_tinfo);
13554 parser->in_switch_statement_p = in_switch_statement_p;
13555 parser->in_statement = in_statement;
13557 /* Now we're all done with the switch-statement. */
13558 finish_switch_stmt (statement);
13561 return statement;
13563 break;
13565 default:
13566 cp_parser_error (parser, "expected selection-statement");
13567 return error_mark_node;
13571 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
13572 If we have seen at least one decl-specifier, and the next token is not
13573 a parenthesis (after "int (" we might be looking at a functional cast)
13574 neither we are dealing with a concept-check expression then we must be
13575 looking at a declaration. */
13577 static void
13578 cp_parser_maybe_commit_to_declaration (cp_parser* parser,
13579 cp_decl_specifier_seq *decl_specs)
13581 if (decl_specs->any_specifiers_p
13582 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
13583 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
13584 && !cp_parser_error_occurred (parser)
13585 && !(decl_specs->type
13586 && TREE_CODE (decl_specs->type) == TYPE_DECL
13587 && is_constrained_auto (TREE_TYPE (decl_specs->type))))
13588 cp_parser_commit_to_tentative_parse (parser);
13591 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
13592 The declarator shall not specify a function or an array. Returns
13593 TRUE if the declarator is valid, FALSE otherwise. */
13595 static bool
13596 cp_parser_check_condition_declarator (cp_parser* parser,
13597 cp_declarator *declarator,
13598 location_t loc)
13600 if (declarator == cp_error_declarator
13601 || function_declarator_p (declarator)
13602 || declarator->kind == cdk_array)
13604 if (declarator == cp_error_declarator)
13605 /* Already complained. */;
13606 else if (declarator->kind == cdk_array)
13607 error_at (loc, "condition declares an array");
13608 else
13609 error_at (loc, "condition declares a function");
13610 if (parser->fully_implicit_function_template_p)
13611 abort_fully_implicit_template (parser);
13612 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
13613 /*or_comma=*/false,
13614 /*consume_paren=*/false);
13615 return false;
13617 else
13618 return true;
13621 /* Parse a condition.
13623 condition:
13624 expression
13625 type-specifier-seq declarator = initializer-clause
13626 type-specifier-seq declarator braced-init-list
13628 GNU Extension:
13630 condition:
13631 type-specifier-seq declarator asm-specification [opt]
13632 attributes [opt] = assignment-expression
13634 Returns the expression that should be tested. */
13636 static tree
13637 cp_parser_condition (cp_parser* parser)
13639 cp_decl_specifier_seq type_specifiers;
13640 const char *saved_message;
13641 int declares_class_or_enum;
13643 /* Try the declaration first. */
13644 cp_parser_parse_tentatively (parser);
13645 /* New types are not allowed in the type-specifier-seq for a
13646 condition. */
13647 saved_message = parser->type_definition_forbidden_message;
13648 parser->type_definition_forbidden_message
13649 = G_("types may not be defined in conditions");
13650 /* Parse the type-specifier-seq. */
13651 cp_parser_decl_specifier_seq (parser,
13652 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
13653 &type_specifiers,
13654 &declares_class_or_enum);
13655 /* Restore the saved message. */
13656 parser->type_definition_forbidden_message = saved_message;
13658 /* Gather the attributes that were provided with the
13659 decl-specifiers. */
13660 tree prefix_attributes = type_specifiers.attributes;
13662 cp_parser_maybe_commit_to_declaration (parser, &type_specifiers);
13664 /* If all is well, we might be looking at a declaration. */
13665 if (!cp_parser_error_occurred (parser))
13667 tree decl;
13668 tree asm_specification;
13669 tree attributes;
13670 cp_declarator *declarator;
13671 tree initializer = NULL_TREE;
13672 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13674 /* Parse the declarator. */
13675 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13676 CP_PARSER_FLAGS_NONE,
13677 /*ctor_dtor_or_conv_p=*/NULL,
13678 /*parenthesized_p=*/NULL,
13679 /*member_p=*/false,
13680 /*friend_p=*/false,
13681 /*static_p=*/false);
13682 /* Parse the attributes. */
13683 attributes = cp_parser_attributes_opt (parser);
13684 /* Parse the asm-specification. */
13685 asm_specification = cp_parser_asm_specification_opt (parser);
13686 /* If the next token is not an `=' or '{', then we might still be
13687 looking at an expression. For example:
13689 if (A(a).x)
13691 looks like a decl-specifier-seq and a declarator -- but then
13692 there is no `=', so this is an expression. */
13693 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
13694 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13695 cp_parser_simulate_error (parser);
13697 /* If we did see an `=' or '{', then we are looking at a declaration
13698 for sure. */
13699 if (cp_parser_parse_definitely (parser))
13701 tree pushed_scope;
13702 bool non_constant_p = false;
13703 int flags = LOOKUP_ONLYCONVERTING;
13705 if (!cp_parser_check_condition_declarator (parser, declarator, loc))
13706 return error_mark_node;
13708 /* Create the declaration. */
13709 decl = start_decl (declarator, &type_specifiers,
13710 /*initialized_p=*/true,
13711 attributes, prefix_attributes,
13712 &pushed_scope);
13714 declarator->init_loc = cp_lexer_peek_token (parser->lexer)->location;
13715 /* Parse the initializer. */
13716 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13718 initializer = cp_parser_braced_list (parser, &non_constant_p);
13719 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
13720 flags = 0;
13722 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13724 /* Consume the `='. */
13725 cp_lexer_consume_token (parser->lexer);
13726 initializer = cp_parser_initializer_clause (parser,
13727 &non_constant_p);
13729 else
13731 cp_parser_error (parser, "expected initializer");
13732 initializer = error_mark_node;
13734 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
13735 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
13737 /* Process the initializer. */
13738 cp_finish_decl (decl,
13739 initializer, !non_constant_p,
13740 asm_specification,
13741 flags);
13743 if (pushed_scope)
13744 pop_scope (pushed_scope);
13746 return convert_from_reference (decl);
13749 /* If we didn't even get past the declarator successfully, we are
13750 definitely not looking at a declaration. */
13751 else
13752 cp_parser_abort_tentative_parse (parser);
13754 /* Otherwise, we are looking at an expression. */
13755 return cp_parser_expression (parser);
13758 /* Parses a for-statement or range-for-statement until the closing ')',
13759 not included. */
13761 static tree
13762 cp_parser_for (cp_parser *parser, bool ivdep, unsigned short unroll,
13763 bool novector)
13765 tree init, scope, decl;
13766 bool is_range_for;
13768 /* Begin the for-statement. */
13769 scope = begin_for_scope (&init);
13771 /* Maybe parse the optional init-statement in a range-based for loop. */
13772 if (cp_parser_range_based_for_with_init_p (parser)
13773 /* Checked for diagnostic purposes only. */
13774 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13776 tree dummy;
13777 cp_parser_init_statement (parser, &dummy);
13778 if (cxx_dialect < cxx20)
13780 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
13781 OPT_Wc__20_extensions,
13782 "range-based %<for%> loops with initializer only "
13783 "available with %<-std=c++20%> or %<-std=gnu++20%>");
13784 decl = error_mark_node;
13788 /* Parse the initialization. */
13789 is_range_for = cp_parser_init_statement (parser, &decl);
13791 if (is_range_for)
13792 return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll,
13793 novector, false);
13794 else
13795 return cp_parser_c_for (parser, scope, init, ivdep, unroll, novector);
13798 static tree
13799 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
13800 unsigned short unroll, bool novector)
13802 /* Normal for loop */
13803 tree condition = NULL_TREE;
13804 tree expression = NULL_TREE;
13805 tree stmt;
13807 stmt = begin_for_stmt (scope, init);
13808 /* The init-statement has already been parsed in
13809 cp_parser_init_statement, so no work is needed here. */
13810 finish_init_stmt (stmt);
13812 /* If there's a condition, process it. */
13813 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13814 condition = cp_parser_condition (parser);
13815 else if (ivdep)
13817 cp_parser_error (parser, "missing loop condition in loop with "
13818 "%<GCC ivdep%> pragma");
13819 condition = error_mark_node;
13821 else if (unroll)
13823 cp_parser_error (parser, "missing loop condition in loop with "
13824 "%<GCC unroll%> pragma");
13825 condition = error_mark_node;
13827 finish_for_cond (condition, stmt, ivdep, unroll, novector);
13828 /* Look for the `;'. */
13829 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13831 /* If there's an expression, process it. */
13832 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
13833 expression = cp_parser_expression (parser);
13834 finish_for_expr (expression, stmt);
13836 return stmt;
13839 /* Tries to parse a range-based for-statement:
13841 range-based-for:
13842 decl-specifier-seq declarator : expression
13844 The decl-specifier-seq declarator and the `:' are already parsed by
13845 cp_parser_init_statement. If processing_template_decl it returns a
13846 newly created RANGE_FOR_STMT; if not, it is converted to a
13847 regular FOR_STMT. */
13849 static tree
13850 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
13851 bool ivdep, unsigned short unroll, bool novector,
13852 bool is_omp)
13854 tree stmt, range_expr;
13855 auto_vec <cxx_binding *, 16> bindings;
13856 auto_vec <tree, 16> names;
13857 cp_decomp decomp_d, *decomp = NULL;
13859 /* Get the range declaration momentarily out of the way so that
13860 the range expression doesn't clash with it. */
13861 if (range_decl != error_mark_node)
13863 if (DECL_HAS_VALUE_EXPR_P (range_decl))
13865 tree v = DECL_VALUE_EXPR (range_decl);
13866 /* For decomposition declaration get all of the corresponding
13867 declarations out of the way. */
13868 if (TREE_CODE (v) == ARRAY_REF
13869 && VAR_P (TREE_OPERAND (v, 0))
13870 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
13872 tree d = range_decl;
13873 range_decl = TREE_OPERAND (v, 0);
13874 decomp = &decomp_d;
13875 decomp->count = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
13876 decomp->decl = d;
13877 for (unsigned int i = 0; i < decomp->count;
13878 i++, d = DECL_CHAIN (d))
13880 tree name = DECL_NAME (d);
13881 names.safe_push (name);
13882 bindings.safe_push (IDENTIFIER_BINDING (name));
13883 IDENTIFIER_BINDING (name)
13884 = IDENTIFIER_BINDING (name)->previous;
13888 if (names.is_empty ())
13890 tree name = DECL_NAME (range_decl);
13891 names.safe_push (name);
13892 bindings.safe_push (IDENTIFIER_BINDING (name));
13893 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
13897 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13898 range_expr = cp_parser_braced_list (parser);
13899 else
13900 range_expr = cp_parser_expression (parser);
13902 /* Put the range declaration(s) back into scope. */
13903 for (unsigned int i = 0; i < names.length (); i++)
13905 cxx_binding *binding = bindings[i];
13906 binding->previous = IDENTIFIER_BINDING (names[i]);
13907 IDENTIFIER_BINDING (names[i]) = binding;
13910 /* finish_omp_for has its own code for the following, so just
13911 return the range_expr instead. */
13912 if (is_omp)
13913 return range_expr;
13915 /* If in template, STMT is converted to a normal for-statement
13916 at instantiation. If not, it is done just ahead. */
13917 if (processing_template_decl)
13919 if (check_for_bare_parameter_packs (range_expr))
13920 range_expr = error_mark_node;
13921 stmt = begin_range_for_stmt (scope, init);
13922 if (ivdep)
13923 RANGE_FOR_IVDEP (stmt) = 1;
13924 if (unroll)
13925 RANGE_FOR_UNROLL (stmt) = build_int_cst (integer_type_node, unroll);
13926 if (novector)
13927 RANGE_FOR_NOVECTOR (stmt) = 1;
13928 finish_range_for_decl (stmt, range_decl, range_expr);
13929 if (!type_dependent_expression_p (range_expr)
13930 /* do_auto_deduction doesn't mess with template init-lists. */
13931 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
13932 do_range_for_auto_deduction (range_decl, range_expr, decomp);
13934 else
13936 stmt = begin_for_stmt (scope, init);
13937 stmt = cp_convert_range_for (stmt, range_decl, range_expr, decomp,
13938 ivdep, unroll, novector);
13940 return stmt;
13943 /* Subroutine of cp_convert_range_for: given the initializer expression,
13944 builds up the range temporary. */
13946 static tree
13947 build_range_temp (tree range_expr)
13949 /* Find out the type deduced by the declaration
13950 `auto &&__range = range_expr'. */
13951 tree auto_node = make_auto ();
13952 tree range_type = cp_build_reference_type (auto_node, true);
13953 range_type = do_auto_deduction (range_type, range_expr, auto_node);
13955 /* Create the __range variable. */
13956 tree range_temp = build_decl (input_location, VAR_DECL,
13957 for_range__identifier, range_type);
13958 TREE_USED (range_temp) = 1;
13959 DECL_ARTIFICIAL (range_temp) = 1;
13961 return range_temp;
13964 /* Used by cp_parser_range_for in template context: we aren't going to
13965 do a full conversion yet, but we still need to resolve auto in the
13966 type of the for-range-declaration if present. This is basically
13967 a shortcut version of cp_convert_range_for. */
13969 static void
13970 do_range_for_auto_deduction (tree decl, tree range_expr, cp_decomp *decomp)
13972 tree auto_node = type_uses_auto (TREE_TYPE (decl));
13973 if (auto_node)
13975 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
13976 range_temp = convert_from_reference (build_range_temp (range_expr));
13977 iter_type = (cp_parser_perform_range_for_lookup
13978 (range_temp, &begin_dummy, &end_dummy));
13979 if (iter_type)
13981 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
13982 iter_type);
13983 iter_decl = build_x_indirect_ref (input_location, iter_decl,
13984 RO_UNARY_STAR, NULL_TREE,
13985 tf_warning_or_error);
13986 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
13987 iter_decl, auto_node,
13988 tf_warning_or_error,
13989 adc_variable_type);
13990 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
13991 cp_finish_decomp (decl, decomp);
13996 /* Warns when the loop variable should be changed to a reference type to
13997 avoid unnecessary copying. I.e., from
13999 for (const auto x : range)
14001 where range returns a reference, to
14003 for (const auto &x : range)
14005 if this version doesn't make a copy.
14007 This function also warns when the loop variable is initialized with
14008 a value of a different type resulting in a copy:
14010 int arr[10];
14011 for (const double &x : arr)
14013 DECL is the RANGE_DECL; EXPR is the *__for_begin expression.
14014 This function is never called when processing_template_decl is on. */
14016 static void
14017 warn_for_range_copy (tree decl, tree expr)
14019 if (!warn_range_loop_construct
14020 || decl == error_mark_node)
14021 return;
14023 location_t loc = DECL_SOURCE_LOCATION (decl);
14024 tree type = TREE_TYPE (decl);
14026 if (from_macro_expansion_at (loc))
14027 return;
14029 if (TYPE_REF_P (type))
14031 if (glvalue_p (expr)
14032 && ref_conv_binds_to_temporary (type, expr).is_true ())
14034 auto_diagnostic_group d;
14035 if (warning_at (loc, OPT_Wrange_loop_construct,
14036 "loop variable %qD of type %qT binds to a temporary "
14037 "constructed from type %qT", decl, type,
14038 TREE_TYPE (expr)))
14040 tree ref = cp_build_qualified_type (TREE_TYPE (expr),
14041 TYPE_QUAL_CONST);
14042 ref = cp_build_reference_type (ref, /*rval*/false);
14043 inform (loc, "use non-reference type %qT to make the copy "
14044 "explicit or %qT to prevent copying",
14045 non_reference (type), ref);
14048 return;
14050 else if (!CP_TYPE_CONST_P (type))
14051 return;
14053 /* Since small trivially copyable types are cheap to copy, we suppress the
14054 warning for them. 64B is a common size of a cache line. */
14055 if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST
14056 || (tree_to_uhwi (TYPE_SIZE_UNIT (type)) <= 64
14057 && trivially_copyable_p (type)))
14058 return;
14060 /* If we can initialize a reference directly, suggest that to avoid the
14061 copy. */
14062 tree rtype = cp_build_reference_type (type, /*rval*/false);
14063 if (ref_conv_binds_to_temporary (rtype, expr).is_false ())
14065 auto_diagnostic_group d;
14066 if (warning_at (loc, OPT_Wrange_loop_construct,
14067 "loop variable %qD creates a copy from type %qT",
14068 decl, type))
14070 gcc_rich_location richloc (loc);
14071 richloc.add_fixit_insert_before ("&");
14072 inform (&richloc, "use reference type to prevent copying");
14077 /* Converts a range-based for-statement into a normal
14078 for-statement, as per the definition.
14080 for (RANGE_DECL : RANGE_EXPR)
14081 BLOCK
14083 should be equivalent to:
14086 auto &&__range = RANGE_EXPR;
14087 for (auto __begin = BEGIN_EXPR, __end = END_EXPR;
14088 __begin != __end;
14089 ++__begin)
14091 RANGE_DECL = *__begin;
14092 BLOCK
14096 If RANGE_EXPR is an array:
14097 BEGIN_EXPR = __range
14098 END_EXPR = __range + ARRAY_SIZE(__range)
14099 Else if RANGE_EXPR has a member 'begin' or 'end':
14100 BEGIN_EXPR = __range.begin()
14101 END_EXPR = __range.end()
14102 Else:
14103 BEGIN_EXPR = begin(__range)
14104 END_EXPR = end(__range);
14106 If __range has a member 'begin' but not 'end', or vice versa, we must
14107 still use the second alternative (it will surely fail, however).
14108 When calling begin()/end() in the third alternative we must use
14109 argument dependent lookup, but always considering 'std' as an associated
14110 namespace. */
14112 tree
14113 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
14114 cp_decomp *decomp, bool ivdep, unsigned short unroll,
14115 bool novector)
14117 tree begin, end;
14118 tree iter_type, begin_expr, end_expr;
14119 tree condition, expression;
14121 range_expr = mark_lvalue_use (range_expr);
14123 if (range_decl == error_mark_node || range_expr == error_mark_node)
14124 /* If an error happened previously do nothing or else a lot of
14125 unhelpful errors would be issued. */
14126 begin_expr = end_expr = iter_type = error_mark_node;
14127 else
14129 tree range_temp;
14131 if (VAR_P (range_expr)
14132 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
14133 /* Can't bind a reference to an array of runtime bound. */
14134 range_temp = range_expr;
14135 else
14137 range_temp = build_range_temp (range_expr);
14138 pushdecl (range_temp);
14139 cp_finish_decl (range_temp, range_expr,
14140 /*is_constant_init*/false, NULL_TREE,
14141 LOOKUP_ONLYCONVERTING);
14142 range_temp = convert_from_reference (range_temp);
14144 iter_type = cp_parser_perform_range_for_lookup (range_temp,
14145 &begin_expr, &end_expr);
14148 /* The new for initialization statement. */
14149 begin = build_decl (input_location, VAR_DECL, for_begin__identifier,
14150 iter_type);
14151 TREE_USED (begin) = 1;
14152 DECL_ARTIFICIAL (begin) = 1;
14153 pushdecl (begin);
14154 cp_finish_decl (begin, begin_expr,
14155 /*is_constant_init*/false, NULL_TREE,
14156 LOOKUP_ONLYCONVERTING);
14158 if (cxx_dialect >= cxx17)
14159 iter_type = cv_unqualified (TREE_TYPE (end_expr));
14160 end = build_decl (input_location, VAR_DECL, for_end__identifier, iter_type);
14161 TREE_USED (end) = 1;
14162 DECL_ARTIFICIAL (end) = 1;
14163 pushdecl (end);
14164 cp_finish_decl (end, end_expr,
14165 /*is_constant_init*/false, NULL_TREE,
14166 LOOKUP_ONLYCONVERTING);
14168 finish_init_stmt (statement);
14170 /* The new for condition. */
14171 condition = build_x_binary_op (input_location, NE_EXPR,
14172 begin, ERROR_MARK,
14173 end, ERROR_MARK,
14174 NULL_TREE, NULL, tf_warning_or_error);
14175 finish_for_cond (condition, statement, ivdep, unroll, novector);
14177 /* The new increment expression. */
14178 expression = finish_unary_op_expr (input_location,
14179 PREINCREMENT_EXPR, begin,
14180 tf_warning_or_error);
14181 finish_for_expr (expression, statement);
14183 /* The declaration is initialized with *__begin inside the loop body. */
14184 tree deref_begin = build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
14185 NULL_TREE, tf_warning_or_error);
14186 cp_finish_decl (range_decl, deref_begin,
14187 /*is_constant_init*/false, NULL_TREE,
14188 LOOKUP_ONLYCONVERTING, decomp);
14189 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
14190 cp_finish_decomp (range_decl, decomp);
14192 warn_for_range_copy (range_decl, deref_begin);
14194 return statement;
14197 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
14198 We need to solve both at the same time because the method used
14199 depends on the existence of members begin or end.
14200 Returns the type deduced for the iterator expression. */
14202 static tree
14203 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
14205 if (error_operand_p (range))
14207 *begin = *end = error_mark_node;
14208 return error_mark_node;
14211 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
14213 error ("range-based %<for%> expression of type %qT "
14214 "has incomplete type", TREE_TYPE (range));
14215 *begin = *end = error_mark_node;
14216 return error_mark_node;
14218 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
14220 /* If RANGE is an array, we will use pointer arithmetic. */
14221 *begin = decay_conversion (range, tf_warning_or_error);
14222 *end = build_binary_op (input_location, PLUS_EXPR,
14223 range,
14224 array_type_nelts_top (TREE_TYPE (range)),
14225 false);
14226 return TREE_TYPE (*begin);
14228 else
14230 /* If it is not an array, we must do a bit of magic. */
14231 tree id_begin, id_end;
14232 tree member_begin, member_end;
14234 *begin = *end = error_mark_node;
14236 id_begin = get_identifier ("begin");
14237 id_end = get_identifier ("end");
14238 member_begin = lookup_member (TREE_TYPE (range), id_begin,
14239 /*protect=*/2, /*want_type=*/false,
14240 tf_warning_or_error);
14241 member_end = lookup_member (TREE_TYPE (range), id_end,
14242 /*protect=*/2, /*want_type=*/false,
14243 tf_warning_or_error);
14245 if (member_begin != NULL_TREE && member_end != NULL_TREE)
14247 /* Use the member functions. */
14248 *begin = cp_parser_range_for_member_function (range, id_begin);
14249 *end = cp_parser_range_for_member_function (range, id_end);
14251 else
14253 /* Use global functions with ADL. */
14254 releasing_vec vec;
14256 vec_safe_push (vec, range);
14258 member_begin = perform_koenig_lookup (id_begin, vec,
14259 tf_warning_or_error);
14260 *begin = finish_call_expr (member_begin, &vec, false, true,
14261 tf_warning_or_error);
14262 member_end = perform_koenig_lookup (id_end, vec,
14263 tf_warning_or_error);
14264 *end = finish_call_expr (member_end, &vec, false, true,
14265 tf_warning_or_error);
14268 /* Last common checks. */
14269 if (*begin == error_mark_node || *end == error_mark_node)
14271 /* If one of the expressions is an error do no more checks. */
14272 *begin = *end = error_mark_node;
14273 return error_mark_node;
14275 else if (type_dependent_expression_p (*begin)
14276 || type_dependent_expression_p (*end))
14277 /* Can happen, when, eg, in a template context, Koenig lookup
14278 can't resolve begin/end (c++/58503). */
14279 return NULL_TREE;
14280 else
14282 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
14283 /* The unqualified type of the __begin and __end temporaries should
14284 be the same, as required by the multiple auto declaration. */
14285 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
14287 if (cxx_dialect >= cxx17
14288 && (build_x_binary_op (input_location, NE_EXPR,
14289 *begin, ERROR_MARK,
14290 *end, ERROR_MARK,
14291 NULL_TREE, NULL, tf_none)
14292 != error_mark_node))
14293 /* P0184R0 allows __begin and __end to have different types,
14294 but make sure they are comparable so we can give a better
14295 diagnostic. */;
14296 else
14297 error ("inconsistent begin/end types in range-based %<for%> "
14298 "statement: %qT and %qT",
14299 TREE_TYPE (*begin), TREE_TYPE (*end));
14301 return iter_type;
14306 /* Helper function for cp_parser_perform_range_for_lookup.
14307 Builds a tree for RANGE.IDENTIFIER(). */
14309 static tree
14310 cp_parser_range_for_member_function (tree range, tree identifier)
14312 tree member, res;
14314 member = finish_class_member_access_expr (range, identifier,
14315 false, tf_warning_or_error);
14316 if (member == error_mark_node)
14317 return error_mark_node;
14319 releasing_vec vec;
14320 res = finish_call_expr (member, &vec,
14321 /*disallow_virtual=*/false,
14322 /*koenig_p=*/false,
14323 tf_warning_or_error);
14324 return res;
14327 /* Parse an iteration-statement.
14329 iteration-statement:
14330 while ( condition ) statement
14331 do statement while ( expression ) ;
14332 for ( init-statement condition [opt] ; expression [opt] )
14333 statement
14335 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
14337 static tree
14338 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
14339 unsigned short unroll, bool novector)
14341 cp_token *token;
14342 enum rid keyword;
14343 tree statement;
14344 unsigned char in_statement;
14345 token_indent_info guard_tinfo;
14347 /* Peek at the next token. */
14348 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
14349 if (!token)
14350 return error_mark_node;
14352 guard_tinfo = get_token_indent_info (token);
14354 /* Remember whether or not we are already within an iteration
14355 statement. */
14356 in_statement = parser->in_statement;
14358 /* Special case for OMP loop intervening code. Parsing of permitted
14359 collapsed loop nests is handled elsewhere. */
14360 if (parser->omp_for_parse_state)
14362 error_at (token->location,
14363 "loop not permitted in intervening code in OpenMP loop body");
14364 parser->omp_for_parse_state->fail = true;
14367 /* See what kind of keyword it is. */
14368 keyword = token->keyword;
14369 switch (keyword)
14371 case RID_WHILE:
14373 tree condition;
14375 /* Begin the while-statement. */
14376 statement = begin_while_stmt ();
14377 /* Look for the `('. */
14378 matching_parens parens;
14379 parens.require_open (parser);
14380 /* Parse the condition. */
14381 condition = cp_parser_condition (parser);
14382 finish_while_stmt_cond (condition, statement, ivdep, unroll, novector);
14383 /* Look for the `)'. */
14384 parens.require_close (parser);
14385 /* Parse the dependent statement. */
14386 parser->in_statement = IN_ITERATION_STMT;
14387 bool prev = note_iteration_stmt_body_start ();
14388 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
14389 note_iteration_stmt_body_end (prev);
14390 parser->in_statement = in_statement;
14391 /* We're done with the while-statement. */
14392 finish_while_stmt (statement);
14394 break;
14396 case RID_DO:
14398 tree expression;
14400 /* Begin the do-statement. */
14401 statement = begin_do_stmt ();
14402 /* Parse the body of the do-statement. */
14403 parser->in_statement = IN_ITERATION_STMT;
14404 bool prev = note_iteration_stmt_body_start ();
14405 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
14406 note_iteration_stmt_body_end (prev);
14407 parser->in_statement = in_statement;
14408 finish_do_body (statement);
14409 /* Look for the `while' keyword. */
14410 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
14411 /* Look for the `('. */
14412 matching_parens parens;
14413 parens.require_open (parser);
14414 /* Parse the expression. */
14415 expression = cp_parser_expression (parser);
14416 /* We're done with the do-statement. */
14417 finish_do_stmt (expression, statement, ivdep, unroll, novector);
14418 /* Look for the `)'. */
14419 parens.require_close (parser);
14420 /* Look for the `;'. */
14421 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14423 break;
14425 case RID_FOR:
14427 /* Look for the `('. */
14428 matching_parens parens;
14429 parens.require_open (parser);
14431 statement = cp_parser_for (parser, ivdep, unroll, novector);
14433 /* Look for the `)'. */
14434 parens.require_close (parser);
14436 /* Parse the body of the for-statement. */
14437 parser->in_statement = IN_ITERATION_STMT;
14438 bool prev = note_iteration_stmt_body_start ();
14439 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
14440 note_iteration_stmt_body_end (prev);
14441 parser->in_statement = in_statement;
14443 /* We're done with the for-statement. */
14444 finish_for_stmt (statement);
14446 break;
14448 default:
14449 cp_parser_error (parser, "expected iteration-statement");
14450 statement = error_mark_node;
14451 break;
14454 return statement;
14457 /* Parse an init-statement or the declarator of a range-based-for.
14458 Returns true if a range-based-for declaration is seen.
14460 init-statement:
14461 expression-statement
14462 simple-declaration
14463 alias-declaration */
14465 static bool
14466 cp_parser_init_statement (cp_parser *parser, tree *decl)
14468 /* If the next token is a `;', then we have an empty
14469 expression-statement. Grammatically, this is also a
14470 simple-declaration, but an invalid one, because it does not
14471 declare anything. Therefore, if we did not handle this case
14472 specially, we would issue an error message about an invalid
14473 declaration. */
14474 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14476 bool is_range_for = false;
14477 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
14479 /* A colon is used in range-based for. */
14480 parser->colon_corrects_to_scope_p = false;
14482 /* We're going to speculatively look for a declaration, falling back
14483 to an expression, if necessary. */
14484 cp_parser_parse_tentatively (parser);
14485 bool expect_semicolon_p = true;
14486 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
14488 cp_parser_alias_declaration (parser);
14489 expect_semicolon_p = false;
14490 if (cxx_dialect < cxx23
14491 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
14492 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
14493 OPT_Wc__23_extensions,
14494 "alias-declaration in init-statement only "
14495 "available with %<-std=c++23%> or %<-std=gnu++23%>");
14497 else
14498 /* Parse the declaration. */
14499 cp_parser_simple_declaration (parser,
14500 /*function_definition_allowed_p=*/false,
14501 decl);
14502 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
14503 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14505 /* It is a range-for, consume the ':'. */
14506 cp_lexer_consume_token (parser->lexer);
14507 is_range_for = true;
14508 if (cxx_dialect < cxx11)
14509 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
14510 OPT_Wc__11_extensions,
14511 "range-based %<for%> loops only available with "
14512 "%<-std=c++11%> or %<-std=gnu++11%>");
14514 else if (expect_semicolon_p)
14515 /* The ';' is not consumed yet because we told
14516 cp_parser_simple_declaration not to. */
14517 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14519 if (cp_parser_parse_definitely (parser))
14520 return is_range_for;
14521 /* If the tentative parse failed, then we shall need to look for an
14522 expression-statement. */
14524 /* If we are here, it is an expression-statement. */
14525 cp_parser_expression_statement (parser, NULL_TREE);
14526 return false;
14529 /* Parse a jump-statement.
14531 jump-statement:
14532 break ;
14533 continue ;
14534 return expression [opt] ;
14535 return braced-init-list ;
14536 coroutine-return-statement;
14537 goto identifier ;
14539 GNU extension:
14541 jump-statement:
14542 goto * expression ;
14544 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
14546 static tree
14547 cp_parser_jump_statement (cp_parser* parser)
14549 tree statement = error_mark_node;
14550 cp_token *token;
14551 enum rid keyword;
14552 unsigned char in_statement;
14554 /* Peek at the next token. */
14555 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
14556 if (!token)
14557 return error_mark_node;
14559 /* See what kind of keyword it is. */
14560 keyword = token->keyword;
14561 switch (keyword)
14563 case RID_BREAK:
14564 in_statement = parser->in_statement & ~IN_IF_STMT;
14565 switch (in_statement)
14567 case 0:
14568 error_at (token->location, "break statement not within loop or switch");
14569 break;
14570 default:
14571 gcc_assert ((in_statement & IN_SWITCH_STMT)
14572 || in_statement == IN_ITERATION_STMT);
14573 statement = finish_break_stmt ();
14574 if (in_statement == IN_ITERATION_STMT)
14575 break_maybe_infinite_loop ();
14576 break;
14577 case IN_OMP_BLOCK:
14578 error_at (token->location, "invalid exit from OpenMP structured block");
14579 break;
14580 case IN_OMP_FOR:
14581 error_at (token->location, "break statement used with OpenMP for loop");
14582 break;
14584 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14585 break;
14587 case RID_CONTINUE:
14588 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
14590 case 0:
14591 error_at (token->location, "continue statement not within a loop");
14592 break;
14593 /* Fall through. */
14594 case IN_ITERATION_STMT:
14595 case IN_OMP_FOR:
14596 statement = finish_continue_stmt ();
14597 break;
14598 case IN_OMP_BLOCK:
14599 error_at (token->location, "invalid exit from OpenMP structured block");
14600 break;
14601 default:
14602 gcc_unreachable ();
14604 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14605 break;
14607 case RID_CO_RETURN:
14608 case RID_RETURN:
14610 tree expr;
14612 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14614 cp_lexer_set_source_position (parser->lexer);
14615 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14616 expr = cp_parser_braced_list (parser);
14618 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14619 expr = cp_parser_expression (parser);
14620 else
14621 /* If the next token is a `;', then there is no
14622 expression. */
14623 expr = NULL_TREE;
14624 /* Build the return-statement, check co-return first, since type
14625 deduction is not valid there. */
14626 if (keyword == RID_CO_RETURN)
14627 statement = finish_co_return_stmt (token->location, expr);
14628 else if (FNDECL_USED_AUTO (current_function_decl) && in_discarded_stmt)
14629 /* Don't deduce from a discarded return statement. */;
14630 else
14631 statement = finish_return_stmt (expr);
14632 /* Look for the final `;'. */
14633 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14635 break;
14637 case RID_GOTO:
14638 if (parser->in_function_body
14639 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
14640 && cxx_dialect < cxx23)
14642 error ("%<goto%> in %<constexpr%> function only available with "
14643 "%<-std=c++2b%> or %<-std=gnu++2b%>");
14644 cp_function_chain->invalid_constexpr = true;
14647 /* Create the goto-statement. */
14648 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
14650 /* Issue a warning about this use of a GNU extension. */
14651 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
14652 /* Consume the '*' token. */
14653 cp_lexer_consume_token (parser->lexer);
14654 /* Parse the dependent expression. */
14655 finish_goto_stmt (cp_parser_expression (parser));
14657 else
14658 finish_goto_stmt (cp_parser_identifier (parser));
14659 /* Look for the final `;'. */
14660 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14661 break;
14663 default:
14664 cp_parser_error (parser, "expected jump-statement");
14665 break;
14668 return statement;
14671 /* Parse a declaration-statement.
14673 declaration-statement:
14674 block-declaration */
14676 static void
14677 cp_parser_declaration_statement (cp_parser* parser)
14679 void *p;
14681 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
14682 p = obstack_alloc (&declarator_obstack, 0);
14684 /* Parse the block-declaration. */
14685 cp_parser_block_declaration (parser, /*statement_p=*/true);
14687 /* Free any declarators allocated. */
14688 obstack_free (&declarator_obstack, p);
14691 /* Some dependent statements (like `if (cond) statement'), are
14692 implicitly in their own scope. In other words, if the statement is
14693 a single statement (as opposed to a compound-statement), it is
14694 none-the-less treated as if it were enclosed in braces. Any
14695 declarations appearing in the dependent statement are out of scope
14696 after control passes that point. This function parses a statement,
14697 but ensures that is in its own scope, even if it is not a
14698 compound-statement.
14700 If IF_P is not NULL, *IF_P is set to indicate whether the statement
14701 is a (possibly labeled) if statement which is not enclosed in
14702 braces and has an else clause. This is used to implement
14703 -Wparentheses.
14705 CHAIN is a vector of if-else-if conditions. This is used to implement
14706 -Wduplicated-cond.
14708 Returns the new statement. */
14710 static tree
14711 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
14712 const token_indent_info &guard_tinfo,
14713 vec<tree> *chain)
14715 tree statement;
14716 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
14717 location_t body_loc_after_labels = UNKNOWN_LOCATION;
14718 token_indent_info body_tinfo
14719 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
14721 if (if_p != NULL)
14722 *if_p = false;
14724 /* Mark if () ; with a special NOP_EXPR. */
14725 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14727 cp_lexer_consume_token (parser->lexer);
14728 statement = add_stmt (build_empty_stmt (body_loc));
14730 if (guard_tinfo.keyword == RID_IF
14731 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
14732 warning_at (body_loc, OPT_Wempty_body,
14733 "suggest braces around empty body in an %<if%> statement");
14734 else if (guard_tinfo.keyword == RID_ELSE)
14735 warning_at (body_loc, OPT_Wempty_body,
14736 "suggest braces around empty body in an %<else%> statement");
14738 /* if a compound is opened, we simply parse the statement directly. */
14739 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14740 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
14741 /* If the token is not a `{', then we must take special action. */
14742 else
14744 /* Create a compound-statement. */
14745 statement = begin_compound_stmt (0);
14746 /* Parse the dependent-statement. */
14747 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
14748 &body_loc_after_labels);
14749 /* Finish the dummy compound-statement. */
14750 finish_compound_stmt (statement);
14753 token_indent_info next_tinfo
14754 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
14755 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
14757 if (body_loc_after_labels != UNKNOWN_LOCATION
14758 && next_tinfo.type != CPP_SEMICOLON)
14759 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
14760 guard_tinfo.location, guard_tinfo.keyword);
14762 /* Return the statement. */
14763 return statement;
14766 /* For some dependent statements (like `while (cond) statement'), we
14767 have already created a scope. Therefore, even if the dependent
14768 statement is a compound-statement, we do not want to create another
14769 scope. */
14771 static void
14772 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
14773 const token_indent_info &guard_tinfo)
14775 /* If the token is a `{', then we must take special action. */
14776 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14778 token_indent_info body_tinfo
14779 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
14780 location_t loc_after_labels = UNKNOWN_LOCATION;
14782 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
14783 &loc_after_labels);
14784 token_indent_info next_tinfo
14785 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
14786 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
14788 if (loc_after_labels != UNKNOWN_LOCATION
14789 && next_tinfo.type != CPP_SEMICOLON)
14790 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
14791 guard_tinfo.location,
14792 guard_tinfo.keyword);
14794 else
14796 /* Avoid calling cp_parser_compound_statement, so that we
14797 don't create a new scope. Do everything else by hand. */
14798 matching_braces braces;
14799 braces.require_open (parser);
14800 /* If the next keyword is `__label__' we have a label declaration. */
14801 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
14802 cp_parser_label_declaration (parser);
14803 /* Parse an (optional) statement-seq. */
14804 cp_parser_statement_seq_opt (parser, NULL_TREE);
14805 braces.require_close (parser);
14809 /* Modules */
14811 /* Parse a module-name,
14812 identifier
14813 module-name . identifier
14814 header-name
14816 Returns a pointer to module object, NULL. */
14818 static module_state *
14819 cp_parser_module_name (cp_parser *parser)
14821 cp_token *token = cp_lexer_peek_token (parser->lexer);
14822 if (token->type == CPP_HEADER_NAME)
14824 cp_lexer_consume_token (parser->lexer);
14826 return get_module (token->u.value);
14829 module_state *parent = NULL;
14830 bool partitioned = false;
14831 if (token->type == CPP_COLON && named_module_p ())
14833 partitioned = true;
14834 cp_lexer_consume_token (parser->lexer);
14837 for (;;)
14839 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME)
14841 cp_parser_error (parser, "expected module-name");
14842 break;
14845 tree name = cp_lexer_consume_token (parser->lexer)->u.value;
14846 parent = get_module (name, parent, partitioned);
14847 token = cp_lexer_peek_token (parser->lexer);
14848 if (!partitioned && token->type == CPP_COLON)
14849 partitioned = true;
14850 else if (token->type != CPP_DOT)
14851 break;
14853 cp_lexer_consume_token (parser->lexer);
14856 return parent;
14859 /* Named module-declaration
14860 __module ; PRAGMA_EOL
14861 __module private ; PRAGMA_EOL (unimplemented)
14862 [__export] __module module-name attr-spec-seq-opt ; PRAGMA_EOL
14865 static module_parse
14866 cp_parser_module_declaration (cp_parser *parser, module_parse mp_state,
14867 bool exporting)
14869 /* We're a pseudo pragma. */
14870 parser->lexer->in_pragma = true;
14871 cp_token *token = cp_lexer_consume_token (parser->lexer);
14873 if (flag_header_unit)
14875 error_at (token->location,
14876 "module-declaration not permitted in header-unit");
14877 goto skip_eol;
14879 else if (mp_state == MP_FIRST && !exporting
14880 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14882 /* Start global module fragment. */
14883 cp_lexer_consume_token (parser->lexer);
14884 module_kind = MK_NAMED;
14885 mp_state = MP_GLOBAL;
14886 cp_parser_require_pragma_eol (parser, token);
14888 else if (!exporting
14889 && cp_lexer_next_token_is (parser->lexer, CPP_COLON)
14890 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_PRIVATE)
14891 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_SEMICOLON))
14893 cp_lexer_consume_token (parser->lexer);
14894 cp_lexer_consume_token (parser->lexer);
14895 cp_lexer_consume_token (parser->lexer);
14896 cp_parser_require_pragma_eol (parser, token);
14898 if ((mp_state == MP_PURVIEW || mp_state == MP_PURVIEW_IMPORTS)
14899 && module_has_cmi_p ())
14901 mp_state = MP_PRIVATE_IMPORTS;
14902 sorry_at (token->location, "private module fragment");
14904 else
14905 error_at (token->location,
14906 "private module fragment only permitted in purview"
14907 " of module interface or partition");
14909 else if (!(mp_state == MP_FIRST || mp_state == MP_GLOBAL))
14911 /* Neither the first declaration, nor in a GMF. */
14912 error_at (token->location, "module-declaration only permitted as first"
14913 " declaration, or ending a global module fragment");
14914 skip_eol:
14915 cp_parser_skip_to_pragma_eol (parser, token);
14917 else
14919 module_state *mod = cp_parser_module_name (parser);
14920 tree attrs = cp_parser_attributes_opt (parser);
14922 mp_state = MP_PURVIEW_IMPORTS;
14923 if (!mod || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
14924 goto skip_eol;
14926 declare_module (mod, token->location, exporting, attrs, parse_in);
14927 cp_parser_require_pragma_eol (parser, token);
14930 return mp_state;
14933 /* Import-declaration
14934 [__export] __import module-name attr-spec-seq-opt ; PRAGMA_EOL */
14936 static void
14937 cp_parser_import_declaration (cp_parser *parser, module_parse mp_state,
14938 bool exporting)
14940 /* We're a pseudo pragma. */
14941 parser->lexer->in_pragma = true;
14942 cp_token *token = cp_lexer_consume_token (parser->lexer);
14944 if (mp_state == MP_PURVIEW || mp_state == MP_PRIVATE)
14946 error_at (token->location, "post-module-declaration"
14947 " imports must be contiguous");
14948 note_lexer:
14949 inform (token->location, "perhaps insert a line break, or other"
14950 " disambiguation, to prevent this being considered a"
14951 " module control-line");
14952 skip_eol:
14953 cp_parser_skip_to_pragma_eol (parser, token);
14955 else if (current_scope () != global_namespace)
14957 error_at (token->location, "import-declaration must be at global scope");
14958 goto note_lexer;
14960 else
14962 module_state *mod = cp_parser_module_name (parser);
14963 tree attrs = cp_parser_attributes_opt (parser);
14965 if (!mod || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
14966 goto skip_eol;
14967 cp_parser_require_pragma_eol (parser, token);
14969 if (parser->in_unbraced_linkage_specification_p)
14970 error_at (token->location, "import cannot appear directly in"
14971 " a linkage-specification");
14973 if (mp_state == MP_PURVIEW_IMPORTS || mp_state == MP_PRIVATE_IMPORTS)
14975 /* Module-purview imports must not be from source inclusion
14976 [cpp.import]/7 */
14977 if (attrs
14978 && private_lookup_attribute ("__translated",
14979 strlen ("__translated"), attrs))
14980 error_at (token->location, "post-module-declaration imports"
14981 " must not be include-translated");
14982 else if (!token->main_source_p)
14983 error_at (token->location, "post-module-declaration imports"
14984 " must not be from header inclusion");
14987 import_module (mod, token->location, exporting, attrs, parse_in);
14991 /* export-declaration.
14993 export declaration
14994 export { declaration-seq-opt } */
14996 static void
14997 cp_parser_module_export (cp_parser *parser)
14999 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT));
15000 cp_token *token = cp_lexer_consume_token (parser->lexer);
15002 if (!module_interface_p ())
15003 error_at (token->location,
15004 "%qE may only occur after a module interface declaration",
15005 token->u.value);
15007 bool braced = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE);
15009 unsigned mk = module_kind;
15010 if (module_exporting_p ())
15011 error_at (token->location,
15012 "%qE may only occur once in an export declaration",
15013 token->u.value);
15014 module_kind |= MK_EXPORTING;
15016 if (braced)
15018 cp_ensure_no_omp_declare_simd (parser);
15019 cp_ensure_no_oacc_routine (parser);
15021 cp_lexer_consume_token (parser->lexer);
15022 cp_parser_declaration_seq_opt (parser);
15023 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15025 else
15027 /* Explicitly check if the next tokens might be a
15028 module-directive line, so we can give a clearer error message
15029 about why the directive will be rejected. */
15030 if (cp_lexer_next_token_is_keyword (parser->lexer, RID__MODULE)
15031 || cp_lexer_next_token_is_keyword (parser->lexer, RID__IMPORT)
15032 || cp_lexer_next_token_is_keyword (parser->lexer, RID__EXPORT))
15033 error_at (token->location, "%<export%> not part of following"
15034 " module-directive");
15035 cp_parser_declaration (parser, NULL_TREE);
15038 module_kind = mk;
15041 /* Declarations [gram.dcl.dcl] */
15043 /* Parse an optional declaration-sequence. TOP_LEVEL is true, if this
15044 is the top-level declaration sequence. That affects whether we
15045 deal with module-preamble.
15047 declaration-seq:
15048 declaration
15049 declaration-seq declaration */
15051 static void
15052 cp_parser_declaration_seq_opt (cp_parser* parser)
15054 while (true)
15056 cp_token *token = cp_lexer_peek_token (parser->lexer);
15058 if (token->type == CPP_CLOSE_BRACE
15059 || token->type == CPP_EOF)
15060 break;
15061 else
15062 cp_parser_toplevel_declaration (parser);
15066 /* Parse a declaration.
15068 declaration:
15069 block-declaration
15070 function-definition
15071 template-declaration
15072 explicit-instantiation
15073 explicit-specialization
15074 linkage-specification
15075 namespace-definition
15077 C++17:
15078 deduction-guide
15080 modules:
15081 (all these are only allowed at the outermost level, check
15082 that semantically, for better diagnostics)
15083 module-declaration
15084 module-export-declaration
15085 module-import-declaration
15086 export-declaration
15088 GNU extension:
15090 declaration:
15091 __extension__ declaration */
15093 static void
15094 cp_parser_declaration (cp_parser* parser, tree prefix_attrs)
15096 int saved_pedantic;
15098 /* Check for the `__extension__' keyword. */
15099 if (cp_parser_extension_opt (parser, &saved_pedantic))
15101 /* Parse the qualified declaration. */
15102 cp_parser_declaration (parser, prefix_attrs);
15103 /* Restore the PEDANTIC flag. */
15104 pedantic = saved_pedantic;
15106 return;
15109 /* Try to figure out what kind of declaration is present. */
15110 cp_token *token1 = cp_lexer_peek_token (parser->lexer);
15111 cp_token *token2 = (token1->type == CPP_EOF
15112 ? token1 : cp_lexer_peek_nth_token (parser->lexer, 2));
15114 if (token1->type == CPP_SEMICOLON)
15116 cp_lexer_consume_token (parser->lexer);
15117 /* A declaration consisting of a single semicolon is invalid
15118 * before C++11. Allow it unless we're being pedantic. */
15119 if (cxx_dialect < cxx11)
15120 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
15121 return;
15123 else if (cp_lexer_nth_token_is (parser->lexer,
15124 cp_parser_skip_std_attribute_spec_seq (parser,
15126 CPP_SEMICOLON))
15128 location_t attrs_loc = token1->location;
15129 tree std_attrs = cp_parser_std_attribute_spec_seq (parser);
15131 if (std_attrs && (flag_openmp || flag_openmp_simd))
15133 gcc_assert (!parser->lexer->in_omp_attribute_pragma);
15134 std_attrs = cp_parser_handle_statement_omp_attributes (parser,
15135 std_attrs);
15136 if (parser->lexer->in_omp_attribute_pragma)
15138 cp_lexer *lexer = parser->lexer;
15139 while (parser->lexer->in_omp_attribute_pragma)
15141 gcc_assert (cp_lexer_next_token_is (parser->lexer,
15142 CPP_PRAGMA));
15143 cp_parser_pragma (parser, pragma_external, NULL);
15145 cp_lexer_destroy (lexer);
15149 if (std_attrs != NULL_TREE && !attribute_ignored_p (std_attrs))
15150 warning_at (make_location (attrs_loc, attrs_loc, parser->lexer),
15151 OPT_Wattributes, "attribute ignored");
15152 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15153 cp_lexer_consume_token (parser->lexer);
15154 return;
15157 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
15158 void *p = obstack_alloc (&declarator_obstack, 0);
15160 tree attributes = NULL_TREE;
15162 /* Conditionally, allow attributes to precede a linkage specification. */
15163 if (token1->keyword == RID_ATTRIBUTE)
15165 cp_lexer_save_tokens (parser->lexer);
15166 attributes = cp_parser_attributes_opt (parser);
15167 cp_token *t1 = cp_lexer_peek_token (parser->lexer);
15168 cp_token *t2 = (t1->type == CPP_EOF
15169 ? t1 : cp_lexer_peek_nth_token (parser->lexer, 2));
15170 if (t1->keyword == RID_EXTERN
15171 && cp_parser_is_pure_string_literal (t2))
15173 cp_lexer_commit_tokens (parser->lexer);
15174 /* We might have already been here. */
15175 if (!c_dialect_objc ())
15177 location_t where = get_finish (t2->location);
15178 warning_at (token1->location, OPT_Wattributes, "attributes are"
15179 " not permitted in this position");
15180 where = linemap_position_for_loc_and_offset (line_table,
15181 where, 1);
15182 inform (where, "attributes may be inserted here");
15183 attributes = NULL_TREE;
15185 token1 = t1;
15186 token2 = t2;
15188 else
15190 cp_lexer_rollback_tokens (parser->lexer);
15191 attributes = NULL_TREE;
15194 /* If we already had some attributes, and we've added more, then prepend.
15195 Otherwise attributes just contains any that we just read. */
15196 if (prefix_attrs)
15198 if (attributes)
15199 TREE_CHAIN (prefix_attrs) = attributes;
15200 attributes = prefix_attrs;
15203 /* If the next token is `extern' and the following token is a string
15204 literal, then we have a linkage specification. */
15205 if (token1->keyword == RID_EXTERN
15206 && cp_parser_is_pure_string_literal (token2))
15207 cp_parser_linkage_specification (parser, attributes);
15208 /* If the next token is `template', then we have either a template
15209 declaration, an explicit instantiation, or an explicit
15210 specialization. */
15211 else if (token1->keyword == RID_TEMPLATE)
15213 /* `template <>' indicates a template specialization. */
15214 if (token2->type == CPP_LESS
15215 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
15216 cp_parser_explicit_specialization (parser);
15217 /* `template <' indicates a template declaration. */
15218 else if (token2->type == CPP_LESS)
15219 cp_parser_template_declaration (parser, /*member_p=*/false);
15220 /* Anything else must be an explicit instantiation. */
15221 else
15222 cp_parser_explicit_instantiation (parser);
15224 /* If the next token is `export', it's new-style modules or
15225 old-style template. */
15226 else if (token1->keyword == RID_EXPORT)
15228 if (!modules_p ())
15229 cp_parser_template_declaration (parser, /*member_p=*/false);
15230 else
15231 cp_parser_module_export (parser);
15233 else if (cp_token_is_module_directive (token1))
15235 bool exporting = token1->keyword == RID__EXPORT;
15236 cp_token *next = exporting ? token2 : token1;
15237 if (exporting)
15238 cp_lexer_consume_token (parser->lexer);
15239 // In module purview this will be ill-formed.
15240 auto state = (!named_module_p () ? MP_NOT_MODULE
15241 : module_purview_p () ? MP_PURVIEW
15242 : MP_GLOBAL);
15243 if (next->keyword == RID__MODULE)
15244 cp_parser_module_declaration (parser, state, exporting);
15245 else
15246 cp_parser_import_declaration (parser, state, exporting);
15248 /* If the next token is `extern', 'static' or 'inline' and the one
15249 after that is `template', we have a GNU extended explicit
15250 instantiation directive. */
15251 else if (cp_parser_allow_gnu_extensions_p (parser)
15252 && token2->keyword == RID_TEMPLATE
15253 && (token1->keyword == RID_EXTERN
15254 || token1->keyword == RID_STATIC
15255 || token1->keyword == RID_INLINE))
15256 cp_parser_explicit_instantiation (parser);
15257 /* If the next token is `namespace', check for a named or unnamed
15258 namespace definition. */
15259 else if (token1->keyword == RID_NAMESPACE
15260 && (/* A named namespace definition. */
15261 (token2->type == CPP_NAME
15262 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
15263 != CPP_EQ))
15264 || (token2->type == CPP_OPEN_SQUARE
15265 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
15266 == CPP_OPEN_SQUARE)
15267 /* An unnamed namespace definition. */
15268 || token2->type == CPP_OPEN_BRACE
15269 || token2->keyword == RID_ATTRIBUTE))
15270 cp_parser_namespace_definition (parser);
15271 /* An inline (associated) namespace definition. */
15272 else if (token2->keyword == RID_NAMESPACE
15273 && token1->keyword == RID_INLINE)
15274 cp_parser_namespace_definition (parser);
15275 /* Objective-C++ declaration/definition. */
15276 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1->keyword))
15277 cp_parser_objc_declaration (parser, attributes);
15278 else if (c_dialect_objc ()
15279 && token1->keyword == RID_ATTRIBUTE
15280 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
15281 cp_parser_objc_declaration (parser, attributes);
15282 /* At this point we may have a template declared by a concept
15283 introduction. */
15284 else if (flag_concepts
15285 && cp_parser_template_declaration_after_export (parser,
15286 /*member_p=*/false))
15287 /* We did. */;
15288 else
15289 /* Try to parse a block-declaration, or a function-definition. */
15290 cp_parser_block_declaration (parser, /*statement_p=*/false);
15292 /* Free any declarators allocated. */
15293 obstack_free (&declarator_obstack, p);
15296 /* Parse a namespace-scope declaration. */
15298 static void
15299 cp_parser_toplevel_declaration (cp_parser* parser)
15301 cp_token *token = cp_lexer_peek_token (parser->lexer);
15303 if (token->type == CPP_PRAGMA)
15304 /* A top-level declaration can consist solely of a #pragma. A
15305 nested declaration cannot, so this is done here and not in
15306 cp_parser_declaration. (A #pragma at block scope is
15307 handled in cp_parser_statement.) */
15308 cp_parser_pragma (parser, pragma_external, NULL);
15309 else
15310 /* Parse the declaration itself. */
15311 cp_parser_declaration (parser, NULL_TREE);
15314 /* Parse a block-declaration.
15316 block-declaration:
15317 simple-declaration
15318 asm-definition
15319 namespace-alias-definition
15320 using-declaration
15321 using-directive
15323 GNU Extension:
15325 block-declaration:
15326 __extension__ block-declaration
15328 C++0x Extension:
15330 block-declaration:
15331 static_assert-declaration
15333 If STATEMENT_P is TRUE, then this block-declaration is occurring as
15334 part of a declaration-statement. */
15336 static void
15337 cp_parser_block_declaration (cp_parser *parser,
15338 bool statement_p)
15340 int saved_pedantic;
15342 /* Check for the `__extension__' keyword. */
15343 if (cp_parser_extension_opt (parser, &saved_pedantic))
15345 /* Parse the qualified declaration. */
15346 cp_parser_block_declaration (parser, statement_p);
15347 /* Restore the PEDANTIC flag. */
15348 pedantic = saved_pedantic;
15350 return;
15353 /* Peek at the next token to figure out which kind of declaration is
15354 present. */
15355 cp_token *token1 = cp_lexer_peek_token (parser->lexer);
15356 size_t attr_idx;
15358 /* If the next keyword is `asm', we have an asm-definition. */
15359 if (token1->keyword == RID_ASM)
15361 if (statement_p)
15362 cp_parser_commit_to_tentative_parse (parser);
15363 cp_parser_asm_definition (parser);
15365 /* If the next keyword is `namespace', we have a
15366 namespace-alias-definition. */
15367 else if (token1->keyword == RID_NAMESPACE)
15368 cp_parser_namespace_alias_definition (parser);
15369 /* If the next keyword is `using', we have a
15370 using-declaration, a using-directive, or an alias-declaration. */
15371 else if (token1->keyword == RID_USING)
15373 cp_token *token2;
15375 if (statement_p)
15376 cp_parser_commit_to_tentative_parse (parser);
15377 /* If the token after `using' is `namespace', then we have a
15378 using-directive. */
15379 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
15380 if (token2->keyword == RID_NAMESPACE)
15381 cp_parser_using_directive (parser);
15382 else if (token2->keyword == RID_ENUM)
15383 cp_parser_using_enum (parser);
15384 /* If the second token after 'using' is '=', then we have an
15385 alias-declaration. */
15386 else if (cxx_dialect >= cxx11
15387 && token2->type == CPP_NAME
15388 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
15389 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
15390 cp_parser_alias_declaration (parser);
15391 /* Otherwise, it's a using-declaration. */
15392 else
15393 cp_parser_using_declaration (parser,
15394 /*access_declaration_p=*/false);
15396 /* If the next keyword is `__label__' we have a misplaced label
15397 declaration. */
15398 else if (token1->keyword == RID_LABEL)
15400 cp_lexer_consume_token (parser->lexer);
15401 error_at (token1->location, "%<__label__%> not at the beginning of a block");
15402 cp_parser_skip_to_end_of_statement (parser);
15403 /* If the next token is now a `;', consume it. */
15404 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15405 cp_lexer_consume_token (parser->lexer);
15407 /* If the next token is `static_assert' we have a static assertion. */
15408 else if (token1->keyword == RID_STATIC_ASSERT)
15409 cp_parser_static_assert (parser, /*member_p=*/false);
15410 /* If the next tokens after attributes is `using namespace', then we have
15411 a using-directive. */
15412 else if ((attr_idx = cp_parser_skip_std_attribute_spec_seq (parser, 1)) != 1
15413 && cp_lexer_nth_token_is_keyword (parser->lexer, attr_idx,
15414 RID_USING)
15415 && cp_lexer_nth_token_is_keyword (parser->lexer, attr_idx + 1,
15416 RID_NAMESPACE))
15418 if (statement_p)
15419 cp_parser_commit_to_tentative_parse (parser);
15420 cp_parser_using_directive (parser);
15422 /* Anything else must be a simple-declaration. */
15423 else
15424 cp_parser_simple_declaration (parser, !statement_p,
15425 /*maybe_range_for_decl*/NULL);
15428 /* Parse a simple-declaration.
15430 simple-declaration:
15431 decl-specifier-seq [opt] init-declarator-list [opt] ;
15432 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
15433 brace-or-equal-initializer ;
15435 init-declarator-list:
15436 init-declarator
15437 init-declarator-list , init-declarator
15439 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
15440 function-definition as a simple-declaration.
15442 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
15443 parsed declaration if it is an uninitialized single declarator not followed
15444 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
15445 if present, will not be consumed. */
15447 static void
15448 cp_parser_simple_declaration (cp_parser* parser,
15449 bool function_definition_allowed_p,
15450 tree *maybe_range_for_decl)
15452 cp_decl_specifier_seq decl_specifiers;
15453 int declares_class_or_enum;
15454 bool saw_declarator;
15455 location_t comma_loc = UNKNOWN_LOCATION;
15456 location_t init_loc = UNKNOWN_LOCATION;
15458 if (maybe_range_for_decl)
15459 *maybe_range_for_decl = NULL_TREE;
15461 /* Defer access checks until we know what is being declared; the
15462 checks for names appearing in the decl-specifier-seq should be
15463 done as if we were in the scope of the thing being declared. */
15464 push_deferring_access_checks (dk_deferred);
15466 /* Parse the decl-specifier-seq. We have to keep track of whether
15467 or not the decl-specifier-seq declares a named class or
15468 enumeration type, since that is the only case in which the
15469 init-declarator-list is allowed to be empty.
15471 [dcl.dcl]
15473 In a simple-declaration, the optional init-declarator-list can be
15474 omitted only when declaring a class or enumeration, that is when
15475 the decl-specifier-seq contains either a class-specifier, an
15476 elaborated-type-specifier, or an enum-specifier. */
15477 cp_parser_decl_specifier_seq (parser,
15478 CP_PARSER_FLAGS_OPTIONAL,
15479 &decl_specifiers,
15480 &declares_class_or_enum);
15481 /* We no longer need to defer access checks. */
15482 stop_deferring_access_checks ();
15484 cp_omp_declare_simd_data odsd;
15485 if (decl_specifiers.attributes && (flag_openmp || flag_openmp_simd))
15486 cp_parser_handle_directive_omp_attributes (parser,
15487 &decl_specifiers.attributes,
15488 &odsd, true);
15490 /* In a block scope, a valid declaration must always have a
15491 decl-specifier-seq. By not trying to parse declarators, we can
15492 resolve the declaration/expression ambiguity more quickly. */
15493 if (!function_definition_allowed_p
15494 && !decl_specifiers.any_specifiers_p)
15496 cp_parser_error (parser, "expected declaration");
15497 goto done;
15500 /* If the next two tokens are both identifiers, the code is
15501 erroneous. The usual cause of this situation is code like:
15503 T t;
15505 where "T" should name a type -- but does not. */
15506 if (!decl_specifiers.any_type_specifiers_p
15507 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15509 /* If parsing tentatively, we should commit; we really are
15510 looking at a declaration. */
15511 cp_parser_commit_to_tentative_parse (parser);
15512 /* Give up. */
15513 goto done;
15516 cp_parser_maybe_commit_to_declaration (parser, &decl_specifiers);
15518 /* Look for C++17 decomposition declaration. */
15519 for (size_t n = 1; ; n++)
15520 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
15521 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
15522 continue;
15523 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
15524 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
15525 && decl_specifiers.any_specifiers_p)
15527 tree decl
15528 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
15529 maybe_range_for_decl,
15530 &init_loc);
15532 /* The next token should be either a `,' or a `;'. */
15533 cp_token *token = cp_lexer_peek_token (parser->lexer);
15534 /* If it's a `;', we are done. */
15535 if (token->type == CPP_SEMICOLON)
15536 goto finish;
15537 else if (maybe_range_for_decl)
15539 if (*maybe_range_for_decl == NULL_TREE)
15540 *maybe_range_for_decl = error_mark_node;
15541 goto finish;
15543 /* Anything else is an error. */
15544 else
15546 /* If we have already issued an error message we don't need
15547 to issue another one. */
15548 if ((decl != error_mark_node
15549 && DECL_INITIAL (decl) != error_mark_node)
15550 || cp_parser_uncommitted_to_tentative_parse_p (parser))
15551 cp_parser_error (parser, "expected %<;%>");
15552 /* Skip tokens until we reach the end of the statement. */
15553 cp_parser_skip_to_end_of_statement (parser);
15554 /* If the next token is now a `;', consume it. */
15555 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15556 cp_lexer_consume_token (parser->lexer);
15557 goto done;
15560 else
15561 break;
15563 tree last_type;
15564 bool auto_specifier_p;
15565 /* NULL_TREE if both variable and function declaration are allowed,
15566 error_mark_node if function declaration are not allowed and
15567 a FUNCTION_DECL that should be diagnosed if it is followed by
15568 variable declarations. */
15569 tree auto_function_declaration;
15571 last_type = NULL_TREE;
15572 auto_specifier_p
15573 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
15574 auto_function_declaration = NULL_TREE;
15576 /* Keep going until we hit the `;' at the end of the simple
15577 declaration. */
15578 saw_declarator = false;
15579 while (cp_lexer_next_token_is_not (parser->lexer,
15580 CPP_SEMICOLON))
15582 cp_token *token;
15583 bool function_definition_p;
15584 tree decl;
15585 tree auto_result = NULL_TREE;
15587 if (saw_declarator)
15589 /* If we are processing next declarator, comma is expected */
15590 token = cp_lexer_peek_token (parser->lexer);
15591 gcc_assert (token->type == CPP_COMMA);
15592 cp_lexer_consume_token (parser->lexer);
15593 if (maybe_range_for_decl)
15595 *maybe_range_for_decl = error_mark_node;
15596 if (comma_loc == UNKNOWN_LOCATION)
15597 comma_loc = token->location;
15600 else
15601 saw_declarator = true;
15603 /* Parse the init-declarator. */
15604 decl = cp_parser_init_declarator (parser,
15605 CP_PARSER_FLAGS_NONE,
15606 &decl_specifiers,
15607 /*checks=*/NULL,
15608 function_definition_allowed_p,
15609 /*member_p=*/false,
15610 declares_class_or_enum,
15611 &function_definition_p,
15612 maybe_range_for_decl,
15613 &init_loc,
15614 &auto_result);
15615 /* If an error occurred while parsing tentatively, exit quickly.
15616 (That usually happens when in the body of a function; each
15617 statement is treated as a declaration-statement until proven
15618 otherwise.) */
15619 if (cp_parser_error_occurred (parser))
15620 goto done;
15622 if (auto_specifier_p && cxx_dialect >= cxx14)
15624 /* If the init-declarator-list contains more than one
15625 init-declarator, they shall all form declarations of
15626 variables. */
15627 if (auto_function_declaration == NULL_TREE)
15628 auto_function_declaration
15629 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
15630 else if (TREE_CODE (decl) == FUNCTION_DECL
15631 || auto_function_declaration != error_mark_node)
15633 error_at (decl_specifiers.locations[ds_type_spec],
15634 "non-variable %qD in declaration with more than one "
15635 "declarator with placeholder type",
15636 TREE_CODE (decl) == FUNCTION_DECL
15637 ? decl : auto_function_declaration);
15638 auto_function_declaration = error_mark_node;
15642 if (auto_result
15643 && (!processing_template_decl || !type_uses_auto (auto_result)))
15645 if (last_type
15646 && last_type != error_mark_node
15647 && !same_type_p (auto_result, last_type))
15649 /* If the list of declarators contains more than one declarator,
15650 the type of each declared variable is determined as described
15651 above. If the type deduced for the template parameter U is not
15652 the same in each deduction, the program is ill-formed. */
15653 error_at (decl_specifiers.locations[ds_type_spec],
15654 "inconsistent deduction for %qT: %qT and then %qT",
15655 decl_specifiers.type, last_type, auto_result);
15656 last_type = error_mark_node;
15658 else
15659 last_type = auto_result;
15662 /* Handle function definitions specially. */
15663 if (function_definition_p)
15665 /* If the next token is a `,', then we are probably
15666 processing something like:
15668 void f() {}, *p;
15670 which is erroneous. */
15671 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15673 cp_token *token = cp_lexer_peek_token (parser->lexer);
15674 error_at (token->location,
15675 "mixing"
15676 " declarations and function-definitions is forbidden");
15678 /* Otherwise, we're done with the list of declarators. */
15679 else
15681 pop_deferring_access_checks ();
15682 cp_finalize_omp_declare_simd (parser, &odsd);
15683 return;
15686 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
15687 *maybe_range_for_decl = decl;
15688 /* The next token should be either a `,' or a `;'. */
15689 token = cp_lexer_peek_token (parser->lexer);
15690 /* If it's a `,', there are more declarators to come. */
15691 if (token->type == CPP_COMMA)
15692 /* will be consumed next time around */;
15693 /* If it's a `;', we are done. */
15694 else if (token->type == CPP_SEMICOLON)
15695 break;
15696 else if (maybe_range_for_decl)
15698 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
15699 permerror (decl_specifiers.locations[ds_type_spec],
15700 "types may not be defined in a for-range-declaration");
15701 break;
15703 /* Anything else is an error. */
15704 else
15706 /* If we have already issued an error message we don't need
15707 to issue another one. */
15708 if ((decl != error_mark_node
15709 && DECL_INITIAL (decl) != error_mark_node)
15710 || cp_parser_uncommitted_to_tentative_parse_p (parser))
15711 cp_parser_error (parser, "expected %<,%> or %<;%>");
15712 /* Skip tokens until we reach the end of the statement. */
15713 cp_parser_skip_to_end_of_statement (parser);
15714 /* If the next token is now a `;', consume it. */
15715 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15716 cp_lexer_consume_token (parser->lexer);
15717 goto done;
15719 /* After the first time around, a function-definition is not
15720 allowed -- even if it was OK at first. For example:
15722 int i, f() {}
15724 is not valid. */
15725 function_definition_allowed_p = false;
15728 /* Issue an error message if no declarators are present, and the
15729 decl-specifier-seq does not itself declare a class or
15730 enumeration: [dcl.dcl]/3. */
15731 if (!saw_declarator)
15733 if (cp_parser_declares_only_class_p (parser))
15735 if (!declares_class_or_enum
15736 && decl_specifiers.type
15737 && OVERLOAD_TYPE_P (decl_specifiers.type))
15738 /* Ensure an error is issued anyway when finish_decltype_type,
15739 called via cp_parser_decl_specifier_seq, returns a class or
15740 an enumeration (c++/51786). */
15741 decl_specifiers.type = NULL_TREE;
15742 shadow_tag (&decl_specifiers);
15744 /* Perform any deferred access checks. */
15745 perform_deferred_access_checks (tf_warning_or_error);
15748 /* Consume the `;'. */
15749 finish:
15750 if (!maybe_range_for_decl)
15751 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15752 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15754 if (init_loc != UNKNOWN_LOCATION)
15755 error_at (init_loc, "initializer in range-based %<for%> loop");
15756 if (comma_loc != UNKNOWN_LOCATION)
15757 error_at (comma_loc,
15758 "multiple declarations in range-based %<for%> loop");
15761 done:
15762 pop_deferring_access_checks ();
15763 cp_finalize_omp_declare_simd (parser, &odsd);
15766 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
15767 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
15768 initializer ; */
15770 static tree
15771 cp_parser_decomposition_declaration (cp_parser *parser,
15772 cp_decl_specifier_seq *decl_specifiers,
15773 tree *maybe_range_for_decl,
15774 location_t *init_loc)
15776 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
15777 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
15778 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
15780 /* Parse the identifier-list. */
15781 auto_vec<cp_expr, 10> v;
15782 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
15783 while (true)
15785 cp_expr e = cp_parser_identifier (parser);
15786 if (e.get_value () == error_mark_node)
15787 break;
15788 v.safe_push (e);
15789 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15790 break;
15791 cp_lexer_consume_token (parser->lexer);
15794 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
15795 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15797 end_loc = UNKNOWN_LOCATION;
15798 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
15799 false);
15800 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
15801 cp_lexer_consume_token (parser->lexer);
15802 else
15804 cp_parser_skip_to_end_of_statement (parser);
15805 return error_mark_node;
15809 if (cxx_dialect < cxx17)
15810 pedwarn (loc, OPT_Wc__17_extensions,
15811 "structured bindings only available with "
15812 "%<-std=c++17%> or %<-std=gnu++17%>");
15814 tree pushed_scope;
15815 cp_declarator *declarator = make_declarator (cdk_decomp);
15816 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
15817 declarator->id_loc = loc;
15818 if (ref_qual != REF_QUAL_NONE)
15819 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
15820 ref_qual == REF_QUAL_RVALUE,
15821 NULL_TREE);
15822 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
15823 NULL_TREE, decl_specifiers->attributes,
15824 &pushed_scope);
15825 tree orig_decl = decl;
15827 unsigned int i;
15828 cp_expr e;
15829 cp_decl_specifier_seq decl_specs;
15830 clear_decl_specs (&decl_specs);
15831 decl_specs.type = make_auto ();
15832 tree prev = decl;
15833 FOR_EACH_VEC_ELT (v, i, e)
15835 if (i == 0)
15836 declarator = make_id_declarator (NULL_TREE, e.get_value (),
15837 sfk_none, e.get_location ());
15838 else
15840 declarator->u.id.unqualified_name = e.get_value ();
15841 declarator->id_loc = e.get_location ();
15843 tree elt_pushed_scope;
15844 tree decl2 = start_decl (declarator, &decl_specs, SD_DECOMPOSITION,
15845 NULL_TREE, NULL_TREE, &elt_pushed_scope);
15846 if (decl2 == error_mark_node)
15847 decl = error_mark_node;
15848 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
15850 /* Ensure we've diagnosed redeclaration if we aren't creating
15851 a new VAR_DECL. */
15852 gcc_assert (errorcount);
15853 decl = error_mark_node;
15855 else
15856 prev = decl2;
15857 if (elt_pushed_scope)
15858 pop_scope (elt_pushed_scope);
15861 if (v.is_empty ())
15863 error_at (loc, "empty structured binding declaration");
15864 decl = error_mark_node;
15867 if (maybe_range_for_decl == NULL
15868 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
15870 bool non_constant_p = false, is_direct_init = false;
15871 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
15872 tree initializer = cp_parser_initializer (parser, &is_direct_init,
15873 &non_constant_p);
15874 if (initializer == NULL_TREE
15875 || (TREE_CODE (initializer) == TREE_LIST
15876 && TREE_CHAIN (initializer))
15877 || (is_direct_init
15878 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
15879 && CONSTRUCTOR_NELTS (initializer) != 1))
15881 error_at (loc, "invalid initializer for structured binding "
15882 "declaration");
15883 initializer = error_mark_node;
15886 if (decl != error_mark_node)
15888 cp_decomp decomp = { prev, v.length () };
15889 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
15890 (is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT),
15891 &decomp);
15892 cp_finish_decomp (decl, &decomp);
15895 else if (decl != error_mark_node)
15897 *maybe_range_for_decl = prev;
15898 cp_decomp decomp = { prev, v.length () };
15899 /* Ensure DECL_VALUE_EXPR is created for all the decls but
15900 the underlying DECL. */
15901 cp_finish_decomp (decl, &decomp);
15904 if (pushed_scope)
15905 pop_scope (pushed_scope);
15907 if (decl == error_mark_node && DECL_P (orig_decl))
15909 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
15910 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
15913 return decl;
15916 /* Names of storage classes. */
15918 static const char *const
15919 cp_storage_class_name[] = {
15920 "", "auto", "register", "static", "extern", "mutable"
15923 /* Parse a decl-specifier-seq.
15925 decl-specifier-seq:
15926 decl-specifier-seq [opt] decl-specifier
15927 decl-specifier attribute-specifier-seq [opt] (C++11)
15929 decl-specifier:
15930 storage-class-specifier
15931 type-specifier
15932 function-specifier
15933 friend
15934 typedef
15936 GNU Extension:
15938 decl-specifier:
15939 attributes
15941 Concepts Extension:
15943 decl-specifier:
15944 concept
15946 Set *DECL_SPECS to a representation of the decl-specifier-seq.
15948 The parser flags FLAGS is used to control type-specifier parsing.
15950 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
15951 flags:
15953 1: one of the decl-specifiers is an elaborated-type-specifier
15954 (i.e., a type declaration)
15955 2: one of the decl-specifiers is an enum-specifier or a
15956 class-specifier (i.e., a type definition)
15960 static void
15961 cp_parser_decl_specifier_seq (cp_parser* parser,
15962 cp_parser_flags flags,
15963 cp_decl_specifier_seq *decl_specs,
15964 int* declares_class_or_enum)
15966 bool constructor_possible_p = !parser->in_declarator_p;
15967 bool found_decl_spec = false;
15968 cp_token *start_token = NULL;
15969 cp_decl_spec ds;
15971 /* Clear DECL_SPECS. */
15972 clear_decl_specs (decl_specs);
15974 /* Assume no class or enumeration type is declared. */
15975 *declares_class_or_enum = 0;
15977 /* Keep reading specifiers until there are no more to read. */
15978 while (true)
15980 bool constructor_p;
15981 cp_token *token;
15982 ds = ds_last;
15984 /* Peek at the next token. */
15985 token = cp_lexer_peek_token (parser->lexer);
15987 /* Save the first token of the decl spec list for error
15988 reporting. */
15989 if (!start_token)
15990 start_token = token;
15991 /* Handle attributes. */
15992 if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) == 0
15993 && cp_next_tokens_can_be_attribute_p (parser))
15995 /* Parse the attributes. */
15996 tree attrs = cp_parser_attributes_opt (parser);
15998 /* In a sequence of declaration specifiers, c++11 attributes
15999 appertain to the type that precede them. In that case
16000 [dcl.spec]/1 says:
16002 The attribute-specifier-seq affects the type only for
16003 the declaration it appears in, not other declarations
16004 involving the same type.
16006 But for now let's force the user to position the
16007 attribute either at the beginning of the declaration or
16008 after the declarator-id, which would clearly mean that it
16009 applies to the declarator. */
16010 if (cxx11_attribute_p (attrs))
16012 if (!found_decl_spec)
16013 /* The c++11 attribute is at the beginning of the
16014 declaration. It appertains to the entity being
16015 declared. */;
16016 else
16018 if (find_contract (attrs))
16020 diagnose_misapplied_contracts (attrs);
16021 attrs = NULL_TREE;
16023 else if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
16025 /* This is an attribute following a
16026 class-specifier. */
16027 if (decl_specs->type_definition_p)
16028 warn_misplaced_attr_for_class_type (token->location,
16029 decl_specs->type);
16030 attrs = NULL_TREE;
16032 else
16034 decl_specs->std_attributes
16035 = attr_chainon (decl_specs->std_attributes, attrs);
16036 if (decl_specs->locations[ds_std_attribute] == 0)
16037 decl_specs->locations[ds_std_attribute] = token->location;
16039 continue;
16043 decl_specs->attributes
16044 = attr_chainon (decl_specs->attributes, attrs);
16045 if (decl_specs->locations[ds_attribute] == 0)
16046 decl_specs->locations[ds_attribute] = token->location;
16047 continue;
16049 /* Assume we will find a decl-specifier keyword. */
16050 found_decl_spec = true;
16051 /* If the next token is an appropriate keyword, we can simply
16052 add it to the list. */
16053 switch (token->keyword)
16055 /* decl-specifier:
16056 friend
16057 constexpr
16058 constinit */
16059 case RID_FRIEND:
16060 if (!at_class_scope_p ())
16062 gcc_rich_location richloc (token->location);
16063 richloc.add_fixit_remove ();
16064 error_at (&richloc, "%<friend%> used outside of class");
16065 cp_lexer_purge_token (parser->lexer);
16067 else
16069 ds = ds_friend;
16070 /* Consume the token. */
16071 cp_lexer_consume_token (parser->lexer);
16073 break;
16075 case RID_CONSTEXPR:
16076 ds = ds_constexpr;
16077 cp_lexer_consume_token (parser->lexer);
16078 break;
16080 case RID_CONSTINIT:
16081 ds = ds_constinit;
16082 cp_lexer_consume_token (parser->lexer);
16083 break;
16085 case RID_CONSTEVAL:
16086 ds = ds_consteval;
16087 cp_lexer_consume_token (parser->lexer);
16088 break;
16090 case RID_CONCEPT:
16091 ds = ds_concept;
16092 cp_lexer_consume_token (parser->lexer);
16094 if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
16095 break;
16097 /* Warn for concept as a decl-specifier. We'll rewrite these as
16098 concept declarations later. */
16099 if (!flag_concepts_ts)
16101 cp_token *next = cp_lexer_peek_token (parser->lexer);
16102 if (next->keyword == RID_BOOL)
16103 permerror (next->location, "the %<bool%> keyword is not "
16104 "allowed in a C++20 concept definition");
16105 else
16106 error_at (token->location, "C++20 concept definition syntax "
16107 "is %<concept <name> = <expr>%>");
16110 /* In C++20 a concept definition is just 'concept name = expr;'
16111 Support that syntax as a TS extension by pretending we've seen
16112 the 'bool' specifier. */
16113 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
16114 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ)
16115 && !decl_specs->any_type_specifiers_p)
16117 cp_parser_set_decl_spec_type (decl_specs, boolean_type_node,
16118 token, /*type_definition*/false);
16119 decl_specs->any_type_specifiers_p = true;
16121 break;
16123 /* function-specifier:
16124 inline
16125 virtual
16126 explicit */
16127 case RID_INLINE:
16128 case RID_VIRTUAL:
16129 case RID_EXPLICIT:
16130 cp_parser_function_specifier_opt (parser, decl_specs);
16131 break;
16133 /* decl-specifier:
16134 typedef */
16135 case RID_TYPEDEF:
16136 ds = ds_typedef;
16137 /* Consume the token. */
16138 cp_lexer_consume_token (parser->lexer);
16140 if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
16141 break;
16143 /* A constructor declarator cannot appear in a typedef. */
16144 constructor_possible_p = false;
16145 /* The "typedef" keyword can only occur in a declaration; we
16146 may as well commit at this point. */
16147 cp_parser_commit_to_tentative_parse (parser);
16149 if (decl_specs->storage_class != sc_none)
16151 if (decl_specs->conflicting_specifiers_p)
16152 break;
16153 gcc_rich_location richloc (token->location);
16154 location_t oloc = decl_specs->locations[ds_storage_class];
16155 richloc.add_location_if_nearby (oloc);
16156 error_at (&richloc,
16157 "%<typedef%> specifier conflicts with %qs",
16158 cp_storage_class_name[decl_specs->storage_class]);
16159 decl_specs->conflicting_specifiers_p = true;
16161 break;
16163 /* storage-class-specifier:
16164 auto
16165 register
16166 static
16167 extern
16168 mutable
16170 GNU Extension:
16171 thread */
16172 case RID_AUTO:
16173 if (cxx_dialect == cxx98)
16175 /* Consume the token. */
16176 cp_lexer_consume_token (parser->lexer);
16178 /* Complain about `auto' as a storage specifier, if
16179 we're complaining about C++0x compatibility. */
16180 gcc_rich_location richloc (token->location);
16181 richloc.add_fixit_remove ();
16182 warning_at (&richloc, OPT_Wc__11_compat,
16183 "%<auto%> changes meaning in C++11; "
16184 "please remove it");
16186 /* Set the storage class anyway. */
16187 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
16188 token);
16190 else
16191 /* C++0x auto type-specifier. */
16192 found_decl_spec = false;
16193 break;
16195 case RID_REGISTER:
16196 case RID_STATIC:
16197 case RID_EXTERN:
16198 case RID_MUTABLE:
16199 /* Consume the token. */
16200 cp_lexer_consume_token (parser->lexer);
16201 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
16202 token);
16203 break;
16204 case RID_THREAD:
16205 /* Consume the token. */
16206 ds = ds_thread;
16207 cp_lexer_consume_token (parser->lexer);
16208 break;
16210 default:
16211 /* We did not yet find a decl-specifier yet. */
16212 found_decl_spec = false;
16213 break;
16216 if (found_decl_spec
16217 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
16218 && token->keyword != RID_CONSTEXPR)
16219 error ("%qD invalid in condition", ridpointers[token->keyword]);
16221 if (found_decl_spec
16222 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
16223 && token->keyword != RID_MUTABLE
16224 && token->keyword != RID_CONSTEXPR
16225 && token->keyword != RID_CONSTEVAL)
16227 if (token->keyword != RID_STATIC)
16228 error_at (token->location, "%qD invalid in lambda",
16229 ridpointers[token->keyword]);
16230 else if (cxx_dialect < cxx23)
16231 pedwarn (token->location, OPT_Wc__23_extensions,
16232 "%qD only valid in lambda with %<-std=c++23%> or "
16233 "%<-std=gnu++23%>", ridpointers[token->keyword]);
16236 if (ds != ds_last)
16237 set_and_check_decl_spec_loc (decl_specs, ds, token);
16239 /* Constructors are a special case. The `S' in `S()' is not a
16240 decl-specifier; it is the beginning of the declarator. */
16241 constructor_p
16242 = (!found_decl_spec
16243 && constructor_possible_p
16244 && (cp_parser_constructor_declarator_p
16245 (parser, flags, decl_spec_seq_has_spec_p (decl_specs,
16246 ds_friend))));
16248 /* If we don't have a DECL_SPEC yet, then we must be looking at
16249 a type-specifier. */
16250 if (!found_decl_spec && !constructor_p)
16252 int decl_spec_declares_class_or_enum;
16253 bool is_cv_qualifier;
16254 tree type_spec;
16256 if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
16257 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
16259 type_spec
16260 = cp_parser_type_specifier (parser, flags,
16261 decl_specs,
16262 /*is_declaration=*/true,
16263 &decl_spec_declares_class_or_enum,
16264 &is_cv_qualifier);
16265 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
16267 /* If this type-specifier referenced a user-defined type
16268 (a typedef, class-name, etc.), then we can't allow any
16269 more such type-specifiers henceforth.
16271 [dcl.spec]
16273 The longest sequence of decl-specifiers that could
16274 possibly be a type name is taken as the
16275 decl-specifier-seq of a declaration. The sequence shall
16276 be self-consistent as described below.
16278 [dcl.type]
16280 As a general rule, at most one type-specifier is allowed
16281 in the complete decl-specifier-seq of a declaration. The
16282 only exceptions are the following:
16284 -- const or volatile can be combined with any other
16285 type-specifier.
16287 -- signed or unsigned can be combined with char, long,
16288 short, or int.
16290 -- ..
16292 Example:
16294 typedef char* Pc;
16295 void g (const int Pc);
16297 Here, Pc is *not* part of the decl-specifier seq; it's
16298 the declarator. Therefore, once we see a type-specifier
16299 (other than a cv-qualifier), we forbid any additional
16300 user-defined types. We *do* still allow things like `int
16301 int' to be considered a decl-specifier-seq, and issue the
16302 error message later. */
16303 if (type_spec && !is_cv_qualifier)
16304 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
16305 /* A constructor declarator cannot follow a type-specifier. */
16306 if (type_spec)
16308 constructor_possible_p = false;
16309 found_decl_spec = true;
16310 if (!is_cv_qualifier)
16311 decl_specs->any_type_specifiers_p = true;
16313 if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) != 0)
16314 error_at (token->location, "type-specifier invalid in lambda");
16318 /* If we still do not have a DECL_SPEC, then there are no more
16319 decl-specifiers. */
16320 if (!found_decl_spec)
16321 break;
16323 if (decl_specs->std_attributes)
16325 error_at (decl_specs->locations[ds_std_attribute],
16326 "standard attributes in middle of decl-specifiers");
16327 inform (decl_specs->locations[ds_std_attribute],
16328 "standard attributes must precede the decl-specifiers to "
16329 "apply to the declaration, or follow them to apply to "
16330 "the type");
16333 decl_specs->any_specifiers_p = true;
16334 /* After we see one decl-specifier, further decl-specifiers are
16335 always optional. */
16336 flags |= CP_PARSER_FLAGS_OPTIONAL;
16339 /* Don't allow a friend specifier with a class definition. */
16340 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
16341 && (*declares_class_or_enum & 2))
16342 error_at (decl_specs->locations[ds_friend],
16343 "class definition may not be declared a friend");
16346 /* Parse an (optional) storage-class-specifier.
16348 storage-class-specifier:
16349 auto
16350 register
16351 static
16352 extern
16353 mutable
16355 GNU Extension:
16357 storage-class-specifier:
16358 thread
16360 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
16362 static tree
16363 cp_parser_storage_class_specifier_opt (cp_parser* parser)
16365 switch (cp_lexer_peek_token (parser->lexer)->keyword)
16367 case RID_AUTO:
16368 if (cxx_dialect != cxx98)
16369 return NULL_TREE;
16370 /* Fall through for C++98. */
16371 gcc_fallthrough ();
16373 case RID_REGISTER:
16374 case RID_STATIC:
16375 case RID_EXTERN:
16376 case RID_MUTABLE:
16377 case RID_THREAD:
16378 /* Consume the token. */
16379 return cp_lexer_consume_token (parser->lexer)->u.value;
16381 default:
16382 return NULL_TREE;
16386 /* Parse an (optional) function-specifier.
16388 function-specifier:
16389 inline
16390 virtual
16391 explicit
16393 C++20 Extension:
16394 explicit(constant-expression)
16396 Returns an IDENTIFIER_NODE corresponding to the keyword used.
16397 Updates DECL_SPECS, if it is non-NULL. */
16399 static tree
16400 cp_parser_function_specifier_opt (cp_parser* parser,
16401 cp_decl_specifier_seq *decl_specs)
16403 cp_token *token = cp_lexer_peek_token (parser->lexer);
16404 switch (token->keyword)
16406 case RID_INLINE:
16407 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
16408 break;
16410 case RID_VIRTUAL:
16411 /* 14.5.2.3 [temp.mem]
16413 A member function template shall not be virtual. */
16414 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
16415 && current_class_type)
16416 error_at (token->location, "templates may not be %<virtual%>");
16417 else
16418 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
16419 break;
16421 case RID_EXPLICIT:
16423 tree id = cp_lexer_consume_token (parser->lexer)->u.value;
16424 /* If we see '(', it's C++20 explicit(bool). */
16425 tree expr;
16426 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16428 matching_parens parens;
16429 parens.consume_open (parser);
16431 /* New types are not allowed in an explicit-specifier. */
16432 const char *saved_message
16433 = parser->type_definition_forbidden_message;
16434 parser->type_definition_forbidden_message
16435 = G_("types may not be defined in explicit-specifier");
16437 if (cxx_dialect < cxx20)
16438 pedwarn (token->location, OPT_Wc__20_extensions,
16439 "%<explicit(bool)%> only available with %<-std=c++20%> "
16440 "or %<-std=gnu++20%>");
16442 /* Parse the constant-expression. */
16443 expr = cp_parser_constant_expression (parser);
16445 /* Restore the saved message. */
16446 parser->type_definition_forbidden_message = saved_message;
16447 parens.require_close (parser);
16449 else
16450 /* The explicit-specifier explicit without a constant-expression is
16451 equivalent to the explicit-specifier explicit(true). */
16452 expr = boolean_true_node;
16454 /* [dcl.fct.spec]
16455 "the constant-expression, if supplied, shall be a contextually
16456 converted constant expression of type bool." */
16457 expr = build_explicit_specifier (expr, tf_warning_or_error);
16458 /* We could evaluate it -- mark the decl as appropriate. */
16459 if (expr == boolean_true_node)
16460 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
16461 else if (expr == boolean_false_node)
16462 /* Don't mark the decl as explicit. */;
16463 else if (decl_specs)
16464 /* The expression was value-dependent. Remember it so that we can
16465 substitute it later. */
16466 decl_specs->explicit_specifier = expr;
16467 return id;
16470 default:
16471 return NULL_TREE;
16474 /* Consume the token. */
16475 return cp_lexer_consume_token (parser->lexer)->u.value;
16478 /* Parse a linkage-specification.
16480 linkage-specification:
16481 extern string-literal { declaration-seq [opt] }
16482 extern string-literal declaration */
16484 static void
16485 cp_parser_linkage_specification (cp_parser* parser, tree prefix_attr)
16487 /* Look for the `extern' keyword. */
16488 cp_token *extern_token
16489 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
16491 /* Look for the string-literal. */
16492 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
16493 tree linkage = cp_parser_string_literal (parser, /*translate=*/false,
16494 /*wide_ok=*/false);
16496 /* Transform the literal into an identifier. If the literal is a
16497 wide-character string, or contains embedded NULs, then we can't
16498 handle it as the user wants. */
16499 if (linkage == error_mark_node
16500 || strlen (TREE_STRING_POINTER (linkage))
16501 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
16503 cp_parser_error (parser, "invalid linkage-specification");
16504 /* Assume C++ linkage. */
16505 linkage = lang_name_cplusplus;
16507 else
16508 linkage = get_identifier (TREE_STRING_POINTER (linkage));
16510 /* We're now using the new linkage. */
16511 unsigned saved_module = module_kind;
16512 module_kind &= ~MK_ATTACH;
16513 push_lang_context (linkage);
16515 /* Preserve the location of the innermost linkage specification,
16516 tracking the locations of nested specifications via a local. */
16517 location_t saved_location
16518 = parser->innermost_linkage_specification_location;
16519 /* Construct a location ranging from the start of the "extern" to
16520 the end of the string-literal, with the caret at the start, e.g.:
16521 extern "C" {
16522 ^~~~~~~~~~
16524 parser->innermost_linkage_specification_location
16525 = make_location (extern_token->location,
16526 extern_token->location,
16527 get_finish (string_token->location));
16529 /* If the next token is a `{', then we're using the first
16530 production. */
16531 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16533 cp_ensure_no_omp_declare_simd (parser);
16534 cp_ensure_no_oacc_routine (parser);
16536 /* Consume the `{' token. */
16537 matching_braces braces;
16538 braces.consume_open (parser);
16539 /* Parse the declarations. */
16540 cp_parser_declaration_seq_opt (parser);
16541 /* Look for the closing `}'. */
16542 braces.require_close (parser);
16544 /* Otherwise, there's just one declaration. */
16545 else
16547 bool saved_in_unbraced_linkage_specification_p;
16549 saved_in_unbraced_linkage_specification_p
16550 = parser->in_unbraced_linkage_specification_p;
16551 parser->in_unbraced_linkage_specification_p = true;
16552 cp_parser_declaration (parser, prefix_attr);
16553 parser->in_unbraced_linkage_specification_p
16554 = saved_in_unbraced_linkage_specification_p;
16557 /* We're done with the linkage-specification. */
16558 pop_lang_context ();
16559 module_kind = saved_module;
16561 /* Restore location of parent linkage specification, if any. */
16562 parser->innermost_linkage_specification_location = saved_location;
16565 /* Parse a static_assert-declaration.
16567 static_assert-declaration:
16568 static_assert ( constant-expression , string-literal ) ;
16569 static_assert ( constant-expression ) ; (C++17)
16571 If MEMBER_P, this static_assert is a class member. */
16573 static void
16574 cp_parser_static_assert (cp_parser *parser, bool member_p)
16576 cp_expr condition;
16577 location_t token_loc;
16578 tree message;
16580 /* Peek at the `static_assert' token so we can keep track of exactly
16581 where the static assertion started. */
16582 token_loc = cp_lexer_peek_token (parser->lexer)->location;
16584 /* Look for the `static_assert' keyword. */
16585 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
16586 RT_STATIC_ASSERT))
16587 return;
16589 /* We know we are in a static assertion; commit to any tentative
16590 parse. */
16591 if (cp_parser_parsing_tentatively (parser))
16592 cp_parser_commit_to_tentative_parse (parser);
16594 /* Parse the `(' starting the static assertion condition. */
16595 matching_parens parens;
16596 parens.require_open (parser);
16598 /* Parse the constant-expression. Allow a non-constant expression
16599 here in order to give better diagnostics in finish_static_assert. */
16600 condition =
16601 cp_parser_constant_expression (parser,
16602 /*allow_non_constant_p=*/true,
16603 /*non_constant_p=*/nullptr);
16605 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
16607 if (pedantic && cxx_dialect < cxx17)
16608 pedwarn (input_location, OPT_Wc__17_extensions,
16609 "%<static_assert%> without a message "
16610 "only available with %<-std=c++17%> or %<-std=gnu++17%>");
16611 /* Eat the ')' */
16612 cp_lexer_consume_token (parser->lexer);
16613 message = build_string (1, "");
16614 TREE_TYPE (message) = char_array_type_node;
16615 fix_string_type (message);
16617 else
16619 /* Parse the separating `,'. */
16620 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
16622 /* Parse the string-literal message. */
16623 message = cp_parser_string_literal (parser, /*translate=*/false,
16624 /*wide_ok=*/true);
16626 /* A `)' completes the static assertion. */
16627 if (!parens.require_close (parser))
16628 cp_parser_skip_to_closing_parenthesis (parser,
16629 /*recovering=*/true,
16630 /*or_comma=*/false,
16631 /*consume_paren=*/true);
16634 /* A semicolon terminates the declaration. */
16635 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16637 /* Get the location for the static assertion. Use that of the
16638 condition if available, otherwise, use that of the "static_assert"
16639 token. */
16640 location_t assert_loc = condition.get_location ();
16641 if (assert_loc == UNKNOWN_LOCATION)
16642 assert_loc = token_loc;
16644 /* Complete the static assertion, which may mean either processing
16645 the static assert now or saving it for template instantiation. */
16646 finish_static_assert (condition, message, assert_loc, member_p,
16647 /*show_expr_p=*/false);
16650 /* Parse the expression in decltype ( expression ). */
16652 static tree
16653 cp_parser_decltype_expr (cp_parser *parser,
16654 bool &id_expression_or_member_access_p)
16656 cp_token *id_expr_start_token;
16657 tree expr;
16659 /* First, try parsing an id-expression. */
16660 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
16661 cp_parser_parse_tentatively (parser);
16662 expr = cp_parser_id_expression (parser,
16663 /*template_keyword_p=*/false,
16664 /*check_dependency_p=*/true,
16665 /*template_p=*/NULL,
16666 /*declarator_p=*/false,
16667 /*optional_p=*/false);
16669 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
16671 bool non_integral_constant_expression_p = false;
16672 tree id_expression = expr;
16673 cp_id_kind idk;
16674 const char *error_msg;
16676 if (identifier_p (expr))
16677 /* Lookup the name we got back from the id-expression. */
16678 expr = cp_parser_lookup_name_simple (parser, expr,
16679 id_expr_start_token->location);
16681 if (expr
16682 && expr != error_mark_node
16683 && TREE_CODE (expr) != TYPE_DECL
16684 && (TREE_CODE (expr) != BIT_NOT_EXPR
16685 || !TYPE_P (TREE_OPERAND (expr, 0)))
16686 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
16688 /* Complete lookup of the id-expression. */
16689 expr = (finish_id_expression
16690 (id_expression, expr, parser->scope, &idk,
16691 /*integral_constant_expression_p=*/false,
16692 /*allow_non_integral_constant_expression_p=*/true,
16693 &non_integral_constant_expression_p,
16694 /*template_p=*/false,
16695 /*done=*/true,
16696 /*address_p=*/false,
16697 /*template_arg_p=*/false,
16698 &error_msg,
16699 id_expr_start_token->location));
16701 if (error_msg)
16703 /* We found an id-expression, but it was something that we
16704 should not have found. This is an error, not something
16705 we can recover from, so report the error we found and
16706 we'll recover as gracefully as possible. */
16707 cp_parser_parse_definitely (parser);
16708 cp_parser_error (parser, error_msg);
16709 id_expression_or_member_access_p = true;
16710 return error_mark_node;
16714 if (expr
16715 && expr != error_mark_node
16716 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
16717 /* We have an id-expression. */
16718 id_expression_or_member_access_p = true;
16721 if (!id_expression_or_member_access_p)
16723 /* Abort the id-expression parse. */
16724 cp_parser_abort_tentative_parse (parser);
16726 /* Parsing tentatively, again. */
16727 cp_parser_parse_tentatively (parser);
16729 /* Parse a class member access. */
16730 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
16731 /*cast_p=*/false, /*decltype*/true,
16732 /*member_access_only_p=*/true, NULL);
16734 if (expr
16735 && expr != error_mark_node
16736 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
16737 /* We have an id-expression. */
16738 id_expression_or_member_access_p = true;
16741 if (id_expression_or_member_access_p)
16742 /* We have parsed the complete id-expression or member access. */
16743 cp_parser_parse_definitely (parser);
16744 else
16746 /* Abort our attempt to parse an id-expression or member access
16747 expression. */
16748 cp_parser_abort_tentative_parse (parser);
16750 /* Parse a full expression. */
16751 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
16752 /*decltype_p=*/true);
16755 return expr;
16758 /* Parse a `decltype' type. Returns the type.
16760 decltype-specifier:
16761 decltype ( expression )
16762 C++14:
16763 decltype ( auto ) */
16765 static tree
16766 cp_parser_decltype (cp_parser *parser)
16768 bool id_expression_or_member_access_p = false;
16769 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
16771 if (start_token->type == CPP_DECLTYPE)
16773 /* Already parsed. */
16774 cp_lexer_consume_token (parser->lexer);
16775 return saved_checks_value (start_token->u.tree_check_value);
16778 /* Look for the `decltype' token. */
16779 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
16780 return error_mark_node;
16782 /* Parse the opening `('. */
16783 matching_parens parens;
16784 if (!parens.require_open (parser))
16785 return error_mark_node;
16787 /* Since we're going to preserve any side-effects from this parse, set up a
16788 firewall to protect our callers from cp_parser_commit_to_tentative_parse
16789 in the expression. */
16790 tentative_firewall firewall (parser);
16792 /* If in_declarator_p, a reparse as an expression might succeed (60361).
16793 Otherwise, commit now for better diagnostics. */
16794 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
16795 && !parser->in_declarator_p)
16796 cp_parser_commit_to_topmost_tentative_parse (parser);
16798 push_deferring_access_checks (dk_deferred);
16800 tree expr = NULL_TREE;
16802 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO)
16803 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
16805 /* decltype (auto) */
16806 cp_lexer_consume_token (parser->lexer);
16807 if (cxx_dialect < cxx14)
16809 error_at (start_token->location,
16810 "%<decltype(auto)%> type specifier only available with "
16811 "%<-std=c++14%> or %<-std=gnu++14%>");
16812 expr = error_mark_node;
16815 else
16817 /* decltype (expression) */
16819 /* Types cannot be defined in a `decltype' expression. Save away the
16820 old message and set the new one. */
16821 const char *saved_message = parser->type_definition_forbidden_message;
16822 parser->type_definition_forbidden_message
16823 = G_("types may not be defined in %<decltype%> expressions");
16825 /* The restrictions on constant-expressions do not apply inside
16826 decltype expressions. */
16827 bool saved_integral_constant_expression_p
16828 = parser->integral_constant_expression_p;
16829 bool saved_non_integral_constant_expression_p
16830 = parser->non_integral_constant_expression_p;
16831 parser->integral_constant_expression_p = false;
16833 /* Within a parenthesized expression, a `>' token is always
16834 the greater-than operator. */
16835 bool saved_greater_than_is_operator_p
16836 = parser->greater_than_is_operator_p;
16837 parser->greater_than_is_operator_p = true;
16839 /* Don't synthesize an implicit template type parameter here. This
16840 could happen with C++23 code like
16842 void f(decltype(new auto{0}));
16844 where we want to deduce the auto right away so that the parameter
16845 is of type 'int *'. */
16846 auto cleanup = make_temp_override
16847 (parser->auto_is_implicit_function_template_parm_p, false);
16849 /* Do not actually evaluate the expression. */
16850 ++cp_unevaluated_operand;
16852 /* Do not warn about problems with the expression. */
16853 ++c_inhibit_evaluation_warnings;
16855 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
16856 STRIP_ANY_LOCATION_WRAPPER (expr);
16858 /* Go back to evaluating expressions. */
16859 --cp_unevaluated_operand;
16860 --c_inhibit_evaluation_warnings;
16862 /* The `>' token might be the end of a template-id or
16863 template-parameter-list now. */
16864 parser->greater_than_is_operator_p
16865 = saved_greater_than_is_operator_p;
16867 /* Restore the old message and the integral constant expression
16868 flags. */
16869 parser->type_definition_forbidden_message = saved_message;
16870 parser->integral_constant_expression_p
16871 = saved_integral_constant_expression_p;
16872 parser->non_integral_constant_expression_p
16873 = saved_non_integral_constant_expression_p;
16876 /* Parse to the closing `)'. */
16877 if (expr == error_mark_node || !parens.require_close (parser))
16879 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16880 /*consume_paren=*/true);
16881 expr = error_mark_node;
16884 /* If we got a parse error while tentative, bail out now. */
16885 if (cp_parser_error_occurred (parser))
16887 pop_deferring_access_checks ();
16888 return error_mark_node;
16891 if (!expr)
16892 /* Build auto. */
16893 expr = make_decltype_auto ();
16894 else
16895 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
16896 tf_warning_or_error);
16898 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
16899 it again. */
16900 start_token->type = CPP_DECLTYPE;
16901 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
16902 start_token->tree_check_p = true;
16903 start_token->u.tree_check_value->value = expr;
16904 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
16905 start_token->keyword = RID_MAX;
16907 location_t loc = start_token->location;
16908 loc = make_location (loc, loc, parser->lexer);
16909 start_token->location = loc;
16911 cp_lexer_purge_tokens_after (parser->lexer, start_token);
16913 pop_to_parent_deferring_access_checks ();
16915 return expr;
16918 /* Special member functions [gram.special] */
16920 /* Parse a conversion-function-id.
16922 conversion-function-id:
16923 operator conversion-type-id
16925 Returns an IDENTIFIER_NODE representing the operator. */
16927 static tree
16928 cp_parser_conversion_function_id (cp_parser* parser)
16930 tree type;
16931 tree saved_scope;
16932 tree saved_qualifying_scope;
16933 tree saved_object_scope;
16934 tree pushed_scope = NULL_TREE;
16936 /* Look for the `operator' token. */
16937 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
16938 return error_mark_node;
16939 /* When we parse the conversion-type-id, the current scope will be
16940 reset. However, we need that information in able to look up the
16941 conversion function later, so we save it here. */
16942 saved_scope = parser->scope;
16943 saved_qualifying_scope = parser->qualifying_scope;
16944 saved_object_scope = parser->object_scope;
16945 /* We must enter the scope of the class so that the names of
16946 entities declared within the class are available in the
16947 conversion-type-id. For example, consider:
16949 struct S {
16950 typedef int I;
16951 operator I();
16954 S::operator I() { ... }
16956 In order to see that `I' is a type-name in the definition, we
16957 must be in the scope of `S'. */
16958 if (saved_scope)
16959 pushed_scope = push_scope (saved_scope);
16960 /* Parse the conversion-type-id. */
16961 type = cp_parser_conversion_type_id (parser);
16962 /* Leave the scope of the class, if any. */
16963 if (pushed_scope)
16964 pop_scope (pushed_scope);
16965 /* Restore the saved scope. */
16966 parser->scope = saved_scope;
16967 parser->qualifying_scope = saved_qualifying_scope;
16968 parser->object_scope = saved_object_scope;
16969 /* If the TYPE is invalid, indicate failure. */
16970 if (type == error_mark_node)
16971 return error_mark_node;
16972 return make_conv_op_name (type);
16975 /* Parse a conversion-type-id:
16977 conversion-type-id:
16978 type-specifier-seq conversion-declarator [opt]
16980 Returns the TYPE specified. */
16982 static tree
16983 cp_parser_conversion_type_id (cp_parser* parser)
16985 tree attributes;
16986 cp_decl_specifier_seq type_specifiers;
16987 cp_declarator *declarator;
16988 tree type_specified;
16989 const char *saved_message;
16991 /* Parse the attributes. */
16992 attributes = cp_parser_attributes_opt (parser);
16994 saved_message = parser->type_definition_forbidden_message;
16995 parser->type_definition_forbidden_message
16996 = G_("types may not be defined in a conversion-type-id");
16998 /* Parse the type-specifiers. DR 2413 clarifies that `typename' is
16999 optional in conversion-type-id. */
17000 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
17001 /*is_declaration=*/false,
17002 /*is_trailing_return=*/false,
17003 &type_specifiers);
17005 parser->type_definition_forbidden_message = saved_message;
17007 /* If that didn't work, stop. */
17008 if (type_specifiers.type == error_mark_node)
17009 return error_mark_node;
17010 /* Parse the conversion-declarator. */
17011 declarator = cp_parser_conversion_declarator_opt (parser);
17013 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
17014 /*initialized=*/0, &attributes);
17015 if (attributes)
17016 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
17018 /* Don't give this error when parsing tentatively. This happens to
17019 work because we always parse this definitively once. */
17020 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
17021 && type_uses_auto (type_specified))
17023 if (cxx_dialect < cxx14)
17025 error ("invalid use of %<auto%> in conversion operator");
17026 return error_mark_node;
17028 else if (template_parm_scope_p ())
17029 warning (0, "use of %<auto%> in member template "
17030 "conversion operator can never be deduced");
17033 return type_specified;
17036 /* Parse an (optional) conversion-declarator.
17038 conversion-declarator:
17039 ptr-operator conversion-declarator [opt]
17043 static cp_declarator *
17044 cp_parser_conversion_declarator_opt (cp_parser* parser)
17046 enum tree_code code;
17047 tree class_type, std_attributes = NULL_TREE;
17048 cp_cv_quals cv_quals;
17050 /* We don't know if there's a ptr-operator next, or not. */
17051 cp_parser_parse_tentatively (parser);
17052 /* Try the ptr-operator. */
17053 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
17054 &std_attributes);
17055 /* If it worked, look for more conversion-declarators. */
17056 if (cp_parser_parse_definitely (parser))
17058 cp_declarator *declarator;
17060 /* Parse another optional declarator. */
17061 declarator = cp_parser_conversion_declarator_opt (parser);
17063 declarator = cp_parser_make_indirect_declarator
17064 (code, class_type, cv_quals, declarator, std_attributes);
17066 return declarator;
17069 return NULL;
17072 /* Parse an (optional) ctor-initializer.
17074 ctor-initializer:
17075 : mem-initializer-list */
17077 static void
17078 cp_parser_ctor_initializer_opt (cp_parser* parser)
17080 /* If the next token is not a `:', then there is no
17081 ctor-initializer. */
17082 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17084 /* Do default initialization of any bases and members. */
17085 if (DECL_CONSTRUCTOR_P (current_function_decl))
17086 finish_mem_initializers (NULL_TREE);
17087 return;
17090 /* Consume the `:' token. */
17091 cp_lexer_consume_token (parser->lexer);
17092 /* And the mem-initializer-list. */
17093 cp_parser_mem_initializer_list (parser);
17096 /* Parse a mem-initializer-list.
17098 mem-initializer-list:
17099 mem-initializer ... [opt]
17100 mem-initializer ... [opt] , mem-initializer-list */
17102 static void
17103 cp_parser_mem_initializer_list (cp_parser* parser)
17105 tree mem_initializer_list = NULL_TREE;
17106 tree target_ctor = error_mark_node;
17107 cp_token *token = cp_lexer_peek_token (parser->lexer);
17109 /* Let the semantic analysis code know that we are starting the
17110 mem-initializer-list. */
17111 if (!DECL_CONSTRUCTOR_P (current_function_decl))
17112 error_at (token->location,
17113 "only constructors take member initializers");
17115 /* Loop through the list. */
17116 while (true)
17118 tree mem_initializer;
17120 token = cp_lexer_peek_token (parser->lexer);
17121 /* Parse the mem-initializer. */
17122 mem_initializer = cp_parser_mem_initializer (parser);
17123 /* If the next token is a `...', we're expanding member initializers. */
17124 bool ellipsis = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
17125 if (ellipsis
17126 || (mem_initializer != error_mark_node
17127 && check_for_bare_parameter_packs (TREE_PURPOSE
17128 (mem_initializer))))
17130 /* Consume the `...'. */
17131 if (ellipsis)
17132 cp_lexer_consume_token (parser->lexer);
17134 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
17135 can be expanded but members cannot. */
17136 if (mem_initializer != error_mark_node
17137 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
17139 error_at (token->location,
17140 "cannot expand initializer for member %qD",
17141 TREE_PURPOSE (mem_initializer));
17142 mem_initializer = error_mark_node;
17145 /* Construct the pack expansion type. */
17146 if (mem_initializer != error_mark_node)
17147 mem_initializer = make_pack_expansion (mem_initializer);
17149 if (target_ctor != error_mark_node
17150 && mem_initializer != error_mark_node)
17152 error ("mem-initializer for %qD follows constructor delegation",
17153 TREE_PURPOSE (mem_initializer));
17154 mem_initializer = error_mark_node;
17156 /* Look for a target constructor. */
17157 if (mem_initializer != error_mark_node
17158 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
17159 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
17161 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
17162 if (mem_initializer_list)
17164 error ("constructor delegation follows mem-initializer for %qD",
17165 TREE_PURPOSE (mem_initializer_list));
17166 mem_initializer = error_mark_node;
17168 target_ctor = mem_initializer;
17170 /* Add it to the list, unless it was erroneous. */
17171 if (mem_initializer != error_mark_node)
17173 TREE_CHAIN (mem_initializer) = mem_initializer_list;
17174 mem_initializer_list = mem_initializer;
17176 /* If the next token is not a `,', we're done. */
17177 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17178 break;
17179 /* Consume the `,' token. */
17180 cp_lexer_consume_token (parser->lexer);
17183 /* Perform semantic analysis. */
17184 if (DECL_CONSTRUCTOR_P (current_function_decl))
17185 finish_mem_initializers (mem_initializer_list);
17188 /* Parse a mem-initializer.
17190 mem-initializer:
17191 mem-initializer-id ( expression-list [opt] )
17192 mem-initializer-id braced-init-list
17194 GNU extension:
17196 mem-initializer:
17197 ( expression-list [opt] )
17199 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
17200 class) or FIELD_DECL (for a non-static data member) to initialize;
17201 the TREE_VALUE is the expression-list. An empty initialization
17202 list is represented by void_list_node. */
17204 static tree
17205 cp_parser_mem_initializer (cp_parser* parser)
17207 tree mem_initializer_id;
17208 tree expression_list;
17209 tree member;
17210 cp_token *token = cp_lexer_peek_token (parser->lexer);
17212 /* Find out what is being initialized. */
17213 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17215 permerror (token->location,
17216 "anachronistic old-style base class initializer");
17217 mem_initializer_id = NULL_TREE;
17219 else
17221 mem_initializer_id = cp_parser_mem_initializer_id (parser);
17222 if (mem_initializer_id == error_mark_node)
17223 return mem_initializer_id;
17225 member = expand_member_init (mem_initializer_id);
17226 if (member && !DECL_P (member))
17227 in_base_initializer = 1;
17229 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17231 cp_lexer_set_source_position (parser->lexer);
17232 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17233 expression_list = cp_parser_braced_list (parser);
17234 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
17235 expression_list = build_tree_list (NULL_TREE, expression_list);
17237 else
17239 vec<tree, va_gc> *vec;
17240 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
17241 /*cast_p=*/false,
17242 /*allow_expansion_p=*/true,
17243 /*non_constant_p=*/NULL,
17244 /*close_paren_loc=*/NULL,
17245 /*wrap_locations_p=*/true);
17246 if (vec == NULL)
17247 return error_mark_node;
17248 expression_list = build_tree_list_vec (vec);
17249 release_tree_vector (vec);
17252 if (expression_list == error_mark_node)
17253 return error_mark_node;
17254 if (!expression_list)
17255 expression_list = void_type_node;
17257 in_base_initializer = 0;
17259 if (!member)
17260 return error_mark_node;
17261 tree node = build_tree_list (member, expression_list);
17263 /* We can't attach the source location of this initializer directly to
17264 the list node, so we instead attach it to a dummy EMPTY_CLASS_EXPR
17265 within the TREE_TYPE of the list node. */
17266 location_t loc
17267 = make_location (token->location, token->location, parser->lexer);
17268 tree dummy = build0 (EMPTY_CLASS_EXPR, NULL_TREE);
17269 SET_EXPR_LOCATION (dummy, loc);
17270 TREE_TYPE (node) = dummy;
17272 return node;
17275 /* Parse a mem-initializer-id.
17277 mem-initializer-id:
17278 :: [opt] nested-name-specifier [opt] class-name
17279 decltype-specifier (C++11)
17280 identifier
17282 Returns a TYPE indicating the class to be initialized for the first
17283 production (and the second in C++11). Returns an IDENTIFIER_NODE
17284 indicating the data member to be initialized for the last production. */
17286 static tree
17287 cp_parser_mem_initializer_id (cp_parser* parser)
17289 bool global_scope_p;
17290 bool nested_name_specifier_p;
17291 bool template_p = false;
17292 tree id;
17294 cp_token *token = cp_lexer_peek_token (parser->lexer);
17296 /* `typename' is not allowed in this context ([temp.res]). */
17297 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
17299 error_at (token->location,
17300 "keyword %<typename%> not allowed in this context (a qualified "
17301 "member initializer is implicitly a type)");
17302 cp_lexer_consume_token (parser->lexer);
17304 /* Look for the optional `::' operator. */
17305 global_scope_p
17306 = (cp_parser_global_scope_opt (parser,
17307 /*current_scope_valid_p=*/false)
17308 != NULL_TREE);
17309 /* Look for the optional nested-name-specifier. The simplest way to
17310 implement:
17312 [temp.res]
17314 The keyword `typename' is not permitted in a base-specifier or
17315 mem-initializer; in these contexts a qualified name that
17316 depends on a template-parameter is implicitly assumed to be a
17317 type name.
17319 is to assume that we have seen the `typename' keyword at this
17320 point. */
17321 nested_name_specifier_p
17322 = (cp_parser_nested_name_specifier_opt (parser,
17323 /*typename_keyword_p=*/true,
17324 /*check_dependency_p=*/true,
17325 /*type_p=*/true,
17326 /*is_declaration=*/true)
17327 != NULL_TREE);
17328 if (nested_name_specifier_p)
17329 template_p = cp_parser_optional_template_keyword (parser);
17330 /* If there is a `::' operator or a nested-name-specifier, then we
17331 are definitely looking for a class-name. */
17332 if (global_scope_p || nested_name_specifier_p)
17333 return cp_parser_class_name (parser,
17334 /*typename_keyword_p=*/true,
17335 /*template_keyword_p=*/template_p,
17336 typename_type,
17337 /*check_dependency_p=*/true,
17338 /*class_head_p=*/false,
17339 /*is_declaration=*/true);
17340 /* Otherwise, we could also be looking for an ordinary identifier. */
17341 cp_parser_parse_tentatively (parser);
17342 if (cp_lexer_next_token_is_decltype (parser->lexer))
17343 /* Try a decltype-specifier. */
17344 id = cp_parser_decltype (parser);
17345 else
17346 /* Otherwise, try a class-name. */
17347 id = cp_parser_class_name (parser,
17348 /*typename_keyword_p=*/true,
17349 /*template_keyword_p=*/false,
17350 none_type,
17351 /*check_dependency_p=*/true,
17352 /*class_head_p=*/false,
17353 /*is_declaration=*/true);
17354 /* If we found one, we're done. */
17355 if (cp_parser_parse_definitely (parser))
17356 return id;
17357 /* Otherwise, look for an ordinary identifier. */
17358 return cp_parser_identifier (parser);
17361 /* Overloading [gram.over] */
17363 /* Parse an operator-function-id.
17365 operator-function-id:
17366 operator operator
17368 Returns an IDENTIFIER_NODE for the operator which is a
17369 human-readable spelling of the identifier, e.g., `operator +'. */
17371 static cp_expr
17372 cp_parser_operator_function_id (cp_parser* parser)
17374 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
17375 /* Look for the `operator' keyword. */
17376 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
17377 return error_mark_node;
17378 /* And then the name of the operator itself. */
17379 return cp_parser_operator (parser, start_loc);
17382 /* Return an identifier node for a user-defined literal operator.
17383 The suffix identifier is chained to the operator name identifier. */
17385 tree
17386 cp_literal_operator_id (const char* name)
17388 tree identifier;
17389 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
17390 + strlen (name) + 10);
17391 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
17392 identifier = get_identifier (buffer);
17393 XDELETEVEC (buffer);
17395 return identifier;
17398 /* Parse an operator.
17400 operator:
17401 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
17402 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
17403 || ++ -- , ->* -> () []
17405 GNU Extensions:
17407 operator:
17408 <? >? <?= >?=
17410 Returns an IDENTIFIER_NODE for the operator which is a
17411 human-readable spelling of the identifier, e.g., `operator +'. */
17413 static cp_expr
17414 cp_parser_operator (cp_parser* parser, location_t start_loc)
17416 tree id = NULL_TREE;
17417 cp_token *token;
17418 bool utf8 = false;
17420 /* Peek at the next token. */
17421 token = cp_lexer_peek_token (parser->lexer);
17423 location_t end_loc = token->location;
17425 /* Figure out which operator we have. */
17426 enum tree_code op = ERROR_MARK;
17427 bool assop = false;
17428 bool consumed = false;
17429 switch (token->type)
17431 case CPP_KEYWORD:
17433 /* The keyword should be either `new', `delete' or `co_await'. */
17434 if (token->keyword == RID_NEW)
17435 op = NEW_EXPR;
17436 else if (token->keyword == RID_DELETE)
17437 op = DELETE_EXPR;
17438 else if (token->keyword == RID_CO_AWAIT)
17439 op = CO_AWAIT_EXPR;
17440 else
17441 break;
17443 /* Consume the `new', `delete' or co_await token. */
17444 end_loc = cp_lexer_consume_token (parser->lexer)->location;
17446 /* Peek at the next token. */
17447 token = cp_lexer_peek_token (parser->lexer);
17448 /* If it's a `[' token then this is the array variant of the
17449 operator. */
17450 if (token->type == CPP_OPEN_SQUARE
17451 && op != CO_AWAIT_EXPR)
17453 /* Consume the `[' token. */
17454 cp_lexer_consume_token (parser->lexer);
17455 /* Look for the `]' token. */
17456 if (cp_token *close_token
17457 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
17458 end_loc = close_token->location;
17459 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
17461 consumed = true;
17462 break;
17465 case CPP_PLUS:
17466 op = PLUS_EXPR;
17467 break;
17469 case CPP_MINUS:
17470 op = MINUS_EXPR;
17471 break;
17473 case CPP_MULT:
17474 op = MULT_EXPR;
17475 break;
17477 case CPP_DIV:
17478 op = TRUNC_DIV_EXPR;
17479 break;
17481 case CPP_MOD:
17482 op = TRUNC_MOD_EXPR;
17483 break;
17485 case CPP_XOR:
17486 op = BIT_XOR_EXPR;
17487 break;
17489 case CPP_AND:
17490 op = BIT_AND_EXPR;
17491 break;
17493 case CPP_OR:
17494 op = BIT_IOR_EXPR;
17495 break;
17497 case CPP_COMPL:
17498 op = BIT_NOT_EXPR;
17499 break;
17501 case CPP_NOT:
17502 op = TRUTH_NOT_EXPR;
17503 break;
17505 case CPP_EQ:
17506 assop = true;
17507 op = NOP_EXPR;
17508 break;
17510 case CPP_LESS:
17511 op = LT_EXPR;
17512 break;
17514 case CPP_GREATER:
17515 op = GT_EXPR;
17516 break;
17518 case CPP_PLUS_EQ:
17519 assop = true;
17520 op = PLUS_EXPR;
17521 break;
17523 case CPP_MINUS_EQ:
17524 assop = true;
17525 op = MINUS_EXPR;
17526 break;
17528 case CPP_MULT_EQ:
17529 assop = true;
17530 op = MULT_EXPR;
17531 break;
17533 case CPP_DIV_EQ:
17534 assop = true;
17535 op = TRUNC_DIV_EXPR;
17536 break;
17538 case CPP_MOD_EQ:
17539 assop = true;
17540 op = TRUNC_MOD_EXPR;
17541 break;
17543 case CPP_XOR_EQ:
17544 assop = true;
17545 op = BIT_XOR_EXPR;
17546 break;
17548 case CPP_AND_EQ:
17549 assop = true;
17550 op = BIT_AND_EXPR;
17551 break;
17553 case CPP_OR_EQ:
17554 assop = true;
17555 op = BIT_IOR_EXPR;
17556 break;
17558 case CPP_LSHIFT:
17559 op = LSHIFT_EXPR;
17560 break;
17562 case CPP_RSHIFT:
17563 op = RSHIFT_EXPR;
17564 break;
17566 case CPP_LSHIFT_EQ:
17567 assop = true;
17568 op = LSHIFT_EXPR;
17569 break;
17571 case CPP_RSHIFT_EQ:
17572 assop = true;
17573 op = RSHIFT_EXPR;
17574 break;
17576 case CPP_EQ_EQ:
17577 op = EQ_EXPR;
17578 break;
17580 case CPP_NOT_EQ:
17581 op = NE_EXPR;
17582 break;
17584 case CPP_LESS_EQ:
17585 op = LE_EXPR;
17586 break;
17588 case CPP_GREATER_EQ:
17589 op = GE_EXPR;
17590 break;
17592 case CPP_SPACESHIP:
17593 op = SPACESHIP_EXPR;
17594 break;
17596 case CPP_AND_AND:
17597 op = TRUTH_ANDIF_EXPR;
17598 break;
17600 case CPP_OR_OR:
17601 op = TRUTH_ORIF_EXPR;
17602 break;
17604 case CPP_PLUS_PLUS:
17605 op = POSTINCREMENT_EXPR;
17606 break;
17608 case CPP_MINUS_MINUS:
17609 op = PREDECREMENT_EXPR;
17610 break;
17612 case CPP_COMMA:
17613 op = COMPOUND_EXPR;
17614 break;
17616 case CPP_DEREF_STAR:
17617 op = MEMBER_REF;
17618 break;
17620 case CPP_DEREF:
17621 op = COMPONENT_REF;
17622 break;
17624 case CPP_QUERY:
17625 op = COND_EXPR;
17626 /* Consume the `?'. */
17627 cp_lexer_consume_token (parser->lexer);
17628 /* Look for the matching `:'. */
17629 cp_parser_require (parser, CPP_COLON, RT_COLON);
17630 consumed = true;
17631 break;
17633 case CPP_OPEN_PAREN:
17635 /* Consume the `('. */
17636 matching_parens parens;
17637 parens.consume_open (parser);
17638 /* Look for the matching `)'. */
17639 token = parens.require_close (parser);
17640 if (token)
17641 end_loc = token->location;
17642 op = CALL_EXPR;
17643 consumed = true;
17644 break;
17647 case CPP_OPEN_SQUARE:
17648 /* Consume the `['. */
17649 cp_lexer_consume_token (parser->lexer);
17650 /* Look for the matching `]'. */
17651 token = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
17652 if (token)
17653 end_loc = token->location;
17654 op = ARRAY_REF;
17655 consumed = true;
17656 break;
17658 case CPP_UTF8STRING:
17659 case CPP_UTF8STRING_USERDEF:
17660 utf8 = true;
17661 /* FALLTHRU */
17662 case CPP_STRING:
17663 case CPP_WSTRING:
17664 case CPP_STRING16:
17665 case CPP_STRING32:
17666 case CPP_STRING_USERDEF:
17667 case CPP_WSTRING_USERDEF:
17668 case CPP_STRING16_USERDEF:
17669 case CPP_STRING32_USERDEF:
17671 tree string_tree;
17672 int sz, len;
17674 if (cxx_dialect == cxx98)
17675 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
17677 /* Consume the string. */
17678 cp_expr str = cp_parser_userdef_string_literal (parser,
17679 /*lookup_udlit=*/false);
17680 if (str == error_mark_node)
17681 return error_mark_node;
17682 else if (TREE_CODE (str) == USERDEF_LITERAL)
17684 string_tree = USERDEF_LITERAL_VALUE (str.get_value ());
17685 id = USERDEF_LITERAL_SUFFIX_ID (str.get_value ());
17686 end_loc = str.get_location ();
17688 else
17690 string_tree = str;
17691 /* Look for the suffix identifier. */
17692 token = cp_lexer_peek_token (parser->lexer);
17693 if (token->type == CPP_NAME)
17695 id = cp_parser_identifier (parser);
17696 end_loc = token->location;
17698 else if (token->type == CPP_KEYWORD)
17700 error ("unexpected keyword;"
17701 " remove space between quotes and suffix identifier");
17702 return error_mark_node;
17704 else
17706 error ("expected suffix identifier");
17707 return error_mark_node;
17710 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
17711 (TREE_TYPE (TREE_TYPE (string_tree))));
17712 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
17713 if (len != 0)
17715 error ("expected empty string after %<operator%> keyword");
17716 return error_mark_node;
17718 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
17719 != char_type_node)
17721 error ("invalid encoding prefix in literal operator");
17722 return error_mark_node;
17724 if (id != error_mark_node)
17726 const char *name = IDENTIFIER_POINTER (id);
17727 id = cp_literal_operator_id (name);
17729 /* Generate a location of the form:
17730 "" _suffix_identifier
17731 ^~~~~~~~~~~~~~~~~~~~~
17732 with caret == start at the start token, finish at the end of the
17733 suffix identifier. */
17734 location_t combined_loc
17735 = make_location (start_loc, start_loc, parser->lexer);
17736 return cp_expr (id, combined_loc);
17739 default:
17740 /* Anything else is an error. */
17741 break;
17744 /* If we have selected an identifier, we need to consume the
17745 operator token. */
17746 if (op != ERROR_MARK)
17748 id = ovl_op_identifier (assop, op);
17749 if (!consumed)
17750 cp_lexer_consume_token (parser->lexer);
17752 /* Otherwise, no valid operator name was present. */
17753 else
17755 cp_parser_error (parser, "expected operator");
17756 id = error_mark_node;
17759 start_loc = make_location (start_loc, start_loc, get_finish (end_loc));
17760 return cp_expr (id, start_loc);
17763 /* Parse a template-declaration.
17765 template-declaration:
17766 export [opt] template < template-parameter-list > declaration
17768 If MEMBER_P is TRUE, this template-declaration occurs within a
17769 class-specifier.
17771 The grammar rule given by the standard isn't correct. What
17772 is really meant is:
17774 template-declaration:
17775 export [opt] template-parameter-list-seq
17776 decl-specifier-seq [opt] init-declarator [opt] ;
17777 export [opt] template-parameter-list-seq
17778 function-definition
17780 template-parameter-list-seq:
17781 template-parameter-list-seq [opt]
17782 template < template-parameter-list >
17784 Concept Extensions:
17786 template-parameter-list-seq:
17787 template < template-parameter-list > requires-clause [opt]
17789 requires-clause:
17790 requires logical-or-expression */
17792 static void
17793 cp_parser_template_declaration (cp_parser* parser, bool member_p)
17795 /* Check for `export'. */
17796 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
17798 /* Consume the `export' token. */
17799 cp_lexer_consume_token (parser->lexer);
17800 /* Warn that this use of export is deprecated. */
17801 if (cxx_dialect < cxx11)
17802 warning (0, "keyword %<export%> not implemented, and will be ignored");
17803 else if (cxx_dialect < cxx20)
17804 warning (0, "keyword %<export%> is deprecated, and is ignored");
17805 else
17806 warning (0, "keyword %<export%> is enabled with %<-fmodules-ts%>");
17809 cp_parser_template_declaration_after_export (parser, member_p);
17812 /* Parse a template-parameter-list.
17814 template-parameter-list:
17815 template-parameter
17816 template-parameter-list , template-parameter
17818 Returns a TREE_LIST. Each node represents a template parameter.
17819 The nodes are connected via their TREE_CHAINs. */
17821 static tree
17822 cp_parser_template_parameter_list (cp_parser* parser)
17824 tree parameter_list = NULL_TREE;
17826 /* Don't create wrapper nodes within a template-parameter-list,
17827 since we don't want to have different types based on the
17828 spelling location of constants and decls within them. */
17829 auto_suppress_location_wrappers sentinel;
17831 begin_template_parm_list ();
17833 /* The loop below parses the template parms. We first need to know
17834 the total number of template parms to be able to compute proper
17835 canonical types of each dependent type. So after the loop, when
17836 we know the total number of template parms,
17837 end_template_parm_list computes the proper canonical types and
17838 fixes up the dependent types accordingly. */
17839 while (true)
17841 tree parameter;
17842 bool is_non_type;
17843 bool is_parameter_pack;
17844 location_t parm_loc;
17846 /* Parse the template-parameter. */
17847 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
17848 parameter = cp_parser_template_parameter (parser,
17849 &is_non_type,
17850 &is_parameter_pack);
17851 /* Add it to the list. */
17852 if (parameter != error_mark_node)
17853 parameter_list = process_template_parm (parameter_list,
17854 parm_loc,
17855 parameter,
17856 is_non_type,
17857 is_parameter_pack);
17858 else
17860 tree err_parm = build_tree_list (parameter, parameter);
17861 parameter_list = chainon (parameter_list, err_parm);
17864 /* If the next token is not a `,', we're done. */
17865 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17866 break;
17867 /* Otherwise, consume the `,' token. */
17868 cp_lexer_consume_token (parser->lexer);
17871 return end_template_parm_list (parameter_list);
17874 /* Parse a introduction-list.
17876 introduction-list:
17877 introduced-parameter
17878 introduction-list , introduced-parameter
17880 introduced-parameter:
17881 ...[opt] identifier
17883 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
17884 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
17885 WILDCARD_DECL will also have DECL_NAME set and token location in
17886 DECL_SOURCE_LOCATION. */
17888 static tree
17889 cp_parser_introduction_list (cp_parser *parser)
17891 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
17893 while (true)
17895 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
17896 if (is_pack)
17897 cp_lexer_consume_token (parser->lexer);
17899 tree identifier = cp_parser_identifier (parser);
17900 if (identifier == error_mark_node)
17901 break;
17903 /* Build placeholder. */
17904 tree parm = build_nt (WILDCARD_DECL);
17905 DECL_SOURCE_LOCATION (parm)
17906 = cp_lexer_peek_token (parser->lexer)->location;
17907 DECL_NAME (parm) = identifier;
17908 WILDCARD_PACK_P (parm) = is_pack;
17909 vec_safe_push (introduction_vec, parm);
17911 /* If the next token is not a `,', we're done. */
17912 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17913 break;
17914 /* Otherwise, consume the `,' token. */
17915 cp_lexer_consume_token (parser->lexer);
17918 /* Convert the vec into a TREE_VEC. */
17919 tree introduction_list = make_tree_vec (introduction_vec->length ());
17920 unsigned int n;
17921 tree parm;
17922 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
17923 TREE_VEC_ELT (introduction_list, n) = parm;
17925 release_tree_vector (introduction_vec);
17926 return introduction_list;
17929 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
17930 is an abstract declarator. */
17932 static inline cp_declarator*
17933 get_id_declarator (cp_declarator *declarator)
17935 cp_declarator *d = declarator;
17936 while (d && d->kind != cdk_id)
17937 d = d->declarator;
17938 return d;
17941 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
17942 is an abstract declarator. */
17944 static inline tree
17945 get_unqualified_id (cp_declarator *declarator)
17947 declarator = get_id_declarator (declarator);
17948 if (declarator)
17949 return declarator->u.id.unqualified_name;
17950 else
17951 return NULL_TREE;
17954 /* Returns true if TYPE would declare a constrained constrained-parameter. */
17956 static inline bool
17957 is_constrained_parameter (tree type)
17959 return (type
17960 && TREE_CODE (type) == TYPE_DECL
17961 && CONSTRAINED_PARM_CONCEPT (type)
17962 && DECL_P (CONSTRAINED_PARM_CONCEPT (type)));
17965 /* Returns true if PARM declares a constrained-parameter. */
17967 static inline bool
17968 is_constrained_parameter (cp_parameter_declarator *parm)
17970 return is_constrained_parameter (parm->decl_specifiers.type);
17973 /* Check that the type parameter is only a declarator-id, and that its
17974 type is not cv-qualified. */
17976 bool
17977 cp_parser_check_constrained_type_parm (cp_parser *parser,
17978 cp_parameter_declarator *parm)
17980 if (!parm->declarator)
17981 return true;
17983 if (parm->declarator->kind != cdk_id)
17985 cp_parser_error (parser, "invalid constrained type parameter");
17986 return false;
17989 /* Don't allow cv-qualified type parameters. */
17990 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
17991 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
17993 cp_parser_error (parser, "cv-qualified type parameter");
17994 return false;
17997 return true;
18000 /* Finish parsing/processing a template type parameter and checking
18001 various restrictions. */
18003 static inline tree
18004 cp_parser_constrained_type_template_parm (cp_parser *parser,
18005 tree id,
18006 cp_parameter_declarator* parmdecl)
18008 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
18009 return finish_template_type_parm (class_type_node, id);
18010 else
18011 return error_mark_node;
18014 static tree
18015 finish_constrained_template_template_parm (tree proto, tree id)
18017 /* FIXME: This should probably be copied, and we may need to adjust
18018 the template parameter depths. */
18019 tree saved_parms = current_template_parms;
18020 begin_template_parm_list ();
18021 current_template_parms = DECL_TEMPLATE_PARMS (proto);
18022 end_template_parm_list ();
18024 tree parm = finish_template_template_parm (class_type_node, id);
18025 current_template_parms = saved_parms;
18027 return parm;
18030 /* Finish parsing/processing a template template parameter by borrowing
18031 the template parameter list from the prototype parameter. */
18033 static tree
18034 cp_parser_constrained_template_template_parm (cp_parser *parser,
18035 tree proto,
18036 tree id,
18037 cp_parameter_declarator *parmdecl)
18039 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
18040 return error_mark_node;
18041 return finish_constrained_template_template_parm (proto, id);
18044 /* Create a new non-type template parameter from the given PARM
18045 declarator. */
18047 static tree
18048 cp_parser_constrained_non_type_template_parm (bool *is_non_type,
18049 cp_parameter_declarator *parm)
18051 *is_non_type = true;
18052 cp_declarator *decl = parm->declarator;
18053 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
18054 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
18055 return grokdeclarator (decl, specs, TPARM, 0, NULL);
18058 /* Build a constrained template parameter based on the PARMDECL
18059 declarator. The type of PARMDECL is the constrained type, which
18060 refers to the prototype template parameter that ultimately
18061 specifies the type of the declared parameter. */
18063 static tree
18064 finish_constrained_parameter (cp_parser *parser,
18065 cp_parameter_declarator *parmdecl,
18066 bool *is_non_type)
18068 tree decl = parmdecl->decl_specifiers.type;
18069 tree id = get_unqualified_id (parmdecl->declarator);
18070 tree def = parmdecl->default_argument;
18071 tree proto = DECL_INITIAL (decl);
18073 /* Build the parameter. Return an error if the declarator was invalid. */
18074 tree parm;
18075 if (TREE_CODE (proto) == TYPE_DECL)
18076 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
18077 else if (TREE_CODE (proto) == TEMPLATE_DECL)
18078 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
18079 parmdecl);
18080 else
18081 parm = cp_parser_constrained_non_type_template_parm (is_non_type, parmdecl);
18082 if (parm == error_mark_node)
18083 return error_mark_node;
18085 /* Finish the parameter decl and create a node attaching the
18086 default argument and constraint. */
18087 parm = build_tree_list (def, parm);
18088 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
18090 return parm;
18093 /* Returns true if the parsed type actually represents the declaration
18094 of a type template-parameter. */
18096 static bool
18097 declares_constrained_type_template_parameter (tree type)
18099 return (is_constrained_parameter (type)
18100 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
18103 /* Returns true if the parsed type actually represents the declaration of
18104 a template template-parameter. */
18106 static bool
18107 declares_constrained_template_template_parameter (tree type)
18109 return (is_constrained_parameter (type)
18110 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
18113 /* Parse a default argument for a type template-parameter.
18114 Note that diagnostics are handled in cp_parser_template_parameter. */
18116 static tree
18117 cp_parser_default_type_template_argument (cp_parser *parser)
18119 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
18121 /* Consume the `=' token. */
18122 cp_lexer_consume_token (parser->lexer);
18124 cp_token *token = cp_lexer_peek_token (parser->lexer);
18126 /* Tell cp_parser_lambda_expression this is a default argument. */
18127 auto lvf = make_temp_override (parser->local_variables_forbidden_p);
18128 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
18130 /* Parse the default-argument. */
18131 push_deferring_access_checks (dk_no_deferred);
18132 tree default_argument = cp_parser_type_id (parser,
18133 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
18134 NULL);
18135 pop_deferring_access_checks ();
18137 if (flag_concepts && type_uses_auto (default_argument))
18139 error_at (token->location,
18140 "invalid use of %<auto%> in default template argument");
18141 return error_mark_node;
18144 return default_argument;
18147 /* Parse a default argument for a template template-parameter. */
18149 static tree
18150 cp_parser_default_template_template_argument (cp_parser *parser)
18152 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
18154 bool is_template;
18156 /* Consume the `='. */
18157 cp_lexer_consume_token (parser->lexer);
18158 /* Parse the id-expression. */
18159 push_deferring_access_checks (dk_no_deferred);
18160 /* save token before parsing the id-expression, for error
18161 reporting */
18162 const cp_token* token = cp_lexer_peek_token (parser->lexer);
18163 tree default_argument
18164 = cp_parser_id_expression (parser,
18165 /*template_keyword_p=*/false,
18166 /*check_dependency_p=*/true,
18167 /*template_p=*/&is_template,
18168 /*declarator_p=*/false,
18169 /*optional_p=*/false);
18170 if (TREE_CODE (default_argument) == TYPE_DECL)
18171 /* If the id-expression was a template-id that refers to
18172 a template-class, we already have the declaration here,
18173 so no further lookup is needed. */
18175 else
18176 /* Look up the name. */
18177 default_argument
18178 = cp_parser_lookup_name (parser, default_argument,
18179 none_type,
18180 /*is_template=*/is_template,
18181 /*is_namespace=*/false,
18182 /*check_dependency=*/true,
18183 /*ambiguous_decls=*/NULL,
18184 token->location);
18185 /* See if the default argument is valid. */
18186 default_argument = check_template_template_default_arg (default_argument);
18187 pop_deferring_access_checks ();
18188 return default_argument;
18191 /* Parse a template-parameter.
18193 template-parameter:
18194 type-parameter
18195 parameter-declaration
18197 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
18198 the parameter. The TREE_PURPOSE is the default value, if any.
18199 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
18200 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
18201 set to true iff this parameter is a parameter pack. */
18203 static tree
18204 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
18205 bool *is_parameter_pack)
18207 cp_token *token;
18208 cp_parameter_declarator *parameter_declarator;
18209 tree parm;
18211 /* Assume it is a type parameter or a template parameter. */
18212 *is_non_type = false;
18213 /* Assume it not a parameter pack. */
18214 *is_parameter_pack = false;
18215 /* Peek at the next token. */
18216 token = cp_lexer_peek_token (parser->lexer);
18217 /* If it is `template', we have a type-parameter. */
18218 if (token->keyword == RID_TEMPLATE)
18219 return cp_parser_type_parameter (parser, is_parameter_pack);
18220 /* If it is `class' or `typename' we do not know yet whether it is a
18221 type parameter or a non-type parameter. Consider:
18223 template <typename T, typename T::X X> ...
18227 template <class C, class D*> ...
18229 Here, the first parameter is a type parameter, and the second is
18230 a non-type parameter. We can tell by looking at the token after
18231 the identifier -- if it is a `,', `=', or `>' then we have a type
18232 parameter. */
18233 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
18235 /* Peek at the token after `class' or `typename'. */
18236 token = cp_lexer_peek_nth_token (parser->lexer, 2);
18237 /* If it's an ellipsis, we have a template type parameter
18238 pack. */
18239 if (token->type == CPP_ELLIPSIS)
18240 return cp_parser_type_parameter (parser, is_parameter_pack);
18241 /* If it's an identifier, skip it. */
18242 if (token->type == CPP_NAME)
18243 token = cp_lexer_peek_nth_token (parser->lexer, 3);
18244 /* Now, see if the token looks like the end of a template
18245 parameter. */
18246 if (token->type == CPP_COMMA
18247 || token->type == CPP_EQ
18248 || token->type == CPP_GREATER)
18249 return cp_parser_type_parameter (parser, is_parameter_pack);
18252 /* Otherwise, it is a non-type parameter or a constrained parameter.
18254 [temp.param]
18256 When parsing a default template-argument for a non-type
18257 template-parameter, the first non-nested `>' is taken as the end
18258 of the template parameter-list rather than a greater-than
18259 operator. */
18260 parameter_declarator
18261 = cp_parser_parameter_declaration (parser,
18262 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
18263 /*template_parm_p=*/true,
18264 /*parenthesized_p=*/NULL);
18266 if (!parameter_declarator)
18267 return error_mark_node;
18269 /* If the parameter declaration is marked as a parameter pack, set
18270 *IS_PARAMETER_PACK to notify the caller. */
18271 if (parameter_declarator->template_parameter_pack_p)
18272 *is_parameter_pack = true;
18274 if (parameter_declarator->default_argument)
18276 /* Can happen in some cases of erroneous input (c++/34892). */
18277 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18278 /* Consume the `...' for better error recovery. */
18279 cp_lexer_consume_token (parser->lexer);
18282 /* The parameter may have been constrained type parameter. */
18283 if (is_constrained_parameter (parameter_declarator))
18284 return finish_constrained_parameter (parser,
18285 parameter_declarator,
18286 is_non_type);
18288 // Now we're sure that the parameter is a non-type parameter.
18289 *is_non_type = true;
18291 parm = grokdeclarator (parameter_declarator->declarator,
18292 &parameter_declarator->decl_specifiers,
18293 TPARM, /*initialized=*/0,
18294 /*attrlist=*/NULL);
18295 if (parm == error_mark_node)
18296 return error_mark_node;
18298 return build_tree_list (parameter_declarator->default_argument, parm);
18301 /* Parse a type-parameter.
18303 type-parameter:
18304 class identifier [opt]
18305 class identifier [opt] = type-id
18306 typename identifier [opt]
18307 typename identifier [opt] = type-id
18308 template < template-parameter-list > class identifier [opt]
18309 template < template-parameter-list > class identifier [opt]
18310 = id-expression
18312 GNU Extension (variadic templates):
18314 type-parameter:
18315 class ... identifier [opt]
18316 typename ... identifier [opt]
18318 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
18319 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
18320 the declaration of the parameter.
18322 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
18324 static tree
18325 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
18327 cp_token *token;
18328 tree parameter;
18330 /* Look for a keyword to tell us what kind of parameter this is. */
18331 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
18332 if (!token)
18333 return error_mark_node;
18335 switch (token->keyword)
18337 case RID_CLASS:
18338 case RID_TYPENAME:
18340 tree identifier;
18341 tree default_argument;
18343 /* If the next token is an ellipsis, we have a template
18344 argument pack. */
18345 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18347 /* Consume the `...' token. */
18348 cp_lexer_consume_token (parser->lexer);
18349 maybe_warn_variadic_templates ();
18351 *is_parameter_pack = true;
18354 /* If the next token is an identifier, then it names the
18355 parameter. */
18356 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18357 identifier = cp_parser_identifier (parser);
18358 else
18359 identifier = NULL_TREE;
18361 /* Create the parameter. */
18362 parameter = finish_template_type_parm (class_type_node, identifier);
18364 /* If the next token is an `=', we have a default argument. */
18365 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18367 default_argument
18368 = cp_parser_default_type_template_argument (parser);
18370 /* Template parameter packs cannot have default
18371 arguments. */
18372 if (*is_parameter_pack)
18374 if (identifier)
18375 error_at (token->location,
18376 "template parameter pack %qD cannot have a "
18377 "default argument", identifier);
18378 else
18379 error_at (token->location,
18380 "template parameter packs cannot have "
18381 "default arguments");
18382 default_argument = NULL_TREE;
18384 else if (check_for_bare_parameter_packs (default_argument))
18385 default_argument = error_mark_node;
18387 else
18388 default_argument = NULL_TREE;
18390 /* Create the combined representation of the parameter and the
18391 default argument. */
18392 parameter = build_tree_list (default_argument, parameter);
18394 break;
18396 case RID_TEMPLATE:
18398 tree identifier;
18399 tree default_argument;
18401 /* Look for the `<'. */
18402 cp_parser_require (parser, CPP_LESS, RT_LESS);
18403 /* Parse the template-parameter-list. */
18404 cp_parser_template_parameter_list (parser);
18405 /* Look for the `>'. */
18406 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
18408 /* If template requirements are present, parse them. */
18409 if (flag_concepts)
18411 tree reqs = get_shorthand_constraints (current_template_parms);
18412 if (tree dreqs = cp_parser_requires_clause_opt (parser, false))
18413 reqs = combine_constraint_expressions (reqs, dreqs);
18414 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
18417 /* Look for the `class' or 'typename' keywords. */
18418 cp_parser_type_parameter_key (parser);
18419 /* If the next token is an ellipsis, we have a template
18420 argument pack. */
18421 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18423 /* Consume the `...' token. */
18424 cp_lexer_consume_token (parser->lexer);
18425 maybe_warn_variadic_templates ();
18427 *is_parameter_pack = true;
18429 /* If the next token is an `=', then there is a
18430 default-argument. If the next token is a `>', we are at
18431 the end of the parameter-list. If the next token is a `,',
18432 then we are at the end of this parameter. */
18433 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
18434 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
18435 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18437 identifier = cp_parser_identifier (parser);
18438 /* Treat invalid names as if the parameter were nameless. */
18439 if (identifier == error_mark_node)
18440 identifier = NULL_TREE;
18442 else
18443 identifier = NULL_TREE;
18445 /* Create the template parameter. */
18446 parameter = finish_template_template_parm (class_type_node,
18447 identifier);
18449 /* If the next token is an `=', then there is a
18450 default-argument. */
18451 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18453 default_argument
18454 = cp_parser_default_template_template_argument (parser);
18456 /* Template parameter packs cannot have default
18457 arguments. */
18458 if (*is_parameter_pack)
18460 if (identifier)
18461 error_at (token->location,
18462 "template parameter pack %qD cannot "
18463 "have a default argument",
18464 identifier);
18465 else
18466 error_at (token->location, "template parameter packs cannot "
18467 "have default arguments");
18468 default_argument = NULL_TREE;
18471 else
18472 default_argument = NULL_TREE;
18474 /* Create the combined representation of the parameter and the
18475 default argument. */
18476 parameter = build_tree_list (default_argument, parameter);
18478 break;
18480 default:
18481 gcc_unreachable ();
18482 break;
18485 return parameter;
18488 /* Parse a template-id.
18490 template-id:
18491 template-name < template-argument-list [opt] >
18493 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
18494 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
18495 returned. Otherwise, if the template-name names a function, or set
18496 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
18497 names a class, returns a TYPE_DECL for the specialization.
18499 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
18500 uninstantiated templates. */
18502 static tree
18503 cp_parser_template_id (cp_parser *parser,
18504 bool template_keyword_p,
18505 bool check_dependency_p,
18506 enum tag_types tag_type,
18507 bool is_declaration)
18509 tree templ;
18510 tree arguments;
18511 tree template_id;
18512 cp_token_position start_of_id = 0;
18513 cp_token *next_token = NULL, *next_token_2 = NULL;
18514 bool is_identifier;
18516 /* If the next token corresponds to a template-id, there is no need
18517 to reparse it. */
18518 cp_token *token = cp_lexer_peek_token (parser->lexer);
18520 if (token->type == CPP_TEMPLATE_ID)
18522 cp_lexer_consume_token (parser->lexer);
18523 return saved_checks_value (token->u.tree_check_value);
18526 /* Avoid performing name lookup if there is no possibility of
18527 finding a template-id. */
18528 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
18529 || (token->type == CPP_NAME
18530 && !cp_parser_nth_token_starts_template_argument_list_p
18531 (parser, 2)))
18533 cp_parser_error (parser, "expected template-id");
18534 return error_mark_node;
18537 /* Remember where the template-id starts. */
18538 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
18539 start_of_id = cp_lexer_token_position (parser->lexer, false);
18541 push_deferring_access_checks (dk_deferred);
18543 /* Parse the template-name. */
18544 is_identifier = false;
18545 templ = cp_parser_template_name (parser, template_keyword_p,
18546 check_dependency_p,
18547 is_declaration,
18548 tag_type,
18549 &is_identifier);
18551 /* Push any access checks inside the firewall we're about to create. */
18552 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
18553 pop_deferring_access_checks ();
18554 if (templ == error_mark_node || is_identifier)
18555 return templ;
18557 /* Since we're going to preserve any side-effects from this parse, set up a
18558 firewall to protect our callers from cp_parser_commit_to_tentative_parse
18559 in the template arguments. */
18560 tentative_firewall firewall (parser);
18561 reopen_deferring_access_checks (checks);
18563 /* If we find the sequence `[:' after a template-name, it's probably
18564 a digraph-typo for `< ::'. Substitute the tokens and check if we can
18565 parse correctly the argument list. */
18566 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
18567 == CPP_OPEN_SQUARE)
18568 && next_token->flags & DIGRAPH
18569 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
18570 == CPP_COLON)
18571 && !(next_token_2->flags & PREV_WHITE))
18573 cp_parser_parse_tentatively (parser);
18574 /* Change `:' into `::'. */
18575 next_token_2->type = CPP_SCOPE;
18576 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
18577 CPP_LESS. */
18578 cp_lexer_consume_token (parser->lexer);
18580 /* Parse the arguments. */
18581 arguments = cp_parser_enclosed_template_argument_list (parser);
18582 if (!cp_parser_parse_definitely (parser))
18584 /* If we couldn't parse an argument list, then we revert our changes
18585 and return simply an error. Maybe this is not a template-id
18586 after all. */
18587 next_token_2->type = CPP_COLON;
18588 cp_parser_error (parser, "expected %<<%>");
18589 pop_deferring_access_checks ();
18590 return error_mark_node;
18592 /* Otherwise, emit an error about the invalid digraph, but continue
18593 parsing because we got our argument list. */
18594 if (permerror (next_token->location,
18595 "%<<::%> cannot begin a template-argument list"))
18597 static bool hint = false;
18598 inform (next_token->location,
18599 "%<<:%> is an alternate spelling for %<[%>."
18600 " Insert whitespace between %<<%> and %<::%>");
18601 if (!hint && !flag_permissive)
18603 inform (next_token->location, "(if you use %<-fpermissive%> "
18604 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
18605 "accept your code)");
18606 hint = true;
18610 else
18612 /* Look for the `<' that starts the template-argument-list. */
18613 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
18615 pop_deferring_access_checks ();
18616 return error_mark_node;
18618 /* Parse the arguments. */
18619 arguments = cp_parser_enclosed_template_argument_list (parser);
18621 if ((cxx_dialect > cxx17)
18622 && (TREE_CODE (templ) == FUNCTION_DECL || identifier_p (templ))
18623 && !template_keyword_p
18624 && (cp_parser_error_occurred (parser)
18625 || cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)))
18627 /* This didn't go well. */
18628 if (TREE_CODE (templ) == FUNCTION_DECL)
18630 /* C++20 says that "function-name < a;" is now ill-formed. */
18631 if (cp_parser_error_occurred (parser))
18633 error_at (token->location, "invalid template-argument-list");
18634 inform (token->location, "function name as the left hand "
18635 "operand of %<<%> is ill-formed in C++20; wrap the "
18636 "function name in %<()%>");
18638 else
18639 /* We expect "f<targs>" to be followed by "(args)". */
18640 error_at (cp_lexer_peek_token (parser->lexer)->location,
18641 "expected %<(%> after template-argument-list");
18642 if (start_of_id)
18643 /* Purge all subsequent tokens. */
18644 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
18646 else
18647 cp_parser_simulate_error (parser);
18648 pop_deferring_access_checks ();
18649 return error_mark_node;
18653 /* Set the location to be of the form:
18654 template-name < template-argument-list [opt] >
18655 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18656 with caret == start at the start of the template-name,
18657 ranging until the closing '>'. */
18658 location_t combined_loc
18659 = make_location (token->location, token->location, parser->lexer);
18661 /* Check for concepts autos where they don't belong. We could
18662 identify types in some cases of identifier TEMPL, looking ahead
18663 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
18664 types. We reject them in functions, but if what we have is an
18665 identifier, even with none_type we can't conclude it's NOT a
18666 type, we have to wait for template substitution. */
18667 if (flag_concepts && check_auto_in_tmpl_args (templ, arguments))
18668 template_id = error_mark_node;
18669 /* Build a representation of the specialization. */
18670 else if (identifier_p (templ))
18671 template_id = build_min_nt_loc (combined_loc,
18672 TEMPLATE_ID_EXPR,
18673 templ, arguments);
18674 else if (DECL_TYPE_TEMPLATE_P (templ)
18675 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
18677 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
18678 template (rather than some instantiation thereof) only if
18679 is not nested within some other construct. For example, in
18680 "template <typename T> void f(T) { A<T>::", A<T> is just an
18681 instantiation of A. */
18682 bool entering_scope
18683 = (template_parm_scope_p ()
18684 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE));
18685 template_id
18686 = finish_template_type (templ, arguments, entering_scope);
18688 else if (concept_definition_p (templ))
18690 /* The caller will decide whether this is a concept check or type
18691 constraint. */
18692 template_id = build2_loc (combined_loc, TEMPLATE_ID_EXPR,
18693 boolean_type_node, templ, arguments);
18695 else if (variable_template_p (templ))
18697 template_id = lookup_template_variable (templ, arguments, tf_warning_or_error);
18698 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
18699 SET_EXPR_LOCATION (template_id, combined_loc);
18701 else if (TREE_CODE (templ) == TYPE_DECL
18702 && TREE_CODE (TREE_TYPE (templ)) == TYPENAME_TYPE)
18704 /* Some type template in dependent scope. */
18705 tree &name = TYPENAME_TYPE_FULLNAME (TREE_TYPE (templ));
18706 name = build_min_nt_loc (combined_loc,
18707 TEMPLATE_ID_EXPR,
18708 name, arguments);
18709 template_id = templ;
18711 else
18713 /* If it's not a class-template or a template-template, it should be
18714 a function-template. */
18715 gcc_assert (OVL_P (templ) || BASELINK_P (templ));
18717 template_id = lookup_template_function (templ, arguments);
18718 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
18719 SET_EXPR_LOCATION (template_id, combined_loc);
18722 /* If parsing tentatively, replace the sequence of tokens that makes
18723 up the template-id with a CPP_TEMPLATE_ID token. That way,
18724 should we re-parse the token stream, we will not have to repeat
18725 the effort required to do the parse, nor will we issue duplicate
18726 error messages about problems during instantiation of the
18727 template. */
18728 if (start_of_id
18729 /* Don't do this if we had a parse error in a declarator; re-parsing
18730 might succeed if a name changes meaning (60361). */
18731 && !(cp_parser_error_occurred (parser)
18732 && cp_parser_parsing_tentatively (parser)
18733 && parser->in_declarator_p))
18735 /* Reset the contents of the START_OF_ID token. */
18736 token->type = CPP_TEMPLATE_ID;
18737 token->location = combined_loc;
18739 /* Retrieve any deferred checks. Do not pop this access checks yet
18740 so the memory will not be reclaimed during token replacing below. */
18741 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
18742 token->tree_check_p = true;
18743 token->u.tree_check_value->value = template_id;
18744 token->u.tree_check_value->checks = get_deferred_access_checks ();
18745 token->keyword = RID_MAX;
18747 /* Purge all subsequent tokens. */
18748 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
18750 /* ??? Can we actually assume that, if template_id ==
18751 error_mark_node, we will have issued a diagnostic to the
18752 user, as opposed to simply marking the tentative parse as
18753 failed? */
18754 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
18755 error_at (token->location, "parse error in template argument list");
18758 pop_to_parent_deferring_access_checks ();
18759 return template_id;
18762 /* Like cp_parser_template_id, called in non-type context. */
18764 static tree
18765 cp_parser_template_id_expr (cp_parser *parser,
18766 bool template_keyword_p,
18767 bool check_dependency_p,
18768 bool is_declaration)
18770 tree x = cp_parser_template_id (parser, template_keyword_p, check_dependency_p,
18771 none_type, is_declaration);
18772 if (TREE_CODE (x) == TEMPLATE_ID_EXPR
18773 && concept_check_p (x))
18774 /* We didn't check the arguments in cp_parser_template_id; do that now. */
18775 return build_concept_id (x);
18776 return x;
18779 /* Parse a template-name.
18781 template-name:
18782 identifier
18784 The standard should actually say:
18786 template-name:
18787 identifier
18788 operator-function-id
18790 A defect report has been filed about this issue.
18792 A conversion-function-id cannot be a template name because they cannot
18793 be part of a template-id. In fact, looking at this code:
18795 a.operator K<int>()
18797 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
18798 It is impossible to call a templated conversion-function-id with an
18799 explicit argument list, since the only allowed template parameter is
18800 the type to which it is converting.
18802 If TEMPLATE_KEYWORD_P is true, then we have just seen the
18803 `template' keyword, in a construction like:
18805 T::template f<3>()
18807 In that case `f' is taken to be a template-name, even though there
18808 is no way of knowing for sure.
18810 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
18811 name refers to a set of overloaded functions, at least one of which
18812 is a template, or an IDENTIFIER_NODE with the name of the template,
18813 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
18814 names are looked up inside uninstantiated templates. */
18816 static tree
18817 cp_parser_template_name (cp_parser* parser,
18818 bool template_keyword_p,
18819 bool check_dependency_p,
18820 bool is_declaration,
18821 enum tag_types tag_type,
18822 bool *is_identifier)
18824 tree identifier;
18825 tree decl;
18826 cp_token *token = cp_lexer_peek_token (parser->lexer);
18828 /* If the next token is `operator', then we have either an
18829 operator-function-id or a conversion-function-id. */
18830 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
18832 /* We don't know whether we're looking at an
18833 operator-function-id or a conversion-function-id. */
18834 cp_parser_parse_tentatively (parser);
18835 /* Try an operator-function-id. */
18836 identifier = cp_parser_operator_function_id (parser);
18837 /* If that didn't work, try a conversion-function-id. */
18838 if (!cp_parser_parse_definitely (parser))
18840 cp_parser_error (parser, "expected template-name");
18841 return error_mark_node;
18844 /* Look for the identifier. */
18845 else
18846 identifier = cp_parser_identifier (parser);
18848 /* If we didn't find an identifier, we don't have a template-id. */
18849 if (identifier == error_mark_node)
18850 return error_mark_node;
18852 /* If the name immediately followed the `template' keyword, then it
18853 is a template-name. However, if the next token is not `<', then
18854 we do not treat it as a template-name, since it is not being used
18855 as part of a template-id. This enables us to handle constructs
18856 like:
18858 template <typename T> struct S { S(); };
18859 template <typename T> S<T>::S();
18861 correctly. We would treat `S' as a template -- if it were `S<T>'
18862 -- but we do not if there is no `<'. */
18864 if (processing_template_decl
18865 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
18867 /* In a declaration, in a dependent context, we pretend that the
18868 "template" keyword was present in order to improve error
18869 recovery. For example, given:
18871 template <typename T> void f(T::X<int>);
18873 we want to treat "X<int>" as a template-id. */
18874 if (is_declaration
18875 && !template_keyword_p
18876 && parser->scope && TYPE_P (parser->scope)
18877 && check_dependency_p
18878 && dependent_scope_p (parser->scope)
18879 /* Do not do this for dtors (or ctors), since they never
18880 need the template keyword before their name. */
18881 && !constructor_name_p (identifier, parser->scope))
18883 cp_token_position start = 0;
18885 /* Explain what went wrong. */
18886 error_at (token->location, "non-template %qD used as template",
18887 identifier);
18888 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
18889 parser->scope, identifier);
18890 /* If parsing tentatively, find the location of the "<" token. */
18891 if (cp_parser_simulate_error (parser))
18892 start = cp_lexer_token_position (parser->lexer, true);
18893 /* Parse the template arguments so that we can issue error
18894 messages about them. */
18895 cp_lexer_consume_token (parser->lexer);
18896 cp_parser_enclosed_template_argument_list (parser);
18897 /* Skip tokens until we find a good place from which to
18898 continue parsing. */
18899 cp_parser_skip_to_closing_parenthesis (parser,
18900 /*recovering=*/true,
18901 /*or_comma=*/true,
18902 /*consume_paren=*/false);
18903 /* If parsing tentatively, permanently remove the
18904 template argument list. That will prevent duplicate
18905 error messages from being issued about the missing
18906 "template" keyword. */
18907 if (start)
18908 cp_lexer_purge_tokens_after (parser->lexer, start);
18909 if (is_identifier)
18910 *is_identifier = true;
18911 parser->context->object_type = NULL_TREE;
18912 return identifier;
18915 /* If the "template" keyword is present, then there is generally
18916 no point in doing name-lookup, so we just return IDENTIFIER.
18917 But, if the qualifying scope is non-dependent then we can
18918 (and must) do name-lookup normally. */
18919 if (template_keyword_p)
18921 tree scope = (parser->scope ? parser->scope
18922 : parser->context->object_type);
18923 if (scope && TYPE_P (scope)
18924 && (!CLASS_TYPE_P (scope)
18925 || (check_dependency_p && dependent_scope_p (scope))))
18927 /* We're optimizing away the call to cp_parser_lookup_name, but
18928 we still need to do this. */
18929 parser->object_scope = parser->context->object_type;
18930 parser->context->object_type = NULL_TREE;
18931 return identifier;
18936 /* cp_parser_lookup_name clears OBJECT_TYPE. */
18937 tree scope = (parser->scope ? parser->scope
18938 : parser->context->object_type);
18940 /* Look up the name. */
18941 decl = cp_parser_lookup_name (parser, identifier,
18942 tag_type,
18943 /*is_template=*/1 + template_keyword_p,
18944 /*is_namespace=*/false,
18945 check_dependency_p,
18946 /*ambiguous_decls=*/NULL,
18947 token->location);
18949 decl = strip_using_decl (decl);
18951 /* 13.3 [temp.names] A < is interpreted as the delimiter of a
18952 template-argument-list if it follows a name that is not a
18953 conversion-function-id and
18954 - that follows the keyword template or a ~ after a nested-name-specifier or
18955 in a class member access expression, or
18956 - for which name lookup finds the injected-class-name of a class template
18957 or finds any declaration of a template, or
18958 - that is an unqualified name for which name lookup either finds one or
18959 more functions or finds nothing, or
18960 - that is a terminal name in a using-declarator (9.9), in a declarator-id
18961 (9.3.4), or in a type-only context other than a nested-name-specifier
18962 (13.8). */
18964 /* Handle injected-class-name. */
18965 decl = maybe_get_template_decl_from_type_decl (decl);
18967 /* If DECL is a template, then the name was a template-name. */
18968 if (TREE_CODE (decl) == TEMPLATE_DECL)
18970 if ((TREE_DEPRECATED (decl) || TREE_UNAVAILABLE (decl))
18971 && deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
18973 tree d = DECL_TEMPLATE_RESULT (decl);
18974 tree attr;
18975 if (TREE_CODE (d) == TYPE_DECL)
18976 attr = TYPE_ATTRIBUTES (TREE_TYPE (d));
18977 else
18978 attr = DECL_ATTRIBUTES (d);
18979 if (TREE_UNAVAILABLE (decl))
18981 attr = lookup_attribute ("unavailable", attr);
18982 error_unavailable_use (decl, attr);
18984 else if (TREE_DEPRECATED (decl)
18985 && deprecated_state != DEPRECATED_SUPPRESS)
18987 attr = lookup_attribute ("deprecated", attr);
18988 warn_deprecated_use (decl, attr);
18992 else
18994 /* Look through an overload set for any templates. */
18995 bool found = false;
18997 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
18998 !found && iter; ++iter)
18999 if (TREE_CODE (*iter) == TEMPLATE_DECL)
19000 found = true;
19002 /* "an unqualified name for which name lookup either finds one or more
19003 functions or finds nothing". */
19004 if (!found
19005 && (cxx_dialect > cxx17)
19006 && !scope
19007 && cp_lexer_next_token_is (parser->lexer, CPP_LESS)
19008 && tag_type == none_type)
19010 /* The "more functions" case. Just use the OVERLOAD as normally.
19011 We don't use is_overloaded_fn here to avoid considering
19012 BASELINKs. */
19013 if (TREE_CODE (decl) == OVERLOAD
19014 /* Name lookup found one function. */
19015 || TREE_CODE (decl) == FUNCTION_DECL
19016 /* Name lookup found nothing. */
19017 || decl == error_mark_node)
19018 found = true;
19021 /* "that follows the keyword template"..."in a type-only context" */
19022 if (!found && scope
19023 && (template_keyword_p || tag_type != none_type)
19024 && dependentish_scope_p (scope)
19025 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
19026 found = true;
19028 if (!found)
19030 /* The name does not name a template. */
19031 cp_parser_error (parser, "expected template-name");
19032 return error_mark_node;
19034 else if ((!DECL_P (decl) && !is_overloaded_fn (decl))
19035 || TREE_CODE (decl) == USING_DECL
19036 /* cp_parser_template_id can only handle some TYPE_DECLs. */
19037 || (TREE_CODE (decl) == TYPE_DECL
19038 && TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE))
19039 /* Repeat the lookup at instantiation time. */
19040 decl = identifier;
19043 return decl;
19046 /* Parse a template-argument-list.
19048 template-argument-list:
19049 template-argument ... [opt]
19050 template-argument-list , template-argument ... [opt]
19052 Returns a TREE_VEC containing the arguments. */
19054 static tree
19055 cp_parser_template_argument_list (cp_parser* parser)
19057 bool saved_in_template_argument_list_p;
19058 bool saved_ice_p;
19059 bool saved_non_ice_p;
19061 /* Don't create location wrapper nodes within a template-argument-list. */
19062 auto_suppress_location_wrappers sentinel;
19064 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
19065 parser->in_template_argument_list_p = true;
19066 /* Even if the template-id appears in an integral
19067 constant-expression, the contents of the argument list do
19068 not. */
19069 saved_ice_p = parser->integral_constant_expression_p;
19070 parser->integral_constant_expression_p = false;
19071 saved_non_ice_p = parser->non_integral_constant_expression_p;
19072 parser->non_integral_constant_expression_p = false;
19074 /* Parse the arguments. */
19075 auto_vec<tree, 10> args;
19078 if (!args.is_empty ())
19079 /* Consume the comma. */
19080 cp_lexer_consume_token (parser->lexer);
19082 /* Parse the template-argument. */
19083 tree argument = cp_parser_template_argument (parser);
19085 /* If the next token is an ellipsis, we're expanding a template
19086 argument pack. */
19087 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19089 if (argument == error_mark_node)
19091 cp_token *token = cp_lexer_peek_token (parser->lexer);
19092 error_at (token->location,
19093 "expected parameter pack before %<...%>");
19095 /* Consume the `...' token. */
19096 cp_lexer_consume_token (parser->lexer);
19098 /* Make the argument into a TYPE_PACK_EXPANSION or
19099 EXPR_PACK_EXPANSION. */
19100 argument = make_pack_expansion (argument);
19103 args.safe_push (argument);
19105 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
19107 int n_args = args.length ();
19108 tree vec = make_tree_vec (n_args);
19110 for (int i = 0; i < n_args; i++)
19111 TREE_VEC_ELT (vec, i) = args[i];
19113 parser->non_integral_constant_expression_p = saved_non_ice_p;
19114 parser->integral_constant_expression_p = saved_ice_p;
19115 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
19116 if (CHECKING_P)
19117 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
19118 return vec;
19121 /* Parse a template-argument.
19123 template-argument:
19124 assignment-expression
19125 type-id
19126 id-expression
19128 The representation is that of an assignment-expression, type-id, or
19129 id-expression -- except that the qualified id-expression is
19130 evaluated, so that the value returned is either a DECL or an
19131 OVERLOAD.
19133 Although the standard says "assignment-expression", it forbids
19134 throw-expressions or assignments in the template argument.
19135 Therefore, we use "conditional-expression" instead. */
19137 static tree
19138 cp_parser_template_argument (cp_parser* parser)
19140 tree argument;
19141 bool template_p;
19142 bool address_p;
19143 bool maybe_type_id = false;
19144 cp_token *token = NULL, *argument_start_token = NULL;
19145 location_t loc = 0;
19146 cp_id_kind idk;
19148 /* There's really no way to know what we're looking at, so we just
19149 try each alternative in order.
19151 [temp.arg]
19153 In a template-argument, an ambiguity between a type-id and an
19154 expression is resolved to a type-id, regardless of the form of
19155 the corresponding template-parameter.
19157 Therefore, we try a type-id first. */
19158 cp_parser_parse_tentatively (parser);
19159 argument = cp_parser_template_type_arg (parser);
19160 /* If there was no error parsing the type-id but the next token is a
19161 '>>', our behavior depends on which dialect of C++ we're
19162 parsing. In C++98, we probably found a typo for '> >'. But there
19163 are type-id which are also valid expressions. For instance:
19165 struct X { int operator >> (int); };
19166 template <int V> struct Foo {};
19167 Foo<X () >> 5> r;
19169 Here 'X()' is a valid type-id of a function type, but the user just
19170 wanted to write the expression "X() >> 5". Thus, we remember that we
19171 found a valid type-id, but we still try to parse the argument as an
19172 expression to see what happens.
19174 In C++0x, the '>>' will be considered two separate '>'
19175 tokens. */
19176 if (!cp_parser_error_occurred (parser)
19177 && ((cxx_dialect == cxx98
19178 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19179 /* Similarly for >= which
19180 cp_parser_next_token_ends_template_argument_p treats for
19181 diagnostics purposes as mistyped > =, but can be valid
19182 after a type-id. */
19183 || cp_lexer_next_token_is (parser->lexer, CPP_GREATER_EQ)))
19185 maybe_type_id = true;
19186 cp_parser_abort_tentative_parse (parser);
19188 else
19190 /* If the next token isn't a `,' or a `>', then this argument wasn't
19191 really finished. This means that the argument is not a valid
19192 type-id. */
19193 if (!cp_parser_next_token_ends_template_argument_p (parser))
19194 cp_parser_error (parser, "expected template-argument");
19195 /* If that worked, we're done. */
19196 if (cp_parser_parse_definitely (parser))
19197 return argument;
19199 /* We're still not sure what the argument will be. */
19200 cp_parser_parse_tentatively (parser);
19201 /* Try a template. */
19202 argument_start_token = cp_lexer_peek_token (parser->lexer);
19203 argument = cp_parser_id_expression (parser,
19204 /*template_keyword_p=*/false,
19205 /*check_dependency_p=*/true,
19206 &template_p,
19207 /*declarator_p=*/false,
19208 /*optional_p=*/false);
19209 /* If the next token isn't a `,' or a `>', then this argument wasn't
19210 really finished. */
19211 if (!cp_parser_next_token_ends_template_argument_p (parser))
19212 cp_parser_error (parser, "expected template-argument");
19213 if (!cp_parser_error_occurred (parser))
19215 /* Figure out what is being referred to. If the id-expression
19216 was for a class template specialization, then we will have a
19217 TYPE_DECL at this point. There is no need to do name lookup
19218 at this point in that case. */
19219 if (TREE_CODE (argument) != TYPE_DECL)
19220 argument = cp_parser_lookup_name (parser, argument,
19221 none_type,
19222 /*is_template=*/template_p,
19223 /*is_namespace=*/false,
19224 /*check_dependency=*/true,
19225 /*ambiguous_decls=*/NULL,
19226 argument_start_token->location);
19227 if (TREE_CODE (argument) != TEMPLATE_DECL
19228 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
19229 cp_parser_error (parser, "expected template-name");
19231 if (cp_parser_parse_definitely (parser))
19233 if (TREE_UNAVAILABLE (argument))
19234 error_unavailable_use (argument, NULL_TREE);
19235 else if (TREE_DEPRECATED (argument))
19236 warn_deprecated_use (argument, NULL_TREE);
19237 return argument;
19239 /* It must be a non-type argument. In C++17 any constant-expression is
19240 allowed. */
19241 if (cxx_dialect > cxx14)
19242 goto general_expr;
19244 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
19246 -- an integral constant-expression of integral or enumeration
19247 type; or
19249 -- the name of a non-type template-parameter; or
19251 -- the name of an object or function with external linkage...
19253 -- the address of an object or function with external linkage...
19255 -- a pointer to member... */
19256 /* Look for a non-type template parameter. */
19257 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19259 cp_parser_parse_tentatively (parser);
19260 argument = cp_parser_primary_expression (parser,
19261 /*address_p=*/false,
19262 /*cast_p=*/false,
19263 /*template_arg_p=*/true,
19264 &idk);
19265 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
19266 || !cp_parser_next_token_ends_template_argument_p (parser))
19267 cp_parser_simulate_error (parser);
19268 if (cp_parser_parse_definitely (parser))
19269 return argument;
19272 /* If the next token is "&", the argument must be the address of an
19273 object or function with external linkage. */
19274 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
19275 if (address_p)
19277 loc = cp_lexer_peek_token (parser->lexer)->location;
19278 cp_lexer_consume_token (parser->lexer);
19280 /* See if we might have an id-expression. */
19281 token = cp_lexer_peek_token (parser->lexer);
19282 if (token->type == CPP_NAME
19283 || token->keyword == RID_OPERATOR
19284 || token->type == CPP_SCOPE
19285 || token->type == CPP_TEMPLATE_ID
19286 || token->type == CPP_NESTED_NAME_SPECIFIER)
19288 cp_parser_parse_tentatively (parser);
19289 argument = cp_parser_primary_expression (parser,
19290 address_p,
19291 /*cast_p=*/false,
19292 /*template_arg_p=*/true,
19293 &idk);
19294 if (cp_parser_error_occurred (parser)
19295 || !cp_parser_next_token_ends_template_argument_p (parser))
19296 cp_parser_abort_tentative_parse (parser);
19297 else
19299 tree probe;
19301 if (INDIRECT_REF_P (argument))
19303 /* Strip the dereference temporarily. */
19304 gcc_assert (REFERENCE_REF_P (argument));
19305 argument = TREE_OPERAND (argument, 0);
19308 /* If we're in a template, we represent a qualified-id referring
19309 to a static data member as a SCOPE_REF even if the scope isn't
19310 dependent so that we can check access control later. */
19311 probe = argument;
19312 if (TREE_CODE (probe) == SCOPE_REF)
19313 probe = TREE_OPERAND (probe, 1);
19314 if (VAR_P (probe))
19316 /* A variable without external linkage might still be a
19317 valid constant-expression, so no error is issued here
19318 if the external-linkage check fails. */
19319 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
19320 cp_parser_simulate_error (parser);
19322 else if (is_overloaded_fn (argument))
19323 /* All overloaded functions are allowed; if the external
19324 linkage test does not pass, an error will be issued
19325 later. */
19327 else if (address_p
19328 && (TREE_CODE (argument) == OFFSET_REF
19329 || TREE_CODE (argument) == SCOPE_REF))
19330 /* A pointer-to-member. */
19332 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
19334 else
19335 cp_parser_simulate_error (parser);
19337 if (cp_parser_parse_definitely (parser))
19339 if (address_p)
19340 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
19341 NULL_TREE, tf_warning_or_error);
19342 else
19343 argument = convert_from_reference (argument);
19344 return argument;
19348 /* If the argument started with "&", there are no other valid
19349 alternatives at this point. */
19350 if (address_p)
19352 cp_parser_error (parser, "invalid non-type template argument");
19353 return error_mark_node;
19356 general_expr:
19357 /* If the argument wasn't successfully parsed as a type-id followed
19358 by '>>', the argument can only be a constant expression now.
19359 Otherwise, we try parsing the constant-expression tentatively,
19360 because the argument could really be a type-id. */
19361 if (maybe_type_id)
19362 cp_parser_parse_tentatively (parser);
19364 if (cxx_dialect <= cxx14)
19365 argument = cp_parser_constant_expression (parser);
19366 else
19368 /* In C++20, we can encounter a braced-init-list. */
19369 if (cxx_dialect >= cxx20
19370 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19371 return cp_parser_braced_list (parser);
19373 /* With C++17 generalized non-type template arguments we need to handle
19374 lvalue constant expressions, too. */
19375 argument = cp_parser_assignment_expression (parser);
19376 require_potential_constant_expression (argument);
19379 if (!maybe_type_id)
19380 return argument;
19381 if (!cp_parser_next_token_ends_template_argument_p (parser))
19382 cp_parser_error (parser, "expected template-argument");
19383 if (cp_parser_parse_definitely (parser))
19384 return argument;
19385 /* We did our best to parse the argument as a non type-id, but that
19386 was the only alternative that matched (albeit with a '>' after
19387 it). We can assume it's just a typo from the user, and a
19388 diagnostic will then be issued. */
19389 return cp_parser_template_type_arg (parser);
19392 /* Parse an explicit-instantiation.
19394 explicit-instantiation:
19395 template declaration
19397 Although the standard says `declaration', what it really means is:
19399 explicit-instantiation:
19400 template decl-specifier-seq [opt] declarator [opt] ;
19402 Things like `template int S<int>::i = 5, int S<double>::j;' are not
19403 supposed to be allowed. A defect report has been filed about this
19404 issue.
19406 GNU Extension:
19408 explicit-instantiation:
19409 storage-class-specifier template
19410 decl-specifier-seq [opt] declarator [opt] ;
19411 function-specifier template
19412 decl-specifier-seq [opt] declarator [opt] ; */
19414 static void
19415 cp_parser_explicit_instantiation (cp_parser* parser)
19417 int declares_class_or_enum;
19418 cp_decl_specifier_seq decl_specifiers;
19419 tree extension_specifier = NULL_TREE;
19421 auto_timevar tv (TV_TEMPLATE_INST);
19423 /* Look for an (optional) storage-class-specifier or
19424 function-specifier. */
19425 if (cp_parser_allow_gnu_extensions_p (parser))
19427 extension_specifier
19428 = cp_parser_storage_class_specifier_opt (parser);
19429 if (!extension_specifier)
19430 extension_specifier
19431 = cp_parser_function_specifier_opt (parser,
19432 /*decl_specs=*/NULL);
19435 /* Look for the `template' keyword. */
19436 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
19437 /* Let the front end know that we are processing an explicit
19438 instantiation. */
19439 begin_explicit_instantiation ();
19440 /* [temp.explicit] says that we are supposed to ignore access
19441 control while processing explicit instantiation directives. */
19442 push_deferring_access_checks (dk_no_check);
19443 /* Parse a decl-specifier-seq. */
19444 cp_parser_decl_specifier_seq (parser,
19445 CP_PARSER_FLAGS_OPTIONAL,
19446 &decl_specifiers,
19447 &declares_class_or_enum);
19449 cp_omp_declare_simd_data odsd;
19450 if (decl_specifiers.attributes && (flag_openmp || flag_openmp_simd))
19451 cp_parser_handle_directive_omp_attributes (parser,
19452 &decl_specifiers.attributes,
19453 &odsd, true);
19455 /* If there was exactly one decl-specifier, and it declared a class,
19456 and there's no declarator, then we have an explicit type
19457 instantiation. */
19458 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
19460 tree type = check_tag_decl (&decl_specifiers,
19461 /*explicit_type_instantiation_p=*/true);
19462 /* Turn access control back on for names used during
19463 template instantiation. */
19464 pop_deferring_access_checks ();
19465 if (type)
19466 do_type_instantiation (type, extension_specifier,
19467 /*complain=*/tf_error);
19469 else
19471 cp_declarator *declarator;
19472 tree decl;
19474 /* Parse the declarator. */
19475 declarator
19476 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19477 CP_PARSER_FLAGS_NONE,
19478 /*ctor_dtor_or_conv_p=*/NULL,
19479 /*parenthesized_p=*/NULL,
19480 /*member_p=*/false,
19481 /*friend_p=*/false,
19482 /*static_p=*/false);
19483 if (declares_class_or_enum & 2)
19484 cp_parser_check_for_definition_in_return_type (declarator,
19485 decl_specifiers.type,
19486 decl_specifiers.locations[ds_type_spec]);
19487 if (declarator != cp_error_declarator)
19489 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
19490 permerror (decl_specifiers.locations[ds_inline],
19491 "explicit instantiation shall not use"
19492 " %<inline%> specifier");
19493 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
19494 permerror (decl_specifiers.locations[ds_constexpr],
19495 "explicit instantiation shall not use"
19496 " %<constexpr%> specifier");
19497 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_consteval))
19498 permerror (decl_specifiers.locations[ds_consteval],
19499 "explicit instantiation shall not use"
19500 " %<consteval%> specifier");
19502 decl = grokdeclarator (declarator, &decl_specifiers,
19503 NORMAL, 0, &decl_specifiers.attributes);
19504 /* Turn access control back on for names used during
19505 template instantiation. */
19506 pop_deferring_access_checks ();
19507 /* Do the explicit instantiation. */
19508 do_decl_instantiation (decl, extension_specifier);
19510 else
19512 pop_deferring_access_checks ();
19513 /* Skip the body of the explicit instantiation. */
19514 cp_parser_skip_to_end_of_statement (parser);
19517 /* We're done with the instantiation. */
19518 end_explicit_instantiation ();
19520 cp_parser_consume_semicolon_at_end_of_statement (parser);
19522 cp_finalize_omp_declare_simd (parser, &odsd);
19525 /* Parse an explicit-specialization.
19527 explicit-specialization:
19528 template < > declaration
19530 Although the standard says `declaration', what it really means is:
19532 explicit-specialization:
19533 template <> decl-specifier [opt] init-declarator [opt] ;
19534 template <> function-definition
19535 template <> explicit-specialization
19536 template <> template-declaration */
19538 static void
19539 cp_parser_explicit_specialization (cp_parser* parser)
19541 cp_token *token = cp_lexer_peek_token (parser->lexer);
19543 /* Look for the `template' keyword. */
19544 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
19545 /* Look for the `<'. */
19546 cp_parser_require (parser, CPP_LESS, RT_LESS);
19547 /* Look for the `>'. */
19548 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
19549 /* We have processed another parameter list. */
19550 ++parser->num_template_parameter_lists;
19552 /* [temp]
19554 A template ... explicit specialization ... shall not have C
19555 linkage. */
19556 bool need_lang_pop = current_lang_name == lang_name_c;
19557 if (need_lang_pop)
19559 error_at (token->location, "template specialization with C linkage");
19560 maybe_show_extern_c_location ();
19562 /* Give it C++ linkage to avoid confusing other parts of the
19563 front end. */
19564 push_lang_context (lang_name_cplusplus);
19567 /* Let the front end know that we are beginning a specialization. */
19568 if (begin_specialization ())
19570 /* If the next keyword is `template', we need to figure out
19571 whether or not we're looking a template-declaration. */
19572 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19574 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
19575 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
19576 cp_parser_template_declaration_after_export (parser,
19577 /*member_p=*/false);
19578 else
19579 cp_parser_explicit_specialization (parser);
19581 else
19582 /* Parse the dependent declaration. */
19583 cp_parser_single_declaration (parser,
19584 /*checks=*/NULL,
19585 /*member_p=*/false,
19586 /*explicit_specialization_p=*/true,
19587 /*friend_p=*/NULL);
19590 /* We're done with the specialization. */
19591 end_specialization ();
19593 /* For the erroneous case of a template with C linkage, we pushed an
19594 implicit C++ linkage scope; exit that scope now. */
19595 if (need_lang_pop)
19596 pop_lang_context ();
19598 /* We're done with this parameter list. */
19599 --parser->num_template_parameter_lists;
19602 /* Preserve the attributes across a garbage collect (by making it a GC
19603 root), which can occur when parsing a member function. */
19605 static GTY(()) vec<tree, va_gc> *cp_parser_decl_specs_attrs;
19607 /* Parse a type-specifier.
19609 type-specifier:
19610 simple-type-specifier
19611 class-specifier
19612 enum-specifier
19613 elaborated-type-specifier
19614 cv-qualifier
19616 GNU Extension:
19618 type-specifier:
19619 __complex__
19621 Returns a representation of the type-specifier. For a
19622 class-specifier, enum-specifier, or elaborated-type-specifier, a
19623 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
19625 The parser flags FLAGS is used to control type-specifier parsing.
19627 If IS_DECLARATION is TRUE, then this type-specifier is appearing
19628 in a decl-specifier-seq.
19630 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
19631 class-specifier, enum-specifier, or elaborated-type-specifier, then
19632 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
19633 if a type is declared; 2 if it is defined. Otherwise, it is set to
19634 zero.
19636 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
19637 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
19638 is set to FALSE. */
19640 static tree
19641 cp_parser_type_specifier (cp_parser* parser,
19642 cp_parser_flags flags,
19643 cp_decl_specifier_seq *decl_specs,
19644 bool is_declaration,
19645 int* declares_class_or_enum,
19646 bool* is_cv_qualifier)
19648 tree type_spec = NULL_TREE;
19649 cp_token *token;
19650 enum rid keyword;
19651 cp_decl_spec ds = ds_last;
19653 /* Assume this type-specifier does not declare a new type. */
19654 if (declares_class_or_enum)
19655 *declares_class_or_enum = 0;
19656 /* And that it does not specify a cv-qualifier. */
19657 if (is_cv_qualifier)
19658 *is_cv_qualifier = false;
19659 /* Peek at the next token. */
19660 token = cp_lexer_peek_token (parser->lexer);
19662 /* If we're looking at a keyword, we can use that to guide the
19663 production we choose. */
19664 keyword = token->keyword;
19665 switch (keyword)
19667 case RID_ENUM:
19668 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
19669 goto elaborated_type_specifier;
19671 /* Look for the enum-specifier. */
19672 type_spec = cp_parser_enum_specifier (parser);
19673 /* If that worked, we're done. */
19674 if (type_spec)
19676 if (declares_class_or_enum)
19677 *declares_class_or_enum = 2;
19678 if (decl_specs)
19679 cp_parser_set_decl_spec_type (decl_specs,
19680 type_spec,
19681 token,
19682 /*type_definition_p=*/true);
19683 return type_spec;
19685 else
19686 goto elaborated_type_specifier;
19688 /* Any of these indicate either a class-specifier, or an
19689 elaborated-type-specifier. */
19690 case RID_CLASS:
19691 case RID_STRUCT:
19692 case RID_UNION:
19693 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
19694 goto elaborated_type_specifier;
19696 /* Parse tentatively so that we can back up if we don't find a
19697 class-specifier. */
19698 cp_parser_parse_tentatively (parser);
19699 if (decl_specs->attributes)
19700 vec_safe_push (cp_parser_decl_specs_attrs, decl_specs->attributes);
19701 /* Look for the class-specifier. */
19702 type_spec = cp_parser_class_specifier (parser);
19703 if (decl_specs->attributes)
19704 cp_parser_decl_specs_attrs->pop ();
19705 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
19706 /* If that worked, we're done. */
19707 if (cp_parser_parse_definitely (parser))
19709 if (declares_class_or_enum)
19710 *declares_class_or_enum = 2;
19711 if (decl_specs)
19712 cp_parser_set_decl_spec_type (decl_specs,
19713 type_spec,
19714 token,
19715 /*type_definition_p=*/true);
19716 return type_spec;
19719 /* Fall through. */
19720 elaborated_type_specifier:
19721 /* We're declaring (not defining) a class or enum. */
19722 if (declares_class_or_enum)
19723 *declares_class_or_enum = 1;
19725 /* Fall through. */
19726 case RID_TYPENAME:
19727 /* Look for an elaborated-type-specifier. */
19728 type_spec
19729 = (cp_parser_elaborated_type_specifier
19730 (parser,
19731 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
19732 is_declaration));
19733 if (decl_specs)
19734 cp_parser_set_decl_spec_type (decl_specs,
19735 type_spec,
19736 token,
19737 /*type_definition_p=*/false);
19738 return type_spec;
19740 case RID_CONST:
19741 ds = ds_const;
19742 if (is_cv_qualifier)
19743 *is_cv_qualifier = true;
19744 break;
19746 case RID_VOLATILE:
19747 ds = ds_volatile;
19748 if (is_cv_qualifier)
19749 *is_cv_qualifier = true;
19750 break;
19752 case RID_RESTRICT:
19753 ds = ds_restrict;
19754 if (is_cv_qualifier)
19755 *is_cv_qualifier = true;
19756 break;
19758 case RID_COMPLEX:
19759 /* The `__complex__' keyword is a GNU extension. */
19760 ds = ds_complex;
19761 break;
19763 default:
19764 break;
19767 /* Handle simple keywords. */
19768 if (ds != ds_last)
19770 if (decl_specs)
19772 set_and_check_decl_spec_loc (decl_specs, ds, token);
19773 decl_specs->any_specifiers_p = true;
19775 return cp_lexer_consume_token (parser->lexer)->u.value;
19778 /* If we do not already have a type-specifier, assume we are looking
19779 at a simple-type-specifier. */
19780 type_spec = cp_parser_simple_type_specifier (parser,
19781 decl_specs,
19782 flags);
19784 /* If we didn't find a type-specifier, and a type-specifier was not
19785 optional in this context, issue an error message. */
19786 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
19788 cp_parser_error (parser, "expected type specifier");
19789 return error_mark_node;
19792 return type_spec;
19795 /* Parse a simple-type-specifier.
19797 simple-type-specifier:
19798 :: [opt] nested-name-specifier [opt] type-name
19799 :: [opt] nested-name-specifier template template-id
19800 char
19801 wchar_t
19802 bool
19803 short
19805 long
19806 signed
19807 unsigned
19808 float
19809 double
19810 void
19812 C++11 Extension:
19814 simple-type-specifier:
19815 auto
19816 decltype ( expression )
19817 char16_t
19818 char32_t
19819 __underlying_type ( type-id )
19821 C++17 extension:
19823 nested-name-specifier(opt) template-name
19825 GNU Extension:
19827 simple-type-specifier:
19828 __int128
19829 __typeof__ unary-expression
19830 __typeof__ ( type-id )
19831 __typeof__ ( type-id ) { initializer-list , [opt] }
19833 Concepts Extension:
19835 simple-type-specifier:
19836 constrained-type-specifier
19838 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
19839 appropriately updated. */
19841 static tree
19842 cp_parser_simple_type_specifier (cp_parser* parser,
19843 cp_decl_specifier_seq *decl_specs,
19844 cp_parser_flags flags)
19846 tree type = NULL_TREE;
19847 cp_token *token;
19848 int idx;
19850 /* Peek at the next token. */
19851 token = cp_lexer_peek_token (parser->lexer);
19853 /* If we're looking at a keyword, things are easy. */
19854 switch (token->keyword)
19856 case RID_CHAR:
19857 if (decl_specs)
19858 decl_specs->explicit_char_p = true;
19859 type = char_type_node;
19860 break;
19861 case RID_CHAR8:
19862 type = char8_type_node;
19863 break;
19864 case RID_CHAR16:
19865 type = char16_type_node;
19866 break;
19867 case RID_CHAR32:
19868 type = char32_type_node;
19869 break;
19870 case RID_WCHAR:
19871 type = wchar_type_node;
19872 break;
19873 case RID_BOOL:
19874 type = boolean_type_node;
19875 break;
19876 case RID_SHORT:
19877 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
19878 type = short_integer_type_node;
19879 break;
19880 case RID_INT:
19881 if (decl_specs)
19882 decl_specs->explicit_int_p = true;
19883 type = integer_type_node;
19884 break;
19885 case RID_INT_N_0:
19886 case RID_INT_N_1:
19887 case RID_INT_N_2:
19888 case RID_INT_N_3:
19889 idx = token->keyword - RID_INT_N_0;
19890 if (! int_n_enabled_p [idx])
19891 break;
19892 if (decl_specs)
19894 decl_specs->explicit_intN_p = true;
19895 decl_specs->int_n_idx = idx;
19896 /* Check if the alternate "__intN__" form has been used instead of
19897 "__intN". */
19898 if (startswith (IDENTIFIER_POINTER (token->u.value)
19899 + (IDENTIFIER_LENGTH (token->u.value) - 2), "__"))
19900 decl_specs->int_n_alt = true;
19902 type = int_n_trees [idx].signed_type;
19903 break;
19904 case RID_LONG:
19905 if (decl_specs)
19906 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
19907 type = long_integer_type_node;
19908 break;
19909 case RID_SIGNED:
19910 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
19911 type = integer_type_node;
19912 break;
19913 case RID_UNSIGNED:
19914 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
19915 type = unsigned_type_node;
19916 break;
19917 case RID_FLOAT:
19918 type = float_type_node;
19919 break;
19920 case RID_DOUBLE:
19921 type = double_type_node;
19922 break;
19923 CASE_RID_FLOATN_NX:
19924 type = FLOATN_NX_TYPE_NODE (token->keyword - RID_FLOATN_NX_FIRST);
19925 if (type == NULL_TREE)
19926 error ("%<_Float%d%s%> is not supported on this target",
19927 floatn_nx_types[token->keyword - RID_FLOATN_NX_FIRST].n,
19928 floatn_nx_types[token->keyword - RID_FLOATN_NX_FIRST].extended
19929 ? "x" : "");
19930 break;
19931 case RID_VOID:
19932 type = void_type_node;
19933 break;
19935 case RID_AUTO:
19936 maybe_warn_cpp0x (CPP0X_AUTO);
19937 if (parser->auto_is_implicit_function_template_parm_p)
19939 /* The 'auto' might be the placeholder return type for a function decl
19940 with trailing return type. */
19941 bool have_trailing_return_fn_decl = false;
19943 cp_parser_parse_tentatively (parser);
19944 cp_lexer_consume_token (parser->lexer);
19945 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
19946 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
19947 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
19948 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
19950 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19952 cp_lexer_consume_token (parser->lexer);
19953 cp_parser_skip_to_closing_parenthesis (parser,
19954 /*recovering*/false,
19955 /*or_comma*/false,
19956 /*consume_paren*/true);
19957 continue;
19960 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
19962 have_trailing_return_fn_decl = true;
19963 break;
19966 cp_lexer_consume_token (parser->lexer);
19968 cp_parser_abort_tentative_parse (parser);
19970 if (have_trailing_return_fn_decl)
19972 type = make_auto ();
19973 break;
19976 if (cxx_dialect >= cxx14)
19978 type = synthesize_implicit_template_parm (parser, NULL_TREE);
19979 type = TREE_TYPE (type);
19981 else
19982 type = error_mark_node;
19984 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
19986 if (cxx_dialect < cxx14)
19987 error_at (token->location,
19988 "use of %<auto%> in lambda parameter declaration "
19989 "only available with "
19990 "%<-std=c++14%> or %<-std=gnu++14%>");
19992 else if (!flag_concepts_ts && parser->in_template_argument_list_p)
19993 pedwarn (token->location, 0,
19994 "use of %<auto%> in template argument "
19995 "only available with %<-fconcepts-ts%>");
19996 else if (!flag_concepts)
19997 pedwarn (token->location, 0,
19998 "use of %<auto%> in parameter declaration "
19999 "only available with %<-std=c++20%> or %<-fconcepts%>");
20000 else if (cxx_dialect < cxx14)
20001 error_at (token->location,
20002 "use of %<auto%> in parameter declaration "
20003 "only available with "
20004 "%<-std=c++14%> or %<-std=gnu++14%>");
20006 else
20007 type = make_auto ();
20008 break;
20010 case RID_DECLTYPE:
20011 /* Since DR 743, decltype can either be a simple-type-specifier by
20012 itself or begin a nested-name-specifier. Parsing it will replace
20013 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
20014 handling below decide what to do. */
20015 cp_parser_decltype (parser);
20016 cp_lexer_set_token_position (parser->lexer, token);
20017 break;
20019 case RID_TYPEOF:
20020 /* Consume the `typeof' token. */
20021 cp_lexer_consume_token (parser->lexer);
20022 /* Parse the operand to `typeof'. */
20023 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
20024 /* If it is not already a TYPE, take its type. */
20025 if (!TYPE_P (type))
20026 type = finish_typeof (type);
20028 if (decl_specs)
20029 cp_parser_set_decl_spec_type (decl_specs, type,
20030 token,
20031 /*type_definition_p=*/false);
20033 return type;
20035 #define DEFTRAIT_TYPE(CODE, NAME, ARITY) \
20036 case RID_##CODE:
20037 #include "cp-trait.def"
20038 #undef DEFTRAIT_TYPE
20039 type = cp_parser_trait (parser, token->keyword);
20040 if (decl_specs)
20041 cp_parser_set_decl_spec_type (decl_specs, type,
20042 token,
20043 /*type_definition_p=*/false);
20045 return type;
20047 default:
20048 break;
20051 /* If token is an already-parsed decltype not followed by ::,
20052 it's a simple-type-specifier. */
20053 if (token->type == CPP_DECLTYPE
20054 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
20056 type = saved_checks_value (token->u.tree_check_value);
20057 if (decl_specs)
20059 cp_parser_set_decl_spec_type (decl_specs, type,
20060 token,
20061 /*type_definition_p=*/false);
20062 /* Remember that we are handling a decltype in order to
20063 implement the resolution of DR 1510 when the argument
20064 isn't instantiation dependent. */
20065 decl_specs->decltype_p = true;
20067 cp_lexer_consume_token (parser->lexer);
20068 return type;
20071 /* If the type-specifier was for a built-in type, we're done. */
20072 if (type)
20074 /* Record the type. */
20075 if (decl_specs
20076 && (token->keyword != RID_SIGNED
20077 && token->keyword != RID_UNSIGNED
20078 && token->keyword != RID_SHORT
20079 && token->keyword != RID_LONG))
20080 cp_parser_set_decl_spec_type (decl_specs,
20081 type,
20082 token,
20083 /*type_definition_p=*/false);
20084 if (decl_specs)
20085 decl_specs->any_specifiers_p = true;
20087 /* Consume the token. */
20088 cp_lexer_consume_token (parser->lexer);
20090 if (type == error_mark_node)
20091 return error_mark_node;
20093 /* There is no valid C++ program where a non-template type is
20094 followed by a "<". That usually indicates that the user thought
20095 that the type was a template. */
20096 cp_parser_check_for_invalid_template_id (parser, type, none_type,
20097 token->location);
20099 return TYPE_NAME (type);
20102 /* The type-specifier must be a user-defined type. */
20103 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
20105 bool qualified_p;
20106 bool global_p;
20107 const bool typename_p = (cxx_dialect >= cxx20
20108 && (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
20110 /* Don't gobble tokens or issue error messages if this is an
20111 optional type-specifier. */
20112 if (flags & CP_PARSER_FLAGS_OPTIONAL)
20113 cp_parser_parse_tentatively (parser);
20115 /* Remember current tentative parsing state -- if we know we need
20116 a type, we can give better diagnostics here. */
20117 bool tent = cp_parser_parsing_tentatively (parser);
20119 token = cp_lexer_peek_token (parser->lexer);
20121 /* Look for the optional `::' operator. */
20122 global_p
20123 = (cp_parser_global_scope_opt (parser,
20124 /*current_scope_valid_p=*/false)
20125 != NULL_TREE);
20126 /* Look for the nested-name specifier. */
20127 qualified_p
20128 = (cp_parser_nested_name_specifier_opt (parser,
20129 /*typename_keyword_p=*/false,
20130 /*check_dependency_p=*/true,
20131 /*type_p=*/false,
20132 /*is_declaration=*/false)
20133 != NULL_TREE);
20134 /* If we have seen a nested-name-specifier, and the next token
20135 is `template', then we are using the template-id production. */
20136 if (parser->scope
20137 && cp_parser_optional_template_keyword (parser))
20139 /* Look for the template-id. */
20140 type = cp_parser_template_id (parser,
20141 /*template_keyword_p=*/true,
20142 /*check_dependency_p=*/true,
20143 none_type,
20144 /*is_declaration=*/false);
20145 /* If the template-id did not name a type, we are out of
20146 luck. */
20147 if (TREE_CODE (type) != TYPE_DECL)
20149 /* ...unless we pretend we have seen 'typename'. */
20150 if (typename_p)
20151 type = cp_parser_make_typename_type (parser, type,
20152 token->location);
20153 else
20155 cp_parser_error (parser, "expected template-id for type");
20156 type = error_mark_node;
20160 /* DR 1812: A < following a qualified-id in a typename-specifier
20161 could safely be assumed to begin a template argument list, so
20162 the template keyword should be optional. */
20163 else if (parser->scope
20164 && qualified_p
20165 && typename_p
20166 && cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID))
20168 cp_parser_parse_tentatively (parser);
20170 type = cp_parser_template_id (parser,
20171 /*template_keyword_p=*/true,
20172 /*check_dependency_p=*/true,
20173 none_type,
20174 /*is_declaration=*/false);
20175 /* This is handled below, so back off. */
20176 if (type && concept_check_p (type))
20177 cp_parser_simulate_error (parser);
20179 if (!cp_parser_parse_definitely (parser))
20180 type = NULL_TREE;
20181 else if (TREE_CODE (type) == TEMPLATE_ID_EXPR)
20182 type = make_typename_type (parser->scope, type, typename_type,
20183 /*complain=*/tf_error);
20184 else if (TREE_CODE (type) != TYPE_DECL)
20185 type = NULL_TREE;
20188 /* Otherwise, look for a type-name. */
20189 if (!type)
20191 if (cxx_dialect >= cxx17 || flag_concepts)
20192 cp_parser_parse_tentatively (parser);
20194 type = cp_parser_type_name (parser, (qualified_p && typename_p));
20196 if ((cxx_dialect >= cxx17 || flag_concepts)
20197 && !cp_parser_parse_definitely (parser))
20198 type = NULL_TREE;
20201 if (!type && flag_concepts && decl_specs)
20203 /* Try for a type-constraint with template arguments. We check
20204 decl_specs here to avoid trying this for a functional cast. */
20206 cp_parser_parse_tentatively (parser);
20208 type = cp_parser_template_id (parser,
20209 /*template_keyword_p=*/false,
20210 /*check_dependency_p=*/true,
20211 none_type,
20212 /*is_declaration=*/false);
20213 if (type && concept_check_p (type))
20215 location_t loc = EXPR_LOCATION (type);
20216 type = cp_parser_placeholder_type_specifier (parser, loc,
20217 type, tent);
20218 if (tent && type == error_mark_node)
20219 /* Perhaps it's a concept-check expression. */
20220 cp_parser_simulate_error (parser);
20222 else
20223 cp_parser_simulate_error (parser);
20225 if (!cp_parser_parse_definitely (parser))
20226 type = NULL_TREE;
20229 if (!type && cxx_dialect >= cxx17)
20231 /* Try class template argument deduction or type-constraint without
20232 template arguments. */
20233 tree name = cp_parser_identifier (parser);
20234 if (name && TREE_CODE (name) == IDENTIFIER_NODE
20235 && parser->scope != error_mark_node)
20237 location_t loc
20238 = cp_lexer_previous_token (parser->lexer)->location;
20239 tree tmpl = cp_parser_lookup_name (parser, name,
20240 none_type,
20241 /*is_template=*/false,
20242 /*is_namespace=*/false,
20243 /*check_dependency=*/true,
20244 /*ambiguous_decls=*/NULL,
20245 token->location);
20246 if (tmpl && tmpl != error_mark_node
20247 && ctad_template_p (tmpl))
20248 type = make_template_placeholder (tmpl);
20249 else if (flag_concepts && tmpl && concept_definition_p (tmpl))
20250 type = cp_parser_placeholder_type_specifier (parser, loc,
20251 tmpl, tent);
20252 else
20254 type = error_mark_node;
20255 if (!cp_parser_simulate_error (parser))
20256 cp_parser_name_lookup_error (parser, name, tmpl,
20257 NLE_TYPE, token->location);
20260 else
20261 type = error_mark_node;
20264 /* If it didn't work out, we don't have a TYPE. */
20265 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
20266 && !cp_parser_parse_definitely (parser))
20267 type = NULL_TREE;
20269 /* Keep track of all name-lookups performed in class scopes. */
20270 if (type
20271 && !global_p
20272 && !qualified_p
20273 && TREE_CODE (type) == TYPE_DECL
20274 && identifier_p (DECL_NAME (type)))
20275 maybe_note_name_used_in_class (DECL_NAME (type), type);
20277 if (type && decl_specs)
20278 cp_parser_set_decl_spec_type (decl_specs, type,
20279 token,
20280 /*type_definition_p=*/false);
20283 /* If we didn't get a type-name, issue an error message. */
20284 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
20286 cp_parser_error (parser, "expected type-name");
20287 return error_mark_node;
20290 if (type && type != error_mark_node)
20292 /* See if TYPE is an Objective-C type, and if so, parse and
20293 accept any protocol references following it. Do this before
20294 the cp_parser_check_for_invalid_template_id() call, because
20295 Objective-C types can be followed by '<...>' which would
20296 enclose protocol names rather than template arguments, and so
20297 everything is fine. */
20298 if (c_dialect_objc () && !parser->scope
20299 && (objc_is_id (type) || objc_is_class_name (type)))
20301 tree protos = cp_parser_objc_protocol_refs_opt (parser);
20302 tree qual_type = objc_get_protocol_qualified_type (type, protos);
20304 /* Clobber the "unqualified" type previously entered into
20305 DECL_SPECS with the new, improved protocol-qualified version. */
20306 if (decl_specs)
20307 decl_specs->type = qual_type;
20309 return qual_type;
20312 /* There is no valid C++ program where a non-template type is
20313 followed by a "<". That usually indicates that the user
20314 thought that the type was a template. */
20315 cp_parser_check_for_invalid_template_id (parser, type,
20316 none_type,
20317 token->location);
20320 return type;
20323 /* Parse the remainder of a placholder-type-specifier.
20325 placeholder-type-specifier:
20326 type-constraint_opt auto
20327 type-constraint_opt decltype(auto)
20329 The raw form of the constraint is parsed in cp_parser_simple_type_specifier
20330 and passed as TMPL. This function converts TMPL to an actual type-constraint,
20331 parses the placeholder type, and performs some contextual syntactic analysis.
20333 LOC provides the location of the template name.
20335 TENTATIVE is true if the type-specifier parsing is tentative; in that case,
20336 don't give an error if TMPL isn't a valid type-constraint, as the template-id
20337 might actually be a concept-check,
20339 Note that the Concepts TS allows the auto or decltype(auto) to be
20340 omitted in a constrained-type-specifier. */
20342 static tree
20343 cp_parser_placeholder_type_specifier (cp_parser *parser, location_t loc,
20344 tree tmpl, bool tentative)
20346 if (tmpl == error_mark_node)
20347 return error_mark_node;
20349 tree orig_tmpl = tmpl;
20351 /* Get the arguments as written for subsequent analysis. */
20352 tree args = NULL_TREE;
20353 if (TREE_CODE (tmpl) == TEMPLATE_ID_EXPR)
20355 args = TREE_OPERAND (tmpl, 1);
20356 tmpl = TREE_OPERAND (tmpl, 0);
20358 else
20359 /* A concept-name with no arguments can't be an expression. */
20360 tentative = false;
20362 tsubst_flags_t complain = tentative ? tf_none : tf_warning_or_error;
20364 /* Get the concept and prototype parameter for the constraint. */
20365 tree_pair info = finish_type_constraints (tmpl, args, complain);
20366 tree con = info.first;
20367 tree proto = info.second;
20368 if (con == error_mark_node)
20369 return error_mark_node;
20371 /* As per the standard, require auto or decltype(auto), except in some
20372 cases (template parameter lists, -fconcepts-ts enabled). */
20373 cp_token *placeholder = NULL, *close_paren = NULL;
20374 if (cxx_dialect >= cxx20)
20376 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
20377 placeholder = cp_lexer_consume_token (parser->lexer);
20378 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DECLTYPE))
20380 placeholder = cp_lexer_consume_token (parser->lexer);
20381 matching_parens parens;
20382 parens.require_open (parser);
20383 cp_parser_require_keyword (parser, RID_AUTO, RT_AUTO);
20384 close_paren = parens.require_close (parser);
20388 /* A type constraint constrains a contextually determined type or type
20389 parameter pack. However, the Concepts TS does allow concepts
20390 to introduce non-type and template template parameters. */
20391 if (TREE_CODE (proto) != TYPE_DECL)
20393 if (!flag_concepts_ts
20394 || !processing_template_parmlist)
20396 if (!tentative)
20398 error_at (loc, "%qE does not constrain a type", DECL_NAME (con));
20399 inform (DECL_SOURCE_LOCATION (con), "concept defined here");
20401 return error_mark_node;
20405 /* In a template parameter list, a type-parameter can be introduced
20406 by type-constraints alone. */
20407 if (processing_template_parmlist && !placeholder)
20409 /* In a default argument we may not be creating new parameters. */
20410 if (parser->local_variables_forbidden_p & LOCAL_VARS_FORBIDDEN)
20412 /* If this assert turns out to be false, do error() instead. */
20413 gcc_assert (tentative);
20414 return error_mark_node;
20416 return build_constrained_parameter (con, proto, args);
20419 /* Diagnose issues placeholder issues. */
20420 if (!flag_concepts_ts
20421 && !parser->in_result_type_constraint_p
20422 && !placeholder)
20424 if (tentative)
20425 /* Perhaps it's a concept-check expression (c++/91073). */
20426 return error_mark_node;
20428 tree id = build_nt (TEMPLATE_ID_EXPR, tmpl, args);
20429 tree expr = DECL_P (orig_tmpl) ? DECL_NAME (con) : id;
20430 error_at (input_location,
20431 "expected %<auto%> or %<decltype(auto)%> after %qE", expr);
20432 /* Fall through. This is an error of omission. */
20434 else if (parser->in_result_type_constraint_p && placeholder)
20436 /* A trailing return type only allows type-constraints. */
20437 error_at (input_location,
20438 "unexpected placeholder in constrained result type");
20441 /* In a parameter-declaration-clause, a placeholder-type-specifier
20442 results in an invented template parameter. */
20443 if (parser->auto_is_implicit_function_template_parm_p)
20445 if (close_paren)
20447 location_t loc = make_location (placeholder->location,
20448 placeholder->location,
20449 close_paren->location);
20450 error_at (loc, "cannot declare a parameter with %<decltype(auto)%>");
20451 return error_mark_node;
20453 tree parm = build_constrained_parameter (con, proto, args);
20454 return synthesize_implicit_template_parm (parser, parm);
20457 /* Determine if the type should be deduced using template argument
20458 deduction or decltype deduction. Note that the latter is always
20459 used for type-constraints in trailing return types. */
20460 bool decltype_p = placeholder
20461 ? placeholder->keyword == RID_DECLTYPE
20462 : parser->in_result_type_constraint_p;
20464 /* Otherwise, this is the type of a variable or return type. */
20465 if (decltype_p)
20466 return make_constrained_decltype_auto (con, args);
20467 else
20468 return make_constrained_auto (con, args);
20471 /* Parse a type-name.
20473 type-name:
20474 class-name
20475 enum-name
20476 typedef-name
20477 simple-template-id [in c++0x]
20479 enum-name:
20480 identifier
20482 typedef-name:
20483 identifier
20485 Concepts:
20487 type-name:
20488 concept-name
20489 partial-concept-id
20491 concept-name:
20492 identifier
20494 Returns a TYPE_DECL for the type. */
20496 static tree
20497 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
20499 tree type_decl;
20501 /* We can't know yet whether it is a class-name or not. */
20502 cp_parser_parse_tentatively (parser);
20503 /* Try a class-name. */
20504 type_decl = cp_parser_class_name (parser,
20505 typename_keyword_p,
20506 /*template_keyword_p=*/false,
20507 none_type,
20508 /*check_dependency_p=*/true,
20509 /*class_head_p=*/false,
20510 /*is_declaration=*/false);
20511 /* If it's not a class-name, keep looking. */
20512 if (!cp_parser_parse_definitely (parser))
20514 if (cxx_dialect < cxx11)
20515 /* It must be a typedef-name or an enum-name. */
20516 return cp_parser_nonclass_name (parser);
20518 cp_parser_parse_tentatively (parser);
20519 /* It is either a simple-template-id representing an
20520 instantiation of an alias template... */
20521 type_decl = cp_parser_template_id (parser,
20522 /*template_keyword_p=*/false,
20523 /*check_dependency_p=*/true,
20524 none_type,
20525 /*is_declaration=*/false);
20526 /* Note that this must be an instantiation of an alias template
20527 because [temp.names]/6 says:
20529 A template-id that names an alias template specialization
20530 is a type-name.
20532 Whereas [temp.names]/7 says:
20534 A simple-template-id that names a class template
20535 specialization is a class-name.
20537 With concepts, this could also be a partial-concept-id that
20538 declares a non-type template parameter. */
20539 if (type_decl != NULL_TREE
20540 && TREE_CODE (type_decl) == TYPE_DECL
20541 && TYPE_DECL_ALIAS_P (type_decl))
20542 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
20543 else
20544 cp_parser_simulate_error (parser);
20546 if (!cp_parser_parse_definitely (parser))
20547 /* ... Or a typedef-name or an enum-name. */
20548 return cp_parser_nonclass_name (parser);
20551 return type_decl;
20554 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
20555 or a concept-name.
20557 enum-name:
20558 identifier
20560 typedef-name:
20561 identifier
20563 concept-name:
20564 identifier
20566 Returns a TYPE_DECL for the type. */
20568 static tree
20569 cp_parser_nonclass_name (cp_parser* parser)
20571 tree type_decl;
20572 tree identifier;
20574 cp_token *token = cp_lexer_peek_token (parser->lexer);
20575 identifier = cp_parser_identifier (parser);
20576 if (identifier == error_mark_node)
20577 return error_mark_node;
20579 /* Look up the type-name. */
20580 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
20582 type_decl = strip_using_decl (type_decl);
20584 if (TREE_CODE (type_decl) != TYPE_DECL
20585 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
20587 /* See if this is an Objective-C type. */
20588 tree protos = cp_parser_objc_protocol_refs_opt (parser);
20589 tree type = objc_get_protocol_qualified_type (identifier, protos);
20590 if (type)
20591 type_decl = TYPE_NAME (type);
20594 /* Issue an error if we did not find a type-name. */
20595 if (TREE_CODE (type_decl) != TYPE_DECL
20596 /* In Objective-C, we have the complication that class names are
20597 normally type names and start declarations (eg, the
20598 "NSObject" in "NSObject *object;"), but can be used in an
20599 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
20600 is an expression. So, a classname followed by a dot is not a
20601 valid type-name. */
20602 || (objc_is_class_name (TREE_TYPE (type_decl))
20603 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
20605 if (!cp_parser_simulate_error (parser))
20606 cp_parser_name_lookup_error (parser, identifier, type_decl,
20607 NLE_TYPE, token->location);
20608 return error_mark_node;
20610 /* Remember that the name was used in the definition of the
20611 current class so that we can check later to see if the
20612 meaning would have been different after the class was
20613 entirely defined. */
20614 else if (type_decl != error_mark_node
20615 && !parser->scope)
20616 maybe_note_name_used_in_class (identifier, type_decl);
20618 return type_decl;
20621 /* Parse an elaborated-type-specifier. Note that the grammar given
20622 here incorporates the resolution to DR68.
20624 elaborated-type-specifier:
20625 class-key :: [opt] nested-name-specifier [opt] identifier
20626 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
20627 enum-key :: [opt] nested-name-specifier [opt] identifier
20628 typename :: [opt] nested-name-specifier identifier
20629 typename :: [opt] nested-name-specifier template [opt]
20630 template-id
20632 GNU extension:
20634 elaborated-type-specifier:
20635 class-key attributes :: [opt] nested-name-specifier [opt] identifier
20636 class-key attributes :: [opt] nested-name-specifier [opt]
20637 template [opt] template-id
20638 enum attributes :: [opt] nested-name-specifier [opt] identifier
20640 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
20641 declared `friend'. If IS_DECLARATION is TRUE, then this
20642 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
20643 something is being declared.
20645 Returns the TYPE specified. */
20647 static tree
20648 cp_parser_elaborated_type_specifier (cp_parser* parser,
20649 bool is_friend,
20650 bool is_declaration)
20652 enum tag_types tag_type;
20653 tree identifier;
20654 tree type = NULL_TREE;
20655 tree attributes = NULL_TREE;
20656 tree globalscope;
20657 cp_token *token = NULL;
20659 /* For class and enum types the location of the class-key or enum-key. */
20660 location_t key_loc = cp_lexer_peek_token (parser->lexer)->location;
20661 /* For a scoped enum, the 'class' or 'struct' keyword id. */
20662 rid scoped_key = RID_MAX;
20664 /* See if we're looking at the `enum' keyword. */
20665 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
20667 /* Consume the `enum' token. */
20668 cp_lexer_consume_token (parser->lexer);
20669 /* Remember that it's an enumeration type. */
20670 tag_type = enum_type;
20671 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
20672 enums) is used here. */
20673 cp_token *token = cp_lexer_peek_token (parser->lexer);
20674 if (cp_parser_is_keyword (token, scoped_key = RID_CLASS)
20675 || cp_parser_is_keyword (token, scoped_key = RID_STRUCT))
20677 location_t loc = token->location;
20678 gcc_rich_location richloc (loc);
20679 richloc.add_range (input_location);
20680 richloc.add_fixit_remove ();
20681 pedwarn (&richloc, 0, "elaborated-type-specifier for "
20682 "a scoped enum must not use the %qD keyword",
20683 token->u.value);
20684 /* Consume the `struct' or `class' and parse it anyway. */
20685 cp_lexer_consume_token (parser->lexer);
20686 /* Create a combined location for the whole scoped-enum-key. */
20687 key_loc = make_location (key_loc, key_loc, loc);
20689 else
20690 scoped_key = RID_MAX;
20692 /* Parse the attributes. */
20693 attributes = cp_parser_attributes_opt (parser);
20695 /* Or, it might be `typename'. */
20696 else if (cp_lexer_next_token_is_keyword (parser->lexer,
20697 RID_TYPENAME))
20699 /* Consume the `typename' token. */
20700 cp_lexer_consume_token (parser->lexer);
20701 /* Remember that it's a `typename' type. */
20702 tag_type = typename_type;
20704 /* Otherwise it must be a class-key. */
20705 else
20707 key_loc = cp_lexer_peek_token (parser->lexer)->location;
20708 tag_type = cp_parser_class_key (parser);
20709 if (tag_type == none_type)
20710 return error_mark_node;
20711 /* Parse the attributes. */
20712 attributes = cp_parser_attributes_opt (parser);
20715 /* Look for the `::' operator. */
20716 globalscope = cp_parser_global_scope_opt (parser,
20717 /*current_scope_valid_p=*/false);
20718 /* Look for the nested-name-specifier. */
20719 tree nested_name_specifier;
20720 if (tag_type == typename_type && !globalscope)
20722 nested_name_specifier
20723 = cp_parser_nested_name_specifier (parser,
20724 /*typename_keyword_p=*/true,
20725 /*check_dependency_p=*/true,
20726 /*type_p=*/true,
20727 is_declaration);
20728 if (!nested_name_specifier)
20729 return error_mark_node;
20731 else
20732 /* Even though `typename' is not present, the proposed resolution
20733 to Core Issue 180 says that in `class A<T>::B', `B' should be
20734 considered a type-name, even if `A<T>' is dependent. */
20735 nested_name_specifier
20736 = cp_parser_nested_name_specifier_opt (parser,
20737 /*typename_keyword_p=*/true,
20738 /*check_dependency_p=*/true,
20739 /*type_p=*/true,
20740 is_declaration);
20741 /* For everything but enumeration types, consider a template-id.
20742 For an enumeration type, consider only a plain identifier. */
20743 if (tag_type != enum_type)
20745 bool template_p = false;
20746 tree decl;
20748 /* Allow the `template' keyword. */
20749 template_p = cp_parser_optional_template_keyword (parser);
20750 /* If we didn't see `template', we don't know if there's a
20751 template-id or not. */
20752 if (!template_p)
20753 cp_parser_parse_tentatively (parser);
20754 /* The `template' keyword must follow a nested-name-specifier. */
20755 else if (!nested_name_specifier && !globalscope)
20757 cp_parser_error (parser, "%<template%> must follow a nested-"
20758 "name-specifier");
20759 return error_mark_node;
20762 /* Parse the template-id. */
20763 token = cp_lexer_peek_token (parser->lexer);
20764 decl = cp_parser_template_id (parser, template_p,
20765 /*check_dependency_p=*/true,
20766 tag_type,
20767 is_declaration);
20768 /* If we didn't find a template-id, look for an ordinary
20769 identifier. */
20770 if (!template_p && !cp_parser_parse_definitely (parser))
20772 /* We can get here when cp_parser_template_id, called by
20773 cp_parser_class_name with tag_type == none_type, succeeds
20774 and caches a BASELINK. Then, when called again here,
20775 instead of failing and returning an error_mark_node
20776 returns it (see template/typename17.C in C++11).
20777 ??? Could we diagnose this earlier? */
20778 else if (tag_type == typename_type && BASELINK_P (decl))
20780 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
20781 type = error_mark_node;
20783 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
20784 in effect, then we must assume that, upon instantiation, the
20785 template will correspond to a class. */
20786 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
20787 && tag_type == typename_type)
20788 type = make_typename_type (parser->scope, decl,
20789 typename_type,
20790 /*complain=*/tf_error);
20791 /* If the `typename' keyword is in effect and DECL is not a type
20792 decl, then type is non existent. */
20793 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
20795 else if (TREE_CODE (decl) == TYPE_DECL)
20797 type = check_elaborated_type_specifier (tag_type, decl,
20798 /*allow_template_p=*/true);
20800 /* If the next token is a semicolon, this must be a specialization,
20801 instantiation, or friend declaration. Check the scope while we
20802 still know whether or not we had a nested-name-specifier. */
20803 if (type != error_mark_node
20804 && !nested_name_specifier && !is_friend
20805 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20806 check_unqualified_spec_or_inst (type, token->location);
20808 else if (decl == error_mark_node)
20809 type = error_mark_node;
20812 if (!type)
20814 token = cp_lexer_peek_token (parser->lexer);
20815 identifier = cp_parser_identifier (parser);
20817 if (identifier == error_mark_node)
20819 parser->scope = NULL_TREE;
20820 return error_mark_node;
20823 /* For a `typename', we needn't call xref_tag. */
20824 if (tag_type == typename_type
20825 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
20826 return cp_parser_make_typename_type (parser, identifier,
20827 token->location);
20829 /* Template parameter lists apply only if we are not within a
20830 function parameter list. */
20831 bool template_parm_lists_apply
20832 = parser->num_template_parameter_lists;
20833 if (template_parm_lists_apply)
20834 for (cp_binding_level *s = current_binding_level;
20835 s && s->kind != sk_template_parms;
20836 s = s->level_chain)
20837 if (s->kind == sk_function_parms)
20838 template_parm_lists_apply = false;
20840 /* Look up a qualified name in the usual way. */
20841 if (parser->scope)
20843 tree decl;
20844 tree ambiguous_decls;
20846 decl = cp_parser_lookup_name (parser, identifier,
20847 tag_type,
20848 /*is_template=*/false,
20849 /*is_namespace=*/false,
20850 /*check_dependency=*/true,
20851 &ambiguous_decls,
20852 token->location);
20854 /* If the lookup was ambiguous, an error will already have been
20855 issued. */
20856 if (ambiguous_decls)
20857 return error_mark_node;
20859 /* If we are parsing friend declaration, DECL may be a
20860 TEMPLATE_DECL tree node here. However, we need to check
20861 whether this TEMPLATE_DECL results in valid code. Consider
20862 the following example:
20864 namespace N {
20865 template <class T> class C {};
20867 class X {
20868 template <class T> friend class N::C; // #1, valid code
20870 template <class T> class Y {
20871 friend class N::C; // #2, invalid code
20874 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
20875 name lookup of `N::C'. We see that friend declaration must
20876 be template for the code to be valid. Note that
20877 processing_template_decl does not work here since it is
20878 always 1 for the above two cases. */
20880 decl = (cp_parser_maybe_treat_template_as_class
20881 (decl, /*tag_name_p=*/is_friend
20882 && template_parm_lists_apply));
20884 if (TREE_CODE (decl) != TYPE_DECL)
20886 cp_parser_diagnose_invalid_type_name (parser,
20887 identifier,
20888 token->location);
20889 return error_mark_node;
20892 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
20894 bool allow_template = (template_parm_lists_apply
20895 || DECL_SELF_REFERENCE_P (decl));
20896 type = check_elaborated_type_specifier (tag_type, decl,
20897 allow_template);
20899 if (type == error_mark_node)
20900 return error_mark_node;
20903 /* Forward declarations of nested types, such as
20905 class C1::C2;
20906 class C1::C2::C3;
20908 are invalid unless all components preceding the final '::'
20909 are complete. If all enclosing types are complete, these
20910 declarations become merely pointless.
20912 Invalid forward declarations of nested types are errors
20913 caught elsewhere in parsing. Those that are pointless arrive
20914 here. */
20916 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20917 && !is_friend && is_declaration
20918 && !processing_explicit_instantiation)
20919 warning (0, "declaration %qD does not declare anything", decl);
20921 type = TREE_TYPE (decl);
20923 else
20925 /* An elaborated-type-specifier sometimes introduces a new type and
20926 sometimes names an existing type. Normally, the rule is that it
20927 introduces a new type only if there is not an existing type of
20928 the same name already in scope. For example, given:
20930 struct S {};
20931 void f() { struct S s; }
20933 the `struct S' in the body of `f' is the same `struct S' as in
20934 the global scope; the existing definition is used. However, if
20935 there were no global declaration, this would introduce a new
20936 local class named `S'.
20938 An exception to this rule applies to the following code:
20940 namespace N { struct S; }
20942 Here, the elaborated-type-specifier names a new type
20943 unconditionally; even if there is already an `S' in the
20944 containing scope this declaration names a new type.
20945 This exception only applies if the elaborated-type-specifier
20946 forms the complete declaration:
20948 [class.name]
20950 A declaration consisting solely of `class-key identifier ;' is
20951 either a redeclaration of the name in the current scope or a
20952 forward declaration of the identifier as a class name. It
20953 introduces the name into the current scope.
20955 We are in this situation precisely when the next token is a `;'.
20957 An exception to the exception is that a `friend' declaration does
20958 *not* name a new type; i.e., given:
20960 struct S { friend struct T; };
20962 `T' is not a new type in the scope of `S'.
20964 Also, `new struct S' or `sizeof (struct S)' never results in the
20965 definition of a new type; a new type can only be declared in a
20966 declaration context. */
20968 TAG_how how;
20970 if (is_friend)
20971 /* Friends have special name lookup rules. */
20972 how = TAG_how::HIDDEN_FRIEND;
20973 else if (is_declaration
20974 && cp_lexer_next_token_is (parser->lexer,
20975 CPP_SEMICOLON))
20976 /* This is a `class-key identifier ;' */
20977 how = TAG_how::CURRENT_ONLY;
20978 else
20979 how = TAG_how::GLOBAL;
20981 bool template_p =
20982 (template_parm_lists_apply
20983 && (cp_parser_next_token_starts_class_definition_p (parser)
20984 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
20985 /* An unqualified name was used to reference this type, so
20986 there were no qualifying templates. */
20987 if (template_parm_lists_apply
20988 && !cp_parser_check_template_parameters (parser,
20989 /*num_templates=*/0,
20990 /*template_id*/false,
20991 token->location,
20992 /*declarator=*/NULL))
20993 return error_mark_node;
20995 type = xref_tag (tag_type, identifier, how, template_p);
20999 if (type == error_mark_node)
21000 return error_mark_node;
21002 /* Allow attributes on forward declarations of classes. */
21003 if (attributes)
21005 if (TREE_CODE (type) == TYPENAME_TYPE)
21006 warning (OPT_Wattributes,
21007 "attributes ignored on uninstantiated type");
21008 else if (tag_type != enum_type
21009 && TREE_CODE (type) != BOUND_TEMPLATE_TEMPLATE_PARM
21010 && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
21011 && ! processing_explicit_instantiation)
21012 warning (OPT_Wattributes,
21013 "attributes ignored on template instantiation");
21014 else if (is_friend && cxx11_attribute_p (attributes))
21016 if (warning (OPT_Wattributes, "attribute ignored"))
21017 inform (input_location, "an attribute that appertains to a friend "
21018 "declaration that is not a definition is ignored");
21020 else if (is_declaration && cp_parser_declares_only_class_p (parser))
21021 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
21022 else
21023 warning (OPT_Wattributes,
21024 "attributes ignored on elaborated-type-specifier that is "
21025 "not a forward declaration");
21028 if (tag_type == enum_type)
21029 cp_parser_maybe_warn_enum_key (parser, key_loc, type, scoped_key);
21030 else
21032 /* Diagnose class/struct/union mismatches. IS_DECLARATION is false
21033 for alias definition. */
21034 bool decl_class = (is_declaration
21035 && cp_parser_declares_only_class_p (parser));
21036 cp_parser_check_class_key (parser, key_loc, tag_type, type, false,
21037 decl_class);
21039 /* Indicate whether this class was declared as a `class' or as a
21040 `struct'. */
21041 if (CLASS_TYPE_P (type) && !currently_open_class (type))
21042 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
21045 /* A "<" cannot follow an elaborated type specifier. If that
21046 happens, the user was probably trying to form a template-id. */
21047 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
21048 token->location);
21050 return type;
21053 /* Parse an enum-specifier.
21055 enum-specifier:
21056 enum-head { enumerator-list [opt] }
21057 enum-head { enumerator-list , } [C++0x]
21059 enum-head:
21060 enum-key identifier [opt] enum-base [opt]
21061 enum-key nested-name-specifier identifier enum-base [opt]
21063 enum-key:
21064 enum
21065 enum class [C++0x]
21066 enum struct [C++0x]
21068 enum-base: [C++0x]
21069 : type-specifier-seq
21071 opaque-enum-specifier:
21072 enum-key identifier enum-base [opt] ;
21074 GNU Extensions:
21075 enum-key attributes[opt] identifier [opt] enum-base [opt]
21076 { enumerator-list [opt] }attributes[opt]
21077 enum-key attributes[opt] identifier [opt] enum-base [opt]
21078 { enumerator-list, }attributes[opt] [C++0x]
21080 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
21081 if the token stream isn't an enum-specifier after all. */
21083 static tree
21084 cp_parser_enum_specifier (cp_parser* parser)
21086 tree identifier;
21087 tree type = NULL_TREE;
21088 tree prev_scope;
21089 tree nested_name_specifier = NULL_TREE;
21090 tree attributes;
21091 bool scoped_enum_p = false;
21092 bool has_underlying_type = false;
21093 bool nested_being_defined = false;
21094 bool new_value_list = false;
21095 bool is_new_type = false;
21096 bool is_unnamed = false;
21097 tree underlying_type = NULL_TREE;
21098 cp_token *type_start_token = NULL;
21099 auto cleanup = make_temp_override (parser->colon_corrects_to_scope_p, false);
21101 /* Parse tentatively so that we can back up if we don't find a
21102 enum-specifier. */
21103 cp_parser_parse_tentatively (parser);
21105 /* Caller guarantees that the current token is 'enum', an identifier
21106 possibly follows, and the token after that is an opening brace.
21107 If we don't have an identifier, fabricate an anonymous name for
21108 the enumeration being defined. */
21109 cp_lexer_consume_token (parser->lexer);
21111 /* Parse the "class" or "struct", which indicates a scoped
21112 enumeration type in C++0x. */
21113 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
21114 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
21116 if (cxx_dialect < cxx11)
21117 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
21119 /* Consume the `struct' or `class' token. */
21120 cp_lexer_consume_token (parser->lexer);
21122 scoped_enum_p = true;
21125 attributes = cp_parser_attributes_opt (parser);
21127 /* Clear the qualification. */
21128 parser->scope = NULL_TREE;
21129 parser->qualifying_scope = NULL_TREE;
21130 parser->object_scope = NULL_TREE;
21132 /* Figure out in what scope the declaration is being placed. */
21133 prev_scope = current_scope ();
21135 type_start_token = cp_lexer_peek_token (parser->lexer);
21137 push_deferring_access_checks (dk_no_check);
21138 nested_name_specifier
21139 = cp_parser_nested_name_specifier_opt (parser,
21140 /*typename_keyword_p=*/true,
21141 /*check_dependency_p=*/false,
21142 /*type_p=*/false,
21143 /*is_declaration=*/false);
21145 if (nested_name_specifier)
21147 tree name;
21149 identifier = cp_parser_identifier (parser);
21150 name = cp_parser_lookup_name (parser, identifier,
21151 enum_type,
21152 /*is_template=*/false,
21153 /*is_namespace=*/false,
21154 /*check_dependency=*/true,
21155 /*ambiguous_decls=*/NULL,
21156 input_location);
21157 if (name && name != error_mark_node)
21159 type = TREE_TYPE (name);
21160 if (TREE_CODE (type) == TYPENAME_TYPE)
21162 /* Are template enums allowed in ISO? */
21163 if (template_parm_scope_p ())
21164 pedwarn (type_start_token->location, OPT_Wpedantic,
21165 "%qD is an enumeration template", name);
21166 /* ignore a typename reference, for it will be solved by name
21167 in start_enum. */
21168 type = NULL_TREE;
21171 else if (nested_name_specifier == error_mark_node)
21172 /* We already issued an error. */;
21173 else
21175 error_at (type_start_token->location,
21176 "%qD does not name an enumeration in %qT",
21177 identifier, nested_name_specifier);
21178 nested_name_specifier = error_mark_node;
21181 else
21183 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21184 identifier = cp_parser_identifier (parser);
21185 else
21187 identifier = make_anon_name ();
21188 is_unnamed = true;
21189 if (scoped_enum_p)
21190 error_at (type_start_token->location,
21191 "unnamed scoped enum is not allowed");
21194 pop_deferring_access_checks ();
21196 /* Check for the `:' that denotes a specified underlying type in C++0x.
21197 Note that a ':' could also indicate a bitfield width, however. */
21198 location_t colon_loc = UNKNOWN_LOCATION;
21199 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
21201 cp_decl_specifier_seq type_specifiers;
21203 /* Consume the `:'. */
21204 colon_loc = cp_lexer_peek_token (parser->lexer)->location;
21205 cp_lexer_consume_token (parser->lexer);
21207 auto tdf
21208 = make_temp_override (parser->type_definition_forbidden_message,
21209 G_("types may not be defined in enum-base"));
21211 /* Parse the type-specifier-seq. */
21212 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
21213 /*is_declaration=*/false,
21214 /*is_trailing_return=*/false,
21215 &type_specifiers);
21217 /* At this point this is surely not elaborated type specifier. */
21218 if (!cp_parser_parse_definitely (parser))
21219 return NULL_TREE;
21221 if (cxx_dialect < cxx11)
21222 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
21224 has_underlying_type = true;
21226 /* If that didn't work, stop. */
21227 if (type_specifiers.type != error_mark_node)
21229 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
21230 /*initialized=*/0, NULL);
21231 if (underlying_type == error_mark_node
21232 || check_for_bare_parameter_packs (underlying_type))
21233 underlying_type = NULL_TREE;
21237 /* Look for the `{' but don't consume it yet. */
21238 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21240 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
21242 if (has_underlying_type)
21243 cp_parser_commit_to_tentative_parse (parser);
21244 cp_parser_error (parser, "expected %<{%>");
21245 if (has_underlying_type)
21246 return error_mark_node;
21248 /* An opaque-enum-specifier must have a ';' here. */
21249 if ((scoped_enum_p || underlying_type)
21250 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21252 if (has_underlying_type)
21253 pedwarn (colon_loc,
21254 OPT_Welaborated_enum_base,
21255 "declaration of enumeration with "
21256 "fixed underlying type and no enumerator list is "
21257 "only permitted as a standalone declaration");
21258 else
21259 cp_parser_error (parser, "expected %<;%> or %<{%>");
21263 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
21264 return NULL_TREE;
21266 if (nested_name_specifier)
21268 if (CLASS_TYPE_P (nested_name_specifier))
21270 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
21271 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
21272 push_scope (nested_name_specifier);
21274 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
21275 push_nested_namespace (nested_name_specifier);
21278 /* Issue an error message if type-definitions are forbidden here. */
21279 if (!cp_parser_check_type_definition (parser))
21280 type = error_mark_node;
21281 else
21282 /* Create the new type. We do this before consuming the opening
21283 brace so the enum will be recorded as being on the line of its
21284 tag (or the 'enum' keyword, if there is no tag). */
21285 type = start_enum (identifier, type, underlying_type,
21286 attributes, scoped_enum_p, &is_new_type);
21288 /* If the next token is not '{' it is an opaque-enum-specifier or an
21289 elaborated-type-specifier. */
21290 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21292 auto_timevar tv (TV_PARSE_ENUM);
21294 if (nested_name_specifier
21295 && nested_name_specifier != error_mark_node)
21297 /* The following catches invalid code such as:
21298 enum class S<int>::E { A, B, C }; */
21299 if (!processing_specialization
21300 && CLASS_TYPE_P (nested_name_specifier)
21301 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
21302 error_at (type_start_token->location, "cannot add an enumerator "
21303 "list to a template instantiation");
21305 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
21307 error_at (type_start_token->location,
21308 "%<%T::%E%> has not been declared",
21309 TYPE_CONTEXT (nested_name_specifier),
21310 nested_name_specifier);
21311 type = error_mark_node;
21313 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
21314 && !CLASS_TYPE_P (nested_name_specifier))
21316 error_at (type_start_token->location, "nested name specifier "
21317 "%qT for enum declaration does not name a class "
21318 "or namespace", nested_name_specifier);
21319 type = error_mark_node;
21321 /* If that scope does not contain the scope in which the
21322 class was originally declared, the program is invalid. */
21323 else if (prev_scope && !is_ancestor (prev_scope,
21324 nested_name_specifier))
21326 if (at_namespace_scope_p ())
21327 error_at (type_start_token->location,
21328 "declaration of %qD in namespace %qD which does not "
21329 "enclose %qD",
21330 type, prev_scope, nested_name_specifier);
21331 else
21332 error_at (type_start_token->location,
21333 "declaration of %qD in %qD which does not "
21334 "enclose %qD",
21335 type, prev_scope, nested_name_specifier);
21336 type = error_mark_node;
21338 /* If that scope is the scope where the declaration is being placed
21339 the program is invalid. */
21340 else if (CLASS_TYPE_P (nested_name_specifier)
21341 && CLASS_TYPE_P (prev_scope)
21342 && same_type_p (nested_name_specifier, prev_scope))
21344 permerror (type_start_token->location,
21345 "extra qualification not allowed");
21346 nested_name_specifier = NULL_TREE;
21350 if (scoped_enum_p)
21351 begin_scope (sk_scoped_enum, type);
21353 /* Consume the opening brace. */
21354 matching_braces braces;
21355 braces.consume_open (parser);
21357 if (type == error_mark_node)
21358 ; /* Nothing to add */
21359 else if (OPAQUE_ENUM_P (type)
21360 || (cxx_dialect > cxx98 && processing_specialization))
21362 new_value_list = true;
21363 SET_OPAQUE_ENUM_P (type, false);
21364 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
21366 else
21368 error_at (type_start_token->location,
21369 "multiple definition of %q#T", type);
21370 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
21371 "previous definition here");
21372 type = error_mark_node;
21375 if (type == error_mark_node)
21376 cp_parser_skip_to_end_of_block_or_statement (parser);
21377 /* If the next token is not '}', then there are some enumerators. */
21378 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21380 if (is_unnamed && !scoped_enum_p
21381 /* Don't warn for enum {} a; here. */
21382 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SEMICOLON))
21383 pedwarn (type_start_token->location, OPT_Wpedantic,
21384 "ISO C++ forbids empty unnamed enum");
21386 else
21388 /* We've seen a '{' so we know we're in an enum-specifier.
21389 Commit to any tentative parse to get syntax errors. */
21390 cp_parser_commit_to_tentative_parse (parser);
21391 cp_parser_enumerator_list (parser, type);
21394 /* Consume the final '}'. */
21395 braces.require_close (parser);
21397 if (scoped_enum_p)
21398 finish_scope ();
21400 else
21402 /* If a ';' follows, then it is an opaque-enum-specifier
21403 and additional restrictions apply. */
21404 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21406 if (is_unnamed)
21407 error_at (type_start_token->location,
21408 "opaque-enum-specifier without name");
21409 else if (nested_name_specifier)
21410 error_at (type_start_token->location,
21411 "opaque-enum-specifier must use a simple identifier");
21415 /* Look for trailing attributes to apply to this enumeration, and
21416 apply them if appropriate. */
21417 if (cp_parser_allow_gnu_extensions_p (parser))
21419 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
21420 cplus_decl_attributes (&type,
21421 trailing_attr,
21422 (int) ATTR_FLAG_TYPE_IN_PLACE);
21425 /* Finish up the enumeration. */
21426 if (type != error_mark_node)
21428 if (new_value_list)
21429 finish_enum_value_list (type);
21430 if (is_new_type)
21431 finish_enum (type);
21434 if (nested_name_specifier)
21436 if (CLASS_TYPE_P (nested_name_specifier))
21438 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
21439 pop_scope (nested_name_specifier);
21441 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
21442 pop_nested_namespace (nested_name_specifier);
21444 return type;
21447 /* Parse an enumerator-list. The enumerators all have the indicated
21448 TYPE.
21450 enumerator-list:
21451 enumerator-definition
21452 enumerator-list , enumerator-definition */
21454 static void
21455 cp_parser_enumerator_list (cp_parser* parser, tree type)
21457 while (true)
21459 /* Parse an enumerator-definition. */
21460 cp_parser_enumerator_definition (parser, type);
21462 /* If the next token is not a ',', we've reached the end of
21463 the list. */
21464 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21465 break;
21466 /* Otherwise, consume the `,' and keep going. */
21467 cp_lexer_consume_token (parser->lexer);
21468 /* If the next token is a `}', there is a trailing comma. */
21469 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21471 if (cxx_dialect < cxx11)
21472 pedwarn (input_location, OPT_Wpedantic,
21473 "comma at end of enumerator list");
21474 break;
21479 /* Parse an enumerator-definition. The enumerator has the indicated
21480 TYPE.
21482 enumerator-definition:
21483 enumerator
21484 enumerator = constant-expression
21486 enumerator:
21487 identifier
21489 GNU Extensions:
21491 enumerator-definition:
21492 enumerator attributes [opt]
21493 enumerator attributes [opt] = constant-expression */
21495 static void
21496 cp_parser_enumerator_definition (cp_parser* parser, tree type)
21498 tree identifier;
21499 tree value;
21500 location_t loc;
21502 /* Save the input location because we are interested in the location
21503 of the identifier and not the location of the explicit value. */
21504 loc = cp_lexer_peek_token (parser->lexer)->location;
21506 /* Look for the identifier. */
21507 identifier = cp_parser_identifier (parser);
21508 if (identifier == error_mark_node)
21509 return;
21511 /* Parse any specified attributes. */
21512 tree attrs = cp_parser_attributes_opt (parser);
21514 /* If the next token is an '=', then there is an explicit value. */
21515 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21517 /* Consume the `=' token. */
21518 cp_lexer_consume_token (parser->lexer);
21519 /* Parse the value. */
21520 value = cp_parser_constant_expression (parser);
21522 else
21523 value = NULL_TREE;
21525 /* If we are processing a template, make sure the initializer of the
21526 enumerator doesn't contain any bare template parameter pack. */
21527 if (current_lambda_expr ())
21529 /* In a lambda it should work, but doesn't currently. */
21530 if (uses_parameter_packs (value))
21532 sorry ("unexpanded parameter pack in enumerator in lambda");
21533 value = error_mark_node;
21536 else if (check_for_bare_parameter_packs (value))
21537 value = error_mark_node;
21539 /* Create the enumerator. */
21540 build_enumerator (identifier, value, type, attrs, loc);
21543 /* Parse a namespace-name.
21545 namespace-name:
21546 original-namespace-name
21547 namespace-alias
21549 Returns the NAMESPACE_DECL for the namespace. */
21551 static tree
21552 cp_parser_namespace_name (cp_parser* parser)
21554 tree identifier;
21555 tree namespace_decl;
21557 cp_token *token = cp_lexer_peek_token (parser->lexer);
21559 /* Get the name of the namespace. */
21560 identifier = cp_parser_identifier (parser);
21561 if (identifier == error_mark_node)
21562 return error_mark_node;
21564 /* Look up the identifier in the currently active scope. Look only
21565 for namespaces, due to:
21567 [basic.lookup.udir]
21569 When looking up a namespace-name in a using-directive or alias
21570 definition, only namespace names are considered.
21572 And:
21574 [basic.lookup.qual]
21576 During the lookup of a name preceding the :: scope resolution
21577 operator, object, function, and enumerator names are ignored.
21579 (Note that cp_parser_qualifying_entity only calls this
21580 function if the token after the name is the scope resolution
21581 operator.) */
21582 namespace_decl = cp_parser_lookup_name (parser, identifier,
21583 none_type,
21584 /*is_template=*/false,
21585 /*is_namespace=*/true,
21586 /*check_dependency=*/true,
21587 /*ambiguous_decls=*/NULL,
21588 token->location);
21589 /* If it's not a namespace, issue an error. */
21590 if (namespace_decl == error_mark_node
21591 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
21593 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21595 auto_diagnostic_group d;
21596 name_hint hint;
21597 if (namespace_decl == error_mark_node
21598 && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
21599 hint = suggest_alternative_in_explicit_scope (token->location,
21600 identifier,
21601 parser->scope);
21602 if (const char *suggestion = hint.suggestion ())
21604 gcc_rich_location richloc (token->location);
21605 richloc.add_fixit_replace (suggestion);
21606 error_at (&richloc,
21607 "%qD is not a namespace-name; did you mean %qs?",
21608 identifier, suggestion);
21610 else
21611 error_at (token->location, "%qD is not a namespace-name",
21612 identifier);
21614 else
21615 cp_parser_error (parser, "expected namespace-name");
21616 namespace_decl = error_mark_node;
21619 return namespace_decl;
21622 /* Parse a namespace-definition.
21624 namespace-definition:
21625 named-namespace-definition
21626 unnamed-namespace-definition
21628 named-namespace-definition:
21629 original-namespace-definition
21630 extension-namespace-definition
21632 original-namespace-definition:
21633 namespace identifier { namespace-body }
21635 extension-namespace-definition:
21636 namespace original-namespace-name { namespace-body }
21638 unnamed-namespace-definition:
21639 namespace { namespace-body } */
21641 static void
21642 cp_parser_namespace_definition (cp_parser* parser)
21644 tree identifier;
21645 int nested_definition_count = 0;
21647 cp_ensure_no_omp_declare_simd (parser);
21648 cp_ensure_no_oacc_routine (parser);
21650 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
21651 const bool topmost_inline_p = is_inline;
21653 if (is_inline)
21655 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
21656 cp_lexer_consume_token (parser->lexer);
21659 /* Look for the `namespace' keyword. */
21660 cp_token* token
21661 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
21663 /* Parse any specified attributes before the identifier. */
21664 tree attribs = cp_parser_attributes_opt (parser);
21666 for (;;)
21668 identifier = NULL_TREE;
21670 bool nested_inline_p = cp_lexer_next_token_is_keyword (parser->lexer,
21671 RID_INLINE);
21672 if (nested_inline_p && nested_definition_count != 0)
21674 if (pedantic && cxx_dialect < cxx20)
21675 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
21676 OPT_Wc__20_extensions, "nested inline namespace "
21677 "definitions only available with %<-std=c++20%> or "
21678 "%<-std=gnu++20%>");
21679 cp_lexer_consume_token (parser->lexer);
21682 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21684 identifier = cp_parser_identifier (parser);
21686 if (cp_next_tokens_can_be_std_attribute_p (parser))
21687 pedwarn (input_location, OPT_Wpedantic,
21688 "standard attributes on namespaces must precede "
21689 "the namespace name");
21691 /* Parse any attributes specified after the identifier. */
21692 attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
21695 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
21697 /* Don't forget that the innermost namespace might have been
21698 marked as inline. Use |= because we cannot overwrite
21699 IS_INLINE in case the outermost namespace is inline, but
21700 there are no nested inlines. */
21701 is_inline |= nested_inline_p;
21702 break;
21705 if (!nested_definition_count && pedantic && cxx_dialect < cxx17)
21706 pedwarn (input_location, OPT_Wc__17_extensions,
21707 "nested namespace definitions only available with "
21708 "%<-std=c++17%> or %<-std=gnu++17%>");
21710 /* Nested namespace names can create new namespaces (unlike
21711 other qualified-ids). */
21712 if (int count = (identifier
21713 ? push_namespace (identifier, nested_inline_p)
21714 : 0))
21715 nested_definition_count += count;
21716 else
21717 cp_parser_error (parser, "nested namespace name required");
21718 cp_lexer_consume_token (parser->lexer);
21721 if (nested_definition_count && !identifier)
21722 cp_parser_error (parser, "namespace name required");
21724 if (nested_definition_count && attribs)
21725 error_at (token->location,
21726 "a nested namespace definition cannot have attributes");
21727 if (nested_definition_count && topmost_inline_p)
21728 error_at (token->location,
21729 "a nested namespace definition cannot be inline");
21731 /* Start the namespace. */
21732 nested_definition_count += push_namespace (identifier, is_inline);
21734 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
21736 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
21738 /* Look for the `{' to validate starting the namespace. */
21739 matching_braces braces;
21740 if (braces.require_open (parser))
21742 /* Parse the body of the namespace. */
21743 cp_parser_namespace_body (parser);
21745 /* Look for the final `}'. */
21746 braces.require_close (parser);
21749 if (has_visibility)
21750 pop_visibility (1);
21752 /* Pop the nested namespace definitions. */
21753 while (nested_definition_count--)
21754 pop_namespace ();
21757 /* Parse a namespace-body.
21759 namespace-body:
21760 declaration-seq [opt] */
21762 static void
21763 cp_parser_namespace_body (cp_parser* parser)
21765 cp_parser_declaration_seq_opt (parser);
21768 /* Parse a namespace-alias-definition.
21770 namespace-alias-definition:
21771 namespace identifier = qualified-namespace-specifier ; */
21773 static void
21774 cp_parser_namespace_alias_definition (cp_parser* parser)
21776 tree identifier;
21777 tree namespace_specifier;
21779 cp_token *token = cp_lexer_peek_token (parser->lexer);
21781 /* Look for the `namespace' keyword. */
21782 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
21783 /* Look for the identifier. */
21784 identifier = cp_parser_identifier (parser);
21785 if (identifier == error_mark_node)
21786 return;
21787 /* Look for the `=' token. */
21788 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
21789 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21791 error_at (token->location, "%<namespace%> definition is not allowed here");
21792 /* Skip the definition. */
21793 cp_lexer_consume_token (parser->lexer);
21794 if (cp_parser_skip_to_closing_brace (parser))
21795 cp_lexer_consume_token (parser->lexer);
21796 return;
21798 cp_parser_require (parser, CPP_EQ, RT_EQ);
21799 /* Look for the qualified-namespace-specifier. */
21800 namespace_specifier
21801 = cp_parser_qualified_namespace_specifier (parser);
21802 cp_warn_deprecated_use_scopes (namespace_specifier);
21803 /* Look for the `;' token. */
21804 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21806 /* Register the alias in the symbol table. */
21807 do_namespace_alias (identifier, namespace_specifier);
21810 /* Parse a qualified-namespace-specifier.
21812 qualified-namespace-specifier:
21813 :: [opt] nested-name-specifier [opt] namespace-name
21815 Returns a NAMESPACE_DECL corresponding to the specified
21816 namespace. */
21818 static tree
21819 cp_parser_qualified_namespace_specifier (cp_parser* parser)
21821 /* Look for the optional `::'. */
21822 cp_parser_global_scope_opt (parser,
21823 /*current_scope_valid_p=*/false);
21825 /* Look for the optional nested-name-specifier. */
21826 cp_parser_nested_name_specifier_opt (parser,
21827 /*typename_keyword_p=*/false,
21828 /*check_dependency_p=*/true,
21829 /*type_p=*/false,
21830 /*is_declaration=*/true);
21832 return cp_parser_namespace_name (parser);
21835 /* Subroutine of cp_parser_using_declaration. */
21837 static tree
21838 finish_using_decl (tree qscope, tree identifier, bool typename_p = false)
21840 tree decl = NULL_TREE;
21841 if (at_class_scope_p ())
21843 /* Create the USING_DECL. */
21844 decl = do_class_using_decl (qscope, identifier);
21846 if (check_for_bare_parameter_packs (decl))
21847 return error_mark_node;
21849 if (decl && typename_p)
21850 USING_DECL_TYPENAME_P (decl) = 1;
21852 /* Add it to the list of members in this class. */
21853 finish_member_declaration (decl);
21855 else
21856 finish_nonmember_using_decl (qscope, identifier);
21857 return decl;
21860 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
21861 access declaration.
21863 using-declaration:
21864 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
21865 using :: unqualified-id ;
21867 access-declaration:
21868 qualified-id ;
21872 static bool
21873 cp_parser_using_declaration (cp_parser* parser,
21874 bool access_declaration_p)
21876 cp_token *token;
21877 bool typename_p = false;
21878 bool global_scope_p;
21879 tree identifier;
21880 tree qscope;
21881 int oldcount = errorcount;
21882 cp_token *diag_token = NULL;
21884 if (access_declaration_p)
21886 diag_token = cp_lexer_peek_token (parser->lexer);
21887 cp_parser_parse_tentatively (parser);
21889 else
21891 /* Look for the `using' keyword. */
21892 cp_parser_require_keyword (parser, RID_USING, RT_USING);
21894 again:
21895 /* Peek at the next token. */
21896 token = cp_lexer_peek_token (parser->lexer);
21897 /* See if it's `typename'. */
21898 if (token->keyword == RID_TYPENAME)
21900 /* Remember that we've seen it. */
21901 typename_p = true;
21902 /* Consume the `typename' token. */
21903 cp_lexer_consume_token (parser->lexer);
21907 /* Look for the optional global scope qualification. */
21908 global_scope_p
21909 = (cp_parser_global_scope_opt (parser,
21910 /*current_scope_valid_p=*/false)
21911 != NULL_TREE);
21913 /* If we saw `typename', or didn't see `::', then there must be a
21914 nested-name-specifier present. */
21915 if (typename_p || !global_scope_p)
21917 qscope = cp_parser_nested_name_specifier (parser, typename_p,
21918 /*check_dependency_p=*/true,
21919 /*type_p=*/false,
21920 /*is_declaration=*/true);
21921 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
21923 cp_parser_skip_to_end_of_block_or_statement (parser);
21924 return false;
21927 /* Otherwise, we could be in either of the two productions. In that
21928 case, treat the nested-name-specifier as optional. */
21929 else
21930 qscope = cp_parser_nested_name_specifier_opt (parser,
21931 /*typename_keyword_p=*/false,
21932 /*check_dependency_p=*/true,
21933 /*type_p=*/false,
21934 /*is_declaration=*/true);
21935 if (!qscope)
21936 qscope = global_namespace;
21938 cp_warn_deprecated_use_scopes (qscope);
21940 if (access_declaration_p
21941 && !MAYBE_CLASS_TYPE_P (qscope)
21942 && TREE_CODE (qscope) != ENUMERAL_TYPE)
21943 /* If the qualifying scope of an access-declaration isn't a class
21944 or enumeration type then it can't be valid. */
21945 cp_parser_simulate_error (parser);
21947 if (access_declaration_p && cp_parser_error_occurred (parser))
21948 /* Something has already gone wrong; there's no need to parse
21949 further. Since an error has occurred, the return value of
21950 cp_parser_parse_definitely will be false, as required. */
21951 return cp_parser_parse_definitely (parser);
21953 token = cp_lexer_peek_token (parser->lexer);
21954 /* Parse the unqualified-id. */
21955 identifier = cp_parser_unqualified_id (parser,
21956 /*template_keyword_p=*/false,
21957 /*check_dependency_p=*/true,
21958 /*declarator_p=*/true,
21959 /*optional_p=*/false);
21961 if (access_declaration_p)
21963 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21964 cp_parser_simulate_error (parser);
21965 if (!cp_parser_parse_definitely (parser))
21966 return false;
21968 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21970 cp_token *ell = cp_lexer_consume_token (parser->lexer);
21971 if (cxx_dialect < cxx17)
21972 pedwarn (ell->location, OPT_Wc__17_extensions,
21973 "pack expansion in using-declaration only available "
21974 "with %<-std=c++17%> or %<-std=gnu++17%>");
21976 /* A parameter pack can appear in the qualifying scope, and/or in the
21977 terminal name (if naming a conversion function). Logically they're
21978 part of a single pack expansion of the overall USING_DECL, but we
21979 express them as separate pack expansions within the USING_DECL since
21980 we can't create a pack expansion over a USING_DECL. */
21981 bool saw_parm_pack = false;
21982 if (uses_parameter_packs (qscope))
21984 qscope = make_pack_expansion (qscope);
21985 saw_parm_pack = true;
21987 if (identifier_p (identifier)
21988 && IDENTIFIER_CONV_OP_P (identifier)
21989 && uses_parameter_packs (TREE_TYPE (identifier)))
21991 identifier = make_conv_op_name (make_pack_expansion
21992 (TREE_TYPE (identifier)));
21993 saw_parm_pack = true;
21995 if (!saw_parm_pack)
21997 /* Issue an error in terms using a SCOPE_REF that includes both
21998 components. */
21999 tree name
22000 = build_qualified_name (NULL_TREE, qscope, identifier, false);
22001 make_pack_expansion (name);
22002 gcc_assert (seen_error ());
22003 qscope = identifier = error_mark_node;
22007 /* The function we call to handle a using-declaration is different
22008 depending on what scope we are in. */
22009 if (qscope == error_mark_node || identifier == error_mark_node)
22011 else if (!identifier_p (identifier)
22012 && TREE_CODE (identifier) != BIT_NOT_EXPR)
22013 /* [namespace.udecl]
22015 A using declaration shall not name a template-id. */
22016 error_at (token->location,
22017 "a template-id may not appear in a using-declaration");
22018 else
22020 tree decl = finish_using_decl (qscope, identifier, typename_p);
22022 if (decl == error_mark_node)
22024 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22025 return false;
22029 if (!access_declaration_p
22030 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22032 cp_token *comma = cp_lexer_consume_token (parser->lexer);
22033 if (cxx_dialect < cxx17)
22034 pedwarn (comma->location, OPT_Wc__17_extensions,
22035 "comma-separated list in using-declaration only available "
22036 "with %<-std=c++17%> or %<-std=gnu++17%>");
22037 goto again;
22040 /* Look for the final `;'. */
22041 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22043 if (access_declaration_p && errorcount == oldcount)
22044 warning_at (diag_token->location, OPT_Wdeprecated,
22045 "access declarations are deprecated "
22046 "in favour of using-declarations; "
22047 "suggestion: add the %<using%> keyword");
22049 return true;
22052 /* C++20 using enum declaration.
22054 using-enum-declaration :
22055 using elaborated-enum-specifier ; */
22057 static void
22058 cp_parser_using_enum (cp_parser *parser)
22060 cp_parser_require_keyword (parser, RID_USING, RT_USING);
22062 /* Using cp_parser_elaborated_type_specifier rejects typedef-names, which
22063 breaks one of the motivating examples in using-enum-5.C.
22064 cp_parser_simple_type_specifier seems to be closer to what we actually
22065 want, though that hasn't been properly specified yet. */
22067 /* Consume 'enum'. */
22068 gcc_checking_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM));
22069 cp_lexer_consume_token (parser->lexer);
22071 cp_token *start = cp_lexer_peek_token (parser->lexer);
22073 tree type = (cp_parser_simple_type_specifier
22074 (parser, NULL, CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
22076 cp_token *end = cp_lexer_previous_token (parser->lexer);
22078 if (type == error_mark_node
22079 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
22081 cp_parser_skip_to_end_of_block_or_statement (parser);
22082 return;
22084 if (TREE_CODE (type) == TYPE_DECL)
22085 type = TREE_TYPE (type);
22087 /* The elaborated-enum-specifier shall not name a dependent type and the type
22088 shall have a reachable enum-specifier. */
22089 const char *msg = nullptr;
22090 if (cxx_dialect < cxx20)
22091 msg = _("%<using enum%> "
22092 "only available with %<-std=c++20%> or %<-std=gnu++20%>");
22093 else if (dependent_type_p (type))
22094 msg = _("%<using enum%> of dependent type %qT");
22095 else if (TREE_CODE (type) != ENUMERAL_TYPE)
22096 msg = _("%<using enum%> of non-enumeration type %q#T");
22097 else if (!COMPLETE_TYPE_P (type))
22098 msg = _("%<using enum%> of incomplete type %qT");
22099 else if (OPAQUE_ENUM_P (type))
22100 msg = _("%<using enum%> of %qT before its enum-specifier");
22101 if (msg)
22103 location_t loc = make_location (start, start, end);
22104 auto_diagnostic_group g;
22105 error_at (loc, msg, type);
22106 loc = location_of (type);
22107 if (cxx_dialect < cxx20 || loc == input_location)
22109 else if (OPAQUE_ENUM_P (type))
22110 inform (loc, "opaque-enum-declaration here");
22111 else
22112 inform (loc, "declared here");
22115 /* A using-enum-declaration introduces the enumerator names of the named
22116 enumeration as if by a using-declaration for each enumerator. */
22117 if (TREE_CODE (type) == ENUMERAL_TYPE)
22118 for (tree v = TYPE_VALUES (type); v; v = TREE_CHAIN (v))
22119 finish_using_decl (type, DECL_NAME (TREE_VALUE (v)));
22122 /* Parse an alias-declaration.
22124 alias-declaration:
22125 using identifier attribute-specifier-seq [opt] = type-id */
22127 static tree
22128 cp_parser_alias_declaration (cp_parser* parser)
22130 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
22131 location_t id_location, type_location;
22132 cp_declarator *declarator;
22133 cp_decl_specifier_seq decl_specs;
22134 bool member_p;
22135 const char *saved_message = NULL;
22137 /* Look for the `using' keyword. */
22138 cp_token *using_token
22139 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
22140 if (using_token == NULL)
22141 return error_mark_node;
22143 id_location = cp_lexer_peek_token (parser->lexer)->location;
22144 id = cp_parser_identifier (parser);
22145 if (id == error_mark_node)
22146 return error_mark_node;
22148 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
22149 attributes = cp_parser_attributes_opt (parser);
22150 if (attributes == error_mark_node)
22151 return error_mark_node;
22153 cp_parser_require (parser, CPP_EQ, RT_EQ);
22155 if (cp_parser_error_occurred (parser))
22156 return error_mark_node;
22158 cp_parser_commit_to_tentative_parse (parser);
22160 /* Now we are going to parse the type-id of the declaration. */
22163 [dcl.type]/3 says:
22165 "A type-specifier-seq shall not define a class or enumeration
22166 unless it appears in the type-id of an alias-declaration (7.1.3) that
22167 is not the declaration of a template-declaration."
22169 In other words, if we currently are in an alias template, the
22170 type-id should not define a type.
22172 So let's set parser->type_definition_forbidden_message in that
22173 case; cp_parser_check_type_definition (called by
22174 cp_parser_class_specifier) will then emit an error if a type is
22175 defined in the type-id. */
22176 if (parser->num_template_parameter_lists)
22178 saved_message = parser->type_definition_forbidden_message;
22179 parser->type_definition_forbidden_message =
22180 G_("types may not be defined in alias template declarations");
22183 type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
22184 &type_location);
22186 /* Restore the error message if need be. */
22187 if (parser->num_template_parameter_lists)
22188 parser->type_definition_forbidden_message = saved_message;
22190 if (type == error_mark_node
22191 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
22193 cp_parser_skip_to_end_of_block_or_statement (parser);
22194 return error_mark_node;
22197 /* A typedef-name can also be introduced by an alias-declaration. The
22198 identifier following the using keyword becomes a typedef-name. It has
22199 the same semantics as if it were introduced by the typedef
22200 specifier. In particular, it does not define a new type and it shall
22201 not appear in the type-id. */
22203 clear_decl_specs (&decl_specs);
22204 decl_specs.type = type;
22205 if (attributes != NULL_TREE)
22207 decl_specs.attributes = attributes;
22208 set_and_check_decl_spec_loc (&decl_specs,
22209 ds_attribute,
22210 attrs_token);
22212 set_and_check_decl_spec_loc (&decl_specs,
22213 ds_typedef,
22214 using_token);
22215 set_and_check_decl_spec_loc (&decl_specs,
22216 ds_alias,
22217 using_token);
22218 decl_specs.locations[ds_type_spec] = type_location;
22220 if (parser->num_template_parameter_lists
22221 && !cp_parser_check_template_parameters (parser,
22222 /*num_templates=*/0,
22223 /*template_id*/false,
22224 id_location,
22225 /*declarator=*/NULL))
22226 return error_mark_node;
22228 declarator = make_id_declarator (NULL_TREE, id, sfk_none, id_location);
22230 member_p = at_class_scope_p ();
22231 if (member_p)
22232 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
22233 NULL_TREE, attributes);
22234 else
22235 decl = start_decl (declarator, &decl_specs, 0,
22236 attributes, NULL_TREE, &pushed_scope);
22237 if (decl == error_mark_node)
22238 return decl;
22240 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
22242 if (pushed_scope)
22243 pop_scope (pushed_scope);
22245 /* If decl is a template, return its TEMPLATE_DECL so that it gets
22246 added into the symbol table; otherwise, return the TYPE_DECL. */
22247 if (DECL_LANG_SPECIFIC (decl)
22248 && DECL_TEMPLATE_INFO (decl)
22249 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
22251 decl = DECL_TI_TEMPLATE (decl);
22252 if (member_p)
22253 check_member_template (decl);
22256 return decl;
22259 /* Parse a using-directive.
22261 using-directive:
22262 attribute-specifier-seq [opt] using namespace :: [opt]
22263 nested-name-specifier [opt] namespace-name ; */
22265 static void
22266 cp_parser_using_directive (cp_parser* parser)
22268 tree namespace_decl;
22269 tree attribs = cp_parser_std_attribute_spec_seq (parser);
22270 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22272 /* Error during attribute parsing that resulted in skipping
22273 to next semicolon. */
22274 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22275 return;
22278 /* Look for the `using' keyword. */
22279 cp_parser_require_keyword (parser, RID_USING, RT_USING);
22280 /* And the `namespace' keyword. */
22281 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
22282 /* Look for the optional `::' operator. */
22283 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
22284 /* And the optional nested-name-specifier. */
22285 cp_parser_nested_name_specifier_opt (parser,
22286 /*typename_keyword_p=*/false,
22287 /*check_dependency_p=*/true,
22288 /*type_p=*/false,
22289 /*is_declaration=*/true);
22290 /* Get the namespace being used. */
22291 namespace_decl = cp_parser_namespace_name (parser);
22292 cp_warn_deprecated_use_scopes (namespace_decl);
22293 /* And any specified GNU attributes. */
22294 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
22295 attribs = chainon (attribs, cp_parser_gnu_attributes_opt (parser));
22297 /* Update the symbol table. */
22298 finish_using_directive (namespace_decl, attribs);
22300 /* Look for the final `;'. */
22301 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22304 /* Parse an asm-definition.
22306 asm-qualifier:
22307 volatile
22308 inline
22309 goto
22311 asm-qualifier-list:
22312 asm-qualifier
22313 asm-qualifier-list asm-qualifier
22315 asm-definition:
22316 asm ( string-literal ) ;
22318 GNU Extension:
22320 asm-definition:
22321 asm asm-qualifier-list [opt] ( string-literal ) ;
22322 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt] ) ;
22323 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
22324 : asm-operand-list [opt] ) ;
22325 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
22326 : asm-operand-list [opt]
22327 : asm-clobber-list [opt] ) ;
22328 asm asm-qualifier-list [opt] ( string-literal : : asm-operand-list [opt]
22329 : asm-clobber-list [opt]
22330 : asm-goto-list ) ;
22332 The form with asm-goto-list is valid if and only if the asm-qualifier-list
22333 contains goto, and is the only allowed form in that case. No duplicates are
22334 allowed in an asm-qualifier-list. */
22336 static void
22337 cp_parser_asm_definition (cp_parser* parser)
22339 tree outputs = NULL_TREE;
22340 tree inputs = NULL_TREE;
22341 tree clobbers = NULL_TREE;
22342 tree labels = NULL_TREE;
22343 tree asm_stmt;
22344 bool extended_p = false;
22345 bool invalid_inputs_p = false;
22346 bool invalid_outputs_p = false;
22347 required_token missing = RT_NONE;
22348 location_t asm_loc = cp_lexer_peek_token (parser->lexer)->location;
22350 /* Look for the `asm' keyword. */
22351 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
22353 /* In C++20, unevaluated inline assembly is permitted in constexpr
22354 functions. */
22355 if (parser->in_function_body
22356 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
22357 && cxx_dialect < cxx20)
22358 pedwarn (asm_loc, OPT_Wc__20_extensions, "%<asm%> in %<constexpr%> "
22359 "function only available with %<-std=c++20%> or "
22360 "%<-std=gnu++20%>");
22362 /* Handle the asm-qualifier-list. */
22363 location_t volatile_loc = UNKNOWN_LOCATION;
22364 location_t inline_loc = UNKNOWN_LOCATION;
22365 location_t goto_loc = UNKNOWN_LOCATION;
22366 location_t first_loc = UNKNOWN_LOCATION;
22368 if (cp_parser_allow_gnu_extensions_p (parser))
22369 for (;;)
22371 cp_token *token = cp_lexer_peek_token (parser->lexer);
22372 location_t loc = token->location;
22373 switch (cp_lexer_peek_token (parser->lexer)->keyword)
22375 case RID_VOLATILE:
22376 if (volatile_loc)
22378 error_at (loc, "duplicate %<asm%> qualifier %qT",
22379 token->u.value);
22380 inform (volatile_loc, "first seen here");
22382 else
22384 if (!parser->in_function_body)
22385 warning_at (loc, 0, "%<asm%> qualifier %qT ignored "
22386 "outside of function body", token->u.value);
22387 volatile_loc = loc;
22389 cp_lexer_consume_token (parser->lexer);
22390 continue;
22392 case RID_INLINE:
22393 if (inline_loc)
22395 error_at (loc, "duplicate %<asm%> qualifier %qT",
22396 token->u.value);
22397 inform (inline_loc, "first seen here");
22399 else
22400 inline_loc = loc;
22401 if (!first_loc)
22402 first_loc = loc;
22403 cp_lexer_consume_token (parser->lexer);
22404 continue;
22406 case RID_GOTO:
22407 if (goto_loc)
22409 error_at (loc, "duplicate %<asm%> qualifier %qT",
22410 token->u.value);
22411 inform (goto_loc, "first seen here");
22413 else
22414 goto_loc = loc;
22415 if (!first_loc)
22416 first_loc = loc;
22417 cp_lexer_consume_token (parser->lexer);
22418 continue;
22420 case RID_CONST:
22421 case RID_RESTRICT:
22422 error_at (loc, "%qT is not an %<asm%> qualifier", token->u.value);
22423 cp_lexer_consume_token (parser->lexer);
22424 continue;
22426 default:
22427 break;
22429 break;
22432 bool volatile_p = (volatile_loc != UNKNOWN_LOCATION);
22433 bool inline_p = (inline_loc != UNKNOWN_LOCATION);
22434 bool goto_p = (goto_loc != UNKNOWN_LOCATION);
22436 if (!parser->in_function_body && (inline_p || goto_p))
22438 error_at (first_loc, "%<asm%> qualifier outside of function body");
22439 inline_p = goto_p = false;
22442 /* Look for the opening `('. */
22443 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22444 return;
22445 /* Look for the string. */
22446 tree string = cp_parser_string_literal (parser, /*translate=*/false,
22447 /*wide_ok=*/false);
22448 if (string == error_mark_node)
22450 cp_parser_skip_to_closing_parenthesis (parser, true, false,
22451 /*consume_paren=*/true);
22452 return;
22455 /* If we're allowing GNU extensions, check for the extended assembly
22456 syntax. Unfortunately, the `:' tokens need not be separated by
22457 a space in C, and so, for compatibility, we tolerate that here
22458 too. Doing that means that we have to treat the `::' operator as
22459 two `:' tokens. */
22460 if (cp_parser_allow_gnu_extensions_p (parser)
22461 && parser->in_function_body
22462 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
22463 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
22465 bool inputs_p = false;
22466 bool clobbers_p = false;
22467 bool labels_p = false;
22469 /* The extended syntax was used. */
22470 extended_p = true;
22472 /* Look for outputs. */
22473 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22475 /* Consume the `:'. */
22476 cp_lexer_consume_token (parser->lexer);
22477 /* Parse the output-operands. */
22478 if (cp_lexer_next_token_is_not (parser->lexer,
22479 CPP_COLON)
22480 && cp_lexer_next_token_is_not (parser->lexer,
22481 CPP_SCOPE)
22482 && cp_lexer_next_token_is_not (parser->lexer,
22483 CPP_CLOSE_PAREN))
22485 outputs = cp_parser_asm_operand_list (parser);
22486 if (outputs == error_mark_node)
22487 invalid_outputs_p = true;
22490 /* If the next token is `::', there are no outputs, and the
22491 next token is the beginning of the inputs. */
22492 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22493 /* The inputs are coming next. */
22494 inputs_p = true;
22496 /* Look for inputs. */
22497 if (inputs_p
22498 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22500 /* Consume the `:' or `::'. */
22501 cp_lexer_consume_token (parser->lexer);
22502 /* Parse the output-operands. */
22503 if (cp_lexer_next_token_is_not (parser->lexer,
22504 CPP_COLON)
22505 && cp_lexer_next_token_is_not (parser->lexer,
22506 CPP_SCOPE)
22507 && cp_lexer_next_token_is_not (parser->lexer,
22508 CPP_CLOSE_PAREN))
22510 inputs = cp_parser_asm_operand_list (parser);
22511 if (inputs == error_mark_node)
22512 invalid_inputs_p = true;
22515 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22516 /* The clobbers are coming next. */
22517 clobbers_p = true;
22519 /* Look for clobbers. */
22520 if (clobbers_p
22521 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22523 clobbers_p = true;
22524 /* Consume the `:' or `::'. */
22525 cp_lexer_consume_token (parser->lexer);
22526 /* Parse the clobbers. */
22527 if (cp_lexer_next_token_is_not (parser->lexer,
22528 CPP_COLON)
22529 && cp_lexer_next_token_is_not (parser->lexer,
22530 CPP_CLOSE_PAREN))
22531 clobbers = cp_parser_asm_clobber_list (parser);
22533 else if (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22534 /* The labels are coming next. */
22535 labels_p = true;
22537 /* Look for labels. */
22538 if (labels_p
22539 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
22541 labels_p = true;
22542 /* Consume the `:' or `::'. */
22543 cp_lexer_consume_token (parser->lexer);
22544 /* Parse the labels. */
22545 labels = cp_parser_asm_label_list (parser);
22548 if (goto_p && !labels_p)
22549 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
22551 else if (goto_p)
22552 missing = RT_COLON_SCOPE;
22554 /* Look for the closing `)'. */
22555 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
22556 missing ? missing : RT_CLOSE_PAREN))
22557 cp_parser_skip_to_closing_parenthesis (parser, true, false,
22558 /*consume_paren=*/true);
22559 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22561 if (!invalid_inputs_p && !invalid_outputs_p)
22563 /* Create the ASM_EXPR. */
22564 if (parser->in_function_body)
22566 asm_stmt = finish_asm_stmt (asm_loc, volatile_p, string, outputs,
22567 inputs, clobbers, labels, inline_p);
22568 /* If the extended syntax was not used, mark the ASM_EXPR. */
22569 if (!extended_p)
22571 tree temp = asm_stmt;
22572 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
22573 temp = TREE_OPERAND (temp, 0);
22575 ASM_INPUT_P (temp) = 1;
22578 else
22579 symtab->finalize_toplevel_asm (string);
22583 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
22584 type that comes from the decl-specifier-seq. */
22586 static tree
22587 strip_declarator_types (tree type, cp_declarator *declarator)
22589 for (cp_declarator *d = declarator; d;)
22590 switch (d->kind)
22592 case cdk_id:
22593 case cdk_decomp:
22594 case cdk_error:
22595 d = NULL;
22596 break;
22598 default:
22599 if (TYPE_PTRMEMFUNC_P (type))
22600 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
22601 type = TREE_TYPE (type);
22602 d = d->declarator;
22603 break;
22606 return type;
22609 /* Warn about the most vexing parse syntactic ambiguity, i.e., warn when
22610 a construct looks like a variable definition but is actually a function
22611 declaration. DECL_SPECIFIERS is the decl-specifier-seq and DECLARATOR
22612 is the declarator for this function declaration. */
22614 static void
22615 warn_about_ambiguous_parse (const cp_decl_specifier_seq *decl_specifiers,
22616 const cp_declarator *declarator)
22618 /* Only warn if we are declaring a function at block scope. */
22619 if (!at_function_scope_p ())
22620 return;
22622 /* And only if there is no storage class specified. */
22623 if (decl_specifiers->storage_class != sc_none
22624 || decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
22625 return;
22627 if (declarator->kind != cdk_function
22628 || !declarator->declarator
22629 || declarator->declarator->kind != cdk_id
22630 || !identifier_p (get_unqualified_id
22631 (const_cast<cp_declarator *>(declarator))))
22632 return;
22634 /* Don't warn when the whole declarator (not just the declarator-id!)
22635 was parenthesized. That is, don't warn for int(n()) but do warn
22636 for int(f)(). */
22637 if (declarator->parenthesized != UNKNOWN_LOCATION)
22638 return;
22640 tree type;
22641 if (decl_specifiers->type)
22643 type = decl_specifiers->type;
22644 if (TREE_CODE (type) == TYPE_DECL)
22645 type = TREE_TYPE (type);
22647 /* If the return type is void there is no ambiguity. */
22648 if (same_type_p (type, void_type_node))
22649 return;
22651 else if (decl_specifiers->any_type_specifiers_p)
22652 /* Code like long f(); will have null ->type. If we have any
22653 type-specifiers, pretend we've seen int. */
22654 type = integer_type_node;
22655 else
22656 return;
22658 auto_diagnostic_group d;
22659 location_t loc = declarator->u.function.parens_loc;
22660 tree params = declarator->u.function.parameters;
22661 const bool has_list_ctor_p = CLASS_TYPE_P (type) && TYPE_HAS_LIST_CTOR (type);
22663 /* The T t() case. */
22664 if (params == void_list_node)
22666 if (warning_at (loc, OPT_Wvexing_parse,
22667 "empty parentheses were disambiguated as a function "
22668 "declaration"))
22670 /* () means value-initialization (C++03 and up); {} (C++11 and up)
22671 means value-initialization or aggregate-initialization, nothing
22672 means default-initialization. We can only suggest removing the
22673 parentheses/adding {} if T has a default constructor. */
22674 if (!CLASS_TYPE_P (type) || TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
22676 gcc_rich_location iloc (loc);
22677 iloc.add_fixit_remove ();
22678 inform (&iloc, "remove parentheses to default-initialize "
22679 "a variable");
22680 if (cxx_dialect >= cxx11 && !has_list_ctor_p)
22682 if (CP_AGGREGATE_TYPE_P (type))
22683 inform (loc, "or replace parentheses with braces to "
22684 "aggregate-initialize a variable");
22685 else
22686 inform (loc, "or replace parentheses with braces to "
22687 "value-initialize a variable");
22691 return;
22694 /* If we had (...) or the parameter-list wasn't parenthesized,
22695 we're done. */
22696 if (params == NULL_TREE || !PARENTHESIZED_LIST_P (params))
22697 return;
22699 /* The T t(X()) case. */
22700 if (list_length (params) == 2)
22702 if (warning_at (loc, OPT_Wvexing_parse,
22703 "parentheses were disambiguated as a function "
22704 "declaration"))
22706 gcc_rich_location iloc (loc);
22707 /* {}-initialization means that we can use an initializer-list
22708 constructor if no default constructor is available, so don't
22709 suggest using {} for classes that have an initializer_list
22710 constructor. */
22711 if (cxx_dialect >= cxx11 && !has_list_ctor_p)
22713 iloc.add_fixit_replace (get_start (loc), "{");
22714 iloc.add_fixit_replace (get_finish (loc), "}");
22715 inform (&iloc, "replace parentheses with braces to declare a "
22716 "variable");
22718 else
22720 iloc.add_fixit_insert_after (get_start (loc), "(");
22721 iloc.add_fixit_insert_before (get_finish (loc), ")");
22722 inform (&iloc, "add parentheses to declare a variable");
22726 /* The T t(X(), X()) case. */
22727 else if (warning_at (loc, OPT_Wvexing_parse,
22728 "parentheses were disambiguated as a function "
22729 "declaration"))
22731 gcc_rich_location iloc (loc);
22732 if (cxx_dialect >= cxx11 && !has_list_ctor_p)
22734 iloc.add_fixit_replace (get_start (loc), "{");
22735 iloc.add_fixit_replace (get_finish (loc), "}");
22736 inform (&iloc, "replace parentheses with braces to declare a "
22737 "variable");
22742 /* If DECLARATOR with DECL_SPECS is a function declarator that has
22743 the form of a deduction guide, tag it as such. CTOR_DTOR_OR_CONV_P
22744 has the same meaning as in cp_parser_declarator. */
22746 static void
22747 cp_parser_maybe_adjust_declarator_for_dguide (cp_parser *parser,
22748 cp_decl_specifier_seq *decl_specs,
22749 cp_declarator *declarator,
22750 int *ctor_dtor_or_conv_p)
22752 if (cxx_dialect >= cxx17
22753 && *ctor_dtor_or_conv_p <= 0
22754 && !decl_specs->type
22755 && !decl_specs->any_type_specifiers_p
22756 && function_declarator_p (declarator))
22758 cp_declarator *id = get_id_declarator (declarator);
22759 tree name = id->u.id.unqualified_name;
22760 parser->scope = id->u.id.qualifying_scope;
22761 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
22762 if (tmpl
22763 && (DECL_CLASS_TEMPLATE_P (tmpl)
22764 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
22766 id->u.id.unqualified_name = dguide_name (tmpl);
22767 id->u.id.sfk = sfk_deduction_guide;
22768 *ctor_dtor_or_conv_p = 1;
22773 /* Declarators [gram.dcl.decl] */
22775 /* Parse an init-declarator.
22777 init-declarator:
22778 declarator initializer [opt]
22780 GNU Extension:
22782 init-declarator:
22783 declarator asm-specification [opt] attributes [opt] initializer [opt]
22785 function-definition:
22786 decl-specifier-seq [opt] declarator ctor-initializer [opt]
22787 function-body
22788 decl-specifier-seq [opt] declarator function-try-block
22790 GNU Extension:
22792 function-definition:
22793 __extension__ function-definition
22795 TM Extension:
22797 function-definition:
22798 decl-specifier-seq [opt] declarator function-transaction-block
22800 The parser flags FLAGS is used to control type-specifier parsing.
22802 The DECL_SPECIFIERS apply to this declarator. Returns a
22803 representation of the entity declared. If MEMBER_P is TRUE, then
22804 this declarator appears in a class scope. The new DECL created by
22805 this declarator is returned.
22807 The CHECKS are access checks that should be performed once we know
22808 what entity is being declared (and, therefore, what classes have
22809 befriended it).
22811 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
22812 for a function-definition here as well. If the declarator is a
22813 declarator for a function-definition, *FUNCTION_DEFINITION_P will
22814 be TRUE upon return. By that point, the function-definition will
22815 have been completely parsed.
22817 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
22818 is FALSE.
22820 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
22821 parsed declaration if it is an uninitialized single declarator not followed
22822 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
22823 if present, will not be consumed. If returned, this declarator will be
22824 created with SD_INITIALIZED but will not call cp_finish_decl.
22826 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
22827 and there is an initializer, the pointed location_t is set to the
22828 location of the '=' or `(', or '{' in C++11 token introducing the
22829 initializer. */
22831 static tree
22832 cp_parser_init_declarator (cp_parser* parser,
22833 cp_parser_flags flags,
22834 cp_decl_specifier_seq *decl_specifiers,
22835 vec<deferred_access_check, va_gc> *checks,
22836 bool function_definition_allowed_p,
22837 bool member_p,
22838 int declares_class_or_enum,
22839 bool* function_definition_p,
22840 tree* maybe_range_for_decl,
22841 location_t* init_loc,
22842 tree* auto_result)
22844 cp_token *token = NULL, *asm_spec_start_token = NULL,
22845 *attributes_start_token = NULL;
22846 cp_declarator *declarator;
22847 tree prefix_attributes;
22848 tree attributes = NULL;
22849 tree asm_specification;
22850 tree initializer;
22851 tree decl = NULL_TREE;
22852 tree scope;
22853 int is_initialized;
22854 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
22855 initialized with "= ..", CPP_OPEN_PAREN if initialized with
22856 "(...)". */
22857 enum cpp_ttype initialization_kind;
22858 bool is_direct_init = false;
22859 bool is_non_constant_init;
22860 int ctor_dtor_or_conv_p;
22861 bool friend_p = cp_parser_friend_p (decl_specifiers);
22862 bool static_p = decl_specifiers->storage_class == sc_static;
22863 tree pushed_scope = NULL_TREE;
22864 bool range_for_decl_p = false;
22865 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
22866 location_t tmp_init_loc = UNKNOWN_LOCATION;
22868 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_consteval))
22869 flags |= CP_PARSER_FLAGS_CONSTEVAL;
22871 /* Assume that this is not the declarator for a function
22872 definition. */
22873 if (function_definition_p)
22874 *function_definition_p = false;
22876 /* Default arguments are only permitted for function parameters. */
22877 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
22878 parser->default_arg_ok_p = false;
22880 /* Defer access checks while parsing the declarator; we cannot know
22881 what names are accessible until we know what is being
22882 declared. */
22883 resume_deferring_access_checks ();
22885 token = cp_lexer_peek_token (parser->lexer);
22887 /* Parse the declarator. */
22888 declarator
22889 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22890 flags, &ctor_dtor_or_conv_p,
22891 /*parenthesized_p=*/NULL,
22892 member_p, friend_p, static_p);
22893 /* Gather up the deferred checks. */
22894 stop_deferring_access_checks ();
22896 parser->default_arg_ok_p = saved_default_arg_ok_p;
22898 /* If the DECLARATOR was erroneous, there's no need to go
22899 further. */
22900 if (declarator == cp_error_declarator)
22901 return error_mark_node;
22903 /* Check that the number of template-parameter-lists is OK. */
22904 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
22905 token->location))
22906 return error_mark_node;
22908 if (declares_class_or_enum & 2)
22909 cp_parser_check_for_definition_in_return_type (declarator,
22910 decl_specifiers->type,
22911 decl_specifiers->locations[ds_type_spec]);
22913 /* Figure out what scope the entity declared by the DECLARATOR is
22914 located in. `grokdeclarator' sometimes changes the scope, so
22915 we compute it now. */
22916 scope = get_scope_of_declarator (declarator);
22918 /* Perform any lookups in the declared type which were thought to be
22919 dependent, but are not in the scope of the declarator. */
22920 decl_specifiers->type
22921 = maybe_update_decl_type (decl_specifiers->type, scope);
22923 /* If we're allowing GNU extensions, look for an
22924 asm-specification. */
22925 if (cp_parser_allow_gnu_extensions_p (parser))
22927 /* Look for an asm-specification. */
22928 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
22929 asm_specification = cp_parser_asm_specification_opt (parser);
22931 else
22932 asm_specification = NULL_TREE;
22934 /* Gather the attributes that were provided with the
22935 decl-specifiers. */
22936 prefix_attributes = decl_specifiers->attributes;
22938 /* Look for attributes. */
22939 attributes_start_token = cp_lexer_peek_token (parser->lexer);
22940 attributes = cp_parser_attributes_opt (parser);
22942 /* Peek at the next token. */
22943 token = cp_lexer_peek_token (parser->lexer);
22945 bool bogus_implicit_tmpl = false;
22947 if (function_declarator_p (declarator))
22949 /* Handle C++17 deduction guides. Note that class-scope
22950 non-template deduction guides are instead handled in
22951 cp_parser_member_declaration. */
22952 cp_parser_maybe_adjust_declarator_for_dguide (parser,
22953 decl_specifiers,
22954 declarator,
22955 &ctor_dtor_or_conv_p);
22957 if (!member_p && !cp_parser_error_occurred (parser))
22958 warn_about_ambiguous_parse (decl_specifiers, declarator);
22960 /* Check to see if the token indicates the start of a
22961 function-definition. */
22962 if (cp_parser_token_starts_function_definition_p (token))
22964 if (!function_definition_allowed_p)
22966 /* If a function-definition should not appear here, issue an
22967 error message. */
22968 cp_parser_error (parser,
22969 "a function-definition is not allowed here");
22970 return error_mark_node;
22973 location_t func_brace_location
22974 = cp_lexer_peek_token (parser->lexer)->location;
22976 /* Neither attributes nor an asm-specification are allowed
22977 on a function-definition. */
22978 if (asm_specification)
22979 error_at (asm_spec_start_token->location,
22980 "an %<asm%> specification is not allowed "
22981 "on a function-definition");
22982 if (attributes)
22983 error_at (attributes_start_token->location,
22984 "attributes are not allowed "
22985 "on a function-definition");
22986 /* This is a function-definition. */
22987 *function_definition_p = true;
22989 /* Parse the function definition. */
22990 if (member_p)
22991 decl = cp_parser_save_member_function_body (parser,
22992 decl_specifiers,
22993 declarator,
22994 prefix_attributes);
22995 else
22996 decl =
22997 (cp_parser_function_definition_from_specifiers_and_declarator
22998 (parser, decl_specifiers, prefix_attributes, declarator));
23000 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
23002 /* This is where the prologue starts... */
23003 DECL_STRUCT_FUNCTION (decl)->function_start_locus
23004 = func_brace_location;
23007 return decl;
23010 else if (parser->fully_implicit_function_template_p)
23012 /* A non-template declaration involving a function parameter list
23013 containing an implicit template parameter will be made into a
23014 template. If the resulting declaration is not going to be an
23015 actual function then finish the template scope here to prevent it.
23016 An error message will be issued once we have a decl to talk about.
23018 FIXME probably we should do type deduction rather than create an
23019 implicit template, but the standard currently doesn't allow it. */
23020 bogus_implicit_tmpl = true;
23021 finish_fully_implicit_template (parser, NULL_TREE);
23024 /* [dcl.dcl]
23026 Only in function declarations for constructors, destructors, type
23027 conversions, and deduction guides can the decl-specifier-seq be omitted.
23029 We explicitly postpone this check past the point where we handle
23030 function-definitions because we tolerate function-definitions
23031 that are missing their return types in some modes. */
23032 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
23034 cp_parser_error (parser,
23035 "expected constructor, destructor, or type conversion");
23036 return error_mark_node;
23039 /* An `=' or an '{' in C++11, indicate an initializer. An '(' may indicate
23040 an initializer as well. */
23041 if (token->type == CPP_EQ
23042 || token->type == CPP_OPEN_PAREN
23043 || token->type == CPP_OPEN_BRACE)
23045 /* Don't get fooled into thinking that F(i)(1)(2) is an initializer.
23046 It isn't; it's an expression. (Here '(i)' would have already been
23047 parsed as a declarator.) */
23048 if (token->type == CPP_OPEN_PAREN
23049 && cp_parser_uncommitted_to_tentative_parse_p (parser))
23051 cp_lexer_save_tokens (parser->lexer);
23052 cp_lexer_consume_token (parser->lexer);
23053 cp_parser_skip_to_closing_parenthesis (parser,
23054 /*recovering*/false,
23055 /*or_comma*/false,
23056 /*consume_paren*/true);
23057 /* If this is an initializer, only a ',' or ';' can follow: either
23058 we have another init-declarator, or we're at the end of an
23059 init-declarator-list which can only be followed by a ';'. */
23060 bool ok = (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23061 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
23062 cp_lexer_rollback_tokens (parser->lexer);
23063 if (UNLIKELY (!ok))
23064 /* Not an init-declarator. */
23065 return error_mark_node;
23067 is_initialized = SD_INITIALIZED;
23068 initialization_kind = token->type;
23069 declarator->init_loc = token->location;
23070 if (maybe_range_for_decl)
23071 *maybe_range_for_decl = error_mark_node;
23072 tmp_init_loc = token->location;
23073 if (init_loc && *init_loc == UNKNOWN_LOCATION)
23074 *init_loc = tmp_init_loc;
23076 if (token->type == CPP_EQ
23077 && function_declarator_p (declarator))
23079 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
23080 if (t2->keyword == RID_DEFAULT)
23081 is_initialized = SD_DEFAULTED;
23082 else if (t2->keyword == RID_DELETE)
23083 is_initialized = SD_DELETED;
23086 else
23088 /* If the init-declarator isn't initialized and isn't followed by a
23089 `,' or `;', it's not a valid init-declarator. */
23090 if (token->type != CPP_COMMA
23091 && token->type != CPP_SEMICOLON)
23093 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
23094 range_for_decl_p = true;
23095 else
23097 if (!maybe_range_for_decl)
23098 cp_parser_error (parser, "expected initializer");
23099 return error_mark_node;
23102 is_initialized = SD_UNINITIALIZED;
23103 initialization_kind = CPP_EOF;
23106 /* Because start_decl has side-effects, we should only call it if we
23107 know we're going ahead. By this point, we know that we cannot
23108 possibly be looking at any other construct. */
23109 cp_parser_commit_to_tentative_parse (parser);
23111 /* Enter the newly declared entry in the symbol table. If we're
23112 processing a declaration in a class-specifier, we wait until
23113 after processing the initializer. */
23114 if (!member_p)
23116 if (parser->in_unbraced_linkage_specification_p)
23117 decl_specifiers->storage_class = sc_extern;
23118 decl = start_decl (declarator, decl_specifiers,
23119 range_for_decl_p? SD_INITIALIZED : is_initialized,
23120 attributes, prefix_attributes, &pushed_scope);
23121 cp_finalize_omp_declare_simd (parser, decl);
23122 cp_finalize_oacc_routine (parser, decl, false);
23123 /* Adjust location of decl if declarator->id_loc is more appropriate:
23124 set, and decl wasn't merged with another decl, in which case its
23125 location would be different from input_location, and more accurate. */
23126 if (DECL_P (decl)
23127 && declarator->id_loc != UNKNOWN_LOCATION
23128 && DECL_SOURCE_LOCATION (decl) == input_location)
23129 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
23131 else if (scope)
23132 /* Enter the SCOPE. That way unqualified names appearing in the
23133 initializer will be looked up in SCOPE. */
23134 pushed_scope = push_scope (scope);
23136 /* Perform deferred access control checks, now that we know in which
23137 SCOPE the declared entity resides. */
23138 if (!member_p && decl)
23140 tree saved_current_function_decl = NULL_TREE;
23142 /* If the entity being declared is a function, pretend that we
23143 are in its scope. If it is a `friend', it may have access to
23144 things that would not otherwise be accessible. */
23145 if (TREE_CODE (decl) == FUNCTION_DECL)
23147 saved_current_function_decl = current_function_decl;
23148 current_function_decl = decl;
23151 /* Perform access checks for template parameters. */
23152 cp_parser_perform_template_parameter_access_checks (checks);
23154 /* Perform the access control checks for the declarator and the
23155 decl-specifiers. */
23156 perform_deferred_access_checks (tf_warning_or_error);
23158 /* Restore the saved value. */
23159 if (TREE_CODE (decl) == FUNCTION_DECL)
23160 current_function_decl = saved_current_function_decl;
23163 /* Parse the initializer. */
23164 initializer = NULL_TREE;
23165 is_direct_init = false;
23166 is_non_constant_init = true;
23167 if (is_initialized)
23169 if (function_declarator_p (declarator))
23171 if (initialization_kind == CPP_EQ)
23172 initializer = cp_parser_pure_specifier (parser);
23173 else
23175 /* If the declaration was erroneous, we don't really
23176 know what the user intended, so just silently
23177 consume the initializer. */
23178 if (decl != error_mark_node)
23179 error_at (tmp_init_loc, "initializer provided for function");
23180 cp_parser_skip_to_closing_parenthesis (parser,
23181 /*recovering=*/true,
23182 /*or_comma=*/false,
23183 /*consume_paren=*/true);
23186 else
23188 /* We want to record the extra mangling scope for in-class
23189 initializers of class members and initializers of static
23190 data member templates and namespace-scope initializers.
23191 The former involves deferring parsing of the initializer
23192 until end of class as with default arguments. So right
23193 here we only handle the latter two. */
23194 bool has_lambda_scope = false;
23196 if (decl != error_mark_node
23197 && !member_p
23198 && (processing_template_decl || DECL_NAMESPACE_SCOPE_P (decl)))
23199 has_lambda_scope = true;
23201 if (has_lambda_scope)
23202 start_lambda_scope (decl);
23203 initializer = cp_parser_initializer (parser,
23204 &is_direct_init,
23205 &is_non_constant_init);
23206 if (has_lambda_scope)
23207 finish_lambda_scope ();
23208 if (initializer == error_mark_node)
23209 cp_parser_skip_to_end_of_statement (parser);
23213 /* The old parser allows attributes to appear after a parenthesized
23214 initializer. Mark Mitchell proposed removing this functionality
23215 on the GCC mailing lists on 2002-08-13. This parser accepts the
23216 attributes -- but ignores them. Made a permerror in GCC 8. */
23217 if (cp_parser_allow_gnu_extensions_p (parser)
23218 && initialization_kind == CPP_OPEN_PAREN
23219 && cp_parser_attributes_opt (parser)
23220 && permerror (input_location,
23221 "attributes after parenthesized initializer ignored"))
23223 static bool hint;
23224 if (flag_permissive && !hint)
23226 hint = true;
23227 inform (input_location,
23228 "this flexibility is deprecated and will be removed");
23232 /* And now complain about a non-function implicit template. */
23233 if (bogus_implicit_tmpl && decl != error_mark_node)
23234 error_at (DECL_SOURCE_LOCATION (decl),
23235 "non-function %qD declared as implicit template", decl);
23237 /* For an in-class declaration, use `grokfield' to create the
23238 declaration. */
23239 if (member_p)
23241 if (pushed_scope)
23243 pop_scope (pushed_scope);
23244 pushed_scope = NULL_TREE;
23246 decl = grokfield (declarator, decl_specifiers,
23247 initializer, !is_non_constant_init,
23248 /*asmspec=*/NULL_TREE,
23249 attr_chainon (attributes, prefix_attributes));
23250 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
23251 cp_parser_save_default_args (parser, decl);
23252 cp_finalize_omp_declare_simd (parser, decl);
23253 cp_finalize_oacc_routine (parser, decl, false);
23256 /* Finish processing the declaration. But, skip member
23257 declarations. */
23258 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
23260 cp_finish_decl (decl,
23261 initializer, !is_non_constant_init,
23262 asm_specification,
23263 /* If the initializer is in parentheses, then this is
23264 a direct-initialization, which means that an
23265 `explicit' constructor is OK. Otherwise, an
23266 `explicit' constructor cannot be used. */
23267 ((is_direct_init || !is_initialized)
23268 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
23270 else if ((cxx_dialect != cxx98) && friend_p
23271 && decl && TREE_CODE (decl) == FUNCTION_DECL)
23272 /* Core issue #226 (C++0x only): A default template-argument
23273 shall not be specified in a friend class template
23274 declaration. */
23275 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
23276 /*is_partial=*/false, /*is_friend_decl=*/1);
23278 if (!friend_p && pushed_scope)
23279 pop_scope (pushed_scope);
23281 if (function_declarator_p (declarator)
23282 && parser->fully_implicit_function_template_p)
23284 if (member_p)
23285 decl = finish_fully_implicit_template (parser, decl);
23286 else
23287 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
23290 if (auto_result && is_initialized && decl_specifiers->type
23291 && type_uses_auto (decl_specifiers->type))
23292 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
23294 return decl;
23297 /* Parse a declarator.
23299 declarator:
23300 direct-declarator
23301 ptr-operator declarator
23303 abstract-declarator:
23304 ptr-operator abstract-declarator [opt]
23305 direct-abstract-declarator
23307 GNU Extensions:
23309 declarator:
23310 attributes [opt] direct-declarator
23311 attributes [opt] ptr-operator declarator
23313 abstract-declarator:
23314 attributes [opt] ptr-operator abstract-declarator [opt]
23315 attributes [opt] direct-abstract-declarator
23317 The parser flags FLAGS is used to control type-specifier parsing.
23319 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
23320 detect constructors, destructors, deduction guides, or conversion operators.
23321 It is set to -1 if the declarator is a name, and +1 if it is a
23322 function. Otherwise it is set to zero. Usually you just want to
23323 test for >0, but internally the negative value is used.
23325 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
23326 a decl-specifier-seq unless it declares a constructor, destructor,
23327 or conversion. It might seem that we could check this condition in
23328 semantic analysis, rather than parsing, but that makes it difficult
23329 to handle something like `f()'. We want to notice that there are
23330 no decl-specifiers, and therefore realize that this is an
23331 expression, not a declaration.)
23333 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
23334 the declarator is a direct-declarator of the form "(...)".
23336 MEMBER_P is true iff this declarator is a member-declarator.
23338 FRIEND_P is true iff this declarator is a friend.
23340 STATIC_P is true iff the keyword static was seen. */
23342 static cp_declarator *
23343 cp_parser_declarator (cp_parser* parser,
23344 cp_parser_declarator_kind dcl_kind,
23345 cp_parser_flags flags,
23346 int* ctor_dtor_or_conv_p,
23347 bool* parenthesized_p,
23348 bool member_p, bool friend_p, bool static_p)
23350 cp_declarator *declarator;
23351 enum tree_code code;
23352 cp_cv_quals cv_quals;
23353 tree class_type;
23354 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
23356 /* Assume this is not a constructor, destructor, or type-conversion
23357 operator. */
23358 if (ctor_dtor_or_conv_p)
23359 *ctor_dtor_or_conv_p = 0;
23361 if (cp_parser_allow_gnu_extensions_p (parser))
23362 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
23364 /* Check for the ptr-operator production. */
23365 cp_parser_parse_tentatively (parser);
23366 /* Parse the ptr-operator. */
23367 code = cp_parser_ptr_operator (parser,
23368 &class_type,
23369 &cv_quals,
23370 &std_attributes);
23372 /* If that worked, then we have a ptr-operator. */
23373 if (cp_parser_parse_definitely (parser))
23375 /* If a ptr-operator was found, then this declarator was not
23376 parenthesized. */
23377 if (parenthesized_p)
23378 *parenthesized_p = false;
23379 /* The dependent declarator is optional if we are parsing an
23380 abstract-declarator. */
23381 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
23382 cp_parser_parse_tentatively (parser);
23384 /* Parse the dependent declarator. */
23385 declarator = cp_parser_declarator (parser, dcl_kind, flags,
23386 /*ctor_dtor_or_conv_p=*/NULL,
23387 /*parenthesized_p=*/NULL,
23388 member_p, friend_p, static_p);
23390 /* If we are parsing an abstract-declarator, we must handle the
23391 case where the dependent declarator is absent. */
23392 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
23393 && !cp_parser_parse_definitely (parser))
23394 declarator = NULL;
23396 declarator = cp_parser_make_indirect_declarator
23397 (code, class_type, cv_quals, declarator, std_attributes);
23399 /* Everything else is a direct-declarator. */
23400 else
23402 if (parenthesized_p)
23403 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
23404 CPP_OPEN_PAREN);
23405 declarator = cp_parser_direct_declarator (parser, dcl_kind,
23406 flags, ctor_dtor_or_conv_p,
23407 member_p, friend_p, static_p);
23410 if (gnu_attributes && declarator && declarator != cp_error_declarator)
23411 declarator->attributes = gnu_attributes;
23412 return declarator;
23415 /* Parse a direct-declarator or direct-abstract-declarator.
23417 direct-declarator:
23418 declarator-id
23419 direct-declarator ( parameter-declaration-clause )
23420 cv-qualifier-seq [opt]
23421 ref-qualifier [opt]
23422 exception-specification [opt]
23423 direct-declarator [ constant-expression [opt] ]
23424 ( declarator )
23426 direct-abstract-declarator:
23427 direct-abstract-declarator [opt]
23428 ( parameter-declaration-clause )
23429 cv-qualifier-seq [opt]
23430 ref-qualifier [opt]
23431 exception-specification [opt]
23432 direct-abstract-declarator [opt] [ constant-expression [opt] ]
23433 ( abstract-declarator )
23435 Returns a representation of the declarator. DCL_KIND is
23436 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
23437 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
23438 we are parsing a direct-declarator. It is
23439 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
23440 of ambiguity we prefer an abstract declarator, as per
23441 [dcl.ambig.res].
23442 The parser flags FLAGS is used to control type-specifier parsing.
23443 CTOR_DTOR_OR_CONV_P, MEMBER_P, FRIEND_P, and STATIC_P are
23444 as for cp_parser_declarator. */
23446 static cp_declarator *
23447 cp_parser_direct_declarator (cp_parser* parser,
23448 cp_parser_declarator_kind dcl_kind,
23449 cp_parser_flags flags,
23450 int* ctor_dtor_or_conv_p,
23451 bool member_p, bool friend_p, bool static_p)
23453 cp_token *token;
23454 cp_declarator *declarator = NULL;
23455 tree scope = NULL_TREE;
23456 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
23457 bool saved_in_declarator_p = parser->in_declarator_p;
23458 bool first = true;
23459 tree pushed_scope = NULL_TREE;
23460 cp_token *open_paren = NULL, *close_paren = NULL;
23462 while (true)
23464 /* Peek at the next token. */
23465 token = cp_lexer_peek_token (parser->lexer);
23466 if (token->type == CPP_OPEN_PAREN)
23468 /* This is either a parameter-declaration-clause, or a
23469 parenthesized declarator. When we know we are parsing a
23470 named declarator, it must be a parenthesized declarator
23471 if FIRST is true. For instance, `(int)' is a
23472 parameter-declaration-clause, with an omitted
23473 direct-abstract-declarator. But `((*))', is a
23474 parenthesized abstract declarator. Finally, when T is a
23475 template parameter `(T)' is a
23476 parameter-declaration-clause, and not a parenthesized
23477 named declarator.
23479 We first try and parse a parameter-declaration-clause,
23480 and then try a nested declarator (if FIRST is true).
23482 It is not an error for it not to be a
23483 parameter-declaration-clause, even when FIRST is
23484 false. Consider,
23486 int i (int);
23487 int i (3);
23489 The first is the declaration of a function while the
23490 second is the definition of a variable, including its
23491 initializer.
23493 Having seen only the parenthesis, we cannot know which of
23494 these two alternatives should be selected. Even more
23495 complex are examples like:
23497 int i (int (a));
23498 int i (int (3));
23500 The former is a function-declaration; the latter is a
23501 variable initialization.
23503 Thus again, we try a parameter-declaration-clause, and if
23504 that fails, we back out and return. */
23506 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
23508 tree params;
23509 bool is_declarator = false;
23511 open_paren = NULL;
23513 /* In a member-declarator, the only valid interpretation
23514 of a parenthesis is the start of a
23515 parameter-declaration-clause. (It is invalid to
23516 initialize a static data member with a parenthesized
23517 initializer; only the "=" form of initialization is
23518 permitted.) */
23519 if (!member_p)
23520 cp_parser_parse_tentatively (parser);
23522 /* Consume the `('. */
23523 const location_t parens_start = token->location;
23524 matching_parens parens;
23525 parens.consume_open (parser);
23526 if (first)
23528 /* If this is going to be an abstract declarator, we're
23529 in a declarator and we can't have default args. */
23530 parser->default_arg_ok_p = false;
23531 parser->in_declarator_p = true;
23534 begin_scope (sk_function_parms, NULL_TREE);
23536 /* Parse the parameter-declaration-clause. */
23537 params
23538 = cp_parser_parameter_declaration_clause (parser, flags);
23539 const location_t parens_end
23540 = cp_lexer_peek_token (parser->lexer)->location;
23542 /* Consume the `)'. */
23543 parens.require_close (parser);
23545 /* If all went well, parse the cv-qualifier-seq,
23546 ref-qualifier and the exception-specification. */
23547 if (member_p || cp_parser_parse_definitely (parser))
23549 cp_cv_quals cv_quals;
23550 cp_virt_specifiers virt_specifiers;
23551 cp_ref_qualifier ref_qual;
23552 tree exception_specification;
23553 tree late_return;
23554 tree attrs;
23555 bool memfn = (member_p || (pushed_scope
23556 && CLASS_TYPE_P (pushed_scope)));
23557 unsigned char local_variables_forbidden_p
23558 = parser->local_variables_forbidden_p;
23559 /* 'this' is not allowed in static member functions. */
23560 if (static_p || friend_p)
23561 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
23563 is_declarator = true;
23565 if (ctor_dtor_or_conv_p)
23566 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
23567 first = false;
23569 /* Parse the cv-qualifier-seq. */
23570 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
23571 /* Parse the ref-qualifier. */
23572 ref_qual = cp_parser_ref_qualifier_opt (parser);
23573 /* Parse the tx-qualifier. */
23574 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
23576 tree save_ccp = current_class_ptr;
23577 tree save_ccr = current_class_ref;
23578 if (memfn && !friend_p && !static_p)
23579 /* DR 1207: 'this' is in scope after the cv-quals. */
23580 inject_this_parameter (current_class_type, cv_quals);
23582 /* If it turned out that this is e.g. a pointer to a
23583 function, we don't want to delay noexcept parsing. */
23584 if (declarator == NULL || declarator->kind != cdk_id)
23585 flags &= ~CP_PARSER_FLAGS_DELAY_NOEXCEPT;
23587 /* Parse the exception-specification. */
23588 exception_specification
23589 = cp_parser_exception_specification_opt (parser,
23590 flags);
23592 attrs = cp_parser_std_attribute_spec_seq (parser);
23594 cp_omp_declare_simd_data odsd;
23595 if ((flag_openmp || flag_openmp_simd)
23596 && declarator
23597 && declarator->std_attributes
23598 && declarator->kind == cdk_id)
23600 tree *pa = &declarator->std_attributes;
23601 cp_parser_handle_directive_omp_attributes (parser, pa,
23602 &odsd, false);
23605 /* In here, we handle cases where attribute is used after
23606 the function declaration. For example:
23607 void func (int x) __attribute__((vector(..))); */
23608 tree gnu_attrs = NULL_TREE;
23609 tree requires_clause = NULL_TREE;
23610 late_return
23611 = cp_parser_late_return_type_opt (parser, declarator,
23612 requires_clause);
23614 cp_finalize_omp_declare_simd (parser, &odsd);
23616 /* Parse the virt-specifier-seq. */
23617 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
23619 location_t parens_loc = make_location (parens_start,
23620 parens_start,
23621 parens_end);
23622 /* Create the function-declarator. */
23623 declarator = make_call_declarator (declarator,
23624 params,
23625 cv_quals,
23626 virt_specifiers,
23627 ref_qual,
23628 tx_qual,
23629 exception_specification,
23630 late_return,
23631 requires_clause,
23632 attrs,
23633 parens_loc);
23634 declarator->attributes = gnu_attrs;
23635 /* Any subsequent parameter lists are to do with
23636 return type, so are not those of the declared
23637 function. */
23638 parser->default_arg_ok_p = false;
23640 current_class_ptr = save_ccp;
23641 current_class_ref = save_ccr;
23643 /* Restore the state of local_variables_forbidden_p. */
23644 parser->local_variables_forbidden_p
23645 = local_variables_forbidden_p;
23648 /* Remove the function parms from scope. */
23649 pop_bindings_and_leave_scope ();
23651 if (is_declarator)
23652 /* Repeat the main loop. */
23653 continue;
23656 /* If this is the first, we can try a parenthesized
23657 declarator. */
23658 if (first)
23660 bool saved_in_type_id_in_expr_p;
23662 parser->default_arg_ok_p = saved_default_arg_ok_p;
23663 parser->in_declarator_p = saved_in_declarator_p;
23665 open_paren = token;
23666 /* Consume the `('. */
23667 matching_parens parens;
23668 parens.consume_open (parser);
23669 /* Parse the nested declarator. */
23670 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
23671 parser->in_type_id_in_expr_p = true;
23672 declarator
23673 = cp_parser_declarator (parser, dcl_kind, flags,
23674 ctor_dtor_or_conv_p,
23675 /*parenthesized_p=*/NULL,
23676 member_p, friend_p,
23677 /*static_p=*/false);
23678 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
23679 first = false;
23680 /* Expect a `)'. */
23681 close_paren = cp_lexer_peek_token (parser->lexer);
23682 if (!parens.require_close (parser))
23683 declarator = cp_error_declarator;
23684 if (declarator == cp_error_declarator)
23685 break;
23687 goto handle_declarator;
23689 /* Otherwise, we must be done. */
23690 else
23691 break;
23693 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
23694 && token->type == CPP_OPEN_SQUARE
23695 && !cp_next_tokens_can_be_attribute_p (parser))
23697 /* Parse an array-declarator. */
23698 tree bounds, attrs;
23700 if (ctor_dtor_or_conv_p)
23701 *ctor_dtor_or_conv_p = 0;
23703 open_paren = NULL;
23704 first = false;
23705 parser->default_arg_ok_p = false;
23706 parser->in_declarator_p = true;
23707 /* Consume the `['. */
23708 cp_lexer_consume_token (parser->lexer);
23709 /* Peek at the next token. */
23710 token = cp_lexer_peek_token (parser->lexer);
23711 /* If the next token is `]', then there is no
23712 constant-expression. */
23713 if (token->type != CPP_CLOSE_SQUARE)
23715 bool non_constant_p;
23716 bounds
23717 = cp_parser_constant_expression (parser,
23718 /*allow_non_constant=*/true,
23719 &non_constant_p);
23720 if (!non_constant_p)
23721 /* OK */;
23722 else if (error_operand_p (bounds))
23723 /* Already gave an error. */;
23724 else if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
23725 /* Let compute_array_index_type diagnose this. */;
23726 else if (!parser->in_function_body
23727 || parsing_function_declarator ())
23729 /* Normally, the array bound must be an integral constant
23730 expression. However, as an extension, we allow VLAs
23731 in function scopes as long as they aren't part of a
23732 parameter declaration. */
23733 cp_parser_error (parser,
23734 "array bound is not an integer constant");
23735 bounds = error_mark_node;
23737 else if (processing_template_decl
23738 && !type_dependent_expression_p (bounds))
23740 /* Remember this wasn't a constant-expression. */
23741 bounds = build_nop (TREE_TYPE (bounds), bounds);
23742 TREE_SIDE_EFFECTS (bounds) = 1;
23745 else
23746 bounds = NULL_TREE;
23747 /* Look for the closing `]'. */
23748 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
23750 declarator = cp_error_declarator;
23751 break;
23754 attrs = cp_parser_std_attribute_spec_seq (parser);
23755 declarator = make_array_declarator (declarator, bounds);
23756 declarator->std_attributes = attrs;
23758 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
23761 tree qualifying_scope;
23762 tree unqualified_name;
23763 tree attrs;
23764 special_function_kind sfk;
23765 bool abstract_ok;
23766 bool pack_expansion_p = false;
23767 cp_token *declarator_id_start_token;
23769 /* Parse a declarator-id */
23770 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
23771 if (abstract_ok)
23773 cp_parser_parse_tentatively (parser);
23775 /* If we see an ellipsis, we should be looking at a
23776 parameter pack. */
23777 if (token->type == CPP_ELLIPSIS)
23779 /* Consume the `...' */
23780 cp_lexer_consume_token (parser->lexer);
23782 pack_expansion_p = true;
23786 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
23787 unqualified_name
23788 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
23789 qualifying_scope = parser->scope;
23790 if (abstract_ok)
23792 bool okay = false;
23794 if (!unqualified_name && pack_expansion_p)
23796 /* Check whether an error occurred. */
23797 okay = !cp_parser_error_occurred (parser);
23799 /* We already consumed the ellipsis to mark a
23800 parameter pack, but we have no way to report it,
23801 so abort the tentative parse. We will be exiting
23802 immediately anyway. */
23803 cp_parser_abort_tentative_parse (parser);
23805 else
23806 okay = cp_parser_parse_definitely (parser);
23808 if (!okay)
23809 unqualified_name = error_mark_node;
23810 else if (unqualified_name
23811 && (qualifying_scope
23812 || (!identifier_p (unqualified_name))))
23814 cp_parser_error (parser, "expected unqualified-id");
23815 unqualified_name = error_mark_node;
23819 if (!unqualified_name)
23820 return NULL;
23821 if (unqualified_name == error_mark_node)
23823 declarator = cp_error_declarator;
23824 pack_expansion_p = false;
23825 declarator->parameter_pack_p = false;
23826 break;
23829 attrs = cp_parser_std_attribute_spec_seq (parser);
23831 if (qualifying_scope && at_namespace_scope_p ()
23832 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
23834 /* In the declaration of a member of a template class
23835 outside of the class itself, the SCOPE will sometimes
23836 be a TYPENAME_TYPE. For example, given:
23838 template <typename T>
23839 int S<T>::R::i = 3;
23841 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
23842 this context, we must resolve S<T>::R to an ordinary
23843 type, rather than a typename type.
23845 The reason we normally avoid resolving TYPENAME_TYPEs
23846 is that a specialization of `S' might render
23847 `S<T>::R' not a type. However, if `S' is
23848 specialized, then this `i' will not be used, so there
23849 is no harm in resolving the types here. */
23850 tree type;
23852 /* Resolve the TYPENAME_TYPE. */
23853 type = resolve_typename_type (qualifying_scope,
23854 /*only_current_p=*/false);
23855 /* If that failed, the declarator is invalid. */
23856 if (TREE_CODE (type) == TYPENAME_TYPE)
23858 if (typedef_variant_p (type))
23859 error_at (declarator_id_start_token->location,
23860 "cannot define member of dependent typedef "
23861 "%qT", type);
23862 else
23863 error_at (declarator_id_start_token->location,
23864 "%<%T::%E%> is not a type",
23865 TYPE_CONTEXT (qualifying_scope),
23866 TYPE_IDENTIFIER (qualifying_scope));
23868 qualifying_scope = type;
23871 sfk = sfk_none;
23873 if (unqualified_name)
23875 tree class_type;
23877 if (qualifying_scope
23878 && CLASS_TYPE_P (qualifying_scope))
23879 class_type = qualifying_scope;
23880 else
23881 class_type = current_class_type;
23883 if (TREE_CODE (unqualified_name) == TYPE_DECL)
23885 tree name_type = TREE_TYPE (unqualified_name);
23887 if (!class_type || !same_type_p (name_type, class_type))
23889 /* We do not attempt to print the declarator
23890 here because we do not have enough
23891 information about its original syntactic
23892 form. */
23893 cp_parser_error (parser, "invalid declarator");
23894 declarator = cp_error_declarator;
23895 break;
23897 else if (qualifying_scope
23898 && CLASSTYPE_USE_TEMPLATE (name_type))
23900 error_at (declarator_id_start_token->location,
23901 "invalid use of constructor as a template");
23902 inform (declarator_id_start_token->location,
23903 "use %<%T::%D%> instead of %<%T::%D%> to "
23904 "name the constructor in a qualified name",
23905 class_type,
23906 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
23907 class_type, name_type);
23908 declarator = cp_error_declarator;
23909 break;
23911 unqualified_name = constructor_name (class_type);
23914 if (class_type)
23916 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
23917 sfk = sfk_destructor;
23918 else if (identifier_p (unqualified_name)
23919 && IDENTIFIER_CONV_OP_P (unqualified_name))
23920 sfk = sfk_conversion;
23921 else if (/* There's no way to declare a constructor
23922 for an unnamed type, even if the type
23923 got a name for linkage purposes. */
23924 !TYPE_WAS_UNNAMED (class_type)
23925 /* Handle correctly (c++/19200):
23927 struct S {
23928 struct T{};
23929 friend void S(T);
23932 and also:
23934 namespace N {
23935 void S();
23938 struct S {
23939 friend void N::S();
23940 }; */
23941 && (!friend_p || class_type == qualifying_scope)
23942 && constructor_name_p (unqualified_name,
23943 class_type))
23944 sfk = sfk_constructor;
23945 else if (is_overloaded_fn (unqualified_name)
23946 && DECL_CONSTRUCTOR_P (get_first_fn
23947 (unqualified_name)))
23948 sfk = sfk_constructor;
23950 if (ctor_dtor_or_conv_p && sfk != sfk_none)
23951 *ctor_dtor_or_conv_p = -1;
23954 declarator = make_id_declarator (qualifying_scope,
23955 unqualified_name,
23956 sfk, token->location);
23957 declarator->std_attributes = attrs;
23958 declarator->parameter_pack_p = pack_expansion_p;
23960 if (pack_expansion_p)
23961 maybe_warn_variadic_templates ();
23963 /* We're looking for this case in [temp.res]:
23964 A qualified-id is assumed to name a type if [...]
23965 - it is a decl-specifier of the decl-specifier-seq of a
23966 parameter-declaration in a declarator of a function or
23967 function template declaration, ... */
23968 if (cxx_dialect >= cxx20
23969 && (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL)
23970 && declarator->kind == cdk_id
23971 && !at_class_scope_p ()
23972 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23974 /* ...whose declarator-id is qualified. If it isn't, never
23975 assume the parameters to refer to types. */
23976 if (qualifying_scope == NULL_TREE)
23977 flags &= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
23978 else
23980 /* Now we have something like
23981 template <typename T> int C::x(S::p);
23982 which can be a function template declaration or a
23983 variable template definition. If name lookup for
23984 the declarator-id C::x finds one or more function
23985 templates, assume S::p to name a type. Otherwise,
23986 don't. */
23987 tree decl
23988 = cp_parser_lookup_name (parser, unqualified_name,
23989 none_type,
23990 /*is_template=*/false,
23991 /*is_namespace=*/false,
23992 /*check_dependency=*/false,
23993 /*ambiguous_decls=*/NULL,
23994 token->location);
23996 if (!is_overloaded_fn (decl)
23997 /* Allow
23998 template<typename T>
23999 A<T>::A(T::type) { } */
24000 && !(MAYBE_CLASS_TYPE_P (qualifying_scope)
24001 && constructor_name_p (unqualified_name,
24002 qualifying_scope)))
24003 flags &= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
24008 handle_declarator:;
24009 scope = get_scope_of_declarator (declarator);
24010 if (scope)
24012 /* Any names that appear after the declarator-id for a
24013 member are looked up in the containing scope. */
24014 if (at_function_scope_p ())
24016 /* But declarations with qualified-ids can't appear in a
24017 function. */
24018 cp_parser_error (parser, "qualified-id in declaration");
24019 declarator = cp_error_declarator;
24020 break;
24022 pushed_scope = push_scope (scope);
24024 parser->in_declarator_p = true;
24025 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
24026 || (declarator && declarator->kind == cdk_id))
24027 /* Default args are only allowed on function
24028 declarations. */
24029 parser->default_arg_ok_p = saved_default_arg_ok_p;
24030 else
24031 parser->default_arg_ok_p = false;
24033 first = false;
24035 /* We're done. */
24036 else
24037 break;
24040 /* For an abstract declarator, we might wind up with nothing at this
24041 point. That's an error; the declarator is not optional. */
24042 if (!declarator)
24043 cp_parser_error (parser, "expected declarator");
24044 else if (open_paren)
24046 /* Record overly parenthesized declarator so we can give a
24047 diagnostic about confusing decl/expr disambiguation. */
24048 if (declarator->kind == cdk_array)
24050 /* If the open and close parens are on different lines, this
24051 is probably a formatting thing, so ignore. */
24052 expanded_location open = expand_location (open_paren->location);
24053 expanded_location close = expand_location (close_paren->location);
24054 if (open.line != close.line || open.file != close.file)
24055 open_paren = NULL;
24057 if (open_paren)
24058 declarator->parenthesized = make_location (open_paren->location,
24059 open_paren->location,
24060 close_paren->location);
24063 /* If we entered a scope, we must exit it now. */
24064 if (pushed_scope)
24065 pop_scope (pushed_scope);
24067 parser->default_arg_ok_p = saved_default_arg_ok_p;
24068 parser->in_declarator_p = saved_in_declarator_p;
24070 return declarator;
24073 /* Parse a ptr-operator.
24075 ptr-operator:
24076 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
24077 * cv-qualifier-seq [opt]
24079 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
24080 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
24082 GNU Extension:
24084 ptr-operator:
24085 & cv-qualifier-seq [opt]
24087 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
24088 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
24089 an rvalue reference. In the case of a pointer-to-member, *TYPE is
24090 filled in with the TYPE containing the member. *CV_QUALS is
24091 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
24092 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
24093 Note that the tree codes returned by this function have nothing
24094 to do with the types of trees that will be eventually be created
24095 to represent the pointer or reference type being parsed. They are
24096 just constants with suggestive names. */
24097 static enum tree_code
24098 cp_parser_ptr_operator (cp_parser* parser,
24099 tree* type,
24100 cp_cv_quals *cv_quals,
24101 tree *attributes)
24103 enum tree_code code = ERROR_MARK;
24104 cp_token *token;
24105 tree attrs = NULL_TREE;
24107 /* Assume that it's not a pointer-to-member. */
24108 *type = NULL_TREE;
24109 /* And that there are no cv-qualifiers. */
24110 *cv_quals = TYPE_UNQUALIFIED;
24112 /* Peek at the next token. */
24113 token = cp_lexer_peek_token (parser->lexer);
24115 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
24116 if (token->type == CPP_MULT)
24117 code = INDIRECT_REF;
24118 else if (token->type == CPP_AND)
24119 code = ADDR_EXPR;
24120 else if ((cxx_dialect != cxx98) &&
24121 token->type == CPP_AND_AND) /* C++0x only */
24122 code = NON_LVALUE_EXPR;
24124 if (code != ERROR_MARK)
24126 /* Consume the `*', `&' or `&&'. */
24127 cp_lexer_consume_token (parser->lexer);
24129 /* A `*' can be followed by a cv-qualifier-seq, and so can a
24130 `&', if we are allowing GNU extensions. (The only qualifier
24131 that can legally appear after `&' is `restrict', but that is
24132 enforced during semantic analysis. */
24133 if (code == INDIRECT_REF
24134 || cp_parser_allow_gnu_extensions_p (parser))
24135 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
24137 attrs = cp_parser_std_attribute_spec_seq (parser);
24138 if (attributes != NULL)
24139 *attributes = attrs;
24141 else
24143 /* Try the pointer-to-member case. */
24144 cp_parser_parse_tentatively (parser);
24145 /* Look for the optional `::' operator. */
24146 cp_parser_global_scope_opt (parser,
24147 /*current_scope_valid_p=*/false);
24148 /* Look for the nested-name specifier. */
24149 token = cp_lexer_peek_token (parser->lexer);
24150 cp_parser_nested_name_specifier (parser,
24151 /*typename_keyword_p=*/false,
24152 /*check_dependency_p=*/true,
24153 /*type_p=*/false,
24154 /*is_declaration=*/false);
24155 /* If we found it, and the next token is a `*', then we are
24156 indeed looking at a pointer-to-member operator. */
24157 if (!cp_parser_error_occurred (parser)
24158 && cp_parser_require (parser, CPP_MULT, RT_MULT))
24160 /* Indicate that the `*' operator was used. */
24161 code = INDIRECT_REF;
24163 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
24164 error_at (token->location, "%qD is a namespace", parser->scope);
24165 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
24166 error_at (token->location, "cannot form pointer to member of "
24167 "non-class %q#T", parser->scope);
24168 else
24170 /* The type of which the member is a member is given by the
24171 current SCOPE. */
24172 *type = parser->scope;
24173 /* The next name will not be qualified. */
24174 parser->scope = NULL_TREE;
24175 parser->qualifying_scope = NULL_TREE;
24176 parser->object_scope = NULL_TREE;
24177 /* Look for optional c++11 attributes. */
24178 attrs = cp_parser_std_attribute_spec_seq (parser);
24179 if (attributes != NULL)
24180 *attributes = attrs;
24181 /* Look for the optional cv-qualifier-seq. */
24182 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
24185 /* If that didn't work we don't have a ptr-operator. */
24186 if (!cp_parser_parse_definitely (parser))
24187 cp_parser_error (parser, "expected ptr-operator");
24190 return code;
24193 /* Parse an (optional) cv-qualifier-seq.
24195 cv-qualifier-seq:
24196 cv-qualifier cv-qualifier-seq [opt]
24198 cv-qualifier:
24199 const
24200 volatile
24202 GNU Extension:
24204 cv-qualifier:
24205 __restrict__
24207 Returns a bitmask representing the cv-qualifiers. */
24209 static cp_cv_quals
24210 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
24212 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
24214 while (true)
24216 cp_token *token;
24217 cp_cv_quals cv_qualifier;
24219 /* Peek at the next token. */
24220 token = cp_lexer_peek_token (parser->lexer);
24221 /* See if it's a cv-qualifier. */
24222 switch (token->keyword)
24224 case RID_CONST:
24225 cv_qualifier = TYPE_QUAL_CONST;
24226 break;
24228 case RID_VOLATILE:
24229 cv_qualifier = TYPE_QUAL_VOLATILE;
24230 break;
24232 case RID_RESTRICT:
24233 cv_qualifier = TYPE_QUAL_RESTRICT;
24234 break;
24236 default:
24237 cv_qualifier = TYPE_UNQUALIFIED;
24238 break;
24241 if (!cv_qualifier)
24242 break;
24244 if (cv_quals & cv_qualifier)
24246 gcc_rich_location richloc (token->location);
24247 richloc.add_fixit_remove ();
24248 error_at (&richloc, "duplicate cv-qualifier");
24249 cp_lexer_purge_token (parser->lexer);
24251 else
24253 cp_lexer_consume_token (parser->lexer);
24254 cv_quals |= cv_qualifier;
24258 return cv_quals;
24261 /* Parse an (optional) ref-qualifier
24263 ref-qualifier:
24267 Returns cp_ref_qualifier representing ref-qualifier. */
24269 static cp_ref_qualifier
24270 cp_parser_ref_qualifier_opt (cp_parser* parser)
24272 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
24274 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
24275 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
24276 return ref_qual;
24278 while (true)
24280 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
24281 cp_token *token = cp_lexer_peek_token (parser->lexer);
24283 switch (token->type)
24285 case CPP_AND:
24286 curr_ref_qual = REF_QUAL_LVALUE;
24287 break;
24289 case CPP_AND_AND:
24290 curr_ref_qual = REF_QUAL_RVALUE;
24291 break;
24293 default:
24294 curr_ref_qual = REF_QUAL_NONE;
24295 break;
24298 if (!curr_ref_qual)
24299 break;
24300 else if (ref_qual)
24302 error_at (token->location, "multiple ref-qualifiers");
24303 cp_lexer_purge_token (parser->lexer);
24305 else
24307 ref_qual = curr_ref_qual;
24308 cp_lexer_consume_token (parser->lexer);
24312 return ref_qual;
24315 /* Parse an optional tx-qualifier.
24317 tx-qualifier:
24318 transaction_safe
24319 transaction_safe_dynamic */
24321 static tree
24322 cp_parser_tx_qualifier_opt (cp_parser *parser)
24324 cp_token *token = cp_lexer_peek_token (parser->lexer);
24325 if (token->type == CPP_NAME)
24327 tree name = token->u.value;
24328 const char *p = IDENTIFIER_POINTER (name);
24329 const int len = strlen ("transaction_safe");
24330 if (startswith (p, "transaction_safe"))
24332 p += len;
24333 if (*p == '\0'
24334 || !strcmp (p, "_dynamic"))
24336 cp_lexer_consume_token (parser->lexer);
24337 if (!flag_tm)
24339 error ("%qE requires %<-fgnu-tm%>", name);
24340 return NULL_TREE;
24342 else
24343 return name;
24347 return NULL_TREE;
24350 /* Parse an (optional) virt-specifier-seq.
24352 virt-specifier-seq:
24353 virt-specifier virt-specifier-seq [opt]
24355 virt-specifier:
24356 override
24357 final
24359 Returns a bitmask representing the virt-specifiers. */
24361 static cp_virt_specifiers
24362 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
24364 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
24366 while (true)
24368 cp_token *token;
24369 cp_virt_specifiers virt_specifier;
24371 /* Peek at the next token. */
24372 token = cp_lexer_peek_token (parser->lexer);
24373 /* See if it's a virt-specifier-qualifier. */
24374 if (token->type != CPP_NAME)
24375 break;
24376 if (id_equal (token->u.value, "override"))
24378 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
24379 virt_specifier = VIRT_SPEC_OVERRIDE;
24381 else if (id_equal (token->u.value, "final"))
24383 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
24384 virt_specifier = VIRT_SPEC_FINAL;
24386 else if (id_equal (token->u.value, "__final"))
24388 virt_specifier = VIRT_SPEC_FINAL;
24390 else
24391 break;
24393 if (virt_specifiers & virt_specifier)
24395 gcc_rich_location richloc (token->location);
24396 richloc.add_fixit_remove ();
24397 error_at (&richloc, "duplicate virt-specifier");
24398 cp_lexer_purge_token (parser->lexer);
24400 else
24402 cp_lexer_consume_token (parser->lexer);
24403 virt_specifiers |= virt_specifier;
24406 return virt_specifiers;
24409 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
24410 is in scope even though it isn't real. */
24412 void
24413 inject_this_parameter (tree ctype, cp_cv_quals quals)
24415 tree this_parm;
24417 if (current_class_ptr)
24419 /* We don't clear this between NSDMIs. Is it already what we want? */
24420 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
24421 if (DECL_P (current_class_ptr)
24422 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
24423 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
24424 && cp_type_quals (type) == quals)
24425 return;
24428 this_parm = build_this_parm (NULL_TREE, ctype, quals);
24429 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
24430 current_class_ptr = NULL_TREE;
24431 current_class_ref
24432 = cp_build_fold_indirect_ref (this_parm);
24433 current_class_ptr = this_parm;
24436 /* Return true iff our current scope is a non-static data member
24437 initializer. */
24439 bool
24440 parsing_nsdmi (void)
24442 /* We recognize NSDMI context by the context-less 'this' pointer set up
24443 by the function above. */
24444 if (current_class_ptr
24445 && TREE_CODE (current_class_ptr) == PARM_DECL
24446 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
24447 return true;
24448 return false;
24451 /* True if we're parsing a function declarator. */
24453 bool
24454 parsing_function_declarator ()
24456 /* this_entity is NULL for a function parameter scope while parsing the
24457 declarator; it is set when parsing the body of the function. */
24458 return (current_binding_level->kind == sk_function_parms
24459 && !current_binding_level->this_entity);
24462 /* Parse a late-specified return type, if any. This is not a separate
24463 non-terminal, but part of a function declarator, which looks like
24465 -> trailing-type-specifier-seq abstract-declarator(opt)
24467 Returns the type indicated by the type-id.
24469 In addition to this, parse any queued up #pragma omp declare simd
24470 clauses, and #pragma acc routine clauses.
24472 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
24473 function. */
24475 static tree
24476 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
24477 tree& requires_clause)
24479 cp_token *token;
24480 tree type = NULL_TREE;
24481 bool declare_simd_p = (parser->omp_declare_simd
24482 && declarator
24483 && declarator->kind == cdk_id);
24485 bool oacc_routine_p = (parser->oacc_routine
24486 && declarator
24487 && declarator->kind == cdk_id);
24489 /* Peek at the next token. */
24490 token = cp_lexer_peek_token (parser->lexer);
24491 /* A late-specified return type is indicated by an initial '->'. */
24492 if (token->type != CPP_DEREF
24493 && token->keyword != RID_REQUIRES
24494 && !(token->type == CPP_NAME
24495 && token->u.value == ridpointers[RID_REQUIRES])
24496 && !(declare_simd_p || oacc_routine_p))
24497 return NULL_TREE;
24499 if (token->type == CPP_DEREF)
24501 /* Consume the ->. */
24502 cp_lexer_consume_token (parser->lexer);
24504 type = cp_parser_trailing_type_id (parser);
24507 /* Function declarations may be followed by a trailing
24508 requires-clause. */
24509 requires_clause = cp_parser_requires_clause_opt (parser, false);
24511 if (declare_simd_p)
24512 declarator->attributes
24513 = cp_parser_late_parsing_omp_declare_simd (parser,
24514 declarator->attributes);
24515 if (oacc_routine_p)
24516 declarator->attributes
24517 = cp_parser_late_parsing_oacc_routine (parser,
24518 declarator->attributes);
24520 return type;
24523 /* Parse a declarator-id.
24525 declarator-id:
24526 id-expression
24527 :: [opt] nested-name-specifier [opt] type-name
24529 In the `id-expression' case, the value returned is as for
24530 cp_parser_id_expression if the id-expression was an unqualified-id.
24531 If the id-expression was a qualified-id, then a SCOPE_REF is
24532 returned. The first operand is the scope (either a NAMESPACE_DECL
24533 or TREE_TYPE), but the second is still just a representation of an
24534 unqualified-id. */
24536 static tree
24537 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
24539 tree id;
24540 /* The expression must be an id-expression. Assume that qualified
24541 names are the names of types so that:
24543 template <class T>
24544 int S<T>::R::i = 3;
24546 will work; we must treat `S<T>::R' as the name of a type.
24547 Similarly, assume that qualified names are templates, where
24548 required, so that:
24550 template <class T>
24551 int S<T>::R<T>::i = 3;
24553 will work, too. */
24554 id = cp_parser_id_expression (parser,
24555 /*template_keyword_p=*/false,
24556 /*check_dependency_p=*/false,
24557 /*template_p=*/NULL,
24558 /*declarator_p=*/true,
24559 optional_p);
24560 if (id && BASELINK_P (id))
24561 id = BASELINK_FUNCTIONS (id);
24562 return id;
24565 /* Parse a type-id.
24567 type-id:
24568 type-specifier-seq abstract-declarator [opt]
24570 The parser flags FLAGS is used to control type-specifier parsing.
24572 If IS_TEMPLATE_ARG is true, we are parsing a template argument.
24574 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
24575 i.e. we've just seen "->".
24577 Returns the TYPE specified. */
24579 static tree
24580 cp_parser_type_id_1 (cp_parser *parser, cp_parser_flags flags,
24581 bool is_template_arg, bool is_trailing_return,
24582 location_t *type_location)
24584 cp_decl_specifier_seq type_specifier_seq;
24585 cp_declarator *abstract_declarator;
24587 /* Parse the type-specifier-seq. */
24588 cp_parser_type_specifier_seq (parser, flags,
24589 /*is_declaration=*/false,
24590 is_trailing_return,
24591 &type_specifier_seq);
24592 if (type_location)
24593 *type_location = type_specifier_seq.locations[ds_type_spec];
24595 if (is_template_arg && type_specifier_seq.type
24596 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
24597 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
24598 /* A bare template name as a template argument is a template template
24599 argument, not a placeholder, so fail parsing it as a type argument. */
24601 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
24602 cp_parser_simulate_error (parser);
24603 return error_mark_node;
24605 if (type_specifier_seq.type == error_mark_node)
24606 return error_mark_node;
24608 /* There might or might not be an abstract declarator. */
24609 cp_parser_parse_tentatively (parser);
24610 /* Look for the declarator. */
24611 abstract_declarator
24612 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT,
24613 CP_PARSER_FLAGS_NONE, NULL,
24614 /*parenthesized_p=*/NULL,
24615 /*member_p=*/false,
24616 /*friend_p=*/false,
24617 /*static_p=*/false);
24618 /* Check to see if there really was a declarator. */
24619 if (!cp_parser_parse_definitely (parser))
24620 abstract_declarator = NULL;
24622 bool auto_typeid_ok = false;
24623 /* The concepts TS allows 'auto' as a type-id. */
24624 if (flag_concepts_ts)
24625 auto_typeid_ok = !parser->in_type_id_in_expr_p;
24626 /* DR 625 prohibits use of auto as a template-argument. We allow 'auto'
24627 outside the template-argument-list context here only for the sake of
24628 diagnostic: grokdeclarator then can emit a better error message for
24629 e.g. using T = auto. */
24630 else if (flag_concepts)
24631 auto_typeid_ok = (!parser->in_type_id_in_expr_p
24632 && !parser->in_template_argument_list_p);
24634 if (type_specifier_seq.type
24635 && !auto_typeid_ok
24636 /* None of the valid uses of 'auto' in C++14 involve the type-id
24637 nonterminal, but it is valid in a trailing-return-type. */
24638 && !(cxx_dialect >= cxx14 && is_trailing_return))
24639 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
24641 /* A type-id with type 'auto' is only ok if the abstract declarator
24642 is a function declarator with a late-specified return type.
24644 A type-id with 'auto' is also valid in a trailing-return-type
24645 in a compound-requirement. */
24646 if (abstract_declarator
24647 && abstract_declarator->kind == cdk_function
24648 && abstract_declarator->u.function.late_return_type)
24649 /* OK */;
24650 else if (parser->in_result_type_constraint_p)
24651 /* OK */;
24652 else
24654 if (!cp_parser_simulate_error (parser))
24656 location_t loc = type_specifier_seq.locations[ds_type_spec];
24657 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
24659 auto_diagnostic_group g;
24660 gcc_rich_location richloc (loc);
24661 richloc.add_fixit_insert_after ("<>");
24662 error_at (&richloc, "missing template arguments after %qE",
24663 tmpl);
24664 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
24665 tmpl);
24667 else if (parser->in_template_argument_list_p)
24668 error_at (loc, "%qT not permitted in template argument",
24669 auto_node);
24670 else
24671 error_at (loc, "invalid use of %qT", auto_node);
24673 return error_mark_node;
24677 return groktypename (&type_specifier_seq, abstract_declarator,
24678 is_template_arg);
24681 /* Wrapper for cp_parser_type_id_1. */
24683 static tree
24684 cp_parser_type_id (cp_parser *parser, cp_parser_flags flags,
24685 location_t *type_location)
24687 return cp_parser_type_id_1 (parser, flags, false, false, type_location);
24690 /* Wrapper for cp_parser_type_id_1. */
24692 static tree
24693 cp_parser_template_type_arg (cp_parser *parser)
24695 tree r;
24696 const char *saved_message = parser->type_definition_forbidden_message;
24697 parser->type_definition_forbidden_message
24698 = G_("types may not be defined in template arguments");
24699 r = cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_NONE, true, false, NULL);
24700 parser->type_definition_forbidden_message = saved_message;
24701 return r;
24704 /* Wrapper for cp_parser_type_id_1. */
24706 static tree
24707 cp_parser_trailing_type_id (cp_parser *parser)
24709 return cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
24710 false, true, NULL);
24713 /* Parse a type-specifier-seq.
24715 type-specifier-seq:
24716 type-specifier type-specifier-seq [opt]
24718 GNU extension:
24720 type-specifier-seq:
24721 attributes type-specifier-seq [opt]
24723 The parser flags FLAGS is used to control type-specifier parsing.
24725 If IS_DECLARATION is true, we are at the start of a "condition" or
24726 exception-declaration, so we might be followed by a declarator-id.
24728 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
24729 i.e. we've just seen "->".
24731 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
24733 static void
24734 cp_parser_type_specifier_seq (cp_parser* parser,
24735 cp_parser_flags flags,
24736 bool is_declaration,
24737 bool is_trailing_return,
24738 cp_decl_specifier_seq *type_specifier_seq)
24740 bool seen_type_specifier = false;
24741 cp_token *start_token = NULL;
24743 /* Clear the TYPE_SPECIFIER_SEQ. */
24744 clear_decl_specs (type_specifier_seq);
24746 flags |= CP_PARSER_FLAGS_OPTIONAL;
24747 /* In the context of a trailing return type, enum E { } is an
24748 elaborated-type-specifier followed by a function-body, not an
24749 enum-specifier. */
24750 if (is_trailing_return)
24751 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
24753 /* Parse the type-specifiers and attributes. */
24754 while (true)
24756 tree type_specifier;
24757 bool is_cv_qualifier;
24759 /* Check for attributes first. */
24760 if (cp_next_tokens_can_be_attribute_p (parser))
24762 /* GNU attributes at the end of a declaration apply to the
24763 declaration as a whole, not to the trailing return type. So look
24764 ahead to see if these attributes are at the end. */
24765 if (seen_type_specifier && is_trailing_return
24766 && cp_next_tokens_can_be_gnu_attribute_p (parser))
24768 size_t n = cp_parser_skip_attributes_opt (parser, 1);
24769 cp_token *tok = cp_lexer_peek_nth_token (parser->lexer, n);
24770 if (tok->type == CPP_SEMICOLON || tok->type == CPP_COMMA
24771 || tok->type == CPP_EQ || tok->type == CPP_OPEN_BRACE)
24772 break;
24774 type_specifier_seq->attributes
24775 = attr_chainon (type_specifier_seq->attributes,
24776 cp_parser_attributes_opt (parser));
24777 continue;
24780 /* record the token of the beginning of the type specifier seq,
24781 for error reporting purposes*/
24782 if (!start_token)
24783 start_token = cp_lexer_peek_token (parser->lexer);
24785 /* Look for the type-specifier. */
24786 type_specifier = cp_parser_type_specifier (parser,
24787 flags,
24788 type_specifier_seq,
24789 /*is_declaration=*/false,
24790 NULL,
24791 &is_cv_qualifier);
24792 if (!type_specifier)
24794 /* If the first type-specifier could not be found, this is not a
24795 type-specifier-seq at all. */
24796 if (!seen_type_specifier)
24798 /* Set in_declarator_p to avoid skipping to the semicolon. */
24799 int in_decl = parser->in_declarator_p;
24800 parser->in_declarator_p = true;
24802 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
24803 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
24804 cp_parser_error (parser, "expected type-specifier");
24806 parser->in_declarator_p = in_decl;
24808 type_specifier_seq->type = error_mark_node;
24809 return;
24811 /* If subsequent type-specifiers could not be found, the
24812 type-specifier-seq is complete. */
24813 break;
24816 seen_type_specifier = true;
24817 /* The standard says that a condition can be:
24819 type-specifier-seq declarator = assignment-expression
24821 However, given:
24823 struct S {};
24824 if (int S = ...)
24826 we should treat the "S" as a declarator, not as a
24827 type-specifier. The standard doesn't say that explicitly for
24828 type-specifier-seq, but it does say that for
24829 decl-specifier-seq in an ordinary declaration. Perhaps it
24830 would be clearer just to allow a decl-specifier-seq here, and
24831 then add a semantic restriction that if any decl-specifiers
24832 that are not type-specifiers appear, the program is invalid. */
24833 if (is_declaration && !is_cv_qualifier)
24834 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
24838 /* Return whether the function currently being declared has an associated
24839 template parameter list. */
24841 static bool
24842 function_being_declared_is_template_p (cp_parser* parser)
24844 if (!current_template_parms || processing_template_parmlist)
24845 return false;
24847 if (parser->implicit_template_scope)
24848 return true;
24850 if (at_class_scope_p ()
24851 && TYPE_BEING_DEFINED (current_class_type))
24852 return parser->num_template_parameter_lists != 0;
24854 return ((int) parser->num_template_parameter_lists > template_class_depth
24855 (current_class_type));
24858 /* Parse a parameter-declaration-clause.
24860 parameter-declaration-clause:
24861 parameter-declaration-list [opt] ... [opt]
24862 parameter-declaration-list , ...
24864 The parser flags FLAGS is used to control type-specifier parsing.
24866 Returns a representation for the parameter declarations. A return
24867 value of NULL indicates a parameter-declaration-clause consisting
24868 only of an ellipsis. */
24870 static tree
24871 cp_parser_parameter_declaration_clause (cp_parser* parser,
24872 cp_parser_flags flags)
24874 tree parameters;
24875 cp_token *token;
24876 bool ellipsis_p;
24878 auto cleanup = make_temp_override
24879 (parser->auto_is_implicit_function_template_parm_p);
24881 if (!processing_specialization
24882 && !processing_template_parmlist
24883 && !processing_explicit_instantiation
24884 /* default_arg_ok_p tracks whether this is a parameter-clause for an
24885 actual function or a random abstract declarator. */
24886 && parser->default_arg_ok_p)
24887 if (!current_function_decl
24888 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
24889 parser->auto_is_implicit_function_template_parm_p = true;
24891 /* Peek at the next token. */
24892 token = cp_lexer_peek_token (parser->lexer);
24893 /* Check for trivial parameter-declaration-clauses. */
24894 if (token->type == CPP_ELLIPSIS)
24896 /* Consume the `...' token. */
24897 cp_lexer_consume_token (parser->lexer);
24898 return NULL_TREE;
24900 else if (token->type == CPP_CLOSE_PAREN)
24901 /* There are no parameters. */
24902 return void_list_node;
24903 /* Check for `(void)', too, which is a special case. */
24904 else if (token->keyword == RID_VOID
24905 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
24906 == CPP_CLOSE_PAREN))
24908 /* Consume the `void' token. */
24909 cp_lexer_consume_token (parser->lexer);
24910 /* There are no parameters. */
24911 return explicit_void_list_node;
24914 /* A vector of parameters that haven't been pushed yet. */
24915 auto_vec<tree> pending_decls;
24917 /* Parse the parameter-declaration-list. */
24918 parameters = cp_parser_parameter_declaration_list (parser, flags,
24919 &pending_decls);
24920 /* If a parse error occurred while parsing the
24921 parameter-declaration-list, then the entire
24922 parameter-declaration-clause is erroneous. */
24923 if (parameters == error_mark_node)
24924 return NULL_TREE;
24926 /* Peek at the next token. */
24927 token = cp_lexer_peek_token (parser->lexer);
24928 /* If it's a `,', the clause should terminate with an ellipsis. */
24929 if (token->type == CPP_COMMA)
24931 /* Consume the `,'. */
24932 cp_lexer_consume_token (parser->lexer);
24933 /* Expect an ellipsis. */
24934 ellipsis_p
24935 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
24937 /* It might also be `...' if the optional trailing `,' was
24938 omitted. */
24939 else if (token->type == CPP_ELLIPSIS)
24941 /* Consume the `...' token. */
24942 cp_lexer_consume_token (parser->lexer);
24943 /* And remember that we saw it. */
24944 ellipsis_p = true;
24946 else
24947 ellipsis_p = false;
24949 /* A valid parameter-declaration-clause can only be followed by a ')'.
24950 So it's time to push all the parameters we have seen now that we
24951 know we have a valid declaration. Note that here we may not have
24952 committed yet, nor should we. Pushing here will detect the error
24953 of redefining a parameter. */
24954 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24955 for (tree p : pending_decls)
24956 pushdecl (p);
24958 /* Finish the parameter list. */
24959 if (!ellipsis_p)
24960 parameters = chainon (parameters, void_list_node);
24962 return parameters;
24965 /* Parse a parameter-declaration-list.
24967 parameter-declaration-list:
24968 parameter-declaration
24969 parameter-declaration-list , parameter-declaration
24971 The parser flags FLAGS is used to control type-specifier parsing.
24972 PENDING_DECLS is a vector of parameters that haven't been pushed yet.
24974 Returns a representation of the parameter-declaration-list, as for
24975 cp_parser_parameter_declaration_clause. However, the
24976 `void_list_node' is never appended to the list. */
24978 static tree
24979 cp_parser_parameter_declaration_list (cp_parser* parser,
24980 cp_parser_flags flags,
24981 auto_vec<tree> *pending_decls)
24983 tree parameters = NULL_TREE;
24984 tree *tail = &parameters;
24985 bool saved_in_unbraced_linkage_specification_p;
24986 int index = 0;
24988 /* The special considerations that apply to a function within an
24989 unbraced linkage specifications do not apply to the parameters
24990 to the function. */
24991 saved_in_unbraced_linkage_specification_p
24992 = parser->in_unbraced_linkage_specification_p;
24993 parser->in_unbraced_linkage_specification_p = false;
24995 /* Look for more parameters. */
24996 while (true)
24998 cp_parameter_declarator *parameter;
24999 tree decl = error_mark_node;
25000 bool parenthesized_p = false;
25002 /* Parse the parameter. */
25003 parameter
25004 = cp_parser_parameter_declaration (parser, flags,
25005 /*template_parm_p=*/false,
25006 &parenthesized_p);
25008 /* We don't know yet if the enclosing context is unavailable or deprecated,
25009 so wait and deal with it in grokparms if appropriate. */
25010 deprecated_state = UNAVAILABLE_DEPRECATED_SUPPRESS;
25012 if (parameter && !cp_parser_error_occurred (parser))
25014 decl = grokdeclarator (parameter->declarator,
25015 &parameter->decl_specifiers,
25016 PARM,
25017 parameter->default_argument != NULL_TREE,
25018 &parameter->decl_specifiers.attributes);
25019 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
25020 DECL_SOURCE_LOCATION (decl) = parameter->loc;
25023 deprecated_state = DEPRECATED_NORMAL;
25025 /* If a parse error occurred parsing the parameter declaration,
25026 then the entire parameter-declaration-list is erroneous. */
25027 if (decl == error_mark_node)
25029 parameters = error_mark_node;
25030 break;
25033 if (parameter->decl_specifiers.attributes)
25034 cplus_decl_attributes (&decl,
25035 parameter->decl_specifiers.attributes,
25037 if (DECL_NAME (decl))
25039 /* We cannot always pushdecl while parsing tentatively because
25040 it may have side effects and we can't be sure yet if we're
25041 parsing a declaration, e.g.:
25043 S foo(int(x), int(x), int{x});
25045 where it's not clear if we're dealing with a constructor call
25046 or a function declaration until we've seen the last argument
25047 which breaks it up.
25048 It's safe to pushdecl so long as it doesn't result in a clash
25049 with an already-pushed parameter. But we don't delay pushing
25050 different parameters to handle
25052 S foo(int(i), decltype(i) j = 42);
25054 which is valid. */
25055 if (pending_decls
25056 && cp_parser_uncommitted_to_tentative_parse_p (parser)
25057 /* See if PARAMETERS already contains a parameter with the same
25058 DECL_NAME as DECL. */
25059 && [parameters, decl] {
25060 for (tree p = parameters; p; p = TREE_CHAIN (p))
25061 if (DECL_NAME (decl) == DECL_NAME (TREE_VALUE (p)))
25062 return true;
25063 return false;
25064 }())
25065 pending_decls->safe_push (decl);
25066 else
25067 decl = pushdecl (decl);
25070 if (decl != error_mark_node)
25072 retrofit_lang_decl (decl);
25073 DECL_PARM_INDEX (decl) = ++index;
25074 DECL_PARM_LEVEL (decl) = function_parm_depth ();
25077 /* Add the new parameter to the list. */
25078 *tail = build_tree_list (parameter->default_argument, decl);
25079 tail = &TREE_CHAIN (*tail);
25081 /* If the parameters were parenthesized, it's the case of
25082 T foo(X(x)) which looks like a variable definition but
25083 is a function declaration. */
25084 if (index == 1 || PARENTHESIZED_LIST_P (parameters))
25085 PARENTHESIZED_LIST_P (parameters) = parenthesized_p;
25087 /* Peek at the next token. */
25088 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
25089 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
25090 /* These are for Objective-C++ */
25091 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
25092 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25093 /* The parameter-declaration-list is complete. */
25094 break;
25095 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25097 cp_token *token;
25099 /* Peek at the next token. */
25100 token = cp_lexer_peek_nth_token (parser->lexer, 2);
25101 /* If it's an ellipsis, then the list is complete. */
25102 if (token->type == CPP_ELLIPSIS)
25103 break;
25104 /* Otherwise, there must be more parameters. Consume the
25105 `,'. */
25106 cp_lexer_consume_token (parser->lexer);
25107 /* When parsing something like:
25109 int i(float f, double d)
25111 we can tell after seeing the declaration for "f" that we
25112 are not looking at an initialization of a variable "i",
25113 but rather at the declaration of a function "i".
25115 Due to the fact that the parsing of template arguments
25116 (as specified to a template-id) requires backtracking we
25117 cannot use this technique when inside a template argument
25118 list. */
25119 if (!parser->in_template_argument_list_p
25120 && !parser->in_type_id_in_expr_p
25121 && cp_parser_uncommitted_to_tentative_parse_p (parser)
25122 /* However, a parameter-declaration of the form
25123 "float(f)" (which is a valid declaration of a
25124 parameter "f") can also be interpreted as an
25125 expression (the conversion of "f" to "float"). */
25126 && !parenthesized_p)
25127 cp_parser_commit_to_tentative_parse (parser);
25129 else
25131 cp_parser_error (parser, "expected %<,%> or %<...%>");
25132 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
25133 cp_parser_skip_to_closing_parenthesis (parser,
25134 /*recovering=*/true,
25135 /*or_comma=*/false,
25136 /*consume_paren=*/false);
25137 break;
25141 parser->in_unbraced_linkage_specification_p
25142 = saved_in_unbraced_linkage_specification_p;
25144 /* Reset implicit_template_scope if we are about to leave the function
25145 parameter list that introduced it. Note that for out-of-line member
25146 definitions, there will be one or more class scopes before we get to
25147 the template parameter scope. */
25149 if (cp_binding_level *its = parser->implicit_template_scope)
25150 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
25152 while (maybe_its->kind == sk_class)
25153 maybe_its = maybe_its->level_chain;
25154 if (maybe_its == its)
25156 parser->implicit_template_parms = 0;
25157 parser->implicit_template_scope = 0;
25161 return parameters;
25164 /* Parse a parameter declaration.
25166 parameter-declaration:
25167 decl-specifier-seq ... [opt] declarator
25168 decl-specifier-seq declarator = assignment-expression
25169 decl-specifier-seq ... [opt] abstract-declarator [opt]
25170 decl-specifier-seq abstract-declarator [opt] = assignment-expression
25172 The parser flags FLAGS is used to control type-specifier parsing.
25174 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
25175 declares a template parameter. (In that case, a non-nested `>'
25176 token encountered during the parsing of the assignment-expression
25177 is not interpreted as a greater-than operator.)
25179 Returns a representation of the parameter, or NULL if an error
25180 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
25181 true iff the declarator is of the form "(p)". */
25183 static cp_parameter_declarator *
25184 cp_parser_parameter_declaration (cp_parser *parser,
25185 cp_parser_flags flags,
25186 bool template_parm_p,
25187 bool *parenthesized_p)
25189 int declares_class_or_enum;
25190 cp_decl_specifier_seq decl_specifiers;
25191 cp_declarator *declarator;
25192 tree default_argument;
25193 cp_token *token = NULL, *declarator_token_start = NULL;
25194 const char *saved_message;
25195 bool template_parameter_pack_p = false;
25197 /* In a template parameter, `>' is not an operator.
25199 [temp.param]
25201 When parsing a default template-argument for a non-type
25202 template-parameter, the first non-nested `>' is taken as the end
25203 of the template parameter-list rather than a greater-than
25204 operator. */
25206 /* Type definitions may not appear in parameter types. */
25207 saved_message = parser->type_definition_forbidden_message;
25208 parser->type_definition_forbidden_message
25209 = G_("types may not be defined in parameter types");
25211 int template_parm_idx = (function_being_declared_is_template_p (parser) ?
25212 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
25213 (current_template_parms)) : 0);
25215 /* Parse the declaration-specifiers. */
25216 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
25217 cp_parser_decl_specifier_seq (parser,
25218 flags,
25219 &decl_specifiers,
25220 &declares_class_or_enum);
25222 /* [dcl.spec.auto.general]: "A placeholder-type-specifier of the form
25223 type-constraint opt auto can be used as a decl-specifier of the
25224 decl-specifier-seq of a parameter-declaration of a function declaration
25225 or lambda-expression..." but we must not synthesize an implicit template
25226 type parameter in its declarator. That is, in "void f(auto[auto{10}]);"
25227 we want to synthesize only the first auto. */
25228 auto cleanup = make_temp_override
25229 (parser->auto_is_implicit_function_template_parm_p, false);
25231 /* Complain about missing 'typename' or other invalid type names. */
25232 if (!decl_specifiers.any_type_specifiers_p
25233 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
25234 decl_specifiers.type = error_mark_node;
25236 /* If an error occurred, there's no reason to attempt to parse the
25237 rest of the declaration. */
25238 if (cp_parser_error_occurred (parser))
25240 parser->type_definition_forbidden_message = saved_message;
25241 return NULL;
25244 /* Peek at the next token. */
25245 token = cp_lexer_peek_token (parser->lexer);
25247 /* If the next token is a `)', `,', `=', `>', or `...', then there
25248 is no declarator. However, when variadic templates are enabled,
25249 there may be a declarator following `...'. */
25250 if (token->type == CPP_CLOSE_PAREN
25251 || token->type == CPP_COMMA
25252 || token->type == CPP_EQ
25253 || token->type == CPP_GREATER)
25255 declarator = NULL;
25256 if (parenthesized_p)
25257 *parenthesized_p = false;
25259 /* Otherwise, there should be a declarator. */
25260 else
25262 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
25263 parser->default_arg_ok_p = false;
25265 /* After seeing a decl-specifier-seq, if the next token is not a
25266 "(" or "{", there is no possibility that the code is a valid
25267 expression. Therefore, if parsing tentatively, we commit at
25268 this point. */
25269 if (!parser->in_template_argument_list_p
25270 /* In an expression context, having seen:
25272 (int((char ...
25274 we cannot be sure whether we are looking at a
25275 function-type (taking a "char" as a parameter) or a cast
25276 of some object of type "char" to "int". */
25277 && !parser->in_type_id_in_expr_p
25278 && cp_parser_uncommitted_to_tentative_parse_p (parser)
25279 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
25281 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25283 if (decl_specifiers.type
25284 && template_placeholder_p (decl_specifiers.type))
25285 /* This is a CTAD expression, not a parameter declaration. */
25286 cp_parser_simulate_error (parser);
25288 else
25289 cp_parser_commit_to_tentative_parse (parser);
25291 /* Parse the declarator. */
25292 declarator_token_start = token;
25293 declarator = cp_parser_declarator (parser,
25294 CP_PARSER_DECLARATOR_EITHER,
25295 CP_PARSER_FLAGS_NONE,
25296 /*ctor_dtor_or_conv_p=*/NULL,
25297 parenthesized_p,
25298 /*member_p=*/false,
25299 /*friend_p=*/false,
25300 /*static_p=*/false);
25301 parser->default_arg_ok_p = saved_default_arg_ok_p;
25302 /* After the declarator, allow more attributes. */
25303 decl_specifiers.attributes
25304 = attr_chainon (decl_specifiers.attributes,
25305 cp_parser_attributes_opt (parser));
25307 /* If the declarator is a template parameter pack, remember that and
25308 clear the flag in the declarator itself so we don't get errors
25309 from grokdeclarator. */
25310 if (template_parm_p && declarator && declarator->parameter_pack_p)
25312 declarator->parameter_pack_p = false;
25313 template_parameter_pack_p = true;
25317 /* If the next token is an ellipsis, and we have not seen a declarator
25318 name, and if either the type of the declarator contains parameter
25319 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
25320 for, eg, abbreviated integral type names), then we actually have a
25321 parameter pack expansion expression. Otherwise, leave the ellipsis
25322 for a C-style variadic function. */
25323 token = cp_lexer_peek_token (parser->lexer);
25325 /* If a function parameter pack was specified and an implicit template
25326 parameter was introduced during cp_parser_parameter_declaration,
25327 change any implicit parameters introduced into packs. */
25328 if (parser->implicit_template_parms
25329 && ((token->type == CPP_ELLIPSIS
25330 && declarator_can_be_parameter_pack (declarator))
25331 || (declarator && declarator->parameter_pack_p)))
25333 int latest_template_parm_idx = TREE_VEC_LENGTH
25334 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
25336 if (latest_template_parm_idx != template_parm_idx)
25337 decl_specifiers.type = convert_generic_types_to_packs
25338 (decl_specifiers.type,
25339 template_parm_idx, latest_template_parm_idx);
25342 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25344 tree type = decl_specifiers.type;
25346 if (type && DECL_P (type))
25347 type = TREE_TYPE (type);
25349 if (((type
25350 && TREE_CODE (type) != TYPE_PACK_EXPANSION
25351 && (template_parm_p || uses_parameter_packs (type)))
25352 || (!type && template_parm_p))
25353 && declarator_can_be_parameter_pack (declarator))
25355 /* Consume the `...'. */
25356 cp_lexer_consume_token (parser->lexer);
25357 maybe_warn_variadic_templates ();
25359 /* Build a pack expansion type */
25360 if (template_parm_p)
25361 template_parameter_pack_p = true;
25362 else if (declarator)
25363 declarator->parameter_pack_p = true;
25364 else
25365 decl_specifiers.type = make_pack_expansion (type);
25369 /* The restriction on defining new types applies only to the type
25370 of the parameter, not to the default argument. */
25371 parser->type_definition_forbidden_message = saved_message;
25373 /* If the next token is `=', then process a default argument. */
25374 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
25376 tree type = decl_specifiers.type;
25377 token = cp_lexer_peek_token (parser->lexer);
25378 if (declarator)
25379 declarator->init_loc = token->location;
25380 /* If we are defining a class, then the tokens that make up the
25381 default argument must be saved and processed later. */
25382 if (!template_parm_p && at_class_scope_p ()
25383 && TYPE_BEING_DEFINED (current_class_type)
25384 && !LAMBDA_TYPE_P (current_class_type))
25385 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
25387 /* A constrained-type-specifier may declare a type
25388 template-parameter. */
25389 else if (declares_constrained_type_template_parameter (type))
25390 default_argument
25391 = cp_parser_default_type_template_argument (parser);
25393 /* A constrained-type-specifier may declare a
25394 template-template-parameter. */
25395 else if (declares_constrained_template_template_parameter (type))
25396 default_argument
25397 = cp_parser_default_template_template_argument (parser);
25399 /* Outside of a class definition, we can just parse the
25400 assignment-expression. */
25401 else
25402 default_argument
25403 = cp_parser_default_argument (parser, template_parm_p);
25405 if (!parser->default_arg_ok_p)
25407 permerror (token->location,
25408 "default arguments are only "
25409 "permitted for function parameters");
25411 else if ((declarator && declarator->parameter_pack_p)
25412 || template_parameter_pack_p
25413 || (decl_specifiers.type
25414 && PACK_EXPANSION_P (decl_specifiers.type)))
25416 /* Find the name of the parameter pack. */
25417 cp_declarator *id_declarator = declarator;
25418 while (id_declarator && id_declarator->kind != cdk_id)
25419 id_declarator = id_declarator->declarator;
25421 if (id_declarator && id_declarator->kind == cdk_id)
25422 error_at (declarator_token_start->location,
25423 template_parm_p
25424 ? G_("template parameter pack %qD "
25425 "cannot have a default argument")
25426 : G_("parameter pack %qD cannot have "
25427 "a default argument"),
25428 id_declarator->u.id.unqualified_name);
25429 else
25430 error_at (declarator_token_start->location,
25431 template_parm_p
25432 ? G_("template parameter pack cannot have "
25433 "a default argument")
25434 : G_("parameter pack cannot have a "
25435 "default argument"));
25437 default_argument = NULL_TREE;
25440 else
25441 default_argument = NULL_TREE;
25443 if (default_argument)
25444 STRIP_ANY_LOCATION_WRAPPER (default_argument);
25446 /* Generate a location for the parameter, ranging from the start of the
25447 initial token to the end of the final token (using input_location for
25448 the latter, set up by cp_lexer_set_source_position_from_token when
25449 consuming tokens).
25451 If we have a identifier, then use it for the caret location, e.g.
25453 extern int callee (int one, int (*two)(int, int), float three);
25454 ~~~~~~^~~~~~~~~~~~~~
25456 otherwise, reuse the start location for the caret location e.g.:
25458 extern int callee (int one, int (*)(int, int), float three);
25459 ^~~~~~~~~~~~~~~~~
25462 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
25463 ? declarator->id_loc
25464 : decl_spec_token_start->location);
25465 location_t param_loc = make_location (caret_loc,
25466 decl_spec_token_start->location,
25467 input_location);
25469 return make_parameter_declarator (&decl_specifiers,
25470 declarator,
25471 default_argument,
25472 param_loc,
25473 template_parameter_pack_p);
25476 /* Parse a default argument and return it.
25478 TEMPLATE_PARM_P is true if this is a default argument for a
25479 non-type template parameter. */
25480 static tree
25481 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
25483 tree default_argument = NULL_TREE;
25484 bool saved_greater_than_is_operator_p;
25485 unsigned char saved_local_variables_forbidden_p;
25487 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
25488 set correctly. */
25489 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
25490 parser->greater_than_is_operator_p = !template_parm_p;
25491 auto odsd = make_temp_override (parser->omp_declare_simd, NULL);
25492 auto ord = make_temp_override (parser->oacc_routine, NULL);
25493 auto oafp = make_temp_override (parser->omp_attrs_forbidden_p, false);
25495 /* Local variable names (and the `this' keyword) may not
25496 appear in a default argument. */
25497 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
25498 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
25499 /* Parse the assignment-expression. */
25500 if (template_parm_p)
25501 push_deferring_access_checks (dk_no_deferred);
25502 tree saved_class_ptr = NULL_TREE;
25503 tree saved_class_ref = NULL_TREE;
25504 /* The "this" pointer is not valid in a default argument. */
25505 if (cfun)
25507 saved_class_ptr = current_class_ptr;
25508 cp_function_chain->x_current_class_ptr = NULL_TREE;
25509 saved_class_ref = current_class_ref;
25510 cp_function_chain->x_current_class_ref = NULL_TREE;
25512 default_argument = cp_parser_initializer (parser);
25513 /* Restore the "this" pointer. */
25514 if (cfun)
25516 cp_function_chain->x_current_class_ptr = saved_class_ptr;
25517 cp_function_chain->x_current_class_ref = saved_class_ref;
25519 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
25520 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
25521 if (template_parm_p)
25522 pop_deferring_access_checks ();
25523 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
25524 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
25526 return default_argument;
25529 /* Parse a function-body.
25531 function-body:
25532 compound_statement */
25534 static void
25535 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
25537 cp_parser_compound_statement (parser, NULL, (in_function_try_block
25538 ? BCS_TRY_BLOCK : BCS_NORMAL),
25539 true);
25542 /* Parse a ctor-initializer-opt followed by a function-body. Return
25543 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
25544 is true we are parsing a function-try-block. */
25546 static void
25547 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
25548 bool in_function_try_block)
25550 tree body, list;
25551 const bool check_body_p
25552 = (DECL_CONSTRUCTOR_P (current_function_decl)
25553 && DECL_DECLARED_CONSTEXPR_P (current_function_decl));
25554 tree last = NULL;
25556 if (in_function_try_block
25557 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
25558 && cxx_dialect < cxx20)
25560 if (DECL_CONSTRUCTOR_P (current_function_decl))
25561 pedwarn (input_location, OPT_Wc__20_extensions,
25562 "function-try-block body of %<constexpr%> constructor only "
25563 "available with %<-std=c++20%> or %<-std=gnu++20%>");
25564 else
25565 pedwarn (input_location, OPT_Wc__20_extensions,
25566 "function-try-block body of %<constexpr%> function only "
25567 "available with %<-std=c++20%> or %<-std=gnu++20%>");
25570 /* Begin the function body. */
25571 body = begin_function_body ();
25572 /* Parse the optional ctor-initializer. */
25573 cp_parser_ctor_initializer_opt (parser);
25575 /* If we're parsing a constexpr constructor definition, we need
25576 to check that the constructor body is indeed empty. However,
25577 before we get to cp_parser_function_body lot of junk has been
25578 generated, so we can't just check that we have an empty block.
25579 Rather we take a snapshot of the outermost block, and check whether
25580 cp_parser_function_body changed its state. */
25581 if (check_body_p)
25583 list = cur_stmt_list;
25584 if (STATEMENT_LIST_TAIL (list))
25585 last = STATEMENT_LIST_TAIL (list)->stmt;
25587 /* Parse the function-body. */
25588 cp_parser_function_body (parser, in_function_try_block);
25589 if (check_body_p)
25590 check_constexpr_ctor_body (last, list, /*complain=*/true);
25591 /* Finish the function body. */
25592 finish_function_body (body);
25595 /* Parse an initializer.
25597 initializer:
25598 = initializer-clause
25599 ( expression-list )
25601 Returns an expression representing the initializer. If no
25602 initializer is present, NULL_TREE is returned.
25604 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
25605 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
25606 set to TRUE if there is no initializer present. If there is an
25607 initializer, and it is not a constant-expression, *NON_CONSTANT_P
25608 is set to true; otherwise it is set to false. */
25610 static tree
25611 cp_parser_initializer (cp_parser *parser, bool *is_direct_init /*=nullptr*/,
25612 bool *non_constant_p /*=nullptr*/, bool subexpression_p)
25614 cp_token *token;
25615 tree init;
25617 /* Peek at the next token. */
25618 token = cp_lexer_peek_token (parser->lexer);
25620 /* Let our caller know whether or not this initializer was
25621 parenthesized. */
25622 if (is_direct_init)
25623 *is_direct_init = (token->type != CPP_EQ);
25624 /* Assume that the initializer is constant. */
25625 if (non_constant_p)
25626 *non_constant_p = false;
25628 if (token->type == CPP_EQ)
25630 /* Consume the `='. */
25631 cp_lexer_consume_token (parser->lexer);
25632 /* Parse the initializer-clause. */
25633 init = cp_parser_initializer_clause (parser, non_constant_p);
25635 else if (token->type == CPP_OPEN_PAREN)
25637 vec<tree, va_gc> *vec;
25638 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
25639 /*cast_p=*/false,
25640 /*allow_expansion_p=*/true,
25641 non_constant_p);
25642 if (vec == NULL)
25643 return error_mark_node;
25644 init = build_tree_list_vec (vec);
25645 release_tree_vector (vec);
25647 else if (token->type == CPP_OPEN_BRACE)
25649 cp_lexer_set_source_position (parser->lexer);
25650 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
25651 init = cp_parser_braced_list (parser, non_constant_p);
25652 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
25654 else
25656 /* Anything else is an error. */
25657 cp_parser_error (parser, "expected initializer");
25658 init = error_mark_node;
25661 if (!subexpression_p && check_for_bare_parameter_packs (init))
25662 init = error_mark_node;
25664 return init;
25667 /* Parse an initializer-clause.
25669 initializer-clause:
25670 assignment-expression
25671 braced-init-list
25673 Returns an expression representing the initializer.
25675 If the `assignment-expression' production is used the value
25676 returned is simply a representation for the expression.
25678 Otherwise, calls cp_parser_braced_list. */
25680 static cp_expr
25681 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
25683 cp_expr initializer;
25685 /* Assume the expression is constant. */
25686 if (non_constant_p)
25687 *non_constant_p = false;
25689 /* If it is not a `{', then we are looking at an
25690 assignment-expression. */
25691 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
25693 initializer
25694 = cp_parser_constant_expression (parser,
25695 /*allow_non_constant_p=*/2,
25696 non_constant_p);
25698 else
25699 initializer = cp_parser_braced_list (parser, non_constant_p);
25701 return initializer;
25704 /* Parse a brace-enclosed initializer list.
25706 braced-init-list:
25707 { initializer-list , [opt] }
25708 { designated-initializer-list , [opt] }
25711 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
25712 the elements of the initializer-list (or NULL, if the last
25713 production is used). The TREE_TYPE for the CONSTRUCTOR will be
25714 NULL_TREE. There is no way to detect whether or not the optional
25715 trailing `,' was provided. NON_CONSTANT_P is as for
25716 cp_parser_initializer. */
25718 static cp_expr
25719 cp_parser_braced_list (cp_parser *parser, bool *non_constant_p /*=nullptr*/)
25721 tree initializer;
25722 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
25724 /* Consume the `{' token. */
25725 matching_braces braces;
25726 braces.require_open (parser);
25727 /* Create a CONSTRUCTOR to represent the braced-initializer. */
25728 initializer = make_node (CONSTRUCTOR);
25729 /* If it's not a `}', then there is a non-trivial initializer. */
25730 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
25732 bool designated;
25733 /* Parse the initializer list. */
25734 CONSTRUCTOR_ELTS (initializer)
25735 = cp_parser_initializer_list (parser, non_constant_p, &designated);
25736 CONSTRUCTOR_IS_DESIGNATED_INIT (initializer) = designated;
25737 /* A trailing `,' token is allowed. */
25738 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25739 cp_lexer_consume_token (parser->lexer);
25741 else if (non_constant_p)
25742 *non_constant_p = false;
25743 /* Now, there should be a trailing `}'. */
25744 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
25745 braces.require_close (parser);
25746 TREE_TYPE (initializer) = init_list_type_node;
25747 recompute_constructor_flags (initializer);
25749 cp_expr result (initializer);
25750 /* Build a location of the form:
25751 { ... }
25752 ^~~~~~~
25753 with caret==start at the open brace, finish at the close brace. */
25754 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
25755 result.set_location (combined_loc);
25756 return result;
25759 /* Consume tokens up to, but not including, the next non-nested closing `]'.
25760 Returns true iff we found a closing `]'. */
25762 static bool
25763 cp_parser_skip_up_to_closing_square_bracket (cp_parser *parser)
25765 unsigned square_depth = 0;
25767 while (true)
25769 cp_token * token = cp_lexer_peek_token (parser->lexer);
25771 switch (token->type)
25773 case CPP_PRAGMA_EOL:
25774 if (!parser->lexer->in_pragma)
25775 break;
25776 /* FALLTHRU */
25778 case CPP_EOF:
25779 /* If we've run out of tokens, then there is no closing `]'. */
25780 return false;
25782 case CPP_OPEN_SQUARE:
25783 ++square_depth;
25784 break;
25786 case CPP_CLOSE_SQUARE:
25787 if (!square_depth--)
25788 return true;
25789 break;
25791 default:
25792 break;
25795 /* Consume the current token, skipping it. */
25796 cp_lexer_consume_token (parser->lexer);
25800 /* Consume tokens up to, and including, the next non-nested closing `]'.
25801 Returns true iff we found a closing `]'. */
25803 static bool
25804 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
25806 bool found = cp_parser_skip_up_to_closing_square_bracket (parser);
25807 if (found)
25808 cp_lexer_consume_token (parser->lexer);
25809 return found;
25812 /* Return true if we are looking at an array-designator, false otherwise. */
25814 static bool
25815 cp_parser_array_designator_p (cp_parser *parser)
25817 /* Consume the `['. */
25818 cp_lexer_consume_token (parser->lexer);
25820 cp_lexer_save_tokens (parser->lexer);
25822 /* Skip tokens until the next token is a closing square bracket.
25823 If we find the closing `]', and the next token is a `=', then
25824 we are looking at an array designator. */
25825 bool array_designator_p
25826 = (cp_parser_skip_to_closing_square_bracket (parser)
25827 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
25829 /* Roll back the tokens we skipped. */
25830 cp_lexer_rollback_tokens (parser->lexer);
25832 return array_designator_p;
25835 /* Parse an initializer-list.
25837 initializer-list:
25838 initializer-clause ... [opt]
25839 initializer-list , initializer-clause ... [opt]
25841 C++20 Extension:
25843 designated-initializer-list:
25844 designated-initializer-clause
25845 designated-initializer-list , designated-initializer-clause
25847 designated-initializer-clause:
25848 designator brace-or-equal-initializer
25850 designator:
25851 . identifier
25853 GNU Extension:
25855 initializer-list:
25856 designation initializer-clause ...[opt]
25857 initializer-list , designation initializer-clause ...[opt]
25859 designation:
25860 . identifier =
25861 identifier :
25862 [ constant-expression ] =
25864 Returns a vec of constructor_elt. The VALUE of each elt is an expression
25865 for the initializer. If the INDEX of the elt is non-NULL, it is the
25866 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
25867 as for cp_parser_initializer. Set *DESIGNATED to a boolean whether there
25868 are any designators. */
25870 static vec<constructor_elt, va_gc> *
25871 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p,
25872 bool *designated)
25874 vec<constructor_elt, va_gc> *v = NULL;
25875 bool first_p = true;
25876 tree first_designator = NULL_TREE;
25878 /* Assume all of the expressions are constant. */
25879 if (non_constant_p)
25880 *non_constant_p = false;
25882 unsigned nelts = 0;
25883 int suppress = suppress_location_wrappers;
25885 /* Parse the rest of the list. */
25886 while (true)
25888 cp_token *token;
25889 tree designator;
25890 tree initializer;
25891 bool clause_non_constant_p;
25892 bool direct_p = false;
25893 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25895 /* Handle the C++20 syntax, '. id ='. */
25896 if ((cxx_dialect >= cxx20
25897 || cp_parser_allow_gnu_extensions_p (parser))
25898 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
25899 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
25900 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
25901 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
25902 == CPP_OPEN_BRACE)))
25904 if (pedantic && cxx_dialect < cxx20)
25905 pedwarn (loc, OPT_Wc__20_extensions,
25906 "C++ designated initializers only available with "
25907 "%<-std=c++20%> or %<-std=gnu++20%>");
25908 /* Consume the `.'. */
25909 cp_lexer_consume_token (parser->lexer);
25910 /* Consume the identifier. */
25911 designator = cp_lexer_consume_token (parser->lexer)->u.value;
25912 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
25913 /* Consume the `='. */
25914 cp_lexer_consume_token (parser->lexer);
25915 else
25916 direct_p = true;
25918 /* Also, if the next token is an identifier and the following one is a
25919 colon, we are looking at the GNU designated-initializer
25920 syntax. */
25921 else if (cp_parser_allow_gnu_extensions_p (parser)
25922 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
25923 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
25924 == CPP_COLON))
25926 /* Warn the user that they are using an extension. */
25927 pedwarn (loc, OPT_Wpedantic,
25928 "ISO C++ does not allow GNU designated initializers");
25929 /* Consume the identifier. */
25930 designator = cp_lexer_consume_token (parser->lexer)->u.value;
25931 /* Consume the `:'. */
25932 cp_lexer_consume_token (parser->lexer);
25934 /* Also handle C99 array designators, '[ const ] ='. */
25935 else if (cp_parser_allow_gnu_extensions_p (parser)
25936 && !c_dialect_objc ()
25937 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
25939 /* In C++11, [ could start a lambda-introducer. */
25940 bool non_const = false;
25942 cp_parser_parse_tentatively (parser);
25944 if (!cp_parser_array_designator_p (parser))
25946 cp_parser_simulate_error (parser);
25947 designator = NULL_TREE;
25949 else
25951 designator = cp_parser_constant_expression (parser, true,
25952 &non_const);
25953 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25954 cp_parser_require (parser, CPP_EQ, RT_EQ);
25957 if (!cp_parser_parse_definitely (parser))
25958 designator = NULL_TREE;
25959 else if (non_const
25960 && (!require_potential_rvalue_constant_expression
25961 (designator)))
25962 designator = NULL_TREE;
25963 if (designator)
25964 /* Warn the user that they are using an extension. */
25965 pedwarn (loc, OPT_Wpedantic,
25966 "ISO C++ does not allow C99 designated initializers");
25968 else
25969 designator = NULL_TREE;
25971 if (first_p)
25973 first_designator = designator;
25974 first_p = false;
25976 else if (cxx_dialect >= cxx20
25977 && first_designator != error_mark_node
25978 && (!first_designator != !designator))
25980 error_at (loc, "either all initializer clauses should be designated "
25981 "or none of them should be");
25982 first_designator = error_mark_node;
25984 else if (cxx_dialect < cxx20 && !first_designator)
25985 first_designator = designator;
25987 /* Parse the initializer. */
25988 initializer = cp_parser_initializer_clause (parser,
25989 &clause_non_constant_p);
25990 /* If any clause is non-constant, so is the entire initializer. */
25991 if (clause_non_constant_p && non_constant_p)
25992 *non_constant_p = true;
25994 if (TREE_CODE (initializer) == CONSTRUCTOR)
25995 /* This uses |= rather than = because C_I_D_I could have been set in
25996 cp_parser_functional_cast so we must be careful not to clear the
25997 flag. */
25998 CONSTRUCTOR_IS_DIRECT_INIT (initializer) |= direct_p;
26000 /* If we have an ellipsis, this is an initializer pack
26001 expansion. */
26002 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26004 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26006 /* Consume the `...'. */
26007 cp_lexer_consume_token (parser->lexer);
26009 if (designator && cxx_dialect >= cxx20)
26010 error_at (loc,
26011 "%<...%> not allowed in designated initializer list");
26013 /* Turn the initializer into an initializer expansion. */
26014 initializer = make_pack_expansion (initializer);
26017 /* Add it to the vector. */
26018 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
26020 /* If the next token is not a comma, we have reached the end of
26021 the list. */
26022 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
26023 break;
26025 /* Peek at the next token. */
26026 token = cp_lexer_peek_nth_token (parser->lexer, 2);
26027 /* If the next token is a `}', then we're still done. An
26028 initializer-clause can have a trailing `,' after the
26029 initializer-list and before the closing `}'. */
26030 if (token->type == CPP_CLOSE_BRACE)
26031 break;
26033 /* Suppress location wrappers in a long initializer to save memory
26034 (14179). The cutoff is chosen arbitrarily. */
26035 const unsigned loc_max = 256;
26036 unsigned incr = 1;
26037 if (TREE_CODE (initializer) == CONSTRUCTOR)
26038 /* Look one level down because it's easy. Looking deeper would require
26039 passing down a nelts pointer, and I don't think multi-level massive
26040 initializers are common enough to justify this. */
26041 incr = CONSTRUCTOR_NELTS (initializer);
26042 nelts += incr;
26043 if (nelts >= loc_max && (nelts - incr) < loc_max)
26044 ++suppress_location_wrappers;
26046 /* Consume the `,' token. */
26047 cp_lexer_consume_token (parser->lexer);
26050 /* The same identifier shall not appear in multiple designators
26051 of a designated-initializer-list. */
26052 if (first_designator)
26054 unsigned int i;
26055 tree designator, val;
26056 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
26057 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
26059 if (IDENTIFIER_MARKED (designator))
26061 error_at (cp_expr_loc_or_input_loc (val),
26062 "%<.%s%> designator used multiple times in "
26063 "the same initializer list",
26064 IDENTIFIER_POINTER (designator));
26065 (*v)[i].index = error_mark_node;
26067 else
26068 IDENTIFIER_MARKED (designator) = 1;
26070 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
26071 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
26072 IDENTIFIER_MARKED (designator) = 0;
26075 suppress_location_wrappers = suppress;
26077 *designated = first_designator != NULL_TREE;
26078 return v;
26081 /* Classes [gram.class] */
26083 /* Parse a class-name.
26085 class-name:
26086 identifier
26087 template-id
26089 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
26090 to indicate that names looked up in dependent types should be
26091 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
26092 keyword has been used to indicate that the name that appears next
26093 is a template. TAG_TYPE indicates the explicit tag given before
26094 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
26095 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
26096 is the class being defined in a class-head. If ENUM_OK is TRUE,
26097 enum-names are also accepted.
26099 Returns the TYPE_DECL representing the class. */
26101 static tree
26102 cp_parser_class_name (cp_parser *parser,
26103 bool typename_keyword_p,
26104 bool template_keyword_p,
26105 enum tag_types tag_type,
26106 bool check_dependency_p,
26107 bool class_head_p,
26108 bool is_declaration,
26109 bool enum_ok)
26111 tree decl;
26112 tree identifier = NULL_TREE;
26114 /* All class-names start with an identifier. */
26115 cp_token *token = cp_lexer_peek_token (parser->lexer);
26116 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
26118 cp_parser_error (parser, "expected class-name");
26119 return error_mark_node;
26122 /* PARSER->SCOPE can be cleared when parsing the template-arguments
26123 to a template-id, so we save it here. Consider object scope too,
26124 so that make_typename_type below can use it (cp_parser_template_name
26125 considers object scope also). This may happen with code like
26127 p->template A<T>::a()
26129 where we first want to look up A<T>::a in the class of the object
26130 expression, as per [basic.lookup.classref]. */
26131 tree scope = parser->scope ? parser->scope : parser->context->object_type;
26132 /* This only checks parser->scope to avoid duplicate errors; if
26133 ->object_type is erroneous, go on to give a parse error. */
26134 if (parser->scope == error_mark_node)
26135 return error_mark_node;
26137 /* Any name names a type if we're following the `typename' keyword
26138 in a qualified name where the enclosing scope is type-dependent. */
26139 const bool typename_p = (typename_keyword_p
26140 && parser->scope
26141 && TYPE_P (parser->scope)
26142 && dependent_scope_p (parser->scope));
26143 /* Handle the common case (an identifier, but not a template-id)
26144 efficiently. */
26145 if (token->type == CPP_NAME
26146 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
26148 cp_token *identifier_token;
26149 bool ambiguous_p;
26151 /* Look for the identifier. */
26152 identifier_token = cp_lexer_peek_token (parser->lexer);
26153 ambiguous_p = identifier_token->error_reported;
26154 identifier = cp_parser_identifier (parser);
26155 /* If the next token isn't an identifier, we are certainly not
26156 looking at a class-name. */
26157 if (identifier == error_mark_node)
26158 decl = error_mark_node;
26159 /* If we know this is a type-name, there's no need to look it
26160 up. */
26161 else if (typename_p)
26162 decl = identifier;
26163 else
26165 tree ambiguous_decls;
26166 /* If we already know that this lookup is ambiguous, then
26167 we've already issued an error message; there's no reason
26168 to check again. */
26169 if (ambiguous_p)
26171 cp_parser_simulate_error (parser);
26172 return error_mark_node;
26174 /* If the next token is a `::', then the name must be a type
26175 name.
26177 [basic.lookup.qual]
26179 During the lookup for a name preceding the :: scope
26180 resolution operator, object, function, and enumerator
26181 names are ignored. */
26182 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
26183 tag_type = scope_type;
26184 /* Look up the name. */
26185 decl = cp_parser_lookup_name (parser, identifier,
26186 tag_type,
26187 /*is_template=*/false,
26188 /*is_namespace=*/false,
26189 check_dependency_p,
26190 &ambiguous_decls,
26191 identifier_token->location);
26192 if (ambiguous_decls)
26194 if (cp_parser_parsing_tentatively (parser))
26195 cp_parser_simulate_error (parser);
26196 return error_mark_node;
26200 else
26202 /* Try a template-id. */
26203 decl = cp_parser_template_id (parser, template_keyword_p,
26204 check_dependency_p,
26205 tag_type,
26206 is_declaration);
26207 if (decl == error_mark_node)
26208 return error_mark_node;
26211 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
26213 /* If this is a typename, create a TYPENAME_TYPE. */
26214 if (typename_p && decl != error_mark_node)
26216 decl = make_typename_type (scope, decl, typename_type,
26217 /*complain=*/tf_error);
26218 if (decl != error_mark_node)
26219 decl = TYPE_NAME (decl);
26222 decl = strip_using_decl (decl);
26224 /* Check to see that it is really the name of a class. */
26225 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
26226 && identifier_p (TREE_OPERAND (decl, 0))
26227 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
26228 /* Situations like this:
26230 template <typename T> struct A {
26231 typename T::template X<int>::I i;
26234 are problematic. Is `T::template X<int>' a class-name? The
26235 standard does not seem to be definitive, but there is no other
26236 valid interpretation of the following `::'. Therefore, those
26237 names are considered class-names. */
26239 decl = make_typename_type (scope, decl, tag_type, tf_error);
26240 if (decl != error_mark_node)
26241 decl = TYPE_NAME (decl);
26243 else if (TREE_CODE (decl) != TYPE_DECL
26244 || TREE_TYPE (decl) == error_mark_node
26245 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
26246 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
26247 /* In Objective-C 2.0, a classname followed by '.' starts a
26248 dot-syntax expression, and it's not a type-name. */
26249 || (c_dialect_objc ()
26250 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
26251 && objc_is_class_name (decl)))
26252 decl = error_mark_node;
26254 if (decl == error_mark_node)
26255 cp_parser_error (parser, "expected class-name");
26256 else if (identifier && !parser->scope)
26257 maybe_note_name_used_in_class (identifier, decl);
26259 return decl;
26262 /* Make sure that any member-function parameters are in scope.
26263 For instance, a function's noexcept-specifier can use the function's
26264 parameters:
26266 struct S {
26267 void fn (int p) noexcept(noexcept(p));
26270 so we need to make sure name lookup can find them. This is used
26271 when we delay parsing of the noexcept-specifier. */
26273 static void
26274 inject_parm_decls (tree decl)
26276 begin_scope (sk_function_parms, decl);
26277 tree args = DECL_ARGUMENTS (decl);
26279 do_push_parm_decls (decl, args, /*nonparms=*/NULL);
26281 if (args && is_this_parameter (args))
26283 gcc_checking_assert (current_class_ptr == NULL_TREE);
26284 current_class_ref = cp_build_fold_indirect_ref (args);
26285 current_class_ptr = args;
26289 /* Undo the effects of inject_parm_decls. */
26291 static void
26292 pop_injected_parms (void)
26294 pop_bindings_and_leave_scope ();
26295 current_class_ptr = current_class_ref = NULL_TREE;
26298 /* Parse a class-specifier.
26300 class-specifier:
26301 class-head { member-specification [opt] }
26303 Returns the TREE_TYPE representing the class. */
26305 tree
26306 cp_parser_class_specifier (cp_parser* parser)
26308 auto_timevar tv (TV_PARSE_STRUCT);
26310 tree type;
26311 tree attributes = NULL_TREE;
26312 bool nested_name_specifier_p;
26313 unsigned saved_num_template_parameter_lists;
26314 bool saved_in_function_body;
26315 unsigned char in_statement;
26316 bool in_switch_statement_p;
26317 bool saved_in_unbraced_linkage_specification_p;
26318 tree old_scope = NULL_TREE;
26319 tree scope = NULL_TREE;
26320 cp_token *closing_brace;
26322 push_deferring_access_checks (dk_no_deferred);
26324 /* Parse the class-head. */
26325 type = cp_parser_class_head (parser,
26326 &nested_name_specifier_p);
26327 /* If the class-head was a semantic disaster, skip the entire body
26328 of the class. */
26329 if (!type)
26331 cp_parser_skip_to_end_of_block_or_statement (parser);
26332 pop_deferring_access_checks ();
26333 return error_mark_node;
26336 /* Look for the `{'. */
26337 matching_braces braces;
26338 if (!braces.require_open (parser))
26340 pop_deferring_access_checks ();
26341 return error_mark_node;
26344 cp_ensure_no_omp_declare_simd (parser);
26345 cp_ensure_no_oacc_routine (parser);
26347 /* Issue an error message if type-definitions are forbidden here. */
26348 bool type_definition_ok_p = cp_parser_check_type_definition (parser);
26349 /* Remember that we are defining one more class. */
26350 ++parser->num_classes_being_defined;
26351 /* Inside the class, surrounding template-parameter-lists do not
26352 apply. */
26353 saved_num_template_parameter_lists
26354 = parser->num_template_parameter_lists;
26355 parser->num_template_parameter_lists = 0;
26356 /* We are not in a function body. */
26357 saved_in_function_body = parser->in_function_body;
26358 parser->in_function_body = false;
26359 /* Or in a loop. */
26360 in_statement = parser->in_statement;
26361 parser->in_statement = 0;
26362 /* Or in a switch. */
26363 in_switch_statement_p = parser->in_switch_statement_p;
26364 parser->in_switch_statement_p = false;
26365 /* We are not immediately inside an extern "lang" block. */
26366 saved_in_unbraced_linkage_specification_p
26367 = parser->in_unbraced_linkage_specification_p;
26368 parser->in_unbraced_linkage_specification_p = false;
26369 /* 'this' from an enclosing non-static member function is unavailable. */
26370 tree saved_ccp = current_class_ptr;
26371 tree saved_ccr = current_class_ref;
26372 current_class_ptr = NULL_TREE;
26373 current_class_ref = NULL_TREE;
26375 /* Start the class. */
26376 if (nested_name_specifier_p)
26378 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
26379 /* SCOPE must be a scope nested inside current scope. */
26380 if (is_nested_namespace (current_namespace,
26381 decl_namespace_context (scope)))
26382 old_scope = push_inner_scope (scope);
26383 else
26384 nested_name_specifier_p = false;
26386 type = begin_class_definition (type);
26388 if (type == error_mark_node)
26389 /* If the type is erroneous, skip the entire body of the class. */
26390 cp_parser_skip_to_closing_brace (parser);
26391 else
26392 /* Parse the member-specification. */
26393 cp_parser_member_specification_opt (parser);
26395 /* Look for the trailing `}'. */
26396 closing_brace = braces.require_close (parser);
26397 /* Look for trailing attributes to apply to this class. */
26398 if (cp_parser_allow_gnu_extensions_p (parser))
26399 attributes = cp_parser_gnu_attributes_opt (parser);
26400 if (type != error_mark_node)
26401 type = finish_struct (type, attributes);
26402 if (nested_name_specifier_p)
26403 pop_inner_scope (old_scope, scope);
26405 /* We've finished a type definition. Check for the common syntax
26406 error of forgetting a semicolon after the definition. We need to
26407 be careful, as we can't just check for not-a-semicolon and be done
26408 with it; the user might have typed:
26410 class X { } c = ...;
26411 class X { } *p = ...;
26413 and so forth. Instead, enumerate all the possible tokens that
26414 might follow this production; if we don't see one of them, then
26415 complain and silently insert the semicolon. */
26417 cp_token *token = cp_lexer_peek_token (parser->lexer);
26418 bool want_semicolon = true;
26420 if (cp_next_tokens_can_be_std_attribute_p (parser))
26421 /* Don't try to parse c++11 attributes here. As per the
26422 grammar, that should be a task for
26423 cp_parser_decl_specifier_seq. */
26424 want_semicolon = false;
26426 switch (token->type)
26428 case CPP_NAME:
26429 case CPP_SEMICOLON:
26430 case CPP_MULT:
26431 case CPP_AND:
26432 case CPP_OPEN_PAREN:
26433 case CPP_CLOSE_PAREN:
26434 case CPP_COMMA:
26435 case CPP_SCOPE:
26436 want_semicolon = false;
26437 break;
26439 /* While it's legal for type qualifiers and storage class
26440 specifiers to follow type definitions in the grammar, only
26441 compiler testsuites contain code like that. Assume that if
26442 we see such code, then what we're really seeing is a case
26443 like:
26445 class X { }
26446 const <type> var = ...;
26450 class Y { }
26451 static <type> func (...) ...
26453 i.e. the qualifier or specifier applies to the next
26454 declaration. To do so, however, we need to look ahead one
26455 more token to see if *that* token is a type specifier.
26457 This code could be improved to handle:
26459 class Z { }
26460 static const <type> var = ...; */
26461 case CPP_KEYWORD:
26462 if (keyword_is_decl_specifier (token->keyword))
26464 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
26466 /* Handling user-defined types here would be nice, but very
26467 tricky. */
26468 want_semicolon
26469 = (lookahead->type == CPP_KEYWORD
26470 && keyword_begins_type_specifier (lookahead->keyword));
26472 break;
26473 default:
26474 break;
26477 /* If we don't have a type, then something is very wrong and we
26478 shouldn't try to do anything clever. Likewise for not seeing the
26479 closing brace. */
26480 if (closing_brace && TYPE_P (type) && want_semicolon)
26482 /* Locate the closing brace. */
26483 cp_token_position prev
26484 = cp_lexer_previous_token_position (parser->lexer);
26485 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
26486 location_t loc = prev_token->location;
26488 /* We want to suggest insertion of a ';' immediately *after* the
26489 closing brace, so, if we can, offset the location by 1 column. */
26490 location_t next_loc = loc;
26491 if (!linemap_location_from_macro_expansion_p (line_table, loc))
26492 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
26494 rich_location richloc (line_table, next_loc);
26496 /* If we successfully offset the location, suggest the fix-it. */
26497 if (next_loc != loc)
26498 richloc.add_fixit_insert_before (next_loc, ";");
26500 if (CLASSTYPE_DECLARED_CLASS (type))
26501 error_at (&richloc,
26502 "expected %<;%> after class definition");
26503 else if (TREE_CODE (type) == RECORD_TYPE)
26504 error_at (&richloc,
26505 "expected %<;%> after struct definition");
26506 else if (TREE_CODE (type) == UNION_TYPE)
26507 error_at (&richloc,
26508 "expected %<;%> after union definition");
26509 else
26510 gcc_unreachable ();
26512 /* Unget one token and smash it to look as though we encountered
26513 a semicolon in the input stream. */
26514 cp_lexer_set_token_position (parser->lexer, prev);
26515 token = cp_lexer_peek_token (parser->lexer);
26516 token->type = CPP_SEMICOLON;
26517 token->keyword = RID_MAX;
26521 /* If this class is not itself within the scope of another class,
26522 then we need to parse the bodies of all of the queued function
26523 definitions. Note that the queued functions defined in a class
26524 are not always processed immediately following the
26525 class-specifier for that class. Consider:
26527 struct A {
26528 struct B { void f() { sizeof (A); } };
26531 If `f' were processed before the processing of `A' were
26532 completed, there would be no way to compute the size of `A'.
26533 Note that the nesting we are interested in here is lexical --
26534 not the semantic nesting given by TYPE_CONTEXT. In particular,
26535 for:
26537 struct A { struct B; };
26538 struct A::B { void f() { } };
26540 there is no need to delay the parsing of `A::B::f'. */
26541 if (--parser->num_classes_being_defined == 0)
26543 tree decl;
26544 tree class_type = NULL_TREE;
26545 tree pushed_scope = NULL_TREE;
26546 unsigned ix;
26547 cp_default_arg_entry *e;
26549 if (!type_definition_ok_p || any_erroneous_template_args_p (type))
26551 /* Skip default arguments, NSDMIs, etc, in order to improve
26552 error recovery (c++/71169, c++/71832). */
26553 vec_safe_truncate (unparsed_funs_with_default_args, 0);
26554 vec_safe_truncate (unparsed_nsdmis, 0);
26555 vec_safe_truncate (unparsed_funs_with_definitions, 0);
26558 /* In a first pass, parse default arguments to the functions.
26559 Then, in a second pass, parse the bodies of the functions.
26560 This two-phased approach handles cases like:
26562 struct S {
26563 void f() { g(); }
26564 void g(int i = 3);
26568 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
26570 decl = e->decl;
26571 /* If there are default arguments that have not yet been processed,
26572 take care of them now. */
26573 if (class_type != e->class_type)
26575 if (pushed_scope)
26576 pop_scope (pushed_scope);
26577 class_type = e->class_type;
26578 pushed_scope = push_scope (class_type);
26580 /* Make sure that any template parameters are in scope. */
26581 maybe_begin_member_template_processing (decl);
26582 /* Parse the default argument expressions. */
26583 cp_parser_late_parsing_default_args (parser, decl);
26584 /* Remove any template parameters from the symbol table. */
26585 maybe_end_member_template_processing ();
26587 vec_safe_truncate (unparsed_funs_with_default_args, 0);
26589 /* If there are noexcept-specifiers that have not yet been processed,
26590 take care of them now. Do this before processing NSDMIs as they
26591 may depend on noexcept-specifiers already having been processed. */
26592 FOR_EACH_VEC_SAFE_ELT (unparsed_noexcepts, ix, decl)
26594 tree ctx = DECL_CONTEXT (decl);
26595 if (class_type != ctx)
26597 if (pushed_scope)
26598 pop_scope (pushed_scope);
26599 class_type = ctx;
26600 pushed_scope = push_scope (class_type);
26603 tree def_parse = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl));
26604 def_parse = TREE_PURPOSE (def_parse);
26606 /* Make sure that any template parameters are in scope. */
26607 maybe_begin_member_template_processing (decl);
26609 /* Make sure that any member-function parameters are in scope.
26610 This function doesn't expect ccp to be set. */
26611 current_class_ptr = current_class_ref = NULL_TREE;
26612 inject_parm_decls (decl);
26614 /* 'this' is not allowed in static member functions. */
26615 unsigned char local_variables_forbidden_p
26616 = parser->local_variables_forbidden_p;
26617 if (DECL_THIS_STATIC (decl))
26618 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
26620 /* Now we can parse the noexcept-specifier. */
26621 tree spec = cp_parser_late_noexcept_specifier (parser, def_parse);
26623 if (spec == error_mark_node)
26624 spec = NULL_TREE;
26626 /* Update the fn's type directly -- it might have escaped
26627 beyond this decl :( */
26628 fixup_deferred_exception_variants (TREE_TYPE (decl), spec);
26629 /* Update any instantiations we've already created. We must
26630 keep the new noexcept-specifier wrapped in a DEFERRED_NOEXCEPT
26631 so that maybe_instantiate_noexcept can tsubst the NOEXCEPT_EXPR
26632 in the pattern. */
26633 for (tree i : DEFPARSE_INSTANTIATIONS (def_parse))
26634 DEFERRED_NOEXCEPT_PATTERN (TREE_PURPOSE (i))
26635 = spec ? TREE_PURPOSE (spec) : error_mark_node;
26637 /* Restore the state of local_variables_forbidden_p. */
26638 parser->local_variables_forbidden_p = local_variables_forbidden_p;
26640 /* The finish_struct call above performed various override checking,
26641 but it skipped unparsed noexcept-specifier operands. Now that we
26642 have resolved them, check again. */
26643 noexcept_override_late_checks (decl);
26645 /* Remove any member-function parameters from the symbol table. */
26646 pop_injected_parms ();
26648 /* Remove any template parameters from the symbol table. */
26649 maybe_end_member_template_processing ();
26651 vec_safe_truncate (unparsed_noexcepts, 0);
26653 /* Now parse any NSDMIs. */
26654 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
26656 tree ctx = type_context_for_name_lookup (decl);
26657 if (class_type != ctx)
26659 if (pushed_scope)
26660 pop_scope (pushed_scope);
26661 class_type = ctx;
26662 pushed_scope = push_scope (class_type);
26664 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
26665 cp_parser_late_parsing_nsdmi (parser, decl);
26667 vec_safe_truncate (unparsed_nsdmis, 0);
26669 /* Now contract attributes. */
26670 FOR_EACH_VEC_SAFE_ELT (unparsed_contracts, ix, decl)
26672 tree ctx = DECL_CONTEXT (decl);
26673 if (class_type != ctx)
26675 if (pushed_scope)
26676 pop_scope (pushed_scope);
26677 class_type = ctx;
26678 pushed_scope = push_scope (class_type);
26681 temp_override<tree> cfd(current_function_decl, decl);
26683 /* Make sure that any template parameters are in scope. */
26684 maybe_begin_member_template_processing (decl);
26686 /* Make sure that any member-function parameters are in scope.
26687 This function doesn't expect ccp to be set. */
26688 current_class_ptr = current_class_ref = NULL_TREE;
26689 inject_parm_decls (decl);
26691 /* 'this' is not allowed in static member functions. */
26692 unsigned char local_variables_forbidden_p
26693 = parser->local_variables_forbidden_p;
26694 if (DECL_THIS_STATIC (decl))
26695 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
26697 /* Now we can parse contract conditions. */
26698 for (tree a = DECL_ATTRIBUTES (decl); a; a = TREE_CHAIN (a))
26700 if (cxx_contract_attribute_p (a))
26701 cp_parser_late_contract_condition (parser, decl, a);
26704 /* Restore the state of local_variables_forbidden_p. */
26705 parser->local_variables_forbidden_p = local_variables_forbidden_p;
26707 /* Remove any member-function parameters from the symbol table. */
26708 pop_injected_parms ();
26710 /* Remove any template parameters from the symbol table. */
26711 maybe_end_member_template_processing ();
26713 /* Perform any deferred contract matching. */
26714 match_deferred_contracts (decl);
26716 vec_safe_truncate (unparsed_contracts, 0);
26718 current_class_ptr = NULL_TREE;
26719 current_class_ref = NULL_TREE;
26720 if (pushed_scope)
26721 pop_scope (pushed_scope);
26723 /* Now parse the body of the functions. */
26724 if (flag_openmp)
26726 /* OpenMP UDRs need to be parsed before all other functions. */
26727 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
26728 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
26729 cp_parser_late_parsing_for_member (parser, decl);
26730 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
26731 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
26732 cp_parser_late_parsing_for_member (parser, decl);
26734 else
26735 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
26736 cp_parser_late_parsing_for_member (parser, decl);
26737 vec_safe_truncate (unparsed_funs_with_definitions, 0);
26740 /* Put back any saved access checks. */
26741 pop_deferring_access_checks ();
26743 /* Restore saved state. */
26744 parser->in_switch_statement_p = in_switch_statement_p;
26745 parser->in_statement = in_statement;
26746 parser->in_function_body = saved_in_function_body;
26747 parser->num_template_parameter_lists
26748 = saved_num_template_parameter_lists;
26749 parser->in_unbraced_linkage_specification_p
26750 = saved_in_unbraced_linkage_specification_p;
26751 current_class_ptr = saved_ccp;
26752 current_class_ref = saved_ccr;
26754 return type;
26757 /* Parse a class-head.
26759 class-head:
26760 class-key identifier [opt] base-clause [opt]
26761 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
26762 class-key nested-name-specifier [opt] template-id
26763 base-clause [opt]
26765 class-virt-specifier:
26766 final
26768 GNU Extensions:
26769 class-key attributes identifier [opt] base-clause [opt]
26770 class-key attributes nested-name-specifier identifier base-clause [opt]
26771 class-key attributes nested-name-specifier [opt] template-id
26772 base-clause [opt]
26774 Upon return BASES is initialized to the list of base classes (or
26775 NULL, if there are none) in the same form returned by
26776 cp_parser_base_clause.
26778 Returns the TYPE of the indicated class. Sets
26779 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
26780 involving a nested-name-specifier was used, and FALSE otherwise.
26782 Returns error_mark_node if this is not a class-head.
26784 Returns NULL_TREE if the class-head is syntactically valid, but
26785 semantically invalid in a way that means we should skip the entire
26786 body of the class. */
26788 static tree
26789 cp_parser_class_head (cp_parser* parser,
26790 bool* nested_name_specifier_p)
26792 tree nested_name_specifier;
26793 enum tag_types class_key;
26794 tree id = NULL_TREE;
26795 tree type = NULL_TREE;
26796 tree attributes;
26797 tree bases;
26798 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
26799 bool template_id_p = false;
26800 bool qualified_p = false;
26801 bool invalid_nested_name_p = false;
26802 bool invalid_explicit_specialization_p = false;
26803 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
26804 tree pushed_scope = NULL_TREE;
26805 unsigned num_templates;
26806 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
26807 /* Assume no nested-name-specifier will be present. */
26808 *nested_name_specifier_p = false;
26809 /* Assume no template parameter lists will be used in defining the
26810 type. */
26811 num_templates = 0;
26812 parser->colon_corrects_to_scope_p = false;
26814 /* Look for the class-key. */
26815 class_key = cp_parser_class_key (parser);
26816 if (class_key == none_type)
26817 return error_mark_node;
26819 location_t class_head_start_location = input_location;
26821 /* Parse the attributes. */
26822 attributes = cp_parser_attributes_opt (parser);
26823 if (find_contract (attributes))
26824 diagnose_misapplied_contracts (attributes);
26826 /* If the next token is `::', that is invalid -- but sometimes
26827 people do try to write:
26829 struct ::S {};
26831 Handle this gracefully by accepting the extra qualifier, and then
26832 issuing an error about it later if this really is a
26833 class-head. If it turns out just to be an elaborated type
26834 specifier, remain silent. */
26835 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
26836 qualified_p = true;
26838 /* It is OK to define an inaccessible class; for example:
26840 class A { class B; };
26841 class A::B {};
26843 So we want to ignore access when parsing the class name.
26844 However, we might be tentatively parsing what is really an
26845 elaborated-type-specifier naming a template-id, e.g.
26847 struct C<&D::m> c;
26849 In this case the tentative parse as a class-head will fail, but not
26850 before cp_parser_template_id splices in a CPP_TEMPLATE_ID token.
26851 Since dk_no_check is sticky, we must instead use dk_deferred so that
26852 any such CPP_TEMPLATE_ID token created during this tentative parse
26853 will correctly capture the access checks imposed by the template-id . */
26854 push_deferring_access_checks (dk_deferred);
26856 /* Determine the name of the class. Begin by looking for an
26857 optional nested-name-specifier. */
26858 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
26859 nested_name_specifier
26860 = cp_parser_nested_name_specifier_opt (parser,
26861 /*typename_keyword_p=*/false,
26862 /*check_dependency_p=*/false,
26863 /*type_p=*/true,
26864 /*is_declaration=*/false);
26865 /* If there was a nested-name-specifier, then there *must* be an
26866 identifier. */
26868 cp_token *bad_template_keyword = NULL;
26870 if (nested_name_specifier)
26872 type_start_token = cp_lexer_peek_token (parser->lexer);
26873 /* Although the grammar says `identifier', it really means
26874 `class-name' or `template-name'. You are only allowed to
26875 define a class that has already been declared with this
26876 syntax.
26878 The proposed resolution for Core Issue 180 says that wherever
26879 you see `class T::X' you should treat `X' as a type-name.
26881 We do not know if we will see a class-name, or a
26882 template-name. We look for a class-name first, in case the
26883 class-name is a template-id; if we looked for the
26884 template-name first we would stop after the template-name. */
26885 cp_parser_parse_tentatively (parser);
26886 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26887 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
26888 type = cp_parser_class_name (parser,
26889 /*typename_keyword_p=*/false,
26890 /*template_keyword_p=*/false,
26891 class_type,
26892 /*check_dependency_p=*/false,
26893 /*class_head_p=*/true,
26894 /*is_declaration=*/false);
26895 /* If that didn't work, ignore the nested-name-specifier. */
26896 if (!cp_parser_parse_definitely (parser))
26898 invalid_nested_name_p = true;
26899 type_start_token = cp_lexer_peek_token (parser->lexer);
26900 id = cp_parser_identifier (parser);
26901 if (id == error_mark_node)
26902 id = NULL_TREE;
26904 /* If we could not find a corresponding TYPE, treat this
26905 declaration like an unqualified declaration. */
26906 if (type == error_mark_node)
26907 nested_name_specifier = NULL_TREE;
26908 /* Otherwise, count the number of templates used in TYPE and its
26909 containing scopes. */
26910 else
26911 num_templates = num_template_headers_for_class (TREE_TYPE (type));
26913 /* Otherwise, the identifier is optional. */
26914 else
26916 /* We don't know whether what comes next is a template-id,
26917 an identifier, or nothing at all. */
26918 cp_parser_parse_tentatively (parser);
26919 /* Check for a template-id. */
26920 type_start_token = cp_lexer_peek_token (parser->lexer);
26921 id = cp_parser_template_id (parser,
26922 /*template_keyword_p=*/false,
26923 /*check_dependency_p=*/true,
26924 class_key,
26925 /*is_declaration=*/true);
26926 /* If that didn't work, it could still be an identifier. */
26927 if (!cp_parser_parse_definitely (parser))
26929 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26931 type_start_token = cp_lexer_peek_token (parser->lexer);
26932 id = cp_parser_identifier (parser);
26934 else
26935 id = NULL_TREE;
26937 else
26939 template_id_p = true;
26940 ++num_templates;
26944 pop_deferring_access_checks ();
26946 if (id)
26948 cp_parser_check_for_invalid_template_id (parser, id,
26949 class_key,
26950 type_start_token->location);
26952 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
26954 /* If it's not a `:' or a `{' then we can't really be looking at a
26955 class-head, since a class-head only appears as part of a
26956 class-specifier. We have to detect this situation before calling
26957 xref_tag, since that has irreversible side-effects. */
26958 if (!cp_parser_next_token_starts_class_definition_p (parser))
26960 cp_parser_error (parser, "expected %<{%> or %<:%>");
26961 type = error_mark_node;
26962 goto out;
26965 /* At this point, we're going ahead with the class-specifier, even
26966 if some other problem occurs. */
26967 cp_parser_commit_to_tentative_parse (parser);
26968 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
26970 cp_parser_error (parser,
26971 "cannot specify %<override%> for a class");
26972 type = error_mark_node;
26973 goto out;
26975 /* Issue the error about the overly-qualified name now. */
26976 if (qualified_p)
26978 cp_parser_error (parser,
26979 "global qualification of class name is invalid");
26980 type = error_mark_node;
26981 goto out;
26983 else if (invalid_nested_name_p)
26985 cp_parser_error (parser,
26986 "qualified name does not name a class");
26987 type = error_mark_node;
26988 goto out;
26990 else if (nested_name_specifier)
26992 tree scope;
26994 if (bad_template_keyword)
26995 /* [temp.names]: in a qualified-id formed by a class-head-name, the
26996 keyword template shall not appear at the top level. */
26997 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
26998 "keyword %<template%> not allowed in class-head-name");
27000 /* Reject typedef-names in class heads. */
27001 if (!DECL_IMPLICIT_TYPEDEF_P (type))
27003 error_at (type_start_token->location,
27004 "invalid class name in declaration of %qD",
27005 type);
27006 type = NULL_TREE;
27007 goto done;
27010 /* Figure out in what scope the declaration is being placed. */
27011 scope = current_scope ();
27012 /* If that scope does not contain the scope in which the
27013 class was originally declared, the program is invalid. */
27014 if (scope && !is_ancestor (scope, nested_name_specifier))
27016 if (at_namespace_scope_p ())
27017 error_at (type_start_token->location,
27018 "declaration of %qD in namespace %qD which does not "
27019 "enclose %qD",
27020 type, scope, nested_name_specifier);
27021 else
27022 error_at (type_start_token->location,
27023 "declaration of %qD in %qD which does not enclose %qD",
27024 type, scope, nested_name_specifier);
27025 type = NULL_TREE;
27026 goto done;
27028 /* [dcl.meaning]
27030 A declarator-id shall not be qualified except for the
27031 definition of a ... nested class outside of its class
27032 ... [or] the definition or explicit instantiation of a
27033 class member of a namespace outside of its namespace. */
27034 if (scope == nested_name_specifier)
27035 permerror (nested_name_specifier_token_start->location,
27036 "extra qualification not allowed");
27038 /* An explicit-specialization must be preceded by "template <>". If
27039 it is not, try to recover gracefully. */
27040 if (at_namespace_scope_p ()
27041 && parser->num_template_parameter_lists == 0
27042 && !processing_template_parmlist
27043 && template_id_p)
27045 /* Build a location of this form:
27046 struct typename <ARGS>
27047 ^~~~~~~~~~~~~~~~~~~~~~
27048 with caret==start at the start token, and
27049 finishing at the end of the type. */
27050 location_t reported_loc
27051 = make_location (class_head_start_location,
27052 class_head_start_location,
27053 get_finish (type_start_token->location));
27054 rich_location richloc (line_table, reported_loc);
27055 richloc.add_fixit_insert_before (class_head_start_location,
27056 "template <> ");
27057 error_at (&richloc,
27058 "an explicit specialization must be preceded by"
27059 " %<template <>%>");
27060 invalid_explicit_specialization_p = true;
27061 /* Take the same action that would have been taken by
27062 cp_parser_explicit_specialization. */
27063 ++parser->num_template_parameter_lists;
27064 begin_specialization ();
27066 /* There must be no "return" statements between this point and the
27067 end of this function; set "type "to the correct return value and
27068 use "goto done;" to return. */
27069 /* Make sure that the right number of template parameters were
27070 present. */
27071 if (!cp_parser_check_template_parameters (parser, num_templates,
27072 template_id_p,
27073 type_start_token->location,
27074 /*declarator=*/NULL))
27076 /* If something went wrong, there is no point in even trying to
27077 process the class-definition. */
27078 type = NULL_TREE;
27079 goto done;
27082 /* Look up the type. */
27083 if (template_id_p)
27085 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
27086 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
27087 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
27089 error_at (type_start_token->location,
27090 "function template %qD redeclared as a class template", id);
27091 type = error_mark_node;
27093 else
27095 type = TREE_TYPE (id);
27096 type = maybe_process_partial_specialization (type);
27098 /* Check the scope while we still know whether or not we had a
27099 nested-name-specifier. */
27100 if (type != error_mark_node)
27101 check_unqualified_spec_or_inst (type, type_start_token->location);
27103 if (nested_name_specifier)
27104 pushed_scope = push_scope (nested_name_specifier);
27106 else if (nested_name_specifier)
27108 type = TREE_TYPE (type);
27110 /* Given:
27112 template <typename T> struct S { struct T };
27113 template <typename T> struct S<T>::T { };
27115 we will get a TYPENAME_TYPE when processing the definition of
27116 `S::T'. We need to resolve it to the actual type before we
27117 try to define it. */
27118 if (TREE_CODE (type) == TYPENAME_TYPE)
27120 type = resolve_typename_type (type, /*only_current_p=*/false);
27121 if (TREE_CODE (type) == TYPENAME_TYPE)
27123 cp_parser_error (parser, "could not resolve typename type");
27124 type = error_mark_node;
27128 type = maybe_process_partial_specialization (type);
27129 if (type == error_mark_node)
27131 type = NULL_TREE;
27132 goto done;
27135 /* Enter the scope indicated by the nested-name-specifier. */
27136 pushed_scope = push_scope (nested_name_specifier);
27137 /* Get the canonical version of this type. */
27138 type = TYPE_MAIN_DECL (type);
27139 /* Call push_template_decl if it seems like we should be defining a
27140 template either from the template headers or the type we're
27141 defining, so that we diagnose both extra and missing headers. */
27142 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
27143 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
27144 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
27146 type = push_template_decl (type);
27147 if (type == error_mark_node)
27149 type = NULL_TREE;
27150 goto done;
27154 type = TREE_TYPE (type);
27155 *nested_name_specifier_p = true;
27157 else /* The name is not a nested name. */
27159 /* If the class was unnamed, create a dummy name. */
27160 if (!id)
27161 id = make_anon_name ();
27162 TAG_how how = (parser->in_type_id_in_expr_p
27163 ? TAG_how::INNERMOST_NON_CLASS
27164 : TAG_how::CURRENT_ONLY);
27165 type = xref_tag (class_key, id, how,
27166 parser->num_template_parameter_lists);
27169 /* Diagnose class/struct/union mismatches. */
27170 cp_parser_check_class_key (parser, UNKNOWN_LOCATION, class_key, type,
27171 true, true);
27173 /* Indicate whether this class was declared as a `class' or as a
27174 `struct'. */
27175 if (TREE_CODE (type) == RECORD_TYPE)
27176 CLASSTYPE_DECLARED_CLASS (type) = class_key == class_type;
27178 /* If this type was already complete, and we see another definition,
27179 that's an error. Likewise if the type is already being defined:
27180 this can happen, eg, when it's defined from within an expression
27181 (c++/84605). */
27182 if (type != error_mark_node
27183 && (COMPLETE_TYPE_P (type) || TYPE_BEING_DEFINED (type)))
27185 error_at (type_start_token->location, "redefinition of %q#T",
27186 type);
27187 inform (location_of (type), "previous definition of %q#T",
27188 type);
27189 type = NULL_TREE;
27190 goto done;
27192 else if (type == error_mark_node)
27193 type = NULL_TREE;
27195 if (type)
27197 if (current_lambda_expr ()
27198 && uses_parameter_packs (attributes))
27200 /* In a lambda this should work, but doesn't currently. */
27201 sorry ("unexpanded parameter pack in local class in lambda");
27202 attributes = NULL_TREE;
27205 /* Apply attributes now, before any use of the class as a template
27206 argument in its base list. */
27207 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
27208 fixup_attribute_variants (type);
27211 /* Associate constraints with the type. */
27212 if (flag_concepts)
27213 type = associate_classtype_constraints (type);
27215 /* We will have entered the scope containing the class; the names of
27216 base classes should be looked up in that context. For example:
27218 struct A { struct B {}; struct C; };
27219 struct A::C : B {};
27221 is valid. */
27223 /* Get the list of base-classes, if there is one. Defer access checking
27224 until the entire list has been seen, as per [class.access.general]. */
27225 push_deferring_access_checks (dk_deferred);
27226 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27228 if (type)
27229 pushclass (type);
27230 bases = cp_parser_base_clause (parser);
27231 if (type)
27232 popclass ();
27234 else
27235 bases = NULL_TREE;
27237 /* If we're really defining a class, process the base classes.
27238 If they're invalid, fail. */
27239 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27240 xref_basetypes (type, bases);
27242 /* Now that all bases have been seen and attached to the class, check
27243 accessibility of the types named in the base-clause. This must be
27244 done relative to the class scope, so that we accept e.g.
27246 struct A { protected: struct B {}; };
27247 struct C : A::B, A {}; // OK: A::B is accessible via base A
27249 as per [class.access.general]. */
27250 if (type)
27251 pushclass (type);
27252 pop_to_parent_deferring_access_checks ();
27253 if (type)
27254 popclass ();
27256 done:
27257 /* Leave the scope given by the nested-name-specifier. We will
27258 enter the class scope itself while processing the members. */
27259 if (pushed_scope)
27260 pop_scope (pushed_scope);
27262 if (invalid_explicit_specialization_p)
27264 end_specialization ();
27265 --parser->num_template_parameter_lists;
27268 if (type)
27269 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
27270 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
27271 CLASSTYPE_FINAL (type) = 1;
27272 out:
27273 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27274 return type;
27277 /* Parse a class-key.
27279 class-key:
27280 class
27281 struct
27282 union
27284 Returns the kind of class-key specified, or none_type to indicate
27285 error. */
27287 static enum tag_types
27288 cp_parser_class_key (cp_parser* parser)
27290 cp_token *token;
27291 enum tag_types tag_type;
27293 /* Look for the class-key. */
27294 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
27295 if (!token)
27296 return none_type;
27298 /* Check to see if the TOKEN is a class-key. */
27299 tag_type = cp_parser_token_is_class_key (token);
27300 if (!tag_type)
27301 cp_parser_error (parser, "expected class-key");
27302 return tag_type;
27305 /* Parse a type-parameter-key.
27307 type-parameter-key:
27308 class
27309 typename
27312 static void
27313 cp_parser_type_parameter_key (cp_parser* parser)
27315 /* Look for the type-parameter-key. */
27316 enum tag_types tag_type = none_type;
27317 cp_token *token = cp_lexer_peek_token (parser->lexer);
27318 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
27320 cp_lexer_consume_token (parser->lexer);
27321 if (pedantic && tag_type == typename_type
27322 && cxx_dialect < cxx17)
27323 /* typename is not allowed in a template template parameter
27324 by the standard until C++17. */
27325 pedwarn (token->location, OPT_Wc__17_extensions,
27326 "ISO C++ forbids typename key in template template parameter;"
27327 " use %<-std=c++17%> or %<-std=gnu++17%>");
27329 else
27330 cp_parser_error (parser, "expected %<class%> or %<typename%>");
27332 return;
27335 /* Parse an (optional) member-specification.
27337 member-specification:
27338 member-declaration member-specification [opt]
27339 access-specifier : member-specification [opt] */
27341 static void
27342 cp_parser_member_specification_opt (cp_parser* parser)
27344 while (true)
27346 cp_token *token;
27347 enum rid keyword;
27349 /* Peek at the next token. */
27350 token = cp_lexer_peek_token (parser->lexer);
27351 /* If it's a `}', or EOF then we've seen all the members. */
27352 if (token->type == CPP_CLOSE_BRACE
27353 || token->type == CPP_EOF
27354 || token->type == CPP_PRAGMA_EOL)
27355 break;
27357 /* See if this token is a keyword. */
27358 keyword = token->keyword;
27359 switch (keyword)
27361 case RID_PUBLIC:
27362 case RID_PROTECTED:
27363 case RID_PRIVATE:
27364 /* Consume the access-specifier. */
27365 cp_lexer_consume_token (parser->lexer);
27366 /* Remember which access-specifier is active. */
27367 current_access_specifier = token->u.value;
27368 /* Look for the `:'. */
27369 cp_parser_require (parser, CPP_COLON, RT_COLON);
27370 break;
27372 default:
27373 /* Accept #pragmas at class scope. */
27374 if (token->type == CPP_PRAGMA)
27376 cp_parser_pragma (parser, pragma_member, NULL);
27377 break;
27380 /* Otherwise, the next construction must be a
27381 member-declaration. */
27382 cp_parser_member_declaration (parser);
27387 /* Parse a member-declaration.
27389 member-declaration:
27390 decl-specifier-seq [opt] member-declarator-list [opt] ;
27391 function-definition ; [opt]
27392 :: [opt] nested-name-specifier template [opt] unqualified-id ;
27393 using-declaration
27394 template-declaration
27395 alias-declaration
27397 member-declarator-list:
27398 member-declarator
27399 member-declarator-list , member-declarator
27401 member-declarator:
27402 declarator pure-specifier [opt]
27403 declarator constant-initializer [opt]
27404 identifier [opt] : constant-expression
27406 GNU Extensions:
27408 member-declaration:
27409 __extension__ member-declaration
27411 member-declarator:
27412 declarator attributes [opt] pure-specifier [opt]
27413 declarator attributes [opt] constant-initializer [opt]
27414 identifier [opt] attributes [opt] : constant-expression
27416 C++0x Extensions:
27418 member-declaration:
27419 static_assert-declaration */
27421 static void
27422 cp_parser_member_declaration (cp_parser* parser)
27424 cp_decl_specifier_seq decl_specifiers;
27425 tree prefix_attributes;
27426 tree decl;
27427 int declares_class_or_enum;
27428 bool friend_p;
27429 cp_token *token = NULL;
27430 cp_token *decl_spec_token_start = NULL;
27431 cp_token *initializer_token_start = NULL;
27432 int saved_pedantic;
27433 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27435 /* Check for the `__extension__' keyword. */
27436 if (cp_parser_extension_opt (parser, &saved_pedantic))
27438 /* Recurse. */
27439 cp_parser_member_declaration (parser);
27440 /* Restore the old value of the PEDANTIC flag. */
27441 pedantic = saved_pedantic;
27443 return;
27446 /* Check for a template-declaration. */
27447 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27449 /* An explicit specialization here is an error condition, and we
27450 expect the specialization handler to detect and report this. */
27451 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
27452 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
27453 cp_parser_explicit_specialization (parser);
27454 else
27455 cp_parser_template_declaration (parser, /*member_p=*/true);
27457 return;
27459 /* Check for a template introduction. */
27460 else if (cp_parser_template_declaration_after_export (parser, true))
27461 return;
27463 /* Check for a using-declaration. */
27464 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
27466 if (cxx_dialect < cxx11)
27467 /* Parse the using-declaration. */
27468 cp_parser_using_declaration (parser, /*access_declaration_p=*/false);
27469 else if (cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_ENUM))
27470 cp_parser_using_enum (parser);
27471 else
27473 tree decl;
27474 bool alias_decl_expected;
27475 cp_parser_parse_tentatively (parser);
27476 decl = cp_parser_alias_declaration (parser);
27477 /* Note that if we actually see the '=' token after the
27478 identifier, cp_parser_alias_declaration commits the
27479 tentative parse. In that case, we really expect an
27480 alias-declaration. Otherwise, we expect a using
27481 declaration. */
27482 alias_decl_expected =
27483 !cp_parser_uncommitted_to_tentative_parse_p (parser);
27484 cp_parser_parse_definitely (parser);
27486 if (alias_decl_expected)
27487 finish_member_declaration (decl);
27488 else
27489 cp_parser_using_declaration (parser,
27490 /*access_declaration_p=*/false);
27492 return;
27495 /* Check for @defs. */
27496 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
27498 tree ivar, member;
27499 tree ivar_chains = cp_parser_objc_defs_expression (parser);
27500 ivar = ivar_chains;
27501 while (ivar)
27503 member = ivar;
27504 ivar = TREE_CHAIN (member);
27505 TREE_CHAIN (member) = NULL_TREE;
27506 finish_member_declaration (member);
27508 return;
27511 /* If the next token is `static_assert' we have a static assertion. */
27512 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
27514 cp_parser_static_assert (parser, /*member_p=*/true);
27515 return;
27518 parser->colon_corrects_to_scope_p = false;
27520 cp_omp_declare_simd_data odsd;
27521 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
27522 goto out;
27524 /* Parse the decl-specifier-seq. */
27525 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
27526 cp_parser_decl_specifier_seq (parser,
27527 (CP_PARSER_FLAGS_OPTIONAL
27528 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
27529 &decl_specifiers,
27530 &declares_class_or_enum);
27532 if (decl_specifiers.attributes && (flag_openmp || flag_openmp_simd))
27533 cp_parser_handle_directive_omp_attributes (parser,
27534 &decl_specifiers.attributes,
27535 &odsd, true);
27537 /* Check for an invalid type-name. */
27538 if (!decl_specifiers.any_type_specifiers_p
27539 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
27540 goto out;
27541 /* If there is no declarator, then the decl-specifier-seq should
27542 specify a type. */
27543 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27545 /* If there was no decl-specifier-seq, and the next token is a
27546 `;', then we have something like:
27548 struct S { ; };
27550 [class.mem]
27552 Each member-declaration shall declare at least one member
27553 name of the class. */
27554 if (!decl_specifiers.any_specifiers_p)
27556 cp_token *token = cp_lexer_peek_token (parser->lexer);
27557 if (!in_system_header_at (token->location))
27559 gcc_rich_location richloc (token->location);
27560 richloc.add_fixit_remove ();
27561 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
27564 else
27566 /* See if this declaration is a friend. */
27567 friend_p = cp_parser_friend_p (&decl_specifiers);
27568 /* If there were decl-specifiers, check to see if there was
27569 a class-declaration. */
27570 tree type = check_tag_decl (&decl_specifiers,
27571 /*explicit_type_instantiation_p=*/false);
27572 /* Nested classes have already been added to the class, but
27573 a `friend' needs to be explicitly registered. */
27574 if (friend_p)
27576 /* If the `friend' keyword was present, the friend must
27577 be introduced with a class-key. */
27578 if (!declares_class_or_enum && cxx_dialect < cxx11)
27579 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
27580 "in C++03 a class-key must be used "
27581 "when declaring a friend");
27582 /* In this case:
27584 template <typename T> struct A {
27585 friend struct A<T>::B;
27588 A<T>::B will be represented by a TYPENAME_TYPE, and
27589 therefore not recognized by check_tag_decl. */
27590 if (!type)
27592 type = decl_specifiers.type;
27593 if (type && TREE_CODE (type) == TYPE_DECL)
27594 type = TREE_TYPE (type);
27596 /* Warn if an attribute cannot appear here, as per
27597 [dcl.attr.grammar]/5. But not when declares_class_or_enum:
27598 we ignore attributes in elaborated-type-specifiers. */
27599 if (!declares_class_or_enum
27600 && cxx11_attribute_p (decl_specifiers.attributes))
27602 decl_specifiers.attributes = NULL_TREE;
27603 if (warning_at (decl_spec_token_start->location,
27604 OPT_Wattributes, "attribute ignored"))
27605 inform (decl_spec_token_start->location, "an attribute "
27606 "that appertains to a friend declaration that "
27607 "is not a definition is ignored");
27609 if (!type || !TYPE_P (type))
27610 error_at (decl_spec_token_start->location,
27611 "friend declaration does not name a class or "
27612 "function");
27613 else
27614 make_friend_class (current_class_type, type,
27615 /*complain=*/true);
27617 /* If there is no TYPE, an error message will already have
27618 been issued. */
27619 else if (!type || type == error_mark_node)
27621 /* An anonymous aggregate has to be handled specially; such
27622 a declaration really declares a data member (with a
27623 particular type), as opposed to a nested class. */
27624 else if (ANON_AGGR_TYPE_P (type))
27626 /* C++11 9.5/6. */
27627 if (decl_specifiers.storage_class != sc_none)
27628 error_at (decl_spec_token_start->location,
27629 "a storage class on an anonymous aggregate "
27630 "in class scope is not allowed");
27632 /* Remove constructors and such from TYPE, now that we
27633 know it is an anonymous aggregate. */
27634 fixup_anonymous_aggr (type);
27635 /* And make the corresponding data member. */
27636 decl = build_decl (decl_spec_token_start->location,
27637 FIELD_DECL, NULL_TREE, type);
27638 /* Add it to the class. */
27639 finish_member_declaration (decl);
27641 else
27642 cp_parser_check_access_in_redeclaration
27643 (TYPE_NAME (type),
27644 decl_spec_token_start->location);
27647 else
27649 bool assume_semicolon = false;
27651 /* Clear attributes from the decl_specifiers but keep them
27652 around as prefix attributes that apply them to the entity
27653 being declared. */
27654 prefix_attributes = decl_specifiers.attributes;
27655 decl_specifiers.attributes = NULL_TREE;
27656 if (parser->omp_declare_simd
27657 && (parser->omp_declare_simd->attribs[0]
27658 == &decl_specifiers.attributes))
27659 parser->omp_declare_simd->attribs[0] = &prefix_attributes;
27661 /* See if these declarations will be friends. */
27662 friend_p = cp_parser_friend_p (&decl_specifiers);
27664 /* Keep going until we hit the `;' at the end of the
27665 declaration. */
27666 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27668 tree attributes = NULL_TREE;
27669 tree first_attribute;
27670 tree initializer;
27671 bool named_bitfld = false;
27673 /* Peek at the next token. */
27674 token = cp_lexer_peek_token (parser->lexer);
27676 /* The following code wants to know early if it is a bit-field
27677 or some other declaration. Attributes can appear before
27678 the `:' token. Skip over them without consuming any tokens
27679 to peek if they are followed by `:'. */
27680 if (cp_next_tokens_can_be_attribute_p (parser)
27681 || (token->type == CPP_NAME
27682 && cp_nth_tokens_can_be_attribute_p (parser, 2)
27683 && (named_bitfld = true)))
27685 size_t n
27686 = cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
27687 token = cp_lexer_peek_nth_token (parser->lexer, n);
27690 /* Check for a bitfield declaration. */
27691 if (token->type == CPP_COLON
27692 || (token->type == CPP_NAME
27693 && token == cp_lexer_peek_token (parser->lexer)
27694 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
27695 && (named_bitfld = true)))
27697 tree identifier;
27698 tree width;
27699 tree late_attributes = NULL_TREE;
27700 location_t id_location
27701 = cp_lexer_peek_token (parser->lexer)->location;
27703 if (named_bitfld)
27704 identifier = cp_parser_identifier (parser);
27705 else
27706 identifier = NULL_TREE;
27708 /* Look for attributes that apply to the bitfield. */
27709 attributes = cp_parser_attributes_opt (parser);
27711 /* Consume the `:' token. */
27712 cp_lexer_consume_token (parser->lexer);
27714 /* Get the width of the bitfield. */
27715 width = cp_parser_constant_expression (parser, false, NULL,
27716 cxx_dialect >= cxx11);
27718 /* In C++20 and as extension for C++11 and above we allow
27719 default member initializers for bit-fields. */
27720 initializer = NULL_TREE;
27721 if (cxx_dialect >= cxx11
27722 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
27723 || cp_lexer_next_token_is (parser->lexer,
27724 CPP_OPEN_BRACE)))
27726 location_t loc
27727 = cp_lexer_peek_token (parser->lexer)->location;
27728 if (cxx_dialect < cxx20
27729 && identifier != NULL_TREE)
27730 pedwarn (loc, OPT_Wc__20_extensions,
27731 "default member initializers for bit-fields "
27732 "only available with %<-std=c++20%> or "
27733 "%<-std=gnu++20%>");
27735 initializer = cp_parser_save_nsdmi (parser);
27736 if (identifier == NULL_TREE)
27738 error_at (loc, "default member initializer for "
27739 "unnamed bit-field");
27740 initializer = NULL_TREE;
27743 else
27745 /* Look for attributes that apply to the bitfield after
27746 the `:' token and width. This is where GCC used to
27747 parse attributes in the past, pedwarn if there is
27748 a std attribute. */
27749 if (cp_next_tokens_can_be_std_attribute_p (parser))
27750 pedwarn (input_location, OPT_Wpedantic,
27751 "ISO C++ allows bit-field attributes only "
27752 "before the %<:%> token");
27754 late_attributes = cp_parser_attributes_opt (parser);
27757 attributes = attr_chainon (attributes, late_attributes);
27759 /* Remember which attributes are prefix attributes and
27760 which are not. */
27761 first_attribute = attributes;
27762 /* Combine the attributes. */
27763 attributes = attr_chainon (prefix_attributes, attributes);
27765 /* Create the bitfield declaration. */
27766 decl = grokbitfield (identifier
27767 ? make_id_declarator (NULL_TREE,
27768 identifier,
27769 sfk_none,
27770 id_location)
27771 : NULL,
27772 &decl_specifiers,
27773 width, initializer,
27774 attributes);
27776 else
27778 cp_declarator *declarator;
27779 tree asm_specification;
27780 int ctor_dtor_or_conv_p;
27781 bool static_p = (decl_specifiers.storage_class == sc_static);
27782 cp_parser_flags flags = CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
27783 /* We can't delay parsing for friends,
27784 alias-declarations, and typedefs, even though the
27785 standard seems to require it. */
27786 if (!friend_p
27787 && !decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
27788 flags |= CP_PARSER_FLAGS_DELAY_NOEXCEPT;
27790 /* Parse the declarator. */
27791 declarator
27792 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
27793 flags,
27794 &ctor_dtor_or_conv_p,
27795 /*parenthesized_p=*/NULL,
27796 /*member_p=*/true,
27797 friend_p, static_p);
27799 /* If something went wrong parsing the declarator, make sure
27800 that we at least consume some tokens. */
27801 if (declarator == cp_error_declarator)
27803 /* Skip to the end of the statement. */
27804 cp_parser_skip_to_end_of_statement (parser);
27805 /* If the next token is not a semicolon, that is
27806 probably because we just skipped over the body of
27807 a function. So, we consume a semicolon if
27808 present, but do not issue an error message if it
27809 is not present. */
27810 if (cp_lexer_next_token_is (parser->lexer,
27811 CPP_SEMICOLON))
27812 cp_lexer_consume_token (parser->lexer);
27813 goto out;
27816 /* Handle class-scope non-template C++17 deduction guides. */
27817 cp_parser_maybe_adjust_declarator_for_dguide (parser,
27818 &decl_specifiers,
27819 declarator,
27820 &ctor_dtor_or_conv_p);
27822 if (declares_class_or_enum & 2)
27823 cp_parser_check_for_definition_in_return_type
27824 (declarator, decl_specifiers.type,
27825 decl_specifiers.locations[ds_type_spec]);
27827 /* Look for an asm-specification. */
27828 asm_specification = cp_parser_asm_specification_opt (parser);
27829 /* Look for attributes that apply to the declaration. */
27830 attributes = cp_parser_attributes_opt (parser);
27831 /* Remember which attributes are prefix attributes and
27832 which are not. */
27833 first_attribute = attributes;
27834 /* Combine the attributes. */
27835 attributes = attr_chainon (prefix_attributes, attributes);
27837 /* If it's an `=', then we have a constant-initializer or a
27838 pure-specifier. It is not correct to parse the
27839 initializer before registering the member declaration
27840 since the member declaration should be in scope while
27841 its initializer is processed. However, the rest of the
27842 front end does not yet provide an interface that allows
27843 us to handle this correctly. */
27844 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
27846 /* In [class.mem]:
27848 A pure-specifier shall be used only in the declaration of
27849 a virtual function.
27851 A member-declarator can contain a constant-initializer
27852 only if it declares a static member of integral or
27853 enumeration type.
27855 Therefore, if the DECLARATOR is for a function, we look
27856 for a pure-specifier; otherwise, we look for a
27857 constant-initializer. When we call `grokfield', it will
27858 perform more stringent semantics checks. */
27859 initializer_token_start = cp_lexer_peek_token (parser->lexer);
27860 declarator->init_loc = initializer_token_start->location;
27861 if (function_declarator_p (declarator)
27862 || (decl_specifiers.type
27863 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
27864 && declarator->kind == cdk_id
27865 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
27866 == FUNCTION_TYPE)))
27867 initializer = cp_parser_pure_specifier (parser);
27868 else if (decl_specifiers.storage_class != sc_static)
27869 initializer = cp_parser_save_nsdmi (parser);
27870 else if (cxx_dialect >= cxx11)
27872 /* Don't require a constant rvalue in C++11, since we
27873 might want a reference constant. We'll enforce
27874 constancy later. */
27875 cp_lexer_consume_token (parser->lexer);
27876 /* Parse the initializer. */
27877 initializer = cp_parser_initializer_clause (parser);
27879 else
27880 /* Parse the initializer. */
27881 initializer = cp_parser_constant_initializer (parser);
27883 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
27884 && !function_declarator_p (declarator))
27886 declarator->init_loc
27887 = cp_lexer_peek_token (parser->lexer)->location;
27888 if (decl_specifiers.storage_class != sc_static)
27889 initializer = cp_parser_save_nsdmi (parser);
27890 else
27891 initializer = cp_parser_initializer (parser);
27893 /* Detect invalid bit-field cases such as
27895 int *p : 4;
27896 int &&r : 3;
27898 and similar. */
27899 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
27900 /* If there were no type specifiers, it was a
27901 constructor. */
27902 && decl_specifiers.any_type_specifiers_p)
27904 /* This is called for a decent diagnostic only. */
27905 tree d = grokdeclarator (declarator, &decl_specifiers,
27906 BITFIELD, /*initialized=*/false,
27907 &attributes);
27908 if (!error_operand_p (d))
27909 error_at (DECL_SOURCE_LOCATION (d),
27910 "bit-field %qD has non-integral type %qT",
27911 d, TREE_TYPE (d));
27912 cp_parser_skip_to_end_of_statement (parser);
27913 /* Avoid "extra ;" pedwarns. */
27914 if (cp_lexer_next_token_is (parser->lexer,
27915 CPP_SEMICOLON))
27916 cp_lexer_consume_token (parser->lexer);
27917 goto out;
27919 /* Otherwise, there is no initializer. */
27920 else
27921 initializer = NULL_TREE;
27923 /* See if we are probably looking at a function
27924 definition. We are certainly not looking at a
27925 member-declarator. Calling `grokfield' has
27926 side-effects, so we must not do it unless we are sure
27927 that we are looking at a member-declarator. */
27928 if (cp_parser_token_starts_function_definition_p
27929 (cp_lexer_peek_token (parser->lexer)))
27931 /* The grammar does not allow a pure-specifier to be
27932 used when a member function is defined. (It is
27933 possible that this fact is an oversight in the
27934 standard, since a pure function may be defined
27935 outside of the class-specifier. */
27936 if (initializer && initializer_token_start)
27937 error_at (initializer_token_start->location,
27938 "pure-specifier on function-definition");
27939 decl = cp_parser_save_member_function_body (parser,
27940 &decl_specifiers,
27941 declarator,
27942 attributes);
27944 if (parser->fully_implicit_function_template_p)
27945 decl = finish_fully_implicit_template (parser, decl);
27946 /* If the member was not a friend, declare it here. */
27947 if (!friend_p)
27948 finish_member_declaration (decl);
27949 /* Peek at the next token. */
27950 token = cp_lexer_peek_token (parser->lexer);
27951 /* If the next token is a semicolon, consume it. */
27952 if (token->type == CPP_SEMICOLON)
27954 location_t semicolon_loc
27955 = cp_lexer_consume_token (parser->lexer)->location;
27956 gcc_rich_location richloc (semicolon_loc);
27957 richloc.add_fixit_remove ();
27958 warning_at (&richloc, OPT_Wextra_semi,
27959 "extra %<;%> after in-class "
27960 "function definition");
27962 goto out;
27964 else
27965 if (declarator->kind == cdk_function)
27966 declarator->id_loc = token->location;
27968 /* Create the declaration. */
27969 decl = grokfield (declarator, &decl_specifiers,
27970 initializer, /*init_const_expr_p=*/true,
27971 asm_specification, attributes);
27973 if (parser->fully_implicit_function_template_p)
27975 if (friend_p)
27976 finish_fully_implicit_template (parser, 0);
27977 else
27978 decl = finish_fully_implicit_template (parser, decl);
27982 cp_finalize_omp_declare_simd (parser, decl);
27983 cp_finalize_oacc_routine (parser, decl, false);
27985 /* Reset PREFIX_ATTRIBUTES. */
27986 if (attributes != error_mark_node)
27988 while (attributes && TREE_CHAIN (attributes) != first_attribute)
27989 attributes = TREE_CHAIN (attributes);
27990 if (attributes)
27991 TREE_CHAIN (attributes) = NULL_TREE;
27994 /* If there is any qualification still in effect, clear it
27995 now; we will be starting fresh with the next declarator. */
27996 parser->scope = NULL_TREE;
27997 parser->qualifying_scope = NULL_TREE;
27998 parser->object_scope = NULL_TREE;
27999 /* If it's a `,', then there are more declarators. */
28000 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28002 cp_lexer_consume_token (parser->lexer);
28003 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
28005 cp_token *token = cp_lexer_previous_token (parser->lexer);
28006 gcc_rich_location richloc (token->location);
28007 richloc.add_fixit_remove ();
28008 error_at (&richloc, "stray %<,%> at end of "
28009 "member declaration");
28012 /* If the next token isn't a `;', then we have a parse error. */
28013 else if (cp_lexer_next_token_is_not (parser->lexer,
28014 CPP_SEMICOLON))
28016 /* The next token might be a ways away from where the
28017 actual semicolon is missing. Find the previous token
28018 and use that for our error position. */
28019 cp_token *token = cp_lexer_previous_token (parser->lexer);
28020 gcc_rich_location richloc (token->location);
28021 richloc.add_fixit_insert_after (";");
28022 error_at (&richloc, "expected %<;%> at end of "
28023 "member declaration");
28025 /* Assume that the user meant to provide a semicolon. If
28026 we were to cp_parser_skip_to_end_of_statement, we might
28027 skip to a semicolon inside a member function definition
28028 and issue nonsensical error messages. */
28029 assume_semicolon = true;
28032 if (decl)
28034 /* Add DECL to the list of members. */
28035 if (!friend_p
28036 /* Explicitly include, eg, NSDMIs, for better error
28037 recovery (c++/58650). */
28038 || !DECL_DECLARES_FUNCTION_P (decl))
28039 finish_member_declaration (decl);
28041 if (DECL_DECLARES_FUNCTION_P (decl))
28042 cp_parser_save_default_args (parser, STRIP_TEMPLATE (decl));
28043 else if (TREE_CODE (decl) == FIELD_DECL
28044 && DECL_INITIAL (decl))
28045 /* Add DECL to the queue of NSDMI to be parsed later. */
28046 vec_safe_push (unparsed_nsdmis, decl);
28049 if (assume_semicolon)
28050 goto out;
28054 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
28055 out:
28056 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
28057 cp_finalize_omp_declare_simd (parser, &odsd);
28060 /* Parse a pure-specifier.
28062 pure-specifier:
28065 Returns INTEGER_ZERO_NODE if a pure specifier is found.
28066 Otherwise, ERROR_MARK_NODE is returned. */
28068 static tree
28069 cp_parser_pure_specifier (cp_parser* parser)
28071 cp_token *token;
28073 /* Look for the `=' token. */
28074 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28075 return error_mark_node;
28076 /* Look for the `0' token. */
28077 token = cp_lexer_peek_token (parser->lexer);
28079 if (token->type == CPP_EOF
28080 || token->type == CPP_PRAGMA_EOL)
28081 return error_mark_node;
28083 cp_lexer_consume_token (parser->lexer);
28085 /* Accept = default or = delete in c++0x mode. */
28086 if (token->keyword == RID_DEFAULT
28087 || token->keyword == RID_DELETE)
28089 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
28090 return token->u.value;
28093 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
28094 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
28096 cp_parser_error (parser,
28097 "invalid pure specifier (only %<= 0%> is allowed)");
28098 cp_parser_skip_to_end_of_statement (parser);
28099 return error_mark_node;
28101 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
28103 error_at (token->location, "templates may not be %<virtual%>");
28104 return error_mark_node;
28107 return integer_zero_node;
28110 /* Parse a constant-initializer.
28112 constant-initializer:
28113 = constant-expression
28115 Returns a representation of the constant-expression. */
28117 static tree
28118 cp_parser_constant_initializer (cp_parser* parser)
28120 /* Look for the `=' token. */
28121 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28122 return error_mark_node;
28124 /* It is invalid to write:
28126 struct S { static const int i = { 7 }; };
28129 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28131 cp_parser_error (parser,
28132 "a brace-enclosed initializer is not allowed here");
28133 /* Consume the opening brace. */
28134 matching_braces braces;
28135 braces.consume_open (parser);
28136 /* Skip the initializer. */
28137 cp_parser_skip_to_closing_brace (parser);
28138 /* Look for the trailing `}'. */
28139 braces.require_close (parser);
28141 return error_mark_node;
28144 return cp_parser_constant_expression (parser);
28147 /* Derived classes [gram.class.derived] */
28149 /* Parse a base-clause.
28151 base-clause:
28152 : base-specifier-list
28154 base-specifier-list:
28155 base-specifier ... [opt]
28156 base-specifier-list , base-specifier ... [opt]
28158 Returns a TREE_LIST representing the base-classes, in the order in
28159 which they were declared. The representation of each node is as
28160 described by cp_parser_base_specifier.
28162 In the case that no bases are specified, this function will return
28163 NULL_TREE, not ERROR_MARK_NODE. */
28165 static tree
28166 cp_parser_base_clause (cp_parser* parser)
28168 tree bases = NULL_TREE;
28170 /* Look for the `:' that begins the list. */
28171 cp_parser_require (parser, CPP_COLON, RT_COLON);
28173 /* Scan the base-specifier-list. */
28174 while (true)
28176 cp_token *token;
28177 tree base;
28178 bool pack_expansion_p = false;
28180 /* Look for the base-specifier. */
28181 base = cp_parser_base_specifier (parser);
28182 /* Look for the (optional) ellipsis. */
28183 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28185 /* Consume the `...'. */
28186 cp_lexer_consume_token (parser->lexer);
28188 pack_expansion_p = true;
28191 /* Add BASE to the front of the list. */
28192 if (base && base != error_mark_node)
28194 if (pack_expansion_p)
28195 /* Make this a pack expansion type. */
28196 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
28198 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
28200 TREE_CHAIN (base) = bases;
28201 bases = base;
28204 /* Peek at the next token. */
28205 token = cp_lexer_peek_token (parser->lexer);
28206 /* If it's not a comma, then the list is complete. */
28207 if (token->type != CPP_COMMA)
28208 break;
28209 /* Consume the `,'. */
28210 cp_lexer_consume_token (parser->lexer);
28213 /* PARSER->SCOPE may still be non-NULL at this point, if the last
28214 base class had a qualified name. However, the next name that
28215 appears is certainly not qualified. */
28216 parser->scope = NULL_TREE;
28217 parser->qualifying_scope = NULL_TREE;
28218 parser->object_scope = NULL_TREE;
28220 return nreverse (bases);
28223 /* Parse a base-specifier.
28225 base-specifier:
28226 :: [opt] nested-name-specifier [opt] class-name
28227 virtual access-specifier [opt] :: [opt] nested-name-specifier
28228 [opt] class-name
28229 access-specifier virtual [opt] :: [opt] nested-name-specifier
28230 [opt] class-name
28232 Returns a TREE_LIST. The TREE_PURPOSE will be one of
28233 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
28234 indicate the specifiers provided. The TREE_VALUE will be a TYPE
28235 (or the ERROR_MARK_NODE) indicating the type that was specified. */
28237 static tree
28238 cp_parser_base_specifier (cp_parser* parser)
28240 cp_token *token;
28241 bool done = false;
28242 bool virtual_p = false;
28243 bool duplicate_virtual_error_issued_p = false;
28244 bool duplicate_access_error_issued_p = false;
28245 bool class_scope_p, template_p;
28246 tree access = access_default_node;
28247 tree type;
28249 /* Process the optional `virtual' and `access-specifier'. */
28250 while (!done)
28252 /* Peek at the next token. */
28253 token = cp_lexer_peek_token (parser->lexer);
28254 /* Process `virtual'. */
28255 switch (token->keyword)
28257 case RID_VIRTUAL:
28258 /* If `virtual' appears more than once, issue an error. */
28259 if (virtual_p && !duplicate_virtual_error_issued_p)
28261 cp_parser_error (parser,
28262 "%<virtual%> specified more than once in base-specifier");
28263 duplicate_virtual_error_issued_p = true;
28266 virtual_p = true;
28268 /* Consume the `virtual' token. */
28269 cp_lexer_consume_token (parser->lexer);
28271 break;
28273 case RID_PUBLIC:
28274 case RID_PROTECTED:
28275 case RID_PRIVATE:
28276 /* If more than one access specifier appears, issue an
28277 error. */
28278 if (access != access_default_node
28279 && !duplicate_access_error_issued_p)
28281 cp_parser_error (parser,
28282 "more than one access specifier in base-specifier");
28283 duplicate_access_error_issued_p = true;
28286 access = ridpointers[(int) token->keyword];
28288 /* Consume the access-specifier. */
28289 cp_lexer_consume_token (parser->lexer);
28291 break;
28293 default:
28294 done = true;
28295 break;
28298 /* It is not uncommon to see programs mechanically, erroneously, use
28299 the 'typename' keyword to denote (dependent) qualified types
28300 as base classes. */
28301 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
28303 token = cp_lexer_peek_token (parser->lexer);
28304 if (!processing_template_decl)
28305 error_at (token->location,
28306 "keyword %<typename%> not allowed outside of templates");
28307 else
28308 error_at (token->location,
28309 "keyword %<typename%> not allowed in this context "
28310 "(the base class is implicitly a type)");
28311 cp_lexer_consume_token (parser->lexer);
28314 /* Look for the optional `::' operator. */
28315 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
28316 /* Look for the nested-name-specifier. The simplest way to
28317 implement:
28319 [temp.res]
28321 The keyword `typename' is not permitted in a base-specifier or
28322 mem-initializer; in these contexts a qualified name that
28323 depends on a template-parameter is implicitly assumed to be a
28324 type name.
28326 is to pretend that we have seen the `typename' keyword at this
28327 point. */
28328 cp_parser_nested_name_specifier_opt (parser,
28329 /*typename_keyword_p=*/true,
28330 /*check_dependency_p=*/true,
28331 /*type_p=*/true,
28332 /*is_declaration=*/true);
28333 /* If the base class is given by a qualified name, assume that names
28334 we see are type names or templates, as appropriate. */
28335 class_scope_p = (parser->scope && TYPE_P (parser->scope));
28336 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
28338 if (!parser->scope
28339 && cp_lexer_next_token_is_decltype (parser->lexer))
28340 /* DR 950 allows decltype as a base-specifier. */
28341 type = cp_parser_decltype (parser);
28342 else
28344 /* Otherwise, look for the class-name. */
28345 type = cp_parser_class_name (parser,
28346 class_scope_p,
28347 template_p,
28348 typename_type,
28349 /*check_dependency_p=*/true,
28350 /*class_head_p=*/false,
28351 /*is_declaration=*/true);
28352 type = TREE_TYPE (type);
28355 if (type == error_mark_node)
28356 return error_mark_node;
28358 return finish_base_specifier (type, access, virtual_p);
28361 /* Exception handling [gram.exception] */
28363 /* Save the tokens that make up the noexcept-specifier for a member-function.
28364 Returns a DEFERRED_PARSE. */
28366 static tree
28367 cp_parser_save_noexcept (cp_parser *parser)
28369 cp_token *first = parser->lexer->next_token;
28370 /* We want everything up to, including, the final ')'. */
28371 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0);
28372 cp_token *last = parser->lexer->next_token;
28374 /* As with default arguments and NSDMIs, make use of DEFERRED_PARSE
28375 to carry the information we will need. */
28376 tree expr = make_node (DEFERRED_PARSE);
28377 /* Save away the noexcept-specifier; we will process it when the
28378 class is complete. */
28379 DEFPARSE_TOKENS (expr) = cp_token_cache_new (first, last);
28380 DEFPARSE_INSTANTIATIONS (expr) = nullptr;
28381 expr = build_tree_list (expr, NULL_TREE);
28382 return expr;
28385 /* Used for late processing of noexcept-specifiers of member-functions.
28386 DEFAULT_ARG is the unparsed operand of a noexcept-specifier which
28387 we saved for later; parse it now. DECL is the declaration of the
28388 member function. */
28390 static tree
28391 cp_parser_late_noexcept_specifier (cp_parser *parser, tree default_arg)
28393 /* Make sure we've gotten something that hasn't been parsed yet. */
28394 gcc_assert (TREE_CODE (default_arg) == DEFERRED_PARSE);
28396 push_unparsed_function_queues (parser);
28398 /* Push the saved tokens for the noexcept-specifier onto the parser's
28399 lexer stack. */
28400 cp_token_cache *tokens = DEFPARSE_TOKENS (default_arg);
28401 cp_parser_push_lexer_for_tokens (parser, tokens);
28403 /* Parse the cached noexcept-specifier. */
28404 tree parsed_arg
28405 = cp_parser_noexcept_specification_opt (parser,
28406 CP_PARSER_FLAGS_NONE,
28407 /*require_constexpr=*/true,
28408 /*consumed_expr=*/NULL,
28409 /*return_cond=*/false);
28411 /* Revert to the main lexer. */
28412 cp_parser_pop_lexer (parser);
28414 /* Restore the queue. */
28415 pop_unparsed_function_queues (parser);
28417 /* And we're done. */
28418 return parsed_arg;
28421 /* Perform late checking of overriding function with respect to their
28422 noexcept-specifiers. FNDECL is the member function that potentially
28423 overrides some virtual function with the same signature. */
28425 static void
28426 noexcept_override_late_checks (tree fndecl)
28428 tree binfo = TYPE_BINFO (DECL_CONTEXT (fndecl));
28429 tree base_binfo;
28431 if (DECL_STATIC_FUNCTION_P (fndecl))
28432 return;
28434 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
28436 tree basetype = BINFO_TYPE (base_binfo);
28438 if (!TYPE_POLYMORPHIC_P (basetype))
28439 continue;
28441 tree fn = look_for_overrides_here (basetype, fndecl);
28442 if (fn)
28443 maybe_check_overriding_exception_spec (fndecl, fn);
28447 /* Parse an (optional) noexcept-specification.
28449 noexcept-specification:
28450 noexcept ( constant-expression ) [opt]
28452 If no noexcept-specification is present, returns NULL_TREE.
28453 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
28454 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
28455 there are no parentheses. CONSUMED_EXPR will be set accordingly.
28456 Otherwise, returns a noexcept specification unless RETURN_COND is true,
28457 in which case a boolean condition is returned instead. The parser flags
28458 FLAGS is used to control parsing. QUALS are qualifiers indicating whether
28459 the (member) function is `const'. */
28461 static tree
28462 cp_parser_noexcept_specification_opt (cp_parser* parser,
28463 cp_parser_flags flags,
28464 bool require_constexpr,
28465 bool* consumed_expr,
28466 bool return_cond)
28468 cp_token *token;
28469 const char *saved_message;
28471 /* Peek at the next token. */
28472 token = cp_lexer_peek_token (parser->lexer);
28474 /* Is it a noexcept-specification? */
28475 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
28477 tree expr;
28479 /* [class.mem]/6 says that a noexcept-specifer (within the
28480 member-specification of the class) is a complete-class context of
28481 a class. So, if the noexcept-specifier has the optional expression,
28482 just save the tokens, and reparse this after we're done with the
28483 class. */
28485 if ((flags & CP_PARSER_FLAGS_DELAY_NOEXCEPT)
28486 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN)
28487 /* No need to delay parsing for a number literal or true/false. */
28488 && !((cp_lexer_nth_token_is (parser->lexer, 3, CPP_NUMBER)
28489 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
28490 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_PAREN))
28491 && at_class_scope_p ()
28492 && TYPE_BEING_DEFINED (current_class_type)
28493 && !LAMBDA_TYPE_P (current_class_type))
28494 return cp_parser_save_noexcept (parser);
28496 cp_lexer_consume_token (parser->lexer);
28498 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
28500 matching_parens parens;
28501 parens.consume_open (parser);
28503 if (require_constexpr)
28505 /* Types may not be defined in an exception-specification. */
28506 saved_message = parser->type_definition_forbidden_message;
28507 parser->type_definition_forbidden_message
28508 = G_("types may not be defined in an exception-specification");
28510 bool non_constant_p;
28511 expr
28512 = cp_parser_constant_expression (parser,
28513 /*allow_non_constant=*/true,
28514 &non_constant_p);
28515 if (non_constant_p
28516 && !require_potential_rvalue_constant_expression (expr))
28518 expr = NULL_TREE;
28519 return_cond = true;
28522 /* Restore the saved message. */
28523 parser->type_definition_forbidden_message = saved_message;
28525 else
28527 expr = cp_parser_expression (parser);
28528 *consumed_expr = true;
28531 parens.require_close (parser);
28533 else
28535 expr = boolean_true_node;
28536 if (!require_constexpr)
28537 *consumed_expr = false;
28540 /* We cannot build a noexcept-spec right away because this will check
28541 that expr is a constexpr. */
28542 if (!return_cond)
28543 return build_noexcept_spec (expr, tf_warning_or_error);
28544 else
28545 return expr;
28547 else
28548 return NULL_TREE;
28551 /* Parse an (optional) exception-specification.
28553 exception-specification:
28554 throw ( type-id-list [opt] )
28556 Returns a TREE_LIST representing the exception-specification. The
28557 TREE_VALUE of each node is a type. The parser flags FLAGS is used to
28558 control parsing. QUALS are qualifiers indicating whether the (member)
28559 function is `const'. */
28561 static tree
28562 cp_parser_exception_specification_opt (cp_parser* parser,
28563 cp_parser_flags flags)
28565 cp_token *token;
28566 tree type_id_list;
28567 const char *saved_message;
28569 /* Peek at the next token. */
28570 token = cp_lexer_peek_token (parser->lexer);
28572 /* Is it a noexcept-specification? */
28573 type_id_list
28574 = cp_parser_noexcept_specification_opt (parser, flags,
28575 /*require_constexpr=*/true,
28576 /*consumed_expr=*/NULL,
28577 /*return_cond=*/false);
28578 if (type_id_list != NULL_TREE)
28579 return type_id_list;
28581 /* If it's not `throw', then there's no exception-specification. */
28582 if (!cp_parser_is_keyword (token, RID_THROW))
28583 return NULL_TREE;
28585 location_t loc = token->location;
28587 /* Consume the `throw'. */
28588 cp_lexer_consume_token (parser->lexer);
28590 /* Look for the `('. */
28591 matching_parens parens;
28592 parens.require_open (parser);
28594 /* Peek at the next token. */
28595 token = cp_lexer_peek_token (parser->lexer);
28596 /* If it's not a `)', then there is a type-id-list. */
28597 if (token->type != CPP_CLOSE_PAREN)
28599 /* Types may not be defined in an exception-specification. */
28600 saved_message = parser->type_definition_forbidden_message;
28601 parser->type_definition_forbidden_message
28602 = G_("types may not be defined in an exception-specification");
28603 /* Parse the type-id-list. */
28604 type_id_list = cp_parser_type_id_list (parser);
28605 /* Restore the saved message. */
28606 parser->type_definition_forbidden_message = saved_message;
28608 if (cxx_dialect >= cxx17)
28610 error_at (loc, "ISO C++17 does not allow dynamic exception "
28611 "specifications");
28612 type_id_list = NULL_TREE;
28614 else if (cxx_dialect >= cxx11)
28615 warning_at (loc, OPT_Wdeprecated,
28616 "dynamic exception specifications are deprecated in "
28617 "C++11");
28619 /* In C++17, throw() is equivalent to noexcept (true). throw()
28620 is deprecated in C++11 and above as well, but is still widely used,
28621 so don't warn about it yet. */
28622 else if (cxx_dialect >= cxx17)
28623 type_id_list = noexcept_true_spec;
28624 else
28625 type_id_list = empty_except_spec;
28627 /* Look for the `)'. */
28628 parens.require_close (parser);
28630 return type_id_list;
28633 /* Parse an (optional) type-id-list.
28635 type-id-list:
28636 type-id ... [opt]
28637 type-id-list , type-id ... [opt]
28639 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
28640 in the order that the types were presented. */
28642 static tree
28643 cp_parser_type_id_list (cp_parser* parser)
28645 tree types = NULL_TREE;
28647 while (true)
28649 cp_token *token;
28650 tree type;
28652 token = cp_lexer_peek_token (parser->lexer);
28654 /* Get the next type-id. */
28655 type = cp_parser_type_id (parser);
28656 /* Check for invalid 'auto'. */
28657 if (flag_concepts && type_uses_auto (type))
28659 error_at (token->location,
28660 "invalid use of %<auto%> in exception-specification");
28661 type = error_mark_node;
28663 /* Parse the optional ellipsis. */
28664 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28666 /* Consume the `...'. */
28667 cp_lexer_consume_token (parser->lexer);
28669 /* Turn the type into a pack expansion expression. */
28670 type = make_pack_expansion (type);
28672 /* Add it to the list. */
28673 types = add_exception_specifier (types, type, /*complain=*/1);
28674 /* Peek at the next token. */
28675 token = cp_lexer_peek_token (parser->lexer);
28676 /* If it is not a `,', we are done. */
28677 if (token->type != CPP_COMMA)
28678 break;
28679 /* Consume the `,'. */
28680 cp_lexer_consume_token (parser->lexer);
28683 return nreverse (types);
28686 /* Parse a try-block.
28688 try-block:
28689 try compound-statement handler-seq */
28691 static tree
28692 cp_parser_try_block (cp_parser* parser)
28694 tree try_block;
28696 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
28697 if (parser->in_function_body
28698 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
28699 && cxx_dialect < cxx20)
28700 pedwarn (input_location, OPT_Wc__20_extensions,
28701 "%<try%> in %<constexpr%> function only "
28702 "available with %<-std=c++20%> or %<-std=gnu++20%>");
28704 try_block = begin_try_block ();
28705 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
28706 finish_try_block (try_block);
28707 cp_parser_handler_seq (parser);
28708 finish_handler_sequence (try_block);
28710 return try_block;
28713 /* Parse a function-try-block.
28715 function-try-block:
28716 try ctor-initializer [opt] function-body handler-seq */
28718 static void
28719 cp_parser_function_try_block (cp_parser* parser)
28721 tree compound_stmt;
28722 tree try_block;
28724 /* Look for the `try' keyword. */
28725 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
28726 return;
28727 /* Let the rest of the front end know where we are. */
28728 try_block = begin_function_try_block (&compound_stmt);
28729 /* Parse the function-body. */
28730 cp_parser_ctor_initializer_opt_and_function_body
28731 (parser, /*in_function_try_block=*/true);
28732 /* We're done with the `try' part. */
28733 finish_function_try_block (try_block);
28734 /* Parse the handlers. */
28735 cp_parser_handler_seq (parser);
28736 /* We're done with the handlers. */
28737 finish_function_handler_sequence (try_block, compound_stmt);
28740 /* Parse a handler-seq.
28742 handler-seq:
28743 handler handler-seq [opt] */
28745 static void
28746 cp_parser_handler_seq (cp_parser* parser)
28748 while (true)
28750 cp_token *token;
28752 /* Parse the handler. */
28753 cp_parser_handler (parser);
28754 /* Peek at the next token. */
28755 token = cp_lexer_peek_token (parser->lexer);
28756 /* If it's not `catch' then there are no more handlers. */
28757 if (!cp_parser_is_keyword (token, RID_CATCH))
28758 break;
28762 /* Parse a handler.
28764 handler:
28765 catch ( exception-declaration ) compound-statement */
28767 static void
28768 cp_parser_handler (cp_parser* parser)
28770 tree handler;
28771 tree declaration;
28773 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
28774 handler = begin_handler ();
28775 matching_parens parens;
28776 parens.require_open (parser);
28777 declaration = cp_parser_exception_declaration (parser);
28778 finish_handler_parms (declaration, handler);
28779 parens.require_close (parser);
28780 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
28781 finish_handler (handler);
28784 /* Parse an exception-declaration.
28786 exception-declaration:
28787 type-specifier-seq declarator
28788 type-specifier-seq abstract-declarator
28789 type-specifier-seq
28792 Returns a VAR_DECL for the declaration, or NULL_TREE if the
28793 ellipsis variant is used. */
28795 static tree
28796 cp_parser_exception_declaration (cp_parser* parser)
28798 cp_decl_specifier_seq type_specifiers;
28799 cp_declarator *declarator;
28800 const char *saved_message;
28802 /* If it's an ellipsis, it's easy to handle. */
28803 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28805 /* Consume the `...' token. */
28806 cp_lexer_consume_token (parser->lexer);
28807 return NULL_TREE;
28810 /* Types may not be defined in exception-declarations. */
28811 saved_message = parser->type_definition_forbidden_message;
28812 parser->type_definition_forbidden_message
28813 = G_("types may not be defined in exception-declarations");
28815 /* Parse the type-specifier-seq. */
28816 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
28817 /*is_declaration=*/true,
28818 /*is_trailing_return=*/false,
28819 &type_specifiers);
28820 /* If it's a `)', then there is no declarator. */
28821 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
28822 declarator = NULL;
28823 else
28824 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
28825 CP_PARSER_FLAGS_NONE,
28826 /*ctor_dtor_or_conv_p=*/NULL,
28827 /*parenthesized_p=*/NULL,
28828 /*member_p=*/false,
28829 /*friend_p=*/false,
28830 /*static_p=*/false);
28832 /* Restore the saved message. */
28833 parser->type_definition_forbidden_message = saved_message;
28835 if (!type_specifiers.any_specifiers_p)
28836 return error_mark_node;
28838 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
28841 /* Parse a throw-expression.
28843 throw-expression:
28844 throw assignment-expression [opt]
28846 Returns a THROW_EXPR representing the throw-expression. */
28848 static tree
28849 cp_parser_throw_expression (cp_parser* parser)
28851 tree expression;
28852 cp_token* token;
28853 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28855 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
28856 token = cp_lexer_peek_token (parser->lexer);
28857 /* Figure out whether or not there is an assignment-expression
28858 following the "throw" keyword. */
28859 if (token->type == CPP_COMMA
28860 || token->type == CPP_SEMICOLON
28861 || token->type == CPP_CLOSE_PAREN
28862 || token->type == CPP_CLOSE_SQUARE
28863 || token->type == CPP_CLOSE_BRACE
28864 || token->type == CPP_COLON)
28865 expression = NULL_TREE;
28866 else
28867 expression = cp_parser_assignment_expression (parser);
28869 /* Construct a location e.g.:
28870 throw x
28871 ^~~~~~~
28872 with caret == start at the start of the "throw" token, and
28873 the end at the end of the final token we consumed. */
28874 location_t combined_loc = make_location (start_loc, start_loc,
28875 parser->lexer);
28876 expression = build_throw (combined_loc, expression);
28878 return expression;
28881 /* Parse a yield-expression.
28883 yield-expression:
28884 co_yield assignment-expression
28885 co_yield braced-init-list
28887 Returns a CO_YIELD_EXPR representing the yield-expression. */
28889 static tree
28890 cp_parser_yield_expression (cp_parser* parser)
28892 tree expr;
28894 cp_token *token = cp_lexer_peek_token (parser->lexer);
28895 location_t kw_loc = token->location; /* Save for later. */
28897 cp_parser_require_keyword (parser, RID_CO_YIELD, RT_CO_YIELD);
28899 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28901 cp_lexer_set_source_position (parser->lexer);
28902 /* ??? : probably a moot point? */
28903 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
28904 expr = cp_parser_braced_list (parser);
28906 else
28907 expr = cp_parser_assignment_expression (parser);
28909 if (expr == error_mark_node)
28910 return expr;
28912 return finish_co_yield_expr (kw_loc, expr);
28915 /* GNU Extensions */
28917 /* Parse an (optional) asm-specification.
28919 asm-specification:
28920 asm ( string-literal )
28922 If the asm-specification is present, returns a STRING_CST
28923 corresponding to the string-literal. Otherwise, returns
28924 NULL_TREE. */
28926 static tree
28927 cp_parser_asm_specification_opt (cp_parser* parser)
28929 /* Peek at the next token. */
28930 cp_token *token = cp_lexer_peek_token (parser->lexer);
28931 /* If the next token isn't the `asm' keyword, then there's no
28932 asm-specification. */
28933 if (!cp_parser_is_keyword (token, RID_ASM))
28934 return NULL_TREE;
28936 /* Consume the `asm' token. */
28937 cp_lexer_consume_token (parser->lexer);
28938 /* Look for the `('. */
28939 matching_parens parens;
28940 parens.require_open (parser);
28942 /* Look for the string-literal. */
28943 tree asm_specification = cp_parser_string_literal (parser,
28944 /*translate=*/false,
28945 /*wide_ok=*/false);
28947 /* Look for the `)'. */
28948 parens.require_close (parser);
28950 return asm_specification;
28953 /* Parse an asm-operand-list.
28955 asm-operand-list:
28956 asm-operand
28957 asm-operand-list , asm-operand
28959 asm-operand:
28960 string-literal ( expression )
28961 [ string-literal ] string-literal ( expression )
28963 Returns a TREE_LIST representing the operands. The TREE_VALUE of
28964 each node is the expression. The TREE_PURPOSE is itself a
28965 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
28966 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
28967 is a STRING_CST for the string literal before the parenthesis. Returns
28968 ERROR_MARK_NODE if any of the operands are invalid. */
28970 static tree
28971 cp_parser_asm_operand_list (cp_parser* parser)
28973 tree asm_operands = NULL_TREE;
28974 bool invalid_operands = false;
28976 while (true)
28978 tree name;
28980 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
28982 /* Consume the `[' token. */
28983 cp_lexer_consume_token (parser->lexer);
28984 /* Read the operand name. */
28985 name = cp_parser_identifier (parser);
28986 if (name != error_mark_node)
28987 name = build_string (IDENTIFIER_LENGTH (name),
28988 IDENTIFIER_POINTER (name));
28989 /* Look for the closing `]'. */
28990 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
28992 else
28993 name = NULL_TREE;
28994 /* Look for the string-literal. */
28995 tree string_literal = cp_parser_string_literal (parser,
28996 /*translate=*/false,
28997 /*wide_ok=*/false);
28999 /* Look for the `('. */
29000 matching_parens parens;
29001 parens.require_open (parser);
29002 /* Parse the expression. */
29003 tree expression = cp_parser_expression (parser);
29004 /* Look for the `)'. */
29005 parens.require_close (parser);
29007 if (name == error_mark_node
29008 || string_literal == error_mark_node
29009 || expression == error_mark_node)
29010 invalid_operands = true;
29012 /* Add this operand to the list. */
29013 asm_operands = tree_cons (build_tree_list (name, string_literal),
29014 expression,
29015 asm_operands);
29016 /* If the next token is not a `,', there are no more
29017 operands. */
29018 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
29019 break;
29020 /* Consume the `,'. */
29021 cp_lexer_consume_token (parser->lexer);
29024 return invalid_operands ? error_mark_node : nreverse (asm_operands);
29027 /* Parse an asm-clobber-list.
29029 asm-clobber-list:
29030 string-literal
29031 asm-clobber-list , string-literal
29033 Returns a TREE_LIST, indicating the clobbers in the order that they
29034 appeared. The TREE_VALUE of each node is a STRING_CST. */
29036 static tree
29037 cp_parser_asm_clobber_list (cp_parser* parser)
29039 tree clobbers = NULL_TREE;
29041 while (true)
29043 /* Look for the string literal. */
29044 tree string_literal = cp_parser_string_literal (parser,
29045 /*translate=*/false,
29046 /*wide_ok=*/false);
29047 /* Add it to the list. */
29048 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
29049 /* If the next token is not a `,', then the list is
29050 complete. */
29051 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
29052 break;
29053 /* Consume the `,' token. */
29054 cp_lexer_consume_token (parser->lexer);
29057 return clobbers;
29060 /* Parse an asm-label-list.
29062 asm-label-list:
29063 identifier
29064 asm-label-list , identifier
29066 Returns a TREE_LIST, indicating the labels in the order that they
29067 appeared. The TREE_VALUE of each node is a label. */
29069 static tree
29070 cp_parser_asm_label_list (cp_parser* parser)
29072 tree labels = NULL_TREE;
29074 while (true)
29076 tree identifier, label, name;
29078 /* Look for the identifier. */
29079 identifier = cp_parser_identifier (parser);
29080 if (!error_operand_p (identifier))
29082 label = lookup_label (identifier);
29083 if (TREE_CODE (label) == LABEL_DECL)
29085 TREE_USED (label) = 1;
29086 check_goto (label);
29087 name = build_string (IDENTIFIER_LENGTH (identifier),
29088 IDENTIFIER_POINTER (identifier));
29089 labels = tree_cons (name, label, labels);
29092 /* If the next token is not a `,', then the list is
29093 complete. */
29094 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
29095 break;
29096 /* Consume the `,' token. */
29097 cp_lexer_consume_token (parser->lexer);
29100 return nreverse (labels);
29103 /* Return TRUE iff the next tokens in the stream are possibly the
29104 beginning of a GNU extension attribute. */
29106 static bool
29107 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
29109 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
29112 /* Return TRUE iff the next tokens in the stream are possibly the
29113 beginning of a standard C++-11 attribute specifier. */
29115 static bool
29116 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
29118 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
29121 /* Return TRUE iff the next Nth tokens in the stream are possibly the
29122 beginning of a standard C++-11 attribute specifier. */
29124 static bool
29125 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
29127 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
29129 return (cxx_dialect >= cxx11
29130 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
29131 || (token->type == CPP_OPEN_SQUARE
29132 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
29133 && token->type == CPP_OPEN_SQUARE)));
29136 /* Return TRUE iff the next Nth tokens in the stream are possibly the
29137 beginning of a GNU extension attribute. */
29139 static bool
29140 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
29142 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
29144 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
29147 /* Return true iff the next tokens can be the beginning of either a
29148 GNU attribute list, or a standard C++11 attribute sequence. */
29150 static bool
29151 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
29153 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
29154 || cp_next_tokens_can_be_std_attribute_p (parser));
29157 /* Return true iff the next Nth tokens can be the beginning of either
29158 a GNU attribute list, or a standard C++11 attribute sequence. */
29160 static bool
29161 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
29163 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
29164 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
29167 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
29168 of GNU attributes, or return NULL. */
29170 static tree
29171 cp_parser_attributes_opt (cp_parser *parser)
29173 tree attrs = NULL_TREE;
29174 while (true)
29176 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
29177 attrs = attr_chainon (attrs, cp_parser_gnu_attributes_opt (parser));
29178 else if (cp_next_tokens_can_be_std_attribute_p (parser))
29179 attrs = attr_chainon (attrs, cp_parser_std_attribute_spec_seq (parser));
29180 else
29181 break;
29183 return attrs;
29186 /* Parse an (optional) series of attributes.
29188 attributes:
29189 attributes attribute
29191 attribute:
29192 __attribute__ (( attribute-list [opt] ))
29194 The return value is as for cp_parser_gnu_attribute_list. */
29196 static tree
29197 cp_parser_gnu_attributes_opt (cp_parser* parser)
29199 tree attributes = NULL_TREE;
29201 auto cleanup = make_temp_override
29202 (parser->auto_is_implicit_function_template_parm_p, false);
29204 while (true)
29206 cp_token *token;
29207 tree attribute_list;
29208 bool ok = true;
29210 /* Peek at the next token. */
29211 token = cp_lexer_peek_token (parser->lexer);
29212 /* If it's not `__attribute__', then we're done. */
29213 if (token->keyword != RID_ATTRIBUTE)
29214 break;
29216 /* Consume the `__attribute__' keyword. */
29217 cp_lexer_consume_token (parser->lexer);
29218 /* Look for the two `(' tokens. */
29219 matching_parens outer_parens;
29220 if (!outer_parens.require_open (parser))
29221 ok = false;
29222 matching_parens inner_parens;
29223 if (!inner_parens.require_open (parser))
29224 ok = false;
29226 /* Peek at the next token. */
29227 token = cp_lexer_peek_token (parser->lexer);
29228 if (token->type != CPP_CLOSE_PAREN)
29229 /* Parse the attribute-list. */
29230 attribute_list = cp_parser_gnu_attribute_list (parser);
29231 else
29232 /* If the next token is a `)', then there is no attribute
29233 list. */
29234 attribute_list = NULL;
29236 /* Look for the two `)' tokens. */
29237 if (!inner_parens.require_close (parser))
29238 ok = false;
29239 if (!outer_parens.require_close (parser))
29240 ok = false;
29241 if (!ok)
29242 cp_parser_skip_to_end_of_statement (parser);
29244 /* Add these new attributes to the list. */
29245 attributes = attr_chainon (attributes, attribute_list);
29248 return attributes;
29251 /* Parse a GNU attribute-list.
29253 attribute-list:
29254 attribute
29255 attribute-list , attribute
29257 attribute:
29258 identifier
29259 identifier ( identifier )
29260 identifier ( identifier , expression-list )
29261 identifier ( expression-list )
29263 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
29264 to an attribute. The TREE_PURPOSE of each node is the identifier
29265 indicating which attribute is in use. The TREE_VALUE represents
29266 the arguments, if any. */
29268 static tree
29269 cp_parser_gnu_attribute_list (cp_parser* parser, bool exactly_one /* = false */)
29271 tree attribute_list = NULL_TREE;
29272 bool save_translate_strings_p = parser->translate_strings_p;
29274 /* Don't create wrapper nodes within attributes: the
29275 handlers don't know how to handle them. */
29276 auto_suppress_location_wrappers sentinel;
29278 parser->translate_strings_p = false;
29279 while (true)
29281 cp_token *token;
29282 tree identifier;
29283 tree attribute;
29285 /* Look for the identifier. We also allow keywords here; for
29286 example `__attribute__ ((const))' is legal. */
29287 token = cp_lexer_peek_token (parser->lexer);
29288 if (token->type == CPP_NAME
29289 || token->type == CPP_KEYWORD)
29291 tree arguments = NULL_TREE;
29293 /* Consume the token, but save it since we need it for the
29294 SIMD enabled function parsing. */
29295 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
29297 /* Save away the identifier that indicates which attribute
29298 this is. */
29299 identifier = (token->type == CPP_KEYWORD)
29300 /* For keywords, use the canonical spelling, not the
29301 parsed identifier. */
29302 ? ridpointers[(int) token->keyword]
29303 : id_token->u.value;
29305 identifier = canonicalize_attr_name (identifier);
29306 attribute = build_tree_list (identifier, NULL_TREE);
29308 /* Peek at the next token. */
29309 token = cp_lexer_peek_token (parser->lexer);
29310 /* If it's an `(', then parse the attribute arguments. */
29311 if (token->type == CPP_OPEN_PAREN)
29313 vec<tree, va_gc> *vec;
29314 int attr_flag = (attribute_takes_identifier_p (identifier)
29315 ? id_attr : normal_attr);
29316 if (is_attribute_p ("assume", identifier))
29317 attr_flag = assume_attr;
29318 vec = cp_parser_parenthesized_expression_list
29319 (parser, attr_flag, /*cast_p=*/false,
29320 /*allow_expansion_p=*/false,
29321 /*non_constant_p=*/NULL);
29322 if (vec == NULL)
29323 arguments = error_mark_node;
29324 else
29326 arguments = build_tree_list_vec (vec);
29327 release_tree_vector (vec);
29329 /* Save the arguments away. */
29330 TREE_VALUE (attribute) = arguments;
29333 if (arguments != error_mark_node)
29335 /* Add this attribute to the list. */
29336 TREE_CHAIN (attribute) = attribute_list;
29337 attribute_list = attribute;
29340 token = cp_lexer_peek_token (parser->lexer);
29342 /* Unless EXACTLY_ONE is set look for more attributes.
29343 If the next token isn't a `,', we're done. */
29344 if (exactly_one || token->type != CPP_COMMA)
29345 break;
29347 /* Consume the comma and keep going. */
29348 cp_lexer_consume_token (parser->lexer);
29350 parser->translate_strings_p = save_translate_strings_p;
29352 /* We built up the list in reverse order. */
29353 return nreverse (attribute_list);
29356 /* Parse arguments of omp::directive attribute.
29358 ( directive-name ,[opt] clause-list[opt] )
29360 For directive just remember the first/last tokens for subsequent
29361 parsing. */
29363 static void
29364 cp_parser_omp_directive_args (cp_parser *parser, tree attribute)
29366 cp_token *first = cp_lexer_peek_nth_token (parser->lexer, 2);
29367 if (first->type == CPP_CLOSE_PAREN)
29369 cp_lexer_consume_token (parser->lexer);
29370 error_at (first->location, "expected OpenMP directive name");
29371 cp_lexer_consume_token (parser->lexer);
29372 TREE_VALUE (attribute) = NULL_TREE;
29373 return;
29375 size_t n = cp_parser_skip_balanced_tokens (parser, 1);
29376 if (n == 1)
29378 cp_lexer_consume_token (parser->lexer);
29379 error_at (first->location, "expected attribute argument as balanced "
29380 "token sequence");
29381 TREE_VALUE (attribute) = NULL_TREE;
29382 return;
29384 for (n = n - 2; n; --n)
29385 cp_lexer_consume_token (parser->lexer);
29386 cp_token *last = cp_lexer_peek_token (parser->lexer);
29387 cp_lexer_consume_token (parser->lexer);
29388 tree arg = make_node (DEFERRED_PARSE);
29389 DEFPARSE_TOKENS (arg) = cp_token_cache_new (first, last);
29390 DEFPARSE_INSTANTIATIONS (arg) = nullptr;
29391 TREE_VALUE (attribute) = tree_cons (NULL_TREE, arg, TREE_VALUE (attribute));
29394 /* Parse arguments of omp::sequence attribute.
29396 ( omp::[opt] directive-attr [ , omp::[opt] directive-attr ]... ) */
29398 static void
29399 cp_parser_omp_sequence_args (cp_parser *parser, tree attribute)
29401 matching_parens parens;
29402 parens.consume_open (parser);
29405 cp_token *token = cp_lexer_peek_token (parser->lexer);
29406 if (token->type == CPP_NAME
29407 && token->u.value == omp_identifier
29408 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
29410 cp_lexer_consume_token (parser->lexer);
29411 cp_lexer_consume_token (parser->lexer);
29412 token = cp_lexer_peek_token (parser->lexer);
29414 bool directive = false;
29415 const char *p;
29416 if (token->type != CPP_NAME)
29417 p = "";
29418 else
29419 p = IDENTIFIER_POINTER (token->u.value);
29420 if (strcmp (p, "directive") == 0)
29421 directive = true;
29422 else if (strcmp (p, "sequence") != 0)
29424 error_at (token->location, "expected %<directive%> or %<sequence%>");
29425 cp_parser_skip_to_closing_parenthesis (parser,
29426 /*recovering=*/true,
29427 /*or_comma=*/true,
29428 /*consume_paren=*/false);
29429 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
29430 break;
29431 cp_lexer_consume_token (parser->lexer);
29433 cp_lexer_consume_token (parser->lexer);
29434 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
29435 cp_parser_required_error (parser, RT_OPEN_PAREN, false,
29436 UNKNOWN_LOCATION);
29437 else if (directive)
29438 cp_parser_omp_directive_args (parser, attribute);
29439 else
29440 cp_parser_omp_sequence_args (parser, attribute);
29441 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
29442 break;
29443 cp_lexer_consume_token (parser->lexer);
29445 while (1);
29446 if (!parens.require_close (parser))
29447 cp_parser_skip_to_closing_parenthesis (parser, true, false,
29448 /*consume_paren=*/true);
29451 /* Parse a standard C++11 attribute.
29453 The returned representation is a TREE_LIST which TREE_PURPOSE is
29454 the scoped name of the attribute, and the TREE_VALUE is its
29455 arguments list.
29457 Note that the scoped name of the attribute is itself a TREE_LIST
29458 which TREE_PURPOSE is the namespace of the attribute, and
29459 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
29460 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
29461 and which TREE_PURPOSE is directly the attribute name.
29463 Clients of the attribute code should use get_attribute_namespace
29464 and get_attribute_name to get the actual namespace and name of
29465 attributes, regardless of their being GNU or C++11 attributes.
29467 attribute:
29468 attribute-token attribute-argument-clause [opt]
29470 attribute-token:
29471 identifier
29472 attribute-scoped-token
29474 attribute-scoped-token:
29475 attribute-namespace :: identifier
29477 attribute-namespace:
29478 identifier
29480 attribute-argument-clause:
29481 ( balanced-token-seq )
29483 balanced-token-seq:
29484 balanced-token [opt]
29485 balanced-token-seq balanced-token
29487 balanced-token:
29488 ( balanced-token-seq )
29489 [ balanced-token-seq ]
29490 { balanced-token-seq }. */
29492 static tree
29493 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
29495 tree attribute, attr_id = NULL_TREE, arguments;
29496 cp_token *token;
29498 auto cleanup = make_temp_override
29499 (parser->auto_is_implicit_function_template_parm_p, false);
29501 /* First, parse name of the attribute, a.k.a attribute-token. */
29503 token = cp_lexer_peek_token (parser->lexer);
29504 if (token->type == CPP_NAME)
29505 attr_id = token->u.value;
29506 else if (token->type == CPP_KEYWORD)
29507 attr_id = ridpointers[(int) token->keyword];
29508 else if (token->flags & NAMED_OP)
29509 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
29511 if (attr_id == NULL_TREE)
29512 return NULL_TREE;
29514 cp_lexer_consume_token (parser->lexer);
29516 token = cp_lexer_peek_token (parser->lexer);
29517 if (token->type == CPP_SCOPE)
29519 /* We are seeing a scoped attribute token. */
29521 cp_lexer_consume_token (parser->lexer);
29522 if (attr_ns)
29523 error_at (token->location, "attribute using prefix used together "
29524 "with scoped attribute token");
29525 attr_ns = attr_id;
29527 token = cp_lexer_peek_token (parser->lexer);
29528 if (token->type == CPP_NAME)
29529 attr_id = token->u.value;
29530 else if (token->type == CPP_KEYWORD)
29531 attr_id = ridpointers[(int) token->keyword];
29532 else if (token->flags & NAMED_OP)
29533 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
29534 else
29536 error_at (token->location,
29537 "expected an identifier for the attribute name");
29538 return error_mark_node;
29540 cp_lexer_consume_token (parser->lexer);
29542 attr_ns = canonicalize_attr_name (attr_ns);
29543 attr_id = canonicalize_attr_name (attr_id);
29544 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
29545 NULL_TREE);
29546 token = cp_lexer_peek_token (parser->lexer);
29548 else if (attr_ns)
29550 attr_ns = canonicalize_attr_name (attr_ns);
29551 attr_id = canonicalize_attr_name (attr_id);
29552 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
29553 NULL_TREE);
29555 else
29557 attr_id = canonicalize_attr_name (attr_id);
29558 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
29559 NULL_TREE);
29561 /* We used to treat C++11 noreturn attribute as equivalent to GNU's,
29562 but no longer: we have to be able to tell [[noreturn]] and
29563 __attribute__((noreturn)) apart. */
29564 /* C++14 deprecated attribute is equivalent to GNU's. */
29565 if (is_attribute_p ("deprecated", attr_id))
29566 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
29567 /* C++17 fallthrough attribute is equivalent to GNU's. */
29568 else if (is_attribute_p ("fallthrough", attr_id))
29569 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
29570 /* C++23 assume attribute is equivalent to GNU's. */
29571 else if (is_attribute_p ("assume", attr_id))
29572 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
29573 /* Transactional Memory TS optimize_for_synchronized attribute is
29574 equivalent to GNU transaction_callable. */
29575 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
29576 TREE_PURPOSE (attribute)
29577 = get_identifier ("transaction_callable");
29578 /* Transactional Memory attributes are GNU attributes. */
29579 else if (tm_attr_to_mask (attr_id))
29580 TREE_PURPOSE (attribute) = attr_id;
29583 /* Now parse the optional argument clause of the attribute. */
29585 if (token->type != CPP_OPEN_PAREN)
29587 if ((flag_openmp || flag_openmp_simd)
29588 && attr_ns == omp_identifier
29589 && (is_attribute_p ("directive", attr_id)
29590 || is_attribute_p ("sequence", attr_id)))
29592 error_at (token->location, "%<omp::%E%> attribute requires argument",
29593 attr_id);
29594 return NULL_TREE;
29596 return attribute;
29600 vec<tree, va_gc> *vec;
29601 int attr_flag = normal_attr;
29603 /* Maybe we don't expect to see any arguments for this attribute. */
29604 const attribute_spec *as
29605 = lookup_attribute_spec (TREE_PURPOSE (attribute));
29606 if (as && as->max_length == 0)
29608 error_at (token->location, "%qE attribute does not take any arguments",
29609 attr_id);
29610 cp_parser_skip_to_closing_parenthesis (parser,
29611 /*recovering=*/true,
29612 /*or_comma=*/false,
29613 /*consume_paren=*/true);
29614 return error_mark_node;
29617 if (is_attribute_p ("assume", attr_id)
29618 && (attr_ns == NULL_TREE || attr_ns == gnu_identifier))
29619 /* The assume attribute needs special handling of the argument. */
29620 attr_flag = assume_attr;
29621 else if (attr_ns == gnu_identifier
29622 && attribute_takes_identifier_p (attr_id))
29623 /* A GNU attribute that takes an identifier in parameter. */
29624 attr_flag = id_attr;
29626 /* If this is a fake attribute created to handle -Wno-attributes,
29627 we must skip parsing the arguments. */
29628 if (as == NULL || attribute_ignored_p (as))
29630 if ((flag_openmp || flag_openmp_simd) && attr_ns == omp_identifier)
29632 if (is_attribute_p ("directive", attr_id))
29634 cp_parser_omp_directive_args (parser, attribute);
29635 return attribute;
29637 else if (is_attribute_p ("sequence", attr_id))
29639 TREE_VALUE (TREE_PURPOSE (attribute))
29640 = get_identifier ("directive");
29641 cp_parser_omp_sequence_args (parser, attribute);
29642 TREE_VALUE (attribute) = nreverse (TREE_VALUE (attribute));
29643 return attribute;
29647 /* For unknown attributes, just skip balanced tokens instead of
29648 trying to parse the arguments. Set TREE_VALUE (attribute) to
29649 error_mark_node to distinguish skipped arguments from attributes
29650 with no arguments. */
29651 for (size_t n = cp_parser_skip_balanced_tokens (parser, 1) - 1; n; --n)
29652 cp_lexer_consume_token (parser->lexer);
29653 TREE_VALUE (attribute) = error_mark_node;
29654 return attribute;
29657 vec = cp_parser_parenthesized_expression_list
29658 (parser, attr_flag, /*cast_p=*/false,
29659 /*allow_expansion_p=*/true,
29660 /*non_constant_p=*/NULL);
29661 if (vec == NULL)
29662 arguments = error_mark_node;
29663 else
29665 if (vec->is_empty ())
29666 /* e.g. [[attr()]]. */
29667 error_at (token->location, "parentheses must be omitted if "
29668 "%qE attribute argument list is empty",
29669 attr_id);
29670 arguments = build_tree_list_vec (vec);
29671 release_tree_vector (vec);
29674 if (arguments == error_mark_node)
29675 attribute = error_mark_node;
29676 else
29677 TREE_VALUE (attribute) = arguments;
29680 return attribute;
29683 /* Warn if the attribute ATTRIBUTE appears more than once in the
29684 attribute-list ATTRIBUTES. This used to be enforced for certain
29685 attributes, but the restriction was removed in P2156.
29686 LOC is the location of ATTRIBUTE. Returns true if ATTRIBUTE was not
29687 found in ATTRIBUTES. */
29689 static bool
29690 cp_parser_check_std_attribute (location_t loc, tree attributes, tree attribute)
29692 static auto alist = { "noreturn", "deprecated", "nodiscard", "maybe_unused",
29693 "likely", "unlikely", "fallthrough",
29694 "no_unique_address", "carries_dependency" };
29695 if (attributes)
29696 for (const auto &a : alist)
29697 if (is_attribute_p (a, get_attribute_name (attribute))
29698 && is_attribute_namespace_p ("", attribute)
29699 && lookup_attribute ("", a, attributes))
29701 if (!from_macro_expansion_at (loc))
29702 warning_at (loc, OPT_Wattributes, "attribute %qs specified "
29703 "multiple times", a);
29704 return false;
29706 return true;
29709 /* Parse a list of standard C++-11 attributes.
29711 attribute-list:
29712 attribute [opt]
29713 attribute-list , attribute[opt]
29714 attribute ...
29715 attribute-list , attribute ...
29718 static tree
29719 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
29721 tree attributes = NULL_TREE, attribute = NULL_TREE;
29722 cp_token *token = NULL;
29724 while (true)
29726 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29727 attribute = cp_parser_std_attribute (parser, attr_ns);
29728 if (attribute == error_mark_node)
29729 break;
29730 if (attribute != NULL_TREE)
29732 if (cp_parser_check_std_attribute (loc, attributes, attribute))
29734 TREE_CHAIN (attribute) = attributes;
29735 attributes = attribute;
29738 token = cp_lexer_peek_token (parser->lexer);
29739 if (token->type == CPP_ELLIPSIS)
29741 cp_lexer_consume_token (parser->lexer);
29742 if (attribute == NULL_TREE)
29743 error_at (token->location,
29744 "expected attribute before %<...%>");
29745 else if (TREE_VALUE (attribute) == NULL_TREE)
29747 error_at (token->location, "attribute with no arguments "
29748 "contains no parameter packs");
29749 return error_mark_node;
29751 else if (TREE_VALUE (attribute) != error_mark_node)
29753 tree pack = make_pack_expansion (TREE_VALUE (attribute));
29754 if (pack == error_mark_node)
29755 return error_mark_node;
29756 TREE_VALUE (attribute) = pack;
29758 token = cp_lexer_peek_token (parser->lexer);
29760 if (token->type != CPP_COMMA)
29761 break;
29762 cp_lexer_consume_token (parser->lexer);
29764 attributes = nreverse (attributes);
29765 return attributes;
29768 /* Optionally parse a C++20 contract role. A NULL return means that no
29769 contract role was specified.
29771 contract-role:
29772 % default
29773 % identifier
29775 If the identifier does not name a known contract role, it will
29776 be assumed to be default. Returns the identifier for the role
29777 token. */
29779 static tree
29780 cp_parser_contract_role (cp_parser *parser)
29782 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_MOD));
29783 cp_lexer_consume_token (parser->lexer);
29785 cp_token *token = cp_lexer_peek_token (parser->lexer);
29786 tree role_id = NULL_TREE;
29787 if (token->type == CPP_NAME)
29788 role_id = token->u.value;
29789 else if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
29790 role_id = get_identifier ("default");
29791 else
29793 error_at (token->location, "expected contract-role");
29794 return error_mark_node;
29796 cp_lexer_consume_token (parser->lexer);
29798 /* FIXME: Warn about invalid/unknown roles? */
29799 return role_id;
29802 /* Parse an optional contract mode.
29804 contract-mode:
29805 contract-semantic
29806 [contract-level] [contract-role]
29808 contract-semantic:
29809 check_never_continue
29810 check_maybe_continue
29811 check_always_continue
29813 contract-level:
29814 default
29815 audit
29816 axiom
29818 contract-role:
29819 default
29820 identifier
29822 This grammar is taken from P1332R0. During parsing, this sets options
29823 on the MODE object to determine the configuration of the contract.
29825 Returns a tree containing the identifiers used in the configuration.
29826 This is either an IDENTIFIER with the literal semantic or a TREE_LIST
29827 whose TREE_VALUE is the contract-level and whose TREE_PURPOSE is the
29828 contract-role, if any. NULL_TREE is returned if no information is
29829 given (i.e., all defaults selected). */
29831 static tree
29832 cp_parser_contract_mode_opt (cp_parser *parser,
29833 bool postcondition_p)
29835 /* The mode is empty; the level and role are default. */
29836 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
29837 return NULL_TREE;
29839 /* There is only a role; the level is default. */
29840 if (cp_lexer_next_token_is (parser->lexer, CPP_MOD))
29842 tree role_id = cp_parser_contract_role (parser);
29843 return build_tree_list (role_id, get_identifier ("default"));
29846 /* Otherwise, match semantic or level. */
29847 cp_token *token = cp_lexer_peek_token (parser->lexer);
29848 contract_level level = CONTRACT_INVALID;
29849 contract_semantic semantic = CCS_INVALID;
29850 tree config_id;
29851 if (token->type == CPP_NAME)
29853 config_id = token->u.value;
29855 /* Either a named level, a concrete semantic, or an identifier
29856 for a postcondition. */
29857 const char *ident = IDENTIFIER_POINTER (token->u.value);
29858 level = map_contract_level (ident);
29859 semantic = map_contract_semantic (ident);
29861 /* The identifier is the return value for a postcondition. */
29862 if (level == CONTRACT_INVALID && semantic == CCS_INVALID
29863 && postcondition_p)
29864 return NULL_TREE;
29866 else if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
29868 config_id = get_identifier ("default");
29869 level = CONTRACT_DEFAULT;
29871 else
29873 /* We got some other token other than a ':'. */
29874 error_at (token->location, "expected contract semantic or level");
29875 return NULL_TREE;
29878 /* Consume the literal semantic or level token. */
29879 cp_lexer_consume_token (parser->lexer);
29881 if (semantic == CCS_INVALID && level == CONTRACT_INVALID)
29883 error_at (token->location,
29884 "expected contract level: "
29885 "%<default%>, %<audit%>, or %<axiom%>");
29886 return NULL_TREE;
29889 /* We matched an explicit semantic. */
29890 if (semantic != CCS_INVALID)
29892 if (cp_lexer_next_token_is (parser->lexer, CPP_MOD))
29894 error ("invalid use of contract role for explicit semantic");
29895 cp_lexer_consume_token (parser->lexer);
29896 cp_lexer_consume_token (parser->lexer);
29898 return config_id;
29901 /* We matched a level, there may be a role; otherwise this is default. */
29902 if (cp_lexer_next_token_is (parser->lexer, CPP_MOD))
29904 tree role_id = cp_parser_contract_role (parser);
29905 return build_tree_list (role_id, config_id);
29908 return build_tree_list (NULL_TREE, config_id);
29911 static tree
29912 find_error (tree *tp, int *, void *)
29914 if (*tp == error_mark_node)
29915 return *tp;
29916 return NULL_TREE;
29919 static bool
29920 contains_error_p (tree t)
29922 return walk_tree (&t, find_error, NULL, NULL);
29925 /* Parse a standard C++20 contract attribute specifier.
29927 contract-attribute-specifier:
29928 [ [ assert contract-level [opt] : conditional-expression ] ]
29929 [ [ pre contract-level [opt] : conditional-expression ] ]
29930 [ [ post contract-level [opt] identifier [opt] : conditional-expression ] ]
29932 For free functions, we cannot determine the type of the postcondition
29933 identifier because the we haven't called grokdeclarator yet. In those
29934 cases we parse the postcondition as if the identifier was declared as
29935 'auto <identifier>'. We then instantiate the postcondition once the
29936 return type is known.
29938 For member functions, contracts are in the complete-class context, so the
29939 parse is deferred. We also have the return type avaialable (unless it's
29940 deduced), so we don't need to parse the postcondition in terms of a
29941 placeholder. */
29943 static tree
29944 cp_parser_contract_attribute_spec (cp_parser *parser, tree attribute)
29946 gcc_assert (contract_attribute_p (attribute));
29947 cp_token *token = cp_lexer_consume_token (parser->lexer);
29948 location_t loc = token->location;
29950 bool assertion_p = is_attribute_p ("assert", attribute);
29951 bool postcondition_p = is_attribute_p ("post", attribute);
29953 /* Parse the optional mode. */
29954 tree mode = cp_parser_contract_mode_opt (parser, postcondition_p);
29956 /* Check for postcondition identifiers. */
29957 cp_expr identifier;
29958 if (postcondition_p && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29959 identifier = cp_parser_identifier (parser);
29960 if (identifier == error_mark_node)
29961 return error_mark_node;
29963 cp_parser_require (parser, CPP_COLON, RT_COLON);
29965 /* Defer the parsing of pre/post contracts inside class definitions. */
29966 tree contract;
29967 if (!assertion_p &&
29968 current_class_type &&
29969 TYPE_BEING_DEFINED (current_class_type))
29971 /* Skip until we reach an unenclose ']'. If we ran into an unnested ']'
29972 that doesn't close the attribute, return an error and let the attribute
29973 handling code emit an error for missing ']]'. */
29974 cp_token *first = cp_lexer_peek_token (parser->lexer);
29975 cp_parser_skip_to_closing_parenthesis_1 (parser,
29976 /*recovering=*/false,
29977 CPP_CLOSE_SQUARE,
29978 /*consume_paren=*/false);
29979 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE
29980 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_CLOSE_SQUARE)
29981 return error_mark_node;
29982 cp_token *last = cp_lexer_peek_token (parser->lexer);
29984 /* Build a deferred-parse node. */
29985 tree condition = make_node (DEFERRED_PARSE);
29986 DEFPARSE_TOKENS (condition) = cp_token_cache_new (first, last);
29987 DEFPARSE_INSTANTIATIONS (condition) = NULL;
29989 /* And its corresponding contract. */
29990 contract = grok_contract (attribute, mode, identifier, condition, loc);
29992 else
29994 /* Enable location wrappers when parsing contracts. */
29995 auto suppression = make_temp_override (suppress_location_wrappers, 0);
29997 /* Build a fake variable for the result identifier. */
29998 tree result = NULL_TREE;
29999 if (identifier)
30001 begin_scope (sk_block, NULL_TREE);
30002 result = make_postcondition_variable (identifier);
30003 ++processing_template_decl;
30006 /* Parse the condition, ensuring that parameters or the return variable
30007 aren't flagged for use outside the body of a function. */
30008 ++processing_contract_condition;
30009 cp_expr condition = cp_parser_conditional_expression (parser);
30010 --processing_contract_condition;
30012 /* Try to recover from errors by scanning up to the end of the
30013 attribute. Sometimes we get partially parsed expressions, so
30014 we need to search the condition for errors. */
30015 if (contains_error_p (condition))
30016 cp_parser_skip_up_to_closing_square_bracket (parser);
30018 /* Build the contract. */
30019 contract = grok_contract (attribute, mode, result, condition, loc);
30021 /* Leave our temporary scope for the postcondition result. */
30022 if (result)
30024 --processing_template_decl;
30025 pop_bindings_and_leave_scope ();
30029 if (!flag_contracts)
30031 error_at (loc, "contracts are only available with %<-fcontracts%>");
30032 return error_mark_node;
30035 return finish_contract_attribute (attribute, contract);
30038 /* Parse a contract condition for a deferred contract. */
30040 void cp_parser_late_contract_condition (cp_parser *parser,
30041 tree fn,
30042 tree attribute)
30044 tree contract = TREE_VALUE (TREE_VALUE (attribute));
30046 /* Make sure we've gotten something that hasn't been parsed yet or that
30047 we're not parsing an invalid contract. */
30048 tree condition = CONTRACT_CONDITION (contract);
30049 if (TREE_CODE (condition) != DEFERRED_PARSE)
30050 return;
30052 tree identifier = NULL_TREE;
30053 if (TREE_CODE (contract) == POSTCONDITION_STMT)
30054 identifier = POSTCONDITION_IDENTIFIER (contract);
30056 /* Build a fake variable for the result identifier. */
30057 tree result = NULL_TREE;
30058 if (identifier)
30060 /* TODO: Can we guarantee that the identifier has a location? */
30061 location_t loc = cp_expr_location (contract);
30062 tree type = TREE_TYPE (TREE_TYPE (fn));
30063 if (!check_postcondition_result (fn, type, loc))
30065 invalidate_contract (contract);
30066 return;
30069 begin_scope (sk_block, NULL_TREE);
30070 result = make_postcondition_variable (identifier, type);
30071 ++processing_template_decl;
30074 /* 'this' is not allowed in preconditions of constructors or in postconditions
30075 of destructors. Note that the previous value of this variable is
30076 established by the calling function, so we need to save it here. */
30077 tree saved_ccr = current_class_ref;
30078 tree saved_ccp = current_class_ptr;
30079 if ((DECL_CONSTRUCTOR_P (fn) && PRECONDITION_P (contract)) ||
30080 (DECL_DESTRUCTOR_P (fn) && POSTCONDITION_P (contract)))
30082 current_class_ref = current_class_ptr = NULL_TREE;
30083 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
30086 push_unparsed_function_queues (parser);
30088 /* Push the saved tokens onto the parser's lexer stack. */
30089 cp_token_cache *tokens = DEFPARSE_TOKENS (condition);
30090 cp_parser_push_lexer_for_tokens (parser, tokens);
30092 /* Parse the condition, ensuring that parameters or the return variable
30093 aren't flagged for use outside the body of a function. */
30094 ++processing_contract_condition;
30095 condition = cp_parser_conditional_expression (parser);
30096 --processing_contract_condition;
30098 /* Revert to the main lexer. */
30099 cp_parser_pop_lexer (parser);
30101 /* Restore the queue. */
30102 pop_unparsed_function_queues (parser);
30104 current_class_ref = saved_ccr;
30105 current_class_ptr = saved_ccp;
30107 /* Commit to changes. */
30108 update_late_contract (contract, result, condition);
30110 /* Leave our temporary scope for the postcondition result. */
30111 if (result)
30113 --processing_template_decl;
30114 pop_bindings_and_leave_scope ();
30118 /* Parse a standard C++-11 attribute specifier.
30120 attribute-specifier:
30121 [ [ attribute-using-prefix [opt] attribute-list ] ]
30122 contract-attribute-specifier
30123 alignment-specifier
30125 attribute-using-prefix:
30126 using attribute-namespace :
30128 alignment-specifier:
30129 alignas ( type-id ... [opt] )
30130 alignas ( alignment-expression ... [opt] ).
30132 Extensions for contracts:
30134 contract-attribute-specifier:
30135 [ [ assert : contract-mode [opt] : conditional-expression ] ]
30136 [ [ pre : contract-mode [opt] : conditional-expression ] ]
30137 [ [ post : contract-mode [opt] identifier [opt] :
30138 conditional-expression ] ] */
30140 static tree
30141 cp_parser_std_attribute_spec (cp_parser *parser)
30143 tree attributes = NULL_TREE;
30144 cp_token *token = cp_lexer_peek_token (parser->lexer);
30146 if (token->type == CPP_OPEN_SQUARE
30147 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
30149 tree attr_ns = NULL_TREE;
30150 tree attr_name = NULL_TREE;
30152 cp_lexer_consume_token (parser->lexer);
30153 cp_lexer_consume_token (parser->lexer);
30155 token = cp_lexer_peek_token (parser->lexer);
30156 if (token->type == CPP_NAME)
30158 attr_name = token->u.value;
30159 attr_name = canonicalize_attr_name (attr_name);
30162 /* Handle contract-attribute-specs specially. */
30163 if (attr_name && contract_attribute_p (attr_name))
30165 tree attrs = cp_parser_contract_attribute_spec (parser, attr_name);
30166 if (attrs != error_mark_node)
30167 attributes = attrs;
30168 goto finish_attrs;
30171 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
30173 token = cp_lexer_peek_nth_token (parser->lexer, 2);
30174 if (token->type == CPP_NAME)
30175 attr_ns = token->u.value;
30176 else if (token->type == CPP_KEYWORD)
30177 attr_ns = ridpointers[(int) token->keyword];
30178 else if (token->flags & NAMED_OP)
30179 attr_ns = get_identifier (cpp_type2name (token->type,
30180 token->flags));
30181 if (attr_ns
30182 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
30184 if (cxx_dialect < cxx17)
30185 pedwarn (input_location, OPT_Wc__17_extensions,
30186 "attribute using prefix only available "
30187 "with %<-std=c++17%> or %<-std=gnu++17%>");
30189 cp_lexer_consume_token (parser->lexer);
30190 cp_lexer_consume_token (parser->lexer);
30191 cp_lexer_consume_token (parser->lexer);
30193 else
30194 attr_ns = NULL_TREE;
30197 attributes = cp_parser_std_attribute_list (parser, attr_ns);
30199 finish_attrs:
30200 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
30201 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
30202 cp_parser_skip_to_end_of_statement (parser);
30203 else
30204 /* Warn about parsing c++11 attribute in non-c++11 mode, only
30205 when we are sure that we have actually parsed them. */
30206 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
30208 else
30210 tree alignas_expr;
30212 /* Look for an alignment-specifier. */
30214 token = cp_lexer_peek_token (parser->lexer);
30216 if (token->type != CPP_KEYWORD
30217 || token->keyword != RID_ALIGNAS)
30218 return NULL_TREE;
30220 cp_lexer_consume_token (parser->lexer);
30221 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
30223 matching_parens parens;
30224 if (!parens.require_open (parser))
30225 return error_mark_node;
30227 cp_parser_parse_tentatively (parser);
30228 alignas_expr = cp_parser_type_id (parser);
30230 if (!cp_parser_parse_definitely (parser))
30232 alignas_expr = cp_parser_assignment_expression (parser);
30233 if (alignas_expr == error_mark_node)
30234 cp_parser_skip_to_end_of_statement (parser);
30235 if (alignas_expr == NULL_TREE
30236 || alignas_expr == error_mark_node)
30237 return alignas_expr;
30240 alignas_expr = cxx_alignas_expr (alignas_expr);
30241 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
30243 /* Handle alignas (pack...). */
30244 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30246 cp_lexer_consume_token (parser->lexer);
30247 alignas_expr = make_pack_expansion (alignas_expr);
30250 /* Something went wrong, so don't build the attribute. */
30251 if (alignas_expr == error_mark_node)
30252 return error_mark_node;
30254 /* Missing ')' means the code cannot possibly be valid; go ahead
30255 and commit to make sure we issue a hard error. */
30256 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
30257 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
30258 cp_parser_commit_to_tentative_parse (parser);
30260 if (!parens.require_close (parser))
30261 return error_mark_node;
30263 /* Build the C++-11 representation of an 'aligned'
30264 attribute. */
30265 attributes
30266 = build_tree_list (build_tree_list (gnu_identifier,
30267 aligned_identifier), alignas_expr);
30270 return attributes;
30273 /* Parse a standard C++-11 attribute-specifier-seq.
30275 attribute-specifier-seq:
30276 attribute-specifier-seq [opt] attribute-specifier */
30278 static tree
30279 cp_parser_std_attribute_spec_seq (cp_parser *parser)
30281 tree attr_specs = NULL_TREE;
30282 tree attr_last = NULL_TREE;
30284 /* Don't create wrapper nodes within attributes: the
30285 handlers don't know how to handle them. */
30286 auto_suppress_location_wrappers sentinel;
30288 while (true)
30290 tree attr_spec = cp_parser_std_attribute_spec (parser);
30291 if (attr_spec == NULL_TREE)
30292 break;
30293 if (attr_spec == error_mark_node)
30294 return error_mark_node;
30296 if (attr_last)
30297 TREE_CHAIN (attr_last) = attr_spec;
30298 else
30299 attr_specs = attr_last = attr_spec;
30300 attr_last = tree_last (attr_last);
30303 return attr_specs;
30306 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
30307 return index of the first token after balanced-token, or N on failure. */
30309 static size_t
30310 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
30312 size_t orig_n = n;
30313 int nparens = 0, nbraces = 0, nsquares = 0;
30315 switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
30317 case CPP_PRAGMA_EOL:
30318 if (!parser->lexer->in_pragma)
30319 break;
30320 /* FALLTHRU */
30321 case CPP_EOF:
30322 /* Ran out of tokens. */
30323 return orig_n;
30324 case CPP_OPEN_PAREN:
30325 ++nparens;
30326 break;
30327 case CPP_OPEN_BRACE:
30328 ++nbraces;
30329 break;
30330 case CPP_OPEN_SQUARE:
30331 ++nsquares;
30332 break;
30333 case CPP_CLOSE_PAREN:
30334 --nparens;
30335 break;
30336 case CPP_CLOSE_BRACE:
30337 --nbraces;
30338 break;
30339 case CPP_CLOSE_SQUARE:
30340 --nsquares;
30341 break;
30342 default:
30343 break;
30345 while (nparens || nbraces || nsquares);
30346 return n;
30349 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
30350 return index of the first token after the GNU attribute tokens, or N on
30351 failure. */
30353 static size_t
30354 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
30356 while (true)
30358 if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
30359 || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
30360 || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
30361 break;
30363 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
30364 if (n2 == n + 2)
30365 break;
30366 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
30367 break;
30368 n = n2 + 1;
30370 return n;
30373 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
30374 next token), return index of the first token after the standard C++11
30375 attribute tokens, or N on failure. */
30377 static size_t
30378 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
30380 while (true)
30382 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
30383 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
30385 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
30386 if (n2 == n + 1)
30387 break;
30388 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
30389 break;
30390 n = n2 + 1;
30392 else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
30393 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
30395 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
30396 if (n2 == n + 1)
30397 break;
30398 n = n2;
30400 else
30401 break;
30403 return n;
30406 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
30407 as the next token), return index of the first token after the attribute
30408 tokens, or N on failure. */
30410 static size_t
30411 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
30413 if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
30414 return cp_parser_skip_gnu_attributes_opt (parser, n);
30415 return cp_parser_skip_std_attribute_spec_seq (parser, n);
30418 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
30419 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
30420 current value of the PEDANTIC flag, regardless of whether or not
30421 the `__extension__' keyword is present. The caller is responsible
30422 for restoring the value of the PEDANTIC flag. */
30424 static bool
30425 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
30427 /* Save the old value of the PEDANTIC flag. */
30428 *saved_pedantic = pedantic;
30430 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
30432 /* Consume the `__extension__' token. */
30433 cp_lexer_consume_token (parser->lexer);
30434 /* We're not being pedantic while the `__extension__' keyword is
30435 in effect. */
30436 pedantic = 0;
30438 return true;
30441 return false;
30444 /* Parse a label declaration.
30446 label-declaration:
30447 __label__ label-declarator-seq ;
30449 label-declarator-seq:
30450 identifier , label-declarator-seq
30451 identifier */
30453 static void
30454 cp_parser_label_declaration (cp_parser* parser)
30456 /* Look for the `__label__' keyword. */
30457 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
30459 while (true)
30461 tree identifier;
30463 /* Look for an identifier. */
30464 identifier = cp_parser_identifier (parser);
30465 /* If we failed, stop. */
30466 if (identifier == error_mark_node)
30467 break;
30468 /* Declare it as a label. */
30469 finish_label_decl (identifier);
30470 /* If the next token is a `;', stop. */
30471 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30472 break;
30473 /* Look for the `,' separating the label declarations. */
30474 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
30477 /* Look for the final `;'. */
30478 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
30481 // -------------------------------------------------------------------------- //
30482 // Concept definitions
30484 static tree
30485 cp_parser_concept_definition (cp_parser *parser)
30487 /* A concept definition is an unevaluated context. */
30488 cp_unevaluated u;
30490 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_CONCEPT));
30491 cp_lexer_consume_token (parser->lexer);
30493 cp_expr id = cp_parser_identifier (parser);
30494 if (id == error_mark_node)
30496 cp_parser_skip_to_end_of_statement (parser);
30497 cp_parser_consume_semicolon_at_end_of_statement (parser);
30498 return NULL_TREE;
30501 tree attrs = cp_parser_attributes_opt (parser);
30503 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
30505 cp_parser_skip_to_end_of_statement (parser);
30506 cp_parser_consume_semicolon_at_end_of_statement (parser);
30507 return error_mark_node;
30510 processing_constraint_expression_sentinel parsing_constraint;
30511 tree init = cp_parser_constraint_expression (parser);
30512 if (init == error_mark_node)
30513 cp_parser_skip_to_end_of_statement (parser);
30515 /* Consume the trailing ';'. Diagnose the problem if it isn't there,
30516 but continue as if it were. */
30517 cp_parser_consume_semicolon_at_end_of_statement (parser);
30519 return finish_concept_definition (id, init, attrs);
30522 // -------------------------------------------------------------------------- //
30523 // Requires Clause
30525 /* Diagnose an expression that should appear in ()'s within a requires-clause
30526 and suggest where to place those parentheses. */
30528 static void
30529 cp_parser_diagnose_ungrouped_constraint_plain (location_t loc)
30531 error_at (loc, "expression must be enclosed in parentheses");
30534 static void
30535 cp_parser_diagnose_ungrouped_constraint_rich (location_t loc)
30537 gcc_rich_location richloc (loc);
30538 richloc.add_fixit_insert_before ("(");
30539 richloc.add_fixit_insert_after (")");
30540 error_at (&richloc, "expression must be enclosed in parentheses");
30543 /* Characterizes the likely kind of expression intended by a mis-written
30544 primary constraint. */
30545 enum primary_constraint_error
30547 pce_ok,
30548 pce_maybe_operator,
30549 pce_maybe_postfix
30552 /* Returns true if the token(s) following a primary-expression in a
30553 constraint-logical-* expression would require parentheses. */
30555 static primary_constraint_error
30556 cp_parser_constraint_requires_parens (cp_parser *parser, bool lambda_p)
30558 cp_token *token = cp_lexer_peek_token (parser->lexer);
30559 switch (token->type)
30561 default:
30562 return pce_ok;
30564 case CPP_EQ:
30566 /* An equal sign may be part of the definition of a function,
30567 and not an assignment operator, when parsing the expression
30568 for a trailing requires-clause. For example:
30570 template<typename T>
30571 struct S {
30572 S() requires C<T> = default;
30575 Don't try to reparse this a binary operator. */
30576 if (cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_DELETE)
30577 || cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_DEFAULT))
30578 return pce_ok;
30580 gcc_fallthrough ();
30583 /* Arithmetic operators. */
30584 case CPP_PLUS:
30585 case CPP_MINUS:
30586 case CPP_MULT:
30587 case CPP_DIV:
30588 case CPP_MOD:
30589 /* Bitwise operators. */
30590 case CPP_AND:
30591 case CPP_OR:
30592 case CPP_XOR:
30593 case CPP_RSHIFT:
30594 case CPP_LSHIFT:
30595 /* Relational operators. */
30596 case CPP_EQ_EQ:
30597 case CPP_NOT_EQ:
30598 case CPP_LESS:
30599 case CPP_GREATER:
30600 case CPP_LESS_EQ:
30601 case CPP_GREATER_EQ:
30602 case CPP_SPACESHIP:
30603 /* Pointer-to-member. */
30604 case CPP_DOT_STAR:
30605 case CPP_DEREF_STAR:
30606 /* Assignment operators. */
30607 case CPP_PLUS_EQ:
30608 case CPP_MINUS_EQ:
30609 case CPP_MULT_EQ:
30610 case CPP_DIV_EQ:
30611 case CPP_MOD_EQ:
30612 case CPP_AND_EQ:
30613 case CPP_OR_EQ:
30614 case CPP_XOR_EQ:
30615 case CPP_RSHIFT_EQ:
30616 case CPP_LSHIFT_EQ:
30617 /* Conditional operator */
30618 case CPP_QUERY:
30619 /* Unenclosed binary or conditional operator. */
30620 return pce_maybe_operator;
30622 case CPP_OPEN_PAREN:
30624 /* A primary constraint that precedes the parameter-list of a
30625 lambda expression is followed by an open paren.
30627 []<typename T> requires C (T a, T b) { ... }
30629 Don't try to re-parse this as a postfix expression. */
30630 if (lambda_p)
30631 return pce_ok;
30633 gcc_fallthrough ();
30635 case CPP_OPEN_SQUARE:
30637 /* A primary-constraint-expression followed by a '[[' is not a
30638 postfix expression. */
30639 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE))
30640 return pce_ok;
30642 gcc_fallthrough ();
30644 case CPP_PLUS_PLUS:
30645 case CPP_MINUS_MINUS:
30646 case CPP_DOT:
30647 /* Unenclosed postfix operator. */
30648 return pce_maybe_postfix;
30650 case CPP_DEREF:
30651 /* A primary constraint that precedes the lambda-declarator of a
30652 lambda expression is followed by trailing return type.
30654 []<typename T> requires C -> void {}
30656 Don't try to re-parse this as a postfix expression in
30657 C++23 and later. In C++20 ( needs to come in between but we
30658 allow it to be omitted with pedwarn. */
30659 if (lambda_p)
30660 return pce_ok;
30661 /* Unenclosed postfix operator. */
30662 return pce_maybe_postfix;
30666 /* Returns true if the next token begins a unary expression, preceded by
30667 an operator or keyword. */
30669 static bool
30670 cp_parser_unary_constraint_requires_parens (cp_parser *parser)
30672 cp_token *token = cp_lexer_peek_token (parser->lexer);
30673 switch (token->type)
30675 case CPP_NOT:
30676 case CPP_PLUS:
30677 case CPP_MINUS:
30678 case CPP_MULT:
30679 case CPP_COMPL:
30680 case CPP_PLUS_PLUS:
30681 case CPP_MINUS_MINUS:
30682 return true;
30684 case CPP_KEYWORD:
30686 switch (token->keyword)
30688 case RID_STATCAST:
30689 case RID_DYNCAST:
30690 case RID_REINTCAST:
30691 case RID_CONSTCAST:
30692 case RID_TYPEID:
30693 case RID_SIZEOF:
30694 case RID_ALIGNOF:
30695 case RID_NOEXCEPT:
30696 case RID_NEW:
30697 case RID_DELETE:
30698 case RID_THROW:
30699 return true;
30701 default:
30702 break;
30706 default:
30707 break;
30710 return false;
30713 /* Parse a primary expression within a constraint. */
30715 static cp_expr
30716 cp_parser_constraint_primary_expression (cp_parser *parser, bool lambda_p)
30718 /* If this looks like a unary expression, parse it as such, but diagnose
30719 it as ill-formed; it requires parens. */
30720 if (cp_parser_unary_constraint_requires_parens (parser))
30722 cp_expr e = cp_parser_assignment_expression (parser, NULL, false, false);
30723 cp_parser_diagnose_ungrouped_constraint_rich (e.get_location());
30724 return e;
30727 cp_lexer_save_tokens (parser->lexer);
30728 cp_id_kind idk;
30729 location_t loc = input_location;
30730 cp_expr expr = cp_parser_primary_expression (parser,
30731 /*address_p=*/false,
30732 /*cast_p=*/false,
30733 /*template_arg_p=*/false,
30734 &idk);
30735 expr.maybe_add_location_wrapper ();
30737 primary_constraint_error pce = pce_ok;
30738 if (expr != error_mark_node)
30740 /* The primary-expression could be part of an unenclosed non-logical
30741 compound expression. */
30742 pce = cp_parser_constraint_requires_parens (parser, lambda_p);
30744 if (pce == pce_ok)
30746 cp_lexer_commit_tokens (parser->lexer);
30747 return finish_constraint_primary_expr (expr);
30750 /* Retry the parse at a lower precedence. If that succeeds, diagnose the
30751 error, but return the expression as if it were valid. */
30752 cp_lexer_rollback_tokens (parser->lexer);
30753 cp_parser_parse_tentatively (parser);
30754 if (pce == pce_maybe_operator)
30755 expr = cp_parser_assignment_expression (parser, NULL, false, false);
30756 else
30757 expr = cp_parser_simple_cast_expression (parser);
30758 if (cp_parser_parse_definitely (parser))
30760 cp_parser_diagnose_ungrouped_constraint_rich (expr.get_location());
30761 return expr;
30764 /* Otherwise, something has gone very wrong, and we can't generate a more
30765 meaningful diagnostic or recover. */
30766 cp_parser_diagnose_ungrouped_constraint_plain (loc);
30767 return error_mark_node;
30770 /* Parse a constraint-logical-and-expression.
30772 constraint-logical-and-expression:
30773 primary-expression
30774 constraint-logical-and-expression '&&' primary-expression */
30776 static cp_expr
30777 cp_parser_constraint_logical_and_expression (cp_parser *parser, bool lambda_p)
30779 cp_expr lhs = cp_parser_constraint_primary_expression (parser, lambda_p);
30780 while (cp_lexer_next_token_is (parser->lexer, CPP_AND_AND))
30782 cp_token *op = cp_lexer_consume_token (parser->lexer);
30783 tree rhs = cp_parser_constraint_primary_expression (parser, lambda_p);
30784 lhs = finish_constraint_and_expr (op->location, lhs, rhs);
30786 return lhs;
30789 /* Parse a constraint-logical-or-expression.
30791 constraint-logical-or-expression:
30792 constraint-logical-and-expression
30793 constraint-logical-or-expression '||' constraint-logical-and-expression */
30795 static cp_expr
30796 cp_parser_constraint_logical_or_expression (cp_parser *parser, bool lambda_p)
30798 cp_expr lhs = cp_parser_constraint_logical_and_expression (parser, lambda_p);
30799 while (cp_lexer_next_token_is (parser->lexer, CPP_OR_OR))
30801 cp_token *op = cp_lexer_consume_token (parser->lexer);
30802 cp_expr rhs = cp_parser_constraint_logical_and_expression (parser, lambda_p);
30803 lhs = finish_constraint_or_expr (op->location, lhs, rhs);
30805 return lhs;
30808 /* Parse the expression after a requires-clause. This has a different grammar
30809 than that in the concepts TS. */
30811 static tree
30812 cp_parser_requires_clause_expression (cp_parser *parser, bool lambda_p)
30814 processing_constraint_expression_sentinel parsing_constraint;
30815 ++processing_template_decl;
30816 cp_expr expr = cp_parser_constraint_logical_or_expression (parser, lambda_p);
30817 --processing_template_decl;
30818 if (check_for_bare_parameter_packs (expr))
30819 expr = error_mark_node;
30820 return expr;
30823 /* Parse a expression after a requires clause.
30825 constraint-expression:
30826 logical-or-expression
30828 The required logical-or-expression must be a constant expression. Note
30829 that we don't check that the expression is constepxr here. We defer until
30830 we analyze constraints and then, we only check atomic constraints. */
30832 static tree
30833 cp_parser_constraint_expression (cp_parser *parser)
30835 processing_constraint_expression_sentinel parsing_constraint;
30836 ++processing_template_decl;
30837 cp_expr expr = cp_parser_binary_expression (parser, false, true,
30838 PREC_NOT_OPERATOR, NULL);
30839 --processing_template_decl;
30840 if (check_for_bare_parameter_packs (expr))
30841 expr = error_mark_node;
30842 expr.maybe_add_location_wrapper ();
30843 return expr;
30846 /* Optionally parse a requires clause:
30848 requires-clause:
30849 `requires` constraint-logical-or-expression.
30850 [ConceptsTS]
30851 `requires constraint-expression.
30853 LAMBDA_P is true when the requires-clause is parsed before the
30854 parameter-list of a lambda-declarator. */
30856 static tree
30857 cp_parser_requires_clause_opt (cp_parser *parser, bool lambda_p)
30859 /* A requires clause is an unevaluated context. */
30860 cp_unevaluated u;
30862 cp_token *tok = cp_lexer_peek_token (parser->lexer);
30863 if (tok->keyword != RID_REQUIRES)
30865 if (!flag_concepts && tok->type == CPP_NAME
30866 && tok->u.value == ridpointers[RID_REQUIRES])
30868 error_at (cp_lexer_peek_token (parser->lexer)->location,
30869 "%<requires%> only available with "
30870 "%<-std=c++20%> or %<-fconcepts%>");
30871 /* Parse and discard the requires-clause. */
30872 cp_lexer_consume_token (parser->lexer);
30873 cp_parser_constraint_expression (parser);
30875 return NULL_TREE;
30878 cp_token *tok2 = cp_lexer_peek_nth_token (parser->lexer, 2);
30879 if (tok2->type == CPP_OPEN_BRACE)
30881 /* An opening brace following the start of a requires-clause is
30882 ill-formed; the user likely forgot the second `requires' that
30883 would start a requires-expression. */
30884 gcc_rich_location richloc (tok2->location);
30885 richloc.add_fixit_insert_after (tok->location, " requires");
30886 error_at (&richloc, "missing additional %<requires%> to start "
30887 "a requires-expression");
30888 /* Don't consume the `requires', so that it's reused as the start of a
30889 requires-expression. */
30891 else
30892 cp_lexer_consume_token (parser->lexer);
30894 if (!flag_concepts_ts)
30895 return cp_parser_requires_clause_expression (parser, lambda_p);
30896 else
30897 return cp_parser_constraint_expression (parser);
30900 /*---------------------------------------------------------------------------
30901 Requires expressions
30902 ---------------------------------------------------------------------------*/
30904 /* Parse a requires expression
30906 requirement-expression:
30907 'requires' requirement-parameter-list [opt] requirement-body */
30909 static tree
30910 cp_parser_requires_expression (cp_parser *parser)
30912 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
30913 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
30915 /* Avoid committing to outer tentative parse. */
30916 tentative_firewall firewall (parser);
30918 /* This is definitely a requires-expression. */
30919 cp_parser_commit_to_tentative_parse (parser);
30921 tree parms, reqs;
30923 /* Local parameters are delared as variables within the scope
30924 of the expression. They are not visible past the end of
30925 the expression. Expressions within the requires-expression
30926 are unevaluated. */
30927 struct scope_sentinel
30929 scope_sentinel ()
30931 ++cp_unevaluated_operand;
30932 begin_scope (sk_function_parms, NULL_TREE);
30933 current_binding_level->requires_expression = true;
30936 ~scope_sentinel ()
30938 pop_bindings_and_leave_scope ();
30939 --cp_unevaluated_operand;
30941 } s;
30943 /* Parse the optional parameter list. */
30944 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30946 parms = cp_parser_requirement_parameter_list (parser);
30947 if (parms == error_mark_node)
30948 return error_mark_node;
30950 else
30951 parms = NULL_TREE;
30953 /* Parse the requirement body. */
30954 ++processing_template_decl;
30955 reqs = cp_parser_requirement_body (parser);
30956 --processing_template_decl;
30957 if (reqs == error_mark_node)
30958 return error_mark_node;
30961 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
30962 the parm chain. */
30963 grokparms (parms, &parms);
30964 loc = make_location (loc, loc, parser->lexer);
30965 tree expr = finish_requires_expr (loc, parms, reqs);
30966 if (!processing_template_decl)
30968 /* Perform semantic processing now to diagnose any invalid types and
30969 expressions. */
30970 int saved_errorcount = errorcount;
30971 tsubst_requires_expr (expr, NULL_TREE, tf_warning_or_error, NULL_TREE);
30972 if (errorcount > saved_errorcount)
30973 return error_mark_node;
30975 return expr;
30978 /* Parse a parameterized requirement.
30980 requirement-parameter-list:
30981 '(' parameter-declaration-clause ')' */
30983 static tree
30984 cp_parser_requirement_parameter_list (cp_parser *parser)
30986 matching_parens parens;
30987 if (!parens.require_open (parser))
30988 return error_mark_node;
30990 tree parms = (cp_parser_parameter_declaration_clause
30991 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
30993 if (!parens.require_close (parser))
30994 return error_mark_node;
30996 /* Modify the declared parameters by removing their context
30997 so they don't refer to the enclosing scope and explicitly
30998 indicating that they are constraint variables. */
30999 for (tree parm = parms; parm; parm = TREE_CHAIN (parm))
31001 if (parm == void_list_node || parm == explicit_void_list_node)
31002 break;
31003 tree decl = TREE_VALUE (parm);
31004 if (decl != error_mark_node)
31006 DECL_CONTEXT (decl) = NULL_TREE;
31007 CONSTRAINT_VAR_P (decl) = true;
31011 return parms;
31014 /* Parse the body of a requirement.
31016 requirement-body:
31017 '{' requirement-list '}' */
31018 static tree
31019 cp_parser_requirement_body (cp_parser *parser)
31021 matching_braces braces;
31022 if (!braces.require_open (parser))
31023 return error_mark_node;
31025 tree reqs = cp_parser_requirement_seq (parser);
31027 if (!braces.require_close (parser))
31028 return error_mark_node;
31030 return reqs;
31033 /* Parse a sequence of requirements.
31035 requirement-seq:
31036 requirement
31037 requirement-seq requirement */
31039 static tree
31040 cp_parser_requirement_seq (cp_parser *parser)
31042 tree result = NULL_TREE;
31045 tree req = cp_parser_requirement (parser);
31046 if (req != error_mark_node)
31047 result = tree_cons (NULL_TREE, req, result);
31049 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE)
31050 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF));
31052 /* If there are no valid requirements, this is not a valid expression. */
31053 if (!result)
31054 return error_mark_node;
31056 /* Reverse the order of requirements so they are analyzed in order. */
31057 return nreverse (result);
31060 /* Parse a syntactic requirement or type requirement.
31062 requirement:
31063 simple-requirement
31064 compound-requirement
31065 type-requirement
31066 nested-requirement */
31068 static tree
31069 cp_parser_requirement (cp_parser *parser)
31071 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
31072 return cp_parser_compound_requirement (parser);
31073 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
31075 /* It's probably a type-requirement. */
31076 cp_parser_parse_tentatively (parser);
31077 tree req = cp_parser_type_requirement (parser);
31078 if (cp_parser_parse_definitely (parser))
31079 return req;
31080 /* No, maybe it's something like typename T::type(); */
31081 cp_parser_parse_tentatively (parser);
31082 req = cp_parser_simple_requirement (parser);
31083 if (cp_parser_parse_definitely (parser))
31084 return req;
31085 /* Non-tentative for the error. */
31086 return cp_parser_type_requirement (parser);
31088 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
31089 return cp_parser_nested_requirement (parser);
31090 else
31091 return cp_parser_simple_requirement (parser);
31094 /* Parse a simple requirement.
31096 simple-requirement:
31097 expression ';' */
31099 static tree
31100 cp_parser_simple_requirement (cp_parser *parser)
31102 location_t start = cp_lexer_peek_token (parser->lexer)->location;
31103 cp_expr expr = cp_parser_expression (parser, NULL, false, false);
31104 if (expr == error_mark_node)
31105 cp_parser_skip_to_end_of_statement (parser);
31107 cp_parser_consume_semicolon_at_end_of_statement (parser);
31109 if (!expr || expr == error_mark_node)
31110 return error_mark_node;
31112 /* Sometimes we don't get locations, so use the cached token location
31113 as a reasonable approximation. */
31114 if (expr.get_location() == UNKNOWN_LOCATION)
31115 expr.set_location (start);
31117 for (tree t = expr; ; )
31119 if (TREE_CODE (t) == TRUTH_ANDIF_EXPR
31120 || TREE_CODE (t) == TRUTH_ORIF_EXPR)
31122 t = TREE_OPERAND (t, 0);
31123 continue;
31125 if (concept_check_p (t))
31127 gcc_rich_location richloc (get_start (start));
31128 richloc.add_fixit_insert_before (start, "requires ");
31129 warning_at (&richloc, OPT_Wmissing_requires, "testing "
31130 "if a concept-id is a valid expression; add "
31131 "%<requires%> to check satisfaction");
31133 break;
31136 return finish_simple_requirement (expr.get_location (), expr);
31139 /* Parse a type requirement
31141 type-requirement
31142 nested-name-specifier [opt] required-type-name ';'
31144 required-type-name:
31145 type-name
31146 'template' [opt] simple-template-id */
31148 static tree
31149 cp_parser_type_requirement (cp_parser *parser)
31151 cp_token *start_tok = cp_lexer_consume_token (parser->lexer);
31152 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31154 // Save the scope before parsing name specifiers.
31155 tree saved_scope = parser->scope;
31156 tree saved_object_scope = parser->object_scope;
31157 tree saved_qualifying_scope = parser->qualifying_scope;
31158 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
31159 cp_parser_nested_name_specifier_opt (parser,
31160 /*typename_keyword_p=*/true,
31161 /*check_dependency_p=*/true,
31162 /*type_p=*/true,
31163 /*is_declaration=*/false);
31165 tree type;
31166 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
31168 cp_lexer_consume_token (parser->lexer);
31169 type = cp_parser_template_id (parser,
31170 /*template_keyword_p=*/true,
31171 /*check_dependency_p=*/true,
31172 /*tag_type=*/none_type,
31173 /*is_declaration=*/false);
31174 type = make_typename_type (parser->scope, type, typename_type,
31175 /*complain=*/tf_error);
31177 else
31178 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
31180 if (TREE_CODE (type) == TYPE_DECL)
31181 type = TREE_TYPE (type);
31183 parser->scope = saved_scope;
31184 parser->object_scope = saved_object_scope;
31185 parser->qualifying_scope = saved_qualifying_scope;
31187 if (type == error_mark_node)
31188 cp_parser_skip_to_end_of_statement (parser);
31190 cp_parser_consume_semicolon_at_end_of_statement (parser);
31192 if (type == error_mark_node)
31193 return error_mark_node;
31195 loc = make_location (loc, start_tok->location, parser->lexer);
31196 return finish_type_requirement (loc, type);
31199 /* Parse a compound requirement
31201 compound-requirement:
31202 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
31204 static tree
31205 cp_parser_compound_requirement (cp_parser *parser)
31207 /* Parse an expression enclosed in '{ }'s. */
31208 matching_braces braces;
31209 if (!braces.require_open (parser))
31210 return error_mark_node;
31212 cp_token *expr_token = cp_lexer_peek_token (parser->lexer);
31214 tree expr = cp_parser_expression (parser, NULL, false, false);
31215 if (expr == error_mark_node)
31216 cp_parser_skip_to_closing_brace (parser);
31218 if (!braces.require_close (parser))
31220 cp_parser_skip_to_end_of_statement (parser);
31221 cp_parser_consume_semicolon_at_end_of_statement (parser);
31222 return error_mark_node;
31225 /* If the expression was invalid, skip the remainder of the requirement. */
31226 if (!expr || expr == error_mark_node)
31228 cp_parser_skip_to_end_of_statement (parser);
31229 cp_parser_consume_semicolon_at_end_of_statement (parser);
31230 return error_mark_node;
31233 /* Parse the optional noexcept. */
31234 bool noexcept_p = false;
31235 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
31237 cp_lexer_consume_token (parser->lexer);
31238 noexcept_p = true;
31241 /* Parse the optional trailing return type. */
31242 tree type = NULL_TREE;
31243 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
31245 cp_lexer_consume_token (parser->lexer);
31246 cp_token *tok = cp_lexer_peek_token (parser->lexer);
31248 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
31249 parser->in_result_type_constraint_p = true;
31250 /* C++20 allows either a type-id or a type-constraint. Parsing
31251 a type-id will subsume the parsing for a type-constraint but
31252 allow for more syntactic forms (e.g., const C<T>*). */
31253 type = cp_parser_trailing_type_id (parser);
31254 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
31255 if (type == error_mark_node)
31256 return error_mark_node;
31258 location_t type_loc = make_location (tok->location, tok->location,
31259 parser->lexer);
31261 /* Check that we haven't written something like 'const C<T>*'. */
31262 if (type_uses_auto (type))
31264 if (!is_auto (type))
31266 error_at (type_loc,
31267 "result type is not a plain type-constraint");
31268 cp_parser_consume_semicolon_at_end_of_statement (parser);
31269 return error_mark_node;
31272 else if (!flag_concepts_ts)
31273 /* P1452R2 removed the trailing-return-type option. */
31274 error_at (type_loc,
31275 "return-type-requirement is not a type-constraint");
31278 location_t loc = make_location (expr_token->location,
31279 braces.open_location (),
31280 parser->lexer);
31282 cp_parser_consume_semicolon_at_end_of_statement (parser);
31284 if (expr == error_mark_node || type == error_mark_node)
31285 return error_mark_node;
31287 return finish_compound_requirement (loc, expr, type, noexcept_p);
31290 /* Parse a nested requirement. This is the same as a requires clause.
31292 nested-requirement:
31293 requires-clause */
31295 static tree
31296 cp_parser_nested_requirement (cp_parser *parser)
31298 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
31299 cp_token *tok = cp_lexer_consume_token (parser->lexer);
31300 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31301 tree req = cp_parser_constraint_expression (parser);
31302 if (req == error_mark_node)
31303 cp_parser_skip_to_end_of_statement (parser);
31304 loc = make_location (loc, tok->location, parser->lexer);
31305 cp_parser_consume_semicolon_at_end_of_statement (parser);
31306 if (req == error_mark_node)
31307 return error_mark_node;
31308 return finish_nested_requirement (loc, req);
31311 /* Support Functions */
31313 /* Return the appropriate prefer_type argument for lookup_name based on
31314 tag_type. */
31316 static inline LOOK_want
31317 prefer_type_arg (tag_types tag_type)
31319 switch (tag_type)
31321 case none_type: return LOOK_want::NORMAL; // No preference.
31322 case scope_type: return LOOK_want::TYPE_NAMESPACE; // Type or namespace.
31323 default: return LOOK_want::TYPE; // Type only.
31327 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
31328 NAME should have one of the representations used for an
31329 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
31330 is returned. If PARSER->SCOPE is a dependent type, then a
31331 SCOPE_REF is returned.
31333 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
31334 returned; the name was already resolved when the TEMPLATE_ID_EXPR
31335 was formed. Abstractly, such entities should not be passed to this
31336 function, because they do not need to be looked up, but it is
31337 simpler to check for this special case here, rather than at the
31338 call-sites.
31340 In cases not explicitly covered above, this function returns a
31341 DECL, OVERLOAD, or baselink representing the result of the lookup.
31342 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
31343 is returned.
31345 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
31346 (e.g., "struct") that was used. In that case bindings that do not
31347 refer to types are ignored.
31349 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
31350 ignored. If IS_TEMPLATE IS 2, the 'template' keyword was specified.
31352 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
31353 are ignored.
31355 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
31356 types.
31358 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
31359 TREE_LIST of candidates if name-lookup results in an ambiguity, and
31360 NULL_TREE otherwise. */
31362 static cp_expr
31363 cp_parser_lookup_name (cp_parser *parser, tree name,
31364 enum tag_types tag_type,
31365 int is_template,
31366 bool is_namespace,
31367 bool check_dependency,
31368 tree *ambiguous_decls,
31369 location_t name_location)
31371 tree decl;
31372 tree object_type = parser->context->object_type;
31374 /* Assume that the lookup will be unambiguous. */
31375 if (ambiguous_decls)
31376 *ambiguous_decls = NULL_TREE;
31378 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
31379 no longer valid. Note that if we are parsing tentatively, and
31380 the parse fails, OBJECT_TYPE will be automatically restored. */
31381 parser->context->object_type = NULL_TREE;
31383 if (name == error_mark_node)
31384 return error_mark_node;
31386 /* A template-id has already been resolved; there is no lookup to
31387 do. */
31388 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
31389 return name;
31390 if (BASELINK_P (name))
31392 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
31393 == TEMPLATE_ID_EXPR);
31394 return name;
31397 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
31398 it should already have been checked to make sure that the name
31399 used matches the type being destroyed. */
31400 if (TREE_CODE (name) == BIT_NOT_EXPR)
31402 tree type;
31404 /* Figure out to which type this destructor applies. */
31405 if (parser->scope)
31406 type = parser->scope;
31407 else if (object_type)
31408 type = object_type;
31409 else
31410 type = current_class_type;
31411 /* If that's not a class type, there is no destructor. */
31412 if (!type || !CLASS_TYPE_P (type))
31413 return error_mark_node;
31415 /* In a non-static member function, check implicit this->. */
31416 if (current_class_ref)
31417 return lookup_destructor (current_class_ref, parser->scope, name,
31418 tf_warning_or_error);
31420 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
31421 lazily_declare_fn (sfk_destructor, type);
31423 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
31424 return dtor;
31426 return error_mark_node;
31429 /* By this point, the NAME should be an ordinary identifier. If
31430 the id-expression was a qualified name, the qualifying scope is
31431 stored in PARSER->SCOPE at this point. */
31432 gcc_assert (identifier_p (name));
31434 /* Perform the lookup. */
31435 if (parser->scope)
31437 bool dependent_p;
31439 if (parser->scope == error_mark_node)
31440 return error_mark_node;
31442 /* If the SCOPE is dependent, the lookup must be deferred until
31443 the template is instantiated -- unless we are explicitly
31444 looking up names in uninstantiated templates. Even then, we
31445 cannot look up the name if the scope is not a class type; it
31446 might, for example, be a template type parameter. */
31447 dependent_p = (TYPE_P (parser->scope)
31448 && dependent_scope_p (parser->scope));
31449 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
31450 && dependent_p)
31451 /* Defer lookup. */
31452 decl = error_mark_node;
31453 else
31455 tree pushed_scope = NULL_TREE;
31457 /* If PARSER->SCOPE is a dependent type, then it must be a
31458 class type, and we must not be checking dependencies;
31459 otherwise, we would have processed this lookup above. So
31460 that PARSER->SCOPE is not considered a dependent base by
31461 lookup_member, we must enter the scope here. */
31462 if (dependent_p)
31463 pushed_scope = push_scope (parser->scope);
31465 /* If the PARSER->SCOPE is a template specialization, it
31466 may be instantiated during name lookup. In that case,
31467 errors may be issued. Even if we rollback the current
31468 tentative parse, those errors are valid. */
31469 decl = lookup_qualified_name (parser->scope, name,
31470 prefer_type_arg (tag_type),
31471 /*complain=*/true);
31473 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
31474 lookup result and the nested-name-specifier nominates a class C:
31475 * if the name specified after the nested-name-specifier, when
31476 looked up in C, is the injected-class-name of C (Clause 9), or
31477 * if the name specified after the nested-name-specifier is the
31478 same as the identifier or the simple-template-id's template-
31479 name in the last component of the nested-name-specifier,
31480 the name is instead considered to name the constructor of
31481 class C. [ Note: for example, the constructor is not an
31482 acceptable lookup result in an elaborated-type-specifier so
31483 the constructor would not be used in place of the
31484 injected-class-name. --end note ] Such a constructor name
31485 shall be used only in the declarator-id of a declaration that
31486 names a constructor or in a using-declaration. */
31487 if (tag_type == none_type
31488 && DECL_SELF_REFERENCE_P (decl)
31489 && same_type_p (DECL_CONTEXT (decl), parser->scope))
31490 decl = lookup_qualified_name (parser->scope, ctor_identifier,
31491 prefer_type_arg (tag_type),
31492 /*complain=*/true);
31494 if (pushed_scope)
31495 pop_scope (pushed_scope);
31498 /* If the scope is a dependent type and either we deferred lookup or
31499 we did lookup but didn't find the name, rememeber the name. */
31500 if (decl == error_mark_node && TYPE_P (parser->scope)
31501 && dependent_type_p (parser->scope))
31503 if (tag_type)
31505 tree type;
31507 /* The resolution to Core Issue 180 says that `struct
31508 A::B' should be considered a type-name, even if `A'
31509 is dependent. */
31510 type = make_typename_type (parser->scope, name, tag_type,
31511 /*complain=*/tf_error);
31512 if (type != error_mark_node)
31513 decl = TYPE_NAME (type);
31515 else if (is_template
31516 && (cp_parser_next_token_ends_template_argument_p (parser)
31517 || cp_lexer_next_token_is (parser->lexer,
31518 CPP_CLOSE_PAREN)))
31519 decl = make_unbound_class_template (parser->scope,
31520 name, NULL_TREE,
31521 /*complain=*/tf_error);
31522 else
31523 decl = build_qualified_name (/*type=*/NULL_TREE,
31524 parser->scope, name,
31525 is_template);
31527 parser->qualifying_scope = parser->scope;
31528 parser->object_scope = NULL_TREE;
31530 else if (object_type)
31532 bool dep = dependent_scope_p (object_type);
31534 /* Look up the name in the scope of the OBJECT_TYPE, unless the
31535 OBJECT_TYPE is not a class. */
31536 if (!dep && CLASS_TYPE_P (object_type))
31537 /* If the OBJECT_TYPE is a template specialization, it may
31538 be instantiated during name lookup. In that case, errors
31539 may be issued. Even if we rollback the current tentative
31540 parse, those errors are valid. */
31541 decl = lookup_member (object_type,
31542 name,
31543 /*protect=*/0,
31544 /*prefer_type=*/tag_type != none_type,
31545 tf_warning_or_error);
31546 else
31547 decl = NULL_TREE;
31549 /* If we didn't find a member and have dependent bases, the member lookup
31550 is now dependent. */
31551 if (!dep && !decl && any_dependent_bases_p (object_type))
31552 dep = true;
31554 if (dep && is_template == 2)
31555 /* The template keyword specifies a dependent template. */;
31556 else if (!decl)
31557 /* Look it up in the enclosing context. DR 141: When looking for a
31558 template-name after -> or ., only consider class templates. */
31559 decl = lookup_name (name, is_namespace ? LOOK_want::NAMESPACE
31560 /* DR 141: When looking in the
31561 current enclosing context for a
31562 template-name after -> or ., only
31563 consider class templates. */
31564 : is_template ? LOOK_want::TYPE
31565 : prefer_type_arg (tag_type));
31567 /* If we did unqualified lookup of a dependent member-qualified name and
31568 found something, do we want to use it? P1787 clarified that we need
31569 to look in the object scope first even if it's dependent, but for now
31570 let's still use it in some cases.
31571 FIXME remember unqualified lookup result to use if member lookup fails
31572 at instantiation time. */
31573 if (decl && dep && is_template)
31575 saved_token_sentinel toks (parser->lexer, STS_ROLLBACK);
31576 /* Only use the unqualified class template lookup if we're actually
31577 looking at a template arg list. */
31578 if (!cp_parser_skip_entire_template_parameter_list (parser))
31579 decl = NULL_TREE;
31582 /* If we know we're looking for a type (e.g. A in p->A::x),
31583 mock up a typename. */
31584 if (!decl && dep && tag_type != none_type)
31586 tree type = build_typename_type (object_type, name, name,
31587 typename_type);
31588 decl = TYPE_NAME (type);
31591 parser->object_scope = object_type;
31592 parser->qualifying_scope = NULL_TREE;
31594 else
31596 decl = lookup_name (name, is_namespace ? LOOK_want::NAMESPACE
31597 : prefer_type_arg (tag_type));
31598 parser->qualifying_scope = NULL_TREE;
31599 parser->object_scope = NULL_TREE;
31602 /* If the lookup failed, let our caller know. */
31603 if (!decl || decl == error_mark_node)
31604 return error_mark_node;
31606 /* If we have resolved the name of a member declaration, check to
31607 see if the declaration is accessible. When the name resolves to
31608 set of overloaded functions, accessibility is checked when
31609 overload resolution is done. If we have a TREE_LIST, then the lookup
31610 is either ambiguous or it found multiple injected-class-names, the
31611 accessibility of which is trivially satisfied.
31613 During an explicit instantiation, access is not checked at all,
31614 as per [temp.explicit]. */
31615 if (DECL_P (decl))
31616 check_accessibility_of_qualified_id (decl, object_type, parser->scope,
31617 tf_warning_or_error);
31619 /* Pull out the template from an injected-class-name (or multiple). */
31620 if (is_template)
31621 decl = maybe_get_template_decl_from_type_decl (decl);
31623 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
31624 if (TREE_CODE (decl) == TREE_LIST)
31626 if (ambiguous_decls)
31627 *ambiguous_decls = decl;
31628 /* The error message we have to print is too complicated for
31629 cp_parser_error, so we incorporate its actions directly. */
31630 if (!cp_parser_simulate_error (parser))
31632 error_at (name_location, "reference to %qD is ambiguous",
31633 name);
31634 print_candidates (decl);
31636 return error_mark_node;
31639 gcc_assert (DECL_P (decl)
31640 || TREE_CODE (decl) == OVERLOAD
31641 || TREE_CODE (decl) == SCOPE_REF
31642 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
31643 || BASELINK_P (decl));
31645 maybe_record_typedef_use (decl);
31647 return cp_expr (decl, name_location);
31650 /* Like cp_parser_lookup_name, but for use in the typical case where
31651 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
31652 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
31654 static tree
31655 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
31657 return cp_parser_lookup_name (parser, name,
31658 none_type,
31659 /*is_template=*/false,
31660 /*is_namespace=*/false,
31661 /*check_dependency=*/true,
31662 /*ambiguous_decls=*/NULL,
31663 location);
31666 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
31667 the current context, return the TYPE_DECL. If TAG_NAME_P is
31668 true, the DECL indicates the class being defined in a class-head,
31669 or declared in an elaborated-type-specifier.
31671 Otherwise, return DECL. */
31673 static tree
31674 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
31676 /* If the TEMPLATE_DECL is being declared as part of a class-head,
31677 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
31679 struct A {
31680 template <typename T> struct B;
31683 template <typename T> struct A::B {};
31685 Similarly, in an elaborated-type-specifier:
31687 namespace N { struct X{}; }
31689 struct A {
31690 template <typename T> friend struct N::X;
31693 However, if the DECL refers to a class type, and we are in
31694 the scope of the class, then the name lookup automatically
31695 finds the TYPE_DECL created by build_self_reference rather
31696 than a TEMPLATE_DECL. For example, in:
31698 template <class T> struct S {
31699 S s;
31702 there is no need to handle such case. */
31704 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
31705 return DECL_TEMPLATE_RESULT (decl);
31707 return decl;
31710 /* If too many, or too few, template-parameter lists apply to the
31711 declarator, issue an error message. Returns TRUE if all went well,
31712 and FALSE otherwise. */
31714 static bool
31715 cp_parser_check_declarator_template_parameters (cp_parser* parser,
31716 cp_declarator *declarator,
31717 location_t declarator_location)
31719 switch (declarator->kind)
31721 case cdk_id:
31723 unsigned num_templates = 0;
31724 tree scope = declarator->u.id.qualifying_scope;
31725 bool template_id_p = false;
31727 if (scope)
31728 num_templates = num_template_headers_for_class (scope);
31729 else if (TREE_CODE (declarator->u.id.unqualified_name)
31730 == TEMPLATE_ID_EXPR)
31732 /* If the DECLARATOR has the form `X<y>' then it uses one
31733 additional level of template parameters. */
31734 ++num_templates;
31735 template_id_p = true;
31738 return cp_parser_check_template_parameters
31739 (parser, num_templates, template_id_p, declarator_location,
31740 declarator);
31743 case cdk_function:
31744 case cdk_array:
31745 case cdk_pointer:
31746 case cdk_reference:
31747 case cdk_ptrmem:
31748 return (cp_parser_check_declarator_template_parameters
31749 (parser, declarator->declarator, declarator_location));
31751 case cdk_decomp:
31752 case cdk_error:
31753 return true;
31755 default:
31756 gcc_unreachable ();
31758 return false;
31761 /* NUM_TEMPLATES were used in the current declaration. If that is
31762 invalid, return FALSE and issue an error messages. Otherwise,
31763 return TRUE. If DECLARATOR is non-NULL, then we are checking a
31764 declarator and we can print more accurate diagnostics. */
31766 static bool
31767 cp_parser_check_template_parameters (cp_parser* parser,
31768 unsigned num_templates,
31769 bool template_id_p,
31770 location_t location,
31771 cp_declarator *declarator)
31773 /* If there are the same number of template classes and parameter
31774 lists, that's OK. */
31775 if (parser->num_template_parameter_lists == num_templates)
31776 return true;
31777 /* If there are more, but only one more, and the name ends in an identifier,
31778 then we are declaring a primary template. That's OK too. */
31779 if (!template_id_p
31780 && parser->num_template_parameter_lists == num_templates + 1)
31781 return true;
31783 if (cp_parser_simulate_error (parser))
31784 return false;
31786 /* If there are more template classes than parameter lists, we have
31787 something like:
31789 template <class T> void S<T>::R<T>::f (); */
31790 if (parser->num_template_parameter_lists < num_templates)
31792 if (declarator && !current_function_decl)
31793 error_at (location, "specializing member %<%T::%E%> "
31794 "requires %<template<>%> syntax",
31795 declarator->u.id.qualifying_scope,
31796 declarator->u.id.unqualified_name);
31797 else if (declarator)
31798 error_at (location, "invalid declaration of %<%T::%E%>",
31799 declarator->u.id.qualifying_scope,
31800 declarator->u.id.unqualified_name);
31801 else
31802 error_at (location, "too few template-parameter-lists");
31803 return false;
31805 /* Otherwise, there are too many template parameter lists. We have
31806 something like:
31808 template <class T> template <class U> void S::f(); */
31809 error_at (location, "too many template-parameter-lists");
31810 return false;
31813 /* Parse an optional `::' token indicating that the following name is
31814 from the global namespace. If so, PARSER->SCOPE is set to the
31815 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
31816 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
31817 Returns the new value of PARSER->SCOPE, if the `::' token is
31818 present, and NULL_TREE otherwise. */
31820 static tree
31821 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
31823 cp_token *token;
31825 /* Peek at the next token. */
31826 token = cp_lexer_peek_token (parser->lexer);
31827 /* If we're looking at a `::' token then we're starting from the
31828 global namespace, not our current location. */
31829 if (token->type == CPP_SCOPE)
31831 /* Consume the `::' token. */
31832 cp_lexer_consume_token (parser->lexer);
31833 /* Set the SCOPE so that we know where to start the lookup. */
31834 parser->scope = global_namespace;
31835 parser->qualifying_scope = global_namespace;
31836 parser->object_scope = NULL_TREE;
31838 return parser->scope;
31840 else if (!current_scope_valid_p)
31842 parser->scope = NULL_TREE;
31843 parser->qualifying_scope = NULL_TREE;
31844 parser->object_scope = NULL_TREE;
31847 return NULL_TREE;
31850 /* Returns TRUE if the upcoming token sequence is the start of a
31851 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
31852 declarator is preceded by the `friend' specifier. The parser flags FLAGS
31853 is used to control type-specifier parsing. */
31855 static bool
31856 cp_parser_constructor_declarator_p (cp_parser *parser, cp_parser_flags flags,
31857 bool friend_p)
31859 bool constructor_p;
31860 bool outside_class_specifier_p;
31861 tree nested_name_specifier;
31862 cp_token *next_token;
31864 /* The common case is that this is not a constructor declarator, so
31865 try to avoid doing lots of work if at all possible. It's not
31866 valid declare a constructor at function scope. */
31867 if (parser->in_function_body)
31868 return false;
31869 /* And only certain tokens can begin a constructor declarator. */
31870 next_token = cp_lexer_peek_token (parser->lexer);
31871 if (next_token->type != CPP_NAME
31872 && next_token->type != CPP_SCOPE
31873 && next_token->type != CPP_NESTED_NAME_SPECIFIER
31874 /* DR 2237 (C++20 only): A simple-template-id is no longer valid as the
31875 declarator-id of a constructor or destructor. */
31876 && (next_token->type != CPP_TEMPLATE_ID || cxx_dialect >= cxx20))
31877 return false;
31879 /* Parse tentatively; we are going to roll back all of the tokens
31880 consumed here. */
31881 cp_parser_parse_tentatively (parser);
31882 /* Assume that we are looking at a constructor declarator. */
31883 constructor_p = true;
31885 /* Look for the optional `::' operator. */
31886 cp_parser_global_scope_opt (parser,
31887 /*current_scope_valid_p=*/false);
31888 /* Look for the nested-name-specifier. */
31889 nested_name_specifier
31890 = (cp_parser_nested_name_specifier_opt (parser,
31891 /*typename_keyword_p=*/false,
31892 /*check_dependency_p=*/false,
31893 /*type_p=*/false,
31894 /*is_declaration=*/false));
31896 /* Resolve the TYPENAME_TYPE, because the call above didn't do it. */
31897 if (nested_name_specifier
31898 && TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
31900 tree s = resolve_typename_type (nested_name_specifier,
31901 /*only_current_p=*/false);
31902 if (TREE_CODE (s) != TYPENAME_TYPE)
31903 nested_name_specifier = s;
31906 outside_class_specifier_p = (!at_class_scope_p ()
31907 || !TYPE_BEING_DEFINED (current_class_type)
31908 || friend_p);
31910 /* Outside of a class-specifier, there must be a
31911 nested-name-specifier. Except in C++17 mode, where we
31912 might be declaring a guiding declaration. */
31913 if (!nested_name_specifier && outside_class_specifier_p
31914 && cxx_dialect < cxx17)
31915 constructor_p = false;
31916 else if (nested_name_specifier == error_mark_node)
31917 constructor_p = false;
31919 /* If we have a class scope, this is easy; DR 147 says that S::S always
31920 names the constructor, and no other qualified name could. */
31921 if (constructor_p && nested_name_specifier
31922 && CLASS_TYPE_P (nested_name_specifier))
31924 tree id = cp_parser_unqualified_id (parser,
31925 /*template_keyword_p=*/false,
31926 /*check_dependency_p=*/false,
31927 /*declarator_p=*/true,
31928 /*optional_p=*/false);
31929 if (is_overloaded_fn (id))
31930 id = DECL_NAME (get_first_fn (id));
31931 if (!constructor_name_p (id, nested_name_specifier))
31932 constructor_p = false;
31934 /* If we still think that this might be a constructor-declarator,
31935 look for a class-name. */
31936 else if (constructor_p)
31938 /* If we have:
31940 template <typename T> struct S {
31941 S();
31944 we must recognize that the nested `S' names a class. */
31945 if (cxx_dialect >= cxx17)
31946 cp_parser_parse_tentatively (parser);
31948 tree type_decl;
31949 type_decl = cp_parser_class_name (parser,
31950 /*typename_keyword_p=*/false,
31951 /*template_keyword_p=*/false,
31952 none_type,
31953 /*check_dependency_p=*/false,
31954 /*class_head_p=*/false,
31955 /*is_declaration=*/false);
31957 if (cxx_dialect >= cxx17
31958 && !cp_parser_parse_definitely (parser))
31960 type_decl = NULL_TREE;
31961 tree tmpl = cp_parser_template_name (parser,
31962 /*template_keyword*/false,
31963 /*check_dependency_p*/false,
31964 /*is_declaration*/false,
31965 none_type,
31966 /*is_identifier*/NULL);
31967 if (DECL_CLASS_TEMPLATE_P (tmpl)
31968 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
31969 /* It's a deduction guide, return true. */;
31970 else
31971 cp_parser_simulate_error (parser);
31974 /* If there was no class-name, then this is not a constructor.
31975 Otherwise, if we are in a class-specifier and we aren't
31976 handling a friend declaration, check that its type matches
31977 current_class_type (c++/38313). Note: error_mark_node
31978 is left alone for error recovery purposes. */
31979 constructor_p = (!cp_parser_error_occurred (parser)
31980 && (outside_class_specifier_p
31981 || type_decl == NULL_TREE
31982 || type_decl == error_mark_node
31983 || same_type_p (current_class_type,
31984 TREE_TYPE (type_decl))));
31986 /* If we're still considering a constructor, we have to see a `(',
31987 to begin the parameter-declaration-clause, followed by either a
31988 `)', an `...', or a decl-specifier. We need to check for a
31989 type-specifier to avoid being fooled into thinking that:
31991 S (f) (int);
31993 is a constructor. (It is actually a function named `f' that
31994 takes one parameter (of type `int') and returns a value of type
31995 `S'. */
31996 if (constructor_p
31997 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31998 constructor_p = false;
32000 if (constructor_p
32001 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
32002 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
32003 /* A parameter declaration begins with a decl-specifier,
32004 which is either the "attribute" keyword, a storage class
32005 specifier, or (usually) a type-specifier. */
32006 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer)
32007 /* GNU attributes can actually appear both at the start of
32008 a parameter and parenthesized declarator.
32009 S (__attribute__((unused)) int);
32010 is a constructor, but
32011 S (__attribute__((unused)) foo) (int);
32012 is a function declaration. [[attribute]] can appear in the
32013 first form too, but not in the second form. */
32014 && !cp_next_tokens_can_be_std_attribute_p (parser))
32016 tree type;
32017 tree pushed_scope = NULL_TREE;
32018 unsigned saved_num_template_parameter_lists;
32020 if (cp_parser_allow_gnu_extensions_p (parser)
32021 && cp_next_tokens_can_be_gnu_attribute_p (parser))
32023 unsigned int n = cp_parser_skip_gnu_attributes_opt (parser, 1);
32024 while (--n)
32025 cp_lexer_consume_token (parser->lexer);
32028 /* Names appearing in the type-specifier should be looked up
32029 in the scope of the class. */
32030 if (current_class_type)
32031 type = NULL_TREE;
32032 else if (type_decl)
32034 type = TREE_TYPE (type_decl);
32035 if (TREE_CODE (type) == TYPENAME_TYPE)
32037 type = resolve_typename_type (type,
32038 /*only_current_p=*/false);
32039 if (TREE_CODE (type) == TYPENAME_TYPE)
32041 cp_parser_abort_tentative_parse (parser);
32042 return false;
32045 pushed_scope = push_scope (type);
32048 /* Inside the constructor parameter list, surrounding
32049 template-parameter-lists do not apply. */
32050 saved_num_template_parameter_lists
32051 = parser->num_template_parameter_lists;
32052 parser->num_template_parameter_lists = 0;
32054 /* Look for the type-specifier. It's not optional, but its typename
32055 might be. Unless this is a friend declaration; we don't want to
32056 treat
32058 friend S (T::fn)(int);
32060 as a constructor, but with P0634, we might assume a type when
32061 looking for the type-specifier. It is actually a function named
32062 `T::fn' that takes one parameter (of type `int') and returns a
32063 value of type `S'. Constructors can be friends, but they must
32064 use a qualified name.
32066 Parse with an empty set of declaration specifiers since we're
32067 trying to match a decl-specifier-seq of the first parameter.
32068 This must be non-null so that cp_parser_simple_type_specifier
32069 will recognize a constrained placeholder type such as:
32070 'C<int> auto' where C is a type concept. */
32071 cp_decl_specifier_seq ctor_specs;
32072 clear_decl_specs (&ctor_specs);
32073 cp_parser_type_specifier (parser,
32074 (friend_p ? CP_PARSER_FLAGS_NONE
32075 : (flags & ~CP_PARSER_FLAGS_OPTIONAL)),
32076 /*decl_specs=*/&ctor_specs,
32077 /*is_declarator=*/true,
32078 /*declares_class_or_enum=*/NULL,
32079 /*is_cv_qualifier=*/NULL);
32081 parser->num_template_parameter_lists
32082 = saved_num_template_parameter_lists;
32084 /* Leave the scope of the class. */
32085 if (pushed_scope)
32086 pop_scope (pushed_scope);
32088 constructor_p = !cp_parser_error_occurred (parser);
32092 /* We did not really want to consume any tokens. */
32093 cp_parser_abort_tentative_parse (parser);
32095 return constructor_p;
32098 /* Parse the definition of the function given by the DECL_SPECIFIERS,
32099 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
32100 they must be performed once we are in the scope of the function.
32102 Returns the function defined. */
32104 static tree
32105 cp_parser_function_definition_from_specifiers_and_declarator
32106 (cp_parser* parser,
32107 cp_decl_specifier_seq *decl_specifiers,
32108 tree attributes,
32109 const cp_declarator *declarator)
32111 tree fn;
32112 bool success_p;
32114 /* Begin the function-definition. */
32115 success_p = start_function (decl_specifiers, declarator, attributes);
32117 /* The things we're about to see are not directly qualified by any
32118 template headers we've seen thus far. */
32119 reset_specialization ();
32121 /* If there were names looked up in the decl-specifier-seq that we
32122 did not check, check them now. We must wait until we are in the
32123 scope of the function to perform the checks, since the function
32124 might be a friend. */
32125 perform_deferred_access_checks (tf_warning_or_error);
32127 if (success_p)
32129 cp_finalize_omp_declare_simd (parser, current_function_decl);
32130 parser->omp_declare_simd = NULL;
32131 cp_finalize_oacc_routine (parser, current_function_decl, true);
32132 parser->oacc_routine = NULL;
32135 if (!success_p)
32137 /* Skip the entire function. */
32138 cp_parser_skip_to_end_of_block_or_statement (parser);
32139 fn = error_mark_node;
32141 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
32143 /* Seen already, skip it. An error message has already been output. */
32144 cp_parser_skip_to_end_of_block_or_statement (parser);
32145 fn = current_function_decl;
32146 current_function_decl = NULL_TREE;
32147 /* If this is a function from a class, pop the nested class. */
32148 if (current_class_name)
32149 pop_nested_class ();
32151 else
32153 auto_timevar tv (DECL_DECLARED_INLINE_P (current_function_decl)
32154 ? TV_PARSE_INLINE : TV_PARSE_FUNC);
32155 fn = cp_parser_function_definition_after_declarator (parser,
32156 /*inline_p=*/false);
32159 return fn;
32162 /* Parse the part of a function-definition that follows the
32163 declarator. INLINE_P is TRUE iff this function is an inline
32164 function defined within a class-specifier.
32166 Returns the function defined. */
32168 static tree
32169 cp_parser_function_definition_after_declarator (cp_parser* parser,
32170 bool inline_p)
32172 tree fn;
32173 bool saved_in_unbraced_linkage_specification_p;
32174 bool saved_in_function_body;
32175 unsigned saved_num_template_parameter_lists;
32176 cp_token *token;
32177 bool fully_implicit_function_template_p
32178 = parser->fully_implicit_function_template_p;
32179 parser->fully_implicit_function_template_p = false;
32180 tree implicit_template_parms
32181 = parser->implicit_template_parms;
32182 parser->implicit_template_parms = 0;
32183 cp_binding_level* implicit_template_scope
32184 = parser->implicit_template_scope;
32185 parser->implicit_template_scope = 0;
32187 saved_in_function_body = parser->in_function_body;
32188 parser->in_function_body = true;
32189 /* If the next token is `return', then the code may be trying to
32190 make use of the "named return value" extension that G++ used to
32191 support. */
32192 token = cp_lexer_peek_token (parser->lexer);
32193 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
32195 /* Consume the `return' keyword. */
32196 cp_lexer_consume_token (parser->lexer);
32197 /* Look for the identifier that indicates what value is to be
32198 returned. */
32199 cp_parser_identifier (parser);
32200 /* Issue an error message. */
32201 error_at (token->location,
32202 "named return values are no longer supported");
32203 /* Skip tokens until we reach the start of the function body. */
32204 while (true)
32206 cp_token *token = cp_lexer_peek_token (parser->lexer);
32207 if (token->type == CPP_OPEN_BRACE
32208 || token->type == CPP_EOF
32209 || token->type == CPP_PRAGMA_EOL)
32210 break;
32211 cp_lexer_consume_token (parser->lexer);
32214 /* The `extern' in `extern "C" void f () { ... }' does not apply to
32215 anything declared inside `f'. */
32216 saved_in_unbraced_linkage_specification_p
32217 = parser->in_unbraced_linkage_specification_p;
32218 parser->in_unbraced_linkage_specification_p = false;
32219 /* Inside the function, surrounding template-parameter-lists do not
32220 apply. */
32221 saved_num_template_parameter_lists
32222 = parser->num_template_parameter_lists;
32223 parser->num_template_parameter_lists = 0;
32225 /* If the next token is `try', `__transaction_atomic', or
32226 `__transaction_relaxed`, then we are looking at either function-try-block
32227 or function-transaction-block. Note that all of these include the
32228 function-body. */
32229 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
32230 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
32231 else if (cp_lexer_next_token_is_keyword (parser->lexer,
32232 RID_TRANSACTION_RELAXED))
32233 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
32234 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
32235 cp_parser_function_try_block (parser);
32236 else
32237 cp_parser_ctor_initializer_opt_and_function_body
32238 (parser, /*in_function_try_block=*/false);
32240 /* Finish the function. */
32241 fn = finish_function (inline_p);
32243 if (modules_p ()
32244 && !inline_p
32245 && TYPE_P (DECL_CONTEXT (fn))
32246 && (DECL_DECLARED_INLINE_P (fn)
32247 || processing_template_decl))
32248 set_defining_module (fn);
32250 /* Generate code for it, if necessary. */
32251 expand_or_defer_fn (fn);
32253 /* Restore the saved values. */
32254 parser->in_unbraced_linkage_specification_p
32255 = saved_in_unbraced_linkage_specification_p;
32256 parser->num_template_parameter_lists
32257 = saved_num_template_parameter_lists;
32258 parser->in_function_body = saved_in_function_body;
32260 parser->fully_implicit_function_template_p
32261 = fully_implicit_function_template_p;
32262 parser->implicit_template_parms
32263 = implicit_template_parms;
32264 parser->implicit_template_scope
32265 = implicit_template_scope;
32267 if (parser->fully_implicit_function_template_p)
32268 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
32270 return fn;
32273 /* Parse a template-declaration body (following argument list). */
32275 static void
32276 cp_parser_template_declaration_after_parameters (cp_parser* parser,
32277 tree parameter_list,
32278 bool member_p)
32280 tree decl = NULL_TREE;
32281 bool friend_p = false;
32283 /* We just processed one more parameter list. */
32284 ++parser->num_template_parameter_lists;
32286 /* Get the deferred access checks from the parameter list. These
32287 will be checked once we know what is being declared, as for a
32288 member template the checks must be performed in the scope of the
32289 class containing the member. */
32290 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
32292 /* Tentatively parse for a new template parameter list, which can either be
32293 the template keyword or a template introduction. */
32294 if (cp_parser_template_declaration_after_export (parser, member_p))
32295 /* OK */;
32296 else if (cxx_dialect >= cxx11
32297 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
32298 decl = cp_parser_alias_declaration (parser);
32299 else if (flag_concepts
32300 && cp_lexer_next_token_is_keyword (parser->lexer, RID_CONCEPT)
32301 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
32302 /* -fconcept-ts 'concept bool' syntax is handled below, in
32303 cp_parser_single_declaration. */
32304 decl = cp_parser_concept_definition (parser);
32305 else
32307 cp_token *token = cp_lexer_peek_token (parser->lexer);
32308 decl = cp_parser_single_declaration (parser,
32309 checks,
32310 member_p,
32311 /*explicit_specialization_p=*/false,
32312 &friend_p);
32314 /* If this is a member template declaration, let the front
32315 end know. */
32316 if (member_p && !friend_p && decl)
32318 if (TREE_CODE (decl) == TYPE_DECL)
32319 cp_parser_check_access_in_redeclaration (decl, token->location);
32321 decl = finish_member_template_decl (decl);
32323 else if (friend_p && decl
32324 && DECL_DECLARES_TYPE_P (decl))
32325 make_friend_class (current_class_type, TREE_TYPE (decl),
32326 /*complain=*/true);
32328 /* We are done with the current parameter list. */
32329 --parser->num_template_parameter_lists;
32331 pop_deferring_access_checks ();
32333 /* Finish up. */
32334 finish_template_decl (parameter_list);
32336 /* Check the template arguments for a literal operator template. */
32337 if (decl
32338 && DECL_DECLARES_FUNCTION_P (decl)
32339 && UDLIT_OPER_P (DECL_NAME (decl)))
32341 bool ok = true;
32342 if (parameter_list == NULL_TREE)
32343 ok = false;
32344 else
32346 int num_parms = TREE_VEC_LENGTH (parameter_list);
32347 if (num_parms == 1)
32349 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
32350 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
32351 if (TREE_CODE (parm) != PARM_DECL)
32352 ok = false;
32353 else if (MAYBE_CLASS_TYPE_P (TREE_TYPE (parm))
32354 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
32355 /* OK, C++20 string literal operator template. We don't need
32356 to warn in lower dialects here because we will have already
32357 warned about the template parameter. */;
32358 else if (TREE_TYPE (parm) != char_type_node
32359 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
32360 ok = false;
32362 else if (num_parms == 2 && cxx_dialect >= cxx14)
32364 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
32365 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
32366 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
32367 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
32368 if (TREE_CODE (parm) != PARM_DECL
32369 || TREE_TYPE (parm) != TREE_TYPE (type)
32370 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
32371 ok = false;
32372 else
32373 /* http://cplusplus.github.io/EWG/ewg-active.html#66 */
32374 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
32375 "ISO C++ did not adopt string literal operator templa"
32376 "tes taking an argument pack of characters");
32378 else
32379 ok = false;
32381 if (!ok)
32383 if (cxx_dialect > cxx17)
32384 error_at (DECL_SOURCE_LOCATION (decl), "literal operator "
32385 "template %qD has invalid parameter list; expected "
32386 "non-type template parameter pack %<<char...>%> or "
32387 "single non-type parameter of class type",
32388 decl);
32389 else
32390 error_at (DECL_SOURCE_LOCATION (decl), "literal operator "
32391 "template %qD has invalid parameter list; expected "
32392 "non-type template parameter pack %<<char...>%>",
32393 decl);
32397 /* Register member declarations. */
32398 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
32399 finish_member_declaration (decl);
32400 /* If DECL is a function template, we must return to parse it later.
32401 (Even though there is no definition, there might be default
32402 arguments that need handling.) */
32403 if (member_p && decl
32404 && DECL_DECLARES_FUNCTION_P (decl))
32405 vec_safe_push (unparsed_funs_with_definitions, decl);
32408 /* Parse a template introduction header for a template-declaration. Returns
32409 false if tentative parse fails. */
32411 static bool
32412 cp_parser_template_introduction (cp_parser* parser, bool member_p)
32414 cp_parser_parse_tentatively (parser);
32416 tree saved_scope = parser->scope;
32417 tree saved_object_scope = parser->object_scope;
32418 tree saved_qualifying_scope = parser->qualifying_scope;
32419 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32421 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
32423 /* In classes don't parse valid unnamed bitfields as invalid
32424 template introductions. */
32425 if (member_p)
32426 parser->colon_corrects_to_scope_p = false;
32428 /* Look for the optional `::' operator. */
32429 cp_parser_global_scope_opt (parser,
32430 /*current_scope_valid_p=*/false);
32431 /* Look for the nested-name-specifier. */
32432 cp_parser_nested_name_specifier_opt (parser,
32433 /*typename_keyword_p=*/false,
32434 /*check_dependency_p=*/true,
32435 /*type_p=*/false,
32436 /*is_declaration=*/false);
32438 cp_token *token = cp_lexer_peek_token (parser->lexer);
32439 tree concept_name = cp_parser_identifier (parser);
32441 /* Look up the concept for which we will be matching
32442 template parameters. */
32443 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
32444 token->location);
32445 parser->scope = saved_scope;
32446 parser->object_scope = saved_object_scope;
32447 parser->qualifying_scope = saved_qualifying_scope;
32448 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32450 if (concept_name == error_mark_node
32451 || (seen_error () && !concept_definition_p (tmpl_decl)))
32452 cp_parser_simulate_error (parser);
32454 /* Look for opening brace for introduction. */
32455 matching_braces braces;
32456 braces.require_open (parser);
32457 location_t open_loc = input_location;
32459 if (!cp_parser_parse_definitely (parser))
32460 return false;
32462 push_deferring_access_checks (dk_deferred);
32464 /* Build vector of placeholder parameters and grab
32465 matching identifiers. */
32466 tree introduction_list = cp_parser_introduction_list (parser);
32468 /* Look for closing brace for introduction. */
32469 if (!braces.require_close (parser))
32470 return true;
32472 /* The introduction-list shall not be empty. */
32473 int nargs = TREE_VEC_LENGTH (introduction_list);
32474 if (nargs == 0)
32476 /* In cp_parser_introduction_list we have already issued an error. */
32477 return true;
32480 if (tmpl_decl == error_mark_node)
32482 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
32483 token->location);
32484 return true;
32487 /* Build and associate the constraint. */
32488 location_t introduction_loc = make_location (open_loc,
32489 start_token->location,
32490 parser->lexer);
32491 tree parms = finish_template_introduction (tmpl_decl,
32492 introduction_list,
32493 introduction_loc);
32494 if (parms && parms != error_mark_node)
32496 if (!flag_concepts_ts)
32497 pedwarn (introduction_loc, 0, "template-introductions"
32498 " are not part of C++20 concepts; use %qs to enable",
32499 "-fconcepts-ts");
32501 cp_parser_template_declaration_after_parameters (parser, parms,
32502 member_p);
32503 return true;
32506 if (parms == NULL_TREE)
32507 error_at (token->location, "no matching concept for template-introduction");
32509 return true;
32512 /* Parse a normal template-declaration following the template keyword. */
32514 static void
32515 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
32517 tree parameter_list;
32518 bool need_lang_pop;
32519 location_t location = input_location;
32521 /* Look for the `<' token. */
32522 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
32523 return;
32524 if (at_class_scope_p () && current_function_decl)
32526 /* 14.5.2.2 [temp.mem]
32528 A local class shall not have member templates. */
32529 error_at (location,
32530 "invalid declaration of member template in local class");
32531 cp_parser_skip_to_end_of_block_or_statement (parser);
32532 return;
32534 /* [temp]
32536 A template ... shall not have C linkage. */
32537 if (current_lang_name == lang_name_c)
32539 error_at (location, "template with C linkage");
32540 maybe_show_extern_c_location ();
32541 /* Give it C++ linkage to avoid confusing other parts of the
32542 front end. */
32543 push_lang_context (lang_name_cplusplus);
32544 need_lang_pop = true;
32546 else
32547 need_lang_pop = false;
32549 /* We cannot perform access checks on the template parameter
32550 declarations until we know what is being declared, just as we
32551 cannot check the decl-specifier list. */
32552 push_deferring_access_checks (dk_deferred);
32554 /* If the next token is `>', then we have an invalid
32555 specialization. Rather than complain about an invalid template
32556 parameter, issue an error message here. */
32557 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
32559 cp_parser_error (parser, "invalid explicit specialization");
32560 begin_specialization ();
32561 parameter_list = NULL_TREE;
32563 else
32565 /* Parse the template parameters. */
32566 parameter_list = cp_parser_template_parameter_list (parser);
32569 /* Look for the `>'. */
32570 cp_parser_require_end_of_template_parameter_list (parser);
32572 /* Manage template requirements */
32573 if (flag_concepts)
32575 tree reqs = get_shorthand_constraints (current_template_parms);
32576 if (tree treqs = cp_parser_requires_clause_opt (parser, false))
32577 reqs = combine_constraint_expressions (reqs, treqs);
32578 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
32581 cp_parser_template_declaration_after_parameters (parser, parameter_list,
32582 member_p);
32584 /* For the erroneous case of a template with C linkage, we pushed an
32585 implicit C++ linkage scope; exit that scope now. */
32586 if (need_lang_pop)
32587 pop_lang_context ();
32590 /* Parse a template-declaration, assuming that the `export' (and
32591 `extern') keywords, if present, has already been scanned. MEMBER_P
32592 is as for cp_parser_template_declaration. */
32594 static bool
32595 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
32597 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
32599 cp_lexer_consume_token (parser->lexer);
32600 cp_parser_explicit_template_declaration (parser, member_p);
32601 return true;
32603 else if (flag_concepts)
32604 return cp_parser_template_introduction (parser, member_p);
32606 return false;
32609 /* Perform the deferred access checks from a template-parameter-list.
32610 CHECKS is a TREE_LIST of access checks, as returned by
32611 get_deferred_access_checks. */
32613 static void
32614 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
32616 ++processing_template_parmlist;
32617 perform_access_checks (checks, tf_warning_or_error);
32618 --processing_template_parmlist;
32621 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
32622 `function-definition' sequence that follows a template header.
32623 If MEMBER_P is true, this declaration appears in a class scope.
32625 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
32626 *FRIEND_P is set to TRUE iff the declaration is a friend. */
32628 static tree
32629 cp_parser_single_declaration (cp_parser* parser,
32630 vec<deferred_access_check, va_gc> *checks,
32631 bool member_p,
32632 bool explicit_specialization_p,
32633 bool* friend_p)
32635 int declares_class_or_enum;
32636 tree decl = NULL_TREE;
32637 cp_decl_specifier_seq decl_specifiers;
32638 bool function_definition_p = false;
32639 cp_token *decl_spec_token_start;
32641 /* This function is only used when processing a template
32642 declaration. */
32643 gcc_assert (innermost_scope_kind () == sk_template_parms
32644 || innermost_scope_kind () == sk_template_spec);
32646 /* Defer access checks until we know what is being declared. */
32647 push_deferring_access_checks (dk_deferred);
32649 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
32650 alternative. */
32651 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
32652 cp_parser_decl_specifier_seq (parser,
32653 (CP_PARSER_FLAGS_OPTIONAL
32654 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
32655 &decl_specifiers,
32656 &declares_class_or_enum);
32658 cp_omp_declare_simd_data odsd;
32659 if (decl_specifiers.attributes && (flag_openmp || flag_openmp_simd))
32660 cp_parser_handle_directive_omp_attributes (parser,
32661 &decl_specifiers.attributes,
32662 &odsd, true);
32664 if (friend_p)
32665 *friend_p = cp_parser_friend_p (&decl_specifiers);
32667 /* There are no template typedefs. */
32668 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
32670 error_at (decl_spec_token_start->location,
32671 "template declaration of %<typedef%>");
32672 decl = error_mark_node;
32675 /* Gather up the access checks that occurred the
32676 decl-specifier-seq. */
32677 stop_deferring_access_checks ();
32679 /* Check for the declaration of a template class. */
32680 if (declares_class_or_enum)
32682 if (cp_parser_declares_only_class_p (parser)
32683 || (declares_class_or_enum & 2))
32685 decl = shadow_tag (&decl_specifiers);
32687 /* In this case:
32689 struct C {
32690 friend template <typename T> struct A<T>::B;
32693 A<T>::B will be represented by a TYPENAME_TYPE, and
32694 therefore not recognized by shadow_tag. */
32695 if (friend_p && *friend_p
32696 && !decl
32697 && decl_specifiers.type
32698 && TYPE_P (decl_specifiers.type))
32699 decl = decl_specifiers.type;
32701 if (decl && decl != error_mark_node)
32702 decl = TYPE_NAME (decl);
32703 else
32704 decl = error_mark_node;
32706 /* If this is a declaration, but not a definition, associate
32707 any constraints with the type declaration. Constraints
32708 are associated with definitions in cp_parser_class_specifier. */
32709 if (declares_class_or_enum == 1)
32710 associate_classtype_constraints (TREE_TYPE (decl));
32712 /* Perform access checks for template parameters. */
32713 cp_parser_perform_template_parameter_access_checks (checks);
32715 /* Give a helpful diagnostic for
32716 template <class T> struct A { } a;
32717 if we aren't already recovering from an error. */
32718 if (!cp_parser_declares_only_class_p (parser)
32719 && !seen_error ())
32721 error_at (cp_lexer_peek_token (parser->lexer)->location,
32722 "a class template declaration must not declare "
32723 "anything else");
32724 cp_parser_skip_to_end_of_block_or_statement (parser);
32725 goto out;
32730 /* Complain about missing 'typename' or other invalid type names. */
32731 if (!decl_specifiers.any_type_specifiers_p
32732 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
32734 /* cp_parser_parse_and_diagnose_invalid_type_name calls
32735 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
32736 the rest of this declaration. */
32737 decl = error_mark_node;
32738 goto out;
32741 /* If it's not a template class, try for a template function. If
32742 the next token is a `;', then this declaration does not declare
32743 anything. But, if there were errors in the decl-specifiers, then
32744 the error might well have come from an attempted class-specifier.
32745 In that case, there's no need to warn about a missing declarator. */
32746 if (!decl
32747 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
32748 || decl_specifiers.type != error_mark_node))
32750 int flags = CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
32751 /* We don't delay parsing for friends, though CWG 2510 may change
32752 that. */
32753 if (member_p && !(friend_p && *friend_p))
32754 flags |= CP_PARSER_FLAGS_DELAY_NOEXCEPT;
32755 decl = cp_parser_init_declarator (parser,
32756 flags,
32757 &decl_specifiers,
32758 checks,
32759 /*function_definition_allowed_p=*/true,
32760 member_p,
32761 declares_class_or_enum,
32762 &function_definition_p,
32763 NULL, NULL, NULL);
32765 /* 7.1.1-1 [dcl.stc]
32767 A storage-class-specifier shall not be specified in an explicit
32768 specialization... */
32769 if (decl
32770 && explicit_specialization_p
32771 && decl_specifiers.storage_class != sc_none)
32773 error_at (decl_spec_token_start->location,
32774 "explicit template specialization cannot have a storage class");
32775 decl = error_mark_node;
32778 if (decl && VAR_P (decl))
32779 check_template_variable (decl);
32782 /* Look for a trailing `;' after the declaration. */
32783 if (!function_definition_p
32784 && (decl == error_mark_node
32785 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
32786 cp_parser_skip_to_end_of_block_or_statement (parser);
32788 out:
32789 pop_deferring_access_checks ();
32791 /* Clear any current qualification; whatever comes next is the start
32792 of something new. */
32793 parser->scope = NULL_TREE;
32794 parser->qualifying_scope = NULL_TREE;
32795 parser->object_scope = NULL_TREE;
32797 cp_finalize_omp_declare_simd (parser, &odsd);
32799 return decl;
32802 /* Parse a cast-expression that is not the operand of a unary "&". */
32804 static cp_expr
32805 cp_parser_simple_cast_expression (cp_parser *parser)
32807 return cp_parser_cast_expression (parser, /*address_p=*/false,
32808 /*cast_p=*/false, /*decltype*/false, NULL);
32811 /* Parse a functional cast to TYPE. Returns an expression
32812 representing the cast. */
32814 static cp_expr
32815 cp_parser_functional_cast (cp_parser* parser, tree type)
32817 vec<tree, va_gc> *vec;
32818 tree expression_list;
32819 cp_expr cast;
32821 location_t start_loc = input_location;
32823 if (!type)
32824 type = error_mark_node;
32826 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
32828 cp_lexer_set_source_position (parser->lexer);
32829 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
32830 expression_list = cp_parser_braced_list (parser);
32831 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
32832 if (TREE_CODE (type) == TYPE_DECL)
32833 type = TREE_TYPE (type);
32835 cast = finish_compound_literal (type, expression_list,
32836 tf_warning_or_error, fcl_functional);
32837 /* Create a location of the form:
32838 type_name{i, f}
32839 ^~~~~~~~~~~~~~~
32840 with caret == start at the start of the type name,
32841 finishing at the closing brace. */
32842 location_t combined_loc = make_location (start_loc, start_loc,
32843 parser->lexer);
32844 cast.set_location (combined_loc);
32845 return cast;
32849 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
32850 /*cast_p=*/true,
32851 /*allow_expansion_p=*/true,
32852 /*non_constant_p=*/NULL);
32853 if (vec == NULL)
32854 expression_list = error_mark_node;
32855 else
32857 expression_list = build_tree_list_vec (vec);
32858 release_tree_vector (vec);
32861 /* Create a location of the form:
32862 float(i)
32863 ^~~~~~~~
32864 with caret == start at the start of the type name,
32865 finishing at the closing paren. */
32866 location_t combined_loc = make_location (start_loc, start_loc,
32867 parser->lexer);
32868 cast = build_functional_cast (combined_loc, type, expression_list,
32869 tf_warning_or_error);
32871 /* [expr.const]/1: In an integral constant expression "only type
32872 conversions to integral or enumeration type can be used". */
32873 if (TREE_CODE (type) == TYPE_DECL)
32874 type = TREE_TYPE (type);
32875 if (cast != error_mark_node
32876 && !cast_valid_in_integral_constant_expression_p (type)
32877 && cp_parser_non_integral_constant_expression (parser,
32878 NIC_CONSTRUCTOR))
32879 return error_mark_node;
32881 return cast;
32884 /* Save the tokens that make up the body of a member function defined
32885 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
32886 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
32887 specifiers applied to the declaration. Returns the FUNCTION_DECL
32888 for the member function. */
32890 static tree
32891 cp_parser_save_member_function_body (cp_parser* parser,
32892 cp_decl_specifier_seq *decl_specifiers,
32893 cp_declarator *declarator,
32894 tree attributes)
32896 cp_token *first;
32897 cp_token *last;
32898 tree fn;
32899 bool function_try_block = false;
32901 /* Create the FUNCTION_DECL. */
32902 fn = grokmethod (decl_specifiers, declarator, attributes);
32903 cp_finalize_omp_declare_simd (parser, fn);
32904 cp_finalize_oacc_routine (parser, fn, true);
32905 /* If something went badly wrong, bail out now. */
32906 if (fn == error_mark_node)
32908 /* If there's a function-body, skip it. */
32909 if (cp_parser_token_starts_function_definition_p
32910 (cp_lexer_peek_token (parser->lexer)))
32911 cp_parser_skip_to_end_of_block_or_statement (parser);
32912 return error_mark_node;
32915 /* Remember it, if there are default args to post process. */
32916 cp_parser_save_default_args (parser, fn);
32918 /* Save away the tokens that make up the body of the
32919 function. */
32920 first = parser->lexer->next_token;
32922 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
32923 cp_lexer_consume_token (parser->lexer);
32924 else if (cp_lexer_next_token_is_keyword (parser->lexer,
32925 RID_TRANSACTION_ATOMIC))
32927 cp_lexer_consume_token (parser->lexer);
32928 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
32929 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
32930 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
32931 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
32932 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
32933 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
32934 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
32936 cp_lexer_consume_token (parser->lexer);
32937 cp_lexer_consume_token (parser->lexer);
32938 cp_lexer_consume_token (parser->lexer);
32939 cp_lexer_consume_token (parser->lexer);
32940 cp_lexer_consume_token (parser->lexer);
32942 else
32943 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
32944 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
32946 cp_lexer_consume_token (parser->lexer);
32947 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
32948 break;
32952 /* Handle function try blocks. */
32953 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
32955 cp_lexer_consume_token (parser->lexer);
32956 function_try_block = true;
32958 /* We can have braced-init-list mem-initializers before the fn body. */
32959 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
32961 cp_lexer_consume_token (parser->lexer);
32962 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
32964 /* cache_group will stop after an un-nested { } pair, too. */
32965 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
32966 break;
32968 /* variadic mem-inits have ... after the ')'. */
32969 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
32970 cp_lexer_consume_token (parser->lexer);
32973 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
32974 /* Handle function try blocks. */
32975 if (function_try_block)
32976 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
32977 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
32978 last = parser->lexer->next_token;
32980 /* Save away the inline definition; we will process it when the
32981 class is complete. */
32982 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
32983 DECL_PENDING_INLINE_P (fn) = 1;
32985 /* We need to know that this was defined in the class, so that
32986 friend templates are handled correctly. */
32987 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
32989 /* Add FN to the queue of functions to be parsed later. */
32990 vec_safe_push (unparsed_funs_with_definitions, fn);
32992 return fn;
32995 /* Save the tokens that make up the in-class initializer for a non-static
32996 data member. Returns a DEFERRED_PARSE. */
32998 static tree
32999 cp_parser_save_nsdmi (cp_parser* parser)
33001 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
33004 /* Parse a template-argument-list, as well as the trailing ">" (but
33005 not the opening "<"). See cp_parser_template_argument_list for the
33006 return value. */
33008 static tree
33009 cp_parser_enclosed_template_argument_list (cp_parser* parser)
33011 tree arguments;
33012 tree saved_scope;
33013 tree saved_qualifying_scope;
33014 tree saved_object_scope;
33015 bool saved_greater_than_is_operator_p;
33017 /* [temp.names]
33019 When parsing a template-id, the first non-nested `>' is taken as
33020 the end of the template-argument-list rather than a greater-than
33021 operator. */
33022 saved_greater_than_is_operator_p
33023 = parser->greater_than_is_operator_p;
33024 parser->greater_than_is_operator_p = false;
33025 /* Parsing the argument list may modify SCOPE, so we save it
33026 here. */
33027 saved_scope = parser->scope;
33028 saved_qualifying_scope = parser->qualifying_scope;
33029 saved_object_scope = parser->object_scope;
33030 /* We need to evaluate the template arguments, even though this
33031 template-id may be nested within a "sizeof". */
33032 cp_evaluated ev;
33033 /* Parse the template-argument-list itself. */
33034 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
33035 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT)
33036 || cp_lexer_next_token_is (parser->lexer, CPP_GREATER_EQ)
33037 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT_EQ))
33039 arguments = make_tree_vec (0);
33040 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (arguments, 0);
33042 else
33043 arguments = cp_parser_template_argument_list (parser);
33044 /* Look for the `>' that ends the template-argument-list. If we find
33045 a '>>' instead, it's probably just a typo. */
33046 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
33048 if (cxx_dialect != cxx98)
33050 /* In C++0x, a `>>' in a template argument list or cast
33051 expression is considered to be two separate `>'
33052 tokens. So, change the current token to a `>', but don't
33053 consume it: it will be consumed later when the outer
33054 template argument list (or cast expression) is parsed.
33055 Note that this replacement of `>' for `>>' is necessary
33056 even if we are parsing tentatively: in the tentative
33057 case, after calling
33058 cp_parser_enclosed_template_argument_list we will always
33059 throw away all of the template arguments and the first
33060 closing `>', either because the template argument list
33061 was erroneous or because we are replacing those tokens
33062 with a CPP_TEMPLATE_ID token. The second `>' (which will
33063 not have been thrown away) is needed either to close an
33064 outer template argument list or to complete a new-style
33065 cast. */
33066 cp_token *token = cp_lexer_peek_token (parser->lexer);
33067 token->type = CPP_GREATER;
33069 else if (!saved_greater_than_is_operator_p)
33071 /* If we're in a nested template argument list, the '>>' has
33072 to be a typo for '> >'. We emit the error message, but we
33073 continue parsing and we push a '>' as next token, so that
33074 the argument list will be parsed correctly. Note that the
33075 global source location is still on the token before the
33076 '>>', so we need to say explicitly where we want it. */
33077 cp_token *token = cp_lexer_peek_token (parser->lexer);
33078 gcc_rich_location richloc (token->location);
33079 richloc.add_fixit_replace ("> >");
33080 error_at (&richloc, "%<>>%> should be %<> >%> "
33081 "within a nested template argument list");
33083 token->type = CPP_GREATER;
33085 else
33087 /* If this is not a nested template argument list, the '>>'
33088 is a typo for '>'. Emit an error message and continue.
33089 Same deal about the token location, but here we can get it
33090 right by consuming the '>>' before issuing the diagnostic. */
33091 cp_token *token = cp_lexer_consume_token (parser->lexer);
33092 error_at (token->location,
33093 "spurious %<>>%>, use %<>%> to terminate "
33094 "a template argument list");
33097 /* Similarly for >>= and >=. */
33098 else if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER_EQ)
33099 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT_EQ))
33101 cp_token *token = cp_lexer_consume_token (parser->lexer);
33102 gcc_rich_location richloc (token->location);
33103 enum cpp_ttype new_type;
33104 const char *replacement;
33105 if (token->type == CPP_GREATER_EQ)
33107 replacement = "> =";
33108 new_type = CPP_EQ;
33110 else if (!saved_greater_than_is_operator_p)
33112 if (cxx_dialect != cxx98)
33113 replacement = ">> =";
33114 else
33115 replacement = "> > =";
33116 new_type = CPP_GREATER;
33118 else
33120 replacement = "> >=";
33121 new_type = CPP_GREATER_EQ;
33123 richloc.add_fixit_replace (replacement);
33124 error_at (&richloc, "%qs should be %qs to terminate a template "
33125 "argument list",
33126 cpp_type2name (token->type, token->flags), replacement);
33127 token->type = new_type;
33129 else
33130 cp_parser_require_end_of_template_parameter_list (parser);
33131 /* The `>' token might be a greater-than operator again now. */
33132 parser->greater_than_is_operator_p
33133 = saved_greater_than_is_operator_p;
33134 /* Restore the SAVED_SCOPE. */
33135 parser->scope = saved_scope;
33136 parser->qualifying_scope = saved_qualifying_scope;
33137 parser->object_scope = saved_object_scope;
33139 return arguments;
33142 /* MEMBER_FUNCTION is a member function, or a friend. If default
33143 arguments, or the body of the function have not yet been parsed,
33144 parse them now. */
33146 static void
33147 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
33149 auto_timevar tv (TV_PARSE_INMETH);
33151 /* If this member is a template, get the underlying
33152 FUNCTION_DECL. */
33153 if (DECL_FUNCTION_TEMPLATE_P (member_function))
33154 member_function = DECL_TEMPLATE_RESULT (member_function);
33156 /* There should not be any class definitions in progress at this
33157 point; the bodies of members are only parsed outside of all class
33158 definitions. */
33159 gcc_assert (parser->num_classes_being_defined == 0);
33160 /* While we're parsing the member functions we might encounter more
33161 classes. We want to handle them right away, but we don't want
33162 them getting mixed up with functions that are currently in the
33163 queue. */
33164 push_unparsed_function_queues (parser);
33166 /* Make sure that any template parameters are in scope. */
33167 maybe_begin_member_template_processing (member_function);
33169 /* If the body of the function has not yet been parsed, parse it
33170 now. Except if the tokens have been purged (PR c++/39751). */
33171 if (DECL_PENDING_INLINE_P (member_function)
33172 && !DECL_PENDING_INLINE_INFO (member_function)->first->purged_p)
33174 tree function_scope;
33175 cp_token_cache *tokens;
33177 /* The function is no longer pending; we are processing it. */
33178 tokens = DECL_PENDING_INLINE_INFO (member_function);
33179 DECL_PENDING_INLINE_INFO (member_function) = NULL;
33180 DECL_PENDING_INLINE_P (member_function) = 0;
33182 /* If this is a local class, enter the scope of the containing
33183 function. */
33184 function_scope = current_function_decl;
33185 if (function_scope)
33186 push_function_context ();
33188 /* Push the body of the function onto the lexer stack. */
33189 cp_parser_push_lexer_for_tokens (parser, tokens);
33191 /* Let the front end know that we going to be defining this
33192 function. */
33193 start_preparsed_function (member_function, NULL_TREE,
33194 SF_PRE_PARSED | SF_INCLASS_INLINE);
33196 /* #pragma omp declare reduction needs special parsing. */
33197 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
33199 parser->lexer->in_pragma = true;
33200 cp_parser_omp_declare_reduction_exprs (member_function, parser);
33201 finish_function (/*inline_p=*/true);
33202 cp_check_omp_declare_reduction (member_function);
33204 else
33205 /* Now, parse the body of the function. */
33206 cp_parser_function_definition_after_declarator (parser,
33207 /*inline_p=*/true);
33209 /* Leave the scope of the containing function. */
33210 if (function_scope)
33211 pop_function_context ();
33212 cp_parser_pop_lexer (parser);
33215 /* Remove any template parameters from the symbol table. */
33216 maybe_end_member_template_processing ();
33218 /* Restore the queue. */
33219 pop_unparsed_function_queues (parser);
33222 /* If DECL contains any default args, remember it on the unparsed
33223 functions queue. */
33225 static void
33226 cp_parser_save_default_args (cp_parser* parser, tree decl)
33228 tree probe;
33230 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
33231 probe;
33232 probe = TREE_CHAIN (probe))
33233 if (TREE_PURPOSE (probe))
33235 cp_default_arg_entry entry = {current_class_type, decl};
33236 vec_safe_push (unparsed_funs_with_default_args, entry);
33237 break;
33240 /* Remember if there is a noexcept-specifier to post process. */
33241 tree spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl));
33242 if (UNPARSED_NOEXCEPT_SPEC_P (spec))
33243 vec_safe_push (unparsed_noexcepts, decl);
33245 /* Contracts are deferred. */
33246 for (tree attr = DECL_ATTRIBUTES (decl); attr; attr = TREE_CHAIN (attr))
33247 if (cxx_contract_attribute_p (attr))
33249 vec_safe_push (unparsed_contracts, decl);
33250 break;
33254 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
33255 which is either a FIELD_DECL or PARM_DECL. Parse it and return
33256 the result. For a PARM_DECL, PARMTYPE is the corresponding type
33257 from the parameter-type-list. */
33259 static tree
33260 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
33261 tree default_arg, tree parmtype)
33263 cp_token_cache *tokens;
33264 tree parsed_arg;
33266 if (default_arg == error_mark_node)
33267 return error_mark_node;
33269 /* Push the saved tokens for the default argument onto the parser's
33270 lexer stack. */
33271 tokens = DEFPARSE_TOKENS (default_arg);
33272 cp_parser_push_lexer_for_tokens (parser, tokens);
33274 start_lambda_scope (decl);
33276 /* Parse the default argument. */
33277 parsed_arg = cp_parser_initializer (parser);
33278 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
33279 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
33281 finish_lambda_scope ();
33283 if (parsed_arg == error_mark_node)
33284 cp_parser_skip_to_end_of_statement (parser);
33286 if (!processing_template_decl)
33288 /* In a non-template class, check conversions now. In a template,
33289 we'll wait and instantiate these as needed. */
33290 if (TREE_CODE (decl) == PARM_DECL)
33291 parsed_arg = check_default_argument (parmtype, parsed_arg,
33292 tf_warning_or_error);
33293 else if (maybe_reject_flexarray_init (decl, parsed_arg))
33294 parsed_arg = error_mark_node;
33295 else
33296 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
33299 /* If the token stream has not been completely used up, then
33300 there was extra junk after the end of the default
33301 argument. */
33302 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
33304 if (TREE_CODE (decl) == PARM_DECL)
33305 cp_parser_error (parser, "expected %<,%>");
33306 else
33307 cp_parser_error (parser, "expected %<;%>");
33310 /* Revert to the main lexer. */
33311 cp_parser_pop_lexer (parser);
33313 return parsed_arg;
33316 /* FIELD is a non-static data member with an initializer which we saved for
33317 later; parse it now. */
33319 static void
33320 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
33322 tree def;
33324 maybe_begin_member_template_processing (field);
33326 push_unparsed_function_queues (parser);
33327 def = cp_parser_late_parse_one_default_arg (parser, field,
33328 DECL_INITIAL (field),
33329 NULL_TREE);
33330 pop_unparsed_function_queues (parser);
33332 maybe_end_member_template_processing ();
33334 DECL_INITIAL (field) = def;
33337 /* FN is a FUNCTION_DECL which may contains a parameter with an
33338 unparsed DEFERRED_PARSE. Parse the default args now. This function
33339 assumes that the current scope is the scope in which the default
33340 argument should be processed. */
33342 static void
33343 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
33345 unsigned char saved_local_variables_forbidden_p;
33347 /* While we're parsing the default args, we might (due to the
33348 statement expression extension) encounter more classes. We want
33349 to handle them right away, but we don't want them getting mixed
33350 up with default args that are currently in the queue. */
33351 push_unparsed_function_queues (parser);
33353 /* Local variable names (and the `this' keyword) may not appear
33354 in a default argument. */
33355 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
33356 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
33358 push_defarg_context (fn);
33360 begin_scope (sk_function_parms, fn);
33362 /* Gather the PARM_DECLs into a vec so we can keep track of them when
33363 pushdecl clears DECL_CHAIN. */
33364 releasing_vec parms;
33365 for (tree parmdecl = DECL_ARGUMENTS (fn); parmdecl;
33366 parmdecl = DECL_CHAIN (parmdecl))
33367 vec_safe_push (parms, parmdecl);
33369 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
33370 for (int i = 0;
33371 parm && parm != void_list_node;
33372 parm = TREE_CHAIN (parm),
33373 ++i)
33375 tree default_arg = TREE_PURPOSE (parm);
33376 tree parsed_arg;
33378 tree parmdecl = parms[i];
33379 pushdecl (parmdecl);
33381 if (!default_arg)
33382 continue;
33384 if (TREE_CODE (default_arg) != DEFERRED_PARSE)
33385 /* This can happen for a friend declaration for a function
33386 already declared with default arguments. */
33387 continue;
33389 parsed_arg
33390 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
33391 default_arg,
33392 TREE_VALUE (parm));
33393 TREE_PURPOSE (parm) = parsed_arg;
33395 /* Update any instantiations we've already created. */
33396 for (tree copy : DEFPARSE_INSTANTIATIONS (default_arg))
33397 TREE_PURPOSE (copy) = parsed_arg;
33400 pop_bindings_and_leave_scope ();
33402 /* Restore DECL_CHAINs after clobbering by pushdecl. */
33403 parm = NULL_TREE;
33404 for (int i = parms->length () - 1; i >= 0; --i)
33406 DECL_CHAIN (parms[i]) = parm;
33407 parm = parms[i];
33410 pop_defarg_context ();
33412 /* Make sure no default arg is missing. */
33413 check_default_args (fn);
33415 /* Restore the state of local_variables_forbidden_p. */
33416 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
33418 /* Restore the queue. */
33419 pop_unparsed_function_queues (parser);
33422 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
33424 sizeof ... ( identifier )
33426 where the 'sizeof' token has already been consumed. */
33428 static tree
33429 cp_parser_sizeof_pack (cp_parser *parser)
33431 /* Consume the `...'. */
33432 cp_lexer_consume_token (parser->lexer);
33433 maybe_warn_variadic_templates ();
33435 matching_parens parens;
33436 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
33437 if (paren)
33438 parens.consume_open (parser);
33439 else
33440 permerror (cp_lexer_peek_token (parser->lexer)->location,
33441 "%<sizeof...%> argument must be surrounded by parentheses");
33443 cp_token *token = cp_lexer_peek_token (parser->lexer);
33444 tree name = cp_parser_identifier (parser);
33445 if (name == error_mark_node)
33446 return error_mark_node;
33447 /* The name is not qualified. */
33448 parser->scope = NULL_TREE;
33449 parser->qualifying_scope = NULL_TREE;
33450 parser->object_scope = NULL_TREE;
33451 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
33452 if (expr == error_mark_node)
33453 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
33454 token->location);
33455 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
33456 expr = TREE_TYPE (expr);
33457 else if (TREE_CODE (expr) == CONST_DECL)
33458 expr = DECL_INITIAL (expr);
33459 expr = make_pack_expansion (expr);
33460 if (expr != error_mark_node)
33461 PACK_EXPANSION_SIZEOF_P (expr) = true;
33463 if (paren)
33464 parens.require_close (parser);
33466 return expr;
33469 /* Parse the operand of `sizeof' (or a similar operator). Returns
33470 either a TYPE or an expression, depending on the form of the
33471 input. The KEYWORD indicates which kind of expression we have
33472 encountered. */
33474 static tree
33475 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
33477 tree expr = NULL_TREE;
33478 const char *saved_message;
33479 const char *saved_message_arg;
33480 bool saved_integral_constant_expression_p;
33481 bool saved_non_integral_constant_expression_p;
33483 /* If it's a `...', then we are computing the length of a parameter
33484 pack. */
33485 if (keyword == RID_SIZEOF
33486 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
33487 return cp_parser_sizeof_pack (parser);
33489 /* Types cannot be defined in a `sizeof' expression. Save away the
33490 old message. */
33491 saved_message = parser->type_definition_forbidden_message;
33492 saved_message_arg = parser->type_definition_forbidden_message_arg;
33493 parser->type_definition_forbidden_message
33494 = G_("types may not be defined in %qs expressions");
33495 parser->type_definition_forbidden_message_arg
33496 = IDENTIFIER_POINTER (ridpointers[keyword]);
33498 /* The restrictions on constant-expressions do not apply inside
33499 sizeof expressions. */
33500 saved_integral_constant_expression_p
33501 = parser->integral_constant_expression_p;
33502 saved_non_integral_constant_expression_p
33503 = parser->non_integral_constant_expression_p;
33504 parser->integral_constant_expression_p = false;
33506 auto cleanup = make_temp_override
33507 (parser->auto_is_implicit_function_template_parm_p, false);
33509 /* Do not actually evaluate the expression. */
33510 ++cp_unevaluated_operand;
33511 ++c_inhibit_evaluation_warnings;
33512 /* If it's a `(', then we might be looking at the type-id
33513 construction. */
33514 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33516 tree type = NULL_TREE;
33518 tentative_firewall firewall (parser);
33520 /* We can't be sure yet whether we're looking at a type-id or an
33521 expression. */
33522 cp_parser_parse_tentatively (parser);
33524 matching_parens parens;
33525 parens.consume_open (parser);
33527 /* Note: as a GNU Extension, compound literals are considered
33528 postfix-expressions as they are in C99, so they are valid
33529 arguments to sizeof. See comment in cp_parser_cast_expression
33530 for details. */
33531 if (cp_parser_compound_literal_p (parser))
33532 cp_parser_simulate_error (parser);
33533 else
33535 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
33536 parser->in_type_id_in_expr_p = true;
33537 /* Look for the type-id. */
33538 type = cp_parser_type_id (parser);
33539 /* Look for the closing `)'. */
33540 parens.require_close (parser);
33541 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
33544 /* If all went well, then we're done. */
33545 if (cp_parser_parse_definitely (parser))
33546 expr = type;
33547 else
33549 /* Commit to the tentative_firewall so we get syntax errors. */
33550 cp_parser_commit_to_tentative_parse (parser);
33552 expr = cp_parser_unary_expression (parser);
33555 else
33556 expr = cp_parser_unary_expression (parser);
33558 /* Go back to evaluating expressions. */
33559 --cp_unevaluated_operand;
33560 --c_inhibit_evaluation_warnings;
33562 /* And restore the old one. */
33563 parser->type_definition_forbidden_message = saved_message;
33564 parser->type_definition_forbidden_message_arg = saved_message_arg;
33565 parser->integral_constant_expression_p
33566 = saved_integral_constant_expression_p;
33567 parser->non_integral_constant_expression_p
33568 = saved_non_integral_constant_expression_p;
33570 return expr;
33573 /* If the current declaration has no declarator, return true. */
33575 static bool
33576 cp_parser_declares_only_class_p (cp_parser *parser)
33578 /* If the next token is a `;' or a `,' then there is no
33579 declarator. */
33580 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
33581 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
33584 /* Update the DECL_SPECS to reflect the storage class indicated by
33585 KEYWORD. */
33587 static void
33588 cp_parser_set_storage_class (cp_parser *parser,
33589 cp_decl_specifier_seq *decl_specs,
33590 enum rid keyword,
33591 cp_token *token)
33593 cp_storage_class storage_class;
33595 switch (keyword)
33597 case RID_AUTO:
33598 storage_class = sc_auto;
33599 break;
33600 case RID_REGISTER:
33601 storage_class = sc_register;
33602 break;
33603 case RID_STATIC:
33604 storage_class = sc_static;
33605 break;
33606 case RID_EXTERN:
33607 storage_class = sc_extern;
33608 break;
33609 case RID_MUTABLE:
33610 storage_class = sc_mutable;
33611 break;
33612 default:
33613 gcc_unreachable ();
33616 if (parser->in_unbraced_linkage_specification_p)
33618 error_at (token->location, "invalid use of %qD in linkage specification",
33619 ridpointers[keyword]);
33620 return;
33622 else if (decl_specs->storage_class != sc_none)
33624 if (decl_specs->conflicting_specifiers_p)
33625 return;
33626 gcc_rich_location richloc (token->location);
33627 richloc.add_location_if_nearby (decl_specs->locations[ds_storage_class]);
33628 if (decl_specs->storage_class == storage_class)
33629 error_at (&richloc, "duplicate %qD specifier", ridpointers[keyword]);
33630 else
33631 error_at (&richloc,
33632 "%qD specifier conflicts with %qs",
33633 ridpointers[keyword],
33634 cp_storage_class_name[decl_specs->storage_class]);
33635 decl_specs->conflicting_specifiers_p = true;
33636 return;
33639 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
33640 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
33641 && decl_specs->gnu_thread_keyword_p)
33643 pedwarn (decl_specs->locations[ds_thread], 0,
33644 "%<__thread%> before %qD", ridpointers[keyword]);
33647 decl_specs->storage_class = storage_class;
33648 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
33650 /* A storage class specifier cannot be applied alongside a typedef
33651 specifier. If there is a typedef specifier present then set
33652 conflicting_specifiers_p which will trigger an error later
33653 on in grokdeclarator. */
33654 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
33655 && !decl_specs->conflicting_specifiers_p)
33657 gcc_rich_location richloc (token->location);
33658 richloc.add_location_if_nearby (decl_specs->locations[ds_typedef]);
33659 error_at (&richloc,
33660 "%qD specifier conflicts with %<typedef%>",
33661 ridpointers[keyword]);
33662 decl_specs->conflicting_specifiers_p = true;
33666 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
33667 is true, the type is a class or enum definition. */
33669 static void
33670 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
33671 tree type_spec,
33672 cp_token *token,
33673 bool type_definition_p)
33675 decl_specs->any_specifiers_p = true;
33677 /* If the user tries to redeclare bool, char8_t, char16_t, char32_t, or
33678 wchar_t (with, for example, in "typedef int wchar_t;") we remember that
33679 this is what happened. In system headers, we ignore these
33680 declarations so that G++ can work with system headers that are not
33681 C++-safe. */
33682 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
33683 && !type_definition_p
33684 && TYPE_P (type_spec)
33685 && (type_spec == boolean_type_node
33686 || type_spec == char8_type_node
33687 || type_spec == char16_type_node
33688 || type_spec == char32_type_node
33689 || extended_float_type_p (type_spec)
33690 || type_spec == wchar_type_node)
33691 && (decl_specs->type
33692 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
33693 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
33694 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
33695 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
33697 decl_specs->redefined_builtin_type = type_spec;
33698 set_and_check_decl_spec_loc (decl_specs,
33699 ds_redefined_builtin_type_spec,
33700 token);
33701 if (!decl_specs->type)
33703 decl_specs->type = type_spec;
33704 decl_specs->type_definition_p = false;
33705 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
33708 else if (decl_specs->type)
33709 decl_specs->multiple_types_p = true;
33710 else
33712 decl_specs->type = type_spec;
33713 decl_specs->type_definition_p = type_definition_p;
33714 decl_specs->redefined_builtin_type = NULL_TREE;
33715 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
33719 /* True iff TOKEN is the GNU keyword __thread. */
33721 static bool
33722 token_is__thread (cp_token *token)
33724 gcc_assert (token->keyword == RID_THREAD);
33725 return id_equal (token->u.value, "__thread");
33728 /* Set the location for a declarator specifier and check if it is
33729 duplicated.
33731 DECL_SPECS is the sequence of declarator specifiers onto which to
33732 set the location.
33734 DS is the single declarator specifier to set which location is to
33735 be set onto the existing sequence of declarators.
33737 LOCATION is the location for the declarator specifier to
33738 consider. */
33740 static void
33741 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
33742 cp_decl_spec ds, cp_token *token)
33744 gcc_assert (ds < ds_last);
33746 if (decl_specs == NULL)
33747 return;
33749 location_t location = token->location;
33751 if (decl_specs->locations[ds] == 0)
33753 decl_specs->locations[ds] = location;
33754 if (ds == ds_thread)
33755 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
33757 else
33759 if (ds == ds_long)
33761 if (decl_specs->locations[ds_long_long] != 0)
33762 error_at (location,
33763 "%<long long long%> is too long for GCC");
33764 else
33766 decl_specs->locations[ds_long_long] = location;
33767 pedwarn_cxx98 (location,
33768 OPT_Wlong_long,
33769 "ISO C++ 1998 does not support %<long long%>");
33772 else if (ds == ds_thread)
33774 bool gnu = token_is__thread (token);
33775 gcc_rich_location richloc (location);
33776 if (gnu != decl_specs->gnu_thread_keyword_p)
33778 richloc.add_range (decl_specs->locations[ds_thread]);
33779 error_at (&richloc,
33780 "both %<__thread%> and %<thread_local%> specified");
33782 else
33784 richloc.add_fixit_remove ();
33785 error_at (&richloc, "duplicate %qD", token->u.value);
33788 else
33790 static const char *const decl_spec_names[] = {
33791 "signed",
33792 "unsigned",
33793 "short",
33794 "long",
33795 "const",
33796 "volatile",
33797 "restrict",
33798 "inline",
33799 "virtual",
33800 "explicit",
33801 "friend",
33802 "typedef",
33803 "using",
33804 "constexpr",
33805 "__complex",
33806 "constinit",
33807 "consteval"
33809 gcc_rich_location richloc (location);
33810 richloc.add_fixit_remove ();
33811 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
33816 /* Return true iff the declarator specifier DS is present in the
33817 sequence of declarator specifiers DECL_SPECS. */
33819 bool
33820 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
33821 cp_decl_spec ds)
33823 gcc_assert (ds < ds_last);
33825 if (decl_specs == NULL)
33826 return false;
33828 return decl_specs->locations[ds] != 0;
33831 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
33832 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
33834 static bool
33835 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
33837 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
33840 /* Issue an error message indicating that TOKEN_DESC was expected.
33841 If KEYWORD is true, it indicated this function is called by
33842 cp_parser_require_keword and the required token can only be
33843 a indicated keyword.
33845 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
33846 within any error as the location of an "opening" token matching
33847 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
33848 RT_CLOSE_PAREN). */
33850 static void
33851 cp_parser_required_error (cp_parser *parser,
33852 required_token token_desc,
33853 bool keyword,
33854 location_t matching_location)
33856 if (cp_parser_simulate_error (parser))
33857 return;
33859 const char *gmsgid = NULL;
33860 switch (token_desc)
33862 case RT_NEW:
33863 gmsgid = G_("expected %<new%>");
33864 break;
33865 case RT_DELETE:
33866 gmsgid = G_("expected %<delete%>");
33867 break;
33868 case RT_RETURN:
33869 gmsgid = G_("expected %<return%>");
33870 break;
33871 case RT_WHILE:
33872 gmsgid = G_("expected %<while%>");
33873 break;
33874 case RT_EXTERN:
33875 gmsgid = G_("expected %<extern%>");
33876 break;
33877 case RT_STATIC_ASSERT:
33878 gmsgid = G_("expected %<static_assert%>");
33879 break;
33880 case RT_DECLTYPE:
33881 gmsgid = G_("expected %<decltype%>");
33882 break;
33883 case RT_OPERATOR:
33884 gmsgid = G_("expected %<operator%>");
33885 break;
33886 case RT_CLASS:
33887 gmsgid = G_("expected %<class%>");
33888 break;
33889 case RT_TEMPLATE:
33890 gmsgid = G_("expected %<template%>");
33891 break;
33892 case RT_NAMESPACE:
33893 gmsgid = G_("expected %<namespace%>");
33894 break;
33895 case RT_USING:
33896 gmsgid = G_("expected %<using%>");
33897 break;
33898 case RT_ASM:
33899 gmsgid = G_("expected %<asm%>");
33900 break;
33901 case RT_TRY:
33902 gmsgid = G_("expected %<try%>");
33903 break;
33904 case RT_CATCH:
33905 gmsgid = G_("expected %<catch%>");
33906 break;
33907 case RT_THROW:
33908 gmsgid = G_("expected %<throw%>");
33909 break;
33910 case RT_AUTO:
33911 gmsgid = G_("expected %<auto%>");
33912 break;
33913 case RT_LABEL:
33914 gmsgid = G_("expected %<__label__%>");
33915 break;
33916 case RT_AT_TRY:
33917 gmsgid = G_("expected %<@try%>");
33918 break;
33919 case RT_AT_SYNCHRONIZED:
33920 gmsgid = G_("expected %<@synchronized%>");
33921 break;
33922 case RT_AT_THROW:
33923 gmsgid = G_("expected %<@throw%>");
33924 break;
33925 case RT_TRANSACTION_ATOMIC:
33926 gmsgid = G_("expected %<__transaction_atomic%>");
33927 break;
33928 case RT_TRANSACTION_RELAXED:
33929 gmsgid = G_("expected %<__transaction_relaxed%>");
33930 break;
33931 case RT_CO_YIELD:
33932 gmsgid = G_("expected %<co_yield%>");
33933 break;
33934 default:
33935 break;
33938 if (!gmsgid && !keyword)
33940 switch (token_desc)
33942 case RT_SEMICOLON:
33943 gmsgid = G_("expected %<;%>");
33944 break;
33945 case RT_OPEN_PAREN:
33946 gmsgid = G_("expected %<(%>");
33947 break;
33948 case RT_CLOSE_BRACE:
33949 gmsgid = G_("expected %<}%>");
33950 break;
33951 case RT_OPEN_BRACE:
33952 gmsgid = G_("expected %<{%>");
33953 break;
33954 case RT_CLOSE_SQUARE:
33955 gmsgid = G_("expected %<]%>");
33956 break;
33957 case RT_OPEN_SQUARE:
33958 gmsgid = G_("expected %<[%>");
33959 break;
33960 case RT_COMMA:
33961 gmsgid = G_("expected %<,%>");
33962 break;
33963 case RT_SCOPE:
33964 gmsgid = G_("expected %<::%>");
33965 break;
33966 case RT_LESS:
33967 gmsgid = G_("expected %<<%>");
33968 break;
33969 case RT_GREATER:
33970 gmsgid = G_("expected %<>%>");
33971 break;
33972 case RT_EQ:
33973 gmsgid = G_("expected %<=%>");
33974 break;
33975 case RT_ELLIPSIS:
33976 gmsgid = G_("expected %<...%>");
33977 break;
33978 case RT_MULT:
33979 gmsgid = G_("expected %<*%>");
33980 break;
33981 case RT_COMPL:
33982 gmsgid = G_("expected %<~%>");
33983 break;
33984 case RT_COLON:
33985 gmsgid = G_("expected %<:%>");
33986 break;
33987 case RT_COLON_SCOPE:
33988 gmsgid = G_("expected %<:%> or %<::%>");
33989 break;
33990 case RT_CLOSE_PAREN:
33991 gmsgid = G_("expected %<)%>");
33992 break;
33993 case RT_COMMA_CLOSE_PAREN:
33994 gmsgid = G_("expected %<,%> or %<)%>");
33995 break;
33996 case RT_PRAGMA_EOL:
33997 gmsgid = G_("expected end of line");
33998 break;
33999 case RT_NAME:
34000 gmsgid = G_("expected identifier");
34001 break;
34002 case RT_SELECT:
34003 gmsgid = G_("expected selection-statement");
34004 break;
34005 case RT_ITERATION:
34006 gmsgid = G_("expected iteration-statement");
34007 break;
34008 case RT_JUMP:
34009 gmsgid = G_("expected jump-statement");
34010 break;
34011 case RT_CLASS_KEY:
34012 gmsgid = G_("expected class-key");
34013 break;
34014 case RT_CLASS_TYPENAME_TEMPLATE:
34015 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
34016 break;
34017 default:
34018 gcc_unreachable ();
34022 if (gmsgid)
34023 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
34027 /* If the next token is of the indicated TYPE, consume it. Otherwise,
34028 issue an error message indicating that TOKEN_DESC was expected.
34030 Returns the token consumed, if the token had the appropriate type.
34031 Otherwise, returns NULL.
34033 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
34034 within any error as the location of an "opening" token matching
34035 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
34036 RT_CLOSE_PAREN). */
34038 static cp_token *
34039 cp_parser_require (cp_parser* parser,
34040 enum cpp_ttype type,
34041 required_token token_desc,
34042 location_t matching_location)
34044 if (cp_lexer_next_token_is (parser->lexer, type))
34045 return cp_lexer_consume_token (parser->lexer);
34046 else
34048 /* Output the MESSAGE -- unless we're parsing tentatively. */
34049 if (!cp_parser_simulate_error (parser))
34050 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
34051 matching_location);
34052 return NULL;
34056 /* Skip an entire parameter list from start to finish. The next token must
34057 be the initial "<" of the parameter list. Returns true on success and
34058 false otherwise. */
34060 static bool
34061 cp_parser_skip_entire_template_parameter_list (cp_parser* parser)
34063 /* Consume the "<" because cp_parser_skip_to_end_of_template_parameter_list
34064 requires it. */
34065 cp_lexer_consume_token (parser->lexer);
34066 return cp_parser_skip_to_end_of_template_parameter_list (parser);
34069 /* Ensure we are at the end of a template parameter list. If we are, return.
34070 If we are not, something has gone wrong, in which case issue an error and
34071 skip to end of the parameter list. */
34073 static void
34074 cp_parser_require_end_of_template_parameter_list (cp_parser* parser)
34076 /* Are we ready, yet? If not, issue error message. */
34077 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
34078 return;
34080 cp_parser_skip_to_end_of_template_parameter_list (parser);
34083 /* You should only call this function from inside a template parameter list
34084 (i.e. the current token should at least be the initial "<" of the
34085 parameter list). If you are skipping the entire list, it may be better to
34086 use cp_parser_skip_entire_template_parameter_list.
34088 Tokens are skipped until the final ">" is found, or if we see
34089 '{', '}', ';', or if we find an unbalanced ')' or ']'.
34091 Returns true if we successfully reached the end, and false if
34092 something unexpected happened (e.g. end of file). */
34094 static bool
34095 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
34097 /* Current level of '< ... >'. */
34098 unsigned level = 0;
34099 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
34100 unsigned nesting_depth = 0;
34102 /* Skip tokens until the desired token is found. */
34103 while (true)
34105 /* Peek at the next token. */
34106 switch (cp_lexer_peek_token (parser->lexer)->type)
34108 case CPP_LESS:
34109 if (!nesting_depth)
34110 ++level;
34111 break;
34113 case CPP_RSHIFT:
34114 if (cxx_dialect == cxx98)
34115 /* C++0x views the `>>' operator as two `>' tokens, but
34116 C++98 does not. */
34117 break;
34118 else if (!nesting_depth && level-- == 0)
34120 /* We've hit a `>>' where the first `>' closes the
34121 template argument list, and the second `>' is
34122 spurious. Just consume the `>>' and stop; we've
34123 already produced at least one error. */
34124 cp_lexer_consume_token (parser->lexer);
34125 return false;
34127 /* Fall through for C++0x, so we handle the second `>' in
34128 the `>>'. */
34129 gcc_fallthrough ();
34131 case CPP_GREATER:
34132 if (!nesting_depth && level-- == 0)
34134 /* We've reached the token we want, consume it and stop. */
34135 cp_lexer_consume_token (parser->lexer);
34136 return true;
34138 break;
34140 case CPP_OPEN_PAREN:
34141 case CPP_OPEN_SQUARE:
34142 ++nesting_depth;
34143 break;
34145 case CPP_CLOSE_PAREN:
34146 case CPP_CLOSE_SQUARE:
34147 if (nesting_depth-- == 0)
34148 return false;
34149 break;
34151 case CPP_EOF:
34152 case CPP_PRAGMA_EOL:
34153 case CPP_SEMICOLON:
34154 case CPP_OPEN_BRACE:
34155 case CPP_CLOSE_BRACE:
34156 /* The '>' was probably forgotten, don't look further. */
34157 return false;
34159 default:
34160 break;
34163 /* Consume this token. */
34164 cp_lexer_consume_token (parser->lexer);
34168 /* If the next token is the indicated keyword, consume it. Otherwise,
34169 issue an error message indicating that TOKEN_DESC was expected.
34171 Returns the token consumed, if the token had the appropriate type.
34172 Otherwise, returns NULL. */
34174 static cp_token *
34175 cp_parser_require_keyword (cp_parser* parser,
34176 enum rid keyword,
34177 required_token token_desc)
34179 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
34181 if (token && token->keyword != keyword)
34183 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
34184 UNKNOWN_LOCATION);
34185 return NULL;
34188 return token;
34191 /* Returns TRUE iff TOKEN is a token that can begin the body of a
34192 function-definition. */
34194 static bool
34195 cp_parser_token_starts_function_definition_p (cp_token* token)
34197 return (/* An ordinary function-body begins with an `{'. */
34198 token->type == CPP_OPEN_BRACE
34199 /* A ctor-initializer begins with a `:'. */
34200 || token->type == CPP_COLON
34201 /* A function-try-block begins with `try'. */
34202 || token->keyword == RID_TRY
34203 /* A function-transaction-block begins with `__transaction_atomic'
34204 or `__transaction_relaxed'. */
34205 || token->keyword == RID_TRANSACTION_ATOMIC
34206 || token->keyword == RID_TRANSACTION_RELAXED
34207 /* The named return value extension begins with `return'. */
34208 || token->keyword == RID_RETURN);
34211 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
34212 definition. */
34214 static bool
34215 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
34217 cp_token *token;
34219 token = cp_lexer_peek_token (parser->lexer);
34220 return (token->type == CPP_OPEN_BRACE
34221 || (token->type == CPP_COLON
34222 && !parser->colon_doesnt_start_class_def_p));
34225 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
34226 C++0x) ending a template-argument. */
34228 static bool
34229 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
34231 cp_token *token;
34233 token = cp_lexer_peek_token (parser->lexer);
34234 return (token->type == CPP_COMMA
34235 || token->type == CPP_GREATER
34236 || token->type == CPP_ELLIPSIS
34237 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)
34238 /* For better diagnostics, treat >>= like that too, that
34239 shouldn't appear non-nested in template arguments. */
34240 || token->type == CPP_RSHIFT_EQ);
34243 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
34244 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
34246 static bool
34247 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
34248 size_t n)
34250 cp_token *token;
34252 token = cp_lexer_peek_nth_token (parser->lexer, n);
34253 if (token->type == CPP_LESS)
34254 return true;
34255 /* Check for the sequence `<::' in the original code. It would be lexed as
34256 `[:', where `[' is a digraph, and there is no whitespace before
34257 `:'. */
34258 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
34260 cp_token *token2;
34261 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
34262 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
34263 return true;
34265 return false;
34268 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
34269 or none_type otherwise. */
34271 static enum tag_types
34272 cp_parser_token_is_class_key (cp_token* token)
34274 switch (token->keyword)
34276 case RID_CLASS:
34277 return class_type;
34278 case RID_STRUCT:
34279 return record_type;
34280 case RID_UNION:
34281 return union_type;
34283 default:
34284 return none_type;
34288 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
34289 or none_type otherwise or if the token is null. */
34291 static enum tag_types
34292 cp_parser_token_is_type_parameter_key (cp_token* token)
34294 if (!token)
34295 return none_type;
34297 switch (token->keyword)
34299 case RID_CLASS:
34300 return class_type;
34301 case RID_TYPENAME:
34302 return typename_type;
34304 default:
34305 return none_type;
34309 /* Diagnose redundant enum-keys. */
34311 static void
34312 cp_parser_maybe_warn_enum_key (cp_parser *parser, location_t key_loc,
34313 tree type, rid scoped_key)
34315 if (!warn_redundant_tags)
34316 return;
34318 tree type_decl = TYPE_MAIN_DECL (type);
34319 tree name = DECL_NAME (type_decl);
34320 /* Look up the NAME to see if it unambiguously refers to the TYPE. */
34321 push_deferring_access_checks (dk_no_check);
34322 tree decl = cp_parser_lookup_name_simple (parser, name, input_location);
34323 pop_deferring_access_checks ();
34325 /* The enum-key is redundant for uses of the TYPE that are not
34326 declarations and for which name lookup returns just the type
34327 itself. */
34328 if (decl != type_decl)
34329 return;
34331 if (scoped_key != RID_CLASS
34332 && scoped_key != RID_STRUCT
34333 && current_lang_name != lang_name_cplusplus
34334 && current_namespace == global_namespace)
34336 /* Avoid issuing the diagnostic for apparently redundant (unscoped)
34337 enum tag in shared C/C++ code in files (such as headers) included
34338 in the main source file. */
34339 const line_map_ordinary *map = NULL;
34340 linemap_resolve_location (line_table, key_loc,
34341 LRK_MACRO_DEFINITION_LOCATION,
34342 &map);
34343 if (!MAIN_FILE_P (map))
34344 return;
34347 gcc_rich_location richloc (key_loc);
34348 richloc.add_fixit_remove (key_loc);
34349 warning_at (&richloc, OPT_Wredundant_tags,
34350 "redundant enum-key %<enum%s%> in reference to %q#T",
34351 (scoped_key == RID_CLASS ? " class"
34352 : scoped_key == RID_STRUCT ? " struct" : ""), type);
34355 /* Describes the set of declarations of a struct, class, or class template
34356 or its specializations. Used for -Wmismatched-tags. */
34358 class class_decl_loc_t
34360 public:
34362 class_decl_loc_t ()
34363 : locvec (), idxdef (), def_class_key ()
34365 locvec.create (4);
34368 /* Constructs an object for a single declaration of a class with
34369 CLASS_KEY at the current location in the current function (or
34370 at another scope). KEY_REDUNDANT is true if the class-key may
34371 be omitted in the current context without an ambiguity with
34372 another symbol with the same name.
34373 DEF_P is true for a class declaration that is a definition.
34374 CURLOC is the associated location. */
34375 class_decl_loc_t (tag_types class_key, bool key_redundant, bool def_p,
34376 location_t curloc = input_location)
34377 : locvec (), idxdef (def_p ? 0 : UINT_MAX), def_class_key (class_key)
34379 locvec.create (4);
34380 class_key_loc_t ckl (current_function_decl, curloc, class_key,
34381 key_redundant);
34382 locvec.quick_push (ckl);
34385 /* Copy, assign, and destroy the object. Necessary because LOCVEC
34386 isn't safely copyable and assignable and doesn't release storage
34387 on its own. */
34388 class_decl_loc_t (const class_decl_loc_t &rhs)
34389 : locvec (rhs.locvec.copy ()), idxdef (rhs.idxdef),
34390 def_class_key (rhs.def_class_key)
34393 class_decl_loc_t& operator= (const class_decl_loc_t &rhs)
34395 if (this == &rhs)
34396 return *this;
34397 locvec.release ();
34398 locvec = rhs.locvec.copy ();
34399 idxdef = rhs.idxdef;
34400 def_class_key = rhs.def_class_key;
34401 return *this;
34404 ~class_decl_loc_t ()
34406 locvec.release ();
34409 /* Issues -Wmismatched-tags for a single class. */
34410 void diag_mismatched_tags (tree);
34412 /* Issues -Wmismatched-tags for all classes. */
34413 static void diag_mismatched_tags ();
34415 /* Adds TYPE_DECL to the collection of class decls and diagnoses
34416 redundant tags (if -Wredundant-tags is enabled). */
34417 static void add (cp_parser *, location_t, tag_types, tree, bool, bool);
34419 /* Either adds this decl to the collection of class decls
34420 or diagnoses it, whichever is appropriate. */
34421 void add_or_diag_mismatched_tag (tree, tag_types, bool, bool);
34423 private:
34425 tree function (unsigned i) const
34427 return locvec[i].func;
34430 location_t location (unsigned i) const
34432 return locvec[i].loc;
34435 bool key_redundant (unsigned i) const
34437 return locvec[i].key_redundant;
34440 tag_types class_key (unsigned i) const
34442 return locvec[i].class_key;
34445 /* True if a definition for the class has been seen. */
34446 bool def_p () const
34448 return idxdef < locvec.length ();
34451 /* The location of a single mention of a class type with the given
34452 class-key. */
34453 struct class_key_loc_t
34455 class_key_loc_t (tree func, location_t loc, tag_types key, bool redundant)
34456 : func (func), loc (loc), class_key (key), key_redundant (redundant)
34459 /* The function the type is mentioned in. */
34460 tree func;
34461 /* The exact location. */
34462 location_t loc;
34463 /* The class-key used in the mention of the type. */
34464 tag_types class_key;
34465 /* True when the class-key could be omitted at this location
34466 without an ambiguity with another symbol of the same name. */
34467 bool key_redundant;
34469 /* Avoid using auto_vec here since it's not safe to copy due to pr90904. */
34470 vec <class_key_loc_t> locvec;
34471 /* LOCVEC index of the definition or UINT_MAX if none exists. */
34472 unsigned idxdef;
34473 /* The class-key the class was last declared with or none_type when
34474 it has been declared with a mismatched key. */
34475 tag_types def_class_key;
34477 /* A mapping between a TYPE_DECL for a class and the class_decl_loc_t
34478 description above. */
34479 typedef hash_map<tree_decl_hash, class_decl_loc_t> class_to_loc_map_t;
34480 static class_to_loc_map_t class2loc;
34483 class_decl_loc_t::class_to_loc_map_t class_decl_loc_t::class2loc;
34485 /* Issue an error message if the CLASS_KEY does not match the TYPE.
34486 DEF_P is expected to be set for a definition of class TYPE. DECL_P
34487 is set for a declaration of class TYPE and clear for a reference to
34488 it that is not a declaration of it. */
34490 static void
34491 cp_parser_check_class_key (cp_parser *parser, location_t key_loc,
34492 tag_types class_key, tree type, bool def_p,
34493 bool decl_p)
34495 if (type == error_mark_node)
34496 return;
34498 bool seen_as_union = TREE_CODE (type) == UNION_TYPE;
34499 if (seen_as_union != (class_key == union_type))
34501 if (permerror (input_location, "%qs tag used in naming %q#T",
34502 class_key == union_type ? "union"
34503 : class_key == record_type ? "struct" : "class",
34504 type))
34505 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
34506 "%q#T was previously declared here", type);
34507 return;
34510 if (!warn_mismatched_tags && !warn_redundant_tags)
34511 return;
34513 /* Only consider the true class-keys below and ignore typename_type,
34514 etc. that are not C++ class-keys. */
34515 if (class_key != class_type
34516 && class_key != record_type
34517 && class_key != union_type)
34518 return;
34520 class_decl_loc_t::add (parser, key_loc, class_key, type, def_p, decl_p);
34523 /* Returns the template or specialization of one to which the RECORD_TYPE
34524 TYPE corresponds. */
34526 static tree
34527 specialization_of (tree type)
34529 tree ret = type;
34531 /* Determine the template or its partial specialization to which TYPE
34532 corresponds. */
34533 if (tree ti = most_specialized_partial_spec (type, tf_none))
34534 if (ti != error_mark_node)
34535 ret = TREE_TYPE (TI_TEMPLATE (ti));
34537 if (ret == type)
34538 ret = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (type);
34540 return TYPE_MAIN_DECL (ret);
34544 /* Adds the class TYPE to the collection of class decls and diagnoses
34545 redundant tags (if -Wredundant-tags is enabled).
34546 DEF_P is expected to be set for a definition of class TYPE. DECL_P
34547 is set for a (likely, based on syntactic context) declaration of class
34548 TYPE and clear for a reference to it that is not a declaration of it. */
34550 void
34551 class_decl_loc_t::add (cp_parser *parser, location_t key_loc,
34552 tag_types class_key, tree type, bool def_p, bool decl_p)
34554 tree type_decl = TYPE_MAIN_DECL (type);
34555 tree name = DECL_NAME (type_decl);
34556 /* Look up the NAME to see if it unambiguously refers to the TYPE
34557 and set KEY_REDUNDANT if so. */
34558 push_deferring_access_checks (dk_no_check);
34559 tree decl = cp_parser_lookup_name_simple (parser, name, input_location);
34560 pop_deferring_access_checks ();
34562 /* The class-key is redundant for uses of the CLASS_TYPE that are
34563 neither definitions of it nor declarations, and for which name
34564 lookup returns just the type itself. */
34565 bool key_redundant = (!def_p && !decl_p
34566 && (decl == type_decl
34567 || TREE_CODE (decl) == TEMPLATE_DECL
34568 || (CLASS_TYPE_P (type)
34569 && TYPE_BEING_DEFINED (type))));
34571 if (key_redundant
34572 && class_key != class_type
34573 && current_lang_name != lang_name_cplusplus
34574 && current_namespace == global_namespace)
34576 /* Avoid issuing the diagnostic for apparently redundant struct
34577 and union class-keys in shared C/C++ code in files (such as
34578 headers) included in the main source file. */
34579 const line_map_ordinary *map = NULL;
34580 linemap_resolve_location (line_table, key_loc,
34581 LRK_MACRO_DEFINITION_LOCATION,
34582 &map);
34583 if (!MAIN_FILE_P (map))
34584 key_redundant = false;
34587 /* Set if a declaration of TYPE has previously been seen or if it must
34588 exist in a precompiled header. */
34589 bool exist;
34590 class_decl_loc_t *rdl = &class2loc.get_or_insert (type_decl, &exist);
34591 if (!exist)
34593 tree type = TREE_TYPE (type_decl);
34594 if (def_p || !COMPLETE_TYPE_P (type))
34596 /* TYPE_DECL is the first declaration or definition of the type
34597 (outside precompiled headers -- see below). Just create
34598 a new entry for it and return unless it's a declaration
34599 involving a template that may need to be diagnosed by
34600 -Wredundant-tags. */
34601 *rdl = class_decl_loc_t (class_key, false, def_p);
34602 if (TREE_CODE (decl) != TEMPLATE_DECL)
34603 return;
34605 else
34607 /* TYPE was previously defined in some unknown precompiled header.
34608 Simply add a record of its definition at an unknown location and
34609 proceed below to add a reference to it at the current location.
34610 (Declarations in precompiled headers that are not definitions
34611 are ignored.) */
34612 tag_types def_key
34613 = CLASSTYPE_DECLARED_CLASS (type) ? class_type : record_type;
34614 location_t def_loc = DECL_SOURCE_LOCATION (type_decl);
34615 *rdl = class_decl_loc_t (def_key, false, true, def_loc);
34616 exist = true;
34620 /* A prior declaration of TYPE_DECL has been seen. */
34622 if (key_redundant)
34624 gcc_rich_location richloc (key_loc);
34625 richloc.add_fixit_remove (key_loc);
34626 warning_at (&richloc, OPT_Wredundant_tags,
34627 "redundant class-key %qs in reference to %q#T",
34628 class_key == union_type ? "union"
34629 : class_key == record_type ? "struct" : "class",
34630 type);
34633 if (!exist)
34634 /* Do nothing if this is the first declaration of the type. */
34635 return;
34637 if (rdl->idxdef != UINT_MAX && rdl->def_class_key == class_key)
34638 /* Do nothing if the class-key in this declaration matches
34639 the definition. */
34640 return;
34642 rdl->add_or_diag_mismatched_tag (type_decl, class_key, key_redundant,
34643 def_p);
34646 /* Either adds this DECL corresponding to the TYPE_DECL to the collection
34647 of class decls or diagnoses it, whichever is appropriate. */
34649 void
34650 class_decl_loc_t::add_or_diag_mismatched_tag (tree type_decl,
34651 tag_types class_key,
34652 bool redundant,
34653 bool def_p)
34655 /* Reset the CLASS_KEY associated with this type on mismatch.
34656 This is an optimization that lets the diagnostic code skip
34657 over classes that use the same class-key in all declarations. */
34658 if (def_class_key != class_key)
34659 def_class_key = none_type;
34661 /* Set IDXDEF to the index of the vector corresponding to
34662 the definition. */
34663 if (def_p)
34664 idxdef = locvec.length ();
34666 /* Append a record of this declaration to the vector. */
34667 class_key_loc_t ckl (current_function_decl, input_location, class_key,
34668 redundant);
34669 locvec.safe_push (ckl);
34671 if (idxdef == UINT_MAX)
34672 return;
34674 /* As a space optimization diagnose declarations of a class
34675 whose definition has been seen and purge the LOCVEC of
34676 all entries except the definition. */
34677 diag_mismatched_tags (type_decl);
34678 if (idxdef)
34680 class_decl_loc_t::class_key_loc_t ent = locvec[idxdef];
34681 locvec.release ();
34682 locvec.reserve (2);
34683 locvec.safe_push (ent);
34684 idxdef = 0;
34686 else
34687 /* Pop the entry pushed above for this declaration. */
34688 locvec.pop ();
34691 /* Issues -Wmismatched-tags for a single class. */
34693 void
34694 class_decl_loc_t::diag_mismatched_tags (tree type_decl)
34696 if (!warn_mismatched_tags)
34697 return;
34699 /* Number of uses of the class. */
34700 const unsigned ndecls = locvec.length ();
34702 /* The class (or template) declaration guiding the decisions about
34703 the diagnostic. For ordinary classes it's the same as THIS. For
34704 uses of instantiations of templates other than their declarations
34705 it points to the record for the declaration of the corresponding
34706 primary template or partial specialization. */
34707 class_decl_loc_t *cdlguide = this;
34709 tree type = TREE_TYPE (type_decl);
34710 if (CLASS_TYPE_P (type) && CLASSTYPE_IMPLICIT_INSTANTIATION (type))
34712 /* For implicit instantiations of a primary template look up
34713 the primary or partial specialization and use it as
34714 the expected class-key rather than using the class-key of
34715 the first reference to the instantiation. The primary must
34716 be (and inevitably is) at index zero. */
34717 tree spec = specialization_of (type);
34718 cdlguide = class2loc.get (spec);
34719 /* It's possible that we didn't find SPEC. Consider:
34721 template<typename T> struct A {
34722 template<typename U> struct W { };
34724 struct A<int>::W<int> w; // #1
34726 where while parsing A and #1 we've stashed
34727 A<T>
34728 A<T>::W<U>
34729 A<int>::W<int>
34730 into CLASS2LOC. If TYPE is A<int>::W<int>, specialization_of
34731 will yield A<int>::W<U> which may be in CLASS2LOC if we had
34732 an A<int> class specialization, but otherwise won't be in it.
34733 So try to look up A<T>::W<U>. */
34734 if (!cdlguide)
34736 spec = DECL_TEMPLATE_RESULT (most_general_template (spec));
34737 cdlguide = class2loc.get (spec);
34739 /* Now we really should have found something. */
34740 gcc_assert (cdlguide != NULL);
34742 /* Skip declarations that consistently use the same class-key. */
34743 else if (def_class_key != none_type)
34744 return;
34746 /* Set if a definition for the class has been seen. */
34747 const bool def_p = cdlguide->def_p ();
34749 /* The index of the declaration whose class-key this declaration
34750 is expected to match. It's either the class-key of the class
34751 definition if one exists or the first declaration otherwise. */
34752 const unsigned idxguide = def_p ? cdlguide->idxdef : 0;
34754 /* The class-key the class is expected to be declared with: it's
34755 either the key used in its definition or the first declaration
34756 if no definition has been provided.
34757 For implicit instantiations of a primary template it's
34758 the class-key used to declare the primary with. The primary
34759 must be at index zero. */
34760 const tag_types xpect_key = cdlguide->class_key (idxguide);
34762 unsigned idx = 0;
34763 /* Advance IDX to the first declaration that either is not
34764 a definition or that doesn't match the first declaration
34765 if no definition is provided. */
34766 while (class_key (idx) == xpect_key)
34767 if (++idx == ndecls)
34768 return;
34770 /* Save the current function before changing it below. */
34771 tree save_func = current_function_decl;
34772 /* Set the function declaration to print in diagnostic context. */
34773 current_function_decl = function (idx);
34775 const char *xmatchkstr = xpect_key == record_type ? "class" : "struct";
34776 const char *xpectkstr = xpect_key == record_type ? "struct" : "class";
34778 location_t loc = location (idx);
34779 bool key_redundant_p = key_redundant (idx);
34780 auto_diagnostic_group d;
34781 /* Issue a warning for the first mismatched declaration.
34782 Avoid using "%#qT" since the class-key for the same type will
34783 be the same regardless of which one was used in the declaraion. */
34784 if (warning_at (loc, OPT_Wmismatched_tags,
34785 "%qT declared with a mismatched class-key %qs",
34786 type_decl, xmatchkstr))
34788 /* Suggest how to avoid the warning for each instance since
34789 the guidance may be different depending on context. */
34790 inform (loc,
34791 (key_redundant_p
34792 ? G_("remove the class-key or replace it with %qs")
34793 : G_("replace the class-key with %qs")),
34794 xpectkstr);
34796 /* Also point to the first declaration or definition that guided
34797 the decision to issue the warning above. */
34798 inform (cdlguide->location (idxguide),
34799 (def_p
34800 ? G_("%qT defined as %qs here")
34801 : G_("%qT first declared as %qs here")),
34802 type_decl, xpectkstr);
34805 /* Issue warnings for the remaining inconsistent declarations. */
34806 for (unsigned i = idx + 1; i != ndecls; ++i)
34808 tag_types clskey = class_key (i);
34809 /* Skip over the declarations that match either the definition
34810 if one was provided or the first declaration. */
34811 if (clskey == xpect_key)
34812 continue;
34814 loc = location (i);
34815 key_redundant_p = key_redundant (i);
34816 /* Set the function declaration to print in diagnostic context. */
34817 current_function_decl = function (i);
34818 if (warning_at (loc, OPT_Wmismatched_tags,
34819 "%qT declared with a mismatched class-key %qs",
34820 type_decl, xmatchkstr))
34821 /* Suggest how to avoid the warning for each instance since
34822 the guidance may be different depending on context. */
34823 inform (loc,
34824 (key_redundant_p
34825 ? G_("remove the class-key or replace it with %qs")
34826 : G_("replace the class-key with %qs")),
34827 xpectkstr);
34830 /* Restore the current function in case it was replaced above. */
34831 current_function_decl = save_func;
34834 /* Issues -Wmismatched-tags for all classes. Called at the end
34835 of processing a translation unit, after declarations of all class
34836 types and their uses have been recorded. */
34838 void
34839 class_decl_loc_t::diag_mismatched_tags ()
34841 /* CLASS2LOC should be empty if both -Wmismatched-tags and
34842 -Wredundant-tags are disabled. */
34843 gcc_assert (warn_mismatched_tags
34844 || warn_redundant_tags
34845 || class2loc.is_empty ());
34847 /* Save the current function before changing on return. It should
34848 be null at this point. */
34849 temp_override<tree> cleanup (current_function_decl);
34851 if (warn_mismatched_tags)
34853 /* Iterate over the collected class/struct/template declarations. */
34854 typedef class_to_loc_map_t::iterator iter_t;
34855 for (iter_t it = class2loc.begin (); it != class2loc.end (); ++it)
34857 tree type_decl = (*it).first;
34858 class_decl_loc_t &recloc = (*it).second;
34859 recloc.diag_mismatched_tags (type_decl);
34863 class2loc.empty ();
34866 /* Issue an error message if DECL is redeclared with different
34867 access than its original declaration [class.access.spec/3].
34868 This applies to nested classes, nested class templates and
34869 enumerations [class.mem/1]. */
34871 static void
34872 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
34874 if (!decl
34875 || (!CLASS_TYPE_P (TREE_TYPE (decl))
34876 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
34877 return;
34879 if ((TREE_PRIVATE (decl)
34880 != (current_access_specifier == access_private_node))
34881 || (TREE_PROTECTED (decl)
34882 != (current_access_specifier == access_protected_node)))
34883 error_at (location, "%qD redeclared with different access", decl);
34886 /* Look for the `template' keyword, as a syntactic disambiguator.
34887 Return TRUE iff it is present, in which case it will be
34888 consumed. */
34890 static bool
34891 cp_parser_optional_template_keyword (cp_parser *parser)
34893 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
34895 /* In C++98 the `template' keyword can only be used within templates;
34896 outside templates the parser can always figure out what is a
34897 template and what is not. In C++11, per the resolution of DR 468,
34898 `template' is allowed in cases where it is not strictly necessary. */
34899 if (!processing_template_decl
34900 && pedantic && cxx_dialect == cxx98)
34902 cp_token *token = cp_lexer_peek_token (parser->lexer);
34903 pedwarn (token->location, OPT_Wpedantic,
34904 "in C++98 %<template%> (as a disambiguator) is only "
34905 "allowed within templates");
34906 /* If this part of the token stream is rescanned, the same
34907 error message would be generated. So, we purge the token
34908 from the stream. */
34909 cp_lexer_purge_token (parser->lexer);
34910 return false;
34912 else
34914 /* Consume the `template' keyword. */
34915 cp_lexer_consume_token (parser->lexer);
34916 return true;
34919 return false;
34922 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
34923 set PARSER->SCOPE, and perform other related actions. */
34925 static void
34926 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
34928 struct tree_check *check_value;
34930 /* Get the stored value. */
34931 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
34932 /* Set the scope from the stored value. */
34933 parser->scope = saved_checks_value (check_value);
34934 parser->qualifying_scope = check_value->qualifying_scope;
34935 parser->object_scope = parser->context->object_type;
34936 parser->context->object_type = NULL_TREE;
34939 /* Consume tokens up through a non-nested END token. Returns TRUE if we
34940 encounter the end of a block before what we were looking for. */
34942 static bool
34943 cp_parser_cache_group (cp_parser *parser,
34944 enum cpp_ttype end,
34945 unsigned depth)
34947 while (true)
34949 cp_token *token = cp_lexer_peek_token (parser->lexer);
34951 /* Abort a parenthesized expression if we encounter a semicolon. */
34952 if ((end == CPP_CLOSE_PAREN || depth == 0)
34953 && token->type == CPP_SEMICOLON)
34954 return true;
34955 /* If we've reached the end of the file, stop. */
34956 if (token->type == CPP_EOF
34957 || (end != CPP_PRAGMA_EOL
34958 && token->type == CPP_PRAGMA_EOL))
34959 return true;
34960 if (token->type == CPP_CLOSE_BRACE && depth == 0)
34961 /* We've hit the end of an enclosing block, so there's been some
34962 kind of syntax error. */
34963 return true;
34965 /* Consume the token. */
34966 cp_lexer_consume_token (parser->lexer);
34967 /* See if it starts a new group. */
34968 if (token->type == CPP_OPEN_BRACE)
34970 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
34971 /* In theory this should probably check end == '}', but
34972 cp_parser_save_member_function_body needs it to exit
34973 after either '}' or ')' when called with ')'. */
34974 if (depth == 0)
34975 return false;
34977 else if (token->type == CPP_OPEN_PAREN)
34979 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
34980 if (depth == 0 && end == CPP_CLOSE_PAREN)
34981 return false;
34983 else if (token->type == CPP_PRAGMA)
34984 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
34985 else if (token->type == end)
34986 return false;
34990 /* Like above, for caching a default argument or NSDMI. Both of these are
34991 terminated by a non-nested comma, but it can be unclear whether or not a
34992 comma is nested in a template argument list unless we do more parsing.
34993 In order to handle this ambiguity, when we encounter a ',' after a '<'
34994 we try to parse what follows as a parameter-declaration-list (in the
34995 case of a default argument) or a member-declarator (in the case of an
34996 NSDMI). If that succeeds, then we stop caching. */
34998 static tree
34999 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
35001 unsigned depth = 0;
35002 int maybe_template_id = 0;
35003 cp_token *first_token;
35004 cp_token *token;
35005 tree default_argument;
35007 /* Add tokens until we have processed the entire default
35008 argument. We add the range [first_token, token). */
35009 first_token = cp_lexer_peek_token (parser->lexer);
35010 if (first_token->type == CPP_OPEN_BRACE)
35012 /* For list-initialization, this is straightforward. */
35013 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
35014 token = cp_lexer_peek_token (parser->lexer);
35016 else while (true)
35018 bool done = false;
35020 /* Peek at the next token. */
35021 token = cp_lexer_peek_token (parser->lexer);
35022 /* What we do depends on what token we have. */
35023 switch (token->type)
35025 /* In valid code, a default argument must be
35026 immediately followed by a `,' `)', or `...'. */
35027 case CPP_COMMA:
35028 if (depth == 0 && maybe_template_id)
35030 /* If we've seen a '<', we might be in a
35031 template-argument-list. Until Core issue 325 is
35032 resolved, we don't know how this situation ought
35033 to be handled, so try to DTRT. We check whether
35034 what comes after the comma is a valid parameter
35035 declaration list. If it is, then the comma ends
35036 the default argument; otherwise the default
35037 argument continues. */
35038 bool error = false;
35039 cp_token *peek;
35041 /* Set ITALP so cp_parser_parameter_declaration_list
35042 doesn't decide to commit to this parse. */
35043 bool saved_italp = parser->in_template_argument_list_p;
35044 parser->in_template_argument_list_p = true;
35046 cp_parser_parse_tentatively (parser);
35048 if (nsdmi)
35050 /* Parse declarators until we reach a non-comma or
35051 somthing that cannot be an initializer.
35052 Just checking whether we're looking at a single
35053 declarator is insufficient. Consider:
35054 int var = tuple<T,U>::x;
35055 The template parameter 'U' looks exactly like a
35056 declarator. */
35059 int ctor_dtor_or_conv_p;
35060 cp_lexer_consume_token (parser->lexer);
35061 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
35062 CP_PARSER_FLAGS_NONE,
35063 &ctor_dtor_or_conv_p,
35064 /*parenthesized_p=*/NULL,
35065 /*member_p=*/true,
35066 /*friend_p=*/false,
35067 /*static_p=*/false);
35068 peek = cp_lexer_peek_token (parser->lexer);
35069 if (cp_parser_error_occurred (parser))
35070 break;
35072 while (peek->type == CPP_COMMA);
35073 /* If we met an '=' or ';' then the original comma
35074 was the end of the NSDMI. Otherwise assume
35075 we're still in the NSDMI. */
35076 error = (peek->type != CPP_EQ
35077 && peek->type != CPP_SEMICOLON);
35079 else
35081 cp_lexer_consume_token (parser->lexer);
35082 begin_scope (sk_function_parms, NULL_TREE);
35083 tree t = cp_parser_parameter_declaration_list
35084 (parser, CP_PARSER_FLAGS_NONE,
35085 /*pending_decls*/nullptr);
35086 if (t == error_mark_node)
35087 error = true;
35088 pop_bindings_and_leave_scope ();
35090 if (!cp_parser_error_occurred (parser) && !error)
35091 done = true;
35092 cp_parser_abort_tentative_parse (parser);
35094 parser->in_template_argument_list_p = saved_italp;
35095 break;
35097 /* FALLTHRU */
35098 case CPP_CLOSE_PAREN:
35099 case CPP_ELLIPSIS:
35100 /* If we run into a non-nested `;', `}', or `]',
35101 then the code is invalid -- but the default
35102 argument is certainly over. */
35103 case CPP_SEMICOLON:
35104 case CPP_CLOSE_BRACE:
35105 case CPP_CLOSE_SQUARE:
35106 if (depth == 0
35107 /* Handle correctly int n = sizeof ... ( p ); */
35108 && token->type != CPP_ELLIPSIS)
35109 done = true;
35110 /* Update DEPTH, if necessary. */
35111 else if (token->type == CPP_CLOSE_PAREN
35112 || token->type == CPP_CLOSE_BRACE
35113 || token->type == CPP_CLOSE_SQUARE)
35114 --depth;
35115 break;
35117 case CPP_OPEN_PAREN:
35118 case CPP_OPEN_SQUARE:
35119 case CPP_OPEN_BRACE:
35120 ++depth;
35121 break;
35123 case CPP_LESS:
35124 if (depth == 0)
35125 /* This might be the comparison operator, or it might
35126 start a template argument list. */
35127 ++maybe_template_id;
35128 break;
35130 case CPP_RSHIFT:
35131 if (cxx_dialect == cxx98)
35132 break;
35133 /* Fall through for C++0x, which treats the `>>'
35134 operator like two `>' tokens in certain
35135 cases. */
35136 gcc_fallthrough ();
35138 case CPP_GREATER:
35139 if (depth == 0)
35141 /* This might be an operator, or it might close a
35142 template argument list. But if a previous '<'
35143 started a template argument list, this will have
35144 closed it, so we can't be in one anymore. */
35145 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
35146 if (maybe_template_id < 0)
35147 maybe_template_id = 0;
35149 break;
35151 /* If we run out of tokens, issue an error message. */
35152 case CPP_EOF:
35153 case CPP_PRAGMA_EOL:
35154 error_at (token->location, "file ends in default argument");
35155 return error_mark_node;
35157 case CPP_NAME:
35158 case CPP_SCOPE:
35159 /* In these cases, we should look for template-ids.
35160 For example, if the default argument is
35161 `X<int, double>()', we need to do name lookup to
35162 figure out whether or not `X' is a template; if
35163 so, the `,' does not end the default argument.
35165 That is not yet done. */
35166 break;
35168 default:
35169 break;
35172 /* If we've reached the end, stop. */
35173 if (done)
35174 break;
35176 /* Add the token to the token block. */
35177 token = cp_lexer_consume_token (parser->lexer);
35180 /* Create a DEFERRED_PARSE to represent the unparsed default
35181 argument. */
35182 default_argument = make_node (DEFERRED_PARSE);
35183 DEFPARSE_TOKENS (default_argument)
35184 = cp_token_cache_new (first_token, token);
35185 DEFPARSE_INSTANTIATIONS (default_argument) = NULL;
35187 return default_argument;
35190 /* A location to use for diagnostics about an unparsed DEFERRED_PARSE. */
35192 location_t
35193 defparse_location (tree default_argument)
35195 cp_token_cache *tokens = DEFPARSE_TOKENS (default_argument);
35196 location_t start = tokens->first->location;
35197 location_t end = tokens->last->location;
35198 return make_location (start, start, end);
35201 /* Begin parsing tentatively. We always save tokens while parsing
35202 tentatively so that if the tentative parsing fails we can restore the
35203 tokens. */
35205 static void
35206 cp_parser_parse_tentatively (cp_parser* parser)
35208 /* Enter a new parsing context. */
35209 parser->context = cp_parser_context_new (parser->context);
35210 /* Begin saving tokens. */
35211 cp_lexer_save_tokens (parser->lexer);
35212 /* In order to avoid repetitive access control error messages,
35213 access checks are queued up until we are no longer parsing
35214 tentatively. */
35215 push_deferring_access_checks (dk_deferred);
35218 /* Commit to the currently active tentative parse. */
35220 static void
35221 cp_parser_commit_to_tentative_parse (cp_parser* parser)
35223 cp_parser_context *context;
35224 cp_lexer *lexer;
35226 /* Mark all of the levels as committed. */
35227 lexer = parser->lexer;
35228 for (context = parser->context; context->next; context = context->next)
35230 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
35231 break;
35232 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
35233 while (!cp_lexer_saving_tokens (lexer))
35234 lexer = lexer->next;
35235 cp_lexer_commit_tokens (lexer);
35239 /* Commit to the topmost currently active tentative parse.
35241 Note that this function shouldn't be called when there are
35242 irreversible side-effects while in a tentative state. For
35243 example, we shouldn't create a permanent entry in the symbol
35244 table, or issue an error message that might not apply if the
35245 tentative parse is aborted. */
35247 static void
35248 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
35250 cp_parser_context *context = parser->context;
35251 cp_lexer *lexer = parser->lexer;
35253 if (context)
35255 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
35256 return;
35257 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
35259 while (!cp_lexer_saving_tokens (lexer))
35260 lexer = lexer->next;
35261 cp_lexer_commit_tokens (lexer);
35265 /* Abort the currently active tentative parse. All consumed tokens
35266 will be rolled back, and no diagnostics will be issued. */
35268 static void
35269 cp_parser_abort_tentative_parse (cp_parser* parser)
35271 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
35272 || errorcount > 0);
35273 cp_parser_simulate_error (parser);
35274 /* Now, pretend that we want to see if the construct was
35275 successfully parsed. */
35276 cp_parser_parse_definitely (parser);
35279 /* Stop parsing tentatively. If a parse error has occurred, restore the
35280 token stream. Otherwise, commit to the tokens we have consumed.
35281 Returns true if no error occurred; false otherwise. */
35283 static bool
35284 cp_parser_parse_definitely (cp_parser* parser)
35286 bool error_occurred;
35287 cp_parser_context *context;
35289 /* Remember whether or not an error occurred, since we are about to
35290 destroy that information. */
35291 error_occurred = cp_parser_error_occurred (parser);
35292 /* Remove the topmost context from the stack. */
35293 context = parser->context;
35294 parser->context = context->next;
35295 /* If no parse errors occurred, commit to the tentative parse. */
35296 if (!error_occurred)
35298 /* Commit to the tokens read tentatively, unless that was
35299 already done. */
35300 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
35301 cp_lexer_commit_tokens (parser->lexer);
35303 pop_to_parent_deferring_access_checks ();
35305 /* Otherwise, if errors occurred, roll back our state so that things
35306 are just as they were before we began the tentative parse. */
35307 else
35309 cp_lexer_rollback_tokens (parser->lexer);
35310 pop_deferring_access_checks ();
35312 /* Add the context to the front of the free list. */
35313 context->next = cp_parser_context_free_list;
35314 cp_parser_context_free_list = context;
35316 return !error_occurred;
35319 /* Returns true if we are parsing tentatively and are not committed to
35320 this tentative parse. */
35322 static bool
35323 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
35325 return (cp_parser_parsing_tentatively (parser)
35326 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
35329 /* Returns nonzero iff an error has occurred during the most recent
35330 tentative parse. */
35332 static bool
35333 cp_parser_error_occurred (cp_parser* parser)
35335 return (cp_parser_parsing_tentatively (parser)
35336 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
35339 /* Returns nonzero if GNU extensions are allowed. */
35341 static bool
35342 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
35344 return parser->allow_gnu_extensions_p;
35347 /* Objective-C++ Productions */
35350 /* Parse an Objective-C expression, which feeds into a primary-expression
35351 above.
35353 objc-expression:
35354 objc-message-expression
35355 objc-string-literal
35356 objc-encode-expression
35357 objc-protocol-expression
35358 objc-selector-expression
35360 Returns a tree representation of the expression. */
35362 static cp_expr
35363 cp_parser_objc_expression (cp_parser* parser)
35365 /* Try to figure out what kind of declaration is present. */
35366 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
35368 switch (kwd->type)
35370 case CPP_OPEN_SQUARE:
35371 return cp_parser_objc_message_expression (parser);
35373 case CPP_OBJC_STRING:
35374 kwd = cp_lexer_consume_token (parser->lexer);
35375 return objc_build_string_object (kwd->u.value);
35377 case CPP_KEYWORD:
35378 switch (kwd->keyword)
35380 case RID_AT_ENCODE:
35381 return cp_parser_objc_encode_expression (parser);
35383 case RID_AT_PROTOCOL:
35384 return cp_parser_objc_protocol_expression (parser);
35386 case RID_AT_SELECTOR:
35387 return cp_parser_objc_selector_expression (parser);
35389 default:
35390 break;
35392 /* FALLTHRU */
35393 default:
35394 error_at (kwd->location,
35395 "misplaced %<@%D%> Objective-C++ construct",
35396 kwd->u.value);
35397 cp_parser_skip_to_end_of_block_or_statement (parser);
35400 return error_mark_node;
35403 /* Parse an Objective-C message expression.
35405 objc-message-expression:
35406 [ objc-message-receiver objc-message-args ]
35408 Returns a representation of an Objective-C message. */
35410 static tree
35411 cp_parser_objc_message_expression (cp_parser* parser)
35413 tree receiver, messageargs;
35415 parser->objective_c_message_context_p = true;
35416 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
35417 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
35418 receiver = cp_parser_objc_message_receiver (parser);
35419 messageargs = cp_parser_objc_message_args (parser);
35420 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
35421 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
35423 tree result = objc_build_message_expr (receiver, messageargs);
35425 /* Construct a location e.g.
35426 [self func1:5]
35427 ^~~~~~~~~~~~~~
35428 ranging from the '[' to the ']', with the caret at the start. */
35429 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
35430 protected_set_expr_location (result, combined_loc);
35432 parser->objective_c_message_context_p = false;
35433 return result;
35436 /* Parse an objc-message-receiver.
35438 objc-message-receiver:
35439 expression
35440 simple-type-specifier
35442 Returns a representation of the type or expression. */
35444 static tree
35445 cp_parser_objc_message_receiver (cp_parser* parser)
35447 tree rcv;
35449 /* An Objective-C message receiver may be either (1) a type
35450 or (2) an expression. */
35451 cp_parser_parse_tentatively (parser);
35452 rcv = cp_parser_expression (parser);
35454 /* If that worked out, fine. */
35455 if (cp_parser_parse_definitely (parser))
35456 return rcv;
35458 cp_parser_parse_tentatively (parser);
35459 rcv = cp_parser_simple_type_specifier (parser,
35460 /*decl_specs=*/NULL,
35461 CP_PARSER_FLAGS_NONE);
35463 if (cp_parser_parse_definitely (parser))
35464 return objc_get_class_reference (rcv);
35466 cp_parser_error (parser, "objective-c++ message receiver expected");
35467 return error_mark_node;
35470 /* Parse the arguments and selectors comprising an Objective-C message.
35472 objc-message-args:
35473 objc-selector
35474 objc-selector-args
35475 objc-selector-args , objc-comma-args
35477 objc-selector-args:
35478 objc-selector [opt] : assignment-expression
35479 objc-selector-args objc-selector [opt] : assignment-expression
35481 objc-comma-args:
35482 assignment-expression
35483 objc-comma-args , assignment-expression
35485 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
35486 selector arguments and TREE_VALUE containing a list of comma
35487 arguments. */
35489 static tree
35490 cp_parser_objc_message_args (cp_parser* parser)
35492 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
35493 bool maybe_unary_selector_p = true;
35494 cp_token *token = cp_lexer_peek_token (parser->lexer);
35496 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
35498 tree selector = NULL_TREE, arg;
35500 if (token->type != CPP_COLON)
35501 selector = cp_parser_objc_selector (parser);
35503 /* Detect if we have a unary selector. */
35504 if (maybe_unary_selector_p
35505 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
35506 return build_tree_list (selector, NULL_TREE);
35508 maybe_unary_selector_p = false;
35509 cp_parser_require (parser, CPP_COLON, RT_COLON);
35510 arg = cp_parser_assignment_expression (parser);
35512 sel_args
35513 = chainon (sel_args,
35514 build_tree_list (selector, arg));
35516 token = cp_lexer_peek_token (parser->lexer);
35519 /* Handle non-selector arguments, if any. */
35520 while (token->type == CPP_COMMA)
35522 tree arg;
35524 cp_lexer_consume_token (parser->lexer);
35525 arg = cp_parser_assignment_expression (parser);
35527 addl_args
35528 = chainon (addl_args,
35529 build_tree_list (NULL_TREE, arg));
35531 token = cp_lexer_peek_token (parser->lexer);
35534 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
35536 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
35537 return build_tree_list (error_mark_node, error_mark_node);
35540 return build_tree_list (sel_args, addl_args);
35543 /* Parse an Objective-C encode expression.
35545 objc-encode-expression:
35546 @encode objc-typename
35548 Returns an encoded representation of the type argument. */
35550 static cp_expr
35551 cp_parser_objc_encode_expression (cp_parser* parser)
35553 tree type;
35554 cp_token *token;
35555 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
35557 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
35558 matching_parens parens;
35559 parens.require_open (parser);
35560 token = cp_lexer_peek_token (parser->lexer);
35561 type = complete_type (cp_parser_type_id (parser));
35562 parens.require_close (parser);
35564 if (!type)
35566 error_at (token->location,
35567 "%<@encode%> must specify a type as an argument");
35568 return error_mark_node;
35571 /* This happens if we find @encode(T) (where T is a template
35572 typename or something dependent on a template typename) when
35573 parsing a template. In that case, we can't compile it
35574 immediately, but we rather create an AT_ENCODE_EXPR which will
35575 need to be instantiated when the template is used.
35577 if (dependent_type_p (type))
35579 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
35580 TREE_READONLY (value) = 1;
35581 return value;
35585 /* Build a location of the form:
35586 @encode(int)
35587 ^~~~~~~~~~~~
35588 with caret==start at the @ token, finishing at the close paren. */
35589 location_t combined_loc = make_location (start_loc, start_loc, parser->lexer);
35591 return cp_expr (objc_build_encode_expr (type), combined_loc);
35594 /* Parse an Objective-C @defs expression. */
35596 static tree
35597 cp_parser_objc_defs_expression (cp_parser *parser)
35599 tree name;
35601 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
35602 matching_parens parens;
35603 parens.require_open (parser);
35604 name = cp_parser_identifier (parser);
35605 parens.require_close (parser);
35607 return objc_get_class_ivars (name);
35610 /* Parse an Objective-C protocol expression.
35612 objc-protocol-expression:
35613 @protocol ( identifier )
35615 Returns a representation of the protocol expression. */
35617 static tree
35618 cp_parser_objc_protocol_expression (cp_parser* parser)
35620 tree proto;
35621 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
35623 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
35624 matching_parens parens;
35625 parens.require_open (parser);
35626 proto = cp_parser_identifier (parser);
35627 parens.require_close (parser);
35629 /* Build a location of the form:
35630 @protocol(prot)
35631 ^~~~~~~~~~~~~~~
35632 with caret==start at the @ token, finishing at the close paren. */
35633 location_t combined_loc = make_location (start_loc, start_loc, parser->lexer);
35634 tree result = objc_build_protocol_expr (proto);
35635 protected_set_expr_location (result, combined_loc);
35636 return result;
35639 /* Parse an Objective-C selector expression.
35641 objc-selector-expression:
35642 @selector ( objc-method-signature )
35644 objc-method-signature:
35645 objc-selector
35646 objc-selector-seq
35648 objc-selector-seq:
35649 objc-selector :
35650 objc-selector-seq objc-selector :
35652 Returns a representation of the method selector. */
35654 static tree
35655 cp_parser_objc_selector_expression (cp_parser* parser)
35657 tree sel_seq = NULL_TREE;
35658 bool maybe_unary_selector_p = true;
35659 cp_token *token;
35660 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35662 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
35663 matching_parens parens;
35664 parens.require_open (parser);
35665 token = cp_lexer_peek_token (parser->lexer);
35667 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
35668 || token->type == CPP_SCOPE)
35670 tree selector = NULL_TREE;
35672 if (token->type != CPP_COLON
35673 || token->type == CPP_SCOPE)
35674 selector = cp_parser_objc_selector (parser);
35676 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
35677 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
35679 /* Detect if we have a unary selector. */
35680 if (maybe_unary_selector_p)
35682 sel_seq = selector;
35683 goto finish_selector;
35685 else
35687 cp_parser_error (parser, "expected %<:%>");
35690 maybe_unary_selector_p = false;
35691 token = cp_lexer_consume_token (parser->lexer);
35693 if (token->type == CPP_SCOPE)
35695 sel_seq
35696 = chainon (sel_seq,
35697 build_tree_list (selector, NULL_TREE));
35698 sel_seq
35699 = chainon (sel_seq,
35700 build_tree_list (NULL_TREE, NULL_TREE));
35702 else
35703 sel_seq
35704 = chainon (sel_seq,
35705 build_tree_list (selector, NULL_TREE));
35707 token = cp_lexer_peek_token (parser->lexer);
35710 finish_selector:
35711 parens.require_close (parser);
35714 /* Build a location of the form:
35715 @selector(func)
35716 ^~~~~~~~~~~~~~~
35717 with caret==start at the @ token, finishing at the close paren. */
35718 location_t combined_loc = make_location (loc, loc, parser->lexer);
35719 tree result = objc_build_selector_expr (combined_loc, sel_seq);
35720 /* TODO: objc_build_selector_expr doesn't always honor the location. */
35721 protected_set_expr_location (result, combined_loc);
35722 return result;
35725 /* Parse a list of identifiers.
35727 objc-identifier-list:
35728 identifier
35729 objc-identifier-list , identifier
35731 Returns a TREE_LIST of identifier nodes. */
35733 static tree
35734 cp_parser_objc_identifier_list (cp_parser* parser)
35736 tree identifier;
35737 tree list;
35738 cp_token *sep;
35740 identifier = cp_parser_identifier (parser);
35741 if (identifier == error_mark_node)
35742 return error_mark_node;
35744 list = build_tree_list (NULL_TREE, identifier);
35745 sep = cp_lexer_peek_token (parser->lexer);
35747 while (sep->type == CPP_COMMA)
35749 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
35750 identifier = cp_parser_identifier (parser);
35751 if (identifier == error_mark_node)
35752 return list;
35754 list = chainon (list, build_tree_list (NULL_TREE,
35755 identifier));
35756 sep = cp_lexer_peek_token (parser->lexer);
35759 return list;
35762 /* Parse an Objective-C alias declaration.
35764 objc-alias-declaration:
35765 @compatibility_alias identifier identifier ;
35767 This function registers the alias mapping with the Objective-C front end.
35768 It returns nothing. */
35770 static void
35771 cp_parser_objc_alias_declaration (cp_parser* parser)
35773 tree alias, orig;
35775 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
35776 alias = cp_parser_identifier (parser);
35777 orig = cp_parser_identifier (parser);
35778 objc_declare_alias (alias, orig);
35779 cp_parser_consume_semicolon_at_end_of_statement (parser);
35782 /* Parse an Objective-C class forward-declaration.
35784 objc-class-declaration:
35785 @class objc-identifier-list ;
35787 The function registers the forward declarations with the Objective-C
35788 front end. It returns nothing. */
35790 static void
35791 cp_parser_objc_class_declaration (cp_parser* parser)
35793 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
35794 while (true)
35796 tree id;
35798 id = cp_parser_identifier (parser);
35799 if (id == error_mark_node)
35800 break;
35802 objc_declare_class (id);
35804 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
35805 cp_lexer_consume_token (parser->lexer);
35806 else
35807 break;
35809 cp_parser_consume_semicolon_at_end_of_statement (parser);
35812 /* Parse a list of Objective-C protocol references.
35814 objc-protocol-refs-opt:
35815 objc-protocol-refs [opt]
35817 objc-protocol-refs:
35818 < objc-identifier-list >
35820 Returns a TREE_LIST of identifiers, if any. */
35822 static tree
35823 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
35825 tree protorefs = NULL_TREE;
35827 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
35829 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
35830 protorefs = cp_parser_objc_identifier_list (parser);
35831 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
35834 return protorefs;
35837 /* Parse a Objective-C visibility specification. */
35839 static void
35840 cp_parser_objc_visibility_spec (cp_parser* parser)
35842 cp_token *vis = cp_lexer_peek_token (parser->lexer);
35844 switch (vis->keyword)
35846 case RID_AT_PRIVATE:
35847 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
35848 break;
35849 case RID_AT_PROTECTED:
35850 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
35851 break;
35852 case RID_AT_PUBLIC:
35853 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
35854 break;
35855 case RID_AT_PACKAGE:
35856 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
35857 break;
35858 default:
35859 return;
35862 /* Eat '@private'/'@protected'/'@public'. */
35863 cp_lexer_consume_token (parser->lexer);
35866 /* Parse an Objective-C method type. Return 'true' if it is a class
35867 (+) method, and 'false' if it is an instance (-) method. */
35869 static inline bool
35870 cp_parser_objc_method_type (cp_parser* parser)
35872 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
35873 return true;
35874 else
35875 return false;
35878 /* Parse an Objective-C protocol qualifier. */
35880 static tree
35881 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
35883 tree quals = NULL_TREE, node;
35884 cp_token *token = cp_lexer_peek_token (parser->lexer);
35886 node = token->u.value;
35888 while (node && identifier_p (node)
35889 && (node == ridpointers [(int) RID_IN]
35890 || node == ridpointers [(int) RID_OUT]
35891 || node == ridpointers [(int) RID_INOUT]
35892 || node == ridpointers [(int) RID_BYCOPY]
35893 || node == ridpointers [(int) RID_BYREF]
35894 || node == ridpointers [(int) RID_ONEWAY]))
35896 quals = tree_cons (NULL_TREE, node, quals);
35897 cp_lexer_consume_token (parser->lexer);
35898 token = cp_lexer_peek_token (parser->lexer);
35899 node = token->u.value;
35902 return quals;
35905 /* Parse an Objective-C typename. */
35907 static tree
35908 cp_parser_objc_typename (cp_parser* parser)
35910 tree type_name = NULL_TREE;
35912 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
35914 tree proto_quals, cp_type = NULL_TREE;
35916 matching_parens parens;
35917 parens.consume_open (parser); /* Eat '('. */
35918 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
35920 /* An ObjC type name may consist of just protocol qualifiers, in which
35921 case the type shall default to 'id'. */
35922 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
35924 cp_type = cp_parser_type_id (parser);
35926 /* If the type could not be parsed, an error has already
35927 been produced. For error recovery, behave as if it had
35928 not been specified, which will use the default type
35929 'id'. */
35930 if (cp_type == error_mark_node)
35932 cp_type = NULL_TREE;
35933 /* We need to skip to the closing parenthesis as
35934 cp_parser_type_id() does not seem to do it for
35935 us. */
35936 cp_parser_skip_to_closing_parenthesis (parser,
35937 /*recovering=*/true,
35938 /*or_comma=*/false,
35939 /*consume_paren=*/false);
35943 parens.require_close (parser);
35944 type_name = build_tree_list (proto_quals, cp_type);
35947 return type_name;
35950 /* Check to see if TYPE refers to an Objective-C selector name. */
35952 static bool
35953 cp_parser_objc_selector_p (enum cpp_ttype type)
35955 return (type == CPP_NAME || type == CPP_KEYWORD
35956 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
35957 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
35958 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
35959 || type == CPP_XOR || type == CPP_XOR_EQ);
35962 /* Parse an Objective-C selector. */
35964 static tree
35965 cp_parser_objc_selector (cp_parser* parser)
35967 cp_token *token = cp_lexer_consume_token (parser->lexer);
35969 if (!cp_parser_objc_selector_p (token->type))
35971 error_at (token->location, "invalid Objective-C++ selector name");
35972 return error_mark_node;
35975 /* C++ operator names are allowed to appear in ObjC selectors. */
35976 switch (token->type)
35978 case CPP_AND_AND: return get_identifier ("and");
35979 case CPP_AND_EQ: return get_identifier ("and_eq");
35980 case CPP_AND: return get_identifier ("bitand");
35981 case CPP_OR: return get_identifier ("bitor");
35982 case CPP_COMPL: return get_identifier ("compl");
35983 case CPP_NOT: return get_identifier ("not");
35984 case CPP_NOT_EQ: return get_identifier ("not_eq");
35985 case CPP_OR_OR: return get_identifier ("or");
35986 case CPP_OR_EQ: return get_identifier ("or_eq");
35987 case CPP_XOR: return get_identifier ("xor");
35988 case CPP_XOR_EQ: return get_identifier ("xor_eq");
35989 default: return token->u.value;
35993 /* Parse an Objective-C params list. */
35995 static tree
35996 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
35998 tree params = NULL_TREE;
35999 bool maybe_unary_selector_p = true;
36000 cp_token *token = cp_lexer_peek_token (parser->lexer);
36002 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
36004 tree selector = NULL_TREE, type_name, identifier;
36005 tree parm_attr = NULL_TREE;
36007 if (token->keyword == RID_ATTRIBUTE)
36008 break;
36010 if (token->type != CPP_COLON)
36011 selector = cp_parser_objc_selector (parser);
36013 /* Detect if we have a unary selector. */
36014 if (maybe_unary_selector_p
36015 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
36017 params = selector; /* Might be followed by attributes. */
36018 break;
36021 maybe_unary_selector_p = false;
36022 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
36024 /* Something went quite wrong. There should be a colon
36025 here, but there is not. Stop parsing parameters. */
36026 break;
36028 type_name = cp_parser_objc_typename (parser);
36029 /* New ObjC allows attributes on parameters too. */
36030 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
36031 parm_attr = cp_parser_attributes_opt (parser);
36032 identifier = cp_parser_identifier (parser);
36034 params
36035 = chainon (params,
36036 objc_build_keyword_decl (selector,
36037 type_name,
36038 identifier,
36039 parm_attr));
36041 token = cp_lexer_peek_token (parser->lexer);
36044 if (params == NULL_TREE)
36046 cp_parser_error (parser, "objective-c++ method declaration is expected");
36047 return error_mark_node;
36050 /* We allow tail attributes for the method. */
36051 if (token->keyword == RID_ATTRIBUTE)
36053 *attributes = cp_parser_attributes_opt (parser);
36054 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
36055 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
36056 return params;
36057 cp_parser_error (parser,
36058 "method attributes must be specified at the end");
36059 return error_mark_node;
36062 if (params == NULL_TREE)
36064 cp_parser_error (parser, "objective-c++ method declaration is expected");
36065 return error_mark_node;
36067 return params;
36070 /* Parse the non-keyword Objective-C params. */
36072 static tree
36073 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
36074 tree* attributes)
36076 tree params = make_node (TREE_LIST);
36077 cp_token *token = cp_lexer_peek_token (parser->lexer);
36078 *ellipsisp = false; /* Initially, assume no ellipsis. */
36080 while (token->type == CPP_COMMA)
36082 cp_parameter_declarator *parmdecl;
36083 tree parm;
36085 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
36086 token = cp_lexer_peek_token (parser->lexer);
36088 if (token->type == CPP_ELLIPSIS)
36090 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
36091 *ellipsisp = true;
36092 token = cp_lexer_peek_token (parser->lexer);
36093 break;
36096 /* TODO: parse attributes for tail parameters. */
36097 parmdecl = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
36098 false, NULL);
36099 parm = grokdeclarator (parmdecl->declarator,
36100 &parmdecl->decl_specifiers,
36101 PARM, /*initialized=*/0,
36102 /*attrlist=*/NULL);
36104 chainon (params, build_tree_list (NULL_TREE, parm));
36105 token = cp_lexer_peek_token (parser->lexer);
36108 /* We allow tail attributes for the method. */
36109 if (token->keyword == RID_ATTRIBUTE)
36111 if (*attributes == NULL_TREE)
36113 *attributes = cp_parser_attributes_opt (parser);
36114 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
36115 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
36116 return params;
36118 else
36119 /* We have an error, but parse the attributes, so that we can
36120 carry on. */
36121 *attributes = cp_parser_attributes_opt (parser);
36123 cp_parser_error (parser,
36124 "method attributes must be specified at the end");
36125 return error_mark_node;
36128 return params;
36131 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
36133 static void
36134 cp_parser_objc_interstitial_code (cp_parser* parser)
36136 cp_token *token = cp_lexer_peek_token (parser->lexer);
36138 /* If the next token is `extern' and the following token is a string
36139 literal, then we have a linkage specification. */
36140 if (token->keyword == RID_EXTERN
36141 && cp_parser_is_pure_string_literal
36142 (cp_lexer_peek_nth_token (parser->lexer, 2)))
36143 cp_parser_linkage_specification (parser, NULL_TREE);
36144 /* Handle #pragma, if any. */
36145 else if (token->type == CPP_PRAGMA)
36146 cp_parser_pragma (parser, pragma_objc_icode, NULL);
36147 /* Allow stray semicolons. */
36148 else if (token->type == CPP_SEMICOLON)
36149 cp_lexer_consume_token (parser->lexer);
36150 /* Mark methods as optional or required, when building protocols. */
36151 else if (token->keyword == RID_AT_OPTIONAL)
36153 cp_lexer_consume_token (parser->lexer);
36154 objc_set_method_opt (true);
36156 else if (token->keyword == RID_AT_REQUIRED)
36158 cp_lexer_consume_token (parser->lexer);
36159 objc_set_method_opt (false);
36161 else if (token->keyword == RID_NAMESPACE)
36162 cp_parser_namespace_definition (parser);
36163 /* Other stray characters must generate errors. */
36164 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
36166 cp_lexer_consume_token (parser->lexer);
36167 error ("stray %qs between Objective-C++ methods",
36168 token->type == CPP_OPEN_BRACE ? "{" : "}");
36170 /* Finally, try to parse a block-declaration, or a function-definition. */
36171 else
36172 cp_parser_block_declaration (parser, /*statement_p=*/false);
36175 /* Parse a method signature. */
36177 static tree
36178 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
36180 tree rettype, kwdparms, optparms;
36181 bool ellipsis = false;
36182 bool is_class_method;
36184 is_class_method = cp_parser_objc_method_type (parser);
36185 rettype = cp_parser_objc_typename (parser);
36186 *attributes = NULL_TREE;
36187 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
36188 if (kwdparms == error_mark_node)
36189 return error_mark_node;
36190 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
36191 if (optparms == error_mark_node)
36192 return error_mark_node;
36194 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
36197 static bool
36198 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
36200 tree tattr;
36201 cp_lexer_save_tokens (parser->lexer);
36202 tattr = cp_parser_attributes_opt (parser);
36203 gcc_assert (tattr) ;
36205 /* If the attributes are followed by a method introducer, this is not allowed.
36206 Dump the attributes and flag the situation. */
36207 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
36208 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
36209 return true;
36211 /* Otherwise, the attributes introduce some interstitial code, possibly so
36212 rewind to allow that check. */
36213 cp_lexer_rollback_tokens (parser->lexer);
36214 return false;
36217 /* Parse an Objective-C method prototype list. */
36219 static void
36220 cp_parser_objc_method_prototype_list (cp_parser* parser)
36222 cp_token *token = cp_lexer_peek_token (parser->lexer);
36224 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
36226 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
36228 tree attributes, sig;
36229 bool is_class_method;
36230 if (token->type == CPP_PLUS)
36231 is_class_method = true;
36232 else
36233 is_class_method = false;
36234 sig = cp_parser_objc_method_signature (parser, &attributes);
36235 if (sig == error_mark_node)
36237 cp_parser_skip_to_end_of_block_or_statement (parser);
36238 token = cp_lexer_peek_token (parser->lexer);
36239 continue;
36241 objc_add_method_declaration (is_class_method, sig, attributes);
36242 cp_parser_consume_semicolon_at_end_of_statement (parser);
36244 else if (token->keyword == RID_AT_PROPERTY)
36245 cp_parser_objc_at_property_declaration (parser);
36246 else if (token->keyword == RID_ATTRIBUTE
36247 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
36248 warning_at (cp_lexer_peek_token (parser->lexer)->location,
36249 OPT_Wattributes,
36250 "prefix attributes are ignored for methods");
36251 else
36252 /* Allow for interspersed non-ObjC++ code. */
36253 cp_parser_objc_interstitial_code (parser);
36255 token = cp_lexer_peek_token (parser->lexer);
36258 if (token->type != CPP_EOF)
36259 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
36260 else
36261 cp_parser_error (parser, "expected %<@end%>");
36263 objc_finish_interface ();
36266 /* Parse an Objective-C method definition list. */
36268 static void
36269 cp_parser_objc_method_definition_list (cp_parser* parser)
36271 for (;;)
36273 cp_token *token = cp_lexer_peek_token (parser->lexer);
36275 if (token->keyword == RID_AT_END)
36277 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
36278 break;
36280 else if (token->type == CPP_EOF)
36282 cp_parser_error (parser, "expected %<@end%>");
36283 break;
36285 else if (token->type == CPP_PLUS || token->type == CPP_MINUS)
36287 bool is_class_method = token->type == CPP_PLUS;
36289 push_deferring_access_checks (dk_deferred);
36290 tree attribute;
36291 tree sig = cp_parser_objc_method_signature (parser, &attribute);
36292 if (sig == error_mark_node)
36293 cp_parser_skip_to_end_of_block_or_statement (parser);
36294 else
36296 objc_start_method_definition (is_class_method, sig,
36297 attribute, NULL_TREE);
36299 /* For historical reasons, we accept an optional semicolon. */
36300 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
36301 cp_lexer_consume_token (parser->lexer);
36303 perform_deferred_access_checks (tf_warning_or_error);
36304 stop_deferring_access_checks ();
36305 tree meth
36306 = cp_parser_function_definition_after_declarator (parser, false);
36307 pop_deferring_access_checks ();
36308 objc_finish_method_definition (meth);
36311 /* The following case will be removed once @synthesize is
36312 completely implemented. */
36313 else if (token->keyword == RID_AT_PROPERTY)
36314 cp_parser_objc_at_property_declaration (parser);
36315 else if (token->keyword == RID_AT_SYNTHESIZE)
36316 cp_parser_objc_at_synthesize_declaration (parser);
36317 else if (token->keyword == RID_AT_DYNAMIC)
36318 cp_parser_objc_at_dynamic_declaration (parser);
36319 else if (token->keyword == RID_ATTRIBUTE
36320 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
36321 warning_at (token->location, OPT_Wattributes,
36322 "prefix attributes are ignored for methods");
36323 else
36324 /* Allow for interspersed non-ObjC++ code. */
36325 cp_parser_objc_interstitial_code (parser);
36328 objc_finish_implementation ();
36331 /* Parse Objective-C ivars. */
36333 static void
36334 cp_parser_objc_class_ivars (cp_parser* parser)
36336 cp_token *token = cp_lexer_peek_token (parser->lexer);
36338 if (token->type != CPP_OPEN_BRACE)
36339 return; /* No ivars specified. */
36341 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
36342 token = cp_lexer_peek_token (parser->lexer);
36344 while (token->type != CPP_CLOSE_BRACE
36345 && token->keyword != RID_AT_END && token->type != CPP_EOF)
36347 cp_decl_specifier_seq declspecs;
36348 int decl_class_or_enum_p;
36349 tree prefix_attributes;
36351 cp_parser_objc_visibility_spec (parser);
36353 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
36354 break;
36356 cp_parser_decl_specifier_seq (parser,
36357 CP_PARSER_FLAGS_OPTIONAL,
36358 &declspecs,
36359 &decl_class_or_enum_p);
36361 /* auto, register, static, extern, mutable. */
36362 if (declspecs.storage_class != sc_none)
36364 cp_parser_error (parser, "invalid type for instance variable");
36365 declspecs.storage_class = sc_none;
36368 /* thread_local. */
36369 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
36371 cp_parser_error (parser, "invalid type for instance variable");
36372 declspecs.locations[ds_thread] = 0;
36375 /* typedef. */
36376 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
36378 cp_parser_error (parser, "invalid type for instance variable");
36379 declspecs.locations[ds_typedef] = 0;
36382 prefix_attributes = declspecs.attributes;
36383 declspecs.attributes = NULL_TREE;
36385 /* Keep going until we hit the `;' at the end of the
36386 declaration. */
36387 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
36389 tree width = NULL_TREE, attributes, first_attribute, decl;
36390 cp_declarator *declarator = NULL;
36391 int ctor_dtor_or_conv_p;
36393 /* Check for a (possibly unnamed) bitfield declaration. */
36394 token = cp_lexer_peek_token (parser->lexer);
36395 if (token->type == CPP_COLON)
36396 goto eat_colon;
36398 if (token->type == CPP_NAME
36399 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
36400 == CPP_COLON))
36402 /* Get the name of the bitfield. */
36403 declarator = make_id_declarator (NULL_TREE,
36404 cp_parser_identifier (parser),
36405 sfk_none, token->location);
36407 eat_colon:
36408 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
36409 /* Get the width of the bitfield. */
36410 width
36411 = cp_parser_constant_expression (parser);
36413 else
36415 /* Parse the declarator. */
36416 declarator
36417 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
36418 CP_PARSER_FLAGS_NONE,
36419 &ctor_dtor_or_conv_p,
36420 /*parenthesized_p=*/NULL,
36421 /*member_p=*/false,
36422 /*friend_p=*/false,
36423 /*static_p=*/false);
36426 /* Look for attributes that apply to the ivar. */
36427 attributes = cp_parser_attributes_opt (parser);
36428 /* Remember which attributes are prefix attributes and
36429 which are not. */
36430 first_attribute = attributes;
36431 /* Combine the attributes. */
36432 attributes = attr_chainon (prefix_attributes, attributes);
36434 if (width)
36435 /* Create the bitfield declaration. */
36436 decl = grokbitfield (declarator, &declspecs,
36437 width, NULL_TREE, attributes);
36438 else
36439 decl = grokfield (declarator, &declspecs,
36440 NULL_TREE, /*init_const_expr_p=*/false,
36441 NULL_TREE, attributes);
36443 /* Add the instance variable. */
36444 if (decl != error_mark_node && decl != NULL_TREE)
36445 objc_add_instance_variable (decl);
36447 /* Reset PREFIX_ATTRIBUTES. */
36448 if (attributes != error_mark_node)
36450 while (attributes && TREE_CHAIN (attributes) != first_attribute)
36451 attributes = TREE_CHAIN (attributes);
36452 if (attributes)
36453 TREE_CHAIN (attributes) = NULL_TREE;
36456 token = cp_lexer_peek_token (parser->lexer);
36458 if (token->type == CPP_COMMA)
36460 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
36461 continue;
36463 break;
36466 cp_parser_consume_semicolon_at_end_of_statement (parser);
36467 token = cp_lexer_peek_token (parser->lexer);
36470 if (token->keyword == RID_AT_END)
36471 cp_parser_error (parser, "expected %<}%>");
36473 /* Do not consume the RID_AT_END, so it will be read again as terminating
36474 the @interface of @implementation. */
36475 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
36476 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
36478 /* For historical reasons, we accept an optional semicolon. */
36479 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
36480 cp_lexer_consume_token (parser->lexer);
36483 /* Parse an Objective-C protocol declaration. */
36485 static void
36486 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
36488 tree proto, protorefs;
36489 cp_token *tok;
36491 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
36492 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
36494 tok = cp_lexer_peek_token (parser->lexer);
36495 error_at (tok->location, "identifier expected after %<@protocol%>");
36496 cp_parser_consume_semicolon_at_end_of_statement (parser);
36497 return;
36500 /* See if we have a forward declaration or a definition. */
36501 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
36503 /* Try a forward declaration first. */
36504 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
36506 while (true)
36508 tree id;
36510 id = cp_parser_identifier (parser);
36511 if (id == error_mark_node)
36512 break;
36514 objc_declare_protocol (id, attributes);
36516 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36517 cp_lexer_consume_token (parser->lexer);
36518 else
36519 break;
36521 cp_parser_consume_semicolon_at_end_of_statement (parser);
36524 /* Ok, we got a full-fledged definition (or at least should). */
36525 else
36527 proto = cp_parser_identifier (parser);
36528 protorefs = cp_parser_objc_protocol_refs_opt (parser);
36529 objc_start_protocol (proto, protorefs, attributes);
36530 cp_parser_objc_method_prototype_list (parser);
36534 /* Parse an Objective-C superclass or category. */
36536 static void
36537 cp_parser_objc_superclass_or_category (cp_parser *parser,
36538 bool iface_p,
36539 tree *super,
36540 tree *categ, bool *is_class_extension)
36542 cp_token *next = cp_lexer_peek_token (parser->lexer);
36544 *super = *categ = NULL_TREE;
36545 *is_class_extension = false;
36546 if (next->type == CPP_COLON)
36548 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
36549 *super = cp_parser_identifier (parser);
36551 else if (next->type == CPP_OPEN_PAREN)
36553 matching_parens parens;
36554 parens.consume_open (parser); /* Eat '('. */
36556 /* If there is no category name, and this is an @interface, we
36557 have a class extension. */
36558 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
36560 *categ = NULL_TREE;
36561 *is_class_extension = true;
36563 else
36564 *categ = cp_parser_identifier (parser);
36566 parens.require_close (parser);
36570 /* Parse an Objective-C class interface. */
36572 static void
36573 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
36575 tree name, super, categ, protos;
36576 bool is_class_extension;
36578 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
36579 location_t nam_loc = cp_lexer_peek_token (parser->lexer)->location;
36580 name = cp_parser_identifier (parser);
36581 if (name == error_mark_node)
36583 /* It's hard to recover because even if valid @interface stuff
36584 is to follow, we can't compile it (or validate it) if we
36585 don't even know which class it refers to. Let's assume this
36586 was a stray '@interface' token in the stream and skip it.
36588 return;
36590 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
36591 &is_class_extension);
36592 protos = cp_parser_objc_protocol_refs_opt (parser);
36594 /* We have either a class or a category on our hands. */
36595 if (categ || is_class_extension)
36596 objc_start_category_interface (name, categ, protos, attributes);
36597 else
36599 objc_start_class_interface (name, nam_loc, super, protos, attributes);
36600 /* Handle instance variable declarations, if any. */
36601 cp_parser_objc_class_ivars (parser);
36602 objc_continue_interface ();
36605 cp_parser_objc_method_prototype_list (parser);
36608 /* Parse an Objective-C class implementation. */
36610 static void
36611 cp_parser_objc_class_implementation (cp_parser* parser)
36613 tree name, super, categ;
36614 bool is_class_extension;
36616 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
36617 name = cp_parser_identifier (parser);
36618 if (name == error_mark_node)
36620 /* It's hard to recover because even if valid @implementation
36621 stuff is to follow, we can't compile it (or validate it) if
36622 we don't even know which class it refers to. Let's assume
36623 this was a stray '@implementation' token in the stream and
36624 skip it.
36626 return;
36628 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
36629 &is_class_extension);
36631 /* We have either a class or a category on our hands. */
36632 if (categ)
36633 objc_start_category_implementation (name, categ);
36634 else
36636 objc_start_class_implementation (name, super);
36637 /* Handle instance variable declarations, if any. */
36638 cp_parser_objc_class_ivars (parser);
36639 objc_continue_implementation ();
36642 cp_parser_objc_method_definition_list (parser);
36645 /* Consume the @end token and finish off the implementation. */
36647 static void
36648 cp_parser_objc_end_implementation (cp_parser* parser)
36650 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
36651 objc_finish_implementation ();
36654 /* Parse an Objective-C declaration. */
36656 static void
36657 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
36659 /* Try to figure out what kind of declaration is present. */
36660 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
36662 if (attributes)
36663 switch (kwd->keyword)
36665 case RID_AT_ALIAS:
36666 case RID_AT_CLASS:
36667 case RID_AT_END:
36668 error_at (kwd->location, "attributes may not be specified before"
36669 " the %<@%D%> Objective-C++ keyword",
36670 kwd->u.value);
36671 attributes = NULL;
36672 break;
36673 case RID_AT_IMPLEMENTATION:
36674 warning_at (kwd->location, OPT_Wattributes,
36675 "prefix attributes are ignored before %<@%D%>",
36676 kwd->u.value);
36677 attributes = NULL;
36678 default:
36679 break;
36682 switch (kwd->keyword)
36684 case RID_AT_ALIAS:
36685 cp_parser_objc_alias_declaration (parser);
36686 break;
36687 case RID_AT_CLASS:
36688 cp_parser_objc_class_declaration (parser);
36689 break;
36690 case RID_AT_PROTOCOL:
36691 cp_parser_objc_protocol_declaration (parser, attributes);
36692 break;
36693 case RID_AT_INTERFACE:
36694 cp_parser_objc_class_interface (parser, attributes);
36695 break;
36696 case RID_AT_IMPLEMENTATION:
36697 cp_parser_objc_class_implementation (parser);
36698 break;
36699 case RID_AT_END:
36700 cp_parser_objc_end_implementation (parser);
36701 break;
36702 default:
36703 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
36704 kwd->u.value);
36705 cp_parser_skip_to_end_of_block_or_statement (parser);
36709 /* Parse an Objective-C try-catch-finally statement.
36711 objc-try-catch-finally-stmt:
36712 @try compound-statement objc-catch-clause-seq [opt]
36713 objc-finally-clause [opt]
36715 objc-catch-clause-seq:
36716 objc-catch-clause objc-catch-clause-seq [opt]
36718 objc-catch-clause:
36719 @catch ( objc-exception-declaration ) compound-statement
36721 objc-finally-clause:
36722 @finally compound-statement
36724 objc-exception-declaration:
36725 parameter-declaration
36726 '...'
36728 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
36730 Returns NULL_TREE.
36732 PS: This function is identical to c_parser_objc_try_catch_finally_statement
36733 for C. Keep them in sync. */
36735 static tree
36736 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
36738 location_t location;
36739 tree stmt;
36741 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
36742 location = cp_lexer_peek_token (parser->lexer)->location;
36743 objc_maybe_warn_exceptions (location);
36744 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
36745 node, lest it get absorbed into the surrounding block. */
36746 stmt = push_stmt_list ();
36747 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
36748 objc_begin_try_stmt (location, pop_stmt_list (stmt));
36750 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
36752 cp_parameter_declarator *parm;
36753 tree parameter_declaration = error_mark_node;
36754 bool seen_open_paren = false;
36755 matching_parens parens;
36757 cp_lexer_consume_token (parser->lexer);
36758 if (parens.require_open (parser))
36759 seen_open_paren = true;
36760 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
36762 /* We have "@catch (...)" (where the '...' are literally
36763 what is in the code). Skip the '...'.
36764 parameter_declaration is set to NULL_TREE, and
36765 objc_being_catch_clauses() knows that that means
36766 '...'. */
36767 cp_lexer_consume_token (parser->lexer);
36768 parameter_declaration = NULL_TREE;
36770 else
36772 /* We have "@catch (NSException *exception)" or something
36773 like that. Parse the parameter declaration. */
36774 parm = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
36775 false, NULL);
36776 if (parm == NULL)
36777 parameter_declaration = error_mark_node;
36778 else
36779 parameter_declaration = grokdeclarator (parm->declarator,
36780 &parm->decl_specifiers,
36781 PARM, /*initialized=*/0,
36782 /*attrlist=*/NULL);
36784 if (seen_open_paren)
36785 parens.require_close (parser);
36786 else
36788 /* If there was no open parenthesis, we are recovering from
36789 an error, and we are trying to figure out what mistake
36790 the user has made. */
36792 /* If there is an immediate closing parenthesis, the user
36793 probably forgot the opening one (ie, they typed "@catch
36794 NSException *e)". Parse the closing parenthesis and keep
36795 going. */
36796 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
36797 cp_lexer_consume_token (parser->lexer);
36799 /* If these is no immediate closing parenthesis, the user
36800 probably doesn't know that parenthesis are required at
36801 all (ie, they typed "@catch NSException *e"). So, just
36802 forget about the closing parenthesis and keep going. */
36804 objc_begin_catch_clause (parameter_declaration);
36805 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
36806 objc_finish_catch_clause ();
36808 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
36810 cp_lexer_consume_token (parser->lexer);
36811 location = cp_lexer_peek_token (parser->lexer)->location;
36812 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
36813 node, lest it get absorbed into the surrounding block. */
36814 stmt = push_stmt_list ();
36815 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
36816 objc_build_finally_clause (location, pop_stmt_list (stmt));
36819 return objc_finish_try_stmt ();
36822 /* Parse an Objective-C synchronized statement.
36824 objc-synchronized-stmt:
36825 @synchronized ( expression ) compound-statement
36827 Returns NULL_TREE. */
36829 static tree
36830 cp_parser_objc_synchronized_statement (cp_parser *parser)
36832 location_t location;
36833 tree lock, stmt;
36835 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
36837 location = cp_lexer_peek_token (parser->lexer)->location;
36838 objc_maybe_warn_exceptions (location);
36839 matching_parens parens;
36840 parens.require_open (parser);
36841 lock = cp_parser_expression (parser);
36842 parens.require_close (parser);
36844 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
36845 node, lest it get absorbed into the surrounding block. */
36846 stmt = push_stmt_list ();
36847 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
36849 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
36852 /* Parse an Objective-C throw statement.
36854 objc-throw-stmt:
36855 @throw assignment-expression [opt] ;
36857 Returns a constructed '@throw' statement. */
36859 static tree
36860 cp_parser_objc_throw_statement (cp_parser *parser)
36862 tree expr = NULL_TREE;
36863 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36865 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
36867 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
36868 expr = cp_parser_expression (parser);
36870 cp_parser_consume_semicolon_at_end_of_statement (parser);
36872 return objc_build_throw_stmt (loc, expr);
36875 /* Parse an Objective-C statement. */
36877 static tree
36878 cp_parser_objc_statement (cp_parser * parser)
36880 /* Try to figure out what kind of declaration is present. */
36881 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
36883 switch (kwd->keyword)
36885 case RID_AT_TRY:
36886 return cp_parser_objc_try_catch_finally_statement (parser);
36887 case RID_AT_SYNCHRONIZED:
36888 return cp_parser_objc_synchronized_statement (parser);
36889 case RID_AT_THROW:
36890 return cp_parser_objc_throw_statement (parser);
36891 default:
36892 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
36893 kwd->u.value);
36894 cp_parser_skip_to_end_of_block_or_statement (parser);
36897 return error_mark_node;
36900 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
36901 look ahead to see if an objc keyword follows the attributes. This
36902 is to detect the use of prefix attributes on ObjC @interface and
36903 @protocol. */
36905 static bool
36906 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
36908 cp_lexer_save_tokens (parser->lexer);
36909 tree addon = cp_parser_attributes_opt (parser);
36910 if (addon
36911 && OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
36913 cp_lexer_commit_tokens (parser->lexer);
36914 if (*attrib)
36915 TREE_CHAIN (*attrib) = addon;
36916 else
36917 *attrib = addon;
36918 return true;
36920 cp_lexer_rollback_tokens (parser->lexer);
36921 return false;
36924 /* This routine is a minimal replacement for
36925 c_parser_struct_declaration () used when parsing the list of
36926 types/names or ObjC++ properties. For example, when parsing the
36927 code
36929 @property (readonly) int a, b, c;
36931 this function is responsible for parsing "int a, int b, int c" and
36932 returning the declarations as CHAIN of DECLs.
36934 TODO: Share this code with cp_parser_objc_class_ivars. It's very
36935 similar parsing. */
36936 static tree
36937 cp_parser_objc_struct_declaration (cp_parser *parser)
36939 tree decls = NULL_TREE;
36940 cp_decl_specifier_seq declspecs;
36941 int decl_class_or_enum_p;
36942 tree prefix_attributes;
36944 cp_parser_decl_specifier_seq (parser,
36945 CP_PARSER_FLAGS_NONE,
36946 &declspecs,
36947 &decl_class_or_enum_p);
36949 if (declspecs.type == error_mark_node)
36950 return error_mark_node;
36952 /* auto, register, static, extern, mutable. */
36953 if (declspecs.storage_class != sc_none)
36955 cp_parser_error (parser, "invalid type for property");
36956 declspecs.storage_class = sc_none;
36959 /* thread_local. */
36960 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
36962 cp_parser_error (parser, "invalid type for property");
36963 declspecs.locations[ds_thread] = 0;
36966 /* typedef. */
36967 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
36969 cp_parser_error (parser, "invalid type for property");
36970 declspecs.locations[ds_typedef] = 0;
36973 prefix_attributes = declspecs.attributes;
36974 declspecs.attributes = NULL_TREE;
36976 /* Keep going until we hit the `;' at the end of the declaration. */
36977 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
36979 tree attributes, first_attribute, decl;
36980 cp_declarator *declarator;
36981 cp_token *token;
36983 /* Parse the declarator. */
36984 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
36985 CP_PARSER_FLAGS_NONE,
36986 NULL, NULL, false, false, false);
36988 /* Look for attributes that apply to the ivar. */
36989 attributes = cp_parser_attributes_opt (parser);
36990 /* Remember which attributes are prefix attributes and
36991 which are not. */
36992 first_attribute = attributes;
36993 /* Combine the attributes. */
36994 attributes = attr_chainon (prefix_attributes, attributes);
36996 decl = grokfield (declarator, &declspecs,
36997 NULL_TREE, /*init_const_expr_p=*/false,
36998 NULL_TREE, attributes);
37000 if (decl == error_mark_node || decl == NULL_TREE)
37001 return error_mark_node;
37003 /* Reset PREFIX_ATTRIBUTES. */
37004 if (attributes != error_mark_node)
37006 while (attributes && TREE_CHAIN (attributes) != first_attribute)
37007 attributes = TREE_CHAIN (attributes);
37008 if (attributes)
37009 TREE_CHAIN (attributes) = NULL_TREE;
37012 DECL_CHAIN (decl) = decls;
37013 decls = decl;
37015 token = cp_lexer_peek_token (parser->lexer);
37016 if (token->type == CPP_COMMA)
37018 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
37019 continue;
37021 else
37022 break;
37024 return decls;
37027 /* Parse an Objective-C @property declaration. The syntax is:
37029 objc-property-declaration:
37030 '@property' objc-property-attributes[opt] struct-declaration ;
37032 objc-property-attributes:
37033 '(' objc-property-attribute-list ')'
37035 objc-property-attribute-list:
37036 objc-property-attribute
37037 objc-property-attribute-list, objc-property-attribute
37039 objc-property-attribute
37040 'getter' = identifier
37041 'setter' = identifier
37042 'readonly'
37043 'readwrite'
37044 'assign'
37045 'retain'
37046 'copy'
37047 'nonatomic'
37049 For example:
37050 @property NSString *name;
37051 @property (readonly) id object;
37052 @property (retain, nonatomic, getter=getTheName) id name;
37053 @property int a, b, c;
37055 PS: This function is identical to
37056 c_parser_objc_at_property_declaration for C. Keep them in sync. */
37057 static void
37058 cp_parser_objc_at_property_declaration (cp_parser *parser)
37060 /* Parse the optional attribute list.
37062 A list of parsed, but not verified, attributes. */
37063 auto_delete_vec<property_attribute_info> prop_attr_list;
37064 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37066 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
37068 /* Parse the optional attribute list... */
37069 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37071 /* Eat the '('. */
37072 matching_parens parens;
37073 location_t attr_start = cp_lexer_peek_token (parser->lexer)->location;
37074 parens.consume_open (parser);
37075 bool syntax_error = false;
37077 /* Allow empty @property attribute lists, but with a warning. */
37078 location_t attr_end = cp_lexer_peek_token (parser->lexer)->location;
37079 location_t attr_comb;
37080 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
37082 attr_comb = make_location (attr_end, attr_start, attr_end);
37083 warning_at (attr_comb, OPT_Wattributes,
37084 "empty property attribute list");
37086 else
37087 while (true)
37089 cp_token *token = cp_lexer_peek_token (parser->lexer);
37090 attr_start = token->location;
37091 attr_end = get_finish (token->location);
37092 attr_comb = make_location (attr_start, attr_start, attr_end);
37094 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_COMMA)
37096 warning_at (attr_comb, OPT_Wattributes,
37097 "missing property attribute");
37098 if (token->type == CPP_CLOSE_PAREN)
37099 break;
37100 cp_lexer_consume_token (parser->lexer);
37101 continue;
37104 tree attr_name = NULL_TREE;
37105 if (identifier_p (token->u.value))
37106 attr_name = token->u.value;
37108 enum rid keyword;
37109 if (token->type == CPP_NAME)
37110 keyword = C_RID_CODE (token->u.value);
37111 else if (token->type == CPP_KEYWORD
37112 && token->keyword == RID_CLASS)
37113 /* Account for accepting the 'class' keyword in this context. */
37114 keyword = RID_CLASS;
37115 else
37116 keyword = RID_MAX; /* By definition, an unknown property. */
37117 cp_lexer_consume_token (parser->lexer);
37119 enum objc_property_attribute_kind prop_kind
37120 = objc_prop_attr_kind_for_rid (keyword);
37121 property_attribute_info *prop
37122 = new property_attribute_info (attr_name, attr_comb, prop_kind);
37123 prop_attr_list.safe_push (prop);
37125 tree meth_name;
37126 switch (prop->prop_kind)
37128 default: break;
37129 case OBJC_PROPERTY_ATTR_UNKNOWN:
37130 if (attr_name)
37131 error_at (attr_start, "unknown property attribute %qE",
37132 attr_name);
37133 else
37134 error_at (attr_start, "unknown property attribute");
37135 prop->parse_error = syntax_error = true;
37136 break;
37138 case OBJC_PROPERTY_ATTR_GETTER:
37139 case OBJC_PROPERTY_ATTR_SETTER:
37140 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
37142 attr_comb = make_location (attr_end, attr_start, attr_end);
37143 error_at (attr_comb, "expected %<=%> after Objective-C %qE",
37144 attr_name);
37145 prop->parse_error = syntax_error = true;
37146 break;
37149 token = cp_lexer_peek_token (parser->lexer);
37150 attr_end = token->location;
37151 cp_lexer_consume_token (parser->lexer); /* eat the = */
37153 if (!cp_parser_objc_selector_p
37154 (cp_lexer_peek_token (parser->lexer)->type))
37156 attr_comb = make_location (attr_end, attr_start, attr_end);
37157 error_at (attr_comb, "expected %qE selector name",
37158 attr_name);
37159 prop->parse_error = syntax_error = true;
37160 break;
37163 /* Get the end of the method name, and consume the name. */
37164 token = cp_lexer_peek_token (parser->lexer);
37165 attr_end = get_finish (token->location);
37166 /* Because method names may contain C++ keywords, we have a
37167 routine to fetch them (this also consumes the token). */
37168 meth_name = cp_parser_objc_selector (parser);
37170 if (prop->prop_kind == OBJC_PROPERTY_ATTR_SETTER)
37172 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
37174 attr_comb = make_location (attr_end, attr_start,
37175 attr_end);
37176 error_at (attr_comb, "setter method names must"
37177 " terminate with %<:%>");
37178 prop->parse_error = syntax_error = true;
37180 else
37182 attr_end = get_finish (cp_lexer_peek_token
37183 (parser->lexer)->location);
37184 cp_lexer_consume_token (parser->lexer);
37186 attr_comb = make_location (attr_start, attr_start,
37187 attr_end);
37189 else
37190 attr_comb = make_location (attr_start, attr_start,
37191 attr_end);
37192 prop->ident = meth_name;
37193 /* Updated location including all that was successfully
37194 parsed. */
37195 prop->prop_loc = attr_comb;
37196 break;
37199 /* If we see a comma here, then keep going - even if we already
37200 saw a syntax error. For simple mistakes e.g. (asign, getter=x)
37201 this makes a more useful output and avoid spurious warnings
37202 about missing attributes that are, in fact, specified after the
37203 one with the syntax error. */
37204 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37205 cp_lexer_consume_token (parser->lexer);
37206 else
37207 break;
37210 if (syntax_error || !parens.require_close (parser))
37211 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37212 /*or_comma=*/false,
37213 /*consume_paren=*/true);
37216 /* 'properties' is the list of properties that we read. Usually a
37217 single one, but maybe more (eg, in "@property int a, b, c;" there
37218 are three).
37219 TODO: Update this parsing so that it accepts (erroneous) bitfields so
37220 that we can issue a meaningful and consistent (between C/C++) error
37221 message from objc_add_property_declaration (). */
37222 tree properties = cp_parser_objc_struct_declaration (parser);
37224 if (properties == error_mark_node)
37225 cp_parser_skip_to_end_of_statement (parser);
37226 else if (properties == NULL_TREE)
37227 cp_parser_error (parser, "expected identifier");
37228 else
37230 /* Comma-separated properties are chained together in reverse order;
37231 add them one by one. */
37232 properties = nreverse (properties);
37233 for (; properties; properties = TREE_CHAIN (properties))
37234 objc_add_property_declaration (loc, copy_node (properties),
37235 prop_attr_list);
37238 cp_parser_consume_semicolon_at_end_of_statement (parser);
37241 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
37243 objc-synthesize-declaration:
37244 @synthesize objc-synthesize-identifier-list ;
37246 objc-synthesize-identifier-list:
37247 objc-synthesize-identifier
37248 objc-synthesize-identifier-list, objc-synthesize-identifier
37250 objc-synthesize-identifier
37251 identifier
37252 identifier = identifier
37254 For example:
37255 @synthesize MyProperty;
37256 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
37258 PS: This function is identical to c_parser_objc_at_synthesize_declaration
37259 for C. Keep them in sync.
37261 static void
37262 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
37264 tree list = NULL_TREE;
37265 location_t loc;
37266 loc = cp_lexer_peek_token (parser->lexer)->location;
37268 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
37269 while (true)
37271 tree property, ivar;
37272 property = cp_parser_identifier (parser);
37273 if (property == error_mark_node)
37275 cp_parser_consume_semicolon_at_end_of_statement (parser);
37276 return;
37278 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
37280 cp_lexer_consume_token (parser->lexer);
37281 ivar = cp_parser_identifier (parser);
37282 if (ivar == error_mark_node)
37284 cp_parser_consume_semicolon_at_end_of_statement (parser);
37285 return;
37288 else
37289 ivar = NULL_TREE;
37290 list = chainon (list, build_tree_list (ivar, property));
37291 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37292 cp_lexer_consume_token (parser->lexer);
37293 else
37294 break;
37296 cp_parser_consume_semicolon_at_end_of_statement (parser);
37297 objc_add_synthesize_declaration (loc, list);
37300 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
37302 objc-dynamic-declaration:
37303 @dynamic identifier-list ;
37305 For example:
37306 @dynamic MyProperty;
37307 @dynamic MyProperty, AnotherProperty;
37309 PS: This function is identical to c_parser_objc_at_dynamic_declaration
37310 for C. Keep them in sync.
37312 static void
37313 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
37315 tree list = NULL_TREE;
37316 location_t loc;
37317 loc = cp_lexer_peek_token (parser->lexer)->location;
37319 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
37320 while (true)
37322 tree property;
37323 property = cp_parser_identifier (parser);
37324 if (property == error_mark_node)
37326 cp_parser_consume_semicolon_at_end_of_statement (parser);
37327 return;
37329 list = chainon (list, build_tree_list (NULL, property));
37330 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37331 cp_lexer_consume_token (parser->lexer);
37332 else
37333 break;
37335 cp_parser_consume_semicolon_at_end_of_statement (parser);
37336 objc_add_dynamic_declaration (loc, list);
37340 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 / 4.5 / 5.0 parsing routines. */
37342 /* Returns name of the next clause.
37343 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
37344 the token is not consumed. Otherwise appropriate pragma_omp_clause is
37345 returned and the token is consumed. */
37347 static pragma_omp_clause
37348 cp_parser_omp_clause_name (cp_parser *parser)
37350 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
37352 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
37353 result = PRAGMA_OACC_CLAUSE_AUTO;
37354 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
37355 result = PRAGMA_OMP_CLAUSE_IF;
37356 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
37357 result = PRAGMA_OMP_CLAUSE_DEFAULT;
37358 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
37359 result = PRAGMA_OACC_CLAUSE_DELETE;
37360 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
37361 result = PRAGMA_OMP_CLAUSE_PRIVATE;
37362 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
37363 result = PRAGMA_OMP_CLAUSE_FOR;
37364 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37366 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37367 const char *p = IDENTIFIER_POINTER (id);
37369 switch (p[0])
37371 case 'a':
37372 if (!strcmp ("affinity", p))
37373 result = PRAGMA_OMP_CLAUSE_AFFINITY;
37374 else if (!strcmp ("aligned", p))
37375 result = PRAGMA_OMP_CLAUSE_ALIGNED;
37376 else if (!strcmp ("allocate", p))
37377 result = PRAGMA_OMP_CLAUSE_ALLOCATE;
37378 else if (!strcmp ("async", p))
37379 result = PRAGMA_OACC_CLAUSE_ASYNC;
37380 else if (!strcmp ("attach", p))
37381 result = PRAGMA_OACC_CLAUSE_ATTACH;
37382 break;
37383 case 'b':
37384 if (!strcmp ("bind", p))
37385 result = PRAGMA_OMP_CLAUSE_BIND;
37386 break;
37387 case 'c':
37388 if (!strcmp ("collapse", p))
37389 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
37390 else if (!strcmp ("copy", p))
37391 result = PRAGMA_OACC_CLAUSE_COPY;
37392 else if (!strcmp ("copyin", p))
37393 result = PRAGMA_OMP_CLAUSE_COPYIN;
37394 else if (!strcmp ("copyout", p))
37395 result = PRAGMA_OACC_CLAUSE_COPYOUT;
37396 else if (!strcmp ("copyprivate", p))
37397 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
37398 else if (!strcmp ("create", p))
37399 result = PRAGMA_OACC_CLAUSE_CREATE;
37400 break;
37401 case 'd':
37402 if (!strcmp ("defaultmap", p))
37403 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
37404 else if (!strcmp ("depend", p))
37405 result = PRAGMA_OMP_CLAUSE_DEPEND;
37406 else if (!strcmp ("detach", p))
37407 result = PRAGMA_OACC_CLAUSE_DETACH;
37408 else if (!strcmp ("device", p))
37409 result = PRAGMA_OMP_CLAUSE_DEVICE;
37410 else if (!strcmp ("deviceptr", p))
37411 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
37412 else if (!strcmp ("device_resident", p))
37413 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
37414 else if (!strcmp ("device_type", p))
37415 result = PRAGMA_OMP_CLAUSE_DEVICE_TYPE;
37416 else if (!strcmp ("dist_schedule", p))
37417 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
37418 else if (!strcmp ("doacross", p))
37419 result = PRAGMA_OMP_CLAUSE_DOACROSS;
37420 break;
37421 case 'e':
37422 if (!strcmp ("enter", p))
37423 result = PRAGMA_OMP_CLAUSE_ENTER;
37424 break;
37425 case 'f':
37426 if (!strcmp ("filter", p))
37427 result = PRAGMA_OMP_CLAUSE_FILTER;
37428 else if (!strcmp ("final", p))
37429 result = PRAGMA_OMP_CLAUSE_FINAL;
37430 else if (!strcmp ("finalize", p))
37431 result = PRAGMA_OACC_CLAUSE_FINALIZE;
37432 else if (!strcmp ("firstprivate", p))
37433 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
37434 else if (!strcmp ("from", p))
37435 result = PRAGMA_OMP_CLAUSE_FROM;
37436 break;
37437 case 'g':
37438 if (!strcmp ("gang", p))
37439 result = PRAGMA_OACC_CLAUSE_GANG;
37440 else if (!strcmp ("grainsize", p))
37441 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
37442 break;
37443 case 'h':
37444 if (!strcmp ("has_device_addr", p))
37445 result = PRAGMA_OMP_CLAUSE_HAS_DEVICE_ADDR;
37446 else if (!strcmp ("hint", p))
37447 result = PRAGMA_OMP_CLAUSE_HINT;
37448 else if (!strcmp ("host", p))
37449 result = PRAGMA_OACC_CLAUSE_HOST;
37450 break;
37451 case 'i':
37452 if (!strcmp ("if_present", p))
37453 result = PRAGMA_OACC_CLAUSE_IF_PRESENT;
37454 else if (!strcmp ("in_reduction", p))
37455 result = PRAGMA_OMP_CLAUSE_IN_REDUCTION;
37456 else if (!strcmp ("inbranch", p))
37457 result = PRAGMA_OMP_CLAUSE_INBRANCH;
37458 else if (!strcmp ("independent", p))
37459 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
37460 else if (!strcmp ("is_device_ptr", p))
37461 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
37462 break;
37463 case 'l':
37464 if (!strcmp ("lastprivate", p))
37465 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
37466 else if (!strcmp ("linear", p))
37467 result = PRAGMA_OMP_CLAUSE_LINEAR;
37468 else if (!strcmp ("link", p))
37469 result = PRAGMA_OMP_CLAUSE_LINK;
37470 break;
37471 case 'm':
37472 if (!strcmp ("map", p))
37473 result = PRAGMA_OMP_CLAUSE_MAP;
37474 else if (!strcmp ("mergeable", p))
37475 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
37476 break;
37477 case 'n':
37478 if (!strcmp ("no_create", p))
37479 result = PRAGMA_OACC_CLAUSE_NO_CREATE;
37480 else if (!strcmp ("nogroup", p))
37481 result = PRAGMA_OMP_CLAUSE_NOGROUP;
37482 else if (!strcmp ("nohost", p))
37483 result = PRAGMA_OACC_CLAUSE_NOHOST;
37484 else if (!strcmp ("nontemporal", p))
37485 result = PRAGMA_OMP_CLAUSE_NONTEMPORAL;
37486 else if (!strcmp ("notinbranch", p))
37487 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
37488 else if (!strcmp ("nowait", p))
37489 result = PRAGMA_OMP_CLAUSE_NOWAIT;
37490 else if (!strcmp ("num_gangs", p))
37491 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
37492 else if (!strcmp ("num_tasks", p))
37493 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
37494 else if (!strcmp ("num_teams", p))
37495 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
37496 else if (!strcmp ("num_threads", p))
37497 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
37498 else if (!strcmp ("num_workers", p))
37499 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
37500 break;
37501 case 'o':
37502 if (!strcmp ("ordered", p))
37503 result = PRAGMA_OMP_CLAUSE_ORDERED;
37504 else if (!strcmp ("order", p))
37505 result = PRAGMA_OMP_CLAUSE_ORDER;
37506 break;
37507 case 'p':
37508 if (!strcmp ("parallel", p))
37509 result = PRAGMA_OMP_CLAUSE_PARALLEL;
37510 else if (!strcmp ("present", p))
37511 result = PRAGMA_OACC_CLAUSE_PRESENT;
37512 else if (!strcmp ("present_or_copy", p)
37513 || !strcmp ("pcopy", p))
37514 result = PRAGMA_OACC_CLAUSE_COPY;
37515 else if (!strcmp ("present_or_copyin", p)
37516 || !strcmp ("pcopyin", p))
37517 result = PRAGMA_OACC_CLAUSE_COPYIN;
37518 else if (!strcmp ("present_or_copyout", p)
37519 || !strcmp ("pcopyout", p))
37520 result = PRAGMA_OACC_CLAUSE_COPYOUT;
37521 else if (!strcmp ("present_or_create", p)
37522 || !strcmp ("pcreate", p))
37523 result = PRAGMA_OACC_CLAUSE_CREATE;
37524 else if (!strcmp ("priority", p))
37525 result = PRAGMA_OMP_CLAUSE_PRIORITY;
37526 else if (!strcmp ("proc_bind", p))
37527 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
37528 break;
37529 case 'r':
37530 if (!strcmp ("reduction", p))
37531 result = PRAGMA_OMP_CLAUSE_REDUCTION;
37532 break;
37533 case 's':
37534 if (!strcmp ("safelen", p))
37535 result = PRAGMA_OMP_CLAUSE_SAFELEN;
37536 else if (!strcmp ("schedule", p))
37537 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
37538 else if (!strcmp ("sections", p))
37539 result = PRAGMA_OMP_CLAUSE_SECTIONS;
37540 else if (!strcmp ("self", p)) /* "self" is a synonym for "host". */
37541 result = PRAGMA_OACC_CLAUSE_HOST;
37542 else if (!strcmp ("seq", p))
37543 result = PRAGMA_OACC_CLAUSE_SEQ;
37544 else if (!strcmp ("shared", p))
37545 result = PRAGMA_OMP_CLAUSE_SHARED;
37546 else if (!strcmp ("simd", p))
37547 result = PRAGMA_OMP_CLAUSE_SIMD;
37548 else if (!strcmp ("simdlen", p))
37549 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
37550 break;
37551 case 't':
37552 if (!strcmp ("task_reduction", p))
37553 result = PRAGMA_OMP_CLAUSE_TASK_REDUCTION;
37554 else if (!strcmp ("taskgroup", p))
37555 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
37556 else if (!strcmp ("thread_limit", p))
37557 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
37558 else if (!strcmp ("threads", p))
37559 result = PRAGMA_OMP_CLAUSE_THREADS;
37560 else if (!strcmp ("tile", p))
37561 result = PRAGMA_OACC_CLAUSE_TILE;
37562 else if (!strcmp ("to", p))
37563 result = PRAGMA_OMP_CLAUSE_TO;
37564 break;
37565 case 'u':
37566 if (!strcmp ("uniform", p))
37567 result = PRAGMA_OMP_CLAUSE_UNIFORM;
37568 else if (!strcmp ("untied", p))
37569 result = PRAGMA_OMP_CLAUSE_UNTIED;
37570 else if (!strcmp ("use_device", p))
37571 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
37572 else if (!strcmp ("use_device_addr", p))
37573 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR;
37574 else if (!strcmp ("use_device_ptr", p))
37575 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
37576 break;
37577 case 'v':
37578 if (!strcmp ("vector", p))
37579 result = PRAGMA_OACC_CLAUSE_VECTOR;
37580 else if (!strcmp ("vector_length", p))
37581 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
37582 break;
37583 case 'w':
37584 if (!strcmp ("wait", p))
37585 result = PRAGMA_OACC_CLAUSE_WAIT;
37586 else if (!strcmp ("worker", p))
37587 result = PRAGMA_OACC_CLAUSE_WORKER;
37588 break;
37592 if (result != PRAGMA_OMP_CLAUSE_NONE)
37593 cp_lexer_consume_token (parser->lexer);
37595 return result;
37598 /* Validate that a clause of the given type does not already exist. */
37600 static void
37601 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
37602 const char *name, location_t location)
37604 if (omp_find_clause (clauses, code))
37605 error_at (location, "too many %qs clauses", name);
37608 /* OpenMP 2.5:
37609 variable-list:
37610 identifier
37611 variable-list , identifier
37613 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
37614 colon). An opening parenthesis will have been consumed by the caller.
37616 If KIND is nonzero, create the appropriate node and install the decl
37617 in OMP_CLAUSE_DECL and add the node to the head of the list.
37619 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
37620 return the list created.
37622 COLON can be NULL if only closing parenthesis should end the list,
37623 or pointer to bool which will receive false if the list is terminated
37624 by closing parenthesis or true if the list is terminated by colon.
37626 The optional ALLOW_DEREF argument is true if list items can use the deref
37627 (->) operator. */
37629 struct omp_dim
37631 tree low_bound, length;
37632 location_t loc;
37633 bool no_colon;
37634 omp_dim (tree lb, tree len, location_t lo, bool nc)
37635 : low_bound (lb), length (len), loc (lo), no_colon (nc) {}
37638 static tree
37639 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
37640 tree list, bool *colon,
37641 bool allow_deref = false)
37643 auto_vec<omp_dim> dims;
37644 bool array_section_p;
37645 cp_token *token;
37646 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37647 if (colon)
37649 parser->colon_corrects_to_scope_p = false;
37650 *colon = false;
37652 while (1)
37654 tree name, decl;
37656 if (kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
37657 cp_parser_parse_tentatively (parser);
37658 token = cp_lexer_peek_token (parser->lexer);
37659 if (kind != 0
37660 && cp_parser_is_keyword (token, RID_THIS))
37662 decl = finish_this_expr ();
37663 if (TREE_CODE (decl) == NON_LVALUE_EXPR
37664 || CONVERT_EXPR_P (decl))
37665 decl = TREE_OPERAND (decl, 0);
37666 cp_lexer_consume_token (parser->lexer);
37668 else if (cp_parser_is_keyword (token, RID_FUNCTION_NAME)
37669 || cp_parser_is_keyword (token, RID_PRETTY_FUNCTION_NAME)
37670 || cp_parser_is_keyword (token, RID_C99_FUNCTION_NAME))
37672 cp_id_kind idk;
37673 decl = cp_parser_primary_expression (parser, false, false, false,
37674 &idk);
37676 else if (kind == OMP_CLAUSE_DEPEND
37677 && cp_parser_is_keyword (token, RID_OMP_ALL_MEMORY)
37678 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
37679 || cp_lexer_nth_token_is (parser->lexer, 2,
37680 CPP_CLOSE_PAREN)))
37682 decl = ridpointers[RID_OMP_ALL_MEMORY];
37683 cp_lexer_consume_token (parser->lexer);
37685 else
37687 name = cp_parser_id_expression (parser, /*template_p=*/false,
37688 /*check_dependency_p=*/true,
37689 /*template_p=*/NULL,
37690 /*declarator_p=*/false,
37691 /*optional_p=*/false);
37692 if (name == error_mark_node)
37694 if ((kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
37695 && cp_parser_simulate_error (parser))
37696 goto depend_lvalue;
37697 goto skip_comma;
37700 if (identifier_p (name))
37701 decl = cp_parser_lookup_name_simple (parser, name, token->location);
37702 else
37703 decl = name;
37704 if (decl == error_mark_node)
37706 if ((kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
37707 && cp_parser_simulate_error (parser))
37708 goto depend_lvalue;
37709 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
37710 token->location);
37713 if (outer_automatic_var_p (decl))
37714 decl = process_outer_var_ref (decl, tf_warning_or_error);
37715 if (decl == error_mark_node)
37717 else if (kind != 0)
37719 switch (kind)
37721 case OMP_CLAUSE__CACHE_:
37722 /* The OpenACC cache directive explicitly only allows "array
37723 elements or subarrays". */
37724 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
37726 error_at (token->location, "expected %<[%>");
37727 decl = error_mark_node;
37728 break;
37730 /* FALLTHROUGH. */
37731 case OMP_CLAUSE_MAP:
37732 case OMP_CLAUSE_FROM:
37733 case OMP_CLAUSE_TO:
37734 start_component_ref:
37735 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT)
37736 || (allow_deref
37737 && cp_lexer_next_token_is (parser->lexer, CPP_DEREF)))
37739 cpp_ttype ttype
37740 = cp_lexer_next_token_is (parser->lexer, CPP_DOT)
37741 ? CPP_DOT : CPP_DEREF;
37742 location_t loc
37743 = cp_lexer_peek_token (parser->lexer)->location;
37744 cp_id_kind idk = CP_ID_KIND_NONE;
37745 cp_lexer_consume_token (parser->lexer);
37746 decl = convert_from_reference (decl);
37747 decl = (cp_parser_postfix_dot_deref_expression
37748 (parser, ttype, cp_expr (decl, token->location),
37749 false, &idk, loc));
37751 /* FALLTHROUGH. */
37752 case OMP_CLAUSE_AFFINITY:
37753 case OMP_CLAUSE_DEPEND:
37754 case OMP_CLAUSE_REDUCTION:
37755 case OMP_CLAUSE_IN_REDUCTION:
37756 case OMP_CLAUSE_TASK_REDUCTION:
37757 case OMP_CLAUSE_HAS_DEVICE_ADDR:
37758 array_section_p = false;
37759 dims.truncate (0);
37760 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
37762 location_t loc = UNKNOWN_LOCATION;
37763 tree low_bound = NULL_TREE, length = NULL_TREE;
37764 bool no_colon = false;
37766 parser->colon_corrects_to_scope_p = false;
37767 cp_lexer_consume_token (parser->lexer);
37768 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
37770 loc = cp_lexer_peek_token (parser->lexer)->location;
37771 low_bound = cp_parser_expression (parser);
37772 /* Later handling is not prepared to see through these. */
37773 gcc_checking_assert (!location_wrapper_p (low_bound));
37775 if (!colon)
37776 parser->colon_corrects_to_scope_p
37777 = saved_colon_corrects_to_scope_p;
37778 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
37780 length = integer_one_node;
37781 no_colon = true;
37783 else
37785 /* Look for `:'. */
37786 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37788 if ((kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
37789 && cp_parser_simulate_error (parser))
37790 goto depend_lvalue;
37791 goto skip_comma;
37793 if (kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
37794 cp_parser_commit_to_tentative_parse (parser);
37795 else
37796 array_section_p = true;
37797 if (!cp_lexer_next_token_is (parser->lexer,
37798 CPP_CLOSE_SQUARE))
37800 length = cp_parser_expression (parser);
37801 /* Later handling is not prepared to see through these. */
37802 gcc_checking_assert (!location_wrapper_p (length));
37805 /* Look for the closing `]'. */
37806 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
37807 RT_CLOSE_SQUARE))
37809 if ((kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
37810 && cp_parser_simulate_error (parser))
37811 goto depend_lvalue;
37812 goto skip_comma;
37815 dims.safe_push (omp_dim (low_bound, length, loc, no_colon));
37818 if ((kind == OMP_CLAUSE_MAP
37819 || kind == OMP_CLAUSE_FROM
37820 || kind == OMP_CLAUSE_TO)
37821 && !array_section_p
37822 && (cp_lexer_next_token_is (parser->lexer, CPP_DOT)
37823 || (allow_deref
37824 && cp_lexer_next_token_is (parser->lexer,
37825 CPP_DEREF))))
37827 for (unsigned i = 0; i < dims.length (); i++)
37829 gcc_assert (dims[i].length == integer_one_node);
37830 decl = build_array_ref (dims[i].loc,
37831 decl, dims[i].low_bound);
37833 goto start_component_ref;
37835 else
37836 for (unsigned i = 0; i < dims.length (); i++)
37837 decl = tree_cons (dims[i].low_bound, dims[i].length, decl);
37839 break;
37840 default:
37841 break;
37844 if (kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
37846 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
37847 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
37848 && cp_parser_simulate_error (parser))
37850 depend_lvalue:
37851 cp_parser_abort_tentative_parse (parser);
37852 decl = cp_parser_assignment_expression (parser, NULL,
37853 false, false);
37855 else
37856 cp_parser_parse_definitely (parser);
37859 tree u = build_omp_clause (token->location, kind);
37860 OMP_CLAUSE_DECL (u) = decl;
37861 OMP_CLAUSE_CHAIN (u) = list;
37862 list = u;
37864 else
37865 list = tree_cons (decl, NULL_TREE, list);
37867 get_comma:
37868 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
37869 break;
37870 cp_lexer_consume_token (parser->lexer);
37873 if (colon)
37874 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37876 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
37878 *colon = true;
37879 cp_parser_require (parser, CPP_COLON, RT_COLON);
37880 return list;
37883 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37885 int ending;
37887 /* Try to resync to an unnested comma. Copied from
37888 cp_parser_parenthesized_expression_list. */
37889 skip_comma:
37890 if (colon)
37891 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37892 ending = cp_parser_skip_to_closing_parenthesis (parser,
37893 /*recovering=*/true,
37894 /*or_comma=*/true,
37895 /*consume_paren=*/true);
37896 if (ending < 0)
37897 goto get_comma;
37900 return list;
37903 /* Similarly, but expect leading and trailing parenthesis. This is a very
37904 common case for omp clauses. */
37906 static tree
37907 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list,
37908 bool allow_deref = false)
37910 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37911 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL,
37912 allow_deref);
37913 return list;
37916 /* OpenACC 2.0:
37917 copy ( variable-list )
37918 copyin ( variable-list )
37919 copyout ( variable-list )
37920 create ( variable-list )
37921 delete ( variable-list )
37922 present ( variable-list )
37924 OpenACC 2.6:
37925 no_create ( variable-list )
37926 attach ( variable-list )
37927 detach ( variable-list ) */
37929 static tree
37930 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
37931 tree list)
37933 enum gomp_map_kind kind;
37934 switch (c_kind)
37936 case PRAGMA_OACC_CLAUSE_ATTACH:
37937 kind = GOMP_MAP_ATTACH;
37938 break;
37939 case PRAGMA_OACC_CLAUSE_COPY:
37940 kind = GOMP_MAP_TOFROM;
37941 break;
37942 case PRAGMA_OACC_CLAUSE_COPYIN:
37943 kind = GOMP_MAP_TO;
37944 break;
37945 case PRAGMA_OACC_CLAUSE_COPYOUT:
37946 kind = GOMP_MAP_FROM;
37947 break;
37948 case PRAGMA_OACC_CLAUSE_CREATE:
37949 kind = GOMP_MAP_ALLOC;
37950 break;
37951 case PRAGMA_OACC_CLAUSE_DELETE:
37952 kind = GOMP_MAP_RELEASE;
37953 break;
37954 case PRAGMA_OACC_CLAUSE_DETACH:
37955 kind = GOMP_MAP_DETACH;
37956 break;
37957 case PRAGMA_OACC_CLAUSE_DEVICE:
37958 kind = GOMP_MAP_FORCE_TO;
37959 break;
37960 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
37961 kind = GOMP_MAP_DEVICE_RESIDENT;
37962 break;
37963 case PRAGMA_OACC_CLAUSE_HOST:
37964 kind = GOMP_MAP_FORCE_FROM;
37965 break;
37966 case PRAGMA_OACC_CLAUSE_LINK:
37967 kind = GOMP_MAP_LINK;
37968 break;
37969 case PRAGMA_OACC_CLAUSE_NO_CREATE:
37970 kind = GOMP_MAP_IF_PRESENT;
37971 break;
37972 case PRAGMA_OACC_CLAUSE_PRESENT:
37973 kind = GOMP_MAP_FORCE_PRESENT;
37974 break;
37975 default:
37976 gcc_unreachable ();
37978 tree nl, c;
37979 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list, true);
37981 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
37982 OMP_CLAUSE_SET_MAP_KIND (c, kind);
37984 return nl;
37987 /* OpenACC 2.0:
37988 deviceptr ( variable-list ) */
37990 static tree
37991 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
37993 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37994 tree vars, t;
37996 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
37997 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
37998 variable-list must only allow for pointer variables. */
37999 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
38000 for (t = vars; t; t = TREE_CHAIN (t))
38002 tree v = TREE_PURPOSE (t);
38003 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
38004 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
38005 OMP_CLAUSE_DECL (u) = v;
38006 OMP_CLAUSE_CHAIN (u) = list;
38007 list = u;
38010 return list;
38013 /* OpenACC 2.5:
38014 auto
38015 finalize
38016 independent
38017 nohost
38018 seq */
38020 static tree
38021 cp_parser_oacc_simple_clause (location_t loc, enum omp_clause_code code,
38022 tree list)
38024 check_no_duplicate_clause (list, code, omp_clause_code_name[code], loc);
38026 tree c = build_omp_clause (loc, code);
38027 OMP_CLAUSE_CHAIN (c) = list;
38029 return c;
38032 /* OpenACC:
38033 num_gangs ( expression )
38034 num_workers ( expression )
38035 vector_length ( expression ) */
38037 static tree
38038 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
38039 const char *str, tree list)
38041 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38043 matching_parens parens;
38044 if (!parens.require_open (parser))
38045 return list;
38047 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
38049 if (t == error_mark_node
38050 || !parens.require_close (parser))
38052 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38053 /*or_comma=*/false,
38054 /*consume_paren=*/true);
38055 return list;
38058 check_no_duplicate_clause (list, code, str, loc);
38060 tree c = build_omp_clause (loc, code);
38061 OMP_CLAUSE_OPERAND (c, 0) = t;
38062 OMP_CLAUSE_CHAIN (c) = list;
38063 return c;
38066 /* OpenACC:
38068 gang [( gang-arg-list )]
38069 worker [( [num:] int-expr )]
38070 vector [( [length:] int-expr )]
38072 where gang-arg is one of:
38074 [num:] int-expr
38075 static: size-expr
38077 and size-expr may be:
38080 int-expr
38083 static tree
38084 cp_parser_oacc_shape_clause (cp_parser *parser, location_t loc,
38085 omp_clause_code kind,
38086 const char *str, tree list)
38088 const char *id = "num";
38089 cp_lexer *lexer = parser->lexer;
38090 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
38092 if (kind == OMP_CLAUSE_VECTOR)
38093 id = "length";
38095 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
38097 matching_parens parens;
38098 parens.consume_open (parser);
38102 cp_token *next = cp_lexer_peek_token (lexer);
38103 int idx = 0;
38105 /* Gang static argument. */
38106 if (kind == OMP_CLAUSE_GANG
38107 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
38109 cp_lexer_consume_token (lexer);
38111 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
38112 goto cleanup_error;
38114 idx = 1;
38115 if (ops[idx] != NULL)
38117 cp_parser_error (parser, "too many %<static%> arguments");
38118 goto cleanup_error;
38121 /* Check for the '*' argument. */
38122 if (cp_lexer_next_token_is (lexer, CPP_MULT)
38123 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
38124 || cp_lexer_nth_token_is (parser->lexer, 2,
38125 CPP_CLOSE_PAREN)))
38127 cp_lexer_consume_token (lexer);
38128 ops[idx] = integer_minus_one_node;
38130 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
38132 cp_lexer_consume_token (lexer);
38133 continue;
38135 else break;
38138 /* Worker num: argument and vector length: arguments. */
38139 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
38140 && id_equal (next->u.value, id)
38141 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
38143 cp_lexer_consume_token (lexer); /* id */
38144 cp_lexer_consume_token (lexer); /* ':' */
38147 /* Now collect the actual argument. */
38148 if (ops[idx] != NULL_TREE)
38150 cp_parser_error (parser, "unexpected argument");
38151 goto cleanup_error;
38154 tree expr = cp_parser_assignment_expression (parser, NULL, false,
38155 false);
38156 if (expr == error_mark_node)
38157 goto cleanup_error;
38159 mark_exp_read (expr);
38160 ops[idx] = expr;
38162 if (kind == OMP_CLAUSE_GANG
38163 && cp_lexer_next_token_is (lexer, CPP_COMMA))
38165 cp_lexer_consume_token (lexer);
38166 continue;
38168 break;
38170 while (1);
38172 if (!parens.require_close (parser))
38173 goto cleanup_error;
38176 check_no_duplicate_clause (list, kind, str, loc);
38178 c = build_omp_clause (loc, kind);
38180 if (ops[1])
38181 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
38183 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
38184 OMP_CLAUSE_CHAIN (c) = list;
38186 return c;
38188 cleanup_error:
38189 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
38190 return list;
38193 /* OpenACC 2.0:
38194 tile ( size-expr-list ) */
38196 static tree
38197 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
38199 tree c, expr = error_mark_node;
38200 tree tile = NULL_TREE;
38202 /* Collapse and tile are mutually exclusive. (The spec doesn't say
38203 so, but the spec authors never considered such a case and have
38204 differing opinions on what it might mean, including 'not
38205 allowed'.) */
38206 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
38207 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
38208 clause_loc);
38210 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38211 return list;
38215 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
38216 return list;
38218 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
38219 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
38220 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
38222 cp_lexer_consume_token (parser->lexer);
38223 expr = integer_zero_node;
38225 else
38226 expr = cp_parser_constant_expression (parser);
38228 tile = tree_cons (NULL_TREE, expr, tile);
38230 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
38232 /* Consume the trailing ')'. */
38233 cp_lexer_consume_token (parser->lexer);
38235 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
38236 tile = nreverse (tile);
38237 OMP_CLAUSE_TILE_LIST (c) = tile;
38238 OMP_CLAUSE_CHAIN (c) = list;
38239 return c;
38242 /* OpenACC 2.0
38243 Parse wait clause or directive parameters. */
38245 static tree
38246 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
38248 vec<tree, va_gc> *args;
38249 tree t, args_tree;
38251 args = cp_parser_parenthesized_expression_list (parser, non_attr,
38252 /*cast_p=*/false,
38253 /*allow_expansion_p=*/true,
38254 /*non_constant_p=*/NULL);
38256 if (args == NULL || args->length () == 0)
38258 if (args != NULL)
38260 cp_parser_error (parser, "expected integer expression list");
38261 release_tree_vector (args);
38263 return list;
38266 args_tree = build_tree_list_vec (args);
38268 release_tree_vector (args);
38270 for (t = args_tree; t; t = TREE_CHAIN (t))
38272 tree targ = TREE_VALUE (t);
38274 if (targ != error_mark_node)
38276 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
38277 error ("%<wait%> expression must be integral");
38278 else
38280 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
38282 targ = mark_rvalue_use (targ);
38283 OMP_CLAUSE_DECL (c) = targ;
38284 OMP_CLAUSE_CHAIN (c) = list;
38285 list = c;
38290 return list;
38293 /* OpenACC:
38294 wait [( int-expr-list )] */
38296 static tree
38297 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
38299 location_t location = cp_lexer_peek_token (parser->lexer)->location;
38301 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
38302 list = cp_parser_oacc_wait_list (parser, location, list);
38303 else
38305 tree c = build_omp_clause (location, OMP_CLAUSE_WAIT);
38307 OMP_CLAUSE_DECL (c) = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
38308 OMP_CLAUSE_CHAIN (c) = list;
38309 list = c;
38312 return list;
38315 /* OpenMP 3.0:
38316 collapse ( constant-expression ) */
38318 static tree
38319 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
38321 tree c, num;
38322 location_t loc;
38323 HOST_WIDE_INT n;
38325 loc = cp_lexer_peek_token (parser->lexer)->location;
38326 matching_parens parens;
38327 if (!parens.require_open (parser))
38328 return list;
38330 num = cp_parser_constant_expression (parser);
38332 if (!parens.require_close (parser))
38333 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38334 /*or_comma=*/false,
38335 /*consume_paren=*/true);
38337 if (num == error_mark_node)
38338 return list;
38339 num = fold_non_dependent_expr (num);
38340 if (!tree_fits_shwi_p (num)
38341 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
38342 || (n = tree_to_shwi (num)) <= 0
38343 || (int) n != n)
38345 error_at (loc, "collapse argument needs positive constant integer expression");
38346 return list;
38349 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
38350 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
38351 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
38352 OMP_CLAUSE_CHAIN (c) = list;
38353 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
38355 return c;
38358 /* OpenMP 2.5:
38359 default ( none | shared )
38361 OpenMP 5.1:
38362 default ( private | firstprivate )
38364 OpenACC:
38365 default ( none | present ) */
38367 static tree
38368 cp_parser_omp_clause_default (cp_parser *parser, tree list,
38369 location_t location, bool is_oacc)
38371 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
38372 tree c;
38374 matching_parens parens;
38375 if (!parens.require_open (parser))
38376 return list;
38377 if (!is_oacc && cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
38379 kind = OMP_CLAUSE_DEFAULT_PRIVATE;
38380 cp_lexer_consume_token (parser->lexer);
38382 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38384 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38385 const char *p = IDENTIFIER_POINTER (id);
38387 switch (p[0])
38389 case 'n':
38390 if (strcmp ("none", p) != 0)
38391 goto invalid_kind;
38392 kind = OMP_CLAUSE_DEFAULT_NONE;
38393 break;
38395 case 'p':
38396 if (strcmp ("present", p) != 0 || !is_oacc)
38397 goto invalid_kind;
38398 kind = OMP_CLAUSE_DEFAULT_PRESENT;
38399 break;
38401 case 'f':
38402 if (strcmp ("firstprivate", p) != 0 || is_oacc)
38403 goto invalid_kind;
38404 kind = OMP_CLAUSE_DEFAULT_FIRSTPRIVATE;
38405 break;
38407 case 's':
38408 if (strcmp ("shared", p) != 0 || is_oacc)
38409 goto invalid_kind;
38410 kind = OMP_CLAUSE_DEFAULT_SHARED;
38411 break;
38413 default:
38414 goto invalid_kind;
38417 cp_lexer_consume_token (parser->lexer);
38419 else
38421 invalid_kind:
38422 if (is_oacc)
38423 cp_parser_error (parser, "expected %<none%> or %<present%>");
38424 else
38425 cp_parser_error (parser, "expected %<none%>, %<shared%>, "
38426 "%<private%> or %<firstprivate%>");
38429 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
38430 || !parens.require_close (parser))
38431 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38432 /*or_comma=*/false,
38433 /*consume_paren=*/true);
38435 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
38436 return list;
38438 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
38439 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
38440 OMP_CLAUSE_CHAIN (c) = list;
38441 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
38443 return c;
38446 /* OpenMP 3.1:
38447 final ( expression ) */
38449 static tree
38450 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
38452 tree t, c;
38454 matching_parens parens;
38455 if (!parens.require_open (parser))
38456 return list;
38458 t = cp_parser_assignment_expression (parser);
38460 if (t == error_mark_node
38461 || !parens.require_close (parser))
38462 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38463 /*or_comma=*/false,
38464 /*consume_paren=*/true);
38466 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
38468 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
38469 OMP_CLAUSE_FINAL_EXPR (c) = t;
38470 OMP_CLAUSE_CHAIN (c) = list;
38472 return c;
38475 /* OpenMP 2.5:
38476 if ( expression )
38478 OpenMP 4.5:
38479 if ( directive-name-modifier : expression )
38481 directive-name-modifier:
38482 parallel | task | taskloop | target data | target | target update
38483 | target enter data | target exit data
38485 OpenMP 5.0:
38486 directive-name-modifier:
38487 ... | simd | cancel */
38489 static tree
38490 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
38491 bool is_omp)
38493 tree t, c;
38494 enum tree_code if_modifier = ERROR_MARK;
38496 matching_parens parens;
38497 if (!parens.require_open (parser))
38498 return list;
38500 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38502 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38503 const char *p = IDENTIFIER_POINTER (id);
38504 int n = 2;
38506 if (strcmp ("cancel", p) == 0)
38507 if_modifier = VOID_CST;
38508 else if (strcmp ("parallel", p) == 0)
38509 if_modifier = OMP_PARALLEL;
38510 else if (strcmp ("simd", p) == 0)
38511 if_modifier = OMP_SIMD;
38512 else if (strcmp ("task", p) == 0)
38513 if_modifier = OMP_TASK;
38514 else if (strcmp ("taskloop", p) == 0)
38515 if_modifier = OMP_TASKLOOP;
38516 else if (strcmp ("target", p) == 0)
38518 if_modifier = OMP_TARGET;
38519 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
38521 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
38522 p = IDENTIFIER_POINTER (id);
38523 if (strcmp ("data", p) == 0)
38524 if_modifier = OMP_TARGET_DATA;
38525 else if (strcmp ("update", p) == 0)
38526 if_modifier = OMP_TARGET_UPDATE;
38527 else if (strcmp ("enter", p) == 0)
38528 if_modifier = OMP_TARGET_ENTER_DATA;
38529 else if (strcmp ("exit", p) == 0)
38530 if_modifier = OMP_TARGET_EXIT_DATA;
38531 if (if_modifier != OMP_TARGET)
38532 n = 3;
38533 else
38535 location_t loc
38536 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
38537 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
38538 "or %<exit%>");
38539 if_modifier = ERROR_MARK;
38541 if (if_modifier == OMP_TARGET_ENTER_DATA
38542 || if_modifier == OMP_TARGET_EXIT_DATA)
38544 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
38546 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
38547 p = IDENTIFIER_POINTER (id);
38548 if (strcmp ("data", p) == 0)
38549 n = 4;
38551 if (n != 4)
38553 location_t loc
38554 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
38555 error_at (loc, "expected %<data%>");
38556 if_modifier = ERROR_MARK;
38561 if (if_modifier != ERROR_MARK)
38563 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
38565 while (n-- > 0)
38566 cp_lexer_consume_token (parser->lexer);
38568 else
38570 if (n > 2)
38572 location_t loc
38573 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
38574 error_at (loc, "expected %<:%>");
38576 if_modifier = ERROR_MARK;
38581 t = cp_parser_assignment_expression (parser);
38583 if (t == error_mark_node
38584 || !parens.require_close (parser))
38585 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38586 /*or_comma=*/false,
38587 /*consume_paren=*/true);
38589 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
38590 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
38592 if (if_modifier != ERROR_MARK
38593 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
38595 const char *p = NULL;
38596 switch (if_modifier)
38598 case VOID_CST: p = "cancel"; break;
38599 case OMP_PARALLEL: p = "parallel"; break;
38600 case OMP_SIMD: p = "simd"; break;
38601 case OMP_TASK: p = "task"; break;
38602 case OMP_TASKLOOP: p = "taskloop"; break;
38603 case OMP_TARGET_DATA: p = "target data"; break;
38604 case OMP_TARGET: p = "target"; break;
38605 case OMP_TARGET_UPDATE: p = "target update"; break;
38606 case OMP_TARGET_ENTER_DATA: p = "target enter data"; break;
38607 case OMP_TARGET_EXIT_DATA: p = "target exit data"; break;
38608 default: gcc_unreachable ();
38610 error_at (location, "too many %<if%> clauses with %qs modifier",
38612 return list;
38614 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
38616 if (!is_omp)
38617 error_at (location, "too many %<if%> clauses");
38618 else
38619 error_at (location, "too many %<if%> clauses without modifier");
38620 return list;
38622 else if (if_modifier == ERROR_MARK
38623 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
38625 error_at (location, "if any %<if%> clause has modifier, then all "
38626 "%<if%> clauses have to use modifier");
38627 return list;
38631 c = build_omp_clause (location, OMP_CLAUSE_IF);
38632 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
38633 OMP_CLAUSE_IF_EXPR (c) = t;
38634 OMP_CLAUSE_CHAIN (c) = list;
38636 return c;
38639 /* OpenMP 3.1:
38640 mergeable */
38642 static tree
38643 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
38644 tree list, location_t location)
38646 tree c;
38648 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
38649 location);
38651 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
38652 OMP_CLAUSE_CHAIN (c) = list;
38653 return c;
38656 /* OpenMP 2.5:
38657 nowait */
38659 static tree
38660 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
38661 tree list, location_t location)
38663 tree c;
38665 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
38667 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
38668 OMP_CLAUSE_CHAIN (c) = list;
38669 return c;
38672 /* OpenMP 2.5:
38673 num_threads ( expression ) */
38675 static tree
38676 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
38677 location_t location)
38679 tree t, c;
38681 matching_parens parens;
38682 if (!parens.require_open (parser))
38683 return list;
38685 t = cp_parser_assignment_expression (parser);
38687 if (t == error_mark_node
38688 || !parens.require_close (parser))
38689 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38690 /*or_comma=*/false,
38691 /*consume_paren=*/true);
38693 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
38694 "num_threads", location);
38696 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
38697 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
38698 OMP_CLAUSE_CHAIN (c) = list;
38700 return c;
38703 /* OpenMP 4.5:
38704 num_tasks ( expression )
38706 OpenMP 5.1:
38707 num_tasks ( strict : expression ) */
38709 static tree
38710 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
38711 location_t location)
38713 tree t, c;
38715 matching_parens parens;
38716 if (!parens.require_open (parser))
38717 return list;
38719 bool strict = false;
38720 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
38721 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
38723 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38724 if (!strcmp (IDENTIFIER_POINTER (id), "strict"))
38726 strict = true;
38727 cp_lexer_consume_token (parser->lexer);
38728 cp_lexer_consume_token (parser->lexer);
38732 t = cp_parser_assignment_expression (parser);
38734 if (t == error_mark_node
38735 || !parens.require_close (parser))
38736 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38737 /*or_comma=*/false,
38738 /*consume_paren=*/true);
38740 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
38741 "num_tasks", location);
38743 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
38744 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
38745 OMP_CLAUSE_NUM_TASKS_STRICT (c) = strict;
38746 OMP_CLAUSE_CHAIN (c) = list;
38748 return c;
38751 /* OpenMP 4.5:
38752 grainsize ( expression )
38754 OpenMP 5.1:
38755 grainsize ( strict : expression ) */
38757 static tree
38758 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
38759 location_t location)
38761 tree t, c;
38763 matching_parens parens;
38764 if (!parens.require_open (parser))
38765 return list;
38767 bool strict = false;
38768 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
38769 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
38771 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38772 if (!strcmp (IDENTIFIER_POINTER (id), "strict"))
38774 strict = true;
38775 cp_lexer_consume_token (parser->lexer);
38776 cp_lexer_consume_token (parser->lexer);
38780 t = cp_parser_assignment_expression (parser);
38782 if (t == error_mark_node
38783 || !parens.require_close (parser))
38784 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38785 /*or_comma=*/false,
38786 /*consume_paren=*/true);
38788 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
38789 "grainsize", location);
38791 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
38792 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
38793 OMP_CLAUSE_GRAINSIZE_STRICT (c) = strict;
38794 OMP_CLAUSE_CHAIN (c) = list;
38796 return c;
38799 /* OpenMP 4.5:
38800 priority ( expression ) */
38802 static tree
38803 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
38804 location_t location)
38806 tree t, c;
38808 matching_parens parens;
38809 if (!parens.require_open (parser))
38810 return list;
38812 t = cp_parser_assignment_expression (parser);
38814 if (t == error_mark_node
38815 || !parens.require_close (parser))
38816 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38817 /*or_comma=*/false,
38818 /*consume_paren=*/true);
38820 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
38821 "priority", location);
38823 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
38824 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
38825 OMP_CLAUSE_CHAIN (c) = list;
38827 return c;
38830 /* OpenMP 4.5:
38831 hint ( expression ) */
38833 static tree
38834 cp_parser_omp_clause_hint (cp_parser *parser, tree list, location_t location)
38836 tree t, c;
38838 matching_parens parens;
38839 if (!parens.require_open (parser))
38840 return list;
38842 t = cp_parser_assignment_expression (parser);
38844 if (t != error_mark_node)
38846 t = fold_non_dependent_expr (t);
38847 if (!value_dependent_expression_p (t)
38848 && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
38849 || !tree_fits_shwi_p (t)
38850 || tree_int_cst_sgn (t) == -1))
38851 error_at (location, "expected constant integer expression with "
38852 "valid sync-hint value");
38854 if (t == error_mark_node
38855 || !parens.require_close (parser))
38856 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38857 /*or_comma=*/false,
38858 /*consume_paren=*/true);
38859 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
38861 c = build_omp_clause (location, OMP_CLAUSE_HINT);
38862 OMP_CLAUSE_HINT_EXPR (c) = t;
38863 OMP_CLAUSE_CHAIN (c) = list;
38865 return c;
38868 /* OpenMP 5.1:
38869 filter ( integer-expression ) */
38871 static tree
38872 cp_parser_omp_clause_filter (cp_parser *parser, tree list, location_t location)
38874 tree t, c;
38876 matching_parens parens;
38877 if (!parens.require_open (parser))
38878 return list;
38880 t = cp_parser_assignment_expression (parser);
38882 if (t == error_mark_node
38883 || !parens.require_close (parser))
38884 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38885 /*or_comma=*/false,
38886 /*consume_paren=*/true);
38887 check_no_duplicate_clause (list, OMP_CLAUSE_FILTER, "filter", location);
38889 c = build_omp_clause (location, OMP_CLAUSE_FILTER);
38890 OMP_CLAUSE_FILTER_EXPR (c) = t;
38891 OMP_CLAUSE_CHAIN (c) = list;
38893 return c;
38896 /* OpenMP 4.5:
38897 defaultmap ( tofrom : scalar )
38899 OpenMP 5.0:
38900 defaultmap ( implicit-behavior [ : variable-category ] ) */
38902 static tree
38903 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
38904 location_t location)
38906 tree c, id;
38907 const char *p;
38908 enum omp_clause_defaultmap_kind behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
38909 enum omp_clause_defaultmap_kind category
38910 = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED;
38912 matching_parens parens;
38913 if (!parens.require_open (parser))
38914 return list;
38916 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
38917 p = "default";
38918 else if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38920 invalid_behavior:
38921 cp_parser_error (parser, "expected %<alloc%>, %<to%>, %<from%>, "
38922 "%<tofrom%>, %<firstprivate%>, %<none%> "
38923 "or %<default%>");
38924 goto out_err;
38926 else
38928 id = cp_lexer_peek_token (parser->lexer)->u.value;
38929 p = IDENTIFIER_POINTER (id);
38932 switch (p[0])
38934 case 'a':
38935 if (strcmp ("alloc", p) == 0)
38936 behavior = OMP_CLAUSE_DEFAULTMAP_ALLOC;
38937 else
38938 goto invalid_behavior;
38939 break;
38941 case 'd':
38942 if (strcmp ("default", p) == 0)
38943 behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
38944 else
38945 goto invalid_behavior;
38946 break;
38948 case 'f':
38949 if (strcmp ("firstprivate", p) == 0)
38950 behavior = OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE;
38951 else if (strcmp ("from", p) == 0)
38952 behavior = OMP_CLAUSE_DEFAULTMAP_FROM;
38953 else
38954 goto invalid_behavior;
38955 break;
38957 case 'n':
38958 if (strcmp ("none", p) == 0)
38959 behavior = OMP_CLAUSE_DEFAULTMAP_NONE;
38960 else
38961 goto invalid_behavior;
38962 break;
38964 case 'p':
38965 if (strcmp ("present", p) == 0)
38966 behavior = OMP_CLAUSE_DEFAULTMAP_PRESENT;
38967 else
38968 goto invalid_behavior;
38969 break;
38971 case 't':
38972 if (strcmp ("tofrom", p) == 0)
38973 behavior = OMP_CLAUSE_DEFAULTMAP_TOFROM;
38974 else if (strcmp ("to", p) == 0)
38975 behavior = OMP_CLAUSE_DEFAULTMAP_TO;
38976 else
38977 goto invalid_behavior;
38978 break;
38980 default:
38981 goto invalid_behavior;
38983 cp_lexer_consume_token (parser->lexer);
38985 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
38987 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
38988 goto out_err;
38990 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38992 invalid_category:
38993 cp_parser_error (parser, "expected %<scalar%>, %<aggregate%>, "
38994 "%<all%>");
38995 goto out_err;
38997 id = cp_lexer_peek_token (parser->lexer)->u.value;
38998 p = IDENTIFIER_POINTER (id);
39000 switch (p[0])
39002 case 'a':
39003 if (strcmp ("aggregate", p) == 0)
39004 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE;
39005 else if (strcmp ("all", p) == 0)
39006 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_ALL;
39007 else
39008 goto invalid_category;
39009 break;
39011 case 'p':
39012 if (strcmp ("pointer", p) == 0)
39013 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER;
39014 else
39015 goto invalid_category;
39016 break;
39018 case 's':
39019 if (strcmp ("scalar", p) == 0)
39020 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR;
39021 else
39022 goto invalid_category;
39023 break;
39025 default:
39026 goto invalid_category;
39029 cp_lexer_consume_token (parser->lexer);
39031 if (!parens.require_close (parser))
39032 goto out_err;
39034 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
39035 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEFAULTMAP
39036 && (category == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
39037 || category == OMP_CLAUSE_DEFAULTMAP_CATEGORY_ALL
39038 || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c) == category
39039 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c)
39040 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)
39041 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c)
39042 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_ALL)))
39044 enum omp_clause_defaultmap_kind cat = category;
39045 location_t loc = OMP_CLAUSE_LOCATION (c);
39046 if (cat == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
39047 || (cat == OMP_CLAUSE_DEFAULTMAP_CATEGORY_ALL
39048 && (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c)
39049 != OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)))
39050 cat = OMP_CLAUSE_DEFAULTMAP_CATEGORY (c);
39051 p = NULL;
39052 switch (cat)
39054 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED:
39055 p = NULL;
39056 break;
39057 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_ALL:
39058 p = "all";
39059 break;
39060 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE:
39061 p = "aggregate";
39062 break;
39063 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER:
39064 p = "pointer";
39065 break;
39066 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR:
39067 p = "scalar";
39068 break;
39069 default:
39070 gcc_unreachable ();
39072 if (p)
39073 error_at (loc, "too many %<defaultmap%> clauses with %qs category",
39075 else
39076 error_at (loc, "too many %<defaultmap%> clauses with unspecified "
39077 "category");
39078 break;
39081 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
39082 OMP_CLAUSE_DEFAULTMAP_SET_KIND (c, behavior, category);
39083 OMP_CLAUSE_CHAIN (c) = list;
39084 return c;
39086 out_err:
39087 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39088 /*or_comma=*/false,
39089 /*consume_paren=*/true);
39090 return list;
39093 /* OpenMP 5.0:
39094 order ( concurrent )
39096 OpenMP 5.1:
39097 order ( order-modifier : concurrent )
39099 order-modifier:
39100 reproducible
39101 unconstrained */
39103 static tree
39104 cp_parser_omp_clause_order (cp_parser *parser, tree list, location_t location)
39106 tree c, id;
39107 const char *p;
39108 bool unconstrained = false;
39109 bool reproducible = false;
39111 matching_parens parens;
39112 if (!parens.require_open (parser))
39113 return list;
39115 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
39116 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
39118 id = cp_lexer_peek_token (parser->lexer)->u.value;
39119 p = IDENTIFIER_POINTER (id);
39120 if (strcmp (p, "unconstrained") == 0)
39121 unconstrained = true;
39122 else if (strcmp (p, "reproducible") == 0)
39123 reproducible = true;
39124 else
39126 cp_parser_error (parser, "expected %<reproducible%> or "
39127 "%<unconstrained%>");
39128 goto out_err;
39130 cp_lexer_consume_token (parser->lexer);
39131 cp_lexer_consume_token (parser->lexer);
39133 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39135 cp_parser_error (parser, "expected %<concurrent%>");
39136 goto out_err;
39138 else
39140 id = cp_lexer_peek_token (parser->lexer)->u.value;
39141 p = IDENTIFIER_POINTER (id);
39143 if (strcmp (p, "concurrent") != 0)
39145 cp_parser_error (parser, "expected %<concurrent%>");
39146 goto out_err;
39148 cp_lexer_consume_token (parser->lexer);
39149 if (!parens.require_close (parser))
39150 goto out_err;
39152 check_no_duplicate_clause (list, OMP_CLAUSE_ORDER, "order", location);
39153 c = build_omp_clause (location, OMP_CLAUSE_ORDER);
39154 OMP_CLAUSE_ORDER_UNCONSTRAINED (c) = unconstrained;
39155 OMP_CLAUSE_ORDER_REPRODUCIBLE (c) = reproducible;
39156 OMP_CLAUSE_CHAIN (c) = list;
39157 return c;
39159 out_err:
39160 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39161 /*or_comma=*/false,
39162 /*consume_paren=*/true);
39163 return list;
39166 /* OpenMP 5.0:
39167 bind ( teams | parallel | thread ) */
39169 static tree
39170 cp_parser_omp_clause_bind (cp_parser *parser, tree list,
39171 location_t location)
39173 tree c;
39174 const char *p;
39175 enum omp_clause_bind_kind kind = OMP_CLAUSE_BIND_THREAD;
39177 matching_parens parens;
39178 if (!parens.require_open (parser))
39179 return list;
39181 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39183 invalid:
39184 cp_parser_error (parser,
39185 "expected %<teams%>, %<parallel%> or %<thread%>");
39186 goto out_err;
39188 else
39190 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39191 p = IDENTIFIER_POINTER (id);
39193 if (strcmp (p, "teams") == 0)
39194 kind = OMP_CLAUSE_BIND_TEAMS;
39195 else if (strcmp (p, "parallel") == 0)
39196 kind = OMP_CLAUSE_BIND_PARALLEL;
39197 else if (strcmp (p, "thread") != 0)
39198 goto invalid;
39199 cp_lexer_consume_token (parser->lexer);
39200 if (!parens.require_close (parser))
39201 goto out_err;
39203 /* check_no_duplicate_clause (list, OMP_CLAUSE_BIND, "bind", location); */
39204 c = build_omp_clause (location, OMP_CLAUSE_BIND);
39205 OMP_CLAUSE_BIND_KIND (c) = kind;
39206 OMP_CLAUSE_CHAIN (c) = list;
39207 return c;
39209 out_err:
39210 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39211 /*or_comma=*/false,
39212 /*consume_paren=*/true);
39213 return list;
39216 /* OpenMP 2.5:
39217 ordered
39219 OpenMP 4.5:
39220 ordered ( constant-expression ) */
39222 static tree
39223 cp_parser_omp_clause_ordered (cp_parser *parser,
39224 tree list, location_t location)
39226 tree c, num = NULL_TREE;
39227 HOST_WIDE_INT n;
39229 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
39230 "ordered", location);
39232 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
39234 matching_parens parens;
39235 parens.consume_open (parser);
39237 num = cp_parser_constant_expression (parser);
39239 if (!parens.require_close (parser))
39240 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39241 /*or_comma=*/false,
39242 /*consume_paren=*/true);
39244 if (num == error_mark_node)
39245 return list;
39246 num = fold_non_dependent_expr (num);
39247 if (!tree_fits_shwi_p (num)
39248 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
39249 || (n = tree_to_shwi (num)) <= 0
39250 || (int) n != n)
39252 error_at (location,
39253 "ordered argument needs positive constant integer "
39254 "expression");
39255 return list;
39259 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
39260 OMP_CLAUSE_ORDERED_EXPR (c) = num;
39261 OMP_CLAUSE_CHAIN (c) = list;
39262 return c;
39265 /* OpenMP 2.5:
39266 reduction ( reduction-operator : variable-list )
39268 reduction-operator:
39269 One of: + * - & ^ | && ||
39271 OpenMP 3.1:
39273 reduction-operator:
39274 One of: + * - & ^ | && || min max
39276 OpenMP 4.0:
39278 reduction-operator:
39279 One of: + * - & ^ | && ||
39280 id-expression
39282 OpenMP 5.0:
39283 reduction ( reduction-modifier, reduction-operator : variable-list )
39284 in_reduction ( reduction-operator : variable-list )
39285 task_reduction ( reduction-operator : variable-list ) */
39287 static tree
39288 cp_parser_omp_clause_reduction (cp_parser *parser, enum omp_clause_code kind,
39289 bool is_omp, tree list)
39291 enum tree_code code = ERROR_MARK;
39292 tree nlist, c, id = NULL_TREE;
39293 bool task = false;
39294 bool inscan = false;
39296 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
39297 return list;
39299 if (kind == OMP_CLAUSE_REDUCTION && is_omp)
39301 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT)
39302 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
39304 cp_lexer_consume_token (parser->lexer);
39305 cp_lexer_consume_token (parser->lexer);
39307 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
39308 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
39310 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39311 const char *p = IDENTIFIER_POINTER (id);
39312 if (strcmp (p, "task") == 0)
39313 task = true;
39314 else if (strcmp (p, "inscan") == 0)
39315 inscan = true;
39316 if (task || inscan)
39318 cp_lexer_consume_token (parser->lexer);
39319 cp_lexer_consume_token (parser->lexer);
39324 switch (cp_lexer_peek_token (parser->lexer)->type)
39326 case CPP_PLUS: code = PLUS_EXPR; break;
39327 case CPP_MULT: code = MULT_EXPR; break;
39328 case CPP_MINUS: code = MINUS_EXPR; break;
39329 case CPP_AND: code = BIT_AND_EXPR; break;
39330 case CPP_XOR: code = BIT_XOR_EXPR; break;
39331 case CPP_OR: code = BIT_IOR_EXPR; break;
39332 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
39333 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
39334 default: break;
39337 if (code != ERROR_MARK)
39338 cp_lexer_consume_token (parser->lexer);
39339 else
39341 bool saved_colon_corrects_to_scope_p;
39342 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
39343 parser->colon_corrects_to_scope_p = false;
39344 id = cp_parser_id_expression (parser, /*template_p=*/false,
39345 /*check_dependency_p=*/true,
39346 /*template_p=*/NULL,
39347 /*declarator_p=*/false,
39348 /*optional_p=*/false);
39349 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
39350 if (identifier_p (id))
39352 const char *p = IDENTIFIER_POINTER (id);
39354 if (strcmp (p, "min") == 0)
39355 code = MIN_EXPR;
39356 else if (strcmp (p, "max") == 0)
39357 code = MAX_EXPR;
39358 else if (id == ovl_op_identifier (false, PLUS_EXPR))
39359 code = PLUS_EXPR;
39360 else if (id == ovl_op_identifier (false, MULT_EXPR))
39361 code = MULT_EXPR;
39362 else if (id == ovl_op_identifier (false, MINUS_EXPR))
39363 code = MINUS_EXPR;
39364 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
39365 code = BIT_AND_EXPR;
39366 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
39367 code = BIT_IOR_EXPR;
39368 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
39369 code = BIT_XOR_EXPR;
39370 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
39371 code = TRUTH_ANDIF_EXPR;
39372 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
39373 code = TRUTH_ORIF_EXPR;
39374 id = omp_reduction_id (code, id, NULL_TREE);
39375 tree scope = parser->scope;
39376 if (scope)
39377 id = build_qualified_name (NULL_TREE, scope, id, false);
39378 parser->scope = NULL_TREE;
39379 parser->qualifying_scope = NULL_TREE;
39380 parser->object_scope = NULL_TREE;
39382 else
39384 error ("invalid reduction-identifier");
39385 resync_fail:
39386 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39387 /*or_comma=*/false,
39388 /*consume_paren=*/true);
39389 return list;
39393 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
39394 goto resync_fail;
39396 nlist = cp_parser_omp_var_list_no_open (parser, kind, list,
39397 NULL);
39398 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
39400 OMP_CLAUSE_REDUCTION_CODE (c) = code;
39401 if (task)
39402 OMP_CLAUSE_REDUCTION_TASK (c) = 1;
39403 else if (inscan)
39404 OMP_CLAUSE_REDUCTION_INSCAN (c) = 1;
39405 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
39408 return nlist;
39411 /* OpenMP 2.5:
39412 schedule ( schedule-kind )
39413 schedule ( schedule-kind , expression )
39415 schedule-kind:
39416 static | dynamic | guided | runtime | auto
39418 OpenMP 4.5:
39419 schedule ( schedule-modifier : schedule-kind )
39420 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
39422 schedule-modifier:
39423 simd
39424 monotonic
39425 nonmonotonic */
39427 static tree
39428 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
39430 tree c, t;
39431 int modifiers = 0, nmodifiers = 0;
39433 matching_parens parens;
39434 if (!parens.require_open (parser))
39435 return list;
39437 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
39439 location_t comma = UNKNOWN_LOCATION;
39440 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39442 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39443 const char *p = IDENTIFIER_POINTER (id);
39444 if (strcmp ("simd", p) == 0)
39445 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
39446 else if (strcmp ("monotonic", p) == 0)
39447 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
39448 else if (strcmp ("nonmonotonic", p) == 0)
39449 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
39450 else
39451 break;
39452 comma = UNKNOWN_LOCATION;
39453 cp_lexer_consume_token (parser->lexer);
39454 if (nmodifiers++ == 0
39455 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39457 comma = cp_lexer_peek_token (parser->lexer)->location;
39458 cp_lexer_consume_token (parser->lexer);
39460 else
39462 cp_parser_require (parser, CPP_COLON, RT_COLON);
39463 break;
39466 if (comma != UNKNOWN_LOCATION)
39467 error_at (comma, "expected %<:%>");
39469 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39471 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39472 const char *p = IDENTIFIER_POINTER (id);
39474 switch (p[0])
39476 case 'd':
39477 if (strcmp ("dynamic", p) != 0)
39478 goto invalid_kind;
39479 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
39480 break;
39482 case 'g':
39483 if (strcmp ("guided", p) != 0)
39484 goto invalid_kind;
39485 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
39486 break;
39488 case 'r':
39489 if (strcmp ("runtime", p) != 0)
39490 goto invalid_kind;
39491 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
39492 break;
39494 default:
39495 goto invalid_kind;
39498 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
39499 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
39500 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
39501 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
39502 else
39503 goto invalid_kind;
39504 cp_lexer_consume_token (parser->lexer);
39506 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
39507 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
39508 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
39509 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
39511 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
39512 "specified");
39513 modifiers = 0;
39516 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39518 cp_token *token;
39519 cp_lexer_consume_token (parser->lexer);
39521 token = cp_lexer_peek_token (parser->lexer);
39522 t = cp_parser_assignment_expression (parser);
39524 if (t == error_mark_node)
39525 goto resync_fail;
39526 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
39527 error_at (token->location, "schedule %<runtime%> does not take "
39528 "a %<chunk_size%> parameter");
39529 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
39530 error_at (token->location, "schedule %<auto%> does not take "
39531 "a %<chunk_size%> parameter");
39532 else
39533 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
39535 if (!parens.require_close (parser))
39536 goto resync_fail;
39538 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
39539 goto resync_fail;
39541 OMP_CLAUSE_SCHEDULE_KIND (c)
39542 = (enum omp_clause_schedule_kind)
39543 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
39545 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
39546 OMP_CLAUSE_CHAIN (c) = list;
39547 return c;
39549 invalid_kind:
39550 cp_parser_error (parser, "invalid schedule kind");
39551 resync_fail:
39552 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39553 /*or_comma=*/false,
39554 /*consume_paren=*/true);
39555 return list;
39558 /* OpenMP 3.0:
39559 untied */
39561 static tree
39562 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
39563 tree list, location_t location)
39565 tree c;
39567 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
39569 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
39570 OMP_CLAUSE_CHAIN (c) = list;
39571 return c;
39574 /* OpenMP 4.0:
39575 inbranch
39576 notinbranch */
39578 static tree
39579 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
39580 tree list, location_t location)
39582 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
39583 tree c = build_omp_clause (location, code);
39584 OMP_CLAUSE_CHAIN (c) = list;
39585 return c;
39588 /* OpenMP 4.0:
39589 parallel
39591 sections
39592 taskgroup */
39594 static tree
39595 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
39596 enum omp_clause_code code,
39597 tree list, location_t location)
39599 tree c = build_omp_clause (location, code);
39600 OMP_CLAUSE_CHAIN (c) = list;
39601 return c;
39604 /* OpenMP 4.5:
39605 nogroup */
39607 static tree
39608 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
39609 tree list, location_t location)
39611 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
39612 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
39613 OMP_CLAUSE_CHAIN (c) = list;
39614 return c;
39617 /* OpenMP 4.5:
39618 simd
39619 threads */
39621 static tree
39622 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
39623 enum omp_clause_code code,
39624 tree list, location_t location)
39626 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
39627 tree c = build_omp_clause (location, code);
39628 OMP_CLAUSE_CHAIN (c) = list;
39629 return c;
39632 /* OpenMP 4.0:
39633 num_teams ( expression )
39635 OpenMP 5.1:
39636 num_teams ( expression : expression ) */
39638 static tree
39639 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
39640 location_t location)
39642 tree upper, lower = NULL_TREE, c;
39644 matching_parens parens;
39645 if (!parens.require_open (parser))
39646 return list;
39648 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
39649 parser->colon_corrects_to_scope_p = false;
39650 upper = cp_parser_assignment_expression (parser);
39651 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
39653 if (upper != error_mark_node
39654 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
39656 lower = upper;
39657 cp_lexer_consume_token (parser->lexer);
39658 upper = cp_parser_assignment_expression (parser);
39661 if (upper == error_mark_node
39662 || !parens.require_close (parser))
39663 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39664 /*or_comma=*/false,
39665 /*consume_paren=*/true);
39667 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
39668 "num_teams", location);
39670 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
39671 OMP_CLAUSE_NUM_TEAMS_UPPER_EXPR (c) = upper;
39672 OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c) = lower;
39673 OMP_CLAUSE_CHAIN (c) = list;
39675 return c;
39678 /* OpenMP 4.0:
39679 thread_limit ( expression ) */
39681 static tree
39682 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
39683 location_t location)
39685 tree t, c;
39687 matching_parens parens;
39688 if (!parens.require_open (parser))
39689 return list;
39691 t = cp_parser_assignment_expression (parser);
39693 if (t == error_mark_node
39694 || !parens.require_close (parser))
39695 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39696 /*or_comma=*/false,
39697 /*consume_paren=*/true);
39699 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
39700 "thread_limit", location);
39702 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
39703 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
39704 OMP_CLAUSE_CHAIN (c) = list;
39706 return c;
39709 /* OpenMP 4.0:
39710 aligned ( variable-list )
39711 aligned ( variable-list : constant-expression ) */
39713 static tree
39714 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
39716 tree nlist, c, alignment = NULL_TREE;
39717 bool colon;
39719 matching_parens parens;
39720 if (!parens.require_open (parser))
39721 return list;
39723 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
39724 &colon);
39726 if (colon)
39728 alignment = cp_parser_constant_expression (parser);
39730 if (!parens.require_close (parser))
39731 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39732 /*or_comma=*/false,
39733 /*consume_paren=*/true);
39735 if (alignment == error_mark_node)
39736 alignment = NULL_TREE;
39739 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
39740 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
39742 return nlist;
39745 /* OpenMP 5.0:
39746 allocate ( variable-list )
39747 allocate ( expression : variable-list )
39749 OpenMP 5.1:
39750 allocate ( allocator-modifier : variable-list )
39751 allocate ( allocator-modifier , allocator-modifier : variable-list )
39753 allocator-modifier:
39754 allocator ( expression )
39755 align ( expression ) */
39757 static tree
39758 cp_parser_omp_clause_allocate (cp_parser *parser, tree list)
39760 tree nlist, c, allocator = NULL_TREE, align = NULL_TREE;
39761 bool colon, has_modifiers = false;
39763 matching_parens parens;
39764 if (!parens.require_open (parser))
39765 return list;
39767 cp_parser_parse_tentatively (parser);
39768 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
39769 parser->colon_corrects_to_scope_p = false;
39770 for (int mod = 0; mod < 2; mod++)
39771 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
39772 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
39774 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39775 const char *p = IDENTIFIER_POINTER (id);
39776 if (strcmp (p, "allocator") != 0 && strcmp (p, "align") != 0)
39777 break;
39778 cp_lexer_consume_token (parser->lexer);
39779 matching_parens parens2;
39780 if (!parens2.require_open (parser))
39781 break;
39782 if (strcmp (p, "allocator") == 0)
39784 if (allocator != NULL_TREE)
39785 break;
39786 allocator = cp_parser_assignment_expression (parser);
39788 else
39790 if (align != NULL_TREE)
39791 break;
39792 align = cp_parser_assignment_expression (parser);
39794 if (!parens2.require_close (parser))
39795 break;
39796 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
39798 has_modifiers = true;
39799 break;
39801 if (mod != 0 || cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
39802 break;
39803 cp_lexer_consume_token (parser->lexer);
39805 else
39806 break;
39807 if (!has_modifiers)
39809 cp_parser_abort_tentative_parse (parser);
39810 align = NULL_TREE;
39811 allocator = NULL_TREE;
39812 cp_parser_parse_tentatively (parser);
39813 allocator = cp_parser_assignment_expression (parser);
39815 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
39816 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
39818 cp_parser_parse_definitely (parser);
39819 cp_lexer_consume_token (parser->lexer);
39820 if (allocator == error_mark_node)
39821 allocator = NULL_TREE;
39822 if (align == error_mark_node)
39823 align = NULL_TREE;
39825 else
39827 cp_parser_abort_tentative_parse (parser);
39828 allocator = NULL_TREE;
39829 align = NULL_TREE;
39832 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALLOCATE, list,
39833 &colon);
39835 if (allocator || align)
39836 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
39838 OMP_CLAUSE_ALLOCATE_ALLOCATOR (c) = allocator;
39839 OMP_CLAUSE_ALLOCATE_ALIGN (c) = align;
39842 return nlist;
39845 /* OpenMP 2.5:
39846 lastprivate ( variable-list )
39848 OpenMP 5.0:
39849 lastprivate ( [ lastprivate-modifier : ] variable-list ) */
39851 static tree
39852 cp_parser_omp_clause_lastprivate (cp_parser *parser, tree list)
39854 bool conditional = false;
39856 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
39857 return list;
39859 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
39860 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
39862 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39863 const char *p = IDENTIFIER_POINTER (id);
39865 if (strcmp ("conditional", p) == 0)
39867 conditional = true;
39868 cp_lexer_consume_token (parser->lexer);
39869 cp_lexer_consume_token (parser->lexer);
39873 tree nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LASTPRIVATE,
39874 list, NULL);
39876 if (conditional)
39877 for (tree c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
39878 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 1;
39879 return nlist;
39882 /* OpenMP 4.0:
39883 linear ( variable-list )
39884 linear ( variable-list : expression )
39886 OpenMP 4.5:
39887 linear ( modifier ( variable-list ) )
39888 linear ( modifier ( variable-list ) : expression )
39890 modifier:
39893 uval
39895 OpenMP 5.2:
39896 linear ( variable-list : modifiers-list )
39898 modifiers:
39901 uval
39902 step ( expression ) */
39904 static tree
39905 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
39906 bool declare_simd)
39908 tree nlist, c, step = integer_one_node;
39909 bool colon;
39910 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
39911 bool old_linear_modifier = false;
39913 matching_parens parens;
39914 if (!parens.require_open (parser))
39915 return list;
39917 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39919 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39920 const char *p = IDENTIFIER_POINTER (id);
39922 if (strcmp ("ref", p) == 0)
39923 kind = OMP_CLAUSE_LINEAR_REF;
39924 else if (strcmp ("val", p) == 0)
39925 kind = OMP_CLAUSE_LINEAR_VAL;
39926 else if (strcmp ("uval", p) == 0)
39927 kind = OMP_CLAUSE_LINEAR_UVAL;
39928 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
39930 cp_lexer_consume_token (parser->lexer);
39931 old_linear_modifier = true;
39933 else
39934 kind = OMP_CLAUSE_LINEAR_DEFAULT;
39937 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
39938 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
39939 &colon);
39940 else
39942 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
39943 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
39944 if (colon)
39945 cp_parser_require (parser, CPP_COLON, RT_COLON);
39946 else if (!parens.require_close (parser))
39947 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39948 /*or_comma=*/false,
39949 /*consume_paren=*/true);
39952 if (colon)
39954 bool has_modifiers = false;
39955 if (kind == OMP_CLAUSE_LINEAR_DEFAULT
39956 && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39958 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39959 const char *p = IDENTIFIER_POINTER (id);
39960 size_t pos = 0;
39961 if (strcmp ("ref", p) == 0
39962 || strcmp ("val", p) == 0
39963 || strcmp ("uval", p) == 0)
39964 pos = 2;
39965 else if (strcmp ("step", p) == 0
39966 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
39968 pos = cp_parser_skip_balanced_tokens (parser, 2);
39969 if (pos == 2)
39970 pos = 0;
39972 if (pos != 0
39973 && (cp_lexer_nth_token_is (parser->lexer, pos, CPP_COMMA)
39974 || cp_lexer_nth_token_is (parser->lexer, pos,
39975 CPP_CLOSE_PAREN)))
39976 has_modifiers = true;
39979 step = NULL_TREE;
39980 if (has_modifiers)
39982 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39984 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39985 const char *p = IDENTIFIER_POINTER (id);
39986 enum omp_clause_linear_kind nkind = OMP_CLAUSE_LINEAR_DEFAULT;
39987 if (strcmp ("ref", p) == 0)
39988 nkind = OMP_CLAUSE_LINEAR_REF;
39989 else if (strcmp ("val", p) == 0)
39990 nkind = OMP_CLAUSE_LINEAR_VAL;
39991 else if (strcmp ("uval", p) == 0)
39992 nkind = OMP_CLAUSE_LINEAR_UVAL;
39993 if (nkind != OMP_CLAUSE_LINEAR_DEFAULT)
39995 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
39996 error_at (cp_lexer_peek_token (parser->lexer)->location,
39997 "multiple linear modifiers");
39998 kind = nkind;
39999 cp_lexer_consume_token (parser->lexer);
40001 else if (strcmp ("step", p) == 0)
40003 location_t step_loc
40004 = cp_lexer_peek_token (parser->lexer)->location;
40005 cp_lexer_consume_token (parser->lexer);
40006 matching_parens parens2;
40007 if (parens2.require_open (parser))
40009 if (step)
40010 error_at (step_loc, "multiple %<step%> modifiers");
40011 if (declare_simd
40012 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
40013 && cp_lexer_nth_token_is (parser->lexer, 2,
40014 CPP_CLOSE_PAREN))
40016 cp_token *token
40017 = cp_lexer_peek_token (parser->lexer);
40018 location_t tok_loc = token->location;
40019 cp_parser_parse_tentatively (parser);
40020 step = cp_parser_id_expression (parser, false, true,
40021 NULL, false, false);
40022 if (step != error_mark_node)
40023 step = cp_parser_lookup_name_simple (parser, step,
40024 tok_loc);
40025 if (step == error_mark_node)
40027 step = NULL_TREE;
40028 cp_parser_abort_tentative_parse (parser);
40030 else if (!cp_parser_parse_definitely (parser))
40031 step = NULL_TREE;
40033 if (!step)
40034 step = cp_parser_assignment_expression (parser);
40035 if (!parens2.require_close (parser))
40036 cp_parser_skip_to_closing_parenthesis (parser, true,
40037 false, true);
40039 else
40040 break;
40042 else
40043 break;
40044 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
40046 cp_lexer_consume_token (parser->lexer);
40047 continue;
40049 break;
40051 if (!step)
40052 step = integer_one_node;
40054 else if (declare_simd
40055 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
40056 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
40058 cp_token *token = cp_lexer_peek_token (parser->lexer);
40059 cp_parser_parse_tentatively (parser);
40060 step = cp_parser_id_expression (parser, /*template_p=*/false,
40061 /*check_dependency_p=*/true,
40062 /*template_p=*/NULL,
40063 /*declarator_p=*/false,
40064 /*optional_p=*/false);
40065 if (step != error_mark_node)
40066 step = cp_parser_lookup_name_simple (parser, step, token->location);
40067 if (step == error_mark_node)
40069 step = NULL_TREE;
40070 cp_parser_abort_tentative_parse (parser);
40072 else if (!cp_parser_parse_definitely (parser))
40073 step = NULL_TREE;
40075 if (!step)
40076 step = cp_parser_assignment_expression (parser);
40078 if (!parens.require_close (parser))
40079 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40080 /*or_comma=*/false,
40081 /*consume_paren=*/true);
40083 if (step == error_mark_node)
40084 return list;
40087 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
40089 OMP_CLAUSE_LINEAR_STEP (c) = step;
40090 OMP_CLAUSE_LINEAR_KIND (c) = kind;
40091 OMP_CLAUSE_LINEAR_OLD_LINEAR_MODIFIER (c) = old_linear_modifier;
40094 return nlist;
40097 /* OpenMP 4.0:
40098 safelen ( constant-expression ) */
40100 static tree
40101 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
40102 location_t location)
40104 tree t, c;
40106 matching_parens parens;
40107 if (!parens.require_open (parser))
40108 return list;
40110 t = cp_parser_constant_expression (parser);
40112 if (t == error_mark_node
40113 || !parens.require_close (parser))
40114 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40115 /*or_comma=*/false,
40116 /*consume_paren=*/true);
40118 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
40120 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
40121 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
40122 OMP_CLAUSE_CHAIN (c) = list;
40124 return c;
40127 /* OpenMP 4.0:
40128 simdlen ( constant-expression ) */
40130 static tree
40131 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
40132 location_t location)
40134 tree t, c;
40136 matching_parens parens;
40137 if (!parens.require_open (parser))
40138 return list;
40140 t = cp_parser_constant_expression (parser);
40142 if (t == error_mark_node
40143 || !parens.require_close (parser))
40144 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40145 /*or_comma=*/false,
40146 /*consume_paren=*/true);
40148 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
40150 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
40151 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
40152 OMP_CLAUSE_CHAIN (c) = list;
40154 return c;
40157 /* OpenMP 4.5:
40158 vec:
40159 identifier [+/- integer]
40160 vec , identifier [+/- integer]
40163 static tree
40164 cp_parser_omp_clause_doacross_sink (cp_parser *parser, location_t clause_loc,
40165 tree list, bool depend_p)
40167 tree vec = NULL;
40169 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
40171 cp_parser_error (parser, "expected identifier");
40172 return list;
40175 if (!depend_p)
40177 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40178 if (strcmp (IDENTIFIER_POINTER (id), "omp_cur_iteration") == 0
40179 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_MINUS)
40180 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_NUMBER)
40181 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_PAREN))
40183 tree val = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
40184 if (integer_onep (val))
40186 cp_lexer_consume_token (parser->lexer);
40187 cp_lexer_consume_token (parser->lexer);
40188 cp_lexer_consume_token (parser->lexer);
40189 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DOACROSS);
40190 OMP_CLAUSE_DOACROSS_KIND (u) = OMP_CLAUSE_DOACROSS_SINK;
40191 OMP_CLAUSE_CHAIN (u) = list;
40192 return u;
40197 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40199 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
40200 tree t, identifier = cp_parser_identifier (parser);
40201 tree addend = NULL;
40203 if (identifier == error_mark_node)
40204 t = error_mark_node;
40205 else
40207 t = cp_parser_lookup_name_simple
40208 (parser, identifier,
40209 cp_lexer_peek_token (parser->lexer)->location);
40210 if (t == error_mark_node)
40211 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
40212 id_loc);
40215 bool neg = false;
40216 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
40217 neg = true;
40218 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
40220 addend = integer_zero_node;
40221 goto add_to_vector;
40223 cp_lexer_consume_token (parser->lexer);
40225 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
40227 cp_parser_error (parser, "expected integer");
40228 return list;
40231 addend = cp_lexer_peek_token (parser->lexer)->u.value;
40232 if (TREE_CODE (addend) != INTEGER_CST)
40234 cp_parser_error (parser, "expected integer");
40235 return list;
40237 cp_lexer_consume_token (parser->lexer);
40239 add_to_vector:
40240 if (t != error_mark_node)
40242 vec = tree_cons (addend, t, vec);
40243 if (neg)
40244 OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (vec) = 1;
40247 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
40248 || !cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
40249 break;
40251 cp_lexer_consume_token (parser->lexer);
40254 if (vec)
40256 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DOACROSS);
40257 OMP_CLAUSE_DOACROSS_KIND (u) = OMP_CLAUSE_DOACROSS_SINK;
40258 OMP_CLAUSE_DOACROSS_DEPEND (u) = depend_p;
40259 OMP_CLAUSE_DECL (u) = nreverse (vec);
40260 OMP_CLAUSE_CHAIN (u) = list;
40261 return u;
40263 return list;
40266 /* OpenMP 5.0:
40267 detach ( event-handle ) */
40269 static tree
40270 cp_parser_omp_clause_detach (cp_parser *parser, tree list)
40272 matching_parens parens;
40274 if (!parens.require_open (parser))
40275 return list;
40277 cp_token *token;
40278 tree name, decl;
40280 token = cp_lexer_peek_token (parser->lexer);
40281 name = cp_parser_id_expression (parser, /*template_p=*/false,
40282 /*check_dependency_p=*/true,
40283 /*template_p=*/NULL,
40284 /*declarator_p=*/false,
40285 /*optional_p=*/false);
40286 if (name == error_mark_node)
40287 decl = error_mark_node;
40288 else
40290 if (identifier_p (name))
40291 decl = cp_parser_lookup_name_simple (parser, name, token->location);
40292 else
40293 decl = name;
40294 if (decl == error_mark_node)
40295 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
40296 token->location);
40299 if (decl == error_mark_node
40300 || !parens.require_close (parser))
40301 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40302 /*or_comma=*/false,
40303 /*consume_paren=*/true);
40305 tree u = build_omp_clause (token->location, OMP_CLAUSE_DETACH);
40306 OMP_CLAUSE_DECL (u) = decl;
40307 OMP_CLAUSE_CHAIN (u) = list;
40309 return u;
40312 /* OpenMP 5.0:
40313 iterators ( iterators-definition )
40315 iterators-definition:
40316 iterator-specifier
40317 iterator-specifier , iterators-definition
40319 iterator-specifier:
40320 identifier = range-specification
40321 iterator-type identifier = range-specification
40323 range-specification:
40324 begin : end
40325 begin : end : step */
40327 static tree
40328 cp_parser_omp_iterators (cp_parser *parser)
40330 tree ret = NULL_TREE, *last = &ret;
40331 cp_lexer_consume_token (parser->lexer);
40333 matching_parens parens;
40334 if (!parens.require_open (parser))
40335 return error_mark_node;
40337 bool saved_colon_corrects_to_scope_p
40338 = parser->colon_corrects_to_scope_p;
40339 bool saved_colon_doesnt_start_class_def_p
40340 = parser->colon_doesnt_start_class_def_p;
40344 tree iter_type;
40345 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
40346 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ))
40347 iter_type = integer_type_node;
40348 else
40350 const char *saved_message
40351 = parser->type_definition_forbidden_message;
40352 parser->type_definition_forbidden_message
40353 = G_("types may not be defined in iterator type");
40355 iter_type = cp_parser_type_id (parser);
40357 parser->type_definition_forbidden_message = saved_message;
40360 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
40361 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
40363 cp_parser_error (parser, "expected identifier");
40364 break;
40367 tree id = cp_parser_identifier (parser);
40368 if (id == error_mark_node)
40369 break;
40371 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
40372 break;
40374 parser->colon_corrects_to_scope_p = false;
40375 parser->colon_doesnt_start_class_def_p = true;
40376 tree begin = cp_parser_assignment_expression (parser);
40378 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
40379 break;
40381 tree end = cp_parser_assignment_expression (parser);
40383 tree step = integer_one_node;
40384 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
40386 cp_lexer_consume_token (parser->lexer);
40387 step = cp_parser_assignment_expression (parser);
40390 tree iter_var = build_decl (loc, VAR_DECL, id, iter_type);
40391 DECL_ARTIFICIAL (iter_var) = 1;
40392 DECL_CONTEXT (iter_var) = current_function_decl;
40393 pushdecl (iter_var);
40395 *last = make_tree_vec (6);
40396 TREE_VEC_ELT (*last, 0) = iter_var;
40397 TREE_VEC_ELT (*last, 1) = begin;
40398 TREE_VEC_ELT (*last, 2) = end;
40399 TREE_VEC_ELT (*last, 3) = step;
40400 last = &TREE_CHAIN (*last);
40402 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
40404 cp_lexer_consume_token (parser->lexer);
40405 continue;
40407 break;
40409 while (1);
40411 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
40412 parser->colon_doesnt_start_class_def_p
40413 = saved_colon_doesnt_start_class_def_p;
40415 if (!parens.require_close (parser))
40416 cp_parser_skip_to_closing_parenthesis (parser,
40417 /*recovering=*/true,
40418 /*or_comma=*/false,
40419 /*consume_paren=*/true);
40421 return ret ? ret : error_mark_node;
40424 /* OpenMP 5.0:
40425 affinity ( [aff-modifier :] variable-list )
40426 aff-modifier:
40427 iterator ( iterators-definition ) */
40429 static tree
40430 cp_parser_omp_clause_affinity (cp_parser *parser, tree list)
40432 tree nlist, c, iterators = NULL_TREE;
40434 matching_parens parens;
40435 if (!parens.require_open (parser))
40436 return list;
40438 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40440 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40441 const char *p = IDENTIFIER_POINTER (id);
40442 bool parse_iter = ((strcmp ("iterator", p) == 0)
40443 && (cp_lexer_nth_token_is (parser->lexer, 2,
40444 CPP_OPEN_PAREN)));
40445 if (parse_iter)
40447 size_t n = cp_parser_skip_balanced_tokens (parser, 2);
40448 parse_iter = cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON);
40450 if (parse_iter)
40452 begin_scope (sk_omp, NULL);
40453 iterators = cp_parser_omp_iterators (parser);
40454 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
40456 if (iterators)
40457 poplevel (0, 1, 0);
40458 cp_parser_skip_to_closing_parenthesis (parser,
40459 /*recovering=*/true,
40460 /*or_comma=*/false,
40461 /*consume_paren=*/true);
40462 return list;
40466 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_AFFINITY,
40467 list, NULL);
40468 if (iterators)
40470 tree block = poplevel (1, 1, 0);
40471 if (iterators != error_mark_node)
40473 TREE_VEC_ELT (iterators, 5) = block;
40474 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
40475 OMP_CLAUSE_DECL (c) = build_tree_list (iterators,
40476 OMP_CLAUSE_DECL (c));
40479 return nlist;
40482 /* OpenMP 4.0:
40483 depend ( depend-kind : variable-list )
40485 depend-kind:
40486 in | out | inout
40488 OpenMP 4.5:
40489 depend ( source )
40491 depend ( sink : vec )
40493 OpenMP 5.0:
40494 depend ( depend-modifier , depend-kind: variable-list )
40496 depend-kind:
40497 in | out | inout | mutexinoutset | depobj
40499 depend-modifier:
40500 iterator ( iterators-definition ) */
40502 static tree
40503 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
40505 tree nlist, c, iterators = NULL_TREE;
40506 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_LAST;
40507 enum omp_clause_doacross_kind dkind = OMP_CLAUSE_DOACROSS_LAST;
40509 matching_parens parens;
40510 if (!parens.require_open (parser))
40511 return list;
40515 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
40516 goto invalid_kind;
40518 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40519 const char *p = IDENTIFIER_POINTER (id);
40521 if (strcmp ("iterator", p) == 0 && iterators == NULL_TREE)
40523 begin_scope (sk_omp, NULL);
40524 iterators = cp_parser_omp_iterators (parser);
40525 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
40526 continue;
40528 if (strcmp ("in", p) == 0)
40529 kind = OMP_CLAUSE_DEPEND_IN;
40530 else if (strcmp ("inout", p) == 0)
40531 kind = OMP_CLAUSE_DEPEND_INOUT;
40532 else if (strcmp ("inoutset", p) == 0)
40533 kind = OMP_CLAUSE_DEPEND_INOUTSET;
40534 else if (strcmp ("mutexinoutset", p) == 0)
40535 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
40536 else if (strcmp ("out", p) == 0)
40537 kind = OMP_CLAUSE_DEPEND_OUT;
40538 else if (strcmp ("depobj", p) == 0)
40539 kind = OMP_CLAUSE_DEPEND_DEPOBJ;
40540 else if (strcmp ("sink", p) == 0)
40541 dkind = OMP_CLAUSE_DOACROSS_SINK;
40542 else if (strcmp ("source", p) == 0)
40543 dkind = OMP_CLAUSE_DOACROSS_SOURCE;
40544 else
40545 goto invalid_kind;
40546 break;
40548 while (1);
40550 cp_lexer_consume_token (parser->lexer);
40552 if (iterators
40553 && (dkind == OMP_CLAUSE_DOACROSS_SOURCE
40554 || dkind == OMP_CLAUSE_DOACROSS_SINK))
40556 poplevel (0, 1, 0);
40557 error_at (loc, "%<iterator%> modifier incompatible with %qs",
40558 dkind == OMP_CLAUSE_DOACROSS_SOURCE ? "source" : "sink");
40559 iterators = NULL_TREE;
40562 if (dkind == OMP_CLAUSE_DOACROSS_SOURCE)
40564 c = build_omp_clause (loc, OMP_CLAUSE_DOACROSS);
40565 OMP_CLAUSE_DOACROSS_KIND (c) = dkind;
40566 OMP_CLAUSE_DOACROSS_DEPEND (c) = 1;
40567 OMP_CLAUSE_DECL (c) = NULL_TREE;
40568 OMP_CLAUSE_CHAIN (c) = list;
40569 if (!parens.require_close (parser))
40570 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40571 /*or_comma=*/false,
40572 /*consume_paren=*/true);
40573 return c;
40576 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
40577 goto resync_fail;
40579 if (dkind == OMP_CLAUSE_DOACROSS_SINK)
40581 nlist = cp_parser_omp_clause_doacross_sink (parser, loc, list, true);
40582 if (!parens.require_close (parser))
40583 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40584 /*or_comma=*/false,
40585 /*consume_paren=*/true);
40587 else
40589 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
40590 list, NULL);
40592 if (iterators)
40594 tree block = poplevel (1, 1, 0);
40595 if (iterators == error_mark_node)
40596 iterators = NULL_TREE;
40597 else
40598 TREE_VEC_ELT (iterators, 5) = block;
40601 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
40603 OMP_CLAUSE_DEPEND_KIND (c) = kind;
40604 if (iterators)
40605 OMP_CLAUSE_DECL (c)
40606 = build_tree_list (iterators, OMP_CLAUSE_DECL (c));
40609 return nlist;
40611 invalid_kind:
40612 cp_parser_error (parser, "invalid depend kind");
40613 resync_fail:
40614 if (iterators)
40615 poplevel (0, 1, 0);
40616 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40617 /*or_comma=*/false,
40618 /*consume_paren=*/true);
40619 return list;
40622 /* OpenMP 5.2:
40623 doacross ( source : )
40624 doacross ( source : omp_cur_iteration )
40626 doacross ( sink : vec )
40627 doacross ( sink : omp_cur_iteration - logical_iteration ) */
40629 static tree
40630 cp_parser_omp_clause_doacross (cp_parser *parser, tree list, location_t loc)
40632 tree nlist;
40633 enum omp_clause_doacross_kind kind = OMP_CLAUSE_DOACROSS_LAST;
40635 matching_parens parens;
40636 if (!parens.require_open (parser))
40637 return list;
40639 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
40641 invalid_kind:
40642 cp_parser_error (parser, "invalid doacross kind");
40643 resync_fail:
40644 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40645 /*or_comma=*/false,
40646 /*consume_paren=*/true);
40647 return list;
40650 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40651 const char *p = IDENTIFIER_POINTER (id);
40653 if (strcmp ("sink", p) == 0)
40654 kind = OMP_CLAUSE_DOACROSS_SINK;
40655 else if (strcmp ("source", p) == 0)
40656 kind = OMP_CLAUSE_DOACROSS_SOURCE;
40657 else
40658 goto invalid_kind;
40660 cp_lexer_consume_token (parser->lexer);
40662 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
40663 goto resync_fail;
40665 if (kind == OMP_CLAUSE_DOACROSS_SOURCE)
40667 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40669 id = cp_lexer_peek_token (parser->lexer)->u.value;
40670 p = IDENTIFIER_POINTER (id);
40671 if (strcmp (p, "omp_cur_iteration") == 0)
40672 cp_lexer_consume_token (parser->lexer);
40674 nlist = build_omp_clause (loc, OMP_CLAUSE_DOACROSS);
40675 OMP_CLAUSE_DOACROSS_KIND (nlist) = OMP_CLAUSE_DOACROSS_SOURCE;
40676 OMP_CLAUSE_DECL (nlist) = NULL_TREE;
40677 OMP_CLAUSE_CHAIN (nlist) = list;
40679 else
40680 nlist = cp_parser_omp_clause_doacross_sink (parser, loc, list, false);
40682 if (!parens.require_close (parser))
40683 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40684 /*or_comma=*/false,
40685 /*consume_paren=*/true);
40686 return nlist;
40689 /* OpenMP 4.0:
40690 from ( variable-list )
40691 to ( variable-list )
40693 OpenMP 5.1:
40694 from ( [present :] variable-list )
40695 to ( [present :] variable-list ) */
40697 static tree
40698 cp_parser_omp_clause_from_to (cp_parser *parser, enum omp_clause_code kind,
40699 tree list)
40701 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
40702 return list;
40704 bool present = false;
40705 cp_token *token = cp_lexer_peek_token (parser->lexer);
40707 if (token->type == CPP_NAME
40708 && strcmp (IDENTIFIER_POINTER (token->u.value), "present") == 0
40709 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
40711 present = true;
40712 cp_lexer_consume_token (parser->lexer);
40713 cp_lexer_consume_token (parser->lexer);
40716 tree nl = cp_parser_omp_var_list_no_open (parser, kind, list, NULL, true);
40717 if (present)
40718 for (tree c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
40719 OMP_CLAUSE_MOTION_PRESENT (c) = 1;
40721 return nl;
40724 /* OpenMP 4.0:
40725 map ( map-kind : variable-list )
40726 map ( variable-list )
40728 map-kind:
40729 alloc | to | from | tofrom
40731 OpenMP 4.5:
40732 map-kind:
40733 alloc | to | from | tofrom | release | delete
40735 map ( always [,] map-kind: variable-list )
40737 OpenMP 5.0:
40738 map ( [map-type-modifier[,] ...] map-kind: variable-list )
40740 map-type-modifier:
40741 always | close */
40743 static tree
40744 cp_parser_omp_clause_map (cp_parser *parser, tree list)
40746 tree nlist, c;
40747 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
40749 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
40750 return list;
40752 int pos = 1;
40753 int map_kind_pos = 0;
40754 while (cp_lexer_peek_nth_token (parser->lexer, pos)->type == CPP_NAME
40755 || cp_lexer_peek_nth_token (parser->lexer, pos)->keyword == RID_DELETE)
40757 if (cp_lexer_peek_nth_token (parser->lexer, pos + 1)->type == CPP_COLON)
40759 map_kind_pos = pos;
40760 break;
40763 if (cp_lexer_peek_nth_token (parser->lexer, pos + 1)->type == CPP_COMMA)
40764 pos++;
40765 pos++;
40768 bool always_modifier = false;
40769 bool close_modifier = false;
40770 bool present_modifier = false;
40771 for (int pos = 1; pos < map_kind_pos; ++pos)
40773 cp_token *tok = cp_lexer_peek_token (parser->lexer);
40774 if (tok->type == CPP_COMMA)
40776 cp_lexer_consume_token (parser->lexer);
40777 continue;
40780 const char *p = IDENTIFIER_POINTER (tok->u.value);
40781 if (strcmp ("always", p) == 0)
40783 if (always_modifier)
40785 cp_parser_error (parser, "too many %<always%> modifiers");
40786 cp_parser_skip_to_closing_parenthesis (parser,
40787 /*recovering=*/true,
40788 /*or_comma=*/false,
40789 /*consume_paren=*/true);
40790 return list;
40792 always_modifier = true;
40794 else if (strcmp ("close", p) == 0)
40796 if (close_modifier)
40798 cp_parser_error (parser, "too many %<close%> modifiers");
40799 cp_parser_skip_to_closing_parenthesis (parser,
40800 /*recovering=*/true,
40801 /*or_comma=*/false,
40802 /*consume_paren=*/true);
40803 return list;
40805 close_modifier = true;
40807 else if (strcmp ("present", p) == 0)
40809 if (present_modifier)
40811 cp_parser_error (parser, "too many %<present%> modifiers");
40812 cp_parser_skip_to_closing_parenthesis (parser,
40813 /*recovering=*/true,
40814 /*or_comma=*/false,
40815 /*consume_paren=*/true);
40816 return list;
40818 present_modifier = true;
40820 else
40822 cp_parser_error (parser, "%<map%> clause with map-type modifier other"
40823 " than %<always%>, %<close%> or %<present%>");
40824 cp_parser_skip_to_closing_parenthesis (parser,
40825 /*recovering=*/true,
40826 /*or_comma=*/false,
40827 /*consume_paren=*/true);
40828 return list;
40831 cp_lexer_consume_token (parser->lexer);
40834 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
40835 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
40837 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40838 const char *p = IDENTIFIER_POINTER (id);
40839 int always_present_modifier = always_modifier && present_modifier;
40841 if (strcmp ("alloc", p) == 0)
40842 kind = present_modifier ? GOMP_MAP_PRESENT_ALLOC : GOMP_MAP_ALLOC;
40843 else if (strcmp ("to", p) == 0)
40844 kind = (always_present_modifier ? GOMP_MAP_ALWAYS_PRESENT_TO
40845 : present_modifier ? GOMP_MAP_PRESENT_TO
40846 : always_modifier ? GOMP_MAP_ALWAYS_TO
40847 : GOMP_MAP_TO);
40848 else if (strcmp ("from", p) == 0)
40849 kind = (always_present_modifier ? GOMP_MAP_ALWAYS_PRESENT_FROM
40850 : present_modifier ? GOMP_MAP_PRESENT_FROM
40851 : always_modifier ? GOMP_MAP_ALWAYS_FROM
40852 : GOMP_MAP_FROM);
40853 else if (strcmp ("tofrom", p) == 0)
40854 kind = (always_present_modifier ? GOMP_MAP_ALWAYS_PRESENT_TOFROM
40855 : present_modifier ? GOMP_MAP_PRESENT_TOFROM
40856 : always_modifier ? GOMP_MAP_ALWAYS_TOFROM
40857 : GOMP_MAP_TOFROM);
40858 else if (strcmp ("release", p) == 0)
40859 kind = GOMP_MAP_RELEASE;
40860 else
40862 cp_parser_error (parser, "invalid map kind");
40863 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40864 /*or_comma=*/false,
40865 /*consume_paren=*/true);
40866 return list;
40868 cp_lexer_consume_token (parser->lexer);
40869 cp_lexer_consume_token (parser->lexer);
40871 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
40872 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
40874 kind = GOMP_MAP_DELETE;
40875 cp_lexer_consume_token (parser->lexer);
40876 cp_lexer_consume_token (parser->lexer);
40879 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
40880 NULL, true);
40882 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
40883 OMP_CLAUSE_SET_MAP_KIND (c, kind);
40885 return nlist;
40888 /* OpenMP 4.0:
40889 device ( expression )
40891 OpenMP 5.0:
40892 device ( [device-modifier :] integer-expression )
40894 device-modifier:
40895 ancestor | device_num */
40897 static tree
40898 cp_parser_omp_clause_device (cp_parser *parser, tree list,
40899 location_t location)
40901 tree t, c;
40902 bool ancestor = false;
40904 matching_parens parens;
40905 if (!parens.require_open (parser))
40906 return list;
40908 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
40909 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
40911 cp_token *tok = cp_lexer_peek_token (parser->lexer);
40912 const char *p = IDENTIFIER_POINTER (tok->u.value);
40913 if (strcmp ("ancestor", p) == 0)
40915 ancestor = true;
40917 /* A requires directive with the reverse_offload clause must be
40918 specified. */
40919 if ((omp_requires_mask & OMP_REQUIRES_REVERSE_OFFLOAD) == 0)
40921 error_at (tok->location, "%<ancestor%> device modifier not "
40922 "preceded by %<requires%> directive "
40923 "with %<reverse_offload%> clause");
40924 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
40925 return list;
40928 else if (strcmp ("device_num", p) == 0)
40930 else
40932 error_at (tok->location, "expected %<ancestor%> or %<device_num%>");
40933 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
40934 return list;
40936 cp_lexer_consume_token (parser->lexer);
40937 cp_lexer_consume_token (parser->lexer);
40940 t = cp_parser_assignment_expression (parser);
40942 if (t == error_mark_node
40943 || !parens.require_close (parser))
40944 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40945 /*or_comma=*/false,
40946 /*consume_paren=*/true);
40948 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
40949 "device", location);
40951 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
40952 OMP_CLAUSE_DEVICE_ID (c) = t;
40953 OMP_CLAUSE_CHAIN (c) = list;
40954 OMP_CLAUSE_DEVICE_ANCESTOR (c) = ancestor;
40956 return c;
40959 /* OpenMP 4.0:
40960 dist_schedule ( static )
40961 dist_schedule ( static , expression ) */
40963 static tree
40964 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
40965 location_t location)
40967 tree c, t;
40969 matching_parens parens;
40970 if (!parens.require_open (parser))
40971 return list;
40973 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
40975 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
40976 goto invalid_kind;
40977 cp_lexer_consume_token (parser->lexer);
40979 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
40981 cp_lexer_consume_token (parser->lexer);
40983 t = cp_parser_assignment_expression (parser);
40985 if (t == error_mark_node)
40986 goto resync_fail;
40987 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
40989 if (!parens.require_close (parser))
40990 goto resync_fail;
40992 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
40993 goto resync_fail;
40995 /* check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE,
40996 "dist_schedule", location); */
40997 if (omp_find_clause (list, OMP_CLAUSE_DIST_SCHEDULE))
40998 warning_at (location, 0, "too many %qs clauses", "dist_schedule");
40999 OMP_CLAUSE_CHAIN (c) = list;
41000 return c;
41002 invalid_kind:
41003 cp_parser_error (parser, "invalid dist_schedule kind");
41004 resync_fail:
41005 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41006 /*or_comma=*/false,
41007 /*consume_paren=*/true);
41008 return list;
41011 /* OpenMP 4.0:
41012 proc_bind ( proc-bind-kind )
41014 proc-bind-kind:
41015 primary | master | close | spread
41016 where OpenMP 5.1 added 'primary' and deprecated the alias 'master'. */
41018 static tree
41019 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
41020 location_t location)
41022 tree c;
41023 enum omp_clause_proc_bind_kind kind;
41025 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
41026 return list;
41028 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41030 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41031 const char *p = IDENTIFIER_POINTER (id);
41033 if (strcmp ("primary", p) == 0)
41034 kind = OMP_CLAUSE_PROC_BIND_PRIMARY;
41035 else if (strcmp ("master", p) == 0)
41036 kind = OMP_CLAUSE_PROC_BIND_MASTER;
41037 else if (strcmp ("close", p) == 0)
41038 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
41039 else if (strcmp ("spread", p) == 0)
41040 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
41041 else
41042 goto invalid_kind;
41044 else
41045 goto invalid_kind;
41047 cp_lexer_consume_token (parser->lexer);
41048 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
41049 goto resync_fail;
41051 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
41052 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
41053 location);
41054 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
41055 OMP_CLAUSE_CHAIN (c) = list;
41056 return c;
41058 invalid_kind:
41059 cp_parser_error (parser, "invalid depend kind");
41060 resync_fail:
41061 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41062 /*or_comma=*/false,
41063 /*consume_paren=*/true);
41064 return list;
41067 /* OpenMP 5.0:
41068 device_type ( host | nohost | any ) */
41070 static tree
41071 cp_parser_omp_clause_device_type (cp_parser *parser, tree list,
41072 location_t location)
41074 tree c;
41075 enum omp_clause_device_type_kind kind;
41077 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
41078 return list;
41080 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41082 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41083 const char *p = IDENTIFIER_POINTER (id);
41085 if (strcmp ("host", p) == 0)
41086 kind = OMP_CLAUSE_DEVICE_TYPE_HOST;
41087 else if (strcmp ("nohost", p) == 0)
41088 kind = OMP_CLAUSE_DEVICE_TYPE_NOHOST;
41089 else if (strcmp ("any", p) == 0)
41090 kind = OMP_CLAUSE_DEVICE_TYPE_ANY;
41091 else
41092 goto invalid_kind;
41094 else
41095 goto invalid_kind;
41097 cp_lexer_consume_token (parser->lexer);
41098 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
41099 goto resync_fail;
41101 c = build_omp_clause (location, OMP_CLAUSE_DEVICE_TYPE);
41102 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE_TYPE, "device_type",
41103 location);
41104 OMP_CLAUSE_DEVICE_TYPE_KIND (c) = kind;
41105 OMP_CLAUSE_CHAIN (c) = list;
41106 return c;
41108 invalid_kind:
41109 cp_parser_error (parser, "invalid depend kind");
41110 resync_fail:
41111 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41112 /*or_comma=*/false,
41113 /*consume_paren=*/true);
41114 return list;
41117 /* OpenACC:
41118 async [( int-expr )] */
41120 static tree
41121 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
41123 tree c, t;
41124 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
41126 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
41128 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
41130 matching_parens parens;
41131 parens.consume_open (parser);
41133 t = cp_parser_assignment_expression (parser);
41134 if (t == error_mark_node
41135 || !parens.require_close (parser))
41136 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41137 /*or_comma=*/false,
41138 /*consume_paren=*/true);
41141 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
41143 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
41144 OMP_CLAUSE_ASYNC_EXPR (c) = t;
41145 OMP_CLAUSE_CHAIN (c) = list;
41146 list = c;
41148 return list;
41151 /* Parse all OpenACC clauses. The set clauses allowed by the directive
41152 is a bitmask in MASK. Return the list of clauses found. */
41154 static tree
41155 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
41156 const char *where, cp_token *pragma_tok,
41157 bool finish_p = true)
41159 tree clauses = NULL;
41160 bool first = true;
41162 /* Don't create location wrapper nodes within OpenACC clauses. */
41163 auto_suppress_location_wrappers sentinel;
41165 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
41167 location_t here;
41168 pragma_omp_clause c_kind;
41169 omp_clause_code code;
41170 const char *c_name;
41171 tree prev = clauses;
41173 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
41174 cp_lexer_consume_token (parser->lexer);
41176 here = cp_lexer_peek_token (parser->lexer)->location;
41177 c_kind = cp_parser_omp_clause_name (parser);
41179 switch (c_kind)
41181 case PRAGMA_OACC_CLAUSE_ASYNC:
41182 clauses = cp_parser_oacc_clause_async (parser, clauses);
41183 c_name = "async";
41184 break;
41185 case PRAGMA_OACC_CLAUSE_AUTO:
41186 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_AUTO,
41187 clauses);
41188 c_name = "auto";
41189 break;
41190 case PRAGMA_OACC_CLAUSE_ATTACH:
41191 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41192 c_name = "attach";
41193 break;
41194 case PRAGMA_OACC_CLAUSE_COLLAPSE:
41195 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
41196 c_name = "collapse";
41197 break;
41198 case PRAGMA_OACC_CLAUSE_COPY:
41199 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41200 c_name = "copy";
41201 break;
41202 case PRAGMA_OACC_CLAUSE_COPYIN:
41203 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41204 c_name = "copyin";
41205 break;
41206 case PRAGMA_OACC_CLAUSE_COPYOUT:
41207 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41208 c_name = "copyout";
41209 break;
41210 case PRAGMA_OACC_CLAUSE_CREATE:
41211 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41212 c_name = "create";
41213 break;
41214 case PRAGMA_OACC_CLAUSE_DELETE:
41215 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41216 c_name = "delete";
41217 break;
41218 case PRAGMA_OMP_CLAUSE_DEFAULT:
41219 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
41220 c_name = "default";
41221 break;
41222 case PRAGMA_OACC_CLAUSE_DETACH:
41223 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41224 c_name = "detach";
41225 break;
41226 case PRAGMA_OACC_CLAUSE_DEVICE:
41227 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41228 c_name = "device";
41229 break;
41230 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
41231 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
41232 c_name = "deviceptr";
41233 break;
41234 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
41235 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41236 c_name = "device_resident";
41237 break;
41238 case PRAGMA_OACC_CLAUSE_FINALIZE:
41239 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_FINALIZE,
41240 clauses);
41241 c_name = "finalize";
41242 break;
41243 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
41244 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
41245 clauses);
41246 c_name = "firstprivate";
41247 break;
41248 case PRAGMA_OACC_CLAUSE_GANG:
41249 c_name = "gang";
41250 clauses = cp_parser_oacc_shape_clause (parser, here, OMP_CLAUSE_GANG,
41251 c_name, clauses);
41252 break;
41253 case PRAGMA_OACC_CLAUSE_HOST:
41254 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41255 c_name = "host";
41256 break;
41257 case PRAGMA_OACC_CLAUSE_IF:
41258 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
41259 c_name = "if";
41260 break;
41261 case PRAGMA_OACC_CLAUSE_IF_PRESENT:
41262 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_IF_PRESENT,
41263 clauses);
41264 c_name = "if_present";
41265 break;
41266 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
41267 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_INDEPENDENT,
41268 clauses);
41269 c_name = "independent";
41270 break;
41271 case PRAGMA_OACC_CLAUSE_LINK:
41272 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41273 c_name = "link";
41274 break;
41275 case PRAGMA_OACC_CLAUSE_NO_CREATE:
41276 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41277 c_name = "no_create";
41278 break;
41279 case PRAGMA_OACC_CLAUSE_NOHOST:
41280 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_NOHOST,
41281 clauses);
41282 c_name = "nohost";
41283 break;
41284 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
41285 code = OMP_CLAUSE_NUM_GANGS;
41286 c_name = "num_gangs";
41287 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
41288 clauses);
41289 break;
41290 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
41291 c_name = "num_workers";
41292 code = OMP_CLAUSE_NUM_WORKERS;
41293 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
41294 clauses);
41295 break;
41296 case PRAGMA_OACC_CLAUSE_PRESENT:
41297 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41298 c_name = "present";
41299 break;
41300 case PRAGMA_OACC_CLAUSE_PRIVATE:
41301 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
41302 clauses);
41303 c_name = "private";
41304 break;
41305 case PRAGMA_OACC_CLAUSE_REDUCTION:
41306 clauses
41307 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
41308 false, clauses);
41309 c_name = "reduction";
41310 break;
41311 case PRAGMA_OACC_CLAUSE_SEQ:
41312 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_SEQ,
41313 clauses);
41314 c_name = "seq";
41315 break;
41316 case PRAGMA_OACC_CLAUSE_TILE:
41317 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
41318 c_name = "tile";
41319 break;
41320 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
41321 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
41322 clauses);
41323 c_name = "use_device";
41324 break;
41325 case PRAGMA_OACC_CLAUSE_VECTOR:
41326 c_name = "vector";
41327 clauses = cp_parser_oacc_shape_clause (parser, here,
41328 OMP_CLAUSE_VECTOR,
41329 c_name, clauses);
41330 break;
41331 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
41332 c_name = "vector_length";
41333 code = OMP_CLAUSE_VECTOR_LENGTH;
41334 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
41335 clauses);
41336 break;
41337 case PRAGMA_OACC_CLAUSE_WAIT:
41338 clauses = cp_parser_oacc_clause_wait (parser, clauses);
41339 c_name = "wait";
41340 break;
41341 case PRAGMA_OACC_CLAUSE_WORKER:
41342 c_name = "worker";
41343 clauses = cp_parser_oacc_shape_clause (parser, here,
41344 OMP_CLAUSE_WORKER,
41345 c_name, clauses);
41346 break;
41347 default:
41348 cp_parser_error (parser, "expected an OpenACC clause");
41349 goto saw_error;
41352 first = false;
41354 if (((mask >> c_kind) & 1) == 0)
41356 /* Remove the invalid clause(s) from the list to avoid
41357 confusing the rest of the compiler. */
41358 clauses = prev;
41359 error_at (here, "%qs is not valid for %qs", c_name, where);
41363 saw_error:
41364 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41366 if (finish_p)
41367 return finish_omp_clauses (clauses, C_ORT_ACC);
41369 return clauses;
41372 /* Parse all OpenMP clauses. The set clauses allowed by the directive
41373 is a bitmask in MASK. Return the list of clauses found.
41374 FINISH_P set if finish_omp_clauses should be called.
41375 NESTED non-zero if clauses should be terminated by closing paren instead
41376 of end of pragma. If it is 2, additionally commas are required in between
41377 the clauses. */
41379 static tree
41380 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
41381 const char *where, cp_token *pragma_tok,
41382 bool finish_p = true, int nested = 0)
41384 tree clauses = NULL;
41385 bool first = true;
41386 cp_token *token = NULL;
41388 /* Don't create location wrapper nodes within OpenMP clauses. */
41389 auto_suppress_location_wrappers sentinel;
41391 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
41393 pragma_omp_clause c_kind;
41394 const char *c_name;
41395 tree prev = clauses;
41397 if (nested && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
41398 break;
41400 if (!first || nested != 2)
41402 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
41403 cp_lexer_consume_token (parser->lexer);
41404 else if (nested == 2)
41405 error_at (cp_lexer_peek_token (parser->lexer)->location,
41406 "clauses in %<simd%> trait should be separated "
41407 "by %<,%>");
41410 token = cp_lexer_peek_token (parser->lexer);
41411 c_kind = cp_parser_omp_clause_name (parser);
41413 switch (c_kind)
41415 case PRAGMA_OMP_CLAUSE_BIND:
41416 clauses = cp_parser_omp_clause_bind (parser, clauses,
41417 token->location);
41418 c_name = "bind";
41419 break;
41420 case PRAGMA_OMP_CLAUSE_COLLAPSE:
41421 clauses = cp_parser_omp_clause_collapse (parser, clauses,
41422 token->location);
41423 c_name = "collapse";
41424 break;
41425 case PRAGMA_OMP_CLAUSE_COPYIN:
41426 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
41427 c_name = "copyin";
41428 break;
41429 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
41430 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
41431 clauses);
41432 c_name = "copyprivate";
41433 break;
41434 case PRAGMA_OMP_CLAUSE_DEFAULT:
41435 clauses = cp_parser_omp_clause_default (parser, clauses,
41436 token->location, false);
41437 c_name = "default";
41438 break;
41439 case PRAGMA_OMP_CLAUSE_FILTER:
41440 clauses = cp_parser_omp_clause_filter (parser, clauses,
41441 token->location);
41442 c_name = "filter";
41443 break;
41444 case PRAGMA_OMP_CLAUSE_FINAL:
41445 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
41446 c_name = "final";
41447 break;
41448 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
41449 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
41450 clauses);
41451 c_name = "firstprivate";
41452 break;
41453 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
41454 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
41455 token->location);
41456 c_name = "grainsize";
41457 break;
41458 case PRAGMA_OMP_CLAUSE_HINT:
41459 clauses = cp_parser_omp_clause_hint (parser, clauses,
41460 token->location);
41461 c_name = "hint";
41462 break;
41463 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
41464 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
41465 token->location);
41466 c_name = "defaultmap";
41467 break;
41468 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
41469 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
41470 clauses);
41471 c_name = "use_device_ptr";
41472 break;
41473 case PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR:
41474 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_ADDR,
41475 clauses);
41476 c_name = "use_device_addr";
41477 break;
41478 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
41479 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
41480 clauses);
41481 c_name = "is_device_ptr";
41482 break;
41483 case PRAGMA_OMP_CLAUSE_HAS_DEVICE_ADDR:
41484 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_HAS_DEVICE_ADDR,
41485 clauses);
41486 c_name = "has_device_addr";
41487 break;
41488 case PRAGMA_OMP_CLAUSE_IF:
41489 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
41490 true);
41491 c_name = "if";
41492 break;
41493 case PRAGMA_OMP_CLAUSE_IN_REDUCTION:
41494 clauses
41495 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_IN_REDUCTION,
41496 true, clauses);
41497 c_name = "in_reduction";
41498 break;
41499 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
41500 clauses = cp_parser_omp_clause_lastprivate (parser, clauses);
41501 c_name = "lastprivate";
41502 break;
41503 case PRAGMA_OMP_CLAUSE_MERGEABLE:
41504 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
41505 token->location);
41506 c_name = "mergeable";
41507 break;
41508 case PRAGMA_OMP_CLAUSE_NOWAIT:
41509 clauses = cp_parser_omp_clause_nowait (parser, clauses,
41510 token->location);
41511 c_name = "nowait";
41512 break;
41513 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
41514 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
41515 token->location);
41516 c_name = "num_tasks";
41517 break;
41518 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
41519 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
41520 token->location);
41521 c_name = "num_threads";
41522 break;
41523 case PRAGMA_OMP_CLAUSE_ORDER:
41524 clauses = cp_parser_omp_clause_order (parser, clauses,
41525 token->location);
41526 c_name = "order";
41527 break;
41528 case PRAGMA_OMP_CLAUSE_ORDERED:
41529 clauses = cp_parser_omp_clause_ordered (parser, clauses,
41530 token->location);
41531 c_name = "ordered";
41532 break;
41533 case PRAGMA_OMP_CLAUSE_PRIORITY:
41534 clauses = cp_parser_omp_clause_priority (parser, clauses,
41535 token->location);
41536 c_name = "priority";
41537 break;
41538 case PRAGMA_OMP_CLAUSE_PRIVATE:
41539 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
41540 clauses);
41541 c_name = "private";
41542 break;
41543 case PRAGMA_OMP_CLAUSE_REDUCTION:
41544 clauses
41545 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
41546 true, clauses);
41547 c_name = "reduction";
41548 break;
41549 case PRAGMA_OMP_CLAUSE_SCHEDULE:
41550 clauses = cp_parser_omp_clause_schedule (parser, clauses,
41551 token->location);
41552 c_name = "schedule";
41553 break;
41554 case PRAGMA_OMP_CLAUSE_SHARED:
41555 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
41556 clauses);
41557 c_name = "shared";
41558 break;
41559 case PRAGMA_OMP_CLAUSE_TASK_REDUCTION:
41560 clauses
41561 = cp_parser_omp_clause_reduction (parser,
41562 OMP_CLAUSE_TASK_REDUCTION,
41563 true, clauses);
41564 c_name = "task_reduction";
41565 break;
41566 case PRAGMA_OMP_CLAUSE_UNTIED:
41567 clauses = cp_parser_omp_clause_untied (parser, clauses,
41568 token->location);
41569 c_name = "untied";
41570 break;
41571 case PRAGMA_OMP_CLAUSE_INBRANCH:
41572 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
41573 clauses, token->location);
41574 c_name = "inbranch";
41575 break;
41576 case PRAGMA_OMP_CLAUSE_NONTEMPORAL:
41577 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_NONTEMPORAL,
41578 clauses);
41579 c_name = "nontemporal";
41580 break;
41581 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
41582 clauses = cp_parser_omp_clause_branch (parser,
41583 OMP_CLAUSE_NOTINBRANCH,
41584 clauses, token->location);
41585 c_name = "notinbranch";
41586 break;
41587 case PRAGMA_OMP_CLAUSE_PARALLEL:
41588 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
41589 clauses, token->location);
41590 c_name = "parallel";
41591 if (!first)
41593 clause_not_first:
41594 error_at (token->location, "%qs must be the first clause of %qs",
41595 c_name, where);
41596 clauses = prev;
41598 break;
41599 case PRAGMA_OMP_CLAUSE_FOR:
41600 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
41601 clauses, token->location);
41602 c_name = "for";
41603 if (!first)
41604 goto clause_not_first;
41605 break;
41606 case PRAGMA_OMP_CLAUSE_SECTIONS:
41607 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
41608 clauses, token->location);
41609 c_name = "sections";
41610 if (!first)
41611 goto clause_not_first;
41612 break;
41613 case PRAGMA_OMP_CLAUSE_TASKGROUP:
41614 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
41615 clauses, token->location);
41616 c_name = "taskgroup";
41617 if (!first)
41618 goto clause_not_first;
41619 break;
41620 case PRAGMA_OMP_CLAUSE_LINK:
41621 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
41622 c_name = "link";
41623 break;
41624 case PRAGMA_OMP_CLAUSE_TO:
41625 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
41627 tree nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_ENTER,
41628 clauses);
41629 for (tree c = nl; c != clauses; c = OMP_CLAUSE_CHAIN (c))
41630 OMP_CLAUSE_ENTER_TO (c) = 1;
41631 clauses = nl;
41633 else
41634 clauses = cp_parser_omp_clause_from_to (parser, OMP_CLAUSE_TO,
41635 clauses);
41636 c_name = "to";
41637 break;
41638 case PRAGMA_OMP_CLAUSE_FROM:
41639 clauses = cp_parser_omp_clause_from_to (parser, OMP_CLAUSE_FROM,
41640 clauses);
41641 c_name = "from";
41642 break;
41643 case PRAGMA_OMP_CLAUSE_UNIFORM:
41644 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
41645 clauses);
41646 c_name = "uniform";
41647 break;
41648 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
41649 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
41650 token->location);
41651 c_name = "num_teams";
41652 break;
41653 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
41654 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
41655 token->location);
41656 c_name = "thread_limit";
41657 break;
41658 case PRAGMA_OMP_CLAUSE_ALIGNED:
41659 clauses = cp_parser_omp_clause_aligned (parser, clauses);
41660 c_name = "aligned";
41661 break;
41662 case PRAGMA_OMP_CLAUSE_ALLOCATE:
41663 clauses = cp_parser_omp_clause_allocate (parser, clauses);
41664 c_name = "allocate";
41665 break;
41666 case PRAGMA_OMP_CLAUSE_LINEAR:
41668 bool declare_simd = false;
41669 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
41670 declare_simd = true;
41671 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
41673 c_name = "linear";
41674 break;
41675 case PRAGMA_OMP_CLAUSE_AFFINITY:
41676 clauses = cp_parser_omp_clause_affinity (parser, clauses);
41677 c_name = "affinity";
41678 break;
41679 case PRAGMA_OMP_CLAUSE_DEPEND:
41680 clauses = cp_parser_omp_clause_depend (parser, clauses,
41681 token->location);
41682 c_name = "depend";
41683 break;
41684 case PRAGMA_OMP_CLAUSE_DOACROSS:
41685 clauses = cp_parser_omp_clause_doacross (parser, clauses,
41686 token->location);
41687 c_name = "doacross";
41688 break;
41689 case PRAGMA_OMP_CLAUSE_DETACH:
41690 clauses = cp_parser_omp_clause_detach (parser, clauses);
41691 c_name = "detach";
41692 break;
41693 case PRAGMA_OMP_CLAUSE_MAP:
41694 clauses = cp_parser_omp_clause_map (parser, clauses);
41695 c_name = "map";
41696 break;
41697 case PRAGMA_OMP_CLAUSE_DEVICE:
41698 clauses = cp_parser_omp_clause_device (parser, clauses,
41699 token->location);
41700 c_name = "device";
41701 break;
41702 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
41703 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
41704 token->location);
41705 c_name = "dist_schedule";
41706 break;
41707 case PRAGMA_OMP_CLAUSE_PROC_BIND:
41708 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
41709 token->location);
41710 c_name = "proc_bind";
41711 break;
41712 case PRAGMA_OMP_CLAUSE_DEVICE_TYPE:
41713 clauses = cp_parser_omp_clause_device_type (parser, clauses,
41714 token->location);
41715 c_name = "device_type";
41716 break;
41717 case PRAGMA_OMP_CLAUSE_SAFELEN:
41718 clauses = cp_parser_omp_clause_safelen (parser, clauses,
41719 token->location);
41720 c_name = "safelen";
41721 break;
41722 case PRAGMA_OMP_CLAUSE_SIMDLEN:
41723 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
41724 token->location);
41725 c_name = "simdlen";
41726 break;
41727 case PRAGMA_OMP_CLAUSE_NOGROUP:
41728 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
41729 token->location);
41730 c_name = "nogroup";
41731 break;
41732 case PRAGMA_OMP_CLAUSE_THREADS:
41733 clauses
41734 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
41735 clauses, token->location);
41736 c_name = "threads";
41737 break;
41738 case PRAGMA_OMP_CLAUSE_SIMD:
41739 clauses
41740 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
41741 clauses, token->location);
41742 c_name = "simd";
41743 break;
41744 case PRAGMA_OMP_CLAUSE_ENTER:
41745 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_ENTER,
41746 clauses);
41747 c_name = "enter";
41748 break;
41749 default:
41750 cp_parser_error (parser, "expected an OpenMP clause");
41751 goto saw_error;
41754 first = false;
41756 if (((mask >> c_kind) & 1) == 0)
41758 /* Remove the invalid clause(s) from the list to avoid
41759 confusing the rest of the compiler. */
41760 clauses = prev;
41761 error_at (token->location, "%qs is not valid for %qs", c_name, where);
41764 saw_error:
41765 if (!nested)
41766 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41767 if (finish_p)
41769 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
41770 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
41771 else
41772 return finish_omp_clauses (clauses, C_ORT_OMP);
41774 return clauses;
41777 /* OpenMP 2.5:
41778 structured-block:
41779 statement
41781 In practice, we're also interested in adding the statement to an
41782 outer node. So it is convenient if we work around the fact that
41783 cp_parser_statement calls add_stmt. */
41785 static unsigned
41786 cp_parser_begin_omp_structured_block (cp_parser *parser)
41788 unsigned save = parser->in_statement;
41790 /* Only move the values to IN_OMP_BLOCK if they weren't false.
41791 This preserves the "not within loop or switch" style error messages
41792 for nonsense cases like
41793 void foo() {
41794 #pragma omp single
41795 break;
41798 if (parser->in_statement)
41799 parser->in_statement = IN_OMP_BLOCK;
41801 return save;
41804 static void
41805 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
41807 parser->in_statement = save;
41810 static tree
41811 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
41813 tree stmt = begin_omp_structured_block ();
41814 unsigned int save = cp_parser_begin_omp_structured_block (parser);
41816 parser->omp_attrs_forbidden_p = true;
41817 cp_parser_statement (parser, NULL_TREE, false, if_p);
41819 cp_parser_end_omp_structured_block (parser, save);
41820 return finish_omp_structured_block (stmt);
41823 /* OpenMP 5.x:
41824 # pragma omp allocate (list) clauses
41826 OpenMP 5.0 clause:
41827 allocator (omp_allocator_handle_t expression)
41829 OpenMP 5.1 additional clause:
41830 align (constant-expression)] */
41832 static void
41833 cp_parser_omp_allocate (cp_parser *parser, cp_token *pragma_tok)
41835 tree allocator = NULL_TREE;
41836 tree alignment = NULL_TREE;
41837 location_t loc = pragma_tok->location;
41838 tree nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_ALLOCATE, NULL_TREE);
41842 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
41843 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
41844 cp_lexer_consume_token (parser->lexer);
41846 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41847 break;
41848 matching_parens parens;
41849 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41850 const char *p = IDENTIFIER_POINTER (id);
41851 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
41852 cp_lexer_consume_token (parser->lexer);
41853 if (strcmp (p, "allocator") != 0 && strcmp (p, "align") != 0)
41855 error_at (cloc, "expected %<allocator%> or %<align%>");
41856 break;
41858 if (!parens.require_open (parser))
41859 break;
41860 tree expr = cp_parser_assignment_expression (parser);
41861 if (p[2] == 'i' && alignment)
41863 error_at (cloc, "too many %qs clauses", "align");
41864 break;
41866 else if (p[2] == 'i')
41868 if (expr != error_mark_node)
41869 alignment = expr;
41870 /* FIXME: Remove when adding check to semantics.cc; cf FIXME below. */
41871 if (alignment
41872 && !type_dependent_expression_p (alignment)
41873 && !INTEGRAL_TYPE_P (TREE_TYPE (alignment)))
41875 error_at (cloc, "%<align%> clause argument needs to be "
41876 "positive constant power of two integer "
41877 "expression");
41878 alignment = NULL_TREE;
41880 else if (alignment)
41882 alignment = mark_rvalue_use (alignment);
41883 if (!processing_template_decl)
41885 alignment = maybe_constant_value (alignment);
41886 if (TREE_CODE (alignment) != INTEGER_CST
41887 || !tree_fits_uhwi_p (alignment)
41888 || !integer_pow2p (alignment))
41890 error_at (cloc, "%<align%> clause argument needs to be "
41891 "positive constant power of two integer "
41892 "expression");
41893 alignment = NULL_TREE;
41898 else if (allocator)
41900 error_at (cloc, "too many %qs clauses", "allocator");
41901 break;
41903 else
41905 if (expr != error_mark_node)
41906 allocator = expr;
41908 parens.require_close (parser);
41909 } while (true);
41910 cp_parser_require_pragma_eol (parser, pragma_tok);
41912 if (allocator || alignment)
41913 for (tree c = nl; c != NULL_TREE; c = OMP_CLAUSE_CHAIN (c))
41915 OMP_CLAUSE_ALLOCATE_ALLOCATOR (c) = allocator;
41916 OMP_CLAUSE_ALLOCATE_ALIGN (c) = alignment;
41919 /* FIXME: When implementing properly, delete the align/allocate expr error
41920 check above and add one in semantics.cc (to properly handle templates).
41921 Base this on the allocator/align modifiers check for the 'allocate' clause
41922 in semantics.cc's finish_omp_clauses. */
41923 sorry_at (loc, "%<#pragma omp allocate%> not yet supported");
41926 /* OpenMP 2.5:
41927 # pragma omp atomic new-line
41928 expression-stmt
41930 expression-stmt:
41931 x binop= expr | x++ | ++x | x-- | --x
41932 binop:
41933 +, *, -, /, &, ^, |, <<, >>
41935 where x is an lvalue expression with scalar type.
41937 OpenMP 3.1:
41938 # pragma omp atomic new-line
41939 update-stmt
41941 # pragma omp atomic read new-line
41942 read-stmt
41944 # pragma omp atomic write new-line
41945 write-stmt
41947 # pragma omp atomic update new-line
41948 update-stmt
41950 # pragma omp atomic capture new-line
41951 capture-stmt
41953 # pragma omp atomic capture new-line
41954 capture-block
41956 read-stmt:
41957 v = x
41958 write-stmt:
41959 x = expr
41960 update-stmt:
41961 expression-stmt | x = x binop expr
41962 capture-stmt:
41963 v = expression-stmt
41964 capture-block:
41965 { v = x; update-stmt; } | { update-stmt; v = x; }
41967 OpenMP 4.0:
41968 update-stmt:
41969 expression-stmt | x = x binop expr | x = expr binop x
41970 capture-stmt:
41971 v = update-stmt
41972 capture-block:
41973 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
41975 OpenMP 5.1:
41976 # pragma omp atomic compare new-line
41977 conditional-update-atomic
41979 # pragma omp atomic compare capture new-line
41980 conditional-update-capture-atomic
41982 conditional-update-atomic:
41983 cond-expr-stmt | cond-update-stmt
41984 cond-expr-stmt:
41985 x = expr ordop x ? expr : x;
41986 x = x ordop expr ? expr : x;
41987 x = x == e ? d : x;
41988 cond-update-stmt:
41989 if (expr ordop x) { x = expr; }
41990 if (x ordop expr) { x = expr; }
41991 if (x == e) { x = d; }
41992 ordop:
41993 <, >
41994 conditional-update-capture-atomic:
41995 v = cond-expr-stmt
41996 { v = x; cond-expr-stmt }
41997 { cond-expr-stmt v = x; }
41998 { v = x; cond-update-stmt }
41999 { cond-update-stmt v = x; }
42000 if (x == e) { x = d; } else { v = x; }
42001 { r = x == e; if (r) { x = d; } }
42002 { r = x == e; if (r) { x = d; } else { v = x; } }
42004 where x, r and v are lvalue expressions with scalar type,
42005 expr, e and d are expressions with scalar type and e might be
42006 the same as v. */
42008 static void
42009 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok, bool openacc)
42011 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
42012 tree rhs1 = NULL_TREE, orig_lhs, r = NULL_TREE;
42013 location_t loc = pragma_tok->location;
42014 enum tree_code code = ERROR_MARK, opcode = NOP_EXPR;
42015 enum omp_memory_order memory_order = OMP_MEMORY_ORDER_UNSPECIFIED;
42016 bool structured_block = false;
42017 tree clauses = NULL_TREE;
42018 bool capture = false;
42019 bool compare = false;
42020 bool weak = false;
42021 enum omp_memory_order fail = OMP_MEMORY_ORDER_UNSPECIFIED;
42022 bool no_semicolon = false;
42023 bool extra_scope = false;
42025 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
42027 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
42028 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
42029 cp_lexer_consume_token (parser->lexer);
42031 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42033 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
42034 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
42035 const char *p = IDENTIFIER_POINTER (id);
42036 enum tree_code new_code = ERROR_MARK;
42037 enum omp_memory_order new_memory_order
42038 = OMP_MEMORY_ORDER_UNSPECIFIED;
42039 bool new_capture = false;
42040 bool new_compare = false;
42041 bool new_weak = false;
42042 enum omp_memory_order new_fail = OMP_MEMORY_ORDER_UNSPECIFIED;
42044 if (!strcmp (p, "read"))
42045 new_code = OMP_ATOMIC_READ;
42046 else if (!strcmp (p, "write"))
42047 new_code = NOP_EXPR;
42048 else if (!strcmp (p, "update"))
42049 new_code = OMP_ATOMIC;
42050 else if (openacc && !strcmp (p, "capture"))
42051 new_code = OMP_ATOMIC_CAPTURE_NEW;
42052 else if (openacc)
42054 p = NULL;
42055 error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
42056 "or %<capture%> clause");
42058 else if (!strcmp (p, "capture"))
42059 new_capture = true;
42060 else if (!strcmp (p, "compare"))
42061 new_compare = true;
42062 else if (!strcmp (p, "weak"))
42063 new_weak = true;
42064 else if (!strcmp (p, "fail"))
42066 matching_parens parens;
42068 cp_lexer_consume_token (parser->lexer);
42069 if (!parens.require_open (parser))
42070 continue;
42072 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42074 id = cp_lexer_peek_token (parser->lexer)->u.value;
42075 const char *q = IDENTIFIER_POINTER (id);
42077 if (!strcmp (q, "seq_cst"))
42078 new_fail = OMP_MEMORY_ORDER_SEQ_CST;
42079 else if (!strcmp (q, "acquire"))
42080 new_fail = OMP_MEMORY_ORDER_ACQUIRE;
42081 else if (!strcmp (q, "relaxed"))
42082 new_fail = OMP_MEMORY_ORDER_RELAXED;
42085 if (new_fail != OMP_MEMORY_ORDER_UNSPECIFIED)
42087 cp_lexer_consume_token (parser->lexer);
42088 if (fail != OMP_MEMORY_ORDER_UNSPECIFIED)
42089 error_at (cloc, "too many %qs clauses", "fail");
42090 else
42091 fail = new_fail;
42093 else
42094 cp_parser_error (parser, "expected %<seq_cst%>, %<acquire%> "
42095 "or %<relaxed%>");
42096 if (new_fail == OMP_MEMORY_ORDER_UNSPECIFIED
42097 || !parens.require_close (parser))
42098 cp_parser_skip_to_closing_parenthesis (parser,
42099 /*recovering=*/true,
42100 /*or_comma=*/false,
42101 /*consume_paren=*/true);
42102 continue;
42104 else if (!strcmp (p, "seq_cst"))
42105 new_memory_order = OMP_MEMORY_ORDER_SEQ_CST;
42106 else if (!strcmp (p, "acq_rel"))
42107 new_memory_order = OMP_MEMORY_ORDER_ACQ_REL;
42108 else if (!strcmp (p, "release"))
42109 new_memory_order = OMP_MEMORY_ORDER_RELEASE;
42110 else if (!strcmp (p, "acquire"))
42111 new_memory_order = OMP_MEMORY_ORDER_ACQUIRE;
42112 else if (!strcmp (p, "relaxed"))
42113 new_memory_order = OMP_MEMORY_ORDER_RELAXED;
42114 else if (!strcmp (p, "hint"))
42116 cp_lexer_consume_token (parser->lexer);
42117 clauses = cp_parser_omp_clause_hint (parser, clauses, cloc);
42118 continue;
42120 else
42122 p = NULL;
42123 error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
42124 "%<capture%>, %<compare%>, %<weak%>, %<fail%>, "
42125 "%<seq_cst%>, %<acq_rel%>, %<release%>, "
42126 "%<relaxed%> or %<hint%> clause");
42128 if (p)
42130 if (new_code != ERROR_MARK)
42132 /* OpenACC permits 'update capture'. */
42133 if (openacc
42134 && code == OMP_ATOMIC
42135 && new_code == OMP_ATOMIC_CAPTURE_NEW)
42136 code = new_code;
42137 else if (code != ERROR_MARK)
42138 error_at (cloc, "too many atomic clauses");
42139 else
42140 code = new_code;
42142 else if (new_memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
42144 if (memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
42145 error_at (cloc, "too many memory order clauses");
42146 else
42147 memory_order = new_memory_order;
42149 else if (new_capture)
42151 if (capture)
42152 error_at (cloc, "too many %qs clauses", "capture");
42153 else
42154 capture = true;
42156 else if (new_compare)
42158 if (compare)
42159 error_at (cloc, "too many %qs clauses", "compare");
42160 else
42161 compare = true;
42163 else if (new_weak)
42165 if (weak)
42166 error_at (cloc, "too many %qs clauses", "weak");
42167 else
42168 weak = true;
42170 cp_lexer_consume_token (parser->lexer);
42171 continue;
42174 break;
42176 cp_parser_require_pragma_eol (parser, pragma_tok);
42178 if (code == ERROR_MARK)
42179 code = OMP_ATOMIC;
42180 if (capture)
42182 if (code != OMP_ATOMIC)
42183 error_at (loc, "%qs clause is incompatible with %<read%> or %<write%> "
42184 "clauses", "capture");
42185 else
42186 code = OMP_ATOMIC_CAPTURE_NEW;
42188 if (compare && code != OMP_ATOMIC && code != OMP_ATOMIC_CAPTURE_NEW)
42190 error_at (loc, "%qs clause is incompatible with %<read%> or %<write%> "
42191 "clauses", "compare");
42192 compare = false;
42194 if (fail != OMP_MEMORY_ORDER_UNSPECIFIED && !compare)
42196 error_at (loc, "%qs clause requires %qs clause", "fail", "compare");
42197 fail = OMP_MEMORY_ORDER_UNSPECIFIED;
42199 if (weak && !compare)
42201 error_at (loc, "%qs clause requires %qs clause", "weak", "compare");
42202 weak = false;
42204 if (openacc)
42205 memory_order = OMP_MEMORY_ORDER_RELAXED;
42206 else if (memory_order == OMP_MEMORY_ORDER_UNSPECIFIED)
42208 omp_requires_mask
42209 = (enum omp_requires) (omp_requires_mask
42210 | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED);
42211 switch ((enum omp_memory_order)
42212 (omp_requires_mask & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER))
42214 case OMP_MEMORY_ORDER_UNSPECIFIED:
42215 case OMP_MEMORY_ORDER_RELAXED:
42216 memory_order = OMP_MEMORY_ORDER_RELAXED;
42217 break;
42218 case OMP_MEMORY_ORDER_SEQ_CST:
42219 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
42220 break;
42221 case OMP_MEMORY_ORDER_ACQ_REL:
42222 switch (code)
42224 case OMP_ATOMIC_READ:
42225 memory_order = OMP_MEMORY_ORDER_ACQUIRE;
42226 break;
42227 case NOP_EXPR: /* atomic write */
42228 memory_order = OMP_MEMORY_ORDER_RELEASE;
42229 break;
42230 default:
42231 memory_order = OMP_MEMORY_ORDER_ACQ_REL;
42232 break;
42234 break;
42235 default:
42236 gcc_unreachable ();
42239 else
42240 switch (code)
42242 case OMP_ATOMIC_READ:
42243 if (memory_order == OMP_MEMORY_ORDER_RELEASE)
42245 error_at (loc, "%<#pragma omp atomic read%> incompatible with "
42246 "%<release%> clause");
42247 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
42249 else if (memory_order == OMP_MEMORY_ORDER_ACQ_REL)
42250 memory_order = OMP_MEMORY_ORDER_ACQUIRE;
42251 break;
42252 case NOP_EXPR: /* atomic write */
42253 if (memory_order == OMP_MEMORY_ORDER_ACQUIRE)
42255 error_at (loc, "%<#pragma omp atomic write%> incompatible with "
42256 "%<acquire%> clause");
42257 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
42259 else if (memory_order == OMP_MEMORY_ORDER_ACQ_REL)
42260 memory_order = OMP_MEMORY_ORDER_RELEASE;
42261 break;
42262 default:
42263 break;
42265 if (fail != OMP_MEMORY_ORDER_UNSPECIFIED)
42266 memory_order
42267 = (enum omp_memory_order) (memory_order
42268 | (fail << OMP_FAIL_MEMORY_ORDER_SHIFT));
42270 switch (code)
42272 case OMP_ATOMIC_READ:
42273 case NOP_EXPR: /* atomic write */
42274 v = cp_parser_unary_expression (parser);
42275 if (v == error_mark_node)
42276 goto saw_error;
42277 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
42278 goto saw_error;
42279 if (code == NOP_EXPR)
42280 lhs = cp_parser_expression (parser);
42281 else
42282 lhs = cp_parser_unary_expression (parser);
42283 if (lhs == error_mark_node)
42284 goto saw_error;
42285 if (code == NOP_EXPR)
42287 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
42288 opcode. */
42289 code = OMP_ATOMIC;
42290 rhs = lhs;
42291 lhs = v;
42292 v = NULL_TREE;
42294 goto done;
42295 case OMP_ATOMIC_CAPTURE_NEW:
42296 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
42298 cp_lexer_consume_token (parser->lexer);
42299 structured_block = true;
42301 else if (compare
42302 && cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
42303 break;
42304 else
42306 v = cp_parser_unary_expression (parser);
42307 if (v == error_mark_node)
42308 goto saw_error;
42309 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
42310 goto saw_error;
42311 if (compare
42312 && cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
42314 location_t eloc = cp_lexer_peek_token (parser->lexer)->location;
42315 error_at (eloc, "expected expression");
42316 goto saw_error;
42319 default:
42320 break;
42323 restart:
42324 if (compare && cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
42326 cp_lexer_consume_token (parser->lexer);
42328 matching_parens parens;
42329 if (!parens.require_open (parser))
42330 goto saw_error;
42331 location_t eloc = cp_lexer_peek_token (parser->lexer)->location;
42332 tree cmp_expr;
42333 if (r)
42334 cmp_expr = cp_parser_unary_expression (parser);
42335 else
42336 cmp_expr = cp_parser_binary_expression (parser, false, true,
42337 PREC_NOT_OPERATOR, NULL);
42338 if (!parens.require_close (parser))
42339 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
42340 if (cmp_expr == error_mark_node)
42341 goto saw_error;
42342 if (r)
42344 if (!cp_tree_equal (cmp_expr, r))
42345 goto bad_if;
42346 cmp_expr = rhs;
42347 rhs = NULL_TREE;
42348 gcc_assert (TREE_CODE (cmp_expr) == EQ_EXPR);
42350 if (TREE_CODE (cmp_expr) == EQ_EXPR)
42352 else if (!structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
42354 error_at (EXPR_LOC_OR_LOC (cmp_expr, eloc),
42355 "expected %<==%> comparison in %<if%> condition");
42356 goto saw_error;
42358 else if (TREE_CODE (cmp_expr) != GT_EXPR
42359 && TREE_CODE (cmp_expr) != LT_EXPR)
42361 error_at (EXPR_LOC_OR_LOC (cmp_expr, eloc),
42362 "expected %<==%>, %<<%> or %<>%> comparison in %<if%> "
42363 "condition");
42364 goto saw_error;
42366 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
42367 goto saw_error;
42369 extra_scope = true;
42370 eloc = cp_lexer_peek_token (parser->lexer)->location;
42371 lhs = cp_parser_unary_expression (parser);
42372 orig_lhs = lhs;
42373 if (lhs == error_mark_node)
42374 goto saw_error;
42375 if (!cp_lexer_next_token_is (parser->lexer, CPP_EQ))
42377 cp_parser_error (parser, "expected %<=%>");
42378 goto saw_error;
42380 cp_lexer_consume_token (parser->lexer);
42381 eloc = cp_lexer_peek_token (parser->lexer)->location;
42382 if (TREE_CODE (cmp_expr) == EQ_EXPR)
42383 rhs1 = cp_parser_expression (parser);
42384 else
42385 rhs1 = cp_parser_simple_cast_expression (parser);
42387 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
42388 goto saw_error;
42390 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
42391 goto saw_error;
42393 extra_scope = false;
42394 no_semicolon = true;
42396 if (cp_tree_equal (TREE_OPERAND (cmp_expr, 0), lhs))
42398 if (TREE_CODE (cmp_expr) == EQ_EXPR)
42400 opcode = COND_EXPR;
42401 rhs = TREE_OPERAND (cmp_expr, 1);
42403 else if (cp_tree_equal (TREE_OPERAND (cmp_expr, 1), rhs1))
42405 opcode = (TREE_CODE (cmp_expr) == GT_EXPR
42406 ? MIN_EXPR : MAX_EXPR);
42407 rhs = rhs1;
42408 rhs1 = TREE_OPERAND (cmp_expr, 0);
42410 else
42411 goto bad_if;
42413 else if (TREE_CODE (cmp_expr) == EQ_EXPR)
42414 goto bad_if;
42415 else if (cp_tree_equal (TREE_OPERAND (cmp_expr, 1), lhs)
42416 && cp_tree_equal (TREE_OPERAND (cmp_expr, 0), rhs1))
42418 opcode = (TREE_CODE (cmp_expr) == GT_EXPR
42419 ? MAX_EXPR : MIN_EXPR);
42420 rhs = rhs1;
42421 rhs1 = TREE_OPERAND (cmp_expr, 1);
42423 else
42425 bad_if:
42426 cp_parser_error (parser,
42427 "invalid form of %<#pragma omp atomic compare%>");
42428 goto saw_error;
42431 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
42433 if (code != OMP_ATOMIC_CAPTURE_NEW
42434 || (structured_block && r == NULL_TREE)
42435 || TREE_CODE (cmp_expr) != EQ_EXPR)
42437 eloc = cp_lexer_peek_token (parser->lexer)->location;
42438 error_at (eloc, "unexpected %<else%>");
42439 goto saw_error;
42442 cp_lexer_consume_token (parser->lexer);
42444 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
42445 goto saw_error;
42447 extra_scope = true;
42448 v = cp_parser_unary_expression (parser);
42449 if (v == error_mark_node)
42450 goto saw_error;
42451 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
42452 goto saw_error;
42454 tree expr = cp_parser_simple_cast_expression (parser);
42456 if (!cp_tree_equal (expr, lhs))
42457 goto bad_if;
42459 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
42460 goto saw_error;
42462 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
42463 goto saw_error;
42465 extra_scope = false;
42466 code = OMP_ATOMIC_CAPTURE_OLD;
42467 if (r == NULL_TREE)
42468 /* Signal to c_finish_omp_atomic that in
42469 if (x == e) { x = d; } else { v = x; }
42470 case the store to v should be conditional. */
42471 r = void_list_node;
42473 else if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
42475 cp_parser_error (parser, "expected %<else%>");
42476 goto saw_error;
42478 else if (code == OMP_ATOMIC_CAPTURE_NEW
42479 && r != NULL_TREE
42480 && v == NULL_TREE)
42481 code = OMP_ATOMIC;
42482 goto stmt_done;
42484 lhs = cp_parser_unary_expression (parser);
42485 orig_lhs = lhs;
42486 switch (TREE_CODE (lhs))
42488 case ERROR_MARK:
42489 goto saw_error;
42491 case POSTINCREMENT_EXPR:
42492 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
42493 code = OMP_ATOMIC_CAPTURE_OLD;
42494 /* FALLTHROUGH */
42495 case PREINCREMENT_EXPR:
42496 lhs = TREE_OPERAND (lhs, 0);
42497 opcode = PLUS_EXPR;
42498 rhs = integer_one_node;
42499 if (compare)
42500 goto invalid_compare;
42501 break;
42503 case POSTDECREMENT_EXPR:
42504 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
42505 code = OMP_ATOMIC_CAPTURE_OLD;
42506 /* FALLTHROUGH */
42507 case PREDECREMENT_EXPR:
42508 lhs = TREE_OPERAND (lhs, 0);
42509 opcode = MINUS_EXPR;
42510 rhs = integer_one_node;
42511 if (compare)
42512 goto invalid_compare;
42513 break;
42515 case COMPOUND_EXPR:
42516 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
42517 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
42518 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
42519 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
42520 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
42521 (TREE_OPERAND (lhs, 1), 0), 0)))
42522 == BOOLEAN_TYPE)
42523 /* Undo effects of boolean_increment for post {in,de}crement. */
42524 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
42525 /* FALLTHRU */
42526 case MODIFY_EXPR:
42527 if (TREE_CODE (lhs) == MODIFY_EXPR
42528 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
42530 /* Undo effects of boolean_increment. */
42531 if (integer_onep (TREE_OPERAND (lhs, 1)))
42533 /* This is pre or post increment. */
42534 rhs = TREE_OPERAND (lhs, 1);
42535 lhs = TREE_OPERAND (lhs, 0);
42536 opcode = NOP_EXPR;
42537 if (code == OMP_ATOMIC_CAPTURE_NEW
42538 && !structured_block
42539 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
42540 code = OMP_ATOMIC_CAPTURE_OLD;
42541 if (compare)
42542 goto invalid_compare;
42543 break;
42546 /* FALLTHRU */
42547 default:
42548 if (compare && !cp_lexer_next_token_is (parser->lexer, CPP_EQ))
42550 cp_parser_error (parser, "expected %<=%>");
42551 goto saw_error;
42553 switch (cp_lexer_peek_token (parser->lexer)->type)
42555 case CPP_MULT_EQ:
42556 opcode = MULT_EXPR;
42557 break;
42558 case CPP_DIV_EQ:
42559 opcode = TRUNC_DIV_EXPR;
42560 break;
42561 case CPP_PLUS_EQ:
42562 opcode = PLUS_EXPR;
42563 break;
42564 case CPP_MINUS_EQ:
42565 opcode = MINUS_EXPR;
42566 break;
42567 case CPP_LSHIFT_EQ:
42568 opcode = LSHIFT_EXPR;
42569 break;
42570 case CPP_RSHIFT_EQ:
42571 opcode = RSHIFT_EXPR;
42572 break;
42573 case CPP_AND_EQ:
42574 opcode = BIT_AND_EXPR;
42575 break;
42576 case CPP_OR_EQ:
42577 opcode = BIT_IOR_EXPR;
42578 break;
42579 case CPP_XOR_EQ:
42580 opcode = BIT_XOR_EXPR;
42581 break;
42582 case CPP_EQ:
42583 enum cp_parser_prec oprec;
42584 cp_token *token;
42585 cp_lexer_consume_token (parser->lexer);
42586 cp_parser_parse_tentatively (parser);
42587 rhs1 = cp_parser_simple_cast_expression (parser);
42588 if (rhs1 == error_mark_node)
42590 cp_parser_abort_tentative_parse (parser);
42591 cp_parser_simple_cast_expression (parser);
42592 goto saw_error;
42594 token = cp_lexer_peek_token (parser->lexer);
42595 if (token->type != CPP_SEMICOLON
42596 && (!compare || token->type != CPP_QUERY)
42597 && !cp_tree_equal (lhs, rhs1))
42599 cp_parser_abort_tentative_parse (parser);
42600 cp_parser_parse_tentatively (parser);
42601 rhs = cp_parser_binary_expression (parser, false, true,
42602 PREC_NOT_OPERATOR, NULL);
42603 if (rhs == error_mark_node)
42605 cp_parser_abort_tentative_parse (parser);
42606 cp_parser_binary_expression (parser, false, true,
42607 PREC_NOT_OPERATOR, NULL);
42608 goto saw_error;
42610 switch (TREE_CODE (rhs))
42612 case MULT_EXPR:
42613 case TRUNC_DIV_EXPR:
42614 case RDIV_EXPR:
42615 case PLUS_EXPR:
42616 case MINUS_EXPR:
42617 case LSHIFT_EXPR:
42618 case RSHIFT_EXPR:
42619 case BIT_AND_EXPR:
42620 case BIT_IOR_EXPR:
42621 case BIT_XOR_EXPR:
42622 if (compare)
42623 break;
42624 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
42626 if (cp_parser_parse_definitely (parser))
42628 opcode = TREE_CODE (rhs);
42629 rhs1 = TREE_OPERAND (rhs, 0);
42630 rhs = TREE_OPERAND (rhs, 1);
42631 goto stmt_done;
42633 else
42634 goto saw_error;
42636 break;
42637 case EQ_EXPR:
42638 if (!compare
42639 || code != OMP_ATOMIC_CAPTURE_NEW
42640 || !structured_block
42641 || v
42642 || r)
42643 break;
42644 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
42645 && cp_lexer_nth_token_is_keyword (parser->lexer,
42646 2, RID_IF))
42648 if (cp_parser_parse_definitely (parser))
42650 r = lhs;
42651 lhs = NULL_TREE;
42652 rhs1 = NULL_TREE;
42653 cp_lexer_consume_token (parser->lexer);
42654 goto restart;
42657 break;
42658 case GT_EXPR:
42659 case LT_EXPR:
42660 if (compare
42661 && cp_lexer_next_token_is (parser->lexer, CPP_QUERY)
42662 && cp_tree_equal (lhs, TREE_OPERAND (rhs, 1))
42663 && cp_parser_parse_definitely (parser))
42665 opcode = TREE_CODE (rhs);
42666 rhs1 = TREE_OPERAND (rhs, 0);
42667 rhs = TREE_OPERAND (rhs, 1);
42668 cond_expr:
42669 cp_lexer_consume_token (parser->lexer);
42670 bool saved_colon_corrects_to_scope_p
42671 = parser->colon_corrects_to_scope_p;
42672 parser->colon_corrects_to_scope_p = false;
42673 tree e1 = cp_parser_expression (parser);
42674 parser->colon_corrects_to_scope_p
42675 = saved_colon_corrects_to_scope_p;
42676 cp_parser_require (parser, CPP_COLON, RT_COLON);
42677 tree e2 = cp_parser_simple_cast_expression (parser);
42678 if (cp_tree_equal (lhs, e2))
42680 if (cp_tree_equal (lhs, rhs1))
42682 if (opcode == EQ_EXPR)
42684 opcode = COND_EXPR;
42685 rhs1 = e1;
42686 goto stmt_done;
42688 if (cp_tree_equal (rhs, e1))
42690 opcode
42691 = opcode == GT_EXPR ? MIN_EXPR : MAX_EXPR;
42692 rhs = e1;
42693 goto stmt_done;
42696 else
42698 gcc_assert (opcode != EQ_EXPR);
42699 if (cp_tree_equal (rhs1, e1))
42701 opcode
42702 = opcode == GT_EXPR ? MAX_EXPR : MIN_EXPR;
42703 rhs1 = rhs;
42704 rhs = e1;
42705 goto stmt_done;
42709 cp_parser_error (parser,
42710 "invalid form of "
42711 "%<#pragma omp atomic compare%>");
42712 goto saw_error;
42714 break;
42715 default:
42716 break;
42718 cp_parser_abort_tentative_parse (parser);
42719 if (structured_block
42720 && code == OMP_ATOMIC_CAPTURE_OLD
42721 && !compare)
42723 rhs = cp_parser_expression (parser);
42724 if (rhs == error_mark_node)
42725 goto saw_error;
42726 opcode = NOP_EXPR;
42727 rhs1 = NULL_TREE;
42728 goto stmt_done;
42730 cp_parser_error (parser,
42731 "invalid form of %<#pragma omp atomic%>");
42732 goto saw_error;
42734 if (!cp_parser_parse_definitely (parser))
42735 goto saw_error;
42736 switch (token->type)
42738 case CPP_SEMICOLON:
42739 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
42741 code = OMP_ATOMIC_CAPTURE_OLD;
42742 v = lhs;
42743 lhs = NULL_TREE;
42744 lhs1 = rhs1;
42745 rhs1 = NULL_TREE;
42746 cp_lexer_consume_token (parser->lexer);
42747 goto restart;
42749 else if (structured_block && !compare)
42751 opcode = NOP_EXPR;
42752 rhs = rhs1;
42753 rhs1 = NULL_TREE;
42754 goto stmt_done;
42756 cp_parser_error (parser,
42757 "invalid form of %<#pragma omp atomic%>");
42758 goto saw_error;
42759 case CPP_MULT:
42760 opcode = MULT_EXPR;
42761 break;
42762 case CPP_DIV:
42763 opcode = TRUNC_DIV_EXPR;
42764 break;
42765 case CPP_PLUS:
42766 opcode = PLUS_EXPR;
42767 break;
42768 case CPP_MINUS:
42769 opcode = MINUS_EXPR;
42770 break;
42771 case CPP_LSHIFT:
42772 opcode = LSHIFT_EXPR;
42773 break;
42774 case CPP_RSHIFT:
42775 opcode = RSHIFT_EXPR;
42776 break;
42777 case CPP_AND:
42778 opcode = BIT_AND_EXPR;
42779 break;
42780 case CPP_OR:
42781 opcode = BIT_IOR_EXPR;
42782 break;
42783 case CPP_XOR:
42784 opcode = BIT_XOR_EXPR;
42785 break;
42786 case CPP_EQ_EQ:
42787 opcode = EQ_EXPR;
42788 break;
42789 case CPP_GREATER:
42790 opcode = GT_EXPR;
42791 break;
42792 case CPP_LESS:
42793 opcode = LT_EXPR;
42794 break;
42795 default:
42796 cp_parser_error (parser,
42797 "invalid operator for %<#pragma omp atomic%>");
42798 goto saw_error;
42800 if (compare
42801 && TREE_CODE_CLASS (opcode) != tcc_comparison)
42803 cp_parser_error (parser,
42804 "invalid form of "
42805 "%<#pragma omp atomic compare%>");
42806 goto saw_error;
42808 oprec = TOKEN_PRECEDENCE (token);
42809 gcc_assert (oprec != PREC_NOT_OPERATOR);
42810 if (commutative_tree_code (opcode))
42811 oprec = (enum cp_parser_prec) (oprec - 1);
42812 cp_lexer_consume_token (parser->lexer);
42813 rhs = cp_parser_binary_expression (parser, false, false,
42814 oprec, NULL);
42815 if (rhs == error_mark_node)
42816 goto saw_error;
42817 if (compare)
42819 if (!cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
42821 cp_parser_error (parser,
42822 "invalid form of "
42823 "%<#pragma omp atomic compare%>");
42824 goto saw_error;
42826 goto cond_expr;
42828 goto stmt_done;
42829 default:
42830 cp_parser_error (parser,
42831 "invalid operator for %<#pragma omp atomic%>");
42832 goto saw_error;
42834 cp_lexer_consume_token (parser->lexer);
42836 rhs = cp_parser_expression (parser);
42837 if (rhs == error_mark_node)
42838 goto saw_error;
42839 break;
42841 stmt_done:
42842 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW && r == NULL_TREE)
42844 if (!no_semicolon
42845 && !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
42846 goto saw_error;
42847 no_semicolon = false;
42848 v = cp_parser_unary_expression (parser);
42849 if (v == error_mark_node)
42850 goto saw_error;
42851 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
42852 goto saw_error;
42853 lhs1 = cp_parser_unary_expression (parser);
42854 if (lhs1 == error_mark_node)
42855 goto saw_error;
42857 if (structured_block)
42859 if (!no_semicolon)
42860 cp_parser_consume_semicolon_at_end_of_statement (parser);
42861 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
42863 done:
42864 if (weak && opcode != COND_EXPR)
42866 error_at (loc, "%<weak%> clause requires atomic equality comparison");
42867 weak = false;
42869 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
42870 finish_omp_atomic (pragma_tok->location, code, opcode, lhs, rhs, v, lhs1,
42871 rhs1, r, clauses, memory_order, weak);
42872 if (!structured_block && !no_semicolon)
42873 cp_parser_consume_semicolon_at_end_of_statement (parser);
42874 return;
42876 invalid_compare:
42877 error ("invalid form of %<pragma omp atomic compare%>");
42878 /* FALLTHRU */
42879 saw_error:
42880 cp_parser_skip_to_end_of_block_or_statement (parser);
42881 if (extra_scope && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
42882 cp_lexer_consume_token (parser->lexer);
42883 if (structured_block)
42885 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
42886 cp_lexer_consume_token (parser->lexer);
42887 else if (code == OMP_ATOMIC_CAPTURE_NEW)
42889 cp_parser_skip_to_end_of_block_or_statement (parser);
42890 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
42891 cp_lexer_consume_token (parser->lexer);
42897 /* OpenMP 2.5:
42898 # pragma omp barrier new-line */
42900 static void
42901 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
42903 cp_parser_require_pragma_eol (parser, pragma_tok);
42904 finish_omp_barrier ();
42907 /* OpenMP 2.5:
42908 # pragma omp critical [(name)] new-line
42909 structured-block
42911 OpenMP 4.5:
42912 # pragma omp critical [(name) [hint(expression)]] new-line
42913 structured-block */
42915 #define OMP_CRITICAL_CLAUSE_MASK \
42916 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
42918 static tree
42919 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
42921 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
42923 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
42925 matching_parens parens;
42926 parens.consume_open (parser);
42928 name = cp_parser_identifier (parser);
42930 if (name == error_mark_node
42931 || !parens.require_close (parser))
42932 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
42933 /*or_comma=*/false,
42934 /*consume_paren=*/true);
42935 if (name == error_mark_node)
42936 name = NULL;
42938 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
42939 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
42940 cp_lexer_consume_token (parser->lexer);
42943 clauses = cp_parser_omp_all_clauses (parser, OMP_CRITICAL_CLAUSE_MASK,
42944 "#pragma omp critical", pragma_tok);
42946 stmt = cp_parser_omp_structured_block (parser, if_p);
42947 return c_finish_omp_critical (input_location, stmt, name, clauses);
42950 /* OpenMP 5.0:
42951 # pragma omp depobj ( depobj ) depobj-clause new-line
42953 depobj-clause:
42954 depend (dependence-type : locator)
42955 destroy
42956 update (dependence-type)
42958 dependence-type:
42961 inout
42962 mutexinout */
42964 static void
42965 cp_parser_omp_depobj (cp_parser *parser, cp_token *pragma_tok)
42967 location_t loc = pragma_tok->location;
42968 matching_parens parens;
42969 if (!parens.require_open (parser))
42971 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
42972 return;
42975 tree depobj = cp_parser_assignment_expression (parser);
42977 if (!parens.require_close (parser))
42978 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
42979 /*or_comma=*/false,
42980 /*consume_paren=*/true);
42982 tree clause = NULL_TREE;
42983 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INVALID;
42984 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
42985 cp_lexer_consume_token (parser->lexer);
42986 location_t c_loc = cp_lexer_peek_token (parser->lexer)->location;
42987 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42989 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
42990 const char *p = IDENTIFIER_POINTER (id);
42992 cp_lexer_consume_token (parser->lexer);
42993 if (!strcmp ("depend", p))
42995 /* Don't create location wrapper nodes within the depend clause. */
42996 auto_suppress_location_wrappers sentinel;
42997 clause = cp_parser_omp_clause_depend (parser, NULL_TREE, c_loc);
42998 if (clause)
42999 clause = finish_omp_clauses (clause, C_ORT_OMP);
43000 if (!clause)
43001 clause = error_mark_node;
43003 else if (!strcmp ("destroy", p))
43004 kind = OMP_CLAUSE_DEPEND_LAST;
43005 else if (!strcmp ("update", p))
43007 matching_parens c_parens;
43008 if (c_parens.require_open (parser))
43010 location_t c2_loc
43011 = cp_lexer_peek_token (parser->lexer)->location;
43012 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43014 tree id2 = cp_lexer_peek_token (parser->lexer)->u.value;
43015 const char *p2 = IDENTIFIER_POINTER (id2);
43017 cp_lexer_consume_token (parser->lexer);
43018 if (!strcmp ("in", p2))
43019 kind = OMP_CLAUSE_DEPEND_IN;
43020 else if (!strcmp ("out", p2))
43021 kind = OMP_CLAUSE_DEPEND_OUT;
43022 else if (!strcmp ("inout", p2))
43023 kind = OMP_CLAUSE_DEPEND_INOUT;
43024 else if (!strcmp ("mutexinoutset", p2))
43025 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
43026 else if (!strcmp ("inoutset", p2))
43027 kind = OMP_CLAUSE_DEPEND_INOUTSET;
43029 if (kind == OMP_CLAUSE_DEPEND_INVALID)
43031 clause = error_mark_node;
43032 error_at (c2_loc, "expected %<in%>, %<out%>, %<inout%>, "
43033 "%<mutexinoutset%> or %<inoutset%>");
43035 if (!c_parens.require_close (parser))
43036 cp_parser_skip_to_closing_parenthesis (parser,
43037 /*recovering=*/true,
43038 /*or_comma=*/false,
43039 /*consume_paren=*/true);
43041 else
43042 clause = error_mark_node;
43045 if (!clause && kind == OMP_CLAUSE_DEPEND_INVALID)
43047 clause = error_mark_node;
43048 error_at (c_loc, "expected %<depend%>, %<destroy%> or %<update%> clause");
43050 cp_parser_require_pragma_eol (parser, pragma_tok);
43052 finish_omp_depobj (loc, depobj, kind, clause);
43056 /* OpenMP 2.5:
43057 # pragma omp flush flush-vars[opt] new-line
43059 flush-vars:
43060 ( variable-list )
43062 OpenMP 5.0:
43063 # pragma omp flush memory-order-clause new-line */
43065 static void
43066 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
43068 enum memmodel mo = MEMMODEL_LAST;
43069 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
43070 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
43071 cp_lexer_consume_token (parser->lexer);
43072 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43074 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43075 const char *p = IDENTIFIER_POINTER (id);
43076 if (!strcmp (p, "seq_cst"))
43077 mo = MEMMODEL_SEQ_CST;
43078 else if (!strcmp (p, "acq_rel"))
43079 mo = MEMMODEL_ACQ_REL;
43080 else if (!strcmp (p, "release"))
43081 mo = MEMMODEL_RELEASE;
43082 else if (!strcmp (p, "acquire"))
43083 mo = MEMMODEL_ACQUIRE;
43084 else
43085 error_at (cp_lexer_peek_token (parser->lexer)->location,
43086 "expected %<seq_cst%>, %<acq_rel%>, %<release%> or "
43087 "%<acquire%>");
43088 cp_lexer_consume_token (parser->lexer);
43090 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
43092 if (mo != MEMMODEL_LAST)
43093 error_at (cp_lexer_peek_token (parser->lexer)->location,
43094 "%<flush%> list specified together with memory order "
43095 "clause");
43096 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
43098 cp_parser_require_pragma_eol (parser, pragma_tok);
43100 finish_omp_flush (mo);
43103 /* Helper function, to parse omp for increment expression. */
43105 static tree
43106 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
43108 tree cond = cp_parser_binary_expression (parser, false, true,
43109 PREC_NOT_OPERATOR, NULL);
43110 if (cond == error_mark_node
43111 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
43113 cp_parser_skip_to_end_of_statement (parser);
43114 return error_mark_node;
43117 switch (TREE_CODE (cond))
43119 case GT_EXPR:
43120 case GE_EXPR:
43121 case LT_EXPR:
43122 case LE_EXPR:
43123 break;
43124 case NE_EXPR:
43125 if (code != OACC_LOOP)
43126 break;
43127 gcc_fallthrough ();
43128 default:
43129 return error_mark_node;
43132 /* If decl is an iterator, preserve LHS and RHS of the relational
43133 expr until finish_omp_for. */
43134 if (decl
43135 && (type_dependent_expression_p (decl)
43136 || CLASS_TYPE_P (TREE_TYPE (decl))))
43137 return cond;
43139 return build_x_binary_op (cp_expr_loc_or_input_loc (cond),
43140 TREE_CODE (cond),
43141 TREE_OPERAND (cond, 0), ERROR_MARK,
43142 TREE_OPERAND (cond, 1), ERROR_MARK,
43143 NULL_TREE, /*overload=*/NULL, tf_warning_or_error);
43146 /* Helper function, to parse omp for increment expression. */
43148 static tree
43149 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
43151 cp_token *token = cp_lexer_peek_token (parser->lexer);
43152 enum tree_code op;
43153 tree lhs, rhs;
43154 cp_id_kind idk;
43155 bool decl_first;
43157 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
43159 op = (token->type == CPP_PLUS_PLUS
43160 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
43161 cp_lexer_consume_token (parser->lexer);
43162 lhs = cp_parser_simple_cast_expression (parser);
43163 if (lhs != decl
43164 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
43165 return error_mark_node;
43166 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
43169 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
43170 if (lhs != decl
43171 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
43172 return error_mark_node;
43174 token = cp_lexer_peek_token (parser->lexer);
43175 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
43177 op = (token->type == CPP_PLUS_PLUS
43178 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
43179 cp_lexer_consume_token (parser->lexer);
43180 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
43183 op = cp_parser_assignment_operator_opt (parser);
43184 if (op == ERROR_MARK)
43185 return error_mark_node;
43187 if (op != NOP_EXPR)
43189 rhs = cp_parser_assignment_expression (parser);
43190 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
43191 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
43194 lhs = cp_parser_binary_expression (parser, false, false,
43195 PREC_ADDITIVE_EXPRESSION, NULL);
43196 token = cp_lexer_peek_token (parser->lexer);
43197 decl_first = (lhs == decl
43198 || (processing_template_decl && cp_tree_equal (lhs, decl)));
43199 if (decl_first)
43200 lhs = NULL_TREE;
43201 if (token->type != CPP_PLUS
43202 && token->type != CPP_MINUS)
43203 return error_mark_node;
43207 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
43208 cp_lexer_consume_token (parser->lexer);
43209 rhs = cp_parser_binary_expression (parser, false, false,
43210 PREC_ADDITIVE_EXPRESSION, NULL);
43211 token = cp_lexer_peek_token (parser->lexer);
43212 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
43214 if (lhs == NULL_TREE)
43216 if (op == PLUS_EXPR)
43217 lhs = rhs;
43218 else
43219 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
43220 NULL_TREE, tf_warning_or_error);
43222 else
43223 lhs = build_x_binary_op (input_location, op,
43224 lhs, ERROR_MARK,
43225 rhs, ERROR_MARK,
43226 NULL_TREE, NULL, tf_warning_or_error);
43229 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
43231 if (!decl_first)
43233 if ((rhs != decl
43234 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
43235 || op == MINUS_EXPR)
43236 return error_mark_node;
43237 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
43239 else
43240 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
43242 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
43245 /* Parse the initialization statement of an OpenMP for loop. Range-for
43246 is handled separately in cp_convert_omp_range_for.
43248 On entry SL is the current statement list. Parsing of some forms
43249 of initialization pops this list and stores its contents in either INIT
43250 or THIS_PRE_BODY, and sets SL to null. Initialization for class
43251 iterators is added directly to SL and it is not popped until later.
43253 On return, DECL is set if the initialization is by binding the
43254 iteration variable. If the initialization is by assignment, REAL_DECL
43255 is set to point to a variable in an outer scope. ORIG_INIT is set
43256 if the iteration variable is of class type; this is a copy saved for
43257 error checking in finish_omp_for.
43259 Return true if the resulting construct should have an
43260 OMP_CLAUSE_PRIVATE added to it. */
43262 static tree
43263 cp_parser_omp_for_loop_init (cp_parser *parser,
43264 tree &this_pre_body,
43265 tree &sl,
43266 tree &init,
43267 tree &orig_init,
43268 tree &decl,
43269 tree &real_decl)
43271 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
43272 return NULL_TREE;
43274 tree add_private_clause = NULL_TREE;
43276 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
43278 init-expr:
43279 var = lb
43280 integer-type var = lb
43281 random-access-iterator-type var = lb
43282 pointer-type var = lb
43284 cp_decl_specifier_seq type_specifiers;
43286 /* First, try to parse as an initialized declaration. See
43287 cp_parser_condition, from whence the bulk of this is copied. */
43289 cp_parser_parse_tentatively (parser);
43290 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
43291 /*is_declaration=*/true,
43292 /*is_trailing_return=*/false,
43293 &type_specifiers);
43294 if (cp_parser_parse_definitely (parser))
43296 /* If parsing a type specifier seq succeeded, then this
43297 MUST be a initialized declaration. */
43298 tree asm_specification, attributes;
43299 cp_declarator *declarator;
43301 declarator = cp_parser_declarator (parser,
43302 CP_PARSER_DECLARATOR_NAMED,
43303 CP_PARSER_FLAGS_NONE,
43304 /*ctor_dtor_or_conv_p=*/NULL,
43305 /*parenthesized_p=*/NULL,
43306 /*member_p=*/false,
43307 /*friend_p=*/false,
43308 /*static_p=*/false);
43309 attributes = cp_parser_attributes_opt (parser);
43310 asm_specification = cp_parser_asm_specification_opt (parser);
43312 if (declarator == cp_error_declarator)
43313 cp_parser_skip_to_end_of_statement (parser);
43315 else
43317 tree pushed_scope, auto_node;
43319 decl = start_decl (declarator, &type_specifiers,
43320 SD_INITIALIZED, attributes,
43321 /*prefix_attributes=*/NULL_TREE,
43322 &pushed_scope);
43324 auto_node = type_uses_auto (TREE_TYPE (decl));
43325 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
43327 if (cp_lexer_next_token_is (parser->lexer,
43328 CPP_OPEN_PAREN))
43329 error ("parenthesized initialization is not allowed in "
43330 "OpenMP %<for%> loop");
43331 else
43332 /* Trigger an error. */
43333 cp_parser_require (parser, CPP_EQ, RT_EQ);
43335 init = error_mark_node;
43336 cp_parser_skip_to_end_of_statement (parser);
43338 else if (CLASS_TYPE_P (TREE_TYPE (decl))
43339 || type_dependent_expression_p (decl)
43340 || auto_node)
43342 bool is_non_constant_init;
43344 init = cp_parser_initializer (parser,
43345 /*is_direct_init=*/nullptr,
43346 &is_non_constant_init);
43348 if (auto_node)
43350 TREE_TYPE (decl)
43351 = do_auto_deduction (TREE_TYPE (decl), init,
43352 auto_node);
43354 if (!CLASS_TYPE_P (TREE_TYPE (decl))
43355 && !type_dependent_expression_p (decl))
43356 goto non_class;
43359 cp_finish_decl (decl, init, !is_non_constant_init,
43360 asm_specification,
43361 LOOKUP_ONLYCONVERTING);
43362 orig_init = init;
43364 /* In the case of a class iterator, do not pop sl here.
43365 Both class initialization and finalization must happen in
43366 the enclosing init block scope. For now set the init
43367 expression to null; it'll be filled in properly in
43368 finish_omp_for before stuffing it in the OMP_FOR. */
43369 if (CLASS_TYPE_P (TREE_TYPE (decl)))
43370 init = NULL_TREE;
43371 else /* It is a parameterized type. */
43373 init = pop_stmt_list (sl);
43374 sl = NULL_TREE;
43375 if (init && TREE_CODE (init) == STATEMENT_LIST)
43377 tree_stmt_iterator i = tsi_start (init);
43378 /* Move lambda DECL_EXPRs to the enclosing block. */
43379 while (!tsi_end_p (i))
43381 tree t = tsi_stmt (i);
43382 if (TREE_CODE (t) == DECL_EXPR
43383 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
43385 tsi_delink (&i);
43386 add_stmt (t);
43387 continue;
43389 break;
43391 if (tsi_one_before_end_p (i))
43393 tree t = tsi_stmt (i);
43394 tsi_delink (&i);
43395 free_stmt_list (init);
43396 init = t;
43401 else
43402 /* This is an initialized declaration of non-class,
43403 non-parameterized type iteration variable. */
43405 /* Consume '='. */
43406 cp_lexer_consume_token (parser->lexer);
43407 init = cp_parser_assignment_expression (parser);
43409 non_class:
43410 if (TYPE_REF_P (TREE_TYPE (decl)))
43411 init = error_mark_node;
43412 else
43413 cp_finish_decl (decl, NULL_TREE,
43414 /*init_const_expr_p=*/false,
43415 asm_specification,
43416 LOOKUP_ONLYCONVERTING);
43417 this_pre_body = pop_stmt_list (sl);
43418 sl = NULL_TREE;
43421 if (pushed_scope)
43422 pop_scope (pushed_scope);
43425 else
43427 cp_id_kind idk;
43428 /* If parsing a type specifier sequence failed, then
43429 this MUST be a simple expression. */
43430 cp_parser_parse_tentatively (parser);
43431 decl = cp_parser_primary_expression (parser, false, false,
43432 false, &idk);
43433 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
43434 if (!cp_parser_error_occurred (parser)
43435 && decl
43436 && (TREE_CODE (decl) == COMPONENT_REF
43437 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
43439 cp_parser_abort_tentative_parse (parser);
43440 cp_parser_parse_tentatively (parser);
43441 cp_token *token = cp_lexer_peek_token (parser->lexer);
43442 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
43443 /*check_dependency_p=*/true,
43444 /*template_p=*/NULL,
43445 /*declarator_p=*/false,
43446 /*optional_p=*/false);
43447 if (name != error_mark_node
43448 && last_tok == cp_lexer_peek_token (parser->lexer))
43450 decl = cp_parser_lookup_name_simple (parser, name,
43451 token->location);
43452 if (TREE_CODE (decl) == FIELD_DECL)
43453 add_private_clause = omp_privatize_field (decl, false);
43455 cp_parser_abort_tentative_parse (parser);
43456 cp_parser_parse_tentatively (parser);
43457 decl = cp_parser_primary_expression (parser, false, false,
43458 false, &idk);
43460 if (!cp_parser_error_occurred (parser)
43461 && decl
43462 && DECL_P (decl)
43463 && CLASS_TYPE_P (TREE_TYPE (decl)))
43465 tree rhs;
43467 cp_parser_parse_definitely (parser);
43468 cp_parser_require (parser, CPP_EQ, RT_EQ);
43469 rhs = cp_parser_assignment_expression (parser);
43470 orig_init = rhs;
43471 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
43472 decl, NOP_EXPR,
43473 rhs, NULL_TREE,
43474 tf_warning_or_error));
43475 if (!add_private_clause)
43476 add_private_clause = decl;
43478 else
43480 decl = NULL;
43481 cp_parser_abort_tentative_parse (parser);
43482 init = cp_parser_expression (parser);
43483 if (init)
43485 if (TREE_CODE (init) == MODIFY_EXPR
43486 || TREE_CODE (init) == MODOP_EXPR)
43487 real_decl = TREE_OPERAND (init, 0);
43490 this_pre_body = pop_stmt_list (sl);
43491 sl = NULL_TREE;
43493 return add_private_clause;
43496 /* Helper for cp_parser_omp_loop_nest, handle one range-for loop
43497 including introducing new temporaries for the range start and end,
43498 doing auto deduction, and processing decomposition variables.
43500 This function is also called from pt.cc during template instantiation.
43501 In that case SL is NULL_TREE, otherwise it is the current statement
43502 list. */
43503 void
43504 cp_convert_omp_range_for (tree &this_pre_body, tree &sl,
43505 tree &decl, tree &orig_decl, tree &init,
43506 tree &orig_init, tree &cond, tree &incr)
43508 tree begin, end, range_temp_decl = NULL_TREE;
43509 tree iter_type, begin_expr, end_expr;
43510 bool clear_has_value_expr = false;
43512 if (processing_template_decl)
43514 if (check_for_bare_parameter_packs (init))
43515 init = error_mark_node;
43516 if (!type_dependent_expression_p (init)
43517 /* do_auto_deduction doesn't mess with template init-lists. */
43518 && !BRACE_ENCLOSED_INITIALIZER_P (init))
43520 tree d = decl;
43521 cp_decomp decomp_d, *decomp = NULL;
43522 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
43524 tree v = DECL_VALUE_EXPR (decl);
43525 if (TREE_CODE (v) == ARRAY_REF
43526 && VAR_P (TREE_OPERAND (v, 0))
43527 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
43529 d = TREE_OPERAND (v, 0);
43530 decomp = &decomp_d;
43531 decomp->count = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
43532 decomp->decl = decl;
43535 do_range_for_auto_deduction (d, init, decomp);
43537 cond = global_namespace;
43538 incr = NULL_TREE;
43539 orig_init = init;
43540 if (sl)
43542 this_pre_body = pop_stmt_list (sl);
43543 sl = NULL_TREE;
43545 return;
43548 init = mark_lvalue_use (init);
43550 if (decl == error_mark_node || init == error_mark_node)
43551 /* If an error happened previously do nothing or else a lot of
43552 unhelpful errors would be issued. */
43553 begin_expr = end_expr = iter_type = error_mark_node;
43554 else
43556 tree range_temp;
43558 if (VAR_P (init)
43559 && array_of_runtime_bound_p (TREE_TYPE (init)))
43560 /* Can't bind a reference to an array of runtime bound. */
43561 range_temp = init;
43562 else
43564 range_temp = build_range_temp (init);
43565 DECL_NAME (range_temp) = NULL_TREE;
43566 pushdecl (range_temp);
43567 cp_finish_decl (range_temp, init,
43568 /*is_constant_init*/false, NULL_TREE,
43569 LOOKUP_ONLYCONVERTING);
43570 range_temp_decl = range_temp;
43571 range_temp = convert_from_reference (range_temp);
43573 iter_type = cp_parser_perform_range_for_lookup (range_temp,
43574 &begin_expr, &end_expr);
43577 tree end_iter_type = iter_type;
43578 if (cxx_dialect >= cxx17)
43579 end_iter_type = cv_unqualified (TREE_TYPE (end_expr));
43580 end = build_decl (input_location, VAR_DECL, NULL_TREE, end_iter_type);
43581 TREE_USED (end) = 1;
43582 DECL_ARTIFICIAL (end) = 1;
43583 pushdecl (end);
43584 cp_finish_decl (end, end_expr,
43585 /*is_constant_init*/false, NULL_TREE,
43586 LOOKUP_ONLYCONVERTING);
43588 /* The new for initialization statement. */
43589 begin = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
43590 TREE_USED (begin) = 1;
43591 DECL_ARTIFICIAL (begin) = 1;
43592 pushdecl (begin);
43593 orig_init = init;
43594 if (CLASS_TYPE_P (iter_type))
43595 init = NULL_TREE;
43596 else
43598 init = begin_expr;
43599 begin_expr = NULL_TREE;
43601 cp_finish_decl (begin, begin_expr,
43602 /*is_constant_init*/false, NULL_TREE,
43603 LOOKUP_ONLYCONVERTING);
43605 /* The new for condition. */
43606 if (CLASS_TYPE_P (iter_type))
43607 cond = build2 (NE_EXPR, boolean_type_node, begin, end);
43608 else
43609 cond = build_x_binary_op (input_location, NE_EXPR,
43610 begin, ERROR_MARK,
43611 end, ERROR_MARK,
43612 NULL_TREE, NULL, tf_warning_or_error);
43614 /* The new increment expression. */
43615 if (CLASS_TYPE_P (iter_type))
43616 incr = build2 (PREINCREMENT_EXPR, iter_type, begin, NULL_TREE);
43617 else
43618 incr = finish_unary_op_expr (input_location,
43619 PREINCREMENT_EXPR, begin,
43620 tf_warning_or_error);
43622 orig_decl = decl;
43623 decl = begin;
43624 /* Defer popping sl here. */
43626 cp_decomp decomp_d, *decomp = NULL;
43627 if (orig_decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (orig_decl))
43629 tree v = DECL_VALUE_EXPR (orig_decl);
43630 if (TREE_CODE (v) == ARRAY_REF
43631 && VAR_P (TREE_OPERAND (v, 0))
43632 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
43634 tree d = orig_decl;
43635 orig_decl = TREE_OPERAND (v, 0);
43636 decomp = &decomp_d;
43637 decomp->count = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
43638 decomp->decl = d;
43642 tree auto_node = type_uses_auto (TREE_TYPE (orig_decl));
43643 if (auto_node)
43645 tree t = build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
43646 NULL_TREE, tf_none);
43647 if (!error_operand_p (t))
43649 TREE_TYPE (orig_decl) = do_auto_deduction (TREE_TYPE (orig_decl),
43650 t, auto_node);
43651 if (decomp)
43653 ++processing_template_decl;
43654 cp_finish_decomp (orig_decl, decomp);
43655 --processing_template_decl;
43656 if (!processing_template_decl)
43657 clear_has_value_expr = true;
43662 /* The output ORIG_DECL is not a decl. Instead, it is a tree structure
43663 that holds decls for variables implementing the iterator, represented
43664 as a TREE_LIST whose TREE_CHAIN is a vector. The first two elements
43665 of the vector are decls of scratch variables for the range start and
43666 end that will eventually be bound in the implicit scope surrounding
43667 the whole loop nest. The remaining elements are decls of derived
43668 decomposition variables that are bound inside the loop body. This
43669 structure is further mangled by finish_omp_for into the form required
43670 for the OMP_FOR_ORIG_DECLS field of the OMP_FOR tree node. */\
43671 unsigned decomp_cnt = decomp ? decomp->count : 0;
43672 tree v = make_tree_vec (decomp_cnt + 3);
43673 TREE_VEC_ELT (v, 0) = range_temp_decl;
43674 TREE_VEC_ELT (v, 1) = end;
43675 TREE_VEC_ELT (v, 2) = orig_decl;
43676 if (clear_has_value_expr)
43677 TREE_PUBLIC (v) = 1;
43678 for (unsigned i = 0; i < decomp_cnt; i++)
43680 if (clear_has_value_expr)
43682 /* If cp_finish_decomp was called with processing_template_decl
43683 temporarily set to 1, then decomp names will have deduced
43684 name but the DECL_VALUE_EXPR will be dependent. Hide those
43685 from folding of other loop initializers e.g. for warning
43686 purposes until cp_finish_omp_range_for. */
43687 gcc_checking_assert (DECL_HAS_VALUE_EXPR_P (decomp->decl)
43688 || (TREE_TYPE (decomp->decl)
43689 == error_mark_node));
43690 DECL_HAS_VALUE_EXPR_P (decomp->decl) = 0;
43692 TREE_VEC_ELT (v, i + 3) = decomp->decl;
43693 decomp->decl = DECL_CHAIN (decomp->decl);
43695 orig_decl = tree_cons (NULL_TREE, NULL_TREE, v);
43698 /* Helper for cp_parser_omp_for_loop, finalize part of range for
43699 inside of the collapsed body. */
43701 void
43702 cp_finish_omp_range_for (tree orig, tree begin)
43704 gcc_assert (TREE_CODE (orig) == TREE_LIST
43705 && TREE_CODE (TREE_CHAIN (orig)) == TREE_VEC);
43706 tree decl = TREE_VEC_ELT (TREE_CHAIN (orig), 2);
43707 cp_decomp decomp_d, *decomp = NULL;
43709 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
43711 decomp = &decomp_d;
43712 decomp_d.decl = TREE_VEC_ELT (TREE_CHAIN (orig), 3);
43713 decomp_d.count = TREE_VEC_LENGTH (TREE_CHAIN (orig)) - 3;
43714 if (TREE_PUBLIC (TREE_CHAIN (orig)))
43716 /* Undo temporary clearing of DECL_HAS_VALUE_EXPR_P done
43717 by cp_convert_omp_range_for above. */
43718 TREE_PUBLIC (TREE_CHAIN (orig)) = 0;
43719 tree d = decomp_d.decl;
43720 for (unsigned i = 0; i < decomp_d.count; i++)
43722 if (TREE_TYPE (d) != error_mark_node)
43723 DECL_HAS_VALUE_EXPR_P (d) = 1;
43724 d = DECL_CHAIN (d);
43729 /* The declaration is initialized with *__begin inside the loop body. */
43730 cp_finish_decl (decl,
43731 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
43732 NULL_TREE, tf_warning_or_error),
43733 /*is_constant_init*/false, NULL_TREE,
43734 LOOKUP_ONLYCONVERTING, decomp);
43735 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
43736 cp_finish_decomp (decl, decomp);
43739 /* Return true if next tokens contain a standard attribute that contains
43740 omp::directive (DIRECTIVE). */
43742 static bool
43743 cp_parser_omp_section_scan (cp_parser *parser, const char *directive,
43744 bool tentative)
43746 size_t n = cp_parser_skip_attributes_opt (parser, 1), i;
43747 if (n < 10)
43748 return false;
43749 for (i = 5; i < n - 4; i++)
43750 if (cp_lexer_nth_token_is (parser->lexer, i, CPP_NAME)
43751 && cp_lexer_nth_token_is (parser->lexer, i + 1, CPP_OPEN_PAREN)
43752 && cp_lexer_nth_token_is (parser->lexer, i + 2, CPP_NAME))
43754 tree first = cp_lexer_peek_nth_token (parser->lexer, i)->u.value;
43755 tree second = cp_lexer_peek_nth_token (parser->lexer, i + 2)->u.value;
43756 if (strcmp (IDENTIFIER_POINTER (first), "directive"))
43757 continue;
43758 if (strcmp (IDENTIFIER_POINTER (second), directive) == 0)
43759 break;
43761 if (i == n - 4)
43762 return false;
43763 cp_parser_parse_tentatively (parser);
43764 location_t first_loc = cp_lexer_peek_token (parser->lexer)->location;
43765 location_t last_loc
43766 = cp_lexer_peek_nth_token (parser->lexer, n - 1)->location;
43767 location_t middle_loc = UNKNOWN_LOCATION;
43768 tree std_attrs = cp_parser_std_attribute_spec_seq (parser);
43769 int cnt = 0;
43770 bool seen = false;
43771 for (tree attr = std_attrs; attr; attr = TREE_CHAIN (attr))
43772 if (get_attribute_namespace (attr) == omp_identifier
43773 && is_attribute_p ("directive", get_attribute_name (attr)))
43775 for (tree a = TREE_VALUE (attr); a; a = TREE_CHAIN (a))
43777 tree d = TREE_VALUE (a);
43778 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
43779 cp_token *first = DEFPARSE_TOKENS (d)->first;
43780 cnt++;
43781 if (first->type == CPP_NAME
43782 && strcmp (IDENTIFIER_POINTER (first->u.value),
43783 directive) == 0)
43785 seen = true;
43786 if (middle_loc == UNKNOWN_LOCATION)
43787 middle_loc = first->location;
43791 if (!seen || tentative)
43793 cp_parser_abort_tentative_parse (parser);
43794 return seen;
43796 if (cnt != 1 || TREE_CHAIN (std_attrs))
43798 error_at (make_location (first_loc, last_loc, middle_loc),
43799 "%<[[omp::directive(%s)]]%> must be the only specified "
43800 "attribute on a statement", directive);
43801 cp_parser_abort_tentative_parse (parser);
43802 return false;
43804 if (!cp_parser_parse_definitely (parser))
43805 return false;
43806 cp_parser_handle_statement_omp_attributes (parser, std_attrs);
43807 return true;
43810 /* Parse an OpenMP structured block sequence. KIND is the corresponding
43811 separating directive. */
43813 static tree
43814 cp_parser_omp_structured_block_sequence (cp_parser *parser,
43815 enum pragma_kind kind)
43817 tree stmt = begin_omp_structured_block ();
43818 unsigned int save = cp_parser_begin_omp_structured_block (parser);
43820 cp_parser_statement (parser, NULL_TREE, false, NULL);
43821 while (true)
43823 cp_token *token = cp_lexer_peek_token (parser->lexer);
43825 if (token->type == CPP_CLOSE_BRACE
43826 || token->type == CPP_EOF
43827 || token->type == CPP_PRAGMA_EOL
43828 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END)
43829 || (kind != PRAGMA_NONE
43830 && cp_parser_pragma_kind (token) == kind))
43831 break;
43833 if (kind != PRAGMA_NONE
43834 && cp_parser_omp_section_scan (parser,
43835 kind == PRAGMA_OMP_SCAN
43836 ? "scan" : "section", false))
43837 break;
43839 cp_parser_statement (parser, NULL_TREE, false, NULL);
43842 cp_parser_end_omp_structured_block (parser, save);
43843 return finish_omp_structured_block (stmt);
43847 /* OpenMP 5.0:
43849 scan-loop-body:
43850 { structured-block scan-directive structured-block } */
43852 static void
43853 cp_parser_omp_scan_loop_body (cp_parser *parser)
43855 tree substmt, clauses = NULL_TREE;
43856 bool found_scan = false;
43858 matching_braces braces;
43859 if (!braces.require_open (parser))
43860 return;
43862 cp_token *tok = cp_lexer_peek_token (parser->lexer);
43863 if (cp_parser_pragma_kind (tok) != PRAGMA_OMP_SCAN)
43864 substmt = cp_parser_omp_structured_block_sequence (parser, PRAGMA_OMP_SCAN);
43865 else
43867 warning_at (tok->location, 0, "%<#pragma omp scan%> with zero preceding "
43868 "executable statements");
43869 substmt = build_empty_stmt (tok->location);
43871 substmt = build2 (OMP_SCAN, void_type_node, substmt, NULL_TREE);
43872 add_stmt (substmt);
43874 tok = cp_lexer_peek_token (parser->lexer);
43875 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SCAN)
43877 enum omp_clause_code clause = OMP_CLAUSE_ERROR;
43878 found_scan = true;
43880 cp_lexer_consume_token (parser->lexer);
43882 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
43883 cp_lexer_consume_token (parser->lexer);
43885 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43887 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43888 const char *p = IDENTIFIER_POINTER (id);
43889 if (strcmp (p, "inclusive") == 0)
43890 clause = OMP_CLAUSE_INCLUSIVE;
43891 else if (strcmp (p, "exclusive") == 0)
43892 clause = OMP_CLAUSE_EXCLUSIVE;
43894 if (clause != OMP_CLAUSE_ERROR)
43896 cp_lexer_consume_token (parser->lexer);
43897 clauses = cp_parser_omp_var_list (parser, clause, NULL_TREE);
43899 else
43900 cp_parser_error (parser, "expected %<inclusive%> or "
43901 "%<exclusive%> clause");
43903 cp_parser_require_pragma_eol (parser, tok);
43905 else
43906 error ("expected %<#pragma omp scan%>");
43908 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
43909 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
43910 substmt = cp_parser_omp_structured_block_sequence (parser, PRAGMA_NONE);
43911 else
43913 if (found_scan)
43914 warning_at (tok->location, 0, "%<#pragma omp scan%> with zero "
43915 "succeeding executable statements");
43916 substmt = build_empty_stmt (tok->location);
43918 substmt = build2_loc (tok->location, OMP_SCAN, void_type_node, substmt,
43919 clauses);
43920 add_stmt (substmt);
43922 braces.require_close (parser);
43926 /* This function parses a single level of a loop nest, invoking itself
43927 recursively if necessary.
43929 loop-nest :: for (...) loop-body
43930 loop-body :: loop-nest
43931 | { [intervening-code] loop-body [intervening-code] }
43932 | final-loop-body
43933 intervening-code :: structured-block-sequence
43934 final-loop-body :: structured-block
43936 For a collapsed loop nest, only a single OMP_FOR is built, pulling out
43937 all the iterator information from the inner loops into vectors in the
43938 parser->omp_for_parse_state structure.
43940 In the "range for" case, it is transformed into a regular "for" iterator
43941 by introducing some temporary variables for the begin/end,
43942 as well as bindings of the actual iteration variables which are
43943 injected into the body of the loop.
43945 Initialization code for iterator variables may end up either in the
43946 init vector (simple assignments), in omp_for_parse_state->pre_body
43947 (decl_exprs for iterators bound in the for statement), or in the
43948 scope surrounding this level of loop initialization.
43950 The scopes of class iterator variables and their finalizers need to
43951 be adjusted after parsing so that all of the initialization happens
43952 in a scope surrounding all of the intervening and body code. For
43953 this reason we separately store the initialization and body blocks
43954 for each level of loops in the omp_for_parse_state structure and
43955 reassemble/reorder them in cp_parser_omp_for. See additional
43956 comments there about the use of placeholders, etc. */
43958 static tree
43959 cp_parser_omp_loop_nest (cp_parser *parser, bool *if_p)
43961 tree decl, cond, incr, init;
43962 tree orig_init, real_decl, orig_decl;
43963 tree init_block, body_block;
43964 tree init_placeholder, body_placeholder;
43965 tree init_scope;
43966 tree this_pre_body = NULL_TREE;
43967 bool moreloops;
43968 unsigned char save_in_statement;
43969 tree add_private_clause = NULL_TREE;
43970 location_t loc;
43971 bool is_range_for = false;
43972 tree sl = NULL_TREE;
43973 struct omp_for_parse_data *omp_for_parse_state
43974 = parser->omp_for_parse_state;
43975 gcc_assert (omp_for_parse_state);
43976 int depth = omp_for_parse_state->depth;
43978 /* We have already matched the FOR token but not consumed it yet. */
43979 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR));
43980 loc = cp_lexer_consume_token (parser->lexer)->location;
43982 /* Forbid break/continue in the loop initializer, condition, and
43983 increment expressions. */
43984 save_in_statement = parser->in_statement;
43985 parser->in_statement = IN_OMP_BLOCK;
43987 /* We are not in intervening code now. */
43988 omp_for_parse_state->in_intervening_code = false;
43990 /* Don't create location wrapper nodes within an OpenMP "for"
43991 statement. */
43992 auto_suppress_location_wrappers sentinel;
43994 matching_parens parens;
43995 if (!parens.require_open (parser))
43996 return NULL;
43998 init = orig_init = decl = real_decl = orig_decl = NULL_TREE;
44000 init_placeholder = build_stmt (input_location, EXPR_STMT,
44001 integer_zero_node);
44002 vec_safe_push (omp_for_parse_state->init_placeholderv, init_placeholder);
44004 /* The init_block acts as a container for this level of loop goo. */
44005 init_block = push_stmt_list ();
44006 vec_safe_push (omp_for_parse_state->init_blockv, init_block);
44008 /* Wrap a scope around this entire level of loop to hold bindings
44009 of loop iteration variables. We can't insert them directly
44010 in the containing scope because that would cause their visibility to
44011 be incorrect with respect to intervening code after this loop.
44012 We will combine the nested init_scopes in postprocessing after the
44013 entire loop is parsed. */
44014 init_scope = begin_compound_stmt (0);
44016 /* Now we need another level of statement list container to capture the
44017 initialization (and possible finalization) bits. In some cases this
44018 container may be popped off during initializer parsing to store code in
44019 INIT or THIS_PRE_BODY, depending on the form of initialization. If
44020 we have a class iterator we will pop it at the end of parsing this
44021 level, so the cleanups are handled correctly. */
44022 sl = push_stmt_list ();
44024 if (omp_for_parse_state->code != OACC_LOOP && cxx_dialect >= cxx11)
44026 /* Save tokens so that we can put them back. */
44027 cp_lexer_save_tokens (parser->lexer);
44029 /* Look for ':' that is not nested in () or {}. */
44030 is_range_for
44031 = (cp_parser_skip_to_closing_parenthesis_1 (parser,
44032 /*recovering=*/false,
44033 CPP_COLON,
44034 /*consume_paren=*/
44035 false) == -1);
44037 /* Roll back the tokens we skipped. */
44038 cp_lexer_rollback_tokens (parser->lexer);
44040 if (is_range_for)
44042 bool saved_colon_corrects_to_scope_p
44043 = parser->colon_corrects_to_scope_p;
44045 /* A colon is used in range-based for. */
44046 parser->colon_corrects_to_scope_p = false;
44048 /* Parse the declaration. */
44049 cp_parser_simple_declaration (parser,
44050 /*function_definition_allowed_p=*/
44051 false, &decl);
44052 parser->colon_corrects_to_scope_p
44053 = saved_colon_corrects_to_scope_p;
44055 cp_parser_require (parser, CPP_COLON, RT_COLON);
44057 init = cp_parser_range_for (parser, NULL_TREE, NULL_TREE, decl,
44058 false, 0, false, true);
44060 cp_convert_omp_range_for (this_pre_body, sl, decl,
44061 orig_decl, init, orig_init,
44062 cond, incr);
44064 if (omp_for_parse_state->ordered_cl)
44065 error_at (OMP_CLAUSE_LOCATION (omp_for_parse_state->ordered_cl),
44066 "%<ordered%> clause with parameter on "
44067 "range-based %<for%> loop");
44069 goto parse_close_paren;
44073 add_private_clause
44074 = cp_parser_omp_for_loop_init (parser, this_pre_body, sl,
44075 init, orig_init, decl, real_decl);
44077 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
44079 /* If the iteration variable was introduced via a declaration in the
44080 for statement, DECL points at it. Otherwise DECL is null and
44081 REAL_DECL is a variable previously declared in an outer scope.
44082 Make REAL_DECL point at the iteration variable no matter where it
44083 was introduced. */
44084 if (decl)
44085 real_decl = decl;
44087 /* Some clauses treat iterator variables specially. */
44088 if (omp_for_parse_state->cclauses != NULL
44089 && omp_for_parse_state->cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
44090 && real_decl != NULL_TREE
44091 && omp_for_parse_state->code != OMP_LOOP)
44093 tree *c;
44094 for (c = &(omp_for_parse_state->cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]);
44095 *c ; )
44096 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
44097 && OMP_CLAUSE_DECL (*c) == real_decl)
44099 error_at (loc, "iteration variable %qD"
44100 " should not be firstprivate", real_decl);
44101 *c = OMP_CLAUSE_CHAIN (*c);
44103 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
44104 && OMP_CLAUSE_DECL (*c) == real_decl)
44106 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
44107 tree l = *c;
44108 *c = OMP_CLAUSE_CHAIN (*c);
44109 if (omp_for_parse_state->code == OMP_SIMD)
44111 OMP_CLAUSE_CHAIN (l)
44112 = omp_for_parse_state->cclauses[C_OMP_CLAUSE_SPLIT_FOR];
44113 omp_for_parse_state->cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
44115 else
44117 OMP_CLAUSE_CHAIN (l) = omp_for_parse_state->clauses;
44118 omp_for_parse_state->clauses = l;
44120 add_private_clause = NULL_TREE;
44122 else
44124 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
44125 && OMP_CLAUSE_DECL (*c) == real_decl)
44126 add_private_clause = NULL_TREE;
44127 c = &OMP_CLAUSE_CHAIN (*c);
44131 if (add_private_clause)
44133 tree c;
44134 for (c = omp_for_parse_state->clauses; c ; c = OMP_CLAUSE_CHAIN (c))
44136 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
44137 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
44138 && OMP_CLAUSE_DECL (c) == decl)
44139 break;
44140 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
44141 && OMP_CLAUSE_DECL (c) == decl)
44142 error_at (loc, "iteration variable %qD "
44143 "should not be firstprivate",
44144 decl);
44145 else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
44146 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
44147 && OMP_CLAUSE_DECL (c) == decl)
44148 error_at (loc, "iteration variable %qD should not be reduction",
44149 decl);
44151 if (c == NULL)
44153 if ((omp_for_parse_state->code == OMP_SIMD
44154 && omp_for_parse_state->count != 1)
44155 || omp_for_parse_state->code == OMP_LOOP)
44156 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
44157 else if (omp_for_parse_state->code != OMP_SIMD)
44158 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
44159 else
44160 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
44161 OMP_CLAUSE_DECL (c) = add_private_clause;
44162 c = finish_omp_clauses (c, C_ORT_OMP);
44163 if (c)
44165 OMP_CLAUSE_CHAIN (c) = omp_for_parse_state->clauses;
44166 omp_for_parse_state->clauses = c;
44167 /* For linear, signal that we need to fill up
44168 the so far unknown linear step. */
44169 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
44170 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
44175 cond = NULL;
44176 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
44177 cond = cp_parser_omp_for_cond (parser, decl, omp_for_parse_state->code);
44178 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
44180 incr = NULL;
44181 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
44183 /* If decl is an iterator, preserve the operator on decl
44184 until finish_omp_for. */
44185 if (real_decl
44186 && ((processing_template_decl
44187 && (TREE_TYPE (real_decl) == NULL_TREE
44188 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl))))
44189 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
44190 incr = cp_parser_omp_for_incr (parser, real_decl);
44191 else
44192 incr = cp_parser_expression (parser);
44193 protected_set_expr_location_if_unset (incr, input_location);
44196 parse_close_paren:
44197 if (!parens.require_close (parser))
44198 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
44199 /*or_comma=*/false,
44200 /*consume_paren=*/true);
44202 /* We've parsed all the for (...) stuff now. Store the bits. */
44203 TREE_VEC_ELT (omp_for_parse_state->declv, depth) = decl;
44204 TREE_VEC_ELT (omp_for_parse_state->initv, depth) = init;
44205 TREE_VEC_ELT (omp_for_parse_state->condv, depth) = cond;
44206 TREE_VEC_ELT (omp_for_parse_state->incrv, depth) = incr;
44207 if (orig_init)
44209 omp_for_parse_state->orig_inits.safe_grow_cleared (depth + 1, true);
44210 omp_for_parse_state->orig_inits[depth] = orig_init;
44212 if (orig_decl)
44214 if (!omp_for_parse_state->orig_declv)
44215 omp_for_parse_state->orig_declv
44216 = copy_node (omp_for_parse_state->declv);
44217 TREE_VEC_ELT (omp_for_parse_state->orig_declv, depth) = orig_decl;
44219 else if (omp_for_parse_state->orig_declv)
44220 TREE_VEC_ELT (omp_for_parse_state->orig_declv, depth) = decl;
44221 if (this_pre_body)
44222 append_to_statement_list_force (this_pre_body,
44223 &(omp_for_parse_state->pre_body));
44225 /* Start a nested block for the loop body. */
44226 body_placeholder = build_stmt (input_location, EXPR_STMT,
44227 integer_zero_node);
44228 vec_safe_push (omp_for_parse_state->body_placeholderv, body_placeholder);
44229 body_block = push_stmt_list ();
44230 vec_safe_push (omp_for_parse_state->body_blockv, body_block);
44232 moreloops = depth < omp_for_parse_state->count - 1;
44233 omp_for_parse_state->want_nested_loop = moreloops;
44234 if (moreloops && cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
44236 omp_for_parse_state->depth++;
44237 add_stmt (cp_parser_omp_loop_nest (parser, if_p));
44238 omp_for_parse_state->depth--;
44240 else if (moreloops
44241 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
44243 /* This is the open brace in the loop-body grammar production. Rather
44244 than trying to special-case braces, just parse it as a compound
44245 statement and handle the nested loop-body case there. Note that
44246 when we see a further open brace inside the compound statement
44247 loop-body, we don't know whether it is the start of intervening
44248 code that is a compound statement, or a level of braces
44249 surrounding a nested loop-body. Use the WANT_NESTED_LOOP state
44250 bit to ensure we have only one nested loop at each level. */
44252 omp_for_parse_state->in_intervening_code = true;
44253 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
44254 omp_for_parse_state->in_intervening_code = false;
44256 if (omp_for_parse_state->want_nested_loop)
44258 /* We have already parsed the whole loop body and not found a
44259 nested loop. */
44260 error_at (omp_for_parse_state->for_loc,
44261 "not enough nested loops");
44262 omp_for_parse_state->fail = true;
44264 if_p = NULL;
44266 else
44268 /* This is the final-loop-body case in the grammar: we have something
44269 that is not a FOR and not an open brace. */
44270 if (moreloops)
44272 /* If we were expecting a nested loop, give an error and mark
44273 that parsing has failed, and try to recover by parsing the
44274 body as regular code without further collapsing. */
44275 error_at (omp_for_parse_state->for_loc,
44276 "not enough nested loops");
44277 omp_for_parse_state->fail = true;
44279 parser->in_statement = IN_OMP_FOR;
44281 /* Generate the parts of range for that belong in the loop body,
44282 to be executed on every iteration. This includes setting the
44283 user-declared decomposition variables from the compiler-generated
44284 temporaries that are the real iteration variables for OMP_FOR.
44285 FIXME: Not sure if this is correct with respect to visibility
44286 of the variables from intervening code. However, putting this
44287 code in each level of loop instead of all around the innermost
44288 body also makes the decomposition variables visible to the
44289 inner for init/bound/step exressions, which is not supposed to
44290 happen and causes test failures. */
44291 if (omp_for_parse_state->orig_declv)
44292 for (int i = 0; i < omp_for_parse_state->count; i++)
44294 tree o = TREE_VEC_ELT (omp_for_parse_state->orig_declv, i);
44295 tree d = TREE_VEC_ELT (omp_for_parse_state->declv, i);
44296 if (o != d)
44297 cp_finish_omp_range_for (o, d);
44300 /* Now parse the final-loop-body for the innermost loop. */
44301 parser->omp_for_parse_state = NULL;
44302 if (omp_for_parse_state->inscan)
44303 cp_parser_omp_scan_loop_body (parser);
44304 else
44305 cp_parser_statement (parser, NULL_TREE, false, if_p);
44306 parser->omp_for_parse_state = omp_for_parse_state;
44308 parser->in_statement = save_in_statement;
44309 omp_for_parse_state->want_nested_loop = false;
44310 omp_for_parse_state->in_intervening_code = true;
44312 /* Pop and remember the body block. Add the body placeholder
44313 to the surrounding statement list instead. This is just a unique
44314 token that will be replaced when we reassemble the generated
44315 code for the entire omp for statement. */
44316 body_block = pop_stmt_list (body_block);
44317 omp_for_parse_state->body_blockv[depth] = body_block;
44318 add_stmt (body_placeholder);
44320 /* Pop and remember the init block. */
44321 if (sl)
44322 add_stmt (pop_stmt_list (sl));
44323 finish_compound_stmt (init_scope);
44324 init_block = pop_stmt_list (init_block);
44325 omp_for_parse_state->init_blockv[depth] = init_block;
44327 /* Return the init placeholder rather than the remembered init block.
44328 Again, this is just a unique cookie that will be used to reassemble
44329 code pieces when the entire omp for statement has been parsed. */
44330 return init_placeholder;
44333 /* Worker for find_structured_blocks. *TP points to a STATEMENT_LIST
44334 and ITER is the element that is or contains a nested loop. This
44335 function moves the statements before and after ITER into
44336 OMP_STRUCTURED_BLOCKs and modifies *TP. */
44337 static void
44338 insert_structured_blocks (tree *tp, tree_stmt_iterator iter)
44340 tree sl = push_stmt_list ();
44341 for (tree_stmt_iterator i = tsi_start (*tp); !tsi_end_p (i); )
44342 if (i == iter)
44344 sl = pop_stmt_list (sl);
44345 if (TREE_CODE (sl) != STATEMENT_LIST || !tsi_end_p (tsi_start (sl)))
44346 tsi_link_before (&i,
44347 build1 (OMP_STRUCTURED_BLOCK, void_type_node, sl),
44348 TSI_SAME_STMT);
44349 i++;
44350 sl = push_stmt_list ();
44352 else
44354 tree s = tsi_stmt (i);
44355 tsi_delink (&i); /* Advances i to next statement. */
44356 add_stmt (s);
44358 sl = pop_stmt_list (sl);
44359 if (TREE_CODE (sl) != STATEMENT_LIST || !tsi_end_p (tsi_start (sl)))
44360 tsi_link_after (&iter,
44361 build1 (OMP_STRUCTURED_BLOCK, void_type_node, sl),
44362 TSI_SAME_STMT);
44365 /* Helper to find and mark structured blocks in intervening code for a
44366 single loop level with markers for later error checking. *TP is the
44367 piece of code to be marked and INNER is the inner loop placeholder.
44368 Returns true if INNER was found (recursively) in *TP. */
44369 static bool
44370 find_structured_blocks (tree *tp, tree inner)
44372 if (*tp == inner)
44373 return true;
44374 else if (TREE_CODE (*tp) == BIND_EXPR)
44375 return find_structured_blocks (&(BIND_EXPR_BODY (*tp)), inner);
44376 else if (TREE_CODE (*tp) == STATEMENT_LIST)
44378 for (tree_stmt_iterator i = tsi_start (*tp); !tsi_end_p (i); ++i)
44380 tree *p = tsi_stmt_ptr (i);
44381 /* The normal case is that there is no intervening code and we
44382 do not have to insert any OMP_STRUCTURED_BLOCK markers. */
44383 if (find_structured_blocks (p, inner))
44385 if (!(i == tsi_start (*tp) && i == tsi_last (*tp)))
44386 insert_structured_blocks (tp, i);
44387 return true;
44390 return false;
44392 else if (TREE_CODE (*tp) == TRY_FINALLY_EXPR)
44393 return find_structured_blocks (&(TREE_OPERAND (*tp, 0)), inner);
44394 else if (TREE_CODE (*tp) == CLEANUP_STMT)
44395 return find_structured_blocks (&(CLEANUP_BODY (*tp)), inner);
44396 else
44397 return false;
44400 /* Helpers used for relinking tree structures: In tree rooted at
44401 CONTEXT, replace ORIG with REPLACEMENT. If FLATTEN is true, try to combine
44402 nested BIND_EXPRs. Gives an assertion if it fails to find ORIG. */
44404 struct sit_data {
44405 tree orig;
44406 tree repl;
44407 bool flatten;
44410 static tree
44411 substitute_in_tree_walker (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
44412 void *dp)
44414 struct sit_data *sit = (struct sit_data *)dp;
44415 if (*tp == sit->orig)
44417 *tp = sit->repl;
44418 return *tp;
44420 /* Remove redundant BIND_EXPRs with no bindings even when not specifically
44421 trying to flatten. */
44422 else if (TREE_CODE (*tp) == BIND_EXPR
44423 && BIND_EXPR_BODY (*tp) == sit->orig
44424 && !BIND_EXPR_VARS (*tp)
44425 && (sit->flatten || TREE_CODE (sit->repl) == BIND_EXPR))
44427 *tp = sit->repl;
44428 return *tp;
44430 else if (sit->flatten
44431 && TREE_CODE (*tp) == BIND_EXPR
44432 && TREE_CODE (sit->repl) == BIND_EXPR)
44434 if (BIND_EXPR_BODY (*tp) == sit->orig)
44436 /* Merge binding lists for two directly nested BIND_EXPRs,
44437 keeping the outer one. */
44438 BIND_EXPR_VARS (*tp) = chainon (BIND_EXPR_VARS (*tp),
44439 BIND_EXPR_VARS (sit->repl));
44440 BIND_EXPR_BODY (*tp) = BIND_EXPR_BODY (sit->repl);
44441 return *tp;
44443 else if (TREE_CODE (BIND_EXPR_BODY (*tp)) == STATEMENT_LIST)
44444 /* There might be a statement list containing cleanup_points
44445 etc between the two levels of BIND_EXPR. We can still merge
44446 them, again keeping the outer BIND_EXPR. */
44447 for (tree_stmt_iterator i = tsi_start (BIND_EXPR_BODY (*tp));
44448 !tsi_end_p (i); ++i)
44450 tree *p = tsi_stmt_ptr (i);
44451 if (*p == sit->orig)
44453 BIND_EXPR_VARS (*tp) = chainon (BIND_EXPR_VARS (*tp),
44454 BIND_EXPR_VARS (sit->repl));
44455 *p = BIND_EXPR_BODY (sit->repl);
44456 return *tp;
44460 return NULL;
44463 static void
44464 substitute_in_tree (tree *context, tree orig, tree repl, bool flatten)
44466 struct sit_data data;
44468 gcc_assert (*context && orig && repl);
44469 if (TREE_CODE (repl) == BIND_EXPR && !BIND_EXPR_VARS (repl))
44470 repl = BIND_EXPR_BODY (repl);
44471 data.orig = orig;
44472 data.repl = repl;
44473 data.flatten = flatten;
44475 tree result = cp_walk_tree (context, substitute_in_tree_walker,
44476 (void *)&data, NULL);
44477 gcc_assert (result != NULL_TREE);
44480 /* Walker to patch up the BLOCK_NODE hierarchy after the above surgery.
44481 *DP is is the parent block. */
44483 static tree
44484 fixup_blocks_walker (tree *tp, int *walk_subtrees, void *dp)
44486 tree superblock = *(tree *)dp;
44488 if (TREE_CODE (*tp) == BIND_EXPR)
44490 tree block = BIND_EXPR_BLOCK (*tp);
44491 if (superblock)
44493 BLOCK_SUPERCONTEXT (block) = superblock;
44494 BLOCK_CHAIN (block) = BLOCK_SUBBLOCKS (superblock);
44495 BLOCK_SUBBLOCKS (superblock) = block;
44497 BLOCK_SUBBLOCKS (block) = NULL_TREE;
44498 cp_walk_tree (&BIND_EXPR_BODY (*tp), fixup_blocks_walker,
44499 (void *)&block, NULL);
44500 *walk_subtrees = 0;
44503 return NULL;
44506 /* Parse the restricted form of the for statement allowed by OpenMP. */
44508 static tree
44509 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
44510 tree *cclauses, bool *if_p)
44512 tree ret;
44513 tree cl, ordered_cl = NULL_TREE;
44514 int collapse = 1, ordered = 0;
44515 unsigned int count;
44516 bool tiling = false;
44517 bool inscan = false;
44518 struct omp_for_parse_data data;
44519 struct omp_for_parse_data *save_data = parser->omp_for_parse_state;
44520 tree result;
44521 location_t loc_first = cp_lexer_peek_token (parser->lexer)->location;
44523 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
44524 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
44525 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
44526 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
44528 tiling = true;
44529 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
44531 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
44532 && OMP_CLAUSE_ORDERED_EXPR (cl))
44534 ordered_cl = cl;
44535 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
44537 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_REDUCTION
44538 && OMP_CLAUSE_REDUCTION_INSCAN (cl)
44539 && (code == OMP_SIMD || code == OMP_FOR))
44540 inscan = true;
44542 if (ordered && ordered < collapse)
44544 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
44545 "%<ordered%> clause parameter is less than %<collapse%>");
44546 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
44547 = build_int_cst (NULL_TREE, collapse);
44548 ordered = collapse;
44551 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
44552 count = ordered ? ordered : collapse;
44554 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
44556 cp_parser_error (parser, "for statement expected");
44557 return NULL;
44560 /* Initialize parse state for recursive descent. */
44561 data.declv = make_tree_vec (count);
44562 data.initv = make_tree_vec (count);
44563 data.condv = make_tree_vec (count);
44564 data.incrv = make_tree_vec (count);
44565 data.pre_body = NULL_TREE;
44566 data.for_loc = cp_lexer_peek_token (parser->lexer)->location;
44567 data.count = count;
44568 data.depth = 0;
44569 data.want_nested_loop = true;
44570 data.ordered = ordered > 0;
44571 data.in_intervening_code = false;
44572 data.perfect_nesting_fail = false;
44573 data.fail = false;
44574 data.inscan = inscan;
44575 data.saw_intervening_code = false;
44576 data.code = code;
44577 data.orig_declv = NULL_TREE;
44578 data.clauses = clauses;
44579 data.cclauses = cclauses;
44580 data.ordered_cl = ordered_cl;
44581 parser->omp_for_parse_state = &data;
44583 cp_parser_omp_loop_nest (parser, if_p);
44585 /* Bomb out early if there was an error (not enough loops, etc). */
44586 if (data.fail || data.declv == NULL_TREE)
44588 parser->omp_for_parse_state = save_data;
44589 return NULL_TREE;
44592 /* Relink the init and body blocks that were built during parsing. At
44593 this point we have a structure nested like
44594 init 0
44595 body 0
44596 init 1
44597 body 1
44598 init 2
44599 body 2
44600 and we want to turn it into
44601 init 0
44602 init 1
44603 init 2
44604 omp_for
44605 body 0
44606 body 1
44607 body 2
44608 We also need to flatten the init blocks, as some code for later
44609 processing of combined directives gets confused otherwise. */
44611 gcc_assert (vec_safe_length (data.init_blockv) == count);
44612 gcc_assert (vec_safe_length (data.body_blockv) == count);
44613 gcc_assert (vec_safe_length (data.init_placeholderv) == count);
44614 gcc_assert (vec_safe_length (data.body_placeholderv) == count);
44616 /* First insert markers for structured blocks for intervening code in
44617 the loop bodies. */
44618 for (unsigned int i = 0; i < count - 1; i++)
44620 bool good = find_structured_blocks (&(data.body_blockv[i]),
44621 data.init_placeholderv[i+1]);
44622 gcc_assert (good);
44625 /* Do the substitution from the inside out. */
44626 for (unsigned int i = count - 1; i > 0; i--)
44628 substitute_in_tree (&(data.body_blockv[i-1]),
44629 data.init_placeholderv[i],
44630 data.body_blockv[i], false);
44631 substitute_in_tree (&(data.init_blockv[i-1]),
44632 data.body_placeholderv[i-1],
44633 data.init_blockv[i], true);
44636 /* Generate the OMP_FOR. Note finish_omp_for adds the OMP_FOR
44637 (and possibly other stuff) to the current statement list but
44638 returns a pointer to the OMP_FOR itself, or null in case of error. */
44639 result = push_stmt_list ();
44640 ret = finish_omp_for (loc_first, code, data.declv, data.orig_declv,
44641 data.initv, data.condv, data.incrv,
44642 data.body_blockv[0],
44643 data.pre_body, &data.orig_inits, data.clauses);
44644 result = pop_stmt_list (result);
44646 /* Check for errors involving lb/ub/incr expressions referencing
44647 variables declared in intervening code. */
44648 if (data.saw_intervening_code
44649 && !c_omp_check_loop_binding_exprs (ret, &data.orig_inits))
44650 ret = NULL_TREE;
44652 if (ret)
44654 /* Splice the omp_for into the nest of init blocks. */
44655 substitute_in_tree (&(data.init_blockv[0]),
44656 data.body_placeholderv[count - 1],
44657 result, true);
44659 /* Some later processing for combined directives assumes
44660 that the BIND_EXPR containing range for variables appears
44661 at top level in the OMP_FOR body. Fix that up if it's
44662 not the case, e.g. because there is intervening code. */
44663 if (code != OACC_LOOP)
44664 finish_omp_for_block (data.init_blockv[0], ret);
44666 /* Clean up the block subblock/superblock links. Per comment in
44667 begin_compound_stmt, "we don't build BLOCK nodes when processing
44668 templates", so skip this step in that case. */
44669 if (!processing_template_decl)
44671 tree superblock = NULL_TREE;
44672 cp_walk_tree (&data.init_blockv[0], fixup_blocks_walker,
44673 (void *)&superblock, NULL);
44676 /* Finally record the result. */
44677 add_stmt (data.init_blockv[0]);
44680 parser->omp_for_parse_state = save_data;
44681 return ret;
44684 /* Helper function for OpenMP parsing, split clauses and call
44685 finish_omp_clauses on each of the set of clauses afterwards. */
44687 static void
44688 cp_omp_split_clauses (location_t loc, enum tree_code code,
44689 omp_clause_mask mask, tree clauses, tree *cclauses)
44691 int i;
44692 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
44693 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
44694 if (cclauses[i])
44695 cclauses[i] = finish_omp_clauses (cclauses[i],
44696 i == C_OMP_CLAUSE_SPLIT_TARGET
44697 ? C_ORT_OMP_TARGET : C_ORT_OMP);
44700 /* OpenMP 5.0:
44701 #pragma omp loop loop-clause[optseq] new-line
44702 for-loop */
44704 #define OMP_LOOP_CLAUSE_MASK \
44705 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
44706 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
44707 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
44708 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
44709 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_BIND) \
44710 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
44712 static tree
44713 cp_parser_omp_loop (cp_parser *parser, cp_token *pragma_tok,
44714 char *p_name, omp_clause_mask mask, tree *cclauses,
44715 bool *if_p)
44717 tree clauses, sb, ret;
44718 unsigned int save;
44719 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
44721 strcat (p_name, " loop");
44722 mask |= OMP_LOOP_CLAUSE_MASK;
44724 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
44725 cclauses == NULL);
44726 if (cclauses)
44728 cp_omp_split_clauses (loc, OMP_LOOP, mask, clauses, cclauses);
44729 clauses = cclauses[C_OMP_CLAUSE_SPLIT_LOOP];
44732 keep_next_level (true);
44733 sb = begin_omp_structured_block ();
44734 save = cp_parser_begin_omp_structured_block (parser);
44736 ret = cp_parser_omp_for_loop (parser, OMP_LOOP, clauses, cclauses, if_p);
44738 cp_parser_end_omp_structured_block (parser, save);
44739 add_stmt (finish_omp_structured_block (sb));
44741 return ret;
44744 /* OpenMP 4.0:
44745 #pragma omp simd simd-clause[optseq] new-line
44746 for-loop */
44748 #define OMP_SIMD_CLAUSE_MASK \
44749 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
44750 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
44751 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
44752 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
44753 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
44754 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
44755 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
44756 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
44757 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
44758 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL) \
44759 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
44761 static tree
44762 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
44763 char *p_name, omp_clause_mask mask, tree *cclauses,
44764 bool *if_p)
44766 tree clauses, sb, ret;
44767 unsigned int save;
44768 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
44770 strcat (p_name, " simd");
44771 mask |= OMP_SIMD_CLAUSE_MASK;
44773 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
44774 cclauses == NULL);
44775 if (cclauses)
44777 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
44778 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
44781 keep_next_level (true);
44782 sb = begin_omp_structured_block ();
44783 save = cp_parser_begin_omp_structured_block (parser);
44785 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
44787 cp_parser_end_omp_structured_block (parser, save);
44788 add_stmt (finish_omp_structured_block (sb));
44790 return ret;
44793 /* OpenMP 2.5:
44794 #pragma omp for for-clause[optseq] new-line
44795 for-loop
44797 OpenMP 4.0:
44798 #pragma omp for simd for-simd-clause[optseq] new-line
44799 for-loop */
44801 #define OMP_FOR_CLAUSE_MASK \
44802 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
44803 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
44804 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
44805 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
44806 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
44807 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
44808 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
44809 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
44810 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
44811 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
44812 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
44814 static tree
44815 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
44816 char *p_name, omp_clause_mask mask, tree *cclauses,
44817 bool *if_p)
44819 tree clauses, sb, ret;
44820 unsigned int save;
44821 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
44823 strcat (p_name, " for");
44824 mask |= OMP_FOR_CLAUSE_MASK;
44825 /* parallel for{, simd} disallows nowait clause, but for
44826 target {teams distribute ,}parallel for{, simd} it should be accepted. */
44827 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
44828 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
44829 /* Composite distribute parallel for{, simd} disallows ordered clause. */
44830 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
44831 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
44833 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
44835 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
44836 const char *p = IDENTIFIER_POINTER (id);
44838 if (strcmp (p, "simd") == 0)
44840 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
44841 if (cclauses == NULL)
44842 cclauses = cclauses_buf;
44844 cp_lexer_consume_token (parser->lexer);
44845 if (!flag_openmp) /* flag_openmp_simd */
44846 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
44847 cclauses, if_p);
44848 sb = begin_omp_structured_block ();
44849 save = cp_parser_begin_omp_structured_block (parser);
44850 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
44851 cclauses, if_p);
44852 cp_parser_end_omp_structured_block (parser, save);
44853 tree body = finish_omp_structured_block (sb);
44854 if (ret == NULL)
44855 return ret;
44856 ret = make_node (OMP_FOR);
44857 TREE_TYPE (ret) = void_type_node;
44858 OMP_FOR_BODY (ret) = body;
44859 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
44860 SET_EXPR_LOCATION (ret, loc);
44861 add_stmt (ret);
44862 return ret;
44865 if (!flag_openmp) /* flag_openmp_simd */
44867 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44868 return NULL_TREE;
44871 /* Composite distribute parallel for disallows linear clause. */
44872 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
44873 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
44875 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
44876 cclauses == NULL);
44877 if (cclauses)
44879 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
44880 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
44883 keep_next_level (true);
44884 sb = begin_omp_structured_block ();
44885 save = cp_parser_begin_omp_structured_block (parser);
44887 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
44889 cp_parser_end_omp_structured_block (parser, save);
44890 add_stmt (finish_omp_structured_block (sb));
44892 return ret;
44895 static tree cp_parser_omp_taskloop (cp_parser *, cp_token *, char *,
44896 omp_clause_mask, tree *, bool *);
44898 /* OpenMP 2.5:
44899 # pragma omp master new-line
44900 structured-block */
44902 static tree
44903 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok,
44904 char *p_name, omp_clause_mask mask, tree *cclauses,
44905 bool *if_p)
44907 tree clauses, sb, ret;
44908 unsigned int save;
44909 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
44911 strcat (p_name, " master");
44913 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
44915 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
44916 const char *p = IDENTIFIER_POINTER (id);
44918 if (strcmp (p, "taskloop") == 0)
44920 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
44921 if (cclauses == NULL)
44922 cclauses = cclauses_buf;
44924 cp_lexer_consume_token (parser->lexer);
44925 if (!flag_openmp) /* flag_openmp_simd */
44926 return cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
44927 cclauses, if_p);
44928 sb = begin_omp_structured_block ();
44929 save = cp_parser_begin_omp_structured_block (parser);
44930 ret = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
44931 cclauses, if_p);
44932 cp_parser_end_omp_structured_block (parser, save);
44933 tree body = finish_omp_structured_block (sb);
44934 if (ret == NULL)
44935 return ret;
44936 ret = c_finish_omp_master (loc, body);
44937 OMP_MASTER_COMBINED (ret) = 1;
44938 return ret;
44941 if (!flag_openmp) /* flag_openmp_simd */
44943 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44944 return NULL_TREE;
44947 if (cclauses)
44949 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
44950 false);
44951 cp_omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
44953 else
44954 cp_parser_require_pragma_eol (parser, pragma_tok);
44956 return c_finish_omp_master (loc,
44957 cp_parser_omp_structured_block (parser, if_p));
44960 /* OpenMP 5.1:
44961 # pragma omp masked masked-clauses new-line
44962 structured-block */
44964 #define OMP_MASKED_CLAUSE_MASK \
44965 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FILTER)
44967 static tree
44968 cp_parser_omp_masked (cp_parser *parser, cp_token *pragma_tok,
44969 char *p_name, omp_clause_mask mask, tree *cclauses,
44970 bool *if_p)
44972 tree clauses, sb, ret;
44973 unsigned int save;
44974 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
44976 strcat (p_name, " masked");
44977 mask |= OMP_MASKED_CLAUSE_MASK;
44979 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
44981 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
44982 const char *p = IDENTIFIER_POINTER (id);
44984 if (strcmp (p, "taskloop") == 0)
44986 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
44987 if (cclauses == NULL)
44988 cclauses = cclauses_buf;
44990 cp_lexer_consume_token (parser->lexer);
44991 if (!flag_openmp) /* flag_openmp_simd */
44992 return cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
44993 cclauses, if_p);
44994 sb = begin_omp_structured_block ();
44995 save = cp_parser_begin_omp_structured_block (parser);
44996 ret = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
44997 cclauses, if_p);
44998 cp_parser_end_omp_structured_block (parser, save);
44999 tree body = finish_omp_structured_block (sb);
45000 if (ret == NULL)
45001 return ret;
45002 ret = c_finish_omp_masked (loc, body,
45003 cclauses[C_OMP_CLAUSE_SPLIT_MASKED]);
45004 OMP_MASKED_COMBINED (ret) = 1;
45005 return ret;
45008 if (!flag_openmp) /* flag_openmp_simd */
45010 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45011 return NULL_TREE;
45014 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
45015 cclauses == NULL);
45016 if (cclauses)
45018 cp_omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
45019 clauses = cclauses[C_OMP_CLAUSE_SPLIT_MASKED];
45022 return c_finish_omp_masked (loc,
45023 cp_parser_omp_structured_block (parser, if_p),
45024 clauses);
45027 /* OpenMP 2.5:
45028 # pragma omp ordered new-line
45029 structured-block
45031 OpenMP 4.5:
45032 # pragma omp ordered ordered-clauses new-line
45033 structured-block */
45035 #define OMP_ORDERED_CLAUSE_MASK \
45036 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
45037 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
45039 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
45040 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
45041 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DOACROSS))
45043 static bool
45044 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
45045 enum pragma_context context, bool *if_p)
45047 location_t loc = pragma_tok->location;
45048 int n = 1;
45050 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
45051 n = 2;
45053 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_NAME))
45055 tree id = cp_lexer_peek_nth_token (parser->lexer, n)->u.value;
45056 const char *p = IDENTIFIER_POINTER (id);
45058 if (strcmp (p, "depend") == 0 || strcmp (p, "doacross") == 0)
45060 if (!flag_openmp) /* flag_openmp_simd */
45062 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45063 return false;
45065 if (context == pragma_stmt)
45067 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
45068 "%qs clause may only be used in compound "
45069 "statements", p);
45070 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45071 return true;
45073 tree clauses
45074 = cp_parser_omp_all_clauses (parser,
45075 OMP_ORDERED_DEPEND_CLAUSE_MASK,
45076 "#pragma omp ordered", pragma_tok);
45077 c_finish_omp_ordered (loc, clauses, NULL_TREE);
45078 return false;
45082 tree clauses
45083 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
45084 "#pragma omp ordered", pragma_tok);
45086 if (!flag_openmp /* flag_openmp_simd */
45087 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
45088 return false;
45090 c_finish_omp_ordered (loc, clauses,
45091 cp_parser_omp_structured_block (parser, if_p));
45092 return true;
45095 /* OpenMP 2.5:
45097 section-scope:
45098 { section-sequence }
45100 section-sequence:
45101 section-directive[opt] structured-block
45102 section-sequence section-directive structured-block */
45104 static tree
45105 cp_parser_omp_sections_scope (cp_parser *parser)
45107 tree stmt, substmt;
45108 bool error_suppress = false;
45109 cp_token *tok;
45111 matching_braces braces;
45112 if (!braces.require_open (parser))
45113 return NULL_TREE;
45115 stmt = push_stmt_list ();
45117 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
45118 != PRAGMA_OMP_SECTION
45119 && !cp_parser_omp_section_scan (parser, "section", true))
45121 substmt = cp_parser_omp_structured_block_sequence (parser,
45122 PRAGMA_OMP_SECTION);
45123 substmt = build1 (OMP_SECTION, void_type_node, substmt);
45124 add_stmt (substmt);
45127 while (1)
45129 tok = cp_lexer_peek_token (parser->lexer);
45130 if (tok->type == CPP_CLOSE_BRACE)
45131 break;
45132 if (tok->type == CPP_EOF)
45133 break;
45135 if (cp_parser_omp_section_scan (parser, "section", false))
45136 tok = cp_lexer_peek_token (parser->lexer);
45137 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
45139 cp_lexer_consume_token (parser->lexer);
45140 cp_parser_require_pragma_eol (parser, tok);
45141 error_suppress = false;
45143 else if (!error_suppress)
45145 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
45146 error_suppress = true;
45149 substmt = cp_parser_omp_structured_block_sequence (parser,
45150 PRAGMA_OMP_SECTION);
45151 substmt = build1 (OMP_SECTION, void_type_node, substmt);
45152 add_stmt (substmt);
45154 braces.require_close (parser);
45156 substmt = pop_stmt_list (stmt);
45158 stmt = make_node (OMP_SECTIONS);
45159 TREE_TYPE (stmt) = void_type_node;
45160 OMP_SECTIONS_BODY (stmt) = substmt;
45162 add_stmt (stmt);
45163 return stmt;
45166 /* OpenMP 2.5:
45167 # pragma omp sections sections-clause[optseq] newline
45168 sections-scope */
45170 #define OMP_SECTIONS_CLAUSE_MASK \
45171 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
45172 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
45173 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
45174 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
45175 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
45176 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
45178 static tree
45179 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
45180 char *p_name, omp_clause_mask mask, tree *cclauses)
45182 tree clauses, ret;
45183 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
45185 strcat (p_name, " sections");
45186 mask |= OMP_SECTIONS_CLAUSE_MASK;
45187 if (cclauses)
45188 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
45190 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
45191 cclauses == NULL);
45192 if (cclauses)
45194 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
45195 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
45198 ret = cp_parser_omp_sections_scope (parser);
45199 if (ret)
45200 OMP_SECTIONS_CLAUSES (ret) = clauses;
45202 return ret;
45205 /* OpenMP 2.5:
45206 # pragma omp parallel parallel-clause[optseq] new-line
45207 structured-block
45208 # pragma omp parallel for parallel-for-clause[optseq] new-line
45209 structured-block
45210 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
45211 structured-block
45213 OpenMP 4.0:
45214 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
45215 structured-block */
45217 #define OMP_PARALLEL_CLAUSE_MASK \
45218 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
45219 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
45220 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
45221 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
45222 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
45223 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
45224 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
45225 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
45226 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
45227 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
45229 static tree
45230 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
45231 char *p_name, omp_clause_mask mask, tree *cclauses,
45232 bool *if_p)
45234 tree stmt, clauses, block;
45235 unsigned int save;
45236 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
45238 strcat (p_name, " parallel");
45239 mask |= OMP_PARALLEL_CLAUSE_MASK;
45240 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
45241 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
45242 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
45243 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
45245 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
45247 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
45248 if (cclauses == NULL)
45249 cclauses = cclauses_buf;
45251 cp_lexer_consume_token (parser->lexer);
45252 if (!flag_openmp) /* flag_openmp_simd */
45253 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
45254 if_p);
45255 block = begin_omp_parallel ();
45256 save = cp_parser_begin_omp_structured_block (parser);
45257 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
45258 if_p);
45259 cp_parser_end_omp_structured_block (parser, save);
45260 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
45261 block);
45262 if (ret == NULL_TREE)
45263 return ret;
45264 OMP_PARALLEL_COMBINED (stmt) = 1;
45265 return stmt;
45267 /* When combined with distribute, parallel has to be followed by for.
45268 #pragma omp target parallel is allowed though. */
45269 else if (cclauses
45270 && (mask & (OMP_CLAUSE_MASK_1
45271 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
45273 error_at (loc, "expected %<for%> after %qs", p_name);
45274 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45275 return NULL_TREE;
45277 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45279 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
45280 const char *p = IDENTIFIER_POINTER (id);
45281 if (cclauses == NULL && strcmp (p, "masked") == 0)
45283 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
45284 cclauses = cclauses_buf;
45286 cp_lexer_consume_token (parser->lexer);
45287 if (!flag_openmp) /* flag_openmp_simd */
45288 return cp_parser_omp_masked (parser, pragma_tok, p_name, mask,
45289 cclauses, if_p);
45290 block = begin_omp_parallel ();
45291 save = cp_parser_begin_omp_structured_block (parser);
45292 tree ret = cp_parser_omp_masked (parser, pragma_tok, p_name, mask,
45293 cclauses, if_p);
45294 cp_parser_end_omp_structured_block (parser, save);
45295 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
45296 block);
45297 if (ret == NULL_TREE)
45298 return ret;
45299 /* masked does have just filter clause, but during gimplification
45300 isn't represented by a gimplification omp context, so for
45301 #pragma omp parallel masked don't set OMP_PARALLEL_COMBINED,
45302 so that
45303 #pragma omp parallel masked
45304 #pragma omp taskloop simd lastprivate (x)
45305 isn't confused with
45306 #pragma omp parallel masked taskloop simd lastprivate (x) */
45307 if (OMP_MASKED_COMBINED (ret))
45308 OMP_PARALLEL_COMBINED (stmt) = 1;
45309 return stmt;
45311 else if (cclauses == NULL && strcmp (p, "master") == 0)
45313 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
45314 cclauses = cclauses_buf;
45316 cp_lexer_consume_token (parser->lexer);
45317 if (!flag_openmp) /* flag_openmp_simd */
45318 return cp_parser_omp_master (parser, pragma_tok, p_name, mask,
45319 cclauses, if_p);
45320 block = begin_omp_parallel ();
45321 save = cp_parser_begin_omp_structured_block (parser);
45322 tree ret = cp_parser_omp_master (parser, pragma_tok, p_name, mask,
45323 cclauses, if_p);
45324 cp_parser_end_omp_structured_block (parser, save);
45325 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
45326 block);
45327 if (ret == NULL_TREE)
45328 return ret;
45329 /* master doesn't have any clauses and during gimplification
45330 isn't represented by a gimplification omp context, so for
45331 #pragma omp parallel master don't set OMP_PARALLEL_COMBINED,
45332 so that
45333 #pragma omp parallel master
45334 #pragma omp taskloop simd lastprivate (x)
45335 isn't confused with
45336 #pragma omp parallel master taskloop simd lastprivate (x) */
45337 if (OMP_MASTER_COMBINED (ret))
45338 OMP_PARALLEL_COMBINED (stmt) = 1;
45339 return stmt;
45341 else if (strcmp (p, "loop") == 0)
45343 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
45344 if (cclauses == NULL)
45345 cclauses = cclauses_buf;
45347 cp_lexer_consume_token (parser->lexer);
45348 if (!flag_openmp) /* flag_openmp_simd */
45349 return cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
45350 cclauses, if_p);
45351 block = begin_omp_parallel ();
45352 save = cp_parser_begin_omp_structured_block (parser);
45353 tree ret = cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
45354 cclauses, if_p);
45355 cp_parser_end_omp_structured_block (parser, save);
45356 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
45357 block);
45358 if (ret == NULL_TREE)
45359 return ret;
45360 OMP_PARALLEL_COMBINED (stmt) = 1;
45361 return stmt;
45363 else if (!flag_openmp) /* flag_openmp_simd */
45365 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45366 return NULL_TREE;
45368 else if (cclauses == NULL && strcmp (p, "sections") == 0)
45370 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
45371 cclauses = cclauses_buf;
45373 cp_lexer_consume_token (parser->lexer);
45374 block = begin_omp_parallel ();
45375 save = cp_parser_begin_omp_structured_block (parser);
45376 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
45377 cp_parser_end_omp_structured_block (parser, save);
45378 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
45379 block);
45380 OMP_PARALLEL_COMBINED (stmt) = 1;
45381 return stmt;
45384 else if (!flag_openmp) /* flag_openmp_simd */
45386 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45387 return NULL_TREE;
45390 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
45391 cclauses == NULL);
45392 if (cclauses)
45394 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
45395 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
45398 block = begin_omp_parallel ();
45399 save = cp_parser_begin_omp_structured_block (parser);
45400 parser->omp_attrs_forbidden_p = true;
45401 cp_parser_statement (parser, NULL_TREE, false, if_p);
45402 cp_parser_end_omp_structured_block (parser, save);
45403 stmt = finish_omp_parallel (clauses, block);
45404 return stmt;
45407 /* OpenMP 2.5:
45408 # pragma omp single single-clause[optseq] new-line
45409 structured-block */
45411 #define OMP_SINGLE_CLAUSE_MASK \
45412 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
45413 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
45414 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
45415 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
45416 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
45418 static tree
45419 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
45421 tree stmt = make_node (OMP_SINGLE);
45422 TREE_TYPE (stmt) = void_type_node;
45423 SET_EXPR_LOCATION (stmt, pragma_tok->location);
45425 OMP_SINGLE_CLAUSES (stmt)
45426 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
45427 "#pragma omp single", pragma_tok);
45428 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
45430 return add_stmt (stmt);
45433 /* OpenMP 5.1:
45434 # pragma omp scope scope-clause[optseq] new-line
45435 structured-block */
45437 #define OMP_SCOPE_CLAUSE_MASK \
45438 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
45439 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
45440 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
45441 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
45442 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
45444 static tree
45445 cp_parser_omp_scope (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
45447 tree stmt = make_node (OMP_SCOPE);
45448 TREE_TYPE (stmt) = void_type_node;
45449 SET_EXPR_LOCATION (stmt, pragma_tok->location);
45451 OMP_SCOPE_CLAUSES (stmt)
45452 = cp_parser_omp_all_clauses (parser, OMP_SCOPE_CLAUSE_MASK,
45453 "#pragma omp scope", pragma_tok);
45454 OMP_SCOPE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
45456 return add_stmt (stmt);
45459 /* OpenMP 3.0:
45460 # pragma omp task task-clause[optseq] new-line
45461 structured-block */
45463 #define OMP_TASK_CLAUSE_MASK \
45464 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
45465 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
45466 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
45467 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
45468 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
45469 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
45470 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
45471 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
45472 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
45473 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
45474 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
45475 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION) \
45476 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DETACH) \
45477 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_AFFINITY))
45479 static tree
45480 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
45482 tree clauses, block;
45483 unsigned int save;
45485 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
45486 "#pragma omp task", pragma_tok);
45487 block = begin_omp_task ();
45488 save = cp_parser_begin_omp_structured_block (parser);
45489 parser->omp_attrs_forbidden_p = true;
45490 cp_parser_statement (parser, NULL_TREE, false, if_p);
45491 cp_parser_end_omp_structured_block (parser, save);
45492 return finish_omp_task (clauses, block);
45495 /* OpenMP 3.0:
45496 # pragma omp taskwait new-line
45498 OpenMP 5.0:
45499 # pragma omp taskwait taskwait-clause[opt] new-line */
45501 #define OMP_TASKWAIT_CLAUSE_MASK \
45502 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
45503 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
45505 static void
45506 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
45508 tree clauses
45509 = cp_parser_omp_all_clauses (parser, OMP_TASKWAIT_CLAUSE_MASK,
45510 "#pragma omp taskwait", pragma_tok);
45512 if (clauses)
45514 tree stmt = make_node (OMP_TASK);
45515 TREE_TYPE (stmt) = void_node;
45516 OMP_TASK_CLAUSES (stmt) = clauses;
45517 OMP_TASK_BODY (stmt) = NULL_TREE;
45518 SET_EXPR_LOCATION (stmt, pragma_tok->location);
45519 add_stmt (stmt);
45521 else
45522 finish_omp_taskwait ();
45525 /* OpenMP 3.1:
45526 # pragma omp taskyield new-line */
45528 static void
45529 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
45531 cp_parser_require_pragma_eol (parser, pragma_tok);
45532 finish_omp_taskyield ();
45535 /* OpenMP 4.0:
45536 # pragma omp taskgroup new-line
45537 structured-block
45539 OpenMP 5.0:
45540 # pragma omp taskgroup taskgroup-clause[optseq] new-line */
45542 #define OMP_TASKGROUP_CLAUSE_MASK \
45543 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
45544 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
45546 static tree
45547 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
45549 tree clauses
45550 = cp_parser_omp_all_clauses (parser, OMP_TASKGROUP_CLAUSE_MASK,
45551 "#pragma omp taskgroup", pragma_tok);
45552 return c_finish_omp_taskgroup (input_location,
45553 cp_parser_omp_structured_block (parser,
45554 if_p),
45555 clauses);
45559 /* OpenMP 2.5:
45560 # pragma omp threadprivate (variable-list) */
45562 static void
45563 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
45565 tree vars;
45567 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
45568 cp_parser_require_pragma_eol (parser, pragma_tok);
45570 finish_omp_threadprivate (vars);
45573 /* OpenMP 4.0:
45574 # pragma omp cancel cancel-clause[optseq] new-line */
45576 #define OMP_CANCEL_CLAUSE_MASK \
45577 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
45578 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
45579 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
45580 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
45581 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
45583 static void
45584 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
45586 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
45587 "#pragma omp cancel", pragma_tok);
45588 finish_omp_cancel (clauses);
45591 /* OpenMP 4.0:
45592 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
45594 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
45595 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
45596 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
45597 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
45598 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
45600 static bool
45601 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
45602 enum pragma_context context)
45604 tree clauses;
45605 bool point_seen = false;
45607 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45609 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
45610 const char *p = IDENTIFIER_POINTER (id);
45612 if (strcmp (p, "point") == 0)
45614 cp_lexer_consume_token (parser->lexer);
45615 point_seen = true;
45618 if (!point_seen)
45620 cp_parser_error (parser, "expected %<point%>");
45621 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45622 return false;
45625 if (context != pragma_compound)
45627 if (context == pragma_stmt)
45628 error_at (pragma_tok->location,
45629 "%<#pragma %s%> may only be used in compound statements",
45630 "omp cancellation point");
45631 else
45632 cp_parser_error (parser, "expected declaration specifiers");
45633 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45634 return true;
45637 clauses = cp_parser_omp_all_clauses (parser,
45638 OMP_CANCELLATION_POINT_CLAUSE_MASK,
45639 "#pragma omp cancellation point",
45640 pragma_tok);
45641 finish_omp_cancellation_point (clauses);
45642 return true;
45645 /* OpenMP 4.0:
45646 #pragma omp distribute distribute-clause[optseq] new-line
45647 for-loop */
45649 #define OMP_DISTRIBUTE_CLAUSE_MASK \
45650 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
45651 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
45652 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
45653 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
45654 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
45655 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
45656 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
45658 static tree
45659 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
45660 char *p_name, omp_clause_mask mask, tree *cclauses,
45661 bool *if_p)
45663 tree clauses, sb, ret;
45664 unsigned int save;
45665 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
45667 strcat (p_name, " distribute");
45668 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
45670 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45672 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
45673 const char *p = IDENTIFIER_POINTER (id);
45674 bool simd = false;
45675 bool parallel = false;
45677 if (strcmp (p, "simd") == 0)
45678 simd = true;
45679 else
45680 parallel = strcmp (p, "parallel") == 0;
45681 if (parallel || simd)
45683 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
45684 if (cclauses == NULL)
45685 cclauses = cclauses_buf;
45686 cp_lexer_consume_token (parser->lexer);
45687 if (!flag_openmp) /* flag_openmp_simd */
45689 if (simd)
45690 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
45691 cclauses, if_p);
45692 else
45693 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
45694 cclauses, if_p);
45696 sb = begin_omp_structured_block ();
45697 save = cp_parser_begin_omp_structured_block (parser);
45698 if (simd)
45699 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
45700 cclauses, if_p);
45701 else
45702 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
45703 cclauses, if_p);
45704 cp_parser_end_omp_structured_block (parser, save);
45705 tree body = finish_omp_structured_block (sb);
45706 if (ret == NULL)
45707 return ret;
45708 ret = make_node (OMP_DISTRIBUTE);
45709 TREE_TYPE (ret) = void_type_node;
45710 OMP_FOR_BODY (ret) = body;
45711 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
45712 SET_EXPR_LOCATION (ret, loc);
45713 add_stmt (ret);
45714 return ret;
45717 if (!flag_openmp) /* flag_openmp_simd */
45719 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45720 return NULL_TREE;
45723 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
45724 cclauses == NULL);
45725 if (cclauses)
45727 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
45728 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
45731 keep_next_level (true);
45732 sb = begin_omp_structured_block ();
45733 save = cp_parser_begin_omp_structured_block (parser);
45735 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
45737 cp_parser_end_omp_structured_block (parser, save);
45738 add_stmt (finish_omp_structured_block (sb));
45740 return ret;
45743 /* OpenMP 4.0:
45744 # pragma omp teams teams-clause[optseq] new-line
45745 structured-block */
45747 #define OMP_TEAMS_CLAUSE_MASK \
45748 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
45749 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
45750 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
45751 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
45752 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
45753 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
45754 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
45755 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
45757 static tree
45758 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
45759 char *p_name, omp_clause_mask mask, tree *cclauses,
45760 bool *if_p)
45762 tree clauses, sb, ret;
45763 unsigned int save;
45764 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
45766 strcat (p_name, " teams");
45767 mask |= OMP_TEAMS_CLAUSE_MASK;
45769 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45771 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
45772 const char *p = IDENTIFIER_POINTER (id);
45773 if (strcmp (p, "distribute") == 0)
45775 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
45776 if (cclauses == NULL)
45777 cclauses = cclauses_buf;
45779 cp_lexer_consume_token (parser->lexer);
45780 if (!flag_openmp) /* flag_openmp_simd */
45781 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
45782 cclauses, if_p);
45783 keep_next_level (true);
45784 sb = begin_omp_structured_block ();
45785 save = cp_parser_begin_omp_structured_block (parser);
45786 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
45787 cclauses, if_p);
45788 cp_parser_end_omp_structured_block (parser, save);
45789 tree body = finish_omp_structured_block (sb);
45790 if (ret == NULL)
45791 return ret;
45792 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
45793 ret = make_node (OMP_TEAMS);
45794 TREE_TYPE (ret) = void_type_node;
45795 OMP_TEAMS_CLAUSES (ret) = clauses;
45796 OMP_TEAMS_BODY (ret) = body;
45797 OMP_TEAMS_COMBINED (ret) = 1;
45798 SET_EXPR_LOCATION (ret, loc);
45799 return add_stmt (ret);
45801 else if (strcmp (p, "loop") == 0)
45803 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
45804 if (cclauses == NULL)
45805 cclauses = cclauses_buf;
45807 cp_lexer_consume_token (parser->lexer);
45808 if (!flag_openmp) /* flag_openmp_simd */
45809 return cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
45810 cclauses, if_p);
45811 keep_next_level (true);
45812 sb = begin_omp_structured_block ();
45813 save = cp_parser_begin_omp_structured_block (parser);
45814 ret = cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
45815 cclauses, if_p);
45816 cp_parser_end_omp_structured_block (parser, save);
45817 tree body = finish_omp_structured_block (sb);
45818 if (ret == NULL)
45819 return ret;
45820 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
45821 ret = make_node (OMP_TEAMS);
45822 TREE_TYPE (ret) = void_type_node;
45823 OMP_TEAMS_CLAUSES (ret) = clauses;
45824 OMP_TEAMS_BODY (ret) = body;
45825 OMP_TEAMS_COMBINED (ret) = 1;
45826 SET_EXPR_LOCATION (ret, loc);
45827 return add_stmt (ret);
45830 if (!flag_openmp) /* flag_openmp_simd */
45832 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45833 return NULL_TREE;
45836 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
45837 cclauses == NULL);
45838 if (cclauses)
45840 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
45841 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
45844 tree stmt = make_node (OMP_TEAMS);
45845 TREE_TYPE (stmt) = void_type_node;
45846 OMP_TEAMS_CLAUSES (stmt) = clauses;
45847 keep_next_level (true);
45848 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
45849 SET_EXPR_LOCATION (stmt, loc);
45851 return add_stmt (stmt);
45854 /* OpenMP 4.0:
45855 # pragma omp target data target-data-clause[optseq] new-line
45856 structured-block */
45858 #define OMP_TARGET_DATA_CLAUSE_MASK \
45859 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
45860 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
45861 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
45862 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR) \
45863 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR))
45865 static tree
45866 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
45868 if (flag_openmp)
45869 omp_requires_mask
45870 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
45872 tree clauses
45873 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
45874 "#pragma omp target data", pragma_tok);
45875 c_omp_adjust_map_clauses (clauses, false);
45876 int map_seen = 0;
45877 for (tree *pc = &clauses; *pc;)
45879 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
45880 switch (OMP_CLAUSE_MAP_KIND (*pc))
45882 case GOMP_MAP_TO:
45883 case GOMP_MAP_ALWAYS_TO:
45884 case GOMP_MAP_PRESENT_TO:
45885 case GOMP_MAP_ALWAYS_PRESENT_TO:
45886 case GOMP_MAP_FROM:
45887 case GOMP_MAP_ALWAYS_FROM:
45888 case GOMP_MAP_PRESENT_FROM:
45889 case GOMP_MAP_ALWAYS_PRESENT_FROM:
45890 case GOMP_MAP_TOFROM:
45891 case GOMP_MAP_ALWAYS_TOFROM:
45892 case GOMP_MAP_PRESENT_TOFROM:
45893 case GOMP_MAP_ALWAYS_PRESENT_TOFROM:
45894 case GOMP_MAP_ALLOC:
45895 case GOMP_MAP_PRESENT_ALLOC:
45896 map_seen = 3;
45897 break;
45898 case GOMP_MAP_FIRSTPRIVATE_POINTER:
45899 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
45900 case GOMP_MAP_ALWAYS_POINTER:
45901 case GOMP_MAP_ATTACH_DETACH:
45902 break;
45903 default:
45904 map_seen |= 1;
45905 error_at (OMP_CLAUSE_LOCATION (*pc),
45906 "%<#pragma omp target data%> with map-type other "
45907 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
45908 "on %<map%> clause");
45909 *pc = OMP_CLAUSE_CHAIN (*pc);
45910 continue;
45912 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_PTR
45913 || OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_ADDR)
45914 map_seen = 3;
45915 pc = &OMP_CLAUSE_CHAIN (*pc);
45918 if (map_seen != 3)
45920 if (map_seen == 0)
45921 error_at (pragma_tok->location,
45922 "%<#pragma omp target data%> must contain at least "
45923 "one %<map%>, %<use_device_ptr%> or %<use_device_addr%> "
45924 "clause");
45925 return NULL_TREE;
45928 tree stmt = make_node (OMP_TARGET_DATA);
45929 TREE_TYPE (stmt) = void_type_node;
45930 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
45932 keep_next_level (true);
45933 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
45935 SET_EXPR_LOCATION (stmt, pragma_tok->location);
45936 return add_stmt (stmt);
45939 /* OpenMP 4.5:
45940 # pragma omp target enter data target-enter-data-clause[optseq] new-line
45941 structured-block */
45943 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
45944 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
45945 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
45946 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
45947 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
45948 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
45950 static bool
45951 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
45952 enum pragma_context context)
45954 bool data_seen = false;
45955 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45957 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
45958 const char *p = IDENTIFIER_POINTER (id);
45960 if (strcmp (p, "data") == 0)
45962 cp_lexer_consume_token (parser->lexer);
45963 data_seen = true;
45966 if (!data_seen)
45968 cp_parser_error (parser, "expected %<data%>");
45969 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45970 return false;
45973 if (context == pragma_stmt)
45975 error_at (pragma_tok->location,
45976 "%<#pragma %s%> may only be used in compound statements",
45977 "omp target enter data");
45978 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45979 return true;
45982 if (flag_openmp)
45983 omp_requires_mask
45984 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
45986 tree clauses
45987 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
45988 "#pragma omp target enter data", pragma_tok);
45989 c_omp_adjust_map_clauses (clauses, false);
45990 int map_seen = 0;
45991 for (tree *pc = &clauses; *pc;)
45993 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
45994 switch (OMP_CLAUSE_MAP_KIND (*pc))
45996 case GOMP_MAP_TO:
45997 case GOMP_MAP_ALWAYS_TO:
45998 case GOMP_MAP_PRESENT_TO:
45999 case GOMP_MAP_ALWAYS_PRESENT_TO:
46000 case GOMP_MAP_ALLOC:
46001 case GOMP_MAP_PRESENT_ALLOC:
46002 map_seen = 3;
46003 break;
46004 case GOMP_MAP_TOFROM:
46005 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_TO);
46006 map_seen = 3;
46007 break;
46008 case GOMP_MAP_ALWAYS_TOFROM:
46009 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_ALWAYS_TO);
46010 map_seen = 3;
46011 break;
46012 case GOMP_MAP_PRESENT_TOFROM:
46013 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_PRESENT_TO);
46014 map_seen = 3;
46015 break;
46016 case GOMP_MAP_ALWAYS_PRESENT_TOFROM:
46017 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_ALWAYS_PRESENT_TO);
46018 map_seen = 3;
46019 break;
46020 case GOMP_MAP_FIRSTPRIVATE_POINTER:
46021 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
46022 case GOMP_MAP_ALWAYS_POINTER:
46023 case GOMP_MAP_ATTACH_DETACH:
46024 break;
46025 default:
46026 map_seen |= 1;
46027 error_at (OMP_CLAUSE_LOCATION (*pc),
46028 "%<#pragma omp target enter data%> with map-type other "
46029 "than %<to%>, %<tofrom%> or %<alloc%> on %<map%> clause");
46030 *pc = OMP_CLAUSE_CHAIN (*pc);
46031 continue;
46033 pc = &OMP_CLAUSE_CHAIN (*pc);
46036 if (map_seen != 3)
46038 if (map_seen == 0)
46039 error_at (pragma_tok->location,
46040 "%<#pragma omp target enter data%> must contain at least "
46041 "one %<map%> clause");
46042 return true;
46045 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
46046 TREE_TYPE (stmt) = void_type_node;
46047 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
46048 SET_EXPR_LOCATION (stmt, pragma_tok->location);
46049 add_stmt (stmt);
46050 return true;
46053 /* OpenMP 4.5:
46054 # pragma omp target exit data target-enter-data-clause[optseq] new-line
46055 structured-block */
46057 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
46058 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
46059 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
46060 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
46061 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
46062 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
46064 static bool
46065 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
46066 enum pragma_context context)
46068 bool data_seen = false;
46069 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46071 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46072 const char *p = IDENTIFIER_POINTER (id);
46074 if (strcmp (p, "data") == 0)
46076 cp_lexer_consume_token (parser->lexer);
46077 data_seen = true;
46080 if (!data_seen)
46082 cp_parser_error (parser, "expected %<data%>");
46083 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46084 return false;
46087 if (context == pragma_stmt)
46089 error_at (pragma_tok->location,
46090 "%<#pragma %s%> may only be used in compound statements",
46091 "omp target exit data");
46092 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46093 return true;
46096 if (flag_openmp)
46097 omp_requires_mask
46098 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
46100 tree clauses
46101 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
46102 "#pragma omp target exit data", pragma_tok);
46103 c_omp_adjust_map_clauses (clauses, false);
46104 int map_seen = 0;
46105 for (tree *pc = &clauses; *pc;)
46107 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
46108 switch (OMP_CLAUSE_MAP_KIND (*pc))
46110 case GOMP_MAP_FROM:
46111 case GOMP_MAP_ALWAYS_FROM:
46112 case GOMP_MAP_PRESENT_FROM:
46113 case GOMP_MAP_ALWAYS_PRESENT_FROM:
46114 case GOMP_MAP_RELEASE:
46115 case GOMP_MAP_DELETE:
46116 map_seen = 3;
46117 break;
46118 case GOMP_MAP_TOFROM:
46119 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_FROM);
46120 map_seen = 3;
46121 break;
46122 case GOMP_MAP_ALWAYS_TOFROM:
46123 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_ALWAYS_FROM);
46124 map_seen = 3;
46125 break;
46126 case GOMP_MAP_PRESENT_TOFROM:
46127 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_PRESENT_FROM);
46128 map_seen = 3;
46129 break;
46130 case GOMP_MAP_ALWAYS_PRESENT_TOFROM:
46131 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_ALWAYS_PRESENT_FROM);
46132 map_seen = 3;
46133 break;
46134 case GOMP_MAP_FIRSTPRIVATE_POINTER:
46135 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
46136 case GOMP_MAP_ALWAYS_POINTER:
46137 case GOMP_MAP_ATTACH_DETACH:
46138 break;
46139 default:
46140 map_seen |= 1;
46141 error_at (OMP_CLAUSE_LOCATION (*pc),
46142 "%<#pragma omp target exit data%> with map-type other "
46143 "than %<from%>, %<tofrom%>, %<release%> or %<delete%> "
46144 "on %<map%> clause");
46145 *pc = OMP_CLAUSE_CHAIN (*pc);
46146 continue;
46148 pc = &OMP_CLAUSE_CHAIN (*pc);
46151 if (map_seen != 3)
46153 if (map_seen == 0)
46154 error_at (pragma_tok->location,
46155 "%<#pragma omp target exit data%> must contain at least "
46156 "one %<map%> clause");
46157 return true;
46160 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
46161 TREE_TYPE (stmt) = void_type_node;
46162 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
46163 SET_EXPR_LOCATION (stmt, pragma_tok->location);
46164 add_stmt (stmt);
46165 return true;
46168 /* OpenMP 4.0:
46169 # pragma omp target update target-update-clause[optseq] new-line */
46171 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
46172 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
46173 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
46174 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
46175 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
46176 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
46177 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
46179 static bool
46180 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
46181 enum pragma_context context)
46183 if (context == pragma_stmt)
46185 error_at (pragma_tok->location,
46186 "%<#pragma %s%> may only be used in compound statements",
46187 "omp target update");
46188 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46189 return true;
46192 tree clauses
46193 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
46194 "#pragma omp target update", pragma_tok);
46195 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
46196 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
46198 error_at (pragma_tok->location,
46199 "%<#pragma omp target update%> must contain at least one "
46200 "%<from%> or %<to%> clauses");
46201 return true;
46204 if (flag_openmp)
46205 omp_requires_mask
46206 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
46208 tree stmt = make_node (OMP_TARGET_UPDATE);
46209 TREE_TYPE (stmt) = void_type_node;
46210 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
46211 SET_EXPR_LOCATION (stmt, pragma_tok->location);
46212 add_stmt (stmt);
46213 return true;
46216 /* OpenMP 4.0:
46217 # pragma omp target target-clause[optseq] new-line
46218 structured-block */
46220 #define OMP_TARGET_CLAUSE_MASK \
46221 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
46222 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
46223 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
46224 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
46225 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
46226 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
46227 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
46228 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
46229 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
46230 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION) \
46231 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
46232 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR)\
46233 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HAS_DEVICE_ADDR))
46235 static bool
46236 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
46237 enum pragma_context context, bool *if_p)
46239 if (flag_openmp)
46240 omp_requires_mask
46241 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
46243 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46245 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46246 const char *p = IDENTIFIER_POINTER (id);
46247 enum tree_code ccode = ERROR_MARK;
46249 if (strcmp (p, "teams") == 0)
46250 ccode = OMP_TEAMS;
46251 else if (strcmp (p, "parallel") == 0)
46252 ccode = OMP_PARALLEL;
46253 else if (strcmp (p, "simd") == 0)
46254 ccode = OMP_SIMD;
46255 if (ccode != ERROR_MARK)
46257 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
46258 char p_name[sizeof ("#pragma omp target teams distribute "
46259 "parallel for simd")];
46261 cp_lexer_consume_token (parser->lexer);
46262 strcpy (p_name, "#pragma omp target");
46263 if (!flag_openmp) /* flag_openmp_simd */
46265 tree stmt;
46266 switch (ccode)
46268 case OMP_TEAMS:
46269 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
46270 OMP_TARGET_CLAUSE_MASK,
46271 cclauses, if_p);
46272 break;
46273 case OMP_PARALLEL:
46274 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
46275 OMP_TARGET_CLAUSE_MASK,
46276 cclauses, if_p);
46277 break;
46278 case OMP_SIMD:
46279 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
46280 OMP_TARGET_CLAUSE_MASK,
46281 cclauses, if_p);
46282 break;
46283 default:
46284 gcc_unreachable ();
46286 return stmt != NULL_TREE;
46288 keep_next_level (true);
46289 tree sb = begin_omp_structured_block (), ret;
46290 unsigned save = cp_parser_begin_omp_structured_block (parser);
46291 switch (ccode)
46293 case OMP_TEAMS:
46294 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
46295 OMP_TARGET_CLAUSE_MASK, cclauses,
46296 if_p);
46297 break;
46298 case OMP_PARALLEL:
46299 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
46300 OMP_TARGET_CLAUSE_MASK, cclauses,
46301 if_p);
46302 break;
46303 case OMP_SIMD:
46304 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
46305 OMP_TARGET_CLAUSE_MASK, cclauses,
46306 if_p);
46307 break;
46308 default:
46309 gcc_unreachable ();
46311 cp_parser_end_omp_structured_block (parser, save);
46312 tree body = finish_omp_structured_block (sb);
46313 if (ret == NULL_TREE)
46314 return false;
46315 if (ccode == OMP_TEAMS && !processing_template_decl)
46316 /* For combined target teams, ensure the num_teams and
46317 thread_limit clause expressions are evaluated on the host,
46318 before entering the target construct. */
46319 for (tree c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
46320 c; c = OMP_CLAUSE_CHAIN (c))
46321 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
46322 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
46323 for (int i = 0;
46324 i <= (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS); ++i)
46325 if (OMP_CLAUSE_OPERAND (c, i)
46326 && TREE_CODE (OMP_CLAUSE_OPERAND (c, i)) != INTEGER_CST)
46328 tree expr = OMP_CLAUSE_OPERAND (c, i);
46329 expr = force_target_expr (TREE_TYPE (expr), expr,
46330 tf_none);
46331 if (expr == error_mark_node)
46332 continue;
46333 tree tmp = TARGET_EXPR_SLOT (expr);
46334 add_stmt (expr);
46335 OMP_CLAUSE_OPERAND (c, i) = expr;
46336 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
46337 OMP_CLAUSE_FIRSTPRIVATE);
46338 OMP_CLAUSE_DECL (tc) = tmp;
46339 OMP_CLAUSE_CHAIN (tc)
46340 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
46341 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
46343 c_omp_adjust_map_clauses (cclauses[C_OMP_CLAUSE_SPLIT_TARGET], true);
46344 finish_omp_target (pragma_tok->location,
46345 cclauses[C_OMP_CLAUSE_SPLIT_TARGET], body, true);
46346 return true;
46348 else if (!flag_openmp) /* flag_openmp_simd */
46350 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46351 return false;
46353 else if (strcmp (p, "data") == 0)
46355 cp_lexer_consume_token (parser->lexer);
46356 cp_parser_omp_target_data (parser, pragma_tok, if_p);
46357 return true;
46359 else if (strcmp (p, "enter") == 0)
46361 cp_lexer_consume_token (parser->lexer);
46362 return cp_parser_omp_target_enter_data (parser, pragma_tok, context);
46364 else if (strcmp (p, "exit") == 0)
46366 cp_lexer_consume_token (parser->lexer);
46367 return cp_parser_omp_target_exit_data (parser, pragma_tok, context);
46369 else if (strcmp (p, "update") == 0)
46371 cp_lexer_consume_token (parser->lexer);
46372 return cp_parser_omp_target_update (parser, pragma_tok, context);
46375 if (!flag_openmp) /* flag_openmp_simd */
46377 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46378 return false;
46381 tree clauses = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
46382 "#pragma omp target", pragma_tok,
46383 false);
46384 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
46385 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
46387 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
46388 OMP_CLAUSE_DECL (nc) = OMP_CLAUSE_DECL (c);
46389 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_ALWAYS_TOFROM);
46390 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (c);
46391 OMP_CLAUSE_CHAIN (c) = nc;
46393 clauses = finish_omp_clauses (clauses, C_ORT_OMP_TARGET);
46395 c_omp_adjust_map_clauses (clauses, true);
46396 keep_next_level (true);
46397 tree body = cp_parser_omp_structured_block (parser, if_p);
46399 finish_omp_target (pragma_tok->location, clauses, body, false);
46400 return true;
46403 /* OpenACC 2.0:
46404 # pragma acc cache (variable-list) new-line
46407 static tree
46408 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
46410 /* Don't create location wrapper nodes within 'OMP_CLAUSE__CACHE_'
46411 clauses. */
46412 auto_suppress_location_wrappers sentinel;
46414 tree stmt, clauses;
46416 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
46417 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
46419 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
46421 stmt = make_node (OACC_CACHE);
46422 TREE_TYPE (stmt) = void_type_node;
46423 OACC_CACHE_CLAUSES (stmt) = clauses;
46424 SET_EXPR_LOCATION (stmt, pragma_tok->location);
46425 add_stmt (stmt);
46427 return stmt;
46430 /* OpenACC 2.0:
46431 # pragma acc data oacc-data-clause[optseq] new-line
46432 structured-block */
46434 #define OACC_DATA_CLAUSE_MASK \
46435 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
46436 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
46437 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
46438 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
46439 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
46440 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
46441 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DETACH) \
46442 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
46443 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
46444 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
46445 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
46447 static tree
46448 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
46450 tree stmt, clauses, block;
46451 unsigned int save;
46453 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
46454 "#pragma acc data", pragma_tok);
46456 block = begin_omp_parallel ();
46457 save = cp_parser_begin_omp_structured_block (parser);
46458 cp_parser_statement (parser, NULL_TREE, false, if_p);
46459 cp_parser_end_omp_structured_block (parser, save);
46460 stmt = finish_oacc_data (clauses, block);
46461 return stmt;
46464 /* OpenACC 2.0:
46465 # pragma acc host_data <clauses> new-line
46466 structured-block */
46468 #define OACC_HOST_DATA_CLAUSE_MASK \
46469 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) \
46470 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
46471 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) )
46473 static tree
46474 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
46476 tree stmt, clauses, block;
46477 unsigned int save;
46479 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
46480 "#pragma acc host_data", pragma_tok,
46481 false);
46482 if (!omp_find_clause (clauses, OMP_CLAUSE_USE_DEVICE_PTR))
46484 error_at (pragma_tok->location,
46485 "%<host_data%> construct requires %<use_device%> clause");
46486 return error_mark_node;
46488 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
46489 block = begin_omp_parallel ();
46490 save = cp_parser_begin_omp_structured_block (parser);
46491 cp_parser_statement (parser, NULL_TREE, false, if_p);
46492 cp_parser_end_omp_structured_block (parser, save);
46493 stmt = finish_oacc_host_data (clauses, block);
46494 return stmt;
46497 /* OpenACC 2.0:
46498 # pragma acc declare oacc-data-clause[optseq] new-line
46501 #define OACC_DECLARE_CLAUSE_MASK \
46502 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
46503 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
46504 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
46505 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
46506 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
46507 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
46508 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
46509 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
46511 static tree
46512 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
46514 tree clauses, stmt;
46515 bool error = false;
46516 bool found_in_scope = global_bindings_p ();
46518 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
46519 "#pragma acc declare", pragma_tok, true);
46522 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
46524 error_at (pragma_tok->location,
46525 "no valid clauses specified in %<#pragma acc declare%>");
46526 return NULL_TREE;
46529 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
46531 location_t loc = OMP_CLAUSE_LOCATION (t);
46532 tree decl = OMP_CLAUSE_DECL (t);
46533 if (!DECL_P (decl))
46535 error_at (loc, "array section in %<#pragma acc declare%>");
46536 error = true;
46537 continue;
46539 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
46540 switch (OMP_CLAUSE_MAP_KIND (t))
46542 case GOMP_MAP_FIRSTPRIVATE_POINTER:
46543 case GOMP_MAP_ALLOC:
46544 case GOMP_MAP_TO:
46545 case GOMP_MAP_FORCE_DEVICEPTR:
46546 case GOMP_MAP_DEVICE_RESIDENT:
46547 break;
46549 case GOMP_MAP_LINK:
46550 if (!global_bindings_p ()
46551 && (TREE_STATIC (decl)
46552 || !DECL_EXTERNAL (decl)))
46554 error_at (loc,
46555 "%qD must be a global variable in "
46556 "%<#pragma acc declare link%>",
46557 decl);
46558 error = true;
46559 continue;
46561 break;
46563 default:
46564 if (global_bindings_p ())
46566 error_at (loc, "invalid OpenACC clause at file scope");
46567 error = true;
46568 continue;
46570 if (DECL_EXTERNAL (decl))
46572 error_at (loc,
46573 "invalid use of %<extern%> variable %qD "
46574 "in %<#pragma acc declare%>", decl);
46575 error = true;
46576 continue;
46578 else if (TREE_PUBLIC (decl))
46580 error_at (loc,
46581 "invalid use of %<global%> variable %qD "
46582 "in %<#pragma acc declare%>", decl);
46583 error = true;
46584 continue;
46586 break;
46589 if (!found_in_scope)
46590 /* This seems to ignore the existence of cleanup scopes?
46591 What is the meaning for local extern decls? The local
46592 extern is in this scope, but it is referring to a decl that
46593 is namespace scope. */
46594 for (tree d = current_binding_level->names; d; d = TREE_CHAIN (d))
46595 if (d == decl)
46597 found_in_scope = true;
46598 break;
46600 if (!found_in_scope)
46602 error_at (loc,
46603 "%qD must be a variable declared in the same scope as "
46604 "%<#pragma acc declare%>", decl);
46605 error = true;
46606 continue;
46609 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
46610 || lookup_attribute ("omp declare target link",
46611 DECL_ATTRIBUTES (decl)))
46613 error_at (loc, "variable %qD used more than once with "
46614 "%<#pragma acc declare%>", decl);
46615 error = true;
46616 continue;
46619 if (!error)
46621 tree id;
46623 if (DECL_LOCAL_DECL_P (decl))
46624 /* We need to mark the aliased decl, as that is the entity
46625 that is being referred to. This won't work for
46626 dependent variables, but it didn't work for them before
46627 DECL_LOCAL_DECL_P was a thing either. But then
46628 dependent local extern variable decls are as rare as
46629 hen's teeth. */
46630 if (auto alias = DECL_LOCAL_DECL_ALIAS (decl))
46631 if (alias != error_mark_node)
46632 decl = alias;
46634 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
46635 id = get_identifier ("omp declare target link");
46636 else
46637 id = get_identifier ("omp declare target");
46639 DECL_ATTRIBUTES (decl)
46640 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
46641 if (current_binding_level->kind == sk_namespace)
46643 symtab_node *node = symtab_node::get (decl);
46644 if (node != NULL)
46646 node->offloadable = 1;
46647 if (ENABLE_OFFLOADING)
46649 g->have_offload = true;
46650 if (is_a <varpool_node *> (node))
46651 vec_safe_push (offload_vars, decl);
46658 if (error || current_binding_level->kind == sk_namespace)
46659 return NULL_TREE;
46661 stmt = make_node (OACC_DECLARE);
46662 TREE_TYPE (stmt) = void_type_node;
46663 OACC_DECLARE_CLAUSES (stmt) = clauses;
46664 SET_EXPR_LOCATION (stmt, pragma_tok->location);
46666 add_stmt (stmt);
46668 return NULL_TREE;
46671 /* OpenACC 2.0:
46672 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
46676 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
46678 LOC is the location of the #pragma token.
46681 #define OACC_ENTER_DATA_CLAUSE_MASK \
46682 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
46683 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
46684 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
46685 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
46686 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
46687 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
46689 #define OACC_EXIT_DATA_CLAUSE_MASK \
46690 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
46691 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
46692 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
46693 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
46694 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DETACH) \
46695 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
46696 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
46698 static tree
46699 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
46700 bool enter)
46702 location_t loc = pragma_tok->location;
46703 tree stmt, clauses;
46704 const char *p = "";
46706 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46707 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
46709 if (strcmp (p, "data") != 0)
46711 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
46712 enter ? "enter" : "exit");
46713 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46714 return NULL_TREE;
46717 cp_lexer_consume_token (parser->lexer);
46719 if (enter)
46720 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
46721 "#pragma acc enter data", pragma_tok);
46722 else
46723 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
46724 "#pragma acc exit data", pragma_tok);
46726 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
46728 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
46729 enter ? "enter" : "exit");
46730 return NULL_TREE;
46733 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
46734 TREE_TYPE (stmt) = void_type_node;
46735 OMP_STANDALONE_CLAUSES (stmt) = clauses;
46736 SET_EXPR_LOCATION (stmt, loc);
46737 add_stmt (stmt);
46738 return stmt;
46741 /* OpenACC 2.0:
46742 # pragma acc loop oacc-loop-clause[optseq] new-line
46743 structured-block */
46745 #define OACC_LOOP_CLAUSE_MASK \
46746 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
46747 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
46748 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
46749 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
46750 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
46751 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
46752 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
46753 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
46754 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
46755 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
46757 static tree
46758 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
46759 omp_clause_mask mask, tree *cclauses, bool *if_p)
46761 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
46763 strcat (p_name, " loop");
46764 mask |= OACC_LOOP_CLAUSE_MASK;
46766 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
46767 cclauses == NULL);
46768 if (cclauses)
46770 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
46771 if (*cclauses)
46772 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
46773 if (clauses)
46774 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
46777 tree block = begin_omp_structured_block ();
46778 int save = cp_parser_begin_omp_structured_block (parser);
46779 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
46780 cp_parser_end_omp_structured_block (parser, save);
46782 /* Later processing of combined acc loop constructs gets confused
46783 by an extra level of empty nested BIND_EXPRs, so flatten them. */
46784 block = finish_omp_structured_block (block);
46785 if (TREE_CODE (block) == BIND_EXPR
46786 && TREE_CODE (BIND_EXPR_BODY (block)) == BIND_EXPR
46787 && !BIND_EXPR_VARS (block))
46788 block = BIND_EXPR_BODY (block);
46789 add_stmt (block);
46791 return stmt;
46794 /* OpenACC 2.0:
46795 # pragma acc kernels oacc-kernels-clause[optseq] new-line
46796 structured-block
46800 # pragma acc parallel oacc-parallel-clause[optseq] new-line
46801 structured-block
46803 OpenACC 2.6:
46805 # pragma acc serial oacc-serial-clause[optseq] new-line
46808 #define OACC_KERNELS_CLAUSE_MASK \
46809 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
46810 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
46811 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
46812 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
46813 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
46814 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
46815 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
46816 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
46817 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
46818 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
46819 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
46820 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
46821 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
46822 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
46823 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
46825 #define OACC_PARALLEL_CLAUSE_MASK \
46826 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
46827 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
46828 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
46829 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
46830 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
46831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
46832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
46833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
46834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
46835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
46836 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
46837 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
46838 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
46839 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
46840 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
46841 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
46842 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
46843 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
46845 #define OACC_SERIAL_CLAUSE_MASK \
46846 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
46847 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
46848 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
46849 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
46850 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
46851 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
46852 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
46853 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
46854 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
46855 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
46856 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
46857 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
46858 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
46859 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
46860 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
46862 static tree
46863 cp_parser_oacc_compute (cp_parser *parser, cp_token *pragma_tok,
46864 char *p_name, bool *if_p)
46866 omp_clause_mask mask;
46867 enum tree_code code;
46868 switch (cp_parser_pragma_kind (pragma_tok))
46870 case PRAGMA_OACC_KERNELS:
46871 strcat (p_name, " kernels");
46872 mask = OACC_KERNELS_CLAUSE_MASK;
46873 code = OACC_KERNELS;
46874 break;
46875 case PRAGMA_OACC_PARALLEL:
46876 strcat (p_name, " parallel");
46877 mask = OACC_PARALLEL_CLAUSE_MASK;
46878 code = OACC_PARALLEL;
46879 break;
46880 case PRAGMA_OACC_SERIAL:
46881 strcat (p_name, " serial");
46882 mask = OACC_SERIAL_CLAUSE_MASK;
46883 code = OACC_SERIAL;
46884 break;
46885 default:
46886 gcc_unreachable ();
46889 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46891 const char *p
46892 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
46893 if (strcmp (p, "loop") == 0)
46895 cp_lexer_consume_token (parser->lexer);
46896 tree block = begin_omp_parallel ();
46897 tree clauses;
46898 tree stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask,
46899 &clauses, if_p);
46900 protected_set_expr_location (stmt, pragma_tok->location);
46901 return finish_omp_construct (code, block, clauses);
46905 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
46907 tree block = begin_omp_parallel ();
46908 unsigned int save = cp_parser_begin_omp_structured_block (parser);
46909 cp_parser_statement (parser, NULL_TREE, false, if_p);
46910 cp_parser_end_omp_structured_block (parser, save);
46911 return finish_omp_construct (code, block, clauses);
46914 /* OpenACC 2.0:
46915 # pragma acc update oacc-update-clause[optseq] new-line
46918 #define OACC_UPDATE_CLAUSE_MASK \
46919 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
46920 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
46921 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
46922 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
46923 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
46924 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
46926 static tree
46927 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
46929 tree stmt, clauses;
46931 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
46932 "#pragma acc update", pragma_tok);
46934 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
46936 error_at (pragma_tok->location,
46937 "%<#pragma acc update%> must contain at least one "
46938 "%<device%> or %<host%> or %<self%> clause");
46939 return NULL_TREE;
46942 stmt = make_node (OACC_UPDATE);
46943 TREE_TYPE (stmt) = void_type_node;
46944 OACC_UPDATE_CLAUSES (stmt) = clauses;
46945 SET_EXPR_LOCATION (stmt, pragma_tok->location);
46946 add_stmt (stmt);
46947 return stmt;
46950 /* OpenACC 2.0:
46951 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
46953 LOC is the location of the #pragma token.
46956 #define OACC_WAIT_CLAUSE_MASK \
46957 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
46959 static tree
46960 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
46962 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
46963 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
46965 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
46966 list = cp_parser_oacc_wait_list (parser, loc, list);
46968 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
46969 "#pragma acc wait", pragma_tok);
46971 stmt = c_finish_oacc_wait (loc, list, clauses);
46972 stmt = finish_expr_stmt (stmt);
46974 return stmt;
46977 /* OpenMP 4.0:
46978 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
46980 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
46981 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
46982 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
46983 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
46984 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
46985 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
46986 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
46988 static void
46989 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
46990 enum pragma_context context,
46991 bool variant_p)
46993 bool first_p = parser->omp_declare_simd == NULL;
46994 cp_omp_declare_simd_data data;
46995 if (first_p)
46997 data.error_seen = false;
46998 data.fndecl_seen = false;
46999 data.variant_p = variant_p;
47000 data.tokens = vNULL;
47001 data.attribs[0] = NULL;
47002 data.attribs[1] = NULL;
47003 data.loc = UNKNOWN_LOCATION;
47004 /* It is safe to take the address of a local variable; it will only be
47005 used while this scope is live. */
47006 parser->omp_declare_simd = &data;
47008 else if (parser->omp_declare_simd->variant_p != variant_p)
47010 error_at (pragma_tok->location,
47011 "%<#pragma omp declare %s%> followed by "
47012 "%<#pragma omp declare %s%>",
47013 parser->omp_declare_simd->variant_p ? "variant" : "simd",
47014 parser->omp_declare_simd->variant_p ? "simd" : "variant");
47015 parser->omp_declare_simd->error_seen = true;
47018 /* Store away all pragma tokens. */
47019 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
47020 cp_lexer_consume_token (parser->lexer);
47021 cp_parser_require_pragma_eol (parser, pragma_tok);
47022 struct cp_token_cache *cp
47023 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
47024 parser->omp_declare_simd->tokens.safe_push (cp);
47026 if (first_p)
47028 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
47029 cp_parser_pragma (parser, context, NULL);
47030 switch (context)
47032 case pragma_external:
47033 cp_parser_declaration (parser, NULL_TREE);
47034 break;
47035 case pragma_member:
47036 cp_parser_member_declaration (parser);
47037 break;
47038 case pragma_objc_icode:
47039 cp_parser_block_declaration (parser, /*statement_p=*/false);
47040 break;
47041 default:
47042 cp_parser_declaration_statement (parser);
47043 break;
47045 if (parser->omp_declare_simd
47046 && !parser->omp_declare_simd->error_seen
47047 && !parser->omp_declare_simd->fndecl_seen)
47048 error_at (pragma_tok->location,
47049 "%<#pragma omp declare %s%> not immediately followed by "
47050 "function declaration or definition",
47051 parser->omp_declare_simd->variant_p ? "variant" : "simd");
47052 data.tokens.release ();
47053 parser->omp_declare_simd = NULL;
47057 static const char *const omp_construct_selectors[] = {
47058 "simd", "target", "teams", "parallel", "for", NULL };
47059 static const char *const omp_device_selectors[] = {
47060 "kind", "isa", "arch", NULL };
47061 static const char *const omp_implementation_selectors[] = {
47062 "vendor", "extension", "atomic_default_mem_order", "unified_address",
47063 "unified_shared_memory", "dynamic_allocators", "reverse_offload", NULL };
47064 static const char *const omp_user_selectors[] = {
47065 "condition", NULL };
47067 /* OpenMP 5.0:
47069 trait-selector:
47070 trait-selector-name[([trait-score:]trait-property[,trait-property[,...]])]
47072 trait-score:
47073 score(score-expression) */
47075 static tree
47076 cp_parser_omp_context_selector (cp_parser *parser, tree set, bool has_parms_p)
47078 tree ret = NULL_TREE;
47081 tree selector;
47082 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
47083 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47084 selector = cp_lexer_peek_token (parser->lexer)->u.value;
47085 else
47087 cp_parser_error (parser, "expected trait selector name");
47088 return error_mark_node;
47091 tree properties = NULL_TREE;
47092 const char *const *selectors = NULL;
47093 bool allow_score = true;
47094 bool allow_user = false;
47095 int property_limit = 0;
47096 enum { CTX_PROPERTY_NONE, CTX_PROPERTY_USER, CTX_PROPERTY_NAME_LIST,
47097 CTX_PROPERTY_ID, CTX_PROPERTY_EXPR,
47098 CTX_PROPERTY_SIMD } property_kind = CTX_PROPERTY_NONE;
47099 switch (IDENTIFIER_POINTER (set)[0])
47101 case 'c': /* construct */
47102 selectors = omp_construct_selectors;
47103 allow_score = false;
47104 property_limit = 1;
47105 property_kind = CTX_PROPERTY_SIMD;
47106 break;
47107 case 'd': /* device */
47108 selectors = omp_device_selectors;
47109 allow_score = false;
47110 allow_user = true;
47111 property_limit = 3;
47112 property_kind = CTX_PROPERTY_NAME_LIST;
47113 break;
47114 case 'i': /* implementation */
47115 selectors = omp_implementation_selectors;
47116 allow_user = true;
47117 property_limit = 3;
47118 property_kind = CTX_PROPERTY_NAME_LIST;
47119 break;
47120 case 'u': /* user */
47121 selectors = omp_user_selectors;
47122 property_limit = 1;
47123 property_kind = CTX_PROPERTY_EXPR;
47124 break;
47125 default:
47126 gcc_unreachable ();
47128 for (int i = 0; ; i++)
47130 if (selectors[i] == NULL)
47132 if (allow_user)
47134 property_kind = CTX_PROPERTY_USER;
47135 break;
47137 else
47139 error ("selector %qs not allowed for context selector "
47140 "set %qs", IDENTIFIER_POINTER (selector),
47141 IDENTIFIER_POINTER (set));
47142 cp_lexer_consume_token (parser->lexer);
47143 return error_mark_node;
47146 if (i == property_limit)
47147 property_kind = CTX_PROPERTY_NONE;
47148 if (strcmp (selectors[i], IDENTIFIER_POINTER (selector)) == 0)
47149 break;
47151 if (property_kind == CTX_PROPERTY_NAME_LIST
47152 && IDENTIFIER_POINTER (set)[0] == 'i'
47153 && strcmp (IDENTIFIER_POINTER (selector),
47154 "atomic_default_mem_order") == 0)
47155 property_kind = CTX_PROPERTY_ID;
47157 cp_lexer_consume_token (parser->lexer);
47159 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
47161 if (property_kind == CTX_PROPERTY_NONE)
47163 error ("selector %qs does not accept any properties",
47164 IDENTIFIER_POINTER (selector));
47165 return error_mark_node;
47168 matching_parens parens;
47169 parens.consume_open (parser);
47171 cp_token *token = cp_lexer_peek_token (parser->lexer);
47172 if (allow_score
47173 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
47174 && strcmp (IDENTIFIER_POINTER (token->u.value), "score") == 0
47175 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
47177 cp_lexer_save_tokens (parser->lexer);
47178 cp_lexer_consume_token (parser->lexer);
47179 cp_lexer_consume_token (parser->lexer);
47180 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
47181 true)
47182 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
47184 cp_lexer_rollback_tokens (parser->lexer);
47185 cp_lexer_consume_token (parser->lexer);
47187 matching_parens parens2;
47188 parens2.require_open (parser);
47189 tree score = cp_parser_constant_expression (parser);
47190 if (!parens2.require_close (parser))
47191 cp_parser_skip_to_closing_parenthesis (parser, true,
47192 false, true);
47193 cp_parser_require (parser, CPP_COLON, RT_COLON);
47194 if (score != error_mark_node)
47196 score = fold_non_dependent_expr (score);
47197 if (value_dependent_expression_p (score))
47198 properties = tree_cons (get_identifier (" score"),
47199 score, properties);
47200 else if (!INTEGRAL_TYPE_P (TREE_TYPE (score))
47201 || TREE_CODE (score) != INTEGER_CST)
47202 error_at (token->location, "score argument must be "
47203 "constant integer expression");
47204 else if (tree_int_cst_sgn (score) < 0)
47205 error_at (token->location, "score argument must be "
47206 "non-negative");
47207 else
47208 properties = tree_cons (get_identifier (" score"),
47209 score, properties);
47212 else
47213 cp_lexer_rollback_tokens (parser->lexer);
47215 token = cp_lexer_peek_token (parser->lexer);
47218 switch (property_kind)
47220 tree t;
47221 case CTX_PROPERTY_USER:
47224 t = cp_parser_constant_expression (parser);
47225 if (t != error_mark_node)
47227 t = fold_non_dependent_expr (t);
47228 if (TREE_CODE (t) == STRING_CST)
47229 properties = tree_cons (NULL_TREE, t, properties);
47230 else if (!value_dependent_expression_p (t)
47231 && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
47232 || !tree_fits_shwi_p (t)))
47233 error_at (token->location, "property must be "
47234 "constant integer expression or string "
47235 "literal");
47236 else
47237 properties = tree_cons (NULL_TREE, t, properties);
47239 else
47240 return error_mark_node;
47242 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
47243 cp_lexer_consume_token (parser->lexer);
47244 else
47245 break;
47247 while (1);
47248 break;
47249 case CTX_PROPERTY_ID:
47250 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
47251 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47253 tree prop = cp_lexer_peek_token (parser->lexer)->u.value;
47254 cp_lexer_consume_token (parser->lexer);
47255 properties = tree_cons (prop, NULL_TREE, properties);
47257 else
47259 cp_parser_error (parser, "expected identifier");
47260 return error_mark_node;
47262 break;
47263 case CTX_PROPERTY_NAME_LIST:
47266 tree prop = NULL_TREE, value = NULL_TREE;
47267 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
47268 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47270 prop = cp_lexer_peek_token (parser->lexer)->u.value;
47271 cp_lexer_consume_token (parser->lexer);
47273 else if (cp_lexer_next_token_is (parser->lexer, CPP_STRING))
47274 value = cp_parser_string_literal (parser,
47275 /*translate=*/false,
47276 /*wide_ok=*/false);
47277 else
47279 cp_parser_error (parser, "expected identifier or "
47280 "string literal");
47281 return error_mark_node;
47284 properties = tree_cons (prop, value, properties);
47286 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
47287 cp_lexer_consume_token (parser->lexer);
47288 else
47289 break;
47291 while (1);
47292 break;
47293 case CTX_PROPERTY_EXPR:
47294 t = cp_parser_constant_expression (parser);
47295 if (t != error_mark_node)
47297 t = fold_non_dependent_expr (t);
47298 if (!value_dependent_expression_p (t)
47299 && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
47300 || !tree_fits_shwi_p (t)))
47301 error_at (token->location, "property must be "
47302 "constant integer expression");
47303 else
47304 properties = tree_cons (NULL_TREE, t, properties);
47306 else
47307 return error_mark_node;
47308 break;
47309 case CTX_PROPERTY_SIMD:
47310 if (!has_parms_p)
47312 error_at (token->location, "properties for %<simd%> "
47313 "selector may not be specified in "
47314 "%<metadirective%>");
47315 return error_mark_node;
47317 properties
47318 = cp_parser_omp_all_clauses (parser,
47319 OMP_DECLARE_SIMD_CLAUSE_MASK,
47320 "simd", NULL, true, 2);
47321 break;
47322 default:
47323 gcc_unreachable ();
47326 if (!parens.require_close (parser))
47327 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
47329 properties = nreverse (properties);
47331 else if (property_kind == CTX_PROPERTY_NAME_LIST
47332 || property_kind == CTX_PROPERTY_ID
47333 || property_kind == CTX_PROPERTY_EXPR)
47335 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
47336 return error_mark_node;
47339 ret = tree_cons (selector, properties, ret);
47341 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
47342 cp_lexer_consume_token (parser->lexer);
47343 else
47344 break;
47346 while (1);
47348 return nreverse (ret);
47351 /* OpenMP 5.0:
47353 trait-set-selector[,trait-set-selector[,...]]
47355 trait-set-selector:
47356 trait-set-selector-name = { trait-selector[, trait-selector[, ...]] }
47358 trait-set-selector-name:
47359 constructor
47360 device
47361 implementation
47362 user */
47364 static tree
47365 cp_parser_omp_context_selector_specification (cp_parser *parser,
47366 bool has_parms_p)
47368 tree ret = NULL_TREE;
47371 const char *setp = "";
47372 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47373 setp
47374 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
47375 switch (setp[0])
47377 case 'c':
47378 if (strcmp (setp, "construct") == 0)
47379 setp = NULL;
47380 break;
47381 case 'd':
47382 if (strcmp (setp, "device") == 0)
47383 setp = NULL;
47384 break;
47385 case 'i':
47386 if (strcmp (setp, "implementation") == 0)
47387 setp = NULL;
47388 break;
47389 case 'u':
47390 if (strcmp (setp, "user") == 0)
47391 setp = NULL;
47392 break;
47393 default:
47394 break;
47396 if (setp)
47398 cp_parser_error (parser, "expected %<construct%>, %<device%>, "
47399 "%<implementation%> or %<user%>");
47400 return error_mark_node;
47403 tree set = cp_lexer_peek_token (parser->lexer)->u.value;
47404 cp_lexer_consume_token (parser->lexer);
47406 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
47407 return error_mark_node;
47409 matching_braces braces;
47410 if (!braces.require_open (parser))
47411 return error_mark_node;
47413 tree selectors
47414 = cp_parser_omp_context_selector (parser, set, has_parms_p);
47415 if (selectors == error_mark_node)
47417 cp_parser_skip_to_closing_brace (parser);
47418 ret = error_mark_node;
47420 else if (ret != error_mark_node)
47421 ret = tree_cons (set, selectors, ret);
47423 braces.require_close (parser);
47425 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
47426 cp_lexer_consume_token (parser->lexer);
47427 else
47428 break;
47430 while (1);
47432 if (ret == error_mark_node)
47433 return ret;
47434 return nreverse (ret);
47437 /* Assumption clauses:
47438 OpenMP 5.1
47439 absent (directive-name-list)
47440 contains (directive-name-list)
47441 holds (expression)
47442 no_openmp
47443 no_openmp_routines
47444 no_parallelism */
47446 static void
47447 cp_parser_omp_assumption_clauses (cp_parser *parser, cp_token *pragma_tok,
47448 bool is_assume)
47450 bool no_openmp = false;
47451 bool no_openmp_routines = false;
47452 bool no_parallelism = false;
47453 bitmap_head absent_head, contains_head;
47455 bitmap_obstack_initialize (NULL);
47456 bitmap_initialize (&absent_head, &bitmap_default_obstack);
47457 bitmap_initialize (&contains_head, &bitmap_default_obstack);
47459 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
47460 error_at (cp_lexer_peek_token (parser->lexer)->location,
47461 "expected at least one assumption clause");
47463 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
47465 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
47466 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
47467 cp_lexer_consume_token (parser->lexer);
47469 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47470 break;
47472 const char *p
47473 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
47474 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
47476 if (!strcmp (p, "no_openmp"))
47478 cp_lexer_consume_token (parser->lexer);
47479 if (no_openmp)
47480 error_at (cloc, "too many %qs clauses", "no_openmp");
47481 no_openmp = true;
47483 else if (!strcmp (p, "no_openmp_routines"))
47485 cp_lexer_consume_token (parser->lexer);
47486 if (no_openmp_routines)
47487 error_at (cloc, "too many %qs clauses", "no_openmp_routines");
47488 no_openmp_routines = true;
47490 else if (!strcmp (p, "no_parallelism"))
47492 cp_lexer_consume_token (parser->lexer);
47493 if (no_parallelism)
47494 error_at (cloc, "too many %qs clauses", "no_parallelism");
47495 no_parallelism = true;
47497 else if (!strcmp (p, "holds"))
47499 cp_lexer_consume_token (parser->lexer);
47500 matching_parens parens;
47501 if (parens.require_open (parser))
47503 location_t eloc = cp_lexer_peek_token (parser->lexer)->location;
47504 tree t = cp_parser_assignment_expression (parser);
47505 if (!type_dependent_expression_p (t))
47506 t = contextual_conv_bool (t, tf_warning_or_error);
47507 if (is_assume && !error_operand_p (t))
47508 finish_expr_stmt (build_assume_call (eloc, t));
47509 if (!parens.require_close (parser))
47510 cp_parser_skip_to_closing_parenthesis (parser,
47511 /*recovering=*/true,
47512 /*or_comma=*/false,
47513 /*consume_paren=*/true);
47516 else if (!strcmp (p, "absent") || !strcmp (p, "contains"))
47518 cp_lexer_consume_token (parser->lexer);
47519 matching_parens parens;
47520 if (parens.require_open (parser))
47524 const char *directive[3] = {};
47525 int i;
47526 location_t dloc
47527 = cp_lexer_peek_token (parser->lexer)->location;
47528 for (i = 0; i < 3; i++)
47530 tree id;
47531 if (cp_lexer_nth_token_is (parser->lexer, i + 1, CPP_NAME))
47532 id = cp_lexer_peek_nth_token (parser->lexer,
47533 i + 1)->u.value;
47534 else if (cp_lexer_nth_token_is (parser->lexer, i + 1,
47535 CPP_KEYWORD))
47537 enum rid rid
47538 = cp_lexer_peek_nth_token (parser->lexer,
47539 i + 1)->keyword;
47540 id = ridpointers[rid];
47542 else
47543 break;
47544 directive[i] = IDENTIFIER_POINTER (id);
47546 if (i == 0)
47547 error_at (dloc, "expected directive name");
47548 else
47550 const struct c_omp_directive *dir
47551 = c_omp_categorize_directive (directive[0],
47552 directive[1],
47553 directive[2]);
47554 if (dir == NULL
47555 || dir->kind == C_OMP_DIR_DECLARATIVE
47556 || dir->kind == C_OMP_DIR_INFORMATIONAL
47557 || dir->id == PRAGMA_OMP_END
47558 || (!dir->second && directive[1])
47559 || (!dir->third && directive[2]))
47560 error_at (dloc, "unknown OpenMP directive name in "
47561 "%qs clause argument", p);
47562 else
47564 int id = dir - c_omp_directives;
47565 if (bitmap_bit_p (p[0] == 'a' ? &contains_head
47566 : &absent_head, id))
47567 error_at (dloc, "%<%s%s%s%s%s%> directive "
47568 "mentioned in both %<absent%> and "
47569 "%<contains%> clauses",
47570 directive[0],
47571 directive[1] ? " " : "",
47572 directive[1] ? directive[1] : "",
47573 directive[2] ? " " : "",
47574 directive[2] ? directive[2] : "");
47575 else if (!bitmap_set_bit (p[0] == 'a'
47576 ? &absent_head
47577 : &contains_head, id))
47578 error_at (dloc, "%<%s%s%s%s%s%> directive "
47579 "mentioned multiple times in %qs "
47580 "clauses",
47581 directive[0],
47582 directive[1] ? " " : "",
47583 directive[1] ? directive[1] : "",
47584 directive[2] ? " " : "",
47585 directive[2] ? directive[2] : "", p);
47587 for (; i; --i)
47588 cp_lexer_consume_token (parser->lexer);
47590 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
47591 cp_lexer_consume_token (parser->lexer);
47592 else
47593 break;
47595 while (1);
47596 if (!parens.require_close (parser))
47597 cp_parser_skip_to_closing_parenthesis (parser,
47598 /*recovering=*/true,
47599 /*or_comma=*/false,
47600 /*consume_paren=*/true);
47603 else if (startswith (p, "ext_"))
47605 warning_at (cloc, 0, "unknown assumption clause %qs", p);
47606 cp_lexer_consume_token (parser->lexer);
47607 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
47608 for (size_t n = cp_parser_skip_balanced_tokens (parser, 1) - 1;
47609 n; --n)
47610 cp_lexer_consume_token (parser->lexer);
47612 else
47614 cp_lexer_consume_token (parser->lexer);
47615 error_at (cloc, "expected assumption clause");
47616 break;
47619 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47622 /* OpenMP 5.1
47623 # pragma omp assume clauses[optseq] new-line */
47625 static void
47626 cp_parser_omp_assume (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
47628 cp_parser_omp_assumption_clauses (parser, pragma_tok, true);
47629 add_stmt (cp_parser_omp_structured_block (parser, if_p));
47632 /* OpenMP 5.1
47633 # pragma omp assumes clauses[optseq] new-line */
47635 static bool
47636 cp_parser_omp_assumes (cp_parser *parser, cp_token *pragma_tok)
47638 cp_parser_omp_assumption_clauses (parser, pragma_tok, false);
47639 return false;
47642 /* Finalize #pragma omp declare variant after a fndecl has been parsed, and put
47643 that into "omp declare variant base" attribute. */
47645 static tree
47646 cp_finish_omp_declare_variant (cp_parser *parser, cp_token *pragma_tok,
47647 tree attrs)
47649 matching_parens parens;
47650 if (!parens.require_open (parser))
47652 fail:
47653 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47654 return attrs;
47657 bool template_p;
47658 cp_id_kind idk = CP_ID_KIND_NONE;
47659 cp_token *varid_token = cp_lexer_peek_token (parser->lexer);
47660 cp_expr varid
47661 = cp_parser_id_expression (parser, /*template_keyword_p=*/false,
47662 /*check_dependency_p=*/true,
47663 /*template_p=*/&template_p,
47664 /*declarator_p=*/false,
47665 /*optional_p=*/false);
47666 parens.require_close (parser);
47668 tree variant;
47669 if (TREE_CODE (varid) == TEMPLATE_ID_EXPR
47670 || TREE_CODE (varid) == TYPE_DECL
47671 || varid == error_mark_node)
47672 variant = varid;
47673 else if (varid_token->type == CPP_NAME && varid_token->error_reported)
47674 variant = NULL_TREE;
47675 else
47677 tree ambiguous_decls;
47678 variant = cp_parser_lookup_name (parser, varid, none_type,
47679 template_p, /*is_namespace=*/false,
47680 /*check_dependency=*/true,
47681 &ambiguous_decls,
47682 varid.get_location ());
47683 if (ambiguous_decls)
47684 variant = NULL_TREE;
47686 if (variant == NULL_TREE)
47687 variant = error_mark_node;
47688 else if (TREE_CODE (variant) != SCOPE_REF)
47690 const char *error_msg;
47691 variant
47692 = finish_id_expression (varid, variant, parser->scope,
47693 &idk, false, true,
47694 &parser->non_integral_constant_expression_p,
47695 template_p, true, false, false, &error_msg,
47696 varid.get_location ());
47697 if (error_msg)
47698 cp_parser_error (parser, error_msg);
47700 location_t caret_loc = get_pure_location (varid.get_location ());
47701 location_t start_loc = get_start (varid_token->location);
47702 location_t finish_loc = get_finish (varid.get_location ());
47703 location_t varid_loc = make_location (caret_loc, start_loc, finish_loc);
47705 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
47706 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
47707 cp_lexer_consume_token (parser->lexer);
47709 const char *clause = "";
47710 location_t match_loc = cp_lexer_peek_token (parser->lexer)->location;
47711 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47712 clause = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
47713 if (strcmp (clause, "match"))
47715 cp_parser_error (parser, "expected %<match%>");
47716 goto fail;
47719 cp_lexer_consume_token (parser->lexer);
47721 if (!parens.require_open (parser))
47722 goto fail;
47724 tree ctx = cp_parser_omp_context_selector_specification (parser, true);
47725 if (ctx == error_mark_node)
47726 goto fail;
47727 ctx = omp_check_context_selector (match_loc, ctx);
47728 if (ctx != error_mark_node && variant != error_mark_node)
47730 tree match_loc_node = maybe_wrap_with_location (integer_zero_node,
47731 match_loc);
47732 tree loc_node = maybe_wrap_with_location (integer_zero_node, varid_loc);
47733 loc_node = tree_cons (match_loc_node,
47734 build_int_cst (integer_type_node, idk),
47735 build_tree_list (loc_node, integer_zero_node));
47736 attrs = tree_cons (get_identifier ("omp declare variant base"),
47737 tree_cons (variant, ctx, loc_node), attrs);
47738 if (processing_template_decl)
47739 ATTR_IS_DEPENDENT (attrs) = 1;
47742 parens.require_close (parser);
47743 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47744 return attrs;
47748 /* Finalize #pragma omp declare simd clauses after direct declarator has
47749 been parsed, and put that into "omp declare simd" attribute. */
47751 static tree
47752 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
47754 struct cp_token_cache *ce;
47755 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
47756 int i;
47758 if (!data->error_seen && data->fndecl_seen)
47760 error ("%<#pragma omp declare %s%> not immediately followed by "
47761 "a single function declaration or definition",
47762 data->variant_p ? "variant" : "simd");
47763 data->error_seen = true;
47765 if (data->error_seen)
47766 return attrs;
47768 FOR_EACH_VEC_ELT (data->tokens, i, ce)
47770 tree c, cl;
47772 cp_parser_push_lexer_for_tokens (parser, ce);
47773 parser->lexer->in_pragma = true;
47774 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
47775 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
47776 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
47777 const char *kind = IDENTIFIER_POINTER (id);
47778 cp_lexer_consume_token (parser->lexer);
47779 if (strcmp (kind, "simd") == 0)
47781 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
47782 "#pragma omp declare simd",
47783 pragma_tok);
47784 if (cl)
47785 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
47786 c = build_tree_list (get_identifier ("omp declare simd"), cl);
47787 TREE_CHAIN (c) = attrs;
47788 if (processing_template_decl)
47789 ATTR_IS_DEPENDENT (c) = 1;
47790 attrs = c;
47792 else
47794 gcc_assert (strcmp (kind, "variant") == 0);
47795 attrs
47796 = cp_finish_omp_declare_variant (parser, pragma_tok, attrs);
47798 cp_parser_pop_lexer (parser);
47801 cp_lexer *lexer = NULL;
47802 for (int i = 0; i < 2; i++)
47804 if (data->attribs[i] == NULL)
47805 continue;
47806 for (tree *pa = data->attribs[i]; *pa; )
47807 if (get_attribute_namespace (*pa) == omp_identifier
47808 && is_attribute_p ("directive", get_attribute_name (*pa)))
47810 for (tree a = TREE_VALUE (*pa); a; a = TREE_CHAIN (a))
47812 tree d = TREE_VALUE (a);
47813 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
47814 cp_token *first = DEFPARSE_TOKENS (d)->first;
47815 cp_token *last = DEFPARSE_TOKENS (d)->last;
47816 const char *directive[3] = {};
47817 for (int j = 0; j < 3; j++)
47819 tree id = NULL_TREE;
47820 if (first + j == last)
47821 break;
47822 if (first[j].type == CPP_NAME)
47823 id = first[j].u.value;
47824 else if (first[j].type == CPP_KEYWORD)
47825 id = ridpointers[(int) first[j].keyword];
47826 else
47827 break;
47828 directive[j] = IDENTIFIER_POINTER (id);
47830 const c_omp_directive *dir = NULL;
47831 if (directive[0])
47832 dir = c_omp_categorize_directive (directive[0], directive[1],
47833 directive[2]);
47834 if (dir == NULL)
47836 error_at (first->location,
47837 "unknown OpenMP directive name in "
47838 "%<omp::directive%> attribute argument");
47839 continue;
47841 if (dir->id != PRAGMA_OMP_DECLARE
47842 || (strcmp (directive[1], "simd") != 0
47843 && strcmp (directive[1], "variant") != 0))
47845 error_at (first->location,
47846 "OpenMP directive other than %<declare simd%> "
47847 "or %<declare variant%> appertains to a "
47848 "declaration");
47849 continue;
47852 if (parser->omp_attrs_forbidden_p)
47854 error_at (first->location,
47855 "mixing OpenMP directives with attribute and "
47856 "pragma syntax on the same statement");
47857 parser->omp_attrs_forbidden_p = false;
47860 if (!flag_openmp && strcmp (directive[1], "simd") != 0)
47861 continue;
47862 if (lexer == NULL)
47864 lexer = cp_lexer_alloc ();
47865 lexer->debugging_p = parser->lexer->debugging_p;
47867 vec_safe_reserve (lexer->buffer, (last - first) + 2);
47868 cp_token tok = {};
47869 tok.type = CPP_PRAGMA;
47870 tok.keyword = RID_MAX;
47871 tok.u.value = build_int_cst (NULL, PRAGMA_OMP_DECLARE);
47872 tok.location = first->location;
47873 lexer->buffer->quick_push (tok);
47874 while (++first < last)
47875 lexer->buffer->quick_push (*first);
47876 tok = {};
47877 tok.type = CPP_PRAGMA_EOL;
47878 tok.keyword = RID_MAX;
47879 tok.location = last->location;
47880 lexer->buffer->quick_push (tok);
47881 tok = {};
47882 tok.type = CPP_EOF;
47883 tok.keyword = RID_MAX;
47884 tok.location = last->location;
47885 lexer->buffer->quick_push (tok);
47886 lexer->next = parser->lexer;
47887 lexer->next_token = lexer->buffer->address ();
47888 lexer->last_token = lexer->next_token
47889 + lexer->buffer->length ()
47890 - 1;
47891 lexer->in_omp_attribute_pragma = true;
47892 parser->lexer = lexer;
47893 /* Move the current source position to that of the first token
47894 in the new lexer. */
47895 cp_lexer_set_source_position_from_token (lexer->next_token);
47897 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
47898 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
47899 const char *kind = IDENTIFIER_POINTER (id);
47900 cp_lexer_consume_token (parser->lexer);
47902 tree c, cl;
47903 if (strcmp (kind, "simd") == 0)
47905 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
47906 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
47907 cp_lexer_consume_token (parser->lexer);
47909 omp_clause_mask mask = OMP_DECLARE_SIMD_CLAUSE_MASK;
47910 cl = cp_parser_omp_all_clauses (parser, mask,
47911 "#pragma omp declare simd",
47912 pragma_tok);
47913 if (cl)
47914 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
47915 c = build_tree_list (get_identifier ("omp declare simd"),
47916 cl);
47917 TREE_CHAIN (c) = attrs;
47918 if (processing_template_decl)
47919 ATTR_IS_DEPENDENT (c) = 1;
47920 attrs = c;
47922 else
47924 gcc_assert (strcmp (kind, "variant") == 0);
47925 attrs
47926 = cp_finish_omp_declare_variant (parser, pragma_tok,
47927 attrs);
47929 gcc_assert (parser->lexer != lexer);
47930 vec_safe_truncate (lexer->buffer, 0);
47932 *pa = TREE_CHAIN (*pa);
47934 else
47935 pa = &TREE_CHAIN (*pa);
47937 if (lexer)
47938 cp_lexer_destroy (lexer);
47940 data->fndecl_seen = true;
47941 return attrs;
47944 /* Helper for cp_parser_omp_declare_target, handle one to or link clause
47945 on #pragma omp declare target. Return false if errors were reported. */
47947 static bool
47948 handle_omp_declare_target_clause (tree c, tree t, int device_type)
47950 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
47951 tree at2 = lookup_attribute ("omp declare target link", DECL_ATTRIBUTES (t));
47952 tree id;
47953 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
47955 id = get_identifier ("omp declare target link");
47956 std::swap (at1, at2);
47958 else
47959 id = get_identifier ("omp declare target");
47960 if (at2)
47962 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
47963 error_at (OMP_CLAUSE_LOCATION (c),
47964 "%qD specified both in declare target %<link%> and %qs"
47965 " clauses", t, OMP_CLAUSE_ENTER_TO (c) ? "to" : "enter");
47966 else
47967 error_at (OMP_CLAUSE_LOCATION (c),
47968 "%qD specified both in declare target %<link%> and "
47969 "%<to%> or %<enter%> clauses", t);
47970 return false;
47972 if (!at1)
47974 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
47975 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
47976 return true;
47978 symtab_node *node = symtab_node::get (t);
47979 if (node != NULL)
47981 node->offloadable = 1;
47982 if (ENABLE_OFFLOADING)
47984 g->have_offload = true;
47985 if (is_a <varpool_node *> (node))
47986 vec_safe_push (offload_vars, t);
47990 if (TREE_CODE (t) != FUNCTION_DECL)
47991 return true;
47992 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_HOST) != 0)
47994 tree at3 = lookup_attribute ("omp declare target host",
47995 DECL_ATTRIBUTES (t));
47996 if (at3 == NULL_TREE)
47998 id = get_identifier ("omp declare target host");
47999 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
48002 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_NOHOST) != 0)
48004 tree at3 = lookup_attribute ("omp declare target nohost",
48005 DECL_ATTRIBUTES (t));
48006 if (at3 == NULL_TREE)
48008 id = get_identifier ("omp declare target nohost");
48009 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
48012 return true;
48015 /* OpenMP 4.0:
48016 # pragma omp declare target new-line
48017 declarations and definitions
48018 # pragma omp end declare target new-line
48020 OpenMP 4.5:
48021 # pragma omp declare target ( extended-list ) new-line
48023 # pragma omp declare target declare-target-clauses[seq] new-line */
48025 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
48026 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
48027 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ENTER) \
48028 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK) \
48029 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE_TYPE))
48031 static void
48032 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
48034 tree clauses = NULL_TREE;
48035 int device_type = 0;
48036 bool only_device_type = true;
48037 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
48038 || (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
48039 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME)))
48040 clauses
48041 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
48042 "#pragma omp declare target", pragma_tok);
48043 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
48045 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_ENTER,
48046 clauses);
48047 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
48048 cp_parser_require_pragma_eol (parser, pragma_tok);
48050 else
48052 cp_omp_declare_target_attr a
48053 = { parser->lexer->in_omp_attribute_pragma, -1 };
48054 vec_safe_push (scope_chain->omp_declare_target_attribute, a);
48055 cp_parser_require_pragma_eol (parser, pragma_tok);
48056 return;
48058 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
48059 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE)
48060 device_type |= OMP_CLAUSE_DEVICE_TYPE_KIND (c);
48061 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
48063 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE)
48064 continue;
48065 tree t = OMP_CLAUSE_DECL (c);
48066 only_device_type = false;
48067 if (!handle_omp_declare_target_clause (c, t, device_type))
48068 continue;
48069 if (VAR_OR_FUNCTION_DECL_P (t)
48070 && DECL_LOCAL_DECL_P (t)
48071 && DECL_LANG_SPECIFIC (t)
48072 && DECL_LOCAL_DECL_ALIAS (t)
48073 && DECL_LOCAL_DECL_ALIAS (t) != error_mark_node)
48074 handle_omp_declare_target_clause (c, DECL_LOCAL_DECL_ALIAS (t),
48075 device_type);
48077 if (device_type && only_device_type)
48078 error_at (OMP_CLAUSE_LOCATION (clauses),
48079 "directive with only %<device_type%> clause");
48082 /* OpenMP 5.1
48083 # pragma omp begin assumes clauses[optseq] new-line
48085 # pragma omp begin declare target clauses[optseq] new-line */
48087 #define OMP_BEGIN_DECLARE_TARGET_CLAUSE_MASK \
48088 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE_TYPE)
48090 static void
48091 cp_parser_omp_begin (cp_parser *parser, cp_token *pragma_tok)
48093 const char *p = "";
48094 bool in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
48095 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
48097 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
48098 p = IDENTIFIER_POINTER (id);
48100 if (strcmp (p, "declare") == 0)
48102 cp_lexer_consume_token (parser->lexer);
48103 p = "";
48104 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
48106 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
48107 p = IDENTIFIER_POINTER (id);
48109 if (strcmp (p, "target") == 0)
48111 cp_lexer_consume_token (parser->lexer);
48112 tree clauses
48113 = cp_parser_omp_all_clauses (parser,
48114 OMP_BEGIN_DECLARE_TARGET_CLAUSE_MASK,
48115 "#pragma omp begin declare target",
48116 pragma_tok);
48117 int device_type = 0;
48118 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
48119 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE)
48120 device_type |= OMP_CLAUSE_DEVICE_TYPE_KIND (c);
48121 cp_omp_declare_target_attr a
48122 = { in_omp_attribute_pragma, device_type };
48123 vec_safe_push (scope_chain->omp_declare_target_attribute, a);
48125 else
48127 cp_parser_error (parser, "expected %<target%>");
48128 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
48131 else if (strcmp (p, "assumes") == 0)
48133 cp_lexer_consume_token (parser->lexer);
48134 cp_parser_omp_assumption_clauses (parser, pragma_tok, false);
48135 cp_omp_begin_assumes_data a = { in_omp_attribute_pragma };
48136 vec_safe_push (scope_chain->omp_begin_assumes, a);
48138 else
48140 cp_parser_error (parser, "expected %<declare target%> or %<assumes%>");
48141 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
48145 /* OpenMP 4.0:
48146 # pragma omp end declare target new-line
48148 OpenMP 5.1:
48149 # pragma omp end assumes new-line */
48151 static void
48152 cp_parser_omp_end (cp_parser *parser, cp_token *pragma_tok)
48154 const char *p = "";
48155 bool in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
48156 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
48158 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
48159 p = IDENTIFIER_POINTER (id);
48161 if (strcmp (p, "declare") == 0)
48163 cp_lexer_consume_token (parser->lexer);
48164 p = "";
48165 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
48167 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
48168 p = IDENTIFIER_POINTER (id);
48170 if (strcmp (p, "target") == 0)
48171 cp_lexer_consume_token (parser->lexer);
48172 else
48174 cp_parser_error (parser, "expected %<target%>");
48175 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
48176 return;
48178 cp_parser_require_pragma_eol (parser, pragma_tok);
48179 if (!vec_safe_length (scope_chain->omp_declare_target_attribute))
48180 error_at (pragma_tok->location,
48181 "%<#pragma omp end declare target%> without corresponding "
48182 "%<#pragma omp declare target%> or "
48183 "%<#pragma omp begin declare target%>");
48184 else
48186 cp_omp_declare_target_attr
48187 a = scope_chain->omp_declare_target_attribute->pop ();
48188 if (a.attr_syntax != in_omp_attribute_pragma)
48190 if (a.attr_syntax)
48191 error_at (pragma_tok->location,
48192 "%qs in attribute syntax terminated "
48193 "with %qs in pragma syntax",
48194 a.device_type >= 0 ? "begin declare target"
48195 : "declare target",
48196 "end declare target");
48197 else
48198 error_at (pragma_tok->location,
48199 "%qs in pragma syntax terminated "
48200 "with %qs in attribute syntax",
48201 a.device_type >= 0 ? "begin declare target"
48202 : "declare target",
48203 "end declare target");
48207 else if (strcmp (p, "assumes") == 0)
48209 cp_lexer_consume_token (parser->lexer);
48210 cp_parser_require_pragma_eol (parser, pragma_tok);
48211 if (!vec_safe_length (scope_chain->omp_begin_assumes))
48212 error_at (pragma_tok->location,
48213 "%qs without corresponding %qs",
48214 "#pragma omp end assumes", "#pragma omp begin assumes");
48215 else
48217 cp_omp_begin_assumes_data
48218 a = scope_chain->omp_begin_assumes->pop ();
48219 if (a.attr_syntax != in_omp_attribute_pragma)
48221 if (a.attr_syntax)
48222 error_at (pragma_tok->location,
48223 "%qs in attribute syntax terminated "
48224 "with %qs in pragma syntax",
48225 "begin assumes", "end assumes");
48226 else
48227 error_at (pragma_tok->location,
48228 "%qs in pragma syntax terminated "
48229 "with %qs in attribute syntax",
48230 "begin assumes", "end assumes");
48234 else
48236 cp_parser_error (parser, "expected %<declare%> or %<assumes%>");
48237 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
48238 return;
48242 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
48243 expression and optional initializer clause of
48244 #pragma omp declare reduction. We store the expression(s) as
48245 either 3, 6 or 7 special statements inside of the artificial function's
48246 body. The first two statements are DECL_EXPRs for the artificial
48247 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
48248 expression that uses those variables.
48249 If there was any INITIALIZER clause, this is followed by further statements,
48250 the fourth and fifth statements are DECL_EXPRs for the artificial
48251 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
48252 constructor variant (first token after open paren is not omp_priv),
48253 then the sixth statement is a statement with the function call expression
48254 that uses the OMP_PRIV and optionally OMP_ORIG variable.
48255 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
48256 to initialize the OMP_PRIV artificial variable and there is seventh
48257 statement, a DECL_EXPR of the OMP_PRIV statement again. */
48259 static bool
48260 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
48262 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
48263 gcc_assert (TYPE_REF_P (type));
48264 type = TREE_TYPE (type);
48265 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
48266 DECL_ARTIFICIAL (omp_out) = 1;
48267 pushdecl (omp_out);
48268 add_decl_expr (omp_out);
48269 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
48270 DECL_ARTIFICIAL (omp_in) = 1;
48271 pushdecl (omp_in);
48272 add_decl_expr (omp_in);
48273 tree combiner;
48274 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
48276 keep_next_level (true);
48277 tree block = begin_omp_structured_block ();
48278 combiner = cp_parser_expression (parser);
48279 finish_expr_stmt (combiner);
48280 block = finish_omp_structured_block (block);
48281 if (processing_template_decl)
48282 block = build_stmt (input_location, EXPR_STMT, block);
48283 add_stmt (block);
48285 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
48286 return false;
48288 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
48289 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
48290 cp_lexer_consume_token (parser->lexer);
48292 const char *p = "";
48293 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
48295 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
48296 p = IDENTIFIER_POINTER (id);
48299 if (strcmp (p, "initializer") == 0)
48301 cp_lexer_consume_token (parser->lexer);
48302 matching_parens parens;
48303 if (!parens.require_open (parser))
48304 return false;
48306 p = "";
48307 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
48309 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
48310 p = IDENTIFIER_POINTER (id);
48313 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
48314 DECL_ARTIFICIAL (omp_priv) = 1;
48315 pushdecl (omp_priv);
48316 add_decl_expr (omp_priv);
48317 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
48318 DECL_ARTIFICIAL (omp_orig) = 1;
48319 pushdecl (omp_orig);
48320 add_decl_expr (omp_orig);
48322 keep_next_level (true);
48323 block = begin_omp_structured_block ();
48325 bool ctor = false;
48326 if (strcmp (p, "omp_priv") == 0)
48328 bool is_non_constant_init;
48329 ctor = true;
48330 cp_lexer_consume_token (parser->lexer);
48331 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
48332 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
48333 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
48334 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
48335 == CPP_CLOSE_PAREN
48336 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
48337 == CPP_CLOSE_PAREN))
48339 finish_omp_structured_block (block);
48340 error ("invalid initializer clause");
48341 return false;
48343 initializer = cp_parser_initializer (parser,
48344 /*is_direct_init=*/nullptr,
48345 &is_non_constant_init);
48346 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
48347 NULL_TREE, LOOKUP_ONLYCONVERTING);
48349 else
48351 cp_parser_parse_tentatively (parser);
48352 /* Don't create location wrapper nodes here. */
48353 auto_suppress_location_wrappers sentinel;
48354 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
48355 /*check_dependency_p=*/true,
48356 /*template_p=*/NULL,
48357 /*declarator_p=*/false,
48358 /*optional_p=*/false);
48359 vec<tree, va_gc> *args;
48360 if (fn_name == error_mark_node
48361 || cp_parser_error_occurred (parser)
48362 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
48363 || ((args = cp_parser_parenthesized_expression_list
48364 (parser, non_attr, /*cast_p=*/false,
48365 /*allow_expansion_p=*/true,
48366 /*non_constant_p=*/NULL)),
48367 cp_parser_error_occurred (parser)))
48369 finish_omp_structured_block (block);
48370 cp_parser_abort_tentative_parse (parser);
48371 cp_parser_error (parser, "expected id-expression (arguments)");
48372 return false;
48374 unsigned int i;
48375 tree arg;
48376 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
48377 if (arg == omp_priv
48378 || (TREE_CODE (arg) == ADDR_EXPR
48379 && TREE_OPERAND (arg, 0) == omp_priv))
48380 break;
48381 cp_parser_abort_tentative_parse (parser);
48382 if (arg == NULL_TREE)
48383 error ("one of the initializer call arguments should be %<omp_priv%>"
48384 " or %<&omp_priv%>");
48385 initializer = cp_parser_postfix_expression (parser, false, false, false,
48386 false, NULL);
48387 finish_expr_stmt (initializer);
48390 block = finish_omp_structured_block (block);
48391 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
48392 if (processing_template_decl)
48393 block = build_stmt (input_location, EXPR_STMT, block);
48394 add_stmt (block);
48396 if (ctor)
48397 add_decl_expr (omp_orig);
48399 if (!parens.require_close (parser))
48400 return false;
48403 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
48404 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
48405 UNKNOWN_LOCATION);
48407 return true;
48410 /* OpenMP 4.0
48411 #pragma omp declare reduction (reduction-id : typename-list : expression) \
48412 initializer-clause[opt] new-line
48414 initializer-clause:
48415 initializer (omp_priv initializer)
48416 initializer (function-name (argument-list)) */
48418 static void
48419 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
48420 enum pragma_context)
48422 auto_vec<tree> types;
48423 enum tree_code reduc_code = ERROR_MARK;
48424 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
48425 unsigned int i;
48426 cp_token *first_token;
48427 cp_token_cache *cp;
48428 int errs;
48429 void *p;
48431 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
48432 p = obstack_alloc (&declarator_obstack, 0);
48434 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
48435 goto fail;
48437 switch (cp_lexer_peek_token (parser->lexer)->type)
48439 case CPP_PLUS:
48440 reduc_code = PLUS_EXPR;
48441 break;
48442 case CPP_MULT:
48443 reduc_code = MULT_EXPR;
48444 break;
48445 case CPP_MINUS:
48446 reduc_code = MINUS_EXPR;
48447 break;
48448 case CPP_AND:
48449 reduc_code = BIT_AND_EXPR;
48450 break;
48451 case CPP_XOR:
48452 reduc_code = BIT_XOR_EXPR;
48453 break;
48454 case CPP_OR:
48455 reduc_code = BIT_IOR_EXPR;
48456 break;
48457 case CPP_AND_AND:
48458 reduc_code = TRUTH_ANDIF_EXPR;
48459 break;
48460 case CPP_OR_OR:
48461 reduc_code = TRUTH_ORIF_EXPR;
48462 break;
48463 case CPP_NAME:
48464 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
48465 break;
48466 default:
48467 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
48468 "%<|%>, %<&&%>, %<||%> or identifier");
48469 goto fail;
48472 if (reduc_code != ERROR_MARK)
48473 cp_lexer_consume_token (parser->lexer);
48475 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
48476 if (reduc_id == error_mark_node)
48477 goto fail;
48479 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
48480 goto fail;
48482 /* Types may not be defined in declare reduction type list. */
48483 const char *saved_message;
48484 saved_message = parser->type_definition_forbidden_message;
48485 parser->type_definition_forbidden_message
48486 = G_("types may not be defined in declare reduction type list");
48487 bool saved_colon_corrects_to_scope_p;
48488 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
48489 parser->colon_corrects_to_scope_p = false;
48490 bool saved_colon_doesnt_start_class_def_p;
48491 saved_colon_doesnt_start_class_def_p
48492 = parser->colon_doesnt_start_class_def_p;
48493 parser->colon_doesnt_start_class_def_p = true;
48495 while (true)
48497 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
48498 type = cp_parser_type_id (parser);
48499 if (type == error_mark_node)
48501 else if (ARITHMETIC_TYPE_P (type)
48502 && (orig_reduc_id == NULL_TREE
48503 || (TREE_CODE (type) != COMPLEX_TYPE
48504 && (id_equal (orig_reduc_id, "min")
48505 || id_equal (orig_reduc_id, "max")))))
48506 error_at (loc, "predeclared arithmetic type %qT in "
48507 "%<#pragma omp declare reduction%>", type);
48508 else if (FUNC_OR_METHOD_TYPE_P (type)
48509 || TREE_CODE (type) == ARRAY_TYPE)
48510 error_at (loc, "function or array type %qT in "
48511 "%<#pragma omp declare reduction%>", type);
48512 else if (TYPE_REF_P (type))
48513 error_at (loc, "reference type %qT in "
48514 "%<#pragma omp declare reduction%>", type);
48515 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
48516 error_at (loc, "%<const%>, %<volatile%> or %<__restrict%>-qualified "
48517 "type %qT in %<#pragma omp declare reduction%>", type);
48518 else
48519 types.safe_push (type);
48521 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
48522 cp_lexer_consume_token (parser->lexer);
48523 else
48524 break;
48527 /* Restore the saved message. */
48528 parser->type_definition_forbidden_message = saved_message;
48529 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
48530 parser->colon_doesnt_start_class_def_p
48531 = saved_colon_doesnt_start_class_def_p;
48533 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
48534 || types.is_empty ())
48536 fail:
48537 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
48538 goto done;
48541 first_token = cp_lexer_peek_token (parser->lexer);
48542 cp = NULL;
48543 errs = errorcount;
48544 FOR_EACH_VEC_ELT (types, i, type)
48546 tree fntype
48547 = build_function_type_list (void_type_node,
48548 cp_build_reference_type (type, false),
48549 NULL_TREE);
48550 tree this_reduc_id = reduc_id;
48551 if (!dependent_type_p (type))
48552 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
48553 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
48554 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
48555 DECL_ARTIFICIAL (fndecl) = 1;
48556 DECL_EXTERNAL (fndecl) = 1;
48557 DECL_DECLARED_INLINE_P (fndecl) = 1;
48558 DECL_IGNORED_P (fndecl) = 1;
48559 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
48560 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
48561 DECL_ATTRIBUTES (fndecl)
48562 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
48563 DECL_ATTRIBUTES (fndecl));
48564 bool block_scope = false;
48565 if (current_function_decl)
48567 block_scope = true;
48568 DECL_CONTEXT (fndecl) = current_function_decl;
48569 DECL_LOCAL_DECL_P (fndecl) = true;
48572 if (processing_template_decl)
48573 fndecl = push_template_decl (fndecl);
48575 if (block_scope)
48577 if (!processing_template_decl)
48578 pushdecl (fndecl);
48580 else if (current_class_type)
48582 if (cp == NULL)
48584 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
48585 cp_lexer_consume_token (parser->lexer);
48586 cp = cp_token_cache_new (first_token,
48587 cp_lexer_peek_nth_token (parser->lexer,
48588 2));
48590 DECL_STATIC_FUNCTION_P (fndecl) = 1;
48591 finish_member_declaration (fndecl);
48592 DECL_PENDING_INLINE_INFO (fndecl) = cp;
48593 DECL_PENDING_INLINE_P (fndecl) = 1;
48594 vec_safe_push (unparsed_funs_with_definitions, fndecl);
48595 continue;
48597 else
48599 DECL_CONTEXT (fndecl) = current_namespace;
48600 tree d = pushdecl (fndecl);
48601 /* We should never meet a matched duplicate decl. */
48602 gcc_checking_assert (d == error_mark_node || d == fndecl);
48605 tree block = NULL_TREE;
48606 if (!block_scope)
48607 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
48608 else
48609 block = begin_omp_structured_block ();
48610 if (cp)
48612 cp_parser_push_lexer_for_tokens (parser, cp);
48613 parser->lexer->in_pragma = true;
48616 bool ok = cp_parser_omp_declare_reduction_exprs (fndecl, parser);
48618 if (cp)
48619 cp_parser_pop_lexer (parser);
48620 if (!block_scope)
48621 finish_function (/*inline_p=*/false);
48622 else
48624 DECL_CONTEXT (fndecl) = current_function_decl;
48625 if (DECL_TEMPLATE_INFO (fndecl))
48626 DECL_CONTEXT (DECL_TI_TEMPLATE (fndecl)) = current_function_decl;
48628 if (!ok)
48629 goto fail;
48631 if (block_scope)
48633 block = finish_omp_structured_block (block);
48634 if (TREE_CODE (block) == BIND_EXPR)
48635 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
48636 else if (TREE_CODE (block) == STATEMENT_LIST)
48637 DECL_SAVED_TREE (fndecl) = block;
48638 if (processing_template_decl)
48639 add_decl_expr (fndecl);
48642 cp_check_omp_declare_reduction (fndecl);
48643 if (cp == NULL && types.length () > 1)
48644 cp = cp_token_cache_new (first_token,
48645 cp_lexer_peek_nth_token (parser->lexer, 2));
48646 if (errs != errorcount)
48647 break;
48650 cp_parser_require_pragma_eol (parser, pragma_tok);
48652 done:
48653 /* Free any declarators allocated. */
48654 obstack_free (&declarator_obstack, p);
48657 /* OpenMP 4.0
48658 #pragma omp declare simd declare-simd-clauses[optseq] new-line
48659 #pragma omp declare reduction (reduction-id : typename-list : expression) \
48660 initializer-clause[opt] new-line
48661 #pragma omp declare target new-line
48663 OpenMP 5.0
48664 #pragma omp declare variant (identifier) match (context-selector) */
48666 static bool
48667 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
48668 enum pragma_context context)
48670 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
48672 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
48673 const char *p = IDENTIFIER_POINTER (id);
48675 if (strcmp (p, "simd") == 0)
48677 cp_lexer_consume_token (parser->lexer);
48678 cp_parser_omp_declare_simd (parser, pragma_tok,
48679 context, false);
48680 return true;
48682 if (flag_openmp && strcmp (p, "variant") == 0)
48684 cp_lexer_consume_token (parser->lexer);
48685 cp_parser_omp_declare_simd (parser, pragma_tok,
48686 context, true);
48687 return true;
48689 cp_ensure_no_omp_declare_simd (parser);
48690 if (strcmp (p, "reduction") == 0)
48692 cp_lexer_consume_token (parser->lexer);
48693 cp_parser_omp_declare_reduction (parser, pragma_tok,
48694 context);
48695 return false;
48697 if (!flag_openmp) /* flag_openmp_simd */
48699 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
48700 return false;
48702 if (strcmp (p, "target") == 0)
48704 cp_lexer_consume_token (parser->lexer);
48705 cp_parser_omp_declare_target (parser, pragma_tok);
48706 return false;
48709 cp_parser_error (parser, "expected %<simd%>, %<reduction%>, "
48710 "%<target%> or %<variant%>");
48711 cp_parser_require_pragma_eol (parser, pragma_tok);
48712 return false;
48715 /* OpenMP 5.0
48716 #pragma omp requires clauses[optseq] new-line */
48718 static bool
48719 cp_parser_omp_requires (cp_parser *parser, cp_token *pragma_tok)
48721 enum omp_requires new_req = (enum omp_requires) 0;
48723 location_t loc = pragma_tok->location;
48724 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
48726 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
48727 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
48728 cp_lexer_consume_token (parser->lexer);
48730 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
48732 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
48733 const char *p = IDENTIFIER_POINTER (id);
48734 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
48735 enum omp_requires this_req = (enum omp_requires) 0;
48737 if (!strcmp (p, "unified_address"))
48738 this_req = OMP_REQUIRES_UNIFIED_ADDRESS;
48739 else if (!strcmp (p, "unified_shared_memory"))
48740 this_req = OMP_REQUIRES_UNIFIED_SHARED_MEMORY;
48741 else if (!strcmp (p, "dynamic_allocators"))
48742 this_req = OMP_REQUIRES_DYNAMIC_ALLOCATORS;
48743 else if (!strcmp (p, "reverse_offload"))
48744 this_req = OMP_REQUIRES_REVERSE_OFFLOAD;
48745 else if (!strcmp (p, "atomic_default_mem_order"))
48747 cp_lexer_consume_token (parser->lexer);
48749 matching_parens parens;
48750 if (parens.require_open (parser))
48752 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
48754 id = cp_lexer_peek_token (parser->lexer)->u.value;
48755 p = IDENTIFIER_POINTER (id);
48757 if (!strcmp (p, "seq_cst"))
48758 this_req
48759 = (enum omp_requires) OMP_MEMORY_ORDER_SEQ_CST;
48760 else if (!strcmp (p, "relaxed"))
48761 this_req
48762 = (enum omp_requires) OMP_MEMORY_ORDER_RELAXED;
48763 else if (!strcmp (p, "acq_rel"))
48764 this_req
48765 = (enum omp_requires) OMP_MEMORY_ORDER_ACQ_REL;
48767 if (this_req == 0)
48769 error_at (cp_lexer_peek_token (parser->lexer)->location,
48770 "expected %<seq_cst%>, %<relaxed%> or "
48771 "%<acq_rel%>");
48772 switch (cp_lexer_peek_token (parser->lexer)->type)
48774 case CPP_EOF:
48775 case CPP_PRAGMA_EOL:
48776 case CPP_CLOSE_PAREN:
48777 break;
48778 default:
48779 if (cp_lexer_nth_token_is (parser->lexer, 2,
48780 CPP_CLOSE_PAREN))
48781 cp_lexer_consume_token (parser->lexer);
48782 break;
48785 else
48786 cp_lexer_consume_token (parser->lexer);
48788 if (!parens.require_close (parser))
48789 cp_parser_skip_to_closing_parenthesis (parser,
48790 /*recovering=*/true,
48791 /*or_comma=*/false,
48792 /*consume_paren=*/
48793 true);
48795 if (this_req == 0)
48797 cp_parser_require_pragma_eol (parser, pragma_tok);
48798 return false;
48801 p = NULL;
48803 else
48805 error_at (cloc, "expected %<unified_address%>, "
48806 "%<unified_shared_memory%>, "
48807 "%<dynamic_allocators%>, "
48808 "%<reverse_offload%> "
48809 "or %<atomic_default_mem_order%> clause");
48810 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
48811 return false;
48813 if (p)
48814 cp_lexer_consume_token (parser->lexer);
48815 if (this_req)
48817 if ((this_req & ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
48819 if ((this_req & new_req) != 0)
48820 error_at (cloc, "too many %qs clauses", p);
48821 if (this_req != OMP_REQUIRES_DYNAMIC_ALLOCATORS
48822 && (omp_requires_mask & OMP_REQUIRES_TARGET_USED) != 0)
48823 error_at (cloc, "%qs clause used lexically after first "
48824 "target construct or offloading API", p);
48826 else if ((new_req & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
48828 error_at (cloc, "too many %qs clauses",
48829 "atomic_default_mem_order");
48830 this_req = (enum omp_requires) 0;
48832 else if ((omp_requires_mask
48833 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
48835 error_at (cloc, "more than one %<atomic_default_mem_order%>"
48836 " clause in a single compilation unit");
48837 this_req
48838 = (enum omp_requires)
48839 (omp_requires_mask
48840 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER);
48842 else if ((omp_requires_mask
48843 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED) != 0)
48844 error_at (cloc, "%<atomic_default_mem_order%> clause used "
48845 "lexically after first %<atomic%> construct "
48846 "without memory order clause");
48847 new_req = (enum omp_requires) (new_req | this_req);
48848 omp_requires_mask
48849 = (enum omp_requires) (omp_requires_mask | this_req);
48850 continue;
48853 break;
48855 cp_parser_require_pragma_eol (parser, pragma_tok);
48857 if (new_req == 0)
48858 error_at (loc, "%<pragma omp requires%> requires at least one clause");
48859 return false;
48863 /* OpenMP 5.1:
48864 #pragma omp nothing new-line */
48866 static void
48867 cp_parser_omp_nothing (cp_parser *parser, cp_token *pragma_tok)
48869 cp_parser_require_pragma_eol (parser, pragma_tok);
48873 /* OpenMP 5.1
48874 #pragma omp error clauses[optseq] new-line */
48876 static bool
48877 cp_parser_omp_error (cp_parser *parser, cp_token *pragma_tok,
48878 enum pragma_context context)
48880 int at_compilation = -1;
48881 int severity_fatal = -1;
48882 tree message = NULL_TREE;
48883 bool bad = false;
48884 location_t loc = pragma_tok->location;
48886 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
48888 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
48889 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
48890 cp_lexer_consume_token (parser->lexer);
48892 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
48893 break;
48895 const char *p
48896 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
48897 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
48898 static const char *args[] = {
48899 "execution", "compilation", "warning", "fatal"
48901 int *v = NULL;
48902 int idx = 0, n = -1;
48903 tree m = NULL_TREE;
48905 if (!strcmp (p, "at"))
48906 v = &at_compilation;
48907 else if (!strcmp (p, "severity"))
48909 v = &severity_fatal;
48910 idx += 2;
48912 else if (strcmp (p, "message"))
48914 error_at (cloc,
48915 "expected %<at%>, %<severity%> or %<message%> clause");
48916 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
48917 return false;
48920 cp_lexer_consume_token (parser->lexer);
48922 matching_parens parens;
48923 if (parens.require_open (parser))
48925 if (v == NULL)
48927 m = cp_parser_assignment_expression (parser);
48928 if (type_dependent_expression_p (m))
48929 m = build1 (IMPLICIT_CONV_EXPR, const_string_type_node, m);
48930 else
48931 m = perform_implicit_conversion_flags (const_string_type_node, m,
48932 tf_warning_or_error,
48933 LOOKUP_NORMAL);
48935 else
48937 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
48939 tree val = cp_lexer_peek_token (parser->lexer)->u.value;
48940 const char *q = IDENTIFIER_POINTER (val);
48942 if (!strcmp (q, args[idx]))
48943 n = 0;
48944 else if (!strcmp (q, args[idx + 1]))
48945 n = 1;
48947 if (n == -1)
48949 error_at (cp_lexer_peek_token (parser->lexer)->location,
48950 "expected %qs or %qs", args[idx], args[idx + 1]);
48951 bad = true;
48952 switch (cp_lexer_peek_token (parser->lexer)->type)
48954 case CPP_EOF:
48955 case CPP_PRAGMA_EOL:
48956 case CPP_CLOSE_PAREN:
48957 break;
48958 default:
48959 if (cp_lexer_nth_token_is (parser->lexer, 2,
48960 CPP_CLOSE_PAREN))
48961 cp_lexer_consume_token (parser->lexer);
48962 break;
48965 else
48966 cp_lexer_consume_token (parser->lexer);
48969 if (!parens.require_close (parser))
48970 cp_parser_skip_to_closing_parenthesis (parser,
48971 /*recovering=*/true,
48972 /*or_comma=*/false,
48973 /*consume_paren=*/
48974 true);
48976 if (v == NULL)
48978 if (message)
48980 error_at (cloc, "too many %qs clauses", p);
48981 bad = true;
48983 else
48984 message = m;
48986 else if (n != -1)
48988 if (*v != -1)
48990 error_at (cloc, "too many %qs clauses", p);
48991 bad = true;
48993 else
48994 *v = n;
48997 else
48998 bad = true;
49000 cp_parser_require_pragma_eol (parser, pragma_tok);
49001 if (bad)
49002 return true;
49004 if (at_compilation == -1)
49005 at_compilation = 1;
49006 if (severity_fatal == -1)
49007 severity_fatal = 1;
49008 if (!at_compilation)
49010 if (context != pragma_compound)
49012 error_at (loc, "%<#pragma omp error%> with %<at(execution)%> clause "
49013 "may only be used in compound statements");
49014 return true;
49016 tree fndecl
49017 = builtin_decl_explicit (severity_fatal ? BUILT_IN_GOMP_ERROR
49018 : BUILT_IN_GOMP_WARNING);
49019 if (!message)
49020 message = build_zero_cst (const_string_type_node);
49021 tree stmt = build_call_expr_loc (loc, fndecl, 2, message,
49022 build_all_ones_cst (size_type_node));
49023 add_stmt (stmt);
49024 return true;
49027 if (in_discarded_stmt)
49028 return false;
49030 const char *msg = NULL;
49031 if (message)
49033 msg = c_getstr (fold_for_warn (message));
49034 if (msg == NULL)
49035 msg = _("<message unknown at compile time>");
49037 if (msg)
49038 emit_diagnostic (severity_fatal ? DK_ERROR : DK_WARNING, loc, 0,
49039 "%<pragma omp error%> encountered: %s", msg);
49040 else
49041 emit_diagnostic (severity_fatal ? DK_ERROR : DK_WARNING, loc, 0,
49042 "%<pragma omp error%> encountered");
49043 return false;
49046 /* OpenMP 4.5:
49047 #pragma omp taskloop taskloop-clause[optseq] new-line
49048 for-loop
49050 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
49051 for-loop */
49053 #define OMP_TASKLOOP_CLAUSE_MASK \
49054 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
49055 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
49056 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
49057 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
49058 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
49059 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
49060 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
49061 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
49062 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
49063 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
49064 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
49065 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
49066 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
49067 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
49068 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
49069 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
49070 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
49072 static tree
49073 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
49074 char *p_name, omp_clause_mask mask, tree *cclauses,
49075 bool *if_p)
49077 tree clauses, sb, ret;
49078 unsigned int save;
49079 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
49081 strcat (p_name, " taskloop");
49082 mask |= OMP_TASKLOOP_CLAUSE_MASK;
49083 /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
49084 clause. */
49085 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)) != 0)
49086 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION);
49088 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
49090 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
49091 const char *p = IDENTIFIER_POINTER (id);
49093 if (strcmp (p, "simd") == 0)
49095 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
49096 if (cclauses == NULL)
49097 cclauses = cclauses_buf;
49099 cp_lexer_consume_token (parser->lexer);
49100 if (!flag_openmp) /* flag_openmp_simd */
49101 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
49102 cclauses, if_p);
49103 sb = begin_omp_structured_block ();
49104 save = cp_parser_begin_omp_structured_block (parser);
49105 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
49106 cclauses, if_p);
49107 cp_parser_end_omp_structured_block (parser, save);
49108 tree body = finish_omp_structured_block (sb);
49109 if (ret == NULL)
49110 return ret;
49111 ret = make_node (OMP_TASKLOOP);
49112 TREE_TYPE (ret) = void_type_node;
49113 OMP_FOR_BODY (ret) = body;
49114 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
49115 SET_EXPR_LOCATION (ret, loc);
49116 add_stmt (ret);
49117 return ret;
49120 if (!flag_openmp) /* flag_openmp_simd */
49122 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
49123 return NULL_TREE;
49126 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
49127 cclauses == NULL);
49128 if (cclauses)
49130 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
49131 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
49134 keep_next_level (true);
49135 sb = begin_omp_structured_block ();
49136 save = cp_parser_begin_omp_structured_block (parser);
49138 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
49139 if_p);
49141 cp_parser_end_omp_structured_block (parser, save);
49142 add_stmt (finish_omp_structured_block (sb));
49144 return ret;
49148 /* OpenACC 2.0:
49149 # pragma acc routine oacc-routine-clause[optseq] new-line
49150 function-definition
49152 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
49155 #define OACC_ROUTINE_CLAUSE_MASK \
49156 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
49157 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
49158 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
49159 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
49160 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NOHOST) )
49162 /* Parse the OpenACC routine pragma. This has an optional '( name )'
49163 component, which must resolve to a declared namespace-scope
49164 function. The clauses are either processed directly (for a named
49165 function), or defered until the immediatley following declaration
49166 is parsed. */
49168 static void
49169 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
49170 enum pragma_context context)
49172 gcc_checking_assert (context == pragma_external);
49173 /* The checking for "another pragma following this one" in the "no optional
49174 '( name )'" case makes sure that we dont re-enter. */
49175 gcc_checking_assert (parser->oacc_routine == NULL);
49177 cp_oacc_routine_data data;
49178 data.error_seen = false;
49179 data.fndecl_seen = false;
49180 data.tokens = vNULL;
49181 data.clauses = NULL_TREE;
49182 data.loc = pragma_tok->location;
49183 /* It is safe to take the address of a local variable; it will only be
49184 used while this scope is live. */
49185 parser->oacc_routine = &data;
49187 /* Look for optional '( name )'. */
49188 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
49190 matching_parens parens;
49191 parens.consume_open (parser); /* '(' */
49193 /* We parse the name as an id-expression. If it resolves to
49194 anything other than a non-overloaded function at namespace
49195 scope, it's an error. */
49196 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
49197 tree name = cp_parser_id_expression (parser,
49198 /*template_keyword_p=*/false,
49199 /*check_dependency_p=*/false,
49200 /*template_p=*/NULL,
49201 /*declarator_p=*/false,
49202 /*optional_p=*/false);
49203 tree decl = (identifier_p (name)
49204 ? cp_parser_lookup_name_simple (parser, name, name_loc)
49205 : name);
49206 if (name != error_mark_node && decl == error_mark_node)
49207 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
49209 if (decl == error_mark_node
49210 || !parens.require_close (parser))
49212 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
49213 parser->oacc_routine = NULL;
49214 return;
49217 data.clauses
49218 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
49219 "#pragma acc routine",
49220 cp_lexer_peek_token (parser->lexer));
49221 /* The clauses are in reverse order; fix that to make later diagnostic
49222 emission easier. */
49223 data.clauses = nreverse (data.clauses);
49225 if (decl && is_overloaded_fn (decl)
49226 && (TREE_CODE (decl) != FUNCTION_DECL
49227 || DECL_FUNCTION_TEMPLATE_P (decl)))
49229 error_at (name_loc,
49230 "%<#pragma acc routine%> names a set of overloads");
49231 parser->oacc_routine = NULL;
49232 return;
49235 /* Perhaps we should use the same rule as declarations in different
49236 namespaces? */
49237 if (!DECL_NAMESPACE_SCOPE_P (decl))
49239 error_at (name_loc,
49240 "%qD does not refer to a namespace scope function", decl);
49241 parser->oacc_routine = NULL;
49242 return;
49245 if (TREE_CODE (decl) != FUNCTION_DECL)
49247 error_at (name_loc, "%qD does not refer to a function", decl);
49248 parser->oacc_routine = NULL;
49249 return;
49252 cp_finalize_oacc_routine (parser, decl, false);
49253 parser->oacc_routine = NULL;
49255 else /* No optional '( name )'. */
49257 /* Store away all pragma tokens. */
49258 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
49259 cp_lexer_consume_token (parser->lexer);
49260 cp_parser_require_pragma_eol (parser, pragma_tok);
49261 struct cp_token_cache *cp
49262 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
49263 parser->oacc_routine->tokens.safe_push (cp);
49265 /* Emit a helpful diagnostic if there's another pragma following this
49266 one. */
49267 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
49269 cp_ensure_no_oacc_routine (parser);
49270 data.tokens.release ();
49271 /* ..., and then just keep going. */
49272 return;
49275 /* We only have to consider the pragma_external case here. */
49276 cp_parser_declaration (parser, NULL_TREE);
49277 if (parser->oacc_routine
49278 && !parser->oacc_routine->fndecl_seen)
49279 cp_ensure_no_oacc_routine (parser);
49280 else
49281 parser->oacc_routine = NULL;
49282 data.tokens.release ();
49286 /* Finalize #pragma acc routine clauses after direct declarator has
49287 been parsed. */
49289 static tree
49290 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
49292 struct cp_token_cache *ce;
49293 cp_oacc_routine_data *data = parser->oacc_routine;
49295 if (!data->error_seen && data->fndecl_seen)
49297 error_at (data->loc,
49298 "%<#pragma acc routine%> not immediately followed by "
49299 "a single function declaration or definition");
49300 data->error_seen = true;
49302 if (data->error_seen)
49303 return attrs;
49305 gcc_checking_assert (data->tokens.length () == 1);
49306 ce = data->tokens[0];
49308 cp_parser_push_lexer_for_tokens (parser, ce);
49309 parser->lexer->in_pragma = true;
49310 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
49312 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
49313 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
49314 parser->oacc_routine->clauses
49315 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
49316 "#pragma acc routine", pragma_tok);
49317 /* The clauses are in reverse order; fix that to make later diagnostic
49318 emission easier. */
49319 parser->oacc_routine->clauses = nreverse (parser->oacc_routine->clauses);
49320 cp_parser_pop_lexer (parser);
49321 /* Later, cp_finalize_oacc_routine will process the clauses. */
49322 parser->oacc_routine->fndecl_seen = true;
49324 return attrs;
49327 /* Apply any saved OpenACC routine clauses to a just-parsed
49328 declaration. */
49330 static void
49331 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
49333 if (UNLIKELY (parser->oacc_routine != NULL))
49335 /* Keep going if we're in error reporting mode. */
49336 if (parser->oacc_routine->error_seen
49337 || fndecl == error_mark_node)
49338 return;
49340 if (TREE_CODE (fndecl) != FUNCTION_DECL)
49342 if (parser->oacc_routine->fndecl_seen)
49344 error_at (parser->oacc_routine->loc,
49345 "%<#pragma acc routine%> not immediately followed by"
49346 " a single function declaration or definition");
49347 parser->oacc_routine = NULL;
49348 return;
49351 cp_ensure_no_oacc_routine (parser);
49352 return;
49355 int compatible
49356 = oacc_verify_routine_clauses (fndecl, &parser->oacc_routine->clauses,
49357 parser->oacc_routine->loc,
49358 "#pragma acc routine");
49359 if (compatible < 0)
49361 parser->oacc_routine = NULL;
49362 return;
49364 if (compatible > 0)
49367 else
49369 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
49371 error_at (parser->oacc_routine->loc,
49372 TREE_USED (fndecl)
49373 ? G_("%<#pragma acc routine%> must be applied before"
49374 " use")
49375 : G_("%<#pragma acc routine%> must be applied before"
49376 " definition"));
49377 parser->oacc_routine = NULL;
49378 return;
49381 /* Set the routine's level of parallelism. */
49382 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
49383 oacc_replace_fn_attrib (fndecl, dims);
49385 /* Add an "omp declare target" attribute. */
49386 DECL_ATTRIBUTES (fndecl)
49387 = tree_cons (get_identifier ("omp declare target"),
49388 parser->oacc_routine->clauses,
49389 DECL_ATTRIBUTES (fndecl));
49394 /* Main entry point to OpenMP statement pragmas. */
49396 static void
49397 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
49399 tree stmt;
49400 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
49401 omp_clause_mask mask (0);
49403 switch (cp_parser_pragma_kind (pragma_tok))
49405 case PRAGMA_OACC_ATOMIC:
49406 cp_parser_omp_atomic (parser, pragma_tok, true);
49407 return;
49408 case PRAGMA_OACC_CACHE:
49409 stmt = cp_parser_oacc_cache (parser, pragma_tok);
49410 break;
49411 case PRAGMA_OACC_DATA:
49412 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
49413 break;
49414 case PRAGMA_OACC_ENTER_DATA:
49415 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
49416 break;
49417 case PRAGMA_OACC_EXIT_DATA:
49418 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
49419 break;
49420 case PRAGMA_OACC_HOST_DATA:
49421 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
49422 break;
49423 case PRAGMA_OACC_KERNELS:
49424 case PRAGMA_OACC_PARALLEL:
49425 case PRAGMA_OACC_SERIAL:
49426 strcpy (p_name, "#pragma acc");
49427 stmt = cp_parser_oacc_compute (parser, pragma_tok, p_name, if_p);
49428 break;
49429 case PRAGMA_OACC_LOOP:
49430 strcpy (p_name, "#pragma acc");
49431 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
49432 if_p);
49433 break;
49434 case PRAGMA_OACC_UPDATE:
49435 stmt = cp_parser_oacc_update (parser, pragma_tok);
49436 break;
49437 case PRAGMA_OACC_WAIT:
49438 stmt = cp_parser_oacc_wait (parser, pragma_tok);
49439 break;
49440 case PRAGMA_OMP_ALLOCATE:
49441 cp_parser_omp_allocate (parser, pragma_tok);
49442 return;
49443 case PRAGMA_OMP_ATOMIC:
49444 cp_parser_omp_atomic (parser, pragma_tok, false);
49445 return;
49446 case PRAGMA_OMP_CRITICAL:
49447 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
49448 break;
49449 case PRAGMA_OMP_DISTRIBUTE:
49450 strcpy (p_name, "#pragma omp");
49451 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
49452 if_p);
49453 break;
49454 case PRAGMA_OMP_FOR:
49455 strcpy (p_name, "#pragma omp");
49456 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
49457 if_p);
49458 break;
49459 case PRAGMA_OMP_LOOP:
49460 strcpy (p_name, "#pragma omp");
49461 stmt = cp_parser_omp_loop (parser, pragma_tok, p_name, mask, NULL,
49462 if_p);
49463 break;
49464 case PRAGMA_OMP_MASKED:
49465 strcpy (p_name, "#pragma omp");
49466 stmt = cp_parser_omp_masked (parser, pragma_tok, p_name, mask, NULL,
49467 if_p);
49468 break;
49469 case PRAGMA_OMP_MASTER:
49470 strcpy (p_name, "#pragma omp");
49471 stmt = cp_parser_omp_master (parser, pragma_tok, p_name, mask, NULL,
49472 if_p);
49473 break;
49474 case PRAGMA_OMP_PARALLEL:
49475 strcpy (p_name, "#pragma omp");
49476 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
49477 if_p);
49478 break;
49479 case PRAGMA_OMP_SCOPE:
49480 stmt = cp_parser_omp_scope (parser, pragma_tok, if_p);
49481 break;
49482 case PRAGMA_OMP_SECTIONS:
49483 strcpy (p_name, "#pragma omp");
49484 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
49485 break;
49486 case PRAGMA_OMP_SIMD:
49487 strcpy (p_name, "#pragma omp");
49488 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
49489 if_p);
49490 break;
49491 case PRAGMA_OMP_SINGLE:
49492 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
49493 break;
49494 case PRAGMA_OMP_TASK:
49495 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
49496 break;
49497 case PRAGMA_OMP_TASKGROUP:
49498 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
49499 break;
49500 case PRAGMA_OMP_TASKLOOP:
49501 strcpy (p_name, "#pragma omp");
49502 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
49503 if_p);
49504 break;
49505 case PRAGMA_OMP_TEAMS:
49506 strcpy (p_name, "#pragma omp");
49507 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
49508 if_p);
49509 break;
49510 case PRAGMA_OMP_ASSUME:
49511 cp_parser_omp_assume (parser, pragma_tok, if_p);
49512 return;
49513 default:
49514 gcc_unreachable ();
49517 protected_set_expr_location (stmt, pragma_tok->location);
49520 /* Transactional Memory parsing routines. */
49522 /* Parse a transaction attribute.
49524 txn-attribute:
49525 attribute
49526 [ [ identifier ] ]
49528 We use this instead of cp_parser_attributes_opt for transactions to avoid
49529 the pedwarn in C++98 mode. */
49531 static tree
49532 cp_parser_txn_attribute_opt (cp_parser *parser)
49534 cp_token *token;
49535 tree attr_name, attr = NULL;
49537 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
49538 return cp_parser_attributes_opt (parser);
49540 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
49541 return NULL_TREE;
49542 cp_lexer_consume_token (parser->lexer);
49543 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
49544 goto error1;
49546 token = cp_lexer_peek_token (parser->lexer);
49547 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
49549 token = cp_lexer_consume_token (parser->lexer);
49551 attr_name = (token->type == CPP_KEYWORD
49552 /* For keywords, use the canonical spelling,
49553 not the parsed identifier. */
49554 ? ridpointers[(int) token->keyword]
49555 : token->u.value);
49556 attr = build_tree_list (attr_name, NULL_TREE);
49558 else
49559 cp_parser_error (parser, "expected identifier");
49561 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
49562 error1:
49563 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
49564 return attr;
49567 /* Parse a __transaction_atomic or __transaction_relaxed statement.
49569 transaction-statement:
49570 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
49571 compound-statement
49572 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
49575 static tree
49576 cp_parser_transaction (cp_parser *parser, cp_token *token)
49578 unsigned char old_in = parser->in_transaction;
49579 unsigned char this_in = 1, new_in;
49580 enum rid keyword = token->keyword;
49581 tree stmt, attrs, noex;
49583 cp_lexer_consume_token (parser->lexer);
49585 if (keyword == RID_TRANSACTION_RELAXED
49586 || keyword == RID_SYNCHRONIZED)
49587 this_in |= TM_STMT_ATTR_RELAXED;
49588 else
49590 attrs = cp_parser_txn_attribute_opt (parser);
49591 if (attrs)
49592 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
49595 /* Parse a noexcept specification. */
49596 if (keyword == RID_ATOMIC_NOEXCEPT)
49597 noex = boolean_true_node;
49598 else if (keyword == RID_ATOMIC_CANCEL)
49600 /* cancel-and-throw is unimplemented. */
49601 sorry ("%<atomic_cancel%>");
49602 noex = NULL_TREE;
49604 else
49605 noex = cp_parser_noexcept_specification_opt (parser,
49606 CP_PARSER_FLAGS_NONE,
49607 /*require_constexpr=*/true,
49608 /*consumed_expr=*/NULL,
49609 /*return_cond=*/true);
49611 /* Keep track if we're in the lexical scope of an outer transaction. */
49612 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
49614 stmt = begin_transaction_stmt (token->location, NULL, this_in);
49616 parser->in_transaction = new_in;
49617 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
49618 parser->in_transaction = old_in;
49620 finish_transaction_stmt (stmt, NULL, this_in, noex);
49622 return stmt;
49625 /* Parse a __transaction_atomic or __transaction_relaxed expression.
49627 transaction-expression:
49628 __transaction_atomic txn-noexcept-spec[opt] ( expression )
49629 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
49632 static tree
49633 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
49635 unsigned char old_in = parser->in_transaction;
49636 unsigned char this_in = 1;
49637 cp_token *token;
49638 tree expr, noex;
49639 bool noex_expr;
49640 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
49642 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
49643 || keyword == RID_TRANSACTION_RELAXED);
49645 if (!flag_tm)
49646 error_at (loc,
49647 keyword == RID_TRANSACTION_RELAXED
49648 ? G_("%<__transaction_relaxed%> without transactional memory "
49649 "support enabled")
49650 : G_("%<__transaction_atomic%> without transactional memory "
49651 "support enabled"));
49653 token = cp_parser_require_keyword (parser, keyword,
49654 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
49655 : RT_TRANSACTION_RELAXED));
49656 gcc_assert (token != NULL);
49658 if (keyword == RID_TRANSACTION_RELAXED)
49659 this_in |= TM_STMT_ATTR_RELAXED;
49661 /* Set this early. This might mean that we allow transaction_cancel in
49662 an expression that we find out later actually has to be a constexpr.
49663 However, we expect that cxx_constant_value will be able to deal with
49664 this; also, if the noexcept has no constexpr, then what we parse next
49665 really is a transaction's body. */
49666 parser->in_transaction = this_in;
49668 /* Parse a noexcept specification. */
49669 noex = cp_parser_noexcept_specification_opt (parser,
49670 CP_PARSER_FLAGS_NONE,
49671 /*require_constexpr=*/false,
49672 &noex_expr,
49673 /*return_cond=*/true);
49675 if (!noex || !noex_expr
49676 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
49678 matching_parens parens;
49679 parens.require_open (parser);
49681 expr = cp_parser_expression (parser);
49682 expr = finish_parenthesized_expr (expr);
49684 parens.require_close (parser);
49686 else
49688 /* The only expression that is available got parsed for the noexcept
49689 already. noexcept is true then. */
49690 expr = noex;
49691 noex = boolean_true_node;
49694 expr = build_transaction_expr (token->location, expr, this_in, noex);
49695 parser->in_transaction = old_in;
49697 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
49698 return error_mark_node;
49700 return (flag_tm ? expr : error_mark_node);
49703 /* Parse a function-transaction-block.
49705 function-transaction-block:
49706 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
49707 function-body
49708 __transaction_atomic txn-attribute[opt] function-try-block
49709 __transaction_relaxed ctor-initializer[opt] function-body
49710 __transaction_relaxed function-try-block
49713 static void
49714 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
49716 unsigned char old_in = parser->in_transaction;
49717 unsigned char new_in = 1;
49718 tree compound_stmt, stmt, attrs;
49719 cp_token *token;
49721 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
49722 || keyword == RID_TRANSACTION_RELAXED);
49723 token = cp_parser_require_keyword (parser, keyword,
49724 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
49725 : RT_TRANSACTION_RELAXED));
49726 gcc_assert (token != NULL);
49728 if (keyword == RID_TRANSACTION_RELAXED)
49729 new_in |= TM_STMT_ATTR_RELAXED;
49730 else
49732 attrs = cp_parser_txn_attribute_opt (parser);
49733 if (attrs)
49734 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
49737 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
49739 parser->in_transaction = new_in;
49741 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
49742 cp_parser_function_try_block (parser);
49743 else
49744 cp_parser_ctor_initializer_opt_and_function_body
49745 (parser, /*in_function_try_block=*/false);
49747 parser->in_transaction = old_in;
49749 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
49752 /* Parse a __transaction_cancel statement.
49754 cancel-statement:
49755 __transaction_cancel txn-attribute[opt] ;
49756 __transaction_cancel txn-attribute[opt] throw-expression ;
49758 ??? Cancel and throw is not yet implemented. */
49760 static tree
49761 cp_parser_transaction_cancel (cp_parser *parser)
49763 cp_token *token;
49764 bool is_outer = false;
49765 tree stmt, attrs;
49767 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
49768 RT_TRANSACTION_CANCEL);
49769 gcc_assert (token != NULL);
49771 attrs = cp_parser_txn_attribute_opt (parser);
49772 if (attrs)
49773 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
49775 /* ??? Parse cancel-and-throw here. */
49777 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
49779 if (!flag_tm)
49781 error_at (token->location, "%<__transaction_cancel%> without "
49782 "transactional memory support enabled");
49783 return error_mark_node;
49785 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
49787 error_at (token->location, "%<__transaction_cancel%> within a "
49788 "%<__transaction_relaxed%>");
49789 return error_mark_node;
49791 else if (is_outer)
49793 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
49794 && !is_tm_may_cancel_outer (current_function_decl))
49796 error_at (token->location, "outer %<__transaction_cancel%> not "
49797 "within outer %<__transaction_atomic%>");
49798 error_at (token->location,
49799 " or a %<transaction_may_cancel_outer%> function");
49800 return error_mark_node;
49803 else if (parser->in_transaction == 0)
49805 error_at (token->location, "%<__transaction_cancel%> not within "
49806 "%<__transaction_atomic%>");
49807 return error_mark_node;
49810 stmt = build_tm_abort_call (token->location, is_outer);
49811 add_stmt (stmt);
49813 return stmt;
49817 /* Special handling for the first token or line in the file. The first
49818 thing in the file might be #pragma GCC pch_preprocess, which loads a
49819 PCH file, which is a GC collection point. So we need to handle this
49820 first pragma without benefit of an existing lexer structure.
49822 Always returns one token to the caller in *FIRST_TOKEN. This is
49823 either the true first token of the file, or the first token after
49824 the initial pragma. */
49826 static void
49827 cp_parser_initial_pragma (cp_token *first_token)
49829 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
49830 return;
49832 cp_lexer_get_preprocessor_token (0, first_token);
49834 tree name = NULL;
49835 if (first_token->type == CPP_STRING)
49837 name = first_token->u.value;
49839 cp_lexer_get_preprocessor_token (0, first_token);
49842 /* Skip to the end of the pragma. */
49843 if (first_token->type != CPP_PRAGMA_EOL)
49845 error_at (first_token->location,
49846 "malformed %<#pragma GCC pch_preprocess%>");
49848 cp_lexer_get_preprocessor_token (0, first_token);
49849 while (first_token->type != CPP_PRAGMA_EOL);
49852 /* Now actually load the PCH file. */
49853 if (name)
49854 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
49856 /* Read one more token to return to our caller. We have to do this
49857 after reading the PCH file in, since its pointers have to be
49858 live. */
49859 cp_lexer_get_preprocessor_token (0, first_token);
49862 /* Parse a pragma GCC ivdep. */
49864 static bool
49865 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
49867 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
49868 return true;
49871 /* Parse a pragma GCC unroll. */
49873 static unsigned short
49874 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
49876 location_t location = cp_lexer_peek_token (parser->lexer)->location;
49877 tree expr = cp_parser_constant_expression (parser);
49878 unsigned short unroll;
49879 expr = maybe_constant_value (expr);
49880 HOST_WIDE_INT lunroll = 0;
49881 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
49882 || TREE_CODE (expr) != INTEGER_CST
49883 || (lunroll = tree_to_shwi (expr)) < 0
49884 || lunroll >= USHRT_MAX)
49886 error_at (location, "%<#pragma GCC unroll%> requires an"
49887 " assignment-expression that evaluates to a non-negative"
49888 " integral constant less than %u", USHRT_MAX);
49889 unroll = 0;
49891 else
49893 unroll = (unsigned short)lunroll;
49894 if (unroll == 0)
49895 unroll = 1;
49897 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
49898 return unroll;
49901 /* Parse a pragma GCC novector. */
49903 static bool
49904 cp_parser_pragma_novector (cp_parser *parser, cp_token *pragma_tok)
49906 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
49907 return true;
49910 /* Normal parsing of a pragma token. Here we can (and must) use the
49911 regular lexer. */
49913 static bool
49914 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
49916 cp_token *pragma_tok;
49917 unsigned int id;
49918 tree stmt;
49919 bool ret = false;
49921 pragma_tok = cp_lexer_consume_token (parser->lexer);
49922 gcc_assert (pragma_tok->type == CPP_PRAGMA);
49923 parser->lexer->in_pragma = true;
49925 id = cp_parser_pragma_kind (pragma_tok);
49926 if (parser->omp_for_parse_state
49927 && parser->omp_for_parse_state->in_intervening_code
49928 && id >= PRAGMA_OMP__START_
49929 && id <= PRAGMA_OMP__LAST_)
49931 error_at (pragma_tok->location,
49932 "intervening code must not contain OpenMP directives");
49933 parser->omp_for_parse_state->fail = true;
49934 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
49935 return false;
49937 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
49938 cp_ensure_no_omp_declare_simd (parser);
49939 switch (id)
49941 case PRAGMA_GCC_PCH_PREPROCESS:
49942 error_at (pragma_tok->location,
49943 "%<#pragma GCC pch_preprocess%> must be first");
49944 break;
49946 case PRAGMA_OMP_BARRIER:
49947 switch (context)
49949 case pragma_compound:
49950 cp_parser_omp_barrier (parser, pragma_tok);
49951 return false;
49952 case pragma_stmt:
49953 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
49954 "used in compound statements", "omp barrier");
49955 ret = true;
49956 break;
49957 default:
49958 goto bad_stmt;
49960 break;
49962 case PRAGMA_OMP_DEPOBJ:
49963 switch (context)
49965 case pragma_compound:
49966 cp_parser_omp_depobj (parser, pragma_tok);
49967 return false;
49968 case pragma_stmt:
49969 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
49970 "used in compound statements", "omp depobj");
49971 ret = true;
49972 break;
49973 default:
49974 goto bad_stmt;
49976 break;
49978 case PRAGMA_OMP_FLUSH:
49979 switch (context)
49981 case pragma_compound:
49982 cp_parser_omp_flush (parser, pragma_tok);
49983 return false;
49984 case pragma_stmt:
49985 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
49986 "used in compound statements", "omp flush");
49987 ret = true;
49988 break;
49989 default:
49990 goto bad_stmt;
49992 break;
49994 case PRAGMA_OMP_TASKWAIT:
49995 switch (context)
49997 case pragma_compound:
49998 cp_parser_omp_taskwait (parser, pragma_tok);
49999 return false;
50000 case pragma_stmt:
50001 error_at (pragma_tok->location,
50002 "%<#pragma %s%> may only be used in compound statements",
50003 "omp taskwait");
50004 ret = true;
50005 break;
50006 default:
50007 goto bad_stmt;
50009 break;
50011 case PRAGMA_OMP_TASKYIELD:
50012 switch (context)
50014 case pragma_compound:
50015 cp_parser_omp_taskyield (parser, pragma_tok);
50016 return false;
50017 case pragma_stmt:
50018 error_at (pragma_tok->location,
50019 "%<#pragma %s%> may only be used in compound statements",
50020 "omp taskyield");
50021 ret = true;
50022 break;
50023 default:
50024 goto bad_stmt;
50026 break;
50028 case PRAGMA_OMP_CANCEL:
50029 switch (context)
50031 case pragma_compound:
50032 cp_parser_omp_cancel (parser, pragma_tok);
50033 return false;
50034 case pragma_stmt:
50035 error_at (pragma_tok->location,
50036 "%<#pragma %s%> may only be used in compound statements",
50037 "omp cancel");
50038 ret = true;
50039 break;
50040 default:
50041 goto bad_stmt;
50043 break;
50045 case PRAGMA_OMP_CANCELLATION_POINT:
50046 return cp_parser_omp_cancellation_point (parser, pragma_tok, context);
50048 case PRAGMA_OMP_THREADPRIVATE:
50049 cp_parser_omp_threadprivate (parser, pragma_tok);
50050 return false;
50052 case PRAGMA_OMP_DECLARE:
50053 return cp_parser_omp_declare (parser, pragma_tok, context);
50055 case PRAGMA_OACC_DECLARE:
50056 cp_parser_oacc_declare (parser, pragma_tok);
50057 return false;
50059 case PRAGMA_OACC_ENTER_DATA:
50060 if (context == pragma_stmt)
50062 error_at (pragma_tok->location,
50063 "%<#pragma %s%> may only be used in compound statements",
50064 "acc enter data");
50065 ret = true;
50066 break;
50068 else if (context != pragma_compound)
50069 goto bad_stmt;
50070 cp_parser_omp_construct (parser, pragma_tok, if_p);
50071 return true;
50073 case PRAGMA_OACC_EXIT_DATA:
50074 if (context == pragma_stmt)
50076 error_at (pragma_tok->location,
50077 "%<#pragma %s%> may only be used in compound statements",
50078 "acc exit data");
50079 ret = true;
50080 break;
50082 else if (context != pragma_compound)
50083 goto bad_stmt;
50084 cp_parser_omp_construct (parser, pragma_tok, if_p);
50085 return true;
50087 case PRAGMA_OACC_ROUTINE:
50088 if (context != pragma_external)
50090 error_at (pragma_tok->location,
50091 "%<#pragma acc routine%> must be at file scope");
50092 ret = true;
50093 break;
50095 cp_parser_oacc_routine (parser, pragma_tok, context);
50096 return false;
50098 case PRAGMA_OACC_UPDATE:
50099 if (context == pragma_stmt)
50101 error_at (pragma_tok->location,
50102 "%<#pragma %s%> may only be used in compound statements",
50103 "acc update");
50104 ret = true;
50105 break;
50107 else if (context != pragma_compound)
50108 goto bad_stmt;
50109 cp_parser_omp_construct (parser, pragma_tok, if_p);
50110 return true;
50112 case PRAGMA_OACC_WAIT:
50113 if (context == pragma_stmt)
50115 error_at (pragma_tok->location,
50116 "%<#pragma %s%> may only be used in compound statements",
50117 "acc wait");
50118 ret = true;
50119 break;
50121 else if (context != pragma_compound)
50122 goto bad_stmt;
50123 cp_parser_omp_construct (parser, pragma_tok, if_p);
50124 return true;
50125 case PRAGMA_OMP_ALLOCATE:
50126 cp_parser_omp_allocate (parser, pragma_tok);
50127 return false;
50128 case PRAGMA_OACC_ATOMIC:
50129 case PRAGMA_OACC_CACHE:
50130 case PRAGMA_OACC_DATA:
50131 case PRAGMA_OACC_HOST_DATA:
50132 case PRAGMA_OACC_KERNELS:
50133 case PRAGMA_OACC_LOOP:
50134 case PRAGMA_OACC_PARALLEL:
50135 case PRAGMA_OACC_SERIAL:
50136 case PRAGMA_OMP_ASSUME:
50137 case PRAGMA_OMP_ATOMIC:
50138 case PRAGMA_OMP_CRITICAL:
50139 case PRAGMA_OMP_DISTRIBUTE:
50140 case PRAGMA_OMP_FOR:
50141 case PRAGMA_OMP_LOOP:
50142 case PRAGMA_OMP_MASKED:
50143 case PRAGMA_OMP_MASTER:
50144 case PRAGMA_OMP_PARALLEL:
50145 case PRAGMA_OMP_SCOPE:
50146 case PRAGMA_OMP_SECTIONS:
50147 case PRAGMA_OMP_SIMD:
50148 case PRAGMA_OMP_SINGLE:
50149 case PRAGMA_OMP_TASK:
50150 case PRAGMA_OMP_TASKGROUP:
50151 case PRAGMA_OMP_TASKLOOP:
50152 case PRAGMA_OMP_TEAMS:
50153 if (context != pragma_stmt && context != pragma_compound)
50154 goto bad_stmt;
50155 stmt = push_omp_privatization_clauses (false);
50156 cp_parser_omp_construct (parser, pragma_tok, if_p);
50157 pop_omp_privatization_clauses (stmt);
50158 return true;
50160 case PRAGMA_OMP_REQUIRES:
50161 if (context != pragma_external)
50163 error_at (pragma_tok->location,
50164 "%<#pragma omp requires%> may only be used at file or "
50165 "namespace scope");
50166 ret = true;
50167 break;
50169 return cp_parser_omp_requires (parser, pragma_tok);
50171 case PRAGMA_OMP_ASSUMES:
50172 if (context != pragma_external)
50174 error_at (pragma_tok->location,
50175 "%<#pragma omp assumes%> may only be used at file or "
50176 "namespace scope");
50177 ret = true;
50178 break;
50180 return cp_parser_omp_assumes (parser, pragma_tok);
50182 case PRAGMA_OMP_NOTHING:
50183 cp_parser_omp_nothing (parser, pragma_tok);
50184 return false;
50186 case PRAGMA_OMP_ERROR:
50187 return cp_parser_omp_error (parser, pragma_tok, context);
50189 case PRAGMA_OMP_ORDERED:
50190 if (context != pragma_stmt && context != pragma_compound)
50191 goto bad_stmt;
50192 stmt = push_omp_privatization_clauses (false);
50193 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
50194 pop_omp_privatization_clauses (stmt);
50195 return ret;
50197 case PRAGMA_OMP_TARGET:
50198 if (context != pragma_stmt && context != pragma_compound)
50199 goto bad_stmt;
50200 stmt = push_omp_privatization_clauses (false);
50201 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
50202 pop_omp_privatization_clauses (stmt);
50203 return ret;
50205 case PRAGMA_OMP_BEGIN:
50206 cp_parser_omp_begin (parser, pragma_tok);
50207 return false;
50209 case PRAGMA_OMP_END:
50210 cp_parser_omp_end (parser, pragma_tok);
50211 return false;
50213 case PRAGMA_OMP_SCAN:
50214 error_at (pragma_tok->location,
50215 "%<#pragma omp scan%> may only be used in "
50216 "a loop construct with %<inscan%> %<reduction%> clause");
50217 break;
50219 case PRAGMA_OMP_SECTION:
50220 error_at (pragma_tok->location,
50221 "%<#pragma omp section%> may only be used in "
50222 "%<#pragma omp sections%> construct");
50223 break;
50225 case PRAGMA_IVDEP:
50226 case PRAGMA_UNROLL:
50227 case PRAGMA_NOVECTOR:
50229 bool ivdep;
50230 unsigned short unroll = 0;
50231 bool novector = false;
50232 const char *pragma_str;
50234 switch (id)
50236 case PRAGMA_IVDEP:
50237 pragma_str = "ivdep";
50238 break;
50239 case PRAGMA_UNROLL:
50240 pragma_str = "unroll";
50241 break;
50242 case PRAGMA_NOVECTOR:
50243 pragma_str = "novector";
50244 break;
50245 default:
50246 gcc_unreachable ();
50249 if (context == pragma_external)
50251 error_at (pragma_tok->location,
50252 "%<#pragma GCC %s%> must be inside a function",
50253 pragma_str);
50254 break;
50257 cp_token *tok = pragma_tok;
50258 bool has_more = true;
50261 switch (cp_parser_pragma_kind (tok))
50263 case PRAGMA_IVDEP:
50265 if (tok != pragma_tok)
50266 tok = cp_lexer_consume_token (parser->lexer);
50267 ivdep = cp_parser_pragma_ivdep (parser, tok);
50268 break;
50270 case PRAGMA_UNROLL:
50272 if (tok != pragma_tok)
50273 tok = cp_lexer_consume_token (parser->lexer);
50274 unroll = cp_parser_pragma_unroll (parser, tok);
50275 break;
50277 case PRAGMA_NOVECTOR:
50279 if (tok != pragma_tok)
50280 tok = cp_lexer_consume_token (parser->lexer);
50281 novector = cp_parser_pragma_novector (parser, tok);
50282 break;
50284 default:
50285 has_more = false;
50286 break;
50288 tok = cp_lexer_peek_token (the_parser->lexer);
50289 has_more = has_more && tok->type == CPP_PRAGMA;
50291 while (has_more);
50293 if (tok->type != CPP_KEYWORD
50294 || (tok->keyword != RID_FOR
50295 && tok->keyword != RID_WHILE
50296 && tok->keyword != RID_DO))
50298 cp_parser_error (parser, "for, while or do statement expected");
50299 return false;
50301 cp_parser_iteration_statement (parser, if_p, ivdep, unroll, novector);
50302 return true;
50305 default:
50306 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
50307 c_invoke_pragma_handler (id);
50308 break;
50310 bad_stmt:
50311 cp_parser_error (parser, "expected declaration specifiers");
50312 break;
50315 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
50316 return ret;
50319 /* Helper for pragma_lex in preprocess-only mode; in this mode, we have not
50320 populated the lexer with any tokens (the tokens rather being read by
50321 c-ppoutput.c's machinery), so we need to read enough tokens now to handle
50322 a pragma. */
50323 static void
50324 maybe_read_tokens_for_pragma_lex ()
50326 const auto lexer = the_parser->lexer;
50327 if (!lexer->buffer->is_empty ())
50328 return;
50330 /* Read the rest of the tokens comprising the pragma line. */
50331 cp_token *tok;
50334 tok = vec_safe_push (lexer->buffer, cp_token ());
50335 cp_lexer_get_preprocessor_token (C_LEX_STRING_NO_JOIN, tok);
50336 gcc_assert (tok->type != CPP_EOF);
50337 } while (tok->type != CPP_PRAGMA_EOL);
50338 lexer->next_token = lexer->buffer->address ();
50339 lexer->last_token = lexer->next_token + lexer->buffer->length () - 1;
50342 /* The interface the pragma parsers have to the lexer. */
50344 enum cpp_ttype
50345 pragma_lex (tree *value, location_t *loc)
50347 if (flag_preprocess_only)
50348 maybe_read_tokens_for_pragma_lex ();
50350 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
50351 enum cpp_ttype ret = tok->type;
50353 *value = tok->u.value;
50354 if (loc)
50355 *loc = tok->location;
50357 if (ret == CPP_PRAGMA_EOL)
50358 ret = CPP_EOF;
50359 else if (ret == CPP_STRING)
50360 *value = cp_parser_string_literal (the_parser, /*translate=*/false,
50361 /*wide_ok=*/false);
50362 else
50364 if (ret == CPP_KEYWORD)
50365 ret = CPP_NAME;
50366 cp_lexer_consume_token (the_parser->lexer);
50369 return ret;
50372 void
50373 pragma_lex_discard_to_eol ()
50375 /* We have already read all the tokens, so we just need to discard
50376 them here. */
50377 const auto lexer = the_parser->lexer;
50378 lexer->next_token = lexer->last_token;
50379 lexer->buffer->truncate (0);
50383 /* External interface. */
50385 /* Parse one entire translation unit. */
50387 void
50388 c_parse_file (void)
50390 static bool already_called = false;
50392 if (already_called)
50393 fatal_error (input_location,
50394 "multi-source compilation not implemented for C++");
50395 already_called = true;
50397 /* cp_lexer_new_main is called before doing any GC allocation
50398 because tokenization might load a PCH file. */
50399 cp_lexer_new_main ();
50401 cp_parser_translation_unit (the_parser);
50402 class_decl_loc_t::diag_mismatched_tags ();
50404 the_parser = NULL;
50406 finish_translation_unit ();
50409 /* Create an identifier for a generic parameter type (a synthesized
50410 template parameter implied by `auto' or a concept identifier). */
50412 static GTY(()) int generic_parm_count;
50413 static tree
50414 make_generic_type_name ()
50416 char buf[32];
50417 sprintf (buf, "auto:%d", ++generic_parm_count);
50418 return get_identifier (buf);
50421 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
50422 (creating a new template parameter list if necessary). Returns the newly
50423 created template type parm. */
50425 static tree
50426 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
50428 /* A requires-clause is not a function and cannot have placeholders. */
50429 if (current_binding_level->requires_expression)
50431 error ("placeholder type not allowed in this context");
50432 return error_mark_node;
50435 gcc_assert (current_binding_level->kind == sk_function_parms);
50437 /* We are either continuing a function template that already contains implicit
50438 template parameters, creating a new fully-implicit function template, or
50439 extending an existing explicit function template with implicit template
50440 parameters. */
50442 cp_binding_level *const entry_scope = current_binding_level;
50444 bool become_template = false;
50445 cp_binding_level *parent_scope = 0;
50447 if (parser->implicit_template_scope)
50449 gcc_assert (parser->implicit_template_parms);
50451 current_binding_level = parser->implicit_template_scope;
50453 else
50455 /* Roll back to the existing template parameter scope (in the case of
50456 extending an explicit function template) or introduce a new template
50457 parameter scope ahead of the function parameter scope (or class scope
50458 in the case of out-of-line member definitions). The function scope is
50459 added back after template parameter synthesis below. */
50461 cp_binding_level *scope = entry_scope;
50463 while (scope->kind == sk_function_parms)
50465 parent_scope = scope;
50466 scope = scope->level_chain;
50468 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
50470 /* If not defining a class, then any class scope is a scope level in
50471 an out-of-line member definition. In this case simply wind back
50472 beyond the first such scope to inject the template parameter list.
50473 Otherwise wind back to the class being defined. The latter can
50474 occur in class member friend declarations such as:
50476 class A {
50477 void foo (auto);
50479 class B {
50480 friend void A::foo (auto);
50483 The template parameter list synthesized for the friend declaration
50484 must be injected in the scope of 'B'. This can also occur in
50485 erroneous cases such as:
50487 struct A {
50488 struct B {
50489 void foo (auto);
50491 void B::foo (auto) {}
50494 Here the attempted definition of 'B::foo' within 'A' is ill-formed
50495 but, nevertheless, the template parameter list synthesized for the
50496 declarator should be injected into the scope of 'A' as if the
50497 ill-formed template was specified explicitly. */
50499 while (scope->kind == sk_class && !scope->defining_class_p)
50501 parent_scope = scope;
50502 scope = scope->level_chain;
50506 current_binding_level = scope;
50508 if (scope->kind != sk_template_parms
50509 || !function_being_declared_is_template_p (parser))
50511 /* Introduce a new template parameter list for implicit template
50512 parameters. */
50514 become_template = true;
50516 parser->implicit_template_scope
50517 = begin_scope (sk_template_parms, NULL);
50519 ++processing_template_decl;
50521 parser->fully_implicit_function_template_p = true;
50522 ++parser->num_template_parameter_lists;
50524 else
50526 /* Synthesize implicit template parameters at the end of the explicit
50527 template parameter list. */
50529 gcc_assert (current_template_parms);
50531 parser->implicit_template_scope = scope;
50533 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
50534 parser->implicit_template_parms
50535 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
50539 /* Synthesize a new template parameter and track the current template
50540 parameter chain with implicit_template_parms. */
50542 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
50543 tree synth_id = make_generic_type_name ();
50544 bool non_type = false;
50546 /* Synthesize the type template parameter. */
50547 gcc_assert(!proto || TREE_CODE (proto) == TYPE_DECL);
50548 tree synth_tmpl_parm = finish_template_type_parm (class_type_node, synth_id);
50550 if (become_template)
50551 current_template_parms = tree_cons (size_int (current_template_depth + 1),
50552 NULL_TREE, current_template_parms);
50554 /* Attach the constraint to the parm before processing. */
50555 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
50556 TREE_TYPE (node) = constr;
50557 tree new_parm
50558 = process_template_parm (parser->implicit_template_parms,
50559 input_location,
50560 node,
50561 /*non_type=*/non_type,
50562 /*param_pack=*/false);
50563 // Process_template_parm returns the list of parms, and
50564 // parser->implicit_template_parms holds the final node of the parm
50565 // list. We really want to manipulate the newly appended element.
50566 gcc_checking_assert (!parser->implicit_template_parms
50567 || parser->implicit_template_parms == new_parm);
50568 if (parser->implicit_template_parms)
50569 new_parm = TREE_CHAIN (new_parm);
50570 gcc_checking_assert (!TREE_CHAIN (new_parm));
50572 // Record the last implicit parm node
50573 parser->implicit_template_parms = new_parm;
50575 /* Mark the synthetic declaration "virtual". This is used when
50576 comparing template-heads to determine if whether an abbreviated
50577 function template is equivalent to an explicit template.
50579 Note that DECL_ARTIFICIAL is used elsewhere for template
50580 parameters. */
50581 if (TREE_VALUE (new_parm) != error_mark_node)
50582 DECL_VIRTUAL_P (TREE_VALUE (new_parm)) = true;
50584 tree new_decl = get_local_decls ();
50585 if (non_type)
50586 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
50587 new_decl = DECL_INITIAL (new_decl);
50589 /* If creating a fully implicit function template, start the new implicit
50590 template parameter list with this synthesized type, otherwise grow the
50591 current template parameter list. */
50593 if (become_template)
50595 parent_scope->level_chain = current_binding_level;
50597 tree new_parms = make_tree_vec (1);
50598 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
50599 TREE_VALUE (current_template_parms) = new_parms;
50601 else
50603 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
50604 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
50605 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
50606 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
50609 /* If the new parameter was constrained, we need to add that to the
50610 constraints in the template parameter list. */
50611 if (tree req = TEMPLATE_PARM_CONSTRAINTS (new_parm))
50613 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
50614 reqs = combine_constraint_expressions (reqs, req);
50615 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
50618 current_binding_level = entry_scope;
50620 return new_decl;
50623 /* Finish the declaration of a fully implicit function template. Such a
50624 template has no explicit template parameter list so has not been through the
50625 normal template head and tail processing. synthesize_implicit_template_parm
50626 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
50627 provided if the declaration is a class member such that its template
50628 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
50629 form is returned. Otherwise NULL_TREE is returned. */
50631 static tree
50632 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
50634 gcc_assert (parser->fully_implicit_function_template_p);
50636 if (member_decl_opt && member_decl_opt != error_mark_node
50637 && DECL_VIRTUAL_P (member_decl_opt))
50639 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
50640 "implicit templates may not be %<virtual%>");
50641 DECL_VIRTUAL_P (member_decl_opt) = false;
50644 if (member_decl_opt)
50645 member_decl_opt = finish_member_template_decl (member_decl_opt);
50646 end_template_decl ();
50648 parser->fully_implicit_function_template_p = false;
50649 parser->implicit_template_parms = 0;
50650 parser->implicit_template_scope = 0;
50651 --parser->num_template_parameter_lists;
50653 return member_decl_opt;
50656 /* Like finish_fully_implicit_template, but to be used in error
50657 recovery, rearranging scopes so that we restore the state we had
50658 before synthesize_implicit_template_parm inserted the implement
50659 template parms scope. */
50661 static void
50662 abort_fully_implicit_template (cp_parser *parser)
50664 cp_binding_level *return_to_scope = current_binding_level;
50666 if (parser->implicit_template_scope
50667 && return_to_scope != parser->implicit_template_scope)
50669 cp_binding_level *child = return_to_scope;
50670 for (cp_binding_level *scope = child->level_chain;
50671 scope != parser->implicit_template_scope;
50672 scope = child->level_chain)
50673 child = scope;
50674 child->level_chain = parser->implicit_template_scope->level_chain;
50675 parser->implicit_template_scope->level_chain = return_to_scope;
50676 current_binding_level = parser->implicit_template_scope;
50678 else
50679 return_to_scope = return_to_scope->level_chain;
50681 finish_fully_implicit_template (parser, NULL);
50683 gcc_assert (current_binding_level == return_to_scope);
50686 /* Helper function for diagnostics that have complained about things
50687 being used with 'extern "C"' linkage.
50689 Attempt to issue a note showing where the 'extern "C"' linkage began. */
50691 void
50692 maybe_show_extern_c_location (void)
50694 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
50695 inform (the_parser->innermost_linkage_specification_location,
50696 "%<extern \"C\"%> linkage started here");
50699 #include "gt-cp-parser.h"