c++: Implement P2615 'Meaningful Exports' [PR107688]
[official-gcc.git] / gcc / cp / parser.cc
bloba2bc6f69000cef65231779ea4327a3be5e86173d
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2024 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #define INCLUDE_MEMORY
23 #include "system.h"
24 #include "coretypes.h"
25 #include "cp-tree.h"
26 #include "c-family/c-common.h"
27 #include "timevar.h"
28 #include "stringpool.h"
29 #include "cgraph.h"
30 #include "print-tree.h"
31 #include "attribs.h"
32 #include "trans-mem.h"
33 #include "intl.h"
34 #include "decl.h"
35 #include "c-family/c-objc.h"
36 #include "plugin.h"
37 #include "tree-pretty-print.h"
38 #include "parser.h"
39 #include "gomp-constants.h"
40 #include "omp-general.h"
41 #include "omp-offload.h"
42 #include "c-family/c-indentation.h"
43 #include "context.h"
44 #include "gcc-rich-location.h"
45 #include "tree-iterator.h"
46 #include "cp-name-hint.h"
47 #include "memmodel.h"
48 #include "c-family/known-headers.h"
49 #include "contracts.h"
50 #include "bitmap.h"
51 #include "builtins.h"
54 /* The lexer. */
56 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
57 and c-lex.cc) and the C++ parser. */
59 /* The various kinds of non integral constant we encounter. */
60 enum non_integral_constant {
61 NIC_NONE,
62 /* floating-point literal */
63 NIC_FLOAT,
64 /* %<this%> */
65 NIC_THIS,
66 /* %<__FUNCTION__%> */
67 NIC_FUNC_NAME,
68 /* %<__PRETTY_FUNCTION__%> */
69 NIC_PRETTY_FUNC,
70 /* %<__func__%> */
71 NIC_C99_FUNC,
72 /* "%<va_arg%> */
73 NIC_VA_ARG,
74 /* a cast */
75 NIC_CAST,
76 /* %<typeid%> operator */
77 NIC_TYPEID,
78 /* non-constant compound literals */
79 NIC_NCC,
80 /* a function call */
81 NIC_FUNC_CALL,
82 /* an increment */
83 NIC_INC,
84 /* an decrement */
85 NIC_DEC,
86 /* an array reference */
87 NIC_ARRAY_REF,
88 /* %<->%> */
89 NIC_ARROW,
90 /* %<.%> */
91 NIC_POINT,
92 /* the address of a label */
93 NIC_ADDR_LABEL,
94 /* %<*%> */
95 NIC_STAR,
96 /* %<&%> */
97 NIC_ADDR,
98 /* %<++%> */
99 NIC_PREINCREMENT,
100 /* %<--%> */
101 NIC_PREDECREMENT,
102 /* %<new%> */
103 NIC_NEW,
104 /* %<delete%> */
105 NIC_DEL,
106 /* calls to overloaded operators */
107 NIC_OVERLOADED,
108 /* an assignment */
109 NIC_ASSIGNMENT,
110 /* a comma operator */
111 NIC_COMMA,
112 /* a call to a constructor */
113 NIC_CONSTRUCTOR,
114 /* a transaction expression */
115 NIC_TRANSACTION
118 /* The various kinds of errors about name-lookup failing. */
119 enum name_lookup_error {
120 /* NULL */
121 NLE_NULL,
122 /* is not a type */
123 NLE_TYPE,
124 /* is not a class or namespace */
125 NLE_CXX98,
126 /* is not a class, namespace, or enumeration */
127 NLE_NOT_CXX98
130 /* The various kinds of required token */
131 enum required_token {
132 RT_NONE,
133 RT_SEMICOLON, /* ';' */
134 RT_OPEN_PAREN, /* '(' */
135 RT_CLOSE_BRACE, /* '}' */
136 RT_OPEN_BRACE, /* '{' */
137 RT_CLOSE_SQUARE, /* ']' */
138 RT_OPEN_SQUARE, /* '[' */
139 RT_COMMA, /* ',' */
140 RT_SCOPE, /* '::' */
141 RT_LESS, /* '<' */
142 RT_GREATER, /* '>' */
143 RT_EQ, /* '=' */
144 RT_ELLIPSIS, /* '...' */
145 RT_MULT, /* '*' */
146 RT_COMPL, /* '~' */
147 RT_COLON, /* ':' */
148 RT_COLON_SCOPE, /* ':' or '::' */
149 RT_CLOSE_PAREN, /* ')' */
150 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
151 RT_PRAGMA_EOL, /* end of line */
152 RT_NAME, /* identifier */
154 /* The type is CPP_KEYWORD */
155 RT_NEW, /* new */
156 RT_DELETE, /* delete */
157 RT_RETURN, /* return */
158 RT_WHILE, /* while */
159 RT_EXTERN, /* extern */
160 RT_STATIC_ASSERT, /* static_assert */
161 RT_DECLTYPE, /* decltype */
162 RT_OPERATOR, /* operator */
163 RT_CLASS, /* class */
164 RT_TEMPLATE, /* template */
165 RT_NAMESPACE, /* namespace */
166 RT_USING, /* using */
167 RT_ASM, /* asm */
168 RT_TRY, /* try */
169 RT_CATCH, /* catch */
170 RT_THROW, /* throw */
171 RT_AUTO, /* auto */
172 RT_LABEL, /* __label__ */
173 RT_AT_TRY, /* @try */
174 RT_AT_SYNCHRONIZED, /* @synchronized */
175 RT_AT_THROW, /* @throw */
177 RT_SELECT, /* selection-statement */
178 RT_ITERATION, /* iteration-statement */
179 RT_JUMP, /* jump-statement */
180 RT_CLASS_KEY, /* class-key */
181 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
182 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
183 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
184 RT_TRANSACTION_CANCEL, /* __transaction_cancel */
186 RT_CO_YIELD /* co_yield */
189 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
190 reverting it on destruction. */
192 class type_id_in_expr_sentinel
194 cp_parser *parser;
195 bool saved;
196 public:
197 type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
198 : parser (parser),
199 saved (parser->in_type_id_in_expr_p)
200 { parser->in_type_id_in_expr_p = set; }
201 ~type_id_in_expr_sentinel ()
202 { parser->in_type_id_in_expr_p = saved; }
205 /* Prototypes. */
207 static cp_lexer *cp_lexer_new_main
208 (void);
209 static cp_lexer *cp_lexer_new_from_tokens
210 (cp_token_cache *tokens);
211 static void cp_lexer_destroy
212 (cp_lexer *);
213 static int cp_lexer_saving_tokens
214 (const cp_lexer *);
215 static cp_token *cp_lexer_token_at
216 (cp_lexer *, cp_token_position);
217 static void cp_lexer_get_preprocessor_token
218 (unsigned, cp_token *);
219 static inline cp_token *cp_lexer_peek_token
220 (cp_lexer *);
221 static cp_token *cp_lexer_peek_nth_token
222 (cp_lexer *, size_t);
223 static inline bool cp_lexer_next_token_is
224 (cp_lexer *, enum cpp_ttype);
225 static bool cp_lexer_next_token_is_not
226 (cp_lexer *, enum cpp_ttype);
227 static bool cp_lexer_next_token_is_keyword
228 (cp_lexer *, enum rid);
229 static cp_token *cp_lexer_consume_token
230 (cp_lexer *);
231 static void cp_lexer_purge_token
232 (cp_lexer *);
233 static void cp_lexer_purge_tokens_after
234 (cp_lexer *, cp_token_position);
235 static void cp_lexer_save_tokens
236 (cp_lexer *);
237 static void cp_lexer_commit_tokens
238 (cp_lexer *);
239 static void cp_lexer_rollback_tokens
240 (cp_lexer *);
241 static void cp_lexer_print_token
242 (FILE *, cp_token *);
243 static inline bool cp_lexer_debugging_p
244 (cp_lexer *);
245 static void cp_lexer_start_debugging
246 (cp_lexer *) ATTRIBUTE_UNUSED;
247 static void cp_lexer_stop_debugging
248 (cp_lexer *) ATTRIBUTE_UNUSED;
249 static const cp_trait *cp_lexer_peek_trait
250 (cp_lexer *);
251 static const cp_trait *cp_lexer_peek_trait_expr
252 (cp_lexer *);
253 static const cp_trait *cp_lexer_peek_trait_type
254 (cp_lexer *);
256 static cp_token_cache *cp_token_cache_new
257 (cp_token *, cp_token *);
258 static tree cp_parser_late_noexcept_specifier
259 (cp_parser *, tree);
260 static void noexcept_override_late_checks
261 (tree);
263 static void cp_parser_initial_pragma
264 (cp_token *);
266 static bool cp_parser_omp_declare_reduction_exprs
267 (tree, cp_parser *);
268 static void cp_finalize_oacc_routine
269 (cp_parser *, tree, bool);
271 static void check_omp_intervening_code
272 (cp_parser *);
275 /* Manifest constants. */
276 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
277 #define CP_SAVED_TOKEN_STACK 5
279 /* Variables. */
281 /* The stream to which debugging output should be written. */
282 static FILE *cp_lexer_debug_stream;
284 /* Nonzero if we are parsing an unevaluated operand: an operand to
285 sizeof, typeof, or alignof. */
286 int cp_unevaluated_operand;
288 /* Dump up to NUM tokens in BUFFER to FILE starting with token
289 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
290 first token in BUFFER. If NUM is 0, dump all the tokens. If
291 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
292 highlighted by surrounding it in [[ ]]. */
294 static void
295 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
296 cp_token *start_token, unsigned num,
297 cp_token *curr_token)
299 unsigned i, nprinted;
300 cp_token *token;
301 bool do_print;
303 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
305 if (buffer == NULL)
306 return;
308 if (num == 0)
309 num = buffer->length ();
311 if (start_token == NULL)
312 start_token = buffer->address ();
314 if (start_token > buffer->address ())
316 cp_lexer_print_token (file, &(*buffer)[0]);
317 fprintf (file, " ... ");
320 do_print = false;
321 nprinted = 0;
322 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
324 if (token == start_token)
325 do_print = true;
327 if (!do_print)
328 continue;
330 nprinted++;
331 if (token == curr_token)
332 fprintf (file, "[[");
334 cp_lexer_print_token (file, token);
336 if (token == curr_token)
337 fprintf (file, "]]");
339 switch (token->type)
341 case CPP_SEMICOLON:
342 case CPP_OPEN_BRACE:
343 case CPP_CLOSE_BRACE:
344 case CPP_EOF:
345 fputc ('\n', file);
346 break;
348 default:
349 fputc (' ', file);
353 if (i == num && i < buffer->length ())
355 fprintf (file, " ... ");
356 cp_lexer_print_token (file, &buffer->last ());
359 fprintf (file, "\n");
363 /* Dump all tokens in BUFFER to stderr. */
365 void
366 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
368 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
371 DEBUG_FUNCTION void
372 debug (vec<cp_token, va_gc> &ref)
374 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
377 DEBUG_FUNCTION void
378 debug (vec<cp_token, va_gc> *ptr)
380 if (ptr)
381 debug (*ptr);
382 else
383 fprintf (stderr, "<nil>\n");
387 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
388 description for T. */
390 static void
391 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
393 if (t)
395 fprintf (file, "%s: ", desc);
396 print_node_brief (file, "", t, 0);
401 /* Dump parser context C to FILE. */
403 static void
404 cp_debug_print_context (FILE *file, cp_parser_context *c)
406 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
407 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
408 print_node_brief (file, "", c->object_type, 0);
409 fprintf (file, "}\n");
413 /* Print the stack of parsing contexts to FILE starting with FIRST. */
415 static void
416 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
418 unsigned i;
419 cp_parser_context *c;
421 fprintf (file, "Parsing context stack:\n");
422 for (i = 0, c = first; c; c = c->next, i++)
424 fprintf (file, "\t#%u: ", i);
425 cp_debug_print_context (file, c);
430 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
432 static void
433 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
435 if (flag)
436 fprintf (file, "%s: true\n", desc);
440 /* Print an unparsed function entry UF to FILE. */
442 static void
443 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
445 unsigned i;
446 cp_default_arg_entry *default_arg_fn;
447 tree fn;
449 fprintf (file, "\tFunctions with default args:\n");
450 for (i = 0;
451 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
452 i++)
454 fprintf (file, "\t\tClass type: ");
455 print_node_brief (file, "", default_arg_fn->class_type, 0);
456 fprintf (file, "\t\tDeclaration: ");
457 print_node_brief (file, "", default_arg_fn->decl, 0);
458 fprintf (file, "\n");
461 fprintf (file, "\n\tFunctions with definitions that require "
462 "post-processing\n\t\t");
463 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
465 print_node_brief (file, "", fn, 0);
466 fprintf (file, " ");
468 fprintf (file, "\n");
470 fprintf (file, "\n\tNon-static data members with initializers that require "
471 "post-processing\n\t\t");
472 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
474 print_node_brief (file, "", fn, 0);
475 fprintf (file, " ");
477 fprintf (file, "\n");
481 /* Print the stack of unparsed member functions S to FILE. */
483 static void
484 cp_debug_print_unparsed_queues (FILE *file,
485 vec<cp_unparsed_functions_entry, va_gc> *s)
487 unsigned i;
488 cp_unparsed_functions_entry *uf;
490 fprintf (file, "Unparsed functions\n");
491 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
493 fprintf (file, "#%u:\n", i);
494 cp_debug_print_unparsed_function (file, uf);
499 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
500 the given PARSER. If FILE is NULL, the output is printed on stderr. */
502 static void
503 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
505 cp_token *next_token, *first_token, *start_token;
507 if (file == NULL)
508 file = stderr;
510 next_token = parser->lexer->next_token;
511 first_token = parser->lexer->buffer->address ();
512 start_token = (next_token > first_token + window_size / 2)
513 ? next_token - window_size / 2
514 : first_token;
515 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
516 next_token);
520 /* Dump debugging information for the given PARSER. If FILE is NULL,
521 the output is printed on stderr. */
523 void
524 cp_debug_parser (FILE *file, cp_parser *parser)
526 const size_t window_size = 20;
527 cp_token *token;
528 expanded_location eloc;
530 if (file == NULL)
531 file = stderr;
533 fprintf (file, "Parser state\n\n");
534 fprintf (file, "Number of tokens: %u\n",
535 vec_safe_length (parser->lexer->buffer));
536 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
537 cp_debug_print_tree_if_set (file, "Object scope",
538 parser->object_scope);
539 cp_debug_print_tree_if_set (file, "Qualifying scope",
540 parser->qualifying_scope);
541 cp_debug_print_context_stack (file, parser->context);
542 cp_debug_print_flag (file, "Allow GNU extensions",
543 parser->allow_gnu_extensions_p);
544 cp_debug_print_flag (file, "'>' token is greater-than",
545 parser->greater_than_is_operator_p);
546 cp_debug_print_flag (file, "Default args allowed in current "
547 "parameter list", parser->default_arg_ok_p);
548 cp_debug_print_flag (file, "Parsing integral constant-expression",
549 parser->integral_constant_expression_p);
550 cp_debug_print_flag (file, "Allow non-constant expression in current "
551 "constant-expression",
552 parser->allow_non_integral_constant_expression_p);
553 cp_debug_print_flag (file, "Seen non-constant expression",
554 parser->non_integral_constant_expression_p);
555 cp_debug_print_flag (file, "Local names forbidden in current context",
556 (parser->local_variables_forbidden_p
557 & LOCAL_VARS_FORBIDDEN));
558 cp_debug_print_flag (file, "'this' forbidden in current context",
559 (parser->local_variables_forbidden_p
560 & THIS_FORBIDDEN));
561 cp_debug_print_flag (file, "In unbraced linkage specification",
562 parser->in_unbraced_linkage_specification_p);
563 cp_debug_print_flag (file, "In unbraced export declaration",
564 parser->in_unbraced_export_declaration_p);
565 cp_debug_print_flag (file, "Parsing a declarator",
566 parser->in_declarator_p);
567 cp_debug_print_flag (file, "In template argument list",
568 parser->in_template_argument_list_p);
569 cp_debug_print_flag (file, "Parsing an iteration statement",
570 parser->in_statement & IN_ITERATION_STMT);
571 cp_debug_print_flag (file, "Parsing a switch statement",
572 parser->in_statement & IN_SWITCH_STMT);
573 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
574 parser->in_statement & IN_OMP_BLOCK);
575 cp_debug_print_flag (file, "Parsing an OpenMP loop",
576 parser->in_statement & IN_OMP_FOR);
577 cp_debug_print_flag (file, "Parsing an if statement",
578 parser->in_statement & IN_IF_STMT);
579 cp_debug_print_flag (file, "Parsing a type-id in an expression "
580 "context", parser->in_type_id_in_expr_p);
581 cp_debug_print_flag (file, "String expressions should be translated "
582 "to execution character set",
583 parser->translate_strings_p);
584 cp_debug_print_flag (file, "Parsing function body outside of a "
585 "local class", parser->in_function_body);
586 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
587 parser->colon_corrects_to_scope_p);
588 cp_debug_print_flag (file, "Colon doesn't start a class definition",
589 parser->colon_doesnt_start_class_def_p);
590 cp_debug_print_flag (file, "Parsing an Objective-C++ message context",
591 parser->objective_c_message_context_p);
592 if (parser->type_definition_forbidden_message)
593 fprintf (file, "Error message for forbidden type definitions: %s %s\n",
594 parser->type_definition_forbidden_message,
595 parser->type_definition_forbidden_message_arg
596 ? parser->type_definition_forbidden_message_arg : "<none>");
597 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
598 fprintf (file, "Number of class definitions in progress: %u\n",
599 parser->num_classes_being_defined);
600 fprintf (file, "Number of template parameter lists for the current "
601 "declaration: %u\n", parser->num_template_parameter_lists);
602 cp_debug_parser_tokens (file, parser, window_size);
603 token = parser->lexer->next_token;
604 fprintf (file, "Next token to parse:\n");
605 fprintf (file, "\tToken: ");
606 cp_lexer_print_token (file, token);
607 eloc = expand_location (token->location);
608 fprintf (file, "\n\tFile: %s\n", eloc.file);
609 fprintf (file, "\tLine: %d\n", eloc.line);
610 fprintf (file, "\tColumn: %d\n", eloc.column);
613 DEBUG_FUNCTION void
614 debug (cp_parser &ref)
616 cp_debug_parser (stderr, &ref);
619 DEBUG_FUNCTION void
620 debug (cp_parser *ptr)
622 if (ptr)
623 debug (*ptr);
624 else
625 fprintf (stderr, "<nil>\n");
628 /* Allocate memory for a new lexer object and return it. */
630 static cp_lexer *
631 cp_lexer_alloc (void)
633 /* Allocate the memory. */
634 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
636 /* Initially we are not debugging. */
637 lexer->debugging_p = false;
639 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
641 /* Create the buffer. */
642 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
644 return lexer;
647 /* Return TRUE if token is the start of a module declaration that will be
648 terminated by a CPP_PRAGMA_EOL token. */
649 static inline bool
650 cp_token_is_module_directive (cp_token *token)
652 return token->keyword == RID__EXPORT
653 || token->keyword == RID__MODULE
654 || token->keyword == RID__IMPORT;
657 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
658 PRAGMA_NONE. */
660 static enum pragma_kind
661 cp_parser_pragma_kind (cp_token *token)
663 if (token->type != CPP_PRAGMA)
664 return PRAGMA_NONE;
665 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
666 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
669 /* Handle early pragmas such as #pragma GCC diagnostic, which needs to be done
670 during preprocessing for the case of preprocessing-related diagnostics. This
671 is called immediately after pushing the CPP_PRAGMA_EOL token onto
672 lexer->buffer. */
674 static void
675 cp_lexer_handle_early_pragma (cp_lexer *lexer)
677 const auto first_token = lexer->buffer->address ();
678 const auto last_token = first_token + lexer->buffer->length () - 1;
680 /* Back up to the start of the pragma so pragma_lex () can parse it when
681 c-pragma lib asks it to. */
682 auto begin = last_token;
683 gcc_assert (begin->type == CPP_PRAGMA_EOL);
684 while (begin->type != CPP_PRAGMA)
686 if (cp_token_is_module_directive (begin))
687 return;
688 gcc_assert (begin != first_token);
689 --begin;
691 gcc_assert (!lexer->next_token);
692 gcc_assert (!lexer->last_token);
693 lexer->next_token = begin;
694 lexer->last_token = last_token;
696 /* Dispatch it. */
697 const unsigned int id
698 = cp_parser_pragma_kind (cp_lexer_consume_token (lexer));
699 if (id >= PRAGMA_FIRST_EXTERNAL)
700 c_invoke_early_pragma_handler (id);
702 /* Reset to normal state. */
703 lexer->next_token = lexer->last_token = nullptr;
706 /* The parser. */
707 static cp_parser *cp_parser_new (cp_lexer *);
708 static GTY (()) cp_parser *the_parser;
710 /* Create a new main C++ lexer, the lexer that gets tokens from the
711 preprocessor, and also create the main parser. */
713 static cp_lexer *
714 cp_lexer_new_main (void)
716 cp_token token;
718 /* It's possible that parsing the first pragma will load a PCH file,
719 which is a GC collection point. So we have to do that before
720 allocating any memory. */
721 cp_lexer_get_preprocessor_token (C_LEX_STRING_NO_JOIN, &token);
722 cp_parser_initial_pragma (&token);
723 c_common_no_more_pch ();
725 cp_lexer *lexer = cp_lexer_alloc ();
726 /* Put the first token in the buffer. */
727 cp_token *tok = lexer->buffer->quick_push (token);
729 uintptr_t filter = 0;
730 if (modules_p ())
731 filter = module_token_cdtor (parse_in, filter);
733 /* Create the parser now, so we can use it to handle early pragmas. */
734 gcc_assert (!the_parser);
735 the_parser = cp_parser_new (lexer);
737 /* Get the remaining tokens from the preprocessor. */
738 while (tok->type != CPP_EOF)
740 if (filter)
741 /* Process the previous token. */
742 module_token_lang (tok->type, tok->keyword, tok->u.value,
743 tok->location, filter);
745 /* Check for early pragmas that need to be handled now. */
746 if (tok->type == CPP_PRAGMA_EOL)
747 cp_lexer_handle_early_pragma (lexer);
749 tok = vec_safe_push (lexer->buffer, cp_token ());
750 cp_lexer_get_preprocessor_token (C_LEX_STRING_NO_JOIN, tok);
753 lexer->next_token = lexer->buffer->address ();
754 lexer->last_token = lexer->next_token
755 + lexer->buffer->length ()
756 - 1;
758 if (lexer->buffer->length () != 1)
760 /* Set the EOF token's location to be the just after the previous
761 token's range. That way 'at-eof' diagnostics point at something
762 meaninful. */
763 auto range = get_range_from_loc (line_table, tok[-1].location);
764 tok[0].location
765 = linemap_position_for_loc_and_offset (line_table, range.m_finish, 1);
768 if (filter)
769 module_token_cdtor (parse_in, filter);
771 /* Subsequent preprocessor diagnostics should use compiler
772 diagnostic functions to get the compiler source location. */
773 override_libcpp_locations = true;
775 maybe_check_all_macros (parse_in);
777 /* If we processed any #pragma GCC target directives, we handled them early so
778 any macros they defined would be effective during preprocessing. Now, we
779 need to reset to the default state to begin compilation, and we will
780 process them again at the correct time as needed. */
781 c_reset_target_pragmas ();
783 gcc_assert (!lexer->next_token->purged_p);
784 return lexer;
787 /* Create a lexer and parser to be used during preprocess-only mode.
788 This will be filled with tokens to parse when needed by pragma_lex (). */
789 void
790 c_init_preprocess ()
792 gcc_assert (!the_parser);
793 the_parser = cp_parser_new (cp_lexer_alloc ());
796 /* Create a new lexer whose token stream is primed with the tokens in
797 CACHE. When these tokens are exhausted, no new tokens will be read. */
799 static cp_lexer *
800 cp_lexer_new_from_tokens (cp_token_cache *cache)
802 cp_token *first = cache->first;
803 cp_token *last = cache->last;
804 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
806 /* We do not own the buffer. */
807 lexer->buffer = NULL;
809 /* Insert an EOF token. */
810 lexer->saved_type = last->type;
811 lexer->saved_keyword = last->keyword;
812 last->type = CPP_EOF;
813 last->keyword = RID_MAX;
815 lexer->next_token = first;
816 lexer->last_token = last;
818 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
820 /* Initially we are not debugging. */
821 lexer->debugging_p = false;
823 gcc_assert (!lexer->next_token->purged_p
824 && !lexer->last_token->purged_p);
825 return lexer;
828 /* Frees all resources associated with LEXER. */
830 static void
831 cp_lexer_destroy (cp_lexer *lexer)
833 if (lexer->buffer)
834 vec_free (lexer->buffer);
835 else
837 /* Restore the token we overwrite with EOF. */
838 lexer->last_token->type = lexer->saved_type;
839 lexer->last_token->keyword = lexer->saved_keyword;
841 lexer->saved_tokens.release ();
842 ggc_free (lexer);
845 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
846 be used. The point of this flag is to help the compiler to fold away calls
847 to cp_lexer_debugging_p within this source file at compile time, when the
848 lexer is not being debugged. */
850 #define LEXER_DEBUGGING_ENABLED_P false
852 /* Returns nonzero if debugging information should be output. */
854 static inline bool
855 cp_lexer_debugging_p (cp_lexer *lexer)
857 if (!LEXER_DEBUGGING_ENABLED_P)
858 return false;
860 return lexer->debugging_p;
864 static inline cp_token_position
865 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
867 return lexer->next_token - previous_p;
870 static inline cp_token *
871 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
873 return pos;
876 static inline void
877 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
879 lexer->next_token = cp_lexer_token_at (lexer, pos);
882 static inline cp_token_position
883 cp_lexer_previous_token_position (cp_lexer *lexer)
885 return cp_lexer_token_position (lexer, true);
888 static inline cp_token *
889 cp_lexer_previous_token (cp_lexer *lexer)
891 cp_token_position tp = cp_lexer_previous_token_position (lexer);
893 /* Skip past purged tokens. */
894 while (tp->purged_p)
896 gcc_assert (tp != vec_safe_address (lexer->buffer));
897 tp--;
900 return cp_lexer_token_at (lexer, tp);
903 /* Same as above, but return NULL when the lexer doesn't own the token
904 buffer or if the next_token is at the start of the token
905 vector or if all previous tokens are purged. */
907 static cp_token *
908 cp_lexer_safe_previous_token (cp_lexer *lexer)
910 if (lexer->buffer
911 && lexer->next_token != lexer->buffer->address ())
913 cp_token_position tp = cp_lexer_previous_token_position (lexer);
915 /* Skip past purged tokens. */
916 while (tp->purged_p)
918 if (tp == lexer->buffer->address ())
919 return NULL;
920 tp--;
922 return cp_lexer_token_at (lexer, tp);
925 return NULL;
928 /* Overload for make_location, taking the lexer to mean the location of the
929 previous token. */
931 static inline location_t
932 make_location (location_t caret, location_t start, cp_lexer *lexer)
934 cp_token *t = cp_lexer_previous_token (lexer);
935 return make_location (caret, start, t->location);
938 /* Overload for make_location taking tokens instead of locations. */
940 static inline location_t
941 make_location (cp_token *caret, cp_token *start, cp_token *end)
943 return make_location (caret->location, start->location, end->location);
946 /* nonzero if we are presently saving tokens. */
948 static inline int
949 cp_lexer_saving_tokens (const cp_lexer* lexer)
951 return lexer->saved_tokens.length () != 0;
954 /* Store the next token from the preprocessor in *TOKEN. Return true
955 if we reach EOF. If LEXER is NULL, assume we are handling an
956 initial #pragma pch_preprocess, and thus want the lexer to return
957 processed strings.
959 Diagnostics issued from this function must have their controlling option (if
960 any) in c.opt annotated as a libcpp option via the CppReason property. */
962 static void
963 cp_lexer_get_preprocessor_token (unsigned flags, cp_token *token)
965 static int is_extern_c = 0;
967 /* Get a new token from the preprocessor. */
968 token->type
969 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
970 flags);
971 token->keyword = RID_MAX;
972 token->purged_p = false;
973 token->error_reported = false;
974 token->tree_check_p = false;
975 /* Usually never see a zero, but just in case ... */
976 token->main_source_p = line_table->depth <= 1;
978 /* On some systems, some header files are surrounded by an
979 implicit extern "C" block. Set a flag in the token if it
980 comes from such a header. */
981 is_extern_c += pending_lang_change;
982 pending_lang_change = 0;
983 token->implicit_extern_c = is_extern_c > 0;
985 /* Check to see if this token is a keyword. */
986 if (token->type == CPP_NAME)
988 if (IDENTIFIER_KEYWORD_P (token->u.value))
990 /* Mark this token as a keyword. */
991 token->type = CPP_KEYWORD;
992 /* Record which keyword. */
993 token->keyword = C_RID_CODE (token->u.value);
995 else
997 if (warn_cxx11_compat
998 && ((C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
999 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
1000 /* These are outside the CXX11 range. */
1001 || C_RID_CODE (token->u.value) == RID_ALIGNOF
1002 || C_RID_CODE (token->u.value) == RID_ALIGNAS
1003 || C_RID_CODE (token->u.value)== RID_THREAD))
1005 /* Warn about the C++11 keyword (but still treat it as
1006 an identifier). */
1007 warning_at (token->location, OPT_Wc__11_compat,
1008 "identifier %qE is a keyword in C++11",
1009 token->u.value);
1011 /* Clear out the C_RID_CODE so we don't warn about this
1012 particular identifier-turned-keyword again. */
1013 C_SET_RID_CODE (token->u.value, RID_MAX);
1015 if (warn_cxx20_compat
1016 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX20
1017 && C_RID_CODE (token->u.value) <= RID_LAST_CXX20)
1019 /* Warn about the C++20 keyword (but still treat it as
1020 an identifier). */
1021 warning_at (token->location, OPT_Wc__20_compat,
1022 "identifier %qE is a keyword in C++20",
1023 token->u.value);
1025 /* Clear out the C_RID_CODE so we don't warn about this
1026 particular identifier-turned-keyword again. */
1027 C_SET_RID_CODE (token->u.value, RID_MAX);
1030 token->keyword = RID_MAX;
1033 else if (token->type == CPP_AT_NAME)
1035 /* This only happens in Objective-C++; it must be a keyword. */
1036 token->type = CPP_KEYWORD;
1037 switch (C_RID_CODE (token->u.value))
1039 /* Replace 'class' with '@class', 'private' with '@private',
1040 etc. This prevents confusion with the C++ keyword
1041 'class', and makes the tokens consistent with other
1042 Objective-C 'AT' keywords. For example '@class' is
1043 reported as RID_AT_CLASS which is consistent with
1044 '@synchronized', which is reported as
1045 RID_AT_SYNCHRONIZED.
1047 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
1048 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
1049 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
1050 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
1051 case RID_THROW: token->keyword = RID_AT_THROW; break;
1052 case RID_TRY: token->keyword = RID_AT_TRY; break;
1053 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
1054 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
1055 default: token->keyword = C_RID_CODE (token->u.value);
1060 /* Update the globals input_location and the input file stack from TOKEN. */
1061 static inline void
1062 cp_lexer_set_source_position_from_token (cp_token *token)
1064 input_location = token->location;
1067 /* Update the globals input_location and the input file stack from LEXER. */
1068 static inline void
1069 cp_lexer_set_source_position (cp_lexer *lexer)
1071 cp_token *token = cp_lexer_peek_token (lexer);
1072 cp_lexer_set_source_position_from_token (token);
1075 /* Return a pointer to the next token in the token stream, but do not
1076 consume it. */
1078 static inline cp_token *
1079 cp_lexer_peek_token (cp_lexer *lexer)
1081 if (cp_lexer_debugging_p (lexer))
1083 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
1084 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
1085 putc ('\n', cp_lexer_debug_stream);
1087 return lexer->next_token;
1090 /* Return true if the next token has the indicated TYPE. */
1092 static inline bool
1093 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
1095 return cp_lexer_peek_token (lexer)->type == type;
1098 /* Return true if the next token does not have the indicated TYPE. */
1100 static inline bool
1101 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
1103 return !cp_lexer_next_token_is (lexer, type);
1106 /* Return true if the next token is the indicated KEYWORD. */
1108 static inline bool
1109 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
1111 return cp_lexer_peek_token (lexer)->keyword == keyword;
1114 static inline bool
1115 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
1117 return cp_lexer_peek_nth_token (lexer, n)->type == type;
1120 static inline bool
1121 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
1123 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
1126 /* Return true if KEYWORD can start a decl-specifier. */
1128 bool
1129 cp_keyword_starts_decl_specifier_p (enum rid keyword)
1131 switch (keyword)
1133 /* auto specifier: storage-class-specifier in C++,
1134 simple-type-specifier in C++0x. */
1135 case RID_AUTO:
1136 /* Storage classes. */
1137 case RID_REGISTER:
1138 case RID_STATIC:
1139 case RID_EXTERN:
1140 case RID_MUTABLE:
1141 case RID_THREAD:
1142 /* Elaborated type specifiers. */
1143 case RID_ENUM:
1144 case RID_CLASS:
1145 case RID_STRUCT:
1146 case RID_UNION:
1147 case RID_TYPENAME:
1148 /* Simple type specifiers. */
1149 case RID_CHAR:
1150 case RID_CHAR8:
1151 case RID_CHAR16:
1152 case RID_CHAR32:
1153 case RID_WCHAR:
1154 case RID_BOOL:
1155 case RID_SHORT:
1156 case RID_INT:
1157 case RID_LONG:
1158 case RID_SIGNED:
1159 case RID_UNSIGNED:
1160 case RID_FLOAT:
1161 case RID_DOUBLE:
1162 CASE_RID_FLOATN_NX:
1163 case RID_VOID:
1164 /* CV qualifiers. */
1165 case RID_CONST:
1166 case RID_VOLATILE:
1167 /* Function specifiers. */
1168 case RID_EXPLICIT:
1169 case RID_VIRTUAL:
1170 /* friend/typdef/inline specifiers. */
1171 case RID_FRIEND:
1172 case RID_TYPEDEF:
1173 case RID_INLINE:
1174 /* GNU extensions. */
1175 case RID_TYPEOF:
1176 /* C++11 extensions. */
1177 case RID_DECLTYPE:
1178 case RID_CONSTEXPR:
1179 /* C++20 extensions. */
1180 case RID_CONSTINIT:
1181 case RID_CONSTEVAL:
1182 return true;
1184 default:
1185 if (keyword >= RID_FIRST_INT_N
1186 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
1187 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
1188 return true;
1189 return false;
1193 /* Peeks the corresponding built-in trait if the first token is
1194 a built-in trait and the second token is either `(' or `<' depending
1195 on the trait. Otherwise, returns nullptr. */
1197 static const cp_trait *
1198 cp_lexer_peek_trait (cp_lexer *lexer)
1200 const cp_token *token1 = cp_lexer_peek_token (lexer);
1201 if (token1->type == CPP_NAME && IDENTIFIER_TRAIT_P (token1->u.value))
1203 const cp_trait &trait = cp_traits[IDENTIFIER_CP_INDEX (token1->u.value)];
1204 const bool is_pack_element = (trait.kind == CPTK_TYPE_PACK_ELEMENT);
1206 /* Check if the subsequent token is a `<' token to
1207 __type_pack_element or is a `(' token to everything else. */
1208 const cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
1209 if (is_pack_element && token2->type != CPP_LESS)
1210 return nullptr;
1211 if (!is_pack_element && token2->type != CPP_OPEN_PAREN)
1212 return nullptr;
1214 return &trait;
1216 return nullptr;
1219 /* Similarly, but only if the token is an expression-yielding
1220 built-in trait. */
1222 static const cp_trait *
1223 cp_lexer_peek_trait_expr (cp_lexer *lexer)
1225 const cp_trait *trait = cp_lexer_peek_trait (lexer);
1226 if (trait && !trait->type)
1227 return trait;
1229 return nullptr;
1232 /* Similarly, but only if the token is a type-yielding
1233 built-in trait. */
1235 static const cp_trait *
1236 cp_lexer_peek_trait_type (cp_lexer *lexer)
1238 const cp_trait *trait = cp_lexer_peek_trait (lexer);
1239 if (trait && trait->type)
1240 return trait;
1242 return nullptr;
1245 /* Return true if the next token is a keyword for a decl-specifier. */
1247 static bool
1248 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
1250 cp_token *token;
1252 if (cp_lexer_peek_trait_type (lexer))
1253 return true;
1255 token = cp_lexer_peek_token (lexer);
1256 return cp_keyword_starts_decl_specifier_p (token->keyword);
1259 /* Returns TRUE iff the token T begins a decltype type. */
1261 static bool
1262 token_is_decltype (cp_token *t)
1264 return (t->keyword == RID_DECLTYPE
1265 || t->type == CPP_DECLTYPE);
1268 /* Returns TRUE iff the next token begins a decltype type. */
1270 static bool
1271 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1273 cp_token *t = cp_lexer_peek_token (lexer);
1274 return token_is_decltype (t);
1277 /* Called when processing a token with tree_check_value; perform or defer the
1278 associated checks and return the value. */
1280 static tree
1281 saved_checks_value (struct tree_check *check_value)
1283 /* Perform any access checks that were deferred. */
1284 vec<deferred_access_check, va_gc> *checks;
1285 deferred_access_check *chk;
1286 checks = check_value->checks;
1287 if (checks)
1289 int i;
1290 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1291 perform_or_defer_access_check (chk->binfo,
1292 chk->decl,
1293 chk->diag_decl, tf_warning_or_error);
1295 /* Return the stored value. */
1296 return check_value->value;
1299 /* Return a pointer to the Nth token in the token stream. If N is 1,
1300 then this is precisely equivalent to cp_lexer_peek_token (except
1301 that it is not inline). One would like to disallow that case, but
1302 there is one case (cp_parser_nth_token_starts_template_id) where
1303 the caller passes a variable for N and it might be 1. */
1305 static cp_token *
1306 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1308 cp_token *token;
1310 /* N is 1-based, not zero-based. */
1311 gcc_assert (n > 0);
1313 if (cp_lexer_debugging_p (lexer))
1314 fprintf (cp_lexer_debug_stream,
1315 "cp_lexer: peeking ahead " HOST_SIZE_T_PRINT_DEC " at token: ",
1316 (fmt_size_t) n);
1318 --n;
1319 token = lexer->next_token;
1320 while (n && token->type != CPP_EOF)
1322 ++token;
1323 if (!token->purged_p)
1324 --n;
1327 if (cp_lexer_debugging_p (lexer))
1329 cp_lexer_print_token (cp_lexer_debug_stream, token);
1330 putc ('\n', cp_lexer_debug_stream);
1333 return token;
1336 /* Return the next token, and advance the lexer's next_token pointer
1337 to point to the next non-purged token. */
1339 static cp_token *
1340 cp_lexer_consume_token (cp_lexer* lexer)
1342 cp_token *token = lexer->next_token;
1346 gcc_assert (token->type != CPP_EOF);
1347 lexer->next_token++;
1349 while (lexer->next_token->purged_p);
1351 cp_lexer_set_source_position_from_token (token);
1353 /* Provide debugging output. */
1354 if (cp_lexer_debugging_p (lexer))
1356 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1357 cp_lexer_print_token (cp_lexer_debug_stream, token);
1358 putc ('\n', cp_lexer_debug_stream);
1361 return token;
1364 /* Permanently remove the next token from the token stream, and
1365 advance the next_token pointer to refer to the next non-purged
1366 token. */
1368 static void
1369 cp_lexer_purge_token (cp_lexer *lexer)
1371 cp_token *tok = lexer->next_token;
1373 gcc_assert (tok->type != CPP_EOF);
1374 tok->purged_p = true;
1375 tok->location = UNKNOWN_LOCATION;
1376 tok->u.value = NULL_TREE;
1377 tok->keyword = RID_MAX;
1380 tok++;
1381 while (tok->purged_p);
1382 lexer->next_token = tok;
1385 /* Permanently remove all tokens after TOK, up to, but not
1386 including, the token that will be returned next by
1387 cp_lexer_peek_token. */
1389 static void
1390 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1392 cp_token *peek = lexer->next_token;
1394 gcc_assert (tok < peek);
1396 for (tok++; tok != peek; tok++)
1398 tok->purged_p = true;
1399 tok->location = UNKNOWN_LOCATION;
1400 tok->u.value = NULL_TREE;
1401 tok->keyword = RID_MAX;
1405 /* Begin saving tokens. All tokens consumed after this point will be
1406 preserved. */
1408 static void
1409 cp_lexer_save_tokens (cp_lexer* lexer)
1411 /* Provide debugging output. */
1412 if (cp_lexer_debugging_p (lexer))
1413 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1415 lexer->saved_tokens.safe_push (lexer->next_token);
1418 /* Commit to the portion of the token stream most recently saved. */
1420 static void
1421 cp_lexer_commit_tokens (cp_lexer* lexer)
1423 /* Provide debugging output. */
1424 if (cp_lexer_debugging_p (lexer))
1425 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1427 lexer->saved_tokens.pop ();
1430 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1431 to the token stream. Stop saving tokens. */
1433 static void
1434 cp_lexer_rollback_tokens (cp_lexer* lexer)
1436 /* Provide debugging output. */
1437 if (cp_lexer_debugging_p (lexer))
1438 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1440 lexer->next_token = lexer->saved_tokens.pop ();
1443 /* Determines what saved_token_sentinel does when going out of scope. */
1445 enum saved_token_sentinel_mode {
1446 STS_COMMIT,
1447 STS_ROLLBACK,
1448 STS_DONOTHING
1451 /* RAII wrapper around the above functions, with sanity checking (the token
1452 stream should be the same at the point of instantiation as it is at the
1453 point of destruction).
1455 Creating a variable saves tokens. MODE determines what happens when the
1456 object is destroyed. STS_COMMIT commits tokens (default),
1457 STS_ROLLBACK rolls-back and STS_DONOTHING does nothing. Calling
1458 rollback() will immediately roll-back tokens and set MODE to
1459 STS_DONOTHING. */
1461 struct saved_token_sentinel
1463 cp_lexer *lexer;
1464 unsigned len;
1465 saved_token_sentinel_mode mode;
1466 saved_token_sentinel (cp_lexer *_lexer,
1467 saved_token_sentinel_mode _mode = STS_COMMIT)
1468 : lexer (_lexer), mode (_mode)
1470 len = lexer->saved_tokens.length ();
1471 cp_lexer_save_tokens (lexer);
1473 void rollback ()
1475 cp_lexer_rollback_tokens (lexer);
1476 cp_lexer_set_source_position_from_token
1477 (cp_lexer_previous_token (lexer));
1478 mode = STS_DONOTHING;
1480 ~saved_token_sentinel ()
1482 if (mode == STS_COMMIT)
1483 cp_lexer_commit_tokens (lexer);
1484 else if (mode == STS_ROLLBACK)
1485 rollback ();
1487 gcc_assert (lexer->saved_tokens.length () == len);
1491 /* Print a representation of the TOKEN on the STREAM. */
1493 static void
1494 cp_lexer_print_token (FILE * stream, cp_token *token)
1496 /* We don't use cpp_type2name here because the parser defines
1497 a few tokens of its own. */
1498 static const char *const token_names[] = {
1499 /* cpplib-defined token types */
1500 #define OP(e, s) #e,
1501 #define TK(e, s) #e,
1502 TTYPE_TABLE
1503 #undef OP
1504 #undef TK
1505 /* C++ parser token types - see "Manifest constants", above. */
1506 "KEYWORD",
1507 "TEMPLATE_ID",
1508 "NESTED_NAME_SPECIFIER",
1511 /* For some tokens, print the associated data. */
1512 switch (token->type)
1514 case CPP_KEYWORD:
1515 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1516 For example, `struct' is mapped to an INTEGER_CST. */
1517 if (!identifier_p (token->u.value))
1518 break;
1519 /* fall through */
1520 case CPP_NAME:
1521 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1522 break;
1524 case CPP_STRING:
1525 case CPP_STRING16:
1526 case CPP_STRING32:
1527 case CPP_WSTRING:
1528 case CPP_UTF8STRING:
1529 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1530 break;
1532 case CPP_NUMBER:
1533 print_generic_expr (stream, token->u.value);
1534 break;
1536 default:
1537 /* If we have a name for the token, print it out. Otherwise, we
1538 simply give the numeric code. */
1539 if (token->type < ARRAY_SIZE(token_names))
1540 fputs (token_names[token->type], stream);
1541 else
1542 fprintf (stream, "[%d]", token->type);
1543 break;
1547 DEBUG_FUNCTION void
1548 debug (cp_token &ref)
1550 cp_lexer_print_token (stderr, &ref);
1551 fprintf (stderr, "\n");
1554 DEBUG_FUNCTION void
1555 debug (cp_token *ptr)
1557 if (ptr)
1558 debug (*ptr);
1559 else
1560 fprintf (stderr, "<nil>\n");
1564 /* Start emitting debugging information. */
1566 static void
1567 cp_lexer_start_debugging (cp_lexer* lexer)
1569 if (!LEXER_DEBUGGING_ENABLED_P)
1570 fatal_error (input_location,
1571 "%<LEXER_DEBUGGING_ENABLED_P%> is not set to true");
1573 lexer->debugging_p = true;
1574 cp_lexer_debug_stream = stderr;
1577 /* Stop emitting debugging information. */
1579 static void
1580 cp_lexer_stop_debugging (cp_lexer* lexer)
1582 if (!LEXER_DEBUGGING_ENABLED_P)
1583 fatal_error (input_location,
1584 "%<LEXER_DEBUGGING_ENABLED_P%> is not set to true");
1586 lexer->debugging_p = false;
1587 cp_lexer_debug_stream = NULL;
1590 /* Create a new cp_token_cache, representing a range of tokens. */
1592 static cp_token_cache *
1593 cp_token_cache_new (cp_token *first, cp_token *last)
1595 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1596 cache->first = first;
1597 cache->last = last;
1598 return cache;
1601 /* Diagnose if #pragma omp declare simd isn't followed immediately
1602 by function declaration or definition. */
1604 static inline void
1605 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1607 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1609 error ("%<#pragma omp declare %s%> not immediately followed by "
1610 "function declaration or definition",
1611 parser->omp_declare_simd->variant_p ? "variant" : "simd");
1612 parser->omp_declare_simd = NULL;
1616 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1617 and put that into "omp declare simd" attribute. */
1619 static inline void
1620 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1622 if (UNLIKELY (parser->omp_declare_simd != NULL))
1624 if (fndecl == error_mark_node)
1626 parser->omp_declare_simd = NULL;
1627 return;
1629 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1631 cp_ensure_no_omp_declare_simd (parser);
1632 return;
1637 /* Similarly, but for use in declaration parsing functions
1638 which call cp_parser_handle_directive_omp_attributes. */
1640 static inline void
1641 cp_finalize_omp_declare_simd (cp_parser *parser, cp_omp_declare_simd_data *data)
1643 if (parser->omp_declare_simd != data)
1644 return;
1646 if (!parser->omp_declare_simd->error_seen
1647 && !parser->omp_declare_simd->fndecl_seen)
1648 error_at (parser->omp_declare_simd->loc,
1649 "%<declare %s%> directive not immediately followed by "
1650 "function declaration or definition",
1651 parser->omp_declare_simd->variant_p ? "variant" : "simd");
1652 parser->omp_declare_simd = NULL;
1655 /* Diagnose if #pragma acc routine isn't followed immediately by function
1656 declaration or definition. */
1658 static inline void
1659 cp_ensure_no_oacc_routine (cp_parser *parser)
1661 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1663 error_at (parser->oacc_routine->loc,
1664 "%<#pragma acc routine%> not immediately followed by "
1665 "function declaration or definition");
1666 parser->oacc_routine = NULL;
1670 /* Decl-specifiers. */
1672 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1674 static void
1675 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1677 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1680 /* Declarators. */
1682 /* Nothing other than the parser should be creating declarators;
1683 declarators are a semi-syntactic representation of C++ entities.
1684 Other parts of the front end that need to create entities (like
1685 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1687 static cp_declarator *make_call_declarator
1688 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier,
1689 tree, tree, tree, tree, tree, location_t);
1690 static cp_declarator *make_array_declarator
1691 (cp_declarator *, tree);
1692 static cp_declarator *make_pointer_declarator
1693 (cp_cv_quals, cp_declarator *, tree);
1694 static cp_declarator *make_reference_declarator
1695 (cp_cv_quals, cp_declarator *, bool, tree);
1696 static cp_declarator *make_ptrmem_declarator
1697 (cp_cv_quals, tree, cp_declarator *, tree);
1699 /* An erroneous declarator. */
1700 static cp_declarator *cp_error_declarator;
1702 /* The obstack on which declarators and related data structures are
1703 allocated. */
1704 static struct obstack declarator_obstack;
1706 /* Alloc BYTES from the declarator memory pool. */
1708 static inline void *
1709 alloc_declarator (size_t bytes)
1711 return obstack_alloc (&declarator_obstack, bytes);
1714 /* Allocate a declarator of the indicated KIND. Clear fields that are
1715 common to all declarators. */
1717 static cp_declarator *
1718 make_declarator (cp_declarator_kind kind)
1720 cp_declarator *declarator;
1722 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1723 declarator->kind = kind;
1724 declarator->parenthesized = UNKNOWN_LOCATION;
1725 declarator->attributes = NULL_TREE;
1726 declarator->std_attributes = NULL_TREE;
1727 declarator->declarator = NULL;
1728 declarator->parameter_pack_p = false;
1729 declarator->id_loc = UNKNOWN_LOCATION;
1730 declarator->init_loc = UNKNOWN_LOCATION;
1732 return declarator;
1735 /* Make a declarator for a generalized identifier. If
1736 QUALIFYING_SCOPE is non-NULL, the identifier is
1737 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1738 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1739 is, if any. */
1741 static cp_declarator *
1742 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1743 special_function_kind sfk, location_t id_location)
1745 cp_declarator *declarator;
1747 /* It is valid to write:
1749 class C { void f(); };
1750 typedef C D;
1751 void D::f();
1753 The standard is not clear about whether `typedef const C D' is
1754 legal; as of 2002-09-15 the committee is considering that
1755 question. EDG 3.0 allows that syntax. Therefore, we do as
1756 well. */
1757 if (qualifying_scope && TYPE_P (qualifying_scope))
1758 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1760 gcc_assert (identifier_p (unqualified_name)
1761 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1762 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1764 declarator = make_declarator (cdk_id);
1765 declarator->u.id.qualifying_scope = qualifying_scope;
1766 declarator->u.id.unqualified_name = unqualified_name;
1767 declarator->u.id.sfk = sfk;
1768 declarator->id_loc = id_location;
1770 return declarator;
1773 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1774 of modifiers such as const or volatile to apply to the pointer
1775 type, represented as identifiers. ATTRIBUTES represent the attributes that
1776 appertain to the pointer or reference. */
1778 cp_declarator *
1779 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1780 tree attributes)
1782 cp_declarator *declarator;
1784 declarator = make_declarator (cdk_pointer);
1785 declarator->declarator = target;
1786 declarator->u.pointer.qualifiers = cv_qualifiers;
1787 declarator->u.pointer.class_type = NULL_TREE;
1788 if (target)
1790 declarator->id_loc = target->id_loc;
1791 declarator->parameter_pack_p = target->parameter_pack_p;
1792 target->parameter_pack_p = false;
1794 else
1795 declarator->parameter_pack_p = false;
1797 declarator->std_attributes = attributes;
1799 return declarator;
1802 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1803 represent the attributes that appertain to the pointer or
1804 reference. */
1806 cp_declarator *
1807 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1808 bool rvalue_ref, tree attributes)
1810 cp_declarator *declarator;
1812 declarator = make_declarator (cdk_reference);
1813 declarator->declarator = target;
1814 declarator->u.reference.qualifiers = cv_qualifiers;
1815 declarator->u.reference.rvalue_ref = rvalue_ref;
1816 if (target)
1818 declarator->id_loc = target->id_loc;
1819 declarator->parameter_pack_p = target->parameter_pack_p;
1820 target->parameter_pack_p = false;
1822 else
1823 declarator->parameter_pack_p = false;
1825 declarator->std_attributes = attributes;
1827 return declarator;
1830 /* Like make_pointer_declarator -- but for a pointer to a non-static
1831 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1832 appertain to the pointer or reference. */
1834 cp_declarator *
1835 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1836 cp_declarator *pointee,
1837 tree attributes)
1839 cp_declarator *declarator;
1841 declarator = make_declarator (cdk_ptrmem);
1842 declarator->declarator = pointee;
1843 declarator->u.pointer.qualifiers = cv_qualifiers;
1844 declarator->u.pointer.class_type = class_type;
1846 if (pointee)
1848 declarator->parameter_pack_p = pointee->parameter_pack_p;
1849 pointee->parameter_pack_p = false;
1851 else
1852 declarator->parameter_pack_p = false;
1854 declarator->std_attributes = attributes;
1856 return declarator;
1859 /* Make a declarator for the function given by TARGET, with the
1860 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1861 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1862 indicates what exceptions can be thrown. STD_ATTRS contains
1863 attributes that appertain to the function type. */
1865 cp_declarator *
1866 make_call_declarator (cp_declarator *target,
1867 tree parms,
1868 cp_cv_quals cv_qualifiers,
1869 cp_virt_specifiers virt_specifiers,
1870 cp_ref_qualifier ref_qualifier,
1871 tree tx_qualifier,
1872 tree exception_specification,
1873 tree late_return_type,
1874 tree requires_clause,
1875 tree std_attrs,
1876 location_t parens_loc)
1878 cp_declarator *declarator;
1880 declarator = make_declarator (cdk_function);
1881 declarator->declarator = target;
1882 declarator->u.function.parameters = parms;
1883 declarator->u.function.qualifiers = cv_qualifiers;
1884 declarator->u.function.virt_specifiers = virt_specifiers;
1885 declarator->u.function.ref_qualifier = ref_qualifier;
1886 declarator->u.function.tx_qualifier = tx_qualifier;
1887 declarator->u.function.exception_specification = exception_specification;
1888 declarator->u.function.late_return_type = late_return_type;
1889 declarator->u.function.requires_clause = requires_clause;
1890 declarator->u.function.parens_loc = parens_loc;
1891 if (target)
1893 declarator->id_loc = target->id_loc;
1894 declarator->parameter_pack_p = target->parameter_pack_p;
1895 target->parameter_pack_p = false;
1897 else
1898 declarator->parameter_pack_p = false;
1900 declarator->std_attributes = std_attrs;
1902 return declarator;
1905 /* Make a declarator for an array of BOUNDS elements, each of which is
1906 defined by ELEMENT. */
1908 cp_declarator *
1909 make_array_declarator (cp_declarator *element, tree bounds)
1911 cp_declarator *declarator;
1913 declarator = make_declarator (cdk_array);
1914 declarator->declarator = element;
1915 declarator->u.array.bounds = bounds;
1916 if (element)
1918 declarator->id_loc = element->id_loc;
1919 declarator->parameter_pack_p = element->parameter_pack_p;
1920 element->parameter_pack_p = false;
1922 else
1923 declarator->parameter_pack_p = false;
1925 return declarator;
1928 /* Determine whether the declarator we've seen so far can be a
1929 parameter pack, when followed by an ellipsis. */
1930 static bool
1931 declarator_can_be_parameter_pack (cp_declarator *declarator)
1933 if (declarator && declarator->parameter_pack_p)
1934 /* We already saw an ellipsis. */
1935 return false;
1937 /* Search for a declarator name, or any other declarator that goes
1938 after the point where the ellipsis could appear in a parameter
1939 pack. If we find any of these, then this declarator cannot be
1940 made into a parameter pack. */
1941 bool found = false;
1942 while (declarator && !found)
1944 switch ((int)declarator->kind)
1946 case cdk_id:
1947 case cdk_array:
1948 case cdk_decomp:
1949 found = true;
1950 break;
1952 case cdk_error:
1953 return true;
1955 default:
1956 declarator = declarator->declarator;
1957 break;
1961 return !found;
1964 cp_parameter_declarator *no_parameters;
1966 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1967 DECLARATOR and DEFAULT_ARGUMENT. */
1969 cp_parameter_declarator *
1970 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1971 cp_declarator *declarator,
1972 tree default_argument,
1973 location_t loc,
1974 bool template_parameter_pack_p = false)
1976 cp_parameter_declarator *parameter;
1978 parameter = ((cp_parameter_declarator *)
1979 alloc_declarator (sizeof (cp_parameter_declarator)));
1980 parameter->next = NULL;
1981 if (decl_specifiers)
1982 parameter->decl_specifiers = *decl_specifiers;
1983 else
1984 clear_decl_specs (&parameter->decl_specifiers);
1985 parameter->declarator = declarator;
1986 parameter->default_argument = default_argument;
1987 parameter->template_parameter_pack_p = template_parameter_pack_p;
1988 parameter->loc = loc;
1990 return parameter;
1993 /* Returns true iff DECLARATOR is a declaration for a function. */
1995 static bool
1996 function_declarator_p (const cp_declarator *declarator)
1998 while (declarator)
2000 if (declarator->kind == cdk_function
2001 && declarator->declarator->kind == cdk_id)
2002 return true;
2003 if (declarator->kind == cdk_id
2004 || declarator->kind == cdk_decomp
2005 || declarator->kind == cdk_error)
2006 return false;
2007 declarator = declarator->declarator;
2009 return false;
2012 /* The parser. */
2014 /* Overview
2015 --------
2017 A cp_parser parses the token stream as specified by the C++
2018 grammar. Its job is purely parsing, not semantic analysis. For
2019 example, the parser breaks the token stream into declarators,
2020 expressions, statements, and other similar syntactic constructs.
2021 It does not check that the types of the expressions on either side
2022 of an assignment-statement are compatible, or that a function is
2023 not declared with a parameter of type `void'.
2025 The parser invokes routines elsewhere in the compiler to perform
2026 semantic analysis and to build up the abstract syntax tree for the
2027 code processed.
2029 The parser (and the template instantiation code, which is, in a
2030 way, a close relative of parsing) are the only parts of the
2031 compiler that should be calling push_scope and pop_scope, or
2032 related functions. The parser (and template instantiation code)
2033 keeps track of what scope is presently active; everything else
2034 should simply honor that. (The code that generates static
2035 initializers may also need to set the scope, in order to check
2036 access control correctly when emitting the initializers.)
2038 Methodology
2039 -----------
2041 The parser is of the standard recursive-descent variety. Upcoming
2042 tokens in the token stream are examined in order to determine which
2043 production to use when parsing a non-terminal. Some C++ constructs
2044 require arbitrary look ahead to disambiguate. For example, it is
2045 impossible, in the general case, to tell whether a statement is an
2046 expression or declaration without scanning the entire statement.
2047 Therefore, the parser is capable of "parsing tentatively." When the
2048 parser is not sure what construct comes next, it enters this mode.
2049 Then, while we attempt to parse the construct, the parser queues up
2050 error messages, rather than issuing them immediately, and saves the
2051 tokens it consumes. If the construct is parsed successfully, the
2052 parser "commits", i.e., it issues any queued error messages and
2053 the tokens that were being preserved are permanently discarded.
2054 If, however, the construct is not parsed successfully, the parser
2055 rolls back its state completely so that it can resume parsing using
2056 a different alternative.
2058 Future Improvements
2059 -------------------
2061 The performance of the parser could probably be improved substantially.
2062 We could often eliminate the need to parse tentatively by looking ahead
2063 a little bit. In some places, this approach might not entirely eliminate
2064 the need to parse tentatively, but it might still speed up the average
2065 case. */
2067 /* Flags that are passed to some parsing functions. These values can
2068 be bitwise-ored together. */
2070 enum
2072 /* No flags. */
2073 CP_PARSER_FLAGS_NONE = 0x0,
2074 /* The construct is optional. If it is not present, then no error
2075 should be issued. */
2076 CP_PARSER_FLAGS_OPTIONAL = 0x1,
2077 /* When parsing a type-specifier, treat user-defined type-names
2078 as non-type identifiers. */
2079 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
2080 /* When parsing a type-specifier, do not try to parse a class-specifier
2081 or enum-specifier. */
2082 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
2083 /* When parsing a decl-specifier-seq, only allow type-specifier or
2084 constexpr. */
2085 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
2086 /* When parsing a decl-specifier-seq, only allow mutable, constexpr or
2087 for C++20 consteval or for C++23 static. */
2088 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10,
2089 /* When parsing a decl-specifier-seq, allow missing typename. */
2090 CP_PARSER_FLAGS_TYPENAME_OPTIONAL = 0x20,
2091 /* When parsing of the noexcept-specifier should be delayed. */
2092 CP_PARSER_FLAGS_DELAY_NOEXCEPT = 0x40,
2093 /* When parsing a consteval declarator. */
2094 CP_PARSER_FLAGS_CONSTEVAL = 0x80,
2095 /* When parsing a parameter declaration. */
2096 CP_PARSER_FLAGS_PARAMETER = 0x100
2099 /* This type is used for parameters and variables which hold
2100 combinations of the above flags. */
2101 typedef int cp_parser_flags;
2103 /* The different kinds of declarators we want to parse. */
2105 enum cp_parser_declarator_kind
2107 /* We want an abstract declarator. */
2108 CP_PARSER_DECLARATOR_ABSTRACT,
2109 /* We want a named declarator. */
2110 CP_PARSER_DECLARATOR_NAMED,
2111 /* We don't mind, but the name must be an unqualified-id. */
2112 CP_PARSER_DECLARATOR_EITHER
2115 /* The precedence values used to parse binary expressions. The minimum value
2116 of PREC must be 1, because zero is reserved to quickly discriminate
2117 binary operators from other tokens. */
2119 enum cp_parser_prec
2121 PREC_NOT_OPERATOR,
2122 PREC_LOGICAL_OR_EXPRESSION,
2123 PREC_LOGICAL_AND_EXPRESSION,
2124 PREC_INCLUSIVE_OR_EXPRESSION,
2125 PREC_EXCLUSIVE_OR_EXPRESSION,
2126 PREC_AND_EXPRESSION,
2127 PREC_EQUALITY_EXPRESSION,
2128 PREC_RELATIONAL_EXPRESSION,
2129 PREC_SPACESHIP_EXPRESSION,
2130 PREC_SHIFT_EXPRESSION,
2131 PREC_ADDITIVE_EXPRESSION,
2132 PREC_MULTIPLICATIVE_EXPRESSION,
2133 PREC_PM_EXPRESSION,
2134 NUM_PREC_VALUES = PREC_PM_EXPRESSION
2137 /* A mapping from a token type to a corresponding tree node type, with a
2138 precedence value. */
2140 struct cp_parser_binary_operations_map_node
2142 /* The token type. */
2143 enum cpp_ttype token_type;
2144 /* The corresponding tree code. */
2145 enum tree_code tree_type;
2146 /* The precedence of this operator. */
2147 enum cp_parser_prec prec;
2150 struct cp_parser_expression_stack_entry
2152 /* Left hand side of the binary operation we are currently
2153 parsing. */
2154 cp_expr lhs;
2155 /* Original tree code for left hand side, if it was a binary
2156 expression itself (used for -Wparentheses). */
2157 enum tree_code lhs_type;
2158 /* Tree code for the binary operation we are parsing. */
2159 enum tree_code tree_type;
2160 /* Precedence of the binary operation we are parsing. */
2161 enum cp_parser_prec prec;
2162 /* Location of the binary operation we are parsing. */
2163 location_t loc;
2164 /* Flags from the operator token. */
2165 unsigned char flags;
2168 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
2169 entries because precedence levels on the stack are monotonically
2170 increasing. */
2171 typedef struct cp_parser_expression_stack_entry
2172 cp_parser_expression_stack[NUM_PREC_VALUES];
2174 /* Used for parsing OMP for loops.
2176 Some notes on flags used for context:
2177 parser->omp_for_parse_state is non-null anywhere inside the OMP FOR
2178 construct, except for the final-loop-body.
2179 The want_nested_loop flag is true if inside a {} sequence where
2180 a loop-nest (or another {} sequence containing a loop-nest) is expected,
2181 but has not yet been seen. It's false when parsing intervening code
2182 statements or their substatements that cannot contain a loop-nest.
2183 The in_intervening_code flag is true when parsing any intervening code,
2184 including substatements, and whether or not want_nested_loop is true.
2186 And, about error handling:
2187 The saw_intervening_code flag is set if the loop is not perfectly
2188 nested, even in the usual case where this is not an error.
2189 perfect_nesting_fail is set if an error has been diagnosed because an
2190 imperfectly-nested loop was found where a perfectly-nested one is
2191 required (we diagnose this only once).
2192 fail is set if any kind of structural error in the loop nest
2193 has been found and diagnosed.
2195 struct omp_for_parse_data {
2196 enum tree_code code;
2197 tree declv, condv, incrv, initv;
2198 tree pre_body;
2199 tree orig_declv;
2200 auto_vec<tree, 4> orig_inits;
2201 int count; /* Expected nesting depth. */
2202 int depth; /* Current nesting depth. */
2203 location_t for_loc;
2204 releasing_vec init_blockv;
2205 releasing_vec body_blockv;
2206 releasing_vec init_placeholderv;
2207 releasing_vec body_placeholderv;
2208 bool ordered : 1;
2209 bool inscan : 1;
2210 bool want_nested_loop : 1;
2211 bool in_intervening_code : 1;
2212 bool saw_intervening_code : 1;
2213 bool perfect_nesting_fail : 1;
2214 bool fail : 1;
2215 tree clauses;
2216 tree *cclauses;
2217 tree ordered_cl;
2220 /* Prototypes. */
2222 /* Constructors and destructors. */
2224 static cp_parser_context *cp_parser_context_new
2225 (cp_parser_context *);
2227 /* Class variables. */
2229 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
2231 /* The operator-precedence table used by cp_parser_binary_expression.
2232 Transformed into an associative array (binops_by_token) by
2233 cp_parser_new. */
2235 static const cp_parser_binary_operations_map_node binops[] = {
2236 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
2237 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
2239 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
2240 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
2241 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
2243 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
2244 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
2246 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
2247 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
2249 { CPP_SPACESHIP, SPACESHIP_EXPR, PREC_SPACESHIP_EXPRESSION },
2251 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
2252 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
2253 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
2254 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
2256 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
2257 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
2259 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
2261 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
2263 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
2265 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
2267 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
2270 /* The same as binops, but initialized by cp_parser_new so that
2271 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
2272 for speed. */
2273 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
2275 /* Constructors and destructors. */
2277 /* Construct a new context. The context below this one on the stack
2278 is given by NEXT. */
2280 static cp_parser_context *
2281 cp_parser_context_new (cp_parser_context* next)
2283 cp_parser_context *context;
2285 /* Allocate the storage. */
2286 if (cp_parser_context_free_list != NULL)
2288 /* Pull the first entry from the free list. */
2289 context = cp_parser_context_free_list;
2290 cp_parser_context_free_list = context->next;
2291 memset (context, 0, sizeof (*context));
2293 else
2294 context = ggc_cleared_alloc<cp_parser_context> ();
2296 /* No errors have occurred yet in this context. */
2297 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
2298 /* If this is not the bottommost context, copy information that we
2299 need from the previous context. */
2300 if (next)
2302 /* If, in the NEXT context, we are parsing an `x->' or `x.'
2303 expression, then we are parsing one in this context, too. */
2304 context->object_type = next->object_type;
2305 /* Thread the stack. */
2306 context->next = next;
2309 return context;
2312 /* Managing the unparsed function queues. */
2314 #define unparsed_funs_with_default_args \
2315 parser->unparsed_queues->last ().funs_with_default_args
2316 #define unparsed_funs_with_definitions \
2317 parser->unparsed_queues->last ().funs_with_definitions
2318 #define unparsed_nsdmis \
2319 parser->unparsed_queues->last ().nsdmis
2320 #define unparsed_noexcepts \
2321 parser->unparsed_queues->last ().noexcepts
2322 #define unparsed_contracts \
2323 parser->unparsed_queues->last ().contracts
2325 static void
2326 push_unparsed_function_queues (cp_parser *parser)
2328 cp_unparsed_functions_entry e
2329 = { NULL, make_tree_vector (), NULL, NULL, NULL };
2330 vec_safe_push (parser->unparsed_queues, e);
2333 static void
2334 pop_unparsed_function_queues (cp_parser *parser)
2336 release_tree_vector (unparsed_funs_with_definitions);
2337 parser->unparsed_queues->pop ();
2340 /* Prototypes. */
2342 /* Routines to parse various constructs.
2344 Those that return `tree' will return the error_mark_node (rather
2345 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2346 Sometimes, they will return an ordinary node if error-recovery was
2347 attempted, even though a parse error occurred. So, to check
2348 whether or not a parse error occurred, you should always use
2349 cp_parser_error_occurred. If the construct is optional (indicated
2350 either by an `_opt' in the name of the function that does the
2351 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2352 the construct is not present. */
2354 /* Lexical conventions [gram.lex] */
2356 static tree finish_userdef_string_literal
2357 (tree);
2359 /* Basic concepts [gram.basic] */
2361 static void cp_parser_translation_unit (cp_parser *);
2363 /* Expressions [gram.expr] */
2365 static cp_expr cp_parser_primary_expression
2366 (cp_parser *, bool, bool, bool, cp_id_kind *);
2367 static cp_expr cp_parser_id_expression
2368 (cp_parser *, bool, bool, bool *, bool, bool);
2369 static cp_expr cp_parser_unqualified_id
2370 (cp_parser *, bool, bool, bool, bool);
2371 static tree cp_parser_nested_name_specifier_opt
2372 (cp_parser *, bool, bool, bool, bool, bool = false);
2373 static tree cp_parser_nested_name_specifier
2374 (cp_parser *, bool, bool, bool, bool);
2375 static tree cp_parser_qualifying_entity
2376 (cp_parser *, bool, bool, bool, bool, bool);
2377 static cp_expr cp_parser_postfix_expression
2378 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2379 static tree cp_parser_postfix_open_square_expression
2380 (cp_parser *, tree, bool, bool);
2381 static tree cp_parser_postfix_dot_deref_expression
2382 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2383 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2384 (cp_parser *, int, bool, bool, bool *, location_t * = NULL,
2385 bool = false);
2386 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2387 enum { non_attr = 0, normal_attr = 1, id_attr = 2, assume_attr = 3,
2388 uneval_string_attr = 4 };
2389 static void cp_parser_pseudo_destructor_name
2390 (cp_parser *, tree, tree *, tree *);
2391 static cp_expr cp_parser_unary_expression
2392 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2393 static enum tree_code cp_parser_unary_operator
2394 (cp_token *);
2395 static tree cp_parser_has_attribute_expression
2396 (cp_parser *);
2397 static tree cp_parser_new_expression
2398 (cp_parser *);
2399 static vec<tree, va_gc> *cp_parser_new_placement
2400 (cp_parser *);
2401 static tree cp_parser_new_type_id
2402 (cp_parser *, tree *);
2403 static cp_declarator *cp_parser_new_declarator_opt
2404 (cp_parser *);
2405 static cp_declarator *cp_parser_direct_new_declarator
2406 (cp_parser *);
2407 static vec<tree, va_gc> *cp_parser_new_initializer
2408 (cp_parser *);
2409 static tree cp_parser_delete_expression
2410 (cp_parser *);
2411 static cp_expr cp_parser_cast_expression
2412 (cp_parser *, bool, bool, bool, cp_id_kind *);
2413 static cp_expr cp_parser_binary_expression
2414 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2415 static tree cp_parser_question_colon_clause
2416 (cp_parser *, cp_expr);
2417 static cp_expr cp_parser_conditional_expression (cp_parser *);
2418 static cp_expr cp_parser_assignment_expression
2419 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2420 static enum tree_code cp_parser_assignment_operator_opt
2421 (cp_parser *);
2422 static cp_expr cp_parser_expression
2423 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2424 static cp_expr cp_parser_constant_expression
2425 (cp_parser *, int = 0, bool * = NULL, bool = false);
2426 static cp_expr cp_parser_builtin_offsetof
2427 (cp_parser *);
2428 static cp_expr cp_parser_lambda_expression
2429 (cp_parser *);
2430 static void cp_parser_lambda_introducer
2431 (cp_parser *, tree);
2432 static bool cp_parser_lambda_declarator_opt
2433 (cp_parser *, tree);
2434 static void cp_parser_lambda_body
2435 (cp_parser *, tree);
2437 /* Statements [gram.stmt.stmt] */
2439 static void cp_parser_statement
2440 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2441 static void cp_parser_label_for_labeled_statement
2442 (cp_parser *, tree);
2443 static tree cp_parser_expression_statement
2444 (cp_parser *, tree);
2445 static tree cp_parser_compound_statement
2446 (cp_parser *, tree, int, bool);
2447 static void cp_parser_statement_seq_opt
2448 (cp_parser *, tree);
2449 static tree cp_parser_selection_statement
2450 (cp_parser *, bool *, vec<tree> *);
2451 static tree cp_parser_condition
2452 (cp_parser *);
2453 static tree cp_parser_iteration_statement
2454 (cp_parser *, bool *, bool, tree, bool);
2455 static bool cp_parser_init_statement
2456 (cp_parser *, tree *decl);
2457 static tree cp_parser_for
2458 (cp_parser *, bool, tree, bool);
2459 static tree cp_parser_c_for
2460 (cp_parser *, tree, tree, bool, tree, bool);
2461 static tree cp_parser_range_for
2462 (cp_parser *, tree, tree, tree, bool, tree, bool, bool);
2463 static void do_range_for_auto_deduction
2464 (tree, tree, cp_decomp *);
2465 static tree cp_parser_perform_range_for_lookup
2466 (tree, tree *, tree *);
2467 static tree cp_parser_range_for_member_function
2468 (tree, tree);
2469 static tree cp_parser_jump_statement
2470 (cp_parser *);
2471 static void cp_parser_declaration_statement
2472 (cp_parser *);
2474 static tree cp_parser_implicitly_scoped_statement
2475 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2476 static void cp_parser_already_scoped_statement
2477 (cp_parser *, bool *, const token_indent_info &);
2479 /* State of module-declaration parsing. */
2480 enum module_parse
2482 MP_NOT_MODULE, /* Not a module. */
2484 _MP_UNUSED,
2486 MP_FIRST, /* First declaration of TU. */
2487 MP_GLOBAL, /* Global Module Fragment. */
2489 MP_PURVIEW_IMPORTS, /* Imports of a module. */
2490 MP_PURVIEW, /* Purview of a named module. */
2492 MP_PRIVATE_IMPORTS, /* Imports of a Private Module Fragment. */
2493 MP_PRIVATE, /* Private Module Fragment. */
2496 static module_parse cp_parser_module_declaration
2497 (cp_parser *parser, module_parse, bool exporting);
2498 static void cp_parser_import_declaration
2499 (cp_parser *parser, module_parse, bool exporting);
2501 /* Declarations [gram.dcl.dcl] */
2503 static void cp_parser_declaration_seq_opt
2504 (cp_parser *);
2505 static void cp_parser_declaration
2506 (cp_parser *, tree);
2507 static void cp_parser_toplevel_declaration
2508 (cp_parser *);
2509 static void cp_parser_block_declaration
2510 (cp_parser *, bool);
2511 static void cp_parser_simple_declaration
2512 (cp_parser *, bool, tree *);
2513 static void cp_parser_decl_specifier_seq
2514 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2515 static tree cp_parser_storage_class_specifier_opt
2516 (cp_parser *);
2517 static tree cp_parser_function_specifier_opt
2518 (cp_parser *, cp_decl_specifier_seq *);
2519 static tree cp_parser_type_specifier
2520 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2521 int *, bool *);
2522 static tree cp_parser_simple_type_specifier
2523 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2524 static tree cp_parser_placeholder_type_specifier
2525 (cp_parser *, location_t, tree, bool);
2526 static tree cp_parser_type_name
2527 (cp_parser *, bool);
2528 static tree cp_parser_nonclass_name
2529 (cp_parser* parser);
2530 static tree cp_parser_elaborated_type_specifier
2531 (cp_parser *, bool, bool);
2532 static tree cp_parser_enum_specifier
2533 (cp_parser *);
2534 static void cp_parser_enumerator_list
2535 (cp_parser *, tree);
2536 static void cp_parser_enumerator_definition
2537 (cp_parser *, tree);
2538 static tree cp_parser_namespace_name
2539 (cp_parser *);
2540 static void cp_parser_namespace_definition
2541 (cp_parser *);
2542 static void cp_parser_namespace_body
2543 (cp_parser *);
2544 static tree cp_parser_qualified_namespace_specifier
2545 (cp_parser *);
2546 static void cp_parser_namespace_alias_definition
2547 (cp_parser *);
2548 static bool cp_parser_using_declaration
2549 (cp_parser *, bool);
2550 static void cp_parser_using_directive
2551 (cp_parser *);
2552 static void cp_parser_using_enum
2553 (cp_parser *);
2554 static tree cp_parser_alias_declaration
2555 (cp_parser *);
2556 static void cp_parser_asm_definition
2557 (cp_parser *);
2558 static void cp_parser_linkage_specification
2559 (cp_parser *, tree);
2560 static void cp_parser_static_assert
2561 (cp_parser *, bool);
2562 static tree cp_parser_decltype
2563 (cp_parser *);
2564 static tree cp_parser_decomposition_declaration
2565 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2567 /* Declarators [gram.dcl.decl] */
2569 static tree cp_parser_init_declarator
2570 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *,
2571 vec<deferred_access_check, va_gc> *, bool, bool, int, bool *, tree *,
2572 location_t *, tree *);
2573 static cp_declarator *cp_parser_declarator
2574 (cp_parser *, cp_parser_declarator_kind, cp_parser_flags, int *, bool *,
2575 bool, bool, bool);
2576 static cp_declarator *cp_parser_direct_declarator
2577 (cp_parser *, cp_parser_declarator_kind, cp_parser_flags, int *, bool, bool,
2578 bool);
2579 static enum tree_code cp_parser_ptr_operator
2580 (cp_parser *, tree *, cp_cv_quals *, tree *);
2581 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2582 (cp_parser *);
2583 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2584 (cp_parser *);
2585 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2586 (cp_parser *);
2587 static tree cp_parser_tx_qualifier_opt
2588 (cp_parser *);
2589 static tree cp_parser_late_return_type_opt
2590 (cp_parser *, cp_declarator *, tree &);
2591 static tree cp_parser_declarator_id
2592 (cp_parser *, bool);
2593 static tree cp_parser_type_id
2594 (cp_parser *, cp_parser_flags = CP_PARSER_FLAGS_NONE, location_t * = NULL);
2595 static tree cp_parser_template_type_arg
2596 (cp_parser *);
2597 static tree cp_parser_trailing_type_id (cp_parser *);
2598 static tree cp_parser_type_id_1
2599 (cp_parser *, cp_parser_flags, bool, bool, location_t *);
2600 static void cp_parser_type_specifier_seq
2601 (cp_parser *, cp_parser_flags, bool, bool, cp_decl_specifier_seq *);
2602 static tree cp_parser_parameter_declaration_clause
2603 (cp_parser *, cp_parser_flags);
2604 static tree cp_parser_parameter_declaration_list
2605 (cp_parser *, cp_parser_flags, auto_vec<tree> *);
2606 static cp_parameter_declarator *cp_parser_parameter_declaration
2607 (cp_parser *, cp_parser_flags, bool, bool *);
2608 static tree cp_parser_default_argument
2609 (cp_parser *, bool);
2610 static void cp_parser_function_body
2611 (cp_parser *, bool);
2612 static tree cp_parser_initializer
2613 (cp_parser *, bool * = nullptr, bool * = nullptr, bool = false);
2614 static cp_expr cp_parser_initializer_clause
2615 (cp_parser *, bool * = nullptr);
2616 static cp_expr cp_parser_braced_list
2617 (cp_parser*, bool * = nullptr);
2618 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2619 (cp_parser *, bool *, bool *);
2621 static void cp_parser_ctor_initializer_opt_and_function_body
2622 (cp_parser *, bool);
2624 static tree cp_parser_late_parsing_omp_declare_simd
2625 (cp_parser *, tree);
2627 static tree cp_parser_late_parsing_oacc_routine
2628 (cp_parser *, tree);
2630 static tree synthesize_implicit_template_parm
2631 (cp_parser *, tree);
2632 static tree finish_fully_implicit_template
2633 (cp_parser *, tree);
2634 static void abort_fully_implicit_template
2635 (cp_parser *);
2637 /* Classes [gram.class] */
2639 static tree cp_parser_class_name
2640 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2641 static tree cp_parser_class_specifier
2642 (cp_parser *);
2643 static tree cp_parser_class_head
2644 (cp_parser *, bool *);
2645 static enum tag_types cp_parser_class_key
2646 (cp_parser *);
2647 static void cp_parser_type_parameter_key
2648 (cp_parser* parser);
2649 static void cp_parser_member_specification_opt
2650 (cp_parser *);
2651 static void cp_parser_member_declaration
2652 (cp_parser *);
2653 static tree cp_parser_pure_specifier
2654 (cp_parser *);
2655 static tree cp_parser_constant_initializer
2656 (cp_parser *);
2658 /* Derived classes [gram.class.derived] */
2660 static tree cp_parser_base_clause
2661 (cp_parser *);
2662 static tree cp_parser_base_specifier
2663 (cp_parser *);
2665 /* Special member functions [gram.special] */
2667 static tree cp_parser_conversion_function_id
2668 (cp_parser *);
2669 static tree cp_parser_conversion_type_id
2670 (cp_parser *);
2671 static cp_declarator *cp_parser_conversion_declarator_opt
2672 (cp_parser *);
2673 static void cp_parser_ctor_initializer_opt
2674 (cp_parser *);
2675 static void cp_parser_mem_initializer_list
2676 (cp_parser *);
2677 static tree cp_parser_mem_initializer
2678 (cp_parser *);
2679 static tree cp_parser_mem_initializer_id
2680 (cp_parser *);
2682 /* Overloading [gram.over] */
2684 static cp_expr cp_parser_operator_function_id
2685 (cp_parser *);
2686 static cp_expr cp_parser_operator
2687 (cp_parser *, location_t);
2689 /* Templates [gram.temp] */
2691 static void cp_parser_template_declaration
2692 (cp_parser *, bool);
2693 static tree cp_parser_template_parameter_list
2694 (cp_parser *);
2695 static tree cp_parser_template_parameter
2696 (cp_parser *, bool *, bool *);
2697 static tree cp_parser_type_parameter
2698 (cp_parser *, bool *);
2699 static tree cp_parser_template_id
2700 (cp_parser *, bool, bool, enum tag_types, bool);
2701 static tree cp_parser_template_id_expr
2702 (cp_parser *, bool, bool, bool);
2703 static tree cp_parser_template_name
2704 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2705 static tree cp_parser_template_argument_list
2706 (cp_parser *);
2707 static tree cp_parser_template_argument
2708 (cp_parser *);
2709 static void cp_parser_explicit_instantiation
2710 (cp_parser *);
2711 static void cp_parser_explicit_specialization
2712 (cp_parser *);
2714 /* Exception handling [gram.except] */
2716 static tree cp_parser_try_block
2717 (cp_parser *);
2718 static void cp_parser_function_try_block
2719 (cp_parser *);
2720 static void cp_parser_handler_seq
2721 (cp_parser *);
2722 static void cp_parser_handler
2723 (cp_parser *);
2724 static tree cp_parser_exception_declaration
2725 (cp_parser *);
2726 static tree cp_parser_throw_expression
2727 (cp_parser *);
2728 static tree cp_parser_exception_specification_opt
2729 (cp_parser *, cp_parser_flags);
2730 static tree cp_parser_type_id_list
2731 (cp_parser *);
2732 static tree cp_parser_noexcept_specification_opt
2733 (cp_parser *, cp_parser_flags, bool, bool *, bool);
2735 /* GNU Extensions */
2737 static tree cp_parser_asm_specification_opt
2738 (cp_parser *);
2739 static tree cp_parser_asm_operand_list
2740 (cp_parser *);
2741 static tree cp_parser_asm_clobber_list
2742 (cp_parser *);
2743 static tree cp_parser_asm_label_list
2744 (cp_parser *);
2745 static bool cp_next_tokens_can_be_attribute_p
2746 (cp_parser *);
2747 static bool cp_next_tokens_can_be_gnu_attribute_p
2748 (cp_parser *);
2749 static bool cp_next_tokens_can_be_std_attribute_p
2750 (cp_parser *);
2751 static bool cp_nth_tokens_can_be_std_attribute_p
2752 (cp_parser *, size_t);
2753 static bool cp_nth_tokens_can_be_gnu_attribute_p
2754 (cp_parser *, size_t);
2755 static bool cp_nth_tokens_can_be_attribute_p
2756 (cp_parser *, size_t);
2757 static tree cp_parser_attributes_opt
2758 (cp_parser *);
2759 static tree cp_parser_gnu_attributes_opt
2760 (cp_parser *);
2761 static tree cp_parser_gnu_attribute_list
2762 (cp_parser *, bool = false);
2763 static tree cp_parser_std_attribute
2764 (cp_parser *, tree);
2765 static tree cp_parser_std_attribute_spec
2766 (cp_parser *);
2767 static tree cp_parser_std_attribute_spec_seq
2768 (cp_parser *);
2769 static size_t cp_parser_skip_std_attribute_spec_seq
2770 (cp_parser *, size_t);
2771 static size_t cp_parser_skip_attributes_opt
2772 (cp_parser *, size_t);
2773 static bool cp_parser_extension_opt
2774 (cp_parser *, int *);
2775 static void cp_parser_label_declaration
2776 (cp_parser *);
2778 /* Concept Extensions */
2780 static tree cp_parser_concept_definition
2781 (cp_parser *);
2782 static tree cp_parser_constraint_expression
2783 (cp_parser *);
2784 static tree cp_parser_requires_clause_opt
2785 (cp_parser *, bool);
2786 static tree cp_parser_requires_expression
2787 (cp_parser *);
2788 static tree cp_parser_requirement_parameter_list
2789 (cp_parser *);
2790 static tree cp_parser_requirement_body
2791 (cp_parser *);
2792 static tree cp_parser_requirement_seq
2793 (cp_parser *);
2794 static tree cp_parser_requirement
2795 (cp_parser *);
2796 static tree cp_parser_simple_requirement
2797 (cp_parser *);
2798 static tree cp_parser_compound_requirement
2799 (cp_parser *);
2800 static tree cp_parser_type_requirement
2801 (cp_parser *);
2802 static tree cp_parser_nested_requirement
2803 (cp_parser *);
2805 /* Transactional Memory Extensions */
2807 static tree cp_parser_transaction
2808 (cp_parser *, cp_token *);
2809 static tree cp_parser_transaction_expression
2810 (cp_parser *, enum rid);
2811 static void cp_parser_function_transaction
2812 (cp_parser *, enum rid);
2813 static tree cp_parser_transaction_cancel
2814 (cp_parser *);
2816 /* Coroutine extensions. */
2818 static tree cp_parser_yield_expression
2819 (cp_parser *);
2821 /* Contracts */
2823 static void cp_parser_late_contract_condition
2824 (cp_parser *, tree, tree);
2826 enum pragma_context {
2827 pragma_external,
2828 pragma_member,
2829 pragma_objc_icode,
2830 pragma_stmt,
2831 pragma_compound
2833 static bool cp_parser_pragma
2834 (cp_parser *, enum pragma_context, bool *);
2836 /* Objective-C++ Productions */
2838 static tree cp_parser_objc_message_receiver
2839 (cp_parser *);
2840 static tree cp_parser_objc_message_args
2841 (cp_parser *);
2842 static tree cp_parser_objc_message_expression
2843 (cp_parser *);
2844 static cp_expr cp_parser_objc_encode_expression
2845 (cp_parser *);
2846 static tree cp_parser_objc_defs_expression
2847 (cp_parser *);
2848 static tree cp_parser_objc_protocol_expression
2849 (cp_parser *);
2850 static tree cp_parser_objc_selector_expression
2851 (cp_parser *);
2852 static cp_expr cp_parser_objc_expression
2853 (cp_parser *);
2854 static bool cp_parser_objc_selector_p
2855 (enum cpp_ttype);
2856 static tree cp_parser_objc_selector
2857 (cp_parser *);
2858 static tree cp_parser_objc_protocol_refs_opt
2859 (cp_parser *);
2860 static void cp_parser_objc_declaration
2861 (cp_parser *, tree);
2862 static tree cp_parser_objc_statement
2863 (cp_parser *);
2864 static bool cp_parser_objc_valid_prefix_attributes
2865 (cp_parser *, tree *);
2866 static void cp_parser_objc_at_property_declaration
2867 (cp_parser *) ;
2868 static void cp_parser_objc_at_synthesize_declaration
2869 (cp_parser *) ;
2870 static void cp_parser_objc_at_dynamic_declaration
2871 (cp_parser *) ;
2872 static tree cp_parser_objc_struct_declaration
2873 (cp_parser *) ;
2875 /* Utility Routines */
2877 static cp_expr cp_parser_lookup_name
2878 (cp_parser *, tree, enum tag_types, int, bool, bool, tree *, location_t);
2879 static tree cp_parser_lookup_name_simple
2880 (cp_parser *, tree, location_t);
2881 static tree cp_parser_maybe_treat_template_as_class
2882 (tree, bool);
2883 static bool cp_parser_check_declarator_template_parameters
2884 (cp_parser *, cp_declarator *, location_t);
2885 static bool cp_parser_check_template_parameters
2886 (cp_parser *, unsigned, bool, location_t, cp_declarator *);
2887 static cp_expr cp_parser_simple_cast_expression
2888 (cp_parser *);
2889 static tree cp_parser_global_scope_opt
2890 (cp_parser *, bool);
2891 static bool cp_parser_constructor_declarator_p
2892 (cp_parser *, cp_parser_flags, bool);
2893 static tree cp_parser_function_definition_from_specifiers_and_declarator
2894 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2895 static tree cp_parser_function_definition_after_declarator
2896 (cp_parser *, bool);
2897 static bool cp_parser_template_declaration_after_export
2898 (cp_parser *, bool);
2899 static void cp_parser_perform_template_parameter_access_checks
2900 (vec<deferred_access_check, va_gc> *);
2901 static tree cp_parser_single_declaration
2902 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2903 static cp_expr cp_parser_functional_cast
2904 (cp_parser *, tree);
2905 static tree cp_parser_save_member_function_body
2906 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2907 static tree cp_parser_save_nsdmi
2908 (cp_parser *);
2909 static tree cp_parser_enclosed_template_argument_list
2910 (cp_parser *);
2911 static void cp_parser_save_default_args
2912 (cp_parser *, tree);
2913 static void cp_parser_late_parsing_for_member
2914 (cp_parser *, tree);
2915 static tree cp_parser_late_parse_one_default_arg
2916 (cp_parser *, tree, tree, tree);
2917 static void cp_parser_late_parsing_nsdmi
2918 (cp_parser *, tree);
2919 static void cp_parser_late_parsing_default_args
2920 (cp_parser *, tree);
2921 static tree cp_parser_sizeof_operand
2922 (cp_parser *, enum rid);
2923 static cp_expr cp_parser_trait
2924 (cp_parser *, const cp_trait *);
2925 static bool cp_parser_declares_only_class_p
2926 (cp_parser *);
2927 static void cp_parser_set_storage_class
2928 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2929 static void cp_parser_set_decl_spec_type
2930 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2931 static void set_and_check_decl_spec_loc
2932 (cp_decl_specifier_seq *decl_specs,
2933 cp_decl_spec ds, cp_token *);
2934 static bool cp_parser_friend_p
2935 (const cp_decl_specifier_seq *);
2936 static void cp_parser_required_error
2937 (cp_parser *, required_token, bool, location_t);
2938 static cp_token *cp_parser_require
2939 (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2940 static cp_token *cp_parser_require_keyword
2941 (cp_parser *, enum rid, required_token);
2942 static bool cp_parser_token_starts_function_definition_p
2943 (cp_token *);
2944 static bool cp_parser_next_token_starts_class_definition_p
2945 (cp_parser *);
2946 static bool cp_parser_next_token_ends_template_argument_p
2947 (cp_parser *);
2948 static bool cp_parser_nth_token_starts_template_argument_list_p
2949 (cp_parser *, size_t);
2950 static enum tag_types cp_parser_token_is_class_key
2951 (cp_token *);
2952 static enum tag_types cp_parser_token_is_type_parameter_key
2953 (cp_token *);
2954 static void cp_parser_maybe_warn_enum_key (cp_parser *, location_t, tree, rid);
2955 static void cp_parser_check_class_key
2956 (cp_parser *, location_t, enum tag_types, tree type, bool, bool);
2957 static void cp_parser_check_access_in_redeclaration
2958 (tree type, location_t location);
2959 static bool cp_parser_optional_template_keyword
2960 (cp_parser *);
2961 static void cp_parser_pre_parsed_nested_name_specifier
2962 (cp_parser *);
2963 static bool cp_parser_cache_group
2964 (cp_parser *, enum cpp_ttype, unsigned);
2965 static tree cp_parser_cache_defarg
2966 (cp_parser *parser, bool nsdmi);
2967 static void cp_parser_parse_tentatively
2968 (cp_parser *);
2969 static void cp_parser_commit_to_tentative_parse
2970 (cp_parser *);
2971 static void cp_parser_commit_to_topmost_tentative_parse
2972 (cp_parser *);
2973 static void cp_parser_abort_tentative_parse
2974 (cp_parser *);
2975 static bool cp_parser_parse_definitely
2976 (cp_parser *);
2977 static inline bool cp_parser_parsing_tentatively
2978 (cp_parser *);
2979 static bool cp_parser_uncommitted_to_tentative_parse_p
2980 (cp_parser *);
2981 static void cp_parser_error
2982 (cp_parser *, const char *);
2983 static void cp_parser_name_lookup_error
2984 (cp_parser *, tree, tree, name_lookup_error, location_t);
2985 static bool cp_parser_simulate_error
2986 (cp_parser *);
2987 static bool cp_parser_check_type_definition
2988 (cp_parser *);
2989 static void cp_parser_check_for_definition_in_return_type
2990 (cp_declarator *, tree, location_t type_location);
2991 static void cp_parser_check_for_invalid_template_id
2992 (cp_parser *, tree, enum tag_types, location_t location);
2993 static bool cp_parser_non_integral_constant_expression
2994 (cp_parser *, non_integral_constant);
2995 static void cp_parser_diagnose_invalid_type_name
2996 (cp_parser *, tree, location_t);
2997 static bool cp_parser_parse_and_diagnose_invalid_type_name
2998 (cp_parser *);
2999 static int cp_parser_skip_to_closing_parenthesis
3000 (cp_parser *, bool, bool, bool);
3001 static void cp_parser_skip_to_end_of_statement
3002 (cp_parser *);
3003 static void cp_parser_consume_semicolon_at_end_of_statement
3004 (cp_parser *);
3005 static void cp_parser_skip_to_end_of_block_or_statement
3006 (cp_parser *);
3007 static bool cp_parser_skip_to_closing_brace
3008 (cp_parser *);
3009 static bool cp_parser_skip_entire_template_parameter_list
3010 (cp_parser *);
3011 static void cp_parser_require_end_of_template_parameter_list
3012 (cp_parser *);
3013 static bool cp_parser_skip_to_end_of_template_parameter_list
3014 (cp_parser *);
3015 static void cp_parser_skip_to_pragma_eol
3016 (cp_parser*, cp_token *);
3017 static bool cp_parser_error_occurred
3018 (cp_parser *);
3019 static bool cp_parser_allow_gnu_extensions_p
3020 (cp_parser *);
3021 static bool cp_parser_is_pure_string_literal
3022 (cp_token *);
3023 static bool cp_parser_is_string_literal
3024 (cp_token *);
3025 static bool cp_parser_is_keyword
3026 (cp_token *, enum rid);
3027 static tree cp_parser_make_typename_type
3028 (cp_parser *, tree, location_t location);
3029 static cp_declarator * cp_parser_make_indirect_declarator
3030 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
3031 static bool cp_parser_compound_literal_p
3032 (cp_parser *);
3033 static bool cp_parser_array_designator_p
3034 (cp_parser *);
3035 static bool cp_parser_init_statement_p
3036 (cp_parser *);
3037 static bool cp_parser_skip_up_to_closing_square_bracket
3038 (cp_parser *);
3039 static bool cp_parser_skip_to_closing_square_bracket
3040 (cp_parser *);
3041 static size_t cp_parser_skip_balanced_tokens (cp_parser *, size_t);
3042 static tree cp_parser_omp_loop_nest (cp_parser *, bool *);
3044 // -------------------------------------------------------------------------- //
3045 // Unevaluated Operand Guard
3047 // Implementation of an RAII helper for unevaluated operand parsing.
3048 cp_unevaluated::cp_unevaluated ()
3050 ++cp_unevaluated_operand;
3051 ++c_inhibit_evaluation_warnings;
3054 cp_unevaluated::~cp_unevaluated ()
3056 --c_inhibit_evaluation_warnings;
3057 --cp_unevaluated_operand;
3060 // -------------------------------------------------------------------------- //
3061 // Tentative Parsing
3063 /* Returns nonzero if we are parsing tentatively. */
3065 static inline bool
3066 cp_parser_parsing_tentatively (cp_parser* parser)
3068 return parser->context->next != NULL;
3071 /* Returns nonzero if TOKEN is a string literal. */
3073 static bool
3074 cp_parser_is_pure_string_literal (cp_token* token)
3076 return (token->type == CPP_STRING ||
3077 token->type == CPP_STRING16 ||
3078 token->type == CPP_STRING32 ||
3079 token->type == CPP_WSTRING ||
3080 token->type == CPP_UTF8STRING);
3083 /* Returns nonzero if TOKEN is a string literal
3084 of a user-defined string literal. */
3086 static bool
3087 cp_parser_is_string_literal (cp_token* token)
3089 return (cp_parser_is_pure_string_literal (token) ||
3090 token->type == CPP_STRING_USERDEF ||
3091 token->type == CPP_STRING16_USERDEF ||
3092 token->type == CPP_STRING32_USERDEF ||
3093 token->type == CPP_WSTRING_USERDEF ||
3094 token->type == CPP_UTF8STRING_USERDEF);
3097 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
3099 static bool
3100 cp_parser_is_keyword (cp_token* token, enum rid keyword)
3102 return token->keyword == keyword;
3105 /* Helper function for cp_parser_error.
3106 Having peeked a token of kind TOK1_KIND that might signify
3107 a conflict marker, peek successor tokens to determine
3108 if we actually do have a conflict marker.
3109 Specifically, we consider a run of 7 '<', '=' or '>' characters
3110 at the start of a line as a conflict marker.
3111 These come through the lexer as three pairs and a single,
3112 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
3113 If it returns true, *OUT_LOC is written to with the location/range
3114 of the marker. */
3116 static bool
3117 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
3118 location_t *out_loc)
3120 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
3121 if (token2->type != tok1_kind)
3122 return false;
3123 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
3124 if (token3->type != tok1_kind)
3125 return false;
3126 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
3127 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
3128 return false;
3130 /* It must be at the start of the line. */
3131 location_t start_loc = cp_lexer_peek_token (lexer)->location;
3132 if (LOCATION_COLUMN (start_loc) != 1)
3133 return false;
3135 /* We have a conflict marker. Construct a location of the form:
3136 <<<<<<<
3137 ^~~~~~~
3138 with start == caret, finishing at the end of the marker. */
3139 location_t finish_loc = get_finish (token4->location);
3140 *out_loc = make_location (start_loc, start_loc, finish_loc);
3142 return true;
3145 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
3146 RT_CLOSE_PAREN. */
3148 static const char *
3149 get_matching_symbol (required_token token_desc)
3151 switch (token_desc)
3153 default:
3154 gcc_unreachable ();
3155 return "";
3156 case RT_CLOSE_BRACE:
3157 return "{";
3158 case RT_CLOSE_PAREN:
3159 return "(";
3163 /* Attempt to convert TOKEN_DESC from a required_token to an
3164 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
3166 static enum cpp_ttype
3167 get_required_cpp_ttype (required_token token_desc)
3169 switch (token_desc)
3171 case RT_SEMICOLON:
3172 return CPP_SEMICOLON;
3173 case RT_OPEN_PAREN:
3174 return CPP_OPEN_PAREN;
3175 case RT_CLOSE_BRACE:
3176 return CPP_CLOSE_BRACE;
3177 case RT_OPEN_BRACE:
3178 return CPP_OPEN_BRACE;
3179 case RT_CLOSE_SQUARE:
3180 return CPP_CLOSE_SQUARE;
3181 case RT_OPEN_SQUARE:
3182 return CPP_OPEN_SQUARE;
3183 case RT_COMMA:
3184 return CPP_COMMA;
3185 case RT_COLON:
3186 return CPP_COLON;
3187 case RT_CLOSE_PAREN:
3188 return CPP_CLOSE_PAREN;
3190 default:
3191 /* Use CPP_EOF as a "no completions possible" code. */
3192 return CPP_EOF;
3197 /* Subroutine of cp_parser_error and cp_parser_required_error.
3199 Issue a diagnostic of the form
3200 FILE:LINE: MESSAGE before TOKEN
3201 where TOKEN is the next token in the input stream. MESSAGE
3202 (specified by the caller) is usually of the form "expected
3203 OTHER-TOKEN".
3205 This bypasses the check for tentative passing, and potentially
3206 adds material needed by cp_parser_required_error.
3208 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
3209 suggesting insertion of the missing token.
3211 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
3212 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
3213 location. */
3215 static void
3216 cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
3217 required_token missing_token_desc,
3218 location_t matching_location)
3220 cp_token *token = cp_lexer_peek_token (parser->lexer);
3221 /* This diagnostic makes more sense if it is tagged to the line
3222 of the token we just peeked at. */
3223 cp_lexer_set_source_position_from_token (token);
3225 if (token->type == CPP_PRAGMA)
3227 error_at (token->location,
3228 "%<#pragma%> is not allowed here");
3229 cp_parser_skip_to_pragma_eol (parser, token);
3230 return;
3233 /* If this is actually a conflict marker, report it as such. */
3234 if (token->type == CPP_LSHIFT
3235 || token->type == CPP_RSHIFT
3236 || token->type == CPP_EQ_EQ)
3238 location_t loc;
3239 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
3241 error_at (loc, "version control conflict marker in file");
3242 expanded_location token_exploc = expand_location (token->location);
3243 /* Consume tokens until the end of the source line. */
3244 for (;;)
3246 cp_lexer_consume_token (parser->lexer);
3247 cp_token *next = cp_lexer_peek_token (parser->lexer);
3248 if (next->type == CPP_EOF)
3249 break;
3250 if (next->location == UNKNOWN_LOCATION
3251 || loc == UNKNOWN_LOCATION)
3252 break;
3254 expanded_location next_exploc = expand_location (next->location);
3255 if (next_exploc.file != token_exploc.file)
3256 break;
3257 if (next_exploc.line != token_exploc.line)
3258 break;
3260 return;
3264 auto_diagnostic_group d;
3265 gcc_rich_location richloc (input_location);
3267 bool added_matching_location = false;
3269 if (missing_token_desc != RT_NONE)
3270 if (cp_token *prev_token = cp_lexer_safe_previous_token (parser->lexer))
3272 /* Potentially supply a fix-it hint, suggesting to add the
3273 missing token immediately after the *previous* token.
3274 This may move the primary location within richloc. */
3275 enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
3276 location_t prev_token_loc = prev_token->location;
3277 maybe_suggest_missing_token_insertion (&richloc, ttype,
3278 prev_token_loc);
3280 /* If matching_location != UNKNOWN_LOCATION, highlight it.
3281 Attempt to consolidate diagnostics by printing it as a
3282 secondary range within the main diagnostic. */
3283 if (matching_location != UNKNOWN_LOCATION)
3284 added_matching_location
3285 = richloc.add_location_if_nearby (matching_location);
3288 /* If we were parsing a string-literal and there is an unknown name
3289 token right after, then check to see if that could also have been
3290 a literal string by checking the name against a list of known
3291 standard string literal constants defined in header files. If
3292 there is one, then add that as an hint to the error message. */
3293 name_hint h;
3294 if (token->type == CPP_NAME)
3295 if (cp_token *prev_token = cp_lexer_safe_previous_token (parser->lexer))
3296 if (cp_parser_is_string_literal (prev_token))
3298 tree name = token->u.value;
3299 const char *token_name = IDENTIFIER_POINTER (name);
3300 const char *header_hint
3301 = get_cp_stdlib_header_for_string_macro_name (token_name);
3302 if (header_hint != NULL)
3303 h = name_hint (NULL, new suggest_missing_header (token->location,
3304 token_name,
3305 header_hint));
3308 /* Actually emit the error. */
3309 c_parse_error (gmsgid,
3310 /* Because c_parser_error does not understand
3311 CPP_KEYWORD, keywords are treated like
3312 identifiers. */
3313 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
3314 token->u.value, token->flags, &richloc);
3316 if (missing_token_desc != RT_NONE)
3318 /* If we weren't able to consolidate matching_location, then
3319 print it as a secondary diagnostic. */
3320 if (matching_location != UNKNOWN_LOCATION
3321 && !added_matching_location)
3322 inform (matching_location, "to match this %qs",
3323 get_matching_symbol (missing_token_desc));
3327 /* If not parsing tentatively, issue a diagnostic of the form
3328 FILE:LINE: MESSAGE before TOKEN
3329 where TOKEN is the next token in the input stream. MESSAGE
3330 (specified by the caller) is usually of the form "expected
3331 OTHER-TOKEN". */
3333 static void
3334 cp_parser_error (cp_parser* parser, const char* gmsgid)
3336 if (!cp_parser_simulate_error (parser))
3337 cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
3340 /* Issue an error about name-lookup failing. NAME is the
3341 IDENTIFIER_NODE DECL is the result of
3342 the lookup (as returned from cp_parser_lookup_name). DESIRED is
3343 the thing that we hoped to find. */
3345 static void
3346 cp_parser_name_lookup_error (cp_parser* parser,
3347 tree name,
3348 tree decl,
3349 name_lookup_error desired,
3350 location_t location)
3352 /* If name lookup completely failed, tell the user that NAME was not
3353 declared. */
3354 if (decl == error_mark_node)
3356 if (parser->scope && parser->scope != global_namespace)
3357 error_at (location, "%<%E::%E%> has not been declared",
3358 parser->scope, name);
3359 else if (parser->scope == global_namespace)
3360 error_at (location, "%<::%E%> has not been declared", name);
3361 else if (parser->object_scope
3362 && !CLASS_TYPE_P (parser->object_scope))
3363 error_at (location, "request for member %qE in non-class type %qT",
3364 name, parser->object_scope);
3365 else if (parser->object_scope)
3366 error_at (location, "%<%T::%E%> has not been declared",
3367 parser->object_scope, name);
3368 else
3369 error_at (location, "%qE has not been declared", name);
3371 else if (parser->scope && parser->scope != global_namespace)
3373 switch (desired)
3375 case NLE_TYPE:
3376 error_at (location, "%<%E::%E%> is not a type",
3377 parser->scope, name);
3378 break;
3379 case NLE_CXX98:
3380 error_at (location, "%<%E::%E%> is not a class or namespace",
3381 parser->scope, name);
3382 break;
3383 case NLE_NOT_CXX98:
3384 error_at (location,
3385 "%<%E::%E%> is not a class, namespace, or enumeration",
3386 parser->scope, name);
3387 break;
3388 default:
3389 gcc_unreachable ();
3393 else if (parser->scope == global_namespace)
3395 switch (desired)
3397 case NLE_TYPE:
3398 error_at (location, "%<::%E%> is not a type", name);
3399 break;
3400 case NLE_CXX98:
3401 error_at (location, "%<::%E%> is not a class or namespace", name);
3402 break;
3403 case NLE_NOT_CXX98:
3404 error_at (location,
3405 "%<::%E%> is not a class, namespace, or enumeration",
3406 name);
3407 break;
3408 default:
3409 gcc_unreachable ();
3412 else
3414 switch (desired)
3416 case NLE_TYPE:
3417 error_at (location, "%qE is not a type", name);
3418 break;
3419 case NLE_CXX98:
3420 error_at (location, "%qE is not a class or namespace", name);
3421 break;
3422 case NLE_NOT_CXX98:
3423 error_at (location,
3424 "%qE is not a class, namespace, or enumeration", name);
3425 break;
3426 default:
3427 gcc_unreachable ();
3432 /* If we are parsing tentatively, remember that an error has occurred
3433 during this tentative parse. Returns true if the error was
3434 simulated; false if a message should be issued by the caller. */
3436 static bool
3437 cp_parser_simulate_error (cp_parser* parser)
3439 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3441 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3442 return true;
3444 return false;
3447 /* This function is called when a type is defined. If type
3448 definitions are forbidden at this point, an error message is
3449 issued. */
3451 static bool
3452 cp_parser_check_type_definition (cp_parser* parser)
3454 /* If types are forbidden here, issue a message. */
3455 if (parser->type_definition_forbidden_message)
3457 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3458 or %qs in the message need to be interpreted. */
3459 error (parser->type_definition_forbidden_message,
3460 parser->type_definition_forbidden_message_arg);
3461 return false;
3463 return true;
3466 /* This function is called when the DECLARATOR is processed. The TYPE
3467 was a type defined in the decl-specifiers. If it is invalid to
3468 define a type in the decl-specifiers for DECLARATOR, an error is
3469 issued. TYPE_LOCATION is the location of TYPE and is used
3470 for error reporting. */
3472 static void
3473 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3474 tree type, location_t type_location)
3476 /* [dcl.fct] forbids type definitions in return types.
3477 Unfortunately, it's not easy to know whether or not we are
3478 processing a return type until after the fact. */
3479 while (declarator
3480 && (declarator->kind == cdk_pointer
3481 || declarator->kind == cdk_reference
3482 || declarator->kind == cdk_ptrmem))
3483 declarator = declarator->declarator;
3484 if (declarator
3485 && declarator->kind == cdk_function)
3487 error_at (type_location,
3488 "new types may not be defined in a return type");
3489 inform (type_location,
3490 "(perhaps a semicolon is missing after the definition of %qT)",
3491 type);
3495 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3496 "<" in any valid C++ program. If the next token is indeed "<",
3497 issue a message warning the user about what appears to be an
3498 invalid attempt to form a template-id. LOCATION is the location
3499 of the type-specifier (TYPE) */
3501 static void
3502 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3503 tree type,
3504 enum tag_types tag_type,
3505 location_t location)
3507 cp_token_position start = 0;
3509 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3511 if (TREE_CODE (type) == TYPE_DECL)
3512 type = TREE_TYPE (type);
3513 if (TYPE_P (type) && !template_placeholder_p (type))
3514 error_at (location, "%qT is not a template", type);
3515 else if (identifier_p (type))
3517 if (tag_type != none_type)
3518 error_at (location, "%qE is not a class template", type);
3519 else
3520 error_at (location, "%qE is not a template", type);
3522 else
3523 error_at (location, "invalid template-id");
3524 /* Remember the location of the invalid "<". */
3525 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3526 start = cp_lexer_token_position (parser->lexer, true);
3527 /* Consume the "<". */
3528 cp_lexer_consume_token (parser->lexer);
3529 /* Parse the template arguments. */
3530 cp_parser_enclosed_template_argument_list (parser);
3531 /* Permanently remove the invalid template arguments so that
3532 this error message is not issued again. */
3533 if (start)
3534 cp_lexer_purge_tokens_after (parser->lexer, start);
3538 /* If parsing an integral constant-expression, issue an error message
3539 about the fact that THING appeared and return true. Otherwise,
3540 return false. In either case, set
3541 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3543 static bool
3544 cp_parser_non_integral_constant_expression (cp_parser *parser,
3545 non_integral_constant thing)
3547 parser->non_integral_constant_expression_p = true;
3548 if (parser->integral_constant_expression_p)
3550 if (!parser->allow_non_integral_constant_expression_p)
3552 const char *msg = NULL;
3553 switch (thing)
3555 case NIC_FLOAT:
3556 pedwarn (input_location, OPT_Wpedantic,
3557 "ISO C++ forbids using a floating-point literal "
3558 "in a constant-expression");
3559 return true;
3560 case NIC_CAST:
3561 error ("a cast to a type other than an integral or "
3562 "enumeration type cannot appear in a "
3563 "constant-expression");
3564 return true;
3565 case NIC_TYPEID:
3566 error ("%<typeid%> operator "
3567 "cannot appear in a constant-expression");
3568 return true;
3569 case NIC_NCC:
3570 error ("non-constant compound literals "
3571 "cannot appear in a constant-expression");
3572 return true;
3573 case NIC_FUNC_CALL:
3574 error ("a function call "
3575 "cannot appear in a constant-expression");
3576 return true;
3577 case NIC_INC:
3578 error ("an increment "
3579 "cannot appear in a constant-expression");
3580 return true;
3581 case NIC_DEC:
3582 error ("an decrement "
3583 "cannot appear in a constant-expression");
3584 return true;
3585 case NIC_ARRAY_REF:
3586 error ("an array reference "
3587 "cannot appear in a constant-expression");
3588 return true;
3589 case NIC_ADDR_LABEL:
3590 error ("the address of a label "
3591 "cannot appear in a constant-expression");
3592 return true;
3593 case NIC_OVERLOADED:
3594 error ("calls to overloaded operators "
3595 "cannot appear in a constant-expression");
3596 return true;
3597 case NIC_ASSIGNMENT:
3598 error ("an assignment cannot appear in a constant-expression");
3599 return true;
3600 case NIC_COMMA:
3601 error ("a comma operator "
3602 "cannot appear in a constant-expression");
3603 return true;
3604 case NIC_CONSTRUCTOR:
3605 error ("a call to a constructor "
3606 "cannot appear in a constant-expression");
3607 return true;
3608 case NIC_TRANSACTION:
3609 error ("a transaction expression "
3610 "cannot appear in a constant-expression");
3611 return true;
3612 case NIC_THIS:
3613 msg = "this";
3614 break;
3615 case NIC_FUNC_NAME:
3616 msg = "__FUNCTION__";
3617 break;
3618 case NIC_PRETTY_FUNC:
3619 msg = "__PRETTY_FUNCTION__";
3620 break;
3621 case NIC_C99_FUNC:
3622 msg = "__func__";
3623 break;
3624 case NIC_VA_ARG:
3625 msg = "va_arg";
3626 break;
3627 case NIC_ARROW:
3628 msg = "->";
3629 break;
3630 case NIC_POINT:
3631 msg = ".";
3632 break;
3633 case NIC_STAR:
3634 msg = "*";
3635 break;
3636 case NIC_ADDR:
3637 msg = "&";
3638 break;
3639 case NIC_PREINCREMENT:
3640 msg = "++";
3641 break;
3642 case NIC_PREDECREMENT:
3643 msg = "--";
3644 break;
3645 case NIC_NEW:
3646 msg = "new";
3647 break;
3648 case NIC_DEL:
3649 msg = "delete";
3650 break;
3651 default:
3652 gcc_unreachable ();
3654 if (msg)
3655 error ("%qs cannot appear in a constant-expression", msg);
3656 return true;
3659 return false;
3662 /* Emit a diagnostic for an invalid type name. This function commits
3663 to the current active tentative parse, if any. (Otherwise, the
3664 problematic construct might be encountered again later, resulting
3665 in duplicate error messages.) LOCATION is the location of ID. */
3667 static void
3668 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3669 location_t location)
3671 tree decl, ambiguous_decls;
3672 cp_parser_commit_to_tentative_parse (parser);
3673 /* Try to lookup the identifier. */
3674 decl = cp_parser_lookup_name (parser, id, none_type,
3675 /*is_template=*/false,
3676 /*is_namespace=*/false,
3677 /*check_dependency=*/true,
3678 &ambiguous_decls, location);
3679 if (ambiguous_decls)
3680 /* If the lookup was ambiguous, an error will already have
3681 been issued. */
3682 return;
3683 /* If the lookup found a template-name, it means that the user forgot
3684 to specify an argument list. Emit a useful error message. */
3685 if (DECL_TYPE_TEMPLATE_P (decl))
3687 auto_diagnostic_group d;
3688 error_at (location,
3689 "invalid use of template-name %qE without an argument list",
3690 decl);
3691 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3692 inform (location, "class template argument deduction is only available "
3693 "with %<-std=c++17%> or %<-std=gnu++17%>");
3694 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3696 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3697 error_at (location, "invalid use of destructor %qD as a type", id);
3698 else if (TREE_CODE (decl) == TYPE_DECL)
3699 /* Something like 'unsigned A a;' */
3700 error_at (location, "invalid combination of multiple type-specifiers");
3701 else if (!parser->scope)
3703 /* Issue an error message. */
3704 auto_diagnostic_group d;
3705 name_hint hint;
3706 if (TREE_CODE (id) == IDENTIFIER_NODE)
3707 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3708 if (const char *suggestion = hint.suggestion ())
3710 gcc_rich_location richloc (location);
3711 richloc.add_fixit_replace (suggestion);
3712 error_at (&richloc,
3713 "%qE does not name a type; did you mean %qs?",
3714 id, suggestion);
3716 else
3717 error_at (location, "%qE does not name a type", id);
3718 /* If we're in a template class, it's possible that the user was
3719 referring to a type from a base class. For example:
3721 template <typename T> struct A { typedef T X; };
3722 template <typename T> struct B : public A<T> { X x; };
3724 The user should have said "typename A<T>::X". */
3725 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3726 inform (location, "C++11 %<constexpr%> only available with "
3727 "%<-std=c++11%> or %<-std=gnu++11%>");
3728 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3729 inform (location, "C++11 %<noexcept%> only available with "
3730 "%<-std=c++11%> or %<-std=gnu++11%>");
3731 else if (TREE_CODE (id) == IDENTIFIER_NODE
3732 && (id_equal (id, "module") || id_equal (id, "import")))
3734 if (modules_p ())
3735 inform (location, "%qE is not recognized as a module control-line",
3736 id);
3737 else if (cxx_dialect < cxx20)
3738 inform (location, "C++20 %qE only available with %<-fmodules-ts%>",
3739 id);
3740 else
3741 inform (location, "C++20 %qE only available with %<-fmodules-ts%>"
3742 ", which is not yet enabled with %<-std=c++20%>", id);
3744 else if (cxx_dialect < cxx11
3745 && TREE_CODE (id) == IDENTIFIER_NODE
3746 && id_equal (id, "thread_local"))
3747 inform (location, "C++11 %<thread_local%> only available with "
3748 "%<-std=c++11%> or %<-std=gnu++11%>");
3749 else if (cxx_dialect < cxx20 && id == ridpointers[(int)RID_CONSTINIT])
3750 inform (location, "C++20 %<constinit%> only available with "
3751 "%<-std=c++20%> or %<-std=gnu++20%>");
3752 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3753 inform (location, "%<concept%> only available with %<-std=c++20%> or "
3754 "%<-fconcepts%>");
3755 else if (!flag_concepts && id == ridpointers[(int)RID_REQUIRES])
3756 inform (location, "%<requires%> only available with %<-std=c++20%> or "
3757 "%<-fconcepts%>");
3758 else if (processing_template_decl && current_class_type
3759 && TYPE_BINFO (current_class_type))
3761 for (tree b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3762 b; b = TREE_CHAIN (b))
3764 tree base_type = BINFO_TYPE (b);
3765 if (CLASS_TYPE_P (base_type)
3766 && dependent_type_p (base_type))
3768 /* Go from a particular instantiation of the
3769 template (which will have an empty TYPE_FIELDs),
3770 to the main version. */
3771 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3772 for (tree field = TYPE_FIELDS (base_type);
3773 field; field = DECL_CHAIN (field))
3774 if (TREE_CODE (field) == TYPE_DECL
3775 && DECL_NAME (field) == id)
3777 inform (location,
3778 "(perhaps %<typename %T::%E%> was intended)",
3779 BINFO_TYPE (b), id);
3780 goto found;
3784 found:;
3787 /* Here we diagnose qualified-ids where the scope is actually correct,
3788 but the identifier does not resolve to a valid type name. */
3789 else if (parser->scope != error_mark_node)
3791 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3793 auto_diagnostic_group d;
3794 name_hint hint;
3795 if (decl == error_mark_node)
3796 hint = suggest_alternative_in_explicit_scope (location, id,
3797 parser->scope);
3798 const char *suggestion = hint.suggestion ();
3799 gcc_rich_location richloc (location_of (id));
3800 if (suggestion)
3801 richloc.add_fixit_replace (suggestion);
3802 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3804 if (suggestion)
3805 error_at (&richloc,
3806 "%qE in namespace %qE does not name a template"
3807 " type; did you mean %qs?",
3808 id, parser->scope, suggestion);
3809 else
3810 error_at (&richloc,
3811 "%qE in namespace %qE does not name a template type",
3812 id, parser->scope);
3814 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3816 if (suggestion)
3817 error_at (&richloc,
3818 "%qE in namespace %qE does not name a template"
3819 " type; did you mean %qs?",
3820 TREE_OPERAND (id, 0), parser->scope, suggestion);
3821 else
3822 error_at (&richloc,
3823 "%qE in namespace %qE does not name a template"
3824 " type",
3825 TREE_OPERAND (id, 0), parser->scope);
3827 else
3829 if (suggestion)
3830 error_at (&richloc,
3831 "%qE in namespace %qE does not name a type"
3832 "; did you mean %qs?",
3833 id, parser->scope, suggestion);
3834 else
3835 error_at (&richloc,
3836 "%qE in namespace %qE does not name a type",
3837 id, parser->scope);
3839 if (DECL_P (decl))
3840 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3842 else if (CLASS_TYPE_P (parser->scope)
3843 && constructor_name_p (id, parser->scope))
3845 /* A<T>::A<T>() */
3846 auto_diagnostic_group d;
3847 error_at (location, "%<%T::%E%> names the constructor, not"
3848 " the type", parser->scope, id);
3849 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3850 error_at (location, "and %qT has no template constructors",
3851 parser->scope);
3853 else if (TYPE_P (parser->scope)
3854 && dependent_scope_p (parser->scope))
3856 gcc_rich_location richloc (location);
3857 richloc.add_fixit_insert_before ("typename ");
3858 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3859 error_at (&richloc,
3860 "need %<typename%> before %<%T::%D::%E%> because "
3861 "%<%T::%D%> is a dependent scope",
3862 TYPE_CONTEXT (parser->scope),
3863 TYPENAME_TYPE_FULLNAME (parser->scope),
3865 TYPE_CONTEXT (parser->scope),
3866 TYPENAME_TYPE_FULLNAME (parser->scope));
3867 else
3868 error_at (&richloc, "need %<typename%> before %<%T::%E%> because "
3869 "%qT is a dependent scope",
3870 parser->scope, id, parser->scope);
3872 else if (TYPE_P (parser->scope))
3874 auto_diagnostic_group d;
3875 if (!COMPLETE_TYPE_P (parser->scope))
3876 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3877 parser->scope);
3878 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3879 error_at (location_of (id),
3880 "%qE in %q#T does not name a template type",
3881 id, parser->scope);
3882 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3883 error_at (location_of (id),
3884 "%qE in %q#T does not name a template type",
3885 TREE_OPERAND (id, 0), parser->scope);
3886 else
3887 error_at (location_of (id),
3888 "%qE in %q#T does not name a type",
3889 id, parser->scope);
3890 if (DECL_P (decl))
3891 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3893 else
3894 gcc_unreachable ();
3898 /* Check for a common situation where a type-name should be present,
3899 but is not, and issue a sensible error message. Returns true if an
3900 invalid type-name was detected.
3902 The situation handled by this function are variable declarations of the
3903 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3904 Usually, `ID' should name a type, but if we got here it means that it
3905 does not. We try to emit the best possible error message depending on
3906 how exactly the id-expression looks like. */
3908 static bool
3909 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3911 tree id;
3912 cp_token *token = cp_lexer_peek_token (parser->lexer);
3914 /* Avoid duplicate error about ambiguous lookup. */
3915 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3917 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3918 if (next->type == CPP_NAME && next->error_reported)
3919 goto out;
3922 cp_parser_parse_tentatively (parser);
3923 id = cp_parser_id_expression (parser,
3924 /*template_keyword_p=*/false,
3925 /*check_dependency_p=*/true,
3926 /*template_p=*/NULL,
3927 /*declarator_p=*/true,
3928 /*optional_p=*/false);
3929 /* If the next token is a (, this is a function with no explicit return
3930 type, i.e. constructor, destructor or conversion op. */
3931 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3932 || TREE_CODE (id) == TYPE_DECL)
3934 cp_parser_abort_tentative_parse (parser);
3935 return false;
3937 if (!cp_parser_parse_definitely (parser))
3938 return false;
3940 /* Emit a diagnostic for the invalid type. */
3941 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3942 out:
3943 /* If we aren't in the middle of a declarator (i.e. in a
3944 parameter-declaration-clause), skip to the end of the declaration;
3945 there's no point in trying to process it. */
3946 if (!parser->in_declarator_p)
3947 cp_parser_skip_to_end_of_block_or_statement (parser);
3948 return true;
3951 /* Consume tokens up to, and including, the next non-nested closing `)'.
3952 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3953 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3954 found an unnested token of that type. */
3956 static int
3957 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3958 bool recovering,
3959 cpp_ttype or_ttype,
3960 bool consume_paren)
3962 unsigned paren_depth = 0;
3963 unsigned brace_depth = 0;
3964 unsigned square_depth = 0;
3965 unsigned condop_depth = 0;
3967 if (recovering && or_ttype == CPP_EOF
3968 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3969 return 0;
3971 while (true)
3973 cp_token * token = cp_lexer_peek_token (parser->lexer);
3975 /* Have we found what we're looking for before the closing paren? */
3976 if (token->type == or_ttype && or_ttype != CPP_EOF
3977 && !brace_depth && !paren_depth && !square_depth && !condop_depth)
3978 return -1;
3980 switch (token->type)
3982 case CPP_PRAGMA_EOL:
3983 if (!parser->lexer->in_pragma)
3984 break;
3985 /* FALLTHRU */
3986 case CPP_EOF:
3987 /* If we've run out of tokens, then there is no closing `)'. */
3988 return 0;
3990 /* This is good for lambda expression capture-lists. */
3991 case CPP_OPEN_SQUARE:
3992 ++square_depth;
3993 break;
3994 case CPP_CLOSE_SQUARE:
3995 if (!square_depth--)
3996 return 0;
3997 break;
3999 case CPP_SEMICOLON:
4000 /* This matches the processing in skip_to_end_of_statement. */
4001 if (!brace_depth)
4002 return 0;
4003 break;
4005 case CPP_OPEN_BRACE:
4006 ++brace_depth;
4007 break;
4008 case CPP_CLOSE_BRACE:
4009 if (!brace_depth--)
4010 return 0;
4011 break;
4013 case CPP_OPEN_PAREN:
4014 if (!brace_depth)
4015 ++paren_depth;
4016 break;
4018 case CPP_CLOSE_PAREN:
4019 if (!brace_depth && !paren_depth--)
4021 if (consume_paren)
4022 cp_lexer_consume_token (parser->lexer);
4023 return 1;
4025 break;
4027 case CPP_QUERY:
4028 if (!brace_depth && !paren_depth && !square_depth)
4029 ++condop_depth;
4030 break;
4032 case CPP_COLON:
4033 if (!brace_depth && !paren_depth && !square_depth && condop_depth > 0)
4034 condop_depth--;
4035 break;
4037 case CPP_KEYWORD:
4038 if (!cp_token_is_module_directive (token))
4039 break;
4040 /* FALLTHROUGH */
4042 case CPP_PRAGMA:
4043 /* We fell into a pragma. Skip it, and continue. */
4044 cp_parser_skip_to_pragma_eol (parser, recovering ? token : nullptr);
4045 continue;
4047 default:
4048 break;
4051 /* Consume the token. */
4052 cp_lexer_consume_token (parser->lexer);
4056 /* Consume tokens up to, and including, the next non-nested closing `)'.
4057 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
4058 are doing error recovery. Returns -1 if OR_COMMA is true and we
4059 found an unnested token of that type. */
4061 static int
4062 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
4063 bool recovering,
4064 bool or_comma,
4065 bool consume_paren)
4067 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
4068 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
4069 ttype, consume_paren);
4072 /* Consume tokens until we reach the end of the current statement.
4073 Normally, that will be just before consuming a `;'. However, if a
4074 non-nested `}' comes first, then we stop before consuming that. */
4076 static void
4077 cp_parser_skip_to_end_of_statement (cp_parser* parser)
4079 unsigned nesting_depth = 0;
4081 /* Unwind generic function template scope if necessary. */
4082 if (parser->fully_implicit_function_template_p)
4083 abort_fully_implicit_template (parser);
4085 while (true)
4087 cp_token *token = cp_lexer_peek_token (parser->lexer);
4089 switch (token->type)
4091 case CPP_PRAGMA_EOL:
4092 if (!parser->lexer->in_pragma)
4093 break;
4094 /* FALLTHRU */
4095 case CPP_EOF:
4096 /* If we've run out of tokens, stop. */
4097 return;
4099 case CPP_SEMICOLON:
4100 /* If the next token is a `;', we have reached the end of the
4101 statement. */
4102 if (!nesting_depth)
4103 return;
4104 break;
4106 case CPP_CLOSE_BRACE:
4107 /* If this is a non-nested '}', stop before consuming it.
4108 That way, when confronted with something like:
4110 { 3 + }
4112 we stop before consuming the closing '}', even though we
4113 have not yet reached a `;'. */
4114 if (nesting_depth == 0)
4115 return;
4117 /* If it is the closing '}' for a block that we have
4118 scanned, stop -- but only after consuming the token.
4119 That way given:
4121 void f g () { ... }
4122 typedef int I;
4124 we will stop after the body of the erroneously declared
4125 function, but before consuming the following `typedef'
4126 declaration. */
4127 if (--nesting_depth == 0)
4129 cp_lexer_consume_token (parser->lexer);
4130 return;
4132 break;
4134 case CPP_OPEN_BRACE:
4135 ++nesting_depth;
4136 break;
4138 case CPP_KEYWORD:
4139 if (!cp_token_is_module_directive (token))
4140 break;
4141 /* FALLTHROUGH */
4143 case CPP_PRAGMA:
4144 /* We fell into a pragma. Skip it, and continue or return. */
4145 cp_parser_skip_to_pragma_eol (parser, token);
4146 if (!nesting_depth)
4147 return;
4148 continue;
4150 default:
4151 break;
4154 /* Consume the token. */
4155 cp_lexer_consume_token (parser->lexer);
4159 /* This function is called at the end of a statement or declaration.
4160 If the next token is a semicolon, it is consumed; otherwise, error
4161 recovery is attempted. */
4163 static void
4164 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
4166 /* Look for the trailing `;'. */
4167 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
4169 /* If there is additional (erroneous) input, skip to the end of
4170 the statement. */
4171 cp_parser_skip_to_end_of_statement (parser);
4172 /* If the next token is now a `;', consume it. */
4173 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
4174 cp_lexer_consume_token (parser->lexer);
4178 /* Skip tokens until we have consumed an entire block, or until we
4179 have consumed a non-nested `;'. */
4181 static void
4182 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
4184 int nesting_depth = 0;
4186 /* Unwind generic function template scope if necessary. */
4187 if (parser->fully_implicit_function_template_p)
4188 abort_fully_implicit_template (parser);
4190 while (nesting_depth >= 0)
4192 cp_token *token = cp_lexer_peek_token (parser->lexer);
4194 switch (token->type)
4196 case CPP_PRAGMA_EOL:
4197 if (!parser->lexer->in_pragma)
4198 break;
4199 /* FALLTHRU */
4200 case CPP_EOF:
4201 /* If we've run out of tokens, stop. */
4202 return;
4204 case CPP_SEMICOLON:
4205 /* Stop if this is an unnested ';'. */
4206 if (!nesting_depth)
4207 nesting_depth = -1;
4208 break;
4210 case CPP_CLOSE_BRACE:
4211 /* Stop if this is an unnested '}', or closes the outermost
4212 nesting level. */
4213 nesting_depth--;
4214 if (nesting_depth < 0)
4215 return;
4216 if (!nesting_depth)
4217 nesting_depth = -1;
4218 break;
4220 case CPP_OPEN_BRACE:
4221 /* Nest. */
4222 nesting_depth++;
4223 break;
4225 case CPP_KEYWORD:
4226 if (!cp_token_is_module_directive (token))
4227 break;
4228 /* FALLTHROUGH */
4230 case CPP_PRAGMA:
4231 /* Skip it, and continue or return. */
4232 cp_parser_skip_to_pragma_eol (parser, token);
4233 if (!nesting_depth)
4234 return;
4235 continue;
4237 default:
4238 break;
4241 /* Consume the token. */
4242 cp_lexer_consume_token (parser->lexer);
4246 /* Skip tokens until a non-nested closing curly brace is the next
4247 token, or there are no more tokens. Return true in the first case,
4248 false otherwise. */
4250 static bool
4251 cp_parser_skip_to_closing_brace (cp_parser *parser)
4253 unsigned nesting_depth = 0;
4255 while (true)
4257 cp_token *token = cp_lexer_peek_token (parser->lexer);
4259 switch (token->type)
4261 case CPP_PRAGMA_EOL:
4262 if (!parser->lexer->in_pragma)
4263 break;
4264 /* FALLTHRU */
4265 case CPP_EOF:
4266 /* If we've run out of tokens, stop. */
4267 return false;
4269 case CPP_CLOSE_BRACE:
4270 /* If the next token is a non-nested `}', then we have reached
4271 the end of the current block. */
4272 if (nesting_depth-- == 0)
4273 return true;
4274 break;
4276 case CPP_OPEN_BRACE:
4277 /* If it the next token is a `{', then we are entering a new
4278 block. Consume the entire block. */
4279 ++nesting_depth;
4280 break;
4282 default:
4283 break;
4286 /* Consume the token. */
4287 cp_lexer_consume_token (parser->lexer);
4291 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
4292 parameter is the PRAGMA token, allowing us to purge the entire pragma
4293 sequence. PRAGMA_TOK can be NULL, if we're speculatively scanning
4294 forwards (not error recovery). */
4296 static void
4297 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
4299 cp_token *token;
4303 /* The preprocessor makes sure that a PRAGMA_EOL token appears
4304 before an EOF token, even when the EOF is on the pragma line.
4305 We should never get here without being inside a deferred
4306 pragma. */
4307 gcc_checking_assert (cp_lexer_next_token_is_not (parser->lexer, CPP_EOF));
4308 token = cp_lexer_consume_token (parser->lexer);
4310 while (token->type != CPP_PRAGMA_EOL);
4312 if (pragma_tok)
4314 parser->lexer->in_pragma = false;
4315 if (parser->lexer->in_omp_attribute_pragma
4316 && cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4318 parser->lexer = parser->lexer->next;
4319 /* Put the current source position back where it was before this
4320 lexer was pushed. */
4321 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4326 /* Require pragma end of line, resyncing with it as necessary. The
4327 arguments are as for cp_parser_skip_to_pragma_eol. */
4329 static void
4330 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
4332 parser->lexer->in_pragma = false;
4333 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
4334 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
4335 else if (parser->lexer->in_omp_attribute_pragma
4336 && cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4338 parser->lexer = parser->lexer->next;
4339 /* Put the current source position back where it was before this
4340 lexer was pushed. */
4341 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4345 /* This is a simple wrapper around make_typename_type. When the id is
4346 an unresolved identifier node, we can provide a superior diagnostic
4347 using cp_parser_diagnose_invalid_type_name. */
4349 static tree
4350 cp_parser_make_typename_type (cp_parser *parser, tree id,
4351 location_t id_location)
4353 tree result;
4354 if (identifier_p (id))
4356 result = make_typename_type (parser->scope, id, typename_type,
4357 /*complain=*/tf_none);
4358 if (result == error_mark_node)
4359 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
4360 return result;
4362 return make_typename_type (parser->scope, id, typename_type, tf_error);
4365 /* This is a wrapper around the
4366 make_{pointer,ptrmem,reference}_declarator functions that decides
4367 which one to call based on the CODE and CLASS_TYPE arguments. The
4368 CODE argument should be one of the values returned by
4369 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
4370 appertain to the pointer or reference. */
4372 static cp_declarator *
4373 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
4374 cp_cv_quals cv_qualifiers,
4375 cp_declarator *target,
4376 tree attributes)
4378 if (code == ERROR_MARK || target == cp_error_declarator)
4379 return cp_error_declarator;
4381 if (code == INDIRECT_REF)
4382 if (class_type == NULL_TREE)
4383 return make_pointer_declarator (cv_qualifiers, target, attributes);
4384 else
4385 return make_ptrmem_declarator (cv_qualifiers, class_type,
4386 target, attributes);
4387 else if (code == ADDR_EXPR && class_type == NULL_TREE)
4388 return make_reference_declarator (cv_qualifiers, target,
4389 false, attributes);
4390 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
4391 return make_reference_declarator (cv_qualifiers, target,
4392 true, attributes);
4393 gcc_unreachable ();
4396 /* Create a new C++ parser. */
4398 static cp_parser *
4399 cp_parser_new (cp_lexer *lexer)
4401 /* Initialize the binops_by_token so that we can get the tree
4402 directly from the token. */
4403 for (unsigned i = 0; i < ARRAY_SIZE (binops); i++)
4404 binops_by_token[binops[i].token_type] = binops[i];
4406 cp_parser *parser = ggc_cleared_alloc<cp_parser> ();
4407 parser->lexer = lexer;
4408 parser->context = cp_parser_context_new (NULL);
4410 /* For now, we always accept GNU extensions. */
4411 parser->allow_gnu_extensions_p = 1;
4413 /* The `>' token is a greater-than operator, not the end of a
4414 template-id. */
4415 parser->greater_than_is_operator_p = true;
4417 parser->default_arg_ok_p = true;
4419 /* We are not parsing a constant-expression. */
4420 parser->integral_constant_expression_p = false;
4421 parser->allow_non_integral_constant_expression_p = false;
4422 parser->non_integral_constant_expression_p = false;
4424 /* Local variable names are not forbidden. */
4425 parser->local_variables_forbidden_p = 0;
4427 /* We are not processing an `extern "C"' declaration. */
4428 parser->in_unbraced_linkage_specification_p = false;
4430 /* We aren't parsing an export-declaration. */
4431 parser->in_unbraced_export_declaration_p = false;
4433 /* We are not processing a declarator. */
4434 parser->in_declarator_p = false;
4436 /* We are not processing a template-argument-list. */
4437 parser->in_template_argument_list_p = false;
4439 /* We are not in an iteration statement. */
4440 parser->in_statement = 0;
4442 /* We are not in a switch statement. */
4443 parser->in_switch_statement_p = false;
4445 /* We are not parsing a type-id inside an expression. */
4446 parser->in_type_id_in_expr_p = false;
4448 /* String literals should be translated to the execution character set. */
4449 parser->translate_strings_p = true;
4451 /* We are not parsing a function body. */
4452 parser->in_function_body = false;
4454 /* We can correct until told otherwise. */
4455 parser->colon_corrects_to_scope_p = true;
4457 /* The unparsed function queue is empty. */
4458 push_unparsed_function_queues (parser);
4460 /* There are no classes being defined. */
4461 parser->num_classes_being_defined = 0;
4463 /* No template parameters apply. */
4464 parser->num_template_parameter_lists = 0;
4466 /* Special parsing data structures. */
4467 parser->omp_declare_simd = NULL;
4468 parser->oacc_routine = NULL;
4470 /* Disallow OpenMP array sections in expressions. */
4471 parser->omp_array_section_p = false;
4473 /* Not declaring an implicit function template. */
4474 parser->auto_is_implicit_function_template_parm_p = false;
4475 parser->fully_implicit_function_template_p = false;
4476 parser->implicit_template_parms = 0;
4477 parser->implicit_template_scope = 0;
4479 /* Allow constrained-type-specifiers. */
4480 parser->prevent_constrained_type_specifiers = 0;
4482 /* We haven't yet seen an 'extern "C"'. */
4483 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
4485 return parser;
4488 /* Create a cp_lexer structure which will emit the tokens in CACHE
4489 and push it onto the parser's lexer stack. This is used for delayed
4490 parsing of in-class method bodies and default arguments, and should
4491 not be confused with tentative parsing. */
4492 static void
4493 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
4495 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
4496 lexer->next = parser->lexer;
4497 parser->lexer = lexer;
4499 /* Move the current source position to that of the first token in the
4500 new lexer. */
4501 cp_lexer_set_source_position_from_token (lexer->next_token);
4504 /* Pop the top lexer off the parser stack. This is never used for the
4505 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
4506 static void
4507 cp_parser_pop_lexer (cp_parser *parser)
4509 cp_lexer *lexer = parser->lexer;
4510 parser->lexer = lexer->next;
4511 cp_lexer_destroy (lexer);
4513 /* Put the current source position back where it was before this
4514 lexer was pushed. */
4515 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4518 /* Lexical conventions [gram.lex] */
4520 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
4521 identifier. */
4523 static cp_expr
4524 cp_parser_identifier (cp_parser* parser)
4526 cp_token *token;
4528 /* Look for the identifier. */
4529 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
4530 /* Return the value. */
4531 if (token)
4532 return cp_expr (token->u.value, token->location);
4533 else
4534 return error_mark_node;
4537 /* Worker for cp_parser_string_literal, cp_parser_userdef_string_literal
4538 and cp_parser_unevaluated_string_literal.
4539 Do not call this directly; use either of the above.
4541 Parse a sequence of adjacent string constants. Return a
4542 TREE_STRING representing the combined, nul-terminated string
4543 constant. If TRANSLATE is true, translate the string to the
4544 execution character set. If WIDE_OK is true, a wide string is
4545 valid here. If UDL_OK is true, a string literal with user-defined
4546 suffix can be used in this context. If UNEVAL is true, diagnose
4547 numeric and conditional escape sequences in it if pedantic.
4549 C++98 [lex.string] says that if a narrow string literal token is
4550 adjacent to a wide string literal token, the behavior is undefined.
4551 However, C99 6.4.5p4 says that this results in a wide string literal.
4552 We follow C99 here, for consistency with the C front end.
4554 This code is largely lifted from lex_string() in c-lex.cc.
4556 FUTURE: ObjC++ will need to handle @-strings here. */
4558 static cp_expr
4559 cp_parser_string_literal_common (cp_parser *parser, bool translate,
4560 bool wide_ok, bool udl_ok,
4561 bool lookup_udlit, bool uneval)
4563 tree value;
4564 size_t count;
4565 struct obstack str_ob;
4566 struct obstack loc_ob;
4567 cpp_string str, istr, *strs;
4568 cp_token *tok;
4569 enum cpp_ttype type, curr_type;
4570 int have_suffix_p = 0;
4571 tree string_tree;
4572 tree suffix_id = NULL_TREE;
4573 bool curr_tok_is_userdef_p = false;
4575 tok = cp_lexer_peek_token (parser->lexer);
4576 if (!cp_parser_is_string_literal (tok))
4578 cp_parser_error (parser, "expected string-literal");
4579 return error_mark_node;
4582 location_t loc = tok->location;
4584 if (cpp_userdef_string_p (tok->type))
4586 if (!udl_ok)
4588 error_at (loc, "string literal with user-defined suffix "
4589 "is invalid in this context");
4590 return error_mark_node;
4592 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4593 curr_type = cpp_userdef_string_remove_type (tok->type);
4594 curr_tok_is_userdef_p = true;
4596 else
4598 string_tree = tok->u.value;
4599 curr_type = tok->type;
4601 type = curr_type;
4603 /* Try to avoid the overhead of creating and destroying an obstack
4604 for the common case of just one string. */
4605 if (!cp_parser_is_string_literal
4606 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4608 cp_lexer_consume_token (parser->lexer);
4610 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4611 str.len = TREE_STRING_LENGTH (string_tree);
4612 count = 1;
4614 if (curr_tok_is_userdef_p)
4616 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4617 have_suffix_p = 1;
4618 curr_type = cpp_userdef_string_remove_type (tok->type);
4620 else
4621 curr_type = tok->type;
4623 strs = &str;
4625 else
4627 location_t last_tok_loc = tok->location;
4628 gcc_obstack_init (&str_ob);
4629 gcc_obstack_init (&loc_ob);
4630 count = 0;
4634 cp_lexer_consume_token (parser->lexer);
4635 count++;
4636 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4637 str.len = TREE_STRING_LENGTH (string_tree);
4639 if (curr_tok_is_userdef_p)
4641 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4642 if (have_suffix_p == 0)
4644 suffix_id = curr_suffix_id;
4645 have_suffix_p = 1;
4647 else if (have_suffix_p == 1
4648 && curr_suffix_id != suffix_id)
4650 error ("inconsistent user-defined literal suffixes"
4651 " %qD and %qD in string literal",
4652 suffix_id, curr_suffix_id);
4653 have_suffix_p = -1;
4655 curr_type = cpp_userdef_string_remove_type (tok->type);
4657 else
4658 curr_type = tok->type;
4660 if (type != curr_type)
4662 if (type == CPP_STRING)
4663 type = curr_type;
4664 else if (curr_type != CPP_STRING)
4666 rich_location rich_loc (line_table, tok->location);
4667 rich_loc.add_range (last_tok_loc);
4668 error_at (&rich_loc,
4669 "concatenation of string literals with "
4670 "conflicting encoding prefixes");
4674 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4675 obstack_grow (&loc_ob, &tok->location, sizeof (location_t));
4677 last_tok_loc = tok->location;
4679 tok = cp_lexer_peek_token (parser->lexer);
4680 if (cpp_userdef_string_p (tok->type))
4682 if (!udl_ok)
4684 error_at (loc, "string literal with user-defined suffix "
4685 "is invalid in this context");
4686 return error_mark_node;
4688 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4689 curr_type = cpp_userdef_string_remove_type (tok->type);
4690 curr_tok_is_userdef_p = true;
4692 else
4694 string_tree = tok->u.value;
4695 curr_type = tok->type;
4696 curr_tok_is_userdef_p = false;
4699 while (cp_parser_is_string_literal (tok));
4701 /* A string literal built by concatenation has its caret=start at
4702 the start of the initial string, and its finish at the finish of
4703 the final string literal. */
4704 loc = make_location (loc, loc, get_finish (last_tok_loc));
4706 strs = (cpp_string *) obstack_finish (&str_ob);
4709 if (type != CPP_STRING && !wide_ok)
4711 cp_parser_error (parser, "a wide string is invalid in this context");
4712 type = CPP_STRING;
4714 if (uneval)
4715 type = CPP_UNEVAL_STRING;
4717 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4718 (parse_in, strs, count, &istr, type))
4720 value = build_string (istr.len, (const char *)istr.text);
4721 free (CONST_CAST (unsigned char *, istr.text));
4722 if (count > 1)
4724 location_t *locs = (location_t *)obstack_finish (&loc_ob);
4725 gcc_assert (g_string_concat_db);
4726 g_string_concat_db->record_string_concatenation (count, locs);
4729 switch (type)
4731 default:
4732 case CPP_STRING:
4733 TREE_TYPE (value) = char_array_type_node;
4734 break;
4735 case CPP_UTF8STRING:
4736 if (flag_char8_t)
4737 TREE_TYPE (value) = char8_array_type_node;
4738 else
4739 TREE_TYPE (value) = char_array_type_node;
4740 break;
4741 case CPP_STRING16:
4742 TREE_TYPE (value) = char16_array_type_node;
4743 break;
4744 case CPP_STRING32:
4745 TREE_TYPE (value) = char32_array_type_node;
4746 break;
4747 case CPP_WSTRING:
4748 TREE_TYPE (value) = wchar_array_type_node;
4749 break;
4752 value = fix_string_type (value);
4754 if (have_suffix_p)
4756 tree literal = build_userdef_literal (suffix_id, value,
4757 OT_NONE, NULL_TREE);
4758 if (lookup_udlit)
4759 value = finish_userdef_string_literal (literal);
4760 else
4761 value = literal;
4764 else
4765 /* cpp_interpret_string has issued an error. */
4766 value = error_mark_node;
4768 if (count > 1)
4770 obstack_free (&str_ob, 0);
4771 obstack_free (&loc_ob, 0);
4774 return cp_expr (value, loc);
4777 /* Parse a sequence of adjacent string constants. Return a TREE_STRING
4778 representing the combined, nul-terminated string constant. If
4779 TRANSLATE is true, translate the string to the execution character set.
4780 If WIDE_OK is true, a wide string is valid here.
4782 This function issues an error if a user defined string literal is
4783 encountered; use cp_parser_userdef_string_literal if UDLs are allowed. */
4785 static inline cp_expr
4786 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
4788 return cp_parser_string_literal_common (parser, translate, wide_ok,
4789 /*udl_ok=*/false,
4790 /*lookup_udlit=*/false,
4791 /*uneval=*/false);
4794 /* Parse a string literal or user defined string literal.
4796 user-defined-string-literal :
4797 string-literal ud-suffix
4799 If LOOKUP_UDLIT, perform a lookup for a suitable template function. */
4801 static inline cp_expr
4802 cp_parser_userdef_string_literal (cp_parser *parser, bool lookup_udlit)
4804 return cp_parser_string_literal_common (parser, /*translate=*/true,
4805 /*wide_ok=*/true, /*udl_ok=*/true,
4806 lookup_udlit, /*uneval=*/false);
4809 /* Parse an unevaluated string literal.
4811 unevaluated-string:
4812 string-literal */
4814 static inline cp_expr
4815 cp_parser_unevaluated_string_literal (cp_parser *parser)
4817 return cp_parser_string_literal_common (parser, /*translate=*/false,
4818 /*wide_ok=*/false, /*udl_ok=*/false,
4819 /*lookup_udlit=*/false,
4820 /*uneval=*/true);
4823 /* Look up a literal operator with the name and the exact arguments. */
4825 static tree
4826 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4828 tree decl = lookup_name (name);
4829 if (!decl || !is_overloaded_fn (decl))
4830 return error_mark_node;
4832 for (lkp_iterator iter (decl); iter; ++iter)
4834 tree fn = *iter;
4836 if (tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn)))
4838 unsigned int ix;
4839 bool found = true;
4841 for (ix = 0;
4842 found && ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4843 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4845 tree tparm = TREE_VALUE (parmtypes);
4846 tree targ = TREE_TYPE ((*args)[ix]);
4847 bool ptr = TYPE_PTR_P (tparm);
4848 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4849 if ((ptr || arr || !same_type_p (tparm, targ))
4850 && (!ptr || !arr
4851 || !same_type_p (TREE_TYPE (tparm),
4852 TREE_TYPE (targ))))
4853 found = false;
4856 if (found
4857 && ix == vec_safe_length (args)
4858 /* May be this should be sufficient_parms_p instead,
4859 depending on how exactly should user-defined literals
4860 work in presence of default arguments on the literal
4861 operator parameters. */
4862 && parmtypes == void_list_node)
4863 return decl;
4867 return error_mark_node;
4870 /* Parse a user-defined char constant. Returns a call to a user-defined
4871 literal operator taking the character as an argument. */
4873 static cp_expr
4874 cp_parser_userdef_char_literal (cp_parser *parser)
4876 cp_token *token = cp_lexer_consume_token (parser->lexer);
4877 tree literal = token->u.value;
4878 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4879 tree value = USERDEF_LITERAL_VALUE (literal);
4880 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4881 tree decl, result;
4883 /* Build up a call to the user-defined operator */
4884 /* Lookup the name we got back from the id-expression. */
4885 releasing_vec args;
4886 vec_safe_push (args, value);
4887 decl = lookup_literal_operator (name, args);
4888 if (!decl || decl == error_mark_node)
4890 error ("unable to find character literal operator %qD with %qT argument",
4891 name, TREE_TYPE (value));
4892 return error_mark_node;
4894 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4895 return result;
4898 /* A subroutine of cp_parser_userdef_numeric_literal to
4899 create a char... template parameter pack from a string node. */
4901 static tree
4902 make_char_string_pack (tree value)
4904 tree charvec;
4905 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4906 const unsigned char *str
4907 = (const unsigned char *) TREE_STRING_POINTER (value);
4908 int i, len = TREE_STRING_LENGTH (value) - 1;
4909 tree argvec = make_tree_vec (1);
4911 /* Fill in CHARVEC with all of the parameters. */
4912 charvec = make_tree_vec (len);
4913 for (i = 0; i < len; ++i)
4915 unsigned char s[3] = { '\'', str[i], '\'' };
4916 cpp_string in = { 3, s };
4917 cpp_string out = { 0, 0 };
4918 if (!cpp_interpret_string (parse_in, &in, 1, &out, CPP_STRING))
4919 return NULL_TREE;
4920 gcc_assert (out.len == 2);
4921 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node,
4922 out.text[0]);
4925 /* Build the argument packs. */
4926 ARGUMENT_PACK_ARGS (argpack) = charvec;
4928 TREE_VEC_ELT (argvec, 0) = argpack;
4930 return argvec;
4933 /* A subroutine of cp_parser_userdef_numeric_literal to
4934 create a char... template parameter pack from a string node. */
4936 static tree
4937 make_string_pack (tree value)
4939 tree charvec;
4940 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4941 const unsigned char *str
4942 = (const unsigned char *) TREE_STRING_POINTER (value);
4943 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4944 int len = TREE_STRING_LENGTH (value) / sz - 1;
4945 tree argvec = make_tree_vec (2);
4947 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4948 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4950 /* First template parm is character type. */
4951 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4953 /* Fill in CHARVEC with all of the parameters. */
4954 charvec = make_tree_vec (len);
4955 for (int i = 0; i < len; ++i)
4956 TREE_VEC_ELT (charvec, i)
4957 = double_int_to_tree (str_char_type_node,
4958 double_int::from_buffer (str + i * sz, sz));
4960 /* Build the argument packs. */
4961 ARGUMENT_PACK_ARGS (argpack) = charvec;
4963 TREE_VEC_ELT (argvec, 1) = argpack;
4965 return argvec;
4968 /* Parse a user-defined numeric constant. returns a call to a user-defined
4969 literal operator. */
4971 static cp_expr
4972 cp_parser_userdef_numeric_literal (cp_parser *parser)
4974 cp_token *token = cp_lexer_consume_token (parser->lexer);
4975 tree literal = token->u.value;
4976 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4977 tree value = USERDEF_LITERAL_VALUE (literal);
4978 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4979 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4980 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4981 tree decl, result;
4983 /* Look for a literal operator taking the exact type of numeric argument
4984 as the literal value. */
4985 releasing_vec args;
4986 vec_safe_push (args, value);
4987 decl = lookup_literal_operator (name, args);
4988 if (decl && decl != error_mark_node)
4990 result = finish_call_expr (decl, &args, false, true,
4991 tf_warning_or_error);
4993 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4995 warning_at (token->location, OPT_Woverflow,
4996 "integer literal exceeds range of %qT type",
4997 long_long_unsigned_type_node);
4999 else
5001 if (overflow > 0)
5002 warning_at (token->location, OPT_Woverflow,
5003 "floating literal exceeds range of %qT type",
5004 long_double_type_node);
5005 else if (overflow < 0)
5006 warning_at (token->location, OPT_Woverflow,
5007 "floating literal truncated to zero");
5010 return result;
5013 /* If the numeric argument didn't work, look for a raw literal
5014 operator taking a const char* argument consisting of the number
5015 in string format. */
5016 args->truncate (0);
5017 vec_safe_push (args, num_string);
5018 decl = lookup_literal_operator (name, args);
5019 if (decl && decl != error_mark_node)
5021 result = finish_call_expr (decl, &args, false, true,
5022 tf_warning_or_error);
5023 return result;
5026 /* If the raw literal didn't work, look for a non-type template
5027 function with parameter pack char.... Call the function with
5028 template parameter characters representing the number. */
5029 args->truncate (0);
5030 decl = lookup_literal_operator (name, args);
5031 if (decl && decl != error_mark_node)
5033 tree tmpl_args = make_char_string_pack (num_string);
5034 if (tmpl_args == NULL_TREE)
5036 error ("failed to translate literal to execution character set %qT",
5037 num_string);
5038 return error_mark_node;
5040 decl = lookup_template_function (decl, tmpl_args);
5041 result = finish_call_expr (decl, &args, false, true,
5042 tf_warning_or_error);
5043 return result;
5046 /* In C++14 the standard library defines complex number suffixes that
5047 conflict with GNU extensions. Prefer them if <complex> is #included. */
5048 bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
5049 bool i14 = (cxx_dialect > cxx11
5050 && (id_equal (suffix_id, "i")
5051 || id_equal (suffix_id, "if")
5052 || id_equal (suffix_id, "il")));
5053 diagnostic_t kind = DK_ERROR;
5054 int opt = 0;
5056 if (i14 && ext)
5058 tree cxlit = lookup_qualified_name (std_node, "complex_literals",
5059 LOOK_want::NORMAL, false);
5060 if (cxlit == error_mark_node)
5062 /* No <complex>, so pedwarn and use GNU semantics. */
5063 kind = DK_PEDWARN;
5064 opt = OPT_Wpedantic;
5068 bool complained
5069 = emit_diagnostic (kind, input_location, opt,
5070 "unable to find numeric literal operator %qD", name);
5072 if (!complained)
5073 /* Don't inform either. */;
5074 else if (i14)
5076 inform (token->location, "add %<using namespace std::complex_literals%> "
5077 "(from %<<complex>%>) to enable the C++14 user-defined literal "
5078 "suffixes");
5079 if (ext)
5080 inform (token->location, "or use %<j%> instead of %<i%> for the "
5081 "GNU built-in suffix");
5083 else if (!ext)
5084 inform (token->location, "use %<-fext-numeric-literals%> "
5085 "to enable more built-in suffixes");
5087 if (kind == DK_ERROR)
5088 value = error_mark_node;
5089 else
5091 /* Use the built-in semantics. */
5092 tree type;
5093 if (id_equal (suffix_id, "i"))
5095 if (TREE_CODE (value) == INTEGER_CST)
5096 type = integer_type_node;
5097 else
5098 type = double_type_node;
5100 else if (id_equal (suffix_id, "if"))
5101 type = float_type_node;
5102 else /* if (id_equal (suffix_id, "il")) */
5103 type = long_double_type_node;
5105 value = fold_build2 (COMPLEX_EXPR, build_complex_type (type),
5106 build_zero_cst (type), fold_convert (type, value));
5109 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5110 /* Avoid repeated diagnostics. */
5111 token->u.value = value;
5112 return value;
5115 /* Parse a user-defined string constant. Returns a call to a user-defined
5116 literal operator taking a character pointer and the length of the string
5117 as arguments. */
5119 static tree
5120 finish_userdef_string_literal (tree literal)
5122 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
5123 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
5124 tree value = USERDEF_LITERAL_VALUE (literal);
5125 int len = TREE_STRING_LENGTH (value)
5126 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
5127 tree decl;
5129 /* Build up a call to the user-defined operator. */
5130 /* Lookup the name we got back from the id-expression. */
5131 releasing_vec args;
5132 vec_safe_push (args, value);
5133 vec_safe_push (args, build_int_cst (size_type_node, len));
5134 decl = lookup_literal_operator (name, args);
5136 if (decl && decl != error_mark_node)
5137 return finish_call_expr (decl, &args, false, true,
5138 tf_warning_or_error);
5140 /* Look for a suitable template function, either (C++20) with a single
5141 parameter of class type, or (N3599) with typename parameter CharT and
5142 parameter pack CharT... */
5143 args->truncate (0);
5144 decl = lookup_literal_operator (name, args);
5145 if (decl && decl != error_mark_node)
5147 /* Use resolve_nondeduced_context to try to choose one form of template
5148 or the other. */
5149 tree tmpl_args = make_tree_vec (1);
5150 TREE_VEC_ELT (tmpl_args, 0) = value;
5151 decl = lookup_template_function (decl, tmpl_args);
5152 tree res = resolve_nondeduced_context (decl, tf_none);
5153 if (DECL_P (res))
5154 decl = res;
5155 else
5157 TREE_OPERAND (decl, 1) = make_string_pack (value);
5158 res = resolve_nondeduced_context (decl, tf_none);
5159 if (DECL_P (res))
5160 decl = res;
5162 if (!DECL_P (decl) && cxx_dialect > cxx17)
5163 TREE_OPERAND (decl, 1) = tmpl_args;
5164 return finish_call_expr (decl, &args, false, true,
5165 tf_warning_or_error);
5168 error ("unable to find string literal operator %qD with %qT, %qT arguments",
5169 name, TREE_TYPE (value), size_type_node);
5170 return error_mark_node;
5174 /* Basic concepts [gram.basic] */
5176 /* Parse a translation-unit.
5178 translation-unit:
5179 declaration-seq [opt] */
5181 static void
5182 cp_parser_translation_unit (cp_parser* parser)
5184 gcc_checking_assert (!cp_error_declarator);
5186 /* Create the declarator obstack. */
5187 gcc_obstack_init (&declarator_obstack);
5188 /* Create the error declarator. */
5189 cp_error_declarator = make_declarator (cdk_error);
5190 /* Create the empty parameter list. */
5191 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
5192 UNKNOWN_LOCATION);
5193 /* Remember where the base of the declarator obstack lies. */
5194 void *declarator_obstack_base = obstack_next_free (&declarator_obstack);
5196 push_deferring_access_checks (flag_access_control
5197 ? dk_no_deferred : dk_no_check);
5199 module_parse mp_state = MP_NOT_MODULE;
5200 if (modules_p () && !header_module_p ())
5201 mp_state = MP_FIRST;
5203 bool implicit_extern_c = false;
5205 /* Parse until EOF. */
5206 for (;;)
5208 cp_token *token = cp_lexer_peek_token (parser->lexer);
5210 /* If we're entering or exiting a region that's implicitly
5211 extern "C", modify the lang context appropriately. This is
5212 so horrible. Please die. */
5213 if (implicit_extern_c
5214 != cp_lexer_peek_token (parser->lexer)->implicit_extern_c)
5216 implicit_extern_c = !implicit_extern_c;
5217 if (implicit_extern_c)
5218 push_lang_context (lang_name_c);
5219 else
5220 pop_lang_context ();
5223 if (token->type == CPP_EOF)
5224 break;
5226 if (modules_p ())
5228 /* Top-level module declarations are ok, and change the
5229 portion of file we're in. Top-level import declarations
5230 are significant for the import portions. */
5232 cp_token *next = token;
5233 bool exporting = token->keyword == RID__EXPORT;
5234 if (exporting)
5236 cp_lexer_consume_token (parser->lexer);
5237 next = cp_lexer_peek_token (parser->lexer);
5239 if (next->keyword == RID__MODULE)
5241 mp_state
5242 = cp_parser_module_declaration (parser, mp_state, exporting);
5243 continue;
5245 else if (next->keyword == RID__IMPORT)
5247 if (mp_state == MP_FIRST)
5248 mp_state = MP_NOT_MODULE;
5249 cp_parser_import_declaration (parser, mp_state, exporting);
5250 continue;
5252 else
5253 gcc_checking_assert (!exporting);
5255 if (mp_state == MP_GLOBAL && token->main_source_p)
5257 static bool warned = false;
5258 if (!warned)
5260 warned = true;
5261 pedwarn (token->location, OPT_Wglobal_module,
5262 "global module fragment contents must be"
5263 " from preprocessor inclusion");
5268 /* This relies on the ordering of module_parse values. */
5269 if (mp_state == MP_PURVIEW_IMPORTS || mp_state == MP_PRIVATE_IMPORTS)
5270 /* We're no longer in the import portion of a named module. */
5271 mp_state = module_parse (mp_state + 1);
5272 else if (mp_state == MP_FIRST)
5273 mp_state = MP_NOT_MODULE;
5275 if (token->type == CPP_CLOSE_BRACE)
5277 cp_parser_error (parser, "expected declaration");
5278 cp_lexer_consume_token (parser->lexer);
5279 /* If the next token is now a `;', consume it. */
5280 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
5281 cp_lexer_consume_token (parser->lexer);
5283 else
5284 cp_parser_toplevel_declaration (parser);
5287 /* Get rid of the token array; we don't need it any more. */
5288 cp_lexer_destroy (parser->lexer);
5289 parser->lexer = NULL;
5291 /* The EOF should have reset this. */
5292 gcc_checking_assert (!implicit_extern_c);
5294 /* Make sure the declarator obstack was fully cleaned up. */
5295 gcc_assert (obstack_next_free (&declarator_obstack)
5296 == declarator_obstack_base);
5299 /* Return the appropriate tsubst flags for parsing, possibly in N3276
5300 decltype context. */
5302 static inline tsubst_flags_t
5303 complain_flags (bool decltype_p)
5305 tsubst_flags_t complain = tf_warning_or_error;
5306 if (decltype_p)
5307 complain |= tf_decltype;
5308 return complain;
5311 /* We're about to parse a collection of statements. If we're currently
5312 parsing tentatively, set up a firewall so that any nested
5313 cp_parser_commit_to_tentative_parse won't affect the current context. */
5315 static cp_token_position
5316 cp_parser_start_tentative_firewall (cp_parser *parser)
5318 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5319 return 0;
5321 cp_parser_parse_tentatively (parser);
5322 cp_parser_commit_to_topmost_tentative_parse (parser);
5323 return cp_lexer_token_position (parser->lexer, false);
5326 /* We've finished parsing the collection of statements. Wrap up the
5327 firewall and replace the relevant tokens with the parsed form. */
5329 static void
5330 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
5331 tree expr)
5333 if (!start)
5334 return;
5336 /* Finish the firewall level. */
5337 cp_parser_parse_definitely (parser);
5338 /* And remember the result of the parse for when we try again. */
5339 cp_token *token = cp_lexer_token_at (parser->lexer, start);
5340 token->type = CPP_PREPARSED_EXPR;
5341 token->u.value = expr;
5342 token->keyword = RID_MAX;
5343 cp_lexer_purge_tokens_after (parser->lexer, start);
5346 /* Like the above functions, but let the user modify the tokens. Used by
5347 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
5348 later parses, so it makes sense to localize the effects of
5349 cp_parser_commit_to_tentative_parse. */
5351 struct tentative_firewall
5353 cp_parser *parser;
5354 bool set;
5356 tentative_firewall (cp_parser *p): parser(p)
5358 /* If we're currently parsing tentatively, start a committed level as a
5359 firewall and then an inner tentative parse. */
5360 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
5362 cp_parser_parse_tentatively (parser);
5363 cp_parser_commit_to_topmost_tentative_parse (parser);
5364 cp_parser_parse_tentatively (parser);
5368 ~tentative_firewall()
5370 if (set)
5372 /* Finish the inner tentative parse and the firewall, propagating any
5373 uncommitted error state to the outer tentative parse. */
5374 bool err = cp_parser_error_occurred (parser);
5375 cp_parser_parse_definitely (parser);
5376 cp_parser_parse_definitely (parser);
5377 if (err)
5378 cp_parser_simulate_error (parser);
5383 /* Some tokens naturally come in pairs e.g.'(' and ')'.
5384 This class is for tracking such a matching pair of symbols.
5385 In particular, it tracks the location of the first token,
5386 so that if the second token is missing, we can highlight the
5387 location of the first token when notifying the user about the
5388 problem. */
5390 template <typename traits_t>
5391 class token_pair
5393 public:
5394 /* token_pair's ctor. */
5395 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
5397 /* If the next token is the opening symbol for this pair, consume it and
5398 return true.
5399 Otherwise, issue an error and return false.
5400 In either case, record the location of the opening token. */
5402 bool require_open (cp_parser *parser)
5404 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
5405 return cp_parser_require (parser, traits_t::open_token_type,
5406 traits_t::required_token_open);
5409 /* Consume the next token from PARSER, recording its location as
5410 that of the opening token within the pair. */
5412 cp_token * consume_open (cp_parser *parser)
5414 cp_token *tok = cp_lexer_consume_token (parser->lexer);
5415 gcc_assert (tok->type == traits_t::open_token_type);
5416 m_open_loc = tok->location;
5417 return tok;
5420 /* If the next token is the closing symbol for this pair, consume it
5421 and return it.
5422 Otherwise, issue an error, highlighting the location of the
5423 corresponding opening token, and return NULL. */
5425 cp_token *require_close (cp_parser *parser) const
5427 return cp_parser_require (parser, traits_t::close_token_type,
5428 traits_t::required_token_close,
5429 m_open_loc);
5432 location_t open_location () const { return m_open_loc; }
5434 private:
5435 location_t m_open_loc;
5438 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
5440 struct matching_paren_traits
5442 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
5443 static const enum required_token required_token_open = RT_OPEN_PAREN;
5444 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
5445 static const enum required_token required_token_close = RT_CLOSE_PAREN;
5448 /* "matching_parens" is a token_pair<T> class for tracking matching
5449 pairs of parentheses. */
5451 typedef token_pair<matching_paren_traits> matching_parens;
5453 /* Traits for token_pair<T> for tracking matching pairs of braces. */
5455 struct matching_brace_traits
5457 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
5458 static const enum required_token required_token_open = RT_OPEN_BRACE;
5459 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
5460 static const enum required_token required_token_close = RT_CLOSE_BRACE;
5463 /* "matching_braces" is a token_pair<T> class for tracking matching
5464 pairs of braces. */
5466 typedef token_pair<matching_brace_traits> matching_braces;
5469 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
5470 enclosing parentheses. */
5472 static cp_expr
5473 cp_parser_statement_expr (cp_parser *parser)
5475 cp_token_position start = cp_parser_start_tentative_firewall (parser);
5476 auto oas = make_temp_override (parser->omp_array_section_p, false);
5478 /* Consume the '('. */
5479 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
5480 matching_parens parens;
5481 parens.consume_open (parser);
5482 /* Start the statement-expression. */
5483 tree expr = begin_stmt_expr ();
5484 /* Parse the compound-statement. */
5485 cp_parser_compound_statement (parser, expr, BCS_STMT_EXPR, false);
5486 /* Finish up. */
5487 expr = finish_stmt_expr (expr, false);
5488 /* Consume the ')'. */
5489 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
5490 if (!parens.require_close (parser))
5491 cp_parser_skip_to_end_of_statement (parser);
5493 cp_parser_end_tentative_firewall (parser, start, expr);
5494 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
5495 return cp_expr (expr, combined_loc);
5498 /* Expressions [gram.expr] */
5500 /* Parse a fold-operator.
5502 fold-operator:
5503 - * / % ^ & | = < > << >>
5504 = -= *= /= %= ^= &= |= <<= >>=
5505 == != <= >= && || , .* ->*
5507 This returns the tree code corresponding to the matched operator
5508 as an int. When the current token matches a compound assignment
5509 operator, the resulting tree code is the negative value of the
5510 non-assignment operator. */
5512 static int
5513 cp_parser_fold_operator (cp_token *token)
5515 switch (token->type)
5517 case CPP_PLUS: return PLUS_EXPR;
5518 case CPP_MINUS: return MINUS_EXPR;
5519 case CPP_MULT: return MULT_EXPR;
5520 case CPP_DIV: return TRUNC_DIV_EXPR;
5521 case CPP_MOD: return TRUNC_MOD_EXPR;
5522 case CPP_XOR: return BIT_XOR_EXPR;
5523 case CPP_AND: return BIT_AND_EXPR;
5524 case CPP_OR: return BIT_IOR_EXPR;
5525 case CPP_LSHIFT: return LSHIFT_EXPR;
5526 case CPP_RSHIFT: return RSHIFT_EXPR;
5528 case CPP_EQ: return -NOP_EXPR;
5529 case CPP_PLUS_EQ: return -PLUS_EXPR;
5530 case CPP_MINUS_EQ: return -MINUS_EXPR;
5531 case CPP_MULT_EQ: return -MULT_EXPR;
5532 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
5533 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
5534 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
5535 case CPP_AND_EQ: return -BIT_AND_EXPR;
5536 case CPP_OR_EQ: return -BIT_IOR_EXPR;
5537 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
5538 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
5540 case CPP_EQ_EQ: return EQ_EXPR;
5541 case CPP_NOT_EQ: return NE_EXPR;
5542 case CPP_LESS: return LT_EXPR;
5543 case CPP_GREATER: return GT_EXPR;
5544 case CPP_LESS_EQ: return LE_EXPR;
5545 case CPP_GREATER_EQ: return GE_EXPR;
5547 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
5548 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
5550 case CPP_COMMA: return COMPOUND_EXPR;
5552 case CPP_DOT_STAR: return DOTSTAR_EXPR;
5553 case CPP_DEREF_STAR: return MEMBER_REF;
5555 default: return ERROR_MARK;
5559 /* Returns true if CODE indicates a binary expression, which is not allowed in
5560 the LHS of a fold-expression. More codes will need to be added to use this
5561 function in other contexts. */
5563 static bool
5564 is_binary_op (tree_code code)
5566 switch (code)
5568 case PLUS_EXPR:
5569 case POINTER_PLUS_EXPR:
5570 case MINUS_EXPR:
5571 case MULT_EXPR:
5572 case TRUNC_DIV_EXPR:
5573 case TRUNC_MOD_EXPR:
5574 case BIT_XOR_EXPR:
5575 case BIT_AND_EXPR:
5576 case BIT_IOR_EXPR:
5577 case LSHIFT_EXPR:
5578 case RSHIFT_EXPR:
5580 case MODOP_EXPR:
5582 case EQ_EXPR:
5583 case NE_EXPR:
5584 case LE_EXPR:
5585 case GE_EXPR:
5586 case LT_EXPR:
5587 case GT_EXPR:
5589 case TRUTH_ANDIF_EXPR:
5590 case TRUTH_ORIF_EXPR:
5592 case COMPOUND_EXPR:
5594 case DOTSTAR_EXPR:
5595 case MEMBER_REF:
5596 return true;
5598 default:
5599 return false;
5603 /* If the next token is a suitable fold operator, consume it and return as
5604 the function above. */
5606 static int
5607 cp_parser_fold_operator (cp_parser *parser)
5609 cp_token* token = cp_lexer_peek_token (parser->lexer);
5610 int code = cp_parser_fold_operator (token);
5611 if (code != ERROR_MARK)
5612 cp_lexer_consume_token (parser->lexer);
5613 return code;
5616 /* Parse a fold-expression.
5618 fold-expression:
5619 ( ... folding-operator cast-expression)
5620 ( cast-expression folding-operator ... )
5621 ( cast-expression folding operator ... folding-operator cast-expression)
5623 Note that the '(' and ')' are matched in primary expression. */
5625 static cp_expr
5626 cp_parser_fold_expression (cp_parser *parser, tree expr1)
5628 cp_id_kind pidk;
5629 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5630 const cp_token *token = nullptr;
5632 // Left fold.
5633 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5635 if (expr1)
5636 return error_mark_node;
5637 cp_lexer_consume_token (parser->lexer);
5638 token = cp_lexer_peek_token (parser->lexer);
5639 int op = cp_parser_fold_operator (parser);
5640 if (op == ERROR_MARK)
5642 cp_parser_error (parser, "expected binary operator");
5643 return error_mark_node;
5646 tree expr = cp_parser_cast_expression (parser, false, false,
5647 false, &pidk);
5648 if (expr == error_mark_node)
5649 return error_mark_node;
5650 loc = make_location (token->location, loc, parser->lexer);
5651 return finish_left_unary_fold_expr (loc, expr, op);
5654 token = cp_lexer_peek_token (parser->lexer);
5655 int op = cp_parser_fold_operator (parser);
5656 if (op == ERROR_MARK)
5658 cp_parser_error (parser, "expected binary operator");
5659 return error_mark_node;
5662 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
5664 cp_parser_error (parser, "expected ...");
5665 return error_mark_node;
5667 cp_lexer_consume_token (parser->lexer);
5669 /* The operands of a fold-expression are cast-expressions, so binary or
5670 conditional expressions are not allowed. We check this here to avoid
5671 tentative parsing. */
5672 if (EXPR_P (expr1) && warning_suppressed_p (expr1, OPT_Wparentheses))
5673 /* OK, the expression was parenthesized. */;
5674 else if (is_binary_op (TREE_CODE (expr1)))
5675 error_at (location_of (expr1),
5676 "binary expression in operand of fold-expression");
5677 else if (TREE_CODE (expr1) == COND_EXPR
5678 || (REFERENCE_REF_P (expr1)
5679 && TREE_CODE (TREE_OPERAND (expr1, 0)) == COND_EXPR))
5680 error_at (location_of (expr1),
5681 "conditional expression in operand of fold-expression");
5683 // Right fold.
5684 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5686 loc = make_location (token->location, loc, parser->lexer);
5687 return finish_right_unary_fold_expr (loc, expr1, op);
5690 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
5692 cp_parser_error (parser, "mismatched operator in fold-expression");
5693 return error_mark_node;
5695 cp_lexer_consume_token (parser->lexer);
5697 // Binary left or right fold.
5698 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
5699 if (expr2 == error_mark_node)
5700 return error_mark_node;
5701 loc = make_location (token->location, loc, parser->lexer);
5702 return finish_binary_fold_expr (loc, expr1, expr2, op);
5705 /* Parse a primary-expression.
5707 primary-expression:
5708 literal
5709 this
5710 ( expression )
5711 id-expression
5712 lambda-expression (C++11)
5714 GNU Extensions:
5716 primary-expression:
5717 ( compound-statement )
5718 __builtin_va_arg ( assignment-expression , type-id )
5719 __builtin_offsetof ( type-id , offsetof-expression )
5721 C++ Extensions:
5722 __has_nothrow_assign ( type-id )
5723 __has_nothrow_constructor ( type-id )
5724 __has_nothrow_copy ( type-id )
5725 __has_trivial_assign ( type-id )
5726 __has_trivial_constructor ( type-id )
5727 __has_trivial_copy ( type-id )
5728 __has_trivial_destructor ( type-id )
5729 __has_virtual_destructor ( type-id )
5730 __is_abstract ( type-id )
5731 __is_base_of ( type-id , type-id )
5732 __is_class ( type-id )
5733 __is_empty ( type-id )
5734 __is_enum ( type-id )
5735 __is_final ( type-id )
5736 __is_literal_type ( type-id )
5737 __is_pod ( type-id )
5738 __is_polymorphic ( type-id )
5739 __is_std_layout ( type-id )
5740 __is_trivial ( type-id )
5741 __is_union ( type-id )
5743 Objective-C++ Extension:
5745 primary-expression:
5746 objc-expression
5748 literal:
5749 __null
5751 ADDRESS_P is true iff this expression was immediately preceded by
5752 "&" and therefore might denote a pointer-to-member. CAST_P is true
5753 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5754 true iff this expression is a template argument.
5756 Returns a representation of the expression. Upon return, *IDK
5757 indicates what kind of id-expression (if any) was present. */
5759 static cp_expr
5760 cp_parser_primary_expression (cp_parser *parser,
5761 bool address_p,
5762 bool cast_p,
5763 bool template_arg_p,
5764 bool decltype_p,
5765 cp_id_kind *idk)
5767 cp_token *token = NULL;
5769 /* Assume the primary expression is not an id-expression. */
5770 *idk = CP_ID_KIND_NONE;
5772 /* Peek at the next token. */
5773 token = cp_lexer_peek_token (parser->lexer);
5774 switch ((int) token->type)
5776 /* literal:
5777 integer-literal
5778 character-literal
5779 floating-literal
5780 string-literal
5781 boolean-literal
5782 pointer-literal
5783 user-defined-literal */
5784 case CPP_CHAR:
5785 case CPP_CHAR16:
5786 case CPP_CHAR32:
5787 case CPP_WCHAR:
5788 case CPP_UTF8CHAR:
5789 case CPP_NUMBER:
5790 case CPP_PREPARSED_EXPR:
5791 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5792 return cp_parser_userdef_numeric_literal (parser);
5793 token = cp_lexer_consume_token (parser->lexer);
5794 if (TREE_CODE (token->u.value) == FIXED_CST)
5796 error_at (token->location,
5797 "fixed-point types not supported in C++");
5798 return error_mark_node;
5800 /* Floating-point literals are only allowed in an integral
5801 constant expression if they are cast to an integral or
5802 enumeration type. */
5803 if ((TREE_CODE (token->u.value) == REAL_CST
5804 || (TREE_CODE (token->u.value) == EXCESS_PRECISION_EXPR
5805 && TREE_CODE (TREE_OPERAND (token->u.value, 0)) == REAL_CST))
5806 && parser->integral_constant_expression_p
5807 && pedantic)
5809 /* CAST_P will be set even in invalid code like "int(2.7 +
5810 ...)". Therefore, we have to check that the next token
5811 is sure to end the cast. */
5812 if (cast_p)
5814 cp_token *next_token;
5816 next_token = cp_lexer_peek_token (parser->lexer);
5817 if (/* The comma at the end of an
5818 enumerator-definition. */
5819 next_token->type != CPP_COMMA
5820 /* The curly brace at the end of an enum-specifier. */
5821 && next_token->type != CPP_CLOSE_BRACE
5822 /* The end of a statement. */
5823 && next_token->type != CPP_SEMICOLON
5824 /* The end of the cast-expression. */
5825 && next_token->type != CPP_CLOSE_PAREN
5826 /* The end of an array bound. */
5827 && next_token->type != CPP_CLOSE_SQUARE
5828 /* The closing ">" in a template-argument-list. */
5829 && (next_token->type != CPP_GREATER
5830 || parser->greater_than_is_operator_p)
5831 /* C++0x only: A ">>" treated like two ">" tokens,
5832 in a template-argument-list. */
5833 && (next_token->type != CPP_RSHIFT
5834 || (cxx_dialect == cxx98)
5835 || parser->greater_than_is_operator_p))
5836 cast_p = false;
5839 /* If we are within a cast, then the constraint that the
5840 cast is to an integral or enumeration type will be
5841 checked at that point. If we are not within a cast, then
5842 this code is invalid. */
5843 if (!cast_p)
5844 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5846 return (cp_expr (token->u.value, token->location, token->flags & DECIMAL_INT)
5847 .maybe_add_location_wrapper ());
5849 case CPP_CHAR_USERDEF:
5850 case CPP_CHAR16_USERDEF:
5851 case CPP_CHAR32_USERDEF:
5852 case CPP_WCHAR_USERDEF:
5853 case CPP_UTF8CHAR_USERDEF:
5854 return cp_parser_userdef_char_literal (parser);
5856 case CPP_STRING:
5857 case CPP_STRING16:
5858 case CPP_STRING32:
5859 case CPP_WSTRING:
5860 case CPP_UTF8STRING:
5861 case CPP_STRING_USERDEF:
5862 case CPP_STRING16_USERDEF:
5863 case CPP_STRING32_USERDEF:
5864 case CPP_WSTRING_USERDEF:
5865 case CPP_UTF8STRING_USERDEF:
5866 /* ??? Should wide strings be allowed when parser->translate_strings_p
5867 is false (i.e. in attributes)? If not, we can kill the third
5868 argument to cp_parser_string_literal. */
5869 if (parser->translate_strings_p)
5870 return (cp_parser_userdef_string_literal (parser,
5871 /*lookup_udlit=*/true)
5872 .maybe_add_location_wrapper ());
5873 else
5874 return (cp_parser_string_literal (parser,
5875 /*translate=*/false,
5876 /*wide_ok=*/true)
5877 .maybe_add_location_wrapper ());
5879 case CPP_OPEN_PAREN:
5880 /* If we see `( { ' then we are looking at the beginning of
5881 a GNU statement-expression. */
5882 if (cp_parser_allow_gnu_extensions_p (parser)
5883 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5885 /* Statement-expressions are not allowed by the standard. */
5886 pedwarn (token->location, OPT_Wpedantic,
5887 "ISO C++ forbids braced-groups within expressions");
5889 /* And they're not allowed outside of a function-body; you
5890 cannot, for example, write:
5892 int i = ({ int j = 3; j + 1; });
5894 at class or namespace scope. */
5895 if (!parser->in_function_body
5896 || parser->in_template_argument_list_p)
5898 error_at (token->location,
5899 "statement-expressions are not allowed outside "
5900 "functions nor in template-argument lists");
5901 cp_parser_skip_to_end_of_block_or_statement (parser);
5902 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5903 cp_lexer_consume_token (parser->lexer);
5904 return error_mark_node;
5906 else
5907 return cp_parser_statement_expr (parser);
5909 /* Otherwise it's a normal parenthesized expression. */
5911 cp_expr expr;
5912 bool saved_greater_than_is_operator_p;
5914 location_t open_paren_loc = token->location;
5916 /* Consume the `('. */
5917 matching_parens parens;
5918 parens.consume_open (parser);
5919 /* Within a parenthesized expression, a `>' token is always
5920 the greater-than operator. */
5921 saved_greater_than_is_operator_p
5922 = parser->greater_than_is_operator_p;
5923 parser->greater_than_is_operator_p = true;
5925 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5926 /* Left fold expression. */
5927 expr = NULL_TREE;
5928 else
5929 /* Parse the parenthesized expression. */
5930 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5932 token = cp_lexer_peek_token (parser->lexer);
5933 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5935 expr = cp_parser_fold_expression (parser, expr);
5936 if (expr != error_mark_node
5937 && cxx_dialect < cxx17)
5938 pedwarn (input_location, OPT_Wc__17_extensions,
5939 "fold-expressions only available with %<-std=c++17%> "
5940 "or %<-std=gnu++17%>");
5942 else
5943 /* Let the front end know that this expression was
5944 enclosed in parentheses. This matters in case, for
5945 example, the expression is of the form `A::B', since
5946 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5947 not. */
5948 expr = finish_parenthesized_expr (expr);
5950 /* DR 705: Wrapping an unqualified name in parentheses
5951 suppresses arg-dependent lookup. We want to pass back
5952 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5953 (c++/37862), but none of the others. */
5954 if (*idk != CP_ID_KIND_QUALIFIED)
5955 *idk = CP_ID_KIND_NONE;
5957 /* The `>' token might be the end of a template-id or
5958 template-parameter-list now. */
5959 parser->greater_than_is_operator_p
5960 = saved_greater_than_is_operator_p;
5962 /* Consume the `)'. */
5963 token = cp_lexer_peek_token (parser->lexer);
5964 location_t close_paren_loc = token->location;
5965 bool no_wparens = warning_suppressed_p (expr, OPT_Wparentheses);
5966 expr.set_range (open_paren_loc, close_paren_loc);
5967 if (no_wparens)
5968 suppress_warning (expr, OPT_Wparentheses);
5969 if (!parens.require_close (parser)
5970 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5971 cp_parser_skip_to_end_of_statement (parser);
5973 return expr;
5976 case CPP_OPEN_SQUARE:
5978 if (c_dialect_objc ())
5980 /* We might have an Objective-C++ message. */
5981 cp_parser_parse_tentatively (parser);
5982 tree msg = cp_parser_objc_message_expression (parser);
5983 /* If that works out, we're done ... */
5984 if (cp_parser_parse_definitely (parser))
5985 return msg;
5986 /* ... else, fall though to see if it's a lambda. */
5988 cp_expr lam = cp_parser_lambda_expression (parser);
5989 /* Don't warn about a failed tentative parse. */
5990 if (cp_parser_error_occurred (parser))
5991 return error_mark_node;
5992 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5993 return lam;
5996 case CPP_OBJC_STRING:
5997 if (c_dialect_objc ())
5998 /* We have an Objective-C++ string literal. */
5999 return cp_parser_objc_expression (parser);
6000 cp_parser_error (parser, "expected primary-expression");
6001 return error_mark_node;
6003 case CPP_KEYWORD:
6004 switch (token->keyword)
6006 /* These two are the boolean literals. */
6007 case RID_TRUE:
6008 cp_lexer_consume_token (parser->lexer);
6009 return cp_expr (boolean_true_node, token->location);
6010 case RID_FALSE:
6011 cp_lexer_consume_token (parser->lexer);
6012 return cp_expr (boolean_false_node, token->location);
6014 /* The `__null' literal. */
6015 case RID_NULL:
6016 cp_lexer_consume_token (parser->lexer);
6017 return cp_expr (null_node, token->location);
6019 /* The `nullptr' literal. */
6020 case RID_NULLPTR:
6021 cp_lexer_consume_token (parser->lexer);
6022 return cp_expr (nullptr_node, token->location);
6024 /* Recognize the `this' keyword. */
6025 case RID_THIS:
6026 cp_lexer_consume_token (parser->lexer);
6027 if (parser->local_variables_forbidden_p & THIS_FORBIDDEN)
6029 error_at (token->location,
6030 "%<this%> may not be used in this context");
6031 return error_mark_node;
6033 /* Pointers cannot appear in constant-expressions. */
6034 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
6035 return error_mark_node;
6036 return cp_expr (finish_this_expr (), token->location);
6038 /* The `operator' keyword can be the beginning of an
6039 id-expression. */
6040 case RID_OPERATOR:
6041 goto id_expression;
6043 case RID_FUNCTION_NAME:
6044 case RID_PRETTY_FUNCTION_NAME:
6045 case RID_C99_FUNCTION_NAME:
6047 non_integral_constant name;
6049 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
6050 __func__ are the names of variables -- but they are
6051 treated specially. Therefore, they are handled here,
6052 rather than relying on the generic id-expression logic
6053 below. Grammatically, these names are id-expressions.
6055 Consume the token. */
6056 token = cp_lexer_consume_token (parser->lexer);
6058 switch (token->keyword)
6060 case RID_FUNCTION_NAME:
6061 name = NIC_FUNC_NAME;
6062 break;
6063 case RID_PRETTY_FUNCTION_NAME:
6064 name = NIC_PRETTY_FUNC;
6065 break;
6066 case RID_C99_FUNCTION_NAME:
6067 name = NIC_C99_FUNC;
6068 break;
6069 default:
6070 gcc_unreachable ();
6073 if (cp_parser_non_integral_constant_expression (parser, name))
6074 return error_mark_node;
6076 /* Look up the name. */
6077 return finish_fname (token->u.value);
6080 case RID_VA_ARG:
6082 tree expression;
6083 tree type;
6084 location_t type_location;
6085 location_t start_loc
6086 = cp_lexer_peek_token (parser->lexer)->location;
6087 /* The `__builtin_va_arg' construct is used to handle
6088 `va_arg'. Consume the `__builtin_va_arg' token. */
6089 cp_lexer_consume_token (parser->lexer);
6090 /* Look for the opening `('. */
6091 matching_parens parens;
6092 parens.require_open (parser);
6093 /* Now, parse the assignment-expression. */
6094 expression = cp_parser_assignment_expression (parser);
6095 /* Look for the `,'. */
6096 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
6097 type_location = cp_lexer_peek_token (parser->lexer)->location;
6098 /* Parse the type-id. */
6100 type_id_in_expr_sentinel s (parser);
6101 type = cp_parser_type_id (parser);
6103 /* Look for the closing `)'. */
6104 location_t finish_loc
6105 = cp_lexer_peek_token (parser->lexer)->location;
6106 parens.require_close (parser);
6107 /* Using `va_arg' in a constant-expression is not
6108 allowed. */
6109 if (cp_parser_non_integral_constant_expression (parser,
6110 NIC_VA_ARG))
6111 return error_mark_node;
6112 /* Construct a location of the form:
6113 __builtin_va_arg (v, int)
6114 ~~~~~~~~~~~~~~~~~~~~~^~~~
6115 with the caret at the type, ranging from the start of the
6116 "__builtin_va_arg" token to the close paren. */
6117 location_t combined_loc
6118 = make_location (type_location, start_loc, finish_loc);
6119 return build_x_va_arg (combined_loc, expression, type);
6122 case RID_OFFSETOF:
6123 return cp_parser_builtin_offsetof (parser);
6125 // C++ concepts
6126 case RID_REQUIRES:
6127 return cp_parser_requires_expression (parser);
6129 /* Objective-C++ expressions. */
6130 case RID_AT_ENCODE:
6131 case RID_AT_PROTOCOL:
6132 case RID_AT_SELECTOR:
6133 return cp_parser_objc_expression (parser);
6135 case RID_OMP_ALL_MEMORY:
6136 gcc_assert (flag_openmp);
6137 cp_lexer_consume_token (parser->lexer);
6138 error_at (token->location,
6139 "%<omp_all_memory%> may only be used in OpenMP "
6140 "%<depend%> clause");
6141 return error_mark_node;
6143 case RID_TEMPLATE:
6144 if (parser->in_function_body
6145 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6146 == CPP_LESS))
6148 error_at (token->location,
6149 "a template declaration cannot appear at block scope");
6150 cp_parser_skip_to_end_of_block_or_statement (parser);
6151 return error_mark_node;
6153 /* FALLTHRU */
6154 default:
6155 cp_parser_error (parser, "expected primary-expression");
6156 return error_mark_node;
6159 /* An id-expression can start with either an identifier, a
6160 `::' as the beginning of a qualified-id, or the "operator"
6161 keyword. */
6162 case CPP_NAME:
6163 if (const cp_trait* trait = cp_lexer_peek_trait_expr (parser->lexer))
6164 return cp_parser_trait (parser, trait);
6165 /* FALLTHRU */
6166 case CPP_SCOPE:
6167 case CPP_TEMPLATE_ID:
6168 case CPP_NESTED_NAME_SPECIFIER:
6170 id_expression:
6171 cp_expr id_expression;
6172 cp_expr decl;
6173 const char *error_msg;
6174 bool template_p;
6175 bool done;
6176 cp_token *id_expr_token;
6178 /* Parse the id-expression. */
6179 id_expression
6180 = cp_parser_id_expression (parser,
6181 /*template_keyword_p=*/false,
6182 /*check_dependency_p=*/true,
6183 &template_p,
6184 /*declarator_p=*/false,
6185 /*optional_p=*/false);
6186 if (id_expression == error_mark_node)
6187 return error_mark_node;
6188 id_expr_token = token;
6189 token = cp_lexer_peek_token (parser->lexer);
6190 done = (token->type != CPP_OPEN_SQUARE
6191 && token->type != CPP_OPEN_PAREN
6192 && token->type != CPP_DOT
6193 && token->type != CPP_DEREF
6194 && token->type != CPP_PLUS_PLUS
6195 && token->type != CPP_MINUS_MINUS);
6196 /* If we have a template-id, then no further lookup is
6197 required. If the template-id was for a template-class, we
6198 will sometimes have a TYPE_DECL at this point. */
6199 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
6200 || TREE_CODE (id_expression) == TYPE_DECL)
6201 decl = id_expression;
6202 /* Look up the name. */
6203 else
6205 tree ambiguous_decls;
6207 /* If we already know that this lookup is ambiguous, then
6208 we've already issued an error message; there's no reason
6209 to check again. */
6210 if (id_expr_token->type == CPP_NAME
6211 && id_expr_token->error_reported)
6213 cp_parser_simulate_error (parser);
6214 return error_mark_node;
6217 decl = cp_parser_lookup_name (parser, id_expression,
6218 none_type,
6219 template_p,
6220 /*is_namespace=*/false,
6221 /*check_dependency=*/true,
6222 &ambiguous_decls,
6223 id_expression.get_location ());
6224 /* If the lookup was ambiguous, an error will already have
6225 been issued. */
6226 if (ambiguous_decls)
6227 return error_mark_node;
6229 /* In Objective-C++, we may have an Objective-C 2.0
6230 dot-syntax for classes here. */
6231 if (c_dialect_objc ()
6232 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
6233 && TREE_CODE (decl) == TYPE_DECL
6234 && objc_is_class_name (decl))
6236 tree component;
6237 cp_lexer_consume_token (parser->lexer);
6238 component = cp_parser_identifier (parser);
6239 if (component == error_mark_node)
6240 return error_mark_node;
6242 tree result = objc_build_class_component_ref (id_expression,
6243 component);
6244 /* Build a location of the form:
6245 expr.component
6246 ~~~~~^~~~~~~~~
6247 with caret at the start of the component name (at
6248 input_location), ranging from the start of the id_expression
6249 to the end of the component name. */
6250 location_t combined_loc
6251 = make_location (input_location, id_expression.get_start (),
6252 get_finish (input_location));
6253 protected_set_expr_location (result, combined_loc);
6254 return result;
6257 /* In Objective-C++, an instance variable (ivar) may be preferred
6258 to whatever cp_parser_lookup_name() found.
6259 Call objc_lookup_ivar. To avoid exposing cp_expr to the
6260 rest of c-family, we have to do a little extra work to preserve
6261 any location information in cp_expr "decl". Given that
6262 objc_lookup_ivar is implemented in "c-family" and "objc", we
6263 have a trip through the pure "tree" type, rather than cp_expr.
6264 Naively copying it back to "decl" would implicitly give the
6265 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
6266 store an EXPR_LOCATION. Hence we only update "decl" (and
6267 hence its location_t) if we get back a different tree node. */
6268 tree decl_tree = objc_lookup_ivar (decl.get_value (),
6269 id_expression);
6270 if (decl_tree != decl.get_value ())
6271 decl = cp_expr (decl_tree);
6273 /* If name lookup gives us a SCOPE_REF, then the
6274 qualifying scope was dependent. */
6275 if (TREE_CODE (decl) == SCOPE_REF)
6277 /* At this point, we do not know if DECL is a valid
6278 integral constant expression. We assume that it is
6279 in fact such an expression, so that code like:
6281 template <int N> struct A {
6282 int a[B<N>::i];
6285 is accepted. At template-instantiation time, we
6286 will check that B<N>::i is actually a constant. */
6287 return decl;
6289 /* Check to see if DECL is a local variable in a context
6290 where that is forbidden. */
6291 if ((parser->local_variables_forbidden_p & LOCAL_VARS_FORBIDDEN)
6292 && local_variable_p (decl)
6293 /* DR 2082 permits local variables in unevaluated contexts
6294 within a default argument. */
6295 && !cp_unevaluated_operand)
6297 const char *msg
6298 = (TREE_CODE (decl) == PARM_DECL
6299 ? G_("parameter %qD may not appear in this context")
6300 : G_("local variable %qD may not appear in this context"));
6301 error_at (id_expression.get_location (), msg,
6302 decl.get_value ());
6303 return error_mark_node;
6307 decl = (finish_id_expression
6308 (id_expression, decl, parser->scope,
6309 idk,
6310 parser->integral_constant_expression_p,
6311 parser->allow_non_integral_constant_expression_p,
6312 &parser->non_integral_constant_expression_p,
6313 template_p, done, address_p,
6314 template_arg_p,
6315 &error_msg,
6316 id_expression.get_location ()));
6317 if (error_msg)
6318 cp_parser_error (parser, error_msg);
6319 /* Build a location for an id-expression of the form:
6320 ::ns::id
6321 ~~~~~~^~
6325 i.e. from the start of the first token to the end of the final
6326 token, with the caret at the start of the unqualified-id. */
6327 location_t caret_loc = get_pure_location (id_expression.get_location ());
6328 location_t start_loc = get_start (id_expr_token->location);
6329 location_t finish_loc = get_finish (id_expression.get_location ());
6330 location_t combined_loc
6331 = make_location (caret_loc, start_loc, finish_loc);
6333 decl.set_location (combined_loc);
6334 return decl;
6337 /* Anything else is an error. */
6338 default:
6339 cp_parser_error (parser, "expected primary-expression");
6340 return error_mark_node;
6344 static inline cp_expr
6345 cp_parser_primary_expression (cp_parser *parser,
6346 bool address_p,
6347 bool cast_p,
6348 bool template_arg_p,
6349 cp_id_kind *idk)
6351 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
6352 /*decltype*/false, idk);
6355 /* Complain about missing template keyword when naming a dependent
6356 member template. */
6358 static void
6359 missing_template_diag (location_t loc, diagnostic_t diag_kind = DK_WARNING)
6361 if (warning_suppressed_at (loc, OPT_Wmissing_template_keyword))
6362 return;
6364 gcc_rich_location richloc (loc);
6365 richloc.add_fixit_insert_before ("template");
6366 emit_diagnostic (diag_kind, &richloc, OPT_Wmissing_template_keyword,
6367 "expected %qs keyword before dependent "
6368 "template name", "template");
6369 suppress_warning_at (loc, OPT_Wmissing_template_keyword);
6372 /* Parse an id-expression.
6374 id-expression:
6375 unqualified-id
6376 qualified-id
6378 qualified-id:
6379 :: [opt] nested-name-specifier template [opt] unqualified-id
6380 :: identifier
6381 :: operator-function-id
6382 :: template-id
6384 Return a representation of the unqualified portion of the
6385 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
6386 a `::' or nested-name-specifier.
6388 Often, if the id-expression was a qualified-id, the caller will
6389 want to make a SCOPE_REF to represent the qualified-id. This
6390 function does not do this in order to avoid wastefully creating
6391 SCOPE_REFs when they are not required.
6393 If TEMPLATE_KEYWORD_P is true, then we have just seen the
6394 `template' keyword.
6396 If CHECK_DEPENDENCY_P is false, then names are looked up inside
6397 uninstantiated templates.
6399 If *TEMPLATE_P is non-NULL, it is set to true iff the
6400 `template' keyword is used to explicitly indicate that the entity
6401 named is a template.
6403 If DECLARATOR_P is true, the id-expression is appearing as part of
6404 a declarator, rather than as part of an expression. */
6406 static cp_expr
6407 cp_parser_id_expression (cp_parser *parser,
6408 bool template_keyword_p,
6409 bool check_dependency_p,
6410 bool *template_p,
6411 bool declarator_p,
6412 bool optional_p)
6414 bool global_scope_p;
6415 bool nested_name_specifier_p;
6417 /* Assume the `template' keyword was not used. */
6418 if (template_p)
6419 *template_p = template_keyword_p;
6421 /* Look for the optional `::' operator. */
6422 global_scope_p
6423 = (!template_keyword_p
6424 && (cp_parser_global_scope_opt (parser,
6425 /*current_scope_valid_p=*/false)
6426 != NULL_TREE));
6428 /* Look for the optional nested-name-specifier. */
6429 nested_name_specifier_p
6430 = (cp_parser_nested_name_specifier_opt (parser,
6431 /*typename_keyword_p=*/false,
6432 check_dependency_p,
6433 /*type_p=*/false,
6434 declarator_p,
6435 template_keyword_p)
6436 != NULL_TREE);
6438 cp_expr id = NULL_TREE;
6439 tree scope = parser->scope;
6441 /* Peek at the next token. */
6442 cp_token *token = cp_lexer_peek_token (parser->lexer);
6444 /* If there is a nested-name-specifier, then we are looking at
6445 the first qualified-id production. */
6446 if (nested_name_specifier_p)
6448 tree saved_object_scope;
6449 tree saved_qualifying_scope;
6451 /* See if the next token is the `template' keyword. */
6452 if (!template_p)
6453 template_p = &template_keyword_p;
6454 *template_p = cp_parser_optional_template_keyword (parser);
6455 /* Name lookup we do during the processing of the
6456 unqualified-id might obliterate SCOPE. */
6457 saved_object_scope = parser->object_scope;
6458 saved_qualifying_scope = parser->qualifying_scope;
6459 /* Process the final unqualified-id. */
6460 id = cp_parser_unqualified_id (parser, *template_p,
6461 check_dependency_p,
6462 declarator_p,
6463 /*optional_p=*/false);
6464 /* Restore the SAVED_SCOPE for our caller. */
6465 parser->scope = scope;
6466 parser->object_scope = saved_object_scope;
6467 parser->qualifying_scope = saved_qualifying_scope;
6469 /* Otherwise, if we are in global scope, then we are looking at one
6470 of the other qualified-id productions. */
6471 else if (global_scope_p)
6473 /* If it's an identifier, and the next token is not a "<", then
6474 we can avoid the template-id case. This is an optimization
6475 for this common case. */
6476 if (token->type == CPP_NAME
6477 && !cp_parser_nth_token_starts_template_argument_list_p
6478 (parser, 2))
6479 return cp_parser_identifier (parser);
6481 cp_parser_parse_tentatively (parser);
6482 /* Try a template-id. */
6483 id = cp_parser_template_id_expr (parser,
6484 /*template_keyword_p=*/false,
6485 /*check_dependency_p=*/true,
6486 declarator_p);
6487 /* If that worked, we're done. */
6488 if (cp_parser_parse_definitely (parser))
6489 return id;
6491 /* Peek at the next token. (Changes in the token buffer may
6492 have invalidated the pointer obtained above.) */
6493 token = cp_lexer_peek_token (parser->lexer);
6495 switch (token->type)
6497 case CPP_NAME:
6498 id = cp_parser_identifier (parser);
6499 break;
6501 case CPP_KEYWORD:
6502 if (token->keyword == RID_OPERATOR)
6504 id = cp_parser_operator_function_id (parser);
6505 break;
6507 /* Fall through. */
6509 default:
6510 cp_parser_error (parser, "expected id-expression");
6511 return error_mark_node;
6514 else
6516 if (!scope)
6517 scope = parser->context->object_type;
6518 id = cp_parser_unqualified_id (parser, template_keyword_p,
6519 /*check_dependency_p=*/true,
6520 declarator_p,
6521 optional_p);
6524 if (id && TREE_CODE (id) == IDENTIFIER_NODE
6525 && warn_missing_template_keyword
6526 && !template_keyword_p
6527 /* Don't warn if we're looking inside templates. */
6528 && check_dependency_p
6529 /* In a template argument list a > could be closing
6530 the enclosing targs. */
6531 && !parser->in_template_argument_list_p
6532 && scope && dependentish_scope_p (scope)
6533 /* Don't confuse an ill-formed constructor declarator for a missing
6534 template keyword in a return type. */
6535 && !(declarator_p && constructor_name_p (id, scope))
6536 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1)
6537 && warning_enabled_at (token->location,
6538 OPT_Wmissing_template_keyword))
6540 saved_token_sentinel toks (parser->lexer, STS_ROLLBACK);
6541 if (cp_parser_skip_entire_template_parameter_list (parser)
6542 /* An operator after the > suggests that the > ends a
6543 template-id; a name or literal suggests that the > is an
6544 operator. */
6545 && (cp_lexer_peek_token (parser->lexer)->type
6546 <= CPP_LAST_PUNCTUATOR))
6547 missing_template_diag (token->location);
6550 return id;
6553 /* Parse an unqualified-id.
6555 unqualified-id:
6556 identifier
6557 operator-function-id
6558 conversion-function-id
6559 ~ class-name
6560 template-id
6562 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
6563 keyword, in a construct like `A::template ...'.
6565 Returns a representation of unqualified-id. For the `identifier'
6566 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
6567 production a BIT_NOT_EXPR is returned; the operand of the
6568 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
6569 other productions, see the documentation accompanying the
6570 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
6571 names are looked up in uninstantiated templates. If DECLARATOR_P
6572 is true, the unqualified-id is appearing as part of a declarator,
6573 rather than as part of an expression. */
6575 static cp_expr
6576 cp_parser_unqualified_id (cp_parser* parser,
6577 bool template_keyword_p,
6578 bool check_dependency_p,
6579 bool declarator_p,
6580 bool optional_p)
6582 cp_token *token;
6584 /* Peek at the next token. */
6585 token = cp_lexer_peek_token (parser->lexer);
6587 switch ((int) token->type)
6589 case CPP_NAME:
6591 tree id;
6593 /* We don't know yet whether or not this will be a
6594 template-id. */
6595 cp_parser_parse_tentatively (parser);
6596 /* Try a template-id. */
6597 id = cp_parser_template_id_expr (parser, template_keyword_p,
6598 check_dependency_p,
6599 declarator_p);
6600 /* If it worked, we're done. */
6601 if (cp_parser_parse_definitely (parser))
6602 return id;
6603 /* Otherwise, it's an ordinary identifier. */
6604 return cp_parser_identifier (parser);
6607 case CPP_TEMPLATE_ID:
6608 return cp_parser_template_id_expr (parser, template_keyword_p,
6609 check_dependency_p,
6610 declarator_p);
6612 case CPP_COMPL:
6614 tree type_decl;
6615 tree qualifying_scope;
6616 tree object_scope;
6617 tree scope;
6618 bool done;
6619 location_t tilde_loc = token->location;
6621 /* Consume the `~' token. */
6622 cp_lexer_consume_token (parser->lexer);
6623 /* Parse the class-name. The standard, as written, seems to
6624 say that:
6626 template <typename T> struct S { ~S (); };
6627 template <typename T> S<T>::~S() {}
6629 is invalid, since `~' must be followed by a class-name, but
6630 `S<T>' is dependent, and so not known to be a class.
6631 That's not right; we need to look in uninstantiated
6632 templates. A further complication arises from:
6634 template <typename T> void f(T t) {
6635 t.T::~T();
6638 Here, it is not possible to look up `T' in the scope of `T'
6639 itself. We must look in both the current scope, and the
6640 scope of the containing complete expression.
6642 Yet another issue is:
6644 struct S {
6645 int S;
6646 ~S();
6649 S::~S() {}
6651 The standard does not seem to say that the `S' in `~S'
6652 should refer to the type `S' and not the data member
6653 `S::S'. */
6655 /* DR 244 says that we look up the name after the "~" in the
6656 same scope as we looked up the qualifying name. That idea
6657 isn't fully worked out; it's more complicated than that. */
6658 scope = parser->scope;
6659 object_scope = parser->object_scope;
6660 qualifying_scope = parser->qualifying_scope;
6662 /* Check for invalid scopes. */
6663 if (scope == error_mark_node)
6665 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
6666 cp_lexer_consume_token (parser->lexer);
6667 return error_mark_node;
6669 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
6671 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6672 error_at (token->location,
6673 "scope %qT before %<~%> is not a class-name",
6674 scope);
6675 cp_parser_simulate_error (parser);
6676 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
6677 cp_lexer_consume_token (parser->lexer);
6678 return error_mark_node;
6680 if (template_keyword_p)
6682 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6683 error_at (tilde_loc, "%<template%> keyword not permitted in "
6684 "destructor name");
6685 cp_parser_simulate_error (parser);
6686 return error_mark_node;
6689 gcc_assert (!scope || TYPE_P (scope));
6691 token = cp_lexer_peek_token (parser->lexer);
6693 /* Create a location with caret == start at the tilde,
6694 finishing at the end of the peeked token, e.g:
6695 ~token
6696 ^~~~~~. */
6697 location_t loc
6698 = make_location (tilde_loc, tilde_loc, token->location);
6700 /* If the name is of the form "X::~X" it's OK even if X is a
6701 typedef. */
6703 if (scope
6704 && token->type == CPP_NAME
6705 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6706 != CPP_LESS)
6707 && (token->u.value == TYPE_IDENTIFIER (scope)
6708 || (CLASS_TYPE_P (scope)
6709 && constructor_name_p (token->u.value, scope))))
6711 cp_lexer_consume_token (parser->lexer);
6712 return build_min_nt_loc (loc, BIT_NOT_EXPR, scope);
6715 /* ~auto means the destructor of whatever the object is. */
6716 if (cp_parser_is_keyword (token, RID_AUTO))
6718 if (cxx_dialect < cxx14)
6719 pedwarn (loc, OPT_Wc__14_extensions,
6720 "%<~auto%> only available with "
6721 "%<-std=c++14%> or %<-std=gnu++14%>");
6722 cp_lexer_consume_token (parser->lexer);
6723 return build_min_nt_loc (loc, BIT_NOT_EXPR, make_auto ());
6726 /* DR 2237 (C++20 only): A simple-template-id is no longer valid as the
6727 declarator-id of a constructor or destructor. */
6728 if (token->type == CPP_TEMPLATE_ID && declarator_p)
6730 auto_diagnostic_group d;
6731 bool w = false;
6732 if (cxx_dialect >= cxx20 && !cp_parser_simulate_error (parser))
6733 w = pedwarn (tilde_loc, OPT_Wtemplate_id_cdtor,
6734 "template-id not allowed for destructor in C++20");
6735 else if (cxx_dialect < cxx20
6736 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6737 w = warning_at (tilde_loc, OPT_Wtemplate_id_cdtor,
6738 "template-id not allowed for destructor in C++20");
6739 if (w)
6740 inform (tilde_loc, "remove the %qs", "< >");
6743 /* If there was an explicit qualification (S::~T), first look
6744 in the scope given by the qualification (i.e., S).
6746 Note: in the calls to cp_parser_class_name below we pass
6747 typename_type so that lookup finds the injected-class-name
6748 rather than the constructor. */
6749 done = false;
6750 type_decl = NULL_TREE;
6751 if (scope)
6753 cp_parser_parse_tentatively (parser);
6754 type_decl = cp_parser_class_name (parser,
6755 /*typename_keyword_p=*/false,
6756 /*template_keyword_p=*/false,
6757 typename_type,
6758 /*check_dependency=*/false,
6759 /*class_head_p=*/false,
6760 declarator_p);
6761 if (cp_parser_parse_definitely (parser))
6762 done = true;
6764 /* In "N::S::~S", look in "N" as well. */
6765 if (!done && scope && qualifying_scope)
6767 cp_parser_parse_tentatively (parser);
6768 parser->scope = qualifying_scope;
6769 parser->object_scope = NULL_TREE;
6770 parser->qualifying_scope = NULL_TREE;
6771 type_decl
6772 = cp_parser_class_name (parser,
6773 /*typename_keyword_p=*/false,
6774 /*template_keyword_p=*/false,
6775 typename_type,
6776 /*check_dependency=*/false,
6777 /*class_head_p=*/false,
6778 declarator_p);
6779 if (cp_parser_parse_definitely (parser))
6780 done = true;
6782 /* In "p->S::~T", look in the scope given by "*p" as well. */
6783 else if (!done && object_scope)
6785 cp_parser_parse_tentatively (parser);
6786 parser->scope = object_scope;
6787 parser->object_scope = NULL_TREE;
6788 parser->qualifying_scope = NULL_TREE;
6789 type_decl
6790 = cp_parser_class_name (parser,
6791 /*typename_keyword_p=*/false,
6792 /*template_keyword_p=*/false,
6793 typename_type,
6794 /*check_dependency=*/false,
6795 /*class_head_p=*/false,
6796 declarator_p);
6797 if (cp_parser_parse_definitely (parser))
6798 done = true;
6800 /* Look in the surrounding context. */
6801 if (!done)
6803 parser->scope = NULL_TREE;
6804 parser->object_scope = NULL_TREE;
6805 parser->qualifying_scope = NULL_TREE;
6806 if (processing_template_decl)
6807 cp_parser_parse_tentatively (parser);
6808 type_decl
6809 = cp_parser_class_name (parser,
6810 /*typename_keyword_p=*/false,
6811 /*template_keyword_p=*/false,
6812 typename_type,
6813 /*check_dependency=*/false,
6814 /*class_head_p=*/false,
6815 declarator_p);
6816 if (processing_template_decl
6817 && ! cp_parser_parse_definitely (parser))
6819 /* We couldn't find a type with this name. If we're parsing
6820 tentatively, fail and try something else. */
6821 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6823 cp_parser_simulate_error (parser);
6824 return error_mark_node;
6826 /* Otherwise, accept it and check for a match at instantiation
6827 time. */
6828 type_decl = cp_parser_identifier (parser);
6829 if (type_decl != error_mark_node)
6830 type_decl = build_min_nt_loc (loc, BIT_NOT_EXPR, type_decl);
6831 return type_decl;
6834 /* If an error occurred, assume that the name of the
6835 destructor is the same as the name of the qualifying
6836 class. That allows us to keep parsing after running
6837 into ill-formed destructor names. */
6838 if (type_decl == error_mark_node && scope)
6839 return build_min_nt_loc (loc, BIT_NOT_EXPR, scope);
6840 else if (type_decl == error_mark_node)
6841 return error_mark_node;
6843 /* Check that destructor name and scope match. */
6844 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6846 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6847 error_at (loc,
6848 "declaration of %<~%T%> as member of %qT",
6849 type_decl, scope);
6850 cp_parser_simulate_error (parser);
6851 return error_mark_node;
6854 /* [class.dtor]
6856 A typedef-name that names a class shall not be used as the
6857 identifier in the declarator for a destructor declaration. */
6858 if (declarator_p
6859 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6860 && !DECL_SELF_REFERENCE_P (type_decl)
6861 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6862 error_at (loc,
6863 "typedef-name %qD used as destructor declarator",
6864 type_decl);
6866 return build_min_nt_loc (loc, BIT_NOT_EXPR, TREE_TYPE (type_decl));
6869 case CPP_KEYWORD:
6870 if (token->keyword == RID_OPERATOR)
6872 cp_expr id;
6874 /* This could be a template-id, so we try that first. */
6875 cp_parser_parse_tentatively (parser);
6876 /* Try a template-id. */
6877 id = cp_parser_template_id_expr (parser, template_keyword_p,
6878 /*check_dependency_p=*/true,
6879 declarator_p);
6880 /* If that worked, we're done. */
6881 if (cp_parser_parse_definitely (parser))
6882 return id;
6883 /* We still don't know whether we're looking at an
6884 operator-function-id or a conversion-function-id. */
6885 cp_parser_parse_tentatively (parser);
6886 /* Try an operator-function-id. */
6887 id = cp_parser_operator_function_id (parser);
6888 /* If that didn't work, try a conversion-function-id. */
6889 if (!cp_parser_parse_definitely (parser))
6890 id = cp_parser_conversion_function_id (parser);
6892 return id;
6894 /* Fall through. */
6896 default:
6897 if (optional_p)
6898 return NULL_TREE;
6899 cp_parser_error (parser, "expected unqualified-id");
6900 return error_mark_node;
6904 /* Check [temp.names]/5: A name prefixed by the keyword template shall
6905 be a template-id or the name shall refer to a class template or an
6906 alias template. */
6908 static void
6909 check_template_keyword_in_nested_name_spec (tree name)
6911 if (CLASS_TYPE_P (name)
6912 && ((CLASSTYPE_USE_TEMPLATE (name)
6913 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (name)))
6914 || CLASSTYPE_IS_TEMPLATE (name)))
6915 return;
6917 if (TREE_CODE (name) == TYPENAME_TYPE
6918 && TREE_CODE (TYPENAME_TYPE_FULLNAME (name)) == TEMPLATE_ID_EXPR)
6919 return;
6920 /* Alias templates are also OK. */
6921 else if (alias_template_specialization_p (name, nt_opaque))
6922 return;
6924 permerror (input_location, TYPE_P (name)
6925 ? G_("%qT is not a template")
6926 : G_("%qD is not a template"),
6927 name);
6930 /* Parse an (optional) nested-name-specifier.
6932 nested-name-specifier: [C++98]
6933 class-or-namespace-name :: nested-name-specifier [opt]
6934 class-or-namespace-name :: template nested-name-specifier [opt]
6936 nested-name-specifier: [C++0x]
6937 type-name ::
6938 namespace-name ::
6939 nested-name-specifier identifier ::
6940 nested-name-specifier template [opt] simple-template-id ::
6942 PARSER->SCOPE should be set appropriately before this function is
6943 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6944 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6945 in name lookups.
6947 Sets PARSER->SCOPE to the class (TYPE) or namespace
6948 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6949 it unchanged if there is no nested-name-specifier. Returns the new
6950 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6952 If CHECK_DEPENDENCY_P is FALSE, names are looked up in dependent scopes.
6954 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6955 part of a declaration and/or decl-specifier. */
6957 static tree
6958 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6959 bool typename_keyword_p,
6960 bool check_dependency_p,
6961 bool type_p,
6962 bool is_declaration,
6963 bool template_keyword_p /* = false */)
6965 bool success = false;
6966 cp_token_position start = 0;
6967 cp_token *token;
6969 /* Remember where the nested-name-specifier starts. */
6970 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
6971 && cp_lexer_next_token_is_not (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
6973 start = cp_lexer_token_position (parser->lexer, false);
6974 push_deferring_access_checks (dk_deferred);
6977 while (true)
6979 tree new_scope;
6980 tree old_scope;
6981 tree saved_qualifying_scope;
6983 /* Spot cases that cannot be the beginning of a
6984 nested-name-specifier. */
6985 token = cp_lexer_peek_token (parser->lexer);
6987 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6988 the already parsed nested-name-specifier. */
6989 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6991 /* Grab the nested-name-specifier and continue the loop. */
6992 cp_parser_pre_parsed_nested_name_specifier (parser);
6993 /* If we originally encountered this nested-name-specifier
6994 with CHECK_DEPENDENCY_P set to true, we will not have
6995 resolved TYPENAME_TYPEs, so we must do so here. */
6996 if (is_declaration
6997 && !check_dependency_p
6998 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
7000 new_scope = resolve_typename_type (parser->scope,
7001 /*only_current_p=*/false);
7002 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
7003 parser->scope = new_scope;
7005 success = true;
7006 continue;
7009 /* Spot cases that cannot be the beginning of a
7010 nested-name-specifier. On the second and subsequent times
7011 through the loop, we look for the `template' keyword. */
7012 if (success && token->keyword == RID_TEMPLATE)
7014 /* A template-id can start a nested-name-specifier. */
7015 else if (token->type == CPP_TEMPLATE_ID)
7017 /* DR 743: decltype can be used in a nested-name-specifier. */
7018 else if (token_is_decltype (token))
7020 else
7022 /* If the next token is not an identifier, then it is
7023 definitely not a type-name or namespace-name. */
7024 if (token->type != CPP_NAME)
7025 break;
7026 /* If the following token is neither a `<' (to begin a
7027 template-id), nor a `::', then we are not looking at a
7028 nested-name-specifier. */
7029 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7031 if (token->type == CPP_COLON
7032 && parser->colon_corrects_to_scope_p
7033 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME
7034 /* name:name is a valid sequence in an Objective C message. */
7035 && !parser->objective_c_message_context_p)
7037 gcc_rich_location richloc (token->location);
7038 richloc.add_fixit_replace ("::");
7039 error_at (&richloc,
7040 "found %<:%> in nested-name-specifier, "
7041 "expected %<::%>");
7042 token->type = CPP_SCOPE;
7045 if (token->type != CPP_SCOPE
7046 && !cp_parser_nth_token_starts_template_argument_list_p
7047 (parser, 2))
7048 break;
7051 /* The nested-name-specifier is optional, so we parse
7052 tentatively. */
7053 cp_parser_parse_tentatively (parser);
7055 /* Look for the optional `template' keyword, if this isn't the
7056 first time through the loop. */
7057 if (success)
7059 template_keyword_p = cp_parser_optional_template_keyword (parser);
7060 /* DR1710: "In a qualified-id used as the name in
7061 a typename-specifier, elaborated-type-specifier, using-declaration,
7062 or class-or-decltype, an optional keyword template appearing at
7063 the top level is ignored." */
7064 if (!template_keyword_p
7065 && typename_keyword_p
7066 && cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
7067 template_keyword_p = true;
7070 /* Save the old scope since the name lookup we are about to do
7071 might destroy it. */
7072 old_scope = parser->scope;
7073 saved_qualifying_scope = parser->qualifying_scope;
7074 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
7075 look up names in "X<T>::I" in order to determine that "Y" is
7076 a template. So, if we have a typename at this point, we make
7077 an effort to look through it. */
7078 if (is_declaration
7079 && !check_dependency_p
7080 && !typename_keyword_p
7081 && parser->scope
7082 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
7083 parser->scope = resolve_typename_type (parser->scope,
7084 /*only_current_p=*/false);
7085 /* Parse the qualifying entity. */
7086 new_scope
7087 = cp_parser_qualifying_entity (parser,
7088 typename_keyword_p,
7089 template_keyword_p,
7090 check_dependency_p,
7091 type_p,
7092 is_declaration);
7093 /* Look for the `::' token. */
7094 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7096 /* If we found what we wanted, we keep going; otherwise, we're
7097 done. */
7098 if (!cp_parser_parse_definitely (parser))
7100 bool error_p = false;
7102 /* Restore the OLD_SCOPE since it was valid before the
7103 failed attempt at finding the last
7104 class-or-namespace-name. */
7105 parser->scope = old_scope;
7106 parser->qualifying_scope = saved_qualifying_scope;
7108 /* If the next token is a decltype, and the one after that is a
7109 `::', then the decltype has failed to resolve to a class or
7110 enumeration type. Give this error even when parsing
7111 tentatively since it can't possibly be valid--and we're going
7112 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
7113 won't get another chance.*/
7114 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
7115 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
7116 == CPP_SCOPE))
7118 token = cp_lexer_consume_token (parser->lexer);
7119 tree dtype = token->u.tree_check_value->value;
7120 if (dtype != error_mark_node)
7121 error_at (token->location, "%<decltype%> evaluates to %qT, "
7122 "which is not a class or enumeration type",
7123 dtype);
7124 parser->scope = error_mark_node;
7125 error_p = true;
7126 /* As below. */
7127 success = true;
7128 cp_lexer_consume_token (parser->lexer);
7131 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
7132 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
7134 /* If we have a non-type template-id followed by ::, it can't
7135 possibly be valid. */
7136 token = cp_lexer_peek_token (parser->lexer);
7137 tree tid = token->u.tree_check_value->value;
7138 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
7139 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
7141 tree tmpl = NULL_TREE;
7142 if (is_overloaded_fn (tid))
7144 tree fns = get_fns (tid);
7145 if (OVL_SINGLE_P (fns))
7146 tmpl = OVL_FIRST (fns);
7147 if (function_concept_p (fns))
7148 error_at (token->location, "concept-id %qD "
7149 "in nested-name-specifier", tid);
7150 else
7151 error_at (token->location, "function template-id "
7152 "%qD in nested-name-specifier", tid);
7154 else
7156 tmpl = TREE_OPERAND (tid, 0);
7157 if (variable_concept_p (tmpl)
7158 || standard_concept_p (tmpl))
7159 error_at (token->location, "concept-id %qD "
7160 "in nested-name-specifier", tid);
7161 else
7163 /* Variable template. */
7164 gcc_assert (variable_template_p (tmpl));
7165 error_at (token->location, "variable template-id "
7166 "%qD in nested-name-specifier", tid);
7169 if (tmpl)
7170 inform (DECL_SOURCE_LOCATION (tmpl),
7171 "%qD declared here", tmpl);
7173 parser->scope = error_mark_node;
7174 error_p = true;
7175 /* As below. */
7176 success = true;
7177 cp_lexer_consume_token (parser->lexer);
7178 cp_lexer_consume_token (parser->lexer);
7182 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
7183 break;
7184 /* If the next token is an identifier, and the one after
7185 that is a `::', then any valid interpretation would have
7186 found a class-or-namespace-name. */
7187 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
7188 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
7189 == CPP_SCOPE)
7190 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7191 != CPP_COMPL))
7193 token = cp_lexer_consume_token (parser->lexer);
7194 if (!error_p)
7196 if (!token->error_reported)
7198 tree decl;
7199 tree ambiguous_decls;
7201 decl = cp_parser_lookup_name (parser, token->u.value,
7202 none_type,
7203 /*is_template=*/false,
7204 /*is_namespace=*/false,
7205 /*check_dependency=*/true,
7206 &ambiguous_decls,
7207 token->location);
7208 if (TREE_CODE (decl) == TEMPLATE_DECL)
7209 error_at (token->location,
7210 "%qD used without template arguments",
7211 decl);
7212 else if (ambiguous_decls)
7214 // cp_parser_lookup_name has the same diagnostic,
7215 // thus make sure to emit it at most once.
7216 if (cp_parser_uncommitted_to_tentative_parse_p
7217 (parser))
7219 error_at (token->location,
7220 "reference to %qD is ambiguous",
7221 token->u.value);
7222 print_candidates (ambiguous_decls);
7224 decl = error_mark_node;
7226 else
7228 if (cxx_dialect != cxx98)
7229 cp_parser_name_lookup_error
7230 (parser, token->u.value, decl, NLE_NOT_CXX98,
7231 token->location);
7232 else
7233 cp_parser_name_lookup_error
7234 (parser, token->u.value, decl, NLE_CXX98,
7235 token->location);
7238 parser->scope = error_mark_node;
7239 error_p = true;
7240 /* Treat this as a successful nested-name-specifier
7241 due to:
7243 [basic.lookup.qual]
7245 If the name found is not a class-name (clause
7246 _class_) or namespace-name (_namespace.def_), the
7247 program is ill-formed. */
7248 success = true;
7250 cp_lexer_consume_token (parser->lexer);
7252 break;
7254 /* We've found one valid nested-name-specifier. */
7255 success = true;
7256 /* Name lookup always gives us a DECL. */
7257 if (TREE_CODE (new_scope) == TYPE_DECL)
7258 new_scope = TREE_TYPE (new_scope);
7259 /* Uses of "template" must be followed by actual templates. */
7260 if (template_keyword_p)
7261 check_template_keyword_in_nested_name_spec (new_scope);
7262 /* If it is a class scope, try to complete it; we are about to
7263 be looking up names inside the class. */
7264 if (TYPE_P (new_scope)
7265 /* Since checking types for dependency can be expensive,
7266 avoid doing it if the type is already complete. */
7267 && !COMPLETE_TYPE_P (new_scope)
7268 /* Do not try to complete dependent types. */
7269 && !dependent_type_p (new_scope))
7271 new_scope = complete_type (new_scope);
7272 /* If it is a typedef to current class, use the current
7273 class instead, as the typedef won't have any names inside
7274 it yet. */
7275 if (!COMPLETE_TYPE_P (new_scope)
7276 && currently_open_class (new_scope))
7277 new_scope = TYPE_MAIN_VARIANT (new_scope);
7279 /* Make sure we look in the right scope the next time through
7280 the loop. */
7281 parser->scope = new_scope;
7284 /* If parsing tentatively, replace the sequence of tokens that makes
7285 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
7286 token. That way, should we re-parse the token stream, we will
7287 not have to repeat the effort required to do the parse, nor will
7288 we issue duplicate error messages. */
7289 if (success && start)
7291 cp_token *token;
7293 token = cp_lexer_token_at (parser->lexer, start);
7294 /* Reset the contents of the START token. */
7295 token->type = CPP_NESTED_NAME_SPECIFIER;
7296 /* Retrieve any deferred checks. Do not pop this access checks yet
7297 so the memory will not be reclaimed during token replacing below. */
7298 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
7299 token->tree_check_p = true;
7300 token->u.tree_check_value->value = parser->scope;
7301 token->u.tree_check_value->checks = get_deferred_access_checks ();
7302 token->u.tree_check_value->qualifying_scope =
7303 parser->qualifying_scope;
7304 token->keyword = RID_MAX;
7306 /* Purge all subsequent tokens. */
7307 cp_lexer_purge_tokens_after (parser->lexer, start);
7310 if (start)
7311 pop_to_parent_deferring_access_checks ();
7313 return success ? parser->scope : NULL_TREE;
7316 /* Parse a nested-name-specifier. See
7317 cp_parser_nested_name_specifier_opt for details. This function
7318 behaves identically, except that it will an issue an error if no
7319 nested-name-specifier is present. */
7321 static tree
7322 cp_parser_nested_name_specifier (cp_parser *parser,
7323 bool typename_keyword_p,
7324 bool check_dependency_p,
7325 bool type_p,
7326 bool is_declaration)
7328 tree scope;
7330 /* Look for the nested-name-specifier. */
7331 scope = cp_parser_nested_name_specifier_opt (parser,
7332 typename_keyword_p,
7333 check_dependency_p,
7334 type_p,
7335 is_declaration);
7336 /* If it was not present, issue an error message. */
7337 if (!scope)
7339 cp_parser_error (parser, "expected nested-name-specifier");
7340 parser->scope = NULL_TREE;
7343 return scope;
7346 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
7347 this is either a class-name or a namespace-name (which corresponds
7348 to the class-or-namespace-name production in the grammar). For
7349 C++0x, it can also be a type-name that refers to an enumeration
7350 type or a simple-template-id.
7352 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
7353 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
7354 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
7355 TYPE_P is TRUE iff the next name should be taken as a class-name,
7356 even the same name is declared to be another entity in the same
7357 scope.
7359 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
7360 specified by the class-or-namespace-name. If neither is found the
7361 ERROR_MARK_NODE is returned. */
7363 static tree
7364 cp_parser_qualifying_entity (cp_parser *parser,
7365 bool typename_keyword_p,
7366 bool template_keyword_p,
7367 bool check_dependency_p,
7368 bool type_p,
7369 bool is_declaration)
7371 tree saved_scope;
7372 tree saved_qualifying_scope;
7373 tree saved_object_scope;
7374 tree scope;
7375 bool only_class_p;
7376 bool successful_parse_p;
7378 /* DR 743: decltype can appear in a nested-name-specifier. */
7379 if (cp_lexer_next_token_is_decltype (parser->lexer))
7381 scope = cp_parser_decltype (parser);
7382 if (TREE_CODE (scope) != ENUMERAL_TYPE
7383 && !MAYBE_CLASS_TYPE_P (scope))
7385 cp_parser_simulate_error (parser);
7386 return error_mark_node;
7388 if (TYPE_NAME (scope))
7389 scope = TYPE_NAME (scope);
7390 return scope;
7393 /* Before we try to parse the class-name, we must save away the
7394 current PARSER->SCOPE since cp_parser_class_name will destroy
7395 it. */
7396 saved_scope = parser->scope;
7397 saved_qualifying_scope = parser->qualifying_scope;
7398 saved_object_scope = parser->object_scope;
7399 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
7400 there is no need to look for a namespace-name. */
7401 only_class_p = template_keyword_p
7402 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
7403 if (!only_class_p)
7404 cp_parser_parse_tentatively (parser);
7405 scope = cp_parser_class_name (parser,
7406 typename_keyword_p,
7407 template_keyword_p,
7408 type_p ? class_type : none_type,
7409 check_dependency_p,
7410 /*class_head_p=*/false,
7411 is_declaration,
7412 /*enum_ok=*/cxx_dialect > cxx98);
7413 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
7414 /* If that didn't work, try for a namespace-name. */
7415 if (!only_class_p && !successful_parse_p)
7417 /* Restore the saved scope. */
7418 parser->scope = saved_scope;
7419 parser->qualifying_scope = saved_qualifying_scope;
7420 parser->object_scope = saved_object_scope;
7421 /* If we are not looking at an identifier followed by the scope
7422 resolution operator, then this is not part of a
7423 nested-name-specifier. (Note that this function is only used
7424 to parse the components of a nested-name-specifier.) */
7425 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
7426 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
7427 return error_mark_node;
7428 scope = cp_parser_namespace_name (parser);
7431 return scope;
7434 /* Return true if we are looking at a compound-literal, false otherwise. */
7436 static bool
7437 cp_parser_compound_literal_p (cp_parser *parser)
7439 cp_lexer_save_tokens (parser->lexer);
7441 /* Skip tokens until the next token is a closing parenthesis.
7442 If we find the closing `)', and the next token is a `{', then
7443 we are looking at a compound-literal. */
7444 bool compound_literal_p
7445 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7446 /*consume_paren=*/true)
7447 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
7449 /* Roll back the tokens we skipped. */
7450 cp_lexer_rollback_tokens (parser->lexer);
7452 return compound_literal_p;
7455 /* Return true if EXPR is the integer constant zero or a complex constant
7456 of zero, without any folding, but ignoring location wrappers. */
7458 bool
7459 literal_integer_zerop (const_tree expr)
7461 return (location_wrapper_p (expr)
7462 && integer_zerop (TREE_OPERAND (expr, 0)));
7465 /* Parse a postfix-expression.
7467 postfix-expression:
7468 primary-expression
7469 postfix-expression [ expression ]
7470 postfix-expression ( expression-list [opt] )
7471 simple-type-specifier ( expression-list [opt] )
7472 typename :: [opt] nested-name-specifier identifier
7473 ( expression-list [opt] )
7474 typename :: [opt] nested-name-specifier template [opt] template-id
7475 ( expression-list [opt] )
7476 postfix-expression . template [opt] id-expression
7477 postfix-expression -> template [opt] id-expression
7478 postfix-expression . pseudo-destructor-name
7479 postfix-expression -> pseudo-destructor-name
7480 postfix-expression ++
7481 postfix-expression --
7482 dynamic_cast < type-id > ( expression )
7483 static_cast < type-id > ( expression )
7484 reinterpret_cast < type-id > ( expression )
7485 const_cast < type-id > ( expression )
7486 typeid ( expression )
7487 typeid ( type-id )
7489 GNU Extension:
7491 postfix-expression:
7492 ( type-id ) { initializer-list , [opt] }
7494 This extension is a GNU version of the C99 compound-literal
7495 construct. (The C99 grammar uses `type-name' instead of `type-id',
7496 but they are essentially the same concept.)
7498 If ADDRESS_P is true, the postfix expression is the operand of the
7499 `&' operator. CAST_P is true if this expression is the target of a
7500 cast.
7502 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
7503 class member access expressions [expr.ref].
7505 Returns a representation of the expression. */
7507 static cp_expr
7508 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
7509 bool member_access_only_p, bool decltype_p,
7510 cp_id_kind * pidk_return)
7512 cp_token *token;
7513 location_t loc;
7514 enum rid keyword;
7515 cp_id_kind idk = CP_ID_KIND_NONE;
7516 cp_expr postfix_expression = NULL_TREE;
7517 bool is_member_access = false;
7519 /* Peek at the next token. */
7520 token = cp_lexer_peek_token (parser->lexer);
7521 loc = token->location;
7522 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
7524 /* Some of the productions are determined by keywords. */
7525 keyword = token->keyword;
7526 switch (keyword)
7528 case RID_DYNCAST:
7529 case RID_STATCAST:
7530 case RID_REINTCAST:
7531 case RID_CONSTCAST:
7533 tree type;
7534 cp_expr expression;
7535 const char *saved_message;
7536 bool saved_in_type_id_in_expr_p;
7538 /* All of these can be handled in the same way from the point
7539 of view of parsing. Begin by consuming the token
7540 identifying the cast. */
7541 cp_lexer_consume_token (parser->lexer);
7543 /* New types cannot be defined in the cast. */
7544 saved_message = parser->type_definition_forbidden_message;
7545 parser->type_definition_forbidden_message
7546 = G_("types may not be defined in casts");
7548 /* Look for the opening `<'. */
7549 cp_parser_require (parser, CPP_LESS, RT_LESS);
7550 /* Parse the type to which we are casting. */
7551 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7552 parser->in_type_id_in_expr_p = true;
7553 type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
7554 NULL);
7555 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7556 /* Look for the closing `>'. */
7557 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
7558 /* Restore the old message. */
7559 parser->type_definition_forbidden_message = saved_message;
7561 bool saved_greater_than_is_operator_p
7562 = parser->greater_than_is_operator_p;
7563 parser->greater_than_is_operator_p = true;
7565 /* And the expression which is being cast. */
7566 matching_parens parens;
7567 parens.require_open (parser);
7568 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
7569 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
7570 RT_CLOSE_PAREN);
7571 location_t end_loc = close_paren ?
7572 close_paren->location : UNKNOWN_LOCATION;
7574 parser->greater_than_is_operator_p
7575 = saved_greater_than_is_operator_p;
7577 /* Only type conversions to integral or enumeration types
7578 can be used in constant-expressions. */
7579 if (!cast_valid_in_integral_constant_expression_p (type)
7580 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
7582 postfix_expression = error_mark_node;
7583 break;
7586 /* Construct a location e.g. :
7587 reinterpret_cast <int *> (expr)
7588 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7589 ranging from the start of the "*_cast" token to the final closing
7590 paren, with the caret at the start. */
7591 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
7593 switch (keyword)
7595 case RID_DYNCAST:
7596 postfix_expression
7597 = build_dynamic_cast (cp_cast_loc, type, expression,
7598 tf_warning_or_error);
7599 break;
7600 case RID_STATCAST:
7601 postfix_expression
7602 = build_static_cast (cp_cast_loc, type, expression,
7603 tf_warning_or_error);
7604 break;
7605 case RID_REINTCAST:
7606 postfix_expression
7607 = build_reinterpret_cast (cp_cast_loc, type, expression,
7608 tf_warning_or_error);
7609 break;
7610 case RID_CONSTCAST:
7611 postfix_expression
7612 = build_const_cast (cp_cast_loc, type, expression,
7613 tf_warning_or_error);
7614 break;
7615 default:
7616 gcc_unreachable ();
7619 break;
7621 case RID_TYPEID:
7623 tree type;
7624 const char *saved_message;
7625 bool saved_in_type_id_in_expr_p;
7627 /* Consume the `typeid' token. */
7628 cp_lexer_consume_token (parser->lexer);
7629 /* Look for the `(' token. */
7630 matching_parens parens;
7631 parens.require_open (parser);
7632 /* Types cannot be defined in a `typeid' expression. */
7633 saved_message = parser->type_definition_forbidden_message;
7634 parser->type_definition_forbidden_message
7635 = G_("types may not be defined in a %<typeid%> expression");
7636 /* We can't be sure yet whether we're looking at a type-id or an
7637 expression. */
7638 cp_parser_parse_tentatively (parser);
7639 /* Try a type-id first. */
7640 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7641 parser->in_type_id_in_expr_p = true;
7642 type = cp_parser_type_id (parser);
7643 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7644 /* Look for the `)' token. Otherwise, we can't be sure that
7645 we're not looking at an expression: consider `typeid (int
7646 (3))', for example. */
7647 cp_token *close_paren = parens.require_close (parser);
7648 /* If all went well, simply lookup the type-id. */
7649 if (cp_parser_parse_definitely (parser))
7650 postfix_expression = get_typeid (type, tf_warning_or_error);
7651 /* Otherwise, fall back to the expression variant. */
7652 else
7654 tree expression;
7656 /* Look for an expression. */
7657 expression = cp_parser_expression (parser, & idk);
7658 /* Compute its typeid. */
7659 postfix_expression = build_typeid (expression, tf_warning_or_error);
7660 /* Look for the `)' token. */
7661 close_paren = parens.require_close (parser);
7663 /* Restore the saved message. */
7664 parser->type_definition_forbidden_message = saved_message;
7665 /* `typeid' may not appear in an integral constant expression. */
7666 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
7667 postfix_expression = error_mark_node;
7669 /* Construct a location e.g. :
7670 typeid (expr)
7671 ^~~~~~~~~~~~~
7672 ranging from the start of the "typeid" token to the final closing
7673 paren, with the caret at the start. */
7674 if (close_paren)
7676 location_t typeid_loc
7677 = make_location (start_loc, start_loc, close_paren->location);
7678 postfix_expression.set_location (typeid_loc);
7679 postfix_expression.maybe_add_location_wrapper ();
7682 break;
7684 case RID_TYPENAME:
7686 tree type;
7687 /* The syntax permitted here is the same permitted for an
7688 elaborated-type-specifier. */
7689 ++parser->prevent_constrained_type_specifiers;
7690 type = cp_parser_elaborated_type_specifier (parser,
7691 /*is_friend=*/false,
7692 /*is_declaration=*/false);
7693 --parser->prevent_constrained_type_specifiers;
7694 postfix_expression = cp_parser_functional_cast (parser, type);
7696 break;
7698 case RID_ADDRESSOF:
7699 case RID_BUILTIN_SHUFFLE:
7700 case RID_BUILTIN_SHUFFLEVECTOR:
7701 case RID_BUILTIN_LAUNDER:
7702 case RID_BUILTIN_ASSOC_BARRIER:
7704 vec<tree, va_gc> *vec;
7706 cp_lexer_consume_token (parser->lexer);
7707 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
7708 /*cast_p=*/false, /*allow_expansion_p=*/true,
7709 /*non_constant_p=*/NULL);
7710 if (vec == NULL)
7712 postfix_expression = error_mark_node;
7713 break;
7716 for (tree p : *vec)
7717 mark_exp_read (p);
7719 switch (keyword)
7721 case RID_ADDRESSOF:
7722 if (vec->length () == 1)
7723 postfix_expression
7724 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
7725 else
7727 error_at (loc, "wrong number of arguments to "
7728 "%<__builtin_addressof%>");
7729 postfix_expression = error_mark_node;
7731 break;
7733 case RID_BUILTIN_LAUNDER:
7734 if (vec->length () == 1)
7735 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
7736 tf_warning_or_error);
7737 else
7739 error_at (loc, "wrong number of arguments to "
7740 "%<__builtin_launder%>");
7741 postfix_expression = error_mark_node;
7743 break;
7745 case RID_BUILTIN_ASSOC_BARRIER:
7746 if (vec->length () == 1)
7747 postfix_expression = build1_loc (loc, PAREN_EXPR,
7748 TREE_TYPE ((*vec)[0]),
7749 (*vec)[0]);
7750 else
7752 error_at (loc, "wrong number of arguments to "
7753 "%<__builtin_assoc_barrier%>");
7754 postfix_expression = error_mark_node;
7756 break;
7758 case RID_BUILTIN_SHUFFLE:
7759 if (vec->length () == 2)
7760 postfix_expression
7761 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
7762 (*vec)[1], tf_warning_or_error);
7763 else if (vec->length () == 3)
7764 postfix_expression
7765 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
7766 (*vec)[2], tf_warning_or_error);
7767 else
7769 error_at (loc, "wrong number of arguments to "
7770 "%<__builtin_shuffle%>");
7771 postfix_expression = error_mark_node;
7773 break;
7775 case RID_BUILTIN_SHUFFLEVECTOR:
7776 if (vec->length () < 3)
7778 error_at (loc, "wrong number of arguments to "
7779 "%<__builtin_shufflevector%>");
7780 postfix_expression = error_mark_node;
7782 else
7784 postfix_expression
7785 = build_x_shufflevector (loc, vec, tf_warning_or_error);
7787 break;
7789 default:
7790 gcc_unreachable ();
7792 break;
7795 case RID_BUILTIN_CONVERTVECTOR:
7797 tree expression;
7798 tree type;
7799 /* Consume the `__builtin_convertvector' token. */
7800 cp_lexer_consume_token (parser->lexer);
7801 /* Look for the opening `('. */
7802 matching_parens parens;
7803 parens.require_open (parser);
7804 /* Now, parse the assignment-expression. */
7805 expression = cp_parser_assignment_expression (parser);
7806 /* Look for the `,'. */
7807 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7808 location_t type_location
7809 = cp_lexer_peek_token (parser->lexer)->location;
7810 /* Parse the type-id. */
7812 type_id_in_expr_sentinel s (parser);
7813 type = cp_parser_type_id (parser);
7815 /* Look for the closing `)'. */
7816 parens.require_close (parser);
7817 postfix_expression
7818 = cp_build_vec_convert (expression, type_location, type,
7819 tf_warning_or_error);
7820 break;
7823 case RID_BUILTIN_BIT_CAST:
7825 tree expression;
7826 tree type;
7827 /* Consume the `__builtin_bit_cast' token. */
7828 cp_lexer_consume_token (parser->lexer);
7829 /* Look for the opening `('. */
7830 matching_parens parens;
7831 parens.require_open (parser);
7832 location_t type_location
7833 = cp_lexer_peek_token (parser->lexer)->location;
7834 /* Parse the type-id. */
7836 type_id_in_expr_sentinel s (parser);
7837 type = cp_parser_type_id (parser);
7839 /* Look for the `,'. */
7840 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7841 /* Now, parse the assignment-expression. */
7842 expression = cp_parser_assignment_expression (parser);
7843 /* Look for the closing `)'. */
7844 parens.require_close (parser);
7845 postfix_expression
7846 = cp_build_bit_cast (type_location, type, expression,
7847 tf_warning_or_error);
7848 break;
7851 default:
7853 tree type;
7855 /* If the next thing is a simple-type-specifier, we may be
7856 looking at a functional cast. We could also be looking at
7857 an id-expression. So, we try the functional cast, and if
7858 that doesn't work we fall back to the primary-expression. */
7859 cp_parser_parse_tentatively (parser);
7860 /* Look for the simple-type-specifier. */
7861 ++parser->prevent_constrained_type_specifiers;
7862 type = cp_parser_simple_type_specifier (parser,
7863 /*decl_specs=*/NULL,
7864 CP_PARSER_FLAGS_NONE);
7865 --parser->prevent_constrained_type_specifiers;
7866 /* Parse the cast itself. */
7867 if (!cp_parser_error_occurred (parser))
7868 postfix_expression
7869 = cp_parser_functional_cast (parser, type);
7870 /* If that worked, we're done. */
7871 if (cp_parser_parse_definitely (parser))
7872 break;
7874 /* If the functional-cast didn't work out, try a
7875 compound-literal. */
7876 if (cp_parser_allow_gnu_extensions_p (parser)
7877 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7879 cp_expr initializer = NULL_TREE;
7881 cp_parser_parse_tentatively (parser);
7883 matching_parens parens;
7884 parens.consume_open (parser);
7886 /* Avoid calling cp_parser_type_id pointlessly, see comment
7887 in cp_parser_cast_expression about c++/29234. */
7888 if (!cp_parser_compound_literal_p (parser))
7889 cp_parser_simulate_error (parser);
7890 else
7892 /* Parse the type. */
7893 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7894 parser->in_type_id_in_expr_p = true;
7895 type = cp_parser_type_id (parser);
7896 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7897 parens.require_close (parser);
7900 /* If things aren't going well, there's no need to
7901 keep going. */
7902 if (!cp_parser_error_occurred (parser))
7903 /* Parse the brace-enclosed initializer list. */
7904 initializer = cp_parser_braced_list (parser);
7905 /* If that worked, we're definitely looking at a
7906 compound-literal expression. */
7907 if (cp_parser_parse_definitely (parser))
7909 /* Warn the user that a compound literal is not
7910 allowed in standard C++. */
7911 pedwarn (input_location, OPT_Wpedantic,
7912 "ISO C++ forbids compound-literals");
7913 /* For simplicity, we disallow compound literals in
7914 constant-expressions. We could
7915 allow compound literals of integer type, whose
7916 initializer was a constant, in constant
7917 expressions. Permitting that usage, as a further
7918 extension, would not change the meaning of any
7919 currently accepted programs. (Of course, as
7920 compound literals are not part of ISO C++, the
7921 standard has nothing to say.) */
7922 if (cp_parser_non_integral_constant_expression (parser,
7923 NIC_NCC))
7925 postfix_expression = error_mark_node;
7926 break;
7928 /* Form the representation of the compound-literal. */
7929 postfix_expression
7930 = finish_compound_literal (type, initializer,
7931 tf_warning_or_error, fcl_c99);
7932 postfix_expression.set_location (initializer.get_location ());
7933 break;
7937 /* It must be a primary-expression. */
7938 postfix_expression
7939 = cp_parser_primary_expression (parser, address_p, cast_p,
7940 /*template_arg_p=*/false,
7941 decltype_p,
7942 &idk);
7944 break;
7947 /* Note that we don't need to worry about calling build_cplus_new on a
7948 class-valued CALL_EXPR in decltype when it isn't the end of the
7949 postfix-expression; unary_complex_lvalue will take care of that for
7950 all these cases. */
7952 /* Keep looping until the postfix-expression is complete. */
7953 while (true)
7955 if (idk == CP_ID_KIND_UNQUALIFIED
7956 && identifier_p (postfix_expression)
7957 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7958 /* It is not a Koenig lookup function call. */
7959 postfix_expression
7960 = unqualified_name_lookup_error (postfix_expression);
7962 /* Peek at the next token. */
7963 token = cp_lexer_peek_token (parser->lexer);
7965 switch (token->type)
7967 case CPP_OPEN_SQUARE:
7968 if (cp_next_tokens_can_be_std_attribute_p (parser))
7970 cp_parser_error (parser,
7971 "two consecutive %<[%> shall "
7972 "only introduce an attribute");
7973 return error_mark_node;
7975 postfix_expression
7976 = cp_parser_postfix_open_square_expression (parser,
7977 postfix_expression,
7978 false,
7979 decltype_p);
7980 postfix_expression.set_range (start_loc,
7981 postfix_expression.get_location ());
7983 idk = CP_ID_KIND_NONE;
7984 is_member_access = false;
7985 break;
7987 case CPP_OPEN_PAREN:
7988 /* postfix-expression ( expression-list [opt] ) */
7990 bool koenig_p;
7991 bool is_builtin_constant_p;
7992 bool saved_integral_constant_expression_p = false;
7993 bool saved_non_integral_constant_expression_p = false;
7994 tsubst_flags_t complain = complain_flags (decltype_p);
7995 vec<tree, va_gc> *args;
7996 location_t close_paren_loc = UNKNOWN_LOCATION;
7997 location_t combined_loc = UNKNOWN_LOCATION;
7999 is_member_access = false;
8001 tree stripped_expression
8002 = tree_strip_any_location_wrapper (postfix_expression);
8003 is_builtin_constant_p
8004 = DECL_IS_BUILTIN_CONSTANT_P (stripped_expression);
8005 if (is_builtin_constant_p)
8007 /* The whole point of __builtin_constant_p is to allow
8008 non-constant expressions to appear as arguments. */
8009 saved_integral_constant_expression_p
8010 = parser->integral_constant_expression_p;
8011 saved_non_integral_constant_expression_p
8012 = parser->non_integral_constant_expression_p;
8013 parser->integral_constant_expression_p = false;
8015 else if (TREE_CODE (stripped_expression) == FUNCTION_DECL
8016 && fndecl_built_in_p (stripped_expression,
8017 BUILT_IN_CLASSIFY_TYPE))
8019 /* __builtin_classify_type (type) */
8020 auto cl1 = make_temp_override
8021 (parser->type_definition_forbidden_message,
8022 G_("types may not be defined in "
8023 "%<__builtin_classify_type%> calls"));
8024 auto cl2 = make_temp_override
8025 (parser->type_definition_forbidden_message_arg,
8026 NULL);
8027 auto cl3 = make_temp_override (parser->in_type_id_in_expr_p,
8028 true);
8029 cp_unevaluated uev;
8030 cp_parser_parse_tentatively (parser);
8031 matching_parens parens;
8032 parens.consume_open (parser);
8033 tree type = cp_parser_type_id (parser);
8034 parens.require_close (parser);
8035 if (cp_parser_parse_definitely (parser))
8037 if (dependent_type_p (type))
8039 postfix_expression = build_vl_exp (CALL_EXPR, 4);
8040 CALL_EXPR_FN (postfix_expression)
8041 = stripped_expression;
8042 CALL_EXPR_STATIC_CHAIN (postfix_expression) = type;
8043 CALL_EXPR_ARG (postfix_expression, 0)
8044 = build_min (SIZEOF_EXPR, size_type_node, type);
8045 TREE_TYPE (postfix_expression) = integer_type_node;
8047 else
8049 postfix_expression
8050 = build_int_cst (integer_type_node,
8051 type_to_class (type));
8053 break;
8056 args = (cp_parser_parenthesized_expression_list
8057 (parser, non_attr,
8058 /*cast_p=*/false, /*allow_expansion_p=*/true,
8059 /*non_constant_p=*/NULL,
8060 /*close_paren_loc=*/&close_paren_loc,
8061 /*wrap_locations_p=*/true));
8062 if (is_builtin_constant_p)
8064 parser->integral_constant_expression_p
8065 = saved_integral_constant_expression_p;
8066 parser->non_integral_constant_expression_p
8067 = saved_non_integral_constant_expression_p;
8070 if (args == NULL)
8072 postfix_expression = error_mark_node;
8073 break;
8076 /* Function calls are not permitted in
8077 constant-expressions. */
8078 if (! builtin_valid_in_constant_expr_p (postfix_expression)
8079 && cp_parser_non_integral_constant_expression (parser,
8080 NIC_FUNC_CALL))
8082 postfix_expression = error_mark_node;
8083 release_tree_vector (args);
8084 break;
8087 koenig_p = false;
8088 if (idk == CP_ID_KIND_UNQUALIFIED
8089 || idk == CP_ID_KIND_TEMPLATE_ID)
8091 if (identifier_p (postfix_expression)
8092 /* In C++20, we may need to perform ADL for a template
8093 name. */
8094 || (TREE_CODE (postfix_expression) == TEMPLATE_ID_EXPR
8095 && identifier_p (TREE_OPERAND (postfix_expression, 0))))
8097 if (!args->is_empty ())
8099 koenig_p = true;
8100 if (!any_type_dependent_arguments_p (args))
8101 postfix_expression
8102 = perform_koenig_lookup (postfix_expression, args,
8103 complain);
8105 else
8106 postfix_expression
8107 = unqualified_fn_lookup_error (postfix_expression);
8109 /* We do not perform argument-dependent lookup if
8110 normal lookup finds a non-function, in accordance
8111 with the expected resolution of DR 218. */
8112 else if (!args->is_empty ()
8113 && is_overloaded_fn (postfix_expression))
8115 /* Do not do argument dependent lookup if regular
8116 lookup finds a member function or a block-scope
8117 function declaration. [basic.lookup.argdep]/3 */
8118 bool do_adl_p = true;
8119 tree fns = get_fns (postfix_expression);
8120 for (lkp_iterator iter (fns); iter; ++iter)
8122 tree fn = STRIP_TEMPLATE (*iter);
8123 if ((TREE_CODE (fn) == USING_DECL
8124 && DECL_DEPENDENT_P (fn))
8125 || DECL_FUNCTION_MEMBER_P (fn)
8126 || DECL_LOCAL_DECL_P (fn))
8128 do_adl_p = false;
8129 break;
8133 if (do_adl_p)
8135 koenig_p = true;
8136 if (!any_type_dependent_arguments_p (args))
8137 postfix_expression
8138 = perform_koenig_lookup (postfix_expression, args,
8139 complain);
8144 /* Temporarily set input_location to the combined location
8145 with call expression range, as e.g. build_out_target_exprs
8146 called from convert_default_arg relies on input_location,
8147 so updating it only when the call is fully built results
8148 in inconsistencies between location handling in templates
8149 and outside of templates. */
8150 if (close_paren_loc != UNKNOWN_LOCATION)
8151 combined_loc = make_location (token->location, start_loc,
8152 close_paren_loc);
8153 iloc_sentinel ils (combined_loc);
8155 if (TREE_CODE (postfix_expression) == OFFSET_REF
8156 || TREE_CODE (postfix_expression) == MEMBER_REF
8157 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
8158 postfix_expression = (build_offset_ref_call_from_tree
8159 (postfix_expression, &args,
8160 complain));
8161 else
8162 /* All other function calls. */
8164 if (DECL_P (postfix_expression)
8165 && parser->omp_for_parse_state
8166 && parser->omp_for_parse_state->in_intervening_code
8167 && omp_runtime_api_call (postfix_expression))
8169 error_at (loc, "calls to the OpenMP runtime API are "
8170 "not permitted in intervening code");
8171 parser->omp_for_parse_state->fail = true;
8173 bool disallow_virtual = (idk == CP_ID_KIND_QUALIFIED);
8174 postfix_expression
8175 = finish_call_expr (postfix_expression, &args,
8176 disallow_virtual,
8177 koenig_p,
8178 complain);
8181 if (close_paren_loc != UNKNOWN_LOCATION)
8182 postfix_expression.set_location (combined_loc);
8184 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
8185 idk = CP_ID_KIND_NONE;
8187 release_tree_vector (args);
8189 break;
8191 case CPP_DOT:
8192 case CPP_DEREF:
8193 /* postfix-expression . template [opt] id-expression
8194 postfix-expression . pseudo-destructor-name
8195 postfix-expression -> template [opt] id-expression
8196 postfix-expression -> pseudo-destructor-name */
8198 /* Consume the `.' or `->' operator. */
8199 cp_lexer_consume_token (parser->lexer);
8201 postfix_expression
8202 = cp_parser_postfix_dot_deref_expression (parser, token->type,
8203 postfix_expression,
8204 false, &idk, loc);
8206 is_member_access = true;
8207 break;
8209 case CPP_PLUS_PLUS:
8210 /* postfix-expression ++ */
8211 /* Consume the `++' token. */
8212 cp_lexer_consume_token (parser->lexer);
8213 /* Generate a representation for the complete expression. */
8214 postfix_expression
8215 = finish_increment_expr (postfix_expression,
8216 POSTINCREMENT_EXPR);
8217 /* Increments may not appear in constant-expressions. */
8218 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
8219 postfix_expression = error_mark_node;
8220 idk = CP_ID_KIND_NONE;
8221 is_member_access = false;
8222 break;
8224 case CPP_MINUS_MINUS:
8225 /* postfix-expression -- */
8226 /* Consume the `--' token. */
8227 cp_lexer_consume_token (parser->lexer);
8228 /* Generate a representation for the complete expression. */
8229 postfix_expression
8230 = finish_increment_expr (postfix_expression,
8231 POSTDECREMENT_EXPR);
8232 /* Decrements may not appear in constant-expressions. */
8233 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
8234 postfix_expression = error_mark_node;
8235 idk = CP_ID_KIND_NONE;
8236 is_member_access = false;
8237 break;
8239 default:
8240 if (pidk_return != NULL)
8241 * pidk_return = idk;
8242 if (member_access_only_p)
8243 return is_member_access
8244 ? postfix_expression
8245 : cp_expr (error_mark_node);
8246 else
8247 return postfix_expression;
8252 /* Helper function for cp_parser_parenthesized_expression_list and
8253 cp_parser_postfix_open_square_expression. Parse a single element
8254 of parenthesized expression list. */
8256 static cp_expr
8257 cp_parser_parenthesized_expression_list_elt (cp_parser *parser, bool cast_p,
8258 bool allow_expansion_p,
8259 bool *non_constant_p)
8261 cp_expr expr (NULL_TREE);
8262 bool expr_non_constant_p;
8264 /* Parse the next assignment-expression. */
8265 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8267 /* A braced-init-list. */
8268 cp_lexer_set_source_position (parser->lexer);
8269 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8270 expr = cp_parser_braced_list (parser,
8271 (non_constant_p != nullptr
8272 ? &expr_non_constant_p
8273 : nullptr));
8274 if (non_constant_p && expr_non_constant_p)
8275 *non_constant_p = true;
8277 else if (non_constant_p)
8279 expr = cp_parser_constant_expression (parser,
8280 /*allow_non_constant_p=*/true,
8281 &expr_non_constant_p);
8282 if (expr_non_constant_p)
8283 *non_constant_p = true;
8285 else
8286 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL, cast_p);
8288 /* If we have an ellipsis, then this is an expression expansion. */
8289 if (allow_expansion_p
8290 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8292 /* Consume the `...'. */
8293 cp_lexer_consume_token (parser->lexer);
8295 /* Build the argument pack. */
8296 expr = make_pack_expansion (expr);
8298 return expr;
8301 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
8302 by cp_parser_builtin_offsetof. We're looking for
8304 postfix-expression [ expression ]
8305 postfix-expression [ braced-init-list ] (C++11)
8306 postfix-expression [ expression-list[opt] ] (C++23)
8308 FOR_OFFSETOF is set if we're being called in that context, which
8309 changes how we deal with integer constant expressions. */
8311 static tree
8312 cp_parser_postfix_open_square_expression (cp_parser *parser,
8313 tree postfix_expression,
8314 bool for_offsetof,
8315 bool decltype_p)
8317 tree index = NULL_TREE;
8318 releasing_vec expression_list = NULL;
8319 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8320 bool saved_greater_than_is_operator_p;
8321 bool saved_colon_corrects_to_scope_p;
8323 /* Consume the `[' token. */
8324 cp_lexer_consume_token (parser->lexer);
8326 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
8327 parser->greater_than_is_operator_p = true;
8329 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8330 if (parser->omp_array_section_p)
8331 parser->colon_corrects_to_scope_p = false;
8333 /* Parse the index expression. */
8334 /* ??? For offsetof, there is a question of what to allow here. If
8335 offsetof is not being used in an integral constant expression context,
8336 then we *could* get the right answer by computing the value at runtime.
8337 If we are in an integral constant expression context, then we might
8338 could accept any constant expression; hard to say without analysis.
8339 Rather than open the barn door too wide right away, allow only integer
8340 constant expressions here. */
8341 if (for_offsetof)
8342 index = cp_parser_constant_expression (parser);
8343 else if (!parser->omp_array_section_p
8344 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8346 if (cxx_dialect >= cxx23
8347 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
8348 *&expression_list = make_tree_vector ();
8349 else if (cxx_dialect >= cxx23)
8351 while (true)
8353 cp_expr expr
8354 = cp_parser_parenthesized_expression_list_elt (parser,
8355 /*cast_p=*/
8356 false,
8357 /*allow_exp_p=*/
8358 true,
8359 /*non_cst_p=*/
8360 NULL);
8362 if (expr == error_mark_node)
8363 index = error_mark_node;
8364 else if (expression_list.get () == NULL
8365 && !PACK_EXPANSION_P (expr.get_value ()))
8366 index = expr.get_value ();
8367 else
8368 vec_safe_push (expression_list, expr.get_value ());
8370 /* If the next token isn't a `,', then we are done. */
8371 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8372 break;
8374 if (expression_list.get () == NULL && index != error_mark_node)
8376 *&expression_list = make_tree_vector_single (index);
8377 index = NULL_TREE;
8380 /* Otherwise, consume the `,' and keep going. */
8381 cp_lexer_consume_token (parser->lexer);
8383 if (expression_list.get () && index == error_mark_node)
8384 expression_list.release ();
8386 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8388 cp_lexer_set_source_position (parser->lexer);
8389 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8390 index = cp_parser_braced_list (parser);
8392 else
8393 index = cp_parser_expression (parser, NULL, /*cast_p=*/false,
8394 /*decltype_p=*/false,
8395 /*warn_comma_p=*/warn_comma_subscript);
8398 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
8400 if (cxx_dialect >= cxx23
8401 && parser->omp_array_section_p
8402 && expression_list.get () != NULL
8403 && vec_safe_length (expression_list) > 1)
8405 error_at (loc, "cannot use multidimensional subscript in OpenMP array "
8406 "section");
8407 index = error_mark_node;
8409 if (parser->omp_array_section_p
8410 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
8412 cp_lexer_consume_token (parser->lexer);
8413 tree length = NULL_TREE;
8414 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
8416 if (cxx_dialect >= cxx23)
8418 cp_expr expr
8419 = cp_parser_parenthesized_expression_list_elt (parser,
8420 /*cast_p=*/
8421 false,
8422 /*allow_exp_p=*/
8423 true,
8424 /*non_cst_p=*/
8425 NULL);
8427 if (expr == error_mark_node)
8428 length = error_mark_node;
8429 else
8430 length = expr.get_value ();
8432 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8434 error_at (loc, "cannot use multidimensional subscript in "
8435 "OpenMP array section");
8436 length = error_mark_node;
8439 else
8440 length
8441 = cp_parser_expression (parser, NULL, /*cast_p=*/false,
8442 /*decltype_p=*/false,
8443 /*warn_comma_p=*/warn_comma_subscript);
8446 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8448 if (index == error_mark_node || length == error_mark_node)
8450 cp_parser_skip_to_closing_square_bracket (parser);
8451 return error_mark_node;
8453 else
8454 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8456 return grok_omp_array_section (input_location, postfix_expression, index,
8457 length);
8460 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8462 /* Look for the closing `]'. */
8463 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8465 /* Build the ARRAY_REF. */
8466 postfix_expression = grok_array_decl (loc, postfix_expression,
8467 index, &expression_list,
8468 tf_warning_or_error
8469 | (decltype_p ? tf_decltype : 0));
8471 /* When not doing offsetof, array references are not permitted in
8472 constant-expressions. */
8473 if (!for_offsetof
8474 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
8475 postfix_expression = error_mark_node;
8477 return postfix_expression;
8480 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
8481 dereference of incomplete type, returns true if error_mark_node should
8482 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
8483 and *DEPENDENT_P. */
8485 bool
8486 cp_parser_dot_deref_incomplete (tree *scope, cp_expr *postfix_expression,
8487 bool *dependent_p)
8489 /* In a template, be permissive by treating an object expression
8490 of incomplete type as dependent (after a pedwarn). */
8491 diagnostic_t kind = (processing_template_decl
8492 && MAYBE_CLASS_TYPE_P (*scope) ? DK_PEDWARN : DK_ERROR);
8494 switch (TREE_CODE (*postfix_expression))
8496 case CAST_EXPR:
8497 case REINTERPRET_CAST_EXPR:
8498 case CONST_CAST_EXPR:
8499 case STATIC_CAST_EXPR:
8500 case DYNAMIC_CAST_EXPR:
8501 case IMPLICIT_CONV_EXPR:
8502 case VIEW_CONVERT_EXPR:
8503 case NON_LVALUE_EXPR:
8504 kind = DK_ERROR;
8505 break;
8506 case OVERLOAD:
8507 /* Don't emit any diagnostic for OVERLOADs. */
8508 kind = DK_IGNORED;
8509 break;
8510 default:
8511 /* Avoid clobbering e.g. DECLs. */
8512 if (!EXPR_P (*postfix_expression))
8513 kind = DK_ERROR;
8514 break;
8517 if (kind == DK_IGNORED)
8518 return false;
8520 location_t exploc = location_of (*postfix_expression);
8521 cxx_incomplete_type_diagnostic (exploc, *postfix_expression, *scope, kind);
8522 if (!MAYBE_CLASS_TYPE_P (*scope))
8523 return true;
8524 if (kind == DK_ERROR)
8525 *scope = *postfix_expression = error_mark_node;
8526 else if (processing_template_decl)
8528 *dependent_p = true;
8529 *scope = TREE_TYPE (*postfix_expression) = NULL_TREE;
8531 return false;
8534 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
8535 by cp_parser_builtin_offsetof. We're looking for
8537 postfix-expression . template [opt] id-expression
8538 postfix-expression . pseudo-destructor-name
8539 postfix-expression -> template [opt] id-expression
8540 postfix-expression -> pseudo-destructor-name
8542 FOR_OFFSETOF is set if we're being called in that context. That sorta
8543 limits what of the above we'll actually accept, but nevermind.
8544 TOKEN_TYPE is the "." or "->" token, which will already have been
8545 removed from the stream. */
8547 static tree
8548 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
8549 enum cpp_ttype token_type,
8550 cp_expr postfix_expression,
8551 bool for_offsetof, cp_id_kind *idk,
8552 location_t location)
8554 tree name;
8555 bool dependent_p;
8556 bool pseudo_destructor_p;
8557 tree scope = NULL_TREE;
8558 location_t start_loc = postfix_expression.get_start ();
8560 /* If this is a `->' operator, dereference the pointer. */
8561 if (token_type == CPP_DEREF)
8562 postfix_expression = build_x_arrow (location, postfix_expression,
8563 tf_warning_or_error);
8564 /* Check to see whether or not the expression is type-dependent and
8565 not the current instantiation. */
8566 dependent_p = type_dependent_object_expression_p (postfix_expression);
8567 /* The identifier following the `->' or `.' is not qualified. */
8568 parser->scope = NULL_TREE;
8569 parser->qualifying_scope = NULL_TREE;
8570 parser->object_scope = NULL_TREE;
8571 *idk = CP_ID_KIND_NONE;
8573 /* Enter the scope corresponding to the type of the object
8574 given by the POSTFIX_EXPRESSION. */
8575 if (!dependent_p)
8577 scope = TREE_TYPE (postfix_expression);
8578 /* According to the standard, no expression should ever have
8579 reference type. Unfortunately, we do not currently match
8580 the standard in this respect in that our internal representation
8581 of an expression may have reference type even when the standard
8582 says it does not. Therefore, we have to manually obtain the
8583 underlying type here. */
8584 scope = non_reference (scope);
8585 /* The type of the POSTFIX_EXPRESSION must be complete. */
8586 /* Unlike the object expression in other contexts, *this is not
8587 required to be of complete type for purposes of class member
8588 access (5.2.5) outside the member function body. */
8589 if (postfix_expression != current_class_ref
8590 && scope != error_mark_node
8591 && !currently_open_class (scope))
8593 scope = complete_type (scope);
8594 if (!COMPLETE_TYPE_P (scope)
8595 && cp_parser_dot_deref_incomplete (&scope, &postfix_expression,
8596 &dependent_p))
8597 return error_mark_node;
8600 if (!dependent_p)
8602 /* Let the name lookup machinery know that we are processing a
8603 class member access expression. */
8604 parser->context->object_type = scope;
8605 /* If something went wrong, we want to be able to discern that case,
8606 as opposed to the case where there was no SCOPE due to the type
8607 of expression being dependent. */
8608 if (!scope)
8609 scope = error_mark_node;
8610 /* If the SCOPE was erroneous, make the various semantic analysis
8611 functions exit quickly -- and without issuing additional error
8612 messages. */
8613 if (scope == error_mark_node)
8614 postfix_expression = error_mark_node;
8618 if (dependent_p)
8620 tree type = TREE_TYPE (postfix_expression);
8621 /* If we don't have a (type-dependent) object of class type, use
8622 typeof to figure out the type of the object. */
8623 if (type == NULL_TREE || is_auto (type))
8624 type = finish_typeof (postfix_expression);
8625 parser->context->object_type = type;
8628 /* Assume this expression is not a pseudo-destructor access. */
8629 pseudo_destructor_p = false;
8631 /* If the SCOPE is a scalar type, then, if this is a valid program,
8632 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
8633 is type dependent, it can be pseudo-destructor-name or something else.
8634 Try to parse it as pseudo-destructor-name first. */
8635 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
8637 tree s;
8638 tree type;
8640 cp_parser_parse_tentatively (parser);
8641 /* Parse the pseudo-destructor-name. */
8642 s = NULL_TREE;
8643 cp_parser_pseudo_destructor_name (parser, postfix_expression,
8644 &s, &type);
8645 if (dependent_p
8646 && (cp_parser_error_occurred (parser)
8647 || !SCALAR_TYPE_P (type)))
8648 cp_parser_abort_tentative_parse (parser);
8649 else if (cp_parser_parse_definitely (parser))
8651 pseudo_destructor_p = true;
8652 postfix_expression
8653 = finish_pseudo_destructor_expr (postfix_expression,
8654 s, type, location);
8658 if (!pseudo_destructor_p)
8660 /* If the SCOPE is not a scalar type, we are looking at an
8661 ordinary class member access expression, rather than a
8662 pseudo-destructor-name. */
8663 bool template_p;
8664 cp_token *token = cp_lexer_peek_token (parser->lexer);
8665 /* Parse the id-expression. */
8666 name = (cp_parser_id_expression
8667 (parser,
8668 cp_parser_optional_template_keyword (parser),
8669 /*check_dependency_p=*/true,
8670 &template_p,
8671 /*declarator_p=*/false,
8672 /*optional_p=*/false));
8673 /* In general, build a SCOPE_REF if the member name is qualified.
8674 However, if the name was not dependent and has already been
8675 resolved; there is no need to build the SCOPE_REF. For example;
8677 struct X { void f(); };
8678 template <typename T> void f(T* t) { t->X::f(); }
8680 Even though "t" is dependent, "X::f" is not and has been resolved
8681 to a BASELINK; there is no need to include scope information. */
8683 /* But we do need to remember that there was an explicit scope for
8684 virtual function calls. */
8685 if (parser->scope)
8686 *idk = CP_ID_KIND_QUALIFIED;
8688 /* If the name is a template-id that names a type, we will get a
8689 TYPE_DECL here. That is invalid code. */
8690 if (TREE_CODE (name) == TYPE_DECL)
8692 error_at (token->location, "invalid use of %qD", name);
8693 postfix_expression = error_mark_node;
8695 else
8697 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
8699 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
8701 error_at (token->location, "%<%D::%D%> is not a class member",
8702 parser->scope, name);
8703 postfix_expression = error_mark_node;
8705 else
8706 name = build_qualified_name (/*type=*/NULL_TREE,
8707 parser->scope,
8708 name,
8709 template_p);
8710 parser->scope = NULL_TREE;
8711 parser->qualifying_scope = NULL_TREE;
8712 parser->object_scope = NULL_TREE;
8714 if (parser->scope && name && BASELINK_P (name))
8715 adjust_result_of_qualified_name_lookup
8716 (name, parser->scope, scope);
8717 postfix_expression
8718 = finish_class_member_access_expr (postfix_expression, name,
8719 template_p,
8720 tf_warning_or_error);
8721 /* Build a location e.g.:
8722 ptr->access_expr
8723 ~~~^~~~~~~~~~~~~
8724 where the caret is at the deref token, ranging from
8725 the start of postfix_expression to the end of the access expr. */
8726 location_t combined_loc
8727 = make_location (input_location, start_loc, parser->lexer);
8728 protected_set_expr_location (postfix_expression, combined_loc);
8732 /* We no longer need to look up names in the scope of the object on
8733 the left-hand side of the `.' or `->' operator. */
8734 parser->context->object_type = NULL_TREE;
8736 /* Outside of offsetof, these operators may not appear in
8737 constant-expressions. */
8738 if (!for_offsetof
8739 && (cp_parser_non_integral_constant_expression
8740 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
8741 postfix_expression = error_mark_node;
8743 return postfix_expression;
8746 /* Parse a parenthesized expression-list.
8748 expression-list:
8749 assignment-expression
8750 expression-list, assignment-expression
8752 attribute-list:
8753 expression-list
8754 identifier
8755 identifier, expression-list
8757 CAST_P is true if this expression is the target of a cast.
8759 ALLOW_EXPANSION_P is true if this expression allows expansion of an
8760 argument pack.
8762 WRAP_LOCATIONS_P is true if expressions within this list for which
8763 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
8764 their source locations.
8766 Returns a vector of trees. Each element is a representation of an
8767 assignment-expression. NULL is returned if the ( and or ) are
8768 missing. An empty, but allocated, vector is returned on no
8769 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
8770 if we are parsing an attribute list for an attribute that wants a
8771 plain identifier argument, normal_attr for an attribute that wants
8772 an expression, or non_attr if we aren't parsing an attribute list. If
8773 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
8774 not all of the expressions in the list were constant.
8775 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
8776 will be written to with the location of the closing parenthesis. If
8777 an error occurs, it may or may not be written to. */
8779 static vec<tree, va_gc> *
8780 cp_parser_parenthesized_expression_list (cp_parser* parser,
8781 int is_attribute_list,
8782 bool cast_p,
8783 bool allow_expansion_p,
8784 bool *non_constant_p,
8785 location_t *close_paren_loc,
8786 bool wrap_locations_p)
8788 vec<tree, va_gc> *expression_list;
8789 bool saved_greater_than_is_operator_p;
8790 bool saved_omp_array_section_p;
8792 /* Assume all the expressions will be constant. */
8793 if (non_constant_p)
8794 *non_constant_p = false;
8796 matching_parens parens;
8797 if (!parens.require_open (parser))
8798 return NULL;
8800 expression_list = make_tree_vector ();
8802 /* Within a parenthesized expression, a `>' token is always
8803 the greater-than operator. */
8804 saved_greater_than_is_operator_p
8805 = parser->greater_than_is_operator_p;
8806 parser->greater_than_is_operator_p = true;
8808 saved_omp_array_section_p = parser->omp_array_section_p;
8809 parser->omp_array_section_p = false;
8811 cp_expr expr (NULL_TREE);
8813 /* Consume expressions until there are no more. */
8814 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8815 while (true)
8817 /* At the beginning of attribute lists, check to see if the
8818 next token is an identifier. */
8819 if (is_attribute_list == id_attr
8820 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
8821 expr = cp_lexer_consume_token (parser->lexer)->u.value;
8822 else if (is_attribute_list == assume_attr)
8823 expr = cp_parser_conditional_expression (parser);
8824 else if (is_attribute_list == uneval_string_attr)
8825 expr = cp_parser_unevaluated_string_literal (parser);
8826 else
8827 expr
8828 = cp_parser_parenthesized_expression_list_elt (parser, cast_p,
8829 allow_expansion_p,
8830 non_constant_p);
8832 if (wrap_locations_p)
8833 expr.maybe_add_location_wrapper ();
8835 /* Add it to the list. We add error_mark_node
8836 expressions to the list, so that we can still tell if
8837 the correct form for a parenthesized expression-list
8838 is found. That gives better errors. */
8839 vec_safe_push (expression_list, expr.get_value ());
8841 if (expr == error_mark_node)
8842 goto skip_comma;
8844 /* After the first item, attribute lists look the same as
8845 expression lists. */
8846 is_attribute_list = non_attr;
8848 get_comma:;
8849 /* If the next token isn't a `,', then we are done. */
8850 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8851 break;
8853 /* Otherwise, consume the `,' and keep going. */
8854 cp_lexer_consume_token (parser->lexer);
8857 if (close_paren_loc)
8858 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
8860 if (!parens.require_close (parser))
8862 int ending;
8864 skip_comma:;
8865 /* We try and resync to an unnested comma, as that will give the
8866 user better diagnostics. */
8867 ending = cp_parser_skip_to_closing_parenthesis (parser,
8868 /*recovering=*/true,
8869 /*or_comma=*/true,
8870 /*consume_paren=*/true);
8871 if (ending < 0)
8872 goto get_comma;
8873 if (!ending)
8875 parser->greater_than_is_operator_p
8876 = saved_greater_than_is_operator_p;
8877 parser->omp_array_section_p = saved_omp_array_section_p;
8878 return NULL;
8882 parser->greater_than_is_operator_p
8883 = saved_greater_than_is_operator_p;
8884 parser->omp_array_section_p = saved_omp_array_section_p;
8886 return expression_list;
8889 /* Parse a pseudo-destructor-name.
8891 pseudo-destructor-name:
8892 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
8893 :: [opt] nested-name-specifier template template-id :: ~ type-name
8894 :: [opt] nested-name-specifier [opt] ~ type-name
8896 If either of the first two productions is used, sets *SCOPE to the
8897 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
8898 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
8899 or ERROR_MARK_NODE if the parse fails. */
8901 static void
8902 cp_parser_pseudo_destructor_name (cp_parser* parser,
8903 tree object,
8904 tree* scope,
8905 tree* type)
8907 bool nested_name_specifier_p;
8909 /* Handle ~auto. */
8910 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
8911 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
8912 && !type_dependent_expression_p (object))
8914 if (cxx_dialect < cxx14)
8915 pedwarn (input_location, OPT_Wc__14_extensions,
8916 "%<~auto%> only available with "
8917 "%<-std=c++14%> or %<-std=gnu++14%>");
8918 cp_lexer_consume_token (parser->lexer);
8919 cp_lexer_consume_token (parser->lexer);
8920 *scope = NULL_TREE;
8921 *type = TREE_TYPE (object);
8922 return;
8925 /* Assume that things will not work out. */
8926 *type = error_mark_node;
8928 /* Look for the optional `::' operator. */
8929 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
8930 /* Look for the optional nested-name-specifier. */
8931 nested_name_specifier_p
8932 = (cp_parser_nested_name_specifier_opt (parser,
8933 /*typename_keyword_p=*/false,
8934 /*check_dependency_p=*/true,
8935 /*type_p=*/false,
8936 /*is_declaration=*/false)
8937 != NULL_TREE);
8938 /* Now, if we saw a nested-name-specifier, we might be doing the
8939 second production. */
8940 if (nested_name_specifier_p
8941 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8943 /* Consume the `template' keyword. */
8944 cp_lexer_consume_token (parser->lexer);
8945 /* Parse the template-id. */
8946 cp_parser_template_id (parser,
8947 /*template_keyword_p=*/true,
8948 /*check_dependency_p=*/false,
8949 class_type,
8950 /*is_declaration=*/true);
8951 /* Look for the `::' token. */
8952 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
8954 /* If the next token is not a `~', then there might be some
8955 additional qualification. */
8956 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
8958 /* At this point, we're looking for "type-name :: ~". The type-name
8959 must not be a class-name, since this is a pseudo-destructor. So,
8960 it must be either an enum-name, or a typedef-name -- both of which
8961 are just identifiers. So, we peek ahead to check that the "::"
8962 and "~" tokens are present; if they are not, then we can avoid
8963 calling type_name. */
8964 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
8965 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
8966 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
8968 cp_parser_error (parser, "non-scalar type");
8969 return;
8972 /* Look for the type-name. */
8973 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
8974 if (*scope == error_mark_node)
8975 return;
8977 /* Look for the `::' token. */
8978 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
8980 else
8981 *scope = NULL_TREE;
8983 /* Look for the `~'. */
8984 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
8986 /* Once we see the ~, this has to be a pseudo-destructor. */
8987 if (!processing_template_decl && !cp_parser_error_occurred (parser))
8988 cp_parser_commit_to_topmost_tentative_parse (parser);
8990 /* Look for the type-name again. We are not responsible for
8991 checking that it matches the first type-name. */
8992 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
8995 /* Parse a unary-expression.
8997 unary-expression:
8998 postfix-expression
8999 ++ cast-expression
9000 -- cast-expression
9001 await-expression
9002 unary-operator cast-expression
9003 sizeof unary-expression
9004 sizeof ( type-id )
9005 alignof ( type-id ) [C++0x]
9006 new-expression
9007 delete-expression
9009 GNU Extensions:
9011 unary-expression:
9012 __extension__ cast-expression
9013 __alignof__ unary-expression
9014 __alignof__ ( type-id )
9015 alignof unary-expression [C++0x]
9016 __real__ cast-expression
9017 __imag__ cast-expression
9018 && identifier
9019 sizeof ( type-id ) { initializer-list , [opt] }
9020 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
9021 __alignof__ ( type-id ) { initializer-list , [opt] }
9023 ADDRESS_P is true iff the unary-expression is appearing as the
9024 operand of the `&' operator. CAST_P is true if this expression is
9025 the target of a cast.
9027 Returns a representation of the expression. */
9029 static cp_expr
9030 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
9031 bool address_p, bool cast_p, bool decltype_p)
9033 cp_token *token;
9034 enum tree_code unary_operator;
9036 /* Peek at the next token. */
9037 token = cp_lexer_peek_token (parser->lexer);
9038 /* Some keywords give away the kind of expression. */
9039 if (token->type == CPP_KEYWORD)
9041 enum rid keyword = token->keyword;
9043 switch (keyword)
9045 case RID_ALIGNOF:
9046 case RID_SIZEOF:
9048 tree operand, ret;
9049 enum tree_code op;
9050 location_t start_loc = token->location;
9052 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
9053 bool std_alignof = id_equal (token->u.value, "alignof");
9055 /* Consume the token. */
9056 cp_lexer_consume_token (parser->lexer);
9057 /* Parse the operand. */
9058 operand = cp_parser_sizeof_operand (parser, keyword);
9060 /* Construct a location e.g. :
9061 alignof (expr)
9062 ^~~~~~~~~~~~~~
9063 with start == caret at the start of the "alignof"/"sizeof"
9064 token, with the endpoint at the final closing paren. */
9065 location_t compound_loc
9066 = make_location (start_loc, start_loc, parser->lexer);
9068 if (TYPE_P (operand))
9069 ret = cxx_sizeof_or_alignof_type (compound_loc, operand, op,
9070 std_alignof, true);
9071 else
9073 /* ISO C++ defines alignof only with types, not with
9074 expressions. So pedwarn if alignof is used with a non-
9075 type expression. However, __alignof__ is ok. */
9076 if (std_alignof)
9077 pedwarn (token->location, OPT_Wpedantic,
9078 "ISO C++ does not allow %<alignof%> "
9079 "with a non-type");
9081 ret = cxx_sizeof_or_alignof_expr (compound_loc, operand, op,
9082 std_alignof, true);
9084 /* For SIZEOF_EXPR, just issue diagnostics, but keep
9085 SIZEOF_EXPR with the original operand. */
9086 if (op == SIZEOF_EXPR && ret != error_mark_node)
9088 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
9090 if (!processing_template_decl && TYPE_P (operand))
9092 ret = build_min (SIZEOF_EXPR, size_type_node,
9093 build1 (NOP_EXPR, operand,
9094 error_mark_node));
9095 SIZEOF_EXPR_TYPE_P (ret) = 1;
9097 else
9098 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
9099 TREE_SIDE_EFFECTS (ret) = 0;
9100 TREE_READONLY (ret) = 1;
9101 SET_EXPR_LOCATION (ret, compound_loc);
9105 cp_expr ret_expr (ret, compound_loc);
9106 ret_expr = ret_expr.maybe_add_location_wrapper ();
9107 return ret_expr;
9110 case RID_BUILTIN_HAS_ATTRIBUTE:
9111 return cp_parser_has_attribute_expression (parser);
9113 case RID_NEW:
9114 return cp_parser_new_expression (parser);
9116 case RID_DELETE:
9117 return cp_parser_delete_expression (parser);
9119 case RID_EXTENSION:
9121 /* The saved value of the PEDANTIC flag. */
9122 int saved_pedantic;
9123 tree expr;
9125 /* Save away the PEDANTIC flag. */
9126 cp_parser_extension_opt (parser, &saved_pedantic);
9127 /* Parse the cast-expression. */
9128 expr = cp_parser_simple_cast_expression (parser);
9129 /* Restore the PEDANTIC flag. */
9130 pedantic = saved_pedantic;
9132 return expr;
9135 case RID_REALPART:
9136 case RID_IMAGPART:
9138 tree expression;
9140 /* Consume the `__real__' or `__imag__' token. */
9141 cp_lexer_consume_token (parser->lexer);
9142 /* Parse the cast-expression. */
9143 expression = cp_parser_simple_cast_expression (parser);
9144 /* Create the complete representation. */
9145 return build_x_unary_op (token->location,
9146 (keyword == RID_REALPART
9147 ? REALPART_EXPR : IMAGPART_EXPR),
9148 expression, NULL_TREE,
9149 tf_warning_or_error);
9151 break;
9153 case RID_TRANSACTION_ATOMIC:
9154 case RID_TRANSACTION_RELAXED:
9155 return cp_parser_transaction_expression (parser, keyword);
9157 case RID_NOEXCEPT:
9159 tree expr;
9160 const char *saved_message;
9161 bool saved_integral_constant_expression_p;
9162 bool saved_non_integral_constant_expression_p;
9163 bool saved_greater_than_is_operator_p;
9165 location_t start_loc = token->location;
9167 cp_lexer_consume_token (parser->lexer);
9168 matching_parens parens;
9169 parens.require_open (parser);
9171 saved_message = parser->type_definition_forbidden_message;
9172 parser->type_definition_forbidden_message
9173 = G_("types may not be defined in %<noexcept%> expressions");
9175 saved_integral_constant_expression_p
9176 = parser->integral_constant_expression_p;
9177 saved_non_integral_constant_expression_p
9178 = parser->non_integral_constant_expression_p;
9179 parser->integral_constant_expression_p = false;
9181 saved_greater_than_is_operator_p
9182 = parser->greater_than_is_operator_p;
9183 parser->greater_than_is_operator_p = true;
9185 ++cp_unevaluated_operand;
9186 ++c_inhibit_evaluation_warnings;
9187 ++cp_noexcept_operand;
9188 expr = cp_parser_expression (parser);
9189 --cp_noexcept_operand;
9190 --c_inhibit_evaluation_warnings;
9191 --cp_unevaluated_operand;
9193 parser->greater_than_is_operator_p
9194 = saved_greater_than_is_operator_p;
9196 parser->integral_constant_expression_p
9197 = saved_integral_constant_expression_p;
9198 parser->non_integral_constant_expression_p
9199 = saved_non_integral_constant_expression_p;
9201 parser->type_definition_forbidden_message = saved_message;
9203 parens.require_close (parser);
9205 /* Construct a location of the form:
9206 noexcept (expr)
9207 ^~~~~~~~~~~~~~~
9208 with start == caret, finishing at the close-paren. */
9209 location_t noexcept_loc
9210 = make_location (start_loc, start_loc, parser->lexer);
9212 return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
9213 noexcept_loc);
9216 case RID_CO_AWAIT:
9218 tree expr;
9219 location_t kw_loc = token->location;
9221 /* Consume the `co_await' token. */
9222 cp_lexer_consume_token (parser->lexer);
9223 /* Parse its cast-expression. */
9224 expr = cp_parser_simple_cast_expression (parser);
9225 if (expr == error_mark_node)
9226 return error_mark_node;
9228 /* Handle [expr.await]. */
9229 return cp_expr (finish_co_await_expr (kw_loc, expr));
9232 default:
9233 break;
9237 /* Look for the `:: new' and `:: delete', which also signal the
9238 beginning of a new-expression, or delete-expression,
9239 respectively. If the next token is `::', then it might be one of
9240 these. */
9241 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9243 enum rid keyword;
9245 /* See if the token after the `::' is one of the keywords in
9246 which we're interested. */
9247 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
9248 /* If it's `new', we have a new-expression. */
9249 if (keyword == RID_NEW)
9250 return cp_parser_new_expression (parser);
9251 /* Similarly, for `delete'. */
9252 else if (keyword == RID_DELETE)
9253 return cp_parser_delete_expression (parser);
9256 /* Look for a unary operator. */
9257 unary_operator = cp_parser_unary_operator (token);
9258 /* The `++' and `--' operators can be handled similarly, even though
9259 they are not technically unary-operators in the grammar. */
9260 if (unary_operator == ERROR_MARK)
9262 if (token->type == CPP_PLUS_PLUS)
9263 unary_operator = PREINCREMENT_EXPR;
9264 else if (token->type == CPP_MINUS_MINUS)
9265 unary_operator = PREDECREMENT_EXPR;
9266 /* Handle the GNU address-of-label extension. */
9267 else if (cp_parser_allow_gnu_extensions_p (parser)
9268 && token->type == CPP_AND_AND)
9270 tree identifier;
9271 tree expression;
9272 location_t start_loc = token->location;
9274 /* Consume the '&&' token. */
9275 cp_lexer_consume_token (parser->lexer);
9276 /* Look for the identifier. */
9277 identifier = cp_parser_identifier (parser);
9278 /* Construct a location of the form:
9279 &&label
9280 ^~~~~~~
9281 with caret==start at the "&&", finish at the end of the label. */
9282 location_t combined_loc
9283 = make_location (start_loc, start_loc, parser->lexer);
9284 /* Create an expression representing the address. */
9285 expression = finish_label_address_expr (identifier, combined_loc);
9286 if (TREE_CODE (expression) == ADDR_EXPR)
9287 mark_label_addressed (identifier);
9288 if (cp_parser_non_integral_constant_expression (parser,
9289 NIC_ADDR_LABEL))
9290 expression = error_mark_node;
9291 return expression;
9294 if (unary_operator != ERROR_MARK)
9296 cp_expr cast_expression;
9297 cp_expr expression = error_mark_node;
9298 non_integral_constant non_constant_p = NIC_NONE;
9299 location_t loc = token->location;
9300 tsubst_flags_t complain = complain_flags (decltype_p);
9302 /* Consume the operator token. */
9303 token = cp_lexer_consume_token (parser->lexer);
9304 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
9306 /* Parse the cast-expression. */
9307 cast_expression
9308 = cp_parser_cast_expression (parser,
9309 unary_operator == ADDR_EXPR,
9310 /*cast_p=*/false,
9311 /*decltype*/false,
9312 pidk);
9314 /* Make a location:
9315 OP_TOKEN CAST_EXPRESSION
9316 ^~~~~~~~~~~~~~~~~~~~~~~~~
9317 with start==caret at the operator token, and
9318 extending to the end of the cast_expression. */
9319 loc = make_location (loc, loc, cast_expression.get_finish ());
9321 /* Now, build an appropriate representation. */
9322 switch (unary_operator)
9324 case INDIRECT_REF:
9325 non_constant_p = NIC_STAR;
9326 expression = build_x_indirect_ref (loc, cast_expression,
9327 RO_UNARY_STAR, NULL_TREE,
9328 complain);
9329 /* TODO: build_x_indirect_ref does not always honor the
9330 location, so ensure it is set. */
9331 expression.set_location (loc);
9332 break;
9334 case ADDR_EXPR:
9335 non_constant_p = NIC_ADDR;
9336 /* Fall through. */
9337 case BIT_NOT_EXPR:
9338 expression = build_x_unary_op (loc, unary_operator,
9339 cast_expression,
9340 NULL_TREE, complain);
9341 /* TODO: build_x_unary_op does not always honor the location,
9342 so ensure it is set. */
9343 expression.set_location (loc);
9344 break;
9346 case PREINCREMENT_EXPR:
9347 case PREDECREMENT_EXPR:
9348 non_constant_p = unary_operator == PREINCREMENT_EXPR
9349 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
9350 /* Fall through. */
9351 case NEGATE_EXPR:
9352 /* Immediately fold negation of a constant, unless the constant is 0
9353 (since -0 == 0) or it would overflow. */
9354 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER)
9356 tree stripped_expr
9357 = tree_strip_any_location_wrapper (cast_expression);
9358 if (CONSTANT_CLASS_P (stripped_expr)
9359 && !integer_zerop (stripped_expr)
9360 && !TREE_OVERFLOW (stripped_expr))
9362 tree folded = fold_build1 (unary_operator,
9363 TREE_TYPE (stripped_expr),
9364 stripped_expr);
9365 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
9367 expression = maybe_wrap_with_location (folded, loc);
9368 break;
9372 /* Fall through. */
9373 case UNARY_PLUS_EXPR:
9374 case TRUTH_NOT_EXPR:
9375 expression = finish_unary_op_expr (loc, unary_operator,
9376 cast_expression, complain);
9377 break;
9379 default:
9380 gcc_unreachable ();
9383 if (non_constant_p != NIC_NONE
9384 && cp_parser_non_integral_constant_expression (parser,
9385 non_constant_p))
9386 expression = error_mark_node;
9388 return expression;
9391 return cp_parser_postfix_expression (parser, address_p, cast_p,
9392 /*member_access_only_p=*/false,
9393 decltype_p,
9394 pidk);
9397 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
9398 unary-operator, the corresponding tree code is returned. */
9400 static enum tree_code
9401 cp_parser_unary_operator (cp_token* token)
9403 switch (token->type)
9405 case CPP_MULT:
9406 return INDIRECT_REF;
9408 case CPP_AND:
9409 return ADDR_EXPR;
9411 case CPP_PLUS:
9412 return UNARY_PLUS_EXPR;
9414 case CPP_MINUS:
9415 return NEGATE_EXPR;
9417 case CPP_NOT:
9418 return TRUTH_NOT_EXPR;
9420 case CPP_COMPL:
9421 return BIT_NOT_EXPR;
9423 default:
9424 return ERROR_MARK;
9428 /* Parse a __builtin_has_attribute([expr|type], attribute-spec) expression.
9429 Returns a representation of the expression. */
9431 static tree
9432 cp_parser_has_attribute_expression (cp_parser *parser)
9434 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9436 /* Consume the __builtin_has_attribute token. */
9437 cp_lexer_consume_token (parser->lexer);
9439 matching_parens parens;
9440 if (!parens.require_open (parser))
9441 return error_mark_node;
9443 /* Types cannot be defined in a `sizeof' expression. Save away the
9444 old message. */
9445 const char *saved_message = parser->type_definition_forbidden_message;
9446 const char *saved_message_arg
9447 = parser->type_definition_forbidden_message_arg;
9448 parser->type_definition_forbidden_message
9449 = G_("types may not be defined in %qs expressions");
9450 parser->type_definition_forbidden_message_arg
9451 = IDENTIFIER_POINTER (ridpointers[RID_BUILTIN_HAS_ATTRIBUTE]);
9453 /* The restrictions on constant-expressions do not apply inside
9454 sizeof expressions. */
9455 bool saved_integral_constant_expression_p
9456 = parser->integral_constant_expression_p;
9457 bool saved_non_integral_constant_expression_p
9458 = parser->non_integral_constant_expression_p;
9459 parser->integral_constant_expression_p = false;
9461 /* Do not actually evaluate the expression. */
9462 ++cp_unevaluated_operand;
9463 ++c_inhibit_evaluation_warnings;
9465 tree oper = NULL_TREE;
9467 /* We can't be sure yet whether we're looking at a type-id or an
9468 expression. */
9469 cp_parser_parse_tentatively (parser);
9471 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9472 parser->in_type_id_in_expr_p = true;
9473 /* Look for the type-id. */
9474 oper = cp_parser_type_id (parser);
9475 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9477 cp_parser_parse_definitely (parser);
9479 /* If the type-id production did not work out, then we must be
9480 looking at an expression. */
9481 if (!oper || oper == error_mark_node)
9482 oper = cp_parser_assignment_expression (parser);
9484 STRIP_ANY_LOCATION_WRAPPER (oper);
9486 /* Go back to evaluating expressions. */
9487 --cp_unevaluated_operand;
9488 --c_inhibit_evaluation_warnings;
9490 /* And restore the old one. */
9491 parser->type_definition_forbidden_message = saved_message;
9492 parser->type_definition_forbidden_message_arg = saved_message_arg;
9493 parser->integral_constant_expression_p
9494 = saved_integral_constant_expression_p;
9495 parser->non_integral_constant_expression_p
9496 = saved_non_integral_constant_expression_p;
9498 /* Consume the comma if it's there. */
9499 if (!cp_parser_require (parser, CPP_COMMA, RT_COMMA))
9501 cp_parser_skip_to_closing_parenthesis (parser, false, false,
9502 /*consume_paren=*/true);
9503 return error_mark_node;
9506 /* Parse the attribute specification. */
9507 bool ret = false;
9508 location_t atloc = cp_lexer_peek_token (parser->lexer)->location;
9509 if (tree attr = cp_parser_gnu_attribute_list (parser, /*exactly_one=*/true))
9511 if (oper == error_mark_node)
9512 /* Nothing. */;
9513 else if (processing_template_decl && uses_template_parms (oper))
9514 sorry_at (atloc, "%<__builtin_has_attribute%> with dependent argument "
9515 "not supported yet");
9516 else
9518 /* Fold constant expressions used in attributes first. */
9519 cp_check_const_attributes (attr);
9521 /* Finally, see if OPER has been declared with ATTR. */
9522 ret = has_attribute (atloc, oper, attr, default_conversion);
9525 parens.require_close (parser);
9527 else
9529 error_at (atloc, "expected identifier");
9530 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9533 /* Construct a location e.g. :
9534 __builtin_has_attribute (oper, attr)
9535 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9536 with start == caret at the start of the built-in token,
9537 and with the endpoint at the final closing paren. */
9538 location_t compound_loc
9539 = make_location (start_loc, start_loc, parser->lexer);
9541 cp_expr ret_expr (ret ? boolean_true_node : boolean_false_node);
9542 ret_expr.set_location (compound_loc);
9543 ret_expr = ret_expr.maybe_add_location_wrapper ();
9544 return ret_expr;
9547 /* Parse a new-expression.
9549 new-expression:
9550 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
9551 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
9553 Returns a representation of the expression. */
9555 static tree
9556 cp_parser_new_expression (cp_parser* parser)
9558 bool global_scope_p;
9559 vec<tree, va_gc> *placement;
9560 tree type;
9561 vec<tree, va_gc> *initializer;
9562 tree nelts = NULL_TREE;
9563 tree ret;
9565 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9567 /* Look for the optional `::' operator. */
9568 global_scope_p
9569 = (cp_parser_global_scope_opt (parser,
9570 /*current_scope_valid_p=*/false)
9571 != NULL_TREE);
9572 /* Look for the `new' operator. */
9573 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
9574 /* There's no easy way to tell a new-placement from the
9575 `( type-id )' construct. */
9576 cp_parser_parse_tentatively (parser);
9577 /* Look for a new-placement. */
9578 placement = cp_parser_new_placement (parser);
9579 /* If that didn't work out, there's no new-placement. */
9580 if (!cp_parser_parse_definitely (parser))
9582 if (placement != NULL)
9583 release_tree_vector (placement);
9584 placement = NULL;
9587 /* If the next token is a `(', then we have a parenthesized
9588 type-id. */
9589 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9591 cp_token *token;
9592 const char *saved_message = parser->type_definition_forbidden_message;
9594 /* Consume the `('. */
9595 matching_parens parens;
9596 parens.consume_open (parser);
9598 /* Parse the type-id. */
9599 parser->type_definition_forbidden_message
9600 = G_("types may not be defined in a new-expression");
9602 type_id_in_expr_sentinel s (parser);
9603 type = cp_parser_type_id (parser);
9605 parser->type_definition_forbidden_message = saved_message;
9607 /* Look for the closing `)'. */
9608 parens.require_close (parser);
9609 token = cp_lexer_peek_token (parser->lexer);
9610 /* There should not be a direct-new-declarator in this production,
9611 but GCC used to allowed this, so we check and emit a sensible error
9612 message for this case. */
9613 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
9615 error_at (token->location,
9616 "array bound forbidden after parenthesized type-id");
9617 inform (token->location,
9618 "try removing the parentheses around the type-id");
9619 cp_parser_direct_new_declarator (parser);
9622 /* Otherwise, there must be a new-type-id. */
9623 else
9624 type = cp_parser_new_type_id (parser, &nelts);
9626 /* If the next token is a `(' or '{', then we have a new-initializer. */
9627 cp_token *token = cp_lexer_peek_token (parser->lexer);
9628 if (token->type == CPP_OPEN_PAREN
9629 || token->type == CPP_OPEN_BRACE)
9630 initializer = cp_parser_new_initializer (parser);
9631 else
9632 initializer = NULL;
9634 /* A new-expression may not appear in an integral constant
9635 expression. */
9636 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
9637 ret = error_mark_node;
9638 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
9639 of a new-type-id or type-id of a new-expression, the new-expression shall
9640 contain a new-initializer of the form ( assignment-expression )".
9641 Additionally, consistently with the spirit of DR 1467, we want to accept
9642 'new auto { 2 }' too. */
9643 else if ((ret = type_uses_auto (type))
9644 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
9645 && (vec_safe_length (initializer) != 1
9646 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
9647 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
9649 error_at (token->location,
9650 "initialization of new-expression for type %<auto%> "
9651 "requires exactly one element");
9652 ret = error_mark_node;
9654 else
9656 /* Construct a location e.g.:
9657 ptr = new int[100]
9658 ^~~~~~~~~~~~
9659 with caret == start at the start of the "new" token, and the end
9660 at the end of the final token we consumed. */
9661 location_t combined_loc = make_location (start_loc, start_loc,
9662 parser->lexer);
9663 /* Create a representation of the new-expression. */
9664 ret = build_new (combined_loc, &placement, type, nelts, &initializer,
9665 global_scope_p, tf_warning_or_error);
9668 if (placement != NULL)
9669 release_tree_vector (placement);
9670 if (initializer != NULL)
9671 release_tree_vector (initializer);
9673 return ret;
9676 /* Parse a new-placement.
9678 new-placement:
9679 ( expression-list )
9681 Returns the same representation as for an expression-list. */
9683 static vec<tree, va_gc> *
9684 cp_parser_new_placement (cp_parser* parser)
9686 vec<tree, va_gc> *expression_list;
9688 /* Parse the expression-list. */
9689 expression_list = (cp_parser_parenthesized_expression_list
9690 (parser, non_attr, /*cast_p=*/false,
9691 /*allow_expansion_p=*/true,
9692 /*non_constant_p=*/NULL));
9694 if (expression_list && expression_list->is_empty ())
9695 error ("expected expression-list or type-id");
9697 return expression_list;
9700 /* Parse a new-type-id.
9702 new-type-id:
9703 type-specifier-seq new-declarator [opt]
9705 Returns the TYPE allocated. If the new-type-id indicates an array
9706 type, *NELTS is set to the number of elements in the last array
9707 bound; the TYPE will not include the last array bound. */
9709 static tree
9710 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
9712 cp_decl_specifier_seq type_specifier_seq;
9713 cp_declarator *new_declarator;
9714 cp_declarator *declarator;
9715 cp_declarator *outer_declarator;
9716 const char *saved_message;
9718 /* The type-specifier sequence must not contain type definitions.
9719 (It cannot contain declarations of new types either, but if they
9720 are not definitions we will catch that because they are not
9721 complete.) */
9722 saved_message = parser->type_definition_forbidden_message;
9723 parser->type_definition_forbidden_message
9724 = G_("types may not be defined in a new-type-id");
9725 /* Parse the type-specifier-seq. */
9726 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
9727 /*is_declaration=*/false,
9728 /*is_trailing_return=*/false,
9729 &type_specifier_seq);
9730 /* Restore the old message. */
9731 parser->type_definition_forbidden_message = saved_message;
9733 if (type_specifier_seq.type == error_mark_node)
9734 return error_mark_node;
9736 /* Parse the new-declarator. */
9737 new_declarator = cp_parser_new_declarator_opt (parser);
9739 /* Determine the number of elements in the last array dimension, if
9740 any. */
9741 *nelts = NULL_TREE;
9742 /* Skip down to the last array dimension. */
9743 declarator = new_declarator;
9744 outer_declarator = NULL;
9745 while (declarator && (declarator->kind == cdk_pointer
9746 || declarator->kind == cdk_ptrmem))
9748 outer_declarator = declarator;
9749 declarator = declarator->declarator;
9751 while (declarator
9752 && declarator->kind == cdk_array
9753 && declarator->declarator
9754 && declarator->declarator->kind == cdk_array)
9756 outer_declarator = declarator;
9757 declarator = declarator->declarator;
9760 if (declarator && declarator->kind == cdk_array)
9762 *nelts = declarator->u.array.bounds;
9763 if (*nelts == error_mark_node)
9764 *nelts = integer_one_node;
9766 if (*nelts == NULL_TREE)
9767 /* Leave [] in the declarator. */;
9768 else if (outer_declarator)
9769 outer_declarator->declarator = declarator->declarator;
9770 else
9771 new_declarator = NULL;
9774 return groktypename (&type_specifier_seq, new_declarator, false);
9777 /* Parse an (optional) new-declarator.
9779 new-declarator:
9780 ptr-operator new-declarator [opt]
9781 direct-new-declarator
9783 Returns the declarator. */
9785 static cp_declarator *
9786 cp_parser_new_declarator_opt (cp_parser* parser)
9788 enum tree_code code;
9789 tree type, std_attributes = NULL_TREE;
9790 cp_cv_quals cv_quals;
9792 /* We don't know if there's a ptr-operator next, or not. */
9793 cp_parser_parse_tentatively (parser);
9794 /* Look for a ptr-operator. */
9795 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
9796 /* If that worked, look for more new-declarators. */
9797 if (cp_parser_parse_definitely (parser))
9799 cp_declarator *declarator;
9801 /* Parse another optional declarator. */
9802 declarator = cp_parser_new_declarator_opt (parser);
9804 declarator = cp_parser_make_indirect_declarator
9805 (code, type, cv_quals, declarator, std_attributes);
9807 return declarator;
9810 /* If the next token is a `[', there is a direct-new-declarator. */
9811 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
9812 return cp_parser_direct_new_declarator (parser);
9814 return NULL;
9817 /* Parse a direct-new-declarator.
9819 direct-new-declarator:
9820 [ expression ]
9821 direct-new-declarator [constant-expression]
9825 static cp_declarator *
9826 cp_parser_direct_new_declarator (cp_parser* parser)
9828 cp_declarator *declarator = NULL;
9829 bool first_p = true;
9831 while (true)
9833 tree expression;
9834 cp_token *token;
9836 /* Look for the opening `['. */
9837 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9839 token = cp_lexer_peek_token (parser->lexer);
9840 if (token->type == CPP_CLOSE_SQUARE && first_p)
9841 expression = NULL_TREE;
9842 else
9843 expression = cp_parser_expression (parser);
9844 /* The standard requires that the expression have integral
9845 type. DR 74 adds enumeration types. We believe that the
9846 real intent is that these expressions be handled like the
9847 expression in a `switch' condition, which also allows
9848 classes with a single conversion to integral or
9849 enumeration type. */
9850 if (expression && !processing_template_decl)
9852 expression
9853 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
9854 expression,
9855 /*complain=*/true);
9856 if (!expression)
9858 error_at (token->location,
9859 "expression in new-declarator must have integral "
9860 "or enumeration type");
9861 expression = error_mark_node;
9865 /* Look for the closing `]'. */
9866 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9868 /* Add this bound to the declarator. */
9869 declarator = make_array_declarator (declarator, expression);
9871 /* If the next token is not a `[', then there are no more
9872 bounds. */
9873 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
9874 break;
9875 first_p = false;
9878 return declarator;
9881 /* Parse a new-initializer.
9883 new-initializer:
9884 ( expression-list [opt] )
9885 braced-init-list
9887 Returns a representation of the expression-list. */
9889 static vec<tree, va_gc> *
9890 cp_parser_new_initializer (cp_parser* parser)
9892 vec<tree, va_gc> *expression_list;
9894 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9896 cp_lexer_set_source_position (parser->lexer);
9897 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9898 tree t = cp_parser_braced_list (parser);
9899 CONSTRUCTOR_IS_DIRECT_INIT (t) = true;
9900 expression_list = make_tree_vector_single (t);
9902 else
9903 expression_list = (cp_parser_parenthesized_expression_list
9904 (parser, non_attr, /*cast_p=*/false,
9905 /*allow_expansion_p=*/true,
9906 /*non_constant_p=*/NULL));
9908 return expression_list;
9911 /* Parse a delete-expression.
9913 delete-expression:
9914 :: [opt] delete cast-expression
9915 :: [opt] delete [ ] cast-expression
9917 Returns a representation of the expression. */
9919 static tree
9920 cp_parser_delete_expression (cp_parser* parser)
9922 bool global_scope_p;
9923 bool array_p;
9924 tree expression;
9925 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9927 /* Look for the optional `::' operator. */
9928 global_scope_p
9929 = (cp_parser_global_scope_opt (parser,
9930 /*current_scope_valid_p=*/false)
9931 != NULL_TREE);
9932 /* Look for the `delete' keyword. */
9933 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
9934 /* See if the array syntax is in use. */
9935 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
9937 /* Consume the `[' token. */
9938 cp_lexer_consume_token (parser->lexer);
9939 /* Look for the `]' token. */
9940 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9941 /* Remember that this is the `[]' construct. */
9942 array_p = true;
9944 else
9945 array_p = false;
9947 /* Parse the cast-expression. */
9948 expression = cp_parser_simple_cast_expression (parser);
9950 /* A delete-expression may not appear in an integral constant
9951 expression. */
9952 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
9953 return error_mark_node;
9955 /* Construct a location e.g.:
9956 delete [ ] ptr
9957 ^~~~~~~~~~~~~~
9958 with caret == start at the start of the "delete" token, and
9959 the end at the end of the final token we consumed. */
9960 location_t combined_loc = make_location (start_loc, start_loc,
9961 parser->lexer);
9962 expression = delete_sanity (combined_loc, expression, NULL_TREE, array_p,
9963 global_scope_p, tf_warning_or_error);
9965 return expression;
9968 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
9969 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
9970 0 otherwise. */
9972 static int
9973 cp_parser_tokens_start_cast_expression (cp_parser *parser)
9975 cp_token *token = cp_lexer_peek_token (parser->lexer);
9976 switch (token->type)
9978 case CPP_COMMA:
9979 case CPP_SEMICOLON:
9980 case CPP_QUERY:
9981 case CPP_COLON:
9982 case CPP_CLOSE_SQUARE:
9983 case CPP_CLOSE_PAREN:
9984 case CPP_CLOSE_BRACE:
9985 case CPP_OPEN_BRACE:
9986 case CPP_DOT:
9987 case CPP_DOT_STAR:
9988 case CPP_DEREF:
9989 case CPP_DEREF_STAR:
9990 case CPP_DIV:
9991 case CPP_MOD:
9992 case CPP_LSHIFT:
9993 case CPP_RSHIFT:
9994 case CPP_LESS:
9995 case CPP_GREATER:
9996 case CPP_LESS_EQ:
9997 case CPP_GREATER_EQ:
9998 case CPP_EQ_EQ:
9999 case CPP_NOT_EQ:
10000 case CPP_EQ:
10001 case CPP_MULT_EQ:
10002 case CPP_DIV_EQ:
10003 case CPP_MOD_EQ:
10004 case CPP_PLUS_EQ:
10005 case CPP_MINUS_EQ:
10006 case CPP_RSHIFT_EQ:
10007 case CPP_LSHIFT_EQ:
10008 case CPP_AND_EQ:
10009 case CPP_XOR_EQ:
10010 case CPP_OR_EQ:
10011 case CPP_XOR:
10012 case CPP_OR:
10013 case CPP_OR_OR:
10014 case CPP_EOF:
10015 case CPP_ELLIPSIS:
10016 return 0;
10018 case CPP_OPEN_PAREN:
10019 /* In ((type ()) () the last () isn't a valid cast-expression,
10020 so the whole must be parsed as postfix-expression. */
10021 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
10022 != CPP_CLOSE_PAREN;
10024 case CPP_OPEN_SQUARE:
10025 /* '[' may start a primary-expression in obj-c++ and in C++11,
10026 as a lambda-expression, eg, '(void)[]{}'. */
10027 if (cxx_dialect >= cxx11)
10028 return -1;
10029 return c_dialect_objc ();
10031 case CPP_PLUS_PLUS:
10032 case CPP_MINUS_MINUS:
10033 /* '++' and '--' may or may not start a cast-expression:
10035 struct T { void operator++(int); };
10036 void f() { (T())++; }
10040 int a;
10041 (int)++a; */
10042 return -1;
10044 default:
10045 return 1;
10049 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
10050 in the order: const_cast, static_cast, reinterpret_cast.
10052 Don't suggest dynamic_cast.
10054 Return the first legal cast kind found, or NULL otherwise. */
10056 static const char *
10057 get_cast_suggestion (tree dst_type, tree orig_expr)
10059 tree trial;
10061 /* Reuse the parser logic by attempting to build the various kinds of
10062 cast, with "complain" disabled.
10063 Identify the first such cast that is valid. */
10065 /* Don't attempt to run such logic within template processing. */
10066 if (processing_template_decl)
10067 return NULL;
10069 /* First try const_cast. */
10070 trial = build_const_cast (input_location, dst_type, orig_expr, tf_none);
10071 if (trial != error_mark_node)
10072 return "const_cast";
10074 /* If that fails, try static_cast. */
10075 trial = build_static_cast (input_location, dst_type, orig_expr, tf_none);
10076 if (trial != error_mark_node)
10077 return "static_cast";
10079 /* Finally, try reinterpret_cast. */
10080 trial = build_reinterpret_cast (input_location, dst_type, orig_expr,
10081 tf_none);
10082 if (trial != error_mark_node)
10083 return "reinterpret_cast";
10085 /* No such cast possible. */
10086 return NULL;
10089 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
10090 suggesting how to convert a C-style cast of the form:
10092 (DST_TYPE)ORIG_EXPR
10094 to a C++-style cast.
10096 The primary range of RICHLOC is asssumed to be that of the original
10097 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
10098 of the parens in the C-style cast. */
10100 static void
10101 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
10102 location_t close_paren_loc, tree orig_expr,
10103 tree dst_type)
10105 /* This function is non-trivial, so bail out now if the warning isn't
10106 going to be emitted. */
10107 if (!warn_old_style_cast)
10108 return;
10110 /* Try to find a legal C++ cast, trying them in order:
10111 const_cast, static_cast, reinterpret_cast. */
10112 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
10113 if (!cast_suggestion)
10114 return;
10116 /* Replace the open paren with "CAST_SUGGESTION<". */
10117 pretty_printer pp;
10118 pp_string (&pp, cast_suggestion);
10119 pp_less (&pp);
10120 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
10122 /* Replace the close paren with "> (". */
10123 rich_loc->add_fixit_replace (close_paren_loc, "> (");
10125 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
10126 rich_loc->add_fixit_insert_after (")");
10130 /* Parse a cast-expression.
10132 cast-expression:
10133 unary-expression
10134 ( type-id ) cast-expression
10136 ADDRESS_P is true iff the unary-expression is appearing as the
10137 operand of the `&' operator. CAST_P is true if this expression is
10138 the target of a cast.
10140 Returns a representation of the expression. */
10142 static cp_expr
10143 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
10144 bool decltype_p, cp_id_kind * pidk)
10146 /* If it's a `(', then we might be looking at a cast. */
10147 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10149 tree type = NULL_TREE;
10150 cp_expr expr (NULL_TREE);
10151 int cast_expression = 0;
10152 const char *saved_message;
10154 /* There's no way to know yet whether or not this is a cast.
10155 For example, `(int (3))' is a unary-expression, while `(int)
10156 3' is a cast. So, we resort to parsing tentatively. */
10157 cp_parser_parse_tentatively (parser);
10158 /* Types may not be defined in a cast. */
10159 saved_message = parser->type_definition_forbidden_message;
10160 parser->type_definition_forbidden_message
10161 = G_("types may not be defined in casts");
10162 /* Consume the `('. */
10163 matching_parens parens;
10164 cp_token *open_paren = parens.consume_open (parser);
10165 location_t open_paren_loc = open_paren->location;
10166 location_t close_paren_loc = UNKNOWN_LOCATION;
10168 /* A very tricky bit is that `(struct S) { 3 }' is a
10169 compound-literal (which we permit in C++ as an extension).
10170 But, that construct is not a cast-expression -- it is a
10171 postfix-expression. (The reason is that `(struct S) { 3 }.i'
10172 is legal; if the compound-literal were a cast-expression,
10173 you'd need an extra set of parentheses.) But, if we parse
10174 the type-id, and it happens to be a class-specifier, then we
10175 will commit to the parse at that point, because we cannot
10176 undo the action that is done when creating a new class. So,
10177 then we cannot back up and do a postfix-expression.
10179 Another tricky case is the following (c++/29234):
10181 struct S { void operator () (); };
10183 void foo ()
10185 ( S()() );
10188 As a type-id we parse the parenthesized S()() as a function
10189 returning a function, groktypename complains and we cannot
10190 back up in this case either.
10192 Therefore, we scan ahead to the closing `)', and check to see
10193 if the tokens after the `)' can start a cast-expression. Otherwise
10194 we are dealing with an unary-expression, a postfix-expression
10195 or something else.
10197 Yet another tricky case, in C++11, is the following (c++/54891):
10199 (void)[]{};
10201 The issue is that usually, besides the case of lambda-expressions,
10202 the parenthesized type-id cannot be followed by '[', and, eg, we
10203 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
10204 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
10205 we don't commit, we try a cast-expression, then an unary-expression.
10207 Save tokens so that we can put them back. */
10208 cp_lexer_save_tokens (parser->lexer);
10210 /* We may be looking at a cast-expression. */
10211 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
10212 /*consume_paren=*/true))
10213 cast_expression
10214 = cp_parser_tokens_start_cast_expression (parser);
10216 /* Roll back the tokens we skipped. */
10217 cp_lexer_rollback_tokens (parser->lexer);
10218 /* If we aren't looking at a cast-expression, simulate an error so
10219 that the call to cp_parser_error_occurred below returns true. */
10220 if (!cast_expression)
10221 cp_parser_simulate_error (parser);
10222 else
10224 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
10225 parser->in_type_id_in_expr_p = true;
10226 /* Look for the type-id. */
10227 type = cp_parser_type_id (parser);
10228 /* Look for the closing `)'. */
10229 cp_token *close_paren = parens.require_close (parser);
10230 if (close_paren)
10231 close_paren_loc = close_paren->location;
10232 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
10235 /* Restore the saved message. */
10236 parser->type_definition_forbidden_message = saved_message;
10238 /* At this point this can only be either a cast or a
10239 parenthesized ctor such as `(T ())' that looks like a cast to
10240 function returning T. */
10241 if (!cp_parser_error_occurred (parser))
10243 /* Only commit if the cast-expression doesn't start with
10244 '++', '--', or '[' in C++11. */
10245 if (cast_expression > 0)
10246 cp_parser_commit_to_topmost_tentative_parse (parser);
10248 expr = cp_parser_cast_expression (parser,
10249 /*address_p=*/false,
10250 /*cast_p=*/true,
10251 /*decltype_p=*/false,
10252 pidk);
10254 if (cp_parser_parse_definitely (parser))
10256 /* Warn about old-style casts, if so requested. */
10257 if (warn_old_style_cast
10258 && !in_system_header_at (input_location)
10259 && !VOID_TYPE_P (type)
10260 && current_lang_name != lang_name_c)
10262 gcc_rich_location rich_loc (input_location);
10263 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
10264 expr, type);
10265 warning_at (&rich_loc, OPT_Wold_style_cast,
10266 "use of old-style cast to %q#T", type);
10269 /* Only type conversions to integral or enumeration types
10270 can be used in constant-expressions. */
10271 if (!cast_valid_in_integral_constant_expression_p (type)
10272 && cp_parser_non_integral_constant_expression (parser,
10273 NIC_CAST))
10274 return error_mark_node;
10276 /* Perform the cast. */
10277 /* Make a location:
10278 (TYPE) EXPR
10279 ^~~~~~~~~~~
10280 with start==caret at the open paren, extending to the
10281 end of "expr". */
10282 location_t cast_loc = make_location (open_paren_loc,
10283 open_paren_loc,
10284 expr.get_finish ());
10285 expr = build_c_cast (cast_loc, type, expr);
10286 return expr;
10289 else
10290 cp_parser_abort_tentative_parse (parser);
10293 /* If we get here, then it's not a cast, so it must be a
10294 unary-expression. */
10295 return cp_parser_unary_expression (parser, pidk, address_p,
10296 cast_p, decltype_p);
10299 /* Parse a binary expression of the general form:
10301 pm-expression:
10302 cast-expression
10303 pm-expression .* cast-expression
10304 pm-expression ->* cast-expression
10306 multiplicative-expression:
10307 pm-expression
10308 multiplicative-expression * pm-expression
10309 multiplicative-expression / pm-expression
10310 multiplicative-expression % pm-expression
10312 additive-expression:
10313 multiplicative-expression
10314 additive-expression + multiplicative-expression
10315 additive-expression - multiplicative-expression
10317 shift-expression:
10318 additive-expression
10319 shift-expression << additive-expression
10320 shift-expression >> additive-expression
10322 relational-expression:
10323 shift-expression
10324 relational-expression < shift-expression
10325 relational-expression > shift-expression
10326 relational-expression <= shift-expression
10327 relational-expression >= shift-expression
10329 GNU Extension:
10331 relational-expression:
10332 relational-expression <? shift-expression
10333 relational-expression >? shift-expression
10335 equality-expression:
10336 relational-expression
10337 equality-expression == relational-expression
10338 equality-expression != relational-expression
10340 and-expression:
10341 equality-expression
10342 and-expression & equality-expression
10344 exclusive-or-expression:
10345 and-expression
10346 exclusive-or-expression ^ and-expression
10348 inclusive-or-expression:
10349 exclusive-or-expression
10350 inclusive-or-expression | exclusive-or-expression
10352 logical-and-expression:
10353 inclusive-or-expression
10354 logical-and-expression && inclusive-or-expression
10356 logical-or-expression:
10357 logical-and-expression
10358 logical-or-expression || logical-and-expression
10360 All these are implemented with a single function like:
10362 binary-expression:
10363 simple-cast-expression
10364 binary-expression <token> binary-expression
10366 CAST_P is true if this expression is the target of a cast.
10368 The binops_by_token map is used to get the tree codes for each <token> type.
10369 binary-expressions are associated according to a precedence table. */
10371 #define TOKEN_PRECEDENCE(token) \
10372 (((token->type == CPP_GREATER \
10373 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
10374 && !parser->greater_than_is_operator_p) \
10375 ? PREC_NOT_OPERATOR \
10376 : binops_by_token[token->type].prec)
10378 static cp_expr
10379 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
10380 bool no_toplevel_fold_p,
10381 bool decltype_p,
10382 enum cp_parser_prec prec,
10383 cp_id_kind * pidk)
10385 cp_parser_expression_stack stack;
10386 cp_parser_expression_stack_entry *sp = &stack[0];
10387 cp_parser_expression_stack_entry *disable_warnings_sp = NULL;
10388 cp_parser_expression_stack_entry current;
10389 cp_expr rhs;
10390 cp_token *token;
10391 enum tree_code rhs_type;
10392 enum cp_parser_prec new_prec, lookahead_prec;
10393 tree overload;
10395 /* Parse the first expression. */
10396 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
10397 ? TRUTH_NOT_EXPR : ERROR_MARK);
10398 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
10399 cast_p, decltype_p, pidk);
10400 current.prec = prec;
10402 if (cp_parser_error_occurred (parser))
10403 return error_mark_node;
10405 for (;;)
10407 /* Get an operator token. */
10408 token = cp_lexer_peek_token (parser->lexer);
10410 if (warn_cxx11_compat
10411 && token->type == CPP_RSHIFT
10412 && !parser->greater_than_is_operator_p)
10414 if (warning_at (token->location, OPT_Wc__11_compat,
10415 "%<>>%> operator is treated"
10416 " as two right angle brackets in C++11"))
10417 inform (token->location,
10418 "suggest parentheses around %<>>%> expression");
10421 new_prec = TOKEN_PRECEDENCE (token);
10422 if (new_prec != PREC_NOT_OPERATOR
10423 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10424 /* This is a fold-expression; handle it later. */
10425 new_prec = PREC_NOT_OPERATOR;
10427 /* Popping an entry off the stack means we completed a subexpression:
10428 - either we found a token which is not an operator (`>' where it is not
10429 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
10430 will happen repeatedly;
10431 - or, we found an operator which has lower priority. This is the case
10432 where the recursive descent *ascends*, as in `3 * 4 + 5' after
10433 parsing `3 * 4'. */
10434 if (new_prec <= current.prec)
10436 if (sp == stack)
10437 break;
10438 else
10439 goto pop;
10442 get_rhs:
10443 current.tree_type = binops_by_token[token->type].tree_type;
10444 current.loc = token->location;
10445 current.flags = token->flags;
10447 /* We used the operator token. */
10448 cp_lexer_consume_token (parser->lexer);
10450 /* For "false && x" or "true || x", x will never be executed;
10451 disable warnings while evaluating it. */
10452 if ((current.tree_type == TRUTH_ANDIF_EXPR
10453 && cp_fully_fold (current.lhs) == truthvalue_false_node)
10454 || (current.tree_type == TRUTH_ORIF_EXPR
10455 && cp_fully_fold (current.lhs) == truthvalue_true_node))
10457 disable_warnings_sp = sp;
10458 ++c_inhibit_evaluation_warnings;
10461 /* Extract another operand. It may be the RHS of this expression
10462 or the LHS of a new, higher priority expression. */
10463 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
10464 ? TRUTH_NOT_EXPR : ERROR_MARK);
10465 rhs = cp_parser_simple_cast_expression (parser);
10467 /* Get another operator token. Look up its precedence to avoid
10468 building a useless (immediately popped) stack entry for common
10469 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
10470 token = cp_lexer_peek_token (parser->lexer);
10471 lookahead_prec = TOKEN_PRECEDENCE (token);
10472 if (lookahead_prec != PREC_NOT_OPERATOR
10473 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10474 lookahead_prec = PREC_NOT_OPERATOR;
10475 if (lookahead_prec > new_prec)
10477 /* ... and prepare to parse the RHS of the new, higher priority
10478 expression. Since precedence levels on the stack are
10479 monotonically increasing, we do not have to care about
10480 stack overflows. */
10481 *sp = current;
10482 ++sp;
10483 current.lhs = rhs;
10484 current.lhs_type = rhs_type;
10485 current.prec = new_prec;
10486 new_prec = lookahead_prec;
10487 goto get_rhs;
10489 pop:
10490 lookahead_prec = new_prec;
10491 /* If the stack is not empty, we have parsed into LHS the right side
10492 (`4' in the example above) of an expression we had suspended.
10493 We can use the information on the stack to recover the LHS (`3')
10494 from the stack together with the tree code (`MULT_EXPR'), and
10495 the precedence of the higher level subexpression
10496 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
10497 which will be used to actually build the additive expression. */
10498 rhs = current.lhs;
10499 rhs_type = current.lhs_type;
10500 --sp;
10501 current = *sp;
10504 /* Undo the disabling of warnings done above. */
10505 if (sp == disable_warnings_sp)
10507 disable_warnings_sp = NULL;
10508 --c_inhibit_evaluation_warnings;
10511 if (warn_logical_not_paren
10512 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
10513 && current.lhs_type == TRUTH_NOT_EXPR
10514 /* Avoid warning for !!x == y. */
10515 && (TREE_CODE (current.lhs) != NE_EXPR
10516 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
10517 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
10518 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
10519 /* Avoid warning for !b == y where b is boolean. */
10520 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
10521 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
10522 != BOOLEAN_TYPE))))
10523 /* Avoid warning for !!b == y where b is boolean. */
10524 && (!(DECL_P (tree_strip_any_location_wrapper (current.lhs))
10525 || (TREE_CODE (current.lhs) == NON_LVALUE_EXPR
10526 && DECL_P (tree_strip_any_location_wrapper
10527 (TREE_OPERAND (current.lhs, 0)))))
10528 || TREE_TYPE (current.lhs) == NULL_TREE
10529 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
10530 warn_logical_not_parentheses (current.loc, current.tree_type,
10531 current.lhs, maybe_constant_value (rhs));
10533 if (warn_xor_used_as_pow
10534 && current.tree_type == BIT_XOR_EXPR
10535 /* Don't warn for named "xor" (as opposed to '^'). */
10536 && !(current.flags & NAMED_OP)
10537 && current.lhs.decimal_p ()
10538 && rhs.decimal_p ())
10539 check_for_xor_used_as_pow
10540 (current.lhs.get_location (),
10541 tree_strip_any_location_wrapper (current.lhs),
10542 current.loc,
10543 rhs.get_location (),
10544 tree_strip_any_location_wrapper (rhs));
10546 overload = NULL;
10548 location_t combined_loc = make_location (current.loc,
10549 current.lhs.get_start (),
10550 rhs.get_finish ());
10552 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
10553 ERROR_MARK for everything that is not a binary expression.
10554 This makes warn_about_parentheses miss some warnings that
10555 involve unary operators. For unary expressions we should
10556 pass the correct tree_code unless the unary expression was
10557 surrounded by parentheses.
10559 if (no_toplevel_fold_p
10560 && lookahead_prec <= current.prec
10561 && sp == stack)
10563 if (current.lhs == error_mark_node || rhs == error_mark_node)
10564 current.lhs = error_mark_node;
10565 else
10567 current.lhs.maybe_add_location_wrapper ();
10568 rhs.maybe_add_location_wrapper ();
10569 current.lhs
10570 = build_min (current.tree_type,
10571 TREE_CODE_CLASS (current.tree_type)
10572 == tcc_comparison
10573 ? boolean_type_node : TREE_TYPE (current.lhs),
10574 current.lhs.get_value (), rhs.get_value ());
10575 SET_EXPR_LOCATION (current.lhs, combined_loc);
10578 else
10580 op_location_t op_loc (current.loc, combined_loc);
10581 current.lhs = build_x_binary_op (op_loc, current.tree_type,
10582 current.lhs, current.lhs_type,
10583 rhs, rhs_type, NULL_TREE, &overload,
10584 complain_flags (decltype_p));
10585 /* TODO: build_x_binary_op doesn't always honor the location. */
10586 current.lhs.set_location (combined_loc);
10588 current.lhs_type = current.tree_type;
10590 /* If the binary operator required the use of an overloaded operator,
10591 then this expression cannot be an integral constant-expression.
10592 An overloaded operator can be used even if both operands are
10593 otherwise permissible in an integral constant-expression if at
10594 least one of the operands is of enumeration type. */
10596 if (overload
10597 && cp_parser_non_integral_constant_expression (parser,
10598 NIC_OVERLOADED))
10599 return error_mark_node;
10602 return current.lhs;
10605 static cp_expr
10606 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
10607 bool no_toplevel_fold_p,
10608 enum cp_parser_prec prec,
10609 cp_id_kind * pidk)
10611 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
10612 /*decltype*/false, prec, pidk);
10615 /* Parse the `? expression : assignment-expression' part of a
10616 conditional-expression. The LOGICAL_OR_EXPR is the
10617 logical-or-expression that started the conditional-expression.
10618 Returns a representation of the entire conditional-expression.
10620 This routine is used by cp_parser_assignment_expression
10621 and cp_parser_conditional_expression.
10623 ? expression : assignment-expression
10625 GNU Extensions:
10627 ? : assignment-expression */
10629 static tree
10630 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
10632 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
10633 cp_expr assignment_expr;
10634 struct cp_token *token;
10635 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10637 /* Consume the `?' token. */
10638 cp_lexer_consume_token (parser->lexer);
10639 token = cp_lexer_peek_token (parser->lexer);
10640 if (cp_parser_allow_gnu_extensions_p (parser)
10641 && token->type == CPP_COLON)
10643 pedwarn (token->location, OPT_Wpedantic,
10644 "ISO C++ does not allow %<?:%> with omitted middle operand");
10645 /* Implicit true clause. */
10646 expr = NULL_TREE;
10647 c_inhibit_evaluation_warnings +=
10648 folded_logical_or_expr == truthvalue_true_node;
10649 warn_for_omitted_condop (token->location, logical_or_expr);
10651 else
10653 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10654 parser->colon_corrects_to_scope_p = false;
10655 /* Parse the expression. */
10656 c_inhibit_evaluation_warnings +=
10657 folded_logical_or_expr == truthvalue_false_node;
10658 expr = cp_parser_expression (parser);
10659 c_inhibit_evaluation_warnings +=
10660 ((folded_logical_or_expr == truthvalue_true_node)
10661 - (folded_logical_or_expr == truthvalue_false_node));
10662 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10665 /* The next token should be a `:'. */
10666 cp_parser_require (parser, CPP_COLON, RT_COLON);
10667 /* Parse the assignment-expression. */
10668 assignment_expr = cp_parser_assignment_expression (parser);
10669 c_inhibit_evaluation_warnings -=
10670 folded_logical_or_expr == truthvalue_true_node;
10672 /* Make a location:
10673 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
10674 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
10675 with the caret at the "?", ranging from the start of
10676 the logical_or_expr to the end of the assignment_expr. */
10677 loc = make_location (loc,
10678 logical_or_expr.get_start (),
10679 assignment_expr.get_finish ());
10681 /* Build the conditional-expression. */
10682 return build_x_conditional_expr (loc, logical_or_expr,
10683 expr,
10684 assignment_expr,
10685 tf_warning_or_error);
10688 /* Parse a conditional-expression.
10690 conditional-expression:
10691 logical-or-expression
10692 logical-or-expression ? expression : assignment-expression
10694 GNU Extensions:
10696 logical-or-expression ? : assignment-expression */
10698 static cp_expr
10699 cp_parser_conditional_expression (cp_parser *parser)
10701 cp_expr expr = cp_parser_binary_expression (parser, false, false, false,
10702 PREC_NOT_OPERATOR, NULL);
10703 /* If the next token is a `?' then we're actually looking at
10704 a conditional-expression; otherwise we're done. */
10705 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
10706 return cp_parser_question_colon_clause (parser, expr);
10707 return expr;
10710 /* Parse an assignment-expression.
10712 assignment-expression:
10713 conditional-expression
10714 logical-or-expression assignment-operator assignment_expression
10715 throw-expression
10716 yield-expression
10718 CAST_P is true if this expression is the target of a cast.
10719 DECLTYPE_P is true if this expression is the operand of decltype.
10721 Returns a representation for the expression. */
10723 static cp_expr
10724 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
10725 bool cast_p, bool decltype_p)
10727 cp_expr expr;
10729 /* If the next token is the `throw' keyword, then we're looking at
10730 a throw-expression. */
10731 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
10732 expr = cp_parser_throw_expression (parser);
10733 /* If the next token is the `co_yield' keyword, then we're looking at
10734 a yield-expression. */
10735 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CO_YIELD))
10736 expr = cp_parser_yield_expression (parser);
10737 /* Otherwise, it must be that we are looking at a
10738 logical-or-expression. */
10739 else
10741 /* Parse the binary expressions (logical-or-expression). */
10742 expr = cp_parser_binary_expression (parser, cast_p, false,
10743 decltype_p,
10744 PREC_NOT_OPERATOR, pidk);
10745 /* If the next token is a `?' then we're actually looking at a
10746 conditional-expression. */
10747 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
10748 return cp_parser_question_colon_clause (parser, expr);
10749 else
10751 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10753 /* If it's an assignment-operator, we're using the second
10754 production. */
10755 enum tree_code assignment_operator
10756 = cp_parser_assignment_operator_opt (parser);
10757 if (assignment_operator != ERROR_MARK)
10759 /* Parse the right-hand side of the assignment. */
10760 cp_expr rhs = cp_parser_initializer_clause (parser);
10762 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
10763 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10765 /* An assignment may not appear in a
10766 constant-expression. */
10767 if (cp_parser_non_integral_constant_expression (parser,
10768 NIC_ASSIGNMENT))
10769 return error_mark_node;
10770 /* Build the assignment expression. Its default
10771 location:
10772 LHS = RHS
10773 ~~~~^~~~~
10774 is the location of the '=' token as the
10775 caret, ranging from the start of the lhs to the
10776 end of the rhs. */
10777 loc = make_location (loc,
10778 expr.get_start (),
10779 rhs.get_finish ());
10780 expr = build_x_modify_expr (loc, expr,
10781 assignment_operator,
10782 rhs, NULL_TREE,
10783 complain_flags (decltype_p));
10784 /* TODO: build_x_modify_expr doesn't honor the location,
10785 so we must set it here. */
10786 expr.set_location (loc);
10791 return expr;
10794 /* Parse an (optional) assignment-operator.
10796 assignment-operator: one of
10797 = *= /= %= += -= >>= <<= &= ^= |=
10799 GNU Extension:
10801 assignment-operator: one of
10802 <?= >?=
10804 If the next token is an assignment operator, the corresponding tree
10805 code is returned, and the token is consumed. For example, for
10806 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
10807 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
10808 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
10809 operator, ERROR_MARK is returned. */
10811 static enum tree_code
10812 cp_parser_assignment_operator_opt (cp_parser* parser)
10814 enum tree_code op;
10815 cp_token *token;
10817 /* Peek at the next token. */
10818 token = cp_lexer_peek_token (parser->lexer);
10820 switch (token->type)
10822 case CPP_EQ:
10823 op = NOP_EXPR;
10824 break;
10826 case CPP_MULT_EQ:
10827 op = MULT_EXPR;
10828 break;
10830 case CPP_DIV_EQ:
10831 op = TRUNC_DIV_EXPR;
10832 break;
10834 case CPP_MOD_EQ:
10835 op = TRUNC_MOD_EXPR;
10836 break;
10838 case CPP_PLUS_EQ:
10839 op = PLUS_EXPR;
10840 break;
10842 case CPP_MINUS_EQ:
10843 op = MINUS_EXPR;
10844 break;
10846 case CPP_RSHIFT_EQ:
10847 op = RSHIFT_EXPR;
10848 break;
10850 case CPP_LSHIFT_EQ:
10851 op = LSHIFT_EXPR;
10852 break;
10854 case CPP_AND_EQ:
10855 op = BIT_AND_EXPR;
10856 break;
10858 case CPP_XOR_EQ:
10859 op = BIT_XOR_EXPR;
10860 break;
10862 case CPP_OR_EQ:
10863 op = BIT_IOR_EXPR;
10864 break;
10866 default:
10867 /* Nothing else is an assignment operator. */
10868 op = ERROR_MARK;
10871 /* An operator followed by ... is a fold-expression, handled elsewhere. */
10872 if (op != ERROR_MARK
10873 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10874 op = ERROR_MARK;
10876 /* If it was an assignment operator, consume it. */
10877 if (op != ERROR_MARK)
10878 cp_lexer_consume_token (parser->lexer);
10880 return op;
10883 /* Parse an expression.
10885 expression:
10886 assignment-expression
10887 expression , assignment-expression
10889 CAST_P is true if this expression is the target of a cast.
10890 DECLTYPE_P is true if this expression is the immediate operand of decltype,
10891 except possibly parenthesized or on the RHS of a comma (N3276).
10892 WARN_COMMA_P is true if a comma should be diagnosed.
10894 Returns a representation of the expression. */
10896 static cp_expr
10897 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
10898 bool cast_p, bool decltype_p, bool warn_comma_p)
10900 cp_expr expression = NULL_TREE;
10901 location_t loc = UNKNOWN_LOCATION;
10903 while (true)
10905 cp_expr assignment_expression;
10907 /* Parse the next assignment-expression. */
10908 assignment_expression
10909 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
10911 /* We don't create a temporary for a call that is the immediate operand
10912 of decltype or on the RHS of a comma. But when we see a comma, we
10913 need to create a temporary for a call on the LHS. */
10914 if (decltype_p && !processing_template_decl
10915 && TREE_CODE (assignment_expression) == CALL_EXPR
10916 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
10917 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10918 assignment_expression
10919 = build_cplus_new (TREE_TYPE (assignment_expression),
10920 assignment_expression, tf_warning_or_error);
10922 /* If this is the first assignment-expression, we can just
10923 save it away. */
10924 if (!expression)
10925 expression = assignment_expression;
10926 else
10928 /* Create a location with caret at the comma, ranging
10929 from the start of the LHS to the end of the RHS. */
10930 loc = make_location (loc,
10931 expression.get_start (),
10932 assignment_expression.get_finish ());
10933 expression = build_x_compound_expr (loc, expression,
10934 assignment_expression, NULL_TREE,
10935 complain_flags (decltype_p));
10936 expression.set_location (loc);
10938 /* If the next token is not a comma, or we're in a fold-expression, then
10939 we are done with the expression. */
10940 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
10941 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10942 break;
10943 /* Consume the `,'. */
10944 loc = cp_lexer_peek_token (parser->lexer)->location;
10945 if (warn_comma_p)
10947 /* [depr.comma.subscript]: A comma expression appearing as
10948 the expr-or-braced-init-list of a subscripting expression
10949 is deprecated. A parenthesized comma expression is not
10950 deprecated. */
10951 warning_at (loc, OPT_Wcomma_subscript,
10952 "top-level comma expression in array subscript "
10953 "is deprecated");
10954 warn_comma_p = false;
10956 cp_lexer_consume_token (parser->lexer);
10957 /* A comma operator cannot appear in a constant-expression. */
10958 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
10959 expression = error_mark_node;
10962 return expression;
10965 /* Parse a constant-expression.
10967 constant-expression:
10968 conditional-expression
10970 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
10971 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
10972 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
10973 is false, NON_CONSTANT_P should be NULL. If ALLOW_NON_CONSTANT_P is
10974 greater than 1, this isn't really a constant-expression, only a
10975 potentially constant-evaluated expression. If STRICT_P is true,
10976 only parse a conditional-expression, otherwise parse an
10977 assignment-expression. See below for rationale. */
10979 static cp_expr
10980 cp_parser_constant_expression (cp_parser* parser,
10981 int allow_non_constant_p /* = 0 */,
10982 bool *non_constant_p /* = NULL */,
10983 bool strict_p /* = false */)
10985 /* It might seem that we could simply parse the
10986 conditional-expression, and then check to see if it were
10987 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
10988 one that the compiler can figure out is constant, possibly after
10989 doing some simplifications or optimizations. The standard has a
10990 precise definition of constant-expression, and we must honor
10991 that, even though it is somewhat more restrictive.
10993 For example:
10995 int i[(2, 3)];
10997 is not a legal declaration, because `(2, 3)' is not a
10998 constant-expression. The `,' operator is forbidden in a
10999 constant-expression. However, GCC's constant-folding machinery
11000 will fold this operation to an INTEGER_CST for `3'. */
11002 /* Save the old settings. */
11003 bool saved_integral_constant_expression_p
11004 = parser->integral_constant_expression_p;
11005 bool saved_allow_non_integral_constant_expression_p
11006 = parser->allow_non_integral_constant_expression_p;
11007 bool saved_non_integral_constant_expression_p
11008 = parser->non_integral_constant_expression_p;
11009 /* We are now parsing a constant-expression. */
11010 parser->integral_constant_expression_p = true;
11011 parser->allow_non_integral_constant_expression_p
11012 = (allow_non_constant_p || cxx_dialect >= cxx11);
11013 parser->non_integral_constant_expression_p = false;
11015 /* A manifestly constant-evaluated expression is evaluated even in an
11016 unevaluated operand. */
11017 cp_evaluated ev (/*reset if*/allow_non_constant_p <= 1);
11019 /* Although the grammar says "conditional-expression", when not STRICT_P,
11020 we parse an "assignment-expression", which also permits
11021 "throw-expression" and the use of assignment operators. In the case
11022 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
11023 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
11024 actually essential that we look for an assignment-expression.
11025 For example, cp_parser_initializer_clauses uses this function to
11026 determine whether a particular assignment-expression is in fact
11027 constant. */
11028 cp_expr expression;
11029 if (strict_p)
11030 expression = cp_parser_conditional_expression (parser);
11031 else
11032 expression = cp_parser_assignment_expression (parser);
11033 /* Restore the old settings. */
11034 parser->integral_constant_expression_p
11035 = saved_integral_constant_expression_p;
11036 parser->allow_non_integral_constant_expression_p
11037 = saved_allow_non_integral_constant_expression_p;
11038 if (cxx_dialect >= cxx11
11039 && (!allow_non_constant_p || non_constant_p))
11041 /* Require an rvalue constant expression here; that's what our
11042 callers expect. Reference constant expressions are handled
11043 separately in e.g. cp_parser_template_argument. */
11044 tree decay = expression;
11045 if (TREE_TYPE (expression)
11046 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
11047 decay = build_address (expression);
11048 bool is_const = is_rvalue_constant_expression (decay);
11049 parser->non_integral_constant_expression_p = !is_const;
11050 if (!is_const && !allow_non_constant_p)
11051 require_rvalue_constant_expression (decay);
11053 if (allow_non_constant_p && non_constant_p)
11054 *non_constant_p = parser->non_integral_constant_expression_p;
11055 parser->non_integral_constant_expression_p
11056 = saved_non_integral_constant_expression_p;
11058 return expression;
11061 /* Parse __builtin_offsetof.
11063 offsetof-expression:
11064 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
11066 offsetof-member-designator:
11067 id-expression
11068 | offsetof-member-designator "." id-expression
11069 | offsetof-member-designator "[" expression "]"
11070 | offsetof-member-designator "->" id-expression */
11072 static cp_expr
11073 cp_parser_builtin_offsetof (cp_parser *parser)
11075 int save_ice_p, save_non_ice_p;
11076 tree type;
11077 cp_expr expr;
11078 cp_id_kind dummy;
11079 cp_token *token;
11080 location_t finish_loc;
11082 /* We're about to accept non-integral-constant things, but will
11083 definitely yield an integral constant expression. Save and
11084 restore these values around our local parsing. */
11085 save_ice_p = parser->integral_constant_expression_p;
11086 save_non_ice_p = parser->non_integral_constant_expression_p;
11088 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
11090 /* Consume the "__builtin_offsetof" token. */
11091 cp_lexer_consume_token (parser->lexer);
11092 /* Consume the opening `('. */
11093 matching_parens parens;
11094 parens.require_open (parser);
11095 /* Parse the type-id. */
11096 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11098 const char *saved_message = parser->type_definition_forbidden_message;
11099 parser->type_definition_forbidden_message
11100 = G_("types may not be defined within %<__builtin_offsetof%>");
11101 type = cp_parser_type_id (parser);
11102 parser->type_definition_forbidden_message = saved_message;
11104 /* Look for the `,'. */
11105 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
11106 token = cp_lexer_peek_token (parser->lexer);
11108 /* Build the (type *)null that begins the traditional offsetof macro. */
11109 tree object_ptr
11110 = build_static_cast (input_location, build_pointer_type (type),
11111 null_pointer_node, tf_warning_or_error);
11113 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
11114 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
11115 true, &dummy, token->location);
11116 while (true)
11118 token = cp_lexer_peek_token (parser->lexer);
11119 switch (token->type)
11121 case CPP_OPEN_SQUARE:
11122 /* offsetof-member-designator "[" expression "]" */
11123 expr = cp_parser_postfix_open_square_expression (parser, expr,
11124 true, false);
11125 break;
11127 case CPP_DEREF:
11128 /* offsetof-member-designator "->" identifier */
11129 expr = grok_array_decl (token->location, expr, integer_zero_node,
11130 NULL, tf_warning_or_error);
11131 /* FALLTHRU */
11133 case CPP_DOT:
11134 /* offsetof-member-designator "." identifier */
11135 cp_lexer_consume_token (parser->lexer);
11136 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
11137 expr, true, &dummy,
11138 token->location);
11139 break;
11141 case CPP_CLOSE_PAREN:
11142 /* Consume the ")" token. */
11143 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
11144 cp_lexer_consume_token (parser->lexer);
11145 goto success;
11147 default:
11148 /* Error. We know the following require will fail, but
11149 that gives the proper error message. */
11150 parens.require_close (parser);
11151 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
11152 expr = error_mark_node;
11153 goto failure;
11157 success:
11158 /* Make a location of the form:
11159 __builtin_offsetof (struct s, f)
11160 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
11161 with caret at the type-id, ranging from the start of the
11162 "_builtin_offsetof" token to the close paren. */
11163 loc = make_location (loc, start_loc, finish_loc);
11164 /* The result will be an INTEGER_CST, so we need to explicitly
11165 preserve the location. */
11166 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
11168 failure:
11169 parser->integral_constant_expression_p = save_ice_p;
11170 parser->non_integral_constant_expression_p = save_non_ice_p;
11172 expr = expr.maybe_add_location_wrapper ();
11173 return expr;
11176 /* Parse a builtin trait expression or type. */
11178 static cp_expr
11179 cp_parser_trait (cp_parser* parser, const cp_trait* trait)
11181 const cp_trait_kind kind = trait->kind;
11182 tree type1, type2 = NULL_TREE;
11183 const bool binary = (trait->arity == 2);
11184 const bool variadic = (trait->arity == -1);
11185 const bool type = trait->type;
11187 /* Get location of initial token. */
11188 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
11190 /* Consume the token. */
11191 cp_lexer_consume_token (parser->lexer);
11193 matching_parens parens;
11194 if (kind == CPTK_TYPE_PACK_ELEMENT)
11195 cp_parser_require (parser, CPP_LESS, RT_LESS);
11196 else
11197 parens.require_open (parser);
11199 if (kind == CPTK_IS_DEDUCIBLE)
11201 const cp_token* token = cp_lexer_peek_token (parser->lexer);
11202 type1 = cp_parser_id_expression (parser,
11203 /*template_keyword_p=*/false,
11204 /*check_dependency_p=*/true,
11205 nullptr,
11206 /*declarator_p=*/false,
11207 /*optional_p=*/false);
11208 type1 = cp_parser_lookup_name_simple (parser, type1, token->location);
11210 else if (kind == CPTK_TYPE_PACK_ELEMENT)
11211 /* __type_pack_element takes an expression as its first argument and uses
11212 template-id syntax instead of function call syntax (for consistency
11213 with Clang). We special case these properties of __type_pack_element
11214 here and elsewhere. */
11215 type1 = cp_parser_constant_expression (parser);
11216 else
11218 type_id_in_expr_sentinel s (parser);
11219 type1 = cp_parser_type_id (parser);
11222 if (type1 == error_mark_node)
11223 return error_mark_node;
11225 if (kind == CPTK_TYPE_PACK_ELEMENT)
11227 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
11228 tree trailing = cp_parser_enclosed_template_argument_list (parser);
11229 for (tree elt : tree_vec_range (trailing))
11231 if (!TYPE_P (elt))
11233 error_at (cp_expr_loc_or_input_loc (elt),
11234 "trailing argument to %<__type_pack_element%> "
11235 "is not a type");
11236 return error_mark_node;
11239 type2 = trailing;
11241 else if (binary)
11243 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
11246 type_id_in_expr_sentinel s (parser);
11247 type2 = cp_parser_type_id (parser);
11250 if (type2 == error_mark_node)
11251 return error_mark_node;
11253 else if (variadic)
11255 auto_vec<tree, 4> trailing;
11256 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11258 cp_lexer_consume_token (parser->lexer);
11259 tree elt = cp_parser_type_id (parser);
11260 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11262 cp_lexer_consume_token (parser->lexer);
11263 elt = make_pack_expansion (elt);
11265 if (elt == error_mark_node)
11266 return error_mark_node;
11267 trailing.safe_push (elt);
11269 type2 = make_tree_vec (trailing.length ());
11270 for (int i = 0; i < TREE_VEC_LENGTH (type2); ++i)
11271 TREE_VEC_ELT (type2, i) = trailing[i];
11274 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
11275 if (kind == CPTK_TYPE_PACK_ELEMENT)
11276 /* cp_parser_enclosed_template_argument_list above already took care
11277 of parsing the closing '>'. */;
11278 else
11279 parens.require_close (parser);
11281 /* Construct a location of the form:
11282 __is_trivially_copyable(_Tp)
11283 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
11284 with start == caret, finishing at the close-paren. */
11285 location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
11287 /* Complete the trait expression, which may mean either processing
11288 the trait expr now or saving it for template instantiation. */
11289 switch (kind)
11291 case CPTK_BASES:
11292 return cp_expr (finish_bases (type1, false), trait_loc);
11293 case CPTK_DIRECT_BASES:
11294 return cp_expr (finish_bases (type1, true), trait_loc);
11295 default:
11296 if (type)
11297 return finish_trait_type (kind, type1, type2, tf_warning_or_error);
11298 else
11299 return finish_trait_expr (trait_loc, kind, type1, type2);
11303 /* Parse a lambda expression.
11305 lambda-expression:
11306 lambda-introducer lambda-declarator [opt] compound-statement
11307 lambda-introducer < template-parameter-list > requires-clause [opt]
11308 lambda-declarator [opt] compound-statement
11310 Returns a representation of the expression. */
11312 static cp_expr
11313 cp_parser_lambda_expression (cp_parser* parser)
11315 tree lambda_expr = build_lambda_expr ();
11316 tree type;
11317 bool ok = true;
11318 cp_token *token = cp_lexer_peek_token (parser->lexer);
11319 cp_token_position start = 0;
11321 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
11323 if (cxx_dialect >= cxx20)
11325 /* C++20 allows lambdas in unevaluated context, but one in the type of a
11326 non-type parameter is nonsensical.
11328 Distinguish a lambda in the parameter type from a lambda in the
11329 default argument by looking at local_variables_forbidden_p, which is
11330 only set in default arguments. */
11331 if (processing_template_parmlist && !parser->local_variables_forbidden_p)
11333 error_at (token->location,
11334 "lambda-expression in template parameter type");
11335 token->error_reported = true;
11336 ok = false;
11339 else if (cp_unevaluated_operand)
11341 if (!token->error_reported)
11343 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
11344 "lambda-expression in unevaluated context"
11345 " only available with %<-std=c++20%> or %<-std=gnu++20%>");
11346 token->error_reported = true;
11348 ok = false;
11350 else if (parser->in_template_argument_list_p || processing_template_parmlist)
11352 if (!token->error_reported)
11354 error_at (token->location, "lambda-expression in template-argument"
11355 " only available with %<-std=c++20%> or %<-std=gnu++20%>");
11356 token->error_reported = true;
11358 ok = false;
11361 /* We may be in the middle of deferred access check. Disable
11362 it now. */
11363 push_deferring_access_checks (dk_no_deferred);
11365 cp_parser_lambda_introducer (parser, lambda_expr);
11366 if (cp_parser_error_occurred (parser))
11367 return error_mark_node;
11369 type = begin_lambda_type (lambda_expr);
11370 if (type == error_mark_node)
11371 return error_mark_node;
11373 record_lambda_scope (lambda_expr);
11374 record_lambda_scope_discriminator (lambda_expr);
11376 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
11377 determine_visibility (TYPE_NAME (type));
11379 /* Now that we've started the type, add the capture fields for any
11380 explicit captures. */
11381 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
11384 /* Inside the class, surrounding template-parameter-lists do not apply. */
11385 unsigned int saved_num_template_parameter_lists
11386 = parser->num_template_parameter_lists;
11387 unsigned char in_statement = parser->in_statement;
11388 bool in_switch_statement_p = parser->in_switch_statement_p;
11389 bool fully_implicit_function_template_p
11390 = parser->fully_implicit_function_template_p;
11391 tree implicit_template_parms = parser->implicit_template_parms;
11392 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
11393 bool auto_is_implicit_function_template_parm_p
11394 = parser->auto_is_implicit_function_template_parm_p;
11395 bool saved_omp_array_section_p = parser->omp_array_section_p;
11397 parser->num_template_parameter_lists = 0;
11398 parser->in_statement = 0;
11399 parser->in_switch_statement_p = false;
11400 parser->fully_implicit_function_template_p = false;
11401 parser->implicit_template_parms = 0;
11402 parser->implicit_template_scope = 0;
11403 parser->auto_is_implicit_function_template_parm_p = false;
11404 parser->omp_array_section_p = false;
11406 /* The body of a lambda in a discarded statement is not discarded. */
11407 bool discarded = in_discarded_stmt;
11408 in_discarded_stmt = 0;
11410 /* Similarly the body of a lambda in immediate function context is not
11411 in immediate function context. */
11412 bool save_in_consteval_if_p = in_consteval_if_p;
11413 in_consteval_if_p = false;
11415 /* By virtue of defining a local class, a lambda expression has access to
11416 the private variables of enclosing classes. */
11418 if (cp_parser_start_tentative_firewall (parser))
11419 start = token;
11421 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
11423 if (ok && cp_parser_error_occurred (parser))
11424 ok = false;
11426 if (ok)
11427 cp_parser_lambda_body (parser, lambda_expr);
11428 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
11430 if (cp_parser_skip_to_closing_brace (parser))
11431 cp_lexer_consume_token (parser->lexer);
11434 /* The capture list was built up in reverse order; fix that now. */
11435 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
11436 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
11438 if (ok)
11439 maybe_add_lambda_conv_op (type);
11441 finish_struct (type, /*attributes=*/NULL_TREE);
11443 in_consteval_if_p = save_in_consteval_if_p;
11444 in_discarded_stmt = discarded;
11446 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
11447 parser->in_statement = in_statement;
11448 parser->in_switch_statement_p = in_switch_statement_p;
11449 parser->fully_implicit_function_template_p
11450 = fully_implicit_function_template_p;
11451 parser->implicit_template_parms = implicit_template_parms;
11452 parser->implicit_template_scope = implicit_template_scope;
11453 parser->auto_is_implicit_function_template_parm_p
11454 = auto_is_implicit_function_template_parm_p;
11455 parser->omp_array_section_p = saved_omp_array_section_p;
11458 /* This field is only used during parsing of the lambda. */
11459 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
11461 /* This lambda shouldn't have any proxies left at this point. */
11462 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
11463 /* And now that we're done, push proxies for an enclosing lambda. */
11464 insert_pending_capture_proxies ();
11466 /* Update the lambda expression to a range. */
11467 LAMBDA_EXPR_LOCATION (lambda_expr) = make_location (token->location,
11468 token->location,
11469 parser->lexer);
11471 if (ok)
11472 lambda_expr = build_lambda_object (lambda_expr);
11473 else
11474 lambda_expr = error_mark_node;
11476 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
11478 pop_deferring_access_checks ();
11480 return lambda_expr;
11483 /* Parse the beginning of a lambda expression.
11485 lambda-introducer:
11486 [ lambda-capture [opt] ]
11488 LAMBDA_EXPR is the current representation of the lambda expression. */
11490 static void
11491 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
11493 /* Need commas after the first capture. */
11494 bool first = true;
11496 /* Eat the leading `['. */
11497 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
11499 /* Record default capture mode. "[&" "[=" "[&," "[=," */
11500 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
11501 && !cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS)
11502 && !cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME)
11503 && !cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
11504 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
11505 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11506 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
11508 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
11510 cp_lexer_consume_token (parser->lexer);
11511 first = false;
11513 if (!(at_function_scope_p () || parsing_nsdmi ()))
11514 error ("non-local lambda expression cannot have a capture-default");
11517 hash_set<tree, true> ids;
11518 tree first_capture_id = NULL_TREE;
11519 unsigned name_independent_cnt = 0;
11520 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
11522 cp_token* capture_token;
11523 tree capture_id;
11524 tree capture_init_expr;
11525 cp_id_kind idk = CP_ID_KIND_NONE;
11526 bool explicit_init_p = false;
11528 enum capture_kind_type
11530 BY_COPY,
11531 BY_REFERENCE
11533 enum capture_kind_type capture_kind = BY_COPY;
11535 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
11537 error ("expected end of capture-list");
11538 return;
11541 if (first)
11542 first = false;
11543 else
11544 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
11546 /* Possibly capture `this'. */
11547 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
11549 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11550 if (cxx_dialect < cxx20 && pedantic
11551 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
11552 pedwarn (loc, OPT_Wc__20_extensions,
11553 "explicit by-copy capture of %<this%> "
11554 "with by-copy capture default only available with "
11555 "%<-std=c++20%> or %<-std=gnu++20%>");
11556 cp_lexer_consume_token (parser->lexer);
11557 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
11558 pedwarn (input_location, 0,
11559 "already captured %qD in lambda expression",
11560 this_identifier);
11561 else
11562 add_capture (lambda_expr, /*id=*/this_identifier,
11563 /*initializer=*/finish_this_expr (),
11564 /*by_reference_p=*/true, explicit_init_p, NULL);
11565 continue;
11568 /* Possibly capture `*this'. */
11569 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
11570 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
11572 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11573 if (cxx_dialect < cxx17)
11574 pedwarn (loc, OPT_Wc__17_extensions,
11575 "%<*this%> capture only available with "
11576 "%<-std=c++17%> or %<-std=gnu++17%>");
11577 cp_lexer_consume_token (parser->lexer);
11578 cp_lexer_consume_token (parser->lexer);
11579 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
11580 pedwarn (input_location, 0,
11581 "already captured %qD in lambda expression",
11582 this_identifier);
11583 else
11584 add_capture (lambda_expr, /*id=*/this_identifier,
11585 /*initializer=*/finish_this_expr (),
11586 /*by_reference_p=*/false, explicit_init_p, NULL);
11587 continue;
11590 /* But reject `&this'. */
11591 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
11592 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
11594 error_at (cp_lexer_peek_token (parser->lexer)->location,
11595 "%<this%> cannot be captured by reference");
11596 cp_lexer_consume_token (parser->lexer);
11597 cp_lexer_consume_token (parser->lexer);
11598 continue;
11601 /* Remember whether we want to capture as a reference or not. */
11602 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
11604 capture_kind = BY_REFERENCE;
11605 cp_lexer_consume_token (parser->lexer);
11608 bool init_pack_expansion = false;
11609 location_t ellipsis_loc = UNKNOWN_LOCATION;
11610 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11612 ellipsis_loc = cp_lexer_peek_token (parser->lexer)->location;
11613 if (cxx_dialect < cxx20)
11614 pedwarn (ellipsis_loc, OPT_Wc__20_extensions,
11615 "pack init-capture only available with "
11616 "%<-std=c++20%> or %<-std=gnu++20%>");
11617 cp_lexer_consume_token (parser->lexer);
11618 init_pack_expansion = true;
11621 /* Early C++20 drafts had ...& instead of &...; be forgiving. */
11622 if (init_pack_expansion && capture_kind != BY_REFERENCE
11623 && cp_lexer_next_token_is (parser->lexer, CPP_AND))
11625 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
11626 0, "%<&%> should come before %<...%>");
11627 capture_kind = BY_REFERENCE;
11628 cp_lexer_consume_token (parser->lexer);
11631 /* Get the identifier. */
11632 capture_token = cp_lexer_peek_token (parser->lexer);
11633 capture_id = cp_parser_identifier (parser);
11635 if (capture_id == error_mark_node)
11636 /* Would be nice to have a cp_parser_skip_to_closing_x for general
11637 delimiters, but I modified this to stop on unnested ']' as well. It
11638 was already changed to stop on unnested '}', so the
11639 "closing_parenthesis" name is no more misleading with my change. */
11641 cp_parser_skip_to_closing_parenthesis (parser,
11642 /*recovering=*/true,
11643 /*or_comma=*/true,
11644 /*consume_paren=*/true);
11645 break;
11648 /* Find the initializer for this capture. */
11649 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
11650 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
11651 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11653 /* An explicit initializer exists. */
11654 if (cxx_dialect < cxx14)
11655 pedwarn (input_location, OPT_Wc__14_extensions,
11656 "lambda capture initializers "
11657 "only available with %<-std=c++14%> or %<-std=gnu++14%>");
11658 capture_init_expr = cp_parser_initializer (parser,
11659 /*direct_init=*/nullptr,
11660 /*non_constant=*/nullptr,
11661 /*subexpression_p=*/true);
11662 explicit_init_p = true;
11663 if (capture_init_expr == NULL_TREE)
11665 error ("empty initializer for lambda init-capture");
11666 capture_init_expr = error_mark_node;
11668 if (init_pack_expansion)
11669 capture_init_expr = make_pack_expansion (capture_init_expr);
11671 else
11673 const char* error_msg;
11675 /* Turn the identifier into an id-expression. */
11676 capture_init_expr
11677 = cp_parser_lookup_name_simple (parser, capture_id,
11678 capture_token->location);
11680 if (capture_init_expr == error_mark_node)
11682 unqualified_name_lookup_error (capture_id);
11683 continue;
11685 else if (!VAR_P (capture_init_expr)
11686 && TREE_CODE (capture_init_expr) != PARM_DECL)
11688 error_at (capture_token->location,
11689 "capture of non-variable %qE",
11690 capture_init_expr);
11691 if (DECL_P (capture_init_expr))
11692 inform (DECL_SOURCE_LOCATION (capture_init_expr),
11693 "%q#D declared here", capture_init_expr);
11694 continue;
11696 if (VAR_P (capture_init_expr)
11697 && decl_storage_duration (capture_init_expr) != dk_auto)
11699 if (pedwarn (capture_token->location, 0, "capture of variable "
11700 "%qD with non-automatic storage duration",
11701 capture_init_expr))
11702 inform (DECL_SOURCE_LOCATION (capture_init_expr),
11703 "%q#D declared here", capture_init_expr);
11704 continue;
11707 capture_init_expr
11708 = finish_id_expression
11709 (capture_id,
11710 capture_init_expr,
11711 parser->scope,
11712 &idk,
11713 /*integral_constant_expression_p=*/false,
11714 /*allow_non_integral_constant_expression_p=*/false,
11715 /*non_integral_constant_expression_p=*/NULL,
11716 /*template_p=*/false,
11717 /*done=*/true,
11718 /*address_p=*/false,
11719 /*template_arg_p=*/false,
11720 &error_msg,
11721 capture_token->location);
11723 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11725 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11726 cp_lexer_consume_token (parser->lexer);
11727 capture_init_expr = make_pack_expansion (capture_init_expr);
11728 if (init_pack_expansion)
11730 /* If what follows is an initializer, the second '...' is
11731 invalid. But for cases like [...xs...], the first one
11732 is invalid. */
11733 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
11734 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
11735 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11736 ellipsis_loc = loc;
11737 error_at (ellipsis_loc, "too many %<...%> in lambda capture");
11738 continue;
11743 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
11744 && !explicit_init_p)
11746 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
11747 && capture_kind == BY_COPY)
11748 pedwarn (capture_token->location, 0, "explicit by-copy capture "
11749 "of %qD redundant with by-copy capture default",
11750 capture_id);
11751 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
11752 && capture_kind == BY_REFERENCE)
11753 pedwarn (capture_token->location, 0, "explicit by-reference "
11754 "capture of %qD redundant with by-reference capture "
11755 "default", capture_id);
11758 /* Check for duplicates.
11759 Optimize for the zero or one explicit captures cases and only create
11760 the hash_set after adding second capture. */
11761 bool found = false;
11762 if (!ids.is_empty ())
11763 found = ids.add (capture_id);
11764 else if (first_capture_id == NULL_TREE)
11765 first_capture_id = capture_id;
11766 else if (capture_id == first_capture_id)
11767 found = true;
11768 else
11770 ids.add (first_capture_id);
11771 ids.add (capture_id);
11773 if (found && explicit_init_p && id_equal (capture_id, "_"))
11774 found = false;
11775 if (found)
11776 pedwarn (input_location, 0,
11777 "already captured %qD in lambda expression", capture_id);
11778 else
11779 add_capture (lambda_expr, capture_id, capture_init_expr,
11780 /*by_reference_p=*/capture_kind == BY_REFERENCE,
11781 explicit_init_p, &name_independent_cnt);
11783 /* If there is any qualification still in effect, clear it
11784 now; we will be starting fresh with the next capture. */
11785 parser->scope = NULL_TREE;
11786 parser->qualifying_scope = NULL_TREE;
11787 parser->object_scope = NULL_TREE;
11790 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11793 /* Parse the (optional) middle of a lambda expression.
11795 lambda-declarator:
11796 ( parameter-declaration-clause ) lambda-specifiers requires-clause [opt]
11797 lambda-specifiers (C++23)
11799 lambda-specifiers:
11800 decl-specifier-seq [opt] noexcept-specifier [opt]
11801 attribute-specifier-seq [opt] trailing-return-type [opt]
11803 LAMBDA_EXPR is the current representation of the lambda expression. */
11805 static bool
11806 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
11808 /* 5.1.1.4 of the standard says:
11809 If a lambda-expression does not include a lambda-declarator, it is as if
11810 the lambda-declarator were ().
11811 This means an empty parameter list, no attributes, and no exception
11812 specification. */
11813 tree param_list = void_list_node;
11814 tree std_attrs = NULL_TREE;
11815 tree gnu_attrs = NULL_TREE;
11816 tree exception_spec = NULL_TREE;
11817 tree template_param_list = NULL_TREE;
11818 tree tx_qual = NULL_TREE;
11819 tree return_type = NULL_TREE;
11820 tree trailing_requires_clause = NULL_TREE;
11821 bool has_param_list = false;
11822 location_t omitted_parms_loc = UNKNOWN_LOCATION;
11823 cp_decl_specifier_seq lambda_specs;
11824 clear_decl_specs (&lambda_specs);
11825 /* A lambda op() is const unless explicitly 'mutable'. */
11826 cp_cv_quals quals = TYPE_QUAL_CONST;
11828 /* The template-parameter-list is optional, but must begin with
11829 an opening angle if present. */
11830 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
11832 if (cxx_dialect < cxx20
11833 && (pedantic || cxx_dialect < cxx14))
11834 pedwarn (parser->lexer->next_token->location, OPT_Wc__20_extensions,
11835 "lambda templates are only available with "
11836 "%<-std=c++20%> or %<-std=gnu++20%>");
11838 cp_lexer_consume_token (parser->lexer);
11840 template_param_list = cp_parser_template_parameter_list (parser);
11841 cp_parser_require_end_of_template_parameter_list (parser);
11843 /* We may have a constrained generic lambda; parse the requires-clause
11844 immediately after the template-parameter-list and combine with any
11845 shorthand constraints present. */
11846 tree dreqs = cp_parser_requires_clause_opt (parser, true);
11847 if (flag_concepts)
11849 tree reqs = get_shorthand_constraints (current_template_parms);
11850 if (dreqs)
11851 reqs = combine_constraint_expressions (reqs, dreqs);
11852 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
11855 /* We just processed one more parameter list. */
11856 ++parser->num_template_parameter_lists;
11859 /* Committee discussion supports allowing attributes here. */
11860 lambda_specs.attributes = cp_parser_attributes_opt (parser);
11862 /* The parameter-declaration-clause is optional (unless
11863 template-parameter-list was given), but must begin with an
11864 opening parenthesis if present. */
11865 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
11867 matching_parens parens;
11868 parens.consume_open (parser);
11870 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
11872 /* Parse parameters. */
11873 param_list
11874 = cp_parser_parameter_declaration_clause
11875 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL);
11877 /* Default arguments shall not be specified in the
11878 parameter-declaration-clause of a lambda-declarator. */
11879 if (pedantic && cxx_dialect < cxx14)
11880 for (tree t = param_list; t; t = TREE_CHAIN (t))
11881 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
11882 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
11883 OPT_Wc__14_extensions,
11884 "default argument specified for lambda parameter");
11886 parens.require_close (parser);
11887 has_param_list = true;
11889 else if (cxx_dialect < cxx23)
11890 omitted_parms_loc = cp_lexer_peek_token (parser->lexer)->location;
11892 /* [expr.prim.lambda.general]
11893 lambda-specifier:
11894 consteval, constexpr, mutable, static
11895 [4] A lambda-specifier-seq shall contain at most one of each
11896 lambda-specifier and shall not contain both constexpr and consteval.
11897 The lambda-specifier-seq shall not contain both mutable and static. */
11898 int declares_class_or_enum;
11899 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
11900 cp_parser_decl_specifier_seq (parser,
11901 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
11902 &lambda_specs, &declares_class_or_enum);
11904 if (omitted_parms_loc && lambda_specs.any_specifiers_p)
11906 pedwarn (omitted_parms_loc, OPT_Wc__23_extensions,
11907 "parameter declaration before lambda declaration "
11908 "specifiers only optional with %<-std=c++2b%> or "
11909 "%<-std=gnu++2b%>");
11910 omitted_parms_loc = UNKNOWN_LOCATION;
11912 /* Peek at the params, see if we have an xobj parameter. */
11913 if (param_list && TREE_PURPOSE (param_list) == this_identifier)
11915 quals = TYPE_UNQUALIFIED;
11916 /* We still need grokdeclarator to see that this is an xobj function
11917 and finish the rest of the work, don't mutate it. */
11918 tree const xobj_param = TREE_VALUE (param_list);
11919 tree const param_type = TREE_TYPE (xobj_param);
11920 /* [expr.prim.lambda.closure-5]
11921 Given a lambda with a lambda-capture, the type of the explicit object
11922 parameter, if any, of the lambda's function call operator (possibly
11923 instantiated from a function call operator template) shall be either:
11924 -- the closure type,
11925 -- a class type derived from the closure type, or
11926 -- a reference to a possibly cv-qualified such type. */
11927 bool const unrelated_with_captures
11928 = (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
11929 || LAMBDA_EXPR_CAPTURE_LIST (lambda_expr))
11930 /* Since a lambda's type is anonymous, we can assume an xobj
11931 parameter is unrelated to the closure if it is non-dependent.
11932 If it is dependent we handle it at instantiation time. */
11933 && !WILDCARD_TYPE_P (non_reference (param_type));
11934 if (unrelated_with_captures)
11936 error_at (DECL_SOURCE_LOCATION (xobj_param),
11937 "a lambda with captures may not have an explicit object "
11938 "parameter of an unrelated type");
11939 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = NULL_TREE;
11942 /* [expr.prim.lambda.general-4]
11943 If the lambda-declarator contains an explicit object parameter
11944 ([dcl.fct]), then no lambda-specifier in the lambda-specifier-seq
11945 shall be mutable or static. */
11946 if (lambda_specs.storage_class == sc_mutable)
11948 auto_diagnostic_group d;
11949 error_at (lambda_specs.locations[ds_storage_class],
11950 "%<mutable%> lambda specifier "
11951 "with explicit object parameter");
11952 /* Tell the user how to do what they probably meant, maybe fixits
11953 would be appropriate later? */
11954 if (unrelated_with_captures)
11955 /* The following hints don't make sense when we already have an
11956 unrelated type with captures, don't emit them. */;
11957 else if (!TYPE_REF_P (param_type))
11958 inform (DECL_SOURCE_LOCATION (xobj_param),
11959 "the passed in closure object will not be mutated because "
11960 "it is taken by value");
11961 else if (TYPE_READONLY (TREE_TYPE (param_type)))
11962 inform (DECL_SOURCE_LOCATION (xobj_param),
11963 "declare the explicit object parameter as non-const "
11964 "reference instead");
11965 else
11966 inform (DECL_SOURCE_LOCATION (xobj_param),
11967 "explicit object parameter is already a mutable "
11968 "reference");
11970 else if (lambda_specs.storage_class == sc_static)
11972 auto_diagnostic_group d;
11973 error_at (lambda_specs.locations[ds_storage_class],
11974 "%<static%> lambda specifier "
11975 "with explicit object parameter");
11976 inform (DECL_SOURCE_LOCATION (xobj_param),
11977 "explicit object parameter declared here");
11980 else if (lambda_specs.storage_class == sc_mutable)
11982 quals = TYPE_UNQUALIFIED;
11984 else if (lambda_specs.storage_class == sc_static)
11986 /* [expr.prim.lambda.general-4]
11987 If the lambda-specifier-seq contains static, there shall be no
11988 lambda-capture. */
11989 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
11990 || LAMBDA_EXPR_CAPTURE_LIST (lambda_expr))
11991 error_at (lambda_specs.locations[ds_storage_class],
11992 "%<static%> lambda specifier with lambda capture");
11993 else
11995 LAMBDA_EXPR_STATIC_P (lambda_expr) = 1;
11996 quals = TYPE_UNQUALIFIED;
12000 tx_qual = cp_parser_tx_qualifier_opt (parser);
12001 if (omitted_parms_loc && tx_qual)
12003 pedwarn (omitted_parms_loc, OPT_Wc__23_extensions,
12004 "parameter declaration before lambda transaction "
12005 "qualifier only optional with %<-std=c++2b%> or "
12006 "%<-std=gnu++2b%>");
12007 omitted_parms_loc = UNKNOWN_LOCATION;
12010 /* Parse optional exception specification. */
12011 exception_spec
12012 = cp_parser_exception_specification_opt (parser, CP_PARSER_FLAGS_NONE);
12014 if (omitted_parms_loc && exception_spec)
12016 pedwarn (omitted_parms_loc, OPT_Wc__23_extensions,
12017 "parameter declaration before lambda exception "
12018 "specification only optional with %<-std=c++2b%> or "
12019 "%<-std=gnu++2b%>");
12020 omitted_parms_loc = UNKNOWN_LOCATION;
12023 /* GCC 8 accepted attributes here, and this is the place for standard C++11
12024 attributes that appertain to the function type. */
12025 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
12026 gnu_attrs = cp_parser_gnu_attributes_opt (parser);
12027 else
12028 std_attrs = cp_parser_std_attribute_spec_seq (parser);
12030 /* Parse optional trailing return type. */
12031 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
12033 if (omitted_parms_loc)
12034 pedwarn (omitted_parms_loc, OPT_Wc__23_extensions,
12035 "parameter declaration before lambda trailing "
12036 "return type only optional with %<-std=c++2b%> or "
12037 "%<-std=gnu++2b%>");
12038 cp_lexer_consume_token (parser->lexer);
12039 return_type = cp_parser_trailing_type_id (parser);
12042 /* Also allow GNU attributes at the very end of the declaration, the usual
12043 place for GNU attributes. */
12044 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
12045 gnu_attrs = chainon (gnu_attrs, cp_parser_gnu_attributes_opt (parser));
12047 if (has_param_list)
12049 /* Parse optional trailing requires clause. */
12050 trailing_requires_clause = cp_parser_requires_clause_opt (parser, false);
12052 /* The function parameters must be in scope all the way until after the
12053 trailing-return-type in case of decltype. */
12054 pop_bindings_and_leave_scope ();
12057 /* Create the function call operator.
12059 Messing with declarators like this is no uglier than building up the
12060 FUNCTION_DECL by hand, and this is less likely to get out of sync with
12061 other code. */
12063 cp_decl_specifier_seq return_type_specs;
12064 cp_declarator* declarator;
12065 tree fco;
12066 void *p;
12068 clear_decl_specs (&return_type_specs);
12069 return_type_specs.type = make_auto ();
12071 if (lambda_specs.locations[ds_constexpr])
12073 if (cxx_dialect >= cxx17)
12074 return_type_specs.locations[ds_constexpr]
12075 = lambda_specs.locations[ds_constexpr];
12076 else
12077 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
12078 "lambda only available with %<-std=c++17%> or "
12079 "%<-std=gnu++17%>");
12081 if (lambda_specs.locations[ds_consteval])
12082 return_type_specs.locations[ds_consteval]
12083 = lambda_specs.locations[ds_consteval];
12084 if (LAMBDA_EXPR_STATIC_P (lambda_expr))
12086 return_type_specs.storage_class = sc_static;
12087 return_type_specs.locations[ds_storage_class]
12088 = lambda_specs.locations[ds_storage_class];
12091 p = obstack_alloc (&declarator_obstack, 0);
12093 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none,
12094 LAMBDA_EXPR_LOCATION (lambda_expr));
12096 declarator = make_call_declarator (declarator, param_list, quals,
12097 VIRT_SPEC_UNSPECIFIED,
12098 REF_QUAL_NONE,
12099 tx_qual,
12100 exception_spec,
12101 return_type,
12102 trailing_requires_clause,
12103 std_attrs,
12104 UNKNOWN_LOCATION);
12106 fco = grokmethod (&return_type_specs,
12107 declarator,
12108 chainon (gnu_attrs, lambda_specs.attributes));
12109 if (fco != error_mark_node)
12111 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
12112 DECL_ARTIFICIAL (fco) = 1;
12113 if (DECL_IOBJ_MEMBER_FUNCTION_P (fco))
12114 /* Give the object parameter a different name. */
12115 DECL_NAME (DECL_ARGUMENTS (fco)) = closure_identifier;
12116 DECL_SET_LAMBDA_FUNCTION (fco, true);
12118 if (template_param_list)
12120 fco = finish_member_template_decl (fco);
12121 finish_template_decl (template_param_list);
12122 --parser->num_template_parameter_lists;
12124 else if (parser->fully_implicit_function_template_p)
12125 fco = finish_fully_implicit_template (parser, fco);
12127 finish_member_declaration (fco);
12128 record_lambda_scope_sig_discriminator (lambda_expr, fco);
12130 obstack_free (&declarator_obstack, p);
12132 return (fco != error_mark_node);
12136 /* Parse the body of a lambda expression, which is simply
12138 compound-statement
12140 but which requires special handling.
12141 LAMBDA_EXPR is the current representation of the lambda expression. */
12143 static void
12144 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
12146 bool nested = (current_function_decl != NULL_TREE);
12147 unsigned char local_variables_forbidden_p
12148 = parser->local_variables_forbidden_p;
12149 bool in_function_body = parser->in_function_body;
12151 /* The body of a lambda-expression is not a subexpression of the enclosing
12152 expression. */
12153 cp_evaluated ev;
12155 if (nested)
12156 push_function_context ();
12157 else
12158 /* Still increment function_depth so that we don't GC in the
12159 middle of an expression. */
12160 ++function_depth;
12162 auto odsd = make_temp_override (parser->omp_declare_simd, NULL);
12163 auto ord = make_temp_override (parser->oacc_routine, NULL);
12164 auto oafp = make_temp_override (parser->omp_attrs_forbidden_p, false);
12165 vec<tree> omp_privatization_save;
12166 save_omp_privatization_clauses (omp_privatization_save);
12167 /* Clear this in case we're in the middle of a default argument. */
12168 parser->local_variables_forbidden_p = 0;
12169 parser->in_function_body = true;
12172 local_specialization_stack s (lss_copy);
12173 tree fco = lambda_function (lambda_expr);
12174 tree body = start_lambda_function (fco, lambda_expr);
12176 /* Originally C++11 required us to peek for 'return expr'; and
12177 process it specially here to deduce the return type. N3638
12178 removed the need for that. */
12179 cp_parser_function_body (parser, false);
12181 finish_lambda_function (body);
12184 restore_omp_privatization_clauses (omp_privatization_save);
12185 parser->local_variables_forbidden_p = local_variables_forbidden_p;
12186 parser->in_function_body = in_function_body;
12187 if (nested)
12188 pop_function_context();
12189 else
12190 --function_depth;
12193 /* Statements [gram.stmt.stmt] */
12195 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
12197 static void
12198 add_debug_begin_stmt (location_t loc)
12200 if (!MAY_HAVE_DEBUG_MARKER_STMTS)
12201 return;
12202 if (DECL_DECLARED_CONCEPT_P (current_function_decl))
12203 /* A concept is never expanded normally. */
12204 return;
12206 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
12207 SET_EXPR_LOCATION (stmt, loc);
12208 add_stmt (stmt);
12211 struct cp_omp_attribute_data
12213 cp_token_cache *tokens;
12214 const c_omp_directive *dir;
12215 c_omp_directive_kind kind;
12218 /* Handle omp::directive and omp::sequence attributes in ATTRS
12219 (if any) at the start of a statement or in attribute-declaration. */
12221 static tree
12222 cp_parser_handle_statement_omp_attributes (cp_parser *parser, tree attrs)
12224 if (!flag_openmp && !flag_openmp_simd)
12225 return attrs;
12227 auto_vec<cp_omp_attribute_data, 16> vec;
12228 int cnt = 0;
12229 int tokens = 0;
12230 bool bad = false;
12231 for (tree *pa = &attrs; *pa; )
12232 if (get_attribute_namespace (*pa) == omp_identifier
12233 && is_attribute_p ("directive", get_attribute_name (*pa)))
12235 cnt++;
12236 for (tree a = TREE_VALUE (*pa); a; a = TREE_CHAIN (a))
12238 tree d = TREE_VALUE (a);
12239 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
12240 cp_token *first = DEFPARSE_TOKENS (d)->first;
12241 cp_token *last = DEFPARSE_TOKENS (d)->last;
12242 if (parser->omp_attrs_forbidden_p)
12244 error_at (first->location,
12245 "mixing OpenMP directives with attribute and pragma "
12246 "syntax on the same statement");
12247 parser->omp_attrs_forbidden_p = false;
12248 bad = true;
12250 else if (TREE_PUBLIC (d))
12252 error_at (first->location,
12253 "OpenMP %<omp::decl%> attribute on a statement");
12254 bad = true;
12256 const char *directive[3] = {};
12257 for (int i = 0; i < 3; i++)
12259 tree id = NULL_TREE;
12260 if (first + i == last)
12261 break;
12262 if (first[i].type == CPP_NAME)
12263 id = first[i].u.value;
12264 else if (first[i].type == CPP_KEYWORD)
12265 id = ridpointers[(int) first[i].keyword];
12266 else
12267 break;
12268 directive[i] = IDENTIFIER_POINTER (id);
12270 const c_omp_directive *dir = NULL;
12271 if (directive[0])
12272 dir = c_omp_categorize_directive (directive[0], directive[1],
12273 directive[2]);
12274 if (dir == NULL)
12276 error_at (first->location,
12277 "unknown OpenMP directive name in %qs attribute "
12278 "argument",
12279 TREE_PUBLIC (d) ? "omp::decl" : "omp::directive");
12280 continue;
12282 c_omp_directive_kind kind = dir->kind;
12283 if (dir->id == PRAGMA_OMP_ORDERED)
12285 /* ordered is C_OMP_DIR_CONSTRUCT only if it doesn't contain
12286 depend/doacross clause. */
12287 if (directive[1]
12288 && (strcmp (directive[1], "depend") == 0
12289 || strcmp (directive[1], "doacross") == 0))
12290 kind = C_OMP_DIR_STANDALONE;
12291 else if (first + 2 < last
12292 && first[1].type == CPP_COMMA
12293 && first[2].type == CPP_NAME
12294 && (strcmp (IDENTIFIER_POINTER (first[2].u.value),
12295 "depend") == 0
12296 || strcmp (IDENTIFIER_POINTER (first[2].u.value),
12297 "doacross") == 0))
12298 kind = C_OMP_DIR_STANDALONE;
12300 else if (dir->id == PRAGMA_OMP_ERROR)
12302 /* error with at(execution) clause is C_OMP_DIR_STANDALONE. */
12303 int paren_depth = 0;
12304 for (int i = 1; first + i < last; i++)
12305 if (first[i].type == CPP_OPEN_PAREN)
12306 paren_depth++;
12307 else if (first[i].type == CPP_CLOSE_PAREN)
12308 paren_depth--;
12309 else if (paren_depth == 0
12310 && first + i + 2 < last
12311 && first[i].type == CPP_NAME
12312 && first[i + 1].type == CPP_OPEN_PAREN
12313 && first[i + 2].type == CPP_NAME
12314 && !strcmp (IDENTIFIER_POINTER (first[i].u.value),
12315 "at")
12316 && !strcmp (IDENTIFIER_POINTER (first[i
12317 + 2].u.value),
12318 "execution"))
12320 kind = C_OMP_DIR_STANDALONE;
12321 break;
12324 cp_omp_attribute_data v = { DEFPARSE_TOKENS (d), dir, kind };
12325 vec.safe_push (v);
12326 if (flag_openmp || dir->simd)
12327 tokens += (last - first) + 1;
12329 cp_omp_attribute_data v = {};
12330 vec.safe_push (v);
12331 *pa = TREE_CHAIN (*pa);
12333 else
12334 pa = &TREE_CHAIN (*pa);
12336 if (bad)
12337 return attrs;
12339 unsigned int i;
12340 cp_omp_attribute_data *v;
12341 cp_omp_attribute_data *construct_seen = nullptr;
12342 cp_omp_attribute_data *standalone_seen = nullptr;
12343 cp_omp_attribute_data *prev_standalone_seen = nullptr;
12344 FOR_EACH_VEC_ELT (vec, i, v)
12345 if (v->tokens)
12347 if (v->kind == C_OMP_DIR_CONSTRUCT && !construct_seen)
12348 construct_seen = v;
12349 else if (v->kind == C_OMP_DIR_STANDALONE && !standalone_seen)
12350 standalone_seen = v;
12352 else
12354 if (standalone_seen && !prev_standalone_seen)
12356 prev_standalone_seen = standalone_seen;
12357 standalone_seen = nullptr;
12361 if (cnt > 1 && construct_seen)
12363 error_at (construct_seen->tokens->first->location,
12364 "OpenMP construct among %<omp::directive%> attributes"
12365 " requires all %<omp::directive%> attributes on the"
12366 " same statement to be in the same %<omp::sequence%>");
12367 return attrs;
12369 if (cnt > 1 && standalone_seen && prev_standalone_seen)
12371 error_at (standalone_seen->tokens->first->location,
12372 "multiple OpenMP standalone directives among"
12373 " %<omp::directive%> attributes must be all within the"
12374 " same %<omp::sequence%>");
12375 return attrs;
12378 if (prev_standalone_seen)
12379 standalone_seen = prev_standalone_seen;
12380 if (standalone_seen
12381 && !cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12383 error_at (standalone_seen->tokens->first->location,
12384 "standalone OpenMP directives in %<omp::directive%> attribute"
12385 " can only appear on an empty statement");
12386 return attrs;
12388 if (cnt && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
12390 cp_token *token = cp_lexer_peek_token (parser->lexer);
12391 enum pragma_kind kind = cp_parser_pragma_kind (token);
12392 if (kind >= PRAGMA_OMP__START_ && kind <= PRAGMA_OMP__LAST_)
12394 error_at (token->location,
12395 "mixing OpenMP directives with attribute and pragma "
12396 "syntax on the same statement");
12397 return attrs;
12401 if (!tokens)
12402 return attrs;
12403 tokens++;
12404 cp_lexer *lexer = cp_lexer_alloc ();
12405 lexer->debugging_p = parser->lexer->debugging_p;
12406 vec_safe_reserve (lexer->buffer, tokens, true);
12407 FOR_EACH_VEC_ELT (vec, i, v)
12409 if (!v->tokens)
12410 continue;
12411 if (!flag_openmp && !v->dir->simd)
12412 continue;
12413 cp_token *first = v->tokens->first;
12414 cp_token *last = v->tokens->last;
12415 cp_token tok = {};
12416 tok.type = CPP_PRAGMA;
12417 tok.keyword = RID_MAX;
12418 tok.u.value = build_int_cst (NULL, v->dir->id);
12419 tok.location = first->location;
12420 lexer->buffer->quick_push (tok);
12421 while (++first < last)
12422 lexer->buffer->quick_push (*first);
12423 tok = {};
12424 tok.type = CPP_PRAGMA_EOL;
12425 tok.keyword = RID_MAX;
12426 tok.location = last->location;
12427 lexer->buffer->quick_push (tok);
12429 cp_token tok = {};
12430 tok.type = CPP_EOF;
12431 tok.keyword = RID_MAX;
12432 tok.location = lexer->buffer->last ().location;
12433 lexer->buffer->quick_push (tok);
12434 lexer->next = parser->lexer;
12435 lexer->next_token = lexer->buffer->address ();
12436 lexer->last_token = lexer->next_token
12437 + lexer->buffer->length ()
12438 - 1;
12439 lexer->in_omp_attribute_pragma = true;
12440 parser->lexer = lexer;
12441 /* Move the current source position to that of the first token in the
12442 new lexer. */
12443 cp_lexer_set_source_position_from_token (lexer->next_token);
12444 return attrs;
12447 /* True if and only if the name is one of the contract types. */
12449 static bool
12450 contract_attribute_p (const_tree id)
12452 return is_attribute_p ("assert", id)
12453 || is_attribute_p ("pre", id)
12454 || is_attribute_p ("post", id);
12457 /* Handle omp::directive and omp::sequence attributes in *PATTRS
12458 (if any) at the start or after declaration-id of a declaration. */
12460 static void
12461 cp_parser_handle_directive_omp_attributes (cp_parser *parser, tree *pattrs,
12462 cp_omp_declare_simd_data *data,
12463 bool start)
12465 if (!flag_openmp && !flag_openmp_simd)
12466 return;
12468 int cnt = 0;
12469 bool bad = false;
12470 bool variant_p = false;
12471 location_t loc = UNKNOWN_LOCATION;
12472 for (tree pa = *pattrs; pa; pa = TREE_CHAIN (pa))
12473 if (get_attribute_namespace (pa) == omp_identifier
12474 && is_attribute_p ("directive", get_attribute_name (pa)))
12476 for (tree a = TREE_VALUE (pa); a; a = TREE_CHAIN (a))
12478 tree d = TREE_VALUE (a);
12479 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
12480 cp_token *first = DEFPARSE_TOKENS (d)->first;
12481 cp_token *last = DEFPARSE_TOKENS (d)->last;
12482 const char *directive[3] = {};
12483 for (int i = 0; i < 3; i++)
12485 tree id = NULL_TREE;
12486 if (first + i == last)
12487 break;
12488 if (first[i].type == CPP_NAME)
12489 id = first[i].u.value;
12490 else if (first[i].type == CPP_KEYWORD)
12491 id = ridpointers[(int) first[i].keyword];
12492 else
12493 break;
12494 directive[i] = IDENTIFIER_POINTER (id);
12496 const c_omp_directive *dir = NULL;
12497 if (directive[0])
12498 dir = c_omp_categorize_directive (directive[0], directive[1],
12499 directive[2]);
12500 if (dir == NULL)
12501 continue;
12502 if (dir->id == PRAGMA_OMP_DECLARE
12503 && (strcmp (directive[1], "simd") == 0
12504 || strcmp (directive[1], "variant") == 0))
12506 if (cnt++ == 0)
12508 variant_p = strcmp (directive[1], "variant") == 0;
12509 loc = first->location;
12511 if (start && parser->omp_declare_simd && !bad)
12513 error_at (first->location,
12514 "mixing OpenMP directives with attribute and "
12515 "pragma syntax on the same declaration");
12516 bad = true;
12522 if (bad)
12524 for (tree *pa = pattrs; *pa; )
12525 if (get_attribute_namespace (*pa) == omp_identifier
12526 && is_attribute_p ("directive", get_attribute_name (*pa)))
12527 *pa = TREE_CHAIN (*pa);
12528 else
12529 pa = &TREE_CHAIN (*pa);
12530 return;
12532 if (cnt == 0)
12533 return;
12535 if (parser->omp_declare_simd == NULL)
12537 data->error_seen = false;
12538 data->fndecl_seen = false;
12539 data->variant_p = variant_p;
12540 data->loc = loc;
12541 data->tokens = vNULL;
12542 data->attribs[0] = NULL;
12543 data->attribs[1] = NULL;
12544 parser->omp_declare_simd = data;
12546 parser->omp_declare_simd->attribs[!start] = pattrs;
12549 /* Parse a statement.
12551 statement:
12552 labeled-statement
12553 expression-statement
12554 compound-statement
12555 selection-statement
12556 iteration-statement
12557 jump-statement
12558 declaration-statement
12559 try-block
12561 C++11:
12563 statement:
12564 labeled-statement
12565 attribute-specifier-seq (opt) expression-statement
12566 attribute-specifier-seq (opt) compound-statement
12567 attribute-specifier-seq (opt) selection-statement
12568 attribute-specifier-seq (opt) iteration-statement
12569 attribute-specifier-seq (opt) jump-statement
12570 declaration-statement
12571 attribute-specifier-seq (opt) try-block
12573 init-statement:
12574 expression-statement
12575 simple-declaration
12576 alias-declaration
12578 TM Extension:
12580 statement:
12581 atomic-statement
12583 IN_COMPOUND is true when the statement is nested inside a
12584 cp_parser_compound_statement.
12586 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12587 is a (possibly labeled) if statement which is not enclosed in braces
12588 and has an else clause. This is used to implement -Wparentheses.
12590 CHAIN is a vector of if-else-if conditions.
12592 Note that this version of parsing restricts assertions to be attached to
12593 empty statements. */
12595 static void
12596 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
12597 const bool in_compound, bool *if_p, vec<tree> *chain,
12598 location_t *loc_after_labels)
12600 tree statement, std_attrs = NULL_TREE;
12601 cp_token *token;
12602 location_t statement_location, attrs_loc;
12603 bool in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
12604 bool has_std_attrs;
12605 /* A copy of IN_COMPOUND which is set to false after seeing a label.
12606 This matters for certain pragmas. */
12607 bool in_compound_for_pragma = in_compound;
12609 restart:
12610 if (if_p != NULL)
12611 *if_p = false;
12612 /* There is no statement yet. */
12613 statement = NULL_TREE;
12615 saved_token_sentinel saved_tokens (parser->lexer);
12616 token = cp_lexer_peek_token (parser->lexer);
12617 attrs_loc = token->location;
12618 if (c_dialect_objc ())
12619 /* In obj-c++, seeing '[[' might be the either the beginning of
12620 c++11 attributes, or a nested objc-message-expression. So
12621 let's parse the c++11 attributes tentatively. */
12622 cp_parser_parse_tentatively (parser);
12623 std_attrs = cp_parser_std_attribute_spec_seq (parser);
12624 if (std_attrs)
12625 attrs_loc = make_location (attrs_loc, attrs_loc, parser->lexer);
12626 if (c_dialect_objc ())
12628 if (!cp_parser_parse_definitely (parser))
12629 std_attrs = NULL_TREE;
12631 has_std_attrs = cp_lexer_peek_token (parser->lexer) != token;
12633 /* Peek at the next token. */
12634 token = cp_lexer_peek_token (parser->lexer);
12636 /* If we have contracts, check that they're valid in this context. */
12637 if (std_attrs != error_mark_node)
12639 if (tree pre = lookup_attribute ("pre", std_attrs))
12640 error_at (EXPR_LOCATION (TREE_VALUE (pre)),
12641 "preconditions cannot be statements");
12642 else if (tree post = lookup_attribute ("post", std_attrs))
12643 error_at (EXPR_LOCATION (TREE_VALUE (post)),
12644 "postconditions cannot be statements");
12646 /* Check that assertions are null statements. */
12647 if (cp_contract_assertion_p (std_attrs))
12648 if (token->type != CPP_SEMICOLON)
12649 error_at (token->location, "assertions must be followed by %<;%>");
12652 bool omp_attrs_forbidden_p;
12653 omp_attrs_forbidden_p = parser->omp_attrs_forbidden_p;
12655 if (std_attrs && (flag_openmp || flag_openmp_simd))
12657 bool handle_omp_attribs = false;
12658 if (token->type == CPP_KEYWORD)
12659 switch (token->keyword)
12661 case RID_IF:
12662 case RID_SWITCH:
12663 case RID_WHILE:
12664 case RID_DO:
12665 case RID_FOR:
12666 case RID_BREAK:
12667 case RID_CONTINUE:
12668 case RID_RETURN:
12669 case RID_CO_RETURN:
12670 case RID_GOTO:
12671 case RID_AT_TRY:
12672 case RID_AT_CATCH:
12673 case RID_AT_FINALLY:
12674 case RID_AT_SYNCHRONIZED:
12675 case RID_AT_THROW:
12676 case RID_TRY:
12677 case RID_TRANSACTION_ATOMIC:
12678 case RID_TRANSACTION_RELAXED:
12679 case RID_SYNCHRONIZED:
12680 case RID_ATOMIC_NOEXCEPT:
12681 case RID_ATOMIC_CANCEL:
12682 case RID_TRANSACTION_CANCEL:
12683 handle_omp_attribs = true;
12684 break;
12685 default:
12686 break;
12688 else if (token->type == CPP_SEMICOLON
12689 || token->type == CPP_OPEN_BRACE
12690 || token->type == CPP_PRAGMA)
12691 handle_omp_attribs = true;
12692 if (handle_omp_attribs)
12694 std_attrs = cp_parser_handle_statement_omp_attributes (parser,
12695 std_attrs);
12696 token = cp_lexer_peek_token (parser->lexer);
12699 parser->omp_attrs_forbidden_p = false;
12701 /* Remember the location of the first token in the statement. */
12702 cp_token *statement_token = token;
12703 statement_location = token->location;
12704 add_debug_begin_stmt (statement_location);
12705 /* If this is a keyword, then that will often determine what kind of
12706 statement we have. */
12707 if (token->type == CPP_KEYWORD)
12709 enum rid keyword = token->keyword;
12711 switch (keyword)
12713 case RID_CASE:
12714 case RID_DEFAULT:
12715 /* Looks like a labeled-statement with a case label.
12716 Parse the label, and then use tail recursion to parse
12717 the statement. */
12718 cp_parser_label_for_labeled_statement (parser, std_attrs);
12719 in_compound_for_pragma = false;
12720 in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
12721 goto restart;
12723 case RID_IF:
12724 case RID_SWITCH:
12725 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12726 statement = cp_parser_selection_statement (parser, if_p, chain);
12727 break;
12729 case RID_WHILE:
12730 case RID_DO:
12731 case RID_FOR:
12732 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12733 statement = cp_parser_iteration_statement (parser, if_p, false,
12734 NULL_TREE, false);
12735 break;
12737 case RID_BREAK:
12738 case RID_CONTINUE:
12739 case RID_RETURN:
12740 case RID_CO_RETURN:
12741 case RID_GOTO:
12742 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12743 statement = cp_parser_jump_statement (parser);
12744 break;
12746 /* Objective-C++ exception-handling constructs. */
12747 case RID_AT_TRY:
12748 case RID_AT_CATCH:
12749 case RID_AT_FINALLY:
12750 case RID_AT_SYNCHRONIZED:
12751 case RID_AT_THROW:
12752 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12753 statement = cp_parser_objc_statement (parser);
12754 break;
12756 case RID_TRY:
12757 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12758 statement = cp_parser_try_block (parser);
12759 break;
12761 case RID_NAMESPACE:
12762 /* This must be a namespace alias definition. */
12763 if (has_std_attrs)
12765 /* Attributes should be parsed as part of the
12766 declaration, so let's un-parse them. */
12767 saved_tokens.rollback();
12768 std_attrs = NULL_TREE;
12770 cp_parser_declaration_statement (parser);
12771 return;
12773 case RID_TRANSACTION_ATOMIC:
12774 case RID_TRANSACTION_RELAXED:
12775 case RID_SYNCHRONIZED:
12776 case RID_ATOMIC_NOEXCEPT:
12777 case RID_ATOMIC_CANCEL:
12778 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12779 statement = cp_parser_transaction (parser, token);
12780 break;
12781 case RID_TRANSACTION_CANCEL:
12782 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12783 statement = cp_parser_transaction_cancel (parser);
12784 break;
12786 default:
12787 /* It might be a keyword like `int' that can start a
12788 declaration-statement. */
12789 break;
12792 else if (token->type == CPP_NAME)
12794 /* If the next token is a `:', then we are looking at a
12795 labeled-statement. */
12796 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12797 if (token->type == CPP_COLON)
12799 /* Looks like a labeled-statement with an ordinary label.
12800 Parse the label, and then use tail recursion to parse
12801 the statement. */
12803 cp_parser_label_for_labeled_statement (parser, std_attrs);
12805 /* If there's no statement, it's not a labeled-statement, just
12806 a label. That's allowed in C++23, but only if we're at the
12807 end of a compound-statement. */
12808 if (in_compound
12809 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12811 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
12812 if (cxx_dialect < cxx23)
12813 pedwarn (loc, OPT_Wc__23_extensions,
12814 "label at end of compound statement only available "
12815 "with %<-std=c++2b%> or %<-std=gnu++2b%>");
12816 return;
12818 in_compound_for_pragma = false;
12819 in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
12820 goto restart;
12823 /* Anything that starts with a `{' must be a compound-statement. */
12824 else if (token->type == CPP_OPEN_BRACE)
12826 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12827 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12829 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
12830 a statement all its own. */
12831 else if (token->type == CPP_PRAGMA)
12833 do_pragma:;
12834 cp_lexer *lexer = parser->lexer;
12835 bool do_restart = false;
12836 /* Only certain OpenMP pragmas are attached to statements, and thus
12837 are considered statements themselves. All others are not. In
12838 the context of a compound, accept the pragma as a "statement" and
12839 return so that we can check for a close brace. Otherwise we
12840 require a real statement and must go back and read one. */
12841 if (in_compound_for_pragma)
12843 if (cp_parser_pragma (parser, pragma_compound, if_p)
12844 && parser->omp_for_parse_state)
12845 check_omp_intervening_code (parser);
12847 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
12848 do_restart = true;
12849 else if (parser->omp_for_parse_state)
12850 check_omp_intervening_code (parser);
12851 if (parser->lexer != lexer
12852 && lexer->in_omp_attribute_pragma
12853 && (!in_omp_attribute_pragma || lexer->orphan_p))
12855 if (saved_tokens.lexer == lexer)
12857 if (saved_tokens.mode == STS_COMMIT)
12858 cp_lexer_commit_tokens (lexer);
12859 gcc_assert (lexer->saved_tokens.length () == saved_tokens.len);
12860 saved_tokens.lexer = parser->lexer;
12861 saved_tokens.mode = STS_DONOTHING;
12862 saved_tokens.len = parser->lexer->saved_tokens.length ();
12864 cp_lexer_destroy (lexer);
12865 lexer = parser->lexer;
12867 if (do_restart)
12868 goto restart;
12869 if (parser->lexer == lexer
12870 && lexer->in_omp_attribute_pragma
12871 && !in_omp_attribute_pragma)
12872 parser->lexer->orphan_p = true;
12873 return;
12875 else if (token->type == CPP_EOF)
12877 cp_parser_error (parser, "expected statement");
12878 return;
12881 /* Everything else must be a declaration-statement or an
12882 expression-statement. Try for the declaration-statement
12883 first, unless we are looking at a `;', in which case we know that
12884 we have an expression-statement. */
12885 if (!statement)
12887 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12889 if (has_std_attrs)
12890 /* Attributes should be parsed as part of the declaration,
12891 so let's un-parse them. */
12892 saved_tokens.rollback();
12894 parser->omp_attrs_forbidden_p = omp_attrs_forbidden_p;
12895 cp_parser_parse_tentatively (parser);
12896 /* Try to parse the declaration-statement. */
12897 cp_parser_declaration_statement (parser);
12898 parser->omp_attrs_forbidden_p = false;
12899 /* If that worked, we're done. */
12900 if (cp_parser_parse_definitely (parser))
12901 return;
12902 /* It didn't work, restore the post-attribute position. */
12903 if (has_std_attrs)
12905 cp_lexer_set_token_position (parser->lexer, statement_token);
12906 if (flag_openmp || flag_openmp_simd)
12908 size_t i = 1;
12909 bool handle_omp_attribs = true;
12910 while (cp_lexer_peek_nth_token (parser->lexer, i)->keyword
12911 == RID_EXTENSION)
12912 i++;
12913 switch (cp_lexer_peek_nth_token (parser->lexer, i)->keyword)
12915 case RID_ASM:
12916 case RID_NAMESPACE:
12917 case RID_USING:
12918 case RID_LABEL:
12919 case RID_STATIC_ASSERT:
12920 /* Don't handle OpenMP attribs on keywords that
12921 always start a declaration statement but don't
12922 accept attribute before it and therefore
12923 the tentative cp_parser_declaration_statement
12924 fails to parse because of that. */
12925 handle_omp_attribs = false;
12926 break;
12927 default:
12928 break;
12931 if (handle_omp_attribs)
12933 parser->omp_attrs_forbidden_p = omp_attrs_forbidden_p;
12934 std_attrs
12935 = cp_parser_handle_statement_omp_attributes
12936 (parser, std_attrs);
12937 parser->omp_attrs_forbidden_p = false;
12938 token = cp_lexer_peek_token (parser->lexer);
12939 if (token->type == CPP_PRAGMA)
12940 goto do_pragma;
12945 /* All preceding labels have been parsed at this point. */
12946 if (loc_after_labels != NULL)
12947 *loc_after_labels = statement_location;
12949 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12951 /* Look for an expression-statement instead. */
12952 statement = cp_parser_expression_statement (parser, in_statement_expr);
12954 std_attrs = process_stmt_assume_attribute (std_attrs, statement,
12955 attrs_loc);
12957 /* Handle [[fallthrough]];. */
12958 if (attribute_fallthrough_p (std_attrs))
12960 /* The next token after the fallthrough attribute is ';'. */
12961 if (statement == NULL_TREE)
12963 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
12964 statement = build_call_expr_internal_loc (statement_location,
12965 IFN_FALLTHROUGH,
12966 void_type_node, 0);
12967 finish_expr_stmt (statement);
12969 else
12970 warning_at (statement_location, OPT_Wattributes,
12971 "%<fallthrough%> attribute not followed by %<;%>");
12972 std_attrs = NULL_TREE;
12975 /* Handle [[assert: ...]]; */
12976 if (cp_contract_assertion_p (std_attrs))
12978 /* Add the assertion as a statement in the current block. */
12979 gcc_assert (!statement || statement == error_mark_node);
12980 emit_assertion (std_attrs);
12981 std_attrs = NULL_TREE;
12985 /* Set the line number for the statement. */
12986 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
12987 SET_EXPR_LOCATION (statement, statement_location);
12989 /* Allow "[[fallthrough]];" or "[[assume(cond)]];", but warn otherwise. */
12990 if (std_attrs != NULL_TREE && any_nonignored_attribute_p (std_attrs))
12991 warning_at (attrs_loc, OPT_Wattributes,
12992 "attributes at the beginning of statement are ignored");
12995 /* Append ATTR to attribute list ATTRS. */
12997 tree
12998 attr_chainon (tree attrs, tree attr)
13000 if (attrs == error_mark_node)
13001 return error_mark_node;
13002 if (attr == error_mark_node)
13003 return error_mark_node;
13004 return chainon (attrs, attr);
13007 /* Parse the label for a labeled-statement, i.e.
13009 label:
13010 attribute-specifier-seq[opt] identifier :
13011 attribute-specifier-seq[opt] case constant-expression :
13012 attribute-specifier-seq[opt] default :
13014 labeled-statement:
13015 label statement
13017 GNU Extension:
13018 case constant-expression ... constant-expression : statement
13020 When a label is parsed without errors, the label is added to the
13021 parse tree by the finish_* functions, so this function doesn't
13022 have to return the label. */
13024 static void
13025 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
13027 cp_token *token;
13028 tree label = NULL_TREE;
13029 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
13031 /* The next token should be an identifier. */
13032 token = cp_lexer_peek_token (parser->lexer);
13033 if (token->type != CPP_NAME
13034 && token->type != CPP_KEYWORD)
13036 cp_parser_error (parser, "expected labeled-statement");
13037 return;
13040 /* Remember whether this case or a user-defined label is allowed to fall
13041 through to. */
13042 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
13044 parser->colon_corrects_to_scope_p = false;
13045 switch (token->keyword)
13047 case RID_CASE:
13049 tree expr, expr_hi;
13050 cp_token *ellipsis;
13052 /* Consume the `case' token. */
13053 cp_lexer_consume_token (parser->lexer);
13054 /* Parse the constant-expression. */
13055 expr = cp_parser_constant_expression (parser);
13056 if (check_for_bare_parameter_packs (expr))
13057 expr = error_mark_node;
13059 ellipsis = cp_lexer_peek_token (parser->lexer);
13060 if (ellipsis->type == CPP_ELLIPSIS)
13062 /* Consume the `...' token. */
13063 cp_lexer_consume_token (parser->lexer);
13064 expr_hi = cp_parser_constant_expression (parser);
13065 if (check_for_bare_parameter_packs (expr_hi))
13066 expr_hi = error_mark_node;
13068 /* We don't need to emit warnings here, as the common code
13069 will do this for us. */
13071 else
13072 expr_hi = NULL_TREE;
13074 if (parser->in_switch_statement_p)
13076 tree l = finish_case_label (token->location, expr, expr_hi);
13077 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
13079 label = CASE_LABEL (l);
13080 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
13083 else
13084 error_at (token->location,
13085 "case label %qE not within a switch statement",
13086 expr);
13088 break;
13090 case RID_DEFAULT:
13091 /* Consume the `default' token. */
13092 cp_lexer_consume_token (parser->lexer);
13094 if (parser->in_switch_statement_p)
13096 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
13097 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
13099 label = CASE_LABEL (l);
13100 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
13103 else
13104 error_at (token->location, "case label not within a switch statement");
13105 break;
13107 default:
13108 /* Anything else must be an ordinary label. */
13109 label = finish_label_stmt (cp_parser_identifier (parser));
13110 if (label && TREE_CODE (label) == LABEL_DECL)
13111 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
13112 break;
13115 /* Require the `:' token. */
13116 cp_parser_require (parser, CPP_COLON, RT_COLON);
13118 /* An ordinary label may optionally be followed by attributes.
13119 However, this is only permitted if the attributes are then
13120 followed by a semicolon. This is because, for backward
13121 compatibility, when parsing
13122 lab: __attribute__ ((unused)) int i;
13123 we want the attribute to attach to "i", not "lab". */
13124 if (label != NULL_TREE
13125 && cp_next_tokens_can_be_gnu_attribute_p (parser))
13127 tree attrs;
13128 cp_parser_parse_tentatively (parser);
13129 attrs = cp_parser_gnu_attributes_opt (parser);
13130 if (attrs == NULL_TREE
13131 /* And fallthrough always binds to the expression-statement. */
13132 || attribute_fallthrough_p (attrs)
13133 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13134 cp_parser_abort_tentative_parse (parser);
13135 else if (!cp_parser_parse_definitely (parser))
13137 else
13138 attributes = attr_chainon (attributes, attrs);
13141 if (attributes != NULL_TREE)
13142 cplus_decl_attributes (&label, attributes, 0);
13144 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
13147 /* Parse an expression-statement.
13149 expression-statement:
13150 expression [opt] ;
13152 Returns the new EXPR_STMT -- or NULL_TREE if the expression
13153 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
13154 indicates whether this expression-statement is part of an
13155 expression statement. */
13157 static tree
13158 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
13160 tree statement = NULL_TREE;
13161 cp_token *token = cp_lexer_peek_token (parser->lexer);
13162 location_t loc = token->location;
13164 /* There might be attribute fallthrough. */
13165 tree attr = cp_parser_gnu_attributes_opt (parser);
13167 /* If the next token is a ';', then there is no expression
13168 statement. */
13169 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13171 statement = cp_parser_expression (parser);
13172 if (statement == error_mark_node
13173 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
13175 /* If we ran into a problem, make sure we complained. */
13176 gcc_assert (seen_error ());
13178 cp_parser_skip_to_end_of_block_or_statement (parser);
13179 return error_mark_node;
13183 attr = process_stmt_assume_attribute (attr, statement, loc);
13185 /* Handle [[fallthrough]];. */
13186 if (attribute_fallthrough_p (attr))
13188 /* The next token after the fallthrough attribute is ';'. */
13189 if (statement == NULL_TREE)
13190 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
13191 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
13192 void_type_node, 0);
13193 else
13194 warning_at (loc, OPT_Wattributes,
13195 "%<fallthrough%> attribute not followed by %<;%>");
13196 attr = NULL_TREE;
13199 /* Allow "[[fallthrough]];", but warn otherwise. */
13200 if (attr != NULL_TREE && any_nonignored_attribute_p (attr))
13201 warning_at (loc, OPT_Wattributes,
13202 "attributes at the beginning of statement are ignored");
13204 /* Give a helpful message for "A<T>::type t;" and the like. */
13205 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
13206 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
13208 if (TREE_CODE (statement) == SCOPE_REF)
13209 error_at (token->location, "need %<typename%> before %qE because "
13210 "%qT is a dependent scope",
13211 statement, TREE_OPERAND (statement, 0));
13212 else if (is_overloaded_fn (statement)
13213 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
13215 /* A::A a; */
13216 tree fn = get_first_fn (statement);
13217 error_at (token->location,
13218 "%<%T::%D%> names the constructor, not the type",
13219 DECL_CONTEXT (fn), DECL_NAME (fn));
13223 /* Consume the final `;'. */
13224 cp_parser_consume_semicolon_at_end_of_statement (parser);
13226 if (in_statement_expr
13227 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
13228 /* This is the final expression statement of a statement
13229 expression. */
13230 statement = finish_stmt_expr_expr (statement, in_statement_expr);
13231 else if (statement)
13232 statement = finish_expr_stmt (statement);
13234 return statement;
13237 /* Parse a compound-statement.
13239 compound-statement:
13240 { statement-seq [opt] label-seq [opt] }
13242 label-seq:
13243 label
13244 label-seq label
13246 GNU extension:
13248 compound-statement:
13249 { label-declaration-seq [opt] statement-seq [opt] }
13251 label-declaration-seq:
13252 label-declaration
13253 label-declaration-seq label-declaration
13255 Returns a tree representing the statement. */
13257 static tree
13258 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
13259 int bcs_flags, bool function_body)
13261 tree compound_stmt;
13262 matching_braces braces;
13264 /* Consume the `{'. */
13265 if (!braces.require_open (parser))
13266 return error_mark_node;
13267 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
13268 && !function_body && cxx_dialect < cxx14)
13269 pedwarn (input_location, OPT_Wpedantic,
13270 "compound-statement in %<constexpr%> function");
13271 /* Begin the compound-statement. */
13272 compound_stmt = begin_compound_stmt (bcs_flags);
13273 /* If the next keyword is `__label__' we have a label declaration. */
13274 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
13275 cp_parser_label_declaration (parser);
13276 /* Parse an (optional) statement-seq. */
13277 cp_parser_statement_seq_opt (parser, in_statement_expr);
13279 /* Consume the `}'. */
13280 braces.require_close (parser);
13282 /* Finish the compound-statement. */
13283 finish_compound_stmt (compound_stmt);
13285 return compound_stmt;
13288 /* Diagnose errors related to imperfectly nested loops in an OMP
13289 loop construct. This function is called when such code is seen.
13290 Only issue one such diagnostic no matter how much invalid
13291 intervening code there is in the loop.
13292 FIXME: maybe the location associated with the diagnostic should
13293 be the current parser token instead of the location of the outer loop
13294 nest. */
13296 static void
13297 check_omp_intervening_code (cp_parser *parser)
13299 struct omp_for_parse_data *omp_for_parse_state
13300 = parser->omp_for_parse_state;
13301 gcc_assert (omp_for_parse_state);
13303 if (!omp_for_parse_state->in_intervening_code)
13304 return;
13305 omp_for_parse_state->saw_intervening_code = true;
13307 /* Only diagnose errors related to perfect nesting once. */
13308 if (!omp_for_parse_state->perfect_nesting_fail)
13310 if (omp_for_parse_state->code == OACC_LOOP)
13312 error_at (omp_for_parse_state->for_loc,
13313 "inner loops must be perfectly nested in "
13314 "%<#pragma acc loop%>");
13315 omp_for_parse_state->perfect_nesting_fail = true;
13317 else if (omp_for_parse_state->ordered)
13319 error_at (omp_for_parse_state->for_loc,
13320 "inner loops must be perfectly nested with "
13321 "%<ordered%> clause");
13322 omp_for_parse_state->perfect_nesting_fail = true;
13324 else if (omp_for_parse_state->inscan)
13326 error_at (omp_for_parse_state->for_loc,
13327 "inner loops must be perfectly nested with "
13328 "%<reduction%> %<inscan%> clause");
13329 omp_for_parse_state->perfect_nesting_fail = true;
13331 /* TODO: Also reject loops with TILE directive. */
13332 if (omp_for_parse_state->perfect_nesting_fail)
13333 omp_for_parse_state->fail = true;
13337 /* Parse an (optional) statement-seq.
13339 statement-seq:
13340 statement
13341 statement-seq [opt] statement */
13343 static void
13344 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
13346 struct omp_for_parse_data *omp_for_parse_state
13347 = parser->omp_for_parse_state;
13348 bool in_omp_loop_block
13349 = omp_for_parse_state ? omp_for_parse_state->want_nested_loop : false;
13351 /* Scan statements until there aren't any more. */
13352 while (true)
13354 cp_token *token = cp_lexer_peek_token (parser->lexer);
13356 /* If we are looking at a `}', then we have run out of
13357 statements; the same is true if we have reached the end
13358 of file, or have stumbled upon a stray '@end'. */
13359 if (token->type == CPP_CLOSE_BRACE
13360 || token->type == CPP_EOF
13361 || token->type == CPP_PRAGMA_EOL
13362 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
13363 break;
13365 /* If we are in a compound statement and find 'else' then
13366 something went wrong. */
13367 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
13369 if (parser->in_statement & IN_IF_STMT)
13370 break;
13371 else
13373 token = cp_lexer_consume_token (parser->lexer);
13374 error_at (token->location, "%<else%> without a previous %<if%>");
13378 /* Handle special cases for OMP FOR canonical loop syntax. */
13379 else if (in_omp_loop_block)
13381 bool want_nested_loop = omp_for_parse_state->want_nested_loop;
13382 if (want_nested_loop
13383 && token->type == CPP_KEYWORD && token->keyword == RID_FOR)
13385 /* Found the nested loop. */
13386 omp_for_parse_state->depth++;
13387 add_stmt (cp_parser_omp_loop_nest (parser, NULL));
13388 omp_for_parse_state->depth--;
13390 else if (token->type == CPP_SEMICOLON)
13392 /* Prior to implementing the OpenMP 5.1 syntax for canonical
13393 loop form, GCC used to accept an empty statements as not
13394 being intervening code. Continue to do that, as an
13395 extension. */
13396 /* FIXME: Maybe issue a warning or something here? */
13397 cp_lexer_consume_token (parser->lexer);
13399 else if (want_nested_loop && token->type == CPP_OPEN_BRACE)
13400 /* The nested compound statement may contain the next loop, or
13401 it might just be intervening code. */
13403 cp_parser_statement (parser, in_statement_expr, true, NULL);
13404 if (omp_for_parse_state->want_nested_loop)
13405 check_omp_intervening_code (parser);
13407 else
13409 /* This must be intervening code. */
13410 omp_for_parse_state->want_nested_loop = false;
13411 /* Defer calling check_omp_intervening_code on pragmas until
13412 cp_parser_statement, because we can't know until we parse
13413 it whether or not the pragma is a statement. */
13414 if (token->type != CPP_PRAGMA)
13415 check_omp_intervening_code (parser);
13416 cp_parser_statement (parser, in_statement_expr, true, NULL);
13417 omp_for_parse_state->want_nested_loop = want_nested_loop;
13419 continue;
13422 /* Parse the statement. */
13423 cp_parser_statement (parser, in_statement_expr, true, NULL);
13427 /* Return true if this is the C++20 version of range-based-for with
13428 init-statement. */
13430 static bool
13431 cp_parser_range_based_for_with_init_p (cp_parser *parser)
13433 bool r = false;
13435 /* Save tokens so that we can put them back. */
13436 cp_lexer_save_tokens (parser->lexer);
13438 /* There has to be an unnested ; followed by an unnested :. */
13439 if (cp_parser_skip_to_closing_parenthesis_1 (parser,
13440 /*recovering=*/false,
13441 CPP_SEMICOLON,
13442 /*consume_paren=*/false) != -1)
13443 goto out;
13445 /* We found the semicolon, eat it now. */
13446 cp_lexer_consume_token (parser->lexer);
13448 /* Now look for ':' that is not nested in () or {}. */
13449 r = (cp_parser_skip_to_closing_parenthesis_1 (parser,
13450 /*recovering=*/false,
13451 CPP_COLON,
13452 /*consume_paren=*/false) == -1);
13454 out:
13455 /* Roll back the tokens we skipped. */
13456 cp_lexer_rollback_tokens (parser->lexer);
13458 return r;
13461 /* Return true if we're looking at (init; cond), false otherwise. */
13463 static bool
13464 cp_parser_init_statement_p (cp_parser *parser)
13466 /* Save tokens so that we can put them back. */
13467 cp_lexer_save_tokens (parser->lexer);
13469 /* Look for ';' that is not nested in () or {}. */
13470 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
13471 /*recovering=*/false,
13472 CPP_SEMICOLON,
13473 /*consume_paren=*/false);
13475 /* Roll back the tokens we skipped. */
13476 cp_lexer_rollback_tokens (parser->lexer);
13478 return ret == -1;
13481 /* Parse a selection-statement.
13483 selection-statement:
13484 if ( init-statement [opt] condition ) statement
13485 if ( init-statement [opt] condition ) statement else statement
13486 switch ( init-statement [opt] condition ) statement
13488 Returns the new IF_STMT or SWITCH_STMT.
13490 If IF_P is not NULL, *IF_P is set to indicate whether the statement
13491 is a (possibly labeled) if statement which is not enclosed in
13492 braces and has an else clause. This is used to implement
13493 -Wparentheses.
13495 CHAIN is a vector of if-else-if conditions. This is used to implement
13496 -Wduplicated-cond. */
13498 static tree
13499 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
13500 vec<tree> *chain)
13502 cp_token *token;
13503 enum rid keyword;
13504 token_indent_info guard_tinfo;
13506 if (if_p != NULL)
13507 *if_p = false;
13509 /* Peek at the next token. */
13510 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
13511 guard_tinfo = get_token_indent_info (token);
13513 /* See what kind of keyword it is. */
13514 keyword = token->keyword;
13515 switch (keyword)
13517 case RID_IF:
13518 case RID_SWITCH:
13520 tree statement;
13521 tree condition;
13523 bool cx = false;
13524 if (keyword == RID_IF
13525 && cp_lexer_next_token_is_keyword (parser->lexer,
13526 RID_CONSTEXPR))
13528 cx = true;
13529 cp_token *tok = cp_lexer_consume_token (parser->lexer);
13530 if (cxx_dialect < cxx17)
13531 pedwarn (tok->location, OPT_Wc__17_extensions,
13532 "%<if constexpr%> only available with "
13533 "%<-std=c++17%> or %<-std=gnu++17%>");
13535 int ce = 0;
13536 if (keyword == RID_IF && !cx)
13538 if (cp_lexer_next_token_is_keyword (parser->lexer,
13539 RID_CONSTEVAL))
13540 ce = 1;
13541 else if (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
13542 && cp_lexer_nth_token_is_keyword (parser->lexer, 2,
13543 RID_CONSTEVAL))
13545 ce = -1;
13546 cp_lexer_consume_token (parser->lexer);
13549 if (ce)
13551 cp_token *tok = cp_lexer_consume_token (parser->lexer);
13552 if (cxx_dialect < cxx23)
13553 pedwarn (tok->location, OPT_Wc__23_extensions,
13554 "%<if consteval%> only available with "
13555 "%<-std=c++2b%> or %<-std=gnu++2b%>");
13557 bool save_in_consteval_if_p = in_consteval_if_p;
13558 statement = begin_if_stmt ();
13559 IF_STMT_CONSTEVAL_P (statement) = true;
13560 condition = finish_if_stmt_cond (boolean_false_node, statement);
13562 gcc_rich_location richloc (tok->location);
13563 bool non_compound_stmt_p = false;
13564 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13566 non_compound_stmt_p = true;
13567 richloc.add_fixit_insert_after (tok->location, "{");
13570 in_consteval_if_p |= ce > 0;
13571 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
13573 if (non_compound_stmt_p)
13575 location_t before_loc
13576 = cp_lexer_peek_token (parser->lexer)->location;
13577 richloc.add_fixit_insert_before (before_loc, "}");
13578 error_at (&richloc,
13579 "%<if consteval%> requires compound statement");
13580 non_compound_stmt_p = false;
13583 finish_then_clause (statement);
13585 /* If the next token is `else', parse the else-clause. */
13586 if (cp_lexer_next_token_is_keyword (parser->lexer,
13587 RID_ELSE))
13589 cp_token *else_tok = cp_lexer_peek_token (parser->lexer);
13590 gcc_rich_location else_richloc (else_tok->location);
13591 guard_tinfo = get_token_indent_info (else_tok);
13592 /* Consume the `else' keyword. */
13593 cp_lexer_consume_token (parser->lexer);
13595 begin_else_clause (statement);
13597 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13599 non_compound_stmt_p = true;
13600 else_richloc.add_fixit_insert_after (else_tok->location,
13601 "{");
13604 in_consteval_if_p = save_in_consteval_if_p | (ce < 0);
13605 cp_parser_implicitly_scoped_statement (parser, NULL,
13606 guard_tinfo);
13608 if (non_compound_stmt_p)
13610 location_t before_loc
13611 = cp_lexer_peek_token (parser->lexer)->location;
13612 else_richloc.add_fixit_insert_before (before_loc, "}");
13613 error_at (&else_richloc,
13614 "%<if consteval%> requires compound statement");
13617 finish_else_clause (statement);
13620 in_consteval_if_p = save_in_consteval_if_p;
13621 if (ce < 0)
13623 std::swap (THEN_CLAUSE (statement), ELSE_CLAUSE (statement));
13624 if (THEN_CLAUSE (statement) == NULL_TREE)
13625 THEN_CLAUSE (statement) = build_empty_stmt (tok->location);
13628 finish_if_stmt (statement);
13629 return statement;
13632 /* Look for the `('. */
13633 matching_parens parens;
13634 if (!parens.require_open (parser))
13636 cp_parser_skip_to_end_of_statement (parser);
13637 return error_mark_node;
13640 /* Begin the selection-statement. */
13641 if (keyword == RID_IF)
13643 statement = begin_if_stmt ();
13644 IF_STMT_CONSTEXPR_P (statement) = cx;
13646 else
13647 statement = begin_switch_stmt ();
13649 /* Parse the optional init-statement. */
13650 if (cp_parser_init_statement_p (parser))
13652 tree decl;
13653 if (cxx_dialect < cxx17)
13654 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
13655 OPT_Wc__17_extensions,
13656 "init-statement in selection statements only available "
13657 "with %<-std=c++17%> or %<-std=gnu++17%>");
13658 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13659 /* A non-empty init-statement can have arbitrary side
13660 effects. */
13661 vec_free (chain);
13662 cp_parser_init_statement (parser, &decl);
13665 /* Parse the condition. */
13666 condition = cp_parser_condition (parser);
13667 /* Look for the `)'. */
13668 if (!parens.require_close (parser))
13669 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13670 /*consume_paren=*/true);
13672 if (keyword == RID_IF)
13674 bool nested_if;
13675 unsigned char in_statement;
13677 /* Add the condition. */
13678 condition = finish_if_stmt_cond (condition, statement);
13680 if (warn_duplicated_cond)
13681 warn_duplicated_cond_add_or_warn (token->location, condition,
13682 &chain);
13684 /* Parse the then-clause. */
13685 in_statement = parser->in_statement;
13686 parser->in_statement |= IN_IF_STMT;
13688 /* Outside a template, the non-selected branch of a constexpr
13689 if is a 'discarded statement', i.e. unevaluated. */
13690 bool was_discarded = in_discarded_stmt;
13691 bool discard_then = (cx && !processing_template_decl
13692 && integer_zerop (condition));
13693 if (discard_then)
13695 in_discarded_stmt = true;
13696 ++c_inhibit_evaluation_warnings;
13699 cp_parser_implicitly_scoped_statement (parser, &nested_if,
13700 guard_tinfo);
13702 parser->in_statement = in_statement;
13704 finish_then_clause (statement);
13706 if (discard_then)
13708 THEN_CLAUSE (statement) = NULL_TREE;
13709 in_discarded_stmt = was_discarded;
13710 --c_inhibit_evaluation_warnings;
13713 /* If the next token is `else', parse the else-clause. */
13714 if (cp_lexer_next_token_is_keyword (parser->lexer,
13715 RID_ELSE))
13717 bool discard_else = (cx && !processing_template_decl
13718 && integer_nonzerop (condition));
13719 if (discard_else)
13721 in_discarded_stmt = true;
13722 ++c_inhibit_evaluation_warnings;
13725 guard_tinfo
13726 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13727 /* Consume the `else' keyword. */
13728 cp_lexer_consume_token (parser->lexer);
13729 if (warn_duplicated_cond)
13731 if (cp_lexer_next_token_is_keyword (parser->lexer,
13732 RID_IF)
13733 && chain == NULL)
13735 /* We've got "if (COND) else if (COND2)". Start
13736 the condition chain and add COND as the first
13737 element. */
13738 chain = new vec<tree> ();
13739 if (!CONSTANT_CLASS_P (condition)
13740 && !TREE_SIDE_EFFECTS (condition))
13742 /* Wrap it in a NOP_EXPR so that we can set the
13743 location of the condition. */
13744 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
13745 condition);
13746 SET_EXPR_LOCATION (e, token->location);
13747 chain->safe_push (e);
13750 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
13751 RID_IF))
13752 /* This is if-else without subsequent if. Zap the
13753 condition chain; we would have already warned at
13754 this point. */
13755 vec_free (chain);
13757 begin_else_clause (statement);
13758 /* Parse the else-clause. */
13759 cp_parser_implicitly_scoped_statement (parser, NULL,
13760 guard_tinfo, chain);
13762 finish_else_clause (statement);
13764 /* If we are currently parsing a then-clause, then
13765 IF_P will not be NULL. We set it to true to
13766 indicate that this if statement has an else clause.
13767 This may trigger the Wparentheses warning below
13768 when we get back up to the parent if statement. */
13769 if (if_p != NULL)
13770 *if_p = true;
13772 if (discard_else)
13774 ELSE_CLAUSE (statement) = NULL_TREE;
13775 in_discarded_stmt = was_discarded;
13776 --c_inhibit_evaluation_warnings;
13779 else
13781 /* This if statement does not have an else clause. If
13782 NESTED_IF is true, then the then-clause has an if
13783 statement which does have an else clause. We warn
13784 about the potential ambiguity. */
13785 if (nested_if)
13786 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
13787 "suggest explicit braces to avoid ambiguous"
13788 " %<else%>");
13789 if (warn_duplicated_cond)
13790 /* We don't need the condition chain anymore. */
13791 vec_free (chain);
13794 /* Now we're all done with the if-statement. */
13795 finish_if_stmt (statement);
13797 else
13799 bool in_switch_statement_p;
13800 unsigned char in_statement;
13802 /* Add the condition. */
13803 finish_switch_cond (condition, statement);
13805 /* Parse the body of the switch-statement. */
13806 in_switch_statement_p = parser->in_switch_statement_p;
13807 in_statement = parser->in_statement;
13808 parser->in_switch_statement_p = true;
13809 parser->in_statement |= IN_SWITCH_STMT;
13810 cp_parser_implicitly_scoped_statement (parser, if_p,
13811 guard_tinfo);
13812 parser->in_switch_statement_p = in_switch_statement_p;
13813 parser->in_statement = in_statement;
13815 /* Now we're all done with the switch-statement. */
13816 finish_switch_stmt (statement);
13819 return statement;
13821 break;
13823 default:
13824 cp_parser_error (parser, "expected selection-statement");
13825 return error_mark_node;
13829 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
13830 If we have seen at least one decl-specifier, and the next token is not
13831 a parenthesis (after "int (" we might be looking at a functional cast)
13832 neither we are dealing with a concept-check expression then we must be
13833 looking at a declaration. */
13835 static void
13836 cp_parser_maybe_commit_to_declaration (cp_parser* parser,
13837 cp_decl_specifier_seq *decl_specs)
13839 if (decl_specs->any_specifiers_p
13840 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
13841 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
13842 && !cp_parser_error_occurred (parser)
13843 && !(decl_specs->type
13844 && TREE_CODE (decl_specs->type) == TYPE_DECL
13845 && is_constrained_auto (TREE_TYPE (decl_specs->type))))
13846 cp_parser_commit_to_tentative_parse (parser);
13849 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
13850 The declarator shall not specify a function or an array. Returns
13851 TRUE if the declarator is valid, FALSE otherwise. */
13853 static bool
13854 cp_parser_check_condition_declarator (cp_parser* parser,
13855 cp_declarator *declarator,
13856 location_t loc)
13858 if (declarator == cp_error_declarator
13859 || function_declarator_p (declarator)
13860 || declarator->kind == cdk_array)
13862 if (declarator == cp_error_declarator)
13863 /* Already complained. */;
13864 else if (declarator->kind == cdk_array)
13865 error_at (loc, "condition declares an array");
13866 else
13867 error_at (loc, "condition declares a function");
13868 if (parser->fully_implicit_function_template_p)
13869 abort_fully_implicit_template (parser);
13870 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
13871 /*or_comma=*/false,
13872 /*consume_paren=*/false);
13873 return false;
13875 else
13876 return true;
13879 /* Parse a condition.
13881 condition:
13882 expression
13883 type-specifier-seq declarator = initializer-clause
13884 type-specifier-seq declarator braced-init-list
13886 GNU Extension:
13888 condition:
13889 type-specifier-seq declarator asm-specification [opt]
13890 attributes [opt] = assignment-expression
13892 Returns the expression that should be tested. */
13894 static tree
13895 cp_parser_condition (cp_parser* parser)
13897 cp_decl_specifier_seq type_specifiers;
13898 const char *saved_message;
13899 int declares_class_or_enum;
13901 /* Try the declaration first. */
13902 cp_parser_parse_tentatively (parser);
13903 /* New types are not allowed in the type-specifier-seq for a
13904 condition. */
13905 saved_message = parser->type_definition_forbidden_message;
13906 parser->type_definition_forbidden_message
13907 = G_("types may not be defined in conditions");
13908 /* Parse the type-specifier-seq. */
13909 cp_parser_decl_specifier_seq (parser,
13910 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
13911 &type_specifiers,
13912 &declares_class_or_enum);
13913 /* Restore the saved message. */
13914 parser->type_definition_forbidden_message = saved_message;
13916 /* Gather the attributes that were provided with the
13917 decl-specifiers. */
13918 tree prefix_attributes = type_specifiers.attributes;
13920 cp_parser_maybe_commit_to_declaration (parser, &type_specifiers);
13922 /* If all is well, we might be looking at a declaration. */
13923 if (!cp_parser_error_occurred (parser))
13925 tree decl;
13926 tree asm_specification;
13927 tree attributes;
13928 cp_declarator *declarator;
13929 tree initializer = NULL_TREE;
13930 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13932 /* Parse the declarator. */
13933 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13934 CP_PARSER_FLAGS_NONE,
13935 /*ctor_dtor_or_conv_p=*/NULL,
13936 /*parenthesized_p=*/NULL,
13937 /*member_p=*/false,
13938 /*friend_p=*/false,
13939 /*static_p=*/false);
13940 /* Parse the attributes. */
13941 attributes = cp_parser_attributes_opt (parser);
13942 /* Parse the asm-specification. */
13943 asm_specification = cp_parser_asm_specification_opt (parser);
13944 /* If the next token is not an `=' or '{', then we might still be
13945 looking at an expression. For example:
13947 if (A(a).x)
13949 looks like a decl-specifier-seq and a declarator -- but then
13950 there is no `=', so this is an expression. */
13951 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
13952 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13953 cp_parser_simulate_error (parser);
13955 /* If we did see an `=' or '{', then we are looking at a declaration
13956 for sure. */
13957 if (cp_parser_parse_definitely (parser))
13959 tree pushed_scope;
13960 bool non_constant_p = false;
13961 int flags = LOOKUP_ONLYCONVERTING;
13963 if (!cp_parser_check_condition_declarator (parser, declarator, loc))
13964 return error_mark_node;
13966 /* Create the declaration. */
13967 decl = start_decl (declarator, &type_specifiers,
13968 /*initialized_p=*/true,
13969 attributes, prefix_attributes,
13970 &pushed_scope);
13972 declarator->init_loc = cp_lexer_peek_token (parser->lexer)->location;
13973 /* Parse the initializer. */
13974 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13976 initializer = cp_parser_braced_list (parser, &non_constant_p);
13977 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
13978 flags = 0;
13980 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13982 /* Consume the `='. */
13983 cp_lexer_consume_token (parser->lexer);
13984 initializer = cp_parser_initializer_clause (parser,
13985 &non_constant_p);
13987 else
13989 cp_parser_error (parser, "expected initializer");
13990 initializer = error_mark_node;
13992 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
13993 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
13995 /* Process the initializer. */
13996 cp_finish_decl (decl,
13997 initializer, !non_constant_p,
13998 asm_specification,
13999 flags);
14001 if (pushed_scope)
14002 pop_scope (pushed_scope);
14004 return convert_from_reference (decl);
14007 /* If we didn't even get past the declarator successfully, we are
14008 definitely not looking at a declaration. */
14009 else
14010 cp_parser_abort_tentative_parse (parser);
14012 /* Otherwise, we are looking at an expression. */
14013 return cp_parser_expression (parser);
14016 /* Parses a for-statement or range-for-statement until the closing ')',
14017 not included. */
14019 static tree
14020 cp_parser_for (cp_parser *parser, bool ivdep, tree unroll, bool novector)
14022 tree init, scope, decl;
14023 bool is_range_for;
14025 /* Begin the for-statement. */
14026 scope = begin_for_scope (&init);
14028 /* Maybe parse the optional init-statement in a range-based for loop. */
14029 if (cp_parser_range_based_for_with_init_p (parser)
14030 /* Checked for diagnostic purposes only. */
14031 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14033 tree dummy;
14034 cp_parser_init_statement (parser, &dummy);
14035 if (cxx_dialect < cxx20)
14037 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
14038 OPT_Wc__20_extensions,
14039 "range-based %<for%> loops with initializer only "
14040 "available with %<-std=c++20%> or %<-std=gnu++20%>");
14041 decl = error_mark_node;
14045 /* Parse the initialization. */
14046 is_range_for = cp_parser_init_statement (parser, &decl);
14048 if (is_range_for)
14049 return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll,
14050 novector, false);
14051 else
14052 return cp_parser_c_for (parser, scope, init, ivdep, unroll, novector);
14055 static tree
14056 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
14057 tree unroll, bool novector)
14059 /* Normal for loop */
14060 tree condition = NULL_TREE;
14061 tree expression = NULL_TREE;
14062 tree stmt;
14064 stmt = begin_for_stmt (scope, init);
14065 /* The init-statement has already been parsed in
14066 cp_parser_init_statement, so no work is needed here. */
14067 finish_init_stmt (stmt);
14069 /* If there's a condition, process it. */
14070 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14071 condition = cp_parser_condition (parser);
14072 else if (ivdep)
14074 cp_parser_error (parser, "missing loop condition in loop with "
14075 "%<GCC ivdep%> pragma");
14076 condition = error_mark_node;
14078 else if (unroll)
14080 cp_parser_error (parser, "missing loop condition in loop with "
14081 "%<GCC unroll%> pragma");
14082 condition = error_mark_node;
14084 finish_for_cond (condition, stmt, ivdep, unroll, novector);
14085 /* Look for the `;'. */
14086 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14088 /* If there's an expression, process it. */
14089 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
14090 expression = cp_parser_expression (parser);
14091 finish_for_expr (expression, stmt);
14093 return stmt;
14096 /* Tries to parse a range-based for-statement:
14098 range-based-for:
14099 decl-specifier-seq declarator : expression
14101 The decl-specifier-seq declarator and the `:' are already parsed by
14102 cp_parser_init_statement. If processing_template_decl it returns a
14103 newly created RANGE_FOR_STMT; if not, it is converted to a
14104 regular FOR_STMT. */
14106 static tree
14107 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
14108 bool ivdep, tree unroll, bool novector, bool is_omp)
14110 tree stmt, range_expr;
14111 auto_vec <cxx_binding *, 16> bindings;
14112 auto_vec <tree, 16> names;
14113 cp_decomp decomp_d, *decomp = NULL;
14115 /* Get the range declaration momentarily out of the way so that
14116 the range expression doesn't clash with it. */
14117 if (range_decl != error_mark_node)
14119 if (DECL_HAS_VALUE_EXPR_P (range_decl))
14121 tree v = DECL_VALUE_EXPR (range_decl);
14122 /* For decomposition declaration get all of the corresponding
14123 declarations out of the way. */
14124 if (TREE_CODE (v) == ARRAY_REF
14125 && VAR_P (TREE_OPERAND (v, 0))
14126 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
14128 tree d = range_decl;
14129 range_decl = TREE_OPERAND (v, 0);
14130 decomp = &decomp_d;
14131 decomp->count = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
14132 decomp->decl = d;
14133 for (unsigned int i = 0; i < decomp->count;
14134 i++, d = DECL_CHAIN (d))
14136 tree name = DECL_NAME (d);
14137 names.safe_push (name);
14138 bindings.safe_push (IDENTIFIER_BINDING (name));
14139 IDENTIFIER_BINDING (name)
14140 = IDENTIFIER_BINDING (name)->previous;
14144 if (names.is_empty ())
14146 tree name = DECL_NAME (range_decl);
14147 names.safe_push (name);
14148 bindings.safe_push (IDENTIFIER_BINDING (name));
14149 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
14153 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14154 range_expr = cp_parser_braced_list (parser);
14155 else
14156 range_expr = cp_parser_expression (parser);
14158 /* Put the range declaration(s) back into scope. */
14159 for (unsigned int i = 0; i < names.length (); i++)
14161 cxx_binding *binding = bindings[i];
14162 binding->previous = IDENTIFIER_BINDING (names[i]);
14163 IDENTIFIER_BINDING (names[i]) = binding;
14166 /* finish_omp_for has its own code for the following, so just
14167 return the range_expr instead. */
14168 if (is_omp)
14169 return range_expr;
14171 /* If in template, STMT is converted to a normal for-statement
14172 at instantiation. If not, it is done just ahead. */
14173 if (processing_template_decl)
14175 if (check_for_bare_parameter_packs (range_expr))
14176 range_expr = error_mark_node;
14177 stmt = begin_range_for_stmt (scope, init);
14178 if (ivdep)
14179 RANGE_FOR_IVDEP (stmt) = 1;
14180 if (unroll)
14181 RANGE_FOR_UNROLL (stmt) = unroll;
14182 if (novector)
14183 RANGE_FOR_NOVECTOR (stmt) = 1;
14184 finish_range_for_decl (stmt, range_decl, range_expr);
14185 if (!type_dependent_expression_p (range_expr)
14186 /* do_auto_deduction doesn't mess with template init-lists. */
14187 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
14188 do_range_for_auto_deduction (range_decl, range_expr, decomp);
14190 else
14192 stmt = begin_for_stmt (scope, init);
14193 stmt = cp_convert_range_for (stmt, range_decl, range_expr, decomp,
14194 ivdep, unroll, novector);
14196 return stmt;
14199 /* Subroutine of cp_convert_range_for: given the initializer expression,
14200 builds up the range temporary. */
14202 static tree
14203 build_range_temp (tree range_expr)
14205 /* Find out the type deduced by the declaration
14206 `auto &&__range = range_expr'. */
14207 tree auto_node = make_auto ();
14208 tree range_type = cp_build_reference_type (auto_node, true);
14209 range_type = do_auto_deduction (range_type, range_expr, auto_node);
14211 /* Create the __range variable. */
14212 tree range_temp = build_decl (input_location, VAR_DECL,
14213 for_range__identifier, range_type);
14214 TREE_USED (range_temp) = 1;
14215 DECL_ARTIFICIAL (range_temp) = 1;
14217 return range_temp;
14220 /* Used by cp_parser_range_for in template context: we aren't going to
14221 do a full conversion yet, but we still need to resolve auto in the
14222 type of the for-range-declaration if present. This is basically
14223 a shortcut version of cp_convert_range_for. */
14225 static void
14226 do_range_for_auto_deduction (tree decl, tree range_expr, cp_decomp *decomp)
14228 tree auto_node = type_uses_auto (TREE_TYPE (decl));
14229 if (auto_node)
14231 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
14232 range_temp = convert_from_reference (build_range_temp (range_expr));
14233 iter_type = (cp_parser_perform_range_for_lookup
14234 (range_temp, &begin_dummy, &end_dummy));
14235 if (iter_type)
14237 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
14238 iter_type);
14239 iter_decl = build_x_indirect_ref (input_location, iter_decl,
14240 RO_UNARY_STAR, NULL_TREE,
14241 tf_warning_or_error);
14242 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
14243 iter_decl, auto_node,
14244 tf_warning_or_error,
14245 adc_variable_type);
14246 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
14247 cp_finish_decomp (decl, decomp);
14252 /* Warns when the loop variable should be changed to a reference type to
14253 avoid unnecessary copying. I.e., from
14255 for (const auto x : range)
14257 where range returns a reference, to
14259 for (const auto &x : range)
14261 if this version doesn't make a copy.
14263 This function also warns when the loop variable is initialized with
14264 a value of a different type resulting in a copy:
14266 int arr[10];
14267 for (const double &x : arr)
14269 DECL is the RANGE_DECL; EXPR is the *__for_begin expression.
14270 This function is never called when processing_template_decl is on. */
14272 static void
14273 warn_for_range_copy (tree decl, tree expr)
14275 if (!warn_range_loop_construct
14276 || decl == error_mark_node)
14277 return;
14279 location_t loc = DECL_SOURCE_LOCATION (decl);
14280 tree type = TREE_TYPE (decl);
14282 if (from_macro_expansion_at (loc))
14283 return;
14285 if (TYPE_REF_P (type))
14287 if (glvalue_p (expr)
14288 && ref_conv_binds_to_temporary (type, expr).is_true ())
14290 auto_diagnostic_group d;
14291 if (warning_at (loc, OPT_Wrange_loop_construct,
14292 "loop variable %qD of type %qT binds to a temporary "
14293 "constructed from type %qT", decl, type,
14294 TREE_TYPE (expr)))
14296 tree ref = cp_build_qualified_type (TREE_TYPE (expr),
14297 TYPE_QUAL_CONST);
14298 ref = cp_build_reference_type (ref, /*rval*/false);
14299 inform (loc, "use non-reference type %qT to make the copy "
14300 "explicit or %qT to prevent copying",
14301 non_reference (type), ref);
14304 return;
14306 else if (!CP_TYPE_CONST_P (type))
14307 return;
14309 /* Since small trivially copyable types are cheap to copy, we suppress the
14310 warning for them. 64B is a common size of a cache line. */
14311 if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST
14312 || (tree_to_uhwi (TYPE_SIZE_UNIT (type)) <= 64
14313 && trivially_copyable_p (type)))
14314 return;
14316 /* If we can initialize a reference directly, suggest that to avoid the
14317 copy. */
14318 tree rtype = cp_build_reference_type (type, /*rval*/false);
14319 if (ref_conv_binds_to_temporary (rtype, expr).is_false ())
14321 auto_diagnostic_group d;
14322 if (warning_at (loc, OPT_Wrange_loop_construct,
14323 "loop variable %qD creates a copy from type %qT",
14324 decl, type))
14326 gcc_rich_location richloc (loc);
14327 richloc.add_fixit_insert_before ("&");
14328 inform (&richloc, "use reference type to prevent copying");
14333 /* Converts a range-based for-statement into a normal
14334 for-statement, as per the definition.
14336 for (RANGE_DECL : RANGE_EXPR)
14337 BLOCK
14339 should be equivalent to:
14342 auto &&__range = RANGE_EXPR;
14343 for (auto __begin = BEGIN_EXPR, __end = END_EXPR;
14344 __begin != __end;
14345 ++__begin)
14347 RANGE_DECL = *__begin;
14348 BLOCK
14352 If RANGE_EXPR is an array:
14353 BEGIN_EXPR = __range
14354 END_EXPR = __range + ARRAY_SIZE(__range)
14355 Else if RANGE_EXPR has a member 'begin' or 'end':
14356 BEGIN_EXPR = __range.begin()
14357 END_EXPR = __range.end()
14358 Else:
14359 BEGIN_EXPR = begin(__range)
14360 END_EXPR = end(__range);
14362 If __range has a member 'begin' but not 'end', or vice versa, we must
14363 still use the second alternative (it will surely fail, however).
14364 When calling begin()/end() in the third alternative we must use
14365 argument dependent lookup, but always considering 'std' as an associated
14366 namespace. */
14368 tree
14369 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
14370 cp_decomp *decomp, bool ivdep, tree unroll,
14371 bool novector)
14373 tree begin, end;
14374 tree iter_type, begin_expr, end_expr;
14375 tree condition, expression;
14377 range_expr = mark_lvalue_use (range_expr);
14379 if (range_decl == error_mark_node || range_expr == error_mark_node)
14380 /* If an error happened previously do nothing or else a lot of
14381 unhelpful errors would be issued. */
14382 begin_expr = end_expr = iter_type = error_mark_node;
14383 else
14385 tree range_temp;
14387 if (VAR_P (range_expr)
14388 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
14389 /* Can't bind a reference to an array of runtime bound. */
14390 range_temp = range_expr;
14391 else
14393 range_temp = build_range_temp (range_expr);
14394 pushdecl (range_temp);
14395 cp_finish_decl (range_temp, range_expr,
14396 /*is_constant_init*/false, NULL_TREE,
14397 LOOKUP_ONLYCONVERTING);
14398 range_temp = convert_from_reference (range_temp);
14400 iter_type = cp_parser_perform_range_for_lookup (range_temp,
14401 &begin_expr, &end_expr);
14404 /* The new for initialization statement. */
14405 begin = build_decl (input_location, VAR_DECL, for_begin__identifier,
14406 iter_type);
14407 TREE_USED (begin) = 1;
14408 DECL_ARTIFICIAL (begin) = 1;
14409 pushdecl (begin);
14410 cp_finish_decl (begin, begin_expr,
14411 /*is_constant_init*/false, NULL_TREE,
14412 LOOKUP_ONLYCONVERTING);
14414 if (cxx_dialect >= cxx17)
14415 iter_type = cv_unqualified (TREE_TYPE (end_expr));
14416 end = build_decl (input_location, VAR_DECL, for_end__identifier, iter_type);
14417 TREE_USED (end) = 1;
14418 DECL_ARTIFICIAL (end) = 1;
14419 pushdecl (end);
14420 cp_finish_decl (end, end_expr,
14421 /*is_constant_init*/false, NULL_TREE,
14422 LOOKUP_ONLYCONVERTING);
14424 finish_init_stmt (statement);
14426 /* The new for condition. */
14427 condition = build_x_binary_op (input_location, NE_EXPR,
14428 begin, ERROR_MARK,
14429 end, ERROR_MARK,
14430 NULL_TREE, NULL, tf_warning_or_error);
14431 finish_for_cond (condition, statement, ivdep, unroll, novector);
14433 /* The new increment expression. */
14434 expression = finish_unary_op_expr (input_location,
14435 PREINCREMENT_EXPR, begin,
14436 tf_warning_or_error);
14437 finish_for_expr (expression, statement);
14439 /* The declaration is initialized with *__begin inside the loop body. */
14440 tree deref_begin = build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
14441 NULL_TREE, tf_warning_or_error);
14442 cp_finish_decl (range_decl, deref_begin,
14443 /*is_constant_init*/false, NULL_TREE,
14444 LOOKUP_ONLYCONVERTING, decomp);
14445 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
14446 cp_finish_decomp (range_decl, decomp);
14448 warn_for_range_copy (range_decl, deref_begin);
14450 return statement;
14453 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
14454 We need to solve both at the same time because the method used
14455 depends on the existence of members begin or end.
14456 Returns the type deduced for the iterator expression. */
14458 static tree
14459 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
14461 if (error_operand_p (range))
14463 *begin = *end = error_mark_node;
14464 return error_mark_node;
14467 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
14469 error ("range-based %<for%> expression of type %qT "
14470 "has incomplete type", TREE_TYPE (range));
14471 *begin = *end = error_mark_node;
14472 return error_mark_node;
14474 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
14476 /* If RANGE is an array, we will use pointer arithmetic. */
14477 *begin = decay_conversion (range, tf_warning_or_error);
14478 *end = build_binary_op (input_location, PLUS_EXPR,
14479 range,
14480 array_type_nelts_top (TREE_TYPE (range)),
14481 false);
14482 return TREE_TYPE (*begin);
14484 else
14486 /* If it is not an array, we must do a bit of magic. */
14487 tree id_begin, id_end;
14488 tree member_begin, member_end;
14490 *begin = *end = error_mark_node;
14492 id_begin = get_identifier ("begin");
14493 id_end = get_identifier ("end");
14494 member_begin = lookup_member (TREE_TYPE (range), id_begin,
14495 /*protect=*/2, /*want_type=*/false,
14496 tf_warning_or_error);
14497 member_end = lookup_member (TREE_TYPE (range), id_end,
14498 /*protect=*/2, /*want_type=*/false,
14499 tf_warning_or_error);
14501 if (member_begin != NULL_TREE && member_end != NULL_TREE)
14503 /* Use the member functions. */
14504 *begin = cp_parser_range_for_member_function (range, id_begin);
14505 *end = cp_parser_range_for_member_function (range, id_end);
14507 else
14509 /* Use global functions with ADL. */
14510 releasing_vec vec;
14512 vec_safe_push (vec, range);
14514 member_begin = perform_koenig_lookup (id_begin, vec,
14515 tf_warning_or_error);
14516 *begin = finish_call_expr (member_begin, &vec, false, true,
14517 tf_warning_or_error);
14518 member_end = perform_koenig_lookup (id_end, vec,
14519 tf_warning_or_error);
14520 *end = finish_call_expr (member_end, &vec, false, true,
14521 tf_warning_or_error);
14524 /* Last common checks. */
14525 if (*begin == error_mark_node || *end == error_mark_node)
14527 /* If one of the expressions is an error do no more checks. */
14528 *begin = *end = error_mark_node;
14529 return error_mark_node;
14531 else if (type_dependent_expression_p (*begin)
14532 || type_dependent_expression_p (*end))
14533 /* Can happen, when, eg, in a template context, Koenig lookup
14534 can't resolve begin/end (c++/58503). */
14535 return NULL_TREE;
14536 else
14538 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
14539 /* The unqualified type of the __begin and __end temporaries should
14540 be the same, as required by the multiple auto declaration. */
14541 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
14543 if (cxx_dialect >= cxx17
14544 && (build_x_binary_op (input_location, NE_EXPR,
14545 *begin, ERROR_MARK,
14546 *end, ERROR_MARK,
14547 NULL_TREE, NULL, tf_none)
14548 != error_mark_node))
14549 /* P0184R0 allows __begin and __end to have different types,
14550 but make sure they are comparable so we can give a better
14551 diagnostic. */;
14552 else
14553 error ("inconsistent begin/end types in range-based %<for%> "
14554 "statement: %qT and %qT",
14555 TREE_TYPE (*begin), TREE_TYPE (*end));
14557 return iter_type;
14562 /* Helper function for cp_parser_perform_range_for_lookup.
14563 Builds a tree for RANGE.IDENTIFIER(). */
14565 static tree
14566 cp_parser_range_for_member_function (tree range, tree identifier)
14568 tree member, res;
14570 member = finish_class_member_access_expr (range, identifier,
14571 false, tf_warning_or_error);
14572 if (member == error_mark_node)
14573 return error_mark_node;
14575 releasing_vec vec;
14576 res = finish_call_expr (member, &vec,
14577 /*disallow_virtual=*/false,
14578 /*koenig_p=*/false,
14579 tf_warning_or_error);
14580 return res;
14583 /* Parse an iteration-statement.
14585 iteration-statement:
14586 while ( condition ) statement
14587 do statement while ( expression ) ;
14588 for ( init-statement condition [opt] ; expression [opt] )
14589 statement
14591 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
14593 static tree
14594 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
14595 tree unroll, bool novector)
14597 cp_token *token;
14598 enum rid keyword;
14599 tree statement;
14600 unsigned char in_statement;
14601 token_indent_info guard_tinfo;
14603 /* Peek at the next token. */
14604 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
14605 if (!token)
14606 return error_mark_node;
14608 guard_tinfo = get_token_indent_info (token);
14610 /* Remember whether or not we are already within an iteration
14611 statement. */
14612 in_statement = parser->in_statement;
14614 /* Special case for OMP loop intervening code. Parsing of permitted
14615 collapsed loop nests is handled elsewhere. */
14616 if (parser->omp_for_parse_state)
14618 error_at (token->location,
14619 "loop not permitted in intervening code in OpenMP loop body");
14620 parser->omp_for_parse_state->fail = true;
14623 /* See what kind of keyword it is. */
14624 keyword = token->keyword;
14625 switch (keyword)
14627 case RID_WHILE:
14629 tree condition;
14631 /* Begin the while-statement. */
14632 statement = begin_while_stmt ();
14633 /* Look for the `('. */
14634 matching_parens parens;
14635 parens.require_open (parser);
14636 /* Parse the condition. */
14637 condition = cp_parser_condition (parser);
14638 finish_while_stmt_cond (condition, statement, ivdep, unroll, novector);
14639 /* Look for the `)'. */
14640 parens.require_close (parser);
14641 /* Parse the dependent statement. */
14642 parser->in_statement = IN_ITERATION_STMT;
14643 bool prev = note_iteration_stmt_body_start ();
14644 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
14645 note_iteration_stmt_body_end (prev);
14646 parser->in_statement = in_statement;
14647 /* We're done with the while-statement. */
14648 finish_while_stmt (statement);
14650 break;
14652 case RID_DO:
14654 tree expression;
14656 /* Begin the do-statement. */
14657 statement = begin_do_stmt ();
14658 /* Parse the body of the do-statement. */
14659 parser->in_statement = IN_ITERATION_STMT;
14660 bool prev = note_iteration_stmt_body_start ();
14661 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
14662 note_iteration_stmt_body_end (prev);
14663 parser->in_statement = in_statement;
14664 finish_do_body (statement);
14665 /* Look for the `while' keyword. */
14666 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
14667 /* Look for the `('. */
14668 matching_parens parens;
14669 parens.require_open (parser);
14670 /* Parse the expression. */
14671 expression = cp_parser_expression (parser);
14672 /* We're done with the do-statement. */
14673 finish_do_stmt (expression, statement, ivdep, unroll, novector);
14674 /* Look for the `)'. */
14675 parens.require_close (parser);
14676 /* Look for the `;'. */
14677 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14679 break;
14681 case RID_FOR:
14683 /* Look for the `('. */
14684 matching_parens parens;
14685 parens.require_open (parser);
14687 statement = cp_parser_for (parser, ivdep, unroll, novector);
14689 /* Look for the `)'. */
14690 parens.require_close (parser);
14692 /* Parse the body of the for-statement. */
14693 parser->in_statement = IN_ITERATION_STMT;
14694 bool prev = note_iteration_stmt_body_start ();
14695 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
14696 note_iteration_stmt_body_end (prev);
14697 parser->in_statement = in_statement;
14699 /* We're done with the for-statement. */
14700 finish_for_stmt (statement);
14702 break;
14704 default:
14705 cp_parser_error (parser, "expected iteration-statement");
14706 statement = error_mark_node;
14707 break;
14710 return statement;
14713 /* Parse an init-statement or the declarator of a range-based-for.
14714 Returns true if a range-based-for declaration is seen.
14716 init-statement:
14717 expression-statement
14718 simple-declaration
14719 alias-declaration */
14721 static bool
14722 cp_parser_init_statement (cp_parser *parser, tree *decl)
14724 /* If the next token is a `;', then we have an empty
14725 expression-statement. Grammatically, this is also a
14726 simple-declaration, but an invalid one, because it does not
14727 declare anything. Therefore, if we did not handle this case
14728 specially, we would issue an error message about an invalid
14729 declaration. */
14730 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14732 bool is_range_for = false;
14733 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
14735 /* A colon is used in range-based for. */
14736 parser->colon_corrects_to_scope_p = false;
14738 /* We're going to speculatively look for a declaration, falling back
14739 to an expression, if necessary. */
14740 cp_parser_parse_tentatively (parser);
14741 bool expect_semicolon_p = true;
14742 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
14744 cp_parser_alias_declaration (parser);
14745 expect_semicolon_p = false;
14746 if (cxx_dialect < cxx23
14747 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
14748 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
14749 OPT_Wc__23_extensions,
14750 "alias-declaration in init-statement only "
14751 "available with %<-std=c++23%> or %<-std=gnu++23%>");
14753 else
14754 /* Parse the declaration. */
14755 cp_parser_simple_declaration (parser,
14756 /*function_definition_allowed_p=*/false,
14757 decl);
14758 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
14759 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14761 /* It is a range-for, consume the ':'. */
14762 cp_lexer_consume_token (parser->lexer);
14763 is_range_for = true;
14764 if (cxx_dialect < cxx11)
14765 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
14766 OPT_Wc__11_extensions,
14767 "range-based %<for%> loops only available with "
14768 "%<-std=c++11%> or %<-std=gnu++11%>");
14770 else if (expect_semicolon_p)
14771 /* The ';' is not consumed yet because we told
14772 cp_parser_simple_declaration not to. */
14773 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14775 if (cp_parser_parse_definitely (parser))
14776 return is_range_for;
14777 /* If the tentative parse failed, then we shall need to look for an
14778 expression-statement. */
14780 /* If we are here, it is an expression-statement. */
14781 cp_parser_expression_statement (parser, NULL_TREE);
14782 return false;
14785 /* Parse a jump-statement.
14787 jump-statement:
14788 break ;
14789 continue ;
14790 return expression [opt] ;
14791 return braced-init-list ;
14792 coroutine-return-statement;
14793 goto identifier ;
14795 GNU extension:
14797 jump-statement:
14798 goto * expression ;
14800 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
14802 static tree
14803 cp_parser_jump_statement (cp_parser* parser)
14805 tree statement = error_mark_node;
14806 cp_token *token;
14807 enum rid keyword;
14808 unsigned char in_statement;
14810 /* Peek at the next token. */
14811 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
14812 if (!token)
14813 return error_mark_node;
14815 /* See what kind of keyword it is. */
14816 keyword = token->keyword;
14817 switch (keyword)
14819 case RID_BREAK:
14820 in_statement = parser->in_statement & ~IN_IF_STMT;
14821 switch (in_statement)
14823 case 0:
14824 error_at (token->location, "break statement not within loop or switch");
14825 break;
14826 default:
14827 gcc_assert ((in_statement & IN_SWITCH_STMT)
14828 || in_statement == IN_ITERATION_STMT);
14829 statement = finish_break_stmt ();
14830 if (in_statement == IN_ITERATION_STMT)
14831 break_maybe_infinite_loop ();
14832 break;
14833 case IN_OMP_BLOCK:
14834 error_at (token->location, "invalid exit from OpenMP structured block");
14835 break;
14836 case IN_OMP_FOR:
14837 error_at (token->location, "break statement used with OpenMP for loop");
14838 break;
14840 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14841 break;
14843 case RID_CONTINUE:
14844 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
14846 case 0:
14847 error_at (token->location, "continue statement not within a loop");
14848 break;
14849 /* Fall through. */
14850 case IN_ITERATION_STMT:
14851 case IN_OMP_FOR:
14852 statement = finish_continue_stmt ();
14853 break;
14854 case IN_OMP_BLOCK:
14855 error_at (token->location, "invalid exit from OpenMP structured block");
14856 break;
14857 default:
14858 gcc_unreachable ();
14860 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14861 break;
14863 case RID_CO_RETURN:
14864 case RID_RETURN:
14866 tree expr;
14868 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14870 cp_lexer_set_source_position (parser->lexer);
14871 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14872 expr = cp_parser_braced_list (parser);
14874 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14875 expr = cp_parser_expression (parser);
14876 else
14877 /* If the next token is a `;', then there is no
14878 expression. */
14879 expr = NULL_TREE;
14880 /* Build the return-statement, check co-return first, since type
14881 deduction is not valid there. */
14882 if (keyword == RID_CO_RETURN)
14883 statement = finish_co_return_stmt (token->location, expr);
14884 else if (FNDECL_USED_AUTO (current_function_decl) && in_discarded_stmt)
14885 /* Don't deduce from a discarded return statement. */;
14886 else
14887 statement = finish_return_stmt (expr);
14888 /* Look for the final `;'. */
14889 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14891 break;
14893 case RID_GOTO:
14894 if (parser->in_function_body
14895 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
14896 && cxx_dialect < cxx23)
14898 error ("%<goto%> in %<constexpr%> function only available with "
14899 "%<-std=c++2b%> or %<-std=gnu++2b%>");
14900 cp_function_chain->invalid_constexpr = true;
14903 /* Create the goto-statement. */
14904 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
14906 /* Issue a warning about this use of a GNU extension. */
14907 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
14908 /* Consume the '*' token. */
14909 cp_lexer_consume_token (parser->lexer);
14910 /* Parse the dependent expression. */
14911 finish_goto_stmt (cp_parser_expression (parser));
14913 else
14914 finish_goto_stmt (cp_parser_identifier (parser));
14915 /* Look for the final `;'. */
14916 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14917 break;
14919 default:
14920 cp_parser_error (parser, "expected jump-statement");
14921 break;
14924 return statement;
14927 /* Parse a declaration-statement.
14929 declaration-statement:
14930 block-declaration */
14932 static void
14933 cp_parser_declaration_statement (cp_parser* parser)
14935 void *p;
14937 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
14938 p = obstack_alloc (&declarator_obstack, 0);
14940 /* Parse the block-declaration. */
14941 cp_parser_block_declaration (parser, /*statement_p=*/true);
14943 /* Free any declarators allocated. */
14944 obstack_free (&declarator_obstack, p);
14947 /* Some dependent statements (like `if (cond) statement'), are
14948 implicitly in their own scope. In other words, if the statement is
14949 a single statement (as opposed to a compound-statement), it is
14950 none-the-less treated as if it were enclosed in braces. Any
14951 declarations appearing in the dependent statement are out of scope
14952 after control passes that point. This function parses a statement,
14953 but ensures that is in its own scope, even if it is not a
14954 compound-statement.
14956 If IF_P is not NULL, *IF_P is set to indicate whether the statement
14957 is a (possibly labeled) if statement which is not enclosed in
14958 braces and has an else clause. This is used to implement
14959 -Wparentheses.
14961 CHAIN is a vector of if-else-if conditions. This is used to implement
14962 -Wduplicated-cond.
14964 Returns the new statement. */
14966 static tree
14967 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
14968 const token_indent_info &guard_tinfo,
14969 vec<tree> *chain)
14971 tree statement;
14972 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
14973 location_t body_loc_after_labels = UNKNOWN_LOCATION;
14974 token_indent_info body_tinfo
14975 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
14977 if (if_p != NULL)
14978 *if_p = false;
14980 /* Mark if () ; with a special NOP_EXPR. */
14981 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14983 cp_lexer_consume_token (parser->lexer);
14984 statement = add_stmt (build_empty_stmt (body_loc));
14986 if (guard_tinfo.keyword == RID_IF
14987 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
14988 warning_at (body_loc, OPT_Wempty_body,
14989 "suggest braces around empty body in an %<if%> statement");
14990 else if (guard_tinfo.keyword == RID_ELSE)
14991 warning_at (body_loc, OPT_Wempty_body,
14992 "suggest braces around empty body in an %<else%> statement");
14994 /* if a compound is opened, we simply parse the statement directly. */
14995 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14996 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
14997 /* If the token is not a `{', then we must take special action. */
14998 else
15000 /* Create a compound-statement. */
15001 statement = begin_compound_stmt (0);
15002 /* Parse the dependent-statement. */
15003 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
15004 &body_loc_after_labels);
15005 /* Finish the dummy compound-statement. */
15006 finish_compound_stmt (statement);
15009 token_indent_info next_tinfo
15010 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
15011 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
15013 if (body_loc_after_labels != UNKNOWN_LOCATION
15014 && next_tinfo.type != CPP_SEMICOLON)
15015 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
15016 guard_tinfo.location, guard_tinfo.keyword);
15018 /* Return the statement. */
15019 return statement;
15022 /* For some dependent statements (like `while (cond) statement'), we
15023 have already created a scope. Therefore, even if the dependent
15024 statement is a compound-statement, we do not want to create another
15025 scope. */
15027 static void
15028 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
15029 const token_indent_info &guard_tinfo)
15031 /* If the token is a `{', then we must take special action. */
15032 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
15034 token_indent_info body_tinfo
15035 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
15036 location_t loc_after_labels = UNKNOWN_LOCATION;
15038 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
15039 &loc_after_labels);
15040 token_indent_info next_tinfo
15041 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
15042 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
15044 if (loc_after_labels != UNKNOWN_LOCATION
15045 && next_tinfo.type != CPP_SEMICOLON)
15046 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
15047 guard_tinfo.location,
15048 guard_tinfo.keyword);
15050 else
15052 /* Avoid calling cp_parser_compound_statement, so that we
15053 don't create a new scope. Do everything else by hand. */
15054 matching_braces braces;
15055 braces.require_open (parser);
15056 /* If the next keyword is `__label__' we have a label declaration. */
15057 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
15058 cp_parser_label_declaration (parser);
15059 /* Parse an (optional) statement-seq. */
15060 cp_parser_statement_seq_opt (parser, NULL_TREE);
15061 braces.require_close (parser);
15065 /* Modules */
15067 /* Parse a module-name or module-partition.
15069 module-name:
15070 module-name-qualifier [opt] identifier
15072 module-partition:
15073 : module-name-qualifier [opt] identifier
15075 module-name-qualifier:
15076 identifier .
15077 module-name-qualifier identifier .
15079 Returns a pointer to the module object, or NULL on failure.
15080 For PARTITION_P, PARENT is the module this is a partition of. */
15082 static module_state *
15083 cp_parser_module_name (cp_parser *parser, bool partition_p = false,
15084 module_state *parent = NULL)
15086 if (partition_p
15087 && cp_lexer_consume_token (parser->lexer)->type != CPP_COLON)
15088 return NULL;
15090 for (;;)
15092 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME)
15094 if (partition_p)
15095 cp_parser_error (parser, "expected module-partition");
15096 else
15097 cp_parser_error (parser, "expected module-name");
15098 return NULL;
15101 tree name = cp_lexer_consume_token (parser->lexer)->u.value;
15102 parent = get_module (name, parent, partition_p);
15103 if (cp_lexer_peek_token (parser->lexer)->type != CPP_DOT)
15104 break;
15106 cp_lexer_consume_token (parser->lexer);
15109 return parent;
15112 /* Parse a module-partition. Defers to cp_parser_module_name. */
15114 static module_state *
15115 cp_parser_module_partition (cp_parser *parser, module_state *parent = NULL)
15117 return cp_parser_module_name (parser, /*partition_p=*/true, parent);
15120 /* Named module-declaration
15121 __module ; PRAGMA_EOL
15122 __module : private ; PRAGMA_EOL (unimplemented)
15123 [__export] __module module-name module-partition [opt]
15124 attr-spec-seq-opt ; PRAGMA_EOL
15127 static module_parse
15128 cp_parser_module_declaration (cp_parser *parser, module_parse mp_state,
15129 bool exporting)
15131 /* We're a pseudo pragma. */
15132 parser->lexer->in_pragma = true;
15133 cp_token *token = cp_lexer_consume_token (parser->lexer);
15135 if (flag_header_unit)
15137 error_at (token->location,
15138 "module-declaration not permitted in header-unit");
15139 goto skip_eol;
15141 else if (mp_state == MP_FIRST && !exporting
15142 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15144 /* Start global module fragment. */
15145 cp_lexer_consume_token (parser->lexer);
15146 module_kind = MK_NAMED;
15147 mp_state = MP_GLOBAL;
15148 cp_parser_require_pragma_eol (parser, token);
15150 else if (!exporting
15151 && cp_lexer_next_token_is (parser->lexer, CPP_COLON)
15152 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_PRIVATE)
15153 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_SEMICOLON))
15155 cp_lexer_consume_token (parser->lexer);
15156 cp_lexer_consume_token (parser->lexer);
15157 cp_lexer_consume_token (parser->lexer);
15158 cp_parser_require_pragma_eol (parser, token);
15160 if ((mp_state == MP_PURVIEW || mp_state == MP_PURVIEW_IMPORTS)
15161 && module_has_cmi_p ())
15163 mp_state = MP_PRIVATE_IMPORTS;
15164 sorry_at (token->location, "private module fragment");
15166 else
15167 error_at (token->location,
15168 "private module fragment only permitted in purview"
15169 " of module interface or partition");
15171 else if (!(mp_state == MP_FIRST || mp_state == MP_GLOBAL))
15173 /* Neither the first declaration, nor in a GMF. */
15174 error_at (token->location, "module-declaration only permitted as first"
15175 " declaration, or ending a global module fragment");
15176 skip_eol:
15177 cp_parser_skip_to_pragma_eol (parser, token);
15179 else
15181 module_state *mod = cp_parser_module_name (parser);
15182 if (mod && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
15183 mod = cp_parser_module_partition (parser, mod);
15184 tree attrs = cp_parser_attributes_opt (parser);
15186 if (mod)
15187 mp_state = MP_PURVIEW_IMPORTS;
15188 if (!mod || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
15189 goto skip_eol;
15191 declare_module (mod, token->location, exporting, attrs, parse_in);
15192 cp_parser_require_pragma_eol (parser, token);
15195 return mp_state;
15198 /* Import-declaration
15199 __import module-name attr-spec-seq-opt ; PRAGMA_EOL
15200 __import module-partition attr-spec-seq-opt ; PRAGMA_EOL
15201 __import header-name attr-spec-seq-opt ; PRAGMA_EOL
15204 static void
15205 cp_parser_import_declaration (cp_parser *parser, module_parse mp_state,
15206 bool exporting)
15208 /* We're a pseudo pragma. */
15209 parser->lexer->in_pragma = true;
15210 cp_token *token = cp_lexer_consume_token (parser->lexer);
15212 if (mp_state == MP_PURVIEW || mp_state == MP_PRIVATE)
15214 error_at (token->location, "post-module-declaration"
15215 " imports must be contiguous");
15216 note_lexer:
15217 inform (token->location, "perhaps insert a line break, or other"
15218 " disambiguation, to prevent this being considered a"
15219 " module control-line");
15220 skip_eol:
15221 cp_parser_skip_to_pragma_eol (parser, token);
15223 else if (current_scope () != global_namespace)
15225 error_at (token->location, "import-declaration must be at global scope");
15226 goto note_lexer;
15228 else
15230 module_state *mod = NULL;
15231 cp_token *next = cp_lexer_peek_token (parser->lexer);
15232 if (next->type == CPP_HEADER_NAME)
15234 cp_lexer_consume_token (parser->lexer);
15235 mod = get_module (next->u.value);
15237 else if (next->type == CPP_COLON)
15239 /* An import specifying a module-partition shall only appear after the
15240 module-declaration in a module unit: [module.import]/4. */
15241 if (named_module_p ()
15242 && (mp_state == MP_PURVIEW_IMPORTS
15243 || mp_state == MP_PRIVATE_IMPORTS))
15244 mod = cp_parser_module_partition (parser);
15245 else
15246 error_at (next->location, "import specifying a module-partition"
15247 " must appear after a named module-declaration");
15249 else
15250 mod = cp_parser_module_name (parser);
15251 tree attrs = cp_parser_attributes_opt (parser);
15253 if (!mod || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
15254 goto skip_eol;
15255 cp_parser_require_pragma_eol (parser, token);
15257 if (mp_state == MP_PURVIEW_IMPORTS || mp_state == MP_PRIVATE_IMPORTS)
15259 /* Module-purview imports must not be from source inclusion
15260 [cpp.import]/7 */
15261 if (attrs
15262 && private_lookup_attribute ("__translated",
15263 strlen ("__translated"), attrs))
15264 error_at (token->location, "post-module-declaration imports"
15265 " must not be include-translated");
15266 else if (!token->main_source_p)
15267 error_at (token->location, "post-module-declaration imports"
15268 " must not be from header inclusion");
15271 import_module (mod, token->location, exporting, attrs, parse_in);
15275 /* export-declaration.
15277 export name-declaration
15278 export { declaration-seq-opt } */
15280 static void
15281 cp_parser_module_export (cp_parser *parser)
15283 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT));
15284 cp_token *token = cp_lexer_consume_token (parser->lexer);
15286 if (!module_interface_p ())
15287 error_at (token->location,
15288 "%qE may only occur after a module interface declaration",
15289 token->u.value);
15291 bool braced = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE);
15293 unsigned mk = module_kind;
15294 if (module_exporting_p ())
15295 error_at (token->location,
15296 "%qE may only occur once in an export declaration",
15297 token->u.value);
15298 module_kind |= MK_EXPORTING;
15300 if (braced)
15302 cp_ensure_no_omp_declare_simd (parser);
15303 cp_ensure_no_oacc_routine (parser);
15305 cp_lexer_consume_token (parser->lexer);
15306 cp_parser_declaration_seq_opt (parser);
15307 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15309 else
15311 /* Explicitly check if the next tokens might be a
15312 module-directive line, so we can give a clearer error message
15313 about why the directive will be rejected. */
15314 if (cp_lexer_next_token_is_keyword (parser->lexer, RID__MODULE)
15315 || cp_lexer_next_token_is_keyword (parser->lexer, RID__IMPORT)
15316 || cp_lexer_next_token_is_keyword (parser->lexer, RID__EXPORT))
15317 error_at (token->location, "%<export%> not part of following"
15318 " module-directive");
15320 bool saved_in_unbraced_export_declaration_p
15321 = parser->in_unbraced_export_declaration_p;
15322 parser->in_unbraced_export_declaration_p = true;
15323 cp_parser_declaration (parser, NULL_TREE);
15324 parser->in_unbraced_export_declaration_p
15325 = saved_in_unbraced_export_declaration_p;
15328 module_kind = mk;
15331 /* Declarations [gram.dcl.dcl] */
15333 /* Parse an optional declaration-sequence. TOP_LEVEL is true, if this
15334 is the top-level declaration sequence. That affects whether we
15335 deal with module-preamble.
15337 declaration-seq:
15338 declaration
15339 declaration-seq declaration */
15341 static void
15342 cp_parser_declaration_seq_opt (cp_parser* parser)
15344 while (true)
15346 cp_token *token = cp_lexer_peek_token (parser->lexer);
15348 if (token->type == CPP_CLOSE_BRACE
15349 || token->type == CPP_EOF)
15350 break;
15351 else
15352 cp_parser_toplevel_declaration (parser);
15356 /* Parse a declaration. The distinction between name-declaration
15357 and special-declaration is only since C++20.
15359 declaration:
15360 name-declaration
15361 special-declaration
15363 name-declaration:
15364 block-declaration
15365 nodeclspec-function-declaration
15366 function-definition
15367 template-declaration
15368 deduction-guide (C++17)
15369 linkage-specification
15370 namespace-definition
15371 empty-declaration
15372 attribute-declaration
15373 module-import-declaration (modules)
15375 special-declaration:
15376 explicit-instantiation
15377 explicit-specialization
15378 export-declaration (modules)
15380 GNU extension:
15382 declaration:
15383 __extension__ declaration */
15385 static void
15386 cp_parser_declaration (cp_parser* parser, tree prefix_attrs)
15388 int saved_pedantic;
15390 /* Check for the `__extension__' keyword. */
15391 if (cp_parser_extension_opt (parser, &saved_pedantic))
15393 /* Parse the qualified declaration. */
15394 cp_parser_declaration (parser, prefix_attrs);
15395 /* Restore the PEDANTIC flag. */
15396 pedantic = saved_pedantic;
15398 return;
15401 /* P2615: Determine if we're parsing a name-declaration specifically,
15402 or if special-declarations are OK too. */
15403 bool require_name_decl_p
15404 = (parser->in_unbraced_export_declaration_p
15405 || (parser->in_unbraced_linkage_specification_p
15406 && cxx_dialect >= cxx20));
15408 /* Try to figure out what kind of declaration is present. */
15409 cp_token *token1 = cp_lexer_peek_token (parser->lexer);
15410 cp_token *token2 = (token1->type == CPP_EOF
15411 ? token1 : cp_lexer_peek_nth_token (parser->lexer, 2));
15413 if (token1->type == CPP_SEMICOLON)
15415 cp_lexer_consume_token (parser->lexer);
15416 /* A declaration consisting of a single semicolon is invalid
15417 * before C++11. Allow it unless we're being pedantic. */
15418 if (cxx_dialect < cxx11)
15419 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
15420 return;
15422 else if (cp_lexer_nth_token_is (parser->lexer,
15423 cp_parser_skip_std_attribute_spec_seq (parser,
15425 CPP_SEMICOLON))
15427 location_t attrs_loc = token1->location;
15428 tree std_attrs = cp_parser_std_attribute_spec_seq (parser);
15430 if (std_attrs && (flag_openmp || flag_openmp_simd))
15432 gcc_assert (!parser->lexer->in_omp_attribute_pragma);
15433 std_attrs = cp_parser_handle_statement_omp_attributes (parser,
15434 std_attrs);
15435 if (parser->lexer->in_omp_attribute_pragma)
15437 cp_lexer *lexer = parser->lexer;
15438 while (parser->lexer->in_omp_attribute_pragma)
15440 gcc_assert (cp_lexer_next_token_is (parser->lexer,
15441 CPP_PRAGMA));
15442 cp_parser_pragma (parser, pragma_external, NULL);
15444 cp_lexer_destroy (lexer);
15448 if (std_attrs != NULL_TREE && any_nonignored_attribute_p (std_attrs))
15449 warning_at (make_location (attrs_loc, attrs_loc, parser->lexer),
15450 OPT_Wattributes, "attribute ignored");
15451 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15452 cp_lexer_consume_token (parser->lexer);
15453 return;
15456 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
15457 void *p = obstack_alloc (&declarator_obstack, 0);
15459 tree attributes = NULL_TREE;
15461 /* Conditionally, allow attributes to precede a linkage specification. */
15462 if (token1->keyword == RID_ATTRIBUTE)
15464 cp_lexer_save_tokens (parser->lexer);
15465 attributes = cp_parser_attributes_opt (parser);
15466 cp_token *t1 = cp_lexer_peek_token (parser->lexer);
15467 cp_token *t2 = (t1->type == CPP_EOF
15468 ? t1 : cp_lexer_peek_nth_token (parser->lexer, 2));
15469 if (t1->keyword == RID_EXTERN
15470 && cp_parser_is_pure_string_literal (t2))
15472 cp_lexer_commit_tokens (parser->lexer);
15473 /* We might have already been here. */
15474 if (!c_dialect_objc ())
15476 location_t where = get_finish (t2->location);
15477 warning_at (token1->location, OPT_Wattributes, "attributes are"
15478 " not permitted in this position");
15479 where = linemap_position_for_loc_and_offset (line_table,
15480 where, 1);
15481 inform (where, "attributes may be inserted here");
15482 attributes = NULL_TREE;
15484 token1 = t1;
15485 token2 = t2;
15487 else
15489 cp_lexer_rollback_tokens (parser->lexer);
15490 attributes = NULL_TREE;
15493 /* If we already had some attributes, and we've added more, then prepend.
15494 Otherwise attributes just contains any that we just read. */
15495 if (prefix_attrs)
15497 if (attributes)
15498 TREE_CHAIN (prefix_attrs) = attributes;
15499 attributes = prefix_attrs;
15502 /* If the next token is `extern' and the following token is a string
15503 literal, then we have a linkage specification. */
15504 if (token1->keyword == RID_EXTERN
15505 && cp_parser_is_pure_string_literal (token2))
15506 cp_parser_linkage_specification (parser, attributes);
15507 /* If the next token is `template', then we have either a template
15508 declaration, an explicit instantiation, or an explicit
15509 specialization. */
15510 else if (token1->keyword == RID_TEMPLATE)
15512 /* `template <>' indicates a template specialization. */
15513 if (token2->type == CPP_LESS
15514 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
15516 if (require_name_decl_p)
15518 auto_diagnostic_group d;
15519 cp_token *token3 = cp_lexer_peek_nth_token (parser->lexer, 3);
15520 location_t loc = make_location (token1, token1, token3);
15521 error_at (loc, "explicit specializations are not permitted here");
15522 if (parser->in_unbraced_export_declaration_p)
15523 inform (loc, "a specialization is always exported alongside "
15524 "its primary template");
15526 cp_parser_explicit_specialization (parser);
15528 /* `template <' indicates a template declaration. */
15529 else if (token2->type == CPP_LESS)
15530 cp_parser_template_declaration (parser, /*member_p=*/false);
15531 /* Anything else must be an explicit instantiation. */
15532 else
15534 if (require_name_decl_p)
15535 error_at (token1->location,
15536 "explicit instantiations are not permitted here");
15537 cp_parser_explicit_instantiation (parser);
15540 /* If the next token is `export', it's new-style modules or
15541 old-style template. */
15542 else if (token1->keyword == RID_EXPORT)
15544 if (!modules_p ())
15545 cp_parser_template_declaration (parser, /*member_p=*/false);
15546 else
15548 /* We check for nested exports in cp_parser_module_export. */
15549 if (require_name_decl_p
15550 && !parser->in_unbraced_export_declaration_p)
15551 error_at (token1->location,
15552 "export-declarations are not permitted here");
15553 cp_parser_module_export (parser);
15556 else if (cp_token_is_module_directive (token1))
15558 bool exporting = token1->keyword == RID__EXPORT;
15559 cp_token *next = exporting ? token2 : token1;
15560 if (exporting)
15561 cp_lexer_consume_token (parser->lexer);
15562 // In module purview this will be ill-formed.
15563 auto state = (!named_module_p () ? MP_NOT_MODULE
15564 : module_purview_p () ? MP_PURVIEW
15565 : MP_GLOBAL);
15566 if (next->keyword == RID__MODULE)
15567 cp_parser_module_declaration (parser, state, exporting);
15568 else
15569 cp_parser_import_declaration (parser, state, exporting);
15571 /* If the next token is `extern', 'static' or 'inline' and the one
15572 after that is `template', we have a GNU extended explicit
15573 instantiation directive. */
15574 else if (cp_parser_allow_gnu_extensions_p (parser)
15575 && token2->keyword == RID_TEMPLATE
15576 && (token1->keyword == RID_EXTERN
15577 || token1->keyword == RID_STATIC
15578 || token1->keyword == RID_INLINE))
15579 cp_parser_explicit_instantiation (parser);
15580 /* If the next token is `namespace', check for a named or unnamed
15581 namespace definition. */
15582 else if (token1->keyword == RID_NAMESPACE
15583 && (/* A named namespace definition. */
15584 (token2->type == CPP_NAME
15585 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
15586 != CPP_EQ))
15587 || (token2->type == CPP_OPEN_SQUARE
15588 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
15589 == CPP_OPEN_SQUARE)
15590 /* An unnamed namespace definition. */
15591 || token2->type == CPP_OPEN_BRACE
15592 || token2->keyword == RID_ATTRIBUTE))
15593 cp_parser_namespace_definition (parser);
15594 /* An inline (associated) namespace definition. */
15595 else if (token2->keyword == RID_NAMESPACE
15596 && token1->keyword == RID_INLINE)
15597 cp_parser_namespace_definition (parser);
15598 /* Objective-C++ declaration/definition. */
15599 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1->keyword))
15600 cp_parser_objc_declaration (parser, attributes);
15601 else if (c_dialect_objc ()
15602 && token1->keyword == RID_ATTRIBUTE
15603 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
15604 cp_parser_objc_declaration (parser, attributes);
15605 /* At this point we may have a template declared by a concept
15606 introduction. */
15607 else if (flag_concepts
15608 && cp_parser_template_declaration_after_export (parser,
15609 /*member_p=*/false))
15610 /* We did. */;
15611 else
15612 /* Try to parse a block-declaration, or a function-definition. */
15613 cp_parser_block_declaration (parser, /*statement_p=*/false);
15615 /* Free any declarators allocated. */
15616 obstack_free (&declarator_obstack, p);
15619 /* Parse a namespace-scope declaration. */
15621 static void
15622 cp_parser_toplevel_declaration (cp_parser* parser)
15624 cp_token *token = cp_lexer_peek_token (parser->lexer);
15626 if (token->type == CPP_PRAGMA)
15627 /* A top-level declaration can consist solely of a #pragma. A
15628 nested declaration cannot, so this is done here and not in
15629 cp_parser_declaration. (A #pragma at block scope is
15630 handled in cp_parser_statement.) */
15631 cp_parser_pragma (parser, pragma_external, NULL);
15632 else
15633 /* Parse the declaration itself. */
15634 cp_parser_declaration (parser, NULL_TREE);
15637 /* Parse a block-declaration.
15639 block-declaration:
15640 simple-declaration
15641 asm-definition
15642 namespace-alias-definition
15643 using-declaration
15644 using-directive
15646 GNU Extension:
15648 block-declaration:
15649 __extension__ block-declaration
15651 C++0x Extension:
15653 block-declaration:
15654 static_assert-declaration
15656 If STATEMENT_P is TRUE, then this block-declaration is occurring as
15657 part of a declaration-statement. */
15659 static void
15660 cp_parser_block_declaration (cp_parser *parser,
15661 bool statement_p)
15663 int saved_pedantic;
15665 /* Check for the `__extension__' keyword. */
15666 if (cp_parser_extension_opt (parser, &saved_pedantic))
15668 /* Parse the qualified declaration. */
15669 cp_parser_block_declaration (parser, statement_p);
15670 /* Restore the PEDANTIC flag. */
15671 pedantic = saved_pedantic;
15673 return;
15676 /* Peek at the next token to figure out which kind of declaration is
15677 present. */
15678 cp_token *token1 = cp_lexer_peek_token (parser->lexer);
15680 /* If the next keyword is `asm', we have an asm-definition. */
15681 if (token1->keyword == RID_ASM)
15683 if (statement_p)
15684 cp_parser_commit_to_tentative_parse (parser);
15685 cp_parser_asm_definition (parser);
15687 /* If the next keyword is `namespace', we have a
15688 namespace-alias-definition. */
15689 else if (token1->keyword == RID_NAMESPACE)
15690 cp_parser_namespace_alias_definition (parser);
15691 /* If the next keyword is `using', we have a
15692 using-declaration, a using-directive, or an alias-declaration. */
15693 else if (token1->keyword == RID_USING)
15695 cp_token *token2;
15697 if (statement_p)
15698 cp_parser_commit_to_tentative_parse (parser);
15699 /* If the token after `using' is `namespace', then we have a
15700 using-directive. */
15701 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
15702 if (token2->keyword == RID_NAMESPACE)
15703 cp_parser_using_directive (parser);
15704 else if (token2->keyword == RID_ENUM)
15705 cp_parser_using_enum (parser);
15706 /* If the second token after 'using' is '=', then we have an
15707 alias-declaration. */
15708 else if (cxx_dialect >= cxx11
15709 && token2->type == CPP_NAME
15710 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
15711 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
15712 cp_parser_alias_declaration (parser);
15713 /* Otherwise, it's a using-declaration. */
15714 else
15715 cp_parser_using_declaration (parser,
15716 /*access_declaration_p=*/false);
15718 /* If the next keyword is `__label__' we have a misplaced label
15719 declaration. */
15720 else if (token1->keyword == RID_LABEL)
15722 cp_lexer_consume_token (parser->lexer);
15723 error_at (token1->location, "%<__label__%> not at the beginning of a block");
15724 cp_parser_skip_to_end_of_statement (parser);
15725 /* If the next token is now a `;', consume it. */
15726 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15727 cp_lexer_consume_token (parser->lexer);
15729 /* If the next token is `static_assert' we have a static assertion. */
15730 else if (token1->keyword == RID_STATIC_ASSERT)
15731 cp_parser_static_assert (parser, /*member_p=*/false);
15732 else
15734 size_t attr_idx = cp_parser_skip_std_attribute_spec_seq (parser, 1);
15735 cp_token *after_attr = NULL;
15736 if (attr_idx != 1)
15737 after_attr = cp_lexer_peek_nth_token (parser->lexer, attr_idx);
15738 /* If the next tokens after attributes is `using namespace', then we have
15739 a using-directive. */
15740 if (after_attr
15741 && after_attr->keyword == RID_USING
15742 && cp_lexer_nth_token_is_keyword (parser->lexer, attr_idx + 1,
15743 RID_NAMESPACE))
15745 if (statement_p)
15746 cp_parser_commit_to_tentative_parse (parser);
15747 cp_parser_using_directive (parser);
15749 /* If the next token after attributes is `asm', then we have
15750 an asm-definition. */
15751 else if (after_attr && after_attr->keyword == RID_ASM)
15753 if (statement_p)
15754 cp_parser_commit_to_tentative_parse (parser);
15755 cp_parser_asm_definition (parser);
15757 /* Anything else must be a simple-declaration. */
15758 else
15759 cp_parser_simple_declaration (parser, !statement_p,
15760 /*maybe_range_for_decl*/NULL);
15764 /* Parse a simple-declaration.
15766 simple-declaration:
15767 decl-specifier-seq [opt] init-declarator-list [opt] ;
15768 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
15769 brace-or-equal-initializer ;
15771 init-declarator-list:
15772 init-declarator
15773 init-declarator-list , init-declarator
15775 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
15776 function-definition as a simple-declaration.
15778 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
15779 parsed declaration if it is an uninitialized single declarator not followed
15780 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
15781 if present, will not be consumed. */
15783 static void
15784 cp_parser_simple_declaration (cp_parser* parser,
15785 bool function_definition_allowed_p,
15786 tree *maybe_range_for_decl)
15788 cp_decl_specifier_seq decl_specifiers;
15789 int declares_class_or_enum;
15790 bool saw_declarator;
15791 location_t comma_loc = UNKNOWN_LOCATION;
15792 location_t init_loc = UNKNOWN_LOCATION;
15794 if (maybe_range_for_decl)
15795 *maybe_range_for_decl = NULL_TREE;
15797 /* Defer access checks until we know what is being declared; the
15798 checks for names appearing in the decl-specifier-seq should be
15799 done as if we were in the scope of the thing being declared. */
15800 push_deferring_access_checks (dk_deferred);
15802 /* Parse the decl-specifier-seq. We have to keep track of whether
15803 or not the decl-specifier-seq declares a named class or
15804 enumeration type, since that is the only case in which the
15805 init-declarator-list is allowed to be empty.
15807 [dcl.dcl]
15809 In a simple-declaration, the optional init-declarator-list can be
15810 omitted only when declaring a class or enumeration, that is when
15811 the decl-specifier-seq contains either a class-specifier, an
15812 elaborated-type-specifier, or an enum-specifier. */
15813 cp_parser_decl_specifier_seq (parser,
15814 CP_PARSER_FLAGS_OPTIONAL,
15815 &decl_specifiers,
15816 &declares_class_or_enum);
15817 /* We no longer need to defer access checks. */
15818 stop_deferring_access_checks ();
15820 cp_omp_declare_simd_data odsd;
15821 if (decl_specifiers.attributes && (flag_openmp || flag_openmp_simd))
15822 cp_parser_handle_directive_omp_attributes (parser,
15823 &decl_specifiers.attributes,
15824 &odsd, true);
15826 /* In a block scope, a valid declaration must always have a
15827 decl-specifier-seq. By not trying to parse declarators, we can
15828 resolve the declaration/expression ambiguity more quickly. */
15829 if (!function_definition_allowed_p
15830 && !decl_specifiers.any_specifiers_p)
15832 cp_parser_error (parser, "expected declaration");
15833 goto done;
15836 /* If the next two tokens are both identifiers, the code is
15837 erroneous. The usual cause of this situation is code like:
15839 T t;
15841 where "T" should name a type -- but does not. */
15842 if (!decl_specifiers.any_type_specifiers_p
15843 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15845 /* If parsing tentatively, we should commit; we really are
15846 looking at a declaration. */
15847 cp_parser_commit_to_tentative_parse (parser);
15848 /* Give up. */
15849 goto done;
15852 cp_parser_maybe_commit_to_declaration (parser, &decl_specifiers);
15854 /* Look for C++17 decomposition declaration. */
15855 for (size_t n = 1; ; n++)
15856 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
15857 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
15858 continue;
15859 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
15860 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
15861 && decl_specifiers.any_specifiers_p)
15863 tree decl
15864 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
15865 maybe_range_for_decl,
15866 &init_loc);
15868 /* The next token should be either a `,' or a `;'. */
15869 cp_token *token = cp_lexer_peek_token (parser->lexer);
15870 /* If it's a `;', we are done. */
15871 if (token->type == CPP_SEMICOLON)
15872 goto finish;
15873 else if (maybe_range_for_decl)
15875 if (*maybe_range_for_decl == NULL_TREE)
15876 *maybe_range_for_decl = error_mark_node;
15877 goto finish;
15879 /* Anything else is an error. */
15880 else
15882 /* If we have already issued an error message we don't need
15883 to issue another one. */
15884 if ((decl != error_mark_node
15885 && DECL_INITIAL (decl) != error_mark_node)
15886 || cp_parser_uncommitted_to_tentative_parse_p (parser))
15887 cp_parser_error (parser, "expected %<;%>");
15888 /* Skip tokens until we reach the end of the statement. */
15889 cp_parser_skip_to_end_of_statement (parser);
15890 /* If the next token is now a `;', consume it. */
15891 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15892 cp_lexer_consume_token (parser->lexer);
15893 goto done;
15896 else
15897 break;
15899 tree last_type;
15900 bool auto_specifier_p;
15901 /* NULL_TREE if both variable and function declaration are allowed,
15902 error_mark_node if function declaration are not allowed and
15903 a FUNCTION_DECL that should be diagnosed if it is followed by
15904 variable declarations. */
15905 tree auto_function_declaration;
15907 last_type = NULL_TREE;
15908 auto_specifier_p
15909 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
15910 auto_function_declaration = NULL_TREE;
15912 /* Keep going until we hit the `;' at the end of the simple
15913 declaration. */
15914 saw_declarator = false;
15915 while (cp_lexer_next_token_is_not (parser->lexer,
15916 CPP_SEMICOLON))
15918 cp_token *token;
15919 bool function_definition_p;
15920 tree decl;
15921 tree auto_result = NULL_TREE;
15923 if (saw_declarator)
15925 /* If we are processing next declarator, comma is expected */
15926 token = cp_lexer_peek_token (parser->lexer);
15927 gcc_assert (token->type == CPP_COMMA);
15928 cp_lexer_consume_token (parser->lexer);
15929 if (maybe_range_for_decl)
15931 *maybe_range_for_decl = error_mark_node;
15932 if (comma_loc == UNKNOWN_LOCATION)
15933 comma_loc = token->location;
15936 else
15937 saw_declarator = true;
15939 /* Parse the init-declarator. */
15940 decl = cp_parser_init_declarator (parser,
15941 CP_PARSER_FLAGS_NONE,
15942 &decl_specifiers,
15943 /*checks=*/NULL,
15944 function_definition_allowed_p,
15945 /*member_p=*/false,
15946 declares_class_or_enum,
15947 &function_definition_p,
15948 maybe_range_for_decl,
15949 &init_loc,
15950 &auto_result);
15951 const bool fndecl_p = TREE_CODE (decl) == FUNCTION_DECL;
15952 /* If an error occurred while parsing tentatively, exit quickly.
15953 (That usually happens when in the body of a function; each
15954 statement is treated as a declaration-statement until proven
15955 otherwise.) */
15956 if (cp_parser_error_occurred (parser))
15957 goto done;
15959 if (auto_specifier_p && cxx_dialect >= cxx14)
15961 /* If the init-declarator-list contains more than one
15962 init-declarator, they shall all form declarations of
15963 variables. */
15964 if (auto_function_declaration == NULL_TREE)
15965 auto_function_declaration = fndecl_p ? decl : error_mark_node;
15966 else if (fndecl_p || auto_function_declaration != error_mark_node)
15968 error_at (decl_specifiers.locations[ds_type_spec],
15969 "non-variable %qD in declaration with more than one "
15970 "declarator with placeholder type",
15971 fndecl_p ? decl : auto_function_declaration);
15972 auto_function_declaration = error_mark_node;
15976 if (auto_result
15977 && (!processing_template_decl || !type_uses_auto (auto_result)))
15979 if (last_type
15980 && last_type != error_mark_node
15981 && !same_type_p (auto_result, last_type))
15983 /* If the list of declarators contains more than one declarator,
15984 the type of each declared variable is determined as described
15985 above. If the type deduced for the template parameter U is not
15986 the same in each deduction, the program is ill-formed. */
15987 error_at (decl_specifiers.locations[ds_type_spec],
15988 "inconsistent deduction for %qT: %qT and then %qT",
15989 decl_specifiers.type, last_type, auto_result);
15990 last_type = error_mark_node;
15992 else
15993 last_type = auto_result;
15996 /* Handle function definitions specially. */
15997 if (function_definition_p)
15999 /* If the next token is a `,', then we are probably
16000 processing something like:
16002 void f() {}, *p;
16004 which is erroneous. */
16005 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16007 cp_token *token = cp_lexer_peek_token (parser->lexer);
16008 error_at (token->location,
16009 "mixing"
16010 " declarations and function-definitions is forbidden");
16012 /* Otherwise, we're done with the list of declarators. */
16013 else
16015 pop_deferring_access_checks ();
16016 cp_finalize_omp_declare_simd (parser, &odsd);
16017 return;
16020 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
16021 *maybe_range_for_decl = decl;
16022 /* The next token should be either a `,' or a `;'. */
16023 token = cp_lexer_peek_token (parser->lexer);
16024 /* If it's a `,', there are more declarators to come. */
16025 if (token->type == CPP_COMMA)
16026 /* will be consumed next time around */;
16027 /* If it's a `;', we are done. */
16028 else if (token->type == CPP_SEMICOLON)
16029 break;
16030 else if (maybe_range_for_decl)
16032 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
16033 permerror (decl_specifiers.locations[ds_type_spec],
16034 "types may not be defined in a for-range-declaration");
16035 break;
16037 /* Anything else is an error. */
16038 else
16040 /* If we have already issued an error message we don't need
16041 to issue another one. */
16042 if ((decl != error_mark_node
16043 /* grokfndecl sets DECL_INITIAL to error_mark_node for
16044 functions. */
16045 && (fndecl_p || DECL_INITIAL (decl) != error_mark_node))
16046 || cp_parser_uncommitted_to_tentative_parse_p (parser))
16047 cp_parser_error (parser, "expected %<,%> or %<;%>");
16048 /* Skip tokens until we reach the end of the statement. */
16049 cp_parser_skip_to_end_of_statement (parser);
16050 /* If the next token is now a `;', consume it. */
16051 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16052 cp_lexer_consume_token (parser->lexer);
16053 goto done;
16055 /* After the first time around, a function-definition is not
16056 allowed -- even if it was OK at first. For example:
16058 int i, f() {}
16060 is not valid. */
16061 function_definition_allowed_p = false;
16064 /* Issue an error message if no declarators are present, and the
16065 decl-specifier-seq does not itself declare a class or
16066 enumeration: [dcl.dcl]/3. */
16067 if (!saw_declarator)
16069 if (cp_parser_declares_only_class_p (parser))
16071 if (!declares_class_or_enum
16072 && decl_specifiers.type
16073 && OVERLOAD_TYPE_P (decl_specifiers.type))
16074 /* Ensure an error is issued anyway when finish_decltype_type,
16075 called via cp_parser_decl_specifier_seq, returns a class or
16076 an enumeration (c++/51786). */
16077 decl_specifiers.type = NULL_TREE;
16078 shadow_tag (&decl_specifiers);
16080 /* Perform any deferred access checks. */
16081 perform_deferred_access_checks (tf_warning_or_error);
16084 /* Consume the `;'. */
16085 finish:
16086 if (!maybe_range_for_decl)
16087 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16088 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16090 if (init_loc != UNKNOWN_LOCATION)
16091 error_at (init_loc, "initializer in range-based %<for%> loop");
16092 if (comma_loc != UNKNOWN_LOCATION)
16093 error_at (comma_loc,
16094 "multiple declarations in range-based %<for%> loop");
16097 done:
16098 pop_deferring_access_checks ();
16099 cp_finalize_omp_declare_simd (parser, &odsd);
16102 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
16103 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
16104 initializer ; */
16106 static tree
16107 cp_parser_decomposition_declaration (cp_parser *parser,
16108 cp_decl_specifier_seq *decl_specifiers,
16109 tree *maybe_range_for_decl,
16110 location_t *init_loc)
16112 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
16113 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
16114 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
16116 /* Parse the identifier-list. */
16117 auto_vec<cp_expr, 10> v;
16118 bool attr_diagnosed = false;
16119 int first_attr = -1;
16120 unsigned int cnt = 0;
16121 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
16122 while (true)
16124 cp_expr e = cp_parser_identifier (parser);
16125 if (e.get_value () == error_mark_node)
16126 break;
16127 tree attr = NULL_TREE;
16128 if (cp_next_tokens_can_be_std_attribute_p (parser))
16130 if (cxx_dialect >= cxx17 && cxx_dialect < cxx26 && !attr_diagnosed)
16132 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
16133 OPT_Wc__26_extensions,
16134 "structured bindings with attributed identifiers "
16135 "only available with %<-std=c++2c%> or "
16136 "%<-std=gnu++2c%>");
16137 attr_diagnosed = true;
16139 attr = cp_parser_std_attribute_spec_seq (parser);
16140 if (attr == error_mark_node)
16141 attr = NULL_TREE;
16142 if (attr && first_attr == -1)
16143 first_attr = v.length ();
16145 v.safe_push (e);
16146 ++cnt;
16147 if (first_attr != -1)
16148 v.safe_push (attr);
16149 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16150 break;
16151 cp_lexer_consume_token (parser->lexer);
16154 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
16155 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
16157 end_loc = UNKNOWN_LOCATION;
16158 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
16159 false);
16160 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
16161 cp_lexer_consume_token (parser->lexer);
16162 else
16164 cp_parser_skip_to_end_of_statement (parser);
16165 return error_mark_node;
16169 if (cxx_dialect < cxx17)
16170 pedwarn (loc, OPT_Wc__17_extensions,
16171 "structured bindings only available with "
16172 "%<-std=c++17%> or %<-std=gnu++17%>");
16174 tree pushed_scope;
16175 cp_declarator *declarator = make_declarator (cdk_decomp);
16176 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
16177 declarator->id_loc = loc;
16178 if (ref_qual != REF_QUAL_NONE)
16179 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
16180 ref_qual == REF_QUAL_RVALUE,
16181 NULL_TREE);
16182 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
16183 NULL_TREE, decl_specifiers->attributes,
16184 &pushed_scope);
16185 tree orig_decl = decl;
16187 unsigned int i;
16188 cp_expr e;
16189 cp_decl_specifier_seq decl_specs;
16190 clear_decl_specs (&decl_specs);
16191 decl_specs.type = make_auto ();
16192 if (decl_specifiers->storage_class == sc_static)
16193 decl_specs.storage_class = sc_static;
16194 tree prev = decl;
16195 FOR_EACH_VEC_ELT (v, i, e)
16197 if (i == 0)
16198 declarator = make_id_declarator (NULL_TREE, e.get_value (),
16199 sfk_none, e.get_location ());
16200 else
16202 declarator->u.id.unqualified_name = e.get_value ();
16203 declarator->id_loc = e.get_location ();
16205 tree elt_pushed_scope;
16206 tree attr = NULL_TREE;
16207 if (first_attr != -1 && i >= (unsigned) first_attr)
16208 attr = v[++i].get_value ();
16209 tree decl2 = start_decl (declarator, &decl_specs, SD_DECOMPOSITION,
16210 NULL_TREE, attr, &elt_pushed_scope);
16211 if (decl2 == error_mark_node)
16212 decl = error_mark_node;
16213 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
16215 /* Ensure we've diagnosed redeclaration if we aren't creating
16216 a new VAR_DECL. */
16217 gcc_assert (errorcount);
16218 decl = error_mark_node;
16220 else
16221 prev = decl2;
16222 if (elt_pushed_scope)
16223 pop_scope (elt_pushed_scope);
16226 if (v.is_empty ())
16228 error_at (loc, "empty structured binding declaration");
16229 decl = error_mark_node;
16232 if (maybe_range_for_decl == NULL
16233 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16235 bool non_constant_p = false, is_direct_init = false;
16236 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
16237 tree initializer = cp_parser_initializer (parser, &is_direct_init,
16238 &non_constant_p);
16239 if (initializer == NULL_TREE
16240 || (TREE_CODE (initializer) == TREE_LIST
16241 && TREE_CHAIN (initializer))
16242 || (is_direct_init
16243 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
16244 && CONSTRUCTOR_NELTS (initializer) != 1))
16246 error_at (loc, "invalid initializer for structured binding "
16247 "declaration");
16248 initializer = error_mark_node;
16251 if (decl != error_mark_node)
16253 cp_decomp decomp = { prev, cnt };
16254 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
16255 (is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT),
16256 &decomp);
16257 cp_finish_decomp (decl, &decomp);
16260 else if (decl != error_mark_node)
16262 *maybe_range_for_decl = prev;
16263 cp_decomp decomp = { prev, cnt };
16264 /* Ensure DECL_VALUE_EXPR is created for all the decls but
16265 the underlying DECL. */
16266 cp_finish_decomp (decl, &decomp);
16269 if (pushed_scope)
16270 pop_scope (pushed_scope);
16272 if (decl == error_mark_node && DECL_P (orig_decl))
16274 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
16275 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
16278 return decl;
16281 /* Names of storage classes. */
16283 static const char *const
16284 cp_storage_class_name[] = {
16285 "", "auto", "register", "static", "extern", "mutable"
16288 /* Parse a decl-specifier-seq.
16290 decl-specifier-seq:
16291 decl-specifier-seq [opt] decl-specifier
16292 decl-specifier attribute-specifier-seq [opt] (C++11)
16294 decl-specifier:
16295 storage-class-specifier
16296 type-specifier
16297 function-specifier
16298 friend
16299 typedef
16301 GNU Extension:
16303 decl-specifier:
16304 attributes
16306 Concepts Extension:
16308 decl-specifier:
16309 concept
16311 Set *DECL_SPECS to a representation of the decl-specifier-seq.
16313 The parser flags FLAGS is used to control type-specifier parsing.
16315 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
16316 flags:
16318 1: one of the decl-specifiers is an elaborated-type-specifier
16319 (i.e., a type declaration)
16320 2: one of the decl-specifiers is an enum-specifier or a
16321 class-specifier (i.e., a type definition)
16325 static void
16326 cp_parser_decl_specifier_seq (cp_parser* parser,
16327 cp_parser_flags flags,
16328 cp_decl_specifier_seq *decl_specs,
16329 int* declares_class_or_enum)
16331 bool constructor_possible_p = !parser->in_declarator_p;
16332 bool found_decl_spec = false;
16333 cp_token *start_token = NULL;
16334 cp_decl_spec ds;
16336 /* Clear DECL_SPECS. */
16337 clear_decl_specs (decl_specs);
16339 /* Assume no class or enumeration type is declared. */
16340 *declares_class_or_enum = 0;
16342 /* Keep a token that additionally will be used for diagnostics. */
16343 cp_token *first_specifier = NULL;
16344 /* Keep reading specifiers until there are no more to read. */
16345 while (true)
16347 bool constructor_p;
16348 cp_token *token;
16349 ds = ds_last;
16351 /* Peek at the next token. */
16352 token = cp_lexer_peek_token (parser->lexer);
16354 /* Save the first token of the decl spec list for error
16355 reporting. */
16356 if (!start_token)
16357 start_token = token;
16358 /* Handle attributes. */
16359 if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) == 0
16360 && cp_next_tokens_can_be_attribute_p (parser))
16362 /* Parse the attributes. */
16363 tree attrs = cp_parser_attributes_opt (parser);
16365 /* In a sequence of declaration specifiers, c++11 attributes
16366 appertain to the type that precede them. In that case
16367 [dcl.spec]/1 says:
16369 The attribute-specifier-seq affects the type only for
16370 the declaration it appears in, not other declarations
16371 involving the same type.
16373 But for now let's force the user to position the
16374 attribute either at the beginning of the declaration or
16375 after the declarator-id, which would clearly mean that it
16376 applies to the declarator. */
16377 if (cxx11_attribute_p (attrs))
16379 if (!found_decl_spec)
16380 /* The c++11 attribute is at the beginning of the
16381 declaration. It appertains to the entity being
16382 declared. */;
16383 else
16385 if (find_contract (attrs))
16387 diagnose_misapplied_contracts (attrs);
16388 attrs = NULL_TREE;
16390 else if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
16392 /* This is an attribute following a
16393 class-specifier. */
16394 if (decl_specs->type_definition_p)
16395 warn_misplaced_attr_for_class_type (token->location,
16396 decl_specs->type);
16397 attrs = NULL_TREE;
16399 else
16401 decl_specs->std_attributes
16402 = attr_chainon (decl_specs->std_attributes, attrs);
16403 if (decl_specs->locations[ds_std_attribute] == 0)
16404 decl_specs->locations[ds_std_attribute] = token->location;
16406 continue;
16410 decl_specs->attributes
16411 = attr_chainon (decl_specs->attributes, attrs);
16412 if (decl_specs->locations[ds_attribute] == 0)
16413 decl_specs->locations[ds_attribute] = token->location;
16414 continue;
16416 /* We know by this point that the token is not part of an attribute. */
16417 if (!first_specifier)
16418 first_specifier = token;
16419 /* Special case for "this" specifier, indicating a parm is an xobj parm.
16420 The "this" specifier must be the first specifier in the declaration,
16421 after any attributes. */
16422 if (token->keyword == RID_THIS && (flags & CP_PARSER_FLAGS_PARAMETER))
16424 cp_lexer_consume_token (parser->lexer);
16425 if (token != first_specifier)
16427 /* Don't emit diagnostics if we have already seen "this",
16428 leave it for set_and_check_decl_spec_loc. */
16429 if (decl_specs->locations[ds_this] == 0)
16431 auto_diagnostic_group d;
16432 gcc_rich_location richloc (token->location);
16433 /* Works, need to add tests for it though. */
16434 richloc.add_fixit_remove ();
16435 richloc.add_fixit_insert_before (first_specifier->location,
16436 "this ");
16437 error_at (&richloc,
16438 "%<this%> must be the first specifier "
16439 "in a parameter declaration");
16442 set_and_check_decl_spec_loc (decl_specs, ds_this, token);
16443 continue;
16446 /* Assume we will find a decl-specifier keyword. */
16447 found_decl_spec = true;
16448 /* If the next token is an appropriate keyword, we can simply
16449 add it to the list. */
16450 switch (token->keyword)
16452 /* decl-specifier:
16453 friend
16454 constexpr
16455 constinit */
16456 case RID_FRIEND:
16457 if (!at_class_scope_p ())
16459 gcc_rich_location richloc (token->location);
16460 richloc.add_fixit_remove ();
16461 error_at (&richloc, "%<friend%> used outside of class");
16462 cp_lexer_purge_token (parser->lexer);
16464 else
16466 ds = ds_friend;
16467 /* Consume the token. */
16468 cp_lexer_consume_token (parser->lexer);
16470 break;
16472 case RID_CONSTEXPR:
16473 ds = ds_constexpr;
16474 cp_lexer_consume_token (parser->lexer);
16475 break;
16477 case RID_CONSTINIT:
16478 ds = ds_constinit;
16479 cp_lexer_consume_token (parser->lexer);
16480 break;
16482 case RID_CONSTEVAL:
16483 ds = ds_consteval;
16484 cp_lexer_consume_token (parser->lexer);
16485 break;
16487 case RID_CONCEPT:
16488 ds = ds_concept;
16489 cp_lexer_consume_token (parser->lexer);
16491 if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
16492 break;
16494 /* Warn for concept as a decl-specifier. We'll rewrite these as
16495 concept declarations later. */
16496 if (!flag_concepts_ts)
16498 cp_token *next = cp_lexer_peek_token (parser->lexer);
16499 if (next->keyword == RID_BOOL)
16500 permerror (next->location, "the %<bool%> keyword is not "
16501 "allowed in a C++20 concept definition");
16502 else
16503 error_at (token->location, "C++20 concept definition syntax "
16504 "is %<concept <name> = <expr>%>");
16507 /* In C++20 a concept definition is just 'concept name = expr;'
16508 Support that syntax as a TS extension by pretending we've seen
16509 the 'bool' specifier. */
16510 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
16511 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ)
16512 && !decl_specs->any_type_specifiers_p)
16514 cp_parser_set_decl_spec_type (decl_specs, boolean_type_node,
16515 token, /*type_definition*/false);
16516 decl_specs->any_type_specifiers_p = true;
16518 break;
16520 /* function-specifier:
16521 inline
16522 virtual
16523 explicit */
16524 case RID_INLINE:
16525 case RID_VIRTUAL:
16526 case RID_EXPLICIT:
16527 cp_parser_function_specifier_opt (parser, decl_specs);
16528 break;
16530 /* decl-specifier:
16531 typedef */
16532 case RID_TYPEDEF:
16533 ds = ds_typedef;
16534 /* Consume the token. */
16535 cp_lexer_consume_token (parser->lexer);
16537 if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
16538 break;
16540 /* A constructor declarator cannot appear in a typedef. */
16541 constructor_possible_p = false;
16542 /* The "typedef" keyword can only occur in a declaration; we
16543 may as well commit at this point. */
16544 cp_parser_commit_to_tentative_parse (parser);
16546 if (decl_specs->storage_class != sc_none)
16548 if (decl_specs->conflicting_specifiers_p)
16549 break;
16550 gcc_rich_location richloc (token->location);
16551 location_t oloc = decl_specs->locations[ds_storage_class];
16552 richloc.add_location_if_nearby (oloc);
16553 error_at (&richloc,
16554 "%<typedef%> specifier conflicts with %qs",
16555 cp_storage_class_name[decl_specs->storage_class]);
16556 decl_specs->conflicting_specifiers_p = true;
16558 break;
16560 /* storage-class-specifier:
16561 auto
16562 register
16563 static
16564 extern
16565 mutable
16567 GNU Extension:
16568 thread */
16569 case RID_AUTO:
16570 if (cxx_dialect == cxx98)
16572 /* Consume the token. */
16573 cp_lexer_consume_token (parser->lexer);
16575 /* Complain about `auto' as a storage specifier, if
16576 we're complaining about C++0x compatibility. */
16577 gcc_rich_location richloc (token->location);
16578 richloc.add_fixit_remove ();
16579 warning_at (&richloc, OPT_Wc__11_compat,
16580 "%<auto%> changes meaning in C++11; "
16581 "please remove it");
16583 /* Set the storage class anyway. */
16584 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
16585 token);
16587 else
16588 /* C++0x auto type-specifier. */
16589 found_decl_spec = false;
16590 break;
16592 case RID_REGISTER:
16593 case RID_STATIC:
16594 case RID_EXTERN:
16595 case RID_MUTABLE:
16596 /* Consume the token. */
16597 cp_lexer_consume_token (parser->lexer);
16598 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
16599 token);
16600 break;
16601 case RID_THREAD:
16602 /* Consume the token. */
16603 ds = ds_thread;
16604 cp_lexer_consume_token (parser->lexer);
16605 break;
16607 default:
16608 /* We did not yet find a decl-specifier yet. */
16609 found_decl_spec = false;
16610 break;
16613 if (found_decl_spec
16614 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
16615 && token->keyword != RID_CONSTEXPR)
16616 error ("%qD invalid in condition", ridpointers[token->keyword]);
16618 if (found_decl_spec
16619 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
16620 && token->keyword != RID_MUTABLE
16621 && token->keyword != RID_CONSTEXPR
16622 && token->keyword != RID_CONSTEVAL)
16624 if (token->keyword != RID_STATIC)
16625 error_at (token->location, "%qD invalid in lambda",
16626 ridpointers[token->keyword]);
16627 else if (cxx_dialect < cxx23)
16628 pedwarn (token->location, OPT_Wc__23_extensions,
16629 "%qD only valid in lambda with %<-std=c++23%> or "
16630 "%<-std=gnu++23%>", ridpointers[token->keyword]);
16633 if (ds != ds_last)
16634 set_and_check_decl_spec_loc (decl_specs, ds, token);
16636 /* Constructors are a special case. The `S' in `S()' is not a
16637 decl-specifier; it is the beginning of the declarator. */
16638 constructor_p
16639 = (!found_decl_spec
16640 && constructor_possible_p
16641 && (cp_parser_constructor_declarator_p
16642 (parser, flags, decl_spec_seq_has_spec_p (decl_specs,
16643 ds_friend))));
16645 /* If we don't have a DECL_SPEC yet, then we must be looking at
16646 a type-specifier. */
16647 if (!found_decl_spec && !constructor_p)
16649 int decl_spec_declares_class_or_enum;
16650 bool is_cv_qualifier;
16651 tree type_spec;
16653 if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
16654 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
16656 type_spec
16657 = cp_parser_type_specifier (parser, flags,
16658 decl_specs,
16659 /*is_declaration=*/true,
16660 &decl_spec_declares_class_or_enum,
16661 &is_cv_qualifier);
16662 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
16664 /* If this type-specifier referenced a user-defined type
16665 (a typedef, class-name, etc.), then we can't allow any
16666 more such type-specifiers henceforth.
16668 [dcl.spec]
16670 The longest sequence of decl-specifiers that could
16671 possibly be a type name is taken as the
16672 decl-specifier-seq of a declaration. The sequence shall
16673 be self-consistent as described below.
16675 [dcl.type]
16677 As a general rule, at most one type-specifier is allowed
16678 in the complete decl-specifier-seq of a declaration. The
16679 only exceptions are the following:
16681 -- const or volatile can be combined with any other
16682 type-specifier.
16684 -- signed or unsigned can be combined with char, long,
16685 short, or int.
16687 -- ..
16689 Example:
16691 typedef char* Pc;
16692 void g (const int Pc);
16694 Here, Pc is *not* part of the decl-specifier seq; it's
16695 the declarator. Therefore, once we see a type-specifier
16696 (other than a cv-qualifier), we forbid any additional
16697 user-defined types. We *do* still allow things like `int
16698 int' to be considered a decl-specifier-seq, and issue the
16699 error message later. */
16700 if (type_spec && !is_cv_qualifier)
16701 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
16702 /* A constructor declarator cannot follow a type-specifier. */
16703 if (type_spec)
16705 constructor_possible_p = false;
16706 found_decl_spec = true;
16707 if (!is_cv_qualifier)
16708 decl_specs->any_type_specifiers_p = true;
16710 if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) != 0)
16711 error_at (token->location, "type-specifier invalid in lambda");
16715 /* If we still do not have a DECL_SPEC, then there are no more
16716 decl-specifiers. */
16717 if (!found_decl_spec)
16718 break;
16720 if (decl_specs->std_attributes)
16722 error_at (decl_specs->locations[ds_std_attribute],
16723 "standard attributes in middle of decl-specifiers");
16724 inform (decl_specs->locations[ds_std_attribute],
16725 "standard attributes must precede the decl-specifiers to "
16726 "apply to the declaration, or follow them to apply to "
16727 "the type");
16730 decl_specs->any_specifiers_p = true;
16731 /* After we see one decl-specifier, further decl-specifiers are
16732 always optional. */
16733 flags |= CP_PARSER_FLAGS_OPTIONAL;
16736 /* Don't allow a friend specifier with a class definition. */
16737 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
16738 && (*declares_class_or_enum & 2))
16739 error_at (decl_specs->locations[ds_friend],
16740 "class definition may not be declared a friend");
16743 /* Parse an (optional) storage-class-specifier.
16745 storage-class-specifier:
16746 auto
16747 register
16748 static
16749 extern
16750 mutable
16752 GNU Extension:
16754 storage-class-specifier:
16755 thread
16757 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
16759 static tree
16760 cp_parser_storage_class_specifier_opt (cp_parser* parser)
16762 switch (cp_lexer_peek_token (parser->lexer)->keyword)
16764 case RID_AUTO:
16765 if (cxx_dialect != cxx98)
16766 return NULL_TREE;
16767 /* Fall through for C++98. */
16768 gcc_fallthrough ();
16770 case RID_REGISTER:
16771 case RID_STATIC:
16772 case RID_EXTERN:
16773 case RID_MUTABLE:
16774 case RID_THREAD:
16775 /* Consume the token. */
16776 return cp_lexer_consume_token (parser->lexer)->u.value;
16778 default:
16779 return NULL_TREE;
16783 /* Parse an (optional) function-specifier.
16785 function-specifier:
16786 inline
16787 virtual
16788 explicit
16790 C++20 Extension:
16791 explicit(constant-expression)
16793 Returns an IDENTIFIER_NODE corresponding to the keyword used.
16794 Updates DECL_SPECS, if it is non-NULL. */
16796 static tree
16797 cp_parser_function_specifier_opt (cp_parser* parser,
16798 cp_decl_specifier_seq *decl_specs)
16800 cp_token *token = cp_lexer_peek_token (parser->lexer);
16801 switch (token->keyword)
16803 case RID_INLINE:
16804 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
16805 break;
16807 case RID_VIRTUAL:
16808 /* 14.5.2.3 [temp.mem]
16810 A member function template shall not be virtual. */
16811 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
16812 && current_class_type)
16813 error_at (token->location, "templates may not be %<virtual%>");
16814 else
16815 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
16816 break;
16818 case RID_EXPLICIT:
16820 tree id = cp_lexer_consume_token (parser->lexer)->u.value;
16821 /* If we see '(', it's C++20 explicit(bool). */
16822 tree expr;
16823 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16825 matching_parens parens;
16826 parens.consume_open (parser);
16828 /* New types are not allowed in an explicit-specifier. */
16829 const char *saved_message
16830 = parser->type_definition_forbidden_message;
16831 parser->type_definition_forbidden_message
16832 = G_("types may not be defined in explicit-specifier");
16834 if (cxx_dialect < cxx20)
16835 pedwarn (token->location, OPT_Wc__20_extensions,
16836 "%<explicit(bool)%> only available with %<-std=c++20%> "
16837 "or %<-std=gnu++20%>");
16839 /* Parse the constant-expression. */
16840 expr = cp_parser_constant_expression (parser);
16842 /* Restore the saved message. */
16843 parser->type_definition_forbidden_message = saved_message;
16844 parens.require_close (parser);
16846 else
16847 /* The explicit-specifier explicit without a constant-expression is
16848 equivalent to the explicit-specifier explicit(true). */
16849 expr = boolean_true_node;
16851 /* [dcl.fct.spec]
16852 "the constant-expression, if supplied, shall be a contextually
16853 converted constant expression of type bool." */
16854 expr = build_explicit_specifier (expr, tf_warning_or_error);
16855 /* We could evaluate it -- mark the decl as appropriate. */
16856 if (expr == boolean_true_node)
16857 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
16858 else if (expr == boolean_false_node)
16859 /* Don't mark the decl as explicit. */;
16860 else if (decl_specs)
16861 /* The expression was value-dependent. Remember it so that we can
16862 substitute it later. */
16863 decl_specs->explicit_specifier = expr;
16864 return id;
16867 default:
16868 return NULL_TREE;
16871 /* Consume the token. */
16872 return cp_lexer_consume_token (parser->lexer)->u.value;
16875 /* Parse a linkage-specification.
16877 linkage-specification:
16878 extern string-literal { declaration-seq [opt] }
16879 extern string-literal name-declaration */
16881 static void
16882 cp_parser_linkage_specification (cp_parser* parser, tree prefix_attr)
16884 /* Look for the `extern' keyword. */
16885 cp_token *extern_token
16886 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
16888 /* Look for the string-literal. */
16889 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
16890 tree linkage;
16891 if (cxx_dialect >= cxx26)
16892 linkage = cp_parser_unevaluated_string_literal (parser);
16893 else
16894 linkage = cp_parser_string_literal (parser, /*translate=*/false,
16895 /*wide_ok=*/false);
16897 /* Transform the literal into an identifier. If the literal is a
16898 wide-character string, or contains embedded NULs, then we can't
16899 handle it as the user wants. */
16900 if (linkage == error_mark_node
16901 || strlen (TREE_STRING_POINTER (linkage))
16902 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
16904 cp_parser_error (parser, "invalid linkage-specification");
16905 /* Assume C++ linkage. */
16906 linkage = lang_name_cplusplus;
16908 else
16909 linkage = get_identifier (TREE_STRING_POINTER (linkage));
16911 /* We're now using the new linkage. */
16912 unsigned saved_module = module_kind;
16913 module_kind &= ~MK_ATTACH;
16914 push_lang_context (linkage);
16916 /* Preserve the location of the innermost linkage specification,
16917 tracking the locations of nested specifications via a local. */
16918 location_t saved_location
16919 = parser->innermost_linkage_specification_location;
16920 /* Construct a location ranging from the start of the "extern" to
16921 the end of the string-literal, with the caret at the start, e.g.:
16922 extern "C" {
16923 ^~~~~~~~~~
16925 parser->innermost_linkage_specification_location
16926 = make_location (extern_token->location,
16927 extern_token->location,
16928 get_finish (string_token->location));
16930 /* If the next token is a `{', then we're using the first
16931 production. */
16932 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16934 cp_ensure_no_omp_declare_simd (parser);
16935 cp_ensure_no_oacc_routine (parser);
16937 /* Consume the `{' token. */
16938 matching_braces braces;
16939 braces.consume_open (parser);
16940 /* Parse the declarations. */
16941 cp_parser_declaration_seq_opt (parser);
16942 /* Look for the closing `}'. */
16943 braces.require_close (parser);
16945 /* Otherwise, there's just one declaration. */
16946 else
16948 bool saved_in_unbraced_linkage_specification_p;
16950 saved_in_unbraced_linkage_specification_p
16951 = parser->in_unbraced_linkage_specification_p;
16952 parser->in_unbraced_linkage_specification_p = true;
16953 cp_parser_declaration (parser, prefix_attr);
16954 parser->in_unbraced_linkage_specification_p
16955 = saved_in_unbraced_linkage_specification_p;
16958 /* We're done with the linkage-specification. */
16959 pop_lang_context ();
16960 module_kind = saved_module;
16962 /* Restore location of parent linkage specification, if any. */
16963 parser->innermost_linkage_specification_location = saved_location;
16966 /* Parse a static_assert-declaration.
16968 static_assert-declaration:
16969 static_assert ( constant-expression , string-literal ) ;
16970 static_assert ( constant-expression ) ; (C++17)
16971 static_assert ( constant-expression, conditional-expression ) ; (C++26)
16973 If MEMBER_P, this static_assert is a class member. */
16975 static void
16976 cp_parser_static_assert (cp_parser *parser, bool member_p)
16978 cp_expr condition;
16979 location_t token_loc;
16980 tree message;
16982 /* Peek at the `static_assert' token so we can keep track of exactly
16983 where the static assertion started. */
16984 token_loc = cp_lexer_peek_token (parser->lexer)->location;
16986 /* Look for the `static_assert' keyword. */
16987 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
16988 RT_STATIC_ASSERT))
16989 return;
16991 /* We know we are in a static assertion; commit to any tentative
16992 parse. */
16993 if (cp_parser_parsing_tentatively (parser))
16994 cp_parser_commit_to_tentative_parse (parser);
16996 /* Parse the `(' starting the static assertion condition. */
16997 matching_parens parens;
16998 parens.require_open (parser);
17000 /* Parse the constant-expression. Allow a non-constant expression
17001 here in order to give better diagnostics in finish_static_assert. */
17002 condition
17003 = cp_parser_constant_expression (parser,
17004 /*allow_non_constant_p=*/true,
17005 /*non_constant_p=*/nullptr);
17007 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
17009 if (pedantic && cxx_dialect < cxx17)
17010 pedwarn (input_location, OPT_Wc__17_extensions,
17011 "%<static_assert%> without a message "
17012 "only available with %<-std=c++17%> or %<-std=gnu++17%>");
17013 /* Eat the ')' */
17014 cp_lexer_consume_token (parser->lexer);
17015 message = build_string (1, "");
17016 TREE_TYPE (message) = char_array_type_node;
17017 fix_string_type (message);
17019 else
17021 /* Parse the separating `,'. */
17022 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
17024 /* Parse the message expression. */
17025 bool string_lit = true;
17026 for (unsigned int i = 1; ; ++i)
17028 cp_token *tok = cp_lexer_peek_nth_token (parser->lexer, i);
17029 if (cp_parser_is_pure_string_literal (tok))
17030 continue;
17031 else if (tok->type == CPP_CLOSE_PAREN)
17032 break;
17033 string_lit = false;
17034 break;
17036 if (!string_lit)
17038 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
17039 if (cxx_dialect < cxx26)
17040 pedwarn (loc, OPT_Wc__26_extensions,
17041 "%<static_assert%> with non-string message only "
17042 "available with %<-std=c++2c%> or %<-std=gnu++2c%>");
17044 message = cp_parser_conditional_expression (parser);
17045 if (TREE_CODE (message) == STRING_CST)
17046 message = build1_loc (loc, PAREN_EXPR, TREE_TYPE (message),
17047 message);
17049 else if (cxx_dialect >= cxx26)
17050 message = cp_parser_unevaluated_string_literal (parser);
17051 else
17052 message = cp_parser_string_literal (parser, /*translate=*/false,
17053 /*wide_ok=*/true);
17055 /* A `)' completes the static assertion. */
17056 if (!parens.require_close (parser))
17057 cp_parser_skip_to_closing_parenthesis (parser,
17058 /*recovering=*/true,
17059 /*or_comma=*/false,
17060 /*consume_paren=*/true);
17063 /* A semicolon terminates the declaration. */
17064 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17066 /* Get the location for the static assertion. Use that of the
17067 condition if available, otherwise, use that of the "static_assert"
17068 token. */
17069 location_t assert_loc = condition.get_location ();
17070 if (assert_loc == UNKNOWN_LOCATION)
17071 assert_loc = token_loc;
17073 /* Complete the static assertion, which may mean either processing
17074 the static assert now or saving it for template instantiation. */
17075 finish_static_assert (condition, message, assert_loc, member_p,
17076 /*show_expr_p=*/false);
17079 /* Parse the expression in decltype ( expression ). */
17081 static tree
17082 cp_parser_decltype_expr (cp_parser *parser,
17083 bool &id_expression_or_member_access_p)
17085 cp_token *id_expr_start_token;
17086 tree expr;
17088 /* First, try parsing an id-expression. */
17089 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
17090 cp_parser_parse_tentatively (parser);
17091 expr = cp_parser_id_expression (parser,
17092 /*template_keyword_p=*/false,
17093 /*check_dependency_p=*/true,
17094 /*template_p=*/NULL,
17095 /*declarator_p=*/false,
17096 /*optional_p=*/false);
17098 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
17100 bool non_integral_constant_expression_p = false;
17101 tree id_expression = expr;
17102 cp_id_kind idk;
17103 const char *error_msg;
17105 if (identifier_p (expr))
17106 /* Lookup the name we got back from the id-expression. */
17107 expr = cp_parser_lookup_name_simple (parser, expr,
17108 id_expr_start_token->location);
17110 if (expr
17111 && expr != error_mark_node
17112 && TREE_CODE (expr) != TYPE_DECL
17113 && (TREE_CODE (expr) != BIT_NOT_EXPR
17114 || !TYPE_P (TREE_OPERAND (expr, 0)))
17115 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
17117 /* Complete lookup of the id-expression. */
17118 expr = (finish_id_expression
17119 (id_expression, expr, parser->scope, &idk,
17120 /*integral_constant_expression_p=*/false,
17121 /*allow_non_integral_constant_expression_p=*/true,
17122 &non_integral_constant_expression_p,
17123 /*template_p=*/false,
17124 /*done=*/true,
17125 /*address_p=*/false,
17126 /*template_arg_p=*/false,
17127 &error_msg,
17128 id_expr_start_token->location));
17130 if (error_msg)
17132 /* We found an id-expression, but it was something that we
17133 should not have found. This is an error, not something
17134 we can recover from, so report the error we found and
17135 we'll recover as gracefully as possible. */
17136 cp_parser_parse_definitely (parser);
17137 cp_parser_error (parser, error_msg);
17138 id_expression_or_member_access_p = true;
17139 return error_mark_node;
17143 if (expr
17144 && expr != error_mark_node
17145 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
17146 /* We have an id-expression. */
17147 id_expression_or_member_access_p = true;
17150 if (!id_expression_or_member_access_p)
17152 /* Abort the id-expression parse. */
17153 cp_parser_abort_tentative_parse (parser);
17155 /* Parsing tentatively, again. */
17156 cp_parser_parse_tentatively (parser);
17158 /* Parse a class member access. */
17159 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
17160 /*cast_p=*/false, /*decltype*/true,
17161 /*member_access_only_p=*/true, NULL);
17163 if (expr
17164 && expr != error_mark_node
17165 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
17166 /* We have an id-expression. */
17167 id_expression_or_member_access_p = true;
17170 if (id_expression_or_member_access_p)
17171 /* We have parsed the complete id-expression or member access. */
17172 cp_parser_parse_definitely (parser);
17173 else
17175 /* Abort our attempt to parse an id-expression or member access
17176 expression. */
17177 cp_parser_abort_tentative_parse (parser);
17179 /* Parse a full expression. */
17180 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
17181 /*decltype_p=*/true);
17184 return expr;
17187 /* Parse a `decltype' type. Returns the type.
17189 decltype-specifier:
17190 decltype ( expression )
17191 C++14:
17192 decltype ( auto ) */
17194 static tree
17195 cp_parser_decltype (cp_parser *parser)
17197 bool id_expression_or_member_access_p = false;
17198 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
17200 if (start_token->type == CPP_DECLTYPE)
17202 /* Already parsed. */
17203 cp_lexer_consume_token (parser->lexer);
17204 return saved_checks_value (start_token->u.tree_check_value);
17207 /* Look for the `decltype' token. */
17208 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
17209 return error_mark_node;
17211 /* Parse the opening `('. */
17212 matching_parens parens;
17213 if (!parens.require_open (parser))
17214 return error_mark_node;
17216 /* Since we're going to preserve any side-effects from this parse, set up a
17217 firewall to protect our callers from cp_parser_commit_to_tentative_parse
17218 in the expression. */
17219 tentative_firewall firewall (parser);
17221 /* If in_declarator_p, a reparse as an expression might succeed (60361).
17222 Otherwise, commit now for better diagnostics. */
17223 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
17224 && !parser->in_declarator_p)
17225 cp_parser_commit_to_topmost_tentative_parse (parser);
17227 push_deferring_access_checks (dk_deferred);
17229 tree expr = NULL_TREE;
17231 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO)
17232 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
17234 /* decltype (auto) */
17235 cp_lexer_consume_token (parser->lexer);
17236 if (cxx_dialect < cxx14)
17238 error_at (start_token->location,
17239 "%<decltype(auto)%> type specifier only available with "
17240 "%<-std=c++14%> or %<-std=gnu++14%>");
17241 expr = error_mark_node;
17244 else
17246 /* decltype (expression) */
17248 /* Types cannot be defined in a `decltype' expression. Save away the
17249 old message and set the new one. */
17250 const char *saved_message = parser->type_definition_forbidden_message;
17251 parser->type_definition_forbidden_message
17252 = G_("types may not be defined in %<decltype%> expressions");
17254 /* The restrictions on constant-expressions do not apply inside
17255 decltype expressions. */
17256 bool saved_integral_constant_expression_p
17257 = parser->integral_constant_expression_p;
17258 bool saved_non_integral_constant_expression_p
17259 = parser->non_integral_constant_expression_p;
17260 parser->integral_constant_expression_p = false;
17262 /* Within a parenthesized expression, a `>' token is always
17263 the greater-than operator. */
17264 bool saved_greater_than_is_operator_p
17265 = parser->greater_than_is_operator_p;
17266 parser->greater_than_is_operator_p = true;
17268 /* Don't synthesize an implicit template type parameter here. This
17269 could happen with C++23 code like
17271 void f(decltype(new auto{0}));
17273 where we want to deduce the auto right away so that the parameter
17274 is of type 'int *'. */
17275 auto cleanup = make_temp_override
17276 (parser->auto_is_implicit_function_template_parm_p, false);
17278 /* Do not actually evaluate the expression. */
17279 ++cp_unevaluated_operand;
17281 /* Do not warn about problems with the expression. */
17282 ++c_inhibit_evaluation_warnings;
17284 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
17285 STRIP_ANY_LOCATION_WRAPPER (expr);
17287 /* Go back to evaluating expressions. */
17288 --cp_unevaluated_operand;
17289 --c_inhibit_evaluation_warnings;
17291 /* The `>' token might be the end of a template-id or
17292 template-parameter-list now. */
17293 parser->greater_than_is_operator_p
17294 = saved_greater_than_is_operator_p;
17296 /* Restore the old message and the integral constant expression
17297 flags. */
17298 parser->type_definition_forbidden_message = saved_message;
17299 parser->integral_constant_expression_p
17300 = saved_integral_constant_expression_p;
17301 parser->non_integral_constant_expression_p
17302 = saved_non_integral_constant_expression_p;
17305 /* Parse to the closing `)'. */
17306 if (expr == error_mark_node || !parens.require_close (parser))
17308 cp_parser_skip_to_closing_parenthesis (parser, true, false,
17309 /*consume_paren=*/true);
17310 expr = error_mark_node;
17313 /* If we got a parse error while tentative, bail out now. */
17314 if (cp_parser_error_occurred (parser))
17316 pop_deferring_access_checks ();
17317 return error_mark_node;
17320 if (!expr)
17321 /* Build auto. */
17322 expr = make_decltype_auto ();
17323 else
17324 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
17325 tf_warning_or_error);
17327 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
17328 it again. */
17329 start_token->type = CPP_DECLTYPE;
17330 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
17331 start_token->tree_check_p = true;
17332 start_token->u.tree_check_value->value = expr;
17333 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
17334 start_token->keyword = RID_MAX;
17336 location_t loc = start_token->location;
17337 loc = make_location (loc, loc, parser->lexer);
17338 start_token->location = loc;
17340 cp_lexer_purge_tokens_after (parser->lexer, start_token);
17342 pop_to_parent_deferring_access_checks ();
17344 return expr;
17347 /* Special member functions [gram.special] */
17349 /* Parse a conversion-function-id.
17351 conversion-function-id:
17352 operator conversion-type-id
17354 Returns an IDENTIFIER_NODE representing the operator. */
17356 static tree
17357 cp_parser_conversion_function_id (cp_parser* parser)
17359 tree type;
17360 tree saved_scope;
17361 tree saved_qualifying_scope;
17362 tree saved_object_scope;
17363 tree pushed_scope = NULL_TREE;
17365 /* Look for the `operator' token. */
17366 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
17367 return error_mark_node;
17368 /* When we parse the conversion-type-id, the current scope will be
17369 reset. However, we need that information in able to look up the
17370 conversion function later, so we save it here. */
17371 saved_scope = parser->scope;
17372 saved_qualifying_scope = parser->qualifying_scope;
17373 saved_object_scope = parser->object_scope;
17374 /* We must enter the scope of the class so that the names of
17375 entities declared within the class are available in the
17376 conversion-type-id. For example, consider:
17378 struct S {
17379 typedef int I;
17380 operator I();
17383 S::operator I() { ... }
17385 In order to see that `I' is a type-name in the definition, we
17386 must be in the scope of `S'. */
17387 if (saved_scope)
17388 pushed_scope = push_scope (saved_scope);
17389 /* Parse the conversion-type-id. */
17390 type = cp_parser_conversion_type_id (parser);
17391 /* Leave the scope of the class, if any. */
17392 if (pushed_scope)
17393 pop_scope (pushed_scope);
17394 /* Restore the saved scope. */
17395 parser->scope = saved_scope;
17396 parser->qualifying_scope = saved_qualifying_scope;
17397 parser->object_scope = saved_object_scope;
17398 /* If the TYPE is invalid, indicate failure. */
17399 if (type == error_mark_node)
17400 return error_mark_node;
17401 return make_conv_op_name (type);
17404 /* Parse a conversion-type-id:
17406 conversion-type-id:
17407 type-specifier-seq conversion-declarator [opt]
17409 Returns the TYPE specified. */
17411 static tree
17412 cp_parser_conversion_type_id (cp_parser* parser)
17414 tree attributes;
17415 cp_decl_specifier_seq type_specifiers;
17416 cp_declarator *declarator;
17417 tree type_specified;
17418 const char *saved_message;
17420 /* Parse the attributes. */
17421 attributes = cp_parser_attributes_opt (parser);
17423 saved_message = parser->type_definition_forbidden_message;
17424 parser->type_definition_forbidden_message
17425 = G_("types may not be defined in a conversion-type-id");
17427 /* Parse the type-specifiers. DR 2413 clarifies that `typename' is
17428 optional in conversion-type-id. */
17429 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
17430 /*is_declaration=*/false,
17431 /*is_trailing_return=*/false,
17432 &type_specifiers);
17434 parser->type_definition_forbidden_message = saved_message;
17436 /* If that didn't work, stop. */
17437 if (type_specifiers.type == error_mark_node)
17438 return error_mark_node;
17439 /* Parse the conversion-declarator. */
17440 declarator = cp_parser_conversion_declarator_opt (parser);
17442 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
17443 /*initialized=*/0, &attributes);
17444 if (attributes)
17445 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
17447 /* Don't give this error when parsing tentatively. This happens to
17448 work because we always parse this definitively once. */
17449 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
17450 && type_uses_auto (type_specified))
17452 if (cxx_dialect < cxx14)
17454 error ("invalid use of %<auto%> in conversion operator");
17455 return error_mark_node;
17457 else if (template_parm_scope_p ())
17458 warning (0, "use of %<auto%> in member template "
17459 "conversion operator can never be deduced");
17462 return type_specified;
17465 /* Parse an (optional) conversion-declarator.
17467 conversion-declarator:
17468 ptr-operator conversion-declarator [opt]
17472 static cp_declarator *
17473 cp_parser_conversion_declarator_opt (cp_parser* parser)
17475 enum tree_code code;
17476 tree class_type, std_attributes = NULL_TREE;
17477 cp_cv_quals cv_quals;
17479 /* We don't know if there's a ptr-operator next, or not. */
17480 cp_parser_parse_tentatively (parser);
17481 /* Try the ptr-operator. */
17482 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
17483 &std_attributes);
17484 /* If it worked, look for more conversion-declarators. */
17485 if (cp_parser_parse_definitely (parser))
17487 cp_declarator *declarator;
17489 /* Parse another optional declarator. */
17490 declarator = cp_parser_conversion_declarator_opt (parser);
17492 declarator = cp_parser_make_indirect_declarator
17493 (code, class_type, cv_quals, declarator, std_attributes);
17495 return declarator;
17498 return NULL;
17501 /* Parse an (optional) ctor-initializer.
17503 ctor-initializer:
17504 : mem-initializer-list */
17506 static void
17507 cp_parser_ctor_initializer_opt (cp_parser* parser)
17509 /* If the next token is not a `:', then there is no
17510 ctor-initializer. */
17511 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17513 /* Do default initialization of any bases and members. */
17514 if (DECL_CONSTRUCTOR_P (current_function_decl))
17515 finish_mem_initializers (NULL_TREE);
17516 return;
17519 /* Consume the `:' token. */
17520 cp_lexer_consume_token (parser->lexer);
17521 /* And the mem-initializer-list. */
17522 cp_parser_mem_initializer_list (parser);
17525 /* Parse a mem-initializer-list.
17527 mem-initializer-list:
17528 mem-initializer ... [opt]
17529 mem-initializer ... [opt] , mem-initializer-list */
17531 static void
17532 cp_parser_mem_initializer_list (cp_parser* parser)
17534 tree mem_initializer_list = NULL_TREE;
17535 tree target_ctor = error_mark_node;
17536 cp_token *token = cp_lexer_peek_token (parser->lexer);
17538 /* Let the semantic analysis code know that we are starting the
17539 mem-initializer-list. */
17540 if (!DECL_CONSTRUCTOR_P (current_function_decl))
17541 error_at (token->location,
17542 "only constructors take member initializers");
17544 /* Loop through the list. */
17545 while (true)
17547 tree mem_initializer;
17549 token = cp_lexer_peek_token (parser->lexer);
17550 /* Parse the mem-initializer. */
17551 mem_initializer = cp_parser_mem_initializer (parser);
17552 /* If the next token is a `...', we're expanding member initializers. */
17553 bool ellipsis = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
17554 if (ellipsis
17555 || (mem_initializer != error_mark_node
17556 && check_for_bare_parameter_packs (TREE_PURPOSE
17557 (mem_initializer))))
17559 /* Consume the `...'. */
17560 if (ellipsis)
17561 cp_lexer_consume_token (parser->lexer);
17563 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
17564 can be expanded but members cannot. */
17565 if (mem_initializer != error_mark_node
17566 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
17568 error_at (token->location,
17569 "cannot expand initializer for member %qD",
17570 TREE_PURPOSE (mem_initializer));
17571 mem_initializer = error_mark_node;
17574 /* Construct the pack expansion type. */
17575 if (mem_initializer != error_mark_node)
17576 mem_initializer = make_pack_expansion (mem_initializer);
17578 if (target_ctor != error_mark_node
17579 && mem_initializer != error_mark_node)
17581 error ("mem-initializer for %qD follows constructor delegation",
17582 TREE_PURPOSE (mem_initializer));
17583 mem_initializer = error_mark_node;
17585 /* Look for a target constructor. */
17586 if (mem_initializer != error_mark_node
17587 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
17588 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
17590 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
17591 if (mem_initializer_list)
17593 error ("constructor delegation follows mem-initializer for %qD",
17594 TREE_PURPOSE (mem_initializer_list));
17595 mem_initializer = error_mark_node;
17597 target_ctor = mem_initializer;
17599 /* Add it to the list, unless it was erroneous. */
17600 if (mem_initializer != error_mark_node)
17602 TREE_CHAIN (mem_initializer) = mem_initializer_list;
17603 mem_initializer_list = mem_initializer;
17605 /* If the next token is not a `,', we're done. */
17606 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17607 break;
17608 /* Consume the `,' token. */
17609 cp_lexer_consume_token (parser->lexer);
17612 /* Perform semantic analysis. */
17613 if (DECL_CONSTRUCTOR_P (current_function_decl))
17614 finish_mem_initializers (mem_initializer_list);
17617 /* Parse a mem-initializer.
17619 mem-initializer:
17620 mem-initializer-id ( expression-list [opt] )
17621 mem-initializer-id braced-init-list
17623 GNU extension:
17625 mem-initializer:
17626 ( expression-list [opt] )
17628 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
17629 class) or FIELD_DECL (for a non-static data member) to initialize;
17630 the TREE_VALUE is the expression-list. An empty initialization
17631 list is represented by void_list_node. */
17633 static tree
17634 cp_parser_mem_initializer (cp_parser* parser)
17636 tree mem_initializer_id;
17637 tree expression_list;
17638 tree member;
17639 cp_token *token = cp_lexer_peek_token (parser->lexer);
17641 /* Find out what is being initialized. */
17642 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17644 permerror (token->location,
17645 "anachronistic old-style base class initializer");
17646 mem_initializer_id = NULL_TREE;
17648 else
17650 mem_initializer_id = cp_parser_mem_initializer_id (parser);
17651 if (mem_initializer_id == error_mark_node)
17652 return mem_initializer_id;
17654 member = expand_member_init (mem_initializer_id);
17655 if (member && !DECL_P (member))
17656 in_base_initializer = 1;
17658 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17660 cp_lexer_set_source_position (parser->lexer);
17661 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17662 expression_list = cp_parser_braced_list (parser);
17663 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
17664 expression_list = build_tree_list (NULL_TREE, expression_list);
17666 else
17668 vec<tree, va_gc> *vec;
17669 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
17670 /*cast_p=*/false,
17671 /*allow_expansion_p=*/true,
17672 /*non_constant_p=*/NULL,
17673 /*close_paren_loc=*/NULL,
17674 /*wrap_locations_p=*/true);
17675 if (vec == NULL)
17676 return error_mark_node;
17677 expression_list = build_tree_list_vec (vec);
17678 release_tree_vector (vec);
17681 if (expression_list == error_mark_node)
17682 return error_mark_node;
17683 if (!expression_list)
17684 expression_list = void_type_node;
17686 in_base_initializer = 0;
17688 if (!member)
17689 return error_mark_node;
17690 tree node = build_tree_list (member, expression_list);
17692 /* We can't attach the source location of this initializer directly to
17693 the list node, so we instead attach it to a dummy EMPTY_CLASS_EXPR
17694 within the TREE_TYPE of the list node. */
17695 location_t loc
17696 = make_location (token->location, token->location, parser->lexer);
17697 tree dummy = build0 (EMPTY_CLASS_EXPR, NULL_TREE);
17698 SET_EXPR_LOCATION (dummy, loc);
17699 TREE_TYPE (node) = dummy;
17701 return node;
17704 /* Parse a mem-initializer-id.
17706 mem-initializer-id:
17707 :: [opt] nested-name-specifier [opt] class-name
17708 decltype-specifier (C++11)
17709 identifier
17711 Returns a TYPE indicating the class to be initialized for the first
17712 production (and the second in C++11). Returns an IDENTIFIER_NODE
17713 indicating the data member to be initialized for the last production. */
17715 static tree
17716 cp_parser_mem_initializer_id (cp_parser* parser)
17718 bool global_scope_p;
17719 bool nested_name_specifier_p;
17720 bool template_p = false;
17721 tree id;
17723 cp_token *token = cp_lexer_peek_token (parser->lexer);
17725 /* `typename' is not allowed in this context ([temp.res]). */
17726 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
17728 error_at (token->location,
17729 "keyword %<typename%> not allowed in this context (a qualified "
17730 "member initializer is implicitly a type)");
17731 cp_lexer_consume_token (parser->lexer);
17733 /* Look for the optional `::' operator. */
17734 global_scope_p
17735 = (cp_parser_global_scope_opt (parser,
17736 /*current_scope_valid_p=*/false)
17737 != NULL_TREE);
17738 /* Look for the optional nested-name-specifier. The simplest way to
17739 implement:
17741 [temp.res]
17743 The keyword `typename' is not permitted in a base-specifier or
17744 mem-initializer; in these contexts a qualified name that
17745 depends on a template-parameter is implicitly assumed to be a
17746 type name.
17748 is to assume that we have seen the `typename' keyword at this
17749 point. */
17750 nested_name_specifier_p
17751 = (cp_parser_nested_name_specifier_opt (parser,
17752 /*typename_keyword_p=*/true,
17753 /*check_dependency_p=*/true,
17754 /*type_p=*/true,
17755 /*is_declaration=*/true)
17756 != NULL_TREE);
17757 if (nested_name_specifier_p)
17758 template_p = cp_parser_optional_template_keyword (parser);
17759 /* If there is a `::' operator or a nested-name-specifier, then we
17760 are definitely looking for a class-name. */
17761 if (global_scope_p || nested_name_specifier_p)
17762 return cp_parser_class_name (parser,
17763 /*typename_keyword_p=*/true,
17764 /*template_keyword_p=*/template_p,
17765 typename_type,
17766 /*check_dependency_p=*/true,
17767 /*class_head_p=*/false,
17768 /*is_declaration=*/true);
17769 /* Otherwise, we could also be looking for an ordinary identifier. */
17770 cp_parser_parse_tentatively (parser);
17771 if (cp_lexer_next_token_is_decltype (parser->lexer))
17772 /* Try a decltype-specifier. */
17773 id = cp_parser_decltype (parser);
17774 else
17775 /* Otherwise, try a class-name. */
17776 id = cp_parser_class_name (parser,
17777 /*typename_keyword_p=*/true,
17778 /*template_keyword_p=*/false,
17779 none_type,
17780 /*check_dependency_p=*/true,
17781 /*class_head_p=*/false,
17782 /*is_declaration=*/true);
17783 /* If we found one, we're done. */
17784 if (cp_parser_parse_definitely (parser))
17785 return id;
17786 /* Otherwise, look for an ordinary identifier. */
17787 return cp_parser_identifier (parser);
17790 /* Overloading [gram.over] */
17792 /* Parse an operator-function-id.
17794 operator-function-id:
17795 operator operator
17797 Returns an IDENTIFIER_NODE for the operator which is a
17798 human-readable spelling of the identifier, e.g., `operator +'. */
17800 static cp_expr
17801 cp_parser_operator_function_id (cp_parser* parser)
17803 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
17804 /* Look for the `operator' keyword. */
17805 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
17806 return error_mark_node;
17807 /* And then the name of the operator itself. */
17808 return cp_parser_operator (parser, start_loc);
17811 /* Return an identifier node for a user-defined literal operator.
17812 The suffix identifier is chained to the operator name identifier. */
17814 tree
17815 cp_literal_operator_id (const char* name)
17817 tree identifier;
17818 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
17819 + strlen (name) + 10);
17820 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
17821 identifier = get_identifier (buffer);
17822 XDELETEVEC (buffer);
17824 return identifier;
17827 /* Parse an operator.
17829 operator:
17830 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
17831 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
17832 || ++ -- , ->* -> () []
17834 GNU Extensions:
17836 operator:
17837 <? >? <?= >?=
17839 Returns an IDENTIFIER_NODE for the operator which is a
17840 human-readable spelling of the identifier, e.g., `operator +'. */
17842 static cp_expr
17843 cp_parser_operator (cp_parser* parser, location_t start_loc)
17845 tree id = NULL_TREE;
17846 cp_token *token;
17847 bool utf8 = false;
17849 /* Peek at the next token. */
17850 token = cp_lexer_peek_token (parser->lexer);
17852 location_t end_loc = token->location;
17854 /* Figure out which operator we have. */
17855 enum tree_code op = ERROR_MARK;
17856 bool assop = false;
17857 bool consumed = false;
17858 switch (token->type)
17860 case CPP_KEYWORD:
17862 /* The keyword should be either `new', `delete' or `co_await'. */
17863 if (token->keyword == RID_NEW)
17864 op = NEW_EXPR;
17865 else if (token->keyword == RID_DELETE)
17866 op = DELETE_EXPR;
17867 else if (token->keyword == RID_CO_AWAIT)
17868 op = CO_AWAIT_EXPR;
17869 else
17870 break;
17872 /* Consume the `new', `delete' or co_await token. */
17873 end_loc = cp_lexer_consume_token (parser->lexer)->location;
17875 /* Peek at the next token. */
17876 token = cp_lexer_peek_token (parser->lexer);
17877 /* If it's a `[' token then this is the array variant of the
17878 operator. */
17879 if (token->type == CPP_OPEN_SQUARE
17880 && op != CO_AWAIT_EXPR)
17882 /* Consume the `[' token. */
17883 cp_lexer_consume_token (parser->lexer);
17884 /* Look for the `]' token. */
17885 if (cp_token *close_token
17886 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
17887 end_loc = close_token->location;
17888 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
17890 consumed = true;
17891 break;
17894 case CPP_PLUS:
17895 op = PLUS_EXPR;
17896 break;
17898 case CPP_MINUS:
17899 op = MINUS_EXPR;
17900 break;
17902 case CPP_MULT:
17903 op = MULT_EXPR;
17904 break;
17906 case CPP_DIV:
17907 op = TRUNC_DIV_EXPR;
17908 break;
17910 case CPP_MOD:
17911 op = TRUNC_MOD_EXPR;
17912 break;
17914 case CPP_XOR:
17915 op = BIT_XOR_EXPR;
17916 break;
17918 case CPP_AND:
17919 op = BIT_AND_EXPR;
17920 break;
17922 case CPP_OR:
17923 op = BIT_IOR_EXPR;
17924 break;
17926 case CPP_COMPL:
17927 op = BIT_NOT_EXPR;
17928 break;
17930 case CPP_NOT:
17931 op = TRUTH_NOT_EXPR;
17932 break;
17934 case CPP_EQ:
17935 assop = true;
17936 op = NOP_EXPR;
17937 break;
17939 case CPP_LESS:
17940 op = LT_EXPR;
17941 break;
17943 case CPP_GREATER:
17944 op = GT_EXPR;
17945 break;
17947 case CPP_PLUS_EQ:
17948 assop = true;
17949 op = PLUS_EXPR;
17950 break;
17952 case CPP_MINUS_EQ:
17953 assop = true;
17954 op = MINUS_EXPR;
17955 break;
17957 case CPP_MULT_EQ:
17958 assop = true;
17959 op = MULT_EXPR;
17960 break;
17962 case CPP_DIV_EQ:
17963 assop = true;
17964 op = TRUNC_DIV_EXPR;
17965 break;
17967 case CPP_MOD_EQ:
17968 assop = true;
17969 op = TRUNC_MOD_EXPR;
17970 break;
17972 case CPP_XOR_EQ:
17973 assop = true;
17974 op = BIT_XOR_EXPR;
17975 break;
17977 case CPP_AND_EQ:
17978 assop = true;
17979 op = BIT_AND_EXPR;
17980 break;
17982 case CPP_OR_EQ:
17983 assop = true;
17984 op = BIT_IOR_EXPR;
17985 break;
17987 case CPP_LSHIFT:
17988 op = LSHIFT_EXPR;
17989 break;
17991 case CPP_RSHIFT:
17992 op = RSHIFT_EXPR;
17993 break;
17995 case CPP_LSHIFT_EQ:
17996 assop = true;
17997 op = LSHIFT_EXPR;
17998 break;
18000 case CPP_RSHIFT_EQ:
18001 assop = true;
18002 op = RSHIFT_EXPR;
18003 break;
18005 case CPP_EQ_EQ:
18006 op = EQ_EXPR;
18007 break;
18009 case CPP_NOT_EQ:
18010 op = NE_EXPR;
18011 break;
18013 case CPP_LESS_EQ:
18014 op = LE_EXPR;
18015 break;
18017 case CPP_GREATER_EQ:
18018 op = GE_EXPR;
18019 break;
18021 case CPP_SPACESHIP:
18022 op = SPACESHIP_EXPR;
18023 break;
18025 case CPP_AND_AND:
18026 op = TRUTH_ANDIF_EXPR;
18027 break;
18029 case CPP_OR_OR:
18030 op = TRUTH_ORIF_EXPR;
18031 break;
18033 case CPP_PLUS_PLUS:
18034 op = POSTINCREMENT_EXPR;
18035 break;
18037 case CPP_MINUS_MINUS:
18038 op = PREDECREMENT_EXPR;
18039 break;
18041 case CPP_COMMA:
18042 op = COMPOUND_EXPR;
18043 break;
18045 case CPP_DEREF_STAR:
18046 op = MEMBER_REF;
18047 break;
18049 case CPP_DEREF:
18050 op = COMPONENT_REF;
18051 break;
18053 case CPP_QUERY:
18054 op = COND_EXPR;
18055 /* Consume the `?'. */
18056 cp_lexer_consume_token (parser->lexer);
18057 /* Look for the matching `:'. */
18058 cp_parser_require (parser, CPP_COLON, RT_COLON);
18059 consumed = true;
18060 break;
18062 case CPP_OPEN_PAREN:
18064 /* Consume the `('. */
18065 matching_parens parens;
18066 parens.consume_open (parser);
18067 /* Look for the matching `)'. */
18068 token = parens.require_close (parser);
18069 if (token)
18070 end_loc = token->location;
18071 op = CALL_EXPR;
18072 consumed = true;
18073 break;
18076 case CPP_OPEN_SQUARE:
18077 /* Consume the `['. */
18078 cp_lexer_consume_token (parser->lexer);
18079 /* Look for the matching `]'. */
18080 token = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
18081 if (token)
18082 end_loc = token->location;
18083 op = ARRAY_REF;
18084 consumed = true;
18085 break;
18087 case CPP_UTF8STRING:
18088 case CPP_UTF8STRING_USERDEF:
18089 utf8 = true;
18090 /* FALLTHRU */
18091 case CPP_STRING:
18092 case CPP_WSTRING:
18093 case CPP_STRING16:
18094 case CPP_STRING32:
18095 case CPP_STRING_USERDEF:
18096 case CPP_WSTRING_USERDEF:
18097 case CPP_STRING16_USERDEF:
18098 case CPP_STRING32_USERDEF:
18100 tree string_tree;
18101 int sz, len;
18103 if (cxx_dialect == cxx98)
18104 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
18106 /* Consume the string. */
18107 cp_expr str = cp_parser_userdef_string_literal (parser,
18108 /*lookup_udlit=*/false);
18109 if (str == error_mark_node)
18110 return error_mark_node;
18111 else if (TREE_CODE (str) == USERDEF_LITERAL)
18113 string_tree = USERDEF_LITERAL_VALUE (str.get_value ());
18114 id = USERDEF_LITERAL_SUFFIX_ID (str.get_value ());
18115 end_loc = str.get_location ();
18117 else
18119 string_tree = str;
18120 /* Look for the suffix identifier. */
18121 token = cp_lexer_peek_token (parser->lexer);
18122 if (token->type == CPP_NAME)
18124 id = cp_parser_identifier (parser);
18125 end_loc = token->location;
18127 else if (token->type == CPP_KEYWORD)
18129 error ("unexpected keyword;"
18130 " remove space between quotes and suffix identifier");
18131 return error_mark_node;
18133 else
18135 error ("expected suffix identifier");
18136 return error_mark_node;
18139 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
18140 (TREE_TYPE (TREE_TYPE (string_tree))));
18141 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
18142 if (len != 0)
18144 error ("expected empty string after %<operator%> keyword");
18145 return error_mark_node;
18147 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
18148 != char_type_node)
18150 error ("invalid encoding prefix in literal operator");
18151 return error_mark_node;
18153 if (id != error_mark_node)
18155 const char *name = IDENTIFIER_POINTER (id);
18156 id = cp_literal_operator_id (name);
18158 /* Generate a location of the form:
18159 "" _suffix_identifier
18160 ^~~~~~~~~~~~~~~~~~~~~
18161 with caret == start at the start token, finish at the end of the
18162 suffix identifier. */
18163 location_t combined_loc
18164 = make_location (start_loc, start_loc, parser->lexer);
18165 return cp_expr (id, combined_loc);
18168 default:
18169 /* Anything else is an error. */
18170 break;
18173 /* If we have selected an identifier, we need to consume the
18174 operator token. */
18175 if (op != ERROR_MARK)
18177 id = ovl_op_identifier (assop, op);
18178 if (!consumed)
18179 cp_lexer_consume_token (parser->lexer);
18181 /* Otherwise, no valid operator name was present. */
18182 else
18184 cp_parser_error (parser, "expected operator");
18185 id = error_mark_node;
18188 start_loc = make_location (start_loc, start_loc, get_finish (end_loc));
18189 return cp_expr (id, start_loc);
18192 /* Parse a template-declaration.
18194 template-declaration:
18195 export [opt] template < template-parameter-list > declaration
18197 If MEMBER_P is TRUE, this template-declaration occurs within a
18198 class-specifier.
18200 The grammar rule given by the standard isn't correct. What
18201 is really meant is:
18203 template-declaration:
18204 export [opt] template-parameter-list-seq
18205 decl-specifier-seq [opt] init-declarator [opt] ;
18206 export [opt] template-parameter-list-seq
18207 function-definition
18209 template-parameter-list-seq:
18210 template-parameter-list-seq [opt]
18211 template < template-parameter-list >
18213 Concept Extensions:
18215 template-parameter-list-seq:
18216 template < template-parameter-list > requires-clause [opt]
18218 requires-clause:
18219 requires logical-or-expression */
18221 static void
18222 cp_parser_template_declaration (cp_parser* parser, bool member_p)
18224 /* Check for `export'. */
18225 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
18227 /* Consume the `export' token. */
18228 cp_lexer_consume_token (parser->lexer);
18229 /* Warn that this use of export is deprecated. */
18230 if (cxx_dialect < cxx11)
18231 warning (0, "keyword %<export%> not implemented, and will be ignored");
18232 else if (cxx_dialect < cxx20)
18233 warning (0, "keyword %<export%> is deprecated, and is ignored");
18234 else
18235 warning (0, "keyword %<export%> is enabled with %<-fmodules-ts%>");
18238 cp_parser_template_declaration_after_export (parser, member_p);
18241 /* Parse a template-parameter-list.
18243 template-parameter-list:
18244 template-parameter
18245 template-parameter-list , template-parameter
18247 Returns a TREE_LIST. Each node represents a template parameter.
18248 The nodes are connected via their TREE_CHAINs. */
18250 static tree
18251 cp_parser_template_parameter_list (cp_parser* parser)
18253 tree parameter_list = NULL_TREE;
18255 /* Don't create wrapper nodes within a template-parameter-list,
18256 since we don't want to have different types based on the
18257 spelling location of constants and decls within them. */
18258 auto_suppress_location_wrappers sentinel;
18260 begin_template_parm_list ();
18262 /* The loop below parses the template parms. We first need to know
18263 the total number of template parms to be able to compute proper
18264 canonical types of each dependent type. So after the loop, when
18265 we know the total number of template parms,
18266 end_template_parm_list computes the proper canonical types and
18267 fixes up the dependent types accordingly. */
18268 while (true)
18270 tree parameter;
18271 bool is_non_type;
18272 bool is_parameter_pack;
18273 location_t parm_loc;
18275 /* Parse the template-parameter. */
18276 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
18277 parameter = cp_parser_template_parameter (parser,
18278 &is_non_type,
18279 &is_parameter_pack);
18280 /* Add it to the list. */
18281 if (parameter != error_mark_node)
18282 parameter_list = process_template_parm (parameter_list,
18283 parm_loc,
18284 parameter,
18285 is_non_type,
18286 is_parameter_pack);
18287 else
18289 tree err_parm = build_tree_list (parameter, parameter);
18290 parameter_list = chainon (parameter_list, err_parm);
18293 /* If the next token is not a `,', we're done. */
18294 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18295 break;
18296 /* Otherwise, consume the `,' token. */
18297 cp_lexer_consume_token (parser->lexer);
18300 return end_template_parm_list (parameter_list);
18303 /* Parse a introduction-list.
18305 introduction-list:
18306 introduced-parameter
18307 introduction-list , introduced-parameter
18309 introduced-parameter:
18310 ...[opt] identifier
18312 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
18313 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
18314 WILDCARD_DECL will also have DECL_NAME set and token location in
18315 DECL_SOURCE_LOCATION. */
18317 static tree
18318 cp_parser_introduction_list (cp_parser *parser)
18320 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
18322 while (true)
18324 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
18325 if (is_pack)
18326 cp_lexer_consume_token (parser->lexer);
18328 tree identifier = cp_parser_identifier (parser);
18329 if (identifier == error_mark_node)
18330 break;
18332 /* Build placeholder. */
18333 tree parm = build_nt (WILDCARD_DECL);
18334 DECL_SOURCE_LOCATION (parm)
18335 = cp_lexer_peek_token (parser->lexer)->location;
18336 DECL_NAME (parm) = identifier;
18337 WILDCARD_PACK_P (parm) = is_pack;
18338 vec_safe_push (introduction_vec, parm);
18340 /* If the next token is not a `,', we're done. */
18341 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18342 break;
18343 /* Otherwise, consume the `,' token. */
18344 cp_lexer_consume_token (parser->lexer);
18347 /* Convert the vec into a TREE_VEC. */
18348 tree introduction_list = make_tree_vec (introduction_vec->length ());
18349 unsigned int n;
18350 tree parm;
18351 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
18352 TREE_VEC_ELT (introduction_list, n) = parm;
18354 release_tree_vector (introduction_vec);
18355 return introduction_list;
18358 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
18359 is an abstract declarator. */
18361 static inline cp_declarator*
18362 get_id_declarator (cp_declarator *declarator)
18364 cp_declarator *d = declarator;
18365 while (d && d->kind != cdk_id)
18366 d = d->declarator;
18367 return d;
18370 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
18371 is an abstract declarator. */
18373 static inline tree
18374 get_unqualified_id (cp_declarator *declarator)
18376 declarator = get_id_declarator (declarator);
18377 if (declarator)
18378 return declarator->u.id.unqualified_name;
18379 else
18380 return NULL_TREE;
18383 /* Returns true if TYPE would declare a constrained constrained-parameter. */
18385 static inline bool
18386 is_constrained_parameter (tree type)
18388 return (type
18389 && TREE_CODE (type) == TYPE_DECL
18390 && CONSTRAINED_PARM_CONCEPT (type)
18391 && DECL_P (CONSTRAINED_PARM_CONCEPT (type)));
18394 /* Returns true if PARM declares a constrained-parameter. */
18396 static inline bool
18397 is_constrained_parameter (cp_parameter_declarator *parm)
18399 return is_constrained_parameter (parm->decl_specifiers.type);
18402 /* Check that the type parameter is only a declarator-id, and that its
18403 type is not cv-qualified. */
18405 bool
18406 cp_parser_check_constrained_type_parm (cp_parser *parser,
18407 cp_parameter_declarator *parm)
18409 if (!parm->declarator)
18410 return true;
18412 if (parm->declarator->kind != cdk_id)
18414 cp_parser_error (parser, "invalid constrained type parameter");
18415 return false;
18418 /* Don't allow cv-qualified type parameters. */
18419 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
18420 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
18422 cp_parser_error (parser, "cv-qualified type parameter");
18423 return false;
18426 return true;
18429 /* Finish parsing/processing a template type parameter and checking
18430 various restrictions. */
18432 static inline tree
18433 cp_parser_constrained_type_template_parm (cp_parser *parser,
18434 tree id,
18435 cp_parameter_declarator* parmdecl)
18437 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
18438 return finish_template_type_parm (class_type_node, id);
18439 else
18440 return error_mark_node;
18443 static tree
18444 finish_constrained_template_template_parm (tree proto, tree id)
18446 /* FIXME: This should probably be copied, and we may need to adjust
18447 the template parameter depths. */
18448 tree saved_parms = current_template_parms;
18449 begin_template_parm_list ();
18450 current_template_parms = DECL_TEMPLATE_PARMS (proto);
18451 end_template_parm_list ();
18453 tree parm = finish_template_template_parm (class_type_node, id);
18454 current_template_parms = saved_parms;
18456 return parm;
18459 /* Finish parsing/processing a template template parameter by borrowing
18460 the template parameter list from the prototype parameter. */
18462 static tree
18463 cp_parser_constrained_template_template_parm (cp_parser *parser,
18464 tree proto,
18465 tree id,
18466 cp_parameter_declarator *parmdecl)
18468 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
18469 return error_mark_node;
18470 return finish_constrained_template_template_parm (proto, id);
18473 /* Create a new non-type template parameter from the given PARM
18474 declarator. */
18476 static tree
18477 cp_parser_constrained_non_type_template_parm (bool *is_non_type,
18478 cp_parameter_declarator *parm)
18480 *is_non_type = true;
18481 cp_declarator *decl = parm->declarator;
18482 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
18483 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
18484 return grokdeclarator (decl, specs, TPARM, 0, NULL);
18487 /* Build a constrained template parameter based on the PARMDECL
18488 declarator. The type of PARMDECL is the constrained type, which
18489 refers to the prototype template parameter that ultimately
18490 specifies the type of the declared parameter. */
18492 static tree
18493 finish_constrained_parameter (cp_parser *parser,
18494 cp_parameter_declarator *parmdecl,
18495 bool *is_non_type)
18497 tree decl = parmdecl->decl_specifiers.type;
18498 tree id = get_unqualified_id (parmdecl->declarator);
18499 tree def = parmdecl->default_argument;
18500 tree proto = DECL_INITIAL (decl);
18502 /* Build the parameter. Return an error if the declarator was invalid. */
18503 tree parm;
18504 if (TREE_CODE (proto) == TYPE_DECL)
18505 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
18506 else if (TREE_CODE (proto) == TEMPLATE_DECL)
18507 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
18508 parmdecl);
18509 else
18510 parm = cp_parser_constrained_non_type_template_parm (is_non_type, parmdecl);
18511 if (parm == error_mark_node)
18512 return error_mark_node;
18514 /* Finish the parameter decl and create a node attaching the
18515 default argument and constraint. */
18516 parm = build_tree_list (def, parm);
18517 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
18519 return parm;
18522 /* Returns true if the parsed type actually represents the declaration
18523 of a type template-parameter. */
18525 static bool
18526 declares_constrained_type_template_parameter (tree type)
18528 return (is_constrained_parameter (type)
18529 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
18532 /* Returns true if the parsed type actually represents the declaration of
18533 a template template-parameter. */
18535 static bool
18536 declares_constrained_template_template_parameter (tree type)
18538 return (is_constrained_parameter (type)
18539 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
18542 /* Parse a default argument for a type template-parameter.
18543 Note that diagnostics are handled in cp_parser_template_parameter. */
18545 static tree
18546 cp_parser_default_type_template_argument (cp_parser *parser)
18548 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
18550 /* Consume the `=' token. */
18551 cp_lexer_consume_token (parser->lexer);
18553 cp_token *token = cp_lexer_peek_token (parser->lexer);
18555 /* Tell cp_parser_lambda_expression this is a default argument. */
18556 auto lvf = make_temp_override (parser->local_variables_forbidden_p);
18557 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
18559 /* Parse the default-argument. */
18560 push_deferring_access_checks (dk_no_deferred);
18561 tree default_argument = cp_parser_type_id (parser,
18562 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
18563 NULL);
18564 pop_deferring_access_checks ();
18566 if (flag_concepts && type_uses_auto (default_argument))
18568 error_at (token->location,
18569 "invalid use of %<auto%> in default template argument");
18570 return error_mark_node;
18573 return default_argument;
18576 /* Parse a default argument for a template template-parameter. */
18578 static tree
18579 cp_parser_default_template_template_argument (cp_parser *parser)
18581 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
18583 bool is_template;
18585 /* Consume the `='. */
18586 cp_lexer_consume_token (parser->lexer);
18587 /* Parse the id-expression. */
18588 push_deferring_access_checks (dk_no_deferred);
18589 /* save token before parsing the id-expression, for error
18590 reporting */
18591 const cp_token* token = cp_lexer_peek_token (parser->lexer);
18592 tree default_argument
18593 = cp_parser_id_expression (parser,
18594 /*template_keyword_p=*/false,
18595 /*check_dependency_p=*/true,
18596 /*template_p=*/&is_template,
18597 /*declarator_p=*/false,
18598 /*optional_p=*/false);
18599 if (TREE_CODE (default_argument) == TYPE_DECL)
18600 /* If the id-expression was a template-id that refers to
18601 a template-class, we already have the declaration here,
18602 so no further lookup is needed. */
18604 else
18605 /* Look up the name. */
18606 default_argument
18607 = cp_parser_lookup_name (parser, default_argument,
18608 none_type,
18609 /*is_template=*/is_template,
18610 /*is_namespace=*/false,
18611 /*check_dependency=*/true,
18612 /*ambiguous_decls=*/NULL,
18613 token->location);
18614 /* See if the default argument is valid. */
18615 default_argument = check_template_template_default_arg (default_argument);
18616 pop_deferring_access_checks ();
18617 return default_argument;
18620 /* Parse a template-parameter.
18622 template-parameter:
18623 type-parameter
18624 parameter-declaration
18626 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
18627 the parameter. The TREE_PURPOSE is the default value, if any.
18628 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
18629 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
18630 set to true iff this parameter is a parameter pack. */
18632 static tree
18633 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
18634 bool *is_parameter_pack)
18636 cp_token *token;
18637 cp_parameter_declarator *parameter_declarator;
18638 tree parm;
18640 /* Assume it is a type parameter or a template parameter. */
18641 *is_non_type = false;
18642 /* Assume it not a parameter pack. */
18643 *is_parameter_pack = false;
18644 /* Peek at the next token. */
18645 token = cp_lexer_peek_token (parser->lexer);
18646 /* If it is `template', we have a type-parameter. */
18647 if (token->keyword == RID_TEMPLATE)
18648 return cp_parser_type_parameter (parser, is_parameter_pack);
18649 /* If it is `class' or `typename' we do not know yet whether it is a
18650 type parameter or a non-type parameter. Consider:
18652 template <typename T, typename T::X X> ...
18656 template <class C, class D*> ...
18658 Here, the first parameter is a type parameter, and the second is
18659 a non-type parameter. We can tell by looking at the token after
18660 the identifier -- if it is a `,', `=', or `>' then we have a type
18661 parameter. */
18662 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
18664 /* Peek at the token after `class' or `typename'. */
18665 token = cp_lexer_peek_nth_token (parser->lexer, 2);
18666 /* If it's an ellipsis, we have a template type parameter
18667 pack. */
18668 if (token->type == CPP_ELLIPSIS)
18669 return cp_parser_type_parameter (parser, is_parameter_pack);
18670 /* If it's an identifier, skip it. */
18671 if (token->type == CPP_NAME)
18672 token = cp_lexer_peek_nth_token (parser->lexer, 3);
18673 /* Now, see if the token looks like the end of a template
18674 parameter. */
18675 if (token->type == CPP_COMMA
18676 || token->type == CPP_EQ
18677 || token->type == CPP_GREATER)
18678 return cp_parser_type_parameter (parser, is_parameter_pack);
18681 /* Otherwise, it is a non-type parameter or a constrained parameter.
18683 [temp.param]
18685 When parsing a default template-argument for a non-type
18686 template-parameter, the first non-nested `>' is taken as the end
18687 of the template parameter-list rather than a greater-than
18688 operator. */
18689 parameter_declarator
18690 = cp_parser_parameter_declaration (parser,
18691 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
18692 /*template_parm_p=*/true,
18693 /*parenthesized_p=*/NULL);
18695 if (!parameter_declarator)
18696 return error_mark_node;
18698 /* If the parameter declaration is marked as a parameter pack, set
18699 *IS_PARAMETER_PACK to notify the caller. */
18700 if (parameter_declarator->template_parameter_pack_p)
18701 *is_parameter_pack = true;
18703 if (parameter_declarator->default_argument)
18705 /* Can happen in some cases of erroneous input (c++/34892). */
18706 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18707 /* Consume the `...' for better error recovery. */
18708 cp_lexer_consume_token (parser->lexer);
18711 /* The parameter may have been constrained type parameter. */
18712 if (is_constrained_parameter (parameter_declarator))
18713 return finish_constrained_parameter (parser,
18714 parameter_declarator,
18715 is_non_type);
18717 // Now we're sure that the parameter is a non-type parameter.
18718 *is_non_type = true;
18720 parm = grokdeclarator (parameter_declarator->declarator,
18721 &parameter_declarator->decl_specifiers,
18722 TPARM, /*initialized=*/0,
18723 /*attrlist=*/NULL);
18724 if (parm == error_mark_node)
18725 return error_mark_node;
18727 return build_tree_list (parameter_declarator->default_argument, parm);
18730 /* Parse a type-parameter.
18732 type-parameter:
18733 class identifier [opt]
18734 class identifier [opt] = type-id
18735 typename identifier [opt]
18736 typename identifier [opt] = type-id
18737 template < template-parameter-list > class identifier [opt]
18738 template < template-parameter-list > class identifier [opt]
18739 = id-expression
18741 GNU Extension (variadic templates):
18743 type-parameter:
18744 class ... identifier [opt]
18745 typename ... identifier [opt]
18747 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
18748 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
18749 the declaration of the parameter.
18751 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
18753 static tree
18754 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
18756 cp_token *token;
18757 tree parameter;
18759 /* Look for a keyword to tell us what kind of parameter this is. */
18760 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
18761 if (!token)
18762 return error_mark_node;
18764 switch (token->keyword)
18766 case RID_CLASS:
18767 case RID_TYPENAME:
18769 tree identifier;
18770 tree default_argument;
18772 /* If the next token is an ellipsis, we have a template
18773 argument pack. */
18774 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18776 /* Consume the `...' token. */
18777 cp_lexer_consume_token (parser->lexer);
18778 maybe_warn_variadic_templates ();
18780 *is_parameter_pack = true;
18783 /* If the next token is an identifier, then it names the
18784 parameter. */
18785 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18786 identifier = cp_parser_identifier (parser);
18787 else
18788 identifier = NULL_TREE;
18790 /* Create the parameter. */
18791 parameter = finish_template_type_parm (class_type_node, identifier);
18793 /* If the next token is an `=', we have a default argument. */
18794 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18796 default_argument
18797 = cp_parser_default_type_template_argument (parser);
18799 /* Template parameter packs cannot have default
18800 arguments. */
18801 if (*is_parameter_pack)
18803 if (identifier)
18804 error_at (token->location,
18805 "template parameter pack %qD cannot have a "
18806 "default argument", identifier);
18807 else
18808 error_at (token->location,
18809 "template parameter packs cannot have "
18810 "default arguments");
18811 default_argument = NULL_TREE;
18813 else if (check_for_bare_parameter_packs (default_argument))
18814 default_argument = error_mark_node;
18816 else
18817 default_argument = NULL_TREE;
18819 /* Create the combined representation of the parameter and the
18820 default argument. */
18821 parameter = build_tree_list (default_argument, parameter);
18823 break;
18825 case RID_TEMPLATE:
18827 tree identifier;
18828 tree default_argument;
18830 /* Look for the `<'. */
18831 cp_parser_require (parser, CPP_LESS, RT_LESS);
18832 /* Parse the template-parameter-list. */
18833 cp_parser_template_parameter_list (parser);
18834 /* Look for the `>'. */
18835 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
18837 /* If template requirements are present, parse them. */
18838 if (flag_concepts)
18840 tree reqs = get_shorthand_constraints (current_template_parms);
18841 if (tree dreqs = cp_parser_requires_clause_opt (parser, false))
18842 reqs = combine_constraint_expressions (reqs, dreqs);
18843 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
18846 /* Look for the `class' or 'typename' keywords. */
18847 cp_parser_type_parameter_key (parser);
18848 /* If the next token is an ellipsis, we have a template
18849 argument pack. */
18850 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18852 /* Consume the `...' token. */
18853 cp_lexer_consume_token (parser->lexer);
18854 maybe_warn_variadic_templates ();
18856 *is_parameter_pack = true;
18858 /* If the next token is an `=', then there is a
18859 default-argument. If the next token is a `>', we are at
18860 the end of the parameter-list. If the next token is a `,',
18861 then we are at the end of this parameter. */
18862 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
18863 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
18864 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18866 identifier = cp_parser_identifier (parser);
18867 /* Treat invalid names as if the parameter were nameless. */
18868 if (identifier == error_mark_node)
18869 identifier = NULL_TREE;
18871 else
18872 identifier = NULL_TREE;
18874 /* Create the template parameter. */
18875 parameter = finish_template_template_parm (class_type_node,
18876 identifier);
18878 /* If the next token is an `=', then there is a
18879 default-argument. */
18880 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18882 default_argument
18883 = cp_parser_default_template_template_argument (parser);
18885 /* Template parameter packs cannot have default
18886 arguments. */
18887 if (*is_parameter_pack)
18889 if (identifier)
18890 error_at (token->location,
18891 "template parameter pack %qD cannot "
18892 "have a default argument",
18893 identifier);
18894 else
18895 error_at (token->location, "template parameter packs cannot "
18896 "have default arguments");
18897 default_argument = NULL_TREE;
18900 else
18901 default_argument = NULL_TREE;
18903 /* Create the combined representation of the parameter and the
18904 default argument. */
18905 parameter = build_tree_list (default_argument, parameter);
18907 break;
18909 default:
18910 gcc_unreachable ();
18911 break;
18914 return parameter;
18917 /* Parse a template-id.
18919 template-id:
18920 template-name < template-argument-list [opt] >
18922 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
18923 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
18924 returned. Otherwise, if the template-name names a function, or set
18925 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
18926 names a class, returns a TYPE_DECL for the specialization.
18928 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
18929 uninstantiated templates. */
18931 static tree
18932 cp_parser_template_id (cp_parser *parser,
18933 bool template_keyword_p,
18934 bool check_dependency_p,
18935 enum tag_types tag_type,
18936 bool is_declaration)
18938 tree templ;
18939 tree arguments;
18940 tree template_id;
18941 cp_token_position start_of_id = 0;
18942 cp_token *next_token = NULL, *next_token_2 = NULL;
18943 bool is_identifier;
18945 /* If the next token corresponds to a template-id, there is no need
18946 to reparse it. */
18947 cp_token *token = cp_lexer_peek_token (parser->lexer);
18949 if (token->type == CPP_TEMPLATE_ID)
18951 cp_lexer_consume_token (parser->lexer);
18952 return saved_checks_value (token->u.tree_check_value);
18955 /* Avoid performing name lookup if there is no possibility of
18956 finding a template-id. */
18957 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
18958 || (token->type == CPP_NAME
18959 && !cp_parser_nth_token_starts_template_argument_list_p
18960 (parser, 2)))
18962 cp_parser_error (parser, "expected template-id");
18963 return error_mark_node;
18966 /* Remember where the template-id starts. */
18967 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
18968 start_of_id = cp_lexer_token_position (parser->lexer, false);
18970 push_deferring_access_checks (dk_deferred);
18972 /* Parse the template-name. */
18973 is_identifier = false;
18974 templ = cp_parser_template_name (parser, template_keyword_p,
18975 check_dependency_p,
18976 is_declaration,
18977 tag_type,
18978 &is_identifier);
18980 /* Push any access checks inside the firewall we're about to create. */
18981 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
18982 pop_deferring_access_checks ();
18983 if (templ == error_mark_node || is_identifier)
18984 return templ;
18986 /* Since we're going to preserve any side-effects from this parse, set up a
18987 firewall to protect our callers from cp_parser_commit_to_tentative_parse
18988 in the template arguments. */
18989 tentative_firewall firewall (parser);
18990 reopen_deferring_access_checks (checks);
18992 /* If we find the sequence `[:' after a template-name, it's probably
18993 a digraph-typo for `< ::'. Substitute the tokens and check if we can
18994 parse correctly the argument list. */
18995 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
18996 == CPP_OPEN_SQUARE)
18997 && next_token->flags & DIGRAPH
18998 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
18999 == CPP_COLON)
19000 && !(next_token_2->flags & PREV_WHITE))
19002 cp_parser_parse_tentatively (parser);
19003 /* Change `:' into `::'. */
19004 next_token_2->type = CPP_SCOPE;
19005 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
19006 CPP_LESS. */
19007 cp_lexer_consume_token (parser->lexer);
19009 /* Parse the arguments. */
19010 arguments = cp_parser_enclosed_template_argument_list (parser);
19011 if (!cp_parser_parse_definitely (parser))
19013 /* If we couldn't parse an argument list, then we revert our changes
19014 and return simply an error. Maybe this is not a template-id
19015 after all. */
19016 next_token_2->type = CPP_COLON;
19017 cp_parser_error (parser, "expected %<<%>");
19018 pop_deferring_access_checks ();
19019 return error_mark_node;
19021 /* Otherwise, emit an error about the invalid digraph, but continue
19022 parsing because we got our argument list. */
19023 if (permerror (next_token->location,
19024 "%<<::%> cannot begin a template-argument list"))
19026 static bool hint = false;
19027 inform (next_token->location,
19028 "%<<:%> is an alternate spelling for %<[%>."
19029 " Insert whitespace between %<<%> and %<::%>");
19030 if (!hint && !flag_permissive)
19032 inform (next_token->location, "(if you use %<-fpermissive%> "
19033 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
19034 "accept your code)");
19035 hint = true;
19039 else
19041 /* Look for the `<' that starts the template-argument-list. */
19042 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
19044 pop_deferring_access_checks ();
19045 return error_mark_node;
19047 /* Parse the arguments. */
19048 arguments = cp_parser_enclosed_template_argument_list (parser);
19050 if ((cxx_dialect > cxx17)
19051 && (TREE_CODE (templ) == FUNCTION_DECL || identifier_p (templ))
19052 && !template_keyword_p
19053 && (cp_parser_error_occurred (parser)
19054 || cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)))
19056 /* This didn't go well. */
19057 if (TREE_CODE (templ) == FUNCTION_DECL)
19059 /* C++20 says that "function-name < a;" is now ill-formed. */
19060 if (cp_parser_error_occurred (parser))
19062 error_at (token->location, "invalid template-argument-list");
19063 inform (token->location, "function name as the left hand "
19064 "operand of %<<%> is ill-formed in C++20; wrap the "
19065 "function name in %<()%>");
19067 else
19068 /* We expect "f<targs>" to be followed by "(args)". */
19069 error_at (cp_lexer_peek_token (parser->lexer)->location,
19070 "expected %<(%> after template-argument-list");
19071 if (start_of_id)
19072 /* Purge all subsequent tokens. */
19073 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
19075 else
19076 cp_parser_simulate_error (parser);
19077 pop_deferring_access_checks ();
19078 return error_mark_node;
19082 /* Set the location to be of the form:
19083 template-name < template-argument-list [opt] >
19084 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
19085 with caret == start at the start of the template-name,
19086 ranging until the closing '>'. */
19087 location_t combined_loc
19088 = make_location (token->location, token->location, parser->lexer);
19090 /* Check for concepts autos where they don't belong. We could
19091 identify types in some cases of identifier TEMPL, looking ahead
19092 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
19093 types. We reject them in functions, but if what we have is an
19094 identifier, even with none_type we can't conclude it's NOT a
19095 type, we have to wait for template substitution. */
19096 if (flag_concepts && check_auto_in_tmpl_args (templ, arguments))
19097 template_id = error_mark_node;
19098 /* Build a representation of the specialization. */
19099 else if (identifier_p (templ))
19100 template_id = build_min_nt_loc (combined_loc,
19101 TEMPLATE_ID_EXPR,
19102 templ, arguments);
19103 else if (DECL_TYPE_TEMPLATE_P (templ)
19104 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
19106 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
19107 template (rather than some instantiation thereof) only if
19108 is not nested within some other construct. For example, in
19109 "template <typename T> void f(T) { A<T>::", A<T> is just an
19110 instantiation of A. */
19111 bool entering_scope
19112 = (template_parm_scope_p ()
19113 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE));
19114 template_id
19115 = finish_template_type (templ, arguments, entering_scope);
19117 else if (concept_definition_p (templ))
19119 /* The caller will decide whether this is a concept check or type
19120 constraint. */
19121 template_id = build2_loc (combined_loc, TEMPLATE_ID_EXPR,
19122 boolean_type_node, templ, arguments);
19124 else if (variable_template_p (templ))
19126 template_id = lookup_template_variable (templ, arguments, tf_warning_or_error);
19127 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
19128 SET_EXPR_LOCATION (template_id, combined_loc);
19130 else if (TREE_CODE (templ) == TYPE_DECL
19131 && TREE_CODE (TREE_TYPE (templ)) == TYPENAME_TYPE)
19133 /* Some type template in dependent scope. */
19134 tree &name = TYPENAME_TYPE_FULLNAME (TREE_TYPE (templ));
19135 name = build_min_nt_loc (combined_loc,
19136 TEMPLATE_ID_EXPR,
19137 name, arguments);
19138 template_id = templ;
19140 else
19142 /* If it's not a class-template or a template-template, it should be
19143 a function-template. */
19144 gcc_assert (OVL_P (templ) || BASELINK_P (templ));
19146 template_id = lookup_template_function (templ, arguments);
19147 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
19148 SET_EXPR_LOCATION (template_id, combined_loc);
19151 /* If parsing tentatively, replace the sequence of tokens that makes
19152 up the template-id with a CPP_TEMPLATE_ID token. That way,
19153 should we re-parse the token stream, we will not have to repeat
19154 the effort required to do the parse, nor will we issue duplicate
19155 error messages about problems during instantiation of the
19156 template. */
19157 if (start_of_id
19158 /* Don't do this if we had a parse error in a declarator; re-parsing
19159 might succeed if a name changes meaning (60361). */
19160 && !(cp_parser_error_occurred (parser)
19161 && cp_parser_parsing_tentatively (parser)
19162 && parser->in_declarator_p))
19164 /* Reset the contents of the START_OF_ID token. */
19165 token->type = CPP_TEMPLATE_ID;
19166 token->location = combined_loc;
19168 /* Retrieve any deferred checks. Do not pop this access checks yet
19169 so the memory will not be reclaimed during token replacing below. */
19170 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
19171 token->tree_check_p = true;
19172 token->u.tree_check_value->value = template_id;
19173 token->u.tree_check_value->checks = get_deferred_access_checks ();
19174 token->keyword = RID_MAX;
19176 /* Purge all subsequent tokens. */
19177 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
19179 /* ??? Can we actually assume that, if template_id ==
19180 error_mark_node, we will have issued a diagnostic to the
19181 user, as opposed to simply marking the tentative parse as
19182 failed? */
19183 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
19184 error_at (token->location, "parse error in template argument list");
19187 pop_to_parent_deferring_access_checks ();
19188 return template_id;
19191 /* Like cp_parser_template_id, called in non-type context. */
19193 static tree
19194 cp_parser_template_id_expr (cp_parser *parser,
19195 bool template_keyword_p,
19196 bool check_dependency_p,
19197 bool is_declaration)
19199 tree x = cp_parser_template_id (parser, template_keyword_p, check_dependency_p,
19200 none_type, is_declaration);
19201 if (TREE_CODE (x) == TEMPLATE_ID_EXPR
19202 && concept_check_p (x))
19203 /* We didn't check the arguments in cp_parser_template_id; do that now. */
19204 return build_concept_id (x);
19205 return x;
19208 /* Parse a template-name.
19210 template-name:
19211 identifier
19213 The standard should actually say:
19215 template-name:
19216 identifier
19217 operator-function-id
19219 A defect report has been filed about this issue.
19221 A conversion-function-id cannot be a template name because they cannot
19222 be part of a template-id. In fact, looking at this code:
19224 a.operator K<int>()
19226 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
19227 It is impossible to call a templated conversion-function-id with an
19228 explicit argument list, since the only allowed template parameter is
19229 the type to which it is converting.
19231 If TEMPLATE_KEYWORD_P is true, then we have just seen the
19232 `template' keyword, in a construction like:
19234 T::template f<3>()
19236 In that case `f' is taken to be a template-name, even though there
19237 is no way of knowing for sure.
19239 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
19240 name refers to a set of overloaded functions, at least one of which
19241 is a template, or an IDENTIFIER_NODE with the name of the template,
19242 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
19243 names are looked up inside uninstantiated templates. */
19245 static tree
19246 cp_parser_template_name (cp_parser* parser,
19247 bool template_keyword_p,
19248 bool check_dependency_p,
19249 bool is_declaration,
19250 enum tag_types tag_type,
19251 bool *is_identifier)
19253 tree identifier;
19254 tree decl;
19255 cp_token *token = cp_lexer_peek_token (parser->lexer);
19257 /* If the next token is `operator', then we have either an
19258 operator-function-id or a conversion-function-id. */
19259 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
19261 /* We don't know whether we're looking at an
19262 operator-function-id or a conversion-function-id. */
19263 cp_parser_parse_tentatively (parser);
19264 /* Try an operator-function-id. */
19265 identifier = cp_parser_operator_function_id (parser);
19266 /* If that didn't work, try a conversion-function-id. */
19267 if (!cp_parser_parse_definitely (parser))
19269 cp_parser_error (parser, "expected template-name");
19270 return error_mark_node;
19273 /* Look for the identifier. */
19274 else
19275 identifier = cp_parser_identifier (parser);
19277 /* If we didn't find an identifier, we don't have a template-id. */
19278 if (identifier == error_mark_node)
19279 return error_mark_node;
19281 /* If the name immediately followed the `template' keyword, then it
19282 is a template-name. However, if the next token is not `<', then
19283 we do not treat it as a template-name, since it is not being used
19284 as part of a template-id. This enables us to handle constructs
19285 like:
19287 template <typename T> struct S { S(); };
19288 template <typename T> S<T>::S();
19290 correctly. We would treat `S' as a template -- if it were `S<T>'
19291 -- but we do not if there is no `<'. */
19293 if (processing_template_decl
19294 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
19296 /* In a declaration, in a dependent context, we pretend that the
19297 "template" keyword was present in order to improve error
19298 recovery. For example, given:
19300 template <typename T> void f(T::X<int>);
19302 we want to treat "X<int>" as a template-id. */
19303 if (is_declaration
19304 && !template_keyword_p
19305 && parser->scope && TYPE_P (parser->scope)
19306 && check_dependency_p
19307 && dependent_scope_p (parser->scope)
19308 /* Do not do this for dtors (or ctors), since they never
19309 need the template keyword before their name. */
19310 && !constructor_name_p (identifier, parser->scope))
19312 cp_token_position start = 0;
19314 /* Explain what went wrong. */
19315 error_at (token->location, "non-template %qD used as template",
19316 identifier);
19317 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
19318 parser->scope, identifier);
19319 /* If parsing tentatively, find the location of the "<" token. */
19320 if (cp_parser_simulate_error (parser))
19321 start = cp_lexer_token_position (parser->lexer, true);
19322 /* Parse the template arguments so that we can issue error
19323 messages about them. */
19324 cp_lexer_consume_token (parser->lexer);
19325 cp_parser_enclosed_template_argument_list (parser);
19326 /* Skip tokens until we find a good place from which to
19327 continue parsing. */
19328 cp_parser_skip_to_closing_parenthesis (parser,
19329 /*recovering=*/true,
19330 /*or_comma=*/true,
19331 /*consume_paren=*/false);
19332 /* If parsing tentatively, permanently remove the
19333 template argument list. That will prevent duplicate
19334 error messages from being issued about the missing
19335 "template" keyword. */
19336 if (start)
19337 cp_lexer_purge_tokens_after (parser->lexer, start);
19338 if (is_identifier)
19339 *is_identifier = true;
19340 parser->context->object_type = NULL_TREE;
19341 return identifier;
19344 /* If the "template" keyword is present, then there is generally
19345 no point in doing name-lookup, so we just return IDENTIFIER.
19346 But, if the qualifying scope is non-dependent then we can
19347 (and must) do name-lookup normally. */
19348 if (template_keyword_p)
19350 tree scope = (parser->scope ? parser->scope
19351 : parser->context->object_type);
19352 if (scope && TYPE_P (scope)
19353 && (!CLASS_TYPE_P (scope)
19354 || (check_dependency_p && dependent_scope_p (scope))))
19356 /* We're optimizing away the call to cp_parser_lookup_name, but
19357 we still need to do this. */
19358 parser->object_scope = parser->context->object_type;
19359 parser->context->object_type = NULL_TREE;
19360 return identifier;
19365 /* cp_parser_lookup_name clears OBJECT_TYPE. */
19366 tree scope = (parser->scope ? parser->scope
19367 : parser->context->object_type);
19369 /* Look up the name. */
19370 decl = cp_parser_lookup_name (parser, identifier,
19371 tag_type,
19372 /*is_template=*/1 + template_keyword_p,
19373 /*is_namespace=*/false,
19374 check_dependency_p,
19375 /*ambiguous_decls=*/NULL,
19376 token->location);
19378 decl = strip_using_decl (decl);
19380 /* 13.3 [temp.names] A < is interpreted as the delimiter of a
19381 template-argument-list if it follows a name that is not a
19382 conversion-function-id and
19383 - that follows the keyword template or a ~ after a nested-name-specifier or
19384 in a class member access expression, or
19385 - for which name lookup finds the injected-class-name of a class template
19386 or finds any declaration of a template, or
19387 - that is an unqualified name for which name lookup either finds one or
19388 more functions or finds nothing, or
19389 - that is a terminal name in a using-declarator (9.9), in a declarator-id
19390 (9.3.4), or in a type-only context other than a nested-name-specifier
19391 (13.8). */
19393 /* Handle injected-class-name. */
19394 decl = maybe_get_template_decl_from_type_decl (decl);
19396 /* If DECL is a template, then the name was a template-name. */
19397 if (TREE_CODE (decl) == TEMPLATE_DECL)
19399 if ((TREE_DEPRECATED (decl) || TREE_UNAVAILABLE (decl))
19400 && deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
19402 tree d = DECL_TEMPLATE_RESULT (decl);
19403 tree attr;
19404 if (TREE_CODE (d) == TYPE_DECL)
19405 attr = TYPE_ATTRIBUTES (TREE_TYPE (d));
19406 else
19407 attr = DECL_ATTRIBUTES (d);
19408 if (TREE_UNAVAILABLE (decl))
19410 attr = lookup_attribute ("unavailable", attr);
19411 error_unavailable_use (decl, attr);
19413 else if (TREE_DEPRECATED (decl)
19414 && deprecated_state != DEPRECATED_SUPPRESS)
19416 attr = lookup_attribute ("deprecated", attr);
19417 warn_deprecated_use (decl, attr);
19421 else
19423 /* Look through an overload set for any templates. */
19424 bool found = false;
19426 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
19427 !found && iter; ++iter)
19428 if (TREE_CODE (*iter) == TEMPLATE_DECL)
19429 found = true;
19431 /* "an unqualified name for which name lookup either finds one or more
19432 functions or finds nothing". */
19433 if (!found
19434 && (cxx_dialect > cxx17)
19435 && !scope
19436 && cp_lexer_next_token_is (parser->lexer, CPP_LESS)
19437 && tag_type == none_type)
19439 /* The "more functions" case. Just use the OVERLOAD as normally.
19440 We don't use is_overloaded_fn here to avoid considering
19441 BASELINKs. */
19442 if (TREE_CODE (decl) == OVERLOAD
19443 /* Name lookup found one function. */
19444 || TREE_CODE (decl) == FUNCTION_DECL
19445 /* Name lookup found nothing. */
19446 || decl == error_mark_node)
19447 found = true;
19450 /* "that follows the keyword template"..."in a type-only context" */
19451 if (!found && scope
19452 && (template_keyword_p || tag_type != none_type)
19453 && dependentish_scope_p (scope)
19454 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
19455 found = true;
19457 if (!found)
19459 /* The name does not name a template. */
19460 cp_parser_error (parser, "expected template-name");
19461 return error_mark_node;
19463 else if ((!DECL_P (decl) && !is_overloaded_fn (decl))
19464 || TREE_CODE (decl) == USING_DECL
19465 /* cp_parser_template_id can only handle some TYPE_DECLs. */
19466 || (TREE_CODE (decl) == TYPE_DECL
19467 && TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE))
19468 /* Repeat the lookup at instantiation time. */
19469 decl = identifier;
19472 return decl;
19475 /* Parse a template-argument-list.
19477 template-argument-list:
19478 template-argument ... [opt]
19479 template-argument-list , template-argument ... [opt]
19481 Returns a TREE_VEC containing the arguments. */
19483 static tree
19484 cp_parser_template_argument_list (cp_parser* parser)
19486 bool saved_in_template_argument_list_p;
19487 bool saved_ice_p;
19488 bool saved_non_ice_p;
19490 /* Don't create location wrapper nodes within a template-argument-list. */
19491 auto_suppress_location_wrappers sentinel;
19493 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
19494 parser->in_template_argument_list_p = true;
19495 /* Even if the template-id appears in an integral
19496 constant-expression, the contents of the argument list do
19497 not. */
19498 saved_ice_p = parser->integral_constant_expression_p;
19499 parser->integral_constant_expression_p = false;
19500 saved_non_ice_p = parser->non_integral_constant_expression_p;
19501 parser->non_integral_constant_expression_p = false;
19503 /* Parse the arguments. */
19504 auto_vec<tree, 10> args;
19507 if (!args.is_empty ())
19508 /* Consume the comma. */
19509 cp_lexer_consume_token (parser->lexer);
19511 /* Parse the template-argument. */
19512 tree argument = cp_parser_template_argument (parser);
19514 /* If the next token is an ellipsis, we're expanding a template
19515 argument pack. */
19516 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19518 if (argument == error_mark_node)
19520 cp_token *token = cp_lexer_peek_token (parser->lexer);
19521 error_at (token->location,
19522 "expected parameter pack before %<...%>");
19524 /* Consume the `...' token. */
19525 cp_lexer_consume_token (parser->lexer);
19527 /* Make the argument into a TYPE_PACK_EXPANSION or
19528 EXPR_PACK_EXPANSION. */
19529 argument = make_pack_expansion (argument);
19532 args.safe_push (argument);
19534 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
19536 int n_args = args.length ();
19537 tree vec = make_tree_vec (n_args);
19539 for (int i = 0; i < n_args; i++)
19540 TREE_VEC_ELT (vec, i) = args[i];
19542 parser->non_integral_constant_expression_p = saved_non_ice_p;
19543 parser->integral_constant_expression_p = saved_ice_p;
19544 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
19545 if (CHECKING_P)
19546 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
19547 return vec;
19550 /* Parse a template-argument.
19552 template-argument:
19553 assignment-expression
19554 type-id
19555 id-expression
19557 The representation is that of an assignment-expression, type-id, or
19558 id-expression -- except that the qualified id-expression is
19559 evaluated, so that the value returned is either a DECL or an
19560 OVERLOAD.
19562 Although the standard says "assignment-expression", it forbids
19563 throw-expressions or assignments in the template argument.
19564 Therefore, we use "conditional-expression" instead. */
19566 static tree
19567 cp_parser_template_argument (cp_parser* parser)
19569 tree argument;
19570 bool template_p;
19571 bool address_p;
19572 bool maybe_type_id = false;
19573 cp_token *token = NULL, *argument_start_token = NULL;
19574 location_t loc = 0;
19575 cp_id_kind idk;
19577 /* There's really no way to know what we're looking at, so we just
19578 try each alternative in order.
19580 [temp.arg]
19582 In a template-argument, an ambiguity between a type-id and an
19583 expression is resolved to a type-id, regardless of the form of
19584 the corresponding template-parameter.
19586 Therefore, we try a type-id first. */
19587 cp_parser_parse_tentatively (parser);
19588 argument = cp_parser_template_type_arg (parser);
19589 /* If there was no error parsing the type-id but the next token is a
19590 '>>', our behavior depends on which dialect of C++ we're
19591 parsing. In C++98, we probably found a typo for '> >'. But there
19592 are type-id which are also valid expressions. For instance:
19594 struct X { int operator >> (int); };
19595 template <int V> struct Foo {};
19596 Foo<X () >> 5> r;
19598 Here 'X()' is a valid type-id of a function type, but the user just
19599 wanted to write the expression "X() >> 5". Thus, we remember that we
19600 found a valid type-id, but we still try to parse the argument as an
19601 expression to see what happens.
19603 In C++0x, the '>>' will be considered two separate '>'
19604 tokens. */
19605 if (!cp_parser_error_occurred (parser)
19606 && ((cxx_dialect == cxx98
19607 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19608 /* Similarly for >= which
19609 cp_parser_next_token_ends_template_argument_p treats for
19610 diagnostics purposes as mistyped > =, but can be valid
19611 after a type-id. */
19612 || cp_lexer_next_token_is (parser->lexer, CPP_GREATER_EQ)))
19614 maybe_type_id = true;
19615 cp_parser_abort_tentative_parse (parser);
19617 else
19619 /* If the next token isn't a `,' or a `>', then this argument wasn't
19620 really finished. This means that the argument is not a valid
19621 type-id. */
19622 if (!cp_parser_next_token_ends_template_argument_p (parser))
19623 cp_parser_error (parser, "expected template-argument");
19624 /* If that worked, we're done. */
19625 if (cp_parser_parse_definitely (parser))
19626 return argument;
19628 /* We're still not sure what the argument will be. */
19629 cp_parser_parse_tentatively (parser);
19630 /* Try a template. */
19631 argument_start_token = cp_lexer_peek_token (parser->lexer);
19632 argument = cp_parser_id_expression (parser,
19633 /*template_keyword_p=*/false,
19634 /*check_dependency_p=*/true,
19635 &template_p,
19636 /*declarator_p=*/false,
19637 /*optional_p=*/false);
19638 /* If the next token isn't a `,' or a `>', then this argument wasn't
19639 really finished. */
19640 if (!cp_parser_next_token_ends_template_argument_p (parser))
19641 cp_parser_error (parser, "expected template-argument");
19642 if (!cp_parser_error_occurred (parser))
19644 /* Figure out what is being referred to. If the id-expression
19645 was for a class template specialization, then we will have a
19646 TYPE_DECL at this point. There is no need to do name lookup
19647 at this point in that case. */
19648 if (TREE_CODE (argument) != TYPE_DECL)
19649 argument = cp_parser_lookup_name (parser, argument,
19650 none_type,
19651 /*is_template=*/template_p,
19652 /*is_namespace=*/false,
19653 /*check_dependency=*/true,
19654 /*ambiguous_decls=*/NULL,
19655 argument_start_token->location);
19656 if (TREE_CODE (argument) != TEMPLATE_DECL
19657 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
19658 cp_parser_error (parser, "expected template-name");
19660 if (cp_parser_parse_definitely (parser))
19662 if (TREE_UNAVAILABLE (argument))
19663 error_unavailable_use (argument, NULL_TREE);
19664 else if (TREE_DEPRECATED (argument))
19665 warn_deprecated_use (argument, NULL_TREE);
19666 return argument;
19668 /* It must be a non-type argument. In C++17 any constant-expression is
19669 allowed. */
19670 if (cxx_dialect > cxx14)
19671 goto general_expr;
19673 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
19675 -- an integral constant-expression of integral or enumeration
19676 type; or
19678 -- the name of a non-type template-parameter; or
19680 -- the name of an object or function with external linkage...
19682 -- the address of an object or function with external linkage...
19684 -- a pointer to member... */
19685 /* Look for a non-type template parameter. */
19686 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19688 cp_parser_parse_tentatively (parser);
19689 argument = cp_parser_primary_expression (parser,
19690 /*address_p=*/false,
19691 /*cast_p=*/false,
19692 /*template_arg_p=*/true,
19693 &idk);
19694 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
19695 || !cp_parser_next_token_ends_template_argument_p (parser))
19696 cp_parser_simulate_error (parser);
19697 if (cp_parser_parse_definitely (parser))
19698 return argument;
19701 /* If the next token is "&", the argument must be the address of an
19702 object or function with external linkage. */
19703 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
19704 if (address_p)
19706 loc = cp_lexer_peek_token (parser->lexer)->location;
19707 cp_lexer_consume_token (parser->lexer);
19709 /* See if we might have an id-expression. */
19710 token = cp_lexer_peek_token (parser->lexer);
19711 if (token->type == CPP_NAME
19712 || token->keyword == RID_OPERATOR
19713 || token->type == CPP_SCOPE
19714 || token->type == CPP_TEMPLATE_ID
19715 || token->type == CPP_NESTED_NAME_SPECIFIER)
19717 cp_parser_parse_tentatively (parser);
19718 argument = cp_parser_primary_expression (parser,
19719 address_p,
19720 /*cast_p=*/false,
19721 /*template_arg_p=*/true,
19722 &idk);
19723 if (cp_parser_error_occurred (parser)
19724 || !cp_parser_next_token_ends_template_argument_p (parser))
19725 cp_parser_abort_tentative_parse (parser);
19726 else
19728 tree probe;
19730 if (INDIRECT_REF_P (argument))
19732 /* Strip the dereference temporarily. */
19733 gcc_assert (REFERENCE_REF_P (argument));
19734 argument = TREE_OPERAND (argument, 0);
19737 /* If we're in a template, we represent a qualified-id referring
19738 to a static data member as a SCOPE_REF even if the scope isn't
19739 dependent so that we can check access control later. */
19740 probe = argument;
19741 if (TREE_CODE (probe) == SCOPE_REF)
19742 probe = TREE_OPERAND (probe, 1);
19743 if (VAR_P (probe))
19745 /* A variable without external linkage might still be a
19746 valid constant-expression, so no error is issued here
19747 if the external-linkage check fails. */
19748 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
19749 cp_parser_simulate_error (parser);
19751 else if (is_overloaded_fn (argument))
19752 /* All overloaded functions are allowed; if the external
19753 linkage test does not pass, an error will be issued
19754 later. */
19756 else if (address_p
19757 && (TREE_CODE (argument) == OFFSET_REF
19758 || TREE_CODE (argument) == SCOPE_REF))
19759 /* A pointer-to-member. */
19761 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
19763 else
19764 cp_parser_simulate_error (parser);
19766 if (cp_parser_parse_definitely (parser))
19768 if (address_p)
19769 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
19770 NULL_TREE, tf_warning_or_error);
19771 else
19772 argument = convert_from_reference (argument);
19773 return argument;
19777 /* If the argument started with "&", there are no other valid
19778 alternatives at this point. */
19779 if (address_p)
19781 cp_parser_error (parser, "invalid non-type template argument");
19782 return error_mark_node;
19785 general_expr:
19786 /* If the argument wasn't successfully parsed as a type-id followed
19787 by '>>', the argument can only be a constant expression now.
19788 Otherwise, we try parsing the constant-expression tentatively,
19789 because the argument could really be a type-id. */
19790 if (maybe_type_id)
19791 cp_parser_parse_tentatively (parser);
19793 if (cxx_dialect <= cxx14)
19794 argument = cp_parser_constant_expression (parser);
19795 else
19797 /* In C++20, we can encounter a braced-init-list. */
19798 if (cxx_dialect >= cxx20
19799 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19800 return cp_parser_braced_list (parser);
19802 /* With C++17 generalized non-type template arguments we need to handle
19803 lvalue constant expressions, too. */
19804 argument = cp_parser_assignment_expression (parser);
19805 require_potential_constant_expression (argument);
19808 if (!maybe_type_id)
19809 return argument;
19810 if (!cp_parser_next_token_ends_template_argument_p (parser))
19811 cp_parser_error (parser, "expected template-argument");
19812 if (cp_parser_parse_definitely (parser))
19813 return argument;
19814 /* We did our best to parse the argument as a non type-id, but that
19815 was the only alternative that matched (albeit with a '>' after
19816 it). We can assume it's just a typo from the user, and a
19817 diagnostic will then be issued. */
19818 return cp_parser_template_type_arg (parser);
19821 /* Parse an explicit-instantiation.
19823 explicit-instantiation:
19824 template declaration
19826 Although the standard says `declaration', what it really means is:
19828 explicit-instantiation:
19829 template decl-specifier-seq [opt] declarator [opt] ;
19831 Things like `template int S<int>::i = 5, int S<double>::j;' are not
19832 supposed to be allowed. A defect report has been filed about this
19833 issue.
19835 GNU Extension:
19837 explicit-instantiation:
19838 storage-class-specifier template
19839 decl-specifier-seq [opt] declarator [opt] ;
19840 function-specifier template
19841 decl-specifier-seq [opt] declarator [opt] ; */
19843 static void
19844 cp_parser_explicit_instantiation (cp_parser* parser)
19846 int declares_class_or_enum;
19847 cp_decl_specifier_seq decl_specifiers;
19848 tree extension_specifier = NULL_TREE;
19850 auto_timevar tv (TV_TEMPLATE_INST);
19852 /* Look for an (optional) storage-class-specifier or
19853 function-specifier. */
19854 if (cp_parser_allow_gnu_extensions_p (parser))
19856 extension_specifier
19857 = cp_parser_storage_class_specifier_opt (parser);
19858 if (!extension_specifier)
19859 extension_specifier
19860 = cp_parser_function_specifier_opt (parser,
19861 /*decl_specs=*/NULL);
19864 /* Look for the `template' keyword. */
19865 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
19866 /* Let the front end know that we are processing an explicit
19867 instantiation. */
19868 begin_explicit_instantiation ();
19869 /* [temp.explicit] says that we are supposed to ignore access
19870 control while processing explicit instantiation directives. */
19871 push_deferring_access_checks (dk_no_check);
19872 /* Parse a decl-specifier-seq. */
19873 cp_parser_decl_specifier_seq (parser,
19874 CP_PARSER_FLAGS_OPTIONAL,
19875 &decl_specifiers,
19876 &declares_class_or_enum);
19878 cp_omp_declare_simd_data odsd;
19879 if (decl_specifiers.attributes && (flag_openmp || flag_openmp_simd))
19880 cp_parser_handle_directive_omp_attributes (parser,
19881 &decl_specifiers.attributes,
19882 &odsd, true);
19884 /* If there was exactly one decl-specifier, and it declared a class,
19885 and there's no declarator, then we have an explicit type
19886 instantiation. */
19887 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
19889 tree type = check_tag_decl (&decl_specifiers,
19890 /*explicit_type_instantiation_p=*/true);
19891 /* Turn access control back on for names used during
19892 template instantiation. */
19893 pop_deferring_access_checks ();
19894 if (type)
19895 do_type_instantiation (type, extension_specifier,
19896 /*complain=*/tf_error);
19898 else
19900 cp_declarator *declarator;
19901 tree decl;
19903 /* Parse the declarator. */
19904 declarator
19905 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19906 CP_PARSER_FLAGS_NONE,
19907 /*ctor_dtor_or_conv_p=*/NULL,
19908 /*parenthesized_p=*/NULL,
19909 /*member_p=*/false,
19910 /*friend_p=*/false,
19911 /*static_p=*/false);
19912 if (declares_class_or_enum & 2)
19913 cp_parser_check_for_definition_in_return_type (declarator,
19914 decl_specifiers.type,
19915 decl_specifiers.locations[ds_type_spec]);
19916 if (declarator != cp_error_declarator)
19918 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
19919 permerror (decl_specifiers.locations[ds_inline],
19920 "explicit instantiation shall not use"
19921 " %<inline%> specifier");
19922 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
19923 permerror (decl_specifiers.locations[ds_constexpr],
19924 "explicit instantiation shall not use"
19925 " %<constexpr%> specifier");
19926 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_consteval))
19927 permerror (decl_specifiers.locations[ds_consteval],
19928 "explicit instantiation shall not use"
19929 " %<consteval%> specifier");
19931 decl = grokdeclarator (declarator, &decl_specifiers,
19932 NORMAL, 0, &decl_specifiers.attributes);
19933 /* Turn access control back on for names used during
19934 template instantiation. */
19935 pop_deferring_access_checks ();
19936 /* Do the explicit instantiation. */
19937 do_decl_instantiation (decl, extension_specifier);
19939 else
19941 pop_deferring_access_checks ();
19942 /* Skip the body of the explicit instantiation. */
19943 cp_parser_skip_to_end_of_statement (parser);
19946 /* We're done with the instantiation. */
19947 end_explicit_instantiation ();
19949 cp_parser_consume_semicolon_at_end_of_statement (parser);
19951 cp_finalize_omp_declare_simd (parser, &odsd);
19954 /* Parse an explicit-specialization.
19956 explicit-specialization:
19957 template < > declaration
19959 Although the standard says `declaration', what it really means is:
19961 explicit-specialization:
19962 template <> decl-specifier [opt] init-declarator [opt] ;
19963 template <> function-definition
19964 template <> explicit-specialization
19965 template <> template-declaration */
19967 static void
19968 cp_parser_explicit_specialization (cp_parser* parser)
19970 cp_token *token = cp_lexer_peek_token (parser->lexer);
19972 /* Look for the `template' keyword. */
19973 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
19974 /* Look for the `<'. */
19975 cp_parser_require (parser, CPP_LESS, RT_LESS);
19976 /* Look for the `>'. */
19977 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
19978 /* We have processed another parameter list. */
19979 ++parser->num_template_parameter_lists;
19981 /* [temp]
19983 A template ... explicit specialization ... shall not have C
19984 linkage. */
19985 bool need_lang_pop = current_lang_name == lang_name_c;
19986 if (need_lang_pop)
19988 error_at (token->location, "template specialization with C linkage");
19989 maybe_show_extern_c_location ();
19991 /* Give it C++ linkage to avoid confusing other parts of the
19992 front end. */
19993 push_lang_context (lang_name_cplusplus);
19996 /* Let the front end know that we are beginning a specialization. */
19997 if (begin_specialization ())
19999 /* If the next keyword is `template', we need to figure out
20000 whether or not we're looking a template-declaration. */
20001 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
20003 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
20004 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
20005 cp_parser_template_declaration_after_export (parser,
20006 /*member_p=*/false);
20007 else
20008 cp_parser_explicit_specialization (parser);
20010 else
20011 /* Parse the dependent declaration. */
20012 cp_parser_single_declaration (parser,
20013 /*checks=*/NULL,
20014 /*member_p=*/false,
20015 /*explicit_specialization_p=*/true,
20016 /*friend_p=*/NULL);
20019 /* We're done with the specialization. */
20020 end_specialization ();
20022 /* For the erroneous case of a template with C linkage, we pushed an
20023 implicit C++ linkage scope; exit that scope now. */
20024 if (need_lang_pop)
20025 pop_lang_context ();
20027 /* We're done with this parameter list. */
20028 --parser->num_template_parameter_lists;
20031 /* Preserve the attributes across a garbage collect (by making it a GC
20032 root), which can occur when parsing a member function. */
20034 static GTY(()) vec<tree, va_gc> *cp_parser_decl_specs_attrs;
20036 /* Parse a type-specifier.
20038 type-specifier:
20039 simple-type-specifier
20040 class-specifier
20041 enum-specifier
20042 elaborated-type-specifier
20043 cv-qualifier
20045 GNU Extension:
20047 type-specifier:
20048 __complex__
20050 Returns a representation of the type-specifier. For a
20051 class-specifier, enum-specifier, or elaborated-type-specifier, a
20052 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
20054 The parser flags FLAGS is used to control type-specifier parsing.
20056 If IS_DECLARATION is TRUE, then this type-specifier is appearing
20057 in a decl-specifier-seq.
20059 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
20060 class-specifier, enum-specifier, or elaborated-type-specifier, then
20061 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
20062 if a type is declared; 2 if it is defined. Otherwise, it is set to
20063 zero.
20065 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
20066 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
20067 is set to FALSE. */
20069 static tree
20070 cp_parser_type_specifier (cp_parser* parser,
20071 cp_parser_flags flags,
20072 cp_decl_specifier_seq *decl_specs,
20073 bool is_declaration,
20074 int* declares_class_or_enum,
20075 bool* is_cv_qualifier)
20077 tree type_spec = NULL_TREE;
20078 cp_token *token;
20079 enum rid keyword;
20080 cp_decl_spec ds = ds_last;
20082 /* Assume this type-specifier does not declare a new type. */
20083 if (declares_class_or_enum)
20084 *declares_class_or_enum = 0;
20085 /* And that it does not specify a cv-qualifier. */
20086 if (is_cv_qualifier)
20087 *is_cv_qualifier = false;
20088 /* Peek at the next token. */
20089 token = cp_lexer_peek_token (parser->lexer);
20091 /* If we're looking at a keyword, we can use that to guide the
20092 production we choose. */
20093 keyword = token->keyword;
20094 switch (keyword)
20096 case RID_ENUM:
20097 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
20098 goto elaborated_type_specifier;
20100 /* Look for the enum-specifier. */
20101 type_spec = cp_parser_enum_specifier (parser);
20102 /* If that worked, we're done. */
20103 if (type_spec)
20105 if (declares_class_or_enum)
20106 *declares_class_or_enum = 2;
20107 if (decl_specs)
20108 cp_parser_set_decl_spec_type (decl_specs,
20109 type_spec,
20110 token,
20111 /*type_definition_p=*/true);
20112 return type_spec;
20114 else
20115 goto elaborated_type_specifier;
20117 /* Any of these indicate either a class-specifier, or an
20118 elaborated-type-specifier. */
20119 case RID_CLASS:
20120 case RID_STRUCT:
20121 case RID_UNION:
20122 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
20123 goto elaborated_type_specifier;
20125 /* Parse tentatively so that we can back up if we don't find a
20126 class-specifier. */
20127 cp_parser_parse_tentatively (parser);
20128 if (decl_specs->attributes)
20129 vec_safe_push (cp_parser_decl_specs_attrs, decl_specs->attributes);
20130 /* Look for the class-specifier. */
20131 type_spec = cp_parser_class_specifier (parser);
20132 if (decl_specs->attributes)
20133 cp_parser_decl_specs_attrs->pop ();
20134 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
20135 /* If that worked, we're done. */
20136 if (cp_parser_parse_definitely (parser))
20138 if (declares_class_or_enum)
20139 *declares_class_or_enum = 2;
20140 if (decl_specs)
20141 cp_parser_set_decl_spec_type (decl_specs,
20142 type_spec,
20143 token,
20144 /*type_definition_p=*/true);
20145 return type_spec;
20148 /* Fall through. */
20149 elaborated_type_specifier:
20150 /* We're declaring (not defining) a class or enum. */
20151 if (declares_class_or_enum)
20152 *declares_class_or_enum = 1;
20154 /* Fall through. */
20155 case RID_TYPENAME:
20156 /* Look for an elaborated-type-specifier. */
20157 type_spec
20158 = (cp_parser_elaborated_type_specifier
20159 (parser,
20160 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
20161 is_declaration));
20162 if (decl_specs)
20163 cp_parser_set_decl_spec_type (decl_specs,
20164 type_spec,
20165 token,
20166 /*type_definition_p=*/false);
20167 return type_spec;
20169 case RID_CONST:
20170 ds = ds_const;
20171 if (is_cv_qualifier)
20172 *is_cv_qualifier = true;
20173 break;
20175 case RID_VOLATILE:
20176 ds = ds_volatile;
20177 if (is_cv_qualifier)
20178 *is_cv_qualifier = true;
20179 break;
20181 case RID_RESTRICT:
20182 ds = ds_restrict;
20183 if (is_cv_qualifier)
20184 *is_cv_qualifier = true;
20185 break;
20187 case RID_COMPLEX:
20188 /* The `__complex__' keyword is a GNU extension. */
20189 ds = ds_complex;
20190 break;
20192 default:
20193 break;
20196 /* Handle simple keywords. */
20197 if (ds != ds_last)
20199 if (decl_specs)
20201 set_and_check_decl_spec_loc (decl_specs, ds, token);
20202 decl_specs->any_specifiers_p = true;
20204 return cp_lexer_consume_token (parser->lexer)->u.value;
20207 /* If we do not already have a type-specifier, assume we are looking
20208 at a simple-type-specifier. */
20209 type_spec = cp_parser_simple_type_specifier (parser,
20210 decl_specs,
20211 flags);
20213 /* If we didn't find a type-specifier, and a type-specifier was not
20214 optional in this context, issue an error message. */
20215 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
20217 cp_parser_error (parser, "expected type specifier");
20218 return error_mark_node;
20221 return type_spec;
20224 /* Parse a simple-type-specifier.
20226 simple-type-specifier:
20227 :: [opt] nested-name-specifier [opt] type-name
20228 :: [opt] nested-name-specifier template template-id
20229 char
20230 wchar_t
20231 bool
20232 short
20234 long
20235 signed
20236 unsigned
20237 float
20238 double
20239 void
20241 C++11 Extension:
20243 simple-type-specifier:
20244 auto
20245 decltype ( expression )
20246 char16_t
20247 char32_t
20248 __underlying_type ( type-id )
20250 C++17 extension:
20252 nested-name-specifier(opt) template-name
20254 GNU Extension:
20256 simple-type-specifier:
20257 __int128
20258 __typeof__ unary-expression
20259 __typeof__ ( type-id )
20260 __typeof__ ( type-id ) { initializer-list , [opt] }
20262 Concepts Extension:
20264 simple-type-specifier:
20265 constrained-type-specifier
20267 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
20268 appropriately updated. */
20270 static tree
20271 cp_parser_simple_type_specifier (cp_parser* parser,
20272 cp_decl_specifier_seq *decl_specs,
20273 cp_parser_flags flags)
20275 tree type = NULL_TREE;
20276 cp_token *token;
20277 int idx;
20279 /* Peek at the next token. */
20280 token = cp_lexer_peek_token (parser->lexer);
20282 /* If we're looking at a keyword, things are easy. */
20283 switch (token->keyword)
20285 case RID_CHAR:
20286 if (decl_specs)
20287 decl_specs->explicit_char_p = true;
20288 type = char_type_node;
20289 break;
20290 case RID_CHAR8:
20291 type = char8_type_node;
20292 break;
20293 case RID_CHAR16:
20294 type = char16_type_node;
20295 break;
20296 case RID_CHAR32:
20297 type = char32_type_node;
20298 break;
20299 case RID_WCHAR:
20300 type = wchar_type_node;
20301 break;
20302 case RID_BOOL:
20303 type = boolean_type_node;
20304 break;
20305 case RID_SHORT:
20306 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
20307 type = short_integer_type_node;
20308 break;
20309 case RID_INT:
20310 if (decl_specs)
20311 decl_specs->explicit_int_p = true;
20312 type = integer_type_node;
20313 break;
20314 case RID_INT_N_0:
20315 case RID_INT_N_1:
20316 case RID_INT_N_2:
20317 case RID_INT_N_3:
20318 idx = token->keyword - RID_INT_N_0;
20319 if (! int_n_enabled_p [idx])
20320 break;
20321 if (decl_specs)
20323 decl_specs->explicit_intN_p = true;
20324 decl_specs->int_n_idx = idx;
20325 /* Check if the alternate "__intN__" form has been used instead of
20326 "__intN". */
20327 if (startswith (IDENTIFIER_POINTER (token->u.value)
20328 + (IDENTIFIER_LENGTH (token->u.value) - 2), "__"))
20329 decl_specs->int_n_alt = true;
20331 type = int_n_trees [idx].signed_type;
20332 break;
20333 case RID_LONG:
20334 if (decl_specs)
20335 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
20336 type = long_integer_type_node;
20337 break;
20338 case RID_SIGNED:
20339 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
20340 type = integer_type_node;
20341 break;
20342 case RID_UNSIGNED:
20343 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
20344 type = unsigned_type_node;
20345 break;
20346 case RID_FLOAT:
20347 type = float_type_node;
20348 break;
20349 case RID_DOUBLE:
20350 type = double_type_node;
20351 break;
20352 CASE_RID_FLOATN_NX:
20353 type = FLOATN_NX_TYPE_NODE (token->keyword - RID_FLOATN_NX_FIRST);
20354 if (type == NULL_TREE)
20355 error ("%<_Float%d%s%> is not supported on this target",
20356 floatn_nx_types[token->keyword - RID_FLOATN_NX_FIRST].n,
20357 floatn_nx_types[token->keyword - RID_FLOATN_NX_FIRST].extended
20358 ? "x" : "");
20359 break;
20360 case RID_VOID:
20361 type = void_type_node;
20362 break;
20364 case RID_AUTO:
20365 maybe_warn_cpp0x (CPP0X_AUTO);
20366 if (parser->auto_is_implicit_function_template_parm_p)
20368 /* The 'auto' might be the placeholder return type for a function decl
20369 with trailing return type. */
20370 bool have_trailing_return_fn_decl = false;
20372 cp_parser_parse_tentatively (parser);
20373 cp_lexer_consume_token (parser->lexer);
20374 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
20375 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
20376 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
20377 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
20379 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20381 cp_lexer_consume_token (parser->lexer);
20382 cp_parser_skip_to_closing_parenthesis (parser,
20383 /*recovering*/false,
20384 /*or_comma*/false,
20385 /*consume_paren*/true);
20386 continue;
20389 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
20391 have_trailing_return_fn_decl = true;
20392 break;
20395 cp_lexer_consume_token (parser->lexer);
20397 cp_parser_abort_tentative_parse (parser);
20399 if (have_trailing_return_fn_decl)
20401 type = make_auto ();
20402 break;
20405 if (cxx_dialect >= cxx14)
20407 type = synthesize_implicit_template_parm (parser, NULL_TREE);
20408 type = TREE_TYPE (type);
20410 else
20411 type = error_mark_node;
20413 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
20415 if (cxx_dialect < cxx14)
20416 error_at (token->location,
20417 "use of %<auto%> in lambda parameter declaration "
20418 "only available with "
20419 "%<-std=c++14%> or %<-std=gnu++14%>");
20421 else if (!flag_concepts_ts && parser->in_template_argument_list_p)
20422 pedwarn (token->location, 0,
20423 "use of %<auto%> in template argument "
20424 "only available with %<-fconcepts-ts%>");
20425 else if (!flag_concepts)
20426 pedwarn (token->location, 0,
20427 "use of %<auto%> in parameter declaration "
20428 "only available with %<-std=c++20%> or %<-fconcepts%>");
20429 else if (cxx_dialect < cxx14)
20430 error_at (token->location,
20431 "use of %<auto%> in parameter declaration "
20432 "only available with "
20433 "%<-std=c++14%> or %<-std=gnu++14%>");
20435 else
20436 type = make_auto ();
20437 break;
20439 case RID_DECLTYPE:
20440 /* Since DR 743, decltype can either be a simple-type-specifier by
20441 itself or begin a nested-name-specifier. Parsing it will replace
20442 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
20443 handling below decide what to do. */
20444 cp_parser_decltype (parser);
20445 cp_lexer_set_token_position (parser->lexer, token);
20446 break;
20448 case RID_TYPEOF:
20449 /* Consume the `typeof' token. */
20450 cp_lexer_consume_token (parser->lexer);
20451 /* Parse the operand to `typeof'. */
20452 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
20453 /* If it is not already a TYPE, take its type. */
20454 if (!TYPE_P (type))
20455 type = finish_typeof (type);
20457 if (decl_specs)
20458 cp_parser_set_decl_spec_type (decl_specs, type,
20459 token,
20460 /*type_definition_p=*/false);
20462 return type;
20464 default:
20465 /* If token is a type-yielding built-in traits, parse it. */
20466 const cp_trait* trait = cp_lexer_peek_trait_type (parser->lexer);
20467 if (trait)
20469 type = cp_parser_trait (parser, trait);
20470 if (decl_specs)
20471 cp_parser_set_decl_spec_type (decl_specs, type,
20472 token,
20473 /*type_definition_p=*/false);
20475 return type;
20478 break;
20481 /* If token is an already-parsed decltype not followed by ::,
20482 it's a simple-type-specifier. */
20483 if (token->type == CPP_DECLTYPE
20484 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
20486 type = saved_checks_value (token->u.tree_check_value);
20487 if (decl_specs)
20489 cp_parser_set_decl_spec_type (decl_specs, type,
20490 token,
20491 /*type_definition_p=*/false);
20492 /* Remember that we are handling a decltype in order to
20493 implement the resolution of DR 1510 when the argument
20494 isn't instantiation dependent. */
20495 decl_specs->decltype_p = true;
20497 cp_lexer_consume_token (parser->lexer);
20498 return type;
20501 /* If the type-specifier was for a built-in type, we're done. */
20502 if (type)
20504 /* Record the type. */
20505 if (decl_specs
20506 && (token->keyword != RID_SIGNED
20507 && token->keyword != RID_UNSIGNED
20508 && token->keyword != RID_SHORT
20509 && token->keyword != RID_LONG))
20510 cp_parser_set_decl_spec_type (decl_specs,
20511 type,
20512 token,
20513 /*type_definition_p=*/false);
20514 if (decl_specs)
20515 decl_specs->any_specifiers_p = true;
20517 /* Consume the token. */
20518 cp_lexer_consume_token (parser->lexer);
20520 if (type == error_mark_node)
20521 return error_mark_node;
20523 /* There is no valid C++ program where a non-template type is
20524 followed by a "<". That usually indicates that the user thought
20525 that the type was a template. */
20526 cp_parser_check_for_invalid_template_id (parser, type, none_type,
20527 token->location);
20529 return TYPE_NAME (type);
20532 /* The type-specifier must be a user-defined type. */
20533 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
20535 bool qualified_p;
20536 bool global_p;
20537 const bool typename_p = (cxx_dialect >= cxx20
20538 && (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
20540 /* Don't gobble tokens or issue error messages if this is an
20541 optional type-specifier. */
20542 if (flags & CP_PARSER_FLAGS_OPTIONAL)
20543 cp_parser_parse_tentatively (parser);
20545 /* Remember current tentative parsing state -- if we know we need
20546 a type, we can give better diagnostics here. */
20547 bool tent = cp_parser_parsing_tentatively (parser);
20549 token = cp_lexer_peek_token (parser->lexer);
20551 /* Look for the optional `::' operator. */
20552 global_p
20553 = (cp_parser_global_scope_opt (parser,
20554 /*current_scope_valid_p=*/false)
20555 != NULL_TREE);
20556 /* Look for the nested-name specifier. */
20557 qualified_p
20558 = (cp_parser_nested_name_specifier_opt (parser,
20559 /*typename_keyword_p=*/false,
20560 /*check_dependency_p=*/true,
20561 /*type_p=*/false,
20562 /*is_declaration=*/false)
20563 != NULL_TREE);
20564 /* If we have seen a nested-name-specifier, and the next token
20565 is `template', then we are using the template-id production. */
20566 if (parser->scope
20567 && cp_parser_optional_template_keyword (parser))
20569 /* Look for the template-id. */
20570 type = cp_parser_template_id (parser,
20571 /*template_keyword_p=*/true,
20572 /*check_dependency_p=*/true,
20573 none_type,
20574 /*is_declaration=*/false);
20575 /* If the template-id did not name a type, we are out of
20576 luck. */
20577 if (TREE_CODE (type) != TYPE_DECL)
20579 /* ...unless we pretend we have seen 'typename'. */
20580 if (typename_p)
20581 type = cp_parser_make_typename_type (parser, type,
20582 token->location);
20583 else
20585 cp_parser_error (parser, "expected template-id for type");
20586 type = error_mark_node;
20590 /* DR 1812: A < following a qualified-id in a typename-specifier
20591 could safely be assumed to begin a template argument list, so
20592 the template keyword should be optional. */
20593 else if (parser->scope
20594 && qualified_p
20595 && typename_p
20596 && cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID))
20598 cp_parser_parse_tentatively (parser);
20600 type = cp_parser_template_id (parser,
20601 /*template_keyword_p=*/true,
20602 /*check_dependency_p=*/true,
20603 none_type,
20604 /*is_declaration=*/false);
20605 /* This is handled below, so back off. */
20606 if (type && concept_check_p (type))
20607 cp_parser_simulate_error (parser);
20609 if (!cp_parser_parse_definitely (parser))
20610 type = NULL_TREE;
20611 else if (TREE_CODE (type) == TEMPLATE_ID_EXPR)
20612 type = make_typename_type (parser->scope, type, typename_type,
20613 /*complain=*/tf_error);
20614 else if (TREE_CODE (type) != TYPE_DECL)
20615 type = NULL_TREE;
20618 /* Otherwise, look for a type-name. */
20619 if (!type)
20621 if (cxx_dialect >= cxx17 || flag_concepts)
20622 cp_parser_parse_tentatively (parser);
20624 type = cp_parser_type_name (parser, (qualified_p && typename_p));
20626 if ((cxx_dialect >= cxx17 || flag_concepts)
20627 && !cp_parser_parse_definitely (parser))
20628 type = NULL_TREE;
20631 if (!type && flag_concepts && decl_specs)
20633 /* Try for a type-constraint with template arguments. We check
20634 decl_specs here to avoid trying this for a functional cast. */
20636 cp_parser_parse_tentatively (parser);
20638 type = cp_parser_template_id (parser,
20639 /*template_keyword_p=*/false,
20640 /*check_dependency_p=*/true,
20641 none_type,
20642 /*is_declaration=*/false);
20643 if (type && concept_check_p (type))
20645 location_t loc = EXPR_LOCATION (type);
20646 type = cp_parser_placeholder_type_specifier (parser, loc,
20647 type, tent);
20648 if (tent && type == error_mark_node)
20649 /* Perhaps it's a concept-check expression. */
20650 cp_parser_simulate_error (parser);
20652 else
20653 cp_parser_simulate_error (parser);
20655 if (!cp_parser_parse_definitely (parser))
20656 type = NULL_TREE;
20659 if (!type && cxx_dialect >= cxx17)
20661 /* Try class template argument deduction or type-constraint without
20662 template arguments. */
20663 tree name = cp_parser_identifier (parser);
20664 if (name && TREE_CODE (name) == IDENTIFIER_NODE
20665 && parser->scope != error_mark_node)
20667 location_t loc
20668 = cp_lexer_previous_token (parser->lexer)->location;
20669 tree tmpl = cp_parser_lookup_name (parser, name,
20670 none_type,
20671 /*is_template=*/false,
20672 /*is_namespace=*/false,
20673 /*check_dependency=*/true,
20674 /*ambiguous_decls=*/NULL,
20675 token->location);
20676 if (tmpl && tmpl != error_mark_node
20677 && ctad_template_p (tmpl))
20678 type = make_template_placeholder (tmpl);
20679 else if (flag_concepts && tmpl && concept_definition_p (tmpl))
20680 type = cp_parser_placeholder_type_specifier (parser, loc,
20681 tmpl, tent);
20682 else
20684 type = error_mark_node;
20685 if (!cp_parser_simulate_error (parser))
20686 cp_parser_name_lookup_error (parser, name, tmpl,
20687 NLE_TYPE, token->location);
20690 else
20691 type = error_mark_node;
20694 /* If it didn't work out, we don't have a TYPE. */
20695 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
20696 && !cp_parser_parse_definitely (parser))
20697 type = NULL_TREE;
20699 /* Keep track of all name-lookups performed in class scopes. */
20700 if (type
20701 && !global_p
20702 && !qualified_p
20703 && TREE_CODE (type) == TYPE_DECL
20704 && identifier_p (DECL_NAME (type)))
20705 maybe_note_name_used_in_class (DECL_NAME (type), type);
20707 if (type && decl_specs)
20708 cp_parser_set_decl_spec_type (decl_specs, type,
20709 token,
20710 /*type_definition_p=*/false);
20713 /* If we didn't get a type-name, issue an error message. */
20714 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
20716 cp_parser_error (parser, "expected type-name");
20717 return error_mark_node;
20720 if (type && type != error_mark_node)
20722 /* See if TYPE is an Objective-C type, and if so, parse and
20723 accept any protocol references following it. Do this before
20724 the cp_parser_check_for_invalid_template_id() call, because
20725 Objective-C types can be followed by '<...>' which would
20726 enclose protocol names rather than template arguments, and so
20727 everything is fine. */
20728 if (c_dialect_objc () && !parser->scope
20729 && (objc_is_id (type) || objc_is_class_name (type)))
20731 tree protos = cp_parser_objc_protocol_refs_opt (parser);
20732 tree qual_type = objc_get_protocol_qualified_type (type, protos);
20734 /* Clobber the "unqualified" type previously entered into
20735 DECL_SPECS with the new, improved protocol-qualified version. */
20736 if (decl_specs)
20737 decl_specs->type = qual_type;
20739 return qual_type;
20742 /* There is no valid C++ program where a non-template type is
20743 followed by a "<". That usually indicates that the user
20744 thought that the type was a template. */
20745 cp_parser_check_for_invalid_template_id (parser, type,
20746 none_type,
20747 token->location);
20750 return type;
20753 /* Parse the remainder of a placholder-type-specifier.
20755 placeholder-type-specifier:
20756 type-constraint_opt auto
20757 type-constraint_opt decltype(auto)
20759 The raw form of the constraint is parsed in cp_parser_simple_type_specifier
20760 and passed as TMPL. This function converts TMPL to an actual type-constraint,
20761 parses the placeholder type, and performs some contextual syntactic analysis.
20763 LOC provides the location of the template name.
20765 TENTATIVE is true if the type-specifier parsing is tentative; in that case,
20766 don't give an error if TMPL isn't a valid type-constraint, as the template-id
20767 might actually be a concept-check,
20769 Note that the Concepts TS allows the auto or decltype(auto) to be
20770 omitted in a constrained-type-specifier. */
20772 static tree
20773 cp_parser_placeholder_type_specifier (cp_parser *parser, location_t loc,
20774 tree tmpl, bool tentative)
20776 if (tmpl == error_mark_node)
20777 return error_mark_node;
20779 tree orig_tmpl = tmpl;
20781 /* Get the arguments as written for subsequent analysis. */
20782 tree args = NULL_TREE;
20783 if (TREE_CODE (tmpl) == TEMPLATE_ID_EXPR)
20785 args = TREE_OPERAND (tmpl, 1);
20786 tmpl = TREE_OPERAND (tmpl, 0);
20788 else
20789 /* A concept-name with no arguments can't be an expression. */
20790 tentative = false;
20792 tsubst_flags_t complain = tentative ? tf_none : tf_warning_or_error;
20794 /* Get the concept and prototype parameter for the constraint. */
20795 tree_pair info = finish_type_constraints (tmpl, args, complain);
20796 tree con = info.first;
20797 tree proto = info.second;
20798 if (con == error_mark_node)
20799 return error_mark_node;
20801 /* As per the standard, require auto or decltype(auto), except in some
20802 cases (template parameter lists, -fconcepts-ts enabled). */
20803 cp_token *placeholder = NULL, *close_paren = NULL;
20804 if (cxx_dialect >= cxx20)
20806 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
20807 placeholder = cp_lexer_consume_token (parser->lexer);
20808 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DECLTYPE))
20810 placeholder = cp_lexer_consume_token (parser->lexer);
20811 matching_parens parens;
20812 parens.require_open (parser);
20813 cp_parser_require_keyword (parser, RID_AUTO, RT_AUTO);
20814 close_paren = parens.require_close (parser);
20818 /* A type constraint constrains a contextually determined type or type
20819 parameter pack. However, the Concepts TS does allow concepts
20820 to introduce non-type and template template parameters. */
20821 if (TREE_CODE (proto) != TYPE_DECL)
20823 if (!flag_concepts_ts
20824 || !processing_template_parmlist)
20826 if (!tentative)
20828 error_at (loc, "%qE does not constrain a type", DECL_NAME (con));
20829 inform (DECL_SOURCE_LOCATION (con), "concept defined here");
20831 return error_mark_node;
20835 /* In a template parameter list, a type-parameter can be introduced
20836 by type-constraints alone. */
20837 if (processing_template_parmlist && !placeholder)
20839 /* In a default argument we may not be creating new parameters. */
20840 if (parser->local_variables_forbidden_p & LOCAL_VARS_FORBIDDEN)
20842 /* If this assert turns out to be false, do error() instead. */
20843 gcc_assert (tentative);
20844 return error_mark_node;
20846 return build_constrained_parameter (con, proto, args);
20849 /* Diagnose issues placeholder issues. */
20850 if (!flag_concepts_ts
20851 && !parser->in_result_type_constraint_p
20852 && !placeholder)
20854 if (tentative)
20855 /* Perhaps it's a concept-check expression (c++/91073). */
20856 return error_mark_node;
20858 tree id = build_nt (TEMPLATE_ID_EXPR, tmpl, args);
20859 tree expr = DECL_P (orig_tmpl) ? DECL_NAME (con) : id;
20860 error_at (input_location,
20861 "expected %<auto%> or %<decltype(auto)%> after %qE", expr);
20862 /* Fall through. This is an error of omission. */
20864 else if (parser->in_result_type_constraint_p && placeholder)
20866 /* A trailing return type only allows type-constraints. */
20867 error_at (input_location,
20868 "unexpected placeholder in constrained result type");
20871 /* In a parameter-declaration-clause, a placeholder-type-specifier
20872 results in an invented template parameter. */
20873 if (parser->auto_is_implicit_function_template_parm_p)
20875 if (close_paren)
20877 location_t loc = make_location (placeholder->location,
20878 placeholder->location,
20879 close_paren->location);
20880 error_at (loc, "cannot declare a parameter with %<decltype(auto)%>");
20881 return error_mark_node;
20883 tree parm = build_constrained_parameter (con, proto, args);
20884 return synthesize_implicit_template_parm (parser, parm);
20887 /* Determine if the type should be deduced using template argument
20888 deduction or decltype deduction. Note that the latter is always
20889 used for type-constraints in trailing return types. */
20890 bool decltype_p = placeholder
20891 ? placeholder->keyword == RID_DECLTYPE
20892 : parser->in_result_type_constraint_p;
20894 /* Otherwise, this is the type of a variable or return type. */
20895 if (decltype_p)
20896 return make_constrained_decltype_auto (con, args);
20897 else
20898 return make_constrained_auto (con, args);
20901 /* Parse a type-name.
20903 type-name:
20904 class-name
20905 enum-name
20906 typedef-name
20907 simple-template-id [in c++0x]
20909 enum-name:
20910 identifier
20912 typedef-name:
20913 identifier
20915 Concepts:
20917 type-name:
20918 concept-name
20919 partial-concept-id
20921 concept-name:
20922 identifier
20924 Returns a TYPE_DECL for the type. */
20926 static tree
20927 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
20929 tree type_decl;
20931 /* We can't know yet whether it is a class-name or not. */
20932 cp_parser_parse_tentatively (parser);
20933 /* Try a class-name. */
20934 type_decl = cp_parser_class_name (parser,
20935 typename_keyword_p,
20936 /*template_keyword_p=*/false,
20937 none_type,
20938 /*check_dependency_p=*/true,
20939 /*class_head_p=*/false,
20940 /*is_declaration=*/false);
20941 /* If it's not a class-name, keep looking. */
20942 if (!cp_parser_parse_definitely (parser))
20944 if (cxx_dialect < cxx11)
20945 /* It must be a typedef-name or an enum-name. */
20946 return cp_parser_nonclass_name (parser);
20948 cp_parser_parse_tentatively (parser);
20949 /* It is either a simple-template-id representing an
20950 instantiation of an alias template... */
20951 type_decl = cp_parser_template_id (parser,
20952 /*template_keyword_p=*/false,
20953 /*check_dependency_p=*/true,
20954 none_type,
20955 /*is_declaration=*/false);
20956 /* Note that this must be an instantiation of an alias template
20957 because [temp.names]/6 says:
20959 A template-id that names an alias template specialization
20960 is a type-name.
20962 Whereas [temp.names]/7 says:
20964 A simple-template-id that names a class template
20965 specialization is a class-name.
20967 With concepts, this could also be a partial-concept-id that
20968 declares a non-type template parameter. */
20969 if (type_decl != NULL_TREE
20970 && TREE_CODE (type_decl) == TYPE_DECL
20971 && TYPE_DECL_ALIAS_P (type_decl))
20972 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
20973 else
20974 cp_parser_simulate_error (parser);
20976 if (!cp_parser_parse_definitely (parser))
20977 /* ... Or a typedef-name or an enum-name. */
20978 return cp_parser_nonclass_name (parser);
20981 return type_decl;
20984 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
20985 or a concept-name.
20987 enum-name:
20988 identifier
20990 typedef-name:
20991 identifier
20993 concept-name:
20994 identifier
20996 Returns a TYPE_DECL for the type. */
20998 static tree
20999 cp_parser_nonclass_name (cp_parser* parser)
21001 tree type_decl;
21002 tree identifier;
21004 cp_token *token = cp_lexer_peek_token (parser->lexer);
21005 identifier = cp_parser_identifier (parser);
21006 if (identifier == error_mark_node)
21007 return error_mark_node;
21009 /* Look up the type-name. */
21010 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
21012 type_decl = strip_using_decl (type_decl);
21014 if (TREE_CODE (type_decl) != TYPE_DECL
21015 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
21017 /* See if this is an Objective-C type. */
21018 tree protos = cp_parser_objc_protocol_refs_opt (parser);
21019 tree type = objc_get_protocol_qualified_type (identifier, protos);
21020 if (type)
21021 type_decl = TYPE_NAME (type);
21024 /* Issue an error if we did not find a type-name. */
21025 if (TREE_CODE (type_decl) != TYPE_DECL
21026 /* In Objective-C, we have the complication that class names are
21027 normally type names and start declarations (eg, the
21028 "NSObject" in "NSObject *object;"), but can be used in an
21029 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
21030 is an expression. So, a classname followed by a dot is not a
21031 valid type-name. */
21032 || (objc_is_class_name (TREE_TYPE (type_decl))
21033 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
21035 if (!cp_parser_simulate_error (parser))
21036 cp_parser_name_lookup_error (parser, identifier, type_decl,
21037 NLE_TYPE, token->location);
21038 return error_mark_node;
21040 /* Remember that the name was used in the definition of the
21041 current class so that we can check later to see if the
21042 meaning would have been different after the class was
21043 entirely defined. */
21044 else if (type_decl != error_mark_node
21045 && !parser->scope)
21046 maybe_note_name_used_in_class (identifier, type_decl);
21048 return type_decl;
21051 /* Parse an elaborated-type-specifier. Note that the grammar given
21052 here incorporates the resolution to DR68.
21054 elaborated-type-specifier:
21055 class-key :: [opt] nested-name-specifier [opt] identifier
21056 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
21057 enum-key :: [opt] nested-name-specifier [opt] identifier
21058 typename :: [opt] nested-name-specifier identifier
21059 typename :: [opt] nested-name-specifier template [opt]
21060 template-id
21062 GNU extension:
21064 elaborated-type-specifier:
21065 class-key attributes :: [opt] nested-name-specifier [opt] identifier
21066 class-key attributes :: [opt] nested-name-specifier [opt]
21067 template [opt] template-id
21068 enum attributes :: [opt] nested-name-specifier [opt] identifier
21070 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
21071 declared `friend'. If IS_DECLARATION is TRUE, then this
21072 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
21073 something is being declared.
21075 Returns the TYPE specified. */
21077 static tree
21078 cp_parser_elaborated_type_specifier (cp_parser* parser,
21079 bool is_friend,
21080 bool is_declaration)
21082 enum tag_types tag_type;
21083 tree identifier;
21084 tree type = NULL_TREE;
21085 tree attributes = NULL_TREE;
21086 tree globalscope;
21087 cp_token *token = NULL;
21089 /* For class and enum types the location of the class-key or enum-key. */
21090 location_t key_loc = cp_lexer_peek_token (parser->lexer)->location;
21091 /* For a scoped enum, the 'class' or 'struct' keyword id. */
21092 rid scoped_key = RID_MAX;
21094 /* See if we're looking at the `enum' keyword. */
21095 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
21097 /* Consume the `enum' token. */
21098 cp_lexer_consume_token (parser->lexer);
21099 /* Remember that it's an enumeration type. */
21100 tag_type = enum_type;
21101 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
21102 enums) is used here. */
21103 cp_token *token = cp_lexer_peek_token (parser->lexer);
21104 if (cp_parser_is_keyword (token, scoped_key = RID_CLASS)
21105 || cp_parser_is_keyword (token, scoped_key = RID_STRUCT))
21107 location_t loc = token->location;
21108 gcc_rich_location richloc (loc);
21109 richloc.add_range (input_location);
21110 richloc.add_fixit_remove ();
21111 pedwarn (&richloc, 0, "elaborated-type-specifier for "
21112 "a scoped enum must not use the %qD keyword",
21113 token->u.value);
21114 /* Consume the `struct' or `class' and parse it anyway. */
21115 cp_lexer_consume_token (parser->lexer);
21116 /* Create a combined location for the whole scoped-enum-key. */
21117 key_loc = make_location (key_loc, key_loc, loc);
21119 else
21120 scoped_key = RID_MAX;
21122 /* Parse the attributes. */
21123 attributes = cp_parser_attributes_opt (parser);
21125 /* Or, it might be `typename'. */
21126 else if (cp_lexer_next_token_is_keyword (parser->lexer,
21127 RID_TYPENAME))
21129 /* Consume the `typename' token. */
21130 cp_lexer_consume_token (parser->lexer);
21131 /* Remember that it's a `typename' type. */
21132 tag_type = typename_type;
21134 /* Otherwise it must be a class-key. */
21135 else
21137 key_loc = cp_lexer_peek_token (parser->lexer)->location;
21138 tag_type = cp_parser_class_key (parser);
21139 if (tag_type == none_type)
21140 return error_mark_node;
21141 /* Parse the attributes. */
21142 attributes = cp_parser_attributes_opt (parser);
21145 /* Look for the `::' operator. */
21146 globalscope = cp_parser_global_scope_opt (parser,
21147 /*current_scope_valid_p=*/false);
21148 /* Look for the nested-name-specifier. */
21149 tree nested_name_specifier;
21150 if (tag_type == typename_type && !globalscope)
21152 nested_name_specifier
21153 = cp_parser_nested_name_specifier (parser,
21154 /*typename_keyword_p=*/true,
21155 /*check_dependency_p=*/true,
21156 /*type_p=*/true,
21157 is_declaration);
21158 if (!nested_name_specifier)
21159 return error_mark_node;
21161 else
21162 /* Even though `typename' is not present, the proposed resolution
21163 to Core Issue 180 says that in `class A<T>::B', `B' should be
21164 considered a type-name, even if `A<T>' is dependent. */
21165 nested_name_specifier
21166 = cp_parser_nested_name_specifier_opt (parser,
21167 /*typename_keyword_p=*/true,
21168 /*check_dependency_p=*/true,
21169 /*type_p=*/true,
21170 is_declaration);
21171 /* For everything but enumeration types, consider a template-id.
21172 For an enumeration type, consider only a plain identifier. */
21173 if (tag_type != enum_type)
21175 bool template_p = false;
21176 tree decl;
21178 /* Allow the `template' keyword. */
21179 template_p = cp_parser_optional_template_keyword (parser);
21180 /* If we didn't see `template', we don't know if there's a
21181 template-id or not. */
21182 if (!template_p)
21183 cp_parser_parse_tentatively (parser);
21184 /* The `template' keyword must follow a nested-name-specifier. */
21185 else if (!nested_name_specifier && !globalscope)
21187 cp_parser_error (parser, "%<template%> must follow a nested-"
21188 "name-specifier");
21189 return error_mark_node;
21192 /* Parse the template-id. */
21193 token = cp_lexer_peek_token (parser->lexer);
21194 decl = cp_parser_template_id (parser, template_p,
21195 /*check_dependency_p=*/true,
21196 tag_type,
21197 is_declaration);
21198 /* If we didn't find a template-id, look for an ordinary
21199 identifier. */
21200 if (!template_p && !cp_parser_parse_definitely (parser))
21202 /* We can get here when cp_parser_template_id, called by
21203 cp_parser_class_name with tag_type == none_type, succeeds
21204 and caches a BASELINK. Then, when called again here,
21205 instead of failing and returning an error_mark_node
21206 returns it (see template/typename17.C in C++11).
21207 ??? Could we diagnose this earlier? */
21208 else if (tag_type == typename_type && BASELINK_P (decl))
21210 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
21211 type = error_mark_node;
21213 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
21214 in effect, then we must assume that, upon instantiation, the
21215 template will correspond to a class. */
21216 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
21217 && tag_type == typename_type)
21218 type = make_typename_type (parser->scope, decl,
21219 typename_type,
21220 /*complain=*/tf_error);
21221 /* If the `typename' keyword is in effect and DECL is not a type
21222 decl, then type is non existent. */
21223 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
21225 else if (TREE_CODE (decl) == TYPE_DECL)
21227 type = check_elaborated_type_specifier (tag_type, decl,
21228 /*allow_template_p=*/true);
21230 /* If the next token is a semicolon, this must be a specialization,
21231 instantiation, or friend declaration. Check the scope while we
21232 still know whether or not we had a nested-name-specifier. */
21233 if (type != error_mark_node
21234 && !nested_name_specifier && !is_friend
21235 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21236 check_unqualified_spec_or_inst (type, token->location);
21238 else if (decl == error_mark_node)
21239 type = error_mark_node;
21242 if (!type)
21244 token = cp_lexer_peek_token (parser->lexer);
21245 identifier = cp_parser_identifier (parser);
21247 if (identifier == error_mark_node)
21249 parser->scope = NULL_TREE;
21250 return error_mark_node;
21253 /* For a `typename', we needn't call xref_tag. */
21254 if (tag_type == typename_type
21255 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
21256 return cp_parser_make_typename_type (parser, identifier,
21257 token->location);
21259 /* Template parameter lists apply only if we are not within a
21260 function parameter list. */
21261 bool template_parm_lists_apply
21262 = parser->num_template_parameter_lists;
21263 if (template_parm_lists_apply)
21264 for (cp_binding_level *s = current_binding_level;
21265 s && s->kind != sk_template_parms;
21266 s = s->level_chain)
21267 if (s->kind == sk_function_parms)
21268 template_parm_lists_apply = false;
21270 /* Look up a qualified name in the usual way. */
21271 if (parser->scope)
21273 tree decl;
21274 tree ambiguous_decls;
21276 decl = cp_parser_lookup_name (parser, identifier,
21277 tag_type,
21278 /*is_template=*/false,
21279 /*is_namespace=*/false,
21280 /*check_dependency=*/true,
21281 &ambiguous_decls,
21282 token->location);
21284 /* If the lookup was ambiguous, an error will already have been
21285 issued. */
21286 if (ambiguous_decls)
21287 return error_mark_node;
21289 /* If we are parsing friend declaration, DECL may be a
21290 TEMPLATE_DECL tree node here. However, we need to check
21291 whether this TEMPLATE_DECL results in valid code. Consider
21292 the following example:
21294 namespace N {
21295 template <class T> class C {};
21297 class X {
21298 template <class T> friend class N::C; // #1, valid code
21300 template <class T> class Y {
21301 friend class N::C; // #2, invalid code
21304 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
21305 name lookup of `N::C'. We see that friend declaration must
21306 be template for the code to be valid. Note that
21307 processing_template_decl does not work here since it is
21308 always 1 for the above two cases. */
21310 decl = (cp_parser_maybe_treat_template_as_class
21311 (decl, /*tag_name_p=*/is_friend
21312 && template_parm_lists_apply));
21314 if (TREE_CODE (decl) != TYPE_DECL)
21316 cp_parser_diagnose_invalid_type_name (parser,
21317 identifier,
21318 token->location);
21319 return error_mark_node;
21322 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
21324 bool allow_template = (template_parm_lists_apply
21325 || DECL_SELF_REFERENCE_P (decl));
21326 type = check_elaborated_type_specifier (tag_type, decl,
21327 allow_template);
21329 if (type == error_mark_node)
21330 return error_mark_node;
21333 /* Forward declarations of nested types, such as
21335 class C1::C2;
21336 class C1::C2::C3;
21338 are invalid unless all components preceding the final '::'
21339 are complete. If all enclosing types are complete, these
21340 declarations become merely pointless.
21342 Invalid forward declarations of nested types are errors
21343 caught elsewhere in parsing. Those that are pointless arrive
21344 here. */
21346 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21347 && !is_friend && is_declaration
21348 && !processing_explicit_instantiation)
21349 warning (0, "declaration %qD does not declare anything", decl);
21351 type = TREE_TYPE (decl);
21353 else
21355 /* An elaborated-type-specifier sometimes introduces a new type and
21356 sometimes names an existing type. Normally, the rule is that it
21357 introduces a new type only if there is not an existing type of
21358 the same name already in scope. For example, given:
21360 struct S {};
21361 void f() { struct S s; }
21363 the `struct S' in the body of `f' is the same `struct S' as in
21364 the global scope; the existing definition is used. However, if
21365 there were no global declaration, this would introduce a new
21366 local class named `S'.
21368 An exception to this rule applies to the following code:
21370 namespace N { struct S; }
21372 Here, the elaborated-type-specifier names a new type
21373 unconditionally; even if there is already an `S' in the
21374 containing scope this declaration names a new type.
21375 This exception only applies if the elaborated-type-specifier
21376 forms the complete declaration:
21378 [class.name]
21380 A declaration consisting solely of `class-key identifier ;' is
21381 either a redeclaration of the name in the current scope or a
21382 forward declaration of the identifier as a class name. It
21383 introduces the name into the current scope.
21385 We are in this situation precisely when the next token is a `;'.
21387 An exception to the exception is that a `friend' declaration does
21388 *not* name a new type; i.e., given:
21390 struct S { friend struct T; };
21392 `T' is not a new type in the scope of `S'.
21394 Also, `new struct S' or `sizeof (struct S)' never results in the
21395 definition of a new type; a new type can only be declared in a
21396 declaration context. */
21398 TAG_how how;
21400 if (is_friend)
21401 /* Friends have special name lookup rules. */
21402 how = TAG_how::HIDDEN_FRIEND;
21403 else if (is_declaration
21404 && cp_lexer_next_token_is (parser->lexer,
21405 CPP_SEMICOLON))
21406 /* This is a `class-key identifier ;' */
21407 how = TAG_how::CURRENT_ONLY;
21408 else
21409 how = TAG_how::GLOBAL;
21411 bool template_p =
21412 (template_parm_lists_apply
21413 && (cp_parser_next_token_starts_class_definition_p (parser)
21414 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
21415 /* An unqualified name was used to reference this type, so
21416 there were no qualifying templates. */
21417 if (template_parm_lists_apply
21418 && !cp_parser_check_template_parameters (parser,
21419 /*num_templates=*/0,
21420 /*template_id*/false,
21421 token->location,
21422 /*declarator=*/NULL))
21423 return error_mark_node;
21425 type = xref_tag (tag_type, identifier, how, template_p);
21429 if (type == error_mark_node)
21430 return error_mark_node;
21432 /* Allow attributes on forward declarations of classes. */
21433 if (attributes)
21435 if (TREE_CODE (type) == TYPENAME_TYPE)
21436 warning (OPT_Wattributes,
21437 "attributes ignored on uninstantiated type");
21438 else if (tag_type != enum_type
21439 && TREE_CODE (type) != BOUND_TEMPLATE_TEMPLATE_PARM
21440 && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
21441 && ! processing_explicit_instantiation)
21442 warning (OPT_Wattributes,
21443 "attributes ignored on template instantiation");
21444 else if (is_friend && cxx11_attribute_p (attributes))
21446 if (warning (OPT_Wattributes, "attribute ignored"))
21447 inform (input_location, "an attribute that appertains to a friend "
21448 "declaration that is not a definition is ignored");
21450 else if (is_declaration && cp_parser_declares_only_class_p (parser))
21451 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
21452 else
21453 warning (OPT_Wattributes,
21454 "attributes ignored on elaborated-type-specifier that is "
21455 "not a forward declaration");
21458 if (tag_type == enum_type)
21459 cp_parser_maybe_warn_enum_key (parser, key_loc, type, scoped_key);
21460 else
21462 /* Diagnose class/struct/union mismatches. IS_DECLARATION is false
21463 for alias definition. */
21464 bool decl_class = (is_declaration
21465 && cp_parser_declares_only_class_p (parser));
21466 cp_parser_check_class_key (parser, key_loc, tag_type, type, false,
21467 decl_class);
21469 /* Indicate whether this class was declared as a `class' or as a
21470 `struct'. */
21471 if (CLASS_TYPE_P (type) && !currently_open_class (type))
21472 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
21475 /* A "<" cannot follow an elaborated type specifier. If that
21476 happens, the user was probably trying to form a template-id. */
21477 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
21478 token->location);
21480 return type;
21483 /* Parse an enum-specifier.
21485 enum-specifier:
21486 enum-head { enumerator-list [opt] }
21487 enum-head { enumerator-list , } [C++0x]
21489 enum-head:
21490 enum-key identifier [opt] enum-base [opt]
21491 enum-key nested-name-specifier identifier enum-base [opt]
21493 enum-key:
21494 enum
21495 enum class [C++0x]
21496 enum struct [C++0x]
21498 enum-base: [C++0x]
21499 : type-specifier-seq
21501 opaque-enum-specifier:
21502 enum-key identifier enum-base [opt] ;
21504 GNU Extensions:
21505 enum-key attributes[opt] identifier [opt] enum-base [opt]
21506 { enumerator-list [opt] }attributes[opt]
21507 enum-key attributes[opt] identifier [opt] enum-base [opt]
21508 { enumerator-list, }attributes[opt] [C++0x]
21510 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
21511 if the token stream isn't an enum-specifier after all. */
21513 static tree
21514 cp_parser_enum_specifier (cp_parser* parser)
21516 tree identifier;
21517 tree type = NULL_TREE;
21518 tree prev_scope;
21519 tree nested_name_specifier = NULL_TREE;
21520 tree attributes;
21521 bool scoped_enum_p = false;
21522 bool has_underlying_type = false;
21523 bool nested_being_defined = false;
21524 bool new_value_list = false;
21525 bool is_new_type = false;
21526 bool is_unnamed = false;
21527 tree underlying_type = NULL_TREE;
21528 cp_token *type_start_token = NULL;
21529 auto cleanup = make_temp_override (parser->colon_corrects_to_scope_p, false);
21531 /* Parse tentatively so that we can back up if we don't find a
21532 enum-specifier. */
21533 cp_parser_parse_tentatively (parser);
21535 /* Caller guarantees that the current token is 'enum', an identifier
21536 possibly follows, and the token after that is an opening brace.
21537 If we don't have an identifier, fabricate an anonymous name for
21538 the enumeration being defined. */
21539 cp_lexer_consume_token (parser->lexer);
21541 /* Parse the "class" or "struct", which indicates a scoped
21542 enumeration type in C++0x. */
21543 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
21544 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
21546 if (cxx_dialect < cxx11)
21547 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
21549 /* Consume the `struct' or `class' token. */
21550 cp_lexer_consume_token (parser->lexer);
21552 scoped_enum_p = true;
21555 attributes = cp_parser_attributes_opt (parser);
21557 /* Clear the qualification. */
21558 parser->scope = NULL_TREE;
21559 parser->qualifying_scope = NULL_TREE;
21560 parser->object_scope = NULL_TREE;
21562 /* Figure out in what scope the declaration is being placed. */
21563 prev_scope = current_scope ();
21565 type_start_token = cp_lexer_peek_token (parser->lexer);
21567 push_deferring_access_checks (dk_no_check);
21568 nested_name_specifier
21569 = cp_parser_nested_name_specifier_opt (parser,
21570 /*typename_keyword_p=*/true,
21571 /*check_dependency_p=*/false,
21572 /*type_p=*/false,
21573 /*is_declaration=*/false);
21575 if (nested_name_specifier)
21577 tree name;
21579 identifier = cp_parser_identifier (parser);
21580 name = cp_parser_lookup_name (parser, identifier,
21581 enum_type,
21582 /*is_template=*/false,
21583 /*is_namespace=*/false,
21584 /*check_dependency=*/true,
21585 /*ambiguous_decls=*/NULL,
21586 input_location);
21587 if (name && name != error_mark_node)
21589 type = TREE_TYPE (name);
21590 if (TREE_CODE (type) == TYPENAME_TYPE)
21592 /* Are template enums allowed in ISO? */
21593 if (template_parm_scope_p ())
21594 pedwarn (type_start_token->location, OPT_Wpedantic,
21595 "%qD is an enumeration template", name);
21596 /* ignore a typename reference, for it will be solved by name
21597 in start_enum. */
21598 type = NULL_TREE;
21601 else if (nested_name_specifier == error_mark_node)
21602 /* We already issued an error. */;
21603 else
21605 error_at (type_start_token->location,
21606 "%qD does not name an enumeration in %qT",
21607 identifier, nested_name_specifier);
21608 nested_name_specifier = error_mark_node;
21611 else
21613 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21614 identifier = cp_parser_identifier (parser);
21615 else
21617 identifier = make_anon_name ();
21618 is_unnamed = true;
21619 if (scoped_enum_p)
21620 error_at (type_start_token->location,
21621 "unnamed scoped enum is not allowed");
21624 pop_deferring_access_checks ();
21626 /* Check for the `:' that denotes a specified underlying type in C++0x.
21627 Note that a ':' could also indicate a bitfield width, however. */
21628 location_t colon_loc = UNKNOWN_LOCATION;
21629 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
21631 cp_decl_specifier_seq type_specifiers;
21633 /* Consume the `:'. */
21634 colon_loc = cp_lexer_peek_token (parser->lexer)->location;
21635 cp_lexer_consume_token (parser->lexer);
21637 auto tdf
21638 = make_temp_override (parser->type_definition_forbidden_message,
21639 G_("types may not be defined in enum-base"));
21641 /* Parse the type-specifier-seq. */
21642 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
21643 /*is_declaration=*/false,
21644 /*is_trailing_return=*/false,
21645 &type_specifiers);
21647 /* At this point this is surely not elaborated type specifier. */
21648 if (!cp_parser_parse_definitely (parser))
21649 return NULL_TREE;
21651 if (cxx_dialect < cxx11)
21652 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
21654 has_underlying_type = true;
21656 /* If that didn't work, stop. */
21657 if (type_specifiers.type != error_mark_node)
21659 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
21660 /*initialized=*/0, NULL);
21661 if (underlying_type == error_mark_node
21662 || check_for_bare_parameter_packs (underlying_type))
21663 underlying_type = NULL_TREE;
21667 /* Look for the `{' but don't consume it yet. */
21668 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21670 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
21672 if (has_underlying_type)
21673 cp_parser_commit_to_tentative_parse (parser);
21674 cp_parser_error (parser, "expected %<{%>");
21675 if (has_underlying_type)
21676 return error_mark_node;
21678 /* An opaque-enum-specifier must have a ';' here. */
21679 if ((scoped_enum_p || underlying_type)
21680 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21682 if (has_underlying_type)
21683 pedwarn (colon_loc,
21684 OPT_Welaborated_enum_base,
21685 "declaration of enumeration with "
21686 "fixed underlying type and no enumerator list is "
21687 "only permitted as a standalone declaration");
21688 else
21689 cp_parser_error (parser, "expected %<;%> or %<{%>");
21693 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
21694 return NULL_TREE;
21696 if (nested_name_specifier)
21698 if (CLASS_TYPE_P (nested_name_specifier))
21700 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
21701 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
21702 push_scope (nested_name_specifier);
21704 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
21705 push_nested_namespace (nested_name_specifier);
21708 /* Issue an error message if type-definitions are forbidden here. */
21709 if (!cp_parser_check_type_definition (parser))
21710 type = error_mark_node;
21711 else
21712 /* Create the new type. We do this before consuming the opening
21713 brace so the enum will be recorded as being on the line of its
21714 tag (or the 'enum' keyword, if there is no tag). */
21715 type = start_enum (identifier, type, underlying_type,
21716 attributes, scoped_enum_p, &is_new_type);
21718 /* If the next token is not '{' it is an opaque-enum-specifier or an
21719 elaborated-type-specifier. */
21720 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21722 auto_timevar tv (TV_PARSE_ENUM);
21724 if (nested_name_specifier
21725 && nested_name_specifier != error_mark_node)
21727 /* The following catches invalid code such as:
21728 enum class S<int>::E { A, B, C }; */
21729 if (!processing_specialization
21730 && CLASS_TYPE_P (nested_name_specifier)
21731 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
21732 error_at (type_start_token->location, "cannot add an enumerator "
21733 "list to a template instantiation");
21735 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
21737 error_at (type_start_token->location,
21738 "%<%T::%E%> has not been declared",
21739 TYPE_CONTEXT (nested_name_specifier),
21740 nested_name_specifier);
21741 type = error_mark_node;
21743 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
21744 && !CLASS_TYPE_P (nested_name_specifier))
21746 error_at (type_start_token->location, "nested name specifier "
21747 "%qT for enum declaration does not name a class "
21748 "or namespace", nested_name_specifier);
21749 type = error_mark_node;
21751 /* If that scope does not contain the scope in which the
21752 class was originally declared, the program is invalid. */
21753 else if (prev_scope && !is_ancestor (prev_scope,
21754 nested_name_specifier))
21756 if (at_namespace_scope_p ())
21757 error_at (type_start_token->location,
21758 "declaration of %qD in namespace %qD which does not "
21759 "enclose %qD",
21760 type, prev_scope, nested_name_specifier);
21761 else
21762 error_at (type_start_token->location,
21763 "declaration of %qD in %qD which does not "
21764 "enclose %qD",
21765 type, prev_scope, nested_name_specifier);
21766 type = error_mark_node;
21768 /* If that scope is the scope where the declaration is being placed
21769 the program is invalid. */
21770 else if (CLASS_TYPE_P (nested_name_specifier)
21771 && CLASS_TYPE_P (prev_scope)
21772 && same_type_p (nested_name_specifier, prev_scope))
21774 permerror (type_start_token->location,
21775 "extra qualification not allowed");
21776 nested_name_specifier = NULL_TREE;
21780 if (scoped_enum_p)
21781 begin_scope (sk_scoped_enum, type);
21783 /* Consume the opening brace. */
21784 matching_braces braces;
21785 braces.consume_open (parser);
21787 if (type == error_mark_node)
21788 ; /* Nothing to add */
21789 else if (OPAQUE_ENUM_P (type)
21790 || (cxx_dialect > cxx98 && processing_specialization))
21792 new_value_list = true;
21793 SET_OPAQUE_ENUM_P (type, false);
21794 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
21796 else
21798 error_at (type_start_token->location,
21799 "multiple definition of %q#T", type);
21800 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
21801 "previous definition here");
21802 type = error_mark_node;
21805 if (type == error_mark_node)
21806 cp_parser_skip_to_end_of_block_or_statement (parser);
21807 /* If the next token is not '}', then there are some enumerators. */
21808 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21810 if (is_unnamed && !scoped_enum_p
21811 /* Don't warn for enum {} a; here. */
21812 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SEMICOLON))
21813 pedwarn (type_start_token->location, OPT_Wpedantic,
21814 "ISO C++ forbids empty unnamed enum");
21816 else
21818 /* We've seen a '{' so we know we're in an enum-specifier.
21819 Commit to any tentative parse to get syntax errors. */
21820 cp_parser_commit_to_tentative_parse (parser);
21821 cp_parser_enumerator_list (parser, type);
21824 /* Consume the final '}'. */
21825 braces.require_close (parser);
21827 if (scoped_enum_p)
21828 finish_scope ();
21830 else
21832 /* If a ';' follows, then it is an opaque-enum-specifier
21833 and additional restrictions apply. */
21834 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21836 if (is_unnamed)
21837 error_at (type_start_token->location,
21838 "opaque-enum-specifier without name");
21839 else if (nested_name_specifier)
21840 error_at (type_start_token->location,
21841 "opaque-enum-specifier must use a simple identifier");
21845 /* Look for trailing attributes to apply to this enumeration, and
21846 apply them if appropriate. */
21847 if (cp_parser_allow_gnu_extensions_p (parser))
21849 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
21850 cplus_decl_attributes (&type,
21851 trailing_attr,
21852 (int) ATTR_FLAG_TYPE_IN_PLACE);
21855 /* Finish up the enumeration. */
21856 if (type != error_mark_node)
21858 if (new_value_list)
21859 finish_enum_value_list (type);
21860 if (is_new_type)
21861 finish_enum (type);
21864 if (nested_name_specifier)
21866 if (CLASS_TYPE_P (nested_name_specifier))
21868 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
21869 pop_scope (nested_name_specifier);
21871 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
21872 pop_nested_namespace (nested_name_specifier);
21874 return type;
21877 /* Parse an enumerator-list. The enumerators all have the indicated
21878 TYPE.
21880 enumerator-list:
21881 enumerator-definition
21882 enumerator-list , enumerator-definition */
21884 static void
21885 cp_parser_enumerator_list (cp_parser* parser, tree type)
21887 while (true)
21889 /* Parse an enumerator-definition. */
21890 cp_parser_enumerator_definition (parser, type);
21892 /* If the next token is not a ',', we've reached the end of
21893 the list. */
21894 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21895 break;
21896 /* Otherwise, consume the `,' and keep going. */
21897 cp_lexer_consume_token (parser->lexer);
21898 /* If the next token is a `}', there is a trailing comma. */
21899 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21901 if (cxx_dialect < cxx11)
21902 pedwarn (input_location, OPT_Wpedantic,
21903 "comma at end of enumerator list");
21904 break;
21909 /* Parse an enumerator-definition. The enumerator has the indicated
21910 TYPE.
21912 enumerator-definition:
21913 enumerator
21914 enumerator = constant-expression
21916 enumerator:
21917 identifier
21919 GNU Extensions:
21921 enumerator-definition:
21922 enumerator attributes [opt]
21923 enumerator attributes [opt] = constant-expression */
21925 static void
21926 cp_parser_enumerator_definition (cp_parser* parser, tree type)
21928 tree identifier;
21929 tree value;
21930 location_t loc;
21932 /* Save the input location because we are interested in the location
21933 of the identifier and not the location of the explicit value. */
21934 loc = cp_lexer_peek_token (parser->lexer)->location;
21936 /* Look for the identifier. */
21937 identifier = cp_parser_identifier (parser);
21938 if (identifier == error_mark_node)
21939 return;
21941 /* Parse any specified attributes. */
21942 tree attrs = cp_parser_attributes_opt (parser);
21944 /* If the next token is an '=', then there is an explicit value. */
21945 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21947 /* Consume the `=' token. */
21948 cp_lexer_consume_token (parser->lexer);
21949 /* Parse the value. */
21950 value = cp_parser_constant_expression (parser);
21952 else
21953 value = NULL_TREE;
21955 /* If we are processing a template, make sure the initializer of the
21956 enumerator doesn't contain any bare template parameter pack. */
21957 if (current_lambda_expr ())
21959 /* In a lambda it should work, but doesn't currently. */
21960 if (uses_parameter_packs (value))
21962 sorry ("unexpanded parameter pack in enumerator in lambda");
21963 value = error_mark_node;
21966 else if (check_for_bare_parameter_packs (value))
21967 value = error_mark_node;
21969 /* Create the enumerator. */
21970 build_enumerator (identifier, value, type, attrs, loc);
21973 /* Parse a namespace-name.
21975 namespace-name:
21976 original-namespace-name
21977 namespace-alias
21979 Returns the NAMESPACE_DECL for the namespace. */
21981 static tree
21982 cp_parser_namespace_name (cp_parser* parser)
21984 tree identifier;
21985 tree namespace_decl;
21987 cp_token *token = cp_lexer_peek_token (parser->lexer);
21989 /* Get the name of the namespace. */
21990 identifier = cp_parser_identifier (parser);
21991 if (identifier == error_mark_node)
21992 return error_mark_node;
21994 /* Look up the identifier in the currently active scope. Look only
21995 for namespaces, due to:
21997 [basic.lookup.udir]
21999 When looking up a namespace-name in a using-directive or alias
22000 definition, only namespace names are considered.
22002 And:
22004 [basic.lookup.qual]
22006 During the lookup of a name preceding the :: scope resolution
22007 operator, object, function, and enumerator names are ignored.
22009 (Note that cp_parser_qualifying_entity only calls this
22010 function if the token after the name is the scope resolution
22011 operator.) */
22012 namespace_decl = cp_parser_lookup_name (parser, identifier,
22013 none_type,
22014 /*is_template=*/false,
22015 /*is_namespace=*/true,
22016 /*check_dependency=*/true,
22017 /*ambiguous_decls=*/NULL,
22018 token->location);
22019 /* If it's not a namespace, issue an error. */
22020 if (namespace_decl == error_mark_node
22021 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
22023 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
22025 auto_diagnostic_group d;
22026 name_hint hint;
22027 if (namespace_decl == error_mark_node
22028 && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
22029 hint = suggest_alternative_in_explicit_scope (token->location,
22030 identifier,
22031 parser->scope);
22032 if (const char *suggestion = hint.suggestion ())
22034 gcc_rich_location richloc (token->location);
22035 richloc.add_fixit_replace (suggestion);
22036 error_at (&richloc,
22037 "%qD is not a namespace-name; did you mean %qs?",
22038 identifier, suggestion);
22040 else
22041 error_at (token->location, "%qD is not a namespace-name",
22042 identifier);
22044 else
22045 cp_parser_error (parser, "expected namespace-name");
22046 namespace_decl = error_mark_node;
22049 return namespace_decl;
22052 /* Parse a namespace-definition.
22054 namespace-definition:
22055 named-namespace-definition
22056 unnamed-namespace-definition
22058 named-namespace-definition:
22059 original-namespace-definition
22060 extension-namespace-definition
22062 original-namespace-definition:
22063 namespace identifier { namespace-body }
22065 extension-namespace-definition:
22066 namespace original-namespace-name { namespace-body }
22068 unnamed-namespace-definition:
22069 namespace { namespace-body } */
22071 static void
22072 cp_parser_namespace_definition (cp_parser* parser)
22074 tree identifier;
22075 int nested_definition_count = 0;
22077 cp_ensure_no_omp_declare_simd (parser);
22078 cp_ensure_no_oacc_routine (parser);
22080 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
22081 const bool topmost_inline_p = is_inline;
22083 if (is_inline)
22085 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
22086 cp_lexer_consume_token (parser->lexer);
22089 /* Look for the `namespace' keyword. */
22090 cp_token* token
22091 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
22093 /* Parse any specified attributes before the identifier. */
22094 tree attribs = cp_parser_attributes_opt (parser);
22096 for (;;)
22098 identifier = NULL_TREE;
22100 bool nested_inline_p = cp_lexer_next_token_is_keyword (parser->lexer,
22101 RID_INLINE);
22102 if (nested_inline_p && nested_definition_count != 0)
22104 if (pedantic && cxx_dialect < cxx20)
22105 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
22106 OPT_Wc__20_extensions, "nested inline namespace "
22107 "definitions only available with %<-std=c++20%> or "
22108 "%<-std=gnu++20%>");
22109 cp_lexer_consume_token (parser->lexer);
22112 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22114 identifier = cp_parser_identifier (parser);
22116 if (cp_next_tokens_can_be_std_attribute_p (parser))
22117 pedwarn (input_location, OPT_Wpedantic,
22118 "standard attributes on namespaces must precede "
22119 "the namespace name");
22121 /* Parse any attributes specified after the identifier. */
22122 attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
22125 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
22127 /* Don't forget that the innermost namespace might have been
22128 marked as inline. Use |= because we cannot overwrite
22129 IS_INLINE in case the outermost namespace is inline, but
22130 there are no nested inlines. */
22131 is_inline |= nested_inline_p;
22132 break;
22135 if (!nested_definition_count && pedantic && cxx_dialect < cxx17)
22136 pedwarn (input_location, OPT_Wc__17_extensions,
22137 "nested namespace definitions only available with "
22138 "%<-std=c++17%> or %<-std=gnu++17%>");
22140 /* Nested namespace names can create new namespaces (unlike
22141 other qualified-ids). */
22142 if (int count = (identifier
22143 ? push_namespace (identifier, nested_inline_p)
22144 : 0))
22145 nested_definition_count += count;
22146 else
22147 cp_parser_error (parser, "nested namespace name required");
22148 cp_lexer_consume_token (parser->lexer);
22151 if (nested_definition_count && !identifier)
22152 cp_parser_error (parser, "namespace name required");
22154 if (nested_definition_count && attribs)
22155 error_at (token->location,
22156 "a nested namespace definition cannot have attributes");
22157 if (nested_definition_count && topmost_inline_p)
22158 error_at (token->location,
22159 "a nested namespace definition cannot be inline");
22161 /* Start the namespace. */
22162 nested_definition_count += push_namespace (identifier, is_inline);
22164 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
22166 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
22168 /* Look for the `{' to validate starting the namespace. */
22169 matching_braces braces;
22170 if (braces.require_open (parser))
22172 /* Parse the body of the namespace. */
22173 cp_parser_namespace_body (parser);
22175 /* Look for the final `}'. */
22176 braces.require_close (parser);
22179 if (has_visibility)
22180 pop_visibility (1);
22182 /* Pop the nested namespace definitions. */
22183 while (nested_definition_count--)
22184 pop_namespace ();
22187 /* Parse a namespace-body.
22189 namespace-body:
22190 declaration-seq [opt] */
22192 static void
22193 cp_parser_namespace_body (cp_parser* parser)
22195 cp_parser_declaration_seq_opt (parser);
22198 /* Parse a namespace-alias-definition.
22200 namespace-alias-definition:
22201 namespace identifier = qualified-namespace-specifier ; */
22203 static void
22204 cp_parser_namespace_alias_definition (cp_parser* parser)
22206 tree identifier;
22207 tree namespace_specifier;
22209 cp_token *token = cp_lexer_peek_token (parser->lexer);
22211 /* Look for the `namespace' keyword. */
22212 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
22213 /* Look for the identifier. */
22214 identifier = cp_parser_identifier (parser);
22215 if (identifier == error_mark_node)
22216 return;
22217 /* Look for the `=' token. */
22218 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
22219 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22221 error_at (token->location, "%<namespace%> definition is not allowed here");
22222 /* Skip the definition. */
22223 cp_lexer_consume_token (parser->lexer);
22224 if (cp_parser_skip_to_closing_brace (parser))
22225 cp_lexer_consume_token (parser->lexer);
22226 return;
22228 cp_parser_require (parser, CPP_EQ, RT_EQ);
22229 /* Look for the qualified-namespace-specifier. */
22230 namespace_specifier
22231 = cp_parser_qualified_namespace_specifier (parser);
22232 cp_warn_deprecated_use_scopes (namespace_specifier);
22233 /* Look for the `;' token. */
22234 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22236 /* Register the alias in the symbol table. */
22237 do_namespace_alias (identifier, namespace_specifier);
22240 /* Parse a qualified-namespace-specifier.
22242 qualified-namespace-specifier:
22243 :: [opt] nested-name-specifier [opt] namespace-name
22245 Returns a NAMESPACE_DECL corresponding to the specified
22246 namespace. */
22248 static tree
22249 cp_parser_qualified_namespace_specifier (cp_parser* parser)
22251 /* Look for the optional `::'. */
22252 cp_parser_global_scope_opt (parser,
22253 /*current_scope_valid_p=*/false);
22255 /* Look for the optional nested-name-specifier. */
22256 cp_parser_nested_name_specifier_opt (parser,
22257 /*typename_keyword_p=*/false,
22258 /*check_dependency_p=*/true,
22259 /*type_p=*/false,
22260 /*is_declaration=*/true);
22262 return cp_parser_namespace_name (parser);
22265 /* Subroutine of cp_parser_using_declaration. */
22267 static tree
22268 finish_using_decl (tree qscope, tree identifier, bool typename_p = false)
22270 tree decl = NULL_TREE;
22271 if (at_class_scope_p ())
22273 /* Create the USING_DECL. */
22274 decl = do_class_using_decl (qscope, identifier);
22276 if (check_for_bare_parameter_packs (decl))
22277 return error_mark_node;
22279 if (decl && typename_p)
22280 USING_DECL_TYPENAME_P (decl) = 1;
22282 /* Add it to the list of members in this class. */
22283 finish_member_declaration (decl);
22285 else
22286 finish_nonmember_using_decl (qscope, identifier);
22287 return decl;
22290 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
22291 access declaration.
22293 using-declaration:
22294 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
22295 using :: unqualified-id ;
22297 access-declaration:
22298 qualified-id ;
22302 static bool
22303 cp_parser_using_declaration (cp_parser* parser,
22304 bool access_declaration_p)
22306 cp_token *token;
22307 bool typename_p = false;
22308 bool global_scope_p;
22309 tree identifier;
22310 tree qscope;
22311 int oldcount = errorcount;
22312 cp_token *diag_token = NULL;
22314 if (access_declaration_p)
22316 diag_token = cp_lexer_peek_token (parser->lexer);
22317 cp_parser_parse_tentatively (parser);
22319 else
22321 /* Look for the `using' keyword. */
22322 cp_parser_require_keyword (parser, RID_USING, RT_USING);
22324 again:
22325 /* Peek at the next token. */
22326 token = cp_lexer_peek_token (parser->lexer);
22327 /* See if it's `typename'. */
22328 if (token->keyword == RID_TYPENAME)
22330 /* Remember that we've seen it. */
22331 typename_p = true;
22332 /* Consume the `typename' token. */
22333 cp_lexer_consume_token (parser->lexer);
22337 /* Look for the optional global scope qualification. */
22338 global_scope_p
22339 = (cp_parser_global_scope_opt (parser,
22340 /*current_scope_valid_p=*/false)
22341 != NULL_TREE);
22343 /* If we saw `typename', or didn't see `::', then there must be a
22344 nested-name-specifier present. */
22345 if (typename_p || !global_scope_p)
22347 qscope = cp_parser_nested_name_specifier (parser, typename_p,
22348 /*check_dependency_p=*/true,
22349 /*type_p=*/false,
22350 /*is_declaration=*/true);
22351 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
22353 cp_parser_skip_to_end_of_block_or_statement (parser);
22354 return false;
22357 /* Otherwise, we could be in either of the two productions. In that
22358 case, treat the nested-name-specifier as optional. */
22359 else
22360 qscope = cp_parser_nested_name_specifier_opt (parser,
22361 /*typename_keyword_p=*/false,
22362 /*check_dependency_p=*/true,
22363 /*type_p=*/false,
22364 /*is_declaration=*/true);
22365 if (!qscope)
22366 qscope = global_namespace;
22368 cp_warn_deprecated_use_scopes (qscope);
22370 if (access_declaration_p
22371 && !MAYBE_CLASS_TYPE_P (qscope)
22372 && TREE_CODE (qscope) != ENUMERAL_TYPE)
22373 /* If the qualifying scope of an access-declaration isn't a class
22374 or enumeration type then it can't be valid. */
22375 cp_parser_simulate_error (parser);
22377 if (access_declaration_p && cp_parser_error_occurred (parser))
22378 /* Something has already gone wrong; there's no need to parse
22379 further. Since an error has occurred, the return value of
22380 cp_parser_parse_definitely will be false, as required. */
22381 return cp_parser_parse_definitely (parser);
22383 token = cp_lexer_peek_token (parser->lexer);
22384 /* Parse the unqualified-id. */
22385 identifier = cp_parser_unqualified_id (parser,
22386 /*template_keyword_p=*/false,
22387 /*check_dependency_p=*/true,
22388 /*declarator_p=*/true,
22389 /*optional_p=*/false);
22391 if (access_declaration_p)
22393 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22394 cp_parser_simulate_error (parser);
22395 if (!cp_parser_parse_definitely (parser))
22396 return false;
22398 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22400 cp_token *ell = cp_lexer_consume_token (parser->lexer);
22401 if (cxx_dialect < cxx17)
22402 pedwarn (ell->location, OPT_Wc__17_extensions,
22403 "pack expansion in using-declaration only available "
22404 "with %<-std=c++17%> or %<-std=gnu++17%>");
22406 /* A parameter pack can appear in the qualifying scope, and/or in the
22407 terminal name (if naming a conversion function). Logically they're
22408 part of a single pack expansion of the overall USING_DECL, but we
22409 express them as separate pack expansions within the USING_DECL since
22410 we can't create a pack expansion over a USING_DECL. */
22411 bool saw_parm_pack = false;
22412 if (uses_parameter_packs (qscope))
22414 qscope = make_pack_expansion (qscope);
22415 saw_parm_pack = true;
22417 if (identifier_p (identifier)
22418 && IDENTIFIER_CONV_OP_P (identifier)
22419 && uses_parameter_packs (TREE_TYPE (identifier)))
22421 identifier = make_conv_op_name (make_pack_expansion
22422 (TREE_TYPE (identifier)));
22423 saw_parm_pack = true;
22425 if (!saw_parm_pack)
22427 /* Issue an error in terms using a SCOPE_REF that includes both
22428 components. */
22429 tree name
22430 = build_qualified_name (NULL_TREE, qscope, identifier, false);
22431 make_pack_expansion (name);
22432 gcc_assert (seen_error ());
22433 qscope = identifier = error_mark_node;
22437 /* The function we call to handle a using-declaration is different
22438 depending on what scope we are in. */
22439 if (qscope == error_mark_node || identifier == error_mark_node)
22441 else if (!identifier_p (identifier)
22442 && TREE_CODE (identifier) != BIT_NOT_EXPR)
22443 /* [namespace.udecl]
22445 A using declaration shall not name a template-id. */
22446 error_at (token->location,
22447 "a template-id may not appear in a using-declaration");
22448 else
22450 tree decl = finish_using_decl (qscope, identifier, typename_p);
22452 if (decl == error_mark_node)
22454 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22455 return false;
22459 if (!access_declaration_p
22460 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22462 cp_token *comma = cp_lexer_consume_token (parser->lexer);
22463 if (cxx_dialect < cxx17)
22464 pedwarn (comma->location, OPT_Wc__17_extensions,
22465 "comma-separated list in using-declaration only available "
22466 "with %<-std=c++17%> or %<-std=gnu++17%>");
22467 goto again;
22470 /* Look for the final `;'. */
22471 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22473 if (access_declaration_p && errorcount == oldcount)
22474 warning_at (diag_token->location, OPT_Wdeprecated,
22475 "access declarations are deprecated "
22476 "in favor of using-declarations; "
22477 "suggestion: add the %<using%> keyword");
22479 return true;
22482 /* C++20 using enum declaration.
22484 using-enum-declaration :
22485 using elaborated-enum-specifier ; */
22487 static void
22488 cp_parser_using_enum (cp_parser *parser)
22490 cp_parser_require_keyword (parser, RID_USING, RT_USING);
22492 /* Using cp_parser_elaborated_type_specifier rejects typedef-names, which
22493 breaks one of the motivating examples in using-enum-5.C.
22494 cp_parser_simple_type_specifier seems to be closer to what we actually
22495 want, though that hasn't been properly specified yet. */
22497 /* Consume 'enum'. */
22498 gcc_checking_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM));
22499 cp_lexer_consume_token (parser->lexer);
22501 cp_token *start = cp_lexer_peek_token (parser->lexer);
22503 tree type = (cp_parser_simple_type_specifier
22504 (parser, NULL, CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
22506 cp_token *end = cp_lexer_previous_token (parser->lexer);
22508 if (type == error_mark_node
22509 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
22511 cp_parser_skip_to_end_of_block_or_statement (parser);
22512 return;
22514 if (TREE_CODE (type) == TYPE_DECL)
22515 type = TREE_TYPE (type);
22517 /* The elaborated-enum-specifier shall not name a dependent type and the type
22518 shall have a reachable enum-specifier. */
22519 const char *msg = nullptr;
22520 if (cxx_dialect < cxx20)
22521 msg = G_("%<using enum%> "
22522 "only available with %<-std=c++20%> or %<-std=gnu++20%>");
22523 else if (dependent_type_p (type))
22524 msg = G_("%<using enum%> of dependent type %qT");
22525 else if (TREE_CODE (type) != ENUMERAL_TYPE)
22526 msg = G_("%<using enum%> of non-enumeration type %q#T");
22527 else if (!COMPLETE_TYPE_P (type))
22528 msg = G_("%<using enum%> of incomplete type %qT");
22529 else if (OPAQUE_ENUM_P (type))
22530 msg = G_("%<using enum%> of %qT before its enum-specifier");
22531 if (msg)
22533 location_t loc = make_location (start, start, end);
22534 auto_diagnostic_group g;
22535 error_at (loc, msg, type);
22536 loc = location_of (type);
22537 if (cxx_dialect < cxx20 || loc == input_location)
22539 else if (OPAQUE_ENUM_P (type))
22540 inform (loc, "opaque-enum-declaration here");
22541 else
22542 inform (loc, "declared here");
22545 /* A using-enum-declaration introduces the enumerator names of the named
22546 enumeration as if by a using-declaration for each enumerator. */
22547 if (TREE_CODE (type) == ENUMERAL_TYPE)
22548 for (tree v = TYPE_VALUES (type); v; v = TREE_CHAIN (v))
22549 finish_using_decl (type, DECL_NAME (TREE_VALUE (v)));
22552 /* Parse an alias-declaration.
22554 alias-declaration:
22555 using identifier attribute-specifier-seq [opt] = type-id */
22557 static tree
22558 cp_parser_alias_declaration (cp_parser* parser)
22560 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
22561 location_t id_location, type_location;
22562 cp_declarator *declarator;
22563 cp_decl_specifier_seq decl_specs;
22564 bool member_p;
22565 const char *saved_message = NULL;
22567 /* Look for the `using' keyword. */
22568 cp_token *using_token
22569 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
22570 if (using_token == NULL)
22571 return error_mark_node;
22573 id_location = cp_lexer_peek_token (parser->lexer)->location;
22574 id = cp_parser_identifier (parser);
22575 if (id == error_mark_node)
22576 return error_mark_node;
22578 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
22579 attributes = cp_parser_attributes_opt (parser);
22580 if (attributes == error_mark_node)
22581 return error_mark_node;
22583 cp_parser_require (parser, CPP_EQ, RT_EQ);
22585 if (cp_parser_error_occurred (parser))
22586 return error_mark_node;
22588 cp_parser_commit_to_tentative_parse (parser);
22590 /* Now we are going to parse the type-id of the declaration. */
22593 [dcl.type]/3 says:
22595 "A type-specifier-seq shall not define a class or enumeration
22596 unless it appears in the type-id of an alias-declaration (7.1.3) that
22597 is not the declaration of a template-declaration."
22599 In other words, if we currently are in an alias template, the
22600 type-id should not define a type.
22602 So let's set parser->type_definition_forbidden_message in that
22603 case; cp_parser_check_type_definition (called by
22604 cp_parser_class_specifier) will then emit an error if a type is
22605 defined in the type-id. */
22606 if (parser->num_template_parameter_lists)
22608 saved_message = parser->type_definition_forbidden_message;
22609 parser->type_definition_forbidden_message =
22610 G_("types may not be defined in alias template declarations");
22613 type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
22614 &type_location);
22616 /* Restore the error message if need be. */
22617 if (parser->num_template_parameter_lists)
22618 parser->type_definition_forbidden_message = saved_message;
22620 if (type == error_mark_node
22621 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
22623 cp_parser_skip_to_end_of_block_or_statement (parser);
22624 return error_mark_node;
22627 /* A typedef-name can also be introduced by an alias-declaration. The
22628 identifier following the using keyword becomes a typedef-name. It has
22629 the same semantics as if it were introduced by the typedef
22630 specifier. In particular, it does not define a new type and it shall
22631 not appear in the type-id. */
22633 clear_decl_specs (&decl_specs);
22634 decl_specs.type = type;
22635 if (attributes != NULL_TREE)
22637 decl_specs.attributes = attributes;
22638 set_and_check_decl_spec_loc (&decl_specs,
22639 ds_attribute,
22640 attrs_token);
22642 set_and_check_decl_spec_loc (&decl_specs,
22643 ds_typedef,
22644 using_token);
22645 set_and_check_decl_spec_loc (&decl_specs,
22646 ds_alias,
22647 using_token);
22648 decl_specs.locations[ds_type_spec] = type_location;
22650 if (parser->num_template_parameter_lists
22651 && !cp_parser_check_template_parameters (parser,
22652 /*num_templates=*/0,
22653 /*template_id*/false,
22654 id_location,
22655 /*declarator=*/NULL))
22656 return error_mark_node;
22658 declarator = make_id_declarator (NULL_TREE, id, sfk_none, id_location);
22660 member_p = at_class_scope_p ();
22661 if (member_p)
22662 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
22663 NULL_TREE, attributes);
22664 else
22665 decl = start_decl (declarator, &decl_specs, 0,
22666 attributes, NULL_TREE, &pushed_scope);
22667 if (decl == error_mark_node)
22668 return decl;
22670 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
22672 if (pushed_scope)
22673 pop_scope (pushed_scope);
22675 /* If decl is a template, return its TEMPLATE_DECL so that it gets
22676 added into the symbol table; otherwise, return the TYPE_DECL. */
22677 if (DECL_LANG_SPECIFIC (decl)
22678 && DECL_TEMPLATE_INFO (decl)
22679 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
22681 decl = DECL_TI_TEMPLATE (decl);
22682 if (member_p)
22683 check_member_template (decl);
22686 return decl;
22689 /* Parse a using-directive.
22691 using-directive:
22692 attribute-specifier-seq [opt] using namespace :: [opt]
22693 nested-name-specifier [opt] namespace-name ; */
22695 static void
22696 cp_parser_using_directive (cp_parser* parser)
22698 tree namespace_decl;
22699 tree attribs = cp_parser_std_attribute_spec_seq (parser);
22700 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22702 /* Error during attribute parsing that resulted in skipping
22703 to next semicolon. */
22704 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22705 return;
22708 /* Look for the `using' keyword. */
22709 cp_parser_require_keyword (parser, RID_USING, RT_USING);
22710 /* And the `namespace' keyword. */
22711 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
22712 /* Look for the optional `::' operator. */
22713 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
22714 /* And the optional nested-name-specifier. */
22715 cp_parser_nested_name_specifier_opt (parser,
22716 /*typename_keyword_p=*/false,
22717 /*check_dependency_p=*/true,
22718 /*type_p=*/false,
22719 /*is_declaration=*/true);
22720 /* Get the namespace being used. */
22721 namespace_decl = cp_parser_namespace_name (parser);
22722 cp_warn_deprecated_use_scopes (namespace_decl);
22723 /* And any specified GNU attributes. */
22724 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
22725 attribs = chainon (attribs, cp_parser_gnu_attributes_opt (parser));
22727 /* Update the symbol table. */
22728 finish_using_directive (namespace_decl, attribs);
22730 /* Look for the final `;'. */
22731 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22734 /* Parse an asm-definition.
22736 asm-qualifier:
22737 volatile
22738 inline
22739 goto
22741 asm-qualifier-list:
22742 asm-qualifier
22743 asm-qualifier-list asm-qualifier
22745 asm-definition:
22746 asm ( string-literal ) ;
22748 GNU Extension:
22750 asm-definition:
22751 asm asm-qualifier-list [opt] ( string-literal ) ;
22752 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt] ) ;
22753 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
22754 : asm-operand-list [opt] ) ;
22755 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
22756 : asm-operand-list [opt]
22757 : asm-clobber-list [opt] ) ;
22758 asm asm-qualifier-list [opt] ( string-literal : : asm-operand-list [opt]
22759 : asm-clobber-list [opt]
22760 : asm-goto-list ) ;
22762 The form with asm-goto-list is valid if and only if the asm-qualifier-list
22763 contains goto, and is the only allowed form in that case. No duplicates are
22764 allowed in an asm-qualifier-list. */
22766 static void
22767 cp_parser_asm_definition (cp_parser* parser)
22769 tree outputs = NULL_TREE;
22770 tree inputs = NULL_TREE;
22771 tree clobbers = NULL_TREE;
22772 tree labels = NULL_TREE;
22773 tree asm_stmt;
22774 bool extended_p = false;
22775 bool invalid_inputs_p = false;
22776 bool invalid_outputs_p = false;
22777 required_token missing = RT_NONE;
22778 tree std_attrs = cp_parser_std_attribute_spec_seq (parser);
22779 location_t asm_loc = cp_lexer_peek_token (parser->lexer)->location;
22781 /* Look for the `asm' keyword. */
22782 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
22784 /* In C++20, unevaluated inline assembly is permitted in constexpr
22785 functions. */
22786 if (parser->in_function_body
22787 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
22788 && cxx_dialect < cxx20)
22789 pedwarn (asm_loc, OPT_Wc__20_extensions, "%<asm%> in %<constexpr%> "
22790 "function only available with %<-std=c++20%> or "
22791 "%<-std=gnu++20%>");
22793 /* Handle the asm-qualifier-list. */
22794 location_t volatile_loc = UNKNOWN_LOCATION;
22795 location_t inline_loc = UNKNOWN_LOCATION;
22796 location_t goto_loc = UNKNOWN_LOCATION;
22797 location_t first_loc = UNKNOWN_LOCATION;
22799 if (cp_parser_allow_gnu_extensions_p (parser))
22800 for (;;)
22802 cp_token *token = cp_lexer_peek_token (parser->lexer);
22803 location_t loc = token->location;
22804 switch (cp_lexer_peek_token (parser->lexer)->keyword)
22806 case RID_VOLATILE:
22807 if (volatile_loc)
22809 error_at (loc, "duplicate %<asm%> qualifier %qT",
22810 token->u.value);
22811 inform (volatile_loc, "first seen here");
22813 else
22815 if (!parser->in_function_body)
22816 warning_at (loc, 0, "%<asm%> qualifier %qT ignored "
22817 "outside of function body", token->u.value);
22818 volatile_loc = loc;
22820 cp_lexer_consume_token (parser->lexer);
22821 continue;
22823 case RID_INLINE:
22824 if (inline_loc)
22826 error_at (loc, "duplicate %<asm%> qualifier %qT",
22827 token->u.value);
22828 inform (inline_loc, "first seen here");
22830 else
22831 inline_loc = loc;
22832 if (!first_loc)
22833 first_loc = loc;
22834 cp_lexer_consume_token (parser->lexer);
22835 continue;
22837 case RID_GOTO:
22838 if (goto_loc)
22840 error_at (loc, "duplicate %<asm%> qualifier %qT",
22841 token->u.value);
22842 inform (goto_loc, "first seen here");
22844 else
22845 goto_loc = loc;
22846 if (!first_loc)
22847 first_loc = loc;
22848 cp_lexer_consume_token (parser->lexer);
22849 continue;
22851 case RID_CONST:
22852 case RID_RESTRICT:
22853 error_at (loc, "%qT is not an %<asm%> qualifier", token->u.value);
22854 cp_lexer_consume_token (parser->lexer);
22855 continue;
22857 default:
22858 break;
22860 break;
22863 bool volatile_p = (volatile_loc != UNKNOWN_LOCATION);
22864 bool inline_p = (inline_loc != UNKNOWN_LOCATION);
22865 bool goto_p = (goto_loc != UNKNOWN_LOCATION);
22867 if (!parser->in_function_body && (inline_p || goto_p))
22869 error_at (first_loc, "%<asm%> qualifier outside of function body");
22870 inline_p = goto_p = false;
22873 /* Look for the opening `('. */
22874 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22875 return;
22876 /* Look for the string. */
22877 tree string = cp_parser_string_literal (parser, /*translate=*/false,
22878 /*wide_ok=*/false);
22879 if (string == error_mark_node)
22881 cp_parser_skip_to_closing_parenthesis (parser, true, false,
22882 /*consume_paren=*/true);
22883 return;
22886 /* If we're allowing GNU extensions, check for the extended assembly
22887 syntax. Unfortunately, the `:' tokens need not be separated by
22888 a space in C, and so, for compatibility, we tolerate that here
22889 too. Doing that means that we have to treat the `::' operator as
22890 two `:' tokens. */
22891 if (cp_parser_allow_gnu_extensions_p (parser)
22892 && parser->in_function_body
22893 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
22894 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
22896 bool inputs_p = false;
22897 bool clobbers_p = false;
22898 bool labels_p = false;
22900 /* The extended syntax was used. */
22901 extended_p = true;
22903 /* Look for outputs. */
22904 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22906 /* Consume the `:'. */
22907 cp_lexer_consume_token (parser->lexer);
22908 /* Parse the output-operands. */
22909 if (cp_lexer_next_token_is_not (parser->lexer,
22910 CPP_COLON)
22911 && cp_lexer_next_token_is_not (parser->lexer,
22912 CPP_SCOPE)
22913 && cp_lexer_next_token_is_not (parser->lexer,
22914 CPP_CLOSE_PAREN))
22916 outputs = cp_parser_asm_operand_list (parser);
22917 if (outputs == error_mark_node)
22918 invalid_outputs_p = true;
22921 /* If the next token is `::', there are no outputs, and the
22922 next token is the beginning of the inputs. */
22923 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22924 /* The inputs are coming next. */
22925 inputs_p = true;
22927 /* Look for inputs. */
22928 if (inputs_p
22929 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22931 /* Consume the `:' or `::'. */
22932 cp_lexer_consume_token (parser->lexer);
22933 /* Parse the output-operands. */
22934 if (cp_lexer_next_token_is_not (parser->lexer,
22935 CPP_COLON)
22936 && cp_lexer_next_token_is_not (parser->lexer,
22937 CPP_SCOPE)
22938 && cp_lexer_next_token_is_not (parser->lexer,
22939 CPP_CLOSE_PAREN))
22941 inputs = cp_parser_asm_operand_list (parser);
22942 if (inputs == error_mark_node)
22943 invalid_inputs_p = true;
22946 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22947 /* The clobbers are coming next. */
22948 clobbers_p = true;
22950 /* Look for clobbers. */
22951 if (clobbers_p
22952 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22954 clobbers_p = true;
22955 /* Consume the `:' or `::'. */
22956 cp_lexer_consume_token (parser->lexer);
22957 /* Parse the clobbers. */
22958 if (cp_lexer_next_token_is_not (parser->lexer,
22959 CPP_COLON)
22960 && cp_lexer_next_token_is_not (parser->lexer,
22961 CPP_CLOSE_PAREN))
22962 clobbers = cp_parser_asm_clobber_list (parser);
22964 else if (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22965 /* The labels are coming next. */
22966 labels_p = true;
22968 /* Look for labels. */
22969 if (labels_p
22970 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
22972 labels_p = true;
22973 /* Consume the `:' or `::'. */
22974 cp_lexer_consume_token (parser->lexer);
22975 /* Parse the labels. */
22976 labels = cp_parser_asm_label_list (parser);
22979 if (goto_p && !labels_p)
22980 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
22982 else if (goto_p)
22983 missing = RT_COLON_SCOPE;
22985 /* Look for the closing `)'. */
22986 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
22987 missing ? missing : RT_CLOSE_PAREN))
22988 cp_parser_skip_to_closing_parenthesis (parser, true, false,
22989 /*consume_paren=*/true);
22990 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22992 if (!invalid_inputs_p && !invalid_outputs_p)
22994 /* Create the ASM_EXPR. */
22995 if (parser->in_function_body)
22997 asm_stmt = finish_asm_stmt (asm_loc, volatile_p, string, outputs,
22998 inputs, clobbers, labels, inline_p);
22999 /* If the extended syntax was not used, mark the ASM_EXPR. */
23000 if (!extended_p)
23002 tree temp = asm_stmt;
23003 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
23004 temp = TREE_OPERAND (temp, 0);
23006 ASM_INPUT_P (temp) = 1;
23009 else
23010 symtab->finalize_toplevel_asm (string);
23013 if (std_attrs && any_nonignored_attribute_p (std_attrs))
23014 warning_at (asm_loc, OPT_Wattributes,
23015 "attributes ignored on %<asm%> declaration");
23018 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
23019 type that comes from the decl-specifier-seq. */
23021 static tree
23022 strip_declarator_types (tree type, cp_declarator *declarator)
23024 for (cp_declarator *d = declarator; d;)
23025 switch (d->kind)
23027 case cdk_id:
23028 case cdk_decomp:
23029 case cdk_error:
23030 d = NULL;
23031 break;
23033 default:
23034 if (TYPE_PTRMEMFUNC_P (type))
23035 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
23036 type = TREE_TYPE (type);
23037 d = d->declarator;
23038 break;
23041 return type;
23044 /* Warn about the most vexing parse syntactic ambiguity, i.e., warn when
23045 a construct looks like a variable definition but is actually a function
23046 declaration. DECL_SPECIFIERS is the decl-specifier-seq and DECLARATOR
23047 is the declarator for this function declaration. */
23049 static void
23050 warn_about_ambiguous_parse (const cp_decl_specifier_seq *decl_specifiers,
23051 const cp_declarator *declarator)
23053 /* Only warn if we are declaring a function at block scope. */
23054 if (!at_function_scope_p ())
23055 return;
23057 /* And only if there is no storage class specified. */
23058 if (decl_specifiers->storage_class != sc_none
23059 || decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
23060 return;
23062 if (declarator->kind != cdk_function
23063 || !declarator->declarator
23064 || declarator->declarator->kind != cdk_id
23065 || !identifier_p (get_unqualified_id
23066 (const_cast<cp_declarator *>(declarator))))
23067 return;
23069 /* Don't warn when the whole declarator (not just the declarator-id!)
23070 was parenthesized. That is, don't warn for int(n()) but do warn
23071 for int(f)(). */
23072 if (declarator->parenthesized != UNKNOWN_LOCATION)
23073 return;
23075 tree type;
23076 if (decl_specifiers->type)
23078 type = decl_specifiers->type;
23079 if (TREE_CODE (type) == TYPE_DECL)
23080 type = TREE_TYPE (type);
23082 /* If the return type is void there is no ambiguity. */
23083 if (same_type_p (type, void_type_node))
23084 return;
23086 else if (decl_specifiers->any_type_specifiers_p)
23087 /* Code like long f(); will have null ->type. If we have any
23088 type-specifiers, pretend we've seen int. */
23089 type = integer_type_node;
23090 else
23091 return;
23093 auto_diagnostic_group d;
23094 location_t loc = declarator->u.function.parens_loc;
23095 tree params = declarator->u.function.parameters;
23096 const bool has_list_ctor_p = CLASS_TYPE_P (type) && TYPE_HAS_LIST_CTOR (type);
23098 /* The T t() case. */
23099 if (params == void_list_node)
23101 if (warning_at (loc, OPT_Wvexing_parse,
23102 "empty parentheses were disambiguated as a function "
23103 "declaration"))
23105 /* () means value-initialization (C++03 and up); {} (C++11 and up)
23106 means value-initialization or aggregate-initialization, nothing
23107 means default-initialization. We can only suggest removing the
23108 parentheses/adding {} if T has a default constructor. */
23109 if (!CLASS_TYPE_P (type) || TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
23111 gcc_rich_location iloc (loc);
23112 iloc.add_fixit_remove ();
23113 inform (&iloc, "remove parentheses to default-initialize "
23114 "a variable");
23115 if (cxx_dialect >= cxx11 && !has_list_ctor_p)
23117 if (CP_AGGREGATE_TYPE_P (type))
23118 inform (loc, "or replace parentheses with braces to "
23119 "aggregate-initialize a variable");
23120 else
23121 inform (loc, "or replace parentheses with braces to "
23122 "value-initialize a variable");
23126 return;
23129 /* If we had (...) or the parameter-list wasn't parenthesized,
23130 we're done. */
23131 if (params == NULL_TREE || !PARENTHESIZED_LIST_P (params))
23132 return;
23134 /* The T t(X()) case. */
23135 if (list_length (params) == 2)
23137 if (warning_at (loc, OPT_Wvexing_parse,
23138 "parentheses were disambiguated as a function "
23139 "declaration"))
23141 gcc_rich_location iloc (loc);
23142 /* {}-initialization means that we can use an initializer-list
23143 constructor if no default constructor is available, so don't
23144 suggest using {} for classes that have an initializer_list
23145 constructor. */
23146 if (cxx_dialect >= cxx11 && !has_list_ctor_p)
23148 iloc.add_fixit_replace (get_start (loc), "{");
23149 iloc.add_fixit_replace (get_finish (loc), "}");
23150 inform (&iloc, "replace parentheses with braces to declare a "
23151 "variable");
23153 else
23155 iloc.add_fixit_insert_after (get_start (loc), "(");
23156 iloc.add_fixit_insert_before (get_finish (loc), ")");
23157 inform (&iloc, "add parentheses to declare a variable");
23161 /* The T t(X(), X()) case. */
23162 else if (warning_at (loc, OPT_Wvexing_parse,
23163 "parentheses were disambiguated as a function "
23164 "declaration"))
23166 gcc_rich_location iloc (loc);
23167 if (cxx_dialect >= cxx11 && !has_list_ctor_p)
23169 iloc.add_fixit_replace (get_start (loc), "{");
23170 iloc.add_fixit_replace (get_finish (loc), "}");
23171 inform (&iloc, "replace parentheses with braces to declare a "
23172 "variable");
23177 /* If DECLARATOR with DECL_SPECS is a function declarator that has
23178 the form of a deduction guide, tag it as such. CTOR_DTOR_OR_CONV_P
23179 has the same meaning as in cp_parser_declarator. */
23181 static void
23182 cp_parser_maybe_adjust_declarator_for_dguide (cp_parser *parser,
23183 cp_decl_specifier_seq *decl_specs,
23184 cp_declarator *declarator,
23185 int *ctor_dtor_or_conv_p)
23187 if (cxx_dialect >= cxx17
23188 && *ctor_dtor_or_conv_p <= 0
23189 && !decl_specs->type
23190 && !decl_specs->any_type_specifiers_p
23191 && function_declarator_p (declarator))
23193 cp_declarator *id = get_id_declarator (declarator);
23194 tree name = id->u.id.unqualified_name;
23195 parser->scope = id->u.id.qualifying_scope;
23196 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
23197 if (tmpl
23198 && (DECL_CLASS_TEMPLATE_P (tmpl)
23199 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
23201 id->u.id.unqualified_name = dguide_name (tmpl);
23202 id->u.id.sfk = sfk_deduction_guide;
23203 *ctor_dtor_or_conv_p = 1;
23208 /* Declarators [gram.dcl.decl] */
23210 /* Parse an init-declarator.
23212 init-declarator:
23213 declarator initializer [opt]
23215 GNU Extension:
23217 init-declarator:
23218 declarator asm-specification [opt] attributes [opt] initializer [opt]
23220 function-definition:
23221 decl-specifier-seq [opt] declarator ctor-initializer [opt]
23222 function-body
23223 decl-specifier-seq [opt] declarator function-try-block
23225 GNU Extension:
23227 function-definition:
23228 __extension__ function-definition
23230 TM Extension:
23232 function-definition:
23233 decl-specifier-seq [opt] declarator function-transaction-block
23235 The parser flags FLAGS is used to control type-specifier parsing.
23237 The DECL_SPECIFIERS apply to this declarator. Returns a
23238 representation of the entity declared. If MEMBER_P is TRUE, then
23239 this declarator appears in a class scope. The new DECL created by
23240 this declarator is returned.
23242 The CHECKS are access checks that should be performed once we know
23243 what entity is being declared (and, therefore, what classes have
23244 befriended it).
23246 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
23247 for a function-definition here as well. If the declarator is a
23248 declarator for a function-definition, *FUNCTION_DEFINITION_P will
23249 be TRUE upon return. By that point, the function-definition will
23250 have been completely parsed.
23252 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
23253 is FALSE.
23255 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
23256 parsed declaration if it is an uninitialized single declarator not followed
23257 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
23258 if present, will not be consumed. If returned, this declarator will be
23259 created with SD_INITIALIZED but will not call cp_finish_decl.
23261 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
23262 and there is an initializer, the pointed location_t is set to the
23263 location of the '=' or `(', or '{' in C++11 token introducing the
23264 initializer. */
23266 static tree
23267 cp_parser_init_declarator (cp_parser* parser,
23268 cp_parser_flags flags,
23269 cp_decl_specifier_seq *decl_specifiers,
23270 vec<deferred_access_check, va_gc> *checks,
23271 bool function_definition_allowed_p,
23272 bool member_p,
23273 int declares_class_or_enum,
23274 bool* function_definition_p,
23275 tree* maybe_range_for_decl,
23276 location_t* init_loc,
23277 tree* auto_result)
23279 cp_token *token = NULL, *asm_spec_start_token = NULL,
23280 *attributes_start_token = NULL;
23281 cp_declarator *declarator;
23282 tree prefix_attributes;
23283 tree attributes = NULL;
23284 tree asm_specification;
23285 tree initializer;
23286 tree decl = NULL_TREE;
23287 tree scope;
23288 int is_initialized;
23289 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
23290 initialized with "= ..", CPP_OPEN_PAREN if initialized with
23291 "(...)". */
23292 enum cpp_ttype initialization_kind;
23293 bool is_direct_init = false;
23294 bool is_non_constant_init;
23295 int ctor_dtor_or_conv_p;
23296 bool friend_p = cp_parser_friend_p (decl_specifiers);
23297 bool static_p = decl_specifiers->storage_class == sc_static;
23298 tree pushed_scope = NULL_TREE;
23299 bool range_for_decl_p = false;
23300 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
23301 location_t tmp_init_loc = UNKNOWN_LOCATION;
23303 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_consteval))
23304 flags |= CP_PARSER_FLAGS_CONSTEVAL;
23306 /* Assume that this is not the declarator for a function
23307 definition. */
23308 if (function_definition_p)
23309 *function_definition_p = false;
23311 /* Default arguments are only permitted for function parameters. */
23312 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
23313 parser->default_arg_ok_p = false;
23315 /* Defer access checks while parsing the declarator; we cannot know
23316 what names are accessible until we know what is being
23317 declared. */
23318 resume_deferring_access_checks ();
23320 token = cp_lexer_peek_token (parser->lexer);
23322 /* Parse the declarator. */
23323 declarator
23324 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23325 flags, &ctor_dtor_or_conv_p,
23326 /*parenthesized_p=*/NULL,
23327 member_p, friend_p, static_p);
23328 /* Gather up the deferred checks. */
23329 stop_deferring_access_checks ();
23331 parser->default_arg_ok_p = saved_default_arg_ok_p;
23333 /* If the DECLARATOR was erroneous, there's no need to go
23334 further. */
23335 if (declarator == cp_error_declarator)
23336 return error_mark_node;
23338 /* Check that the number of template-parameter-lists is OK. */
23339 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
23340 token->location))
23341 return error_mark_node;
23343 if (declares_class_or_enum & 2)
23344 cp_parser_check_for_definition_in_return_type (declarator,
23345 decl_specifiers->type,
23346 decl_specifiers->locations[ds_type_spec]);
23348 /* Figure out what scope the entity declared by the DECLARATOR is
23349 located in. `grokdeclarator' sometimes changes the scope, so
23350 we compute it now. */
23351 scope = get_scope_of_declarator (declarator);
23353 /* Perform any lookups in the declared type which were thought to be
23354 dependent, but are not in the scope of the declarator. */
23355 decl_specifiers->type
23356 = maybe_update_decl_type (decl_specifiers->type, scope);
23358 /* If we're allowing GNU extensions, look for an
23359 asm-specification. */
23360 if (cp_parser_allow_gnu_extensions_p (parser))
23362 /* Look for an asm-specification. */
23363 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
23364 asm_specification = cp_parser_asm_specification_opt (parser);
23366 else
23367 asm_specification = NULL_TREE;
23369 /* Gather the attributes that were provided with the
23370 decl-specifiers. */
23371 prefix_attributes = decl_specifiers->attributes;
23373 /* Look for attributes. */
23374 attributes_start_token = cp_lexer_peek_token (parser->lexer);
23375 attributes = cp_parser_attributes_opt (parser);
23377 /* Peek at the next token. */
23378 token = cp_lexer_peek_token (parser->lexer);
23380 bool bogus_implicit_tmpl = false;
23382 if (function_declarator_p (declarator))
23384 /* Handle C++17 deduction guides. Note that class-scope
23385 non-template deduction guides are instead handled in
23386 cp_parser_member_declaration. */
23387 cp_parser_maybe_adjust_declarator_for_dguide (parser,
23388 decl_specifiers,
23389 declarator,
23390 &ctor_dtor_or_conv_p);
23392 if (!member_p && !cp_parser_error_occurred (parser))
23393 warn_about_ambiguous_parse (decl_specifiers, declarator);
23395 /* Check to see if the token indicates the start of a
23396 function-definition. */
23397 if (cp_parser_token_starts_function_definition_p (token))
23399 if (!function_definition_allowed_p)
23401 /* If a function-definition should not appear here, issue an
23402 error message. */
23403 cp_parser_error (parser,
23404 "a function-definition is not allowed here");
23405 return error_mark_node;
23408 location_t func_brace_location
23409 = cp_lexer_peek_token (parser->lexer)->location;
23411 /* Neither attributes nor an asm-specification are allowed
23412 on a function-definition. */
23413 if (asm_specification)
23414 error_at (asm_spec_start_token->location,
23415 "an %<asm%> specification is not allowed "
23416 "on a function-definition");
23417 if (attributes)
23418 error_at (attributes_start_token->location,
23419 "attributes are not allowed "
23420 "on a function-definition");
23421 /* This is a function-definition. */
23422 *function_definition_p = true;
23424 /* Parse the function definition. */
23425 if (member_p)
23426 decl = cp_parser_save_member_function_body (parser,
23427 decl_specifiers,
23428 declarator,
23429 prefix_attributes);
23430 else
23431 decl =
23432 (cp_parser_function_definition_from_specifiers_and_declarator
23433 (parser, decl_specifiers, prefix_attributes, declarator));
23435 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
23437 /* This is where the prologue starts... */
23438 DECL_STRUCT_FUNCTION (decl)->function_start_locus
23439 = func_brace_location;
23442 return decl;
23445 else if (parser->fully_implicit_function_template_p)
23447 /* A non-template declaration involving a function parameter list
23448 containing an implicit template parameter will be made into a
23449 template. If the resulting declaration is not going to be an
23450 actual function then finish the template scope here to prevent it.
23451 An error message will be issued once we have a decl to talk about.
23453 FIXME probably we should do type deduction rather than create an
23454 implicit template, but the standard currently doesn't allow it. */
23455 bogus_implicit_tmpl = true;
23456 finish_fully_implicit_template (parser, NULL_TREE);
23459 /* [dcl.dcl]
23461 Only in function declarations for constructors, destructors, type
23462 conversions, and deduction guides can the decl-specifier-seq be omitted.
23464 We explicitly postpone this check past the point where we handle
23465 function-definitions because we tolerate function-definitions
23466 that are missing their return types in some modes. */
23467 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
23469 cp_parser_error (parser,
23470 "expected constructor, destructor, or type conversion");
23471 return error_mark_node;
23474 /* An `=' or an '{' in C++11, indicate an initializer. An '(' may indicate
23475 an initializer as well. */
23476 if (token->type == CPP_EQ
23477 || token->type == CPP_OPEN_PAREN
23478 || token->type == CPP_OPEN_BRACE)
23480 /* Don't get fooled into thinking that F(i)(1)(2) is an initializer.
23481 It isn't; it's an expression. (Here '(i)' would have already been
23482 parsed as a declarator.) */
23483 if (token->type == CPP_OPEN_PAREN
23484 && cp_parser_uncommitted_to_tentative_parse_p (parser))
23486 cp_lexer_save_tokens (parser->lexer);
23487 cp_lexer_consume_token (parser->lexer);
23488 cp_parser_skip_to_closing_parenthesis (parser,
23489 /*recovering*/false,
23490 /*or_comma*/false,
23491 /*consume_paren*/true);
23492 /* If this is an initializer, only a ',' or ';' can follow: either
23493 we have another init-declarator, or we're at the end of an
23494 init-declarator-list which can only be followed by a ';'. */
23495 bool ok = (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23496 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
23497 cp_lexer_rollback_tokens (parser->lexer);
23498 if (UNLIKELY (!ok))
23499 /* Not an init-declarator. */
23500 return error_mark_node;
23502 is_initialized = SD_INITIALIZED;
23503 initialization_kind = token->type;
23504 declarator->init_loc = token->location;
23505 if (maybe_range_for_decl)
23506 *maybe_range_for_decl = error_mark_node;
23507 tmp_init_loc = token->location;
23508 if (init_loc && *init_loc == UNKNOWN_LOCATION)
23509 *init_loc = tmp_init_loc;
23511 if (token->type == CPP_EQ
23512 && function_declarator_p (declarator))
23514 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
23515 if (t2->keyword == RID_DEFAULT)
23516 is_initialized = SD_DEFAULTED;
23517 else if (t2->keyword == RID_DELETE)
23518 is_initialized = SD_DELETED;
23521 else
23523 /* If the init-declarator isn't initialized and isn't followed by a
23524 `,' or `;', it's not a valid init-declarator. */
23525 if (token->type != CPP_COMMA
23526 && token->type != CPP_SEMICOLON)
23528 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
23529 range_for_decl_p = true;
23530 else
23532 if (!maybe_range_for_decl)
23533 cp_parser_error (parser, "expected initializer");
23534 return error_mark_node;
23537 is_initialized = SD_UNINITIALIZED;
23538 initialization_kind = CPP_EOF;
23541 /* Because start_decl has side-effects, we should only call it if we
23542 know we're going ahead. By this point, we know that we cannot
23543 possibly be looking at any other construct. */
23544 cp_parser_commit_to_tentative_parse (parser);
23546 /* Enter the newly declared entry in the symbol table. If we're
23547 processing a declaration in a class-specifier, we wait until
23548 after processing the initializer. */
23549 if (!member_p)
23551 if (parser->in_unbraced_linkage_specification_p)
23552 decl_specifiers->storage_class = sc_extern;
23553 decl = start_decl (declarator, decl_specifiers,
23554 range_for_decl_p? SD_INITIALIZED : is_initialized,
23555 attributes, prefix_attributes, &pushed_scope);
23556 cp_finalize_omp_declare_simd (parser, decl);
23557 cp_finalize_oacc_routine (parser, decl, false);
23558 /* Adjust location of decl if declarator->id_loc is more appropriate:
23559 set, and decl wasn't merged with another decl, in which case its
23560 location would be different from input_location, and more accurate. */
23561 if (DECL_P (decl)
23562 && declarator->id_loc != UNKNOWN_LOCATION
23563 && DECL_SOURCE_LOCATION (decl) == input_location)
23564 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
23566 else if (scope)
23567 /* Enter the SCOPE. That way unqualified names appearing in the
23568 initializer will be looked up in SCOPE. */
23569 pushed_scope = push_scope (scope);
23571 /* Perform deferred access control checks, now that we know in which
23572 SCOPE the declared entity resides. */
23573 if (!member_p && decl)
23575 tree saved_current_function_decl = NULL_TREE;
23577 /* If the entity being declared is a function, pretend that we
23578 are in its scope. If it is a `friend', it may have access to
23579 things that would not otherwise be accessible. */
23580 if (TREE_CODE (decl) == FUNCTION_DECL)
23582 saved_current_function_decl = current_function_decl;
23583 current_function_decl = decl;
23586 /* Perform access checks for template parameters. */
23587 cp_parser_perform_template_parameter_access_checks (checks);
23589 /* Perform the access control checks for the declarator and the
23590 decl-specifiers. */
23591 perform_deferred_access_checks (tf_warning_or_error);
23593 /* Restore the saved value. */
23594 if (TREE_CODE (decl) == FUNCTION_DECL)
23595 current_function_decl = saved_current_function_decl;
23598 /* Parse the initializer. */
23599 initializer = NULL_TREE;
23600 is_direct_init = false;
23601 is_non_constant_init = true;
23602 if (is_initialized)
23604 if (function_declarator_p (declarator))
23606 if (initialization_kind == CPP_EQ)
23607 initializer = cp_parser_pure_specifier (parser);
23608 else
23610 /* If the declaration was erroneous, we don't really
23611 know what the user intended, so just silently
23612 consume the initializer. */
23613 if (decl != error_mark_node)
23614 error_at (tmp_init_loc, "initializer provided for function");
23615 cp_parser_skip_to_closing_parenthesis (parser,
23616 /*recovering=*/true,
23617 /*or_comma=*/false,
23618 /*consume_paren=*/true);
23621 else
23623 /* We want to record the extra mangling scope for in-class
23624 initializers of class members and initializers of static
23625 data member templates and namespace-scope initializers.
23626 The former involves deferring parsing of the initializer
23627 until end of class as with default arguments. So right
23628 here we only handle the latter two. */
23629 bool has_lambda_scope = false;
23631 if (decl != error_mark_node
23632 && !member_p
23633 && (processing_template_decl || DECL_NAMESPACE_SCOPE_P (decl)))
23634 has_lambda_scope = true;
23636 if (has_lambda_scope)
23637 start_lambda_scope (decl);
23638 initializer = cp_parser_initializer (parser,
23639 &is_direct_init,
23640 &is_non_constant_init);
23641 if (has_lambda_scope)
23642 finish_lambda_scope ();
23643 if (initializer == error_mark_node)
23644 cp_parser_skip_to_end_of_statement (parser);
23648 /* The old parser allows attributes to appear after a parenthesized
23649 initializer. Mark Mitchell proposed removing this functionality
23650 on the GCC mailing lists on 2002-08-13. This parser accepts the
23651 attributes -- but ignores them. Made a permerror in GCC 8. */
23652 if (cp_parser_allow_gnu_extensions_p (parser)
23653 && initialization_kind == CPP_OPEN_PAREN
23654 && cp_parser_attributes_opt (parser)
23655 && permerror (input_location,
23656 "attributes after parenthesized initializer ignored"))
23658 static bool hint;
23659 if (flag_permissive && !hint)
23661 hint = true;
23662 inform (input_location,
23663 "this flexibility is deprecated and will be removed");
23667 /* And now complain about a non-function implicit template. */
23668 if (bogus_implicit_tmpl && decl != error_mark_node)
23669 error_at (DECL_SOURCE_LOCATION (decl),
23670 "non-function %qD declared as implicit template", decl);
23672 /* For an in-class declaration, use `grokfield' to create the
23673 declaration. */
23674 if (member_p)
23676 if (pushed_scope)
23678 pop_scope (pushed_scope);
23679 pushed_scope = NULL_TREE;
23681 decl = grokfield (declarator, decl_specifiers,
23682 initializer, !is_non_constant_init,
23683 /*asmspec=*/NULL_TREE,
23684 attr_chainon (attributes, prefix_attributes));
23685 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
23686 cp_parser_save_default_args (parser, decl);
23687 cp_finalize_omp_declare_simd (parser, decl);
23688 cp_finalize_oacc_routine (parser, decl, false);
23691 /* Finish processing the declaration. But, skip member
23692 declarations. */
23693 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
23695 cp_finish_decl (decl,
23696 initializer, !is_non_constant_init,
23697 asm_specification,
23698 /* If the initializer is in parentheses, then this is
23699 a direct-initialization, which means that an
23700 `explicit' constructor is OK. Otherwise, an
23701 `explicit' constructor cannot be used. */
23702 ((is_direct_init || !is_initialized)
23703 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
23705 else if ((cxx_dialect != cxx98) && friend_p
23706 && decl && TREE_CODE (decl) == FUNCTION_DECL)
23707 /* Core issue #226 (C++0x only): A default template-argument
23708 shall not be specified in a friend class template
23709 declaration. */
23710 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
23711 /*is_partial=*/false, /*is_friend_decl=*/1);
23713 if (!friend_p && pushed_scope)
23714 pop_scope (pushed_scope);
23716 if (function_declarator_p (declarator)
23717 && parser->fully_implicit_function_template_p)
23719 if (member_p)
23720 decl = finish_fully_implicit_template (parser, decl);
23721 else
23722 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
23725 if (auto_result && is_initialized && decl_specifiers->type
23726 && type_uses_auto (decl_specifiers->type))
23727 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
23729 return decl;
23732 /* Parse a declarator.
23734 declarator:
23735 direct-declarator
23736 ptr-operator declarator
23738 abstract-declarator:
23739 ptr-operator abstract-declarator [opt]
23740 direct-abstract-declarator
23742 GNU Extensions:
23744 declarator:
23745 attributes [opt] direct-declarator
23746 attributes [opt] ptr-operator declarator
23748 abstract-declarator:
23749 attributes [opt] ptr-operator abstract-declarator [opt]
23750 attributes [opt] direct-abstract-declarator
23752 The parser flags FLAGS is used to control type-specifier parsing.
23754 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
23755 detect constructors, destructors, deduction guides, or conversion operators.
23756 It is set to -1 if the declarator is a name, and +1 if it is a
23757 function. Otherwise it is set to zero. Usually you just want to
23758 test for >0, but internally the negative value is used.
23760 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
23761 a decl-specifier-seq unless it declares a constructor, destructor,
23762 or conversion. It might seem that we could check this condition in
23763 semantic analysis, rather than parsing, but that makes it difficult
23764 to handle something like `f()'. We want to notice that there are
23765 no decl-specifiers, and therefore realize that this is an
23766 expression, not a declaration.)
23768 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
23769 the declarator is a direct-declarator of the form "(...)".
23771 MEMBER_P is true iff this declarator is a member-declarator.
23773 FRIEND_P is true iff this declarator is a friend.
23775 STATIC_P is true iff the keyword static was seen. */
23777 static cp_declarator *
23778 cp_parser_declarator (cp_parser* parser,
23779 cp_parser_declarator_kind dcl_kind,
23780 cp_parser_flags flags,
23781 int* ctor_dtor_or_conv_p,
23782 bool* parenthesized_p,
23783 bool member_p, bool friend_p, bool static_p)
23785 cp_declarator *declarator;
23786 enum tree_code code;
23787 cp_cv_quals cv_quals;
23788 tree class_type;
23789 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
23791 /* Assume this is not a constructor, destructor, or type-conversion
23792 operator. */
23793 if (ctor_dtor_or_conv_p)
23794 *ctor_dtor_or_conv_p = 0;
23796 if (cp_parser_allow_gnu_extensions_p (parser))
23797 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
23799 /* Check for the ptr-operator production. */
23800 cp_parser_parse_tentatively (parser);
23801 /* Parse the ptr-operator. */
23802 code = cp_parser_ptr_operator (parser,
23803 &class_type,
23804 &cv_quals,
23805 &std_attributes);
23807 /* If that worked, then we have a ptr-operator. */
23808 if (cp_parser_parse_definitely (parser))
23810 /* If a ptr-operator was found, then this declarator was not
23811 parenthesized. */
23812 if (parenthesized_p)
23813 *parenthesized_p = false;
23814 /* The dependent declarator is optional if we are parsing an
23815 abstract-declarator. */
23816 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
23817 cp_parser_parse_tentatively (parser);
23819 /* Parse the dependent declarator. */
23820 declarator = cp_parser_declarator (parser, dcl_kind, flags,
23821 /*ctor_dtor_or_conv_p=*/NULL,
23822 /*parenthesized_p=*/NULL,
23823 member_p, friend_p, static_p);
23825 /* If we are parsing an abstract-declarator, we must handle the
23826 case where the dependent declarator is absent. */
23827 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
23828 && !cp_parser_parse_definitely (parser))
23829 declarator = NULL;
23831 declarator = cp_parser_make_indirect_declarator
23832 (code, class_type, cv_quals, declarator, std_attributes);
23834 /* Everything else is a direct-declarator. */
23835 else
23837 if (parenthesized_p)
23838 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
23839 CPP_OPEN_PAREN);
23840 declarator = cp_parser_direct_declarator (parser, dcl_kind,
23841 flags, ctor_dtor_or_conv_p,
23842 member_p, friend_p, static_p);
23845 if (gnu_attributes && declarator && declarator != cp_error_declarator)
23846 declarator->attributes = gnu_attributes;
23847 return declarator;
23850 /* Parse a direct-declarator or direct-abstract-declarator.
23852 direct-declarator:
23853 declarator-id
23854 direct-declarator ( parameter-declaration-clause )
23855 cv-qualifier-seq [opt]
23856 ref-qualifier [opt]
23857 exception-specification [opt]
23858 direct-declarator [ constant-expression [opt] ]
23859 ( declarator )
23861 direct-abstract-declarator:
23862 direct-abstract-declarator [opt]
23863 ( parameter-declaration-clause )
23864 cv-qualifier-seq [opt]
23865 ref-qualifier [opt]
23866 exception-specification [opt]
23867 direct-abstract-declarator [opt] [ constant-expression [opt] ]
23868 ( abstract-declarator )
23870 Returns a representation of the declarator. DCL_KIND is
23871 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
23872 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
23873 we are parsing a direct-declarator. It is
23874 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
23875 of ambiguity we prefer an abstract declarator, as per
23876 [dcl.ambig.res].
23877 The parser flags FLAGS is used to control type-specifier parsing.
23878 CTOR_DTOR_OR_CONV_P, MEMBER_P, FRIEND_P, and STATIC_P are
23879 as for cp_parser_declarator. */
23881 static cp_declarator *
23882 cp_parser_direct_declarator (cp_parser* parser,
23883 cp_parser_declarator_kind dcl_kind,
23884 cp_parser_flags flags,
23885 int* ctor_dtor_or_conv_p,
23886 bool member_p, bool friend_p, bool static_p)
23888 cp_token *token;
23889 cp_declarator *declarator = NULL;
23890 tree scope = NULL_TREE;
23891 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
23892 bool saved_in_declarator_p = parser->in_declarator_p;
23893 bool first = true;
23894 tree pushed_scope = NULL_TREE;
23895 cp_token *open_paren = NULL, *close_paren = NULL;
23897 while (true)
23899 /* Peek at the next token. */
23900 token = cp_lexer_peek_token (parser->lexer);
23901 if (token->type == CPP_OPEN_PAREN)
23903 /* This is either a parameter-declaration-clause, or a
23904 parenthesized declarator. When we know we are parsing a
23905 named declarator, it must be a parenthesized declarator
23906 if FIRST is true. For instance, `(int)' is a
23907 parameter-declaration-clause, with an omitted
23908 direct-abstract-declarator. But `((*))', is a
23909 parenthesized abstract declarator. Finally, when T is a
23910 template parameter `(T)' is a
23911 parameter-declaration-clause, and not a parenthesized
23912 named declarator.
23914 We first try and parse a parameter-declaration-clause,
23915 and then try a nested declarator (if FIRST is true).
23917 It is not an error for it not to be a
23918 parameter-declaration-clause, even when FIRST is
23919 false. Consider,
23921 int i (int);
23922 int i (3);
23924 The first is the declaration of a function while the
23925 second is the definition of a variable, including its
23926 initializer.
23928 Having seen only the parenthesis, we cannot know which of
23929 these two alternatives should be selected. Even more
23930 complex are examples like:
23932 int i (int (a));
23933 int i (int (3));
23935 The former is a function-declaration; the latter is a
23936 variable initialization.
23938 Thus again, we try a parameter-declaration-clause, and if
23939 that fails, we back out and return. */
23941 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
23943 tree params;
23944 bool is_declarator = false;
23946 open_paren = NULL;
23948 /* In a member-declarator, the only valid interpretation
23949 of a parenthesis is the start of a
23950 parameter-declaration-clause. (It is invalid to
23951 initialize a static data member with a parenthesized
23952 initializer; only the "=" form of initialization is
23953 permitted.) */
23954 if (!member_p)
23955 cp_parser_parse_tentatively (parser);
23957 /* Consume the `('. */
23958 const location_t parens_start = token->location;
23959 matching_parens parens;
23960 parens.consume_open (parser);
23961 if (first)
23963 /* If this is going to be an abstract declarator, we're
23964 in a declarator and we can't have default args. */
23965 parser->default_arg_ok_p = false;
23966 parser->in_declarator_p = true;
23969 begin_scope (sk_function_parms, NULL_TREE);
23971 /* Parse the parameter-declaration-clause. */
23972 params
23973 = cp_parser_parameter_declaration_clause (parser, flags);
23974 const location_t parens_end
23975 = cp_lexer_peek_token (parser->lexer)->location;
23977 /* Consume the `)'. */
23978 parens.require_close (parser);
23980 /* For code like
23981 int x(auto(42));
23982 A a(auto(i), 42);
23983 we have synthesized an implicit template parameter and marked
23984 what we thought was a function as an implicit function template.
23985 But now, having seen the whole parameter list, we know it's not
23986 a function declaration, so undo that. */
23987 if (cp_parser_error_occurred (parser)
23988 && parser->fully_implicit_function_template_p
23989 /* Don't do this for the inner (). */
23990 && parser->default_arg_ok_p)
23991 abort_fully_implicit_template (parser);
23993 /* If all went well, parse the cv-qualifier-seq,
23994 ref-qualifier and the exception-specification. */
23995 if (member_p || cp_parser_parse_definitely (parser))
23997 cp_cv_quals cv_quals;
23998 cp_virt_specifiers virt_specifiers;
23999 cp_ref_qualifier ref_qual;
24000 tree exception_specification;
24001 tree late_return;
24002 tree attrs;
24003 bool memfn = (member_p || (pushed_scope
24004 && CLASS_TYPE_P (pushed_scope)));
24005 unsigned char local_variables_forbidden_p
24006 = parser->local_variables_forbidden_p;
24007 /* 'this' is not allowed in static member functions. */
24008 if (static_p || friend_p)
24009 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
24011 is_declarator = true;
24013 if (ctor_dtor_or_conv_p)
24014 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
24015 first = false;
24017 /* Parse the cv-qualifier-seq. */
24018 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
24019 /* Parse the ref-qualifier. */
24020 ref_qual = cp_parser_ref_qualifier_opt (parser);
24021 /* Parse the tx-qualifier. */
24022 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
24024 tree save_ccp = current_class_ptr;
24025 tree save_ccr = current_class_ref;
24026 if (memfn && !friend_p && !static_p)
24027 /* DR 1207: 'this' is in scope after the cv-quals. */
24028 inject_this_parameter (current_class_type, cv_quals);
24030 /* If it turned out that this is e.g. a pointer to a
24031 function, we don't want to delay noexcept parsing. */
24032 if (declarator == NULL || declarator->kind != cdk_id)
24033 flags &= ~CP_PARSER_FLAGS_DELAY_NOEXCEPT;
24035 /* Parse the exception-specification. */
24036 exception_specification
24037 = cp_parser_exception_specification_opt (parser,
24038 flags);
24040 attrs = cp_parser_std_attribute_spec_seq (parser);
24042 cp_omp_declare_simd_data odsd;
24043 if ((flag_openmp || flag_openmp_simd)
24044 && declarator
24045 && declarator->std_attributes
24046 && declarator->kind == cdk_id)
24048 tree *pa = &declarator->std_attributes;
24049 cp_parser_handle_directive_omp_attributes (parser, pa,
24050 &odsd, false);
24053 /* In here, we handle cases where attribute is used after
24054 the function declaration. For example:
24055 void func (int x) __attribute__((vector(..))); */
24056 tree gnu_attrs = NULL_TREE;
24057 tree requires_clause = NULL_TREE;
24058 late_return
24059 = cp_parser_late_return_type_opt (parser, declarator,
24060 requires_clause);
24062 cp_finalize_omp_declare_simd (parser, &odsd);
24064 /* Parse the virt-specifier-seq. */
24065 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
24067 location_t parens_loc = make_location (parens_start,
24068 parens_start,
24069 parens_end);
24070 /* Create the function-declarator. */
24071 declarator = make_call_declarator (declarator,
24072 params,
24073 cv_quals,
24074 virt_specifiers,
24075 ref_qual,
24076 tx_qual,
24077 exception_specification,
24078 late_return,
24079 requires_clause,
24080 attrs,
24081 parens_loc);
24082 declarator->attributes = gnu_attrs;
24083 /* Any subsequent parameter lists are to do with
24084 return type, so are not those of the declared
24085 function. */
24086 parser->default_arg_ok_p = false;
24088 current_class_ptr = save_ccp;
24089 current_class_ref = save_ccr;
24091 /* Restore the state of local_variables_forbidden_p. */
24092 parser->local_variables_forbidden_p
24093 = local_variables_forbidden_p;
24096 /* Remove the function parms from scope. */
24097 pop_bindings_and_leave_scope ();
24099 if (is_declarator)
24100 /* Repeat the main loop. */
24101 continue;
24104 /* If this is the first, we can try a parenthesized
24105 declarator. */
24106 if (first)
24108 bool saved_in_type_id_in_expr_p;
24110 parser->default_arg_ok_p = saved_default_arg_ok_p;
24111 parser->in_declarator_p = saved_in_declarator_p;
24113 open_paren = token;
24114 /* Consume the `('. */
24115 matching_parens parens;
24116 parens.consume_open (parser);
24117 /* Parse the nested declarator. */
24118 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
24119 parser->in_type_id_in_expr_p = true;
24120 declarator
24121 = cp_parser_declarator (parser, dcl_kind, flags,
24122 ctor_dtor_or_conv_p,
24123 /*parenthesized_p=*/NULL,
24124 member_p, friend_p,
24125 /*static_p=*/false);
24126 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
24127 first = false;
24128 /* Expect a `)'. */
24129 close_paren = cp_lexer_peek_token (parser->lexer);
24130 if (!parens.require_close (parser))
24131 declarator = cp_error_declarator;
24132 if (declarator == cp_error_declarator)
24133 break;
24135 goto handle_declarator;
24137 /* Otherwise, we must be done. */
24138 else
24139 break;
24141 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
24142 && token->type == CPP_OPEN_SQUARE
24143 && !cp_next_tokens_can_be_attribute_p (parser))
24145 /* Parse an array-declarator. */
24146 tree bounds, attrs;
24148 if (ctor_dtor_or_conv_p)
24149 *ctor_dtor_or_conv_p = 0;
24151 open_paren = NULL;
24152 first = false;
24153 parser->default_arg_ok_p = false;
24154 parser->in_declarator_p = true;
24155 /* Consume the `['. */
24156 cp_lexer_consume_token (parser->lexer);
24157 /* Peek at the next token. */
24158 token = cp_lexer_peek_token (parser->lexer);
24159 /* If the next token is `]', then there is no
24160 constant-expression. */
24161 if (token->type != CPP_CLOSE_SQUARE)
24163 bool non_constant_p;
24164 bounds
24165 = cp_parser_constant_expression (parser,
24166 /*allow_non_constant=*/true,
24167 &non_constant_p);
24168 if (!non_constant_p)
24169 /* OK */;
24170 else if (error_operand_p (bounds))
24171 /* Already gave an error. */;
24172 else if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
24173 /* Let compute_array_index_type diagnose this. */;
24174 else if (!parser->in_function_body
24175 || parsing_function_declarator ())
24177 /* Normally, the array bound must be an integral constant
24178 expression. However, as an extension, we allow VLAs
24179 in function scopes as long as they aren't part of a
24180 parameter declaration. */
24181 cp_parser_error (parser,
24182 "array bound is not an integer constant");
24183 bounds = error_mark_node;
24185 else if (processing_template_decl
24186 && !type_dependent_expression_p (bounds))
24188 /* Remember this wasn't a constant-expression. */
24189 bounds = build_nop (TREE_TYPE (bounds), bounds);
24190 TREE_SIDE_EFFECTS (bounds) = 1;
24193 else
24194 bounds = NULL_TREE;
24195 /* Look for the closing `]'. */
24196 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
24198 declarator = cp_error_declarator;
24199 break;
24202 attrs = cp_parser_std_attribute_spec_seq (parser);
24203 declarator = make_array_declarator (declarator, bounds);
24204 declarator->std_attributes = attrs;
24206 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
24209 tree qualifying_scope;
24210 tree unqualified_name;
24211 tree attrs;
24212 special_function_kind sfk;
24213 bool abstract_ok;
24214 bool pack_expansion_p = false;
24215 cp_token *declarator_id_start_token;
24217 /* Parse a declarator-id */
24218 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
24219 if (abstract_ok)
24221 cp_parser_parse_tentatively (parser);
24223 /* If we see an ellipsis, we should be looking at a
24224 parameter pack. */
24225 if (token->type == CPP_ELLIPSIS)
24227 /* Consume the `...' */
24228 cp_lexer_consume_token (parser->lexer);
24230 pack_expansion_p = true;
24234 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
24235 unqualified_name
24236 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
24237 qualifying_scope = parser->scope;
24238 if (abstract_ok)
24240 bool okay = false;
24242 if (!unqualified_name && pack_expansion_p)
24244 /* Check whether an error occurred. */
24245 okay = !cp_parser_error_occurred (parser);
24247 /* We already consumed the ellipsis to mark a
24248 parameter pack, but we have no way to report it,
24249 so abort the tentative parse. We will be exiting
24250 immediately anyway. */
24251 cp_parser_abort_tentative_parse (parser);
24253 else
24254 okay = cp_parser_parse_definitely (parser);
24256 if (!okay)
24257 unqualified_name = error_mark_node;
24258 else if (unqualified_name
24259 && (qualifying_scope
24260 || (!identifier_p (unqualified_name))))
24262 cp_parser_error (parser, "expected unqualified-id");
24263 unqualified_name = error_mark_node;
24267 if (!unqualified_name)
24268 return NULL;
24269 if (unqualified_name == error_mark_node)
24271 declarator = cp_error_declarator;
24272 pack_expansion_p = false;
24273 declarator->parameter_pack_p = false;
24274 break;
24277 attrs = cp_parser_std_attribute_spec_seq (parser);
24279 if (qualifying_scope && at_namespace_scope_p ()
24280 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
24282 /* In the declaration of a member of a template class
24283 outside of the class itself, the SCOPE will sometimes
24284 be a TYPENAME_TYPE. For example, given:
24286 template <typename T>
24287 int S<T>::R::i = 3;
24289 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
24290 this context, we must resolve S<T>::R to an ordinary
24291 type, rather than a typename type.
24293 The reason we normally avoid resolving TYPENAME_TYPEs
24294 is that a specialization of `S' might render
24295 `S<T>::R' not a type. However, if `S' is
24296 specialized, then this `i' will not be used, so there
24297 is no harm in resolving the types here. */
24298 tree type;
24300 /* Resolve the TYPENAME_TYPE. */
24301 type = resolve_typename_type (qualifying_scope,
24302 /*only_current_p=*/false);
24303 /* If that failed, the declarator is invalid. */
24304 if (TREE_CODE (type) == TYPENAME_TYPE)
24306 if (typedef_variant_p (type))
24307 error_at (declarator_id_start_token->location,
24308 "cannot define member of dependent typedef "
24309 "%qT", type);
24310 else
24311 error_at (declarator_id_start_token->location,
24312 "%<%T::%E%> is not a type",
24313 TYPE_CONTEXT (qualifying_scope),
24314 TYPE_IDENTIFIER (qualifying_scope));
24316 qualifying_scope = type;
24319 sfk = sfk_none;
24321 if (unqualified_name)
24323 tree class_type;
24325 if (qualifying_scope
24326 && CLASS_TYPE_P (qualifying_scope))
24327 class_type = qualifying_scope;
24328 else
24329 class_type = current_class_type;
24331 if (TREE_CODE (unqualified_name) == TYPE_DECL)
24333 tree name_type = TREE_TYPE (unqualified_name);
24335 if (!class_type || !same_type_p (name_type, class_type))
24337 /* We do not attempt to print the declarator
24338 here because we do not have enough
24339 information about its original syntactic
24340 form. */
24341 cp_parser_error (parser, "invalid declarator");
24342 declarator = cp_error_declarator;
24343 break;
24345 else if (qualifying_scope
24346 && CLASSTYPE_USE_TEMPLATE (name_type))
24348 error_at (declarator_id_start_token->location,
24349 "invalid use of constructor as a template");
24350 inform (declarator_id_start_token->location,
24351 "use %<%T::%D%> instead of %<%T::%D%> to "
24352 "name the constructor in a qualified name",
24353 class_type,
24354 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
24355 class_type, name_type);
24356 declarator = cp_error_declarator;
24357 break;
24359 unqualified_name = constructor_name (class_type);
24362 if (class_type)
24364 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
24365 sfk = sfk_destructor;
24366 else if (identifier_p (unqualified_name)
24367 && IDENTIFIER_CONV_OP_P (unqualified_name))
24368 sfk = sfk_conversion;
24369 else if (/* There's no way to declare a constructor
24370 for an unnamed type, even if the type
24371 got a name for linkage purposes. */
24372 !TYPE_WAS_UNNAMED (class_type)
24373 /* Handle correctly (c++/19200):
24375 struct S {
24376 struct T{};
24377 friend void S(T);
24380 and also:
24382 namespace N {
24383 void S();
24386 struct S {
24387 friend void N::S();
24388 }; */
24389 && (!friend_p || class_type == qualifying_scope)
24390 && constructor_name_p (unqualified_name,
24391 class_type))
24392 sfk = sfk_constructor;
24393 else if (is_overloaded_fn (unqualified_name)
24394 && DECL_CONSTRUCTOR_P (get_first_fn
24395 (unqualified_name)))
24396 sfk = sfk_constructor;
24398 if (ctor_dtor_or_conv_p && sfk != sfk_none)
24399 *ctor_dtor_or_conv_p = -1;
24402 declarator = make_id_declarator (qualifying_scope,
24403 unqualified_name,
24404 sfk, token->location);
24405 declarator->std_attributes = attrs;
24406 declarator->parameter_pack_p = pack_expansion_p;
24408 if (pack_expansion_p)
24409 maybe_warn_variadic_templates ();
24411 /* We're looking for this case in [temp.res]:
24412 A qualified-id is assumed to name a type if [...]
24413 - it is a decl-specifier of the decl-specifier-seq of a
24414 parameter-declaration in a declarator of a function or
24415 function template declaration, ... */
24416 if (cxx_dialect >= cxx20
24417 && (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL)
24418 && declarator->kind == cdk_id
24419 && !at_class_scope_p ()
24420 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24422 /* ...whose declarator-id is qualified. If it isn't, never
24423 assume the parameters to refer to types. */
24424 if (qualifying_scope == NULL_TREE)
24425 flags &= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
24426 else
24428 /* Now we have something like
24429 template <typename T> int C::x(S::p);
24430 which can be a function template declaration or a
24431 variable template definition. If name lookup for
24432 the declarator-id C::x finds one or more function
24433 templates, assume S::p to name a type. Otherwise,
24434 don't. */
24435 tree decl
24436 = cp_parser_lookup_name (parser, unqualified_name,
24437 none_type,
24438 /*is_template=*/false,
24439 /*is_namespace=*/false,
24440 /*check_dependency=*/false,
24441 /*ambiguous_decls=*/NULL,
24442 token->location);
24444 if (!is_overloaded_fn (decl)
24445 /* Allow
24446 template<typename T>
24447 A<T>::A(T::type) { } */
24448 && !(MAYBE_CLASS_TYPE_P (qualifying_scope)
24449 && constructor_name_p (unqualified_name,
24450 qualifying_scope)))
24451 flags &= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
24456 handle_declarator:;
24457 scope = get_scope_of_declarator (declarator);
24458 if (scope)
24460 /* Any names that appear after the declarator-id for a
24461 member are looked up in the containing scope. */
24462 if (at_function_scope_p ())
24464 /* But declarations with qualified-ids can't appear in a
24465 function. */
24466 cp_parser_error (parser, "qualified-id in declaration");
24467 declarator = cp_error_declarator;
24468 break;
24470 pushed_scope = push_scope (scope);
24472 parser->in_declarator_p = true;
24473 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
24474 || (declarator && declarator->kind == cdk_id))
24475 /* Default args are only allowed on function
24476 declarations. */
24477 parser->default_arg_ok_p = saved_default_arg_ok_p;
24478 else
24479 parser->default_arg_ok_p = false;
24481 first = false;
24483 /* We're done. */
24484 else
24485 break;
24488 /* For an abstract declarator, we might wind up with nothing at this
24489 point. That's an error; the declarator is not optional. */
24490 if (!declarator)
24491 cp_parser_error (parser, "expected declarator");
24492 else if (open_paren)
24494 /* Record overly parenthesized declarator so we can give a
24495 diagnostic about confusing decl/expr disambiguation. */
24496 if (declarator->kind == cdk_array)
24498 /* If the open and close parens are on different lines, this
24499 is probably a formatting thing, so ignore. */
24500 expanded_location open = expand_location (open_paren->location);
24501 expanded_location close = expand_location (close_paren->location);
24502 if (open.line != close.line || open.file != close.file)
24503 open_paren = NULL;
24505 if (open_paren)
24506 declarator->parenthesized = make_location (open_paren->location,
24507 open_paren->location,
24508 close_paren->location);
24511 /* If we entered a scope, we must exit it now. */
24512 if (pushed_scope)
24513 pop_scope (pushed_scope);
24515 parser->default_arg_ok_p = saved_default_arg_ok_p;
24516 parser->in_declarator_p = saved_in_declarator_p;
24518 return declarator;
24521 /* Parse a ptr-operator.
24523 ptr-operator:
24524 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
24525 * cv-qualifier-seq [opt]
24527 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
24528 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
24530 GNU Extension:
24532 ptr-operator:
24533 & cv-qualifier-seq [opt]
24535 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
24536 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
24537 an rvalue reference. In the case of a pointer-to-member, *TYPE is
24538 filled in with the TYPE containing the member. *CV_QUALS is
24539 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
24540 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
24541 Note that the tree codes returned by this function have nothing
24542 to do with the types of trees that will be eventually be created
24543 to represent the pointer or reference type being parsed. They are
24544 just constants with suggestive names. */
24545 static enum tree_code
24546 cp_parser_ptr_operator (cp_parser* parser,
24547 tree* type,
24548 cp_cv_quals *cv_quals,
24549 tree *attributes)
24551 enum tree_code code = ERROR_MARK;
24552 cp_token *token;
24553 tree attrs = NULL_TREE;
24555 /* Assume that it's not a pointer-to-member. */
24556 *type = NULL_TREE;
24557 /* And that there are no cv-qualifiers. */
24558 *cv_quals = TYPE_UNQUALIFIED;
24560 /* Peek at the next token. */
24561 token = cp_lexer_peek_token (parser->lexer);
24563 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
24564 if (token->type == CPP_MULT)
24565 code = INDIRECT_REF;
24566 else if (token->type == CPP_AND)
24567 code = ADDR_EXPR;
24568 else if ((cxx_dialect != cxx98) &&
24569 token->type == CPP_AND_AND) /* C++0x only */
24570 code = NON_LVALUE_EXPR;
24572 if (code != ERROR_MARK)
24574 /* Consume the `*', `&' or `&&'. */
24575 cp_lexer_consume_token (parser->lexer);
24577 /* A `*' can be followed by a cv-qualifier-seq, and so can a
24578 `&', if we are allowing GNU extensions. (The only qualifier
24579 that can legally appear after `&' is `restrict', but that is
24580 enforced during semantic analysis. */
24581 if (code == INDIRECT_REF
24582 || cp_parser_allow_gnu_extensions_p (parser))
24583 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
24585 attrs = cp_parser_std_attribute_spec_seq (parser);
24586 if (attributes != NULL)
24587 *attributes = attrs;
24589 else
24591 /* Try the pointer-to-member case. */
24592 cp_parser_parse_tentatively (parser);
24593 /* Look for the optional `::' operator. */
24594 cp_parser_global_scope_opt (parser,
24595 /*current_scope_valid_p=*/false);
24596 /* Look for the nested-name specifier. */
24597 token = cp_lexer_peek_token (parser->lexer);
24598 cp_parser_nested_name_specifier (parser,
24599 /*typename_keyword_p=*/false,
24600 /*check_dependency_p=*/true,
24601 /*type_p=*/false,
24602 /*is_declaration=*/false);
24603 /* If we found it, and the next token is a `*', then we are
24604 indeed looking at a pointer-to-member operator. */
24605 if (!cp_parser_error_occurred (parser)
24606 && cp_parser_require (parser, CPP_MULT, RT_MULT))
24608 /* Indicate that the `*' operator was used. */
24609 code = INDIRECT_REF;
24611 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
24612 error_at (token->location, "%qD is a namespace", parser->scope);
24613 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
24614 error_at (token->location, "cannot form pointer to member of "
24615 "non-class %q#T", parser->scope);
24616 else
24618 /* The type of which the member is a member is given by the
24619 current SCOPE. */
24620 *type = parser->scope;
24621 /* The next name will not be qualified. */
24622 parser->scope = NULL_TREE;
24623 parser->qualifying_scope = NULL_TREE;
24624 parser->object_scope = NULL_TREE;
24625 /* Look for optional c++11 attributes. */
24626 attrs = cp_parser_std_attribute_spec_seq (parser);
24627 if (attributes != NULL)
24628 *attributes = attrs;
24629 /* Look for the optional cv-qualifier-seq. */
24630 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
24633 /* If that didn't work we don't have a ptr-operator. */
24634 if (!cp_parser_parse_definitely (parser))
24635 cp_parser_error (parser, "expected ptr-operator");
24638 return code;
24641 /* Parse an (optional) cv-qualifier-seq.
24643 cv-qualifier-seq:
24644 cv-qualifier cv-qualifier-seq [opt]
24646 cv-qualifier:
24647 const
24648 volatile
24650 GNU Extension:
24652 cv-qualifier:
24653 __restrict__
24655 Returns a bitmask representing the cv-qualifiers. */
24657 static cp_cv_quals
24658 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
24660 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
24662 while (true)
24664 cp_token *token;
24665 cp_cv_quals cv_qualifier;
24667 /* Peek at the next token. */
24668 token = cp_lexer_peek_token (parser->lexer);
24669 /* See if it's a cv-qualifier. */
24670 switch (token->keyword)
24672 case RID_CONST:
24673 cv_qualifier = TYPE_QUAL_CONST;
24674 break;
24676 case RID_VOLATILE:
24677 cv_qualifier = TYPE_QUAL_VOLATILE;
24678 break;
24680 case RID_RESTRICT:
24681 cv_qualifier = TYPE_QUAL_RESTRICT;
24682 break;
24684 default:
24685 cv_qualifier = TYPE_UNQUALIFIED;
24686 break;
24689 if (!cv_qualifier)
24690 break;
24692 if (cv_quals & cv_qualifier)
24694 gcc_rich_location richloc (token->location);
24695 richloc.add_fixit_remove ();
24696 error_at (&richloc, "duplicate cv-qualifier");
24697 cp_lexer_purge_token (parser->lexer);
24699 else
24701 cp_lexer_consume_token (parser->lexer);
24702 cv_quals |= cv_qualifier;
24706 return cv_quals;
24709 /* Parse an (optional) ref-qualifier
24711 ref-qualifier:
24715 Returns cp_ref_qualifier representing ref-qualifier. */
24717 static cp_ref_qualifier
24718 cp_parser_ref_qualifier_opt (cp_parser* parser)
24720 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
24722 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
24723 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
24724 return ref_qual;
24726 while (true)
24728 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
24729 cp_token *token = cp_lexer_peek_token (parser->lexer);
24731 switch (token->type)
24733 case CPP_AND:
24734 curr_ref_qual = REF_QUAL_LVALUE;
24735 break;
24737 case CPP_AND_AND:
24738 curr_ref_qual = REF_QUAL_RVALUE;
24739 break;
24741 default:
24742 curr_ref_qual = REF_QUAL_NONE;
24743 break;
24746 if (!curr_ref_qual)
24747 break;
24748 else if (ref_qual)
24750 error_at (token->location, "multiple ref-qualifiers");
24751 cp_lexer_purge_token (parser->lexer);
24753 else
24755 ref_qual = curr_ref_qual;
24756 cp_lexer_consume_token (parser->lexer);
24760 return ref_qual;
24763 /* Parse an optional tx-qualifier.
24765 tx-qualifier:
24766 transaction_safe
24767 transaction_safe_dynamic */
24769 static tree
24770 cp_parser_tx_qualifier_opt (cp_parser *parser)
24772 cp_token *token = cp_lexer_peek_token (parser->lexer);
24773 if (token->type == CPP_NAME)
24775 tree name = token->u.value;
24776 const char *p = IDENTIFIER_POINTER (name);
24777 const int len = strlen ("transaction_safe");
24778 if (startswith (p, "transaction_safe"))
24780 p += len;
24781 if (*p == '\0'
24782 || !strcmp (p, "_dynamic"))
24784 cp_lexer_consume_token (parser->lexer);
24785 if (!flag_tm)
24787 error ("%qE requires %<-fgnu-tm%>", name);
24788 return NULL_TREE;
24790 else
24791 return name;
24795 return NULL_TREE;
24798 /* Parse an (optional) virt-specifier-seq.
24800 virt-specifier-seq:
24801 virt-specifier virt-specifier-seq [opt]
24803 virt-specifier:
24804 override
24805 final
24807 Returns a bitmask representing the virt-specifiers. */
24809 static cp_virt_specifiers
24810 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
24812 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
24814 while (true)
24816 cp_token *token;
24817 cp_virt_specifiers virt_specifier;
24819 /* Peek at the next token. */
24820 token = cp_lexer_peek_token (parser->lexer);
24821 /* See if it's a virt-specifier-qualifier. */
24822 if (token->type != CPP_NAME)
24823 break;
24824 if (id_equal (token->u.value, "override"))
24826 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
24827 virt_specifier = VIRT_SPEC_OVERRIDE;
24829 else if (id_equal (token->u.value, "final"))
24831 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
24832 virt_specifier = VIRT_SPEC_FINAL;
24834 else if (id_equal (token->u.value, "__final"))
24836 virt_specifier = VIRT_SPEC_FINAL;
24838 else
24839 break;
24841 if (virt_specifiers & virt_specifier)
24843 gcc_rich_location richloc (token->location);
24844 richloc.add_fixit_remove ();
24845 error_at (&richloc, "duplicate virt-specifier");
24846 cp_lexer_purge_token (parser->lexer);
24848 else
24850 cp_lexer_consume_token (parser->lexer);
24851 virt_specifiers |= virt_specifier;
24854 return virt_specifiers;
24857 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
24858 is in scope even though it isn't real. */
24860 void
24861 inject_this_parameter (tree ctype, cp_cv_quals quals)
24863 tree this_parm;
24865 if (current_class_ptr)
24867 /* We don't clear this between NSDMIs. Is it already what we want? */
24868 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
24869 if (DECL_P (current_class_ptr)
24870 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
24871 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
24872 && cp_type_quals (type) == quals)
24873 return;
24876 this_parm = build_this_parm (NULL_TREE, ctype, quals);
24877 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
24878 current_class_ptr = NULL_TREE;
24879 current_class_ref
24880 = cp_build_fold_indirect_ref (this_parm);
24881 current_class_ptr = this_parm;
24884 /* Return true iff our current scope is a non-static data member
24885 initializer. */
24887 bool
24888 parsing_nsdmi (void)
24890 /* We recognize NSDMI context by the context-less 'this' pointer set up
24891 by the function above. */
24892 if (current_class_ptr
24893 && TREE_CODE (current_class_ptr) == PARM_DECL
24894 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
24895 return true;
24896 return false;
24899 /* True if we're parsing a function declarator. */
24901 bool
24902 parsing_function_declarator ()
24904 /* this_entity is NULL for a function parameter scope while parsing the
24905 declarator; it is set when parsing the body of the function. */
24906 return (current_binding_level->kind == sk_function_parms
24907 && !current_binding_level->this_entity);
24910 /* Parse a late-specified return type, if any. This is not a separate
24911 non-terminal, but part of a function declarator, which looks like
24913 -> trailing-type-specifier-seq abstract-declarator(opt)
24915 Returns the type indicated by the type-id.
24917 In addition to this, parse any queued up #pragma omp declare simd
24918 clauses, and #pragma acc routine clauses.
24920 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
24921 function. */
24923 static tree
24924 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
24925 tree& requires_clause)
24927 cp_token *token;
24928 tree type = NULL_TREE;
24929 bool declare_simd_p = (parser->omp_declare_simd
24930 && declarator
24931 && declarator->kind == cdk_id);
24933 bool oacc_routine_p = (parser->oacc_routine
24934 && declarator
24935 && declarator->kind == cdk_id);
24937 /* Peek at the next token. */
24938 token = cp_lexer_peek_token (parser->lexer);
24939 /* A late-specified return type is indicated by an initial '->'. */
24940 if (token->type != CPP_DEREF
24941 && token->keyword != RID_REQUIRES
24942 && !(token->type == CPP_NAME
24943 && token->u.value == ridpointers[RID_REQUIRES])
24944 && !(declare_simd_p || oacc_routine_p))
24945 return NULL_TREE;
24947 if (token->type == CPP_DEREF)
24949 /* Consume the ->. */
24950 cp_lexer_consume_token (parser->lexer);
24952 type = cp_parser_trailing_type_id (parser);
24955 /* Function declarations may be followed by a trailing
24956 requires-clause. */
24957 requires_clause = cp_parser_requires_clause_opt (parser, false);
24959 if (declare_simd_p)
24960 declarator->attributes
24961 = cp_parser_late_parsing_omp_declare_simd (parser,
24962 declarator->attributes);
24963 if (oacc_routine_p)
24964 declarator->attributes
24965 = cp_parser_late_parsing_oacc_routine (parser,
24966 declarator->attributes);
24968 return type;
24971 /* Parse a declarator-id.
24973 declarator-id:
24974 id-expression
24975 :: [opt] nested-name-specifier [opt] type-name
24977 In the `id-expression' case, the value returned is as for
24978 cp_parser_id_expression if the id-expression was an unqualified-id.
24979 If the id-expression was a qualified-id, then a SCOPE_REF is
24980 returned. The first operand is the scope (either a NAMESPACE_DECL
24981 or TREE_TYPE), but the second is still just a representation of an
24982 unqualified-id. */
24984 static tree
24985 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
24987 tree id;
24988 /* The expression must be an id-expression. Assume that qualified
24989 names are the names of types so that:
24991 template <class T>
24992 int S<T>::R::i = 3;
24994 will work; we must treat `S<T>::R' as the name of a type.
24995 Similarly, assume that qualified names are templates, where
24996 required, so that:
24998 template <class T>
24999 int S<T>::R<T>::i = 3;
25001 will work, too. */
25002 id = cp_parser_id_expression (parser,
25003 /*template_keyword_p=*/false,
25004 /*check_dependency_p=*/false,
25005 /*template_p=*/NULL,
25006 /*declarator_p=*/true,
25007 optional_p);
25008 if (id && BASELINK_P (id))
25009 id = BASELINK_FUNCTIONS (id);
25010 return id;
25013 /* Parse a type-id.
25015 type-id:
25016 type-specifier-seq abstract-declarator [opt]
25018 The parser flags FLAGS is used to control type-specifier parsing.
25020 If IS_TEMPLATE_ARG is true, we are parsing a template argument.
25022 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
25023 i.e. we've just seen "->".
25025 Returns the TYPE specified. */
25027 static tree
25028 cp_parser_type_id_1 (cp_parser *parser, cp_parser_flags flags,
25029 bool is_template_arg, bool is_trailing_return,
25030 location_t *type_location)
25032 cp_decl_specifier_seq type_specifier_seq;
25033 cp_declarator *abstract_declarator;
25035 /* Parse the type-specifier-seq. */
25036 cp_parser_type_specifier_seq (parser, flags,
25037 /*is_declaration=*/false,
25038 is_trailing_return,
25039 &type_specifier_seq);
25040 if (type_location)
25041 *type_location = type_specifier_seq.locations[ds_type_spec];
25043 if (is_template_arg && type_specifier_seq.type
25044 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
25045 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
25046 /* A bare template name as a template argument is a template template
25047 argument, not a placeholder, so fail parsing it as a type argument. */
25049 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
25050 cp_parser_simulate_error (parser);
25051 return error_mark_node;
25053 if (type_specifier_seq.type == error_mark_node)
25054 return error_mark_node;
25056 /* There might or might not be an abstract declarator. */
25057 cp_parser_parse_tentatively (parser);
25058 /* Look for the declarator. */
25059 abstract_declarator
25060 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT,
25061 CP_PARSER_FLAGS_NONE, NULL,
25062 /*parenthesized_p=*/NULL,
25063 /*member_p=*/false,
25064 /*friend_p=*/false,
25065 /*static_p=*/false);
25066 /* Check to see if there really was a declarator. */
25067 if (!cp_parser_parse_definitely (parser))
25068 abstract_declarator = NULL;
25070 bool auto_typeid_ok = false;
25071 /* The concepts TS allows 'auto' as a type-id. */
25072 if (flag_concepts_ts)
25073 auto_typeid_ok = !parser->in_type_id_in_expr_p;
25074 /* DR 625 prohibits use of auto as a template-argument. We allow 'auto'
25075 outside the template-argument-list context here only for the sake of
25076 diagnostic: grokdeclarator then can emit a better error message for
25077 e.g. using T = auto. */
25078 else if (flag_concepts)
25079 auto_typeid_ok = (!parser->in_type_id_in_expr_p
25080 && !parser->in_template_argument_list_p);
25082 if (type_specifier_seq.type
25083 && !auto_typeid_ok
25084 /* None of the valid uses of 'auto' in C++14 involve the type-id
25085 nonterminal, but it is valid in a trailing-return-type. */
25086 && !(cxx_dialect >= cxx14 && is_trailing_return))
25087 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
25089 /* A type-id with type 'auto' is only ok if the abstract declarator
25090 is a function declarator with a late-specified return type.
25092 A type-id with 'auto' is also valid in a trailing-return-type
25093 in a compound-requirement. */
25094 if (abstract_declarator
25095 && abstract_declarator->kind == cdk_function
25096 && abstract_declarator->u.function.late_return_type)
25097 /* OK */;
25098 else if (parser->in_result_type_constraint_p)
25099 /* OK */;
25100 else
25102 if (!cp_parser_simulate_error (parser))
25104 location_t loc = type_specifier_seq.locations[ds_type_spec];
25105 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
25107 auto_diagnostic_group g;
25108 gcc_rich_location richloc (loc);
25109 richloc.add_fixit_insert_after ("<>");
25110 error_at (&richloc, "missing template arguments after %qE",
25111 tmpl);
25112 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
25113 tmpl);
25115 else if (parser->in_template_argument_list_p)
25116 error_at (loc, "%qT not permitted in template argument",
25117 auto_node);
25118 else
25119 error_at (loc, "invalid use of %qT", auto_node);
25121 return error_mark_node;
25125 return groktypename (&type_specifier_seq, abstract_declarator,
25126 is_template_arg);
25129 /* Wrapper for cp_parser_type_id_1. */
25131 static tree
25132 cp_parser_type_id (cp_parser *parser, cp_parser_flags flags,
25133 location_t *type_location)
25135 return cp_parser_type_id_1 (parser, flags, false, false, type_location);
25138 /* Wrapper for cp_parser_type_id_1. */
25140 static tree
25141 cp_parser_template_type_arg (cp_parser *parser)
25143 const char *saved_message = parser->type_definition_forbidden_message;
25144 parser->type_definition_forbidden_message
25145 = G_("types may not be defined in template arguments");
25146 tree r = cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_NONE,
25147 /*is_template_arg=*/true,
25148 /*is_trailing_return=*/false, nullptr);
25149 parser->type_definition_forbidden_message = saved_message;
25150 /* cp_parser_type_id_1 checks for auto, but only for
25151 ->auto_is_implicit_function_template_parm_p. */
25152 if (cxx_dialect >= cxx14 && !flag_concepts_ts && type_uses_auto (r))
25154 error ("invalid use of %<auto%> in template argument");
25155 r = error_mark_node;
25157 return r;
25160 /* Wrapper for cp_parser_type_id_1. */
25162 static tree
25163 cp_parser_trailing_type_id (cp_parser *parser)
25165 return cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
25166 false, true, NULL);
25169 /* Parse a type-specifier-seq.
25171 type-specifier-seq:
25172 type-specifier type-specifier-seq [opt]
25174 GNU extension:
25176 type-specifier-seq:
25177 attributes type-specifier-seq [opt]
25179 The parser flags FLAGS is used to control type-specifier parsing.
25181 If IS_DECLARATION is true, we are at the start of a "condition" or
25182 exception-declaration, so we might be followed by a declarator-id.
25184 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
25185 i.e. we've just seen "->".
25187 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
25189 static void
25190 cp_parser_type_specifier_seq (cp_parser* parser,
25191 cp_parser_flags flags,
25192 bool is_declaration,
25193 bool is_trailing_return,
25194 cp_decl_specifier_seq *type_specifier_seq)
25196 bool seen_type_specifier = false;
25197 cp_token *start_token = NULL;
25199 /* Clear the TYPE_SPECIFIER_SEQ. */
25200 clear_decl_specs (type_specifier_seq);
25202 flags |= CP_PARSER_FLAGS_OPTIONAL;
25203 /* In the context of a trailing return type, enum E { } is an
25204 elaborated-type-specifier followed by a function-body, not an
25205 enum-specifier. */
25206 if (is_trailing_return)
25207 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
25209 /* Parse the type-specifiers and attributes. */
25210 while (true)
25212 tree type_specifier;
25213 bool is_cv_qualifier;
25215 /* Check for attributes first. */
25216 if (cp_next_tokens_can_be_attribute_p (parser))
25218 /* GNU attributes at the end of a declaration apply to the
25219 declaration as a whole, not to the trailing return type. So look
25220 ahead to see if these attributes are at the end. */
25221 if (seen_type_specifier && is_trailing_return
25222 && cp_next_tokens_can_be_gnu_attribute_p (parser))
25224 size_t n = cp_parser_skip_attributes_opt (parser, 1);
25225 cp_token *tok = cp_lexer_peek_nth_token (parser->lexer, n);
25226 if (tok->type == CPP_SEMICOLON || tok->type == CPP_COMMA
25227 || tok->type == CPP_EQ || tok->type == CPP_OPEN_BRACE)
25228 break;
25230 type_specifier_seq->attributes
25231 = attr_chainon (type_specifier_seq->attributes,
25232 cp_parser_attributes_opt (parser));
25233 continue;
25236 /* record the token of the beginning of the type specifier seq,
25237 for error reporting purposes*/
25238 if (!start_token)
25239 start_token = cp_lexer_peek_token (parser->lexer);
25241 /* Look for the type-specifier. */
25242 type_specifier = cp_parser_type_specifier (parser,
25243 flags,
25244 type_specifier_seq,
25245 /*is_declaration=*/false,
25246 NULL,
25247 &is_cv_qualifier);
25248 if (!type_specifier)
25250 /* If the first type-specifier could not be found, this is not a
25251 type-specifier-seq at all. */
25252 if (!seen_type_specifier)
25254 /* Set in_declarator_p to avoid skipping to the semicolon. */
25255 int in_decl = parser->in_declarator_p;
25256 parser->in_declarator_p = true;
25258 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
25259 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
25260 cp_parser_error (parser, "expected type-specifier");
25262 parser->in_declarator_p = in_decl;
25264 type_specifier_seq->type = error_mark_node;
25265 return;
25267 /* If subsequent type-specifiers could not be found, the
25268 type-specifier-seq is complete. */
25269 break;
25272 seen_type_specifier = true;
25273 /* The standard says that a condition can be:
25275 type-specifier-seq declarator = assignment-expression
25277 However, given:
25279 struct S {};
25280 if (int S = ...)
25282 we should treat the "S" as a declarator, not as a
25283 type-specifier. The standard doesn't say that explicitly for
25284 type-specifier-seq, but it does say that for
25285 decl-specifier-seq in an ordinary declaration. Perhaps it
25286 would be clearer just to allow a decl-specifier-seq here, and
25287 then add a semantic restriction that if any decl-specifiers
25288 that are not type-specifiers appear, the program is invalid. */
25289 if (is_declaration && !is_cv_qualifier)
25290 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
25294 /* Return whether the function currently being declared has an associated
25295 template parameter list. */
25297 static bool
25298 function_being_declared_is_template_p (cp_parser* parser)
25300 if (!current_template_parms || processing_template_parmlist)
25301 return false;
25303 if (parser->implicit_template_scope)
25304 return true;
25306 if (at_class_scope_p ()
25307 && TYPE_BEING_DEFINED (current_class_type))
25308 return parser->num_template_parameter_lists != 0;
25310 return ((int) parser->num_template_parameter_lists > template_class_depth
25311 (current_class_type));
25314 /* Parse a parameter-declaration-clause.
25316 parameter-declaration-clause:
25317 parameter-declaration-list [opt] ... [opt]
25318 parameter-declaration-list , ...
25320 The parser flags FLAGS is used to control type-specifier parsing.
25322 Returns a representation for the parameter declarations. A return
25323 value of NULL indicates a parameter-declaration-clause consisting
25324 only of an ellipsis. */
25326 static tree
25327 cp_parser_parameter_declaration_clause (cp_parser* parser,
25328 cp_parser_flags flags)
25330 tree parameters;
25331 cp_token *token;
25332 bool ellipsis_p;
25334 auto cleanup = make_temp_override
25335 (parser->auto_is_implicit_function_template_parm_p);
25337 if (!processing_specialization
25338 && !processing_template_parmlist
25339 && !processing_explicit_instantiation
25340 /* default_arg_ok_p tracks whether this is a parameter-clause for an
25341 actual function or a random abstract declarator. */
25342 && parser->default_arg_ok_p)
25343 if (!current_function_decl
25344 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
25345 parser->auto_is_implicit_function_template_parm_p = true;
25347 /* Peek at the next token. */
25348 token = cp_lexer_peek_token (parser->lexer);
25349 /* Check for trivial parameter-declaration-clauses. */
25350 if (token->type == CPP_ELLIPSIS)
25352 /* Consume the `...' token. */
25353 cp_lexer_consume_token (parser->lexer);
25354 return NULL_TREE;
25356 else if (token->type == CPP_CLOSE_PAREN)
25357 /* There are no parameters. */
25358 return void_list_node;
25359 /* Check for `(void)', too, which is a special case. */
25360 else if (token->keyword == RID_VOID
25361 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
25362 == CPP_CLOSE_PAREN))
25364 /* Consume the `void' token. */
25365 cp_lexer_consume_token (parser->lexer);
25366 /* There are no parameters. */
25367 return explicit_void_list_node;
25370 /* A vector of parameters that haven't been pushed yet. */
25371 auto_vec<tree> pending_decls;
25373 /* Parse the parameter-declaration-list. */
25374 parameters = cp_parser_parameter_declaration_list (parser, flags,
25375 &pending_decls);
25376 /* If a parse error occurred while parsing the
25377 parameter-declaration-list, then the entire
25378 parameter-declaration-clause is erroneous. */
25379 if (parameters == error_mark_node)
25380 return NULL_TREE;
25382 /* Peek at the next token. */
25383 token = cp_lexer_peek_token (parser->lexer);
25384 /* If it's a `,', the clause should terminate with an ellipsis. */
25385 if (token->type == CPP_COMMA)
25387 /* Consume the `,'. */
25388 cp_lexer_consume_token (parser->lexer);
25389 /* Expect an ellipsis. */
25390 ellipsis_p
25391 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
25393 /* It might also be `...' if the optional trailing `,' was
25394 omitted. */
25395 else if (token->type == CPP_ELLIPSIS)
25397 /* Consume the `...' token. */
25398 cp_lexer_consume_token (parser->lexer);
25399 /* And remember that we saw it. */
25400 ellipsis_p = true;
25402 else
25403 ellipsis_p = false;
25405 /* A valid parameter-declaration-clause can only be followed by a ')'.
25406 So it's time to push all the parameters we have seen now that we
25407 know we have a valid declaration. Note that here we may not have
25408 committed yet, nor should we. Pushing here will detect the error
25409 of redefining a parameter. */
25410 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
25412 for (tree p : pending_decls)
25413 pushdecl (p);
25415 /* Delayed checking of auto parameters. */
25416 if (!parser->auto_is_implicit_function_template_parm_p
25417 && cxx_dialect >= cxx14)
25418 for (tree p = parameters; p; p = TREE_CHAIN (p))
25419 if (type_uses_auto (TREE_TYPE (TREE_VALUE (p))))
25421 error_at (location_of (TREE_VALUE (p)),
25422 "%<auto%> parameter not permitted in this context");
25423 TREE_TYPE (TREE_VALUE (p)) = error_mark_node;
25427 /* Finish the parameter list. */
25428 if (!ellipsis_p)
25429 parameters = chainon (parameters, void_list_node);
25431 return parameters;
25434 /* Parse a parameter-declaration-list.
25436 parameter-declaration-list:
25437 parameter-declaration
25438 parameter-declaration-list , parameter-declaration
25440 The parser flags FLAGS is used to control type-specifier parsing.
25441 PENDING_DECLS is a vector of parameters that haven't been pushed yet.
25443 Returns a representation of the parameter-declaration-list, as for
25444 cp_parser_parameter_declaration_clause. However, the
25445 `void_list_node' is never appended to the list. */
25447 static tree
25448 cp_parser_parameter_declaration_list (cp_parser* parser,
25449 cp_parser_flags flags,
25450 auto_vec<tree> *pending_decls)
25452 tree parameters = NULL_TREE;
25453 tree *tail = &parameters;
25454 bool saved_in_unbraced_linkage_specification_p;
25455 int index = 0;
25457 /* The special considerations that apply to a function within an
25458 unbraced linkage specifications do not apply to the parameters
25459 to the function. */
25460 saved_in_unbraced_linkage_specification_p
25461 = parser->in_unbraced_linkage_specification_p;
25462 parser->in_unbraced_linkage_specification_p = false;
25464 /* Look for more parameters. */
25465 while (true)
25467 cp_parameter_declarator *parameter;
25468 tree decl = error_mark_node;
25469 bool parenthesized_p = false;
25471 /* Parse the parameter. */
25472 parameter
25473 = cp_parser_parameter_declaration (parser, flags,
25474 /*template_parm_p=*/false,
25475 &parenthesized_p);
25477 /* We don't know yet if the enclosing context is unavailable or deprecated,
25478 so wait and deal with it in grokparms if appropriate. */
25479 deprecated_state = UNAVAILABLE_DEPRECATED_SUPPRESS;
25481 if (parameter && !cp_parser_error_occurred (parser))
25483 decl = grokdeclarator (parameter->declarator,
25484 &parameter->decl_specifiers,
25485 PARM,
25486 parameter->default_argument != NULL_TREE,
25487 &parameter->decl_specifiers.attributes);
25488 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
25489 DECL_SOURCE_LOCATION (decl) = parameter->loc;
25492 deprecated_state = DEPRECATED_NORMAL;
25494 /* If a parse error occurred parsing the parameter declaration,
25495 then the entire parameter-declaration-list is erroneous. */
25496 if (decl == error_mark_node)
25498 parameters = error_mark_node;
25499 break;
25502 if (parameter->decl_specifiers.attributes)
25503 cplus_decl_attributes (&decl,
25504 parameter->decl_specifiers.attributes,
25506 if (DECL_NAME (decl))
25508 /* We cannot always pushdecl while parsing tentatively because
25509 it may have side effects and we can't be sure yet if we're
25510 parsing a declaration, e.g.:
25512 S foo(int(x), int(x), int{x});
25514 where it's not clear if we're dealing with a constructor call
25515 or a function declaration until we've seen the last argument
25516 which breaks it up.
25517 It's safe to pushdecl so long as it doesn't result in a clash
25518 with an already-pushed parameter. But we don't delay pushing
25519 different parameters to handle
25521 S foo(int(i), decltype(i) j = 42);
25523 which is valid. */
25524 if (pending_decls
25525 && cp_parser_uncommitted_to_tentative_parse_p (parser)
25526 /* See if PARAMETERS already contains a parameter with the same
25527 DECL_NAME as DECL. */
25528 && [parameters, decl] {
25529 for (tree p = parameters; p; p = TREE_CHAIN (p))
25530 if (DECL_NAME (decl) == DECL_NAME (TREE_VALUE (p)))
25531 return true;
25532 return false;
25533 }())
25534 pending_decls->safe_push (decl);
25535 else
25536 decl = pushdecl (decl);
25539 if (decl != error_mark_node)
25541 retrofit_lang_decl (decl);
25542 DECL_PARM_INDEX (decl) = ++index;
25543 DECL_PARM_LEVEL (decl) = function_parm_depth ();
25546 /* Add the new parameter to the list. */
25547 *tail = build_tree_list (parameter->default_argument, decl);
25548 tail = &TREE_CHAIN (*tail);
25550 /* If the parameters were parenthesized, it's the case of
25551 T foo(X(x)) which looks like a variable definition but
25552 is a function declaration. */
25553 if (index == 1 || PARENTHESIZED_LIST_P (parameters))
25554 PARENTHESIZED_LIST_P (parameters) = parenthesized_p;
25556 /* Peek at the next token. */
25557 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
25558 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
25559 /* These are for Objective-C++ */
25560 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
25561 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25562 /* The parameter-declaration-list is complete. */
25563 break;
25564 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25566 cp_token *token;
25568 /* Peek at the next token. */
25569 token = cp_lexer_peek_nth_token (parser->lexer, 2);
25570 /* If it's an ellipsis, then the list is complete. */
25571 if (token->type == CPP_ELLIPSIS)
25572 break;
25573 /* Otherwise, there must be more parameters. Consume the
25574 `,'. */
25575 cp_lexer_consume_token (parser->lexer);
25576 /* When parsing something like:
25578 int i(float f, double d)
25580 we can tell after seeing the declaration for "f" that we
25581 are not looking at an initialization of a variable "i",
25582 but rather at the declaration of a function "i".
25584 Due to the fact that the parsing of template arguments
25585 (as specified to a template-id) requires backtracking we
25586 cannot use this technique when inside a template argument
25587 list. */
25588 if (!parser->in_template_argument_list_p
25589 && !parser->in_type_id_in_expr_p
25590 && cp_parser_uncommitted_to_tentative_parse_p (parser)
25591 /* However, a parameter-declaration of the form
25592 "float(f)" (which is a valid declaration of a
25593 parameter "f") can also be interpreted as an
25594 expression (the conversion of "f" to "float"). */
25595 && !parenthesized_p)
25596 cp_parser_commit_to_tentative_parse (parser);
25598 else
25600 cp_parser_error (parser, "expected %<,%> or %<...%>");
25601 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
25602 cp_parser_skip_to_closing_parenthesis (parser,
25603 /*recovering=*/true,
25604 /*or_comma=*/false,
25605 /*consume_paren=*/false);
25606 break;
25610 parser->in_unbraced_linkage_specification_p
25611 = saved_in_unbraced_linkage_specification_p;
25613 /* Reset implicit_template_scope if we are about to leave the function
25614 parameter list that introduced it. Note that for out-of-line member
25615 definitions, there will be one or more class scopes before we get to
25616 the template parameter scope. */
25618 if (cp_binding_level *its = parser->implicit_template_scope)
25619 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
25621 while (maybe_its->kind == sk_class)
25622 maybe_its = maybe_its->level_chain;
25623 if (maybe_its == its)
25625 parser->implicit_template_parms = 0;
25626 parser->implicit_template_scope = 0;
25630 return parameters;
25633 /* Parse a parameter declaration.
25635 parameter-declaration:
25636 decl-specifier-seq ... [opt] declarator
25637 decl-specifier-seq declarator = assignment-expression
25638 decl-specifier-seq ... [opt] abstract-declarator [opt]
25639 decl-specifier-seq abstract-declarator [opt] = assignment-expression
25641 The parser flags FLAGS is used to control type-specifier parsing.
25643 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
25644 declares a template parameter. (In that case, a non-nested `>'
25645 token encountered during the parsing of the assignment-expression
25646 is not interpreted as a greater-than operator.)
25648 Returns a representation of the parameter, or NULL if an error
25649 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
25650 true iff the declarator is of the form "(p)". */
25652 static cp_parameter_declarator *
25653 cp_parser_parameter_declaration (cp_parser *parser,
25654 cp_parser_flags flags,
25655 bool template_parm_p,
25656 bool *parenthesized_p)
25658 int declares_class_or_enum;
25659 cp_decl_specifier_seq decl_specifiers;
25660 cp_declarator *declarator;
25661 tree default_argument;
25662 cp_token *token = NULL, *declarator_token_start = NULL;
25663 const char *saved_message;
25664 bool template_parameter_pack_p = false;
25666 /* In a template parameter, `>' is not an operator.
25668 [temp.param]
25670 When parsing a default template-argument for a non-type
25671 template-parameter, the first non-nested `>' is taken as the end
25672 of the template parameter-list rather than a greater-than
25673 operator. */
25675 /* Type definitions may not appear in parameter types. */
25676 saved_message = parser->type_definition_forbidden_message;
25677 parser->type_definition_forbidden_message
25678 = G_("types may not be defined in parameter types");
25680 int template_parm_idx = (function_being_declared_is_template_p (parser) ?
25681 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
25682 (current_template_parms)) : 0);
25684 /* Parse the declaration-specifiers. */
25685 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
25686 cp_parser_decl_specifier_seq (parser,
25687 flags | CP_PARSER_FLAGS_PARAMETER,
25688 &decl_specifiers,
25689 &declares_class_or_enum);
25691 /* [dcl.spec.auto.general]: "A placeholder-type-specifier of the form
25692 type-constraint opt auto can be used as a decl-specifier of the
25693 decl-specifier-seq of a parameter-declaration of a function declaration
25694 or lambda-expression..." but we must not synthesize an implicit template
25695 type parameter in its declarator. That is, in "void f(auto[auto{10}]);"
25696 we want to synthesize only the first auto. */
25697 auto cleanup = make_temp_override
25698 (parser->auto_is_implicit_function_template_parm_p, false);
25700 /* Complain about missing 'typename' or other invalid type names. */
25701 if (!decl_specifiers.any_type_specifiers_p
25702 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
25703 decl_specifiers.type = error_mark_node;
25705 /* If an error occurred, there's no reason to attempt to parse the
25706 rest of the declaration. */
25707 if (cp_parser_error_occurred (parser))
25709 parser->type_definition_forbidden_message = saved_message;
25710 return NULL;
25713 /* Peek at the next token. */
25714 token = cp_lexer_peek_token (parser->lexer);
25716 /* If the next token is a `)', `,', `=', `>', or `...', then there
25717 is no declarator. However, when variadic templates are enabled,
25718 there may be a declarator following `...'. */
25719 if (token->type == CPP_CLOSE_PAREN
25720 || token->type == CPP_COMMA
25721 || token->type == CPP_EQ
25722 || token->type == CPP_GREATER)
25724 declarator = NULL;
25725 if (parenthesized_p)
25726 *parenthesized_p = false;
25728 /* Otherwise, there should be a declarator. */
25729 else
25731 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
25732 parser->default_arg_ok_p = false;
25734 /* After seeing a decl-specifier-seq, if the next token is not a
25735 "(" or "{", there is no possibility that the code is a valid
25736 expression. Therefore, if parsing tentatively, we commit at
25737 this point. */
25738 if (!parser->in_template_argument_list_p
25739 /* In an expression context, having seen:
25741 (int((char ...
25743 we cannot be sure whether we are looking at a
25744 function-type (taking a "char" as a parameter) or a cast
25745 of some object of type "char" to "int". */
25746 && !parser->in_type_id_in_expr_p
25747 && cp_parser_uncommitted_to_tentative_parse_p (parser)
25748 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
25750 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25752 if (decl_specifiers.type
25753 && template_placeholder_p (decl_specifiers.type))
25754 /* This is a CTAD expression, not a parameter declaration. */
25755 cp_parser_simulate_error (parser);
25757 else
25758 cp_parser_commit_to_tentative_parse (parser);
25760 /* Parse the declarator. */
25761 declarator_token_start = token;
25762 declarator = cp_parser_declarator (parser,
25763 CP_PARSER_DECLARATOR_EITHER,
25764 CP_PARSER_FLAGS_NONE,
25765 /*ctor_dtor_or_conv_p=*/NULL,
25766 parenthesized_p,
25767 /*member_p=*/false,
25768 /*friend_p=*/false,
25769 /*static_p=*/false);
25770 parser->default_arg_ok_p = saved_default_arg_ok_p;
25771 /* After the declarator, allow more attributes. */
25772 decl_specifiers.attributes
25773 = attr_chainon (decl_specifiers.attributes,
25774 cp_parser_attributes_opt (parser));
25776 /* If the declarator is a template parameter pack, remember that and
25777 clear the flag in the declarator itself so we don't get errors
25778 from grokdeclarator. */
25779 if (template_parm_p && declarator && declarator->parameter_pack_p)
25781 declarator->parameter_pack_p = false;
25782 template_parameter_pack_p = true;
25786 /* If the next token is an ellipsis, and we have not seen a declarator
25787 name, and if either the type of the declarator contains parameter
25788 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
25789 for, eg, abbreviated integral type names), then we actually have a
25790 parameter pack expansion expression. Otherwise, leave the ellipsis
25791 for a C-style variadic function. */
25792 token = cp_lexer_peek_token (parser->lexer);
25794 bool xobj_param_p
25795 = decl_spec_seq_has_spec_p (&decl_specifiers, ds_this);
25796 if (xobj_param_p && template_parm_p)
25798 error_at (decl_specifiers.locations[ds_this],
25799 "%<this%> specifier in template parameter declaration");
25800 xobj_param_p = false;
25801 decl_specifiers.locations[ds_this] = 0;
25804 /* If a function parameter pack was specified and an implicit template
25805 parameter was introduced during cp_parser_parameter_declaration,
25806 change any implicit parameters introduced into packs. */
25807 if (parser->implicit_template_parms
25808 && ((token->type == CPP_ELLIPSIS
25809 && declarator_can_be_parameter_pack (declarator))
25810 || (declarator && declarator->parameter_pack_p)))
25812 int latest_template_parm_idx = TREE_VEC_LENGTH
25813 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
25815 if (latest_template_parm_idx != template_parm_idx)
25816 decl_specifiers.type
25817 = convert_generic_types_to_packs (decl_specifiers.type,
25818 template_parm_idx,
25819 latest_template_parm_idx);
25822 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25824 tree type = decl_specifiers.type;
25826 if (type && DECL_P (type))
25827 type = TREE_TYPE (type);
25829 if (((type
25830 && TREE_CODE (type) != TYPE_PACK_EXPANSION
25831 && (template_parm_p || uses_parameter_packs (type)))
25832 || (!type && template_parm_p))
25833 && declarator_can_be_parameter_pack (declarator))
25835 /* Consume the `...'. */
25836 cp_lexer_consume_token (parser->lexer);
25837 maybe_warn_variadic_templates ();
25839 /* Build a pack expansion type */
25840 if (template_parm_p)
25841 template_parameter_pack_p = true;
25842 else if (declarator)
25843 declarator->parameter_pack_p = true;
25844 else
25845 decl_specifiers.type = make_pack_expansion (type);
25849 if (xobj_param_p
25850 && ((declarator && declarator->parameter_pack_p)
25851 || (decl_specifiers.type
25852 && PACK_EXPANSION_P (decl_specifiers.type))))
25854 location_t xobj_param
25855 = make_location (decl_specifiers.locations[ds_this],
25856 decl_spec_token_start->location,
25857 input_location);
25858 error_at (xobj_param,
25859 "an explicit object parameter cannot "
25860 "be a function parameter pack");
25861 xobj_param_p = false;
25862 decl_specifiers.locations[ds_this] = 0;
25865 /* The restriction on defining new types applies only to the type
25866 of the parameter, not to the default argument. */
25867 parser->type_definition_forbidden_message = saved_message;
25868 cp_token *eq_token = NULL;
25869 /* If the next token is `=', then process a default argument. */
25870 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
25872 tree type = decl_specifiers.type;
25873 token = cp_lexer_peek_token (parser->lexer);
25874 /* Used for diagnostics with an xobj parameter. */
25875 eq_token = token;
25876 if (declarator)
25877 declarator->init_loc = token->location;
25878 /* If we are defining a class, then the tokens that make up the
25879 default argument must be saved and processed later. */
25880 if (!template_parm_p && at_class_scope_p ()
25881 && TYPE_BEING_DEFINED (current_class_type)
25882 && !LAMBDA_TYPE_P (current_class_type))
25883 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
25885 /* A constrained-type-specifier may declare a type
25886 template-parameter. */
25887 else if (declares_constrained_type_template_parameter (type))
25888 default_argument
25889 = cp_parser_default_type_template_argument (parser);
25891 /* A constrained-type-specifier may declare a
25892 template-template-parameter. */
25893 else if (declares_constrained_template_template_parameter (type))
25894 default_argument
25895 = cp_parser_default_template_template_argument (parser);
25897 /* Outside of a class definition, we can just parse the
25898 assignment-expression. */
25899 else
25900 default_argument
25901 = cp_parser_default_argument (parser, template_parm_p);
25903 if (!parser->default_arg_ok_p)
25905 permerror (token->location,
25906 "default arguments are only "
25907 "permitted for function parameters");
25909 else if ((declarator && declarator->parameter_pack_p)
25910 || template_parameter_pack_p
25911 || (decl_specifiers.type
25912 && PACK_EXPANSION_P (decl_specifiers.type)))
25914 /* Find the name of the parameter pack. */
25915 cp_declarator *id_declarator = declarator;
25916 while (id_declarator && id_declarator->kind != cdk_id)
25917 id_declarator = id_declarator->declarator;
25919 if (id_declarator && id_declarator->kind == cdk_id)
25920 error_at (declarator_token_start->location,
25921 template_parm_p
25922 ? G_("template parameter pack %qD "
25923 "cannot have a default argument")
25924 : G_("parameter pack %qD cannot have "
25925 "a default argument"),
25926 id_declarator->u.id.unqualified_name);
25927 else
25928 error_at (declarator_token_start->location,
25929 template_parm_p
25930 ? G_("template parameter pack cannot have "
25931 "a default argument")
25932 : G_("parameter pack cannot have a "
25933 "default argument"));
25935 default_argument = NULL_TREE;
25938 else
25939 default_argument = NULL_TREE;
25941 if (default_argument)
25942 STRIP_ANY_LOCATION_WRAPPER (default_argument);
25944 if (xobj_param_p)
25946 if (default_argument)
25948 /* If there is a default_argument, eq_token should always be set. */
25949 gcc_assert (eq_token);
25950 location_t param_with_init_loc
25951 = make_location (eq_token->location,
25952 decl_spec_token_start->location,
25953 input_location);
25954 error_at (param_with_init_loc,
25955 "an explicit object parameter "
25956 "may not have a default argument");
25958 /* Xobj parameters can not have default arguments, thus
25959 we can reuse the default argument field to flag the param as such. */
25960 default_argument = this_identifier;
25963 /* Generate a location for the parameter, ranging from the start of the
25964 initial token to the end of the final token (using input_location for
25965 the latter, set up by cp_lexer_set_source_position_from_token when
25966 consuming tokens).
25968 If we have a identifier, then use it for the caret location, e.g.
25970 extern int callee (int one, int (*two)(int, int), float three);
25971 ~~~~~~^~~~~~~~~~~~~~
25973 otherwise, reuse the start location for the caret location e.g.:
25975 extern int callee (int one, int (*)(int, int), float three);
25976 ^~~~~~~~~~~~~~~~~
25979 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
25980 ? declarator->id_loc
25981 : decl_spec_token_start->location);
25982 location_t param_loc = make_location (caret_loc,
25983 decl_spec_token_start->location,
25984 input_location);
25986 return make_parameter_declarator (&decl_specifiers,
25987 declarator,
25988 default_argument,
25989 param_loc,
25990 template_parameter_pack_p);
25993 /* Parse a default argument and return it.
25995 TEMPLATE_PARM_P is true if this is a default argument for a
25996 non-type template parameter. */
25997 static tree
25998 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
26000 tree default_argument = NULL_TREE;
26001 bool saved_greater_than_is_operator_p;
26002 unsigned char saved_local_variables_forbidden_p;
26004 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
26005 set correctly. */
26006 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
26007 parser->greater_than_is_operator_p = !template_parm_p;
26008 auto odsd = make_temp_override (parser->omp_declare_simd, NULL);
26009 auto ord = make_temp_override (parser->oacc_routine, NULL);
26010 auto oafp = make_temp_override (parser->omp_attrs_forbidden_p, false);
26012 /* Local variable names (and the `this' keyword) may not
26013 appear in a default argument. */
26014 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
26015 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
26016 /* Parse the assignment-expression. */
26017 if (template_parm_p)
26018 push_deferring_access_checks (dk_no_deferred);
26019 tree saved_class_ptr = NULL_TREE;
26020 tree saved_class_ref = NULL_TREE;
26021 /* The "this" pointer is not valid in a default argument. */
26022 if (cfun)
26024 saved_class_ptr = current_class_ptr;
26025 cp_function_chain->x_current_class_ptr = NULL_TREE;
26026 saved_class_ref = current_class_ref;
26027 cp_function_chain->x_current_class_ref = NULL_TREE;
26029 default_argument = cp_parser_initializer (parser);
26030 /* Restore the "this" pointer. */
26031 if (cfun)
26033 cp_function_chain->x_current_class_ptr = saved_class_ptr;
26034 cp_function_chain->x_current_class_ref = saved_class_ref;
26036 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
26037 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
26038 if (template_parm_p)
26039 pop_deferring_access_checks ();
26040 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
26041 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
26043 return default_argument;
26046 /* Parse a function-body.
26048 function-body:
26049 compound_statement */
26051 static void
26052 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
26054 cp_parser_compound_statement (parser, NULL, (in_function_try_block
26055 ? BCS_TRY_BLOCK : BCS_NORMAL),
26056 true);
26059 /* Parse a ctor-initializer-opt followed by a function-body. Return
26060 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
26061 is true we are parsing a function-try-block. */
26063 static void
26064 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
26065 bool in_function_try_block)
26067 tree body, list;
26068 const bool check_body_p
26069 = (DECL_CONSTRUCTOR_P (current_function_decl)
26070 && DECL_DECLARED_CONSTEXPR_P (current_function_decl));
26071 tree last = NULL;
26073 if (in_function_try_block
26074 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
26075 && cxx_dialect < cxx20)
26077 if (DECL_CONSTRUCTOR_P (current_function_decl))
26078 pedwarn (input_location, OPT_Wc__20_extensions,
26079 "function-try-block body of %<constexpr%> constructor only "
26080 "available with %<-std=c++20%> or %<-std=gnu++20%>");
26081 else
26082 pedwarn (input_location, OPT_Wc__20_extensions,
26083 "function-try-block body of %<constexpr%> function only "
26084 "available with %<-std=c++20%> or %<-std=gnu++20%>");
26087 /* Begin the function body. */
26088 body = begin_function_body ();
26089 /* Parse the optional ctor-initializer. */
26090 cp_parser_ctor_initializer_opt (parser);
26092 /* If we're parsing a constexpr constructor definition, we need
26093 to check that the constructor body is indeed empty. However,
26094 before we get to cp_parser_function_body lot of junk has been
26095 generated, so we can't just check that we have an empty block.
26096 Rather we take a snapshot of the outermost block, and check whether
26097 cp_parser_function_body changed its state. */
26098 if (check_body_p)
26100 list = cur_stmt_list;
26101 if (STATEMENT_LIST_TAIL (list))
26102 last = STATEMENT_LIST_TAIL (list)->stmt;
26104 /* Parse the function-body. */
26105 cp_parser_function_body (parser, in_function_try_block);
26106 if (check_body_p)
26107 check_constexpr_ctor_body (last, list, /*complain=*/true);
26108 /* Finish the function body. */
26109 finish_function_body (body);
26112 /* Parse an initializer.
26114 initializer:
26115 = initializer-clause
26116 ( expression-list )
26118 Returns an expression representing the initializer. If no
26119 initializer is present, NULL_TREE is returned.
26121 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
26122 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
26123 set to TRUE if there is no initializer present. If there is an
26124 initializer, and it is not a constant-expression, *NON_CONSTANT_P
26125 is set to true; otherwise it is set to false. */
26127 static tree
26128 cp_parser_initializer (cp_parser *parser, bool *is_direct_init /*=nullptr*/,
26129 bool *non_constant_p /*=nullptr*/, bool subexpression_p)
26131 cp_token *token;
26132 tree init;
26134 /* Peek at the next token. */
26135 token = cp_lexer_peek_token (parser->lexer);
26137 /* Let our caller know whether or not this initializer was
26138 parenthesized. */
26139 if (is_direct_init)
26140 *is_direct_init = (token->type != CPP_EQ);
26141 /* Assume that the initializer is constant. */
26142 if (non_constant_p)
26143 *non_constant_p = false;
26145 if (token->type == CPP_EQ)
26147 /* Consume the `='. */
26148 cp_lexer_consume_token (parser->lexer);
26149 /* Parse the initializer-clause. */
26150 init = cp_parser_initializer_clause (parser, non_constant_p);
26152 else if (token->type == CPP_OPEN_PAREN)
26154 vec<tree, va_gc> *vec;
26155 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
26156 /*cast_p=*/false,
26157 /*allow_expansion_p=*/true,
26158 non_constant_p);
26159 if (vec == NULL)
26160 return error_mark_node;
26161 init = build_tree_list_vec (vec);
26162 release_tree_vector (vec);
26164 else if (token->type == CPP_OPEN_BRACE)
26166 cp_lexer_set_source_position (parser->lexer);
26167 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
26168 init = cp_parser_braced_list (parser, non_constant_p);
26169 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
26171 else
26173 /* Anything else is an error. */
26174 cp_parser_error (parser, "expected initializer");
26175 init = error_mark_node;
26178 if (!subexpression_p && check_for_bare_parameter_packs (init))
26179 init = error_mark_node;
26181 return init;
26184 /* Parse an initializer-clause.
26186 initializer-clause:
26187 assignment-expression
26188 braced-init-list
26190 Returns an expression representing the initializer.
26192 If the `assignment-expression' production is used the value
26193 returned is simply a representation for the expression.
26195 Otherwise, calls cp_parser_braced_list. */
26197 static cp_expr
26198 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
26200 cp_expr initializer;
26202 /* Assume the expression is constant. */
26203 if (non_constant_p)
26204 *non_constant_p = false;
26206 /* If it is not a `{', then we are looking at an
26207 assignment-expression. */
26208 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
26210 initializer
26211 = cp_parser_constant_expression (parser,
26212 /*allow_non_constant_p=*/2,
26213 non_constant_p);
26215 else
26216 initializer = cp_parser_braced_list (parser, non_constant_p);
26218 return initializer;
26221 /* Parse a brace-enclosed initializer list.
26223 braced-init-list:
26224 { initializer-list , [opt] }
26225 { designated-initializer-list , [opt] }
26228 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
26229 the elements of the initializer-list (or NULL, if the last
26230 production is used). The TREE_TYPE for the CONSTRUCTOR will be
26231 NULL_TREE. There is no way to detect whether or not the optional
26232 trailing `,' was provided. NON_CONSTANT_P is as for
26233 cp_parser_initializer. */
26235 static cp_expr
26236 cp_parser_braced_list (cp_parser *parser, bool *non_constant_p /*=nullptr*/)
26238 tree initializer;
26239 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
26240 auto oas = make_temp_override (parser->omp_array_section_p, false);
26242 /* Consume the `{' token. */
26243 matching_braces braces;
26244 braces.require_open (parser);
26245 /* Create a CONSTRUCTOR to represent the braced-initializer. */
26246 initializer = make_node (CONSTRUCTOR);
26247 /* If it's not a `}', then there is a non-trivial initializer. */
26248 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
26250 bool designated;
26251 /* Parse the initializer list. */
26252 CONSTRUCTOR_ELTS (initializer)
26253 = cp_parser_initializer_list (parser, non_constant_p, &designated);
26254 CONSTRUCTOR_IS_DESIGNATED_INIT (initializer) = designated;
26255 /* A trailing `,' token is allowed. */
26256 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26257 cp_lexer_consume_token (parser->lexer);
26259 else if (non_constant_p)
26260 *non_constant_p = false;
26261 /* Now, there should be a trailing `}'. */
26262 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
26263 braces.require_close (parser);
26264 TREE_TYPE (initializer) = init_list_type_node;
26265 recompute_constructor_flags (initializer);
26267 cp_expr result (initializer);
26268 /* Build a location of the form:
26269 { ... }
26270 ^~~~~~~
26271 with caret==start at the open brace, finish at the close brace. */
26272 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
26273 result.set_location (combined_loc);
26274 return result;
26277 /* Consume tokens up to, but not including, the next non-nested closing `]'.
26278 Returns true iff we found a closing `]'. */
26280 static bool
26281 cp_parser_skip_up_to_closing_square_bracket (cp_parser *parser)
26283 unsigned square_depth = 0;
26285 while (true)
26287 cp_token * token = cp_lexer_peek_token (parser->lexer);
26289 switch (token->type)
26291 case CPP_PRAGMA_EOL:
26292 if (!parser->lexer->in_pragma)
26293 break;
26294 /* FALLTHRU */
26296 case CPP_EOF:
26297 /* If we've run out of tokens, then there is no closing `]'. */
26298 return false;
26300 case CPP_OPEN_SQUARE:
26301 ++square_depth;
26302 break;
26304 case CPP_CLOSE_SQUARE:
26305 if (!square_depth--)
26306 return true;
26307 break;
26309 default:
26310 break;
26313 /* Consume the current token, skipping it. */
26314 cp_lexer_consume_token (parser->lexer);
26318 /* Consume tokens up to, and including, the next non-nested closing `]'.
26319 Returns true iff we found a closing `]'. */
26321 static bool
26322 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
26324 bool found = cp_parser_skip_up_to_closing_square_bracket (parser);
26325 if (found)
26326 cp_lexer_consume_token (parser->lexer);
26327 return found;
26330 /* Return true if we are looking at an array-designator, false otherwise. */
26332 static bool
26333 cp_parser_array_designator_p (cp_parser *parser)
26335 /* Consume the `['. */
26336 cp_lexer_consume_token (parser->lexer);
26338 cp_lexer_save_tokens (parser->lexer);
26340 /* Skip tokens until the next token is a closing square bracket.
26341 If we find the closing `]', and the next token is a `=', then
26342 we are looking at an array designator. */
26343 bool array_designator_p
26344 = (cp_parser_skip_to_closing_square_bracket (parser)
26345 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
26347 /* Roll back the tokens we skipped. */
26348 cp_lexer_rollback_tokens (parser->lexer);
26350 return array_designator_p;
26353 /* Parse an initializer-list.
26355 initializer-list:
26356 initializer-clause ... [opt]
26357 initializer-list , initializer-clause ... [opt]
26359 C++20 Extension:
26361 designated-initializer-list:
26362 designated-initializer-clause
26363 designated-initializer-list , designated-initializer-clause
26365 designated-initializer-clause:
26366 designator brace-or-equal-initializer
26368 designator:
26369 . identifier
26371 GNU Extension:
26373 initializer-list:
26374 designation initializer-clause ...[opt]
26375 initializer-list , designation initializer-clause ...[opt]
26377 designation:
26378 . identifier =
26379 identifier :
26380 [ constant-expression ] =
26382 Returns a vec of constructor_elt. The VALUE of each elt is an expression
26383 for the initializer. If the INDEX of the elt is non-NULL, it is the
26384 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
26385 as for cp_parser_initializer. Set *DESIGNATED to a boolean whether there
26386 are any designators. */
26388 static vec<constructor_elt, va_gc> *
26389 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p,
26390 bool *designated)
26392 vec<constructor_elt, va_gc> *v = NULL;
26393 bool first_p = true;
26394 tree first_designator = NULL_TREE;
26396 /* Assume all of the expressions are constant. */
26397 if (non_constant_p)
26398 *non_constant_p = false;
26400 unsigned nelts = 0;
26401 int suppress = suppress_location_wrappers;
26403 /* Parse the rest of the list. */
26404 while (true)
26406 cp_token *token;
26407 tree designator;
26408 tree initializer;
26409 bool clause_non_constant_p;
26410 bool direct_p = false;
26411 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26413 /* Handle the C++20 syntax, '. id ='. */
26414 if ((cxx_dialect >= cxx20
26415 || cp_parser_allow_gnu_extensions_p (parser))
26416 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
26417 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
26418 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
26419 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
26420 == CPP_OPEN_BRACE)))
26422 if (pedantic && cxx_dialect < cxx20)
26423 pedwarn (loc, OPT_Wc__20_extensions,
26424 "C++ designated initializers only available with "
26425 "%<-std=c++20%> or %<-std=gnu++20%>");
26426 /* Consume the `.'. */
26427 cp_lexer_consume_token (parser->lexer);
26428 /* Consume the identifier. */
26429 designator = cp_lexer_consume_token (parser->lexer)->u.value;
26430 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
26431 /* Consume the `='. */
26432 cp_lexer_consume_token (parser->lexer);
26433 else
26434 direct_p = true;
26436 /* Also, if the next token is an identifier and the following one is a
26437 colon, we are looking at the GNU designated-initializer
26438 syntax. */
26439 else if (cp_parser_allow_gnu_extensions_p (parser)
26440 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
26441 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
26442 == CPP_COLON))
26444 /* Warn the user that they are using an extension. */
26445 pedwarn (loc, OPT_Wpedantic,
26446 "ISO C++ does not allow GNU designated initializers");
26447 /* Consume the identifier. */
26448 designator = cp_lexer_consume_token (parser->lexer)->u.value;
26449 /* Consume the `:'. */
26450 cp_lexer_consume_token (parser->lexer);
26452 /* Also handle C99 array designators, '[ const ] ='. */
26453 else if (cp_parser_allow_gnu_extensions_p (parser)
26454 && !c_dialect_objc ()
26455 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
26457 /* In C++11, [ could start a lambda-introducer. */
26458 bool non_const = false;
26460 cp_parser_parse_tentatively (parser);
26462 if (!cp_parser_array_designator_p (parser))
26464 cp_parser_simulate_error (parser);
26465 designator = NULL_TREE;
26467 else
26469 designator = cp_parser_constant_expression (parser, true,
26470 &non_const);
26471 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
26472 cp_parser_require (parser, CPP_EQ, RT_EQ);
26475 if (!cp_parser_parse_definitely (parser))
26476 designator = NULL_TREE;
26477 else if (non_const
26478 && (!require_potential_rvalue_constant_expression
26479 (designator)))
26480 designator = NULL_TREE;
26481 if (designator)
26482 /* Warn the user that they are using an extension. */
26483 pedwarn (loc, OPT_Wpedantic,
26484 "ISO C++ does not allow C99 designated initializers");
26486 else
26487 designator = NULL_TREE;
26489 if (first_p)
26491 first_designator = designator;
26492 first_p = false;
26494 else if (cxx_dialect >= cxx20
26495 && first_designator != error_mark_node
26496 && (!first_designator != !designator))
26498 error_at (loc, "either all initializer clauses should be designated "
26499 "or none of them should be");
26500 first_designator = error_mark_node;
26502 else if (cxx_dialect < cxx20 && !first_designator)
26503 first_designator = designator;
26505 /* Parse the initializer. */
26506 initializer = cp_parser_initializer_clause (parser,
26507 (non_constant_p != nullptr
26508 ? &clause_non_constant_p
26509 : nullptr));
26510 /* If any clause is non-constant, so is the entire initializer. */
26511 if (non_constant_p && clause_non_constant_p)
26512 *non_constant_p = true;
26514 if (TREE_CODE (initializer) == CONSTRUCTOR)
26515 /* This uses |= rather than = because C_I_D_I could have been set in
26516 cp_parser_functional_cast so we must be careful not to clear the
26517 flag. */
26518 CONSTRUCTOR_IS_DIRECT_INIT (initializer) |= direct_p;
26520 /* If we have an ellipsis, this is an initializer pack
26521 expansion. */
26522 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26524 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26526 /* Consume the `...'. */
26527 cp_lexer_consume_token (parser->lexer);
26529 if (designator && cxx_dialect >= cxx20)
26530 error_at (loc,
26531 "%<...%> not allowed in designated initializer list");
26533 /* Turn the initializer into an initializer expansion. */
26534 initializer = make_pack_expansion (initializer);
26537 /* Add it to the vector. */
26538 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
26540 /* If the next token is not a comma, we have reached the end of
26541 the list. */
26542 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
26543 break;
26545 /* Peek at the next token. */
26546 token = cp_lexer_peek_nth_token (parser->lexer, 2);
26547 /* If the next token is a `}', then we're still done. An
26548 initializer-clause can have a trailing `,' after the
26549 initializer-list and before the closing `}'. */
26550 if (token->type == CPP_CLOSE_BRACE)
26551 break;
26553 /* Suppress location wrappers in a long initializer to save memory
26554 (14179). The cutoff is chosen arbitrarily. */
26555 const unsigned loc_max = 256;
26556 unsigned incr = 1;
26557 if (TREE_CODE (initializer) == CONSTRUCTOR)
26558 /* Look one level down because it's easy. Looking deeper would require
26559 passing down a nelts pointer, and I don't think multi-level massive
26560 initializers are common enough to justify this. */
26561 incr = CONSTRUCTOR_NELTS (initializer);
26562 nelts += incr;
26563 if (nelts >= loc_max && (nelts - incr) < loc_max)
26564 ++suppress_location_wrappers;
26566 /* Consume the `,' token. */
26567 cp_lexer_consume_token (parser->lexer);
26570 /* The same identifier shall not appear in multiple designators
26571 of a designated-initializer-list. */
26572 if (first_designator)
26574 unsigned int i;
26575 tree designator, val;
26576 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
26577 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
26579 if (IDENTIFIER_MARKED (designator))
26581 error_at (cp_expr_loc_or_input_loc (val),
26582 "%<.%s%> designator used multiple times in "
26583 "the same initializer list",
26584 IDENTIFIER_POINTER (designator));
26585 (*v)[i].index = error_mark_node;
26587 else
26588 IDENTIFIER_MARKED (designator) = 1;
26590 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
26591 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
26592 IDENTIFIER_MARKED (designator) = 0;
26595 suppress_location_wrappers = suppress;
26597 *designated = first_designator != NULL_TREE;
26598 return v;
26601 /* Classes [gram.class] */
26603 /* Parse a class-name.
26605 class-name:
26606 identifier
26607 template-id
26609 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
26610 to indicate that names looked up in dependent types should be
26611 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
26612 keyword has been used to indicate that the name that appears next
26613 is a template. TAG_TYPE indicates the explicit tag given before
26614 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
26615 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
26616 is the class being defined in a class-head. If ENUM_OK is TRUE,
26617 enum-names are also accepted.
26619 Returns the TYPE_DECL representing the class. */
26621 static tree
26622 cp_parser_class_name (cp_parser *parser,
26623 bool typename_keyword_p,
26624 bool template_keyword_p,
26625 enum tag_types tag_type,
26626 bool check_dependency_p,
26627 bool class_head_p,
26628 bool is_declaration,
26629 bool enum_ok)
26631 tree decl;
26632 tree identifier = NULL_TREE;
26634 /* All class-names start with an identifier. */
26635 cp_token *token = cp_lexer_peek_token (parser->lexer);
26636 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
26638 cp_parser_error (parser, "expected class-name");
26639 return error_mark_node;
26642 /* PARSER->SCOPE can be cleared when parsing the template-arguments
26643 to a template-id, so we save it here. Consider object scope too,
26644 so that make_typename_type below can use it (cp_parser_template_name
26645 considers object scope also). This may happen with code like
26647 p->template A<T>::a()
26649 where we first want to look up A<T>::a in the class of the object
26650 expression, as per [basic.lookup.classref]. */
26651 tree scope = parser->scope ? parser->scope : parser->context->object_type;
26652 /* This only checks parser->scope to avoid duplicate errors; if
26653 ->object_type is erroneous, go on to give a parse error. */
26654 if (parser->scope == error_mark_node)
26655 return error_mark_node;
26657 /* Any name names a type if we're following the `typename' keyword
26658 in a qualified name where the enclosing scope is type-dependent. */
26659 const bool typename_p = (typename_keyword_p
26660 && parser->scope
26661 && TYPE_P (parser->scope)
26662 && dependent_scope_p (parser->scope));
26663 /* Handle the common case (an identifier, but not a template-id)
26664 efficiently. */
26665 if (token->type == CPP_NAME
26666 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
26668 cp_token *identifier_token;
26669 bool ambiguous_p;
26671 /* Look for the identifier. */
26672 identifier_token = cp_lexer_peek_token (parser->lexer);
26673 ambiguous_p = identifier_token->error_reported;
26674 identifier = cp_parser_identifier (parser);
26675 /* If the next token isn't an identifier, we are certainly not
26676 looking at a class-name. */
26677 if (identifier == error_mark_node)
26678 decl = error_mark_node;
26679 /* If we know this is a type-name, there's no need to look it
26680 up. */
26681 else if (typename_p)
26682 decl = identifier;
26683 else
26685 tree ambiguous_decls;
26686 /* If we already know that this lookup is ambiguous, then
26687 we've already issued an error message; there's no reason
26688 to check again. */
26689 if (ambiguous_p)
26691 cp_parser_simulate_error (parser);
26692 return error_mark_node;
26694 /* If the next token is a `::', then the name must be a type
26695 name.
26697 [basic.lookup.qual]
26699 During the lookup for a name preceding the :: scope
26700 resolution operator, object, function, and enumerator
26701 names are ignored. */
26702 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
26703 tag_type = scope_type;
26704 /* Look up the name. */
26705 decl = cp_parser_lookup_name (parser, identifier,
26706 tag_type,
26707 /*is_template=*/false,
26708 /*is_namespace=*/false,
26709 check_dependency_p,
26710 &ambiguous_decls,
26711 identifier_token->location);
26712 if (ambiguous_decls)
26714 if (cp_parser_parsing_tentatively (parser))
26715 cp_parser_simulate_error (parser);
26716 return error_mark_node;
26720 else
26722 /* Try a template-id. */
26723 decl = cp_parser_template_id (parser, template_keyword_p,
26724 check_dependency_p,
26725 tag_type,
26726 is_declaration);
26727 if (decl == error_mark_node)
26728 return error_mark_node;
26731 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
26733 /* If this is a typename, create a TYPENAME_TYPE. */
26734 if (typename_p && decl != error_mark_node)
26736 decl = make_typename_type (scope, decl, typename_type,
26737 /*complain=*/tf_error);
26738 if (decl != error_mark_node)
26739 decl = TYPE_NAME (decl);
26742 decl = strip_using_decl (decl);
26744 /* Check to see that it is really the name of a class. */
26745 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
26746 && identifier_p (TREE_OPERAND (decl, 0))
26747 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
26748 /* Situations like this:
26750 template <typename T> struct A {
26751 typename T::template X<int>::I i;
26754 are problematic. Is `T::template X<int>' a class-name? The
26755 standard does not seem to be definitive, but there is no other
26756 valid interpretation of the following `::'. Therefore, those
26757 names are considered class-names. */
26759 decl = make_typename_type (scope, decl, tag_type, tf_error);
26760 if (decl != error_mark_node)
26761 decl = TYPE_NAME (decl);
26763 else if (TREE_CODE (decl) != TYPE_DECL
26764 || TREE_TYPE (decl) == error_mark_node
26765 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
26766 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
26767 /* In Objective-C 2.0, a classname followed by '.' starts a
26768 dot-syntax expression, and it's not a type-name. */
26769 || (c_dialect_objc ()
26770 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
26771 && objc_is_class_name (decl)))
26772 decl = error_mark_node;
26774 if (decl == error_mark_node)
26775 cp_parser_error (parser, "expected class-name");
26776 else if (identifier && !parser->scope)
26777 maybe_note_name_used_in_class (identifier, decl);
26779 return decl;
26782 /* Make sure that any member-function parameters are in scope.
26783 For instance, a function's noexcept-specifier can use the function's
26784 parameters:
26786 struct S {
26787 void fn (int p) noexcept(noexcept(p));
26790 so we need to make sure name lookup can find them. This is used
26791 when we delay parsing of the noexcept-specifier. */
26793 static void
26794 inject_parm_decls (tree decl)
26796 begin_scope (sk_function_parms, decl);
26797 tree args = DECL_ARGUMENTS (decl);
26799 do_push_parm_decls (decl, args, /*nonparms=*/NULL);
26801 if (args && is_this_parameter (args))
26803 gcc_checking_assert (current_class_ptr == NULL_TREE);
26804 current_class_ref = cp_build_fold_indirect_ref (args);
26805 current_class_ptr = args;
26809 /* Undo the effects of inject_parm_decls. */
26811 static void
26812 pop_injected_parms (void)
26814 pop_bindings_and_leave_scope ();
26815 current_class_ptr = current_class_ref = NULL_TREE;
26818 /* Parse a class-specifier.
26820 class-specifier:
26821 class-head { member-specification [opt] }
26823 Returns the TREE_TYPE representing the class. */
26825 tree
26826 cp_parser_class_specifier (cp_parser* parser)
26828 auto_timevar tv (TV_PARSE_STRUCT);
26830 tree type;
26831 tree attributes = NULL_TREE;
26832 bool nested_name_specifier_p;
26833 unsigned saved_num_template_parameter_lists;
26834 bool saved_in_function_body;
26835 unsigned char in_statement;
26836 bool in_switch_statement_p;
26837 bool saved_in_unbraced_linkage_specification_p;
26838 bool saved_in_unbraced_export_declaration_p;
26839 tree old_scope = NULL_TREE;
26840 tree scope = NULL_TREE;
26841 cp_token *closing_brace;
26843 push_deferring_access_checks (dk_no_deferred);
26845 /* Parse the class-head. */
26846 type = cp_parser_class_head (parser,
26847 &nested_name_specifier_p);
26848 /* If the class-head was a semantic disaster, skip the entire body
26849 of the class. */
26850 if (!type)
26852 cp_parser_skip_to_end_of_block_or_statement (parser);
26853 pop_deferring_access_checks ();
26854 return error_mark_node;
26857 /* Look for the `{'. */
26858 matching_braces braces;
26859 if (!braces.require_open (parser))
26861 pop_deferring_access_checks ();
26862 return error_mark_node;
26865 cp_ensure_no_omp_declare_simd (parser);
26866 cp_ensure_no_oacc_routine (parser);
26868 /* Issue an error message if type-definitions are forbidden here. */
26869 bool type_definition_ok_p = cp_parser_check_type_definition (parser);
26870 /* Remember that we are defining one more class. */
26871 ++parser->num_classes_being_defined;
26872 /* Inside the class, surrounding template-parameter-lists do not
26873 apply. */
26874 saved_num_template_parameter_lists
26875 = parser->num_template_parameter_lists;
26876 parser->num_template_parameter_lists = 0;
26877 /* We are not in a function body. */
26878 saved_in_function_body = parser->in_function_body;
26879 parser->in_function_body = false;
26880 /* Or in a loop. */
26881 in_statement = parser->in_statement;
26882 parser->in_statement = 0;
26883 /* Or in a switch. */
26884 in_switch_statement_p = parser->in_switch_statement_p;
26885 parser->in_switch_statement_p = false;
26886 /* We are not immediately inside an extern "lang" block. */
26887 saved_in_unbraced_linkage_specification_p
26888 = parser->in_unbraced_linkage_specification_p;
26889 parser->in_unbraced_linkage_specification_p = false;
26890 /* Or in an export-declaration. */
26891 saved_in_unbraced_export_declaration_p
26892 = parser->in_unbraced_export_declaration_p;
26893 parser->in_unbraced_export_declaration_p = false;
26894 /* 'this' from an enclosing non-static member function is unavailable. */
26895 tree saved_ccp = current_class_ptr;
26896 tree saved_ccr = current_class_ref;
26897 current_class_ptr = NULL_TREE;
26898 current_class_ref = NULL_TREE;
26900 /* Start the class. */
26901 if (nested_name_specifier_p)
26903 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
26904 /* SCOPE must be a scope nested inside current scope. */
26905 if (is_nested_namespace (current_namespace,
26906 decl_namespace_context (scope)))
26907 old_scope = push_inner_scope (scope);
26908 else
26909 nested_name_specifier_p = false;
26911 type = begin_class_definition (type);
26913 if (type == error_mark_node)
26914 /* If the type is erroneous, skip the entire body of the class. */
26915 cp_parser_skip_to_closing_brace (parser);
26916 else
26917 /* Parse the member-specification. */
26918 cp_parser_member_specification_opt (parser);
26920 /* Look for the trailing `}'. */
26921 closing_brace = braces.require_close (parser);
26922 /* Look for trailing attributes to apply to this class. */
26923 if (cp_parser_allow_gnu_extensions_p (parser))
26924 attributes = cp_parser_gnu_attributes_opt (parser);
26925 if (type != error_mark_node)
26926 type = finish_struct (type, attributes);
26927 if (nested_name_specifier_p)
26928 pop_inner_scope (old_scope, scope);
26930 /* We've finished a type definition. Check for the common syntax
26931 error of forgetting a semicolon after the definition. We need to
26932 be careful, as we can't just check for not-a-semicolon and be done
26933 with it; the user might have typed:
26935 class X { } c = ...;
26936 class X { } *p = ...;
26938 and so forth. Instead, enumerate all the possible tokens that
26939 might follow this production; if we don't see one of them, then
26940 complain and silently insert the semicolon. */
26942 cp_token *token = cp_lexer_peek_token (parser->lexer);
26943 bool want_semicolon = true;
26945 if (cp_next_tokens_can_be_std_attribute_p (parser))
26946 /* Don't try to parse c++11 attributes here. As per the
26947 grammar, that should be a task for
26948 cp_parser_decl_specifier_seq. */
26949 want_semicolon = false;
26951 switch (token->type)
26953 case CPP_NAME:
26954 case CPP_SEMICOLON:
26955 case CPP_MULT:
26956 case CPP_AND:
26957 case CPP_OPEN_PAREN:
26958 case CPP_CLOSE_PAREN:
26959 case CPP_COMMA:
26960 case CPP_SCOPE:
26961 want_semicolon = false;
26962 break;
26964 /* While it's legal for type qualifiers and storage class
26965 specifiers to follow type definitions in the grammar, only
26966 compiler testsuites contain code like that. Assume that if
26967 we see such code, then what we're really seeing is a case
26968 like:
26970 class X { }
26971 const <type> var = ...;
26975 class Y { }
26976 static <type> func (...) ...
26978 i.e. the qualifier or specifier applies to the next
26979 declaration. To do so, however, we need to look ahead one
26980 more token to see if *that* token is a type specifier.
26982 This code could be improved to handle:
26984 class Z { }
26985 static const <type> var = ...; */
26986 case CPP_KEYWORD:
26987 if (keyword_is_decl_specifier (token->keyword))
26989 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
26991 /* Handling user-defined types here would be nice, but very
26992 tricky. */
26993 want_semicolon
26994 = (lookahead->type == CPP_KEYWORD
26995 && keyword_begins_type_specifier (lookahead->keyword));
26997 break;
26998 default:
26999 break;
27002 /* If we don't have a type, then something is very wrong and we
27003 shouldn't try to do anything clever. Likewise for not seeing the
27004 closing brace. */
27005 if (closing_brace && TYPE_P (type) && want_semicolon)
27007 /* Locate the closing brace. */
27008 cp_token_position prev
27009 = cp_lexer_previous_token_position (parser->lexer);
27010 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
27011 location_t loc = prev_token->location;
27013 /* We want to suggest insertion of a ';' immediately *after* the
27014 closing brace, so, if we can, offset the location by 1 column. */
27015 location_t next_loc = loc;
27016 if (!linemap_location_from_macro_expansion_p (line_table, loc))
27017 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
27019 rich_location richloc (line_table, next_loc);
27021 /* If we successfully offset the location, suggest the fix-it. */
27022 if (next_loc != loc)
27023 richloc.add_fixit_insert_before (next_loc, ";");
27025 if (CLASSTYPE_DECLARED_CLASS (type))
27026 error_at (&richloc,
27027 "expected %<;%> after class definition");
27028 else if (TREE_CODE (type) == RECORD_TYPE)
27029 error_at (&richloc,
27030 "expected %<;%> after struct definition");
27031 else if (TREE_CODE (type) == UNION_TYPE)
27032 error_at (&richloc,
27033 "expected %<;%> after union definition");
27034 else
27035 gcc_unreachable ();
27037 /* Unget one token and smash it to look as though we encountered
27038 a semicolon in the input stream. */
27039 cp_lexer_set_token_position (parser->lexer, prev);
27040 token = cp_lexer_peek_token (parser->lexer);
27041 token->type = CPP_SEMICOLON;
27042 token->keyword = RID_MAX;
27046 /* If this class is not itself within the scope of another class,
27047 then we need to parse the bodies of all of the queued function
27048 definitions. Note that the queued functions defined in a class
27049 are not always processed immediately following the
27050 class-specifier for that class. Consider:
27052 struct A {
27053 struct B { void f() { sizeof (A); } };
27056 If `f' were processed before the processing of `A' were
27057 completed, there would be no way to compute the size of `A'.
27058 Note that the nesting we are interested in here is lexical --
27059 not the semantic nesting given by TYPE_CONTEXT. In particular,
27060 for:
27062 struct A { struct B; };
27063 struct A::B { void f() { } };
27065 there is no need to delay the parsing of `A::B::f'. */
27066 if (--parser->num_classes_being_defined == 0)
27068 tree decl;
27069 tree class_type = NULL_TREE;
27070 tree pushed_scope = NULL_TREE;
27071 unsigned ix;
27072 cp_default_arg_entry *e;
27074 if (!type_definition_ok_p || any_erroneous_template_args_p (type))
27076 /* Skip default arguments, NSDMIs, etc, in order to improve
27077 error recovery (c++/71169, c++/71832). */
27078 vec_safe_truncate (unparsed_funs_with_default_args, 0);
27079 vec_safe_truncate (unparsed_nsdmis, 0);
27080 vec_safe_truncate (unparsed_funs_with_definitions, 0);
27083 /* In a first pass, parse default arguments to the functions.
27084 Then, in a second pass, parse the bodies of the functions.
27085 This two-phased approach handles cases like:
27087 struct S {
27088 void f() { g(); }
27089 void g(int i = 3);
27093 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
27095 decl = e->decl;
27096 /* If there are default arguments that have not yet been processed,
27097 take care of them now. */
27098 if (class_type != e->class_type)
27100 if (pushed_scope)
27101 pop_scope (pushed_scope);
27102 class_type = e->class_type;
27103 pushed_scope = push_scope (class_type);
27105 /* Make sure that any template parameters are in scope. */
27106 maybe_begin_member_template_processing (decl);
27107 /* Parse the default argument expressions. */
27108 cp_parser_late_parsing_default_args (parser, decl);
27109 /* Remove any template parameters from the symbol table. */
27110 maybe_end_member_template_processing ();
27112 vec_safe_truncate (unparsed_funs_with_default_args, 0);
27114 /* If there are noexcept-specifiers that have not yet been processed,
27115 take care of them now. Do this before processing NSDMIs as they
27116 may depend on noexcept-specifiers already having been processed. */
27117 FOR_EACH_VEC_SAFE_ELT (unparsed_noexcepts, ix, decl)
27119 tree ctx = DECL_CONTEXT (decl);
27120 if (class_type != ctx)
27122 if (pushed_scope)
27123 pop_scope (pushed_scope);
27124 class_type = ctx;
27125 pushed_scope = push_scope (class_type);
27128 tree def_parse = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl));
27129 def_parse = TREE_PURPOSE (def_parse);
27131 /* Make sure that any template parameters are in scope. */
27132 maybe_begin_member_template_processing (decl);
27134 /* Make sure that any member-function parameters are in scope.
27135 This function doesn't expect ccp to be set. */
27136 current_class_ptr = current_class_ref = NULL_TREE;
27137 inject_parm_decls (decl);
27139 /* 'this' is not allowed in static member functions. */
27140 unsigned char local_variables_forbidden_p
27141 = parser->local_variables_forbidden_p;
27142 if (DECL_THIS_STATIC (decl))
27143 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
27145 /* Now we can parse the noexcept-specifier. */
27146 tree spec = cp_parser_late_noexcept_specifier (parser, def_parse);
27148 if (spec == error_mark_node)
27149 spec = NULL_TREE;
27151 /* Update the fn's type directly -- it might have escaped
27152 beyond this decl :( */
27153 fixup_deferred_exception_variants (TREE_TYPE (decl), spec);
27154 /* Update any instantiations we've already created. We must
27155 keep the new noexcept-specifier wrapped in a DEFERRED_NOEXCEPT
27156 so that maybe_instantiate_noexcept can tsubst the NOEXCEPT_EXPR
27157 in the pattern. */
27158 for (tree i : DEFPARSE_INSTANTIATIONS (def_parse))
27159 DEFERRED_NOEXCEPT_PATTERN (TREE_PURPOSE (i))
27160 = spec ? TREE_PURPOSE (spec) : error_mark_node;
27162 /* Restore the state of local_variables_forbidden_p. */
27163 parser->local_variables_forbidden_p = local_variables_forbidden_p;
27165 /* The finish_struct call above performed various override checking,
27166 but it skipped unparsed noexcept-specifier operands. Now that we
27167 have resolved them, check again. */
27168 noexcept_override_late_checks (decl);
27170 /* Remove any member-function parameters from the symbol table. */
27171 pop_injected_parms ();
27173 /* Remove any template parameters from the symbol table. */
27174 maybe_end_member_template_processing ();
27176 vec_safe_truncate (unparsed_noexcepts, 0);
27178 /* Now parse any NSDMIs. */
27179 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
27181 tree ctx = type_context_for_name_lookup (decl);
27182 if (class_type != ctx)
27184 if (pushed_scope)
27185 pop_scope (pushed_scope);
27186 class_type = ctx;
27187 pushed_scope = push_scope (class_type);
27189 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
27190 cp_parser_late_parsing_nsdmi (parser, decl);
27192 vec_safe_truncate (unparsed_nsdmis, 0);
27194 /* Now contract attributes. */
27195 FOR_EACH_VEC_SAFE_ELT (unparsed_contracts, ix, decl)
27197 tree ctx = DECL_CONTEXT (decl);
27198 if (class_type != ctx)
27200 if (pushed_scope)
27201 pop_scope (pushed_scope);
27202 class_type = ctx;
27203 pushed_scope = push_scope (class_type);
27206 temp_override<tree> cfd(current_function_decl, decl);
27208 /* Make sure that any template parameters are in scope. */
27209 maybe_begin_member_template_processing (decl);
27211 /* Make sure that any member-function parameters are in scope.
27212 This function doesn't expect ccp to be set. */
27213 current_class_ptr = current_class_ref = NULL_TREE;
27214 inject_parm_decls (decl);
27216 /* 'this' is not allowed in static member functions. */
27217 unsigned char local_variables_forbidden_p
27218 = parser->local_variables_forbidden_p;
27219 if (DECL_THIS_STATIC (decl))
27220 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
27222 /* Now we can parse contract conditions. */
27223 for (tree a = DECL_ATTRIBUTES (decl); a; a = TREE_CHAIN (a))
27225 if (cxx_contract_attribute_p (a))
27226 cp_parser_late_contract_condition (parser, decl, a);
27229 /* Restore the state of local_variables_forbidden_p. */
27230 parser->local_variables_forbidden_p = local_variables_forbidden_p;
27232 /* Remove any member-function parameters from the symbol table. */
27233 pop_injected_parms ();
27235 /* Remove any template parameters from the symbol table. */
27236 maybe_end_member_template_processing ();
27238 /* Perform any deferred contract matching. */
27239 match_deferred_contracts (decl);
27241 vec_safe_truncate (unparsed_contracts, 0);
27243 current_class_ptr = NULL_TREE;
27244 current_class_ref = NULL_TREE;
27245 if (pushed_scope)
27246 pop_scope (pushed_scope);
27248 /* Now parse the body of the functions. */
27249 if (flag_openmp)
27251 /* OpenMP UDRs need to be parsed before all other functions. */
27252 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
27253 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
27254 cp_parser_late_parsing_for_member (parser, decl);
27255 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
27256 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
27257 cp_parser_late_parsing_for_member (parser, decl);
27259 else
27260 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
27261 cp_parser_late_parsing_for_member (parser, decl);
27262 vec_safe_truncate (unparsed_funs_with_definitions, 0);
27265 /* Put back any saved access checks. */
27266 pop_deferring_access_checks ();
27268 /* Restore saved state. */
27269 parser->in_switch_statement_p = in_switch_statement_p;
27270 parser->in_statement = in_statement;
27271 parser->in_function_body = saved_in_function_body;
27272 parser->num_template_parameter_lists
27273 = saved_num_template_parameter_lists;
27274 parser->in_unbraced_linkage_specification_p
27275 = saved_in_unbraced_linkage_specification_p;
27276 parser->in_unbraced_export_declaration_p
27277 = saved_in_unbraced_export_declaration_p;
27278 current_class_ptr = saved_ccp;
27279 current_class_ref = saved_ccr;
27281 return type;
27284 /* Parse a class-head.
27286 class-head:
27287 class-key identifier [opt] base-clause [opt]
27288 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
27289 class-key nested-name-specifier [opt] template-id
27290 base-clause [opt]
27292 class-virt-specifier:
27293 final
27295 GNU Extensions:
27296 class-key attributes identifier [opt] base-clause [opt]
27297 class-key attributes nested-name-specifier identifier base-clause [opt]
27298 class-key attributes nested-name-specifier [opt] template-id
27299 base-clause [opt]
27301 Upon return BASES is initialized to the list of base classes (or
27302 NULL, if there are none) in the same form returned by
27303 cp_parser_base_clause.
27305 Returns the TYPE of the indicated class. Sets
27306 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
27307 involving a nested-name-specifier was used, and FALSE otherwise.
27309 Returns error_mark_node if this is not a class-head.
27311 Returns NULL_TREE if the class-head is syntactically valid, but
27312 semantically invalid in a way that means we should skip the entire
27313 body of the class. */
27315 static tree
27316 cp_parser_class_head (cp_parser* parser,
27317 bool* nested_name_specifier_p)
27319 tree nested_name_specifier;
27320 enum tag_types class_key;
27321 tree id = NULL_TREE;
27322 tree type = NULL_TREE;
27323 tree attributes;
27324 tree bases;
27325 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
27326 bool template_id_p = false;
27327 bool qualified_p = false;
27328 bool invalid_nested_name_p = false;
27329 bool invalid_explicit_specialization_p = false;
27330 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27331 tree pushed_scope = NULL_TREE;
27332 unsigned num_templates;
27333 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
27334 /* Assume no nested-name-specifier will be present. */
27335 *nested_name_specifier_p = false;
27336 /* Assume no template parameter lists will be used in defining the
27337 type. */
27338 num_templates = 0;
27339 parser->colon_corrects_to_scope_p = false;
27341 /* Look for the class-key. */
27342 class_key = cp_parser_class_key (parser);
27343 if (class_key == none_type)
27344 return error_mark_node;
27346 location_t class_head_start_location = input_location;
27348 /* Parse the attributes. */
27349 attributes = cp_parser_attributes_opt (parser);
27350 if (find_contract (attributes))
27351 diagnose_misapplied_contracts (attributes);
27353 /* If the next token is `::', that is invalid -- but sometimes
27354 people do try to write:
27356 struct ::S {};
27358 Handle this gracefully by accepting the extra qualifier, and then
27359 issuing an error about it later if this really is a
27360 class-head. If it turns out just to be an elaborated type
27361 specifier, remain silent. */
27362 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
27363 qualified_p = true;
27365 /* It is OK to define an inaccessible class; for example:
27367 class A { class B; };
27368 class A::B {};
27370 So we want to ignore access when parsing the class name.
27371 However, we might be tentatively parsing what is really an
27372 elaborated-type-specifier naming a template-id, e.g.
27374 struct C<&D::m> c;
27376 In this case the tentative parse as a class-head will fail, but not
27377 before cp_parser_template_id splices in a CPP_TEMPLATE_ID token.
27378 Since dk_no_check is sticky, we must instead use dk_deferred so that
27379 any such CPP_TEMPLATE_ID token created during this tentative parse
27380 will correctly capture the access checks imposed by the template-id . */
27381 push_deferring_access_checks (dk_deferred);
27383 /* Determine the name of the class. Begin by looking for an
27384 optional nested-name-specifier. */
27385 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
27386 nested_name_specifier
27387 = cp_parser_nested_name_specifier_opt (parser,
27388 /*typename_keyword_p=*/false,
27389 /*check_dependency_p=*/false,
27390 /*type_p=*/true,
27391 /*is_declaration=*/false);
27392 /* If there was a nested-name-specifier, then there *must* be an
27393 identifier. */
27395 cp_token *bad_template_keyword = NULL;
27397 if (nested_name_specifier)
27399 type_start_token = cp_lexer_peek_token (parser->lexer);
27400 /* Although the grammar says `identifier', it really means
27401 `class-name' or `template-name'. You are only allowed to
27402 define a class that has already been declared with this
27403 syntax.
27405 The proposed resolution for Core Issue 180 says that wherever
27406 you see `class T::X' you should treat `X' as a type-name.
27408 We do not know if we will see a class-name, or a
27409 template-name. We look for a class-name first, in case the
27410 class-name is a template-id; if we looked for the
27411 template-name first we would stop after the template-name. */
27412 cp_parser_parse_tentatively (parser);
27413 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27414 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
27415 type = cp_parser_class_name (parser,
27416 /*typename_keyword_p=*/false,
27417 /*template_keyword_p=*/false,
27418 class_type,
27419 /*check_dependency_p=*/false,
27420 /*class_head_p=*/true,
27421 /*is_declaration=*/false);
27422 /* If that didn't work, ignore the nested-name-specifier. */
27423 if (!cp_parser_parse_definitely (parser))
27425 invalid_nested_name_p = true;
27426 type_start_token = cp_lexer_peek_token (parser->lexer);
27427 id = cp_parser_identifier (parser);
27428 if (id == error_mark_node)
27429 id = NULL_TREE;
27431 /* If we could not find a corresponding TYPE, treat this
27432 declaration like an unqualified declaration. */
27433 if (type == error_mark_node)
27434 nested_name_specifier = NULL_TREE;
27435 /* Otherwise, count the number of templates used in TYPE and its
27436 containing scopes. */
27437 else
27438 num_templates = num_template_headers_for_class (TREE_TYPE (type));
27440 /* Otherwise, the identifier is optional. */
27441 else
27443 /* We don't know whether what comes next is a template-id,
27444 an identifier, or nothing at all. */
27445 cp_parser_parse_tentatively (parser);
27446 /* Check for a template-id. */
27447 type_start_token = cp_lexer_peek_token (parser->lexer);
27448 id = cp_parser_template_id (parser,
27449 /*template_keyword_p=*/false,
27450 /*check_dependency_p=*/true,
27451 class_key,
27452 /*is_declaration=*/true);
27453 /* If that didn't work, it could still be an identifier. */
27454 if (!cp_parser_parse_definitely (parser))
27456 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27458 type_start_token = cp_lexer_peek_token (parser->lexer);
27459 id = cp_parser_identifier (parser);
27461 else
27462 id = NULL_TREE;
27464 else
27466 template_id_p = true;
27467 ++num_templates;
27471 pop_deferring_access_checks ();
27473 if (id)
27475 cp_parser_check_for_invalid_template_id (parser, id,
27476 class_key,
27477 type_start_token->location);
27479 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
27481 /* If it's not a `:' or a `{' then we can't really be looking at a
27482 class-head, since a class-head only appears as part of a
27483 class-specifier. We have to detect this situation before calling
27484 xref_tag, since that has irreversible side-effects. */
27485 if (!cp_parser_next_token_starts_class_definition_p (parser))
27487 cp_parser_error (parser, "expected %<{%> or %<:%>");
27488 type = error_mark_node;
27489 goto out;
27492 /* At this point, we're going ahead with the class-specifier, even
27493 if some other problem occurs. */
27494 cp_parser_commit_to_tentative_parse (parser);
27495 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
27497 cp_parser_error (parser,
27498 "cannot specify %<override%> for a class");
27499 type = error_mark_node;
27500 goto out;
27502 /* Issue the error about the overly-qualified name now. */
27503 if (qualified_p)
27505 cp_parser_error (parser,
27506 "global qualification of class name is invalid");
27507 type = error_mark_node;
27508 goto out;
27510 else if (invalid_nested_name_p)
27512 cp_parser_error (parser,
27513 "qualified name does not name a class");
27514 type = error_mark_node;
27515 goto out;
27517 else if (nested_name_specifier)
27519 tree scope;
27521 if (bad_template_keyword)
27522 /* [temp.names]: in a qualified-id formed by a class-head-name, the
27523 keyword template shall not appear at the top level. */
27524 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
27525 "keyword %<template%> not allowed in class-head-name");
27527 /* Reject typedef-names in class heads. */
27528 if (!DECL_IMPLICIT_TYPEDEF_P (type))
27530 error_at (type_start_token->location,
27531 "invalid class name in declaration of %qD",
27532 type);
27533 type = NULL_TREE;
27534 goto done;
27537 /* Figure out in what scope the declaration is being placed. */
27538 scope = current_scope ();
27539 /* If that scope does not contain the scope in which the
27540 class was originally declared, the program is invalid. */
27541 if (scope && !is_ancestor (scope, nested_name_specifier))
27543 if (at_namespace_scope_p ())
27544 error_at (type_start_token->location,
27545 "declaration of %qD in namespace %qD which does not "
27546 "enclose %qD",
27547 type, scope, nested_name_specifier);
27548 else
27549 error_at (type_start_token->location,
27550 "declaration of %qD in %qD which does not enclose %qD",
27551 type, scope, nested_name_specifier);
27552 type = NULL_TREE;
27553 goto done;
27555 /* [dcl.meaning]
27557 A declarator-id shall not be qualified except for the
27558 definition of a ... nested class outside of its class
27559 ... [or] the definition or explicit instantiation of a
27560 class member of a namespace outside of its namespace. */
27561 if (scope == nested_name_specifier)
27562 permerror (nested_name_specifier_token_start->location,
27563 "extra qualification not allowed");
27565 /* The name-declaration of an export-declaration shall not declare
27566 a partial specialization. */
27567 if (template_id_p
27568 && parser->in_unbraced_export_declaration_p
27569 && !processing_specialization
27570 && !processing_explicit_instantiation)
27572 auto_diagnostic_group d;
27573 location_t loc = type_start_token->location;
27574 error_at (loc, "declaration of partial specialization in unbraced "
27575 "export-declaration");
27576 inform (loc, "a specialization is always exported alongside its "
27577 "primary template");
27579 /* An explicit-specialization must be preceded by "template <>". If
27580 it is not, try to recover gracefully. */
27581 if (at_namespace_scope_p ()
27582 && parser->num_template_parameter_lists == 0
27583 && !processing_template_parmlist
27584 && template_id_p)
27586 /* Build a location of this form:
27587 struct typename <ARGS>
27588 ^~~~~~~~~~~~~~~~~~~~~~
27589 with caret==start at the start token, and
27590 finishing at the end of the type. */
27591 location_t reported_loc
27592 = make_location (class_head_start_location,
27593 class_head_start_location,
27594 get_finish (type_start_token->location));
27595 rich_location richloc (line_table, reported_loc);
27596 richloc.add_fixit_insert_before (class_head_start_location,
27597 "template <> ");
27598 error_at (&richloc,
27599 "an explicit specialization must be preceded by"
27600 " %<template <>%>");
27601 invalid_explicit_specialization_p = true;
27602 /* Take the same action that would have been taken by
27603 cp_parser_explicit_specialization. */
27604 ++parser->num_template_parameter_lists;
27605 begin_specialization ();
27607 /* There must be no "return" statements between this point and the
27608 end of this function; set "type "to the correct return value and
27609 use "goto done;" to return. */
27610 /* Make sure that the right number of template parameters were
27611 present. */
27612 if (!cp_parser_check_template_parameters (parser, num_templates,
27613 template_id_p,
27614 type_start_token->location,
27615 /*declarator=*/NULL))
27617 /* If something went wrong, there is no point in even trying to
27618 process the class-definition. */
27619 type = NULL_TREE;
27620 goto done;
27623 /* Look up the type. */
27624 if (template_id_p)
27626 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
27627 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
27628 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
27630 error_at (type_start_token->location,
27631 "function template %qD redeclared as a class template", id);
27632 type = error_mark_node;
27634 else
27636 type = TREE_TYPE (id);
27637 type = maybe_process_partial_specialization (type);
27639 /* Check the scope while we still know whether or not we had a
27640 nested-name-specifier. */
27641 if (type != error_mark_node)
27642 check_unqualified_spec_or_inst (type, type_start_token->location);
27644 if (nested_name_specifier)
27645 pushed_scope = push_scope (nested_name_specifier);
27647 else if (nested_name_specifier)
27649 type = TREE_TYPE (type);
27651 /* Given:
27653 template <typename T> struct S { struct T };
27654 template <typename T> struct S<T>::T { };
27656 we will get a TYPENAME_TYPE when processing the definition of
27657 `S::T'. We need to resolve it to the actual type before we
27658 try to define it. */
27659 if (TREE_CODE (type) == TYPENAME_TYPE)
27661 type = resolve_typename_type (type, /*only_current_p=*/false);
27662 if (TREE_CODE (type) == TYPENAME_TYPE)
27664 cp_parser_error (parser, "could not resolve typename type");
27665 type = error_mark_node;
27669 type = maybe_process_partial_specialization (type);
27670 if (type == error_mark_node)
27672 type = NULL_TREE;
27673 goto done;
27676 /* Enter the scope indicated by the nested-name-specifier. */
27677 pushed_scope = push_scope (nested_name_specifier);
27678 /* Get the canonical version of this type. */
27679 type = TYPE_MAIN_DECL (type);
27680 /* Call push_template_decl if it seems like we should be defining a
27681 template either from the template headers or the type we're
27682 defining, so that we diagnose both extra and missing headers. */
27683 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
27684 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
27685 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
27687 type = push_template_decl (type);
27688 if (type == error_mark_node)
27690 type = NULL_TREE;
27691 goto done;
27695 type = TREE_TYPE (type);
27696 *nested_name_specifier_p = true;
27698 else /* The name is not a nested name. */
27700 /* If the class was unnamed, create a dummy name. */
27701 if (!id)
27702 id = make_anon_name ();
27703 TAG_how how = (parser->in_type_id_in_expr_p
27704 ? TAG_how::INNERMOST_NON_CLASS
27705 : TAG_how::CURRENT_ONLY);
27706 type = xref_tag (class_key, id, how,
27707 parser->num_template_parameter_lists);
27710 /* Diagnose class/struct/union mismatches. */
27711 cp_parser_check_class_key (parser, UNKNOWN_LOCATION, class_key, type,
27712 true, true);
27714 /* Indicate whether this class was declared as a `class' or as a
27715 `struct'. */
27716 if (TREE_CODE (type) == RECORD_TYPE)
27717 CLASSTYPE_DECLARED_CLASS (type) = class_key == class_type;
27719 /* If this type was already complete, and we see another definition,
27720 that's an error. Likewise if the type is already being defined:
27721 this can happen, eg, when it's defined from within an expression
27722 (c++/84605). */
27723 if (type != error_mark_node
27724 && (COMPLETE_TYPE_P (type) || TYPE_BEING_DEFINED (type)))
27726 error_at (type_start_token->location, "redefinition of %q#T",
27727 type);
27728 inform (location_of (type), "previous definition of %q#T",
27729 type);
27730 type = NULL_TREE;
27731 goto done;
27733 else if (type == error_mark_node)
27734 type = NULL_TREE;
27736 if (type)
27738 if (current_lambda_expr ()
27739 && uses_parameter_packs (attributes))
27741 /* In a lambda this should work, but doesn't currently. */
27742 sorry ("unexpanded parameter pack in local class in lambda");
27743 attributes = NULL_TREE;
27746 /* Apply attributes now, before any use of the class as a template
27747 argument in its base list. */
27748 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
27749 fixup_attribute_variants (type);
27752 /* Associate constraints with the type. */
27753 if (flag_concepts)
27754 type = associate_classtype_constraints (type);
27756 /* We will have entered the scope containing the class; the names of
27757 base classes should be looked up in that context. For example:
27759 struct A { struct B {}; struct C; };
27760 struct A::C : B {};
27762 is valid. */
27764 /* Get the list of base-classes, if there is one. Defer access checking
27765 until the entire list has been seen, as per [class.access.general]. */
27766 push_deferring_access_checks (dk_deferred);
27767 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27769 if (type)
27771 pushclass (type);
27772 start_lambda_scope (TYPE_NAME (type));
27774 bases = cp_parser_base_clause (parser);
27775 if (type)
27777 finish_lambda_scope ();
27778 popclass ();
27781 else
27782 bases = NULL_TREE;
27784 /* If we're really defining a class, process the base classes.
27785 If they're invalid, fail. */
27786 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27787 xref_basetypes (type, bases);
27789 /* Now that all bases have been seen and attached to the class, check
27790 accessibility of the types named in the base-clause. This must be
27791 done relative to the class scope, so that we accept e.g.
27793 struct A { protected: struct B {}; };
27794 struct C : A::B, A {}; // OK: A::B is accessible via base A
27796 as per [class.access.general]. */
27797 if (type)
27798 pushclass (type);
27799 pop_to_parent_deferring_access_checks ();
27800 if (type)
27801 popclass ();
27803 done:
27804 /* Leave the scope given by the nested-name-specifier. We will
27805 enter the class scope itself while processing the members. */
27806 if (pushed_scope)
27807 pop_scope (pushed_scope);
27809 if (invalid_explicit_specialization_p)
27811 end_specialization ();
27812 --parser->num_template_parameter_lists;
27815 if (type)
27816 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
27817 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
27818 CLASSTYPE_FINAL (type) = 1;
27819 out:
27820 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27821 return type;
27824 /* Parse a class-key.
27826 class-key:
27827 class
27828 struct
27829 union
27831 Returns the kind of class-key specified, or none_type to indicate
27832 error. */
27834 static enum tag_types
27835 cp_parser_class_key (cp_parser* parser)
27837 cp_token *token;
27838 enum tag_types tag_type;
27840 /* Look for the class-key. */
27841 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
27842 if (!token)
27843 return none_type;
27845 /* Check to see if the TOKEN is a class-key. */
27846 tag_type = cp_parser_token_is_class_key (token);
27847 if (!tag_type)
27848 cp_parser_error (parser, "expected class-key");
27849 return tag_type;
27852 /* Parse a type-parameter-key.
27854 type-parameter-key:
27855 class
27856 typename
27859 static void
27860 cp_parser_type_parameter_key (cp_parser* parser)
27862 /* Look for the type-parameter-key. */
27863 enum tag_types tag_type = none_type;
27864 cp_token *token = cp_lexer_peek_token (parser->lexer);
27865 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
27867 cp_lexer_consume_token (parser->lexer);
27868 if (pedantic && tag_type == typename_type
27869 && cxx_dialect < cxx17)
27870 /* typename is not allowed in a template template parameter
27871 by the standard until C++17. */
27872 pedwarn (token->location, OPT_Wc__17_extensions,
27873 "ISO C++ forbids typename key in template template parameter;"
27874 " use %<-std=c++17%> or %<-std=gnu++17%>");
27876 else
27877 cp_parser_error (parser, "expected %<class%> or %<typename%>");
27879 return;
27882 /* Parse an (optional) member-specification.
27884 member-specification:
27885 member-declaration member-specification [opt]
27886 access-specifier : member-specification [opt] */
27888 static void
27889 cp_parser_member_specification_opt (cp_parser* parser)
27891 while (true)
27893 cp_token *token;
27894 enum rid keyword;
27896 /* Peek at the next token. */
27897 token = cp_lexer_peek_token (parser->lexer);
27898 /* If it's a `}', or EOF then we've seen all the members. */
27899 if (token->type == CPP_CLOSE_BRACE
27900 || token->type == CPP_EOF
27901 || token->type == CPP_PRAGMA_EOL)
27902 break;
27904 /* See if this token is a keyword. */
27905 keyword = token->keyword;
27906 switch (keyword)
27908 case RID_PUBLIC:
27909 case RID_PROTECTED:
27910 case RID_PRIVATE:
27911 /* Consume the access-specifier. */
27912 cp_lexer_consume_token (parser->lexer);
27913 /* Remember which access-specifier is active. */
27914 current_access_specifier = token->u.value;
27915 /* Look for the `:'. */
27916 cp_parser_require (parser, CPP_COLON, RT_COLON);
27917 break;
27919 default:
27920 /* Accept #pragmas at class scope. */
27921 if (token->type == CPP_PRAGMA)
27923 cp_parser_pragma (parser, pragma_member, NULL);
27924 break;
27927 /* Otherwise, the next construction must be a
27928 member-declaration. */
27929 cp_parser_member_declaration (parser);
27934 /* Parse a member-declaration.
27936 member-declaration:
27937 decl-specifier-seq [opt] member-declarator-list [opt] ;
27938 function-definition ; [opt]
27939 :: [opt] nested-name-specifier template [opt] unqualified-id ;
27940 using-declaration
27941 template-declaration
27942 alias-declaration
27944 member-declarator-list:
27945 member-declarator
27946 member-declarator-list , member-declarator
27948 member-declarator:
27949 declarator pure-specifier [opt]
27950 declarator constant-initializer [opt]
27951 identifier [opt] : constant-expression
27953 GNU Extensions:
27955 member-declaration:
27956 __extension__ member-declaration
27958 member-declarator:
27959 declarator attributes [opt] pure-specifier [opt]
27960 declarator attributes [opt] constant-initializer [opt]
27961 identifier [opt] attributes [opt] : constant-expression
27963 C++0x Extensions:
27965 member-declaration:
27966 static_assert-declaration */
27968 static void
27969 cp_parser_member_declaration (cp_parser* parser)
27971 cp_decl_specifier_seq decl_specifiers;
27972 tree prefix_attributes;
27973 tree decl;
27974 int declares_class_or_enum;
27975 bool friend_p;
27976 cp_token *token = NULL;
27977 cp_token *decl_spec_token_start = NULL;
27978 cp_token *initializer_token_start = NULL;
27979 int saved_pedantic;
27980 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27982 /* Check for the `__extension__' keyword. */
27983 if (cp_parser_extension_opt (parser, &saved_pedantic))
27985 /* Recurse. */
27986 cp_parser_member_declaration (parser);
27987 /* Restore the old value of the PEDANTIC flag. */
27988 pedantic = saved_pedantic;
27990 return;
27993 /* Check for a template-declaration. */
27994 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27996 /* An explicit specialization here is an error condition, and we
27997 expect the specialization handler to detect and report this. */
27998 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
27999 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
28000 cp_parser_explicit_specialization (parser);
28001 else
28002 cp_parser_template_declaration (parser, /*member_p=*/true);
28004 return;
28006 /* Check for a template introduction. */
28007 else if (cp_parser_template_declaration_after_export (parser, true))
28008 return;
28010 /* Check for a using-declaration. */
28011 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
28013 if (cxx_dialect < cxx11)
28014 /* Parse the using-declaration. */
28015 cp_parser_using_declaration (parser, /*access_declaration_p=*/false);
28016 else if (cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_ENUM))
28017 cp_parser_using_enum (parser);
28018 else
28020 tree decl;
28021 bool alias_decl_expected;
28022 cp_parser_parse_tentatively (parser);
28023 decl = cp_parser_alias_declaration (parser);
28024 /* Note that if we actually see the '=' token after the
28025 identifier, cp_parser_alias_declaration commits the
28026 tentative parse. In that case, we really expect an
28027 alias-declaration. Otherwise, we expect a using
28028 declaration. */
28029 alias_decl_expected =
28030 !cp_parser_uncommitted_to_tentative_parse_p (parser);
28031 cp_parser_parse_definitely (parser);
28033 if (alias_decl_expected)
28034 finish_member_declaration (decl);
28035 else
28036 cp_parser_using_declaration (parser,
28037 /*access_declaration_p=*/false);
28039 return;
28042 /* Check for @defs. */
28043 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
28045 tree ivar, member;
28046 tree ivar_chains = cp_parser_objc_defs_expression (parser);
28047 ivar = ivar_chains;
28048 while (ivar)
28050 member = ivar;
28051 ivar = TREE_CHAIN (member);
28052 TREE_CHAIN (member) = NULL_TREE;
28053 finish_member_declaration (member);
28055 return;
28058 /* If the next token is `static_assert' we have a static assertion. */
28059 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
28061 cp_parser_static_assert (parser, /*member_p=*/true);
28062 return;
28065 parser->colon_corrects_to_scope_p = false;
28067 cp_omp_declare_simd_data odsd;
28068 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
28069 goto out;
28071 /* Parse the decl-specifier-seq. */
28072 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
28073 cp_parser_decl_specifier_seq (parser,
28074 (CP_PARSER_FLAGS_OPTIONAL
28075 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
28076 &decl_specifiers,
28077 &declares_class_or_enum);
28079 if (decl_specifiers.attributes && (flag_openmp || flag_openmp_simd))
28080 cp_parser_handle_directive_omp_attributes (parser,
28081 &decl_specifiers.attributes,
28082 &odsd, true);
28084 /* Check for an invalid type-name. */
28085 if (!decl_specifiers.any_type_specifiers_p
28086 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
28087 goto out;
28088 /* If there is no declarator, then the decl-specifier-seq should
28089 specify a type. */
28090 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
28092 /* If there was no decl-specifier-seq, and the next token is a
28093 `;', then we have something like:
28095 struct S { ; };
28097 [class.mem]
28099 Each member-declaration shall declare at least one member
28100 name of the class. */
28101 if (!decl_specifiers.any_specifiers_p)
28103 cp_token *token = cp_lexer_peek_token (parser->lexer);
28104 if (cxx_dialect < cxx11 && !in_system_header_at (token->location))
28106 gcc_rich_location richloc (token->location);
28107 richloc.add_fixit_remove ();
28108 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
28111 else
28113 /* See if this declaration is a friend. */
28114 friend_p = cp_parser_friend_p (&decl_specifiers);
28115 /* If there were decl-specifiers, check to see if there was
28116 a class-declaration. */
28117 tree type = check_tag_decl (&decl_specifiers,
28118 /*explicit_type_instantiation_p=*/false);
28119 /* Nested classes have already been added to the class, but
28120 a `friend' needs to be explicitly registered. */
28121 if (friend_p)
28123 /* If the `friend' keyword was present, the friend must
28124 be introduced with a class-key. */
28125 if (!declares_class_or_enum && cxx_dialect < cxx11)
28126 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
28127 "in C++03 a class-key must be used "
28128 "when declaring a friend");
28129 /* In this case:
28131 template <typename T> struct A {
28132 friend struct A<T>::B;
28135 A<T>::B will be represented by a TYPENAME_TYPE, and
28136 therefore not recognized by check_tag_decl. */
28137 if (!type)
28139 type = decl_specifiers.type;
28140 if (type && TREE_CODE (type) == TYPE_DECL)
28141 type = TREE_TYPE (type);
28143 /* Warn if an attribute cannot appear here, as per
28144 [dcl.attr.grammar]/5. But not when declares_class_or_enum:
28145 we ignore attributes in elaborated-type-specifiers. */
28146 if (!declares_class_or_enum
28147 && cxx11_attribute_p (decl_specifiers.attributes))
28149 decl_specifiers.attributes = NULL_TREE;
28150 if (warning_at (decl_spec_token_start->location,
28151 OPT_Wattributes, "attribute ignored"))
28152 inform (decl_spec_token_start->location, "an attribute "
28153 "that appertains to a friend declaration that "
28154 "is not a definition is ignored");
28156 if (!type || !TYPE_P (type))
28157 error_at (decl_spec_token_start->location,
28158 "friend declaration does not name a class or "
28159 "function");
28160 else
28161 make_friend_class (current_class_type, type,
28162 /*complain=*/true);
28164 /* If there is no TYPE, an error message will already have
28165 been issued. */
28166 else if (!type || type == error_mark_node)
28168 /* An anonymous aggregate has to be handled specially; such
28169 a declaration really declares a data member (with a
28170 particular type), as opposed to a nested class. */
28171 else if (ANON_AGGR_TYPE_P (type))
28173 /* C++11 9.5/6. */
28174 if (decl_specifiers.storage_class != sc_none)
28175 error_at (decl_spec_token_start->location,
28176 "a storage class on an anonymous aggregate "
28177 "in class scope is not allowed");
28179 /* Remove constructors and such from TYPE, now that we
28180 know it is an anonymous aggregate. */
28181 fixup_anonymous_aggr (type);
28182 /* And make the corresponding data member. */
28183 decl = build_decl (decl_spec_token_start->location,
28184 FIELD_DECL, NULL_TREE, type);
28185 /* Add it to the class. */
28186 finish_member_declaration (decl);
28188 else
28189 cp_parser_check_access_in_redeclaration
28190 (TYPE_NAME (type),
28191 decl_spec_token_start->location);
28194 else
28196 bool assume_semicolon = false;
28198 /* Clear attributes from the decl_specifiers but keep them
28199 around as prefix attributes that apply them to the entity
28200 being declared. */
28201 prefix_attributes = decl_specifiers.attributes;
28202 decl_specifiers.attributes = NULL_TREE;
28203 if (parser->omp_declare_simd
28204 && (parser->omp_declare_simd->attribs[0]
28205 == &decl_specifiers.attributes))
28206 parser->omp_declare_simd->attribs[0] = &prefix_attributes;
28208 /* See if these declarations will be friends. */
28209 friend_p = cp_parser_friend_p (&decl_specifiers);
28211 /* Keep going until we hit the `;' at the end of the
28212 declaration. */
28213 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
28215 tree attributes = NULL_TREE;
28216 tree first_attribute;
28217 tree initializer;
28218 bool named_bitfld = false;
28220 /* Peek at the next token. */
28221 token = cp_lexer_peek_token (parser->lexer);
28223 /* The following code wants to know early if it is a bit-field
28224 or some other declaration. Attributes can appear before
28225 the `:' token. Skip over them without consuming any tokens
28226 to peek if they are followed by `:'. */
28227 if (cp_next_tokens_can_be_attribute_p (parser)
28228 || (token->type == CPP_NAME
28229 && cp_nth_tokens_can_be_attribute_p (parser, 2)
28230 && (named_bitfld = true)))
28232 size_t n
28233 = cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
28234 token = cp_lexer_peek_nth_token (parser->lexer, n);
28237 /* Check for a bitfield declaration. */
28238 if (token->type == CPP_COLON
28239 || (token->type == CPP_NAME
28240 && token == cp_lexer_peek_token (parser->lexer)
28241 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
28242 && (named_bitfld = true)))
28244 tree identifier;
28245 tree width;
28246 tree late_attributes = NULL_TREE;
28247 location_t id_location
28248 = cp_lexer_peek_token (parser->lexer)->location;
28250 if (named_bitfld)
28251 identifier = cp_parser_identifier (parser);
28252 else
28253 identifier = NULL_TREE;
28255 /* Look for attributes that apply to the bitfield. */
28256 attributes = cp_parser_attributes_opt (parser);
28258 /* Consume the `:' token. */
28259 cp_lexer_consume_token (parser->lexer);
28261 /* Get the width of the bitfield. */
28262 width = cp_parser_constant_expression (parser, false, NULL,
28263 cxx_dialect >= cxx11);
28265 /* In C++20 and as extension for C++11 and above we allow
28266 default member initializers for bit-fields. */
28267 initializer = NULL_TREE;
28268 if (cxx_dialect >= cxx11
28269 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
28270 || cp_lexer_next_token_is (parser->lexer,
28271 CPP_OPEN_BRACE)))
28273 location_t loc
28274 = cp_lexer_peek_token (parser->lexer)->location;
28275 if (cxx_dialect < cxx20
28276 && identifier != NULL_TREE)
28277 pedwarn (loc, OPT_Wc__20_extensions,
28278 "default member initializers for bit-fields "
28279 "only available with %<-std=c++20%> or "
28280 "%<-std=gnu++20%>");
28282 initializer = cp_parser_save_nsdmi (parser);
28283 if (identifier == NULL_TREE)
28285 error_at (loc, "default member initializer for "
28286 "unnamed bit-field");
28287 initializer = NULL_TREE;
28290 else
28292 /* Look for attributes that apply to the bitfield after
28293 the `:' token and width. This is where GCC used to
28294 parse attributes in the past, pedwarn if there is
28295 a std attribute. */
28296 if (cp_next_tokens_can_be_std_attribute_p (parser))
28297 pedwarn (input_location, OPT_Wpedantic,
28298 "ISO C++ allows bit-field attributes only "
28299 "before the %<:%> token");
28301 late_attributes = cp_parser_attributes_opt (parser);
28304 attributes = attr_chainon (attributes, late_attributes);
28306 /* Remember which attributes are prefix attributes and
28307 which are not. */
28308 first_attribute = attributes;
28309 /* Combine the attributes. */
28310 attributes = attr_chainon (prefix_attributes, attributes);
28312 /* Create the bitfield declaration. */
28313 decl = grokbitfield (identifier
28314 ? make_id_declarator (NULL_TREE,
28315 identifier,
28316 sfk_none,
28317 id_location)
28318 : NULL,
28319 &decl_specifiers,
28320 width, initializer,
28321 attributes);
28323 else
28325 cp_declarator *declarator;
28326 tree asm_specification;
28327 int ctor_dtor_or_conv_p;
28328 bool static_p = (decl_specifiers.storage_class == sc_static);
28329 cp_parser_flags flags = CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
28330 /* We can't delay parsing for friends,
28331 alias-declarations, and typedefs, even though the
28332 standard seems to require it. */
28333 if (!friend_p
28334 && !decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
28335 flags |= CP_PARSER_FLAGS_DELAY_NOEXCEPT;
28337 /* Parse the declarator. */
28338 declarator
28339 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28340 flags,
28341 &ctor_dtor_or_conv_p,
28342 /*parenthesized_p=*/NULL,
28343 /*member_p=*/true,
28344 friend_p, static_p);
28346 /* If something went wrong parsing the declarator, make sure
28347 that we at least consume some tokens. */
28348 if (declarator == cp_error_declarator)
28350 /* Skip to the end of the statement. */
28351 cp_parser_skip_to_end_of_statement (parser);
28352 /* If the next token is not a semicolon, that is
28353 probably because we just skipped over the body of
28354 a function. So, we consume a semicolon if
28355 present, but do not issue an error message if it
28356 is not present. */
28357 if (cp_lexer_next_token_is (parser->lexer,
28358 CPP_SEMICOLON))
28359 cp_lexer_consume_token (parser->lexer);
28360 goto out;
28363 /* Handle class-scope non-template C++17 deduction guides. */
28364 cp_parser_maybe_adjust_declarator_for_dguide (parser,
28365 &decl_specifiers,
28366 declarator,
28367 &ctor_dtor_or_conv_p);
28369 if (declares_class_or_enum & 2)
28370 cp_parser_check_for_definition_in_return_type
28371 (declarator, decl_specifiers.type,
28372 decl_specifiers.locations[ds_type_spec]);
28374 /* Look for an asm-specification. */
28375 asm_specification = cp_parser_asm_specification_opt (parser);
28376 /* Look for attributes that apply to the declaration. */
28377 attributes = cp_parser_attributes_opt (parser);
28378 /* Remember which attributes are prefix attributes and
28379 which are not. */
28380 first_attribute = attributes;
28381 /* Combine the attributes. */
28382 attributes = attr_chainon (prefix_attributes, attributes);
28384 /* If it's an `=', then we have a constant-initializer or a
28385 pure-specifier. It is not correct to parse the
28386 initializer before registering the member declaration
28387 since the member declaration should be in scope while
28388 its initializer is processed. However, the rest of the
28389 front end does not yet provide an interface that allows
28390 us to handle this correctly. */
28391 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
28393 /* In [class.mem]:
28395 A pure-specifier shall be used only in the declaration of
28396 a virtual function.
28398 A member-declarator can contain a constant-initializer
28399 only if it declares a static member of integral or
28400 enumeration type.
28402 Therefore, if the DECLARATOR is for a function, we look
28403 for a pure-specifier; otherwise, we look for a
28404 constant-initializer. When we call `grokfield', it will
28405 perform more stringent semantics checks. */
28406 initializer_token_start = cp_lexer_peek_token (parser->lexer);
28407 declarator->init_loc = initializer_token_start->location;
28408 if (function_declarator_p (declarator)
28409 || (decl_specifiers.type
28410 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
28411 && declarator->kind == cdk_id
28412 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
28413 == FUNCTION_TYPE)))
28414 initializer = cp_parser_pure_specifier (parser);
28415 else if (decl_specifiers.storage_class != sc_static)
28416 initializer = cp_parser_save_nsdmi (parser);
28417 else if (cxx_dialect >= cxx11)
28419 /* Don't require a constant rvalue in C++11, since we
28420 might want a reference constant. We'll enforce
28421 constancy later. */
28422 cp_lexer_consume_token (parser->lexer);
28423 /* Parse the initializer. */
28424 initializer = cp_parser_initializer_clause (parser);
28426 else
28427 /* Parse the initializer. */
28428 initializer = cp_parser_constant_initializer (parser);
28430 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
28431 && !function_declarator_p (declarator))
28433 declarator->init_loc
28434 = cp_lexer_peek_token (parser->lexer)->location;
28435 if (decl_specifiers.storage_class != sc_static)
28436 initializer = cp_parser_save_nsdmi (parser);
28437 else
28438 initializer = cp_parser_initializer (parser);
28440 /* Detect invalid bit-field cases such as
28442 int *p : 4;
28443 int &&r : 3;
28445 and similar. */
28446 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
28447 /* If there were no type specifiers, it was a
28448 constructor. */
28449 && decl_specifiers.any_type_specifiers_p)
28451 /* This is called for a decent diagnostic only. */
28452 tree d = grokdeclarator (declarator, &decl_specifiers,
28453 BITFIELD, /*initialized=*/false,
28454 &attributes);
28455 if (!error_operand_p (d))
28456 error_at (DECL_SOURCE_LOCATION (d),
28457 "bit-field %qD has non-integral type %qT",
28458 d, TREE_TYPE (d));
28459 cp_parser_skip_to_end_of_statement (parser);
28460 /* Avoid "extra ;" pedwarns. */
28461 if (cp_lexer_next_token_is (parser->lexer,
28462 CPP_SEMICOLON))
28463 cp_lexer_consume_token (parser->lexer);
28464 goto out;
28466 /* Otherwise, there is no initializer. */
28467 else
28468 initializer = NULL_TREE;
28470 /* See if we are probably looking at a function
28471 definition. We are certainly not looking at a
28472 member-declarator. Calling `grokfield' has
28473 side-effects, so we must not do it unless we are sure
28474 that we are looking at a member-declarator. */
28475 if (cp_parser_token_starts_function_definition_p
28476 (cp_lexer_peek_token (parser->lexer)))
28478 /* The grammar does not allow a pure-specifier to be
28479 used when a member function is defined. (It is
28480 possible that this fact is an oversight in the
28481 standard, since a pure function may be defined
28482 outside of the class-specifier. */
28483 if (initializer && initializer_token_start)
28484 error_at (initializer_token_start->location,
28485 "pure-specifier on function-definition");
28486 decl = cp_parser_save_member_function_body (parser,
28487 &decl_specifiers,
28488 declarator,
28489 attributes);
28491 if (parser->fully_implicit_function_template_p)
28492 decl = finish_fully_implicit_template (parser, decl);
28493 /* If the member was not a friend, declare it here. */
28494 if (!friend_p)
28495 finish_member_declaration (decl);
28496 /* Peek at the next token. */
28497 token = cp_lexer_peek_token (parser->lexer);
28498 /* If the next token is a semicolon, consume it. */
28499 if (token->type == CPP_SEMICOLON)
28501 location_t semicolon_loc
28502 = cp_lexer_consume_token (parser->lexer)->location;
28503 gcc_rich_location richloc (semicolon_loc);
28504 richloc.add_fixit_remove ();
28505 warning_at (&richloc, OPT_Wextra_semi,
28506 "extra %<;%> after in-class "
28507 "function definition");
28509 goto out;
28511 else
28512 if (declarator->kind == cdk_function)
28513 declarator->id_loc = token->location;
28515 /* Create the declaration. */
28516 decl = grokfield (declarator, &decl_specifiers,
28517 initializer, /*init_const_expr_p=*/true,
28518 asm_specification, attributes);
28520 if (parser->fully_implicit_function_template_p)
28522 if (friend_p)
28523 finish_fully_implicit_template (parser, 0);
28524 else
28525 decl = finish_fully_implicit_template (parser, decl);
28529 cp_finalize_omp_declare_simd (parser, decl);
28530 cp_finalize_oacc_routine (parser, decl, false);
28532 /* Reset PREFIX_ATTRIBUTES. */
28533 if (attributes != error_mark_node)
28535 while (attributes && TREE_CHAIN (attributes) != first_attribute)
28536 attributes = TREE_CHAIN (attributes);
28537 if (attributes)
28538 TREE_CHAIN (attributes) = NULL_TREE;
28541 /* If there is any qualification still in effect, clear it
28542 now; we will be starting fresh with the next declarator. */
28543 parser->scope = NULL_TREE;
28544 parser->qualifying_scope = NULL_TREE;
28545 parser->object_scope = NULL_TREE;
28546 /* If it's a `,', then there are more declarators. */
28547 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28549 cp_lexer_consume_token (parser->lexer);
28550 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
28552 cp_token *token = cp_lexer_previous_token (parser->lexer);
28553 gcc_rich_location richloc (token->location);
28554 richloc.add_fixit_remove ();
28555 error_at (&richloc, "stray %<,%> at end of "
28556 "member declaration");
28559 /* If the next token isn't a `;', then we have a parse error. */
28560 else if (cp_lexer_next_token_is_not (parser->lexer,
28561 CPP_SEMICOLON))
28563 /* The next token might be a ways away from where the
28564 actual semicolon is missing. Find the previous token
28565 and use that for our error position. */
28566 cp_token *token = cp_lexer_previous_token (parser->lexer);
28567 gcc_rich_location richloc (token->location);
28568 richloc.add_fixit_insert_after (";");
28569 error_at (&richloc, "expected %<;%> at end of "
28570 "member declaration");
28572 /* Assume that the user meant to provide a semicolon. If
28573 we were to cp_parser_skip_to_end_of_statement, we might
28574 skip to a semicolon inside a member function definition
28575 and issue nonsensical error messages. */
28576 assume_semicolon = true;
28579 if (decl)
28581 /* Add DECL to the list of members. */
28582 if (!friend_p
28583 /* Explicitly include, eg, NSDMIs, for better error
28584 recovery (c++/58650). */
28585 || !DECL_DECLARES_FUNCTION_P (decl))
28586 finish_member_declaration (decl);
28588 if (DECL_DECLARES_FUNCTION_P (decl))
28589 cp_parser_save_default_args (parser, STRIP_TEMPLATE (decl));
28590 else if (TREE_CODE (decl) == FIELD_DECL
28591 && DECL_INITIAL (decl))
28592 /* Add DECL to the queue of NSDMI to be parsed later. */
28593 vec_safe_push (unparsed_nsdmis, decl);
28596 if (assume_semicolon)
28597 goto out;
28601 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
28602 out:
28603 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
28604 cp_finalize_omp_declare_simd (parser, &odsd);
28607 /* Parse a pure-specifier.
28609 pure-specifier:
28612 Returns INTEGER_ZERO_NODE if a pure specifier is found.
28613 Otherwise, ERROR_MARK_NODE is returned. */
28615 static tree
28616 cp_parser_pure_specifier (cp_parser* parser)
28618 cp_token *token;
28620 /* Look for the `=' token. */
28621 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28622 return error_mark_node;
28623 /* Look for the `0' token. */
28624 token = cp_lexer_peek_token (parser->lexer);
28626 if (token->type == CPP_EOF
28627 || token->type == CPP_PRAGMA_EOL)
28628 return error_mark_node;
28630 cp_lexer_consume_token (parser->lexer);
28632 /* Accept = default or = delete in c++0x mode. */
28633 if (token->keyword == RID_DEFAULT
28634 || token->keyword == RID_DELETE)
28636 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
28637 return token->u.value;
28640 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
28641 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
28643 cp_parser_error (parser,
28644 "invalid pure specifier (only %<= 0%> is allowed)");
28645 cp_parser_skip_to_end_of_statement (parser);
28646 return error_mark_node;
28648 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
28650 error_at (token->location, "templates may not be %<virtual%>");
28651 return error_mark_node;
28654 return integer_zero_node;
28657 /* Parse a constant-initializer.
28659 constant-initializer:
28660 = constant-expression
28662 Returns a representation of the constant-expression. */
28664 static tree
28665 cp_parser_constant_initializer (cp_parser* parser)
28667 /* Look for the `=' token. */
28668 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28669 return error_mark_node;
28671 /* It is invalid to write:
28673 struct S { static const int i = { 7 }; };
28676 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28678 cp_parser_error (parser,
28679 "a brace-enclosed initializer is not allowed here");
28680 /* Consume the opening brace. */
28681 matching_braces braces;
28682 braces.consume_open (parser);
28683 /* Skip the initializer. */
28684 cp_parser_skip_to_closing_brace (parser);
28685 /* Look for the trailing `}'. */
28686 braces.require_close (parser);
28688 return error_mark_node;
28691 return cp_parser_constant_expression (parser);
28694 /* Derived classes [gram.class.derived] */
28696 /* Parse a base-clause.
28698 base-clause:
28699 : base-specifier-list
28701 base-specifier-list:
28702 base-specifier ... [opt]
28703 base-specifier-list , base-specifier ... [opt]
28705 Returns a TREE_LIST representing the base-classes, in the order in
28706 which they were declared. The representation of each node is as
28707 described by cp_parser_base_specifier.
28709 In the case that no bases are specified, this function will return
28710 NULL_TREE, not ERROR_MARK_NODE. */
28712 static tree
28713 cp_parser_base_clause (cp_parser* parser)
28715 tree bases = NULL_TREE;
28717 /* Look for the `:' that begins the list. */
28718 cp_parser_require (parser, CPP_COLON, RT_COLON);
28720 /* Scan the base-specifier-list. */
28721 while (true)
28723 cp_token *token;
28724 tree base;
28725 bool pack_expansion_p = false;
28727 /* Look for the base-specifier. */
28728 base = cp_parser_base_specifier (parser);
28729 /* Look for the (optional) ellipsis. */
28730 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28732 /* Consume the `...'. */
28733 cp_lexer_consume_token (parser->lexer);
28735 pack_expansion_p = true;
28738 /* Add BASE to the front of the list. */
28739 if (base && base != error_mark_node)
28741 if (pack_expansion_p)
28742 /* Make this a pack expansion type. */
28743 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
28745 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
28747 TREE_CHAIN (base) = bases;
28748 bases = base;
28751 /* Peek at the next token. */
28752 token = cp_lexer_peek_token (parser->lexer);
28753 /* If it's not a comma, then the list is complete. */
28754 if (token->type != CPP_COMMA)
28755 break;
28756 /* Consume the `,'. */
28757 cp_lexer_consume_token (parser->lexer);
28760 /* PARSER->SCOPE may still be non-NULL at this point, if the last
28761 base class had a qualified name. However, the next name that
28762 appears is certainly not qualified. */
28763 parser->scope = NULL_TREE;
28764 parser->qualifying_scope = NULL_TREE;
28765 parser->object_scope = NULL_TREE;
28767 return nreverse (bases);
28770 /* Parse a base-specifier.
28772 base-specifier:
28773 :: [opt] nested-name-specifier [opt] class-name
28774 virtual access-specifier [opt] :: [opt] nested-name-specifier
28775 [opt] class-name
28776 access-specifier virtual [opt] :: [opt] nested-name-specifier
28777 [opt] class-name
28779 Returns a TREE_LIST. The TREE_PURPOSE will be one of
28780 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
28781 indicate the specifiers provided. The TREE_VALUE will be a TYPE
28782 (or the ERROR_MARK_NODE) indicating the type that was specified. */
28784 static tree
28785 cp_parser_base_specifier (cp_parser* parser)
28787 cp_token *token;
28788 bool done = false;
28789 bool virtual_p = false;
28790 bool duplicate_virtual_error_issued_p = false;
28791 bool duplicate_access_error_issued_p = false;
28792 bool class_scope_p, template_p;
28793 tree access = access_default_node;
28794 tree type;
28796 /* Process the optional `virtual' and `access-specifier'. */
28797 while (!done)
28799 /* Peek at the next token. */
28800 token = cp_lexer_peek_token (parser->lexer);
28801 /* Process `virtual'. */
28802 switch (token->keyword)
28804 case RID_VIRTUAL:
28805 /* If `virtual' appears more than once, issue an error. */
28806 if (virtual_p && !duplicate_virtual_error_issued_p)
28808 cp_parser_error (parser,
28809 "%<virtual%> specified more than once in base-specifier");
28810 duplicate_virtual_error_issued_p = true;
28813 virtual_p = true;
28815 /* Consume the `virtual' token. */
28816 cp_lexer_consume_token (parser->lexer);
28818 break;
28820 case RID_PUBLIC:
28821 case RID_PROTECTED:
28822 case RID_PRIVATE:
28823 /* If more than one access specifier appears, issue an
28824 error. */
28825 if (access != access_default_node
28826 && !duplicate_access_error_issued_p)
28828 cp_parser_error (parser,
28829 "more than one access specifier in base-specifier");
28830 duplicate_access_error_issued_p = true;
28833 access = ridpointers[(int) token->keyword];
28835 /* Consume the access-specifier. */
28836 cp_lexer_consume_token (parser->lexer);
28838 break;
28840 default:
28841 done = true;
28842 break;
28845 /* It is not uncommon to see programs mechanically, erroneously, use
28846 the 'typename' keyword to denote (dependent) qualified types
28847 as base classes. */
28848 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
28850 token = cp_lexer_peek_token (parser->lexer);
28851 if (!processing_template_decl)
28852 error_at (token->location,
28853 "keyword %<typename%> not allowed outside of templates");
28854 else
28855 error_at (token->location,
28856 "keyword %<typename%> not allowed in this context "
28857 "(the base class is implicitly a type)");
28858 cp_lexer_consume_token (parser->lexer);
28861 /* Look for the optional `::' operator. */
28862 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
28863 /* Look for the nested-name-specifier. The simplest way to
28864 implement:
28866 [temp.res]
28868 The keyword `typename' is not permitted in a base-specifier or
28869 mem-initializer; in these contexts a qualified name that
28870 depends on a template-parameter is implicitly assumed to be a
28871 type name.
28873 is to pretend that we have seen the `typename' keyword at this
28874 point. */
28875 cp_parser_nested_name_specifier_opt (parser,
28876 /*typename_keyword_p=*/true,
28877 /*check_dependency_p=*/true,
28878 /*type_p=*/true,
28879 /*is_declaration=*/true);
28880 /* If the base class is given by a qualified name, assume that names
28881 we see are type names or templates, as appropriate. */
28882 class_scope_p = (parser->scope && TYPE_P (parser->scope));
28883 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
28885 if (!parser->scope
28886 && cp_lexer_next_token_is_decltype (parser->lexer))
28887 /* DR 950 allows decltype as a base-specifier. */
28888 type = cp_parser_decltype (parser);
28889 else
28891 /* Otherwise, look for the class-name. */
28892 type = cp_parser_class_name (parser,
28893 class_scope_p,
28894 template_p,
28895 typename_type,
28896 /*check_dependency_p=*/true,
28897 /*class_head_p=*/false,
28898 /*is_declaration=*/true);
28899 type = TREE_TYPE (type);
28902 if (type == error_mark_node)
28903 return error_mark_node;
28905 return finish_base_specifier (type, access, virtual_p);
28908 /* Exception handling [gram.exception] */
28910 /* Save the tokens that make up the noexcept-specifier for a member-function.
28911 Returns a DEFERRED_PARSE. */
28913 static tree
28914 cp_parser_save_noexcept (cp_parser *parser)
28916 cp_token *first = parser->lexer->next_token;
28917 /* We want everything up to, including, the final ')'. */
28918 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0);
28919 cp_token *last = parser->lexer->next_token;
28921 /* As with default arguments and NSDMIs, make use of DEFERRED_PARSE
28922 to carry the information we will need. */
28923 tree expr = make_node (DEFERRED_PARSE);
28924 /* Save away the noexcept-specifier; we will process it when the
28925 class is complete. */
28926 DEFPARSE_TOKENS (expr) = cp_token_cache_new (first, last);
28927 DEFPARSE_INSTANTIATIONS (expr) = nullptr;
28928 expr = build_tree_list (expr, NULL_TREE);
28929 return expr;
28932 /* Used for late processing of noexcept-specifiers of member-functions.
28933 DEFAULT_ARG is the unparsed operand of a noexcept-specifier which
28934 we saved for later; parse it now. DECL is the declaration of the
28935 member function. */
28937 static tree
28938 cp_parser_late_noexcept_specifier (cp_parser *parser, tree default_arg)
28940 /* Make sure we've gotten something that hasn't been parsed yet. */
28941 gcc_assert (TREE_CODE (default_arg) == DEFERRED_PARSE);
28943 push_unparsed_function_queues (parser);
28945 /* Push the saved tokens for the noexcept-specifier onto the parser's
28946 lexer stack. */
28947 cp_token_cache *tokens = DEFPARSE_TOKENS (default_arg);
28948 cp_parser_push_lexer_for_tokens (parser, tokens);
28950 /* Parse the cached noexcept-specifier. */
28951 tree parsed_arg
28952 = cp_parser_noexcept_specification_opt (parser,
28953 CP_PARSER_FLAGS_NONE,
28954 /*require_constexpr=*/true,
28955 /*consumed_expr=*/NULL,
28956 /*return_cond=*/false);
28958 /* Revert to the main lexer. */
28959 cp_parser_pop_lexer (parser);
28961 /* Restore the queue. */
28962 pop_unparsed_function_queues (parser);
28964 /* And we're done. */
28965 return parsed_arg;
28968 /* Perform late checking of overriding function with respect to their
28969 noexcept-specifiers. FNDECL is the member function that potentially
28970 overrides some virtual function with the same signature. */
28972 static void
28973 noexcept_override_late_checks (tree fndecl)
28975 tree binfo = TYPE_BINFO (DECL_CONTEXT (fndecl));
28976 tree base_binfo;
28978 if (DECL_STATIC_FUNCTION_P (fndecl))
28979 return;
28981 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
28983 tree basetype = BINFO_TYPE (base_binfo);
28985 if (!TYPE_POLYMORPHIC_P (basetype))
28986 continue;
28988 tree fn = look_for_overrides_here (basetype, fndecl);
28989 if (fn)
28990 maybe_check_overriding_exception_spec (fndecl, fn);
28994 /* Parse an (optional) noexcept-specification.
28996 noexcept-specification:
28997 noexcept ( constant-expression ) [opt]
28999 If no noexcept-specification is present, returns NULL_TREE.
29000 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
29001 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
29002 there are no parentheses. CONSUMED_EXPR will be set accordingly.
29003 Otherwise, returns a noexcept specification unless RETURN_COND is true,
29004 in which case a boolean condition is returned instead. The parser flags
29005 FLAGS is used to control parsing. QUALS are qualifiers indicating whether
29006 the (member) function is `const'. */
29008 static tree
29009 cp_parser_noexcept_specification_opt (cp_parser* parser,
29010 cp_parser_flags flags,
29011 bool require_constexpr,
29012 bool* consumed_expr,
29013 bool return_cond)
29015 cp_token *token;
29016 const char *saved_message;
29018 /* Peek at the next token. */
29019 token = cp_lexer_peek_token (parser->lexer);
29021 /* Is it a noexcept-specification? */
29022 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
29024 tree expr;
29026 /* [class.mem]/6 says that a noexcept-specifer (within the
29027 member-specification of the class) is a complete-class context of
29028 a class. So, if the noexcept-specifier has the optional expression,
29029 just save the tokens, and reparse this after we're done with the
29030 class. */
29032 if ((flags & CP_PARSER_FLAGS_DELAY_NOEXCEPT)
29033 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN)
29034 /* No need to delay parsing for a number literal or true/false. */
29035 && !((cp_lexer_nth_token_is (parser->lexer, 3, CPP_NUMBER)
29036 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
29037 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_PAREN))
29038 && at_class_scope_p ()
29039 && TYPE_BEING_DEFINED (current_class_type)
29040 && !LAMBDA_TYPE_P (current_class_type))
29041 return cp_parser_save_noexcept (parser);
29043 cp_lexer_consume_token (parser->lexer);
29045 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
29047 matching_parens parens;
29048 parens.consume_open (parser);
29050 if (require_constexpr)
29052 /* Types may not be defined in an exception-specification. */
29053 saved_message = parser->type_definition_forbidden_message;
29054 parser->type_definition_forbidden_message
29055 = G_("types may not be defined in an exception-specification");
29057 bool non_constant_p;
29058 expr
29059 = cp_parser_constant_expression (parser,
29060 /*allow_non_constant=*/true,
29061 &non_constant_p);
29062 if (non_constant_p
29063 && !require_potential_rvalue_constant_expression (expr))
29065 expr = NULL_TREE;
29066 return_cond = true;
29069 /* Restore the saved message. */
29070 parser->type_definition_forbidden_message = saved_message;
29072 else
29074 expr = cp_parser_expression (parser);
29075 *consumed_expr = true;
29078 parens.require_close (parser);
29080 else
29082 expr = boolean_true_node;
29083 if (!require_constexpr)
29084 *consumed_expr = false;
29087 /* We cannot build a noexcept-spec right away because this will check
29088 that expr is a constexpr. */
29089 if (!return_cond)
29090 return build_noexcept_spec (expr, tf_warning_or_error);
29091 else
29092 return expr;
29094 else
29095 return NULL_TREE;
29098 /* Parse an (optional) exception-specification.
29100 exception-specification:
29101 throw ( type-id-list [opt] )
29103 Returns a TREE_LIST representing the exception-specification. The
29104 TREE_VALUE of each node is a type. The parser flags FLAGS is used to
29105 control parsing. QUALS are qualifiers indicating whether the (member)
29106 function is `const'. */
29108 static tree
29109 cp_parser_exception_specification_opt (cp_parser* parser,
29110 cp_parser_flags flags)
29112 cp_token *token;
29113 tree type_id_list;
29114 const char *saved_message;
29116 /* Peek at the next token. */
29117 token = cp_lexer_peek_token (parser->lexer);
29119 /* Is it a noexcept-specification? */
29120 type_id_list
29121 = cp_parser_noexcept_specification_opt (parser, flags,
29122 /*require_constexpr=*/true,
29123 /*consumed_expr=*/NULL,
29124 /*return_cond=*/false);
29125 if (type_id_list != NULL_TREE)
29126 return type_id_list;
29128 /* If it's not `throw', then there's no exception-specification. */
29129 if (!cp_parser_is_keyword (token, RID_THROW))
29130 return NULL_TREE;
29132 location_t loc = token->location;
29134 /* Consume the `throw'. */
29135 cp_lexer_consume_token (parser->lexer);
29137 /* Look for the `('. */
29138 matching_parens parens;
29139 parens.require_open (parser);
29141 /* Peek at the next token. */
29142 token = cp_lexer_peek_token (parser->lexer);
29143 /* If it's not a `)', then there is a type-id-list. */
29144 if (token->type != CPP_CLOSE_PAREN)
29146 /* Types may not be defined in an exception-specification. */
29147 saved_message = parser->type_definition_forbidden_message;
29148 parser->type_definition_forbidden_message
29149 = G_("types may not be defined in an exception-specification");
29150 /* Parse the type-id-list. */
29151 type_id_list = cp_parser_type_id_list (parser);
29152 /* Restore the saved message. */
29153 parser->type_definition_forbidden_message = saved_message;
29155 if (cxx_dialect >= cxx17)
29157 error_at (loc, "ISO C++17 does not allow dynamic exception "
29158 "specifications");
29159 type_id_list = NULL_TREE;
29161 else if (cxx_dialect >= cxx11)
29162 warning_at (loc, OPT_Wdeprecated,
29163 "dynamic exception specifications are deprecated in "
29164 "C++11");
29166 /* In C++17, throw() is equivalent to noexcept (true). throw()
29167 is deprecated in C++11 and above as well, but is still widely used,
29168 so don't warn about it yet. */
29169 else if (cxx_dialect >= cxx17)
29170 type_id_list = noexcept_true_spec;
29171 else
29172 type_id_list = empty_except_spec;
29174 /* Look for the `)'. */
29175 parens.require_close (parser);
29177 return type_id_list;
29180 /* Parse an (optional) type-id-list.
29182 type-id-list:
29183 type-id ... [opt]
29184 type-id-list , type-id ... [opt]
29186 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
29187 in the order that the types were presented. */
29189 static tree
29190 cp_parser_type_id_list (cp_parser* parser)
29192 tree types = NULL_TREE;
29194 while (true)
29196 cp_token *token;
29197 tree type;
29199 token = cp_lexer_peek_token (parser->lexer);
29201 /* Get the next type-id. */
29202 type = cp_parser_type_id (parser);
29203 /* Check for invalid 'auto'. */
29204 if (flag_concepts && type_uses_auto (type))
29206 error_at (token->location,
29207 "invalid use of %<auto%> in exception-specification");
29208 type = error_mark_node;
29210 /* Parse the optional ellipsis. */
29211 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
29213 /* Consume the `...'. */
29214 cp_lexer_consume_token (parser->lexer);
29216 /* Turn the type into a pack expansion expression. */
29217 type = make_pack_expansion (type);
29219 /* Add it to the list. */
29220 types = add_exception_specifier (types, type, /*complain=*/1);
29221 /* Peek at the next token. */
29222 token = cp_lexer_peek_token (parser->lexer);
29223 /* If it is not a `,', we are done. */
29224 if (token->type != CPP_COMMA)
29225 break;
29226 /* Consume the `,'. */
29227 cp_lexer_consume_token (parser->lexer);
29230 return nreverse (types);
29233 /* Parse a try-block.
29235 try-block:
29236 try compound-statement handler-seq */
29238 static tree
29239 cp_parser_try_block (cp_parser* parser)
29241 tree try_block;
29243 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
29244 if (parser->in_function_body
29245 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
29246 && cxx_dialect < cxx20)
29247 pedwarn (input_location, OPT_Wc__20_extensions,
29248 "%<try%> in %<constexpr%> function only "
29249 "available with %<-std=c++20%> or %<-std=gnu++20%>");
29251 try_block = begin_try_block ();
29252 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
29253 finish_try_block (try_block);
29254 cp_parser_handler_seq (parser);
29255 finish_handler_sequence (try_block);
29257 return try_block;
29260 /* Parse a function-try-block.
29262 function-try-block:
29263 try ctor-initializer [opt] function-body handler-seq */
29265 static void
29266 cp_parser_function_try_block (cp_parser* parser)
29268 tree compound_stmt;
29269 tree try_block;
29271 /* Look for the `try' keyword. */
29272 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
29273 return;
29274 /* Let the rest of the front end know where we are. */
29275 try_block = begin_function_try_block (&compound_stmt);
29276 /* Parse the function-body. */
29277 cp_parser_ctor_initializer_opt_and_function_body
29278 (parser, /*in_function_try_block=*/true);
29279 /* We're done with the `try' part. */
29280 finish_function_try_block (try_block);
29281 /* Parse the handlers. */
29282 cp_parser_handler_seq (parser);
29283 /* We're done with the handlers. */
29284 finish_function_handler_sequence (try_block, compound_stmt);
29287 /* Parse a handler-seq.
29289 handler-seq:
29290 handler handler-seq [opt] */
29292 static void
29293 cp_parser_handler_seq (cp_parser* parser)
29295 while (true)
29297 cp_token *token;
29299 /* Parse the handler. */
29300 cp_parser_handler (parser);
29301 /* Peek at the next token. */
29302 token = cp_lexer_peek_token (parser->lexer);
29303 /* If it's not `catch' then there are no more handlers. */
29304 if (!cp_parser_is_keyword (token, RID_CATCH))
29305 break;
29309 /* Parse a handler.
29311 handler:
29312 catch ( exception-declaration ) compound-statement */
29314 static void
29315 cp_parser_handler (cp_parser* parser)
29317 tree handler;
29318 tree declaration;
29320 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
29321 handler = begin_handler ();
29322 matching_parens parens;
29323 parens.require_open (parser);
29324 declaration = cp_parser_exception_declaration (parser);
29325 finish_handler_parms (declaration, handler);
29326 parens.require_close (parser);
29327 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29328 finish_handler (handler);
29331 /* Parse an exception-declaration.
29333 exception-declaration:
29334 type-specifier-seq declarator
29335 type-specifier-seq abstract-declarator
29336 type-specifier-seq
29339 Returns a VAR_DECL for the declaration, or NULL_TREE if the
29340 ellipsis variant is used. */
29342 static tree
29343 cp_parser_exception_declaration (cp_parser* parser)
29345 cp_decl_specifier_seq type_specifiers;
29346 cp_declarator *declarator;
29347 const char *saved_message;
29349 /* If it's an ellipsis, it's easy to handle. */
29350 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
29352 /* Consume the `...' token. */
29353 cp_lexer_consume_token (parser->lexer);
29354 return NULL_TREE;
29357 /* Types may not be defined in exception-declarations. */
29358 saved_message = parser->type_definition_forbidden_message;
29359 parser->type_definition_forbidden_message
29360 = G_("types may not be defined in exception-declarations");
29362 /* Parse the type-specifier-seq. */
29363 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
29364 /*is_declaration=*/true,
29365 /*is_trailing_return=*/false,
29366 &type_specifiers);
29367 /* If it's a `)', then there is no declarator. */
29368 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
29369 declarator = NULL;
29370 else
29371 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
29372 CP_PARSER_FLAGS_NONE,
29373 /*ctor_dtor_or_conv_p=*/NULL,
29374 /*parenthesized_p=*/NULL,
29375 /*member_p=*/false,
29376 /*friend_p=*/false,
29377 /*static_p=*/false);
29379 /* Restore the saved message. */
29380 parser->type_definition_forbidden_message = saved_message;
29382 if (!type_specifiers.any_specifiers_p)
29383 return error_mark_node;
29385 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
29388 /* Parse a throw-expression.
29390 throw-expression:
29391 throw assignment-expression [opt]
29393 Returns a THROW_EXPR representing the throw-expression. */
29395 static tree
29396 cp_parser_throw_expression (cp_parser* parser)
29398 tree expression;
29399 cp_token* token;
29400 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29402 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
29403 token = cp_lexer_peek_token (parser->lexer);
29404 /* Figure out whether or not there is an assignment-expression
29405 following the "throw" keyword. */
29406 if (token->type == CPP_COMMA
29407 || token->type == CPP_SEMICOLON
29408 || token->type == CPP_CLOSE_PAREN
29409 || token->type == CPP_CLOSE_SQUARE
29410 || token->type == CPP_CLOSE_BRACE
29411 || token->type == CPP_COLON)
29412 expression = NULL_TREE;
29413 else
29414 expression = cp_parser_assignment_expression (parser);
29416 /* Construct a location e.g.:
29417 throw x
29418 ^~~~~~~
29419 with caret == start at the start of the "throw" token, and
29420 the end at the end of the final token we consumed. */
29421 location_t combined_loc = make_location (start_loc, start_loc,
29422 parser->lexer);
29423 expression = build_throw (combined_loc, expression, tf_warning_or_error);
29425 return expression;
29428 /* Parse a yield-expression.
29430 yield-expression:
29431 co_yield assignment-expression
29432 co_yield braced-init-list
29434 Returns a CO_YIELD_EXPR representing the yield-expression. */
29436 static tree
29437 cp_parser_yield_expression (cp_parser* parser)
29439 tree expr;
29441 cp_token *token = cp_lexer_peek_token (parser->lexer);
29442 location_t kw_loc = token->location; /* Save for later. */
29444 cp_parser_require_keyword (parser, RID_CO_YIELD, RT_CO_YIELD);
29446 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29448 cp_lexer_set_source_position (parser->lexer);
29449 /* ??? : probably a moot point? */
29450 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
29451 expr = cp_parser_braced_list (parser);
29453 else
29454 expr = cp_parser_assignment_expression (parser);
29456 if (expr == error_mark_node)
29457 return expr;
29459 return finish_co_yield_expr (kw_loc, expr);
29462 /* GNU Extensions */
29464 /* Parse an (optional) asm-specification.
29466 asm-specification:
29467 asm ( string-literal )
29469 If the asm-specification is present, returns a STRING_CST
29470 corresponding to the string-literal. Otherwise, returns
29471 NULL_TREE. */
29473 static tree
29474 cp_parser_asm_specification_opt (cp_parser* parser)
29476 /* Peek at the next token. */
29477 cp_token *token = cp_lexer_peek_token (parser->lexer);
29478 /* If the next token isn't the `asm' keyword, then there's no
29479 asm-specification. */
29480 if (!cp_parser_is_keyword (token, RID_ASM))
29481 return NULL_TREE;
29483 /* Consume the `asm' token. */
29484 cp_lexer_consume_token (parser->lexer);
29485 /* Look for the `('. */
29486 matching_parens parens;
29487 parens.require_open (parser);
29489 /* Look for the string-literal. */
29490 tree asm_specification = cp_parser_string_literal (parser,
29491 /*translate=*/false,
29492 /*wide_ok=*/false);
29494 /* Look for the `)'. */
29495 parens.require_close (parser);
29497 return asm_specification;
29500 /* Parse an asm-operand-list.
29502 asm-operand-list:
29503 asm-operand
29504 asm-operand-list , asm-operand
29506 asm-operand:
29507 string-literal ( expression )
29508 [ string-literal ] string-literal ( expression )
29510 Returns a TREE_LIST representing the operands. The TREE_VALUE of
29511 each node is the expression. The TREE_PURPOSE is itself a
29512 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
29513 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
29514 is a STRING_CST for the string literal before the parenthesis. Returns
29515 ERROR_MARK_NODE if any of the operands are invalid. */
29517 static tree
29518 cp_parser_asm_operand_list (cp_parser* parser)
29520 tree asm_operands = NULL_TREE;
29521 bool invalid_operands = false;
29523 while (true)
29525 tree name;
29527 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
29529 /* Consume the `[' token. */
29530 cp_lexer_consume_token (parser->lexer);
29531 /* Read the operand name. */
29532 name = cp_parser_identifier (parser);
29533 if (name != error_mark_node)
29534 name = build_string (IDENTIFIER_LENGTH (name),
29535 IDENTIFIER_POINTER (name));
29536 /* Look for the closing `]'. */
29537 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
29539 else
29540 name = NULL_TREE;
29541 /* Look for the string-literal. */
29542 tree string_literal = cp_parser_string_literal (parser,
29543 /*translate=*/false,
29544 /*wide_ok=*/false);
29546 /* Look for the `('. */
29547 matching_parens parens;
29548 parens.require_open (parser);
29549 /* Parse the expression. */
29550 tree expression = cp_parser_expression (parser);
29551 /* Look for the `)'. */
29552 parens.require_close (parser);
29554 if (name == error_mark_node
29555 || string_literal == error_mark_node
29556 || expression == error_mark_node)
29557 invalid_operands = true;
29559 /* Add this operand to the list. */
29560 asm_operands = tree_cons (build_tree_list (name, string_literal),
29561 expression,
29562 asm_operands);
29563 /* If the next token is not a `,', there are no more
29564 operands. */
29565 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
29566 break;
29567 /* Consume the `,'. */
29568 cp_lexer_consume_token (parser->lexer);
29571 return invalid_operands ? error_mark_node : nreverse (asm_operands);
29574 /* Parse an asm-clobber-list.
29576 asm-clobber-list:
29577 string-literal
29578 asm-clobber-list , string-literal
29580 Returns a TREE_LIST, indicating the clobbers in the order that they
29581 appeared. The TREE_VALUE of each node is a STRING_CST. */
29583 static tree
29584 cp_parser_asm_clobber_list (cp_parser* parser)
29586 tree clobbers = NULL_TREE;
29588 while (true)
29590 /* Look for the string literal. */
29591 tree string_literal = cp_parser_string_literal (parser,
29592 /*translate=*/false,
29593 /*wide_ok=*/false);
29594 /* Add it to the list. */
29595 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
29596 /* If the next token is not a `,', then the list is
29597 complete. */
29598 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
29599 break;
29600 /* Consume the `,' token. */
29601 cp_lexer_consume_token (parser->lexer);
29604 return clobbers;
29607 /* Parse an asm-label-list.
29609 asm-label-list:
29610 identifier
29611 asm-label-list , identifier
29613 Returns a TREE_LIST, indicating the labels in the order that they
29614 appeared. The TREE_VALUE of each node is a label. */
29616 static tree
29617 cp_parser_asm_label_list (cp_parser* parser)
29619 tree labels = NULL_TREE;
29621 while (true)
29623 tree identifier, label, name;
29625 /* Look for the identifier. */
29626 identifier = cp_parser_identifier (parser);
29627 if (!error_operand_p (identifier))
29629 label = lookup_label (identifier);
29630 if (TREE_CODE (label) == LABEL_DECL)
29632 TREE_USED (label) = 1;
29633 check_goto (label);
29634 name = build_string (IDENTIFIER_LENGTH (identifier),
29635 IDENTIFIER_POINTER (identifier));
29636 labels = tree_cons (name, label, labels);
29639 /* If the next token is not a `,', then the list is
29640 complete. */
29641 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
29642 break;
29643 /* Consume the `,' token. */
29644 cp_lexer_consume_token (parser->lexer);
29647 return nreverse (labels);
29650 /* Return TRUE iff the next tokens in the stream are possibly the
29651 beginning of a GNU extension attribute. */
29653 static bool
29654 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
29656 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
29659 /* Return TRUE iff the next tokens in the stream are possibly the
29660 beginning of a standard C++-11 attribute specifier. */
29662 static bool
29663 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
29665 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
29668 /* Return TRUE iff the next Nth tokens in the stream are possibly the
29669 beginning of a standard C++-11 attribute specifier. */
29671 static bool
29672 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
29674 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
29676 return (cxx_dialect >= cxx11
29677 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
29678 || (token->type == CPP_OPEN_SQUARE
29679 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
29680 && token->type == CPP_OPEN_SQUARE)));
29683 /* Return TRUE iff the next Nth tokens in the stream are possibly the
29684 beginning of a GNU extension attribute. */
29686 static bool
29687 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
29689 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
29691 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
29694 /* Return true iff the next tokens can be the beginning of either a
29695 GNU attribute list, or a standard C++11 attribute sequence. */
29697 static bool
29698 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
29700 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
29701 || cp_next_tokens_can_be_std_attribute_p (parser));
29704 /* Return true iff the next Nth tokens can be the beginning of either
29705 a GNU attribute list, or a standard C++11 attribute sequence. */
29707 static bool
29708 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
29710 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
29711 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
29714 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
29715 of GNU attributes, or return NULL. */
29717 static tree
29718 cp_parser_attributes_opt (cp_parser *parser)
29720 tree attrs = NULL_TREE;
29721 while (true)
29723 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
29724 attrs = attr_chainon (attrs, cp_parser_gnu_attributes_opt (parser));
29725 else if (cp_next_tokens_can_be_std_attribute_p (parser))
29726 attrs = attr_chainon (attrs, cp_parser_std_attribute_spec_seq (parser));
29727 else
29728 break;
29730 return attrs;
29733 /* Parse an (optional) series of attributes.
29735 attributes:
29736 attributes attribute
29738 attribute:
29739 __attribute__ (( attribute-list [opt] ))
29741 The return value is as for cp_parser_gnu_attribute_list. */
29743 static tree
29744 cp_parser_gnu_attributes_opt (cp_parser* parser)
29746 tree attributes = NULL_TREE;
29748 auto cleanup = make_temp_override
29749 (parser->auto_is_implicit_function_template_parm_p, false);
29751 while (true)
29753 cp_token *token;
29754 tree attribute_list;
29755 bool ok = true;
29757 /* Peek at the next token. */
29758 token = cp_lexer_peek_token (parser->lexer);
29759 /* If it's not `__attribute__', then we're done. */
29760 if (token->keyword != RID_ATTRIBUTE)
29761 break;
29763 /* Consume the `__attribute__' keyword. */
29764 cp_lexer_consume_token (parser->lexer);
29765 /* Look for the two `(' tokens. */
29766 matching_parens outer_parens;
29767 if (!outer_parens.require_open (parser))
29768 ok = false;
29769 matching_parens inner_parens;
29770 if (!inner_parens.require_open (parser))
29771 ok = false;
29773 /* Peek at the next token. */
29774 token = cp_lexer_peek_token (parser->lexer);
29775 if (token->type != CPP_CLOSE_PAREN)
29776 /* Parse the attribute-list. */
29777 attribute_list = cp_parser_gnu_attribute_list (parser);
29778 else
29779 /* If the next token is a `)', then there is no attribute
29780 list. */
29781 attribute_list = NULL;
29783 /* Look for the two `)' tokens. */
29784 if (!inner_parens.require_close (parser))
29785 ok = false;
29786 if (!outer_parens.require_close (parser))
29787 ok = false;
29788 if (!ok)
29789 cp_parser_skip_to_end_of_statement (parser);
29791 /* Add these new attributes to the list. */
29792 attributes = attr_chainon (attributes, attribute_list);
29795 return attributes;
29798 /* Parse a GNU attribute-list.
29800 attribute-list:
29801 attribute
29802 attribute-list , attribute
29804 attribute:
29805 identifier
29806 identifier ( identifier )
29807 identifier ( identifier , expression-list )
29808 identifier ( expression-list )
29810 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
29811 to an attribute. The TREE_PURPOSE of each node is the identifier
29812 indicating which attribute is in use. The TREE_VALUE represents
29813 the arguments, if any. */
29815 static tree
29816 cp_parser_gnu_attribute_list (cp_parser* parser, bool exactly_one /* = false */)
29818 tree attribute_list = NULL_TREE;
29819 bool save_translate_strings_p = parser->translate_strings_p;
29821 /* Don't create wrapper nodes within attributes: the
29822 handlers don't know how to handle them. */
29823 auto_suppress_location_wrappers sentinel;
29825 parser->translate_strings_p = false;
29826 while (true)
29828 cp_token *token;
29829 tree identifier;
29830 tree attribute;
29832 /* Look for the identifier. We also allow keywords here; for
29833 example `__attribute__ ((const))' is legal. */
29834 token = cp_lexer_peek_token (parser->lexer);
29835 if (token->type == CPP_NAME
29836 || token->type == CPP_KEYWORD)
29838 tree arguments = NULL_TREE;
29840 /* Consume the token, but save it since we need it for the
29841 SIMD enabled function parsing. */
29842 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
29844 /* Save away the identifier that indicates which attribute
29845 this is. */
29846 identifier = (token->type == CPP_KEYWORD)
29847 /* For keywords, use the canonical spelling, not the
29848 parsed identifier. */
29849 ? ridpointers[(int) token->keyword]
29850 : id_token->u.value;
29852 identifier = canonicalize_attr_name (identifier);
29853 attribute = build_tree_list (identifier, NULL_TREE);
29855 /* Peek at the next token. */
29856 token = cp_lexer_peek_token (parser->lexer);
29857 /* If it's an `(', then parse the attribute arguments. */
29858 if (token->type == CPP_OPEN_PAREN)
29860 vec<tree, va_gc> *vec;
29861 int attr_flag = (attribute_takes_identifier_p (identifier)
29862 ? id_attr : normal_attr);
29863 if (is_attribute_p ("assume", identifier))
29864 attr_flag = assume_attr;
29865 vec = cp_parser_parenthesized_expression_list
29866 (parser, attr_flag, /*cast_p=*/false,
29867 /*allow_expansion_p=*/false,
29868 /*non_constant_p=*/NULL);
29869 if (vec == NULL)
29870 arguments = error_mark_node;
29871 else
29873 arguments = build_tree_list_vec (vec);
29874 release_tree_vector (vec);
29876 /* Save the arguments away. */
29877 TREE_VALUE (attribute) = arguments;
29880 if (arguments != error_mark_node)
29882 /* Add this attribute to the list. */
29883 TREE_CHAIN (attribute) = attribute_list;
29884 attribute_list = attribute;
29887 token = cp_lexer_peek_token (parser->lexer);
29889 /* Unless EXACTLY_ONE is set look for more attributes.
29890 If the next token isn't a `,', we're done. */
29891 if (exactly_one || token->type != CPP_COMMA)
29892 break;
29894 /* Consume the comma and keep going. */
29895 cp_lexer_consume_token (parser->lexer);
29897 parser->translate_strings_p = save_translate_strings_p;
29899 /* We built up the list in reverse order. */
29900 return nreverse (attribute_list);
29903 /* Parse arguments of omp::directive attribute.
29905 ( directive-name ,[opt] clause-list[opt] )
29907 For directive just remember the first/last tokens for subsequent
29908 parsing. */
29910 static void
29911 cp_parser_omp_directive_args (cp_parser *parser, tree attribute, bool decl_p)
29913 cp_token *first = cp_lexer_peek_nth_token (parser->lexer, 2);
29914 if (first->type == CPP_CLOSE_PAREN)
29916 cp_lexer_consume_token (parser->lexer);
29917 error_at (first->location, "expected OpenMP directive name");
29918 cp_lexer_consume_token (parser->lexer);
29919 TREE_VALUE (attribute) = NULL_TREE;
29920 return;
29922 size_t n = cp_parser_skip_balanced_tokens (parser, 1);
29923 if (n == 1)
29925 cp_lexer_consume_token (parser->lexer);
29926 error_at (first->location, "expected attribute argument as balanced "
29927 "token sequence");
29928 TREE_VALUE (attribute) = NULL_TREE;
29929 return;
29931 for (n = n - 2; n; --n)
29932 cp_lexer_consume_token (parser->lexer);
29933 cp_token *last = cp_lexer_peek_token (parser->lexer);
29934 cp_lexer_consume_token (parser->lexer);
29935 tree arg = make_node (DEFERRED_PARSE);
29936 DEFPARSE_TOKENS (arg) = cp_token_cache_new (first, last);
29937 DEFPARSE_INSTANTIATIONS (arg) = nullptr;
29938 if (decl_p)
29939 TREE_PUBLIC (arg) = 1;
29940 TREE_VALUE (attribute) = tree_cons (NULL_TREE, arg, TREE_VALUE (attribute));
29943 /* Parse arguments of omp::sequence attribute.
29945 ( omp::[opt] directive-attr [ , omp::[opt] directive-attr ]... ) */
29947 static void
29948 cp_parser_omp_sequence_args (cp_parser *parser, tree attribute)
29950 matching_parens parens;
29951 parens.consume_open (parser);
29954 cp_token *token = cp_lexer_peek_token (parser->lexer);
29955 if (token->type == CPP_NAME
29956 && token->u.value == omp_identifier
29957 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
29959 cp_lexer_consume_token (parser->lexer);
29960 cp_lexer_consume_token (parser->lexer);
29961 token = cp_lexer_peek_token (parser->lexer);
29963 bool directive = false;
29964 const char *p;
29965 if (token->type != CPP_NAME)
29966 p = "";
29967 else
29968 p = IDENTIFIER_POINTER (token->u.value);
29969 if (strcmp (p, "directive") == 0)
29970 directive = true;
29971 else if (strcmp (p, "sequence") != 0)
29973 error_at (token->location, "expected %<directive%> or %<sequence%>");
29974 cp_parser_skip_to_closing_parenthesis (parser,
29975 /*recovering=*/true,
29976 /*or_comma=*/true,
29977 /*consume_paren=*/false);
29978 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
29979 break;
29980 cp_lexer_consume_token (parser->lexer);
29982 cp_lexer_consume_token (parser->lexer);
29983 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
29984 cp_parser_required_error (parser, RT_OPEN_PAREN, false,
29985 UNKNOWN_LOCATION);
29986 else if (directive)
29987 cp_parser_omp_directive_args (parser, attribute, false);
29988 else
29989 cp_parser_omp_sequence_args (parser, attribute);
29990 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
29991 break;
29992 cp_lexer_consume_token (parser->lexer);
29994 while (1);
29995 if (!parens.require_close (parser))
29996 cp_parser_skip_to_closing_parenthesis (parser, true, false,
29997 /*consume_paren=*/true);
30000 /* Parse a standard C++11 attribute.
30002 The returned representation is a TREE_LIST which TREE_PURPOSE is
30003 the scoped name of the attribute, and the TREE_VALUE is its
30004 arguments list.
30006 Note that the scoped name of the attribute is itself a TREE_LIST
30007 which TREE_PURPOSE is the namespace of the attribute, and
30008 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
30009 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
30010 and which TREE_PURPOSE is directly the attribute name.
30012 Clients of the attribute code should use get_attribute_namespace
30013 and get_attribute_name to get the actual namespace and name of
30014 attributes, regardless of their being GNU or C++11 attributes.
30016 attribute:
30017 attribute-token attribute-argument-clause [opt]
30019 attribute-token:
30020 identifier
30021 attribute-scoped-token
30023 attribute-scoped-token:
30024 attribute-namespace :: identifier
30026 attribute-namespace:
30027 identifier
30029 attribute-argument-clause:
30030 ( balanced-token-seq )
30032 balanced-token-seq:
30033 balanced-token [opt]
30034 balanced-token-seq balanced-token
30036 balanced-token:
30037 ( balanced-token-seq )
30038 [ balanced-token-seq ]
30039 { balanced-token-seq }. */
30041 static tree
30042 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
30044 tree attribute, attr_id = NULL_TREE, arguments;
30045 cp_token *token;
30047 auto cleanup = make_temp_override
30048 (parser->auto_is_implicit_function_template_parm_p, false);
30050 /* First, parse name of the attribute, a.k.a attribute-token. */
30052 token = cp_lexer_peek_token (parser->lexer);
30053 if (token->type == CPP_NAME)
30054 attr_id = token->u.value;
30055 else if (token->type == CPP_KEYWORD)
30056 attr_id = ridpointers[(int) token->keyword];
30057 else if (token->flags & NAMED_OP)
30058 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
30060 if (attr_id == NULL_TREE)
30061 return NULL_TREE;
30063 cp_lexer_consume_token (parser->lexer);
30065 token = cp_lexer_peek_token (parser->lexer);
30066 if (token->type == CPP_SCOPE)
30068 /* We are seeing a scoped attribute token. */
30070 cp_lexer_consume_token (parser->lexer);
30071 if (attr_ns)
30072 error_at (token->location, "attribute using prefix used together "
30073 "with scoped attribute token");
30074 attr_ns = attr_id;
30076 token = cp_lexer_peek_token (parser->lexer);
30077 if (token->type == CPP_NAME)
30078 attr_id = token->u.value;
30079 else if (token->type == CPP_KEYWORD)
30080 attr_id = ridpointers[(int) token->keyword];
30081 else if (token->flags & NAMED_OP)
30082 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
30083 else
30085 error_at (token->location,
30086 "expected an identifier for the attribute name");
30087 return error_mark_node;
30089 cp_lexer_consume_token (parser->lexer);
30091 attr_ns = canonicalize_attr_name (attr_ns);
30092 attr_id = canonicalize_attr_name (attr_id);
30093 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
30094 NULL_TREE);
30095 token = cp_lexer_peek_token (parser->lexer);
30097 else if (attr_ns)
30099 attr_ns = canonicalize_attr_name (attr_ns);
30100 attr_id = canonicalize_attr_name (attr_id);
30101 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
30102 NULL_TREE);
30104 else
30106 attr_id = canonicalize_attr_name (attr_id);
30107 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
30108 NULL_TREE);
30110 /* We used to treat C++11 noreturn attribute as equivalent to GNU's,
30111 but no longer: we have to be able to tell [[noreturn]] and
30112 __attribute__((noreturn)) apart. */
30113 /* C++14 deprecated attribute is equivalent to GNU's. */
30114 if (is_attribute_p ("deprecated", attr_id))
30115 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
30116 /* C++17 fallthrough attribute is equivalent to GNU's. */
30117 else if (is_attribute_p ("fallthrough", attr_id))
30118 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
30119 /* C++23 assume attribute is equivalent to GNU's. */
30120 else if (is_attribute_p ("assume", attr_id))
30121 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
30122 /* Transactional Memory TS optimize_for_synchronized attribute is
30123 equivalent to GNU transaction_callable. */
30124 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
30125 TREE_PURPOSE (attribute)
30126 = get_identifier ("transaction_callable");
30127 /* Transactional Memory attributes are GNU attributes. */
30128 else if (tm_attr_to_mask (attr_id))
30129 TREE_PURPOSE (attribute) = attr_id;
30132 /* Now parse the optional argument clause of the attribute. */
30134 if (token->type != CPP_OPEN_PAREN)
30136 if ((flag_openmp || flag_openmp_simd)
30137 && attr_ns == omp_identifier
30138 && (is_attribute_p ("directive", attr_id)
30139 || is_attribute_p ("sequence", attr_id)
30140 || is_attribute_p ("decl", attr_id)))
30142 error_at (token->location, "%<omp::%E%> attribute requires argument",
30143 attr_id);
30144 return NULL_TREE;
30146 return attribute;
30150 vec<tree, va_gc> *vec;
30151 int attr_flag = normal_attr;
30153 /* Maybe we don't expect to see any arguments for this attribute. */
30154 const attribute_spec *as
30155 = lookup_attribute_spec (TREE_PURPOSE (attribute));
30156 if (as && as->max_length == 0)
30158 error_at (token->location, "%qE attribute does not take any arguments",
30159 attr_id);
30160 cp_parser_skip_to_closing_parenthesis (parser,
30161 /*recovering=*/true,
30162 /*or_comma=*/false,
30163 /*consume_paren=*/true);
30164 return error_mark_node;
30167 if (is_attribute_p ("assume", attr_id)
30168 && (attr_ns == NULL_TREE || attr_ns == gnu_identifier))
30169 /* The assume attribute needs special handling of the argument. */
30170 attr_flag = assume_attr;
30171 else if (attr_ns == gnu_identifier
30172 && attribute_takes_identifier_p (attr_id))
30173 /* A GNU attribute that takes an identifier in parameter. */
30174 attr_flag = id_attr;
30175 else if (attr_ns == NULL_TREE
30176 && cxx_dialect >= cxx26
30177 && (is_attribute_p ("deprecated", attr_id)
30178 || is_attribute_p ("nodiscard", attr_id)))
30179 attr_flag = uneval_string_attr;
30181 /* If this is a fake attribute created to handle -Wno-attributes,
30182 we must skip parsing the arguments. */
30183 if (as == NULL || attribute_ignored_p (as))
30185 if ((flag_openmp || flag_openmp_simd) && attr_ns == omp_identifier)
30187 if (is_attribute_p ("directive", attr_id))
30189 cp_parser_omp_directive_args (parser, attribute, false);
30190 return attribute;
30192 else if (is_attribute_p ("decl", attr_id))
30194 TREE_VALUE (TREE_PURPOSE (attribute))
30195 = get_identifier ("directive");
30196 cp_parser_omp_directive_args (parser, attribute, true);
30197 return attribute;
30199 else if (is_attribute_p ("sequence", attr_id))
30201 TREE_VALUE (TREE_PURPOSE (attribute))
30202 = get_identifier ("directive");
30203 cp_parser_omp_sequence_args (parser, attribute);
30204 TREE_VALUE (attribute) = nreverse (TREE_VALUE (attribute));
30205 return attribute;
30209 /* For unknown attributes, just skip balanced tokens instead of
30210 trying to parse the arguments. Set TREE_VALUE (attribute) to
30211 error_mark_node to distinguish skipped arguments from attributes
30212 with no arguments. */
30213 for (size_t n = cp_parser_skip_balanced_tokens (parser, 1) - 1; n; --n)
30214 cp_lexer_consume_token (parser->lexer);
30215 TREE_VALUE (attribute) = error_mark_node;
30216 return attribute;
30219 vec = cp_parser_parenthesized_expression_list
30220 (parser, attr_flag, /*cast_p=*/false,
30221 /*allow_expansion_p=*/true,
30222 /*non_constant_p=*/NULL);
30223 if (vec == NULL)
30224 arguments = error_mark_node;
30225 else
30227 if (vec->is_empty ())
30228 /* e.g. [[attr()]]. */
30229 error_at (token->location, "parentheses must be omitted if "
30230 "%qE attribute argument list is empty",
30231 attr_id);
30232 arguments = build_tree_list_vec (vec);
30233 release_tree_vector (vec);
30236 if (arguments == error_mark_node)
30237 attribute = error_mark_node;
30238 else
30239 TREE_VALUE (attribute) = arguments;
30242 return attribute;
30245 /* Warn if the attribute ATTRIBUTE appears more than once in the
30246 attribute-list ATTRIBUTES. This used to be enforced for certain
30247 attributes, but the restriction was removed in P2156.
30248 LOC is the location of ATTRIBUTE. Returns true if ATTRIBUTE was not
30249 found in ATTRIBUTES. */
30251 static bool
30252 cp_parser_check_std_attribute (location_t loc, tree attributes, tree attribute)
30254 static auto alist = { "noreturn", "deprecated", "nodiscard", "maybe_unused",
30255 "likely", "unlikely", "fallthrough",
30256 "no_unique_address", "carries_dependency" };
30257 if (attributes)
30258 for (const auto &a : alist)
30259 if (is_attribute_p (a, get_attribute_name (attribute))
30260 && is_attribute_namespace_p ("", attribute)
30261 && lookup_attribute ("", a, attributes))
30263 if (!from_macro_expansion_at (loc))
30264 warning_at (loc, OPT_Wattributes, "attribute %qs specified "
30265 "multiple times", a);
30266 return false;
30268 return true;
30271 /* Parse a list of standard C++-11 attributes.
30273 attribute-list:
30274 attribute [opt]
30275 attribute-list , attribute[opt]
30276 attribute ...
30277 attribute-list , attribute ...
30280 static tree
30281 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
30283 tree attributes = NULL_TREE, attribute = NULL_TREE;
30284 cp_token *token = NULL;
30286 while (true)
30288 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30289 attribute = cp_parser_std_attribute (parser, attr_ns);
30290 if (attribute == error_mark_node)
30291 break;
30292 if (attribute != NULL_TREE)
30294 if (cp_parser_check_std_attribute (loc, attributes, attribute))
30296 TREE_CHAIN (attribute) = attributes;
30297 attributes = attribute;
30300 token = cp_lexer_peek_token (parser->lexer);
30301 if (token->type == CPP_ELLIPSIS)
30303 cp_lexer_consume_token (parser->lexer);
30304 if (attribute == NULL_TREE)
30305 error_at (token->location,
30306 "expected attribute before %<...%>");
30307 else if (TREE_VALUE (attribute) == NULL_TREE)
30309 error_at (token->location, "attribute with no arguments "
30310 "contains no parameter packs");
30311 return error_mark_node;
30313 else if (TREE_VALUE (attribute) != error_mark_node)
30315 tree pack = make_pack_expansion (TREE_VALUE (attribute));
30316 if (pack == error_mark_node)
30317 return error_mark_node;
30318 TREE_VALUE (attribute) = pack;
30320 token = cp_lexer_peek_token (parser->lexer);
30322 if (token->type != CPP_COMMA)
30323 break;
30324 cp_lexer_consume_token (parser->lexer);
30326 attributes = nreverse (attributes);
30327 return attributes;
30330 /* Optionally parse a C++20 contract role. A NULL return means that no
30331 contract role was specified.
30333 contract-role:
30334 % default
30335 % identifier
30337 If the identifier does not name a known contract role, it will
30338 be assumed to be default. Returns the identifier for the role
30339 token. */
30341 static tree
30342 cp_parser_contract_role (cp_parser *parser)
30344 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_MOD));
30345 cp_lexer_consume_token (parser->lexer);
30347 cp_token *token = cp_lexer_peek_token (parser->lexer);
30348 tree role_id = NULL_TREE;
30349 if (token->type == CPP_NAME)
30350 role_id = token->u.value;
30351 else if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
30352 role_id = get_identifier ("default");
30353 else
30355 error_at (token->location, "expected contract-role");
30356 return error_mark_node;
30358 cp_lexer_consume_token (parser->lexer);
30360 /* FIXME: Warn about invalid/unknown roles? */
30361 return role_id;
30364 /* Parse an optional contract mode.
30366 contract-mode:
30367 contract-semantic
30368 [contract-level] [contract-role]
30370 contract-semantic:
30371 check_never_continue
30372 check_maybe_continue
30373 check_always_continue
30375 contract-level:
30376 default
30377 audit
30378 axiom
30380 contract-role:
30381 default
30382 identifier
30384 This grammar is taken from P1332R0. During parsing, this sets options
30385 on the MODE object to determine the configuration of the contract.
30387 Returns a tree containing the identifiers used in the configuration.
30388 This is either an IDENTIFIER with the literal semantic or a TREE_LIST
30389 whose TREE_VALUE is the contract-level and whose TREE_PURPOSE is the
30390 contract-role, if any. NULL_TREE is returned if no information is
30391 given (i.e., all defaults selected). */
30393 static tree
30394 cp_parser_contract_mode_opt (cp_parser *parser,
30395 bool postcondition_p)
30397 /* The mode is empty; the level and role are default. */
30398 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
30399 return NULL_TREE;
30401 /* There is only a role; the level is default. */
30402 if (cp_lexer_next_token_is (parser->lexer, CPP_MOD))
30404 tree role_id = cp_parser_contract_role (parser);
30405 return build_tree_list (role_id, get_identifier ("default"));
30408 /* Otherwise, match semantic or level. */
30409 cp_token *token = cp_lexer_peek_token (parser->lexer);
30410 contract_level level = CONTRACT_INVALID;
30411 contract_semantic semantic = CCS_INVALID;
30412 tree config_id;
30413 if (token->type == CPP_NAME)
30415 config_id = token->u.value;
30417 /* Either a named level, a concrete semantic, or an identifier
30418 for a postcondition. */
30419 const char *ident = IDENTIFIER_POINTER (token->u.value);
30420 level = map_contract_level (ident);
30421 semantic = map_contract_semantic (ident);
30423 /* The identifier is the return value for a postcondition. */
30424 if (level == CONTRACT_INVALID && semantic == CCS_INVALID
30425 && postcondition_p)
30426 return NULL_TREE;
30428 else if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
30430 config_id = get_identifier ("default");
30431 level = CONTRACT_DEFAULT;
30433 else
30435 /* We got some other token other than a ':'. */
30436 error_at (token->location, "expected contract semantic or level");
30437 return NULL_TREE;
30440 /* Consume the literal semantic or level token. */
30441 cp_lexer_consume_token (parser->lexer);
30443 if (semantic == CCS_INVALID && level == CONTRACT_INVALID)
30445 error_at (token->location,
30446 "expected contract level: "
30447 "%<default%>, %<audit%>, or %<axiom%>");
30448 return NULL_TREE;
30451 /* We matched an explicit semantic. */
30452 if (semantic != CCS_INVALID)
30454 if (cp_lexer_next_token_is (parser->lexer, CPP_MOD))
30456 error ("invalid use of contract role for explicit semantic");
30457 cp_lexer_consume_token (parser->lexer);
30458 cp_lexer_consume_token (parser->lexer);
30460 return config_id;
30463 /* We matched a level, there may be a role; otherwise this is default. */
30464 if (cp_lexer_next_token_is (parser->lexer, CPP_MOD))
30466 tree role_id = cp_parser_contract_role (parser);
30467 return build_tree_list (role_id, config_id);
30470 return build_tree_list (NULL_TREE, config_id);
30473 static tree
30474 find_error (tree *tp, int *, void *)
30476 if (*tp == error_mark_node)
30477 return *tp;
30478 return NULL_TREE;
30481 static bool
30482 contains_error_p (tree t)
30484 return walk_tree (&t, find_error, NULL, NULL);
30487 /* Parse a standard C++20 contract attribute specifier.
30489 contract-attribute-specifier:
30490 [ [ assert contract-level [opt] : conditional-expression ] ]
30491 [ [ pre contract-level [opt] : conditional-expression ] ]
30492 [ [ post contract-level [opt] identifier [opt] : conditional-expression ] ]
30494 For free functions, we cannot determine the type of the postcondition
30495 identifier because the we haven't called grokdeclarator yet. In those
30496 cases we parse the postcondition as if the identifier was declared as
30497 'auto <identifier>'. We then instantiate the postcondition once the
30498 return type is known.
30500 For member functions, contracts are in the complete-class context, so the
30501 parse is deferred. We also have the return type avaialable (unless it's
30502 deduced), so we don't need to parse the postcondition in terms of a
30503 placeholder. */
30505 static tree
30506 cp_parser_contract_attribute_spec (cp_parser *parser, tree attribute)
30508 gcc_assert (contract_attribute_p (attribute));
30509 cp_token *token = cp_lexer_consume_token (parser->lexer);
30510 location_t loc = token->location;
30512 bool assertion_p = is_attribute_p ("assert", attribute);
30513 bool postcondition_p = is_attribute_p ("post", attribute);
30515 /* Parse the optional mode. */
30516 tree mode = cp_parser_contract_mode_opt (parser, postcondition_p);
30518 /* Check for postcondition identifiers. */
30519 cp_expr identifier;
30520 if (postcondition_p && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30521 identifier = cp_parser_identifier (parser);
30522 if (identifier == error_mark_node)
30523 return error_mark_node;
30525 cp_parser_require (parser, CPP_COLON, RT_COLON);
30527 /* Defer the parsing of pre/post contracts inside class definitions. */
30528 tree contract;
30529 if (!assertion_p &&
30530 current_class_type &&
30531 TYPE_BEING_DEFINED (current_class_type))
30533 /* Skip until we reach an unenclose ']'. If we ran into an unnested ']'
30534 that doesn't close the attribute, return an error and let the attribute
30535 handling code emit an error for missing ']]'. */
30536 cp_token *first = cp_lexer_peek_token (parser->lexer);
30537 cp_parser_skip_to_closing_parenthesis_1 (parser,
30538 /*recovering=*/false,
30539 CPP_CLOSE_SQUARE,
30540 /*consume_paren=*/false);
30541 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE
30542 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_CLOSE_SQUARE)
30543 return error_mark_node;
30544 cp_token *last = cp_lexer_peek_token (parser->lexer);
30546 /* Build a deferred-parse node. */
30547 tree condition = make_node (DEFERRED_PARSE);
30548 DEFPARSE_TOKENS (condition) = cp_token_cache_new (first, last);
30549 DEFPARSE_INSTANTIATIONS (condition) = NULL;
30551 /* And its corresponding contract. */
30552 contract = grok_contract (attribute, mode, identifier, condition, loc);
30554 else
30556 /* Enable location wrappers when parsing contracts. */
30557 auto suppression = make_temp_override (suppress_location_wrappers, 0);
30559 /* Build a fake variable for the result identifier. */
30560 tree result = NULL_TREE;
30561 if (identifier)
30563 begin_scope (sk_block, NULL_TREE);
30564 result = make_postcondition_variable (identifier);
30565 ++processing_template_decl;
30568 /* Parse the condition, ensuring that parameters or the return variable
30569 aren't flagged for use outside the body of a function. */
30570 ++processing_contract_condition;
30571 cp_expr condition = cp_parser_conditional_expression (parser);
30572 --processing_contract_condition;
30574 /* Try to recover from errors by scanning up to the end of the
30575 attribute. Sometimes we get partially parsed expressions, so
30576 we need to search the condition for errors. */
30577 if (contains_error_p (condition))
30578 cp_parser_skip_up_to_closing_square_bracket (parser);
30580 /* Build the contract. */
30581 contract = grok_contract (attribute, mode, result, condition, loc);
30583 /* Leave our temporary scope for the postcondition result. */
30584 if (result)
30586 --processing_template_decl;
30587 pop_bindings_and_leave_scope ();
30591 if (!flag_contracts)
30593 error_at (loc, "contracts are only available with %<-fcontracts%>");
30594 return error_mark_node;
30597 return finish_contract_attribute (attribute, contract);
30600 /* Parse a contract condition for a deferred contract. */
30602 void cp_parser_late_contract_condition (cp_parser *parser,
30603 tree fn,
30604 tree attribute)
30606 tree contract = TREE_VALUE (TREE_VALUE (attribute));
30608 /* Make sure we've gotten something that hasn't been parsed yet or that
30609 we're not parsing an invalid contract. */
30610 tree condition = CONTRACT_CONDITION (contract);
30611 if (TREE_CODE (condition) != DEFERRED_PARSE)
30612 return;
30614 tree identifier = NULL_TREE;
30615 if (TREE_CODE (contract) == POSTCONDITION_STMT)
30616 identifier = POSTCONDITION_IDENTIFIER (contract);
30618 /* Build a fake variable for the result identifier. */
30619 tree result = NULL_TREE;
30620 if (identifier)
30622 /* TODO: Can we guarantee that the identifier has a location? */
30623 location_t loc = cp_expr_location (contract);
30624 tree type = TREE_TYPE (TREE_TYPE (fn));
30625 if (!check_postcondition_result (fn, type, loc))
30627 invalidate_contract (contract);
30628 return;
30631 begin_scope (sk_block, NULL_TREE);
30632 result = make_postcondition_variable (identifier, type);
30633 ++processing_template_decl;
30636 /* 'this' is not allowed in preconditions of constructors or in postconditions
30637 of destructors. Note that the previous value of this variable is
30638 established by the calling function, so we need to save it here. */
30639 tree saved_ccr = current_class_ref;
30640 tree saved_ccp = current_class_ptr;
30641 if ((DECL_CONSTRUCTOR_P (fn) && PRECONDITION_P (contract)) ||
30642 (DECL_DESTRUCTOR_P (fn) && POSTCONDITION_P (contract)))
30644 current_class_ref = current_class_ptr = NULL_TREE;
30645 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
30648 push_unparsed_function_queues (parser);
30650 /* Push the saved tokens onto the parser's lexer stack. */
30651 cp_token_cache *tokens = DEFPARSE_TOKENS (condition);
30652 cp_parser_push_lexer_for_tokens (parser, tokens);
30654 /* Parse the condition, ensuring that parameters or the return variable
30655 aren't flagged for use outside the body of a function. */
30656 ++processing_contract_condition;
30657 condition = cp_parser_conditional_expression (parser);
30658 --processing_contract_condition;
30660 /* Revert to the main lexer. */
30661 cp_parser_pop_lexer (parser);
30663 /* Restore the queue. */
30664 pop_unparsed_function_queues (parser);
30666 current_class_ref = saved_ccr;
30667 current_class_ptr = saved_ccp;
30669 /* Commit to changes. */
30670 update_late_contract (contract, result, condition);
30672 /* Leave our temporary scope for the postcondition result. */
30673 if (result)
30675 --processing_template_decl;
30676 pop_bindings_and_leave_scope ();
30680 /* Parse a standard C++-11 attribute specifier.
30682 attribute-specifier:
30683 [ [ attribute-using-prefix [opt] attribute-list ] ]
30684 contract-attribute-specifier
30685 alignment-specifier
30687 attribute-using-prefix:
30688 using attribute-namespace :
30690 alignment-specifier:
30691 alignas ( type-id ... [opt] )
30692 alignas ( alignment-expression ... [opt] ).
30694 Extensions for contracts:
30696 contract-attribute-specifier:
30697 [ [ assert : contract-mode [opt] : conditional-expression ] ]
30698 [ [ pre : contract-mode [opt] : conditional-expression ] ]
30699 [ [ post : contract-mode [opt] identifier [opt] :
30700 conditional-expression ] ]
30702 Return void_list_node if the current token doesn't start an
30703 attribute-specifier to differentiate from NULL_TREE returned e.g.
30704 for [ [ ] ]. */
30706 static tree
30707 cp_parser_std_attribute_spec (cp_parser *parser)
30709 tree attributes = NULL_TREE;
30710 cp_token *token = cp_lexer_peek_token (parser->lexer);
30712 if (token->type == CPP_OPEN_SQUARE
30713 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
30715 tree attr_ns = NULL_TREE;
30716 tree attr_name = NULL_TREE;
30718 cp_lexer_consume_token (parser->lexer);
30719 cp_lexer_consume_token (parser->lexer);
30721 token = cp_lexer_peek_token (parser->lexer);
30722 if (token->type == CPP_NAME)
30724 attr_name = token->u.value;
30725 attr_name = canonicalize_attr_name (attr_name);
30728 /* Handle contract-attribute-specs specially. */
30729 if (attr_name && contract_attribute_p (attr_name))
30731 tree attrs = cp_parser_contract_attribute_spec (parser, attr_name);
30732 if (attrs != error_mark_node)
30733 attributes = attrs;
30734 goto finish_attrs;
30737 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
30739 token = cp_lexer_peek_nth_token (parser->lexer, 2);
30740 if (token->type == CPP_NAME)
30741 attr_ns = token->u.value;
30742 else if (token->type == CPP_KEYWORD)
30743 attr_ns = ridpointers[(int) token->keyword];
30744 else if (token->flags & NAMED_OP)
30745 attr_ns = get_identifier (cpp_type2name (token->type,
30746 token->flags));
30747 if (attr_ns
30748 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
30750 if (cxx_dialect < cxx17)
30751 pedwarn (input_location, OPT_Wc__17_extensions,
30752 "attribute using prefix only available "
30753 "with %<-std=c++17%> or %<-std=gnu++17%>");
30755 cp_lexer_consume_token (parser->lexer);
30756 cp_lexer_consume_token (parser->lexer);
30757 cp_lexer_consume_token (parser->lexer);
30759 else
30760 attr_ns = NULL_TREE;
30763 attributes = cp_parser_std_attribute_list (parser, attr_ns);
30765 finish_attrs:
30766 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
30767 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
30768 cp_parser_skip_to_end_of_statement (parser);
30769 else
30770 /* Warn about parsing c++11 attribute in non-c++11 mode, only
30771 when we are sure that we have actually parsed them. */
30772 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
30774 else
30776 tree alignas_expr;
30778 /* Look for an alignment-specifier. */
30780 token = cp_lexer_peek_token (parser->lexer);
30782 if (token->type != CPP_KEYWORD
30783 || token->keyword != RID_ALIGNAS)
30784 return void_list_node;
30786 cp_lexer_consume_token (parser->lexer);
30787 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
30789 matching_parens parens;
30790 if (!parens.require_open (parser))
30791 return error_mark_node;
30793 cp_parser_parse_tentatively (parser);
30794 alignas_expr = cp_parser_type_id (parser);
30796 if (!cp_parser_parse_definitely (parser))
30798 alignas_expr = cp_parser_assignment_expression (parser);
30799 if (alignas_expr == error_mark_node)
30800 cp_parser_skip_to_end_of_statement (parser);
30801 if (alignas_expr == NULL_TREE
30802 || alignas_expr == error_mark_node)
30803 return alignas_expr;
30806 alignas_expr = cxx_alignas_expr (alignas_expr);
30807 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
30809 /* Handle alignas (pack...). */
30810 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30812 cp_lexer_consume_token (parser->lexer);
30813 alignas_expr = make_pack_expansion (alignas_expr);
30816 /* Something went wrong, so don't build the attribute. */
30817 if (alignas_expr == error_mark_node)
30818 return error_mark_node;
30820 /* Missing ')' means the code cannot possibly be valid; go ahead
30821 and commit to make sure we issue a hard error. */
30822 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
30823 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
30824 cp_parser_commit_to_tentative_parse (parser);
30826 if (!parens.require_close (parser))
30827 return error_mark_node;
30829 /* Build the C++-11 representation of an 'aligned'
30830 attribute. */
30831 attributes
30832 = build_tree_list (build_tree_list (gnu_identifier,
30833 aligned_identifier), alignas_expr);
30836 return attributes;
30839 /* Parse a standard C++-11 attribute-specifier-seq.
30841 attribute-specifier-seq:
30842 attribute-specifier-seq [opt] attribute-specifier */
30844 static tree
30845 cp_parser_std_attribute_spec_seq (cp_parser *parser)
30847 tree attr_specs = NULL_TREE;
30848 tree attr_last = NULL_TREE;
30850 /* Don't create wrapper nodes within attributes: the
30851 handlers don't know how to handle them. */
30852 auto_suppress_location_wrappers sentinel;
30854 while (true)
30856 tree attr_spec = cp_parser_std_attribute_spec (parser);
30857 if (attr_spec == void_list_node)
30858 break;
30859 /* Accept [[]][[]]; for which cp_parser_std_attribute_spec
30860 returns NULL_TREE as there are no attributes. */
30861 if (attr_spec == NULL_TREE)
30862 continue;
30863 if (attr_spec == error_mark_node)
30864 return error_mark_node;
30866 if (attr_last)
30867 TREE_CHAIN (attr_last) = attr_spec;
30868 else
30869 attr_specs = attr_last = attr_spec;
30870 attr_last = tree_last (attr_last);
30873 return attr_specs;
30876 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
30877 return index of the first token after balanced-token, or N on failure. */
30879 static size_t
30880 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
30882 size_t orig_n = n;
30883 int nparens = 0, nbraces = 0, nsquares = 0;
30885 switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
30887 case CPP_PRAGMA_EOL:
30888 if (!parser->lexer->in_pragma)
30889 break;
30890 /* FALLTHRU */
30891 case CPP_EOF:
30892 /* Ran out of tokens. */
30893 return orig_n;
30894 case CPP_OPEN_PAREN:
30895 ++nparens;
30896 break;
30897 case CPP_OPEN_BRACE:
30898 ++nbraces;
30899 break;
30900 case CPP_OPEN_SQUARE:
30901 ++nsquares;
30902 break;
30903 case CPP_CLOSE_PAREN:
30904 --nparens;
30905 break;
30906 case CPP_CLOSE_BRACE:
30907 --nbraces;
30908 break;
30909 case CPP_CLOSE_SQUARE:
30910 --nsquares;
30911 break;
30912 default:
30913 break;
30915 while (nparens || nbraces || nsquares);
30916 return n;
30919 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
30920 return index of the first token after the GNU attribute tokens, or N on
30921 failure. */
30923 static size_t
30924 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
30926 while (true)
30928 if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
30929 || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
30930 || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
30931 break;
30933 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
30934 if (n2 == n + 2)
30935 break;
30936 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
30937 break;
30938 n = n2 + 1;
30940 return n;
30943 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
30944 next token), return index of the first token after the standard C++11
30945 attribute tokens, or N on failure. */
30947 static size_t
30948 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
30950 while (true)
30952 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
30953 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
30955 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
30956 if (n2 == n + 1)
30957 break;
30958 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
30959 break;
30960 n = n2 + 1;
30962 else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
30963 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
30965 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
30966 if (n2 == n + 1)
30967 break;
30968 n = n2;
30970 else
30971 break;
30973 return n;
30976 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
30977 as the next token), return index of the first token after the attribute
30978 tokens, or N on failure. */
30980 static size_t
30981 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
30983 if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
30984 return cp_parser_skip_gnu_attributes_opt (parser, n);
30985 return cp_parser_skip_std_attribute_spec_seq (parser, n);
30988 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
30989 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
30990 current value of the PEDANTIC flag, regardless of whether or not
30991 the `__extension__' keyword is present. The caller is responsible
30992 for restoring the value of the PEDANTIC flag. */
30994 static bool
30995 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
30997 /* Save the old value of the PEDANTIC flag. */
30998 *saved_pedantic = pedantic;
31000 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
31002 /* Consume the `__extension__' token. */
31003 cp_lexer_consume_token (parser->lexer);
31004 /* We're not being pedantic while the `__extension__' keyword is
31005 in effect. */
31006 pedantic = 0;
31008 return true;
31011 return false;
31014 /* Parse a label declaration.
31016 label-declaration:
31017 __label__ label-declarator-seq ;
31019 label-declarator-seq:
31020 identifier , label-declarator-seq
31021 identifier */
31023 static void
31024 cp_parser_label_declaration (cp_parser* parser)
31026 /* Look for the `__label__' keyword. */
31027 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
31029 while (true)
31031 tree identifier;
31033 /* Look for an identifier. */
31034 identifier = cp_parser_identifier (parser);
31035 /* If we failed, stop. */
31036 if (identifier == error_mark_node)
31037 break;
31038 /* Declare it as a label. */
31039 finish_label_decl (identifier);
31040 /* If the next token is a `;', stop. */
31041 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31042 break;
31043 /* Look for the `,' separating the label declarations. */
31044 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
31047 /* Look for the final `;'. */
31048 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
31051 // -------------------------------------------------------------------------- //
31052 // Concept definitions
31054 static tree
31055 cp_parser_concept_definition (cp_parser *parser)
31057 /* A concept definition is an unevaluated context. */
31058 cp_unevaluated u;
31060 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_CONCEPT));
31061 cp_lexer_consume_token (parser->lexer);
31063 cp_expr id = cp_parser_identifier (parser);
31064 if (id == error_mark_node)
31066 cp_parser_skip_to_end_of_statement (parser);
31067 cp_parser_consume_semicolon_at_end_of_statement (parser);
31068 return NULL_TREE;
31071 tree attrs = cp_parser_attributes_opt (parser);
31073 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
31075 cp_parser_skip_to_end_of_statement (parser);
31076 cp_parser_consume_semicolon_at_end_of_statement (parser);
31077 return error_mark_node;
31080 processing_constraint_expression_sentinel parsing_constraint;
31081 tree init = cp_parser_constraint_expression (parser);
31082 if (init == error_mark_node)
31083 cp_parser_skip_to_end_of_statement (parser);
31085 /* Consume the trailing ';'. Diagnose the problem if it isn't there,
31086 but continue as if it were. */
31087 cp_parser_consume_semicolon_at_end_of_statement (parser);
31089 return finish_concept_definition (id, init, attrs);
31092 // -------------------------------------------------------------------------- //
31093 // Requires Clause
31095 /* Diagnose an expression that should appear in ()'s within a requires-clause
31096 and suggest where to place those parentheses. */
31098 static void
31099 cp_parser_diagnose_ungrouped_constraint_plain (location_t loc)
31101 error_at (loc, "expression must be enclosed in parentheses");
31104 static void
31105 cp_parser_diagnose_ungrouped_constraint_rich (location_t loc)
31107 gcc_rich_location richloc (loc);
31108 richloc.add_fixit_insert_before ("(");
31109 richloc.add_fixit_insert_after (")");
31110 error_at (&richloc, "expression must be enclosed in parentheses");
31113 /* Characterizes the likely kind of expression intended by a mis-written
31114 primary constraint. */
31115 enum primary_constraint_error
31117 pce_ok,
31118 pce_maybe_operator,
31119 pce_maybe_postfix
31122 /* Returns true if the token(s) following a primary-expression in a
31123 constraint-logical-* expression would require parentheses. */
31125 static primary_constraint_error
31126 cp_parser_constraint_requires_parens (cp_parser *parser, bool lambda_p)
31128 cp_token *token = cp_lexer_peek_token (parser->lexer);
31129 switch (token->type)
31131 default:
31132 return pce_ok;
31134 case CPP_EQ:
31136 /* An equal sign may be part of the definition of a function,
31137 and not an assignment operator, when parsing the expression
31138 for a trailing requires-clause. For example:
31140 template<typename T>
31141 struct S {
31142 S() requires C<T> = default;
31145 Don't try to reparse this a binary operator. */
31146 if (cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_DELETE)
31147 || cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_DEFAULT))
31148 return pce_ok;
31150 gcc_fallthrough ();
31153 /* Arithmetic operators. */
31154 case CPP_PLUS:
31155 case CPP_MINUS:
31156 case CPP_MULT:
31157 case CPP_DIV:
31158 case CPP_MOD:
31159 /* Bitwise operators. */
31160 case CPP_AND:
31161 case CPP_OR:
31162 case CPP_XOR:
31163 case CPP_RSHIFT:
31164 case CPP_LSHIFT:
31165 /* Relational operators. */
31166 case CPP_EQ_EQ:
31167 case CPP_NOT_EQ:
31168 case CPP_LESS:
31169 case CPP_GREATER:
31170 case CPP_LESS_EQ:
31171 case CPP_GREATER_EQ:
31172 case CPP_SPACESHIP:
31173 /* Pointer-to-member. */
31174 case CPP_DOT_STAR:
31175 case CPP_DEREF_STAR:
31176 /* Assignment operators. */
31177 case CPP_PLUS_EQ:
31178 case CPP_MINUS_EQ:
31179 case CPP_MULT_EQ:
31180 case CPP_DIV_EQ:
31181 case CPP_MOD_EQ:
31182 case CPP_AND_EQ:
31183 case CPP_OR_EQ:
31184 case CPP_XOR_EQ:
31185 case CPP_RSHIFT_EQ:
31186 case CPP_LSHIFT_EQ:
31187 /* Conditional operator */
31188 case CPP_QUERY:
31189 /* Unenclosed binary or conditional operator. */
31190 return pce_maybe_operator;
31192 case CPP_OPEN_PAREN:
31194 /* A primary constraint that precedes the parameter-list of a
31195 lambda expression is followed by an open paren.
31197 []<typename T> requires C (T a, T b) { ... }
31199 Don't try to re-parse this as a postfix expression. */
31200 if (lambda_p)
31201 return pce_ok;
31203 gcc_fallthrough ();
31205 case CPP_OPEN_SQUARE:
31207 /* A primary-constraint-expression followed by a '[[' is not a
31208 postfix expression. */
31209 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE))
31210 return pce_ok;
31212 gcc_fallthrough ();
31214 case CPP_PLUS_PLUS:
31215 case CPP_MINUS_MINUS:
31216 case CPP_DOT:
31217 /* Unenclosed postfix operator. */
31218 return pce_maybe_postfix;
31220 case CPP_DEREF:
31221 /* A primary constraint that precedes the lambda-declarator of a
31222 lambda expression is followed by trailing return type.
31224 []<typename T> requires C -> void {}
31226 Don't try to re-parse this as a postfix expression in
31227 C++23 and later. In C++20 ( needs to come in between but we
31228 allow it to be omitted with pedwarn. */
31229 if (lambda_p)
31230 return pce_ok;
31231 /* Unenclosed postfix operator. */
31232 return pce_maybe_postfix;
31236 /* Returns true if the next token begins a unary expression, preceded by
31237 an operator or keyword. */
31239 static bool
31240 cp_parser_unary_constraint_requires_parens (cp_parser *parser)
31242 cp_token *token = cp_lexer_peek_token (parser->lexer);
31243 switch (token->type)
31245 case CPP_NOT:
31246 case CPP_PLUS:
31247 case CPP_MINUS:
31248 case CPP_MULT:
31249 case CPP_COMPL:
31250 case CPP_PLUS_PLUS:
31251 case CPP_MINUS_MINUS:
31252 return true;
31254 case CPP_KEYWORD:
31256 switch (token->keyword)
31258 case RID_STATCAST:
31259 case RID_DYNCAST:
31260 case RID_REINTCAST:
31261 case RID_CONSTCAST:
31262 case RID_TYPEID:
31263 case RID_SIZEOF:
31264 case RID_ALIGNOF:
31265 case RID_NOEXCEPT:
31266 case RID_NEW:
31267 case RID_DELETE:
31268 case RID_THROW:
31269 return true;
31271 default:
31272 break;
31276 default:
31277 break;
31280 return false;
31283 /* Parse a primary expression within a constraint. */
31285 static cp_expr
31286 cp_parser_constraint_primary_expression (cp_parser *parser, bool lambda_p)
31288 /* If this looks like a unary expression, parse it as such, but diagnose
31289 it as ill-formed; it requires parens. */
31290 if (cp_parser_unary_constraint_requires_parens (parser))
31292 cp_expr e = cp_parser_assignment_expression (parser, NULL, false, false);
31293 cp_parser_diagnose_ungrouped_constraint_rich (e.get_location());
31294 return e;
31297 cp_lexer_save_tokens (parser->lexer);
31298 cp_id_kind idk;
31299 location_t loc = input_location;
31300 cp_expr expr = cp_parser_primary_expression (parser,
31301 /*address_p=*/false,
31302 /*cast_p=*/false,
31303 /*template_arg_p=*/false,
31304 &idk);
31305 expr.maybe_add_location_wrapper ();
31307 primary_constraint_error pce = pce_ok;
31308 if (expr != error_mark_node)
31310 /* The primary-expression could be part of an unenclosed non-logical
31311 compound expression. */
31312 pce = cp_parser_constraint_requires_parens (parser, lambda_p);
31314 if (pce == pce_ok)
31316 cp_lexer_commit_tokens (parser->lexer);
31317 return finish_constraint_primary_expr (expr);
31320 /* Retry the parse at a lower precedence. If that succeeds, diagnose the
31321 error, but return the expression as if it were valid. */
31322 cp_lexer_rollback_tokens (parser->lexer);
31323 cp_parser_parse_tentatively (parser);
31324 if (pce == pce_maybe_operator)
31325 expr = cp_parser_assignment_expression (parser, NULL, false, false);
31326 else
31327 expr = cp_parser_simple_cast_expression (parser);
31328 if (cp_parser_parse_definitely (parser))
31330 cp_parser_diagnose_ungrouped_constraint_rich (expr.get_location());
31331 return expr;
31334 /* Otherwise, something has gone very wrong, and we can't generate a more
31335 meaningful diagnostic or recover. */
31336 cp_parser_diagnose_ungrouped_constraint_plain (loc);
31337 return error_mark_node;
31340 /* Parse a constraint-logical-and-expression.
31342 constraint-logical-and-expression:
31343 primary-expression
31344 constraint-logical-and-expression '&&' primary-expression */
31346 static cp_expr
31347 cp_parser_constraint_logical_and_expression (cp_parser *parser, bool lambda_p)
31349 cp_expr lhs = cp_parser_constraint_primary_expression (parser, lambda_p);
31350 while (cp_lexer_next_token_is (parser->lexer, CPP_AND_AND))
31352 cp_token *op = cp_lexer_consume_token (parser->lexer);
31353 tree rhs = cp_parser_constraint_primary_expression (parser, lambda_p);
31354 lhs = finish_constraint_and_expr (op->location, lhs, rhs);
31356 return lhs;
31359 /* Parse a constraint-logical-or-expression.
31361 constraint-logical-or-expression:
31362 constraint-logical-and-expression
31363 constraint-logical-or-expression '||' constraint-logical-and-expression */
31365 static cp_expr
31366 cp_parser_constraint_logical_or_expression (cp_parser *parser, bool lambda_p)
31368 cp_expr lhs = cp_parser_constraint_logical_and_expression (parser, lambda_p);
31369 while (cp_lexer_next_token_is (parser->lexer, CPP_OR_OR))
31371 cp_token *op = cp_lexer_consume_token (parser->lexer);
31372 cp_expr rhs = cp_parser_constraint_logical_and_expression (parser, lambda_p);
31373 lhs = finish_constraint_or_expr (op->location, lhs, rhs);
31375 return lhs;
31378 /* Parse the expression after a requires-clause. This has a different grammar
31379 than that in the concepts TS. */
31381 static tree
31382 cp_parser_requires_clause_expression (cp_parser *parser, bool lambda_p)
31384 processing_constraint_expression_sentinel parsing_constraint;
31385 ++processing_template_decl;
31386 cp_expr expr = cp_parser_constraint_logical_or_expression (parser, lambda_p);
31387 --processing_template_decl;
31388 if (check_for_bare_parameter_packs (expr))
31389 expr = error_mark_node;
31390 return expr;
31393 /* Parse a expression after a requires clause.
31395 constraint-expression:
31396 logical-or-expression
31398 The required logical-or-expression must be a constant expression. Note
31399 that we don't check that the expression is constepxr here. We defer until
31400 we analyze constraints and then, we only check atomic constraints. */
31402 static tree
31403 cp_parser_constraint_expression (cp_parser *parser)
31405 processing_constraint_expression_sentinel parsing_constraint;
31406 ++processing_template_decl;
31407 cp_expr expr = cp_parser_binary_expression (parser, false, true,
31408 PREC_NOT_OPERATOR, NULL);
31409 --processing_template_decl;
31410 if (check_for_bare_parameter_packs (expr))
31411 expr = error_mark_node;
31412 expr.maybe_add_location_wrapper ();
31413 return expr;
31416 /* Optionally parse a requires clause:
31418 requires-clause:
31419 `requires` constraint-logical-or-expression.
31420 [ConceptsTS]
31421 `requires constraint-expression.
31423 LAMBDA_P is true when the requires-clause is parsed before the
31424 parameter-list of a lambda-declarator. */
31426 static tree
31427 cp_parser_requires_clause_opt (cp_parser *parser, bool lambda_p)
31429 /* A requires clause is an unevaluated context. */
31430 cp_unevaluated u;
31432 cp_token *tok = cp_lexer_peek_token (parser->lexer);
31433 if (tok->keyword != RID_REQUIRES)
31435 if (!flag_concepts && tok->type == CPP_NAME
31436 && tok->u.value == ridpointers[RID_REQUIRES])
31438 error_at (cp_lexer_peek_token (parser->lexer)->location,
31439 "%<requires%> only available with "
31440 "%<-std=c++20%> or %<-fconcepts%>");
31441 /* Parse and discard the requires-clause. */
31442 cp_lexer_consume_token (parser->lexer);
31443 cp_parser_constraint_expression (parser);
31445 return NULL_TREE;
31448 cp_token *tok2 = cp_lexer_peek_nth_token (parser->lexer, 2);
31449 if (tok2->type == CPP_OPEN_BRACE)
31451 /* An opening brace following the start of a requires-clause is
31452 ill-formed; the user likely forgot the second `requires' that
31453 would start a requires-expression. */
31454 gcc_rich_location richloc (tok2->location);
31455 richloc.add_fixit_insert_after (tok->location, " requires");
31456 error_at (&richloc, "missing additional %<requires%> to start "
31457 "a requires-expression");
31458 /* Don't consume the `requires', so that it's reused as the start of a
31459 requires-expression. */
31461 else
31462 cp_lexer_consume_token (parser->lexer);
31464 if (!flag_concepts_ts)
31465 return cp_parser_requires_clause_expression (parser, lambda_p);
31466 else
31467 return cp_parser_constraint_expression (parser);
31470 /*---------------------------------------------------------------------------
31471 Requires expressions
31472 ---------------------------------------------------------------------------*/
31474 /* Parse a requires expression
31476 requirement-expression:
31477 'requires' requirement-parameter-list [opt] requirement-body */
31479 static tree
31480 cp_parser_requires_expression (cp_parser *parser)
31482 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
31483 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
31485 /* Avoid committing to outer tentative parse. */
31486 tentative_firewall firewall (parser);
31488 /* This is definitely a requires-expression. */
31489 cp_parser_commit_to_tentative_parse (parser);
31491 tree parms, reqs;
31493 /* Local parameters are delared as variables within the scope
31494 of the expression. They are not visible past the end of
31495 the expression. Expressions within the requires-expression
31496 are unevaluated. */
31497 struct scope_sentinel
31499 scope_sentinel ()
31501 ++cp_unevaluated_operand;
31502 begin_scope (sk_function_parms, NULL_TREE);
31503 current_binding_level->requires_expression = true;
31506 ~scope_sentinel ()
31508 pop_bindings_and_leave_scope ();
31509 --cp_unevaluated_operand;
31511 } s;
31513 /* Parse the optional parameter list. */
31514 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31516 parms = cp_parser_requirement_parameter_list (parser);
31517 if (parms == error_mark_node)
31518 return error_mark_node;
31520 else
31521 parms = NULL_TREE;
31523 /* Parse the requirement body. */
31524 ++processing_template_decl;
31525 reqs = cp_parser_requirement_body (parser);
31526 --processing_template_decl;
31527 if (reqs == error_mark_node)
31528 return error_mark_node;
31531 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
31532 the parm chain. */
31533 grokparms (parms, &parms);
31534 loc = make_location (loc, loc, parser->lexer);
31535 tree expr = finish_requires_expr (loc, parms, reqs);
31536 if (!processing_template_decl)
31538 /* Perform semantic processing now to diagnose any invalid types and
31539 expressions. */
31540 int saved_errorcount = errorcount;
31541 tsubst_requires_expr (expr, NULL_TREE, tf_warning_or_error, NULL_TREE);
31542 if (errorcount > saved_errorcount)
31543 return error_mark_node;
31545 return expr;
31548 /* Parse a parameterized requirement.
31550 requirement-parameter-list:
31551 '(' parameter-declaration-clause ')' */
31553 static tree
31554 cp_parser_requirement_parameter_list (cp_parser *parser)
31556 matching_parens parens;
31557 if (!parens.require_open (parser))
31558 return error_mark_node;
31560 tree parms = (cp_parser_parameter_declaration_clause
31561 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
31563 if (!parens.require_close (parser))
31564 return error_mark_node;
31566 /* Modify the declared parameters by removing their context
31567 so they don't refer to the enclosing scope and explicitly
31568 indicating that they are constraint variables. */
31569 for (tree parm = parms; parm; parm = TREE_CHAIN (parm))
31571 if (parm == void_list_node || parm == explicit_void_list_node)
31572 break;
31573 tree decl = TREE_VALUE (parm);
31574 if (decl != error_mark_node)
31576 DECL_CONTEXT (decl) = NULL_TREE;
31577 CONSTRAINT_VAR_P (decl) = true;
31581 return parms;
31584 /* Parse the body of a requirement.
31586 requirement-body:
31587 '{' requirement-list '}' */
31588 static tree
31589 cp_parser_requirement_body (cp_parser *parser)
31591 matching_braces braces;
31592 if (!braces.require_open (parser))
31593 return error_mark_node;
31595 tree reqs = cp_parser_requirement_seq (parser);
31597 if (!braces.require_close (parser))
31598 return error_mark_node;
31600 return reqs;
31603 /* Parse a sequence of requirements.
31605 requirement-seq:
31606 requirement
31607 requirement-seq requirement */
31609 static tree
31610 cp_parser_requirement_seq (cp_parser *parser)
31612 tree result = NULL_TREE;
31615 tree req = cp_parser_requirement (parser);
31616 if (req != error_mark_node)
31617 result = tree_cons (NULL_TREE, req, result);
31619 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE)
31620 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF));
31622 /* If there are no valid requirements, this is not a valid expression. */
31623 if (!result)
31624 return error_mark_node;
31626 /* Reverse the order of requirements so they are analyzed in order. */
31627 return nreverse (result);
31630 /* Parse a syntactic requirement or type requirement.
31632 requirement:
31633 simple-requirement
31634 compound-requirement
31635 type-requirement
31636 nested-requirement */
31638 static tree
31639 cp_parser_requirement (cp_parser *parser)
31641 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
31642 return cp_parser_compound_requirement (parser);
31643 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
31645 /* It's probably a type-requirement. */
31646 cp_parser_parse_tentatively (parser);
31647 tree req = cp_parser_type_requirement (parser);
31648 if (cp_parser_parse_definitely (parser))
31649 return req;
31650 /* No, maybe it's something like typename T::type(); */
31651 cp_parser_parse_tentatively (parser);
31652 req = cp_parser_simple_requirement (parser);
31653 if (cp_parser_parse_definitely (parser))
31654 return req;
31655 /* Non-tentative for the error. */
31656 return cp_parser_type_requirement (parser);
31658 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
31659 return cp_parser_nested_requirement (parser);
31660 else
31661 return cp_parser_simple_requirement (parser);
31664 /* Parse a simple requirement.
31666 simple-requirement:
31667 expression ';' */
31669 static tree
31670 cp_parser_simple_requirement (cp_parser *parser)
31672 location_t start = cp_lexer_peek_token (parser->lexer)->location;
31673 cp_expr expr = cp_parser_expression (parser, NULL, false, false);
31674 if (expr == error_mark_node)
31675 cp_parser_skip_to_end_of_statement (parser);
31677 cp_parser_consume_semicolon_at_end_of_statement (parser);
31679 if (!expr || expr == error_mark_node)
31680 return error_mark_node;
31682 /* Sometimes we don't get locations, so use the cached token location
31683 as a reasonable approximation. */
31684 if (expr.get_location() == UNKNOWN_LOCATION)
31685 expr.set_location (start);
31687 for (tree t = expr; ; )
31689 if (TREE_CODE (t) == TRUTH_ANDIF_EXPR
31690 || TREE_CODE (t) == TRUTH_ORIF_EXPR)
31692 t = TREE_OPERAND (t, 0);
31693 continue;
31695 if (concept_check_p (t))
31697 gcc_rich_location richloc (get_start (start));
31698 richloc.add_fixit_insert_before (start, "requires ");
31699 warning_at (&richloc, OPT_Wmissing_requires, "testing "
31700 "if a concept-id is a valid expression; add "
31701 "%<requires%> to check satisfaction");
31703 break;
31706 return finish_simple_requirement (expr.get_location (), expr);
31709 /* Parse a type requirement
31711 type-requirement
31712 nested-name-specifier [opt] required-type-name ';'
31714 required-type-name:
31715 type-name
31716 'template' [opt] simple-template-id */
31718 static tree
31719 cp_parser_type_requirement (cp_parser *parser)
31721 cp_token *start_tok = cp_lexer_consume_token (parser->lexer);
31722 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31724 // Save the scope before parsing name specifiers.
31725 tree saved_scope = parser->scope;
31726 tree saved_object_scope = parser->object_scope;
31727 tree saved_qualifying_scope = parser->qualifying_scope;
31728 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
31729 cp_parser_nested_name_specifier_opt (parser,
31730 /*typename_keyword_p=*/true,
31731 /*check_dependency_p=*/true,
31732 /*type_p=*/true,
31733 /*is_declaration=*/false);
31735 tree type;
31736 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
31738 cp_lexer_consume_token (parser->lexer);
31739 type = cp_parser_template_id (parser,
31740 /*template_keyword_p=*/true,
31741 /*check_dependency_p=*/true,
31742 /*tag_type=*/none_type,
31743 /*is_declaration=*/false);
31744 type = make_typename_type (parser->scope, type, typename_type,
31745 /*complain=*/tf_error);
31747 else
31748 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
31750 if (TREE_CODE (type) == TYPE_DECL)
31751 type = TREE_TYPE (type);
31753 parser->scope = saved_scope;
31754 parser->object_scope = saved_object_scope;
31755 parser->qualifying_scope = saved_qualifying_scope;
31757 if (type == error_mark_node)
31758 cp_parser_skip_to_end_of_statement (parser);
31760 cp_parser_consume_semicolon_at_end_of_statement (parser);
31762 if (type == error_mark_node)
31763 return error_mark_node;
31765 loc = make_location (loc, start_tok->location, parser->lexer);
31766 return finish_type_requirement (loc, type);
31769 /* Parse a compound requirement
31771 compound-requirement:
31772 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
31774 static tree
31775 cp_parser_compound_requirement (cp_parser *parser)
31777 /* Parse an expression enclosed in '{ }'s. */
31778 matching_braces braces;
31779 if (!braces.require_open (parser))
31780 return error_mark_node;
31782 cp_token *expr_token = cp_lexer_peek_token (parser->lexer);
31784 tree expr = cp_parser_expression (parser, NULL, false, false);
31785 if (expr == error_mark_node)
31786 cp_parser_skip_to_closing_brace (parser);
31788 if (!braces.require_close (parser))
31790 cp_parser_skip_to_end_of_statement (parser);
31791 cp_parser_consume_semicolon_at_end_of_statement (parser);
31792 return error_mark_node;
31795 /* If the expression was invalid, skip the remainder of the requirement. */
31796 if (!expr || expr == error_mark_node)
31798 cp_parser_skip_to_end_of_statement (parser);
31799 cp_parser_consume_semicolon_at_end_of_statement (parser);
31800 return error_mark_node;
31803 /* Parse the optional noexcept. */
31804 bool noexcept_p = false;
31805 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
31807 cp_lexer_consume_token (parser->lexer);
31808 noexcept_p = true;
31811 /* Parse the optional trailing return type. */
31812 tree type = NULL_TREE;
31813 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
31815 cp_lexer_consume_token (parser->lexer);
31816 cp_token *tok = cp_lexer_peek_token (parser->lexer);
31818 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
31819 parser->in_result_type_constraint_p = true;
31820 /* C++20 allows either a type-id or a type-constraint. Parsing
31821 a type-id will subsume the parsing for a type-constraint but
31822 allow for more syntactic forms (e.g., const C<T>*). */
31823 type = cp_parser_trailing_type_id (parser);
31824 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
31825 if (type == error_mark_node)
31826 return error_mark_node;
31828 location_t type_loc = make_location (tok->location, tok->location,
31829 parser->lexer);
31831 /* Check that we haven't written something like 'const C<T>*'. */
31832 if (type_uses_auto (type))
31834 if (!is_auto (type))
31836 error_at (type_loc,
31837 "result type is not a plain type-constraint");
31838 cp_parser_consume_semicolon_at_end_of_statement (parser);
31839 return error_mark_node;
31842 else if (!flag_concepts_ts)
31843 /* P1452R2 removed the trailing-return-type option. */
31844 error_at (type_loc,
31845 "return-type-requirement is not a type-constraint");
31848 location_t loc = make_location (expr_token->location,
31849 braces.open_location (),
31850 parser->lexer);
31852 cp_parser_consume_semicolon_at_end_of_statement (parser);
31854 if (expr == error_mark_node || type == error_mark_node)
31855 return error_mark_node;
31857 return finish_compound_requirement (loc, expr, type, noexcept_p);
31860 /* Parse a nested requirement. This is the same as a requires clause.
31862 nested-requirement:
31863 requires-clause */
31865 static tree
31866 cp_parser_nested_requirement (cp_parser *parser)
31868 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
31869 cp_token *tok = cp_lexer_consume_token (parser->lexer);
31870 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31871 tree req = cp_parser_constraint_expression (parser);
31872 if (req == error_mark_node)
31873 cp_parser_skip_to_end_of_statement (parser);
31874 loc = make_location (loc, tok->location, parser->lexer);
31875 cp_parser_consume_semicolon_at_end_of_statement (parser);
31876 if (req == error_mark_node)
31877 return error_mark_node;
31878 return finish_nested_requirement (loc, req);
31881 /* Support Functions */
31883 /* Return the appropriate prefer_type argument for lookup_name based on
31884 tag_type. */
31886 static inline LOOK_want
31887 prefer_type_arg (tag_types tag_type)
31889 switch (tag_type)
31891 case none_type: return LOOK_want::NORMAL; // No preference.
31892 case scope_type: return LOOK_want::TYPE_NAMESPACE; // Type or namespace.
31893 default: return LOOK_want::TYPE; // Type only.
31897 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
31898 NAME should have one of the representations used for an
31899 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
31900 is returned. If PARSER->SCOPE is a dependent type, then a
31901 SCOPE_REF is returned.
31903 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
31904 returned; the name was already resolved when the TEMPLATE_ID_EXPR
31905 was formed. Abstractly, such entities should not be passed to this
31906 function, because they do not need to be looked up, but it is
31907 simpler to check for this special case here, rather than at the
31908 call-sites.
31910 In cases not explicitly covered above, this function returns a
31911 DECL, OVERLOAD, or baselink representing the result of the lookup.
31912 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
31913 is returned.
31915 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
31916 (e.g., "struct") that was used. In that case bindings that do not
31917 refer to types are ignored.
31919 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
31920 ignored. If IS_TEMPLATE IS 2, the 'template' keyword was specified.
31922 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
31923 are ignored.
31925 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
31926 types.
31928 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
31929 TREE_LIST of candidates if name-lookup results in an ambiguity, and
31930 NULL_TREE otherwise. */
31932 static cp_expr
31933 cp_parser_lookup_name (cp_parser *parser, tree name,
31934 enum tag_types tag_type,
31935 int is_template,
31936 bool is_namespace,
31937 bool check_dependency,
31938 tree *ambiguous_decls,
31939 location_t name_location)
31941 tree decl;
31942 tree object_type = parser->context->object_type;
31944 /* Assume that the lookup will be unambiguous. */
31945 if (ambiguous_decls)
31946 *ambiguous_decls = NULL_TREE;
31948 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
31949 no longer valid. Note that if we are parsing tentatively, and
31950 the parse fails, OBJECT_TYPE will be automatically restored. */
31951 parser->context->object_type = NULL_TREE;
31953 if (name == error_mark_node)
31954 return error_mark_node;
31956 /* A template-id has already been resolved; there is no lookup to
31957 do. */
31958 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
31959 return name;
31960 if (BASELINK_P (name))
31962 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
31963 == TEMPLATE_ID_EXPR);
31964 return name;
31967 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
31968 it should already have been checked to make sure that the name
31969 used matches the type being destroyed. */
31970 if (TREE_CODE (name) == BIT_NOT_EXPR)
31972 tree type;
31974 /* Figure out to which type this destructor applies. */
31975 if (parser->scope)
31976 type = parser->scope;
31977 else if (object_type)
31978 type = object_type;
31979 else
31980 type = current_class_type;
31981 /* If that's not a class type, there is no destructor. */
31982 if (!type || !CLASS_TYPE_P (type))
31983 return error_mark_node;
31985 /* In a non-static member function, check implicit this->. */
31986 if (current_class_ref)
31987 return lookup_destructor (current_class_ref, parser->scope, name,
31988 tf_warning_or_error);
31990 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
31991 lazily_declare_fn (sfk_destructor, type);
31993 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
31994 return dtor;
31996 return error_mark_node;
31999 /* By this point, the NAME should be an ordinary identifier. If
32000 the id-expression was a qualified name, the qualifying scope is
32001 stored in PARSER->SCOPE at this point. */
32002 gcc_assert (identifier_p (name));
32004 /* Perform the lookup. */
32005 if (parser->scope)
32007 bool dependent_p;
32009 if (parser->scope == error_mark_node)
32010 return error_mark_node;
32012 /* If the SCOPE is dependent, the lookup must be deferred until
32013 the template is instantiated -- unless we are explicitly
32014 looking up names in uninstantiated templates. Even then, we
32015 cannot look up the name if the scope is not a class type; it
32016 might, for example, be a template type parameter. */
32017 dependent_p = (TYPE_P (parser->scope)
32018 && dependent_scope_p (parser->scope));
32019 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
32020 && dependent_p)
32021 /* Defer lookup. */
32022 decl = error_mark_node;
32023 else
32025 tree pushed_scope = NULL_TREE;
32027 /* If PARSER->SCOPE is a dependent type, then it must be a
32028 class type, and we must not be checking dependencies;
32029 otherwise, we would have processed this lookup above. So
32030 that PARSER->SCOPE is not considered a dependent base by
32031 lookup_member, we must enter the scope here. */
32032 if (dependent_p)
32033 pushed_scope = push_scope (parser->scope);
32035 /* If the PARSER->SCOPE is a template specialization, it
32036 may be instantiated during name lookup. In that case,
32037 errors may be issued. Even if we rollback the current
32038 tentative parse, those errors are valid. */
32039 decl = lookup_qualified_name (parser->scope, name,
32040 prefer_type_arg (tag_type),
32041 /*complain=*/true);
32043 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
32044 lookup result and the nested-name-specifier nominates a class C:
32045 * if the name specified after the nested-name-specifier, when
32046 looked up in C, is the injected-class-name of C (Clause 9), or
32047 * if the name specified after the nested-name-specifier is the
32048 same as the identifier or the simple-template-id's template-
32049 name in the last component of the nested-name-specifier,
32050 the name is instead considered to name the constructor of
32051 class C. [ Note: for example, the constructor is not an
32052 acceptable lookup result in an elaborated-type-specifier so
32053 the constructor would not be used in place of the
32054 injected-class-name. --end note ] Such a constructor name
32055 shall be used only in the declarator-id of a declaration that
32056 names a constructor or in a using-declaration. */
32057 if (tag_type == none_type
32058 && DECL_SELF_REFERENCE_P (decl)
32059 && same_type_p (DECL_CONTEXT (decl), parser->scope))
32060 decl = lookup_qualified_name (parser->scope, ctor_identifier,
32061 prefer_type_arg (tag_type),
32062 /*complain=*/true);
32064 if (pushed_scope)
32065 pop_scope (pushed_scope);
32068 /* If the scope is a dependent type and either we deferred lookup or
32069 we did lookup but didn't find the name, rememeber the name. */
32070 if (decl == error_mark_node && TYPE_P (parser->scope)
32071 && dependent_type_p (parser->scope))
32073 if (tag_type)
32075 tree type;
32077 /* The resolution to Core Issue 180 says that `struct
32078 A::B' should be considered a type-name, even if `A'
32079 is dependent. */
32080 type = make_typename_type (parser->scope, name, tag_type,
32081 /*complain=*/tf_error);
32082 if (type != error_mark_node)
32083 decl = TYPE_NAME (type);
32085 else if (is_template
32086 && (cp_parser_next_token_ends_template_argument_p (parser)
32087 || cp_lexer_next_token_is (parser->lexer,
32088 CPP_CLOSE_PAREN)))
32089 decl = make_unbound_class_template (parser->scope,
32090 name, NULL_TREE,
32091 /*complain=*/tf_error);
32092 else
32093 decl = build_qualified_name (/*type=*/NULL_TREE,
32094 parser->scope, name,
32095 is_template);
32097 parser->qualifying_scope = parser->scope;
32098 parser->object_scope = NULL_TREE;
32100 else if (object_type)
32102 bool dep = dependent_scope_p (object_type);
32104 /* Look up the name in the scope of the OBJECT_TYPE, unless the
32105 OBJECT_TYPE is not a class. */
32106 if (!dep && CLASS_TYPE_P (object_type))
32107 /* If the OBJECT_TYPE is a template specialization, it may
32108 be instantiated during name lookup. In that case, errors
32109 may be issued. Even if we rollback the current tentative
32110 parse, those errors are valid. */
32111 decl = lookup_member (object_type,
32112 name,
32113 /*protect=*/0,
32114 /*prefer_type=*/tag_type != none_type,
32115 tf_warning_or_error);
32116 else
32117 decl = NULL_TREE;
32119 /* If we didn't find a member and have dependent bases, the member lookup
32120 is now dependent. */
32121 if (!dep && !decl && any_dependent_bases_p (object_type))
32122 dep = true;
32124 if (dep && is_template == 2)
32125 /* The template keyword specifies a dependent template. */;
32126 else if (!decl)
32127 /* Look it up in the enclosing context. DR 141: When looking for a
32128 template-name after -> or ., only consider class templates. */
32129 decl = lookup_name (name, is_namespace ? LOOK_want::NAMESPACE
32130 /* DR 141: When looking in the
32131 current enclosing context for a
32132 template-name after -> or ., only
32133 consider class templates. */
32134 : is_template ? LOOK_want::TYPE
32135 : prefer_type_arg (tag_type));
32137 /* If we did unqualified lookup of a dependent member-qualified name and
32138 found something, do we want to use it? P1787 clarified that we need
32139 to look in the object scope first even if it's dependent, but for now
32140 let's still use it in some cases.
32141 FIXME remember unqualified lookup result to use if member lookup fails
32142 at instantiation time. */
32143 if (decl && dep && is_template)
32145 saved_token_sentinel toks (parser->lexer, STS_ROLLBACK);
32146 /* Only use the unqualified class template lookup if we're actually
32147 looking at a template arg list. */
32148 if (!cp_parser_skip_entire_template_parameter_list (parser))
32149 decl = NULL_TREE;
32152 /* If we know we're looking for a type (e.g. A in p->A::x),
32153 mock up a typename. */
32154 if (!decl && dep && tag_type != none_type)
32156 tree type = build_typename_type (object_type, name, name,
32157 typename_type);
32158 decl = TYPE_NAME (type);
32161 parser->object_scope = object_type;
32162 parser->qualifying_scope = NULL_TREE;
32164 else
32166 decl = lookup_name (name, is_namespace ? LOOK_want::NAMESPACE
32167 : prefer_type_arg (tag_type));
32168 parser->qualifying_scope = NULL_TREE;
32169 parser->object_scope = NULL_TREE;
32172 /* If the lookup failed, let our caller know. */
32173 if (!decl || decl == error_mark_node)
32174 return error_mark_node;
32176 /* If we have resolved the name of a member declaration, check to
32177 see if the declaration is accessible. When the name resolves to
32178 set of overloaded functions, accessibility is checked when
32179 overload resolution is done. If we have a TREE_LIST, then the lookup
32180 is either ambiguous or it found multiple injected-class-names, the
32181 accessibility of which is trivially satisfied.
32183 During an explicit instantiation, access is not checked at all,
32184 as per [temp.explicit]. */
32185 if (DECL_P (decl))
32186 check_accessibility_of_qualified_id (decl, object_type, parser->scope,
32187 tf_warning_or_error);
32189 /* Pull out the template from an injected-class-name (or multiple). */
32190 if (is_template)
32191 decl = maybe_get_template_decl_from_type_decl (decl);
32193 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
32194 if (TREE_CODE (decl) == TREE_LIST)
32196 if (ambiguous_decls)
32197 *ambiguous_decls = decl;
32198 /* The error message we have to print is too complicated for
32199 cp_parser_error, so we incorporate its actions directly. */
32200 if (!cp_parser_simulate_error (parser))
32202 error_at (name_location, "reference to %qD is ambiguous",
32203 name);
32204 print_candidates (decl);
32206 return error_mark_node;
32209 gcc_assert (DECL_P (decl)
32210 || TREE_CODE (decl) == OVERLOAD
32211 || TREE_CODE (decl) == SCOPE_REF
32212 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
32213 || BASELINK_P (decl));
32215 maybe_record_typedef_use (decl);
32217 return cp_expr (decl, name_location);
32220 /* Like cp_parser_lookup_name, but for use in the typical case where
32221 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
32222 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
32224 static tree
32225 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
32227 return cp_parser_lookup_name (parser, name,
32228 none_type,
32229 /*is_template=*/false,
32230 /*is_namespace=*/false,
32231 /*check_dependency=*/true,
32232 /*ambiguous_decls=*/NULL,
32233 location);
32236 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
32237 the current context, return the TYPE_DECL. If TAG_NAME_P is
32238 true, the DECL indicates the class being defined in a class-head,
32239 or declared in an elaborated-type-specifier.
32241 Otherwise, return DECL. */
32243 static tree
32244 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
32246 /* If the TEMPLATE_DECL is being declared as part of a class-head,
32247 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
32249 struct A {
32250 template <typename T> struct B;
32253 template <typename T> struct A::B {};
32255 Similarly, in an elaborated-type-specifier:
32257 namespace N { struct X{}; }
32259 struct A {
32260 template <typename T> friend struct N::X;
32263 However, if the DECL refers to a class type, and we are in
32264 the scope of the class, then the name lookup automatically
32265 finds the TYPE_DECL created by build_self_reference rather
32266 than a TEMPLATE_DECL. For example, in:
32268 template <class T> struct S {
32269 S s;
32272 there is no need to handle such case. */
32274 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
32275 return DECL_TEMPLATE_RESULT (decl);
32277 return decl;
32280 /* If too many, or too few, template-parameter lists apply to the
32281 declarator, issue an error message. Returns TRUE if all went well,
32282 and FALSE otherwise. */
32284 static bool
32285 cp_parser_check_declarator_template_parameters (cp_parser* parser,
32286 cp_declarator *declarator,
32287 location_t declarator_location)
32289 switch (declarator->kind)
32291 case cdk_id:
32293 unsigned num_templates = 0;
32294 tree scope = declarator->u.id.qualifying_scope;
32295 bool template_id_p = false;
32297 if (scope)
32298 num_templates = num_template_headers_for_class (scope);
32299 else if (TREE_CODE (declarator->u.id.unqualified_name)
32300 == TEMPLATE_ID_EXPR)
32302 /* If the DECLARATOR has the form `X<y>' then it uses one
32303 additional level of template parameters. */
32304 ++num_templates;
32305 template_id_p = true;
32308 return cp_parser_check_template_parameters
32309 (parser, num_templates, template_id_p, declarator_location,
32310 declarator);
32313 case cdk_function:
32314 case cdk_array:
32315 case cdk_pointer:
32316 case cdk_reference:
32317 case cdk_ptrmem:
32318 return (cp_parser_check_declarator_template_parameters
32319 (parser, declarator->declarator, declarator_location));
32321 case cdk_decomp:
32322 case cdk_error:
32323 return true;
32325 default:
32326 gcc_unreachable ();
32328 return false;
32331 /* NUM_TEMPLATES were used in the current declaration. If that is
32332 invalid, return FALSE and issue an error messages. Otherwise,
32333 return TRUE. If DECLARATOR is non-NULL, then we are checking a
32334 declarator and we can print more accurate diagnostics. */
32336 static bool
32337 cp_parser_check_template_parameters (cp_parser* parser,
32338 unsigned num_templates,
32339 bool template_id_p,
32340 location_t location,
32341 cp_declarator *declarator)
32343 /* If there are the same number of template classes and parameter
32344 lists, that's OK. */
32345 if (parser->num_template_parameter_lists == num_templates)
32346 return true;
32347 /* If there are more, but only one more, and the name ends in an identifier,
32348 then we are declaring a primary template. That's OK too. */
32349 if (!template_id_p
32350 && parser->num_template_parameter_lists == num_templates + 1)
32351 return true;
32353 if (cp_parser_simulate_error (parser))
32354 return false;
32356 /* If there are more template classes than parameter lists, we have
32357 something like:
32359 template <class T> void S<T>::R<T>::f (); */
32360 if (parser->num_template_parameter_lists < num_templates)
32362 if (declarator && !current_function_decl)
32363 error_at (location, "specializing member %<%T::%E%> "
32364 "requires %<template<>%> syntax",
32365 declarator->u.id.qualifying_scope,
32366 declarator->u.id.unqualified_name);
32367 else if (declarator)
32368 error_at (location, "invalid declaration of %<%T::%E%>",
32369 declarator->u.id.qualifying_scope,
32370 declarator->u.id.unqualified_name);
32371 else
32372 error_at (location, "too few template-parameter-lists");
32373 return false;
32375 /* Otherwise, there are too many template parameter lists. We have
32376 something like:
32378 template <class T> template <class U> void S::f(); */
32379 error_at (location, "too many template-parameter-lists");
32380 return false;
32383 /* Parse an optional `::' token indicating that the following name is
32384 from the global namespace. If so, PARSER->SCOPE is set to the
32385 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
32386 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
32387 Returns the new value of PARSER->SCOPE, if the `::' token is
32388 present, and NULL_TREE otherwise. */
32390 static tree
32391 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
32393 cp_token *token;
32395 /* Peek at the next token. */
32396 token = cp_lexer_peek_token (parser->lexer);
32397 /* If we're looking at a `::' token then we're starting from the
32398 global namespace, not our current location. */
32399 if (token->type == CPP_SCOPE)
32401 /* Consume the `::' token. */
32402 cp_lexer_consume_token (parser->lexer);
32403 /* Set the SCOPE so that we know where to start the lookup. */
32404 parser->scope = global_namespace;
32405 parser->qualifying_scope = global_namespace;
32406 parser->object_scope = NULL_TREE;
32408 return parser->scope;
32410 else if (!current_scope_valid_p)
32412 parser->scope = NULL_TREE;
32413 parser->qualifying_scope = NULL_TREE;
32414 parser->object_scope = NULL_TREE;
32417 return NULL_TREE;
32420 /* Returns TRUE if the upcoming token sequence is the start of a
32421 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
32422 declarator is preceded by the `friend' specifier. The parser flags FLAGS
32423 is used to control type-specifier parsing. */
32425 static bool
32426 cp_parser_constructor_declarator_p (cp_parser *parser, cp_parser_flags flags,
32427 bool friend_p)
32429 bool constructor_p;
32430 bool outside_class_specifier_p;
32431 tree nested_name_specifier;
32432 cp_token *next_token;
32434 /* The common case is that this is not a constructor declarator, so
32435 try to avoid doing lots of work if at all possible. It's not
32436 valid declare a constructor at function scope. */
32437 if (parser->in_function_body)
32438 return false;
32439 /* And only certain tokens can begin a constructor declarator. */
32440 next_token = cp_lexer_peek_token (parser->lexer);
32441 if (next_token->type != CPP_NAME
32442 && next_token->type != CPP_SCOPE
32443 && next_token->type != CPP_NESTED_NAME_SPECIFIER
32444 && next_token->type != CPP_TEMPLATE_ID)
32445 return false;
32447 /* Parse tentatively; we are going to roll back all of the tokens
32448 consumed here. */
32449 cp_parser_parse_tentatively (parser);
32450 /* Assume that we are looking at a constructor declarator. */
32451 constructor_p = true;
32453 /* Look for the optional `::' operator. */
32454 cp_parser_global_scope_opt (parser,
32455 /*current_scope_valid_p=*/false);
32456 /* Look for the nested-name-specifier. */
32457 nested_name_specifier
32458 = (cp_parser_nested_name_specifier_opt (parser,
32459 /*typename_keyword_p=*/false,
32460 /*check_dependency_p=*/false,
32461 /*type_p=*/false,
32462 /*is_declaration=*/false));
32464 /* Resolve the TYPENAME_TYPE, because the call above didn't do it. */
32465 if (nested_name_specifier
32466 && TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
32468 tree s = resolve_typename_type (nested_name_specifier,
32469 /*only_current_p=*/false);
32470 if (TREE_CODE (s) != TYPENAME_TYPE)
32471 nested_name_specifier = s;
32474 outside_class_specifier_p = (!at_class_scope_p ()
32475 || !TYPE_BEING_DEFINED (current_class_type)
32476 || friend_p);
32478 /* Outside of a class-specifier, there must be a
32479 nested-name-specifier. Except in C++17 mode, where we
32480 might be declaring a guiding declaration. */
32481 if (!nested_name_specifier && outside_class_specifier_p
32482 && cxx_dialect < cxx17)
32483 constructor_p = false;
32484 else if (nested_name_specifier == error_mark_node)
32485 constructor_p = false;
32487 /* If we have a class scope, this is easy; DR 147 says that S::S always
32488 names the constructor, and no other qualified name could. */
32489 if (constructor_p && nested_name_specifier
32490 && CLASS_TYPE_P (nested_name_specifier))
32492 tree id = cp_parser_unqualified_id (parser,
32493 /*template_keyword_p=*/false,
32494 /*check_dependency_p=*/false,
32495 /*declarator_p=*/true,
32496 /*optional_p=*/false);
32497 if (is_overloaded_fn (id))
32498 id = DECL_NAME (get_first_fn (id));
32499 if (!constructor_name_p (id, nested_name_specifier))
32500 constructor_p = false;
32502 /* If we still think that this might be a constructor-declarator,
32503 look for a class-name. */
32504 else if (constructor_p)
32506 /* If we have:
32508 template <typename T> struct S {
32509 S();
32512 we must recognize that the nested `S' names a class. */
32513 if (cxx_dialect >= cxx17)
32514 cp_parser_parse_tentatively (parser);
32516 tree type_decl;
32517 type_decl = cp_parser_class_name (parser,
32518 /*typename_keyword_p=*/false,
32519 /*template_keyword_p=*/false,
32520 none_type,
32521 /*check_dependency_p=*/false,
32522 /*class_head_p=*/false,
32523 /*is_declaration=*/false);
32525 if (cxx_dialect >= cxx17
32526 && !cp_parser_parse_definitely (parser))
32528 type_decl = NULL_TREE;
32529 tree tmpl = cp_parser_template_name (parser,
32530 /*template_keyword*/false,
32531 /*check_dependency_p*/false,
32532 /*is_declaration*/false,
32533 none_type,
32534 /*is_identifier*/NULL);
32535 if (DECL_CLASS_TEMPLATE_P (tmpl)
32536 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
32537 /* It's a deduction guide, return true. */;
32538 else
32539 cp_parser_simulate_error (parser);
32542 /* If there was no class-name, then this is not a constructor.
32543 Otherwise, if we are in a class-specifier and we aren't
32544 handling a friend declaration, check that its type matches
32545 current_class_type (c++/38313). Note: error_mark_node
32546 is left alone for error recovery purposes. */
32547 constructor_p = (!cp_parser_error_occurred (parser)
32548 && (outside_class_specifier_p
32549 || type_decl == NULL_TREE
32550 || type_decl == error_mark_node
32551 || same_type_p (current_class_type,
32552 TREE_TYPE (type_decl))));
32554 /* If we're still considering a constructor, we have to see a `(',
32555 to begin the parameter-declaration-clause, followed by either a
32556 `)', an `...', or a decl-specifier. We need to check for a
32557 type-specifier to avoid being fooled into thinking that:
32559 S (f) (int);
32561 is a constructor. (It is actually a function named `f' that
32562 takes one parameter (of type `int') and returns a value of type
32563 `S'. */
32564 if (constructor_p
32565 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32566 constructor_p = false;
32568 if (constructor_p
32569 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
32570 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
32571 /* A parameter declaration begins with a decl-specifier,
32572 which is either the "attribute" keyword, a storage class
32573 specifier, or (usually) a type-specifier. */
32574 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer)
32575 /* GNU attributes can actually appear both at the start of
32576 a parameter and parenthesized declarator.
32577 S (__attribute__((unused)) int);
32578 is a constructor, but
32579 S (__attribute__((unused)) foo) (int);
32580 is a function declaration. [[attribute]] can appear in the
32581 first form too, but not in the second form. */
32582 && !cp_next_tokens_can_be_std_attribute_p (parser))
32584 tree type;
32585 tree pushed_scope = NULL_TREE;
32586 unsigned saved_num_template_parameter_lists;
32588 if (cp_parser_allow_gnu_extensions_p (parser)
32589 && cp_next_tokens_can_be_gnu_attribute_p (parser))
32591 unsigned int n = cp_parser_skip_gnu_attributes_opt (parser, 1);
32592 while (--n)
32593 cp_lexer_consume_token (parser->lexer);
32596 /* Names appearing in the type-specifier should be looked up
32597 in the scope of the class. */
32598 if (current_class_type)
32599 type = NULL_TREE;
32600 else if (type_decl)
32602 type = TREE_TYPE (type_decl);
32603 if (TREE_CODE (type) == TYPENAME_TYPE)
32605 type = resolve_typename_type (type,
32606 /*only_current_p=*/false);
32607 if (TREE_CODE (type) == TYPENAME_TYPE)
32609 cp_parser_abort_tentative_parse (parser);
32610 return false;
32613 pushed_scope = push_scope (type);
32616 /* Inside the constructor parameter list, surrounding
32617 template-parameter-lists do not apply. */
32618 saved_num_template_parameter_lists
32619 = parser->num_template_parameter_lists;
32620 parser->num_template_parameter_lists = 0;
32622 /* Look for the type-specifier. It's not optional, but its typename
32623 might be. Unless this is a friend declaration; we don't want to
32624 treat
32626 friend S (T::fn)(int);
32628 as a constructor, but with P0634, we might assume a type when
32629 looking for the type-specifier. It is actually a function named
32630 `T::fn' that takes one parameter (of type `int') and returns a
32631 value of type `S'. Constructors can be friends, but they must
32632 use a qualified name.
32634 Parse with an empty set of declaration specifiers since we're
32635 trying to match a decl-specifier-seq of the first parameter.
32636 This must be non-null so that cp_parser_simple_type_specifier
32637 will recognize a constrained placeholder type such as:
32638 'C<int> auto' where C is a type concept. */
32639 cp_decl_specifier_seq ctor_specs;
32640 clear_decl_specs (&ctor_specs);
32641 cp_parser_type_specifier (parser,
32642 (friend_p ? CP_PARSER_FLAGS_NONE
32643 : (flags & ~CP_PARSER_FLAGS_OPTIONAL)),
32644 /*decl_specs=*/&ctor_specs,
32645 /*is_declarator=*/true,
32646 /*declares_class_or_enum=*/NULL,
32647 /*is_cv_qualifier=*/NULL);
32649 parser->num_template_parameter_lists
32650 = saved_num_template_parameter_lists;
32652 /* Leave the scope of the class. */
32653 if (pushed_scope)
32654 pop_scope (pushed_scope);
32656 constructor_p = !cp_parser_error_occurred (parser);
32660 /* We did not really want to consume any tokens. */
32661 cp_parser_abort_tentative_parse (parser);
32663 /* DR 2237 (C++20 only): A simple-template-id is no longer valid as the
32664 declarator-id of a constructor or destructor. */
32665 if (constructor_p
32666 && cp_lexer_peek_token (parser->lexer)->type == CPP_TEMPLATE_ID)
32668 auto_diagnostic_group d;
32669 if (emit_diagnostic (cxx_dialect >= cxx20 ? DK_PEDWARN : DK_WARNING,
32670 input_location, OPT_Wtemplate_id_cdtor,
32671 "template-id not allowed for constructor in C++20"))
32672 inform (input_location, "remove the %qs", "< >");
32675 return constructor_p;
32678 /* Parse the definition of the function given by the DECL_SPECIFIERS,
32679 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
32680 they must be performed once we are in the scope of the function.
32682 Returns the function defined. */
32684 static tree
32685 cp_parser_function_definition_from_specifiers_and_declarator
32686 (cp_parser* parser,
32687 cp_decl_specifier_seq *decl_specifiers,
32688 tree attributes,
32689 const cp_declarator *declarator)
32691 tree fn;
32692 bool success_p;
32694 /* Begin the function-definition. */
32695 success_p = start_function (decl_specifiers, declarator, attributes);
32697 /* The things we're about to see are not directly qualified by any
32698 template headers we've seen thus far. */
32699 reset_specialization ();
32701 /* If there were names looked up in the decl-specifier-seq that we
32702 did not check, check them now. We must wait until we are in the
32703 scope of the function to perform the checks, since the function
32704 might be a friend. */
32705 perform_deferred_access_checks (tf_warning_or_error);
32707 if (success_p)
32709 cp_finalize_omp_declare_simd (parser, current_function_decl);
32710 parser->omp_declare_simd = NULL;
32711 cp_finalize_oacc_routine (parser, current_function_decl, true);
32712 parser->oacc_routine = NULL;
32715 if (!success_p)
32717 /* Skip the entire function. */
32718 cp_parser_skip_to_end_of_block_or_statement (parser);
32719 fn = error_mark_node;
32721 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
32723 /* Seen already, skip it. An error message has already been output. */
32724 cp_parser_skip_to_end_of_block_or_statement (parser);
32725 fn = current_function_decl;
32726 current_function_decl = NULL_TREE;
32727 /* If this is a function from a class, pop the nested class. */
32728 if (current_class_name)
32729 pop_nested_class ();
32731 else
32733 auto_timevar tv (DECL_DECLARED_INLINE_P (current_function_decl)
32734 ? TV_PARSE_INLINE : TV_PARSE_FUNC);
32735 fn = cp_parser_function_definition_after_declarator (parser,
32736 /*inline_p=*/false);
32739 return fn;
32742 /* Parse the part of a function-definition that follows the
32743 declarator. INLINE_P is TRUE iff this function is an inline
32744 function defined within a class-specifier.
32746 Returns the function defined. */
32748 static tree
32749 cp_parser_function_definition_after_declarator (cp_parser* parser,
32750 bool inline_p)
32752 tree fn;
32753 bool saved_in_unbraced_linkage_specification_p;
32754 bool saved_in_function_body;
32755 unsigned saved_num_template_parameter_lists;
32756 cp_token *token;
32757 bool fully_implicit_function_template_p
32758 = parser->fully_implicit_function_template_p;
32759 parser->fully_implicit_function_template_p = false;
32760 tree implicit_template_parms
32761 = parser->implicit_template_parms;
32762 parser->implicit_template_parms = 0;
32763 cp_binding_level* implicit_template_scope
32764 = parser->implicit_template_scope;
32765 parser->implicit_template_scope = 0;
32767 saved_in_function_body = parser->in_function_body;
32768 parser->in_function_body = true;
32769 /* If the next token is `return', then the code may be trying to
32770 make use of the "named return value" extension that G++ used to
32771 support. */
32772 token = cp_lexer_peek_token (parser->lexer);
32773 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
32775 /* Consume the `return' keyword. */
32776 cp_lexer_consume_token (parser->lexer);
32777 /* Look for the identifier that indicates what value is to be
32778 returned. */
32779 cp_parser_identifier (parser);
32780 /* Issue an error message. */
32781 error_at (token->location,
32782 "named return values are no longer supported");
32783 /* Skip tokens until we reach the start of the function body. */
32784 while (true)
32786 cp_token *token = cp_lexer_peek_token (parser->lexer);
32787 if (token->type == CPP_OPEN_BRACE
32788 || token->type == CPP_EOF
32789 || token->type == CPP_PRAGMA_EOL)
32790 break;
32791 cp_lexer_consume_token (parser->lexer);
32794 /* The `extern' in `extern "C" void f () { ... }' does not apply to
32795 anything declared inside `f'. */
32796 saved_in_unbraced_linkage_specification_p
32797 = parser->in_unbraced_linkage_specification_p;
32798 parser->in_unbraced_linkage_specification_p = false;
32799 /* Inside the function, surrounding template-parameter-lists do not
32800 apply. */
32801 saved_num_template_parameter_lists
32802 = parser->num_template_parameter_lists;
32803 parser->num_template_parameter_lists = 0;
32805 /* If the next token is `try', `__transaction_atomic', or
32806 `__transaction_relaxed`, then we are looking at either function-try-block
32807 or function-transaction-block. Note that all of these include the
32808 function-body. */
32809 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
32810 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
32811 else if (cp_lexer_next_token_is_keyword (parser->lexer,
32812 RID_TRANSACTION_RELAXED))
32813 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
32814 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
32815 cp_parser_function_try_block (parser);
32816 else
32817 cp_parser_ctor_initializer_opt_and_function_body
32818 (parser, /*in_function_try_block=*/false);
32820 /* Finish the function. */
32821 fn = finish_function (inline_p);
32823 if (modules_p ()
32824 && !inline_p
32825 && TYPE_P (DECL_CONTEXT (fn))
32826 && (DECL_DECLARED_INLINE_P (fn)
32827 || processing_template_decl))
32828 set_defining_module (fn);
32830 /* Generate code for it, if necessary. */
32831 expand_or_defer_fn (fn);
32833 /* Restore the saved values. */
32834 parser->in_unbraced_linkage_specification_p
32835 = saved_in_unbraced_linkage_specification_p;
32836 parser->num_template_parameter_lists
32837 = saved_num_template_parameter_lists;
32838 parser->in_function_body = saved_in_function_body;
32840 parser->fully_implicit_function_template_p
32841 = fully_implicit_function_template_p;
32842 parser->implicit_template_parms
32843 = implicit_template_parms;
32844 parser->implicit_template_scope
32845 = implicit_template_scope;
32847 if (parser->fully_implicit_function_template_p)
32848 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
32850 return fn;
32853 /* Parse a template-declaration body (following argument list). */
32855 static void
32856 cp_parser_template_declaration_after_parameters (cp_parser* parser,
32857 tree parameter_list,
32858 bool member_p)
32860 tree decl = NULL_TREE;
32861 bool friend_p = false;
32863 /* We just processed one more parameter list. */
32864 ++parser->num_template_parameter_lists;
32866 /* Get the deferred access checks from the parameter list. These
32867 will be checked once we know what is being declared, as for a
32868 member template the checks must be performed in the scope of the
32869 class containing the member. */
32870 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
32872 /* Tentatively parse for a new template parameter list, which can either be
32873 the template keyword or a template introduction. */
32874 if (cp_parser_template_declaration_after_export (parser, member_p))
32875 /* OK */;
32876 else if (cxx_dialect >= cxx11
32877 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
32878 decl = cp_parser_alias_declaration (parser);
32879 else if (flag_concepts
32880 && cp_lexer_next_token_is_keyword (parser->lexer, RID_CONCEPT)
32881 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
32882 /* -fconcept-ts 'concept bool' syntax is handled below, in
32883 cp_parser_single_declaration. */
32884 decl = cp_parser_concept_definition (parser);
32885 else
32887 cp_token *token = cp_lexer_peek_token (parser->lexer);
32888 decl = cp_parser_single_declaration (parser,
32889 checks,
32890 member_p,
32891 /*explicit_specialization_p=*/false,
32892 &friend_p);
32894 /* If this is a member template declaration, let the front
32895 end know. */
32896 if (member_p && !friend_p && decl)
32898 if (TREE_CODE (decl) == TYPE_DECL)
32899 cp_parser_check_access_in_redeclaration (decl, token->location);
32901 decl = finish_member_template_decl (decl);
32903 else if (friend_p && decl
32904 && DECL_DECLARES_TYPE_P (decl))
32905 make_friend_class (current_class_type, TREE_TYPE (decl),
32906 /*complain=*/true);
32908 /* We are done with the current parameter list. */
32909 --parser->num_template_parameter_lists;
32911 pop_deferring_access_checks ();
32913 /* Finish up. */
32914 finish_template_decl (parameter_list);
32916 /* Check the template arguments for a literal operator template. */
32917 if (decl
32918 && DECL_DECLARES_FUNCTION_P (decl)
32919 && UDLIT_OPER_P (DECL_NAME (decl)))
32921 bool ok = true;
32922 if (parameter_list == NULL_TREE)
32923 ok = false;
32924 else
32926 int num_parms = TREE_VEC_LENGTH (parameter_list);
32927 if (num_parms == 1)
32929 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
32930 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
32931 if (TREE_CODE (parm) != PARM_DECL)
32932 ok = false;
32933 else if (MAYBE_CLASS_TYPE_P (TREE_TYPE (parm))
32934 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
32935 /* OK, C++20 string literal operator template. We don't need
32936 to warn in lower dialects here because we will have already
32937 warned about the template parameter. */;
32938 else if (TREE_TYPE (parm) != char_type_node
32939 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
32940 ok = false;
32942 else if (num_parms == 2 && cxx_dialect >= cxx14)
32944 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
32945 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
32946 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
32947 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
32948 if (TREE_CODE (parm) != PARM_DECL
32949 || TREE_TYPE (parm) != TREE_TYPE (type)
32950 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
32951 ok = false;
32952 else
32953 /* http://cplusplus.github.io/EWG/ewg-active.html#66 */
32954 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
32955 "ISO C++ did not adopt string literal operator templa"
32956 "tes taking an argument pack of characters");
32958 else
32959 ok = false;
32961 if (!ok)
32963 if (cxx_dialect > cxx17)
32964 error_at (DECL_SOURCE_LOCATION (decl), "literal operator "
32965 "template %qD has invalid parameter list; expected "
32966 "non-type template parameter pack %<<char...>%> or "
32967 "single non-type parameter of class type",
32968 decl);
32969 else
32970 error_at (DECL_SOURCE_LOCATION (decl), "literal operator "
32971 "template %qD has invalid parameter list; expected "
32972 "non-type template parameter pack %<<char...>%>",
32973 decl);
32977 /* Register member declarations. */
32978 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
32979 finish_member_declaration (decl);
32980 /* If DECL is a function template, we must return to parse it later.
32981 (Even though there is no definition, there might be default
32982 arguments that need handling.) */
32983 if (member_p && decl
32984 && DECL_DECLARES_FUNCTION_P (decl))
32985 vec_safe_push (unparsed_funs_with_definitions, decl);
32988 /* Parse a template introduction header for a template-declaration. Returns
32989 false if tentative parse fails. */
32991 static bool
32992 cp_parser_template_introduction (cp_parser* parser, bool member_p)
32994 cp_parser_parse_tentatively (parser);
32996 tree saved_scope = parser->scope;
32997 tree saved_object_scope = parser->object_scope;
32998 tree saved_qualifying_scope = parser->qualifying_scope;
32999 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
33001 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
33003 /* In classes don't parse valid unnamed bitfields as invalid
33004 template introductions. */
33005 if (member_p)
33006 parser->colon_corrects_to_scope_p = false;
33008 /* Look for the optional `::' operator. */
33009 cp_parser_global_scope_opt (parser,
33010 /*current_scope_valid_p=*/false);
33011 /* Look for the nested-name-specifier. */
33012 cp_parser_nested_name_specifier_opt (parser,
33013 /*typename_keyword_p=*/false,
33014 /*check_dependency_p=*/true,
33015 /*type_p=*/false,
33016 /*is_declaration=*/false);
33018 cp_token *token = cp_lexer_peek_token (parser->lexer);
33019 tree concept_name = cp_parser_identifier (parser);
33021 /* Look up the concept for which we will be matching
33022 template parameters. */
33023 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
33024 token->location);
33025 parser->scope = saved_scope;
33026 parser->object_scope = saved_object_scope;
33027 parser->qualifying_scope = saved_qualifying_scope;
33028 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
33030 if (concept_name == error_mark_node
33031 || (seen_error () && !concept_definition_p (tmpl_decl)))
33032 cp_parser_simulate_error (parser);
33034 /* Look for opening brace for introduction. */
33035 matching_braces braces;
33036 braces.require_open (parser);
33037 location_t open_loc = input_location;
33039 if (!cp_parser_parse_definitely (parser))
33040 return false;
33042 push_deferring_access_checks (dk_deferred);
33044 /* Build vector of placeholder parameters and grab
33045 matching identifiers. */
33046 tree introduction_list = cp_parser_introduction_list (parser);
33048 /* Look for closing brace for introduction. */
33049 if (!braces.require_close (parser))
33050 return true;
33052 /* The introduction-list shall not be empty. */
33053 int nargs = TREE_VEC_LENGTH (introduction_list);
33054 if (nargs == 0)
33056 /* In cp_parser_introduction_list we have already issued an error. */
33057 return true;
33060 if (tmpl_decl == error_mark_node)
33062 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
33063 token->location);
33064 return true;
33067 /* Build and associate the constraint. */
33068 location_t introduction_loc = make_location (open_loc,
33069 start_token->location,
33070 parser->lexer);
33071 tree parms = finish_template_introduction (tmpl_decl,
33072 introduction_list,
33073 introduction_loc);
33074 if (parms && parms != error_mark_node)
33076 if (!flag_concepts_ts)
33077 pedwarn (introduction_loc, 0, "template-introductions"
33078 " are not part of C++20 concepts; use %qs to enable",
33079 "-fconcepts-ts");
33081 cp_parser_template_declaration_after_parameters (parser, parms,
33082 member_p);
33083 return true;
33086 if (parms == NULL_TREE)
33087 error_at (token->location, "no matching concept for template-introduction");
33089 return true;
33092 /* Parse a normal template-declaration following the template keyword. */
33094 static void
33095 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
33097 tree parameter_list;
33098 bool need_lang_pop;
33099 location_t location = input_location;
33101 /* Look for the `<' token. */
33102 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
33103 return;
33104 if (at_class_scope_p () && current_function_decl)
33106 /* 14.5.2.2 [temp.mem]
33108 A local class shall not have member templates. */
33109 error_at (location,
33110 "invalid declaration of member template in local class");
33111 cp_parser_skip_to_end_of_block_or_statement (parser);
33112 return;
33114 /* [temp]
33116 A template ... shall not have C linkage. */
33117 if (current_lang_name == lang_name_c)
33119 error_at (location, "template with C linkage");
33120 maybe_show_extern_c_location ();
33121 /* Give it C++ linkage to avoid confusing other parts of the
33122 front end. */
33123 push_lang_context (lang_name_cplusplus);
33124 need_lang_pop = true;
33126 else
33127 need_lang_pop = false;
33129 /* We cannot perform access checks on the template parameter
33130 declarations until we know what is being declared, just as we
33131 cannot check the decl-specifier list. */
33132 push_deferring_access_checks (dk_deferred);
33134 /* If the next token is `>', then we have an invalid
33135 specialization. Rather than complain about an invalid template
33136 parameter, issue an error message here. */
33137 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
33139 cp_parser_error (parser, "invalid explicit specialization");
33140 begin_specialization ();
33141 parameter_list = NULL_TREE;
33143 else
33145 /* Parse the template parameters. */
33146 parameter_list = cp_parser_template_parameter_list (parser);
33149 /* Look for the `>'. */
33150 cp_parser_require_end_of_template_parameter_list (parser);
33152 /* Manage template requirements */
33153 if (flag_concepts)
33155 tree reqs = get_shorthand_constraints (current_template_parms);
33156 if (tree treqs = cp_parser_requires_clause_opt (parser, false))
33157 reqs = combine_constraint_expressions (reqs, treqs);
33158 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
33161 cp_parser_template_declaration_after_parameters (parser, parameter_list,
33162 member_p);
33164 /* For the erroneous case of a template with C linkage, we pushed an
33165 implicit C++ linkage scope; exit that scope now. */
33166 if (need_lang_pop)
33167 pop_lang_context ();
33170 /* Parse a template-declaration, assuming that the `export' (and
33171 `extern') keywords, if present, has already been scanned. MEMBER_P
33172 is as for cp_parser_template_declaration. */
33174 static bool
33175 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
33177 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
33179 cp_lexer_consume_token (parser->lexer);
33180 cp_parser_explicit_template_declaration (parser, member_p);
33181 return true;
33183 else if (flag_concepts)
33184 return cp_parser_template_introduction (parser, member_p);
33186 return false;
33189 /* Perform the deferred access checks from a template-parameter-list.
33190 CHECKS is a TREE_LIST of access checks, as returned by
33191 get_deferred_access_checks. */
33193 static void
33194 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
33196 ++processing_template_parmlist;
33197 perform_access_checks (checks, tf_warning_or_error);
33198 --processing_template_parmlist;
33201 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
33202 `function-definition' sequence that follows a template header.
33203 If MEMBER_P is true, this declaration appears in a class scope.
33205 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
33206 *FRIEND_P is set to TRUE iff the declaration is a friend. */
33208 static tree
33209 cp_parser_single_declaration (cp_parser* parser,
33210 vec<deferred_access_check, va_gc> *checks,
33211 bool member_p,
33212 bool explicit_specialization_p,
33213 bool* friend_p)
33215 int declares_class_or_enum;
33216 tree decl = NULL_TREE;
33217 cp_decl_specifier_seq decl_specifiers;
33218 bool function_definition_p = false;
33219 cp_token *decl_spec_token_start;
33221 /* This function is only used when processing a template
33222 declaration. */
33223 gcc_assert (innermost_scope_kind () == sk_template_parms
33224 || innermost_scope_kind () == sk_template_spec);
33226 /* Defer access checks until we know what is being declared. */
33227 push_deferring_access_checks (dk_deferred);
33229 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
33230 alternative. */
33231 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
33232 cp_parser_decl_specifier_seq (parser,
33233 (CP_PARSER_FLAGS_OPTIONAL
33234 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
33235 &decl_specifiers,
33236 &declares_class_or_enum);
33238 cp_omp_declare_simd_data odsd;
33239 if (decl_specifiers.attributes && (flag_openmp || flag_openmp_simd))
33240 cp_parser_handle_directive_omp_attributes (parser,
33241 &decl_specifiers.attributes,
33242 &odsd, true);
33244 if (friend_p)
33245 *friend_p = cp_parser_friend_p (&decl_specifiers);
33247 /* There are no template typedefs. */
33248 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
33250 error_at (decl_spec_token_start->location,
33251 "template declaration of %<typedef%>");
33252 decl = error_mark_node;
33255 /* Gather up the access checks that occurred the
33256 decl-specifier-seq. */
33257 stop_deferring_access_checks ();
33259 /* Check for the declaration of a template class. */
33260 if (declares_class_or_enum)
33262 if (cp_parser_declares_only_class_p (parser)
33263 || (declares_class_or_enum & 2))
33265 decl = shadow_tag (&decl_specifiers);
33267 /* In this case:
33269 struct C {
33270 friend template <typename T> struct A<T>::B;
33273 A<T>::B will be represented by a TYPENAME_TYPE, and
33274 therefore not recognized by shadow_tag. */
33275 if (friend_p && *friend_p
33276 && !decl
33277 && decl_specifiers.type
33278 && TYPE_P (decl_specifiers.type))
33279 decl = decl_specifiers.type;
33281 if (decl && decl != error_mark_node)
33282 decl = TYPE_NAME (decl);
33283 else
33284 decl = error_mark_node;
33286 /* If this is a declaration, but not a definition, associate
33287 any constraints with the type declaration. Constraints
33288 are associated with definitions in cp_parser_class_specifier. */
33289 if (declares_class_or_enum == 1)
33290 associate_classtype_constraints (TREE_TYPE (decl));
33292 /* Perform access checks for template parameters. */
33293 cp_parser_perform_template_parameter_access_checks (checks);
33295 /* Give a helpful diagnostic for
33296 template <class T> struct A { } a;
33297 if we aren't already recovering from an error. */
33298 if (!cp_parser_declares_only_class_p (parser)
33299 && !seen_error ())
33301 error_at (cp_lexer_peek_token (parser->lexer)->location,
33302 "a class template declaration must not declare "
33303 "anything else");
33304 cp_parser_skip_to_end_of_block_or_statement (parser);
33305 goto out;
33310 /* Complain about missing 'typename' or other invalid type names. */
33311 if (!decl_specifiers.any_type_specifiers_p
33312 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
33314 /* cp_parser_parse_and_diagnose_invalid_type_name calls
33315 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
33316 the rest of this declaration. */
33317 decl = error_mark_node;
33318 goto out;
33321 /* If it's not a template class, try for a template function. If
33322 the next token is a `;', then this declaration does not declare
33323 anything. But, if there were errors in the decl-specifiers, then
33324 the error might well have come from an attempted class-specifier.
33325 In that case, there's no need to warn about a missing declarator. */
33326 if (!decl
33327 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
33328 || decl_specifiers.type != error_mark_node))
33330 int flags = CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
33331 /* We don't delay parsing for friends, though CWG 2510 may change
33332 that. */
33333 if (member_p && !(friend_p && *friend_p))
33334 flags |= CP_PARSER_FLAGS_DELAY_NOEXCEPT;
33335 decl = cp_parser_init_declarator (parser,
33336 flags,
33337 &decl_specifiers,
33338 checks,
33339 /*function_definition_allowed_p=*/true,
33340 member_p,
33341 declares_class_or_enum,
33342 &function_definition_p,
33343 NULL, NULL, NULL);
33345 /* 7.1.1-1 [dcl.stc]
33347 A storage-class-specifier shall not be specified in an explicit
33348 specialization... */
33349 if (decl
33350 && explicit_specialization_p
33351 && decl_specifiers.storage_class != sc_none)
33353 error_at (decl_spec_token_start->location,
33354 "explicit template specialization cannot have a storage class");
33355 decl = error_mark_node;
33358 if (decl && VAR_P (decl))
33359 check_template_variable (decl);
33362 /* Look for a trailing `;' after the declaration. */
33363 if (!function_definition_p
33364 && (decl == error_mark_node
33365 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
33366 cp_parser_skip_to_end_of_block_or_statement (parser);
33368 out:
33369 pop_deferring_access_checks ();
33371 /* Clear any current qualification; whatever comes next is the start
33372 of something new. */
33373 parser->scope = NULL_TREE;
33374 parser->qualifying_scope = NULL_TREE;
33375 parser->object_scope = NULL_TREE;
33377 cp_finalize_omp_declare_simd (parser, &odsd);
33379 return decl;
33382 /* Parse a cast-expression that is not the operand of a unary "&". */
33384 static cp_expr
33385 cp_parser_simple_cast_expression (cp_parser *parser)
33387 return cp_parser_cast_expression (parser, /*address_p=*/false,
33388 /*cast_p=*/false, /*decltype*/false, NULL);
33391 /* Parse a functional cast to TYPE. Returns an expression
33392 representing the cast. */
33394 static cp_expr
33395 cp_parser_functional_cast (cp_parser* parser, tree type)
33397 vec<tree, va_gc> *vec;
33398 tree expression_list;
33399 cp_expr cast;
33401 location_t start_loc = input_location;
33403 if (!type)
33404 type = error_mark_node;
33406 if (TREE_CODE (type) == TYPE_DECL
33407 && is_auto (TREE_TYPE (type)))
33408 type = TREE_TYPE (type);
33410 if (is_auto (type)
33411 && !AUTO_IS_DECLTYPE (type)
33412 && !PLACEHOLDER_TYPE_CONSTRAINTS (type)
33413 && !CLASS_PLACEHOLDER_TEMPLATE (type))
33414 /* auto(x) and auto{x} need to use a level-less auto. */
33415 type = make_cast_auto ();
33417 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
33419 cp_lexer_set_source_position (parser->lexer);
33420 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
33421 expression_list = cp_parser_braced_list (parser);
33422 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
33423 if (TREE_CODE (type) == TYPE_DECL)
33424 type = TREE_TYPE (type);
33426 cast = finish_compound_literal (type, expression_list,
33427 tf_warning_or_error, fcl_functional);
33428 /* Create a location of the form:
33429 type_name{i, f}
33430 ^~~~~~~~~~~~~~~
33431 with caret == start at the start of the type name,
33432 finishing at the closing brace. */
33433 location_t combined_loc = make_location (start_loc, start_loc,
33434 parser->lexer);
33435 cast.set_location (combined_loc);
33436 return cast;
33440 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
33441 /*cast_p=*/true,
33442 /*allow_expansion_p=*/true,
33443 /*non_constant_p=*/NULL);
33444 if (vec == NULL)
33445 expression_list = error_mark_node;
33446 else
33448 expression_list = build_tree_list_vec (vec);
33449 release_tree_vector (vec);
33452 /* Create a location of the form:
33453 float(i)
33454 ^~~~~~~~
33455 with caret == start at the start of the type name,
33456 finishing at the closing paren. */
33457 location_t combined_loc = make_location (start_loc, start_loc,
33458 parser->lexer);
33459 cast = build_functional_cast (combined_loc, type, expression_list,
33460 tf_warning_or_error);
33462 /* [expr.const]/1: In an integral constant expression "only type
33463 conversions to integral or enumeration type can be used". */
33464 if (TREE_CODE (type) == TYPE_DECL)
33465 type = TREE_TYPE (type);
33466 if (cast != error_mark_node
33467 && !cast_valid_in_integral_constant_expression_p (type)
33468 && cp_parser_non_integral_constant_expression (parser,
33469 NIC_CONSTRUCTOR))
33470 return error_mark_node;
33472 return cast;
33475 /* Save the tokens that make up the body of a member function defined
33476 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
33477 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
33478 specifiers applied to the declaration. Returns the FUNCTION_DECL
33479 for the member function. */
33481 static tree
33482 cp_parser_save_member_function_body (cp_parser* parser,
33483 cp_decl_specifier_seq *decl_specifiers,
33484 cp_declarator *declarator,
33485 tree attributes)
33487 cp_token *first;
33488 cp_token *last;
33489 tree fn;
33490 bool function_try_block = false;
33492 /* Create the FUNCTION_DECL. */
33493 fn = grokmethod (decl_specifiers, declarator, attributes);
33494 cp_finalize_omp_declare_simd (parser, fn);
33495 cp_finalize_oacc_routine (parser, fn, true);
33496 /* If something went badly wrong, bail out now. */
33497 if (fn == error_mark_node)
33499 /* If there's a function-body, skip it. */
33500 if (cp_parser_token_starts_function_definition_p
33501 (cp_lexer_peek_token (parser->lexer)))
33502 cp_parser_skip_to_end_of_block_or_statement (parser);
33503 return error_mark_node;
33506 /* Remember it, if there are default args to post process. */
33507 cp_parser_save_default_args (parser, fn);
33509 /* Save away the tokens that make up the body of the
33510 function. */
33511 first = parser->lexer->next_token;
33513 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
33514 cp_lexer_consume_token (parser->lexer);
33515 else if (cp_lexer_next_token_is_keyword (parser->lexer,
33516 RID_TRANSACTION_ATOMIC))
33518 cp_lexer_consume_token (parser->lexer);
33519 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
33520 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
33521 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
33522 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
33523 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
33524 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
33525 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
33527 cp_lexer_consume_token (parser->lexer);
33528 cp_lexer_consume_token (parser->lexer);
33529 cp_lexer_consume_token (parser->lexer);
33530 cp_lexer_consume_token (parser->lexer);
33531 cp_lexer_consume_token (parser->lexer);
33533 else
33534 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
33535 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
33537 cp_lexer_consume_token (parser->lexer);
33538 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
33539 break;
33543 /* Handle function try blocks. */
33544 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
33546 cp_lexer_consume_token (parser->lexer);
33547 function_try_block = true;
33549 /* We can have braced-init-list mem-initializers before the fn body. */
33550 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
33552 cp_lexer_consume_token (parser->lexer);
33553 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
33555 /* cache_group will stop after an un-nested { } pair, too. */
33556 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
33557 break;
33559 /* variadic mem-inits have ... after the ')'. */
33560 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
33561 cp_lexer_consume_token (parser->lexer);
33564 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
33565 /* Handle function try blocks. */
33566 if (function_try_block)
33567 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
33568 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
33569 last = parser->lexer->next_token;
33571 /* Save away the inline definition; we will process it when the
33572 class is complete. */
33573 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
33574 DECL_PENDING_INLINE_P (fn) = 1;
33576 /* We need to know that this was defined in the class, so that
33577 friend templates are handled correctly. */
33578 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
33580 /* Add FN to the queue of functions to be parsed later. */
33581 vec_safe_push (unparsed_funs_with_definitions, fn);
33583 return fn;
33586 /* Save the tokens that make up the in-class initializer for a non-static
33587 data member. Returns a DEFERRED_PARSE. */
33589 static tree
33590 cp_parser_save_nsdmi (cp_parser* parser)
33592 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
33595 /* Parse a template-argument-list, as well as the trailing ">" (but
33596 not the opening "<"). See cp_parser_template_argument_list for the
33597 return value. */
33599 static tree
33600 cp_parser_enclosed_template_argument_list (cp_parser* parser)
33602 tree arguments;
33603 tree saved_scope;
33604 tree saved_qualifying_scope;
33605 tree saved_object_scope;
33606 bool saved_greater_than_is_operator_p;
33608 /* [temp.names]
33610 When parsing a template-id, the first non-nested `>' is taken as
33611 the end of the template-argument-list rather than a greater-than
33612 operator. */
33613 saved_greater_than_is_operator_p
33614 = parser->greater_than_is_operator_p;
33615 parser->greater_than_is_operator_p = false;
33616 /* Parsing the argument list may modify SCOPE, so we save it
33617 here. */
33618 saved_scope = parser->scope;
33619 saved_qualifying_scope = parser->qualifying_scope;
33620 saved_object_scope = parser->object_scope;
33621 /* We need to evaluate the template arguments, even though this
33622 template-id may be nested within a "sizeof". */
33623 cp_evaluated ev;
33624 /* Parse the template-argument-list itself. */
33625 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
33626 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT)
33627 || cp_lexer_next_token_is (parser->lexer, CPP_GREATER_EQ)
33628 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT_EQ))
33630 arguments = make_tree_vec (0);
33631 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (arguments, 0);
33633 else
33634 arguments = cp_parser_template_argument_list (parser);
33635 /* Look for the `>' that ends the template-argument-list. If we find
33636 a '>>' instead, it's probably just a typo. */
33637 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
33639 if (cxx_dialect != cxx98)
33641 /* In C++0x, a `>>' in a template argument list or cast
33642 expression is considered to be two separate `>'
33643 tokens. So, change the current token to a `>', but don't
33644 consume it: it will be consumed later when the outer
33645 template argument list (or cast expression) is parsed.
33646 Note that this replacement of `>' for `>>' is necessary
33647 even if we are parsing tentatively: in the tentative
33648 case, after calling
33649 cp_parser_enclosed_template_argument_list we will always
33650 throw away all of the template arguments and the first
33651 closing `>', either because the template argument list
33652 was erroneous or because we are replacing those tokens
33653 with a CPP_TEMPLATE_ID token. The second `>' (which will
33654 not have been thrown away) is needed either to close an
33655 outer template argument list or to complete a new-style
33656 cast. */
33657 cp_token *token = cp_lexer_peek_token (parser->lexer);
33658 token->type = CPP_GREATER;
33660 else if (!saved_greater_than_is_operator_p)
33662 /* If we're in a nested template argument list, the '>>' has
33663 to be a typo for '> >'. We emit the error message, but we
33664 continue parsing and we push a '>' as next token, so that
33665 the argument list will be parsed correctly. Note that the
33666 global source location is still on the token before the
33667 '>>', so we need to say explicitly where we want it. */
33668 cp_token *token = cp_lexer_peek_token (parser->lexer);
33669 gcc_rich_location richloc (token->location);
33670 richloc.add_fixit_replace ("> >");
33671 error_at (&richloc, "%<>>%> should be %<> >%> "
33672 "within a nested template argument list");
33674 token->type = CPP_GREATER;
33676 else
33678 /* If this is not a nested template argument list, the '>>'
33679 is a typo for '>'. Emit an error message and continue.
33680 Same deal about the token location, but here we can get it
33681 right by consuming the '>>' before issuing the diagnostic. */
33682 cp_token *token = cp_lexer_consume_token (parser->lexer);
33683 error_at (token->location,
33684 "spurious %<>>%>, use %<>%> to terminate "
33685 "a template argument list");
33688 /* Similarly for >>= and >=. */
33689 else if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER_EQ)
33690 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT_EQ))
33692 cp_token *token = cp_lexer_consume_token (parser->lexer);
33693 gcc_rich_location richloc (token->location);
33694 enum cpp_ttype new_type;
33695 const char *replacement;
33696 if (token->type == CPP_GREATER_EQ)
33698 replacement = "> =";
33699 new_type = CPP_EQ;
33701 else if (!saved_greater_than_is_operator_p)
33703 if (cxx_dialect != cxx98)
33704 replacement = ">> =";
33705 else
33706 replacement = "> > =";
33707 new_type = CPP_GREATER;
33709 else
33711 replacement = "> >=";
33712 new_type = CPP_GREATER_EQ;
33714 richloc.add_fixit_replace (replacement);
33715 error_at (&richloc, "%qs should be %qs to terminate a template "
33716 "argument list",
33717 cpp_type2name (token->type, token->flags), replacement);
33718 token->type = new_type;
33720 else
33721 cp_parser_require_end_of_template_parameter_list (parser);
33722 /* The `>' token might be a greater-than operator again now. */
33723 parser->greater_than_is_operator_p
33724 = saved_greater_than_is_operator_p;
33725 /* Restore the SAVED_SCOPE. */
33726 parser->scope = saved_scope;
33727 parser->qualifying_scope = saved_qualifying_scope;
33728 parser->object_scope = saved_object_scope;
33730 return arguments;
33733 /* MEMBER_FUNCTION is a member function, or a friend. If default
33734 arguments, or the body of the function have not yet been parsed,
33735 parse them now. */
33737 static void
33738 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
33740 auto_timevar tv (TV_PARSE_INMETH);
33742 /* If this member is a template, get the underlying
33743 FUNCTION_DECL. */
33744 if (DECL_FUNCTION_TEMPLATE_P (member_function))
33745 member_function = DECL_TEMPLATE_RESULT (member_function);
33747 /* There should not be any class definitions in progress at this
33748 point; the bodies of members are only parsed outside of all class
33749 definitions. */
33750 gcc_assert (parser->num_classes_being_defined == 0);
33751 /* While we're parsing the member functions we might encounter more
33752 classes. We want to handle them right away, but we don't want
33753 them getting mixed up with functions that are currently in the
33754 queue. */
33755 push_unparsed_function_queues (parser);
33757 /* Make sure that any template parameters are in scope. */
33758 maybe_begin_member_template_processing (member_function);
33760 /* If the body of the function has not yet been parsed, parse it
33761 now. Except if the tokens have been purged (PR c++/39751). */
33762 if (DECL_PENDING_INLINE_P (member_function)
33763 && !DECL_PENDING_INLINE_INFO (member_function)->first->purged_p)
33765 tree function_scope;
33766 cp_token_cache *tokens;
33768 /* The function is no longer pending; we are processing it. */
33769 tokens = DECL_PENDING_INLINE_INFO (member_function);
33770 DECL_PENDING_INLINE_INFO (member_function) = NULL;
33771 DECL_PENDING_INLINE_P (member_function) = 0;
33773 /* If this is a local class, enter the scope of the containing
33774 function. */
33775 function_scope = current_function_decl;
33776 if (function_scope)
33777 push_function_context ();
33779 /* Push the body of the function onto the lexer stack. */
33780 cp_parser_push_lexer_for_tokens (parser, tokens);
33782 /* Let the front end know that we going to be defining this
33783 function. */
33784 start_preparsed_function (member_function, NULL_TREE,
33785 SF_PRE_PARSED | SF_INCLASS_INLINE);
33787 /* #pragma omp declare reduction needs special parsing. */
33788 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
33790 parser->lexer->in_pragma = true;
33791 cp_parser_omp_declare_reduction_exprs (member_function, parser);
33792 finish_function (/*inline_p=*/true);
33793 cp_check_omp_declare_reduction (member_function);
33795 else
33796 /* Now, parse the body of the function. */
33797 cp_parser_function_definition_after_declarator (parser,
33798 /*inline_p=*/true);
33800 /* Leave the scope of the containing function. */
33801 if (function_scope)
33802 pop_function_context ();
33803 cp_parser_pop_lexer (parser);
33806 /* Remove any template parameters from the symbol table. */
33807 maybe_end_member_template_processing ();
33809 /* Restore the queue. */
33810 pop_unparsed_function_queues (parser);
33813 /* If DECL contains any default args, remember it on the unparsed
33814 functions queue. */
33816 static void
33817 cp_parser_save_default_args (cp_parser* parser, tree decl)
33819 tree probe;
33821 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
33822 probe;
33823 probe = TREE_CHAIN (probe))
33824 if (TREE_PURPOSE (probe))
33826 cp_default_arg_entry entry = {current_class_type, decl};
33827 vec_safe_push (unparsed_funs_with_default_args, entry);
33828 break;
33831 /* Remember if there is a noexcept-specifier to post process. */
33832 tree spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl));
33833 if (UNPARSED_NOEXCEPT_SPEC_P (spec))
33834 vec_safe_push (unparsed_noexcepts, decl);
33836 /* Contracts are deferred. */
33837 for (tree attr = DECL_ATTRIBUTES (decl); attr; attr = TREE_CHAIN (attr))
33838 if (cxx_contract_attribute_p (attr))
33840 vec_safe_push (unparsed_contracts, decl);
33841 break;
33845 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
33846 which is either a FIELD_DECL or PARM_DECL. Parse it and return
33847 the result. For a PARM_DECL, PARMTYPE is the corresponding type
33848 from the parameter-type-list. */
33850 static tree
33851 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
33852 tree default_arg, tree parmtype)
33854 cp_token_cache *tokens;
33855 tree parsed_arg;
33857 if (default_arg == error_mark_node)
33858 return error_mark_node;
33860 /* Push the saved tokens for the default argument onto the parser's
33861 lexer stack. */
33862 tokens = DEFPARSE_TOKENS (default_arg);
33863 cp_parser_push_lexer_for_tokens (parser, tokens);
33865 start_lambda_scope (decl);
33867 /* Parse the default argument. */
33868 parsed_arg = cp_parser_initializer (parser);
33869 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
33870 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
33872 finish_lambda_scope ();
33874 if (parsed_arg == error_mark_node)
33875 cp_parser_skip_to_end_of_statement (parser);
33877 if (!processing_template_decl)
33879 /* In a non-template class, check conversions now. In a template,
33880 we'll wait and instantiate these as needed. */
33881 if (TREE_CODE (decl) == PARM_DECL)
33882 parsed_arg = check_default_argument (parmtype, parsed_arg,
33883 tf_warning_or_error);
33884 else if (maybe_reject_flexarray_init (decl, parsed_arg))
33885 parsed_arg = error_mark_node;
33886 else
33887 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
33890 /* If the token stream has not been completely used up, then
33891 there was extra junk after the end of the default
33892 argument. */
33893 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
33895 if (TREE_CODE (decl) == PARM_DECL)
33896 cp_parser_error (parser, "expected %<,%>");
33897 else
33898 cp_parser_error (parser, "expected %<;%>");
33901 /* Revert to the main lexer. */
33902 cp_parser_pop_lexer (parser);
33904 return parsed_arg;
33907 /* FIELD is a non-static data member with an initializer which we saved for
33908 later; parse it now. */
33910 static void
33911 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
33913 tree def;
33915 maybe_begin_member_template_processing (field);
33917 push_unparsed_function_queues (parser);
33918 def = cp_parser_late_parse_one_default_arg (parser, field,
33919 DECL_INITIAL (field),
33920 NULL_TREE);
33921 pop_unparsed_function_queues (parser);
33923 maybe_end_member_template_processing ();
33925 DECL_INITIAL (field) = def;
33928 /* FN is a FUNCTION_DECL which may contains a parameter with an
33929 unparsed DEFERRED_PARSE. Parse the default args now. This function
33930 assumes that the current scope is the scope in which the default
33931 argument should be processed. */
33933 static void
33934 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
33936 unsigned char saved_local_variables_forbidden_p;
33938 /* While we're parsing the default args, we might (due to the
33939 statement expression extension) encounter more classes. We want
33940 to handle them right away, but we don't want them getting mixed
33941 up with default args that are currently in the queue. */
33942 push_unparsed_function_queues (parser);
33944 /* Local variable names (and the `this' keyword) may not appear
33945 in a default argument. */
33946 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
33947 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
33949 push_defarg_context (fn);
33951 begin_scope (sk_function_parms, fn);
33953 /* Gather the PARM_DECLs into a vec so we can keep track of them when
33954 pushdecl clears DECL_CHAIN. */
33955 releasing_vec parms;
33956 for (tree parmdecl = DECL_ARGUMENTS (fn); parmdecl;
33957 parmdecl = DECL_CHAIN (parmdecl))
33958 vec_safe_push (parms, parmdecl);
33960 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
33961 for (int i = 0;
33962 parm && parm != void_list_node;
33963 parm = TREE_CHAIN (parm),
33964 ++i)
33966 tree default_arg = TREE_PURPOSE (parm);
33967 tree parsed_arg;
33969 tree parmdecl = parms[i];
33970 pushdecl (parmdecl);
33972 if (!default_arg)
33973 continue;
33975 if (TREE_CODE (default_arg) != DEFERRED_PARSE)
33976 /* This can happen for a friend declaration for a function
33977 already declared with default arguments. */
33978 continue;
33980 parsed_arg
33981 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
33982 default_arg,
33983 TREE_VALUE (parm));
33984 TREE_PURPOSE (parm) = parsed_arg;
33986 /* Update any instantiations we've already created. */
33987 for (tree copy : DEFPARSE_INSTANTIATIONS (default_arg))
33988 TREE_PURPOSE (copy) = parsed_arg;
33991 pop_bindings_and_leave_scope ();
33993 /* Restore DECL_CHAINs after clobbering by pushdecl. */
33994 parm = NULL_TREE;
33995 for (int i = parms->length () - 1; i >= 0; --i)
33997 DECL_CHAIN (parms[i]) = parm;
33998 parm = parms[i];
34001 pop_defarg_context ();
34003 /* Make sure no default arg is missing. */
34004 check_default_args (fn);
34006 /* Restore the state of local_variables_forbidden_p. */
34007 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
34009 /* Restore the queue. */
34010 pop_unparsed_function_queues (parser);
34013 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
34015 sizeof ... ( identifier )
34017 where the 'sizeof' token has already been consumed. */
34019 static tree
34020 cp_parser_sizeof_pack (cp_parser *parser)
34022 /* Consume the `...'. */
34023 cp_lexer_consume_token (parser->lexer);
34024 maybe_warn_variadic_templates ();
34026 matching_parens parens;
34027 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
34028 if (paren)
34029 parens.consume_open (parser);
34030 else
34031 permerror (cp_lexer_peek_token (parser->lexer)->location,
34032 "%<sizeof...%> argument must be surrounded by parentheses");
34034 cp_token *token = cp_lexer_peek_token (parser->lexer);
34035 tree name = cp_parser_identifier (parser);
34036 if (name == error_mark_node)
34037 return error_mark_node;
34038 /* The name is not qualified. */
34039 parser->scope = NULL_TREE;
34040 parser->qualifying_scope = NULL_TREE;
34041 parser->object_scope = NULL_TREE;
34042 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
34043 if (expr == error_mark_node)
34044 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
34045 token->location);
34046 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
34047 expr = TREE_TYPE (expr);
34048 else if (TREE_CODE (expr) == CONST_DECL)
34049 expr = DECL_INITIAL (expr);
34050 expr = make_pack_expansion (expr);
34051 if (expr != error_mark_node)
34052 PACK_EXPANSION_SIZEOF_P (expr) = true;
34054 if (paren)
34055 parens.require_close (parser);
34057 return expr;
34060 /* Parse the operand of `sizeof' (or a similar operator). Returns
34061 either a TYPE or an expression, depending on the form of the
34062 input. The KEYWORD indicates which kind of expression we have
34063 encountered. */
34065 static tree
34066 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
34068 tree expr = NULL_TREE;
34069 const char *saved_message;
34070 const char *saved_message_arg;
34071 bool saved_integral_constant_expression_p;
34072 bool saved_non_integral_constant_expression_p;
34074 /* If it's a `...', then we are computing the length of a parameter
34075 pack. */
34076 if (keyword == RID_SIZEOF
34077 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
34078 return cp_parser_sizeof_pack (parser);
34080 /* Types cannot be defined in a `sizeof' expression. Save away the
34081 old message. */
34082 saved_message = parser->type_definition_forbidden_message;
34083 saved_message_arg = parser->type_definition_forbidden_message_arg;
34084 parser->type_definition_forbidden_message
34085 = G_("types may not be defined in %qs expressions");
34086 parser->type_definition_forbidden_message_arg
34087 = IDENTIFIER_POINTER (ridpointers[keyword]);
34089 /* The restrictions on constant-expressions do not apply inside
34090 sizeof expressions. */
34091 saved_integral_constant_expression_p
34092 = parser->integral_constant_expression_p;
34093 saved_non_integral_constant_expression_p
34094 = parser->non_integral_constant_expression_p;
34095 parser->integral_constant_expression_p = false;
34097 auto cleanup = make_temp_override
34098 (parser->auto_is_implicit_function_template_parm_p, false);
34100 /* Do not actually evaluate the expression. */
34101 ++cp_unevaluated_operand;
34102 ++c_inhibit_evaluation_warnings;
34103 /* If it's a `(', then we might be looking at the type-id
34104 construction. */
34105 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34107 tree type = NULL_TREE;
34109 tentative_firewall firewall (parser);
34111 /* We can't be sure yet whether we're looking at a type-id or an
34112 expression. */
34113 cp_parser_parse_tentatively (parser);
34115 matching_parens parens;
34116 parens.consume_open (parser);
34118 /* Note: as a GNU Extension, compound literals are considered
34119 postfix-expressions as they are in C99, so they are valid
34120 arguments to sizeof. See comment in cp_parser_cast_expression
34121 for details. */
34122 if (cp_parser_compound_literal_p (parser))
34123 cp_parser_simulate_error (parser);
34124 else
34126 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
34127 parser->in_type_id_in_expr_p = true;
34128 /* Look for the type-id. */
34129 type = cp_parser_type_id (parser);
34130 /* Look for the closing `)'. */
34131 parens.require_close (parser);
34132 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
34135 /* If all went well, then we're done. */
34136 if (cp_parser_parse_definitely (parser))
34137 expr = type;
34138 else
34140 /* Commit to the tentative_firewall so we get syntax errors. */
34141 cp_parser_commit_to_tentative_parse (parser);
34143 expr = cp_parser_unary_expression (parser);
34146 else
34147 expr = cp_parser_unary_expression (parser);
34149 /* Go back to evaluating expressions. */
34150 --cp_unevaluated_operand;
34151 --c_inhibit_evaluation_warnings;
34153 /* And restore the old one. */
34154 parser->type_definition_forbidden_message = saved_message;
34155 parser->type_definition_forbidden_message_arg = saved_message_arg;
34156 parser->integral_constant_expression_p
34157 = saved_integral_constant_expression_p;
34158 parser->non_integral_constant_expression_p
34159 = saved_non_integral_constant_expression_p;
34161 return expr;
34164 /* If the current declaration has no declarator, return true. */
34166 static bool
34167 cp_parser_declares_only_class_p (cp_parser *parser)
34169 /* If the next token is a `;' or a `,' then there is no
34170 declarator. */
34171 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
34172 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
34175 /* Update the DECL_SPECS to reflect the storage class indicated by
34176 KEYWORD. */
34178 static void
34179 cp_parser_set_storage_class (cp_parser *parser,
34180 cp_decl_specifier_seq *decl_specs,
34181 enum rid keyword,
34182 cp_token *token)
34184 cp_storage_class storage_class;
34186 switch (keyword)
34188 case RID_AUTO:
34189 storage_class = sc_auto;
34190 break;
34191 case RID_REGISTER:
34192 storage_class = sc_register;
34193 break;
34194 case RID_STATIC:
34195 storage_class = sc_static;
34196 break;
34197 case RID_EXTERN:
34198 storage_class = sc_extern;
34199 break;
34200 case RID_MUTABLE:
34201 storage_class = sc_mutable;
34202 break;
34203 default:
34204 gcc_unreachable ();
34207 if (parser->in_unbraced_linkage_specification_p)
34209 error_at (token->location, "invalid use of %qD in linkage specification",
34210 ridpointers[keyword]);
34211 return;
34213 else if (decl_specs->storage_class != sc_none)
34215 if (decl_specs->conflicting_specifiers_p)
34216 return;
34217 gcc_rich_location richloc (token->location);
34218 richloc.add_location_if_nearby (decl_specs->locations[ds_storage_class]);
34219 if (decl_specs->storage_class == storage_class)
34220 error_at (&richloc, "duplicate %qD specifier", ridpointers[keyword]);
34221 else
34222 error_at (&richloc,
34223 "%qD specifier conflicts with %qs",
34224 ridpointers[keyword],
34225 cp_storage_class_name[decl_specs->storage_class]);
34226 decl_specs->conflicting_specifiers_p = true;
34227 return;
34230 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
34231 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
34232 && decl_specs->gnu_thread_keyword_p)
34234 pedwarn (decl_specs->locations[ds_thread], 0,
34235 "%<__thread%> before %qD", ridpointers[keyword]);
34238 decl_specs->storage_class = storage_class;
34239 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
34241 /* A storage class specifier cannot be applied alongside a typedef
34242 specifier. If there is a typedef specifier present then set
34243 conflicting_specifiers_p which will trigger an error later
34244 on in grokdeclarator. */
34245 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
34246 && !decl_specs->conflicting_specifiers_p)
34248 gcc_rich_location richloc (token->location);
34249 richloc.add_location_if_nearby (decl_specs->locations[ds_typedef]);
34250 error_at (&richloc,
34251 "%qD specifier conflicts with %<typedef%>",
34252 ridpointers[keyword]);
34253 decl_specs->conflicting_specifiers_p = true;
34257 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
34258 is true, the type is a class or enum definition. */
34260 static void
34261 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
34262 tree type_spec,
34263 cp_token *token,
34264 bool type_definition_p)
34266 decl_specs->any_specifiers_p = true;
34268 /* If the user tries to redeclare bool, char8_t, char16_t, char32_t, or
34269 wchar_t (with, for example, in "typedef int wchar_t;") we remember that
34270 this is what happened. In system headers, we ignore these
34271 declarations so that G++ can work with system headers that are not
34272 C++-safe. */
34273 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
34274 && !type_definition_p
34275 && TYPE_P (type_spec)
34276 && (type_spec == boolean_type_node
34277 || type_spec == char8_type_node
34278 || type_spec == char16_type_node
34279 || type_spec == char32_type_node
34280 || extended_float_type_p (type_spec)
34281 || type_spec == wchar_type_node)
34282 && (decl_specs->type
34283 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
34284 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
34285 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
34286 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
34288 decl_specs->redefined_builtin_type = type_spec;
34289 set_and_check_decl_spec_loc (decl_specs,
34290 ds_redefined_builtin_type_spec,
34291 token);
34292 if (!decl_specs->type)
34294 decl_specs->type = type_spec;
34295 decl_specs->type_definition_p = false;
34296 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
34299 else if (decl_specs->type)
34300 decl_specs->multiple_types_p = true;
34301 else
34303 decl_specs->type = type_spec;
34304 decl_specs->type_definition_p = type_definition_p;
34305 decl_specs->redefined_builtin_type = NULL_TREE;
34306 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
34310 /* True iff TOKEN is the GNU keyword __thread. */
34312 static bool
34313 token_is__thread (cp_token *token)
34315 gcc_assert (token->keyword == RID_THREAD);
34316 return id_equal (token->u.value, "__thread");
34319 /* Set the location for a declarator specifier and check if it is
34320 duplicated.
34322 DECL_SPECS is the sequence of declarator specifiers onto which to
34323 set the location.
34325 DS is the single declarator specifier to set which location is to
34326 be set onto the existing sequence of declarators.
34328 LOCATION is the location for the declarator specifier to
34329 consider. */
34331 static void
34332 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
34333 cp_decl_spec ds, cp_token *token)
34335 gcc_assert (ds < ds_last);
34337 if (decl_specs == NULL)
34338 return;
34340 location_t location = token->location;
34342 if (decl_specs->locations[ds] == 0)
34344 decl_specs->locations[ds] = location;
34345 if (ds == ds_thread)
34346 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
34348 else
34350 if (ds == ds_long)
34352 if (decl_specs->locations[ds_long_long] != 0)
34353 error_at (location,
34354 "%<long long long%> is too long for GCC");
34355 else
34357 decl_specs->locations[ds_long_long] = location;
34358 pedwarn_cxx98 (location,
34359 OPT_Wlong_long,
34360 "ISO C++ 1998 does not support %<long long%>");
34363 else if (ds == ds_thread)
34365 bool gnu = token_is__thread (token);
34366 gcc_rich_location richloc (location);
34367 if (gnu != decl_specs->gnu_thread_keyword_p)
34369 richloc.add_range (decl_specs->locations[ds_thread]);
34370 error_at (&richloc,
34371 "both %<__thread%> and %<thread_local%> specified");
34373 else
34375 richloc.add_fixit_remove ();
34376 error_at (&richloc, "duplicate %qD", token->u.value);
34379 else
34381 /* These correspond to cp-tree.h:cp_decl_spec,
34382 changes here should also be reflected there. */
34383 static const char *const decl_spec_names[] = {
34384 "signed",
34385 "unsigned",
34386 "short",
34387 "long",
34388 "const",
34389 "volatile",
34390 "restrict",
34391 "inline",
34392 "virtual",
34393 "explicit",
34394 "friend",
34395 "typedef",
34396 "using",
34397 "constexpr",
34398 "__complex",
34399 "constinit",
34400 "consteval",
34401 "this"
34403 gcc_rich_location richloc (location);
34404 richloc.add_fixit_remove ();
34405 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
34410 /* Return true iff the declarator specifier DS is present in the
34411 sequence of declarator specifiers DECL_SPECS. */
34413 bool
34414 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
34415 cp_decl_spec ds)
34417 gcc_assert (ds < ds_last);
34419 if (decl_specs == NULL)
34420 return false;
34422 return decl_specs->locations[ds] != 0;
34425 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
34426 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
34428 static bool
34429 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
34431 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
34434 /* Issue an error message indicating that TOKEN_DESC was expected.
34435 If KEYWORD is true, it indicated this function is called by
34436 cp_parser_require_keword and the required token can only be
34437 a indicated keyword.
34439 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
34440 within any error as the location of an "opening" token matching
34441 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
34442 RT_CLOSE_PAREN). */
34444 static void
34445 cp_parser_required_error (cp_parser *parser,
34446 required_token token_desc,
34447 bool keyword,
34448 location_t matching_location)
34450 if (cp_parser_simulate_error (parser))
34451 return;
34453 const char *gmsgid = NULL;
34454 switch (token_desc)
34456 case RT_NEW:
34457 gmsgid = G_("expected %<new%>");
34458 break;
34459 case RT_DELETE:
34460 gmsgid = G_("expected %<delete%>");
34461 break;
34462 case RT_RETURN:
34463 gmsgid = G_("expected %<return%>");
34464 break;
34465 case RT_WHILE:
34466 gmsgid = G_("expected %<while%>");
34467 break;
34468 case RT_EXTERN:
34469 gmsgid = G_("expected %<extern%>");
34470 break;
34471 case RT_STATIC_ASSERT:
34472 gmsgid = G_("expected %<static_assert%>");
34473 break;
34474 case RT_DECLTYPE:
34475 gmsgid = G_("expected %<decltype%>");
34476 break;
34477 case RT_OPERATOR:
34478 gmsgid = G_("expected %<operator%>");
34479 break;
34480 case RT_CLASS:
34481 gmsgid = G_("expected %<class%>");
34482 break;
34483 case RT_TEMPLATE:
34484 gmsgid = G_("expected %<template%>");
34485 break;
34486 case RT_NAMESPACE:
34487 gmsgid = G_("expected %<namespace%>");
34488 break;
34489 case RT_USING:
34490 gmsgid = G_("expected %<using%>");
34491 break;
34492 case RT_ASM:
34493 gmsgid = G_("expected %<asm%>");
34494 break;
34495 case RT_TRY:
34496 gmsgid = G_("expected %<try%>");
34497 break;
34498 case RT_CATCH:
34499 gmsgid = G_("expected %<catch%>");
34500 break;
34501 case RT_THROW:
34502 gmsgid = G_("expected %<throw%>");
34503 break;
34504 case RT_AUTO:
34505 gmsgid = G_("expected %<auto%>");
34506 break;
34507 case RT_LABEL:
34508 gmsgid = G_("expected %<__label__%>");
34509 break;
34510 case RT_AT_TRY:
34511 gmsgid = G_("expected %<@try%>");
34512 break;
34513 case RT_AT_SYNCHRONIZED:
34514 gmsgid = G_("expected %<@synchronized%>");
34515 break;
34516 case RT_AT_THROW:
34517 gmsgid = G_("expected %<@throw%>");
34518 break;
34519 case RT_TRANSACTION_ATOMIC:
34520 gmsgid = G_("expected %<__transaction_atomic%>");
34521 break;
34522 case RT_TRANSACTION_RELAXED:
34523 gmsgid = G_("expected %<__transaction_relaxed%>");
34524 break;
34525 case RT_CO_YIELD:
34526 gmsgid = G_("expected %<co_yield%>");
34527 break;
34528 default:
34529 break;
34532 if (!gmsgid && !keyword)
34534 switch (token_desc)
34536 case RT_SEMICOLON:
34537 gmsgid = G_("expected %<;%>");
34538 break;
34539 case RT_OPEN_PAREN:
34540 gmsgid = G_("expected %<(%>");
34541 break;
34542 case RT_CLOSE_BRACE:
34543 gmsgid = G_("expected %<}%>");
34544 break;
34545 case RT_OPEN_BRACE:
34546 gmsgid = G_("expected %<{%>");
34547 break;
34548 case RT_CLOSE_SQUARE:
34549 gmsgid = G_("expected %<]%>");
34550 break;
34551 case RT_OPEN_SQUARE:
34552 gmsgid = G_("expected %<[%>");
34553 break;
34554 case RT_COMMA:
34555 gmsgid = G_("expected %<,%>");
34556 break;
34557 case RT_SCOPE:
34558 gmsgid = G_("expected %<::%>");
34559 break;
34560 case RT_LESS:
34561 gmsgid = G_("expected %<<%>");
34562 break;
34563 case RT_GREATER:
34564 gmsgid = G_("expected %<>%>");
34565 break;
34566 case RT_EQ:
34567 gmsgid = G_("expected %<=%>");
34568 break;
34569 case RT_ELLIPSIS:
34570 gmsgid = G_("expected %<...%>");
34571 break;
34572 case RT_MULT:
34573 gmsgid = G_("expected %<*%>");
34574 break;
34575 case RT_COMPL:
34576 gmsgid = G_("expected %<~%>");
34577 break;
34578 case RT_COLON:
34579 gmsgid = G_("expected %<:%>");
34580 break;
34581 case RT_COLON_SCOPE:
34582 gmsgid = G_("expected %<:%> or %<::%>");
34583 break;
34584 case RT_CLOSE_PAREN:
34585 gmsgid = G_("expected %<)%>");
34586 break;
34587 case RT_COMMA_CLOSE_PAREN:
34588 gmsgid = G_("expected %<,%> or %<)%>");
34589 break;
34590 case RT_PRAGMA_EOL:
34591 gmsgid = G_("expected end of line");
34592 break;
34593 case RT_NAME:
34594 gmsgid = G_("expected identifier");
34595 break;
34596 case RT_SELECT:
34597 gmsgid = G_("expected selection-statement");
34598 break;
34599 case RT_ITERATION:
34600 gmsgid = G_("expected iteration-statement");
34601 break;
34602 case RT_JUMP:
34603 gmsgid = G_("expected jump-statement");
34604 break;
34605 case RT_CLASS_KEY:
34606 gmsgid = G_("expected class-key");
34607 break;
34608 case RT_CLASS_TYPENAME_TEMPLATE:
34609 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
34610 break;
34611 default:
34612 gcc_unreachable ();
34616 if (gmsgid)
34617 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
34621 /* If the next token is of the indicated TYPE, consume it. Otherwise,
34622 issue an error message indicating that TOKEN_DESC was expected.
34624 Returns the token consumed, if the token had the appropriate type.
34625 Otherwise, returns NULL.
34627 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
34628 within any error as the location of an "opening" token matching
34629 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
34630 RT_CLOSE_PAREN). */
34632 static cp_token *
34633 cp_parser_require (cp_parser* parser,
34634 enum cpp_ttype type,
34635 required_token token_desc,
34636 location_t matching_location)
34638 if (cp_lexer_next_token_is (parser->lexer, type))
34639 return cp_lexer_consume_token (parser->lexer);
34640 else
34642 /* Output the MESSAGE -- unless we're parsing tentatively. */
34643 if (!cp_parser_simulate_error (parser))
34644 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
34645 matching_location);
34646 return NULL;
34650 /* Skip an entire parameter list from start to finish. The next token must
34651 be the initial "<" of the parameter list. Returns true on success and
34652 false otherwise. */
34654 static bool
34655 cp_parser_skip_entire_template_parameter_list (cp_parser* parser)
34657 /* Consume the "<" because cp_parser_skip_to_end_of_template_parameter_list
34658 requires it. */
34659 cp_lexer_consume_token (parser->lexer);
34660 return cp_parser_skip_to_end_of_template_parameter_list (parser);
34663 /* Ensure we are at the end of a template parameter list. If we are, return.
34664 If we are not, something has gone wrong, in which case issue an error and
34665 skip to end of the parameter list. */
34667 static void
34668 cp_parser_require_end_of_template_parameter_list (cp_parser* parser)
34670 /* Are we ready, yet? If not, issue error message. */
34671 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
34672 return;
34674 cp_parser_skip_to_end_of_template_parameter_list (parser);
34677 /* You should only call this function from inside a template parameter list
34678 (i.e. the current token should at least be the initial "<" of the
34679 parameter list). If you are skipping the entire list, it may be better to
34680 use cp_parser_skip_entire_template_parameter_list.
34682 Tokens are skipped until the final ">" is found, or if we see
34683 '{', '}', ';', or if we find an unbalanced ')' or ']'.
34685 Returns true if we successfully reached the end, and false if
34686 something unexpected happened (e.g. end of file). */
34688 static bool
34689 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
34691 /* Current level of '< ... >'. */
34692 unsigned level = 0;
34693 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
34694 unsigned nesting_depth = 0;
34696 /* Skip tokens until the desired token is found. */
34697 while (true)
34699 /* Peek at the next token. */
34700 switch (cp_lexer_peek_token (parser->lexer)->type)
34702 case CPP_LESS:
34703 if (!nesting_depth)
34704 ++level;
34705 break;
34707 case CPP_RSHIFT:
34708 if (cxx_dialect == cxx98)
34709 /* C++0x views the `>>' operator as two `>' tokens, but
34710 C++98 does not. */
34711 break;
34712 else if (!nesting_depth && level-- == 0)
34714 /* We've hit a `>>' where the first `>' closes the
34715 template argument list, and the second `>' is
34716 spurious. Just consume the `>>' and stop; we've
34717 already produced at least one error. */
34718 cp_lexer_consume_token (parser->lexer);
34719 return false;
34721 /* Fall through for C++0x, so we handle the second `>' in
34722 the `>>'. */
34723 gcc_fallthrough ();
34725 case CPP_GREATER:
34726 if (!nesting_depth && level-- == 0)
34728 /* We've reached the token we want, consume it and stop. */
34729 cp_lexer_consume_token (parser->lexer);
34730 return true;
34732 break;
34734 case CPP_OPEN_PAREN:
34735 case CPP_OPEN_SQUARE:
34736 ++nesting_depth;
34737 break;
34739 case CPP_CLOSE_PAREN:
34740 case CPP_CLOSE_SQUARE:
34741 if (nesting_depth-- == 0)
34742 return false;
34743 break;
34745 case CPP_EOF:
34746 case CPP_PRAGMA_EOL:
34747 case CPP_SEMICOLON:
34748 case CPP_OPEN_BRACE:
34749 case CPP_CLOSE_BRACE:
34750 /* The '>' was probably forgotten, don't look further. */
34751 return false;
34753 default:
34754 break;
34757 /* Consume this token. */
34758 cp_lexer_consume_token (parser->lexer);
34762 /* If the next token is the indicated keyword, consume it. Otherwise,
34763 issue an error message indicating that TOKEN_DESC was expected.
34765 Returns the token consumed, if the token had the appropriate type.
34766 Otherwise, returns NULL. */
34768 static cp_token *
34769 cp_parser_require_keyword (cp_parser* parser,
34770 enum rid keyword,
34771 required_token token_desc)
34773 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
34775 if (token && token->keyword != keyword)
34777 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
34778 UNKNOWN_LOCATION);
34779 return NULL;
34782 return token;
34785 /* Returns TRUE iff TOKEN is a token that can begin the body of a
34786 function-definition. */
34788 static bool
34789 cp_parser_token_starts_function_definition_p (cp_token* token)
34791 return (/* An ordinary function-body begins with an `{'. */
34792 token->type == CPP_OPEN_BRACE
34793 /* A ctor-initializer begins with a `:'. */
34794 || token->type == CPP_COLON
34795 /* A function-try-block begins with `try'. */
34796 || token->keyword == RID_TRY
34797 /* A function-transaction-block begins with `__transaction_atomic'
34798 or `__transaction_relaxed'. */
34799 || token->keyword == RID_TRANSACTION_ATOMIC
34800 || token->keyword == RID_TRANSACTION_RELAXED
34801 /* The named return value extension begins with `return'. */
34802 || token->keyword == RID_RETURN);
34805 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
34806 definition. */
34808 static bool
34809 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
34811 cp_token *token;
34813 token = cp_lexer_peek_token (parser->lexer);
34814 return (token->type == CPP_OPEN_BRACE
34815 || (token->type == CPP_COLON
34816 && !parser->colon_doesnt_start_class_def_p));
34819 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
34820 C++0x) ending a template-argument. */
34822 static bool
34823 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
34825 cp_token *token;
34827 token = cp_lexer_peek_token (parser->lexer);
34828 return (token->type == CPP_COMMA
34829 || token->type == CPP_GREATER
34830 || token->type == CPP_ELLIPSIS
34831 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)
34832 /* For better diagnostics, treat >>= like that too, that
34833 shouldn't appear non-nested in template arguments. */
34834 || token->type == CPP_RSHIFT_EQ);
34837 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
34838 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
34840 static bool
34841 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
34842 size_t n)
34844 cp_token *token;
34846 token = cp_lexer_peek_nth_token (parser->lexer, n);
34847 if (token->type == CPP_LESS)
34848 return true;
34849 /* Check for the sequence `<::' in the original code. It would be lexed as
34850 `[:', where `[' is a digraph, and there is no whitespace before
34851 `:'. */
34852 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
34854 cp_token *token2;
34855 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
34856 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
34857 return true;
34859 return false;
34862 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
34863 or none_type otherwise. */
34865 static enum tag_types
34866 cp_parser_token_is_class_key (cp_token* token)
34868 switch (token->keyword)
34870 case RID_CLASS:
34871 return class_type;
34872 case RID_STRUCT:
34873 return record_type;
34874 case RID_UNION:
34875 return union_type;
34877 default:
34878 return none_type;
34882 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
34883 or none_type otherwise or if the token is null. */
34885 static enum tag_types
34886 cp_parser_token_is_type_parameter_key (cp_token* token)
34888 if (!token)
34889 return none_type;
34891 switch (token->keyword)
34893 case RID_CLASS:
34894 return class_type;
34895 case RID_TYPENAME:
34896 return typename_type;
34898 default:
34899 return none_type;
34903 /* Diagnose redundant enum-keys. */
34905 static void
34906 cp_parser_maybe_warn_enum_key (cp_parser *parser, location_t key_loc,
34907 tree type, rid scoped_key)
34909 if (!warn_redundant_tags)
34910 return;
34912 tree type_decl = TYPE_MAIN_DECL (type);
34913 tree name = DECL_NAME (type_decl);
34914 /* Look up the NAME to see if it unambiguously refers to the TYPE. */
34915 push_deferring_access_checks (dk_no_check);
34916 tree decl = cp_parser_lookup_name_simple (parser, name, input_location);
34917 pop_deferring_access_checks ();
34919 /* The enum-key is redundant for uses of the TYPE that are not
34920 declarations and for which name lookup returns just the type
34921 itself. */
34922 if (decl != type_decl)
34923 return;
34925 if (scoped_key != RID_CLASS
34926 && scoped_key != RID_STRUCT
34927 && current_lang_name != lang_name_cplusplus
34928 && current_namespace == global_namespace)
34930 /* Avoid issuing the diagnostic for apparently redundant (unscoped)
34931 enum tag in shared C/C++ code in files (such as headers) included
34932 in the main source file. */
34933 const line_map_ordinary *map = NULL;
34934 linemap_resolve_location (line_table, key_loc,
34935 LRK_MACRO_DEFINITION_LOCATION,
34936 &map);
34937 if (!MAIN_FILE_P (map))
34938 return;
34941 gcc_rich_location richloc (key_loc);
34942 richloc.add_fixit_remove (key_loc);
34943 warning_at (&richloc, OPT_Wredundant_tags,
34944 "redundant enum-key %<enum%s%> in reference to %q#T",
34945 (scoped_key == RID_CLASS ? " class"
34946 : scoped_key == RID_STRUCT ? " struct" : ""), type);
34949 /* Describes the set of declarations of a struct, class, or class template
34950 or its specializations. Used for -Wmismatched-tags. */
34952 class class_decl_loc_t
34954 public:
34956 class_decl_loc_t ()
34957 : locvec (), idxdef (), def_class_key ()
34959 locvec.create (4);
34962 /* Constructs an object for a single declaration of a class with
34963 CLASS_KEY at the current location in the current function (or
34964 at another scope). KEY_REDUNDANT is true if the class-key may
34965 be omitted in the current context without an ambiguity with
34966 another symbol with the same name.
34967 DEF_P is true for a class declaration that is a definition.
34968 CURLOC is the associated location. */
34969 class_decl_loc_t (tag_types class_key, bool key_redundant, bool def_p,
34970 location_t curloc = input_location)
34971 : locvec (), idxdef (def_p ? 0 : UINT_MAX), def_class_key (class_key)
34973 locvec.create (4);
34974 class_key_loc_t ckl (current_function_decl, curloc, class_key,
34975 key_redundant);
34976 locvec.quick_push (ckl);
34979 /* Copy, assign, and destroy the object. Necessary because LOCVEC
34980 isn't safely copyable and assignable and doesn't release storage
34981 on its own. */
34982 class_decl_loc_t (const class_decl_loc_t &rhs)
34983 : locvec (rhs.locvec.copy ()), idxdef (rhs.idxdef),
34984 def_class_key (rhs.def_class_key)
34987 class_decl_loc_t& operator= (const class_decl_loc_t &rhs)
34989 if (this == &rhs)
34990 return *this;
34991 locvec.release ();
34992 locvec = rhs.locvec.copy ();
34993 idxdef = rhs.idxdef;
34994 def_class_key = rhs.def_class_key;
34995 return *this;
34998 ~class_decl_loc_t ()
35000 locvec.release ();
35003 /* Issues -Wmismatched-tags for a single class. */
35004 void diag_mismatched_tags (tree);
35006 /* Issues -Wmismatched-tags for all classes. */
35007 static void diag_mismatched_tags ();
35009 /* Adds TYPE_DECL to the collection of class decls and diagnoses
35010 redundant tags (if -Wredundant-tags is enabled). */
35011 static void add (cp_parser *, location_t, tag_types, tree, bool, bool);
35013 /* Either adds this decl to the collection of class decls
35014 or diagnoses it, whichever is appropriate. */
35015 void add_or_diag_mismatched_tag (tree, tag_types, bool, bool);
35017 private:
35019 tree function (unsigned i) const
35021 return locvec[i].func;
35024 location_t location (unsigned i) const
35026 return locvec[i].loc;
35029 bool key_redundant (unsigned i) const
35031 return locvec[i].key_redundant;
35034 tag_types class_key (unsigned i) const
35036 return locvec[i].class_key;
35039 /* True if a definition for the class has been seen. */
35040 bool def_p () const
35042 return idxdef < locvec.length ();
35045 /* The location of a single mention of a class type with the given
35046 class-key. */
35047 struct class_key_loc_t
35049 class_key_loc_t (tree func, location_t loc, tag_types key, bool redundant)
35050 : func (func), loc (loc), class_key (key), key_redundant (redundant)
35053 /* The function the type is mentioned in. */
35054 tree func;
35055 /* The exact location. */
35056 location_t loc;
35057 /* The class-key used in the mention of the type. */
35058 tag_types class_key;
35059 /* True when the class-key could be omitted at this location
35060 without an ambiguity with another symbol of the same name. */
35061 bool key_redundant;
35063 /* Avoid using auto_vec here since it's not safe to copy due to pr90904. */
35064 vec <class_key_loc_t> locvec;
35065 /* LOCVEC index of the definition or UINT_MAX if none exists. */
35066 unsigned idxdef;
35067 /* The class-key the class was last declared with or none_type when
35068 it has been declared with a mismatched key. */
35069 tag_types def_class_key;
35071 /* A mapping between a TYPE_DECL for a class and the class_decl_loc_t
35072 description above. */
35073 typedef hash_map<tree_decl_hash, class_decl_loc_t> class_to_loc_map_t;
35074 static class_to_loc_map_t class2loc;
35077 class_decl_loc_t::class_to_loc_map_t class_decl_loc_t::class2loc;
35079 /* Issue an error message if the CLASS_KEY does not match the TYPE.
35080 DEF_P is expected to be set for a definition of class TYPE. DECL_P
35081 is set for a declaration of class TYPE and clear for a reference to
35082 it that is not a declaration of it. */
35084 static void
35085 cp_parser_check_class_key (cp_parser *parser, location_t key_loc,
35086 tag_types class_key, tree type, bool def_p,
35087 bool decl_p)
35089 if (type == error_mark_node)
35090 return;
35092 bool seen_as_union = TREE_CODE (type) == UNION_TYPE;
35093 if (seen_as_union != (class_key == union_type))
35095 if (permerror (input_location, "%qs tag used in naming %q#T",
35096 class_key == union_type ? "union"
35097 : class_key == record_type ? "struct" : "class",
35098 type))
35099 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
35100 "%q#T was previously declared here", type);
35101 return;
35104 if (!warn_mismatched_tags && !warn_redundant_tags)
35105 return;
35107 /* Only consider the true class-keys below and ignore typename_type,
35108 etc. that are not C++ class-keys. */
35109 if (class_key != class_type
35110 && class_key != record_type
35111 && class_key != union_type)
35112 return;
35114 class_decl_loc_t::add (parser, key_loc, class_key, type, def_p, decl_p);
35117 /* Returns the template or specialization of one to which the RECORD_TYPE
35118 TYPE corresponds. */
35120 static tree
35121 specialization_of (tree type)
35123 tree ret = type;
35125 /* Determine the template or its partial specialization to which TYPE
35126 corresponds. */
35127 if (tree ti = most_specialized_partial_spec (type, tf_none))
35128 if (ti != error_mark_node)
35129 ret = TREE_TYPE (TI_TEMPLATE (ti));
35131 if (ret == type)
35132 ret = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (type);
35134 return TYPE_MAIN_DECL (ret);
35138 /* Adds the class TYPE to the collection of class decls and diagnoses
35139 redundant tags (if -Wredundant-tags is enabled).
35140 DEF_P is expected to be set for a definition of class TYPE. DECL_P
35141 is set for a (likely, based on syntactic context) declaration of class
35142 TYPE and clear for a reference to it that is not a declaration of it. */
35144 void
35145 class_decl_loc_t::add (cp_parser *parser, location_t key_loc,
35146 tag_types class_key, tree type, bool def_p, bool decl_p)
35148 tree type_decl = TYPE_MAIN_DECL (type);
35149 tree name = DECL_NAME (type_decl);
35150 /* Look up the NAME to see if it unambiguously refers to the TYPE
35151 and set KEY_REDUNDANT if so. */
35152 push_deferring_access_checks (dk_no_check);
35153 tree decl = cp_parser_lookup_name_simple (parser, name, input_location);
35154 pop_deferring_access_checks ();
35156 /* The class-key is redundant for uses of the CLASS_TYPE that are
35157 neither definitions of it nor declarations, and for which name
35158 lookup returns just the type itself. */
35159 bool key_redundant = (!def_p && !decl_p
35160 && (decl == type_decl
35161 || TREE_CODE (decl) == TEMPLATE_DECL
35162 || (CLASS_TYPE_P (type)
35163 && TYPE_BEING_DEFINED (type))));
35165 if (key_redundant
35166 && class_key != class_type
35167 && current_lang_name != lang_name_cplusplus
35168 && current_namespace == global_namespace)
35170 /* Avoid issuing the diagnostic for apparently redundant struct
35171 and union class-keys in shared C/C++ code in files (such as
35172 headers) included in the main source file. */
35173 const line_map_ordinary *map = NULL;
35174 linemap_resolve_location (line_table, key_loc,
35175 LRK_MACRO_DEFINITION_LOCATION,
35176 &map);
35177 if (!MAIN_FILE_P (map))
35178 key_redundant = false;
35181 /* Set if a declaration of TYPE has previously been seen or if it must
35182 exist in a precompiled header. */
35183 bool exist;
35184 class_decl_loc_t *rdl = &class2loc.get_or_insert (type_decl, &exist);
35185 if (!exist)
35187 tree type = TREE_TYPE (type_decl);
35188 if (def_p || !COMPLETE_TYPE_P (type))
35190 /* TYPE_DECL is the first declaration or definition of the type
35191 (outside precompiled headers -- see below). Just create
35192 a new entry for it and return unless it's a declaration
35193 involving a template that may need to be diagnosed by
35194 -Wredundant-tags. */
35195 *rdl = class_decl_loc_t (class_key, false, def_p);
35196 if (TREE_CODE (decl) != TEMPLATE_DECL)
35197 return;
35199 else
35201 /* TYPE was previously defined in some unknown precompiled header.
35202 Simply add a record of its definition at an unknown location and
35203 proceed below to add a reference to it at the current location.
35204 (Declarations in precompiled headers that are not definitions
35205 are ignored.) */
35206 tag_types def_key
35207 = CLASSTYPE_DECLARED_CLASS (type) ? class_type : record_type;
35208 location_t def_loc = DECL_SOURCE_LOCATION (type_decl);
35209 *rdl = class_decl_loc_t (def_key, false, true, def_loc);
35210 exist = true;
35214 /* A prior declaration of TYPE_DECL has been seen. */
35216 if (key_redundant)
35218 gcc_rich_location richloc (key_loc);
35219 richloc.add_fixit_remove (key_loc);
35220 warning_at (&richloc, OPT_Wredundant_tags,
35221 "redundant class-key %qs in reference to %q#T",
35222 class_key == union_type ? "union"
35223 : class_key == record_type ? "struct" : "class",
35224 type);
35227 if (!exist)
35228 /* Do nothing if this is the first declaration of the type. */
35229 return;
35231 if (rdl->idxdef != UINT_MAX && rdl->def_class_key == class_key)
35232 /* Do nothing if the class-key in this declaration matches
35233 the definition. */
35234 return;
35236 rdl->add_or_diag_mismatched_tag (type_decl, class_key, key_redundant,
35237 def_p);
35240 /* Either adds this DECL corresponding to the TYPE_DECL to the collection
35241 of class decls or diagnoses it, whichever is appropriate. */
35243 void
35244 class_decl_loc_t::add_or_diag_mismatched_tag (tree type_decl,
35245 tag_types class_key,
35246 bool redundant,
35247 bool def_p)
35249 /* Reset the CLASS_KEY associated with this type on mismatch.
35250 This is an optimization that lets the diagnostic code skip
35251 over classes that use the same class-key in all declarations. */
35252 if (def_class_key != class_key)
35253 def_class_key = none_type;
35255 /* Set IDXDEF to the index of the vector corresponding to
35256 the definition. */
35257 if (def_p)
35258 idxdef = locvec.length ();
35260 /* Append a record of this declaration to the vector. */
35261 class_key_loc_t ckl (current_function_decl, input_location, class_key,
35262 redundant);
35263 locvec.safe_push (ckl);
35265 if (idxdef == UINT_MAX)
35266 return;
35268 /* As a space optimization diagnose declarations of a class
35269 whose definition has been seen and purge the LOCVEC of
35270 all entries except the definition. */
35271 diag_mismatched_tags (type_decl);
35272 if (idxdef)
35274 class_decl_loc_t::class_key_loc_t ent = locvec[idxdef];
35275 locvec.release ();
35276 locvec.reserve (2);
35277 locvec.safe_push (ent);
35278 idxdef = 0;
35280 else
35281 /* Pop the entry pushed above for this declaration. */
35282 locvec.pop ();
35285 /* Issues -Wmismatched-tags for a single class. */
35287 void
35288 class_decl_loc_t::diag_mismatched_tags (tree type_decl)
35290 if (!warn_mismatched_tags)
35291 return;
35293 /* Number of uses of the class. */
35294 const unsigned ndecls = locvec.length ();
35296 /* The class (or template) declaration guiding the decisions about
35297 the diagnostic. For ordinary classes it's the same as THIS. For
35298 uses of instantiations of templates other than their declarations
35299 it points to the record for the declaration of the corresponding
35300 primary template or partial specialization. */
35301 class_decl_loc_t *cdlguide = this;
35303 tree type = TREE_TYPE (type_decl);
35304 if (CLASS_TYPE_P (type) && CLASSTYPE_IMPLICIT_INSTANTIATION (type))
35306 /* For implicit instantiations of a primary template look up
35307 the primary or partial specialization and use it as
35308 the expected class-key rather than using the class-key of
35309 the first reference to the instantiation. The primary must
35310 be (and inevitably is) at index zero. */
35311 tree spec = specialization_of (type);
35312 cdlguide = class2loc.get (spec);
35313 /* It's possible that we didn't find SPEC. Consider:
35315 template<typename T> struct A {
35316 template<typename U> struct W { };
35318 struct A<int>::W<int> w; // #1
35320 where while parsing A and #1 we've stashed
35321 A<T>
35322 A<T>::W<U>
35323 A<int>::W<int>
35324 into CLASS2LOC. If TYPE is A<int>::W<int>, specialization_of
35325 will yield A<int>::W<U> which may be in CLASS2LOC if we had
35326 an A<int> class specialization, but otherwise won't be in it.
35327 So try to look up A<T>::W<U>. */
35328 if (!cdlguide)
35330 spec = DECL_TEMPLATE_RESULT (most_general_template (spec));
35331 cdlguide = class2loc.get (spec);
35333 /* Now we really should have found something. */
35334 gcc_assert (cdlguide != NULL);
35336 /* Skip declarations that consistently use the same class-key. */
35337 else if (def_class_key != none_type)
35338 return;
35340 /* Set if a definition for the class has been seen. */
35341 const bool def_p = cdlguide->def_p ();
35343 /* The index of the declaration whose class-key this declaration
35344 is expected to match. It's either the class-key of the class
35345 definition if one exists or the first declaration otherwise. */
35346 const unsigned idxguide = def_p ? cdlguide->idxdef : 0;
35348 /* The class-key the class is expected to be declared with: it's
35349 either the key used in its definition or the first declaration
35350 if no definition has been provided.
35351 For implicit instantiations of a primary template it's
35352 the class-key used to declare the primary with. The primary
35353 must be at index zero. */
35354 const tag_types xpect_key = cdlguide->class_key (idxguide);
35356 unsigned idx = 0;
35357 /* Advance IDX to the first declaration that either is not
35358 a definition or that doesn't match the first declaration
35359 if no definition is provided. */
35360 while (class_key (idx) == xpect_key)
35361 if (++idx == ndecls)
35362 return;
35364 /* Save the current function before changing it below. */
35365 tree save_func = current_function_decl;
35366 /* Set the function declaration to print in diagnostic context. */
35367 current_function_decl = function (idx);
35369 const char *xmatchkstr = xpect_key == record_type ? "class" : "struct";
35370 const char *xpectkstr = xpect_key == record_type ? "struct" : "class";
35372 location_t loc = location (idx);
35373 bool key_redundant_p = key_redundant (idx);
35374 auto_diagnostic_group d;
35375 /* Issue a warning for the first mismatched declaration.
35376 Avoid using "%#qT" since the class-key for the same type will
35377 be the same regardless of which one was used in the declaraion. */
35378 if (warning_at (loc, OPT_Wmismatched_tags,
35379 "%qT declared with a mismatched class-key %qs",
35380 type_decl, xmatchkstr))
35382 /* Suggest how to avoid the warning for each instance since
35383 the guidance may be different depending on context. */
35384 inform (loc,
35385 (key_redundant_p
35386 ? G_("remove the class-key or replace it with %qs")
35387 : G_("replace the class-key with %qs")),
35388 xpectkstr);
35390 /* Also point to the first declaration or definition that guided
35391 the decision to issue the warning above. */
35392 inform (cdlguide->location (idxguide),
35393 (def_p
35394 ? G_("%qT defined as %qs here")
35395 : G_("%qT first declared as %qs here")),
35396 type_decl, xpectkstr);
35399 /* Issue warnings for the remaining inconsistent declarations. */
35400 for (unsigned i = idx + 1; i != ndecls; ++i)
35402 tag_types clskey = class_key (i);
35403 /* Skip over the declarations that match either the definition
35404 if one was provided or the first declaration. */
35405 if (clskey == xpect_key)
35406 continue;
35408 loc = location (i);
35409 key_redundant_p = key_redundant (i);
35410 /* Set the function declaration to print in diagnostic context. */
35411 current_function_decl = function (i);
35412 if (warning_at (loc, OPT_Wmismatched_tags,
35413 "%qT declared with a mismatched class-key %qs",
35414 type_decl, xmatchkstr))
35415 /* Suggest how to avoid the warning for each instance since
35416 the guidance may be different depending on context. */
35417 inform (loc,
35418 (key_redundant_p
35419 ? G_("remove the class-key or replace it with %qs")
35420 : G_("replace the class-key with %qs")),
35421 xpectkstr);
35424 /* Restore the current function in case it was replaced above. */
35425 current_function_decl = save_func;
35428 /* Issues -Wmismatched-tags for all classes. Called at the end
35429 of processing a translation unit, after declarations of all class
35430 types and their uses have been recorded. */
35432 void
35433 class_decl_loc_t::diag_mismatched_tags ()
35435 /* CLASS2LOC should be empty if both -Wmismatched-tags and
35436 -Wredundant-tags are disabled. */
35437 gcc_assert (warn_mismatched_tags
35438 || warn_redundant_tags
35439 || class2loc.is_empty ());
35441 /* Save the current function before changing on return. It should
35442 be null at this point. */
35443 temp_override<tree> cleanup (current_function_decl);
35445 if (warn_mismatched_tags)
35447 /* Iterate over the collected class/struct/template declarations. */
35448 typedef class_to_loc_map_t::iterator iter_t;
35449 for (iter_t it = class2loc.begin (); it != class2loc.end (); ++it)
35451 tree type_decl = (*it).first;
35452 class_decl_loc_t &recloc = (*it).second;
35453 recloc.diag_mismatched_tags (type_decl);
35457 class2loc.empty ();
35460 /* Issue an error message if DECL is redeclared with different
35461 access than its original declaration [class.access.spec/3].
35462 This applies to nested classes, nested class templates and
35463 enumerations [class.mem/1]. */
35465 static void
35466 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
35468 if (!decl
35469 || (!(CLASS_TYPE_P (TREE_TYPE (decl))
35470 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
35471 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
35472 return;
35474 if ((TREE_PRIVATE (decl)
35475 != (current_access_specifier == access_private_node))
35476 || (TREE_PROTECTED (decl)
35477 != (current_access_specifier == access_protected_node)))
35478 error_at (location, "%qD redeclared with different access", decl);
35481 /* Look for the `template' keyword, as a syntactic disambiguator.
35482 Return TRUE iff it is present, in which case it will be
35483 consumed. */
35485 static bool
35486 cp_parser_optional_template_keyword (cp_parser *parser)
35488 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
35490 /* In C++98 the `template' keyword can only be used within templates;
35491 outside templates the parser can always figure out what is a
35492 template and what is not. In C++11, per the resolution of DR 468,
35493 `template' is allowed in cases where it is not strictly necessary. */
35494 if (!processing_template_decl
35495 && pedantic && cxx_dialect == cxx98)
35497 cp_token *token = cp_lexer_peek_token (parser->lexer);
35498 pedwarn (token->location, OPT_Wpedantic,
35499 "in C++98 %<template%> (as a disambiguator) is only "
35500 "allowed within templates");
35501 /* If this part of the token stream is rescanned, the same
35502 error message would be generated. So, we purge the token
35503 from the stream. */
35504 cp_lexer_purge_token (parser->lexer);
35505 return false;
35507 else
35509 /* Consume the `template' keyword. */
35510 cp_lexer_consume_token (parser->lexer);
35511 return true;
35514 return false;
35517 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
35518 set PARSER->SCOPE, and perform other related actions. */
35520 static void
35521 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
35523 struct tree_check *check_value;
35525 /* Get the stored value. */
35526 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
35527 /* Set the scope from the stored value. */
35528 parser->scope = saved_checks_value (check_value);
35529 parser->qualifying_scope = check_value->qualifying_scope;
35530 parser->object_scope = parser->context->object_type;
35531 parser->context->object_type = NULL_TREE;
35534 /* Consume tokens up through a non-nested END token. Returns TRUE if we
35535 encounter the end of a block before what we were looking for. */
35537 static bool
35538 cp_parser_cache_group (cp_parser *parser,
35539 enum cpp_ttype end,
35540 unsigned depth)
35542 while (true)
35544 cp_token *token = cp_lexer_peek_token (parser->lexer);
35546 /* Abort a parenthesized expression if we encounter a semicolon. */
35547 if ((end == CPP_CLOSE_PAREN || depth == 0)
35548 && token->type == CPP_SEMICOLON)
35549 return true;
35550 /* If we've reached the end of the file, stop. */
35551 if (token->type == CPP_EOF
35552 || (end != CPP_PRAGMA_EOL
35553 && token->type == CPP_PRAGMA_EOL))
35554 return true;
35555 if (token->type == CPP_CLOSE_BRACE && depth == 0)
35556 /* We've hit the end of an enclosing block, so there's been some
35557 kind of syntax error. */
35558 return true;
35560 /* Consume the token. */
35561 cp_lexer_consume_token (parser->lexer);
35562 /* See if it starts a new group. */
35563 if (token->type == CPP_OPEN_BRACE)
35565 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
35566 /* In theory this should probably check end == '}', but
35567 cp_parser_save_member_function_body needs it to exit
35568 after either '}' or ')' when called with ')'. */
35569 if (depth == 0)
35570 return false;
35572 else if (token->type == CPP_OPEN_PAREN)
35574 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
35575 if (depth == 0 && end == CPP_CLOSE_PAREN)
35576 return false;
35578 else if (token->type == CPP_PRAGMA)
35579 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
35580 else if (token->type == end)
35581 return false;
35585 /* Like above, for caching a default argument or NSDMI. Both of these are
35586 terminated by a non-nested comma, but it can be unclear whether or not a
35587 comma is nested in a template argument list unless we do more parsing.
35588 In order to handle this ambiguity, when we encounter a ',' after a '<'
35589 we try to parse what follows as a parameter-declaration-list (in the
35590 case of a default argument) or a member-declarator (in the case of an
35591 NSDMI). If that succeeds, then we stop caching. */
35593 static tree
35594 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
35596 unsigned depth = 0;
35597 int maybe_template_id = 0;
35598 cp_token *first_token;
35599 cp_token *token;
35600 tree default_argument;
35602 /* Add tokens until we have processed the entire default
35603 argument. We add the range [first_token, token). */
35604 first_token = cp_lexer_peek_token (parser->lexer);
35605 if (first_token->type == CPP_OPEN_BRACE)
35607 /* For list-initialization, this is straightforward. */
35608 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
35609 token = cp_lexer_peek_token (parser->lexer);
35611 else while (true)
35613 bool done = false;
35615 /* Peek at the next token. */
35616 token = cp_lexer_peek_token (parser->lexer);
35617 /* What we do depends on what token we have. */
35618 switch (token->type)
35620 /* In valid code, a default argument must be
35621 immediately followed by a `,' `)', or `...'. */
35622 case CPP_COMMA:
35623 if (depth == 0 && maybe_template_id)
35625 /* If we've seen a '<', we might be in a
35626 template-argument-list. Until Core issue 325 is
35627 resolved, we don't know how this situation ought
35628 to be handled, so try to DTRT. We check whether
35629 what comes after the comma is a valid parameter
35630 declaration list. If it is, then the comma ends
35631 the default argument; otherwise the default
35632 argument continues. */
35633 bool error = false;
35634 cp_token *peek;
35636 /* Set ITALP so cp_parser_parameter_declaration_list
35637 doesn't decide to commit to this parse. */
35638 bool saved_italp = parser->in_template_argument_list_p;
35639 parser->in_template_argument_list_p = true;
35641 cp_parser_parse_tentatively (parser);
35643 if (nsdmi)
35645 /* Parse declarators until we reach a non-comma or
35646 somthing that cannot be an initializer.
35647 Just checking whether we're looking at a single
35648 declarator is insufficient. Consider:
35649 int var = tuple<T,U>::x;
35650 The template parameter 'U' looks exactly like a
35651 declarator. */
35654 int ctor_dtor_or_conv_p;
35655 cp_lexer_consume_token (parser->lexer);
35656 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
35657 CP_PARSER_FLAGS_NONE,
35658 &ctor_dtor_or_conv_p,
35659 /*parenthesized_p=*/NULL,
35660 /*member_p=*/true,
35661 /*friend_p=*/false,
35662 /*static_p=*/false);
35663 peek = cp_lexer_peek_token (parser->lexer);
35664 if (cp_parser_error_occurred (parser))
35665 break;
35667 while (peek->type == CPP_COMMA);
35668 /* If we met an '=' or ';' then the original comma
35669 was the end of the NSDMI. Otherwise assume
35670 we're still in the NSDMI. */
35671 error = (peek->type != CPP_EQ
35672 && peek->type != CPP_SEMICOLON);
35674 else
35676 cp_lexer_consume_token (parser->lexer);
35677 begin_scope (sk_function_parms, NULL_TREE);
35678 tree t = cp_parser_parameter_declaration_list
35679 (parser, CP_PARSER_FLAGS_NONE,
35680 /*pending_decls*/nullptr);
35681 if (t == error_mark_node)
35682 error = true;
35683 pop_bindings_and_leave_scope ();
35685 if (!cp_parser_error_occurred (parser) && !error)
35686 done = true;
35687 cp_parser_abort_tentative_parse (parser);
35689 parser->in_template_argument_list_p = saved_italp;
35690 break;
35692 /* FALLTHRU */
35693 case CPP_CLOSE_PAREN:
35694 case CPP_ELLIPSIS:
35695 /* If we run into a non-nested `;', `}', or `]',
35696 then the code is invalid -- but the default
35697 argument is certainly over. */
35698 case CPP_SEMICOLON:
35699 case CPP_CLOSE_BRACE:
35700 case CPP_CLOSE_SQUARE:
35701 if (depth == 0
35702 /* Handle correctly int n = sizeof ... ( p ); */
35703 && token->type != CPP_ELLIPSIS)
35704 done = true;
35705 /* Update DEPTH, if necessary. */
35706 else if (token->type == CPP_CLOSE_PAREN
35707 || token->type == CPP_CLOSE_BRACE
35708 || token->type == CPP_CLOSE_SQUARE)
35709 --depth;
35710 break;
35712 case CPP_OPEN_PAREN:
35713 case CPP_OPEN_SQUARE:
35714 case CPP_OPEN_BRACE:
35715 ++depth;
35716 break;
35718 case CPP_LESS:
35719 if (depth == 0)
35720 /* This might be the comparison operator, or it might
35721 start a template argument list. */
35722 ++maybe_template_id;
35723 break;
35725 case CPP_RSHIFT:
35726 if (cxx_dialect == cxx98)
35727 break;
35728 /* Fall through for C++0x, which treats the `>>'
35729 operator like two `>' tokens in certain
35730 cases. */
35731 gcc_fallthrough ();
35733 case CPP_GREATER:
35734 if (depth == 0)
35736 /* This might be an operator, or it might close a
35737 template argument list. But if a previous '<'
35738 started a template argument list, this will have
35739 closed it, so we can't be in one anymore. */
35740 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
35741 if (maybe_template_id < 0)
35742 maybe_template_id = 0;
35744 break;
35746 /* If we run out of tokens, issue an error message. */
35747 case CPP_EOF:
35748 case CPP_PRAGMA_EOL:
35749 error_at (token->location, "file ends in default argument");
35750 return error_mark_node;
35752 case CPP_NAME:
35753 case CPP_SCOPE:
35754 /* In these cases, we should look for template-ids.
35755 For example, if the default argument is
35756 `X<int, double>()', we need to do name lookup to
35757 figure out whether or not `X' is a template; if
35758 so, the `,' does not end the default argument.
35760 That is not yet done. */
35761 break;
35763 default:
35764 break;
35767 /* If we've reached the end, stop. */
35768 if (done)
35769 break;
35771 /* Add the token to the token block. */
35772 token = cp_lexer_consume_token (parser->lexer);
35775 /* Create a DEFERRED_PARSE to represent the unparsed default
35776 argument. */
35777 default_argument = make_node (DEFERRED_PARSE);
35778 DEFPARSE_TOKENS (default_argument)
35779 = cp_token_cache_new (first_token, token);
35780 DEFPARSE_INSTANTIATIONS (default_argument) = NULL;
35782 return default_argument;
35785 /* A location to use for diagnostics about an unparsed DEFERRED_PARSE. */
35787 location_t
35788 defparse_location (tree default_argument)
35790 cp_token_cache *tokens = DEFPARSE_TOKENS (default_argument);
35791 location_t start = tokens->first->location;
35792 location_t end = tokens->last->location;
35793 return make_location (start, start, end);
35796 /* Begin parsing tentatively. We always save tokens while parsing
35797 tentatively so that if the tentative parsing fails we can restore the
35798 tokens. */
35800 static void
35801 cp_parser_parse_tentatively (cp_parser* parser)
35803 /* Enter a new parsing context. */
35804 parser->context = cp_parser_context_new (parser->context);
35805 /* Begin saving tokens. */
35806 cp_lexer_save_tokens (parser->lexer);
35807 /* In order to avoid repetitive access control error messages,
35808 access checks are queued up until we are no longer parsing
35809 tentatively. */
35810 push_deferring_access_checks (dk_deferred);
35813 /* Commit to the currently active tentative parse. */
35815 static void
35816 cp_parser_commit_to_tentative_parse (cp_parser* parser)
35818 cp_parser_context *context;
35819 cp_lexer *lexer;
35821 /* Mark all of the levels as committed. */
35822 lexer = parser->lexer;
35823 for (context = parser->context; context->next; context = context->next)
35825 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
35826 break;
35827 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
35828 while (!cp_lexer_saving_tokens (lexer))
35829 lexer = lexer->next;
35830 cp_lexer_commit_tokens (lexer);
35834 /* Commit to the topmost currently active tentative parse.
35836 Note that this function shouldn't be called when there are
35837 irreversible side-effects while in a tentative state. For
35838 example, we shouldn't create a permanent entry in the symbol
35839 table, or issue an error message that might not apply if the
35840 tentative parse is aborted. */
35842 static void
35843 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
35845 cp_parser_context *context = parser->context;
35846 cp_lexer *lexer = parser->lexer;
35848 if (context)
35850 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
35851 return;
35852 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
35854 while (!cp_lexer_saving_tokens (lexer))
35855 lexer = lexer->next;
35856 cp_lexer_commit_tokens (lexer);
35860 /* Abort the currently active tentative parse. All consumed tokens
35861 will be rolled back, and no diagnostics will be issued. */
35863 static void
35864 cp_parser_abort_tentative_parse (cp_parser* parser)
35866 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
35867 || errorcount > 0);
35868 cp_parser_simulate_error (parser);
35869 /* Now, pretend that we want to see if the construct was
35870 successfully parsed. */
35871 cp_parser_parse_definitely (parser);
35874 /* Stop parsing tentatively. If a parse error has occurred, restore the
35875 token stream. Otherwise, commit to the tokens we have consumed.
35876 Returns true if no error occurred; false otherwise. */
35878 static bool
35879 cp_parser_parse_definitely (cp_parser* parser)
35881 bool error_occurred;
35882 cp_parser_context *context;
35884 /* Remember whether or not an error occurred, since we are about to
35885 destroy that information. */
35886 error_occurred = cp_parser_error_occurred (parser);
35887 /* Remove the topmost context from the stack. */
35888 context = parser->context;
35889 parser->context = context->next;
35890 /* If no parse errors occurred, commit to the tentative parse. */
35891 if (!error_occurred)
35893 /* Commit to the tokens read tentatively, unless that was
35894 already done. */
35895 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
35896 cp_lexer_commit_tokens (parser->lexer);
35898 pop_to_parent_deferring_access_checks ();
35900 /* Otherwise, if errors occurred, roll back our state so that things
35901 are just as they were before we began the tentative parse. */
35902 else
35904 cp_lexer_rollback_tokens (parser->lexer);
35905 pop_deferring_access_checks ();
35907 /* Add the context to the front of the free list. */
35908 context->next = cp_parser_context_free_list;
35909 cp_parser_context_free_list = context;
35911 return !error_occurred;
35914 /* Returns true if we are parsing tentatively and are not committed to
35915 this tentative parse. */
35917 static bool
35918 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
35920 return (cp_parser_parsing_tentatively (parser)
35921 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
35924 /* Returns nonzero iff an error has occurred during the most recent
35925 tentative parse. */
35927 static bool
35928 cp_parser_error_occurred (cp_parser* parser)
35930 return (cp_parser_parsing_tentatively (parser)
35931 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
35934 /* Returns nonzero if GNU extensions are allowed. */
35936 static bool
35937 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
35939 return parser->allow_gnu_extensions_p;
35942 /* Objective-C++ Productions */
35945 /* Parse an Objective-C expression, which feeds into a primary-expression
35946 above.
35948 objc-expression:
35949 objc-message-expression
35950 objc-string-literal
35951 objc-encode-expression
35952 objc-protocol-expression
35953 objc-selector-expression
35955 Returns a tree representation of the expression. */
35957 static cp_expr
35958 cp_parser_objc_expression (cp_parser* parser)
35960 /* Try to figure out what kind of declaration is present. */
35961 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
35963 switch (kwd->type)
35965 case CPP_OPEN_SQUARE:
35966 return cp_parser_objc_message_expression (parser);
35968 case CPP_OBJC_STRING:
35969 kwd = cp_lexer_consume_token (parser->lexer);
35970 return objc_build_string_object (kwd->u.value);
35972 case CPP_KEYWORD:
35973 switch (kwd->keyword)
35975 case RID_AT_ENCODE:
35976 return cp_parser_objc_encode_expression (parser);
35978 case RID_AT_PROTOCOL:
35979 return cp_parser_objc_protocol_expression (parser);
35981 case RID_AT_SELECTOR:
35982 return cp_parser_objc_selector_expression (parser);
35984 default:
35985 break;
35987 /* FALLTHRU */
35988 default:
35989 error_at (kwd->location,
35990 "misplaced %<@%D%> Objective-C++ construct",
35991 kwd->u.value);
35992 cp_parser_skip_to_end_of_block_or_statement (parser);
35995 return error_mark_node;
35998 /* Parse an Objective-C message expression.
36000 objc-message-expression:
36001 [ objc-message-receiver objc-message-args ]
36003 Returns a representation of an Objective-C message. */
36005 static tree
36006 cp_parser_objc_message_expression (cp_parser* parser)
36008 tree receiver, messageargs;
36010 parser->objective_c_message_context_p = true;
36011 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
36012 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
36013 receiver = cp_parser_objc_message_receiver (parser);
36014 messageargs = cp_parser_objc_message_args (parser);
36015 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
36016 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
36018 tree result = objc_build_message_expr (receiver, messageargs);
36020 /* Construct a location e.g.
36021 [self func1:5]
36022 ^~~~~~~~~~~~~~
36023 ranging from the '[' to the ']', with the caret at the start. */
36024 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
36025 protected_set_expr_location (result, combined_loc);
36027 parser->objective_c_message_context_p = false;
36028 return result;
36031 /* Parse an objc-message-receiver.
36033 objc-message-receiver:
36034 expression
36035 simple-type-specifier
36037 Returns a representation of the type or expression. */
36039 static tree
36040 cp_parser_objc_message_receiver (cp_parser* parser)
36042 tree rcv;
36044 /* An Objective-C message receiver may be either (1) a type
36045 or (2) an expression. */
36046 cp_parser_parse_tentatively (parser);
36047 rcv = cp_parser_expression (parser);
36049 /* If that worked out, fine. */
36050 if (cp_parser_parse_definitely (parser))
36051 return rcv;
36053 cp_parser_parse_tentatively (parser);
36054 rcv = cp_parser_simple_type_specifier (parser,
36055 /*decl_specs=*/NULL,
36056 CP_PARSER_FLAGS_NONE);
36058 if (cp_parser_parse_definitely (parser))
36059 return objc_get_class_reference (rcv);
36061 cp_parser_error (parser, "objective-c++ message receiver expected");
36062 return error_mark_node;
36065 /* Parse the arguments and selectors comprising an Objective-C message.
36067 objc-message-args:
36068 objc-selector
36069 objc-selector-args
36070 objc-selector-args , objc-comma-args
36072 objc-selector-args:
36073 objc-selector [opt] : assignment-expression
36074 objc-selector-args objc-selector [opt] : assignment-expression
36076 objc-comma-args:
36077 assignment-expression
36078 objc-comma-args , assignment-expression
36080 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
36081 selector arguments and TREE_VALUE containing a list of comma
36082 arguments. */
36084 static tree
36085 cp_parser_objc_message_args (cp_parser* parser)
36087 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
36088 bool maybe_unary_selector_p = true;
36089 cp_token *token = cp_lexer_peek_token (parser->lexer);
36091 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
36093 tree selector = NULL_TREE, arg;
36095 if (token->type != CPP_COLON)
36096 selector = cp_parser_objc_selector (parser);
36098 /* Detect if we have a unary selector. */
36099 if (maybe_unary_selector_p
36100 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
36101 return build_tree_list (selector, NULL_TREE);
36103 maybe_unary_selector_p = false;
36104 cp_parser_require (parser, CPP_COLON, RT_COLON);
36105 arg = cp_parser_assignment_expression (parser);
36107 sel_args
36108 = chainon (sel_args,
36109 build_tree_list (selector, arg));
36111 token = cp_lexer_peek_token (parser->lexer);
36114 /* Handle non-selector arguments, if any. */
36115 while (token->type == CPP_COMMA)
36117 tree arg;
36119 cp_lexer_consume_token (parser->lexer);
36120 arg = cp_parser_assignment_expression (parser);
36122 addl_args
36123 = chainon (addl_args,
36124 build_tree_list (NULL_TREE, arg));
36126 token = cp_lexer_peek_token (parser->lexer);
36129 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
36131 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
36132 return build_tree_list (error_mark_node, error_mark_node);
36135 return build_tree_list (sel_args, addl_args);
36138 /* Parse an Objective-C encode expression.
36140 objc-encode-expression:
36141 @encode objc-typename
36143 Returns an encoded representation of the type argument. */
36145 static cp_expr
36146 cp_parser_objc_encode_expression (cp_parser* parser)
36148 tree type;
36149 cp_token *token;
36150 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
36152 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
36153 matching_parens parens;
36154 parens.require_open (parser);
36155 token = cp_lexer_peek_token (parser->lexer);
36156 type = complete_type (cp_parser_type_id (parser));
36157 parens.require_close (parser);
36159 if (!type)
36161 error_at (token->location,
36162 "%<@encode%> must specify a type as an argument");
36163 return error_mark_node;
36166 /* This happens if we find @encode(T) (where T is a template
36167 typename or something dependent on a template typename) when
36168 parsing a template. In that case, we can't compile it
36169 immediately, but we rather create an AT_ENCODE_EXPR which will
36170 need to be instantiated when the template is used.
36172 if (dependent_type_p (type))
36174 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
36175 TREE_READONLY (value) = 1;
36176 return value;
36180 /* Build a location of the form:
36181 @encode(int)
36182 ^~~~~~~~~~~~
36183 with caret==start at the @ token, finishing at the close paren. */
36184 location_t combined_loc = make_location (start_loc, start_loc, parser->lexer);
36186 return cp_expr (objc_build_encode_expr (type), combined_loc);
36189 /* Parse an Objective-C @defs expression. */
36191 static tree
36192 cp_parser_objc_defs_expression (cp_parser *parser)
36194 tree name;
36196 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
36197 matching_parens parens;
36198 parens.require_open (parser);
36199 name = cp_parser_identifier (parser);
36200 parens.require_close (parser);
36202 return objc_get_class_ivars (name);
36205 /* Parse an Objective-C protocol expression.
36207 objc-protocol-expression:
36208 @protocol ( identifier )
36210 Returns a representation of the protocol expression. */
36212 static tree
36213 cp_parser_objc_protocol_expression (cp_parser* parser)
36215 tree proto;
36216 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
36218 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
36219 matching_parens parens;
36220 parens.require_open (parser);
36221 proto = cp_parser_identifier (parser);
36222 parens.require_close (parser);
36224 /* Build a location of the form:
36225 @protocol(prot)
36226 ^~~~~~~~~~~~~~~
36227 with caret==start at the @ token, finishing at the close paren. */
36228 location_t combined_loc = make_location (start_loc, start_loc, parser->lexer);
36229 tree result = objc_build_protocol_expr (proto);
36230 protected_set_expr_location (result, combined_loc);
36231 return result;
36234 /* Parse an Objective-C selector expression.
36236 objc-selector-expression:
36237 @selector ( objc-method-signature )
36239 objc-method-signature:
36240 objc-selector
36241 objc-selector-seq
36243 objc-selector-seq:
36244 objc-selector :
36245 objc-selector-seq objc-selector :
36247 Returns a representation of the method selector. */
36249 static tree
36250 cp_parser_objc_selector_expression (cp_parser* parser)
36252 tree sel_seq = NULL_TREE;
36253 bool maybe_unary_selector_p = true;
36254 cp_token *token;
36255 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36257 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
36258 matching_parens parens;
36259 parens.require_open (parser);
36260 token = cp_lexer_peek_token (parser->lexer);
36262 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
36263 || token->type == CPP_SCOPE)
36265 tree selector = NULL_TREE;
36267 if (token->type != CPP_COLON
36268 || token->type == CPP_SCOPE)
36269 selector = cp_parser_objc_selector (parser);
36271 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
36272 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
36274 /* Detect if we have a unary selector. */
36275 if (maybe_unary_selector_p)
36277 sel_seq = selector;
36278 goto finish_selector;
36280 else
36282 cp_parser_error (parser, "expected %<:%>");
36285 maybe_unary_selector_p = false;
36286 token = cp_lexer_consume_token (parser->lexer);
36288 if (token->type == CPP_SCOPE)
36290 sel_seq
36291 = chainon (sel_seq,
36292 build_tree_list (selector, NULL_TREE));
36293 sel_seq
36294 = chainon (sel_seq,
36295 build_tree_list (NULL_TREE, NULL_TREE));
36297 else
36298 sel_seq
36299 = chainon (sel_seq,
36300 build_tree_list (selector, NULL_TREE));
36302 token = cp_lexer_peek_token (parser->lexer);
36305 finish_selector:
36306 parens.require_close (parser);
36309 /* Build a location of the form:
36310 @selector(func)
36311 ^~~~~~~~~~~~~~~
36312 with caret==start at the @ token, finishing at the close paren. */
36313 location_t combined_loc = make_location (loc, loc, parser->lexer);
36314 tree result = objc_build_selector_expr (combined_loc, sel_seq);
36315 /* TODO: objc_build_selector_expr doesn't always honor the location. */
36316 protected_set_expr_location (result, combined_loc);
36317 return result;
36320 /* Parse a list of identifiers.
36322 objc-identifier-list:
36323 identifier
36324 objc-identifier-list , identifier
36326 Returns a TREE_LIST of identifier nodes. */
36328 static tree
36329 cp_parser_objc_identifier_list (cp_parser* parser)
36331 tree identifier;
36332 tree list;
36333 cp_token *sep;
36335 identifier = cp_parser_identifier (parser);
36336 if (identifier == error_mark_node)
36337 return error_mark_node;
36339 list = build_tree_list (NULL_TREE, identifier);
36340 sep = cp_lexer_peek_token (parser->lexer);
36342 while (sep->type == CPP_COMMA)
36344 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
36345 identifier = cp_parser_identifier (parser);
36346 if (identifier == error_mark_node)
36347 return list;
36349 list = chainon (list, build_tree_list (NULL_TREE,
36350 identifier));
36351 sep = cp_lexer_peek_token (parser->lexer);
36354 return list;
36357 /* Parse an Objective-C alias declaration.
36359 objc-alias-declaration:
36360 @compatibility_alias identifier identifier ;
36362 This function registers the alias mapping with the Objective-C front end.
36363 It returns nothing. */
36365 static void
36366 cp_parser_objc_alias_declaration (cp_parser* parser)
36368 tree alias, orig;
36370 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
36371 alias = cp_parser_identifier (parser);
36372 orig = cp_parser_identifier (parser);
36373 objc_declare_alias (alias, orig);
36374 cp_parser_consume_semicolon_at_end_of_statement (parser);
36377 /* Parse an Objective-C class forward-declaration.
36379 objc-class-declaration:
36380 @class objc-identifier-list ;
36382 The function registers the forward declarations with the Objective-C
36383 front end. It returns nothing. */
36385 static void
36386 cp_parser_objc_class_declaration (cp_parser* parser)
36388 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
36389 while (true)
36391 tree id;
36393 id = cp_parser_identifier (parser);
36394 if (id == error_mark_node)
36395 break;
36397 objc_declare_class (id);
36399 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36400 cp_lexer_consume_token (parser->lexer);
36401 else
36402 break;
36404 cp_parser_consume_semicolon_at_end_of_statement (parser);
36407 /* Parse a list of Objective-C protocol references.
36409 objc-protocol-refs-opt:
36410 objc-protocol-refs [opt]
36412 objc-protocol-refs:
36413 < objc-identifier-list >
36415 Returns a TREE_LIST of identifiers, if any. */
36417 static tree
36418 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
36420 tree protorefs = NULL_TREE;
36422 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
36424 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
36425 protorefs = cp_parser_objc_identifier_list (parser);
36426 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
36429 return protorefs;
36432 /* Parse a Objective-C visibility specification. */
36434 static void
36435 cp_parser_objc_visibility_spec (cp_parser* parser)
36437 cp_token *vis = cp_lexer_peek_token (parser->lexer);
36439 switch (vis->keyword)
36441 case RID_AT_PRIVATE:
36442 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
36443 break;
36444 case RID_AT_PROTECTED:
36445 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
36446 break;
36447 case RID_AT_PUBLIC:
36448 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
36449 break;
36450 case RID_AT_PACKAGE:
36451 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
36452 break;
36453 default:
36454 return;
36457 /* Eat '@private'/'@protected'/'@public'. */
36458 cp_lexer_consume_token (parser->lexer);
36461 /* Parse an Objective-C method type. Return 'true' if it is a class
36462 (+) method, and 'false' if it is an instance (-) method. */
36464 static inline bool
36465 cp_parser_objc_method_type (cp_parser* parser)
36467 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
36468 return true;
36469 else
36470 return false;
36473 /* Parse an Objective-C protocol qualifier. */
36475 static tree
36476 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
36478 tree quals = NULL_TREE, node;
36479 cp_token *token = cp_lexer_peek_token (parser->lexer);
36481 node = token->u.value;
36483 while (node && identifier_p (node)
36484 && (node == ridpointers [(int) RID_IN]
36485 || node == ridpointers [(int) RID_OUT]
36486 || node == ridpointers [(int) RID_INOUT]
36487 || node == ridpointers [(int) RID_BYCOPY]
36488 || node == ridpointers [(int) RID_BYREF]
36489 || node == ridpointers [(int) RID_ONEWAY]))
36491 quals = tree_cons (NULL_TREE, node, quals);
36492 cp_lexer_consume_token (parser->lexer);
36493 token = cp_lexer_peek_token (parser->lexer);
36494 node = token->u.value;
36497 return quals;
36500 /* Parse an Objective-C typename. */
36502 static tree
36503 cp_parser_objc_typename (cp_parser* parser)
36505 tree type_name = NULL_TREE;
36507 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36509 tree proto_quals, cp_type = NULL_TREE;
36511 matching_parens parens;
36512 parens.consume_open (parser); /* Eat '('. */
36513 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
36515 /* An ObjC type name may consist of just protocol qualifiers, in which
36516 case the type shall default to 'id'. */
36517 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
36519 cp_type = cp_parser_type_id (parser);
36521 /* If the type could not be parsed, an error has already
36522 been produced. For error recovery, behave as if it had
36523 not been specified, which will use the default type
36524 'id'. */
36525 if (cp_type == error_mark_node)
36527 cp_type = NULL_TREE;
36528 /* We need to skip to the closing parenthesis as
36529 cp_parser_type_id() does not seem to do it for
36530 us. */
36531 cp_parser_skip_to_closing_parenthesis (parser,
36532 /*recovering=*/true,
36533 /*or_comma=*/false,
36534 /*consume_paren=*/false);
36538 parens.require_close (parser);
36539 type_name = build_tree_list (proto_quals, cp_type);
36542 return type_name;
36545 /* Check to see if TYPE refers to an Objective-C selector name. */
36547 static bool
36548 cp_parser_objc_selector_p (enum cpp_ttype type)
36550 return (type == CPP_NAME || type == CPP_KEYWORD
36551 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
36552 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
36553 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
36554 || type == CPP_XOR || type == CPP_XOR_EQ);
36557 /* Parse an Objective-C selector. */
36559 static tree
36560 cp_parser_objc_selector (cp_parser* parser)
36562 cp_token *token = cp_lexer_consume_token (parser->lexer);
36564 if (!cp_parser_objc_selector_p (token->type))
36566 error_at (token->location, "invalid Objective-C++ selector name");
36567 return error_mark_node;
36570 /* C++ operator names are allowed to appear in ObjC selectors. */
36571 switch (token->type)
36573 case CPP_AND_AND: return get_identifier ("and");
36574 case CPP_AND_EQ: return get_identifier ("and_eq");
36575 case CPP_AND: return get_identifier ("bitand");
36576 case CPP_OR: return get_identifier ("bitor");
36577 case CPP_COMPL: return get_identifier ("compl");
36578 case CPP_NOT: return get_identifier ("not");
36579 case CPP_NOT_EQ: return get_identifier ("not_eq");
36580 case CPP_OR_OR: return get_identifier ("or");
36581 case CPP_OR_EQ: return get_identifier ("or_eq");
36582 case CPP_XOR: return get_identifier ("xor");
36583 case CPP_XOR_EQ: return get_identifier ("xor_eq");
36584 default: return token->u.value;
36588 /* Parse an Objective-C params list. */
36590 static tree
36591 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
36593 tree params = NULL_TREE;
36594 bool maybe_unary_selector_p = true;
36595 cp_token *token = cp_lexer_peek_token (parser->lexer);
36597 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
36599 tree selector = NULL_TREE, type_name, identifier;
36600 tree parm_attr = NULL_TREE;
36602 if (token->keyword == RID_ATTRIBUTE)
36603 break;
36605 if (token->type != CPP_COLON)
36606 selector = cp_parser_objc_selector (parser);
36608 /* Detect if we have a unary selector. */
36609 if (maybe_unary_selector_p
36610 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
36612 params = selector; /* Might be followed by attributes. */
36613 break;
36616 maybe_unary_selector_p = false;
36617 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
36619 /* Something went quite wrong. There should be a colon
36620 here, but there is not. Stop parsing parameters. */
36621 break;
36623 type_name = cp_parser_objc_typename (parser);
36624 /* New ObjC allows attributes on parameters too. */
36625 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
36626 parm_attr = cp_parser_attributes_opt (parser);
36627 identifier = cp_parser_identifier (parser);
36629 params
36630 = chainon (params,
36631 objc_build_keyword_decl (selector,
36632 type_name,
36633 identifier,
36634 parm_attr));
36636 token = cp_lexer_peek_token (parser->lexer);
36639 if (params == NULL_TREE)
36641 cp_parser_error (parser, "objective-c++ method declaration is expected");
36642 return error_mark_node;
36645 /* We allow tail attributes for the method. */
36646 if (token->keyword == RID_ATTRIBUTE)
36648 *attributes = cp_parser_attributes_opt (parser);
36649 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
36650 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
36651 return params;
36652 cp_parser_error (parser,
36653 "method attributes must be specified at the end");
36654 return error_mark_node;
36657 if (params == NULL_TREE)
36659 cp_parser_error (parser, "objective-c++ method declaration is expected");
36660 return error_mark_node;
36662 return params;
36665 /* Parse the non-keyword Objective-C params. */
36667 static tree
36668 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
36669 tree* attributes)
36671 tree params = make_node (TREE_LIST);
36672 cp_token *token = cp_lexer_peek_token (parser->lexer);
36673 *ellipsisp = false; /* Initially, assume no ellipsis. */
36675 while (token->type == CPP_COMMA)
36677 cp_parameter_declarator *parmdecl;
36678 tree parm;
36680 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
36681 token = cp_lexer_peek_token (parser->lexer);
36683 if (token->type == CPP_ELLIPSIS)
36685 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
36686 *ellipsisp = true;
36687 token = cp_lexer_peek_token (parser->lexer);
36688 break;
36691 /* TODO: parse attributes for tail parameters. */
36692 parmdecl = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
36693 false, NULL);
36694 parm = grokdeclarator (parmdecl->declarator,
36695 &parmdecl->decl_specifiers,
36696 PARM, /*initialized=*/0,
36697 /*attrlist=*/NULL);
36699 chainon (params, build_tree_list (NULL_TREE, parm));
36700 token = cp_lexer_peek_token (parser->lexer);
36703 /* We allow tail attributes for the method. */
36704 if (token->keyword == RID_ATTRIBUTE)
36706 if (*attributes == NULL_TREE)
36708 *attributes = cp_parser_attributes_opt (parser);
36709 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
36710 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
36711 return params;
36713 else
36714 /* We have an error, but parse the attributes, so that we can
36715 carry on. */
36716 *attributes = cp_parser_attributes_opt (parser);
36718 cp_parser_error (parser,
36719 "method attributes must be specified at the end");
36720 return error_mark_node;
36723 return params;
36726 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
36728 static void
36729 cp_parser_objc_interstitial_code (cp_parser* parser)
36731 cp_token *token = cp_lexer_peek_token (parser->lexer);
36733 /* If the next token is `extern' and the following token is a string
36734 literal, then we have a linkage specification. */
36735 if (token->keyword == RID_EXTERN
36736 && cp_parser_is_pure_string_literal
36737 (cp_lexer_peek_nth_token (parser->lexer, 2)))
36738 cp_parser_linkage_specification (parser, NULL_TREE);
36739 /* Handle #pragma, if any. */
36740 else if (token->type == CPP_PRAGMA)
36741 cp_parser_pragma (parser, pragma_objc_icode, NULL);
36742 /* Allow stray semicolons. */
36743 else if (token->type == CPP_SEMICOLON)
36744 cp_lexer_consume_token (parser->lexer);
36745 /* Mark methods as optional or required, when building protocols. */
36746 else if (token->keyword == RID_AT_OPTIONAL)
36748 cp_lexer_consume_token (parser->lexer);
36749 objc_set_method_opt (true);
36751 else if (token->keyword == RID_AT_REQUIRED)
36753 cp_lexer_consume_token (parser->lexer);
36754 objc_set_method_opt (false);
36756 else if (token->keyword == RID_NAMESPACE)
36757 cp_parser_namespace_definition (parser);
36758 /* Other stray characters must generate errors. */
36759 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
36761 cp_lexer_consume_token (parser->lexer);
36762 error ("stray %qs between Objective-C++ methods",
36763 token->type == CPP_OPEN_BRACE ? "{" : "}");
36765 /* Finally, try to parse a block-declaration, or a function-definition. */
36766 else
36767 cp_parser_block_declaration (parser, /*statement_p=*/false);
36770 /* Parse a method signature. */
36772 static tree
36773 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
36775 tree rettype, kwdparms, optparms;
36776 bool ellipsis = false;
36777 bool is_class_method;
36779 is_class_method = cp_parser_objc_method_type (parser);
36780 rettype = cp_parser_objc_typename (parser);
36781 *attributes = NULL_TREE;
36782 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
36783 if (kwdparms == error_mark_node)
36784 return error_mark_node;
36785 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
36786 if (optparms == error_mark_node)
36787 return error_mark_node;
36789 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
36792 static bool
36793 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
36795 tree tattr;
36796 cp_lexer_save_tokens (parser->lexer);
36797 tattr = cp_parser_attributes_opt (parser);
36798 gcc_assert (tattr) ;
36800 /* If the attributes are followed by a method introducer, this is not allowed.
36801 Dump the attributes and flag the situation. */
36802 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
36803 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
36804 return true;
36806 /* Otherwise, the attributes introduce some interstitial code, possibly so
36807 rewind to allow that check. */
36808 cp_lexer_rollback_tokens (parser->lexer);
36809 return false;
36812 /* Parse an Objective-C method prototype list. */
36814 static void
36815 cp_parser_objc_method_prototype_list (cp_parser* parser)
36817 cp_token *token = cp_lexer_peek_token (parser->lexer);
36819 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
36821 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
36823 tree attributes, sig;
36824 bool is_class_method;
36825 if (token->type == CPP_PLUS)
36826 is_class_method = true;
36827 else
36828 is_class_method = false;
36829 sig = cp_parser_objc_method_signature (parser, &attributes);
36830 if (sig == error_mark_node)
36832 cp_parser_skip_to_end_of_block_or_statement (parser);
36833 token = cp_lexer_peek_token (parser->lexer);
36834 continue;
36836 objc_add_method_declaration (is_class_method, sig, attributes);
36837 cp_parser_consume_semicolon_at_end_of_statement (parser);
36839 else if (token->keyword == RID_AT_PROPERTY)
36840 cp_parser_objc_at_property_declaration (parser);
36841 else if (token->keyword == RID_ATTRIBUTE
36842 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
36843 warning_at (cp_lexer_peek_token (parser->lexer)->location,
36844 OPT_Wattributes,
36845 "prefix attributes are ignored for methods");
36846 else
36847 /* Allow for interspersed non-ObjC++ code. */
36848 cp_parser_objc_interstitial_code (parser);
36850 token = cp_lexer_peek_token (parser->lexer);
36853 if (token->type != CPP_EOF)
36854 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
36855 else
36856 cp_parser_error (parser, "expected %<@end%>");
36858 objc_finish_interface ();
36861 /* Parse an Objective-C method definition list. */
36863 static void
36864 cp_parser_objc_method_definition_list (cp_parser* parser)
36866 for (;;)
36868 cp_token *token = cp_lexer_peek_token (parser->lexer);
36870 if (token->keyword == RID_AT_END)
36872 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
36873 break;
36875 else if (token->type == CPP_EOF)
36877 cp_parser_error (parser, "expected %<@end%>");
36878 break;
36880 else if (token->type == CPP_PLUS || token->type == CPP_MINUS)
36882 bool is_class_method = token->type == CPP_PLUS;
36884 push_deferring_access_checks (dk_deferred);
36885 tree attribute;
36886 tree sig = cp_parser_objc_method_signature (parser, &attribute);
36887 if (sig == error_mark_node)
36888 cp_parser_skip_to_end_of_block_or_statement (parser);
36889 else
36891 objc_start_method_definition (is_class_method, sig,
36892 attribute, NULL_TREE);
36894 /* For historical reasons, we accept an optional semicolon. */
36895 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
36896 cp_lexer_consume_token (parser->lexer);
36898 perform_deferred_access_checks (tf_warning_or_error);
36899 stop_deferring_access_checks ();
36900 tree meth
36901 = cp_parser_function_definition_after_declarator (parser, false);
36902 pop_deferring_access_checks ();
36903 objc_finish_method_definition (meth);
36906 /* The following case will be removed once @synthesize is
36907 completely implemented. */
36908 else if (token->keyword == RID_AT_PROPERTY)
36909 cp_parser_objc_at_property_declaration (parser);
36910 else if (token->keyword == RID_AT_SYNTHESIZE)
36911 cp_parser_objc_at_synthesize_declaration (parser);
36912 else if (token->keyword == RID_AT_DYNAMIC)
36913 cp_parser_objc_at_dynamic_declaration (parser);
36914 else if (token->keyword == RID_ATTRIBUTE
36915 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
36916 warning_at (token->location, OPT_Wattributes,
36917 "prefix attributes are ignored for methods");
36918 else
36919 /* Allow for interspersed non-ObjC++ code. */
36920 cp_parser_objc_interstitial_code (parser);
36923 objc_finish_implementation ();
36926 /* Parse Objective-C ivars. */
36928 static void
36929 cp_parser_objc_class_ivars (cp_parser* parser)
36931 cp_token *token = cp_lexer_peek_token (parser->lexer);
36933 if (token->type != CPP_OPEN_BRACE)
36934 return; /* No ivars specified. */
36936 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
36937 token = cp_lexer_peek_token (parser->lexer);
36939 while (token->type != CPP_CLOSE_BRACE
36940 && token->keyword != RID_AT_END && token->type != CPP_EOF)
36942 cp_decl_specifier_seq declspecs;
36943 int decl_class_or_enum_p;
36944 tree prefix_attributes;
36946 cp_parser_objc_visibility_spec (parser);
36948 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
36949 break;
36951 cp_parser_decl_specifier_seq (parser,
36952 CP_PARSER_FLAGS_OPTIONAL,
36953 &declspecs,
36954 &decl_class_or_enum_p);
36956 /* auto, register, static, extern, mutable. */
36957 if (declspecs.storage_class != sc_none)
36959 cp_parser_error (parser, "invalid type for instance variable");
36960 declspecs.storage_class = sc_none;
36963 /* thread_local. */
36964 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
36966 cp_parser_error (parser, "invalid type for instance variable");
36967 declspecs.locations[ds_thread] = 0;
36970 /* typedef. */
36971 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
36973 cp_parser_error (parser, "invalid type for instance variable");
36974 declspecs.locations[ds_typedef] = 0;
36977 prefix_attributes = declspecs.attributes;
36978 declspecs.attributes = NULL_TREE;
36980 /* Keep going until we hit the `;' at the end of the
36981 declaration. */
36982 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
36984 tree width = NULL_TREE, attributes, first_attribute, decl;
36985 cp_declarator *declarator = NULL;
36986 int ctor_dtor_or_conv_p;
36988 /* Check for a (possibly unnamed) bitfield declaration. */
36989 token = cp_lexer_peek_token (parser->lexer);
36990 if (token->type == CPP_COLON)
36991 goto eat_colon;
36993 if (token->type == CPP_NAME
36994 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
36995 == CPP_COLON))
36997 /* Get the name of the bitfield. */
36998 declarator = make_id_declarator (NULL_TREE,
36999 cp_parser_identifier (parser),
37000 sfk_none, token->location);
37002 eat_colon:
37003 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
37004 /* Get the width of the bitfield. */
37005 width
37006 = cp_parser_constant_expression (parser);
37008 else
37010 /* Parse the declarator. */
37011 declarator
37012 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
37013 CP_PARSER_FLAGS_NONE,
37014 &ctor_dtor_or_conv_p,
37015 /*parenthesized_p=*/NULL,
37016 /*member_p=*/false,
37017 /*friend_p=*/false,
37018 /*static_p=*/false);
37021 /* Look for attributes that apply to the ivar. */
37022 attributes = cp_parser_attributes_opt (parser);
37023 /* Remember which attributes are prefix attributes and
37024 which are not. */
37025 first_attribute = attributes;
37026 /* Combine the attributes. */
37027 attributes = attr_chainon (prefix_attributes, attributes);
37029 if (width)
37030 /* Create the bitfield declaration. */
37031 decl = grokbitfield (declarator, &declspecs,
37032 width, NULL_TREE, attributes);
37033 else
37034 decl = grokfield (declarator, &declspecs,
37035 NULL_TREE, /*init_const_expr_p=*/false,
37036 NULL_TREE, attributes);
37038 /* Add the instance variable. */
37039 if (decl != error_mark_node && decl != NULL_TREE)
37040 objc_add_instance_variable (decl);
37042 /* Reset PREFIX_ATTRIBUTES. */
37043 if (attributes != error_mark_node)
37045 while (attributes && TREE_CHAIN (attributes) != first_attribute)
37046 attributes = TREE_CHAIN (attributes);
37047 if (attributes)
37048 TREE_CHAIN (attributes) = NULL_TREE;
37051 token = cp_lexer_peek_token (parser->lexer);
37053 if (token->type == CPP_COMMA)
37055 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
37056 continue;
37058 break;
37061 cp_parser_consume_semicolon_at_end_of_statement (parser);
37062 token = cp_lexer_peek_token (parser->lexer);
37065 if (token->keyword == RID_AT_END)
37066 cp_parser_error (parser, "expected %<}%>");
37068 /* Do not consume the RID_AT_END, so it will be read again as terminating
37069 the @interface of @implementation. */
37070 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
37071 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
37073 /* For historical reasons, we accept an optional semicolon. */
37074 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
37075 cp_lexer_consume_token (parser->lexer);
37078 /* Parse an Objective-C protocol declaration. */
37080 static void
37081 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
37083 tree proto, protorefs;
37084 cp_token *tok;
37086 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
37087 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
37089 tok = cp_lexer_peek_token (parser->lexer);
37090 error_at (tok->location, "identifier expected after %<@protocol%>");
37091 cp_parser_consume_semicolon_at_end_of_statement (parser);
37092 return;
37095 /* See if we have a forward declaration or a definition. */
37096 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
37098 /* Try a forward declaration first. */
37099 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
37101 while (true)
37103 tree id;
37105 id = cp_parser_identifier (parser);
37106 if (id == error_mark_node)
37107 break;
37109 objc_declare_protocol (id, attributes);
37111 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37112 cp_lexer_consume_token (parser->lexer);
37113 else
37114 break;
37116 cp_parser_consume_semicolon_at_end_of_statement (parser);
37119 /* Ok, we got a full-fledged definition (or at least should). */
37120 else
37122 proto = cp_parser_identifier (parser);
37123 protorefs = cp_parser_objc_protocol_refs_opt (parser);
37124 objc_start_protocol (proto, protorefs, attributes);
37125 cp_parser_objc_method_prototype_list (parser);
37129 /* Parse an Objective-C superclass or category. */
37131 static void
37132 cp_parser_objc_superclass_or_category (cp_parser *parser,
37133 bool iface_p,
37134 tree *super,
37135 tree *categ, bool *is_class_extension)
37137 cp_token *next = cp_lexer_peek_token (parser->lexer);
37139 *super = *categ = NULL_TREE;
37140 *is_class_extension = false;
37141 if (next->type == CPP_COLON)
37143 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
37144 *super = cp_parser_identifier (parser);
37146 else if (next->type == CPP_OPEN_PAREN)
37148 matching_parens parens;
37149 parens.consume_open (parser); /* Eat '('. */
37151 /* If there is no category name, and this is an @interface, we
37152 have a class extension. */
37153 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
37155 *categ = NULL_TREE;
37156 *is_class_extension = true;
37158 else
37159 *categ = cp_parser_identifier (parser);
37161 parens.require_close (parser);
37165 /* Parse an Objective-C class interface. */
37167 static void
37168 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
37170 tree name, super, categ, protos;
37171 bool is_class_extension;
37173 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
37174 location_t nam_loc = cp_lexer_peek_token (parser->lexer)->location;
37175 name = cp_parser_identifier (parser);
37176 if (name == error_mark_node)
37178 /* It's hard to recover because even if valid @interface stuff
37179 is to follow, we can't compile it (or validate it) if we
37180 don't even know which class it refers to. Let's assume this
37181 was a stray '@interface' token in the stream and skip it.
37183 return;
37185 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
37186 &is_class_extension);
37187 protos = cp_parser_objc_protocol_refs_opt (parser);
37189 /* We have either a class or a category on our hands. */
37190 if (categ || is_class_extension)
37191 objc_start_category_interface (name, categ, protos, attributes);
37192 else
37194 objc_start_class_interface (name, nam_loc, super, protos, attributes);
37195 /* Handle instance variable declarations, if any. */
37196 cp_parser_objc_class_ivars (parser);
37197 objc_continue_interface ();
37200 cp_parser_objc_method_prototype_list (parser);
37203 /* Parse an Objective-C class implementation. */
37205 static void
37206 cp_parser_objc_class_implementation (cp_parser* parser)
37208 tree name, super, categ;
37209 bool is_class_extension;
37211 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
37212 name = cp_parser_identifier (parser);
37213 if (name == error_mark_node)
37215 /* It's hard to recover because even if valid @implementation
37216 stuff is to follow, we can't compile it (or validate it) if
37217 we don't even know which class it refers to. Let's assume
37218 this was a stray '@implementation' token in the stream and
37219 skip it.
37221 return;
37223 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
37224 &is_class_extension);
37226 /* We have either a class or a category on our hands. */
37227 if (categ)
37228 objc_start_category_implementation (name, categ);
37229 else
37231 objc_start_class_implementation (name, super);
37232 /* Handle instance variable declarations, if any. */
37233 cp_parser_objc_class_ivars (parser);
37234 objc_continue_implementation ();
37237 cp_parser_objc_method_definition_list (parser);
37240 /* Consume the @end token and finish off the implementation. */
37242 static void
37243 cp_parser_objc_end_implementation (cp_parser* parser)
37245 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
37246 objc_finish_implementation ();
37249 /* Parse an Objective-C declaration. */
37251 static void
37252 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
37254 /* Try to figure out what kind of declaration is present. */
37255 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
37257 if (attributes)
37258 switch (kwd->keyword)
37260 case RID_AT_ALIAS:
37261 case RID_AT_CLASS:
37262 case RID_AT_END:
37263 error_at (kwd->location, "attributes may not be specified before"
37264 " the %<@%D%> Objective-C++ keyword",
37265 kwd->u.value);
37266 attributes = NULL;
37267 break;
37268 case RID_AT_IMPLEMENTATION:
37269 warning_at (kwd->location, OPT_Wattributes,
37270 "prefix attributes are ignored before %<@%D%>",
37271 kwd->u.value);
37272 attributes = NULL;
37273 default:
37274 break;
37277 switch (kwd->keyword)
37279 case RID_AT_ALIAS:
37280 cp_parser_objc_alias_declaration (parser);
37281 break;
37282 case RID_AT_CLASS:
37283 cp_parser_objc_class_declaration (parser);
37284 break;
37285 case RID_AT_PROTOCOL:
37286 cp_parser_objc_protocol_declaration (parser, attributes);
37287 break;
37288 case RID_AT_INTERFACE:
37289 cp_parser_objc_class_interface (parser, attributes);
37290 break;
37291 case RID_AT_IMPLEMENTATION:
37292 cp_parser_objc_class_implementation (parser);
37293 break;
37294 case RID_AT_END:
37295 cp_parser_objc_end_implementation (parser);
37296 break;
37297 default:
37298 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
37299 kwd->u.value);
37300 cp_parser_skip_to_end_of_block_or_statement (parser);
37304 /* Parse an Objective-C try-catch-finally statement.
37306 objc-try-catch-finally-stmt:
37307 @try compound-statement objc-catch-clause-seq [opt]
37308 objc-finally-clause [opt]
37310 objc-catch-clause-seq:
37311 objc-catch-clause objc-catch-clause-seq [opt]
37313 objc-catch-clause:
37314 @catch ( objc-exception-declaration ) compound-statement
37316 objc-finally-clause:
37317 @finally compound-statement
37319 objc-exception-declaration:
37320 parameter-declaration
37321 '...'
37323 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
37325 Returns NULL_TREE.
37327 PS: This function is identical to c_parser_objc_try_catch_finally_statement
37328 for C. Keep them in sync. */
37330 static tree
37331 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
37333 location_t location;
37334 tree stmt;
37336 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
37337 location = cp_lexer_peek_token (parser->lexer)->location;
37338 objc_maybe_warn_exceptions (location);
37339 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
37340 node, lest it get absorbed into the surrounding block. */
37341 stmt = push_stmt_list ();
37342 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
37343 objc_begin_try_stmt (location, pop_stmt_list (stmt));
37345 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
37347 cp_parameter_declarator *parm;
37348 tree parameter_declaration = error_mark_node;
37349 bool seen_open_paren = false;
37350 matching_parens parens;
37352 cp_lexer_consume_token (parser->lexer);
37353 if (parens.require_open (parser))
37354 seen_open_paren = true;
37355 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
37357 /* We have "@catch (...)" (where the '...' are literally
37358 what is in the code). Skip the '...'.
37359 parameter_declaration is set to NULL_TREE, and
37360 objc_being_catch_clauses() knows that that means
37361 '...'. */
37362 cp_lexer_consume_token (parser->lexer);
37363 parameter_declaration = NULL_TREE;
37365 else
37367 /* We have "@catch (NSException *exception)" or something
37368 like that. Parse the parameter declaration. */
37369 parm = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
37370 false, NULL);
37371 if (parm == NULL)
37372 parameter_declaration = error_mark_node;
37373 else
37374 parameter_declaration = grokdeclarator (parm->declarator,
37375 &parm->decl_specifiers,
37376 PARM, /*initialized=*/0,
37377 /*attrlist=*/NULL);
37379 if (seen_open_paren)
37380 parens.require_close (parser);
37381 else
37383 /* If there was no open parenthesis, we are recovering from
37384 an error, and we are trying to figure out what mistake
37385 the user has made. */
37387 /* If there is an immediate closing parenthesis, the user
37388 probably forgot the opening one (ie, they typed "@catch
37389 NSException *e)". Parse the closing parenthesis and keep
37390 going. */
37391 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
37392 cp_lexer_consume_token (parser->lexer);
37394 /* If these is no immediate closing parenthesis, the user
37395 probably doesn't know that parenthesis are required at
37396 all (ie, they typed "@catch NSException *e"). So, just
37397 forget about the closing parenthesis and keep going. */
37399 objc_begin_catch_clause (parameter_declaration);
37400 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
37401 objc_finish_catch_clause ();
37403 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
37405 cp_lexer_consume_token (parser->lexer);
37406 location = cp_lexer_peek_token (parser->lexer)->location;
37407 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
37408 node, lest it get absorbed into the surrounding block. */
37409 stmt = push_stmt_list ();
37410 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
37411 objc_build_finally_clause (location, pop_stmt_list (stmt));
37414 return objc_finish_try_stmt ();
37417 /* Parse an Objective-C synchronized statement.
37419 objc-synchronized-stmt:
37420 @synchronized ( expression ) compound-statement
37422 Returns NULL_TREE. */
37424 static tree
37425 cp_parser_objc_synchronized_statement (cp_parser *parser)
37427 location_t location;
37428 tree lock, stmt;
37430 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
37432 location = cp_lexer_peek_token (parser->lexer)->location;
37433 objc_maybe_warn_exceptions (location);
37434 matching_parens parens;
37435 parens.require_open (parser);
37436 lock = cp_parser_expression (parser);
37437 parens.require_close (parser);
37439 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
37440 node, lest it get absorbed into the surrounding block. */
37441 stmt = push_stmt_list ();
37442 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
37444 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
37447 /* Parse an Objective-C throw statement.
37449 objc-throw-stmt:
37450 @throw assignment-expression [opt] ;
37452 Returns a constructed '@throw' statement. */
37454 static tree
37455 cp_parser_objc_throw_statement (cp_parser *parser)
37457 tree expr = NULL_TREE;
37458 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37460 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
37462 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
37463 expr = cp_parser_expression (parser);
37465 cp_parser_consume_semicolon_at_end_of_statement (parser);
37467 return objc_build_throw_stmt (loc, expr);
37470 /* Parse an Objective-C statement. */
37472 static tree
37473 cp_parser_objc_statement (cp_parser * parser)
37475 /* Try to figure out what kind of declaration is present. */
37476 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
37478 switch (kwd->keyword)
37480 case RID_AT_TRY:
37481 return cp_parser_objc_try_catch_finally_statement (parser);
37482 case RID_AT_SYNCHRONIZED:
37483 return cp_parser_objc_synchronized_statement (parser);
37484 case RID_AT_THROW:
37485 return cp_parser_objc_throw_statement (parser);
37486 default:
37487 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
37488 kwd->u.value);
37489 cp_parser_skip_to_end_of_block_or_statement (parser);
37492 return error_mark_node;
37495 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
37496 look ahead to see if an objc keyword follows the attributes. This
37497 is to detect the use of prefix attributes on ObjC @interface and
37498 @protocol. */
37500 static bool
37501 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
37503 cp_lexer_save_tokens (parser->lexer);
37504 tree addon = cp_parser_attributes_opt (parser);
37505 if (addon
37506 && OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
37508 cp_lexer_commit_tokens (parser->lexer);
37509 if (*attrib)
37510 TREE_CHAIN (*attrib) = addon;
37511 else
37512 *attrib = addon;
37513 return true;
37515 cp_lexer_rollback_tokens (parser->lexer);
37516 return false;
37519 /* This routine is a minimal replacement for
37520 c_parser_struct_declaration () used when parsing the list of
37521 types/names or ObjC++ properties. For example, when parsing the
37522 code
37524 @property (readonly) int a, b, c;
37526 this function is responsible for parsing "int a, int b, int c" and
37527 returning the declarations as CHAIN of DECLs.
37529 TODO: Share this code with cp_parser_objc_class_ivars. It's very
37530 similar parsing. */
37531 static tree
37532 cp_parser_objc_struct_declaration (cp_parser *parser)
37534 tree decls = NULL_TREE;
37535 cp_decl_specifier_seq declspecs;
37536 int decl_class_or_enum_p;
37537 tree prefix_attributes;
37539 cp_parser_decl_specifier_seq (parser,
37540 CP_PARSER_FLAGS_NONE,
37541 &declspecs,
37542 &decl_class_or_enum_p);
37544 if (declspecs.type == error_mark_node)
37545 return error_mark_node;
37547 /* auto, register, static, extern, mutable. */
37548 if (declspecs.storage_class != sc_none)
37550 cp_parser_error (parser, "invalid type for property");
37551 declspecs.storage_class = sc_none;
37554 /* thread_local. */
37555 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
37557 cp_parser_error (parser, "invalid type for property");
37558 declspecs.locations[ds_thread] = 0;
37561 /* typedef. */
37562 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
37564 cp_parser_error (parser, "invalid type for property");
37565 declspecs.locations[ds_typedef] = 0;
37568 prefix_attributes = declspecs.attributes;
37569 declspecs.attributes = NULL_TREE;
37571 /* Keep going until we hit the `;' at the end of the declaration. */
37572 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
37574 tree attributes, first_attribute, decl;
37575 cp_declarator *declarator;
37576 cp_token *token;
37578 /* Parse the declarator. */
37579 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
37580 CP_PARSER_FLAGS_NONE,
37581 NULL, NULL, false, false, false);
37583 /* Look for attributes that apply to the ivar. */
37584 attributes = cp_parser_attributes_opt (parser);
37585 /* Remember which attributes are prefix attributes and
37586 which are not. */
37587 first_attribute = attributes;
37588 /* Combine the attributes. */
37589 attributes = attr_chainon (prefix_attributes, attributes);
37591 decl = grokfield (declarator, &declspecs,
37592 NULL_TREE, /*init_const_expr_p=*/false,
37593 NULL_TREE, attributes);
37595 if (decl == error_mark_node || decl == NULL_TREE)
37596 return error_mark_node;
37598 /* Reset PREFIX_ATTRIBUTES. */
37599 if (attributes != error_mark_node)
37601 while (attributes && TREE_CHAIN (attributes) != first_attribute)
37602 attributes = TREE_CHAIN (attributes);
37603 if (attributes)
37604 TREE_CHAIN (attributes) = NULL_TREE;
37607 DECL_CHAIN (decl) = decls;
37608 decls = decl;
37610 token = cp_lexer_peek_token (parser->lexer);
37611 if (token->type == CPP_COMMA)
37613 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
37614 continue;
37616 else
37617 break;
37619 return decls;
37622 /* Parse an Objective-C @property declaration. The syntax is:
37624 objc-property-declaration:
37625 '@property' objc-property-attributes[opt] struct-declaration ;
37627 objc-property-attributes:
37628 '(' objc-property-attribute-list ')'
37630 objc-property-attribute-list:
37631 objc-property-attribute
37632 objc-property-attribute-list, objc-property-attribute
37634 objc-property-attribute
37635 'getter' = identifier
37636 'setter' = identifier
37637 'readonly'
37638 'readwrite'
37639 'assign'
37640 'retain'
37641 'copy'
37642 'nonatomic'
37644 For example:
37645 @property NSString *name;
37646 @property (readonly) id object;
37647 @property (retain, nonatomic, getter=getTheName) id name;
37648 @property int a, b, c;
37650 PS: This function is identical to
37651 c_parser_objc_at_property_declaration for C. Keep them in sync. */
37652 static void
37653 cp_parser_objc_at_property_declaration (cp_parser *parser)
37655 /* Parse the optional attribute list.
37657 A list of parsed, but not verified, attributes. */
37658 auto_delete_vec<property_attribute_info> prop_attr_list;
37659 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37661 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
37663 /* Parse the optional attribute list... */
37664 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37666 /* Eat the '('. */
37667 matching_parens parens;
37668 location_t attr_start = cp_lexer_peek_token (parser->lexer)->location;
37669 parens.consume_open (parser);
37670 bool syntax_error = false;
37672 /* Allow empty @property attribute lists, but with a warning. */
37673 location_t attr_end = cp_lexer_peek_token (parser->lexer)->location;
37674 location_t attr_comb;
37675 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
37677 attr_comb = make_location (attr_end, attr_start, attr_end);
37678 warning_at (attr_comb, OPT_Wattributes,
37679 "empty property attribute list");
37681 else
37682 while (true)
37684 cp_token *token = cp_lexer_peek_token (parser->lexer);
37685 attr_start = token->location;
37686 attr_end = get_finish (token->location);
37687 attr_comb = make_location (attr_start, attr_start, attr_end);
37689 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_COMMA)
37691 warning_at (attr_comb, OPT_Wattributes,
37692 "missing property attribute");
37693 if (token->type == CPP_CLOSE_PAREN)
37694 break;
37695 cp_lexer_consume_token (parser->lexer);
37696 continue;
37699 tree attr_name = NULL_TREE;
37700 if (identifier_p (token->u.value))
37701 attr_name = token->u.value;
37703 enum rid keyword;
37704 if (token->type == CPP_NAME)
37705 keyword = C_RID_CODE (token->u.value);
37706 else if (token->type == CPP_KEYWORD
37707 && token->keyword == RID_CLASS)
37708 /* Account for accepting the 'class' keyword in this context. */
37709 keyword = RID_CLASS;
37710 else
37711 keyword = RID_MAX; /* By definition, an unknown property. */
37712 cp_lexer_consume_token (parser->lexer);
37714 enum objc_property_attribute_kind prop_kind
37715 = objc_prop_attr_kind_for_rid (keyword);
37716 property_attribute_info *prop
37717 = new property_attribute_info (attr_name, attr_comb, prop_kind);
37718 prop_attr_list.safe_push (prop);
37720 tree meth_name;
37721 switch (prop->prop_kind)
37723 default: break;
37724 case OBJC_PROPERTY_ATTR_UNKNOWN:
37725 if (attr_name)
37726 error_at (attr_start, "unknown property attribute %qE",
37727 attr_name);
37728 else
37729 error_at (attr_start, "unknown property attribute");
37730 prop->parse_error = syntax_error = true;
37731 break;
37733 case OBJC_PROPERTY_ATTR_GETTER:
37734 case OBJC_PROPERTY_ATTR_SETTER:
37735 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
37737 attr_comb = make_location (attr_end, attr_start, attr_end);
37738 error_at (attr_comb, "expected %<=%> after Objective-C %qE",
37739 attr_name);
37740 prop->parse_error = syntax_error = true;
37741 break;
37744 token = cp_lexer_peek_token (parser->lexer);
37745 attr_end = token->location;
37746 cp_lexer_consume_token (parser->lexer); /* eat the = */
37748 if (!cp_parser_objc_selector_p
37749 (cp_lexer_peek_token (parser->lexer)->type))
37751 attr_comb = make_location (attr_end, attr_start, attr_end);
37752 error_at (attr_comb, "expected %qE selector name",
37753 attr_name);
37754 prop->parse_error = syntax_error = true;
37755 break;
37758 /* Get the end of the method name, and consume the name. */
37759 token = cp_lexer_peek_token (parser->lexer);
37760 attr_end = get_finish (token->location);
37761 /* Because method names may contain C++ keywords, we have a
37762 routine to fetch them (this also consumes the token). */
37763 meth_name = cp_parser_objc_selector (parser);
37765 if (prop->prop_kind == OBJC_PROPERTY_ATTR_SETTER)
37767 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
37769 attr_comb = make_location (attr_end, attr_start,
37770 attr_end);
37771 error_at (attr_comb, "setter method names must"
37772 " terminate with %<:%>");
37773 prop->parse_error = syntax_error = true;
37775 else
37777 attr_end = get_finish (cp_lexer_peek_token
37778 (parser->lexer)->location);
37779 cp_lexer_consume_token (parser->lexer);
37781 attr_comb = make_location (attr_start, attr_start,
37782 attr_end);
37784 else
37785 attr_comb = make_location (attr_start, attr_start,
37786 attr_end);
37787 prop->ident = meth_name;
37788 /* Updated location including all that was successfully
37789 parsed. */
37790 prop->prop_loc = attr_comb;
37791 break;
37794 /* If we see a comma here, then keep going - even if we already
37795 saw a syntax error. For simple mistakes e.g. (asign, getter=x)
37796 this makes a more useful output and avoid spurious warnings
37797 about missing attributes that are, in fact, specified after the
37798 one with the syntax error. */
37799 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37800 cp_lexer_consume_token (parser->lexer);
37801 else
37802 break;
37805 if (syntax_error || !parens.require_close (parser))
37806 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37807 /*or_comma=*/false,
37808 /*consume_paren=*/true);
37811 /* 'properties' is the list of properties that we read. Usually a
37812 single one, but maybe more (eg, in "@property int a, b, c;" there
37813 are three).
37814 TODO: Update this parsing so that it accepts (erroneous) bitfields so
37815 that we can issue a meaningful and consistent (between C/C++) error
37816 message from objc_add_property_declaration (). */
37817 tree properties = cp_parser_objc_struct_declaration (parser);
37819 if (properties == error_mark_node)
37820 cp_parser_skip_to_end_of_statement (parser);
37821 else if (properties == NULL_TREE)
37822 cp_parser_error (parser, "expected identifier");
37823 else
37825 /* Comma-separated properties are chained together in reverse order;
37826 add them one by one. */
37827 properties = nreverse (properties);
37828 for (; properties; properties = TREE_CHAIN (properties))
37829 objc_add_property_declaration (loc, copy_node (properties),
37830 prop_attr_list);
37833 cp_parser_consume_semicolon_at_end_of_statement (parser);
37836 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
37838 objc-synthesize-declaration:
37839 @synthesize objc-synthesize-identifier-list ;
37841 objc-synthesize-identifier-list:
37842 objc-synthesize-identifier
37843 objc-synthesize-identifier-list, objc-synthesize-identifier
37845 objc-synthesize-identifier
37846 identifier
37847 identifier = identifier
37849 For example:
37850 @synthesize MyProperty;
37851 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
37853 PS: This function is identical to c_parser_objc_at_synthesize_declaration
37854 for C. Keep them in sync.
37856 static void
37857 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
37859 tree list = NULL_TREE;
37860 location_t loc;
37861 loc = cp_lexer_peek_token (parser->lexer)->location;
37863 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
37864 while (true)
37866 tree property, ivar;
37867 property = cp_parser_identifier (parser);
37868 if (property == error_mark_node)
37870 cp_parser_consume_semicolon_at_end_of_statement (parser);
37871 return;
37873 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
37875 cp_lexer_consume_token (parser->lexer);
37876 ivar = cp_parser_identifier (parser);
37877 if (ivar == error_mark_node)
37879 cp_parser_consume_semicolon_at_end_of_statement (parser);
37880 return;
37883 else
37884 ivar = NULL_TREE;
37885 list = chainon (list, build_tree_list (ivar, property));
37886 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37887 cp_lexer_consume_token (parser->lexer);
37888 else
37889 break;
37891 cp_parser_consume_semicolon_at_end_of_statement (parser);
37892 objc_add_synthesize_declaration (loc, list);
37895 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
37897 objc-dynamic-declaration:
37898 @dynamic identifier-list ;
37900 For example:
37901 @dynamic MyProperty;
37902 @dynamic MyProperty, AnotherProperty;
37904 PS: This function is identical to c_parser_objc_at_dynamic_declaration
37905 for C. Keep them in sync.
37907 static void
37908 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
37910 tree list = NULL_TREE;
37911 location_t loc;
37912 loc = cp_lexer_peek_token (parser->lexer)->location;
37914 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
37915 while (true)
37917 tree property;
37918 property = cp_parser_identifier (parser);
37919 if (property == error_mark_node)
37921 cp_parser_consume_semicolon_at_end_of_statement (parser);
37922 return;
37924 list = chainon (list, build_tree_list (NULL, property));
37925 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37926 cp_lexer_consume_token (parser->lexer);
37927 else
37928 break;
37930 cp_parser_consume_semicolon_at_end_of_statement (parser);
37931 objc_add_dynamic_declaration (loc, list);
37935 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 / 4.5 / 5.0 parsing routines. */
37937 /* Returns name of the next clause.
37938 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
37939 the token is not consumed. Otherwise appropriate pragma_omp_clause is
37940 returned and the token is consumed. */
37942 static pragma_omp_clause
37943 cp_parser_omp_clause_name (cp_parser *parser)
37945 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
37947 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
37948 result = PRAGMA_OACC_CLAUSE_AUTO;
37949 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
37950 result = PRAGMA_OMP_CLAUSE_IF;
37951 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
37952 result = PRAGMA_OMP_CLAUSE_DEFAULT;
37953 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
37954 result = PRAGMA_OACC_CLAUSE_DELETE;
37955 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
37956 result = PRAGMA_OMP_CLAUSE_PRIVATE;
37957 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
37958 result = PRAGMA_OMP_CLAUSE_FOR;
37959 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37961 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37962 const char *p = IDENTIFIER_POINTER (id);
37964 switch (p[0])
37966 case 'a':
37967 if (!strcmp ("affinity", p))
37968 result = PRAGMA_OMP_CLAUSE_AFFINITY;
37969 else if (!strcmp ("aligned", p))
37970 result = PRAGMA_OMP_CLAUSE_ALIGNED;
37971 else if (!strcmp ("allocate", p))
37972 result = PRAGMA_OMP_CLAUSE_ALLOCATE;
37973 else if (!strcmp ("async", p))
37974 result = PRAGMA_OACC_CLAUSE_ASYNC;
37975 else if (!strcmp ("attach", p))
37976 result = PRAGMA_OACC_CLAUSE_ATTACH;
37977 break;
37978 case 'b':
37979 if (!strcmp ("bind", p))
37980 result = PRAGMA_OMP_CLAUSE_BIND;
37981 break;
37982 case 'c':
37983 if (!strcmp ("collapse", p))
37984 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
37985 else if (!strcmp ("copy", p))
37986 result = PRAGMA_OACC_CLAUSE_COPY;
37987 else if (!strcmp ("copyin", p))
37988 result = PRAGMA_OMP_CLAUSE_COPYIN;
37989 else if (!strcmp ("copyout", p))
37990 result = PRAGMA_OACC_CLAUSE_COPYOUT;
37991 else if (!strcmp ("copyprivate", p))
37992 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
37993 else if (!strcmp ("create", p))
37994 result = PRAGMA_OACC_CLAUSE_CREATE;
37995 break;
37996 case 'd':
37997 if (!strcmp ("defaultmap", p))
37998 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
37999 else if (!strcmp ("depend", p))
38000 result = PRAGMA_OMP_CLAUSE_DEPEND;
38001 else if (!strcmp ("detach", p))
38002 result = PRAGMA_OACC_CLAUSE_DETACH;
38003 else if (!strcmp ("device", p))
38004 result = PRAGMA_OMP_CLAUSE_DEVICE;
38005 else if (!strcmp ("deviceptr", p))
38006 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
38007 else if (!strcmp ("device_resident", p))
38008 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
38009 else if (!strcmp ("device_type", p))
38010 result = PRAGMA_OMP_CLAUSE_DEVICE_TYPE;
38011 else if (!strcmp ("dist_schedule", p))
38012 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
38013 else if (!strcmp ("doacross", p))
38014 result = PRAGMA_OMP_CLAUSE_DOACROSS;
38015 break;
38016 case 'e':
38017 if (!strcmp ("enter", p))
38018 result = PRAGMA_OMP_CLAUSE_ENTER;
38019 break;
38020 case 'f':
38021 if (!strcmp ("filter", p))
38022 result = PRAGMA_OMP_CLAUSE_FILTER;
38023 else if (!strcmp ("final", p))
38024 result = PRAGMA_OMP_CLAUSE_FINAL;
38025 else if (!strcmp ("finalize", p))
38026 result = PRAGMA_OACC_CLAUSE_FINALIZE;
38027 else if (!strcmp ("firstprivate", p))
38028 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
38029 else if (!strcmp ("from", p))
38030 result = PRAGMA_OMP_CLAUSE_FROM;
38031 break;
38032 case 'g':
38033 if (!strcmp ("gang", p))
38034 result = PRAGMA_OACC_CLAUSE_GANG;
38035 else if (!strcmp ("grainsize", p))
38036 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
38037 break;
38038 case 'h':
38039 if (!strcmp ("has_device_addr", p))
38040 result = PRAGMA_OMP_CLAUSE_HAS_DEVICE_ADDR;
38041 else if (!strcmp ("hint", p))
38042 result = PRAGMA_OMP_CLAUSE_HINT;
38043 else if (!strcmp ("host", p))
38044 result = PRAGMA_OACC_CLAUSE_HOST;
38045 break;
38046 case 'i':
38047 if (!strcmp ("if_present", p))
38048 result = PRAGMA_OACC_CLAUSE_IF_PRESENT;
38049 else if (!strcmp ("in_reduction", p))
38050 result = PRAGMA_OMP_CLAUSE_IN_REDUCTION;
38051 else if (!strcmp ("inbranch", p))
38052 result = PRAGMA_OMP_CLAUSE_INBRANCH;
38053 else if (!strcmp ("independent", p))
38054 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
38055 else if (!strcmp ("indirect", p))
38056 result = PRAGMA_OMP_CLAUSE_INDIRECT;
38057 else if (!strcmp ("is_device_ptr", p))
38058 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
38059 break;
38060 case 'l':
38061 if (!strcmp ("lastprivate", p))
38062 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
38063 else if (!strcmp ("linear", p))
38064 result = PRAGMA_OMP_CLAUSE_LINEAR;
38065 else if (!strcmp ("link", p))
38066 result = PRAGMA_OMP_CLAUSE_LINK;
38067 break;
38068 case 'm':
38069 if (!strcmp ("map", p))
38070 result = PRAGMA_OMP_CLAUSE_MAP;
38071 else if (!strcmp ("mergeable", p))
38072 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
38073 break;
38074 case 'n':
38075 if (!strcmp ("no_create", p))
38076 result = PRAGMA_OACC_CLAUSE_NO_CREATE;
38077 else if (!strcmp ("nogroup", p))
38078 result = PRAGMA_OMP_CLAUSE_NOGROUP;
38079 else if (!strcmp ("nohost", p))
38080 result = PRAGMA_OACC_CLAUSE_NOHOST;
38081 else if (!strcmp ("nontemporal", p))
38082 result = PRAGMA_OMP_CLAUSE_NONTEMPORAL;
38083 else if (!strcmp ("notinbranch", p))
38084 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
38085 else if (!strcmp ("nowait", p))
38086 result = PRAGMA_OMP_CLAUSE_NOWAIT;
38087 else if (!strcmp ("num_gangs", p))
38088 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
38089 else if (!strcmp ("num_tasks", p))
38090 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
38091 else if (!strcmp ("num_teams", p))
38092 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
38093 else if (!strcmp ("num_threads", p))
38094 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
38095 else if (!strcmp ("num_workers", p))
38096 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
38097 break;
38098 case 'o':
38099 if (!strcmp ("ordered", p))
38100 result = PRAGMA_OMP_CLAUSE_ORDERED;
38101 else if (!strcmp ("order", p))
38102 result = PRAGMA_OMP_CLAUSE_ORDER;
38103 break;
38104 case 'p':
38105 if (!strcmp ("parallel", p))
38106 result = PRAGMA_OMP_CLAUSE_PARALLEL;
38107 else if (!strcmp ("present", p))
38108 result = PRAGMA_OACC_CLAUSE_PRESENT;
38109 else if (!strcmp ("present_or_copy", p)
38110 || !strcmp ("pcopy", p))
38111 result = PRAGMA_OACC_CLAUSE_COPY;
38112 else if (!strcmp ("present_or_copyin", p)
38113 || !strcmp ("pcopyin", p))
38114 result = PRAGMA_OACC_CLAUSE_COPYIN;
38115 else if (!strcmp ("present_or_copyout", p)
38116 || !strcmp ("pcopyout", p))
38117 result = PRAGMA_OACC_CLAUSE_COPYOUT;
38118 else if (!strcmp ("present_or_create", p)
38119 || !strcmp ("pcreate", p))
38120 result = PRAGMA_OACC_CLAUSE_CREATE;
38121 else if (!strcmp ("priority", p))
38122 result = PRAGMA_OMP_CLAUSE_PRIORITY;
38123 else if (!strcmp ("proc_bind", p))
38124 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
38125 break;
38126 case 'r':
38127 if (!strcmp ("reduction", p))
38128 result = PRAGMA_OMP_CLAUSE_REDUCTION;
38129 break;
38130 case 's':
38131 if (!strcmp ("safelen", p))
38132 result = PRAGMA_OMP_CLAUSE_SAFELEN;
38133 else if (!strcmp ("schedule", p))
38134 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
38135 else if (!strcmp ("sections", p))
38136 result = PRAGMA_OMP_CLAUSE_SECTIONS;
38137 else if (!strcmp ("self", p))
38138 result = PRAGMA_OACC_CLAUSE_SELF;
38139 else if (!strcmp ("seq", p))
38140 result = PRAGMA_OACC_CLAUSE_SEQ;
38141 else if (!strcmp ("shared", p))
38142 result = PRAGMA_OMP_CLAUSE_SHARED;
38143 else if (!strcmp ("simd", p))
38144 result = PRAGMA_OMP_CLAUSE_SIMD;
38145 else if (!strcmp ("simdlen", p))
38146 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
38147 break;
38148 case 't':
38149 if (!strcmp ("task_reduction", p))
38150 result = PRAGMA_OMP_CLAUSE_TASK_REDUCTION;
38151 else if (!strcmp ("taskgroup", p))
38152 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
38153 else if (!strcmp ("thread_limit", p))
38154 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
38155 else if (!strcmp ("threads", p))
38156 result = PRAGMA_OMP_CLAUSE_THREADS;
38157 else if (!strcmp ("tile", p))
38158 result = PRAGMA_OACC_CLAUSE_TILE;
38159 else if (!strcmp ("to", p))
38160 result = PRAGMA_OMP_CLAUSE_TO;
38161 break;
38162 case 'u':
38163 if (!strcmp ("uniform", p))
38164 result = PRAGMA_OMP_CLAUSE_UNIFORM;
38165 else if (!strcmp ("untied", p))
38166 result = PRAGMA_OMP_CLAUSE_UNTIED;
38167 else if (!strcmp ("use_device", p))
38168 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
38169 else if (!strcmp ("use_device_addr", p))
38170 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR;
38171 else if (!strcmp ("use_device_ptr", p))
38172 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
38173 break;
38174 case 'v':
38175 if (!strcmp ("vector", p))
38176 result = PRAGMA_OACC_CLAUSE_VECTOR;
38177 else if (!strcmp ("vector_length", p))
38178 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
38179 break;
38180 case 'w':
38181 if (!strcmp ("wait", p))
38182 result = PRAGMA_OACC_CLAUSE_WAIT;
38183 else if (!strcmp ("worker", p))
38184 result = PRAGMA_OACC_CLAUSE_WORKER;
38185 break;
38189 if (result != PRAGMA_OMP_CLAUSE_NONE)
38190 cp_lexer_consume_token (parser->lexer);
38192 return result;
38195 /* Validate that a clause of the given type does not already exist. */
38197 static void
38198 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
38199 const char *name, location_t location)
38201 if (omp_find_clause (clauses, code))
38202 error_at (location, "too many %qs clauses", name);
38205 /* OpenMP 2.5:
38206 variable-list:
38207 identifier
38208 variable-list , identifier
38210 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
38211 colon). An opening parenthesis will have been consumed by the caller.
38213 If KIND is nonzero, create the appropriate node and install the decl
38214 in OMP_CLAUSE_DECL and add the node to the head of the list.
38216 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
38217 return the list created.
38219 COLON can be NULL if only closing parenthesis should end the list,
38220 or pointer to bool which will receive false if the list is terminated
38221 by closing parenthesis or true if the list is terminated by colon.
38223 The optional ALLOW_DEREF argument is true if list items can use the deref
38224 (->) operator. */
38226 struct omp_dim
38228 tree low_bound, length;
38229 location_t loc;
38230 bool no_colon;
38231 omp_dim (tree lb, tree len, location_t lo, bool nc)
38232 : low_bound (lb), length (len), loc (lo), no_colon (nc) {}
38235 static tree
38236 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
38237 tree list, bool *colon,
38238 bool map_lvalue = false)
38240 auto_vec<omp_dim> dims;
38241 bool array_section_p;
38242 cp_token *token;
38243 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
38244 if (colon)
38246 parser->colon_corrects_to_scope_p = false;
38247 *colon = false;
38249 while (1)
38251 tree name, decl;
38253 if (kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
38254 cp_parser_parse_tentatively (parser);
38255 /* This condition doesn't include OMP_CLAUSE_DEPEND or
38256 OMP_CLAUSE_AFFINITY since lvalue ("locator list") parsing for those is
38257 handled further down the function. */
38258 else if (map_lvalue
38259 && (kind == OMP_CLAUSE_MAP
38260 || kind == OMP_CLAUSE_TO
38261 || kind == OMP_CLAUSE_FROM))
38263 auto s = make_temp_override (parser->omp_array_section_p, true);
38264 token = cp_lexer_peek_token (parser->lexer);
38265 location_t loc = token->location;
38266 decl = cp_parser_assignment_expression (parser);
38268 /* This code rewrites a parsed expression containing various tree
38269 codes used to represent array accesses into a more uniform nest of
38270 OMP_ARRAY_SECTION nodes before it is processed by
38271 semantics.cc:handle_omp_array_sections_1. It might be more
38272 efficient to move this logic to that function instead, analysing
38273 the parsed expression directly rather than this preprocessed
38274 form. */
38275 dims.truncate (0);
38276 if (TREE_CODE (decl) == OMP_ARRAY_SECTION)
38278 while (TREE_CODE (decl) == OMP_ARRAY_SECTION)
38280 tree low_bound = TREE_OPERAND (decl, 1);
38281 tree length = TREE_OPERAND (decl, 2);
38282 dims.safe_push (omp_dim (low_bound, length, loc, false));
38283 decl = TREE_OPERAND (decl, 0);
38286 while (TREE_CODE (decl) == ARRAY_REF
38287 || TREE_CODE (decl) == INDIRECT_REF
38288 || TREE_CODE (decl) == COMPOUND_EXPR)
38290 if (REFERENCE_REF_P (decl))
38291 break;
38293 if (TREE_CODE (decl) == COMPOUND_EXPR)
38295 decl = TREE_OPERAND (decl, 1);
38296 STRIP_NOPS (decl);
38298 else if (TREE_CODE (decl) == INDIRECT_REF)
38300 dims.safe_push (omp_dim (integer_zero_node,
38301 integer_one_node, loc, true));
38302 decl = TREE_OPERAND (decl, 0);
38304 else /* ARRAY_REF. */
38306 tree index = TREE_OPERAND (decl, 1);
38307 dims.safe_push (omp_dim (index, integer_one_node, loc,
38308 true));
38309 decl = TREE_OPERAND (decl, 0);
38313 /* Bare references have their own special handling, so remove
38314 the explicit dereference added by convert_from_reference. */
38315 if (REFERENCE_REF_P (decl))
38316 decl = TREE_OPERAND (decl, 0);
38318 for (int i = dims.length () - 1; i >= 0; i--)
38319 decl = grok_omp_array_section (loc, decl, dims[i].low_bound,
38320 dims[i].length);
38322 else if (TREE_CODE (decl) == INDIRECT_REF)
38324 bool ref_p = REFERENCE_REF_P (decl);
38326 /* If we have "*foo" and
38327 - it's an indirection of a reference, "unconvert" it, i.e.
38328 strip the indirection (to just "foo").
38329 - it's an indirection of a pointer, turn it into
38330 "foo[0:1]". */
38331 decl = TREE_OPERAND (decl, 0);
38332 STRIP_NOPS (decl);
38334 if (!ref_p)
38335 decl = grok_omp_array_section (loc, decl, integer_zero_node,
38336 integer_one_node);
38338 else if (TREE_CODE (decl) == ARRAY_REF)
38340 tree idx = TREE_OPERAND (decl, 1);
38342 decl = TREE_OPERAND (decl, 0);
38343 STRIP_NOPS (decl);
38345 decl = grok_omp_array_section (loc, decl, idx, integer_one_node);
38347 else if (TREE_CODE (decl) == NON_LVALUE_EXPR
38348 || CONVERT_EXPR_P (decl))
38349 decl = TREE_OPERAND (decl, 0);
38351 goto build_clause;
38353 token = cp_lexer_peek_token (parser->lexer);
38354 if (kind != 0
38355 && cp_parser_is_keyword (token, RID_THIS))
38357 decl = finish_this_expr ();
38358 if (TREE_CODE (decl) == NON_LVALUE_EXPR
38359 || CONVERT_EXPR_P (decl))
38360 decl = TREE_OPERAND (decl, 0);
38361 cp_lexer_consume_token (parser->lexer);
38363 else if (cp_parser_is_keyword (token, RID_FUNCTION_NAME)
38364 || cp_parser_is_keyword (token, RID_PRETTY_FUNCTION_NAME)
38365 || cp_parser_is_keyword (token, RID_C99_FUNCTION_NAME))
38367 cp_id_kind idk;
38368 decl = cp_parser_primary_expression (parser, false, false, false,
38369 &idk);
38371 else if (kind == OMP_CLAUSE_DEPEND
38372 && cp_parser_is_keyword (token, RID_OMP_ALL_MEMORY)
38373 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
38374 || cp_lexer_nth_token_is (parser->lexer, 2,
38375 CPP_CLOSE_PAREN)))
38377 decl = ridpointers[RID_OMP_ALL_MEMORY];
38378 cp_lexer_consume_token (parser->lexer);
38380 else
38382 name = cp_parser_id_expression (parser, /*template_p=*/false,
38383 /*check_dependency_p=*/true,
38384 /*template_p=*/NULL,
38385 /*declarator_p=*/false,
38386 /*optional_p=*/false);
38387 if (name == error_mark_node)
38389 if ((kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
38390 && cp_parser_simulate_error (parser))
38391 goto depend_lvalue;
38392 goto skip_comma;
38395 if (identifier_p (name))
38396 decl = cp_parser_lookup_name_simple (parser, name, token->location);
38397 else
38398 decl = name;
38399 if (decl == error_mark_node)
38401 if ((kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
38402 && cp_parser_simulate_error (parser))
38403 goto depend_lvalue;
38404 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
38405 token->location);
38408 if (outer_automatic_var_p (decl))
38409 decl = process_outer_var_ref (decl, tf_warning_or_error);
38410 if (decl == error_mark_node)
38412 else if (kind != 0)
38414 switch (kind)
38416 case OMP_CLAUSE__CACHE_:
38417 /* The OpenACC cache directive explicitly only allows "array
38418 elements or subarrays". */
38419 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
38421 error_at (token->location, "expected %<[%>");
38422 decl = error_mark_node;
38423 break;
38425 /* FALLTHROUGH. */
38426 case OMP_CLAUSE_MAP:
38427 case OMP_CLAUSE_FROM:
38428 case OMP_CLAUSE_TO:
38429 start_component_ref:
38430 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT)
38431 || cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
38433 cpp_ttype ttype
38434 = cp_lexer_next_token_is (parser->lexer, CPP_DOT)
38435 ? CPP_DOT : CPP_DEREF;
38436 location_t loc
38437 = cp_lexer_peek_token (parser->lexer)->location;
38438 cp_id_kind idk = CP_ID_KIND_NONE;
38439 cp_lexer_consume_token (parser->lexer);
38440 decl = convert_from_reference (decl);
38441 decl = (cp_parser_postfix_dot_deref_expression
38442 (parser, ttype, cp_expr (decl, token->location),
38443 false, &idk, loc));
38445 /* FALLTHROUGH. */
38446 case OMP_CLAUSE_AFFINITY:
38447 case OMP_CLAUSE_DEPEND:
38448 case OMP_CLAUSE_REDUCTION:
38449 case OMP_CLAUSE_IN_REDUCTION:
38450 case OMP_CLAUSE_TASK_REDUCTION:
38451 case OMP_CLAUSE_HAS_DEVICE_ADDR:
38452 array_section_p = false;
38453 dims.truncate (0);
38454 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
38456 location_t loc = UNKNOWN_LOCATION;
38457 tree low_bound = NULL_TREE, length = NULL_TREE;
38458 bool no_colon = false;
38460 parser->colon_corrects_to_scope_p = false;
38461 cp_lexer_consume_token (parser->lexer);
38462 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
38464 loc = cp_lexer_peek_token (parser->lexer)->location;
38465 low_bound = cp_parser_expression (parser);
38466 /* Later handling is not prepared to see through these. */
38467 gcc_checking_assert (!location_wrapper_p (low_bound));
38469 if (!colon)
38470 parser->colon_corrects_to_scope_p
38471 = saved_colon_corrects_to_scope_p;
38472 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
38474 length = integer_one_node;
38475 no_colon = true;
38477 else
38479 /* Look for `:'. */
38480 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
38482 if ((kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
38483 && cp_parser_simulate_error (parser))
38484 goto depend_lvalue;
38485 goto skip_comma;
38487 if (kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
38488 cp_parser_commit_to_tentative_parse (parser);
38489 else
38490 array_section_p = true;
38491 if (!cp_lexer_next_token_is (parser->lexer,
38492 CPP_CLOSE_SQUARE))
38494 length = cp_parser_expression (parser);
38495 /* Later handling is not prepared to see through these. */
38496 gcc_checking_assert (!location_wrapper_p (length));
38499 /* Look for the closing `]'. */
38500 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
38501 RT_CLOSE_SQUARE))
38503 if ((kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
38504 && cp_parser_simulate_error (parser))
38505 goto depend_lvalue;
38506 goto skip_comma;
38509 dims.safe_push (omp_dim (low_bound, length, loc, no_colon));
38512 if ((kind == OMP_CLAUSE_MAP
38513 || kind == OMP_CLAUSE_FROM
38514 || kind == OMP_CLAUSE_TO)
38515 && !array_section_p
38516 && (cp_lexer_next_token_is (parser->lexer, CPP_DOT)
38517 || cp_lexer_next_token_is (parser->lexer, CPP_DEREF)))
38519 for (unsigned i = 0; i < dims.length (); i++)
38521 gcc_assert (dims[i].length == integer_one_node);
38522 decl = build_array_ref (dims[i].loc,
38523 decl, dims[i].low_bound);
38525 goto start_component_ref;
38527 else
38528 for (unsigned i = 0; i < dims.length (); i++)
38529 decl = build_omp_array_section (input_location, decl,
38530 dims[i].low_bound,
38531 dims[i].length);
38532 break;
38533 default:
38534 break;
38537 if (kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
38539 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
38540 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
38541 && cp_parser_simulate_error (parser))
38543 depend_lvalue:
38544 cp_parser_abort_tentative_parse (parser);
38545 decl = cp_parser_assignment_expression (parser, NULL,
38546 false, false);
38548 else
38549 cp_parser_parse_definitely (parser);
38552 build_clause:
38553 tree u = build_omp_clause (token->location, kind);
38554 OMP_CLAUSE_DECL (u) = decl;
38555 OMP_CLAUSE_CHAIN (u) = list;
38556 list = u;
38558 else
38559 list = tree_cons (decl, NULL_TREE, list);
38561 get_comma:
38562 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
38563 break;
38564 cp_lexer_consume_token (parser->lexer);
38567 if (colon)
38568 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
38570 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
38572 *colon = true;
38573 cp_parser_require (parser, CPP_COLON, RT_COLON);
38574 return list;
38577 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
38579 int ending;
38581 /* Try to resync to an unnested comma. Copied from
38582 cp_parser_parenthesized_expression_list. */
38583 skip_comma:
38584 if (colon)
38585 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
38586 ending = cp_parser_skip_to_closing_parenthesis (parser,
38587 /*recovering=*/true,
38588 /*or_comma=*/true,
38589 /*consume_paren=*/true);
38590 if (ending < 0)
38591 goto get_comma;
38594 return list;
38597 /* Similarly, but expect leading and trailing parenthesis. This is a very
38598 common case for omp clauses. */
38600 static tree
38601 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list,
38602 bool map_lvalue = false)
38604 if (parser->lexer->in_omp_decl_attribute)
38606 if (kind)
38608 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38609 tree u = build_omp_clause (loc, kind);
38610 OMP_CLAUSE_DECL (u) = parser->lexer->in_omp_decl_attribute;
38611 OMP_CLAUSE_CHAIN (u) = list;
38612 return u;
38614 else
38615 return tree_cons (parser->lexer->in_omp_decl_attribute, NULL_TREE,
38616 list);
38619 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38620 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL,
38621 map_lvalue);
38622 return list;
38625 /* OpenACC 2.0:
38626 copy ( variable-list )
38627 copyin ( variable-list )
38628 copyout ( variable-list )
38629 create ( variable-list )
38630 delete ( variable-list )
38631 present ( variable-list )
38633 OpenACC 2.6:
38634 no_create ( variable-list )
38635 attach ( variable-list )
38636 detach ( variable-list )
38638 OpenACC 2.7:
38639 copyin (readonly : variable-list )
38642 static tree
38643 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
38644 tree list)
38646 enum gomp_map_kind kind;
38647 switch (c_kind)
38649 case PRAGMA_OACC_CLAUSE_ATTACH:
38650 kind = GOMP_MAP_ATTACH;
38651 break;
38652 case PRAGMA_OACC_CLAUSE_COPY:
38653 kind = GOMP_MAP_TOFROM;
38654 break;
38655 case PRAGMA_OACC_CLAUSE_COPYIN:
38656 kind = GOMP_MAP_TO;
38657 break;
38658 case PRAGMA_OACC_CLAUSE_COPYOUT:
38659 kind = GOMP_MAP_FROM;
38660 break;
38661 case PRAGMA_OACC_CLAUSE_CREATE:
38662 kind = GOMP_MAP_ALLOC;
38663 break;
38664 case PRAGMA_OACC_CLAUSE_DELETE:
38665 kind = GOMP_MAP_RELEASE;
38666 break;
38667 case PRAGMA_OACC_CLAUSE_DETACH:
38668 kind = GOMP_MAP_DETACH;
38669 break;
38670 case PRAGMA_OACC_CLAUSE_DEVICE:
38671 kind = GOMP_MAP_FORCE_TO;
38672 break;
38673 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
38674 kind = GOMP_MAP_DEVICE_RESIDENT;
38675 break;
38676 case PRAGMA_OACC_CLAUSE_LINK:
38677 kind = GOMP_MAP_LINK;
38678 break;
38679 case PRAGMA_OACC_CLAUSE_NO_CREATE:
38680 kind = GOMP_MAP_IF_PRESENT;
38681 break;
38682 case PRAGMA_OACC_CLAUSE_PRESENT:
38683 kind = GOMP_MAP_FORCE_PRESENT;
38684 break;
38685 case PRAGMA_OACC_CLAUSE_SELF:
38686 /* "The 'host' clause is a synonym for the 'self' clause." */
38687 case PRAGMA_OACC_CLAUSE_HOST:
38688 kind = GOMP_MAP_FORCE_FROM;
38689 break;
38690 default:
38691 gcc_unreachable ();
38694 tree nl = list;
38695 bool readonly = false;
38696 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38698 /* Turn on readonly modifier parsing for copyin clause. */
38699 if (c_kind == PRAGMA_OACC_CLAUSE_COPYIN)
38701 cp_token *token = cp_lexer_peek_token (parser->lexer);
38702 if (token->type == CPP_NAME
38703 && !strcmp (IDENTIFIER_POINTER (token->u.value), "readonly")
38704 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
38706 cp_lexer_consume_token (parser->lexer);
38707 cp_lexer_consume_token (parser->lexer);
38708 readonly = true;
38711 nl = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list, NULL,
38712 false);
38715 for (tree c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
38717 OMP_CLAUSE_SET_MAP_KIND (c, kind);
38718 if (readonly)
38719 OMP_CLAUSE_MAP_READONLY (c) = 1;
38722 return nl;
38725 /* OpenACC 2.0:
38726 deviceptr ( variable-list ) */
38728 static tree
38729 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
38731 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38732 tree vars, t;
38734 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
38735 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
38736 variable-list must only allow for pointer variables. */
38737 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
38738 for (t = vars; t; t = TREE_CHAIN (t))
38740 tree v = TREE_PURPOSE (t);
38741 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
38742 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
38743 OMP_CLAUSE_DECL (u) = v;
38744 OMP_CLAUSE_CHAIN (u) = list;
38745 list = u;
38748 return list;
38751 /* OpenACC 2.5:
38752 auto
38753 finalize
38754 independent
38755 nohost
38756 seq */
38758 static tree
38759 cp_parser_oacc_simple_clause (location_t loc, enum omp_clause_code code,
38760 tree list)
38762 check_no_duplicate_clause (list, code, omp_clause_code_name[code], loc);
38764 tree c = build_omp_clause (loc, code);
38765 OMP_CLAUSE_CHAIN (c) = list;
38767 return c;
38770 /* OpenACC:
38771 num_gangs ( expression )
38772 num_workers ( expression )
38773 vector_length ( expression ) */
38775 static tree
38776 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
38777 const char *str, tree list)
38779 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38781 matching_parens parens;
38782 if (!parens.require_open (parser))
38783 return list;
38785 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
38787 if (t == error_mark_node
38788 || !parens.require_close (parser))
38790 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38791 /*or_comma=*/false,
38792 /*consume_paren=*/true);
38793 return list;
38796 check_no_duplicate_clause (list, code, str, loc);
38798 tree c = build_omp_clause (loc, code);
38799 OMP_CLAUSE_OPERAND (c, 0) = t;
38800 OMP_CLAUSE_CHAIN (c) = list;
38801 return c;
38804 /* OpenACC:
38806 gang [( gang-arg-list )]
38807 worker [( [num:] int-expr )]
38808 vector [( [length:] int-expr )]
38810 where gang-arg is one of:
38812 [num:] int-expr
38813 static: size-expr
38815 and size-expr may be:
38818 int-expr
38821 static tree
38822 cp_parser_oacc_shape_clause (cp_parser *parser, location_t loc,
38823 omp_clause_code kind,
38824 const char *str, tree list)
38826 const char *id = "num";
38827 cp_lexer *lexer = parser->lexer;
38828 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
38830 if (kind == OMP_CLAUSE_VECTOR)
38831 id = "length";
38833 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
38835 matching_parens parens;
38836 parens.consume_open (parser);
38840 cp_token *next = cp_lexer_peek_token (lexer);
38841 int idx = 0;
38843 /* Gang static argument. */
38844 if (kind == OMP_CLAUSE_GANG
38845 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
38847 cp_lexer_consume_token (lexer);
38849 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
38850 goto cleanup_error;
38852 idx = 1;
38853 if (ops[idx] != NULL)
38855 cp_parser_error (parser, "too many %<static%> arguments");
38856 goto cleanup_error;
38859 /* Check for the '*' argument. */
38860 if (cp_lexer_next_token_is (lexer, CPP_MULT)
38861 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
38862 || cp_lexer_nth_token_is (parser->lexer, 2,
38863 CPP_CLOSE_PAREN)))
38865 cp_lexer_consume_token (lexer);
38866 ops[idx] = integer_minus_one_node;
38868 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
38870 cp_lexer_consume_token (lexer);
38871 continue;
38873 else break;
38876 /* Worker num: argument and vector length: arguments. */
38877 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
38878 && id_equal (next->u.value, id)
38879 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
38881 cp_lexer_consume_token (lexer); /* id */
38882 cp_lexer_consume_token (lexer); /* ':' */
38885 /* Now collect the actual argument. */
38886 if (ops[idx] != NULL_TREE)
38888 cp_parser_error (parser, "unexpected argument");
38889 goto cleanup_error;
38892 tree expr = cp_parser_assignment_expression (parser, NULL, false,
38893 false);
38894 if (expr == error_mark_node)
38895 goto cleanup_error;
38897 mark_exp_read (expr);
38898 ops[idx] = expr;
38900 if (kind == OMP_CLAUSE_GANG
38901 && cp_lexer_next_token_is (lexer, CPP_COMMA))
38903 cp_lexer_consume_token (lexer);
38904 continue;
38906 break;
38908 while (1);
38910 if (!parens.require_close (parser))
38911 goto cleanup_error;
38914 check_no_duplicate_clause (list, kind, str, loc);
38916 c = build_omp_clause (loc, kind);
38918 if (ops[1])
38919 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
38921 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
38922 OMP_CLAUSE_CHAIN (c) = list;
38924 return c;
38926 cleanup_error:
38927 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
38928 return list;
38931 /* OpenACC 2.0:
38932 tile ( size-expr-list ) */
38934 static tree
38935 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
38937 tree c, expr = error_mark_node;
38938 tree tile = NULL_TREE;
38940 /* Collapse and tile are mutually exclusive. (The spec doesn't say
38941 so, but the spec authors never considered such a case and have
38942 differing opinions on what it might mean, including 'not
38943 allowed'.) */
38944 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
38945 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
38946 clause_loc);
38948 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38949 return list;
38953 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
38954 return list;
38956 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
38957 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
38958 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
38960 cp_lexer_consume_token (parser->lexer);
38961 expr = integer_zero_node;
38963 else
38964 expr = cp_parser_constant_expression (parser);
38966 tile = tree_cons (NULL_TREE, expr, tile);
38968 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
38970 /* Consume the trailing ')'. */
38971 cp_lexer_consume_token (parser->lexer);
38973 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
38974 tile = nreverse (tile);
38975 OMP_CLAUSE_TILE_LIST (c) = tile;
38976 OMP_CLAUSE_CHAIN (c) = list;
38977 return c;
38980 /* OpenACC 2.0
38981 Parse wait clause or directive parameters. */
38983 static tree
38984 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
38986 vec<tree, va_gc> *args;
38987 tree t, args_tree;
38989 args = cp_parser_parenthesized_expression_list (parser, non_attr,
38990 /*cast_p=*/false,
38991 /*allow_expansion_p=*/true,
38992 /*non_constant_p=*/NULL);
38994 if (args == NULL || args->length () == 0)
38996 if (args != NULL)
38998 cp_parser_error (parser, "expected integer expression list");
38999 release_tree_vector (args);
39001 return list;
39004 args_tree = build_tree_list_vec (args);
39006 release_tree_vector (args);
39008 for (t = args_tree; t; t = TREE_CHAIN (t))
39010 tree targ = TREE_VALUE (t);
39012 if (targ != error_mark_node)
39014 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
39015 error ("%<wait%> expression must be integral");
39016 else
39018 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
39020 targ = mark_rvalue_use (targ);
39021 OMP_CLAUSE_DECL (c) = targ;
39022 OMP_CLAUSE_CHAIN (c) = list;
39023 list = c;
39028 return list;
39031 /* OpenACC:
39032 wait [( int-expr-list )] */
39034 static tree
39035 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
39037 location_t location = cp_lexer_peek_token (parser->lexer)->location;
39039 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
39040 list = cp_parser_oacc_wait_list (parser, location, list);
39041 else
39043 tree c = build_omp_clause (location, OMP_CLAUSE_WAIT);
39045 OMP_CLAUSE_DECL (c) = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
39046 OMP_CLAUSE_CHAIN (c) = list;
39047 list = c;
39050 return list;
39053 /* OpenMP 3.0:
39054 collapse ( constant-expression ) */
39056 static tree
39057 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
39059 tree c, num;
39060 location_t loc;
39061 HOST_WIDE_INT n;
39063 loc = cp_lexer_peek_token (parser->lexer)->location;
39064 matching_parens parens;
39065 if (!parens.require_open (parser))
39066 return list;
39068 num = cp_parser_constant_expression (parser);
39070 if (!parens.require_close (parser))
39071 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39072 /*or_comma=*/false,
39073 /*consume_paren=*/true);
39075 if (num == error_mark_node)
39076 return list;
39077 num = fold_non_dependent_expr (num);
39078 if (!tree_fits_shwi_p (num)
39079 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
39080 || (n = tree_to_shwi (num)) <= 0
39081 || (int) n != n)
39083 error_at (loc, "collapse argument needs positive constant integer expression");
39084 return list;
39087 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
39088 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
39089 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
39090 OMP_CLAUSE_CHAIN (c) = list;
39091 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
39093 return c;
39096 /* OpenMP 2.5:
39097 default ( none | shared )
39099 OpenMP 5.1:
39100 default ( private | firstprivate )
39102 OpenACC:
39103 default ( none | present ) */
39105 static tree
39106 cp_parser_omp_clause_default (cp_parser *parser, tree list,
39107 location_t location, bool is_oacc)
39109 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
39110 tree c;
39112 matching_parens parens;
39113 if (!parens.require_open (parser))
39114 return list;
39115 if (!is_oacc && cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
39117 kind = OMP_CLAUSE_DEFAULT_PRIVATE;
39118 cp_lexer_consume_token (parser->lexer);
39120 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39122 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39123 const char *p = IDENTIFIER_POINTER (id);
39125 switch (p[0])
39127 case 'n':
39128 if (strcmp ("none", p) != 0)
39129 goto invalid_kind;
39130 kind = OMP_CLAUSE_DEFAULT_NONE;
39131 break;
39133 case 'p':
39134 if (strcmp ("present", p) != 0 || !is_oacc)
39135 goto invalid_kind;
39136 kind = OMP_CLAUSE_DEFAULT_PRESENT;
39137 break;
39139 case 'f':
39140 if (strcmp ("firstprivate", p) != 0 || is_oacc)
39141 goto invalid_kind;
39142 kind = OMP_CLAUSE_DEFAULT_FIRSTPRIVATE;
39143 break;
39145 case 's':
39146 if (strcmp ("shared", p) != 0 || is_oacc)
39147 goto invalid_kind;
39148 kind = OMP_CLAUSE_DEFAULT_SHARED;
39149 break;
39151 default:
39152 goto invalid_kind;
39155 cp_lexer_consume_token (parser->lexer);
39157 else
39159 invalid_kind:
39160 if (is_oacc)
39161 cp_parser_error (parser, "expected %<none%> or %<present%>");
39162 else
39163 cp_parser_error (parser, "expected %<none%>, %<shared%>, "
39164 "%<private%> or %<firstprivate%>");
39167 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
39168 || !parens.require_close (parser))
39169 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39170 /*or_comma=*/false,
39171 /*consume_paren=*/true);
39173 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
39174 return list;
39176 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
39177 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
39178 OMP_CLAUSE_CHAIN (c) = list;
39179 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
39181 return c;
39184 /* OpenMP 3.1:
39185 final ( expression ) */
39187 static tree
39188 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
39190 tree t, c;
39192 matching_parens parens;
39193 if (!parens.require_open (parser))
39194 return list;
39196 t = cp_parser_assignment_expression (parser);
39198 if (t == error_mark_node
39199 || !parens.require_close (parser))
39200 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39201 /*or_comma=*/false,
39202 /*consume_paren=*/true);
39204 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
39206 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
39207 OMP_CLAUSE_FINAL_EXPR (c) = t;
39208 OMP_CLAUSE_CHAIN (c) = list;
39210 return c;
39213 /* OpenMP 5.1:
39214 indirect [( expression )]
39217 static tree
39218 cp_parser_omp_clause_indirect (cp_parser *parser, tree list,
39219 location_t location)
39221 tree t;
39223 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
39225 matching_parens parens;
39226 if (!parens.require_open (parser))
39227 return list;
39229 bool non_constant_p;
39230 t = cp_parser_constant_expression (parser, true, &non_constant_p);
39232 if (t != error_mark_node && non_constant_p)
39233 error_at (location, "expected constant logical expression");
39235 if (t == error_mark_node
39236 || !parens.require_close (parser))
39237 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39238 /*or_comma=*/false,
39239 /*consume_paren=*/true);
39241 else
39242 t = integer_one_node;
39244 check_no_duplicate_clause (list, OMP_CLAUSE_INDIRECT, "indirect", location);
39246 tree c = build_omp_clause (location, OMP_CLAUSE_INDIRECT);
39247 OMP_CLAUSE_INDIRECT_EXPR (c) = t;
39248 OMP_CLAUSE_CHAIN (c) = list;
39250 return c;
39253 /* OpenMP 2.5:
39254 if ( expression )
39256 OpenMP 4.5:
39257 if ( directive-name-modifier : expression )
39259 directive-name-modifier:
39260 parallel | task | taskloop | target data | target | target update
39261 | target enter data | target exit data
39263 OpenMP 5.0:
39264 directive-name-modifier:
39265 ... | simd | cancel */
39267 static tree
39268 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
39269 bool is_omp)
39271 tree t, c;
39272 enum tree_code if_modifier = ERROR_MARK;
39274 matching_parens parens;
39275 if (!parens.require_open (parser))
39276 return list;
39278 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39280 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39281 const char *p = IDENTIFIER_POINTER (id);
39282 int n = 2;
39284 if (strcmp ("cancel", p) == 0)
39285 if_modifier = VOID_CST;
39286 else if (strcmp ("parallel", p) == 0)
39287 if_modifier = OMP_PARALLEL;
39288 else if (strcmp ("simd", p) == 0)
39289 if_modifier = OMP_SIMD;
39290 else if (strcmp ("task", p) == 0)
39291 if_modifier = OMP_TASK;
39292 else if (strcmp ("taskloop", p) == 0)
39293 if_modifier = OMP_TASKLOOP;
39294 else if (strcmp ("target", p) == 0)
39296 if_modifier = OMP_TARGET;
39297 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
39299 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
39300 p = IDENTIFIER_POINTER (id);
39301 if (strcmp ("data", p) == 0)
39302 if_modifier = OMP_TARGET_DATA;
39303 else if (strcmp ("update", p) == 0)
39304 if_modifier = OMP_TARGET_UPDATE;
39305 else if (strcmp ("enter", p) == 0)
39306 if_modifier = OMP_TARGET_ENTER_DATA;
39307 else if (strcmp ("exit", p) == 0)
39308 if_modifier = OMP_TARGET_EXIT_DATA;
39309 if (if_modifier != OMP_TARGET)
39310 n = 3;
39311 else
39313 location_t loc
39314 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
39315 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
39316 "or %<exit%>");
39317 if_modifier = ERROR_MARK;
39319 if (if_modifier == OMP_TARGET_ENTER_DATA
39320 || if_modifier == OMP_TARGET_EXIT_DATA)
39322 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
39324 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
39325 p = IDENTIFIER_POINTER (id);
39326 if (strcmp ("data", p) == 0)
39327 n = 4;
39329 if (n != 4)
39331 location_t loc
39332 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
39333 error_at (loc, "expected %<data%>");
39334 if_modifier = ERROR_MARK;
39339 if (if_modifier != ERROR_MARK)
39341 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
39343 while (n-- > 0)
39344 cp_lexer_consume_token (parser->lexer);
39346 else
39348 if (n > 2)
39350 location_t loc
39351 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
39352 error_at (loc, "expected %<:%>");
39354 if_modifier = ERROR_MARK;
39359 t = cp_parser_assignment_expression (parser);
39361 if (t == error_mark_node
39362 || !parens.require_close (parser))
39363 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39364 /*or_comma=*/false,
39365 /*consume_paren=*/true);
39367 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
39368 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
39370 if (if_modifier != ERROR_MARK
39371 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
39373 const char *p = NULL;
39374 switch (if_modifier)
39376 case VOID_CST: p = "cancel"; break;
39377 case OMP_PARALLEL: p = "parallel"; break;
39378 case OMP_SIMD: p = "simd"; break;
39379 case OMP_TASK: p = "task"; break;
39380 case OMP_TASKLOOP: p = "taskloop"; break;
39381 case OMP_TARGET_DATA: p = "target data"; break;
39382 case OMP_TARGET: p = "target"; break;
39383 case OMP_TARGET_UPDATE: p = "target update"; break;
39384 case OMP_TARGET_ENTER_DATA: p = "target enter data"; break;
39385 case OMP_TARGET_EXIT_DATA: p = "target exit data"; break;
39386 default: gcc_unreachable ();
39388 error_at (location, "too many %<if%> clauses with %qs modifier",
39390 return list;
39392 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
39394 if (!is_omp)
39395 error_at (location, "too many %<if%> clauses");
39396 else
39397 error_at (location, "too many %<if%> clauses without modifier");
39398 return list;
39400 else if (if_modifier == ERROR_MARK
39401 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
39403 error_at (location, "if any %<if%> clause has modifier, then all "
39404 "%<if%> clauses have to use modifier");
39405 return list;
39409 c = build_omp_clause (location, OMP_CLAUSE_IF);
39410 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
39411 OMP_CLAUSE_IF_EXPR (c) = t;
39412 OMP_CLAUSE_CHAIN (c) = list;
39414 return c;
39417 /* OpenMP 3.1:
39418 mergeable */
39420 static tree
39421 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
39422 tree list, location_t location)
39424 tree c;
39426 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
39427 location);
39429 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
39430 OMP_CLAUSE_CHAIN (c) = list;
39431 return c;
39434 /* OpenMP 2.5:
39435 nowait */
39437 static tree
39438 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
39439 tree list, location_t location)
39441 tree c;
39443 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
39445 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
39446 OMP_CLAUSE_CHAIN (c) = list;
39447 return c;
39450 /* OpenMP 2.5:
39451 num_threads ( expression ) */
39453 static tree
39454 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
39455 location_t location)
39457 tree t, c;
39459 matching_parens parens;
39460 if (!parens.require_open (parser))
39461 return list;
39463 t = cp_parser_assignment_expression (parser);
39465 if (t == error_mark_node
39466 || !parens.require_close (parser))
39467 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39468 /*or_comma=*/false,
39469 /*consume_paren=*/true);
39471 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
39472 "num_threads", location);
39474 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
39475 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
39476 OMP_CLAUSE_CHAIN (c) = list;
39478 return c;
39481 /* OpenMP 4.5:
39482 num_tasks ( expression )
39484 OpenMP 5.1:
39485 num_tasks ( strict : expression ) */
39487 static tree
39488 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
39489 location_t location)
39491 tree t, c;
39493 matching_parens parens;
39494 if (!parens.require_open (parser))
39495 return list;
39497 bool strict = false;
39498 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
39499 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
39501 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39502 if (!strcmp (IDENTIFIER_POINTER (id), "strict"))
39504 strict = true;
39505 cp_lexer_consume_token (parser->lexer);
39506 cp_lexer_consume_token (parser->lexer);
39510 t = cp_parser_assignment_expression (parser);
39512 if (t == error_mark_node
39513 || !parens.require_close (parser))
39514 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39515 /*or_comma=*/false,
39516 /*consume_paren=*/true);
39518 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
39519 "num_tasks", location);
39521 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
39522 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
39523 OMP_CLAUSE_NUM_TASKS_STRICT (c) = strict;
39524 OMP_CLAUSE_CHAIN (c) = list;
39526 return c;
39529 /* OpenMP 4.5:
39530 grainsize ( expression )
39532 OpenMP 5.1:
39533 grainsize ( strict : expression ) */
39535 static tree
39536 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
39537 location_t location)
39539 tree t, c;
39541 matching_parens parens;
39542 if (!parens.require_open (parser))
39543 return list;
39545 bool strict = false;
39546 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
39547 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
39549 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39550 if (!strcmp (IDENTIFIER_POINTER (id), "strict"))
39552 strict = true;
39553 cp_lexer_consume_token (parser->lexer);
39554 cp_lexer_consume_token (parser->lexer);
39558 t = cp_parser_assignment_expression (parser);
39560 if (t == error_mark_node
39561 || !parens.require_close (parser))
39562 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39563 /*or_comma=*/false,
39564 /*consume_paren=*/true);
39566 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
39567 "grainsize", location);
39569 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
39570 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
39571 OMP_CLAUSE_GRAINSIZE_STRICT (c) = strict;
39572 OMP_CLAUSE_CHAIN (c) = list;
39574 return c;
39577 /* OpenMP 4.5:
39578 priority ( expression ) */
39580 static tree
39581 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
39582 location_t location)
39584 tree t, c;
39586 matching_parens parens;
39587 if (!parens.require_open (parser))
39588 return list;
39590 t = cp_parser_assignment_expression (parser);
39592 if (t == error_mark_node
39593 || !parens.require_close (parser))
39594 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39595 /*or_comma=*/false,
39596 /*consume_paren=*/true);
39598 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
39599 "priority", location);
39601 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
39602 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
39603 OMP_CLAUSE_CHAIN (c) = list;
39605 return c;
39608 /* OpenMP 4.5:
39609 hint ( expression ) */
39611 static tree
39612 cp_parser_omp_clause_hint (cp_parser *parser, tree list, location_t location)
39614 tree t, c;
39616 matching_parens parens;
39617 if (!parens.require_open (parser))
39618 return list;
39620 t = cp_parser_assignment_expression (parser);
39622 if (t != error_mark_node)
39624 t = fold_non_dependent_expr (t);
39625 if (!value_dependent_expression_p (t)
39626 && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
39627 || !tree_fits_shwi_p (t)
39628 || tree_int_cst_sgn (t) == -1))
39629 error_at (location, "expected constant integer expression with "
39630 "valid sync-hint value");
39632 if (t == error_mark_node
39633 || !parens.require_close (parser))
39634 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39635 /*or_comma=*/false,
39636 /*consume_paren=*/true);
39637 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
39639 c = build_omp_clause (location, OMP_CLAUSE_HINT);
39640 OMP_CLAUSE_HINT_EXPR (c) = t;
39641 OMP_CLAUSE_CHAIN (c) = list;
39643 return c;
39646 /* OpenMP 5.1:
39647 filter ( integer-expression ) */
39649 static tree
39650 cp_parser_omp_clause_filter (cp_parser *parser, tree list, location_t location)
39652 tree t, c;
39654 matching_parens parens;
39655 if (!parens.require_open (parser))
39656 return list;
39658 t = cp_parser_assignment_expression (parser);
39660 if (t == error_mark_node
39661 || !parens.require_close (parser))
39662 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39663 /*or_comma=*/false,
39664 /*consume_paren=*/true);
39665 check_no_duplicate_clause (list, OMP_CLAUSE_FILTER, "filter", location);
39667 c = build_omp_clause (location, OMP_CLAUSE_FILTER);
39668 OMP_CLAUSE_FILTER_EXPR (c) = t;
39669 OMP_CLAUSE_CHAIN (c) = list;
39671 return c;
39674 /* OpenMP 4.5:
39675 defaultmap ( tofrom : scalar )
39677 OpenMP 5.0:
39678 defaultmap ( implicit-behavior [ : variable-category ] ) */
39680 static tree
39681 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
39682 location_t location)
39684 tree c, id;
39685 const char *p;
39686 enum omp_clause_defaultmap_kind behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
39687 enum omp_clause_defaultmap_kind category
39688 = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED;
39690 matching_parens parens;
39691 if (!parens.require_open (parser))
39692 return list;
39694 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
39695 p = "default";
39696 else if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39698 invalid_behavior:
39699 cp_parser_error (parser, "expected %<alloc%>, %<to%>, %<from%>, "
39700 "%<tofrom%>, %<firstprivate%>, %<none%> "
39701 "or %<default%>");
39702 goto out_err;
39704 else
39706 id = cp_lexer_peek_token (parser->lexer)->u.value;
39707 p = IDENTIFIER_POINTER (id);
39710 switch (p[0])
39712 case 'a':
39713 if (strcmp ("alloc", p) == 0)
39714 behavior = OMP_CLAUSE_DEFAULTMAP_ALLOC;
39715 else
39716 goto invalid_behavior;
39717 break;
39719 case 'd':
39720 if (strcmp ("default", p) == 0)
39721 behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
39722 else
39723 goto invalid_behavior;
39724 break;
39726 case 'f':
39727 if (strcmp ("firstprivate", p) == 0)
39728 behavior = OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE;
39729 else if (strcmp ("from", p) == 0)
39730 behavior = OMP_CLAUSE_DEFAULTMAP_FROM;
39731 else
39732 goto invalid_behavior;
39733 break;
39735 case 'n':
39736 if (strcmp ("none", p) == 0)
39737 behavior = OMP_CLAUSE_DEFAULTMAP_NONE;
39738 else
39739 goto invalid_behavior;
39740 break;
39742 case 'p':
39743 if (strcmp ("present", p) == 0)
39744 behavior = OMP_CLAUSE_DEFAULTMAP_PRESENT;
39745 else
39746 goto invalid_behavior;
39747 break;
39749 case 't':
39750 if (strcmp ("tofrom", p) == 0)
39751 behavior = OMP_CLAUSE_DEFAULTMAP_TOFROM;
39752 else if (strcmp ("to", p) == 0)
39753 behavior = OMP_CLAUSE_DEFAULTMAP_TO;
39754 else
39755 goto invalid_behavior;
39756 break;
39758 default:
39759 goto invalid_behavior;
39761 cp_lexer_consume_token (parser->lexer);
39763 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
39765 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
39766 goto out_err;
39768 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39770 invalid_category:
39771 cp_parser_error (parser, "expected %<scalar%>, %<aggregate%>, "
39772 "%<all%>");
39773 goto out_err;
39775 id = cp_lexer_peek_token (parser->lexer)->u.value;
39776 p = IDENTIFIER_POINTER (id);
39778 switch (p[0])
39780 case 'a':
39781 if (strcmp ("aggregate", p) == 0)
39782 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE;
39783 else if (strcmp ("all", p) == 0)
39784 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_ALL;
39785 else
39786 goto invalid_category;
39787 break;
39789 case 'p':
39790 if (strcmp ("pointer", p) == 0)
39791 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER;
39792 else
39793 goto invalid_category;
39794 break;
39796 case 's':
39797 if (strcmp ("scalar", p) == 0)
39798 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR;
39799 else
39800 goto invalid_category;
39801 break;
39803 default:
39804 goto invalid_category;
39807 cp_lexer_consume_token (parser->lexer);
39809 if (!parens.require_close (parser))
39810 goto out_err;
39812 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
39813 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEFAULTMAP
39814 && (category == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
39815 || category == OMP_CLAUSE_DEFAULTMAP_CATEGORY_ALL
39816 || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c) == category
39817 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c)
39818 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)
39819 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c)
39820 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_ALL)))
39822 enum omp_clause_defaultmap_kind cat = category;
39823 location_t loc = OMP_CLAUSE_LOCATION (c);
39824 if (cat == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
39825 || (cat == OMP_CLAUSE_DEFAULTMAP_CATEGORY_ALL
39826 && (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c)
39827 != OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)))
39828 cat = OMP_CLAUSE_DEFAULTMAP_CATEGORY (c);
39829 p = NULL;
39830 switch (cat)
39832 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED:
39833 p = NULL;
39834 break;
39835 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_ALL:
39836 p = "all";
39837 break;
39838 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE:
39839 p = "aggregate";
39840 break;
39841 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER:
39842 p = "pointer";
39843 break;
39844 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR:
39845 p = "scalar";
39846 break;
39847 default:
39848 gcc_unreachable ();
39850 if (p)
39851 error_at (loc, "too many %<defaultmap%> clauses with %qs category",
39853 else
39854 error_at (loc, "too many %<defaultmap%> clauses with unspecified "
39855 "category");
39856 break;
39859 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
39860 OMP_CLAUSE_DEFAULTMAP_SET_KIND (c, behavior, category);
39861 OMP_CLAUSE_CHAIN (c) = list;
39862 return c;
39864 out_err:
39865 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39866 /*or_comma=*/false,
39867 /*consume_paren=*/true);
39868 return list;
39871 /* OpenMP 5.0:
39872 order ( concurrent )
39874 OpenMP 5.1:
39875 order ( order-modifier : concurrent )
39877 order-modifier:
39878 reproducible
39879 unconstrained */
39881 static tree
39882 cp_parser_omp_clause_order (cp_parser *parser, tree list, location_t location)
39884 tree c, id;
39885 const char *p;
39886 bool unconstrained = false;
39887 bool reproducible = false;
39889 matching_parens parens;
39890 if (!parens.require_open (parser))
39891 return list;
39893 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
39894 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
39896 id = cp_lexer_peek_token (parser->lexer)->u.value;
39897 p = IDENTIFIER_POINTER (id);
39898 if (strcmp (p, "unconstrained") == 0)
39899 unconstrained = true;
39900 else if (strcmp (p, "reproducible") == 0)
39901 reproducible = true;
39902 else
39904 cp_parser_error (parser, "expected %<reproducible%> or "
39905 "%<unconstrained%>");
39906 goto out_err;
39908 cp_lexer_consume_token (parser->lexer);
39909 cp_lexer_consume_token (parser->lexer);
39911 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39913 cp_parser_error (parser, "expected %<concurrent%>");
39914 goto out_err;
39916 else
39918 id = cp_lexer_peek_token (parser->lexer)->u.value;
39919 p = IDENTIFIER_POINTER (id);
39921 if (strcmp (p, "concurrent") != 0)
39923 cp_parser_error (parser, "expected %<concurrent%>");
39924 goto out_err;
39926 cp_lexer_consume_token (parser->lexer);
39927 if (!parens.require_close (parser))
39928 goto out_err;
39930 check_no_duplicate_clause (list, OMP_CLAUSE_ORDER, "order", location);
39931 c = build_omp_clause (location, OMP_CLAUSE_ORDER);
39932 OMP_CLAUSE_ORDER_UNCONSTRAINED (c) = unconstrained;
39933 OMP_CLAUSE_ORDER_REPRODUCIBLE (c) = reproducible;
39934 OMP_CLAUSE_CHAIN (c) = list;
39935 return c;
39937 out_err:
39938 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39939 /*or_comma=*/false,
39940 /*consume_paren=*/true);
39941 return list;
39944 /* OpenMP 5.0:
39945 bind ( teams | parallel | thread ) */
39947 static tree
39948 cp_parser_omp_clause_bind (cp_parser *parser, tree list,
39949 location_t location)
39951 tree c;
39952 const char *p;
39953 enum omp_clause_bind_kind kind = OMP_CLAUSE_BIND_THREAD;
39955 matching_parens parens;
39956 if (!parens.require_open (parser))
39957 return list;
39959 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39961 invalid:
39962 cp_parser_error (parser,
39963 "expected %<teams%>, %<parallel%> or %<thread%>");
39964 goto out_err;
39966 else
39968 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39969 p = IDENTIFIER_POINTER (id);
39971 if (strcmp (p, "teams") == 0)
39972 kind = OMP_CLAUSE_BIND_TEAMS;
39973 else if (strcmp (p, "parallel") == 0)
39974 kind = OMP_CLAUSE_BIND_PARALLEL;
39975 else if (strcmp (p, "thread") != 0)
39976 goto invalid;
39977 cp_lexer_consume_token (parser->lexer);
39978 if (!parens.require_close (parser))
39979 goto out_err;
39981 /* check_no_duplicate_clause (list, OMP_CLAUSE_BIND, "bind", location); */
39982 c = build_omp_clause (location, OMP_CLAUSE_BIND);
39983 OMP_CLAUSE_BIND_KIND (c) = kind;
39984 OMP_CLAUSE_CHAIN (c) = list;
39985 return c;
39987 out_err:
39988 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39989 /*or_comma=*/false,
39990 /*consume_paren=*/true);
39991 return list;
39994 /* OpenMP 2.5:
39995 ordered
39997 OpenMP 4.5:
39998 ordered ( constant-expression ) */
40000 static tree
40001 cp_parser_omp_clause_ordered (cp_parser *parser,
40002 tree list, location_t location)
40004 tree c, num = NULL_TREE;
40005 HOST_WIDE_INT n;
40007 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
40008 "ordered", location);
40010 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
40012 matching_parens parens;
40013 parens.consume_open (parser);
40015 num = cp_parser_constant_expression (parser);
40017 if (!parens.require_close (parser))
40018 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40019 /*or_comma=*/false,
40020 /*consume_paren=*/true);
40022 if (num == error_mark_node)
40023 return list;
40024 num = fold_non_dependent_expr (num);
40025 if (!tree_fits_shwi_p (num)
40026 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
40027 || (n = tree_to_shwi (num)) <= 0
40028 || (int) n != n)
40030 error_at (location,
40031 "ordered argument needs positive constant integer "
40032 "expression");
40033 return list;
40037 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
40038 OMP_CLAUSE_ORDERED_EXPR (c) = num;
40039 OMP_CLAUSE_CHAIN (c) = list;
40040 return c;
40043 /* OpenMP 2.5:
40044 reduction ( reduction-operator : variable-list )
40046 reduction-operator:
40047 One of: + * - & ^ | && ||
40049 OpenMP 3.1:
40051 reduction-operator:
40052 One of: + * - & ^ | && || min max
40054 OpenMP 4.0:
40056 reduction-operator:
40057 One of: + * - & ^ | && ||
40058 id-expression
40060 OpenMP 5.0:
40061 reduction ( reduction-modifier, reduction-operator : variable-list )
40062 in_reduction ( reduction-operator : variable-list )
40063 task_reduction ( reduction-operator : variable-list ) */
40065 static tree
40066 cp_parser_omp_clause_reduction (cp_parser *parser, enum omp_clause_code kind,
40067 bool is_omp, tree list)
40069 enum tree_code code = ERROR_MARK;
40070 tree nlist, c, id = NULL_TREE;
40071 bool task = false;
40072 bool inscan = false;
40074 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
40075 return list;
40077 if (kind == OMP_CLAUSE_REDUCTION && is_omp)
40079 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT)
40080 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
40082 cp_lexer_consume_token (parser->lexer);
40083 cp_lexer_consume_token (parser->lexer);
40085 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
40086 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
40088 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40089 const char *p = IDENTIFIER_POINTER (id);
40090 if (strcmp (p, "task") == 0)
40091 task = true;
40092 else if (strcmp (p, "inscan") == 0)
40093 inscan = true;
40094 if (task || inscan)
40096 cp_lexer_consume_token (parser->lexer);
40097 cp_lexer_consume_token (parser->lexer);
40102 switch (cp_lexer_peek_token (parser->lexer)->type)
40104 case CPP_PLUS: code = PLUS_EXPR; break;
40105 case CPP_MULT: code = MULT_EXPR; break;
40106 case CPP_MINUS: code = MINUS_EXPR; break;
40107 case CPP_AND: code = BIT_AND_EXPR; break;
40108 case CPP_XOR: code = BIT_XOR_EXPR; break;
40109 case CPP_OR: code = BIT_IOR_EXPR; break;
40110 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
40111 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
40112 default: break;
40115 if (code != ERROR_MARK)
40116 cp_lexer_consume_token (parser->lexer);
40117 else
40119 bool saved_colon_corrects_to_scope_p;
40120 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
40121 parser->colon_corrects_to_scope_p = false;
40122 id = cp_parser_id_expression (parser, /*template_p=*/false,
40123 /*check_dependency_p=*/true,
40124 /*template_p=*/NULL,
40125 /*declarator_p=*/false,
40126 /*optional_p=*/false);
40127 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
40128 if (identifier_p (id))
40130 const char *p = IDENTIFIER_POINTER (id);
40132 if (strcmp (p, "min") == 0)
40133 code = MIN_EXPR;
40134 else if (strcmp (p, "max") == 0)
40135 code = MAX_EXPR;
40136 else if (id == ovl_op_identifier (false, PLUS_EXPR))
40137 code = PLUS_EXPR;
40138 else if (id == ovl_op_identifier (false, MULT_EXPR))
40139 code = MULT_EXPR;
40140 else if (id == ovl_op_identifier (false, MINUS_EXPR))
40141 code = MINUS_EXPR;
40142 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
40143 code = BIT_AND_EXPR;
40144 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
40145 code = BIT_IOR_EXPR;
40146 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
40147 code = BIT_XOR_EXPR;
40148 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
40149 code = TRUTH_ANDIF_EXPR;
40150 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
40151 code = TRUTH_ORIF_EXPR;
40152 id = omp_reduction_id (code, id, NULL_TREE);
40153 tree scope = parser->scope;
40154 if (scope)
40155 id = build_qualified_name (NULL_TREE, scope, id, false);
40156 parser->scope = NULL_TREE;
40157 parser->qualifying_scope = NULL_TREE;
40158 parser->object_scope = NULL_TREE;
40160 else
40162 error ("invalid reduction-identifier");
40163 resync_fail:
40164 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40165 /*or_comma=*/false,
40166 /*consume_paren=*/true);
40167 return list;
40171 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
40172 goto resync_fail;
40174 nlist = cp_parser_omp_var_list_no_open (parser, kind, list,
40175 NULL);
40176 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
40178 OMP_CLAUSE_REDUCTION_CODE (c) = code;
40179 if (task)
40180 OMP_CLAUSE_REDUCTION_TASK (c) = 1;
40181 else if (inscan)
40182 OMP_CLAUSE_REDUCTION_INSCAN (c) = 1;
40183 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
40186 return nlist;
40189 /* OpenMP 2.5:
40190 schedule ( schedule-kind )
40191 schedule ( schedule-kind , expression )
40193 schedule-kind:
40194 static | dynamic | guided | runtime | auto
40196 OpenMP 4.5:
40197 schedule ( schedule-modifier : schedule-kind )
40198 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
40200 schedule-modifier:
40201 simd
40202 monotonic
40203 nonmonotonic */
40205 static tree
40206 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
40208 tree c, t;
40209 int modifiers = 0, nmodifiers = 0;
40211 matching_parens parens;
40212 if (!parens.require_open (parser))
40213 return list;
40215 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
40217 location_t comma = UNKNOWN_LOCATION;
40218 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40220 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40221 const char *p = IDENTIFIER_POINTER (id);
40222 if (strcmp ("simd", p) == 0)
40223 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
40224 else if (strcmp ("monotonic", p) == 0)
40225 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
40226 else if (strcmp ("nonmonotonic", p) == 0)
40227 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
40228 else
40229 break;
40230 comma = UNKNOWN_LOCATION;
40231 cp_lexer_consume_token (parser->lexer);
40232 if (nmodifiers++ == 0
40233 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
40235 comma = cp_lexer_peek_token (parser->lexer)->location;
40236 cp_lexer_consume_token (parser->lexer);
40238 else
40240 cp_parser_require (parser, CPP_COLON, RT_COLON);
40241 break;
40244 if (comma != UNKNOWN_LOCATION)
40245 error_at (comma, "expected %<:%>");
40247 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40249 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40250 const char *p = IDENTIFIER_POINTER (id);
40252 switch (p[0])
40254 case 'd':
40255 if (strcmp ("dynamic", p) != 0)
40256 goto invalid_kind;
40257 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
40258 break;
40260 case 'g':
40261 if (strcmp ("guided", p) != 0)
40262 goto invalid_kind;
40263 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
40264 break;
40266 case 'r':
40267 if (strcmp ("runtime", p) != 0)
40268 goto invalid_kind;
40269 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
40270 break;
40272 default:
40273 goto invalid_kind;
40276 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
40277 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
40278 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
40279 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
40280 else
40281 goto invalid_kind;
40282 cp_lexer_consume_token (parser->lexer);
40284 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
40285 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
40286 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
40287 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
40289 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
40290 "specified");
40291 modifiers = 0;
40294 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
40296 cp_token *token;
40297 cp_lexer_consume_token (parser->lexer);
40299 token = cp_lexer_peek_token (parser->lexer);
40300 t = cp_parser_assignment_expression (parser);
40302 if (t == error_mark_node)
40303 goto resync_fail;
40304 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
40305 error_at (token->location, "schedule %<runtime%> does not take "
40306 "a %<chunk_size%> parameter");
40307 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
40308 error_at (token->location, "schedule %<auto%> does not take "
40309 "a %<chunk_size%> parameter");
40310 else
40311 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
40313 if (!parens.require_close (parser))
40314 goto resync_fail;
40316 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
40317 goto resync_fail;
40319 OMP_CLAUSE_SCHEDULE_KIND (c)
40320 = (enum omp_clause_schedule_kind)
40321 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
40323 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
40324 OMP_CLAUSE_CHAIN (c) = list;
40325 return c;
40327 invalid_kind:
40328 cp_parser_error (parser, "invalid schedule kind");
40329 resync_fail:
40330 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40331 /*or_comma=*/false,
40332 /*consume_paren=*/true);
40333 return list;
40336 /* OpenMP 3.0:
40337 untied */
40339 static tree
40340 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
40341 tree list, location_t location)
40343 tree c;
40345 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
40347 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
40348 OMP_CLAUSE_CHAIN (c) = list;
40349 return c;
40352 /* OpenMP 4.0:
40353 inbranch
40354 notinbranch */
40356 static tree
40357 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
40358 tree list, location_t location)
40360 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
40361 tree c = build_omp_clause (location, code);
40362 OMP_CLAUSE_CHAIN (c) = list;
40363 return c;
40366 /* OpenMP 4.0:
40367 parallel
40369 sections
40370 taskgroup */
40372 static tree
40373 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
40374 enum omp_clause_code code,
40375 tree list, location_t location)
40377 tree c = build_omp_clause (location, code);
40378 OMP_CLAUSE_CHAIN (c) = list;
40379 return c;
40382 /* OpenMP 4.5:
40383 nogroup */
40385 static tree
40386 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
40387 tree list, location_t location)
40389 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
40390 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
40391 OMP_CLAUSE_CHAIN (c) = list;
40392 return c;
40395 /* OpenMP 4.5:
40396 simd
40397 threads */
40399 static tree
40400 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
40401 enum omp_clause_code code,
40402 tree list, location_t location)
40404 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
40405 tree c = build_omp_clause (location, code);
40406 OMP_CLAUSE_CHAIN (c) = list;
40407 return c;
40410 /* OpenMP 4.0:
40411 num_teams ( expression )
40413 OpenMP 5.1:
40414 num_teams ( expression : expression ) */
40416 static tree
40417 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
40418 location_t location)
40420 tree upper, lower = NULL_TREE, c;
40422 matching_parens parens;
40423 if (!parens.require_open (parser))
40424 return list;
40426 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
40427 parser->colon_corrects_to_scope_p = false;
40428 upper = cp_parser_assignment_expression (parser);
40429 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
40431 if (upper != error_mark_node
40432 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
40434 lower = upper;
40435 cp_lexer_consume_token (parser->lexer);
40436 upper = cp_parser_assignment_expression (parser);
40439 if (upper == error_mark_node
40440 || !parens.require_close (parser))
40441 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40442 /*or_comma=*/false,
40443 /*consume_paren=*/true);
40445 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
40446 "num_teams", location);
40448 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
40449 OMP_CLAUSE_NUM_TEAMS_UPPER_EXPR (c) = upper;
40450 OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c) = lower;
40451 OMP_CLAUSE_CHAIN (c) = list;
40453 return c;
40456 /* OpenMP 4.0:
40457 thread_limit ( expression ) */
40459 static tree
40460 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
40461 location_t location)
40463 tree t, c;
40465 matching_parens parens;
40466 if (!parens.require_open (parser))
40467 return list;
40469 t = cp_parser_assignment_expression (parser);
40471 if (t == error_mark_node
40472 || !parens.require_close (parser))
40473 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40474 /*or_comma=*/false,
40475 /*consume_paren=*/true);
40477 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
40478 "thread_limit", location);
40480 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
40481 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
40482 OMP_CLAUSE_CHAIN (c) = list;
40484 return c;
40487 /* OpenMP 4.0:
40488 aligned ( variable-list )
40489 aligned ( variable-list : constant-expression ) */
40491 static tree
40492 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
40494 tree nlist, c, alignment = NULL_TREE;
40495 bool colon;
40497 matching_parens parens;
40498 if (!parens.require_open (parser))
40499 return list;
40501 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
40502 &colon);
40504 if (colon)
40506 alignment = cp_parser_constant_expression (parser);
40508 if (!parens.require_close (parser))
40509 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40510 /*or_comma=*/false,
40511 /*consume_paren=*/true);
40513 if (alignment == error_mark_node)
40514 alignment = NULL_TREE;
40517 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
40518 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
40520 return nlist;
40523 /* OpenMP 5.0:
40524 allocate ( variable-list )
40525 allocate ( expression : variable-list )
40527 OpenMP 5.1:
40528 allocate ( allocator-modifier : variable-list )
40529 allocate ( allocator-modifier , allocator-modifier : variable-list )
40531 allocator-modifier:
40532 allocator ( expression )
40533 align ( expression ) */
40535 static tree
40536 cp_parser_omp_clause_allocate (cp_parser *parser, tree list)
40538 tree nlist, c, allocator = NULL_TREE, align = NULL_TREE;
40539 bool colon, has_modifiers = false;
40541 matching_parens parens;
40542 if (!parens.require_open (parser))
40543 return list;
40545 cp_parser_parse_tentatively (parser);
40546 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
40547 parser->colon_corrects_to_scope_p = false;
40548 for (int mod = 0; mod < 2; mod++)
40549 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
40550 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
40552 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40553 const char *p = IDENTIFIER_POINTER (id);
40554 if (strcmp (p, "allocator") != 0 && strcmp (p, "align") != 0)
40555 break;
40556 cp_lexer_consume_token (parser->lexer);
40557 matching_parens parens2;
40558 if (!parens2.require_open (parser))
40559 break;
40560 if (strcmp (p, "allocator") == 0)
40562 if (allocator != NULL_TREE)
40563 break;
40564 allocator = cp_parser_assignment_expression (parser);
40566 else
40568 if (align != NULL_TREE)
40569 break;
40570 align = cp_parser_assignment_expression (parser);
40572 if (!parens2.require_close (parser))
40573 break;
40574 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
40576 has_modifiers = true;
40577 break;
40579 if (mod != 0 || cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
40580 break;
40581 cp_lexer_consume_token (parser->lexer);
40583 else
40584 break;
40585 if (!has_modifiers)
40587 cp_parser_abort_tentative_parse (parser);
40588 align = NULL_TREE;
40589 allocator = NULL_TREE;
40590 cp_parser_parse_tentatively (parser);
40591 allocator = cp_parser_assignment_expression (parser);
40593 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
40594 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
40596 cp_parser_parse_definitely (parser);
40597 cp_lexer_consume_token (parser->lexer);
40598 if (allocator == error_mark_node)
40599 allocator = NULL_TREE;
40600 if (align == error_mark_node)
40601 align = NULL_TREE;
40603 else
40605 cp_parser_abort_tentative_parse (parser);
40606 allocator = NULL_TREE;
40607 align = NULL_TREE;
40610 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALLOCATE, list,
40611 &colon);
40613 if (allocator || align)
40614 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
40616 OMP_CLAUSE_ALLOCATE_ALLOCATOR (c) = allocator;
40617 OMP_CLAUSE_ALLOCATE_ALIGN (c) = align;
40620 return nlist;
40623 /* OpenMP 2.5:
40624 lastprivate ( variable-list )
40626 OpenMP 5.0:
40627 lastprivate ( [ lastprivate-modifier : ] variable-list ) */
40629 static tree
40630 cp_parser_omp_clause_lastprivate (cp_parser *parser, tree list)
40632 bool conditional = false;
40634 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
40635 return list;
40637 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
40638 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
40640 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40641 const char *p = IDENTIFIER_POINTER (id);
40643 if (strcmp ("conditional", p) == 0)
40645 conditional = true;
40646 cp_lexer_consume_token (parser->lexer);
40647 cp_lexer_consume_token (parser->lexer);
40651 tree nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LASTPRIVATE,
40652 list, NULL);
40654 if (conditional)
40655 for (tree c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
40656 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 1;
40657 return nlist;
40660 /* OpenMP 4.0:
40661 linear ( variable-list )
40662 linear ( variable-list : expression )
40664 OpenMP 4.5:
40665 linear ( modifier ( variable-list ) )
40666 linear ( modifier ( variable-list ) : expression )
40668 modifier:
40671 uval
40673 OpenMP 5.2:
40674 linear ( variable-list : modifiers-list )
40676 modifiers:
40679 uval
40680 step ( expression ) */
40682 static tree
40683 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
40684 bool declare_simd)
40686 tree nlist, c, step = integer_one_node;
40687 bool colon;
40688 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
40689 bool old_linear_modifier = false;
40691 matching_parens parens;
40692 if (!parens.require_open (parser))
40693 return list;
40695 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40697 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40698 const char *p = IDENTIFIER_POINTER (id);
40700 if (strcmp ("ref", p) == 0)
40701 kind = OMP_CLAUSE_LINEAR_REF;
40702 else if (strcmp ("val", p) == 0)
40703 kind = OMP_CLAUSE_LINEAR_VAL;
40704 else if (strcmp ("uval", p) == 0)
40705 kind = OMP_CLAUSE_LINEAR_UVAL;
40706 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
40708 cp_lexer_consume_token (parser->lexer);
40709 old_linear_modifier = true;
40711 else
40712 kind = OMP_CLAUSE_LINEAR_DEFAULT;
40715 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
40716 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
40717 &colon);
40718 else
40720 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
40721 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
40722 if (colon)
40723 cp_parser_require (parser, CPP_COLON, RT_COLON);
40724 else if (!parens.require_close (parser))
40725 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40726 /*or_comma=*/false,
40727 /*consume_paren=*/true);
40730 if (colon)
40732 bool has_modifiers = false;
40733 if (kind == OMP_CLAUSE_LINEAR_DEFAULT
40734 && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40736 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40737 const char *p = IDENTIFIER_POINTER (id);
40738 size_t pos = 0;
40739 if (strcmp ("ref", p) == 0
40740 || strcmp ("val", p) == 0
40741 || strcmp ("uval", p) == 0)
40742 pos = 2;
40743 else if (strcmp ("step", p) == 0
40744 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
40746 pos = cp_parser_skip_balanced_tokens (parser, 2);
40747 if (pos == 2)
40748 pos = 0;
40750 if (pos != 0
40751 && (cp_lexer_nth_token_is (parser->lexer, pos, CPP_COMMA)
40752 || cp_lexer_nth_token_is (parser->lexer, pos,
40753 CPP_CLOSE_PAREN)))
40754 has_modifiers = true;
40757 step = NULL_TREE;
40758 if (has_modifiers)
40760 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40762 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40763 const char *p = IDENTIFIER_POINTER (id);
40764 enum omp_clause_linear_kind nkind = OMP_CLAUSE_LINEAR_DEFAULT;
40765 if (strcmp ("ref", p) == 0)
40766 nkind = OMP_CLAUSE_LINEAR_REF;
40767 else if (strcmp ("val", p) == 0)
40768 nkind = OMP_CLAUSE_LINEAR_VAL;
40769 else if (strcmp ("uval", p) == 0)
40770 nkind = OMP_CLAUSE_LINEAR_UVAL;
40771 if (nkind != OMP_CLAUSE_LINEAR_DEFAULT)
40773 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
40774 error_at (cp_lexer_peek_token (parser->lexer)->location,
40775 "multiple linear modifiers");
40776 kind = nkind;
40777 cp_lexer_consume_token (parser->lexer);
40779 else if (strcmp ("step", p) == 0)
40781 location_t step_loc
40782 = cp_lexer_peek_token (parser->lexer)->location;
40783 cp_lexer_consume_token (parser->lexer);
40784 matching_parens parens2;
40785 if (parens2.require_open (parser))
40787 if (step)
40788 error_at (step_loc, "multiple %<step%> modifiers");
40789 if (declare_simd
40790 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
40791 && cp_lexer_nth_token_is (parser->lexer, 2,
40792 CPP_CLOSE_PAREN))
40794 cp_token *token
40795 = cp_lexer_peek_token (parser->lexer);
40796 location_t tok_loc = token->location;
40797 cp_parser_parse_tentatively (parser);
40798 step = cp_parser_id_expression (parser, false, true,
40799 NULL, false, false);
40800 if (step != error_mark_node)
40801 step = cp_parser_lookup_name_simple (parser, step,
40802 tok_loc);
40803 if (step == error_mark_node)
40805 step = NULL_TREE;
40806 cp_parser_abort_tentative_parse (parser);
40808 else if (!cp_parser_parse_definitely (parser))
40809 step = NULL_TREE;
40811 if (!step)
40812 step = cp_parser_assignment_expression (parser);
40813 if (!parens2.require_close (parser))
40814 cp_parser_skip_to_closing_parenthesis (parser, true,
40815 false, true);
40817 else
40818 break;
40820 else
40821 break;
40822 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
40824 cp_lexer_consume_token (parser->lexer);
40825 continue;
40827 break;
40829 if (!step)
40830 step = integer_one_node;
40832 else if (declare_simd
40833 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
40834 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
40836 cp_token *token = cp_lexer_peek_token (parser->lexer);
40837 cp_parser_parse_tentatively (parser);
40838 step = cp_parser_id_expression (parser, /*template_p=*/false,
40839 /*check_dependency_p=*/true,
40840 /*template_p=*/NULL,
40841 /*declarator_p=*/false,
40842 /*optional_p=*/false);
40843 if (step != error_mark_node)
40844 step = cp_parser_lookup_name_simple (parser, step, token->location);
40845 if (step == error_mark_node)
40847 step = NULL_TREE;
40848 cp_parser_abort_tentative_parse (parser);
40850 else if (!cp_parser_parse_definitely (parser))
40851 step = NULL_TREE;
40853 if (!step)
40854 step = cp_parser_assignment_expression (parser);
40856 if (!parens.require_close (parser))
40857 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40858 /*or_comma=*/false,
40859 /*consume_paren=*/true);
40861 if (step == error_mark_node)
40862 return list;
40865 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
40867 OMP_CLAUSE_LINEAR_STEP (c) = step;
40868 OMP_CLAUSE_LINEAR_KIND (c) = kind;
40869 OMP_CLAUSE_LINEAR_OLD_LINEAR_MODIFIER (c) = old_linear_modifier;
40872 return nlist;
40875 /* OpenMP 4.0:
40876 safelen ( constant-expression ) */
40878 static tree
40879 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
40880 location_t location)
40882 tree t, c;
40884 matching_parens parens;
40885 if (!parens.require_open (parser))
40886 return list;
40888 t = cp_parser_constant_expression (parser);
40890 if (t == error_mark_node
40891 || !parens.require_close (parser))
40892 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40893 /*or_comma=*/false,
40894 /*consume_paren=*/true);
40896 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
40898 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
40899 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
40900 OMP_CLAUSE_CHAIN (c) = list;
40902 return c;
40905 /* OpenMP 4.0:
40906 simdlen ( constant-expression ) */
40908 static tree
40909 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
40910 location_t location)
40912 tree t, c;
40914 matching_parens parens;
40915 if (!parens.require_open (parser))
40916 return list;
40918 t = cp_parser_constant_expression (parser);
40920 if (t == error_mark_node
40921 || !parens.require_close (parser))
40922 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40923 /*or_comma=*/false,
40924 /*consume_paren=*/true);
40926 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
40928 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
40929 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
40930 OMP_CLAUSE_CHAIN (c) = list;
40932 return c;
40935 /* OpenMP 4.5:
40936 vec:
40937 identifier [+/- integer]
40938 vec , identifier [+/- integer]
40941 static tree
40942 cp_parser_omp_clause_doacross_sink (cp_parser *parser, location_t clause_loc,
40943 tree list, bool depend_p)
40945 tree vec = NULL;
40947 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
40949 cp_parser_error (parser, "expected identifier");
40950 return list;
40953 if (!depend_p)
40955 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40956 if (strcmp (IDENTIFIER_POINTER (id), "omp_cur_iteration") == 0
40957 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_MINUS)
40958 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_NUMBER)
40959 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_PAREN))
40961 tree val = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
40962 if (integer_onep (val))
40964 cp_lexer_consume_token (parser->lexer);
40965 cp_lexer_consume_token (parser->lexer);
40966 cp_lexer_consume_token (parser->lexer);
40967 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DOACROSS);
40968 OMP_CLAUSE_DOACROSS_KIND (u) = OMP_CLAUSE_DOACROSS_SINK;
40969 OMP_CLAUSE_CHAIN (u) = list;
40970 return u;
40975 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40977 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
40978 tree t, identifier = cp_parser_identifier (parser);
40979 tree addend = NULL;
40981 if (identifier == error_mark_node)
40982 t = error_mark_node;
40983 else
40985 t = cp_parser_lookup_name_simple
40986 (parser, identifier,
40987 cp_lexer_peek_token (parser->lexer)->location);
40988 if (t == error_mark_node)
40989 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
40990 id_loc);
40993 bool neg = false;
40994 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
40995 neg = true;
40996 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
40998 addend = integer_zero_node;
40999 goto add_to_vector;
41001 cp_lexer_consume_token (parser->lexer);
41003 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
41005 cp_parser_error (parser, "expected integer");
41006 return list;
41009 addend = cp_lexer_peek_token (parser->lexer)->u.value;
41010 if (TREE_CODE (addend) != INTEGER_CST)
41012 cp_parser_error (parser, "expected integer");
41013 return list;
41015 cp_lexer_consume_token (parser->lexer);
41017 add_to_vector:
41018 if (t != error_mark_node)
41020 vec = tree_cons (addend, t, vec);
41021 if (neg)
41022 OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (vec) = 1;
41025 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
41026 || !cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
41027 break;
41029 cp_lexer_consume_token (parser->lexer);
41032 if (vec)
41034 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DOACROSS);
41035 OMP_CLAUSE_DOACROSS_KIND (u) = OMP_CLAUSE_DOACROSS_SINK;
41036 OMP_CLAUSE_DOACROSS_DEPEND (u) = depend_p;
41037 OMP_CLAUSE_DECL (u) = nreverse (vec);
41038 OMP_CLAUSE_CHAIN (u) = list;
41039 return u;
41041 return list;
41044 /* OpenMP 5.0:
41045 detach ( event-handle ) */
41047 static tree
41048 cp_parser_omp_clause_detach (cp_parser *parser, tree list)
41050 matching_parens parens;
41052 if (!parens.require_open (parser))
41053 return list;
41055 cp_token *token;
41056 tree name, decl;
41058 token = cp_lexer_peek_token (parser->lexer);
41059 name = cp_parser_id_expression (parser, /*template_p=*/false,
41060 /*check_dependency_p=*/true,
41061 /*template_p=*/NULL,
41062 /*declarator_p=*/false,
41063 /*optional_p=*/false);
41064 if (name == error_mark_node)
41065 decl = error_mark_node;
41066 else
41068 if (identifier_p (name))
41069 decl = cp_parser_lookup_name_simple (parser, name, token->location);
41070 else
41071 decl = name;
41072 if (decl == error_mark_node)
41073 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
41074 token->location);
41077 if (decl == error_mark_node
41078 || !parens.require_close (parser))
41079 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41080 /*or_comma=*/false,
41081 /*consume_paren=*/true);
41083 tree u = build_omp_clause (token->location, OMP_CLAUSE_DETACH);
41084 OMP_CLAUSE_DECL (u) = decl;
41085 OMP_CLAUSE_CHAIN (u) = list;
41087 return u;
41090 /* OpenMP 5.0:
41091 iterators ( iterators-definition )
41093 iterators-definition:
41094 iterator-specifier
41095 iterator-specifier , iterators-definition
41097 iterator-specifier:
41098 identifier = range-specification
41099 iterator-type identifier = range-specification
41101 range-specification:
41102 begin : end
41103 begin : end : step */
41105 static tree
41106 cp_parser_omp_iterators (cp_parser *parser)
41108 tree ret = NULL_TREE, *last = &ret;
41109 cp_lexer_consume_token (parser->lexer);
41111 matching_parens parens;
41112 if (!parens.require_open (parser))
41113 return error_mark_node;
41115 bool saved_colon_corrects_to_scope_p
41116 = parser->colon_corrects_to_scope_p;
41117 bool saved_colon_doesnt_start_class_def_p
41118 = parser->colon_doesnt_start_class_def_p;
41122 tree iter_type;
41123 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
41124 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ))
41125 iter_type = integer_type_node;
41126 else
41128 const char *saved_message
41129 = parser->type_definition_forbidden_message;
41130 parser->type_definition_forbidden_message
41131 = G_("types may not be defined in iterator type");
41133 iter_type = cp_parser_type_id (parser);
41135 parser->type_definition_forbidden_message = saved_message;
41138 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
41139 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
41141 cp_parser_error (parser, "expected identifier");
41142 break;
41145 tree id = cp_parser_identifier (parser);
41146 if (id == error_mark_node)
41147 break;
41149 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
41150 break;
41152 parser->colon_corrects_to_scope_p = false;
41153 parser->colon_doesnt_start_class_def_p = true;
41154 tree begin = cp_parser_assignment_expression (parser);
41156 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
41157 break;
41159 tree end = cp_parser_assignment_expression (parser);
41161 tree step = integer_one_node;
41162 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
41164 cp_lexer_consume_token (parser->lexer);
41165 step = cp_parser_assignment_expression (parser);
41168 tree iter_var = build_decl (loc, VAR_DECL, id, iter_type);
41169 DECL_ARTIFICIAL (iter_var) = 1;
41170 DECL_CONTEXT (iter_var) = current_function_decl;
41171 pushdecl (iter_var);
41173 *last = make_tree_vec (6);
41174 TREE_VEC_ELT (*last, 0) = iter_var;
41175 TREE_VEC_ELT (*last, 1) = begin;
41176 TREE_VEC_ELT (*last, 2) = end;
41177 TREE_VEC_ELT (*last, 3) = step;
41178 last = &TREE_CHAIN (*last);
41180 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
41182 cp_lexer_consume_token (parser->lexer);
41183 continue;
41185 break;
41187 while (1);
41189 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
41190 parser->colon_doesnt_start_class_def_p
41191 = saved_colon_doesnt_start_class_def_p;
41193 if (!parens.require_close (parser))
41194 cp_parser_skip_to_closing_parenthesis (parser,
41195 /*recovering=*/true,
41196 /*or_comma=*/false,
41197 /*consume_paren=*/true);
41199 return ret ? ret : error_mark_node;
41202 /* OpenMP 5.0:
41203 affinity ( [aff-modifier :] variable-list )
41204 aff-modifier:
41205 iterator ( iterators-definition ) */
41207 static tree
41208 cp_parser_omp_clause_affinity (cp_parser *parser, tree list)
41210 tree nlist, c, iterators = NULL_TREE;
41212 matching_parens parens;
41213 if (!parens.require_open (parser))
41214 return list;
41216 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41218 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41219 const char *p = IDENTIFIER_POINTER (id);
41220 bool parse_iter = ((strcmp ("iterator", p) == 0)
41221 && (cp_lexer_nth_token_is (parser->lexer, 2,
41222 CPP_OPEN_PAREN)));
41223 if (parse_iter)
41225 size_t n = cp_parser_skip_balanced_tokens (parser, 2);
41226 parse_iter = cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON);
41228 if (parse_iter)
41230 begin_scope (sk_omp, NULL);
41231 iterators = cp_parser_omp_iterators (parser);
41232 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
41234 if (iterators)
41235 poplevel (0, 1, 0);
41236 cp_parser_skip_to_closing_parenthesis (parser,
41237 /*recovering=*/true,
41238 /*or_comma=*/false,
41239 /*consume_paren=*/true);
41240 return list;
41244 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_AFFINITY,
41245 list, NULL);
41246 if (iterators)
41248 tree block = poplevel (1, 1, 0);
41249 if (iterators != error_mark_node)
41251 TREE_VEC_ELT (iterators, 5) = block;
41252 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
41253 OMP_CLAUSE_DECL (c) = build_tree_list (iterators,
41254 OMP_CLAUSE_DECL (c));
41257 return nlist;
41260 /* OpenMP 4.0:
41261 depend ( depend-kind : variable-list )
41263 depend-kind:
41264 in | out | inout
41266 OpenMP 4.5:
41267 depend ( source )
41269 depend ( sink : vec )
41271 OpenMP 5.0:
41272 depend ( depend-modifier , depend-kind: variable-list )
41274 depend-kind:
41275 in | out | inout | mutexinoutset | depobj
41277 depend-modifier:
41278 iterator ( iterators-definition ) */
41280 static tree
41281 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
41283 tree nlist, c, iterators = NULL_TREE;
41284 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_LAST;
41285 enum omp_clause_doacross_kind dkind = OMP_CLAUSE_DOACROSS_LAST;
41287 matching_parens parens;
41288 if (!parens.require_open (parser))
41289 return list;
41293 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
41294 goto invalid_kind;
41296 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41297 const char *p = IDENTIFIER_POINTER (id);
41299 if (strcmp ("iterator", p) == 0 && iterators == NULL_TREE)
41301 begin_scope (sk_omp, NULL);
41302 iterators = cp_parser_omp_iterators (parser);
41303 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
41304 continue;
41306 if (strcmp ("in", p) == 0)
41307 kind = OMP_CLAUSE_DEPEND_IN;
41308 else if (strcmp ("inout", p) == 0)
41309 kind = OMP_CLAUSE_DEPEND_INOUT;
41310 else if (strcmp ("inoutset", p) == 0)
41311 kind = OMP_CLAUSE_DEPEND_INOUTSET;
41312 else if (strcmp ("mutexinoutset", p) == 0)
41313 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
41314 else if (strcmp ("out", p) == 0)
41315 kind = OMP_CLAUSE_DEPEND_OUT;
41316 else if (strcmp ("depobj", p) == 0)
41317 kind = OMP_CLAUSE_DEPEND_DEPOBJ;
41318 else if (strcmp ("sink", p) == 0)
41319 dkind = OMP_CLAUSE_DOACROSS_SINK;
41320 else if (strcmp ("source", p) == 0)
41321 dkind = OMP_CLAUSE_DOACROSS_SOURCE;
41322 else
41323 goto invalid_kind;
41324 break;
41326 while (1);
41328 cp_lexer_consume_token (parser->lexer);
41330 if (iterators
41331 && (dkind == OMP_CLAUSE_DOACROSS_SOURCE
41332 || dkind == OMP_CLAUSE_DOACROSS_SINK))
41334 poplevel (0, 1, 0);
41335 error_at (loc, "%<iterator%> modifier incompatible with %qs",
41336 dkind == OMP_CLAUSE_DOACROSS_SOURCE ? "source" : "sink");
41337 iterators = NULL_TREE;
41340 if (dkind == OMP_CLAUSE_DOACROSS_SOURCE)
41342 c = build_omp_clause (loc, OMP_CLAUSE_DOACROSS);
41343 OMP_CLAUSE_DOACROSS_KIND (c) = dkind;
41344 OMP_CLAUSE_DOACROSS_DEPEND (c) = 1;
41345 OMP_CLAUSE_DECL (c) = NULL_TREE;
41346 OMP_CLAUSE_CHAIN (c) = list;
41347 if (!parens.require_close (parser))
41348 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41349 /*or_comma=*/false,
41350 /*consume_paren=*/true);
41351 return c;
41354 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
41355 goto resync_fail;
41357 if (dkind == OMP_CLAUSE_DOACROSS_SINK)
41359 nlist = cp_parser_omp_clause_doacross_sink (parser, loc, list, true);
41360 if (!parens.require_close (parser))
41361 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41362 /*or_comma=*/false,
41363 /*consume_paren=*/true);
41365 else
41367 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
41368 list, NULL);
41370 if (iterators)
41372 tree block = poplevel (1, 1, 0);
41373 if (iterators == error_mark_node)
41374 iterators = NULL_TREE;
41375 else
41376 TREE_VEC_ELT (iterators, 5) = block;
41379 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
41381 OMP_CLAUSE_DEPEND_KIND (c) = kind;
41382 if (iterators)
41383 OMP_CLAUSE_DECL (c)
41384 = build_tree_list (iterators, OMP_CLAUSE_DECL (c));
41387 return nlist;
41389 invalid_kind:
41390 cp_parser_error (parser, "invalid depend kind");
41391 resync_fail:
41392 if (iterators)
41393 poplevel (0, 1, 0);
41394 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41395 /*or_comma=*/false,
41396 /*consume_paren=*/true);
41397 return list;
41400 /* OpenMP 5.2:
41401 doacross ( source : )
41402 doacross ( source : omp_cur_iteration )
41404 doacross ( sink : vec )
41405 doacross ( sink : omp_cur_iteration - logical_iteration ) */
41407 static tree
41408 cp_parser_omp_clause_doacross (cp_parser *parser, tree list, location_t loc)
41410 tree nlist;
41411 enum omp_clause_doacross_kind kind = OMP_CLAUSE_DOACROSS_LAST;
41413 matching_parens parens;
41414 if (!parens.require_open (parser))
41415 return list;
41417 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
41419 invalid_kind:
41420 cp_parser_error (parser, "invalid doacross kind");
41421 resync_fail:
41422 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41423 /*or_comma=*/false,
41424 /*consume_paren=*/true);
41425 return list;
41428 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41429 const char *p = IDENTIFIER_POINTER (id);
41431 if (strcmp ("sink", p) == 0)
41432 kind = OMP_CLAUSE_DOACROSS_SINK;
41433 else if (strcmp ("source", p) == 0)
41434 kind = OMP_CLAUSE_DOACROSS_SOURCE;
41435 else
41436 goto invalid_kind;
41438 cp_lexer_consume_token (parser->lexer);
41440 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
41441 goto resync_fail;
41443 if (kind == OMP_CLAUSE_DOACROSS_SOURCE)
41445 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41447 id = cp_lexer_peek_token (parser->lexer)->u.value;
41448 p = IDENTIFIER_POINTER (id);
41449 if (strcmp (p, "omp_cur_iteration") == 0)
41450 cp_lexer_consume_token (parser->lexer);
41452 nlist = build_omp_clause (loc, OMP_CLAUSE_DOACROSS);
41453 OMP_CLAUSE_DOACROSS_KIND (nlist) = OMP_CLAUSE_DOACROSS_SOURCE;
41454 OMP_CLAUSE_DECL (nlist) = NULL_TREE;
41455 OMP_CLAUSE_CHAIN (nlist) = list;
41457 else
41458 nlist = cp_parser_omp_clause_doacross_sink (parser, loc, list, false);
41460 if (!parens.require_close (parser))
41461 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41462 /*or_comma=*/false,
41463 /*consume_paren=*/true);
41464 return nlist;
41467 /* OpenMP 4.0:
41468 from ( variable-list )
41469 to ( variable-list )
41471 OpenMP 5.1:
41472 from ( [present :] variable-list )
41473 to ( [present :] variable-list ) */
41475 static tree
41476 cp_parser_omp_clause_from_to (cp_parser *parser, enum omp_clause_code kind,
41477 tree list)
41479 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
41480 return list;
41482 bool present = false;
41483 cp_token *token = cp_lexer_peek_token (parser->lexer);
41485 if (token->type == CPP_NAME
41486 && strcmp (IDENTIFIER_POINTER (token->u.value), "present") == 0
41487 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
41489 present = true;
41490 cp_lexer_consume_token (parser->lexer);
41491 cp_lexer_consume_token (parser->lexer);
41494 tree nl = cp_parser_omp_var_list_no_open (parser, kind, list, NULL, true);
41495 if (present)
41496 for (tree c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
41497 OMP_CLAUSE_MOTION_PRESENT (c) = 1;
41499 return nl;
41502 /* OpenMP 4.0:
41503 map ( map-kind : variable-list )
41504 map ( variable-list )
41506 map-kind:
41507 alloc | to | from | tofrom
41509 OpenMP 4.5:
41510 map-kind:
41511 alloc | to | from | tofrom | release | delete
41513 map ( always [,] map-kind: variable-list )
41515 OpenMP 5.0:
41516 map ( [map-type-modifier[,] ...] map-kind: variable-list )
41518 map-type-modifier:
41519 always | close */
41521 static tree
41522 cp_parser_omp_clause_map (cp_parser *parser, tree list)
41524 tree nlist, c;
41525 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
41527 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
41528 return list;
41530 int pos = 1;
41531 int map_kind_pos = 0;
41532 while (cp_lexer_peek_nth_token (parser->lexer, pos)->type == CPP_NAME
41533 || cp_lexer_peek_nth_token (parser->lexer, pos)->keyword == RID_DELETE)
41535 if (cp_lexer_peek_nth_token (parser->lexer, pos + 1)->type == CPP_COLON)
41537 map_kind_pos = pos;
41538 break;
41541 if (cp_lexer_peek_nth_token (parser->lexer, pos + 1)->type == CPP_COMMA)
41542 pos++;
41543 pos++;
41546 bool always_modifier = false;
41547 bool close_modifier = false;
41548 bool present_modifier = false;
41549 for (int pos = 1; pos < map_kind_pos; ++pos)
41551 cp_token *tok = cp_lexer_peek_token (parser->lexer);
41552 if (tok->type == CPP_COMMA)
41554 cp_lexer_consume_token (parser->lexer);
41555 continue;
41558 const char *p = IDENTIFIER_POINTER (tok->u.value);
41559 if (strcmp ("always", p) == 0)
41561 if (always_modifier)
41563 cp_parser_error (parser, "too many %<always%> modifiers");
41564 cp_parser_skip_to_closing_parenthesis (parser,
41565 /*recovering=*/true,
41566 /*or_comma=*/false,
41567 /*consume_paren=*/true);
41568 return list;
41570 always_modifier = true;
41572 else if (strcmp ("close", p) == 0)
41574 if (close_modifier)
41576 cp_parser_error (parser, "too many %<close%> modifiers");
41577 cp_parser_skip_to_closing_parenthesis (parser,
41578 /*recovering=*/true,
41579 /*or_comma=*/false,
41580 /*consume_paren=*/true);
41581 return list;
41583 close_modifier = true;
41585 else if (strcmp ("present", p) == 0)
41587 if (present_modifier)
41589 cp_parser_error (parser, "too many %<present%> modifiers");
41590 cp_parser_skip_to_closing_parenthesis (parser,
41591 /*recovering=*/true,
41592 /*or_comma=*/false,
41593 /*consume_paren=*/true);
41594 return list;
41596 present_modifier = true;
41598 else
41600 cp_parser_error (parser, "%<map%> clause with map-type modifier other"
41601 " than %<always%>, %<close%> or %<present%>");
41602 cp_parser_skip_to_closing_parenthesis (parser,
41603 /*recovering=*/true,
41604 /*or_comma=*/false,
41605 /*consume_paren=*/true);
41606 return list;
41609 cp_lexer_consume_token (parser->lexer);
41612 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
41613 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
41615 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41616 const char *p = IDENTIFIER_POINTER (id);
41617 int always_present_modifier = always_modifier && present_modifier;
41619 if (strcmp ("alloc", p) == 0)
41620 kind = present_modifier ? GOMP_MAP_PRESENT_ALLOC : GOMP_MAP_ALLOC;
41621 else if (strcmp ("to", p) == 0)
41622 kind = (always_present_modifier ? GOMP_MAP_ALWAYS_PRESENT_TO
41623 : present_modifier ? GOMP_MAP_PRESENT_TO
41624 : always_modifier ? GOMP_MAP_ALWAYS_TO
41625 : GOMP_MAP_TO);
41626 else if (strcmp ("from", p) == 0)
41627 kind = (always_present_modifier ? GOMP_MAP_ALWAYS_PRESENT_FROM
41628 : present_modifier ? GOMP_MAP_PRESENT_FROM
41629 : always_modifier ? GOMP_MAP_ALWAYS_FROM
41630 : GOMP_MAP_FROM);
41631 else if (strcmp ("tofrom", p) == 0)
41632 kind = (always_present_modifier ? GOMP_MAP_ALWAYS_PRESENT_TOFROM
41633 : present_modifier ? GOMP_MAP_PRESENT_TOFROM
41634 : always_modifier ? GOMP_MAP_ALWAYS_TOFROM
41635 : GOMP_MAP_TOFROM);
41636 else if (strcmp ("release", p) == 0)
41637 kind = GOMP_MAP_RELEASE;
41638 else
41640 cp_parser_error (parser, "invalid map kind");
41641 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41642 /*or_comma=*/false,
41643 /*consume_paren=*/true);
41644 return list;
41646 cp_lexer_consume_token (parser->lexer);
41647 cp_lexer_consume_token (parser->lexer);
41649 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
41650 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
41652 kind = GOMP_MAP_DELETE;
41653 cp_lexer_consume_token (parser->lexer);
41654 cp_lexer_consume_token (parser->lexer);
41657 /* We introduce a scope here so that errors parsing e.g. "always", "close"
41658 tokens do not propagate to later directives that might use them
41659 legally. */
41660 begin_scope (sk_omp, NULL);
41661 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
41662 NULL, true);
41663 finish_scope ();
41665 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
41666 OMP_CLAUSE_SET_MAP_KIND (c, kind);
41668 return nlist;
41671 /* OpenMP 4.0:
41672 device ( expression )
41674 OpenMP 5.0:
41675 device ( [device-modifier :] integer-expression )
41677 device-modifier:
41678 ancestor | device_num */
41680 static tree
41681 cp_parser_omp_clause_device (cp_parser *parser, tree list,
41682 location_t location)
41684 tree t, c;
41685 bool ancestor = false;
41687 matching_parens parens;
41688 if (!parens.require_open (parser))
41689 return list;
41691 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
41692 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
41694 cp_token *tok = cp_lexer_peek_token (parser->lexer);
41695 const char *p = IDENTIFIER_POINTER (tok->u.value);
41696 if (strcmp ("ancestor", p) == 0)
41698 ancestor = true;
41700 /* A requires directive with the reverse_offload clause must be
41701 specified. */
41702 if ((omp_requires_mask & OMP_REQUIRES_REVERSE_OFFLOAD) == 0)
41704 error_at (tok->location, "%<ancestor%> device modifier not "
41705 "preceded by %<requires%> directive "
41706 "with %<reverse_offload%> clause");
41707 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
41708 return list;
41711 else if (strcmp ("device_num", p) == 0)
41713 else
41715 error_at (tok->location, "expected %<ancestor%> or %<device_num%>");
41716 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
41717 return list;
41719 cp_lexer_consume_token (parser->lexer);
41720 cp_lexer_consume_token (parser->lexer);
41723 t = cp_parser_assignment_expression (parser);
41725 if (t == error_mark_node
41726 || !parens.require_close (parser))
41727 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41728 /*or_comma=*/false,
41729 /*consume_paren=*/true);
41731 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
41732 "device", location);
41734 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
41735 OMP_CLAUSE_DEVICE_ID (c) = t;
41736 OMP_CLAUSE_CHAIN (c) = list;
41737 OMP_CLAUSE_DEVICE_ANCESTOR (c) = ancestor;
41739 return c;
41742 /* OpenMP 4.0:
41743 dist_schedule ( static )
41744 dist_schedule ( static , expression ) */
41746 static tree
41747 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
41748 location_t location)
41750 tree c, t;
41752 matching_parens parens;
41753 if (!parens.require_open (parser))
41754 return list;
41756 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
41758 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
41759 goto invalid_kind;
41760 cp_lexer_consume_token (parser->lexer);
41762 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
41764 cp_lexer_consume_token (parser->lexer);
41766 t = cp_parser_assignment_expression (parser);
41768 if (t == error_mark_node)
41769 goto resync_fail;
41770 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
41772 if (!parens.require_close (parser))
41773 goto resync_fail;
41775 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
41776 goto resync_fail;
41778 /* check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE,
41779 "dist_schedule", location); */
41780 if (omp_find_clause (list, OMP_CLAUSE_DIST_SCHEDULE))
41781 warning_at (location, OPT_Wopenmp, "too many %qs clauses", "dist_schedule");
41782 OMP_CLAUSE_CHAIN (c) = list;
41783 return c;
41785 invalid_kind:
41786 cp_parser_error (parser, "invalid dist_schedule kind");
41787 resync_fail:
41788 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41789 /*or_comma=*/false,
41790 /*consume_paren=*/true);
41791 return list;
41794 /* OpenMP 4.0:
41795 proc_bind ( proc-bind-kind )
41797 proc-bind-kind:
41798 primary | master | close | spread
41799 where OpenMP 5.1 added 'primary' and deprecated the alias 'master'. */
41801 static tree
41802 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
41803 location_t location)
41805 tree c;
41806 enum omp_clause_proc_bind_kind kind;
41808 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
41809 return list;
41811 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41813 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41814 const char *p = IDENTIFIER_POINTER (id);
41816 if (strcmp ("primary", p) == 0)
41817 kind = OMP_CLAUSE_PROC_BIND_PRIMARY;
41818 else if (strcmp ("master", p) == 0)
41819 kind = OMP_CLAUSE_PROC_BIND_MASTER;
41820 else if (strcmp ("close", p) == 0)
41821 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
41822 else if (strcmp ("spread", p) == 0)
41823 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
41824 else
41825 goto invalid_kind;
41827 else
41828 goto invalid_kind;
41830 cp_lexer_consume_token (parser->lexer);
41831 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
41832 goto resync_fail;
41834 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
41835 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
41836 location);
41837 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
41838 OMP_CLAUSE_CHAIN (c) = list;
41839 return c;
41841 invalid_kind:
41842 cp_parser_error (parser, "invalid depend kind");
41843 resync_fail:
41844 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41845 /*or_comma=*/false,
41846 /*consume_paren=*/true);
41847 return list;
41850 /* OpenMP 5.0:
41851 device_type ( host | nohost | any ) */
41853 static tree
41854 cp_parser_omp_clause_device_type (cp_parser *parser, tree list,
41855 location_t location)
41857 tree c;
41858 enum omp_clause_device_type_kind kind;
41860 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
41861 return list;
41863 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41865 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41866 const char *p = IDENTIFIER_POINTER (id);
41868 if (strcmp ("host", p) == 0)
41869 kind = OMP_CLAUSE_DEVICE_TYPE_HOST;
41870 else if (strcmp ("nohost", p) == 0)
41871 kind = OMP_CLAUSE_DEVICE_TYPE_NOHOST;
41872 else if (strcmp ("any", p) == 0)
41873 kind = OMP_CLAUSE_DEVICE_TYPE_ANY;
41874 else
41875 goto invalid_kind;
41877 else
41878 goto invalid_kind;
41880 cp_lexer_consume_token (parser->lexer);
41881 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
41882 goto resync_fail;
41884 c = build_omp_clause (location, OMP_CLAUSE_DEVICE_TYPE);
41885 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE_TYPE, "device_type",
41886 location);
41887 OMP_CLAUSE_DEVICE_TYPE_KIND (c) = kind;
41888 OMP_CLAUSE_CHAIN (c) = list;
41889 return c;
41891 invalid_kind:
41892 cp_parser_error (parser, "invalid depend kind");
41893 resync_fail:
41894 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41895 /*or_comma=*/false,
41896 /*consume_paren=*/true);
41897 return list;
41900 /* OpenACC:
41901 async [( int-expr )] */
41903 static tree
41904 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
41906 tree c, t;
41907 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
41909 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
41911 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
41913 matching_parens parens;
41914 parens.consume_open (parser);
41916 t = cp_parser_assignment_expression (parser);
41917 if (t == error_mark_node
41918 || !parens.require_close (parser))
41919 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41920 /*or_comma=*/false,
41921 /*consume_paren=*/true);
41924 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
41926 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
41927 OMP_CLAUSE_ASYNC_EXPR (c) = t;
41928 OMP_CLAUSE_CHAIN (c) = list;
41929 list = c;
41931 return list;
41934 /* OpenACC 2.7:
41935 self [( expression )] */
41937 static tree
41938 cp_parser_oacc_compute_clause_self (cp_parser *parser, tree list)
41940 tree t;
41941 location_t location = cp_lexer_peek_token (parser->lexer)->location;
41942 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
41944 matching_parens parens;
41945 parens.consume_open (parser);
41946 t = cp_parser_assignment_expression (parser);
41947 if (t == error_mark_node
41948 || !parens.require_close (parser))
41950 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41951 /*or_comma=*/false,
41952 /*consume_paren=*/true);
41953 return list;
41956 else
41957 t = truthvalue_true_node;
41959 for (tree c = list; c; c = OMP_CLAUSE_CHAIN (c))
41960 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SELF)
41962 error_at (location, "too many %<self%> clauses");
41963 return list;
41966 tree c = build_omp_clause (location, OMP_CLAUSE_SELF);
41967 OMP_CLAUSE_SELF_EXPR (c) = t;
41968 OMP_CLAUSE_CHAIN (c) = list;
41969 return c;
41972 /* Parse all OpenACC clauses. The set clauses allowed by the directive
41973 is a bitmask in MASK. Return the list of clauses found. */
41975 static tree
41976 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
41977 const char *where, cp_token *pragma_tok,
41978 bool finish_p = true, bool target_p = false)
41980 tree clauses = NULL;
41981 bool first = true;
41983 /* Don't create location wrapper nodes within OpenACC clauses. */
41984 auto_suppress_location_wrappers sentinel;
41986 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
41988 location_t here;
41989 pragma_omp_clause c_kind;
41990 omp_clause_code code;
41991 const char *c_name;
41992 tree prev = clauses;
41994 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
41995 cp_lexer_consume_token (parser->lexer);
41997 here = cp_lexer_peek_token (parser->lexer)->location;
41998 c_kind = cp_parser_omp_clause_name (parser);
42000 switch (c_kind)
42002 case PRAGMA_OACC_CLAUSE_ASYNC:
42003 clauses = cp_parser_oacc_clause_async (parser, clauses);
42004 c_name = "async";
42005 break;
42006 case PRAGMA_OACC_CLAUSE_AUTO:
42007 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_AUTO,
42008 clauses);
42009 c_name = "auto";
42010 break;
42011 case PRAGMA_OACC_CLAUSE_ATTACH:
42012 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
42013 c_name = "attach";
42014 break;
42015 case PRAGMA_OACC_CLAUSE_COLLAPSE:
42016 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
42017 c_name = "collapse";
42018 break;
42019 case PRAGMA_OACC_CLAUSE_COPY:
42020 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
42021 c_name = "copy";
42022 break;
42023 case PRAGMA_OACC_CLAUSE_COPYIN:
42024 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
42025 c_name = "copyin";
42026 break;
42027 case PRAGMA_OACC_CLAUSE_COPYOUT:
42028 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
42029 c_name = "copyout";
42030 break;
42031 case PRAGMA_OACC_CLAUSE_CREATE:
42032 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
42033 c_name = "create";
42034 break;
42035 case PRAGMA_OACC_CLAUSE_DELETE:
42036 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
42037 c_name = "delete";
42038 break;
42039 case PRAGMA_OMP_CLAUSE_DEFAULT:
42040 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
42041 c_name = "default";
42042 break;
42043 case PRAGMA_OACC_CLAUSE_DETACH:
42044 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
42045 c_name = "detach";
42046 break;
42047 case PRAGMA_OACC_CLAUSE_DEVICE:
42048 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
42049 c_name = "device";
42050 break;
42051 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
42052 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
42053 c_name = "deviceptr";
42054 break;
42055 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
42056 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
42057 c_name = "device_resident";
42058 break;
42059 case PRAGMA_OACC_CLAUSE_FINALIZE:
42060 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_FINALIZE,
42061 clauses);
42062 c_name = "finalize";
42063 break;
42064 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
42065 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
42066 clauses);
42067 c_name = "firstprivate";
42068 break;
42069 case PRAGMA_OACC_CLAUSE_GANG:
42070 c_name = "gang";
42071 clauses = cp_parser_oacc_shape_clause (parser, here, OMP_CLAUSE_GANG,
42072 c_name, clauses);
42073 break;
42074 case PRAGMA_OACC_CLAUSE_HOST:
42075 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
42076 c_name = "host";
42077 break;
42078 case PRAGMA_OACC_CLAUSE_IF:
42079 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
42080 c_name = "if";
42081 break;
42082 case PRAGMA_OACC_CLAUSE_IF_PRESENT:
42083 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_IF_PRESENT,
42084 clauses);
42085 c_name = "if_present";
42086 break;
42087 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
42088 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_INDEPENDENT,
42089 clauses);
42090 c_name = "independent";
42091 break;
42092 case PRAGMA_OACC_CLAUSE_LINK:
42093 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
42094 c_name = "link";
42095 break;
42096 case PRAGMA_OACC_CLAUSE_NO_CREATE:
42097 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
42098 c_name = "no_create";
42099 break;
42100 case PRAGMA_OACC_CLAUSE_NOHOST:
42101 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_NOHOST,
42102 clauses);
42103 c_name = "nohost";
42104 break;
42105 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
42106 code = OMP_CLAUSE_NUM_GANGS;
42107 c_name = "num_gangs";
42108 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
42109 clauses);
42110 break;
42111 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
42112 c_name = "num_workers";
42113 code = OMP_CLAUSE_NUM_WORKERS;
42114 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
42115 clauses);
42116 break;
42117 case PRAGMA_OACC_CLAUSE_PRESENT:
42118 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
42119 c_name = "present";
42120 break;
42121 case PRAGMA_OACC_CLAUSE_PRIVATE:
42122 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
42123 clauses);
42124 c_name = "private";
42125 break;
42126 case PRAGMA_OACC_CLAUSE_REDUCTION:
42127 clauses
42128 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
42129 false, clauses);
42130 c_name = "reduction";
42131 break;
42132 case PRAGMA_OACC_CLAUSE_SELF:
42133 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST)) == 0)
42134 /* OpenACC compute construct */
42135 clauses = cp_parser_oacc_compute_clause_self (parser, clauses);
42136 else
42137 /* OpenACC 'update' directive */
42138 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
42139 c_name = "self";
42140 break;
42141 case PRAGMA_OACC_CLAUSE_SEQ:
42142 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_SEQ,
42143 clauses);
42144 c_name = "seq";
42145 break;
42146 case PRAGMA_OACC_CLAUSE_TILE:
42147 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
42148 c_name = "tile";
42149 break;
42150 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
42151 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
42152 clauses);
42153 c_name = "use_device";
42154 break;
42155 case PRAGMA_OACC_CLAUSE_VECTOR:
42156 c_name = "vector";
42157 clauses = cp_parser_oacc_shape_clause (parser, here,
42158 OMP_CLAUSE_VECTOR,
42159 c_name, clauses);
42160 break;
42161 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
42162 c_name = "vector_length";
42163 code = OMP_CLAUSE_VECTOR_LENGTH;
42164 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
42165 clauses);
42166 break;
42167 case PRAGMA_OACC_CLAUSE_WAIT:
42168 clauses = cp_parser_oacc_clause_wait (parser, clauses);
42169 c_name = "wait";
42170 break;
42171 case PRAGMA_OACC_CLAUSE_WORKER:
42172 c_name = "worker";
42173 clauses = cp_parser_oacc_shape_clause (parser, here,
42174 OMP_CLAUSE_WORKER,
42175 c_name, clauses);
42176 break;
42177 default:
42178 cp_parser_error (parser, "expected an OpenACC clause");
42179 goto saw_error;
42182 first = false;
42184 if (((mask >> c_kind) & 1) == 0)
42186 /* Remove the invalid clause(s) from the list to avoid
42187 confusing the rest of the compiler. */
42188 clauses = prev;
42189 error_at (here, "%qs is not valid for %qs", c_name, where);
42193 saw_error:
42194 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
42196 if (finish_p)
42197 return finish_omp_clauses (clauses, target_p ? C_ORT_ACC_TARGET
42198 : C_ORT_ACC);
42200 return clauses;
42203 /* Parse all OpenMP clauses. The set clauses allowed by the directive
42204 is a bitmask in MASK. Return the list of clauses found.
42205 FINISH_P set if finish_omp_clauses should be called.
42206 NESTED non-zero if clauses should be terminated by closing paren instead
42207 of end of pragma. If it is 2, additionally commas are required in between
42208 the clauses. */
42210 static tree
42211 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
42212 const char *where, cp_token *pragma_tok,
42213 bool finish_p = true, int nested = 0)
42215 tree clauses = NULL;
42216 bool first = true;
42217 cp_token *token = NULL;
42219 /* Don't create location wrapper nodes within OpenMP clauses. */
42220 auto_suppress_location_wrappers sentinel;
42222 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
42224 pragma_omp_clause c_kind;
42225 const char *c_name;
42226 tree prev = clauses;
42228 if (nested && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
42229 break;
42231 if (!first || nested != 2)
42233 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
42234 cp_lexer_consume_token (parser->lexer);
42235 else if (nested == 2)
42236 error_at (cp_lexer_peek_token (parser->lexer)->location,
42237 "clauses in %<simd%> trait should be separated "
42238 "by %<,%>");
42241 token = cp_lexer_peek_token (parser->lexer);
42242 c_kind = cp_parser_omp_clause_name (parser);
42244 switch (c_kind)
42246 case PRAGMA_OMP_CLAUSE_BIND:
42247 clauses = cp_parser_omp_clause_bind (parser, clauses,
42248 token->location);
42249 c_name = "bind";
42250 break;
42251 case PRAGMA_OMP_CLAUSE_COLLAPSE:
42252 clauses = cp_parser_omp_clause_collapse (parser, clauses,
42253 token->location);
42254 c_name = "collapse";
42255 break;
42256 case PRAGMA_OMP_CLAUSE_COPYIN:
42257 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
42258 c_name = "copyin";
42259 break;
42260 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
42261 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
42262 clauses);
42263 c_name = "copyprivate";
42264 break;
42265 case PRAGMA_OMP_CLAUSE_DEFAULT:
42266 clauses = cp_parser_omp_clause_default (parser, clauses,
42267 token->location, false);
42268 c_name = "default";
42269 break;
42270 case PRAGMA_OMP_CLAUSE_FILTER:
42271 clauses = cp_parser_omp_clause_filter (parser, clauses,
42272 token->location);
42273 c_name = "filter";
42274 break;
42275 case PRAGMA_OMP_CLAUSE_FINAL:
42276 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
42277 c_name = "final";
42278 break;
42279 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
42280 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
42281 clauses);
42282 c_name = "firstprivate";
42283 break;
42284 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
42285 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
42286 token->location);
42287 c_name = "grainsize";
42288 break;
42289 case PRAGMA_OMP_CLAUSE_HINT:
42290 clauses = cp_parser_omp_clause_hint (parser, clauses,
42291 token->location);
42292 c_name = "hint";
42293 break;
42294 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
42295 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
42296 token->location);
42297 c_name = "defaultmap";
42298 break;
42299 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
42300 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
42301 clauses);
42302 c_name = "use_device_ptr";
42303 break;
42304 case PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR:
42305 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_ADDR,
42306 clauses);
42307 c_name = "use_device_addr";
42308 break;
42309 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
42310 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
42311 clauses);
42312 c_name = "is_device_ptr";
42313 break;
42314 case PRAGMA_OMP_CLAUSE_HAS_DEVICE_ADDR:
42315 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_HAS_DEVICE_ADDR,
42316 clauses);
42317 c_name = "has_device_addr";
42318 break;
42319 case PRAGMA_OMP_CLAUSE_IF:
42320 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
42321 true);
42322 c_name = "if";
42323 break;
42324 case PRAGMA_OMP_CLAUSE_IN_REDUCTION:
42325 clauses
42326 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_IN_REDUCTION,
42327 true, clauses);
42328 c_name = "in_reduction";
42329 break;
42330 case PRAGMA_OMP_CLAUSE_INDIRECT:
42331 clauses = cp_parser_omp_clause_indirect (parser, clauses,
42332 token->location);
42333 c_name = "indirect";
42334 break;
42335 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
42336 clauses = cp_parser_omp_clause_lastprivate (parser, clauses);
42337 c_name = "lastprivate";
42338 break;
42339 case PRAGMA_OMP_CLAUSE_MERGEABLE:
42340 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
42341 token->location);
42342 c_name = "mergeable";
42343 break;
42344 case PRAGMA_OMP_CLAUSE_NOWAIT:
42345 clauses = cp_parser_omp_clause_nowait (parser, clauses,
42346 token->location);
42347 c_name = "nowait";
42348 break;
42349 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
42350 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
42351 token->location);
42352 c_name = "num_tasks";
42353 break;
42354 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
42355 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
42356 token->location);
42357 c_name = "num_threads";
42358 break;
42359 case PRAGMA_OMP_CLAUSE_ORDER:
42360 clauses = cp_parser_omp_clause_order (parser, clauses,
42361 token->location);
42362 c_name = "order";
42363 break;
42364 case PRAGMA_OMP_CLAUSE_ORDERED:
42365 clauses = cp_parser_omp_clause_ordered (parser, clauses,
42366 token->location);
42367 c_name = "ordered";
42368 break;
42369 case PRAGMA_OMP_CLAUSE_PRIORITY:
42370 clauses = cp_parser_omp_clause_priority (parser, clauses,
42371 token->location);
42372 c_name = "priority";
42373 break;
42374 case PRAGMA_OMP_CLAUSE_PRIVATE:
42375 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
42376 clauses);
42377 c_name = "private";
42378 break;
42379 case PRAGMA_OMP_CLAUSE_REDUCTION:
42380 clauses
42381 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
42382 true, clauses);
42383 c_name = "reduction";
42384 break;
42385 case PRAGMA_OMP_CLAUSE_SCHEDULE:
42386 clauses = cp_parser_omp_clause_schedule (parser, clauses,
42387 token->location);
42388 c_name = "schedule";
42389 break;
42390 case PRAGMA_OMP_CLAUSE_SHARED:
42391 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
42392 clauses);
42393 c_name = "shared";
42394 break;
42395 case PRAGMA_OMP_CLAUSE_TASK_REDUCTION:
42396 clauses
42397 = cp_parser_omp_clause_reduction (parser,
42398 OMP_CLAUSE_TASK_REDUCTION,
42399 true, clauses);
42400 c_name = "task_reduction";
42401 break;
42402 case PRAGMA_OMP_CLAUSE_UNTIED:
42403 clauses = cp_parser_omp_clause_untied (parser, clauses,
42404 token->location);
42405 c_name = "untied";
42406 break;
42407 case PRAGMA_OMP_CLAUSE_INBRANCH:
42408 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
42409 clauses, token->location);
42410 c_name = "inbranch";
42411 break;
42412 case PRAGMA_OMP_CLAUSE_NONTEMPORAL:
42413 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_NONTEMPORAL,
42414 clauses);
42415 c_name = "nontemporal";
42416 break;
42417 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
42418 clauses = cp_parser_omp_clause_branch (parser,
42419 OMP_CLAUSE_NOTINBRANCH,
42420 clauses, token->location);
42421 c_name = "notinbranch";
42422 break;
42423 case PRAGMA_OMP_CLAUSE_PARALLEL:
42424 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
42425 clauses, token->location);
42426 c_name = "parallel";
42427 if (!first)
42429 clause_not_first:
42430 error_at (token->location, "%qs must be the first clause of %qs",
42431 c_name, where);
42432 clauses = prev;
42434 break;
42435 case PRAGMA_OMP_CLAUSE_FOR:
42436 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
42437 clauses, token->location);
42438 c_name = "for";
42439 if (!first)
42440 goto clause_not_first;
42441 break;
42442 case PRAGMA_OMP_CLAUSE_SECTIONS:
42443 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
42444 clauses, token->location);
42445 c_name = "sections";
42446 if (!first)
42447 goto clause_not_first;
42448 break;
42449 case PRAGMA_OMP_CLAUSE_TASKGROUP:
42450 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
42451 clauses, token->location);
42452 c_name = "taskgroup";
42453 if (!first)
42454 goto clause_not_first;
42455 break;
42456 case PRAGMA_OMP_CLAUSE_LINK:
42457 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
42458 c_name = "link";
42459 break;
42460 case PRAGMA_OMP_CLAUSE_TO:
42461 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
42463 tree nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_ENTER,
42464 clauses);
42465 for (tree c = nl; c != clauses; c = OMP_CLAUSE_CHAIN (c))
42466 OMP_CLAUSE_ENTER_TO (c) = 1;
42467 clauses = nl;
42469 else
42470 clauses = cp_parser_omp_clause_from_to (parser, OMP_CLAUSE_TO,
42471 clauses);
42472 c_name = "to";
42473 break;
42474 case PRAGMA_OMP_CLAUSE_FROM:
42475 clauses = cp_parser_omp_clause_from_to (parser, OMP_CLAUSE_FROM,
42476 clauses);
42477 c_name = "from";
42478 break;
42479 case PRAGMA_OMP_CLAUSE_UNIFORM:
42480 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
42481 clauses);
42482 c_name = "uniform";
42483 break;
42484 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
42485 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
42486 token->location);
42487 c_name = "num_teams";
42488 break;
42489 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
42490 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
42491 token->location);
42492 c_name = "thread_limit";
42493 break;
42494 case PRAGMA_OMP_CLAUSE_ALIGNED:
42495 clauses = cp_parser_omp_clause_aligned (parser, clauses);
42496 c_name = "aligned";
42497 break;
42498 case PRAGMA_OMP_CLAUSE_ALLOCATE:
42499 clauses = cp_parser_omp_clause_allocate (parser, clauses);
42500 c_name = "allocate";
42501 break;
42502 case PRAGMA_OMP_CLAUSE_LINEAR:
42504 bool declare_simd = false;
42505 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
42506 declare_simd = true;
42507 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
42509 c_name = "linear";
42510 break;
42511 case PRAGMA_OMP_CLAUSE_AFFINITY:
42512 clauses = cp_parser_omp_clause_affinity (parser, clauses);
42513 c_name = "affinity";
42514 break;
42515 case PRAGMA_OMP_CLAUSE_DEPEND:
42516 clauses = cp_parser_omp_clause_depend (parser, clauses,
42517 token->location);
42518 c_name = "depend";
42519 break;
42520 case PRAGMA_OMP_CLAUSE_DOACROSS:
42521 clauses = cp_parser_omp_clause_doacross (parser, clauses,
42522 token->location);
42523 c_name = "doacross";
42524 break;
42525 case PRAGMA_OMP_CLAUSE_DETACH:
42526 clauses = cp_parser_omp_clause_detach (parser, clauses);
42527 c_name = "detach";
42528 break;
42529 case PRAGMA_OMP_CLAUSE_MAP:
42530 clauses = cp_parser_omp_clause_map (parser, clauses);
42531 c_name = "map";
42532 break;
42533 case PRAGMA_OMP_CLAUSE_DEVICE:
42534 clauses = cp_parser_omp_clause_device (parser, clauses,
42535 token->location);
42536 c_name = "device";
42537 break;
42538 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
42539 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
42540 token->location);
42541 c_name = "dist_schedule";
42542 break;
42543 case PRAGMA_OMP_CLAUSE_PROC_BIND:
42544 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
42545 token->location);
42546 c_name = "proc_bind";
42547 break;
42548 case PRAGMA_OMP_CLAUSE_DEVICE_TYPE:
42549 clauses = cp_parser_omp_clause_device_type (parser, clauses,
42550 token->location);
42551 c_name = "device_type";
42552 break;
42553 case PRAGMA_OMP_CLAUSE_SAFELEN:
42554 clauses = cp_parser_omp_clause_safelen (parser, clauses,
42555 token->location);
42556 c_name = "safelen";
42557 break;
42558 case PRAGMA_OMP_CLAUSE_SIMDLEN:
42559 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
42560 token->location);
42561 c_name = "simdlen";
42562 break;
42563 case PRAGMA_OMP_CLAUSE_NOGROUP:
42564 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
42565 token->location);
42566 c_name = "nogroup";
42567 break;
42568 case PRAGMA_OMP_CLAUSE_THREADS:
42569 clauses
42570 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
42571 clauses, token->location);
42572 c_name = "threads";
42573 break;
42574 case PRAGMA_OMP_CLAUSE_SIMD:
42575 clauses
42576 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
42577 clauses, token->location);
42578 c_name = "simd";
42579 break;
42580 case PRAGMA_OMP_CLAUSE_ENTER:
42581 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_ENTER,
42582 clauses);
42583 c_name = "enter";
42584 break;
42585 default:
42586 cp_parser_error (parser, "expected an OpenMP clause");
42587 goto saw_error;
42590 first = false;
42592 if (((mask >> c_kind) & 1) == 0)
42594 /* Remove the invalid clause(s) from the list to avoid
42595 confusing the rest of the compiler. */
42596 clauses = prev;
42597 error_at (token->location, "%qs is not valid for %qs", c_name, where);
42600 saw_error:
42601 if (!nested)
42602 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
42603 if (finish_p)
42605 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
42606 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
42607 else
42608 return finish_omp_clauses (clauses, C_ORT_OMP);
42610 return clauses;
42613 /* OpenMP 2.5:
42614 structured-block:
42615 statement
42617 In practice, we're also interested in adding the statement to an
42618 outer node. So it is convenient if we work around the fact that
42619 cp_parser_statement calls add_stmt. */
42621 static unsigned
42622 cp_parser_begin_omp_structured_block (cp_parser *parser)
42624 unsigned save = parser->in_statement;
42626 /* Only move the values to IN_OMP_BLOCK if they weren't false.
42627 This preserves the "not within loop or switch" style error messages
42628 for nonsense cases like
42629 void foo() {
42630 #pragma omp single
42631 break;
42634 if (parser->in_statement)
42635 parser->in_statement = IN_OMP_BLOCK;
42637 return save;
42640 static void
42641 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
42643 parser->in_statement = save;
42646 static tree
42647 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
42649 tree stmt = begin_omp_structured_block ();
42650 unsigned int save = cp_parser_begin_omp_structured_block (parser);
42652 parser->omp_attrs_forbidden_p = true;
42653 cp_parser_statement (parser, NULL_TREE, false, if_p);
42655 cp_parser_end_omp_structured_block (parser, save);
42656 return finish_omp_structured_block (stmt);
42659 /* OpenMP 5.x:
42660 # pragma omp allocate (list) clauses
42662 OpenMP 5.0 clause:
42663 allocator (omp_allocator_handle_t expression)
42665 OpenMP 5.1 additional clause:
42666 align (constant-expression)] */
42668 static void
42669 cp_parser_omp_allocate (cp_parser *parser, cp_token *pragma_tok)
42671 tree allocator = NULL_TREE;
42672 tree alignment = NULL_TREE;
42673 location_t loc = pragma_tok->location;
42674 tree nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_ALLOCATE, NULL_TREE);
42678 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
42679 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
42680 cp_lexer_consume_token (parser->lexer);
42682 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42683 break;
42684 matching_parens parens;
42685 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
42686 const char *p = IDENTIFIER_POINTER (id);
42687 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
42688 cp_lexer_consume_token (parser->lexer);
42689 if (strcmp (p, "allocator") != 0 && strcmp (p, "align") != 0)
42691 error_at (cloc, "expected %<allocator%> or %<align%>");
42692 break;
42694 if (!parens.require_open (parser))
42695 break;
42696 tree expr = cp_parser_assignment_expression (parser);
42697 if (p[2] == 'i' && alignment)
42699 error_at (cloc, "too many %qs clauses", "align");
42700 break;
42702 else if (p[2] == 'i')
42704 if (expr != error_mark_node)
42705 alignment = expr;
42706 /* FIXME: Remove when adding check to semantics.cc; cf FIXME below. */
42707 if (alignment
42708 && !type_dependent_expression_p (alignment)
42709 && !INTEGRAL_TYPE_P (TREE_TYPE (alignment)))
42711 error_at (cloc, "%<align%> clause argument needs to be "
42712 "positive constant power of two integer "
42713 "expression");
42714 alignment = NULL_TREE;
42716 else if (alignment)
42718 alignment = mark_rvalue_use (alignment);
42719 if (!processing_template_decl)
42721 alignment = maybe_constant_value (alignment);
42722 if (TREE_CODE (alignment) != INTEGER_CST
42723 || !tree_fits_uhwi_p (alignment)
42724 || !integer_pow2p (alignment))
42726 error_at (cloc, "%<align%> clause argument needs to be "
42727 "positive constant power of two integer "
42728 "expression");
42729 alignment = NULL_TREE;
42734 else if (allocator)
42736 error_at (cloc, "too many %qs clauses", "allocator");
42737 break;
42739 else
42741 if (expr != error_mark_node)
42742 allocator = expr;
42744 parens.require_close (parser);
42745 } while (true);
42746 cp_parser_require_pragma_eol (parser, pragma_tok);
42748 if (allocator || alignment)
42749 for (tree c = nl; c != NULL_TREE; c = OMP_CLAUSE_CHAIN (c))
42751 OMP_CLAUSE_ALLOCATE_ALLOCATOR (c) = allocator;
42752 OMP_CLAUSE_ALLOCATE_ALIGN (c) = alignment;
42755 /* FIXME: When implementing properly, delete the align/allocate expr error
42756 check above and add one in semantics.cc (to properly handle templates).
42757 Base this on the allocator/align modifiers check for the 'allocate' clause
42758 in semantics.cc's finish_omp_clauses. */
42759 sorry_at (loc, "%<#pragma omp allocate%> not yet supported");
42762 /* OpenMP 2.5:
42763 # pragma omp atomic new-line
42764 expression-stmt
42766 expression-stmt:
42767 x binop= expr | x++ | ++x | x-- | --x
42768 binop:
42769 +, *, -, /, &, ^, |, <<, >>
42771 where x is an lvalue expression with scalar type.
42773 OpenMP 3.1:
42774 # pragma omp atomic new-line
42775 update-stmt
42777 # pragma omp atomic read new-line
42778 read-stmt
42780 # pragma omp atomic write new-line
42781 write-stmt
42783 # pragma omp atomic update new-line
42784 update-stmt
42786 # pragma omp atomic capture new-line
42787 capture-stmt
42789 # pragma omp atomic capture new-line
42790 capture-block
42792 read-stmt:
42793 v = x
42794 write-stmt:
42795 x = expr
42796 update-stmt:
42797 expression-stmt | x = x binop expr
42798 capture-stmt:
42799 v = expression-stmt
42800 capture-block:
42801 { v = x; update-stmt; } | { update-stmt; v = x; }
42803 OpenMP 4.0:
42804 update-stmt:
42805 expression-stmt | x = x binop expr | x = expr binop x
42806 capture-stmt:
42807 v = update-stmt
42808 capture-block:
42809 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
42811 OpenMP 5.1:
42812 # pragma omp atomic compare new-line
42813 conditional-update-atomic
42815 # pragma omp atomic compare capture new-line
42816 conditional-update-capture-atomic
42818 conditional-update-atomic:
42819 cond-expr-stmt | cond-update-stmt
42820 cond-expr-stmt:
42821 x = expr ordop x ? expr : x;
42822 x = x ordop expr ? expr : x;
42823 x = x == e ? d : x;
42824 cond-update-stmt:
42825 if (expr ordop x) { x = expr; }
42826 if (x ordop expr) { x = expr; }
42827 if (x == e) { x = d; }
42828 ordop:
42829 <, >
42830 conditional-update-capture-atomic:
42831 v = cond-expr-stmt
42832 { v = x; cond-expr-stmt }
42833 { cond-expr-stmt v = x; }
42834 { v = x; cond-update-stmt }
42835 { cond-update-stmt v = x; }
42836 if (x == e) { x = d; } else { v = x; }
42837 { r = x == e; if (r) { x = d; } }
42838 { r = x == e; if (r) { x = d; } else { v = x; } }
42840 where x, r and v are lvalue expressions with scalar type,
42841 expr, e and d are expressions with scalar type and e might be
42842 the same as v. */
42844 static void
42845 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok, bool openacc)
42847 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
42848 tree rhs1 = NULL_TREE, orig_lhs, r = NULL_TREE;
42849 location_t loc = pragma_tok->location;
42850 enum tree_code code = ERROR_MARK, opcode = NOP_EXPR;
42851 enum omp_memory_order memory_order = OMP_MEMORY_ORDER_UNSPECIFIED;
42852 bool structured_block = false;
42853 tree clauses = NULL_TREE;
42854 bool capture = false;
42855 bool compare = false;
42856 bool weak = false;
42857 enum omp_memory_order fail = OMP_MEMORY_ORDER_UNSPECIFIED;
42858 bool no_semicolon = false;
42859 bool extra_scope = false;
42861 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
42863 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
42864 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
42865 cp_lexer_consume_token (parser->lexer);
42867 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42869 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
42870 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
42871 const char *p = IDENTIFIER_POINTER (id);
42872 enum tree_code new_code = ERROR_MARK;
42873 enum omp_memory_order new_memory_order
42874 = OMP_MEMORY_ORDER_UNSPECIFIED;
42875 bool new_capture = false;
42876 bool new_compare = false;
42877 bool new_weak = false;
42878 enum omp_memory_order new_fail = OMP_MEMORY_ORDER_UNSPECIFIED;
42880 if (!strcmp (p, "read"))
42881 new_code = OMP_ATOMIC_READ;
42882 else if (!strcmp (p, "write"))
42883 new_code = NOP_EXPR;
42884 else if (!strcmp (p, "update"))
42885 new_code = OMP_ATOMIC;
42886 else if (openacc && !strcmp (p, "capture"))
42887 new_code = OMP_ATOMIC_CAPTURE_NEW;
42888 else if (openacc)
42890 p = NULL;
42891 error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
42892 "or %<capture%> clause");
42894 else if (!strcmp (p, "capture"))
42895 new_capture = true;
42896 else if (!strcmp (p, "compare"))
42897 new_compare = true;
42898 else if (!strcmp (p, "weak"))
42899 new_weak = true;
42900 else if (!strcmp (p, "fail"))
42902 matching_parens parens;
42904 cp_lexer_consume_token (parser->lexer);
42905 if (!parens.require_open (parser))
42906 continue;
42908 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42910 id = cp_lexer_peek_token (parser->lexer)->u.value;
42911 const char *q = IDENTIFIER_POINTER (id);
42913 if (!strcmp (q, "seq_cst"))
42914 new_fail = OMP_MEMORY_ORDER_SEQ_CST;
42915 else if (!strcmp (q, "acquire"))
42916 new_fail = OMP_MEMORY_ORDER_ACQUIRE;
42917 else if (!strcmp (q, "relaxed"))
42918 new_fail = OMP_MEMORY_ORDER_RELAXED;
42921 if (new_fail != OMP_MEMORY_ORDER_UNSPECIFIED)
42923 cp_lexer_consume_token (parser->lexer);
42924 if (fail != OMP_MEMORY_ORDER_UNSPECIFIED)
42925 error_at (cloc, "too many %qs clauses", "fail");
42926 else
42927 fail = new_fail;
42929 else
42930 cp_parser_error (parser, "expected %<seq_cst%>, %<acquire%> "
42931 "or %<relaxed%>");
42932 if (new_fail == OMP_MEMORY_ORDER_UNSPECIFIED
42933 || !parens.require_close (parser))
42934 cp_parser_skip_to_closing_parenthesis (parser,
42935 /*recovering=*/true,
42936 /*or_comma=*/false,
42937 /*consume_paren=*/true);
42938 continue;
42940 else if (!strcmp (p, "seq_cst"))
42941 new_memory_order = OMP_MEMORY_ORDER_SEQ_CST;
42942 else if (!strcmp (p, "acq_rel"))
42943 new_memory_order = OMP_MEMORY_ORDER_ACQ_REL;
42944 else if (!strcmp (p, "release"))
42945 new_memory_order = OMP_MEMORY_ORDER_RELEASE;
42946 else if (!strcmp (p, "acquire"))
42947 new_memory_order = OMP_MEMORY_ORDER_ACQUIRE;
42948 else if (!strcmp (p, "relaxed"))
42949 new_memory_order = OMP_MEMORY_ORDER_RELAXED;
42950 else if (!strcmp (p, "hint"))
42952 cp_lexer_consume_token (parser->lexer);
42953 clauses = cp_parser_omp_clause_hint (parser, clauses, cloc);
42954 continue;
42956 else
42958 p = NULL;
42959 error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
42960 "%<capture%>, %<compare%>, %<weak%>, %<fail%>, "
42961 "%<seq_cst%>, %<acq_rel%>, %<release%>, "
42962 "%<relaxed%> or %<hint%> clause");
42964 if (p)
42966 if (new_code != ERROR_MARK)
42968 /* OpenACC permits 'update capture'. */
42969 if (openacc
42970 && code == OMP_ATOMIC
42971 && new_code == OMP_ATOMIC_CAPTURE_NEW)
42972 code = new_code;
42973 else if (code != ERROR_MARK)
42974 error_at (cloc, "too many atomic clauses");
42975 else
42976 code = new_code;
42978 else if (new_memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
42980 if (memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
42981 error_at (cloc, "too many memory order clauses");
42982 else
42983 memory_order = new_memory_order;
42985 else if (new_capture)
42987 if (capture)
42988 error_at (cloc, "too many %qs clauses", "capture");
42989 else
42990 capture = true;
42992 else if (new_compare)
42994 if (compare)
42995 error_at (cloc, "too many %qs clauses", "compare");
42996 else
42997 compare = true;
42999 else if (new_weak)
43001 if (weak)
43002 error_at (cloc, "too many %qs clauses", "weak");
43003 else
43004 weak = true;
43006 cp_lexer_consume_token (parser->lexer);
43007 continue;
43010 break;
43012 cp_parser_require_pragma_eol (parser, pragma_tok);
43014 if (code == ERROR_MARK)
43015 code = OMP_ATOMIC;
43016 if (capture)
43018 if (code != OMP_ATOMIC)
43019 error_at (loc, "%qs clause is incompatible with %<read%> or %<write%> "
43020 "clauses", "capture");
43021 else
43022 code = OMP_ATOMIC_CAPTURE_NEW;
43024 if (compare && code != OMP_ATOMIC && code != OMP_ATOMIC_CAPTURE_NEW)
43026 error_at (loc, "%qs clause is incompatible with %<read%> or %<write%> "
43027 "clauses", "compare");
43028 compare = false;
43030 if (fail != OMP_MEMORY_ORDER_UNSPECIFIED && !compare)
43032 error_at (loc, "%qs clause requires %qs clause", "fail", "compare");
43033 fail = OMP_MEMORY_ORDER_UNSPECIFIED;
43035 if (weak && !compare)
43037 error_at (loc, "%qs clause requires %qs clause", "weak", "compare");
43038 weak = false;
43040 if (openacc)
43041 memory_order = OMP_MEMORY_ORDER_RELAXED;
43042 else if (memory_order == OMP_MEMORY_ORDER_UNSPECIFIED)
43044 omp_requires_mask
43045 = (enum omp_requires) (omp_requires_mask
43046 | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED);
43047 switch ((enum omp_memory_order)
43048 (omp_requires_mask & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER))
43050 case OMP_MEMORY_ORDER_UNSPECIFIED:
43051 case OMP_MEMORY_ORDER_RELAXED:
43052 memory_order = OMP_MEMORY_ORDER_RELAXED;
43053 break;
43054 case OMP_MEMORY_ORDER_SEQ_CST:
43055 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
43056 break;
43057 case OMP_MEMORY_ORDER_ACQUIRE:
43058 if (code == NOP_EXPR) /* atomic write */
43060 error_at (loc, "%<#pragma omp atomic write%> incompatible with "
43061 "%<acquire%> clause implicitly provided by a "
43062 "%<requires%> directive");
43063 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
43065 else
43066 memory_order = OMP_MEMORY_ORDER_ACQUIRE;
43067 break;
43068 case OMP_MEMORY_ORDER_RELEASE:
43069 if (code == OMP_ATOMIC_READ)
43071 error_at (loc, "%<#pragma omp atomic read%> incompatible with "
43072 "%<release%> clause implicitly provided by a "
43073 "%<requires%> directive");
43074 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
43076 else
43077 memory_order = OMP_MEMORY_ORDER_RELEASE;
43078 break;
43079 case OMP_MEMORY_ORDER_ACQ_REL:
43080 switch (code)
43082 case OMP_ATOMIC_READ:
43083 memory_order = OMP_MEMORY_ORDER_ACQUIRE;
43084 break;
43085 case NOP_EXPR: /* atomic write */
43086 memory_order = OMP_MEMORY_ORDER_RELEASE;
43087 break;
43088 default:
43089 memory_order = OMP_MEMORY_ORDER_ACQ_REL;
43090 break;
43092 break;
43093 default:
43094 gcc_unreachable ();
43097 else
43098 switch (code)
43100 case OMP_ATOMIC_READ:
43101 if (memory_order == OMP_MEMORY_ORDER_RELEASE)
43103 error_at (loc, "%<#pragma omp atomic read%> incompatible with "
43104 "%<release%> clause");
43105 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
43107 else if (memory_order == OMP_MEMORY_ORDER_ACQ_REL)
43108 memory_order = OMP_MEMORY_ORDER_ACQUIRE;
43109 break;
43110 case NOP_EXPR: /* atomic write */
43111 if (memory_order == OMP_MEMORY_ORDER_ACQUIRE)
43113 error_at (loc, "%<#pragma omp atomic write%> incompatible with "
43114 "%<acquire%> clause");
43115 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
43117 else if (memory_order == OMP_MEMORY_ORDER_ACQ_REL)
43118 memory_order = OMP_MEMORY_ORDER_RELEASE;
43119 break;
43120 default:
43121 break;
43123 if (fail != OMP_MEMORY_ORDER_UNSPECIFIED)
43124 memory_order
43125 = (enum omp_memory_order) (memory_order
43126 | (fail << OMP_FAIL_MEMORY_ORDER_SHIFT));
43128 switch (code)
43130 case OMP_ATOMIC_READ:
43131 case NOP_EXPR: /* atomic write */
43132 v = cp_parser_unary_expression (parser);
43133 if (v == error_mark_node)
43134 goto saw_error;
43135 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
43136 goto saw_error;
43137 if (code == NOP_EXPR)
43138 lhs = cp_parser_expression (parser);
43139 else
43140 lhs = cp_parser_unary_expression (parser);
43141 if (lhs == error_mark_node)
43142 goto saw_error;
43143 if (code == NOP_EXPR)
43145 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
43146 opcode. */
43147 code = OMP_ATOMIC;
43148 rhs = lhs;
43149 lhs = v;
43150 v = NULL_TREE;
43152 goto done;
43153 case OMP_ATOMIC_CAPTURE_NEW:
43154 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
43156 cp_lexer_consume_token (parser->lexer);
43157 structured_block = true;
43159 else if (compare
43160 && cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
43161 break;
43162 else
43164 v = cp_parser_unary_expression (parser);
43165 if (v == error_mark_node)
43166 goto saw_error;
43167 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
43168 goto saw_error;
43169 if (compare
43170 && cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
43172 location_t eloc = cp_lexer_peek_token (parser->lexer)->location;
43173 error_at (eloc, "expected expression");
43174 goto saw_error;
43177 default:
43178 break;
43181 restart:
43182 if (compare && cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
43184 cp_lexer_consume_token (parser->lexer);
43186 matching_parens parens;
43187 if (!parens.require_open (parser))
43188 goto saw_error;
43189 location_t eloc = cp_lexer_peek_token (parser->lexer)->location;
43190 tree cmp_expr;
43191 if (r)
43192 cmp_expr = cp_parser_unary_expression (parser);
43193 else
43194 cmp_expr = cp_parser_binary_expression (parser, false, true,
43195 PREC_NOT_OPERATOR, NULL);
43196 if (!parens.require_close (parser))
43197 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
43198 if (cmp_expr == error_mark_node)
43199 goto saw_error;
43200 if (r)
43202 if (!cp_tree_equal (cmp_expr, r))
43203 goto bad_if;
43204 cmp_expr = rhs;
43205 rhs = NULL_TREE;
43206 gcc_assert (TREE_CODE (cmp_expr) == EQ_EXPR);
43208 if (TREE_CODE (cmp_expr) == EQ_EXPR)
43210 else if (!structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
43212 error_at (EXPR_LOC_OR_LOC (cmp_expr, eloc),
43213 "expected %<==%> comparison in %<if%> condition");
43214 goto saw_error;
43216 else if (TREE_CODE (cmp_expr) != GT_EXPR
43217 && TREE_CODE (cmp_expr) != LT_EXPR)
43219 error_at (EXPR_LOC_OR_LOC (cmp_expr, eloc),
43220 "expected %<==%>, %<<%> or %<>%> comparison in %<if%> "
43221 "condition");
43222 goto saw_error;
43224 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
43225 goto saw_error;
43227 extra_scope = true;
43228 eloc = cp_lexer_peek_token (parser->lexer)->location;
43229 lhs = cp_parser_unary_expression (parser);
43230 orig_lhs = lhs;
43231 if (lhs == error_mark_node)
43232 goto saw_error;
43233 if (!cp_lexer_next_token_is (parser->lexer, CPP_EQ))
43235 cp_parser_error (parser, "expected %<=%>");
43236 goto saw_error;
43238 cp_lexer_consume_token (parser->lexer);
43239 eloc = cp_lexer_peek_token (parser->lexer)->location;
43240 if (TREE_CODE (cmp_expr) == EQ_EXPR)
43241 rhs1 = cp_parser_expression (parser);
43242 else
43243 rhs1 = cp_parser_simple_cast_expression (parser);
43245 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
43246 goto saw_error;
43248 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
43249 goto saw_error;
43251 extra_scope = false;
43252 no_semicolon = true;
43254 if (cp_tree_equal (TREE_OPERAND (cmp_expr, 0), lhs))
43256 if (TREE_CODE (cmp_expr) == EQ_EXPR)
43258 opcode = COND_EXPR;
43259 rhs = TREE_OPERAND (cmp_expr, 1);
43261 else if (cp_tree_equal (TREE_OPERAND (cmp_expr, 1), rhs1))
43263 opcode = (TREE_CODE (cmp_expr) == GT_EXPR
43264 ? MIN_EXPR : MAX_EXPR);
43265 rhs = rhs1;
43266 rhs1 = TREE_OPERAND (cmp_expr, 0);
43268 else
43269 goto bad_if;
43271 else if (TREE_CODE (cmp_expr) == EQ_EXPR)
43272 goto bad_if;
43273 else if (cp_tree_equal (TREE_OPERAND (cmp_expr, 1), lhs)
43274 && cp_tree_equal (TREE_OPERAND (cmp_expr, 0), rhs1))
43276 opcode = (TREE_CODE (cmp_expr) == GT_EXPR
43277 ? MAX_EXPR : MIN_EXPR);
43278 rhs = rhs1;
43279 rhs1 = TREE_OPERAND (cmp_expr, 1);
43281 else
43283 bad_if:
43284 cp_parser_error (parser,
43285 "invalid form of %<#pragma omp atomic compare%>");
43286 goto saw_error;
43289 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
43291 if (code != OMP_ATOMIC_CAPTURE_NEW
43292 || (structured_block && r == NULL_TREE)
43293 || TREE_CODE (cmp_expr) != EQ_EXPR)
43295 eloc = cp_lexer_peek_token (parser->lexer)->location;
43296 error_at (eloc, "unexpected %<else%>");
43297 goto saw_error;
43300 cp_lexer_consume_token (parser->lexer);
43302 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
43303 goto saw_error;
43305 extra_scope = true;
43306 v = cp_parser_unary_expression (parser);
43307 if (v == error_mark_node)
43308 goto saw_error;
43309 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
43310 goto saw_error;
43312 tree expr = cp_parser_simple_cast_expression (parser);
43314 if (!cp_tree_equal (expr, lhs))
43315 goto bad_if;
43317 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
43318 goto saw_error;
43320 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
43321 goto saw_error;
43323 extra_scope = false;
43324 code = OMP_ATOMIC_CAPTURE_OLD;
43325 if (r == NULL_TREE)
43326 /* Signal to c_finish_omp_atomic that in
43327 if (x == e) { x = d; } else { v = x; }
43328 case the store to v should be conditional. */
43329 r = void_list_node;
43331 else if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
43333 cp_parser_error (parser, "expected %<else%>");
43334 goto saw_error;
43336 else if (code == OMP_ATOMIC_CAPTURE_NEW
43337 && r != NULL_TREE
43338 && v == NULL_TREE)
43339 code = OMP_ATOMIC;
43340 goto stmt_done;
43342 lhs = cp_parser_unary_expression (parser);
43343 orig_lhs = lhs;
43344 switch (TREE_CODE (lhs))
43346 case ERROR_MARK:
43347 goto saw_error;
43349 case POSTINCREMENT_EXPR:
43350 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
43351 code = OMP_ATOMIC_CAPTURE_OLD;
43352 /* FALLTHROUGH */
43353 case PREINCREMENT_EXPR:
43354 lhs = TREE_OPERAND (lhs, 0);
43355 opcode = PLUS_EXPR;
43356 rhs = integer_one_node;
43357 if (compare)
43358 goto invalid_compare;
43359 break;
43361 case POSTDECREMENT_EXPR:
43362 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
43363 code = OMP_ATOMIC_CAPTURE_OLD;
43364 /* FALLTHROUGH */
43365 case PREDECREMENT_EXPR:
43366 lhs = TREE_OPERAND (lhs, 0);
43367 opcode = MINUS_EXPR;
43368 rhs = integer_one_node;
43369 if (compare)
43370 goto invalid_compare;
43371 break;
43373 case COMPOUND_EXPR:
43374 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
43375 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
43376 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
43377 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
43378 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
43379 (TREE_OPERAND (lhs, 1), 0), 0)))
43380 == BOOLEAN_TYPE)
43381 /* Undo effects of boolean_increment for post {in,de}crement. */
43382 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
43383 /* FALLTHRU */
43384 case MODIFY_EXPR:
43385 if (TREE_CODE (lhs) == MODIFY_EXPR
43386 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
43388 /* Undo effects of boolean_increment. */
43389 if (integer_onep (TREE_OPERAND (lhs, 1)))
43391 /* This is pre or post increment. */
43392 rhs = TREE_OPERAND (lhs, 1);
43393 lhs = TREE_OPERAND (lhs, 0);
43394 opcode = NOP_EXPR;
43395 if (code == OMP_ATOMIC_CAPTURE_NEW
43396 && !structured_block
43397 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
43398 code = OMP_ATOMIC_CAPTURE_OLD;
43399 if (compare)
43400 goto invalid_compare;
43401 break;
43404 /* FALLTHRU */
43405 default:
43406 if (compare && !cp_lexer_next_token_is (parser->lexer, CPP_EQ))
43408 cp_parser_error (parser, "expected %<=%>");
43409 goto saw_error;
43411 switch (cp_lexer_peek_token (parser->lexer)->type)
43413 case CPP_MULT_EQ:
43414 opcode = MULT_EXPR;
43415 break;
43416 case CPP_DIV_EQ:
43417 opcode = TRUNC_DIV_EXPR;
43418 break;
43419 case CPP_PLUS_EQ:
43420 opcode = PLUS_EXPR;
43421 break;
43422 case CPP_MINUS_EQ:
43423 opcode = MINUS_EXPR;
43424 break;
43425 case CPP_LSHIFT_EQ:
43426 opcode = LSHIFT_EXPR;
43427 break;
43428 case CPP_RSHIFT_EQ:
43429 opcode = RSHIFT_EXPR;
43430 break;
43431 case CPP_AND_EQ:
43432 opcode = BIT_AND_EXPR;
43433 break;
43434 case CPP_OR_EQ:
43435 opcode = BIT_IOR_EXPR;
43436 break;
43437 case CPP_XOR_EQ:
43438 opcode = BIT_XOR_EXPR;
43439 break;
43440 case CPP_EQ:
43441 enum cp_parser_prec oprec;
43442 cp_token *token;
43443 cp_lexer_consume_token (parser->lexer);
43444 cp_parser_parse_tentatively (parser);
43445 rhs1 = cp_parser_simple_cast_expression (parser);
43446 if (rhs1 == error_mark_node)
43448 cp_parser_abort_tentative_parse (parser);
43449 cp_parser_simple_cast_expression (parser);
43450 goto saw_error;
43452 token = cp_lexer_peek_token (parser->lexer);
43453 if (token->type != CPP_SEMICOLON
43454 && (!compare || token->type != CPP_QUERY)
43455 && !cp_tree_equal (lhs, rhs1))
43457 cp_parser_abort_tentative_parse (parser);
43458 cp_parser_parse_tentatively (parser);
43459 rhs = cp_parser_binary_expression (parser, false, true,
43460 PREC_NOT_OPERATOR, NULL);
43461 if (rhs == error_mark_node)
43463 cp_parser_abort_tentative_parse (parser);
43464 cp_parser_binary_expression (parser, false, true,
43465 PREC_NOT_OPERATOR, NULL);
43466 goto saw_error;
43468 switch (TREE_CODE (rhs))
43470 case MULT_EXPR:
43471 case TRUNC_DIV_EXPR:
43472 case RDIV_EXPR:
43473 case PLUS_EXPR:
43474 case MINUS_EXPR:
43475 case LSHIFT_EXPR:
43476 case RSHIFT_EXPR:
43477 case BIT_AND_EXPR:
43478 case BIT_IOR_EXPR:
43479 case BIT_XOR_EXPR:
43480 if (compare)
43481 break;
43482 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
43484 if (cp_parser_parse_definitely (parser))
43486 opcode = TREE_CODE (rhs);
43487 rhs1 = TREE_OPERAND (rhs, 0);
43488 rhs = TREE_OPERAND (rhs, 1);
43489 goto stmt_done;
43491 else
43492 goto saw_error;
43494 break;
43495 case EQ_EXPR:
43496 if (!compare
43497 || code != OMP_ATOMIC_CAPTURE_NEW
43498 || !structured_block
43499 || v
43500 || r)
43501 break;
43502 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
43503 && cp_lexer_nth_token_is_keyword (parser->lexer,
43504 2, RID_IF))
43506 if (cp_parser_parse_definitely (parser))
43508 r = lhs;
43509 lhs = NULL_TREE;
43510 rhs1 = NULL_TREE;
43511 cp_lexer_consume_token (parser->lexer);
43512 goto restart;
43515 break;
43516 case GT_EXPR:
43517 case LT_EXPR:
43518 if (compare
43519 && cp_lexer_next_token_is (parser->lexer, CPP_QUERY)
43520 && cp_tree_equal (lhs, TREE_OPERAND (rhs, 1))
43521 && cp_parser_parse_definitely (parser))
43523 opcode = TREE_CODE (rhs);
43524 rhs1 = TREE_OPERAND (rhs, 0);
43525 rhs = TREE_OPERAND (rhs, 1);
43526 cond_expr:
43527 cp_lexer_consume_token (parser->lexer);
43528 bool saved_colon_corrects_to_scope_p
43529 = parser->colon_corrects_to_scope_p;
43530 parser->colon_corrects_to_scope_p = false;
43531 tree e1 = cp_parser_expression (parser);
43532 parser->colon_corrects_to_scope_p
43533 = saved_colon_corrects_to_scope_p;
43534 cp_parser_require (parser, CPP_COLON, RT_COLON);
43535 tree e2 = cp_parser_simple_cast_expression (parser);
43536 if (cp_tree_equal (lhs, e2))
43538 if (cp_tree_equal (lhs, rhs1))
43540 if (opcode == EQ_EXPR)
43542 opcode = COND_EXPR;
43543 rhs1 = e1;
43544 goto stmt_done;
43546 if (cp_tree_equal (rhs, e1))
43548 opcode
43549 = opcode == GT_EXPR ? MIN_EXPR : MAX_EXPR;
43550 rhs = e1;
43551 goto stmt_done;
43554 else
43556 gcc_assert (opcode != EQ_EXPR);
43557 if (cp_tree_equal (rhs1, e1))
43559 opcode
43560 = opcode == GT_EXPR ? MAX_EXPR : MIN_EXPR;
43561 rhs1 = rhs;
43562 rhs = e1;
43563 goto stmt_done;
43567 cp_parser_error (parser,
43568 "invalid form of "
43569 "%<#pragma omp atomic compare%>");
43570 goto saw_error;
43572 break;
43573 default:
43574 break;
43576 cp_parser_abort_tentative_parse (parser);
43577 if (structured_block
43578 && code == OMP_ATOMIC_CAPTURE_OLD
43579 && !compare)
43581 rhs = cp_parser_expression (parser);
43582 if (rhs == error_mark_node)
43583 goto saw_error;
43584 opcode = NOP_EXPR;
43585 rhs1 = NULL_TREE;
43586 goto stmt_done;
43588 cp_parser_error (parser,
43589 "invalid form of %<#pragma omp atomic%>");
43590 goto saw_error;
43592 if (!cp_parser_parse_definitely (parser))
43593 goto saw_error;
43594 switch (token->type)
43596 case CPP_SEMICOLON:
43597 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
43599 code = OMP_ATOMIC_CAPTURE_OLD;
43600 v = lhs;
43601 lhs = NULL_TREE;
43602 lhs1 = rhs1;
43603 rhs1 = NULL_TREE;
43604 cp_lexer_consume_token (parser->lexer);
43605 goto restart;
43607 else if (structured_block && !compare)
43609 opcode = NOP_EXPR;
43610 rhs = rhs1;
43611 rhs1 = NULL_TREE;
43612 goto stmt_done;
43614 cp_parser_error (parser,
43615 "invalid form of %<#pragma omp atomic%>");
43616 goto saw_error;
43617 case CPP_MULT:
43618 opcode = MULT_EXPR;
43619 break;
43620 case CPP_DIV:
43621 opcode = TRUNC_DIV_EXPR;
43622 break;
43623 case CPP_PLUS:
43624 opcode = PLUS_EXPR;
43625 break;
43626 case CPP_MINUS:
43627 opcode = MINUS_EXPR;
43628 break;
43629 case CPP_LSHIFT:
43630 opcode = LSHIFT_EXPR;
43631 break;
43632 case CPP_RSHIFT:
43633 opcode = RSHIFT_EXPR;
43634 break;
43635 case CPP_AND:
43636 opcode = BIT_AND_EXPR;
43637 break;
43638 case CPP_OR:
43639 opcode = BIT_IOR_EXPR;
43640 break;
43641 case CPP_XOR:
43642 opcode = BIT_XOR_EXPR;
43643 break;
43644 case CPP_EQ_EQ:
43645 opcode = EQ_EXPR;
43646 break;
43647 case CPP_GREATER:
43648 opcode = GT_EXPR;
43649 break;
43650 case CPP_LESS:
43651 opcode = LT_EXPR;
43652 break;
43653 default:
43654 cp_parser_error (parser,
43655 "invalid operator for %<#pragma omp atomic%>");
43656 goto saw_error;
43658 if (compare
43659 && TREE_CODE_CLASS (opcode) != tcc_comparison)
43661 cp_parser_error (parser,
43662 "invalid form of "
43663 "%<#pragma omp atomic compare%>");
43664 goto saw_error;
43666 oprec = TOKEN_PRECEDENCE (token);
43667 gcc_assert (oprec != PREC_NOT_OPERATOR);
43668 if (commutative_tree_code (opcode))
43669 oprec = (enum cp_parser_prec) (oprec - 1);
43670 cp_lexer_consume_token (parser->lexer);
43671 rhs = cp_parser_binary_expression (parser, false, false,
43672 oprec, NULL);
43673 if (rhs == error_mark_node)
43674 goto saw_error;
43675 if (compare)
43677 if (!cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
43679 cp_parser_error (parser,
43680 "invalid form of "
43681 "%<#pragma omp atomic compare%>");
43682 goto saw_error;
43684 goto cond_expr;
43686 goto stmt_done;
43687 default:
43688 cp_parser_error (parser,
43689 "invalid operator for %<#pragma omp atomic%>");
43690 goto saw_error;
43692 cp_lexer_consume_token (parser->lexer);
43694 rhs = cp_parser_expression (parser);
43695 if (rhs == error_mark_node)
43696 goto saw_error;
43697 break;
43699 stmt_done:
43700 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW && r == NULL_TREE)
43702 if (!no_semicolon
43703 && !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
43704 goto saw_error;
43705 no_semicolon = false;
43706 v = cp_parser_unary_expression (parser);
43707 if (v == error_mark_node)
43708 goto saw_error;
43709 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
43710 goto saw_error;
43711 lhs1 = cp_parser_unary_expression (parser);
43712 if (lhs1 == error_mark_node)
43713 goto saw_error;
43715 if (structured_block)
43717 if (!no_semicolon)
43718 cp_parser_consume_semicolon_at_end_of_statement (parser);
43719 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
43721 done:
43722 if (weak && opcode != COND_EXPR)
43724 error_at (loc, "%<weak%> clause requires atomic equality comparison");
43725 weak = false;
43727 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
43728 finish_omp_atomic (pragma_tok->location, code, opcode, lhs, rhs, v, lhs1,
43729 rhs1, r, clauses, memory_order, weak);
43730 if (!structured_block && !no_semicolon)
43731 cp_parser_consume_semicolon_at_end_of_statement (parser);
43732 return;
43734 invalid_compare:
43735 error ("invalid form of %<pragma omp atomic compare%>");
43736 /* FALLTHRU */
43737 saw_error:
43738 cp_parser_skip_to_end_of_block_or_statement (parser);
43739 if (extra_scope && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
43740 cp_lexer_consume_token (parser->lexer);
43741 if (structured_block)
43743 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
43744 cp_lexer_consume_token (parser->lexer);
43745 else if (code == OMP_ATOMIC_CAPTURE_NEW)
43747 cp_parser_skip_to_end_of_block_or_statement (parser);
43748 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
43749 cp_lexer_consume_token (parser->lexer);
43755 /* OpenMP 2.5:
43756 # pragma omp barrier new-line */
43758 static void
43759 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
43761 cp_parser_require_pragma_eol (parser, pragma_tok);
43762 finish_omp_barrier ();
43765 /* OpenMP 2.5:
43766 # pragma omp critical [(name)] new-line
43767 structured-block
43769 OpenMP 4.5:
43770 # pragma omp critical [(name) [hint(expression)]] new-line
43771 structured-block */
43773 #define OMP_CRITICAL_CLAUSE_MASK \
43774 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
43776 static tree
43777 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
43779 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
43781 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
43783 matching_parens parens;
43784 parens.consume_open (parser);
43786 name = cp_parser_identifier (parser);
43788 if (name == error_mark_node
43789 || !parens.require_close (parser))
43790 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
43791 /*or_comma=*/false,
43792 /*consume_paren=*/true);
43793 if (name == error_mark_node)
43794 name = NULL;
43796 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
43797 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
43798 cp_lexer_consume_token (parser->lexer);
43801 clauses = cp_parser_omp_all_clauses (parser, OMP_CRITICAL_CLAUSE_MASK,
43802 "#pragma omp critical", pragma_tok);
43804 stmt = cp_parser_omp_structured_block (parser, if_p);
43805 return c_finish_omp_critical (input_location, stmt, name, clauses);
43808 /* OpenMP 5.0:
43809 # pragma omp depobj ( depobj ) depobj-clause new-line
43811 depobj-clause:
43812 depend (dependence-type : locator)
43813 destroy
43814 update (dependence-type)
43816 OpenMP 5.2 additionally:
43817 destroy ( depobj )
43819 dependence-type:
43822 inout
43823 mutexinout */
43825 static void
43826 cp_parser_omp_depobj (cp_parser *parser, cp_token *pragma_tok)
43828 location_t loc = pragma_tok->location;
43829 matching_parens parens;
43830 if (!parens.require_open (parser))
43832 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43833 return;
43836 tree depobj = cp_parser_assignment_expression (parser);
43838 if (!parens.require_close (parser))
43839 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
43840 /*or_comma=*/false,
43841 /*consume_paren=*/true);
43843 tree clause = NULL_TREE;
43844 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INVALID;
43845 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
43846 cp_lexer_consume_token (parser->lexer);
43847 location_t c_loc = cp_lexer_peek_token (parser->lexer)->location;
43848 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43850 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43851 const char *p = IDENTIFIER_POINTER (id);
43853 cp_lexer_consume_token (parser->lexer);
43854 if (!strcmp ("depend", p))
43856 /* Don't create location wrapper nodes within the depend clause. */
43857 auto_suppress_location_wrappers sentinel;
43858 clause = cp_parser_omp_clause_depend (parser, NULL_TREE, c_loc);
43859 if (clause)
43860 clause = finish_omp_clauses (clause, C_ORT_OMP);
43861 if (!clause)
43862 clause = error_mark_node;
43864 else if (!strcmp ("destroy", p))
43866 kind = OMP_CLAUSE_DEPEND_LAST;
43867 matching_parens c_parens;
43868 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
43869 && c_parens.require_open (parser))
43871 tree destobj = cp_parser_assignment_expression (parser);
43872 if (depobj != error_mark_node
43873 && destobj != error_mark_node
43874 && !operand_equal_p (destobj, depobj, OEP_MATCH_SIDE_EFFECTS
43875 | OEP_LEXICOGRAPHIC))
43876 warning_at (EXPR_LOC_OR_LOC (destobj, c_loc), OPT_Wopenmp,
43877 "the %<destroy%> expression %qE should be the same "
43878 "as the %<depobj%> argument %qE", destobj, depobj);
43879 if (!c_parens.require_close (parser))
43880 cp_parser_skip_to_closing_parenthesis (parser,
43881 /*recovering=*/true,
43882 /*or_comma=*/false,
43883 /*consume_paren=*/true);
43886 else if (!strcmp ("update", p))
43888 matching_parens c_parens;
43889 if (c_parens.require_open (parser))
43891 location_t c2_loc
43892 = cp_lexer_peek_token (parser->lexer)->location;
43893 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43895 tree id2 = cp_lexer_peek_token (parser->lexer)->u.value;
43896 const char *p2 = IDENTIFIER_POINTER (id2);
43898 cp_lexer_consume_token (parser->lexer);
43899 if (!strcmp ("in", p2))
43900 kind = OMP_CLAUSE_DEPEND_IN;
43901 else if (!strcmp ("out", p2))
43902 kind = OMP_CLAUSE_DEPEND_OUT;
43903 else if (!strcmp ("inout", p2))
43904 kind = OMP_CLAUSE_DEPEND_INOUT;
43905 else if (!strcmp ("mutexinoutset", p2))
43906 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
43907 else if (!strcmp ("inoutset", p2))
43908 kind = OMP_CLAUSE_DEPEND_INOUTSET;
43910 if (kind == OMP_CLAUSE_DEPEND_INVALID)
43912 clause = error_mark_node;
43913 error_at (c2_loc, "expected %<in%>, %<out%>, %<inout%>, "
43914 "%<mutexinoutset%> or %<inoutset%>");
43916 if (!c_parens.require_close (parser))
43917 cp_parser_skip_to_closing_parenthesis (parser,
43918 /*recovering=*/true,
43919 /*or_comma=*/false,
43920 /*consume_paren=*/true);
43922 else
43923 clause = error_mark_node;
43926 if (!clause && kind == OMP_CLAUSE_DEPEND_INVALID)
43928 clause = error_mark_node;
43929 error_at (c_loc, "expected %<depend%>, %<destroy%> or %<update%> clause");
43931 cp_parser_require_pragma_eol (parser, pragma_tok);
43933 finish_omp_depobj (loc, depobj, kind, clause);
43937 /* OpenMP 2.5:
43938 # pragma omp flush flush-vars[opt] new-line
43940 flush-vars:
43941 ( variable-list )
43943 OpenMP 5.0:
43944 # pragma omp flush memory-order-clause new-line */
43946 static void
43947 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
43949 enum memmodel mo = MEMMODEL_LAST;
43950 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
43951 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
43952 cp_lexer_consume_token (parser->lexer);
43953 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43955 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43956 const char *p = IDENTIFIER_POINTER (id);
43957 if (!strcmp (p, "seq_cst"))
43958 mo = MEMMODEL_SEQ_CST;
43959 else if (!strcmp (p, "acq_rel"))
43960 mo = MEMMODEL_ACQ_REL;
43961 else if (!strcmp (p, "release"))
43962 mo = MEMMODEL_RELEASE;
43963 else if (!strcmp (p, "acquire"))
43964 mo = MEMMODEL_ACQUIRE;
43965 else
43966 error_at (cp_lexer_peek_token (parser->lexer)->location,
43967 "expected %<seq_cst%>, %<acq_rel%>, %<release%> or "
43968 "%<acquire%>");
43969 cp_lexer_consume_token (parser->lexer);
43971 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
43973 if (mo != MEMMODEL_LAST)
43974 error_at (cp_lexer_peek_token (parser->lexer)->location,
43975 "%<flush%> list specified together with memory order "
43976 "clause");
43977 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
43979 cp_parser_require_pragma_eol (parser, pragma_tok);
43981 finish_omp_flush (mo);
43984 /* Helper function, to parse omp for increment expression. */
43986 static tree
43987 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
43989 tree cond = cp_parser_binary_expression (parser, false, true,
43990 PREC_NOT_OPERATOR, NULL);
43991 if (cond == error_mark_node
43992 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
43994 cp_parser_skip_to_end_of_statement (parser);
43995 return error_mark_node;
43998 switch (TREE_CODE (cond))
44000 case GT_EXPR:
44001 case GE_EXPR:
44002 case LT_EXPR:
44003 case LE_EXPR:
44004 break;
44005 case NE_EXPR:
44006 if (code != OACC_LOOP)
44007 break;
44008 gcc_fallthrough ();
44009 default:
44010 return error_mark_node;
44013 /* If decl is an iterator, preserve LHS and RHS of the relational
44014 expr until finish_omp_for. */
44015 if (decl
44016 && (type_dependent_expression_p (decl)
44017 || CLASS_TYPE_P (TREE_TYPE (decl))))
44018 return cond;
44020 return build_x_binary_op (cp_expr_loc_or_input_loc (cond),
44021 TREE_CODE (cond),
44022 TREE_OPERAND (cond, 0), ERROR_MARK,
44023 TREE_OPERAND (cond, 1), ERROR_MARK,
44024 NULL_TREE, /*overload=*/NULL, tf_warning_or_error);
44027 /* Helper function, to parse omp for increment expression. */
44029 static tree
44030 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
44032 cp_token *token = cp_lexer_peek_token (parser->lexer);
44033 enum tree_code op;
44034 tree lhs, rhs;
44035 cp_id_kind idk;
44036 bool decl_first;
44038 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
44040 op = (token->type == CPP_PLUS_PLUS
44041 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
44042 cp_lexer_consume_token (parser->lexer);
44043 lhs = cp_parser_simple_cast_expression (parser);
44044 if (lhs != decl
44045 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
44046 return error_mark_node;
44047 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
44050 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
44051 if (lhs != decl
44052 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
44053 return error_mark_node;
44055 token = cp_lexer_peek_token (parser->lexer);
44056 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
44058 op = (token->type == CPP_PLUS_PLUS
44059 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
44060 cp_lexer_consume_token (parser->lexer);
44061 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
44064 op = cp_parser_assignment_operator_opt (parser);
44065 if (op == ERROR_MARK)
44066 return error_mark_node;
44068 if (op != NOP_EXPR)
44070 rhs = cp_parser_assignment_expression (parser);
44071 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
44072 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
44075 lhs = cp_parser_binary_expression (parser, false, false,
44076 PREC_ADDITIVE_EXPRESSION, NULL);
44077 token = cp_lexer_peek_token (parser->lexer);
44078 decl_first = (lhs == decl
44079 || (processing_template_decl && cp_tree_equal (lhs, decl)));
44080 if (decl_first)
44081 lhs = NULL_TREE;
44082 if (token->type != CPP_PLUS
44083 && token->type != CPP_MINUS)
44084 return error_mark_node;
44088 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
44089 cp_lexer_consume_token (parser->lexer);
44090 rhs = cp_parser_binary_expression (parser, false, false,
44091 PREC_ADDITIVE_EXPRESSION, NULL);
44092 token = cp_lexer_peek_token (parser->lexer);
44093 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
44095 if (lhs == NULL_TREE)
44097 if (op == PLUS_EXPR)
44098 lhs = rhs;
44099 else
44100 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
44101 NULL_TREE, tf_warning_or_error);
44103 else
44104 lhs = build_x_binary_op (input_location, op,
44105 lhs, ERROR_MARK,
44106 rhs, ERROR_MARK,
44107 NULL_TREE, NULL, tf_warning_or_error);
44110 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
44112 if (!decl_first)
44114 if ((rhs != decl
44115 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
44116 || op == MINUS_EXPR)
44117 return error_mark_node;
44118 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
44120 else
44121 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
44123 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
44126 /* Parse the initialization statement of an OpenMP for loop. Range-for
44127 is handled separately in cp_convert_omp_range_for.
44129 On entry SL is the current statement list. Parsing of some forms
44130 of initialization pops this list and stores its contents in either INIT
44131 or THIS_PRE_BODY, and sets SL to null. Initialization for class
44132 iterators is added directly to SL and it is not popped until later.
44134 On return, DECL is set if the initialization is by binding the
44135 iteration variable. If the initialization is by assignment, REAL_DECL
44136 is set to point to a variable in an outer scope. ORIG_INIT is set
44137 if the iteration variable is of class type; this is a copy saved for
44138 error checking in finish_omp_for.
44140 Return true if the resulting construct should have an
44141 OMP_CLAUSE_PRIVATE added to it. */
44143 static tree
44144 cp_parser_omp_for_loop_init (cp_parser *parser,
44145 tree &this_pre_body,
44146 tree &sl,
44147 tree &init,
44148 tree &orig_init,
44149 tree &decl,
44150 tree &real_decl)
44152 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
44153 return NULL_TREE;
44155 tree add_private_clause = NULL_TREE;
44157 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
44159 init-expr:
44160 var = lb
44161 integer-type var = lb
44162 random-access-iterator-type var = lb
44163 pointer-type var = lb
44165 cp_decl_specifier_seq type_specifiers;
44167 /* First, try to parse as an initialized declaration. See
44168 cp_parser_condition, from whence the bulk of this is copied. */
44170 cp_parser_parse_tentatively (parser);
44171 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
44172 /*is_declaration=*/true,
44173 /*is_trailing_return=*/false,
44174 &type_specifiers);
44175 if (cp_parser_parse_definitely (parser))
44177 /* If parsing a type specifier seq succeeded, then this
44178 MUST be a initialized declaration. */
44179 tree asm_specification, attributes;
44180 cp_declarator *declarator;
44182 declarator = cp_parser_declarator (parser,
44183 CP_PARSER_DECLARATOR_NAMED,
44184 CP_PARSER_FLAGS_NONE,
44185 /*ctor_dtor_or_conv_p=*/NULL,
44186 /*parenthesized_p=*/NULL,
44187 /*member_p=*/false,
44188 /*friend_p=*/false,
44189 /*static_p=*/false);
44190 attributes = cp_parser_attributes_opt (parser);
44191 asm_specification = cp_parser_asm_specification_opt (parser);
44193 if (declarator == cp_error_declarator)
44194 cp_parser_skip_to_end_of_statement (parser);
44196 else
44198 tree pushed_scope, auto_node;
44200 decl = start_decl (declarator, &type_specifiers,
44201 SD_INITIALIZED, attributes,
44202 /*prefix_attributes=*/NULL_TREE,
44203 &pushed_scope);
44205 auto_node = type_uses_auto (TREE_TYPE (decl));
44206 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
44208 if (cp_lexer_next_token_is (parser->lexer,
44209 CPP_OPEN_PAREN))
44210 error ("parenthesized initialization is not allowed in "
44211 "OpenMP %<for%> loop");
44212 else
44213 /* Trigger an error. */
44214 cp_parser_require (parser, CPP_EQ, RT_EQ);
44216 init = error_mark_node;
44217 cp_parser_skip_to_end_of_statement (parser);
44219 else if (CLASS_TYPE_P (TREE_TYPE (decl))
44220 || type_dependent_expression_p (decl)
44221 || auto_node)
44223 bool is_non_constant_init;
44225 init = cp_parser_initializer (parser,
44226 /*is_direct_init=*/nullptr,
44227 &is_non_constant_init);
44229 if (auto_node)
44231 TREE_TYPE (decl)
44232 = do_auto_deduction (TREE_TYPE (decl), init,
44233 auto_node);
44235 if (!CLASS_TYPE_P (TREE_TYPE (decl))
44236 && !type_dependent_expression_p (decl))
44237 goto non_class;
44240 cp_finish_decl (decl, init, !is_non_constant_init,
44241 asm_specification,
44242 LOOKUP_ONLYCONVERTING);
44243 orig_init = init;
44245 /* In the case of a class iterator, do not pop sl here.
44246 Both class initialization and finalization must happen in
44247 the enclosing init block scope. For now set the init
44248 expression to null; it'll be filled in properly in
44249 finish_omp_for before stuffing it in the OMP_FOR. */
44250 if (CLASS_TYPE_P (TREE_TYPE (decl)))
44251 init = NULL_TREE;
44252 else /* It is a parameterized type. */
44254 init = pop_stmt_list (sl);
44255 sl = NULL_TREE;
44256 if (init && TREE_CODE (init) == STATEMENT_LIST)
44258 tree_stmt_iterator i = tsi_start (init);
44259 /* Move lambda DECL_EXPRs to the enclosing block. */
44260 while (!tsi_end_p (i))
44262 tree t = tsi_stmt (i);
44263 if (TREE_CODE (t) == DECL_EXPR
44264 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
44266 tsi_delink (&i);
44267 add_stmt (t);
44268 continue;
44270 break;
44272 if (tsi_one_before_end_p (i))
44274 tree t = tsi_stmt (i);
44275 tsi_delink (&i);
44276 free_stmt_list (init);
44277 init = t;
44282 else
44283 /* This is an initialized declaration of non-class,
44284 non-parameterized type iteration variable. */
44286 /* Consume '='. */
44287 cp_lexer_consume_token (parser->lexer);
44288 init = cp_parser_assignment_expression (parser);
44290 non_class:
44291 if (TYPE_REF_P (TREE_TYPE (decl)))
44292 init = error_mark_node;
44293 else
44294 cp_finish_decl (decl, NULL_TREE,
44295 /*init_const_expr_p=*/false,
44296 asm_specification,
44297 LOOKUP_ONLYCONVERTING);
44298 this_pre_body = pop_stmt_list (sl);
44299 sl = NULL_TREE;
44302 if (pushed_scope)
44303 pop_scope (pushed_scope);
44306 else
44308 cp_id_kind idk;
44309 /* If parsing a type specifier sequence failed, then
44310 this MUST be a simple expression. */
44311 cp_parser_parse_tentatively (parser);
44312 decl = cp_parser_primary_expression (parser, false, false,
44313 false, &idk);
44314 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
44315 if (!cp_parser_error_occurred (parser)
44316 && decl
44317 && (TREE_CODE (decl) == COMPONENT_REF
44318 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
44320 cp_parser_abort_tentative_parse (parser);
44321 cp_parser_parse_tentatively (parser);
44322 cp_token *token = cp_lexer_peek_token (parser->lexer);
44323 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
44324 /*check_dependency_p=*/true,
44325 /*template_p=*/NULL,
44326 /*declarator_p=*/false,
44327 /*optional_p=*/false);
44328 if (name != error_mark_node
44329 && last_tok == cp_lexer_peek_token (parser->lexer))
44331 decl = cp_parser_lookup_name_simple (parser, name,
44332 token->location);
44333 if (TREE_CODE (decl) == FIELD_DECL)
44334 add_private_clause = omp_privatize_field (decl, false);
44336 cp_parser_abort_tentative_parse (parser);
44337 cp_parser_parse_tentatively (parser);
44338 decl = cp_parser_primary_expression (parser, false, false,
44339 false, &idk);
44341 if (!cp_parser_error_occurred (parser)
44342 && decl
44343 && DECL_P (decl)
44344 && CLASS_TYPE_P (TREE_TYPE (decl)))
44346 tree rhs;
44348 cp_parser_parse_definitely (parser);
44349 cp_parser_require (parser, CPP_EQ, RT_EQ);
44350 rhs = cp_parser_assignment_expression (parser);
44351 orig_init = rhs;
44352 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
44353 decl, NOP_EXPR,
44354 rhs, NULL_TREE,
44355 tf_warning_or_error));
44356 if (!add_private_clause)
44357 add_private_clause = decl;
44359 else
44361 decl = NULL;
44362 cp_parser_abort_tentative_parse (parser);
44363 init = cp_parser_expression (parser);
44364 if (init)
44366 if (TREE_CODE (init) == MODIFY_EXPR
44367 || TREE_CODE (init) == MODOP_EXPR)
44368 real_decl = TREE_OPERAND (init, 0);
44371 this_pre_body = pop_stmt_list (sl);
44372 sl = NULL_TREE;
44374 return add_private_clause;
44377 /* Helper for cp_parser_omp_loop_nest, handle one range-for loop
44378 including introducing new temporaries for the range start and end,
44379 doing auto deduction, and processing decomposition variables.
44381 This function is also called from pt.cc during template instantiation.
44382 In that case SL is NULL_TREE, otherwise it is the current statement
44383 list. */
44384 void
44385 cp_convert_omp_range_for (tree &this_pre_body, tree &sl,
44386 tree &decl, tree &orig_decl, tree &init,
44387 tree &orig_init, tree &cond, tree &incr)
44389 tree begin, end, range_temp_decl = NULL_TREE;
44390 tree iter_type, begin_expr, end_expr;
44391 bool clear_has_value_expr = false;
44393 if (processing_template_decl)
44395 if (check_for_bare_parameter_packs (init))
44396 init = error_mark_node;
44397 if (!type_dependent_expression_p (init)
44398 /* do_auto_deduction doesn't mess with template init-lists. */
44399 && !BRACE_ENCLOSED_INITIALIZER_P (init))
44401 tree d = decl;
44402 cp_decomp decomp_d, *decomp = NULL;
44403 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
44405 tree v = DECL_VALUE_EXPR (decl);
44406 if (TREE_CODE (v) == ARRAY_REF
44407 && VAR_P (TREE_OPERAND (v, 0))
44408 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
44410 d = TREE_OPERAND (v, 0);
44411 decomp = &decomp_d;
44412 decomp->count = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
44413 decomp->decl = decl;
44416 do_range_for_auto_deduction (d, init, decomp);
44418 cond = global_namespace;
44419 incr = NULL_TREE;
44420 orig_init = init;
44421 if (sl)
44423 this_pre_body = pop_stmt_list (sl);
44424 sl = NULL_TREE;
44426 return;
44429 init = mark_lvalue_use (init);
44431 if (decl == error_mark_node || init == error_mark_node)
44432 /* If an error happened previously do nothing or else a lot of
44433 unhelpful errors would be issued. */
44434 begin_expr = end_expr = iter_type = error_mark_node;
44435 else
44437 tree range_temp;
44439 if (VAR_P (init)
44440 && array_of_runtime_bound_p (TREE_TYPE (init)))
44441 /* Can't bind a reference to an array of runtime bound. */
44442 range_temp = init;
44443 else
44445 range_temp = build_range_temp (init);
44446 DECL_NAME (range_temp) = NULL_TREE;
44447 pushdecl (range_temp);
44448 cp_finish_decl (range_temp, init,
44449 /*is_constant_init*/false, NULL_TREE,
44450 LOOKUP_ONLYCONVERTING);
44451 range_temp_decl = range_temp;
44452 range_temp = convert_from_reference (range_temp);
44454 iter_type = cp_parser_perform_range_for_lookup (range_temp,
44455 &begin_expr, &end_expr);
44458 tree end_iter_type = iter_type;
44459 if (cxx_dialect >= cxx17)
44460 end_iter_type = cv_unqualified (TREE_TYPE (end_expr));
44461 end = build_decl (input_location, VAR_DECL, NULL_TREE, end_iter_type);
44462 TREE_USED (end) = 1;
44463 DECL_ARTIFICIAL (end) = 1;
44464 pushdecl (end);
44465 cp_finish_decl (end, end_expr,
44466 /*is_constant_init*/false, NULL_TREE,
44467 LOOKUP_ONLYCONVERTING);
44469 /* The new for initialization statement. */
44470 begin = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
44471 TREE_USED (begin) = 1;
44472 DECL_ARTIFICIAL (begin) = 1;
44473 pushdecl (begin);
44474 orig_init = init;
44475 if (CLASS_TYPE_P (iter_type))
44476 init = NULL_TREE;
44477 else
44479 init = begin_expr;
44480 begin_expr = NULL_TREE;
44482 cp_finish_decl (begin, begin_expr,
44483 /*is_constant_init*/false, NULL_TREE,
44484 LOOKUP_ONLYCONVERTING);
44486 /* The new for condition. */
44487 if (CLASS_TYPE_P (iter_type))
44488 cond = build2 (NE_EXPR, boolean_type_node, begin, end);
44489 else
44490 cond = build_x_binary_op (input_location, NE_EXPR,
44491 begin, ERROR_MARK,
44492 end, ERROR_MARK,
44493 NULL_TREE, NULL, tf_warning_or_error);
44495 /* The new increment expression. */
44496 if (CLASS_TYPE_P (iter_type))
44497 incr = build2 (PREINCREMENT_EXPR, iter_type, begin, NULL_TREE);
44498 else
44499 incr = finish_unary_op_expr (input_location,
44500 PREINCREMENT_EXPR, begin,
44501 tf_warning_or_error);
44503 orig_decl = decl;
44504 decl = begin;
44505 /* Defer popping sl here. */
44507 cp_decomp decomp_d, *decomp = NULL;
44508 if (orig_decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (orig_decl))
44510 tree v = DECL_VALUE_EXPR (orig_decl);
44511 if (TREE_CODE (v) == ARRAY_REF
44512 && VAR_P (TREE_OPERAND (v, 0))
44513 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
44515 tree d = orig_decl;
44516 orig_decl = TREE_OPERAND (v, 0);
44517 decomp = &decomp_d;
44518 decomp->count = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
44519 decomp->decl = d;
44523 tree auto_node = type_uses_auto (TREE_TYPE (orig_decl));
44524 if (auto_node)
44526 tree t = build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
44527 NULL_TREE, tf_none);
44528 if (!error_operand_p (t))
44530 TREE_TYPE (orig_decl) = do_auto_deduction (TREE_TYPE (orig_decl),
44531 t, auto_node);
44532 if (decomp)
44534 ++processing_template_decl;
44535 cp_finish_decomp (orig_decl, decomp);
44536 --processing_template_decl;
44537 if (!processing_template_decl)
44538 clear_has_value_expr = true;
44543 /* The output ORIG_DECL is not a decl. Instead, it is a tree structure
44544 that holds decls for variables implementing the iterator, represented
44545 as a TREE_LIST whose TREE_CHAIN is a vector. The first two elements
44546 of the vector are decls of scratch variables for the range start and
44547 end that will eventually be bound in the implicit scope surrounding
44548 the whole loop nest. The remaining elements are decls of derived
44549 decomposition variables that are bound inside the loop body. This
44550 structure is further mangled by finish_omp_for into the form required
44551 for the OMP_FOR_ORIG_DECLS field of the OMP_FOR tree node. */\
44552 unsigned decomp_cnt = decomp ? decomp->count : 0;
44553 tree v = make_tree_vec (decomp_cnt + 3);
44554 TREE_VEC_ELT (v, 0) = range_temp_decl;
44555 TREE_VEC_ELT (v, 1) = end;
44556 TREE_VEC_ELT (v, 2) = orig_decl;
44557 if (clear_has_value_expr)
44558 TREE_PUBLIC (v) = 1;
44559 for (unsigned i = 0; i < decomp_cnt; i++)
44561 if (clear_has_value_expr)
44563 /* If cp_finish_decomp was called with processing_template_decl
44564 temporarily set to 1, then decomp names will have deduced
44565 name but the DECL_VALUE_EXPR will be dependent. Hide those
44566 from folding of other loop initializers e.g. for warning
44567 purposes until cp_finish_omp_range_for. */
44568 gcc_checking_assert (DECL_HAS_VALUE_EXPR_P (decomp->decl)
44569 || (TREE_TYPE (decomp->decl)
44570 == error_mark_node));
44571 DECL_HAS_VALUE_EXPR_P (decomp->decl) = 0;
44573 TREE_VEC_ELT (v, i + 3) = decomp->decl;
44574 decomp->decl = DECL_CHAIN (decomp->decl);
44576 orig_decl = tree_cons (NULL_TREE, NULL_TREE, v);
44579 /* Helper for cp_parser_omp_for_loop, finalize part of range for
44580 inside of the collapsed body. */
44582 void
44583 cp_finish_omp_range_for (tree orig, tree begin)
44585 gcc_assert (TREE_CODE (orig) == TREE_LIST
44586 && TREE_CODE (TREE_CHAIN (orig)) == TREE_VEC);
44587 tree decl = TREE_VEC_ELT (TREE_CHAIN (orig), 2);
44588 cp_decomp decomp_d, *decomp = NULL;
44590 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
44592 decomp = &decomp_d;
44593 decomp_d.decl = TREE_VEC_ELT (TREE_CHAIN (orig), 3);
44594 decomp_d.count = TREE_VEC_LENGTH (TREE_CHAIN (orig)) - 3;
44595 if (TREE_PUBLIC (TREE_CHAIN (orig)))
44597 /* Undo temporary clearing of DECL_HAS_VALUE_EXPR_P done
44598 by cp_convert_omp_range_for above. */
44599 TREE_PUBLIC (TREE_CHAIN (orig)) = 0;
44600 tree d = decomp_d.decl;
44601 for (unsigned i = 0; i < decomp_d.count; i++)
44603 if (TREE_TYPE (d) != error_mark_node)
44604 DECL_HAS_VALUE_EXPR_P (d) = 1;
44605 d = DECL_CHAIN (d);
44610 /* The declaration is initialized with *__begin inside the loop body. */
44611 cp_finish_decl (decl,
44612 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
44613 NULL_TREE, tf_warning_or_error),
44614 /*is_constant_init*/false, NULL_TREE,
44615 LOOKUP_ONLYCONVERTING, decomp);
44616 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
44617 cp_finish_decomp (decl, decomp);
44620 /* Return true if next tokens contain a standard attribute that contains
44621 omp::directive (DIRECTIVE). */
44623 static bool
44624 cp_parser_omp_section_scan (cp_parser *parser, const char *directive,
44625 bool tentative)
44627 size_t n = cp_parser_skip_attributes_opt (parser, 1), i;
44628 if (n < 10)
44629 return false;
44630 for (i = 5; i < n - 4; i++)
44631 if (cp_lexer_nth_token_is (parser->lexer, i, CPP_NAME)
44632 && cp_lexer_nth_token_is (parser->lexer, i + 1, CPP_OPEN_PAREN)
44633 && cp_lexer_nth_token_is (parser->lexer, i + 2, CPP_NAME))
44635 tree first = cp_lexer_peek_nth_token (parser->lexer, i)->u.value;
44636 tree second = cp_lexer_peek_nth_token (parser->lexer, i + 2)->u.value;
44637 if (strcmp (IDENTIFIER_POINTER (first), "directive")
44638 && strcmp (IDENTIFIER_POINTER (first), "__directive__"))
44639 continue;
44640 if (strcmp (IDENTIFIER_POINTER (second), directive) == 0)
44641 break;
44643 if (i == n - 4)
44644 return false;
44645 cp_parser_parse_tentatively (parser);
44646 location_t first_loc = cp_lexer_peek_token (parser->lexer)->location;
44647 location_t last_loc
44648 = cp_lexer_peek_nth_token (parser->lexer, n - 1)->location;
44649 location_t middle_loc = UNKNOWN_LOCATION;
44650 tree std_attrs = cp_parser_std_attribute_spec_seq (parser);
44651 int cnt = 0;
44652 bool seen = false;
44653 for (tree attr = std_attrs; attr; attr = TREE_CHAIN (attr))
44654 if (get_attribute_namespace (attr) == omp_identifier
44655 && is_attribute_p ("directive", get_attribute_name (attr)))
44657 for (tree a = TREE_VALUE (attr); a; a = TREE_CHAIN (a))
44659 tree d = TREE_VALUE (a);
44660 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
44661 cp_token *first = DEFPARSE_TOKENS (d)->first;
44662 cnt++;
44663 if (first->type == CPP_NAME
44664 && strcmp (IDENTIFIER_POINTER (first->u.value),
44665 directive) == 0)
44667 seen = true;
44668 if (middle_loc == UNKNOWN_LOCATION)
44669 middle_loc = first->location;
44673 if (!seen || tentative)
44675 cp_parser_abort_tentative_parse (parser);
44676 return seen;
44678 if (cnt != 1 || TREE_CHAIN (std_attrs))
44680 error_at (make_location (first_loc, last_loc, middle_loc),
44681 "%<[[omp::directive(%s)]]%> must be the only specified "
44682 "attribute on a statement", directive);
44683 cp_parser_abort_tentative_parse (parser);
44684 return false;
44686 if (!cp_parser_parse_definitely (parser))
44687 return false;
44688 cp_parser_handle_statement_omp_attributes (parser, std_attrs);
44689 return true;
44692 /* Parse an OpenMP structured block sequence. KIND is the corresponding
44693 separating directive. */
44695 static tree
44696 cp_parser_omp_structured_block_sequence (cp_parser *parser,
44697 enum pragma_kind kind)
44699 tree stmt = begin_omp_structured_block ();
44700 unsigned int save = cp_parser_begin_omp_structured_block (parser);
44702 cp_parser_statement (parser, NULL_TREE, false, NULL);
44703 while (true)
44705 cp_token *token = cp_lexer_peek_token (parser->lexer);
44707 if (token->type == CPP_CLOSE_BRACE
44708 || token->type == CPP_EOF
44709 || token->type == CPP_PRAGMA_EOL
44710 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END)
44711 || (kind != PRAGMA_NONE
44712 && cp_parser_pragma_kind (token) == kind))
44713 break;
44715 if (kind != PRAGMA_NONE
44716 && cp_parser_omp_section_scan (parser,
44717 kind == PRAGMA_OMP_SCAN
44718 ? "scan" : "section", false))
44719 break;
44721 cp_parser_statement (parser, NULL_TREE, false, NULL);
44724 cp_parser_end_omp_structured_block (parser, save);
44725 return finish_omp_structured_block (stmt);
44729 /* OpenMP 5.0:
44731 scan-loop-body:
44732 { structured-block scan-directive structured-block } */
44734 static void
44735 cp_parser_omp_scan_loop_body (cp_parser *parser)
44737 tree substmt, clauses = NULL_TREE;
44738 bool found_scan = false;
44740 matching_braces braces;
44741 if (!braces.require_open (parser))
44742 return;
44744 cp_token *tok = cp_lexer_peek_token (parser->lexer);
44745 if (cp_parser_pragma_kind (tok) != PRAGMA_OMP_SCAN)
44746 substmt = cp_parser_omp_structured_block_sequence (parser, PRAGMA_OMP_SCAN);
44747 else
44749 warning_at (tok->location, OPT_Wopenmp,
44750 "%<#pragma omp scan%> with zero preceding executable "
44751 "statements");
44752 substmt = build_empty_stmt (tok->location);
44754 substmt = build2 (OMP_SCAN, void_type_node, substmt, NULL_TREE);
44755 add_stmt (substmt);
44757 tok = cp_lexer_peek_token (parser->lexer);
44758 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SCAN)
44760 enum omp_clause_code clause = OMP_CLAUSE_ERROR;
44761 found_scan = true;
44763 cp_lexer_consume_token (parser->lexer);
44765 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
44766 cp_lexer_consume_token (parser->lexer);
44768 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
44770 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
44771 const char *p = IDENTIFIER_POINTER (id);
44772 if (strcmp (p, "inclusive") == 0)
44773 clause = OMP_CLAUSE_INCLUSIVE;
44774 else if (strcmp (p, "exclusive") == 0)
44775 clause = OMP_CLAUSE_EXCLUSIVE;
44777 if (clause != OMP_CLAUSE_ERROR)
44779 cp_lexer_consume_token (parser->lexer);
44780 clauses = cp_parser_omp_var_list (parser, clause, NULL_TREE);
44782 else
44783 cp_parser_error (parser, "expected %<inclusive%> or "
44784 "%<exclusive%> clause");
44786 cp_parser_require_pragma_eol (parser, tok);
44788 else
44789 error ("expected %<#pragma omp scan%>");
44791 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
44792 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
44793 substmt = cp_parser_omp_structured_block_sequence (parser, PRAGMA_NONE);
44794 else
44796 if (found_scan)
44797 warning_at (tok->location, OPT_Wopenmp,
44798 "%<#pragma omp scan%> with zero succeeding executable "
44799 "statements");
44800 substmt = build_empty_stmt (tok->location);
44802 substmt = build2_loc (tok->location, OMP_SCAN, void_type_node, substmt,
44803 clauses);
44804 add_stmt (substmt);
44806 braces.require_close (parser);
44810 /* This function parses a single level of a loop nest, invoking itself
44811 recursively if necessary.
44813 loop-nest :: for (...) loop-body
44814 loop-body :: loop-nest
44815 | { [intervening-code] loop-body [intervening-code] }
44816 | final-loop-body
44817 intervening-code :: structured-block-sequence
44818 final-loop-body :: structured-block
44820 For a collapsed loop nest, only a single OMP_FOR is built, pulling out
44821 all the iterator information from the inner loops into vectors in the
44822 parser->omp_for_parse_state structure.
44824 In the "range for" case, it is transformed into a regular "for" iterator
44825 by introducing some temporary variables for the begin/end,
44826 as well as bindings of the actual iteration variables which are
44827 injected into the body of the loop.
44829 Initialization code for iterator variables may end up either in the
44830 init vector (simple assignments), in omp_for_parse_state->pre_body
44831 (decl_exprs for iterators bound in the for statement), or in the
44832 scope surrounding this level of loop initialization.
44834 The scopes of class iterator variables and their finalizers need to
44835 be adjusted after parsing so that all of the initialization happens
44836 in a scope surrounding all of the intervening and body code. For
44837 this reason we separately store the initialization and body blocks
44838 for each level of loops in the omp_for_parse_state structure and
44839 reassemble/reorder them in cp_parser_omp_for. See additional
44840 comments there about the use of placeholders, etc. */
44842 static tree
44843 cp_parser_omp_loop_nest (cp_parser *parser, bool *if_p)
44845 tree decl, cond, incr, init;
44846 tree orig_init, real_decl, orig_decl;
44847 tree init_block, body_block;
44848 tree init_placeholder, body_placeholder;
44849 tree init_scope;
44850 tree this_pre_body = NULL_TREE;
44851 bool moreloops;
44852 unsigned char save_in_statement;
44853 tree add_private_clause = NULL_TREE;
44854 location_t loc;
44855 bool is_range_for = false;
44856 tree sl = NULL_TREE;
44857 struct omp_for_parse_data *omp_for_parse_state
44858 = parser->omp_for_parse_state;
44859 gcc_assert (omp_for_parse_state);
44860 int depth = omp_for_parse_state->depth;
44862 /* We have already matched the FOR token but not consumed it yet. */
44863 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR));
44864 loc = cp_lexer_consume_token (parser->lexer)->location;
44866 /* Forbid break/continue in the loop initializer, condition, and
44867 increment expressions. */
44868 save_in_statement = parser->in_statement;
44869 parser->in_statement = IN_OMP_BLOCK;
44871 /* We are not in intervening code now. */
44872 omp_for_parse_state->in_intervening_code = false;
44874 /* Don't create location wrapper nodes within an OpenMP "for"
44875 statement. */
44876 auto_suppress_location_wrappers sentinel;
44878 matching_parens parens;
44879 if (!parens.require_open (parser))
44880 return NULL;
44882 init = orig_init = decl = real_decl = orig_decl = NULL_TREE;
44884 init_placeholder = build_stmt (input_location, EXPR_STMT,
44885 integer_zero_node);
44886 vec_safe_push (omp_for_parse_state->init_placeholderv, init_placeholder);
44888 /* The init_block acts as a container for this level of loop goo. */
44889 init_block = push_stmt_list ();
44890 vec_safe_push (omp_for_parse_state->init_blockv, init_block);
44892 /* Wrap a scope around this entire level of loop to hold bindings
44893 of loop iteration variables. We can't insert them directly
44894 in the containing scope because that would cause their visibility to
44895 be incorrect with respect to intervening code after this loop.
44896 We will combine the nested init_scopes in postprocessing after the
44897 entire loop is parsed. */
44898 init_scope = begin_compound_stmt (0);
44900 /* Now we need another level of statement list container to capture the
44901 initialization (and possible finalization) bits. In some cases this
44902 container may be popped off during initializer parsing to store code in
44903 INIT or THIS_PRE_BODY, depending on the form of initialization. If
44904 we have a class iterator we will pop it at the end of parsing this
44905 level, so the cleanups are handled correctly. */
44906 sl = push_stmt_list ();
44908 if (omp_for_parse_state->code != OACC_LOOP && cxx_dialect >= cxx11)
44910 /* Save tokens so that we can put them back. */
44911 cp_lexer_save_tokens (parser->lexer);
44913 /* Look for ':' that is not nested in () or {}. */
44914 is_range_for
44915 = (cp_parser_skip_to_closing_parenthesis_1 (parser,
44916 /*recovering=*/false,
44917 CPP_COLON,
44918 /*consume_paren=*/
44919 false) == -1);
44921 /* Roll back the tokens we skipped. */
44922 cp_lexer_rollback_tokens (parser->lexer);
44924 if (is_range_for)
44926 bool saved_colon_corrects_to_scope_p
44927 = parser->colon_corrects_to_scope_p;
44929 /* A colon is used in range-based for. */
44930 parser->colon_corrects_to_scope_p = false;
44932 /* Parse the declaration. */
44933 cp_parser_simple_declaration (parser,
44934 /*function_definition_allowed_p=*/
44935 false, &decl);
44936 parser->colon_corrects_to_scope_p
44937 = saved_colon_corrects_to_scope_p;
44939 cp_parser_require (parser, CPP_COLON, RT_COLON);
44941 init = cp_parser_range_for (parser, NULL_TREE, NULL_TREE, decl,
44942 false, NULL_TREE, false, true);
44944 cp_convert_omp_range_for (this_pre_body, sl, decl,
44945 orig_decl, init, orig_init,
44946 cond, incr);
44948 if (omp_for_parse_state->ordered_cl)
44949 error_at (OMP_CLAUSE_LOCATION (omp_for_parse_state->ordered_cl),
44950 "%<ordered%> clause with parameter on "
44951 "range-based %<for%> loop");
44953 goto parse_close_paren;
44957 add_private_clause
44958 = cp_parser_omp_for_loop_init (parser, this_pre_body, sl,
44959 init, orig_init, decl, real_decl);
44961 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
44963 /* If the iteration variable was introduced via a declaration in the
44964 for statement, DECL points at it. Otherwise DECL is null and
44965 REAL_DECL is a variable previously declared in an outer scope.
44966 Make REAL_DECL point at the iteration variable no matter where it
44967 was introduced. */
44968 if (decl)
44969 real_decl = decl;
44971 /* Some clauses treat iterator variables specially. */
44972 if (omp_for_parse_state->cclauses != NULL
44973 && omp_for_parse_state->cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
44974 && real_decl != NULL_TREE
44975 && omp_for_parse_state->code != OMP_LOOP)
44977 tree *c;
44978 for (c = &(omp_for_parse_state->cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]);
44979 *c ; )
44980 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
44981 && OMP_CLAUSE_DECL (*c) == real_decl)
44983 error_at (loc, "iteration variable %qD"
44984 " should not be firstprivate", real_decl);
44985 *c = OMP_CLAUSE_CHAIN (*c);
44987 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
44988 && OMP_CLAUSE_DECL (*c) == real_decl)
44990 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
44991 tree l = *c;
44992 *c = OMP_CLAUSE_CHAIN (*c);
44993 if (omp_for_parse_state->code == OMP_SIMD)
44995 OMP_CLAUSE_CHAIN (l)
44996 = omp_for_parse_state->cclauses[C_OMP_CLAUSE_SPLIT_FOR];
44997 omp_for_parse_state->cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
44999 else
45001 OMP_CLAUSE_CHAIN (l) = omp_for_parse_state->clauses;
45002 omp_for_parse_state->clauses = l;
45004 add_private_clause = NULL_TREE;
45006 else
45008 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
45009 && OMP_CLAUSE_DECL (*c) == real_decl)
45010 add_private_clause = NULL_TREE;
45011 c = &OMP_CLAUSE_CHAIN (*c);
45015 if (add_private_clause)
45017 tree c;
45018 for (c = omp_for_parse_state->clauses; c ; c = OMP_CLAUSE_CHAIN (c))
45020 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
45021 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
45022 && OMP_CLAUSE_DECL (c) == decl)
45023 break;
45024 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
45025 && OMP_CLAUSE_DECL (c) == decl)
45026 error_at (loc, "iteration variable %qD "
45027 "should not be firstprivate",
45028 decl);
45029 else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
45030 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
45031 && OMP_CLAUSE_DECL (c) == decl)
45032 error_at (loc, "iteration variable %qD should not be reduction",
45033 decl);
45035 if (c == NULL)
45037 if ((omp_for_parse_state->code == OMP_SIMD
45038 && omp_for_parse_state->count != 1)
45039 || omp_for_parse_state->code == OMP_LOOP)
45040 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
45041 else if (omp_for_parse_state->code != OMP_SIMD)
45042 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
45043 else
45044 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
45045 OMP_CLAUSE_DECL (c) = add_private_clause;
45046 c = finish_omp_clauses (c, C_ORT_OMP);
45047 if (c)
45049 OMP_CLAUSE_CHAIN (c) = omp_for_parse_state->clauses;
45050 omp_for_parse_state->clauses = c;
45051 /* For linear, signal that we need to fill up
45052 the so far unknown linear step. */
45053 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
45054 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
45059 cond = NULL;
45060 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
45061 cond = cp_parser_omp_for_cond (parser, decl, omp_for_parse_state->code);
45062 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
45064 incr = NULL;
45065 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
45067 /* If decl is an iterator, preserve the operator on decl
45068 until finish_omp_for. */
45069 if (real_decl
45070 && ((processing_template_decl
45071 && (TREE_TYPE (real_decl) == NULL_TREE
45072 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl))))
45073 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
45074 incr = cp_parser_omp_for_incr (parser, real_decl);
45075 else
45076 incr = cp_parser_expression (parser);
45077 protected_set_expr_location_if_unset (incr, input_location);
45080 parse_close_paren:
45081 if (!parens.require_close (parser))
45082 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
45083 /*or_comma=*/false,
45084 /*consume_paren=*/true);
45086 /* We've parsed all the for (...) stuff now. Store the bits. */
45087 TREE_VEC_ELT (omp_for_parse_state->declv, depth) = decl;
45088 TREE_VEC_ELT (omp_for_parse_state->initv, depth) = init;
45089 TREE_VEC_ELT (omp_for_parse_state->condv, depth) = cond;
45090 TREE_VEC_ELT (omp_for_parse_state->incrv, depth) = incr;
45091 if (orig_init)
45093 omp_for_parse_state->orig_inits.safe_grow_cleared (depth + 1, true);
45094 omp_for_parse_state->orig_inits[depth] = orig_init;
45096 if (orig_decl)
45098 if (!omp_for_parse_state->orig_declv)
45099 omp_for_parse_state->orig_declv
45100 = copy_node (omp_for_parse_state->declv);
45101 TREE_VEC_ELT (omp_for_parse_state->orig_declv, depth) = orig_decl;
45103 else if (omp_for_parse_state->orig_declv)
45104 TREE_VEC_ELT (omp_for_parse_state->orig_declv, depth) = decl;
45105 if (this_pre_body)
45106 append_to_statement_list_force (this_pre_body,
45107 &(omp_for_parse_state->pre_body));
45109 /* Start a nested block for the loop body. */
45110 body_placeholder = build_stmt (input_location, EXPR_STMT,
45111 integer_zero_node);
45112 vec_safe_push (omp_for_parse_state->body_placeholderv, body_placeholder);
45113 body_block = push_stmt_list ();
45114 vec_safe_push (omp_for_parse_state->body_blockv, body_block);
45116 moreloops = depth < omp_for_parse_state->count - 1;
45117 omp_for_parse_state->want_nested_loop = moreloops;
45118 if (moreloops && cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
45120 omp_for_parse_state->depth++;
45121 add_stmt (cp_parser_omp_loop_nest (parser, if_p));
45122 omp_for_parse_state->depth--;
45124 else if (moreloops
45125 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
45127 /* This is the open brace in the loop-body grammar production. Rather
45128 than trying to special-case braces, just parse it as a compound
45129 statement and handle the nested loop-body case there. Note that
45130 when we see a further open brace inside the compound statement
45131 loop-body, we don't know whether it is the start of intervening
45132 code that is a compound statement, or a level of braces
45133 surrounding a nested loop-body. Use the WANT_NESTED_LOOP state
45134 bit to ensure we have only one nested loop at each level. */
45136 omp_for_parse_state->in_intervening_code = true;
45137 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
45138 omp_for_parse_state->in_intervening_code = false;
45140 if (omp_for_parse_state->want_nested_loop)
45142 /* We have already parsed the whole loop body and not found a
45143 nested loop. */
45144 error_at (omp_for_parse_state->for_loc,
45145 "not enough nested loops");
45146 omp_for_parse_state->fail = true;
45148 if_p = NULL;
45150 else
45152 /* This is the final-loop-body case in the grammar: we have something
45153 that is not a FOR and not an open brace. */
45154 if (moreloops)
45156 /* If we were expecting a nested loop, give an error and mark
45157 that parsing has failed, and try to recover by parsing the
45158 body as regular code without further collapsing. */
45159 error_at (omp_for_parse_state->for_loc,
45160 "not enough nested loops");
45161 omp_for_parse_state->fail = true;
45163 parser->in_statement = IN_OMP_FOR;
45165 /* Generate the parts of range for that belong in the loop body,
45166 to be executed on every iteration. This includes setting the
45167 user-declared decomposition variables from the compiler-generated
45168 temporaries that are the real iteration variables for OMP_FOR.
45169 FIXME: Not sure if this is correct with respect to visibility
45170 of the variables from intervening code. However, putting this
45171 code in each level of loop instead of all around the innermost
45172 body also makes the decomposition variables visible to the
45173 inner for init/bound/step exressions, which is not supposed to
45174 happen and causes test failures. */
45175 if (omp_for_parse_state->orig_declv)
45176 for (int i = 0; i < omp_for_parse_state->count; i++)
45178 tree o = TREE_VEC_ELT (omp_for_parse_state->orig_declv, i);
45179 tree d = TREE_VEC_ELT (omp_for_parse_state->declv, i);
45180 if (o != d)
45181 cp_finish_omp_range_for (o, d);
45184 /* Now parse the final-loop-body for the innermost loop. */
45185 parser->omp_for_parse_state = NULL;
45186 if (omp_for_parse_state->inscan)
45187 cp_parser_omp_scan_loop_body (parser);
45188 else
45189 cp_parser_statement (parser, NULL_TREE, false, if_p);
45190 parser->omp_for_parse_state = omp_for_parse_state;
45192 parser->in_statement = save_in_statement;
45193 omp_for_parse_state->want_nested_loop = false;
45194 omp_for_parse_state->in_intervening_code = true;
45196 /* Pop and remember the body block. Add the body placeholder
45197 to the surrounding statement list instead. This is just a unique
45198 token that will be replaced when we reassemble the generated
45199 code for the entire omp for statement. */
45200 body_block = pop_stmt_list (body_block);
45201 omp_for_parse_state->body_blockv[depth] = body_block;
45202 add_stmt (body_placeholder);
45204 /* Pop and remember the init block. */
45205 if (sl)
45206 add_stmt (pop_stmt_list (sl));
45207 finish_compound_stmt (init_scope);
45208 init_block = pop_stmt_list (init_block);
45209 omp_for_parse_state->init_blockv[depth] = init_block;
45211 /* Return the init placeholder rather than the remembered init block.
45212 Again, this is just a unique cookie that will be used to reassemble
45213 code pieces when the entire omp for statement has been parsed. */
45214 return init_placeholder;
45217 /* Worker for find_structured_blocks. *TP points to a STATEMENT_LIST
45218 and ITER is the element that is or contains a nested loop. This
45219 function moves the statements before and after ITER into
45220 OMP_STRUCTURED_BLOCKs and modifies *TP. */
45221 static void
45222 insert_structured_blocks (tree *tp, tree_stmt_iterator iter)
45224 tree sl = push_stmt_list ();
45225 for (tree_stmt_iterator i = tsi_start (*tp); !tsi_end_p (i); )
45226 if (i == iter)
45228 sl = pop_stmt_list (sl);
45229 if (TREE_CODE (sl) != STATEMENT_LIST || !tsi_end_p (tsi_start (sl)))
45230 tsi_link_before (&i,
45231 build1 (OMP_STRUCTURED_BLOCK, void_type_node, sl),
45232 TSI_SAME_STMT);
45233 i++;
45234 sl = push_stmt_list ();
45236 else
45238 tree s = tsi_stmt (i);
45239 tsi_delink (&i); /* Advances i to next statement. */
45240 add_stmt (s);
45242 sl = pop_stmt_list (sl);
45243 if (TREE_CODE (sl) != STATEMENT_LIST || !tsi_end_p (tsi_start (sl)))
45244 tsi_link_after (&iter,
45245 build1 (OMP_STRUCTURED_BLOCK, void_type_node, sl),
45246 TSI_SAME_STMT);
45249 /* Helper to find and mark structured blocks in intervening code for a
45250 single loop level with markers for later error checking. *TP is the
45251 piece of code to be marked and INNER is the inner loop placeholder.
45252 Returns true if INNER was found (recursively) in *TP. */
45253 static bool
45254 find_structured_blocks (tree *tp, tree inner)
45256 if (*tp == inner)
45257 return true;
45258 else if (TREE_CODE (*tp) == BIND_EXPR)
45259 return find_structured_blocks (&(BIND_EXPR_BODY (*tp)), inner);
45260 else if (TREE_CODE (*tp) == STATEMENT_LIST)
45262 for (tree_stmt_iterator i = tsi_start (*tp); !tsi_end_p (i); ++i)
45264 tree *p = tsi_stmt_ptr (i);
45265 /* The normal case is that there is no intervening code and we
45266 do not have to insert any OMP_STRUCTURED_BLOCK markers. */
45267 if (find_structured_blocks (p, inner))
45269 if (!(i == tsi_start (*tp) && i == tsi_last (*tp)))
45270 insert_structured_blocks (tp, i);
45271 return true;
45274 return false;
45276 else if (TREE_CODE (*tp) == TRY_FINALLY_EXPR)
45277 return find_structured_blocks (&(TREE_OPERAND (*tp, 0)), inner);
45278 else if (TREE_CODE (*tp) == CLEANUP_STMT)
45279 return find_structured_blocks (&(CLEANUP_BODY (*tp)), inner);
45280 else
45281 return false;
45284 /* Helpers used for relinking tree structures: In tree rooted at
45285 CONTEXT, replace ORIG with REPLACEMENT. If FLATTEN is true, try to combine
45286 nested BIND_EXPRs. Gives an assertion if it fails to find ORIG. */
45288 struct sit_data {
45289 tree orig;
45290 tree repl;
45291 bool flatten;
45294 static tree
45295 substitute_in_tree_walker (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
45296 void *dp)
45298 struct sit_data *sit = (struct sit_data *)dp;
45299 if (*tp == sit->orig)
45301 *tp = sit->repl;
45302 return *tp;
45304 /* Remove redundant BIND_EXPRs with no bindings even when not specifically
45305 trying to flatten. */
45306 else if (TREE_CODE (*tp) == BIND_EXPR
45307 && BIND_EXPR_BODY (*tp) == sit->orig
45308 && !BIND_EXPR_VARS (*tp)
45309 && (sit->flatten || TREE_CODE (sit->repl) == BIND_EXPR))
45311 *tp = sit->repl;
45312 return *tp;
45314 else if (sit->flatten
45315 && TREE_CODE (*tp) == BIND_EXPR
45316 && TREE_CODE (sit->repl) == BIND_EXPR)
45318 if (BIND_EXPR_BODY (*tp) == sit->orig)
45320 /* Merge binding lists for two directly nested BIND_EXPRs,
45321 keeping the outer one. */
45322 BIND_EXPR_VARS (*tp) = chainon (BIND_EXPR_VARS (*tp),
45323 BIND_EXPR_VARS (sit->repl));
45324 BIND_EXPR_BODY (*tp) = BIND_EXPR_BODY (sit->repl);
45325 return *tp;
45327 else if (TREE_CODE (BIND_EXPR_BODY (*tp)) == STATEMENT_LIST)
45328 /* There might be a statement list containing cleanup_points
45329 etc between the two levels of BIND_EXPR. We can still merge
45330 them, again keeping the outer BIND_EXPR. */
45331 for (tree_stmt_iterator i = tsi_start (BIND_EXPR_BODY (*tp));
45332 !tsi_end_p (i); ++i)
45334 tree *p = tsi_stmt_ptr (i);
45335 if (*p == sit->orig)
45337 BIND_EXPR_VARS (*tp) = chainon (BIND_EXPR_VARS (*tp),
45338 BIND_EXPR_VARS (sit->repl));
45339 *p = BIND_EXPR_BODY (sit->repl);
45340 return *tp;
45344 return NULL;
45347 static void
45348 substitute_in_tree (tree *context, tree orig, tree repl, bool flatten)
45350 struct sit_data data;
45352 gcc_assert (*context && orig && repl);
45353 if (TREE_CODE (repl) == BIND_EXPR && !BIND_EXPR_VARS (repl))
45354 repl = BIND_EXPR_BODY (repl);
45355 data.orig = orig;
45356 data.repl = repl;
45357 data.flatten = flatten;
45359 tree result = cp_walk_tree (context, substitute_in_tree_walker,
45360 (void *)&data, NULL);
45361 gcc_assert (result != NULL_TREE);
45364 /* Walker to patch up the BLOCK_NODE hierarchy after the above surgery.
45365 *DP is the parent block. */
45367 static tree
45368 fixup_blocks_walker (tree *tp, int *walk_subtrees, void *dp)
45370 tree superblock = *(tree *)dp;
45372 /* BIND_EXPR_BLOCK may be null if the expression is not a
45373 full-expression; if there's no block, no patching is necessary
45374 for this node. */
45375 if (TREE_CODE (*tp) == BIND_EXPR && BIND_EXPR_BLOCK (*tp))
45377 tree block = BIND_EXPR_BLOCK (*tp);
45378 if (superblock)
45380 BLOCK_SUPERCONTEXT (block) = superblock;
45381 BLOCK_CHAIN (block) = BLOCK_SUBBLOCKS (superblock);
45382 BLOCK_SUBBLOCKS (superblock) = block;
45384 BLOCK_SUBBLOCKS (block) = NULL_TREE;
45385 cp_walk_tree (&BIND_EXPR_BODY (*tp), fixup_blocks_walker,
45386 (void *)&block, NULL);
45387 *walk_subtrees = 0;
45390 return NULL;
45393 /* Parse the restricted form of the for statement allowed by OpenMP. */
45395 static tree
45396 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
45397 tree *cclauses, bool *if_p)
45399 tree ret;
45400 tree cl, ordered_cl = NULL_TREE;
45401 int collapse = 1, ordered = 0;
45402 unsigned int count;
45403 bool tiling = false;
45404 bool inscan = false;
45405 struct omp_for_parse_data data;
45406 struct omp_for_parse_data *save_data = parser->omp_for_parse_state;
45407 tree result;
45408 location_t loc_first = cp_lexer_peek_token (parser->lexer)->location;
45410 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
45411 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
45412 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
45413 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
45415 tiling = true;
45416 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
45418 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
45419 && OMP_CLAUSE_ORDERED_EXPR (cl))
45421 ordered_cl = cl;
45422 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
45424 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_REDUCTION
45425 && OMP_CLAUSE_REDUCTION_INSCAN (cl)
45426 && (code == OMP_SIMD || code == OMP_FOR))
45427 inscan = true;
45429 if (ordered && ordered < collapse)
45431 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
45432 "%<ordered%> clause parameter is less than %<collapse%>");
45433 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
45434 = build_int_cst (NULL_TREE, collapse);
45435 ordered = collapse;
45438 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
45439 count = ordered ? ordered : collapse;
45441 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
45443 cp_parser_error (parser, "for statement expected");
45444 return NULL;
45447 /* Initialize parse state for recursive descent. */
45448 data.declv = make_tree_vec (count);
45449 data.initv = make_tree_vec (count);
45450 data.condv = make_tree_vec (count);
45451 data.incrv = make_tree_vec (count);
45452 data.pre_body = NULL_TREE;
45453 data.for_loc = cp_lexer_peek_token (parser->lexer)->location;
45454 data.count = count;
45455 data.depth = 0;
45456 data.want_nested_loop = true;
45457 data.ordered = ordered > 0;
45458 data.in_intervening_code = false;
45459 data.perfect_nesting_fail = false;
45460 data.fail = false;
45461 data.inscan = inscan;
45462 data.saw_intervening_code = false;
45463 data.code = code;
45464 data.orig_declv = NULL_TREE;
45465 data.clauses = clauses;
45466 data.cclauses = cclauses;
45467 data.ordered_cl = ordered_cl;
45468 parser->omp_for_parse_state = &data;
45470 cp_parser_omp_loop_nest (parser, if_p);
45472 /* Bomb out early if there was an error (not enough loops, etc). */
45473 if (data.fail || data.declv == NULL_TREE)
45475 parser->omp_for_parse_state = save_data;
45476 return NULL_TREE;
45479 /* Relink the init and body blocks that were built during parsing. At
45480 this point we have a structure nested like
45481 init 0
45482 body 0
45483 init 1
45484 body 1
45485 init 2
45486 body 2
45487 and we want to turn it into
45488 init 0
45489 init 1
45490 init 2
45491 omp_for
45492 body 0
45493 body 1
45494 body 2
45495 We also need to flatten the init blocks, as some code for later
45496 processing of combined directives gets confused otherwise. */
45498 gcc_assert (vec_safe_length (data.init_blockv) == count);
45499 gcc_assert (vec_safe_length (data.body_blockv) == count);
45500 gcc_assert (vec_safe_length (data.init_placeholderv) == count);
45501 gcc_assert (vec_safe_length (data.body_placeholderv) == count);
45503 /* First insert markers for structured blocks for intervening code in
45504 the loop bodies. */
45505 for (unsigned int i = 0; i < count - 1; i++)
45507 bool good = find_structured_blocks (&(data.body_blockv[i]),
45508 data.init_placeholderv[i+1]);
45509 gcc_assert (good);
45512 /* Do the substitution from the inside out. */
45513 for (unsigned int i = count - 1; i > 0; i--)
45515 substitute_in_tree (&(data.body_blockv[i-1]),
45516 data.init_placeholderv[i],
45517 data.body_blockv[i], false);
45518 substitute_in_tree (&(data.init_blockv[i-1]),
45519 data.body_placeholderv[i-1],
45520 data.init_blockv[i], true);
45523 /* Generate the OMP_FOR. Note finish_omp_for adds the OMP_FOR
45524 (and possibly other stuff) to the current statement list but
45525 returns a pointer to the OMP_FOR itself, or null in case of error. */
45526 result = push_stmt_list ();
45527 ret = finish_omp_for (loc_first, code, data.declv, data.orig_declv,
45528 data.initv, data.condv, data.incrv,
45529 data.body_blockv[0],
45530 data.pre_body, &data.orig_inits, data.clauses);
45531 result = pop_stmt_list (result);
45533 /* Check for errors involving lb/ub/incr expressions referencing
45534 variables declared in intervening code. */
45535 if (data.saw_intervening_code
45536 && !c_omp_check_loop_binding_exprs (ret, &data.orig_inits))
45537 ret = NULL_TREE;
45539 if (ret)
45541 /* Splice the omp_for into the nest of init blocks. */
45542 substitute_in_tree (&(data.init_blockv[0]),
45543 data.body_placeholderv[count - 1],
45544 result, true);
45546 /* Some later processing for combined directives assumes
45547 that the BIND_EXPR containing range for variables appears
45548 at top level in the OMP_FOR body. Fix that up if it's
45549 not the case, e.g. because there is intervening code. */
45550 if (code != OACC_LOOP)
45551 finish_omp_for_block (data.init_blockv[0], ret);
45553 /* Clean up the block subblock/superblock links. Per comment in
45554 begin_compound_stmt, "we don't build BLOCK nodes when processing
45555 templates", so skip this step in that case. */
45556 if (!processing_template_decl)
45558 tree superblock = NULL_TREE;
45559 cp_walk_tree (&data.init_blockv[0], fixup_blocks_walker,
45560 (void *)&superblock, NULL);
45563 /* Finally record the result. */
45564 add_stmt (data.init_blockv[0]);
45567 parser->omp_for_parse_state = save_data;
45568 return ret;
45571 /* Helper function for OpenMP parsing, split clauses and call
45572 finish_omp_clauses on each of the set of clauses afterwards. */
45574 static void
45575 cp_omp_split_clauses (location_t loc, enum tree_code code,
45576 omp_clause_mask mask, tree clauses, tree *cclauses)
45578 int i;
45579 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
45580 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
45581 if (cclauses[i])
45582 cclauses[i] = finish_omp_clauses (cclauses[i],
45583 i == C_OMP_CLAUSE_SPLIT_TARGET
45584 ? C_ORT_OMP_TARGET : C_ORT_OMP);
45587 /* OpenMP 5.0:
45588 #pragma omp loop loop-clause[optseq] new-line
45589 for-loop */
45591 #define OMP_LOOP_CLAUSE_MASK \
45592 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
45593 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
45594 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
45595 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
45596 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_BIND) \
45597 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
45599 static tree
45600 cp_parser_omp_loop (cp_parser *parser, cp_token *pragma_tok,
45601 char *p_name, omp_clause_mask mask, tree *cclauses,
45602 bool *if_p)
45604 tree clauses, sb, ret;
45605 unsigned int save;
45606 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
45608 strcat (p_name, " loop");
45609 mask |= OMP_LOOP_CLAUSE_MASK;
45611 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
45612 cclauses == NULL);
45613 if (cclauses)
45615 cp_omp_split_clauses (loc, OMP_LOOP, mask, clauses, cclauses);
45616 clauses = cclauses[C_OMP_CLAUSE_SPLIT_LOOP];
45619 keep_next_level (true);
45620 sb = begin_omp_structured_block ();
45621 save = cp_parser_begin_omp_structured_block (parser);
45623 ret = cp_parser_omp_for_loop (parser, OMP_LOOP, clauses, cclauses, if_p);
45625 cp_parser_end_omp_structured_block (parser, save);
45626 add_stmt (finish_omp_structured_block (sb));
45628 return ret;
45631 /* OpenMP 4.0:
45632 #pragma omp simd simd-clause[optseq] new-line
45633 for-loop */
45635 #define OMP_SIMD_CLAUSE_MASK \
45636 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
45637 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
45638 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
45639 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
45640 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
45641 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
45642 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
45643 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
45644 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
45645 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL) \
45646 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
45648 static tree
45649 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
45650 char *p_name, omp_clause_mask mask, tree *cclauses,
45651 bool *if_p)
45653 tree clauses, sb, ret;
45654 unsigned int save;
45655 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
45657 strcat (p_name, " simd");
45658 mask |= OMP_SIMD_CLAUSE_MASK;
45660 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
45661 cclauses == NULL);
45662 if (cclauses)
45664 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
45665 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
45668 keep_next_level (true);
45669 sb = begin_omp_structured_block ();
45670 save = cp_parser_begin_omp_structured_block (parser);
45672 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
45674 cp_parser_end_omp_structured_block (parser, save);
45675 add_stmt (finish_omp_structured_block (sb));
45677 return ret;
45680 /* OpenMP 2.5:
45681 #pragma omp for for-clause[optseq] new-line
45682 for-loop
45684 OpenMP 4.0:
45685 #pragma omp for simd for-simd-clause[optseq] new-line
45686 for-loop */
45688 #define OMP_FOR_CLAUSE_MASK \
45689 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
45690 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
45691 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
45692 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
45693 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
45694 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
45695 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
45696 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
45697 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
45698 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
45699 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
45701 static tree
45702 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
45703 char *p_name, omp_clause_mask mask, tree *cclauses,
45704 bool *if_p)
45706 tree clauses, sb, ret;
45707 unsigned int save;
45708 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
45710 strcat (p_name, " for");
45711 mask |= OMP_FOR_CLAUSE_MASK;
45712 /* parallel for{, simd} disallows nowait clause, but for
45713 target {teams distribute ,}parallel for{, simd} it should be accepted. */
45714 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
45715 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
45716 /* Composite distribute parallel for{, simd} disallows ordered clause. */
45717 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
45718 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
45720 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45722 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
45723 const char *p = IDENTIFIER_POINTER (id);
45725 if (strcmp (p, "simd") == 0)
45727 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
45728 if (cclauses == NULL)
45729 cclauses = cclauses_buf;
45731 cp_lexer_consume_token (parser->lexer);
45732 if (!flag_openmp) /* flag_openmp_simd */
45733 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
45734 cclauses, if_p);
45735 sb = begin_omp_structured_block ();
45736 save = cp_parser_begin_omp_structured_block (parser);
45737 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
45738 cclauses, if_p);
45739 cp_parser_end_omp_structured_block (parser, save);
45740 tree body = finish_omp_structured_block (sb);
45741 if (ret == NULL)
45742 return ret;
45743 ret = make_node (OMP_FOR);
45744 TREE_TYPE (ret) = void_type_node;
45745 OMP_FOR_BODY (ret) = body;
45746 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
45747 SET_EXPR_LOCATION (ret, loc);
45748 add_stmt (ret);
45749 return ret;
45752 if (!flag_openmp) /* flag_openmp_simd */
45754 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45755 return NULL_TREE;
45758 /* Composite distribute parallel for disallows linear clause. */
45759 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
45760 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
45762 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
45763 cclauses == NULL);
45764 if (cclauses)
45766 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
45767 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
45770 keep_next_level (true);
45771 sb = begin_omp_structured_block ();
45772 save = cp_parser_begin_omp_structured_block (parser);
45774 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
45776 cp_parser_end_omp_structured_block (parser, save);
45777 add_stmt (finish_omp_structured_block (sb));
45779 return ret;
45782 static tree cp_parser_omp_taskloop (cp_parser *, cp_token *, char *,
45783 omp_clause_mask, tree *, bool *);
45785 /* OpenMP 2.5:
45786 # pragma omp master new-line
45787 structured-block */
45789 static tree
45790 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok,
45791 char *p_name, omp_clause_mask mask, tree *cclauses,
45792 bool *if_p)
45794 tree clauses, sb, ret;
45795 unsigned int save;
45796 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
45798 strcat (p_name, " master");
45800 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45802 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
45803 const char *p = IDENTIFIER_POINTER (id);
45805 if (strcmp (p, "taskloop") == 0)
45807 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
45808 if (cclauses == NULL)
45809 cclauses = cclauses_buf;
45811 cp_lexer_consume_token (parser->lexer);
45812 if (!flag_openmp) /* flag_openmp_simd */
45813 return cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
45814 cclauses, if_p);
45815 sb = begin_omp_structured_block ();
45816 save = cp_parser_begin_omp_structured_block (parser);
45817 ret = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
45818 cclauses, if_p);
45819 cp_parser_end_omp_structured_block (parser, save);
45820 tree body = finish_omp_structured_block (sb);
45821 if (ret == NULL)
45822 return ret;
45823 ret = c_finish_omp_master (loc, body);
45824 OMP_MASTER_COMBINED (ret) = 1;
45825 return ret;
45828 if (!flag_openmp) /* flag_openmp_simd */
45830 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45831 return NULL_TREE;
45834 if (cclauses)
45836 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
45837 false);
45838 cp_omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
45840 else
45841 cp_parser_require_pragma_eol (parser, pragma_tok);
45843 return c_finish_omp_master (loc,
45844 cp_parser_omp_structured_block (parser, if_p));
45847 /* OpenMP 5.1:
45848 # pragma omp masked masked-clauses new-line
45849 structured-block */
45851 #define OMP_MASKED_CLAUSE_MASK \
45852 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FILTER)
45854 static tree
45855 cp_parser_omp_masked (cp_parser *parser, cp_token *pragma_tok,
45856 char *p_name, omp_clause_mask mask, tree *cclauses,
45857 bool *if_p)
45859 tree clauses, sb, ret;
45860 unsigned int save;
45861 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
45863 strcat (p_name, " masked");
45864 mask |= OMP_MASKED_CLAUSE_MASK;
45866 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45868 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
45869 const char *p = IDENTIFIER_POINTER (id);
45871 if (strcmp (p, "taskloop") == 0)
45873 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
45874 if (cclauses == NULL)
45875 cclauses = cclauses_buf;
45877 cp_lexer_consume_token (parser->lexer);
45878 if (!flag_openmp) /* flag_openmp_simd */
45879 return cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
45880 cclauses, if_p);
45881 sb = begin_omp_structured_block ();
45882 save = cp_parser_begin_omp_structured_block (parser);
45883 ret = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
45884 cclauses, if_p);
45885 cp_parser_end_omp_structured_block (parser, save);
45886 tree body = finish_omp_structured_block (sb);
45887 if (ret == NULL)
45888 return ret;
45889 ret = c_finish_omp_masked (loc, body,
45890 cclauses[C_OMP_CLAUSE_SPLIT_MASKED]);
45891 OMP_MASKED_COMBINED (ret) = 1;
45892 return ret;
45895 if (!flag_openmp) /* flag_openmp_simd */
45897 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45898 return NULL_TREE;
45901 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
45902 cclauses == NULL);
45903 if (cclauses)
45905 cp_omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
45906 clauses = cclauses[C_OMP_CLAUSE_SPLIT_MASKED];
45909 return c_finish_omp_masked (loc,
45910 cp_parser_omp_structured_block (parser, if_p),
45911 clauses);
45914 /* OpenMP 2.5:
45915 # pragma omp ordered new-line
45916 structured-block
45918 OpenMP 4.5:
45919 # pragma omp ordered ordered-clauses new-line
45920 structured-block */
45922 #define OMP_ORDERED_CLAUSE_MASK \
45923 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
45924 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
45926 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
45927 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
45928 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DOACROSS))
45930 static bool
45931 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
45932 enum pragma_context context, bool *if_p)
45934 location_t loc = pragma_tok->location;
45935 int n = 1;
45937 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
45938 n = 2;
45940 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_NAME))
45942 tree id = cp_lexer_peek_nth_token (parser->lexer, n)->u.value;
45943 const char *p = IDENTIFIER_POINTER (id);
45945 if (strcmp (p, "depend") == 0 || strcmp (p, "doacross") == 0)
45947 if (!flag_openmp) /* flag_openmp_simd */
45949 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45950 return false;
45952 if (context == pragma_stmt)
45954 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
45955 "%qs clause may only be used in compound "
45956 "statements", p);
45957 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45958 return true;
45960 tree clauses
45961 = cp_parser_omp_all_clauses (parser,
45962 OMP_ORDERED_DEPEND_CLAUSE_MASK,
45963 "#pragma omp ordered", pragma_tok);
45964 c_finish_omp_ordered (loc, clauses, NULL_TREE);
45965 return false;
45969 tree clauses
45970 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
45971 "#pragma omp ordered", pragma_tok);
45973 if (!flag_openmp /* flag_openmp_simd */
45974 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
45975 return false;
45977 c_finish_omp_ordered (loc, clauses,
45978 cp_parser_omp_structured_block (parser, if_p));
45979 return true;
45982 /* OpenMP 2.5:
45984 section-scope:
45985 { section-sequence }
45987 section-sequence:
45988 section-directive[opt] structured-block
45989 section-sequence section-directive structured-block */
45991 static tree
45992 cp_parser_omp_sections_scope (cp_parser *parser)
45994 tree stmt, substmt;
45995 bool error_suppress = false;
45996 cp_token *tok;
45998 matching_braces braces;
45999 if (!braces.require_open (parser))
46000 return NULL_TREE;
46002 stmt = push_stmt_list ();
46004 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
46005 != PRAGMA_OMP_SECTION
46006 && !cp_parser_omp_section_scan (parser, "section", true))
46008 substmt = cp_parser_omp_structured_block_sequence (parser,
46009 PRAGMA_OMP_SECTION);
46010 substmt = build1 (OMP_SECTION, void_type_node, substmt);
46011 add_stmt (substmt);
46014 while (1)
46016 tok = cp_lexer_peek_token (parser->lexer);
46017 if (tok->type == CPP_CLOSE_BRACE)
46018 break;
46019 if (tok->type == CPP_EOF)
46020 break;
46022 if (cp_parser_omp_section_scan (parser, "section", false))
46023 tok = cp_lexer_peek_token (parser->lexer);
46024 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
46026 cp_lexer_consume_token (parser->lexer);
46027 cp_parser_require_pragma_eol (parser, tok);
46028 error_suppress = false;
46030 else if (!error_suppress)
46032 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
46033 error_suppress = true;
46036 substmt = cp_parser_omp_structured_block_sequence (parser,
46037 PRAGMA_OMP_SECTION);
46038 substmt = build1 (OMP_SECTION, void_type_node, substmt);
46039 add_stmt (substmt);
46041 braces.require_close (parser);
46043 substmt = pop_stmt_list (stmt);
46045 stmt = make_node (OMP_SECTIONS);
46046 TREE_TYPE (stmt) = void_type_node;
46047 OMP_SECTIONS_BODY (stmt) = substmt;
46049 add_stmt (stmt);
46050 return stmt;
46053 /* OpenMP 2.5:
46054 # pragma omp sections sections-clause[optseq] newline
46055 sections-scope */
46057 #define OMP_SECTIONS_CLAUSE_MASK \
46058 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
46059 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
46060 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
46061 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
46062 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
46063 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
46065 static tree
46066 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
46067 char *p_name, omp_clause_mask mask, tree *cclauses)
46069 tree clauses, ret;
46070 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
46072 strcat (p_name, " sections");
46073 mask |= OMP_SECTIONS_CLAUSE_MASK;
46074 if (cclauses)
46075 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
46077 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
46078 cclauses == NULL);
46079 if (cclauses)
46081 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
46082 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
46085 ret = cp_parser_omp_sections_scope (parser);
46086 if (ret)
46087 OMP_SECTIONS_CLAUSES (ret) = clauses;
46089 return ret;
46092 /* OpenMP 2.5:
46093 # pragma omp parallel parallel-clause[optseq] new-line
46094 structured-block
46095 # pragma omp parallel for parallel-for-clause[optseq] new-line
46096 structured-block
46097 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
46098 structured-block
46100 OpenMP 4.0:
46101 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
46102 structured-block */
46104 #define OMP_PARALLEL_CLAUSE_MASK \
46105 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
46106 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
46107 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
46108 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
46109 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
46110 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
46111 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
46112 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
46113 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
46114 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
46116 static tree
46117 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
46118 char *p_name, omp_clause_mask mask, tree *cclauses,
46119 bool *if_p)
46121 tree stmt, clauses, block;
46122 unsigned int save;
46123 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
46125 strcat (p_name, " parallel");
46126 mask |= OMP_PARALLEL_CLAUSE_MASK;
46127 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
46128 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
46129 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
46130 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
46132 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
46134 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
46135 if (cclauses == NULL)
46136 cclauses = cclauses_buf;
46138 cp_lexer_consume_token (parser->lexer);
46139 if (!flag_openmp) /* flag_openmp_simd */
46140 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
46141 if_p);
46142 block = begin_omp_parallel ();
46143 save = cp_parser_begin_omp_structured_block (parser);
46144 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
46145 if_p);
46146 cp_parser_end_omp_structured_block (parser, save);
46147 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
46148 block);
46149 if (ret == NULL_TREE)
46150 return ret;
46151 OMP_PARALLEL_COMBINED (stmt) = 1;
46152 return stmt;
46154 /* When combined with distribute, parallel has to be followed by for.
46155 #pragma omp target parallel is allowed though. */
46156 else if (cclauses
46157 && (mask & (OMP_CLAUSE_MASK_1
46158 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
46160 error_at (loc, "expected %<for%> after %qs", p_name);
46161 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46162 return NULL_TREE;
46164 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46166 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46167 const char *p = IDENTIFIER_POINTER (id);
46168 if (cclauses == NULL && strcmp (p, "masked") == 0)
46170 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
46171 cclauses = cclauses_buf;
46173 cp_lexer_consume_token (parser->lexer);
46174 if (!flag_openmp) /* flag_openmp_simd */
46175 return cp_parser_omp_masked (parser, pragma_tok, p_name, mask,
46176 cclauses, if_p);
46177 block = begin_omp_parallel ();
46178 save = cp_parser_begin_omp_structured_block (parser);
46179 tree ret = cp_parser_omp_masked (parser, pragma_tok, p_name, mask,
46180 cclauses, if_p);
46181 cp_parser_end_omp_structured_block (parser, save);
46182 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
46183 block);
46184 if (ret == NULL_TREE)
46185 return ret;
46186 /* masked does have just filter clause, but during gimplification
46187 isn't represented by a gimplification omp context, so for
46188 #pragma omp parallel masked don't set OMP_PARALLEL_COMBINED,
46189 so that
46190 #pragma omp parallel masked
46191 #pragma omp taskloop simd lastprivate (x)
46192 isn't confused with
46193 #pragma omp parallel masked taskloop simd lastprivate (x) */
46194 if (OMP_MASKED_COMBINED (ret))
46195 OMP_PARALLEL_COMBINED (stmt) = 1;
46196 return stmt;
46198 else if (cclauses == NULL && strcmp (p, "master") == 0)
46200 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
46201 cclauses = cclauses_buf;
46203 cp_lexer_consume_token (parser->lexer);
46204 if (!flag_openmp) /* flag_openmp_simd */
46205 return cp_parser_omp_master (parser, pragma_tok, p_name, mask,
46206 cclauses, if_p);
46207 block = begin_omp_parallel ();
46208 save = cp_parser_begin_omp_structured_block (parser);
46209 tree ret = cp_parser_omp_master (parser, pragma_tok, p_name, mask,
46210 cclauses, if_p);
46211 cp_parser_end_omp_structured_block (parser, save);
46212 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
46213 block);
46214 if (ret == NULL_TREE)
46215 return ret;
46216 /* master doesn't have any clauses and during gimplification
46217 isn't represented by a gimplification omp context, so for
46218 #pragma omp parallel master don't set OMP_PARALLEL_COMBINED,
46219 so that
46220 #pragma omp parallel master
46221 #pragma omp taskloop simd lastprivate (x)
46222 isn't confused with
46223 #pragma omp parallel master taskloop simd lastprivate (x) */
46224 if (OMP_MASTER_COMBINED (ret))
46225 OMP_PARALLEL_COMBINED (stmt) = 1;
46226 return stmt;
46228 else if (strcmp (p, "loop") == 0)
46230 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
46231 if (cclauses == NULL)
46232 cclauses = cclauses_buf;
46234 cp_lexer_consume_token (parser->lexer);
46235 if (!flag_openmp) /* flag_openmp_simd */
46236 return cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
46237 cclauses, if_p);
46238 block = begin_omp_parallel ();
46239 save = cp_parser_begin_omp_structured_block (parser);
46240 tree ret = cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
46241 cclauses, if_p);
46242 cp_parser_end_omp_structured_block (parser, save);
46243 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
46244 block);
46245 if (ret == NULL_TREE)
46246 return ret;
46247 OMP_PARALLEL_COMBINED (stmt) = 1;
46248 return stmt;
46250 else if (!flag_openmp) /* flag_openmp_simd */
46252 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46253 return NULL_TREE;
46255 else if (cclauses == NULL && strcmp (p, "sections") == 0)
46257 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
46258 cclauses = cclauses_buf;
46260 cp_lexer_consume_token (parser->lexer);
46261 block = begin_omp_parallel ();
46262 save = cp_parser_begin_omp_structured_block (parser);
46263 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
46264 cp_parser_end_omp_structured_block (parser, save);
46265 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
46266 block);
46267 OMP_PARALLEL_COMBINED (stmt) = 1;
46268 return stmt;
46271 else if (!flag_openmp) /* flag_openmp_simd */
46273 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46274 return NULL_TREE;
46277 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
46278 cclauses == NULL);
46279 if (cclauses)
46281 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
46282 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
46285 block = begin_omp_parallel ();
46286 save = cp_parser_begin_omp_structured_block (parser);
46287 parser->omp_attrs_forbidden_p = true;
46288 cp_parser_statement (parser, NULL_TREE, false, if_p);
46289 cp_parser_end_omp_structured_block (parser, save);
46290 stmt = finish_omp_parallel (clauses, block);
46291 return stmt;
46294 /* OpenMP 2.5:
46295 # pragma omp single single-clause[optseq] new-line
46296 structured-block */
46298 #define OMP_SINGLE_CLAUSE_MASK \
46299 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
46300 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
46301 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
46302 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
46303 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
46305 static tree
46306 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
46308 tree stmt = make_node (OMP_SINGLE);
46309 TREE_TYPE (stmt) = void_type_node;
46310 SET_EXPR_LOCATION (stmt, pragma_tok->location);
46312 OMP_SINGLE_CLAUSES (stmt)
46313 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
46314 "#pragma omp single", pragma_tok);
46315 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
46317 return add_stmt (stmt);
46320 /* OpenMP 5.1:
46321 # pragma omp scope scope-clause[optseq] new-line
46322 structured-block */
46324 #define OMP_SCOPE_CLAUSE_MASK \
46325 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
46326 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
46327 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
46328 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
46329 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
46331 static tree
46332 cp_parser_omp_scope (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
46334 tree stmt = make_node (OMP_SCOPE);
46335 TREE_TYPE (stmt) = void_type_node;
46336 SET_EXPR_LOCATION (stmt, pragma_tok->location);
46338 OMP_SCOPE_CLAUSES (stmt)
46339 = cp_parser_omp_all_clauses (parser, OMP_SCOPE_CLAUSE_MASK,
46340 "#pragma omp scope", pragma_tok);
46341 OMP_SCOPE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
46343 return add_stmt (stmt);
46346 /* OpenMP 3.0:
46347 # pragma omp task task-clause[optseq] new-line
46348 structured-block */
46350 #define OMP_TASK_CLAUSE_MASK \
46351 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
46352 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
46353 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
46354 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
46355 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
46356 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
46357 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
46358 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
46359 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
46360 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
46361 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
46362 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION) \
46363 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DETACH) \
46364 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_AFFINITY))
46366 static tree
46367 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
46369 tree clauses, block;
46370 unsigned int save;
46372 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
46373 "#pragma omp task", pragma_tok);
46374 block = begin_omp_task ();
46375 save = cp_parser_begin_omp_structured_block (parser);
46376 parser->omp_attrs_forbidden_p = true;
46377 cp_parser_statement (parser, NULL_TREE, false, if_p);
46378 cp_parser_end_omp_structured_block (parser, save);
46379 return finish_omp_task (clauses, block);
46382 /* OpenMP 3.0:
46383 # pragma omp taskwait new-line
46385 OpenMP 5.0:
46386 # pragma omp taskwait taskwait-clause[opt] new-line */
46388 #define OMP_TASKWAIT_CLAUSE_MASK \
46389 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
46390 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
46392 static void
46393 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
46395 tree clauses
46396 = cp_parser_omp_all_clauses (parser, OMP_TASKWAIT_CLAUSE_MASK,
46397 "#pragma omp taskwait", pragma_tok);
46399 if (clauses)
46401 tree stmt = make_node (OMP_TASK);
46402 TREE_TYPE (stmt) = void_node;
46403 OMP_TASK_CLAUSES (stmt) = clauses;
46404 OMP_TASK_BODY (stmt) = NULL_TREE;
46405 SET_EXPR_LOCATION (stmt, pragma_tok->location);
46406 add_stmt (stmt);
46408 else
46409 finish_omp_taskwait ();
46412 /* OpenMP 3.1:
46413 # pragma omp taskyield new-line */
46415 static void
46416 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
46418 cp_parser_require_pragma_eol (parser, pragma_tok);
46419 finish_omp_taskyield ();
46422 /* OpenMP 4.0:
46423 # pragma omp taskgroup new-line
46424 structured-block
46426 OpenMP 5.0:
46427 # pragma omp taskgroup taskgroup-clause[optseq] new-line */
46429 #define OMP_TASKGROUP_CLAUSE_MASK \
46430 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
46431 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
46433 static tree
46434 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
46436 tree clauses
46437 = cp_parser_omp_all_clauses (parser, OMP_TASKGROUP_CLAUSE_MASK,
46438 "#pragma omp taskgroup", pragma_tok);
46439 return c_finish_omp_taskgroup (input_location,
46440 cp_parser_omp_structured_block (parser,
46441 if_p),
46442 clauses);
46446 /* OpenMP 2.5:
46447 # pragma omp threadprivate (variable-list) */
46449 static void
46450 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
46452 tree vars;
46454 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
46455 cp_parser_require_pragma_eol (parser, pragma_tok);
46457 finish_omp_threadprivate (vars);
46460 /* OpenMP 4.0:
46461 # pragma omp cancel cancel-clause[optseq] new-line */
46463 #define OMP_CANCEL_CLAUSE_MASK \
46464 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
46465 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
46466 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
46467 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
46468 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
46470 static void
46471 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
46473 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
46474 "#pragma omp cancel", pragma_tok);
46475 finish_omp_cancel (clauses);
46478 /* OpenMP 4.0:
46479 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
46481 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
46482 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
46483 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
46484 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
46485 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
46487 static bool
46488 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
46489 enum pragma_context context)
46491 tree clauses;
46492 bool point_seen = false;
46494 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46496 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46497 const char *p = IDENTIFIER_POINTER (id);
46499 if (strcmp (p, "point") == 0)
46501 cp_lexer_consume_token (parser->lexer);
46502 point_seen = true;
46505 if (!point_seen)
46507 cp_parser_error (parser, "expected %<point%>");
46508 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46509 return false;
46512 if (context != pragma_compound)
46514 if (context == pragma_stmt)
46515 error_at (pragma_tok->location,
46516 "%<#pragma %s%> may only be used in compound statements",
46517 "omp cancellation point");
46518 else
46519 cp_parser_error (parser, "expected declaration specifiers");
46520 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46521 return true;
46524 clauses = cp_parser_omp_all_clauses (parser,
46525 OMP_CANCELLATION_POINT_CLAUSE_MASK,
46526 "#pragma omp cancellation point",
46527 pragma_tok);
46528 finish_omp_cancellation_point (clauses);
46529 return true;
46532 /* OpenMP 4.0:
46533 #pragma omp distribute distribute-clause[optseq] new-line
46534 for-loop */
46536 #define OMP_DISTRIBUTE_CLAUSE_MASK \
46537 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
46538 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
46539 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
46540 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
46541 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
46542 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
46543 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
46545 static tree
46546 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
46547 char *p_name, omp_clause_mask mask, tree *cclauses,
46548 bool *if_p)
46550 tree clauses, sb, ret;
46551 unsigned int save;
46552 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
46554 strcat (p_name, " distribute");
46555 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
46557 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46559 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46560 const char *p = IDENTIFIER_POINTER (id);
46561 bool simd = false;
46562 bool parallel = false;
46564 if (strcmp (p, "simd") == 0)
46565 simd = true;
46566 else
46567 parallel = strcmp (p, "parallel") == 0;
46568 if (parallel || simd)
46570 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
46571 if (cclauses == NULL)
46572 cclauses = cclauses_buf;
46573 cp_lexer_consume_token (parser->lexer);
46574 if (!flag_openmp) /* flag_openmp_simd */
46576 if (simd)
46577 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
46578 cclauses, if_p);
46579 else
46580 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
46581 cclauses, if_p);
46583 sb = begin_omp_structured_block ();
46584 save = cp_parser_begin_omp_structured_block (parser);
46585 if (simd)
46586 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
46587 cclauses, if_p);
46588 else
46589 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
46590 cclauses, if_p);
46591 cp_parser_end_omp_structured_block (parser, save);
46592 tree body = finish_omp_structured_block (sb);
46593 if (ret == NULL)
46594 return ret;
46595 ret = make_node (OMP_DISTRIBUTE);
46596 TREE_TYPE (ret) = void_type_node;
46597 OMP_FOR_BODY (ret) = body;
46598 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
46599 SET_EXPR_LOCATION (ret, loc);
46600 add_stmt (ret);
46601 return ret;
46604 if (!flag_openmp) /* flag_openmp_simd */
46606 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46607 return NULL_TREE;
46610 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
46611 cclauses == NULL);
46612 if (cclauses)
46614 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
46615 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
46618 keep_next_level (true);
46619 sb = begin_omp_structured_block ();
46620 save = cp_parser_begin_omp_structured_block (parser);
46622 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
46624 cp_parser_end_omp_structured_block (parser, save);
46625 add_stmt (finish_omp_structured_block (sb));
46627 return ret;
46630 /* OpenMP 4.0:
46631 # pragma omp teams teams-clause[optseq] new-line
46632 structured-block */
46634 #define OMP_TEAMS_CLAUSE_MASK \
46635 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
46636 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
46637 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
46638 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
46639 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
46640 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
46641 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
46642 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
46644 static tree
46645 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
46646 char *p_name, omp_clause_mask mask, tree *cclauses,
46647 bool *if_p)
46649 tree clauses, sb, ret;
46650 unsigned int save;
46651 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
46653 strcat (p_name, " teams");
46654 mask |= OMP_TEAMS_CLAUSE_MASK;
46656 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46658 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46659 const char *p = IDENTIFIER_POINTER (id);
46660 if (strcmp (p, "distribute") == 0)
46662 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
46663 if (cclauses == NULL)
46664 cclauses = cclauses_buf;
46666 cp_lexer_consume_token (parser->lexer);
46667 if (!flag_openmp) /* flag_openmp_simd */
46668 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
46669 cclauses, if_p);
46670 keep_next_level (true);
46671 sb = begin_omp_structured_block ();
46672 save = cp_parser_begin_omp_structured_block (parser);
46673 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
46674 cclauses, if_p);
46675 cp_parser_end_omp_structured_block (parser, save);
46676 tree body = finish_omp_structured_block (sb);
46677 if (ret == NULL)
46678 return ret;
46679 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
46680 ret = make_node (OMP_TEAMS);
46681 TREE_TYPE (ret) = void_type_node;
46682 OMP_TEAMS_CLAUSES (ret) = clauses;
46683 OMP_TEAMS_BODY (ret) = body;
46684 OMP_TEAMS_COMBINED (ret) = 1;
46685 SET_EXPR_LOCATION (ret, loc);
46686 return add_stmt (ret);
46688 else if (strcmp (p, "loop") == 0)
46690 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
46691 if (cclauses == NULL)
46692 cclauses = cclauses_buf;
46694 cp_lexer_consume_token (parser->lexer);
46695 if (!flag_openmp) /* flag_openmp_simd */
46696 return cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
46697 cclauses, if_p);
46698 keep_next_level (true);
46699 sb = begin_omp_structured_block ();
46700 save = cp_parser_begin_omp_structured_block (parser);
46701 ret = cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
46702 cclauses, if_p);
46703 cp_parser_end_omp_structured_block (parser, save);
46704 tree body = finish_omp_structured_block (sb);
46705 if (ret == NULL)
46706 return ret;
46707 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
46708 ret = make_node (OMP_TEAMS);
46709 TREE_TYPE (ret) = void_type_node;
46710 OMP_TEAMS_CLAUSES (ret) = clauses;
46711 OMP_TEAMS_BODY (ret) = body;
46712 OMP_TEAMS_COMBINED (ret) = 1;
46713 SET_EXPR_LOCATION (ret, loc);
46714 return add_stmt (ret);
46717 if (!flag_openmp) /* flag_openmp_simd */
46719 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46720 return NULL_TREE;
46723 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
46724 cclauses == NULL);
46725 if (cclauses)
46727 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
46728 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
46731 tree stmt = make_node (OMP_TEAMS);
46732 TREE_TYPE (stmt) = void_type_node;
46733 OMP_TEAMS_CLAUSES (stmt) = clauses;
46734 keep_next_level (true);
46735 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
46736 SET_EXPR_LOCATION (stmt, loc);
46738 return add_stmt (stmt);
46741 /* OpenMP 4.0:
46742 # pragma omp target data target-data-clause[optseq] new-line
46743 structured-block */
46745 #define OMP_TARGET_DATA_CLAUSE_MASK \
46746 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
46747 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
46748 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
46749 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR) \
46750 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR))
46752 static tree
46753 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
46755 if (flag_openmp)
46756 omp_requires_mask
46757 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
46759 tree clauses
46760 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
46761 "#pragma omp target data", pragma_tok);
46762 c_omp_adjust_map_clauses (clauses, false);
46763 int map_seen = 0;
46764 for (tree *pc = &clauses; *pc;)
46766 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
46767 switch (OMP_CLAUSE_MAP_KIND (*pc))
46769 case GOMP_MAP_TO:
46770 case GOMP_MAP_ALWAYS_TO:
46771 case GOMP_MAP_PRESENT_TO:
46772 case GOMP_MAP_ALWAYS_PRESENT_TO:
46773 case GOMP_MAP_FROM:
46774 case GOMP_MAP_ALWAYS_FROM:
46775 case GOMP_MAP_PRESENT_FROM:
46776 case GOMP_MAP_ALWAYS_PRESENT_FROM:
46777 case GOMP_MAP_TOFROM:
46778 case GOMP_MAP_ALWAYS_TOFROM:
46779 case GOMP_MAP_PRESENT_TOFROM:
46780 case GOMP_MAP_ALWAYS_PRESENT_TOFROM:
46781 case GOMP_MAP_ALLOC:
46782 case GOMP_MAP_PRESENT_ALLOC:
46783 map_seen = 3;
46784 break;
46785 case GOMP_MAP_FIRSTPRIVATE_POINTER:
46786 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
46787 case GOMP_MAP_ALWAYS_POINTER:
46788 case GOMP_MAP_ATTACH_DETACH:
46789 case GOMP_MAP_ATTACH:
46790 break;
46791 default:
46792 map_seen |= 1;
46793 error_at (OMP_CLAUSE_LOCATION (*pc),
46794 "%<#pragma omp target data%> with map-type other "
46795 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
46796 "on %<map%> clause");
46797 *pc = OMP_CLAUSE_CHAIN (*pc);
46798 continue;
46800 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_PTR
46801 || OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_ADDR)
46802 map_seen = 3;
46803 pc = &OMP_CLAUSE_CHAIN (*pc);
46806 if (map_seen != 3)
46808 if (map_seen == 0)
46809 error_at (pragma_tok->location,
46810 "%<#pragma omp target data%> must contain at least "
46811 "one %<map%>, %<use_device_ptr%> or %<use_device_addr%> "
46812 "clause");
46813 return NULL_TREE;
46816 tree stmt = make_node (OMP_TARGET_DATA);
46817 TREE_TYPE (stmt) = void_type_node;
46818 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
46820 keep_next_level (true);
46821 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
46823 SET_EXPR_LOCATION (stmt, pragma_tok->location);
46824 return add_stmt (stmt);
46827 /* OpenMP 4.5:
46828 # pragma omp target enter data target-enter-data-clause[optseq] new-line
46829 structured-block */
46831 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
46832 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
46833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
46834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
46835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
46836 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
46838 static bool
46839 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
46840 enum pragma_context context)
46842 bool data_seen = false;
46843 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46845 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46846 const char *p = IDENTIFIER_POINTER (id);
46848 if (strcmp (p, "data") == 0)
46850 cp_lexer_consume_token (parser->lexer);
46851 data_seen = true;
46854 if (!data_seen)
46856 cp_parser_error (parser, "expected %<data%>");
46857 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46858 return false;
46861 if (context == pragma_stmt)
46863 error_at (pragma_tok->location,
46864 "%<#pragma %s%> may only be used in compound statements",
46865 "omp target enter data");
46866 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46867 return true;
46870 if (flag_openmp)
46871 omp_requires_mask
46872 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
46874 tree clauses
46875 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
46876 "#pragma omp target enter data", pragma_tok);
46877 c_omp_adjust_map_clauses (clauses, false);
46878 int map_seen = 0;
46879 for (tree *pc = &clauses; *pc;)
46881 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
46882 switch (OMP_CLAUSE_MAP_KIND (*pc))
46884 case GOMP_MAP_TO:
46885 case GOMP_MAP_ALWAYS_TO:
46886 case GOMP_MAP_PRESENT_TO:
46887 case GOMP_MAP_ALWAYS_PRESENT_TO:
46888 case GOMP_MAP_ALLOC:
46889 case GOMP_MAP_PRESENT_ALLOC:
46890 map_seen = 3;
46891 break;
46892 case GOMP_MAP_TOFROM:
46893 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_TO);
46894 map_seen = 3;
46895 break;
46896 case GOMP_MAP_ALWAYS_TOFROM:
46897 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_ALWAYS_TO);
46898 map_seen = 3;
46899 break;
46900 case GOMP_MAP_PRESENT_TOFROM:
46901 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_PRESENT_TO);
46902 map_seen = 3;
46903 break;
46904 case GOMP_MAP_ALWAYS_PRESENT_TOFROM:
46905 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_ALWAYS_PRESENT_TO);
46906 map_seen = 3;
46907 break;
46908 case GOMP_MAP_FIRSTPRIVATE_POINTER:
46909 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
46910 case GOMP_MAP_ALWAYS_POINTER:
46911 case GOMP_MAP_ATTACH_DETACH:
46912 case GOMP_MAP_ATTACH:
46913 break;
46914 default:
46915 map_seen |= 1;
46916 error_at (OMP_CLAUSE_LOCATION (*pc),
46917 "%<#pragma omp target enter data%> with map-type other "
46918 "than %<to%>, %<tofrom%> or %<alloc%> on %<map%> clause");
46919 *pc = OMP_CLAUSE_CHAIN (*pc);
46920 continue;
46922 pc = &OMP_CLAUSE_CHAIN (*pc);
46925 if (map_seen != 3)
46927 if (map_seen == 0)
46928 error_at (pragma_tok->location,
46929 "%<#pragma omp target enter data%> must contain at least "
46930 "one %<map%> clause");
46931 return true;
46934 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
46935 TREE_TYPE (stmt) = void_type_node;
46936 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
46937 SET_EXPR_LOCATION (stmt, pragma_tok->location);
46938 add_stmt (stmt);
46939 return true;
46942 /* OpenMP 4.5:
46943 # pragma omp target exit data target-enter-data-clause[optseq] new-line
46944 structured-block */
46946 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
46947 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
46948 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
46949 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
46950 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
46951 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
46953 static bool
46954 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
46955 enum pragma_context context)
46957 bool data_seen = false;
46958 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46960 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46961 const char *p = IDENTIFIER_POINTER (id);
46963 if (strcmp (p, "data") == 0)
46965 cp_lexer_consume_token (parser->lexer);
46966 data_seen = true;
46969 if (!data_seen)
46971 cp_parser_error (parser, "expected %<data%>");
46972 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46973 return false;
46976 if (context == pragma_stmt)
46978 error_at (pragma_tok->location,
46979 "%<#pragma %s%> may only be used in compound statements",
46980 "omp target exit data");
46981 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46982 return true;
46985 if (flag_openmp)
46986 omp_requires_mask
46987 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
46989 tree clauses
46990 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
46991 "#pragma omp target exit data", pragma_tok,
46992 false);
46993 clauses = finish_omp_clauses (clauses, C_ORT_OMP_EXIT_DATA);
46994 c_omp_adjust_map_clauses (clauses, false);
46995 int map_seen = 0;
46996 for (tree *pc = &clauses; *pc;)
46998 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
46999 switch (OMP_CLAUSE_MAP_KIND (*pc))
47001 case GOMP_MAP_FROM:
47002 case GOMP_MAP_ALWAYS_FROM:
47003 case GOMP_MAP_PRESENT_FROM:
47004 case GOMP_MAP_ALWAYS_PRESENT_FROM:
47005 case GOMP_MAP_RELEASE:
47006 case GOMP_MAP_DELETE:
47007 map_seen = 3;
47008 break;
47009 case GOMP_MAP_TOFROM:
47010 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_FROM);
47011 map_seen = 3;
47012 break;
47013 case GOMP_MAP_ALWAYS_TOFROM:
47014 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_ALWAYS_FROM);
47015 map_seen = 3;
47016 break;
47017 case GOMP_MAP_PRESENT_TOFROM:
47018 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_PRESENT_FROM);
47019 map_seen = 3;
47020 break;
47021 case GOMP_MAP_ALWAYS_PRESENT_TOFROM:
47022 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_ALWAYS_PRESENT_FROM);
47023 map_seen = 3;
47024 break;
47025 case GOMP_MAP_FIRSTPRIVATE_POINTER:
47026 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
47027 case GOMP_MAP_ALWAYS_POINTER:
47028 case GOMP_MAP_ATTACH_DETACH:
47029 case GOMP_MAP_DETACH:
47030 break;
47031 default:
47032 map_seen |= 1;
47033 error_at (OMP_CLAUSE_LOCATION (*pc),
47034 "%<#pragma omp target exit data%> with map-type other "
47035 "than %<from%>, %<tofrom%>, %<release%> or %<delete%> "
47036 "on %<map%> clause");
47037 *pc = OMP_CLAUSE_CHAIN (*pc);
47038 continue;
47040 pc = &OMP_CLAUSE_CHAIN (*pc);
47043 if (map_seen != 3)
47045 if (map_seen == 0)
47046 error_at (pragma_tok->location,
47047 "%<#pragma omp target exit data%> must contain at least "
47048 "one %<map%> clause");
47049 return true;
47052 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
47053 TREE_TYPE (stmt) = void_type_node;
47054 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
47055 SET_EXPR_LOCATION (stmt, pragma_tok->location);
47056 add_stmt (stmt);
47057 return true;
47060 /* OpenMP 4.0:
47061 # pragma omp target update target-update-clause[optseq] new-line */
47063 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
47064 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
47065 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
47066 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
47067 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
47068 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
47069 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
47071 static bool
47072 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
47073 enum pragma_context context)
47075 if (context == pragma_stmt)
47077 error_at (pragma_tok->location,
47078 "%<#pragma %s%> may only be used in compound statements",
47079 "omp target update");
47080 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47081 return true;
47084 tree clauses
47085 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
47086 "#pragma omp target update", pragma_tok);
47087 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
47088 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
47090 error_at (pragma_tok->location,
47091 "%<#pragma omp target update%> must contain at least one "
47092 "%<from%> or %<to%> clauses");
47093 return true;
47096 if (flag_openmp)
47097 omp_requires_mask
47098 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
47100 tree stmt = make_node (OMP_TARGET_UPDATE);
47101 TREE_TYPE (stmt) = void_type_node;
47102 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
47103 SET_EXPR_LOCATION (stmt, pragma_tok->location);
47104 add_stmt (stmt);
47105 return true;
47108 /* OpenMP 4.0:
47109 # pragma omp target target-clause[optseq] new-line
47110 structured-block */
47112 #define OMP_TARGET_CLAUSE_MASK \
47113 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
47114 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
47115 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
47116 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
47117 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
47118 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
47119 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
47120 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
47121 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
47122 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION) \
47123 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
47124 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR)\
47125 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HAS_DEVICE_ADDR))
47127 static bool
47128 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
47129 enum pragma_context context, bool *if_p)
47131 if (flag_openmp)
47132 omp_requires_mask
47133 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
47135 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47137 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
47138 const char *p = IDENTIFIER_POINTER (id);
47139 enum tree_code ccode = ERROR_MARK;
47141 if (strcmp (p, "teams") == 0)
47142 ccode = OMP_TEAMS;
47143 else if (strcmp (p, "parallel") == 0)
47144 ccode = OMP_PARALLEL;
47145 else if (strcmp (p, "simd") == 0)
47146 ccode = OMP_SIMD;
47147 if (ccode != ERROR_MARK)
47149 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
47150 char p_name[sizeof ("#pragma omp target teams distribute "
47151 "parallel for simd")];
47153 cp_lexer_consume_token (parser->lexer);
47154 strcpy (p_name, "#pragma omp target");
47155 if (!flag_openmp) /* flag_openmp_simd */
47157 tree stmt;
47158 switch (ccode)
47160 case OMP_TEAMS:
47161 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
47162 OMP_TARGET_CLAUSE_MASK,
47163 cclauses, if_p);
47164 break;
47165 case OMP_PARALLEL:
47166 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
47167 OMP_TARGET_CLAUSE_MASK,
47168 cclauses, if_p);
47169 break;
47170 case OMP_SIMD:
47171 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
47172 OMP_TARGET_CLAUSE_MASK,
47173 cclauses, if_p);
47174 break;
47175 default:
47176 gcc_unreachable ();
47178 return stmt != NULL_TREE;
47180 keep_next_level (true);
47181 tree sb = begin_omp_structured_block (), ret;
47182 unsigned save = cp_parser_begin_omp_structured_block (parser);
47183 switch (ccode)
47185 case OMP_TEAMS:
47186 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
47187 OMP_TARGET_CLAUSE_MASK, cclauses,
47188 if_p);
47189 break;
47190 case OMP_PARALLEL:
47191 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
47192 OMP_TARGET_CLAUSE_MASK, cclauses,
47193 if_p);
47194 break;
47195 case OMP_SIMD:
47196 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
47197 OMP_TARGET_CLAUSE_MASK, cclauses,
47198 if_p);
47199 break;
47200 default:
47201 gcc_unreachable ();
47203 cp_parser_end_omp_structured_block (parser, save);
47204 tree body = finish_omp_structured_block (sb);
47205 if (ret == NULL_TREE)
47206 return false;
47207 if (ccode == OMP_TEAMS && !processing_template_decl)
47208 /* For combined target teams, ensure the num_teams and
47209 thread_limit clause expressions are evaluated on the host,
47210 before entering the target construct. */
47211 for (tree c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
47212 c; c = OMP_CLAUSE_CHAIN (c))
47213 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
47214 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
47215 for (int i = 0;
47216 i <= (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS); ++i)
47217 if (OMP_CLAUSE_OPERAND (c, i)
47218 && TREE_CODE (OMP_CLAUSE_OPERAND (c, i)) != INTEGER_CST)
47220 tree expr = OMP_CLAUSE_OPERAND (c, i);
47221 expr = force_target_expr (TREE_TYPE (expr), expr,
47222 tf_none);
47223 if (expr == error_mark_node)
47224 continue;
47225 tree tmp = TARGET_EXPR_SLOT (expr);
47226 add_stmt (expr);
47227 OMP_CLAUSE_OPERAND (c, i) = expr;
47228 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
47229 OMP_CLAUSE_FIRSTPRIVATE);
47230 OMP_CLAUSE_DECL (tc) = tmp;
47231 OMP_CLAUSE_CHAIN (tc)
47232 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
47233 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
47235 c_omp_adjust_map_clauses (cclauses[C_OMP_CLAUSE_SPLIT_TARGET], true);
47236 finish_omp_target (pragma_tok->location,
47237 cclauses[C_OMP_CLAUSE_SPLIT_TARGET], body, true);
47238 return true;
47240 else if (!flag_openmp) /* flag_openmp_simd */
47242 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47243 return false;
47245 else if (strcmp (p, "data") == 0)
47247 cp_lexer_consume_token (parser->lexer);
47248 cp_parser_omp_target_data (parser, pragma_tok, if_p);
47249 return true;
47251 else if (strcmp (p, "enter") == 0)
47253 cp_lexer_consume_token (parser->lexer);
47254 return cp_parser_omp_target_enter_data (parser, pragma_tok, context);
47256 else if (strcmp (p, "exit") == 0)
47258 cp_lexer_consume_token (parser->lexer);
47259 return cp_parser_omp_target_exit_data (parser, pragma_tok, context);
47261 else if (strcmp (p, "update") == 0)
47263 cp_lexer_consume_token (parser->lexer);
47264 return cp_parser_omp_target_update (parser, pragma_tok, context);
47267 if (!flag_openmp) /* flag_openmp_simd */
47269 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47270 return false;
47273 tree clauses = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
47274 "#pragma omp target", pragma_tok,
47275 false);
47276 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
47277 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
47279 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
47280 OMP_CLAUSE_DECL (nc) = OMP_CLAUSE_DECL (c);
47281 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_ALWAYS_TOFROM);
47282 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (c);
47283 OMP_CLAUSE_CHAIN (c) = nc;
47285 clauses = finish_omp_clauses (clauses, C_ORT_OMP_TARGET);
47287 c_omp_adjust_map_clauses (clauses, true);
47288 keep_next_level (true);
47289 tree body = cp_parser_omp_structured_block (parser, if_p);
47291 finish_omp_target (pragma_tok->location, clauses, body, false);
47292 return true;
47295 /* OpenACC 2.0:
47296 # pragma acc cache (variable-list) new-line
47298 OpenACC 2.7:
47299 # pragma acc cache (readonly: variable-list) new-line
47302 static tree
47303 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
47305 /* Don't create location wrapper nodes within 'OMP_CLAUSE__CACHE_'
47306 clauses. */
47307 auto_suppress_location_wrappers sentinel;
47309 tree stmt, clauses = NULL_TREE;
47310 bool readonly = false;
47312 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
47314 cp_token *token = cp_lexer_peek_token (parser->lexer);
47315 if (token->type == CPP_NAME
47316 && !strcmp (IDENTIFIER_POINTER (token->u.value), "readonly")
47317 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
47319 cp_lexer_consume_token (parser->lexer);
47320 cp_lexer_consume_token (parser->lexer);
47321 readonly = true;
47323 clauses = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE__CACHE_,
47324 NULL, NULL);
47327 if (readonly)
47328 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
47329 OMP_CLAUSE__CACHE__READONLY (c) = 1;
47331 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
47333 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
47335 stmt = make_node (OACC_CACHE);
47336 TREE_TYPE (stmt) = void_type_node;
47337 OACC_CACHE_CLAUSES (stmt) = clauses;
47338 SET_EXPR_LOCATION (stmt, pragma_tok->location);
47339 add_stmt (stmt);
47341 return stmt;
47344 /* OpenACC 2.0:
47345 # pragma acc data oacc-data-clause[optseq] new-line
47346 structured-block */
47348 #define OACC_DATA_CLAUSE_MASK \
47349 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
47350 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
47351 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
47352 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
47353 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
47354 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
47355 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DETACH) \
47356 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
47357 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
47358 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
47359 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
47361 static tree
47362 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
47364 tree stmt, clauses, block;
47365 unsigned int save;
47367 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
47368 "#pragma acc data", pragma_tok);
47370 block = begin_omp_parallel ();
47371 save = cp_parser_begin_omp_structured_block (parser);
47372 cp_parser_statement (parser, NULL_TREE, false, if_p);
47373 cp_parser_end_omp_structured_block (parser, save);
47374 stmt = finish_oacc_data (clauses, block);
47375 return stmt;
47378 /* OpenACC 2.0:
47379 # pragma acc host_data <clauses> new-line
47380 structured-block */
47382 #define OACC_HOST_DATA_CLAUSE_MASK \
47383 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) \
47384 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
47385 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) )
47387 static tree
47388 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
47390 tree stmt, clauses, block;
47391 unsigned int save;
47393 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
47394 "#pragma acc host_data", pragma_tok,
47395 false);
47396 if (!omp_find_clause (clauses, OMP_CLAUSE_USE_DEVICE_PTR))
47398 error_at (pragma_tok->location,
47399 "%<host_data%> construct requires %<use_device%> clause");
47400 return error_mark_node;
47402 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
47403 block = begin_omp_parallel ();
47404 save = cp_parser_begin_omp_structured_block (parser);
47405 cp_parser_statement (parser, NULL_TREE, false, if_p);
47406 cp_parser_end_omp_structured_block (parser, save);
47407 stmt = finish_oacc_host_data (clauses, block);
47408 return stmt;
47411 /* OpenACC 2.0:
47412 # pragma acc declare oacc-data-clause[optseq] new-line
47415 #define OACC_DECLARE_CLAUSE_MASK \
47416 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
47417 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
47418 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
47419 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
47420 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
47421 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
47422 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
47423 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
47425 static tree
47426 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
47428 tree clauses, stmt;
47429 bool error = false;
47430 bool found_in_scope = global_bindings_p ();
47432 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
47433 "#pragma acc declare", pragma_tok);
47436 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
47438 error_at (pragma_tok->location,
47439 "no valid clauses specified in %<#pragma acc declare%>");
47440 return NULL_TREE;
47443 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
47445 location_t loc = OMP_CLAUSE_LOCATION (t);
47446 tree decl = OMP_CLAUSE_DECL (t);
47447 if (!DECL_P (decl))
47449 error_at (loc, "array section in %<#pragma acc declare%>");
47450 error = true;
47451 continue;
47453 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
47454 switch (OMP_CLAUSE_MAP_KIND (t))
47456 case GOMP_MAP_FIRSTPRIVATE_POINTER:
47457 case GOMP_MAP_ALLOC:
47458 case GOMP_MAP_TO:
47459 case GOMP_MAP_FORCE_DEVICEPTR:
47460 case GOMP_MAP_DEVICE_RESIDENT:
47461 break;
47463 case GOMP_MAP_LINK:
47464 if (!global_bindings_p ()
47465 && (TREE_STATIC (decl)
47466 || !DECL_EXTERNAL (decl)))
47468 error_at (loc,
47469 "%qD must be a global variable in "
47470 "%<#pragma acc declare link%>",
47471 decl);
47472 error = true;
47473 continue;
47475 break;
47477 default:
47478 if (global_bindings_p ())
47480 error_at (loc, "invalid OpenACC clause at file scope");
47481 error = true;
47482 continue;
47484 if (DECL_EXTERNAL (decl))
47486 error_at (loc,
47487 "invalid use of %<extern%> variable %qD "
47488 "in %<#pragma acc declare%>", decl);
47489 error = true;
47490 continue;
47492 else if (TREE_PUBLIC (decl))
47494 error_at (loc,
47495 "invalid use of %<global%> variable %qD "
47496 "in %<#pragma acc declare%>", decl);
47497 error = true;
47498 continue;
47500 break;
47503 if (!found_in_scope)
47504 /* This seems to ignore the existence of cleanup scopes?
47505 What is the meaning for local extern decls? The local
47506 extern is in this scope, but it is referring to a decl that
47507 is namespace scope. */
47508 for (tree d = current_binding_level->names; d; d = TREE_CHAIN (d))
47509 if (d == decl)
47511 found_in_scope = true;
47512 break;
47514 if (!found_in_scope)
47516 error_at (loc,
47517 "%qD must be a variable declared in the same scope as "
47518 "%<#pragma acc declare%>", decl);
47519 error = true;
47520 continue;
47523 if (!error)
47525 if (DECL_LOCAL_DECL_P (decl))
47526 /* We need to mark the aliased decl, as that is the entity
47527 that is being referred to. This won't work for
47528 dependent variables, but it didn't work for them before
47529 DECL_LOCAL_DECL_P was a thing either. But then
47530 dependent local extern variable decls are as rare as
47531 hen's teeth. */
47532 if (auto alias = DECL_LOCAL_DECL_ALIAS (decl))
47533 if (alias != error_mark_node)
47534 decl = alias;
47536 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
47537 || lookup_attribute ("omp declare target link",
47538 DECL_ATTRIBUTES (decl)))
47540 error_at (loc, "variable %qD used more than once with "
47541 "%<#pragma acc declare%>", decl);
47542 error = true;
47543 continue;
47546 tree id;
47547 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
47548 id = get_identifier ("omp declare target link");
47549 else
47550 id = get_identifier ("omp declare target");
47552 DECL_ATTRIBUTES (decl)
47553 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
47554 if (current_binding_level->kind == sk_namespace)
47556 symtab_node *node = symtab_node::get (decl);
47557 if (node != NULL)
47559 node->offloadable = 1;
47560 if (ENABLE_OFFLOADING)
47562 g->have_offload = true;
47563 if (is_a <varpool_node *> (node))
47564 vec_safe_push (offload_vars, decl);
47571 if (error || current_binding_level->kind == sk_namespace)
47572 return NULL_TREE;
47574 stmt = make_node (OACC_DECLARE);
47575 TREE_TYPE (stmt) = void_type_node;
47576 OACC_DECLARE_CLAUSES (stmt) = clauses;
47577 SET_EXPR_LOCATION (stmt, pragma_tok->location);
47579 add_stmt (stmt);
47581 return NULL_TREE;
47584 /* OpenACC 2.0:
47585 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
47589 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
47591 LOC is the location of the #pragma token.
47594 #define OACC_ENTER_DATA_CLAUSE_MASK \
47595 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
47596 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
47597 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
47598 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
47599 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
47600 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
47602 #define OACC_EXIT_DATA_CLAUSE_MASK \
47603 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
47604 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
47605 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
47606 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
47607 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DETACH) \
47608 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
47609 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
47611 static tree
47612 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
47613 bool enter)
47615 location_t loc = pragma_tok->location;
47616 tree stmt, clauses;
47617 const char *p = "";
47619 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47620 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
47622 if (strcmp (p, "data") != 0)
47624 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
47625 enter ? "enter" : "exit");
47626 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47627 return NULL_TREE;
47630 cp_lexer_consume_token (parser->lexer);
47632 if (enter)
47633 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
47634 "#pragma acc enter data", pragma_tok);
47635 else
47636 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
47637 "#pragma acc exit data", pragma_tok);
47639 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
47641 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
47642 enter ? "enter" : "exit");
47643 return NULL_TREE;
47646 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
47647 TREE_TYPE (stmt) = void_type_node;
47648 OMP_STANDALONE_CLAUSES (stmt) = clauses;
47649 SET_EXPR_LOCATION (stmt, loc);
47650 add_stmt (stmt);
47651 return stmt;
47654 /* OpenACC 2.0:
47655 # pragma acc loop oacc-loop-clause[optseq] new-line
47656 structured-block */
47658 #define OACC_LOOP_CLAUSE_MASK \
47659 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
47660 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
47661 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
47662 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
47663 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
47664 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
47665 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
47666 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
47667 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
47668 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
47670 static tree
47671 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
47672 omp_clause_mask mask, tree *cclauses, bool *if_p)
47674 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
47676 strcat (p_name, " loop");
47677 mask |= OACC_LOOP_CLAUSE_MASK;
47679 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
47680 /*finish_p=*/cclauses == NULL,
47681 /*target=*/is_parallel);
47682 if (cclauses)
47684 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
47685 if (*cclauses)
47686 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC_TARGET);
47687 if (clauses)
47688 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
47691 tree block = begin_omp_structured_block ();
47692 int save = cp_parser_begin_omp_structured_block (parser);
47693 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
47694 cp_parser_end_omp_structured_block (parser, save);
47696 /* Later processing of combined acc loop constructs gets confused
47697 by an extra level of empty nested BIND_EXPRs, so flatten them. */
47698 block = finish_omp_structured_block (block);
47699 if (TREE_CODE (block) == BIND_EXPR
47700 && TREE_CODE (BIND_EXPR_BODY (block)) == BIND_EXPR
47701 && !BIND_EXPR_VARS (block))
47702 block = BIND_EXPR_BODY (block);
47703 add_stmt (block);
47705 return stmt;
47708 /* OpenACC 2.0:
47709 # pragma acc kernels oacc-kernels-clause[optseq] new-line
47710 structured-block
47714 # pragma acc parallel oacc-parallel-clause[optseq] new-line
47715 structured-block
47717 OpenACC 2.6:
47719 # pragma acc serial oacc-serial-clause[optseq] new-line
47722 #define OACC_KERNELS_CLAUSE_MASK \
47723 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
47724 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
47725 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
47726 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
47727 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
47728 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
47729 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
47730 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
47731 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
47732 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
47733 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
47734 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
47735 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
47736 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
47737 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
47738 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
47740 #define OACC_PARALLEL_CLAUSE_MASK \
47741 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
47742 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
47743 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
47744 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
47745 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
47746 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
47747 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
47748 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
47749 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
47750 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
47751 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
47752 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
47753 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
47754 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
47755 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
47756 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
47757 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
47758 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
47759 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
47761 #define OACC_SERIAL_CLAUSE_MASK \
47762 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
47763 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
47764 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
47765 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
47766 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
47767 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
47768 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
47769 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
47770 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
47771 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
47772 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
47773 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
47774 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
47775 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
47776 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
47777 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
47779 static tree
47780 cp_parser_oacc_compute (cp_parser *parser, cp_token *pragma_tok,
47781 char *p_name, bool *if_p)
47783 omp_clause_mask mask;
47784 enum tree_code code;
47785 switch (cp_parser_pragma_kind (pragma_tok))
47787 case PRAGMA_OACC_KERNELS:
47788 strcat (p_name, " kernels");
47789 mask = OACC_KERNELS_CLAUSE_MASK;
47790 code = OACC_KERNELS;
47791 break;
47792 case PRAGMA_OACC_PARALLEL:
47793 strcat (p_name, " parallel");
47794 mask = OACC_PARALLEL_CLAUSE_MASK;
47795 code = OACC_PARALLEL;
47796 break;
47797 case PRAGMA_OACC_SERIAL:
47798 strcat (p_name, " serial");
47799 mask = OACC_SERIAL_CLAUSE_MASK;
47800 code = OACC_SERIAL;
47801 break;
47802 default:
47803 gcc_unreachable ();
47806 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47808 const char *p
47809 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
47810 if (strcmp (p, "loop") == 0)
47812 cp_lexer_consume_token (parser->lexer);
47813 tree block = begin_omp_parallel ();
47814 tree clauses;
47815 tree stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask,
47816 &clauses, if_p);
47817 protected_set_expr_location (stmt, pragma_tok->location);
47818 return finish_omp_construct (code, block, clauses);
47822 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
47823 /*finish_p=*/true,
47824 /*target=*/true);
47826 tree block = begin_omp_parallel ();
47827 unsigned int save = cp_parser_begin_omp_structured_block (parser);
47828 cp_parser_statement (parser, NULL_TREE, false, if_p);
47829 cp_parser_end_omp_structured_block (parser, save);
47830 return finish_omp_construct (code, block, clauses);
47833 /* OpenACC 2.0:
47834 # pragma acc update oacc-update-clause[optseq] new-line
47837 #define OACC_UPDATE_CLAUSE_MASK \
47838 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
47839 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
47840 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
47841 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
47842 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
47843 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
47844 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
47846 static tree
47847 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
47849 tree stmt, clauses;
47851 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
47852 "#pragma acc update", pragma_tok);
47854 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
47856 error_at (pragma_tok->location,
47857 "%<#pragma acc update%> must contain at least one "
47858 "%<device%> or %<host%> or %<self%> clause");
47859 return NULL_TREE;
47862 stmt = make_node (OACC_UPDATE);
47863 TREE_TYPE (stmt) = void_type_node;
47864 OACC_UPDATE_CLAUSES (stmt) = clauses;
47865 SET_EXPR_LOCATION (stmt, pragma_tok->location);
47866 add_stmt (stmt);
47867 return stmt;
47870 /* OpenACC 2.0:
47871 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
47873 LOC is the location of the #pragma token.
47876 #define OACC_WAIT_CLAUSE_MASK \
47877 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
47879 static tree
47880 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
47882 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
47883 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
47885 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
47886 list = cp_parser_oacc_wait_list (parser, loc, list);
47888 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
47889 "#pragma acc wait", pragma_tok);
47891 stmt = c_finish_oacc_wait (loc, list, clauses);
47892 stmt = finish_expr_stmt (stmt);
47894 return stmt;
47897 /* OpenMP 4.0:
47898 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
47900 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
47901 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
47902 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
47903 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
47904 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
47905 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
47906 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
47908 static void
47909 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
47910 enum pragma_context context,
47911 bool variant_p)
47913 bool first_p = parser->omp_declare_simd == NULL;
47914 cp_omp_declare_simd_data data;
47915 if (first_p)
47917 data.error_seen = false;
47918 data.fndecl_seen = false;
47919 data.variant_p = variant_p;
47920 data.tokens = vNULL;
47921 data.attribs[0] = NULL;
47922 data.attribs[1] = NULL;
47923 data.loc = UNKNOWN_LOCATION;
47924 /* It is safe to take the address of a local variable; it will only be
47925 used while this scope is live. */
47926 parser->omp_declare_simd = &data;
47928 else if (parser->omp_declare_simd->variant_p != variant_p)
47930 error_at (pragma_tok->location,
47931 "%<#pragma omp declare %s%> followed by "
47932 "%<#pragma omp declare %s%>",
47933 parser->omp_declare_simd->variant_p ? "variant" : "simd",
47934 parser->omp_declare_simd->variant_p ? "simd" : "variant");
47935 parser->omp_declare_simd->error_seen = true;
47938 /* Store away all pragma tokens. */
47939 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
47940 cp_lexer_consume_token (parser->lexer);
47941 cp_parser_require_pragma_eol (parser, pragma_tok);
47942 struct cp_token_cache *cp
47943 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
47944 parser->omp_declare_simd->tokens.safe_push (cp);
47946 if (first_p)
47948 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
47949 cp_parser_pragma (parser, context, NULL);
47950 switch (context)
47952 case pragma_external:
47953 cp_parser_declaration (parser, NULL_TREE);
47954 break;
47955 case pragma_member:
47956 cp_parser_member_declaration (parser);
47957 break;
47958 case pragma_objc_icode:
47959 cp_parser_block_declaration (parser, /*statement_p=*/false);
47960 break;
47961 default:
47962 cp_parser_declaration_statement (parser);
47963 break;
47965 if (parser->omp_declare_simd
47966 && !parser->omp_declare_simd->error_seen
47967 && !parser->omp_declare_simd->fndecl_seen)
47968 error_at (pragma_tok->location,
47969 "%<#pragma omp declare %s%> not immediately followed by "
47970 "function declaration or definition",
47971 parser->omp_declare_simd->variant_p ? "variant" : "simd");
47972 data.tokens.release ();
47973 parser->omp_declare_simd = NULL;
47977 /* OpenMP 5.0:
47979 trait-selector:
47980 trait-selector-name[([trait-score:]trait-property[,trait-property[,...]])]
47982 trait-score:
47983 score(score-expression)
47985 Note that this function returns a list of trait selectors for the
47986 trait-selector-set SET. */
47988 static tree
47989 cp_parser_omp_context_selector (cp_parser *parser, enum omp_tss_code set,
47990 bool has_parms_p)
47992 tree ret = NULL_TREE;
47995 tree selector;
47996 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
47997 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47998 selector = cp_lexer_peek_token (parser->lexer)->u.value;
47999 else
48001 cp_parser_error (parser, "expected trait selector name");
48002 return error_mark_node;
48005 enum omp_ts_code sel
48006 = omp_lookup_ts_code (set, IDENTIFIER_POINTER (selector));
48008 if (sel == OMP_TRAIT_INVALID)
48010 /* Per the spec, "Implementations can ignore specified selectors
48011 that are not those described in this section"; however, we
48012 must record such selectors because they cause match failures. */
48013 warning_at (cp_lexer_peek_token (parser->lexer)->location,
48014 OPT_Wopenmp,
48015 "unknown selector %qs for context selector set %qs",
48016 IDENTIFIER_POINTER (selector), omp_tss_map[set]);
48017 cp_lexer_consume_token (parser->lexer);
48018 ret = make_trait_selector (sel, NULL_TREE, NULL_TREE, ret);
48019 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
48020 for (size_t n = cp_parser_skip_balanced_tokens (parser, 1) - 1;
48021 n; --n)
48022 cp_lexer_consume_token (parser->lexer);
48023 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
48025 cp_lexer_consume_token (parser->lexer);
48026 continue;
48028 else
48029 break;
48032 cp_lexer_consume_token (parser->lexer);
48034 tree properties = NULL_TREE;
48035 tree scoreval = NULL_TREE;
48036 enum omp_tp_type property_kind = omp_ts_map[sel].tp_type;
48037 bool allow_score = omp_ts_map[sel].allow_score;
48038 tree t;
48040 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
48042 if (property_kind == OMP_TRAIT_PROPERTY_NONE)
48044 error ("selector %qs does not accept any properties",
48045 IDENTIFIER_POINTER (selector));
48046 return error_mark_node;
48049 matching_parens parens;
48050 parens.consume_open (parser);
48052 cp_token *token = cp_lexer_peek_token (parser->lexer);
48053 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
48054 && strcmp (IDENTIFIER_POINTER (token->u.value), "score") == 0
48055 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
48057 cp_lexer_save_tokens (parser->lexer);
48058 cp_lexer_consume_token (parser->lexer);
48059 cp_lexer_consume_token (parser->lexer);
48060 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
48061 true)
48062 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
48064 cp_lexer_rollback_tokens (parser->lexer);
48065 cp_lexer_consume_token (parser->lexer);
48067 matching_parens parens2;
48068 parens2.require_open (parser);
48069 tree score = cp_parser_constant_expression (parser);
48070 if (!parens2.require_close (parser))
48071 cp_parser_skip_to_closing_parenthesis (parser, true,
48072 false, true);
48073 cp_parser_require (parser, CPP_COLON, RT_COLON);
48074 if (!allow_score)
48075 error_at (token->location,
48076 "%<score%> cannot be specified in traits "
48077 "in the %qs trait-selector-set",
48078 omp_tss_map[set]);
48079 else if (score != error_mark_node)
48081 score = fold_non_dependent_expr (score);
48082 if (value_dependent_expression_p (score))
48083 scoreval = score;
48084 else if (!INTEGRAL_TYPE_P (TREE_TYPE (score))
48085 || TREE_CODE (score) != INTEGER_CST)
48086 error_at (token->location, "%<score%> argument must "
48087 "be constant integer expression");
48088 else if (tree_int_cst_sgn (score) < 0)
48089 error_at (token->location, "%<score%> argument must "
48090 "be non-negative");
48091 else
48092 scoreval = score;
48095 else
48096 cp_lexer_rollback_tokens (parser->lexer);
48098 token = cp_lexer_peek_token (parser->lexer);
48101 switch (property_kind)
48103 case OMP_TRAIT_PROPERTY_ID:
48104 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
48105 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
48107 tree prop = cp_lexer_peek_token (parser->lexer)->u.value;
48108 cp_lexer_consume_token (parser->lexer);
48109 properties = make_trait_property (prop, NULL_TREE,
48110 properties);
48112 else
48114 cp_parser_error (parser, "expected identifier");
48115 return error_mark_node;
48117 break;
48118 case OMP_TRAIT_PROPERTY_NAME_LIST:
48121 tree prop = OMP_TP_NAMELIST_NODE;
48122 tree value = NULL_TREE;
48123 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
48124 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
48126 value = cp_lexer_peek_token (parser->lexer)->u.value;
48127 cp_lexer_consume_token (parser->lexer);
48129 else if (cp_lexer_next_token_is (parser->lexer, CPP_STRING))
48130 value = cp_parser_string_literal (parser,
48131 /*translate=*/false,
48132 /*wide_ok=*/false);
48133 else
48135 cp_parser_error (parser, "expected identifier or "
48136 "string literal");
48137 return error_mark_node;
48140 properties = make_trait_property (prop, value, properties);
48142 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
48143 cp_lexer_consume_token (parser->lexer);
48144 else
48145 break;
48147 while (1);
48148 break;
48149 case OMP_TRAIT_PROPERTY_DEV_NUM_EXPR:
48150 case OMP_TRAIT_PROPERTY_BOOL_EXPR:
48151 /* FIXME: this is bogus, the expression need
48152 not be constant. */
48153 t = cp_parser_constant_expression (parser);
48154 if (t != error_mark_node)
48156 t = fold_non_dependent_expr (t);
48157 if (!value_dependent_expression_p (t)
48158 && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
48159 || !tree_fits_shwi_p (t)))
48160 error_at (token->location, "property must be "
48161 "constant integer expression");
48162 else
48163 properties = make_trait_property (NULL_TREE, t,
48164 properties);
48166 else
48167 return error_mark_node;
48168 break;
48169 case OMP_TRAIT_PROPERTY_CLAUSE_LIST:
48170 if (sel == OMP_TRAIT_CONSTRUCT_SIMD)
48172 if (!has_parms_p)
48174 error_at (token->location, "properties for %<simd%> "
48175 "selector may not be specified in "
48176 "%<metadirective%>");
48177 return error_mark_node;
48179 properties
48180 = cp_parser_omp_all_clauses (parser,
48181 OMP_DECLARE_SIMD_CLAUSE_MASK,
48182 "simd", NULL, true, 2);
48184 else if (sel == OMP_TRAIT_IMPLEMENTATION_REQUIRES)
48186 /* FIXME: The "requires" selector was added in OpenMP 5.1.
48187 Currently only the now-deprecated syntax
48188 from OpenMP 5.0 is supported. */
48189 sorry_at (token->location,
48190 "%<requires%> selector is not supported yet");
48191 return error_mark_node;
48193 else
48194 gcc_unreachable ();
48195 break;
48196 default:
48197 gcc_unreachable ();
48200 if (!parens.require_close (parser))
48201 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
48203 properties = nreverse (properties);
48205 else if (property_kind != OMP_TRAIT_PROPERTY_NONE
48206 && property_kind != OMP_TRAIT_PROPERTY_CLAUSE_LIST
48207 && property_kind != OMP_TRAIT_PROPERTY_EXTENSION)
48209 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
48210 return error_mark_node;
48213 ret = make_trait_selector (sel, scoreval, properties, ret);
48215 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
48216 cp_lexer_consume_token (parser->lexer);
48217 else
48218 break;
48220 while (1);
48222 return nreverse (ret);
48225 /* OpenMP 5.0:
48227 trait-set-selector[,trait-set-selector[,...]]
48229 trait-set-selector:
48230 trait-set-selector-name = { trait-selector[, trait-selector[, ...]] }
48232 trait-set-selector-name:
48233 constructor
48234 device
48235 implementation
48236 user */
48238 static tree
48239 cp_parser_omp_context_selector_specification (cp_parser *parser,
48240 bool has_parms_p)
48242 tree ret = NULL_TREE;
48245 const char *setp = "";
48246 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
48247 setp
48248 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
48249 enum omp_tss_code set = omp_lookup_tss_code (setp);
48251 if (set == OMP_TRAIT_SET_INVALID)
48253 cp_parser_error (parser, "expected context selector set name");
48254 return error_mark_node;
48257 cp_lexer_consume_token (parser->lexer);
48259 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
48260 return error_mark_node;
48262 matching_braces braces;
48263 if (!braces.require_open (parser))
48264 return error_mark_node;
48266 tree selectors
48267 = cp_parser_omp_context_selector (parser, set, has_parms_p);
48268 if (selectors == error_mark_node)
48270 cp_parser_skip_to_closing_brace (parser);
48271 ret = error_mark_node;
48273 else if (ret != error_mark_node)
48274 ret = make_trait_set_selector (set, selectors, ret);
48276 braces.require_close (parser);
48278 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
48279 cp_lexer_consume_token (parser->lexer);
48280 else
48281 break;
48283 while (1);
48285 if (ret == error_mark_node)
48286 return ret;
48287 return nreverse (ret);
48290 /* Assumption clauses:
48291 OpenMP 5.1
48292 absent (directive-name-list)
48293 contains (directive-name-list)
48294 holds (expression)
48295 no_openmp
48296 no_openmp_routines
48297 no_parallelism */
48299 static void
48300 cp_parser_omp_assumption_clauses (cp_parser *parser, cp_token *pragma_tok,
48301 bool is_assume)
48303 bool no_openmp = false;
48304 bool no_openmp_routines = false;
48305 bool no_parallelism = false;
48306 bitmap_head absent_head, contains_head;
48308 bitmap_obstack_initialize (NULL);
48309 bitmap_initialize (&absent_head, &bitmap_default_obstack);
48310 bitmap_initialize (&contains_head, &bitmap_default_obstack);
48312 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
48313 error_at (cp_lexer_peek_token (parser->lexer)->location,
48314 "expected at least one assumption clause");
48316 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
48318 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
48319 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
48320 cp_lexer_consume_token (parser->lexer);
48322 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
48323 break;
48325 const char *p
48326 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
48327 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
48329 if (!strcmp (p, "no_openmp"))
48331 cp_lexer_consume_token (parser->lexer);
48332 if (no_openmp)
48333 error_at (cloc, "too many %qs clauses", "no_openmp");
48334 no_openmp = true;
48336 else if (!strcmp (p, "no_openmp_routines"))
48338 cp_lexer_consume_token (parser->lexer);
48339 if (no_openmp_routines)
48340 error_at (cloc, "too many %qs clauses", "no_openmp_routines");
48341 no_openmp_routines = true;
48343 else if (!strcmp (p, "no_parallelism"))
48345 cp_lexer_consume_token (parser->lexer);
48346 if (no_parallelism)
48347 error_at (cloc, "too many %qs clauses", "no_parallelism");
48348 no_parallelism = true;
48350 else if (!strcmp (p, "holds"))
48352 cp_lexer_consume_token (parser->lexer);
48353 matching_parens parens;
48354 if (parens.require_open (parser))
48356 location_t eloc = cp_lexer_peek_token (parser->lexer)->location;
48357 tree t = cp_parser_assignment_expression (parser);
48358 if (!type_dependent_expression_p (t))
48359 t = contextual_conv_bool (t, tf_warning_or_error);
48360 if (is_assume && !error_operand_p (t))
48361 finish_expr_stmt (build_assume_call (eloc, t));
48362 if (!parens.require_close (parser))
48363 cp_parser_skip_to_closing_parenthesis (parser,
48364 /*recovering=*/true,
48365 /*or_comma=*/false,
48366 /*consume_paren=*/true);
48369 else if (!strcmp (p, "absent") || !strcmp (p, "contains"))
48371 cp_lexer_consume_token (parser->lexer);
48372 matching_parens parens;
48373 if (parens.require_open (parser))
48377 const char *directive[3] = {};
48378 int i;
48379 location_t dloc
48380 = cp_lexer_peek_token (parser->lexer)->location;
48381 for (i = 0; i < 3; i++)
48383 tree id;
48384 if (cp_lexer_nth_token_is (parser->lexer, i + 1, CPP_NAME))
48385 id = cp_lexer_peek_nth_token (parser->lexer,
48386 i + 1)->u.value;
48387 else if (cp_lexer_nth_token_is (parser->lexer, i + 1,
48388 CPP_KEYWORD))
48390 enum rid rid
48391 = cp_lexer_peek_nth_token (parser->lexer,
48392 i + 1)->keyword;
48393 id = ridpointers[rid];
48395 else
48396 break;
48397 directive[i] = IDENTIFIER_POINTER (id);
48399 if (i == 0)
48400 error_at (dloc, "expected directive name");
48401 else
48403 const struct c_omp_directive *dir
48404 = c_omp_categorize_directive (directive[0],
48405 directive[1],
48406 directive[2]);
48407 if (dir == NULL
48408 || dir->kind == C_OMP_DIR_DECLARATIVE
48409 || dir->kind == C_OMP_DIR_INFORMATIONAL
48410 || dir->id == PRAGMA_OMP_END
48411 || (!dir->second && directive[1])
48412 || (!dir->third && directive[2]))
48413 error_at (dloc, "unknown OpenMP directive name in "
48414 "%qs clause argument", p);
48415 else
48417 int id = dir - c_omp_directives;
48418 if (bitmap_bit_p (p[0] == 'a' ? &contains_head
48419 : &absent_head, id))
48420 error_at (dloc, "%<%s%s%s%s%s%> directive "
48421 "mentioned in both %<absent%> and "
48422 "%<contains%> clauses",
48423 directive[0],
48424 directive[1] ? " " : "",
48425 directive[1] ? directive[1] : "",
48426 directive[2] ? " " : "",
48427 directive[2] ? directive[2] : "");
48428 else if (!bitmap_set_bit (p[0] == 'a'
48429 ? &absent_head
48430 : &contains_head, id))
48431 error_at (dloc, "%<%s%s%s%s%s%> directive "
48432 "mentioned multiple times in %qs "
48433 "clauses",
48434 directive[0],
48435 directive[1] ? " " : "",
48436 directive[1] ? directive[1] : "",
48437 directive[2] ? " " : "",
48438 directive[2] ? directive[2] : "", p);
48440 for (; i; --i)
48441 cp_lexer_consume_token (parser->lexer);
48443 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
48444 cp_lexer_consume_token (parser->lexer);
48445 else
48446 break;
48448 while (1);
48449 if (!parens.require_close (parser))
48450 cp_parser_skip_to_closing_parenthesis (parser,
48451 /*recovering=*/true,
48452 /*or_comma=*/false,
48453 /*consume_paren=*/true);
48456 else if (startswith (p, "ext_"))
48458 warning_at (cloc, OPT_Wopenmp, "unknown assumption clause %qs", p);
48459 cp_lexer_consume_token (parser->lexer);
48460 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
48461 for (size_t n = cp_parser_skip_balanced_tokens (parser, 1) - 1;
48462 n; --n)
48463 cp_lexer_consume_token (parser->lexer);
48465 else
48467 cp_lexer_consume_token (parser->lexer);
48468 error_at (cloc, "expected assumption clause");
48469 break;
48472 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
48475 /* OpenMP 5.1
48476 # pragma omp assume clauses[optseq] new-line */
48478 static void
48479 cp_parser_omp_assume (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
48481 cp_parser_omp_assumption_clauses (parser, pragma_tok, true);
48482 add_stmt (cp_parser_omp_structured_block (parser, if_p));
48485 /* OpenMP 5.1
48486 # pragma omp assumes clauses[optseq] new-line */
48488 static bool
48489 cp_parser_omp_assumes (cp_parser *parser, cp_token *pragma_tok)
48491 cp_parser_omp_assumption_clauses (parser, pragma_tok, false);
48492 return false;
48495 /* Finalize #pragma omp declare variant after a fndecl has been parsed, and put
48496 that into "omp declare variant base" attribute. */
48498 static tree
48499 cp_finish_omp_declare_variant (cp_parser *parser, cp_token *pragma_tok,
48500 tree attrs)
48502 matching_parens parens;
48503 if (!parens.require_open (parser))
48505 fail:
48506 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
48507 return attrs;
48510 bool template_p;
48511 cp_id_kind idk = CP_ID_KIND_NONE;
48512 cp_token *varid_token = cp_lexer_peek_token (parser->lexer);
48513 cp_expr varid
48514 = cp_parser_id_expression (parser, /*template_keyword_p=*/false,
48515 /*check_dependency_p=*/true,
48516 /*template_p=*/&template_p,
48517 /*declarator_p=*/false,
48518 /*optional_p=*/false);
48519 parens.require_close (parser);
48521 tree variant;
48522 if (TREE_CODE (varid) == TEMPLATE_ID_EXPR
48523 || TREE_CODE (varid) == TYPE_DECL
48524 || varid == error_mark_node)
48525 variant = varid;
48526 else if (varid_token->type == CPP_NAME && varid_token->error_reported)
48527 variant = NULL_TREE;
48528 else
48530 tree ambiguous_decls;
48531 variant = cp_parser_lookup_name (parser, varid, none_type,
48532 template_p, /*is_namespace=*/false,
48533 /*check_dependency=*/true,
48534 &ambiguous_decls,
48535 varid.get_location ());
48536 if (ambiguous_decls)
48537 variant = NULL_TREE;
48539 if (variant == NULL_TREE)
48540 variant = error_mark_node;
48541 else if (TREE_CODE (variant) != SCOPE_REF)
48543 const char *error_msg;
48544 variant
48545 = finish_id_expression (varid, variant, parser->scope,
48546 &idk, false, true,
48547 &parser->non_integral_constant_expression_p,
48548 template_p, true, false, false, &error_msg,
48549 varid.get_location ());
48550 if (error_msg)
48551 cp_parser_error (parser, error_msg);
48553 location_t caret_loc = get_pure_location (varid.get_location ());
48554 location_t start_loc = get_start (varid_token->location);
48555 location_t finish_loc = get_finish (varid.get_location ());
48556 location_t varid_loc = make_location (caret_loc, start_loc, finish_loc);
48558 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
48559 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
48560 cp_lexer_consume_token (parser->lexer);
48562 const char *clause = "";
48563 location_t match_loc = cp_lexer_peek_token (parser->lexer)->location;
48564 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
48565 clause = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
48566 if (strcmp (clause, "match"))
48568 cp_parser_error (parser, "expected %<match%>");
48569 goto fail;
48572 cp_lexer_consume_token (parser->lexer);
48574 if (!parens.require_open (parser))
48575 goto fail;
48577 tree ctx = cp_parser_omp_context_selector_specification (parser, true);
48578 if (ctx == error_mark_node)
48579 goto fail;
48580 ctx = omp_check_context_selector (match_loc, ctx);
48581 if (ctx != error_mark_node && variant != error_mark_node)
48583 tree match_loc_node = maybe_wrap_with_location (integer_zero_node,
48584 match_loc);
48585 tree loc_node = maybe_wrap_with_location (integer_zero_node, varid_loc);
48586 loc_node = tree_cons (match_loc_node,
48587 build_int_cst (integer_type_node, idk),
48588 build_tree_list (loc_node, integer_zero_node));
48589 attrs = tree_cons (get_identifier ("omp declare variant base"),
48590 tree_cons (variant, ctx, loc_node), attrs);
48591 if (processing_template_decl)
48592 ATTR_IS_DEPENDENT (attrs) = 1;
48595 parens.require_close (parser);
48596 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
48597 return attrs;
48601 /* Finalize #pragma omp declare simd clauses after direct declarator has
48602 been parsed, and put that into "omp declare simd" attribute. */
48604 static tree
48605 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
48607 struct cp_token_cache *ce;
48608 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
48609 int i;
48611 if (!data->error_seen && data->fndecl_seen)
48613 error ("%<#pragma omp declare %s%> not immediately followed by "
48614 "a single function declaration or definition",
48615 data->variant_p ? "variant" : "simd");
48616 data->error_seen = true;
48618 if (data->error_seen)
48619 return attrs;
48621 FOR_EACH_VEC_ELT (data->tokens, i, ce)
48623 tree c, cl;
48625 cp_parser_push_lexer_for_tokens (parser, ce);
48626 parser->lexer->in_pragma = true;
48627 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
48628 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
48629 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
48630 const char *kind = IDENTIFIER_POINTER (id);
48631 cp_lexer_consume_token (parser->lexer);
48632 if (strcmp (kind, "simd") == 0)
48634 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
48635 "#pragma omp declare simd",
48636 pragma_tok);
48637 if (cl)
48638 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
48639 c = build_tree_list (get_identifier ("omp declare simd"), cl);
48640 TREE_CHAIN (c) = attrs;
48641 if (processing_template_decl)
48642 ATTR_IS_DEPENDENT (c) = 1;
48643 attrs = c;
48645 else
48647 gcc_assert (strcmp (kind, "variant") == 0);
48648 attrs
48649 = cp_finish_omp_declare_variant (parser, pragma_tok, attrs);
48651 cp_parser_pop_lexer (parser);
48654 cp_lexer *lexer = NULL;
48655 for (int i = 0; i < 2; i++)
48657 if (data->attribs[i] == NULL)
48658 continue;
48659 for (tree *pa = data->attribs[i]; *pa; )
48660 if (get_attribute_namespace (*pa) == omp_identifier
48661 && is_attribute_p ("directive", get_attribute_name (*pa)))
48663 for (tree a = TREE_VALUE (*pa); a; a = TREE_CHAIN (a))
48665 tree d = TREE_VALUE (a);
48666 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
48667 cp_token *first = DEFPARSE_TOKENS (d)->first;
48668 cp_token *last = DEFPARSE_TOKENS (d)->last;
48669 const char *directive[3] = {};
48670 for (int j = 0; j < 3; j++)
48672 tree id = NULL_TREE;
48673 if (first + j == last)
48674 break;
48675 if (first[j].type == CPP_NAME)
48676 id = first[j].u.value;
48677 else if (first[j].type == CPP_KEYWORD)
48678 id = ridpointers[(int) first[j].keyword];
48679 else
48680 break;
48681 directive[j] = IDENTIFIER_POINTER (id);
48683 const c_omp_directive *dir = NULL;
48684 if (directive[0])
48685 dir = c_omp_categorize_directive (directive[0], directive[1],
48686 directive[2]);
48687 if (dir == NULL)
48689 error_at (first->location,
48690 "unknown OpenMP directive name in "
48691 "%qs attribute argument",
48692 TREE_PUBLIC (d)
48693 ? "omp::decl" : "omp::directive");
48694 continue;
48696 if (dir->id != PRAGMA_OMP_DECLARE
48697 || (strcmp (directive[1], "simd") != 0
48698 && strcmp (directive[1], "variant") != 0))
48700 error_at (first->location,
48701 "OpenMP directive other than %<declare simd%> "
48702 "or %<declare variant%> appertains to a "
48703 "declaration");
48704 continue;
48707 if (parser->omp_attrs_forbidden_p)
48709 error_at (first->location,
48710 "mixing OpenMP directives with attribute and "
48711 "pragma syntax on the same statement");
48712 parser->omp_attrs_forbidden_p = false;
48715 if (!flag_openmp && strcmp (directive[1], "simd") != 0)
48716 continue;
48717 if (lexer == NULL)
48719 lexer = cp_lexer_alloc ();
48720 lexer->debugging_p = parser->lexer->debugging_p;
48722 vec_safe_reserve (lexer->buffer, (last - first) + 2);
48723 cp_token tok = {};
48724 tok.type = CPP_PRAGMA;
48725 tok.keyword = RID_MAX;
48726 tok.u.value = build_int_cst (NULL, PRAGMA_OMP_DECLARE);
48727 tok.location = first->location;
48728 lexer->buffer->quick_push (tok);
48729 while (++first < last)
48730 lexer->buffer->quick_push (*first);
48731 tok = {};
48732 tok.type = CPP_PRAGMA_EOL;
48733 tok.keyword = RID_MAX;
48734 tok.location = last->location;
48735 lexer->buffer->quick_push (tok);
48736 tok = {};
48737 tok.type = CPP_EOF;
48738 tok.keyword = RID_MAX;
48739 tok.location = last->location;
48740 lexer->buffer->quick_push (tok);
48741 lexer->next = parser->lexer;
48742 lexer->next_token = lexer->buffer->address ();
48743 lexer->last_token = lexer->next_token
48744 + lexer->buffer->length ()
48745 - 1;
48746 lexer->in_omp_attribute_pragma = true;
48747 parser->lexer = lexer;
48748 /* Move the current source position to that of the first token
48749 in the new lexer. */
48750 cp_lexer_set_source_position_from_token (lexer->next_token);
48752 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
48753 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
48754 const char *kind = IDENTIFIER_POINTER (id);
48755 cp_lexer_consume_token (parser->lexer);
48757 tree c, cl;
48758 if (strcmp (kind, "simd") == 0)
48760 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
48761 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
48762 cp_lexer_consume_token (parser->lexer);
48764 omp_clause_mask mask = OMP_DECLARE_SIMD_CLAUSE_MASK;
48765 cl = cp_parser_omp_all_clauses (parser, mask,
48766 "#pragma omp declare simd",
48767 pragma_tok);
48768 if (cl)
48769 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
48770 c = build_tree_list (get_identifier ("omp declare simd"),
48771 cl);
48772 TREE_CHAIN (c) = attrs;
48773 if (processing_template_decl)
48774 ATTR_IS_DEPENDENT (c) = 1;
48775 attrs = c;
48777 else
48779 gcc_assert (strcmp (kind, "variant") == 0);
48780 attrs
48781 = cp_finish_omp_declare_variant (parser, pragma_tok,
48782 attrs);
48784 gcc_assert (parser->lexer != lexer);
48785 vec_safe_truncate (lexer->buffer, 0);
48787 *pa = TREE_CHAIN (*pa);
48789 else
48790 pa = &TREE_CHAIN (*pa);
48792 if (lexer)
48793 cp_lexer_destroy (lexer);
48795 data->fndecl_seen = true;
48796 return attrs;
48799 /* D should be DEFERRED_PARSE from omp::decl attribute. If it contains
48800 a threadprivate, groupprivate, allocate or declare target directive,
48801 return true and parse it for DECL. */
48803 bool
48804 cp_maybe_parse_omp_decl (tree decl, tree d)
48806 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
48807 cp_token *first = DEFPARSE_TOKENS (d)->first;
48808 cp_token *last = DEFPARSE_TOKENS (d)->last;
48809 const char *directive[3] = {};
48810 for (int j = 0; j < 3; j++)
48812 tree id = NULL_TREE;
48813 if (first + j == last)
48814 break;
48815 if (first[j].type == CPP_NAME)
48816 id = first[j].u.value;
48817 else if (first[j].type == CPP_KEYWORD)
48818 id = ridpointers[(int) first[j].keyword];
48819 else
48820 break;
48821 directive[j] = IDENTIFIER_POINTER (id);
48823 const c_omp_directive *dir = NULL;
48824 if (directive[0])
48825 dir = c_omp_categorize_directive (directive[0], directive[1],
48826 directive[2]);
48827 if (dir == NULL)
48829 error_at (first->location,
48830 "unknown OpenMP directive name in "
48831 "%qs attribute argument", "omp::decl");
48832 return false;
48834 if (dir->id != PRAGMA_OMP_THREADPRIVATE
48835 /* && dir->id != PRAGMA_OMP_GROUPPRIVATE */
48836 && dir->id != PRAGMA_OMP_ALLOCATE
48837 && (dir->id != PRAGMA_OMP_DECLARE
48838 || strcmp (directive[1], "target") != 0))
48839 return false;
48841 if (!flag_openmp && !dir->simd)
48842 return true;
48844 cp_parser *parser = the_parser;
48845 cp_lexer *lexer = cp_lexer_alloc ();
48846 lexer->debugging_p = parser->lexer->debugging_p;
48847 lexer->in_omp_decl_attribute = decl;
48848 vec_safe_reserve (lexer->buffer, last - first + 3, true);
48849 cp_token tok = {};
48850 tok.type = CPP_PRAGMA;
48851 tok.keyword = RID_MAX;
48852 tok.u.value = build_int_cst (NULL, dir->id);
48853 tok.location = first->location;
48854 lexer->buffer->quick_push (tok);
48855 while (++first < last)
48856 lexer->buffer->quick_push (*first);
48857 tok = {};
48858 tok.type = CPP_PRAGMA_EOL;
48859 tok.keyword = RID_MAX;
48860 tok.location = last->location;
48861 lexer->buffer->quick_push (tok);
48862 tok = {};
48863 tok.type = CPP_EOF;
48864 tok.keyword = RID_MAX;
48865 tok.location = last->location;
48866 lexer->buffer->quick_push (tok);
48867 lexer->next = parser->lexer;
48868 lexer->next_token = lexer->buffer->address ();
48869 lexer->last_token = lexer->next_token
48870 + lexer->buffer->length ()
48871 - 1;
48872 lexer->in_omp_attribute_pragma = true;
48873 parser->lexer = lexer;
48874 /* Move the current source position to that of the first token in the
48875 new lexer. */
48876 cp_lexer_set_source_position_from_token (lexer->next_token);
48877 cp_parser_pragma (parser, pragma_external, NULL);
48879 return true;
48882 /* Helper for cp_parser_omp_declare_target, handle one to or link clause
48883 on #pragma omp declare target. Return false if errors were reported. */
48885 static bool
48886 handle_omp_declare_target_clause (tree c, tree t, int device_type,
48887 bool indirect)
48889 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
48890 tree at2 = lookup_attribute ("omp declare target link", DECL_ATTRIBUTES (t));
48891 tree id;
48892 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
48894 id = get_identifier ("omp declare target link");
48895 std::swap (at1, at2);
48897 else
48898 id = get_identifier ("omp declare target");
48899 if (at2)
48901 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
48902 error_at (OMP_CLAUSE_LOCATION (c),
48903 "%qD specified both in declare target %<link%> and %qs"
48904 " clauses", t, OMP_CLAUSE_ENTER_TO (c) ? "to" : "enter");
48905 else
48906 error_at (OMP_CLAUSE_LOCATION (c),
48907 "%qD specified both in declare target %<link%> and "
48908 "%<to%> or %<enter%> clauses", t);
48909 return false;
48911 if (!at1)
48913 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
48914 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
48915 return true;
48917 symtab_node *node = symtab_node::get (t);
48918 if (node != NULL)
48920 node->offloadable = 1;
48921 if (ENABLE_OFFLOADING)
48923 g->have_offload = true;
48924 if (is_a <varpool_node *> (node))
48925 vec_safe_push (offload_vars, t);
48929 if (TREE_CODE (t) != FUNCTION_DECL)
48930 return true;
48931 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_HOST) != 0)
48933 tree at3 = lookup_attribute ("omp declare target host",
48934 DECL_ATTRIBUTES (t));
48935 if (at3 == NULL_TREE)
48937 id = get_identifier ("omp declare target host");
48938 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
48941 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_NOHOST) != 0)
48943 tree at3 = lookup_attribute ("omp declare target nohost",
48944 DECL_ATTRIBUTES (t));
48945 if (at3 == NULL_TREE)
48947 id = get_identifier ("omp declare target nohost");
48948 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
48951 if (indirect)
48953 tree at4 = lookup_attribute ("omp declare target indirect",
48954 DECL_ATTRIBUTES (t));
48955 if (at4 == NULL_TREE)
48957 id = get_identifier ("omp declare target indirect");
48958 DECL_ATTRIBUTES (t)
48959 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
48962 return true;
48965 /* OpenMP 4.0:
48966 # pragma omp declare target new-line
48967 declarations and definitions
48968 # pragma omp end declare target new-line
48970 OpenMP 4.5:
48971 # pragma omp declare target ( extended-list ) new-line
48973 # pragma omp declare target declare-target-clauses[seq] new-line */
48975 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
48976 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
48977 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ENTER) \
48978 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK) \
48979 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE_TYPE) \
48980 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INDIRECT))
48982 static void
48983 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
48985 tree clauses = NULL_TREE;
48986 int device_type = 0;
48987 bool indirect = false;
48988 bool only_device_type_or_indirect = true;
48989 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
48990 || (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
48991 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME)))
48992 clauses
48993 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
48994 "#pragma omp declare target", pragma_tok);
48995 else if (parser->lexer->in_omp_decl_attribute
48996 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
48998 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_ENTER,
48999 clauses);
49000 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
49001 cp_parser_require_pragma_eol (parser, pragma_tok);
49003 else
49005 cp_omp_declare_target_attr a
49006 = { parser->lexer->in_omp_attribute_pragma, -1, false };
49007 vec_safe_push (scope_chain->omp_declare_target_attribute, a);
49008 cp_parser_require_pragma_eol (parser, pragma_tok);
49009 return;
49011 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
49013 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE)
49014 device_type |= OMP_CLAUSE_DEVICE_TYPE_KIND (c);
49015 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_INDIRECT)
49016 indirect |= !integer_zerop (OMP_CLAUSE_INDIRECT_EXPR (c));
49018 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
49020 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE
49021 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_INDIRECT)
49022 continue;
49023 tree t = OMP_CLAUSE_DECL (c);
49024 only_device_type_or_indirect = false;
49025 if (!handle_omp_declare_target_clause (c, t, device_type, indirect))
49026 continue;
49027 if (VAR_OR_FUNCTION_DECL_P (t)
49028 && DECL_LOCAL_DECL_P (t)
49029 && DECL_LANG_SPECIFIC (t)
49030 && DECL_LOCAL_DECL_ALIAS (t)
49031 && DECL_LOCAL_DECL_ALIAS (t) != error_mark_node)
49032 handle_omp_declare_target_clause (c, DECL_LOCAL_DECL_ALIAS (t),
49033 device_type, indirect);
49035 if ((device_type || indirect) && only_device_type_or_indirect)
49036 error_at (OMP_CLAUSE_LOCATION (clauses),
49037 "directive with only %<device_type%> or %<indirect%> clauses");
49038 if (indirect && device_type && device_type != OMP_CLAUSE_DEVICE_TYPE_ANY)
49039 error_at (OMP_CLAUSE_LOCATION (clauses),
49040 "%<device_type%> clause must specify 'any' when used with "
49041 "an %<indirect%> clause");
49044 /* OpenMP 5.1
49045 # pragma omp begin assumes clauses[optseq] new-line
49047 # pragma omp begin declare target clauses[optseq] new-line */
49049 #define OMP_BEGIN_DECLARE_TARGET_CLAUSE_MASK \
49050 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE_TYPE) \
49051 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INDIRECT))
49053 static void
49054 cp_parser_omp_begin (cp_parser *parser, cp_token *pragma_tok)
49056 const char *p = "";
49057 bool in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
49058 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
49060 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
49061 p = IDENTIFIER_POINTER (id);
49063 if (strcmp (p, "declare") == 0)
49065 cp_lexer_consume_token (parser->lexer);
49066 p = "";
49067 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
49069 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
49070 p = IDENTIFIER_POINTER (id);
49072 if (strcmp (p, "target") == 0)
49074 cp_lexer_consume_token (parser->lexer);
49075 tree clauses
49076 = cp_parser_omp_all_clauses (parser,
49077 OMP_BEGIN_DECLARE_TARGET_CLAUSE_MASK,
49078 "#pragma omp begin declare target",
49079 pragma_tok);
49080 int device_type = 0;
49081 bool indirect = 0;
49082 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
49084 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE)
49085 device_type |= OMP_CLAUSE_DEVICE_TYPE_KIND (c);
49086 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_INDIRECT)
49087 indirect |= !integer_zerop (OMP_CLAUSE_INDIRECT_EXPR (c));
49089 cp_omp_declare_target_attr a
49090 = { in_omp_attribute_pragma, device_type, indirect };
49091 vec_safe_push (scope_chain->omp_declare_target_attribute, a);
49093 else
49095 cp_parser_error (parser, "expected %<target%>");
49096 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
49099 else if (strcmp (p, "assumes") == 0)
49101 cp_lexer_consume_token (parser->lexer);
49102 cp_parser_omp_assumption_clauses (parser, pragma_tok, false);
49103 cp_omp_begin_assumes_data a = { in_omp_attribute_pragma };
49104 vec_safe_push (scope_chain->omp_begin_assumes, a);
49106 else
49108 cp_parser_error (parser, "expected %<declare target%> or %<assumes%>");
49109 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
49113 /* OpenMP 4.0:
49114 # pragma omp end declare target new-line
49116 OpenMP 5.1:
49117 # pragma omp end assumes new-line */
49119 static void
49120 cp_parser_omp_end (cp_parser *parser, cp_token *pragma_tok)
49122 const char *p = "";
49123 bool in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
49124 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
49126 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
49127 p = IDENTIFIER_POINTER (id);
49129 if (strcmp (p, "declare") == 0)
49131 cp_lexer_consume_token (parser->lexer);
49132 p = "";
49133 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
49135 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
49136 p = IDENTIFIER_POINTER (id);
49138 if (strcmp (p, "target") == 0)
49139 cp_lexer_consume_token (parser->lexer);
49140 else
49142 cp_parser_error (parser, "expected %<target%>");
49143 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
49144 return;
49146 cp_parser_require_pragma_eol (parser, pragma_tok);
49147 if (!vec_safe_length (scope_chain->omp_declare_target_attribute))
49148 error_at (pragma_tok->location,
49149 "%<#pragma omp end declare target%> without corresponding "
49150 "%<#pragma omp declare target%> or "
49151 "%<#pragma omp begin declare target%>");
49152 else
49154 cp_omp_declare_target_attr
49155 a = scope_chain->omp_declare_target_attribute->pop ();
49156 if (a.attr_syntax != in_omp_attribute_pragma)
49158 if (a.attr_syntax)
49159 error_at (pragma_tok->location,
49160 "%qs in attribute syntax terminated "
49161 "with %qs in pragma syntax",
49162 a.device_type >= 0 ? "begin declare target"
49163 : "declare target",
49164 "end declare target");
49165 else
49166 error_at (pragma_tok->location,
49167 "%qs in pragma syntax terminated "
49168 "with %qs in attribute syntax",
49169 a.device_type >= 0 ? "begin declare target"
49170 : "declare target",
49171 "end declare target");
49175 else if (strcmp (p, "assumes") == 0)
49177 cp_lexer_consume_token (parser->lexer);
49178 cp_parser_require_pragma_eol (parser, pragma_tok);
49179 if (!vec_safe_length (scope_chain->omp_begin_assumes))
49180 error_at (pragma_tok->location,
49181 "%qs without corresponding %qs",
49182 "#pragma omp end assumes", "#pragma omp begin assumes");
49183 else
49185 cp_omp_begin_assumes_data
49186 a = scope_chain->omp_begin_assumes->pop ();
49187 if (a.attr_syntax != in_omp_attribute_pragma)
49189 if (a.attr_syntax)
49190 error_at (pragma_tok->location,
49191 "%qs in attribute syntax terminated "
49192 "with %qs in pragma syntax",
49193 "begin assumes", "end assumes");
49194 else
49195 error_at (pragma_tok->location,
49196 "%qs in pragma syntax terminated "
49197 "with %qs in attribute syntax",
49198 "begin assumes", "end assumes");
49202 else
49204 cp_parser_error (parser, "expected %<declare%> or %<assumes%>");
49205 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
49206 return;
49210 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
49211 expression and optional initializer clause of
49212 #pragma omp declare reduction. We store the expression(s) as
49213 either 3, 6 or 7 special statements inside of the artificial function's
49214 body. The first two statements are DECL_EXPRs for the artificial
49215 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
49216 expression that uses those variables.
49217 If there was any INITIALIZER clause, this is followed by further statements,
49218 the fourth and fifth statements are DECL_EXPRs for the artificial
49219 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
49220 constructor variant (first token after open paren is not omp_priv),
49221 then the sixth statement is a statement with the function call expression
49222 that uses the OMP_PRIV and optionally OMP_ORIG variable.
49223 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
49224 to initialize the OMP_PRIV artificial variable and there is seventh
49225 statement, a DECL_EXPR of the OMP_PRIV statement again. */
49227 static bool
49228 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
49230 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
49231 gcc_assert (TYPE_REF_P (type));
49232 type = TREE_TYPE (type);
49233 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
49234 DECL_ARTIFICIAL (omp_out) = 1;
49235 pushdecl (omp_out);
49236 add_decl_expr (omp_out);
49237 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
49238 DECL_ARTIFICIAL (omp_in) = 1;
49239 pushdecl (omp_in);
49240 add_decl_expr (omp_in);
49241 tree combiner;
49242 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
49244 keep_next_level (true);
49245 tree block = begin_omp_structured_block ();
49246 combiner = cp_parser_expression (parser);
49247 finish_expr_stmt (combiner);
49248 block = finish_omp_structured_block (block);
49249 if (processing_template_decl)
49250 block = build_stmt (input_location, EXPR_STMT, block);
49251 add_stmt (block);
49253 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
49254 return false;
49256 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
49257 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
49258 cp_lexer_consume_token (parser->lexer);
49260 const char *p = "";
49261 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
49263 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
49264 p = IDENTIFIER_POINTER (id);
49267 if (strcmp (p, "initializer") == 0)
49269 cp_lexer_consume_token (parser->lexer);
49270 matching_parens parens;
49271 if (!parens.require_open (parser))
49272 return false;
49274 p = "";
49275 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
49277 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
49278 p = IDENTIFIER_POINTER (id);
49281 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
49282 DECL_ARTIFICIAL (omp_priv) = 1;
49283 pushdecl (omp_priv);
49284 add_decl_expr (omp_priv);
49285 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
49286 DECL_ARTIFICIAL (omp_orig) = 1;
49287 pushdecl (omp_orig);
49288 add_decl_expr (omp_orig);
49290 keep_next_level (true);
49291 block = begin_omp_structured_block ();
49293 bool ctor = false;
49294 if (strcmp (p, "omp_priv") == 0)
49296 bool is_non_constant_init;
49297 ctor = true;
49298 cp_lexer_consume_token (parser->lexer);
49299 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
49300 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
49301 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
49302 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
49303 == CPP_CLOSE_PAREN
49304 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
49305 == CPP_CLOSE_PAREN))
49307 finish_omp_structured_block (block);
49308 error ("invalid initializer clause");
49309 return false;
49311 initializer = cp_parser_initializer (parser,
49312 /*is_direct_init=*/nullptr,
49313 &is_non_constant_init);
49314 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
49315 NULL_TREE, LOOKUP_ONLYCONVERTING);
49317 else
49319 cp_parser_parse_tentatively (parser);
49320 /* Don't create location wrapper nodes here. */
49321 auto_suppress_location_wrappers sentinel;
49322 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
49323 /*check_dependency_p=*/true,
49324 /*template_p=*/NULL,
49325 /*declarator_p=*/false,
49326 /*optional_p=*/false);
49327 vec<tree, va_gc> *args;
49328 if (fn_name == error_mark_node
49329 || cp_parser_error_occurred (parser)
49330 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
49331 || ((args = cp_parser_parenthesized_expression_list
49332 (parser, non_attr, /*cast_p=*/false,
49333 /*allow_expansion_p=*/true,
49334 /*non_constant_p=*/NULL)),
49335 cp_parser_error_occurred (parser)))
49337 finish_omp_structured_block (block);
49338 cp_parser_abort_tentative_parse (parser);
49339 cp_parser_error (parser, "expected id-expression (arguments)");
49340 return false;
49342 unsigned int i;
49343 tree arg;
49344 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
49345 if (arg == omp_priv
49346 || (TREE_CODE (arg) == ADDR_EXPR
49347 && TREE_OPERAND (arg, 0) == omp_priv))
49348 break;
49349 cp_parser_abort_tentative_parse (parser);
49350 if (arg == NULL_TREE)
49351 error ("one of the initializer call arguments should be %<omp_priv%>"
49352 " or %<&omp_priv%>");
49353 initializer = cp_parser_postfix_expression (parser, false, false, false,
49354 false, NULL);
49355 finish_expr_stmt (initializer);
49358 block = finish_omp_structured_block (block);
49359 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
49360 if (processing_template_decl)
49361 block = build_stmt (input_location, EXPR_STMT, block);
49362 add_stmt (block);
49364 if (ctor)
49365 add_decl_expr (omp_orig);
49367 if (!parens.require_close (parser))
49368 return false;
49371 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
49372 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
49373 UNKNOWN_LOCATION);
49375 return true;
49378 /* OpenMP 4.0
49379 #pragma omp declare reduction (reduction-id : typename-list : expression) \
49380 initializer-clause[opt] new-line
49382 initializer-clause:
49383 initializer (omp_priv initializer)
49384 initializer (function-name (argument-list)) */
49386 static void
49387 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
49388 enum pragma_context)
49390 auto_vec<tree> types;
49391 enum tree_code reduc_code = ERROR_MARK;
49392 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
49393 unsigned int i;
49394 cp_token *first_token;
49395 cp_token_cache *cp;
49396 int errs;
49397 void *p;
49399 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
49400 p = obstack_alloc (&declarator_obstack, 0);
49402 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
49403 goto fail;
49405 switch (cp_lexer_peek_token (parser->lexer)->type)
49407 case CPP_PLUS:
49408 reduc_code = PLUS_EXPR;
49409 break;
49410 case CPP_MULT:
49411 reduc_code = MULT_EXPR;
49412 break;
49413 case CPP_MINUS:
49414 reduc_code = MINUS_EXPR;
49415 break;
49416 case CPP_AND:
49417 reduc_code = BIT_AND_EXPR;
49418 break;
49419 case CPP_XOR:
49420 reduc_code = BIT_XOR_EXPR;
49421 break;
49422 case CPP_OR:
49423 reduc_code = BIT_IOR_EXPR;
49424 break;
49425 case CPP_AND_AND:
49426 reduc_code = TRUTH_ANDIF_EXPR;
49427 break;
49428 case CPP_OR_OR:
49429 reduc_code = TRUTH_ORIF_EXPR;
49430 break;
49431 case CPP_NAME:
49432 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
49433 break;
49434 default:
49435 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
49436 "%<|%>, %<&&%>, %<||%> or identifier");
49437 goto fail;
49440 if (reduc_code != ERROR_MARK)
49441 cp_lexer_consume_token (parser->lexer);
49443 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
49444 if (reduc_id == error_mark_node)
49445 goto fail;
49447 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
49448 goto fail;
49450 /* Types may not be defined in declare reduction type list. */
49451 const char *saved_message;
49452 saved_message = parser->type_definition_forbidden_message;
49453 parser->type_definition_forbidden_message
49454 = G_("types may not be defined in declare reduction type list");
49455 bool saved_colon_corrects_to_scope_p;
49456 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
49457 parser->colon_corrects_to_scope_p = false;
49458 bool saved_colon_doesnt_start_class_def_p;
49459 saved_colon_doesnt_start_class_def_p
49460 = parser->colon_doesnt_start_class_def_p;
49461 parser->colon_doesnt_start_class_def_p = true;
49463 while (true)
49465 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
49466 type = cp_parser_type_id (parser);
49467 if (type == error_mark_node)
49469 else if (ARITHMETIC_TYPE_P (type)
49470 && (orig_reduc_id == NULL_TREE
49471 || (TREE_CODE (type) != COMPLEX_TYPE
49472 && (id_equal (orig_reduc_id, "min")
49473 || id_equal (orig_reduc_id, "max")))))
49474 error_at (loc, "predeclared arithmetic type %qT in "
49475 "%<#pragma omp declare reduction%>", type);
49476 else if (FUNC_OR_METHOD_TYPE_P (type)
49477 || TREE_CODE (type) == ARRAY_TYPE)
49478 error_at (loc, "function or array type %qT in "
49479 "%<#pragma omp declare reduction%>", type);
49480 else if (TYPE_REF_P (type))
49481 error_at (loc, "reference type %qT in "
49482 "%<#pragma omp declare reduction%>", type);
49483 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
49484 error_at (loc, "%<const%>, %<volatile%> or %<__restrict%>-qualified "
49485 "type %qT in %<#pragma omp declare reduction%>", type);
49486 else
49487 types.safe_push (type);
49489 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
49490 cp_lexer_consume_token (parser->lexer);
49491 else
49492 break;
49495 /* Restore the saved message. */
49496 parser->type_definition_forbidden_message = saved_message;
49497 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
49498 parser->colon_doesnt_start_class_def_p
49499 = saved_colon_doesnt_start_class_def_p;
49501 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
49502 || types.is_empty ())
49504 fail:
49505 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
49506 goto done;
49509 first_token = cp_lexer_peek_token (parser->lexer);
49510 cp = NULL;
49511 errs = errorcount;
49512 FOR_EACH_VEC_ELT (types, i, type)
49514 tree fntype
49515 = build_function_type_list (void_type_node,
49516 cp_build_reference_type (type, false),
49517 NULL_TREE);
49518 tree this_reduc_id = reduc_id;
49519 if (!dependent_type_p (type))
49520 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
49521 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
49522 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
49523 DECL_ARTIFICIAL (fndecl) = 1;
49524 DECL_EXTERNAL (fndecl) = 1;
49525 DECL_DECLARED_INLINE_P (fndecl) = 1;
49526 DECL_IGNORED_P (fndecl) = 1;
49527 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
49528 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
49529 DECL_ATTRIBUTES (fndecl)
49530 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
49531 DECL_ATTRIBUTES (fndecl));
49532 bool block_scope = false;
49533 if (current_function_decl)
49535 block_scope = true;
49536 DECL_CONTEXT (fndecl) = current_function_decl;
49537 DECL_LOCAL_DECL_P (fndecl) = true;
49540 if (processing_template_decl)
49541 fndecl = push_template_decl (fndecl);
49543 if (block_scope)
49545 if (!processing_template_decl)
49546 pushdecl (fndecl);
49548 else if (current_class_type)
49550 if (cp == NULL)
49552 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
49553 cp_lexer_consume_token (parser->lexer);
49554 cp = cp_token_cache_new (first_token,
49555 cp_lexer_peek_nth_token (parser->lexer,
49556 2));
49558 DECL_STATIC_FUNCTION_P (fndecl) = 1;
49559 finish_member_declaration (fndecl);
49560 DECL_PENDING_INLINE_INFO (fndecl) = cp;
49561 DECL_PENDING_INLINE_P (fndecl) = 1;
49562 vec_safe_push (unparsed_funs_with_definitions, fndecl);
49563 continue;
49565 else
49567 DECL_CONTEXT (fndecl) = current_namespace;
49568 tree d = pushdecl (fndecl);
49569 /* We should never meet a matched duplicate decl. */
49570 gcc_checking_assert (d == error_mark_node || d == fndecl);
49573 tree block = NULL_TREE;
49574 if (!block_scope)
49575 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
49576 else
49577 block = begin_omp_structured_block ();
49578 if (cp)
49580 cp_parser_push_lexer_for_tokens (parser, cp);
49581 parser->lexer->in_pragma = true;
49584 bool ok = cp_parser_omp_declare_reduction_exprs (fndecl, parser);
49586 if (cp)
49587 cp_parser_pop_lexer (parser);
49588 if (!block_scope)
49589 finish_function (/*inline_p=*/false);
49590 else
49592 DECL_CONTEXT (fndecl) = current_function_decl;
49593 if (DECL_TEMPLATE_INFO (fndecl))
49594 DECL_CONTEXT (DECL_TI_TEMPLATE (fndecl)) = current_function_decl;
49596 if (!ok)
49597 goto fail;
49599 if (block_scope)
49601 block = finish_omp_structured_block (block);
49602 if (TREE_CODE (block) == BIND_EXPR)
49603 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
49604 else if (TREE_CODE (block) == STATEMENT_LIST)
49605 DECL_SAVED_TREE (fndecl) = block;
49606 if (processing_template_decl)
49607 add_decl_expr (fndecl);
49610 cp_check_omp_declare_reduction (fndecl);
49611 if (cp == NULL && types.length () > 1)
49612 cp = cp_token_cache_new (first_token,
49613 cp_lexer_peek_nth_token (parser->lexer, 2));
49614 if (errs != errorcount)
49615 break;
49618 cp_parser_require_pragma_eol (parser, pragma_tok);
49620 done:
49621 /* Free any declarators allocated. */
49622 obstack_free (&declarator_obstack, p);
49625 /* OpenMP 4.0
49626 #pragma omp declare simd declare-simd-clauses[optseq] new-line
49627 #pragma omp declare reduction (reduction-id : typename-list : expression) \
49628 initializer-clause[opt] new-line
49629 #pragma omp declare target new-line
49631 OpenMP 5.0
49632 #pragma omp declare variant (identifier) match (context-selector) */
49634 static bool
49635 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
49636 enum pragma_context context)
49638 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
49640 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
49641 const char *p = IDENTIFIER_POINTER (id);
49643 if (strcmp (p, "simd") == 0)
49645 cp_lexer_consume_token (parser->lexer);
49646 cp_parser_omp_declare_simd (parser, pragma_tok,
49647 context, false);
49648 return true;
49650 if (flag_openmp && strcmp (p, "variant") == 0)
49652 cp_lexer_consume_token (parser->lexer);
49653 cp_parser_omp_declare_simd (parser, pragma_tok,
49654 context, true);
49655 return true;
49657 cp_ensure_no_omp_declare_simd (parser);
49658 if (strcmp (p, "reduction") == 0)
49660 cp_lexer_consume_token (parser->lexer);
49661 cp_parser_omp_declare_reduction (parser, pragma_tok,
49662 context);
49663 return false;
49665 if (!flag_openmp) /* flag_openmp_simd */
49667 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
49668 return false;
49670 if (strcmp (p, "target") == 0)
49672 cp_lexer_consume_token (parser->lexer);
49673 cp_parser_omp_declare_target (parser, pragma_tok);
49674 return false;
49677 cp_parser_error (parser, "expected %<simd%>, %<reduction%>, "
49678 "%<target%> or %<variant%>");
49679 cp_parser_require_pragma_eol (parser, pragma_tok);
49680 return false;
49683 /* OpenMP 5.0
49684 #pragma omp requires clauses[optseq] new-line */
49686 static bool
49687 cp_parser_omp_requires (cp_parser *parser, cp_token *pragma_tok)
49689 enum omp_requires new_req = (enum omp_requires) 0;
49691 location_t loc = pragma_tok->location;
49692 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
49694 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
49695 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
49696 cp_lexer_consume_token (parser->lexer);
49698 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
49700 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
49701 const char *p = IDENTIFIER_POINTER (id);
49702 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
49703 enum omp_requires this_req = (enum omp_requires) 0;
49705 if (!strcmp (p, "unified_address"))
49706 this_req = OMP_REQUIRES_UNIFIED_ADDRESS;
49707 else if (!strcmp (p, "unified_shared_memory"))
49708 this_req = OMP_REQUIRES_UNIFIED_SHARED_MEMORY;
49709 else if (!strcmp (p, "dynamic_allocators"))
49710 this_req = OMP_REQUIRES_DYNAMIC_ALLOCATORS;
49711 else if (!strcmp (p, "reverse_offload"))
49712 this_req = OMP_REQUIRES_REVERSE_OFFLOAD;
49713 else if (!strcmp (p, "atomic_default_mem_order"))
49715 cp_lexer_consume_token (parser->lexer);
49717 matching_parens parens;
49718 if (parens.require_open (parser))
49720 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
49722 id = cp_lexer_peek_token (parser->lexer)->u.value;
49723 p = IDENTIFIER_POINTER (id);
49725 if (!strcmp (p, "seq_cst"))
49726 this_req
49727 = (enum omp_requires) OMP_MEMORY_ORDER_SEQ_CST;
49728 else if (!strcmp (p, "relaxed"))
49729 this_req
49730 = (enum omp_requires) OMP_MEMORY_ORDER_RELAXED;
49731 else if (!strcmp (p, "release"))
49732 this_req
49733 = (enum omp_requires) OMP_MEMORY_ORDER_RELEASE;
49734 else if (!strcmp (p, "acq_rel"))
49735 this_req
49736 = (enum omp_requires) OMP_MEMORY_ORDER_ACQ_REL;
49737 else if (!strcmp (p, "acquire"))
49738 this_req
49739 = (enum omp_requires) OMP_MEMORY_ORDER_ACQUIRE;
49741 if (this_req == 0)
49743 error_at (cp_lexer_peek_token (parser->lexer)->location,
49744 "expected %<acq_rel%>, %<acquire%>, "
49745 "%<relaxed%>, %<release%> or %<seq_cst%>");
49746 switch (cp_lexer_peek_token (parser->lexer)->type)
49748 case CPP_EOF:
49749 case CPP_PRAGMA_EOL:
49750 case CPP_CLOSE_PAREN:
49751 break;
49752 default:
49753 if (cp_lexer_nth_token_is (parser->lexer, 2,
49754 CPP_CLOSE_PAREN))
49755 cp_lexer_consume_token (parser->lexer);
49756 break;
49759 else
49760 cp_lexer_consume_token (parser->lexer);
49762 if (!parens.require_close (parser))
49763 cp_parser_skip_to_closing_parenthesis (parser,
49764 /*recovering=*/true,
49765 /*or_comma=*/false,
49766 /*consume_paren=*/
49767 true);
49769 if (this_req == 0)
49771 cp_parser_require_pragma_eol (parser, pragma_tok);
49772 return false;
49775 p = NULL;
49777 else
49779 error_at (cloc, "expected %<unified_address%>, "
49780 "%<unified_shared_memory%>, "
49781 "%<dynamic_allocators%>, "
49782 "%<reverse_offload%> "
49783 "or %<atomic_default_mem_order%> clause");
49784 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
49785 return false;
49787 if (p)
49788 cp_lexer_consume_token (parser->lexer);
49789 if (this_req)
49791 if ((this_req & ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
49793 if ((this_req & new_req) != 0)
49794 error_at (cloc, "too many %qs clauses", p);
49795 if (this_req != OMP_REQUIRES_DYNAMIC_ALLOCATORS
49796 && (omp_requires_mask & OMP_REQUIRES_TARGET_USED) != 0)
49797 error_at (cloc, "%qs clause used lexically after first "
49798 "target construct or offloading API", p);
49800 else if ((new_req & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
49802 error_at (cloc, "too many %qs clauses",
49803 "atomic_default_mem_order");
49804 this_req = (enum omp_requires) 0;
49806 else if ((omp_requires_mask
49807 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
49809 error_at (cloc, "more than one %<atomic_default_mem_order%>"
49810 " clause in a single compilation unit");
49811 this_req
49812 = (enum omp_requires)
49813 (omp_requires_mask
49814 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER);
49816 else if ((omp_requires_mask
49817 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED) != 0)
49818 error_at (cloc, "%<atomic_default_mem_order%> clause used "
49819 "lexically after first %<atomic%> construct "
49820 "without memory order clause");
49821 new_req = (enum omp_requires) (new_req | this_req);
49822 omp_requires_mask
49823 = (enum omp_requires) (omp_requires_mask | this_req);
49824 continue;
49827 break;
49829 cp_parser_require_pragma_eol (parser, pragma_tok);
49831 if (new_req == 0)
49832 error_at (loc, "%<pragma omp requires%> requires at least one clause");
49833 return false;
49837 /* OpenMP 5.1:
49838 #pragma omp nothing new-line */
49840 static void
49841 cp_parser_omp_nothing (cp_parser *parser, cp_token *pragma_tok)
49843 cp_parser_require_pragma_eol (parser, pragma_tok);
49847 /* OpenMP 5.1
49848 #pragma omp error clauses[optseq] new-line */
49850 static bool
49851 cp_parser_omp_error (cp_parser *parser, cp_token *pragma_tok,
49852 enum pragma_context context)
49854 int at_compilation = -1;
49855 int severity_fatal = -1;
49856 tree message = NULL_TREE;
49857 bool bad = false;
49858 location_t loc = pragma_tok->location;
49860 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
49862 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
49863 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
49864 cp_lexer_consume_token (parser->lexer);
49866 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
49867 break;
49869 const char *p
49870 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
49871 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
49872 static const char *args[] = {
49873 "execution", "compilation", "warning", "fatal"
49875 int *v = NULL;
49876 int idx = 0, n = -1;
49877 tree m = NULL_TREE;
49879 if (!strcmp (p, "at"))
49880 v = &at_compilation;
49881 else if (!strcmp (p, "severity"))
49883 v = &severity_fatal;
49884 idx += 2;
49886 else if (strcmp (p, "message"))
49888 error_at (cloc,
49889 "expected %<at%>, %<severity%> or %<message%> clause");
49890 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
49891 return false;
49894 cp_lexer_consume_token (parser->lexer);
49896 matching_parens parens;
49897 if (parens.require_open (parser))
49899 if (v == NULL)
49901 m = cp_parser_assignment_expression (parser);
49902 if (type_dependent_expression_p (m))
49903 m = build1 (IMPLICIT_CONV_EXPR, const_string_type_node, m);
49904 else
49905 m = perform_implicit_conversion_flags (const_string_type_node, m,
49906 tf_warning_or_error,
49907 LOOKUP_NORMAL);
49909 else
49911 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
49913 tree val = cp_lexer_peek_token (parser->lexer)->u.value;
49914 const char *q = IDENTIFIER_POINTER (val);
49916 if (!strcmp (q, args[idx]))
49917 n = 0;
49918 else if (!strcmp (q, args[idx + 1]))
49919 n = 1;
49921 if (n == -1)
49923 error_at (cp_lexer_peek_token (parser->lexer)->location,
49924 "expected %qs or %qs", args[idx], args[idx + 1]);
49925 bad = true;
49926 switch (cp_lexer_peek_token (parser->lexer)->type)
49928 case CPP_EOF:
49929 case CPP_PRAGMA_EOL:
49930 case CPP_CLOSE_PAREN:
49931 break;
49932 default:
49933 if (cp_lexer_nth_token_is (parser->lexer, 2,
49934 CPP_CLOSE_PAREN))
49935 cp_lexer_consume_token (parser->lexer);
49936 break;
49939 else
49940 cp_lexer_consume_token (parser->lexer);
49943 if (!parens.require_close (parser))
49944 cp_parser_skip_to_closing_parenthesis (parser,
49945 /*recovering=*/true,
49946 /*or_comma=*/false,
49947 /*consume_paren=*/
49948 true);
49950 if (v == NULL)
49952 if (message)
49954 error_at (cloc, "too many %qs clauses", p);
49955 bad = true;
49957 else
49958 message = m;
49960 else if (n != -1)
49962 if (*v != -1)
49964 error_at (cloc, "too many %qs clauses", p);
49965 bad = true;
49967 else
49968 *v = n;
49971 else
49972 bad = true;
49974 cp_parser_require_pragma_eol (parser, pragma_tok);
49975 if (bad)
49976 return true;
49978 if (at_compilation == -1)
49979 at_compilation = 1;
49980 if (severity_fatal == -1)
49981 severity_fatal = 1;
49982 if (!at_compilation)
49984 if (context != pragma_compound)
49986 error_at (loc, "%<#pragma omp error%> with %<at(execution)%> clause "
49987 "may only be used in compound statements");
49988 return true;
49990 tree fndecl
49991 = builtin_decl_explicit (severity_fatal ? BUILT_IN_GOMP_ERROR
49992 : BUILT_IN_GOMP_WARNING);
49993 if (!message)
49994 message = build_zero_cst (const_string_type_node);
49995 tree stmt = build_call_expr_loc (loc, fndecl, 2, message,
49996 build_all_ones_cst (size_type_node));
49997 add_stmt (stmt);
49998 return true;
50001 if (in_discarded_stmt)
50002 return false;
50004 const char *msg = NULL;
50005 if (message)
50007 msg = c_getstr (fold_for_warn (message));
50008 if (msg == NULL)
50009 msg = _("<message unknown at compile time>");
50011 if (msg)
50012 emit_diagnostic (severity_fatal ? DK_ERROR : DK_WARNING, loc, 0,
50013 "%<pragma omp error%> encountered: %s", msg);
50014 else
50015 emit_diagnostic (severity_fatal ? DK_ERROR : DK_WARNING, loc, 0,
50016 "%<pragma omp error%> encountered");
50017 return false;
50020 /* OpenMP 4.5:
50021 #pragma omp taskloop taskloop-clause[optseq] new-line
50022 for-loop
50024 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
50025 for-loop */
50027 #define OMP_TASKLOOP_CLAUSE_MASK \
50028 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
50029 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
50030 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
50031 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
50032 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
50033 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
50034 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
50035 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
50036 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
50037 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
50038 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
50039 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
50040 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
50041 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
50042 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
50043 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
50044 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
50046 static tree
50047 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
50048 char *p_name, omp_clause_mask mask, tree *cclauses,
50049 bool *if_p)
50051 tree clauses, sb, ret;
50052 unsigned int save;
50053 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
50055 strcat (p_name, " taskloop");
50056 mask |= OMP_TASKLOOP_CLAUSE_MASK;
50057 /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
50058 clause. */
50059 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)) != 0)
50060 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION);
50062 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
50064 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
50065 const char *p = IDENTIFIER_POINTER (id);
50067 if (strcmp (p, "simd") == 0)
50069 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
50070 if (cclauses == NULL)
50071 cclauses = cclauses_buf;
50073 cp_lexer_consume_token (parser->lexer);
50074 if (!flag_openmp) /* flag_openmp_simd */
50075 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
50076 cclauses, if_p);
50077 sb = begin_omp_structured_block ();
50078 save = cp_parser_begin_omp_structured_block (parser);
50079 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
50080 cclauses, if_p);
50081 cp_parser_end_omp_structured_block (parser, save);
50082 tree body = finish_omp_structured_block (sb);
50083 if (ret == NULL)
50084 return ret;
50085 ret = make_node (OMP_TASKLOOP);
50086 TREE_TYPE (ret) = void_type_node;
50087 OMP_FOR_BODY (ret) = body;
50088 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
50089 SET_EXPR_LOCATION (ret, loc);
50090 add_stmt (ret);
50091 return ret;
50094 if (!flag_openmp) /* flag_openmp_simd */
50096 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
50097 return NULL_TREE;
50100 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
50101 cclauses == NULL);
50102 if (cclauses)
50104 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
50105 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
50108 keep_next_level (true);
50109 sb = begin_omp_structured_block ();
50110 save = cp_parser_begin_omp_structured_block (parser);
50112 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
50113 if_p);
50115 cp_parser_end_omp_structured_block (parser, save);
50116 add_stmt (finish_omp_structured_block (sb));
50118 return ret;
50122 /* OpenACC 2.0:
50123 # pragma acc routine oacc-routine-clause[optseq] new-line
50124 function-definition
50126 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
50129 #define OACC_ROUTINE_CLAUSE_MASK \
50130 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
50131 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
50132 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
50133 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
50134 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NOHOST) )
50136 /* Parse the OpenACC routine pragma. This has an optional '( name )'
50137 component, which must resolve to a declared namespace-scope
50138 function. The clauses are either processed directly (for a named
50139 function), or defered until the immediatley following declaration
50140 is parsed. */
50142 static void
50143 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
50144 enum pragma_context context)
50146 gcc_checking_assert (context == pragma_external);
50147 /* The checking for "another pragma following this one" in the "no optional
50148 '( name )'" case makes sure that we dont re-enter. */
50149 gcc_checking_assert (parser->oacc_routine == NULL);
50151 cp_oacc_routine_data data;
50152 data.error_seen = false;
50153 data.fndecl_seen = false;
50154 data.tokens = vNULL;
50155 data.clauses = NULL_TREE;
50156 data.loc = pragma_tok->location;
50157 /* It is safe to take the address of a local variable; it will only be
50158 used while this scope is live. */
50159 parser->oacc_routine = &data;
50161 /* Look for optional '( name )'. */
50162 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
50164 matching_parens parens;
50165 parens.consume_open (parser); /* '(' */
50167 /* We parse the name as an id-expression. If it resolves to
50168 anything other than a non-overloaded function at namespace
50169 scope, it's an error. */
50170 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
50171 tree name = cp_parser_id_expression (parser,
50172 /*template_keyword_p=*/false,
50173 /*check_dependency_p=*/false,
50174 /*template_p=*/NULL,
50175 /*declarator_p=*/false,
50176 /*optional_p=*/false);
50177 tree decl = (identifier_p (name)
50178 ? cp_parser_lookup_name_simple (parser, name, name_loc)
50179 : name);
50180 if (name != error_mark_node && decl == error_mark_node)
50181 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
50183 if (decl == error_mark_node
50184 || !parens.require_close (parser))
50186 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
50187 parser->oacc_routine = NULL;
50188 return;
50191 data.clauses
50192 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
50193 "#pragma acc routine",
50194 cp_lexer_peek_token (parser->lexer));
50195 /* The clauses are in reverse order; fix that to make later diagnostic
50196 emission easier. */
50197 data.clauses = nreverse (data.clauses);
50199 if (decl && is_overloaded_fn (decl)
50200 && (TREE_CODE (decl) != FUNCTION_DECL
50201 || DECL_FUNCTION_TEMPLATE_P (decl)))
50203 error_at (name_loc,
50204 "%<#pragma acc routine%> names a set of overloads");
50205 parser->oacc_routine = NULL;
50206 return;
50209 /* Perhaps we should use the same rule as declarations in different
50210 namespaces? */
50211 if (!DECL_NAMESPACE_SCOPE_P (decl))
50213 error_at (name_loc,
50214 "%qD does not refer to a namespace scope function", decl);
50215 parser->oacc_routine = NULL;
50216 return;
50219 if (TREE_CODE (decl) != FUNCTION_DECL)
50221 error_at (name_loc, "%qD does not refer to a function", decl);
50222 parser->oacc_routine = NULL;
50223 return;
50226 cp_finalize_oacc_routine (parser, decl, false);
50227 parser->oacc_routine = NULL;
50229 else /* No optional '( name )'. */
50231 /* Store away all pragma tokens. */
50232 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
50233 cp_lexer_consume_token (parser->lexer);
50234 cp_parser_require_pragma_eol (parser, pragma_tok);
50235 struct cp_token_cache *cp
50236 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
50237 parser->oacc_routine->tokens.safe_push (cp);
50239 /* Emit a helpful diagnostic if there's another pragma following this
50240 one. */
50241 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
50243 cp_ensure_no_oacc_routine (parser);
50244 data.tokens.release ();
50245 /* ..., and then just keep going. */
50246 return;
50249 /* We only have to consider the pragma_external case here. */
50250 cp_parser_declaration (parser, NULL_TREE);
50251 if (parser->oacc_routine
50252 && !parser->oacc_routine->fndecl_seen)
50253 cp_ensure_no_oacc_routine (parser);
50254 else
50255 parser->oacc_routine = NULL;
50256 data.tokens.release ();
50260 /* Finalize #pragma acc routine clauses after direct declarator has
50261 been parsed. */
50263 static tree
50264 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
50266 struct cp_token_cache *ce;
50267 cp_oacc_routine_data *data = parser->oacc_routine;
50269 if (!data->error_seen && data->fndecl_seen)
50271 error_at (data->loc,
50272 "%<#pragma acc routine%> not immediately followed by "
50273 "a single function declaration or definition");
50274 data->error_seen = true;
50276 if (data->error_seen)
50277 return attrs;
50279 gcc_checking_assert (data->tokens.length () == 1);
50280 ce = data->tokens[0];
50282 cp_parser_push_lexer_for_tokens (parser, ce);
50283 parser->lexer->in_pragma = true;
50284 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
50286 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
50287 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
50288 parser->oacc_routine->clauses
50289 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
50290 "#pragma acc routine", pragma_tok);
50291 /* The clauses are in reverse order; fix that to make later diagnostic
50292 emission easier. */
50293 parser->oacc_routine->clauses = nreverse (parser->oacc_routine->clauses);
50294 cp_parser_pop_lexer (parser);
50295 /* Later, cp_finalize_oacc_routine will process the clauses. */
50296 parser->oacc_routine->fndecl_seen = true;
50298 return attrs;
50301 /* Apply any saved OpenACC routine clauses to a just-parsed
50302 declaration. */
50304 static void
50305 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
50307 if (UNLIKELY (parser->oacc_routine != NULL))
50309 /* Keep going if we're in error reporting mode. */
50310 if (parser->oacc_routine->error_seen
50311 || fndecl == error_mark_node)
50312 return;
50314 if (TREE_CODE (fndecl) != FUNCTION_DECL)
50316 if (parser->oacc_routine->fndecl_seen)
50318 error_at (parser->oacc_routine->loc,
50319 "%<#pragma acc routine%> not immediately followed by"
50320 " a single function declaration or definition");
50321 parser->oacc_routine = NULL;
50322 return;
50325 cp_ensure_no_oacc_routine (parser);
50326 return;
50329 int compatible
50330 = oacc_verify_routine_clauses (fndecl, &parser->oacc_routine->clauses,
50331 parser->oacc_routine->loc,
50332 "#pragma acc routine");
50333 if (compatible < 0)
50335 parser->oacc_routine = NULL;
50336 return;
50338 if (compatible > 0)
50341 else
50343 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
50345 error_at (parser->oacc_routine->loc,
50346 TREE_USED (fndecl)
50347 ? G_("%<#pragma acc routine%> must be applied before"
50348 " use")
50349 : G_("%<#pragma acc routine%> must be applied before"
50350 " definition"));
50351 parser->oacc_routine = NULL;
50352 return;
50355 /* Set the routine's level of parallelism. */
50356 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
50357 oacc_replace_fn_attrib (fndecl, dims);
50359 /* Add an "omp declare target" attribute. */
50360 DECL_ATTRIBUTES (fndecl)
50361 = tree_cons (get_identifier ("omp declare target"),
50362 parser->oacc_routine->clauses,
50363 DECL_ATTRIBUTES (fndecl));
50368 /* Main entry point to OpenMP statement pragmas. */
50370 static void
50371 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
50373 tree stmt;
50374 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
50375 omp_clause_mask mask (0);
50377 switch (cp_parser_pragma_kind (pragma_tok))
50379 case PRAGMA_OACC_ATOMIC:
50380 cp_parser_omp_atomic (parser, pragma_tok, true);
50381 return;
50382 case PRAGMA_OACC_CACHE:
50383 stmt = cp_parser_oacc_cache (parser, pragma_tok);
50384 break;
50385 case PRAGMA_OACC_DATA:
50386 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
50387 break;
50388 case PRAGMA_OACC_ENTER_DATA:
50389 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
50390 break;
50391 case PRAGMA_OACC_EXIT_DATA:
50392 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
50393 break;
50394 case PRAGMA_OACC_HOST_DATA:
50395 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
50396 break;
50397 case PRAGMA_OACC_KERNELS:
50398 case PRAGMA_OACC_PARALLEL:
50399 case PRAGMA_OACC_SERIAL:
50400 strcpy (p_name, "#pragma acc");
50401 stmt = cp_parser_oacc_compute (parser, pragma_tok, p_name, if_p);
50402 break;
50403 case PRAGMA_OACC_LOOP:
50404 strcpy (p_name, "#pragma acc");
50405 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
50406 if_p);
50407 break;
50408 case PRAGMA_OACC_UPDATE:
50409 stmt = cp_parser_oacc_update (parser, pragma_tok);
50410 break;
50411 case PRAGMA_OACC_WAIT:
50412 stmt = cp_parser_oacc_wait (parser, pragma_tok);
50413 break;
50414 case PRAGMA_OMP_ALLOCATE:
50415 cp_parser_omp_allocate (parser, pragma_tok);
50416 return;
50417 case PRAGMA_OMP_ATOMIC:
50418 cp_parser_omp_atomic (parser, pragma_tok, false);
50419 return;
50420 case PRAGMA_OMP_CRITICAL:
50421 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
50422 break;
50423 case PRAGMA_OMP_DISTRIBUTE:
50424 strcpy (p_name, "#pragma omp");
50425 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
50426 if_p);
50427 break;
50428 case PRAGMA_OMP_FOR:
50429 strcpy (p_name, "#pragma omp");
50430 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
50431 if_p);
50432 break;
50433 case PRAGMA_OMP_LOOP:
50434 strcpy (p_name, "#pragma omp");
50435 stmt = cp_parser_omp_loop (parser, pragma_tok, p_name, mask, NULL,
50436 if_p);
50437 break;
50438 case PRAGMA_OMP_MASKED:
50439 strcpy (p_name, "#pragma omp");
50440 stmt = cp_parser_omp_masked (parser, pragma_tok, p_name, mask, NULL,
50441 if_p);
50442 break;
50443 case PRAGMA_OMP_MASTER:
50444 strcpy (p_name, "#pragma omp");
50445 stmt = cp_parser_omp_master (parser, pragma_tok, p_name, mask, NULL,
50446 if_p);
50447 break;
50448 case PRAGMA_OMP_PARALLEL:
50449 strcpy (p_name, "#pragma omp");
50450 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
50451 if_p);
50452 break;
50453 case PRAGMA_OMP_SCOPE:
50454 stmt = cp_parser_omp_scope (parser, pragma_tok, if_p);
50455 break;
50456 case PRAGMA_OMP_SECTIONS:
50457 strcpy (p_name, "#pragma omp");
50458 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
50459 break;
50460 case PRAGMA_OMP_SIMD:
50461 strcpy (p_name, "#pragma omp");
50462 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
50463 if_p);
50464 break;
50465 case PRAGMA_OMP_SINGLE:
50466 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
50467 break;
50468 case PRAGMA_OMP_TASK:
50469 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
50470 break;
50471 case PRAGMA_OMP_TASKGROUP:
50472 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
50473 break;
50474 case PRAGMA_OMP_TASKLOOP:
50475 strcpy (p_name, "#pragma omp");
50476 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
50477 if_p);
50478 break;
50479 case PRAGMA_OMP_TEAMS:
50480 strcpy (p_name, "#pragma omp");
50481 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
50482 if_p);
50483 break;
50484 case PRAGMA_OMP_ASSUME:
50485 cp_parser_omp_assume (parser, pragma_tok, if_p);
50486 return;
50487 default:
50488 gcc_unreachable ();
50491 protected_set_expr_location (stmt, pragma_tok->location);
50494 /* Transactional Memory parsing routines. */
50496 /* Parse a transaction attribute.
50498 txn-attribute:
50499 attribute
50500 [ [ identifier ] ]
50502 We use this instead of cp_parser_attributes_opt for transactions to avoid
50503 the pedwarn in C++98 mode. */
50505 static tree
50506 cp_parser_txn_attribute_opt (cp_parser *parser)
50508 cp_token *token;
50509 tree attr_name, attr = NULL;
50511 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
50512 return cp_parser_attributes_opt (parser);
50514 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
50515 return NULL_TREE;
50516 cp_lexer_consume_token (parser->lexer);
50517 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
50518 goto error1;
50520 token = cp_lexer_peek_token (parser->lexer);
50521 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
50523 token = cp_lexer_consume_token (parser->lexer);
50525 attr_name = (token->type == CPP_KEYWORD
50526 /* For keywords, use the canonical spelling,
50527 not the parsed identifier. */
50528 ? ridpointers[(int) token->keyword]
50529 : token->u.value);
50530 attr = build_tree_list (attr_name, NULL_TREE);
50532 else
50533 cp_parser_error (parser, "expected identifier");
50535 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
50536 error1:
50537 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
50538 return attr;
50541 /* Parse a __transaction_atomic or __transaction_relaxed statement.
50543 transaction-statement:
50544 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
50545 compound-statement
50546 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
50549 static tree
50550 cp_parser_transaction (cp_parser *parser, cp_token *token)
50552 unsigned char old_in = parser->in_transaction;
50553 unsigned char this_in = 1, new_in;
50554 enum rid keyword = token->keyword;
50555 tree stmt, attrs, noex;
50557 cp_lexer_consume_token (parser->lexer);
50559 if (keyword == RID_TRANSACTION_RELAXED
50560 || keyword == RID_SYNCHRONIZED)
50561 this_in |= TM_STMT_ATTR_RELAXED;
50562 else
50564 attrs = cp_parser_txn_attribute_opt (parser);
50565 if (attrs)
50566 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
50569 /* Parse a noexcept specification. */
50570 if (keyword == RID_ATOMIC_NOEXCEPT)
50571 noex = boolean_true_node;
50572 else if (keyword == RID_ATOMIC_CANCEL)
50574 /* cancel-and-throw is unimplemented. */
50575 sorry ("%<atomic_cancel%>");
50576 noex = NULL_TREE;
50578 else
50579 noex = cp_parser_noexcept_specification_opt (parser,
50580 CP_PARSER_FLAGS_NONE,
50581 /*require_constexpr=*/true,
50582 /*consumed_expr=*/NULL,
50583 /*return_cond=*/true);
50585 /* Keep track if we're in the lexical scope of an outer transaction. */
50586 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
50588 stmt = begin_transaction_stmt (token->location, NULL, this_in);
50590 parser->in_transaction = new_in;
50591 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
50592 parser->in_transaction = old_in;
50594 finish_transaction_stmt (stmt, NULL, this_in, noex);
50596 return stmt;
50599 /* Parse a __transaction_atomic or __transaction_relaxed expression.
50601 transaction-expression:
50602 __transaction_atomic txn-noexcept-spec[opt] ( expression )
50603 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
50606 static tree
50607 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
50609 unsigned char old_in = parser->in_transaction;
50610 unsigned char this_in = 1;
50611 cp_token *token;
50612 tree expr, noex;
50613 bool noex_expr;
50614 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
50616 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
50617 || keyword == RID_TRANSACTION_RELAXED);
50619 if (!flag_tm)
50620 error_at (loc,
50621 keyword == RID_TRANSACTION_RELAXED
50622 ? G_("%<__transaction_relaxed%> without transactional memory "
50623 "support enabled")
50624 : G_("%<__transaction_atomic%> without transactional memory "
50625 "support enabled"));
50627 token = cp_parser_require_keyword (parser, keyword,
50628 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
50629 : RT_TRANSACTION_RELAXED));
50630 gcc_assert (token != NULL);
50632 if (keyword == RID_TRANSACTION_RELAXED)
50633 this_in |= TM_STMT_ATTR_RELAXED;
50635 /* Set this early. This might mean that we allow transaction_cancel in
50636 an expression that we find out later actually has to be a constexpr.
50637 However, we expect that cxx_constant_value will be able to deal with
50638 this; also, if the noexcept has no constexpr, then what we parse next
50639 really is a transaction's body. */
50640 parser->in_transaction = this_in;
50642 /* Parse a noexcept specification. */
50643 noex = cp_parser_noexcept_specification_opt (parser,
50644 CP_PARSER_FLAGS_NONE,
50645 /*require_constexpr=*/false,
50646 &noex_expr,
50647 /*return_cond=*/true);
50649 if (!noex || !noex_expr
50650 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
50652 matching_parens parens;
50653 parens.require_open (parser);
50655 expr = cp_parser_expression (parser);
50656 expr = finish_parenthesized_expr (expr);
50658 parens.require_close (parser);
50660 else
50662 /* The only expression that is available got parsed for the noexcept
50663 already. noexcept is true then. */
50664 expr = noex;
50665 noex = boolean_true_node;
50668 expr = build_transaction_expr (token->location, expr, this_in, noex);
50669 parser->in_transaction = old_in;
50671 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
50672 return error_mark_node;
50674 return (flag_tm ? expr : error_mark_node);
50677 /* Parse a function-transaction-block.
50679 function-transaction-block:
50680 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
50681 function-body
50682 __transaction_atomic txn-attribute[opt] function-try-block
50683 __transaction_relaxed ctor-initializer[opt] function-body
50684 __transaction_relaxed function-try-block
50687 static void
50688 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
50690 unsigned char old_in = parser->in_transaction;
50691 unsigned char new_in = 1;
50692 tree compound_stmt, stmt, attrs;
50693 cp_token *token;
50695 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
50696 || keyword == RID_TRANSACTION_RELAXED);
50697 token = cp_parser_require_keyword (parser, keyword,
50698 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
50699 : RT_TRANSACTION_RELAXED));
50700 gcc_assert (token != NULL);
50702 if (keyword == RID_TRANSACTION_RELAXED)
50703 new_in |= TM_STMT_ATTR_RELAXED;
50704 else
50706 attrs = cp_parser_txn_attribute_opt (parser);
50707 if (attrs)
50708 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
50711 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
50713 parser->in_transaction = new_in;
50715 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
50716 cp_parser_function_try_block (parser);
50717 else
50718 cp_parser_ctor_initializer_opt_and_function_body
50719 (parser, /*in_function_try_block=*/false);
50721 parser->in_transaction = old_in;
50723 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
50726 /* Parse a __transaction_cancel statement.
50728 cancel-statement:
50729 __transaction_cancel txn-attribute[opt] ;
50730 __transaction_cancel txn-attribute[opt] throw-expression ;
50732 ??? Cancel and throw is not yet implemented. */
50734 static tree
50735 cp_parser_transaction_cancel (cp_parser *parser)
50737 cp_token *token;
50738 bool is_outer = false;
50739 tree stmt, attrs;
50741 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
50742 RT_TRANSACTION_CANCEL);
50743 gcc_assert (token != NULL);
50745 attrs = cp_parser_txn_attribute_opt (parser);
50746 if (attrs)
50747 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
50749 /* ??? Parse cancel-and-throw here. */
50751 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
50753 if (!flag_tm)
50755 error_at (token->location, "%<__transaction_cancel%> without "
50756 "transactional memory support enabled");
50757 return error_mark_node;
50759 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
50761 error_at (token->location, "%<__transaction_cancel%> within a "
50762 "%<__transaction_relaxed%>");
50763 return error_mark_node;
50765 else if (is_outer)
50767 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
50768 && !is_tm_may_cancel_outer (current_function_decl))
50770 error_at (token->location, "outer %<__transaction_cancel%> not "
50771 "within outer %<__transaction_atomic%>");
50772 error_at (token->location,
50773 " or a %<transaction_may_cancel_outer%> function");
50774 return error_mark_node;
50777 else if (parser->in_transaction == 0)
50779 error_at (token->location, "%<__transaction_cancel%> not within "
50780 "%<__transaction_atomic%>");
50781 return error_mark_node;
50784 stmt = build_tm_abort_call (token->location, is_outer);
50785 add_stmt (stmt);
50787 return stmt;
50791 /* Special handling for the first token or line in the file. The first
50792 thing in the file might be #pragma GCC pch_preprocess, which loads a
50793 PCH file, which is a GC collection point. So we need to handle this
50794 first pragma without benefit of an existing lexer structure.
50796 Always returns one token to the caller in *FIRST_TOKEN. This is
50797 either the true first token of the file, or the first token after
50798 the initial pragma. */
50800 static void
50801 cp_parser_initial_pragma (cp_token *first_token)
50803 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
50804 return;
50806 cp_lexer_get_preprocessor_token (0, first_token);
50808 tree name = NULL;
50809 if (first_token->type == CPP_STRING)
50811 name = first_token->u.value;
50813 cp_lexer_get_preprocessor_token (0, first_token);
50816 /* Skip to the end of the pragma. */
50817 if (first_token->type != CPP_PRAGMA_EOL)
50819 error_at (first_token->location,
50820 "malformed %<#pragma GCC pch_preprocess%>");
50822 cp_lexer_get_preprocessor_token (0, first_token);
50823 while (first_token->type != CPP_PRAGMA_EOL);
50826 /* Now actually load the PCH file. */
50827 if (name)
50828 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
50830 /* Read one more token to return to our caller. We have to do this
50831 after reading the PCH file in, since its pointers have to be
50832 live. */
50833 cp_lexer_get_preprocessor_token (0, first_token);
50836 /* Parse a pragma GCC ivdep. */
50838 static bool
50839 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
50841 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
50842 return true;
50845 /* Parse a pragma GCC unroll. */
50847 static tree
50848 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
50850 location_t location = cp_lexer_peek_token (parser->lexer)->location;
50851 tree unroll = cp_parser_constant_expression (parser);
50852 unroll = cp_check_pragma_unroll (location, fold_non_dependent_expr (unroll));
50853 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
50854 return unroll;
50857 /* Parse a pragma GCC novector. */
50859 static bool
50860 cp_parser_pragma_novector (cp_parser *parser, cp_token *pragma_tok)
50862 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
50863 return true;
50866 /* Normal parsing of a pragma token. Here we can (and must) use the
50867 regular lexer. */
50869 static bool
50870 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
50872 cp_token *pragma_tok;
50873 unsigned int id;
50874 tree stmt;
50875 bool ret = false;
50877 pragma_tok = cp_lexer_consume_token (parser->lexer);
50878 gcc_assert (pragma_tok->type == CPP_PRAGMA);
50879 parser->lexer->in_pragma = true;
50881 id = cp_parser_pragma_kind (pragma_tok);
50882 if (parser->omp_for_parse_state
50883 && parser->omp_for_parse_state->in_intervening_code
50884 && id >= PRAGMA_OMP__START_
50885 && id <= PRAGMA_OMP__LAST_)
50887 error_at (pragma_tok->location,
50888 "intervening code must not contain OpenMP directives");
50889 parser->omp_for_parse_state->fail = true;
50890 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
50891 return false;
50893 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
50894 cp_ensure_no_omp_declare_simd (parser);
50895 switch (id)
50897 case PRAGMA_GCC_PCH_PREPROCESS:
50898 error_at (pragma_tok->location,
50899 "%<#pragma GCC pch_preprocess%> must be first");
50900 break;
50902 case PRAGMA_OMP_BARRIER:
50903 switch (context)
50905 case pragma_compound:
50906 cp_parser_omp_barrier (parser, pragma_tok);
50907 return false;
50908 case pragma_stmt:
50909 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
50910 "used in compound statements", "omp barrier");
50911 ret = true;
50912 break;
50913 default:
50914 goto bad_stmt;
50916 break;
50918 case PRAGMA_OMP_DEPOBJ:
50919 switch (context)
50921 case pragma_compound:
50922 cp_parser_omp_depobj (parser, pragma_tok);
50923 return false;
50924 case pragma_stmt:
50925 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
50926 "used in compound statements", "omp depobj");
50927 ret = true;
50928 break;
50929 default:
50930 goto bad_stmt;
50932 break;
50934 case PRAGMA_OMP_FLUSH:
50935 switch (context)
50937 case pragma_compound:
50938 cp_parser_omp_flush (parser, pragma_tok);
50939 return false;
50940 case pragma_stmt:
50941 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
50942 "used in compound statements", "omp flush");
50943 ret = true;
50944 break;
50945 default:
50946 goto bad_stmt;
50948 break;
50950 case PRAGMA_OMP_TASKWAIT:
50951 switch (context)
50953 case pragma_compound:
50954 cp_parser_omp_taskwait (parser, pragma_tok);
50955 return false;
50956 case pragma_stmt:
50957 error_at (pragma_tok->location,
50958 "%<#pragma %s%> may only be used in compound statements",
50959 "omp taskwait");
50960 ret = true;
50961 break;
50962 default:
50963 goto bad_stmt;
50965 break;
50967 case PRAGMA_OMP_TASKYIELD:
50968 switch (context)
50970 case pragma_compound:
50971 cp_parser_omp_taskyield (parser, pragma_tok);
50972 return false;
50973 case pragma_stmt:
50974 error_at (pragma_tok->location,
50975 "%<#pragma %s%> may only be used in compound statements",
50976 "omp taskyield");
50977 ret = true;
50978 break;
50979 default:
50980 goto bad_stmt;
50982 break;
50984 case PRAGMA_OMP_CANCEL:
50985 switch (context)
50987 case pragma_compound:
50988 cp_parser_omp_cancel (parser, pragma_tok);
50989 return false;
50990 case pragma_stmt:
50991 error_at (pragma_tok->location,
50992 "%<#pragma %s%> may only be used in compound statements",
50993 "omp cancel");
50994 ret = true;
50995 break;
50996 default:
50997 goto bad_stmt;
50999 break;
51001 case PRAGMA_OMP_CANCELLATION_POINT:
51002 return cp_parser_omp_cancellation_point (parser, pragma_tok, context);
51004 case PRAGMA_OMP_THREADPRIVATE:
51005 cp_parser_omp_threadprivate (parser, pragma_tok);
51006 return false;
51008 case PRAGMA_OMP_DECLARE:
51009 return cp_parser_omp_declare (parser, pragma_tok, context);
51011 case PRAGMA_OACC_DECLARE:
51012 cp_parser_oacc_declare (parser, pragma_tok);
51013 return false;
51015 case PRAGMA_OACC_ENTER_DATA:
51016 if (context == pragma_stmt)
51018 error_at (pragma_tok->location,
51019 "%<#pragma %s%> may only be used in compound statements",
51020 "acc enter data");
51021 ret = true;
51022 break;
51024 else if (context != pragma_compound)
51025 goto bad_stmt;
51026 cp_parser_omp_construct (parser, pragma_tok, if_p);
51027 return true;
51029 case PRAGMA_OACC_EXIT_DATA:
51030 if (context == pragma_stmt)
51032 error_at (pragma_tok->location,
51033 "%<#pragma %s%> may only be used in compound statements",
51034 "acc exit data");
51035 ret = true;
51036 break;
51038 else if (context != pragma_compound)
51039 goto bad_stmt;
51040 cp_parser_omp_construct (parser, pragma_tok, if_p);
51041 return true;
51043 case PRAGMA_OACC_ROUTINE:
51044 if (context != pragma_external)
51046 error_at (pragma_tok->location,
51047 "%<#pragma acc routine%> must be at file scope");
51048 ret = true;
51049 break;
51051 cp_parser_oacc_routine (parser, pragma_tok, context);
51052 return false;
51054 case PRAGMA_OACC_UPDATE:
51055 if (context == pragma_stmt)
51057 error_at (pragma_tok->location,
51058 "%<#pragma %s%> may only be used in compound statements",
51059 "acc update");
51060 ret = true;
51061 break;
51063 else if (context != pragma_compound)
51064 goto bad_stmt;
51065 cp_parser_omp_construct (parser, pragma_tok, if_p);
51066 return true;
51068 case PRAGMA_OACC_WAIT:
51069 if (context == pragma_stmt)
51071 error_at (pragma_tok->location,
51072 "%<#pragma %s%> may only be used in compound statements",
51073 "acc wait");
51074 ret = true;
51075 break;
51077 else if (context != pragma_compound)
51078 goto bad_stmt;
51079 cp_parser_omp_construct (parser, pragma_tok, if_p);
51080 return true;
51081 case PRAGMA_OMP_ALLOCATE:
51082 cp_parser_omp_allocate (parser, pragma_tok);
51083 return false;
51084 case PRAGMA_OACC_ATOMIC:
51085 case PRAGMA_OACC_CACHE:
51086 case PRAGMA_OACC_DATA:
51087 case PRAGMA_OACC_HOST_DATA:
51088 case PRAGMA_OACC_KERNELS:
51089 case PRAGMA_OACC_LOOP:
51090 case PRAGMA_OACC_PARALLEL:
51091 case PRAGMA_OACC_SERIAL:
51092 case PRAGMA_OMP_ASSUME:
51093 case PRAGMA_OMP_ATOMIC:
51094 case PRAGMA_OMP_CRITICAL:
51095 case PRAGMA_OMP_DISTRIBUTE:
51096 case PRAGMA_OMP_FOR:
51097 case PRAGMA_OMP_LOOP:
51098 case PRAGMA_OMP_MASKED:
51099 case PRAGMA_OMP_MASTER:
51100 case PRAGMA_OMP_PARALLEL:
51101 case PRAGMA_OMP_SCOPE:
51102 case PRAGMA_OMP_SECTIONS:
51103 case PRAGMA_OMP_SIMD:
51104 case PRAGMA_OMP_SINGLE:
51105 case PRAGMA_OMP_TASK:
51106 case PRAGMA_OMP_TASKGROUP:
51107 case PRAGMA_OMP_TASKLOOP:
51108 case PRAGMA_OMP_TEAMS:
51109 if (context != pragma_stmt && context != pragma_compound)
51110 goto bad_stmt;
51111 stmt = push_omp_privatization_clauses (false);
51112 cp_parser_omp_construct (parser, pragma_tok, if_p);
51113 pop_omp_privatization_clauses (stmt);
51114 return true;
51116 case PRAGMA_OMP_REQUIRES:
51117 if (context != pragma_external)
51119 error_at (pragma_tok->location,
51120 "%<#pragma omp requires%> may only be used at file or "
51121 "namespace scope");
51122 ret = true;
51123 break;
51125 return cp_parser_omp_requires (parser, pragma_tok);
51127 case PRAGMA_OMP_ASSUMES:
51128 if (context != pragma_external)
51130 error_at (pragma_tok->location,
51131 "%<#pragma omp assumes%> may only be used at file or "
51132 "namespace scope");
51133 ret = true;
51134 break;
51136 return cp_parser_omp_assumes (parser, pragma_tok);
51138 case PRAGMA_OMP_NOTHING:
51139 cp_parser_omp_nothing (parser, pragma_tok);
51140 return false;
51142 case PRAGMA_OMP_ERROR:
51143 return cp_parser_omp_error (parser, pragma_tok, context);
51145 case PRAGMA_OMP_ORDERED:
51146 if (context != pragma_stmt && context != pragma_compound)
51147 goto bad_stmt;
51148 stmt = push_omp_privatization_clauses (false);
51149 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
51150 pop_omp_privatization_clauses (stmt);
51151 return ret;
51153 case PRAGMA_OMP_TARGET:
51154 if (context != pragma_stmt && context != pragma_compound)
51155 goto bad_stmt;
51156 stmt = push_omp_privatization_clauses (false);
51157 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
51158 pop_omp_privatization_clauses (stmt);
51159 return ret;
51161 case PRAGMA_OMP_BEGIN:
51162 cp_parser_omp_begin (parser, pragma_tok);
51163 return false;
51165 case PRAGMA_OMP_END:
51166 cp_parser_omp_end (parser, pragma_tok);
51167 return false;
51169 case PRAGMA_OMP_SCAN:
51170 error_at (pragma_tok->location,
51171 "%<#pragma omp scan%> may only be used in "
51172 "a loop construct with %<inscan%> %<reduction%> clause");
51173 break;
51175 case PRAGMA_OMP_SECTION:
51176 error_at (pragma_tok->location,
51177 "%<#pragma omp section%> may only be used in "
51178 "%<#pragma omp sections%> construct");
51179 break;
51181 case PRAGMA_IVDEP:
51182 case PRAGMA_UNROLL:
51183 case PRAGMA_NOVECTOR:
51185 bool ivdep = false;
51186 tree unroll = NULL_TREE;
51187 bool novector = false;
51188 const char *pragma_str;
51190 switch (id)
51192 case PRAGMA_IVDEP:
51193 pragma_str = "ivdep";
51194 break;
51195 case PRAGMA_UNROLL:
51196 pragma_str = "unroll";
51197 break;
51198 case PRAGMA_NOVECTOR:
51199 pragma_str = "novector";
51200 break;
51201 default:
51202 gcc_unreachable ();
51205 if (context == pragma_external)
51207 error_at (pragma_tok->location,
51208 "%<#pragma GCC %s%> must be inside a function",
51209 pragma_str);
51210 break;
51213 cp_token *tok = pragma_tok;
51214 bool has_more = true;
51217 switch (cp_parser_pragma_kind (tok))
51219 case PRAGMA_IVDEP:
51221 if (tok != pragma_tok)
51222 tok = cp_lexer_consume_token (parser->lexer);
51223 ivdep = cp_parser_pragma_ivdep (parser, tok);
51224 break;
51226 case PRAGMA_UNROLL:
51228 if (tok != pragma_tok)
51229 tok = cp_lexer_consume_token (parser->lexer);
51230 unroll = cp_parser_pragma_unroll (parser, tok);
51231 break;
51233 case PRAGMA_NOVECTOR:
51235 if (tok != pragma_tok)
51236 tok = cp_lexer_consume_token (parser->lexer);
51237 novector = cp_parser_pragma_novector (parser, tok);
51238 break;
51240 default:
51241 has_more = false;
51242 break;
51244 tok = cp_lexer_peek_token (the_parser->lexer);
51245 has_more = has_more && tok->type == CPP_PRAGMA;
51247 while (has_more);
51249 if (tok->type != CPP_KEYWORD
51250 || (tok->keyword != RID_FOR
51251 && tok->keyword != RID_WHILE
51252 && tok->keyword != RID_DO))
51254 cp_parser_error (parser, "for, while or do statement expected");
51255 return false;
51257 cp_parser_iteration_statement (parser, if_p, ivdep, unroll, novector);
51258 return true;
51261 default:
51262 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
51263 c_invoke_pragma_handler (id);
51264 break;
51266 bad_stmt:
51267 cp_parser_error (parser, "expected declaration specifiers");
51268 break;
51271 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
51272 return ret;
51275 /* Helper for pragma_lex in preprocess-only mode; in this mode, we have not
51276 populated the lexer with any tokens (the tokens rather being read by
51277 c-ppoutput.c's machinery), so we need to read enough tokens now to handle
51278 a pragma. */
51279 static void
51280 maybe_read_tokens_for_pragma_lex ()
51282 const auto lexer = the_parser->lexer;
51283 if (!lexer->buffer->is_empty ())
51284 return;
51286 /* Read the rest of the tokens comprising the pragma line. */
51287 cp_token *tok;
51290 tok = vec_safe_push (lexer->buffer, cp_token ());
51291 cp_lexer_get_preprocessor_token (C_LEX_STRING_NO_JOIN, tok);
51292 gcc_assert (tok->type != CPP_EOF);
51293 } while (tok->type != CPP_PRAGMA_EOL);
51294 lexer->next_token = lexer->buffer->address ();
51295 lexer->last_token = lexer->next_token + lexer->buffer->length () - 1;
51298 /* The interface the pragma parsers have to the lexer. */
51300 enum cpp_ttype
51301 pragma_lex (tree *value, location_t *loc)
51303 if (flag_preprocess_only)
51304 maybe_read_tokens_for_pragma_lex ();
51306 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
51307 enum cpp_ttype ret = tok->type;
51309 *value = tok->u.value;
51310 if (loc)
51311 *loc = tok->location;
51313 if (ret == CPP_PRAGMA_EOL)
51314 ret = CPP_EOF;
51315 else if (ret == CPP_STRING)
51316 *value = cp_parser_string_literal (the_parser, /*translate=*/false,
51317 /*wide_ok=*/false);
51318 else
51320 if (ret == CPP_KEYWORD)
51321 ret = CPP_NAME;
51322 cp_lexer_consume_token (the_parser->lexer);
51325 return ret;
51328 void
51329 pragma_lex_discard_to_eol ()
51331 /* We have already read all the tokens, so we just need to discard
51332 them here. */
51333 const auto lexer = the_parser->lexer;
51334 lexer->next_token = lexer->last_token;
51335 lexer->buffer->truncate (0);
51339 /* External interface. */
51341 /* Parse one entire translation unit. */
51343 void
51344 c_parse_file (void)
51346 static bool already_called = false;
51348 if (already_called)
51349 fatal_error (input_location,
51350 "multi-source compilation not implemented for C++");
51351 already_called = true;
51353 /* cp_lexer_new_main is called before doing any GC allocation
51354 because tokenization might load a PCH file. */
51355 cp_lexer_new_main ();
51357 cp_parser_translation_unit (the_parser);
51358 class_decl_loc_t::diag_mismatched_tags ();
51360 the_parser = NULL;
51362 finish_translation_unit ();
51365 /* Create an identifier for a generic parameter type (a synthesized
51366 template parameter implied by `auto' or a concept identifier). */
51368 static GTY(()) int generic_parm_count;
51369 static tree
51370 make_generic_type_name ()
51372 char buf[32];
51373 sprintf (buf, "auto:%d", ++generic_parm_count);
51374 return get_identifier (buf);
51377 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
51378 (creating a new template parameter list if necessary). Returns the newly
51379 created template type parm. */
51381 static tree
51382 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
51384 /* A requires-clause is not a function and cannot have placeholders. */
51385 if (current_binding_level->requires_expression)
51387 error ("placeholder type not allowed in this context");
51388 return error_mark_node;
51391 gcc_assert (current_binding_level->kind == sk_function_parms);
51393 /* We are either continuing a function template that already contains implicit
51394 template parameters, creating a new fully-implicit function template, or
51395 extending an existing explicit function template with implicit template
51396 parameters. */
51398 cp_binding_level *const entry_scope = current_binding_level;
51400 bool become_template = false;
51401 cp_binding_level *parent_scope = 0;
51403 if (parser->implicit_template_scope)
51405 gcc_assert (parser->implicit_template_parms);
51407 current_binding_level = parser->implicit_template_scope;
51409 else
51411 /* Roll back to the existing template parameter scope (in the case of
51412 extending an explicit function template) or introduce a new template
51413 parameter scope ahead of the function parameter scope (or class scope
51414 in the case of out-of-line member definitions). The function scope is
51415 added back after template parameter synthesis below. */
51417 cp_binding_level *scope = entry_scope;
51419 while (scope->kind == sk_function_parms)
51421 parent_scope = scope;
51422 scope = scope->level_chain;
51424 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
51426 /* If not defining a class, then any class scope is a scope level in
51427 an out-of-line member definition. In this case simply wind back
51428 beyond the first such scope to inject the template parameter list.
51429 Otherwise wind back to the class being defined. The latter can
51430 occur in class member friend declarations such as:
51432 class A {
51433 void foo (auto);
51435 class B {
51436 friend void A::foo (auto);
51439 The template parameter list synthesized for the friend declaration
51440 must be injected in the scope of 'B'. This can also occur in
51441 erroneous cases such as:
51443 struct A {
51444 struct B {
51445 void foo (auto);
51447 void B::foo (auto) {}
51450 Here the attempted definition of 'B::foo' within 'A' is ill-formed
51451 but, nevertheless, the template parameter list synthesized for the
51452 declarator should be injected into the scope of 'A' as if the
51453 ill-formed template was specified explicitly. */
51455 while (scope->kind == sk_class && !scope->defining_class_p)
51457 parent_scope = scope;
51458 scope = scope->level_chain;
51462 current_binding_level = scope;
51464 if (scope->kind != sk_template_parms
51465 || !function_being_declared_is_template_p (parser))
51467 /* Introduce a new template parameter list for implicit template
51468 parameters. */
51470 become_template = true;
51472 parser->implicit_template_scope
51473 = begin_scope (sk_template_parms, NULL);
51475 ++processing_template_decl;
51477 parser->fully_implicit_function_template_p = true;
51478 ++parser->num_template_parameter_lists;
51480 else
51482 /* Synthesize implicit template parameters at the end of the explicit
51483 template parameter list. */
51485 gcc_assert (current_template_parms);
51487 parser->implicit_template_scope = scope;
51489 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
51490 parser->implicit_template_parms
51491 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
51495 /* Synthesize a new template parameter and track the current template
51496 parameter chain with implicit_template_parms. */
51498 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
51499 tree synth_id = make_generic_type_name ();
51500 bool non_type = false;
51502 /* Synthesize the type template parameter. */
51503 gcc_assert(!proto || TREE_CODE (proto) == TYPE_DECL);
51504 tree synth_tmpl_parm = finish_template_type_parm (class_type_node, synth_id);
51506 if (become_template)
51507 current_template_parms = tree_cons (size_int (current_template_depth + 1),
51508 NULL_TREE, current_template_parms);
51510 /* Attach the constraint to the parm before processing. */
51511 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
51512 TREE_TYPE (node) = constr;
51513 tree new_parm
51514 = process_template_parm (parser->implicit_template_parms,
51515 input_location,
51516 node,
51517 /*non_type=*/non_type,
51518 /*param_pack=*/false);
51519 // Process_template_parm returns the list of parms, and
51520 // parser->implicit_template_parms holds the final node of the parm
51521 // list. We really want to manipulate the newly appended element.
51522 gcc_checking_assert (!parser->implicit_template_parms
51523 || parser->implicit_template_parms == new_parm);
51524 if (parser->implicit_template_parms)
51525 new_parm = TREE_CHAIN (new_parm);
51526 gcc_checking_assert (!TREE_CHAIN (new_parm));
51528 // Record the last implicit parm node
51529 parser->implicit_template_parms = new_parm;
51531 /* Mark the synthetic declaration "virtual". This is used when
51532 comparing template-heads to determine if whether an abbreviated
51533 function template is equivalent to an explicit template.
51535 Note that DECL_ARTIFICIAL is used elsewhere for template
51536 parameters. */
51537 if (TREE_VALUE (new_parm) != error_mark_node)
51538 DECL_IMPLICIT_TEMPLATE_PARM_P (TREE_VALUE (new_parm)) = true;
51540 tree new_decl = get_local_decls ();
51541 if (non_type)
51542 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
51543 new_decl = DECL_INITIAL (new_decl);
51545 /* If creating a fully implicit function template, start the new implicit
51546 template parameter list with this synthesized type, otherwise grow the
51547 current template parameter list. */
51549 if (become_template)
51551 parent_scope->level_chain = current_binding_level;
51553 tree new_parms = make_tree_vec (1);
51554 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
51555 TREE_VALUE (current_template_parms) = new_parms;
51557 else
51559 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
51560 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
51561 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
51562 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
51565 /* If the new parameter was constrained, we need to add that to the
51566 constraints in the template parameter list. */
51567 if (tree req = TEMPLATE_PARM_CONSTRAINTS (new_parm))
51569 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
51570 reqs = combine_constraint_expressions (reqs, req);
51571 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
51574 current_binding_level = entry_scope;
51576 return new_decl;
51579 /* Finish the declaration of a fully implicit function template. Such a
51580 template has no explicit template parameter list so has not been through the
51581 normal template head and tail processing. synthesize_implicit_template_parm
51582 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
51583 provided if the declaration is a class member such that its template
51584 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
51585 form is returned. Otherwise NULL_TREE is returned. */
51587 static tree
51588 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
51590 gcc_assert (parser->fully_implicit_function_template_p);
51592 if (member_decl_opt && member_decl_opt != error_mark_node
51593 && DECL_VIRTUAL_P (member_decl_opt))
51595 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
51596 "implicit templates may not be %<virtual%>");
51597 DECL_VIRTUAL_P (member_decl_opt) = false;
51600 if (member_decl_opt)
51601 member_decl_opt = finish_member_template_decl (member_decl_opt);
51602 end_template_decl ();
51604 parser->fully_implicit_function_template_p = false;
51605 parser->implicit_template_parms = 0;
51606 parser->implicit_template_scope = 0;
51607 --parser->num_template_parameter_lists;
51609 return member_decl_opt;
51612 /* Like finish_fully_implicit_template, but to be used in error
51613 recovery, rearranging scopes so that we restore the state we had
51614 before synthesize_implicit_template_parm inserted the implement
51615 template parms scope. */
51617 static void
51618 abort_fully_implicit_template (cp_parser *parser)
51620 cp_binding_level *return_to_scope = current_binding_level;
51622 if (parser->implicit_template_scope
51623 && return_to_scope != parser->implicit_template_scope)
51625 cp_binding_level *child = return_to_scope;
51626 for (cp_binding_level *scope = child->level_chain;
51627 scope != parser->implicit_template_scope;
51628 scope = child->level_chain)
51629 child = scope;
51630 child->level_chain = parser->implicit_template_scope->level_chain;
51631 parser->implicit_template_scope->level_chain = return_to_scope;
51632 current_binding_level = parser->implicit_template_scope;
51634 else
51635 return_to_scope = return_to_scope->level_chain;
51637 finish_fully_implicit_template (parser, NULL);
51639 gcc_assert (current_binding_level == return_to_scope);
51642 /* Helper function for diagnostics that have complained about things
51643 being used with 'extern "C"' linkage.
51645 Attempt to issue a note showing where the 'extern "C"' linkage began. */
51647 void
51648 maybe_show_extern_c_location (void)
51650 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
51651 inform (the_parser->innermost_linkage_specification_location,
51652 "%<extern \"C\"%> linkage started here");
51655 #include "gt-cp-parser.h"