c++: look for empty base at specific offset [PR109678]
[official-gcc.git] / gcc / cp / parser.cc
blobd89553e7da81d64a6390c385e009b4ff07b5da58
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2023 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #define INCLUDE_MEMORY
23 #include "system.h"
24 #include "coretypes.h"
25 #include "cp-tree.h"
26 #include "c-family/c-common.h"
27 #include "timevar.h"
28 #include "stringpool.h"
29 #include "cgraph.h"
30 #include "print-tree.h"
31 #include "attribs.h"
32 #include "trans-mem.h"
33 #include "intl.h"
34 #include "decl.h"
35 #include "c-family/c-objc.h"
36 #include "plugin.h"
37 #include "tree-pretty-print.h"
38 #include "parser.h"
39 #include "gomp-constants.h"
40 #include "omp-general.h"
41 #include "omp-offload.h"
42 #include "c-family/c-indentation.h"
43 #include "context.h"
44 #include "gcc-rich-location.h"
45 #include "tree-iterator.h"
46 #include "cp-name-hint.h"
47 #include "memmodel.h"
48 #include "c-family/known-headers.h"
49 #include "contracts.h"
50 #include "bitmap.h"
53 /* The lexer. */
55 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
56 and c-lex.cc) and the C++ parser. */
58 /* The various kinds of non integral constant we encounter. */
59 enum non_integral_constant {
60 NIC_NONE,
61 /* floating-point literal */
62 NIC_FLOAT,
63 /* %<this%> */
64 NIC_THIS,
65 /* %<__FUNCTION__%> */
66 NIC_FUNC_NAME,
67 /* %<__PRETTY_FUNCTION__%> */
68 NIC_PRETTY_FUNC,
69 /* %<__func__%> */
70 NIC_C99_FUNC,
71 /* "%<va_arg%> */
72 NIC_VA_ARG,
73 /* a cast */
74 NIC_CAST,
75 /* %<typeid%> operator */
76 NIC_TYPEID,
77 /* non-constant compound literals */
78 NIC_NCC,
79 /* a function call */
80 NIC_FUNC_CALL,
81 /* an increment */
82 NIC_INC,
83 /* an decrement */
84 NIC_DEC,
85 /* an array reference */
86 NIC_ARRAY_REF,
87 /* %<->%> */
88 NIC_ARROW,
89 /* %<.%> */
90 NIC_POINT,
91 /* the address of a label */
92 NIC_ADDR_LABEL,
93 /* %<*%> */
94 NIC_STAR,
95 /* %<&%> */
96 NIC_ADDR,
97 /* %<++%> */
98 NIC_PREINCREMENT,
99 /* %<--%> */
100 NIC_PREDECREMENT,
101 /* %<new%> */
102 NIC_NEW,
103 /* %<delete%> */
104 NIC_DEL,
105 /* calls to overloaded operators */
106 NIC_OVERLOADED,
107 /* an assignment */
108 NIC_ASSIGNMENT,
109 /* a comma operator */
110 NIC_COMMA,
111 /* a call to a constructor */
112 NIC_CONSTRUCTOR,
113 /* a transaction expression */
114 NIC_TRANSACTION
117 /* The various kinds of errors about name-lookup failing. */
118 enum name_lookup_error {
119 /* NULL */
120 NLE_NULL,
121 /* is not a type */
122 NLE_TYPE,
123 /* is not a class or namespace */
124 NLE_CXX98,
125 /* is not a class, namespace, or enumeration */
126 NLE_NOT_CXX98
129 /* The various kinds of required token */
130 enum required_token {
131 RT_NONE,
132 RT_SEMICOLON, /* ';' */
133 RT_OPEN_PAREN, /* '(' */
134 RT_CLOSE_BRACE, /* '}' */
135 RT_OPEN_BRACE, /* '{' */
136 RT_CLOSE_SQUARE, /* ']' */
137 RT_OPEN_SQUARE, /* '[' */
138 RT_COMMA, /* ',' */
139 RT_SCOPE, /* '::' */
140 RT_LESS, /* '<' */
141 RT_GREATER, /* '>' */
142 RT_EQ, /* '=' */
143 RT_ELLIPSIS, /* '...' */
144 RT_MULT, /* '*' */
145 RT_COMPL, /* '~' */
146 RT_COLON, /* ':' */
147 RT_COLON_SCOPE, /* ':' or '::' */
148 RT_CLOSE_PAREN, /* ')' */
149 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
150 RT_PRAGMA_EOL, /* end of line */
151 RT_NAME, /* identifier */
153 /* The type is CPP_KEYWORD */
154 RT_NEW, /* new */
155 RT_DELETE, /* delete */
156 RT_RETURN, /* return */
157 RT_WHILE, /* while */
158 RT_EXTERN, /* extern */
159 RT_STATIC_ASSERT, /* static_assert */
160 RT_DECLTYPE, /* decltype */
161 RT_OPERATOR, /* operator */
162 RT_CLASS, /* class */
163 RT_TEMPLATE, /* template */
164 RT_NAMESPACE, /* namespace */
165 RT_USING, /* using */
166 RT_ASM, /* asm */
167 RT_TRY, /* try */
168 RT_CATCH, /* catch */
169 RT_THROW, /* throw */
170 RT_AUTO, /* auto */
171 RT_LABEL, /* __label__ */
172 RT_AT_TRY, /* @try */
173 RT_AT_SYNCHRONIZED, /* @synchronized */
174 RT_AT_THROW, /* @throw */
176 RT_SELECT, /* selection-statement */
177 RT_ITERATION, /* iteration-statement */
178 RT_JUMP, /* jump-statement */
179 RT_CLASS_KEY, /* class-key */
180 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
181 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
182 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
183 RT_TRANSACTION_CANCEL, /* __transaction_cancel */
185 RT_CO_YIELD /* co_yield */
188 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
189 reverting it on destruction. */
191 class type_id_in_expr_sentinel
193 cp_parser *parser;
194 bool saved;
195 public:
196 type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
197 : parser (parser),
198 saved (parser->in_type_id_in_expr_p)
199 { parser->in_type_id_in_expr_p = set; }
200 ~type_id_in_expr_sentinel ()
201 { parser->in_type_id_in_expr_p = saved; }
204 /* Prototypes. */
206 static cp_lexer *cp_lexer_new_main
207 (void);
208 static cp_lexer *cp_lexer_new_from_tokens
209 (cp_token_cache *tokens);
210 static void cp_lexer_destroy
211 (cp_lexer *);
212 static int cp_lexer_saving_tokens
213 (const cp_lexer *);
214 static cp_token *cp_lexer_token_at
215 (cp_lexer *, cp_token_position);
216 static void cp_lexer_get_preprocessor_token
217 (unsigned, cp_token *);
218 static inline cp_token *cp_lexer_peek_token
219 (cp_lexer *);
220 static cp_token *cp_lexer_peek_nth_token
221 (cp_lexer *, size_t);
222 static inline bool cp_lexer_next_token_is
223 (cp_lexer *, enum cpp_ttype);
224 static bool cp_lexer_next_token_is_not
225 (cp_lexer *, enum cpp_ttype);
226 static bool cp_lexer_next_token_is_keyword
227 (cp_lexer *, enum rid);
228 static cp_token *cp_lexer_consume_token
229 (cp_lexer *);
230 static void cp_lexer_purge_token
231 (cp_lexer *);
232 static void cp_lexer_purge_tokens_after
233 (cp_lexer *, cp_token_position);
234 static void cp_lexer_save_tokens
235 (cp_lexer *);
236 static void cp_lexer_commit_tokens
237 (cp_lexer *);
238 static void cp_lexer_rollback_tokens
239 (cp_lexer *);
240 static void cp_lexer_print_token
241 (FILE *, cp_token *);
242 static inline bool cp_lexer_debugging_p
243 (cp_lexer *);
244 static void cp_lexer_start_debugging
245 (cp_lexer *) ATTRIBUTE_UNUSED;
246 static void cp_lexer_stop_debugging
247 (cp_lexer *) ATTRIBUTE_UNUSED;
249 static cp_token_cache *cp_token_cache_new
250 (cp_token *, cp_token *);
251 static tree cp_parser_late_noexcept_specifier
252 (cp_parser *, tree);
253 static void noexcept_override_late_checks
254 (tree, tree);
256 static void cp_parser_initial_pragma
257 (cp_token *);
259 static bool cp_parser_omp_declare_reduction_exprs
260 (tree, cp_parser *);
261 static void cp_finalize_oacc_routine
262 (cp_parser *, tree, bool);
264 /* Manifest constants. */
265 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
266 #define CP_SAVED_TOKEN_STACK 5
268 /* Variables. */
270 /* The stream to which debugging output should be written. */
271 static FILE *cp_lexer_debug_stream;
273 /* Nonzero if we are parsing an unevaluated operand: an operand to
274 sizeof, typeof, or alignof. */
275 int cp_unevaluated_operand;
277 /* Dump up to NUM tokens in BUFFER to FILE starting with token
278 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
279 first token in BUFFER. If NUM is 0, dump all the tokens. If
280 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
281 highlighted by surrounding it in [[ ]]. */
283 static void
284 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
285 cp_token *start_token, unsigned num,
286 cp_token *curr_token)
288 unsigned i, nprinted;
289 cp_token *token;
290 bool do_print;
292 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
294 if (buffer == NULL)
295 return;
297 if (num == 0)
298 num = buffer->length ();
300 if (start_token == NULL)
301 start_token = buffer->address ();
303 if (start_token > buffer->address ())
305 cp_lexer_print_token (file, &(*buffer)[0]);
306 fprintf (file, " ... ");
309 do_print = false;
310 nprinted = 0;
311 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
313 if (token == start_token)
314 do_print = true;
316 if (!do_print)
317 continue;
319 nprinted++;
320 if (token == curr_token)
321 fprintf (file, "[[");
323 cp_lexer_print_token (file, token);
325 if (token == curr_token)
326 fprintf (file, "]]");
328 switch (token->type)
330 case CPP_SEMICOLON:
331 case CPP_OPEN_BRACE:
332 case CPP_CLOSE_BRACE:
333 case CPP_EOF:
334 fputc ('\n', file);
335 break;
337 default:
338 fputc (' ', file);
342 if (i == num && i < buffer->length ())
344 fprintf (file, " ... ");
345 cp_lexer_print_token (file, &buffer->last ());
348 fprintf (file, "\n");
352 /* Dump all tokens in BUFFER to stderr. */
354 void
355 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
357 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
360 DEBUG_FUNCTION void
361 debug (vec<cp_token, va_gc> &ref)
363 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
366 DEBUG_FUNCTION void
367 debug (vec<cp_token, va_gc> *ptr)
369 if (ptr)
370 debug (*ptr);
371 else
372 fprintf (stderr, "<nil>\n");
376 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
377 description for T. */
379 static void
380 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
382 if (t)
384 fprintf (file, "%s: ", desc);
385 print_node_brief (file, "", t, 0);
390 /* Dump parser context C to FILE. */
392 static void
393 cp_debug_print_context (FILE *file, cp_parser_context *c)
395 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
396 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
397 print_node_brief (file, "", c->object_type, 0);
398 fprintf (file, "}\n");
402 /* Print the stack of parsing contexts to FILE starting with FIRST. */
404 static void
405 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
407 unsigned i;
408 cp_parser_context *c;
410 fprintf (file, "Parsing context stack:\n");
411 for (i = 0, c = first; c; c = c->next, i++)
413 fprintf (file, "\t#%u: ", i);
414 cp_debug_print_context (file, c);
419 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
421 static void
422 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
424 if (flag)
425 fprintf (file, "%s: true\n", desc);
429 /* Print an unparsed function entry UF to FILE. */
431 static void
432 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
434 unsigned i;
435 cp_default_arg_entry *default_arg_fn;
436 tree fn;
438 fprintf (file, "\tFunctions with default args:\n");
439 for (i = 0;
440 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
441 i++)
443 fprintf (file, "\t\tClass type: ");
444 print_node_brief (file, "", default_arg_fn->class_type, 0);
445 fprintf (file, "\t\tDeclaration: ");
446 print_node_brief (file, "", default_arg_fn->decl, 0);
447 fprintf (file, "\n");
450 fprintf (file, "\n\tFunctions with definitions that require "
451 "post-processing\n\t\t");
452 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
454 print_node_brief (file, "", fn, 0);
455 fprintf (file, " ");
457 fprintf (file, "\n");
459 fprintf (file, "\n\tNon-static data members with initializers that require "
460 "post-processing\n\t\t");
461 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
463 print_node_brief (file, "", fn, 0);
464 fprintf (file, " ");
466 fprintf (file, "\n");
470 /* Print the stack of unparsed member functions S to FILE. */
472 static void
473 cp_debug_print_unparsed_queues (FILE *file,
474 vec<cp_unparsed_functions_entry, va_gc> *s)
476 unsigned i;
477 cp_unparsed_functions_entry *uf;
479 fprintf (file, "Unparsed functions\n");
480 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
482 fprintf (file, "#%u:\n", i);
483 cp_debug_print_unparsed_function (file, uf);
488 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
489 the given PARSER. If FILE is NULL, the output is printed on stderr. */
491 static void
492 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
494 cp_token *next_token, *first_token, *start_token;
496 if (file == NULL)
497 file = stderr;
499 next_token = parser->lexer->next_token;
500 first_token = parser->lexer->buffer->address ();
501 start_token = (next_token > first_token + window_size / 2)
502 ? next_token - window_size / 2
503 : first_token;
504 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
505 next_token);
509 /* Dump debugging information for the given PARSER. If FILE is NULL,
510 the output is printed on stderr. */
512 void
513 cp_debug_parser (FILE *file, cp_parser *parser)
515 const size_t window_size = 20;
516 cp_token *token;
517 expanded_location eloc;
519 if (file == NULL)
520 file = stderr;
522 fprintf (file, "Parser state\n\n");
523 fprintf (file, "Number of tokens: %u\n",
524 vec_safe_length (parser->lexer->buffer));
525 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
526 cp_debug_print_tree_if_set (file, "Object scope",
527 parser->object_scope);
528 cp_debug_print_tree_if_set (file, "Qualifying scope",
529 parser->qualifying_scope);
530 cp_debug_print_context_stack (file, parser->context);
531 cp_debug_print_flag (file, "Allow GNU extensions",
532 parser->allow_gnu_extensions_p);
533 cp_debug_print_flag (file, "'>' token is greater-than",
534 parser->greater_than_is_operator_p);
535 cp_debug_print_flag (file, "Default args allowed in current "
536 "parameter list", parser->default_arg_ok_p);
537 cp_debug_print_flag (file, "Parsing integral constant-expression",
538 parser->integral_constant_expression_p);
539 cp_debug_print_flag (file, "Allow non-constant expression in current "
540 "constant-expression",
541 parser->allow_non_integral_constant_expression_p);
542 cp_debug_print_flag (file, "Seen non-constant expression",
543 parser->non_integral_constant_expression_p);
544 cp_debug_print_flag (file, "Local names forbidden in current context",
545 (parser->local_variables_forbidden_p
546 & LOCAL_VARS_FORBIDDEN));
547 cp_debug_print_flag (file, "'this' forbidden in current context",
548 (parser->local_variables_forbidden_p
549 & THIS_FORBIDDEN));
550 cp_debug_print_flag (file, "In unbraced linkage specification",
551 parser->in_unbraced_linkage_specification_p);
552 cp_debug_print_flag (file, "Parsing a declarator",
553 parser->in_declarator_p);
554 cp_debug_print_flag (file, "In template argument list",
555 parser->in_template_argument_list_p);
556 cp_debug_print_flag (file, "Parsing an iteration statement",
557 parser->in_statement & IN_ITERATION_STMT);
558 cp_debug_print_flag (file, "Parsing a switch statement",
559 parser->in_statement & IN_SWITCH_STMT);
560 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
561 parser->in_statement & IN_OMP_BLOCK);
562 cp_debug_print_flag (file, "Parsing an OpenMP loop",
563 parser->in_statement & IN_OMP_FOR);
564 cp_debug_print_flag (file, "Parsing an if statement",
565 parser->in_statement & IN_IF_STMT);
566 cp_debug_print_flag (file, "Parsing a type-id in an expression "
567 "context", parser->in_type_id_in_expr_p);
568 cp_debug_print_flag (file, "String expressions should be translated "
569 "to execution character set",
570 parser->translate_strings_p);
571 cp_debug_print_flag (file, "Parsing function body outside of a "
572 "local class", parser->in_function_body);
573 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
574 parser->colon_corrects_to_scope_p);
575 cp_debug_print_flag (file, "Colon doesn't start a class definition",
576 parser->colon_doesnt_start_class_def_p);
577 cp_debug_print_flag (file, "Parsing an Objective-C++ message context",
578 parser->objective_c_message_context_p);
579 if (parser->type_definition_forbidden_message)
580 fprintf (file, "Error message for forbidden type definitions: %s %s\n",
581 parser->type_definition_forbidden_message,
582 parser->type_definition_forbidden_message_arg
583 ? parser->type_definition_forbidden_message_arg : "<none>");
584 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
585 fprintf (file, "Number of class definitions in progress: %u\n",
586 parser->num_classes_being_defined);
587 fprintf (file, "Number of template parameter lists for the current "
588 "declaration: %u\n", parser->num_template_parameter_lists);
589 cp_debug_parser_tokens (file, parser, window_size);
590 token = parser->lexer->next_token;
591 fprintf (file, "Next token to parse:\n");
592 fprintf (file, "\tToken: ");
593 cp_lexer_print_token (file, token);
594 eloc = expand_location (token->location);
595 fprintf (file, "\n\tFile: %s\n", eloc.file);
596 fprintf (file, "\tLine: %d\n", eloc.line);
597 fprintf (file, "\tColumn: %d\n", eloc.column);
600 DEBUG_FUNCTION void
601 debug (cp_parser &ref)
603 cp_debug_parser (stderr, &ref);
606 DEBUG_FUNCTION void
607 debug (cp_parser *ptr)
609 if (ptr)
610 debug (*ptr);
611 else
612 fprintf (stderr, "<nil>\n");
615 /* Allocate memory for a new lexer object and return it. */
617 static cp_lexer *
618 cp_lexer_alloc (void)
620 /* Allocate the memory. */
621 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
623 /* Initially we are not debugging. */
624 lexer->debugging_p = false;
626 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
628 /* Create the buffer. */
629 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
631 return lexer;
634 /* Return TRUE if token is the start of a module declaration that will be
635 terminated by a CPP_PRAGMA_EOL token. */
636 static inline bool
637 cp_token_is_module_directive (cp_token *token)
639 return token->keyword == RID__EXPORT
640 || token->keyword == RID__MODULE
641 || token->keyword == RID__IMPORT;
644 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
645 PRAGMA_NONE. */
647 static enum pragma_kind
648 cp_parser_pragma_kind (cp_token *token)
650 if (token->type != CPP_PRAGMA)
651 return PRAGMA_NONE;
652 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
653 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
656 /* Handle early pragmas such as #pragma GCC diagnostic, which needs to be done
657 during preprocessing for the case of preprocessing-related diagnostics. This
658 is called immediately after pushing the CPP_PRAGMA_EOL token onto
659 lexer->buffer. */
661 static void
662 cp_lexer_handle_early_pragma (cp_lexer *lexer)
664 const auto first_token = lexer->buffer->address ();
665 const auto last_token = first_token + lexer->buffer->length () - 1;
667 /* Back up to the start of the pragma so pragma_lex () can parse it when
668 c-pragma lib asks it to. */
669 auto begin = last_token;
670 gcc_assert (begin->type == CPP_PRAGMA_EOL);
671 while (begin->type != CPP_PRAGMA)
673 if (cp_token_is_module_directive (begin))
674 return;
675 gcc_assert (begin != first_token);
676 --begin;
678 gcc_assert (!lexer->next_token);
679 gcc_assert (!lexer->last_token);
680 lexer->next_token = begin;
681 lexer->last_token = last_token;
683 /* Dispatch it. */
684 const unsigned int id
685 = cp_parser_pragma_kind (cp_lexer_consume_token (lexer));
686 if (id >= PRAGMA_FIRST_EXTERNAL)
687 c_invoke_early_pragma_handler (id);
689 /* Reset to normal state. */
690 lexer->next_token = lexer->last_token = nullptr;
693 /* The parser. */
694 static cp_parser *cp_parser_new (cp_lexer *);
695 static GTY (()) cp_parser *the_parser;
697 /* Create a new main C++ lexer, the lexer that gets tokens from the
698 preprocessor, and also create the main parser. */
700 static cp_lexer *
701 cp_lexer_new_main (void)
703 cp_token token;
705 /* It's possible that parsing the first pragma will load a PCH file,
706 which is a GC collection point. So we have to do that before
707 allocating any memory. */
708 cp_lexer_get_preprocessor_token (C_LEX_STRING_NO_JOIN, &token);
709 cp_parser_initial_pragma (&token);
710 c_common_no_more_pch ();
712 cp_lexer *lexer = cp_lexer_alloc ();
713 /* Put the first token in the buffer. */
714 cp_token *tok = lexer->buffer->quick_push (token);
716 uintptr_t filter = 0;
717 if (modules_p ())
718 filter = module_token_cdtor (parse_in, filter);
720 /* Create the parser now, so we can use it to handle early pragmas. */
721 gcc_assert (!the_parser);
722 the_parser = cp_parser_new (lexer);
724 /* Get the remaining tokens from the preprocessor. */
725 while (tok->type != CPP_EOF)
727 if (filter)
728 /* Process the previous token. */
729 module_token_lang (tok->type, tok->keyword, tok->u.value,
730 tok->location, filter);
732 /* Check for early pragmas that need to be handled now. */
733 if (tok->type == CPP_PRAGMA_EOL)
734 cp_lexer_handle_early_pragma (lexer);
736 tok = vec_safe_push (lexer->buffer, cp_token ());
737 cp_lexer_get_preprocessor_token (C_LEX_STRING_NO_JOIN, tok);
740 lexer->next_token = lexer->buffer->address ();
741 lexer->last_token = lexer->next_token
742 + lexer->buffer->length ()
743 - 1;
745 if (lexer->buffer->length () != 1)
747 /* Set the EOF token's location to be the just after the previous
748 token's range. That way 'at-eof' diagnostics point at something
749 meaninful. */
750 auto range = get_range_from_loc (line_table, tok[-1].location);
751 tok[0].location
752 = linemap_position_for_loc_and_offset (line_table, range.m_finish, 1);
755 if (filter)
756 module_token_cdtor (parse_in, filter);
758 /* Subsequent preprocessor diagnostics should use compiler
759 diagnostic functions to get the compiler source location. */
760 override_libcpp_locations = true;
762 maybe_check_all_macros (parse_in);
764 gcc_assert (!lexer->next_token->purged_p);
765 return lexer;
768 /* Create a new lexer whose token stream is primed with the tokens in
769 CACHE. When these tokens are exhausted, no new tokens will be read. */
771 static cp_lexer *
772 cp_lexer_new_from_tokens (cp_token_cache *cache)
774 cp_token *first = cache->first;
775 cp_token *last = cache->last;
776 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
778 /* We do not own the buffer. */
779 lexer->buffer = NULL;
781 /* Insert an EOF token. */
782 lexer->saved_type = last->type;
783 lexer->saved_keyword = last->keyword;
784 last->type = CPP_EOF;
785 last->keyword = RID_MAX;
787 lexer->next_token = first;
788 lexer->last_token = last;
790 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
792 /* Initially we are not debugging. */
793 lexer->debugging_p = false;
795 gcc_assert (!lexer->next_token->purged_p
796 && !lexer->last_token->purged_p);
797 return lexer;
800 /* Frees all resources associated with LEXER. */
802 static void
803 cp_lexer_destroy (cp_lexer *lexer)
805 if (lexer->buffer)
806 vec_free (lexer->buffer);
807 else
809 /* Restore the token we overwrite with EOF. */
810 lexer->last_token->type = lexer->saved_type;
811 lexer->last_token->keyword = lexer->saved_keyword;
813 lexer->saved_tokens.release ();
814 ggc_free (lexer);
817 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
818 be used. The point of this flag is to help the compiler to fold away calls
819 to cp_lexer_debugging_p within this source file at compile time, when the
820 lexer is not being debugged. */
822 #define LEXER_DEBUGGING_ENABLED_P false
824 /* Returns nonzero if debugging information should be output. */
826 static inline bool
827 cp_lexer_debugging_p (cp_lexer *lexer)
829 if (!LEXER_DEBUGGING_ENABLED_P)
830 return false;
832 return lexer->debugging_p;
836 static inline cp_token_position
837 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
839 return lexer->next_token - previous_p;
842 static inline cp_token *
843 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
845 return pos;
848 static inline void
849 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
851 lexer->next_token = cp_lexer_token_at (lexer, pos);
854 static inline cp_token_position
855 cp_lexer_previous_token_position (cp_lexer *lexer)
857 return cp_lexer_token_position (lexer, true);
860 static inline cp_token *
861 cp_lexer_previous_token (cp_lexer *lexer)
863 cp_token_position tp = cp_lexer_previous_token_position (lexer);
865 /* Skip past purged tokens. */
866 while (tp->purged_p)
868 gcc_assert (tp != vec_safe_address (lexer->buffer));
869 tp--;
872 return cp_lexer_token_at (lexer, tp);
875 /* Same as above, but return NULL when the lexer doesn't own the token
876 buffer or if the next_token is at the start of the token
877 vector or if all previous tokens are purged. */
879 static cp_token *
880 cp_lexer_safe_previous_token (cp_lexer *lexer)
882 if (lexer->buffer
883 && lexer->next_token != lexer->buffer->address ())
885 cp_token_position tp = cp_lexer_previous_token_position (lexer);
887 /* Skip past purged tokens. */
888 while (tp->purged_p)
890 if (tp == lexer->buffer->address ())
891 return NULL;
892 tp--;
894 return cp_lexer_token_at (lexer, tp);
897 return NULL;
900 /* Overload for make_location, taking the lexer to mean the location of the
901 previous token. */
903 static inline location_t
904 make_location (location_t caret, location_t start, cp_lexer *lexer)
906 cp_token *t = cp_lexer_previous_token (lexer);
907 return make_location (caret, start, t->location);
910 /* Overload for make_location taking tokens instead of locations. */
912 static inline location_t
913 make_location (cp_token *caret, cp_token *start, cp_token *end)
915 return make_location (caret->location, start->location, end->location);
918 /* nonzero if we are presently saving tokens. */
920 static inline int
921 cp_lexer_saving_tokens (const cp_lexer* lexer)
923 return lexer->saved_tokens.length () != 0;
926 /* Store the next token from the preprocessor in *TOKEN. Return true
927 if we reach EOF. If LEXER is NULL, assume we are handling an
928 initial #pragma pch_preprocess, and thus want the lexer to return
929 processed strings.
931 Diagnostics issued from this function must have their controlling option (if
932 any) in c.opt annotated as a libcpp option via the CppReason property. */
934 static void
935 cp_lexer_get_preprocessor_token (unsigned flags, cp_token *token)
937 static int is_extern_c = 0;
939 /* Get a new token from the preprocessor. */
940 token->type
941 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
942 flags);
943 token->keyword = RID_MAX;
944 token->purged_p = false;
945 token->error_reported = false;
946 token->tree_check_p = false;
947 /* Usually never see a zero, but just in case ... */
948 token->main_source_p = line_table->depth <= 1;
950 /* On some systems, some header files are surrounded by an
951 implicit extern "C" block. Set a flag in the token if it
952 comes from such a header. */
953 is_extern_c += pending_lang_change;
954 pending_lang_change = 0;
955 token->implicit_extern_c = is_extern_c > 0;
957 /* Check to see if this token is a keyword. */
958 if (token->type == CPP_NAME)
960 if (IDENTIFIER_KEYWORD_P (token->u.value))
962 /* Mark this token as a keyword. */
963 token->type = CPP_KEYWORD;
964 /* Record which keyword. */
965 token->keyword = C_RID_CODE (token->u.value);
967 else
969 if (warn_cxx11_compat
970 && ((C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
971 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
972 /* These are outside the CXX11 range. */
973 || C_RID_CODE (token->u.value) == RID_ALIGNOF
974 || C_RID_CODE (token->u.value) == RID_ALIGNAS
975 || C_RID_CODE (token->u.value)== RID_THREAD))
977 /* Warn about the C++11 keyword (but still treat it as
978 an identifier). */
979 warning_at (token->location, OPT_Wc__11_compat,
980 "identifier %qE is a keyword in C++11",
981 token->u.value);
983 /* Clear out the C_RID_CODE so we don't warn about this
984 particular identifier-turned-keyword again. */
985 C_SET_RID_CODE (token->u.value, RID_MAX);
987 if (warn_cxx20_compat
988 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX20
989 && C_RID_CODE (token->u.value) <= RID_LAST_CXX20)
991 /* Warn about the C++20 keyword (but still treat it as
992 an identifier). */
993 warning_at (token->location, OPT_Wc__20_compat,
994 "identifier %qE is a keyword in C++20",
995 token->u.value);
997 /* Clear out the C_RID_CODE so we don't warn about this
998 particular identifier-turned-keyword again. */
999 C_SET_RID_CODE (token->u.value, RID_MAX);
1002 token->keyword = RID_MAX;
1005 else if (token->type == CPP_AT_NAME)
1007 /* This only happens in Objective-C++; it must be a keyword. */
1008 token->type = CPP_KEYWORD;
1009 switch (C_RID_CODE (token->u.value))
1011 /* Replace 'class' with '@class', 'private' with '@private',
1012 etc. This prevents confusion with the C++ keyword
1013 'class', and makes the tokens consistent with other
1014 Objective-C 'AT' keywords. For example '@class' is
1015 reported as RID_AT_CLASS which is consistent with
1016 '@synchronized', which is reported as
1017 RID_AT_SYNCHRONIZED.
1019 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
1020 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
1021 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
1022 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
1023 case RID_THROW: token->keyword = RID_AT_THROW; break;
1024 case RID_TRY: token->keyword = RID_AT_TRY; break;
1025 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
1026 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
1027 default: token->keyword = C_RID_CODE (token->u.value);
1032 /* Update the globals input_location and the input file stack from TOKEN. */
1033 static inline void
1034 cp_lexer_set_source_position_from_token (cp_token *token)
1036 input_location = token->location;
1039 /* Update the globals input_location and the input file stack from LEXER. */
1040 static inline void
1041 cp_lexer_set_source_position (cp_lexer *lexer)
1043 cp_token *token = cp_lexer_peek_token (lexer);
1044 cp_lexer_set_source_position_from_token (token);
1047 /* Return a pointer to the next token in the token stream, but do not
1048 consume it. */
1050 static inline cp_token *
1051 cp_lexer_peek_token (cp_lexer *lexer)
1053 if (cp_lexer_debugging_p (lexer))
1055 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
1056 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
1057 putc ('\n', cp_lexer_debug_stream);
1059 return lexer->next_token;
1062 /* Return true if the next token has the indicated TYPE. */
1064 static inline bool
1065 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
1067 return cp_lexer_peek_token (lexer)->type == type;
1070 /* Return true if the next token does not have the indicated TYPE. */
1072 static inline bool
1073 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
1075 return !cp_lexer_next_token_is (lexer, type);
1078 /* Return true if the next token is the indicated KEYWORD. */
1080 static inline bool
1081 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
1083 return cp_lexer_peek_token (lexer)->keyword == keyword;
1086 static inline bool
1087 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
1089 return cp_lexer_peek_nth_token (lexer, n)->type == type;
1092 static inline bool
1093 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
1095 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
1098 /* Return true if KEYWORD can start a decl-specifier. */
1100 bool
1101 cp_keyword_starts_decl_specifier_p (enum rid keyword)
1103 switch (keyword)
1105 /* auto specifier: storage-class-specifier in C++,
1106 simple-type-specifier in C++0x. */
1107 case RID_AUTO:
1108 /* Storage classes. */
1109 case RID_REGISTER:
1110 case RID_STATIC:
1111 case RID_EXTERN:
1112 case RID_MUTABLE:
1113 case RID_THREAD:
1114 /* Elaborated type specifiers. */
1115 case RID_ENUM:
1116 case RID_CLASS:
1117 case RID_STRUCT:
1118 case RID_UNION:
1119 case RID_TYPENAME:
1120 /* Simple type specifiers. */
1121 case RID_CHAR:
1122 case RID_CHAR8:
1123 case RID_CHAR16:
1124 case RID_CHAR32:
1125 case RID_WCHAR:
1126 case RID_BOOL:
1127 case RID_SHORT:
1128 case RID_INT:
1129 case RID_LONG:
1130 case RID_SIGNED:
1131 case RID_UNSIGNED:
1132 case RID_FLOAT:
1133 case RID_DOUBLE:
1134 CASE_RID_FLOATN_NX:
1135 case RID_VOID:
1136 /* CV qualifiers. */
1137 case RID_CONST:
1138 case RID_VOLATILE:
1139 /* Function specifiers. */
1140 case RID_EXPLICIT:
1141 case RID_VIRTUAL:
1142 /* friend/typdef/inline specifiers. */
1143 case RID_FRIEND:
1144 case RID_TYPEDEF:
1145 case RID_INLINE:
1146 /* GNU extensions. */
1147 case RID_TYPEOF:
1148 /* C++11 extensions. */
1149 case RID_DECLTYPE:
1150 case RID_CONSTEXPR:
1151 /* C++20 extensions. */
1152 case RID_CONSTINIT:
1153 case RID_CONSTEVAL:
1154 return true;
1156 #define DEFTRAIT_TYPE(CODE, NAME, ARITY) \
1157 case RID_##CODE:
1158 #include "cp-trait.def"
1159 #undef DEFTRAIT_TYPE
1160 return true;
1162 default:
1163 if (keyword >= RID_FIRST_INT_N
1164 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
1165 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
1166 return true;
1167 return false;
1171 /* Return true if the next token is a keyword for a decl-specifier. */
1173 static bool
1174 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
1176 cp_token *token;
1178 token = cp_lexer_peek_token (lexer);
1179 return cp_keyword_starts_decl_specifier_p (token->keyword);
1182 /* Returns TRUE iff the token T begins a decltype type. */
1184 static bool
1185 token_is_decltype (cp_token *t)
1187 return (t->keyword == RID_DECLTYPE
1188 || t->type == CPP_DECLTYPE);
1191 /* Returns TRUE iff the next token begins a decltype type. */
1193 static bool
1194 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1196 cp_token *t = cp_lexer_peek_token (lexer);
1197 return token_is_decltype (t);
1200 /* Called when processing a token with tree_check_value; perform or defer the
1201 associated checks and return the value. */
1203 static tree
1204 saved_checks_value (struct tree_check *check_value)
1206 /* Perform any access checks that were deferred. */
1207 vec<deferred_access_check, va_gc> *checks;
1208 deferred_access_check *chk;
1209 checks = check_value->checks;
1210 if (checks)
1212 int i;
1213 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1214 perform_or_defer_access_check (chk->binfo,
1215 chk->decl,
1216 chk->diag_decl, tf_warning_or_error);
1218 /* Return the stored value. */
1219 return check_value->value;
1222 /* Return a pointer to the Nth token in the token stream. If N is 1,
1223 then this is precisely equivalent to cp_lexer_peek_token (except
1224 that it is not inline). One would like to disallow that case, but
1225 there is one case (cp_parser_nth_token_starts_template_id) where
1226 the caller passes a variable for N and it might be 1. */
1228 static cp_token *
1229 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1231 cp_token *token;
1233 /* N is 1-based, not zero-based. */
1234 gcc_assert (n > 0);
1236 if (cp_lexer_debugging_p (lexer))
1237 fprintf (cp_lexer_debug_stream,
1238 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1240 --n;
1241 token = lexer->next_token;
1242 while (n && token->type != CPP_EOF)
1244 ++token;
1245 if (!token->purged_p)
1246 --n;
1249 if (cp_lexer_debugging_p (lexer))
1251 cp_lexer_print_token (cp_lexer_debug_stream, token);
1252 putc ('\n', cp_lexer_debug_stream);
1255 return token;
1258 /* Return the next token, and advance the lexer's next_token pointer
1259 to point to the next non-purged token. */
1261 static cp_token *
1262 cp_lexer_consume_token (cp_lexer* lexer)
1264 cp_token *token = lexer->next_token;
1268 gcc_assert (token->type != CPP_EOF);
1269 lexer->next_token++;
1271 while (lexer->next_token->purged_p);
1273 cp_lexer_set_source_position_from_token (token);
1275 /* Provide debugging output. */
1276 if (cp_lexer_debugging_p (lexer))
1278 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1279 cp_lexer_print_token (cp_lexer_debug_stream, token);
1280 putc ('\n', cp_lexer_debug_stream);
1283 return token;
1286 /* Permanently remove the next token from the token stream, and
1287 advance the next_token pointer to refer to the next non-purged
1288 token. */
1290 static void
1291 cp_lexer_purge_token (cp_lexer *lexer)
1293 cp_token *tok = lexer->next_token;
1295 gcc_assert (tok->type != CPP_EOF);
1296 tok->purged_p = true;
1297 tok->location = UNKNOWN_LOCATION;
1298 tok->u.value = NULL_TREE;
1299 tok->keyword = RID_MAX;
1302 tok++;
1303 while (tok->purged_p);
1304 lexer->next_token = tok;
1307 /* Permanently remove all tokens after TOK, up to, but not
1308 including, the token that will be returned next by
1309 cp_lexer_peek_token. */
1311 static void
1312 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1314 cp_token *peek = lexer->next_token;
1316 gcc_assert (tok < peek);
1318 for (tok++; tok != peek; tok++)
1320 tok->purged_p = true;
1321 tok->location = UNKNOWN_LOCATION;
1322 tok->u.value = NULL_TREE;
1323 tok->keyword = RID_MAX;
1327 /* Begin saving tokens. All tokens consumed after this point will be
1328 preserved. */
1330 static void
1331 cp_lexer_save_tokens (cp_lexer* lexer)
1333 /* Provide debugging output. */
1334 if (cp_lexer_debugging_p (lexer))
1335 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1337 lexer->saved_tokens.safe_push (lexer->next_token);
1340 /* Commit to the portion of the token stream most recently saved. */
1342 static void
1343 cp_lexer_commit_tokens (cp_lexer* lexer)
1345 /* Provide debugging output. */
1346 if (cp_lexer_debugging_p (lexer))
1347 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1349 lexer->saved_tokens.pop ();
1352 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1353 to the token stream. Stop saving tokens. */
1355 static void
1356 cp_lexer_rollback_tokens (cp_lexer* lexer)
1358 /* Provide debugging output. */
1359 if (cp_lexer_debugging_p (lexer))
1360 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1362 lexer->next_token = lexer->saved_tokens.pop ();
1365 /* Determines what saved_token_sentinel does when going out of scope. */
1367 enum saved_token_sentinel_mode {
1368 STS_COMMIT,
1369 STS_ROLLBACK,
1370 STS_DONOTHING
1373 /* RAII wrapper around the above functions, with sanity checking (the token
1374 stream should be the same at the point of instantiation as it is at the
1375 point of destruction).
1377 Creating a variable saves tokens. MODE determines what happens when the
1378 object is destroyed. STS_COMMIT commits tokens (default),
1379 STS_ROLLBACK rolls-back and STS_DONOTHING does nothing. Calling
1380 rollback() will immediately roll-back tokens and set MODE to
1381 STS_DONOTHING. */
1383 struct saved_token_sentinel
1385 cp_lexer *lexer;
1386 unsigned len;
1387 saved_token_sentinel_mode mode;
1388 saved_token_sentinel (cp_lexer *_lexer,
1389 saved_token_sentinel_mode _mode = STS_COMMIT)
1390 : lexer (_lexer), mode (_mode)
1392 len = lexer->saved_tokens.length ();
1393 cp_lexer_save_tokens (lexer);
1395 void rollback ()
1397 cp_lexer_rollback_tokens (lexer);
1398 cp_lexer_set_source_position_from_token
1399 (cp_lexer_previous_token (lexer));
1400 mode = STS_DONOTHING;
1402 ~saved_token_sentinel ()
1404 if (mode == STS_COMMIT)
1405 cp_lexer_commit_tokens (lexer);
1406 else if (mode == STS_ROLLBACK)
1407 rollback ();
1409 gcc_assert (lexer->saved_tokens.length () == len);
1413 /* Print a representation of the TOKEN on the STREAM. */
1415 static void
1416 cp_lexer_print_token (FILE * stream, cp_token *token)
1418 /* We don't use cpp_type2name here because the parser defines
1419 a few tokens of its own. */
1420 static const char *const token_names[] = {
1421 /* cpplib-defined token types */
1422 #define OP(e, s) #e,
1423 #define TK(e, s) #e,
1424 TTYPE_TABLE
1425 #undef OP
1426 #undef TK
1427 /* C++ parser token types - see "Manifest constants", above. */
1428 "KEYWORD",
1429 "TEMPLATE_ID",
1430 "NESTED_NAME_SPECIFIER",
1433 /* For some tokens, print the associated data. */
1434 switch (token->type)
1436 case CPP_KEYWORD:
1437 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1438 For example, `struct' is mapped to an INTEGER_CST. */
1439 if (!identifier_p (token->u.value))
1440 break;
1441 /* fall through */
1442 case CPP_NAME:
1443 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1444 break;
1446 case CPP_STRING:
1447 case CPP_STRING16:
1448 case CPP_STRING32:
1449 case CPP_WSTRING:
1450 case CPP_UTF8STRING:
1451 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1452 break;
1454 case CPP_NUMBER:
1455 print_generic_expr (stream, token->u.value);
1456 break;
1458 default:
1459 /* If we have a name for the token, print it out. Otherwise, we
1460 simply give the numeric code. */
1461 if (token->type < ARRAY_SIZE(token_names))
1462 fputs (token_names[token->type], stream);
1463 else
1464 fprintf (stream, "[%d]", token->type);
1465 break;
1469 DEBUG_FUNCTION void
1470 debug (cp_token &ref)
1472 cp_lexer_print_token (stderr, &ref);
1473 fprintf (stderr, "\n");
1476 DEBUG_FUNCTION void
1477 debug (cp_token *ptr)
1479 if (ptr)
1480 debug (*ptr);
1481 else
1482 fprintf (stderr, "<nil>\n");
1486 /* Start emitting debugging information. */
1488 static void
1489 cp_lexer_start_debugging (cp_lexer* lexer)
1491 if (!LEXER_DEBUGGING_ENABLED_P)
1492 fatal_error (input_location,
1493 "%<LEXER_DEBUGGING_ENABLED_P%> is not set to true");
1495 lexer->debugging_p = true;
1496 cp_lexer_debug_stream = stderr;
1499 /* Stop emitting debugging information. */
1501 static void
1502 cp_lexer_stop_debugging (cp_lexer* lexer)
1504 if (!LEXER_DEBUGGING_ENABLED_P)
1505 fatal_error (input_location,
1506 "%<LEXER_DEBUGGING_ENABLED_P%> is not set to true");
1508 lexer->debugging_p = false;
1509 cp_lexer_debug_stream = NULL;
1512 /* Create a new cp_token_cache, representing a range of tokens. */
1514 static cp_token_cache *
1515 cp_token_cache_new (cp_token *first, cp_token *last)
1517 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1518 cache->first = first;
1519 cache->last = last;
1520 return cache;
1523 /* Diagnose if #pragma omp declare simd isn't followed immediately
1524 by function declaration or definition. */
1526 static inline void
1527 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1529 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1531 error ("%<#pragma omp declare %s%> not immediately followed by "
1532 "function declaration or definition",
1533 parser->omp_declare_simd->variant_p ? "variant" : "simd");
1534 parser->omp_declare_simd = NULL;
1538 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1539 and put that into "omp declare simd" attribute. */
1541 static inline void
1542 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1544 if (UNLIKELY (parser->omp_declare_simd != NULL))
1546 if (fndecl == error_mark_node)
1548 parser->omp_declare_simd = NULL;
1549 return;
1551 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1553 cp_ensure_no_omp_declare_simd (parser);
1554 return;
1559 /* Similarly, but for use in declaration parsing functions
1560 which call cp_parser_handle_directive_omp_attributes. */
1562 static inline void
1563 cp_finalize_omp_declare_simd (cp_parser *parser, cp_omp_declare_simd_data *data)
1565 if (parser->omp_declare_simd != data)
1566 return;
1568 if (!parser->omp_declare_simd->error_seen
1569 && !parser->omp_declare_simd->fndecl_seen)
1570 error_at (parser->omp_declare_simd->loc,
1571 "%<declare %s%> directive not immediately followed by "
1572 "function declaration or definition",
1573 parser->omp_declare_simd->variant_p ? "variant" : "simd");
1574 parser->omp_declare_simd = NULL;
1577 /* Diagnose if #pragma acc routine isn't followed immediately by function
1578 declaration or definition. */
1580 static inline void
1581 cp_ensure_no_oacc_routine (cp_parser *parser)
1583 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1585 error_at (parser->oacc_routine->loc,
1586 "%<#pragma acc routine%> not immediately followed by "
1587 "function declaration or definition");
1588 parser->oacc_routine = NULL;
1592 /* Decl-specifiers. */
1594 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1596 static void
1597 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1599 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1602 /* Declarators. */
1604 /* Nothing other than the parser should be creating declarators;
1605 declarators are a semi-syntactic representation of C++ entities.
1606 Other parts of the front end that need to create entities (like
1607 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1609 static cp_declarator *make_call_declarator
1610 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier,
1611 tree, tree, tree, tree, tree, location_t);
1612 static cp_declarator *make_array_declarator
1613 (cp_declarator *, tree);
1614 static cp_declarator *make_pointer_declarator
1615 (cp_cv_quals, cp_declarator *, tree);
1616 static cp_declarator *make_reference_declarator
1617 (cp_cv_quals, cp_declarator *, bool, tree);
1618 static cp_declarator *make_ptrmem_declarator
1619 (cp_cv_quals, tree, cp_declarator *, tree);
1621 /* An erroneous declarator. */
1622 static cp_declarator *cp_error_declarator;
1624 /* The obstack on which declarators and related data structures are
1625 allocated. */
1626 static struct obstack declarator_obstack;
1628 /* Alloc BYTES from the declarator memory pool. */
1630 static inline void *
1631 alloc_declarator (size_t bytes)
1633 return obstack_alloc (&declarator_obstack, bytes);
1636 /* Allocate a declarator of the indicated KIND. Clear fields that are
1637 common to all declarators. */
1639 static cp_declarator *
1640 make_declarator (cp_declarator_kind kind)
1642 cp_declarator *declarator;
1644 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1645 declarator->kind = kind;
1646 declarator->parenthesized = UNKNOWN_LOCATION;
1647 declarator->attributes = NULL_TREE;
1648 declarator->std_attributes = NULL_TREE;
1649 declarator->declarator = NULL;
1650 declarator->parameter_pack_p = false;
1651 declarator->id_loc = UNKNOWN_LOCATION;
1652 declarator->init_loc = UNKNOWN_LOCATION;
1654 return declarator;
1657 /* Make a declarator for a generalized identifier. If
1658 QUALIFYING_SCOPE is non-NULL, the identifier is
1659 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1660 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1661 is, if any. */
1663 static cp_declarator *
1664 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1665 special_function_kind sfk, location_t id_location)
1667 cp_declarator *declarator;
1669 /* It is valid to write:
1671 class C { void f(); };
1672 typedef C D;
1673 void D::f();
1675 The standard is not clear about whether `typedef const C D' is
1676 legal; as of 2002-09-15 the committee is considering that
1677 question. EDG 3.0 allows that syntax. Therefore, we do as
1678 well. */
1679 if (qualifying_scope && TYPE_P (qualifying_scope))
1680 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1682 gcc_assert (identifier_p (unqualified_name)
1683 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1684 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1686 declarator = make_declarator (cdk_id);
1687 declarator->u.id.qualifying_scope = qualifying_scope;
1688 declarator->u.id.unqualified_name = unqualified_name;
1689 declarator->u.id.sfk = sfk;
1690 declarator->id_loc = id_location;
1692 return declarator;
1695 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1696 of modifiers such as const or volatile to apply to the pointer
1697 type, represented as identifiers. ATTRIBUTES represent the attributes that
1698 appertain to the pointer or reference. */
1700 cp_declarator *
1701 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1702 tree attributes)
1704 cp_declarator *declarator;
1706 declarator = make_declarator (cdk_pointer);
1707 declarator->declarator = target;
1708 declarator->u.pointer.qualifiers = cv_qualifiers;
1709 declarator->u.pointer.class_type = NULL_TREE;
1710 if (target)
1712 declarator->id_loc = target->id_loc;
1713 declarator->parameter_pack_p = target->parameter_pack_p;
1714 target->parameter_pack_p = false;
1716 else
1717 declarator->parameter_pack_p = false;
1719 declarator->std_attributes = attributes;
1721 return declarator;
1724 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1725 represent the attributes that appertain to the pointer or
1726 reference. */
1728 cp_declarator *
1729 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1730 bool rvalue_ref, tree attributes)
1732 cp_declarator *declarator;
1734 declarator = make_declarator (cdk_reference);
1735 declarator->declarator = target;
1736 declarator->u.reference.qualifiers = cv_qualifiers;
1737 declarator->u.reference.rvalue_ref = rvalue_ref;
1738 if (target)
1740 declarator->id_loc = target->id_loc;
1741 declarator->parameter_pack_p = target->parameter_pack_p;
1742 target->parameter_pack_p = false;
1744 else
1745 declarator->parameter_pack_p = false;
1747 declarator->std_attributes = attributes;
1749 return declarator;
1752 /* Like make_pointer_declarator -- but for a pointer to a non-static
1753 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1754 appertain to the pointer or reference. */
1756 cp_declarator *
1757 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1758 cp_declarator *pointee,
1759 tree attributes)
1761 cp_declarator *declarator;
1763 declarator = make_declarator (cdk_ptrmem);
1764 declarator->declarator = pointee;
1765 declarator->u.pointer.qualifiers = cv_qualifiers;
1766 declarator->u.pointer.class_type = class_type;
1768 if (pointee)
1770 declarator->parameter_pack_p = pointee->parameter_pack_p;
1771 pointee->parameter_pack_p = false;
1773 else
1774 declarator->parameter_pack_p = false;
1776 declarator->std_attributes = attributes;
1778 return declarator;
1781 /* Make a declarator for the function given by TARGET, with the
1782 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1783 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1784 indicates what exceptions can be thrown. STD_ATTRS contains
1785 attributes that appertain to the function type. */
1787 cp_declarator *
1788 make_call_declarator (cp_declarator *target,
1789 tree parms,
1790 cp_cv_quals cv_qualifiers,
1791 cp_virt_specifiers virt_specifiers,
1792 cp_ref_qualifier ref_qualifier,
1793 tree tx_qualifier,
1794 tree exception_specification,
1795 tree late_return_type,
1796 tree requires_clause,
1797 tree std_attrs,
1798 location_t parens_loc)
1800 cp_declarator *declarator;
1802 declarator = make_declarator (cdk_function);
1803 declarator->declarator = target;
1804 declarator->u.function.parameters = parms;
1805 declarator->u.function.qualifiers = cv_qualifiers;
1806 declarator->u.function.virt_specifiers = virt_specifiers;
1807 declarator->u.function.ref_qualifier = ref_qualifier;
1808 declarator->u.function.tx_qualifier = tx_qualifier;
1809 declarator->u.function.exception_specification = exception_specification;
1810 declarator->u.function.late_return_type = late_return_type;
1811 declarator->u.function.requires_clause = requires_clause;
1812 declarator->u.function.parens_loc = parens_loc;
1813 if (target)
1815 declarator->id_loc = target->id_loc;
1816 declarator->parameter_pack_p = target->parameter_pack_p;
1817 target->parameter_pack_p = false;
1819 else
1820 declarator->parameter_pack_p = false;
1822 declarator->std_attributes = std_attrs;
1824 return declarator;
1827 /* Make a declarator for an array of BOUNDS elements, each of which is
1828 defined by ELEMENT. */
1830 cp_declarator *
1831 make_array_declarator (cp_declarator *element, tree bounds)
1833 cp_declarator *declarator;
1835 declarator = make_declarator (cdk_array);
1836 declarator->declarator = element;
1837 declarator->u.array.bounds = bounds;
1838 if (element)
1840 declarator->id_loc = element->id_loc;
1841 declarator->parameter_pack_p = element->parameter_pack_p;
1842 element->parameter_pack_p = false;
1844 else
1845 declarator->parameter_pack_p = false;
1847 return declarator;
1850 /* Determine whether the declarator we've seen so far can be a
1851 parameter pack, when followed by an ellipsis. */
1852 static bool
1853 declarator_can_be_parameter_pack (cp_declarator *declarator)
1855 if (declarator && declarator->parameter_pack_p)
1856 /* We already saw an ellipsis. */
1857 return false;
1859 /* Search for a declarator name, or any other declarator that goes
1860 after the point where the ellipsis could appear in a parameter
1861 pack. If we find any of these, then this declarator cannot be
1862 made into a parameter pack. */
1863 bool found = false;
1864 while (declarator && !found)
1866 switch ((int)declarator->kind)
1868 case cdk_id:
1869 case cdk_array:
1870 case cdk_decomp:
1871 found = true;
1872 break;
1874 case cdk_error:
1875 return true;
1877 default:
1878 declarator = declarator->declarator;
1879 break;
1883 return !found;
1886 cp_parameter_declarator *no_parameters;
1888 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1889 DECLARATOR and DEFAULT_ARGUMENT. */
1891 cp_parameter_declarator *
1892 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1893 cp_declarator *declarator,
1894 tree default_argument,
1895 location_t loc,
1896 bool template_parameter_pack_p = false)
1898 cp_parameter_declarator *parameter;
1900 parameter = ((cp_parameter_declarator *)
1901 alloc_declarator (sizeof (cp_parameter_declarator)));
1902 parameter->next = NULL;
1903 if (decl_specifiers)
1904 parameter->decl_specifiers = *decl_specifiers;
1905 else
1906 clear_decl_specs (&parameter->decl_specifiers);
1907 parameter->declarator = declarator;
1908 parameter->default_argument = default_argument;
1909 parameter->template_parameter_pack_p = template_parameter_pack_p;
1910 parameter->loc = loc;
1912 return parameter;
1915 /* Returns true iff DECLARATOR is a declaration for a function. */
1917 static bool
1918 function_declarator_p (const cp_declarator *declarator)
1920 while (declarator)
1922 if (declarator->kind == cdk_function
1923 && declarator->declarator->kind == cdk_id)
1924 return true;
1925 if (declarator->kind == cdk_id
1926 || declarator->kind == cdk_decomp
1927 || declarator->kind == cdk_error)
1928 return false;
1929 declarator = declarator->declarator;
1931 return false;
1934 /* The parser. */
1936 /* Overview
1937 --------
1939 A cp_parser parses the token stream as specified by the C++
1940 grammar. Its job is purely parsing, not semantic analysis. For
1941 example, the parser breaks the token stream into declarators,
1942 expressions, statements, and other similar syntactic constructs.
1943 It does not check that the types of the expressions on either side
1944 of an assignment-statement are compatible, or that a function is
1945 not declared with a parameter of type `void'.
1947 The parser invokes routines elsewhere in the compiler to perform
1948 semantic analysis and to build up the abstract syntax tree for the
1949 code processed.
1951 The parser (and the template instantiation code, which is, in a
1952 way, a close relative of parsing) are the only parts of the
1953 compiler that should be calling push_scope and pop_scope, or
1954 related functions. The parser (and template instantiation code)
1955 keeps track of what scope is presently active; everything else
1956 should simply honor that. (The code that generates static
1957 initializers may also need to set the scope, in order to check
1958 access control correctly when emitting the initializers.)
1960 Methodology
1961 -----------
1963 The parser is of the standard recursive-descent variety. Upcoming
1964 tokens in the token stream are examined in order to determine which
1965 production to use when parsing a non-terminal. Some C++ constructs
1966 require arbitrary look ahead to disambiguate. For example, it is
1967 impossible, in the general case, to tell whether a statement is an
1968 expression or declaration without scanning the entire statement.
1969 Therefore, the parser is capable of "parsing tentatively." When the
1970 parser is not sure what construct comes next, it enters this mode.
1971 Then, while we attempt to parse the construct, the parser queues up
1972 error messages, rather than issuing them immediately, and saves the
1973 tokens it consumes. If the construct is parsed successfully, the
1974 parser "commits", i.e., it issues any queued error messages and
1975 the tokens that were being preserved are permanently discarded.
1976 If, however, the construct is not parsed successfully, the parser
1977 rolls back its state completely so that it can resume parsing using
1978 a different alternative.
1980 Future Improvements
1981 -------------------
1983 The performance of the parser could probably be improved substantially.
1984 We could often eliminate the need to parse tentatively by looking ahead
1985 a little bit. In some places, this approach might not entirely eliminate
1986 the need to parse tentatively, but it might still speed up the average
1987 case. */
1989 /* Flags that are passed to some parsing functions. These values can
1990 be bitwise-ored together. */
1992 enum
1994 /* No flags. */
1995 CP_PARSER_FLAGS_NONE = 0x0,
1996 /* The construct is optional. If it is not present, then no error
1997 should be issued. */
1998 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1999 /* When parsing a type-specifier, treat user-defined type-names
2000 as non-type identifiers. */
2001 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
2002 /* When parsing a type-specifier, do not try to parse a class-specifier
2003 or enum-specifier. */
2004 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
2005 /* When parsing a decl-specifier-seq, only allow type-specifier or
2006 constexpr. */
2007 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
2008 /* When parsing a decl-specifier-seq, only allow mutable, constexpr or
2009 for C++20 consteval or for C++23 static. */
2010 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10,
2011 /* When parsing a decl-specifier-seq, allow missing typename. */
2012 CP_PARSER_FLAGS_TYPENAME_OPTIONAL = 0x20,
2013 /* When parsing of the noexcept-specifier should be delayed. */
2014 CP_PARSER_FLAGS_DELAY_NOEXCEPT = 0x40,
2015 /* When parsing a consteval declarator. */
2016 CP_PARSER_FLAGS_CONSTEVAL = 0x80
2019 /* This type is used for parameters and variables which hold
2020 combinations of the above flags. */
2021 typedef int cp_parser_flags;
2023 /* The different kinds of declarators we want to parse. */
2025 enum cp_parser_declarator_kind
2027 /* We want an abstract declarator. */
2028 CP_PARSER_DECLARATOR_ABSTRACT,
2029 /* We want a named declarator. */
2030 CP_PARSER_DECLARATOR_NAMED,
2031 /* We don't mind, but the name must be an unqualified-id. */
2032 CP_PARSER_DECLARATOR_EITHER
2035 /* The precedence values used to parse binary expressions. The minimum value
2036 of PREC must be 1, because zero is reserved to quickly discriminate
2037 binary operators from other tokens. */
2039 enum cp_parser_prec
2041 PREC_NOT_OPERATOR,
2042 PREC_LOGICAL_OR_EXPRESSION,
2043 PREC_LOGICAL_AND_EXPRESSION,
2044 PREC_INCLUSIVE_OR_EXPRESSION,
2045 PREC_EXCLUSIVE_OR_EXPRESSION,
2046 PREC_AND_EXPRESSION,
2047 PREC_EQUALITY_EXPRESSION,
2048 PREC_RELATIONAL_EXPRESSION,
2049 PREC_SPACESHIP_EXPRESSION,
2050 PREC_SHIFT_EXPRESSION,
2051 PREC_ADDITIVE_EXPRESSION,
2052 PREC_MULTIPLICATIVE_EXPRESSION,
2053 PREC_PM_EXPRESSION,
2054 NUM_PREC_VALUES = PREC_PM_EXPRESSION
2057 /* A mapping from a token type to a corresponding tree node type, with a
2058 precedence value. */
2060 struct cp_parser_binary_operations_map_node
2062 /* The token type. */
2063 enum cpp_ttype token_type;
2064 /* The corresponding tree code. */
2065 enum tree_code tree_type;
2066 /* The precedence of this operator. */
2067 enum cp_parser_prec prec;
2070 struct cp_parser_expression_stack_entry
2072 /* Left hand side of the binary operation we are currently
2073 parsing. */
2074 cp_expr lhs;
2075 /* Original tree code for left hand side, if it was a binary
2076 expression itself (used for -Wparentheses). */
2077 enum tree_code lhs_type;
2078 /* Tree code for the binary operation we are parsing. */
2079 enum tree_code tree_type;
2080 /* Precedence of the binary operation we are parsing. */
2081 enum cp_parser_prec prec;
2082 /* Location of the binary operation we are parsing. */
2083 location_t loc;
2084 /* Flags from the operator token. */
2085 unsigned char flags;
2088 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
2089 entries because precedence levels on the stack are monotonically
2090 increasing. */
2091 typedef struct cp_parser_expression_stack_entry
2092 cp_parser_expression_stack[NUM_PREC_VALUES];
2094 /* Prototypes. */
2096 /* Constructors and destructors. */
2098 static cp_parser_context *cp_parser_context_new
2099 (cp_parser_context *);
2101 /* Class variables. */
2103 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
2105 /* The operator-precedence table used by cp_parser_binary_expression.
2106 Transformed into an associative array (binops_by_token) by
2107 cp_parser_new. */
2109 static const cp_parser_binary_operations_map_node binops[] = {
2110 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
2111 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
2113 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
2114 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
2115 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
2117 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
2118 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
2120 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
2121 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
2123 { CPP_SPACESHIP, SPACESHIP_EXPR, PREC_SPACESHIP_EXPRESSION },
2125 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
2126 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
2127 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
2128 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
2130 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
2131 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
2133 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
2135 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
2137 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
2139 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
2141 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
2144 /* The same as binops, but initialized by cp_parser_new so that
2145 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
2146 for speed. */
2147 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
2149 /* Constructors and destructors. */
2151 /* Construct a new context. The context below this one on the stack
2152 is given by NEXT. */
2154 static cp_parser_context *
2155 cp_parser_context_new (cp_parser_context* next)
2157 cp_parser_context *context;
2159 /* Allocate the storage. */
2160 if (cp_parser_context_free_list != NULL)
2162 /* Pull the first entry from the free list. */
2163 context = cp_parser_context_free_list;
2164 cp_parser_context_free_list = context->next;
2165 memset (context, 0, sizeof (*context));
2167 else
2168 context = ggc_cleared_alloc<cp_parser_context> ();
2170 /* No errors have occurred yet in this context. */
2171 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
2172 /* If this is not the bottommost context, copy information that we
2173 need from the previous context. */
2174 if (next)
2176 /* If, in the NEXT context, we are parsing an `x->' or `x.'
2177 expression, then we are parsing one in this context, too. */
2178 context->object_type = next->object_type;
2179 /* Thread the stack. */
2180 context->next = next;
2183 return context;
2186 /* Managing the unparsed function queues. */
2188 #define unparsed_funs_with_default_args \
2189 parser->unparsed_queues->last ().funs_with_default_args
2190 #define unparsed_funs_with_definitions \
2191 parser->unparsed_queues->last ().funs_with_definitions
2192 #define unparsed_nsdmis \
2193 parser->unparsed_queues->last ().nsdmis
2194 #define unparsed_noexcepts \
2195 parser->unparsed_queues->last ().noexcepts
2196 #define unparsed_contracts \
2197 parser->unparsed_queues->last ().contracts
2199 static void
2200 push_unparsed_function_queues (cp_parser *parser)
2202 cp_unparsed_functions_entry e
2203 = { NULL, make_tree_vector (), NULL, NULL, NULL };
2204 vec_safe_push (parser->unparsed_queues, e);
2207 static void
2208 pop_unparsed_function_queues (cp_parser *parser)
2210 release_tree_vector (unparsed_funs_with_definitions);
2211 parser->unparsed_queues->pop ();
2214 /* Prototypes. */
2216 /* Routines to parse various constructs.
2218 Those that return `tree' will return the error_mark_node (rather
2219 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2220 Sometimes, they will return an ordinary node if error-recovery was
2221 attempted, even though a parse error occurred. So, to check
2222 whether or not a parse error occurred, you should always use
2223 cp_parser_error_occurred. If the construct is optional (indicated
2224 either by an `_opt' in the name of the function that does the
2225 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2226 the construct is not present. */
2228 /* Lexical conventions [gram.lex] */
2230 static tree finish_userdef_string_literal
2231 (tree);
2233 /* Basic concepts [gram.basic] */
2235 static void cp_parser_translation_unit (cp_parser *);
2237 /* Expressions [gram.expr] */
2239 static cp_expr cp_parser_primary_expression
2240 (cp_parser *, bool, bool, bool, cp_id_kind *);
2241 static cp_expr cp_parser_id_expression
2242 (cp_parser *, bool, bool, bool *, bool, bool);
2243 static cp_expr cp_parser_unqualified_id
2244 (cp_parser *, bool, bool, bool, bool);
2245 static tree cp_parser_nested_name_specifier_opt
2246 (cp_parser *, bool, bool, bool, bool, bool = false);
2247 static tree cp_parser_nested_name_specifier
2248 (cp_parser *, bool, bool, bool, bool);
2249 static tree cp_parser_qualifying_entity
2250 (cp_parser *, bool, bool, bool, bool, bool);
2251 static cp_expr cp_parser_postfix_expression
2252 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2253 static tree cp_parser_postfix_open_square_expression
2254 (cp_parser *, tree, bool, bool);
2255 static tree cp_parser_postfix_dot_deref_expression
2256 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2257 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2258 (cp_parser *, int, bool, bool, bool *, location_t * = NULL,
2259 bool = false);
2260 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2261 enum { non_attr = 0, normal_attr = 1, id_attr = 2, assume_attr = 3 };
2262 static void cp_parser_pseudo_destructor_name
2263 (cp_parser *, tree, tree *, tree *);
2264 static cp_expr cp_parser_unary_expression
2265 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2266 static enum tree_code cp_parser_unary_operator
2267 (cp_token *);
2268 static tree cp_parser_has_attribute_expression
2269 (cp_parser *);
2270 static tree cp_parser_new_expression
2271 (cp_parser *);
2272 static vec<tree, va_gc> *cp_parser_new_placement
2273 (cp_parser *);
2274 static tree cp_parser_new_type_id
2275 (cp_parser *, tree *);
2276 static cp_declarator *cp_parser_new_declarator_opt
2277 (cp_parser *);
2278 static cp_declarator *cp_parser_direct_new_declarator
2279 (cp_parser *);
2280 static vec<tree, va_gc> *cp_parser_new_initializer
2281 (cp_parser *);
2282 static tree cp_parser_delete_expression
2283 (cp_parser *);
2284 static cp_expr cp_parser_cast_expression
2285 (cp_parser *, bool, bool, bool, cp_id_kind *);
2286 static cp_expr cp_parser_binary_expression
2287 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2288 static tree cp_parser_question_colon_clause
2289 (cp_parser *, cp_expr);
2290 static cp_expr cp_parser_conditional_expression (cp_parser *);
2291 static cp_expr cp_parser_assignment_expression
2292 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2293 static enum tree_code cp_parser_assignment_operator_opt
2294 (cp_parser *);
2295 static cp_expr cp_parser_expression
2296 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2297 static cp_expr cp_parser_constant_expression
2298 (cp_parser *, int = 0, bool * = NULL, bool = false);
2299 static cp_expr cp_parser_builtin_offsetof
2300 (cp_parser *);
2301 static cp_expr cp_parser_lambda_expression
2302 (cp_parser *);
2303 static void cp_parser_lambda_introducer
2304 (cp_parser *, tree);
2305 static bool cp_parser_lambda_declarator_opt
2306 (cp_parser *, tree);
2307 static void cp_parser_lambda_body
2308 (cp_parser *, tree);
2310 /* Statements [gram.stmt.stmt] */
2312 static void cp_parser_statement
2313 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2314 static void cp_parser_label_for_labeled_statement
2315 (cp_parser *, tree);
2316 static tree cp_parser_expression_statement
2317 (cp_parser *, tree);
2318 static tree cp_parser_compound_statement
2319 (cp_parser *, tree, int, bool);
2320 static void cp_parser_statement_seq_opt
2321 (cp_parser *, tree);
2322 static tree cp_parser_selection_statement
2323 (cp_parser *, bool *, vec<tree> *);
2324 static tree cp_parser_condition
2325 (cp_parser *);
2326 static tree cp_parser_iteration_statement
2327 (cp_parser *, bool *, bool, unsigned short);
2328 static bool cp_parser_init_statement
2329 (cp_parser *, tree *decl);
2330 static tree cp_parser_for
2331 (cp_parser *, bool, unsigned short);
2332 static tree cp_parser_c_for
2333 (cp_parser *, tree, tree, bool, unsigned short);
2334 static tree cp_parser_range_for
2335 (cp_parser *, tree, tree, tree, bool, unsigned short, bool);
2336 static void do_range_for_auto_deduction
2337 (tree, tree, tree, unsigned int);
2338 static tree cp_parser_perform_range_for_lookup
2339 (tree, tree *, tree *);
2340 static tree cp_parser_range_for_member_function
2341 (tree, tree);
2342 static tree cp_parser_jump_statement
2343 (cp_parser *);
2344 static void cp_parser_declaration_statement
2345 (cp_parser *);
2347 static tree cp_parser_implicitly_scoped_statement
2348 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2349 static void cp_parser_already_scoped_statement
2350 (cp_parser *, bool *, const token_indent_info &);
2352 /* State of module-declaration parsing. */
2353 enum module_parse
2355 MP_NOT_MODULE, /* Not a module. */
2357 _MP_UNUSED,
2359 MP_FIRST, /* First declaration of TU. */
2360 MP_GLOBAL, /* Global Module Fragment. */
2362 MP_PURVIEW_IMPORTS, /* Imports of a module. */
2363 MP_PURVIEW, /* Purview of a named module. */
2365 MP_PRIVATE_IMPORTS, /* Imports of a Private Module Fragment. */
2366 MP_PRIVATE, /* Private Module Fragment. */
2369 static module_parse cp_parser_module_declaration
2370 (cp_parser *parser, module_parse, bool exporting);
2371 static void cp_parser_import_declaration
2372 (cp_parser *parser, module_parse, bool exporting);
2374 /* Declarations [gram.dcl.dcl] */
2376 static void cp_parser_declaration_seq_opt
2377 (cp_parser *);
2378 static void cp_parser_declaration
2379 (cp_parser *, tree);
2380 static void cp_parser_toplevel_declaration
2381 (cp_parser *);
2382 static void cp_parser_block_declaration
2383 (cp_parser *, bool);
2384 static void cp_parser_simple_declaration
2385 (cp_parser *, bool, tree *);
2386 static void cp_parser_decl_specifier_seq
2387 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2388 static tree cp_parser_storage_class_specifier_opt
2389 (cp_parser *);
2390 static tree cp_parser_function_specifier_opt
2391 (cp_parser *, cp_decl_specifier_seq *);
2392 static tree cp_parser_type_specifier
2393 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2394 int *, bool *);
2395 static tree cp_parser_simple_type_specifier
2396 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2397 static tree cp_parser_placeholder_type_specifier
2398 (cp_parser *, location_t, tree, bool);
2399 static tree cp_parser_type_name
2400 (cp_parser *, bool);
2401 static tree cp_parser_nonclass_name
2402 (cp_parser* parser);
2403 static tree cp_parser_elaborated_type_specifier
2404 (cp_parser *, bool, bool);
2405 static tree cp_parser_enum_specifier
2406 (cp_parser *);
2407 static void cp_parser_enumerator_list
2408 (cp_parser *, tree);
2409 static void cp_parser_enumerator_definition
2410 (cp_parser *, tree);
2411 static tree cp_parser_namespace_name
2412 (cp_parser *);
2413 static void cp_parser_namespace_definition
2414 (cp_parser *);
2415 static void cp_parser_namespace_body
2416 (cp_parser *);
2417 static tree cp_parser_qualified_namespace_specifier
2418 (cp_parser *);
2419 static void cp_parser_namespace_alias_definition
2420 (cp_parser *);
2421 static bool cp_parser_using_declaration
2422 (cp_parser *, bool);
2423 static void cp_parser_using_directive
2424 (cp_parser *);
2425 static void cp_parser_using_enum
2426 (cp_parser *);
2427 static tree cp_parser_alias_declaration
2428 (cp_parser *);
2429 static void cp_parser_asm_definition
2430 (cp_parser *);
2431 static void cp_parser_linkage_specification
2432 (cp_parser *, tree);
2433 static void cp_parser_static_assert
2434 (cp_parser *, bool);
2435 static tree cp_parser_decltype
2436 (cp_parser *);
2437 static tree cp_parser_decomposition_declaration
2438 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2440 /* Declarators [gram.dcl.decl] */
2442 static tree cp_parser_init_declarator
2443 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *,
2444 vec<deferred_access_check, va_gc> *, bool, bool, int, bool *, tree *,
2445 location_t *, tree *);
2446 static cp_declarator *cp_parser_declarator
2447 (cp_parser *, cp_parser_declarator_kind, cp_parser_flags, int *, bool *,
2448 bool, bool, bool);
2449 static cp_declarator *cp_parser_direct_declarator
2450 (cp_parser *, cp_parser_declarator_kind, cp_parser_flags, int *, bool, bool,
2451 bool);
2452 static enum tree_code cp_parser_ptr_operator
2453 (cp_parser *, tree *, cp_cv_quals *, tree *);
2454 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2455 (cp_parser *);
2456 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2457 (cp_parser *);
2458 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2459 (cp_parser *);
2460 static tree cp_parser_tx_qualifier_opt
2461 (cp_parser *);
2462 static tree cp_parser_late_return_type_opt
2463 (cp_parser *, cp_declarator *, tree &);
2464 static tree cp_parser_declarator_id
2465 (cp_parser *, bool);
2466 static tree cp_parser_type_id
2467 (cp_parser *, cp_parser_flags = CP_PARSER_FLAGS_NONE, location_t * = NULL);
2468 static tree cp_parser_template_type_arg
2469 (cp_parser *);
2470 static tree cp_parser_trailing_type_id (cp_parser *);
2471 static tree cp_parser_type_id_1
2472 (cp_parser *, cp_parser_flags, bool, bool, location_t *);
2473 static void cp_parser_type_specifier_seq
2474 (cp_parser *, cp_parser_flags, bool, bool, cp_decl_specifier_seq *);
2475 static tree cp_parser_parameter_declaration_clause
2476 (cp_parser *, cp_parser_flags);
2477 static tree cp_parser_parameter_declaration_list
2478 (cp_parser *, cp_parser_flags, auto_vec<tree> *);
2479 static cp_parameter_declarator *cp_parser_parameter_declaration
2480 (cp_parser *, cp_parser_flags, bool, bool *);
2481 static tree cp_parser_default_argument
2482 (cp_parser *, bool);
2483 static void cp_parser_function_body
2484 (cp_parser *, bool);
2485 static tree cp_parser_initializer
2486 (cp_parser *, bool *, bool *, bool = false);
2487 static cp_expr cp_parser_initializer_clause
2488 (cp_parser *, bool *);
2489 static cp_expr cp_parser_braced_list
2490 (cp_parser*, bool*);
2491 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2492 (cp_parser *, bool *, bool *);
2494 static void cp_parser_ctor_initializer_opt_and_function_body
2495 (cp_parser *, bool);
2497 static tree cp_parser_late_parsing_omp_declare_simd
2498 (cp_parser *, tree);
2500 static tree cp_parser_late_parsing_oacc_routine
2501 (cp_parser *, tree);
2503 static tree synthesize_implicit_template_parm
2504 (cp_parser *, tree);
2505 static tree finish_fully_implicit_template
2506 (cp_parser *, tree);
2507 static void abort_fully_implicit_template
2508 (cp_parser *);
2510 /* Classes [gram.class] */
2512 static tree cp_parser_class_name
2513 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2514 static tree cp_parser_class_specifier
2515 (cp_parser *);
2516 static tree cp_parser_class_head
2517 (cp_parser *, bool *);
2518 static enum tag_types cp_parser_class_key
2519 (cp_parser *);
2520 static void cp_parser_type_parameter_key
2521 (cp_parser* parser);
2522 static void cp_parser_member_specification_opt
2523 (cp_parser *);
2524 static void cp_parser_member_declaration
2525 (cp_parser *);
2526 static tree cp_parser_pure_specifier
2527 (cp_parser *);
2528 static tree cp_parser_constant_initializer
2529 (cp_parser *);
2531 /* Derived classes [gram.class.derived] */
2533 static tree cp_parser_base_clause
2534 (cp_parser *);
2535 static tree cp_parser_base_specifier
2536 (cp_parser *);
2538 /* Special member functions [gram.special] */
2540 static tree cp_parser_conversion_function_id
2541 (cp_parser *);
2542 static tree cp_parser_conversion_type_id
2543 (cp_parser *);
2544 static cp_declarator *cp_parser_conversion_declarator_opt
2545 (cp_parser *);
2546 static void cp_parser_ctor_initializer_opt
2547 (cp_parser *);
2548 static void cp_parser_mem_initializer_list
2549 (cp_parser *);
2550 static tree cp_parser_mem_initializer
2551 (cp_parser *);
2552 static tree cp_parser_mem_initializer_id
2553 (cp_parser *);
2555 /* Overloading [gram.over] */
2557 static cp_expr cp_parser_operator_function_id
2558 (cp_parser *);
2559 static cp_expr cp_parser_operator
2560 (cp_parser *, location_t);
2562 /* Templates [gram.temp] */
2564 static void cp_parser_template_declaration
2565 (cp_parser *, bool);
2566 static tree cp_parser_template_parameter_list
2567 (cp_parser *);
2568 static tree cp_parser_template_parameter
2569 (cp_parser *, bool *, bool *);
2570 static tree cp_parser_type_parameter
2571 (cp_parser *, bool *);
2572 static tree cp_parser_template_id
2573 (cp_parser *, bool, bool, enum tag_types, bool);
2574 static tree cp_parser_template_id_expr
2575 (cp_parser *, bool, bool, bool);
2576 static tree cp_parser_template_name
2577 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2578 static tree cp_parser_template_argument_list
2579 (cp_parser *);
2580 static tree cp_parser_template_argument
2581 (cp_parser *);
2582 static void cp_parser_explicit_instantiation
2583 (cp_parser *);
2584 static void cp_parser_explicit_specialization
2585 (cp_parser *);
2587 /* Exception handling [gram.except] */
2589 static tree cp_parser_try_block
2590 (cp_parser *);
2591 static void cp_parser_function_try_block
2592 (cp_parser *);
2593 static void cp_parser_handler_seq
2594 (cp_parser *);
2595 static void cp_parser_handler
2596 (cp_parser *);
2597 static tree cp_parser_exception_declaration
2598 (cp_parser *);
2599 static tree cp_parser_throw_expression
2600 (cp_parser *);
2601 static tree cp_parser_exception_specification_opt
2602 (cp_parser *, cp_parser_flags);
2603 static tree cp_parser_type_id_list
2604 (cp_parser *);
2605 static tree cp_parser_noexcept_specification_opt
2606 (cp_parser *, cp_parser_flags, bool, bool *, bool);
2608 /* GNU Extensions */
2610 static tree cp_parser_asm_specification_opt
2611 (cp_parser *);
2612 static tree cp_parser_asm_operand_list
2613 (cp_parser *);
2614 static tree cp_parser_asm_clobber_list
2615 (cp_parser *);
2616 static tree cp_parser_asm_label_list
2617 (cp_parser *);
2618 static bool cp_next_tokens_can_be_attribute_p
2619 (cp_parser *);
2620 static bool cp_next_tokens_can_be_gnu_attribute_p
2621 (cp_parser *);
2622 static bool cp_next_tokens_can_be_std_attribute_p
2623 (cp_parser *);
2624 static bool cp_nth_tokens_can_be_std_attribute_p
2625 (cp_parser *, size_t);
2626 static bool cp_nth_tokens_can_be_gnu_attribute_p
2627 (cp_parser *, size_t);
2628 static bool cp_nth_tokens_can_be_attribute_p
2629 (cp_parser *, size_t);
2630 static tree cp_parser_attributes_opt
2631 (cp_parser *);
2632 static tree cp_parser_gnu_attributes_opt
2633 (cp_parser *);
2634 static tree cp_parser_gnu_attribute_list
2635 (cp_parser *, bool = false);
2636 static tree cp_parser_std_attribute
2637 (cp_parser *, tree);
2638 static tree cp_parser_std_attribute_spec
2639 (cp_parser *);
2640 static tree cp_parser_std_attribute_spec_seq
2641 (cp_parser *);
2642 static size_t cp_parser_skip_std_attribute_spec_seq
2643 (cp_parser *, size_t);
2644 static size_t cp_parser_skip_attributes_opt
2645 (cp_parser *, size_t);
2646 static bool cp_parser_extension_opt
2647 (cp_parser *, int *);
2648 static void cp_parser_label_declaration
2649 (cp_parser *);
2651 /* Concept Extensions */
2653 static tree cp_parser_concept_definition
2654 (cp_parser *);
2655 static tree cp_parser_constraint_expression
2656 (cp_parser *);
2657 static tree cp_parser_requires_clause_opt
2658 (cp_parser *, bool);
2659 static tree cp_parser_requires_expression
2660 (cp_parser *);
2661 static tree cp_parser_requirement_parameter_list
2662 (cp_parser *);
2663 static tree cp_parser_requirement_body
2664 (cp_parser *);
2665 static tree cp_parser_requirement_seq
2666 (cp_parser *);
2667 static tree cp_parser_requirement
2668 (cp_parser *);
2669 static tree cp_parser_simple_requirement
2670 (cp_parser *);
2671 static tree cp_parser_compound_requirement
2672 (cp_parser *);
2673 static tree cp_parser_type_requirement
2674 (cp_parser *);
2675 static tree cp_parser_nested_requirement
2676 (cp_parser *);
2678 /* Transactional Memory Extensions */
2680 static tree cp_parser_transaction
2681 (cp_parser *, cp_token *);
2682 static tree cp_parser_transaction_expression
2683 (cp_parser *, enum rid);
2684 static void cp_parser_function_transaction
2685 (cp_parser *, enum rid);
2686 static tree cp_parser_transaction_cancel
2687 (cp_parser *);
2689 /* Coroutine extensions. */
2691 static tree cp_parser_yield_expression
2692 (cp_parser *);
2694 /* Contracts */
2696 static void cp_parser_late_contract_condition
2697 (cp_parser *, tree, tree);
2699 enum pragma_context {
2700 pragma_external,
2701 pragma_member,
2702 pragma_objc_icode,
2703 pragma_stmt,
2704 pragma_compound
2706 static bool cp_parser_pragma
2707 (cp_parser *, enum pragma_context, bool *);
2709 /* Objective-C++ Productions */
2711 static tree cp_parser_objc_message_receiver
2712 (cp_parser *);
2713 static tree cp_parser_objc_message_args
2714 (cp_parser *);
2715 static tree cp_parser_objc_message_expression
2716 (cp_parser *);
2717 static cp_expr cp_parser_objc_encode_expression
2718 (cp_parser *);
2719 static tree cp_parser_objc_defs_expression
2720 (cp_parser *);
2721 static tree cp_parser_objc_protocol_expression
2722 (cp_parser *);
2723 static tree cp_parser_objc_selector_expression
2724 (cp_parser *);
2725 static cp_expr cp_parser_objc_expression
2726 (cp_parser *);
2727 static bool cp_parser_objc_selector_p
2728 (enum cpp_ttype);
2729 static tree cp_parser_objc_selector
2730 (cp_parser *);
2731 static tree cp_parser_objc_protocol_refs_opt
2732 (cp_parser *);
2733 static void cp_parser_objc_declaration
2734 (cp_parser *, tree);
2735 static tree cp_parser_objc_statement
2736 (cp_parser *);
2737 static bool cp_parser_objc_valid_prefix_attributes
2738 (cp_parser *, tree *);
2739 static void cp_parser_objc_at_property_declaration
2740 (cp_parser *) ;
2741 static void cp_parser_objc_at_synthesize_declaration
2742 (cp_parser *) ;
2743 static void cp_parser_objc_at_dynamic_declaration
2744 (cp_parser *) ;
2745 static tree cp_parser_objc_struct_declaration
2746 (cp_parser *) ;
2748 /* Utility Routines */
2750 static cp_expr cp_parser_lookup_name
2751 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2752 static tree cp_parser_lookup_name_simple
2753 (cp_parser *, tree, location_t);
2754 static tree cp_parser_maybe_treat_template_as_class
2755 (tree, bool);
2756 static bool cp_parser_check_declarator_template_parameters
2757 (cp_parser *, cp_declarator *, location_t);
2758 static bool cp_parser_check_template_parameters
2759 (cp_parser *, unsigned, bool, location_t, cp_declarator *);
2760 static cp_expr cp_parser_simple_cast_expression
2761 (cp_parser *);
2762 static tree cp_parser_global_scope_opt
2763 (cp_parser *, bool);
2764 static bool cp_parser_constructor_declarator_p
2765 (cp_parser *, cp_parser_flags, bool);
2766 static tree cp_parser_function_definition_from_specifiers_and_declarator
2767 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2768 static tree cp_parser_function_definition_after_declarator
2769 (cp_parser *, bool);
2770 static bool cp_parser_template_declaration_after_export
2771 (cp_parser *, bool);
2772 static void cp_parser_perform_template_parameter_access_checks
2773 (vec<deferred_access_check, va_gc> *);
2774 static tree cp_parser_single_declaration
2775 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2776 static cp_expr cp_parser_functional_cast
2777 (cp_parser *, tree);
2778 static tree cp_parser_save_member_function_body
2779 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2780 static tree cp_parser_save_nsdmi
2781 (cp_parser *);
2782 static tree cp_parser_enclosed_template_argument_list
2783 (cp_parser *);
2784 static void cp_parser_save_default_args
2785 (cp_parser *, tree);
2786 static void cp_parser_late_parsing_for_member
2787 (cp_parser *, tree);
2788 static tree cp_parser_late_parse_one_default_arg
2789 (cp_parser *, tree, tree, tree);
2790 static void cp_parser_late_parsing_nsdmi
2791 (cp_parser *, tree);
2792 static void cp_parser_late_parsing_default_args
2793 (cp_parser *, tree);
2794 static tree cp_parser_sizeof_operand
2795 (cp_parser *, enum rid);
2796 static cp_expr cp_parser_trait
2797 (cp_parser *, enum rid);
2798 static bool cp_parser_declares_only_class_p
2799 (cp_parser *);
2800 static void cp_parser_set_storage_class
2801 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2802 static void cp_parser_set_decl_spec_type
2803 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2804 static void set_and_check_decl_spec_loc
2805 (cp_decl_specifier_seq *decl_specs,
2806 cp_decl_spec ds, cp_token *);
2807 static bool cp_parser_friend_p
2808 (const cp_decl_specifier_seq *);
2809 static void cp_parser_required_error
2810 (cp_parser *, required_token, bool, location_t);
2811 static cp_token *cp_parser_require
2812 (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2813 static cp_token *cp_parser_require_keyword
2814 (cp_parser *, enum rid, required_token);
2815 static bool cp_parser_token_starts_function_definition_p
2816 (cp_token *);
2817 static bool cp_parser_next_token_starts_class_definition_p
2818 (cp_parser *);
2819 static bool cp_parser_next_token_ends_template_argument_p
2820 (cp_parser *);
2821 static bool cp_parser_nth_token_starts_template_argument_list_p
2822 (cp_parser *, size_t);
2823 static enum tag_types cp_parser_token_is_class_key
2824 (cp_token *);
2825 static enum tag_types cp_parser_token_is_type_parameter_key
2826 (cp_token *);
2827 static void cp_parser_maybe_warn_enum_key (cp_parser *, location_t, tree, rid);
2828 static void cp_parser_check_class_key
2829 (cp_parser *, location_t, enum tag_types, tree type, bool, bool);
2830 static void cp_parser_check_access_in_redeclaration
2831 (tree type, location_t location);
2832 static bool cp_parser_optional_template_keyword
2833 (cp_parser *);
2834 static void cp_parser_pre_parsed_nested_name_specifier
2835 (cp_parser *);
2836 static bool cp_parser_cache_group
2837 (cp_parser *, enum cpp_ttype, unsigned);
2838 static tree cp_parser_cache_defarg
2839 (cp_parser *parser, bool nsdmi);
2840 static void cp_parser_parse_tentatively
2841 (cp_parser *);
2842 static void cp_parser_commit_to_tentative_parse
2843 (cp_parser *);
2844 static void cp_parser_commit_to_topmost_tentative_parse
2845 (cp_parser *);
2846 static void cp_parser_abort_tentative_parse
2847 (cp_parser *);
2848 static bool cp_parser_parse_definitely
2849 (cp_parser *);
2850 static inline bool cp_parser_parsing_tentatively
2851 (cp_parser *);
2852 static bool cp_parser_uncommitted_to_tentative_parse_p
2853 (cp_parser *);
2854 static void cp_parser_error
2855 (cp_parser *, const char *);
2856 static void cp_parser_name_lookup_error
2857 (cp_parser *, tree, tree, name_lookup_error, location_t);
2858 static bool cp_parser_simulate_error
2859 (cp_parser *);
2860 static bool cp_parser_check_type_definition
2861 (cp_parser *);
2862 static void cp_parser_check_for_definition_in_return_type
2863 (cp_declarator *, tree, location_t type_location);
2864 static void cp_parser_check_for_invalid_template_id
2865 (cp_parser *, tree, enum tag_types, location_t location);
2866 static bool cp_parser_non_integral_constant_expression
2867 (cp_parser *, non_integral_constant);
2868 static void cp_parser_diagnose_invalid_type_name
2869 (cp_parser *, tree, location_t);
2870 static bool cp_parser_parse_and_diagnose_invalid_type_name
2871 (cp_parser *);
2872 static int cp_parser_skip_to_closing_parenthesis
2873 (cp_parser *, bool, bool, bool);
2874 static void cp_parser_skip_to_end_of_statement
2875 (cp_parser *);
2876 static void cp_parser_consume_semicolon_at_end_of_statement
2877 (cp_parser *);
2878 static void cp_parser_skip_to_end_of_block_or_statement
2879 (cp_parser *);
2880 static bool cp_parser_skip_to_closing_brace
2881 (cp_parser *);
2882 static bool cp_parser_skip_entire_template_parameter_list
2883 (cp_parser *);
2884 static void cp_parser_require_end_of_template_parameter_list
2885 (cp_parser *);
2886 static bool cp_parser_skip_to_end_of_template_parameter_list
2887 (cp_parser *);
2888 static void cp_parser_skip_to_pragma_eol
2889 (cp_parser*, cp_token *);
2890 static bool cp_parser_error_occurred
2891 (cp_parser *);
2892 static bool cp_parser_allow_gnu_extensions_p
2893 (cp_parser *);
2894 static bool cp_parser_is_pure_string_literal
2895 (cp_token *);
2896 static bool cp_parser_is_string_literal
2897 (cp_token *);
2898 static bool cp_parser_is_keyword
2899 (cp_token *, enum rid);
2900 static tree cp_parser_make_typename_type
2901 (cp_parser *, tree, location_t location);
2902 static cp_declarator * cp_parser_make_indirect_declarator
2903 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2904 static bool cp_parser_compound_literal_p
2905 (cp_parser *);
2906 static bool cp_parser_array_designator_p
2907 (cp_parser *);
2908 static bool cp_parser_init_statement_p
2909 (cp_parser *);
2910 static bool cp_parser_skip_up_to_closing_square_bracket
2911 (cp_parser *);
2912 static bool cp_parser_skip_to_closing_square_bracket
2913 (cp_parser *);
2914 static size_t cp_parser_skip_balanced_tokens (cp_parser *, size_t);
2916 // -------------------------------------------------------------------------- //
2917 // Unevaluated Operand Guard
2919 // Implementation of an RAII helper for unevaluated operand parsing.
2920 cp_unevaluated::cp_unevaluated ()
2922 ++cp_unevaluated_operand;
2923 ++c_inhibit_evaluation_warnings;
2926 cp_unevaluated::~cp_unevaluated ()
2928 --c_inhibit_evaluation_warnings;
2929 --cp_unevaluated_operand;
2932 // -------------------------------------------------------------------------- //
2933 // Tentative Parsing
2935 /* Returns nonzero if we are parsing tentatively. */
2937 static inline bool
2938 cp_parser_parsing_tentatively (cp_parser* parser)
2940 return parser->context->next != NULL;
2943 /* Returns nonzero if TOKEN is a string literal. */
2945 static bool
2946 cp_parser_is_pure_string_literal (cp_token* token)
2948 return (token->type == CPP_STRING ||
2949 token->type == CPP_STRING16 ||
2950 token->type == CPP_STRING32 ||
2951 token->type == CPP_WSTRING ||
2952 token->type == CPP_UTF8STRING);
2955 /* Returns nonzero if TOKEN is a string literal
2956 of a user-defined string literal. */
2958 static bool
2959 cp_parser_is_string_literal (cp_token* token)
2961 return (cp_parser_is_pure_string_literal (token) ||
2962 token->type == CPP_STRING_USERDEF ||
2963 token->type == CPP_STRING16_USERDEF ||
2964 token->type == CPP_STRING32_USERDEF ||
2965 token->type == CPP_WSTRING_USERDEF ||
2966 token->type == CPP_UTF8STRING_USERDEF);
2969 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2971 static bool
2972 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2974 return token->keyword == keyword;
2977 /* Helper function for cp_parser_error.
2978 Having peeked a token of kind TOK1_KIND that might signify
2979 a conflict marker, peek successor tokens to determine
2980 if we actually do have a conflict marker.
2981 Specifically, we consider a run of 7 '<', '=' or '>' characters
2982 at the start of a line as a conflict marker.
2983 These come through the lexer as three pairs and a single,
2984 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2985 If it returns true, *OUT_LOC is written to with the location/range
2986 of the marker. */
2988 static bool
2989 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2990 location_t *out_loc)
2992 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2993 if (token2->type != tok1_kind)
2994 return false;
2995 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2996 if (token3->type != tok1_kind)
2997 return false;
2998 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2999 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
3000 return false;
3002 /* It must be at the start of the line. */
3003 location_t start_loc = cp_lexer_peek_token (lexer)->location;
3004 if (LOCATION_COLUMN (start_loc) != 1)
3005 return false;
3007 /* We have a conflict marker. Construct a location of the form:
3008 <<<<<<<
3009 ^~~~~~~
3010 with start == caret, finishing at the end of the marker. */
3011 location_t finish_loc = get_finish (token4->location);
3012 *out_loc = make_location (start_loc, start_loc, finish_loc);
3014 return true;
3017 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
3018 RT_CLOSE_PAREN. */
3020 static const char *
3021 get_matching_symbol (required_token token_desc)
3023 switch (token_desc)
3025 default:
3026 gcc_unreachable ();
3027 return "";
3028 case RT_CLOSE_BRACE:
3029 return "{";
3030 case RT_CLOSE_PAREN:
3031 return "(";
3035 /* Attempt to convert TOKEN_DESC from a required_token to an
3036 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
3038 static enum cpp_ttype
3039 get_required_cpp_ttype (required_token token_desc)
3041 switch (token_desc)
3043 case RT_SEMICOLON:
3044 return CPP_SEMICOLON;
3045 case RT_OPEN_PAREN:
3046 return CPP_OPEN_PAREN;
3047 case RT_CLOSE_BRACE:
3048 return CPP_CLOSE_BRACE;
3049 case RT_OPEN_BRACE:
3050 return CPP_OPEN_BRACE;
3051 case RT_CLOSE_SQUARE:
3052 return CPP_CLOSE_SQUARE;
3053 case RT_OPEN_SQUARE:
3054 return CPP_OPEN_SQUARE;
3055 case RT_COMMA:
3056 return CPP_COMMA;
3057 case RT_COLON:
3058 return CPP_COLON;
3059 case RT_CLOSE_PAREN:
3060 return CPP_CLOSE_PAREN;
3062 default:
3063 /* Use CPP_EOF as a "no completions possible" code. */
3064 return CPP_EOF;
3069 /* Subroutine of cp_parser_error and cp_parser_required_error.
3071 Issue a diagnostic of the form
3072 FILE:LINE: MESSAGE before TOKEN
3073 where TOKEN is the next token in the input stream. MESSAGE
3074 (specified by the caller) is usually of the form "expected
3075 OTHER-TOKEN".
3077 This bypasses the check for tentative passing, and potentially
3078 adds material needed by cp_parser_required_error.
3080 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
3081 suggesting insertion of the missing token.
3083 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
3084 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
3085 location. */
3087 static void
3088 cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
3089 required_token missing_token_desc,
3090 location_t matching_location)
3092 cp_token *token = cp_lexer_peek_token (parser->lexer);
3093 /* This diagnostic makes more sense if it is tagged to the line
3094 of the token we just peeked at. */
3095 cp_lexer_set_source_position_from_token (token);
3097 if (token->type == CPP_PRAGMA)
3099 error_at (token->location,
3100 "%<#pragma%> is not allowed here");
3101 cp_parser_skip_to_pragma_eol (parser, token);
3102 return;
3105 /* If this is actually a conflict marker, report it as such. */
3106 if (token->type == CPP_LSHIFT
3107 || token->type == CPP_RSHIFT
3108 || token->type == CPP_EQ_EQ)
3110 location_t loc;
3111 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
3113 error_at (loc, "version control conflict marker in file");
3114 expanded_location token_exploc = expand_location (token->location);
3115 /* Consume tokens until the end of the source line. */
3116 for (;;)
3118 cp_lexer_consume_token (parser->lexer);
3119 cp_token *next = cp_lexer_peek_token (parser->lexer);
3120 if (next->type == CPP_EOF)
3121 break;
3122 if (next->location == UNKNOWN_LOCATION
3123 || loc == UNKNOWN_LOCATION)
3124 break;
3126 expanded_location next_exploc = expand_location (next->location);
3127 if (next_exploc.file != token_exploc.file)
3128 break;
3129 if (next_exploc.line != token_exploc.line)
3130 break;
3132 return;
3136 auto_diagnostic_group d;
3137 gcc_rich_location richloc (input_location);
3139 bool added_matching_location = false;
3141 if (missing_token_desc != RT_NONE)
3142 if (cp_token *prev_token = cp_lexer_safe_previous_token (parser->lexer))
3144 /* Potentially supply a fix-it hint, suggesting to add the
3145 missing token immediately after the *previous* token.
3146 This may move the primary location within richloc. */
3147 enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
3148 location_t prev_token_loc = prev_token->location;
3149 maybe_suggest_missing_token_insertion (&richloc, ttype,
3150 prev_token_loc);
3152 /* If matching_location != UNKNOWN_LOCATION, highlight it.
3153 Attempt to consolidate diagnostics by printing it as a
3154 secondary range within the main diagnostic. */
3155 if (matching_location != UNKNOWN_LOCATION)
3156 added_matching_location
3157 = richloc.add_location_if_nearby (matching_location);
3160 /* If we were parsing a string-literal and there is an unknown name
3161 token right after, then check to see if that could also have been
3162 a literal string by checking the name against a list of known
3163 standard string literal constants defined in header files. If
3164 there is one, then add that as an hint to the error message. */
3165 name_hint h;
3166 if (token->type == CPP_NAME)
3167 if (cp_token *prev_token = cp_lexer_safe_previous_token (parser->lexer))
3168 if (cp_parser_is_string_literal (prev_token))
3170 tree name = token->u.value;
3171 const char *token_name = IDENTIFIER_POINTER (name);
3172 const char *header_hint
3173 = get_cp_stdlib_header_for_string_macro_name (token_name);
3174 if (header_hint != NULL)
3175 h = name_hint (NULL, new suggest_missing_header (token->location,
3176 token_name,
3177 header_hint));
3180 /* Actually emit the error. */
3181 c_parse_error (gmsgid,
3182 /* Because c_parser_error does not understand
3183 CPP_KEYWORD, keywords are treated like
3184 identifiers. */
3185 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
3186 token->u.value, token->flags, &richloc);
3188 if (missing_token_desc != RT_NONE)
3190 /* If we weren't able to consolidate matching_location, then
3191 print it as a secondary diagnostic. */
3192 if (matching_location != UNKNOWN_LOCATION
3193 && !added_matching_location)
3194 inform (matching_location, "to match this %qs",
3195 get_matching_symbol (missing_token_desc));
3199 /* If not parsing tentatively, 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 static void
3206 cp_parser_error (cp_parser* parser, const char* gmsgid)
3208 if (!cp_parser_simulate_error (parser))
3209 cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
3212 /* Issue an error about name-lookup failing. NAME is the
3213 IDENTIFIER_NODE DECL is the result of
3214 the lookup (as returned from cp_parser_lookup_name). DESIRED is
3215 the thing that we hoped to find. */
3217 static void
3218 cp_parser_name_lookup_error (cp_parser* parser,
3219 tree name,
3220 tree decl,
3221 name_lookup_error desired,
3222 location_t location)
3224 /* If name lookup completely failed, tell the user that NAME was not
3225 declared. */
3226 if (decl == error_mark_node)
3228 if (parser->scope && parser->scope != global_namespace)
3229 error_at (location, "%<%E::%E%> has not been declared",
3230 parser->scope, name);
3231 else if (parser->scope == global_namespace)
3232 error_at (location, "%<::%E%> has not been declared", name);
3233 else if (parser->object_scope
3234 && !CLASS_TYPE_P (parser->object_scope))
3235 error_at (location, "request for member %qE in non-class type %qT",
3236 name, parser->object_scope);
3237 else if (parser->object_scope)
3238 error_at (location, "%<%T::%E%> has not been declared",
3239 parser->object_scope, name);
3240 else
3241 error_at (location, "%qE has not been declared", name);
3243 else if (parser->scope && parser->scope != global_namespace)
3245 switch (desired)
3247 case NLE_TYPE:
3248 error_at (location, "%<%E::%E%> is not a type",
3249 parser->scope, name);
3250 break;
3251 case NLE_CXX98:
3252 error_at (location, "%<%E::%E%> is not a class or namespace",
3253 parser->scope, name);
3254 break;
3255 case NLE_NOT_CXX98:
3256 error_at (location,
3257 "%<%E::%E%> is not a class, namespace, or enumeration",
3258 parser->scope, name);
3259 break;
3260 default:
3261 gcc_unreachable ();
3265 else if (parser->scope == global_namespace)
3267 switch (desired)
3269 case NLE_TYPE:
3270 error_at (location, "%<::%E%> is not a type", name);
3271 break;
3272 case NLE_CXX98:
3273 error_at (location, "%<::%E%> is not a class or namespace", name);
3274 break;
3275 case NLE_NOT_CXX98:
3276 error_at (location,
3277 "%<::%E%> is not a class, namespace, or enumeration",
3278 name);
3279 break;
3280 default:
3281 gcc_unreachable ();
3284 else
3286 switch (desired)
3288 case NLE_TYPE:
3289 error_at (location, "%qE is not a type", name);
3290 break;
3291 case NLE_CXX98:
3292 error_at (location, "%qE is not a class or namespace", name);
3293 break;
3294 case NLE_NOT_CXX98:
3295 error_at (location,
3296 "%qE is not a class, namespace, or enumeration", name);
3297 break;
3298 default:
3299 gcc_unreachable ();
3304 /* If we are parsing tentatively, remember that an error has occurred
3305 during this tentative parse. Returns true if the error was
3306 simulated; false if a message should be issued by the caller. */
3308 static bool
3309 cp_parser_simulate_error (cp_parser* parser)
3311 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3313 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3314 return true;
3316 return false;
3319 /* This function is called when a type is defined. If type
3320 definitions are forbidden at this point, an error message is
3321 issued. */
3323 static bool
3324 cp_parser_check_type_definition (cp_parser* parser)
3326 /* If types are forbidden here, issue a message. */
3327 if (parser->type_definition_forbidden_message)
3329 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3330 or %qs in the message need to be interpreted. */
3331 error (parser->type_definition_forbidden_message,
3332 parser->type_definition_forbidden_message_arg);
3333 return false;
3335 return true;
3338 /* This function is called when the DECLARATOR is processed. The TYPE
3339 was a type defined in the decl-specifiers. If it is invalid to
3340 define a type in the decl-specifiers for DECLARATOR, an error is
3341 issued. TYPE_LOCATION is the location of TYPE and is used
3342 for error reporting. */
3344 static void
3345 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3346 tree type, location_t type_location)
3348 /* [dcl.fct] forbids type definitions in return types.
3349 Unfortunately, it's not easy to know whether or not we are
3350 processing a return type until after the fact. */
3351 while (declarator
3352 && (declarator->kind == cdk_pointer
3353 || declarator->kind == cdk_reference
3354 || declarator->kind == cdk_ptrmem))
3355 declarator = declarator->declarator;
3356 if (declarator
3357 && declarator->kind == cdk_function)
3359 error_at (type_location,
3360 "new types may not be defined in a return type");
3361 inform (type_location,
3362 "(perhaps a semicolon is missing after the definition of %qT)",
3363 type);
3367 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3368 "<" in any valid C++ program. If the next token is indeed "<",
3369 issue a message warning the user about what appears to be an
3370 invalid attempt to form a template-id. LOCATION is the location
3371 of the type-specifier (TYPE) */
3373 static void
3374 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3375 tree type,
3376 enum tag_types tag_type,
3377 location_t location)
3379 cp_token_position start = 0;
3381 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3383 if (TREE_CODE (type) == TYPE_DECL)
3384 type = TREE_TYPE (type);
3385 if (TYPE_P (type) && !template_placeholder_p (type))
3386 error_at (location, "%qT is not a template", type);
3387 else if (identifier_p (type))
3389 if (tag_type != none_type)
3390 error_at (location, "%qE is not a class template", type);
3391 else
3392 error_at (location, "%qE is not a template", type);
3394 else
3395 error_at (location, "invalid template-id");
3396 /* Remember the location of the invalid "<". */
3397 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3398 start = cp_lexer_token_position (parser->lexer, true);
3399 /* Consume the "<". */
3400 cp_lexer_consume_token (parser->lexer);
3401 /* Parse the template arguments. */
3402 cp_parser_enclosed_template_argument_list (parser);
3403 /* Permanently remove the invalid template arguments so that
3404 this error message is not issued again. */
3405 if (start)
3406 cp_lexer_purge_tokens_after (parser->lexer, start);
3410 /* If parsing an integral constant-expression, issue an error message
3411 about the fact that THING appeared and return true. Otherwise,
3412 return false. In either case, set
3413 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3415 static bool
3416 cp_parser_non_integral_constant_expression (cp_parser *parser,
3417 non_integral_constant thing)
3419 parser->non_integral_constant_expression_p = true;
3420 if (parser->integral_constant_expression_p)
3422 if (!parser->allow_non_integral_constant_expression_p)
3424 const char *msg = NULL;
3425 switch (thing)
3427 case NIC_FLOAT:
3428 pedwarn (input_location, OPT_Wpedantic,
3429 "ISO C++ forbids using a floating-point literal "
3430 "in a constant-expression");
3431 return true;
3432 case NIC_CAST:
3433 error ("a cast to a type other than an integral or "
3434 "enumeration type cannot appear in a "
3435 "constant-expression");
3436 return true;
3437 case NIC_TYPEID:
3438 error ("%<typeid%> operator "
3439 "cannot appear in a constant-expression");
3440 return true;
3441 case NIC_NCC:
3442 error ("non-constant compound literals "
3443 "cannot appear in a constant-expression");
3444 return true;
3445 case NIC_FUNC_CALL:
3446 error ("a function call "
3447 "cannot appear in a constant-expression");
3448 return true;
3449 case NIC_INC:
3450 error ("an increment "
3451 "cannot appear in a constant-expression");
3452 return true;
3453 case NIC_DEC:
3454 error ("an decrement "
3455 "cannot appear in a constant-expression");
3456 return true;
3457 case NIC_ARRAY_REF:
3458 error ("an array reference "
3459 "cannot appear in a constant-expression");
3460 return true;
3461 case NIC_ADDR_LABEL:
3462 error ("the address of a label "
3463 "cannot appear in a constant-expression");
3464 return true;
3465 case NIC_OVERLOADED:
3466 error ("calls to overloaded operators "
3467 "cannot appear in a constant-expression");
3468 return true;
3469 case NIC_ASSIGNMENT:
3470 error ("an assignment cannot appear in a constant-expression");
3471 return true;
3472 case NIC_COMMA:
3473 error ("a comma operator "
3474 "cannot appear in a constant-expression");
3475 return true;
3476 case NIC_CONSTRUCTOR:
3477 error ("a call to a constructor "
3478 "cannot appear in a constant-expression");
3479 return true;
3480 case NIC_TRANSACTION:
3481 error ("a transaction expression "
3482 "cannot appear in a constant-expression");
3483 return true;
3484 case NIC_THIS:
3485 msg = "this";
3486 break;
3487 case NIC_FUNC_NAME:
3488 msg = "__FUNCTION__";
3489 break;
3490 case NIC_PRETTY_FUNC:
3491 msg = "__PRETTY_FUNCTION__";
3492 break;
3493 case NIC_C99_FUNC:
3494 msg = "__func__";
3495 break;
3496 case NIC_VA_ARG:
3497 msg = "va_arg";
3498 break;
3499 case NIC_ARROW:
3500 msg = "->";
3501 break;
3502 case NIC_POINT:
3503 msg = ".";
3504 break;
3505 case NIC_STAR:
3506 msg = "*";
3507 break;
3508 case NIC_ADDR:
3509 msg = "&";
3510 break;
3511 case NIC_PREINCREMENT:
3512 msg = "++";
3513 break;
3514 case NIC_PREDECREMENT:
3515 msg = "--";
3516 break;
3517 case NIC_NEW:
3518 msg = "new";
3519 break;
3520 case NIC_DEL:
3521 msg = "delete";
3522 break;
3523 default:
3524 gcc_unreachable ();
3526 if (msg)
3527 error ("%qs cannot appear in a constant-expression", msg);
3528 return true;
3531 return false;
3534 /* Emit a diagnostic for an invalid type name. This function commits
3535 to the current active tentative parse, if any. (Otherwise, the
3536 problematic construct might be encountered again later, resulting
3537 in duplicate error messages.) LOCATION is the location of ID. */
3539 static void
3540 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3541 location_t location)
3543 tree decl, ambiguous_decls;
3544 cp_parser_commit_to_tentative_parse (parser);
3545 /* Try to lookup the identifier. */
3546 decl = cp_parser_lookup_name (parser, id, none_type,
3547 /*is_template=*/false,
3548 /*is_namespace=*/false,
3549 /*check_dependency=*/true,
3550 &ambiguous_decls, location);
3551 if (ambiguous_decls)
3552 /* If the lookup was ambiguous, an error will already have
3553 been issued. */
3554 return;
3555 /* If the lookup found a template-name, it means that the user forgot
3556 to specify an argument list. Emit a useful error message. */
3557 if (DECL_TYPE_TEMPLATE_P (decl))
3559 auto_diagnostic_group d;
3560 error_at (location,
3561 "invalid use of template-name %qE without an argument list",
3562 decl);
3563 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3564 inform (location, "class template argument deduction is only available "
3565 "with %<-std=c++17%> or %<-std=gnu++17%>");
3566 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3568 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3569 error_at (location, "invalid use of destructor %qD as a type", id);
3570 else if (TREE_CODE (decl) == TYPE_DECL)
3571 /* Something like 'unsigned A a;' */
3572 error_at (location, "invalid combination of multiple type-specifiers");
3573 else if (!parser->scope)
3575 /* Issue an error message. */
3576 auto_diagnostic_group d;
3577 name_hint hint;
3578 if (TREE_CODE (id) == IDENTIFIER_NODE)
3579 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3580 if (const char *suggestion = hint.suggestion ())
3582 gcc_rich_location richloc (location);
3583 richloc.add_fixit_replace (suggestion);
3584 error_at (&richloc,
3585 "%qE does not name a type; did you mean %qs?",
3586 id, suggestion);
3588 else
3589 error_at (location, "%qE does not name a type", id);
3590 /* If we're in a template class, it's possible that the user was
3591 referring to a type from a base class. For example:
3593 template <typename T> struct A { typedef T X; };
3594 template <typename T> struct B : public A<T> { X x; };
3596 The user should have said "typename A<T>::X". */
3597 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3598 inform (location, "C++11 %<constexpr%> only available with "
3599 "%<-std=c++11%> or %<-std=gnu++11%>");
3600 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3601 inform (location, "C++11 %<noexcept%> only available with "
3602 "%<-std=c++11%> or %<-std=gnu++11%>");
3603 else if (TREE_CODE (id) == IDENTIFIER_NODE
3604 && (id_equal (id, "module") || id_equal (id, "import")))
3606 if (modules_p ())
3607 inform (location, "%qE is not recognized as a module control-line",
3608 id);
3609 else if (cxx_dialect < cxx20)
3610 inform (location, "C++20 %qE only available with %<-fmodules-ts%>",
3611 id);
3612 else
3613 inform (location, "C++20 %qE only available with %<-fmodules-ts%>"
3614 ", which is not yet enabled with %<-std=c++20%>", id);
3616 else if (cxx_dialect < cxx11
3617 && TREE_CODE (id) == IDENTIFIER_NODE
3618 && id_equal (id, "thread_local"))
3619 inform (location, "C++11 %<thread_local%> only available with "
3620 "%<-std=c++11%> or %<-std=gnu++11%>");
3621 else if (cxx_dialect < cxx20 && id == ridpointers[(int)RID_CONSTINIT])
3622 inform (location, "C++20 %<constinit%> only available with "
3623 "%<-std=c++20%> or %<-std=gnu++20%>");
3624 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3625 inform (location, "%<concept%> only available with %<-std=c++20%> or "
3626 "%<-fconcepts%>");
3627 else if (!flag_concepts && id == ridpointers[(int)RID_REQUIRES])
3628 inform (location, "%<requires%> only available with %<-std=c++20%> or "
3629 "%<-fconcepts%>");
3630 else if (processing_template_decl && current_class_type
3631 && TYPE_BINFO (current_class_type))
3633 for (tree b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3634 b; b = TREE_CHAIN (b))
3636 tree base_type = BINFO_TYPE (b);
3637 if (CLASS_TYPE_P (base_type)
3638 && dependent_type_p (base_type))
3640 /* Go from a particular instantiation of the
3641 template (which will have an empty TYPE_FIELDs),
3642 to the main version. */
3643 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3644 for (tree field = TYPE_FIELDS (base_type);
3645 field; field = DECL_CHAIN (field))
3646 if (TREE_CODE (field) == TYPE_DECL
3647 && DECL_NAME (field) == id)
3649 inform (location,
3650 "(perhaps %<typename %T::%E%> was intended)",
3651 BINFO_TYPE (b), id);
3652 goto found;
3656 found:;
3659 /* Here we diagnose qualified-ids where the scope is actually correct,
3660 but the identifier does not resolve to a valid type name. */
3661 else if (parser->scope != error_mark_node)
3663 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3665 auto_diagnostic_group d;
3666 name_hint hint;
3667 if (decl == error_mark_node)
3668 hint = suggest_alternative_in_explicit_scope (location, id,
3669 parser->scope);
3670 const char *suggestion = hint.suggestion ();
3671 gcc_rich_location richloc (location_of (id));
3672 if (suggestion)
3673 richloc.add_fixit_replace (suggestion);
3674 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3676 if (suggestion)
3677 error_at (&richloc,
3678 "%qE in namespace %qE does not name a template"
3679 " type; did you mean %qs?",
3680 id, parser->scope, suggestion);
3681 else
3682 error_at (&richloc,
3683 "%qE in namespace %qE does not name a template type",
3684 id, parser->scope);
3686 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3688 if (suggestion)
3689 error_at (&richloc,
3690 "%qE in namespace %qE does not name a template"
3691 " type; did you mean %qs?",
3692 TREE_OPERAND (id, 0), parser->scope, suggestion);
3693 else
3694 error_at (&richloc,
3695 "%qE in namespace %qE does not name a template"
3696 " type",
3697 TREE_OPERAND (id, 0), parser->scope);
3699 else
3701 if (suggestion)
3702 error_at (&richloc,
3703 "%qE in namespace %qE does not name a type"
3704 "; did you mean %qs?",
3705 id, parser->scope, suggestion);
3706 else
3707 error_at (&richloc,
3708 "%qE in namespace %qE does not name a type",
3709 id, parser->scope);
3711 if (DECL_P (decl))
3712 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3714 else if (CLASS_TYPE_P (parser->scope)
3715 && constructor_name_p (id, parser->scope))
3717 /* A<T>::A<T>() */
3718 auto_diagnostic_group d;
3719 error_at (location, "%<%T::%E%> names the constructor, not"
3720 " the type", parser->scope, id);
3721 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3722 error_at (location, "and %qT has no template constructors",
3723 parser->scope);
3725 else if (TYPE_P (parser->scope)
3726 && dependent_scope_p (parser->scope))
3728 gcc_rich_location richloc (location);
3729 richloc.add_fixit_insert_before ("typename ");
3730 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3731 error_at (&richloc,
3732 "need %<typename%> before %<%T::%D::%E%> because "
3733 "%<%T::%D%> is a dependent scope",
3734 TYPE_CONTEXT (parser->scope),
3735 TYPENAME_TYPE_FULLNAME (parser->scope),
3737 TYPE_CONTEXT (parser->scope),
3738 TYPENAME_TYPE_FULLNAME (parser->scope));
3739 else
3740 error_at (&richloc, "need %<typename%> before %<%T::%E%> because "
3741 "%qT is a dependent scope",
3742 parser->scope, id, parser->scope);
3744 else if (TYPE_P (parser->scope))
3746 auto_diagnostic_group d;
3747 if (!COMPLETE_TYPE_P (parser->scope))
3748 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3749 parser->scope);
3750 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3751 error_at (location_of (id),
3752 "%qE in %q#T does not name a template type",
3753 id, parser->scope);
3754 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3755 error_at (location_of (id),
3756 "%qE in %q#T does not name a template type",
3757 TREE_OPERAND (id, 0), parser->scope);
3758 else
3759 error_at (location_of (id),
3760 "%qE in %q#T does not name a type",
3761 id, parser->scope);
3762 if (DECL_P (decl))
3763 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3765 else
3766 gcc_unreachable ();
3770 /* Check for a common situation where a type-name should be present,
3771 but is not, and issue a sensible error message. Returns true if an
3772 invalid type-name was detected.
3774 The situation handled by this function are variable declarations of the
3775 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3776 Usually, `ID' should name a type, but if we got here it means that it
3777 does not. We try to emit the best possible error message depending on
3778 how exactly the id-expression looks like. */
3780 static bool
3781 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3783 tree id;
3784 cp_token *token = cp_lexer_peek_token (parser->lexer);
3786 /* Avoid duplicate error about ambiguous lookup. */
3787 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3789 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3790 if (next->type == CPP_NAME && next->error_reported)
3791 goto out;
3794 cp_parser_parse_tentatively (parser);
3795 id = cp_parser_id_expression (parser,
3796 /*template_keyword_p=*/false,
3797 /*check_dependency_p=*/true,
3798 /*template_p=*/NULL,
3799 /*declarator_p=*/true,
3800 /*optional_p=*/false);
3801 /* If the next token is a (, this is a function with no explicit return
3802 type, i.e. constructor, destructor or conversion op. */
3803 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3804 || TREE_CODE (id) == TYPE_DECL)
3806 cp_parser_abort_tentative_parse (parser);
3807 return false;
3809 if (!cp_parser_parse_definitely (parser))
3810 return false;
3812 /* Emit a diagnostic for the invalid type. */
3813 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3814 out:
3815 /* If we aren't in the middle of a declarator (i.e. in a
3816 parameter-declaration-clause), skip to the end of the declaration;
3817 there's no point in trying to process it. */
3818 if (!parser->in_declarator_p)
3819 cp_parser_skip_to_end_of_block_or_statement (parser);
3820 return true;
3823 /* Consume tokens up to, and including, the next non-nested closing `)'.
3824 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3825 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3826 found an unnested token of that type. */
3828 static int
3829 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3830 bool recovering,
3831 cpp_ttype or_ttype,
3832 bool consume_paren)
3834 unsigned paren_depth = 0;
3835 unsigned brace_depth = 0;
3836 unsigned square_depth = 0;
3837 unsigned condop_depth = 0;
3839 if (recovering && or_ttype == CPP_EOF
3840 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3841 return 0;
3843 while (true)
3845 cp_token * token = cp_lexer_peek_token (parser->lexer);
3847 /* Have we found what we're looking for before the closing paren? */
3848 if (token->type == or_ttype && or_ttype != CPP_EOF
3849 && !brace_depth && !paren_depth && !square_depth && !condop_depth)
3850 return -1;
3852 switch (token->type)
3854 case CPP_PRAGMA_EOL:
3855 if (!parser->lexer->in_pragma)
3856 break;
3857 /* FALLTHRU */
3858 case CPP_EOF:
3859 /* If we've run out of tokens, then there is no closing `)'. */
3860 return 0;
3862 /* This is good for lambda expression capture-lists. */
3863 case CPP_OPEN_SQUARE:
3864 ++square_depth;
3865 break;
3866 case CPP_CLOSE_SQUARE:
3867 if (!square_depth--)
3868 return 0;
3869 break;
3871 case CPP_SEMICOLON:
3872 /* This matches the processing in skip_to_end_of_statement. */
3873 if (!brace_depth)
3874 return 0;
3875 break;
3877 case CPP_OPEN_BRACE:
3878 ++brace_depth;
3879 break;
3880 case CPP_CLOSE_BRACE:
3881 if (!brace_depth--)
3882 return 0;
3883 break;
3885 case CPP_OPEN_PAREN:
3886 if (!brace_depth)
3887 ++paren_depth;
3888 break;
3890 case CPP_CLOSE_PAREN:
3891 if (!brace_depth && !paren_depth--)
3893 if (consume_paren)
3894 cp_lexer_consume_token (parser->lexer);
3895 return 1;
3897 break;
3899 case CPP_QUERY:
3900 if (!brace_depth && !paren_depth && !square_depth)
3901 ++condop_depth;
3902 break;
3904 case CPP_COLON:
3905 if (!brace_depth && !paren_depth && !square_depth && condop_depth > 0)
3906 condop_depth--;
3907 break;
3909 case CPP_KEYWORD:
3910 if (!cp_token_is_module_directive (token))
3911 break;
3912 /* FALLTHROUGH */
3914 case CPP_PRAGMA:
3915 /* We fell into a pragma. Skip it, and continue. */
3916 cp_parser_skip_to_pragma_eol (parser, recovering ? token : nullptr);
3917 continue;
3919 default:
3920 break;
3923 /* Consume the token. */
3924 cp_lexer_consume_token (parser->lexer);
3928 /* Consume tokens up to, and including, the next non-nested closing `)'.
3929 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3930 are doing error recovery. Returns -1 if OR_COMMA is true and we
3931 found an unnested token of that type. */
3933 static int
3934 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3935 bool recovering,
3936 bool or_comma,
3937 bool consume_paren)
3939 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3940 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3941 ttype, consume_paren);
3944 /* Consume tokens until we reach the end of the current statement.
3945 Normally, that will be just before consuming a `;'. However, if a
3946 non-nested `}' comes first, then we stop before consuming that. */
3948 static void
3949 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3951 unsigned nesting_depth = 0;
3953 /* Unwind generic function template scope if necessary. */
3954 if (parser->fully_implicit_function_template_p)
3955 abort_fully_implicit_template (parser);
3957 while (true)
3959 cp_token *token = cp_lexer_peek_token (parser->lexer);
3961 switch (token->type)
3963 case CPP_PRAGMA_EOL:
3964 if (!parser->lexer->in_pragma)
3965 break;
3966 /* FALLTHRU */
3967 case CPP_EOF:
3968 /* If we've run out of tokens, stop. */
3969 return;
3971 case CPP_SEMICOLON:
3972 /* If the next token is a `;', we have reached the end of the
3973 statement. */
3974 if (!nesting_depth)
3975 return;
3976 break;
3978 case CPP_CLOSE_BRACE:
3979 /* If this is a non-nested '}', stop before consuming it.
3980 That way, when confronted with something like:
3982 { 3 + }
3984 we stop before consuming the closing '}', even though we
3985 have not yet reached a `;'. */
3986 if (nesting_depth == 0)
3987 return;
3989 /* If it is the closing '}' for a block that we have
3990 scanned, stop -- but only after consuming the token.
3991 That way given:
3993 void f g () { ... }
3994 typedef int I;
3996 we will stop after the body of the erroneously declared
3997 function, but before consuming the following `typedef'
3998 declaration. */
3999 if (--nesting_depth == 0)
4001 cp_lexer_consume_token (parser->lexer);
4002 return;
4004 break;
4006 case CPP_OPEN_BRACE:
4007 ++nesting_depth;
4008 break;
4010 case CPP_KEYWORD:
4011 if (!cp_token_is_module_directive (token))
4012 break;
4013 /* FALLTHROUGH */
4015 case CPP_PRAGMA:
4016 /* We fell into a pragma. Skip it, and continue or return. */
4017 cp_parser_skip_to_pragma_eol (parser, token);
4018 if (!nesting_depth)
4019 return;
4020 continue;
4022 default:
4023 break;
4026 /* Consume the token. */
4027 cp_lexer_consume_token (parser->lexer);
4031 /* This function is called at the end of a statement or declaration.
4032 If the next token is a semicolon, it is consumed; otherwise, error
4033 recovery is attempted. */
4035 static void
4036 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
4038 /* Look for the trailing `;'. */
4039 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
4041 /* If there is additional (erroneous) input, skip to the end of
4042 the statement. */
4043 cp_parser_skip_to_end_of_statement (parser);
4044 /* If the next token is now a `;', consume it. */
4045 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
4046 cp_lexer_consume_token (parser->lexer);
4050 /* Skip tokens until we have consumed an entire block, or until we
4051 have consumed a non-nested `;'. */
4053 static void
4054 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
4056 int nesting_depth = 0;
4058 /* Unwind generic function template scope if necessary. */
4059 if (parser->fully_implicit_function_template_p)
4060 abort_fully_implicit_template (parser);
4062 while (nesting_depth >= 0)
4064 cp_token *token = cp_lexer_peek_token (parser->lexer);
4066 switch (token->type)
4068 case CPP_PRAGMA_EOL:
4069 if (!parser->lexer->in_pragma)
4070 break;
4071 /* FALLTHRU */
4072 case CPP_EOF:
4073 /* If we've run out of tokens, stop. */
4074 return;
4076 case CPP_SEMICOLON:
4077 /* Stop if this is an unnested ';'. */
4078 if (!nesting_depth)
4079 nesting_depth = -1;
4080 break;
4082 case CPP_CLOSE_BRACE:
4083 /* Stop if this is an unnested '}', or closes the outermost
4084 nesting level. */
4085 nesting_depth--;
4086 if (nesting_depth < 0)
4087 return;
4088 if (!nesting_depth)
4089 nesting_depth = -1;
4090 break;
4092 case CPP_OPEN_BRACE:
4093 /* Nest. */
4094 nesting_depth++;
4095 break;
4097 case CPP_KEYWORD:
4098 if (!cp_token_is_module_directive (token))
4099 break;
4100 /* FALLTHROUGH */
4102 case CPP_PRAGMA:
4103 /* Skip it, and continue or return. */
4104 cp_parser_skip_to_pragma_eol (parser, token);
4105 if (!nesting_depth)
4106 return;
4107 continue;
4109 default:
4110 break;
4113 /* Consume the token. */
4114 cp_lexer_consume_token (parser->lexer);
4118 /* Skip tokens until a non-nested closing curly brace is the next
4119 token, or there are no more tokens. Return true in the first case,
4120 false otherwise. */
4122 static bool
4123 cp_parser_skip_to_closing_brace (cp_parser *parser)
4125 unsigned nesting_depth = 0;
4127 while (true)
4129 cp_token *token = cp_lexer_peek_token (parser->lexer);
4131 switch (token->type)
4133 case CPP_PRAGMA_EOL:
4134 if (!parser->lexer->in_pragma)
4135 break;
4136 /* FALLTHRU */
4137 case CPP_EOF:
4138 /* If we've run out of tokens, stop. */
4139 return false;
4141 case CPP_CLOSE_BRACE:
4142 /* If the next token is a non-nested `}', then we have reached
4143 the end of the current block. */
4144 if (nesting_depth-- == 0)
4145 return true;
4146 break;
4148 case CPP_OPEN_BRACE:
4149 /* If it the next token is a `{', then we are entering a new
4150 block. Consume the entire block. */
4151 ++nesting_depth;
4152 break;
4154 default:
4155 break;
4158 /* Consume the token. */
4159 cp_lexer_consume_token (parser->lexer);
4163 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
4164 parameter is the PRAGMA token, allowing us to purge the entire pragma
4165 sequence. PRAGMA_TOK can be NULL, if we're speculatively scanning
4166 forwards (not error recovery). */
4168 static void
4169 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
4171 cp_token *token;
4175 /* The preprocessor makes sure that a PRAGMA_EOL token appears
4176 before an EOF token, even when the EOF is on the pragma line.
4177 We should never get here without being inside a deferred
4178 pragma. */
4179 gcc_checking_assert (cp_lexer_next_token_is_not (parser->lexer, CPP_EOF));
4180 token = cp_lexer_consume_token (parser->lexer);
4182 while (token->type != CPP_PRAGMA_EOL);
4184 if (pragma_tok)
4186 parser->lexer->in_pragma = false;
4187 if (parser->lexer->in_omp_attribute_pragma
4188 && cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4190 parser->lexer = parser->lexer->next;
4191 /* Put the current source position back where it was before this
4192 lexer was pushed. */
4193 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4198 /* Require pragma end of line, resyncing with it as necessary. The
4199 arguments are as for cp_parser_skip_to_pragma_eol. */
4201 static void
4202 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
4204 parser->lexer->in_pragma = false;
4205 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
4206 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
4207 else if (parser->lexer->in_omp_attribute_pragma
4208 && cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4210 parser->lexer = parser->lexer->next;
4211 /* Put the current source position back where it was before this
4212 lexer was pushed. */
4213 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4217 /* This is a simple wrapper around make_typename_type. When the id is
4218 an unresolved identifier node, we can provide a superior diagnostic
4219 using cp_parser_diagnose_invalid_type_name. */
4221 static tree
4222 cp_parser_make_typename_type (cp_parser *parser, tree id,
4223 location_t id_location)
4225 tree result;
4226 if (identifier_p (id))
4228 result = make_typename_type (parser->scope, id, typename_type,
4229 /*complain=*/tf_none);
4230 if (result == error_mark_node)
4231 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
4232 return result;
4234 return make_typename_type (parser->scope, id, typename_type, tf_error);
4237 /* This is a wrapper around the
4238 make_{pointer,ptrmem,reference}_declarator functions that decides
4239 which one to call based on the CODE and CLASS_TYPE arguments. The
4240 CODE argument should be one of the values returned by
4241 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
4242 appertain to the pointer or reference. */
4244 static cp_declarator *
4245 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
4246 cp_cv_quals cv_qualifiers,
4247 cp_declarator *target,
4248 tree attributes)
4250 if (code == ERROR_MARK || target == cp_error_declarator)
4251 return cp_error_declarator;
4253 if (code == INDIRECT_REF)
4254 if (class_type == NULL_TREE)
4255 return make_pointer_declarator (cv_qualifiers, target, attributes);
4256 else
4257 return make_ptrmem_declarator (cv_qualifiers, class_type,
4258 target, attributes);
4259 else if (code == ADDR_EXPR && class_type == NULL_TREE)
4260 return make_reference_declarator (cv_qualifiers, target,
4261 false, attributes);
4262 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
4263 return make_reference_declarator (cv_qualifiers, target,
4264 true, attributes);
4265 gcc_unreachable ();
4268 /* Create a new C++ parser. */
4270 static cp_parser *
4271 cp_parser_new (cp_lexer *lexer)
4273 /* Initialize the binops_by_token so that we can get the tree
4274 directly from the token. */
4275 for (unsigned i = 0; i < ARRAY_SIZE (binops); i++)
4276 binops_by_token[binops[i].token_type] = binops[i];
4278 cp_parser *parser = ggc_cleared_alloc<cp_parser> ();
4279 parser->lexer = lexer;
4280 parser->context = cp_parser_context_new (NULL);
4282 /* For now, we always accept GNU extensions. */
4283 parser->allow_gnu_extensions_p = 1;
4285 /* The `>' token is a greater-than operator, not the end of a
4286 template-id. */
4287 parser->greater_than_is_operator_p = true;
4289 parser->default_arg_ok_p = true;
4291 /* We are not parsing a constant-expression. */
4292 parser->integral_constant_expression_p = false;
4293 parser->allow_non_integral_constant_expression_p = false;
4294 parser->non_integral_constant_expression_p = false;
4296 /* Local variable names are not forbidden. */
4297 parser->local_variables_forbidden_p = 0;
4299 /* We are not processing an `extern "C"' declaration. */
4300 parser->in_unbraced_linkage_specification_p = false;
4302 /* We are not processing a declarator. */
4303 parser->in_declarator_p = false;
4305 /* We are not processing a template-argument-list. */
4306 parser->in_template_argument_list_p = false;
4308 /* We are not in an iteration statement. */
4309 parser->in_statement = 0;
4311 /* We are not in a switch statement. */
4312 parser->in_switch_statement_p = false;
4314 /* We are not parsing a type-id inside an expression. */
4315 parser->in_type_id_in_expr_p = false;
4317 /* String literals should be translated to the execution character set. */
4318 parser->translate_strings_p = true;
4320 /* We are not parsing a function body. */
4321 parser->in_function_body = false;
4323 /* We can correct until told otherwise. */
4324 parser->colon_corrects_to_scope_p = true;
4326 /* The unparsed function queue is empty. */
4327 push_unparsed_function_queues (parser);
4329 /* There are no classes being defined. */
4330 parser->num_classes_being_defined = 0;
4332 /* No template parameters apply. */
4333 parser->num_template_parameter_lists = 0;
4335 /* Special parsing data structures. */
4336 parser->omp_declare_simd = NULL;
4337 parser->oacc_routine = NULL;
4339 /* Not declaring an implicit function template. */
4340 parser->auto_is_implicit_function_template_parm_p = false;
4341 parser->fully_implicit_function_template_p = false;
4342 parser->implicit_template_parms = 0;
4343 parser->implicit_template_scope = 0;
4345 /* Allow constrained-type-specifiers. */
4346 parser->prevent_constrained_type_specifiers = 0;
4348 /* We haven't yet seen an 'extern "C"'. */
4349 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
4351 return parser;
4354 /* Create a cp_lexer structure which will emit the tokens in CACHE
4355 and push it onto the parser's lexer stack. This is used for delayed
4356 parsing of in-class method bodies and default arguments, and should
4357 not be confused with tentative parsing. */
4358 static void
4359 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
4361 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
4362 lexer->next = parser->lexer;
4363 parser->lexer = lexer;
4365 /* Move the current source position to that of the first token in the
4366 new lexer. */
4367 cp_lexer_set_source_position_from_token (lexer->next_token);
4370 /* Pop the top lexer off the parser stack. This is never used for the
4371 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
4372 static void
4373 cp_parser_pop_lexer (cp_parser *parser)
4375 cp_lexer *lexer = parser->lexer;
4376 parser->lexer = lexer->next;
4377 cp_lexer_destroy (lexer);
4379 /* Put the current source position back where it was before this
4380 lexer was pushed. */
4381 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4384 /* Lexical conventions [gram.lex] */
4386 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
4387 identifier. */
4389 static cp_expr
4390 cp_parser_identifier (cp_parser* parser)
4392 cp_token *token;
4394 /* Look for the identifier. */
4395 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
4396 /* Return the value. */
4397 if (token)
4398 return cp_expr (token->u.value, token->location);
4399 else
4400 return error_mark_node;
4403 /* Worker for cp_parser_string_literal and cp_parser_userdef_string_literal.
4404 Do not call this directly; use either of the above.
4406 Parse a sequence of adjacent string constants. Return a
4407 TREE_STRING representing the combined, nul-terminated string
4408 constant. If TRANSLATE is true, translate the string to the
4409 execution character set. If WIDE_OK is true, a wide string is
4410 valid here. If UDL_OK is true, a string literal with user-defined
4411 suffix can be used in this context.
4413 C++98 [lex.string] says that if a narrow string literal token is
4414 adjacent to a wide string literal token, the behavior is undefined.
4415 However, C99 6.4.5p4 says that this results in a wide string literal.
4416 We follow C99 here, for consistency with the C front end.
4418 This code is largely lifted from lex_string() in c-lex.cc.
4420 FUTURE: ObjC++ will need to handle @-strings here. */
4422 static cp_expr
4423 cp_parser_string_literal_common (cp_parser *parser, bool translate,
4424 bool wide_ok, bool udl_ok,
4425 bool lookup_udlit)
4427 tree value;
4428 size_t count;
4429 struct obstack str_ob;
4430 struct obstack loc_ob;
4431 cpp_string str, istr, *strs;
4432 cp_token *tok;
4433 enum cpp_ttype type, curr_type;
4434 int have_suffix_p = 0;
4435 tree string_tree;
4436 tree suffix_id = NULL_TREE;
4437 bool curr_tok_is_userdef_p = false;
4439 tok = cp_lexer_peek_token (parser->lexer);
4440 if (!cp_parser_is_string_literal (tok))
4442 cp_parser_error (parser, "expected string-literal");
4443 return error_mark_node;
4446 location_t loc = tok->location;
4448 if (cpp_userdef_string_p (tok->type))
4450 if (!udl_ok)
4452 error_at (loc, "string literal with user-defined suffix "
4453 "is invalid in this context");
4454 return error_mark_node;
4456 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4457 curr_type = cpp_userdef_string_remove_type (tok->type);
4458 curr_tok_is_userdef_p = true;
4460 else
4462 string_tree = tok->u.value;
4463 curr_type = tok->type;
4465 type = curr_type;
4467 /* Try to avoid the overhead of creating and destroying an obstack
4468 for the common case of just one string. */
4469 if (!cp_parser_is_string_literal
4470 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4472 cp_lexer_consume_token (parser->lexer);
4474 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4475 str.len = TREE_STRING_LENGTH (string_tree);
4476 count = 1;
4478 if (curr_tok_is_userdef_p)
4480 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4481 have_suffix_p = 1;
4482 curr_type = cpp_userdef_string_remove_type (tok->type);
4484 else
4485 curr_type = tok->type;
4487 strs = &str;
4489 else
4491 location_t last_tok_loc = tok->location;
4492 gcc_obstack_init (&str_ob);
4493 gcc_obstack_init (&loc_ob);
4494 count = 0;
4498 cp_lexer_consume_token (parser->lexer);
4499 count++;
4500 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4501 str.len = TREE_STRING_LENGTH (string_tree);
4503 if (curr_tok_is_userdef_p)
4505 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4506 if (have_suffix_p == 0)
4508 suffix_id = curr_suffix_id;
4509 have_suffix_p = 1;
4511 else if (have_suffix_p == 1
4512 && curr_suffix_id != suffix_id)
4514 error ("inconsistent user-defined literal suffixes"
4515 " %qD and %qD in string literal",
4516 suffix_id, curr_suffix_id);
4517 have_suffix_p = -1;
4519 curr_type = cpp_userdef_string_remove_type (tok->type);
4521 else
4522 curr_type = tok->type;
4524 if (type != curr_type)
4526 if (type == CPP_STRING)
4527 type = curr_type;
4528 else if (curr_type != CPP_STRING)
4530 rich_location rich_loc (line_table, tok->location);
4531 rich_loc.add_range (last_tok_loc);
4532 error_at (&rich_loc,
4533 "concatenation of string literals with "
4534 "conflicting encoding prefixes");
4538 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4539 obstack_grow (&loc_ob, &tok->location, sizeof (location_t));
4541 last_tok_loc = tok->location;
4543 tok = cp_lexer_peek_token (parser->lexer);
4544 if (cpp_userdef_string_p (tok->type))
4546 if (!udl_ok)
4548 error_at (loc, "string literal with user-defined suffix "
4549 "is invalid in this context");
4550 return error_mark_node;
4552 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4553 curr_type = cpp_userdef_string_remove_type (tok->type);
4554 curr_tok_is_userdef_p = true;
4556 else
4558 string_tree = tok->u.value;
4559 curr_type = tok->type;
4560 curr_tok_is_userdef_p = false;
4563 while (cp_parser_is_string_literal (tok));
4565 /* A string literal built by concatenation has its caret=start at
4566 the start of the initial string, and its finish at the finish of
4567 the final string literal. */
4568 loc = make_location (loc, loc, get_finish (last_tok_loc));
4570 strs = (cpp_string *) obstack_finish (&str_ob);
4573 if (type != CPP_STRING && !wide_ok)
4575 cp_parser_error (parser, "a wide string is invalid in this context");
4576 type = CPP_STRING;
4579 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4580 (parse_in, strs, count, &istr, type))
4582 value = build_string (istr.len, (const char *)istr.text);
4583 free (CONST_CAST (unsigned char *, istr.text));
4584 if (count > 1)
4586 location_t *locs = (location_t *)obstack_finish (&loc_ob);
4587 gcc_assert (g_string_concat_db);
4588 g_string_concat_db->record_string_concatenation (count, locs);
4591 switch (type)
4593 default:
4594 case CPP_STRING:
4595 TREE_TYPE (value) = char_array_type_node;
4596 break;
4597 case CPP_UTF8STRING:
4598 if (flag_char8_t)
4599 TREE_TYPE (value) = char8_array_type_node;
4600 else
4601 TREE_TYPE (value) = char_array_type_node;
4602 break;
4603 case CPP_STRING16:
4604 TREE_TYPE (value) = char16_array_type_node;
4605 break;
4606 case CPP_STRING32:
4607 TREE_TYPE (value) = char32_array_type_node;
4608 break;
4609 case CPP_WSTRING:
4610 TREE_TYPE (value) = wchar_array_type_node;
4611 break;
4614 value = fix_string_type (value);
4616 if (have_suffix_p)
4618 tree literal = build_userdef_literal (suffix_id, value,
4619 OT_NONE, NULL_TREE);
4620 if (lookup_udlit)
4621 value = finish_userdef_string_literal (literal);
4622 else
4623 value = literal;
4626 else
4627 /* cpp_interpret_string has issued an error. */
4628 value = error_mark_node;
4630 if (count > 1)
4632 obstack_free (&str_ob, 0);
4633 obstack_free (&loc_ob, 0);
4636 return cp_expr (value, loc);
4639 /* Parse a sequence of adjacent string constants. Return a TREE_STRING
4640 representing the combined, nul-terminated string constant. If
4641 TRANSLATE is true, translate the string to the execution character set.
4642 If WIDE_OK is true, a wide string is valid here.
4644 This function issues an error if a user defined string literal is
4645 encountered; use cp_parser_userdef_string_literal if UDLs are allowed. */
4647 static inline cp_expr
4648 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
4650 return cp_parser_string_literal_common (parser, translate, wide_ok,
4651 /*udl_ok=*/false,
4652 /*lookup_udlit=*/false);
4655 /* Parse a string literal or user defined string literal.
4657 user-defined-string-literal :
4658 string-literal ud-suffix
4660 If LOOKUP_UDLIT, perform a lookup for a suitable template function. */
4662 static inline cp_expr
4663 cp_parser_userdef_string_literal (cp_parser *parser, bool lookup_udlit)
4665 return cp_parser_string_literal_common (parser, /*translate=*/true,
4666 /*wide_ok=*/true, /*udl_ok=*/true,
4667 lookup_udlit);
4670 /* Look up a literal operator with the name and the exact arguments. */
4672 static tree
4673 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4675 tree decl = lookup_name (name);
4676 if (!decl || !is_overloaded_fn (decl))
4677 return error_mark_node;
4679 for (lkp_iterator iter (decl); iter; ++iter)
4681 tree fn = *iter;
4683 if (tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn)))
4685 unsigned int ix;
4686 bool found = true;
4688 for (ix = 0;
4689 found && ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4690 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4692 tree tparm = TREE_VALUE (parmtypes);
4693 tree targ = TREE_TYPE ((*args)[ix]);
4694 bool ptr = TYPE_PTR_P (tparm);
4695 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4696 if ((ptr || arr || !same_type_p (tparm, targ))
4697 && (!ptr || !arr
4698 || !same_type_p (TREE_TYPE (tparm),
4699 TREE_TYPE (targ))))
4700 found = false;
4703 if (found
4704 && ix == vec_safe_length (args)
4705 /* May be this should be sufficient_parms_p instead,
4706 depending on how exactly should user-defined literals
4707 work in presence of default arguments on the literal
4708 operator parameters. */
4709 && parmtypes == void_list_node)
4710 return decl;
4714 return error_mark_node;
4717 /* Parse a user-defined char constant. Returns a call to a user-defined
4718 literal operator taking the character as an argument. */
4720 static cp_expr
4721 cp_parser_userdef_char_literal (cp_parser *parser)
4723 cp_token *token = cp_lexer_consume_token (parser->lexer);
4724 tree literal = token->u.value;
4725 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4726 tree value = USERDEF_LITERAL_VALUE (literal);
4727 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4728 tree decl, result;
4730 /* Build up a call to the user-defined operator */
4731 /* Lookup the name we got back from the id-expression. */
4732 releasing_vec args;
4733 vec_safe_push (args, value);
4734 decl = lookup_literal_operator (name, args);
4735 if (!decl || decl == error_mark_node)
4737 error ("unable to find character literal operator %qD with %qT argument",
4738 name, TREE_TYPE (value));
4739 return error_mark_node;
4741 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4742 return result;
4745 /* A subroutine of cp_parser_userdef_numeric_literal to
4746 create a char... template parameter pack from a string node. */
4748 static tree
4749 make_char_string_pack (tree value)
4751 tree charvec;
4752 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4753 const unsigned char *str
4754 = (const unsigned char *) TREE_STRING_POINTER (value);
4755 int i, len = TREE_STRING_LENGTH (value) - 1;
4756 tree argvec = make_tree_vec (1);
4758 /* Fill in CHARVEC with all of the parameters. */
4759 charvec = make_tree_vec (len);
4760 for (i = 0; i < len; ++i)
4762 unsigned char s[3] = { '\'', str[i], '\'' };
4763 cpp_string in = { 3, s };
4764 cpp_string out = { 0, 0 };
4765 if (!cpp_interpret_string (parse_in, &in, 1, &out, CPP_STRING))
4766 return NULL_TREE;
4767 gcc_assert (out.len == 2);
4768 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node,
4769 out.text[0]);
4772 /* Build the argument packs. */
4773 ARGUMENT_PACK_ARGS (argpack) = charvec;
4775 TREE_VEC_ELT (argvec, 0) = argpack;
4777 return argvec;
4780 /* A subroutine of cp_parser_userdef_numeric_literal to
4781 create a char... template parameter pack from a string node. */
4783 static tree
4784 make_string_pack (tree value)
4786 tree charvec;
4787 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4788 const unsigned char *str
4789 = (const unsigned char *) TREE_STRING_POINTER (value);
4790 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4791 int len = TREE_STRING_LENGTH (value) / sz - 1;
4792 tree argvec = make_tree_vec (2);
4794 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4795 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4797 /* First template parm is character type. */
4798 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4800 /* Fill in CHARVEC with all of the parameters. */
4801 charvec = make_tree_vec (len);
4802 for (int i = 0; i < len; ++i)
4803 TREE_VEC_ELT (charvec, i)
4804 = double_int_to_tree (str_char_type_node,
4805 double_int::from_buffer (str + i * sz, sz));
4807 /* Build the argument packs. */
4808 ARGUMENT_PACK_ARGS (argpack) = charvec;
4810 TREE_VEC_ELT (argvec, 1) = argpack;
4812 return argvec;
4815 /* Parse a user-defined numeric constant. returns a call to a user-defined
4816 literal operator. */
4818 static cp_expr
4819 cp_parser_userdef_numeric_literal (cp_parser *parser)
4821 cp_token *token = cp_lexer_consume_token (parser->lexer);
4822 tree literal = token->u.value;
4823 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4824 tree value = USERDEF_LITERAL_VALUE (literal);
4825 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4826 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4827 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4828 tree decl, result;
4830 /* Look for a literal operator taking the exact type of numeric argument
4831 as the literal value. */
4832 releasing_vec args;
4833 vec_safe_push (args, value);
4834 decl = lookup_literal_operator (name, args);
4835 if (decl && decl != error_mark_node)
4837 result = finish_call_expr (decl, &args, false, true,
4838 tf_warning_or_error);
4840 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4842 warning_at (token->location, OPT_Woverflow,
4843 "integer literal exceeds range of %qT type",
4844 long_long_unsigned_type_node);
4846 else
4848 if (overflow > 0)
4849 warning_at (token->location, OPT_Woverflow,
4850 "floating literal exceeds range of %qT type",
4851 long_double_type_node);
4852 else if (overflow < 0)
4853 warning_at (token->location, OPT_Woverflow,
4854 "floating literal truncated to zero");
4857 return result;
4860 /* If the numeric argument didn't work, look for a raw literal
4861 operator taking a const char* argument consisting of the number
4862 in string format. */
4863 args->truncate (0);
4864 vec_safe_push (args, num_string);
4865 decl = lookup_literal_operator (name, args);
4866 if (decl && decl != error_mark_node)
4868 result = finish_call_expr (decl, &args, false, true,
4869 tf_warning_or_error);
4870 return result;
4873 /* If the raw literal didn't work, look for a non-type template
4874 function with parameter pack char.... Call the function with
4875 template parameter characters representing the number. */
4876 args->truncate (0);
4877 decl = lookup_literal_operator (name, args);
4878 if (decl && decl != error_mark_node)
4880 tree tmpl_args = make_char_string_pack (num_string);
4881 if (tmpl_args == NULL_TREE)
4883 error ("failed to translate literal to execution character set %qT",
4884 num_string);
4885 return error_mark_node;
4887 decl = lookup_template_function (decl, tmpl_args);
4888 result = finish_call_expr (decl, &args, false, true,
4889 tf_warning_or_error);
4890 return result;
4893 /* In C++14 the standard library defines complex number suffixes that
4894 conflict with GNU extensions. Prefer them if <complex> is #included. */
4895 bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
4896 bool i14 = (cxx_dialect > cxx11
4897 && (id_equal (suffix_id, "i")
4898 || id_equal (suffix_id, "if")
4899 || id_equal (suffix_id, "il")));
4900 diagnostic_t kind = DK_ERROR;
4901 int opt = 0;
4903 if (i14 && ext)
4905 tree cxlit = lookup_qualified_name (std_node, "complex_literals",
4906 LOOK_want::NORMAL, false);
4907 if (cxlit == error_mark_node)
4909 /* No <complex>, so pedwarn and use GNU semantics. */
4910 kind = DK_PEDWARN;
4911 opt = OPT_Wpedantic;
4915 bool complained
4916 = emit_diagnostic (kind, input_location, opt,
4917 "unable to find numeric literal operator %qD", name);
4919 if (!complained)
4920 /* Don't inform either. */;
4921 else if (i14)
4923 inform (token->location, "add %<using namespace std::complex_literals%> "
4924 "(from %<<complex>%>) to enable the C++14 user-defined literal "
4925 "suffixes");
4926 if (ext)
4927 inform (token->location, "or use %<j%> instead of %<i%> for the "
4928 "GNU built-in suffix");
4930 else if (!ext)
4931 inform (token->location, "use %<-fext-numeric-literals%> "
4932 "to enable more built-in suffixes");
4934 if (kind == DK_ERROR)
4935 value = error_mark_node;
4936 else
4938 /* Use the built-in semantics. */
4939 tree type;
4940 if (id_equal (suffix_id, "i"))
4942 if (TREE_CODE (value) == INTEGER_CST)
4943 type = integer_type_node;
4944 else
4945 type = double_type_node;
4947 else if (id_equal (suffix_id, "if"))
4948 type = float_type_node;
4949 else /* if (id_equal (suffix_id, "il")) */
4950 type = long_double_type_node;
4952 value = fold_build2 (COMPLEX_EXPR, build_complex_type (type),
4953 build_zero_cst (type), fold_convert (type, value));
4956 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4957 /* Avoid repeated diagnostics. */
4958 token->u.value = value;
4959 return value;
4962 /* Parse a user-defined string constant. Returns a call to a user-defined
4963 literal operator taking a character pointer and the length of the string
4964 as arguments. */
4966 static tree
4967 finish_userdef_string_literal (tree literal)
4969 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4970 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4971 tree value = USERDEF_LITERAL_VALUE (literal);
4972 int len = TREE_STRING_LENGTH (value)
4973 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4974 tree decl;
4976 /* Build up a call to the user-defined operator. */
4977 /* Lookup the name we got back from the id-expression. */
4978 releasing_vec args;
4979 vec_safe_push (args, value);
4980 vec_safe_push (args, build_int_cst (size_type_node, len));
4981 decl = lookup_literal_operator (name, args);
4983 if (decl && decl != error_mark_node)
4984 return finish_call_expr (decl, &args, false, true,
4985 tf_warning_or_error);
4987 /* Look for a suitable template function, either (C++20) with a single
4988 parameter of class type, or (N3599) with typename parameter CharT and
4989 parameter pack CharT... */
4990 args->truncate (0);
4991 decl = lookup_literal_operator (name, args);
4992 if (decl && decl != error_mark_node)
4994 /* Use resolve_nondeduced_context to try to choose one form of template
4995 or the other. */
4996 tree tmpl_args = make_tree_vec (1);
4997 TREE_VEC_ELT (tmpl_args, 0) = value;
4998 decl = lookup_template_function (decl, tmpl_args);
4999 tree res = resolve_nondeduced_context (decl, tf_none);
5000 if (DECL_P (res))
5001 decl = res;
5002 else
5004 TREE_OPERAND (decl, 1) = make_string_pack (value);
5005 res = resolve_nondeduced_context (decl, tf_none);
5006 if (DECL_P (res))
5007 decl = res;
5009 if (!DECL_P (decl) && cxx_dialect > cxx17)
5010 TREE_OPERAND (decl, 1) = tmpl_args;
5011 return finish_call_expr (decl, &args, false, true,
5012 tf_warning_or_error);
5015 error ("unable to find string literal operator %qD with %qT, %qT arguments",
5016 name, TREE_TYPE (value), size_type_node);
5017 return error_mark_node;
5021 /* Basic concepts [gram.basic] */
5023 /* Parse a translation-unit.
5025 translation-unit:
5026 declaration-seq [opt] */
5028 static void
5029 cp_parser_translation_unit (cp_parser* parser)
5031 gcc_checking_assert (!cp_error_declarator);
5033 /* Create the declarator obstack. */
5034 gcc_obstack_init (&declarator_obstack);
5035 /* Create the error declarator. */
5036 cp_error_declarator = make_declarator (cdk_error);
5037 /* Create the empty parameter list. */
5038 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
5039 UNKNOWN_LOCATION);
5040 /* Remember where the base of the declarator obstack lies. */
5041 void *declarator_obstack_base = obstack_next_free (&declarator_obstack);
5043 push_deferring_access_checks (flag_access_control
5044 ? dk_no_deferred : dk_no_check);
5046 module_parse mp_state = MP_NOT_MODULE;
5047 if (modules_p () && !header_module_p ())
5048 mp_state = MP_FIRST;
5050 bool implicit_extern_c = false;
5052 /* Parse until EOF. */
5053 for (;;)
5055 cp_token *token = cp_lexer_peek_token (parser->lexer);
5057 /* If we're entering or exiting a region that's implicitly
5058 extern "C", modify the lang context appropriately. This is
5059 so horrible. Please die. */
5060 if (implicit_extern_c
5061 != cp_lexer_peek_token (parser->lexer)->implicit_extern_c)
5063 implicit_extern_c = !implicit_extern_c;
5064 if (implicit_extern_c)
5065 push_lang_context (lang_name_c);
5066 else
5067 pop_lang_context ();
5070 if (token->type == CPP_EOF)
5071 break;
5073 if (modules_p ())
5075 /* Top-level module declarations are ok, and change the
5076 portion of file we're in. Top-level import declarations
5077 are significant for the import portions. */
5079 cp_token *next = token;
5080 bool exporting = token->keyword == RID__EXPORT;
5081 if (exporting)
5083 cp_lexer_consume_token (parser->lexer);
5084 next = cp_lexer_peek_token (parser->lexer);
5086 if (next->keyword == RID__MODULE)
5088 mp_state
5089 = cp_parser_module_declaration (parser, mp_state, exporting);
5090 continue;
5092 else if (next->keyword == RID__IMPORT)
5094 if (mp_state == MP_FIRST)
5095 mp_state = MP_NOT_MODULE;
5096 cp_parser_import_declaration (parser, mp_state, exporting);
5097 continue;
5099 else
5100 gcc_checking_assert (!exporting);
5102 if (mp_state == MP_GLOBAL && token->main_source_p)
5104 static bool warned = false;
5105 if (!warned)
5107 warned = true;
5108 error_at (token->location,
5109 "global module fragment contents must be"
5110 " from preprocessor inclusion");
5115 /* This relies on the ordering of module_parse values. */
5116 if (mp_state == MP_PURVIEW_IMPORTS || mp_state == MP_PRIVATE_IMPORTS)
5117 /* We're no longer in the import portion of a named module. */
5118 mp_state = module_parse (mp_state + 1);
5119 else if (mp_state == MP_FIRST)
5120 mp_state = MP_NOT_MODULE;
5122 if (token->type == CPP_CLOSE_BRACE)
5124 cp_parser_error (parser, "expected declaration");
5125 cp_lexer_consume_token (parser->lexer);
5126 /* If the next token is now a `;', consume it. */
5127 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
5128 cp_lexer_consume_token (parser->lexer);
5130 else
5131 cp_parser_toplevel_declaration (parser);
5134 /* Get rid of the token array; we don't need it any more. */
5135 cp_lexer_destroy (parser->lexer);
5136 parser->lexer = NULL;
5138 /* The EOF should have reset this. */
5139 gcc_checking_assert (!implicit_extern_c);
5141 /* Make sure the declarator obstack was fully cleaned up. */
5142 gcc_assert (obstack_next_free (&declarator_obstack)
5143 == declarator_obstack_base);
5146 /* Return the appropriate tsubst flags for parsing, possibly in N3276
5147 decltype context. */
5149 static inline tsubst_flags_t
5150 complain_flags (bool decltype_p)
5152 tsubst_flags_t complain = tf_warning_or_error;
5153 if (decltype_p)
5154 complain |= tf_decltype;
5155 return complain;
5158 /* We're about to parse a collection of statements. If we're currently
5159 parsing tentatively, set up a firewall so that any nested
5160 cp_parser_commit_to_tentative_parse won't affect the current context. */
5162 static cp_token_position
5163 cp_parser_start_tentative_firewall (cp_parser *parser)
5165 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5166 return 0;
5168 cp_parser_parse_tentatively (parser);
5169 cp_parser_commit_to_topmost_tentative_parse (parser);
5170 return cp_lexer_token_position (parser->lexer, false);
5173 /* We've finished parsing the collection of statements. Wrap up the
5174 firewall and replace the relevant tokens with the parsed form. */
5176 static void
5177 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
5178 tree expr)
5180 if (!start)
5181 return;
5183 /* Finish the firewall level. */
5184 cp_parser_parse_definitely (parser);
5185 /* And remember the result of the parse for when we try again. */
5186 cp_token *token = cp_lexer_token_at (parser->lexer, start);
5187 token->type = CPP_PREPARSED_EXPR;
5188 token->u.value = expr;
5189 token->keyword = RID_MAX;
5190 cp_lexer_purge_tokens_after (parser->lexer, start);
5193 /* Like the above functions, but let the user modify the tokens. Used by
5194 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
5195 later parses, so it makes sense to localize the effects of
5196 cp_parser_commit_to_tentative_parse. */
5198 struct tentative_firewall
5200 cp_parser *parser;
5201 bool set;
5203 tentative_firewall (cp_parser *p): parser(p)
5205 /* If we're currently parsing tentatively, start a committed level as a
5206 firewall and then an inner tentative parse. */
5207 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
5209 cp_parser_parse_tentatively (parser);
5210 cp_parser_commit_to_topmost_tentative_parse (parser);
5211 cp_parser_parse_tentatively (parser);
5215 ~tentative_firewall()
5217 if (set)
5219 /* Finish the inner tentative parse and the firewall, propagating any
5220 uncommitted error state to the outer tentative parse. */
5221 bool err = cp_parser_error_occurred (parser);
5222 cp_parser_parse_definitely (parser);
5223 cp_parser_parse_definitely (parser);
5224 if (err)
5225 cp_parser_simulate_error (parser);
5230 /* Some tokens naturally come in pairs e.g.'(' and ')'.
5231 This class is for tracking such a matching pair of symbols.
5232 In particular, it tracks the location of the first token,
5233 so that if the second token is missing, we can highlight the
5234 location of the first token when notifying the user about the
5235 problem. */
5237 template <typename traits_t>
5238 class token_pair
5240 public:
5241 /* token_pair's ctor. */
5242 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
5244 /* If the next token is the opening symbol for this pair, consume it and
5245 return true.
5246 Otherwise, issue an error and return false.
5247 In either case, record the location of the opening token. */
5249 bool require_open (cp_parser *parser)
5251 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
5252 return cp_parser_require (parser, traits_t::open_token_type,
5253 traits_t::required_token_open);
5256 /* Consume the next token from PARSER, recording its location as
5257 that of the opening token within the pair. */
5259 cp_token * consume_open (cp_parser *parser)
5261 cp_token *tok = cp_lexer_consume_token (parser->lexer);
5262 gcc_assert (tok->type == traits_t::open_token_type);
5263 m_open_loc = tok->location;
5264 return tok;
5267 /* If the next token is the closing symbol for this pair, consume it
5268 and return it.
5269 Otherwise, issue an error, highlighting the location of the
5270 corresponding opening token, and return NULL. */
5272 cp_token *require_close (cp_parser *parser) const
5274 return cp_parser_require (parser, traits_t::close_token_type,
5275 traits_t::required_token_close,
5276 m_open_loc);
5279 location_t open_location () const { return m_open_loc; }
5281 private:
5282 location_t m_open_loc;
5285 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
5287 struct matching_paren_traits
5289 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
5290 static const enum required_token required_token_open = RT_OPEN_PAREN;
5291 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
5292 static const enum required_token required_token_close = RT_CLOSE_PAREN;
5295 /* "matching_parens" is a token_pair<T> class for tracking matching
5296 pairs of parentheses. */
5298 typedef token_pair<matching_paren_traits> matching_parens;
5300 /* Traits for token_pair<T> for tracking matching pairs of braces. */
5302 struct matching_brace_traits
5304 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
5305 static const enum required_token required_token_open = RT_OPEN_BRACE;
5306 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
5307 static const enum required_token required_token_close = RT_CLOSE_BRACE;
5310 /* "matching_braces" is a token_pair<T> class for tracking matching
5311 pairs of braces. */
5313 typedef token_pair<matching_brace_traits> matching_braces;
5316 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
5317 enclosing parentheses. */
5319 static cp_expr
5320 cp_parser_statement_expr (cp_parser *parser)
5322 cp_token_position start = cp_parser_start_tentative_firewall (parser);
5324 /* Consume the '('. */
5325 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
5326 matching_parens parens;
5327 parens.consume_open (parser);
5328 /* Start the statement-expression. */
5329 tree expr = begin_stmt_expr ();
5330 /* Parse the compound-statement. */
5331 cp_parser_compound_statement (parser, expr, BCS_STMT_EXPR, false);
5332 /* Finish up. */
5333 expr = finish_stmt_expr (expr, false);
5334 /* Consume the ')'. */
5335 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
5336 if (!parens.require_close (parser))
5337 cp_parser_skip_to_end_of_statement (parser);
5339 cp_parser_end_tentative_firewall (parser, start, expr);
5340 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
5341 return cp_expr (expr, combined_loc);
5344 /* Expressions [gram.expr] */
5346 /* Parse a fold-operator.
5348 fold-operator:
5349 - * / % ^ & | = < > << >>
5350 = -= *= /= %= ^= &= |= <<= >>=
5351 == != <= >= && || , .* ->*
5353 This returns the tree code corresponding to the matched operator
5354 as an int. When the current token matches a compound assignment
5355 operator, the resulting tree code is the negative value of the
5356 non-assignment operator. */
5358 static int
5359 cp_parser_fold_operator (cp_token *token)
5361 switch (token->type)
5363 case CPP_PLUS: return PLUS_EXPR;
5364 case CPP_MINUS: return MINUS_EXPR;
5365 case CPP_MULT: return MULT_EXPR;
5366 case CPP_DIV: return TRUNC_DIV_EXPR;
5367 case CPP_MOD: return TRUNC_MOD_EXPR;
5368 case CPP_XOR: return BIT_XOR_EXPR;
5369 case CPP_AND: return BIT_AND_EXPR;
5370 case CPP_OR: return BIT_IOR_EXPR;
5371 case CPP_LSHIFT: return LSHIFT_EXPR;
5372 case CPP_RSHIFT: return RSHIFT_EXPR;
5374 case CPP_EQ: return -NOP_EXPR;
5375 case CPP_PLUS_EQ: return -PLUS_EXPR;
5376 case CPP_MINUS_EQ: return -MINUS_EXPR;
5377 case CPP_MULT_EQ: return -MULT_EXPR;
5378 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
5379 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
5380 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
5381 case CPP_AND_EQ: return -BIT_AND_EXPR;
5382 case CPP_OR_EQ: return -BIT_IOR_EXPR;
5383 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
5384 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
5386 case CPP_EQ_EQ: return EQ_EXPR;
5387 case CPP_NOT_EQ: return NE_EXPR;
5388 case CPP_LESS: return LT_EXPR;
5389 case CPP_GREATER: return GT_EXPR;
5390 case CPP_LESS_EQ: return LE_EXPR;
5391 case CPP_GREATER_EQ: return GE_EXPR;
5393 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
5394 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
5396 case CPP_COMMA: return COMPOUND_EXPR;
5398 case CPP_DOT_STAR: return DOTSTAR_EXPR;
5399 case CPP_DEREF_STAR: return MEMBER_REF;
5401 default: return ERROR_MARK;
5405 /* Returns true if CODE indicates a binary expression, which is not allowed in
5406 the LHS of a fold-expression. More codes will need to be added to use this
5407 function in other contexts. */
5409 static bool
5410 is_binary_op (tree_code code)
5412 switch (code)
5414 case PLUS_EXPR:
5415 case POINTER_PLUS_EXPR:
5416 case MINUS_EXPR:
5417 case MULT_EXPR:
5418 case TRUNC_DIV_EXPR:
5419 case TRUNC_MOD_EXPR:
5420 case BIT_XOR_EXPR:
5421 case BIT_AND_EXPR:
5422 case BIT_IOR_EXPR:
5423 case LSHIFT_EXPR:
5424 case RSHIFT_EXPR:
5426 case MODOP_EXPR:
5428 case EQ_EXPR:
5429 case NE_EXPR:
5430 case LE_EXPR:
5431 case GE_EXPR:
5432 case LT_EXPR:
5433 case GT_EXPR:
5435 case TRUTH_ANDIF_EXPR:
5436 case TRUTH_ORIF_EXPR:
5438 case COMPOUND_EXPR:
5440 case DOTSTAR_EXPR:
5441 case MEMBER_REF:
5442 return true;
5444 default:
5445 return false;
5449 /* If the next token is a suitable fold operator, consume it and return as
5450 the function above. */
5452 static int
5453 cp_parser_fold_operator (cp_parser *parser)
5455 cp_token* token = cp_lexer_peek_token (parser->lexer);
5456 int code = cp_parser_fold_operator (token);
5457 if (code != ERROR_MARK)
5458 cp_lexer_consume_token (parser->lexer);
5459 return code;
5462 /* Parse a fold-expression.
5464 fold-expression:
5465 ( ... folding-operator cast-expression)
5466 ( cast-expression folding-operator ... )
5467 ( cast-expression folding operator ... folding-operator cast-expression)
5469 Note that the '(' and ')' are matched in primary expression. */
5471 static cp_expr
5472 cp_parser_fold_expression (cp_parser *parser, tree expr1)
5474 cp_id_kind pidk;
5476 // Left fold.
5477 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5479 if (expr1)
5480 return error_mark_node;
5481 cp_lexer_consume_token (parser->lexer);
5482 int op = cp_parser_fold_operator (parser);
5483 if (op == ERROR_MARK)
5485 cp_parser_error (parser, "expected binary operator");
5486 return error_mark_node;
5489 tree expr = cp_parser_cast_expression (parser, false, false,
5490 false, &pidk);
5491 if (expr == error_mark_node)
5492 return error_mark_node;
5493 return finish_left_unary_fold_expr (expr, op);
5496 const cp_token* token = cp_lexer_peek_token (parser->lexer);
5497 int op = cp_parser_fold_operator (parser);
5498 if (op == ERROR_MARK)
5500 cp_parser_error (parser, "expected binary operator");
5501 return error_mark_node;
5504 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
5506 cp_parser_error (parser, "expected ...");
5507 return error_mark_node;
5509 cp_lexer_consume_token (parser->lexer);
5511 /* The operands of a fold-expression are cast-expressions, so binary or
5512 conditional expressions are not allowed. We check this here to avoid
5513 tentative parsing. */
5514 if (EXPR_P (expr1) && warning_suppressed_p (expr1, OPT_Wparentheses))
5515 /* OK, the expression was parenthesized. */;
5516 else if (is_binary_op (TREE_CODE (expr1)))
5517 error_at (location_of (expr1),
5518 "binary expression in operand of fold-expression");
5519 else if (TREE_CODE (expr1) == COND_EXPR
5520 || (REFERENCE_REF_P (expr1)
5521 && TREE_CODE (TREE_OPERAND (expr1, 0)) == COND_EXPR))
5522 error_at (location_of (expr1),
5523 "conditional expression in operand of fold-expression");
5525 // Right fold.
5526 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5527 return finish_right_unary_fold_expr (expr1, op);
5529 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
5531 cp_parser_error (parser, "mismatched operator in fold-expression");
5532 return error_mark_node;
5534 cp_lexer_consume_token (parser->lexer);
5536 // Binary left or right fold.
5537 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
5538 if (expr2 == error_mark_node)
5539 return error_mark_node;
5540 return finish_binary_fold_expr (expr1, expr2, op);
5543 /* Parse a primary-expression.
5545 primary-expression:
5546 literal
5547 this
5548 ( expression )
5549 id-expression
5550 lambda-expression (C++11)
5552 GNU Extensions:
5554 primary-expression:
5555 ( compound-statement )
5556 __builtin_va_arg ( assignment-expression , type-id )
5557 __builtin_offsetof ( type-id , offsetof-expression )
5559 C++ Extensions:
5560 __has_nothrow_assign ( type-id )
5561 __has_nothrow_constructor ( type-id )
5562 __has_nothrow_copy ( type-id )
5563 __has_trivial_assign ( type-id )
5564 __has_trivial_constructor ( type-id )
5565 __has_trivial_copy ( type-id )
5566 __has_trivial_destructor ( type-id )
5567 __has_virtual_destructor ( type-id )
5568 __is_abstract ( type-id )
5569 __is_base_of ( type-id , type-id )
5570 __is_class ( type-id )
5571 __is_empty ( type-id )
5572 __is_enum ( type-id )
5573 __is_final ( type-id )
5574 __is_literal_type ( type-id )
5575 __is_pod ( type-id )
5576 __is_polymorphic ( type-id )
5577 __is_std_layout ( type-id )
5578 __is_trivial ( type-id )
5579 __is_union ( type-id )
5581 Objective-C++ Extension:
5583 primary-expression:
5584 objc-expression
5586 literal:
5587 __null
5589 ADDRESS_P is true iff this expression was immediately preceded by
5590 "&" and therefore might denote a pointer-to-member. CAST_P is true
5591 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5592 true iff this expression is a template argument.
5594 Returns a representation of the expression. Upon return, *IDK
5595 indicates what kind of id-expression (if any) was present. */
5597 static cp_expr
5598 cp_parser_primary_expression (cp_parser *parser,
5599 bool address_p,
5600 bool cast_p,
5601 bool template_arg_p,
5602 bool decltype_p,
5603 cp_id_kind *idk)
5605 cp_token *token = NULL;
5607 /* Assume the primary expression is not an id-expression. */
5608 *idk = CP_ID_KIND_NONE;
5610 /* Peek at the next token. */
5611 token = cp_lexer_peek_token (parser->lexer);
5612 switch ((int) token->type)
5614 /* literal:
5615 integer-literal
5616 character-literal
5617 floating-literal
5618 string-literal
5619 boolean-literal
5620 pointer-literal
5621 user-defined-literal */
5622 case CPP_CHAR:
5623 case CPP_CHAR16:
5624 case CPP_CHAR32:
5625 case CPP_WCHAR:
5626 case CPP_UTF8CHAR:
5627 case CPP_NUMBER:
5628 case CPP_PREPARSED_EXPR:
5629 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5630 return cp_parser_userdef_numeric_literal (parser);
5631 token = cp_lexer_consume_token (parser->lexer);
5632 if (TREE_CODE (token->u.value) == FIXED_CST)
5634 error_at (token->location,
5635 "fixed-point types not supported in C++");
5636 return error_mark_node;
5638 /* Floating-point literals are only allowed in an integral
5639 constant expression if they are cast to an integral or
5640 enumeration type. */
5641 if ((TREE_CODE (token->u.value) == REAL_CST
5642 || (TREE_CODE (token->u.value) == EXCESS_PRECISION_EXPR
5643 && TREE_CODE (TREE_OPERAND (token->u.value, 0)) == REAL_CST))
5644 && parser->integral_constant_expression_p
5645 && pedantic)
5647 /* CAST_P will be set even in invalid code like "int(2.7 +
5648 ...)". Therefore, we have to check that the next token
5649 is sure to end the cast. */
5650 if (cast_p)
5652 cp_token *next_token;
5654 next_token = cp_lexer_peek_token (parser->lexer);
5655 if (/* The comma at the end of an
5656 enumerator-definition. */
5657 next_token->type != CPP_COMMA
5658 /* The curly brace at the end of an enum-specifier. */
5659 && next_token->type != CPP_CLOSE_BRACE
5660 /* The end of a statement. */
5661 && next_token->type != CPP_SEMICOLON
5662 /* The end of the cast-expression. */
5663 && next_token->type != CPP_CLOSE_PAREN
5664 /* The end of an array bound. */
5665 && next_token->type != CPP_CLOSE_SQUARE
5666 /* The closing ">" in a template-argument-list. */
5667 && (next_token->type != CPP_GREATER
5668 || parser->greater_than_is_operator_p)
5669 /* C++0x only: A ">>" treated like two ">" tokens,
5670 in a template-argument-list. */
5671 && (next_token->type != CPP_RSHIFT
5672 || (cxx_dialect == cxx98)
5673 || parser->greater_than_is_operator_p))
5674 cast_p = false;
5677 /* If we are within a cast, then the constraint that the
5678 cast is to an integral or enumeration type will be
5679 checked at that point. If we are not within a cast, then
5680 this code is invalid. */
5681 if (!cast_p)
5682 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5684 return (cp_expr (token->u.value, token->location, token->flags & DECIMAL_INT)
5685 .maybe_add_location_wrapper ());
5687 case CPP_CHAR_USERDEF:
5688 case CPP_CHAR16_USERDEF:
5689 case CPP_CHAR32_USERDEF:
5690 case CPP_WCHAR_USERDEF:
5691 case CPP_UTF8CHAR_USERDEF:
5692 return cp_parser_userdef_char_literal (parser);
5694 case CPP_STRING:
5695 case CPP_STRING16:
5696 case CPP_STRING32:
5697 case CPP_WSTRING:
5698 case CPP_UTF8STRING:
5699 case CPP_STRING_USERDEF:
5700 case CPP_STRING16_USERDEF:
5701 case CPP_STRING32_USERDEF:
5702 case CPP_WSTRING_USERDEF:
5703 case CPP_UTF8STRING_USERDEF:
5704 /* ??? Should wide strings be allowed when parser->translate_strings_p
5705 is false (i.e. in attributes)? If not, we can kill the third
5706 argument to cp_parser_string_literal. */
5707 if (parser->translate_strings_p)
5708 return (cp_parser_userdef_string_literal (parser,
5709 /*lookup_udlit=*/true)
5710 .maybe_add_location_wrapper ());
5711 else
5712 return (cp_parser_string_literal (parser,
5713 /*translate=*/false,
5714 /*wide_ok=*/true)
5715 .maybe_add_location_wrapper ());
5717 case CPP_OPEN_PAREN:
5718 /* If we see `( { ' then we are looking at the beginning of
5719 a GNU statement-expression. */
5720 if (cp_parser_allow_gnu_extensions_p (parser)
5721 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5723 /* Statement-expressions are not allowed by the standard. */
5724 pedwarn (token->location, OPT_Wpedantic,
5725 "ISO C++ forbids braced-groups within expressions");
5727 /* And they're not allowed outside of a function-body; you
5728 cannot, for example, write:
5730 int i = ({ int j = 3; j + 1; });
5732 at class or namespace scope. */
5733 if (!parser->in_function_body
5734 || parser->in_template_argument_list_p)
5736 error_at (token->location,
5737 "statement-expressions are not allowed outside "
5738 "functions nor in template-argument lists");
5739 cp_parser_skip_to_end_of_block_or_statement (parser);
5740 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5741 cp_lexer_consume_token (parser->lexer);
5742 return error_mark_node;
5744 else
5745 return cp_parser_statement_expr (parser);
5747 /* Otherwise it's a normal parenthesized expression. */
5749 cp_expr expr;
5750 bool saved_greater_than_is_operator_p;
5752 location_t open_paren_loc = token->location;
5754 /* Consume the `('. */
5755 matching_parens parens;
5756 parens.consume_open (parser);
5757 /* Within a parenthesized expression, a `>' token is always
5758 the greater-than operator. */
5759 saved_greater_than_is_operator_p
5760 = parser->greater_than_is_operator_p;
5761 parser->greater_than_is_operator_p = true;
5763 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5764 /* Left fold expression. */
5765 expr = NULL_TREE;
5766 else
5767 /* Parse the parenthesized expression. */
5768 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5770 token = cp_lexer_peek_token (parser->lexer);
5771 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5773 expr = cp_parser_fold_expression (parser, expr);
5774 if (expr != error_mark_node
5775 && cxx_dialect < cxx17)
5776 pedwarn (input_location, OPT_Wc__17_extensions,
5777 "fold-expressions only available with %<-std=c++17%> "
5778 "or %<-std=gnu++17%>");
5780 else
5781 /* Let the front end know that this expression was
5782 enclosed in parentheses. This matters in case, for
5783 example, the expression is of the form `A::B', since
5784 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5785 not. */
5786 expr = finish_parenthesized_expr (expr);
5788 /* DR 705: Wrapping an unqualified name in parentheses
5789 suppresses arg-dependent lookup. We want to pass back
5790 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5791 (c++/37862), but none of the others. */
5792 if (*idk != CP_ID_KIND_QUALIFIED)
5793 *idk = CP_ID_KIND_NONE;
5795 /* The `>' token might be the end of a template-id or
5796 template-parameter-list now. */
5797 parser->greater_than_is_operator_p
5798 = saved_greater_than_is_operator_p;
5800 /* Consume the `)'. */
5801 token = cp_lexer_peek_token (parser->lexer);
5802 location_t close_paren_loc = token->location;
5803 bool no_wparens = warning_suppressed_p (expr, OPT_Wparentheses);
5804 expr.set_range (open_paren_loc, close_paren_loc);
5805 if (no_wparens)
5806 suppress_warning (expr, OPT_Wparentheses);
5807 if (!parens.require_close (parser)
5808 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5809 cp_parser_skip_to_end_of_statement (parser);
5811 return expr;
5814 case CPP_OPEN_SQUARE:
5816 if (c_dialect_objc ())
5818 /* We might have an Objective-C++ message. */
5819 cp_parser_parse_tentatively (parser);
5820 tree msg = cp_parser_objc_message_expression (parser);
5821 /* If that works out, we're done ... */
5822 if (cp_parser_parse_definitely (parser))
5823 return msg;
5824 /* ... else, fall though to see if it's a lambda. */
5826 cp_expr lam = cp_parser_lambda_expression (parser);
5827 /* Don't warn about a failed tentative parse. */
5828 if (cp_parser_error_occurred (parser))
5829 return error_mark_node;
5830 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5831 return lam;
5834 case CPP_OBJC_STRING:
5835 if (c_dialect_objc ())
5836 /* We have an Objective-C++ string literal. */
5837 return cp_parser_objc_expression (parser);
5838 cp_parser_error (parser, "expected primary-expression");
5839 return error_mark_node;
5841 case CPP_KEYWORD:
5842 switch (token->keyword)
5844 /* These two are the boolean literals. */
5845 case RID_TRUE:
5846 cp_lexer_consume_token (parser->lexer);
5847 return cp_expr (boolean_true_node, token->location);
5848 case RID_FALSE:
5849 cp_lexer_consume_token (parser->lexer);
5850 return cp_expr (boolean_false_node, token->location);
5852 /* The `__null' literal. */
5853 case RID_NULL:
5854 cp_lexer_consume_token (parser->lexer);
5855 return cp_expr (null_node, token->location);
5857 /* The `nullptr' literal. */
5858 case RID_NULLPTR:
5859 cp_lexer_consume_token (parser->lexer);
5860 return cp_expr (nullptr_node, token->location);
5862 /* Recognize the `this' keyword. */
5863 case RID_THIS:
5864 cp_lexer_consume_token (parser->lexer);
5865 if (parser->local_variables_forbidden_p & THIS_FORBIDDEN)
5867 error_at (token->location,
5868 "%<this%> may not be used in this context");
5869 return error_mark_node;
5871 /* Pointers cannot appear in constant-expressions. */
5872 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5873 return error_mark_node;
5874 return cp_expr (finish_this_expr (), token->location);
5876 /* The `operator' keyword can be the beginning of an
5877 id-expression. */
5878 case RID_OPERATOR:
5879 goto id_expression;
5881 case RID_FUNCTION_NAME:
5882 case RID_PRETTY_FUNCTION_NAME:
5883 case RID_C99_FUNCTION_NAME:
5885 non_integral_constant name;
5887 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5888 __func__ are the names of variables -- but they are
5889 treated specially. Therefore, they are handled here,
5890 rather than relying on the generic id-expression logic
5891 below. Grammatically, these names are id-expressions.
5893 Consume the token. */
5894 token = cp_lexer_consume_token (parser->lexer);
5896 switch (token->keyword)
5898 case RID_FUNCTION_NAME:
5899 name = NIC_FUNC_NAME;
5900 break;
5901 case RID_PRETTY_FUNCTION_NAME:
5902 name = NIC_PRETTY_FUNC;
5903 break;
5904 case RID_C99_FUNCTION_NAME:
5905 name = NIC_C99_FUNC;
5906 break;
5907 default:
5908 gcc_unreachable ();
5911 if (cp_parser_non_integral_constant_expression (parser, name))
5912 return error_mark_node;
5914 /* Look up the name. */
5915 return finish_fname (token->u.value);
5918 case RID_VA_ARG:
5920 tree expression;
5921 tree type;
5922 location_t type_location;
5923 location_t start_loc
5924 = cp_lexer_peek_token (parser->lexer)->location;
5925 /* The `__builtin_va_arg' construct is used to handle
5926 `va_arg'. Consume the `__builtin_va_arg' token. */
5927 cp_lexer_consume_token (parser->lexer);
5928 /* Look for the opening `('. */
5929 matching_parens parens;
5930 parens.require_open (parser);
5931 /* Now, parse the assignment-expression. */
5932 expression = cp_parser_assignment_expression (parser);
5933 /* Look for the `,'. */
5934 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5935 type_location = cp_lexer_peek_token (parser->lexer)->location;
5936 /* Parse the type-id. */
5938 type_id_in_expr_sentinel s (parser);
5939 type = cp_parser_type_id (parser);
5941 /* Look for the closing `)'. */
5942 location_t finish_loc
5943 = cp_lexer_peek_token (parser->lexer)->location;
5944 parens.require_close (parser);
5945 /* Using `va_arg' in a constant-expression is not
5946 allowed. */
5947 if (cp_parser_non_integral_constant_expression (parser,
5948 NIC_VA_ARG))
5949 return error_mark_node;
5950 /* Construct a location of the form:
5951 __builtin_va_arg (v, int)
5952 ~~~~~~~~~~~~~~~~~~~~~^~~~
5953 with the caret at the type, ranging from the start of the
5954 "__builtin_va_arg" token to the close paren. */
5955 location_t combined_loc
5956 = make_location (type_location, start_loc, finish_loc);
5957 return build_x_va_arg (combined_loc, expression, type);
5960 case RID_OFFSETOF:
5961 return cp_parser_builtin_offsetof (parser);
5963 #define DEFTRAIT_EXPR(CODE, NAME, ARITY) \
5964 case RID_##CODE:
5965 #include "cp-trait.def"
5966 #undef DEFTRAIT_EXPR
5967 return cp_parser_trait (parser, token->keyword);
5969 // C++ concepts
5970 case RID_REQUIRES:
5971 return cp_parser_requires_expression (parser);
5973 /* Objective-C++ expressions. */
5974 case RID_AT_ENCODE:
5975 case RID_AT_PROTOCOL:
5976 case RID_AT_SELECTOR:
5977 return cp_parser_objc_expression (parser);
5979 case RID_OMP_ALL_MEMORY:
5980 gcc_assert (flag_openmp);
5981 cp_lexer_consume_token (parser->lexer);
5982 error_at (token->location,
5983 "%<omp_all_memory%> may only be used in OpenMP "
5984 "%<depend%> clause");
5985 return error_mark_node;
5987 case RID_TEMPLATE:
5988 if (parser->in_function_body
5989 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5990 == CPP_LESS))
5992 error_at (token->location,
5993 "a template declaration cannot appear at block scope");
5994 cp_parser_skip_to_end_of_block_or_statement (parser);
5995 return error_mark_node;
5997 /* FALLTHRU */
5998 default:
5999 cp_parser_error (parser, "expected primary-expression");
6000 return error_mark_node;
6003 /* An id-expression can start with either an identifier, a
6004 `::' as the beginning of a qualified-id, or the "operator"
6005 keyword. */
6006 case CPP_NAME:
6007 case CPP_SCOPE:
6008 case CPP_TEMPLATE_ID:
6009 case CPP_NESTED_NAME_SPECIFIER:
6011 id_expression:
6012 cp_expr id_expression;
6013 cp_expr decl;
6014 const char *error_msg;
6015 bool template_p;
6016 bool done;
6017 cp_token *id_expr_token;
6019 /* Parse the id-expression. */
6020 id_expression
6021 = cp_parser_id_expression (parser,
6022 /*template_keyword_p=*/false,
6023 /*check_dependency_p=*/true,
6024 &template_p,
6025 /*declarator_p=*/false,
6026 /*optional_p=*/false);
6027 if (id_expression == error_mark_node)
6028 return error_mark_node;
6029 id_expr_token = token;
6030 token = cp_lexer_peek_token (parser->lexer);
6031 done = (token->type != CPP_OPEN_SQUARE
6032 && token->type != CPP_OPEN_PAREN
6033 && token->type != CPP_DOT
6034 && token->type != CPP_DEREF
6035 && token->type != CPP_PLUS_PLUS
6036 && token->type != CPP_MINUS_MINUS);
6037 /* If we have a template-id, then no further lookup is
6038 required. If the template-id was for a template-class, we
6039 will sometimes have a TYPE_DECL at this point. */
6040 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
6041 || TREE_CODE (id_expression) == TYPE_DECL)
6042 decl = id_expression;
6043 /* Look up the name. */
6044 else
6046 tree ambiguous_decls;
6048 /* If we already know that this lookup is ambiguous, then
6049 we've already issued an error message; there's no reason
6050 to check again. */
6051 if (id_expr_token->type == CPP_NAME
6052 && id_expr_token->error_reported)
6054 cp_parser_simulate_error (parser);
6055 return error_mark_node;
6058 decl = cp_parser_lookup_name (parser, id_expression,
6059 none_type,
6060 template_p,
6061 /*is_namespace=*/false,
6062 /*check_dependency=*/true,
6063 &ambiguous_decls,
6064 id_expression.get_location ());
6065 /* If the lookup was ambiguous, an error will already have
6066 been issued. */
6067 if (ambiguous_decls)
6068 return error_mark_node;
6070 /* In Objective-C++, we may have an Objective-C 2.0
6071 dot-syntax for classes here. */
6072 if (c_dialect_objc ()
6073 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
6074 && TREE_CODE (decl) == TYPE_DECL
6075 && objc_is_class_name (decl))
6077 tree component;
6078 cp_lexer_consume_token (parser->lexer);
6079 component = cp_parser_identifier (parser);
6080 if (component == error_mark_node)
6081 return error_mark_node;
6083 tree result = objc_build_class_component_ref (id_expression,
6084 component);
6085 /* Build a location of the form:
6086 expr.component
6087 ~~~~~^~~~~~~~~
6088 with caret at the start of the component name (at
6089 input_location), ranging from the start of the id_expression
6090 to the end of the component name. */
6091 location_t combined_loc
6092 = make_location (input_location, id_expression.get_start (),
6093 get_finish (input_location));
6094 protected_set_expr_location (result, combined_loc);
6095 return result;
6098 /* In Objective-C++, an instance variable (ivar) may be preferred
6099 to whatever cp_parser_lookup_name() found.
6100 Call objc_lookup_ivar. To avoid exposing cp_expr to the
6101 rest of c-family, we have to do a little extra work to preserve
6102 any location information in cp_expr "decl". Given that
6103 objc_lookup_ivar is implemented in "c-family" and "objc", we
6104 have a trip through the pure "tree" type, rather than cp_expr.
6105 Naively copying it back to "decl" would implicitly give the
6106 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
6107 store an EXPR_LOCATION. Hence we only update "decl" (and
6108 hence its location_t) if we get back a different tree node. */
6109 tree decl_tree = objc_lookup_ivar (decl.get_value (),
6110 id_expression);
6111 if (decl_tree != decl.get_value ())
6112 decl = cp_expr (decl_tree);
6114 /* If name lookup gives us a SCOPE_REF, then the
6115 qualifying scope was dependent. */
6116 if (TREE_CODE (decl) == SCOPE_REF)
6118 /* At this point, we do not know if DECL is a valid
6119 integral constant expression. We assume that it is
6120 in fact such an expression, so that code like:
6122 template <int N> struct A {
6123 int a[B<N>::i];
6126 is accepted. At template-instantiation time, we
6127 will check that B<N>::i is actually a constant. */
6128 return decl;
6130 /* Check to see if DECL is a local variable in a context
6131 where that is forbidden. */
6132 if ((parser->local_variables_forbidden_p & LOCAL_VARS_FORBIDDEN)
6133 && local_variable_p (decl)
6134 /* DR 2082 permits local variables in unevaluated contexts
6135 within a default argument. */
6136 && !cp_unevaluated_operand)
6138 const char *msg
6139 = (TREE_CODE (decl) == PARM_DECL
6140 ? _("parameter %qD may not appear in this context")
6141 : _("local variable %qD may not appear in this context"));
6142 error_at (id_expression.get_location (), msg,
6143 decl.get_value ());
6144 return error_mark_node;
6148 decl = (finish_id_expression
6149 (id_expression, decl, parser->scope,
6150 idk,
6151 parser->integral_constant_expression_p,
6152 parser->allow_non_integral_constant_expression_p,
6153 &parser->non_integral_constant_expression_p,
6154 template_p, done, address_p,
6155 template_arg_p,
6156 &error_msg,
6157 id_expression.get_location ()));
6158 if (error_msg)
6159 cp_parser_error (parser, error_msg);
6160 /* Build a location for an id-expression of the form:
6161 ::ns::id
6162 ~~~~~~^~
6166 i.e. from the start of the first token to the end of the final
6167 token, with the caret at the start of the unqualified-id. */
6168 location_t caret_loc = get_pure_location (id_expression.get_location ());
6169 location_t start_loc = get_start (id_expr_token->location);
6170 location_t finish_loc = get_finish (id_expression.get_location ());
6171 location_t combined_loc
6172 = make_location (caret_loc, start_loc, finish_loc);
6174 decl.set_location (combined_loc);
6175 return decl;
6178 /* Anything else is an error. */
6179 default:
6180 cp_parser_error (parser, "expected primary-expression");
6181 return error_mark_node;
6185 static inline cp_expr
6186 cp_parser_primary_expression (cp_parser *parser,
6187 bool address_p,
6188 bool cast_p,
6189 bool template_arg_p,
6190 cp_id_kind *idk)
6192 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
6193 /*decltype*/false, idk);
6196 /* Complain about missing template keyword when naming a dependent
6197 member template. */
6199 static void
6200 missing_template_diag (location_t loc, diagnostic_t diag_kind = DK_WARNING)
6202 if (warning_suppressed_at (loc, OPT_Wmissing_template_keyword))
6203 return;
6205 gcc_rich_location richloc (loc);
6206 richloc.add_fixit_insert_before ("template");
6207 emit_diagnostic (diag_kind, &richloc, OPT_Wmissing_template_keyword,
6208 "expected %qs keyword before dependent "
6209 "template name", "template");
6210 suppress_warning_at (loc, OPT_Wmissing_template_keyword);
6213 /* Parse an id-expression.
6215 id-expression:
6216 unqualified-id
6217 qualified-id
6219 qualified-id:
6220 :: [opt] nested-name-specifier template [opt] unqualified-id
6221 :: identifier
6222 :: operator-function-id
6223 :: template-id
6225 Return a representation of the unqualified portion of the
6226 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
6227 a `::' or nested-name-specifier.
6229 Often, if the id-expression was a qualified-id, the caller will
6230 want to make a SCOPE_REF to represent the qualified-id. This
6231 function does not do this in order to avoid wastefully creating
6232 SCOPE_REFs when they are not required.
6234 If TEMPLATE_KEYWORD_P is true, then we have just seen the
6235 `template' keyword.
6237 If CHECK_DEPENDENCY_P is false, then names are looked up inside
6238 uninstantiated templates.
6240 If *TEMPLATE_P is non-NULL, it is set to true iff the
6241 `template' keyword is used to explicitly indicate that the entity
6242 named is a template.
6244 If DECLARATOR_P is true, the id-expression is appearing as part of
6245 a declarator, rather than as part of an expression. */
6247 static cp_expr
6248 cp_parser_id_expression (cp_parser *parser,
6249 bool template_keyword_p,
6250 bool check_dependency_p,
6251 bool *template_p,
6252 bool declarator_p,
6253 bool optional_p)
6255 bool global_scope_p;
6256 bool nested_name_specifier_p;
6258 /* Assume the `template' keyword was not used. */
6259 if (template_p)
6260 *template_p = template_keyword_p;
6262 /* Look for the optional `::' operator. */
6263 global_scope_p
6264 = (!template_keyword_p
6265 && (cp_parser_global_scope_opt (parser,
6266 /*current_scope_valid_p=*/false)
6267 != NULL_TREE));
6269 /* Look for the optional nested-name-specifier. */
6270 nested_name_specifier_p
6271 = (cp_parser_nested_name_specifier_opt (parser,
6272 /*typename_keyword_p=*/false,
6273 check_dependency_p,
6274 /*type_p=*/false,
6275 declarator_p,
6276 template_keyword_p)
6277 != NULL_TREE);
6279 cp_expr id = NULL_TREE;
6280 tree scope = parser->scope;
6282 /* Peek at the next token. */
6283 cp_token *token = cp_lexer_peek_token (parser->lexer);
6285 /* If there is a nested-name-specifier, then we are looking at
6286 the first qualified-id production. */
6287 if (nested_name_specifier_p)
6289 tree saved_object_scope;
6290 tree saved_qualifying_scope;
6292 /* See if the next token is the `template' keyword. */
6293 if (!template_p)
6294 template_p = &template_keyword_p;
6295 *template_p = cp_parser_optional_template_keyword (parser);
6296 /* Name lookup we do during the processing of the
6297 unqualified-id might obliterate SCOPE. */
6298 saved_object_scope = parser->object_scope;
6299 saved_qualifying_scope = parser->qualifying_scope;
6300 /* Process the final unqualified-id. */
6301 id = cp_parser_unqualified_id (parser, *template_p,
6302 check_dependency_p,
6303 declarator_p,
6304 /*optional_p=*/false);
6305 /* Restore the SAVED_SCOPE for our caller. */
6306 parser->scope = scope;
6307 parser->object_scope = saved_object_scope;
6308 parser->qualifying_scope = saved_qualifying_scope;
6310 /* Otherwise, if we are in global scope, then we are looking at one
6311 of the other qualified-id productions. */
6312 else if (global_scope_p)
6314 /* If it's an identifier, and the next token is not a "<", then
6315 we can avoid the template-id case. This is an optimization
6316 for this common case. */
6317 if (token->type == CPP_NAME
6318 && !cp_parser_nth_token_starts_template_argument_list_p
6319 (parser, 2))
6320 return cp_parser_identifier (parser);
6322 cp_parser_parse_tentatively (parser);
6323 /* Try a template-id. */
6324 id = cp_parser_template_id_expr (parser,
6325 /*template_keyword_p=*/false,
6326 /*check_dependency_p=*/true,
6327 declarator_p);
6328 /* If that worked, we're done. */
6329 if (cp_parser_parse_definitely (parser))
6330 return id;
6332 /* Peek at the next token. (Changes in the token buffer may
6333 have invalidated the pointer obtained above.) */
6334 token = cp_lexer_peek_token (parser->lexer);
6336 switch (token->type)
6338 case CPP_NAME:
6339 id = cp_parser_identifier (parser);
6340 break;
6342 case CPP_KEYWORD:
6343 if (token->keyword == RID_OPERATOR)
6345 id = cp_parser_operator_function_id (parser);
6346 break;
6348 /* Fall through. */
6350 default:
6351 cp_parser_error (parser, "expected id-expression");
6352 return error_mark_node;
6355 else
6357 if (!scope)
6358 scope = parser->context->object_type;
6359 id = cp_parser_unqualified_id (parser, template_keyword_p,
6360 /*check_dependency_p=*/true,
6361 declarator_p,
6362 optional_p);
6365 if (id && TREE_CODE (id) == IDENTIFIER_NODE
6366 && warn_missing_template_keyword
6367 && !template_keyword_p
6368 /* Don't warn if we're looking inside templates. */
6369 && check_dependency_p
6370 /* In a template argument list a > could be closing
6371 the enclosing targs. */
6372 && !parser->in_template_argument_list_p
6373 && scope && dependentish_scope_p (scope)
6374 /* Don't confuse an ill-formed constructor declarator for a missing
6375 template keyword in a return type. */
6376 && !(declarator_p && constructor_name_p (id, scope))
6377 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1)
6378 && warning_enabled_at (token->location,
6379 OPT_Wmissing_template_keyword))
6381 saved_token_sentinel toks (parser->lexer, STS_ROLLBACK);
6382 if (cp_parser_skip_entire_template_parameter_list (parser)
6383 /* An operator after the > suggests that the > ends a
6384 template-id; a name or literal suggests that the > is an
6385 operator. */
6386 && (cp_lexer_peek_token (parser->lexer)->type
6387 <= CPP_LAST_PUNCTUATOR))
6388 missing_template_diag (token->location);
6391 return id;
6394 /* Parse an unqualified-id.
6396 unqualified-id:
6397 identifier
6398 operator-function-id
6399 conversion-function-id
6400 ~ class-name
6401 template-id
6403 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
6404 keyword, in a construct like `A::template ...'.
6406 Returns a representation of unqualified-id. For the `identifier'
6407 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
6408 production a BIT_NOT_EXPR is returned; the operand of the
6409 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
6410 other productions, see the documentation accompanying the
6411 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
6412 names are looked up in uninstantiated templates. If DECLARATOR_P
6413 is true, the unqualified-id is appearing as part of a declarator,
6414 rather than as part of an expression. */
6416 static cp_expr
6417 cp_parser_unqualified_id (cp_parser* parser,
6418 bool template_keyword_p,
6419 bool check_dependency_p,
6420 bool declarator_p,
6421 bool optional_p)
6423 cp_token *token;
6425 /* Peek at the next token. */
6426 token = cp_lexer_peek_token (parser->lexer);
6428 switch ((int) token->type)
6430 case CPP_NAME:
6432 tree id;
6434 /* We don't know yet whether or not this will be a
6435 template-id. */
6436 cp_parser_parse_tentatively (parser);
6437 /* Try a template-id. */
6438 id = cp_parser_template_id_expr (parser, template_keyword_p,
6439 check_dependency_p,
6440 declarator_p);
6441 /* If it worked, we're done. */
6442 if (cp_parser_parse_definitely (parser))
6443 return id;
6444 /* Otherwise, it's an ordinary identifier. */
6445 return cp_parser_identifier (parser);
6448 case CPP_TEMPLATE_ID:
6449 return cp_parser_template_id_expr (parser, template_keyword_p,
6450 check_dependency_p,
6451 declarator_p);
6453 case CPP_COMPL:
6455 tree type_decl;
6456 tree qualifying_scope;
6457 tree object_scope;
6458 tree scope;
6459 bool done;
6460 location_t tilde_loc = token->location;
6462 /* Consume the `~' token. */
6463 cp_lexer_consume_token (parser->lexer);
6464 /* Parse the class-name. The standard, as written, seems to
6465 say that:
6467 template <typename T> struct S { ~S (); };
6468 template <typename T> S<T>::~S() {}
6470 is invalid, since `~' must be followed by a class-name, but
6471 `S<T>' is dependent, and so not known to be a class.
6472 That's not right; we need to look in uninstantiated
6473 templates. A further complication arises from:
6475 template <typename T> void f(T t) {
6476 t.T::~T();
6479 Here, it is not possible to look up `T' in the scope of `T'
6480 itself. We must look in both the current scope, and the
6481 scope of the containing complete expression.
6483 Yet another issue is:
6485 struct S {
6486 int S;
6487 ~S();
6490 S::~S() {}
6492 The standard does not seem to say that the `S' in `~S'
6493 should refer to the type `S' and not the data member
6494 `S::S'. */
6496 /* DR 244 says that we look up the name after the "~" in the
6497 same scope as we looked up the qualifying name. That idea
6498 isn't fully worked out; it's more complicated than that. */
6499 scope = parser->scope;
6500 object_scope = parser->object_scope;
6501 qualifying_scope = parser->qualifying_scope;
6503 /* Check for invalid scopes. */
6504 if (scope == error_mark_node)
6506 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
6507 cp_lexer_consume_token (parser->lexer);
6508 return error_mark_node;
6510 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
6512 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6513 error_at (token->location,
6514 "scope %qT before %<~%> is not a class-name",
6515 scope);
6516 cp_parser_simulate_error (parser);
6517 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
6518 cp_lexer_consume_token (parser->lexer);
6519 return error_mark_node;
6521 if (template_keyword_p)
6523 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6524 error_at (tilde_loc, "%<template%> keyword not permitted in "
6525 "destructor name");
6526 cp_parser_simulate_error (parser);
6527 return error_mark_node;
6530 gcc_assert (!scope || TYPE_P (scope));
6532 token = cp_lexer_peek_token (parser->lexer);
6534 /* Create a location with caret == start at the tilde,
6535 finishing at the end of the peeked token, e.g:
6536 ~token
6537 ^~~~~~. */
6538 location_t loc
6539 = make_location (tilde_loc, tilde_loc, token->location);
6541 /* If the name is of the form "X::~X" it's OK even if X is a
6542 typedef. */
6544 if (scope
6545 && token->type == CPP_NAME
6546 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6547 != CPP_LESS)
6548 && (token->u.value == TYPE_IDENTIFIER (scope)
6549 || (CLASS_TYPE_P (scope)
6550 && constructor_name_p (token->u.value, scope))))
6552 cp_lexer_consume_token (parser->lexer);
6553 return build_min_nt_loc (loc, BIT_NOT_EXPR, scope);
6556 /* ~auto means the destructor of whatever the object is. */
6557 if (cp_parser_is_keyword (token, RID_AUTO))
6559 if (cxx_dialect < cxx14)
6560 pedwarn (loc, OPT_Wc__14_extensions,
6561 "%<~auto%> only available with "
6562 "%<-std=c++14%> or %<-std=gnu++14%>");
6563 cp_lexer_consume_token (parser->lexer);
6564 return build_min_nt_loc (loc, BIT_NOT_EXPR, make_auto ());
6567 /* DR 2237 (C++20 only): A simple-template-id is no longer valid as the
6568 declarator-id of a constructor or destructor. */
6569 if (token->type == CPP_TEMPLATE_ID && declarator_p
6570 && cxx_dialect >= cxx20)
6572 if (!cp_parser_simulate_error (parser))
6573 error_at (tilde_loc, "template-id not allowed for destructor");
6574 return error_mark_node;
6577 /* If there was an explicit qualification (S::~T), first look
6578 in the scope given by the qualification (i.e., S).
6580 Note: in the calls to cp_parser_class_name below we pass
6581 typename_type so that lookup finds the injected-class-name
6582 rather than the constructor. */
6583 done = false;
6584 type_decl = NULL_TREE;
6585 if (scope)
6587 cp_parser_parse_tentatively (parser);
6588 type_decl = cp_parser_class_name (parser,
6589 /*typename_keyword_p=*/false,
6590 /*template_keyword_p=*/false,
6591 typename_type,
6592 /*check_dependency=*/false,
6593 /*class_head_p=*/false,
6594 declarator_p);
6595 if (cp_parser_parse_definitely (parser))
6596 done = true;
6598 /* In "N::S::~S", look in "N" as well. */
6599 if (!done && scope && qualifying_scope)
6601 cp_parser_parse_tentatively (parser);
6602 parser->scope = qualifying_scope;
6603 parser->object_scope = NULL_TREE;
6604 parser->qualifying_scope = NULL_TREE;
6605 type_decl
6606 = cp_parser_class_name (parser,
6607 /*typename_keyword_p=*/false,
6608 /*template_keyword_p=*/false,
6609 typename_type,
6610 /*check_dependency=*/false,
6611 /*class_head_p=*/false,
6612 declarator_p);
6613 if (cp_parser_parse_definitely (parser))
6614 done = true;
6616 /* In "p->S::~T", look in the scope given by "*p" as well. */
6617 else if (!done && object_scope)
6619 cp_parser_parse_tentatively (parser);
6620 parser->scope = object_scope;
6621 parser->object_scope = NULL_TREE;
6622 parser->qualifying_scope = NULL_TREE;
6623 type_decl
6624 = cp_parser_class_name (parser,
6625 /*typename_keyword_p=*/false,
6626 /*template_keyword_p=*/false,
6627 typename_type,
6628 /*check_dependency=*/false,
6629 /*class_head_p=*/false,
6630 declarator_p);
6631 if (cp_parser_parse_definitely (parser))
6632 done = true;
6634 /* Look in the surrounding context. */
6635 if (!done)
6637 parser->scope = NULL_TREE;
6638 parser->object_scope = NULL_TREE;
6639 parser->qualifying_scope = NULL_TREE;
6640 if (processing_template_decl)
6641 cp_parser_parse_tentatively (parser);
6642 type_decl
6643 = cp_parser_class_name (parser,
6644 /*typename_keyword_p=*/false,
6645 /*template_keyword_p=*/false,
6646 typename_type,
6647 /*check_dependency=*/false,
6648 /*class_head_p=*/false,
6649 declarator_p);
6650 if (processing_template_decl
6651 && ! cp_parser_parse_definitely (parser))
6653 /* We couldn't find a type with this name. If we're parsing
6654 tentatively, fail and try something else. */
6655 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6657 cp_parser_simulate_error (parser);
6658 return error_mark_node;
6660 /* Otherwise, accept it and check for a match at instantiation
6661 time. */
6662 type_decl = cp_parser_identifier (parser);
6663 if (type_decl != error_mark_node)
6664 type_decl = build_min_nt_loc (loc, BIT_NOT_EXPR, type_decl);
6665 return type_decl;
6668 /* If an error occurred, assume that the name of the
6669 destructor is the same as the name of the qualifying
6670 class. That allows us to keep parsing after running
6671 into ill-formed destructor names. */
6672 if (type_decl == error_mark_node && scope)
6673 return build_min_nt_loc (loc, BIT_NOT_EXPR, scope);
6674 else if (type_decl == error_mark_node)
6675 return error_mark_node;
6677 /* Check that destructor name and scope match. */
6678 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6680 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6681 error_at (loc,
6682 "declaration of %<~%T%> as member of %qT",
6683 type_decl, scope);
6684 cp_parser_simulate_error (parser);
6685 return error_mark_node;
6688 /* [class.dtor]
6690 A typedef-name that names a class shall not be used as the
6691 identifier in the declarator for a destructor declaration. */
6692 if (declarator_p
6693 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6694 && !DECL_SELF_REFERENCE_P (type_decl)
6695 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6696 error_at (loc,
6697 "typedef-name %qD used as destructor declarator",
6698 type_decl);
6700 return build_min_nt_loc (loc, BIT_NOT_EXPR, TREE_TYPE (type_decl));
6703 case CPP_KEYWORD:
6704 if (token->keyword == RID_OPERATOR)
6706 cp_expr id;
6708 /* This could be a template-id, so we try that first. */
6709 cp_parser_parse_tentatively (parser);
6710 /* Try a template-id. */
6711 id = cp_parser_template_id_expr (parser, template_keyword_p,
6712 /*check_dependency_p=*/true,
6713 declarator_p);
6714 /* If that worked, we're done. */
6715 if (cp_parser_parse_definitely (parser))
6716 return id;
6717 /* We still don't know whether we're looking at an
6718 operator-function-id or a conversion-function-id. */
6719 cp_parser_parse_tentatively (parser);
6720 /* Try an operator-function-id. */
6721 id = cp_parser_operator_function_id (parser);
6722 /* If that didn't work, try a conversion-function-id. */
6723 if (!cp_parser_parse_definitely (parser))
6724 id = cp_parser_conversion_function_id (parser);
6726 return id;
6728 /* Fall through. */
6730 default:
6731 if (optional_p)
6732 return NULL_TREE;
6733 cp_parser_error (parser, "expected unqualified-id");
6734 return error_mark_node;
6738 /* Check [temp.names]/5: A name prefixed by the keyword template shall
6739 be a template-id or the name shall refer to a class template or an
6740 alias template. */
6742 static void
6743 check_template_keyword_in_nested_name_spec (tree name)
6745 if (CLASS_TYPE_P (name)
6746 && ((CLASSTYPE_USE_TEMPLATE (name)
6747 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (name)))
6748 || CLASSTYPE_IS_TEMPLATE (name)))
6749 return;
6751 if (TREE_CODE (name) == TYPENAME_TYPE
6752 && TREE_CODE (TYPENAME_TYPE_FULLNAME (name)) == TEMPLATE_ID_EXPR)
6753 return;
6754 /* Alias templates are also OK. */
6755 else if (alias_template_specialization_p (name, nt_opaque))
6756 return;
6758 permerror (input_location, TYPE_P (name)
6759 ? G_("%qT is not a template")
6760 : G_("%qD is not a template"),
6761 name);
6764 /* Parse an (optional) nested-name-specifier.
6766 nested-name-specifier: [C++98]
6767 class-or-namespace-name :: nested-name-specifier [opt]
6768 class-or-namespace-name :: template nested-name-specifier [opt]
6770 nested-name-specifier: [C++0x]
6771 type-name ::
6772 namespace-name ::
6773 nested-name-specifier identifier ::
6774 nested-name-specifier template [opt] simple-template-id ::
6776 PARSER->SCOPE should be set appropriately before this function is
6777 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6778 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6779 in name lookups.
6781 Sets PARSER->SCOPE to the class (TYPE) or namespace
6782 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6783 it unchanged if there is no nested-name-specifier. Returns the new
6784 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6786 If CHECK_DEPENDENCY_P is FALSE, names are looked up in dependent scopes.
6788 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6789 part of a declaration and/or decl-specifier. */
6791 static tree
6792 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6793 bool typename_keyword_p,
6794 bool check_dependency_p,
6795 bool type_p,
6796 bool is_declaration,
6797 bool template_keyword_p /* = false */)
6799 bool success = false;
6800 cp_token_position start = 0;
6801 cp_token *token;
6803 /* Remember where the nested-name-specifier starts. */
6804 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
6805 && cp_lexer_next_token_is_not (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
6807 start = cp_lexer_token_position (parser->lexer, false);
6808 push_deferring_access_checks (dk_deferred);
6811 while (true)
6813 tree new_scope;
6814 tree old_scope;
6815 tree saved_qualifying_scope;
6817 /* Spot cases that cannot be the beginning of a
6818 nested-name-specifier. */
6819 token = cp_lexer_peek_token (parser->lexer);
6821 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6822 the already parsed nested-name-specifier. */
6823 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6825 /* Grab the nested-name-specifier and continue the loop. */
6826 cp_parser_pre_parsed_nested_name_specifier (parser);
6827 /* If we originally encountered this nested-name-specifier
6828 with CHECK_DEPENDENCY_P set to true, we will not have
6829 resolved TYPENAME_TYPEs, so we must do so here. */
6830 if (is_declaration
6831 && !check_dependency_p
6832 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6834 new_scope = resolve_typename_type (parser->scope,
6835 /*only_current_p=*/false);
6836 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6837 parser->scope = new_scope;
6839 success = true;
6840 continue;
6843 /* Spot cases that cannot be the beginning of a
6844 nested-name-specifier. On the second and subsequent times
6845 through the loop, we look for the `template' keyword. */
6846 if (success && token->keyword == RID_TEMPLATE)
6848 /* A template-id can start a nested-name-specifier. */
6849 else if (token->type == CPP_TEMPLATE_ID)
6851 /* DR 743: decltype can be used in a nested-name-specifier. */
6852 else if (token_is_decltype (token))
6854 else
6856 /* If the next token is not an identifier, then it is
6857 definitely not a type-name or namespace-name. */
6858 if (token->type != CPP_NAME)
6859 break;
6860 /* If the following token is neither a `<' (to begin a
6861 template-id), nor a `::', then we are not looking at a
6862 nested-name-specifier. */
6863 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6865 if (token->type == CPP_COLON
6866 && parser->colon_corrects_to_scope_p
6867 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME
6868 /* name:name is a valid sequence in an Objective C message. */
6869 && !parser->objective_c_message_context_p)
6871 gcc_rich_location richloc (token->location);
6872 richloc.add_fixit_replace ("::");
6873 error_at (&richloc,
6874 "found %<:%> in nested-name-specifier, "
6875 "expected %<::%>");
6876 token->type = CPP_SCOPE;
6879 if (token->type != CPP_SCOPE
6880 && !cp_parser_nth_token_starts_template_argument_list_p
6881 (parser, 2))
6882 break;
6885 /* The nested-name-specifier is optional, so we parse
6886 tentatively. */
6887 cp_parser_parse_tentatively (parser);
6889 /* Look for the optional `template' keyword, if this isn't the
6890 first time through the loop. */
6891 if (success)
6893 template_keyword_p = cp_parser_optional_template_keyword (parser);
6894 /* DR1710: "In a qualified-id used as the name in
6895 a typename-specifier, elaborated-type-specifier, using-declaration,
6896 or class-or-decltype, an optional keyword template appearing at
6897 the top level is ignored." */
6898 if (!template_keyword_p
6899 && typename_keyword_p
6900 && cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
6901 template_keyword_p = true;
6904 /* Save the old scope since the name lookup we are about to do
6905 might destroy it. */
6906 old_scope = parser->scope;
6907 saved_qualifying_scope = parser->qualifying_scope;
6908 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6909 look up names in "X<T>::I" in order to determine that "Y" is
6910 a template. So, if we have a typename at this point, we make
6911 an effort to look through it. */
6912 if (is_declaration
6913 && !check_dependency_p
6914 && !typename_keyword_p
6915 && parser->scope
6916 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6917 parser->scope = resolve_typename_type (parser->scope,
6918 /*only_current_p=*/false);
6919 /* Parse the qualifying entity. */
6920 new_scope
6921 = cp_parser_qualifying_entity (parser,
6922 typename_keyword_p,
6923 template_keyword_p,
6924 check_dependency_p,
6925 type_p,
6926 is_declaration);
6927 /* Look for the `::' token. */
6928 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6930 /* If we found what we wanted, we keep going; otherwise, we're
6931 done. */
6932 if (!cp_parser_parse_definitely (parser))
6934 bool error_p = false;
6936 /* Restore the OLD_SCOPE since it was valid before the
6937 failed attempt at finding the last
6938 class-or-namespace-name. */
6939 parser->scope = old_scope;
6940 parser->qualifying_scope = saved_qualifying_scope;
6942 /* If the next token is a decltype, and the one after that is a
6943 `::', then the decltype has failed to resolve to a class or
6944 enumeration type. Give this error even when parsing
6945 tentatively since it can't possibly be valid--and we're going
6946 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6947 won't get another chance.*/
6948 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6949 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6950 == CPP_SCOPE))
6952 token = cp_lexer_consume_token (parser->lexer);
6953 tree dtype = token->u.tree_check_value->value;
6954 if (dtype != error_mark_node)
6955 error_at (token->location, "%<decltype%> evaluates to %qT, "
6956 "which is not a class or enumeration type",
6957 dtype);
6958 parser->scope = error_mark_node;
6959 error_p = true;
6960 /* As below. */
6961 success = true;
6962 cp_lexer_consume_token (parser->lexer);
6965 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6966 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6968 /* If we have a non-type template-id followed by ::, it can't
6969 possibly be valid. */
6970 token = cp_lexer_peek_token (parser->lexer);
6971 tree tid = token->u.tree_check_value->value;
6972 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6973 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6975 tree tmpl = NULL_TREE;
6976 if (is_overloaded_fn (tid))
6978 tree fns = get_fns (tid);
6979 if (OVL_SINGLE_P (fns))
6980 tmpl = OVL_FIRST (fns);
6981 if (function_concept_p (fns))
6982 error_at (token->location, "concept-id %qD "
6983 "in nested-name-specifier", tid);
6984 else
6985 error_at (token->location, "function template-id "
6986 "%qD in nested-name-specifier", tid);
6988 else
6990 tmpl = TREE_OPERAND (tid, 0);
6991 if (variable_concept_p (tmpl)
6992 || standard_concept_p (tmpl))
6993 error_at (token->location, "concept-id %qD "
6994 "in nested-name-specifier", tid);
6995 else
6997 /* Variable template. */
6998 gcc_assert (variable_template_p (tmpl));
6999 error_at (token->location, "variable template-id "
7000 "%qD in nested-name-specifier", tid);
7003 if (tmpl)
7004 inform (DECL_SOURCE_LOCATION (tmpl),
7005 "%qD declared here", tmpl);
7007 parser->scope = error_mark_node;
7008 error_p = true;
7009 /* As below. */
7010 success = true;
7011 cp_lexer_consume_token (parser->lexer);
7012 cp_lexer_consume_token (parser->lexer);
7016 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
7017 break;
7018 /* If the next token is an identifier, and the one after
7019 that is a `::', then any valid interpretation would have
7020 found a class-or-namespace-name. */
7021 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
7022 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
7023 == CPP_SCOPE)
7024 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7025 != CPP_COMPL))
7027 token = cp_lexer_consume_token (parser->lexer);
7028 if (!error_p)
7030 if (!token->error_reported)
7032 tree decl;
7033 tree ambiguous_decls;
7035 decl = cp_parser_lookup_name (parser, token->u.value,
7036 none_type,
7037 /*is_template=*/false,
7038 /*is_namespace=*/false,
7039 /*check_dependency=*/true,
7040 &ambiguous_decls,
7041 token->location);
7042 if (TREE_CODE (decl) == TEMPLATE_DECL)
7043 error_at (token->location,
7044 "%qD used without template arguments",
7045 decl);
7046 else if (ambiguous_decls)
7048 // cp_parser_lookup_name has the same diagnostic,
7049 // thus make sure to emit it at most once.
7050 if (cp_parser_uncommitted_to_tentative_parse_p
7051 (parser))
7053 error_at (token->location,
7054 "reference to %qD is ambiguous",
7055 token->u.value);
7056 print_candidates (ambiguous_decls);
7058 decl = error_mark_node;
7060 else
7062 if (cxx_dialect != cxx98)
7063 cp_parser_name_lookup_error
7064 (parser, token->u.value, decl, NLE_NOT_CXX98,
7065 token->location);
7066 else
7067 cp_parser_name_lookup_error
7068 (parser, token->u.value, decl, NLE_CXX98,
7069 token->location);
7072 parser->scope = error_mark_node;
7073 error_p = true;
7074 /* Treat this as a successful nested-name-specifier
7075 due to:
7077 [basic.lookup.qual]
7079 If the name found is not a class-name (clause
7080 _class_) or namespace-name (_namespace.def_), the
7081 program is ill-formed. */
7082 success = true;
7084 cp_lexer_consume_token (parser->lexer);
7086 break;
7088 /* We've found one valid nested-name-specifier. */
7089 success = true;
7090 /* Name lookup always gives us a DECL. */
7091 if (TREE_CODE (new_scope) == TYPE_DECL)
7092 new_scope = TREE_TYPE (new_scope);
7093 /* Uses of "template" must be followed by actual templates. */
7094 if (template_keyword_p)
7095 check_template_keyword_in_nested_name_spec (new_scope);
7096 /* If it is a class scope, try to complete it; we are about to
7097 be looking up names inside the class. */
7098 if (TYPE_P (new_scope)
7099 /* Since checking types for dependency can be expensive,
7100 avoid doing it if the type is already complete. */
7101 && !COMPLETE_TYPE_P (new_scope)
7102 /* Do not try to complete dependent types. */
7103 && !dependent_type_p (new_scope))
7105 new_scope = complete_type (new_scope);
7106 /* If it is a typedef to current class, use the current
7107 class instead, as the typedef won't have any names inside
7108 it yet. */
7109 if (!COMPLETE_TYPE_P (new_scope)
7110 && currently_open_class (new_scope))
7111 new_scope = TYPE_MAIN_VARIANT (new_scope);
7113 /* Make sure we look in the right scope the next time through
7114 the loop. */
7115 parser->scope = new_scope;
7118 /* If parsing tentatively, replace the sequence of tokens that makes
7119 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
7120 token. That way, should we re-parse the token stream, we will
7121 not have to repeat the effort required to do the parse, nor will
7122 we issue duplicate error messages. */
7123 if (success && start)
7125 cp_token *token;
7127 token = cp_lexer_token_at (parser->lexer, start);
7128 /* Reset the contents of the START token. */
7129 token->type = CPP_NESTED_NAME_SPECIFIER;
7130 /* Retrieve any deferred checks. Do not pop this access checks yet
7131 so the memory will not be reclaimed during token replacing below. */
7132 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
7133 token->tree_check_p = true;
7134 token->u.tree_check_value->value = parser->scope;
7135 token->u.tree_check_value->checks = get_deferred_access_checks ();
7136 token->u.tree_check_value->qualifying_scope =
7137 parser->qualifying_scope;
7138 token->keyword = RID_MAX;
7140 /* Purge all subsequent tokens. */
7141 cp_lexer_purge_tokens_after (parser->lexer, start);
7144 if (start)
7145 pop_to_parent_deferring_access_checks ();
7147 return success ? parser->scope : NULL_TREE;
7150 /* Parse a nested-name-specifier. See
7151 cp_parser_nested_name_specifier_opt for details. This function
7152 behaves identically, except that it will an issue an error if no
7153 nested-name-specifier is present. */
7155 static tree
7156 cp_parser_nested_name_specifier (cp_parser *parser,
7157 bool typename_keyword_p,
7158 bool check_dependency_p,
7159 bool type_p,
7160 bool is_declaration)
7162 tree scope;
7164 /* Look for the nested-name-specifier. */
7165 scope = cp_parser_nested_name_specifier_opt (parser,
7166 typename_keyword_p,
7167 check_dependency_p,
7168 type_p,
7169 is_declaration);
7170 /* If it was not present, issue an error message. */
7171 if (!scope)
7173 cp_parser_error (parser, "expected nested-name-specifier");
7174 parser->scope = NULL_TREE;
7177 return scope;
7180 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
7181 this is either a class-name or a namespace-name (which corresponds
7182 to the class-or-namespace-name production in the grammar). For
7183 C++0x, it can also be a type-name that refers to an enumeration
7184 type or a simple-template-id.
7186 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
7187 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
7188 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
7189 TYPE_P is TRUE iff the next name should be taken as a class-name,
7190 even the same name is declared to be another entity in the same
7191 scope.
7193 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
7194 specified by the class-or-namespace-name. If neither is found the
7195 ERROR_MARK_NODE is returned. */
7197 static tree
7198 cp_parser_qualifying_entity (cp_parser *parser,
7199 bool typename_keyword_p,
7200 bool template_keyword_p,
7201 bool check_dependency_p,
7202 bool type_p,
7203 bool is_declaration)
7205 tree saved_scope;
7206 tree saved_qualifying_scope;
7207 tree saved_object_scope;
7208 tree scope;
7209 bool only_class_p;
7210 bool successful_parse_p;
7212 /* DR 743: decltype can appear in a nested-name-specifier. */
7213 if (cp_lexer_next_token_is_decltype (parser->lexer))
7215 scope = cp_parser_decltype (parser);
7216 if (TREE_CODE (scope) != ENUMERAL_TYPE
7217 && !MAYBE_CLASS_TYPE_P (scope))
7219 cp_parser_simulate_error (parser);
7220 return error_mark_node;
7222 if (TYPE_NAME (scope))
7223 scope = TYPE_NAME (scope);
7224 return scope;
7227 /* Before we try to parse the class-name, we must save away the
7228 current PARSER->SCOPE since cp_parser_class_name will destroy
7229 it. */
7230 saved_scope = parser->scope;
7231 saved_qualifying_scope = parser->qualifying_scope;
7232 saved_object_scope = parser->object_scope;
7233 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
7234 there is no need to look for a namespace-name. */
7235 only_class_p = template_keyword_p
7236 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
7237 if (!only_class_p)
7238 cp_parser_parse_tentatively (parser);
7239 scope = cp_parser_class_name (parser,
7240 typename_keyword_p,
7241 template_keyword_p,
7242 type_p ? class_type : none_type,
7243 check_dependency_p,
7244 /*class_head_p=*/false,
7245 is_declaration,
7246 /*enum_ok=*/cxx_dialect > cxx98);
7247 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
7248 /* If that didn't work, try for a namespace-name. */
7249 if (!only_class_p && !successful_parse_p)
7251 /* Restore the saved scope. */
7252 parser->scope = saved_scope;
7253 parser->qualifying_scope = saved_qualifying_scope;
7254 parser->object_scope = saved_object_scope;
7255 /* If we are not looking at an identifier followed by the scope
7256 resolution operator, then this is not part of a
7257 nested-name-specifier. (Note that this function is only used
7258 to parse the components of a nested-name-specifier.) */
7259 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
7260 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
7261 return error_mark_node;
7262 scope = cp_parser_namespace_name (parser);
7265 return scope;
7268 /* Return true if we are looking at a compound-literal, false otherwise. */
7270 static bool
7271 cp_parser_compound_literal_p (cp_parser *parser)
7273 cp_lexer_save_tokens (parser->lexer);
7275 /* Skip tokens until the next token is a closing parenthesis.
7276 If we find the closing `)', and the next token is a `{', then
7277 we are looking at a compound-literal. */
7278 bool compound_literal_p
7279 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7280 /*consume_paren=*/true)
7281 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
7283 /* Roll back the tokens we skipped. */
7284 cp_lexer_rollback_tokens (parser->lexer);
7286 return compound_literal_p;
7289 /* Return true if EXPR is the integer constant zero or a complex constant
7290 of zero, without any folding, but ignoring location wrappers. */
7292 bool
7293 literal_integer_zerop (const_tree expr)
7295 return (location_wrapper_p (expr)
7296 && integer_zerop (TREE_OPERAND (expr, 0)));
7299 /* Parse a postfix-expression.
7301 postfix-expression:
7302 primary-expression
7303 postfix-expression [ expression ]
7304 postfix-expression ( expression-list [opt] )
7305 simple-type-specifier ( expression-list [opt] )
7306 typename :: [opt] nested-name-specifier identifier
7307 ( expression-list [opt] )
7308 typename :: [opt] nested-name-specifier template [opt] template-id
7309 ( expression-list [opt] )
7310 postfix-expression . template [opt] id-expression
7311 postfix-expression -> template [opt] id-expression
7312 postfix-expression . pseudo-destructor-name
7313 postfix-expression -> pseudo-destructor-name
7314 postfix-expression ++
7315 postfix-expression --
7316 dynamic_cast < type-id > ( expression )
7317 static_cast < type-id > ( expression )
7318 reinterpret_cast < type-id > ( expression )
7319 const_cast < type-id > ( expression )
7320 typeid ( expression )
7321 typeid ( type-id )
7323 GNU Extension:
7325 postfix-expression:
7326 ( type-id ) { initializer-list , [opt] }
7328 This extension is a GNU version of the C99 compound-literal
7329 construct. (The C99 grammar uses `type-name' instead of `type-id',
7330 but they are essentially the same concept.)
7332 If ADDRESS_P is true, the postfix expression is the operand of the
7333 `&' operator. CAST_P is true if this expression is the target of a
7334 cast.
7336 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
7337 class member access expressions [expr.ref].
7339 Returns a representation of the expression. */
7341 static cp_expr
7342 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
7343 bool member_access_only_p, bool decltype_p,
7344 cp_id_kind * pidk_return)
7346 cp_token *token;
7347 location_t loc;
7348 enum rid keyword;
7349 cp_id_kind idk = CP_ID_KIND_NONE;
7350 cp_expr postfix_expression = NULL_TREE;
7351 bool is_member_access = false;
7353 /* Peek at the next token. */
7354 token = cp_lexer_peek_token (parser->lexer);
7355 loc = token->location;
7356 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
7358 /* Some of the productions are determined by keywords. */
7359 keyword = token->keyword;
7360 switch (keyword)
7362 case RID_DYNCAST:
7363 case RID_STATCAST:
7364 case RID_REINTCAST:
7365 case RID_CONSTCAST:
7367 tree type;
7368 cp_expr expression;
7369 const char *saved_message;
7370 bool saved_in_type_id_in_expr_p;
7372 /* All of these can be handled in the same way from the point
7373 of view of parsing. Begin by consuming the token
7374 identifying the cast. */
7375 cp_lexer_consume_token (parser->lexer);
7377 /* New types cannot be defined in the cast. */
7378 saved_message = parser->type_definition_forbidden_message;
7379 parser->type_definition_forbidden_message
7380 = G_("types may not be defined in casts");
7382 /* Look for the opening `<'. */
7383 cp_parser_require (parser, CPP_LESS, RT_LESS);
7384 /* Parse the type to which we are casting. */
7385 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7386 parser->in_type_id_in_expr_p = true;
7387 type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
7388 NULL);
7389 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7390 /* Look for the closing `>'. */
7391 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
7392 /* Restore the old message. */
7393 parser->type_definition_forbidden_message = saved_message;
7395 bool saved_greater_than_is_operator_p
7396 = parser->greater_than_is_operator_p;
7397 parser->greater_than_is_operator_p = true;
7399 /* And the expression which is being cast. */
7400 matching_parens parens;
7401 parens.require_open (parser);
7402 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
7403 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
7404 RT_CLOSE_PAREN);
7405 location_t end_loc = close_paren ?
7406 close_paren->location : UNKNOWN_LOCATION;
7408 parser->greater_than_is_operator_p
7409 = saved_greater_than_is_operator_p;
7411 /* Only type conversions to integral or enumeration types
7412 can be used in constant-expressions. */
7413 if (!cast_valid_in_integral_constant_expression_p (type)
7414 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
7416 postfix_expression = error_mark_node;
7417 break;
7420 /* Construct a location e.g. :
7421 reinterpret_cast <int *> (expr)
7422 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7423 ranging from the start of the "*_cast" token to the final closing
7424 paren, with the caret at the start. */
7425 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
7427 switch (keyword)
7429 case RID_DYNCAST:
7430 postfix_expression
7431 = build_dynamic_cast (cp_cast_loc, type, expression,
7432 tf_warning_or_error);
7433 break;
7434 case RID_STATCAST:
7435 postfix_expression
7436 = build_static_cast (cp_cast_loc, type, expression,
7437 tf_warning_or_error);
7438 break;
7439 case RID_REINTCAST:
7440 postfix_expression
7441 = build_reinterpret_cast (cp_cast_loc, type, expression,
7442 tf_warning_or_error);
7443 break;
7444 case RID_CONSTCAST:
7445 postfix_expression
7446 = build_const_cast (cp_cast_loc, type, expression,
7447 tf_warning_or_error);
7448 break;
7449 default:
7450 gcc_unreachable ();
7453 break;
7455 case RID_TYPEID:
7457 tree type;
7458 const char *saved_message;
7459 bool saved_in_type_id_in_expr_p;
7461 /* Consume the `typeid' token. */
7462 cp_lexer_consume_token (parser->lexer);
7463 /* Look for the `(' token. */
7464 matching_parens parens;
7465 parens.require_open (parser);
7466 /* Types cannot be defined in a `typeid' expression. */
7467 saved_message = parser->type_definition_forbidden_message;
7468 parser->type_definition_forbidden_message
7469 = G_("types may not be defined in a %<typeid%> expression");
7470 /* We can't be sure yet whether we're looking at a type-id or an
7471 expression. */
7472 cp_parser_parse_tentatively (parser);
7473 /* Try a type-id first. */
7474 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7475 parser->in_type_id_in_expr_p = true;
7476 type = cp_parser_type_id (parser);
7477 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7478 /* Look for the `)' token. Otherwise, we can't be sure that
7479 we're not looking at an expression: consider `typeid (int
7480 (3))', for example. */
7481 cp_token *close_paren = parens.require_close (parser);
7482 /* If all went well, simply lookup the type-id. */
7483 if (cp_parser_parse_definitely (parser))
7484 postfix_expression = get_typeid (type, tf_warning_or_error);
7485 /* Otherwise, fall back to the expression variant. */
7486 else
7488 tree expression;
7490 /* Look for an expression. */
7491 expression = cp_parser_expression (parser, & idk);
7492 /* Compute its typeid. */
7493 postfix_expression = build_typeid (expression, tf_warning_or_error);
7494 /* Look for the `)' token. */
7495 close_paren = parens.require_close (parser);
7497 /* Restore the saved message. */
7498 parser->type_definition_forbidden_message = saved_message;
7499 /* `typeid' may not appear in an integral constant expression. */
7500 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
7501 postfix_expression = error_mark_node;
7503 /* Construct a location e.g. :
7504 typeid (expr)
7505 ^~~~~~~~~~~~~
7506 ranging from the start of the "typeid" token to the final closing
7507 paren, with the caret at the start. */
7508 if (close_paren)
7510 location_t typeid_loc
7511 = make_location (start_loc, start_loc, close_paren->location);
7512 postfix_expression.set_location (typeid_loc);
7513 postfix_expression.maybe_add_location_wrapper ();
7516 break;
7518 case RID_TYPENAME:
7520 tree type;
7521 /* The syntax permitted here is the same permitted for an
7522 elaborated-type-specifier. */
7523 ++parser->prevent_constrained_type_specifiers;
7524 type = cp_parser_elaborated_type_specifier (parser,
7525 /*is_friend=*/false,
7526 /*is_declaration=*/false);
7527 --parser->prevent_constrained_type_specifiers;
7528 postfix_expression = cp_parser_functional_cast (parser, type);
7530 break;
7532 case RID_ADDRESSOF:
7533 case RID_BUILTIN_SHUFFLE:
7534 case RID_BUILTIN_SHUFFLEVECTOR:
7535 case RID_BUILTIN_LAUNDER:
7536 case RID_BUILTIN_ASSOC_BARRIER:
7538 vec<tree, va_gc> *vec;
7540 cp_lexer_consume_token (parser->lexer);
7541 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
7542 /*cast_p=*/false, /*allow_expansion_p=*/true,
7543 /*non_constant_p=*/NULL);
7544 if (vec == NULL)
7546 postfix_expression = error_mark_node;
7547 break;
7550 for (tree p : *vec)
7551 mark_exp_read (p);
7553 switch (keyword)
7555 case RID_ADDRESSOF:
7556 if (vec->length () == 1)
7557 postfix_expression
7558 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
7559 else
7561 error_at (loc, "wrong number of arguments to "
7562 "%<__builtin_addressof%>");
7563 postfix_expression = error_mark_node;
7565 break;
7567 case RID_BUILTIN_LAUNDER:
7568 if (vec->length () == 1)
7569 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
7570 tf_warning_or_error);
7571 else
7573 error_at (loc, "wrong number of arguments to "
7574 "%<__builtin_launder%>");
7575 postfix_expression = error_mark_node;
7577 break;
7579 case RID_BUILTIN_ASSOC_BARRIER:
7580 if (vec->length () == 1)
7581 postfix_expression = build1_loc (loc, PAREN_EXPR,
7582 TREE_TYPE ((*vec)[0]),
7583 (*vec)[0]);
7584 else
7586 error_at (loc, "wrong number of arguments to "
7587 "%<__builtin_assoc_barrier%>");
7588 postfix_expression = error_mark_node;
7590 break;
7592 case RID_BUILTIN_SHUFFLE:
7593 if (vec->length () == 2)
7594 postfix_expression
7595 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
7596 (*vec)[1], tf_warning_or_error);
7597 else if (vec->length () == 3)
7598 postfix_expression
7599 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
7600 (*vec)[2], tf_warning_or_error);
7601 else
7603 error_at (loc, "wrong number of arguments to "
7604 "%<__builtin_shuffle%>");
7605 postfix_expression = error_mark_node;
7607 break;
7609 case RID_BUILTIN_SHUFFLEVECTOR:
7610 if (vec->length () < 3)
7612 error_at (loc, "wrong number of arguments to "
7613 "%<__builtin_shufflevector%>");
7614 postfix_expression = error_mark_node;
7616 else
7618 postfix_expression
7619 = build_x_shufflevector (loc, vec, tf_warning_or_error);
7621 break;
7623 default:
7624 gcc_unreachable ();
7626 break;
7629 case RID_BUILTIN_CONVERTVECTOR:
7631 tree expression;
7632 tree type;
7633 /* Consume the `__builtin_convertvector' token. */
7634 cp_lexer_consume_token (parser->lexer);
7635 /* Look for the opening `('. */
7636 matching_parens parens;
7637 parens.require_open (parser);
7638 /* Now, parse the assignment-expression. */
7639 expression = cp_parser_assignment_expression (parser);
7640 /* Look for the `,'. */
7641 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7642 location_t type_location
7643 = cp_lexer_peek_token (parser->lexer)->location;
7644 /* Parse the type-id. */
7646 type_id_in_expr_sentinel s (parser);
7647 type = cp_parser_type_id (parser);
7649 /* Look for the closing `)'. */
7650 parens.require_close (parser);
7651 postfix_expression
7652 = cp_build_vec_convert (expression, type_location, type,
7653 tf_warning_or_error);
7654 break;
7657 case RID_BUILTIN_BIT_CAST:
7659 tree expression;
7660 tree type;
7661 /* Consume the `__builtin_bit_cast' token. */
7662 cp_lexer_consume_token (parser->lexer);
7663 /* Look for the opening `('. */
7664 matching_parens parens;
7665 parens.require_open (parser);
7666 location_t type_location
7667 = cp_lexer_peek_token (parser->lexer)->location;
7668 /* Parse the type-id. */
7670 type_id_in_expr_sentinel s (parser);
7671 type = cp_parser_type_id (parser);
7673 /* Look for the `,'. */
7674 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7675 /* Now, parse the assignment-expression. */
7676 expression = cp_parser_assignment_expression (parser);
7677 /* Look for the closing `)'. */
7678 parens.require_close (parser);
7679 postfix_expression
7680 = cp_build_bit_cast (type_location, type, expression,
7681 tf_warning_or_error);
7682 break;
7685 default:
7687 tree type;
7689 /* If the next thing is a simple-type-specifier, we may be
7690 looking at a functional cast. We could also be looking at
7691 an id-expression. So, we try the functional cast, and if
7692 that doesn't work we fall back to the primary-expression. */
7693 cp_parser_parse_tentatively (parser);
7694 /* Look for the simple-type-specifier. */
7695 ++parser->prevent_constrained_type_specifiers;
7696 type = cp_parser_simple_type_specifier (parser,
7697 /*decl_specs=*/NULL,
7698 CP_PARSER_FLAGS_NONE);
7699 --parser->prevent_constrained_type_specifiers;
7700 /* Parse the cast itself. */
7701 if (!cp_parser_error_occurred (parser))
7702 postfix_expression
7703 = cp_parser_functional_cast (parser, type);
7704 /* If that worked, we're done. */
7705 if (cp_parser_parse_definitely (parser))
7706 break;
7708 /* If the functional-cast didn't work out, try a
7709 compound-literal. */
7710 if (cp_parser_allow_gnu_extensions_p (parser)
7711 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7713 cp_expr initializer = NULL_TREE;
7715 cp_parser_parse_tentatively (parser);
7717 matching_parens parens;
7718 parens.consume_open (parser);
7720 /* Avoid calling cp_parser_type_id pointlessly, see comment
7721 in cp_parser_cast_expression about c++/29234. */
7722 if (!cp_parser_compound_literal_p (parser))
7723 cp_parser_simulate_error (parser);
7724 else
7726 /* Parse the type. */
7727 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7728 parser->in_type_id_in_expr_p = true;
7729 type = cp_parser_type_id (parser);
7730 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7731 parens.require_close (parser);
7734 /* If things aren't going well, there's no need to
7735 keep going. */
7736 if (!cp_parser_error_occurred (parser))
7738 bool non_constant_p;
7739 /* Parse the brace-enclosed initializer list. */
7740 initializer = cp_parser_braced_list (parser,
7741 &non_constant_p);
7743 /* If that worked, we're definitely looking at a
7744 compound-literal expression. */
7745 if (cp_parser_parse_definitely (parser))
7747 /* Warn the user that a compound literal is not
7748 allowed in standard C++. */
7749 pedwarn (input_location, OPT_Wpedantic,
7750 "ISO C++ forbids compound-literals");
7751 /* For simplicity, we disallow compound literals in
7752 constant-expressions. We could
7753 allow compound literals of integer type, whose
7754 initializer was a constant, in constant
7755 expressions. Permitting that usage, as a further
7756 extension, would not change the meaning of any
7757 currently accepted programs. (Of course, as
7758 compound literals are not part of ISO C++, the
7759 standard has nothing to say.) */
7760 if (cp_parser_non_integral_constant_expression (parser,
7761 NIC_NCC))
7763 postfix_expression = error_mark_node;
7764 break;
7766 /* Form the representation of the compound-literal. */
7767 postfix_expression
7768 = finish_compound_literal (type, initializer,
7769 tf_warning_or_error, fcl_c99);
7770 postfix_expression.set_location (initializer.get_location ());
7771 break;
7775 /* It must be a primary-expression. */
7776 postfix_expression
7777 = cp_parser_primary_expression (parser, address_p, cast_p,
7778 /*template_arg_p=*/false,
7779 decltype_p,
7780 &idk);
7782 break;
7785 /* Note that we don't need to worry about calling build_cplus_new on a
7786 class-valued CALL_EXPR in decltype when it isn't the end of the
7787 postfix-expression; unary_complex_lvalue will take care of that for
7788 all these cases. */
7790 /* Keep looping until the postfix-expression is complete. */
7791 while (true)
7793 if (idk == CP_ID_KIND_UNQUALIFIED
7794 && identifier_p (postfix_expression)
7795 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7796 /* It is not a Koenig lookup function call. */
7797 postfix_expression
7798 = unqualified_name_lookup_error (postfix_expression);
7800 /* Peek at the next token. */
7801 token = cp_lexer_peek_token (parser->lexer);
7803 switch (token->type)
7805 case CPP_OPEN_SQUARE:
7806 if (cp_next_tokens_can_be_std_attribute_p (parser))
7808 cp_parser_error (parser,
7809 "two consecutive %<[%> shall "
7810 "only introduce an attribute");
7811 return error_mark_node;
7813 postfix_expression
7814 = cp_parser_postfix_open_square_expression (parser,
7815 postfix_expression,
7816 false,
7817 decltype_p);
7818 postfix_expression.set_range (start_loc,
7819 postfix_expression.get_location ());
7821 idk = CP_ID_KIND_NONE;
7822 is_member_access = false;
7823 break;
7825 case CPP_OPEN_PAREN:
7826 /* postfix-expression ( expression-list [opt] ) */
7828 bool koenig_p;
7829 bool is_builtin_constant_p;
7830 bool saved_integral_constant_expression_p = false;
7831 bool saved_non_integral_constant_expression_p = false;
7832 tsubst_flags_t complain = complain_flags (decltype_p);
7833 vec<tree, va_gc> *args;
7834 location_t close_paren_loc = UNKNOWN_LOCATION;
7835 location_t combined_loc = UNKNOWN_LOCATION;
7837 is_member_access = false;
7839 tree stripped_expression
7840 = tree_strip_any_location_wrapper (postfix_expression);
7841 is_builtin_constant_p
7842 = DECL_IS_BUILTIN_CONSTANT_P (stripped_expression);
7843 if (is_builtin_constant_p)
7845 /* The whole point of __builtin_constant_p is to allow
7846 non-constant expressions to appear as arguments. */
7847 saved_integral_constant_expression_p
7848 = parser->integral_constant_expression_p;
7849 saved_non_integral_constant_expression_p
7850 = parser->non_integral_constant_expression_p;
7851 parser->integral_constant_expression_p = false;
7853 args = (cp_parser_parenthesized_expression_list
7854 (parser, non_attr,
7855 /*cast_p=*/false, /*allow_expansion_p=*/true,
7856 /*non_constant_p=*/NULL,
7857 /*close_paren_loc=*/&close_paren_loc,
7858 /*wrap_locations_p=*/true));
7859 if (is_builtin_constant_p)
7861 parser->integral_constant_expression_p
7862 = saved_integral_constant_expression_p;
7863 parser->non_integral_constant_expression_p
7864 = saved_non_integral_constant_expression_p;
7867 if (args == NULL)
7869 postfix_expression = error_mark_node;
7870 break;
7873 /* Function calls are not permitted in
7874 constant-expressions. */
7875 if (! builtin_valid_in_constant_expr_p (postfix_expression)
7876 && cp_parser_non_integral_constant_expression (parser,
7877 NIC_FUNC_CALL))
7879 postfix_expression = error_mark_node;
7880 release_tree_vector (args);
7881 break;
7884 koenig_p = false;
7885 if (idk == CP_ID_KIND_UNQUALIFIED
7886 || idk == CP_ID_KIND_TEMPLATE_ID)
7888 if (identifier_p (postfix_expression)
7889 /* In C++20, we may need to perform ADL for a template
7890 name. */
7891 || (TREE_CODE (postfix_expression) == TEMPLATE_ID_EXPR
7892 && identifier_p (TREE_OPERAND (postfix_expression, 0))))
7894 if (!args->is_empty ())
7896 koenig_p = true;
7897 if (!any_type_dependent_arguments_p (args))
7898 postfix_expression
7899 = perform_koenig_lookup (postfix_expression, args,
7900 complain);
7902 else
7903 postfix_expression
7904 = unqualified_fn_lookup_error (postfix_expression);
7906 /* We do not perform argument-dependent lookup if
7907 normal lookup finds a non-function, in accordance
7908 with the expected resolution of DR 218. */
7909 else if (!args->is_empty ()
7910 && is_overloaded_fn (postfix_expression))
7912 /* Do not do argument dependent lookup if regular
7913 lookup finds a member function or a block-scope
7914 function declaration. [basic.lookup.argdep]/3 */
7915 bool do_adl_p = true;
7916 tree fns = get_fns (postfix_expression);
7917 for (lkp_iterator iter (fns); iter; ++iter)
7919 tree fn = STRIP_TEMPLATE (*iter);
7920 if ((TREE_CODE (fn) == USING_DECL
7921 && DECL_DEPENDENT_P (fn))
7922 || DECL_FUNCTION_MEMBER_P (fn)
7923 || DECL_LOCAL_DECL_P (fn))
7925 do_adl_p = false;
7926 break;
7930 if (do_adl_p)
7932 koenig_p = true;
7933 if (!any_type_dependent_arguments_p (args))
7934 postfix_expression
7935 = perform_koenig_lookup (postfix_expression, args,
7936 complain);
7941 /* Temporarily set input_location to the combined location
7942 with call expression range, as e.g. build_out_target_exprs
7943 called from convert_default_arg relies on input_location,
7944 so updating it only when the call is fully built results
7945 in inconsistencies between location handling in templates
7946 and outside of templates. */
7947 if (close_paren_loc != UNKNOWN_LOCATION)
7948 combined_loc = make_location (token->location, start_loc,
7949 close_paren_loc);
7950 iloc_sentinel ils (combined_loc);
7952 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7954 tree instance = TREE_OPERAND (postfix_expression, 0);
7955 tree fn = TREE_OPERAND (postfix_expression, 1);
7957 if (processing_template_decl
7958 && (type_dependent_object_expression_p (instance)
7959 || (!BASELINK_P (fn)
7960 && TREE_CODE (fn) != FIELD_DECL)
7961 || type_dependent_expression_p (fn)
7962 || any_type_dependent_arguments_p (args)))
7964 maybe_generic_this_capture (instance, fn);
7965 postfix_expression
7966 = build_min_nt_call_vec (postfix_expression, args);
7968 else if (BASELINK_P (fn))
7970 postfix_expression
7971 = (build_new_method_call
7972 (instance, fn, &args, NULL_TREE,
7973 (idk == CP_ID_KIND_QUALIFIED
7974 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7975 : LOOKUP_NORMAL),
7976 /*fn_p=*/NULL,
7977 complain));
7979 else
7980 postfix_expression
7981 = finish_call_expr (postfix_expression, &args,
7982 /*disallow_virtual=*/false,
7983 /*koenig_p=*/false,
7984 complain);
7986 else if (TREE_CODE (postfix_expression) == OFFSET_REF
7987 || TREE_CODE (postfix_expression) == MEMBER_REF
7988 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7989 postfix_expression = (build_offset_ref_call_from_tree
7990 (postfix_expression, &args,
7991 complain));
7992 else if (idk == CP_ID_KIND_QUALIFIED)
7993 /* A call to a static class member, or a namespace-scope
7994 function. */
7995 postfix_expression
7996 = finish_call_expr (postfix_expression, &args,
7997 /*disallow_virtual=*/true,
7998 koenig_p,
7999 complain);
8000 else
8001 /* All other function calls. */
8002 postfix_expression
8003 = finish_call_expr (postfix_expression, &args,
8004 /*disallow_virtual=*/false,
8005 koenig_p,
8006 complain);
8008 if (close_paren_loc != UNKNOWN_LOCATION)
8009 postfix_expression.set_location (combined_loc);
8011 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
8012 idk = CP_ID_KIND_NONE;
8014 release_tree_vector (args);
8016 break;
8018 case CPP_DOT:
8019 case CPP_DEREF:
8020 /* postfix-expression . template [opt] id-expression
8021 postfix-expression . pseudo-destructor-name
8022 postfix-expression -> template [opt] id-expression
8023 postfix-expression -> pseudo-destructor-name */
8025 /* Consume the `.' or `->' operator. */
8026 cp_lexer_consume_token (parser->lexer);
8028 postfix_expression
8029 = cp_parser_postfix_dot_deref_expression (parser, token->type,
8030 postfix_expression,
8031 false, &idk, loc);
8033 is_member_access = true;
8034 break;
8036 case CPP_PLUS_PLUS:
8037 /* postfix-expression ++ */
8038 /* Consume the `++' token. */
8039 cp_lexer_consume_token (parser->lexer);
8040 /* Generate a representation for the complete expression. */
8041 postfix_expression
8042 = finish_increment_expr (postfix_expression,
8043 POSTINCREMENT_EXPR);
8044 /* Increments may not appear in constant-expressions. */
8045 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
8046 postfix_expression = error_mark_node;
8047 idk = CP_ID_KIND_NONE;
8048 is_member_access = false;
8049 break;
8051 case CPP_MINUS_MINUS:
8052 /* postfix-expression -- */
8053 /* Consume the `--' token. */
8054 cp_lexer_consume_token (parser->lexer);
8055 /* Generate a representation for the complete expression. */
8056 postfix_expression
8057 = finish_increment_expr (postfix_expression,
8058 POSTDECREMENT_EXPR);
8059 /* Decrements may not appear in constant-expressions. */
8060 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
8061 postfix_expression = error_mark_node;
8062 idk = CP_ID_KIND_NONE;
8063 is_member_access = false;
8064 break;
8066 default:
8067 if (pidk_return != NULL)
8068 * pidk_return = idk;
8069 if (member_access_only_p)
8070 return is_member_access
8071 ? postfix_expression
8072 : cp_expr (error_mark_node);
8073 else
8074 return postfix_expression;
8079 /* Helper function for cp_parser_parenthesized_expression_list and
8080 cp_parser_postfix_open_square_expression. Parse a single element
8081 of parenthesized expression list. */
8083 static cp_expr
8084 cp_parser_parenthesized_expression_list_elt (cp_parser *parser, bool cast_p,
8085 bool allow_expansion_p,
8086 bool *non_constant_p)
8088 cp_expr expr (NULL_TREE);
8089 bool expr_non_constant_p;
8091 /* Parse the next assignment-expression. */
8092 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8094 /* A braced-init-list. */
8095 cp_lexer_set_source_position (parser->lexer);
8096 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8097 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8098 if (non_constant_p && expr_non_constant_p)
8099 *non_constant_p = true;
8101 else if (non_constant_p)
8103 expr = cp_parser_constant_expression (parser,
8104 /*allow_non_constant_p=*/true,
8105 &expr_non_constant_p);
8106 if (expr_non_constant_p)
8107 *non_constant_p = true;
8109 else
8110 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL, cast_p);
8112 /* If we have an ellipsis, then this is an expression expansion. */
8113 if (allow_expansion_p
8114 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8116 /* Consume the `...'. */
8117 cp_lexer_consume_token (parser->lexer);
8119 /* Build the argument pack. */
8120 expr = make_pack_expansion (expr);
8122 return expr;
8125 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
8126 by cp_parser_builtin_offsetof. We're looking for
8128 postfix-expression [ expression ]
8129 postfix-expression [ braced-init-list ] (C++11)
8130 postfix-expression [ expression-list[opt] ] (C++23)
8132 FOR_OFFSETOF is set if we're being called in that context, which
8133 changes how we deal with integer constant expressions. */
8135 static tree
8136 cp_parser_postfix_open_square_expression (cp_parser *parser,
8137 tree postfix_expression,
8138 bool for_offsetof,
8139 bool decltype_p)
8141 tree index = NULL_TREE;
8142 releasing_vec expression_list = NULL;
8143 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8144 bool saved_greater_than_is_operator_p;
8146 /* Consume the `[' token. */
8147 cp_lexer_consume_token (parser->lexer);
8149 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
8150 parser->greater_than_is_operator_p = true;
8152 /* Parse the index expression. */
8153 /* ??? For offsetof, there is a question of what to allow here. If
8154 offsetof is not being used in an integral constant expression context,
8155 then we *could* get the right answer by computing the value at runtime.
8156 If we are in an integral constant expression context, then we might
8157 could accept any constant expression; hard to say without analysis.
8158 Rather than open the barn door too wide right away, allow only integer
8159 constant expressions here. */
8160 if (for_offsetof)
8161 index = cp_parser_constant_expression (parser);
8162 else
8164 if (cxx_dialect >= cxx23
8165 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
8166 *&expression_list = make_tree_vector ();
8167 else if (cxx_dialect >= cxx23)
8169 while (true)
8171 cp_expr expr
8172 = cp_parser_parenthesized_expression_list_elt (parser,
8173 /*cast_p=*/
8174 false,
8175 /*allow_exp_p=*/
8176 true,
8177 /*non_cst_p=*/
8178 NULL);
8180 if (expr == error_mark_node)
8181 index = error_mark_node;
8182 else if (expression_list.get () == NULL
8183 && !PACK_EXPANSION_P (expr.get_value ()))
8184 index = expr.get_value ();
8185 else
8186 vec_safe_push (expression_list, expr.get_value ());
8188 /* If the next token isn't a `,', then we are done. */
8189 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8190 break;
8192 if (expression_list.get () == NULL && index != error_mark_node)
8194 *&expression_list = make_tree_vector_single (index);
8195 index = NULL_TREE;
8198 /* Otherwise, consume the `,' and keep going. */
8199 cp_lexer_consume_token (parser->lexer);
8201 if (expression_list.get () && index == error_mark_node)
8202 expression_list.release ();
8204 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8206 bool expr_nonconst_p;
8207 cp_lexer_set_source_position (parser->lexer);
8208 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8209 index = cp_parser_braced_list (parser, &expr_nonconst_p);
8211 else
8212 index = cp_parser_expression (parser, NULL, /*cast_p=*/false,
8213 /*decltype_p=*/false,
8214 /*warn_comma_p=*/warn_comma_subscript);
8217 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
8219 /* Look for the closing `]'. */
8220 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8222 /* Build the ARRAY_REF. */
8223 postfix_expression = grok_array_decl (loc, postfix_expression,
8224 index, &expression_list,
8225 tf_warning_or_error
8226 | (decltype_p ? tf_decltype : 0));
8228 /* When not doing offsetof, array references are not permitted in
8229 constant-expressions. */
8230 if (!for_offsetof
8231 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
8232 postfix_expression = error_mark_node;
8234 return postfix_expression;
8237 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
8238 dereference of incomplete type, returns true if error_mark_node should
8239 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
8240 and *DEPENDENT_P. */
8242 bool
8243 cp_parser_dot_deref_incomplete (tree *scope, cp_expr *postfix_expression,
8244 bool *dependent_p)
8246 /* In a template, be permissive by treating an object expression
8247 of incomplete type as dependent (after a pedwarn). */
8248 diagnostic_t kind = (processing_template_decl
8249 && MAYBE_CLASS_TYPE_P (*scope) ? DK_PEDWARN : DK_ERROR);
8251 switch (TREE_CODE (*postfix_expression))
8253 case CAST_EXPR:
8254 case REINTERPRET_CAST_EXPR:
8255 case CONST_CAST_EXPR:
8256 case STATIC_CAST_EXPR:
8257 case DYNAMIC_CAST_EXPR:
8258 case IMPLICIT_CONV_EXPR:
8259 case VIEW_CONVERT_EXPR:
8260 case NON_LVALUE_EXPR:
8261 kind = DK_ERROR;
8262 break;
8263 case OVERLOAD:
8264 /* Don't emit any diagnostic for OVERLOADs. */
8265 kind = DK_IGNORED;
8266 break;
8267 default:
8268 /* Avoid clobbering e.g. DECLs. */
8269 if (!EXPR_P (*postfix_expression))
8270 kind = DK_ERROR;
8271 break;
8274 if (kind == DK_IGNORED)
8275 return false;
8277 location_t exploc = location_of (*postfix_expression);
8278 cxx_incomplete_type_diagnostic (exploc, *postfix_expression, *scope, kind);
8279 if (!MAYBE_CLASS_TYPE_P (*scope))
8280 return true;
8281 if (kind == DK_ERROR)
8282 *scope = *postfix_expression = error_mark_node;
8283 else if (processing_template_decl)
8285 *dependent_p = true;
8286 *scope = TREE_TYPE (*postfix_expression) = NULL_TREE;
8288 return false;
8291 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
8292 by cp_parser_builtin_offsetof. We're looking for
8294 postfix-expression . template [opt] id-expression
8295 postfix-expression . pseudo-destructor-name
8296 postfix-expression -> template [opt] id-expression
8297 postfix-expression -> pseudo-destructor-name
8299 FOR_OFFSETOF is set if we're being called in that context. That sorta
8300 limits what of the above we'll actually accept, but nevermind.
8301 TOKEN_TYPE is the "." or "->" token, which will already have been
8302 removed from the stream. */
8304 static tree
8305 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
8306 enum cpp_ttype token_type,
8307 cp_expr postfix_expression,
8308 bool for_offsetof, cp_id_kind *idk,
8309 location_t location)
8311 tree name;
8312 bool dependent_p;
8313 bool pseudo_destructor_p;
8314 tree scope = NULL_TREE;
8315 location_t start_loc = postfix_expression.get_start ();
8317 /* If this is a `->' operator, dereference the pointer. */
8318 if (token_type == CPP_DEREF)
8319 postfix_expression = build_x_arrow (location, postfix_expression,
8320 tf_warning_or_error);
8321 /* Check to see whether or not the expression is type-dependent and
8322 not the current instantiation. */
8323 dependent_p = type_dependent_object_expression_p (postfix_expression);
8324 /* The identifier following the `->' or `.' is not qualified. */
8325 parser->scope = NULL_TREE;
8326 parser->qualifying_scope = NULL_TREE;
8327 parser->object_scope = NULL_TREE;
8328 *idk = CP_ID_KIND_NONE;
8330 /* Enter the scope corresponding to the type of the object
8331 given by the POSTFIX_EXPRESSION. */
8332 if (!dependent_p)
8334 scope = TREE_TYPE (postfix_expression);
8335 /* According to the standard, no expression should ever have
8336 reference type. Unfortunately, we do not currently match
8337 the standard in this respect in that our internal representation
8338 of an expression may have reference type even when the standard
8339 says it does not. Therefore, we have to manually obtain the
8340 underlying type here. */
8341 scope = non_reference (scope);
8342 /* The type of the POSTFIX_EXPRESSION must be complete. */
8343 /* Unlike the object expression in other contexts, *this is not
8344 required to be of complete type for purposes of class member
8345 access (5.2.5) outside the member function body. */
8346 if (postfix_expression != current_class_ref
8347 && scope != error_mark_node
8348 && !currently_open_class (scope))
8350 scope = complete_type (scope);
8351 if (!COMPLETE_TYPE_P (scope)
8352 && cp_parser_dot_deref_incomplete (&scope, &postfix_expression,
8353 &dependent_p))
8354 return error_mark_node;
8357 if (!dependent_p)
8359 /* Let the name lookup machinery know that we are processing a
8360 class member access expression. */
8361 parser->context->object_type = scope;
8362 /* If something went wrong, we want to be able to discern that case,
8363 as opposed to the case where there was no SCOPE due to the type
8364 of expression being dependent. */
8365 if (!scope)
8366 scope = error_mark_node;
8367 /* If the SCOPE was erroneous, make the various semantic analysis
8368 functions exit quickly -- and without issuing additional error
8369 messages. */
8370 if (scope == error_mark_node)
8371 postfix_expression = error_mark_node;
8375 if (dependent_p)
8377 tree type = TREE_TYPE (postfix_expression);
8378 /* If we don't have a (type-dependent) object of class type, use
8379 typeof to figure out the type of the object. */
8380 if (type == NULL_TREE || is_auto (type))
8381 type = finish_typeof (postfix_expression);
8382 parser->context->object_type = type;
8385 /* Assume this expression is not a pseudo-destructor access. */
8386 pseudo_destructor_p = false;
8388 /* If the SCOPE is a scalar type, then, if this is a valid program,
8389 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
8390 is type dependent, it can be pseudo-destructor-name or something else.
8391 Try to parse it as pseudo-destructor-name first. */
8392 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
8394 tree s;
8395 tree type;
8397 cp_parser_parse_tentatively (parser);
8398 /* Parse the pseudo-destructor-name. */
8399 s = NULL_TREE;
8400 cp_parser_pseudo_destructor_name (parser, postfix_expression,
8401 &s, &type);
8402 if (dependent_p
8403 && (cp_parser_error_occurred (parser)
8404 || !SCALAR_TYPE_P (type)))
8405 cp_parser_abort_tentative_parse (parser);
8406 else if (cp_parser_parse_definitely (parser))
8408 pseudo_destructor_p = true;
8409 postfix_expression
8410 = finish_pseudo_destructor_expr (postfix_expression,
8411 s, type, location);
8415 if (!pseudo_destructor_p)
8417 /* If the SCOPE is not a scalar type, we are looking at an
8418 ordinary class member access expression, rather than a
8419 pseudo-destructor-name. */
8420 bool template_p;
8421 cp_token *token = cp_lexer_peek_token (parser->lexer);
8422 /* Parse the id-expression. */
8423 name = (cp_parser_id_expression
8424 (parser,
8425 cp_parser_optional_template_keyword (parser),
8426 /*check_dependency_p=*/true,
8427 &template_p,
8428 /*declarator_p=*/false,
8429 /*optional_p=*/false));
8430 /* In general, build a SCOPE_REF if the member name is qualified.
8431 However, if the name was not dependent and has already been
8432 resolved; there is no need to build the SCOPE_REF. For example;
8434 struct X { void f(); };
8435 template <typename T> void f(T* t) { t->X::f(); }
8437 Even though "t" is dependent, "X::f" is not and has been resolved
8438 to a BASELINK; there is no need to include scope information. */
8440 /* But we do need to remember that there was an explicit scope for
8441 virtual function calls. */
8442 if (parser->scope)
8443 *idk = CP_ID_KIND_QUALIFIED;
8445 /* If the name is a template-id that names a type, we will get a
8446 TYPE_DECL here. That is invalid code. */
8447 if (TREE_CODE (name) == TYPE_DECL)
8449 error_at (token->location, "invalid use of %qD", name);
8450 postfix_expression = error_mark_node;
8452 else
8454 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
8456 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
8458 error_at (token->location, "%<%D::%D%> is not a class member",
8459 parser->scope, name);
8460 postfix_expression = error_mark_node;
8462 else
8463 name = build_qualified_name (/*type=*/NULL_TREE,
8464 parser->scope,
8465 name,
8466 template_p);
8467 parser->scope = NULL_TREE;
8468 parser->qualifying_scope = NULL_TREE;
8469 parser->object_scope = NULL_TREE;
8471 if (parser->scope && name && BASELINK_P (name))
8472 adjust_result_of_qualified_name_lookup
8473 (name, parser->scope, scope);
8474 postfix_expression
8475 = finish_class_member_access_expr (postfix_expression, name,
8476 template_p,
8477 tf_warning_or_error);
8478 /* Build a location e.g.:
8479 ptr->access_expr
8480 ~~~^~~~~~~~~~~~~
8481 where the caret is at the deref token, ranging from
8482 the start of postfix_expression to the end of the access expr. */
8483 location_t combined_loc
8484 = make_location (input_location, start_loc, parser->lexer);
8485 protected_set_expr_location (postfix_expression, combined_loc);
8489 /* We no longer need to look up names in the scope of the object on
8490 the left-hand side of the `.' or `->' operator. */
8491 parser->context->object_type = NULL_TREE;
8493 /* Outside of offsetof, these operators may not appear in
8494 constant-expressions. */
8495 if (!for_offsetof
8496 && (cp_parser_non_integral_constant_expression
8497 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
8498 postfix_expression = error_mark_node;
8500 return postfix_expression;
8503 /* Parse a parenthesized expression-list.
8505 expression-list:
8506 assignment-expression
8507 expression-list, assignment-expression
8509 attribute-list:
8510 expression-list
8511 identifier
8512 identifier, expression-list
8514 CAST_P is true if this expression is the target of a cast.
8516 ALLOW_EXPANSION_P is true if this expression allows expansion of an
8517 argument pack.
8519 WRAP_LOCATIONS_P is true if expressions within this list for which
8520 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
8521 their source locations.
8523 Returns a vector of trees. Each element is a representation of an
8524 assignment-expression. NULL is returned if the ( and or ) are
8525 missing. An empty, but allocated, vector is returned on no
8526 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
8527 if we are parsing an attribute list for an attribute that wants a
8528 plain identifier argument, normal_attr for an attribute that wants
8529 an expression, or non_attr if we aren't parsing an attribute list. If
8530 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
8531 not all of the expressions in the list were constant.
8532 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
8533 will be written to with the location of the closing parenthesis. If
8534 an error occurs, it may or may not be written to. */
8536 static vec<tree, va_gc> *
8537 cp_parser_parenthesized_expression_list (cp_parser* parser,
8538 int is_attribute_list,
8539 bool cast_p,
8540 bool allow_expansion_p,
8541 bool *non_constant_p,
8542 location_t *close_paren_loc,
8543 bool wrap_locations_p)
8545 vec<tree, va_gc> *expression_list;
8546 bool saved_greater_than_is_operator_p;
8548 /* Assume all the expressions will be constant. */
8549 if (non_constant_p)
8550 *non_constant_p = false;
8552 matching_parens parens;
8553 if (!parens.require_open (parser))
8554 return NULL;
8556 expression_list = make_tree_vector ();
8558 /* Within a parenthesized expression, a `>' token is always
8559 the greater-than operator. */
8560 saved_greater_than_is_operator_p
8561 = parser->greater_than_is_operator_p;
8562 parser->greater_than_is_operator_p = true;
8564 cp_expr expr (NULL_TREE);
8566 /* Consume expressions until there are no more. */
8567 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8568 while (true)
8570 /* At the beginning of attribute lists, check to see if the
8571 next token is an identifier. */
8572 if (is_attribute_list == id_attr
8573 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
8574 expr = cp_lexer_consume_token (parser->lexer)->u.value;
8575 else if (is_attribute_list == assume_attr)
8576 expr = cp_parser_conditional_expression (parser);
8577 else
8578 expr
8579 = cp_parser_parenthesized_expression_list_elt (parser, cast_p,
8580 allow_expansion_p,
8581 non_constant_p);
8583 if (wrap_locations_p)
8584 expr.maybe_add_location_wrapper ();
8586 /* Add it to the list. We add error_mark_node
8587 expressions to the list, so that we can still tell if
8588 the correct form for a parenthesized expression-list
8589 is found. That gives better errors. */
8590 vec_safe_push (expression_list, expr.get_value ());
8592 if (expr == error_mark_node)
8593 goto skip_comma;
8595 /* After the first item, attribute lists look the same as
8596 expression lists. */
8597 is_attribute_list = non_attr;
8599 get_comma:;
8600 /* If the next token isn't a `,', then we are done. */
8601 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8602 break;
8604 /* Otherwise, consume the `,' and keep going. */
8605 cp_lexer_consume_token (parser->lexer);
8608 if (close_paren_loc)
8609 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
8611 if (!parens.require_close (parser))
8613 int ending;
8615 skip_comma:;
8616 /* We try and resync to an unnested comma, as that will give the
8617 user better diagnostics. */
8618 ending = cp_parser_skip_to_closing_parenthesis (parser,
8619 /*recovering=*/true,
8620 /*or_comma=*/true,
8621 /*consume_paren=*/true);
8622 if (ending < 0)
8623 goto get_comma;
8624 if (!ending)
8626 parser->greater_than_is_operator_p
8627 = saved_greater_than_is_operator_p;
8628 return NULL;
8632 parser->greater_than_is_operator_p
8633 = saved_greater_than_is_operator_p;
8635 return expression_list;
8638 /* Parse a pseudo-destructor-name.
8640 pseudo-destructor-name:
8641 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
8642 :: [opt] nested-name-specifier template template-id :: ~ type-name
8643 :: [opt] nested-name-specifier [opt] ~ type-name
8645 If either of the first two productions is used, sets *SCOPE to the
8646 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
8647 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
8648 or ERROR_MARK_NODE if the parse fails. */
8650 static void
8651 cp_parser_pseudo_destructor_name (cp_parser* parser,
8652 tree object,
8653 tree* scope,
8654 tree* type)
8656 bool nested_name_specifier_p;
8658 /* Handle ~auto. */
8659 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
8660 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
8661 && !type_dependent_expression_p (object))
8663 if (cxx_dialect < cxx14)
8664 pedwarn (input_location, OPT_Wc__14_extensions,
8665 "%<~auto%> only available with "
8666 "%<-std=c++14%> or %<-std=gnu++14%>");
8667 cp_lexer_consume_token (parser->lexer);
8668 cp_lexer_consume_token (parser->lexer);
8669 *scope = NULL_TREE;
8670 *type = TREE_TYPE (object);
8671 return;
8674 /* Assume that things will not work out. */
8675 *type = error_mark_node;
8677 /* Look for the optional `::' operator. */
8678 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
8679 /* Look for the optional nested-name-specifier. */
8680 nested_name_specifier_p
8681 = (cp_parser_nested_name_specifier_opt (parser,
8682 /*typename_keyword_p=*/false,
8683 /*check_dependency_p=*/true,
8684 /*type_p=*/false,
8685 /*is_declaration=*/false)
8686 != NULL_TREE);
8687 /* Now, if we saw a nested-name-specifier, we might be doing the
8688 second production. */
8689 if (nested_name_specifier_p
8690 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8692 /* Consume the `template' keyword. */
8693 cp_lexer_consume_token (parser->lexer);
8694 /* Parse the template-id. */
8695 cp_parser_template_id (parser,
8696 /*template_keyword_p=*/true,
8697 /*check_dependency_p=*/false,
8698 class_type,
8699 /*is_declaration=*/true);
8700 /* Look for the `::' token. */
8701 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
8703 /* If the next token is not a `~', then there might be some
8704 additional qualification. */
8705 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
8707 /* At this point, we're looking for "type-name :: ~". The type-name
8708 must not be a class-name, since this is a pseudo-destructor. So,
8709 it must be either an enum-name, or a typedef-name -- both of which
8710 are just identifiers. So, we peek ahead to check that the "::"
8711 and "~" tokens are present; if they are not, then we can avoid
8712 calling type_name. */
8713 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
8714 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
8715 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
8717 cp_parser_error (parser, "non-scalar type");
8718 return;
8721 /* Look for the type-name. */
8722 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
8723 if (*scope == error_mark_node)
8724 return;
8726 /* Look for the `::' token. */
8727 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
8729 else
8730 *scope = NULL_TREE;
8732 /* Look for the `~'. */
8733 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
8735 /* Once we see the ~, this has to be a pseudo-destructor. */
8736 if (!processing_template_decl && !cp_parser_error_occurred (parser))
8737 cp_parser_commit_to_topmost_tentative_parse (parser);
8739 /* Look for the type-name again. We are not responsible for
8740 checking that it matches the first type-name. */
8741 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
8744 /* Parse a unary-expression.
8746 unary-expression:
8747 postfix-expression
8748 ++ cast-expression
8749 -- cast-expression
8750 await-expression
8751 unary-operator cast-expression
8752 sizeof unary-expression
8753 sizeof ( type-id )
8754 alignof ( type-id ) [C++0x]
8755 new-expression
8756 delete-expression
8758 GNU Extensions:
8760 unary-expression:
8761 __extension__ cast-expression
8762 __alignof__ unary-expression
8763 __alignof__ ( type-id )
8764 alignof unary-expression [C++0x]
8765 __real__ cast-expression
8766 __imag__ cast-expression
8767 && identifier
8768 sizeof ( type-id ) { initializer-list , [opt] }
8769 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
8770 __alignof__ ( type-id ) { initializer-list , [opt] }
8772 ADDRESS_P is true iff the unary-expression is appearing as the
8773 operand of the `&' operator. CAST_P is true if this expression is
8774 the target of a cast.
8776 Returns a representation of the expression. */
8778 static cp_expr
8779 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
8780 bool address_p, bool cast_p, bool decltype_p)
8782 cp_token *token;
8783 enum tree_code unary_operator;
8785 /* Peek at the next token. */
8786 token = cp_lexer_peek_token (parser->lexer);
8787 /* Some keywords give away the kind of expression. */
8788 if (token->type == CPP_KEYWORD)
8790 enum rid keyword = token->keyword;
8792 switch (keyword)
8794 case RID_ALIGNOF:
8795 case RID_SIZEOF:
8797 tree operand, ret;
8798 enum tree_code op;
8799 location_t start_loc = token->location;
8801 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
8802 bool std_alignof = id_equal (token->u.value, "alignof");
8804 /* Consume the token. */
8805 cp_lexer_consume_token (parser->lexer);
8806 /* Parse the operand. */
8807 operand = cp_parser_sizeof_operand (parser, keyword);
8809 /* Construct a location e.g. :
8810 alignof (expr)
8811 ^~~~~~~~~~~~~~
8812 with start == caret at the start of the "alignof"/"sizeof"
8813 token, with the endpoint at the final closing paren. */
8814 location_t compound_loc
8815 = make_location (start_loc, start_loc, parser->lexer);
8817 if (TYPE_P (operand))
8818 ret = cxx_sizeof_or_alignof_type (compound_loc, operand, op,
8819 std_alignof, true);
8820 else
8822 /* ISO C++ defines alignof only with types, not with
8823 expressions. So pedwarn if alignof is used with a non-
8824 type expression. However, __alignof__ is ok. */
8825 if (std_alignof)
8826 pedwarn (token->location, OPT_Wpedantic,
8827 "ISO C++ does not allow %<alignof%> "
8828 "with a non-type");
8830 ret = cxx_sizeof_or_alignof_expr (compound_loc, operand, op,
8831 std_alignof, true);
8833 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8834 SIZEOF_EXPR with the original operand. */
8835 if (op == SIZEOF_EXPR && ret != error_mark_node)
8837 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
8839 if (!processing_template_decl && TYPE_P (operand))
8841 ret = build_min (SIZEOF_EXPR, size_type_node,
8842 build1 (NOP_EXPR, operand,
8843 error_mark_node));
8844 SIZEOF_EXPR_TYPE_P (ret) = 1;
8846 else
8847 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
8848 TREE_SIDE_EFFECTS (ret) = 0;
8849 TREE_READONLY (ret) = 1;
8850 SET_EXPR_LOCATION (ret, compound_loc);
8854 cp_expr ret_expr (ret, compound_loc);
8855 ret_expr = ret_expr.maybe_add_location_wrapper ();
8856 return ret_expr;
8859 case RID_BUILTIN_HAS_ATTRIBUTE:
8860 return cp_parser_has_attribute_expression (parser);
8862 case RID_NEW:
8863 return cp_parser_new_expression (parser);
8865 case RID_DELETE:
8866 return cp_parser_delete_expression (parser);
8868 case RID_EXTENSION:
8870 /* The saved value of the PEDANTIC flag. */
8871 int saved_pedantic;
8872 tree expr;
8874 /* Save away the PEDANTIC flag. */
8875 cp_parser_extension_opt (parser, &saved_pedantic);
8876 /* Parse the cast-expression. */
8877 expr = cp_parser_simple_cast_expression (parser);
8878 /* Restore the PEDANTIC flag. */
8879 pedantic = saved_pedantic;
8881 return expr;
8884 case RID_REALPART:
8885 case RID_IMAGPART:
8887 tree expression;
8889 /* Consume the `__real__' or `__imag__' token. */
8890 cp_lexer_consume_token (parser->lexer);
8891 /* Parse the cast-expression. */
8892 expression = cp_parser_simple_cast_expression (parser);
8893 /* Create the complete representation. */
8894 return build_x_unary_op (token->location,
8895 (keyword == RID_REALPART
8896 ? REALPART_EXPR : IMAGPART_EXPR),
8897 expression, NULL_TREE,
8898 tf_warning_or_error);
8900 break;
8902 case RID_TRANSACTION_ATOMIC:
8903 case RID_TRANSACTION_RELAXED:
8904 return cp_parser_transaction_expression (parser, keyword);
8906 case RID_NOEXCEPT:
8908 tree expr;
8909 const char *saved_message;
8910 bool saved_integral_constant_expression_p;
8911 bool saved_non_integral_constant_expression_p;
8912 bool saved_greater_than_is_operator_p;
8914 location_t start_loc = token->location;
8916 cp_lexer_consume_token (parser->lexer);
8917 matching_parens parens;
8918 parens.require_open (parser);
8920 saved_message = parser->type_definition_forbidden_message;
8921 parser->type_definition_forbidden_message
8922 = G_("types may not be defined in %<noexcept%> expressions");
8924 saved_integral_constant_expression_p
8925 = parser->integral_constant_expression_p;
8926 saved_non_integral_constant_expression_p
8927 = parser->non_integral_constant_expression_p;
8928 parser->integral_constant_expression_p = false;
8930 saved_greater_than_is_operator_p
8931 = parser->greater_than_is_operator_p;
8932 parser->greater_than_is_operator_p = true;
8934 ++cp_unevaluated_operand;
8935 ++c_inhibit_evaluation_warnings;
8936 ++cp_noexcept_operand;
8937 expr = cp_parser_expression (parser);
8938 --cp_noexcept_operand;
8939 --c_inhibit_evaluation_warnings;
8940 --cp_unevaluated_operand;
8942 parser->greater_than_is_operator_p
8943 = saved_greater_than_is_operator_p;
8945 parser->integral_constant_expression_p
8946 = saved_integral_constant_expression_p;
8947 parser->non_integral_constant_expression_p
8948 = saved_non_integral_constant_expression_p;
8950 parser->type_definition_forbidden_message = saved_message;
8952 parens.require_close (parser);
8954 /* Construct a location of the form:
8955 noexcept (expr)
8956 ^~~~~~~~~~~~~~~
8957 with start == caret, finishing at the close-paren. */
8958 location_t noexcept_loc
8959 = make_location (start_loc, start_loc, parser->lexer);
8961 return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
8962 noexcept_loc);
8965 case RID_CO_AWAIT:
8967 tree expr;
8968 location_t kw_loc = token->location;
8970 /* Consume the `co_await' token. */
8971 cp_lexer_consume_token (parser->lexer);
8972 /* Parse its cast-expression. */
8973 expr = cp_parser_simple_cast_expression (parser);
8974 if (expr == error_mark_node)
8975 return error_mark_node;
8977 /* Handle [expr.await]. */
8978 return cp_expr (finish_co_await_expr (kw_loc, expr));
8981 default:
8982 break;
8986 /* Look for the `:: new' and `:: delete', which also signal the
8987 beginning of a new-expression, or delete-expression,
8988 respectively. If the next token is `::', then it might be one of
8989 these. */
8990 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8992 enum rid keyword;
8994 /* See if the token after the `::' is one of the keywords in
8995 which we're interested. */
8996 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8997 /* If it's `new', we have a new-expression. */
8998 if (keyword == RID_NEW)
8999 return cp_parser_new_expression (parser);
9000 /* Similarly, for `delete'. */
9001 else if (keyword == RID_DELETE)
9002 return cp_parser_delete_expression (parser);
9005 /* Look for a unary operator. */
9006 unary_operator = cp_parser_unary_operator (token);
9007 /* The `++' and `--' operators can be handled similarly, even though
9008 they are not technically unary-operators in the grammar. */
9009 if (unary_operator == ERROR_MARK)
9011 if (token->type == CPP_PLUS_PLUS)
9012 unary_operator = PREINCREMENT_EXPR;
9013 else if (token->type == CPP_MINUS_MINUS)
9014 unary_operator = PREDECREMENT_EXPR;
9015 /* Handle the GNU address-of-label extension. */
9016 else if (cp_parser_allow_gnu_extensions_p (parser)
9017 && token->type == CPP_AND_AND)
9019 tree identifier;
9020 tree expression;
9021 location_t start_loc = token->location;
9023 /* Consume the '&&' token. */
9024 cp_lexer_consume_token (parser->lexer);
9025 /* Look for the identifier. */
9026 identifier = cp_parser_identifier (parser);
9027 /* Construct a location of the form:
9028 &&label
9029 ^~~~~~~
9030 with caret==start at the "&&", finish at the end of the label. */
9031 location_t combined_loc
9032 = make_location (start_loc, start_loc, parser->lexer);
9033 /* Create an expression representing the address. */
9034 expression = finish_label_address_expr (identifier, combined_loc);
9035 if (cp_parser_non_integral_constant_expression (parser,
9036 NIC_ADDR_LABEL))
9037 expression = error_mark_node;
9038 return expression;
9041 if (unary_operator != ERROR_MARK)
9043 cp_expr cast_expression;
9044 cp_expr expression = error_mark_node;
9045 non_integral_constant non_constant_p = NIC_NONE;
9046 location_t loc = token->location;
9047 tsubst_flags_t complain = complain_flags (decltype_p);
9049 /* Consume the operator token. */
9050 token = cp_lexer_consume_token (parser->lexer);
9051 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
9053 /* Parse the cast-expression. */
9054 cast_expression
9055 = cp_parser_cast_expression (parser,
9056 unary_operator == ADDR_EXPR,
9057 /*cast_p=*/false,
9058 /*decltype*/false,
9059 pidk);
9061 /* Make a location:
9062 OP_TOKEN CAST_EXPRESSION
9063 ^~~~~~~~~~~~~~~~~~~~~~~~~
9064 with start==caret at the operator token, and
9065 extending to the end of the cast_expression. */
9066 loc = make_location (loc, loc, cast_expression.get_finish ());
9068 /* Now, build an appropriate representation. */
9069 switch (unary_operator)
9071 case INDIRECT_REF:
9072 non_constant_p = NIC_STAR;
9073 expression = build_x_indirect_ref (loc, cast_expression,
9074 RO_UNARY_STAR, NULL_TREE,
9075 complain);
9076 /* TODO: build_x_indirect_ref does not always honor the
9077 location, so ensure it is set. */
9078 expression.set_location (loc);
9079 break;
9081 case ADDR_EXPR:
9082 non_constant_p = NIC_ADDR;
9083 /* Fall through. */
9084 case BIT_NOT_EXPR:
9085 expression = build_x_unary_op (loc, unary_operator,
9086 cast_expression,
9087 NULL_TREE, complain);
9088 /* TODO: build_x_unary_op does not always honor the location,
9089 so ensure it is set. */
9090 expression.set_location (loc);
9091 break;
9093 case PREINCREMENT_EXPR:
9094 case PREDECREMENT_EXPR:
9095 non_constant_p = unary_operator == PREINCREMENT_EXPR
9096 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
9097 /* Fall through. */
9098 case NEGATE_EXPR:
9099 /* Immediately fold negation of a constant, unless the constant is 0
9100 (since -0 == 0) or it would overflow. */
9101 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER)
9103 tree stripped_expr
9104 = tree_strip_any_location_wrapper (cast_expression);
9105 if (CONSTANT_CLASS_P (stripped_expr)
9106 && !integer_zerop (stripped_expr)
9107 && !TREE_OVERFLOW (stripped_expr))
9109 tree folded = fold_build1 (unary_operator,
9110 TREE_TYPE (stripped_expr),
9111 stripped_expr);
9112 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
9114 expression = maybe_wrap_with_location (folded, loc);
9115 break;
9119 /* Fall through. */
9120 case UNARY_PLUS_EXPR:
9121 case TRUTH_NOT_EXPR:
9122 expression = finish_unary_op_expr (loc, unary_operator,
9123 cast_expression, complain);
9124 break;
9126 default:
9127 gcc_unreachable ();
9130 if (non_constant_p != NIC_NONE
9131 && cp_parser_non_integral_constant_expression (parser,
9132 non_constant_p))
9133 expression = error_mark_node;
9135 return expression;
9138 return cp_parser_postfix_expression (parser, address_p, cast_p,
9139 /*member_access_only_p=*/false,
9140 decltype_p,
9141 pidk);
9144 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
9145 unary-operator, the corresponding tree code is returned. */
9147 static enum tree_code
9148 cp_parser_unary_operator (cp_token* token)
9150 switch (token->type)
9152 case CPP_MULT:
9153 return INDIRECT_REF;
9155 case CPP_AND:
9156 return ADDR_EXPR;
9158 case CPP_PLUS:
9159 return UNARY_PLUS_EXPR;
9161 case CPP_MINUS:
9162 return NEGATE_EXPR;
9164 case CPP_NOT:
9165 return TRUTH_NOT_EXPR;
9167 case CPP_COMPL:
9168 return BIT_NOT_EXPR;
9170 default:
9171 return ERROR_MARK;
9175 /* Parse a __builtin_has_attribute([expr|type], attribute-spec) expression.
9176 Returns a representation of the expression. */
9178 static tree
9179 cp_parser_has_attribute_expression (cp_parser *parser)
9181 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9183 /* Consume the __builtin_has_attribute token. */
9184 cp_lexer_consume_token (parser->lexer);
9186 matching_parens parens;
9187 if (!parens.require_open (parser))
9188 return error_mark_node;
9190 /* Types cannot be defined in a `sizeof' expression. Save away the
9191 old message. */
9192 const char *saved_message = parser->type_definition_forbidden_message;
9193 const char *saved_message_arg
9194 = parser->type_definition_forbidden_message_arg;
9195 parser->type_definition_forbidden_message
9196 = G_("types may not be defined in %qs expressions");
9197 parser->type_definition_forbidden_message_arg
9198 = IDENTIFIER_POINTER (ridpointers[RID_BUILTIN_HAS_ATTRIBUTE]);
9200 /* The restrictions on constant-expressions do not apply inside
9201 sizeof expressions. */
9202 bool saved_integral_constant_expression_p
9203 = parser->integral_constant_expression_p;
9204 bool saved_non_integral_constant_expression_p
9205 = parser->non_integral_constant_expression_p;
9206 parser->integral_constant_expression_p = false;
9208 /* Do not actually evaluate the expression. */
9209 ++cp_unevaluated_operand;
9210 ++c_inhibit_evaluation_warnings;
9212 tree oper = NULL_TREE;
9214 /* We can't be sure yet whether we're looking at a type-id or an
9215 expression. */
9216 cp_parser_parse_tentatively (parser);
9218 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9219 parser->in_type_id_in_expr_p = true;
9220 /* Look for the type-id. */
9221 oper = cp_parser_type_id (parser);
9222 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9224 cp_parser_parse_definitely (parser);
9226 /* If the type-id production did not work out, then we must be
9227 looking at an expression. */
9228 if (!oper || oper == error_mark_node)
9229 oper = cp_parser_assignment_expression (parser);
9231 STRIP_ANY_LOCATION_WRAPPER (oper);
9233 /* Go back to evaluating expressions. */
9234 --cp_unevaluated_operand;
9235 --c_inhibit_evaluation_warnings;
9237 /* And restore the old one. */
9238 parser->type_definition_forbidden_message = saved_message;
9239 parser->type_definition_forbidden_message_arg = saved_message_arg;
9240 parser->integral_constant_expression_p
9241 = saved_integral_constant_expression_p;
9242 parser->non_integral_constant_expression_p
9243 = saved_non_integral_constant_expression_p;
9245 /* Consume the comma if it's there. */
9246 if (!cp_parser_require (parser, CPP_COMMA, RT_COMMA))
9248 cp_parser_skip_to_closing_parenthesis (parser, false, false,
9249 /*consume_paren=*/true);
9250 return error_mark_node;
9253 /* Parse the attribute specification. */
9254 bool ret = false;
9255 location_t atloc = cp_lexer_peek_token (parser->lexer)->location;
9256 if (tree attr = cp_parser_gnu_attribute_list (parser, /*exactly_one=*/true))
9258 if (oper == error_mark_node)
9259 /* Nothing. */;
9260 else if (processing_template_decl && uses_template_parms (oper))
9261 sorry_at (atloc, "%<__builtin_has_attribute%> with dependent argument "
9262 "not supported yet");
9263 else
9265 /* Fold constant expressions used in attributes first. */
9266 cp_check_const_attributes (attr);
9268 /* Finally, see if OPER has been declared with ATTR. */
9269 ret = has_attribute (atloc, oper, attr, default_conversion);
9272 parens.require_close (parser);
9274 else
9276 error_at (atloc, "expected identifier");
9277 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9280 /* Construct a location e.g. :
9281 __builtin_has_attribute (oper, attr)
9282 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9283 with start == caret at the start of the built-in token,
9284 and with the endpoint at the final closing paren. */
9285 location_t compound_loc
9286 = make_location (start_loc, start_loc, parser->lexer);
9288 cp_expr ret_expr (ret ? boolean_true_node : boolean_false_node);
9289 ret_expr.set_location (compound_loc);
9290 ret_expr = ret_expr.maybe_add_location_wrapper ();
9291 return ret_expr;
9294 /* Parse a new-expression.
9296 new-expression:
9297 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
9298 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
9300 Returns a representation of the expression. */
9302 static tree
9303 cp_parser_new_expression (cp_parser* parser)
9305 bool global_scope_p;
9306 vec<tree, va_gc> *placement;
9307 tree type;
9308 vec<tree, va_gc> *initializer;
9309 tree nelts = NULL_TREE;
9310 tree ret;
9312 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9314 /* Look for the optional `::' operator. */
9315 global_scope_p
9316 = (cp_parser_global_scope_opt (parser,
9317 /*current_scope_valid_p=*/false)
9318 != NULL_TREE);
9319 /* Look for the `new' operator. */
9320 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
9321 /* There's no easy way to tell a new-placement from the
9322 `( type-id )' construct. */
9323 cp_parser_parse_tentatively (parser);
9324 /* Look for a new-placement. */
9325 placement = cp_parser_new_placement (parser);
9326 /* If that didn't work out, there's no new-placement. */
9327 if (!cp_parser_parse_definitely (parser))
9329 if (placement != NULL)
9330 release_tree_vector (placement);
9331 placement = NULL;
9334 /* If the next token is a `(', then we have a parenthesized
9335 type-id. */
9336 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9338 cp_token *token;
9339 const char *saved_message = parser->type_definition_forbidden_message;
9341 /* Consume the `('. */
9342 matching_parens parens;
9343 parens.consume_open (parser);
9345 /* Parse the type-id. */
9346 parser->type_definition_forbidden_message
9347 = G_("types may not be defined in a new-expression");
9349 type_id_in_expr_sentinel s (parser);
9350 type = cp_parser_type_id (parser);
9352 parser->type_definition_forbidden_message = saved_message;
9354 /* Look for the closing `)'. */
9355 parens.require_close (parser);
9356 token = cp_lexer_peek_token (parser->lexer);
9357 /* There should not be a direct-new-declarator in this production,
9358 but GCC used to allowed this, so we check and emit a sensible error
9359 message for this case. */
9360 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
9362 error_at (token->location,
9363 "array bound forbidden after parenthesized type-id");
9364 inform (token->location,
9365 "try removing the parentheses around the type-id");
9366 cp_parser_direct_new_declarator (parser);
9369 /* Otherwise, there must be a new-type-id. */
9370 else
9371 type = cp_parser_new_type_id (parser, &nelts);
9373 /* If the next token is a `(' or '{', then we have a new-initializer. */
9374 cp_token *token = cp_lexer_peek_token (parser->lexer);
9375 if (token->type == CPP_OPEN_PAREN
9376 || token->type == CPP_OPEN_BRACE)
9377 initializer = cp_parser_new_initializer (parser);
9378 else
9379 initializer = NULL;
9381 /* A new-expression may not appear in an integral constant
9382 expression. */
9383 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
9384 ret = error_mark_node;
9385 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
9386 of a new-type-id or type-id of a new-expression, the new-expression shall
9387 contain a new-initializer of the form ( assignment-expression )".
9388 Additionally, consistently with the spirit of DR 1467, we want to accept
9389 'new auto { 2 }' too. */
9390 else if ((ret = type_uses_auto (type))
9391 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
9392 && (vec_safe_length (initializer) != 1
9393 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
9394 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
9396 error_at (token->location,
9397 "initialization of new-expression for type %<auto%> "
9398 "requires exactly one element");
9399 ret = error_mark_node;
9401 else
9403 /* Construct a location e.g.:
9404 ptr = new int[100]
9405 ^~~~~~~~~~~~
9406 with caret == start at the start of the "new" token, and the end
9407 at the end of the final token we consumed. */
9408 location_t combined_loc = make_location (start_loc, start_loc,
9409 parser->lexer);
9410 /* Create a representation of the new-expression. */
9411 ret = build_new (combined_loc, &placement, type, nelts, &initializer,
9412 global_scope_p, tf_warning_or_error);
9415 if (placement != NULL)
9416 release_tree_vector (placement);
9417 if (initializer != NULL)
9418 release_tree_vector (initializer);
9420 return ret;
9423 /* Parse a new-placement.
9425 new-placement:
9426 ( expression-list )
9428 Returns the same representation as for an expression-list. */
9430 static vec<tree, va_gc> *
9431 cp_parser_new_placement (cp_parser* parser)
9433 vec<tree, va_gc> *expression_list;
9435 /* Parse the expression-list. */
9436 expression_list = (cp_parser_parenthesized_expression_list
9437 (parser, non_attr, /*cast_p=*/false,
9438 /*allow_expansion_p=*/true,
9439 /*non_constant_p=*/NULL));
9441 if (expression_list && expression_list->is_empty ())
9442 error ("expected expression-list or type-id");
9444 return expression_list;
9447 /* Parse a new-type-id.
9449 new-type-id:
9450 type-specifier-seq new-declarator [opt]
9452 Returns the TYPE allocated. If the new-type-id indicates an array
9453 type, *NELTS is set to the number of elements in the last array
9454 bound; the TYPE will not include the last array bound. */
9456 static tree
9457 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
9459 cp_decl_specifier_seq type_specifier_seq;
9460 cp_declarator *new_declarator;
9461 cp_declarator *declarator;
9462 cp_declarator *outer_declarator;
9463 const char *saved_message;
9465 /* The type-specifier sequence must not contain type definitions.
9466 (It cannot contain declarations of new types either, but if they
9467 are not definitions we will catch that because they are not
9468 complete.) */
9469 saved_message = parser->type_definition_forbidden_message;
9470 parser->type_definition_forbidden_message
9471 = G_("types may not be defined in a new-type-id");
9472 /* Parse the type-specifier-seq. */
9473 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
9474 /*is_declaration=*/false,
9475 /*is_trailing_return=*/false,
9476 &type_specifier_seq);
9477 /* Restore the old message. */
9478 parser->type_definition_forbidden_message = saved_message;
9480 if (type_specifier_seq.type == error_mark_node)
9481 return error_mark_node;
9483 /* Parse the new-declarator. */
9484 new_declarator = cp_parser_new_declarator_opt (parser);
9486 /* Determine the number of elements in the last array dimension, if
9487 any. */
9488 *nelts = NULL_TREE;
9489 /* Skip down to the last array dimension. */
9490 declarator = new_declarator;
9491 outer_declarator = NULL;
9492 while (declarator && (declarator->kind == cdk_pointer
9493 || declarator->kind == cdk_ptrmem))
9495 outer_declarator = declarator;
9496 declarator = declarator->declarator;
9498 while (declarator
9499 && declarator->kind == cdk_array
9500 && declarator->declarator
9501 && declarator->declarator->kind == cdk_array)
9503 outer_declarator = declarator;
9504 declarator = declarator->declarator;
9507 if (declarator && declarator->kind == cdk_array)
9509 *nelts = declarator->u.array.bounds;
9510 if (*nelts == error_mark_node)
9511 *nelts = integer_one_node;
9513 if (*nelts == NULL_TREE)
9514 /* Leave [] in the declarator. */;
9515 else if (outer_declarator)
9516 outer_declarator->declarator = declarator->declarator;
9517 else
9518 new_declarator = NULL;
9521 return groktypename (&type_specifier_seq, new_declarator, false);
9524 /* Parse an (optional) new-declarator.
9526 new-declarator:
9527 ptr-operator new-declarator [opt]
9528 direct-new-declarator
9530 Returns the declarator. */
9532 static cp_declarator *
9533 cp_parser_new_declarator_opt (cp_parser* parser)
9535 enum tree_code code;
9536 tree type, std_attributes = NULL_TREE;
9537 cp_cv_quals cv_quals;
9539 /* We don't know if there's a ptr-operator next, or not. */
9540 cp_parser_parse_tentatively (parser);
9541 /* Look for a ptr-operator. */
9542 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
9543 /* If that worked, look for more new-declarators. */
9544 if (cp_parser_parse_definitely (parser))
9546 cp_declarator *declarator;
9548 /* Parse another optional declarator. */
9549 declarator = cp_parser_new_declarator_opt (parser);
9551 declarator = cp_parser_make_indirect_declarator
9552 (code, type, cv_quals, declarator, std_attributes);
9554 return declarator;
9557 /* If the next token is a `[', there is a direct-new-declarator. */
9558 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
9559 return cp_parser_direct_new_declarator (parser);
9561 return NULL;
9564 /* Parse a direct-new-declarator.
9566 direct-new-declarator:
9567 [ expression ]
9568 direct-new-declarator [constant-expression]
9572 static cp_declarator *
9573 cp_parser_direct_new_declarator (cp_parser* parser)
9575 cp_declarator *declarator = NULL;
9576 bool first_p = true;
9578 while (true)
9580 tree expression;
9581 cp_token *token;
9583 /* Look for the opening `['. */
9584 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9586 token = cp_lexer_peek_token (parser->lexer);
9587 if (token->type == CPP_CLOSE_SQUARE && first_p)
9588 expression = NULL_TREE;
9589 else
9590 expression = cp_parser_expression (parser);
9591 /* The standard requires that the expression have integral
9592 type. DR 74 adds enumeration types. We believe that the
9593 real intent is that these expressions be handled like the
9594 expression in a `switch' condition, which also allows
9595 classes with a single conversion to integral or
9596 enumeration type. */
9597 if (expression && !processing_template_decl)
9599 expression
9600 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
9601 expression,
9602 /*complain=*/true);
9603 if (!expression)
9605 error_at (token->location,
9606 "expression in new-declarator must have integral "
9607 "or enumeration type");
9608 expression = error_mark_node;
9612 /* Look for the closing `]'. */
9613 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9615 /* Add this bound to the declarator. */
9616 declarator = make_array_declarator (declarator, expression);
9618 /* If the next token is not a `[', then there are no more
9619 bounds. */
9620 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
9621 break;
9622 first_p = false;
9625 return declarator;
9628 /* Parse a new-initializer.
9630 new-initializer:
9631 ( expression-list [opt] )
9632 braced-init-list
9634 Returns a representation of the expression-list. */
9636 static vec<tree, va_gc> *
9637 cp_parser_new_initializer (cp_parser* parser)
9639 vec<tree, va_gc> *expression_list;
9641 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9643 tree t;
9644 bool expr_non_constant_p;
9645 cp_lexer_set_source_position (parser->lexer);
9646 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9647 t = cp_parser_braced_list (parser, &expr_non_constant_p);
9648 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
9649 expression_list = make_tree_vector_single (t);
9651 else
9652 expression_list = (cp_parser_parenthesized_expression_list
9653 (parser, non_attr, /*cast_p=*/false,
9654 /*allow_expansion_p=*/true,
9655 /*non_constant_p=*/NULL));
9657 return expression_list;
9660 /* Parse a delete-expression.
9662 delete-expression:
9663 :: [opt] delete cast-expression
9664 :: [opt] delete [ ] cast-expression
9666 Returns a representation of the expression. */
9668 static tree
9669 cp_parser_delete_expression (cp_parser* parser)
9671 bool global_scope_p;
9672 bool array_p;
9673 tree expression;
9674 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9676 /* Look for the optional `::' operator. */
9677 global_scope_p
9678 = (cp_parser_global_scope_opt (parser,
9679 /*current_scope_valid_p=*/false)
9680 != NULL_TREE);
9681 /* Look for the `delete' keyword. */
9682 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
9683 /* See if the array syntax is in use. */
9684 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
9686 /* Consume the `[' token. */
9687 cp_lexer_consume_token (parser->lexer);
9688 /* Look for the `]' token. */
9689 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9690 /* Remember that this is the `[]' construct. */
9691 array_p = true;
9693 else
9694 array_p = false;
9696 /* Parse the cast-expression. */
9697 expression = cp_parser_simple_cast_expression (parser);
9699 /* A delete-expression may not appear in an integral constant
9700 expression. */
9701 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
9702 return error_mark_node;
9704 /* Construct a location e.g.:
9705 delete [ ] ptr
9706 ^~~~~~~~~~~~~~
9707 with caret == start at the start of the "delete" token, and
9708 the end at the end of the final token we consumed. */
9709 location_t combined_loc = make_location (start_loc, start_loc,
9710 parser->lexer);
9711 expression = delete_sanity (combined_loc, expression, NULL_TREE, array_p,
9712 global_scope_p, tf_warning_or_error);
9714 return expression;
9717 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
9718 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
9719 0 otherwise. */
9721 static int
9722 cp_parser_tokens_start_cast_expression (cp_parser *parser)
9724 cp_token *token = cp_lexer_peek_token (parser->lexer);
9725 switch (token->type)
9727 case CPP_COMMA:
9728 case CPP_SEMICOLON:
9729 case CPP_QUERY:
9730 case CPP_COLON:
9731 case CPP_CLOSE_SQUARE:
9732 case CPP_CLOSE_PAREN:
9733 case CPP_CLOSE_BRACE:
9734 case CPP_OPEN_BRACE:
9735 case CPP_DOT:
9736 case CPP_DOT_STAR:
9737 case CPP_DEREF:
9738 case CPP_DEREF_STAR:
9739 case CPP_DIV:
9740 case CPP_MOD:
9741 case CPP_LSHIFT:
9742 case CPP_RSHIFT:
9743 case CPP_LESS:
9744 case CPP_GREATER:
9745 case CPP_LESS_EQ:
9746 case CPP_GREATER_EQ:
9747 case CPP_EQ_EQ:
9748 case CPP_NOT_EQ:
9749 case CPP_EQ:
9750 case CPP_MULT_EQ:
9751 case CPP_DIV_EQ:
9752 case CPP_MOD_EQ:
9753 case CPP_PLUS_EQ:
9754 case CPP_MINUS_EQ:
9755 case CPP_RSHIFT_EQ:
9756 case CPP_LSHIFT_EQ:
9757 case CPP_AND_EQ:
9758 case CPP_XOR_EQ:
9759 case CPP_OR_EQ:
9760 case CPP_XOR:
9761 case CPP_OR:
9762 case CPP_OR_OR:
9763 case CPP_EOF:
9764 case CPP_ELLIPSIS:
9765 return 0;
9767 case CPP_OPEN_PAREN:
9768 /* In ((type ()) () the last () isn't a valid cast-expression,
9769 so the whole must be parsed as postfix-expression. */
9770 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
9771 != CPP_CLOSE_PAREN;
9773 case CPP_OPEN_SQUARE:
9774 /* '[' may start a primary-expression in obj-c++ and in C++11,
9775 as a lambda-expression, eg, '(void)[]{}'. */
9776 if (cxx_dialect >= cxx11)
9777 return -1;
9778 return c_dialect_objc ();
9780 case CPP_PLUS_PLUS:
9781 case CPP_MINUS_MINUS:
9782 /* '++' and '--' may or may not start a cast-expression:
9784 struct T { void operator++(int); };
9785 void f() { (T())++; }
9789 int a;
9790 (int)++a; */
9791 return -1;
9793 default:
9794 return 1;
9798 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
9799 in the order: const_cast, static_cast, reinterpret_cast.
9801 Don't suggest dynamic_cast.
9803 Return the first legal cast kind found, or NULL otherwise. */
9805 static const char *
9806 get_cast_suggestion (tree dst_type, tree orig_expr)
9808 tree trial;
9810 /* Reuse the parser logic by attempting to build the various kinds of
9811 cast, with "complain" disabled.
9812 Identify the first such cast that is valid. */
9814 /* Don't attempt to run such logic within template processing. */
9815 if (processing_template_decl)
9816 return NULL;
9818 /* First try const_cast. */
9819 trial = build_const_cast (input_location, dst_type, orig_expr, tf_none);
9820 if (trial != error_mark_node)
9821 return "const_cast";
9823 /* If that fails, try static_cast. */
9824 trial = build_static_cast (input_location, dst_type, orig_expr, tf_none);
9825 if (trial != error_mark_node)
9826 return "static_cast";
9828 /* Finally, try reinterpret_cast. */
9829 trial = build_reinterpret_cast (input_location, dst_type, orig_expr,
9830 tf_none);
9831 if (trial != error_mark_node)
9832 return "reinterpret_cast";
9834 /* No such cast possible. */
9835 return NULL;
9838 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
9839 suggesting how to convert a C-style cast of the form:
9841 (DST_TYPE)ORIG_EXPR
9843 to a C++-style cast.
9845 The primary range of RICHLOC is asssumed to be that of the original
9846 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
9847 of the parens in the C-style cast. */
9849 static void
9850 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
9851 location_t close_paren_loc, tree orig_expr,
9852 tree dst_type)
9854 /* This function is non-trivial, so bail out now if the warning isn't
9855 going to be emitted. */
9856 if (!warn_old_style_cast)
9857 return;
9859 /* Try to find a legal C++ cast, trying them in order:
9860 const_cast, static_cast, reinterpret_cast. */
9861 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
9862 if (!cast_suggestion)
9863 return;
9865 /* Replace the open paren with "CAST_SUGGESTION<". */
9866 pretty_printer pp;
9867 pp_string (&pp, cast_suggestion);
9868 pp_less (&pp);
9869 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
9871 /* Replace the close paren with "> (". */
9872 rich_loc->add_fixit_replace (close_paren_loc, "> (");
9874 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
9875 rich_loc->add_fixit_insert_after (")");
9879 /* Parse a cast-expression.
9881 cast-expression:
9882 unary-expression
9883 ( type-id ) cast-expression
9885 ADDRESS_P is true iff the unary-expression is appearing as the
9886 operand of the `&' operator. CAST_P is true if this expression is
9887 the target of a cast.
9889 Returns a representation of the expression. */
9891 static cp_expr
9892 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
9893 bool decltype_p, cp_id_kind * pidk)
9895 /* If it's a `(', then we might be looking at a cast. */
9896 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9898 tree type = NULL_TREE;
9899 cp_expr expr (NULL_TREE);
9900 int cast_expression = 0;
9901 const char *saved_message;
9903 /* There's no way to know yet whether or not this is a cast.
9904 For example, `(int (3))' is a unary-expression, while `(int)
9905 3' is a cast. So, we resort to parsing tentatively. */
9906 cp_parser_parse_tentatively (parser);
9907 /* Types may not be defined in a cast. */
9908 saved_message = parser->type_definition_forbidden_message;
9909 parser->type_definition_forbidden_message
9910 = G_("types may not be defined in casts");
9911 /* Consume the `('. */
9912 matching_parens parens;
9913 cp_token *open_paren = parens.consume_open (parser);
9914 location_t open_paren_loc = open_paren->location;
9915 location_t close_paren_loc = UNKNOWN_LOCATION;
9917 /* A very tricky bit is that `(struct S) { 3 }' is a
9918 compound-literal (which we permit in C++ as an extension).
9919 But, that construct is not a cast-expression -- it is a
9920 postfix-expression. (The reason is that `(struct S) { 3 }.i'
9921 is legal; if the compound-literal were a cast-expression,
9922 you'd need an extra set of parentheses.) But, if we parse
9923 the type-id, and it happens to be a class-specifier, then we
9924 will commit to the parse at that point, because we cannot
9925 undo the action that is done when creating a new class. So,
9926 then we cannot back up and do a postfix-expression.
9928 Another tricky case is the following (c++/29234):
9930 struct S { void operator () (); };
9932 void foo ()
9934 ( S()() );
9937 As a type-id we parse the parenthesized S()() as a function
9938 returning a function, groktypename complains and we cannot
9939 back up in this case either.
9941 Therefore, we scan ahead to the closing `)', and check to see
9942 if the tokens after the `)' can start a cast-expression. Otherwise
9943 we are dealing with an unary-expression, a postfix-expression
9944 or something else.
9946 Yet another tricky case, in C++11, is the following (c++/54891):
9948 (void)[]{};
9950 The issue is that usually, besides the case of lambda-expressions,
9951 the parenthesized type-id cannot be followed by '[', and, eg, we
9952 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9953 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9954 we don't commit, we try a cast-expression, then an unary-expression.
9956 Save tokens so that we can put them back. */
9957 cp_lexer_save_tokens (parser->lexer);
9959 /* We may be looking at a cast-expression. */
9960 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
9961 /*consume_paren=*/true))
9962 cast_expression
9963 = cp_parser_tokens_start_cast_expression (parser);
9965 /* Roll back the tokens we skipped. */
9966 cp_lexer_rollback_tokens (parser->lexer);
9967 /* If we aren't looking at a cast-expression, simulate an error so
9968 that the call to cp_parser_error_occurred below returns true. */
9969 if (!cast_expression)
9970 cp_parser_simulate_error (parser);
9971 else
9973 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9974 parser->in_type_id_in_expr_p = true;
9975 /* Look for the type-id. */
9976 type = cp_parser_type_id (parser);
9977 /* Look for the closing `)'. */
9978 cp_token *close_paren = parens.require_close (parser);
9979 if (close_paren)
9980 close_paren_loc = close_paren->location;
9981 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9984 /* Restore the saved message. */
9985 parser->type_definition_forbidden_message = saved_message;
9987 /* At this point this can only be either a cast or a
9988 parenthesized ctor such as `(T ())' that looks like a cast to
9989 function returning T. */
9990 if (!cp_parser_error_occurred (parser))
9992 /* Only commit if the cast-expression doesn't start with
9993 '++', '--', or '[' in C++11. */
9994 if (cast_expression > 0)
9995 cp_parser_commit_to_topmost_tentative_parse (parser);
9997 expr = cp_parser_cast_expression (parser,
9998 /*address_p=*/false,
9999 /*cast_p=*/true,
10000 /*decltype_p=*/false,
10001 pidk);
10003 if (cp_parser_parse_definitely (parser))
10005 /* Warn about old-style casts, if so requested. */
10006 if (warn_old_style_cast
10007 && !in_system_header_at (input_location)
10008 && !VOID_TYPE_P (type)
10009 && current_lang_name != lang_name_c)
10011 gcc_rich_location rich_loc (input_location);
10012 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
10013 expr, type);
10014 warning_at (&rich_loc, OPT_Wold_style_cast,
10015 "use of old-style cast to %q#T", type);
10018 /* Only type conversions to integral or enumeration types
10019 can be used in constant-expressions. */
10020 if (!cast_valid_in_integral_constant_expression_p (type)
10021 && cp_parser_non_integral_constant_expression (parser,
10022 NIC_CAST))
10023 return error_mark_node;
10025 /* Perform the cast. */
10026 /* Make a location:
10027 (TYPE) EXPR
10028 ^~~~~~~~~~~
10029 with start==caret at the open paren, extending to the
10030 end of "expr". */
10031 location_t cast_loc = make_location (open_paren_loc,
10032 open_paren_loc,
10033 expr.get_finish ());
10034 expr = build_c_cast (cast_loc, type, expr);
10035 return expr;
10038 else
10039 cp_parser_abort_tentative_parse (parser);
10042 /* If we get here, then it's not a cast, so it must be a
10043 unary-expression. */
10044 return cp_parser_unary_expression (parser, pidk, address_p,
10045 cast_p, decltype_p);
10048 /* Parse a binary expression of the general form:
10050 pm-expression:
10051 cast-expression
10052 pm-expression .* cast-expression
10053 pm-expression ->* cast-expression
10055 multiplicative-expression:
10056 pm-expression
10057 multiplicative-expression * pm-expression
10058 multiplicative-expression / pm-expression
10059 multiplicative-expression % pm-expression
10061 additive-expression:
10062 multiplicative-expression
10063 additive-expression + multiplicative-expression
10064 additive-expression - multiplicative-expression
10066 shift-expression:
10067 additive-expression
10068 shift-expression << additive-expression
10069 shift-expression >> additive-expression
10071 relational-expression:
10072 shift-expression
10073 relational-expression < shift-expression
10074 relational-expression > shift-expression
10075 relational-expression <= shift-expression
10076 relational-expression >= shift-expression
10078 GNU Extension:
10080 relational-expression:
10081 relational-expression <? shift-expression
10082 relational-expression >? shift-expression
10084 equality-expression:
10085 relational-expression
10086 equality-expression == relational-expression
10087 equality-expression != relational-expression
10089 and-expression:
10090 equality-expression
10091 and-expression & equality-expression
10093 exclusive-or-expression:
10094 and-expression
10095 exclusive-or-expression ^ and-expression
10097 inclusive-or-expression:
10098 exclusive-or-expression
10099 inclusive-or-expression | exclusive-or-expression
10101 logical-and-expression:
10102 inclusive-or-expression
10103 logical-and-expression && inclusive-or-expression
10105 logical-or-expression:
10106 logical-and-expression
10107 logical-or-expression || logical-and-expression
10109 All these are implemented with a single function like:
10111 binary-expression:
10112 simple-cast-expression
10113 binary-expression <token> binary-expression
10115 CAST_P is true if this expression is the target of a cast.
10117 The binops_by_token map is used to get the tree codes for each <token> type.
10118 binary-expressions are associated according to a precedence table. */
10120 #define TOKEN_PRECEDENCE(token) \
10121 (((token->type == CPP_GREATER \
10122 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
10123 && !parser->greater_than_is_operator_p) \
10124 ? PREC_NOT_OPERATOR \
10125 : binops_by_token[token->type].prec)
10127 static cp_expr
10128 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
10129 bool no_toplevel_fold_p,
10130 bool decltype_p,
10131 enum cp_parser_prec prec,
10132 cp_id_kind * pidk)
10134 cp_parser_expression_stack stack;
10135 cp_parser_expression_stack_entry *sp = &stack[0];
10136 cp_parser_expression_stack_entry *disable_warnings_sp = NULL;
10137 cp_parser_expression_stack_entry current;
10138 cp_expr rhs;
10139 cp_token *token;
10140 enum tree_code rhs_type;
10141 enum cp_parser_prec new_prec, lookahead_prec;
10142 tree overload;
10144 /* Parse the first expression. */
10145 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
10146 ? TRUTH_NOT_EXPR : ERROR_MARK);
10147 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
10148 cast_p, decltype_p, pidk);
10149 current.prec = prec;
10151 if (cp_parser_error_occurred (parser))
10152 return error_mark_node;
10154 for (;;)
10156 /* Get an operator token. */
10157 token = cp_lexer_peek_token (parser->lexer);
10159 if (warn_cxx11_compat
10160 && token->type == CPP_RSHIFT
10161 && !parser->greater_than_is_operator_p)
10163 if (warning_at (token->location, OPT_Wc__11_compat,
10164 "%<>>%> operator is treated"
10165 " as two right angle brackets in C++11"))
10166 inform (token->location,
10167 "suggest parentheses around %<>>%> expression");
10170 new_prec = TOKEN_PRECEDENCE (token);
10171 if (new_prec != PREC_NOT_OPERATOR
10172 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10173 /* This is a fold-expression; handle it later. */
10174 new_prec = PREC_NOT_OPERATOR;
10176 /* Popping an entry off the stack means we completed a subexpression:
10177 - either we found a token which is not an operator (`>' where it is not
10178 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
10179 will happen repeatedly;
10180 - or, we found an operator which has lower priority. This is the case
10181 where the recursive descent *ascends*, as in `3 * 4 + 5' after
10182 parsing `3 * 4'. */
10183 if (new_prec <= current.prec)
10185 if (sp == stack)
10186 break;
10187 else
10188 goto pop;
10191 get_rhs:
10192 current.tree_type = binops_by_token[token->type].tree_type;
10193 current.loc = token->location;
10194 current.flags = token->flags;
10196 /* We used the operator token. */
10197 cp_lexer_consume_token (parser->lexer);
10199 /* For "false && x" or "true || x", x will never be executed;
10200 disable warnings while evaluating it. */
10201 if ((current.tree_type == TRUTH_ANDIF_EXPR
10202 && cp_fully_fold (current.lhs) == truthvalue_false_node)
10203 || (current.tree_type == TRUTH_ORIF_EXPR
10204 && cp_fully_fold (current.lhs) == truthvalue_true_node))
10206 disable_warnings_sp = sp;
10207 ++c_inhibit_evaluation_warnings;
10210 /* Extract another operand. It may be the RHS of this expression
10211 or the LHS of a new, higher priority expression. */
10212 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
10213 ? TRUTH_NOT_EXPR : ERROR_MARK);
10214 rhs = cp_parser_simple_cast_expression (parser);
10216 /* Get another operator token. Look up its precedence to avoid
10217 building a useless (immediately popped) stack entry for common
10218 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
10219 token = cp_lexer_peek_token (parser->lexer);
10220 lookahead_prec = TOKEN_PRECEDENCE (token);
10221 if (lookahead_prec != PREC_NOT_OPERATOR
10222 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10223 lookahead_prec = PREC_NOT_OPERATOR;
10224 if (lookahead_prec > new_prec)
10226 /* ... and prepare to parse the RHS of the new, higher priority
10227 expression. Since precedence levels on the stack are
10228 monotonically increasing, we do not have to care about
10229 stack overflows. */
10230 *sp = current;
10231 ++sp;
10232 current.lhs = rhs;
10233 current.lhs_type = rhs_type;
10234 current.prec = new_prec;
10235 new_prec = lookahead_prec;
10236 goto get_rhs;
10238 pop:
10239 lookahead_prec = new_prec;
10240 /* If the stack is not empty, we have parsed into LHS the right side
10241 (`4' in the example above) of an expression we had suspended.
10242 We can use the information on the stack to recover the LHS (`3')
10243 from the stack together with the tree code (`MULT_EXPR'), and
10244 the precedence of the higher level subexpression
10245 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
10246 which will be used to actually build the additive expression. */
10247 rhs = current.lhs;
10248 rhs_type = current.lhs_type;
10249 --sp;
10250 current = *sp;
10253 /* Undo the disabling of warnings done above. */
10254 if (sp == disable_warnings_sp)
10256 disable_warnings_sp = NULL;
10257 --c_inhibit_evaluation_warnings;
10260 if (warn_logical_not_paren
10261 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
10262 && current.lhs_type == TRUTH_NOT_EXPR
10263 /* Avoid warning for !!x == y. */
10264 && (TREE_CODE (current.lhs) != NE_EXPR
10265 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
10266 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
10267 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
10268 /* Avoid warning for !b == y where b is boolean. */
10269 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
10270 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
10271 != BOOLEAN_TYPE))))
10272 /* Avoid warning for !!b == y where b is boolean. */
10273 && (!(DECL_P (tree_strip_any_location_wrapper (current.lhs))
10274 || (TREE_CODE (current.lhs) == NON_LVALUE_EXPR
10275 && DECL_P (tree_strip_any_location_wrapper
10276 (TREE_OPERAND (current.lhs, 0)))))
10277 || TREE_TYPE (current.lhs) == NULL_TREE
10278 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
10279 warn_logical_not_parentheses (current.loc, current.tree_type,
10280 current.lhs, maybe_constant_value (rhs));
10282 if (warn_xor_used_as_pow
10283 && current.tree_type == BIT_XOR_EXPR
10284 /* Don't warn for named "xor" (as opposed to '^'). */
10285 && !(current.flags & NAMED_OP)
10286 && current.lhs.decimal_p ()
10287 && rhs.decimal_p ())
10288 check_for_xor_used_as_pow
10289 (current.lhs.get_location (),
10290 tree_strip_any_location_wrapper (current.lhs),
10291 current.loc,
10292 rhs.get_location (),
10293 tree_strip_any_location_wrapper (rhs));
10295 overload = NULL;
10297 location_t combined_loc = make_location (current.loc,
10298 current.lhs.get_start (),
10299 rhs.get_finish ());
10301 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
10302 ERROR_MARK for everything that is not a binary expression.
10303 This makes warn_about_parentheses miss some warnings that
10304 involve unary operators. For unary expressions we should
10305 pass the correct tree_code unless the unary expression was
10306 surrounded by parentheses.
10308 if (no_toplevel_fold_p
10309 && lookahead_prec <= current.prec
10310 && sp == stack)
10312 if (current.lhs == error_mark_node || rhs == error_mark_node)
10313 current.lhs = error_mark_node;
10314 else
10316 current.lhs.maybe_add_location_wrapper ();
10317 rhs.maybe_add_location_wrapper ();
10318 current.lhs
10319 = build_min (current.tree_type,
10320 TREE_CODE_CLASS (current.tree_type)
10321 == tcc_comparison
10322 ? boolean_type_node : TREE_TYPE (current.lhs),
10323 current.lhs.get_value (), rhs.get_value ());
10324 SET_EXPR_LOCATION (current.lhs, combined_loc);
10327 else
10329 op_location_t op_loc (current.loc, combined_loc);
10330 current.lhs = build_x_binary_op (op_loc, current.tree_type,
10331 current.lhs, current.lhs_type,
10332 rhs, rhs_type, NULL_TREE, &overload,
10333 complain_flags (decltype_p));
10334 /* TODO: build_x_binary_op doesn't always honor the location. */
10335 current.lhs.set_location (combined_loc);
10337 current.lhs_type = current.tree_type;
10339 /* If the binary operator required the use of an overloaded operator,
10340 then this expression cannot be an integral constant-expression.
10341 An overloaded operator can be used even if both operands are
10342 otherwise permissible in an integral constant-expression if at
10343 least one of the operands is of enumeration type. */
10345 if (overload
10346 && cp_parser_non_integral_constant_expression (parser,
10347 NIC_OVERLOADED))
10348 return error_mark_node;
10351 return current.lhs;
10354 static cp_expr
10355 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
10356 bool no_toplevel_fold_p,
10357 enum cp_parser_prec prec,
10358 cp_id_kind * pidk)
10360 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
10361 /*decltype*/false, prec, pidk);
10364 /* Parse the `? expression : assignment-expression' part of a
10365 conditional-expression. The LOGICAL_OR_EXPR is the
10366 logical-or-expression that started the conditional-expression.
10367 Returns a representation of the entire conditional-expression.
10369 This routine is used by cp_parser_assignment_expression
10370 and cp_parser_conditional_expression.
10372 ? expression : assignment-expression
10374 GNU Extensions:
10376 ? : assignment-expression */
10378 static tree
10379 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
10381 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
10382 cp_expr assignment_expr;
10383 struct cp_token *token;
10384 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10386 /* Consume the `?' token. */
10387 cp_lexer_consume_token (parser->lexer);
10388 token = cp_lexer_peek_token (parser->lexer);
10389 if (cp_parser_allow_gnu_extensions_p (parser)
10390 && token->type == CPP_COLON)
10392 pedwarn (token->location, OPT_Wpedantic,
10393 "ISO C++ does not allow %<?:%> with omitted middle operand");
10394 /* Implicit true clause. */
10395 expr = NULL_TREE;
10396 c_inhibit_evaluation_warnings +=
10397 folded_logical_or_expr == truthvalue_true_node;
10398 warn_for_omitted_condop (token->location, logical_or_expr);
10400 else
10402 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10403 parser->colon_corrects_to_scope_p = false;
10404 /* Parse the expression. */
10405 c_inhibit_evaluation_warnings +=
10406 folded_logical_or_expr == truthvalue_false_node;
10407 expr = cp_parser_expression (parser);
10408 c_inhibit_evaluation_warnings +=
10409 ((folded_logical_or_expr == truthvalue_true_node)
10410 - (folded_logical_or_expr == truthvalue_false_node));
10411 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10414 /* The next token should be a `:'. */
10415 cp_parser_require (parser, CPP_COLON, RT_COLON);
10416 /* Parse the assignment-expression. */
10417 assignment_expr = cp_parser_assignment_expression (parser);
10418 c_inhibit_evaluation_warnings -=
10419 folded_logical_or_expr == truthvalue_true_node;
10421 /* Make a location:
10422 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
10423 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
10424 with the caret at the "?", ranging from the start of
10425 the logical_or_expr to the end of the assignment_expr. */
10426 loc = make_location (loc,
10427 logical_or_expr.get_start (),
10428 assignment_expr.get_finish ());
10430 /* Build the conditional-expression. */
10431 return build_x_conditional_expr (loc, logical_or_expr,
10432 expr,
10433 assignment_expr,
10434 tf_warning_or_error);
10437 /* Parse a conditional-expression.
10439 conditional-expression:
10440 logical-or-expression
10441 logical-or-expression ? expression : assignment-expression
10443 GNU Extensions:
10445 logical-or-expression ? : assignment-expression */
10447 static cp_expr
10448 cp_parser_conditional_expression (cp_parser *parser)
10450 cp_expr expr = cp_parser_binary_expression (parser, false, false, false,
10451 PREC_NOT_OPERATOR, NULL);
10452 /* If the next token is a `?' then we're actually looking at
10453 a conditional-expression; otherwise we're done. */
10454 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
10455 return cp_parser_question_colon_clause (parser, expr);
10456 return expr;
10459 /* Parse an assignment-expression.
10461 assignment-expression:
10462 conditional-expression
10463 logical-or-expression assignment-operator assignment_expression
10464 throw-expression
10465 yield-expression
10467 CAST_P is true if this expression is the target of a cast.
10468 DECLTYPE_P is true if this expression is the operand of decltype.
10470 Returns a representation for the expression. */
10472 static cp_expr
10473 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
10474 bool cast_p, bool decltype_p)
10476 cp_expr expr;
10478 /* If the next token is the `throw' keyword, then we're looking at
10479 a throw-expression. */
10480 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
10481 expr = cp_parser_throw_expression (parser);
10482 /* If the next token is the `co_yield' keyword, then we're looking at
10483 a yield-expression. */
10484 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CO_YIELD))
10485 expr = cp_parser_yield_expression (parser);
10486 /* Otherwise, it must be that we are looking at a
10487 logical-or-expression. */
10488 else
10490 /* Parse the binary expressions (logical-or-expression). */
10491 expr = cp_parser_binary_expression (parser, cast_p, false,
10492 decltype_p,
10493 PREC_NOT_OPERATOR, pidk);
10494 /* If the next token is a `?' then we're actually looking at a
10495 conditional-expression. */
10496 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
10497 return cp_parser_question_colon_clause (parser, expr);
10498 else
10500 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10502 /* If it's an assignment-operator, we're using the second
10503 production. */
10504 enum tree_code assignment_operator
10505 = cp_parser_assignment_operator_opt (parser);
10506 if (assignment_operator != ERROR_MARK)
10508 bool non_constant_p;
10510 /* Parse the right-hand side of the assignment. */
10511 cp_expr rhs = cp_parser_initializer_clause (parser,
10512 &non_constant_p);
10514 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
10515 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10517 /* An assignment may not appear in a
10518 constant-expression. */
10519 if (cp_parser_non_integral_constant_expression (parser,
10520 NIC_ASSIGNMENT))
10521 return error_mark_node;
10522 /* Build the assignment expression. Its default
10523 location:
10524 LHS = RHS
10525 ~~~~^~~~~
10526 is the location of the '=' token as the
10527 caret, ranging from the start of the lhs to the
10528 end of the rhs. */
10529 loc = make_location (loc,
10530 expr.get_start (),
10531 rhs.get_finish ());
10532 expr = build_x_modify_expr (loc, expr,
10533 assignment_operator,
10534 rhs, NULL_TREE,
10535 complain_flags (decltype_p));
10536 /* TODO: build_x_modify_expr doesn't honor the location,
10537 so we must set it here. */
10538 expr.set_location (loc);
10543 return expr;
10546 /* Parse an (optional) assignment-operator.
10548 assignment-operator: one of
10549 = *= /= %= += -= >>= <<= &= ^= |=
10551 GNU Extension:
10553 assignment-operator: one of
10554 <?= >?=
10556 If the next token is an assignment operator, the corresponding tree
10557 code is returned, and the token is consumed. For example, for
10558 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
10559 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
10560 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
10561 operator, ERROR_MARK is returned. */
10563 static enum tree_code
10564 cp_parser_assignment_operator_opt (cp_parser* parser)
10566 enum tree_code op;
10567 cp_token *token;
10569 /* Peek at the next token. */
10570 token = cp_lexer_peek_token (parser->lexer);
10572 switch (token->type)
10574 case CPP_EQ:
10575 op = NOP_EXPR;
10576 break;
10578 case CPP_MULT_EQ:
10579 op = MULT_EXPR;
10580 break;
10582 case CPP_DIV_EQ:
10583 op = TRUNC_DIV_EXPR;
10584 break;
10586 case CPP_MOD_EQ:
10587 op = TRUNC_MOD_EXPR;
10588 break;
10590 case CPP_PLUS_EQ:
10591 op = PLUS_EXPR;
10592 break;
10594 case CPP_MINUS_EQ:
10595 op = MINUS_EXPR;
10596 break;
10598 case CPP_RSHIFT_EQ:
10599 op = RSHIFT_EXPR;
10600 break;
10602 case CPP_LSHIFT_EQ:
10603 op = LSHIFT_EXPR;
10604 break;
10606 case CPP_AND_EQ:
10607 op = BIT_AND_EXPR;
10608 break;
10610 case CPP_XOR_EQ:
10611 op = BIT_XOR_EXPR;
10612 break;
10614 case CPP_OR_EQ:
10615 op = BIT_IOR_EXPR;
10616 break;
10618 default:
10619 /* Nothing else is an assignment operator. */
10620 op = ERROR_MARK;
10623 /* An operator followed by ... is a fold-expression, handled elsewhere. */
10624 if (op != ERROR_MARK
10625 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10626 op = ERROR_MARK;
10628 /* If it was an assignment operator, consume it. */
10629 if (op != ERROR_MARK)
10630 cp_lexer_consume_token (parser->lexer);
10632 return op;
10635 /* Parse an expression.
10637 expression:
10638 assignment-expression
10639 expression , assignment-expression
10641 CAST_P is true if this expression is the target of a cast.
10642 DECLTYPE_P is true if this expression is the immediate operand of decltype,
10643 except possibly parenthesized or on the RHS of a comma (N3276).
10644 WARN_COMMA_P is true if a comma should be diagnosed.
10646 Returns a representation of the expression. */
10648 static cp_expr
10649 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
10650 bool cast_p, bool decltype_p, bool warn_comma_p)
10652 cp_expr expression = NULL_TREE;
10653 location_t loc = UNKNOWN_LOCATION;
10655 while (true)
10657 cp_expr assignment_expression;
10659 /* Parse the next assignment-expression. */
10660 assignment_expression
10661 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
10663 /* We don't create a temporary for a call that is the immediate operand
10664 of decltype or on the RHS of a comma. But when we see a comma, we
10665 need to create a temporary for a call on the LHS. */
10666 if (decltype_p && !processing_template_decl
10667 && TREE_CODE (assignment_expression) == CALL_EXPR
10668 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
10669 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10670 assignment_expression
10671 = build_cplus_new (TREE_TYPE (assignment_expression),
10672 assignment_expression, tf_warning_or_error);
10674 /* If this is the first assignment-expression, we can just
10675 save it away. */
10676 if (!expression)
10677 expression = assignment_expression;
10678 else
10680 /* Create a location with caret at the comma, ranging
10681 from the start of the LHS to the end of the RHS. */
10682 loc = make_location (loc,
10683 expression.get_start (),
10684 assignment_expression.get_finish ());
10685 expression = build_x_compound_expr (loc, expression,
10686 assignment_expression, NULL_TREE,
10687 complain_flags (decltype_p));
10688 expression.set_location (loc);
10690 /* If the next token is not a comma, or we're in a fold-expression, then
10691 we are done with the expression. */
10692 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
10693 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10694 break;
10695 /* Consume the `,'. */
10696 loc = cp_lexer_peek_token (parser->lexer)->location;
10697 if (warn_comma_p)
10699 /* [depr.comma.subscript]: A comma expression appearing as
10700 the expr-or-braced-init-list of a subscripting expression
10701 is deprecated. A parenthesized comma expression is not
10702 deprecated. */
10703 warning_at (loc, OPT_Wcomma_subscript,
10704 "top-level comma expression in array subscript "
10705 "is deprecated");
10706 warn_comma_p = false;
10708 cp_lexer_consume_token (parser->lexer);
10709 /* A comma operator cannot appear in a constant-expression. */
10710 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
10711 expression = error_mark_node;
10714 return expression;
10717 /* Parse a constant-expression.
10719 constant-expression:
10720 conditional-expression
10722 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
10723 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
10724 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
10725 is false, NON_CONSTANT_P should be NULL. If ALLOW_NON_CONSTANT_P is
10726 greater than 1, this isn't really a constant-expression, only a
10727 potentially constant-evaluated expression. If STRICT_P is true,
10728 only parse a conditional-expression, otherwise parse an
10729 assignment-expression. See below for rationale. */
10731 static cp_expr
10732 cp_parser_constant_expression (cp_parser* parser,
10733 int allow_non_constant_p /* = 0 */,
10734 bool *non_constant_p /* = NULL */,
10735 bool strict_p /* = false */)
10737 bool saved_integral_constant_expression_p;
10738 bool saved_allow_non_integral_constant_expression_p;
10739 bool saved_non_integral_constant_expression_p;
10740 cp_expr expression;
10742 /* It might seem that we could simply parse the
10743 conditional-expression, and then check to see if it were
10744 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
10745 one that the compiler can figure out is constant, possibly after
10746 doing some simplifications or optimizations. The standard has a
10747 precise definition of constant-expression, and we must honor
10748 that, even though it is somewhat more restrictive.
10750 For example:
10752 int i[(2, 3)];
10754 is not a legal declaration, because `(2, 3)' is not a
10755 constant-expression. The `,' operator is forbidden in a
10756 constant-expression. However, GCC's constant-folding machinery
10757 will fold this operation to an INTEGER_CST for `3'. */
10759 /* Save the old settings. */
10760 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
10761 saved_allow_non_integral_constant_expression_p
10762 = parser->allow_non_integral_constant_expression_p;
10763 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
10764 /* We are now parsing a constant-expression. */
10765 parser->integral_constant_expression_p = true;
10766 parser->allow_non_integral_constant_expression_p
10767 = (allow_non_constant_p || cxx_dialect >= cxx11);
10768 parser->non_integral_constant_expression_p = false;
10770 /* A manifestly constant-evaluated expression is evaluated even in an
10771 unevaluated operand. */
10772 cp_evaluated ev (/*reset if*/allow_non_constant_p <= 1);
10774 /* Although the grammar says "conditional-expression", when not STRICT_P,
10775 we parse an "assignment-expression", which also permits
10776 "throw-expression" and the use of assignment operators. In the case
10777 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
10778 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
10779 actually essential that we look for an assignment-expression.
10780 For example, cp_parser_initializer_clauses uses this function to
10781 determine whether a particular assignment-expression is in fact
10782 constant. */
10783 if (strict_p)
10784 expression = cp_parser_conditional_expression (parser);
10785 else
10786 expression = cp_parser_assignment_expression (parser);
10787 /* Restore the old settings. */
10788 parser->integral_constant_expression_p
10789 = saved_integral_constant_expression_p;
10790 parser->allow_non_integral_constant_expression_p
10791 = saved_allow_non_integral_constant_expression_p;
10792 if (cxx_dialect >= cxx11)
10794 /* Require an rvalue constant expression here; that's what our
10795 callers expect. Reference constant expressions are handled
10796 separately in e.g. cp_parser_template_argument. */
10797 tree decay = expression;
10798 if (TREE_TYPE (expression)
10799 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
10800 decay = build_address (expression);
10801 bool is_const = is_rvalue_constant_expression (decay);
10802 parser->non_integral_constant_expression_p = !is_const;
10803 if (!is_const && !allow_non_constant_p)
10804 require_rvalue_constant_expression (decay);
10806 if (allow_non_constant_p)
10807 *non_constant_p = parser->non_integral_constant_expression_p;
10808 parser->non_integral_constant_expression_p
10809 = saved_non_integral_constant_expression_p;
10811 return expression;
10814 /* Parse __builtin_offsetof.
10816 offsetof-expression:
10817 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
10819 offsetof-member-designator:
10820 id-expression
10821 | offsetof-member-designator "." id-expression
10822 | offsetof-member-designator "[" expression "]"
10823 | offsetof-member-designator "->" id-expression */
10825 static cp_expr
10826 cp_parser_builtin_offsetof (cp_parser *parser)
10828 int save_ice_p, save_non_ice_p;
10829 tree type;
10830 cp_expr expr;
10831 cp_id_kind dummy;
10832 cp_token *token;
10833 location_t finish_loc;
10835 /* We're about to accept non-integral-constant things, but will
10836 definitely yield an integral constant expression. Save and
10837 restore these values around our local parsing. */
10838 save_ice_p = parser->integral_constant_expression_p;
10839 save_non_ice_p = parser->non_integral_constant_expression_p;
10841 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10843 /* Consume the "__builtin_offsetof" token. */
10844 cp_lexer_consume_token (parser->lexer);
10845 /* Consume the opening `('. */
10846 matching_parens parens;
10847 parens.require_open (parser);
10848 /* Parse the type-id. */
10849 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10851 const char *saved_message = parser->type_definition_forbidden_message;
10852 parser->type_definition_forbidden_message
10853 = G_("types may not be defined within %<__builtin_offsetof%>");
10854 type = cp_parser_type_id (parser);
10855 parser->type_definition_forbidden_message = saved_message;
10857 /* Look for the `,'. */
10858 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10859 token = cp_lexer_peek_token (parser->lexer);
10861 /* Build the (type *)null that begins the traditional offsetof macro. */
10862 tree object_ptr
10863 = build_static_cast (input_location, build_pointer_type (type),
10864 null_pointer_node, tf_warning_or_error);
10866 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
10867 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
10868 true, &dummy, token->location);
10869 while (true)
10871 token = cp_lexer_peek_token (parser->lexer);
10872 switch (token->type)
10874 case CPP_OPEN_SQUARE:
10875 /* offsetof-member-designator "[" expression "]" */
10876 expr = cp_parser_postfix_open_square_expression (parser, expr,
10877 true, false);
10878 break;
10880 case CPP_DEREF:
10881 /* offsetof-member-designator "->" identifier */
10882 expr = grok_array_decl (token->location, expr, integer_zero_node,
10883 NULL, tf_warning_or_error);
10884 /* FALLTHRU */
10886 case CPP_DOT:
10887 /* offsetof-member-designator "." identifier */
10888 cp_lexer_consume_token (parser->lexer);
10889 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
10890 expr, true, &dummy,
10891 token->location);
10892 break;
10894 case CPP_CLOSE_PAREN:
10895 /* Consume the ")" token. */
10896 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10897 cp_lexer_consume_token (parser->lexer);
10898 goto success;
10900 default:
10901 /* Error. We know the following require will fail, but
10902 that gives the proper error message. */
10903 parens.require_close (parser);
10904 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
10905 expr = error_mark_node;
10906 goto failure;
10910 success:
10911 /* Make a location of the form:
10912 __builtin_offsetof (struct s, f)
10913 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
10914 with caret at the type-id, ranging from the start of the
10915 "_builtin_offsetof" token to the close paren. */
10916 loc = make_location (loc, start_loc, finish_loc);
10917 /* The result will be an INTEGER_CST, so we need to explicitly
10918 preserve the location. */
10919 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
10921 failure:
10922 parser->integral_constant_expression_p = save_ice_p;
10923 parser->non_integral_constant_expression_p = save_non_ice_p;
10925 expr = expr.maybe_add_location_wrapper ();
10926 return expr;
10929 /* Parse a builtin trait expression or type. */
10931 static cp_expr
10932 cp_parser_trait (cp_parser* parser, enum rid keyword)
10934 cp_trait_kind kind;
10935 tree type1, type2 = NULL_TREE;
10936 bool binary = false;
10937 bool variadic = false;
10938 bool type = false;
10940 switch (keyword)
10942 #define DEFTRAIT(TCC, CODE, NAME, ARITY) \
10943 case RID_##CODE: \
10944 kind = CPTK_##CODE; \
10945 binary = (ARITY == 2); \
10946 variadic = (ARITY == -1); \
10947 type = (TCC == tcc_type); \
10948 break;
10949 #include "cp-trait.def"
10950 #undef DEFTRAIT
10951 default:
10952 gcc_unreachable ();
10955 /* Get location of initial token. */
10956 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10958 /* Consume the token. */
10959 cp_lexer_consume_token (parser->lexer);
10961 matching_parens parens;
10962 if (kind == CPTK_TYPE_PACK_ELEMENT)
10963 cp_parser_require (parser, CPP_LESS, RT_LESS);
10964 else
10965 parens.require_open (parser);
10967 if (kind == CPTK_IS_DEDUCIBLE)
10969 const cp_token* token = cp_lexer_peek_token (parser->lexer);
10970 type1 = cp_parser_id_expression (parser,
10971 /*template_keyword_p=*/false,
10972 /*check_dependency_p=*/true,
10973 nullptr,
10974 /*declarator_p=*/false,
10975 /*optional_p=*/false);
10976 type1 = cp_parser_lookup_name_simple (parser, type1, token->location);
10978 else if (kind == CPTK_TYPE_PACK_ELEMENT)
10979 /* __type_pack_element takes an expression as its first argument and uses
10980 template-id syntax instead of function call syntax (for consistency
10981 with Clang). We special case these properties of __type_pack_element
10982 here and elsewhere. */
10983 type1 = cp_parser_constant_expression (parser);
10984 else
10986 type_id_in_expr_sentinel s (parser);
10987 type1 = cp_parser_type_id (parser);
10990 if (type1 == error_mark_node)
10991 return error_mark_node;
10993 if (kind == CPTK_TYPE_PACK_ELEMENT)
10995 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10996 tree trailing = cp_parser_enclosed_template_argument_list (parser);
10997 for (tree elt : tree_vec_range (trailing))
10999 if (!TYPE_P (elt))
11001 error_at (cp_expr_loc_or_input_loc (elt),
11002 "trailing argument to %<__type_pack_element%> "
11003 "is not a type");
11004 return error_mark_node;
11007 type2 = trailing;
11009 else if (binary)
11011 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
11014 type_id_in_expr_sentinel s (parser);
11015 type2 = cp_parser_type_id (parser);
11018 if (type2 == error_mark_node)
11019 return error_mark_node;
11021 else if (variadic)
11023 auto_vec<tree, 4> trailing;
11024 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11026 cp_lexer_consume_token (parser->lexer);
11027 tree elt = cp_parser_type_id (parser);
11028 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11030 cp_lexer_consume_token (parser->lexer);
11031 elt = make_pack_expansion (elt);
11033 if (elt == error_mark_node)
11034 return error_mark_node;
11035 trailing.safe_push (elt);
11037 type2 = make_tree_vec (trailing.length ());
11038 for (int i = 0; i < TREE_VEC_LENGTH (type2); ++i)
11039 TREE_VEC_ELT (type2, i) = trailing[i];
11042 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
11043 if (kind == CPTK_TYPE_PACK_ELEMENT)
11044 /* cp_parser_enclosed_template_argument_list above already took care
11045 of parsing the closing '>'. */;
11046 else
11047 parens.require_close (parser);
11049 /* Construct a location of the form:
11050 __is_trivially_copyable(_Tp)
11051 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
11052 with start == caret, finishing at the close-paren. */
11053 location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
11055 /* Complete the trait expression, which may mean either processing
11056 the trait expr now or saving it for template instantiation. */
11057 switch (kind)
11059 case CPTK_BASES:
11060 return cp_expr (finish_bases (type1, false), trait_loc);
11061 case CPTK_DIRECT_BASES:
11062 return cp_expr (finish_bases (type1, true), trait_loc);
11063 default:
11064 if (type)
11065 return finish_trait_type (kind, type1, type2, tf_warning_or_error);
11066 else
11067 return finish_trait_expr (trait_loc, kind, type1, type2);
11071 /* Parse a lambda expression.
11073 lambda-expression:
11074 lambda-introducer lambda-declarator [opt] compound-statement
11075 lambda-introducer < template-parameter-list > requires-clause [opt]
11076 lambda-declarator [opt] compound-statement
11078 Returns a representation of the expression. */
11080 static cp_expr
11081 cp_parser_lambda_expression (cp_parser* parser)
11083 tree lambda_expr = build_lambda_expr ();
11084 tree type;
11085 bool ok = true;
11086 cp_token *token = cp_lexer_peek_token (parser->lexer);
11087 cp_token_position start = 0;
11089 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
11091 if (cxx_dialect >= cxx20)
11093 /* C++20 allows lambdas in unevaluated context, but one in the type of a
11094 non-type parameter is nonsensical.
11096 Distinguish a lambda in the parameter type from a lambda in the
11097 default argument by looking at local_variables_forbidden_p, which is
11098 only set in default arguments. */
11099 if (processing_template_parmlist && !parser->local_variables_forbidden_p)
11101 error_at (token->location,
11102 "lambda-expression in template parameter type");
11103 token->error_reported = true;
11104 ok = false;
11107 else if (cp_unevaluated_operand)
11109 if (!token->error_reported)
11111 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
11112 "lambda-expression in unevaluated context"
11113 " only available with %<-std=c++20%> or %<-std=gnu++20%>");
11114 token->error_reported = true;
11116 ok = false;
11118 else if (parser->in_template_argument_list_p || processing_template_parmlist)
11120 if (!token->error_reported)
11122 error_at (token->location, "lambda-expression in template-argument"
11123 " only available with %<-std=c++20%> or %<-std=gnu++20%>");
11124 token->error_reported = true;
11126 ok = false;
11129 /* We may be in the middle of deferred access check. Disable
11130 it now. */
11131 push_deferring_access_checks (dk_no_deferred);
11133 cp_parser_lambda_introducer (parser, lambda_expr);
11134 if (cp_parser_error_occurred (parser))
11135 return error_mark_node;
11137 type = begin_lambda_type (lambda_expr);
11138 if (type == error_mark_node)
11139 return error_mark_node;
11141 record_lambda_scope (lambda_expr);
11142 record_lambda_scope_discriminator (lambda_expr);
11144 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
11145 determine_visibility (TYPE_NAME (type));
11147 /* Now that we've started the type, add the capture fields for any
11148 explicit captures. */
11149 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
11152 /* Inside the class, surrounding template-parameter-lists do not apply. */
11153 unsigned int saved_num_template_parameter_lists
11154 = parser->num_template_parameter_lists;
11155 unsigned char in_statement = parser->in_statement;
11156 bool in_switch_statement_p = parser->in_switch_statement_p;
11157 bool fully_implicit_function_template_p
11158 = parser->fully_implicit_function_template_p;
11159 tree implicit_template_parms = parser->implicit_template_parms;
11160 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
11161 bool auto_is_implicit_function_template_parm_p
11162 = parser->auto_is_implicit_function_template_parm_p;
11164 parser->num_template_parameter_lists = 0;
11165 parser->in_statement = 0;
11166 parser->in_switch_statement_p = false;
11167 parser->fully_implicit_function_template_p = false;
11168 parser->implicit_template_parms = 0;
11169 parser->implicit_template_scope = 0;
11170 parser->auto_is_implicit_function_template_parm_p = false;
11172 /* The body of a lambda in a discarded statement is not discarded. */
11173 bool discarded = in_discarded_stmt;
11174 in_discarded_stmt = 0;
11176 /* Similarly the body of a lambda in immediate function context is not
11177 in immediate function context. */
11178 bool save_in_consteval_if_p = in_consteval_if_p;
11179 in_consteval_if_p = false;
11181 /* By virtue of defining a local class, a lambda expression has access to
11182 the private variables of enclosing classes. */
11184 if (cp_parser_start_tentative_firewall (parser))
11185 start = token;
11187 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
11189 if (ok && cp_parser_error_occurred (parser))
11190 ok = false;
11192 if (ok)
11193 cp_parser_lambda_body (parser, lambda_expr);
11194 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
11196 if (cp_parser_skip_to_closing_brace (parser))
11197 cp_lexer_consume_token (parser->lexer);
11200 /* The capture list was built up in reverse order; fix that now. */
11201 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
11202 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
11204 if (ok)
11205 maybe_add_lambda_conv_op (type);
11207 finish_struct (type, /*attributes=*/NULL_TREE);
11209 in_consteval_if_p = save_in_consteval_if_p;
11210 in_discarded_stmt = discarded;
11212 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
11213 parser->in_statement = in_statement;
11214 parser->in_switch_statement_p = in_switch_statement_p;
11215 parser->fully_implicit_function_template_p
11216 = fully_implicit_function_template_p;
11217 parser->implicit_template_parms = implicit_template_parms;
11218 parser->implicit_template_scope = implicit_template_scope;
11219 parser->auto_is_implicit_function_template_parm_p
11220 = auto_is_implicit_function_template_parm_p;
11223 /* This field is only used during parsing of the lambda. */
11224 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
11226 /* This lambda shouldn't have any proxies left at this point. */
11227 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
11228 /* And now that we're done, push proxies for an enclosing lambda. */
11229 insert_pending_capture_proxies ();
11231 /* Update the lambda expression to a range. */
11232 LAMBDA_EXPR_LOCATION (lambda_expr) = make_location (token->location,
11233 token->location,
11234 parser->lexer);
11236 if (ok)
11237 lambda_expr = build_lambda_object (lambda_expr);
11238 else
11239 lambda_expr = error_mark_node;
11241 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
11243 pop_deferring_access_checks ();
11245 return lambda_expr;
11248 /* Parse the beginning of a lambda expression.
11250 lambda-introducer:
11251 [ lambda-capture [opt] ]
11253 LAMBDA_EXPR is the current representation of the lambda expression. */
11255 static void
11256 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
11258 /* Need commas after the first capture. */
11259 bool first = true;
11261 /* Eat the leading `['. */
11262 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
11264 /* Record default capture mode. "[&" "[=" "[&," "[=," */
11265 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
11266 && !cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS)
11267 && !cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME)
11268 && !cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
11269 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
11270 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11271 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
11273 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
11275 cp_lexer_consume_token (parser->lexer);
11276 first = false;
11278 if (!(at_function_scope_p () || parsing_nsdmi ()))
11279 error ("non-local lambda expression cannot have a capture-default");
11282 hash_set<tree, true> ids;
11283 tree first_capture_id = NULL_TREE;
11284 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
11286 cp_token* capture_token;
11287 tree capture_id;
11288 tree capture_init_expr;
11289 cp_id_kind idk = CP_ID_KIND_NONE;
11290 bool explicit_init_p = false;
11292 enum capture_kind_type
11294 BY_COPY,
11295 BY_REFERENCE
11297 enum capture_kind_type capture_kind = BY_COPY;
11299 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
11301 error ("expected end of capture-list");
11302 return;
11305 if (first)
11306 first = false;
11307 else
11308 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
11310 /* Possibly capture `this'. */
11311 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
11313 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11314 if (cxx_dialect < cxx20 && pedantic
11315 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
11316 pedwarn (loc, OPT_Wc__20_extensions,
11317 "explicit by-copy capture of %<this%> "
11318 "with by-copy capture default only available with "
11319 "%<-std=c++20%> or %<-std=gnu++20%>");
11320 cp_lexer_consume_token (parser->lexer);
11321 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
11322 pedwarn (input_location, 0,
11323 "already captured %qD in lambda expression",
11324 this_identifier);
11325 else
11326 add_capture (lambda_expr, /*id=*/this_identifier,
11327 /*initializer=*/finish_this_expr (),
11328 /*by_reference_p=*/true, explicit_init_p);
11329 continue;
11332 /* Possibly capture `*this'. */
11333 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
11334 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
11336 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11337 if (cxx_dialect < cxx17)
11338 pedwarn (loc, OPT_Wc__17_extensions,
11339 "%<*this%> capture only available with "
11340 "%<-std=c++17%> or %<-std=gnu++17%>");
11341 cp_lexer_consume_token (parser->lexer);
11342 cp_lexer_consume_token (parser->lexer);
11343 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
11344 pedwarn (input_location, 0,
11345 "already captured %qD in lambda expression",
11346 this_identifier);
11347 else
11348 add_capture (lambda_expr, /*id=*/this_identifier,
11349 /*initializer=*/finish_this_expr (),
11350 /*by_reference_p=*/false, explicit_init_p);
11351 continue;
11354 /* But reject `&this'. */
11355 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
11356 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
11358 error_at (cp_lexer_peek_token (parser->lexer)->location,
11359 "%<this%> cannot be captured by reference");
11360 cp_lexer_consume_token (parser->lexer);
11361 cp_lexer_consume_token (parser->lexer);
11362 continue;
11365 /* Remember whether we want to capture as a reference or not. */
11366 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
11368 capture_kind = BY_REFERENCE;
11369 cp_lexer_consume_token (parser->lexer);
11372 bool init_pack_expansion = false;
11373 location_t ellipsis_loc = UNKNOWN_LOCATION;
11374 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11376 ellipsis_loc = cp_lexer_peek_token (parser->lexer)->location;
11377 if (cxx_dialect < cxx20)
11378 pedwarn (ellipsis_loc, OPT_Wc__20_extensions,
11379 "pack init-capture only available with "
11380 "%<-std=c++20%> or %<-std=gnu++20%>");
11381 cp_lexer_consume_token (parser->lexer);
11382 init_pack_expansion = true;
11385 /* Early C++20 drafts had ...& instead of &...; be forgiving. */
11386 if (init_pack_expansion && capture_kind != BY_REFERENCE
11387 && cp_lexer_next_token_is (parser->lexer, CPP_AND))
11389 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
11390 0, "%<&%> should come before %<...%>");
11391 capture_kind = BY_REFERENCE;
11392 cp_lexer_consume_token (parser->lexer);
11395 /* Get the identifier. */
11396 capture_token = cp_lexer_peek_token (parser->lexer);
11397 capture_id = cp_parser_identifier (parser);
11399 if (capture_id == error_mark_node)
11400 /* Would be nice to have a cp_parser_skip_to_closing_x for general
11401 delimiters, but I modified this to stop on unnested ']' as well. It
11402 was already changed to stop on unnested '}', so the
11403 "closing_parenthesis" name is no more misleading with my change. */
11405 cp_parser_skip_to_closing_parenthesis (parser,
11406 /*recovering=*/true,
11407 /*or_comma=*/true,
11408 /*consume_paren=*/true);
11409 break;
11412 /* Find the initializer for this capture. */
11413 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
11414 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
11415 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11417 bool direct, non_constant;
11418 /* An explicit initializer exists. */
11419 if (cxx_dialect < cxx14)
11420 pedwarn (input_location, OPT_Wc__14_extensions,
11421 "lambda capture initializers "
11422 "only available with %<-std=c++14%> or %<-std=gnu++14%>");
11423 capture_init_expr = cp_parser_initializer (parser, &direct,
11424 &non_constant, true);
11425 explicit_init_p = true;
11426 if (capture_init_expr == NULL_TREE)
11428 error ("empty initializer for lambda init-capture");
11429 capture_init_expr = error_mark_node;
11431 if (init_pack_expansion)
11432 capture_init_expr = make_pack_expansion (capture_init_expr);
11434 else
11436 const char* error_msg;
11438 /* Turn the identifier into an id-expression. */
11439 capture_init_expr
11440 = cp_parser_lookup_name_simple (parser, capture_id,
11441 capture_token->location);
11443 if (capture_init_expr == error_mark_node)
11445 unqualified_name_lookup_error (capture_id);
11446 continue;
11448 else if (!VAR_P (capture_init_expr)
11449 && TREE_CODE (capture_init_expr) != PARM_DECL)
11451 error_at (capture_token->location,
11452 "capture of non-variable %qE",
11453 capture_init_expr);
11454 if (DECL_P (capture_init_expr))
11455 inform (DECL_SOURCE_LOCATION (capture_init_expr),
11456 "%q#D declared here", capture_init_expr);
11457 continue;
11459 if (VAR_P (capture_init_expr)
11460 && decl_storage_duration (capture_init_expr) != dk_auto)
11462 if (pedwarn (capture_token->location, 0, "capture of variable "
11463 "%qD with non-automatic storage duration",
11464 capture_init_expr))
11465 inform (DECL_SOURCE_LOCATION (capture_init_expr),
11466 "%q#D declared here", capture_init_expr);
11467 continue;
11470 capture_init_expr
11471 = finish_id_expression
11472 (capture_id,
11473 capture_init_expr,
11474 parser->scope,
11475 &idk,
11476 /*integral_constant_expression_p=*/false,
11477 /*allow_non_integral_constant_expression_p=*/false,
11478 /*non_integral_constant_expression_p=*/NULL,
11479 /*template_p=*/false,
11480 /*done=*/true,
11481 /*address_p=*/false,
11482 /*template_arg_p=*/false,
11483 &error_msg,
11484 capture_token->location);
11486 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11488 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11489 cp_lexer_consume_token (parser->lexer);
11490 capture_init_expr = make_pack_expansion (capture_init_expr);
11491 if (init_pack_expansion)
11493 /* If what follows is an initializer, the second '...' is
11494 invalid. But for cases like [...xs...], the first one
11495 is invalid. */
11496 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
11497 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
11498 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11499 ellipsis_loc = loc;
11500 error_at (ellipsis_loc, "too many %<...%> in lambda capture");
11501 continue;
11506 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
11507 && !explicit_init_p)
11509 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
11510 && capture_kind == BY_COPY)
11511 pedwarn (capture_token->location, 0, "explicit by-copy capture "
11512 "of %qD redundant with by-copy capture default",
11513 capture_id);
11514 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
11515 && capture_kind == BY_REFERENCE)
11516 pedwarn (capture_token->location, 0, "explicit by-reference "
11517 "capture of %qD redundant with by-reference capture "
11518 "default", capture_id);
11521 /* Check for duplicates.
11522 Optimize for the zero or one explicit captures cases and only create
11523 the hash_set after adding second capture. */
11524 bool found = false;
11525 if (!ids.is_empty ())
11526 found = ids.add (capture_id);
11527 else if (first_capture_id == NULL_TREE)
11528 first_capture_id = capture_id;
11529 else if (capture_id == first_capture_id)
11530 found = true;
11531 else
11533 ids.add (first_capture_id);
11534 ids.add (capture_id);
11536 if (found)
11537 pedwarn (input_location, 0,
11538 "already captured %qD in lambda expression", capture_id);
11539 else
11540 add_capture (lambda_expr, capture_id, capture_init_expr,
11541 /*by_reference_p=*/capture_kind == BY_REFERENCE,
11542 explicit_init_p);
11544 /* If there is any qualification still in effect, clear it
11545 now; we will be starting fresh with the next capture. */
11546 parser->scope = NULL_TREE;
11547 parser->qualifying_scope = NULL_TREE;
11548 parser->object_scope = NULL_TREE;
11551 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11554 /* Parse the (optional) middle of a lambda expression.
11556 lambda-declarator:
11557 ( parameter-declaration-clause ) lambda-specifiers requires-clause [opt]
11558 lambda-specifiers (C++23)
11560 lambda-specifiers:
11561 decl-specifier-seq [opt] noexcept-specifier [opt]
11562 attribute-specifier-seq [opt] trailing-return-type [opt]
11564 LAMBDA_EXPR is the current representation of the lambda expression. */
11566 static bool
11567 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
11569 /* 5.1.1.4 of the standard says:
11570 If a lambda-expression does not include a lambda-declarator, it is as if
11571 the lambda-declarator were ().
11572 This means an empty parameter list, no attributes, and no exception
11573 specification. */
11574 tree param_list = void_list_node;
11575 tree std_attrs = NULL_TREE;
11576 tree gnu_attrs = NULL_TREE;
11577 tree exception_spec = NULL_TREE;
11578 tree template_param_list = NULL_TREE;
11579 tree tx_qual = NULL_TREE;
11580 tree return_type = NULL_TREE;
11581 tree trailing_requires_clause = NULL_TREE;
11582 bool has_param_list = false;
11583 location_t omitted_parms_loc = UNKNOWN_LOCATION;
11584 cp_decl_specifier_seq lambda_specs;
11585 clear_decl_specs (&lambda_specs);
11586 /* A lambda op() is const unless explicitly 'mutable'. */
11587 cp_cv_quals quals = TYPE_QUAL_CONST;
11589 /* The template-parameter-list is optional, but must begin with
11590 an opening angle if present. */
11591 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
11593 if (cxx_dialect < cxx14)
11594 pedwarn (parser->lexer->next_token->location, OPT_Wc__14_extensions,
11595 "lambda templates are only available with "
11596 "%<-std=c++14%> or %<-std=gnu++14%>");
11597 else if (pedantic && cxx_dialect < cxx20)
11598 pedwarn (parser->lexer->next_token->location, OPT_Wc__20_extensions,
11599 "lambda templates are only available with "
11600 "%<-std=c++20%> or %<-std=gnu++20%>");
11602 cp_lexer_consume_token (parser->lexer);
11604 template_param_list = cp_parser_template_parameter_list (parser);
11605 cp_parser_require_end_of_template_parameter_list (parser);
11607 /* We may have a constrained generic lambda; parse the requires-clause
11608 immediately after the template-parameter-list and combine with any
11609 shorthand constraints present. */
11610 tree dreqs = cp_parser_requires_clause_opt (parser, true);
11611 if (flag_concepts)
11613 tree reqs = get_shorthand_constraints (current_template_parms);
11614 if (dreqs)
11615 reqs = combine_constraint_expressions (reqs, dreqs);
11616 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
11619 /* We just processed one more parameter list. */
11620 ++parser->num_template_parameter_lists;
11623 /* Committee discussion supports allowing attributes here. */
11624 lambda_specs.attributes = cp_parser_attributes_opt (parser);
11626 /* The parameter-declaration-clause is optional (unless
11627 template-parameter-list was given), but must begin with an
11628 opening parenthesis if present. */
11629 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
11631 matching_parens parens;
11632 parens.consume_open (parser);
11634 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
11636 /* Parse parameters. */
11637 param_list
11638 = cp_parser_parameter_declaration_clause
11639 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL);
11641 /* Default arguments shall not be specified in the
11642 parameter-declaration-clause of a lambda-declarator. */
11643 if (pedantic && cxx_dialect < cxx14)
11644 for (tree t = param_list; t; t = TREE_CHAIN (t))
11645 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
11646 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
11647 OPT_Wc__14_extensions,
11648 "default argument specified for lambda parameter");
11650 parens.require_close (parser);
11651 has_param_list = true;
11653 else if (cxx_dialect < cxx23)
11654 omitted_parms_loc = cp_lexer_peek_token (parser->lexer)->location;
11656 /* In the decl-specifier-seq of the lambda-declarator, each
11657 decl-specifier shall either be mutable or constexpr. */
11658 int declares_class_or_enum;
11659 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
11660 cp_parser_decl_specifier_seq (parser,
11661 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
11662 &lambda_specs, &declares_class_or_enum);
11664 if (omitted_parms_loc && lambda_specs.any_specifiers_p)
11666 pedwarn (omitted_parms_loc, OPT_Wc__23_extensions,
11667 "parameter declaration before lambda declaration "
11668 "specifiers only optional with %<-std=c++2b%> or "
11669 "%<-std=gnu++2b%>");
11670 omitted_parms_loc = UNKNOWN_LOCATION;
11673 if (lambda_specs.storage_class == sc_mutable)
11675 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
11676 quals = TYPE_UNQUALIFIED;
11678 else if (lambda_specs.storage_class == sc_static)
11680 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
11681 || LAMBDA_EXPR_CAPTURE_LIST (lambda_expr))
11682 error_at (lambda_specs.locations[ds_storage_class],
11683 "%<static%> lambda specifier with lambda capture");
11684 else
11686 LAMBDA_EXPR_STATIC_P (lambda_expr) = 1;
11687 quals = TYPE_UNQUALIFIED;
11691 tx_qual = cp_parser_tx_qualifier_opt (parser);
11692 if (omitted_parms_loc && tx_qual)
11694 pedwarn (omitted_parms_loc, OPT_Wc__23_extensions,
11695 "parameter declaration before lambda transaction "
11696 "qualifier only optional with %<-std=c++2b%> or "
11697 "%<-std=gnu++2b%>");
11698 omitted_parms_loc = UNKNOWN_LOCATION;
11701 /* Parse optional exception specification. */
11702 exception_spec
11703 = cp_parser_exception_specification_opt (parser, CP_PARSER_FLAGS_NONE);
11705 if (omitted_parms_loc && exception_spec)
11707 pedwarn (omitted_parms_loc, OPT_Wc__23_extensions,
11708 "parameter declaration before lambda exception "
11709 "specification only optional with %<-std=c++2b%> or "
11710 "%<-std=gnu++2b%>");
11711 omitted_parms_loc = UNKNOWN_LOCATION;
11714 /* GCC 8 accepted attributes here, and this is the place for standard C++11
11715 attributes that appertain to the function type. */
11716 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
11717 gnu_attrs = cp_parser_gnu_attributes_opt (parser);
11718 else
11719 std_attrs = cp_parser_std_attribute_spec_seq (parser);
11721 /* Parse optional trailing return type. */
11722 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
11724 if (omitted_parms_loc)
11725 pedwarn (omitted_parms_loc, OPT_Wc__23_extensions,
11726 "parameter declaration before lambda trailing "
11727 "return type only optional with %<-std=c++2b%> or "
11728 "%<-std=gnu++2b%>");
11729 cp_lexer_consume_token (parser->lexer);
11730 return_type = cp_parser_trailing_type_id (parser);
11733 /* Also allow GNU attributes at the very end of the declaration, the usual
11734 place for GNU attributes. */
11735 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
11736 gnu_attrs = chainon (gnu_attrs, cp_parser_gnu_attributes_opt (parser));
11738 if (has_param_list)
11740 /* Parse optional trailing requires clause. */
11741 trailing_requires_clause = cp_parser_requires_clause_opt (parser, false);
11743 /* The function parameters must be in scope all the way until after the
11744 trailing-return-type in case of decltype. */
11745 pop_bindings_and_leave_scope ();
11748 /* Create the function call operator.
11750 Messing with declarators like this is no uglier than building up the
11751 FUNCTION_DECL by hand, and this is less likely to get out of sync with
11752 other code. */
11754 cp_decl_specifier_seq return_type_specs;
11755 cp_declarator* declarator;
11756 tree fco;
11757 void *p;
11759 clear_decl_specs (&return_type_specs);
11760 return_type_specs.type = make_auto ();
11762 if (lambda_specs.locations[ds_constexpr])
11764 if (cxx_dialect >= cxx17)
11765 return_type_specs.locations[ds_constexpr]
11766 = lambda_specs.locations[ds_constexpr];
11767 else
11768 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
11769 "lambda only available with %<-std=c++17%> or "
11770 "%<-std=gnu++17%>");
11772 if (lambda_specs.locations[ds_consteval])
11773 return_type_specs.locations[ds_consteval]
11774 = lambda_specs.locations[ds_consteval];
11775 if (LAMBDA_EXPR_STATIC_P (lambda_expr))
11777 return_type_specs.storage_class = sc_static;
11778 return_type_specs.locations[ds_storage_class]
11779 = lambda_specs.locations[ds_storage_class];
11782 p = obstack_alloc (&declarator_obstack, 0);
11784 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none,
11785 LAMBDA_EXPR_LOCATION (lambda_expr));
11787 declarator = make_call_declarator (declarator, param_list, quals,
11788 VIRT_SPEC_UNSPECIFIED,
11789 REF_QUAL_NONE,
11790 tx_qual,
11791 exception_spec,
11792 return_type,
11793 trailing_requires_clause,
11794 std_attrs,
11795 UNKNOWN_LOCATION);
11797 fco = grokmethod (&return_type_specs,
11798 declarator,
11799 chainon (gnu_attrs, lambda_specs.attributes));
11800 if (fco != error_mark_node)
11802 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
11803 DECL_ARTIFICIAL (fco) = 1;
11804 if (!LAMBDA_EXPR_STATIC_P (lambda_expr))
11805 /* Give the object parameter a different name. */
11806 DECL_NAME (DECL_ARGUMENTS (fco)) = closure_identifier;
11807 DECL_SET_LAMBDA_FUNCTION (fco, true);
11809 if (template_param_list)
11811 fco = finish_member_template_decl (fco);
11812 finish_template_decl (template_param_list);
11813 --parser->num_template_parameter_lists;
11815 else if (parser->fully_implicit_function_template_p)
11816 fco = finish_fully_implicit_template (parser, fco);
11818 finish_member_declaration (fco);
11819 record_lambda_scope_sig_discriminator (lambda_expr, fco);
11821 obstack_free (&declarator_obstack, p);
11823 return (fco != error_mark_node);
11827 /* Parse the body of a lambda expression, which is simply
11829 compound-statement
11831 but which requires special handling.
11832 LAMBDA_EXPR is the current representation of the lambda expression. */
11834 static void
11835 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
11837 bool nested = (current_function_decl != NULL_TREE);
11838 unsigned char local_variables_forbidden_p
11839 = parser->local_variables_forbidden_p;
11840 bool in_function_body = parser->in_function_body;
11842 /* The body of a lambda-expression is not a subexpression of the enclosing
11843 expression. */
11844 cp_evaluated ev;
11846 if (nested)
11847 push_function_context ();
11848 else
11849 /* Still increment function_depth so that we don't GC in the
11850 middle of an expression. */
11851 ++function_depth;
11853 auto odsd = make_temp_override (parser->omp_declare_simd, NULL);
11854 auto ord = make_temp_override (parser->oacc_routine, NULL);
11855 auto oafp = make_temp_override (parser->omp_attrs_forbidden_p, false);
11856 vec<tree> omp_privatization_save;
11857 save_omp_privatization_clauses (omp_privatization_save);
11858 /* Clear this in case we're in the middle of a default argument. */
11859 parser->local_variables_forbidden_p = 0;
11860 parser->in_function_body = true;
11863 local_specialization_stack s (lss_copy);
11864 tree fco = lambda_function (lambda_expr);
11865 tree body = start_lambda_function (fco, lambda_expr);
11867 /* Originally C++11 required us to peek for 'return expr'; and
11868 process it specially here to deduce the return type. N3638
11869 removed the need for that. */
11870 cp_parser_function_body (parser, false);
11872 finish_lambda_function (body);
11875 restore_omp_privatization_clauses (omp_privatization_save);
11876 parser->local_variables_forbidden_p = local_variables_forbidden_p;
11877 parser->in_function_body = in_function_body;
11878 if (nested)
11879 pop_function_context();
11880 else
11881 --function_depth;
11884 /* Statements [gram.stmt.stmt] */
11886 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
11888 static void
11889 add_debug_begin_stmt (location_t loc)
11891 if (!MAY_HAVE_DEBUG_MARKER_STMTS)
11892 return;
11893 if (DECL_DECLARED_CONCEPT_P (current_function_decl))
11894 /* A concept is never expanded normally. */
11895 return;
11897 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
11898 SET_EXPR_LOCATION (stmt, loc);
11899 add_stmt (stmt);
11902 struct cp_omp_attribute_data
11904 cp_token_cache *tokens;
11905 const c_omp_directive *dir;
11906 c_omp_directive_kind kind;
11909 /* Handle omp::directive and omp::sequence attributes in ATTRS
11910 (if any) at the start of a statement or in attribute-declaration. */
11912 static tree
11913 cp_parser_handle_statement_omp_attributes (cp_parser *parser, tree attrs)
11915 if (!flag_openmp && !flag_openmp_simd)
11916 return attrs;
11918 auto_vec<cp_omp_attribute_data, 16> vec;
11919 int cnt = 0;
11920 int tokens = 0;
11921 bool bad = false;
11922 for (tree *pa = &attrs; *pa; )
11923 if (get_attribute_namespace (*pa) == omp_identifier
11924 && is_attribute_p ("directive", get_attribute_name (*pa)))
11926 cnt++;
11927 for (tree a = TREE_VALUE (*pa); a; a = TREE_CHAIN (a))
11929 tree d = TREE_VALUE (a);
11930 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
11931 cp_token *first = DEFPARSE_TOKENS (d)->first;
11932 cp_token *last = DEFPARSE_TOKENS (d)->last;
11933 if (parser->omp_attrs_forbidden_p)
11935 error_at (first->location,
11936 "mixing OpenMP directives with attribute and pragma "
11937 "syntax on the same statement");
11938 parser->omp_attrs_forbidden_p = false;
11939 bad = true;
11941 const char *directive[3] = {};
11942 for (int i = 0; i < 3; i++)
11944 tree id = NULL_TREE;
11945 if (first + i == last)
11946 break;
11947 if (first[i].type == CPP_NAME)
11948 id = first[i].u.value;
11949 else if (first[i].type == CPP_KEYWORD)
11950 id = ridpointers[(int) first[i].keyword];
11951 else
11952 break;
11953 directive[i] = IDENTIFIER_POINTER (id);
11955 const c_omp_directive *dir = NULL;
11956 if (directive[0])
11957 dir = c_omp_categorize_directive (directive[0], directive[1],
11958 directive[2]);
11959 if (dir == NULL)
11961 error_at (first->location,
11962 "unknown OpenMP directive name in %<omp::directive%>"
11963 " attribute argument");
11964 continue;
11966 c_omp_directive_kind kind = dir->kind;
11967 if (dir->id == PRAGMA_OMP_ORDERED)
11969 /* ordered is C_OMP_DIR_CONSTRUCT only if it doesn't contain
11970 depend/doacross clause. */
11971 if (directive[1]
11972 && (strcmp (directive[1], "depend") == 0
11973 || strcmp (directive[1], "doacross") == 0))
11974 kind = C_OMP_DIR_STANDALONE;
11975 else if (first + 2 < last
11976 && first[1].type == CPP_COMMA
11977 && first[2].type == CPP_NAME
11978 && (strcmp (IDENTIFIER_POINTER (first[2].u.value),
11979 "depend") == 0
11980 || strcmp (IDENTIFIER_POINTER (first[2].u.value),
11981 "doacross") == 0))
11982 kind = C_OMP_DIR_STANDALONE;
11984 else if (dir->id == PRAGMA_OMP_ERROR)
11986 /* error with at(execution) clause is C_OMP_DIR_STANDALONE. */
11987 int paren_depth = 0;
11988 for (int i = 1; first + i < last; i++)
11989 if (first[i].type == CPP_OPEN_PAREN)
11990 paren_depth++;
11991 else if (first[i].type == CPP_CLOSE_PAREN)
11992 paren_depth--;
11993 else if (paren_depth == 0
11994 && first + i + 2 < last
11995 && first[i].type == CPP_NAME
11996 && first[i + 1].type == CPP_OPEN_PAREN
11997 && first[i + 2].type == CPP_NAME
11998 && !strcmp (IDENTIFIER_POINTER (first[i].u.value),
11999 "at")
12000 && !strcmp (IDENTIFIER_POINTER (first[i
12001 + 2].u.value),
12002 "execution"))
12004 kind = C_OMP_DIR_STANDALONE;
12005 break;
12008 cp_omp_attribute_data v = { DEFPARSE_TOKENS (d), dir, kind };
12009 vec.safe_push (v);
12010 if (flag_openmp || dir->simd)
12011 tokens += (last - first) + 1;
12013 cp_omp_attribute_data v = {};
12014 vec.safe_push (v);
12015 *pa = TREE_CHAIN (*pa);
12017 else
12018 pa = &TREE_CHAIN (*pa);
12020 if (bad)
12021 return attrs;
12023 unsigned int i;
12024 cp_omp_attribute_data *v;
12025 cp_omp_attribute_data *construct_seen = nullptr;
12026 cp_omp_attribute_data *standalone_seen = nullptr;
12027 cp_omp_attribute_data *prev_standalone_seen = nullptr;
12028 FOR_EACH_VEC_ELT (vec, i, v)
12029 if (v->tokens)
12031 if (v->kind == C_OMP_DIR_CONSTRUCT && !construct_seen)
12032 construct_seen = v;
12033 else if (v->kind == C_OMP_DIR_STANDALONE && !standalone_seen)
12034 standalone_seen = v;
12036 else
12038 if (standalone_seen && !prev_standalone_seen)
12040 prev_standalone_seen = standalone_seen;
12041 standalone_seen = nullptr;
12045 if (cnt > 1 && construct_seen)
12047 error_at (construct_seen->tokens->first->location,
12048 "OpenMP construct among %<omp::directive%> attributes"
12049 " requires all %<omp::directive%> attributes on the"
12050 " same statement to be in the same %<omp::sequence%>");
12051 return attrs;
12053 if (cnt > 1 && standalone_seen && prev_standalone_seen)
12055 error_at (standalone_seen->tokens->first->location,
12056 "multiple OpenMP standalone directives among"
12057 " %<omp::directive%> attributes must be all within the"
12058 " same %<omp::sequence%>");
12059 return attrs;
12062 if (prev_standalone_seen)
12063 standalone_seen = prev_standalone_seen;
12064 if (standalone_seen
12065 && !cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12067 error_at (standalone_seen->tokens->first->location,
12068 "standalone OpenMP directives in %<omp::directive%> attribute"
12069 " can only appear on an empty statement");
12070 return attrs;
12072 if (cnt && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
12074 cp_token *token = cp_lexer_peek_token (parser->lexer);
12075 enum pragma_kind kind = cp_parser_pragma_kind (token);
12076 if (kind >= PRAGMA_OMP__START_ && kind <= PRAGMA_OMP__LAST_)
12078 error_at (token->location,
12079 "mixing OpenMP directives with attribute and pragma "
12080 "syntax on the same statement");
12081 return attrs;
12085 if (!tokens)
12086 return attrs;
12087 tokens++;
12088 cp_lexer *lexer = cp_lexer_alloc ();
12089 lexer->debugging_p = parser->lexer->debugging_p;
12090 vec_safe_reserve (lexer->buffer, tokens, true);
12091 FOR_EACH_VEC_ELT (vec, i, v)
12093 if (!v->tokens)
12094 continue;
12095 if (!flag_openmp && !v->dir->simd)
12096 continue;
12097 cp_token *first = v->tokens->first;
12098 cp_token *last = v->tokens->last;
12099 cp_token tok = {};
12100 tok.type = CPP_PRAGMA;
12101 tok.keyword = RID_MAX;
12102 tok.u.value = build_int_cst (NULL, v->dir->id);
12103 tok.location = first->location;
12104 lexer->buffer->quick_push (tok);
12105 while (++first < last)
12106 lexer->buffer->quick_push (*first);
12107 tok = {};
12108 tok.type = CPP_PRAGMA_EOL;
12109 tok.keyword = RID_MAX;
12110 tok.location = last->location;
12111 lexer->buffer->quick_push (tok);
12113 cp_token tok = {};
12114 tok.type = CPP_EOF;
12115 tok.keyword = RID_MAX;
12116 tok.location = lexer->buffer->last ().location;
12117 lexer->buffer->quick_push (tok);
12118 lexer->next = parser->lexer;
12119 lexer->next_token = lexer->buffer->address ();
12120 lexer->last_token = lexer->next_token
12121 + lexer->buffer->length ()
12122 - 1;
12123 lexer->in_omp_attribute_pragma = true;
12124 parser->lexer = lexer;
12125 /* Move the current source position to that of the first token in the
12126 new lexer. */
12127 cp_lexer_set_source_position_from_token (lexer->next_token);
12128 return attrs;
12131 /* True if and only if the name is one of the contract types. */
12133 static bool
12134 contract_attribute_p (const_tree id)
12136 return is_attribute_p ("assert", id)
12137 || is_attribute_p ("pre", id)
12138 || is_attribute_p ("post", id);
12141 /* Handle omp::directive and omp::sequence attributes in *PATTRS
12142 (if any) at the start or after declaration-id of a declaration. */
12144 static void
12145 cp_parser_handle_directive_omp_attributes (cp_parser *parser, tree *pattrs,
12146 cp_omp_declare_simd_data *data,
12147 bool start)
12149 if (!flag_openmp && !flag_openmp_simd)
12150 return;
12152 int cnt = 0;
12153 bool bad = false;
12154 bool variant_p = false;
12155 location_t loc = UNKNOWN_LOCATION;
12156 for (tree pa = *pattrs; pa; pa = TREE_CHAIN (pa))
12157 if (get_attribute_namespace (pa) == omp_identifier
12158 && is_attribute_p ("directive", get_attribute_name (pa)))
12160 for (tree a = TREE_VALUE (pa); a; a = TREE_CHAIN (a))
12162 tree d = TREE_VALUE (a);
12163 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
12164 cp_token *first = DEFPARSE_TOKENS (d)->first;
12165 cp_token *last = DEFPARSE_TOKENS (d)->last;
12166 const char *directive[3] = {};
12167 for (int i = 0; i < 3; i++)
12169 tree id = NULL_TREE;
12170 if (first + i == last)
12171 break;
12172 if (first[i].type == CPP_NAME)
12173 id = first[i].u.value;
12174 else if (first[i].type == CPP_KEYWORD)
12175 id = ridpointers[(int) first[i].keyword];
12176 else
12177 break;
12178 directive[i] = IDENTIFIER_POINTER (id);
12180 const c_omp_directive *dir = NULL;
12181 if (directive[0])
12182 dir = c_omp_categorize_directive (directive[0], directive[1],
12183 directive[2]);
12184 if (dir == NULL)
12185 continue;
12186 if (dir->id == PRAGMA_OMP_DECLARE
12187 && (strcmp (directive[1], "simd") == 0
12188 || strcmp (directive[1], "variant") == 0))
12190 if (cnt++ == 0)
12192 variant_p = strcmp (directive[1], "variant") == 0;
12193 loc = first->location;
12195 if (start && parser->omp_declare_simd && !bad)
12197 error_at (first->location,
12198 "mixing OpenMP directives with attribute and "
12199 "pragma syntax on the same declaration");
12200 bad = true;
12206 if (bad)
12208 for (tree *pa = pattrs; *pa; )
12209 if (get_attribute_namespace (*pa) == omp_identifier
12210 && is_attribute_p ("directive", get_attribute_name (*pa)))
12211 *pa = TREE_CHAIN (*pa);
12212 else
12213 pa = &TREE_CHAIN (*pa);
12214 return;
12216 if (cnt == 0)
12217 return;
12219 if (parser->omp_declare_simd == NULL)
12221 data->error_seen = false;
12222 data->fndecl_seen = false;
12223 data->variant_p = variant_p;
12224 data->loc = loc;
12225 data->tokens = vNULL;
12226 data->attribs[0] = NULL;
12227 data->attribs[1] = NULL;
12228 parser->omp_declare_simd = data;
12230 parser->omp_declare_simd->attribs[!start] = pattrs;
12233 /* Parse a statement.
12235 statement:
12236 labeled-statement
12237 expression-statement
12238 compound-statement
12239 selection-statement
12240 iteration-statement
12241 jump-statement
12242 declaration-statement
12243 try-block
12245 C++11:
12247 statement:
12248 labeled-statement
12249 attribute-specifier-seq (opt) expression-statement
12250 attribute-specifier-seq (opt) compound-statement
12251 attribute-specifier-seq (opt) selection-statement
12252 attribute-specifier-seq (opt) iteration-statement
12253 attribute-specifier-seq (opt) jump-statement
12254 declaration-statement
12255 attribute-specifier-seq (opt) try-block
12257 init-statement:
12258 expression-statement
12259 simple-declaration
12260 alias-declaration
12262 TM Extension:
12264 statement:
12265 atomic-statement
12267 IN_COMPOUND is true when the statement is nested inside a
12268 cp_parser_compound_statement.
12270 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12271 is a (possibly labeled) if statement which is not enclosed in braces
12272 and has an else clause. This is used to implement -Wparentheses.
12274 CHAIN is a vector of if-else-if conditions.
12276 Note that this version of parsing restricts assertions to be attached to
12277 empty statements. */
12279 static void
12280 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
12281 const bool in_compound, bool *if_p, vec<tree> *chain,
12282 location_t *loc_after_labels)
12284 tree statement, std_attrs = NULL_TREE;
12285 cp_token *token;
12286 location_t statement_location, attrs_loc;
12287 bool in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
12288 bool has_std_attrs;
12289 /* A copy of IN_COMPOUND which is set to false after seeing a label.
12290 This matters for certain pragmas. */
12291 bool in_compound_for_pragma = in_compound;
12293 restart:
12294 if (if_p != NULL)
12295 *if_p = false;
12296 /* There is no statement yet. */
12297 statement = NULL_TREE;
12299 saved_token_sentinel saved_tokens (parser->lexer);
12300 token = cp_lexer_peek_token (parser->lexer);
12301 attrs_loc = token->location;
12302 if (c_dialect_objc ())
12303 /* In obj-c++, seeing '[[' might be the either the beginning of
12304 c++11 attributes, or a nested objc-message-expression. So
12305 let's parse the c++11 attributes tentatively. */
12306 cp_parser_parse_tentatively (parser);
12307 std_attrs = cp_parser_std_attribute_spec_seq (parser);
12308 if (std_attrs)
12309 attrs_loc = make_location (attrs_loc, attrs_loc, parser->lexer);
12310 if (c_dialect_objc ())
12312 if (!cp_parser_parse_definitely (parser))
12313 std_attrs = NULL_TREE;
12315 has_std_attrs = cp_lexer_peek_token (parser->lexer) != token;
12317 /* Peek at the next token. */
12318 token = cp_lexer_peek_token (parser->lexer);
12320 /* If we have contracts, check that they're valid in this context. */
12321 if (std_attrs != error_mark_node)
12323 if (tree pre = lookup_attribute ("pre", std_attrs))
12324 error_at (EXPR_LOCATION (TREE_VALUE (pre)),
12325 "preconditions cannot be statements");
12326 else if (tree post = lookup_attribute ("post", std_attrs))
12327 error_at (EXPR_LOCATION (TREE_VALUE (post)),
12328 "postconditions cannot be statements");
12330 /* Check that assertions are null statements. */
12331 if (cp_contract_assertion_p (std_attrs))
12332 if (token->type != CPP_SEMICOLON)
12333 error_at (token->location, "assertions must be followed by %<;%>");
12336 bool omp_attrs_forbidden_p;
12337 omp_attrs_forbidden_p = parser->omp_attrs_forbidden_p;
12339 if (std_attrs && (flag_openmp || flag_openmp_simd))
12341 bool handle_omp_attribs = false;
12342 if (token->type == CPP_KEYWORD)
12343 switch (token->keyword)
12345 case RID_IF:
12346 case RID_SWITCH:
12347 case RID_WHILE:
12348 case RID_DO:
12349 case RID_FOR:
12350 case RID_BREAK:
12351 case RID_CONTINUE:
12352 case RID_RETURN:
12353 case RID_CO_RETURN:
12354 case RID_GOTO:
12355 case RID_AT_TRY:
12356 case RID_AT_CATCH:
12357 case RID_AT_FINALLY:
12358 case RID_AT_SYNCHRONIZED:
12359 case RID_AT_THROW:
12360 case RID_TRY:
12361 case RID_TRANSACTION_ATOMIC:
12362 case RID_TRANSACTION_RELAXED:
12363 case RID_SYNCHRONIZED:
12364 case RID_ATOMIC_NOEXCEPT:
12365 case RID_ATOMIC_CANCEL:
12366 case RID_TRANSACTION_CANCEL:
12367 handle_omp_attribs = true;
12368 break;
12369 default:
12370 break;
12372 else if (token->type == CPP_SEMICOLON
12373 || token->type == CPP_OPEN_BRACE
12374 || token->type == CPP_PRAGMA)
12375 handle_omp_attribs = true;
12376 if (handle_omp_attribs)
12378 std_attrs = cp_parser_handle_statement_omp_attributes (parser,
12379 std_attrs);
12380 token = cp_lexer_peek_token (parser->lexer);
12383 parser->omp_attrs_forbidden_p = false;
12385 /* Remember the location of the first token in the statement. */
12386 cp_token *statement_token = token;
12387 statement_location = token->location;
12388 add_debug_begin_stmt (statement_location);
12389 /* If this is a keyword, then that will often determine what kind of
12390 statement we have. */
12391 if (token->type == CPP_KEYWORD)
12393 enum rid keyword = token->keyword;
12395 switch (keyword)
12397 case RID_CASE:
12398 case RID_DEFAULT:
12399 /* Looks like a labeled-statement with a case label.
12400 Parse the label, and then use tail recursion to parse
12401 the statement. */
12402 cp_parser_label_for_labeled_statement (parser, std_attrs);
12403 in_compound_for_pragma = false;
12404 in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
12405 goto restart;
12407 case RID_IF:
12408 case RID_SWITCH:
12409 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12410 statement = cp_parser_selection_statement (parser, if_p, chain);
12411 break;
12413 case RID_WHILE:
12414 case RID_DO:
12415 case RID_FOR:
12416 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12417 statement = cp_parser_iteration_statement (parser, if_p, false, 0);
12418 break;
12420 case RID_BREAK:
12421 case RID_CONTINUE:
12422 case RID_RETURN:
12423 case RID_CO_RETURN:
12424 case RID_GOTO:
12425 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12426 statement = cp_parser_jump_statement (parser);
12427 break;
12429 /* Objective-C++ exception-handling constructs. */
12430 case RID_AT_TRY:
12431 case RID_AT_CATCH:
12432 case RID_AT_FINALLY:
12433 case RID_AT_SYNCHRONIZED:
12434 case RID_AT_THROW:
12435 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12436 statement = cp_parser_objc_statement (parser);
12437 break;
12439 case RID_TRY:
12440 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12441 statement = cp_parser_try_block (parser);
12442 break;
12444 case RID_NAMESPACE:
12445 /* This must be a namespace alias definition. */
12446 if (has_std_attrs)
12448 /* Attributes should be parsed as part of the
12449 declaration, so let's un-parse them. */
12450 saved_tokens.rollback();
12451 std_attrs = NULL_TREE;
12453 cp_parser_declaration_statement (parser);
12454 return;
12456 case RID_TRANSACTION_ATOMIC:
12457 case RID_TRANSACTION_RELAXED:
12458 case RID_SYNCHRONIZED:
12459 case RID_ATOMIC_NOEXCEPT:
12460 case RID_ATOMIC_CANCEL:
12461 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12462 statement = cp_parser_transaction (parser, token);
12463 break;
12464 case RID_TRANSACTION_CANCEL:
12465 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12466 statement = cp_parser_transaction_cancel (parser);
12467 break;
12469 default:
12470 /* It might be a keyword like `int' that can start a
12471 declaration-statement. */
12472 break;
12475 else if (token->type == CPP_NAME)
12477 /* If the next token is a `:', then we are looking at a
12478 labeled-statement. */
12479 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12480 if (token->type == CPP_COLON)
12482 /* Looks like a labeled-statement with an ordinary label.
12483 Parse the label, and then use tail recursion to parse
12484 the statement. */
12486 cp_parser_label_for_labeled_statement (parser, std_attrs);
12488 /* If there's no statement, it's not a labeled-statement, just
12489 a label. That's allowed in C++23, but only if we're at the
12490 end of a compound-statement. */
12491 if (in_compound
12492 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12494 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
12495 if (cxx_dialect < cxx23)
12496 pedwarn (loc, OPT_Wc__23_extensions,
12497 "label at end of compound statement only available "
12498 "with %<-std=c++2b%> or %<-std=gnu++2b%>");
12499 return;
12501 in_compound_for_pragma = false;
12502 in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
12503 goto restart;
12506 /* Anything that starts with a `{' must be a compound-statement. */
12507 else if (token->type == CPP_OPEN_BRACE)
12509 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12510 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12512 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
12513 a statement all its own. */
12514 else if (token->type == CPP_PRAGMA)
12516 do_pragma:;
12517 cp_lexer *lexer = parser->lexer;
12518 bool do_restart = false;
12519 /* Only certain OpenMP pragmas are attached to statements, and thus
12520 are considered statements themselves. All others are not. In
12521 the context of a compound, accept the pragma as a "statement" and
12522 return so that we can check for a close brace. Otherwise we
12523 require a real statement and must go back and read one. */
12524 if (in_compound_for_pragma)
12525 cp_parser_pragma (parser, pragma_compound, if_p);
12526 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
12527 do_restart = true;
12528 if (parser->lexer != lexer
12529 && lexer->in_omp_attribute_pragma
12530 && (!in_omp_attribute_pragma || lexer->orphan_p))
12532 if (saved_tokens.lexer == lexer)
12534 if (saved_tokens.mode == STS_COMMIT)
12535 cp_lexer_commit_tokens (lexer);
12536 gcc_assert (lexer->saved_tokens.length () == saved_tokens.len);
12537 saved_tokens.lexer = parser->lexer;
12538 saved_tokens.mode = STS_DONOTHING;
12539 saved_tokens.len = parser->lexer->saved_tokens.length ();
12541 cp_lexer_destroy (lexer);
12542 lexer = parser->lexer;
12544 if (do_restart)
12545 goto restart;
12546 if (parser->lexer == lexer
12547 && lexer->in_omp_attribute_pragma
12548 && !in_omp_attribute_pragma)
12549 parser->lexer->orphan_p = true;
12550 return;
12552 else if (token->type == CPP_EOF)
12554 cp_parser_error (parser, "expected statement");
12555 return;
12558 /* Everything else must be a declaration-statement or an
12559 expression-statement. Try for the declaration-statement
12560 first, unless we are looking at a `;', in which case we know that
12561 we have an expression-statement. */
12562 if (!statement)
12564 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12566 if (has_std_attrs)
12567 /* Attributes should be parsed as part of the declaration,
12568 so let's un-parse them. */
12569 saved_tokens.rollback();
12571 parser->omp_attrs_forbidden_p = omp_attrs_forbidden_p;
12572 cp_parser_parse_tentatively (parser);
12573 /* Try to parse the declaration-statement. */
12574 cp_parser_declaration_statement (parser);
12575 parser->omp_attrs_forbidden_p = false;
12576 /* If that worked, we're done. */
12577 if (cp_parser_parse_definitely (parser))
12578 return;
12579 /* It didn't work, restore the post-attribute position. */
12580 if (has_std_attrs)
12582 cp_lexer_set_token_position (parser->lexer, statement_token);
12583 if (flag_openmp || flag_openmp_simd)
12585 size_t i = 1;
12586 bool handle_omp_attribs = true;
12587 while (cp_lexer_peek_nth_token (parser->lexer, i)->keyword
12588 == RID_EXTENSION)
12589 i++;
12590 switch (cp_lexer_peek_nth_token (parser->lexer, i)->keyword)
12592 case RID_ASM:
12593 case RID_NAMESPACE:
12594 case RID_USING:
12595 case RID_LABEL:
12596 case RID_STATIC_ASSERT:
12597 /* Don't handle OpenMP attribs on keywords that
12598 always start a declaration statement but don't
12599 accept attribute before it and therefore
12600 the tentative cp_parser_declaration_statement
12601 fails to parse because of that. */
12602 handle_omp_attribs = false;
12603 break;
12604 default:
12605 break;
12608 if (handle_omp_attribs)
12610 parser->omp_attrs_forbidden_p = omp_attrs_forbidden_p;
12611 std_attrs
12612 = cp_parser_handle_statement_omp_attributes
12613 (parser, std_attrs);
12614 parser->omp_attrs_forbidden_p = false;
12615 token = cp_lexer_peek_token (parser->lexer);
12616 if (token->type == CPP_PRAGMA)
12617 goto do_pragma;
12622 /* All preceding labels have been parsed at this point. */
12623 if (loc_after_labels != NULL)
12624 *loc_after_labels = statement_location;
12626 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12628 /* Look for an expression-statement instead. */
12629 statement = cp_parser_expression_statement (parser, in_statement_expr);
12631 std_attrs = process_stmt_assume_attribute (std_attrs, statement,
12632 attrs_loc);
12634 /* Handle [[fallthrough]];. */
12635 if (attribute_fallthrough_p (std_attrs))
12637 /* The next token after the fallthrough attribute is ';'. */
12638 if (statement == NULL_TREE)
12640 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
12641 statement = build_call_expr_internal_loc (statement_location,
12642 IFN_FALLTHROUGH,
12643 void_type_node, 0);
12644 finish_expr_stmt (statement);
12646 else
12647 warning_at (statement_location, OPT_Wattributes,
12648 "%<fallthrough%> attribute not followed by %<;%>");
12649 std_attrs = NULL_TREE;
12652 /* Handle [[assert: ...]]; */
12653 if (cp_contract_assertion_p (std_attrs))
12655 /* Add the assertion as a statement in the current block. */
12656 gcc_assert (!statement || statement == error_mark_node);
12657 emit_assertion (std_attrs);
12658 std_attrs = NULL_TREE;
12662 /* Set the line number for the statement. */
12663 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
12664 SET_EXPR_LOCATION (statement, statement_location);
12666 /* Allow "[[fallthrough]];" or "[[assume(cond)]];", but warn otherwise. */
12667 if (std_attrs != NULL_TREE)
12668 warning_at (attrs_loc,
12669 OPT_Wattributes,
12670 "attributes at the beginning of statement are ignored");
12673 /* Append ATTR to attribute list ATTRS. */
12675 tree
12676 attr_chainon (tree attrs, tree attr)
12678 if (attrs == error_mark_node)
12679 return error_mark_node;
12680 if (attr == error_mark_node)
12681 return error_mark_node;
12682 return chainon (attrs, attr);
12685 /* Parse the label for a labeled-statement, i.e.
12687 label:
12688 attribute-specifier-seq[opt] identifier :
12689 attribute-specifier-seq[opt] case constant-expression :
12690 attribute-specifier-seq[opt] default :
12692 labeled-statement:
12693 label statement
12695 GNU Extension:
12696 case constant-expression ... constant-expression : statement
12698 When a label is parsed without errors, the label is added to the
12699 parse tree by the finish_* functions, so this function doesn't
12700 have to return the label. */
12702 static void
12703 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
12705 cp_token *token;
12706 tree label = NULL_TREE;
12707 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12709 /* The next token should be an identifier. */
12710 token = cp_lexer_peek_token (parser->lexer);
12711 if (token->type != CPP_NAME
12712 && token->type != CPP_KEYWORD)
12714 cp_parser_error (parser, "expected labeled-statement");
12715 return;
12718 /* Remember whether this case or a user-defined label is allowed to fall
12719 through to. */
12720 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
12722 parser->colon_corrects_to_scope_p = false;
12723 switch (token->keyword)
12725 case RID_CASE:
12727 tree expr, expr_hi;
12728 cp_token *ellipsis;
12730 /* Consume the `case' token. */
12731 cp_lexer_consume_token (parser->lexer);
12732 /* Parse the constant-expression. */
12733 expr = cp_parser_constant_expression (parser);
12734 if (check_for_bare_parameter_packs (expr))
12735 expr = error_mark_node;
12737 ellipsis = cp_lexer_peek_token (parser->lexer);
12738 if (ellipsis->type == CPP_ELLIPSIS)
12740 /* Consume the `...' token. */
12741 cp_lexer_consume_token (parser->lexer);
12742 expr_hi = cp_parser_constant_expression (parser);
12743 if (check_for_bare_parameter_packs (expr_hi))
12744 expr_hi = error_mark_node;
12746 /* We don't need to emit warnings here, as the common code
12747 will do this for us. */
12749 else
12750 expr_hi = NULL_TREE;
12752 if (parser->in_switch_statement_p)
12754 tree l = finish_case_label (token->location, expr, expr_hi);
12755 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
12757 label = CASE_LABEL (l);
12758 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
12761 else
12762 error_at (token->location,
12763 "case label %qE not within a switch statement",
12764 expr);
12766 break;
12768 case RID_DEFAULT:
12769 /* Consume the `default' token. */
12770 cp_lexer_consume_token (parser->lexer);
12772 if (parser->in_switch_statement_p)
12774 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
12775 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
12777 label = CASE_LABEL (l);
12778 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
12781 else
12782 error_at (token->location, "case label not within a switch statement");
12783 break;
12785 default:
12786 /* Anything else must be an ordinary label. */
12787 label = finish_label_stmt (cp_parser_identifier (parser));
12788 if (label && TREE_CODE (label) == LABEL_DECL)
12789 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
12790 break;
12793 /* Require the `:' token. */
12794 cp_parser_require (parser, CPP_COLON, RT_COLON);
12796 /* An ordinary label may optionally be followed by attributes.
12797 However, this is only permitted if the attributes are then
12798 followed by a semicolon. This is because, for backward
12799 compatibility, when parsing
12800 lab: __attribute__ ((unused)) int i;
12801 we want the attribute to attach to "i", not "lab". */
12802 if (label != NULL_TREE
12803 && cp_next_tokens_can_be_gnu_attribute_p (parser))
12805 tree attrs;
12806 cp_parser_parse_tentatively (parser);
12807 attrs = cp_parser_gnu_attributes_opt (parser);
12808 if (attrs == NULL_TREE
12809 /* And fallthrough always binds to the expression-statement. */
12810 || attribute_fallthrough_p (attrs)
12811 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12812 cp_parser_abort_tentative_parse (parser);
12813 else if (!cp_parser_parse_definitely (parser))
12815 else
12816 attributes = attr_chainon (attributes, attrs);
12819 if (attributes != NULL_TREE)
12820 cplus_decl_attributes (&label, attributes, 0);
12822 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12825 /* Parse an expression-statement.
12827 expression-statement:
12828 expression [opt] ;
12830 Returns the new EXPR_STMT -- or NULL_TREE if the expression
12831 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
12832 indicates whether this expression-statement is part of an
12833 expression statement. */
12835 static tree
12836 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
12838 tree statement = NULL_TREE;
12839 cp_token *token = cp_lexer_peek_token (parser->lexer);
12840 location_t loc = token->location;
12842 /* There might be attribute fallthrough. */
12843 tree attr = cp_parser_gnu_attributes_opt (parser);
12845 /* If the next token is a ';', then there is no expression
12846 statement. */
12847 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12849 statement = cp_parser_expression (parser);
12850 if (statement == error_mark_node
12851 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
12853 cp_parser_skip_to_end_of_block_or_statement (parser);
12854 return error_mark_node;
12858 attr = process_stmt_assume_attribute (attr, statement, loc);
12860 /* Handle [[fallthrough]];. */
12861 if (attribute_fallthrough_p (attr))
12863 /* The next token after the fallthrough attribute is ';'. */
12864 if (statement == NULL_TREE)
12865 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
12866 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
12867 void_type_node, 0);
12868 else
12869 warning_at (loc, OPT_Wattributes,
12870 "%<fallthrough%> attribute not followed by %<;%>");
12871 attr = NULL_TREE;
12874 /* Allow "[[fallthrough]];", but warn otherwise. */
12875 if (attr != NULL_TREE)
12876 warning_at (loc, OPT_Wattributes,
12877 "attributes at the beginning of statement are ignored");
12879 /* Give a helpful message for "A<T>::type t;" and the like. */
12880 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
12881 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
12883 if (TREE_CODE (statement) == SCOPE_REF)
12884 error_at (token->location, "need %<typename%> before %qE because "
12885 "%qT is a dependent scope",
12886 statement, TREE_OPERAND (statement, 0));
12887 else if (is_overloaded_fn (statement)
12888 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
12890 /* A::A a; */
12891 tree fn = get_first_fn (statement);
12892 error_at (token->location,
12893 "%<%T::%D%> names the constructor, not the type",
12894 DECL_CONTEXT (fn), DECL_NAME (fn));
12898 /* Consume the final `;'. */
12899 cp_parser_consume_semicolon_at_end_of_statement (parser);
12901 if (in_statement_expr
12902 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12903 /* This is the final expression statement of a statement
12904 expression. */
12905 statement = finish_stmt_expr_expr (statement, in_statement_expr);
12906 else if (statement)
12907 statement = finish_expr_stmt (statement);
12909 return statement;
12912 /* Parse a compound-statement.
12914 compound-statement:
12915 { statement-seq [opt] label-seq [opt] }
12917 label-seq:
12918 label
12919 label-seq label
12921 GNU extension:
12923 compound-statement:
12924 { label-declaration-seq [opt] statement-seq [opt] }
12926 label-declaration-seq:
12927 label-declaration
12928 label-declaration-seq label-declaration
12930 Returns a tree representing the statement. */
12932 static tree
12933 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
12934 int bcs_flags, bool function_body)
12936 tree compound_stmt;
12937 matching_braces braces;
12939 /* Consume the `{'. */
12940 if (!braces.require_open (parser))
12941 return error_mark_node;
12942 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
12943 && !function_body && cxx_dialect < cxx14)
12944 pedwarn (input_location, OPT_Wpedantic,
12945 "compound-statement in %<constexpr%> function");
12946 /* Begin the compound-statement. */
12947 compound_stmt = begin_compound_stmt (bcs_flags);
12948 /* If the next keyword is `__label__' we have a label declaration. */
12949 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12950 cp_parser_label_declaration (parser);
12951 /* Parse an (optional) statement-seq. */
12952 cp_parser_statement_seq_opt (parser, in_statement_expr);
12954 /* Consume the `}'. */
12955 braces.require_close (parser);
12957 /* Finish the compound-statement. */
12958 finish_compound_stmt (compound_stmt);
12960 return compound_stmt;
12963 /* Parse an (optional) statement-seq.
12965 statement-seq:
12966 statement
12967 statement-seq [opt] statement */
12969 static void
12970 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
12972 /* Scan statements until there aren't any more. */
12973 while (true)
12975 cp_token *token = cp_lexer_peek_token (parser->lexer);
12977 /* If we are looking at a `}', then we have run out of
12978 statements; the same is true if we have reached the end
12979 of file, or have stumbled upon a stray '@end'. */
12980 if (token->type == CPP_CLOSE_BRACE
12981 || token->type == CPP_EOF
12982 || token->type == CPP_PRAGMA_EOL
12983 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
12984 break;
12986 /* If we are in a compound statement and find 'else' then
12987 something went wrong. */
12988 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
12990 if (parser->in_statement & IN_IF_STMT)
12991 break;
12992 else
12994 token = cp_lexer_consume_token (parser->lexer);
12995 error_at (token->location, "%<else%> without a previous %<if%>");
12999 /* Parse the statement. */
13000 cp_parser_statement (parser, in_statement_expr, true, NULL);
13004 /* Return true if this is the C++20 version of range-based-for with
13005 init-statement. */
13007 static bool
13008 cp_parser_range_based_for_with_init_p (cp_parser *parser)
13010 bool r = false;
13012 /* Save tokens so that we can put them back. */
13013 cp_lexer_save_tokens (parser->lexer);
13015 /* There has to be an unnested ; followed by an unnested :. */
13016 if (cp_parser_skip_to_closing_parenthesis_1 (parser,
13017 /*recovering=*/false,
13018 CPP_SEMICOLON,
13019 /*consume_paren=*/false) != -1)
13020 goto out;
13022 /* We found the semicolon, eat it now. */
13023 cp_lexer_consume_token (parser->lexer);
13025 /* Now look for ':' that is not nested in () or {}. */
13026 r = (cp_parser_skip_to_closing_parenthesis_1 (parser,
13027 /*recovering=*/false,
13028 CPP_COLON,
13029 /*consume_paren=*/false) == -1);
13031 out:
13032 /* Roll back the tokens we skipped. */
13033 cp_lexer_rollback_tokens (parser->lexer);
13035 return r;
13038 /* Return true if we're looking at (init; cond), false otherwise. */
13040 static bool
13041 cp_parser_init_statement_p (cp_parser *parser)
13043 /* Save tokens so that we can put them back. */
13044 cp_lexer_save_tokens (parser->lexer);
13046 /* Look for ';' that is not nested in () or {}. */
13047 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
13048 /*recovering=*/false,
13049 CPP_SEMICOLON,
13050 /*consume_paren=*/false);
13052 /* Roll back the tokens we skipped. */
13053 cp_lexer_rollback_tokens (parser->lexer);
13055 return ret == -1;
13058 /* Parse a selection-statement.
13060 selection-statement:
13061 if ( init-statement [opt] condition ) statement
13062 if ( init-statement [opt] condition ) statement else statement
13063 switch ( init-statement [opt] condition ) statement
13065 Returns the new IF_STMT or SWITCH_STMT.
13067 If IF_P is not NULL, *IF_P is set to indicate whether the statement
13068 is a (possibly labeled) if statement which is not enclosed in
13069 braces and has an else clause. This is used to implement
13070 -Wparentheses.
13072 CHAIN is a vector of if-else-if conditions. This is used to implement
13073 -Wduplicated-cond. */
13075 static tree
13076 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
13077 vec<tree> *chain)
13079 cp_token *token;
13080 enum rid keyword;
13081 token_indent_info guard_tinfo;
13083 if (if_p != NULL)
13084 *if_p = false;
13086 /* Peek at the next token. */
13087 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
13088 guard_tinfo = get_token_indent_info (token);
13090 /* See what kind of keyword it is. */
13091 keyword = token->keyword;
13092 switch (keyword)
13094 case RID_IF:
13095 case RID_SWITCH:
13097 tree statement;
13098 tree condition;
13100 bool cx = false;
13101 if (keyword == RID_IF
13102 && cp_lexer_next_token_is_keyword (parser->lexer,
13103 RID_CONSTEXPR))
13105 cx = true;
13106 cp_token *tok = cp_lexer_consume_token (parser->lexer);
13107 if (cxx_dialect < cxx17)
13108 pedwarn (tok->location, OPT_Wc__17_extensions,
13109 "%<if constexpr%> only available with "
13110 "%<-std=c++17%> or %<-std=gnu++17%>");
13112 int ce = 0;
13113 if (keyword == RID_IF && !cx)
13115 if (cp_lexer_next_token_is_keyword (parser->lexer,
13116 RID_CONSTEVAL))
13117 ce = 1;
13118 else if (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
13119 && cp_lexer_nth_token_is_keyword (parser->lexer, 2,
13120 RID_CONSTEVAL))
13122 ce = -1;
13123 cp_lexer_consume_token (parser->lexer);
13126 if (ce)
13128 cp_token *tok = cp_lexer_consume_token (parser->lexer);
13129 if (cxx_dialect < cxx23)
13130 pedwarn (tok->location, OPT_Wc__23_extensions,
13131 "%<if consteval%> only available with "
13132 "%<-std=c++2b%> or %<-std=gnu++2b%>");
13134 bool save_in_consteval_if_p = in_consteval_if_p;
13135 statement = begin_if_stmt ();
13136 IF_STMT_CONSTEVAL_P (statement) = true;
13137 condition = finish_if_stmt_cond (boolean_false_node, statement);
13139 gcc_rich_location richloc (tok->location);
13140 bool non_compound_stmt_p = false;
13141 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13143 non_compound_stmt_p = true;
13144 richloc.add_fixit_insert_after (tok->location, "{");
13147 in_consteval_if_p |= ce > 0;
13148 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
13150 if (non_compound_stmt_p)
13152 location_t before_loc
13153 = cp_lexer_peek_token (parser->lexer)->location;
13154 richloc.add_fixit_insert_before (before_loc, "}");
13155 error_at (&richloc,
13156 "%<if consteval%> requires compound statement");
13157 non_compound_stmt_p = false;
13160 finish_then_clause (statement);
13162 /* If the next token is `else', parse the else-clause. */
13163 if (cp_lexer_next_token_is_keyword (parser->lexer,
13164 RID_ELSE))
13166 cp_token *else_tok = cp_lexer_peek_token (parser->lexer);
13167 gcc_rich_location else_richloc (else_tok->location);
13168 guard_tinfo = get_token_indent_info (else_tok);
13169 /* Consume the `else' keyword. */
13170 cp_lexer_consume_token (parser->lexer);
13172 begin_else_clause (statement);
13174 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13176 non_compound_stmt_p = true;
13177 else_richloc.add_fixit_insert_after (else_tok->location,
13178 "{");
13181 in_consteval_if_p = save_in_consteval_if_p | (ce < 0);
13182 cp_parser_implicitly_scoped_statement (parser, NULL,
13183 guard_tinfo);
13185 if (non_compound_stmt_p)
13187 location_t before_loc
13188 = cp_lexer_peek_token (parser->lexer)->location;
13189 else_richloc.add_fixit_insert_before (before_loc, "}");
13190 error_at (&else_richloc,
13191 "%<if consteval%> requires compound statement");
13194 finish_else_clause (statement);
13197 in_consteval_if_p = save_in_consteval_if_p;
13198 if (ce < 0)
13200 std::swap (THEN_CLAUSE (statement), ELSE_CLAUSE (statement));
13201 if (THEN_CLAUSE (statement) == NULL_TREE)
13202 THEN_CLAUSE (statement) = build_empty_stmt (tok->location);
13205 finish_if_stmt (statement);
13206 return statement;
13209 /* Look for the `('. */
13210 matching_parens parens;
13211 if (!parens.require_open (parser))
13213 cp_parser_skip_to_end_of_statement (parser);
13214 return error_mark_node;
13217 /* Begin the selection-statement. */
13218 if (keyword == RID_IF)
13220 statement = begin_if_stmt ();
13221 IF_STMT_CONSTEXPR_P (statement) = cx;
13223 else
13224 statement = begin_switch_stmt ();
13226 /* Parse the optional init-statement. */
13227 if (cp_parser_init_statement_p (parser))
13229 tree decl;
13230 if (cxx_dialect < cxx17)
13231 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
13232 OPT_Wc__17_extensions,
13233 "init-statement in selection statements only available "
13234 "with %<-std=c++17%> or %<-std=gnu++17%>");
13235 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13236 /* A non-empty init-statement can have arbitrary side
13237 effects. */
13238 vec_free (chain);
13239 cp_parser_init_statement (parser, &decl);
13242 /* Parse the condition. */
13243 condition = cp_parser_condition (parser);
13244 /* Look for the `)'. */
13245 if (!parens.require_close (parser))
13246 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13247 /*consume_paren=*/true);
13249 if (keyword == RID_IF)
13251 bool nested_if;
13252 unsigned char in_statement;
13254 /* Add the condition. */
13255 condition = finish_if_stmt_cond (condition, statement);
13257 if (warn_duplicated_cond)
13258 warn_duplicated_cond_add_or_warn (token->location, condition,
13259 &chain);
13261 /* Parse the then-clause. */
13262 in_statement = parser->in_statement;
13263 parser->in_statement |= IN_IF_STMT;
13265 /* Outside a template, the non-selected branch of a constexpr
13266 if is a 'discarded statement', i.e. unevaluated. */
13267 bool was_discarded = in_discarded_stmt;
13268 bool discard_then = (cx && !processing_template_decl
13269 && integer_zerop (condition));
13270 if (discard_then)
13272 in_discarded_stmt = true;
13273 ++c_inhibit_evaluation_warnings;
13276 cp_parser_implicitly_scoped_statement (parser, &nested_if,
13277 guard_tinfo);
13279 parser->in_statement = in_statement;
13281 finish_then_clause (statement);
13283 if (discard_then)
13285 THEN_CLAUSE (statement) = NULL_TREE;
13286 in_discarded_stmt = was_discarded;
13287 --c_inhibit_evaluation_warnings;
13290 /* If the next token is `else', parse the else-clause. */
13291 if (cp_lexer_next_token_is_keyword (parser->lexer,
13292 RID_ELSE))
13294 bool discard_else = (cx && !processing_template_decl
13295 && integer_nonzerop (condition));
13296 if (discard_else)
13298 in_discarded_stmt = true;
13299 ++c_inhibit_evaluation_warnings;
13302 guard_tinfo
13303 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13304 /* Consume the `else' keyword. */
13305 cp_lexer_consume_token (parser->lexer);
13306 if (warn_duplicated_cond)
13308 if (cp_lexer_next_token_is_keyword (parser->lexer,
13309 RID_IF)
13310 && chain == NULL)
13312 /* We've got "if (COND) else if (COND2)". Start
13313 the condition chain and add COND as the first
13314 element. */
13315 chain = new vec<tree> ();
13316 if (!CONSTANT_CLASS_P (condition)
13317 && !TREE_SIDE_EFFECTS (condition))
13319 /* Wrap it in a NOP_EXPR so that we can set the
13320 location of the condition. */
13321 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
13322 condition);
13323 SET_EXPR_LOCATION (e, token->location);
13324 chain->safe_push (e);
13327 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
13328 RID_IF))
13329 /* This is if-else without subsequent if. Zap the
13330 condition chain; we would have already warned at
13331 this point. */
13332 vec_free (chain);
13334 begin_else_clause (statement);
13335 /* Parse the else-clause. */
13336 cp_parser_implicitly_scoped_statement (parser, NULL,
13337 guard_tinfo, chain);
13339 finish_else_clause (statement);
13341 /* If we are currently parsing a then-clause, then
13342 IF_P will not be NULL. We set it to true to
13343 indicate that this if statement has an else clause.
13344 This may trigger the Wparentheses warning below
13345 when we get back up to the parent if statement. */
13346 if (if_p != NULL)
13347 *if_p = true;
13349 if (discard_else)
13351 ELSE_CLAUSE (statement) = NULL_TREE;
13352 in_discarded_stmt = was_discarded;
13353 --c_inhibit_evaluation_warnings;
13356 else
13358 /* This if statement does not have an else clause. If
13359 NESTED_IF is true, then the then-clause has an if
13360 statement which does have an else clause. We warn
13361 about the potential ambiguity. */
13362 if (nested_if)
13363 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
13364 "suggest explicit braces to avoid ambiguous"
13365 " %<else%>");
13366 if (warn_duplicated_cond)
13367 /* We don't need the condition chain anymore. */
13368 vec_free (chain);
13371 /* Now we're all done with the if-statement. */
13372 finish_if_stmt (statement);
13374 else
13376 bool in_switch_statement_p;
13377 unsigned char in_statement;
13379 /* Add the condition. */
13380 finish_switch_cond (condition, statement);
13382 /* Parse the body of the switch-statement. */
13383 in_switch_statement_p = parser->in_switch_statement_p;
13384 in_statement = parser->in_statement;
13385 parser->in_switch_statement_p = true;
13386 parser->in_statement |= IN_SWITCH_STMT;
13387 cp_parser_implicitly_scoped_statement (parser, if_p,
13388 guard_tinfo);
13389 parser->in_switch_statement_p = in_switch_statement_p;
13390 parser->in_statement = in_statement;
13392 /* Now we're all done with the switch-statement. */
13393 finish_switch_stmt (statement);
13396 return statement;
13398 break;
13400 default:
13401 cp_parser_error (parser, "expected selection-statement");
13402 return error_mark_node;
13406 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
13407 If we have seen at least one decl-specifier, and the next token is not
13408 a parenthesis (after "int (" we might be looking at a functional cast)
13409 neither we are dealing with a concept-check expression then we must be
13410 looking at a declaration. */
13412 static void
13413 cp_parser_maybe_commit_to_declaration (cp_parser* parser,
13414 cp_decl_specifier_seq *decl_specs)
13416 if (decl_specs->any_specifiers_p
13417 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
13418 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
13419 && !cp_parser_error_occurred (parser)
13420 && !(decl_specs->type
13421 && TREE_CODE (decl_specs->type) == TYPE_DECL
13422 && is_constrained_auto (TREE_TYPE (decl_specs->type))))
13423 cp_parser_commit_to_tentative_parse (parser);
13426 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
13427 The declarator shall not specify a function or an array. Returns
13428 TRUE if the declarator is valid, FALSE otherwise. */
13430 static bool
13431 cp_parser_check_condition_declarator (cp_parser* parser,
13432 cp_declarator *declarator,
13433 location_t loc)
13435 if (declarator == cp_error_declarator
13436 || function_declarator_p (declarator)
13437 || declarator->kind == cdk_array)
13439 if (declarator == cp_error_declarator)
13440 /* Already complained. */;
13441 else if (declarator->kind == cdk_array)
13442 error_at (loc, "condition declares an array");
13443 else
13444 error_at (loc, "condition declares a function");
13445 if (parser->fully_implicit_function_template_p)
13446 abort_fully_implicit_template (parser);
13447 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
13448 /*or_comma=*/false,
13449 /*consume_paren=*/false);
13450 return false;
13452 else
13453 return true;
13456 /* Parse a condition.
13458 condition:
13459 expression
13460 type-specifier-seq declarator = initializer-clause
13461 type-specifier-seq declarator braced-init-list
13463 GNU Extension:
13465 condition:
13466 type-specifier-seq declarator asm-specification [opt]
13467 attributes [opt] = assignment-expression
13469 Returns the expression that should be tested. */
13471 static tree
13472 cp_parser_condition (cp_parser* parser)
13474 cp_decl_specifier_seq type_specifiers;
13475 const char *saved_message;
13476 int declares_class_or_enum;
13478 /* Try the declaration first. */
13479 cp_parser_parse_tentatively (parser);
13480 /* New types are not allowed in the type-specifier-seq for a
13481 condition. */
13482 saved_message = parser->type_definition_forbidden_message;
13483 parser->type_definition_forbidden_message
13484 = G_("types may not be defined in conditions");
13485 /* Parse the type-specifier-seq. */
13486 cp_parser_decl_specifier_seq (parser,
13487 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
13488 &type_specifiers,
13489 &declares_class_or_enum);
13490 /* Restore the saved message. */
13491 parser->type_definition_forbidden_message = saved_message;
13493 /* Gather the attributes that were provided with the
13494 decl-specifiers. */
13495 tree prefix_attributes = type_specifiers.attributes;
13497 cp_parser_maybe_commit_to_declaration (parser, &type_specifiers);
13499 /* If all is well, we might be looking at a declaration. */
13500 if (!cp_parser_error_occurred (parser))
13502 tree decl;
13503 tree asm_specification;
13504 tree attributes;
13505 cp_declarator *declarator;
13506 tree initializer = NULL_TREE;
13507 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13509 /* Parse the declarator. */
13510 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13511 CP_PARSER_FLAGS_NONE,
13512 /*ctor_dtor_or_conv_p=*/NULL,
13513 /*parenthesized_p=*/NULL,
13514 /*member_p=*/false,
13515 /*friend_p=*/false,
13516 /*static_p=*/false);
13517 /* Parse the attributes. */
13518 attributes = cp_parser_attributes_opt (parser);
13519 /* Parse the asm-specification. */
13520 asm_specification = cp_parser_asm_specification_opt (parser);
13521 /* If the next token is not an `=' or '{', then we might still be
13522 looking at an expression. For example:
13524 if (A(a).x)
13526 looks like a decl-specifier-seq and a declarator -- but then
13527 there is no `=', so this is an expression. */
13528 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
13529 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13530 cp_parser_simulate_error (parser);
13532 /* If we did see an `=' or '{', then we are looking at a declaration
13533 for sure. */
13534 if (cp_parser_parse_definitely (parser))
13536 tree pushed_scope;
13537 bool non_constant_p = false;
13538 int flags = LOOKUP_ONLYCONVERTING;
13540 if (!cp_parser_check_condition_declarator (parser, declarator, loc))
13541 return error_mark_node;
13543 /* Create the declaration. */
13544 decl = start_decl (declarator, &type_specifiers,
13545 /*initialized_p=*/true,
13546 attributes, prefix_attributes,
13547 &pushed_scope);
13549 declarator->init_loc = cp_lexer_peek_token (parser->lexer)->location;
13550 /* Parse the initializer. */
13551 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13553 initializer = cp_parser_braced_list (parser, &non_constant_p);
13554 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
13555 flags = 0;
13557 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13559 /* Consume the `='. */
13560 cp_lexer_consume_token (parser->lexer);
13561 initializer = cp_parser_initializer_clause (parser,
13562 &non_constant_p);
13564 else
13566 cp_parser_error (parser, "expected initializer");
13567 initializer = error_mark_node;
13569 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
13570 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
13572 /* Process the initializer. */
13573 cp_finish_decl (decl,
13574 initializer, !non_constant_p,
13575 asm_specification,
13576 flags);
13578 if (pushed_scope)
13579 pop_scope (pushed_scope);
13581 return convert_from_reference (decl);
13584 /* If we didn't even get past the declarator successfully, we are
13585 definitely not looking at a declaration. */
13586 else
13587 cp_parser_abort_tentative_parse (parser);
13589 /* Otherwise, we are looking at an expression. */
13590 return cp_parser_expression (parser);
13593 /* Parses a for-statement or range-for-statement until the closing ')',
13594 not included. */
13596 static tree
13597 cp_parser_for (cp_parser *parser, bool ivdep, unsigned short unroll)
13599 tree init, scope, decl;
13600 bool is_range_for;
13602 /* Begin the for-statement. */
13603 scope = begin_for_scope (&init);
13605 /* Maybe parse the optional init-statement in a range-based for loop. */
13606 if (cp_parser_range_based_for_with_init_p (parser)
13607 /* Checked for diagnostic purposes only. */
13608 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13610 tree dummy;
13611 cp_parser_init_statement (parser, &dummy);
13612 if (cxx_dialect < cxx20)
13614 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
13615 OPT_Wc__20_extensions,
13616 "range-based %<for%> loops with initializer only "
13617 "available with %<-std=c++20%> or %<-std=gnu++20%>");
13618 decl = error_mark_node;
13622 /* Parse the initialization. */
13623 is_range_for = cp_parser_init_statement (parser, &decl);
13625 if (is_range_for)
13626 return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll,
13627 false);
13628 else
13629 return cp_parser_c_for (parser, scope, init, ivdep, unroll);
13632 static tree
13633 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
13634 unsigned short unroll)
13636 /* Normal for loop */
13637 tree condition = NULL_TREE;
13638 tree expression = NULL_TREE;
13639 tree stmt;
13641 stmt = begin_for_stmt (scope, init);
13642 /* The init-statement has already been parsed in
13643 cp_parser_init_statement, so no work is needed here. */
13644 finish_init_stmt (stmt);
13646 /* If there's a condition, process it. */
13647 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13648 condition = cp_parser_condition (parser);
13649 else if (ivdep)
13651 cp_parser_error (parser, "missing loop condition in loop with "
13652 "%<GCC ivdep%> pragma");
13653 condition = error_mark_node;
13655 else if (unroll)
13657 cp_parser_error (parser, "missing loop condition in loop with "
13658 "%<GCC unroll%> pragma");
13659 condition = error_mark_node;
13661 finish_for_cond (condition, stmt, ivdep, unroll);
13662 /* Look for the `;'. */
13663 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13665 /* If there's an expression, process it. */
13666 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
13667 expression = cp_parser_expression (parser);
13668 finish_for_expr (expression, stmt);
13670 return stmt;
13673 /* Tries to parse a range-based for-statement:
13675 range-based-for:
13676 decl-specifier-seq declarator : expression
13678 The decl-specifier-seq declarator and the `:' are already parsed by
13679 cp_parser_init_statement. If processing_template_decl it returns a
13680 newly created RANGE_FOR_STMT; if not, it is converted to a
13681 regular FOR_STMT. */
13683 static tree
13684 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
13685 bool ivdep, unsigned short unroll, bool is_omp)
13687 tree stmt, range_expr;
13688 auto_vec <cxx_binding *, 16> bindings;
13689 auto_vec <tree, 16> names;
13690 tree decomp_first_name = NULL_TREE;
13691 unsigned int decomp_cnt = 0;
13693 /* Get the range declaration momentarily out of the way so that
13694 the range expression doesn't clash with it. */
13695 if (range_decl != error_mark_node)
13697 if (DECL_HAS_VALUE_EXPR_P (range_decl))
13699 tree v = DECL_VALUE_EXPR (range_decl);
13700 /* For decomposition declaration get all of the corresponding
13701 declarations out of the way. */
13702 if (TREE_CODE (v) == ARRAY_REF
13703 && VAR_P (TREE_OPERAND (v, 0))
13704 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
13706 tree d = range_decl;
13707 range_decl = TREE_OPERAND (v, 0);
13708 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
13709 decomp_first_name = d;
13710 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
13712 tree name = DECL_NAME (d);
13713 names.safe_push (name);
13714 bindings.safe_push (IDENTIFIER_BINDING (name));
13715 IDENTIFIER_BINDING (name)
13716 = IDENTIFIER_BINDING (name)->previous;
13720 if (names.is_empty ())
13722 tree name = DECL_NAME (range_decl);
13723 names.safe_push (name);
13724 bindings.safe_push (IDENTIFIER_BINDING (name));
13725 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
13729 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13731 bool expr_non_constant_p;
13732 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
13734 else
13735 range_expr = cp_parser_expression (parser);
13737 /* Put the range declaration(s) back into scope. */
13738 for (unsigned int i = 0; i < names.length (); i++)
13740 cxx_binding *binding = bindings[i];
13741 binding->previous = IDENTIFIER_BINDING (names[i]);
13742 IDENTIFIER_BINDING (names[i]) = binding;
13745 /* finish_omp_for has its own code for the following, so just
13746 return the range_expr instead. */
13747 if (is_omp)
13748 return range_expr;
13750 /* If in template, STMT is converted to a normal for-statement
13751 at instantiation. If not, it is done just ahead. */
13752 if (processing_template_decl)
13754 if (check_for_bare_parameter_packs (range_expr))
13755 range_expr = error_mark_node;
13756 stmt = begin_range_for_stmt (scope, init);
13757 if (ivdep)
13758 RANGE_FOR_IVDEP (stmt) = 1;
13759 if (unroll)
13760 RANGE_FOR_UNROLL (stmt) = build_int_cst (integer_type_node, unroll);
13761 finish_range_for_decl (stmt, range_decl, range_expr);
13762 if (!type_dependent_expression_p (range_expr)
13763 /* do_auto_deduction doesn't mess with template init-lists. */
13764 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
13765 do_range_for_auto_deduction (range_decl, range_expr, decomp_first_name,
13766 decomp_cnt);
13768 else
13770 stmt = begin_for_stmt (scope, init);
13771 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
13772 decomp_first_name, decomp_cnt, ivdep,
13773 unroll);
13775 return stmt;
13778 /* Subroutine of cp_convert_range_for: given the initializer expression,
13779 builds up the range temporary. */
13781 static tree
13782 build_range_temp (tree range_expr)
13784 /* Find out the type deduced by the declaration
13785 `auto &&__range = range_expr'. */
13786 tree auto_node = make_auto ();
13787 tree range_type = cp_build_reference_type (auto_node, true);
13788 range_type = do_auto_deduction (range_type, range_expr, auto_node);
13790 /* Create the __range variable. */
13791 tree range_temp = build_decl (input_location, VAR_DECL,
13792 for_range__identifier, range_type);
13793 TREE_USED (range_temp) = 1;
13794 DECL_ARTIFICIAL (range_temp) = 1;
13796 return range_temp;
13799 /* Used by cp_parser_range_for in template context: we aren't going to
13800 do a full conversion yet, but we still need to resolve auto in the
13801 type of the for-range-declaration if present. This is basically
13802 a shortcut version of cp_convert_range_for. */
13804 static void
13805 do_range_for_auto_deduction (tree decl, tree range_expr,
13806 tree decomp_first_name, unsigned int decomp_cnt)
13808 tree auto_node = type_uses_auto (TREE_TYPE (decl));
13809 if (auto_node)
13811 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
13812 range_temp = convert_from_reference (build_range_temp (range_expr));
13813 iter_type = (cp_parser_perform_range_for_lookup
13814 (range_temp, &begin_dummy, &end_dummy));
13815 if (iter_type)
13817 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
13818 iter_type);
13819 iter_decl = build_x_indirect_ref (input_location, iter_decl,
13820 RO_UNARY_STAR, NULL_TREE,
13821 tf_warning_or_error);
13822 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
13823 iter_decl, auto_node,
13824 tf_warning_or_error,
13825 adc_variable_type);
13826 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
13827 cp_finish_decomp (decl, decomp_first_name, decomp_cnt);
13832 /* Warns when the loop variable should be changed to a reference type to
13833 avoid unnecessary copying. I.e., from
13835 for (const auto x : range)
13837 where range returns a reference, to
13839 for (const auto &x : range)
13841 if this version doesn't make a copy.
13843 This function also warns when the loop variable is initialized with
13844 a value of a different type resulting in a copy:
13846 int arr[10];
13847 for (const double &x : arr)
13849 DECL is the RANGE_DECL; EXPR is the *__for_begin expression.
13850 This function is never called when processing_template_decl is on. */
13852 static void
13853 warn_for_range_copy (tree decl, tree expr)
13855 if (!warn_range_loop_construct
13856 || decl == error_mark_node)
13857 return;
13859 location_t loc = DECL_SOURCE_LOCATION (decl);
13860 tree type = TREE_TYPE (decl);
13862 if (from_macro_expansion_at (loc))
13863 return;
13865 if (TYPE_REF_P (type))
13867 if (glvalue_p (expr)
13868 && ref_conv_binds_to_temporary (type, expr).is_true ())
13870 auto_diagnostic_group d;
13871 if (warning_at (loc, OPT_Wrange_loop_construct,
13872 "loop variable %qD of type %qT binds to a temporary "
13873 "constructed from type %qT", decl, type,
13874 TREE_TYPE (expr)))
13876 tree ref = cp_build_qualified_type (TREE_TYPE (expr),
13877 TYPE_QUAL_CONST);
13878 ref = cp_build_reference_type (ref, /*rval*/false);
13879 inform (loc, "use non-reference type %qT to make the copy "
13880 "explicit or %qT to prevent copying",
13881 non_reference (type), ref);
13884 return;
13886 else if (!CP_TYPE_CONST_P (type))
13887 return;
13889 /* Since small trivially copyable types are cheap to copy, we suppress the
13890 warning for them. 64B is a common size of a cache line. */
13891 if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST
13892 || (tree_to_uhwi (TYPE_SIZE_UNIT (type)) <= 64
13893 && trivially_copyable_p (type)))
13894 return;
13896 /* If we can initialize a reference directly, suggest that to avoid the
13897 copy. */
13898 tree rtype = cp_build_reference_type (type, /*rval*/false);
13899 if (ref_conv_binds_to_temporary (rtype, expr).is_false ())
13901 auto_diagnostic_group d;
13902 if (warning_at (loc, OPT_Wrange_loop_construct,
13903 "loop variable %qD creates a copy from type %qT",
13904 decl, type))
13906 gcc_rich_location richloc (loc);
13907 richloc.add_fixit_insert_before ("&");
13908 inform (&richloc, "use reference type to prevent copying");
13913 /* Converts a range-based for-statement into a normal
13914 for-statement, as per the definition.
13916 for (RANGE_DECL : RANGE_EXPR)
13917 BLOCK
13919 should be equivalent to:
13922 auto &&__range = RANGE_EXPR;
13923 for (auto __begin = BEGIN_EXPR, __end = END_EXPR;
13924 __begin != __end;
13925 ++__begin)
13927 RANGE_DECL = *__begin;
13928 BLOCK
13932 If RANGE_EXPR is an array:
13933 BEGIN_EXPR = __range
13934 END_EXPR = __range + ARRAY_SIZE(__range)
13935 Else if RANGE_EXPR has a member 'begin' or 'end':
13936 BEGIN_EXPR = __range.begin()
13937 END_EXPR = __range.end()
13938 Else:
13939 BEGIN_EXPR = begin(__range)
13940 END_EXPR = end(__range);
13942 If __range has a member 'begin' but not 'end', or vice versa, we must
13943 still use the second alternative (it will surely fail, however).
13944 When calling begin()/end() in the third alternative we must use
13945 argument dependent lookup, but always considering 'std' as an associated
13946 namespace. */
13948 tree
13949 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
13950 tree decomp_first_name, unsigned int decomp_cnt,
13951 bool ivdep, unsigned short unroll)
13953 tree begin, end;
13954 tree iter_type, begin_expr, end_expr;
13955 tree condition, expression;
13957 range_expr = mark_lvalue_use (range_expr);
13959 if (range_decl == error_mark_node || range_expr == error_mark_node)
13960 /* If an error happened previously do nothing or else a lot of
13961 unhelpful errors would be issued. */
13962 begin_expr = end_expr = iter_type = error_mark_node;
13963 else
13965 tree range_temp;
13967 if (VAR_P (range_expr)
13968 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
13969 /* Can't bind a reference to an array of runtime bound. */
13970 range_temp = range_expr;
13971 else
13973 range_temp = build_range_temp (range_expr);
13974 pushdecl (range_temp);
13975 cp_finish_decl (range_temp, range_expr,
13976 /*is_constant_init*/false, NULL_TREE,
13977 LOOKUP_ONLYCONVERTING);
13978 range_temp = convert_from_reference (range_temp);
13980 iter_type = cp_parser_perform_range_for_lookup (range_temp,
13981 &begin_expr, &end_expr);
13984 /* The new for initialization statement. */
13985 begin = build_decl (input_location, VAR_DECL, for_begin__identifier,
13986 iter_type);
13987 TREE_USED (begin) = 1;
13988 DECL_ARTIFICIAL (begin) = 1;
13989 pushdecl (begin);
13990 cp_finish_decl (begin, begin_expr,
13991 /*is_constant_init*/false, NULL_TREE,
13992 LOOKUP_ONLYCONVERTING);
13994 if (cxx_dialect >= cxx17)
13995 iter_type = cv_unqualified (TREE_TYPE (end_expr));
13996 end = build_decl (input_location, VAR_DECL, for_end__identifier, iter_type);
13997 TREE_USED (end) = 1;
13998 DECL_ARTIFICIAL (end) = 1;
13999 pushdecl (end);
14000 cp_finish_decl (end, end_expr,
14001 /*is_constant_init*/false, NULL_TREE,
14002 LOOKUP_ONLYCONVERTING);
14004 finish_init_stmt (statement);
14006 /* The new for condition. */
14007 condition = build_x_binary_op (input_location, NE_EXPR,
14008 begin, ERROR_MARK,
14009 end, ERROR_MARK,
14010 NULL_TREE, NULL, tf_warning_or_error);
14011 finish_for_cond (condition, statement, ivdep, unroll);
14013 /* The new increment expression. */
14014 expression = finish_unary_op_expr (input_location,
14015 PREINCREMENT_EXPR, begin,
14016 tf_warning_or_error);
14017 finish_for_expr (expression, statement);
14019 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
14020 cp_maybe_mangle_decomp (range_decl, decomp_first_name, decomp_cnt);
14022 /* The declaration is initialized with *__begin inside the loop body. */
14023 tree deref_begin = build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
14024 NULL_TREE, tf_warning_or_error);
14025 cp_finish_decl (range_decl, deref_begin,
14026 /*is_constant_init*/false, NULL_TREE,
14027 LOOKUP_ONLYCONVERTING);
14028 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
14029 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
14031 warn_for_range_copy (range_decl, deref_begin);
14033 return statement;
14036 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
14037 We need to solve both at the same time because the method used
14038 depends on the existence of members begin or end.
14039 Returns the type deduced for the iterator expression. */
14041 static tree
14042 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
14044 if (error_operand_p (range))
14046 *begin = *end = error_mark_node;
14047 return error_mark_node;
14050 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
14052 error ("range-based %<for%> expression of type %qT "
14053 "has incomplete type", TREE_TYPE (range));
14054 *begin = *end = error_mark_node;
14055 return error_mark_node;
14057 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
14059 /* If RANGE is an array, we will use pointer arithmetic. */
14060 *begin = decay_conversion (range, tf_warning_or_error);
14061 *end = build_binary_op (input_location, PLUS_EXPR,
14062 range,
14063 array_type_nelts_top (TREE_TYPE (range)),
14064 false);
14065 return TREE_TYPE (*begin);
14067 else
14069 /* If it is not an array, we must do a bit of magic. */
14070 tree id_begin, id_end;
14071 tree member_begin, member_end;
14073 *begin = *end = error_mark_node;
14075 id_begin = get_identifier ("begin");
14076 id_end = get_identifier ("end");
14077 member_begin = lookup_member (TREE_TYPE (range), id_begin,
14078 /*protect=*/2, /*want_type=*/false,
14079 tf_warning_or_error);
14080 member_end = lookup_member (TREE_TYPE (range), id_end,
14081 /*protect=*/2, /*want_type=*/false,
14082 tf_warning_or_error);
14084 if (member_begin != NULL_TREE && member_end != NULL_TREE)
14086 /* Use the member functions. */
14087 *begin = cp_parser_range_for_member_function (range, id_begin);
14088 *end = cp_parser_range_for_member_function (range, id_end);
14090 else
14092 /* Use global functions with ADL. */
14093 releasing_vec vec;
14095 vec_safe_push (vec, range);
14097 member_begin = perform_koenig_lookup (id_begin, vec,
14098 tf_warning_or_error);
14099 *begin = finish_call_expr (member_begin, &vec, false, true,
14100 tf_warning_or_error);
14101 member_end = perform_koenig_lookup (id_end, vec,
14102 tf_warning_or_error);
14103 *end = finish_call_expr (member_end, &vec, false, true,
14104 tf_warning_or_error);
14107 /* Last common checks. */
14108 if (*begin == error_mark_node || *end == error_mark_node)
14110 /* If one of the expressions is an error do no more checks. */
14111 *begin = *end = error_mark_node;
14112 return error_mark_node;
14114 else if (type_dependent_expression_p (*begin)
14115 || type_dependent_expression_p (*end))
14116 /* Can happen, when, eg, in a template context, Koenig lookup
14117 can't resolve begin/end (c++/58503). */
14118 return NULL_TREE;
14119 else
14121 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
14122 /* The unqualified type of the __begin and __end temporaries should
14123 be the same, as required by the multiple auto declaration. */
14124 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
14126 if (cxx_dialect >= cxx17
14127 && (build_x_binary_op (input_location, NE_EXPR,
14128 *begin, ERROR_MARK,
14129 *end, ERROR_MARK,
14130 NULL_TREE, NULL, tf_none)
14131 != error_mark_node))
14132 /* P0184R0 allows __begin and __end to have different types,
14133 but make sure they are comparable so we can give a better
14134 diagnostic. */;
14135 else
14136 error ("inconsistent begin/end types in range-based %<for%> "
14137 "statement: %qT and %qT",
14138 TREE_TYPE (*begin), TREE_TYPE (*end));
14140 return iter_type;
14145 /* Helper function for cp_parser_perform_range_for_lookup.
14146 Builds a tree for RANGE.IDENTIFIER(). */
14148 static tree
14149 cp_parser_range_for_member_function (tree range, tree identifier)
14151 tree member, res;
14153 member = finish_class_member_access_expr (range, identifier,
14154 false, tf_warning_or_error);
14155 if (member == error_mark_node)
14156 return error_mark_node;
14158 releasing_vec vec;
14159 res = finish_call_expr (member, &vec,
14160 /*disallow_virtual=*/false,
14161 /*koenig_p=*/false,
14162 tf_warning_or_error);
14163 return res;
14166 /* Parse an iteration-statement.
14168 iteration-statement:
14169 while ( condition ) statement
14170 do statement while ( expression ) ;
14171 for ( init-statement condition [opt] ; expression [opt] )
14172 statement
14174 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
14176 static tree
14177 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
14178 unsigned short unroll)
14180 cp_token *token;
14181 enum rid keyword;
14182 tree statement;
14183 unsigned char in_statement;
14184 token_indent_info guard_tinfo;
14186 /* Peek at the next token. */
14187 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
14188 if (!token)
14189 return error_mark_node;
14191 guard_tinfo = get_token_indent_info (token);
14193 /* Remember whether or not we are already within an iteration
14194 statement. */
14195 in_statement = parser->in_statement;
14197 /* See what kind of keyword it is. */
14198 keyword = token->keyword;
14199 switch (keyword)
14201 case RID_WHILE:
14203 tree condition;
14205 /* Begin the while-statement. */
14206 statement = begin_while_stmt ();
14207 /* Look for the `('. */
14208 matching_parens parens;
14209 parens.require_open (parser);
14210 /* Parse the condition. */
14211 condition = cp_parser_condition (parser);
14212 finish_while_stmt_cond (condition, statement, ivdep, unroll);
14213 /* Look for the `)'. */
14214 parens.require_close (parser);
14215 /* Parse the dependent statement. */
14216 parser->in_statement = IN_ITERATION_STMT;
14217 bool prev = note_iteration_stmt_body_start ();
14218 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
14219 note_iteration_stmt_body_end (prev);
14220 parser->in_statement = in_statement;
14221 /* We're done with the while-statement. */
14222 finish_while_stmt (statement);
14224 break;
14226 case RID_DO:
14228 tree expression;
14230 /* Begin the do-statement. */
14231 statement = begin_do_stmt ();
14232 /* Parse the body of the do-statement. */
14233 parser->in_statement = IN_ITERATION_STMT;
14234 bool prev = note_iteration_stmt_body_start ();
14235 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
14236 note_iteration_stmt_body_end (prev);
14237 parser->in_statement = in_statement;
14238 finish_do_body (statement);
14239 /* Look for the `while' keyword. */
14240 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
14241 /* Look for the `('. */
14242 matching_parens parens;
14243 parens.require_open (parser);
14244 /* Parse the expression. */
14245 expression = cp_parser_expression (parser);
14246 /* We're done with the do-statement. */
14247 finish_do_stmt (expression, statement, ivdep, unroll);
14248 /* Look for the `)'. */
14249 parens.require_close (parser);
14250 /* Look for the `;'. */
14251 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14253 break;
14255 case RID_FOR:
14257 /* Look for the `('. */
14258 matching_parens parens;
14259 parens.require_open (parser);
14261 statement = cp_parser_for (parser, ivdep, unroll);
14263 /* Look for the `)'. */
14264 parens.require_close (parser);
14266 /* Parse the body of the for-statement. */
14267 parser->in_statement = IN_ITERATION_STMT;
14268 bool prev = note_iteration_stmt_body_start ();
14269 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
14270 note_iteration_stmt_body_end (prev);
14271 parser->in_statement = in_statement;
14273 /* We're done with the for-statement. */
14274 finish_for_stmt (statement);
14276 break;
14278 default:
14279 cp_parser_error (parser, "expected iteration-statement");
14280 statement = error_mark_node;
14281 break;
14284 return statement;
14287 /* Parse an init-statement or the declarator of a range-based-for.
14288 Returns true if a range-based-for declaration is seen.
14290 init-statement:
14291 expression-statement
14292 simple-declaration
14293 alias-declaration */
14295 static bool
14296 cp_parser_init_statement (cp_parser *parser, tree *decl)
14298 /* If the next token is a `;', then we have an empty
14299 expression-statement. Grammatically, this is also a
14300 simple-declaration, but an invalid one, because it does not
14301 declare anything. Therefore, if we did not handle this case
14302 specially, we would issue an error message about an invalid
14303 declaration. */
14304 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14306 bool is_range_for = false;
14307 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
14309 /* A colon is used in range-based for. */
14310 parser->colon_corrects_to_scope_p = false;
14312 /* We're going to speculatively look for a declaration, falling back
14313 to an expression, if necessary. */
14314 cp_parser_parse_tentatively (parser);
14315 bool expect_semicolon_p = true;
14316 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
14318 cp_parser_alias_declaration (parser);
14319 expect_semicolon_p = false;
14320 if (cxx_dialect < cxx23
14321 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
14322 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
14323 OPT_Wc__23_extensions,
14324 "alias-declaration in init-statement only "
14325 "available with %<-std=c++23%> or %<-std=gnu++23%>");
14327 else
14328 /* Parse the declaration. */
14329 cp_parser_simple_declaration (parser,
14330 /*function_definition_allowed_p=*/false,
14331 decl);
14332 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
14333 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14335 /* It is a range-for, consume the ':'. */
14336 cp_lexer_consume_token (parser->lexer);
14337 is_range_for = true;
14338 if (cxx_dialect < cxx11)
14339 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
14340 OPT_Wc__11_extensions,
14341 "range-based %<for%> loops only available with "
14342 "%<-std=c++11%> or %<-std=gnu++11%>");
14344 else if (expect_semicolon_p)
14345 /* The ';' is not consumed yet because we told
14346 cp_parser_simple_declaration not to. */
14347 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14349 if (cp_parser_parse_definitely (parser))
14350 return is_range_for;
14351 /* If the tentative parse failed, then we shall need to look for an
14352 expression-statement. */
14354 /* If we are here, it is an expression-statement. */
14355 cp_parser_expression_statement (parser, NULL_TREE);
14356 return false;
14359 /* Parse a jump-statement.
14361 jump-statement:
14362 break ;
14363 continue ;
14364 return expression [opt] ;
14365 return braced-init-list ;
14366 coroutine-return-statement;
14367 goto identifier ;
14369 GNU extension:
14371 jump-statement:
14372 goto * expression ;
14374 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
14376 static tree
14377 cp_parser_jump_statement (cp_parser* parser)
14379 tree statement = error_mark_node;
14380 cp_token *token;
14381 enum rid keyword;
14382 unsigned char in_statement;
14384 /* Peek at the next token. */
14385 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
14386 if (!token)
14387 return error_mark_node;
14389 /* See what kind of keyword it is. */
14390 keyword = token->keyword;
14391 switch (keyword)
14393 case RID_BREAK:
14394 in_statement = parser->in_statement & ~IN_IF_STMT;
14395 switch (in_statement)
14397 case 0:
14398 error_at (token->location, "break statement not within loop or switch");
14399 break;
14400 default:
14401 gcc_assert ((in_statement & IN_SWITCH_STMT)
14402 || in_statement == IN_ITERATION_STMT);
14403 statement = finish_break_stmt ();
14404 if (in_statement == IN_ITERATION_STMT)
14405 break_maybe_infinite_loop ();
14406 break;
14407 case IN_OMP_BLOCK:
14408 error_at (token->location, "invalid exit from OpenMP structured block");
14409 break;
14410 case IN_OMP_FOR:
14411 error_at (token->location, "break statement used with OpenMP for loop");
14412 break;
14414 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14415 break;
14417 case RID_CONTINUE:
14418 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
14420 case 0:
14421 error_at (token->location, "continue statement not within a loop");
14422 break;
14423 /* Fall through. */
14424 case IN_ITERATION_STMT:
14425 case IN_OMP_FOR:
14426 statement = finish_continue_stmt ();
14427 break;
14428 case IN_OMP_BLOCK:
14429 error_at (token->location, "invalid exit from OpenMP structured block");
14430 break;
14431 default:
14432 gcc_unreachable ();
14434 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14435 break;
14437 case RID_CO_RETURN:
14438 case RID_RETURN:
14440 tree expr;
14441 bool expr_non_constant_p;
14443 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14445 cp_lexer_set_source_position (parser->lexer);
14446 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14447 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
14449 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14450 expr = cp_parser_expression (parser);
14451 else
14452 /* If the next token is a `;', then there is no
14453 expression. */
14454 expr = NULL_TREE;
14455 /* Build the return-statement, check co-return first, since type
14456 deduction is not valid there. */
14457 if (keyword == RID_CO_RETURN)
14458 statement = finish_co_return_stmt (token->location, expr);
14459 else if (FNDECL_USED_AUTO (current_function_decl) && in_discarded_stmt)
14460 /* Don't deduce from a discarded return statement. */;
14461 else
14462 statement = finish_return_stmt (expr);
14463 /* Look for the final `;'. */
14464 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14466 break;
14468 case RID_GOTO:
14469 if (parser->in_function_body
14470 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
14471 && cxx_dialect < cxx23)
14473 error ("%<goto%> in %<constexpr%> function only available with "
14474 "%<-std=c++2b%> or %<-std=gnu++2b%>");
14475 cp_function_chain->invalid_constexpr = true;
14478 /* Create the goto-statement. */
14479 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
14481 /* Issue a warning about this use of a GNU extension. */
14482 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
14483 /* Consume the '*' token. */
14484 cp_lexer_consume_token (parser->lexer);
14485 /* Parse the dependent expression. */
14486 finish_goto_stmt (cp_parser_expression (parser));
14488 else
14489 finish_goto_stmt (cp_parser_identifier (parser));
14490 /* Look for the final `;'. */
14491 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14492 break;
14494 default:
14495 cp_parser_error (parser, "expected jump-statement");
14496 break;
14499 return statement;
14502 /* Parse a declaration-statement.
14504 declaration-statement:
14505 block-declaration */
14507 static void
14508 cp_parser_declaration_statement (cp_parser* parser)
14510 void *p;
14512 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
14513 p = obstack_alloc (&declarator_obstack, 0);
14515 /* Parse the block-declaration. */
14516 cp_parser_block_declaration (parser, /*statement_p=*/true);
14518 /* Free any declarators allocated. */
14519 obstack_free (&declarator_obstack, p);
14522 /* Some dependent statements (like `if (cond) statement'), are
14523 implicitly in their own scope. In other words, if the statement is
14524 a single statement (as opposed to a compound-statement), it is
14525 none-the-less treated as if it were enclosed in braces. Any
14526 declarations appearing in the dependent statement are out of scope
14527 after control passes that point. This function parses a statement,
14528 but ensures that is in its own scope, even if it is not a
14529 compound-statement.
14531 If IF_P is not NULL, *IF_P is set to indicate whether the statement
14532 is a (possibly labeled) if statement which is not enclosed in
14533 braces and has an else clause. This is used to implement
14534 -Wparentheses.
14536 CHAIN is a vector of if-else-if conditions. This is used to implement
14537 -Wduplicated-cond.
14539 Returns the new statement. */
14541 static tree
14542 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
14543 const token_indent_info &guard_tinfo,
14544 vec<tree> *chain)
14546 tree statement;
14547 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
14548 location_t body_loc_after_labels = UNKNOWN_LOCATION;
14549 token_indent_info body_tinfo
14550 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
14552 if (if_p != NULL)
14553 *if_p = false;
14555 /* Mark if () ; with a special NOP_EXPR. */
14556 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14558 cp_lexer_consume_token (parser->lexer);
14559 statement = add_stmt (build_empty_stmt (body_loc));
14561 if (guard_tinfo.keyword == RID_IF
14562 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
14563 warning_at (body_loc, OPT_Wempty_body,
14564 "suggest braces around empty body in an %<if%> statement");
14565 else if (guard_tinfo.keyword == RID_ELSE)
14566 warning_at (body_loc, OPT_Wempty_body,
14567 "suggest braces around empty body in an %<else%> statement");
14569 /* if a compound is opened, we simply parse the statement directly. */
14570 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14571 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
14572 /* If the token is not a `{', then we must take special action. */
14573 else
14575 /* Create a compound-statement. */
14576 statement = begin_compound_stmt (0);
14577 /* Parse the dependent-statement. */
14578 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
14579 &body_loc_after_labels);
14580 /* Finish the dummy compound-statement. */
14581 finish_compound_stmt (statement);
14584 token_indent_info next_tinfo
14585 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
14586 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
14588 if (body_loc_after_labels != UNKNOWN_LOCATION
14589 && next_tinfo.type != CPP_SEMICOLON)
14590 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
14591 guard_tinfo.location, guard_tinfo.keyword);
14593 /* Return the statement. */
14594 return statement;
14597 /* For some dependent statements (like `while (cond) statement'), we
14598 have already created a scope. Therefore, even if the dependent
14599 statement is a compound-statement, we do not want to create another
14600 scope. */
14602 static void
14603 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
14604 const token_indent_info &guard_tinfo)
14606 /* If the token is a `{', then we must take special action. */
14607 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14609 token_indent_info body_tinfo
14610 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
14611 location_t loc_after_labels = UNKNOWN_LOCATION;
14613 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
14614 &loc_after_labels);
14615 token_indent_info next_tinfo
14616 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
14617 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
14619 if (loc_after_labels != UNKNOWN_LOCATION
14620 && next_tinfo.type != CPP_SEMICOLON)
14621 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
14622 guard_tinfo.location,
14623 guard_tinfo.keyword);
14625 else
14627 /* Avoid calling cp_parser_compound_statement, so that we
14628 don't create a new scope. Do everything else by hand. */
14629 matching_braces braces;
14630 braces.require_open (parser);
14631 /* If the next keyword is `__label__' we have a label declaration. */
14632 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
14633 cp_parser_label_declaration (parser);
14634 /* Parse an (optional) statement-seq. */
14635 cp_parser_statement_seq_opt (parser, NULL_TREE);
14636 braces.require_close (parser);
14640 /* Modules */
14642 /* Parse a module-name,
14643 identifier
14644 module-name . identifier
14645 header-name
14647 Returns a pointer to module object, NULL. */
14649 static module_state *
14650 cp_parser_module_name (cp_parser *parser)
14652 cp_token *token = cp_lexer_peek_token (parser->lexer);
14653 if (token->type == CPP_HEADER_NAME)
14655 cp_lexer_consume_token (parser->lexer);
14657 return get_module (token->u.value);
14660 module_state *parent = NULL;
14661 bool partitioned = false;
14662 if (token->type == CPP_COLON && named_module_p ())
14664 partitioned = true;
14665 cp_lexer_consume_token (parser->lexer);
14668 for (;;)
14670 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME)
14672 cp_parser_error (parser, "expected module-name");
14673 break;
14676 tree name = cp_lexer_consume_token (parser->lexer)->u.value;
14677 parent = get_module (name, parent, partitioned);
14678 token = cp_lexer_peek_token (parser->lexer);
14679 if (!partitioned && token->type == CPP_COLON)
14680 partitioned = true;
14681 else if (token->type != CPP_DOT)
14682 break;
14684 cp_lexer_consume_token (parser->lexer);
14687 return parent;
14690 /* Named module-declaration
14691 __module ; PRAGMA_EOL
14692 __module private ; PRAGMA_EOL (unimplemented)
14693 [__export] __module module-name attr-spec-seq-opt ; PRAGMA_EOL
14696 static module_parse
14697 cp_parser_module_declaration (cp_parser *parser, module_parse mp_state,
14698 bool exporting)
14700 /* We're a pseudo pragma. */
14701 parser->lexer->in_pragma = true;
14702 cp_token *token = cp_lexer_consume_token (parser->lexer);
14704 if (flag_header_unit)
14706 error_at (token->location,
14707 "module-declaration not permitted in header-unit");
14708 goto skip_eol;
14710 else if (mp_state == MP_FIRST && !exporting
14711 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14713 /* Start global module fragment. */
14714 cp_lexer_consume_token (parser->lexer);
14715 module_kind = MK_NAMED;
14716 mp_state = MP_GLOBAL;
14717 cp_parser_require_pragma_eol (parser, token);
14719 else if (!exporting
14720 && cp_lexer_next_token_is (parser->lexer, CPP_COLON)
14721 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_PRIVATE)
14722 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_SEMICOLON))
14724 cp_lexer_consume_token (parser->lexer);
14725 cp_lexer_consume_token (parser->lexer);
14726 cp_lexer_consume_token (parser->lexer);
14727 cp_parser_require_pragma_eol (parser, token);
14729 if ((mp_state == MP_PURVIEW || mp_state == MP_PURVIEW_IMPORTS)
14730 && module_has_cmi_p ())
14732 mp_state = MP_PRIVATE_IMPORTS;
14733 sorry_at (token->location, "private module fragment");
14735 else
14736 error_at (token->location,
14737 "private module fragment only permitted in purview"
14738 " of module interface or partition");
14740 else if (!(mp_state == MP_FIRST || mp_state == MP_GLOBAL))
14742 /* Neither the first declaration, nor in a GMF. */
14743 error_at (token->location, "module-declaration only permitted as first"
14744 " declaration, or ending a global module fragment");
14745 skip_eol:
14746 cp_parser_skip_to_pragma_eol (parser, token);
14748 else
14750 module_state *mod = cp_parser_module_name (parser);
14751 tree attrs = cp_parser_attributes_opt (parser);
14753 mp_state = MP_PURVIEW_IMPORTS;
14754 if (!mod || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
14755 goto skip_eol;
14757 declare_module (mod, token->location, exporting, attrs, parse_in);
14758 cp_parser_require_pragma_eol (parser, token);
14761 return mp_state;
14764 /* Import-declaration
14765 [__export] __import module-name attr-spec-seq-opt ; PRAGMA_EOL */
14767 static void
14768 cp_parser_import_declaration (cp_parser *parser, module_parse mp_state,
14769 bool exporting)
14771 /* We're a pseudo pragma. */
14772 parser->lexer->in_pragma = true;
14773 cp_token *token = cp_lexer_consume_token (parser->lexer);
14775 if (mp_state == MP_PURVIEW || mp_state == MP_PRIVATE)
14777 error_at (token->location, "post-module-declaration"
14778 " imports must be contiguous");
14779 note_lexer:
14780 inform (token->location, "perhaps insert a line break, or other"
14781 " disambiguation, to prevent this being considered a"
14782 " module control-line");
14783 skip_eol:
14784 cp_parser_skip_to_pragma_eol (parser, token);
14786 else if (current_scope () != global_namespace)
14788 error_at (token->location, "import-declaration must be at global scope");
14789 goto note_lexer;
14791 else
14793 module_state *mod = cp_parser_module_name (parser);
14794 tree attrs = cp_parser_attributes_opt (parser);
14796 if (!mod || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
14797 goto skip_eol;
14798 cp_parser_require_pragma_eol (parser, token);
14800 if (parser->in_unbraced_linkage_specification_p)
14801 error_at (token->location, "import cannot appear directly in"
14802 " a linkage-specification");
14804 if (mp_state == MP_PURVIEW_IMPORTS || mp_state == MP_PRIVATE_IMPORTS)
14806 /* Module-purview imports must not be from source inclusion
14807 [cpp.import]/7 */
14808 if (attrs
14809 && private_lookup_attribute ("__translated",
14810 strlen ("__translated"), attrs))
14811 error_at (token->location, "post-module-declaration imports"
14812 " must not be include-translated");
14813 else if (!token->main_source_p)
14814 error_at (token->location, "post-module-declaration imports"
14815 " must not be from header inclusion");
14818 import_module (mod, token->location, exporting, attrs, parse_in);
14822 /* export-declaration.
14824 export declaration
14825 export { declaration-seq-opt } */
14827 static void
14828 cp_parser_module_export (cp_parser *parser)
14830 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT));
14831 cp_token *token = cp_lexer_consume_token (parser->lexer);
14833 if (!module_interface_p ())
14834 error_at (token->location,
14835 "%qE may only occur after a module interface declaration",
14836 token->u.value);
14838 bool braced = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE);
14840 unsigned mk = module_kind;
14841 if (module_exporting_p ())
14842 error_at (token->location,
14843 "%qE may only occur once in an export declaration",
14844 token->u.value);
14845 module_kind |= MK_EXPORTING;
14847 if (braced)
14849 cp_ensure_no_omp_declare_simd (parser);
14850 cp_ensure_no_oacc_routine (parser);
14852 cp_lexer_consume_token (parser->lexer);
14853 cp_parser_declaration_seq_opt (parser);
14854 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14856 else
14858 /* Explicitly check if the next tokens might be a
14859 module-directive line, so we can give a clearer error message
14860 about why the directive will be rejected. */
14861 if (cp_lexer_next_token_is_keyword (parser->lexer, RID__MODULE)
14862 || cp_lexer_next_token_is_keyword (parser->lexer, RID__IMPORT)
14863 || cp_lexer_next_token_is_keyword (parser->lexer, RID__EXPORT))
14864 error_at (token->location, "%<export%> not part of following"
14865 " module-directive");
14866 cp_parser_declaration (parser, NULL_TREE);
14869 module_kind = mk;
14872 /* Declarations [gram.dcl.dcl] */
14874 /* Parse an optional declaration-sequence. TOP_LEVEL is true, if this
14875 is the top-level declaration sequence. That affects whether we
14876 deal with module-preamble.
14878 declaration-seq:
14879 declaration
14880 declaration-seq declaration */
14882 static void
14883 cp_parser_declaration_seq_opt (cp_parser* parser)
14885 while (true)
14887 cp_token *token = cp_lexer_peek_token (parser->lexer);
14889 if (token->type == CPP_CLOSE_BRACE
14890 || token->type == CPP_EOF)
14891 break;
14892 else
14893 cp_parser_toplevel_declaration (parser);
14897 /* Parse a declaration.
14899 declaration:
14900 block-declaration
14901 function-definition
14902 template-declaration
14903 explicit-instantiation
14904 explicit-specialization
14905 linkage-specification
14906 namespace-definition
14908 C++17:
14909 deduction-guide
14911 modules:
14912 (all these are only allowed at the outermost level, check
14913 that semantically, for better diagnostics)
14914 module-declaration
14915 module-export-declaration
14916 module-import-declaration
14917 export-declaration
14919 GNU extension:
14921 declaration:
14922 __extension__ declaration */
14924 static void
14925 cp_parser_declaration (cp_parser* parser, tree prefix_attrs)
14927 int saved_pedantic;
14929 /* Check for the `__extension__' keyword. */
14930 if (cp_parser_extension_opt (parser, &saved_pedantic))
14932 /* Parse the qualified declaration. */
14933 cp_parser_declaration (parser, prefix_attrs);
14934 /* Restore the PEDANTIC flag. */
14935 pedantic = saved_pedantic;
14937 return;
14940 /* Try to figure out what kind of declaration is present. */
14941 cp_token *token1 = cp_lexer_peek_token (parser->lexer);
14942 cp_token *token2 = (token1->type == CPP_EOF
14943 ? token1 : cp_lexer_peek_nth_token (parser->lexer, 2));
14945 if (token1->type == CPP_SEMICOLON)
14947 cp_lexer_consume_token (parser->lexer);
14948 /* A declaration consisting of a single semicolon is invalid
14949 * before C++11. Allow it unless we're being pedantic. */
14950 if (cxx_dialect < cxx11)
14951 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
14952 return;
14954 else if (cp_lexer_nth_token_is (parser->lexer,
14955 cp_parser_skip_std_attribute_spec_seq (parser,
14957 CPP_SEMICOLON))
14959 location_t attrs_loc = token1->location;
14960 tree std_attrs = cp_parser_std_attribute_spec_seq (parser);
14962 if (std_attrs && (flag_openmp || flag_openmp_simd))
14964 gcc_assert (!parser->lexer->in_omp_attribute_pragma);
14965 std_attrs = cp_parser_handle_statement_omp_attributes (parser,
14966 std_attrs);
14967 if (parser->lexer->in_omp_attribute_pragma)
14969 cp_lexer *lexer = parser->lexer;
14970 while (parser->lexer->in_omp_attribute_pragma)
14972 gcc_assert (cp_lexer_next_token_is (parser->lexer,
14973 CPP_PRAGMA));
14974 cp_parser_pragma (parser, pragma_external, NULL);
14976 cp_lexer_destroy (lexer);
14980 if (std_attrs != NULL_TREE && !attribute_ignored_p (std_attrs))
14981 warning_at (make_location (attrs_loc, attrs_loc, parser->lexer),
14982 OPT_Wattributes, "attribute ignored");
14983 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14984 cp_lexer_consume_token (parser->lexer);
14985 return;
14988 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
14989 void *p = obstack_alloc (&declarator_obstack, 0);
14991 tree attributes = NULL_TREE;
14993 /* Conditionally, allow attributes to precede a linkage specification. */
14994 if (token1->keyword == RID_ATTRIBUTE)
14996 cp_lexer_save_tokens (parser->lexer);
14997 attributes = cp_parser_attributes_opt (parser);
14998 cp_token *t1 = cp_lexer_peek_token (parser->lexer);
14999 cp_token *t2 = (t1->type == CPP_EOF
15000 ? t1 : cp_lexer_peek_nth_token (parser->lexer, 2));
15001 if (t1->keyword == RID_EXTERN
15002 && cp_parser_is_pure_string_literal (t2))
15004 cp_lexer_commit_tokens (parser->lexer);
15005 /* We might have already been here. */
15006 if (!c_dialect_objc ())
15008 location_t where = get_finish (t2->location);
15009 warning_at (token1->location, OPT_Wattributes, "attributes are"
15010 " not permitted in this position");
15011 where = linemap_position_for_loc_and_offset (line_table,
15012 where, 1);
15013 inform (where, "attributes may be inserted here");
15014 attributes = NULL_TREE;
15016 token1 = t1;
15017 token2 = t2;
15019 else
15021 cp_lexer_rollback_tokens (parser->lexer);
15022 attributes = NULL_TREE;
15025 /* If we already had some attributes, and we've added more, then prepend.
15026 Otherwise attributes just contains any that we just read. */
15027 if (prefix_attrs)
15029 if (attributes)
15030 TREE_CHAIN (prefix_attrs) = attributes;
15031 attributes = prefix_attrs;
15034 /* If the next token is `extern' and the following token is a string
15035 literal, then we have a linkage specification. */
15036 if (token1->keyword == RID_EXTERN
15037 && cp_parser_is_pure_string_literal (token2))
15038 cp_parser_linkage_specification (parser, attributes);
15039 /* If the next token is `template', then we have either a template
15040 declaration, an explicit instantiation, or an explicit
15041 specialization. */
15042 else if (token1->keyword == RID_TEMPLATE)
15044 /* `template <>' indicates a template specialization. */
15045 if (token2->type == CPP_LESS
15046 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
15047 cp_parser_explicit_specialization (parser);
15048 /* `template <' indicates a template declaration. */
15049 else if (token2->type == CPP_LESS)
15050 cp_parser_template_declaration (parser, /*member_p=*/false);
15051 /* Anything else must be an explicit instantiation. */
15052 else
15053 cp_parser_explicit_instantiation (parser);
15055 /* If the next token is `export', it's new-style modules or
15056 old-style template. */
15057 else if (token1->keyword == RID_EXPORT)
15059 if (!modules_p ())
15060 cp_parser_template_declaration (parser, /*member_p=*/false);
15061 else
15062 cp_parser_module_export (parser);
15064 else if (cp_token_is_module_directive (token1))
15066 bool exporting = token1->keyword == RID__EXPORT;
15067 cp_token *next = exporting ? token2 : token1;
15068 if (exporting)
15069 cp_lexer_consume_token (parser->lexer);
15070 // In module purview this will be ill-formed.
15071 auto state = (!named_module_p () ? MP_NOT_MODULE
15072 : module_purview_p () ? MP_PURVIEW
15073 : MP_GLOBAL);
15074 if (next->keyword == RID__MODULE)
15075 cp_parser_module_declaration (parser, state, exporting);
15076 else
15077 cp_parser_import_declaration (parser, state, exporting);
15079 /* If the next token is `extern', 'static' or 'inline' and the one
15080 after that is `template', we have a GNU extended explicit
15081 instantiation directive. */
15082 else if (cp_parser_allow_gnu_extensions_p (parser)
15083 && token2->keyword == RID_TEMPLATE
15084 && (token1->keyword == RID_EXTERN
15085 || token1->keyword == RID_STATIC
15086 || token1->keyword == RID_INLINE))
15087 cp_parser_explicit_instantiation (parser);
15088 /* If the next token is `namespace', check for a named or unnamed
15089 namespace definition. */
15090 else if (token1->keyword == RID_NAMESPACE
15091 && (/* A named namespace definition. */
15092 (token2->type == CPP_NAME
15093 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
15094 != CPP_EQ))
15095 || (token2->type == CPP_OPEN_SQUARE
15096 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
15097 == CPP_OPEN_SQUARE)
15098 /* An unnamed namespace definition. */
15099 || token2->type == CPP_OPEN_BRACE
15100 || token2->keyword == RID_ATTRIBUTE))
15101 cp_parser_namespace_definition (parser);
15102 /* An inline (associated) namespace definition. */
15103 else if (token2->keyword == RID_NAMESPACE
15104 && token1->keyword == RID_INLINE)
15105 cp_parser_namespace_definition (parser);
15106 /* Objective-C++ declaration/definition. */
15107 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1->keyword))
15108 cp_parser_objc_declaration (parser, attributes);
15109 else if (c_dialect_objc ()
15110 && token1->keyword == RID_ATTRIBUTE
15111 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
15112 cp_parser_objc_declaration (parser, attributes);
15113 /* At this point we may have a template declared by a concept
15114 introduction. */
15115 else if (flag_concepts
15116 && cp_parser_template_declaration_after_export (parser,
15117 /*member_p=*/false))
15118 /* We did. */;
15119 else
15120 /* Try to parse a block-declaration, or a function-definition. */
15121 cp_parser_block_declaration (parser, /*statement_p=*/false);
15123 /* Free any declarators allocated. */
15124 obstack_free (&declarator_obstack, p);
15127 /* Parse a namespace-scope declaration. */
15129 static void
15130 cp_parser_toplevel_declaration (cp_parser* parser)
15132 cp_token *token = cp_lexer_peek_token (parser->lexer);
15134 if (token->type == CPP_PRAGMA)
15135 /* A top-level declaration can consist solely of a #pragma. A
15136 nested declaration cannot, so this is done here and not in
15137 cp_parser_declaration. (A #pragma at block scope is
15138 handled in cp_parser_statement.) */
15139 cp_parser_pragma (parser, pragma_external, NULL);
15140 else
15141 /* Parse the declaration itself. */
15142 cp_parser_declaration (parser, NULL_TREE);
15145 /* Parse a block-declaration.
15147 block-declaration:
15148 simple-declaration
15149 asm-definition
15150 namespace-alias-definition
15151 using-declaration
15152 using-directive
15154 GNU Extension:
15156 block-declaration:
15157 __extension__ block-declaration
15159 C++0x Extension:
15161 block-declaration:
15162 static_assert-declaration
15164 If STATEMENT_P is TRUE, then this block-declaration is occurring as
15165 part of a declaration-statement. */
15167 static void
15168 cp_parser_block_declaration (cp_parser *parser,
15169 bool statement_p)
15171 int saved_pedantic;
15173 /* Check for the `__extension__' keyword. */
15174 if (cp_parser_extension_opt (parser, &saved_pedantic))
15176 /* Parse the qualified declaration. */
15177 cp_parser_block_declaration (parser, statement_p);
15178 /* Restore the PEDANTIC flag. */
15179 pedantic = saved_pedantic;
15181 return;
15184 /* Peek at the next token to figure out which kind of declaration is
15185 present. */
15186 cp_token *token1 = cp_lexer_peek_token (parser->lexer);
15187 size_t attr_idx;
15189 /* If the next keyword is `asm', we have an asm-definition. */
15190 if (token1->keyword == RID_ASM)
15192 if (statement_p)
15193 cp_parser_commit_to_tentative_parse (parser);
15194 cp_parser_asm_definition (parser);
15196 /* If the next keyword is `namespace', we have a
15197 namespace-alias-definition. */
15198 else if (token1->keyword == RID_NAMESPACE)
15199 cp_parser_namespace_alias_definition (parser);
15200 /* If the next keyword is `using', we have a
15201 using-declaration, a using-directive, or an alias-declaration. */
15202 else if (token1->keyword == RID_USING)
15204 cp_token *token2;
15206 if (statement_p)
15207 cp_parser_commit_to_tentative_parse (parser);
15208 /* If the token after `using' is `namespace', then we have a
15209 using-directive. */
15210 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
15211 if (token2->keyword == RID_NAMESPACE)
15212 cp_parser_using_directive (parser);
15213 else if (token2->keyword == RID_ENUM)
15214 cp_parser_using_enum (parser);
15215 /* If the second token after 'using' is '=', then we have an
15216 alias-declaration. */
15217 else if (cxx_dialect >= cxx11
15218 && token2->type == CPP_NAME
15219 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
15220 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
15221 cp_parser_alias_declaration (parser);
15222 /* Otherwise, it's a using-declaration. */
15223 else
15224 cp_parser_using_declaration (parser,
15225 /*access_declaration_p=*/false);
15227 /* If the next keyword is `__label__' we have a misplaced label
15228 declaration. */
15229 else if (token1->keyword == RID_LABEL)
15231 cp_lexer_consume_token (parser->lexer);
15232 error_at (token1->location, "%<__label__%> not at the beginning of a block");
15233 cp_parser_skip_to_end_of_statement (parser);
15234 /* If the next token is now a `;', consume it. */
15235 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15236 cp_lexer_consume_token (parser->lexer);
15238 /* If the next token is `static_assert' we have a static assertion. */
15239 else if (token1->keyword == RID_STATIC_ASSERT)
15240 cp_parser_static_assert (parser, /*member_p=*/false);
15241 /* If the next tokens after attributes is `using namespace', then we have
15242 a using-directive. */
15243 else if ((attr_idx = cp_parser_skip_std_attribute_spec_seq (parser, 1)) != 1
15244 && cp_lexer_nth_token_is_keyword (parser->lexer, attr_idx,
15245 RID_USING)
15246 && cp_lexer_nth_token_is_keyword (parser->lexer, attr_idx + 1,
15247 RID_NAMESPACE))
15249 if (statement_p)
15250 cp_parser_commit_to_tentative_parse (parser);
15251 cp_parser_using_directive (parser);
15253 /* Anything else must be a simple-declaration. */
15254 else
15255 cp_parser_simple_declaration (parser, !statement_p,
15256 /*maybe_range_for_decl*/NULL);
15259 /* Parse a simple-declaration.
15261 simple-declaration:
15262 decl-specifier-seq [opt] init-declarator-list [opt] ;
15263 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
15264 brace-or-equal-initializer ;
15266 init-declarator-list:
15267 init-declarator
15268 init-declarator-list , init-declarator
15270 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
15271 function-definition as a simple-declaration.
15273 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
15274 parsed declaration if it is an uninitialized single declarator not followed
15275 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
15276 if present, will not be consumed. */
15278 static void
15279 cp_parser_simple_declaration (cp_parser* parser,
15280 bool function_definition_allowed_p,
15281 tree *maybe_range_for_decl)
15283 cp_decl_specifier_seq decl_specifiers;
15284 int declares_class_or_enum;
15285 bool saw_declarator;
15286 location_t comma_loc = UNKNOWN_LOCATION;
15287 location_t init_loc = UNKNOWN_LOCATION;
15289 if (maybe_range_for_decl)
15290 *maybe_range_for_decl = NULL_TREE;
15292 /* Defer access checks until we know what is being declared; the
15293 checks for names appearing in the decl-specifier-seq should be
15294 done as if we were in the scope of the thing being declared. */
15295 push_deferring_access_checks (dk_deferred);
15297 /* Parse the decl-specifier-seq. We have to keep track of whether
15298 or not the decl-specifier-seq declares a named class or
15299 enumeration type, since that is the only case in which the
15300 init-declarator-list is allowed to be empty.
15302 [dcl.dcl]
15304 In a simple-declaration, the optional init-declarator-list can be
15305 omitted only when declaring a class or enumeration, that is when
15306 the decl-specifier-seq contains either a class-specifier, an
15307 elaborated-type-specifier, or an enum-specifier. */
15308 cp_parser_decl_specifier_seq (parser,
15309 CP_PARSER_FLAGS_OPTIONAL,
15310 &decl_specifiers,
15311 &declares_class_or_enum);
15312 /* We no longer need to defer access checks. */
15313 stop_deferring_access_checks ();
15315 cp_omp_declare_simd_data odsd;
15316 if (decl_specifiers.attributes && (flag_openmp || flag_openmp_simd))
15317 cp_parser_handle_directive_omp_attributes (parser,
15318 &decl_specifiers.attributes,
15319 &odsd, true);
15321 /* In a block scope, a valid declaration must always have a
15322 decl-specifier-seq. By not trying to parse declarators, we can
15323 resolve the declaration/expression ambiguity more quickly. */
15324 if (!function_definition_allowed_p
15325 && !decl_specifiers.any_specifiers_p)
15327 cp_parser_error (parser, "expected declaration");
15328 goto done;
15331 /* If the next two tokens are both identifiers, the code is
15332 erroneous. The usual cause of this situation is code like:
15334 T t;
15336 where "T" should name a type -- but does not. */
15337 if (!decl_specifiers.any_type_specifiers_p
15338 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15340 /* If parsing tentatively, we should commit; we really are
15341 looking at a declaration. */
15342 cp_parser_commit_to_tentative_parse (parser);
15343 /* Give up. */
15344 goto done;
15347 cp_parser_maybe_commit_to_declaration (parser, &decl_specifiers);
15349 /* Look for C++17 decomposition declaration. */
15350 for (size_t n = 1; ; n++)
15351 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
15352 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
15353 continue;
15354 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
15355 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
15356 && decl_specifiers.any_specifiers_p)
15358 tree decl
15359 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
15360 maybe_range_for_decl,
15361 &init_loc);
15363 /* The next token should be either a `,' or a `;'. */
15364 cp_token *token = cp_lexer_peek_token (parser->lexer);
15365 /* If it's a `;', we are done. */
15366 if (token->type == CPP_SEMICOLON)
15367 goto finish;
15368 else if (maybe_range_for_decl)
15370 if (*maybe_range_for_decl == NULL_TREE)
15371 *maybe_range_for_decl = error_mark_node;
15372 goto finish;
15374 /* Anything else is an error. */
15375 else
15377 /* If we have already issued an error message we don't need
15378 to issue another one. */
15379 if ((decl != error_mark_node
15380 && DECL_INITIAL (decl) != error_mark_node)
15381 || cp_parser_uncommitted_to_tentative_parse_p (parser))
15382 cp_parser_error (parser, "expected %<;%>");
15383 /* Skip tokens until we reach the end of the statement. */
15384 cp_parser_skip_to_end_of_statement (parser);
15385 /* If the next token is now a `;', consume it. */
15386 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15387 cp_lexer_consume_token (parser->lexer);
15388 goto done;
15391 else
15392 break;
15394 tree last_type;
15395 bool auto_specifier_p;
15396 /* NULL_TREE if both variable and function declaration are allowed,
15397 error_mark_node if function declaration are not allowed and
15398 a FUNCTION_DECL that should be diagnosed if it is followed by
15399 variable declarations. */
15400 tree auto_function_declaration;
15402 last_type = NULL_TREE;
15403 auto_specifier_p
15404 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
15405 auto_function_declaration = NULL_TREE;
15407 /* Keep going until we hit the `;' at the end of the simple
15408 declaration. */
15409 saw_declarator = false;
15410 while (cp_lexer_next_token_is_not (parser->lexer,
15411 CPP_SEMICOLON))
15413 cp_token *token;
15414 bool function_definition_p;
15415 tree decl;
15416 tree auto_result = NULL_TREE;
15418 if (saw_declarator)
15420 /* If we are processing next declarator, comma is expected */
15421 token = cp_lexer_peek_token (parser->lexer);
15422 gcc_assert (token->type == CPP_COMMA);
15423 cp_lexer_consume_token (parser->lexer);
15424 if (maybe_range_for_decl)
15426 *maybe_range_for_decl = error_mark_node;
15427 if (comma_loc == UNKNOWN_LOCATION)
15428 comma_loc = token->location;
15431 else
15432 saw_declarator = true;
15434 /* Parse the init-declarator. */
15435 decl = cp_parser_init_declarator (parser,
15436 CP_PARSER_FLAGS_NONE,
15437 &decl_specifiers,
15438 /*checks=*/NULL,
15439 function_definition_allowed_p,
15440 /*member_p=*/false,
15441 declares_class_or_enum,
15442 &function_definition_p,
15443 maybe_range_for_decl,
15444 &init_loc,
15445 &auto_result);
15446 /* If an error occurred while parsing tentatively, exit quickly.
15447 (That usually happens when in the body of a function; each
15448 statement is treated as a declaration-statement until proven
15449 otherwise.) */
15450 if (cp_parser_error_occurred (parser))
15451 goto done;
15453 if (auto_specifier_p && cxx_dialect >= cxx14)
15455 /* If the init-declarator-list contains more than one
15456 init-declarator, they shall all form declarations of
15457 variables. */
15458 if (auto_function_declaration == NULL_TREE)
15459 auto_function_declaration
15460 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
15461 else if (TREE_CODE (decl) == FUNCTION_DECL
15462 || auto_function_declaration != error_mark_node)
15464 error_at (decl_specifiers.locations[ds_type_spec],
15465 "non-variable %qD in declaration with more than one "
15466 "declarator with placeholder type",
15467 TREE_CODE (decl) == FUNCTION_DECL
15468 ? decl : auto_function_declaration);
15469 auto_function_declaration = error_mark_node;
15473 if (auto_result
15474 && (!processing_template_decl || !type_uses_auto (auto_result)))
15476 if (last_type
15477 && last_type != error_mark_node
15478 && !same_type_p (auto_result, last_type))
15480 /* If the list of declarators contains more than one declarator,
15481 the type of each declared variable is determined as described
15482 above. If the type deduced for the template parameter U is not
15483 the same in each deduction, the program is ill-formed. */
15484 error_at (decl_specifiers.locations[ds_type_spec],
15485 "inconsistent deduction for %qT: %qT and then %qT",
15486 decl_specifiers.type, last_type, auto_result);
15487 last_type = error_mark_node;
15489 else
15490 last_type = auto_result;
15493 /* Handle function definitions specially. */
15494 if (function_definition_p)
15496 /* If the next token is a `,', then we are probably
15497 processing something like:
15499 void f() {}, *p;
15501 which is erroneous. */
15502 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15504 cp_token *token = cp_lexer_peek_token (parser->lexer);
15505 error_at (token->location,
15506 "mixing"
15507 " declarations and function-definitions is forbidden");
15509 /* Otherwise, we're done with the list of declarators. */
15510 else
15512 pop_deferring_access_checks ();
15513 cp_finalize_omp_declare_simd (parser, &odsd);
15514 return;
15517 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
15518 *maybe_range_for_decl = decl;
15519 /* The next token should be either a `,' or a `;'. */
15520 token = cp_lexer_peek_token (parser->lexer);
15521 /* If it's a `,', there are more declarators to come. */
15522 if (token->type == CPP_COMMA)
15523 /* will be consumed next time around */;
15524 /* If it's a `;', we are done. */
15525 else if (token->type == CPP_SEMICOLON)
15526 break;
15527 else if (maybe_range_for_decl)
15529 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
15530 permerror (decl_specifiers.locations[ds_type_spec],
15531 "types may not be defined in a for-range-declaration");
15532 break;
15534 /* Anything else is an error. */
15535 else
15537 /* If we have already issued an error message we don't need
15538 to issue another one. */
15539 if ((decl != error_mark_node
15540 && DECL_INITIAL (decl) != error_mark_node)
15541 || cp_parser_uncommitted_to_tentative_parse_p (parser))
15542 cp_parser_error (parser, "expected %<,%> or %<;%>");
15543 /* Skip tokens until we reach the end of the statement. */
15544 cp_parser_skip_to_end_of_statement (parser);
15545 /* If the next token is now a `;', consume it. */
15546 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15547 cp_lexer_consume_token (parser->lexer);
15548 goto done;
15550 /* After the first time around, a function-definition is not
15551 allowed -- even if it was OK at first. For example:
15553 int i, f() {}
15555 is not valid. */
15556 function_definition_allowed_p = false;
15559 /* Issue an error message if no declarators are present, and the
15560 decl-specifier-seq does not itself declare a class or
15561 enumeration: [dcl.dcl]/3. */
15562 if (!saw_declarator)
15564 if (cp_parser_declares_only_class_p (parser))
15566 if (!declares_class_or_enum
15567 && decl_specifiers.type
15568 && OVERLOAD_TYPE_P (decl_specifiers.type))
15569 /* Ensure an error is issued anyway when finish_decltype_type,
15570 called via cp_parser_decl_specifier_seq, returns a class or
15571 an enumeration (c++/51786). */
15572 decl_specifiers.type = NULL_TREE;
15573 shadow_tag (&decl_specifiers);
15575 /* Perform any deferred access checks. */
15576 perform_deferred_access_checks (tf_warning_or_error);
15579 /* Consume the `;'. */
15580 finish:
15581 if (!maybe_range_for_decl)
15582 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15583 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15585 if (init_loc != UNKNOWN_LOCATION)
15586 error_at (init_loc, "initializer in range-based %<for%> loop");
15587 if (comma_loc != UNKNOWN_LOCATION)
15588 error_at (comma_loc,
15589 "multiple declarations in range-based %<for%> loop");
15592 done:
15593 pop_deferring_access_checks ();
15594 cp_finalize_omp_declare_simd (parser, &odsd);
15597 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
15598 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
15599 initializer ; */
15601 static tree
15602 cp_parser_decomposition_declaration (cp_parser *parser,
15603 cp_decl_specifier_seq *decl_specifiers,
15604 tree *maybe_range_for_decl,
15605 location_t *init_loc)
15607 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
15608 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
15609 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
15611 /* Parse the identifier-list. */
15612 auto_vec<cp_expr, 10> v;
15613 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
15614 while (true)
15616 cp_expr e = cp_parser_identifier (parser);
15617 if (e.get_value () == error_mark_node)
15618 break;
15619 v.safe_push (e);
15620 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15621 break;
15622 cp_lexer_consume_token (parser->lexer);
15625 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
15626 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15628 end_loc = UNKNOWN_LOCATION;
15629 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
15630 false);
15631 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
15632 cp_lexer_consume_token (parser->lexer);
15633 else
15635 cp_parser_skip_to_end_of_statement (parser);
15636 return error_mark_node;
15640 if (cxx_dialect < cxx17)
15641 pedwarn (loc, OPT_Wc__17_extensions,
15642 "structured bindings only available with "
15643 "%<-std=c++17%> or %<-std=gnu++17%>");
15645 tree pushed_scope;
15646 cp_declarator *declarator = make_declarator (cdk_decomp);
15647 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
15648 declarator->id_loc = loc;
15649 if (ref_qual != REF_QUAL_NONE)
15650 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
15651 ref_qual == REF_QUAL_RVALUE,
15652 NULL_TREE);
15653 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
15654 NULL_TREE, decl_specifiers->attributes,
15655 &pushed_scope);
15656 tree orig_decl = decl;
15658 unsigned int i;
15659 cp_expr e;
15660 cp_decl_specifier_seq decl_specs;
15661 clear_decl_specs (&decl_specs);
15662 decl_specs.type = make_auto ();
15663 tree prev = decl;
15664 FOR_EACH_VEC_ELT (v, i, e)
15666 if (i == 0)
15667 declarator = make_id_declarator (NULL_TREE, e.get_value (),
15668 sfk_none, e.get_location ());
15669 else
15671 declarator->u.id.unqualified_name = e.get_value ();
15672 declarator->id_loc = e.get_location ();
15674 tree elt_pushed_scope;
15675 tree decl2 = start_decl (declarator, &decl_specs, SD_DECOMPOSITION,
15676 NULL_TREE, NULL_TREE, &elt_pushed_scope);
15677 if (decl2 == error_mark_node)
15678 decl = error_mark_node;
15679 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
15681 /* Ensure we've diagnosed redeclaration if we aren't creating
15682 a new VAR_DECL. */
15683 gcc_assert (errorcount);
15684 decl = error_mark_node;
15686 else
15687 prev = decl2;
15688 if (elt_pushed_scope)
15689 pop_scope (elt_pushed_scope);
15692 if (v.is_empty ())
15694 error_at (loc, "empty structured binding declaration");
15695 decl = error_mark_node;
15698 if (maybe_range_for_decl == NULL
15699 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
15701 bool non_constant_p = false, is_direct_init = false;
15702 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
15703 tree initializer = cp_parser_initializer (parser, &is_direct_init,
15704 &non_constant_p);
15705 if (initializer == NULL_TREE
15706 || (TREE_CODE (initializer) == TREE_LIST
15707 && TREE_CHAIN (initializer))
15708 || (is_direct_init
15709 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
15710 && CONSTRUCTOR_NELTS (initializer) != 1))
15712 error_at (loc, "invalid initializer for structured binding "
15713 "declaration");
15714 initializer = error_mark_node;
15717 if (decl != error_mark_node)
15719 cp_maybe_mangle_decomp (decl, prev, v.length ());
15720 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
15721 (is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
15722 cp_finish_decomp (decl, prev, v.length ());
15725 else if (decl != error_mark_node)
15727 *maybe_range_for_decl = prev;
15728 /* Ensure DECL_VALUE_EXPR is created for all the decls but
15729 the underlying DECL. */
15730 cp_finish_decomp (decl, prev, v.length ());
15733 if (pushed_scope)
15734 pop_scope (pushed_scope);
15736 if (decl == error_mark_node && DECL_P (orig_decl))
15738 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
15739 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
15742 return decl;
15745 /* Names of storage classes. */
15747 static const char *const
15748 cp_storage_class_name[] = {
15749 "", "auto", "register", "static", "extern", "mutable"
15752 /* Parse a decl-specifier-seq.
15754 decl-specifier-seq:
15755 decl-specifier-seq [opt] decl-specifier
15756 decl-specifier attribute-specifier-seq [opt] (C++11)
15758 decl-specifier:
15759 storage-class-specifier
15760 type-specifier
15761 function-specifier
15762 friend
15763 typedef
15765 GNU Extension:
15767 decl-specifier:
15768 attributes
15770 Concepts Extension:
15772 decl-specifier:
15773 concept
15775 Set *DECL_SPECS to a representation of the decl-specifier-seq.
15777 The parser flags FLAGS is used to control type-specifier parsing.
15779 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
15780 flags:
15782 1: one of the decl-specifiers is an elaborated-type-specifier
15783 (i.e., a type declaration)
15784 2: one of the decl-specifiers is an enum-specifier or a
15785 class-specifier (i.e., a type definition)
15789 static void
15790 cp_parser_decl_specifier_seq (cp_parser* parser,
15791 cp_parser_flags flags,
15792 cp_decl_specifier_seq *decl_specs,
15793 int* declares_class_or_enum)
15795 bool constructor_possible_p = !parser->in_declarator_p;
15796 bool found_decl_spec = false;
15797 cp_token *start_token = NULL;
15798 cp_decl_spec ds;
15800 /* Clear DECL_SPECS. */
15801 clear_decl_specs (decl_specs);
15803 /* Assume no class or enumeration type is declared. */
15804 *declares_class_or_enum = 0;
15806 /* Keep reading specifiers until there are no more to read. */
15807 while (true)
15809 bool constructor_p;
15810 cp_token *token;
15811 ds = ds_last;
15813 /* Peek at the next token. */
15814 token = cp_lexer_peek_token (parser->lexer);
15816 /* Save the first token of the decl spec list for error
15817 reporting. */
15818 if (!start_token)
15819 start_token = token;
15820 /* Handle attributes. */
15821 if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) == 0
15822 && cp_next_tokens_can_be_attribute_p (parser))
15824 /* Parse the attributes. */
15825 tree attrs = cp_parser_attributes_opt (parser);
15827 /* In a sequence of declaration specifiers, c++11 attributes
15828 appertain to the type that precede them. In that case
15829 [dcl.spec]/1 says:
15831 The attribute-specifier-seq affects the type only for
15832 the declaration it appears in, not other declarations
15833 involving the same type.
15835 But for now let's force the user to position the
15836 attribute either at the beginning of the declaration or
15837 after the declarator-id, which would clearly mean that it
15838 applies to the declarator. */
15839 if (cxx11_attribute_p (attrs))
15841 if (!found_decl_spec)
15842 /* The c++11 attribute is at the beginning of the
15843 declaration. It appertains to the entity being
15844 declared. */;
15845 else
15847 if (find_contract (attrs))
15849 diagnose_misapplied_contracts (attrs);
15850 attrs = NULL_TREE;
15852 else if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
15854 /* This is an attribute following a
15855 class-specifier. */
15856 if (decl_specs->type_definition_p)
15857 warn_misplaced_attr_for_class_type (token->location,
15858 decl_specs->type);
15859 attrs = NULL_TREE;
15861 else
15863 decl_specs->std_attributes
15864 = attr_chainon (decl_specs->std_attributes, attrs);
15865 if (decl_specs->locations[ds_std_attribute] == 0)
15866 decl_specs->locations[ds_std_attribute] = token->location;
15868 continue;
15872 decl_specs->attributes
15873 = attr_chainon (decl_specs->attributes, attrs);
15874 if (decl_specs->locations[ds_attribute] == 0)
15875 decl_specs->locations[ds_attribute] = token->location;
15876 continue;
15878 /* Assume we will find a decl-specifier keyword. */
15879 found_decl_spec = true;
15880 /* If the next token is an appropriate keyword, we can simply
15881 add it to the list. */
15882 switch (token->keyword)
15884 /* decl-specifier:
15885 friend
15886 constexpr
15887 constinit */
15888 case RID_FRIEND:
15889 if (!at_class_scope_p ())
15891 gcc_rich_location richloc (token->location);
15892 richloc.add_fixit_remove ();
15893 error_at (&richloc, "%<friend%> used outside of class");
15894 cp_lexer_purge_token (parser->lexer);
15896 else
15898 ds = ds_friend;
15899 /* Consume the token. */
15900 cp_lexer_consume_token (parser->lexer);
15902 break;
15904 case RID_CONSTEXPR:
15905 ds = ds_constexpr;
15906 cp_lexer_consume_token (parser->lexer);
15907 break;
15909 case RID_CONSTINIT:
15910 ds = ds_constinit;
15911 cp_lexer_consume_token (parser->lexer);
15912 break;
15914 case RID_CONSTEVAL:
15915 ds = ds_consteval;
15916 cp_lexer_consume_token (parser->lexer);
15917 break;
15919 case RID_CONCEPT:
15920 ds = ds_concept;
15921 cp_lexer_consume_token (parser->lexer);
15923 if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
15924 break;
15926 /* Warn for concept as a decl-specifier. We'll rewrite these as
15927 concept declarations later. */
15928 if (!flag_concepts_ts)
15930 cp_token *next = cp_lexer_peek_token (parser->lexer);
15931 if (next->keyword == RID_BOOL)
15932 permerror (next->location, "the %<bool%> keyword is not "
15933 "allowed in a C++20 concept definition");
15934 else
15935 error_at (token->location, "C++20 concept definition syntax "
15936 "is %<concept <name> = <expr>%>");
15939 /* In C++20 a concept definition is just 'concept name = expr;'
15940 Support that syntax as a TS extension by pretending we've seen
15941 the 'bool' specifier. */
15942 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
15943 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ)
15944 && !decl_specs->any_type_specifiers_p)
15946 cp_parser_set_decl_spec_type (decl_specs, boolean_type_node,
15947 token, /*type_definition*/false);
15948 decl_specs->any_type_specifiers_p = true;
15950 break;
15952 /* function-specifier:
15953 inline
15954 virtual
15955 explicit */
15956 case RID_INLINE:
15957 case RID_VIRTUAL:
15958 case RID_EXPLICIT:
15959 cp_parser_function_specifier_opt (parser, decl_specs);
15960 break;
15962 /* decl-specifier:
15963 typedef */
15964 case RID_TYPEDEF:
15965 ds = ds_typedef;
15966 /* Consume the token. */
15967 cp_lexer_consume_token (parser->lexer);
15969 if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
15970 break;
15972 /* A constructor declarator cannot appear in a typedef. */
15973 constructor_possible_p = false;
15974 /* The "typedef" keyword can only occur in a declaration; we
15975 may as well commit at this point. */
15976 cp_parser_commit_to_tentative_parse (parser);
15978 if (decl_specs->storage_class != sc_none)
15980 if (decl_specs->conflicting_specifiers_p)
15981 break;
15982 gcc_rich_location richloc (token->location);
15983 location_t oloc = decl_specs->locations[ds_storage_class];
15984 richloc.add_location_if_nearby (oloc);
15985 error_at (&richloc,
15986 "%<typedef%> specifier conflicts with %qs",
15987 cp_storage_class_name[decl_specs->storage_class]);
15988 decl_specs->conflicting_specifiers_p = true;
15990 break;
15992 /* storage-class-specifier:
15993 auto
15994 register
15995 static
15996 extern
15997 mutable
15999 GNU Extension:
16000 thread */
16001 case RID_AUTO:
16002 if (cxx_dialect == cxx98)
16004 /* Consume the token. */
16005 cp_lexer_consume_token (parser->lexer);
16007 /* Complain about `auto' as a storage specifier, if
16008 we're complaining about C++0x compatibility. */
16009 gcc_rich_location richloc (token->location);
16010 richloc.add_fixit_remove ();
16011 warning_at (&richloc, OPT_Wc__11_compat,
16012 "%<auto%> changes meaning in C++11; "
16013 "please remove it");
16015 /* Set the storage class anyway. */
16016 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
16017 token);
16019 else
16020 /* C++0x auto type-specifier. */
16021 found_decl_spec = false;
16022 break;
16024 case RID_REGISTER:
16025 case RID_STATIC:
16026 case RID_EXTERN:
16027 case RID_MUTABLE:
16028 /* Consume the token. */
16029 cp_lexer_consume_token (parser->lexer);
16030 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
16031 token);
16032 break;
16033 case RID_THREAD:
16034 /* Consume the token. */
16035 ds = ds_thread;
16036 cp_lexer_consume_token (parser->lexer);
16037 break;
16039 default:
16040 /* We did not yet find a decl-specifier yet. */
16041 found_decl_spec = false;
16042 break;
16045 if (found_decl_spec
16046 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
16047 && token->keyword != RID_CONSTEXPR)
16048 error ("%qD invalid in condition", ridpointers[token->keyword]);
16050 if (found_decl_spec
16051 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
16052 && token->keyword != RID_MUTABLE
16053 && token->keyword != RID_CONSTEXPR
16054 && token->keyword != RID_CONSTEVAL)
16056 if (token->keyword != RID_STATIC)
16057 error_at (token->location, "%qD invalid in lambda",
16058 ridpointers[token->keyword]);
16059 else if (cxx_dialect < cxx23)
16060 pedwarn (token->location, OPT_Wc__23_extensions,
16061 "%qD only valid in lambda with %<-std=c++23%> or "
16062 "%<-std=gnu++23%>", ridpointers[token->keyword]);
16065 if (ds != ds_last)
16066 set_and_check_decl_spec_loc (decl_specs, ds, token);
16068 /* Constructors are a special case. The `S' in `S()' is not a
16069 decl-specifier; it is the beginning of the declarator. */
16070 constructor_p
16071 = (!found_decl_spec
16072 && constructor_possible_p
16073 && (cp_parser_constructor_declarator_p
16074 (parser, flags, decl_spec_seq_has_spec_p (decl_specs,
16075 ds_friend))));
16077 /* If we don't have a DECL_SPEC yet, then we must be looking at
16078 a type-specifier. */
16079 if (!found_decl_spec && !constructor_p)
16081 int decl_spec_declares_class_or_enum;
16082 bool is_cv_qualifier;
16083 tree type_spec;
16085 if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
16086 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
16088 type_spec
16089 = cp_parser_type_specifier (parser, flags,
16090 decl_specs,
16091 /*is_declaration=*/true,
16092 &decl_spec_declares_class_or_enum,
16093 &is_cv_qualifier);
16094 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
16096 /* If this type-specifier referenced a user-defined type
16097 (a typedef, class-name, etc.), then we can't allow any
16098 more such type-specifiers henceforth.
16100 [dcl.spec]
16102 The longest sequence of decl-specifiers that could
16103 possibly be a type name is taken as the
16104 decl-specifier-seq of a declaration. The sequence shall
16105 be self-consistent as described below.
16107 [dcl.type]
16109 As a general rule, at most one type-specifier is allowed
16110 in the complete decl-specifier-seq of a declaration. The
16111 only exceptions are the following:
16113 -- const or volatile can be combined with any other
16114 type-specifier.
16116 -- signed or unsigned can be combined with char, long,
16117 short, or int.
16119 -- ..
16121 Example:
16123 typedef char* Pc;
16124 void g (const int Pc);
16126 Here, Pc is *not* part of the decl-specifier seq; it's
16127 the declarator. Therefore, once we see a type-specifier
16128 (other than a cv-qualifier), we forbid any additional
16129 user-defined types. We *do* still allow things like `int
16130 int' to be considered a decl-specifier-seq, and issue the
16131 error message later. */
16132 if (type_spec && !is_cv_qualifier)
16133 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
16134 /* A constructor declarator cannot follow a type-specifier. */
16135 if (type_spec)
16137 constructor_possible_p = false;
16138 found_decl_spec = true;
16139 if (!is_cv_qualifier)
16140 decl_specs->any_type_specifiers_p = true;
16142 if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) != 0)
16143 error_at (token->location, "type-specifier invalid in lambda");
16147 /* If we still do not have a DECL_SPEC, then there are no more
16148 decl-specifiers. */
16149 if (!found_decl_spec)
16150 break;
16152 if (decl_specs->std_attributes)
16154 error_at (decl_specs->locations[ds_std_attribute],
16155 "standard attributes in middle of decl-specifiers");
16156 inform (decl_specs->locations[ds_std_attribute],
16157 "standard attributes must precede the decl-specifiers to "
16158 "apply to the declaration, or follow them to apply to "
16159 "the type");
16162 decl_specs->any_specifiers_p = true;
16163 /* After we see one decl-specifier, further decl-specifiers are
16164 always optional. */
16165 flags |= CP_PARSER_FLAGS_OPTIONAL;
16168 /* Don't allow a friend specifier with a class definition. */
16169 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
16170 && (*declares_class_or_enum & 2))
16171 error_at (decl_specs->locations[ds_friend],
16172 "class definition may not be declared a friend");
16175 /* Parse an (optional) storage-class-specifier.
16177 storage-class-specifier:
16178 auto
16179 register
16180 static
16181 extern
16182 mutable
16184 GNU Extension:
16186 storage-class-specifier:
16187 thread
16189 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
16191 static tree
16192 cp_parser_storage_class_specifier_opt (cp_parser* parser)
16194 switch (cp_lexer_peek_token (parser->lexer)->keyword)
16196 case RID_AUTO:
16197 if (cxx_dialect != cxx98)
16198 return NULL_TREE;
16199 /* Fall through for C++98. */
16200 gcc_fallthrough ();
16202 case RID_REGISTER:
16203 case RID_STATIC:
16204 case RID_EXTERN:
16205 case RID_MUTABLE:
16206 case RID_THREAD:
16207 /* Consume the token. */
16208 return cp_lexer_consume_token (parser->lexer)->u.value;
16210 default:
16211 return NULL_TREE;
16215 /* Parse an (optional) function-specifier.
16217 function-specifier:
16218 inline
16219 virtual
16220 explicit
16222 C++20 Extension:
16223 explicit(constant-expression)
16225 Returns an IDENTIFIER_NODE corresponding to the keyword used.
16226 Updates DECL_SPECS, if it is non-NULL. */
16228 static tree
16229 cp_parser_function_specifier_opt (cp_parser* parser,
16230 cp_decl_specifier_seq *decl_specs)
16232 cp_token *token = cp_lexer_peek_token (parser->lexer);
16233 switch (token->keyword)
16235 case RID_INLINE:
16236 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
16237 break;
16239 case RID_VIRTUAL:
16240 /* 14.5.2.3 [temp.mem]
16242 A member function template shall not be virtual. */
16243 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
16244 && current_class_type)
16245 error_at (token->location, "templates may not be %<virtual%>");
16246 else
16247 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
16248 break;
16250 case RID_EXPLICIT:
16252 tree id = cp_lexer_consume_token (parser->lexer)->u.value;
16253 /* If we see '(', it's C++20 explicit(bool). */
16254 tree expr;
16255 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16257 matching_parens parens;
16258 parens.consume_open (parser);
16260 /* New types are not allowed in an explicit-specifier. */
16261 const char *saved_message
16262 = parser->type_definition_forbidden_message;
16263 parser->type_definition_forbidden_message
16264 = G_("types may not be defined in explicit-specifier");
16266 if (cxx_dialect < cxx20)
16267 pedwarn (token->location, OPT_Wc__20_extensions,
16268 "%<explicit(bool)%> only available with %<-std=c++20%> "
16269 "or %<-std=gnu++20%>");
16271 /* Parse the constant-expression. */
16272 expr = cp_parser_constant_expression (parser);
16274 /* Restore the saved message. */
16275 parser->type_definition_forbidden_message = saved_message;
16276 parens.require_close (parser);
16278 else
16279 /* The explicit-specifier explicit without a constant-expression is
16280 equivalent to the explicit-specifier explicit(true). */
16281 expr = boolean_true_node;
16283 /* [dcl.fct.spec]
16284 "the constant-expression, if supplied, shall be a contextually
16285 converted constant expression of type bool." */
16286 expr = build_explicit_specifier (expr, tf_warning_or_error);
16287 /* We could evaluate it -- mark the decl as appropriate. */
16288 if (expr == boolean_true_node)
16289 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
16290 else if (expr == boolean_false_node)
16291 /* Don't mark the decl as explicit. */;
16292 else if (decl_specs)
16293 /* The expression was value-dependent. Remember it so that we can
16294 substitute it later. */
16295 decl_specs->explicit_specifier = expr;
16296 return id;
16299 default:
16300 return NULL_TREE;
16303 /* Consume the token. */
16304 return cp_lexer_consume_token (parser->lexer)->u.value;
16307 /* Parse a linkage-specification.
16309 linkage-specification:
16310 extern string-literal { declaration-seq [opt] }
16311 extern string-literal declaration */
16313 static void
16314 cp_parser_linkage_specification (cp_parser* parser, tree prefix_attr)
16316 /* Look for the `extern' keyword. */
16317 cp_token *extern_token
16318 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
16320 /* Look for the string-literal. */
16321 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
16322 tree linkage = cp_parser_string_literal (parser, /*translate=*/false,
16323 /*wide_ok=*/false);
16325 /* Transform the literal into an identifier. If the literal is a
16326 wide-character string, or contains embedded NULs, then we can't
16327 handle it as the user wants. */
16328 if (linkage == error_mark_node
16329 || strlen (TREE_STRING_POINTER (linkage))
16330 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
16332 cp_parser_error (parser, "invalid linkage-specification");
16333 /* Assume C++ linkage. */
16334 linkage = lang_name_cplusplus;
16336 else
16337 linkage = get_identifier (TREE_STRING_POINTER (linkage));
16339 /* We're now using the new linkage. */
16340 unsigned saved_module = module_kind;
16341 module_kind &= ~MK_ATTACH;
16342 push_lang_context (linkage);
16344 /* Preserve the location of the innermost linkage specification,
16345 tracking the locations of nested specifications via a local. */
16346 location_t saved_location
16347 = parser->innermost_linkage_specification_location;
16348 /* Construct a location ranging from the start of the "extern" to
16349 the end of the string-literal, with the caret at the start, e.g.:
16350 extern "C" {
16351 ^~~~~~~~~~
16353 parser->innermost_linkage_specification_location
16354 = make_location (extern_token->location,
16355 extern_token->location,
16356 get_finish (string_token->location));
16358 /* If the next token is a `{', then we're using the first
16359 production. */
16360 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16362 cp_ensure_no_omp_declare_simd (parser);
16363 cp_ensure_no_oacc_routine (parser);
16365 /* Consume the `{' token. */
16366 matching_braces braces;
16367 braces.consume_open (parser);
16368 /* Parse the declarations. */
16369 cp_parser_declaration_seq_opt (parser);
16370 /* Look for the closing `}'. */
16371 braces.require_close (parser);
16373 /* Otherwise, there's just one declaration. */
16374 else
16376 bool saved_in_unbraced_linkage_specification_p;
16378 saved_in_unbraced_linkage_specification_p
16379 = parser->in_unbraced_linkage_specification_p;
16380 parser->in_unbraced_linkage_specification_p = true;
16381 cp_parser_declaration (parser, prefix_attr);
16382 parser->in_unbraced_linkage_specification_p
16383 = saved_in_unbraced_linkage_specification_p;
16386 /* We're done with the linkage-specification. */
16387 pop_lang_context ();
16388 module_kind = saved_module;
16390 /* Restore location of parent linkage specification, if any. */
16391 parser->innermost_linkage_specification_location = saved_location;
16394 /* Parse a static_assert-declaration.
16396 static_assert-declaration:
16397 static_assert ( constant-expression , string-literal ) ;
16398 static_assert ( constant-expression ) ; (C++17)
16400 If MEMBER_P, this static_assert is a class member. */
16402 static void
16403 cp_parser_static_assert(cp_parser *parser, bool member_p)
16405 cp_expr condition;
16406 location_t token_loc;
16407 tree message;
16408 bool dummy;
16410 /* Peek at the `static_assert' token so we can keep track of exactly
16411 where the static assertion started. */
16412 token_loc = cp_lexer_peek_token (parser->lexer)->location;
16414 /* Look for the `static_assert' keyword. */
16415 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
16416 RT_STATIC_ASSERT))
16417 return;
16419 /* We know we are in a static assertion; commit to any tentative
16420 parse. */
16421 if (cp_parser_parsing_tentatively (parser))
16422 cp_parser_commit_to_tentative_parse (parser);
16424 /* Parse the `(' starting the static assertion condition. */
16425 matching_parens parens;
16426 parens.require_open (parser);
16428 /* Parse the constant-expression. Allow a non-constant expression
16429 here in order to give better diagnostics in finish_static_assert. */
16430 condition =
16431 cp_parser_constant_expression (parser,
16432 /*allow_non_constant_p=*/true,
16433 /*non_constant_p=*/&dummy);
16435 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
16437 if (pedantic && cxx_dialect < cxx17)
16438 pedwarn (input_location, OPT_Wc__17_extensions,
16439 "%<static_assert%> without a message "
16440 "only available with %<-std=c++17%> or %<-std=gnu++17%>");
16441 /* Eat the ')' */
16442 cp_lexer_consume_token (parser->lexer);
16443 message = build_string (1, "");
16444 TREE_TYPE (message) = char_array_type_node;
16445 fix_string_type (message);
16447 else
16449 /* Parse the separating `,'. */
16450 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
16452 /* Parse the string-literal message. */
16453 message = cp_parser_string_literal (parser, /*translate=*/false,
16454 /*wide_ok=*/true);
16456 /* A `)' completes the static assertion. */
16457 if (!parens.require_close (parser))
16458 cp_parser_skip_to_closing_parenthesis (parser,
16459 /*recovering=*/true,
16460 /*or_comma=*/false,
16461 /*consume_paren=*/true);
16464 /* A semicolon terminates the declaration. */
16465 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16467 /* Get the location for the static assertion. Use that of the
16468 condition if available, otherwise, use that of the "static_assert"
16469 token. */
16470 location_t assert_loc = condition.get_location ();
16471 if (assert_loc == UNKNOWN_LOCATION)
16472 assert_loc = token_loc;
16474 /* Complete the static assertion, which may mean either processing
16475 the static assert now or saving it for template instantiation. */
16476 finish_static_assert (condition, message, assert_loc, member_p,
16477 /*show_expr_p=*/false);
16480 /* Parse the expression in decltype ( expression ). */
16482 static tree
16483 cp_parser_decltype_expr (cp_parser *parser,
16484 bool &id_expression_or_member_access_p)
16486 cp_token *id_expr_start_token;
16487 tree expr;
16489 /* First, try parsing an id-expression. */
16490 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
16491 cp_parser_parse_tentatively (parser);
16492 expr = cp_parser_id_expression (parser,
16493 /*template_keyword_p=*/false,
16494 /*check_dependency_p=*/true,
16495 /*template_p=*/NULL,
16496 /*declarator_p=*/false,
16497 /*optional_p=*/false);
16499 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
16501 bool non_integral_constant_expression_p = false;
16502 tree id_expression = expr;
16503 cp_id_kind idk;
16504 const char *error_msg;
16506 if (identifier_p (expr))
16507 /* Lookup the name we got back from the id-expression. */
16508 expr = cp_parser_lookup_name_simple (parser, expr,
16509 id_expr_start_token->location);
16511 if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
16512 /* A template without args is not a complete id-expression. */
16513 expr = error_mark_node;
16515 if (expr
16516 && expr != error_mark_node
16517 && TREE_CODE (expr) != TYPE_DECL
16518 && (TREE_CODE (expr) != BIT_NOT_EXPR
16519 || !TYPE_P (TREE_OPERAND (expr, 0)))
16520 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
16522 /* Complete lookup of the id-expression. */
16523 expr = (finish_id_expression
16524 (id_expression, expr, parser->scope, &idk,
16525 /*integral_constant_expression_p=*/false,
16526 /*allow_non_integral_constant_expression_p=*/true,
16527 &non_integral_constant_expression_p,
16528 /*template_p=*/false,
16529 /*done=*/true,
16530 /*address_p=*/false,
16531 /*template_arg_p=*/false,
16532 &error_msg,
16533 id_expr_start_token->location));
16535 if (expr == error_mark_node)
16536 /* We found an id-expression, but it was something that we
16537 should not have found. This is an error, not something
16538 we can recover from, so note that we found an
16539 id-expression and we'll recover as gracefully as
16540 possible. */
16541 id_expression_or_member_access_p = true;
16544 if (expr
16545 && expr != error_mark_node
16546 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
16547 /* We have an id-expression. */
16548 id_expression_or_member_access_p = true;
16551 if (!id_expression_or_member_access_p)
16553 /* Abort the id-expression parse. */
16554 cp_parser_abort_tentative_parse (parser);
16556 /* Parsing tentatively, again. */
16557 cp_parser_parse_tentatively (parser);
16559 /* Parse a class member access. */
16560 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
16561 /*cast_p=*/false, /*decltype*/true,
16562 /*member_access_only_p=*/true, NULL);
16564 if (expr
16565 && expr != error_mark_node
16566 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
16567 /* We have an id-expression. */
16568 id_expression_or_member_access_p = true;
16571 if (id_expression_or_member_access_p)
16572 /* We have parsed the complete id-expression or member access. */
16573 cp_parser_parse_definitely (parser);
16574 else
16576 /* Abort our attempt to parse an id-expression or member access
16577 expression. */
16578 cp_parser_abort_tentative_parse (parser);
16580 /* Parse a full expression. */
16581 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
16582 /*decltype_p=*/true);
16585 return expr;
16588 /* Parse a `decltype' type. Returns the type.
16590 decltype-specifier:
16591 decltype ( expression )
16592 C++14:
16593 decltype ( auto ) */
16595 static tree
16596 cp_parser_decltype (cp_parser *parser)
16598 bool id_expression_or_member_access_p = false;
16599 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
16601 if (start_token->type == CPP_DECLTYPE)
16603 /* Already parsed. */
16604 cp_lexer_consume_token (parser->lexer);
16605 return saved_checks_value (start_token->u.tree_check_value);
16608 /* Look for the `decltype' token. */
16609 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
16610 return error_mark_node;
16612 /* Parse the opening `('. */
16613 matching_parens parens;
16614 if (!parens.require_open (parser))
16615 return error_mark_node;
16617 /* Since we're going to preserve any side-effects from this parse, set up a
16618 firewall to protect our callers from cp_parser_commit_to_tentative_parse
16619 in the expression. */
16620 tentative_firewall firewall (parser);
16622 /* If in_declarator_p, a reparse as an expression might succeed (60361).
16623 Otherwise, commit now for better diagnostics. */
16624 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
16625 && !parser->in_declarator_p)
16626 cp_parser_commit_to_topmost_tentative_parse (parser);
16628 push_deferring_access_checks (dk_deferred);
16630 tree expr = NULL_TREE;
16632 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO)
16633 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
16635 /* decltype (auto) */
16636 cp_lexer_consume_token (parser->lexer);
16637 if (cxx_dialect < cxx14)
16639 error_at (start_token->location,
16640 "%<decltype(auto)%> type specifier only available with "
16641 "%<-std=c++14%> or %<-std=gnu++14%>");
16642 expr = error_mark_node;
16645 else
16647 /* decltype (expression) */
16649 /* Types cannot be defined in a `decltype' expression. Save away the
16650 old message and set the new one. */
16651 const char *saved_message = parser->type_definition_forbidden_message;
16652 parser->type_definition_forbidden_message
16653 = G_("types may not be defined in %<decltype%> expressions");
16655 /* The restrictions on constant-expressions do not apply inside
16656 decltype expressions. */
16657 bool saved_integral_constant_expression_p
16658 = parser->integral_constant_expression_p;
16659 bool saved_non_integral_constant_expression_p
16660 = parser->non_integral_constant_expression_p;
16661 parser->integral_constant_expression_p = false;
16663 /* Within a parenthesized expression, a `>' token is always
16664 the greater-than operator. */
16665 bool saved_greater_than_is_operator_p
16666 = parser->greater_than_is_operator_p;
16667 parser->greater_than_is_operator_p = true;
16669 /* Don't synthesize an implicit template type parameter here. This
16670 could happen with C++23 code like
16672 void f(decltype(new auto{0}));
16674 where we want to deduce the auto right away so that the parameter
16675 is of type 'int *'. */
16676 auto cleanup = make_temp_override
16677 (parser->auto_is_implicit_function_template_parm_p, false);
16679 /* Do not actually evaluate the expression. */
16680 ++cp_unevaluated_operand;
16682 /* Do not warn about problems with the expression. */
16683 ++c_inhibit_evaluation_warnings;
16685 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
16686 STRIP_ANY_LOCATION_WRAPPER (expr);
16688 /* Go back to evaluating expressions. */
16689 --cp_unevaluated_operand;
16690 --c_inhibit_evaluation_warnings;
16692 /* The `>' token might be the end of a template-id or
16693 template-parameter-list now. */
16694 parser->greater_than_is_operator_p
16695 = saved_greater_than_is_operator_p;
16697 /* Restore the old message and the integral constant expression
16698 flags. */
16699 parser->type_definition_forbidden_message = saved_message;
16700 parser->integral_constant_expression_p
16701 = saved_integral_constant_expression_p;
16702 parser->non_integral_constant_expression_p
16703 = saved_non_integral_constant_expression_p;
16706 /* Parse to the closing `)'. */
16707 if (expr == error_mark_node || !parens.require_close (parser))
16709 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16710 /*consume_paren=*/true);
16711 expr = error_mark_node;
16714 /* If we got a parse error while tentative, bail out now. */
16715 if (cp_parser_error_occurred (parser))
16717 pop_deferring_access_checks ();
16718 return error_mark_node;
16721 if (!expr)
16722 /* Build auto. */
16723 expr = make_decltype_auto ();
16724 else
16725 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
16726 tf_warning_or_error);
16728 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
16729 it again. */
16730 start_token->type = CPP_DECLTYPE;
16731 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
16732 start_token->tree_check_p = true;
16733 start_token->u.tree_check_value->value = expr;
16734 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
16735 start_token->keyword = RID_MAX;
16737 location_t loc = start_token->location;
16738 loc = make_location (loc, loc, parser->lexer);
16739 start_token->location = loc;
16741 cp_lexer_purge_tokens_after (parser->lexer, start_token);
16743 pop_to_parent_deferring_access_checks ();
16745 return expr;
16748 /* Special member functions [gram.special] */
16750 /* Parse a conversion-function-id.
16752 conversion-function-id:
16753 operator conversion-type-id
16755 Returns an IDENTIFIER_NODE representing the operator. */
16757 static tree
16758 cp_parser_conversion_function_id (cp_parser* parser)
16760 tree type;
16761 tree saved_scope;
16762 tree saved_qualifying_scope;
16763 tree saved_object_scope;
16764 tree pushed_scope = NULL_TREE;
16766 /* Look for the `operator' token. */
16767 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
16768 return error_mark_node;
16769 /* When we parse the conversion-type-id, the current scope will be
16770 reset. However, we need that information in able to look up the
16771 conversion function later, so we save it here. */
16772 saved_scope = parser->scope;
16773 saved_qualifying_scope = parser->qualifying_scope;
16774 saved_object_scope = parser->object_scope;
16775 /* We must enter the scope of the class so that the names of
16776 entities declared within the class are available in the
16777 conversion-type-id. For example, consider:
16779 struct S {
16780 typedef int I;
16781 operator I();
16784 S::operator I() { ... }
16786 In order to see that `I' is a type-name in the definition, we
16787 must be in the scope of `S'. */
16788 if (saved_scope)
16789 pushed_scope = push_scope (saved_scope);
16790 /* Parse the conversion-type-id. */
16791 type = cp_parser_conversion_type_id (parser);
16792 /* Leave the scope of the class, if any. */
16793 if (pushed_scope)
16794 pop_scope (pushed_scope);
16795 /* Restore the saved scope. */
16796 parser->scope = saved_scope;
16797 parser->qualifying_scope = saved_qualifying_scope;
16798 parser->object_scope = saved_object_scope;
16799 /* If the TYPE is invalid, indicate failure. */
16800 if (type == error_mark_node)
16801 return error_mark_node;
16802 return make_conv_op_name (type);
16805 /* Parse a conversion-type-id:
16807 conversion-type-id:
16808 type-specifier-seq conversion-declarator [opt]
16810 Returns the TYPE specified. */
16812 static tree
16813 cp_parser_conversion_type_id (cp_parser* parser)
16815 tree attributes;
16816 cp_decl_specifier_seq type_specifiers;
16817 cp_declarator *declarator;
16818 tree type_specified;
16819 const char *saved_message;
16821 /* Parse the attributes. */
16822 attributes = cp_parser_attributes_opt (parser);
16824 saved_message = parser->type_definition_forbidden_message;
16825 parser->type_definition_forbidden_message
16826 = G_("types may not be defined in a conversion-type-id");
16828 /* Parse the type-specifiers. DR 2413 clarifies that `typename' is
16829 optional in conversion-type-id. */
16830 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
16831 /*is_declaration=*/false,
16832 /*is_trailing_return=*/false,
16833 &type_specifiers);
16835 parser->type_definition_forbidden_message = saved_message;
16837 /* If that didn't work, stop. */
16838 if (type_specifiers.type == error_mark_node)
16839 return error_mark_node;
16840 /* Parse the conversion-declarator. */
16841 declarator = cp_parser_conversion_declarator_opt (parser);
16843 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
16844 /*initialized=*/0, &attributes);
16845 if (attributes)
16846 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
16848 /* Don't give this error when parsing tentatively. This happens to
16849 work because we always parse this definitively once. */
16850 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
16851 && type_uses_auto (type_specified))
16853 if (cxx_dialect < cxx14)
16855 error ("invalid use of %<auto%> in conversion operator");
16856 return error_mark_node;
16858 else if (template_parm_scope_p ())
16859 warning (0, "use of %<auto%> in member template "
16860 "conversion operator can never be deduced");
16863 return type_specified;
16866 /* Parse an (optional) conversion-declarator.
16868 conversion-declarator:
16869 ptr-operator conversion-declarator [opt]
16873 static cp_declarator *
16874 cp_parser_conversion_declarator_opt (cp_parser* parser)
16876 enum tree_code code;
16877 tree class_type, std_attributes = NULL_TREE;
16878 cp_cv_quals cv_quals;
16880 /* We don't know if there's a ptr-operator next, or not. */
16881 cp_parser_parse_tentatively (parser);
16882 /* Try the ptr-operator. */
16883 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
16884 &std_attributes);
16885 /* If it worked, look for more conversion-declarators. */
16886 if (cp_parser_parse_definitely (parser))
16888 cp_declarator *declarator;
16890 /* Parse another optional declarator. */
16891 declarator = cp_parser_conversion_declarator_opt (parser);
16893 declarator = cp_parser_make_indirect_declarator
16894 (code, class_type, cv_quals, declarator, std_attributes);
16896 return declarator;
16899 return NULL;
16902 /* Parse an (optional) ctor-initializer.
16904 ctor-initializer:
16905 : mem-initializer-list */
16907 static void
16908 cp_parser_ctor_initializer_opt (cp_parser* parser)
16910 /* If the next token is not a `:', then there is no
16911 ctor-initializer. */
16912 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16914 /* Do default initialization of any bases and members. */
16915 if (DECL_CONSTRUCTOR_P (current_function_decl))
16916 finish_mem_initializers (NULL_TREE);
16917 return;
16920 /* Consume the `:' token. */
16921 cp_lexer_consume_token (parser->lexer);
16922 /* And the mem-initializer-list. */
16923 cp_parser_mem_initializer_list (parser);
16926 /* Parse a mem-initializer-list.
16928 mem-initializer-list:
16929 mem-initializer ... [opt]
16930 mem-initializer ... [opt] , mem-initializer-list */
16932 static void
16933 cp_parser_mem_initializer_list (cp_parser* parser)
16935 tree mem_initializer_list = NULL_TREE;
16936 tree target_ctor = error_mark_node;
16937 cp_token *token = cp_lexer_peek_token (parser->lexer);
16939 /* Let the semantic analysis code know that we are starting the
16940 mem-initializer-list. */
16941 if (!DECL_CONSTRUCTOR_P (current_function_decl))
16942 error_at (token->location,
16943 "only constructors take member initializers");
16945 /* Loop through the list. */
16946 while (true)
16948 tree mem_initializer;
16950 token = cp_lexer_peek_token (parser->lexer);
16951 /* Parse the mem-initializer. */
16952 mem_initializer = cp_parser_mem_initializer (parser);
16953 /* If the next token is a `...', we're expanding member initializers. */
16954 bool ellipsis = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
16955 if (ellipsis
16956 || (mem_initializer != error_mark_node
16957 && check_for_bare_parameter_packs (TREE_PURPOSE
16958 (mem_initializer))))
16960 /* Consume the `...'. */
16961 if (ellipsis)
16962 cp_lexer_consume_token (parser->lexer);
16964 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
16965 can be expanded but members cannot. */
16966 if (mem_initializer != error_mark_node
16967 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
16969 error_at (token->location,
16970 "cannot expand initializer for member %qD",
16971 TREE_PURPOSE (mem_initializer));
16972 mem_initializer = error_mark_node;
16975 /* Construct the pack expansion type. */
16976 if (mem_initializer != error_mark_node)
16977 mem_initializer = make_pack_expansion (mem_initializer);
16979 if (target_ctor != error_mark_node
16980 && mem_initializer != error_mark_node)
16982 error ("mem-initializer for %qD follows constructor delegation",
16983 TREE_PURPOSE (mem_initializer));
16984 mem_initializer = error_mark_node;
16986 /* Look for a target constructor. */
16987 if (mem_initializer != error_mark_node
16988 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
16989 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
16991 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
16992 if (mem_initializer_list)
16994 error ("constructor delegation follows mem-initializer for %qD",
16995 TREE_PURPOSE (mem_initializer_list));
16996 mem_initializer = error_mark_node;
16998 target_ctor = mem_initializer;
17000 /* Add it to the list, unless it was erroneous. */
17001 if (mem_initializer != error_mark_node)
17003 TREE_CHAIN (mem_initializer) = mem_initializer_list;
17004 mem_initializer_list = mem_initializer;
17006 /* If the next token is not a `,', we're done. */
17007 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17008 break;
17009 /* Consume the `,' token. */
17010 cp_lexer_consume_token (parser->lexer);
17013 /* Perform semantic analysis. */
17014 if (DECL_CONSTRUCTOR_P (current_function_decl))
17015 finish_mem_initializers (mem_initializer_list);
17018 /* Parse a mem-initializer.
17020 mem-initializer:
17021 mem-initializer-id ( expression-list [opt] )
17022 mem-initializer-id braced-init-list
17024 GNU extension:
17026 mem-initializer:
17027 ( expression-list [opt] )
17029 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
17030 class) or FIELD_DECL (for a non-static data member) to initialize;
17031 the TREE_VALUE is the expression-list. An empty initialization
17032 list is represented by void_list_node. */
17034 static tree
17035 cp_parser_mem_initializer (cp_parser* parser)
17037 tree mem_initializer_id;
17038 tree expression_list;
17039 tree member;
17040 cp_token *token = cp_lexer_peek_token (parser->lexer);
17042 /* Find out what is being initialized. */
17043 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17045 permerror (token->location,
17046 "anachronistic old-style base class initializer");
17047 mem_initializer_id = NULL_TREE;
17049 else
17051 mem_initializer_id = cp_parser_mem_initializer_id (parser);
17052 if (mem_initializer_id == error_mark_node)
17053 return mem_initializer_id;
17055 member = expand_member_init (mem_initializer_id);
17056 if (member && !DECL_P (member))
17057 in_base_initializer = 1;
17059 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17061 bool expr_non_constant_p;
17062 cp_lexer_set_source_position (parser->lexer);
17063 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17064 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
17065 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
17066 expression_list = build_tree_list (NULL_TREE, expression_list);
17068 else
17070 vec<tree, va_gc> *vec;
17071 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
17072 /*cast_p=*/false,
17073 /*allow_expansion_p=*/true,
17074 /*non_constant_p=*/NULL,
17075 /*close_paren_loc=*/NULL,
17076 /*wrap_locations_p=*/true);
17077 if (vec == NULL)
17078 return error_mark_node;
17079 expression_list = build_tree_list_vec (vec);
17080 release_tree_vector (vec);
17083 if (expression_list == error_mark_node)
17084 return error_mark_node;
17085 if (!expression_list)
17086 expression_list = void_type_node;
17088 in_base_initializer = 0;
17090 if (!member)
17091 return error_mark_node;
17092 tree node = build_tree_list (member, expression_list);
17094 /* We can't attach the source location of this initializer directly to
17095 the list node, so we instead attach it to a dummy EMPTY_CLASS_EXPR
17096 within the TREE_TYPE of the list node. */
17097 location_t loc
17098 = make_location (token->location, token->location, parser->lexer);
17099 tree dummy = build0 (EMPTY_CLASS_EXPR, NULL_TREE);
17100 SET_EXPR_LOCATION (dummy, loc);
17101 TREE_TYPE (node) = dummy;
17103 return node;
17106 /* Parse a mem-initializer-id.
17108 mem-initializer-id:
17109 :: [opt] nested-name-specifier [opt] class-name
17110 decltype-specifier (C++11)
17111 identifier
17113 Returns a TYPE indicating the class to be initialized for the first
17114 production (and the second in C++11). Returns an IDENTIFIER_NODE
17115 indicating the data member to be initialized for the last production. */
17117 static tree
17118 cp_parser_mem_initializer_id (cp_parser* parser)
17120 bool global_scope_p;
17121 bool nested_name_specifier_p;
17122 bool template_p = false;
17123 tree id;
17125 cp_token *token = cp_lexer_peek_token (parser->lexer);
17127 /* `typename' is not allowed in this context ([temp.res]). */
17128 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
17130 error_at (token->location,
17131 "keyword %<typename%> not allowed in this context (a qualified "
17132 "member initializer is implicitly a type)");
17133 cp_lexer_consume_token (parser->lexer);
17135 /* Look for the optional `::' operator. */
17136 global_scope_p
17137 = (cp_parser_global_scope_opt (parser,
17138 /*current_scope_valid_p=*/false)
17139 != NULL_TREE);
17140 /* Look for the optional nested-name-specifier. The simplest way to
17141 implement:
17143 [temp.res]
17145 The keyword `typename' is not permitted in a base-specifier or
17146 mem-initializer; in these contexts a qualified name that
17147 depends on a template-parameter is implicitly assumed to be a
17148 type name.
17150 is to assume that we have seen the `typename' keyword at this
17151 point. */
17152 nested_name_specifier_p
17153 = (cp_parser_nested_name_specifier_opt (parser,
17154 /*typename_keyword_p=*/true,
17155 /*check_dependency_p=*/true,
17156 /*type_p=*/true,
17157 /*is_declaration=*/true)
17158 != NULL_TREE);
17159 if (nested_name_specifier_p)
17160 template_p = cp_parser_optional_template_keyword (parser);
17161 /* If there is a `::' operator or a nested-name-specifier, then we
17162 are definitely looking for a class-name. */
17163 if (global_scope_p || nested_name_specifier_p)
17164 return cp_parser_class_name (parser,
17165 /*typename_keyword_p=*/true,
17166 /*template_keyword_p=*/template_p,
17167 typename_type,
17168 /*check_dependency_p=*/true,
17169 /*class_head_p=*/false,
17170 /*is_declaration=*/true);
17171 /* Otherwise, we could also be looking for an ordinary identifier. */
17172 cp_parser_parse_tentatively (parser);
17173 if (cp_lexer_next_token_is_decltype (parser->lexer))
17174 /* Try a decltype-specifier. */
17175 id = cp_parser_decltype (parser);
17176 else
17177 /* Otherwise, try a class-name. */
17178 id = cp_parser_class_name (parser,
17179 /*typename_keyword_p=*/true,
17180 /*template_keyword_p=*/false,
17181 none_type,
17182 /*check_dependency_p=*/true,
17183 /*class_head_p=*/false,
17184 /*is_declaration=*/true);
17185 /* If we found one, we're done. */
17186 if (cp_parser_parse_definitely (parser))
17187 return id;
17188 /* Otherwise, look for an ordinary identifier. */
17189 return cp_parser_identifier (parser);
17192 /* Overloading [gram.over] */
17194 /* Parse an operator-function-id.
17196 operator-function-id:
17197 operator operator
17199 Returns an IDENTIFIER_NODE for the operator which is a
17200 human-readable spelling of the identifier, e.g., `operator +'. */
17202 static cp_expr
17203 cp_parser_operator_function_id (cp_parser* parser)
17205 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
17206 /* Look for the `operator' keyword. */
17207 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
17208 return error_mark_node;
17209 /* And then the name of the operator itself. */
17210 return cp_parser_operator (parser, start_loc);
17213 /* Return an identifier node for a user-defined literal operator.
17214 The suffix identifier is chained to the operator name identifier. */
17216 tree
17217 cp_literal_operator_id (const char* name)
17219 tree identifier;
17220 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
17221 + strlen (name) + 10);
17222 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
17223 identifier = get_identifier (buffer);
17224 XDELETEVEC (buffer);
17226 return identifier;
17229 /* Parse an operator.
17231 operator:
17232 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
17233 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
17234 || ++ -- , ->* -> () []
17236 GNU Extensions:
17238 operator:
17239 <? >? <?= >?=
17241 Returns an IDENTIFIER_NODE for the operator which is a
17242 human-readable spelling of the identifier, e.g., `operator +'. */
17244 static cp_expr
17245 cp_parser_operator (cp_parser* parser, location_t start_loc)
17247 tree id = NULL_TREE;
17248 cp_token *token;
17249 bool utf8 = false;
17251 /* Peek at the next token. */
17252 token = cp_lexer_peek_token (parser->lexer);
17254 location_t end_loc = token->location;
17256 /* Figure out which operator we have. */
17257 enum tree_code op = ERROR_MARK;
17258 bool assop = false;
17259 bool consumed = false;
17260 switch (token->type)
17262 case CPP_KEYWORD:
17264 /* The keyword should be either `new', `delete' or `co_await'. */
17265 if (token->keyword == RID_NEW)
17266 op = NEW_EXPR;
17267 else if (token->keyword == RID_DELETE)
17268 op = DELETE_EXPR;
17269 else if (token->keyword == RID_CO_AWAIT)
17270 op = CO_AWAIT_EXPR;
17271 else
17272 break;
17274 /* Consume the `new', `delete' or co_await token. */
17275 end_loc = cp_lexer_consume_token (parser->lexer)->location;
17277 /* Peek at the next token. */
17278 token = cp_lexer_peek_token (parser->lexer);
17279 /* If it's a `[' token then this is the array variant of the
17280 operator. */
17281 if (token->type == CPP_OPEN_SQUARE
17282 && op != CO_AWAIT_EXPR)
17284 /* Consume the `[' token. */
17285 cp_lexer_consume_token (parser->lexer);
17286 /* Look for the `]' token. */
17287 if (cp_token *close_token
17288 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
17289 end_loc = close_token->location;
17290 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
17292 consumed = true;
17293 break;
17296 case CPP_PLUS:
17297 op = PLUS_EXPR;
17298 break;
17300 case CPP_MINUS:
17301 op = MINUS_EXPR;
17302 break;
17304 case CPP_MULT:
17305 op = MULT_EXPR;
17306 break;
17308 case CPP_DIV:
17309 op = TRUNC_DIV_EXPR;
17310 break;
17312 case CPP_MOD:
17313 op = TRUNC_MOD_EXPR;
17314 break;
17316 case CPP_XOR:
17317 op = BIT_XOR_EXPR;
17318 break;
17320 case CPP_AND:
17321 op = BIT_AND_EXPR;
17322 break;
17324 case CPP_OR:
17325 op = BIT_IOR_EXPR;
17326 break;
17328 case CPP_COMPL:
17329 op = BIT_NOT_EXPR;
17330 break;
17332 case CPP_NOT:
17333 op = TRUTH_NOT_EXPR;
17334 break;
17336 case CPP_EQ:
17337 assop = true;
17338 op = NOP_EXPR;
17339 break;
17341 case CPP_LESS:
17342 op = LT_EXPR;
17343 break;
17345 case CPP_GREATER:
17346 op = GT_EXPR;
17347 break;
17349 case CPP_PLUS_EQ:
17350 assop = true;
17351 op = PLUS_EXPR;
17352 break;
17354 case CPP_MINUS_EQ:
17355 assop = true;
17356 op = MINUS_EXPR;
17357 break;
17359 case CPP_MULT_EQ:
17360 assop = true;
17361 op = MULT_EXPR;
17362 break;
17364 case CPP_DIV_EQ:
17365 assop = true;
17366 op = TRUNC_DIV_EXPR;
17367 break;
17369 case CPP_MOD_EQ:
17370 assop = true;
17371 op = TRUNC_MOD_EXPR;
17372 break;
17374 case CPP_XOR_EQ:
17375 assop = true;
17376 op = BIT_XOR_EXPR;
17377 break;
17379 case CPP_AND_EQ:
17380 assop = true;
17381 op = BIT_AND_EXPR;
17382 break;
17384 case CPP_OR_EQ:
17385 assop = true;
17386 op = BIT_IOR_EXPR;
17387 break;
17389 case CPP_LSHIFT:
17390 op = LSHIFT_EXPR;
17391 break;
17393 case CPP_RSHIFT:
17394 op = RSHIFT_EXPR;
17395 break;
17397 case CPP_LSHIFT_EQ:
17398 assop = true;
17399 op = LSHIFT_EXPR;
17400 break;
17402 case CPP_RSHIFT_EQ:
17403 assop = true;
17404 op = RSHIFT_EXPR;
17405 break;
17407 case CPP_EQ_EQ:
17408 op = EQ_EXPR;
17409 break;
17411 case CPP_NOT_EQ:
17412 op = NE_EXPR;
17413 break;
17415 case CPP_LESS_EQ:
17416 op = LE_EXPR;
17417 break;
17419 case CPP_GREATER_EQ:
17420 op = GE_EXPR;
17421 break;
17423 case CPP_SPACESHIP:
17424 op = SPACESHIP_EXPR;
17425 break;
17427 case CPP_AND_AND:
17428 op = TRUTH_ANDIF_EXPR;
17429 break;
17431 case CPP_OR_OR:
17432 op = TRUTH_ORIF_EXPR;
17433 break;
17435 case CPP_PLUS_PLUS:
17436 op = POSTINCREMENT_EXPR;
17437 break;
17439 case CPP_MINUS_MINUS:
17440 op = PREDECREMENT_EXPR;
17441 break;
17443 case CPP_COMMA:
17444 op = COMPOUND_EXPR;
17445 break;
17447 case CPP_DEREF_STAR:
17448 op = MEMBER_REF;
17449 break;
17451 case CPP_DEREF:
17452 op = COMPONENT_REF;
17453 break;
17455 case CPP_QUERY:
17456 op = COND_EXPR;
17457 /* Consume the `?'. */
17458 cp_lexer_consume_token (parser->lexer);
17459 /* Look for the matching `:'. */
17460 cp_parser_require (parser, CPP_COLON, RT_COLON);
17461 consumed = true;
17462 break;
17464 case CPP_OPEN_PAREN:
17466 /* Consume the `('. */
17467 matching_parens parens;
17468 parens.consume_open (parser);
17469 /* Look for the matching `)'. */
17470 token = parens.require_close (parser);
17471 if (token)
17472 end_loc = token->location;
17473 op = CALL_EXPR;
17474 consumed = true;
17475 break;
17478 case CPP_OPEN_SQUARE:
17479 /* Consume the `['. */
17480 cp_lexer_consume_token (parser->lexer);
17481 /* Look for the matching `]'. */
17482 token = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
17483 if (token)
17484 end_loc = token->location;
17485 op = ARRAY_REF;
17486 consumed = true;
17487 break;
17489 case CPP_UTF8STRING:
17490 case CPP_UTF8STRING_USERDEF:
17491 utf8 = true;
17492 /* FALLTHRU */
17493 case CPP_STRING:
17494 case CPP_WSTRING:
17495 case CPP_STRING16:
17496 case CPP_STRING32:
17497 case CPP_STRING_USERDEF:
17498 case CPP_WSTRING_USERDEF:
17499 case CPP_STRING16_USERDEF:
17500 case CPP_STRING32_USERDEF:
17502 tree string_tree;
17503 int sz, len;
17505 if (cxx_dialect == cxx98)
17506 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
17508 /* Consume the string. */
17509 cp_expr str = cp_parser_userdef_string_literal (parser,
17510 /*lookup_udlit=*/false);
17511 if (str == error_mark_node)
17512 return error_mark_node;
17513 else if (TREE_CODE (str) == USERDEF_LITERAL)
17515 string_tree = USERDEF_LITERAL_VALUE (str.get_value ());
17516 id = USERDEF_LITERAL_SUFFIX_ID (str.get_value ());
17517 end_loc = str.get_location ();
17519 else
17521 string_tree = str;
17522 /* Look for the suffix identifier. */
17523 token = cp_lexer_peek_token (parser->lexer);
17524 if (token->type == CPP_NAME)
17526 id = cp_parser_identifier (parser);
17527 end_loc = token->location;
17529 else if (token->type == CPP_KEYWORD)
17531 error ("unexpected keyword;"
17532 " remove space between quotes and suffix identifier");
17533 return error_mark_node;
17535 else
17537 error ("expected suffix identifier");
17538 return error_mark_node;
17541 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
17542 (TREE_TYPE (TREE_TYPE (string_tree))));
17543 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
17544 if (len != 0)
17546 error ("expected empty string after %<operator%> keyword");
17547 return error_mark_node;
17549 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
17550 != char_type_node)
17552 error ("invalid encoding prefix in literal operator");
17553 return error_mark_node;
17555 if (id != error_mark_node)
17557 const char *name = IDENTIFIER_POINTER (id);
17558 id = cp_literal_operator_id (name);
17560 /* Generate a location of the form:
17561 "" _suffix_identifier
17562 ^~~~~~~~~~~~~~~~~~~~~
17563 with caret == start at the start token, finish at the end of the
17564 suffix identifier. */
17565 location_t combined_loc
17566 = make_location (start_loc, start_loc, parser->lexer);
17567 return cp_expr (id, combined_loc);
17570 default:
17571 /* Anything else is an error. */
17572 break;
17575 /* If we have selected an identifier, we need to consume the
17576 operator token. */
17577 if (op != ERROR_MARK)
17579 id = ovl_op_identifier (assop, op);
17580 if (!consumed)
17581 cp_lexer_consume_token (parser->lexer);
17583 /* Otherwise, no valid operator name was present. */
17584 else
17586 cp_parser_error (parser, "expected operator");
17587 id = error_mark_node;
17590 start_loc = make_location (start_loc, start_loc, get_finish (end_loc));
17591 return cp_expr (id, start_loc);
17594 /* Parse a template-declaration.
17596 template-declaration:
17597 export [opt] template < template-parameter-list > declaration
17599 If MEMBER_P is TRUE, this template-declaration occurs within a
17600 class-specifier.
17602 The grammar rule given by the standard isn't correct. What
17603 is really meant is:
17605 template-declaration:
17606 export [opt] template-parameter-list-seq
17607 decl-specifier-seq [opt] init-declarator [opt] ;
17608 export [opt] template-parameter-list-seq
17609 function-definition
17611 template-parameter-list-seq:
17612 template-parameter-list-seq [opt]
17613 template < template-parameter-list >
17615 Concept Extensions:
17617 template-parameter-list-seq:
17618 template < template-parameter-list > requires-clause [opt]
17620 requires-clause:
17621 requires logical-or-expression */
17623 static void
17624 cp_parser_template_declaration (cp_parser* parser, bool member_p)
17626 /* Check for `export'. */
17627 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
17629 /* Consume the `export' token. */
17630 cp_lexer_consume_token (parser->lexer);
17631 /* Warn that this use of export is deprecated. */
17632 if (cxx_dialect < cxx11)
17633 warning (0, "keyword %<export%> not implemented, and will be ignored");
17634 else if (cxx_dialect < cxx20)
17635 warning (0, "keyword %<export%> is deprecated, and is ignored");
17636 else
17637 warning (0, "keyword %<export%> is enabled with %<-fmodules-ts%>");
17640 cp_parser_template_declaration_after_export (parser, member_p);
17643 /* Parse a template-parameter-list.
17645 template-parameter-list:
17646 template-parameter
17647 template-parameter-list , template-parameter
17649 Returns a TREE_LIST. Each node represents a template parameter.
17650 The nodes are connected via their TREE_CHAINs. */
17652 static tree
17653 cp_parser_template_parameter_list (cp_parser* parser)
17655 tree parameter_list = NULL_TREE;
17657 /* Don't create wrapper nodes within a template-parameter-list,
17658 since we don't want to have different types based on the
17659 spelling location of constants and decls within them. */
17660 auto_suppress_location_wrappers sentinel;
17662 begin_template_parm_list ();
17664 /* The loop below parses the template parms. We first need to know
17665 the total number of template parms to be able to compute proper
17666 canonical types of each dependent type. So after the loop, when
17667 we know the total number of template parms,
17668 end_template_parm_list computes the proper canonical types and
17669 fixes up the dependent types accordingly. */
17670 while (true)
17672 tree parameter;
17673 bool is_non_type;
17674 bool is_parameter_pack;
17675 location_t parm_loc;
17677 /* Parse the template-parameter. */
17678 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
17679 parameter = cp_parser_template_parameter (parser,
17680 &is_non_type,
17681 &is_parameter_pack);
17682 /* Add it to the list. */
17683 if (parameter != error_mark_node)
17684 parameter_list = process_template_parm (parameter_list,
17685 parm_loc,
17686 parameter,
17687 is_non_type,
17688 is_parameter_pack);
17689 else
17691 tree err_parm = build_tree_list (parameter, parameter);
17692 parameter_list = chainon (parameter_list, err_parm);
17695 /* If the next token is not a `,', we're done. */
17696 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17697 break;
17698 /* Otherwise, consume the `,' token. */
17699 cp_lexer_consume_token (parser->lexer);
17702 return end_template_parm_list (parameter_list);
17705 /* Parse a introduction-list.
17707 introduction-list:
17708 introduced-parameter
17709 introduction-list , introduced-parameter
17711 introduced-parameter:
17712 ...[opt] identifier
17714 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
17715 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
17716 WILDCARD_DECL will also have DECL_NAME set and token location in
17717 DECL_SOURCE_LOCATION. */
17719 static tree
17720 cp_parser_introduction_list (cp_parser *parser)
17722 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
17724 while (true)
17726 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
17727 if (is_pack)
17728 cp_lexer_consume_token (parser->lexer);
17730 tree identifier = cp_parser_identifier (parser);
17731 if (identifier == error_mark_node)
17732 break;
17734 /* Build placeholder. */
17735 tree parm = build_nt (WILDCARD_DECL);
17736 DECL_SOURCE_LOCATION (parm)
17737 = cp_lexer_peek_token (parser->lexer)->location;
17738 DECL_NAME (parm) = identifier;
17739 WILDCARD_PACK_P (parm) = is_pack;
17740 vec_safe_push (introduction_vec, parm);
17742 /* If the next token is not a `,', we're done. */
17743 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17744 break;
17745 /* Otherwise, consume the `,' token. */
17746 cp_lexer_consume_token (parser->lexer);
17749 /* Convert the vec into a TREE_VEC. */
17750 tree introduction_list = make_tree_vec (introduction_vec->length ());
17751 unsigned int n;
17752 tree parm;
17753 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
17754 TREE_VEC_ELT (introduction_list, n) = parm;
17756 release_tree_vector (introduction_vec);
17757 return introduction_list;
17760 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
17761 is an abstract declarator. */
17763 static inline cp_declarator*
17764 get_id_declarator (cp_declarator *declarator)
17766 cp_declarator *d = declarator;
17767 while (d && d->kind != cdk_id)
17768 d = d->declarator;
17769 return d;
17772 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
17773 is an abstract declarator. */
17775 static inline tree
17776 get_unqualified_id (cp_declarator *declarator)
17778 declarator = get_id_declarator (declarator);
17779 if (declarator)
17780 return declarator->u.id.unqualified_name;
17781 else
17782 return NULL_TREE;
17785 /* Returns true if TYPE would declare a constrained constrained-parameter. */
17787 static inline bool
17788 is_constrained_parameter (tree type)
17790 return (type
17791 && TREE_CODE (type) == TYPE_DECL
17792 && CONSTRAINED_PARM_CONCEPT (type)
17793 && DECL_P (CONSTRAINED_PARM_CONCEPT (type)));
17796 /* Returns true if PARM declares a constrained-parameter. */
17798 static inline bool
17799 is_constrained_parameter (cp_parameter_declarator *parm)
17801 return is_constrained_parameter (parm->decl_specifiers.type);
17804 /* Check that the type parameter is only a declarator-id, and that its
17805 type is not cv-qualified. */
17807 bool
17808 cp_parser_check_constrained_type_parm (cp_parser *parser,
17809 cp_parameter_declarator *parm)
17811 if (!parm->declarator)
17812 return true;
17814 if (parm->declarator->kind != cdk_id)
17816 cp_parser_error (parser, "invalid constrained type parameter");
17817 return false;
17820 /* Don't allow cv-qualified type parameters. */
17821 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
17822 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
17824 cp_parser_error (parser, "cv-qualified type parameter");
17825 return false;
17828 return true;
17831 /* Finish parsing/processing a template type parameter and checking
17832 various restrictions. */
17834 static inline tree
17835 cp_parser_constrained_type_template_parm (cp_parser *parser,
17836 tree id,
17837 cp_parameter_declarator* parmdecl)
17839 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
17840 return finish_template_type_parm (class_type_node, id);
17841 else
17842 return error_mark_node;
17845 static tree
17846 finish_constrained_template_template_parm (tree proto, tree id)
17848 /* FIXME: This should probably be copied, and we may need to adjust
17849 the template parameter depths. */
17850 tree saved_parms = current_template_parms;
17851 begin_template_parm_list ();
17852 current_template_parms = DECL_TEMPLATE_PARMS (proto);
17853 end_template_parm_list ();
17855 tree parm = finish_template_template_parm (class_type_node, id);
17856 current_template_parms = saved_parms;
17858 return parm;
17861 /* Finish parsing/processing a template template parameter by borrowing
17862 the template parameter list from the prototype parameter. */
17864 static tree
17865 cp_parser_constrained_template_template_parm (cp_parser *parser,
17866 tree proto,
17867 tree id,
17868 cp_parameter_declarator *parmdecl)
17870 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
17871 return error_mark_node;
17872 return finish_constrained_template_template_parm (proto, id);
17875 /* Create a new non-type template parameter from the given PARM
17876 declarator. */
17878 static tree
17879 cp_parser_constrained_non_type_template_parm (bool *is_non_type,
17880 cp_parameter_declarator *parm)
17882 *is_non_type = true;
17883 cp_declarator *decl = parm->declarator;
17884 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
17885 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
17886 return grokdeclarator (decl, specs, TPARM, 0, NULL);
17889 /* Build a constrained template parameter based on the PARMDECL
17890 declarator. The type of PARMDECL is the constrained type, which
17891 refers to the prototype template parameter that ultimately
17892 specifies the type of the declared parameter. */
17894 static tree
17895 finish_constrained_parameter (cp_parser *parser,
17896 cp_parameter_declarator *parmdecl,
17897 bool *is_non_type)
17899 tree decl = parmdecl->decl_specifiers.type;
17900 tree id = get_unqualified_id (parmdecl->declarator);
17901 tree def = parmdecl->default_argument;
17902 tree proto = DECL_INITIAL (decl);
17904 /* Build the parameter. Return an error if the declarator was invalid. */
17905 tree parm;
17906 if (TREE_CODE (proto) == TYPE_DECL)
17907 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
17908 else if (TREE_CODE (proto) == TEMPLATE_DECL)
17909 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
17910 parmdecl);
17911 else
17912 parm = cp_parser_constrained_non_type_template_parm (is_non_type, parmdecl);
17913 if (parm == error_mark_node)
17914 return error_mark_node;
17916 /* Finish the parameter decl and create a node attaching the
17917 default argument and constraint. */
17918 parm = build_tree_list (def, parm);
17919 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
17921 return parm;
17924 /* Returns true if the parsed type actually represents the declaration
17925 of a type template-parameter. */
17927 static bool
17928 declares_constrained_type_template_parameter (tree type)
17930 return (is_constrained_parameter (type)
17931 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
17934 /* Returns true if the parsed type actually represents the declaration of
17935 a template template-parameter. */
17937 static bool
17938 declares_constrained_template_template_parameter (tree type)
17940 return (is_constrained_parameter (type)
17941 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
17944 /* Parse a default argument for a type template-parameter.
17945 Note that diagnostics are handled in cp_parser_template_parameter. */
17947 static tree
17948 cp_parser_default_type_template_argument (cp_parser *parser)
17950 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
17952 /* Consume the `=' token. */
17953 cp_lexer_consume_token (parser->lexer);
17955 cp_token *token = cp_lexer_peek_token (parser->lexer);
17957 /* Tell cp_parser_lambda_expression this is a default argument. */
17958 auto lvf = make_temp_override (parser->local_variables_forbidden_p);
17959 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
17961 /* Parse the default-argument. */
17962 push_deferring_access_checks (dk_no_deferred);
17963 tree default_argument = cp_parser_type_id (parser,
17964 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
17965 NULL);
17966 pop_deferring_access_checks ();
17968 if (flag_concepts && type_uses_auto (default_argument))
17970 error_at (token->location,
17971 "invalid use of %<auto%> in default template argument");
17972 return error_mark_node;
17975 return default_argument;
17978 /* Parse a default argument for a template template-parameter. */
17980 static tree
17981 cp_parser_default_template_template_argument (cp_parser *parser)
17983 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
17985 bool is_template;
17987 /* Consume the `='. */
17988 cp_lexer_consume_token (parser->lexer);
17989 /* Parse the id-expression. */
17990 push_deferring_access_checks (dk_no_deferred);
17991 /* save token before parsing the id-expression, for error
17992 reporting */
17993 const cp_token* token = cp_lexer_peek_token (parser->lexer);
17994 tree default_argument
17995 = cp_parser_id_expression (parser,
17996 /*template_keyword_p=*/false,
17997 /*check_dependency_p=*/true,
17998 /*template_p=*/&is_template,
17999 /*declarator_p=*/false,
18000 /*optional_p=*/false);
18001 if (TREE_CODE (default_argument) == TYPE_DECL)
18002 /* If the id-expression was a template-id that refers to
18003 a template-class, we already have the declaration here,
18004 so no further lookup is needed. */
18006 else
18007 /* Look up the name. */
18008 default_argument
18009 = cp_parser_lookup_name (parser, default_argument,
18010 none_type,
18011 /*is_template=*/is_template,
18012 /*is_namespace=*/false,
18013 /*check_dependency=*/true,
18014 /*ambiguous_decls=*/NULL,
18015 token->location);
18016 /* See if the default argument is valid. */
18017 default_argument = check_template_template_default_arg (default_argument);
18018 pop_deferring_access_checks ();
18019 return default_argument;
18022 /* Parse a template-parameter.
18024 template-parameter:
18025 type-parameter
18026 parameter-declaration
18028 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
18029 the parameter. The TREE_PURPOSE is the default value, if any.
18030 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
18031 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
18032 set to true iff this parameter is a parameter pack. */
18034 static tree
18035 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
18036 bool *is_parameter_pack)
18038 cp_token *token;
18039 cp_parameter_declarator *parameter_declarator;
18040 tree parm;
18042 /* Assume it is a type parameter or a template parameter. */
18043 *is_non_type = false;
18044 /* Assume it not a parameter pack. */
18045 *is_parameter_pack = false;
18046 /* Peek at the next token. */
18047 token = cp_lexer_peek_token (parser->lexer);
18048 /* If it is `template', we have a type-parameter. */
18049 if (token->keyword == RID_TEMPLATE)
18050 return cp_parser_type_parameter (parser, is_parameter_pack);
18051 /* If it is `class' or `typename' we do not know yet whether it is a
18052 type parameter or a non-type parameter. Consider:
18054 template <typename T, typename T::X X> ...
18058 template <class C, class D*> ...
18060 Here, the first parameter is a type parameter, and the second is
18061 a non-type parameter. We can tell by looking at the token after
18062 the identifier -- if it is a `,', `=', or `>' then we have a type
18063 parameter. */
18064 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
18066 /* Peek at the token after `class' or `typename'. */
18067 token = cp_lexer_peek_nth_token (parser->lexer, 2);
18068 /* If it's an ellipsis, we have a template type parameter
18069 pack. */
18070 if (token->type == CPP_ELLIPSIS)
18071 return cp_parser_type_parameter (parser, is_parameter_pack);
18072 /* If it's an identifier, skip it. */
18073 if (token->type == CPP_NAME)
18074 token = cp_lexer_peek_nth_token (parser->lexer, 3);
18075 /* Now, see if the token looks like the end of a template
18076 parameter. */
18077 if (token->type == CPP_COMMA
18078 || token->type == CPP_EQ
18079 || token->type == CPP_GREATER)
18080 return cp_parser_type_parameter (parser, is_parameter_pack);
18083 /* Otherwise, it is a non-type parameter or a constrained parameter.
18085 [temp.param]
18087 When parsing a default template-argument for a non-type
18088 template-parameter, the first non-nested `>' is taken as the end
18089 of the template parameter-list rather than a greater-than
18090 operator. */
18091 parameter_declarator
18092 = cp_parser_parameter_declaration (parser,
18093 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
18094 /*template_parm_p=*/true,
18095 /*parenthesized_p=*/NULL);
18097 if (!parameter_declarator)
18098 return error_mark_node;
18100 /* If the parameter declaration is marked as a parameter pack, set
18101 *IS_PARAMETER_PACK to notify the caller. */
18102 if (parameter_declarator->template_parameter_pack_p)
18103 *is_parameter_pack = true;
18105 if (parameter_declarator->default_argument)
18107 /* Can happen in some cases of erroneous input (c++/34892). */
18108 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18109 /* Consume the `...' for better error recovery. */
18110 cp_lexer_consume_token (parser->lexer);
18113 /* The parameter may have been constrained type parameter. */
18114 if (is_constrained_parameter (parameter_declarator))
18115 return finish_constrained_parameter (parser,
18116 parameter_declarator,
18117 is_non_type);
18119 // Now we're sure that the parameter is a non-type parameter.
18120 *is_non_type = true;
18122 parm = grokdeclarator (parameter_declarator->declarator,
18123 &parameter_declarator->decl_specifiers,
18124 TPARM, /*initialized=*/0,
18125 /*attrlist=*/NULL);
18126 if (parm == error_mark_node)
18127 return error_mark_node;
18129 return build_tree_list (parameter_declarator->default_argument, parm);
18132 /* Parse a type-parameter.
18134 type-parameter:
18135 class identifier [opt]
18136 class identifier [opt] = type-id
18137 typename identifier [opt]
18138 typename identifier [opt] = type-id
18139 template < template-parameter-list > class identifier [opt]
18140 template < template-parameter-list > class identifier [opt]
18141 = id-expression
18143 GNU Extension (variadic templates):
18145 type-parameter:
18146 class ... identifier [opt]
18147 typename ... identifier [opt]
18149 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
18150 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
18151 the declaration of the parameter.
18153 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
18155 static tree
18156 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
18158 cp_token *token;
18159 tree parameter;
18161 /* Look for a keyword to tell us what kind of parameter this is. */
18162 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
18163 if (!token)
18164 return error_mark_node;
18166 switch (token->keyword)
18168 case RID_CLASS:
18169 case RID_TYPENAME:
18171 tree identifier;
18172 tree default_argument;
18174 /* If the next token is an ellipsis, we have a template
18175 argument pack. */
18176 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18178 /* Consume the `...' token. */
18179 cp_lexer_consume_token (parser->lexer);
18180 maybe_warn_variadic_templates ();
18182 *is_parameter_pack = true;
18185 /* If the next token is an identifier, then it names the
18186 parameter. */
18187 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18188 identifier = cp_parser_identifier (parser);
18189 else
18190 identifier = NULL_TREE;
18192 /* Create the parameter. */
18193 parameter = finish_template_type_parm (class_type_node, identifier);
18195 /* If the next token is an `=', we have a default argument. */
18196 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18198 default_argument
18199 = cp_parser_default_type_template_argument (parser);
18201 /* Template parameter packs cannot have default
18202 arguments. */
18203 if (*is_parameter_pack)
18205 if (identifier)
18206 error_at (token->location,
18207 "template parameter pack %qD cannot have a "
18208 "default argument", identifier);
18209 else
18210 error_at (token->location,
18211 "template parameter packs cannot have "
18212 "default arguments");
18213 default_argument = NULL_TREE;
18215 else if (check_for_bare_parameter_packs (default_argument))
18216 default_argument = error_mark_node;
18218 else
18219 default_argument = NULL_TREE;
18221 /* Create the combined representation of the parameter and the
18222 default argument. */
18223 parameter = build_tree_list (default_argument, parameter);
18225 break;
18227 case RID_TEMPLATE:
18229 tree identifier;
18230 tree default_argument;
18232 /* Look for the `<'. */
18233 cp_parser_require (parser, CPP_LESS, RT_LESS);
18234 /* Parse the template-parameter-list. */
18235 cp_parser_template_parameter_list (parser);
18236 /* Look for the `>'. */
18237 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
18239 /* If template requirements are present, parse them. */
18240 if (flag_concepts)
18242 tree reqs = get_shorthand_constraints (current_template_parms);
18243 if (tree dreqs = cp_parser_requires_clause_opt (parser, false))
18244 reqs = combine_constraint_expressions (reqs, dreqs);
18245 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
18248 /* Look for the `class' or 'typename' keywords. */
18249 cp_parser_type_parameter_key (parser);
18250 /* If the next token is an ellipsis, we have a template
18251 argument pack. */
18252 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18254 /* Consume the `...' token. */
18255 cp_lexer_consume_token (parser->lexer);
18256 maybe_warn_variadic_templates ();
18258 *is_parameter_pack = true;
18260 /* If the next token is an `=', then there is a
18261 default-argument. If the next token is a `>', we are at
18262 the end of the parameter-list. If the next token is a `,',
18263 then we are at the end of this parameter. */
18264 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
18265 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
18266 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18268 identifier = cp_parser_identifier (parser);
18269 /* Treat invalid names as if the parameter were nameless. */
18270 if (identifier == error_mark_node)
18271 identifier = NULL_TREE;
18273 else
18274 identifier = NULL_TREE;
18276 /* Create the template parameter. */
18277 parameter = finish_template_template_parm (class_type_node,
18278 identifier);
18280 /* If the next token is an `=', then there is a
18281 default-argument. */
18282 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18284 default_argument
18285 = cp_parser_default_template_template_argument (parser);
18287 /* Template parameter packs cannot have default
18288 arguments. */
18289 if (*is_parameter_pack)
18291 if (identifier)
18292 error_at (token->location,
18293 "template parameter pack %qD cannot "
18294 "have a default argument",
18295 identifier);
18296 else
18297 error_at (token->location, "template parameter packs cannot "
18298 "have default arguments");
18299 default_argument = NULL_TREE;
18302 else
18303 default_argument = NULL_TREE;
18305 /* Create the combined representation of the parameter and the
18306 default argument. */
18307 parameter = build_tree_list (default_argument, parameter);
18309 break;
18311 default:
18312 gcc_unreachable ();
18313 break;
18316 return parameter;
18319 /* Parse a template-id.
18321 template-id:
18322 template-name < template-argument-list [opt] >
18324 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
18325 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
18326 returned. Otherwise, if the template-name names a function, or set
18327 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
18328 names a class, returns a TYPE_DECL for the specialization.
18330 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
18331 uninstantiated templates. */
18333 static tree
18334 cp_parser_template_id (cp_parser *parser,
18335 bool template_keyword_p,
18336 bool check_dependency_p,
18337 enum tag_types tag_type,
18338 bool is_declaration)
18340 tree templ;
18341 tree arguments;
18342 tree template_id;
18343 cp_token_position start_of_id = 0;
18344 cp_token *next_token = NULL, *next_token_2 = NULL;
18345 bool is_identifier;
18347 /* If the next token corresponds to a template-id, there is no need
18348 to reparse it. */
18349 cp_token *token = cp_lexer_peek_token (parser->lexer);
18351 if (token->type == CPP_TEMPLATE_ID)
18353 cp_lexer_consume_token (parser->lexer);
18354 return saved_checks_value (token->u.tree_check_value);
18357 /* Avoid performing name lookup if there is no possibility of
18358 finding a template-id. */
18359 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
18360 || (token->type == CPP_NAME
18361 && !cp_parser_nth_token_starts_template_argument_list_p
18362 (parser, 2)))
18364 cp_parser_error (parser, "expected template-id");
18365 return error_mark_node;
18368 /* Remember where the template-id starts. */
18369 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
18370 start_of_id = cp_lexer_token_position (parser->lexer, false);
18372 push_deferring_access_checks (dk_deferred);
18374 /* Parse the template-name. */
18375 is_identifier = false;
18376 templ = cp_parser_template_name (parser, template_keyword_p,
18377 check_dependency_p,
18378 is_declaration,
18379 tag_type,
18380 &is_identifier);
18382 /* Push any access checks inside the firewall we're about to create. */
18383 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
18384 pop_deferring_access_checks ();
18385 if (templ == error_mark_node || is_identifier)
18386 return templ;
18388 /* Since we're going to preserve any side-effects from this parse, set up a
18389 firewall to protect our callers from cp_parser_commit_to_tentative_parse
18390 in the template arguments. */
18391 tentative_firewall firewall (parser);
18392 reopen_deferring_access_checks (checks);
18394 /* If we find the sequence `[:' after a template-name, it's probably
18395 a digraph-typo for `< ::'. Substitute the tokens and check if we can
18396 parse correctly the argument list. */
18397 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
18398 == CPP_OPEN_SQUARE)
18399 && next_token->flags & DIGRAPH
18400 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
18401 == CPP_COLON)
18402 && !(next_token_2->flags & PREV_WHITE))
18404 cp_parser_parse_tentatively (parser);
18405 /* Change `:' into `::'. */
18406 next_token_2->type = CPP_SCOPE;
18407 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
18408 CPP_LESS. */
18409 cp_lexer_consume_token (parser->lexer);
18411 /* Parse the arguments. */
18412 arguments = cp_parser_enclosed_template_argument_list (parser);
18413 if (!cp_parser_parse_definitely (parser))
18415 /* If we couldn't parse an argument list, then we revert our changes
18416 and return simply an error. Maybe this is not a template-id
18417 after all. */
18418 next_token_2->type = CPP_COLON;
18419 cp_parser_error (parser, "expected %<<%>");
18420 pop_deferring_access_checks ();
18421 return error_mark_node;
18423 /* Otherwise, emit an error about the invalid digraph, but continue
18424 parsing because we got our argument list. */
18425 if (permerror (next_token->location,
18426 "%<<::%> cannot begin a template-argument list"))
18428 static bool hint = false;
18429 inform (next_token->location,
18430 "%<<:%> is an alternate spelling for %<[%>."
18431 " Insert whitespace between %<<%> and %<::%>");
18432 if (!hint && !flag_permissive)
18434 inform (next_token->location, "(if you use %<-fpermissive%> "
18435 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
18436 "accept your code)");
18437 hint = true;
18441 else
18443 /* Look for the `<' that starts the template-argument-list. */
18444 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
18446 pop_deferring_access_checks ();
18447 return error_mark_node;
18449 /* Parse the arguments. */
18450 arguments = cp_parser_enclosed_template_argument_list (parser);
18452 if ((cxx_dialect > cxx17)
18453 && (TREE_CODE (templ) == FUNCTION_DECL || identifier_p (templ))
18454 && !template_keyword_p
18455 && (cp_parser_error_occurred (parser)
18456 || cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)))
18458 /* This didn't go well. */
18459 if (TREE_CODE (templ) == FUNCTION_DECL)
18461 /* C++20 says that "function-name < a;" is now ill-formed. */
18462 if (cp_parser_error_occurred (parser))
18464 error_at (token->location, "invalid template-argument-list");
18465 inform (token->location, "function name as the left hand "
18466 "operand of %<<%> is ill-formed in C++20; wrap the "
18467 "function name in %<()%>");
18469 else
18470 /* We expect "f<targs>" to be followed by "(args)". */
18471 error_at (cp_lexer_peek_token (parser->lexer)->location,
18472 "expected %<(%> after template-argument-list");
18473 if (start_of_id)
18474 /* Purge all subsequent tokens. */
18475 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
18477 else
18478 cp_parser_simulate_error (parser);
18479 pop_deferring_access_checks ();
18480 return error_mark_node;
18484 /* Set the location to be of the form:
18485 template-name < template-argument-list [opt] >
18486 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18487 with caret == start at the start of the template-name,
18488 ranging until the closing '>'. */
18489 location_t combined_loc
18490 = make_location (token->location, token->location, parser->lexer);
18492 /* Check for concepts autos where they don't belong. We could
18493 identify types in some cases of identifier TEMPL, looking ahead
18494 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
18495 types. We reject them in functions, but if what we have is an
18496 identifier, even with none_type we can't conclude it's NOT a
18497 type, we have to wait for template substitution. */
18498 if (flag_concepts && check_auto_in_tmpl_args (templ, arguments))
18499 template_id = error_mark_node;
18500 /* Build a representation of the specialization. */
18501 else if (identifier_p (templ))
18502 template_id = build_min_nt_loc (combined_loc,
18503 TEMPLATE_ID_EXPR,
18504 templ, arguments);
18505 else if (DECL_TYPE_TEMPLATE_P (templ)
18506 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
18508 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
18509 template (rather than some instantiation thereof) only if
18510 is not nested within some other construct. For example, in
18511 "template <typename T> void f(T) { A<T>::", A<T> is just an
18512 instantiation of A. */
18513 bool entering_scope
18514 = (template_parm_scope_p ()
18515 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE));
18516 template_id
18517 = finish_template_type (templ, arguments, entering_scope);
18519 else if (concept_definition_p (templ))
18521 /* The caller will decide whether this is a concept check or type
18522 constraint. */
18523 template_id = build2_loc (combined_loc, TEMPLATE_ID_EXPR,
18524 boolean_type_node, templ, arguments);
18526 else if (variable_template_p (templ))
18528 template_id = lookup_template_variable (templ, arguments);
18529 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
18530 SET_EXPR_LOCATION (template_id, combined_loc);
18532 else if (TREE_CODE (templ) == TYPE_DECL
18533 && TREE_CODE (TREE_TYPE (templ)) == TYPENAME_TYPE)
18535 /* Some type template in dependent scope. */
18536 tree &name = TYPENAME_TYPE_FULLNAME (TREE_TYPE (templ));
18537 name = build_min_nt_loc (combined_loc,
18538 TEMPLATE_ID_EXPR,
18539 name, arguments);
18540 template_id = templ;
18542 else
18544 /* If it's not a class-template or a template-template, it should be
18545 a function-template. */
18546 gcc_assert (OVL_P (templ) || BASELINK_P (templ));
18548 template_id = lookup_template_function (templ, arguments);
18549 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
18550 SET_EXPR_LOCATION (template_id, combined_loc);
18553 /* If parsing tentatively, replace the sequence of tokens that makes
18554 up the template-id with a CPP_TEMPLATE_ID token. That way,
18555 should we re-parse the token stream, we will not have to repeat
18556 the effort required to do the parse, nor will we issue duplicate
18557 error messages about problems during instantiation of the
18558 template. */
18559 if (start_of_id
18560 /* Don't do this if we had a parse error in a declarator; re-parsing
18561 might succeed if a name changes meaning (60361). */
18562 && !(cp_parser_error_occurred (parser)
18563 && cp_parser_parsing_tentatively (parser)
18564 && parser->in_declarator_p))
18566 /* Reset the contents of the START_OF_ID token. */
18567 token->type = CPP_TEMPLATE_ID;
18568 token->location = combined_loc;
18570 /* Retrieve any deferred checks. Do not pop this access checks yet
18571 so the memory will not be reclaimed during token replacing below. */
18572 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
18573 token->tree_check_p = true;
18574 token->u.tree_check_value->value = template_id;
18575 token->u.tree_check_value->checks = get_deferred_access_checks ();
18576 token->keyword = RID_MAX;
18578 /* Purge all subsequent tokens. */
18579 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
18581 /* ??? Can we actually assume that, if template_id ==
18582 error_mark_node, we will have issued a diagnostic to the
18583 user, as opposed to simply marking the tentative parse as
18584 failed? */
18585 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
18586 error_at (token->location, "parse error in template argument list");
18589 pop_to_parent_deferring_access_checks ();
18590 return template_id;
18593 /* Like cp_parser_template_id, called in non-type context. */
18595 static tree
18596 cp_parser_template_id_expr (cp_parser *parser,
18597 bool template_keyword_p,
18598 bool check_dependency_p,
18599 bool is_declaration)
18601 tree x = cp_parser_template_id (parser, template_keyword_p, check_dependency_p,
18602 none_type, is_declaration);
18603 if (TREE_CODE (x) == TEMPLATE_ID_EXPR
18604 && concept_check_p (x))
18605 /* We didn't check the arguments in cp_parser_template_id; do that now. */
18606 return build_concept_id (x);
18607 return x;
18610 /* Parse a template-name.
18612 template-name:
18613 identifier
18615 The standard should actually say:
18617 template-name:
18618 identifier
18619 operator-function-id
18621 A defect report has been filed about this issue.
18623 A conversion-function-id cannot be a template name because they cannot
18624 be part of a template-id. In fact, looking at this code:
18626 a.operator K<int>()
18628 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
18629 It is impossible to call a templated conversion-function-id with an
18630 explicit argument list, since the only allowed template parameter is
18631 the type to which it is converting.
18633 If TEMPLATE_KEYWORD_P is true, then we have just seen the
18634 `template' keyword, in a construction like:
18636 T::template f<3>()
18638 In that case `f' is taken to be a template-name, even though there
18639 is no way of knowing for sure.
18641 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
18642 name refers to a set of overloaded functions, at least one of which
18643 is a template, or an IDENTIFIER_NODE with the name of the template,
18644 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
18645 names are looked up inside uninstantiated templates. */
18647 static tree
18648 cp_parser_template_name (cp_parser* parser,
18649 bool template_keyword_p,
18650 bool check_dependency_p,
18651 bool is_declaration,
18652 enum tag_types tag_type,
18653 bool *is_identifier)
18655 tree identifier;
18656 tree decl;
18657 cp_token *token = cp_lexer_peek_token (parser->lexer);
18659 /* If the next token is `operator', then we have either an
18660 operator-function-id or a conversion-function-id. */
18661 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
18663 /* We don't know whether we're looking at an
18664 operator-function-id or a conversion-function-id. */
18665 cp_parser_parse_tentatively (parser);
18666 /* Try an operator-function-id. */
18667 identifier = cp_parser_operator_function_id (parser);
18668 /* If that didn't work, try a conversion-function-id. */
18669 if (!cp_parser_parse_definitely (parser))
18671 cp_parser_error (parser, "expected template-name");
18672 return error_mark_node;
18675 /* Look for the identifier. */
18676 else
18677 identifier = cp_parser_identifier (parser);
18679 /* If we didn't find an identifier, we don't have a template-id. */
18680 if (identifier == error_mark_node)
18681 return error_mark_node;
18683 /* If the name immediately followed the `template' keyword, then it
18684 is a template-name. However, if the next token is not `<', then
18685 we do not treat it as a template-name, since it is not being used
18686 as part of a template-id. This enables us to handle constructs
18687 like:
18689 template <typename T> struct S { S(); };
18690 template <typename T> S<T>::S();
18692 correctly. We would treat `S' as a template -- if it were `S<T>'
18693 -- but we do not if there is no `<'. */
18695 if (processing_template_decl
18696 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
18698 /* In a declaration, in a dependent context, we pretend that the
18699 "template" keyword was present in order to improve error
18700 recovery. For example, given:
18702 template <typename T> void f(T::X<int>);
18704 we want to treat "X<int>" as a template-id. */
18705 if (is_declaration
18706 && !template_keyword_p
18707 && parser->scope && TYPE_P (parser->scope)
18708 && check_dependency_p
18709 && dependent_scope_p (parser->scope)
18710 /* Do not do this for dtors (or ctors), since they never
18711 need the template keyword before their name. */
18712 && !constructor_name_p (identifier, parser->scope))
18714 cp_token_position start = 0;
18716 /* Explain what went wrong. */
18717 error_at (token->location, "non-template %qD used as template",
18718 identifier);
18719 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
18720 parser->scope, identifier);
18721 /* If parsing tentatively, find the location of the "<" token. */
18722 if (cp_parser_simulate_error (parser))
18723 start = cp_lexer_token_position (parser->lexer, true);
18724 /* Parse the template arguments so that we can issue error
18725 messages about them. */
18726 cp_lexer_consume_token (parser->lexer);
18727 cp_parser_enclosed_template_argument_list (parser);
18728 /* Skip tokens until we find a good place from which to
18729 continue parsing. */
18730 cp_parser_skip_to_closing_parenthesis (parser,
18731 /*recovering=*/true,
18732 /*or_comma=*/true,
18733 /*consume_paren=*/false);
18734 /* If parsing tentatively, permanently remove the
18735 template argument list. That will prevent duplicate
18736 error messages from being issued about the missing
18737 "template" keyword. */
18738 if (start)
18739 cp_lexer_purge_tokens_after (parser->lexer, start);
18740 if (is_identifier)
18741 *is_identifier = true;
18742 parser->context->object_type = NULL_TREE;
18743 return identifier;
18746 /* If the "template" keyword is present, then there is generally
18747 no point in doing name-lookup, so we just return IDENTIFIER.
18748 But, if the qualifying scope is non-dependent then we can
18749 (and must) do name-lookup normally. */
18750 if (template_keyword_p)
18752 tree scope = (parser->scope ? parser->scope
18753 : parser->context->object_type);
18754 if (scope && TYPE_P (scope)
18755 && (!CLASS_TYPE_P (scope)
18756 || (check_dependency_p && dependent_scope_p (scope))))
18758 /* We're optimizing away the call to cp_parser_lookup_name, but
18759 we still need to do this. */
18760 parser->object_scope = parser->context->object_type;
18761 parser->context->object_type = NULL_TREE;
18762 return identifier;
18767 /* cp_parser_lookup_name clears OBJECT_TYPE. */
18768 tree scope = (parser->scope ? parser->scope
18769 : parser->context->object_type);
18771 /* Look up the name. */
18772 decl = cp_parser_lookup_name (parser, identifier,
18773 tag_type,
18774 /*is_template=*/true,
18775 /*is_namespace=*/false,
18776 check_dependency_p,
18777 /*ambiguous_decls=*/NULL,
18778 token->location);
18780 decl = strip_using_decl (decl);
18782 /* 13.3 [temp.names] A < is interpreted as the delimiter of a
18783 template-argument-list if it follows a name that is not a
18784 conversion-function-id and
18785 - that follows the keyword template or a ~ after a nested-name-specifier or
18786 in a class member access expression, or
18787 - for which name lookup finds the injected-class-name of a class template
18788 or finds any declaration of a template, or
18789 - that is an unqualified name for which name lookup either finds one or
18790 more functions or finds nothing, or
18791 - that is a terminal name in a using-declarator (9.9), in a declarator-id
18792 (9.3.4), or in a type-only context other than a nested-name-specifier
18793 (13.8). */
18795 /* Handle injected-class-name. */
18796 decl = maybe_get_template_decl_from_type_decl (decl);
18798 /* If DECL is a template, then the name was a template-name. */
18799 if (TREE_CODE (decl) == TEMPLATE_DECL)
18801 if ((TREE_DEPRECATED (decl) || TREE_UNAVAILABLE (decl))
18802 && deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
18804 tree d = DECL_TEMPLATE_RESULT (decl);
18805 tree attr;
18806 if (TREE_CODE (d) == TYPE_DECL)
18807 attr = TYPE_ATTRIBUTES (TREE_TYPE (d));
18808 else
18809 attr = DECL_ATTRIBUTES (d);
18810 if (TREE_UNAVAILABLE (decl))
18812 attr = lookup_attribute ("unavailable", attr);
18813 error_unavailable_use (decl, attr);
18815 else if (TREE_DEPRECATED (decl)
18816 && deprecated_state != DEPRECATED_SUPPRESS)
18818 attr = lookup_attribute ("deprecated", attr);
18819 warn_deprecated_use (decl, attr);
18823 else
18825 /* Look through an overload set for any templates. */
18826 bool found = false;
18828 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
18829 !found && iter; ++iter)
18830 if (TREE_CODE (*iter) == TEMPLATE_DECL)
18831 found = true;
18833 /* "an unqualified name for which name lookup either finds one or more
18834 functions or finds nothing". */
18835 if (!found
18836 && (cxx_dialect > cxx17)
18837 && !scope
18838 && cp_lexer_next_token_is (parser->lexer, CPP_LESS)
18839 && tag_type == none_type)
18841 /* The "more functions" case. Just use the OVERLOAD as normally.
18842 We don't use is_overloaded_fn here to avoid considering
18843 BASELINKs. */
18844 if (TREE_CODE (decl) == OVERLOAD
18845 /* Name lookup found one function. */
18846 || TREE_CODE (decl) == FUNCTION_DECL
18847 /* Name lookup found nothing. */
18848 || decl == error_mark_node)
18849 found = true;
18852 /* "that follows the keyword template"..."in a type-only context" */
18853 if (!found && scope
18854 && (template_keyword_p || tag_type != none_type)
18855 && dependentish_scope_p (scope)
18856 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
18857 found = true;
18859 if (!found)
18861 /* The name does not name a template. */
18862 cp_parser_error (parser, "expected template-name");
18863 return error_mark_node;
18865 else if ((!DECL_P (decl) && !is_overloaded_fn (decl))
18866 || TREE_CODE (decl) == USING_DECL
18867 /* cp_parser_template_id can only handle some TYPE_DECLs. */
18868 || (TREE_CODE (decl) == TYPE_DECL
18869 && TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE))
18870 /* Repeat the lookup at instantiation time. */
18871 decl = identifier;
18874 return decl;
18877 /* Parse a template-argument-list.
18879 template-argument-list:
18880 template-argument ... [opt]
18881 template-argument-list , template-argument ... [opt]
18883 Returns a TREE_VEC containing the arguments. */
18885 static tree
18886 cp_parser_template_argument_list (cp_parser* parser)
18888 bool saved_in_template_argument_list_p;
18889 bool saved_ice_p;
18890 bool saved_non_ice_p;
18892 /* Don't create location wrapper nodes within a template-argument-list. */
18893 auto_suppress_location_wrappers sentinel;
18895 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
18896 parser->in_template_argument_list_p = true;
18897 /* Even if the template-id appears in an integral
18898 constant-expression, the contents of the argument list do
18899 not. */
18900 saved_ice_p = parser->integral_constant_expression_p;
18901 parser->integral_constant_expression_p = false;
18902 saved_non_ice_p = parser->non_integral_constant_expression_p;
18903 parser->non_integral_constant_expression_p = false;
18905 /* Parse the arguments. */
18906 auto_vec<tree, 10> args;
18909 if (!args.is_empty ())
18910 /* Consume the comma. */
18911 cp_lexer_consume_token (parser->lexer);
18913 /* Parse the template-argument. */
18914 tree argument = cp_parser_template_argument (parser);
18916 /* If the next token is an ellipsis, we're expanding a template
18917 argument pack. */
18918 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18920 if (argument == error_mark_node)
18922 cp_token *token = cp_lexer_peek_token (parser->lexer);
18923 error_at (token->location,
18924 "expected parameter pack before %<...%>");
18926 /* Consume the `...' token. */
18927 cp_lexer_consume_token (parser->lexer);
18929 /* Make the argument into a TYPE_PACK_EXPANSION or
18930 EXPR_PACK_EXPANSION. */
18931 argument = make_pack_expansion (argument);
18934 args.safe_push (argument);
18936 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
18938 int n_args = args.length ();
18939 tree vec = make_tree_vec (n_args);
18941 for (int i = 0; i < n_args; i++)
18942 TREE_VEC_ELT (vec, i) = args[i];
18944 parser->non_integral_constant_expression_p = saved_non_ice_p;
18945 parser->integral_constant_expression_p = saved_ice_p;
18946 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
18947 if (CHECKING_P)
18948 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
18949 return vec;
18952 /* Parse a template-argument.
18954 template-argument:
18955 assignment-expression
18956 type-id
18957 id-expression
18959 The representation is that of an assignment-expression, type-id, or
18960 id-expression -- except that the qualified id-expression is
18961 evaluated, so that the value returned is either a DECL or an
18962 OVERLOAD.
18964 Although the standard says "assignment-expression", it forbids
18965 throw-expressions or assignments in the template argument.
18966 Therefore, we use "conditional-expression" instead. */
18968 static tree
18969 cp_parser_template_argument (cp_parser* parser)
18971 tree argument;
18972 bool template_p;
18973 bool address_p;
18974 bool maybe_type_id = false;
18975 cp_token *token = NULL, *argument_start_token = NULL;
18976 location_t loc = 0;
18977 cp_id_kind idk;
18979 /* There's really no way to know what we're looking at, so we just
18980 try each alternative in order.
18982 [temp.arg]
18984 In a template-argument, an ambiguity between a type-id and an
18985 expression is resolved to a type-id, regardless of the form of
18986 the corresponding template-parameter.
18988 Therefore, we try a type-id first. */
18989 cp_parser_parse_tentatively (parser);
18990 argument = cp_parser_template_type_arg (parser);
18991 /* If there was no error parsing the type-id but the next token is a
18992 '>>', our behavior depends on which dialect of C++ we're
18993 parsing. In C++98, we probably found a typo for '> >'. But there
18994 are type-id which are also valid expressions. For instance:
18996 struct X { int operator >> (int); };
18997 template <int V> struct Foo {};
18998 Foo<X () >> 5> r;
19000 Here 'X()' is a valid type-id of a function type, but the user just
19001 wanted to write the expression "X() >> 5". Thus, we remember that we
19002 found a valid type-id, but we still try to parse the argument as an
19003 expression to see what happens.
19005 In C++0x, the '>>' will be considered two separate '>'
19006 tokens. */
19007 if (!cp_parser_error_occurred (parser)
19008 && ((cxx_dialect == cxx98
19009 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19010 /* Similarly for >= which
19011 cp_parser_next_token_ends_template_argument_p treats for
19012 diagnostics purposes as mistyped > =, but can be valid
19013 after a type-id. */
19014 || cp_lexer_next_token_is (parser->lexer, CPP_GREATER_EQ)))
19016 maybe_type_id = true;
19017 cp_parser_abort_tentative_parse (parser);
19019 else
19021 /* If the next token isn't a `,' or a `>', then this argument wasn't
19022 really finished. This means that the argument is not a valid
19023 type-id. */
19024 if (!cp_parser_next_token_ends_template_argument_p (parser))
19025 cp_parser_error (parser, "expected template-argument");
19026 /* If that worked, we're done. */
19027 if (cp_parser_parse_definitely (parser))
19028 return argument;
19030 /* We're still not sure what the argument will be. */
19031 cp_parser_parse_tentatively (parser);
19032 /* Try a template. */
19033 argument_start_token = cp_lexer_peek_token (parser->lexer);
19034 argument = cp_parser_id_expression (parser,
19035 /*template_keyword_p=*/false,
19036 /*check_dependency_p=*/true,
19037 &template_p,
19038 /*declarator_p=*/false,
19039 /*optional_p=*/false);
19040 /* If the next token isn't a `,' or a `>', then this argument wasn't
19041 really finished. */
19042 if (!cp_parser_next_token_ends_template_argument_p (parser))
19043 cp_parser_error (parser, "expected template-argument");
19044 if (!cp_parser_error_occurred (parser))
19046 /* Figure out what is being referred to. If the id-expression
19047 was for a class template specialization, then we will have a
19048 TYPE_DECL at this point. There is no need to do name lookup
19049 at this point in that case. */
19050 if (TREE_CODE (argument) != TYPE_DECL)
19051 argument = cp_parser_lookup_name (parser, argument,
19052 none_type,
19053 /*is_template=*/template_p,
19054 /*is_namespace=*/false,
19055 /*check_dependency=*/true,
19056 /*ambiguous_decls=*/NULL,
19057 argument_start_token->location);
19058 if (TREE_CODE (argument) != TEMPLATE_DECL
19059 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
19060 cp_parser_error (parser, "expected template-name");
19062 if (cp_parser_parse_definitely (parser))
19064 if (TREE_UNAVAILABLE (argument))
19065 error_unavailable_use (argument, NULL_TREE);
19066 else if (TREE_DEPRECATED (argument))
19067 warn_deprecated_use (argument, NULL_TREE);
19068 return argument;
19070 /* It must be a non-type argument. In C++17 any constant-expression is
19071 allowed. */
19072 if (cxx_dialect > cxx14)
19073 goto general_expr;
19075 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
19077 -- an integral constant-expression of integral or enumeration
19078 type; or
19080 -- the name of a non-type template-parameter; or
19082 -- the name of an object or function with external linkage...
19084 -- the address of an object or function with external linkage...
19086 -- a pointer to member... */
19087 /* Look for a non-type template parameter. */
19088 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19090 cp_parser_parse_tentatively (parser);
19091 argument = cp_parser_primary_expression (parser,
19092 /*address_p=*/false,
19093 /*cast_p=*/false,
19094 /*template_arg_p=*/true,
19095 &idk);
19096 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
19097 || !cp_parser_next_token_ends_template_argument_p (parser))
19098 cp_parser_simulate_error (parser);
19099 if (cp_parser_parse_definitely (parser))
19100 return argument;
19103 /* If the next token is "&", the argument must be the address of an
19104 object or function with external linkage. */
19105 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
19106 if (address_p)
19108 loc = cp_lexer_peek_token (parser->lexer)->location;
19109 cp_lexer_consume_token (parser->lexer);
19111 /* See if we might have an id-expression. */
19112 token = cp_lexer_peek_token (parser->lexer);
19113 if (token->type == CPP_NAME
19114 || token->keyword == RID_OPERATOR
19115 || token->type == CPP_SCOPE
19116 || token->type == CPP_TEMPLATE_ID
19117 || token->type == CPP_NESTED_NAME_SPECIFIER)
19119 cp_parser_parse_tentatively (parser);
19120 argument = cp_parser_primary_expression (parser,
19121 address_p,
19122 /*cast_p=*/false,
19123 /*template_arg_p=*/true,
19124 &idk);
19125 if (cp_parser_error_occurred (parser)
19126 || !cp_parser_next_token_ends_template_argument_p (parser))
19127 cp_parser_abort_tentative_parse (parser);
19128 else
19130 tree probe;
19132 if (INDIRECT_REF_P (argument))
19134 /* Strip the dereference temporarily. */
19135 gcc_assert (REFERENCE_REF_P (argument));
19136 argument = TREE_OPERAND (argument, 0);
19139 /* If we're in a template, we represent a qualified-id referring
19140 to a static data member as a SCOPE_REF even if the scope isn't
19141 dependent so that we can check access control later. */
19142 probe = argument;
19143 if (TREE_CODE (probe) == SCOPE_REF)
19144 probe = TREE_OPERAND (probe, 1);
19145 if (VAR_P (probe))
19147 /* A variable without external linkage might still be a
19148 valid constant-expression, so no error is issued here
19149 if the external-linkage check fails. */
19150 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
19151 cp_parser_simulate_error (parser);
19153 else if (is_overloaded_fn (argument))
19154 /* All overloaded functions are allowed; if the external
19155 linkage test does not pass, an error will be issued
19156 later. */
19158 else if (address_p
19159 && (TREE_CODE (argument) == OFFSET_REF
19160 || TREE_CODE (argument) == SCOPE_REF))
19161 /* A pointer-to-member. */
19163 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
19165 else
19166 cp_parser_simulate_error (parser);
19168 if (cp_parser_parse_definitely (parser))
19170 if (address_p)
19171 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
19172 NULL_TREE, tf_warning_or_error);
19173 else
19174 argument = convert_from_reference (argument);
19175 return argument;
19179 /* If the argument started with "&", there are no other valid
19180 alternatives at this point. */
19181 if (address_p)
19183 cp_parser_error (parser, "invalid non-type template argument");
19184 return error_mark_node;
19187 general_expr:
19188 /* If the argument wasn't successfully parsed as a type-id followed
19189 by '>>', the argument can only be a constant expression now.
19190 Otherwise, we try parsing the constant-expression tentatively,
19191 because the argument could really be a type-id. */
19192 if (maybe_type_id)
19193 cp_parser_parse_tentatively (parser);
19195 if (cxx_dialect <= cxx14)
19196 argument = cp_parser_constant_expression (parser);
19197 else
19199 /* In C++20, we can encounter a braced-init-list. */
19200 if (cxx_dialect >= cxx20
19201 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19203 bool expr_non_constant_p;
19204 return cp_parser_braced_list (parser, &expr_non_constant_p);
19207 /* With C++17 generalized non-type template arguments we need to handle
19208 lvalue constant expressions, too. */
19209 argument = cp_parser_assignment_expression (parser);
19210 require_potential_constant_expression (argument);
19213 if (!maybe_type_id)
19214 return argument;
19215 if (!cp_parser_next_token_ends_template_argument_p (parser))
19216 cp_parser_error (parser, "expected template-argument");
19217 if (cp_parser_parse_definitely (parser))
19218 return argument;
19219 /* We did our best to parse the argument as a non type-id, but that
19220 was the only alternative that matched (albeit with a '>' after
19221 it). We can assume it's just a typo from the user, and a
19222 diagnostic will then be issued. */
19223 return cp_parser_template_type_arg (parser);
19226 /* Parse an explicit-instantiation.
19228 explicit-instantiation:
19229 template declaration
19231 Although the standard says `declaration', what it really means is:
19233 explicit-instantiation:
19234 template decl-specifier-seq [opt] declarator [opt] ;
19236 Things like `template int S<int>::i = 5, int S<double>::j;' are not
19237 supposed to be allowed. A defect report has been filed about this
19238 issue.
19240 GNU Extension:
19242 explicit-instantiation:
19243 storage-class-specifier template
19244 decl-specifier-seq [opt] declarator [opt] ;
19245 function-specifier template
19246 decl-specifier-seq [opt] declarator [opt] ; */
19248 static void
19249 cp_parser_explicit_instantiation (cp_parser* parser)
19251 int declares_class_or_enum;
19252 cp_decl_specifier_seq decl_specifiers;
19253 tree extension_specifier = NULL_TREE;
19255 auto_timevar tv (TV_TEMPLATE_INST);
19257 /* Look for an (optional) storage-class-specifier or
19258 function-specifier. */
19259 if (cp_parser_allow_gnu_extensions_p (parser))
19261 extension_specifier
19262 = cp_parser_storage_class_specifier_opt (parser);
19263 if (!extension_specifier)
19264 extension_specifier
19265 = cp_parser_function_specifier_opt (parser,
19266 /*decl_specs=*/NULL);
19269 /* Look for the `template' keyword. */
19270 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
19271 /* Let the front end know that we are processing an explicit
19272 instantiation. */
19273 begin_explicit_instantiation ();
19274 /* [temp.explicit] says that we are supposed to ignore access
19275 control while processing explicit instantiation directives. */
19276 push_deferring_access_checks (dk_no_check);
19277 /* Parse a decl-specifier-seq. */
19278 cp_parser_decl_specifier_seq (parser,
19279 CP_PARSER_FLAGS_OPTIONAL,
19280 &decl_specifiers,
19281 &declares_class_or_enum);
19283 cp_omp_declare_simd_data odsd;
19284 if (decl_specifiers.attributes && (flag_openmp || flag_openmp_simd))
19285 cp_parser_handle_directive_omp_attributes (parser,
19286 &decl_specifiers.attributes,
19287 &odsd, true);
19289 /* If there was exactly one decl-specifier, and it declared a class,
19290 and there's no declarator, then we have an explicit type
19291 instantiation. */
19292 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
19294 tree type = check_tag_decl (&decl_specifiers,
19295 /*explicit_type_instantiation_p=*/true);
19296 /* Turn access control back on for names used during
19297 template instantiation. */
19298 pop_deferring_access_checks ();
19299 if (type)
19300 do_type_instantiation (type, extension_specifier,
19301 /*complain=*/tf_error);
19303 else
19305 cp_declarator *declarator;
19306 tree decl;
19308 /* Parse the declarator. */
19309 declarator
19310 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19311 CP_PARSER_FLAGS_NONE,
19312 /*ctor_dtor_or_conv_p=*/NULL,
19313 /*parenthesized_p=*/NULL,
19314 /*member_p=*/false,
19315 /*friend_p=*/false,
19316 /*static_p=*/false);
19317 if (declares_class_or_enum & 2)
19318 cp_parser_check_for_definition_in_return_type (declarator,
19319 decl_specifiers.type,
19320 decl_specifiers.locations[ds_type_spec]);
19321 if (declarator != cp_error_declarator)
19323 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
19324 permerror (decl_specifiers.locations[ds_inline],
19325 "explicit instantiation shall not use"
19326 " %<inline%> specifier");
19327 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
19328 permerror (decl_specifiers.locations[ds_constexpr],
19329 "explicit instantiation shall not use"
19330 " %<constexpr%> specifier");
19331 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_consteval))
19332 permerror (decl_specifiers.locations[ds_consteval],
19333 "explicit instantiation shall not use"
19334 " %<consteval%> specifier");
19336 decl = grokdeclarator (declarator, &decl_specifiers,
19337 NORMAL, 0, &decl_specifiers.attributes);
19338 /* Turn access control back on for names used during
19339 template instantiation. */
19340 pop_deferring_access_checks ();
19341 /* Do the explicit instantiation. */
19342 do_decl_instantiation (decl, extension_specifier);
19344 else
19346 pop_deferring_access_checks ();
19347 /* Skip the body of the explicit instantiation. */
19348 cp_parser_skip_to_end_of_statement (parser);
19351 /* We're done with the instantiation. */
19352 end_explicit_instantiation ();
19354 cp_parser_consume_semicolon_at_end_of_statement (parser);
19356 cp_finalize_omp_declare_simd (parser, &odsd);
19359 /* Parse an explicit-specialization.
19361 explicit-specialization:
19362 template < > declaration
19364 Although the standard says `declaration', what it really means is:
19366 explicit-specialization:
19367 template <> decl-specifier [opt] init-declarator [opt] ;
19368 template <> function-definition
19369 template <> explicit-specialization
19370 template <> template-declaration */
19372 static void
19373 cp_parser_explicit_specialization (cp_parser* parser)
19375 cp_token *token = cp_lexer_peek_token (parser->lexer);
19377 /* Look for the `template' keyword. */
19378 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
19379 /* Look for the `<'. */
19380 cp_parser_require (parser, CPP_LESS, RT_LESS);
19381 /* Look for the `>'. */
19382 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
19383 /* We have processed another parameter list. */
19384 ++parser->num_template_parameter_lists;
19386 /* [temp]
19388 A template ... explicit specialization ... shall not have C
19389 linkage. */
19390 bool need_lang_pop = current_lang_name == lang_name_c;
19391 if (need_lang_pop)
19393 error_at (token->location, "template specialization with C linkage");
19394 maybe_show_extern_c_location ();
19396 /* Give it C++ linkage to avoid confusing other parts of the
19397 front end. */
19398 push_lang_context (lang_name_cplusplus);
19401 /* Let the front end know that we are beginning a specialization. */
19402 if (begin_specialization ())
19404 /* If the next keyword is `template', we need to figure out
19405 whether or not we're looking a template-declaration. */
19406 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19408 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
19409 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
19410 cp_parser_template_declaration_after_export (parser,
19411 /*member_p=*/false);
19412 else
19413 cp_parser_explicit_specialization (parser);
19415 else
19416 /* Parse the dependent declaration. */
19417 cp_parser_single_declaration (parser,
19418 /*checks=*/NULL,
19419 /*member_p=*/false,
19420 /*explicit_specialization_p=*/true,
19421 /*friend_p=*/NULL);
19424 /* We're done with the specialization. */
19425 end_specialization ();
19427 /* For the erroneous case of a template with C linkage, we pushed an
19428 implicit C++ linkage scope; exit that scope now. */
19429 if (need_lang_pop)
19430 pop_lang_context ();
19432 /* We're done with this parameter list. */
19433 --parser->num_template_parameter_lists;
19436 /* Preserve the attributes across a garbage collect (by making it a GC
19437 root), which can occur when parsing a member function. */
19439 static GTY(()) vec<tree, va_gc> *cp_parser_decl_specs_attrs;
19441 /* Parse a type-specifier.
19443 type-specifier:
19444 simple-type-specifier
19445 class-specifier
19446 enum-specifier
19447 elaborated-type-specifier
19448 cv-qualifier
19450 GNU Extension:
19452 type-specifier:
19453 __complex__
19455 Returns a representation of the type-specifier. For a
19456 class-specifier, enum-specifier, or elaborated-type-specifier, a
19457 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
19459 The parser flags FLAGS is used to control type-specifier parsing.
19461 If IS_DECLARATION is TRUE, then this type-specifier is appearing
19462 in a decl-specifier-seq.
19464 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
19465 class-specifier, enum-specifier, or elaborated-type-specifier, then
19466 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
19467 if a type is declared; 2 if it is defined. Otherwise, it is set to
19468 zero.
19470 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
19471 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
19472 is set to FALSE. */
19474 static tree
19475 cp_parser_type_specifier (cp_parser* parser,
19476 cp_parser_flags flags,
19477 cp_decl_specifier_seq *decl_specs,
19478 bool is_declaration,
19479 int* declares_class_or_enum,
19480 bool* is_cv_qualifier)
19482 tree type_spec = NULL_TREE;
19483 cp_token *token;
19484 enum rid keyword;
19485 cp_decl_spec ds = ds_last;
19487 /* Assume this type-specifier does not declare a new type. */
19488 if (declares_class_or_enum)
19489 *declares_class_or_enum = 0;
19490 /* And that it does not specify a cv-qualifier. */
19491 if (is_cv_qualifier)
19492 *is_cv_qualifier = false;
19493 /* Peek at the next token. */
19494 token = cp_lexer_peek_token (parser->lexer);
19496 /* If we're looking at a keyword, we can use that to guide the
19497 production we choose. */
19498 keyword = token->keyword;
19499 switch (keyword)
19501 case RID_ENUM:
19502 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
19503 goto elaborated_type_specifier;
19505 /* Look for the enum-specifier. */
19506 type_spec = cp_parser_enum_specifier (parser);
19507 /* If that worked, we're done. */
19508 if (type_spec)
19510 if (declares_class_or_enum)
19511 *declares_class_or_enum = 2;
19512 if (decl_specs)
19513 cp_parser_set_decl_spec_type (decl_specs,
19514 type_spec,
19515 token,
19516 /*type_definition_p=*/true);
19517 return type_spec;
19519 else
19520 goto elaborated_type_specifier;
19522 /* Any of these indicate either a class-specifier, or an
19523 elaborated-type-specifier. */
19524 case RID_CLASS:
19525 case RID_STRUCT:
19526 case RID_UNION:
19527 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
19528 goto elaborated_type_specifier;
19530 /* Parse tentatively so that we can back up if we don't find a
19531 class-specifier. */
19532 cp_parser_parse_tentatively (parser);
19533 if (decl_specs->attributes)
19534 vec_safe_push (cp_parser_decl_specs_attrs, decl_specs->attributes);
19535 /* Look for the class-specifier. */
19536 type_spec = cp_parser_class_specifier (parser);
19537 if (decl_specs->attributes)
19538 cp_parser_decl_specs_attrs->pop ();
19539 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
19540 /* If that worked, we're done. */
19541 if (cp_parser_parse_definitely (parser))
19543 if (declares_class_or_enum)
19544 *declares_class_or_enum = 2;
19545 if (decl_specs)
19546 cp_parser_set_decl_spec_type (decl_specs,
19547 type_spec,
19548 token,
19549 /*type_definition_p=*/true);
19550 return type_spec;
19553 /* Fall through. */
19554 elaborated_type_specifier:
19555 /* We're declaring (not defining) a class or enum. */
19556 if (declares_class_or_enum)
19557 *declares_class_or_enum = 1;
19559 /* Fall through. */
19560 case RID_TYPENAME:
19561 /* Look for an elaborated-type-specifier. */
19562 type_spec
19563 = (cp_parser_elaborated_type_specifier
19564 (parser,
19565 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
19566 is_declaration));
19567 if (decl_specs)
19568 cp_parser_set_decl_spec_type (decl_specs,
19569 type_spec,
19570 token,
19571 /*type_definition_p=*/false);
19572 return type_spec;
19574 case RID_CONST:
19575 ds = ds_const;
19576 if (is_cv_qualifier)
19577 *is_cv_qualifier = true;
19578 break;
19580 case RID_VOLATILE:
19581 ds = ds_volatile;
19582 if (is_cv_qualifier)
19583 *is_cv_qualifier = true;
19584 break;
19586 case RID_RESTRICT:
19587 ds = ds_restrict;
19588 if (is_cv_qualifier)
19589 *is_cv_qualifier = true;
19590 break;
19592 case RID_COMPLEX:
19593 /* The `__complex__' keyword is a GNU extension. */
19594 ds = ds_complex;
19595 break;
19597 default:
19598 break;
19601 /* Handle simple keywords. */
19602 if (ds != ds_last)
19604 if (decl_specs)
19606 set_and_check_decl_spec_loc (decl_specs, ds, token);
19607 decl_specs->any_specifiers_p = true;
19609 return cp_lexer_consume_token (parser->lexer)->u.value;
19612 /* If we do not already have a type-specifier, assume we are looking
19613 at a simple-type-specifier. */
19614 type_spec = cp_parser_simple_type_specifier (parser,
19615 decl_specs,
19616 flags);
19618 /* If we didn't find a type-specifier, and a type-specifier was not
19619 optional in this context, issue an error message. */
19620 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
19622 cp_parser_error (parser, "expected type specifier");
19623 return error_mark_node;
19626 return type_spec;
19629 /* Parse a simple-type-specifier.
19631 simple-type-specifier:
19632 :: [opt] nested-name-specifier [opt] type-name
19633 :: [opt] nested-name-specifier template template-id
19634 char
19635 wchar_t
19636 bool
19637 short
19639 long
19640 signed
19641 unsigned
19642 float
19643 double
19644 void
19646 C++11 Extension:
19648 simple-type-specifier:
19649 auto
19650 decltype ( expression )
19651 char16_t
19652 char32_t
19653 __underlying_type ( type-id )
19655 C++17 extension:
19657 nested-name-specifier(opt) template-name
19659 GNU Extension:
19661 simple-type-specifier:
19662 __int128
19663 __typeof__ unary-expression
19664 __typeof__ ( type-id )
19665 __typeof__ ( type-id ) { initializer-list , [opt] }
19667 Concepts Extension:
19669 simple-type-specifier:
19670 constrained-type-specifier
19672 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
19673 appropriately updated. */
19675 static tree
19676 cp_parser_simple_type_specifier (cp_parser* parser,
19677 cp_decl_specifier_seq *decl_specs,
19678 cp_parser_flags flags)
19680 tree type = NULL_TREE;
19681 cp_token *token;
19682 int idx;
19684 /* Peek at the next token. */
19685 token = cp_lexer_peek_token (parser->lexer);
19687 /* If we're looking at a keyword, things are easy. */
19688 switch (token->keyword)
19690 case RID_CHAR:
19691 if (decl_specs)
19692 decl_specs->explicit_char_p = true;
19693 type = char_type_node;
19694 break;
19695 case RID_CHAR8:
19696 type = char8_type_node;
19697 break;
19698 case RID_CHAR16:
19699 type = char16_type_node;
19700 break;
19701 case RID_CHAR32:
19702 type = char32_type_node;
19703 break;
19704 case RID_WCHAR:
19705 type = wchar_type_node;
19706 break;
19707 case RID_BOOL:
19708 type = boolean_type_node;
19709 break;
19710 case RID_SHORT:
19711 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
19712 type = short_integer_type_node;
19713 break;
19714 case RID_INT:
19715 if (decl_specs)
19716 decl_specs->explicit_int_p = true;
19717 type = integer_type_node;
19718 break;
19719 case RID_INT_N_0:
19720 case RID_INT_N_1:
19721 case RID_INT_N_2:
19722 case RID_INT_N_3:
19723 idx = token->keyword - RID_INT_N_0;
19724 if (! int_n_enabled_p [idx])
19725 break;
19726 if (decl_specs)
19728 decl_specs->explicit_intN_p = true;
19729 decl_specs->int_n_idx = idx;
19730 /* Check if the alternate "__intN__" form has been used instead of
19731 "__intN". */
19732 if (startswith (IDENTIFIER_POINTER (token->u.value)
19733 + (IDENTIFIER_LENGTH (token->u.value) - 2), "__"))
19734 decl_specs->int_n_alt = true;
19736 type = int_n_trees [idx].signed_type;
19737 break;
19738 case RID_LONG:
19739 if (decl_specs)
19740 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
19741 type = long_integer_type_node;
19742 break;
19743 case RID_SIGNED:
19744 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
19745 type = integer_type_node;
19746 break;
19747 case RID_UNSIGNED:
19748 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
19749 type = unsigned_type_node;
19750 break;
19751 case RID_FLOAT:
19752 type = float_type_node;
19753 break;
19754 case RID_DOUBLE:
19755 type = double_type_node;
19756 break;
19757 CASE_RID_FLOATN_NX:
19758 type = FLOATN_NX_TYPE_NODE (token->keyword - RID_FLOATN_NX_FIRST);
19759 if (type == NULL_TREE)
19760 error ("%<_Float%d%s%> is not supported on this target",
19761 floatn_nx_types[token->keyword - RID_FLOATN_NX_FIRST].n,
19762 floatn_nx_types[token->keyword - RID_FLOATN_NX_FIRST].extended
19763 ? "x" : "");
19764 break;
19765 case RID_VOID:
19766 type = void_type_node;
19767 break;
19769 case RID_AUTO:
19770 maybe_warn_cpp0x (CPP0X_AUTO);
19771 if (parser->auto_is_implicit_function_template_parm_p)
19773 /* The 'auto' might be the placeholder return type for a function decl
19774 with trailing return type. */
19775 bool have_trailing_return_fn_decl = false;
19777 cp_parser_parse_tentatively (parser);
19778 cp_lexer_consume_token (parser->lexer);
19779 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
19780 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
19781 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
19782 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
19784 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19786 cp_lexer_consume_token (parser->lexer);
19787 cp_parser_skip_to_closing_parenthesis (parser,
19788 /*recovering*/false,
19789 /*or_comma*/false,
19790 /*consume_paren*/true);
19791 continue;
19794 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
19796 have_trailing_return_fn_decl = true;
19797 break;
19800 cp_lexer_consume_token (parser->lexer);
19802 cp_parser_abort_tentative_parse (parser);
19804 if (have_trailing_return_fn_decl)
19806 type = make_auto ();
19807 break;
19810 if (cxx_dialect >= cxx14)
19812 type = synthesize_implicit_template_parm (parser, NULL_TREE);
19813 type = TREE_TYPE (type);
19815 else
19816 type = error_mark_node;
19818 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
19820 if (cxx_dialect < cxx14)
19821 error_at (token->location,
19822 "use of %<auto%> in lambda parameter declaration "
19823 "only available with "
19824 "%<-std=c++14%> or %<-std=gnu++14%>");
19826 else if (cxx_dialect < cxx14)
19827 error_at (token->location,
19828 "use of %<auto%> in parameter declaration "
19829 "only available with "
19830 "%<-std=c++14%> or %<-std=gnu++14%>");
19831 else if (!flag_concepts)
19832 pedwarn (token->location, 0,
19833 "use of %<auto%> in parameter declaration "
19834 "only available with %<-std=c++20%> or %<-fconcepts%>");
19836 else
19837 type = make_auto ();
19838 break;
19840 case RID_DECLTYPE:
19841 /* Since DR 743, decltype can either be a simple-type-specifier by
19842 itself or begin a nested-name-specifier. Parsing it will replace
19843 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
19844 handling below decide what to do. */
19845 cp_parser_decltype (parser);
19846 cp_lexer_set_token_position (parser->lexer, token);
19847 break;
19849 case RID_TYPEOF:
19850 /* Consume the `typeof' token. */
19851 cp_lexer_consume_token (parser->lexer);
19852 /* Parse the operand to `typeof'. */
19853 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
19854 /* If it is not already a TYPE, take its type. */
19855 if (!TYPE_P (type))
19856 type = finish_typeof (type);
19858 if (decl_specs)
19859 cp_parser_set_decl_spec_type (decl_specs, type,
19860 token,
19861 /*type_definition_p=*/false);
19863 return type;
19865 #define DEFTRAIT_TYPE(CODE, NAME, ARITY) \
19866 case RID_##CODE:
19867 #include "cp-trait.def"
19868 #undef DEFTRAIT_TYPE
19869 type = cp_parser_trait (parser, token->keyword);
19870 if (decl_specs)
19871 cp_parser_set_decl_spec_type (decl_specs, type,
19872 token,
19873 /*type_definition_p=*/false);
19875 return type;
19877 default:
19878 break;
19881 /* If token is an already-parsed decltype not followed by ::,
19882 it's a simple-type-specifier. */
19883 if (token->type == CPP_DECLTYPE
19884 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
19886 type = saved_checks_value (token->u.tree_check_value);
19887 if (decl_specs)
19889 cp_parser_set_decl_spec_type (decl_specs, type,
19890 token,
19891 /*type_definition_p=*/false);
19892 /* Remember that we are handling a decltype in order to
19893 implement the resolution of DR 1510 when the argument
19894 isn't instantiation dependent. */
19895 decl_specs->decltype_p = true;
19897 cp_lexer_consume_token (parser->lexer);
19898 return type;
19901 /* If the type-specifier was for a built-in type, we're done. */
19902 if (type)
19904 /* Record the type. */
19905 if (decl_specs
19906 && (token->keyword != RID_SIGNED
19907 && token->keyword != RID_UNSIGNED
19908 && token->keyword != RID_SHORT
19909 && token->keyword != RID_LONG))
19910 cp_parser_set_decl_spec_type (decl_specs,
19911 type,
19912 token,
19913 /*type_definition_p=*/false);
19914 if (decl_specs)
19915 decl_specs->any_specifiers_p = true;
19917 /* Consume the token. */
19918 cp_lexer_consume_token (parser->lexer);
19920 if (type == error_mark_node)
19921 return error_mark_node;
19923 /* There is no valid C++ program where a non-template type is
19924 followed by a "<". That usually indicates that the user thought
19925 that the type was a template. */
19926 cp_parser_check_for_invalid_template_id (parser, type, none_type,
19927 token->location);
19929 return TYPE_NAME (type);
19932 /* The type-specifier must be a user-defined type. */
19933 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
19935 bool qualified_p;
19936 bool global_p;
19937 const bool typename_p = (cxx_dialect >= cxx20
19938 && (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
19940 /* Don't gobble tokens or issue error messages if this is an
19941 optional type-specifier. */
19942 if (flags & CP_PARSER_FLAGS_OPTIONAL)
19943 cp_parser_parse_tentatively (parser);
19945 /* Remember current tentative parsing state -- if we know we need
19946 a type, we can give better diagnostics here. */
19947 bool tent = cp_parser_parsing_tentatively (parser);
19949 token = cp_lexer_peek_token (parser->lexer);
19951 /* Look for the optional `::' operator. */
19952 global_p
19953 = (cp_parser_global_scope_opt (parser,
19954 /*current_scope_valid_p=*/false)
19955 != NULL_TREE);
19956 /* Look for the nested-name specifier. */
19957 qualified_p
19958 = (cp_parser_nested_name_specifier_opt (parser,
19959 /*typename_keyword_p=*/false,
19960 /*check_dependency_p=*/true,
19961 /*type_p=*/false,
19962 /*is_declaration=*/false)
19963 != NULL_TREE);
19964 /* If we have seen a nested-name-specifier, and the next token
19965 is `template', then we are using the template-id production. */
19966 if (parser->scope
19967 && cp_parser_optional_template_keyword (parser))
19969 /* Look for the template-id. */
19970 type = cp_parser_template_id (parser,
19971 /*template_keyword_p=*/true,
19972 /*check_dependency_p=*/true,
19973 none_type,
19974 /*is_declaration=*/false);
19975 /* If the template-id did not name a type, we are out of
19976 luck. */
19977 if (TREE_CODE (type) != TYPE_DECL)
19979 /* ...unless we pretend we have seen 'typename'. */
19980 if (typename_p)
19981 type = cp_parser_make_typename_type (parser, type,
19982 token->location);
19983 else
19985 cp_parser_error (parser, "expected template-id for type");
19986 type = error_mark_node;
19990 /* DR 1812: A < following a qualified-id in a typename-specifier
19991 could safely be assumed to begin a template argument list, so
19992 the template keyword should be optional. */
19993 else if (parser->scope
19994 && qualified_p
19995 && typename_p
19996 && cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID))
19998 cp_parser_parse_tentatively (parser);
20000 type = cp_parser_template_id (parser,
20001 /*template_keyword_p=*/true,
20002 /*check_dependency_p=*/true,
20003 none_type,
20004 /*is_declaration=*/false);
20005 /* This is handled below, so back off. */
20006 if (type && concept_check_p (type))
20007 cp_parser_simulate_error (parser);
20009 if (!cp_parser_parse_definitely (parser))
20010 type = NULL_TREE;
20011 else if (TREE_CODE (type) == TEMPLATE_ID_EXPR)
20012 type = make_typename_type (parser->scope, type, typename_type,
20013 /*complain=*/tf_error);
20014 else if (TREE_CODE (type) != TYPE_DECL)
20015 type = NULL_TREE;
20018 /* Otherwise, look for a type-name. */
20019 if (!type)
20021 if (cxx_dialect >= cxx17)
20022 cp_parser_parse_tentatively (parser);
20024 type = cp_parser_type_name (parser, (qualified_p && typename_p));
20026 if (cxx_dialect >= cxx17 && !cp_parser_parse_definitely (parser))
20027 type = NULL_TREE;
20030 if (!type && flag_concepts && decl_specs)
20032 /* Try for a type-constraint with template arguments. We check
20033 decl_specs here to avoid trying this for a functional cast. */
20035 cp_parser_parse_tentatively (parser);
20037 type = cp_parser_template_id (parser,
20038 /*template_keyword_p=*/false,
20039 /*check_dependency_p=*/true,
20040 none_type,
20041 /*is_declaration=*/false);
20042 if (type && concept_check_p (type))
20044 location_t loc = EXPR_LOCATION (type);
20045 type = cp_parser_placeholder_type_specifier (parser, loc,
20046 type, tent);
20047 if (tent && type == error_mark_node)
20048 /* Perhaps it's a concept-check expression. */
20049 cp_parser_simulate_error (parser);
20051 else
20052 cp_parser_simulate_error (parser);
20054 if (!cp_parser_parse_definitely (parser))
20055 type = NULL_TREE;
20058 if (!type && cxx_dialect >= cxx17)
20060 /* Try class template argument deduction or type-constraint without
20061 template arguments. */
20062 tree name = cp_parser_identifier (parser);
20063 if (name && TREE_CODE (name) == IDENTIFIER_NODE
20064 && parser->scope != error_mark_node)
20066 location_t loc
20067 = cp_lexer_previous_token (parser->lexer)->location;
20068 tree tmpl = cp_parser_lookup_name (parser, name,
20069 none_type,
20070 /*is_template=*/false,
20071 /*is_namespace=*/false,
20072 /*check_dependency=*/true,
20073 /*ambiguous_decls=*/NULL,
20074 token->location);
20075 if (tmpl && tmpl != error_mark_node
20076 && ctad_template_p (tmpl))
20077 type = make_template_placeholder (tmpl);
20078 else if (flag_concepts && tmpl && concept_definition_p (tmpl))
20079 type = cp_parser_placeholder_type_specifier (parser, loc,
20080 tmpl, tent);
20081 else
20083 type = error_mark_node;
20084 if (!cp_parser_simulate_error (parser))
20085 cp_parser_name_lookup_error (parser, name, tmpl,
20086 NLE_TYPE, token->location);
20089 else
20090 type = error_mark_node;
20093 /* If it didn't work out, we don't have a TYPE. */
20094 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
20095 && !cp_parser_parse_definitely (parser))
20096 type = NULL_TREE;
20098 /* Keep track of all name-lookups performed in class scopes. */
20099 if (type
20100 && !global_p
20101 && !qualified_p
20102 && TREE_CODE (type) == TYPE_DECL
20103 && identifier_p (DECL_NAME (type)))
20104 maybe_note_name_used_in_class (DECL_NAME (type), type);
20106 if (type && decl_specs)
20107 cp_parser_set_decl_spec_type (decl_specs, type,
20108 token,
20109 /*type_definition_p=*/false);
20112 /* If we didn't get a type-name, issue an error message. */
20113 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
20115 cp_parser_error (parser, "expected type-name");
20116 return error_mark_node;
20119 if (type && type != error_mark_node)
20121 /* See if TYPE is an Objective-C type, and if so, parse and
20122 accept any protocol references following it. Do this before
20123 the cp_parser_check_for_invalid_template_id() call, because
20124 Objective-C types can be followed by '<...>' which would
20125 enclose protocol names rather than template arguments, and so
20126 everything is fine. */
20127 if (c_dialect_objc () && !parser->scope
20128 && (objc_is_id (type) || objc_is_class_name (type)))
20130 tree protos = cp_parser_objc_protocol_refs_opt (parser);
20131 tree qual_type = objc_get_protocol_qualified_type (type, protos);
20133 /* Clobber the "unqualified" type previously entered into
20134 DECL_SPECS with the new, improved protocol-qualified version. */
20135 if (decl_specs)
20136 decl_specs->type = qual_type;
20138 return qual_type;
20141 /* There is no valid C++ program where a non-template type is
20142 followed by a "<". That usually indicates that the user
20143 thought that the type was a template. */
20144 cp_parser_check_for_invalid_template_id (parser, type,
20145 none_type,
20146 token->location);
20149 return type;
20152 /* Parse the remainder of a placholder-type-specifier.
20154 placeholder-type-specifier:
20155 type-constraint_opt auto
20156 type-constraint_opt decltype(auto)
20158 The raw form of the constraint is parsed in cp_parser_simple_type_specifier
20159 and passed as TMPL. This function converts TMPL to an actual type-constraint,
20160 parses the placeholder type, and performs some contextual syntactic analysis.
20162 LOC provides the location of the template name.
20164 TENTATIVE is true if the type-specifier parsing is tentative; in that case,
20165 don't give an error if TMPL isn't a valid type-constraint, as the template-id
20166 might actually be a concept-check,
20168 Note that the Concepts TS allows the auto or decltype(auto) to be
20169 omitted in a constrained-type-specifier. */
20171 static tree
20172 cp_parser_placeholder_type_specifier (cp_parser *parser, location_t loc,
20173 tree tmpl, bool tentative)
20175 if (tmpl == error_mark_node)
20176 return error_mark_node;
20178 tree orig_tmpl = tmpl;
20180 /* Get the arguments as written for subsequent analysis. */
20181 tree args = NULL_TREE;
20182 if (TREE_CODE (tmpl) == TEMPLATE_ID_EXPR)
20184 args = TREE_OPERAND (tmpl, 1);
20185 tmpl = TREE_OPERAND (tmpl, 0);
20187 else
20188 /* A concept-name with no arguments can't be an expression. */
20189 tentative = false;
20191 tsubst_flags_t complain = tentative ? tf_none : tf_warning_or_error;
20193 /* Get the concept and prototype parameter for the constraint. */
20194 tree_pair info = finish_type_constraints (tmpl, args, complain);
20195 tree con = info.first;
20196 tree proto = info.second;
20197 if (con == error_mark_node)
20198 return error_mark_node;
20200 /* As per the standard, require auto or decltype(auto), except in some
20201 cases (template parameter lists, -fconcepts-ts enabled). */
20202 cp_token *placeholder = NULL, *close_paren = NULL;
20203 if (cxx_dialect >= cxx20)
20205 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
20206 placeholder = cp_lexer_consume_token (parser->lexer);
20207 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DECLTYPE))
20209 placeholder = cp_lexer_consume_token (parser->lexer);
20210 matching_parens parens;
20211 parens.require_open (parser);
20212 cp_parser_require_keyword (parser, RID_AUTO, RT_AUTO);
20213 close_paren = parens.require_close (parser);
20217 /* A type constraint constrains a contextually determined type or type
20218 parameter pack. However, the Concepts TS does allow concepts
20219 to introduce non-type and template template parameters. */
20220 if (TREE_CODE (proto) != TYPE_DECL)
20222 if (!flag_concepts_ts
20223 || !processing_template_parmlist)
20225 if (!tentative)
20227 error_at (loc, "%qE does not constrain a type", DECL_NAME (con));
20228 inform (DECL_SOURCE_LOCATION (con), "concept defined here");
20230 return error_mark_node;
20234 /* In a template parameter list, a type-parameter can be introduced
20235 by type-constraints alone. */
20236 if (processing_template_parmlist && !placeholder)
20238 /* In a default argument we may not be creating new parameters. */
20239 if (parser->local_variables_forbidden_p & LOCAL_VARS_FORBIDDEN)
20241 /* If this assert turns out to be false, do error() instead. */
20242 gcc_assert (tentative);
20243 return error_mark_node;
20245 return build_constrained_parameter (con, proto, args);
20248 /* Diagnose issues placeholder issues. */
20249 if (!flag_concepts_ts
20250 && !parser->in_result_type_constraint_p
20251 && !placeholder)
20253 if (tentative)
20254 /* Perhaps it's a concept-check expression (c++/91073). */
20255 return error_mark_node;
20257 tree id = build_nt (TEMPLATE_ID_EXPR, tmpl, args);
20258 tree expr = DECL_P (orig_tmpl) ? DECL_NAME (con) : id;
20259 error_at (input_location,
20260 "expected %<auto%> or %<decltype(auto)%> after %qE", expr);
20261 /* Fall through. This is an error of omission. */
20263 else if (parser->in_result_type_constraint_p && placeholder)
20265 /* A trailing return type only allows type-constraints. */
20266 error_at (input_location,
20267 "unexpected placeholder in constrained result type");
20270 /* In a parameter-declaration-clause, a placeholder-type-specifier
20271 results in an invented template parameter. */
20272 if (parser->auto_is_implicit_function_template_parm_p)
20274 if (close_paren)
20276 location_t loc = make_location (placeholder->location,
20277 placeholder->location,
20278 close_paren->location);
20279 error_at (loc, "cannot declare a parameter with %<decltype(auto)%>");
20280 return error_mark_node;
20282 tree parm = build_constrained_parameter (con, proto, args);
20283 return synthesize_implicit_template_parm (parser, parm);
20286 /* Determine if the type should be deduced using template argument
20287 deduction or decltype deduction. Note that the latter is always
20288 used for type-constraints in trailing return types. */
20289 bool decltype_p = placeholder
20290 ? placeholder->keyword == RID_DECLTYPE
20291 : parser->in_result_type_constraint_p;
20293 /* Otherwise, this is the type of a variable or return type. */
20294 if (decltype_p)
20295 return make_constrained_decltype_auto (con, args);
20296 else
20297 return make_constrained_auto (con, args);
20300 /* Parse a type-name.
20302 type-name:
20303 class-name
20304 enum-name
20305 typedef-name
20306 simple-template-id [in c++0x]
20308 enum-name:
20309 identifier
20311 typedef-name:
20312 identifier
20314 Concepts:
20316 type-name:
20317 concept-name
20318 partial-concept-id
20320 concept-name:
20321 identifier
20323 Returns a TYPE_DECL for the type. */
20325 static tree
20326 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
20328 tree type_decl;
20330 /* We can't know yet whether it is a class-name or not. */
20331 cp_parser_parse_tentatively (parser);
20332 /* Try a class-name. */
20333 type_decl = cp_parser_class_name (parser,
20334 typename_keyword_p,
20335 /*template_keyword_p=*/false,
20336 none_type,
20337 /*check_dependency_p=*/true,
20338 /*class_head_p=*/false,
20339 /*is_declaration=*/false);
20340 /* If it's not a class-name, keep looking. */
20341 if (!cp_parser_parse_definitely (parser))
20343 if (cxx_dialect < cxx11)
20344 /* It must be a typedef-name or an enum-name. */
20345 return cp_parser_nonclass_name (parser);
20347 cp_parser_parse_tentatively (parser);
20348 /* It is either a simple-template-id representing an
20349 instantiation of an alias template... */
20350 type_decl = cp_parser_template_id (parser,
20351 /*template_keyword_p=*/false,
20352 /*check_dependency_p=*/true,
20353 none_type,
20354 /*is_declaration=*/false);
20355 /* Note that this must be an instantiation of an alias template
20356 because [temp.names]/6 says:
20358 A template-id that names an alias template specialization
20359 is a type-name.
20361 Whereas [temp.names]/7 says:
20363 A simple-template-id that names a class template
20364 specialization is a class-name.
20366 With concepts, this could also be a partial-concept-id that
20367 declares a non-type template parameter. */
20368 if (type_decl != NULL_TREE
20369 && TREE_CODE (type_decl) == TYPE_DECL
20370 && TYPE_DECL_ALIAS_P (type_decl))
20371 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
20372 else
20373 cp_parser_simulate_error (parser);
20375 if (!cp_parser_parse_definitely (parser))
20376 /* ... Or a typedef-name or an enum-name. */
20377 return cp_parser_nonclass_name (parser);
20380 return type_decl;
20383 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
20384 or a concept-name.
20386 enum-name:
20387 identifier
20389 typedef-name:
20390 identifier
20392 concept-name:
20393 identifier
20395 Returns a TYPE_DECL for the type. */
20397 static tree
20398 cp_parser_nonclass_name (cp_parser* parser)
20400 tree type_decl;
20401 tree identifier;
20403 cp_token *token = cp_lexer_peek_token (parser->lexer);
20404 identifier = cp_parser_identifier (parser);
20405 if (identifier == error_mark_node)
20406 return error_mark_node;
20408 /* Look up the type-name. */
20409 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
20411 type_decl = strip_using_decl (type_decl);
20413 if (TREE_CODE (type_decl) != TYPE_DECL
20414 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
20416 /* See if this is an Objective-C type. */
20417 tree protos = cp_parser_objc_protocol_refs_opt (parser);
20418 tree type = objc_get_protocol_qualified_type (identifier, protos);
20419 if (type)
20420 type_decl = TYPE_NAME (type);
20423 /* Issue an error if we did not find a type-name. */
20424 if (TREE_CODE (type_decl) != TYPE_DECL
20425 /* In Objective-C, we have the complication that class names are
20426 normally type names and start declarations (eg, the
20427 "NSObject" in "NSObject *object;"), but can be used in an
20428 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
20429 is an expression. So, a classname followed by a dot is not a
20430 valid type-name. */
20431 || (objc_is_class_name (TREE_TYPE (type_decl))
20432 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
20434 if (!cp_parser_simulate_error (parser))
20435 cp_parser_name_lookup_error (parser, identifier, type_decl,
20436 NLE_TYPE, token->location);
20437 return error_mark_node;
20439 /* Remember that the name was used in the definition of the
20440 current class so that we can check later to see if the
20441 meaning would have been different after the class was
20442 entirely defined. */
20443 else if (type_decl != error_mark_node
20444 && !parser->scope)
20445 maybe_note_name_used_in_class (identifier, type_decl);
20447 return type_decl;
20450 /* Parse an elaborated-type-specifier. Note that the grammar given
20451 here incorporates the resolution to DR68.
20453 elaborated-type-specifier:
20454 class-key :: [opt] nested-name-specifier [opt] identifier
20455 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
20456 enum-key :: [opt] nested-name-specifier [opt] identifier
20457 typename :: [opt] nested-name-specifier identifier
20458 typename :: [opt] nested-name-specifier template [opt]
20459 template-id
20461 GNU extension:
20463 elaborated-type-specifier:
20464 class-key attributes :: [opt] nested-name-specifier [opt] identifier
20465 class-key attributes :: [opt] nested-name-specifier [opt]
20466 template [opt] template-id
20467 enum attributes :: [opt] nested-name-specifier [opt] identifier
20469 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
20470 declared `friend'. If IS_DECLARATION is TRUE, then this
20471 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
20472 something is being declared.
20474 Returns the TYPE specified. */
20476 static tree
20477 cp_parser_elaborated_type_specifier (cp_parser* parser,
20478 bool is_friend,
20479 bool is_declaration)
20481 enum tag_types tag_type;
20482 tree identifier;
20483 tree type = NULL_TREE;
20484 tree attributes = NULL_TREE;
20485 tree globalscope;
20486 cp_token *token = NULL;
20488 /* For class and enum types the location of the class-key or enum-key. */
20489 location_t key_loc = cp_lexer_peek_token (parser->lexer)->location;
20490 /* For a scoped enum, the 'class' or 'struct' keyword id. */
20491 rid scoped_key = RID_MAX;
20493 /* See if we're looking at the `enum' keyword. */
20494 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
20496 /* Consume the `enum' token. */
20497 cp_lexer_consume_token (parser->lexer);
20498 /* Remember that it's an enumeration type. */
20499 tag_type = enum_type;
20500 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
20501 enums) is used here. */
20502 cp_token *token = cp_lexer_peek_token (parser->lexer);
20503 if (cp_parser_is_keyword (token, scoped_key = RID_CLASS)
20504 || cp_parser_is_keyword (token, scoped_key = RID_STRUCT))
20506 location_t loc = token->location;
20507 gcc_rich_location richloc (loc);
20508 richloc.add_range (input_location);
20509 richloc.add_fixit_remove ();
20510 pedwarn (&richloc, 0, "elaborated-type-specifier for "
20511 "a scoped enum must not use the %qD keyword",
20512 token->u.value);
20513 /* Consume the `struct' or `class' and parse it anyway. */
20514 cp_lexer_consume_token (parser->lexer);
20515 /* Create a combined location for the whole scoped-enum-key. */
20516 key_loc = make_location (key_loc, key_loc, loc);
20518 else
20519 scoped_key = RID_MAX;
20521 /* Parse the attributes. */
20522 attributes = cp_parser_attributes_opt (parser);
20524 /* Or, it might be `typename'. */
20525 else if (cp_lexer_next_token_is_keyword (parser->lexer,
20526 RID_TYPENAME))
20528 /* Consume the `typename' token. */
20529 cp_lexer_consume_token (parser->lexer);
20530 /* Remember that it's a `typename' type. */
20531 tag_type = typename_type;
20533 /* Otherwise it must be a class-key. */
20534 else
20536 key_loc = cp_lexer_peek_token (parser->lexer)->location;
20537 tag_type = cp_parser_class_key (parser);
20538 if (tag_type == none_type)
20539 return error_mark_node;
20540 /* Parse the attributes. */
20541 attributes = cp_parser_attributes_opt (parser);
20544 /* Look for the `::' operator. */
20545 globalscope = cp_parser_global_scope_opt (parser,
20546 /*current_scope_valid_p=*/false);
20547 /* Look for the nested-name-specifier. */
20548 tree nested_name_specifier;
20549 if (tag_type == typename_type && !globalscope)
20551 nested_name_specifier
20552 = cp_parser_nested_name_specifier (parser,
20553 /*typename_keyword_p=*/true,
20554 /*check_dependency_p=*/true,
20555 /*type_p=*/true,
20556 is_declaration);
20557 if (!nested_name_specifier)
20558 return error_mark_node;
20560 else
20561 /* Even though `typename' is not present, the proposed resolution
20562 to Core Issue 180 says that in `class A<T>::B', `B' should be
20563 considered a type-name, even if `A<T>' is dependent. */
20564 nested_name_specifier
20565 = cp_parser_nested_name_specifier_opt (parser,
20566 /*typename_keyword_p=*/true,
20567 /*check_dependency_p=*/true,
20568 /*type_p=*/true,
20569 is_declaration);
20570 /* For everything but enumeration types, consider a template-id.
20571 For an enumeration type, consider only a plain identifier. */
20572 if (tag_type != enum_type)
20574 bool template_p = false;
20575 tree decl;
20577 /* Allow the `template' keyword. */
20578 template_p = cp_parser_optional_template_keyword (parser);
20579 /* If we didn't see `template', we don't know if there's a
20580 template-id or not. */
20581 if (!template_p)
20582 cp_parser_parse_tentatively (parser);
20583 /* The `template' keyword must follow a nested-name-specifier. */
20584 else if (!nested_name_specifier && !globalscope)
20586 cp_parser_error (parser, "%<template%> must follow a nested-"
20587 "name-specifier");
20588 return error_mark_node;
20591 /* Parse the template-id. */
20592 token = cp_lexer_peek_token (parser->lexer);
20593 decl = cp_parser_template_id (parser, template_p,
20594 /*check_dependency_p=*/true,
20595 tag_type,
20596 is_declaration);
20597 /* If we didn't find a template-id, look for an ordinary
20598 identifier. */
20599 if (!template_p && !cp_parser_parse_definitely (parser))
20601 /* We can get here when cp_parser_template_id, called by
20602 cp_parser_class_name with tag_type == none_type, succeeds
20603 and caches a BASELINK. Then, when called again here,
20604 instead of failing and returning an error_mark_node
20605 returns it (see template/typename17.C in C++11).
20606 ??? Could we diagnose this earlier? */
20607 else if (tag_type == typename_type && BASELINK_P (decl))
20609 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
20610 type = error_mark_node;
20612 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
20613 in effect, then we must assume that, upon instantiation, the
20614 template will correspond to a class. */
20615 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
20616 && tag_type == typename_type)
20617 type = make_typename_type (parser->scope, decl,
20618 typename_type,
20619 /*complain=*/tf_error);
20620 /* If the `typename' keyword is in effect and DECL is not a type
20621 decl, then type is non existent. */
20622 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
20624 else if (TREE_CODE (decl) == TYPE_DECL)
20626 type = check_elaborated_type_specifier (tag_type, decl,
20627 /*allow_template_p=*/true);
20629 /* If the next token is a semicolon, this must be a specialization,
20630 instantiation, or friend declaration. Check the scope while we
20631 still know whether or not we had a nested-name-specifier. */
20632 if (type != error_mark_node
20633 && !nested_name_specifier && !is_friend
20634 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20635 check_unqualified_spec_or_inst (type, token->location);
20637 else if (decl == error_mark_node)
20638 type = error_mark_node;
20641 if (!type)
20643 token = cp_lexer_peek_token (parser->lexer);
20644 identifier = cp_parser_identifier (parser);
20646 if (identifier == error_mark_node)
20648 parser->scope = NULL_TREE;
20649 return error_mark_node;
20652 /* For a `typename', we needn't call xref_tag. */
20653 if (tag_type == typename_type
20654 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
20655 return cp_parser_make_typename_type (parser, identifier,
20656 token->location);
20658 /* Template parameter lists apply only if we are not within a
20659 function parameter list. */
20660 bool template_parm_lists_apply
20661 = parser->num_template_parameter_lists;
20662 if (template_parm_lists_apply)
20663 for (cp_binding_level *s = current_binding_level;
20664 s && s->kind != sk_template_parms;
20665 s = s->level_chain)
20666 if (s->kind == sk_function_parms)
20667 template_parm_lists_apply = false;
20669 /* Look up a qualified name in the usual way. */
20670 if (parser->scope)
20672 tree decl;
20673 tree ambiguous_decls;
20675 decl = cp_parser_lookup_name (parser, identifier,
20676 tag_type,
20677 /*is_template=*/false,
20678 /*is_namespace=*/false,
20679 /*check_dependency=*/true,
20680 &ambiguous_decls,
20681 token->location);
20683 /* If the lookup was ambiguous, an error will already have been
20684 issued. */
20685 if (ambiguous_decls)
20686 return error_mark_node;
20688 /* If we are parsing friend declaration, DECL may be a
20689 TEMPLATE_DECL tree node here. However, we need to check
20690 whether this TEMPLATE_DECL results in valid code. Consider
20691 the following example:
20693 namespace N {
20694 template <class T> class C {};
20696 class X {
20697 template <class T> friend class N::C; // #1, valid code
20699 template <class T> class Y {
20700 friend class N::C; // #2, invalid code
20703 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
20704 name lookup of `N::C'. We see that friend declaration must
20705 be template for the code to be valid. Note that
20706 processing_template_decl does not work here since it is
20707 always 1 for the above two cases. */
20709 decl = (cp_parser_maybe_treat_template_as_class
20710 (decl, /*tag_name_p=*/is_friend
20711 && template_parm_lists_apply));
20713 if (TREE_CODE (decl) != TYPE_DECL)
20715 cp_parser_diagnose_invalid_type_name (parser,
20716 identifier,
20717 token->location);
20718 return error_mark_node;
20721 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
20723 bool allow_template = (template_parm_lists_apply
20724 || DECL_SELF_REFERENCE_P (decl));
20725 type = check_elaborated_type_specifier (tag_type, decl,
20726 allow_template);
20728 if (type == error_mark_node)
20729 return error_mark_node;
20732 /* Forward declarations of nested types, such as
20734 class C1::C2;
20735 class C1::C2::C3;
20737 are invalid unless all components preceding the final '::'
20738 are complete. If all enclosing types are complete, these
20739 declarations become merely pointless.
20741 Invalid forward declarations of nested types are errors
20742 caught elsewhere in parsing. Those that are pointless arrive
20743 here. */
20745 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20746 && !is_friend && is_declaration
20747 && !processing_explicit_instantiation)
20748 warning (0, "declaration %qD does not declare anything", decl);
20750 type = TREE_TYPE (decl);
20752 else
20754 /* An elaborated-type-specifier sometimes introduces a new type and
20755 sometimes names an existing type. Normally, the rule is that it
20756 introduces a new type only if there is not an existing type of
20757 the same name already in scope. For example, given:
20759 struct S {};
20760 void f() { struct S s; }
20762 the `struct S' in the body of `f' is the same `struct S' as in
20763 the global scope; the existing definition is used. However, if
20764 there were no global declaration, this would introduce a new
20765 local class named `S'.
20767 An exception to this rule applies to the following code:
20769 namespace N { struct S; }
20771 Here, the elaborated-type-specifier names a new type
20772 unconditionally; even if there is already an `S' in the
20773 containing scope this declaration names a new type.
20774 This exception only applies if the elaborated-type-specifier
20775 forms the complete declaration:
20777 [class.name]
20779 A declaration consisting solely of `class-key identifier ;' is
20780 either a redeclaration of the name in the current scope or a
20781 forward declaration of the identifier as a class name. It
20782 introduces the name into the current scope.
20784 We are in this situation precisely when the next token is a `;'.
20786 An exception to the exception is that a `friend' declaration does
20787 *not* name a new type; i.e., given:
20789 struct S { friend struct T; };
20791 `T' is not a new type in the scope of `S'.
20793 Also, `new struct S' or `sizeof (struct S)' never results in the
20794 definition of a new type; a new type can only be declared in a
20795 declaration context. */
20797 TAG_how how;
20799 if (is_friend)
20800 /* Friends have special name lookup rules. */
20801 how = TAG_how::HIDDEN_FRIEND;
20802 else if (is_declaration
20803 && cp_lexer_next_token_is (parser->lexer,
20804 CPP_SEMICOLON))
20805 /* This is a `class-key identifier ;' */
20806 how = TAG_how::CURRENT_ONLY;
20807 else
20808 how = TAG_how::GLOBAL;
20810 bool template_p =
20811 (template_parm_lists_apply
20812 && (cp_parser_next_token_starts_class_definition_p (parser)
20813 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
20814 /* An unqualified name was used to reference this type, so
20815 there were no qualifying templates. */
20816 if (template_parm_lists_apply
20817 && !cp_parser_check_template_parameters (parser,
20818 /*num_templates=*/0,
20819 /*template_id*/false,
20820 token->location,
20821 /*declarator=*/NULL))
20822 return error_mark_node;
20824 type = xref_tag (tag_type, identifier, how, template_p);
20828 if (type == error_mark_node)
20829 return error_mark_node;
20831 /* Allow attributes on forward declarations of classes. */
20832 if (attributes)
20834 if (TREE_CODE (type) == TYPENAME_TYPE)
20835 warning (OPT_Wattributes,
20836 "attributes ignored on uninstantiated type");
20837 else if (tag_type != enum_type
20838 && TREE_CODE (type) != BOUND_TEMPLATE_TEMPLATE_PARM
20839 && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
20840 && ! processing_explicit_instantiation)
20841 warning (OPT_Wattributes,
20842 "attributes ignored on template instantiation");
20843 else if (is_friend && cxx11_attribute_p (attributes))
20845 if (warning (OPT_Wattributes, "attribute ignored"))
20846 inform (input_location, "an attribute that appertains to a friend "
20847 "declaration that is not a definition is ignored");
20849 else if (is_declaration && cp_parser_declares_only_class_p (parser))
20850 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
20851 else
20852 warning (OPT_Wattributes,
20853 "attributes ignored on elaborated-type-specifier that is "
20854 "not a forward declaration");
20857 if (tag_type == enum_type)
20858 cp_parser_maybe_warn_enum_key (parser, key_loc, type, scoped_key);
20859 else
20861 /* Diagnose class/struct/union mismatches. IS_DECLARATION is false
20862 for alias definition. */
20863 bool decl_class = (is_declaration
20864 && cp_parser_declares_only_class_p (parser));
20865 cp_parser_check_class_key (parser, key_loc, tag_type, type, false,
20866 decl_class);
20868 /* Indicate whether this class was declared as a `class' or as a
20869 `struct'. */
20870 if (CLASS_TYPE_P (type) && !currently_open_class (type))
20871 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
20874 /* A "<" cannot follow an elaborated type specifier. If that
20875 happens, the user was probably trying to form a template-id. */
20876 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
20877 token->location);
20879 return type;
20882 /* Parse an enum-specifier.
20884 enum-specifier:
20885 enum-head { enumerator-list [opt] }
20886 enum-head { enumerator-list , } [C++0x]
20888 enum-head:
20889 enum-key identifier [opt] enum-base [opt]
20890 enum-key nested-name-specifier identifier enum-base [opt]
20892 enum-key:
20893 enum
20894 enum class [C++0x]
20895 enum struct [C++0x]
20897 enum-base: [C++0x]
20898 : type-specifier-seq
20900 opaque-enum-specifier:
20901 enum-key identifier enum-base [opt] ;
20903 GNU Extensions:
20904 enum-key attributes[opt] identifier [opt] enum-base [opt]
20905 { enumerator-list [opt] }attributes[opt]
20906 enum-key attributes[opt] identifier [opt] enum-base [opt]
20907 { enumerator-list, }attributes[opt] [C++0x]
20909 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
20910 if the token stream isn't an enum-specifier after all. */
20912 static tree
20913 cp_parser_enum_specifier (cp_parser* parser)
20915 tree identifier;
20916 tree type = NULL_TREE;
20917 tree prev_scope;
20918 tree nested_name_specifier = NULL_TREE;
20919 tree attributes;
20920 bool scoped_enum_p = false;
20921 bool has_underlying_type = false;
20922 bool nested_being_defined = false;
20923 bool new_value_list = false;
20924 bool is_new_type = false;
20925 bool is_unnamed = false;
20926 tree underlying_type = NULL_TREE;
20927 cp_token *type_start_token = NULL;
20928 auto cleanup = make_temp_override (parser->colon_corrects_to_scope_p, false);
20930 /* Parse tentatively so that we can back up if we don't find a
20931 enum-specifier. */
20932 cp_parser_parse_tentatively (parser);
20934 /* Caller guarantees that the current token is 'enum', an identifier
20935 possibly follows, and the token after that is an opening brace.
20936 If we don't have an identifier, fabricate an anonymous name for
20937 the enumeration being defined. */
20938 cp_lexer_consume_token (parser->lexer);
20940 /* Parse the "class" or "struct", which indicates a scoped
20941 enumeration type in C++0x. */
20942 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
20943 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
20945 if (cxx_dialect < cxx11)
20946 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
20948 /* Consume the `struct' or `class' token. */
20949 cp_lexer_consume_token (parser->lexer);
20951 scoped_enum_p = true;
20954 attributes = cp_parser_attributes_opt (parser);
20956 /* Clear the qualification. */
20957 parser->scope = NULL_TREE;
20958 parser->qualifying_scope = NULL_TREE;
20959 parser->object_scope = NULL_TREE;
20961 /* Figure out in what scope the declaration is being placed. */
20962 prev_scope = current_scope ();
20964 type_start_token = cp_lexer_peek_token (parser->lexer);
20966 push_deferring_access_checks (dk_no_check);
20967 nested_name_specifier
20968 = cp_parser_nested_name_specifier_opt (parser,
20969 /*typename_keyword_p=*/true,
20970 /*check_dependency_p=*/false,
20971 /*type_p=*/false,
20972 /*is_declaration=*/false);
20974 if (nested_name_specifier)
20976 tree name;
20978 identifier = cp_parser_identifier (parser);
20979 name = cp_parser_lookup_name (parser, identifier,
20980 enum_type,
20981 /*is_template=*/false,
20982 /*is_namespace=*/false,
20983 /*check_dependency=*/true,
20984 /*ambiguous_decls=*/NULL,
20985 input_location);
20986 if (name && name != error_mark_node)
20988 type = TREE_TYPE (name);
20989 if (TREE_CODE (type) == TYPENAME_TYPE)
20991 /* Are template enums allowed in ISO? */
20992 if (template_parm_scope_p ())
20993 pedwarn (type_start_token->location, OPT_Wpedantic,
20994 "%qD is an enumeration template", name);
20995 /* ignore a typename reference, for it will be solved by name
20996 in start_enum. */
20997 type = NULL_TREE;
21000 else if (nested_name_specifier == error_mark_node)
21001 /* We already issued an error. */;
21002 else
21004 error_at (type_start_token->location,
21005 "%qD does not name an enumeration in %qT",
21006 identifier, nested_name_specifier);
21007 nested_name_specifier = error_mark_node;
21010 else
21012 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21013 identifier = cp_parser_identifier (parser);
21014 else
21016 identifier = make_anon_name ();
21017 is_unnamed = true;
21018 if (scoped_enum_p)
21019 error_at (type_start_token->location,
21020 "unnamed scoped enum is not allowed");
21023 pop_deferring_access_checks ();
21025 /* Check for the `:' that denotes a specified underlying type in C++0x.
21026 Note that a ':' could also indicate a bitfield width, however. */
21027 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
21029 cp_decl_specifier_seq type_specifiers;
21031 /* Consume the `:'. */
21032 cp_lexer_consume_token (parser->lexer);
21034 auto tdf
21035 = make_temp_override (parser->type_definition_forbidden_message,
21036 G_("types may not be defined in enum-base"));
21038 /* Parse the type-specifier-seq. */
21039 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
21040 /*is_declaration=*/false,
21041 /*is_trailing_return=*/false,
21042 &type_specifiers);
21044 /* At this point this is surely not elaborated type specifier. */
21045 if (!cp_parser_parse_definitely (parser))
21046 return NULL_TREE;
21048 if (cxx_dialect < cxx11)
21049 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
21051 has_underlying_type = true;
21053 /* If that didn't work, stop. */
21054 if (type_specifiers.type != error_mark_node)
21056 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
21057 /*initialized=*/0, NULL);
21058 if (underlying_type == error_mark_node
21059 || check_for_bare_parameter_packs (underlying_type))
21060 underlying_type = NULL_TREE;
21064 /* Look for the `{' but don't consume it yet. */
21065 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21067 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
21069 if (has_underlying_type)
21070 cp_parser_commit_to_tentative_parse (parser);
21071 cp_parser_error (parser, "expected %<{%>");
21072 if (has_underlying_type)
21073 return error_mark_node;
21075 /* An opaque-enum-specifier must have a ';' here. */
21076 if ((scoped_enum_p || underlying_type)
21077 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21079 if (has_underlying_type)
21080 cp_parser_commit_to_tentative_parse (parser);
21081 cp_parser_error (parser, "expected %<;%> or %<{%>");
21082 if (has_underlying_type)
21083 return error_mark_node;
21087 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
21088 return NULL_TREE;
21090 if (nested_name_specifier)
21092 if (CLASS_TYPE_P (nested_name_specifier))
21094 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
21095 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
21096 push_scope (nested_name_specifier);
21098 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
21099 push_nested_namespace (nested_name_specifier);
21102 /* Issue an error message if type-definitions are forbidden here. */
21103 if (!cp_parser_check_type_definition (parser))
21104 type = error_mark_node;
21105 else
21106 /* Create the new type. We do this before consuming the opening
21107 brace so the enum will be recorded as being on the line of its
21108 tag (or the 'enum' keyword, if there is no tag). */
21109 type = start_enum (identifier, type, underlying_type,
21110 attributes, scoped_enum_p, &is_new_type);
21112 /* If the next token is not '{' it is an opaque-enum-specifier or an
21113 elaborated-type-specifier. */
21114 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21116 auto_timevar tv (TV_PARSE_ENUM);
21118 if (nested_name_specifier
21119 && nested_name_specifier != error_mark_node)
21121 /* The following catches invalid code such as:
21122 enum class S<int>::E { A, B, C }; */
21123 if (!processing_specialization
21124 && CLASS_TYPE_P (nested_name_specifier)
21125 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
21126 error_at (type_start_token->location, "cannot add an enumerator "
21127 "list to a template instantiation");
21129 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
21131 error_at (type_start_token->location,
21132 "%<%T::%E%> has not been declared",
21133 TYPE_CONTEXT (nested_name_specifier),
21134 nested_name_specifier);
21135 type = error_mark_node;
21137 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
21138 && !CLASS_TYPE_P (nested_name_specifier))
21140 error_at (type_start_token->location, "nested name specifier "
21141 "%qT for enum declaration does not name a class "
21142 "or namespace", nested_name_specifier);
21143 type = error_mark_node;
21145 /* If that scope does not contain the scope in which the
21146 class was originally declared, the program is invalid. */
21147 else if (prev_scope && !is_ancestor (prev_scope,
21148 nested_name_specifier))
21150 if (at_namespace_scope_p ())
21151 error_at (type_start_token->location,
21152 "declaration of %qD in namespace %qD which does not "
21153 "enclose %qD",
21154 type, prev_scope, nested_name_specifier);
21155 else
21156 error_at (type_start_token->location,
21157 "declaration of %qD in %qD which does not "
21158 "enclose %qD",
21159 type, prev_scope, nested_name_specifier);
21160 type = error_mark_node;
21162 /* If that scope is the scope where the declaration is being placed
21163 the program is invalid. */
21164 else if (CLASS_TYPE_P (nested_name_specifier)
21165 && CLASS_TYPE_P (prev_scope)
21166 && same_type_p (nested_name_specifier, prev_scope))
21168 permerror (type_start_token->location,
21169 "extra qualification not allowed");
21170 nested_name_specifier = NULL_TREE;
21174 if (scoped_enum_p)
21175 begin_scope (sk_scoped_enum, type);
21177 /* Consume the opening brace. */
21178 matching_braces braces;
21179 braces.consume_open (parser);
21181 if (type == error_mark_node)
21182 ; /* Nothing to add */
21183 else if (OPAQUE_ENUM_P (type)
21184 || (cxx_dialect > cxx98 && processing_specialization))
21186 new_value_list = true;
21187 SET_OPAQUE_ENUM_P (type, false);
21188 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
21190 else
21192 error_at (type_start_token->location,
21193 "multiple definition of %q#T", type);
21194 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
21195 "previous definition here");
21196 type = error_mark_node;
21199 if (type == error_mark_node)
21200 cp_parser_skip_to_end_of_block_or_statement (parser);
21201 /* If the next token is not '}', then there are some enumerators. */
21202 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21204 if (is_unnamed && !scoped_enum_p
21205 /* Don't warn for enum {} a; here. */
21206 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SEMICOLON))
21207 pedwarn (type_start_token->location, OPT_Wpedantic,
21208 "ISO C++ forbids empty unnamed enum");
21210 else
21212 /* We've seen a '{' so we know we're in an enum-specifier.
21213 Commit to any tentative parse to get syntax errors. */
21214 cp_parser_commit_to_tentative_parse (parser);
21215 cp_parser_enumerator_list (parser, type);
21218 /* Consume the final '}'. */
21219 braces.require_close (parser);
21221 if (scoped_enum_p)
21222 finish_scope ();
21224 else
21226 /* If a ';' follows, then it is an opaque-enum-specifier
21227 and additional restrictions apply. */
21228 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21230 if (is_unnamed)
21231 error_at (type_start_token->location,
21232 "opaque-enum-specifier without name");
21233 else if (nested_name_specifier)
21234 error_at (type_start_token->location,
21235 "opaque-enum-specifier must use a simple identifier");
21239 /* Look for trailing attributes to apply to this enumeration, and
21240 apply them if appropriate. */
21241 if (cp_parser_allow_gnu_extensions_p (parser))
21243 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
21244 cplus_decl_attributes (&type,
21245 trailing_attr,
21246 (int) ATTR_FLAG_TYPE_IN_PLACE);
21249 /* Finish up the enumeration. */
21250 if (type != error_mark_node)
21252 if (new_value_list)
21253 finish_enum_value_list (type);
21254 if (is_new_type)
21255 finish_enum (type);
21258 if (nested_name_specifier)
21260 if (CLASS_TYPE_P (nested_name_specifier))
21262 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
21263 pop_scope (nested_name_specifier);
21265 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
21266 pop_nested_namespace (nested_name_specifier);
21268 return type;
21271 /* Parse an enumerator-list. The enumerators all have the indicated
21272 TYPE.
21274 enumerator-list:
21275 enumerator-definition
21276 enumerator-list , enumerator-definition */
21278 static void
21279 cp_parser_enumerator_list (cp_parser* parser, tree type)
21281 while (true)
21283 /* Parse an enumerator-definition. */
21284 cp_parser_enumerator_definition (parser, type);
21286 /* If the next token is not a ',', we've reached the end of
21287 the list. */
21288 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21289 break;
21290 /* Otherwise, consume the `,' and keep going. */
21291 cp_lexer_consume_token (parser->lexer);
21292 /* If the next token is a `}', there is a trailing comma. */
21293 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21295 if (cxx_dialect < cxx11)
21296 pedwarn (input_location, OPT_Wpedantic,
21297 "comma at end of enumerator list");
21298 break;
21303 /* Parse an enumerator-definition. The enumerator has the indicated
21304 TYPE.
21306 enumerator-definition:
21307 enumerator
21308 enumerator = constant-expression
21310 enumerator:
21311 identifier
21313 GNU Extensions:
21315 enumerator-definition:
21316 enumerator attributes [opt]
21317 enumerator attributes [opt] = constant-expression */
21319 static void
21320 cp_parser_enumerator_definition (cp_parser* parser, tree type)
21322 tree identifier;
21323 tree value;
21324 location_t loc;
21326 /* Save the input location because we are interested in the location
21327 of the identifier and not the location of the explicit value. */
21328 loc = cp_lexer_peek_token (parser->lexer)->location;
21330 /* Look for the identifier. */
21331 identifier = cp_parser_identifier (parser);
21332 if (identifier == error_mark_node)
21333 return;
21335 /* Parse any specified attributes. */
21336 tree attrs = cp_parser_attributes_opt (parser);
21338 /* If the next token is an '=', then there is an explicit value. */
21339 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21341 /* Consume the `=' token. */
21342 cp_lexer_consume_token (parser->lexer);
21343 /* Parse the value. */
21344 value = cp_parser_constant_expression (parser);
21346 else
21347 value = NULL_TREE;
21349 /* If we are processing a template, make sure the initializer of the
21350 enumerator doesn't contain any bare template parameter pack. */
21351 if (current_lambda_expr ())
21353 /* In a lambda it should work, but doesn't currently. */
21354 if (uses_parameter_packs (value))
21356 sorry ("unexpanded parameter pack in enumerator in lambda");
21357 value = error_mark_node;
21360 else if (check_for_bare_parameter_packs (value))
21361 value = error_mark_node;
21363 /* Create the enumerator. */
21364 build_enumerator (identifier, value, type, attrs, loc);
21367 /* Parse a namespace-name.
21369 namespace-name:
21370 original-namespace-name
21371 namespace-alias
21373 Returns the NAMESPACE_DECL for the namespace. */
21375 static tree
21376 cp_parser_namespace_name (cp_parser* parser)
21378 tree identifier;
21379 tree namespace_decl;
21381 cp_token *token = cp_lexer_peek_token (parser->lexer);
21383 /* Get the name of the namespace. */
21384 identifier = cp_parser_identifier (parser);
21385 if (identifier == error_mark_node)
21386 return error_mark_node;
21388 /* Look up the identifier in the currently active scope. Look only
21389 for namespaces, due to:
21391 [basic.lookup.udir]
21393 When looking up a namespace-name in a using-directive or alias
21394 definition, only namespace names are considered.
21396 And:
21398 [basic.lookup.qual]
21400 During the lookup of a name preceding the :: scope resolution
21401 operator, object, function, and enumerator names are ignored.
21403 (Note that cp_parser_qualifying_entity only calls this
21404 function if the token after the name is the scope resolution
21405 operator.) */
21406 namespace_decl = cp_parser_lookup_name (parser, identifier,
21407 none_type,
21408 /*is_template=*/false,
21409 /*is_namespace=*/true,
21410 /*check_dependency=*/true,
21411 /*ambiguous_decls=*/NULL,
21412 token->location);
21413 /* If it's not a namespace, issue an error. */
21414 if (namespace_decl == error_mark_node
21415 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
21417 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21419 auto_diagnostic_group d;
21420 name_hint hint;
21421 if (namespace_decl == error_mark_node
21422 && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
21423 hint = suggest_alternative_in_explicit_scope (token->location,
21424 identifier,
21425 parser->scope);
21426 if (const char *suggestion = hint.suggestion ())
21428 gcc_rich_location richloc (token->location);
21429 richloc.add_fixit_replace (suggestion);
21430 error_at (&richloc,
21431 "%qD is not a namespace-name; did you mean %qs?",
21432 identifier, suggestion);
21434 else
21435 error_at (token->location, "%qD is not a namespace-name",
21436 identifier);
21438 else
21439 cp_parser_error (parser, "expected namespace-name");
21440 namespace_decl = error_mark_node;
21443 return namespace_decl;
21446 /* Parse a namespace-definition.
21448 namespace-definition:
21449 named-namespace-definition
21450 unnamed-namespace-definition
21452 named-namespace-definition:
21453 original-namespace-definition
21454 extension-namespace-definition
21456 original-namespace-definition:
21457 namespace identifier { namespace-body }
21459 extension-namespace-definition:
21460 namespace original-namespace-name { namespace-body }
21462 unnamed-namespace-definition:
21463 namespace { namespace-body } */
21465 static void
21466 cp_parser_namespace_definition (cp_parser* parser)
21468 tree identifier;
21469 int nested_definition_count = 0;
21471 cp_ensure_no_omp_declare_simd (parser);
21472 cp_ensure_no_oacc_routine (parser);
21474 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
21475 const bool topmost_inline_p = is_inline;
21477 if (is_inline)
21479 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
21480 cp_lexer_consume_token (parser->lexer);
21483 /* Look for the `namespace' keyword. */
21484 cp_token* token
21485 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
21487 /* Parse any specified attributes before the identifier. */
21488 tree attribs = cp_parser_attributes_opt (parser);
21490 for (;;)
21492 identifier = NULL_TREE;
21494 bool nested_inline_p = cp_lexer_next_token_is_keyword (parser->lexer,
21495 RID_INLINE);
21496 if (nested_inline_p && nested_definition_count != 0)
21498 if (pedantic && cxx_dialect < cxx20)
21499 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
21500 OPT_Wc__20_extensions, "nested inline namespace "
21501 "definitions only available with %<-std=c++20%> or "
21502 "%<-std=gnu++20%>");
21503 cp_lexer_consume_token (parser->lexer);
21506 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21508 identifier = cp_parser_identifier (parser);
21510 if (cp_next_tokens_can_be_std_attribute_p (parser))
21511 pedwarn (input_location, OPT_Wpedantic,
21512 "standard attributes on namespaces must precede "
21513 "the namespace name");
21515 /* Parse any attributes specified after the identifier. */
21516 attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
21519 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
21521 /* Don't forget that the innermost namespace might have been
21522 marked as inline. Use |= because we cannot overwrite
21523 IS_INLINE in case the outermost namespace is inline, but
21524 there are no nested inlines. */
21525 is_inline |= nested_inline_p;
21526 break;
21529 if (!nested_definition_count && pedantic && cxx_dialect < cxx17)
21530 pedwarn (input_location, OPT_Wc__17_extensions,
21531 "nested namespace definitions only available with "
21532 "%<-std=c++17%> or %<-std=gnu++17%>");
21534 /* Nested namespace names can create new namespaces (unlike
21535 other qualified-ids). */
21536 if (int count = (identifier
21537 ? push_namespace (identifier, nested_inline_p)
21538 : 0))
21539 nested_definition_count += count;
21540 else
21541 cp_parser_error (parser, "nested namespace name required");
21542 cp_lexer_consume_token (parser->lexer);
21545 if (nested_definition_count && !identifier)
21546 cp_parser_error (parser, "namespace name required");
21548 if (nested_definition_count && attribs)
21549 error_at (token->location,
21550 "a nested namespace definition cannot have attributes");
21551 if (nested_definition_count && topmost_inline_p)
21552 error_at (token->location,
21553 "a nested namespace definition cannot be inline");
21555 /* Start the namespace. */
21556 nested_definition_count += push_namespace (identifier, is_inline);
21558 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
21560 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
21562 /* Look for the `{' to validate starting the namespace. */
21563 matching_braces braces;
21564 if (braces.require_open (parser))
21566 /* Parse the body of the namespace. */
21567 cp_parser_namespace_body (parser);
21569 /* Look for the final `}'. */
21570 braces.require_close (parser);
21573 if (has_visibility)
21574 pop_visibility (1);
21576 /* Pop the nested namespace definitions. */
21577 while (nested_definition_count--)
21578 pop_namespace ();
21581 /* Parse a namespace-body.
21583 namespace-body:
21584 declaration-seq [opt] */
21586 static void
21587 cp_parser_namespace_body (cp_parser* parser)
21589 cp_parser_declaration_seq_opt (parser);
21592 /* Parse a namespace-alias-definition.
21594 namespace-alias-definition:
21595 namespace identifier = qualified-namespace-specifier ; */
21597 static void
21598 cp_parser_namespace_alias_definition (cp_parser* parser)
21600 tree identifier;
21601 tree namespace_specifier;
21603 cp_token *token = cp_lexer_peek_token (parser->lexer);
21605 /* Look for the `namespace' keyword. */
21606 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
21607 /* Look for the identifier. */
21608 identifier = cp_parser_identifier (parser);
21609 if (identifier == error_mark_node)
21610 return;
21611 /* Look for the `=' token. */
21612 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
21613 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21615 error_at (token->location, "%<namespace%> definition is not allowed here");
21616 /* Skip the definition. */
21617 cp_lexer_consume_token (parser->lexer);
21618 if (cp_parser_skip_to_closing_brace (parser))
21619 cp_lexer_consume_token (parser->lexer);
21620 return;
21622 cp_parser_require (parser, CPP_EQ, RT_EQ);
21623 /* Look for the qualified-namespace-specifier. */
21624 namespace_specifier
21625 = cp_parser_qualified_namespace_specifier (parser);
21626 cp_warn_deprecated_use_scopes (namespace_specifier);
21627 /* Look for the `;' token. */
21628 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21630 /* Register the alias in the symbol table. */
21631 do_namespace_alias (identifier, namespace_specifier);
21634 /* Parse a qualified-namespace-specifier.
21636 qualified-namespace-specifier:
21637 :: [opt] nested-name-specifier [opt] namespace-name
21639 Returns a NAMESPACE_DECL corresponding to the specified
21640 namespace. */
21642 static tree
21643 cp_parser_qualified_namespace_specifier (cp_parser* parser)
21645 /* Look for the optional `::'. */
21646 cp_parser_global_scope_opt (parser,
21647 /*current_scope_valid_p=*/false);
21649 /* Look for the optional nested-name-specifier. */
21650 cp_parser_nested_name_specifier_opt (parser,
21651 /*typename_keyword_p=*/false,
21652 /*check_dependency_p=*/true,
21653 /*type_p=*/false,
21654 /*is_declaration=*/true);
21656 return cp_parser_namespace_name (parser);
21659 /* Subroutine of cp_parser_using_declaration. */
21661 static tree
21662 finish_using_decl (tree qscope, tree identifier, bool typename_p = false)
21664 tree decl = NULL_TREE;
21665 if (at_class_scope_p ())
21667 /* Create the USING_DECL. */
21668 decl = do_class_using_decl (qscope, identifier);
21670 if (check_for_bare_parameter_packs (decl))
21671 return error_mark_node;
21673 if (decl && typename_p)
21674 USING_DECL_TYPENAME_P (decl) = 1;
21676 /* Add it to the list of members in this class. */
21677 finish_member_declaration (decl);
21679 else
21680 finish_nonmember_using_decl (qscope, identifier);
21681 return decl;
21684 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
21685 access declaration.
21687 using-declaration:
21688 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
21689 using :: unqualified-id ;
21691 access-declaration:
21692 qualified-id ;
21696 static bool
21697 cp_parser_using_declaration (cp_parser* parser,
21698 bool access_declaration_p)
21700 cp_token *token;
21701 bool typename_p = false;
21702 bool global_scope_p;
21703 tree identifier;
21704 tree qscope;
21705 int oldcount = errorcount;
21706 cp_token *diag_token = NULL;
21708 if (access_declaration_p)
21710 diag_token = cp_lexer_peek_token (parser->lexer);
21711 cp_parser_parse_tentatively (parser);
21713 else
21715 /* Look for the `using' keyword. */
21716 cp_parser_require_keyword (parser, RID_USING, RT_USING);
21718 again:
21719 /* Peek at the next token. */
21720 token = cp_lexer_peek_token (parser->lexer);
21721 /* See if it's `typename'. */
21722 if (token->keyword == RID_TYPENAME)
21724 /* Remember that we've seen it. */
21725 typename_p = true;
21726 /* Consume the `typename' token. */
21727 cp_lexer_consume_token (parser->lexer);
21731 /* Look for the optional global scope qualification. */
21732 global_scope_p
21733 = (cp_parser_global_scope_opt (parser,
21734 /*current_scope_valid_p=*/false)
21735 != NULL_TREE);
21737 /* If we saw `typename', or didn't see `::', then there must be a
21738 nested-name-specifier present. */
21739 if (typename_p || !global_scope_p)
21741 qscope = cp_parser_nested_name_specifier (parser, typename_p,
21742 /*check_dependency_p=*/true,
21743 /*type_p=*/false,
21744 /*is_declaration=*/true);
21745 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
21747 cp_parser_skip_to_end_of_block_or_statement (parser);
21748 return false;
21751 /* Otherwise, we could be in either of the two productions. In that
21752 case, treat the nested-name-specifier as optional. */
21753 else
21754 qscope = cp_parser_nested_name_specifier_opt (parser,
21755 /*typename_keyword_p=*/false,
21756 /*check_dependency_p=*/true,
21757 /*type_p=*/false,
21758 /*is_declaration=*/true);
21759 if (!qscope)
21760 qscope = global_namespace;
21762 cp_warn_deprecated_use_scopes (qscope);
21764 if (access_declaration_p
21765 && !MAYBE_CLASS_TYPE_P (qscope)
21766 && TREE_CODE (qscope) != ENUMERAL_TYPE)
21767 /* If the qualifying scope of an access-declaration isn't a class
21768 or enumeration type then it can't be valid. */
21769 cp_parser_simulate_error (parser);
21771 if (access_declaration_p && cp_parser_error_occurred (parser))
21772 /* Something has already gone wrong; there's no need to parse
21773 further. Since an error has occurred, the return value of
21774 cp_parser_parse_definitely will be false, as required. */
21775 return cp_parser_parse_definitely (parser);
21777 token = cp_lexer_peek_token (parser->lexer);
21778 /* Parse the unqualified-id. */
21779 identifier = cp_parser_unqualified_id (parser,
21780 /*template_keyword_p=*/false,
21781 /*check_dependency_p=*/true,
21782 /*declarator_p=*/true,
21783 /*optional_p=*/false);
21785 if (access_declaration_p)
21787 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21788 cp_parser_simulate_error (parser);
21789 if (!cp_parser_parse_definitely (parser))
21790 return false;
21792 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21794 cp_token *ell = cp_lexer_consume_token (parser->lexer);
21795 if (cxx_dialect < cxx17)
21796 pedwarn (ell->location, OPT_Wc__17_extensions,
21797 "pack expansion in using-declaration only available "
21798 "with %<-std=c++17%> or %<-std=gnu++17%>");
21800 /* A parameter pack can appear in the qualifying scope, and/or in the
21801 terminal name (if naming a conversion function). Logically they're
21802 part of a single pack expansion of the overall USING_DECL, but we
21803 express them as separate pack expansions within the USING_DECL since
21804 we can't create a pack expansion over a USING_DECL. */
21805 bool saw_parm_pack = false;
21806 if (uses_parameter_packs (qscope))
21808 qscope = make_pack_expansion (qscope);
21809 saw_parm_pack = true;
21811 if (identifier_p (identifier)
21812 && IDENTIFIER_CONV_OP_P (identifier)
21813 && uses_parameter_packs (TREE_TYPE (identifier)))
21815 identifier = make_conv_op_name (make_pack_expansion
21816 (TREE_TYPE (identifier)));
21817 saw_parm_pack = true;
21819 if (!saw_parm_pack)
21821 /* Issue an error in terms using a SCOPE_REF that includes both
21822 components. */
21823 tree name
21824 = build_qualified_name (NULL_TREE, qscope, identifier, false);
21825 make_pack_expansion (name);
21826 gcc_assert (seen_error ());
21827 qscope = identifier = error_mark_node;
21831 /* The function we call to handle a using-declaration is different
21832 depending on what scope we are in. */
21833 if (qscope == error_mark_node || identifier == error_mark_node)
21835 else if (!identifier_p (identifier)
21836 && TREE_CODE (identifier) != BIT_NOT_EXPR)
21837 /* [namespace.udecl]
21839 A using declaration shall not name a template-id. */
21840 error_at (token->location,
21841 "a template-id may not appear in a using-declaration");
21842 else
21844 tree decl = finish_using_decl (qscope, identifier, typename_p);
21846 if (decl == error_mark_node)
21848 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21849 return false;
21853 if (!access_declaration_p
21854 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21856 cp_token *comma = cp_lexer_consume_token (parser->lexer);
21857 if (cxx_dialect < cxx17)
21858 pedwarn (comma->location, OPT_Wc__17_extensions,
21859 "comma-separated list in using-declaration only available "
21860 "with %<-std=c++17%> or %<-std=gnu++17%>");
21861 goto again;
21864 /* Look for the final `;'. */
21865 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21867 if (access_declaration_p && errorcount == oldcount)
21868 warning_at (diag_token->location, OPT_Wdeprecated,
21869 "access declarations are deprecated "
21870 "in favour of using-declarations; "
21871 "suggestion: add the %<using%> keyword");
21873 return true;
21876 /* C++20 using enum declaration.
21878 using-enum-declaration :
21879 using elaborated-enum-specifier ; */
21881 static void
21882 cp_parser_using_enum (cp_parser *parser)
21884 cp_parser_require_keyword (parser, RID_USING, RT_USING);
21886 /* Using cp_parser_elaborated_type_specifier rejects typedef-names, which
21887 breaks one of the motivating examples in using-enum-5.C.
21888 cp_parser_simple_type_specifier seems to be closer to what we actually
21889 want, though that hasn't been properly specified yet. */
21891 /* Consume 'enum'. */
21892 gcc_checking_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM));
21893 cp_lexer_consume_token (parser->lexer);
21895 cp_token *start = cp_lexer_peek_token (parser->lexer);
21897 tree type = (cp_parser_simple_type_specifier
21898 (parser, NULL, CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
21900 cp_token *end = cp_lexer_previous_token (parser->lexer);
21902 if (type == error_mark_node
21903 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
21905 cp_parser_skip_to_end_of_block_or_statement (parser);
21906 return;
21908 if (TREE_CODE (type) == TYPE_DECL)
21909 type = TREE_TYPE (type);
21911 /* The elaborated-enum-specifier shall not name a dependent type and the type
21912 shall have a reachable enum-specifier. */
21913 const char *msg = nullptr;
21914 if (cxx_dialect < cxx20)
21915 msg = _("%<using enum%> "
21916 "only available with %<-std=c++20%> or %<-std=gnu++20%>");
21917 else if (dependent_type_p (type))
21918 msg = _("%<using enum%> of dependent type %qT");
21919 else if (TREE_CODE (type) != ENUMERAL_TYPE)
21920 msg = _("%<using enum%> of non-enumeration type %q#T");
21921 else if (!COMPLETE_TYPE_P (type))
21922 msg = _("%<using enum%> of incomplete type %qT");
21923 else if (OPAQUE_ENUM_P (type))
21924 msg = _("%<using enum%> of %qT before its enum-specifier");
21925 if (msg)
21927 location_t loc = make_location (start, start, end);
21928 auto_diagnostic_group g;
21929 error_at (loc, msg, type);
21930 loc = location_of (type);
21931 if (cxx_dialect < cxx20 || loc == input_location)
21933 else if (OPAQUE_ENUM_P (type))
21934 inform (loc, "opaque-enum-declaration here");
21935 else
21936 inform (loc, "declared here");
21939 /* A using-enum-declaration introduces the enumerator names of the named
21940 enumeration as if by a using-declaration for each enumerator. */
21941 if (TREE_CODE (type) == ENUMERAL_TYPE)
21942 for (tree v = TYPE_VALUES (type); v; v = TREE_CHAIN (v))
21943 finish_using_decl (type, DECL_NAME (TREE_VALUE (v)));
21946 /* Parse an alias-declaration.
21948 alias-declaration:
21949 using identifier attribute-specifier-seq [opt] = type-id */
21951 static tree
21952 cp_parser_alias_declaration (cp_parser* parser)
21954 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
21955 location_t id_location, type_location;
21956 cp_declarator *declarator;
21957 cp_decl_specifier_seq decl_specs;
21958 bool member_p;
21959 const char *saved_message = NULL;
21961 /* Look for the `using' keyword. */
21962 cp_token *using_token
21963 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
21964 if (using_token == NULL)
21965 return error_mark_node;
21967 id_location = cp_lexer_peek_token (parser->lexer)->location;
21968 id = cp_parser_identifier (parser);
21969 if (id == error_mark_node)
21970 return error_mark_node;
21972 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
21973 attributes = cp_parser_attributes_opt (parser);
21974 if (attributes == error_mark_node)
21975 return error_mark_node;
21977 cp_parser_require (parser, CPP_EQ, RT_EQ);
21979 if (cp_parser_error_occurred (parser))
21980 return error_mark_node;
21982 cp_parser_commit_to_tentative_parse (parser);
21984 /* Now we are going to parse the type-id of the declaration. */
21987 [dcl.type]/3 says:
21989 "A type-specifier-seq shall not define a class or enumeration
21990 unless it appears in the type-id of an alias-declaration (7.1.3) that
21991 is not the declaration of a template-declaration."
21993 In other words, if we currently are in an alias template, the
21994 type-id should not define a type.
21996 So let's set parser->type_definition_forbidden_message in that
21997 case; cp_parser_check_type_definition (called by
21998 cp_parser_class_specifier) will then emit an error if a type is
21999 defined in the type-id. */
22000 if (parser->num_template_parameter_lists)
22002 saved_message = parser->type_definition_forbidden_message;
22003 parser->type_definition_forbidden_message =
22004 G_("types may not be defined in alias template declarations");
22007 type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
22008 &type_location);
22010 /* Restore the error message if need be. */
22011 if (parser->num_template_parameter_lists)
22012 parser->type_definition_forbidden_message = saved_message;
22014 if (type == error_mark_node
22015 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
22017 cp_parser_skip_to_end_of_block_or_statement (parser);
22018 return error_mark_node;
22021 /* A typedef-name can also be introduced by an alias-declaration. The
22022 identifier following the using keyword becomes a typedef-name. It has
22023 the same semantics as if it were introduced by the typedef
22024 specifier. In particular, it does not define a new type and it shall
22025 not appear in the type-id. */
22027 clear_decl_specs (&decl_specs);
22028 decl_specs.type = type;
22029 if (attributes != NULL_TREE)
22031 decl_specs.attributes = attributes;
22032 set_and_check_decl_spec_loc (&decl_specs,
22033 ds_attribute,
22034 attrs_token);
22036 set_and_check_decl_spec_loc (&decl_specs,
22037 ds_typedef,
22038 using_token);
22039 set_and_check_decl_spec_loc (&decl_specs,
22040 ds_alias,
22041 using_token);
22042 decl_specs.locations[ds_type_spec] = type_location;
22044 if (parser->num_template_parameter_lists
22045 && !cp_parser_check_template_parameters (parser,
22046 /*num_templates=*/0,
22047 /*template_id*/false,
22048 id_location,
22049 /*declarator=*/NULL))
22050 return error_mark_node;
22052 declarator = make_id_declarator (NULL_TREE, id, sfk_none, id_location);
22054 member_p = at_class_scope_p ();
22055 if (member_p)
22056 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
22057 NULL_TREE, attributes);
22058 else
22059 decl = start_decl (declarator, &decl_specs, 0,
22060 attributes, NULL_TREE, &pushed_scope);
22061 if (decl == error_mark_node)
22062 return decl;
22064 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
22066 if (pushed_scope)
22067 pop_scope (pushed_scope);
22069 /* If decl is a template, return its TEMPLATE_DECL so that it gets
22070 added into the symbol table; otherwise, return the TYPE_DECL. */
22071 if (DECL_LANG_SPECIFIC (decl)
22072 && DECL_TEMPLATE_INFO (decl)
22073 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
22075 decl = DECL_TI_TEMPLATE (decl);
22076 if (member_p)
22077 check_member_template (decl);
22080 return decl;
22083 /* Parse a using-directive.
22085 using-directive:
22086 attribute-specifier-seq [opt] using namespace :: [opt]
22087 nested-name-specifier [opt] namespace-name ; */
22089 static void
22090 cp_parser_using_directive (cp_parser* parser)
22092 tree namespace_decl;
22093 tree attribs = cp_parser_std_attribute_spec_seq (parser);
22094 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22096 /* Error during attribute parsing that resulted in skipping
22097 to next semicolon. */
22098 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22099 return;
22102 /* Look for the `using' keyword. */
22103 cp_parser_require_keyword (parser, RID_USING, RT_USING);
22104 /* And the `namespace' keyword. */
22105 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
22106 /* Look for the optional `::' operator. */
22107 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
22108 /* And the optional nested-name-specifier. */
22109 cp_parser_nested_name_specifier_opt (parser,
22110 /*typename_keyword_p=*/false,
22111 /*check_dependency_p=*/true,
22112 /*type_p=*/false,
22113 /*is_declaration=*/true);
22114 /* Get the namespace being used. */
22115 namespace_decl = cp_parser_namespace_name (parser);
22116 cp_warn_deprecated_use_scopes (namespace_decl);
22117 /* And any specified GNU attributes. */
22118 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
22119 attribs = chainon (attribs, cp_parser_gnu_attributes_opt (parser));
22121 /* Update the symbol table. */
22122 finish_using_directive (namespace_decl, attribs);
22124 /* Look for the final `;'. */
22125 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22128 /* Parse an asm-definition.
22130 asm-qualifier:
22131 volatile
22132 inline
22133 goto
22135 asm-qualifier-list:
22136 asm-qualifier
22137 asm-qualifier-list asm-qualifier
22139 asm-definition:
22140 asm ( string-literal ) ;
22142 GNU Extension:
22144 asm-definition:
22145 asm asm-qualifier-list [opt] ( string-literal ) ;
22146 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt] ) ;
22147 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
22148 : asm-operand-list [opt] ) ;
22149 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
22150 : asm-operand-list [opt]
22151 : asm-clobber-list [opt] ) ;
22152 asm asm-qualifier-list [opt] ( string-literal : : asm-operand-list [opt]
22153 : asm-clobber-list [opt]
22154 : asm-goto-list ) ;
22156 The form with asm-goto-list is valid if and only if the asm-qualifier-list
22157 contains goto, and is the only allowed form in that case. No duplicates are
22158 allowed in an asm-qualifier-list. */
22160 static void
22161 cp_parser_asm_definition (cp_parser* parser)
22163 tree outputs = NULL_TREE;
22164 tree inputs = NULL_TREE;
22165 tree clobbers = NULL_TREE;
22166 tree labels = NULL_TREE;
22167 tree asm_stmt;
22168 bool extended_p = false;
22169 bool invalid_inputs_p = false;
22170 bool invalid_outputs_p = false;
22171 required_token missing = RT_NONE;
22172 location_t asm_loc = cp_lexer_peek_token (parser->lexer)->location;
22174 /* Look for the `asm' keyword. */
22175 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
22177 /* In C++20, unevaluated inline assembly is permitted in constexpr
22178 functions. */
22179 if (parser->in_function_body
22180 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
22181 && cxx_dialect < cxx20)
22182 pedwarn (asm_loc, OPT_Wc__20_extensions, "%<asm%> in %<constexpr%> "
22183 "function only available with %<-std=c++20%> or "
22184 "%<-std=gnu++20%>");
22186 /* Handle the asm-qualifier-list. */
22187 location_t volatile_loc = UNKNOWN_LOCATION;
22188 location_t inline_loc = UNKNOWN_LOCATION;
22189 location_t goto_loc = UNKNOWN_LOCATION;
22190 location_t first_loc = UNKNOWN_LOCATION;
22192 if (cp_parser_allow_gnu_extensions_p (parser))
22193 for (;;)
22195 cp_token *token = cp_lexer_peek_token (parser->lexer);
22196 location_t loc = token->location;
22197 switch (cp_lexer_peek_token (parser->lexer)->keyword)
22199 case RID_VOLATILE:
22200 if (volatile_loc)
22202 error_at (loc, "duplicate %<asm%> qualifier %qT",
22203 token->u.value);
22204 inform (volatile_loc, "first seen here");
22206 else
22208 if (!parser->in_function_body)
22209 warning_at (loc, 0, "%<asm%> qualifier %qT ignored "
22210 "outside of function body", token->u.value);
22211 volatile_loc = loc;
22213 cp_lexer_consume_token (parser->lexer);
22214 continue;
22216 case RID_INLINE:
22217 if (inline_loc)
22219 error_at (loc, "duplicate %<asm%> qualifier %qT",
22220 token->u.value);
22221 inform (inline_loc, "first seen here");
22223 else
22224 inline_loc = loc;
22225 if (!first_loc)
22226 first_loc = loc;
22227 cp_lexer_consume_token (parser->lexer);
22228 continue;
22230 case RID_GOTO:
22231 if (goto_loc)
22233 error_at (loc, "duplicate %<asm%> qualifier %qT",
22234 token->u.value);
22235 inform (goto_loc, "first seen here");
22237 else
22238 goto_loc = loc;
22239 if (!first_loc)
22240 first_loc = loc;
22241 cp_lexer_consume_token (parser->lexer);
22242 continue;
22244 case RID_CONST:
22245 case RID_RESTRICT:
22246 error_at (loc, "%qT is not an %<asm%> qualifier", token->u.value);
22247 cp_lexer_consume_token (parser->lexer);
22248 continue;
22250 default:
22251 break;
22253 break;
22256 bool volatile_p = (volatile_loc != UNKNOWN_LOCATION);
22257 bool inline_p = (inline_loc != UNKNOWN_LOCATION);
22258 bool goto_p = (goto_loc != UNKNOWN_LOCATION);
22260 if (!parser->in_function_body && (inline_p || goto_p))
22262 error_at (first_loc, "%<asm%> qualifier outside of function body");
22263 inline_p = goto_p = false;
22266 /* Look for the opening `('. */
22267 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22268 return;
22269 /* Look for the string. */
22270 tree string = cp_parser_string_literal (parser, /*translate=*/false,
22271 /*wide_ok=*/false);
22272 if (string == error_mark_node)
22274 cp_parser_skip_to_closing_parenthesis (parser, true, false,
22275 /*consume_paren=*/true);
22276 return;
22279 /* If we're allowing GNU extensions, check for the extended assembly
22280 syntax. Unfortunately, the `:' tokens need not be separated by
22281 a space in C, and so, for compatibility, we tolerate that here
22282 too. Doing that means that we have to treat the `::' operator as
22283 two `:' tokens. */
22284 if (cp_parser_allow_gnu_extensions_p (parser)
22285 && parser->in_function_body
22286 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
22287 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
22289 bool inputs_p = false;
22290 bool clobbers_p = false;
22291 bool labels_p = false;
22293 /* The extended syntax was used. */
22294 extended_p = true;
22296 /* Look for outputs. */
22297 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22299 /* Consume the `:'. */
22300 cp_lexer_consume_token (parser->lexer);
22301 /* Parse the output-operands. */
22302 if (cp_lexer_next_token_is_not (parser->lexer,
22303 CPP_COLON)
22304 && cp_lexer_next_token_is_not (parser->lexer,
22305 CPP_SCOPE)
22306 && cp_lexer_next_token_is_not (parser->lexer,
22307 CPP_CLOSE_PAREN))
22309 outputs = cp_parser_asm_operand_list (parser);
22310 if (outputs == error_mark_node)
22311 invalid_outputs_p = true;
22314 /* If the next token is `::', there are no outputs, and the
22315 next token is the beginning of the inputs. */
22316 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22317 /* The inputs are coming next. */
22318 inputs_p = true;
22320 /* Look for inputs. */
22321 if (inputs_p
22322 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22324 /* Consume the `:' or `::'. */
22325 cp_lexer_consume_token (parser->lexer);
22326 /* Parse the output-operands. */
22327 if (cp_lexer_next_token_is_not (parser->lexer,
22328 CPP_COLON)
22329 && cp_lexer_next_token_is_not (parser->lexer,
22330 CPP_SCOPE)
22331 && cp_lexer_next_token_is_not (parser->lexer,
22332 CPP_CLOSE_PAREN))
22334 inputs = cp_parser_asm_operand_list (parser);
22335 if (inputs == error_mark_node)
22336 invalid_inputs_p = true;
22339 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22340 /* The clobbers are coming next. */
22341 clobbers_p = true;
22343 /* Look for clobbers. */
22344 if (clobbers_p
22345 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22347 clobbers_p = true;
22348 /* Consume the `:' or `::'. */
22349 cp_lexer_consume_token (parser->lexer);
22350 /* Parse the clobbers. */
22351 if (cp_lexer_next_token_is_not (parser->lexer,
22352 CPP_COLON)
22353 && cp_lexer_next_token_is_not (parser->lexer,
22354 CPP_CLOSE_PAREN))
22355 clobbers = cp_parser_asm_clobber_list (parser);
22357 else if (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22358 /* The labels are coming next. */
22359 labels_p = true;
22361 /* Look for labels. */
22362 if (labels_p
22363 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
22365 labels_p = true;
22366 /* Consume the `:' or `::'. */
22367 cp_lexer_consume_token (parser->lexer);
22368 /* Parse the labels. */
22369 labels = cp_parser_asm_label_list (parser);
22372 if (goto_p && !labels_p)
22373 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
22375 else if (goto_p)
22376 missing = RT_COLON_SCOPE;
22378 /* Look for the closing `)'. */
22379 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
22380 missing ? missing : RT_CLOSE_PAREN))
22381 cp_parser_skip_to_closing_parenthesis (parser, true, false,
22382 /*consume_paren=*/true);
22383 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22385 if (!invalid_inputs_p && !invalid_outputs_p)
22387 /* Create the ASM_EXPR. */
22388 if (parser->in_function_body)
22390 asm_stmt = finish_asm_stmt (asm_loc, volatile_p, string, outputs,
22391 inputs, clobbers, labels, inline_p);
22392 /* If the extended syntax was not used, mark the ASM_EXPR. */
22393 if (!extended_p)
22395 tree temp = asm_stmt;
22396 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
22397 temp = TREE_OPERAND (temp, 0);
22399 ASM_INPUT_P (temp) = 1;
22402 else
22403 symtab->finalize_toplevel_asm (string);
22407 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
22408 type that comes from the decl-specifier-seq. */
22410 static tree
22411 strip_declarator_types (tree type, cp_declarator *declarator)
22413 for (cp_declarator *d = declarator; d;)
22414 switch (d->kind)
22416 case cdk_id:
22417 case cdk_decomp:
22418 case cdk_error:
22419 d = NULL;
22420 break;
22422 default:
22423 if (TYPE_PTRMEMFUNC_P (type))
22424 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
22425 type = TREE_TYPE (type);
22426 d = d->declarator;
22427 break;
22430 return type;
22433 /* Warn about the most vexing parse syntactic ambiguity, i.e., warn when
22434 a construct looks like a variable definition but is actually a function
22435 declaration. DECL_SPECIFIERS is the decl-specifier-seq and DECLARATOR
22436 is the declarator for this function declaration. */
22438 static void
22439 warn_about_ambiguous_parse (const cp_decl_specifier_seq *decl_specifiers,
22440 const cp_declarator *declarator)
22442 /* Only warn if we are declaring a function at block scope. */
22443 if (!at_function_scope_p ())
22444 return;
22446 /* And only if there is no storage class specified. */
22447 if (decl_specifiers->storage_class != sc_none
22448 || decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
22449 return;
22451 if (declarator->kind != cdk_function
22452 || !declarator->declarator
22453 || declarator->declarator->kind != cdk_id
22454 || !identifier_p (get_unqualified_id
22455 (const_cast<cp_declarator *>(declarator))))
22456 return;
22458 /* Don't warn when the whole declarator (not just the declarator-id!)
22459 was parenthesized. That is, don't warn for int(n()) but do warn
22460 for int(f)(). */
22461 if (declarator->parenthesized != UNKNOWN_LOCATION)
22462 return;
22464 tree type;
22465 if (decl_specifiers->type)
22467 type = decl_specifiers->type;
22468 if (TREE_CODE (type) == TYPE_DECL)
22469 type = TREE_TYPE (type);
22471 /* If the return type is void there is no ambiguity. */
22472 if (same_type_p (type, void_type_node))
22473 return;
22475 else if (decl_specifiers->any_type_specifiers_p)
22476 /* Code like long f(); will have null ->type. If we have any
22477 type-specifiers, pretend we've seen int. */
22478 type = integer_type_node;
22479 else
22480 return;
22482 auto_diagnostic_group d;
22483 location_t loc = declarator->u.function.parens_loc;
22484 tree params = declarator->u.function.parameters;
22485 const bool has_list_ctor_p = CLASS_TYPE_P (type) && TYPE_HAS_LIST_CTOR (type);
22487 /* The T t() case. */
22488 if (params == void_list_node)
22490 if (warning_at (loc, OPT_Wvexing_parse,
22491 "empty parentheses were disambiguated as a function "
22492 "declaration"))
22494 /* () means value-initialization (C++03 and up); {} (C++11 and up)
22495 means value-initialization or aggregate-initialization, nothing
22496 means default-initialization. We can only suggest removing the
22497 parentheses/adding {} if T has a default constructor. */
22498 if (!CLASS_TYPE_P (type) || TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
22500 gcc_rich_location iloc (loc);
22501 iloc.add_fixit_remove ();
22502 inform (&iloc, "remove parentheses to default-initialize "
22503 "a variable");
22504 if (cxx_dialect >= cxx11 && !has_list_ctor_p)
22506 if (CP_AGGREGATE_TYPE_P (type))
22507 inform (loc, "or replace parentheses with braces to "
22508 "aggregate-initialize a variable");
22509 else
22510 inform (loc, "or replace parentheses with braces to "
22511 "value-initialize a variable");
22515 return;
22518 /* If we had (...) or the parameter-list wasn't parenthesized,
22519 we're done. */
22520 if (params == NULL_TREE || !PARENTHESIZED_LIST_P (params))
22521 return;
22523 /* The T t(X()) case. */
22524 if (list_length (params) == 2)
22526 if (warning_at (loc, OPT_Wvexing_parse,
22527 "parentheses were disambiguated as a function "
22528 "declaration"))
22530 gcc_rich_location iloc (loc);
22531 /* {}-initialization means that we can use an initializer-list
22532 constructor if no default constructor is available, so don't
22533 suggest using {} for classes that have an initializer_list
22534 constructor. */
22535 if (cxx_dialect >= cxx11 && !has_list_ctor_p)
22537 iloc.add_fixit_replace (get_start (loc), "{");
22538 iloc.add_fixit_replace (get_finish (loc), "}");
22539 inform (&iloc, "replace parentheses with braces to declare a "
22540 "variable");
22542 else
22544 iloc.add_fixit_insert_after (get_start (loc), "(");
22545 iloc.add_fixit_insert_before (get_finish (loc), ")");
22546 inform (&iloc, "add parentheses to declare a variable");
22550 /* The T t(X(), X()) case. */
22551 else if (warning_at (loc, OPT_Wvexing_parse,
22552 "parentheses were disambiguated as a function "
22553 "declaration"))
22555 gcc_rich_location iloc (loc);
22556 if (cxx_dialect >= cxx11 && !has_list_ctor_p)
22558 iloc.add_fixit_replace (get_start (loc), "{");
22559 iloc.add_fixit_replace (get_finish (loc), "}");
22560 inform (&iloc, "replace parentheses with braces to declare a "
22561 "variable");
22566 /* If DECLARATOR with DECL_SPECS is a function declarator that has
22567 the form of a deduction guide, tag it as such. CTOR_DTOR_OR_CONV_P
22568 has the same meaning as in cp_parser_declarator. */
22570 static void
22571 cp_parser_maybe_adjust_declarator_for_dguide (cp_parser *parser,
22572 cp_decl_specifier_seq *decl_specs,
22573 cp_declarator *declarator,
22574 int *ctor_dtor_or_conv_p)
22576 if (cxx_dialect >= cxx17
22577 && *ctor_dtor_or_conv_p <= 0
22578 && !decl_specs->type
22579 && !decl_specs->any_type_specifiers_p
22580 && function_declarator_p (declarator))
22582 cp_declarator *id = get_id_declarator (declarator);
22583 tree name = id->u.id.unqualified_name;
22584 parser->scope = id->u.id.qualifying_scope;
22585 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
22586 if (tmpl
22587 && (DECL_CLASS_TEMPLATE_P (tmpl)
22588 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
22590 id->u.id.unqualified_name = dguide_name (tmpl);
22591 id->u.id.sfk = sfk_deduction_guide;
22592 *ctor_dtor_or_conv_p = 1;
22597 /* Declarators [gram.dcl.decl] */
22599 /* Parse an init-declarator.
22601 init-declarator:
22602 declarator initializer [opt]
22604 GNU Extension:
22606 init-declarator:
22607 declarator asm-specification [opt] attributes [opt] initializer [opt]
22609 function-definition:
22610 decl-specifier-seq [opt] declarator ctor-initializer [opt]
22611 function-body
22612 decl-specifier-seq [opt] declarator function-try-block
22614 GNU Extension:
22616 function-definition:
22617 __extension__ function-definition
22619 TM Extension:
22621 function-definition:
22622 decl-specifier-seq [opt] declarator function-transaction-block
22624 The parser flags FLAGS is used to control type-specifier parsing.
22626 The DECL_SPECIFIERS apply to this declarator. Returns a
22627 representation of the entity declared. If MEMBER_P is TRUE, then
22628 this declarator appears in a class scope. The new DECL created by
22629 this declarator is returned.
22631 The CHECKS are access checks that should be performed once we know
22632 what entity is being declared (and, therefore, what classes have
22633 befriended it).
22635 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
22636 for a function-definition here as well. If the declarator is a
22637 declarator for a function-definition, *FUNCTION_DEFINITION_P will
22638 be TRUE upon return. By that point, the function-definition will
22639 have been completely parsed.
22641 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
22642 is FALSE.
22644 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
22645 parsed declaration if it is an uninitialized single declarator not followed
22646 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
22647 if present, will not be consumed. If returned, this declarator will be
22648 created with SD_INITIALIZED but will not call cp_finish_decl.
22650 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
22651 and there is an initializer, the pointed location_t is set to the
22652 location of the '=' or `(', or '{' in C++11 token introducing the
22653 initializer. */
22655 static tree
22656 cp_parser_init_declarator (cp_parser* parser,
22657 cp_parser_flags flags,
22658 cp_decl_specifier_seq *decl_specifiers,
22659 vec<deferred_access_check, va_gc> *checks,
22660 bool function_definition_allowed_p,
22661 bool member_p,
22662 int declares_class_or_enum,
22663 bool* function_definition_p,
22664 tree* maybe_range_for_decl,
22665 location_t* init_loc,
22666 tree* auto_result)
22668 cp_token *token = NULL, *asm_spec_start_token = NULL,
22669 *attributes_start_token = NULL;
22670 cp_declarator *declarator;
22671 tree prefix_attributes;
22672 tree attributes = NULL;
22673 tree asm_specification;
22674 tree initializer;
22675 tree decl = NULL_TREE;
22676 tree scope;
22677 int is_initialized;
22678 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
22679 initialized with "= ..", CPP_OPEN_PAREN if initialized with
22680 "(...)". */
22681 enum cpp_ttype initialization_kind;
22682 bool is_direct_init = false;
22683 bool is_non_constant_init;
22684 int ctor_dtor_or_conv_p;
22685 bool friend_p = cp_parser_friend_p (decl_specifiers);
22686 bool static_p = decl_specifiers->storage_class == sc_static;
22687 tree pushed_scope = NULL_TREE;
22688 bool range_for_decl_p = false;
22689 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
22690 location_t tmp_init_loc = UNKNOWN_LOCATION;
22692 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_consteval))
22693 flags |= CP_PARSER_FLAGS_CONSTEVAL;
22695 /* Assume that this is not the declarator for a function
22696 definition. */
22697 if (function_definition_p)
22698 *function_definition_p = false;
22700 /* Default arguments are only permitted for function parameters. */
22701 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
22702 parser->default_arg_ok_p = false;
22704 /* Defer access checks while parsing the declarator; we cannot know
22705 what names are accessible until we know what is being
22706 declared. */
22707 resume_deferring_access_checks ();
22709 token = cp_lexer_peek_token (parser->lexer);
22711 /* Parse the declarator. */
22712 declarator
22713 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22714 flags, &ctor_dtor_or_conv_p,
22715 /*parenthesized_p=*/NULL,
22716 member_p, friend_p, static_p);
22717 /* Gather up the deferred checks. */
22718 stop_deferring_access_checks ();
22720 parser->default_arg_ok_p = saved_default_arg_ok_p;
22722 /* If the DECLARATOR was erroneous, there's no need to go
22723 further. */
22724 if (declarator == cp_error_declarator)
22725 return error_mark_node;
22727 /* Check that the number of template-parameter-lists is OK. */
22728 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
22729 token->location))
22730 return error_mark_node;
22732 if (declares_class_or_enum & 2)
22733 cp_parser_check_for_definition_in_return_type (declarator,
22734 decl_specifiers->type,
22735 decl_specifiers->locations[ds_type_spec]);
22737 /* Figure out what scope the entity declared by the DECLARATOR is
22738 located in. `grokdeclarator' sometimes changes the scope, so
22739 we compute it now. */
22740 scope = get_scope_of_declarator (declarator);
22742 /* Perform any lookups in the declared type which were thought to be
22743 dependent, but are not in the scope of the declarator. */
22744 decl_specifiers->type
22745 = maybe_update_decl_type (decl_specifiers->type, scope);
22747 /* If we're allowing GNU extensions, look for an
22748 asm-specification. */
22749 if (cp_parser_allow_gnu_extensions_p (parser))
22751 /* Look for an asm-specification. */
22752 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
22753 asm_specification = cp_parser_asm_specification_opt (parser);
22755 else
22756 asm_specification = NULL_TREE;
22758 /* Gather the attributes that were provided with the
22759 decl-specifiers. */
22760 prefix_attributes = decl_specifiers->attributes;
22762 /* Look for attributes. */
22763 attributes_start_token = cp_lexer_peek_token (parser->lexer);
22764 attributes = cp_parser_attributes_opt (parser);
22766 /* Peek at the next token. */
22767 token = cp_lexer_peek_token (parser->lexer);
22769 bool bogus_implicit_tmpl = false;
22771 if (function_declarator_p (declarator))
22773 /* Handle C++17 deduction guides. Note that class-scope
22774 non-template deduction guides are instead handled in
22775 cp_parser_member_declaration. */
22776 cp_parser_maybe_adjust_declarator_for_dguide (parser,
22777 decl_specifiers,
22778 declarator,
22779 &ctor_dtor_or_conv_p);
22781 if (!member_p && !cp_parser_error_occurred (parser))
22782 warn_about_ambiguous_parse (decl_specifiers, declarator);
22784 /* Check to see if the token indicates the start of a
22785 function-definition. */
22786 if (cp_parser_token_starts_function_definition_p (token))
22788 if (!function_definition_allowed_p)
22790 /* If a function-definition should not appear here, issue an
22791 error message. */
22792 cp_parser_error (parser,
22793 "a function-definition is not allowed here");
22794 return error_mark_node;
22797 location_t func_brace_location
22798 = cp_lexer_peek_token (parser->lexer)->location;
22800 /* Neither attributes nor an asm-specification are allowed
22801 on a function-definition. */
22802 if (asm_specification)
22803 error_at (asm_spec_start_token->location,
22804 "an %<asm%> specification is not allowed "
22805 "on a function-definition");
22806 if (attributes)
22807 error_at (attributes_start_token->location,
22808 "attributes are not allowed "
22809 "on a function-definition");
22810 /* This is a function-definition. */
22811 *function_definition_p = true;
22813 /* Parse the function definition. */
22814 if (member_p)
22815 decl = cp_parser_save_member_function_body (parser,
22816 decl_specifiers,
22817 declarator,
22818 prefix_attributes);
22819 else
22820 decl =
22821 (cp_parser_function_definition_from_specifiers_and_declarator
22822 (parser, decl_specifiers, prefix_attributes, declarator));
22824 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
22826 /* This is where the prologue starts... */
22827 DECL_STRUCT_FUNCTION (decl)->function_start_locus
22828 = func_brace_location;
22831 return decl;
22834 else if (parser->fully_implicit_function_template_p)
22836 /* A non-template declaration involving a function parameter list
22837 containing an implicit template parameter will be made into a
22838 template. If the resulting declaration is not going to be an
22839 actual function then finish the template scope here to prevent it.
22840 An error message will be issued once we have a decl to talk about.
22842 FIXME probably we should do type deduction rather than create an
22843 implicit template, but the standard currently doesn't allow it. */
22844 bogus_implicit_tmpl = true;
22845 finish_fully_implicit_template (parser, NULL_TREE);
22848 /* [dcl.dcl]
22850 Only in function declarations for constructors, destructors, type
22851 conversions, and deduction guides can the decl-specifier-seq be omitted.
22853 We explicitly postpone this check past the point where we handle
22854 function-definitions because we tolerate function-definitions
22855 that are missing their return types in some modes. */
22856 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
22858 cp_parser_error (parser,
22859 "expected constructor, destructor, or type conversion");
22860 return error_mark_node;
22863 /* An `=' or an '{' in C++11, indicate an initializer. An '(' may indicate
22864 an initializer as well. */
22865 if (token->type == CPP_EQ
22866 || token->type == CPP_OPEN_PAREN
22867 || token->type == CPP_OPEN_BRACE)
22869 /* Don't get fooled into thinking that F(i)(1)(2) is an initializer.
22870 It isn't; it's an expression. (Here '(i)' would have already been
22871 parsed as a declarator.) */
22872 if (token->type == CPP_OPEN_PAREN
22873 && cp_parser_uncommitted_to_tentative_parse_p (parser))
22875 cp_lexer_save_tokens (parser->lexer);
22876 cp_lexer_consume_token (parser->lexer);
22877 cp_parser_skip_to_closing_parenthesis (parser,
22878 /*recovering*/false,
22879 /*or_comma*/false,
22880 /*consume_paren*/true);
22881 /* If this is an initializer, only a ',' or ';' can follow: either
22882 we have another init-declarator, or we're at the end of an
22883 init-declarator-list which can only be followed by a ';'. */
22884 bool ok = (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22885 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
22886 cp_lexer_rollback_tokens (parser->lexer);
22887 if (UNLIKELY (!ok))
22888 /* Not an init-declarator. */
22889 return error_mark_node;
22891 is_initialized = SD_INITIALIZED;
22892 initialization_kind = token->type;
22893 declarator->init_loc = token->location;
22894 if (maybe_range_for_decl)
22895 *maybe_range_for_decl = error_mark_node;
22896 tmp_init_loc = token->location;
22897 if (init_loc && *init_loc == UNKNOWN_LOCATION)
22898 *init_loc = tmp_init_loc;
22900 if (token->type == CPP_EQ
22901 && function_declarator_p (declarator))
22903 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
22904 if (t2->keyword == RID_DEFAULT)
22905 is_initialized = SD_DEFAULTED;
22906 else if (t2->keyword == RID_DELETE)
22907 is_initialized = SD_DELETED;
22910 else
22912 /* If the init-declarator isn't initialized and isn't followed by a
22913 `,' or `;', it's not a valid init-declarator. */
22914 if (token->type != CPP_COMMA
22915 && token->type != CPP_SEMICOLON)
22917 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
22918 range_for_decl_p = true;
22919 else
22921 if (!maybe_range_for_decl)
22922 cp_parser_error (parser, "expected initializer");
22923 return error_mark_node;
22926 is_initialized = SD_UNINITIALIZED;
22927 initialization_kind = CPP_EOF;
22930 /* Because start_decl has side-effects, we should only call it if we
22931 know we're going ahead. By this point, we know that we cannot
22932 possibly be looking at any other construct. */
22933 cp_parser_commit_to_tentative_parse (parser);
22935 /* Enter the newly declared entry in the symbol table. If we're
22936 processing a declaration in a class-specifier, we wait until
22937 after processing the initializer. */
22938 if (!member_p)
22940 if (parser->in_unbraced_linkage_specification_p)
22941 decl_specifiers->storage_class = sc_extern;
22942 decl = start_decl (declarator, decl_specifiers,
22943 range_for_decl_p? SD_INITIALIZED : is_initialized,
22944 attributes, prefix_attributes, &pushed_scope);
22945 cp_finalize_omp_declare_simd (parser, decl);
22946 cp_finalize_oacc_routine (parser, decl, false);
22947 /* Adjust location of decl if declarator->id_loc is more appropriate:
22948 set, and decl wasn't merged with another decl, in which case its
22949 location would be different from input_location, and more accurate. */
22950 if (DECL_P (decl)
22951 && declarator->id_loc != UNKNOWN_LOCATION
22952 && DECL_SOURCE_LOCATION (decl) == input_location)
22953 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
22955 else if (scope)
22956 /* Enter the SCOPE. That way unqualified names appearing in the
22957 initializer will be looked up in SCOPE. */
22958 pushed_scope = push_scope (scope);
22960 /* Perform deferred access control checks, now that we know in which
22961 SCOPE the declared entity resides. */
22962 if (!member_p && decl)
22964 tree saved_current_function_decl = NULL_TREE;
22966 /* If the entity being declared is a function, pretend that we
22967 are in its scope. If it is a `friend', it may have access to
22968 things that would not otherwise be accessible. */
22969 if (TREE_CODE (decl) == FUNCTION_DECL)
22971 saved_current_function_decl = current_function_decl;
22972 current_function_decl = decl;
22975 /* Perform access checks for template parameters. */
22976 cp_parser_perform_template_parameter_access_checks (checks);
22978 /* Perform the access control checks for the declarator and the
22979 decl-specifiers. */
22980 perform_deferred_access_checks (tf_warning_or_error);
22982 /* Restore the saved value. */
22983 if (TREE_CODE (decl) == FUNCTION_DECL)
22984 current_function_decl = saved_current_function_decl;
22987 /* Parse the initializer. */
22988 initializer = NULL_TREE;
22989 is_direct_init = false;
22990 is_non_constant_init = true;
22991 if (is_initialized)
22993 if (function_declarator_p (declarator))
22995 if (initialization_kind == CPP_EQ)
22996 initializer = cp_parser_pure_specifier (parser);
22997 else
22999 /* If the declaration was erroneous, we don't really
23000 know what the user intended, so just silently
23001 consume the initializer. */
23002 if (decl != error_mark_node)
23003 error_at (tmp_init_loc, "initializer provided for function");
23004 cp_parser_skip_to_closing_parenthesis (parser,
23005 /*recovering=*/true,
23006 /*or_comma=*/false,
23007 /*consume_paren=*/true);
23010 else
23012 /* We want to record the extra mangling scope for in-class
23013 initializers of class members and initializers of static
23014 data member templates and namespace-scope initializers.
23015 The former involves deferring parsing of the initializer
23016 until end of class as with default arguments. So right
23017 here we only handle the latter two. */
23018 bool has_lambda_scope = false;
23020 if (decl != error_mark_node
23021 && !member_p
23022 && (processing_template_decl || DECL_NAMESPACE_SCOPE_P (decl)))
23023 has_lambda_scope = true;
23025 if (has_lambda_scope)
23026 start_lambda_scope (decl);
23027 initializer = cp_parser_initializer (parser,
23028 &is_direct_init,
23029 &is_non_constant_init);
23030 if (has_lambda_scope)
23031 finish_lambda_scope ();
23032 if (initializer == error_mark_node)
23033 cp_parser_skip_to_end_of_statement (parser);
23037 /* The old parser allows attributes to appear after a parenthesized
23038 initializer. Mark Mitchell proposed removing this functionality
23039 on the GCC mailing lists on 2002-08-13. This parser accepts the
23040 attributes -- but ignores them. Made a permerror in GCC 8. */
23041 if (cp_parser_allow_gnu_extensions_p (parser)
23042 && initialization_kind == CPP_OPEN_PAREN
23043 && cp_parser_attributes_opt (parser)
23044 && permerror (input_location,
23045 "attributes after parenthesized initializer ignored"))
23047 static bool hint;
23048 if (flag_permissive && !hint)
23050 hint = true;
23051 inform (input_location,
23052 "this flexibility is deprecated and will be removed");
23056 /* And now complain about a non-function implicit template. */
23057 if (bogus_implicit_tmpl && decl != error_mark_node)
23058 error_at (DECL_SOURCE_LOCATION (decl),
23059 "non-function %qD declared as implicit template", decl);
23061 /* For an in-class declaration, use `grokfield' to create the
23062 declaration. */
23063 if (member_p)
23065 if (pushed_scope)
23067 pop_scope (pushed_scope);
23068 pushed_scope = NULL_TREE;
23070 decl = grokfield (declarator, decl_specifiers,
23071 initializer, !is_non_constant_init,
23072 /*asmspec=*/NULL_TREE,
23073 attr_chainon (attributes, prefix_attributes));
23074 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
23075 cp_parser_save_default_args (parser, decl);
23076 cp_finalize_omp_declare_simd (parser, decl);
23077 cp_finalize_oacc_routine (parser, decl, false);
23080 /* Finish processing the declaration. But, skip member
23081 declarations. */
23082 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
23084 cp_finish_decl (decl,
23085 initializer, !is_non_constant_init,
23086 asm_specification,
23087 /* If the initializer is in parentheses, then this is
23088 a direct-initialization, which means that an
23089 `explicit' constructor is OK. Otherwise, an
23090 `explicit' constructor cannot be used. */
23091 ((is_direct_init || !is_initialized)
23092 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
23094 else if ((cxx_dialect != cxx98) && friend_p
23095 && decl && TREE_CODE (decl) == FUNCTION_DECL)
23096 /* Core issue #226 (C++0x only): A default template-argument
23097 shall not be specified in a friend class template
23098 declaration. */
23099 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
23100 /*is_partial=*/false, /*is_friend_decl=*/1);
23102 if (!friend_p && pushed_scope)
23103 pop_scope (pushed_scope);
23105 if (function_declarator_p (declarator)
23106 && parser->fully_implicit_function_template_p)
23108 if (member_p)
23109 decl = finish_fully_implicit_template (parser, decl);
23110 else
23111 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
23114 if (auto_result && is_initialized && decl_specifiers->type
23115 && type_uses_auto (decl_specifiers->type))
23116 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
23118 return decl;
23121 /* Parse a declarator.
23123 declarator:
23124 direct-declarator
23125 ptr-operator declarator
23127 abstract-declarator:
23128 ptr-operator abstract-declarator [opt]
23129 direct-abstract-declarator
23131 GNU Extensions:
23133 declarator:
23134 attributes [opt] direct-declarator
23135 attributes [opt] ptr-operator declarator
23137 abstract-declarator:
23138 attributes [opt] ptr-operator abstract-declarator [opt]
23139 attributes [opt] direct-abstract-declarator
23141 The parser flags FLAGS is used to control type-specifier parsing.
23143 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
23144 detect constructors, destructors, deduction guides, or conversion operators.
23145 It is set to -1 if the declarator is a name, and +1 if it is a
23146 function. Otherwise it is set to zero. Usually you just want to
23147 test for >0, but internally the negative value is used.
23149 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
23150 a decl-specifier-seq unless it declares a constructor, destructor,
23151 or conversion. It might seem that we could check this condition in
23152 semantic analysis, rather than parsing, but that makes it difficult
23153 to handle something like `f()'. We want to notice that there are
23154 no decl-specifiers, and therefore realize that this is an
23155 expression, not a declaration.)
23157 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
23158 the declarator is a direct-declarator of the form "(...)".
23160 MEMBER_P is true iff this declarator is a member-declarator.
23162 FRIEND_P is true iff this declarator is a friend.
23164 STATIC_P is true iff the keyword static was seen. */
23166 static cp_declarator *
23167 cp_parser_declarator (cp_parser* parser,
23168 cp_parser_declarator_kind dcl_kind,
23169 cp_parser_flags flags,
23170 int* ctor_dtor_or_conv_p,
23171 bool* parenthesized_p,
23172 bool member_p, bool friend_p, bool static_p)
23174 cp_declarator *declarator;
23175 enum tree_code code;
23176 cp_cv_quals cv_quals;
23177 tree class_type;
23178 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
23180 /* Assume this is not a constructor, destructor, or type-conversion
23181 operator. */
23182 if (ctor_dtor_or_conv_p)
23183 *ctor_dtor_or_conv_p = 0;
23185 if (cp_parser_allow_gnu_extensions_p (parser))
23186 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
23188 /* Check for the ptr-operator production. */
23189 cp_parser_parse_tentatively (parser);
23190 /* Parse the ptr-operator. */
23191 code = cp_parser_ptr_operator (parser,
23192 &class_type,
23193 &cv_quals,
23194 &std_attributes);
23196 /* If that worked, then we have a ptr-operator. */
23197 if (cp_parser_parse_definitely (parser))
23199 /* If a ptr-operator was found, then this declarator was not
23200 parenthesized. */
23201 if (parenthesized_p)
23202 *parenthesized_p = false;
23203 /* The dependent declarator is optional if we are parsing an
23204 abstract-declarator. */
23205 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
23206 cp_parser_parse_tentatively (parser);
23208 /* Parse the dependent declarator. */
23209 declarator = cp_parser_declarator (parser, dcl_kind, flags,
23210 /*ctor_dtor_or_conv_p=*/NULL,
23211 /*parenthesized_p=*/NULL,
23212 member_p, friend_p, static_p);
23214 /* If we are parsing an abstract-declarator, we must handle the
23215 case where the dependent declarator is absent. */
23216 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
23217 && !cp_parser_parse_definitely (parser))
23218 declarator = NULL;
23220 declarator = cp_parser_make_indirect_declarator
23221 (code, class_type, cv_quals, declarator, std_attributes);
23223 /* Everything else is a direct-declarator. */
23224 else
23226 if (parenthesized_p)
23227 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
23228 CPP_OPEN_PAREN);
23229 declarator = cp_parser_direct_declarator (parser, dcl_kind,
23230 flags, ctor_dtor_or_conv_p,
23231 member_p, friend_p, static_p);
23234 if (gnu_attributes && declarator && declarator != cp_error_declarator)
23235 declarator->attributes = gnu_attributes;
23236 return declarator;
23239 /* Parse a direct-declarator or direct-abstract-declarator.
23241 direct-declarator:
23242 declarator-id
23243 direct-declarator ( parameter-declaration-clause )
23244 cv-qualifier-seq [opt]
23245 ref-qualifier [opt]
23246 exception-specification [opt]
23247 direct-declarator [ constant-expression [opt] ]
23248 ( declarator )
23250 direct-abstract-declarator:
23251 direct-abstract-declarator [opt]
23252 ( parameter-declaration-clause )
23253 cv-qualifier-seq [opt]
23254 ref-qualifier [opt]
23255 exception-specification [opt]
23256 direct-abstract-declarator [opt] [ constant-expression [opt] ]
23257 ( abstract-declarator )
23259 Returns a representation of the declarator. DCL_KIND is
23260 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
23261 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
23262 we are parsing a direct-declarator. It is
23263 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
23264 of ambiguity we prefer an abstract declarator, as per
23265 [dcl.ambig.res].
23266 The parser flags FLAGS is used to control type-specifier parsing.
23267 CTOR_DTOR_OR_CONV_P, MEMBER_P, FRIEND_P, and STATIC_P are
23268 as for cp_parser_declarator. */
23270 static cp_declarator *
23271 cp_parser_direct_declarator (cp_parser* parser,
23272 cp_parser_declarator_kind dcl_kind,
23273 cp_parser_flags flags,
23274 int* ctor_dtor_or_conv_p,
23275 bool member_p, bool friend_p, bool static_p)
23277 cp_token *token;
23278 cp_declarator *declarator = NULL;
23279 tree scope = NULL_TREE;
23280 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
23281 bool saved_in_declarator_p = parser->in_declarator_p;
23282 bool first = true;
23283 tree pushed_scope = NULL_TREE;
23284 cp_token *open_paren = NULL, *close_paren = NULL;
23286 while (true)
23288 /* Peek at the next token. */
23289 token = cp_lexer_peek_token (parser->lexer);
23290 if (token->type == CPP_OPEN_PAREN)
23292 /* This is either a parameter-declaration-clause, or a
23293 parenthesized declarator. When we know we are parsing a
23294 named declarator, it must be a parenthesized declarator
23295 if FIRST is true. For instance, `(int)' is a
23296 parameter-declaration-clause, with an omitted
23297 direct-abstract-declarator. But `((*))', is a
23298 parenthesized abstract declarator. Finally, when T is a
23299 template parameter `(T)' is a
23300 parameter-declaration-clause, and not a parenthesized
23301 named declarator.
23303 We first try and parse a parameter-declaration-clause,
23304 and then try a nested declarator (if FIRST is true).
23306 It is not an error for it not to be a
23307 parameter-declaration-clause, even when FIRST is
23308 false. Consider,
23310 int i (int);
23311 int i (3);
23313 The first is the declaration of a function while the
23314 second is the definition of a variable, including its
23315 initializer.
23317 Having seen only the parenthesis, we cannot know which of
23318 these two alternatives should be selected. Even more
23319 complex are examples like:
23321 int i (int (a));
23322 int i (int (3));
23324 The former is a function-declaration; the latter is a
23325 variable initialization.
23327 Thus again, we try a parameter-declaration-clause, and if
23328 that fails, we back out and return. */
23330 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
23332 tree params;
23333 bool is_declarator = false;
23335 open_paren = NULL;
23337 /* In a member-declarator, the only valid interpretation
23338 of a parenthesis is the start of a
23339 parameter-declaration-clause. (It is invalid to
23340 initialize a static data member with a parenthesized
23341 initializer; only the "=" form of initialization is
23342 permitted.) */
23343 if (!member_p)
23344 cp_parser_parse_tentatively (parser);
23346 /* Consume the `('. */
23347 const location_t parens_start = token->location;
23348 matching_parens parens;
23349 parens.consume_open (parser);
23350 if (first)
23352 /* If this is going to be an abstract declarator, we're
23353 in a declarator and we can't have default args. */
23354 parser->default_arg_ok_p = false;
23355 parser->in_declarator_p = true;
23358 begin_scope (sk_function_parms, NULL_TREE);
23360 /* Parse the parameter-declaration-clause. */
23361 params
23362 = cp_parser_parameter_declaration_clause (parser, flags);
23363 const location_t parens_end
23364 = cp_lexer_peek_token (parser->lexer)->location;
23366 /* Consume the `)'. */
23367 parens.require_close (parser);
23369 /* If all went well, parse the cv-qualifier-seq,
23370 ref-qualifier and the exception-specification. */
23371 if (member_p || cp_parser_parse_definitely (parser))
23373 cp_cv_quals cv_quals;
23374 cp_virt_specifiers virt_specifiers;
23375 cp_ref_qualifier ref_qual;
23376 tree exception_specification;
23377 tree late_return;
23378 tree attrs;
23379 bool memfn = (member_p || (pushed_scope
23380 && CLASS_TYPE_P (pushed_scope)));
23381 unsigned char local_variables_forbidden_p
23382 = parser->local_variables_forbidden_p;
23383 /* 'this' is not allowed in static member functions. */
23384 if (static_p || friend_p)
23385 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
23387 is_declarator = true;
23389 if (ctor_dtor_or_conv_p)
23390 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
23391 first = false;
23393 /* Parse the cv-qualifier-seq. */
23394 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
23395 /* Parse the ref-qualifier. */
23396 ref_qual = cp_parser_ref_qualifier_opt (parser);
23397 /* Parse the tx-qualifier. */
23398 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
23400 tree save_ccp = current_class_ptr;
23401 tree save_ccr = current_class_ref;
23402 if (memfn && !friend_p && !static_p)
23403 /* DR 1207: 'this' is in scope after the cv-quals. */
23404 inject_this_parameter (current_class_type, cv_quals);
23406 /* If it turned out that this is e.g. a pointer to a
23407 function, we don't want to delay noexcept parsing. */
23408 if (declarator == NULL || declarator->kind != cdk_id)
23409 flags &= ~CP_PARSER_FLAGS_DELAY_NOEXCEPT;
23411 /* Parse the exception-specification. */
23412 exception_specification
23413 = cp_parser_exception_specification_opt (parser,
23414 flags);
23416 attrs = cp_parser_std_attribute_spec_seq (parser);
23418 cp_omp_declare_simd_data odsd;
23419 if ((flag_openmp || flag_openmp_simd)
23420 && declarator
23421 && declarator->std_attributes
23422 && declarator->kind == cdk_id)
23424 tree *pa = &declarator->std_attributes;
23425 cp_parser_handle_directive_omp_attributes (parser, pa,
23426 &odsd, false);
23429 /* In here, we handle cases where attribute is used after
23430 the function declaration. For example:
23431 void func (int x) __attribute__((vector(..))); */
23432 tree gnu_attrs = NULL_TREE;
23433 tree requires_clause = NULL_TREE;
23434 late_return
23435 = cp_parser_late_return_type_opt (parser, declarator,
23436 requires_clause);
23438 cp_finalize_omp_declare_simd (parser, &odsd);
23440 /* Parse the virt-specifier-seq. */
23441 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
23443 location_t parens_loc = make_location (parens_start,
23444 parens_start,
23445 parens_end);
23446 /* Create the function-declarator. */
23447 declarator = make_call_declarator (declarator,
23448 params,
23449 cv_quals,
23450 virt_specifiers,
23451 ref_qual,
23452 tx_qual,
23453 exception_specification,
23454 late_return,
23455 requires_clause,
23456 attrs,
23457 parens_loc);
23458 declarator->attributes = gnu_attrs;
23459 /* Any subsequent parameter lists are to do with
23460 return type, so are not those of the declared
23461 function. */
23462 parser->default_arg_ok_p = false;
23464 current_class_ptr = save_ccp;
23465 current_class_ref = save_ccr;
23467 /* Restore the state of local_variables_forbidden_p. */
23468 parser->local_variables_forbidden_p
23469 = local_variables_forbidden_p;
23472 /* Remove the function parms from scope. */
23473 pop_bindings_and_leave_scope ();
23475 if (is_declarator)
23476 /* Repeat the main loop. */
23477 continue;
23480 /* If this is the first, we can try a parenthesized
23481 declarator. */
23482 if (first)
23484 bool saved_in_type_id_in_expr_p;
23486 parser->default_arg_ok_p = saved_default_arg_ok_p;
23487 parser->in_declarator_p = saved_in_declarator_p;
23489 open_paren = token;
23490 /* Consume the `('. */
23491 matching_parens parens;
23492 parens.consume_open (parser);
23493 /* Parse the nested declarator. */
23494 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
23495 parser->in_type_id_in_expr_p = true;
23496 declarator
23497 = cp_parser_declarator (parser, dcl_kind, flags,
23498 ctor_dtor_or_conv_p,
23499 /*parenthesized_p=*/NULL,
23500 member_p, friend_p,
23501 /*static_p=*/false);
23502 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
23503 first = false;
23504 /* Expect a `)'. */
23505 close_paren = cp_lexer_peek_token (parser->lexer);
23506 if (!parens.require_close (parser))
23507 declarator = cp_error_declarator;
23508 if (declarator == cp_error_declarator)
23509 break;
23511 goto handle_declarator;
23513 /* Otherwise, we must be done. */
23514 else
23515 break;
23517 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
23518 && token->type == CPP_OPEN_SQUARE
23519 && !cp_next_tokens_can_be_attribute_p (parser))
23521 /* Parse an array-declarator. */
23522 tree bounds, attrs;
23524 if (ctor_dtor_or_conv_p)
23525 *ctor_dtor_or_conv_p = 0;
23527 open_paren = NULL;
23528 first = false;
23529 parser->default_arg_ok_p = false;
23530 parser->in_declarator_p = true;
23531 /* Consume the `['. */
23532 cp_lexer_consume_token (parser->lexer);
23533 /* Peek at the next token. */
23534 token = cp_lexer_peek_token (parser->lexer);
23535 /* If the next token is `]', then there is no
23536 constant-expression. */
23537 if (token->type != CPP_CLOSE_SQUARE)
23539 bool non_constant_p;
23540 bounds
23541 = cp_parser_constant_expression (parser,
23542 /*allow_non_constant=*/true,
23543 &non_constant_p);
23544 if (!non_constant_p)
23545 /* OK */;
23546 else if (error_operand_p (bounds))
23547 /* Already gave an error. */;
23548 else if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
23549 /* Let compute_array_index_type diagnose this. */;
23550 else if (!parser->in_function_body
23551 || parsing_function_declarator ())
23553 /* Normally, the array bound must be an integral constant
23554 expression. However, as an extension, we allow VLAs
23555 in function scopes as long as they aren't part of a
23556 parameter declaration. */
23557 cp_parser_error (parser,
23558 "array bound is not an integer constant");
23559 bounds = error_mark_node;
23561 else if (processing_template_decl
23562 && !type_dependent_expression_p (bounds))
23564 /* Remember this wasn't a constant-expression. */
23565 bounds = build_nop (TREE_TYPE (bounds), bounds);
23566 TREE_SIDE_EFFECTS (bounds) = 1;
23569 else
23570 bounds = NULL_TREE;
23571 /* Look for the closing `]'. */
23572 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
23574 declarator = cp_error_declarator;
23575 break;
23578 attrs = cp_parser_std_attribute_spec_seq (parser);
23579 declarator = make_array_declarator (declarator, bounds);
23580 declarator->std_attributes = attrs;
23582 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
23585 tree qualifying_scope;
23586 tree unqualified_name;
23587 tree attrs;
23588 special_function_kind sfk;
23589 bool abstract_ok;
23590 bool pack_expansion_p = false;
23591 cp_token *declarator_id_start_token;
23593 /* Parse a declarator-id */
23594 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
23595 if (abstract_ok)
23597 cp_parser_parse_tentatively (parser);
23599 /* If we see an ellipsis, we should be looking at a
23600 parameter pack. */
23601 if (token->type == CPP_ELLIPSIS)
23603 /* Consume the `...' */
23604 cp_lexer_consume_token (parser->lexer);
23606 pack_expansion_p = true;
23610 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
23611 unqualified_name
23612 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
23613 qualifying_scope = parser->scope;
23614 if (abstract_ok)
23616 bool okay = false;
23618 if (!unqualified_name && pack_expansion_p)
23620 /* Check whether an error occurred. */
23621 okay = !cp_parser_error_occurred (parser);
23623 /* We already consumed the ellipsis to mark a
23624 parameter pack, but we have no way to report it,
23625 so abort the tentative parse. We will be exiting
23626 immediately anyway. */
23627 cp_parser_abort_tentative_parse (parser);
23629 else
23630 okay = cp_parser_parse_definitely (parser);
23632 if (!okay)
23633 unqualified_name = error_mark_node;
23634 else if (unqualified_name
23635 && (qualifying_scope
23636 || (!identifier_p (unqualified_name))))
23638 cp_parser_error (parser, "expected unqualified-id");
23639 unqualified_name = error_mark_node;
23643 if (!unqualified_name)
23644 return NULL;
23645 if (unqualified_name == error_mark_node)
23647 declarator = cp_error_declarator;
23648 pack_expansion_p = false;
23649 declarator->parameter_pack_p = false;
23650 break;
23653 attrs = cp_parser_std_attribute_spec_seq (parser);
23655 if (qualifying_scope && at_namespace_scope_p ()
23656 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
23658 /* In the declaration of a member of a template class
23659 outside of the class itself, the SCOPE will sometimes
23660 be a TYPENAME_TYPE. For example, given:
23662 template <typename T>
23663 int S<T>::R::i = 3;
23665 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
23666 this context, we must resolve S<T>::R to an ordinary
23667 type, rather than a typename type.
23669 The reason we normally avoid resolving TYPENAME_TYPEs
23670 is that a specialization of `S' might render
23671 `S<T>::R' not a type. However, if `S' is
23672 specialized, then this `i' will not be used, so there
23673 is no harm in resolving the types here. */
23674 tree type;
23676 /* Resolve the TYPENAME_TYPE. */
23677 type = resolve_typename_type (qualifying_scope,
23678 /*only_current_p=*/false);
23679 /* If that failed, the declarator is invalid. */
23680 if (TREE_CODE (type) == TYPENAME_TYPE)
23682 if (typedef_variant_p (type))
23683 error_at (declarator_id_start_token->location,
23684 "cannot define member of dependent typedef "
23685 "%qT", type);
23686 else
23687 error_at (declarator_id_start_token->location,
23688 "%<%T::%E%> is not a type",
23689 TYPE_CONTEXT (qualifying_scope),
23690 TYPE_IDENTIFIER (qualifying_scope));
23692 qualifying_scope = type;
23695 sfk = sfk_none;
23697 if (unqualified_name)
23699 tree class_type;
23701 if (qualifying_scope
23702 && CLASS_TYPE_P (qualifying_scope))
23703 class_type = qualifying_scope;
23704 else
23705 class_type = current_class_type;
23707 if (TREE_CODE (unqualified_name) == TYPE_DECL)
23709 tree name_type = TREE_TYPE (unqualified_name);
23711 if (!class_type || !same_type_p (name_type, class_type))
23713 /* We do not attempt to print the declarator
23714 here because we do not have enough
23715 information about its original syntactic
23716 form. */
23717 cp_parser_error (parser, "invalid declarator");
23718 declarator = cp_error_declarator;
23719 break;
23721 else if (qualifying_scope
23722 && CLASSTYPE_USE_TEMPLATE (name_type))
23724 error_at (declarator_id_start_token->location,
23725 "invalid use of constructor as a template");
23726 inform (declarator_id_start_token->location,
23727 "use %<%T::%D%> instead of %<%T::%D%> to "
23728 "name the constructor in a qualified name",
23729 class_type,
23730 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
23731 class_type, name_type);
23732 declarator = cp_error_declarator;
23733 break;
23735 unqualified_name = constructor_name (class_type);
23738 if (class_type)
23740 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
23741 sfk = sfk_destructor;
23742 else if (identifier_p (unqualified_name)
23743 && IDENTIFIER_CONV_OP_P (unqualified_name))
23744 sfk = sfk_conversion;
23745 else if (/* There's no way to declare a constructor
23746 for an unnamed type, even if the type
23747 got a name for linkage purposes. */
23748 !TYPE_WAS_UNNAMED (class_type)
23749 /* Handle correctly (c++/19200):
23751 struct S {
23752 struct T{};
23753 friend void S(T);
23756 and also:
23758 namespace N {
23759 void S();
23762 struct S {
23763 friend void N::S();
23764 }; */
23765 && (!friend_p || class_type == qualifying_scope)
23766 && constructor_name_p (unqualified_name,
23767 class_type))
23768 sfk = sfk_constructor;
23769 else if (is_overloaded_fn (unqualified_name)
23770 && DECL_CONSTRUCTOR_P (get_first_fn
23771 (unqualified_name)))
23772 sfk = sfk_constructor;
23774 if (ctor_dtor_or_conv_p && sfk != sfk_none)
23775 *ctor_dtor_or_conv_p = -1;
23778 declarator = make_id_declarator (qualifying_scope,
23779 unqualified_name,
23780 sfk, token->location);
23781 declarator->std_attributes = attrs;
23782 declarator->parameter_pack_p = pack_expansion_p;
23784 if (pack_expansion_p)
23785 maybe_warn_variadic_templates ();
23787 /* We're looking for this case in [temp.res]:
23788 A qualified-id is assumed to name a type if [...]
23789 - it is a decl-specifier of the decl-specifier-seq of a
23790 parameter-declaration in a declarator of a function or
23791 function template declaration, ... */
23792 if (cxx_dialect >= cxx20
23793 && (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL)
23794 && declarator->kind == cdk_id
23795 && !at_class_scope_p ()
23796 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23798 /* ...whose declarator-id is qualified. If it isn't, never
23799 assume the parameters to refer to types. */
23800 if (qualifying_scope == NULL_TREE)
23801 flags &= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
23802 else
23804 /* Now we have something like
23805 template <typename T> int C::x(S::p);
23806 which can be a function template declaration or a
23807 variable template definition. If name lookup for
23808 the declarator-id C::x finds one or more function
23809 templates, assume S::p to name a type. Otherwise,
23810 don't. */
23811 tree decl
23812 = cp_parser_lookup_name (parser, unqualified_name,
23813 none_type,
23814 /*is_template=*/false,
23815 /*is_namespace=*/false,
23816 /*check_dependency=*/false,
23817 /*ambiguous_decls=*/NULL,
23818 token->location);
23820 if (!is_overloaded_fn (decl)
23821 /* Allow
23822 template<typename T>
23823 A<T>::A(T::type) { } */
23824 && !(MAYBE_CLASS_TYPE_P (qualifying_scope)
23825 && constructor_name_p (unqualified_name,
23826 qualifying_scope)))
23827 flags &= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
23832 handle_declarator:;
23833 scope = get_scope_of_declarator (declarator);
23834 if (scope)
23836 /* Any names that appear after the declarator-id for a
23837 member are looked up in the containing scope. */
23838 if (at_function_scope_p ())
23840 /* But declarations with qualified-ids can't appear in a
23841 function. */
23842 cp_parser_error (parser, "qualified-id in declaration");
23843 declarator = cp_error_declarator;
23844 break;
23846 pushed_scope = push_scope (scope);
23848 parser->in_declarator_p = true;
23849 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
23850 || (declarator && declarator->kind == cdk_id))
23851 /* Default args are only allowed on function
23852 declarations. */
23853 parser->default_arg_ok_p = saved_default_arg_ok_p;
23854 else
23855 parser->default_arg_ok_p = false;
23857 first = false;
23859 /* We're done. */
23860 else
23861 break;
23864 /* For an abstract declarator, we might wind up with nothing at this
23865 point. That's an error; the declarator is not optional. */
23866 if (!declarator)
23867 cp_parser_error (parser, "expected declarator");
23868 else if (open_paren)
23870 /* Record overly parenthesized declarator so we can give a
23871 diagnostic about confusing decl/expr disambiguation. */
23872 if (declarator->kind == cdk_array)
23874 /* If the open and close parens are on different lines, this
23875 is probably a formatting thing, so ignore. */
23876 expanded_location open = expand_location (open_paren->location);
23877 expanded_location close = expand_location (close_paren->location);
23878 if (open.line != close.line || open.file != close.file)
23879 open_paren = NULL;
23881 if (open_paren)
23882 declarator->parenthesized = make_location (open_paren->location,
23883 open_paren->location,
23884 close_paren->location);
23887 /* If we entered a scope, we must exit it now. */
23888 if (pushed_scope)
23889 pop_scope (pushed_scope);
23891 parser->default_arg_ok_p = saved_default_arg_ok_p;
23892 parser->in_declarator_p = saved_in_declarator_p;
23894 return declarator;
23897 /* Parse a ptr-operator.
23899 ptr-operator:
23900 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
23901 * cv-qualifier-seq [opt]
23903 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
23904 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
23906 GNU Extension:
23908 ptr-operator:
23909 & cv-qualifier-seq [opt]
23911 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
23912 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
23913 an rvalue reference. In the case of a pointer-to-member, *TYPE is
23914 filled in with the TYPE containing the member. *CV_QUALS is
23915 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
23916 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
23917 Note that the tree codes returned by this function have nothing
23918 to do with the types of trees that will be eventually be created
23919 to represent the pointer or reference type being parsed. They are
23920 just constants with suggestive names. */
23921 static enum tree_code
23922 cp_parser_ptr_operator (cp_parser* parser,
23923 tree* type,
23924 cp_cv_quals *cv_quals,
23925 tree *attributes)
23927 enum tree_code code = ERROR_MARK;
23928 cp_token *token;
23929 tree attrs = NULL_TREE;
23931 /* Assume that it's not a pointer-to-member. */
23932 *type = NULL_TREE;
23933 /* And that there are no cv-qualifiers. */
23934 *cv_quals = TYPE_UNQUALIFIED;
23936 /* Peek at the next token. */
23937 token = cp_lexer_peek_token (parser->lexer);
23939 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
23940 if (token->type == CPP_MULT)
23941 code = INDIRECT_REF;
23942 else if (token->type == CPP_AND)
23943 code = ADDR_EXPR;
23944 else if ((cxx_dialect != cxx98) &&
23945 token->type == CPP_AND_AND) /* C++0x only */
23946 code = NON_LVALUE_EXPR;
23948 if (code != ERROR_MARK)
23950 /* Consume the `*', `&' or `&&'. */
23951 cp_lexer_consume_token (parser->lexer);
23953 /* A `*' can be followed by a cv-qualifier-seq, and so can a
23954 `&', if we are allowing GNU extensions. (The only qualifier
23955 that can legally appear after `&' is `restrict', but that is
23956 enforced during semantic analysis. */
23957 if (code == INDIRECT_REF
23958 || cp_parser_allow_gnu_extensions_p (parser))
23959 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
23961 attrs = cp_parser_std_attribute_spec_seq (parser);
23962 if (attributes != NULL)
23963 *attributes = attrs;
23965 else
23967 /* Try the pointer-to-member case. */
23968 cp_parser_parse_tentatively (parser);
23969 /* Look for the optional `::' operator. */
23970 cp_parser_global_scope_opt (parser,
23971 /*current_scope_valid_p=*/false);
23972 /* Look for the nested-name specifier. */
23973 token = cp_lexer_peek_token (parser->lexer);
23974 cp_parser_nested_name_specifier (parser,
23975 /*typename_keyword_p=*/false,
23976 /*check_dependency_p=*/true,
23977 /*type_p=*/false,
23978 /*is_declaration=*/false);
23979 /* If we found it, and the next token is a `*', then we are
23980 indeed looking at a pointer-to-member operator. */
23981 if (!cp_parser_error_occurred (parser)
23982 && cp_parser_require (parser, CPP_MULT, RT_MULT))
23984 /* Indicate that the `*' operator was used. */
23985 code = INDIRECT_REF;
23987 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
23988 error_at (token->location, "%qD is a namespace", parser->scope);
23989 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
23990 error_at (token->location, "cannot form pointer to member of "
23991 "non-class %q#T", parser->scope);
23992 else
23994 /* The type of which the member is a member is given by the
23995 current SCOPE. */
23996 *type = parser->scope;
23997 /* The next name will not be qualified. */
23998 parser->scope = NULL_TREE;
23999 parser->qualifying_scope = NULL_TREE;
24000 parser->object_scope = NULL_TREE;
24001 /* Look for optional c++11 attributes. */
24002 attrs = cp_parser_std_attribute_spec_seq (parser);
24003 if (attributes != NULL)
24004 *attributes = attrs;
24005 /* Look for the optional cv-qualifier-seq. */
24006 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
24009 /* If that didn't work we don't have a ptr-operator. */
24010 if (!cp_parser_parse_definitely (parser))
24011 cp_parser_error (parser, "expected ptr-operator");
24014 return code;
24017 /* Parse an (optional) cv-qualifier-seq.
24019 cv-qualifier-seq:
24020 cv-qualifier cv-qualifier-seq [opt]
24022 cv-qualifier:
24023 const
24024 volatile
24026 GNU Extension:
24028 cv-qualifier:
24029 __restrict__
24031 Returns a bitmask representing the cv-qualifiers. */
24033 static cp_cv_quals
24034 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
24036 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
24038 while (true)
24040 cp_token *token;
24041 cp_cv_quals cv_qualifier;
24043 /* Peek at the next token. */
24044 token = cp_lexer_peek_token (parser->lexer);
24045 /* See if it's a cv-qualifier. */
24046 switch (token->keyword)
24048 case RID_CONST:
24049 cv_qualifier = TYPE_QUAL_CONST;
24050 break;
24052 case RID_VOLATILE:
24053 cv_qualifier = TYPE_QUAL_VOLATILE;
24054 break;
24056 case RID_RESTRICT:
24057 cv_qualifier = TYPE_QUAL_RESTRICT;
24058 break;
24060 default:
24061 cv_qualifier = TYPE_UNQUALIFIED;
24062 break;
24065 if (!cv_qualifier)
24066 break;
24068 if (cv_quals & cv_qualifier)
24070 gcc_rich_location richloc (token->location);
24071 richloc.add_fixit_remove ();
24072 error_at (&richloc, "duplicate cv-qualifier");
24073 cp_lexer_purge_token (parser->lexer);
24075 else
24077 cp_lexer_consume_token (parser->lexer);
24078 cv_quals |= cv_qualifier;
24082 return cv_quals;
24085 /* Parse an (optional) ref-qualifier
24087 ref-qualifier:
24091 Returns cp_ref_qualifier representing ref-qualifier. */
24093 static cp_ref_qualifier
24094 cp_parser_ref_qualifier_opt (cp_parser* parser)
24096 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
24098 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
24099 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
24100 return ref_qual;
24102 while (true)
24104 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
24105 cp_token *token = cp_lexer_peek_token (parser->lexer);
24107 switch (token->type)
24109 case CPP_AND:
24110 curr_ref_qual = REF_QUAL_LVALUE;
24111 break;
24113 case CPP_AND_AND:
24114 curr_ref_qual = REF_QUAL_RVALUE;
24115 break;
24117 default:
24118 curr_ref_qual = REF_QUAL_NONE;
24119 break;
24122 if (!curr_ref_qual)
24123 break;
24124 else if (ref_qual)
24126 error_at (token->location, "multiple ref-qualifiers");
24127 cp_lexer_purge_token (parser->lexer);
24129 else
24131 ref_qual = curr_ref_qual;
24132 cp_lexer_consume_token (parser->lexer);
24136 return ref_qual;
24139 /* Parse an optional tx-qualifier.
24141 tx-qualifier:
24142 transaction_safe
24143 transaction_safe_dynamic */
24145 static tree
24146 cp_parser_tx_qualifier_opt (cp_parser *parser)
24148 cp_token *token = cp_lexer_peek_token (parser->lexer);
24149 if (token->type == CPP_NAME)
24151 tree name = token->u.value;
24152 const char *p = IDENTIFIER_POINTER (name);
24153 const int len = strlen ("transaction_safe");
24154 if (startswith (p, "transaction_safe"))
24156 p += len;
24157 if (*p == '\0'
24158 || !strcmp (p, "_dynamic"))
24160 cp_lexer_consume_token (parser->lexer);
24161 if (!flag_tm)
24163 error ("%qE requires %<-fgnu-tm%>", name);
24164 return NULL_TREE;
24166 else
24167 return name;
24171 return NULL_TREE;
24174 /* Parse an (optional) virt-specifier-seq.
24176 virt-specifier-seq:
24177 virt-specifier virt-specifier-seq [opt]
24179 virt-specifier:
24180 override
24181 final
24183 Returns a bitmask representing the virt-specifiers. */
24185 static cp_virt_specifiers
24186 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
24188 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
24190 while (true)
24192 cp_token *token;
24193 cp_virt_specifiers virt_specifier;
24195 /* Peek at the next token. */
24196 token = cp_lexer_peek_token (parser->lexer);
24197 /* See if it's a virt-specifier-qualifier. */
24198 if (token->type != CPP_NAME)
24199 break;
24200 if (id_equal (token->u.value, "override"))
24202 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
24203 virt_specifier = VIRT_SPEC_OVERRIDE;
24205 else if (id_equal (token->u.value, "final"))
24207 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
24208 virt_specifier = VIRT_SPEC_FINAL;
24210 else if (id_equal (token->u.value, "__final"))
24212 virt_specifier = VIRT_SPEC_FINAL;
24214 else
24215 break;
24217 if (virt_specifiers & virt_specifier)
24219 gcc_rich_location richloc (token->location);
24220 richloc.add_fixit_remove ();
24221 error_at (&richloc, "duplicate virt-specifier");
24222 cp_lexer_purge_token (parser->lexer);
24224 else
24226 cp_lexer_consume_token (parser->lexer);
24227 virt_specifiers |= virt_specifier;
24230 return virt_specifiers;
24233 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
24234 is in scope even though it isn't real. */
24236 void
24237 inject_this_parameter (tree ctype, cp_cv_quals quals)
24239 tree this_parm;
24241 if (current_class_ptr)
24243 /* We don't clear this between NSDMIs. Is it already what we want? */
24244 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
24245 if (DECL_P (current_class_ptr)
24246 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
24247 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
24248 && cp_type_quals (type) == quals)
24249 return;
24252 this_parm = build_this_parm (NULL_TREE, ctype, quals);
24253 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
24254 current_class_ptr = NULL_TREE;
24255 current_class_ref
24256 = cp_build_fold_indirect_ref (this_parm);
24257 current_class_ptr = this_parm;
24260 /* Return true iff our current scope is a non-static data member
24261 initializer. */
24263 bool
24264 parsing_nsdmi (void)
24266 /* We recognize NSDMI context by the context-less 'this' pointer set up
24267 by the function above. */
24268 if (current_class_ptr
24269 && TREE_CODE (current_class_ptr) == PARM_DECL
24270 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
24271 return true;
24272 return false;
24275 /* True if we're parsing a function declarator. */
24277 bool
24278 parsing_function_declarator ()
24280 /* this_entity is NULL for a function parameter scope while parsing the
24281 declarator; it is set when parsing the body of the function. */
24282 return (current_binding_level->kind == sk_function_parms
24283 && !current_binding_level->this_entity);
24286 /* Parse a late-specified return type, if any. This is not a separate
24287 non-terminal, but part of a function declarator, which looks like
24289 -> trailing-type-specifier-seq abstract-declarator(opt)
24291 Returns the type indicated by the type-id.
24293 In addition to this, parse any queued up #pragma omp declare simd
24294 clauses, and #pragma acc routine clauses.
24296 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
24297 function. */
24299 static tree
24300 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
24301 tree& requires_clause)
24303 cp_token *token;
24304 tree type = NULL_TREE;
24305 bool declare_simd_p = (parser->omp_declare_simd
24306 && declarator
24307 && declarator->kind == cdk_id);
24309 bool oacc_routine_p = (parser->oacc_routine
24310 && declarator
24311 && declarator->kind == cdk_id);
24313 /* Peek at the next token. */
24314 token = cp_lexer_peek_token (parser->lexer);
24315 /* A late-specified return type is indicated by an initial '->'. */
24316 if (token->type != CPP_DEREF
24317 && token->keyword != RID_REQUIRES
24318 && !(token->type == CPP_NAME
24319 && token->u.value == ridpointers[RID_REQUIRES])
24320 && !(declare_simd_p || oacc_routine_p))
24321 return NULL_TREE;
24323 if (token->type == CPP_DEREF)
24325 /* Consume the ->. */
24326 cp_lexer_consume_token (parser->lexer);
24328 type = cp_parser_trailing_type_id (parser);
24331 /* Function declarations may be followed by a trailing
24332 requires-clause. */
24333 requires_clause = cp_parser_requires_clause_opt (parser, false);
24335 if (declare_simd_p)
24336 declarator->attributes
24337 = cp_parser_late_parsing_omp_declare_simd (parser,
24338 declarator->attributes);
24339 if (oacc_routine_p)
24340 declarator->attributes
24341 = cp_parser_late_parsing_oacc_routine (parser,
24342 declarator->attributes);
24344 return type;
24347 /* Parse a declarator-id.
24349 declarator-id:
24350 id-expression
24351 :: [opt] nested-name-specifier [opt] type-name
24353 In the `id-expression' case, the value returned is as for
24354 cp_parser_id_expression if the id-expression was an unqualified-id.
24355 If the id-expression was a qualified-id, then a SCOPE_REF is
24356 returned. The first operand is the scope (either a NAMESPACE_DECL
24357 or TREE_TYPE), but the second is still just a representation of an
24358 unqualified-id. */
24360 static tree
24361 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
24363 tree id;
24364 /* The expression must be an id-expression. Assume that qualified
24365 names are the names of types so that:
24367 template <class T>
24368 int S<T>::R::i = 3;
24370 will work; we must treat `S<T>::R' as the name of a type.
24371 Similarly, assume that qualified names are templates, where
24372 required, so that:
24374 template <class T>
24375 int S<T>::R<T>::i = 3;
24377 will work, too. */
24378 id = cp_parser_id_expression (parser,
24379 /*template_keyword_p=*/false,
24380 /*check_dependency_p=*/false,
24381 /*template_p=*/NULL,
24382 /*declarator_p=*/true,
24383 optional_p);
24384 if (id && BASELINK_P (id))
24385 id = BASELINK_FUNCTIONS (id);
24386 return id;
24389 /* Parse a type-id.
24391 type-id:
24392 type-specifier-seq abstract-declarator [opt]
24394 The parser flags FLAGS is used to control type-specifier parsing.
24396 If IS_TEMPLATE_ARG is true, we are parsing a template argument.
24398 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
24399 i.e. we've just seen "->".
24401 Returns the TYPE specified. */
24403 static tree
24404 cp_parser_type_id_1 (cp_parser *parser, cp_parser_flags flags,
24405 bool is_template_arg, bool is_trailing_return,
24406 location_t *type_location)
24408 cp_decl_specifier_seq type_specifier_seq;
24409 cp_declarator *abstract_declarator;
24411 /* Parse the type-specifier-seq. */
24412 cp_parser_type_specifier_seq (parser, flags,
24413 /*is_declaration=*/false,
24414 is_trailing_return,
24415 &type_specifier_seq);
24416 if (type_location)
24417 *type_location = type_specifier_seq.locations[ds_type_spec];
24419 if (is_template_arg && type_specifier_seq.type
24420 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
24421 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
24422 /* A bare template name as a template argument is a template template
24423 argument, not a placeholder, so fail parsing it as a type argument. */
24425 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
24426 cp_parser_simulate_error (parser);
24427 return error_mark_node;
24429 if (type_specifier_seq.type == error_mark_node)
24430 return error_mark_node;
24432 /* There might or might not be an abstract declarator. */
24433 cp_parser_parse_tentatively (parser);
24434 /* Look for the declarator. */
24435 abstract_declarator
24436 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT,
24437 CP_PARSER_FLAGS_NONE, NULL,
24438 /*parenthesized_p=*/NULL,
24439 /*member_p=*/false,
24440 /*friend_p=*/false,
24441 /*static_p=*/false);
24442 /* Check to see if there really was a declarator. */
24443 if (!cp_parser_parse_definitely (parser))
24444 abstract_declarator = NULL;
24446 bool auto_typeid_ok = false;
24447 /* The concepts TS allows 'auto' as a type-id. */
24448 if (flag_concepts_ts)
24449 auto_typeid_ok = !parser->in_type_id_in_expr_p;
24450 /* DR 625 prohibits use of auto as a template-argument. We allow 'auto'
24451 outside the template-argument-list context here only for the sake of
24452 diagnostic: grokdeclarator then can emit a better error message for
24453 e.g. using T = auto. */
24454 else if (flag_concepts)
24455 auto_typeid_ok = (!parser->in_type_id_in_expr_p
24456 && !parser->in_template_argument_list_p);
24458 if (type_specifier_seq.type
24459 && !auto_typeid_ok
24460 /* None of the valid uses of 'auto' in C++14 involve the type-id
24461 nonterminal, but it is valid in a trailing-return-type. */
24462 && !(cxx_dialect >= cxx14 && is_trailing_return))
24463 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
24465 /* A type-id with type 'auto' is only ok if the abstract declarator
24466 is a function declarator with a late-specified return type.
24468 A type-id with 'auto' is also valid in a trailing-return-type
24469 in a compound-requirement. */
24470 if (abstract_declarator
24471 && abstract_declarator->kind == cdk_function
24472 && abstract_declarator->u.function.late_return_type)
24473 /* OK */;
24474 else if (parser->in_result_type_constraint_p)
24475 /* OK */;
24476 else
24478 if (!cp_parser_simulate_error (parser))
24480 location_t loc = type_specifier_seq.locations[ds_type_spec];
24481 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
24483 auto_diagnostic_group g;
24484 gcc_rich_location richloc (loc);
24485 richloc.add_fixit_insert_after ("<>");
24486 error_at (&richloc, "missing template arguments after %qE",
24487 tmpl);
24488 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
24489 tmpl);
24491 else if (parser->in_template_argument_list_p)
24492 error_at (loc, "%qT not permitted in template argument",
24493 auto_node);
24494 else
24495 error_at (loc, "invalid use of %qT", auto_node);
24497 return error_mark_node;
24501 return groktypename (&type_specifier_seq, abstract_declarator,
24502 is_template_arg);
24505 /* Wrapper for cp_parser_type_id_1. */
24507 static tree
24508 cp_parser_type_id (cp_parser *parser, cp_parser_flags flags,
24509 location_t *type_location)
24511 return cp_parser_type_id_1 (parser, flags, false, false, type_location);
24514 /* Wrapper for cp_parser_type_id_1. */
24516 static tree
24517 cp_parser_template_type_arg (cp_parser *parser)
24519 tree r;
24520 const char *saved_message = parser->type_definition_forbidden_message;
24521 parser->type_definition_forbidden_message
24522 = G_("types may not be defined in template arguments");
24523 r = cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_NONE, true, false, NULL);
24524 parser->type_definition_forbidden_message = saved_message;
24525 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
24527 error ("invalid use of %<auto%> in template argument");
24528 r = error_mark_node;
24530 return r;
24533 /* Wrapper for cp_parser_type_id_1. */
24535 static tree
24536 cp_parser_trailing_type_id (cp_parser *parser)
24538 return cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
24539 false, true, NULL);
24542 /* Parse a type-specifier-seq.
24544 type-specifier-seq:
24545 type-specifier type-specifier-seq [opt]
24547 GNU extension:
24549 type-specifier-seq:
24550 attributes type-specifier-seq [opt]
24552 The parser flags FLAGS is used to control type-specifier parsing.
24554 If IS_DECLARATION is true, we are at the start of a "condition" or
24555 exception-declaration, so we might be followed by a declarator-id.
24557 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
24558 i.e. we've just seen "->".
24560 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
24562 static void
24563 cp_parser_type_specifier_seq (cp_parser* parser,
24564 cp_parser_flags flags,
24565 bool is_declaration,
24566 bool is_trailing_return,
24567 cp_decl_specifier_seq *type_specifier_seq)
24569 bool seen_type_specifier = false;
24570 cp_token *start_token = NULL;
24572 /* Clear the TYPE_SPECIFIER_SEQ. */
24573 clear_decl_specs (type_specifier_seq);
24575 flags |= CP_PARSER_FLAGS_OPTIONAL;
24576 /* In the context of a trailing return type, enum E { } is an
24577 elaborated-type-specifier followed by a function-body, not an
24578 enum-specifier. */
24579 if (is_trailing_return)
24580 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
24582 /* Parse the type-specifiers and attributes. */
24583 while (true)
24585 tree type_specifier;
24586 bool is_cv_qualifier;
24588 /* Check for attributes first. */
24589 if (cp_next_tokens_can_be_attribute_p (parser))
24591 /* GNU attributes at the end of a declaration apply to the
24592 declaration as a whole, not to the trailing return type. So look
24593 ahead to see if these attributes are at the end. */
24594 if (seen_type_specifier && is_trailing_return
24595 && cp_next_tokens_can_be_gnu_attribute_p (parser))
24597 size_t n = cp_parser_skip_attributes_opt (parser, 1);
24598 cp_token *tok = cp_lexer_peek_nth_token (parser->lexer, n);
24599 if (tok->type == CPP_SEMICOLON || tok->type == CPP_COMMA
24600 || tok->type == CPP_EQ || tok->type == CPP_OPEN_BRACE)
24601 break;
24603 type_specifier_seq->attributes
24604 = attr_chainon (type_specifier_seq->attributes,
24605 cp_parser_attributes_opt (parser));
24606 continue;
24609 /* record the token of the beginning of the type specifier seq,
24610 for error reporting purposes*/
24611 if (!start_token)
24612 start_token = cp_lexer_peek_token (parser->lexer);
24614 /* Look for the type-specifier. */
24615 type_specifier = cp_parser_type_specifier (parser,
24616 flags,
24617 type_specifier_seq,
24618 /*is_declaration=*/false,
24619 NULL,
24620 &is_cv_qualifier);
24621 if (!type_specifier)
24623 /* If the first type-specifier could not be found, this is not a
24624 type-specifier-seq at all. */
24625 if (!seen_type_specifier)
24627 /* Set in_declarator_p to avoid skipping to the semicolon. */
24628 int in_decl = parser->in_declarator_p;
24629 parser->in_declarator_p = true;
24631 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
24632 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
24633 cp_parser_error (parser, "expected type-specifier");
24635 parser->in_declarator_p = in_decl;
24637 type_specifier_seq->type = error_mark_node;
24638 return;
24640 /* If subsequent type-specifiers could not be found, the
24641 type-specifier-seq is complete. */
24642 break;
24645 seen_type_specifier = true;
24646 /* The standard says that a condition can be:
24648 type-specifier-seq declarator = assignment-expression
24650 However, given:
24652 struct S {};
24653 if (int S = ...)
24655 we should treat the "S" as a declarator, not as a
24656 type-specifier. The standard doesn't say that explicitly for
24657 type-specifier-seq, but it does say that for
24658 decl-specifier-seq in an ordinary declaration. Perhaps it
24659 would be clearer just to allow a decl-specifier-seq here, and
24660 then add a semantic restriction that if any decl-specifiers
24661 that are not type-specifiers appear, the program is invalid. */
24662 if (is_declaration && !is_cv_qualifier)
24663 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
24667 /* Return whether the function currently being declared has an associated
24668 template parameter list. */
24670 static bool
24671 function_being_declared_is_template_p (cp_parser* parser)
24673 if (!current_template_parms || processing_template_parmlist)
24674 return false;
24676 if (parser->implicit_template_scope)
24677 return true;
24679 if (at_class_scope_p ()
24680 && TYPE_BEING_DEFINED (current_class_type))
24681 return parser->num_template_parameter_lists != 0;
24683 return ((int) parser->num_template_parameter_lists > template_class_depth
24684 (current_class_type));
24687 /* Parse a parameter-declaration-clause.
24689 parameter-declaration-clause:
24690 parameter-declaration-list [opt] ... [opt]
24691 parameter-declaration-list , ...
24693 The parser flags FLAGS is used to control type-specifier parsing.
24695 Returns a representation for the parameter declarations. A return
24696 value of NULL indicates a parameter-declaration-clause consisting
24697 only of an ellipsis. */
24699 static tree
24700 cp_parser_parameter_declaration_clause (cp_parser* parser,
24701 cp_parser_flags flags)
24703 tree parameters;
24704 cp_token *token;
24705 bool ellipsis_p;
24707 auto cleanup = make_temp_override
24708 (parser->auto_is_implicit_function_template_parm_p);
24710 if (!processing_specialization
24711 && !processing_template_parmlist
24712 && !processing_explicit_instantiation
24713 /* default_arg_ok_p tracks whether this is a parameter-clause for an
24714 actual function or a random abstract declarator. */
24715 && parser->default_arg_ok_p)
24716 if (!current_function_decl
24717 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
24718 parser->auto_is_implicit_function_template_parm_p = true;
24720 /* Peek at the next token. */
24721 token = cp_lexer_peek_token (parser->lexer);
24722 /* Check for trivial parameter-declaration-clauses. */
24723 if (token->type == CPP_ELLIPSIS)
24725 /* Consume the `...' token. */
24726 cp_lexer_consume_token (parser->lexer);
24727 return NULL_TREE;
24729 else if (token->type == CPP_CLOSE_PAREN)
24730 /* There are no parameters. */
24731 return void_list_node;
24732 /* Check for `(void)', too, which is a special case. */
24733 else if (token->keyword == RID_VOID
24734 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
24735 == CPP_CLOSE_PAREN))
24737 /* Consume the `void' token. */
24738 cp_lexer_consume_token (parser->lexer);
24739 /* There are no parameters. */
24740 return explicit_void_list_node;
24743 /* A vector of parameters that haven't been pushed yet. */
24744 auto_vec<tree> pending_decls;
24746 /* Parse the parameter-declaration-list. */
24747 parameters = cp_parser_parameter_declaration_list (parser, flags,
24748 &pending_decls);
24749 /* If a parse error occurred while parsing the
24750 parameter-declaration-list, then the entire
24751 parameter-declaration-clause is erroneous. */
24752 if (parameters == error_mark_node)
24753 return NULL_TREE;
24755 /* Peek at the next token. */
24756 token = cp_lexer_peek_token (parser->lexer);
24757 /* If it's a `,', the clause should terminate with an ellipsis. */
24758 if (token->type == CPP_COMMA)
24760 /* Consume the `,'. */
24761 cp_lexer_consume_token (parser->lexer);
24762 /* Expect an ellipsis. */
24763 ellipsis_p
24764 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
24766 /* It might also be `...' if the optional trailing `,' was
24767 omitted. */
24768 else if (token->type == CPP_ELLIPSIS)
24770 /* Consume the `...' token. */
24771 cp_lexer_consume_token (parser->lexer);
24772 /* And remember that we saw it. */
24773 ellipsis_p = true;
24775 else
24776 ellipsis_p = false;
24778 /* A valid parameter-declaration-clause can only be followed by a ')'.
24779 So it's time to push all the parameters we have seen now that we
24780 know we have a valid declaration. Note that here we may not have
24781 committed yet, nor should we. Pushing here will detect the error
24782 of redefining a parameter. */
24783 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24784 for (tree p : pending_decls)
24785 pushdecl (p);
24787 /* Finish the parameter list. */
24788 if (!ellipsis_p)
24789 parameters = chainon (parameters, void_list_node);
24791 return parameters;
24794 /* Parse a parameter-declaration-list.
24796 parameter-declaration-list:
24797 parameter-declaration
24798 parameter-declaration-list , parameter-declaration
24800 The parser flags FLAGS is used to control type-specifier parsing.
24801 PENDING_DECLS is a vector of parameters that haven't been pushed yet.
24803 Returns a representation of the parameter-declaration-list, as for
24804 cp_parser_parameter_declaration_clause. However, the
24805 `void_list_node' is never appended to the list. */
24807 static tree
24808 cp_parser_parameter_declaration_list (cp_parser* parser,
24809 cp_parser_flags flags,
24810 auto_vec<tree> *pending_decls)
24812 tree parameters = NULL_TREE;
24813 tree *tail = &parameters;
24814 bool saved_in_unbraced_linkage_specification_p;
24815 int index = 0;
24817 /* The special considerations that apply to a function within an
24818 unbraced linkage specifications do not apply to the parameters
24819 to the function. */
24820 saved_in_unbraced_linkage_specification_p
24821 = parser->in_unbraced_linkage_specification_p;
24822 parser->in_unbraced_linkage_specification_p = false;
24824 /* Look for more parameters. */
24825 while (true)
24827 cp_parameter_declarator *parameter;
24828 tree decl = error_mark_node;
24829 bool parenthesized_p = false;
24831 /* Parse the parameter. */
24832 parameter
24833 = cp_parser_parameter_declaration (parser, flags,
24834 /*template_parm_p=*/false,
24835 &parenthesized_p);
24837 /* We don't know yet if the enclosing context is unavailable or deprecated,
24838 so wait and deal with it in grokparms if appropriate. */
24839 deprecated_state = UNAVAILABLE_DEPRECATED_SUPPRESS;
24841 if (parameter && !cp_parser_error_occurred (parser))
24843 decl = grokdeclarator (parameter->declarator,
24844 &parameter->decl_specifiers,
24845 PARM,
24846 parameter->default_argument != NULL_TREE,
24847 &parameter->decl_specifiers.attributes);
24848 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
24849 DECL_SOURCE_LOCATION (decl) = parameter->loc;
24852 deprecated_state = DEPRECATED_NORMAL;
24854 /* If a parse error occurred parsing the parameter declaration,
24855 then the entire parameter-declaration-list is erroneous. */
24856 if (decl == error_mark_node)
24858 parameters = error_mark_node;
24859 break;
24862 if (parameter->decl_specifiers.attributes)
24863 cplus_decl_attributes (&decl,
24864 parameter->decl_specifiers.attributes,
24866 if (DECL_NAME (decl))
24868 /* We cannot always pushdecl while parsing tentatively because
24869 it may have side effects and we can't be sure yet if we're
24870 parsing a declaration, e.g.:
24872 S foo(int(x), int(x), int{x});
24874 where it's not clear if we're dealing with a constructor call
24875 or a function declaration until we've seen the last argument
24876 which breaks it up.
24877 It's safe to pushdecl so long as it doesn't result in a clash
24878 with an already-pushed parameter. But we don't delay pushing
24879 different parameters to handle
24881 S foo(int(i), decltype(i) j = 42);
24883 which is valid. */
24884 if (pending_decls
24885 && cp_parser_uncommitted_to_tentative_parse_p (parser)
24886 /* See if PARAMETERS already contains a parameter with the same
24887 DECL_NAME as DECL. */
24888 && [parameters, decl] {
24889 for (tree p = parameters; p; p = TREE_CHAIN (p))
24890 if (DECL_NAME (decl) == DECL_NAME (TREE_VALUE (p)))
24891 return true;
24892 return false;
24893 }())
24894 pending_decls->safe_push (decl);
24895 else
24896 decl = pushdecl (decl);
24899 if (decl != error_mark_node)
24901 retrofit_lang_decl (decl);
24902 DECL_PARM_INDEX (decl) = ++index;
24903 DECL_PARM_LEVEL (decl) = function_parm_depth ();
24906 /* Add the new parameter to the list. */
24907 *tail = build_tree_list (parameter->default_argument, decl);
24908 tail = &TREE_CHAIN (*tail);
24910 /* If the parameters were parenthesized, it's the case of
24911 T foo(X(x)) which looks like a variable definition but
24912 is a function declaration. */
24913 if (index == 1 || PARENTHESIZED_LIST_P (parameters))
24914 PARENTHESIZED_LIST_P (parameters) = parenthesized_p;
24916 /* Peek at the next token. */
24917 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
24918 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
24919 /* These are for Objective-C++ */
24920 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
24921 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24922 /* The parameter-declaration-list is complete. */
24923 break;
24924 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24926 cp_token *token;
24928 /* Peek at the next token. */
24929 token = cp_lexer_peek_nth_token (parser->lexer, 2);
24930 /* If it's an ellipsis, then the list is complete. */
24931 if (token->type == CPP_ELLIPSIS)
24932 break;
24933 /* Otherwise, there must be more parameters. Consume the
24934 `,'. */
24935 cp_lexer_consume_token (parser->lexer);
24936 /* When parsing something like:
24938 int i(float f, double d)
24940 we can tell after seeing the declaration for "f" that we
24941 are not looking at an initialization of a variable "i",
24942 but rather at the declaration of a function "i".
24944 Due to the fact that the parsing of template arguments
24945 (as specified to a template-id) requires backtracking we
24946 cannot use this technique when inside a template argument
24947 list. */
24948 if (!parser->in_template_argument_list_p
24949 && !parser->in_type_id_in_expr_p
24950 && cp_parser_uncommitted_to_tentative_parse_p (parser)
24951 /* However, a parameter-declaration of the form
24952 "float(f)" (which is a valid declaration of a
24953 parameter "f") can also be interpreted as an
24954 expression (the conversion of "f" to "float"). */
24955 && !parenthesized_p)
24956 cp_parser_commit_to_tentative_parse (parser);
24958 else
24960 cp_parser_error (parser, "expected %<,%> or %<...%>");
24961 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
24962 cp_parser_skip_to_closing_parenthesis (parser,
24963 /*recovering=*/true,
24964 /*or_comma=*/false,
24965 /*consume_paren=*/false);
24966 break;
24970 parser->in_unbraced_linkage_specification_p
24971 = saved_in_unbraced_linkage_specification_p;
24973 /* Reset implicit_template_scope if we are about to leave the function
24974 parameter list that introduced it. Note that for out-of-line member
24975 definitions, there will be one or more class scopes before we get to
24976 the template parameter scope. */
24978 if (cp_binding_level *its = parser->implicit_template_scope)
24979 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
24981 while (maybe_its->kind == sk_class)
24982 maybe_its = maybe_its->level_chain;
24983 if (maybe_its == its)
24985 parser->implicit_template_parms = 0;
24986 parser->implicit_template_scope = 0;
24990 return parameters;
24993 /* Parse a parameter declaration.
24995 parameter-declaration:
24996 decl-specifier-seq ... [opt] declarator
24997 decl-specifier-seq declarator = assignment-expression
24998 decl-specifier-seq ... [opt] abstract-declarator [opt]
24999 decl-specifier-seq abstract-declarator [opt] = assignment-expression
25001 The parser flags FLAGS is used to control type-specifier parsing.
25003 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
25004 declares a template parameter. (In that case, a non-nested `>'
25005 token encountered during the parsing of the assignment-expression
25006 is not interpreted as a greater-than operator.)
25008 Returns a representation of the parameter, or NULL if an error
25009 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
25010 true iff the declarator is of the form "(p)". */
25012 static cp_parameter_declarator *
25013 cp_parser_parameter_declaration (cp_parser *parser,
25014 cp_parser_flags flags,
25015 bool template_parm_p,
25016 bool *parenthesized_p)
25018 int declares_class_or_enum;
25019 cp_decl_specifier_seq decl_specifiers;
25020 cp_declarator *declarator;
25021 tree default_argument;
25022 cp_token *token = NULL, *declarator_token_start = NULL;
25023 const char *saved_message;
25024 bool template_parameter_pack_p = false;
25026 /* In a template parameter, `>' is not an operator.
25028 [temp.param]
25030 When parsing a default template-argument for a non-type
25031 template-parameter, the first non-nested `>' is taken as the end
25032 of the template parameter-list rather than a greater-than
25033 operator. */
25035 /* Type definitions may not appear in parameter types. */
25036 saved_message = parser->type_definition_forbidden_message;
25037 parser->type_definition_forbidden_message
25038 = G_("types may not be defined in parameter types");
25040 int template_parm_idx = (function_being_declared_is_template_p (parser) ?
25041 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
25042 (current_template_parms)) : 0);
25044 /* Parse the declaration-specifiers. */
25045 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
25046 cp_parser_decl_specifier_seq (parser,
25047 flags,
25048 &decl_specifiers,
25049 &declares_class_or_enum);
25051 /* [dcl.spec.auto.general]: "A placeholder-type-specifier of the form
25052 type-constraint opt auto can be used as a decl-specifier of the
25053 decl-specifier-seq of a parameter-declaration of a function declaration
25054 or lambda-expression..." but we must not synthesize an implicit template
25055 type parameter in its declarator. That is, in "void f(auto[auto{10}]);"
25056 we want to synthesize only the first auto. */
25057 auto cleanup = make_temp_override
25058 (parser->auto_is_implicit_function_template_parm_p, false);
25060 /* Complain about missing 'typename' or other invalid type names. */
25061 if (!decl_specifiers.any_type_specifiers_p
25062 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
25063 decl_specifiers.type = error_mark_node;
25065 /* If an error occurred, there's no reason to attempt to parse the
25066 rest of the declaration. */
25067 if (cp_parser_error_occurred (parser))
25069 parser->type_definition_forbidden_message = saved_message;
25070 return NULL;
25073 /* Peek at the next token. */
25074 token = cp_lexer_peek_token (parser->lexer);
25076 /* If the next token is a `)', `,', `=', `>', or `...', then there
25077 is no declarator. However, when variadic templates are enabled,
25078 there may be a declarator following `...'. */
25079 if (token->type == CPP_CLOSE_PAREN
25080 || token->type == CPP_COMMA
25081 || token->type == CPP_EQ
25082 || token->type == CPP_GREATER)
25084 declarator = NULL;
25085 if (parenthesized_p)
25086 *parenthesized_p = false;
25088 /* Otherwise, there should be a declarator. */
25089 else
25091 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
25092 parser->default_arg_ok_p = false;
25094 /* After seeing a decl-specifier-seq, if the next token is not a
25095 "(" or "{", there is no possibility that the code is a valid
25096 expression. Therefore, if parsing tentatively, we commit at
25097 this point. */
25098 if (!parser->in_template_argument_list_p
25099 /* In an expression context, having seen:
25101 (int((char ...
25103 we cannot be sure whether we are looking at a
25104 function-type (taking a "char" as a parameter) or a cast
25105 of some object of type "char" to "int". */
25106 && !parser->in_type_id_in_expr_p
25107 && cp_parser_uncommitted_to_tentative_parse_p (parser)
25108 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
25110 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25112 if (decl_specifiers.type
25113 && template_placeholder_p (decl_specifiers.type))
25114 /* This is a CTAD expression, not a parameter declaration. */
25115 cp_parser_simulate_error (parser);
25117 else
25118 cp_parser_commit_to_tentative_parse (parser);
25120 /* Parse the declarator. */
25121 declarator_token_start = token;
25122 declarator = cp_parser_declarator (parser,
25123 CP_PARSER_DECLARATOR_EITHER,
25124 CP_PARSER_FLAGS_NONE,
25125 /*ctor_dtor_or_conv_p=*/NULL,
25126 parenthesized_p,
25127 /*member_p=*/false,
25128 /*friend_p=*/false,
25129 /*static_p=*/false);
25130 parser->default_arg_ok_p = saved_default_arg_ok_p;
25131 /* After the declarator, allow more attributes. */
25132 decl_specifiers.attributes
25133 = attr_chainon (decl_specifiers.attributes,
25134 cp_parser_attributes_opt (parser));
25136 /* If the declarator is a template parameter pack, remember that and
25137 clear the flag in the declarator itself so we don't get errors
25138 from grokdeclarator. */
25139 if (template_parm_p && declarator && declarator->parameter_pack_p)
25141 declarator->parameter_pack_p = false;
25142 template_parameter_pack_p = true;
25146 /* If the next token is an ellipsis, and we have not seen a declarator
25147 name, and if either the type of the declarator contains parameter
25148 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
25149 for, eg, abbreviated integral type names), then we actually have a
25150 parameter pack expansion expression. Otherwise, leave the ellipsis
25151 for a C-style variadic function. */
25152 token = cp_lexer_peek_token (parser->lexer);
25154 /* If a function parameter pack was specified and an implicit template
25155 parameter was introduced during cp_parser_parameter_declaration,
25156 change any implicit parameters introduced into packs. */
25157 if (parser->implicit_template_parms
25158 && ((token->type == CPP_ELLIPSIS
25159 && declarator_can_be_parameter_pack (declarator))
25160 || (declarator && declarator->parameter_pack_p)))
25162 int latest_template_parm_idx = TREE_VEC_LENGTH
25163 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
25165 if (latest_template_parm_idx != template_parm_idx)
25166 decl_specifiers.type = convert_generic_types_to_packs
25167 (decl_specifiers.type,
25168 template_parm_idx, latest_template_parm_idx);
25171 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25173 tree type = decl_specifiers.type;
25175 if (type && DECL_P (type))
25176 type = TREE_TYPE (type);
25178 if (((type
25179 && TREE_CODE (type) != TYPE_PACK_EXPANSION
25180 && (template_parm_p || uses_parameter_packs (type)))
25181 || (!type && template_parm_p))
25182 && declarator_can_be_parameter_pack (declarator))
25184 /* Consume the `...'. */
25185 cp_lexer_consume_token (parser->lexer);
25186 maybe_warn_variadic_templates ();
25188 /* Build a pack expansion type */
25189 if (template_parm_p)
25190 template_parameter_pack_p = true;
25191 else if (declarator)
25192 declarator->parameter_pack_p = true;
25193 else
25194 decl_specifiers.type = make_pack_expansion (type);
25198 /* The restriction on defining new types applies only to the type
25199 of the parameter, not to the default argument. */
25200 parser->type_definition_forbidden_message = saved_message;
25202 /* If the next token is `=', then process a default argument. */
25203 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
25205 tree type = decl_specifiers.type;
25206 token = cp_lexer_peek_token (parser->lexer);
25207 if (declarator)
25208 declarator->init_loc = token->location;
25209 /* If we are defining a class, then the tokens that make up the
25210 default argument must be saved and processed later. */
25211 if (!template_parm_p && at_class_scope_p ()
25212 && TYPE_BEING_DEFINED (current_class_type)
25213 && !LAMBDA_TYPE_P (current_class_type))
25214 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
25216 /* A constrained-type-specifier may declare a type
25217 template-parameter. */
25218 else if (declares_constrained_type_template_parameter (type))
25219 default_argument
25220 = cp_parser_default_type_template_argument (parser);
25222 /* A constrained-type-specifier may declare a
25223 template-template-parameter. */
25224 else if (declares_constrained_template_template_parameter (type))
25225 default_argument
25226 = cp_parser_default_template_template_argument (parser);
25228 /* Outside of a class definition, we can just parse the
25229 assignment-expression. */
25230 else
25231 default_argument
25232 = cp_parser_default_argument (parser, template_parm_p);
25234 if (!parser->default_arg_ok_p)
25236 permerror (token->location,
25237 "default arguments are only "
25238 "permitted for function parameters");
25240 else if ((declarator && declarator->parameter_pack_p)
25241 || template_parameter_pack_p
25242 || (decl_specifiers.type
25243 && PACK_EXPANSION_P (decl_specifiers.type)))
25245 /* Find the name of the parameter pack. */
25246 cp_declarator *id_declarator = declarator;
25247 while (id_declarator && id_declarator->kind != cdk_id)
25248 id_declarator = id_declarator->declarator;
25250 if (id_declarator && id_declarator->kind == cdk_id)
25251 error_at (declarator_token_start->location,
25252 template_parm_p
25253 ? G_("template parameter pack %qD "
25254 "cannot have a default argument")
25255 : G_("parameter pack %qD cannot have "
25256 "a default argument"),
25257 id_declarator->u.id.unqualified_name);
25258 else
25259 error_at (declarator_token_start->location,
25260 template_parm_p
25261 ? G_("template parameter pack cannot have "
25262 "a default argument")
25263 : G_("parameter pack cannot have a "
25264 "default argument"));
25266 default_argument = NULL_TREE;
25269 else
25270 default_argument = NULL_TREE;
25272 if (default_argument)
25273 STRIP_ANY_LOCATION_WRAPPER (default_argument);
25275 /* Generate a location for the parameter, ranging from the start of the
25276 initial token to the end of the final token (using input_location for
25277 the latter, set up by cp_lexer_set_source_position_from_token when
25278 consuming tokens).
25280 If we have a identifier, then use it for the caret location, e.g.
25282 extern int callee (int one, int (*two)(int, int), float three);
25283 ~~~~~~^~~~~~~~~~~~~~
25285 otherwise, reuse the start location for the caret location e.g.:
25287 extern int callee (int one, int (*)(int, int), float three);
25288 ^~~~~~~~~~~~~~~~~
25291 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
25292 ? declarator->id_loc
25293 : decl_spec_token_start->location);
25294 location_t param_loc = make_location (caret_loc,
25295 decl_spec_token_start->location,
25296 input_location);
25298 return make_parameter_declarator (&decl_specifiers,
25299 declarator,
25300 default_argument,
25301 param_loc,
25302 template_parameter_pack_p);
25305 /* Parse a default argument and return it.
25307 TEMPLATE_PARM_P is true if this is a default argument for a
25308 non-type template parameter. */
25309 static tree
25310 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
25312 tree default_argument = NULL_TREE;
25313 bool saved_greater_than_is_operator_p;
25314 unsigned char saved_local_variables_forbidden_p;
25315 bool non_constant_p, is_direct_init;
25317 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
25318 set correctly. */
25319 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
25320 parser->greater_than_is_operator_p = !template_parm_p;
25321 auto odsd = make_temp_override (parser->omp_declare_simd, NULL);
25322 auto ord = make_temp_override (parser->oacc_routine, NULL);
25323 auto oafp = make_temp_override (parser->omp_attrs_forbidden_p, false);
25325 /* Local variable names (and the `this' keyword) may not
25326 appear in a default argument. */
25327 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
25328 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
25329 /* Parse the assignment-expression. */
25330 if (template_parm_p)
25331 push_deferring_access_checks (dk_no_deferred);
25332 tree saved_class_ptr = NULL_TREE;
25333 tree saved_class_ref = NULL_TREE;
25334 /* The "this" pointer is not valid in a default argument. */
25335 if (cfun)
25337 saved_class_ptr = current_class_ptr;
25338 cp_function_chain->x_current_class_ptr = NULL_TREE;
25339 saved_class_ref = current_class_ref;
25340 cp_function_chain->x_current_class_ref = NULL_TREE;
25342 default_argument
25343 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
25344 /* Restore the "this" pointer. */
25345 if (cfun)
25347 cp_function_chain->x_current_class_ptr = saved_class_ptr;
25348 cp_function_chain->x_current_class_ref = saved_class_ref;
25350 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
25351 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
25352 if (template_parm_p)
25353 pop_deferring_access_checks ();
25354 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
25355 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
25357 return default_argument;
25360 /* Parse a function-body.
25362 function-body:
25363 compound_statement */
25365 static void
25366 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
25368 cp_parser_compound_statement (parser, NULL, (in_function_try_block
25369 ? BCS_TRY_BLOCK : BCS_NORMAL),
25370 true);
25373 /* Parse a ctor-initializer-opt followed by a function-body. Return
25374 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
25375 is true we are parsing a function-try-block. */
25377 static void
25378 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
25379 bool in_function_try_block)
25381 tree body, list;
25382 const bool check_body_p
25383 = (DECL_CONSTRUCTOR_P (current_function_decl)
25384 && DECL_DECLARED_CONSTEXPR_P (current_function_decl));
25385 tree last = NULL;
25387 if (in_function_try_block
25388 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
25389 && cxx_dialect < cxx20)
25391 if (DECL_CONSTRUCTOR_P (current_function_decl))
25392 pedwarn (input_location, OPT_Wc__20_extensions,
25393 "function-try-block body of %<constexpr%> constructor only "
25394 "available with %<-std=c++20%> or %<-std=gnu++20%>");
25395 else
25396 pedwarn (input_location, OPT_Wc__20_extensions,
25397 "function-try-block body of %<constexpr%> function only "
25398 "available with %<-std=c++20%> or %<-std=gnu++20%>");
25401 /* Begin the function body. */
25402 body = begin_function_body ();
25403 /* Parse the optional ctor-initializer. */
25404 cp_parser_ctor_initializer_opt (parser);
25406 /* If we're parsing a constexpr constructor definition, we need
25407 to check that the constructor body is indeed empty. However,
25408 before we get to cp_parser_function_body lot of junk has been
25409 generated, so we can't just check that we have an empty block.
25410 Rather we take a snapshot of the outermost block, and check whether
25411 cp_parser_function_body changed its state. */
25412 if (check_body_p)
25414 list = cur_stmt_list;
25415 if (STATEMENT_LIST_TAIL (list))
25416 last = STATEMENT_LIST_TAIL (list)->stmt;
25418 /* Parse the function-body. */
25419 cp_parser_function_body (parser, in_function_try_block);
25420 if (check_body_p)
25421 check_constexpr_ctor_body (last, list, /*complain=*/true);
25422 /* Finish the function body. */
25423 finish_function_body (body);
25426 /* Parse an initializer.
25428 initializer:
25429 = initializer-clause
25430 ( expression-list )
25432 Returns an expression representing the initializer. If no
25433 initializer is present, NULL_TREE is returned.
25435 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
25436 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
25437 set to TRUE if there is no initializer present. If there is an
25438 initializer, and it is not a constant-expression, *NON_CONSTANT_P
25439 is set to true; otherwise it is set to false. */
25441 static tree
25442 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
25443 bool* non_constant_p, bool subexpression_p)
25445 cp_token *token;
25446 tree init;
25448 /* Peek at the next token. */
25449 token = cp_lexer_peek_token (parser->lexer);
25451 /* Let our caller know whether or not this initializer was
25452 parenthesized. */
25453 *is_direct_init = (token->type != CPP_EQ);
25454 /* Assume that the initializer is constant. */
25455 *non_constant_p = false;
25457 if (token->type == CPP_EQ)
25459 /* Consume the `='. */
25460 cp_lexer_consume_token (parser->lexer);
25461 /* Parse the initializer-clause. */
25462 init = cp_parser_initializer_clause (parser, non_constant_p);
25464 else if (token->type == CPP_OPEN_PAREN)
25466 vec<tree, va_gc> *vec;
25467 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
25468 /*cast_p=*/false,
25469 /*allow_expansion_p=*/true,
25470 non_constant_p);
25471 if (vec == NULL)
25472 return error_mark_node;
25473 init = build_tree_list_vec (vec);
25474 release_tree_vector (vec);
25476 else if (token->type == CPP_OPEN_BRACE)
25478 cp_lexer_set_source_position (parser->lexer);
25479 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
25480 init = cp_parser_braced_list (parser, non_constant_p);
25481 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
25483 else
25485 /* Anything else is an error. */
25486 cp_parser_error (parser, "expected initializer");
25487 init = error_mark_node;
25490 if (!subexpression_p && check_for_bare_parameter_packs (init))
25491 init = error_mark_node;
25493 return init;
25496 /* Parse an initializer-clause.
25498 initializer-clause:
25499 assignment-expression
25500 braced-init-list
25502 Returns an expression representing the initializer.
25504 If the `assignment-expression' production is used the value
25505 returned is simply a representation for the expression.
25507 Otherwise, calls cp_parser_braced_list. */
25509 static cp_expr
25510 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
25512 cp_expr initializer;
25514 /* Assume the expression is constant. */
25515 *non_constant_p = false;
25517 /* If it is not a `{', then we are looking at an
25518 assignment-expression. */
25519 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
25521 initializer
25522 = cp_parser_constant_expression (parser,
25523 /*allow_non_constant_p=*/2,
25524 non_constant_p);
25526 else
25527 initializer = cp_parser_braced_list (parser, non_constant_p);
25529 return initializer;
25532 /* Parse a brace-enclosed initializer list.
25534 braced-init-list:
25535 { initializer-list , [opt] }
25536 { designated-initializer-list , [opt] }
25539 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
25540 the elements of the initializer-list (or NULL, if the last
25541 production is used). The TREE_TYPE for the CONSTRUCTOR will be
25542 NULL_TREE. There is no way to detect whether or not the optional
25543 trailing `,' was provided. NON_CONSTANT_P is as for
25544 cp_parser_initializer. */
25546 static cp_expr
25547 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
25549 tree initializer;
25550 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
25552 /* Consume the `{' token. */
25553 matching_braces braces;
25554 braces.require_open (parser);
25555 /* Create a CONSTRUCTOR to represent the braced-initializer. */
25556 initializer = make_node (CONSTRUCTOR);
25557 /* If it's not a `}', then there is a non-trivial initializer. */
25558 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
25560 bool designated;
25561 /* Parse the initializer list. */
25562 CONSTRUCTOR_ELTS (initializer)
25563 = cp_parser_initializer_list (parser, non_constant_p, &designated);
25564 CONSTRUCTOR_IS_DESIGNATED_INIT (initializer) = designated;
25565 /* A trailing `,' token is allowed. */
25566 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25567 cp_lexer_consume_token (parser->lexer);
25569 else
25570 *non_constant_p = false;
25571 /* Now, there should be a trailing `}'. */
25572 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
25573 braces.require_close (parser);
25574 TREE_TYPE (initializer) = init_list_type_node;
25575 recompute_constructor_flags (initializer);
25577 cp_expr result (initializer);
25578 /* Build a location of the form:
25579 { ... }
25580 ^~~~~~~
25581 with caret==start at the open brace, finish at the close brace. */
25582 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
25583 result.set_location (combined_loc);
25584 return result;
25587 /* Consume tokens up to, but not including, the next non-nested closing `]'.
25588 Returns true iff we found a closing `]'. */
25590 static bool
25591 cp_parser_skip_up_to_closing_square_bracket (cp_parser *parser)
25593 unsigned square_depth = 0;
25595 while (true)
25597 cp_token * token = cp_lexer_peek_token (parser->lexer);
25599 switch (token->type)
25601 case CPP_PRAGMA_EOL:
25602 if (!parser->lexer->in_pragma)
25603 break;
25604 /* FALLTHRU */
25606 case CPP_EOF:
25607 /* If we've run out of tokens, then there is no closing `]'. */
25608 return false;
25610 case CPP_OPEN_SQUARE:
25611 ++square_depth;
25612 break;
25614 case CPP_CLOSE_SQUARE:
25615 if (!square_depth--)
25616 return true;
25617 break;
25619 default:
25620 break;
25623 /* Consume the current token, skipping it. */
25624 cp_lexer_consume_token (parser->lexer);
25628 /* Consume tokens up to, and including, the next non-nested closing `]'.
25629 Returns true iff we found a closing `]'. */
25631 static bool
25632 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
25634 bool found = cp_parser_skip_up_to_closing_square_bracket (parser);
25635 if (found)
25636 cp_lexer_consume_token (parser->lexer);
25637 return found;
25640 /* Return true if we are looking at an array-designator, false otherwise. */
25642 static bool
25643 cp_parser_array_designator_p (cp_parser *parser)
25645 /* Consume the `['. */
25646 cp_lexer_consume_token (parser->lexer);
25648 cp_lexer_save_tokens (parser->lexer);
25650 /* Skip tokens until the next token is a closing square bracket.
25651 If we find the closing `]', and the next token is a `=', then
25652 we are looking at an array designator. */
25653 bool array_designator_p
25654 = (cp_parser_skip_to_closing_square_bracket (parser)
25655 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
25657 /* Roll back the tokens we skipped. */
25658 cp_lexer_rollback_tokens (parser->lexer);
25660 return array_designator_p;
25663 /* Parse an initializer-list.
25665 initializer-list:
25666 initializer-clause ... [opt]
25667 initializer-list , initializer-clause ... [opt]
25669 C++20 Extension:
25671 designated-initializer-list:
25672 designated-initializer-clause
25673 designated-initializer-list , designated-initializer-clause
25675 designated-initializer-clause:
25676 designator brace-or-equal-initializer
25678 designator:
25679 . identifier
25681 GNU Extension:
25683 initializer-list:
25684 designation initializer-clause ...[opt]
25685 initializer-list , designation initializer-clause ...[opt]
25687 designation:
25688 . identifier =
25689 identifier :
25690 [ constant-expression ] =
25692 Returns a vec of constructor_elt. The VALUE of each elt is an expression
25693 for the initializer. If the INDEX of the elt is non-NULL, it is the
25694 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
25695 as for cp_parser_initializer. Set *DESIGNATED to a boolean whether there
25696 are any designators. */
25698 static vec<constructor_elt, va_gc> *
25699 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p,
25700 bool *designated)
25702 vec<constructor_elt, va_gc> *v = NULL;
25703 bool first_p = true;
25704 tree first_designator = NULL_TREE;
25706 /* Assume all of the expressions are constant. */
25707 *non_constant_p = false;
25709 unsigned nelts = 0;
25710 int suppress = suppress_location_wrappers;
25712 /* Parse the rest of the list. */
25713 while (true)
25715 cp_token *token;
25716 tree designator;
25717 tree initializer;
25718 bool clause_non_constant_p;
25719 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25721 /* Handle the C++20 syntax, '. id ='. */
25722 if ((cxx_dialect >= cxx20
25723 || cp_parser_allow_gnu_extensions_p (parser))
25724 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
25725 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
25726 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
25727 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
25728 == CPP_OPEN_BRACE)))
25730 if (pedantic && cxx_dialect < cxx20)
25731 pedwarn (loc, OPT_Wc__20_extensions,
25732 "C++ designated initializers only available with "
25733 "%<-std=c++20%> or %<-std=gnu++20%>");
25734 /* Consume the `.'. */
25735 cp_lexer_consume_token (parser->lexer);
25736 /* Consume the identifier. */
25737 designator = cp_lexer_consume_token (parser->lexer)->u.value;
25738 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
25739 /* Consume the `='. */
25740 cp_lexer_consume_token (parser->lexer);
25742 /* Also, if the next token is an identifier and the following one is a
25743 colon, we are looking at the GNU designated-initializer
25744 syntax. */
25745 else if (cp_parser_allow_gnu_extensions_p (parser)
25746 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
25747 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
25748 == CPP_COLON))
25750 /* Warn the user that they are using an extension. */
25751 pedwarn (loc, OPT_Wpedantic,
25752 "ISO C++ does not allow GNU designated initializers");
25753 /* Consume the identifier. */
25754 designator = cp_lexer_consume_token (parser->lexer)->u.value;
25755 /* Consume the `:'. */
25756 cp_lexer_consume_token (parser->lexer);
25758 /* Also handle C99 array designators, '[ const ] ='. */
25759 else if (cp_parser_allow_gnu_extensions_p (parser)
25760 && !c_dialect_objc ()
25761 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
25763 /* In C++11, [ could start a lambda-introducer. */
25764 bool non_const = false;
25766 cp_parser_parse_tentatively (parser);
25768 if (!cp_parser_array_designator_p (parser))
25770 cp_parser_simulate_error (parser);
25771 designator = NULL_TREE;
25773 else
25775 designator = cp_parser_constant_expression (parser, true,
25776 &non_const);
25777 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25778 cp_parser_require (parser, CPP_EQ, RT_EQ);
25781 if (!cp_parser_parse_definitely (parser))
25782 designator = NULL_TREE;
25783 else if (non_const
25784 && (!require_potential_rvalue_constant_expression
25785 (designator)))
25786 designator = NULL_TREE;
25787 if (designator)
25788 /* Warn the user that they are using an extension. */
25789 pedwarn (loc, OPT_Wpedantic,
25790 "ISO C++ does not allow C99 designated initializers");
25792 else
25793 designator = NULL_TREE;
25795 if (first_p)
25797 first_designator = designator;
25798 first_p = false;
25800 else if (cxx_dialect >= cxx20
25801 && first_designator != error_mark_node
25802 && (!first_designator != !designator))
25804 error_at (loc, "either all initializer clauses should be designated "
25805 "or none of them should be");
25806 first_designator = error_mark_node;
25808 else if (cxx_dialect < cxx20 && !first_designator)
25809 first_designator = designator;
25811 /* Parse the initializer. */
25812 initializer = cp_parser_initializer_clause (parser,
25813 &clause_non_constant_p);
25814 /* If any clause is non-constant, so is the entire initializer. */
25815 if (clause_non_constant_p)
25816 *non_constant_p = true;
25818 /* If we have an ellipsis, this is an initializer pack
25819 expansion. */
25820 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25822 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25824 /* Consume the `...'. */
25825 cp_lexer_consume_token (parser->lexer);
25827 if (designator && cxx_dialect >= cxx20)
25828 error_at (loc,
25829 "%<...%> not allowed in designated initializer list");
25831 /* Turn the initializer into an initializer expansion. */
25832 initializer = make_pack_expansion (initializer);
25835 /* Add it to the vector. */
25836 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
25838 /* If the next token is not a comma, we have reached the end of
25839 the list. */
25840 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25841 break;
25843 /* Peek at the next token. */
25844 token = cp_lexer_peek_nth_token (parser->lexer, 2);
25845 /* If the next token is a `}', then we're still done. An
25846 initializer-clause can have a trailing `,' after the
25847 initializer-list and before the closing `}'. */
25848 if (token->type == CPP_CLOSE_BRACE)
25849 break;
25851 /* Suppress location wrappers in a long initializer to save memory
25852 (14179). The cutoff is chosen arbitrarily. */
25853 const unsigned loc_max = 256;
25854 unsigned incr = 1;
25855 if (TREE_CODE (initializer) == CONSTRUCTOR)
25856 /* Look one level down because it's easy. Looking deeper would require
25857 passing down a nelts pointer, and I don't think multi-level massive
25858 initializers are common enough to justify this. */
25859 incr = CONSTRUCTOR_NELTS (initializer);
25860 nelts += incr;
25861 if (nelts >= loc_max && (nelts - incr) < loc_max)
25862 ++suppress_location_wrappers;
25864 /* Consume the `,' token. */
25865 cp_lexer_consume_token (parser->lexer);
25868 /* The same identifier shall not appear in multiple designators
25869 of a designated-initializer-list. */
25870 if (first_designator)
25872 unsigned int i;
25873 tree designator, val;
25874 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
25875 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
25877 if (IDENTIFIER_MARKED (designator))
25879 error_at (cp_expr_loc_or_input_loc (val),
25880 "%<.%s%> designator used multiple times in "
25881 "the same initializer list",
25882 IDENTIFIER_POINTER (designator));
25883 (*v)[i].index = error_mark_node;
25885 else
25886 IDENTIFIER_MARKED (designator) = 1;
25888 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
25889 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
25890 IDENTIFIER_MARKED (designator) = 0;
25893 suppress_location_wrappers = suppress;
25895 *designated = first_designator != NULL_TREE;
25896 return v;
25899 /* Classes [gram.class] */
25901 /* Parse a class-name.
25903 class-name:
25904 identifier
25905 template-id
25907 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
25908 to indicate that names looked up in dependent types should be
25909 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
25910 keyword has been used to indicate that the name that appears next
25911 is a template. TAG_TYPE indicates the explicit tag given before
25912 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
25913 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
25914 is the class being defined in a class-head. If ENUM_OK is TRUE,
25915 enum-names are also accepted.
25917 Returns the TYPE_DECL representing the class. */
25919 static tree
25920 cp_parser_class_name (cp_parser *parser,
25921 bool typename_keyword_p,
25922 bool template_keyword_p,
25923 enum tag_types tag_type,
25924 bool check_dependency_p,
25925 bool class_head_p,
25926 bool is_declaration,
25927 bool enum_ok)
25929 tree decl;
25930 tree identifier = NULL_TREE;
25932 /* All class-names start with an identifier. */
25933 cp_token *token = cp_lexer_peek_token (parser->lexer);
25934 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
25936 cp_parser_error (parser, "expected class-name");
25937 return error_mark_node;
25940 /* PARSER->SCOPE can be cleared when parsing the template-arguments
25941 to a template-id, so we save it here. Consider object scope too,
25942 so that make_typename_type below can use it (cp_parser_template_name
25943 considers object scope also). This may happen with code like
25945 p->template A<T>::a()
25947 where we first want to look up A<T>::a in the class of the object
25948 expression, as per [basic.lookup.classref]. */
25949 tree scope = parser->scope ? parser->scope : parser->context->object_type;
25950 /* This only checks parser->scope to avoid duplicate errors; if
25951 ->object_type is erroneous, go on to give a parse error. */
25952 if (parser->scope == error_mark_node)
25953 return error_mark_node;
25955 /* Any name names a type if we're following the `typename' keyword
25956 in a qualified name where the enclosing scope is type-dependent. */
25957 const bool typename_p = (typename_keyword_p
25958 && parser->scope
25959 && TYPE_P (parser->scope)
25960 && dependent_scope_p (parser->scope));
25961 /* Handle the common case (an identifier, but not a template-id)
25962 efficiently. */
25963 if (token->type == CPP_NAME
25964 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
25966 cp_token *identifier_token;
25967 bool ambiguous_p;
25969 /* Look for the identifier. */
25970 identifier_token = cp_lexer_peek_token (parser->lexer);
25971 ambiguous_p = identifier_token->error_reported;
25972 identifier = cp_parser_identifier (parser);
25973 /* If the next token isn't an identifier, we are certainly not
25974 looking at a class-name. */
25975 if (identifier == error_mark_node)
25976 decl = error_mark_node;
25977 /* If we know this is a type-name, there's no need to look it
25978 up. */
25979 else if (typename_p)
25980 decl = identifier;
25981 else
25983 tree ambiguous_decls;
25984 /* If we already know that this lookup is ambiguous, then
25985 we've already issued an error message; there's no reason
25986 to check again. */
25987 if (ambiguous_p)
25989 cp_parser_simulate_error (parser);
25990 return error_mark_node;
25992 /* If the next token is a `::', then the name must be a type
25993 name.
25995 [basic.lookup.qual]
25997 During the lookup for a name preceding the :: scope
25998 resolution operator, object, function, and enumerator
25999 names are ignored. */
26000 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
26001 tag_type = scope_type;
26002 /* Look up the name. */
26003 decl = cp_parser_lookup_name (parser, identifier,
26004 tag_type,
26005 /*is_template=*/false,
26006 /*is_namespace=*/false,
26007 check_dependency_p,
26008 &ambiguous_decls,
26009 identifier_token->location);
26010 if (ambiguous_decls)
26012 if (cp_parser_parsing_tentatively (parser))
26013 cp_parser_simulate_error (parser);
26014 return error_mark_node;
26018 else
26020 /* Try a template-id. */
26021 decl = cp_parser_template_id (parser, template_keyword_p,
26022 check_dependency_p,
26023 tag_type,
26024 is_declaration);
26025 if (decl == error_mark_node)
26026 return error_mark_node;
26029 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
26031 /* If this is a typename, create a TYPENAME_TYPE. */
26032 if (typename_p && decl != error_mark_node)
26034 decl = make_typename_type (scope, decl, typename_type,
26035 /*complain=*/tf_error);
26036 if (decl != error_mark_node)
26037 decl = TYPE_NAME (decl);
26040 decl = strip_using_decl (decl);
26042 /* Check to see that it is really the name of a class. */
26043 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
26044 && identifier_p (TREE_OPERAND (decl, 0))
26045 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
26046 /* Situations like this:
26048 template <typename T> struct A {
26049 typename T::template X<int>::I i;
26052 are problematic. Is `T::template X<int>' a class-name? The
26053 standard does not seem to be definitive, but there is no other
26054 valid interpretation of the following `::'. Therefore, those
26055 names are considered class-names. */
26057 decl = make_typename_type (scope, decl, tag_type, tf_error);
26058 if (decl != error_mark_node)
26059 decl = TYPE_NAME (decl);
26061 else if (TREE_CODE (decl) != TYPE_DECL
26062 || TREE_TYPE (decl) == error_mark_node
26063 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
26064 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
26065 /* In Objective-C 2.0, a classname followed by '.' starts a
26066 dot-syntax expression, and it's not a type-name. */
26067 || (c_dialect_objc ()
26068 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
26069 && objc_is_class_name (decl)))
26070 decl = error_mark_node;
26072 if (decl == error_mark_node)
26073 cp_parser_error (parser, "expected class-name");
26074 else if (identifier && !parser->scope)
26075 maybe_note_name_used_in_class (identifier, decl);
26077 return decl;
26080 /* Make sure that any member-function parameters are in scope.
26081 For instance, a function's noexcept-specifier can use the function's
26082 parameters:
26084 struct S {
26085 void fn (int p) noexcept(noexcept(p));
26088 so we need to make sure name lookup can find them. This is used
26089 when we delay parsing of the noexcept-specifier. */
26091 static void
26092 inject_parm_decls (tree decl)
26094 begin_scope (sk_function_parms, decl);
26095 tree args = DECL_ARGUMENTS (decl);
26097 do_push_parm_decls (decl, args, /*nonparms=*/NULL);
26099 if (args && is_this_parameter (args))
26101 gcc_checking_assert (current_class_ptr == NULL_TREE);
26102 current_class_ref = cp_build_fold_indirect_ref (args);
26103 current_class_ptr = args;
26107 /* Undo the effects of inject_parm_decls. */
26109 static void
26110 pop_injected_parms (void)
26112 pop_bindings_and_leave_scope ();
26113 current_class_ptr = current_class_ref = NULL_TREE;
26116 /* Parse a class-specifier.
26118 class-specifier:
26119 class-head { member-specification [opt] }
26121 Returns the TREE_TYPE representing the class. */
26123 tree
26124 cp_parser_class_specifier (cp_parser* parser)
26126 auto_timevar tv (TV_PARSE_STRUCT);
26128 tree type;
26129 tree attributes = NULL_TREE;
26130 bool nested_name_specifier_p;
26131 unsigned saved_num_template_parameter_lists;
26132 bool saved_in_function_body;
26133 unsigned char in_statement;
26134 bool in_switch_statement_p;
26135 bool saved_in_unbraced_linkage_specification_p;
26136 tree old_scope = NULL_TREE;
26137 tree scope = NULL_TREE;
26138 cp_token *closing_brace;
26140 push_deferring_access_checks (dk_no_deferred);
26142 /* Parse the class-head. */
26143 type = cp_parser_class_head (parser,
26144 &nested_name_specifier_p);
26145 /* If the class-head was a semantic disaster, skip the entire body
26146 of the class. */
26147 if (!type)
26149 cp_parser_skip_to_end_of_block_or_statement (parser);
26150 pop_deferring_access_checks ();
26151 return error_mark_node;
26154 /* Look for the `{'. */
26155 matching_braces braces;
26156 if (!braces.require_open (parser))
26158 pop_deferring_access_checks ();
26159 return error_mark_node;
26162 cp_ensure_no_omp_declare_simd (parser);
26163 cp_ensure_no_oacc_routine (parser);
26165 /* Issue an error message if type-definitions are forbidden here. */
26166 bool type_definition_ok_p = cp_parser_check_type_definition (parser);
26167 /* Remember that we are defining one more class. */
26168 ++parser->num_classes_being_defined;
26169 /* Inside the class, surrounding template-parameter-lists do not
26170 apply. */
26171 saved_num_template_parameter_lists
26172 = parser->num_template_parameter_lists;
26173 parser->num_template_parameter_lists = 0;
26174 /* We are not in a function body. */
26175 saved_in_function_body = parser->in_function_body;
26176 parser->in_function_body = false;
26177 /* Or in a loop. */
26178 in_statement = parser->in_statement;
26179 parser->in_statement = 0;
26180 /* Or in a switch. */
26181 in_switch_statement_p = parser->in_switch_statement_p;
26182 parser->in_switch_statement_p = false;
26183 /* We are not immediately inside an extern "lang" block. */
26184 saved_in_unbraced_linkage_specification_p
26185 = parser->in_unbraced_linkage_specification_p;
26186 parser->in_unbraced_linkage_specification_p = false;
26187 /* 'this' from an enclosing non-static member function is unavailable. */
26188 tree saved_ccp = current_class_ptr;
26189 tree saved_ccr = current_class_ref;
26190 current_class_ptr = NULL_TREE;
26191 current_class_ref = NULL_TREE;
26193 /* Start the class. */
26194 if (nested_name_specifier_p)
26196 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
26197 /* SCOPE must be a scope nested inside current scope. */
26198 if (is_nested_namespace (current_namespace,
26199 decl_namespace_context (scope)))
26200 old_scope = push_inner_scope (scope);
26201 else
26202 nested_name_specifier_p = false;
26204 type = begin_class_definition (type);
26206 if (type == error_mark_node)
26207 /* If the type is erroneous, skip the entire body of the class. */
26208 cp_parser_skip_to_closing_brace (parser);
26209 else
26210 /* Parse the member-specification. */
26211 cp_parser_member_specification_opt (parser);
26213 /* Look for the trailing `}'. */
26214 closing_brace = braces.require_close (parser);
26215 /* Look for trailing attributes to apply to this class. */
26216 if (cp_parser_allow_gnu_extensions_p (parser))
26217 attributes = cp_parser_gnu_attributes_opt (parser);
26218 if (type != error_mark_node)
26219 type = finish_struct (type, attributes);
26220 if (nested_name_specifier_p)
26221 pop_inner_scope (old_scope, scope);
26223 /* We've finished a type definition. Check for the common syntax
26224 error of forgetting a semicolon after the definition. We need to
26225 be careful, as we can't just check for not-a-semicolon and be done
26226 with it; the user might have typed:
26228 class X { } c = ...;
26229 class X { } *p = ...;
26231 and so forth. Instead, enumerate all the possible tokens that
26232 might follow this production; if we don't see one of them, then
26233 complain and silently insert the semicolon. */
26235 cp_token *token = cp_lexer_peek_token (parser->lexer);
26236 bool want_semicolon = true;
26238 if (cp_next_tokens_can_be_std_attribute_p (parser))
26239 /* Don't try to parse c++11 attributes here. As per the
26240 grammar, that should be a task for
26241 cp_parser_decl_specifier_seq. */
26242 want_semicolon = false;
26244 switch (token->type)
26246 case CPP_NAME:
26247 case CPP_SEMICOLON:
26248 case CPP_MULT:
26249 case CPP_AND:
26250 case CPP_OPEN_PAREN:
26251 case CPP_CLOSE_PAREN:
26252 case CPP_COMMA:
26253 case CPP_SCOPE:
26254 want_semicolon = false;
26255 break;
26257 /* While it's legal for type qualifiers and storage class
26258 specifiers to follow type definitions in the grammar, only
26259 compiler testsuites contain code like that. Assume that if
26260 we see such code, then what we're really seeing is a case
26261 like:
26263 class X { }
26264 const <type> var = ...;
26268 class Y { }
26269 static <type> func (...) ...
26271 i.e. the qualifier or specifier applies to the next
26272 declaration. To do so, however, we need to look ahead one
26273 more token to see if *that* token is a type specifier.
26275 This code could be improved to handle:
26277 class Z { }
26278 static const <type> var = ...; */
26279 case CPP_KEYWORD:
26280 if (keyword_is_decl_specifier (token->keyword))
26282 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
26284 /* Handling user-defined types here would be nice, but very
26285 tricky. */
26286 want_semicolon
26287 = (lookahead->type == CPP_KEYWORD
26288 && keyword_begins_type_specifier (lookahead->keyword));
26290 break;
26291 default:
26292 break;
26295 /* If we don't have a type, then something is very wrong and we
26296 shouldn't try to do anything clever. Likewise for not seeing the
26297 closing brace. */
26298 if (closing_brace && TYPE_P (type) && want_semicolon)
26300 /* Locate the closing brace. */
26301 cp_token_position prev
26302 = cp_lexer_previous_token_position (parser->lexer);
26303 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
26304 location_t loc = prev_token->location;
26306 /* We want to suggest insertion of a ';' immediately *after* the
26307 closing brace, so, if we can, offset the location by 1 column. */
26308 location_t next_loc = loc;
26309 if (!linemap_location_from_macro_expansion_p (line_table, loc))
26310 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
26312 rich_location richloc (line_table, next_loc);
26314 /* If we successfully offset the location, suggest the fix-it. */
26315 if (next_loc != loc)
26316 richloc.add_fixit_insert_before (next_loc, ";");
26318 if (CLASSTYPE_DECLARED_CLASS (type))
26319 error_at (&richloc,
26320 "expected %<;%> after class definition");
26321 else if (TREE_CODE (type) == RECORD_TYPE)
26322 error_at (&richloc,
26323 "expected %<;%> after struct definition");
26324 else if (TREE_CODE (type) == UNION_TYPE)
26325 error_at (&richloc,
26326 "expected %<;%> after union definition");
26327 else
26328 gcc_unreachable ();
26330 /* Unget one token and smash it to look as though we encountered
26331 a semicolon in the input stream. */
26332 cp_lexer_set_token_position (parser->lexer, prev);
26333 token = cp_lexer_peek_token (parser->lexer);
26334 token->type = CPP_SEMICOLON;
26335 token->keyword = RID_MAX;
26339 /* If this class is not itself within the scope of another class,
26340 then we need to parse the bodies of all of the queued function
26341 definitions. Note that the queued functions defined in a class
26342 are not always processed immediately following the
26343 class-specifier for that class. Consider:
26345 struct A {
26346 struct B { void f() { sizeof (A); } };
26349 If `f' were processed before the processing of `A' were
26350 completed, there would be no way to compute the size of `A'.
26351 Note that the nesting we are interested in here is lexical --
26352 not the semantic nesting given by TYPE_CONTEXT. In particular,
26353 for:
26355 struct A { struct B; };
26356 struct A::B { void f() { } };
26358 there is no need to delay the parsing of `A::B::f'. */
26359 if (--parser->num_classes_being_defined == 0)
26361 tree decl;
26362 tree class_type = NULL_TREE;
26363 tree pushed_scope = NULL_TREE;
26364 unsigned ix;
26365 cp_default_arg_entry *e;
26367 if (!type_definition_ok_p || any_erroneous_template_args_p (type))
26369 /* Skip default arguments, NSDMIs, etc, in order to improve
26370 error recovery (c++/71169, c++/71832). */
26371 vec_safe_truncate (unparsed_funs_with_default_args, 0);
26372 vec_safe_truncate (unparsed_nsdmis, 0);
26373 vec_safe_truncate (unparsed_funs_with_definitions, 0);
26376 /* In a first pass, parse default arguments to the functions.
26377 Then, in a second pass, parse the bodies of the functions.
26378 This two-phased approach handles cases like:
26380 struct S {
26381 void f() { g(); }
26382 void g(int i = 3);
26386 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
26388 decl = e->decl;
26389 /* If there are default arguments that have not yet been processed,
26390 take care of them now. */
26391 if (class_type != e->class_type)
26393 if (pushed_scope)
26394 pop_scope (pushed_scope);
26395 class_type = e->class_type;
26396 pushed_scope = push_scope (class_type);
26398 /* Make sure that any template parameters are in scope. */
26399 maybe_begin_member_template_processing (decl);
26400 /* Parse the default argument expressions. */
26401 cp_parser_late_parsing_default_args (parser, decl);
26402 /* Remove any template parameters from the symbol table. */
26403 maybe_end_member_template_processing ();
26405 vec_safe_truncate (unparsed_funs_with_default_args, 0);
26407 /* If there are noexcept-specifiers that have not yet been processed,
26408 take care of them now. Do this before processing NSDMIs as they
26409 may depend on noexcept-specifiers already having been processed. */
26410 FOR_EACH_VEC_SAFE_ELT (unparsed_noexcepts, ix, decl)
26412 tree ctx = DECL_CONTEXT (decl);
26413 if (class_type != ctx)
26415 if (pushed_scope)
26416 pop_scope (pushed_scope);
26417 class_type = ctx;
26418 pushed_scope = push_scope (class_type);
26421 tree def_parse = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl));
26422 def_parse = TREE_PURPOSE (def_parse);
26424 /* Make sure that any template parameters are in scope. */
26425 maybe_begin_member_template_processing (decl);
26427 /* Make sure that any member-function parameters are in scope.
26428 This function doesn't expect ccp to be set. */
26429 current_class_ptr = current_class_ref = NULL_TREE;
26430 inject_parm_decls (decl);
26432 /* 'this' is not allowed in static member functions. */
26433 unsigned char local_variables_forbidden_p
26434 = parser->local_variables_forbidden_p;
26435 if (DECL_THIS_STATIC (decl))
26436 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
26438 /* Now we can parse the noexcept-specifier. */
26439 tree spec = cp_parser_late_noexcept_specifier (parser, def_parse);
26441 if (spec == error_mark_node)
26442 spec = NULL_TREE;
26444 /* Update the fn's type directly -- it might have escaped
26445 beyond this decl :( */
26446 fixup_deferred_exception_variants (TREE_TYPE (decl), spec);
26447 /* Update any instantiations we've already created. We must
26448 keep the new noexcept-specifier wrapped in a DEFERRED_NOEXCEPT
26449 so that maybe_instantiate_noexcept can tsubst the NOEXCEPT_EXPR
26450 in the pattern. */
26451 for (tree i : DEFPARSE_INSTANTIATIONS (def_parse))
26452 DEFERRED_NOEXCEPT_PATTERN (TREE_PURPOSE (i))
26453 = spec ? TREE_PURPOSE (spec) : error_mark_node;
26455 /* Restore the state of local_variables_forbidden_p. */
26456 parser->local_variables_forbidden_p = local_variables_forbidden_p;
26458 /* The finish_struct call above performed various override checking,
26459 but it skipped unparsed noexcept-specifier operands. Now that we
26460 have resolved them, check again. */
26461 noexcept_override_late_checks (type, decl);
26463 /* Remove any member-function parameters from the symbol table. */
26464 pop_injected_parms ();
26466 /* Remove any template parameters from the symbol table. */
26467 maybe_end_member_template_processing ();
26469 vec_safe_truncate (unparsed_noexcepts, 0);
26471 /* Now parse any NSDMIs. */
26472 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
26474 tree ctx = type_context_for_name_lookup (decl);
26475 if (class_type != ctx)
26477 if (pushed_scope)
26478 pop_scope (pushed_scope);
26479 class_type = ctx;
26480 pushed_scope = push_scope (class_type);
26482 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
26483 cp_parser_late_parsing_nsdmi (parser, decl);
26485 vec_safe_truncate (unparsed_nsdmis, 0);
26487 /* Now contract attributes. */
26488 FOR_EACH_VEC_SAFE_ELT (unparsed_contracts, ix, decl)
26490 tree ctx = DECL_CONTEXT (decl);
26491 if (class_type != ctx)
26493 if (pushed_scope)
26494 pop_scope (pushed_scope);
26495 class_type = ctx;
26496 pushed_scope = push_scope (class_type);
26499 temp_override<tree> cfd(current_function_decl, decl);
26501 /* Make sure that any template parameters are in scope. */
26502 maybe_begin_member_template_processing (decl);
26504 /* Make sure that any member-function parameters are in scope.
26505 This function doesn't expect ccp to be set. */
26506 current_class_ptr = current_class_ref = NULL_TREE;
26507 inject_parm_decls (decl);
26509 /* 'this' is not allowed in static member functions. */
26510 unsigned char local_variables_forbidden_p
26511 = parser->local_variables_forbidden_p;
26512 if (DECL_THIS_STATIC (decl))
26513 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
26515 /* Now we can parse contract conditions. */
26516 for (tree a = DECL_ATTRIBUTES (decl); a; a = TREE_CHAIN (a))
26518 if (cxx_contract_attribute_p (a))
26519 cp_parser_late_contract_condition (parser, decl, a);
26522 /* Restore the state of local_variables_forbidden_p. */
26523 parser->local_variables_forbidden_p = local_variables_forbidden_p;
26525 /* Remove any member-function parameters from the symbol table. */
26526 pop_injected_parms ();
26528 /* Remove any template parameters from the symbol table. */
26529 maybe_end_member_template_processing ();
26531 /* Perform any deferred contract matching. */
26532 match_deferred_contracts (decl);
26534 vec_safe_truncate (unparsed_contracts, 0);
26536 current_class_ptr = NULL_TREE;
26537 current_class_ref = NULL_TREE;
26538 if (pushed_scope)
26539 pop_scope (pushed_scope);
26541 /* Now parse the body of the functions. */
26542 if (flag_openmp)
26544 /* OpenMP UDRs need to be parsed before all other functions. */
26545 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
26546 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
26547 cp_parser_late_parsing_for_member (parser, decl);
26548 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
26549 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
26550 cp_parser_late_parsing_for_member (parser, decl);
26552 else
26553 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
26554 cp_parser_late_parsing_for_member (parser, decl);
26555 vec_safe_truncate (unparsed_funs_with_definitions, 0);
26558 /* Put back any saved access checks. */
26559 pop_deferring_access_checks ();
26561 /* Restore saved state. */
26562 parser->in_switch_statement_p = in_switch_statement_p;
26563 parser->in_statement = in_statement;
26564 parser->in_function_body = saved_in_function_body;
26565 parser->num_template_parameter_lists
26566 = saved_num_template_parameter_lists;
26567 parser->in_unbraced_linkage_specification_p
26568 = saved_in_unbraced_linkage_specification_p;
26569 current_class_ptr = saved_ccp;
26570 current_class_ref = saved_ccr;
26572 return type;
26575 /* Parse a class-head.
26577 class-head:
26578 class-key identifier [opt] base-clause [opt]
26579 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
26580 class-key nested-name-specifier [opt] template-id
26581 base-clause [opt]
26583 class-virt-specifier:
26584 final
26586 GNU Extensions:
26587 class-key attributes identifier [opt] base-clause [opt]
26588 class-key attributes nested-name-specifier identifier base-clause [opt]
26589 class-key attributes nested-name-specifier [opt] template-id
26590 base-clause [opt]
26592 Upon return BASES is initialized to the list of base classes (or
26593 NULL, if there are none) in the same form returned by
26594 cp_parser_base_clause.
26596 Returns the TYPE of the indicated class. Sets
26597 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
26598 involving a nested-name-specifier was used, and FALSE otherwise.
26600 Returns error_mark_node if this is not a class-head.
26602 Returns NULL_TREE if the class-head is syntactically valid, but
26603 semantically invalid in a way that means we should skip the entire
26604 body of the class. */
26606 static tree
26607 cp_parser_class_head (cp_parser* parser,
26608 bool* nested_name_specifier_p)
26610 tree nested_name_specifier;
26611 enum tag_types class_key;
26612 tree id = NULL_TREE;
26613 tree type = NULL_TREE;
26614 tree attributes;
26615 tree bases;
26616 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
26617 bool template_id_p = false;
26618 bool qualified_p = false;
26619 bool invalid_nested_name_p = false;
26620 bool invalid_explicit_specialization_p = false;
26621 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
26622 tree pushed_scope = NULL_TREE;
26623 unsigned num_templates;
26624 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
26625 /* Assume no nested-name-specifier will be present. */
26626 *nested_name_specifier_p = false;
26627 /* Assume no template parameter lists will be used in defining the
26628 type. */
26629 num_templates = 0;
26630 parser->colon_corrects_to_scope_p = false;
26632 /* Look for the class-key. */
26633 class_key = cp_parser_class_key (parser);
26634 if (class_key == none_type)
26635 return error_mark_node;
26637 location_t class_head_start_location = input_location;
26639 /* Parse the attributes. */
26640 attributes = cp_parser_attributes_opt (parser);
26641 if (find_contract (attributes))
26642 diagnose_misapplied_contracts (attributes);
26644 /* If the next token is `::', that is invalid -- but sometimes
26645 people do try to write:
26647 struct ::S {};
26649 Handle this gracefully by accepting the extra qualifier, and then
26650 issuing an error about it later if this really is a
26651 class-head. If it turns out just to be an elaborated type
26652 specifier, remain silent. */
26653 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
26654 qualified_p = true;
26656 /* It is OK to define an inaccessible class; for example:
26658 class A { class B; };
26659 class A::B {};
26661 So we want to ignore access when parsing the class name.
26662 However, we might be tentatively parsing what is really an
26663 elaborated-type-specifier naming a template-id, e.g.
26665 struct C<&D::m> c;
26667 In this case the tentative parse as a class-head will fail, but not
26668 before cp_parser_template_id splices in a CPP_TEMPLATE_ID token.
26669 Since dk_no_check is sticky, we must instead use dk_deferred so that
26670 any such CPP_TEMPLATE_ID token created during this tentative parse
26671 will correctly capture the access checks imposed by the template-id . */
26672 push_deferring_access_checks (dk_deferred);
26674 /* Determine the name of the class. Begin by looking for an
26675 optional nested-name-specifier. */
26676 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
26677 nested_name_specifier
26678 = cp_parser_nested_name_specifier_opt (parser,
26679 /*typename_keyword_p=*/false,
26680 /*check_dependency_p=*/false,
26681 /*type_p=*/true,
26682 /*is_declaration=*/false);
26683 /* If there was a nested-name-specifier, then there *must* be an
26684 identifier. */
26686 cp_token *bad_template_keyword = NULL;
26688 if (nested_name_specifier)
26690 type_start_token = cp_lexer_peek_token (parser->lexer);
26691 /* Although the grammar says `identifier', it really means
26692 `class-name' or `template-name'. You are only allowed to
26693 define a class that has already been declared with this
26694 syntax.
26696 The proposed resolution for Core Issue 180 says that wherever
26697 you see `class T::X' you should treat `X' as a type-name.
26699 We do not know if we will see a class-name, or a
26700 template-name. We look for a class-name first, in case the
26701 class-name is a template-id; if we looked for the
26702 template-name first we would stop after the template-name. */
26703 cp_parser_parse_tentatively (parser);
26704 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26705 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
26706 type = cp_parser_class_name (parser,
26707 /*typename_keyword_p=*/false,
26708 /*template_keyword_p=*/false,
26709 class_type,
26710 /*check_dependency_p=*/false,
26711 /*class_head_p=*/true,
26712 /*is_declaration=*/false);
26713 /* If that didn't work, ignore the nested-name-specifier. */
26714 if (!cp_parser_parse_definitely (parser))
26716 invalid_nested_name_p = true;
26717 type_start_token = cp_lexer_peek_token (parser->lexer);
26718 id = cp_parser_identifier (parser);
26719 if (id == error_mark_node)
26720 id = NULL_TREE;
26722 /* If we could not find a corresponding TYPE, treat this
26723 declaration like an unqualified declaration. */
26724 if (type == error_mark_node)
26725 nested_name_specifier = NULL_TREE;
26726 /* Otherwise, count the number of templates used in TYPE and its
26727 containing scopes. */
26728 else
26729 num_templates = num_template_headers_for_class (TREE_TYPE (type));
26731 /* Otherwise, the identifier is optional. */
26732 else
26734 /* We don't know whether what comes next is a template-id,
26735 an identifier, or nothing at all. */
26736 cp_parser_parse_tentatively (parser);
26737 /* Check for a template-id. */
26738 type_start_token = cp_lexer_peek_token (parser->lexer);
26739 id = cp_parser_template_id (parser,
26740 /*template_keyword_p=*/false,
26741 /*check_dependency_p=*/true,
26742 class_key,
26743 /*is_declaration=*/true);
26744 /* If that didn't work, it could still be an identifier. */
26745 if (!cp_parser_parse_definitely (parser))
26747 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26749 type_start_token = cp_lexer_peek_token (parser->lexer);
26750 id = cp_parser_identifier (parser);
26752 else
26753 id = NULL_TREE;
26755 else
26757 template_id_p = true;
26758 ++num_templates;
26762 pop_deferring_access_checks ();
26764 if (id)
26766 cp_parser_check_for_invalid_template_id (parser, id,
26767 class_key,
26768 type_start_token->location);
26770 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
26772 /* If it's not a `:' or a `{' then we can't really be looking at a
26773 class-head, since a class-head only appears as part of a
26774 class-specifier. We have to detect this situation before calling
26775 xref_tag, since that has irreversible side-effects. */
26776 if (!cp_parser_next_token_starts_class_definition_p (parser))
26778 cp_parser_error (parser, "expected %<{%> or %<:%>");
26779 type = error_mark_node;
26780 goto out;
26783 /* At this point, we're going ahead with the class-specifier, even
26784 if some other problem occurs. */
26785 cp_parser_commit_to_tentative_parse (parser);
26786 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
26788 cp_parser_error (parser,
26789 "cannot specify %<override%> for a class");
26790 type = error_mark_node;
26791 goto out;
26793 /* Issue the error about the overly-qualified name now. */
26794 if (qualified_p)
26796 cp_parser_error (parser,
26797 "global qualification of class name is invalid");
26798 type = error_mark_node;
26799 goto out;
26801 else if (invalid_nested_name_p)
26803 cp_parser_error (parser,
26804 "qualified name does not name a class");
26805 type = error_mark_node;
26806 goto out;
26808 else if (nested_name_specifier)
26810 tree scope;
26812 if (bad_template_keyword)
26813 /* [temp.names]: in a qualified-id formed by a class-head-name, the
26814 keyword template shall not appear at the top level. */
26815 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
26816 "keyword %<template%> not allowed in class-head-name");
26818 /* Reject typedef-names in class heads. */
26819 if (!DECL_IMPLICIT_TYPEDEF_P (type))
26821 error_at (type_start_token->location,
26822 "invalid class name in declaration of %qD",
26823 type);
26824 type = NULL_TREE;
26825 goto done;
26828 /* Figure out in what scope the declaration is being placed. */
26829 scope = current_scope ();
26830 /* If that scope does not contain the scope in which the
26831 class was originally declared, the program is invalid. */
26832 if (scope && !is_ancestor (scope, nested_name_specifier))
26834 if (at_namespace_scope_p ())
26835 error_at (type_start_token->location,
26836 "declaration of %qD in namespace %qD which does not "
26837 "enclose %qD",
26838 type, scope, nested_name_specifier);
26839 else
26840 error_at (type_start_token->location,
26841 "declaration of %qD in %qD which does not enclose %qD",
26842 type, scope, nested_name_specifier);
26843 type = NULL_TREE;
26844 goto done;
26846 /* [dcl.meaning]
26848 A declarator-id shall not be qualified except for the
26849 definition of a ... nested class outside of its class
26850 ... [or] the definition or explicit instantiation of a
26851 class member of a namespace outside of its namespace. */
26852 if (scope == nested_name_specifier)
26853 permerror (nested_name_specifier_token_start->location,
26854 "extra qualification not allowed");
26856 /* An explicit-specialization must be preceded by "template <>". If
26857 it is not, try to recover gracefully. */
26858 if (at_namespace_scope_p ()
26859 && parser->num_template_parameter_lists == 0
26860 && !processing_template_parmlist
26861 && template_id_p)
26863 /* Build a location of this form:
26864 struct typename <ARGS>
26865 ^~~~~~~~~~~~~~~~~~~~~~
26866 with caret==start at the start token, and
26867 finishing at the end of the type. */
26868 location_t reported_loc
26869 = make_location (class_head_start_location,
26870 class_head_start_location,
26871 get_finish (type_start_token->location));
26872 rich_location richloc (line_table, reported_loc);
26873 richloc.add_fixit_insert_before (class_head_start_location,
26874 "template <> ");
26875 error_at (&richloc,
26876 "an explicit specialization must be preceded by"
26877 " %<template <>%>");
26878 invalid_explicit_specialization_p = true;
26879 /* Take the same action that would have been taken by
26880 cp_parser_explicit_specialization. */
26881 ++parser->num_template_parameter_lists;
26882 begin_specialization ();
26884 /* There must be no "return" statements between this point and the
26885 end of this function; set "type "to the correct return value and
26886 use "goto done;" to return. */
26887 /* Make sure that the right number of template parameters were
26888 present. */
26889 if (!cp_parser_check_template_parameters (parser, num_templates,
26890 template_id_p,
26891 type_start_token->location,
26892 /*declarator=*/NULL))
26894 /* If something went wrong, there is no point in even trying to
26895 process the class-definition. */
26896 type = NULL_TREE;
26897 goto done;
26900 /* Look up the type. */
26901 if (template_id_p)
26903 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
26904 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
26905 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
26907 error_at (type_start_token->location,
26908 "function template %qD redeclared as a class template", id);
26909 type = error_mark_node;
26911 else
26913 type = TREE_TYPE (id);
26914 type = maybe_process_partial_specialization (type);
26916 /* Check the scope while we still know whether or not we had a
26917 nested-name-specifier. */
26918 if (type != error_mark_node)
26919 check_unqualified_spec_or_inst (type, type_start_token->location);
26921 if (nested_name_specifier)
26922 pushed_scope = push_scope (nested_name_specifier);
26924 else if (nested_name_specifier)
26926 type = TREE_TYPE (type);
26928 /* Given:
26930 template <typename T> struct S { struct T };
26931 template <typename T> struct S<T>::T { };
26933 we will get a TYPENAME_TYPE when processing the definition of
26934 `S::T'. We need to resolve it to the actual type before we
26935 try to define it. */
26936 if (TREE_CODE (type) == TYPENAME_TYPE)
26938 type = resolve_typename_type (type, /*only_current_p=*/false);
26939 if (TREE_CODE (type) == TYPENAME_TYPE)
26941 cp_parser_error (parser, "could not resolve typename type");
26942 type = error_mark_node;
26946 type = maybe_process_partial_specialization (type);
26947 if (type == error_mark_node)
26949 type = NULL_TREE;
26950 goto done;
26953 /* Enter the scope indicated by the nested-name-specifier. */
26954 pushed_scope = push_scope (nested_name_specifier);
26955 /* Get the canonical version of this type. */
26956 type = TYPE_MAIN_DECL (type);
26957 /* Call push_template_decl if it seems like we should be defining a
26958 template either from the template headers or the type we're
26959 defining, so that we diagnose both extra and missing headers. */
26960 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
26961 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
26962 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
26964 type = push_template_decl (type);
26965 if (type == error_mark_node)
26967 type = NULL_TREE;
26968 goto done;
26972 type = TREE_TYPE (type);
26973 *nested_name_specifier_p = true;
26975 else /* The name is not a nested name. */
26977 /* If the class was unnamed, create a dummy name. */
26978 if (!id)
26979 id = make_anon_name ();
26980 TAG_how how = (parser->in_type_id_in_expr_p
26981 ? TAG_how::INNERMOST_NON_CLASS
26982 : TAG_how::CURRENT_ONLY);
26983 type = xref_tag (class_key, id, how,
26984 parser->num_template_parameter_lists);
26987 /* Diagnose class/struct/union mismatches. */
26988 cp_parser_check_class_key (parser, UNKNOWN_LOCATION, class_key, type,
26989 true, true);
26991 /* Indicate whether this class was declared as a `class' or as a
26992 `struct'. */
26993 if (TREE_CODE (type) == RECORD_TYPE)
26994 CLASSTYPE_DECLARED_CLASS (type) = class_key == class_type;
26996 /* If this type was already complete, and we see another definition,
26997 that's an error. Likewise if the type is already being defined:
26998 this can happen, eg, when it's defined from within an expression
26999 (c++/84605). */
27000 if (type != error_mark_node
27001 && (COMPLETE_TYPE_P (type) || TYPE_BEING_DEFINED (type)))
27003 error_at (type_start_token->location, "redefinition of %q#T",
27004 type);
27005 inform (location_of (type), "previous definition of %q#T",
27006 type);
27007 type = NULL_TREE;
27008 goto done;
27010 else if (type == error_mark_node)
27011 type = NULL_TREE;
27013 if (type)
27015 if (current_lambda_expr ()
27016 && uses_parameter_packs (attributes))
27018 /* In a lambda this should work, but doesn't currently. */
27019 sorry ("unexpanded parameter pack in local class in lambda");
27020 attributes = NULL_TREE;
27023 /* Apply attributes now, before any use of the class as a template
27024 argument in its base list. */
27025 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
27026 fixup_attribute_variants (type);
27029 /* Associate constraints with the type. */
27030 if (flag_concepts)
27031 type = associate_classtype_constraints (type);
27033 /* We will have entered the scope containing the class; the names of
27034 base classes should be looked up in that context. For example:
27036 struct A { struct B {}; struct C; };
27037 struct A::C : B {};
27039 is valid. */
27041 /* Get the list of base-classes, if there is one. Defer access checking
27042 until the entire list has been seen, as per [class.access.general]. */
27043 push_deferring_access_checks (dk_deferred);
27044 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27046 if (type)
27047 pushclass (type);
27048 bases = cp_parser_base_clause (parser);
27049 if (type)
27050 popclass ();
27052 else
27053 bases = NULL_TREE;
27055 /* If we're really defining a class, process the base classes.
27056 If they're invalid, fail. */
27057 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27058 xref_basetypes (type, bases);
27060 /* Now that all bases have been seen and attached to the class, check
27061 accessibility of the types named in the base-clause. This must be
27062 done relative to the class scope, so that we accept e.g.
27064 struct A { protected: struct B {}; };
27065 struct C : A::B, A {}; // OK: A::B is accessible via base A
27067 as per [class.access.general]. */
27068 if (type)
27069 pushclass (type);
27070 pop_to_parent_deferring_access_checks ();
27071 if (type)
27072 popclass ();
27074 done:
27075 /* Leave the scope given by the nested-name-specifier. We will
27076 enter the class scope itself while processing the members. */
27077 if (pushed_scope)
27078 pop_scope (pushed_scope);
27080 if (invalid_explicit_specialization_p)
27082 end_specialization ();
27083 --parser->num_template_parameter_lists;
27086 if (type)
27087 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
27088 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
27089 CLASSTYPE_FINAL (type) = 1;
27090 out:
27091 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27092 return type;
27095 /* Parse a class-key.
27097 class-key:
27098 class
27099 struct
27100 union
27102 Returns the kind of class-key specified, or none_type to indicate
27103 error. */
27105 static enum tag_types
27106 cp_parser_class_key (cp_parser* parser)
27108 cp_token *token;
27109 enum tag_types tag_type;
27111 /* Look for the class-key. */
27112 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
27113 if (!token)
27114 return none_type;
27116 /* Check to see if the TOKEN is a class-key. */
27117 tag_type = cp_parser_token_is_class_key (token);
27118 if (!tag_type)
27119 cp_parser_error (parser, "expected class-key");
27120 return tag_type;
27123 /* Parse a type-parameter-key.
27125 type-parameter-key:
27126 class
27127 typename
27130 static void
27131 cp_parser_type_parameter_key (cp_parser* parser)
27133 /* Look for the type-parameter-key. */
27134 enum tag_types tag_type = none_type;
27135 cp_token *token = cp_lexer_peek_token (parser->lexer);
27136 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
27138 cp_lexer_consume_token (parser->lexer);
27139 if (pedantic && tag_type == typename_type
27140 && cxx_dialect < cxx17)
27141 /* typename is not allowed in a template template parameter
27142 by the standard until C++17. */
27143 pedwarn (token->location, OPT_Wc__17_extensions,
27144 "ISO C++ forbids typename key in template template parameter;"
27145 " use %<-std=c++17%> or %<-std=gnu++17%>");
27147 else
27148 cp_parser_error (parser, "expected %<class%> or %<typename%>");
27150 return;
27153 /* Parse an (optional) member-specification.
27155 member-specification:
27156 member-declaration member-specification [opt]
27157 access-specifier : member-specification [opt] */
27159 static void
27160 cp_parser_member_specification_opt (cp_parser* parser)
27162 while (true)
27164 cp_token *token;
27165 enum rid keyword;
27167 /* Peek at the next token. */
27168 token = cp_lexer_peek_token (parser->lexer);
27169 /* If it's a `}', or EOF then we've seen all the members. */
27170 if (token->type == CPP_CLOSE_BRACE
27171 || token->type == CPP_EOF
27172 || token->type == CPP_PRAGMA_EOL)
27173 break;
27175 /* See if this token is a keyword. */
27176 keyword = token->keyword;
27177 switch (keyword)
27179 case RID_PUBLIC:
27180 case RID_PROTECTED:
27181 case RID_PRIVATE:
27182 /* Consume the access-specifier. */
27183 cp_lexer_consume_token (parser->lexer);
27184 /* Remember which access-specifier is active. */
27185 current_access_specifier = token->u.value;
27186 /* Look for the `:'. */
27187 cp_parser_require (parser, CPP_COLON, RT_COLON);
27188 break;
27190 default:
27191 /* Accept #pragmas at class scope. */
27192 if (token->type == CPP_PRAGMA)
27194 cp_parser_pragma (parser, pragma_member, NULL);
27195 break;
27198 /* Otherwise, the next construction must be a
27199 member-declaration. */
27200 cp_parser_member_declaration (parser);
27205 /* Parse a member-declaration.
27207 member-declaration:
27208 decl-specifier-seq [opt] member-declarator-list [opt] ;
27209 function-definition ; [opt]
27210 :: [opt] nested-name-specifier template [opt] unqualified-id ;
27211 using-declaration
27212 template-declaration
27213 alias-declaration
27215 member-declarator-list:
27216 member-declarator
27217 member-declarator-list , member-declarator
27219 member-declarator:
27220 declarator pure-specifier [opt]
27221 declarator constant-initializer [opt]
27222 identifier [opt] : constant-expression
27224 GNU Extensions:
27226 member-declaration:
27227 __extension__ member-declaration
27229 member-declarator:
27230 declarator attributes [opt] pure-specifier [opt]
27231 declarator attributes [opt] constant-initializer [opt]
27232 identifier [opt] attributes [opt] : constant-expression
27234 C++0x Extensions:
27236 member-declaration:
27237 static_assert-declaration */
27239 static void
27240 cp_parser_member_declaration (cp_parser* parser)
27242 cp_decl_specifier_seq decl_specifiers;
27243 tree prefix_attributes;
27244 tree decl;
27245 int declares_class_or_enum;
27246 bool friend_p;
27247 cp_token *token = NULL;
27248 cp_token *decl_spec_token_start = NULL;
27249 cp_token *initializer_token_start = NULL;
27250 int saved_pedantic;
27251 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27253 /* Check for the `__extension__' keyword. */
27254 if (cp_parser_extension_opt (parser, &saved_pedantic))
27256 /* Recurse. */
27257 cp_parser_member_declaration (parser);
27258 /* Restore the old value of the PEDANTIC flag. */
27259 pedantic = saved_pedantic;
27261 return;
27264 /* Check for a template-declaration. */
27265 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27267 /* An explicit specialization here is an error condition, and we
27268 expect the specialization handler to detect and report this. */
27269 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
27270 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
27271 cp_parser_explicit_specialization (parser);
27272 else
27273 cp_parser_template_declaration (parser, /*member_p=*/true);
27275 return;
27277 /* Check for a template introduction. */
27278 else if (cp_parser_template_declaration_after_export (parser, true))
27279 return;
27281 /* Check for a using-declaration. */
27282 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
27284 if (cxx_dialect < cxx11)
27285 /* Parse the using-declaration. */
27286 cp_parser_using_declaration (parser, /*access_declaration_p=*/false);
27287 else if (cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_ENUM))
27288 cp_parser_using_enum (parser);
27289 else
27291 tree decl;
27292 bool alias_decl_expected;
27293 cp_parser_parse_tentatively (parser);
27294 decl = cp_parser_alias_declaration (parser);
27295 /* Note that if we actually see the '=' token after the
27296 identifier, cp_parser_alias_declaration commits the
27297 tentative parse. In that case, we really expect an
27298 alias-declaration. Otherwise, we expect a using
27299 declaration. */
27300 alias_decl_expected =
27301 !cp_parser_uncommitted_to_tentative_parse_p (parser);
27302 cp_parser_parse_definitely (parser);
27304 if (alias_decl_expected)
27305 finish_member_declaration (decl);
27306 else
27307 cp_parser_using_declaration (parser,
27308 /*access_declaration_p=*/false);
27310 return;
27313 /* Check for @defs. */
27314 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
27316 tree ivar, member;
27317 tree ivar_chains = cp_parser_objc_defs_expression (parser);
27318 ivar = ivar_chains;
27319 while (ivar)
27321 member = ivar;
27322 ivar = TREE_CHAIN (member);
27323 TREE_CHAIN (member) = NULL_TREE;
27324 finish_member_declaration (member);
27326 return;
27329 /* If the next token is `static_assert' we have a static assertion. */
27330 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
27332 cp_parser_static_assert (parser, /*member_p=*/true);
27333 return;
27336 parser->colon_corrects_to_scope_p = false;
27338 cp_omp_declare_simd_data odsd;
27339 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
27340 goto out;
27342 /* Parse the decl-specifier-seq. */
27343 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
27344 cp_parser_decl_specifier_seq (parser,
27345 (CP_PARSER_FLAGS_OPTIONAL
27346 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
27347 &decl_specifiers,
27348 &declares_class_or_enum);
27350 if (decl_specifiers.attributes && (flag_openmp || flag_openmp_simd))
27351 cp_parser_handle_directive_omp_attributes (parser,
27352 &decl_specifiers.attributes,
27353 &odsd, true);
27355 /* Check for an invalid type-name. */
27356 if (!decl_specifiers.any_type_specifiers_p
27357 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
27358 goto out;
27359 /* If there is no declarator, then the decl-specifier-seq should
27360 specify a type. */
27361 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27363 /* If there was no decl-specifier-seq, and the next token is a
27364 `;', then we have something like:
27366 struct S { ; };
27368 [class.mem]
27370 Each member-declaration shall declare at least one member
27371 name of the class. */
27372 if (!decl_specifiers.any_specifiers_p)
27374 cp_token *token = cp_lexer_peek_token (parser->lexer);
27375 if (!in_system_header_at (token->location))
27377 gcc_rich_location richloc (token->location);
27378 richloc.add_fixit_remove ();
27379 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
27382 else
27384 /* See if this declaration is a friend. */
27385 friend_p = cp_parser_friend_p (&decl_specifiers);
27386 /* If there were decl-specifiers, check to see if there was
27387 a class-declaration. */
27388 tree type = check_tag_decl (&decl_specifiers,
27389 /*explicit_type_instantiation_p=*/false);
27390 /* Nested classes have already been added to the class, but
27391 a `friend' needs to be explicitly registered. */
27392 if (friend_p)
27394 /* If the `friend' keyword was present, the friend must
27395 be introduced with a class-key. */
27396 if (!declares_class_or_enum && cxx_dialect < cxx11)
27397 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
27398 "in C++03 a class-key must be used "
27399 "when declaring a friend");
27400 /* In this case:
27402 template <typename T> struct A {
27403 friend struct A<T>::B;
27406 A<T>::B will be represented by a TYPENAME_TYPE, and
27407 therefore not recognized by check_tag_decl. */
27408 if (!type)
27410 type = decl_specifiers.type;
27411 if (type && TREE_CODE (type) == TYPE_DECL)
27412 type = TREE_TYPE (type);
27414 /* Warn if an attribute cannot appear here, as per
27415 [dcl.attr.grammar]/5. But not when declares_class_or_enum:
27416 we ignore attributes in elaborated-type-specifiers. */
27417 if (!declares_class_or_enum
27418 && cxx11_attribute_p (decl_specifiers.attributes))
27420 decl_specifiers.attributes = NULL_TREE;
27421 if (warning_at (decl_spec_token_start->location,
27422 OPT_Wattributes, "attribute ignored"))
27423 inform (decl_spec_token_start->location, "an attribute "
27424 "that appertains to a friend declaration that "
27425 "is not a definition is ignored");
27427 if (!type || !TYPE_P (type))
27428 error_at (decl_spec_token_start->location,
27429 "friend declaration does not name a class or "
27430 "function");
27431 else
27432 make_friend_class (current_class_type, type,
27433 /*complain=*/true);
27435 /* If there is no TYPE, an error message will already have
27436 been issued. */
27437 else if (!type || type == error_mark_node)
27439 /* An anonymous aggregate has to be handled specially; such
27440 a declaration really declares a data member (with a
27441 particular type), as opposed to a nested class. */
27442 else if (ANON_AGGR_TYPE_P (type))
27444 /* C++11 9.5/6. */
27445 if (decl_specifiers.storage_class != sc_none)
27446 error_at (decl_spec_token_start->location,
27447 "a storage class on an anonymous aggregate "
27448 "in class scope is not allowed");
27450 /* Remove constructors and such from TYPE, now that we
27451 know it is an anonymous aggregate. */
27452 fixup_anonymous_aggr (type);
27453 /* And make the corresponding data member. */
27454 decl = build_decl (decl_spec_token_start->location,
27455 FIELD_DECL, NULL_TREE, type);
27456 /* Add it to the class. */
27457 finish_member_declaration (decl);
27459 else
27460 cp_parser_check_access_in_redeclaration
27461 (TYPE_NAME (type),
27462 decl_spec_token_start->location);
27465 else
27467 bool assume_semicolon = false;
27469 /* Clear attributes from the decl_specifiers but keep them
27470 around as prefix attributes that apply them to the entity
27471 being declared. */
27472 prefix_attributes = decl_specifiers.attributes;
27473 decl_specifiers.attributes = NULL_TREE;
27474 if (parser->omp_declare_simd
27475 && (parser->omp_declare_simd->attribs[0]
27476 == &decl_specifiers.attributes))
27477 parser->omp_declare_simd->attribs[0] = &prefix_attributes;
27479 /* See if these declarations will be friends. */
27480 friend_p = cp_parser_friend_p (&decl_specifiers);
27482 /* Keep going until we hit the `;' at the end of the
27483 declaration. */
27484 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27486 tree attributes = NULL_TREE;
27487 tree first_attribute;
27488 tree initializer;
27489 bool named_bitfld = false;
27491 /* Peek at the next token. */
27492 token = cp_lexer_peek_token (parser->lexer);
27494 /* The following code wants to know early if it is a bit-field
27495 or some other declaration. Attributes can appear before
27496 the `:' token. Skip over them without consuming any tokens
27497 to peek if they are followed by `:'. */
27498 if (cp_next_tokens_can_be_attribute_p (parser)
27499 || (token->type == CPP_NAME
27500 && cp_nth_tokens_can_be_attribute_p (parser, 2)
27501 && (named_bitfld = true)))
27503 size_t n
27504 = cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
27505 token = cp_lexer_peek_nth_token (parser->lexer, n);
27508 /* Check for a bitfield declaration. */
27509 if (token->type == CPP_COLON
27510 || (token->type == CPP_NAME
27511 && token == cp_lexer_peek_token (parser->lexer)
27512 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
27513 && (named_bitfld = true)))
27515 tree identifier;
27516 tree width;
27517 tree late_attributes = NULL_TREE;
27518 location_t id_location
27519 = cp_lexer_peek_token (parser->lexer)->location;
27521 if (named_bitfld)
27522 identifier = cp_parser_identifier (parser);
27523 else
27524 identifier = NULL_TREE;
27526 /* Look for attributes that apply to the bitfield. */
27527 attributes = cp_parser_attributes_opt (parser);
27529 /* Consume the `:' token. */
27530 cp_lexer_consume_token (parser->lexer);
27532 /* Get the width of the bitfield. */
27533 width = cp_parser_constant_expression (parser, false, NULL,
27534 cxx_dialect >= cxx11);
27536 /* In C++20 and as extension for C++11 and above we allow
27537 default member initializers for bit-fields. */
27538 initializer = NULL_TREE;
27539 if (cxx_dialect >= cxx11
27540 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
27541 || cp_lexer_next_token_is (parser->lexer,
27542 CPP_OPEN_BRACE)))
27544 location_t loc
27545 = cp_lexer_peek_token (parser->lexer)->location;
27546 if (cxx_dialect < cxx20
27547 && identifier != NULL_TREE)
27548 pedwarn (loc, OPT_Wc__20_extensions,
27549 "default member initializers for bit-fields "
27550 "only available with %<-std=c++20%> or "
27551 "%<-std=gnu++20%>");
27553 initializer = cp_parser_save_nsdmi (parser);
27554 if (identifier == NULL_TREE)
27556 error_at (loc, "default member initializer for "
27557 "unnamed bit-field");
27558 initializer = NULL_TREE;
27561 else
27563 /* Look for attributes that apply to the bitfield after
27564 the `:' token and width. This is where GCC used to
27565 parse attributes in the past, pedwarn if there is
27566 a std attribute. */
27567 if (cp_next_tokens_can_be_std_attribute_p (parser))
27568 pedwarn (input_location, OPT_Wpedantic,
27569 "ISO C++ allows bit-field attributes only "
27570 "before the %<:%> token");
27572 late_attributes = cp_parser_attributes_opt (parser);
27575 attributes = attr_chainon (attributes, late_attributes);
27577 /* Remember which attributes are prefix attributes and
27578 which are not. */
27579 first_attribute = attributes;
27580 /* Combine the attributes. */
27581 attributes = attr_chainon (prefix_attributes, attributes);
27583 /* Create the bitfield declaration. */
27584 decl = grokbitfield (identifier
27585 ? make_id_declarator (NULL_TREE,
27586 identifier,
27587 sfk_none,
27588 id_location)
27589 : NULL,
27590 &decl_specifiers,
27591 width, initializer,
27592 attributes);
27594 else
27596 cp_declarator *declarator;
27597 tree asm_specification;
27598 int ctor_dtor_or_conv_p;
27599 bool static_p = (decl_specifiers.storage_class == sc_static);
27600 cp_parser_flags flags = CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
27601 /* We can't delay parsing for friends,
27602 alias-declarations, and typedefs, even though the
27603 standard seems to require it. */
27604 if (!friend_p
27605 && !decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
27606 flags |= CP_PARSER_FLAGS_DELAY_NOEXCEPT;
27608 /* Parse the declarator. */
27609 declarator
27610 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
27611 flags,
27612 &ctor_dtor_or_conv_p,
27613 /*parenthesized_p=*/NULL,
27614 /*member_p=*/true,
27615 friend_p, static_p);
27617 /* If something went wrong parsing the declarator, make sure
27618 that we at least consume some tokens. */
27619 if (declarator == cp_error_declarator)
27621 /* Skip to the end of the statement. */
27622 cp_parser_skip_to_end_of_statement (parser);
27623 /* If the next token is not a semicolon, that is
27624 probably because we just skipped over the body of
27625 a function. So, we consume a semicolon if
27626 present, but do not issue an error message if it
27627 is not present. */
27628 if (cp_lexer_next_token_is (parser->lexer,
27629 CPP_SEMICOLON))
27630 cp_lexer_consume_token (parser->lexer);
27631 goto out;
27634 /* Handle class-scope non-template C++17 deduction guides. */
27635 cp_parser_maybe_adjust_declarator_for_dguide (parser,
27636 &decl_specifiers,
27637 declarator,
27638 &ctor_dtor_or_conv_p);
27640 if (declares_class_or_enum & 2)
27641 cp_parser_check_for_definition_in_return_type
27642 (declarator, decl_specifiers.type,
27643 decl_specifiers.locations[ds_type_spec]);
27645 /* Look for an asm-specification. */
27646 asm_specification = cp_parser_asm_specification_opt (parser);
27647 /* Look for attributes that apply to the declaration. */
27648 attributes = cp_parser_attributes_opt (parser);
27649 /* Remember which attributes are prefix attributes and
27650 which are not. */
27651 first_attribute = attributes;
27652 /* Combine the attributes. */
27653 attributes = attr_chainon (prefix_attributes, attributes);
27655 /* If it's an `=', then we have a constant-initializer or a
27656 pure-specifier. It is not correct to parse the
27657 initializer before registering the member declaration
27658 since the member declaration should be in scope while
27659 its initializer is processed. However, the rest of the
27660 front end does not yet provide an interface that allows
27661 us to handle this correctly. */
27662 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
27664 /* In [class.mem]:
27666 A pure-specifier shall be used only in the declaration of
27667 a virtual function.
27669 A member-declarator can contain a constant-initializer
27670 only if it declares a static member of integral or
27671 enumeration type.
27673 Therefore, if the DECLARATOR is for a function, we look
27674 for a pure-specifier; otherwise, we look for a
27675 constant-initializer. When we call `grokfield', it will
27676 perform more stringent semantics checks. */
27677 initializer_token_start = cp_lexer_peek_token (parser->lexer);
27678 declarator->init_loc = initializer_token_start->location;
27679 if (function_declarator_p (declarator)
27680 || (decl_specifiers.type
27681 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
27682 && declarator->kind == cdk_id
27683 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
27684 == FUNCTION_TYPE)))
27685 initializer = cp_parser_pure_specifier (parser);
27686 else if (decl_specifiers.storage_class != sc_static)
27687 initializer = cp_parser_save_nsdmi (parser);
27688 else if (cxx_dialect >= cxx11)
27690 bool nonconst;
27691 /* Don't require a constant rvalue in C++11, since we
27692 might want a reference constant. We'll enforce
27693 constancy later. */
27694 cp_lexer_consume_token (parser->lexer);
27695 /* Parse the initializer. */
27696 initializer = cp_parser_initializer_clause (parser,
27697 &nonconst);
27699 else
27700 /* Parse the initializer. */
27701 initializer = cp_parser_constant_initializer (parser);
27703 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
27704 && !function_declarator_p (declarator))
27706 bool x;
27707 declarator->init_loc
27708 = cp_lexer_peek_token (parser->lexer)->location;
27709 if (decl_specifiers.storage_class != sc_static)
27710 initializer = cp_parser_save_nsdmi (parser);
27711 else
27712 initializer = cp_parser_initializer (parser, &x, &x);
27714 /* Detect invalid bit-field cases such as
27716 int *p : 4;
27717 int &&r : 3;
27719 and similar. */
27720 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
27721 /* If there were no type specifiers, it was a
27722 constructor. */
27723 && decl_specifiers.any_type_specifiers_p)
27725 /* This is called for a decent diagnostic only. */
27726 tree d = grokdeclarator (declarator, &decl_specifiers,
27727 BITFIELD, /*initialized=*/false,
27728 &attributes);
27729 if (!error_operand_p (d))
27730 error_at (DECL_SOURCE_LOCATION (d),
27731 "bit-field %qD has non-integral type %qT",
27732 d, TREE_TYPE (d));
27733 cp_parser_skip_to_end_of_statement (parser);
27734 /* Avoid "extra ;" pedwarns. */
27735 if (cp_lexer_next_token_is (parser->lexer,
27736 CPP_SEMICOLON))
27737 cp_lexer_consume_token (parser->lexer);
27738 goto out;
27740 /* Otherwise, there is no initializer. */
27741 else
27742 initializer = NULL_TREE;
27744 /* See if we are probably looking at a function
27745 definition. We are certainly not looking at a
27746 member-declarator. Calling `grokfield' has
27747 side-effects, so we must not do it unless we are sure
27748 that we are looking at a member-declarator. */
27749 if (cp_parser_token_starts_function_definition_p
27750 (cp_lexer_peek_token (parser->lexer)))
27752 /* The grammar does not allow a pure-specifier to be
27753 used when a member function is defined. (It is
27754 possible that this fact is an oversight in the
27755 standard, since a pure function may be defined
27756 outside of the class-specifier. */
27757 if (initializer && initializer_token_start)
27758 error_at (initializer_token_start->location,
27759 "pure-specifier on function-definition");
27760 decl = cp_parser_save_member_function_body (parser,
27761 &decl_specifiers,
27762 declarator,
27763 attributes);
27765 if (parser->fully_implicit_function_template_p)
27766 decl = finish_fully_implicit_template (parser, decl);
27767 /* If the member was not a friend, declare it here. */
27768 if (!friend_p)
27769 finish_member_declaration (decl);
27770 /* Peek at the next token. */
27771 token = cp_lexer_peek_token (parser->lexer);
27772 /* If the next token is a semicolon, consume it. */
27773 if (token->type == CPP_SEMICOLON)
27775 location_t semicolon_loc
27776 = cp_lexer_consume_token (parser->lexer)->location;
27777 gcc_rich_location richloc (semicolon_loc);
27778 richloc.add_fixit_remove ();
27779 warning_at (&richloc, OPT_Wextra_semi,
27780 "extra %<;%> after in-class "
27781 "function definition");
27783 goto out;
27785 else
27786 if (declarator->kind == cdk_function)
27787 declarator->id_loc = token->location;
27789 /* Create the declaration. */
27790 decl = grokfield (declarator, &decl_specifiers,
27791 initializer, /*init_const_expr_p=*/true,
27792 asm_specification, attributes);
27794 if (parser->fully_implicit_function_template_p)
27796 if (friend_p)
27797 finish_fully_implicit_template (parser, 0);
27798 else
27799 decl = finish_fully_implicit_template (parser, decl);
27803 cp_finalize_omp_declare_simd (parser, decl);
27804 cp_finalize_oacc_routine (parser, decl, false);
27806 /* Reset PREFIX_ATTRIBUTES. */
27807 if (attributes != error_mark_node)
27809 while (attributes && TREE_CHAIN (attributes) != first_attribute)
27810 attributes = TREE_CHAIN (attributes);
27811 if (attributes)
27812 TREE_CHAIN (attributes) = NULL_TREE;
27815 /* If there is any qualification still in effect, clear it
27816 now; we will be starting fresh with the next declarator. */
27817 parser->scope = NULL_TREE;
27818 parser->qualifying_scope = NULL_TREE;
27819 parser->object_scope = NULL_TREE;
27820 /* If it's a `,', then there are more declarators. */
27821 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27823 cp_lexer_consume_token (parser->lexer);
27824 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27826 cp_token *token = cp_lexer_previous_token (parser->lexer);
27827 gcc_rich_location richloc (token->location);
27828 richloc.add_fixit_remove ();
27829 error_at (&richloc, "stray %<,%> at end of "
27830 "member declaration");
27833 /* If the next token isn't a `;', then we have a parse error. */
27834 else if (cp_lexer_next_token_is_not (parser->lexer,
27835 CPP_SEMICOLON))
27837 /* The next token might be a ways away from where the
27838 actual semicolon is missing. Find the previous token
27839 and use that for our error position. */
27840 cp_token *token = cp_lexer_previous_token (parser->lexer);
27841 gcc_rich_location richloc (token->location);
27842 richloc.add_fixit_insert_after (";");
27843 error_at (&richloc, "expected %<;%> at end of "
27844 "member declaration");
27846 /* Assume that the user meant to provide a semicolon. If
27847 we were to cp_parser_skip_to_end_of_statement, we might
27848 skip to a semicolon inside a member function definition
27849 and issue nonsensical error messages. */
27850 assume_semicolon = true;
27853 if (decl)
27855 /* Add DECL to the list of members. */
27856 if (!friend_p
27857 /* Explicitly include, eg, NSDMIs, for better error
27858 recovery (c++/58650). */
27859 || !DECL_DECLARES_FUNCTION_P (decl))
27860 finish_member_declaration (decl);
27862 if (DECL_DECLARES_FUNCTION_P (decl))
27863 cp_parser_save_default_args (parser, STRIP_TEMPLATE (decl));
27864 else if (TREE_CODE (decl) == FIELD_DECL
27865 && DECL_INITIAL (decl))
27866 /* Add DECL to the queue of NSDMI to be parsed later. */
27867 vec_safe_push (unparsed_nsdmis, decl);
27870 if (assume_semicolon)
27871 goto out;
27875 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
27876 out:
27877 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27878 cp_finalize_omp_declare_simd (parser, &odsd);
27881 /* Parse a pure-specifier.
27883 pure-specifier:
27886 Returns INTEGER_ZERO_NODE if a pure specifier is found.
27887 Otherwise, ERROR_MARK_NODE is returned. */
27889 static tree
27890 cp_parser_pure_specifier (cp_parser* parser)
27892 cp_token *token;
27894 /* Look for the `=' token. */
27895 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
27896 return error_mark_node;
27897 /* Look for the `0' token. */
27898 token = cp_lexer_peek_token (parser->lexer);
27900 if (token->type == CPP_EOF
27901 || token->type == CPP_PRAGMA_EOL)
27902 return error_mark_node;
27904 cp_lexer_consume_token (parser->lexer);
27906 /* Accept = default or = delete in c++0x mode. */
27907 if (token->keyword == RID_DEFAULT
27908 || token->keyword == RID_DELETE)
27910 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
27911 return token->u.value;
27914 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
27915 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
27917 cp_parser_error (parser,
27918 "invalid pure specifier (only %<= 0%> is allowed)");
27919 cp_parser_skip_to_end_of_statement (parser);
27920 return error_mark_node;
27922 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
27924 error_at (token->location, "templates may not be %<virtual%>");
27925 return error_mark_node;
27928 return integer_zero_node;
27931 /* Parse a constant-initializer.
27933 constant-initializer:
27934 = constant-expression
27936 Returns a representation of the constant-expression. */
27938 static tree
27939 cp_parser_constant_initializer (cp_parser* parser)
27941 /* Look for the `=' token. */
27942 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
27943 return error_mark_node;
27945 /* It is invalid to write:
27947 struct S { static const int i = { 7 }; };
27950 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27952 cp_parser_error (parser,
27953 "a brace-enclosed initializer is not allowed here");
27954 /* Consume the opening brace. */
27955 matching_braces braces;
27956 braces.consume_open (parser);
27957 /* Skip the initializer. */
27958 cp_parser_skip_to_closing_brace (parser);
27959 /* Look for the trailing `}'. */
27960 braces.require_close (parser);
27962 return error_mark_node;
27965 return cp_parser_constant_expression (parser);
27968 /* Derived classes [gram.class.derived] */
27970 /* Parse a base-clause.
27972 base-clause:
27973 : base-specifier-list
27975 base-specifier-list:
27976 base-specifier ... [opt]
27977 base-specifier-list , base-specifier ... [opt]
27979 Returns a TREE_LIST representing the base-classes, in the order in
27980 which they were declared. The representation of each node is as
27981 described by cp_parser_base_specifier.
27983 In the case that no bases are specified, this function will return
27984 NULL_TREE, not ERROR_MARK_NODE. */
27986 static tree
27987 cp_parser_base_clause (cp_parser* parser)
27989 tree bases = NULL_TREE;
27991 /* Look for the `:' that begins the list. */
27992 cp_parser_require (parser, CPP_COLON, RT_COLON);
27994 /* Scan the base-specifier-list. */
27995 while (true)
27997 cp_token *token;
27998 tree base;
27999 bool pack_expansion_p = false;
28001 /* Look for the base-specifier. */
28002 base = cp_parser_base_specifier (parser);
28003 /* Look for the (optional) ellipsis. */
28004 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28006 /* Consume the `...'. */
28007 cp_lexer_consume_token (parser->lexer);
28009 pack_expansion_p = true;
28012 /* Add BASE to the front of the list. */
28013 if (base && base != error_mark_node)
28015 if (pack_expansion_p)
28016 /* Make this a pack expansion type. */
28017 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
28019 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
28021 TREE_CHAIN (base) = bases;
28022 bases = base;
28025 /* Peek at the next token. */
28026 token = cp_lexer_peek_token (parser->lexer);
28027 /* If it's not a comma, then the list is complete. */
28028 if (token->type != CPP_COMMA)
28029 break;
28030 /* Consume the `,'. */
28031 cp_lexer_consume_token (parser->lexer);
28034 /* PARSER->SCOPE may still be non-NULL at this point, if the last
28035 base class had a qualified name. However, the next name that
28036 appears is certainly not qualified. */
28037 parser->scope = NULL_TREE;
28038 parser->qualifying_scope = NULL_TREE;
28039 parser->object_scope = NULL_TREE;
28041 return nreverse (bases);
28044 /* Parse a base-specifier.
28046 base-specifier:
28047 :: [opt] nested-name-specifier [opt] class-name
28048 virtual access-specifier [opt] :: [opt] nested-name-specifier
28049 [opt] class-name
28050 access-specifier virtual [opt] :: [opt] nested-name-specifier
28051 [opt] class-name
28053 Returns a TREE_LIST. The TREE_PURPOSE will be one of
28054 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
28055 indicate the specifiers provided. The TREE_VALUE will be a TYPE
28056 (or the ERROR_MARK_NODE) indicating the type that was specified. */
28058 static tree
28059 cp_parser_base_specifier (cp_parser* parser)
28061 cp_token *token;
28062 bool done = false;
28063 bool virtual_p = false;
28064 bool duplicate_virtual_error_issued_p = false;
28065 bool duplicate_access_error_issued_p = false;
28066 bool class_scope_p, template_p;
28067 tree access = access_default_node;
28068 tree type;
28070 /* Process the optional `virtual' and `access-specifier'. */
28071 while (!done)
28073 /* Peek at the next token. */
28074 token = cp_lexer_peek_token (parser->lexer);
28075 /* Process `virtual'. */
28076 switch (token->keyword)
28078 case RID_VIRTUAL:
28079 /* If `virtual' appears more than once, issue an error. */
28080 if (virtual_p && !duplicate_virtual_error_issued_p)
28082 cp_parser_error (parser,
28083 "%<virtual%> specified more than once in base-specifier");
28084 duplicate_virtual_error_issued_p = true;
28087 virtual_p = true;
28089 /* Consume the `virtual' token. */
28090 cp_lexer_consume_token (parser->lexer);
28092 break;
28094 case RID_PUBLIC:
28095 case RID_PROTECTED:
28096 case RID_PRIVATE:
28097 /* If more than one access specifier appears, issue an
28098 error. */
28099 if (access != access_default_node
28100 && !duplicate_access_error_issued_p)
28102 cp_parser_error (parser,
28103 "more than one access specifier in base-specifier");
28104 duplicate_access_error_issued_p = true;
28107 access = ridpointers[(int) token->keyword];
28109 /* Consume the access-specifier. */
28110 cp_lexer_consume_token (parser->lexer);
28112 break;
28114 default:
28115 done = true;
28116 break;
28119 /* It is not uncommon to see programs mechanically, erroneously, use
28120 the 'typename' keyword to denote (dependent) qualified types
28121 as base classes. */
28122 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
28124 token = cp_lexer_peek_token (parser->lexer);
28125 if (!processing_template_decl)
28126 error_at (token->location,
28127 "keyword %<typename%> not allowed outside of templates");
28128 else
28129 error_at (token->location,
28130 "keyword %<typename%> not allowed in this context "
28131 "(the base class is implicitly a type)");
28132 cp_lexer_consume_token (parser->lexer);
28135 /* Look for the optional `::' operator. */
28136 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
28137 /* Look for the nested-name-specifier. The simplest way to
28138 implement:
28140 [temp.res]
28142 The keyword `typename' is not permitted in a base-specifier or
28143 mem-initializer; in these contexts a qualified name that
28144 depends on a template-parameter is implicitly assumed to be a
28145 type name.
28147 is to pretend that we have seen the `typename' keyword at this
28148 point. */
28149 cp_parser_nested_name_specifier_opt (parser,
28150 /*typename_keyword_p=*/true,
28151 /*check_dependency_p=*/true,
28152 /*type_p=*/true,
28153 /*is_declaration=*/true);
28154 /* If the base class is given by a qualified name, assume that names
28155 we see are type names or templates, as appropriate. */
28156 class_scope_p = (parser->scope && TYPE_P (parser->scope));
28157 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
28159 if (!parser->scope
28160 && cp_lexer_next_token_is_decltype (parser->lexer))
28161 /* DR 950 allows decltype as a base-specifier. */
28162 type = cp_parser_decltype (parser);
28163 else
28165 /* Otherwise, look for the class-name. */
28166 type = cp_parser_class_name (parser,
28167 class_scope_p,
28168 template_p,
28169 typename_type,
28170 /*check_dependency_p=*/true,
28171 /*class_head_p=*/false,
28172 /*is_declaration=*/true);
28173 type = TREE_TYPE (type);
28176 if (type == error_mark_node)
28177 return error_mark_node;
28179 return finish_base_specifier (type, access, virtual_p);
28182 /* Exception handling [gram.exception] */
28184 /* Save the tokens that make up the noexcept-specifier for a member-function.
28185 Returns a DEFERRED_PARSE. */
28187 static tree
28188 cp_parser_save_noexcept (cp_parser *parser)
28190 cp_token *first = parser->lexer->next_token;
28191 /* We want everything up to, including, the final ')'. */
28192 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0);
28193 cp_token *last = parser->lexer->next_token;
28195 /* As with default arguments and NSDMIs, make use of DEFERRED_PARSE
28196 to carry the information we will need. */
28197 tree expr = make_node (DEFERRED_PARSE);
28198 /* Save away the noexcept-specifier; we will process it when the
28199 class is complete. */
28200 DEFPARSE_TOKENS (expr) = cp_token_cache_new (first, last);
28201 DEFPARSE_INSTANTIATIONS (expr) = nullptr;
28202 expr = build_tree_list (expr, NULL_TREE);
28203 return expr;
28206 /* Used for late processing of noexcept-specifiers of member-functions.
28207 DEFAULT_ARG is the unparsed operand of a noexcept-specifier which
28208 we saved for later; parse it now. DECL is the declaration of the
28209 member function. */
28211 static tree
28212 cp_parser_late_noexcept_specifier (cp_parser *parser, tree default_arg)
28214 /* Make sure we've gotten something that hasn't been parsed yet. */
28215 gcc_assert (TREE_CODE (default_arg) == DEFERRED_PARSE);
28217 push_unparsed_function_queues (parser);
28219 /* Push the saved tokens for the noexcept-specifier onto the parser's
28220 lexer stack. */
28221 cp_token_cache *tokens = DEFPARSE_TOKENS (default_arg);
28222 cp_parser_push_lexer_for_tokens (parser, tokens);
28224 /* Parse the cached noexcept-specifier. */
28225 tree parsed_arg
28226 = cp_parser_noexcept_specification_opt (parser,
28227 CP_PARSER_FLAGS_NONE,
28228 /*require_constexpr=*/true,
28229 /*consumed_expr=*/NULL,
28230 /*return_cond=*/false);
28232 /* Revert to the main lexer. */
28233 cp_parser_pop_lexer (parser);
28235 /* Restore the queue. */
28236 pop_unparsed_function_queues (parser);
28238 /* And we're done. */
28239 return parsed_arg;
28242 /* Perform late checking of overriding function with respect to their
28243 noexcept-specifiers. TYPE is the class and FNDECL is the function
28244 that potentially overrides some virtual function with the same
28245 signature. */
28247 static void
28248 noexcept_override_late_checks (tree type, tree fndecl)
28250 tree binfo = TYPE_BINFO (type);
28251 tree base_binfo;
28253 if (DECL_STATIC_FUNCTION_P (fndecl))
28254 return;
28256 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
28258 tree basetype = BINFO_TYPE (base_binfo);
28260 if (!TYPE_POLYMORPHIC_P (basetype))
28261 continue;
28263 tree fn = look_for_overrides_here (basetype, fndecl);
28264 if (fn)
28265 maybe_check_overriding_exception_spec (fndecl, fn);
28269 /* Parse an (optional) noexcept-specification.
28271 noexcept-specification:
28272 noexcept ( constant-expression ) [opt]
28274 If no noexcept-specification is present, returns NULL_TREE.
28275 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
28276 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
28277 there are no parentheses. CONSUMED_EXPR will be set accordingly.
28278 Otherwise, returns a noexcept specification unless RETURN_COND is true,
28279 in which case a boolean condition is returned instead. The parser flags
28280 FLAGS is used to control parsing. QUALS are qualifiers indicating whether
28281 the (member) function is `const'. */
28283 static tree
28284 cp_parser_noexcept_specification_opt (cp_parser* parser,
28285 cp_parser_flags flags,
28286 bool require_constexpr,
28287 bool* consumed_expr,
28288 bool return_cond)
28290 cp_token *token;
28291 const char *saved_message;
28293 /* Peek at the next token. */
28294 token = cp_lexer_peek_token (parser->lexer);
28296 /* Is it a noexcept-specification? */
28297 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
28299 tree expr;
28301 /* [class.mem]/6 says that a noexcept-specifer (within the
28302 member-specification of the class) is a complete-class context of
28303 a class. So, if the noexcept-specifier has the optional expression,
28304 just save the tokens, and reparse this after we're done with the
28305 class. */
28307 if ((flags & CP_PARSER_FLAGS_DELAY_NOEXCEPT)
28308 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN)
28309 /* No need to delay parsing for a number literal or true/false. */
28310 && !((cp_lexer_nth_token_is (parser->lexer, 3, CPP_NUMBER)
28311 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
28312 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_PAREN))
28313 && at_class_scope_p ()
28314 && TYPE_BEING_DEFINED (current_class_type)
28315 && !LAMBDA_TYPE_P (current_class_type))
28316 return cp_parser_save_noexcept (parser);
28318 cp_lexer_consume_token (parser->lexer);
28320 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
28322 matching_parens parens;
28323 parens.consume_open (parser);
28325 if (require_constexpr)
28327 /* Types may not be defined in an exception-specification. */
28328 saved_message = parser->type_definition_forbidden_message;
28329 parser->type_definition_forbidden_message
28330 = G_("types may not be defined in an exception-specification");
28332 bool non_constant_p;
28333 expr
28334 = cp_parser_constant_expression (parser,
28335 /*allow_non_constant=*/true,
28336 &non_constant_p);
28337 if (non_constant_p
28338 && !require_potential_rvalue_constant_expression (expr))
28340 expr = NULL_TREE;
28341 return_cond = true;
28344 /* Restore the saved message. */
28345 parser->type_definition_forbidden_message = saved_message;
28347 else
28349 expr = cp_parser_expression (parser);
28350 *consumed_expr = true;
28353 parens.require_close (parser);
28355 else
28357 expr = boolean_true_node;
28358 if (!require_constexpr)
28359 *consumed_expr = false;
28362 /* We cannot build a noexcept-spec right away because this will check
28363 that expr is a constexpr. */
28364 if (!return_cond)
28365 return build_noexcept_spec (expr, tf_warning_or_error);
28366 else
28367 return expr;
28369 else
28370 return NULL_TREE;
28373 /* Parse an (optional) exception-specification.
28375 exception-specification:
28376 throw ( type-id-list [opt] )
28378 Returns a TREE_LIST representing the exception-specification. The
28379 TREE_VALUE of each node is a type. The parser flags FLAGS is used to
28380 control parsing. QUALS are qualifiers indicating whether the (member)
28381 function is `const'. */
28383 static tree
28384 cp_parser_exception_specification_opt (cp_parser* parser,
28385 cp_parser_flags flags)
28387 cp_token *token;
28388 tree type_id_list;
28389 const char *saved_message;
28391 /* Peek at the next token. */
28392 token = cp_lexer_peek_token (parser->lexer);
28394 /* Is it a noexcept-specification? */
28395 type_id_list
28396 = cp_parser_noexcept_specification_opt (parser, flags,
28397 /*require_constexpr=*/true,
28398 /*consumed_expr=*/NULL,
28399 /*return_cond=*/false);
28400 if (type_id_list != NULL_TREE)
28401 return type_id_list;
28403 /* If it's not `throw', then there's no exception-specification. */
28404 if (!cp_parser_is_keyword (token, RID_THROW))
28405 return NULL_TREE;
28407 location_t loc = token->location;
28409 /* Consume the `throw'. */
28410 cp_lexer_consume_token (parser->lexer);
28412 /* Look for the `('. */
28413 matching_parens parens;
28414 parens.require_open (parser);
28416 /* Peek at the next token. */
28417 token = cp_lexer_peek_token (parser->lexer);
28418 /* If it's not a `)', then there is a type-id-list. */
28419 if (token->type != CPP_CLOSE_PAREN)
28421 /* Types may not be defined in an exception-specification. */
28422 saved_message = parser->type_definition_forbidden_message;
28423 parser->type_definition_forbidden_message
28424 = G_("types may not be defined in an exception-specification");
28425 /* Parse the type-id-list. */
28426 type_id_list = cp_parser_type_id_list (parser);
28427 /* Restore the saved message. */
28428 parser->type_definition_forbidden_message = saved_message;
28430 if (cxx_dialect >= cxx17)
28432 error_at (loc, "ISO C++17 does not allow dynamic exception "
28433 "specifications");
28434 type_id_list = NULL_TREE;
28436 else if (cxx_dialect >= cxx11)
28437 warning_at (loc, OPT_Wdeprecated,
28438 "dynamic exception specifications are deprecated in "
28439 "C++11");
28441 /* In C++17, throw() is equivalent to noexcept (true). throw()
28442 is deprecated in C++11 and above as well, but is still widely used,
28443 so don't warn about it yet. */
28444 else if (cxx_dialect >= cxx17)
28445 type_id_list = noexcept_true_spec;
28446 else
28447 type_id_list = empty_except_spec;
28449 /* Look for the `)'. */
28450 parens.require_close (parser);
28452 return type_id_list;
28455 /* Parse an (optional) type-id-list.
28457 type-id-list:
28458 type-id ... [opt]
28459 type-id-list , type-id ... [opt]
28461 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
28462 in the order that the types were presented. */
28464 static tree
28465 cp_parser_type_id_list (cp_parser* parser)
28467 tree types = NULL_TREE;
28469 while (true)
28471 cp_token *token;
28472 tree type;
28474 token = cp_lexer_peek_token (parser->lexer);
28476 /* Get the next type-id. */
28477 type = cp_parser_type_id (parser);
28478 /* Check for invalid 'auto'. */
28479 if (flag_concepts && type_uses_auto (type))
28481 error_at (token->location,
28482 "invalid use of %<auto%> in exception-specification");
28483 type = error_mark_node;
28485 /* Parse the optional ellipsis. */
28486 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28488 /* Consume the `...'. */
28489 cp_lexer_consume_token (parser->lexer);
28491 /* Turn the type into a pack expansion expression. */
28492 type = make_pack_expansion (type);
28494 /* Add it to the list. */
28495 types = add_exception_specifier (types, type, /*complain=*/1);
28496 /* Peek at the next token. */
28497 token = cp_lexer_peek_token (parser->lexer);
28498 /* If it is not a `,', we are done. */
28499 if (token->type != CPP_COMMA)
28500 break;
28501 /* Consume the `,'. */
28502 cp_lexer_consume_token (parser->lexer);
28505 return nreverse (types);
28508 /* Parse a try-block.
28510 try-block:
28511 try compound-statement handler-seq */
28513 static tree
28514 cp_parser_try_block (cp_parser* parser)
28516 tree try_block;
28518 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
28519 if (parser->in_function_body
28520 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
28521 && cxx_dialect < cxx20)
28522 pedwarn (input_location, OPT_Wc__20_extensions,
28523 "%<try%> in %<constexpr%> function only "
28524 "available with %<-std=c++20%> or %<-std=gnu++20%>");
28526 try_block = begin_try_block ();
28527 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
28528 finish_try_block (try_block);
28529 cp_parser_handler_seq (parser);
28530 finish_handler_sequence (try_block);
28532 return try_block;
28535 /* Parse a function-try-block.
28537 function-try-block:
28538 try ctor-initializer [opt] function-body handler-seq */
28540 static void
28541 cp_parser_function_try_block (cp_parser* parser)
28543 tree compound_stmt;
28544 tree try_block;
28546 /* Look for the `try' keyword. */
28547 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
28548 return;
28549 /* Let the rest of the front end know where we are. */
28550 try_block = begin_function_try_block (&compound_stmt);
28551 /* Parse the function-body. */
28552 cp_parser_ctor_initializer_opt_and_function_body
28553 (parser, /*in_function_try_block=*/true);
28554 /* We're done with the `try' part. */
28555 finish_function_try_block (try_block);
28556 /* Parse the handlers. */
28557 cp_parser_handler_seq (parser);
28558 /* We're done with the handlers. */
28559 finish_function_handler_sequence (try_block, compound_stmt);
28562 /* Parse a handler-seq.
28564 handler-seq:
28565 handler handler-seq [opt] */
28567 static void
28568 cp_parser_handler_seq (cp_parser* parser)
28570 while (true)
28572 cp_token *token;
28574 /* Parse the handler. */
28575 cp_parser_handler (parser);
28576 /* Peek at the next token. */
28577 token = cp_lexer_peek_token (parser->lexer);
28578 /* If it's not `catch' then there are no more handlers. */
28579 if (!cp_parser_is_keyword (token, RID_CATCH))
28580 break;
28584 /* Parse a handler.
28586 handler:
28587 catch ( exception-declaration ) compound-statement */
28589 static void
28590 cp_parser_handler (cp_parser* parser)
28592 tree handler;
28593 tree declaration;
28595 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
28596 handler = begin_handler ();
28597 matching_parens parens;
28598 parens.require_open (parser);
28599 declaration = cp_parser_exception_declaration (parser);
28600 finish_handler_parms (declaration, handler);
28601 parens.require_close (parser);
28602 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
28603 finish_handler (handler);
28606 /* Parse an exception-declaration.
28608 exception-declaration:
28609 type-specifier-seq declarator
28610 type-specifier-seq abstract-declarator
28611 type-specifier-seq
28614 Returns a VAR_DECL for the declaration, or NULL_TREE if the
28615 ellipsis variant is used. */
28617 static tree
28618 cp_parser_exception_declaration (cp_parser* parser)
28620 cp_decl_specifier_seq type_specifiers;
28621 cp_declarator *declarator;
28622 const char *saved_message;
28624 /* If it's an ellipsis, it's easy to handle. */
28625 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28627 /* Consume the `...' token. */
28628 cp_lexer_consume_token (parser->lexer);
28629 return NULL_TREE;
28632 /* Types may not be defined in exception-declarations. */
28633 saved_message = parser->type_definition_forbidden_message;
28634 parser->type_definition_forbidden_message
28635 = G_("types may not be defined in exception-declarations");
28637 /* Parse the type-specifier-seq. */
28638 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
28639 /*is_declaration=*/true,
28640 /*is_trailing_return=*/false,
28641 &type_specifiers);
28642 /* If it's a `)', then there is no declarator. */
28643 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
28644 declarator = NULL;
28645 else
28646 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
28647 CP_PARSER_FLAGS_NONE,
28648 /*ctor_dtor_or_conv_p=*/NULL,
28649 /*parenthesized_p=*/NULL,
28650 /*member_p=*/false,
28651 /*friend_p=*/false,
28652 /*static_p=*/false);
28654 /* Restore the saved message. */
28655 parser->type_definition_forbidden_message = saved_message;
28657 if (!type_specifiers.any_specifiers_p)
28658 return error_mark_node;
28660 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
28663 /* Parse a throw-expression.
28665 throw-expression:
28666 throw assignment-expression [opt]
28668 Returns a THROW_EXPR representing the throw-expression. */
28670 static tree
28671 cp_parser_throw_expression (cp_parser* parser)
28673 tree expression;
28674 cp_token* token;
28675 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28677 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
28678 token = cp_lexer_peek_token (parser->lexer);
28679 /* Figure out whether or not there is an assignment-expression
28680 following the "throw" keyword. */
28681 if (token->type == CPP_COMMA
28682 || token->type == CPP_SEMICOLON
28683 || token->type == CPP_CLOSE_PAREN
28684 || token->type == CPP_CLOSE_SQUARE
28685 || token->type == CPP_CLOSE_BRACE
28686 || token->type == CPP_COLON)
28687 expression = NULL_TREE;
28688 else
28689 expression = cp_parser_assignment_expression (parser);
28691 /* Construct a location e.g.:
28692 throw x
28693 ^~~~~~~
28694 with caret == start at the start of the "throw" token, and
28695 the end at the end of the final token we consumed. */
28696 location_t combined_loc = make_location (start_loc, start_loc,
28697 parser->lexer);
28698 expression = build_throw (combined_loc, expression);
28700 return expression;
28703 /* Parse a yield-expression.
28705 yield-expression:
28706 co_yield assignment-expression
28707 co_yield braced-init-list
28709 Returns a CO_YIELD_EXPR representing the yield-expression. */
28711 static tree
28712 cp_parser_yield_expression (cp_parser* parser)
28714 tree expr;
28716 cp_token *token = cp_lexer_peek_token (parser->lexer);
28717 location_t kw_loc = token->location; /* Save for later. */
28719 cp_parser_require_keyword (parser, RID_CO_YIELD, RT_CO_YIELD);
28721 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28723 bool expr_non_constant_p;
28724 cp_lexer_set_source_position (parser->lexer);
28725 /* ??? : probably a moot point? */
28726 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
28727 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
28729 else
28730 expr = cp_parser_assignment_expression (parser);
28732 if (expr == error_mark_node)
28733 return expr;
28735 return finish_co_yield_expr (kw_loc, expr);
28738 /* GNU Extensions */
28740 /* Parse an (optional) asm-specification.
28742 asm-specification:
28743 asm ( string-literal )
28745 If the asm-specification is present, returns a STRING_CST
28746 corresponding to the string-literal. Otherwise, returns
28747 NULL_TREE. */
28749 static tree
28750 cp_parser_asm_specification_opt (cp_parser* parser)
28752 /* Peek at the next token. */
28753 cp_token *token = cp_lexer_peek_token (parser->lexer);
28754 /* If the next token isn't the `asm' keyword, then there's no
28755 asm-specification. */
28756 if (!cp_parser_is_keyword (token, RID_ASM))
28757 return NULL_TREE;
28759 /* Consume the `asm' token. */
28760 cp_lexer_consume_token (parser->lexer);
28761 /* Look for the `('. */
28762 matching_parens parens;
28763 parens.require_open (parser);
28765 /* Look for the string-literal. */
28766 tree asm_specification = cp_parser_string_literal (parser,
28767 /*translate=*/false,
28768 /*wide_ok=*/false);
28770 /* Look for the `)'. */
28771 parens.require_close (parser);
28773 return asm_specification;
28776 /* Parse an asm-operand-list.
28778 asm-operand-list:
28779 asm-operand
28780 asm-operand-list , asm-operand
28782 asm-operand:
28783 string-literal ( expression )
28784 [ string-literal ] string-literal ( expression )
28786 Returns a TREE_LIST representing the operands. The TREE_VALUE of
28787 each node is the expression. The TREE_PURPOSE is itself a
28788 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
28789 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
28790 is a STRING_CST for the string literal before the parenthesis. Returns
28791 ERROR_MARK_NODE if any of the operands are invalid. */
28793 static tree
28794 cp_parser_asm_operand_list (cp_parser* parser)
28796 tree asm_operands = NULL_TREE;
28797 bool invalid_operands = false;
28799 while (true)
28801 tree name;
28803 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
28805 /* Consume the `[' token. */
28806 cp_lexer_consume_token (parser->lexer);
28807 /* Read the operand name. */
28808 name = cp_parser_identifier (parser);
28809 if (name != error_mark_node)
28810 name = build_string (IDENTIFIER_LENGTH (name),
28811 IDENTIFIER_POINTER (name));
28812 /* Look for the closing `]'. */
28813 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
28815 else
28816 name = NULL_TREE;
28817 /* Look for the string-literal. */
28818 tree string_literal = cp_parser_string_literal (parser,
28819 /*translate=*/false,
28820 /*wide_ok=*/false);
28822 /* Look for the `('. */
28823 matching_parens parens;
28824 parens.require_open (parser);
28825 /* Parse the expression. */
28826 tree expression = cp_parser_expression (parser);
28827 /* Look for the `)'. */
28828 parens.require_close (parser);
28830 if (name == error_mark_node
28831 || string_literal == error_mark_node
28832 || expression == error_mark_node)
28833 invalid_operands = true;
28835 /* Add this operand to the list. */
28836 asm_operands = tree_cons (build_tree_list (name, string_literal),
28837 expression,
28838 asm_operands);
28839 /* If the next token is not a `,', there are no more
28840 operands. */
28841 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
28842 break;
28843 /* Consume the `,'. */
28844 cp_lexer_consume_token (parser->lexer);
28847 return invalid_operands ? error_mark_node : nreverse (asm_operands);
28850 /* Parse an asm-clobber-list.
28852 asm-clobber-list:
28853 string-literal
28854 asm-clobber-list , string-literal
28856 Returns a TREE_LIST, indicating the clobbers in the order that they
28857 appeared. The TREE_VALUE of each node is a STRING_CST. */
28859 static tree
28860 cp_parser_asm_clobber_list (cp_parser* parser)
28862 tree clobbers = NULL_TREE;
28864 while (true)
28866 /* Look for the string literal. */
28867 tree string_literal = cp_parser_string_literal (parser,
28868 /*translate=*/false,
28869 /*wide_ok=*/false);
28870 /* Add it to the list. */
28871 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
28872 /* If the next token is not a `,', then the list is
28873 complete. */
28874 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
28875 break;
28876 /* Consume the `,' token. */
28877 cp_lexer_consume_token (parser->lexer);
28880 return clobbers;
28883 /* Parse an asm-label-list.
28885 asm-label-list:
28886 identifier
28887 asm-label-list , identifier
28889 Returns a TREE_LIST, indicating the labels in the order that they
28890 appeared. The TREE_VALUE of each node is a label. */
28892 static tree
28893 cp_parser_asm_label_list (cp_parser* parser)
28895 tree labels = NULL_TREE;
28897 while (true)
28899 tree identifier, label, name;
28901 /* Look for the identifier. */
28902 identifier = cp_parser_identifier (parser);
28903 if (!error_operand_p (identifier))
28905 label = lookup_label (identifier);
28906 if (TREE_CODE (label) == LABEL_DECL)
28908 TREE_USED (label) = 1;
28909 check_goto (label);
28910 name = build_string (IDENTIFIER_LENGTH (identifier),
28911 IDENTIFIER_POINTER (identifier));
28912 labels = tree_cons (name, label, labels);
28915 /* If the next token is not a `,', then the list is
28916 complete. */
28917 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
28918 break;
28919 /* Consume the `,' token. */
28920 cp_lexer_consume_token (parser->lexer);
28923 return nreverse (labels);
28926 /* Return TRUE iff the next tokens in the stream are possibly the
28927 beginning of a GNU extension attribute. */
28929 static bool
28930 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
28932 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
28935 /* Return TRUE iff the next tokens in the stream are possibly the
28936 beginning of a standard C++-11 attribute specifier. */
28938 static bool
28939 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
28941 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
28944 /* Return TRUE iff the next Nth tokens in the stream are possibly the
28945 beginning of a standard C++-11 attribute specifier. */
28947 static bool
28948 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
28950 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
28952 return (cxx_dialect >= cxx11
28953 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
28954 || (token->type == CPP_OPEN_SQUARE
28955 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
28956 && token->type == CPP_OPEN_SQUARE)));
28959 /* Return TRUE iff the next Nth tokens in the stream are possibly the
28960 beginning of a GNU extension attribute. */
28962 static bool
28963 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
28965 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
28967 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
28970 /* Return true iff the next tokens can be the beginning of either a
28971 GNU attribute list, or a standard C++11 attribute sequence. */
28973 static bool
28974 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
28976 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
28977 || cp_next_tokens_can_be_std_attribute_p (parser));
28980 /* Return true iff the next Nth tokens can be the beginning of either
28981 a GNU attribute list, or a standard C++11 attribute sequence. */
28983 static bool
28984 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
28986 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
28987 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
28990 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
28991 of GNU attributes, or return NULL. */
28993 static tree
28994 cp_parser_attributes_opt (cp_parser *parser)
28996 tree attrs = NULL_TREE;
28997 while (true)
28999 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
29000 attrs = attr_chainon (attrs, cp_parser_gnu_attributes_opt (parser));
29001 else if (cp_next_tokens_can_be_std_attribute_p (parser))
29002 attrs = attr_chainon (attrs, cp_parser_std_attribute_spec_seq (parser));
29003 else
29004 break;
29006 return attrs;
29009 /* Parse an (optional) series of attributes.
29011 attributes:
29012 attributes attribute
29014 attribute:
29015 __attribute__ (( attribute-list [opt] ))
29017 The return value is as for cp_parser_gnu_attribute_list. */
29019 static tree
29020 cp_parser_gnu_attributes_opt (cp_parser* parser)
29022 tree attributes = NULL_TREE;
29024 auto cleanup = make_temp_override
29025 (parser->auto_is_implicit_function_template_parm_p, false);
29027 while (true)
29029 cp_token *token;
29030 tree attribute_list;
29031 bool ok = true;
29033 /* Peek at the next token. */
29034 token = cp_lexer_peek_token (parser->lexer);
29035 /* If it's not `__attribute__', then we're done. */
29036 if (token->keyword != RID_ATTRIBUTE)
29037 break;
29039 /* Consume the `__attribute__' keyword. */
29040 cp_lexer_consume_token (parser->lexer);
29041 /* Look for the two `(' tokens. */
29042 matching_parens outer_parens;
29043 if (!outer_parens.require_open (parser))
29044 ok = false;
29045 matching_parens inner_parens;
29046 if (!inner_parens.require_open (parser))
29047 ok = false;
29049 /* Peek at the next token. */
29050 token = cp_lexer_peek_token (parser->lexer);
29051 if (token->type != CPP_CLOSE_PAREN)
29052 /* Parse the attribute-list. */
29053 attribute_list = cp_parser_gnu_attribute_list (parser);
29054 else
29055 /* If the next token is a `)', then there is no attribute
29056 list. */
29057 attribute_list = NULL;
29059 /* Look for the two `)' tokens. */
29060 if (!inner_parens.require_close (parser))
29061 ok = false;
29062 if (!outer_parens.require_close (parser))
29063 ok = false;
29064 if (!ok)
29065 cp_parser_skip_to_end_of_statement (parser);
29067 /* Add these new attributes to the list. */
29068 attributes = attr_chainon (attributes, attribute_list);
29071 return attributes;
29074 /* Parse a GNU attribute-list.
29076 attribute-list:
29077 attribute
29078 attribute-list , attribute
29080 attribute:
29081 identifier
29082 identifier ( identifier )
29083 identifier ( identifier , expression-list )
29084 identifier ( expression-list )
29086 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
29087 to an attribute. The TREE_PURPOSE of each node is the identifier
29088 indicating which attribute is in use. The TREE_VALUE represents
29089 the arguments, if any. */
29091 static tree
29092 cp_parser_gnu_attribute_list (cp_parser* parser, bool exactly_one /* = false */)
29094 tree attribute_list = NULL_TREE;
29095 bool save_translate_strings_p = parser->translate_strings_p;
29097 /* Don't create wrapper nodes within attributes: the
29098 handlers don't know how to handle them. */
29099 auto_suppress_location_wrappers sentinel;
29101 parser->translate_strings_p = false;
29102 while (true)
29104 cp_token *token;
29105 tree identifier;
29106 tree attribute;
29108 /* Look for the identifier. We also allow keywords here; for
29109 example `__attribute__ ((const))' is legal. */
29110 token = cp_lexer_peek_token (parser->lexer);
29111 if (token->type == CPP_NAME
29112 || token->type == CPP_KEYWORD)
29114 tree arguments = NULL_TREE;
29116 /* Consume the token, but save it since we need it for the
29117 SIMD enabled function parsing. */
29118 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
29120 /* Save away the identifier that indicates which attribute
29121 this is. */
29122 identifier = (token->type == CPP_KEYWORD)
29123 /* For keywords, use the canonical spelling, not the
29124 parsed identifier. */
29125 ? ridpointers[(int) token->keyword]
29126 : id_token->u.value;
29128 identifier = canonicalize_attr_name (identifier);
29129 attribute = build_tree_list (identifier, NULL_TREE);
29131 /* Peek at the next token. */
29132 token = cp_lexer_peek_token (parser->lexer);
29133 /* If it's an `(', then parse the attribute arguments. */
29134 if (token->type == CPP_OPEN_PAREN)
29136 vec<tree, va_gc> *vec;
29137 int attr_flag = (attribute_takes_identifier_p (identifier)
29138 ? id_attr : normal_attr);
29139 if (is_attribute_p ("assume", identifier))
29140 attr_flag = assume_attr;
29141 vec = cp_parser_parenthesized_expression_list
29142 (parser, attr_flag, /*cast_p=*/false,
29143 /*allow_expansion_p=*/false,
29144 /*non_constant_p=*/NULL);
29145 if (vec == NULL)
29146 arguments = error_mark_node;
29147 else
29149 arguments = build_tree_list_vec (vec);
29150 release_tree_vector (vec);
29152 /* Save the arguments away. */
29153 TREE_VALUE (attribute) = arguments;
29156 if (arguments != error_mark_node)
29158 /* Add this attribute to the list. */
29159 TREE_CHAIN (attribute) = attribute_list;
29160 attribute_list = attribute;
29163 token = cp_lexer_peek_token (parser->lexer);
29165 /* Unless EXACTLY_ONE is set look for more attributes.
29166 If the next token isn't a `,', we're done. */
29167 if (exactly_one || token->type != CPP_COMMA)
29168 break;
29170 /* Consume the comma and keep going. */
29171 cp_lexer_consume_token (parser->lexer);
29173 parser->translate_strings_p = save_translate_strings_p;
29175 /* We built up the list in reverse order. */
29176 return nreverse (attribute_list);
29179 /* Parse arguments of omp::directive attribute.
29181 ( directive-name ,[opt] clause-list[opt] )
29183 For directive just remember the first/last tokens for subsequent
29184 parsing. */
29186 static void
29187 cp_parser_omp_directive_args (cp_parser *parser, tree attribute)
29189 cp_token *first = cp_lexer_peek_nth_token (parser->lexer, 2);
29190 if (first->type == CPP_CLOSE_PAREN)
29192 cp_lexer_consume_token (parser->lexer);
29193 error_at (first->location, "expected OpenMP directive name");
29194 cp_lexer_consume_token (parser->lexer);
29195 TREE_VALUE (attribute) = NULL_TREE;
29196 return;
29198 size_t n = cp_parser_skip_balanced_tokens (parser, 1);
29199 if (n == 1)
29201 cp_lexer_consume_token (parser->lexer);
29202 error_at (first->location, "expected attribute argument as balanced "
29203 "token sequence");
29204 TREE_VALUE (attribute) = NULL_TREE;
29205 return;
29207 for (n = n - 2; n; --n)
29208 cp_lexer_consume_token (parser->lexer);
29209 cp_token *last = cp_lexer_peek_token (parser->lexer);
29210 cp_lexer_consume_token (parser->lexer);
29211 tree arg = make_node (DEFERRED_PARSE);
29212 DEFPARSE_TOKENS (arg) = cp_token_cache_new (first, last);
29213 DEFPARSE_INSTANTIATIONS (arg) = nullptr;
29214 TREE_VALUE (attribute) = tree_cons (NULL_TREE, arg, TREE_VALUE (attribute));
29217 /* Parse arguments of omp::sequence attribute.
29219 ( omp::[opt] directive-attr [ , omp::[opt] directive-attr ]... ) */
29221 static void
29222 cp_parser_omp_sequence_args (cp_parser *parser, tree attribute)
29224 matching_parens parens;
29225 parens.consume_open (parser);
29228 cp_token *token = cp_lexer_peek_token (parser->lexer);
29229 if (token->type == CPP_NAME
29230 && token->u.value == omp_identifier
29231 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
29233 cp_lexer_consume_token (parser->lexer);
29234 cp_lexer_consume_token (parser->lexer);
29235 token = cp_lexer_peek_token (parser->lexer);
29237 bool directive = false;
29238 const char *p;
29239 if (token->type != CPP_NAME)
29240 p = "";
29241 else
29242 p = IDENTIFIER_POINTER (token->u.value);
29243 if (strcmp (p, "directive") == 0)
29244 directive = true;
29245 else if (strcmp (p, "sequence") != 0)
29247 error_at (token->location, "expected %<directive%> or %<sequence%>");
29248 cp_parser_skip_to_closing_parenthesis (parser,
29249 /*recovering=*/true,
29250 /*or_comma=*/true,
29251 /*consume_paren=*/false);
29252 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
29253 break;
29254 cp_lexer_consume_token (parser->lexer);
29256 cp_lexer_consume_token (parser->lexer);
29257 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
29258 cp_parser_required_error (parser, RT_OPEN_PAREN, false,
29259 UNKNOWN_LOCATION);
29260 else if (directive)
29261 cp_parser_omp_directive_args (parser, attribute);
29262 else
29263 cp_parser_omp_sequence_args (parser, attribute);
29264 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
29265 break;
29266 cp_lexer_consume_token (parser->lexer);
29268 while (1);
29269 if (!parens.require_close (parser))
29270 cp_parser_skip_to_closing_parenthesis (parser, true, false,
29271 /*consume_paren=*/true);
29274 /* Parse a standard C++11 attribute.
29276 The returned representation is a TREE_LIST which TREE_PURPOSE is
29277 the scoped name of the attribute, and the TREE_VALUE is its
29278 arguments list.
29280 Note that the scoped name of the attribute is itself a TREE_LIST
29281 which TREE_PURPOSE is the namespace of the attribute, and
29282 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
29283 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
29284 and which TREE_PURPOSE is directly the attribute name.
29286 Clients of the attribute code should use get_attribute_namespace
29287 and get_attribute_name to get the actual namespace and name of
29288 attributes, regardless of their being GNU or C++11 attributes.
29290 attribute:
29291 attribute-token attribute-argument-clause [opt]
29293 attribute-token:
29294 identifier
29295 attribute-scoped-token
29297 attribute-scoped-token:
29298 attribute-namespace :: identifier
29300 attribute-namespace:
29301 identifier
29303 attribute-argument-clause:
29304 ( balanced-token-seq )
29306 balanced-token-seq:
29307 balanced-token [opt]
29308 balanced-token-seq balanced-token
29310 balanced-token:
29311 ( balanced-token-seq )
29312 [ balanced-token-seq ]
29313 { balanced-token-seq }. */
29315 static tree
29316 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
29318 tree attribute, attr_id = NULL_TREE, arguments;
29319 cp_token *token;
29321 auto cleanup = make_temp_override
29322 (parser->auto_is_implicit_function_template_parm_p, false);
29324 /* First, parse name of the attribute, a.k.a attribute-token. */
29326 token = cp_lexer_peek_token (parser->lexer);
29327 if (token->type == CPP_NAME)
29328 attr_id = token->u.value;
29329 else if (token->type == CPP_KEYWORD)
29330 attr_id = ridpointers[(int) token->keyword];
29331 else if (token->flags & NAMED_OP)
29332 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
29334 if (attr_id == NULL_TREE)
29335 return NULL_TREE;
29337 cp_lexer_consume_token (parser->lexer);
29339 token = cp_lexer_peek_token (parser->lexer);
29340 if (token->type == CPP_SCOPE)
29342 /* We are seeing a scoped attribute token. */
29344 cp_lexer_consume_token (parser->lexer);
29345 if (attr_ns)
29346 error_at (token->location, "attribute using prefix used together "
29347 "with scoped attribute token");
29348 attr_ns = attr_id;
29350 token = cp_lexer_peek_token (parser->lexer);
29351 if (token->type == CPP_NAME)
29352 attr_id = token->u.value;
29353 else if (token->type == CPP_KEYWORD)
29354 attr_id = ridpointers[(int) token->keyword];
29355 else if (token->flags & NAMED_OP)
29356 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
29357 else
29359 error_at (token->location,
29360 "expected an identifier for the attribute name");
29361 return error_mark_node;
29363 cp_lexer_consume_token (parser->lexer);
29365 attr_ns = canonicalize_attr_name (attr_ns);
29366 attr_id = canonicalize_attr_name (attr_id);
29367 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
29368 NULL_TREE);
29369 token = cp_lexer_peek_token (parser->lexer);
29371 else if (attr_ns)
29373 attr_ns = canonicalize_attr_name (attr_ns);
29374 attr_id = canonicalize_attr_name (attr_id);
29375 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
29376 NULL_TREE);
29378 else
29380 attr_id = canonicalize_attr_name (attr_id);
29381 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
29382 NULL_TREE);
29384 /* We used to treat C++11 noreturn attribute as equivalent to GNU's,
29385 but no longer: we have to be able to tell [[noreturn]] and
29386 __attribute__((noreturn)) apart. */
29387 /* C++14 deprecated attribute is equivalent to GNU's. */
29388 if (is_attribute_p ("deprecated", attr_id))
29389 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
29390 /* C++17 fallthrough attribute is equivalent to GNU's. */
29391 else if (is_attribute_p ("fallthrough", attr_id))
29392 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
29393 /* C++23 assume attribute is equivalent to GNU's. */
29394 else if (is_attribute_p ("assume", attr_id))
29395 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
29396 /* Transactional Memory TS optimize_for_synchronized attribute is
29397 equivalent to GNU transaction_callable. */
29398 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
29399 TREE_PURPOSE (attribute)
29400 = get_identifier ("transaction_callable");
29401 /* Transactional Memory attributes are GNU attributes. */
29402 else if (tm_attr_to_mask (attr_id))
29403 TREE_PURPOSE (attribute) = attr_id;
29406 /* Now parse the optional argument clause of the attribute. */
29408 if (token->type != CPP_OPEN_PAREN)
29410 if ((flag_openmp || flag_openmp_simd)
29411 && attr_ns == omp_identifier
29412 && (is_attribute_p ("directive", attr_id)
29413 || is_attribute_p ("sequence", attr_id)))
29415 error_at (token->location, "%<omp::%E%> attribute requires argument",
29416 attr_id);
29417 return NULL_TREE;
29419 return attribute;
29423 vec<tree, va_gc> *vec;
29424 int attr_flag = normal_attr;
29426 /* Maybe we don't expect to see any arguments for this attribute. */
29427 const attribute_spec *as
29428 = lookup_attribute_spec (TREE_PURPOSE (attribute));
29429 if (as && as->max_length == 0)
29431 error_at (token->location, "%qE attribute does not take any arguments",
29432 attr_id);
29433 cp_parser_skip_to_closing_parenthesis (parser,
29434 /*recovering=*/true,
29435 /*or_comma=*/false,
29436 /*consume_paren=*/true);
29437 return error_mark_node;
29440 if (is_attribute_p ("assume", attr_id)
29441 && (attr_ns == NULL_TREE || attr_ns == gnu_identifier))
29442 /* The assume attribute needs special handling of the argument. */
29443 attr_flag = assume_attr;
29444 else if (attr_ns == gnu_identifier
29445 && attribute_takes_identifier_p (attr_id))
29446 /* A GNU attribute that takes an identifier in parameter. */
29447 attr_flag = id_attr;
29449 /* If this is a fake attribute created to handle -Wno-attributes,
29450 we must skip parsing the arguments. */
29451 if (as == NULL || attribute_ignored_p (as))
29453 if ((flag_openmp || flag_openmp_simd) && attr_ns == omp_identifier)
29455 if (is_attribute_p ("directive", attr_id))
29457 cp_parser_omp_directive_args (parser, attribute);
29458 return attribute;
29460 else if (is_attribute_p ("sequence", attr_id))
29462 TREE_VALUE (TREE_PURPOSE (attribute))
29463 = get_identifier ("directive");
29464 cp_parser_omp_sequence_args (parser, attribute);
29465 TREE_VALUE (attribute) = nreverse (TREE_VALUE (attribute));
29466 return attribute;
29470 /* For unknown attributes, just skip balanced tokens instead of
29471 trying to parse the arguments. */
29472 for (size_t n = cp_parser_skip_balanced_tokens (parser, 1) - 1; n; --n)
29473 cp_lexer_consume_token (parser->lexer);
29474 return attribute;
29477 vec = cp_parser_parenthesized_expression_list
29478 (parser, attr_flag, /*cast_p=*/false,
29479 /*allow_expansion_p=*/true,
29480 /*non_constant_p=*/NULL);
29481 if (vec == NULL)
29482 arguments = error_mark_node;
29483 else
29485 if (vec->is_empty ())
29486 /* e.g. [[attr()]]. */
29487 error_at (token->location, "parentheses must be omitted if "
29488 "%qE attribute argument list is empty",
29489 attr_id);
29490 arguments = build_tree_list_vec (vec);
29491 release_tree_vector (vec);
29494 if (arguments == error_mark_node)
29495 attribute = error_mark_node;
29496 else
29497 TREE_VALUE (attribute) = arguments;
29500 return attribute;
29503 /* Warn if the attribute ATTRIBUTE appears more than once in the
29504 attribute-list ATTRIBUTES. This used to be enforced for certain
29505 attributes, but the restriction was removed in P2156.
29506 LOC is the location of ATTRIBUTE. Returns true if ATTRIBUTE was not
29507 found in ATTRIBUTES. */
29509 static bool
29510 cp_parser_check_std_attribute (location_t loc, tree attributes, tree attribute)
29512 static auto alist = { "noreturn", "deprecated", "nodiscard", "maybe_unused",
29513 "likely", "unlikely", "fallthrough",
29514 "no_unique_address", "carries_dependency" };
29515 if (attributes)
29516 for (const auto &a : alist)
29517 if (is_attribute_p (a, get_attribute_name (attribute))
29518 && is_attribute_namespace_p ("", attribute)
29519 && lookup_attribute ("", a, attributes))
29521 if (!from_macro_expansion_at (loc))
29522 warning_at (loc, OPT_Wattributes, "attribute %qs specified "
29523 "multiple times", a);
29524 return false;
29526 return true;
29529 /* Parse a list of standard C++-11 attributes.
29531 attribute-list:
29532 attribute [opt]
29533 attribute-list , attribute[opt]
29534 attribute ...
29535 attribute-list , attribute ...
29538 static tree
29539 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
29541 tree attributes = NULL_TREE, attribute = NULL_TREE;
29542 cp_token *token = NULL;
29544 while (true)
29546 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29547 attribute = cp_parser_std_attribute (parser, attr_ns);
29548 if (attribute == error_mark_node)
29549 break;
29550 if (attribute != NULL_TREE)
29552 if (cp_parser_check_std_attribute (loc, attributes, attribute))
29554 TREE_CHAIN (attribute) = attributes;
29555 attributes = attribute;
29558 token = cp_lexer_peek_token (parser->lexer);
29559 if (token->type == CPP_ELLIPSIS)
29561 cp_lexer_consume_token (parser->lexer);
29562 if (attribute == NULL_TREE)
29563 error_at (token->location,
29564 "expected attribute before %<...%>");
29565 else
29567 tree pack = make_pack_expansion (TREE_VALUE (attribute));
29568 if (pack == error_mark_node)
29569 return error_mark_node;
29570 TREE_VALUE (attribute) = pack;
29572 token = cp_lexer_peek_token (parser->lexer);
29574 if (token->type != CPP_COMMA)
29575 break;
29576 cp_lexer_consume_token (parser->lexer);
29578 attributes = nreverse (attributes);
29579 return attributes;
29582 /* Optionally parse a C++20 contract role. A NULL return means that no
29583 contract role was specified.
29585 contract-role:
29586 % default
29587 % identifier
29589 If the identifier does not name a known contract role, it will
29590 be assumed to be default. Returns the identifier for the role
29591 token. */
29593 static tree
29594 cp_parser_contract_role (cp_parser *parser)
29596 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_MOD));
29597 cp_lexer_consume_token (parser->lexer);
29599 cp_token *token = cp_lexer_peek_token (parser->lexer);
29600 tree role_id = NULL_TREE;
29601 if (token->type == CPP_NAME)
29602 role_id = token->u.value;
29603 else if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
29604 role_id = get_identifier ("default");
29605 else
29607 error_at (token->location, "expected contract-role");
29608 return error_mark_node;
29610 cp_lexer_consume_token (parser->lexer);
29612 /* FIXME: Warn about invalid/unknown roles? */
29613 return role_id;
29616 /* Parse an optional contract mode.
29618 contract-mode:
29619 contract-semantic
29620 [contract-level] [contract-role]
29622 contract-semantic:
29623 check_never_continue
29624 check_maybe_continue
29625 check_always_continue
29627 contract-level:
29628 default
29629 audit
29630 axiom
29632 contract-role:
29633 default
29634 identifier
29636 This grammar is taken from P1332R0. During parsing, this sets options
29637 on the MODE object to determine the configuration of the contract.
29639 Returns a tree containing the identifiers used in the configuration.
29640 This is either an IDENTIFIER with the literal semantic or a TREE_LIST
29641 whose TREE_VALUE is the contract-level and whose TREE_PURPOSE is the
29642 contract-role, if any. NULL_TREE is returned if no information is
29643 given (i.e., all defaults selected). */
29645 static tree
29646 cp_parser_contract_mode_opt (cp_parser *parser,
29647 bool postcondition_p)
29649 /* The mode is empty; the level and role are default. */
29650 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
29651 return NULL_TREE;
29653 /* There is only a role; the level is default. */
29654 if (cp_lexer_next_token_is (parser->lexer, CPP_MOD))
29656 tree role_id = cp_parser_contract_role (parser);
29657 return build_tree_list (role_id, get_identifier ("default"));
29660 /* Otherwise, match semantic or level. */
29661 cp_token *token = cp_lexer_peek_token (parser->lexer);
29662 contract_level level = CONTRACT_INVALID;
29663 contract_semantic semantic = CCS_INVALID;
29664 tree config_id;
29665 if (token->type == CPP_NAME)
29667 config_id = token->u.value;
29669 /* Either a named level, a concrete semantic, or an identifier
29670 for a postcondition. */
29671 const char *ident = IDENTIFIER_POINTER (token->u.value);
29672 level = map_contract_level (ident);
29673 semantic = map_contract_semantic (ident);
29675 /* The identifier is the return value for a postcondition. */
29676 if (level == CONTRACT_INVALID && semantic == CCS_INVALID
29677 && postcondition_p)
29678 return NULL_TREE;
29680 else if (token->type == CPP_KEYWORD && token->keyword == RID_DEFAULT)
29682 config_id = get_identifier ("default");
29683 level = CONTRACT_DEFAULT;
29685 else
29687 /* We got some other token other than a ':'. */
29688 error_at (token->location, "expected contract semantic or level");
29689 return NULL_TREE;
29692 /* Consume the literal semantic or level token. */
29693 cp_lexer_consume_token (parser->lexer);
29695 if (semantic == CCS_INVALID && level == CONTRACT_INVALID)
29697 error_at (token->location,
29698 "expected contract level: "
29699 "%<default%>, %<audit%>, or %<axiom%>");
29700 return NULL_TREE;
29703 /* We matched an explicit semantic. */
29704 if (semantic != CCS_INVALID)
29706 if (cp_lexer_next_token_is (parser->lexer, CPP_MOD))
29708 error ("invalid use of contract role for explicit semantic");
29709 cp_lexer_consume_token (parser->lexer);
29710 cp_lexer_consume_token (parser->lexer);
29712 return config_id;
29715 /* We matched a level, there may be a role; otherwise this is default. */
29716 if (cp_lexer_next_token_is (parser->lexer, CPP_MOD))
29718 tree role_id = cp_parser_contract_role (parser);
29719 return build_tree_list (role_id, config_id);
29722 return build_tree_list (NULL_TREE, config_id);
29725 static tree
29726 find_error (tree *tp, int *, void *)
29728 if (*tp == error_mark_node)
29729 return *tp;
29730 return NULL_TREE;
29733 static bool
29734 contains_error_p (tree t)
29736 return walk_tree (&t, find_error, NULL, NULL);
29739 /* Parse a standard C++20 contract attribute specifier.
29741 contract-attribute-specifier:
29742 [ [ assert contract-level [opt] : conditional-expression ] ]
29743 [ [ pre contract-level [opt] : conditional-expression ] ]
29744 [ [ post contract-level [opt] identifier [opt] : conditional-expression ] ]
29746 For free functions, we cannot determine the type of the postcondition
29747 identifier because the we haven't called grokdeclarator yet. In those
29748 cases we parse the postcondition as if the identifier was declared as
29749 'auto <identifier>'. We then instantiate the postcondition once the
29750 return type is known.
29752 For member functions, contracts are in the complete-class context, so the
29753 parse is deferred. We also have the return type avaialable (unless it's
29754 deduced), so we don't need to parse the postcondition in terms of a
29755 placeholder. */
29757 static tree
29758 cp_parser_contract_attribute_spec (cp_parser *parser, tree attribute)
29760 gcc_assert (contract_attribute_p (attribute));
29761 cp_token *token = cp_lexer_consume_token (parser->lexer);
29762 location_t loc = token->location;
29764 bool assertion_p = is_attribute_p ("assert", attribute);
29765 bool postcondition_p = is_attribute_p ("post", attribute);
29767 /* Parse the optional mode. */
29768 tree mode = cp_parser_contract_mode_opt (parser, postcondition_p);
29770 /* Check for postcondition identifiers. */
29771 cp_expr identifier;
29772 if (postcondition_p && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29773 identifier = cp_parser_identifier (parser);
29774 if (identifier == error_mark_node)
29775 return error_mark_node;
29777 cp_parser_require (parser, CPP_COLON, RT_COLON);
29779 /* Defer the parsing of pre/post contracts inside class definitions. */
29780 tree contract;
29781 if (!assertion_p &&
29782 current_class_type &&
29783 TYPE_BEING_DEFINED (current_class_type))
29785 /* Skip until we reach an unenclose ']'. If we ran into an unnested ']'
29786 that doesn't close the attribute, return an error and let the attribute
29787 handling code emit an error for missing ']]'. */
29788 cp_token *first = cp_lexer_peek_token (parser->lexer);
29789 cp_parser_skip_to_closing_parenthesis_1 (parser,
29790 /*recovering=*/false,
29791 CPP_CLOSE_SQUARE,
29792 /*consume_paren=*/false);
29793 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE
29794 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_CLOSE_SQUARE)
29795 return error_mark_node;
29796 cp_token *last = cp_lexer_peek_token (parser->lexer);
29798 /* Build a deferred-parse node. */
29799 tree condition = make_node (DEFERRED_PARSE);
29800 DEFPARSE_TOKENS (condition) = cp_token_cache_new (first, last);
29801 DEFPARSE_INSTANTIATIONS (condition) = NULL;
29803 /* And its corresponding contract. */
29804 contract = grok_contract (attribute, mode, identifier, condition, loc);
29806 else
29808 /* Enable location wrappers when parsing contracts. */
29809 auto suppression = make_temp_override (suppress_location_wrappers, 0);
29811 /* Build a fake variable for the result identifier. */
29812 tree result = NULL_TREE;
29813 if (identifier)
29815 begin_scope (sk_block, NULL_TREE);
29816 result = make_postcondition_variable (identifier);
29817 ++processing_template_decl;
29820 /* Parse the condition, ensuring that parameters or the return variable
29821 aren't flagged for use outside the body of a function. */
29822 ++processing_contract_condition;
29823 cp_expr condition = cp_parser_conditional_expression (parser);
29824 --processing_contract_condition;
29826 /* Try to recover from errors by scanning up to the end of the
29827 attribute. Sometimes we get partially parsed expressions, so
29828 we need to search the condition for errors. */
29829 if (contains_error_p (condition))
29830 cp_parser_skip_up_to_closing_square_bracket (parser);
29832 /* Build the contract. */
29833 contract = grok_contract (attribute, mode, result, condition, loc);
29835 /* Leave our temporary scope for the postcondition result. */
29836 if (result)
29838 --processing_template_decl;
29839 pop_bindings_and_leave_scope ();
29843 if (!flag_contracts)
29845 error_at (loc, "contracts are only available with %<-fcontracts%>");
29846 return error_mark_node;
29849 return finish_contract_attribute (attribute, contract);
29852 /* Parse a contract condition for a deferred contract. */
29854 void cp_parser_late_contract_condition (cp_parser *parser,
29855 tree fn,
29856 tree attribute)
29858 tree contract = TREE_VALUE (TREE_VALUE (attribute));
29860 /* Make sure we've gotten something that hasn't been parsed yet or that
29861 we're not parsing an invalid contract. */
29862 tree condition = CONTRACT_CONDITION (contract);
29863 if (TREE_CODE (condition) != DEFERRED_PARSE)
29864 return;
29866 tree identifier = NULL_TREE;
29867 if (TREE_CODE (contract) == POSTCONDITION_STMT)
29868 identifier = POSTCONDITION_IDENTIFIER (contract);
29870 /* Build a fake variable for the result identifier. */
29871 tree result = NULL_TREE;
29872 if (identifier)
29874 /* TODO: Can we guarantee that the identifier has a location? */
29875 location_t loc = cp_expr_location (contract);
29876 tree type = TREE_TYPE (TREE_TYPE (fn));
29877 if (!check_postcondition_result (fn, type, loc))
29879 invalidate_contract (contract);
29880 return;
29883 begin_scope (sk_block, NULL_TREE);
29884 result = make_postcondition_variable (identifier, type);
29885 ++processing_template_decl;
29888 /* 'this' is not allowed in preconditions of constructors or in postconditions
29889 of destructors. Note that the previous value of this variable is
29890 established by the calling function, so we need to save it here. */
29891 tree saved_ccr = current_class_ref;
29892 tree saved_ccp = current_class_ptr;
29893 if ((DECL_CONSTRUCTOR_P (fn) && PRECONDITION_P (contract)) ||
29894 (DECL_DESTRUCTOR_P (fn) && POSTCONDITION_P (contract)))
29896 current_class_ref = current_class_ptr = NULL_TREE;
29897 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
29900 push_unparsed_function_queues (parser);
29902 /* Push the saved tokens onto the parser's lexer stack. */
29903 cp_token_cache *tokens = DEFPARSE_TOKENS (condition);
29904 cp_parser_push_lexer_for_tokens (parser, tokens);
29906 /* Parse the condition, ensuring that parameters or the return variable
29907 aren't flagged for use outside the body of a function. */
29908 ++processing_contract_condition;
29909 condition = cp_parser_conditional_expression (parser);
29910 --processing_contract_condition;
29912 /* Revert to the main lexer. */
29913 cp_parser_pop_lexer (parser);
29915 /* Restore the queue. */
29916 pop_unparsed_function_queues (parser);
29918 current_class_ref = saved_ccr;
29919 current_class_ptr = saved_ccp;
29921 /* Commit to changes. */
29922 update_late_contract (contract, result, condition);
29924 /* Leave our temporary scope for the postcondition result. */
29925 if (result)
29927 --processing_template_decl;
29928 pop_bindings_and_leave_scope ();
29932 /* Parse a standard C++-11 attribute specifier.
29934 attribute-specifier:
29935 [ [ attribute-using-prefix [opt] attribute-list ] ]
29936 contract-attribute-specifier
29937 alignment-specifier
29939 attribute-using-prefix:
29940 using attribute-namespace :
29942 alignment-specifier:
29943 alignas ( type-id ... [opt] )
29944 alignas ( alignment-expression ... [opt] ).
29946 Extensions for contracts:
29948 contract-attribute-specifier:
29949 [ [ assert : contract-mode [opt] : conditional-expression ] ]
29950 [ [ pre : contract-mode [opt] : conditional-expression ] ]
29951 [ [ post : contract-mode [opt] identifier [opt] :
29952 conditional-expression ] ] */
29954 static tree
29955 cp_parser_std_attribute_spec (cp_parser *parser)
29957 tree attributes = NULL_TREE;
29958 cp_token *token = cp_lexer_peek_token (parser->lexer);
29960 if (token->type == CPP_OPEN_SQUARE
29961 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
29963 tree attr_ns = NULL_TREE;
29964 tree attr_name = NULL_TREE;
29966 cp_lexer_consume_token (parser->lexer);
29967 cp_lexer_consume_token (parser->lexer);
29969 token = cp_lexer_peek_token (parser->lexer);
29970 if (token->type == CPP_NAME)
29972 attr_name = token->u.value;
29973 attr_name = canonicalize_attr_name (attr_name);
29976 /* Handle contract-attribute-specs specially. */
29977 if (attr_name && contract_attribute_p (attr_name))
29979 tree attrs = cp_parser_contract_attribute_spec (parser, attr_name);
29980 if (attrs != error_mark_node)
29981 attributes = attrs;
29982 goto finish_attrs;
29985 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
29987 token = cp_lexer_peek_nth_token (parser->lexer, 2);
29988 if (token->type == CPP_NAME)
29989 attr_ns = token->u.value;
29990 else if (token->type == CPP_KEYWORD)
29991 attr_ns = ridpointers[(int) token->keyword];
29992 else if (token->flags & NAMED_OP)
29993 attr_ns = get_identifier (cpp_type2name (token->type,
29994 token->flags));
29995 if (attr_ns
29996 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
29998 if (cxx_dialect < cxx17)
29999 pedwarn (input_location, OPT_Wc__17_extensions,
30000 "attribute using prefix only available "
30001 "with %<-std=c++17%> or %<-std=gnu++17%>");
30003 cp_lexer_consume_token (parser->lexer);
30004 cp_lexer_consume_token (parser->lexer);
30005 cp_lexer_consume_token (parser->lexer);
30007 else
30008 attr_ns = NULL_TREE;
30011 attributes = cp_parser_std_attribute_list (parser, attr_ns);
30013 finish_attrs:
30014 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
30015 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
30016 cp_parser_skip_to_end_of_statement (parser);
30017 else
30018 /* Warn about parsing c++11 attribute in non-c++11 mode, only
30019 when we are sure that we have actually parsed them. */
30020 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
30022 else
30024 tree alignas_expr;
30026 /* Look for an alignment-specifier. */
30028 token = cp_lexer_peek_token (parser->lexer);
30030 if (token->type != CPP_KEYWORD
30031 || token->keyword != RID_ALIGNAS)
30032 return NULL_TREE;
30034 cp_lexer_consume_token (parser->lexer);
30035 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
30037 matching_parens parens;
30038 if (!parens.require_open (parser))
30039 return error_mark_node;
30041 cp_parser_parse_tentatively (parser);
30042 alignas_expr = cp_parser_type_id (parser);
30044 if (!cp_parser_parse_definitely (parser))
30046 alignas_expr = cp_parser_assignment_expression (parser);
30047 if (alignas_expr == error_mark_node)
30048 cp_parser_skip_to_end_of_statement (parser);
30049 if (alignas_expr == NULL_TREE
30050 || alignas_expr == error_mark_node)
30051 return alignas_expr;
30054 alignas_expr = cxx_alignas_expr (alignas_expr);
30055 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
30057 /* Handle alignas (pack...). */
30058 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30060 cp_lexer_consume_token (parser->lexer);
30061 alignas_expr = make_pack_expansion (alignas_expr);
30064 /* Something went wrong, so don't build the attribute. */
30065 if (alignas_expr == error_mark_node)
30066 return error_mark_node;
30068 /* Missing ')' means the code cannot possibly be valid; go ahead
30069 and commit to make sure we issue a hard error. */
30070 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
30071 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
30072 cp_parser_commit_to_tentative_parse (parser);
30074 if (!parens.require_close (parser))
30075 return error_mark_node;
30077 /* Build the C++-11 representation of an 'aligned'
30078 attribute. */
30079 attributes
30080 = build_tree_list (build_tree_list (gnu_identifier,
30081 aligned_identifier), alignas_expr);
30084 return attributes;
30087 /* Parse a standard C++-11 attribute-specifier-seq.
30089 attribute-specifier-seq:
30090 attribute-specifier-seq [opt] attribute-specifier */
30092 static tree
30093 cp_parser_std_attribute_spec_seq (cp_parser *parser)
30095 tree attr_specs = NULL_TREE;
30096 tree attr_last = NULL_TREE;
30098 /* Don't create wrapper nodes within attributes: the
30099 handlers don't know how to handle them. */
30100 auto_suppress_location_wrappers sentinel;
30102 while (true)
30104 tree attr_spec = cp_parser_std_attribute_spec (parser);
30105 if (attr_spec == NULL_TREE)
30106 break;
30107 if (attr_spec == error_mark_node)
30108 return error_mark_node;
30110 if (attr_last)
30111 TREE_CHAIN (attr_last) = attr_spec;
30112 else
30113 attr_specs = attr_last = attr_spec;
30114 attr_last = tree_last (attr_last);
30117 return attr_specs;
30120 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
30121 return index of the first token after balanced-token, or N on failure. */
30123 static size_t
30124 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
30126 size_t orig_n = n;
30127 int nparens = 0, nbraces = 0, nsquares = 0;
30129 switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
30131 case CPP_PRAGMA_EOL:
30132 if (!parser->lexer->in_pragma)
30133 break;
30134 /* FALLTHRU */
30135 case CPP_EOF:
30136 /* Ran out of tokens. */
30137 return orig_n;
30138 case CPP_OPEN_PAREN:
30139 ++nparens;
30140 break;
30141 case CPP_OPEN_BRACE:
30142 ++nbraces;
30143 break;
30144 case CPP_OPEN_SQUARE:
30145 ++nsquares;
30146 break;
30147 case CPP_CLOSE_PAREN:
30148 --nparens;
30149 break;
30150 case CPP_CLOSE_BRACE:
30151 --nbraces;
30152 break;
30153 case CPP_CLOSE_SQUARE:
30154 --nsquares;
30155 break;
30156 default:
30157 break;
30159 while (nparens || nbraces || nsquares);
30160 return n;
30163 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
30164 return index of the first token after the GNU attribute tokens, or N on
30165 failure. */
30167 static size_t
30168 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
30170 while (true)
30172 if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
30173 || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
30174 || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
30175 break;
30177 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
30178 if (n2 == n + 2)
30179 break;
30180 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
30181 break;
30182 n = n2 + 1;
30184 return n;
30187 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
30188 next token), return index of the first token after the standard C++11
30189 attribute tokens, or N on failure. */
30191 static size_t
30192 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
30194 while (true)
30196 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
30197 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
30199 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
30200 if (n2 == n + 1)
30201 break;
30202 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
30203 break;
30204 n = n2 + 1;
30206 else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
30207 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
30209 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
30210 if (n2 == n + 1)
30211 break;
30212 n = n2;
30214 else
30215 break;
30217 return n;
30220 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
30221 as the next token), return index of the first token after the attribute
30222 tokens, or N on failure. */
30224 static size_t
30225 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
30227 if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
30228 return cp_parser_skip_gnu_attributes_opt (parser, n);
30229 return cp_parser_skip_std_attribute_spec_seq (parser, n);
30232 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
30233 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
30234 current value of the PEDANTIC flag, regardless of whether or not
30235 the `__extension__' keyword is present. The caller is responsible
30236 for restoring the value of the PEDANTIC flag. */
30238 static bool
30239 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
30241 /* Save the old value of the PEDANTIC flag. */
30242 *saved_pedantic = pedantic;
30244 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
30246 /* Consume the `__extension__' token. */
30247 cp_lexer_consume_token (parser->lexer);
30248 /* We're not being pedantic while the `__extension__' keyword is
30249 in effect. */
30250 pedantic = 0;
30252 return true;
30255 return false;
30258 /* Parse a label declaration.
30260 label-declaration:
30261 __label__ label-declarator-seq ;
30263 label-declarator-seq:
30264 identifier , label-declarator-seq
30265 identifier */
30267 static void
30268 cp_parser_label_declaration (cp_parser* parser)
30270 /* Look for the `__label__' keyword. */
30271 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
30273 while (true)
30275 tree identifier;
30277 /* Look for an identifier. */
30278 identifier = cp_parser_identifier (parser);
30279 /* If we failed, stop. */
30280 if (identifier == error_mark_node)
30281 break;
30282 /* Declare it as a label. */
30283 finish_label_decl (identifier);
30284 /* If the next token is a `;', stop. */
30285 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30286 break;
30287 /* Look for the `,' separating the label declarations. */
30288 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
30291 /* Look for the final `;'. */
30292 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
30295 // -------------------------------------------------------------------------- //
30296 // Concept definitions
30298 static tree
30299 cp_parser_concept_definition (cp_parser *parser)
30301 /* A concept definition is an unevaluated context. */
30302 cp_unevaluated u;
30304 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_CONCEPT));
30305 cp_lexer_consume_token (parser->lexer);
30307 cp_expr id = cp_parser_identifier (parser);
30308 if (id == error_mark_node)
30310 cp_parser_skip_to_end_of_statement (parser);
30311 cp_parser_consume_semicolon_at_end_of_statement (parser);
30312 return NULL_TREE;
30315 tree attrs = cp_parser_attributes_opt (parser);
30317 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
30319 cp_parser_skip_to_end_of_statement (parser);
30320 cp_parser_consume_semicolon_at_end_of_statement (parser);
30321 return error_mark_node;
30324 processing_constraint_expression_sentinel parsing_constraint;
30325 tree init = cp_parser_constraint_expression (parser);
30326 if (init == error_mark_node)
30327 cp_parser_skip_to_end_of_statement (parser);
30329 /* Consume the trailing ';'. Diagnose the problem if it isn't there,
30330 but continue as if it were. */
30331 cp_parser_consume_semicolon_at_end_of_statement (parser);
30333 return finish_concept_definition (id, init, attrs);
30336 // -------------------------------------------------------------------------- //
30337 // Requires Clause
30339 /* Diagnose an expression that should appear in ()'s within a requires-clause
30340 and suggest where to place those parentheses. */
30342 static void
30343 cp_parser_diagnose_ungrouped_constraint_plain (location_t loc)
30345 error_at (loc, "expression must be enclosed in parentheses");
30348 static void
30349 cp_parser_diagnose_ungrouped_constraint_rich (location_t loc)
30351 gcc_rich_location richloc (loc);
30352 richloc.add_fixit_insert_before ("(");
30353 richloc.add_fixit_insert_after (")");
30354 error_at (&richloc, "expression must be enclosed in parentheses");
30357 /* Characterizes the likely kind of expression intended by a mis-written
30358 primary constraint. */
30359 enum primary_constraint_error
30361 pce_ok,
30362 pce_maybe_operator,
30363 pce_maybe_postfix
30366 /* Returns true if the token(s) following a primary-expression in a
30367 constraint-logical-* expression would require parentheses. */
30369 static primary_constraint_error
30370 cp_parser_constraint_requires_parens (cp_parser *parser, bool lambda_p)
30372 cp_token *token = cp_lexer_peek_token (parser->lexer);
30373 switch (token->type)
30375 default:
30376 return pce_ok;
30378 case CPP_EQ:
30380 /* An equal sign may be part of the definition of a function,
30381 and not an assignment operator, when parsing the expression
30382 for a trailing requires-clause. For example:
30384 template<typename T>
30385 struct S {
30386 S() requires C<T> = default;
30389 Don't try to reparse this a binary operator. */
30390 if (cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_DELETE)
30391 || cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_DEFAULT))
30392 return pce_ok;
30394 gcc_fallthrough ();
30397 /* Arithmetic operators. */
30398 case CPP_PLUS:
30399 case CPP_MINUS:
30400 case CPP_MULT:
30401 case CPP_DIV:
30402 case CPP_MOD:
30403 /* Bitwise operators. */
30404 case CPP_AND:
30405 case CPP_OR:
30406 case CPP_XOR:
30407 case CPP_RSHIFT:
30408 case CPP_LSHIFT:
30409 /* Relational operators. */
30410 case CPP_EQ_EQ:
30411 case CPP_NOT_EQ:
30412 case CPP_LESS:
30413 case CPP_GREATER:
30414 case CPP_LESS_EQ:
30415 case CPP_GREATER_EQ:
30416 case CPP_SPACESHIP:
30417 /* Pointer-to-member. */
30418 case CPP_DOT_STAR:
30419 case CPP_DEREF_STAR:
30420 /* Assignment operators. */
30421 case CPP_PLUS_EQ:
30422 case CPP_MINUS_EQ:
30423 case CPP_MULT_EQ:
30424 case CPP_DIV_EQ:
30425 case CPP_MOD_EQ:
30426 case CPP_AND_EQ:
30427 case CPP_OR_EQ:
30428 case CPP_XOR_EQ:
30429 case CPP_RSHIFT_EQ:
30430 case CPP_LSHIFT_EQ:
30431 /* Conditional operator */
30432 case CPP_QUERY:
30433 /* Unenclosed binary or conditional operator. */
30434 return pce_maybe_operator;
30436 case CPP_OPEN_PAREN:
30438 /* A primary constraint that precedes the parameter-list of a
30439 lambda expression is followed by an open paren.
30441 []<typename T> requires C (T a, T b) { ... }
30443 Don't try to re-parse this as a postfix expression. */
30444 if (lambda_p)
30445 return pce_ok;
30447 gcc_fallthrough ();
30449 case CPP_OPEN_SQUARE:
30451 /* A primary-constraint-expression followed by a '[[' is not a
30452 postfix expression. */
30453 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE))
30454 return pce_ok;
30456 gcc_fallthrough ();
30458 case CPP_PLUS_PLUS:
30459 case CPP_MINUS_MINUS:
30460 case CPP_DOT:
30461 /* Unenclosed postfix operator. */
30462 return pce_maybe_postfix;
30464 case CPP_DEREF:
30465 /* A primary constraint that precedes the lambda-declarator of a
30466 lambda expression is followed by trailing return type.
30468 []<typename T> requires C -> void {}
30470 Don't try to re-parse this as a postfix expression in
30471 C++23 and later. In C++20 ( needs to come in between but we
30472 allow it to be omitted with pedwarn. */
30473 if (lambda_p)
30474 return pce_ok;
30475 /* Unenclosed postfix operator. */
30476 return pce_maybe_postfix;
30480 /* Returns true if the next token begins a unary expression, preceded by
30481 an operator or keyword. */
30483 static bool
30484 cp_parser_unary_constraint_requires_parens (cp_parser *parser)
30486 cp_token *token = cp_lexer_peek_token (parser->lexer);
30487 switch (token->type)
30489 case CPP_NOT:
30490 case CPP_PLUS:
30491 case CPP_MINUS:
30492 case CPP_MULT:
30493 case CPP_COMPL:
30494 case CPP_PLUS_PLUS:
30495 case CPP_MINUS_MINUS:
30496 return true;
30498 case CPP_KEYWORD:
30500 switch (token->keyword)
30502 case RID_STATCAST:
30503 case RID_DYNCAST:
30504 case RID_REINTCAST:
30505 case RID_CONSTCAST:
30506 case RID_TYPEID:
30507 case RID_SIZEOF:
30508 case RID_ALIGNOF:
30509 case RID_NOEXCEPT:
30510 case RID_NEW:
30511 case RID_DELETE:
30512 case RID_THROW:
30513 return true;
30515 default:
30516 break;
30520 default:
30521 break;
30524 return false;
30527 /* Parse a primary expression within a constraint. */
30529 static cp_expr
30530 cp_parser_constraint_primary_expression (cp_parser *parser, bool lambda_p)
30532 /* If this looks like a unary expression, parse it as such, but diagnose
30533 it as ill-formed; it requires parens. */
30534 if (cp_parser_unary_constraint_requires_parens (parser))
30536 cp_expr e = cp_parser_assignment_expression (parser, NULL, false, false);
30537 cp_parser_diagnose_ungrouped_constraint_rich (e.get_location());
30538 return e;
30541 cp_lexer_save_tokens (parser->lexer);
30542 cp_id_kind idk;
30543 location_t loc = input_location;
30544 cp_expr expr = cp_parser_primary_expression (parser,
30545 /*address_p=*/false,
30546 /*cast_p=*/false,
30547 /*template_arg_p=*/false,
30548 &idk);
30549 expr.maybe_add_location_wrapper ();
30551 primary_constraint_error pce = pce_ok;
30552 if (expr != error_mark_node)
30554 /* The primary-expression could be part of an unenclosed non-logical
30555 compound expression. */
30556 pce = cp_parser_constraint_requires_parens (parser, lambda_p);
30558 if (pce == pce_ok)
30560 cp_lexer_commit_tokens (parser->lexer);
30561 return finish_constraint_primary_expr (expr);
30564 /* Retry the parse at a lower precedence. If that succeeds, diagnose the
30565 error, but return the expression as if it were valid. */
30566 cp_lexer_rollback_tokens (parser->lexer);
30567 cp_parser_parse_tentatively (parser);
30568 if (pce == pce_maybe_operator)
30569 expr = cp_parser_assignment_expression (parser, NULL, false, false);
30570 else
30571 expr = cp_parser_simple_cast_expression (parser);
30572 if (cp_parser_parse_definitely (parser))
30574 cp_parser_diagnose_ungrouped_constraint_rich (expr.get_location());
30575 return expr;
30578 /* Otherwise, something has gone very wrong, and we can't generate a more
30579 meaningful diagnostic or recover. */
30580 cp_parser_diagnose_ungrouped_constraint_plain (loc);
30581 return error_mark_node;
30584 /* Parse a constraint-logical-and-expression.
30586 constraint-logical-and-expression:
30587 primary-expression
30588 constraint-logical-and-expression '&&' primary-expression */
30590 static cp_expr
30591 cp_parser_constraint_logical_and_expression (cp_parser *parser, bool lambda_p)
30593 cp_expr lhs = cp_parser_constraint_primary_expression (parser, lambda_p);
30594 while (cp_lexer_next_token_is (parser->lexer, CPP_AND_AND))
30596 cp_token *op = cp_lexer_consume_token (parser->lexer);
30597 tree rhs = cp_parser_constraint_primary_expression (parser, lambda_p);
30598 lhs = finish_constraint_and_expr (op->location, lhs, rhs);
30600 return lhs;
30603 /* Parse a constraint-logical-or-expression.
30605 constraint-logical-or-expression:
30606 constraint-logical-and-expression
30607 constraint-logical-or-expression '||' constraint-logical-and-expression */
30609 static cp_expr
30610 cp_parser_constraint_logical_or_expression (cp_parser *parser, bool lambda_p)
30612 cp_expr lhs = cp_parser_constraint_logical_and_expression (parser, lambda_p);
30613 while (cp_lexer_next_token_is (parser->lexer, CPP_OR_OR))
30615 cp_token *op = cp_lexer_consume_token (parser->lexer);
30616 cp_expr rhs = cp_parser_constraint_logical_and_expression (parser, lambda_p);
30617 lhs = finish_constraint_or_expr (op->location, lhs, rhs);
30619 return lhs;
30622 /* Parse the expression after a requires-clause. This has a different grammar
30623 than that in the concepts TS. */
30625 static tree
30626 cp_parser_requires_clause_expression (cp_parser *parser, bool lambda_p)
30628 processing_constraint_expression_sentinel parsing_constraint;
30629 ++processing_template_decl;
30630 cp_expr expr = cp_parser_constraint_logical_or_expression (parser, lambda_p);
30631 --processing_template_decl;
30632 if (check_for_bare_parameter_packs (expr))
30633 expr = error_mark_node;
30634 return expr;
30637 /* Parse a expression after a requires clause.
30639 constraint-expression:
30640 logical-or-expression
30642 The required logical-or-expression must be a constant expression. Note
30643 that we don't check that the expression is constepxr here. We defer until
30644 we analyze constraints and then, we only check atomic constraints. */
30646 static tree
30647 cp_parser_constraint_expression (cp_parser *parser)
30649 processing_constraint_expression_sentinel parsing_constraint;
30650 ++processing_template_decl;
30651 cp_expr expr = cp_parser_binary_expression (parser, false, true,
30652 PREC_NOT_OPERATOR, NULL);
30653 --processing_template_decl;
30654 if (check_for_bare_parameter_packs (expr))
30655 expr = error_mark_node;
30656 expr.maybe_add_location_wrapper ();
30657 return expr;
30660 /* Optionally parse a requires clause:
30662 requires-clause:
30663 `requires` constraint-logical-or-expression.
30664 [ConceptsTS]
30665 `requires constraint-expression.
30667 LAMBDA_P is true when the requires-clause is parsed before the
30668 parameter-list of a lambda-declarator. */
30670 static tree
30671 cp_parser_requires_clause_opt (cp_parser *parser, bool lambda_p)
30673 /* A requires clause is an unevaluated context. */
30674 cp_unevaluated u;
30676 cp_token *tok = cp_lexer_peek_token (parser->lexer);
30677 if (tok->keyword != RID_REQUIRES)
30679 if (!flag_concepts && tok->type == CPP_NAME
30680 && tok->u.value == ridpointers[RID_REQUIRES])
30682 error_at (cp_lexer_peek_token (parser->lexer)->location,
30683 "%<requires%> only available with "
30684 "%<-std=c++20%> or %<-fconcepts%>");
30685 /* Parse and discard the requires-clause. */
30686 cp_lexer_consume_token (parser->lexer);
30687 cp_parser_constraint_expression (parser);
30689 return NULL_TREE;
30692 cp_token *tok2 = cp_lexer_peek_nth_token (parser->lexer, 2);
30693 if (tok2->type == CPP_OPEN_BRACE)
30695 /* An opening brace following the start of a requires-clause is
30696 ill-formed; the user likely forgot the second `requires' that
30697 would start a requires-expression. */
30698 gcc_rich_location richloc (tok2->location);
30699 richloc.add_fixit_insert_after (tok->location, " requires");
30700 error_at (&richloc, "missing additional %<requires%> to start "
30701 "a requires-expression");
30702 /* Don't consume the `requires', so that it's reused as the start of a
30703 requires-expression. */
30705 else
30706 cp_lexer_consume_token (parser->lexer);
30708 if (!flag_concepts_ts)
30709 return cp_parser_requires_clause_expression (parser, lambda_p);
30710 else
30711 return cp_parser_constraint_expression (parser);
30714 /*---------------------------------------------------------------------------
30715 Requires expressions
30716 ---------------------------------------------------------------------------*/
30718 /* Parse a requires expression
30720 requirement-expression:
30721 'requires' requirement-parameter-list [opt] requirement-body */
30723 static tree
30724 cp_parser_requires_expression (cp_parser *parser)
30726 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
30727 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
30729 /* Avoid committing to outer tentative parse. */
30730 tentative_firewall firewall (parser);
30732 /* This is definitely a requires-expression. */
30733 cp_parser_commit_to_tentative_parse (parser);
30735 tree parms, reqs;
30737 /* Local parameters are delared as variables within the scope
30738 of the expression. They are not visible past the end of
30739 the expression. Expressions within the requires-expression
30740 are unevaluated. */
30741 struct scope_sentinel
30743 scope_sentinel ()
30745 ++cp_unevaluated_operand;
30746 begin_scope (sk_function_parms, NULL_TREE);
30747 current_binding_level->requires_expression = true;
30750 ~scope_sentinel ()
30752 pop_bindings_and_leave_scope ();
30753 --cp_unevaluated_operand;
30755 } s;
30757 /* Parse the optional parameter list. */
30758 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30760 parms = cp_parser_requirement_parameter_list (parser);
30761 if (parms == error_mark_node)
30762 return error_mark_node;
30764 else
30765 parms = NULL_TREE;
30767 /* Parse the requirement body. */
30768 ++processing_template_decl;
30769 reqs = cp_parser_requirement_body (parser);
30770 --processing_template_decl;
30771 if (reqs == error_mark_node)
30772 return error_mark_node;
30775 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
30776 the parm chain. */
30777 grokparms (parms, &parms);
30778 loc = make_location (loc, loc, parser->lexer);
30779 tree expr = finish_requires_expr (loc, parms, reqs);
30780 if (!processing_template_decl)
30782 /* Perform semantic processing now to diagnose any invalid types and
30783 expressions. */
30784 int saved_errorcount = errorcount;
30785 tsubst_requires_expr (expr, NULL_TREE, tf_warning_or_error, NULL_TREE);
30786 if (errorcount > saved_errorcount)
30787 return error_mark_node;
30789 return expr;
30792 /* Parse a parameterized requirement.
30794 requirement-parameter-list:
30795 '(' parameter-declaration-clause ')' */
30797 static tree
30798 cp_parser_requirement_parameter_list (cp_parser *parser)
30800 matching_parens parens;
30801 if (!parens.require_open (parser))
30802 return error_mark_node;
30804 tree parms = (cp_parser_parameter_declaration_clause
30805 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
30807 if (!parens.require_close (parser))
30808 return error_mark_node;
30810 /* Modify the declared parameters by removing their context
30811 so they don't refer to the enclosing scope and explicitly
30812 indicating that they are constraint variables. */
30813 for (tree parm = parms; parm; parm = TREE_CHAIN (parm))
30815 if (parm == void_list_node || parm == explicit_void_list_node)
30816 break;
30817 tree decl = TREE_VALUE (parm);
30818 if (decl != error_mark_node)
30820 DECL_CONTEXT (decl) = NULL_TREE;
30821 CONSTRAINT_VAR_P (decl) = true;
30825 return parms;
30828 /* Parse the body of a requirement.
30830 requirement-body:
30831 '{' requirement-list '}' */
30832 static tree
30833 cp_parser_requirement_body (cp_parser *parser)
30835 matching_braces braces;
30836 if (!braces.require_open (parser))
30837 return error_mark_node;
30839 tree reqs = cp_parser_requirement_seq (parser);
30841 if (!braces.require_close (parser))
30842 return error_mark_node;
30844 return reqs;
30847 /* Parse a sequence of requirements.
30849 requirement-seq:
30850 requirement
30851 requirement-seq requirement */
30853 static tree
30854 cp_parser_requirement_seq (cp_parser *parser)
30856 tree result = NULL_TREE;
30859 tree req = cp_parser_requirement (parser);
30860 if (req != error_mark_node)
30861 result = tree_cons (NULL_TREE, req, result);
30863 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE)
30864 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF));
30866 /* If there are no valid requirements, this is not a valid expression. */
30867 if (!result)
30868 return error_mark_node;
30870 /* Reverse the order of requirements so they are analyzed in order. */
30871 return nreverse (result);
30874 /* Parse a syntactic requirement or type requirement.
30876 requirement:
30877 simple-requirement
30878 compound-requirement
30879 type-requirement
30880 nested-requirement */
30882 static tree
30883 cp_parser_requirement (cp_parser *parser)
30885 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30886 return cp_parser_compound_requirement (parser);
30887 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
30889 /* It's probably a type-requirement. */
30890 cp_parser_parse_tentatively (parser);
30891 tree req = cp_parser_type_requirement (parser);
30892 if (cp_parser_parse_definitely (parser))
30893 return req;
30894 /* No, maybe it's something like typename T::type(); */
30895 cp_parser_parse_tentatively (parser);
30896 req = cp_parser_simple_requirement (parser);
30897 if (cp_parser_parse_definitely (parser))
30898 return req;
30899 /* Non-tentative for the error. */
30900 return cp_parser_type_requirement (parser);
30902 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
30903 return cp_parser_nested_requirement (parser);
30904 else
30905 return cp_parser_simple_requirement (parser);
30908 /* Parse a simple requirement.
30910 simple-requirement:
30911 expression ';' */
30913 static tree
30914 cp_parser_simple_requirement (cp_parser *parser)
30916 location_t start = cp_lexer_peek_token (parser->lexer)->location;
30917 cp_expr expr = cp_parser_expression (parser, NULL, false, false);
30918 if (expr == error_mark_node)
30919 cp_parser_skip_to_end_of_statement (parser);
30921 cp_parser_consume_semicolon_at_end_of_statement (parser);
30923 if (!expr || expr == error_mark_node)
30924 return error_mark_node;
30926 /* Sometimes we don't get locations, so use the cached token location
30927 as a reasonable approximation. */
30928 if (expr.get_location() == UNKNOWN_LOCATION)
30929 expr.set_location (start);
30931 for (tree t = expr; ; )
30933 if (TREE_CODE (t) == TRUTH_ANDIF_EXPR
30934 || TREE_CODE (t) == TRUTH_ORIF_EXPR)
30936 t = TREE_OPERAND (t, 0);
30937 continue;
30939 if (concept_check_p (t))
30941 gcc_rich_location richloc (get_start (start));
30942 richloc.add_fixit_insert_before (start, "requires ");
30943 warning_at (&richloc, OPT_Wmissing_requires, "testing "
30944 "if a concept-id is a valid expression; add "
30945 "%<requires%> to check satisfaction");
30947 break;
30950 return finish_simple_requirement (expr.get_location (), expr);
30953 /* Parse a type requirement
30955 type-requirement
30956 nested-name-specifier [opt] required-type-name ';'
30958 required-type-name:
30959 type-name
30960 'template' [opt] simple-template-id */
30962 static tree
30963 cp_parser_type_requirement (cp_parser *parser)
30965 cp_token *start_tok = cp_lexer_consume_token (parser->lexer);
30966 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30968 // Save the scope before parsing name specifiers.
30969 tree saved_scope = parser->scope;
30970 tree saved_object_scope = parser->object_scope;
30971 tree saved_qualifying_scope = parser->qualifying_scope;
30972 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
30973 cp_parser_nested_name_specifier_opt (parser,
30974 /*typename_keyword_p=*/true,
30975 /*check_dependency_p=*/false,
30976 /*type_p=*/true,
30977 /*is_declaration=*/false);
30979 tree type;
30980 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
30982 cp_lexer_consume_token (parser->lexer);
30983 type = cp_parser_template_id (parser,
30984 /*template_keyword_p=*/true,
30985 /*check_dependency=*/false,
30986 /*tag_type=*/none_type,
30987 /*is_declaration=*/false);
30988 type = make_typename_type (parser->scope, type, typename_type,
30989 /*complain=*/tf_error);
30991 else
30992 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
30994 if (TREE_CODE (type) == TYPE_DECL)
30995 type = TREE_TYPE (type);
30997 parser->scope = saved_scope;
30998 parser->object_scope = saved_object_scope;
30999 parser->qualifying_scope = saved_qualifying_scope;
31001 if (type == error_mark_node)
31002 cp_parser_skip_to_end_of_statement (parser);
31004 cp_parser_consume_semicolon_at_end_of_statement (parser);
31006 if (type == error_mark_node)
31007 return error_mark_node;
31009 loc = make_location (loc, start_tok->location, parser->lexer);
31010 return finish_type_requirement (loc, type);
31013 /* Parse a compound requirement
31015 compound-requirement:
31016 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
31018 static tree
31019 cp_parser_compound_requirement (cp_parser *parser)
31021 /* Parse an expression enclosed in '{ }'s. */
31022 matching_braces braces;
31023 if (!braces.require_open (parser))
31024 return error_mark_node;
31026 cp_token *expr_token = cp_lexer_peek_token (parser->lexer);
31028 tree expr = cp_parser_expression (parser, NULL, false, false);
31029 if (expr == error_mark_node)
31030 cp_parser_skip_to_closing_brace (parser);
31032 if (!braces.require_close (parser))
31034 cp_parser_skip_to_end_of_statement (parser);
31035 cp_parser_consume_semicolon_at_end_of_statement (parser);
31036 return error_mark_node;
31039 /* If the expression was invalid, skip the remainder of the requirement. */
31040 if (!expr || expr == error_mark_node)
31042 cp_parser_skip_to_end_of_statement (parser);
31043 cp_parser_consume_semicolon_at_end_of_statement (parser);
31044 return error_mark_node;
31047 /* Parse the optional noexcept. */
31048 bool noexcept_p = false;
31049 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
31051 cp_lexer_consume_token (parser->lexer);
31052 noexcept_p = true;
31055 /* Parse the optional trailing return type. */
31056 tree type = NULL_TREE;
31057 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
31059 cp_lexer_consume_token (parser->lexer);
31060 cp_token *tok = cp_lexer_peek_token (parser->lexer);
31062 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
31063 parser->in_result_type_constraint_p = true;
31064 /* C++20 allows either a type-id or a type-constraint. Parsing
31065 a type-id will subsume the parsing for a type-constraint but
31066 allow for more syntactic forms (e.g., const C<T>*). */
31067 type = cp_parser_trailing_type_id (parser);
31068 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
31069 if (type == error_mark_node)
31070 return error_mark_node;
31072 location_t type_loc = make_location (tok->location, tok->location,
31073 parser->lexer);
31075 /* Check that we haven't written something like 'const C<T>*'. */
31076 if (type_uses_auto (type))
31078 if (!is_auto (type))
31080 error_at (type_loc,
31081 "result type is not a plain type-constraint");
31082 cp_parser_consume_semicolon_at_end_of_statement (parser);
31083 return error_mark_node;
31086 else if (!flag_concepts_ts)
31087 /* P1452R2 removed the trailing-return-type option. */
31088 error_at (type_loc,
31089 "return-type-requirement is not a type-constraint");
31092 location_t loc = make_location (expr_token->location,
31093 braces.open_location (),
31094 parser->lexer);
31096 cp_parser_consume_semicolon_at_end_of_statement (parser);
31098 if (expr == error_mark_node || type == error_mark_node)
31099 return error_mark_node;
31101 return finish_compound_requirement (loc, expr, type, noexcept_p);
31104 /* Parse a nested requirement. This is the same as a requires clause.
31106 nested-requirement:
31107 requires-clause */
31109 static tree
31110 cp_parser_nested_requirement (cp_parser *parser)
31112 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
31113 cp_token *tok = cp_lexer_consume_token (parser->lexer);
31114 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31115 tree req = cp_parser_constraint_expression (parser);
31116 if (req == error_mark_node)
31117 cp_parser_skip_to_end_of_statement (parser);
31118 loc = make_location (loc, tok->location, parser->lexer);
31119 cp_parser_consume_semicolon_at_end_of_statement (parser);
31120 if (req == error_mark_node)
31121 return error_mark_node;
31122 return finish_nested_requirement (loc, req);
31125 /* Support Functions */
31127 /* Return the appropriate prefer_type argument for lookup_name based on
31128 tag_type. */
31130 static inline LOOK_want
31131 prefer_type_arg (tag_types tag_type)
31133 switch (tag_type)
31135 case none_type: return LOOK_want::NORMAL; // No preference.
31136 case scope_type: return LOOK_want::TYPE_NAMESPACE; // Type or namespace.
31137 default: return LOOK_want::TYPE; // Type only.
31141 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
31142 NAME should have one of the representations used for an
31143 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
31144 is returned. If PARSER->SCOPE is a dependent type, then a
31145 SCOPE_REF is returned.
31147 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
31148 returned; the name was already resolved when the TEMPLATE_ID_EXPR
31149 was formed. Abstractly, such entities should not be passed to this
31150 function, because they do not need to be looked up, but it is
31151 simpler to check for this special case here, rather than at the
31152 call-sites.
31154 In cases not explicitly covered above, this function returns a
31155 DECL, OVERLOAD, or baselink representing the result of the lookup.
31156 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
31157 is returned.
31159 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
31160 (e.g., "struct") that was used. In that case bindings that do not
31161 refer to types are ignored.
31163 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
31164 ignored.
31166 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
31167 are ignored.
31169 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
31170 types.
31172 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
31173 TREE_LIST of candidates if name-lookup results in an ambiguity, and
31174 NULL_TREE otherwise. */
31176 static cp_expr
31177 cp_parser_lookup_name (cp_parser *parser, tree name,
31178 enum tag_types tag_type,
31179 bool is_template,
31180 bool is_namespace,
31181 bool check_dependency,
31182 tree *ambiguous_decls,
31183 location_t name_location)
31185 tree decl;
31186 tree object_type = parser->context->object_type;
31188 /* Assume that the lookup will be unambiguous. */
31189 if (ambiguous_decls)
31190 *ambiguous_decls = NULL_TREE;
31192 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
31193 no longer valid. Note that if we are parsing tentatively, and
31194 the parse fails, OBJECT_TYPE will be automatically restored. */
31195 parser->context->object_type = NULL_TREE;
31197 if (name == error_mark_node)
31198 return error_mark_node;
31200 /* A template-id has already been resolved; there is no lookup to
31201 do. */
31202 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
31203 return name;
31204 if (BASELINK_P (name))
31206 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
31207 == TEMPLATE_ID_EXPR);
31208 return name;
31211 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
31212 it should already have been checked to make sure that the name
31213 used matches the type being destroyed. */
31214 if (TREE_CODE (name) == BIT_NOT_EXPR)
31216 tree type;
31218 /* Figure out to which type this destructor applies. */
31219 if (parser->scope)
31220 type = parser->scope;
31221 else if (object_type)
31222 type = object_type;
31223 else
31224 type = current_class_type;
31225 /* If that's not a class type, there is no destructor. */
31226 if (!type || !CLASS_TYPE_P (type))
31227 return error_mark_node;
31229 /* In a non-static member function, check implicit this->. */
31230 if (current_class_ref)
31231 return lookup_destructor (current_class_ref, parser->scope, name,
31232 tf_warning_or_error);
31234 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
31235 lazily_declare_fn (sfk_destructor, type);
31237 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
31238 return dtor;
31240 return error_mark_node;
31243 /* By this point, the NAME should be an ordinary identifier. If
31244 the id-expression was a qualified name, the qualifying scope is
31245 stored in PARSER->SCOPE at this point. */
31246 gcc_assert (identifier_p (name));
31248 /* Perform the lookup. */
31249 if (parser->scope)
31251 bool dependent_p;
31253 if (parser->scope == error_mark_node)
31254 return error_mark_node;
31256 /* If the SCOPE is dependent, the lookup must be deferred until
31257 the template is instantiated -- unless we are explicitly
31258 looking up names in uninstantiated templates. Even then, we
31259 cannot look up the name if the scope is not a class type; it
31260 might, for example, be a template type parameter. */
31261 dependent_p = (TYPE_P (parser->scope)
31262 && dependent_scope_p (parser->scope));
31263 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
31264 && dependent_p)
31265 /* Defer lookup. */
31266 decl = error_mark_node;
31267 else
31269 tree pushed_scope = NULL_TREE;
31271 /* If PARSER->SCOPE is a dependent type, then it must be a
31272 class type, and we must not be checking dependencies;
31273 otherwise, we would have processed this lookup above. So
31274 that PARSER->SCOPE is not considered a dependent base by
31275 lookup_member, we must enter the scope here. */
31276 if (dependent_p)
31277 pushed_scope = push_scope (parser->scope);
31279 /* If the PARSER->SCOPE is a template specialization, it
31280 may be instantiated during name lookup. In that case,
31281 errors may be issued. Even if we rollback the current
31282 tentative parse, those errors are valid. */
31283 decl = lookup_qualified_name (parser->scope, name,
31284 prefer_type_arg (tag_type),
31285 /*complain=*/true);
31287 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
31288 lookup result and the nested-name-specifier nominates a class C:
31289 * if the name specified after the nested-name-specifier, when
31290 looked up in C, is the injected-class-name of C (Clause 9), or
31291 * if the name specified after the nested-name-specifier is the
31292 same as the identifier or the simple-template-id's template-
31293 name in the last component of the nested-name-specifier,
31294 the name is instead considered to name the constructor of
31295 class C. [ Note: for example, the constructor is not an
31296 acceptable lookup result in an elaborated-type-specifier so
31297 the constructor would not be used in place of the
31298 injected-class-name. --end note ] Such a constructor name
31299 shall be used only in the declarator-id of a declaration that
31300 names a constructor or in a using-declaration. */
31301 if (tag_type == none_type
31302 && DECL_SELF_REFERENCE_P (decl)
31303 && same_type_p (DECL_CONTEXT (decl), parser->scope))
31304 decl = lookup_qualified_name (parser->scope, ctor_identifier,
31305 prefer_type_arg (tag_type),
31306 /*complain=*/true);
31308 if (pushed_scope)
31309 pop_scope (pushed_scope);
31312 /* If the scope is a dependent type and either we deferred lookup or
31313 we did lookup but didn't find the name, rememeber the name. */
31314 if (decl == error_mark_node && TYPE_P (parser->scope)
31315 && dependent_type_p (parser->scope))
31317 if (tag_type)
31319 tree type;
31321 /* The resolution to Core Issue 180 says that `struct
31322 A::B' should be considered a type-name, even if `A'
31323 is dependent. */
31324 type = make_typename_type (parser->scope, name, tag_type,
31325 /*complain=*/tf_error);
31326 if (type != error_mark_node)
31327 decl = TYPE_NAME (type);
31329 else if (is_template
31330 && (cp_parser_next_token_ends_template_argument_p (parser)
31331 || cp_lexer_next_token_is (parser->lexer,
31332 CPP_CLOSE_PAREN)))
31333 decl = make_unbound_class_template (parser->scope,
31334 name, NULL_TREE,
31335 /*complain=*/tf_error);
31336 else
31337 decl = build_qualified_name (/*type=*/NULL_TREE,
31338 parser->scope, name,
31339 is_template);
31341 parser->qualifying_scope = parser->scope;
31342 parser->object_scope = NULL_TREE;
31344 else if (object_type)
31346 bool dep = dependent_scope_p (object_type);
31348 /* Look up the name in the scope of the OBJECT_TYPE, unless the
31349 OBJECT_TYPE is not a class. */
31350 if (!dep && CLASS_TYPE_P (object_type))
31351 /* If the OBJECT_TYPE is a template specialization, it may
31352 be instantiated during name lookup. In that case, errors
31353 may be issued. Even if we rollback the current tentative
31354 parse, those errors are valid. */
31355 decl = lookup_member (object_type,
31356 name,
31357 /*protect=*/0,
31358 /*prefer_type=*/tag_type != none_type,
31359 tf_warning_or_error);
31360 else
31361 decl = NULL_TREE;
31363 if (!decl)
31364 /* Look it up in the enclosing context. DR 141: When looking for a
31365 template-name after -> or ., only consider class templates. */
31366 decl = lookup_name (name, is_namespace ? LOOK_want::NAMESPACE
31367 /* DR 141: When looking in the
31368 current enclosing context for a
31369 template-name after -> or ., only
31370 consider class templates. */
31371 : is_template ? LOOK_want::TYPE
31372 : prefer_type_arg (tag_type));
31374 /* If we did unqualified lookup of a dependent member-qualified name and
31375 found something, do we want to use it? P1787 clarified that we need
31376 to look in the object scope first even if it's dependent, but for now
31377 let's still use it in some cases.
31378 FIXME remember unqualified lookup result to use if member lookup fails
31379 at instantiation time. */
31380 if (decl && dep && is_template)
31382 saved_token_sentinel toks (parser->lexer, STS_ROLLBACK);
31383 /* Only use the unqualified class template lookup if we're actually
31384 looking at a template arg list. */
31385 if (!cp_parser_skip_entire_template_parameter_list (parser))
31386 decl = NULL_TREE;
31389 /* If we know we're looking for a type (e.g. A in p->A::x),
31390 mock up a typename. */
31391 if (!decl && object_type && tag_type != none_type
31392 && dependentish_scope_p (object_type))
31394 tree type = build_typename_type (object_type, name, name,
31395 typename_type);
31396 decl = TYPE_NAME (type);
31399 parser->object_scope = object_type;
31400 parser->qualifying_scope = NULL_TREE;
31402 else
31404 decl = lookup_name (name, is_namespace ? LOOK_want::NAMESPACE
31405 : prefer_type_arg (tag_type));
31406 parser->qualifying_scope = NULL_TREE;
31407 parser->object_scope = NULL_TREE;
31410 /* If the lookup failed, let our caller know. */
31411 if (!decl || decl == error_mark_node)
31412 return error_mark_node;
31414 /* If we have resolved the name of a member declaration, check to
31415 see if the declaration is accessible. When the name resolves to
31416 set of overloaded functions, accessibility is checked when
31417 overload resolution is done. If we have a TREE_LIST, then the lookup
31418 is either ambiguous or it found multiple injected-class-names, the
31419 accessibility of which is trivially satisfied.
31421 During an explicit instantiation, access is not checked at all,
31422 as per [temp.explicit]. */
31423 if (DECL_P (decl))
31424 check_accessibility_of_qualified_id (decl, object_type, parser->scope,
31425 tf_warning_or_error);
31427 /* Pull out the template from an injected-class-name (or multiple). */
31428 if (is_template)
31429 decl = maybe_get_template_decl_from_type_decl (decl);
31431 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
31432 if (TREE_CODE (decl) == TREE_LIST)
31434 if (ambiguous_decls)
31435 *ambiguous_decls = decl;
31436 /* The error message we have to print is too complicated for
31437 cp_parser_error, so we incorporate its actions directly. */
31438 if (!cp_parser_simulate_error (parser))
31440 error_at (name_location, "reference to %qD is ambiguous",
31441 name);
31442 print_candidates (decl);
31444 return error_mark_node;
31447 gcc_assert (DECL_P (decl)
31448 || TREE_CODE (decl) == OVERLOAD
31449 || TREE_CODE (decl) == SCOPE_REF
31450 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
31451 || BASELINK_P (decl));
31453 maybe_record_typedef_use (decl);
31455 return cp_expr (decl, name_location);
31458 /* Like cp_parser_lookup_name, but for use in the typical case where
31459 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
31460 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
31462 static tree
31463 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
31465 return cp_parser_lookup_name (parser, name,
31466 none_type,
31467 /*is_template=*/false,
31468 /*is_namespace=*/false,
31469 /*check_dependency=*/true,
31470 /*ambiguous_decls=*/NULL,
31471 location);
31474 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
31475 the current context, return the TYPE_DECL. If TAG_NAME_P is
31476 true, the DECL indicates the class being defined in a class-head,
31477 or declared in an elaborated-type-specifier.
31479 Otherwise, return DECL. */
31481 static tree
31482 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
31484 /* If the TEMPLATE_DECL is being declared as part of a class-head,
31485 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
31487 struct A {
31488 template <typename T> struct B;
31491 template <typename T> struct A::B {};
31493 Similarly, in an elaborated-type-specifier:
31495 namespace N { struct X{}; }
31497 struct A {
31498 template <typename T> friend struct N::X;
31501 However, if the DECL refers to a class type, and we are in
31502 the scope of the class, then the name lookup automatically
31503 finds the TYPE_DECL created by build_self_reference rather
31504 than a TEMPLATE_DECL. For example, in:
31506 template <class T> struct S {
31507 S s;
31510 there is no need to handle such case. */
31512 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
31513 return DECL_TEMPLATE_RESULT (decl);
31515 return decl;
31518 /* If too many, or too few, template-parameter lists apply to the
31519 declarator, issue an error message. Returns TRUE if all went well,
31520 and FALSE otherwise. */
31522 static bool
31523 cp_parser_check_declarator_template_parameters (cp_parser* parser,
31524 cp_declarator *declarator,
31525 location_t declarator_location)
31527 switch (declarator->kind)
31529 case cdk_id:
31531 unsigned num_templates = 0;
31532 tree scope = declarator->u.id.qualifying_scope;
31533 bool template_id_p = false;
31535 if (scope)
31536 num_templates = num_template_headers_for_class (scope);
31537 else if (TREE_CODE (declarator->u.id.unqualified_name)
31538 == TEMPLATE_ID_EXPR)
31540 /* If the DECLARATOR has the form `X<y>' then it uses one
31541 additional level of template parameters. */
31542 ++num_templates;
31543 template_id_p = true;
31546 return cp_parser_check_template_parameters
31547 (parser, num_templates, template_id_p, declarator_location,
31548 declarator);
31551 case cdk_function:
31552 case cdk_array:
31553 case cdk_pointer:
31554 case cdk_reference:
31555 case cdk_ptrmem:
31556 return (cp_parser_check_declarator_template_parameters
31557 (parser, declarator->declarator, declarator_location));
31559 case cdk_decomp:
31560 case cdk_error:
31561 return true;
31563 default:
31564 gcc_unreachable ();
31566 return false;
31569 /* NUM_TEMPLATES were used in the current declaration. If that is
31570 invalid, return FALSE and issue an error messages. Otherwise,
31571 return TRUE. If DECLARATOR is non-NULL, then we are checking a
31572 declarator and we can print more accurate diagnostics. */
31574 static bool
31575 cp_parser_check_template_parameters (cp_parser* parser,
31576 unsigned num_templates,
31577 bool template_id_p,
31578 location_t location,
31579 cp_declarator *declarator)
31581 /* If there are the same number of template classes and parameter
31582 lists, that's OK. */
31583 if (parser->num_template_parameter_lists == num_templates)
31584 return true;
31585 /* If there are more, but only one more, and the name ends in an identifier,
31586 then we are declaring a primary template. That's OK too. */
31587 if (!template_id_p
31588 && parser->num_template_parameter_lists == num_templates + 1)
31589 return true;
31591 if (cp_parser_simulate_error (parser))
31592 return false;
31594 /* If there are more template classes than parameter lists, we have
31595 something like:
31597 template <class T> void S<T>::R<T>::f (); */
31598 if (parser->num_template_parameter_lists < num_templates)
31600 if (declarator && !current_function_decl)
31601 error_at (location, "specializing member %<%T::%E%> "
31602 "requires %<template<>%> syntax",
31603 declarator->u.id.qualifying_scope,
31604 declarator->u.id.unqualified_name);
31605 else if (declarator)
31606 error_at (location, "invalid declaration of %<%T::%E%>",
31607 declarator->u.id.qualifying_scope,
31608 declarator->u.id.unqualified_name);
31609 else
31610 error_at (location, "too few template-parameter-lists");
31611 return false;
31613 /* Otherwise, there are too many template parameter lists. We have
31614 something like:
31616 template <class T> template <class U> void S::f(); */
31617 error_at (location, "too many template-parameter-lists");
31618 return false;
31621 /* Parse an optional `::' token indicating that the following name is
31622 from the global namespace. If so, PARSER->SCOPE is set to the
31623 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
31624 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
31625 Returns the new value of PARSER->SCOPE, if the `::' token is
31626 present, and NULL_TREE otherwise. */
31628 static tree
31629 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
31631 cp_token *token;
31633 /* Peek at the next token. */
31634 token = cp_lexer_peek_token (parser->lexer);
31635 /* If we're looking at a `::' token then we're starting from the
31636 global namespace, not our current location. */
31637 if (token->type == CPP_SCOPE)
31639 /* Consume the `::' token. */
31640 cp_lexer_consume_token (parser->lexer);
31641 /* Set the SCOPE so that we know where to start the lookup. */
31642 parser->scope = global_namespace;
31643 parser->qualifying_scope = global_namespace;
31644 parser->object_scope = NULL_TREE;
31646 return parser->scope;
31648 else if (!current_scope_valid_p)
31650 parser->scope = NULL_TREE;
31651 parser->qualifying_scope = NULL_TREE;
31652 parser->object_scope = NULL_TREE;
31655 return NULL_TREE;
31658 /* Returns TRUE if the upcoming token sequence is the start of a
31659 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
31660 declarator is preceded by the `friend' specifier. The parser flags FLAGS
31661 is used to control type-specifier parsing. */
31663 static bool
31664 cp_parser_constructor_declarator_p (cp_parser *parser, cp_parser_flags flags,
31665 bool friend_p)
31667 bool constructor_p;
31668 bool outside_class_specifier_p;
31669 tree nested_name_specifier;
31670 cp_token *next_token;
31672 /* The common case is that this is not a constructor declarator, so
31673 try to avoid doing lots of work if at all possible. It's not
31674 valid declare a constructor at function scope. */
31675 if (parser->in_function_body)
31676 return false;
31677 /* And only certain tokens can begin a constructor declarator. */
31678 next_token = cp_lexer_peek_token (parser->lexer);
31679 if (next_token->type != CPP_NAME
31680 && next_token->type != CPP_SCOPE
31681 && next_token->type != CPP_NESTED_NAME_SPECIFIER
31682 /* DR 2237 (C++20 only): A simple-template-id is no longer valid as the
31683 declarator-id of a constructor or destructor. */
31684 && (next_token->type != CPP_TEMPLATE_ID || cxx_dialect >= cxx20))
31685 return false;
31687 /* Parse tentatively; we are going to roll back all of the tokens
31688 consumed here. */
31689 cp_parser_parse_tentatively (parser);
31690 /* Assume that we are looking at a constructor declarator. */
31691 constructor_p = true;
31693 /* Look for the optional `::' operator. */
31694 cp_parser_global_scope_opt (parser,
31695 /*current_scope_valid_p=*/false);
31696 /* Look for the nested-name-specifier. */
31697 nested_name_specifier
31698 = (cp_parser_nested_name_specifier_opt (parser,
31699 /*typename_keyword_p=*/false,
31700 /*check_dependency_p=*/false,
31701 /*type_p=*/false,
31702 /*is_declaration=*/false));
31704 /* Resolve the TYPENAME_TYPE, because the call above didn't do it. */
31705 if (nested_name_specifier
31706 && TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
31708 tree s = resolve_typename_type (nested_name_specifier,
31709 /*only_current_p=*/false);
31710 if (TREE_CODE (s) != TYPENAME_TYPE)
31711 nested_name_specifier = s;
31714 outside_class_specifier_p = (!at_class_scope_p ()
31715 || !TYPE_BEING_DEFINED (current_class_type)
31716 || friend_p);
31718 /* Outside of a class-specifier, there must be a
31719 nested-name-specifier. Except in C++17 mode, where we
31720 might be declaring a guiding declaration. */
31721 if (!nested_name_specifier && outside_class_specifier_p
31722 && cxx_dialect < cxx17)
31723 constructor_p = false;
31724 else if (nested_name_specifier == error_mark_node)
31725 constructor_p = false;
31727 /* If we have a class scope, this is easy; DR 147 says that S::S always
31728 names the constructor, and no other qualified name could. */
31729 if (constructor_p && nested_name_specifier
31730 && CLASS_TYPE_P (nested_name_specifier))
31732 tree id = cp_parser_unqualified_id (parser,
31733 /*template_keyword_p=*/false,
31734 /*check_dependency_p=*/false,
31735 /*declarator_p=*/true,
31736 /*optional_p=*/false);
31737 if (is_overloaded_fn (id))
31738 id = DECL_NAME (get_first_fn (id));
31739 if (!constructor_name_p (id, nested_name_specifier))
31740 constructor_p = false;
31742 /* If we still think that this might be a constructor-declarator,
31743 look for a class-name. */
31744 else if (constructor_p)
31746 /* If we have:
31748 template <typename T> struct S {
31749 S();
31752 we must recognize that the nested `S' names a class. */
31753 if (cxx_dialect >= cxx17)
31754 cp_parser_parse_tentatively (parser);
31756 tree type_decl;
31757 type_decl = cp_parser_class_name (parser,
31758 /*typename_keyword_p=*/false,
31759 /*template_keyword_p=*/false,
31760 none_type,
31761 /*check_dependency_p=*/false,
31762 /*class_head_p=*/false,
31763 /*is_declaration=*/false);
31765 if (cxx_dialect >= cxx17
31766 && !cp_parser_parse_definitely (parser))
31768 type_decl = NULL_TREE;
31769 tree tmpl = cp_parser_template_name (parser,
31770 /*template_keyword*/false,
31771 /*check_dependency_p*/false,
31772 /*is_declaration*/false,
31773 none_type,
31774 /*is_identifier*/NULL);
31775 if (DECL_CLASS_TEMPLATE_P (tmpl)
31776 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
31777 /* It's a deduction guide, return true. */;
31778 else
31779 cp_parser_simulate_error (parser);
31782 /* If there was no class-name, then this is not a constructor.
31783 Otherwise, if we are in a class-specifier and we aren't
31784 handling a friend declaration, check that its type matches
31785 current_class_type (c++/38313). Note: error_mark_node
31786 is left alone for error recovery purposes. */
31787 constructor_p = (!cp_parser_error_occurred (parser)
31788 && (outside_class_specifier_p
31789 || type_decl == NULL_TREE
31790 || type_decl == error_mark_node
31791 || same_type_p (current_class_type,
31792 TREE_TYPE (type_decl))));
31794 /* If we're still considering a constructor, we have to see a `(',
31795 to begin the parameter-declaration-clause, followed by either a
31796 `)', an `...', or a decl-specifier. We need to check for a
31797 type-specifier to avoid being fooled into thinking that:
31799 S (f) (int);
31801 is a constructor. (It is actually a function named `f' that
31802 takes one parameter (of type `int') and returns a value of type
31803 `S'. */
31804 if (constructor_p
31805 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31806 constructor_p = false;
31808 if (constructor_p
31809 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
31810 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
31811 /* A parameter declaration begins with a decl-specifier,
31812 which is either the "attribute" keyword, a storage class
31813 specifier, or (usually) a type-specifier. */
31814 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer)
31815 /* GNU attributes can actually appear both at the start of
31816 a parameter and parenthesized declarator.
31817 S (__attribute__((unused)) int);
31818 is a constructor, but
31819 S (__attribute__((unused)) foo) (int);
31820 is a function declaration. [[attribute]] can appear in the
31821 first form too, but not in the second form. */
31822 && !cp_next_tokens_can_be_std_attribute_p (parser))
31824 tree type;
31825 tree pushed_scope = NULL_TREE;
31826 unsigned saved_num_template_parameter_lists;
31828 if (cp_parser_allow_gnu_extensions_p (parser)
31829 && cp_next_tokens_can_be_gnu_attribute_p (parser))
31831 unsigned int n = cp_parser_skip_gnu_attributes_opt (parser, 1);
31832 while (--n)
31833 cp_lexer_consume_token (parser->lexer);
31836 /* Names appearing in the type-specifier should be looked up
31837 in the scope of the class. */
31838 if (current_class_type)
31839 type = NULL_TREE;
31840 else if (type_decl)
31842 type = TREE_TYPE (type_decl);
31843 if (TREE_CODE (type) == TYPENAME_TYPE)
31845 type = resolve_typename_type (type,
31846 /*only_current_p=*/false);
31847 if (TREE_CODE (type) == TYPENAME_TYPE)
31849 cp_parser_abort_tentative_parse (parser);
31850 return false;
31853 pushed_scope = push_scope (type);
31856 /* Inside the constructor parameter list, surrounding
31857 template-parameter-lists do not apply. */
31858 saved_num_template_parameter_lists
31859 = parser->num_template_parameter_lists;
31860 parser->num_template_parameter_lists = 0;
31862 /* Look for the type-specifier. It's not optional, but its typename
31863 might be. Unless this is a friend declaration; we don't want to
31864 treat
31866 friend S (T::fn)(int);
31868 as a constructor, but with P0634, we might assume a type when
31869 looking for the type-specifier. It is actually a function named
31870 `T::fn' that takes one parameter (of type `int') and returns a
31871 value of type `S'. Constructors can be friends, but they must
31872 use a qualified name.
31874 Parse with an empty set of declaration specifiers since we're
31875 trying to match a decl-specifier-seq of the first parameter.
31876 This must be non-null so that cp_parser_simple_type_specifier
31877 will recognize a constrained placeholder type such as:
31878 'C<int> auto' where C is a type concept. */
31879 cp_decl_specifier_seq ctor_specs;
31880 clear_decl_specs (&ctor_specs);
31881 cp_parser_type_specifier (parser,
31882 (friend_p ? CP_PARSER_FLAGS_NONE
31883 : (flags & ~CP_PARSER_FLAGS_OPTIONAL)),
31884 /*decl_specs=*/&ctor_specs,
31885 /*is_declarator=*/true,
31886 /*declares_class_or_enum=*/NULL,
31887 /*is_cv_qualifier=*/NULL);
31889 parser->num_template_parameter_lists
31890 = saved_num_template_parameter_lists;
31892 /* Leave the scope of the class. */
31893 if (pushed_scope)
31894 pop_scope (pushed_scope);
31896 constructor_p = !cp_parser_error_occurred (parser);
31900 /* We did not really want to consume any tokens. */
31901 cp_parser_abort_tentative_parse (parser);
31903 return constructor_p;
31906 /* Parse the definition of the function given by the DECL_SPECIFIERS,
31907 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
31908 they must be performed once we are in the scope of the function.
31910 Returns the function defined. */
31912 static tree
31913 cp_parser_function_definition_from_specifiers_and_declarator
31914 (cp_parser* parser,
31915 cp_decl_specifier_seq *decl_specifiers,
31916 tree attributes,
31917 const cp_declarator *declarator)
31919 tree fn;
31920 bool success_p;
31922 /* Begin the function-definition. */
31923 success_p = start_function (decl_specifiers, declarator, attributes);
31925 /* The things we're about to see are not directly qualified by any
31926 template headers we've seen thus far. */
31927 reset_specialization ();
31929 /* If there were names looked up in the decl-specifier-seq that we
31930 did not check, check them now. We must wait until we are in the
31931 scope of the function to perform the checks, since the function
31932 might be a friend. */
31933 perform_deferred_access_checks (tf_warning_or_error);
31935 if (success_p)
31937 cp_finalize_omp_declare_simd (parser, current_function_decl);
31938 parser->omp_declare_simd = NULL;
31939 cp_finalize_oacc_routine (parser, current_function_decl, true);
31940 parser->oacc_routine = NULL;
31943 if (!success_p)
31945 /* Skip the entire function. */
31946 cp_parser_skip_to_end_of_block_or_statement (parser);
31947 fn = error_mark_node;
31949 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
31951 /* Seen already, skip it. An error message has already been output. */
31952 cp_parser_skip_to_end_of_block_or_statement (parser);
31953 fn = current_function_decl;
31954 current_function_decl = NULL_TREE;
31955 /* If this is a function from a class, pop the nested class. */
31956 if (current_class_name)
31957 pop_nested_class ();
31959 else
31961 auto_timevar tv (DECL_DECLARED_INLINE_P (current_function_decl)
31962 ? TV_PARSE_INLINE : TV_PARSE_FUNC);
31963 fn = cp_parser_function_definition_after_declarator (parser,
31964 /*inline_p=*/false);
31967 return fn;
31970 /* Parse the part of a function-definition that follows the
31971 declarator. INLINE_P is TRUE iff this function is an inline
31972 function defined within a class-specifier.
31974 Returns the function defined. */
31976 static tree
31977 cp_parser_function_definition_after_declarator (cp_parser* parser,
31978 bool inline_p)
31980 tree fn;
31981 bool saved_in_unbraced_linkage_specification_p;
31982 bool saved_in_function_body;
31983 unsigned saved_num_template_parameter_lists;
31984 cp_token *token;
31985 bool fully_implicit_function_template_p
31986 = parser->fully_implicit_function_template_p;
31987 parser->fully_implicit_function_template_p = false;
31988 tree implicit_template_parms
31989 = parser->implicit_template_parms;
31990 parser->implicit_template_parms = 0;
31991 cp_binding_level* implicit_template_scope
31992 = parser->implicit_template_scope;
31993 parser->implicit_template_scope = 0;
31995 saved_in_function_body = parser->in_function_body;
31996 parser->in_function_body = true;
31997 /* If the next token is `return', then the code may be trying to
31998 make use of the "named return value" extension that G++ used to
31999 support. */
32000 token = cp_lexer_peek_token (parser->lexer);
32001 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
32003 /* Consume the `return' keyword. */
32004 cp_lexer_consume_token (parser->lexer);
32005 /* Look for the identifier that indicates what value is to be
32006 returned. */
32007 cp_parser_identifier (parser);
32008 /* Issue an error message. */
32009 error_at (token->location,
32010 "named return values are no longer supported");
32011 /* Skip tokens until we reach the start of the function body. */
32012 while (true)
32014 cp_token *token = cp_lexer_peek_token (parser->lexer);
32015 if (token->type == CPP_OPEN_BRACE
32016 || token->type == CPP_EOF
32017 || token->type == CPP_PRAGMA_EOL)
32018 break;
32019 cp_lexer_consume_token (parser->lexer);
32022 /* The `extern' in `extern "C" void f () { ... }' does not apply to
32023 anything declared inside `f'. */
32024 saved_in_unbraced_linkage_specification_p
32025 = parser->in_unbraced_linkage_specification_p;
32026 parser->in_unbraced_linkage_specification_p = false;
32027 /* Inside the function, surrounding template-parameter-lists do not
32028 apply. */
32029 saved_num_template_parameter_lists
32030 = parser->num_template_parameter_lists;
32031 parser->num_template_parameter_lists = 0;
32033 /* If the next token is `try', `__transaction_atomic', or
32034 `__transaction_relaxed`, then we are looking at either function-try-block
32035 or function-transaction-block. Note that all of these include the
32036 function-body. */
32037 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
32038 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
32039 else if (cp_lexer_next_token_is_keyword (parser->lexer,
32040 RID_TRANSACTION_RELAXED))
32041 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
32042 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
32043 cp_parser_function_try_block (parser);
32044 else
32045 cp_parser_ctor_initializer_opt_and_function_body
32046 (parser, /*in_function_try_block=*/false);
32048 /* Finish the function. */
32049 fn = finish_function (inline_p);
32051 if (modules_p ()
32052 && !inline_p
32053 && TYPE_P (DECL_CONTEXT (fn))
32054 && (DECL_DECLARED_INLINE_P (fn)
32055 || processing_template_decl))
32056 set_defining_module (fn);
32058 /* Generate code for it, if necessary. */
32059 expand_or_defer_fn (fn);
32061 /* Restore the saved values. */
32062 parser->in_unbraced_linkage_specification_p
32063 = saved_in_unbraced_linkage_specification_p;
32064 parser->num_template_parameter_lists
32065 = saved_num_template_parameter_lists;
32066 parser->in_function_body = saved_in_function_body;
32068 parser->fully_implicit_function_template_p
32069 = fully_implicit_function_template_p;
32070 parser->implicit_template_parms
32071 = implicit_template_parms;
32072 parser->implicit_template_scope
32073 = implicit_template_scope;
32075 if (parser->fully_implicit_function_template_p)
32076 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
32078 return fn;
32081 /* Parse a template-declaration body (following argument list). */
32083 static void
32084 cp_parser_template_declaration_after_parameters (cp_parser* parser,
32085 tree parameter_list,
32086 bool member_p)
32088 tree decl = NULL_TREE;
32089 bool friend_p = false;
32091 /* We just processed one more parameter list. */
32092 ++parser->num_template_parameter_lists;
32094 /* Get the deferred access checks from the parameter list. These
32095 will be checked once we know what is being declared, as for a
32096 member template the checks must be performed in the scope of the
32097 class containing the member. */
32098 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
32100 /* Tentatively parse for a new template parameter list, which can either be
32101 the template keyword or a template introduction. */
32102 if (cp_parser_template_declaration_after_export (parser, member_p))
32103 /* OK */;
32104 else if (cxx_dialect >= cxx11
32105 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
32106 decl = cp_parser_alias_declaration (parser);
32107 else if (flag_concepts
32108 && cp_lexer_next_token_is_keyword (parser->lexer, RID_CONCEPT)
32109 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
32110 /* -fconcept-ts 'concept bool' syntax is handled below, in
32111 cp_parser_single_declaration. */
32112 decl = cp_parser_concept_definition (parser);
32113 else
32115 cp_token *token = cp_lexer_peek_token (parser->lexer);
32116 decl = cp_parser_single_declaration (parser,
32117 checks,
32118 member_p,
32119 /*explicit_specialization_p=*/false,
32120 &friend_p);
32122 /* If this is a member template declaration, let the front
32123 end know. */
32124 if (member_p && !friend_p && decl)
32126 if (TREE_CODE (decl) == TYPE_DECL)
32127 cp_parser_check_access_in_redeclaration (decl, token->location);
32129 decl = finish_member_template_decl (decl);
32131 else if (friend_p && decl
32132 && DECL_DECLARES_TYPE_P (decl))
32133 make_friend_class (current_class_type, TREE_TYPE (decl),
32134 /*complain=*/true);
32136 /* We are done with the current parameter list. */
32137 --parser->num_template_parameter_lists;
32139 pop_deferring_access_checks ();
32141 /* Finish up. */
32142 finish_template_decl (parameter_list);
32144 /* Check the template arguments for a literal operator template. */
32145 if (decl
32146 && DECL_DECLARES_FUNCTION_P (decl)
32147 && UDLIT_OPER_P (DECL_NAME (decl)))
32149 bool ok = true;
32150 if (parameter_list == NULL_TREE)
32151 ok = false;
32152 else
32154 int num_parms = TREE_VEC_LENGTH (parameter_list);
32155 if (num_parms == 1)
32157 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
32158 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
32159 if (TREE_CODE (parm) != PARM_DECL)
32160 ok = false;
32161 else if (MAYBE_CLASS_TYPE_P (TREE_TYPE (parm))
32162 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
32163 /* OK, C++20 string literal operator template. We don't need
32164 to warn in lower dialects here because we will have already
32165 warned about the template parameter. */;
32166 else if (TREE_TYPE (parm) != char_type_node
32167 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
32168 ok = false;
32170 else if (num_parms == 2 && cxx_dialect >= cxx14)
32172 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
32173 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
32174 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
32175 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
32176 if (TREE_CODE (parm) != PARM_DECL
32177 || TREE_TYPE (parm) != TREE_TYPE (type)
32178 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
32179 ok = false;
32180 else
32181 /* http://cplusplus.github.io/EWG/ewg-active.html#66 */
32182 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
32183 "ISO C++ did not adopt string literal operator templa"
32184 "tes taking an argument pack of characters");
32186 else
32187 ok = false;
32189 if (!ok)
32191 if (cxx_dialect > cxx17)
32192 error_at (DECL_SOURCE_LOCATION (decl), "literal operator "
32193 "template %qD has invalid parameter list; expected "
32194 "non-type template parameter pack %<<char...>%> or "
32195 "single non-type parameter of class type",
32196 decl);
32197 else
32198 error_at (DECL_SOURCE_LOCATION (decl), "literal operator "
32199 "template %qD has invalid parameter list; expected "
32200 "non-type template parameter pack %<<char...>%>",
32201 decl);
32205 /* Register member declarations. */
32206 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
32207 finish_member_declaration (decl);
32208 /* If DECL is a function template, we must return to parse it later.
32209 (Even though there is no definition, there might be default
32210 arguments that need handling.) */
32211 if (member_p && decl
32212 && DECL_DECLARES_FUNCTION_P (decl))
32213 vec_safe_push (unparsed_funs_with_definitions, decl);
32216 /* Parse a template introduction header for a template-declaration. Returns
32217 false if tentative parse fails. */
32219 static bool
32220 cp_parser_template_introduction (cp_parser* parser, bool member_p)
32222 cp_parser_parse_tentatively (parser);
32224 tree saved_scope = parser->scope;
32225 tree saved_object_scope = parser->object_scope;
32226 tree saved_qualifying_scope = parser->qualifying_scope;
32227 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32229 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
32231 /* In classes don't parse valid unnamed bitfields as invalid
32232 template introductions. */
32233 if (member_p)
32234 parser->colon_corrects_to_scope_p = false;
32236 /* Look for the optional `::' operator. */
32237 cp_parser_global_scope_opt (parser,
32238 /*current_scope_valid_p=*/false);
32239 /* Look for the nested-name-specifier. */
32240 cp_parser_nested_name_specifier_opt (parser,
32241 /*typename_keyword_p=*/false,
32242 /*check_dependency_p=*/true,
32243 /*type_p=*/false,
32244 /*is_declaration=*/false);
32246 cp_token *token = cp_lexer_peek_token (parser->lexer);
32247 tree concept_name = cp_parser_identifier (parser);
32249 /* Look up the concept for which we will be matching
32250 template parameters. */
32251 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
32252 token->location);
32253 parser->scope = saved_scope;
32254 parser->object_scope = saved_object_scope;
32255 parser->qualifying_scope = saved_qualifying_scope;
32256 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32258 if (concept_name == error_mark_node
32259 || (seen_error () && !concept_definition_p (tmpl_decl)))
32260 cp_parser_simulate_error (parser);
32262 /* Look for opening brace for introduction. */
32263 matching_braces braces;
32264 braces.require_open (parser);
32265 location_t open_loc = input_location;
32267 if (!cp_parser_parse_definitely (parser))
32268 return false;
32270 push_deferring_access_checks (dk_deferred);
32272 /* Build vector of placeholder parameters and grab
32273 matching identifiers. */
32274 tree introduction_list = cp_parser_introduction_list (parser);
32276 /* Look for closing brace for introduction. */
32277 if (!braces.require_close (parser))
32278 return true;
32280 /* The introduction-list shall not be empty. */
32281 int nargs = TREE_VEC_LENGTH (introduction_list);
32282 if (nargs == 0)
32284 /* In cp_parser_introduction_list we have already issued an error. */
32285 return true;
32288 if (tmpl_decl == error_mark_node)
32290 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
32291 token->location);
32292 return true;
32295 /* Build and associate the constraint. */
32296 location_t introduction_loc = make_location (open_loc,
32297 start_token->location,
32298 parser->lexer);
32299 tree parms = finish_template_introduction (tmpl_decl,
32300 introduction_list,
32301 introduction_loc);
32302 if (parms && parms != error_mark_node)
32304 if (!flag_concepts_ts)
32305 pedwarn (introduction_loc, 0, "template-introductions"
32306 " are not part of C++20 concepts; use %qs to enable",
32307 "-fconcepts-ts");
32309 cp_parser_template_declaration_after_parameters (parser, parms,
32310 member_p);
32311 return true;
32314 if (parms == NULL_TREE)
32315 error_at (token->location, "no matching concept for template-introduction");
32317 return true;
32320 /* Parse a normal template-declaration following the template keyword. */
32322 static void
32323 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
32325 tree parameter_list;
32326 bool need_lang_pop;
32327 location_t location = input_location;
32329 /* Look for the `<' token. */
32330 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
32331 return;
32332 if (at_class_scope_p () && current_function_decl)
32334 /* 14.5.2.2 [temp.mem]
32336 A local class shall not have member templates. */
32337 error_at (location,
32338 "invalid declaration of member template in local class");
32339 cp_parser_skip_to_end_of_block_or_statement (parser);
32340 return;
32342 /* [temp]
32344 A template ... shall not have C linkage. */
32345 if (current_lang_name == lang_name_c)
32347 error_at (location, "template with C linkage");
32348 maybe_show_extern_c_location ();
32349 /* Give it C++ linkage to avoid confusing other parts of the
32350 front end. */
32351 push_lang_context (lang_name_cplusplus);
32352 need_lang_pop = true;
32354 else
32355 need_lang_pop = false;
32357 /* We cannot perform access checks on the template parameter
32358 declarations until we know what is being declared, just as we
32359 cannot check the decl-specifier list. */
32360 push_deferring_access_checks (dk_deferred);
32362 /* If the next token is `>', then we have an invalid
32363 specialization. Rather than complain about an invalid template
32364 parameter, issue an error message here. */
32365 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
32367 cp_parser_error (parser, "invalid explicit specialization");
32368 begin_specialization ();
32369 parameter_list = NULL_TREE;
32371 else
32373 /* Parse the template parameters. */
32374 parameter_list = cp_parser_template_parameter_list (parser);
32377 /* Look for the `>'. */
32378 cp_parser_require_end_of_template_parameter_list (parser);
32380 /* Manage template requirements */
32381 if (flag_concepts)
32383 tree reqs = get_shorthand_constraints (current_template_parms);
32384 if (tree treqs = cp_parser_requires_clause_opt (parser, false))
32385 reqs = combine_constraint_expressions (reqs, treqs);
32386 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
32389 cp_parser_template_declaration_after_parameters (parser, parameter_list,
32390 member_p);
32392 /* For the erroneous case of a template with C linkage, we pushed an
32393 implicit C++ linkage scope; exit that scope now. */
32394 if (need_lang_pop)
32395 pop_lang_context ();
32398 /* Parse a template-declaration, assuming that the `export' (and
32399 `extern') keywords, if present, has already been scanned. MEMBER_P
32400 is as for cp_parser_template_declaration. */
32402 static bool
32403 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
32405 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
32407 cp_lexer_consume_token (parser->lexer);
32408 cp_parser_explicit_template_declaration (parser, member_p);
32409 return true;
32411 else if (flag_concepts)
32412 return cp_parser_template_introduction (parser, member_p);
32414 return false;
32417 /* Perform the deferred access checks from a template-parameter-list.
32418 CHECKS is a TREE_LIST of access checks, as returned by
32419 get_deferred_access_checks. */
32421 static void
32422 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
32424 ++processing_template_parmlist;
32425 perform_access_checks (checks, tf_warning_or_error);
32426 --processing_template_parmlist;
32429 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
32430 `function-definition' sequence that follows a template header.
32431 If MEMBER_P is true, this declaration appears in a class scope.
32433 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
32434 *FRIEND_P is set to TRUE iff the declaration is a friend. */
32436 static tree
32437 cp_parser_single_declaration (cp_parser* parser,
32438 vec<deferred_access_check, va_gc> *checks,
32439 bool member_p,
32440 bool explicit_specialization_p,
32441 bool* friend_p)
32443 int declares_class_or_enum;
32444 tree decl = NULL_TREE;
32445 cp_decl_specifier_seq decl_specifiers;
32446 bool function_definition_p = false;
32447 cp_token *decl_spec_token_start;
32449 /* This function is only used when processing a template
32450 declaration. */
32451 gcc_assert (innermost_scope_kind () == sk_template_parms
32452 || innermost_scope_kind () == sk_template_spec);
32454 /* Defer access checks until we know what is being declared. */
32455 push_deferring_access_checks (dk_deferred);
32457 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
32458 alternative. */
32459 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
32460 cp_parser_decl_specifier_seq (parser,
32461 (CP_PARSER_FLAGS_OPTIONAL
32462 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
32463 &decl_specifiers,
32464 &declares_class_or_enum);
32466 cp_omp_declare_simd_data odsd;
32467 if (decl_specifiers.attributes && (flag_openmp || flag_openmp_simd))
32468 cp_parser_handle_directive_omp_attributes (parser,
32469 &decl_specifiers.attributes,
32470 &odsd, true);
32472 if (friend_p)
32473 *friend_p = cp_parser_friend_p (&decl_specifiers);
32475 /* There are no template typedefs. */
32476 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
32478 error_at (decl_spec_token_start->location,
32479 "template declaration of %<typedef%>");
32480 decl = error_mark_node;
32483 /* Gather up the access checks that occurred the
32484 decl-specifier-seq. */
32485 stop_deferring_access_checks ();
32487 /* Check for the declaration of a template class. */
32488 if (declares_class_or_enum)
32490 if (cp_parser_declares_only_class_p (parser)
32491 || (declares_class_or_enum & 2))
32493 decl = shadow_tag (&decl_specifiers);
32495 /* In this case:
32497 struct C {
32498 friend template <typename T> struct A<T>::B;
32501 A<T>::B will be represented by a TYPENAME_TYPE, and
32502 therefore not recognized by shadow_tag. */
32503 if (friend_p && *friend_p
32504 && !decl
32505 && decl_specifiers.type
32506 && TYPE_P (decl_specifiers.type))
32507 decl = decl_specifiers.type;
32509 if (decl && decl != error_mark_node)
32510 decl = TYPE_NAME (decl);
32511 else
32512 decl = error_mark_node;
32514 /* If this is a declaration, but not a definition, associate
32515 any constraints with the type declaration. Constraints
32516 are associated with definitions in cp_parser_class_specifier. */
32517 if (declares_class_or_enum == 1)
32518 associate_classtype_constraints (TREE_TYPE (decl));
32520 /* Perform access checks for template parameters. */
32521 cp_parser_perform_template_parameter_access_checks (checks);
32523 /* Give a helpful diagnostic for
32524 template <class T> struct A { } a;
32525 if we aren't already recovering from an error. */
32526 if (!cp_parser_declares_only_class_p (parser)
32527 && !seen_error ())
32529 error_at (cp_lexer_peek_token (parser->lexer)->location,
32530 "a class template declaration must not declare "
32531 "anything else");
32532 cp_parser_skip_to_end_of_block_or_statement (parser);
32533 goto out;
32538 /* Complain about missing 'typename' or other invalid type names. */
32539 if (!decl_specifiers.any_type_specifiers_p
32540 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
32542 /* cp_parser_parse_and_diagnose_invalid_type_name calls
32543 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
32544 the rest of this declaration. */
32545 decl = error_mark_node;
32546 goto out;
32549 /* If it's not a template class, try for a template function. If
32550 the next token is a `;', then this declaration does not declare
32551 anything. But, if there were errors in the decl-specifiers, then
32552 the error might well have come from an attempted class-specifier.
32553 In that case, there's no need to warn about a missing declarator. */
32554 if (!decl
32555 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
32556 || decl_specifiers.type != error_mark_node))
32558 int flags = CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
32559 /* We don't delay parsing for friends, though CWG 2510 may change
32560 that. */
32561 if (member_p && !(friend_p && *friend_p))
32562 flags |= CP_PARSER_FLAGS_DELAY_NOEXCEPT;
32563 decl = cp_parser_init_declarator (parser,
32564 flags,
32565 &decl_specifiers,
32566 checks,
32567 /*function_definition_allowed_p=*/true,
32568 member_p,
32569 declares_class_or_enum,
32570 &function_definition_p,
32571 NULL, NULL, NULL);
32573 /* 7.1.1-1 [dcl.stc]
32575 A storage-class-specifier shall not be specified in an explicit
32576 specialization... */
32577 if (decl
32578 && explicit_specialization_p
32579 && decl_specifiers.storage_class != sc_none)
32581 error_at (decl_spec_token_start->location,
32582 "explicit template specialization cannot have a storage class");
32583 decl = error_mark_node;
32586 if (decl && VAR_P (decl))
32587 check_template_variable (decl);
32590 /* Look for a trailing `;' after the declaration. */
32591 if (!function_definition_p
32592 && (decl == error_mark_node
32593 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
32594 cp_parser_skip_to_end_of_block_or_statement (parser);
32596 out:
32597 pop_deferring_access_checks ();
32599 /* Clear any current qualification; whatever comes next is the start
32600 of something new. */
32601 parser->scope = NULL_TREE;
32602 parser->qualifying_scope = NULL_TREE;
32603 parser->object_scope = NULL_TREE;
32605 cp_finalize_omp_declare_simd (parser, &odsd);
32607 return decl;
32610 /* Parse a cast-expression that is not the operand of a unary "&". */
32612 static cp_expr
32613 cp_parser_simple_cast_expression (cp_parser *parser)
32615 return cp_parser_cast_expression (parser, /*address_p=*/false,
32616 /*cast_p=*/false, /*decltype*/false, NULL);
32619 /* Parse a functional cast to TYPE. Returns an expression
32620 representing the cast. */
32622 static cp_expr
32623 cp_parser_functional_cast (cp_parser* parser, tree type)
32625 vec<tree, va_gc> *vec;
32626 tree expression_list;
32627 cp_expr cast;
32628 bool nonconst_p;
32630 location_t start_loc = input_location;
32632 if (!type)
32633 type = error_mark_node;
32635 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
32637 cp_lexer_set_source_position (parser->lexer);
32638 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
32639 expression_list = cp_parser_braced_list (parser, &nonconst_p);
32640 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
32641 if (TREE_CODE (type) == TYPE_DECL)
32642 type = TREE_TYPE (type);
32644 cast = finish_compound_literal (type, expression_list,
32645 tf_warning_or_error, fcl_functional);
32646 /* Create a location of the form:
32647 type_name{i, f}
32648 ^~~~~~~~~~~~~~~
32649 with caret == start at the start of the type name,
32650 finishing at the closing brace. */
32651 location_t combined_loc = make_location (start_loc, start_loc,
32652 parser->lexer);
32653 cast.set_location (combined_loc);
32654 return cast;
32658 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
32659 /*cast_p=*/true,
32660 /*allow_expansion_p=*/true,
32661 /*non_constant_p=*/NULL);
32662 if (vec == NULL)
32663 expression_list = error_mark_node;
32664 else
32666 expression_list = build_tree_list_vec (vec);
32667 release_tree_vector (vec);
32670 /* Create a location of the form:
32671 float(i)
32672 ^~~~~~~~
32673 with caret == start at the start of the type name,
32674 finishing at the closing paren. */
32675 location_t combined_loc = make_location (start_loc, start_loc,
32676 parser->lexer);
32677 cast = build_functional_cast (combined_loc, type, expression_list,
32678 tf_warning_or_error);
32680 /* [expr.const]/1: In an integral constant expression "only type
32681 conversions to integral or enumeration type can be used". */
32682 if (TREE_CODE (type) == TYPE_DECL)
32683 type = TREE_TYPE (type);
32684 if (cast != error_mark_node
32685 && !cast_valid_in_integral_constant_expression_p (type)
32686 && cp_parser_non_integral_constant_expression (parser,
32687 NIC_CONSTRUCTOR))
32688 return error_mark_node;
32690 return cast;
32693 /* Save the tokens that make up the body of a member function defined
32694 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
32695 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
32696 specifiers applied to the declaration. Returns the FUNCTION_DECL
32697 for the member function. */
32699 static tree
32700 cp_parser_save_member_function_body (cp_parser* parser,
32701 cp_decl_specifier_seq *decl_specifiers,
32702 cp_declarator *declarator,
32703 tree attributes)
32705 cp_token *first;
32706 cp_token *last;
32707 tree fn;
32708 bool function_try_block = false;
32710 /* Create the FUNCTION_DECL. */
32711 fn = grokmethod (decl_specifiers, declarator, attributes);
32712 cp_finalize_omp_declare_simd (parser, fn);
32713 cp_finalize_oacc_routine (parser, fn, true);
32714 /* If something went badly wrong, bail out now. */
32715 if (fn == error_mark_node)
32717 /* If there's a function-body, skip it. */
32718 if (cp_parser_token_starts_function_definition_p
32719 (cp_lexer_peek_token (parser->lexer)))
32720 cp_parser_skip_to_end_of_block_or_statement (parser);
32721 return error_mark_node;
32724 /* Remember it, if there are default args to post process. */
32725 cp_parser_save_default_args (parser, fn);
32727 /* Save away the tokens that make up the body of the
32728 function. */
32729 first = parser->lexer->next_token;
32731 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
32732 cp_lexer_consume_token (parser->lexer);
32733 else if (cp_lexer_next_token_is_keyword (parser->lexer,
32734 RID_TRANSACTION_ATOMIC))
32736 cp_lexer_consume_token (parser->lexer);
32737 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
32738 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
32739 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
32740 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
32741 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
32742 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
32743 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
32745 cp_lexer_consume_token (parser->lexer);
32746 cp_lexer_consume_token (parser->lexer);
32747 cp_lexer_consume_token (parser->lexer);
32748 cp_lexer_consume_token (parser->lexer);
32749 cp_lexer_consume_token (parser->lexer);
32751 else
32752 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
32753 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
32755 cp_lexer_consume_token (parser->lexer);
32756 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
32757 break;
32761 /* Handle function try blocks. */
32762 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
32764 cp_lexer_consume_token (parser->lexer);
32765 function_try_block = true;
32767 /* We can have braced-init-list mem-initializers before the fn body. */
32768 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
32770 cp_lexer_consume_token (parser->lexer);
32771 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
32773 /* cache_group will stop after an un-nested { } pair, too. */
32774 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
32775 break;
32777 /* variadic mem-inits have ... after the ')'. */
32778 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
32779 cp_lexer_consume_token (parser->lexer);
32782 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
32783 /* Handle function try blocks. */
32784 if (function_try_block)
32785 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
32786 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
32787 last = parser->lexer->next_token;
32789 /* Save away the inline definition; we will process it when the
32790 class is complete. */
32791 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
32792 DECL_PENDING_INLINE_P (fn) = 1;
32794 /* We need to know that this was defined in the class, so that
32795 friend templates are handled correctly. */
32796 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
32798 /* Add FN to the queue of functions to be parsed later. */
32799 vec_safe_push (unparsed_funs_with_definitions, fn);
32801 return fn;
32804 /* Save the tokens that make up the in-class initializer for a non-static
32805 data member. Returns a DEFERRED_PARSE. */
32807 static tree
32808 cp_parser_save_nsdmi (cp_parser* parser)
32810 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
32813 /* Parse a template-argument-list, as well as the trailing ">" (but
32814 not the opening "<"). See cp_parser_template_argument_list for the
32815 return value. */
32817 static tree
32818 cp_parser_enclosed_template_argument_list (cp_parser* parser)
32820 tree arguments;
32821 tree saved_scope;
32822 tree saved_qualifying_scope;
32823 tree saved_object_scope;
32824 bool saved_greater_than_is_operator_p;
32826 /* [temp.names]
32828 When parsing a template-id, the first non-nested `>' is taken as
32829 the end of the template-argument-list rather than a greater-than
32830 operator. */
32831 saved_greater_than_is_operator_p
32832 = parser->greater_than_is_operator_p;
32833 parser->greater_than_is_operator_p = false;
32834 /* Parsing the argument list may modify SCOPE, so we save it
32835 here. */
32836 saved_scope = parser->scope;
32837 saved_qualifying_scope = parser->qualifying_scope;
32838 saved_object_scope = parser->object_scope;
32839 /* We need to evaluate the template arguments, even though this
32840 template-id may be nested within a "sizeof". */
32841 cp_evaluated ev;
32842 /* Parse the template-argument-list itself. */
32843 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
32844 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT)
32845 || cp_lexer_next_token_is (parser->lexer, CPP_GREATER_EQ)
32846 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT_EQ))
32848 arguments = make_tree_vec (0);
32849 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (arguments, 0);
32851 else
32852 arguments = cp_parser_template_argument_list (parser);
32853 /* Look for the `>' that ends the template-argument-list. If we find
32854 a '>>' instead, it's probably just a typo. */
32855 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
32857 if (cxx_dialect != cxx98)
32859 /* In C++0x, a `>>' in a template argument list or cast
32860 expression is considered to be two separate `>'
32861 tokens. So, change the current token to a `>', but don't
32862 consume it: it will be consumed later when the outer
32863 template argument list (or cast expression) is parsed.
32864 Note that this replacement of `>' for `>>' is necessary
32865 even if we are parsing tentatively: in the tentative
32866 case, after calling
32867 cp_parser_enclosed_template_argument_list we will always
32868 throw away all of the template arguments and the first
32869 closing `>', either because the template argument list
32870 was erroneous or because we are replacing those tokens
32871 with a CPP_TEMPLATE_ID token. The second `>' (which will
32872 not have been thrown away) is needed either to close an
32873 outer template argument list or to complete a new-style
32874 cast. */
32875 cp_token *token = cp_lexer_peek_token (parser->lexer);
32876 token->type = CPP_GREATER;
32878 else if (!saved_greater_than_is_operator_p)
32880 /* If we're in a nested template argument list, the '>>' has
32881 to be a typo for '> >'. We emit the error message, but we
32882 continue parsing and we push a '>' as next token, so that
32883 the argument list will be parsed correctly. Note that the
32884 global source location is still on the token before the
32885 '>>', so we need to say explicitly where we want it. */
32886 cp_token *token = cp_lexer_peek_token (parser->lexer);
32887 gcc_rich_location richloc (token->location);
32888 richloc.add_fixit_replace ("> >");
32889 error_at (&richloc, "%<>>%> should be %<> >%> "
32890 "within a nested template argument list");
32892 token->type = CPP_GREATER;
32894 else
32896 /* If this is not a nested template argument list, the '>>'
32897 is a typo for '>'. Emit an error message and continue.
32898 Same deal about the token location, but here we can get it
32899 right by consuming the '>>' before issuing the diagnostic. */
32900 cp_token *token = cp_lexer_consume_token (parser->lexer);
32901 error_at (token->location,
32902 "spurious %<>>%>, use %<>%> to terminate "
32903 "a template argument list");
32906 /* Similarly for >>= and >=. */
32907 else if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER_EQ)
32908 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT_EQ))
32910 cp_token *token = cp_lexer_consume_token (parser->lexer);
32911 gcc_rich_location richloc (token->location);
32912 enum cpp_ttype new_type;
32913 const char *replacement;
32914 if (token->type == CPP_GREATER_EQ)
32916 replacement = "> =";
32917 new_type = CPP_EQ;
32919 else if (!saved_greater_than_is_operator_p)
32921 if (cxx_dialect != cxx98)
32922 replacement = ">> =";
32923 else
32924 replacement = "> > =";
32925 new_type = CPP_GREATER;
32927 else
32929 replacement = "> >=";
32930 new_type = CPP_GREATER_EQ;
32932 richloc.add_fixit_replace (replacement);
32933 error_at (&richloc, "%qs should be %qs to terminate a template "
32934 "argument list",
32935 cpp_type2name (token->type, token->flags), replacement);
32936 token->type = new_type;
32938 else
32939 cp_parser_require_end_of_template_parameter_list (parser);
32940 /* The `>' token might be a greater-than operator again now. */
32941 parser->greater_than_is_operator_p
32942 = saved_greater_than_is_operator_p;
32943 /* Restore the SAVED_SCOPE. */
32944 parser->scope = saved_scope;
32945 parser->qualifying_scope = saved_qualifying_scope;
32946 parser->object_scope = saved_object_scope;
32948 return arguments;
32951 /* MEMBER_FUNCTION is a member function, or a friend. If default
32952 arguments, or the body of the function have not yet been parsed,
32953 parse them now. */
32955 static void
32956 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
32958 auto_timevar tv (TV_PARSE_INMETH);
32960 /* If this member is a template, get the underlying
32961 FUNCTION_DECL. */
32962 if (DECL_FUNCTION_TEMPLATE_P (member_function))
32963 member_function = DECL_TEMPLATE_RESULT (member_function);
32965 /* There should not be any class definitions in progress at this
32966 point; the bodies of members are only parsed outside of all class
32967 definitions. */
32968 gcc_assert (parser->num_classes_being_defined == 0);
32969 /* While we're parsing the member functions we might encounter more
32970 classes. We want to handle them right away, but we don't want
32971 them getting mixed up with functions that are currently in the
32972 queue. */
32973 push_unparsed_function_queues (parser);
32975 /* Make sure that any template parameters are in scope. */
32976 maybe_begin_member_template_processing (member_function);
32978 /* If the body of the function has not yet been parsed, parse it
32979 now. Except if the tokens have been purged (PR c++/39751). */
32980 if (DECL_PENDING_INLINE_P (member_function)
32981 && !DECL_PENDING_INLINE_INFO (member_function)->first->purged_p)
32983 tree function_scope;
32984 cp_token_cache *tokens;
32986 /* The function is no longer pending; we are processing it. */
32987 tokens = DECL_PENDING_INLINE_INFO (member_function);
32988 DECL_PENDING_INLINE_INFO (member_function) = NULL;
32989 DECL_PENDING_INLINE_P (member_function) = 0;
32991 /* If this is a local class, enter the scope of the containing
32992 function. */
32993 function_scope = current_function_decl;
32994 if (function_scope)
32995 push_function_context ();
32997 /* Push the body of the function onto the lexer stack. */
32998 cp_parser_push_lexer_for_tokens (parser, tokens);
33000 /* Let the front end know that we going to be defining this
33001 function. */
33002 start_preparsed_function (member_function, NULL_TREE,
33003 SF_PRE_PARSED | SF_INCLASS_INLINE);
33005 /* #pragma omp declare reduction needs special parsing. */
33006 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
33008 parser->lexer->in_pragma = true;
33009 cp_parser_omp_declare_reduction_exprs (member_function, parser);
33010 finish_function (/*inline_p=*/true);
33011 cp_check_omp_declare_reduction (member_function);
33013 else
33014 /* Now, parse the body of the function. */
33015 cp_parser_function_definition_after_declarator (parser,
33016 /*inline_p=*/true);
33018 /* Leave the scope of the containing function. */
33019 if (function_scope)
33020 pop_function_context ();
33021 cp_parser_pop_lexer (parser);
33024 /* Remove any template parameters from the symbol table. */
33025 maybe_end_member_template_processing ();
33027 /* Restore the queue. */
33028 pop_unparsed_function_queues (parser);
33031 /* If DECL contains any default args, remember it on the unparsed
33032 functions queue. */
33034 static void
33035 cp_parser_save_default_args (cp_parser* parser, tree decl)
33037 tree probe;
33039 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
33040 probe;
33041 probe = TREE_CHAIN (probe))
33042 if (TREE_PURPOSE (probe))
33044 cp_default_arg_entry entry = {current_class_type, decl};
33045 vec_safe_push (unparsed_funs_with_default_args, entry);
33046 break;
33049 /* Remember if there is a noexcept-specifier to post process. */
33050 tree spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl));
33051 if (UNPARSED_NOEXCEPT_SPEC_P (spec))
33052 vec_safe_push (unparsed_noexcepts, decl);
33054 /* Contracts are deferred. */
33055 for (tree attr = DECL_ATTRIBUTES (decl); attr; attr = TREE_CHAIN (attr))
33056 if (cxx_contract_attribute_p (attr))
33058 vec_safe_push (unparsed_contracts, decl);
33059 break;
33063 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
33064 which is either a FIELD_DECL or PARM_DECL. Parse it and return
33065 the result. For a PARM_DECL, PARMTYPE is the corresponding type
33066 from the parameter-type-list. */
33068 static tree
33069 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
33070 tree default_arg, tree parmtype)
33072 cp_token_cache *tokens;
33073 tree parsed_arg;
33074 bool dummy;
33076 if (default_arg == error_mark_node)
33077 return error_mark_node;
33079 /* Push the saved tokens for the default argument onto the parser's
33080 lexer stack. */
33081 tokens = DEFPARSE_TOKENS (default_arg);
33082 cp_parser_push_lexer_for_tokens (parser, tokens);
33084 start_lambda_scope (decl);
33086 /* Parse the default argument. */
33087 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
33088 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
33089 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
33091 finish_lambda_scope ();
33093 if (parsed_arg == error_mark_node)
33094 cp_parser_skip_to_end_of_statement (parser);
33096 if (!processing_template_decl)
33098 /* In a non-template class, check conversions now. In a template,
33099 we'll wait and instantiate these as needed. */
33100 if (TREE_CODE (decl) == PARM_DECL)
33101 parsed_arg = check_default_argument (parmtype, parsed_arg,
33102 tf_warning_or_error);
33103 else if (maybe_reject_flexarray_init (decl, parsed_arg))
33104 parsed_arg = error_mark_node;
33105 else
33106 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
33109 /* If the token stream has not been completely used up, then
33110 there was extra junk after the end of the default
33111 argument. */
33112 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
33114 if (TREE_CODE (decl) == PARM_DECL)
33115 cp_parser_error (parser, "expected %<,%>");
33116 else
33117 cp_parser_error (parser, "expected %<;%>");
33120 /* Revert to the main lexer. */
33121 cp_parser_pop_lexer (parser);
33123 return parsed_arg;
33126 /* FIELD is a non-static data member with an initializer which we saved for
33127 later; parse it now. */
33129 static void
33130 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
33132 tree def;
33134 maybe_begin_member_template_processing (field);
33136 push_unparsed_function_queues (parser);
33137 def = cp_parser_late_parse_one_default_arg (parser, field,
33138 DECL_INITIAL (field),
33139 NULL_TREE);
33140 pop_unparsed_function_queues (parser);
33142 maybe_end_member_template_processing ();
33144 DECL_INITIAL (field) = def;
33147 /* FN is a FUNCTION_DECL which may contains a parameter with an
33148 unparsed DEFERRED_PARSE. Parse the default args now. This function
33149 assumes that the current scope is the scope in which the default
33150 argument should be processed. */
33152 static void
33153 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
33155 unsigned char saved_local_variables_forbidden_p;
33157 /* While we're parsing the default args, we might (due to the
33158 statement expression extension) encounter more classes. We want
33159 to handle them right away, but we don't want them getting mixed
33160 up with default args that are currently in the queue. */
33161 push_unparsed_function_queues (parser);
33163 /* Local variable names (and the `this' keyword) may not appear
33164 in a default argument. */
33165 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
33166 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
33168 push_defarg_context (fn);
33170 begin_scope (sk_function_parms, fn);
33172 /* Gather the PARM_DECLs into a vec so we can keep track of them when
33173 pushdecl clears DECL_CHAIN. */
33174 releasing_vec parms;
33175 for (tree parmdecl = DECL_ARGUMENTS (fn); parmdecl;
33176 parmdecl = DECL_CHAIN (parmdecl))
33177 vec_safe_push (parms, parmdecl);
33179 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
33180 for (int i = 0;
33181 parm && parm != void_list_node;
33182 parm = TREE_CHAIN (parm),
33183 ++i)
33185 tree default_arg = TREE_PURPOSE (parm);
33186 tree parsed_arg;
33188 tree parmdecl = parms[i];
33189 pushdecl (parmdecl);
33191 if (!default_arg)
33192 continue;
33194 if (TREE_CODE (default_arg) != DEFERRED_PARSE)
33195 /* This can happen for a friend declaration for a function
33196 already declared with default arguments. */
33197 continue;
33199 parsed_arg
33200 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
33201 default_arg,
33202 TREE_VALUE (parm));
33203 TREE_PURPOSE (parm) = parsed_arg;
33205 /* Update any instantiations we've already created. */
33206 for (tree copy : DEFPARSE_INSTANTIATIONS (default_arg))
33207 TREE_PURPOSE (copy) = parsed_arg;
33210 pop_bindings_and_leave_scope ();
33212 /* Restore DECL_CHAINs after clobbering by pushdecl. */
33213 parm = NULL_TREE;
33214 for (int i = parms->length () - 1; i >= 0; --i)
33216 DECL_CHAIN (parms[i]) = parm;
33217 parm = parms[i];
33220 pop_defarg_context ();
33222 /* Make sure no default arg is missing. */
33223 check_default_args (fn);
33225 /* Restore the state of local_variables_forbidden_p. */
33226 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
33228 /* Restore the queue. */
33229 pop_unparsed_function_queues (parser);
33232 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
33234 sizeof ... ( identifier )
33236 where the 'sizeof' token has already been consumed. */
33238 static tree
33239 cp_parser_sizeof_pack (cp_parser *parser)
33241 /* Consume the `...'. */
33242 cp_lexer_consume_token (parser->lexer);
33243 maybe_warn_variadic_templates ();
33245 matching_parens parens;
33246 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
33247 if (paren)
33248 parens.consume_open (parser);
33249 else
33250 permerror (cp_lexer_peek_token (parser->lexer)->location,
33251 "%<sizeof...%> argument must be surrounded by parentheses");
33253 cp_token *token = cp_lexer_peek_token (parser->lexer);
33254 tree name = cp_parser_identifier (parser);
33255 if (name == error_mark_node)
33256 return error_mark_node;
33257 /* The name is not qualified. */
33258 parser->scope = NULL_TREE;
33259 parser->qualifying_scope = NULL_TREE;
33260 parser->object_scope = NULL_TREE;
33261 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
33262 if (expr == error_mark_node)
33263 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
33264 token->location);
33265 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
33266 expr = TREE_TYPE (expr);
33267 else if (TREE_CODE (expr) == CONST_DECL)
33268 expr = DECL_INITIAL (expr);
33269 expr = make_pack_expansion (expr);
33270 if (expr != error_mark_node)
33271 PACK_EXPANSION_SIZEOF_P (expr) = true;
33273 if (paren)
33274 parens.require_close (parser);
33276 return expr;
33279 /* Parse the operand of `sizeof' (or a similar operator). Returns
33280 either a TYPE or an expression, depending on the form of the
33281 input. The KEYWORD indicates which kind of expression we have
33282 encountered. */
33284 static tree
33285 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
33287 tree expr = NULL_TREE;
33288 const char *saved_message;
33289 const char *saved_message_arg;
33290 bool saved_integral_constant_expression_p;
33291 bool saved_non_integral_constant_expression_p;
33293 /* If it's a `...', then we are computing the length of a parameter
33294 pack. */
33295 if (keyword == RID_SIZEOF
33296 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
33297 return cp_parser_sizeof_pack (parser);
33299 /* Types cannot be defined in a `sizeof' expression. Save away the
33300 old message. */
33301 saved_message = parser->type_definition_forbidden_message;
33302 saved_message_arg = parser->type_definition_forbidden_message_arg;
33303 parser->type_definition_forbidden_message
33304 = G_("types may not be defined in %qs expressions");
33305 parser->type_definition_forbidden_message_arg
33306 = IDENTIFIER_POINTER (ridpointers[keyword]);
33308 /* The restrictions on constant-expressions do not apply inside
33309 sizeof expressions. */
33310 saved_integral_constant_expression_p
33311 = parser->integral_constant_expression_p;
33312 saved_non_integral_constant_expression_p
33313 = parser->non_integral_constant_expression_p;
33314 parser->integral_constant_expression_p = false;
33316 auto cleanup = make_temp_override
33317 (parser->auto_is_implicit_function_template_parm_p, false);
33319 /* Do not actually evaluate the expression. */
33320 ++cp_unevaluated_operand;
33321 ++c_inhibit_evaluation_warnings;
33322 /* If it's a `(', then we might be looking at the type-id
33323 construction. */
33324 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33326 tree type = NULL_TREE;
33328 tentative_firewall firewall (parser);
33330 /* We can't be sure yet whether we're looking at a type-id or an
33331 expression. */
33332 cp_parser_parse_tentatively (parser);
33334 matching_parens parens;
33335 parens.consume_open (parser);
33337 /* Note: as a GNU Extension, compound literals are considered
33338 postfix-expressions as they are in C99, so they are valid
33339 arguments to sizeof. See comment in cp_parser_cast_expression
33340 for details. */
33341 if (cp_parser_compound_literal_p (parser))
33342 cp_parser_simulate_error (parser);
33343 else
33345 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
33346 parser->in_type_id_in_expr_p = true;
33347 /* Look for the type-id. */
33348 type = cp_parser_type_id (parser);
33349 /* Look for the closing `)'. */
33350 parens.require_close (parser);
33351 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
33354 /* If all went well, then we're done. */
33355 if (cp_parser_parse_definitely (parser))
33356 expr = type;
33357 else
33359 /* Commit to the tentative_firewall so we get syntax errors. */
33360 cp_parser_commit_to_tentative_parse (parser);
33362 expr = cp_parser_unary_expression (parser);
33365 else
33366 expr = cp_parser_unary_expression (parser);
33368 /* Go back to evaluating expressions. */
33369 --cp_unevaluated_operand;
33370 --c_inhibit_evaluation_warnings;
33372 /* And restore the old one. */
33373 parser->type_definition_forbidden_message = saved_message;
33374 parser->type_definition_forbidden_message_arg = saved_message_arg;
33375 parser->integral_constant_expression_p
33376 = saved_integral_constant_expression_p;
33377 parser->non_integral_constant_expression_p
33378 = saved_non_integral_constant_expression_p;
33380 return expr;
33383 /* If the current declaration has no declarator, return true. */
33385 static bool
33386 cp_parser_declares_only_class_p (cp_parser *parser)
33388 /* If the next token is a `;' or a `,' then there is no
33389 declarator. */
33390 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
33391 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
33394 /* Update the DECL_SPECS to reflect the storage class indicated by
33395 KEYWORD. */
33397 static void
33398 cp_parser_set_storage_class (cp_parser *parser,
33399 cp_decl_specifier_seq *decl_specs,
33400 enum rid keyword,
33401 cp_token *token)
33403 cp_storage_class storage_class;
33405 switch (keyword)
33407 case RID_AUTO:
33408 storage_class = sc_auto;
33409 break;
33410 case RID_REGISTER:
33411 storage_class = sc_register;
33412 break;
33413 case RID_STATIC:
33414 storage_class = sc_static;
33415 break;
33416 case RID_EXTERN:
33417 storage_class = sc_extern;
33418 break;
33419 case RID_MUTABLE:
33420 storage_class = sc_mutable;
33421 break;
33422 default:
33423 gcc_unreachable ();
33426 if (parser->in_unbraced_linkage_specification_p)
33428 error_at (token->location, "invalid use of %qD in linkage specification",
33429 ridpointers[keyword]);
33430 return;
33432 else if (decl_specs->storage_class != sc_none)
33434 if (decl_specs->conflicting_specifiers_p)
33435 return;
33436 gcc_rich_location richloc (token->location);
33437 richloc.add_location_if_nearby (decl_specs->locations[ds_storage_class]);
33438 if (decl_specs->storage_class == storage_class)
33439 error_at (&richloc, "duplicate %qD specifier", ridpointers[keyword]);
33440 else
33441 error_at (&richloc,
33442 "%qD specifier conflicts with %qs",
33443 ridpointers[keyword],
33444 cp_storage_class_name[decl_specs->storage_class]);
33445 decl_specs->conflicting_specifiers_p = true;
33446 return;
33449 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
33450 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
33451 && decl_specs->gnu_thread_keyword_p)
33453 pedwarn (decl_specs->locations[ds_thread], 0,
33454 "%<__thread%> before %qD", ridpointers[keyword]);
33457 decl_specs->storage_class = storage_class;
33458 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
33460 /* A storage class specifier cannot be applied alongside a typedef
33461 specifier. If there is a typedef specifier present then set
33462 conflicting_specifiers_p which will trigger an error later
33463 on in grokdeclarator. */
33464 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
33465 && !decl_specs->conflicting_specifiers_p)
33467 gcc_rich_location richloc (token->location);
33468 richloc.add_location_if_nearby (decl_specs->locations[ds_typedef]);
33469 error_at (&richloc,
33470 "%qD specifier conflicts with %<typedef%>",
33471 ridpointers[keyword]);
33472 decl_specs->conflicting_specifiers_p = true;
33476 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
33477 is true, the type is a class or enum definition. */
33479 static void
33480 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
33481 tree type_spec,
33482 cp_token *token,
33483 bool type_definition_p)
33485 decl_specs->any_specifiers_p = true;
33487 /* If the user tries to redeclare bool, char8_t, char16_t, char32_t, or
33488 wchar_t (with, for example, in "typedef int wchar_t;") we remember that
33489 this is what happened. In system headers, we ignore these
33490 declarations so that G++ can work with system headers that are not
33491 C++-safe. */
33492 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
33493 && !type_definition_p
33494 && TYPE_P (type_spec)
33495 && (type_spec == boolean_type_node
33496 || type_spec == char8_type_node
33497 || type_spec == char16_type_node
33498 || type_spec == char32_type_node
33499 || extended_float_type_p (type_spec)
33500 || type_spec == wchar_type_node)
33501 && (decl_specs->type
33502 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
33503 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
33504 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
33505 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
33507 decl_specs->redefined_builtin_type = type_spec;
33508 set_and_check_decl_spec_loc (decl_specs,
33509 ds_redefined_builtin_type_spec,
33510 token);
33511 if (!decl_specs->type)
33513 decl_specs->type = type_spec;
33514 decl_specs->type_definition_p = false;
33515 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
33518 else if (decl_specs->type)
33519 decl_specs->multiple_types_p = true;
33520 else
33522 decl_specs->type = type_spec;
33523 decl_specs->type_definition_p = type_definition_p;
33524 decl_specs->redefined_builtin_type = NULL_TREE;
33525 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
33529 /* True iff TOKEN is the GNU keyword __thread. */
33531 static bool
33532 token_is__thread (cp_token *token)
33534 gcc_assert (token->keyword == RID_THREAD);
33535 return id_equal (token->u.value, "__thread");
33538 /* Set the location for a declarator specifier and check if it is
33539 duplicated.
33541 DECL_SPECS is the sequence of declarator specifiers onto which to
33542 set the location.
33544 DS is the single declarator specifier to set which location is to
33545 be set onto the existing sequence of declarators.
33547 LOCATION is the location for the declarator specifier to
33548 consider. */
33550 static void
33551 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
33552 cp_decl_spec ds, cp_token *token)
33554 gcc_assert (ds < ds_last);
33556 if (decl_specs == NULL)
33557 return;
33559 location_t location = token->location;
33561 if (decl_specs->locations[ds] == 0)
33563 decl_specs->locations[ds] = location;
33564 if (ds == ds_thread)
33565 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
33567 else
33569 if (ds == ds_long)
33571 if (decl_specs->locations[ds_long_long] != 0)
33572 error_at (location,
33573 "%<long long long%> is too long for GCC");
33574 else
33576 decl_specs->locations[ds_long_long] = location;
33577 pedwarn_cxx98 (location,
33578 OPT_Wlong_long,
33579 "ISO C++ 1998 does not support %<long long%>");
33582 else if (ds == ds_thread)
33584 bool gnu = token_is__thread (token);
33585 gcc_rich_location richloc (location);
33586 if (gnu != decl_specs->gnu_thread_keyword_p)
33588 richloc.add_range (decl_specs->locations[ds_thread]);
33589 error_at (&richloc,
33590 "both %<__thread%> and %<thread_local%> specified");
33592 else
33594 richloc.add_fixit_remove ();
33595 error_at (&richloc, "duplicate %qD", token->u.value);
33598 else
33600 static const char *const decl_spec_names[] = {
33601 "signed",
33602 "unsigned",
33603 "short",
33604 "long",
33605 "const",
33606 "volatile",
33607 "restrict",
33608 "inline",
33609 "virtual",
33610 "explicit",
33611 "friend",
33612 "typedef",
33613 "using",
33614 "constexpr",
33615 "__complex",
33616 "constinit",
33617 "consteval"
33619 gcc_rich_location richloc (location);
33620 richloc.add_fixit_remove ();
33621 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
33626 /* Return true iff the declarator specifier DS is present in the
33627 sequence of declarator specifiers DECL_SPECS. */
33629 bool
33630 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
33631 cp_decl_spec ds)
33633 gcc_assert (ds < ds_last);
33635 if (decl_specs == NULL)
33636 return false;
33638 return decl_specs->locations[ds] != 0;
33641 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
33642 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
33644 static bool
33645 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
33647 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
33650 /* Issue an error message indicating that TOKEN_DESC was expected.
33651 If KEYWORD is true, it indicated this function is called by
33652 cp_parser_require_keword and the required token can only be
33653 a indicated keyword.
33655 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
33656 within any error as the location of an "opening" token matching
33657 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
33658 RT_CLOSE_PAREN). */
33660 static void
33661 cp_parser_required_error (cp_parser *parser,
33662 required_token token_desc,
33663 bool keyword,
33664 location_t matching_location)
33666 if (cp_parser_simulate_error (parser))
33667 return;
33669 const char *gmsgid = NULL;
33670 switch (token_desc)
33672 case RT_NEW:
33673 gmsgid = G_("expected %<new%>");
33674 break;
33675 case RT_DELETE:
33676 gmsgid = G_("expected %<delete%>");
33677 break;
33678 case RT_RETURN:
33679 gmsgid = G_("expected %<return%>");
33680 break;
33681 case RT_WHILE:
33682 gmsgid = G_("expected %<while%>");
33683 break;
33684 case RT_EXTERN:
33685 gmsgid = G_("expected %<extern%>");
33686 break;
33687 case RT_STATIC_ASSERT:
33688 gmsgid = G_("expected %<static_assert%>");
33689 break;
33690 case RT_DECLTYPE:
33691 gmsgid = G_("expected %<decltype%>");
33692 break;
33693 case RT_OPERATOR:
33694 gmsgid = G_("expected %<operator%>");
33695 break;
33696 case RT_CLASS:
33697 gmsgid = G_("expected %<class%>");
33698 break;
33699 case RT_TEMPLATE:
33700 gmsgid = G_("expected %<template%>");
33701 break;
33702 case RT_NAMESPACE:
33703 gmsgid = G_("expected %<namespace%>");
33704 break;
33705 case RT_USING:
33706 gmsgid = G_("expected %<using%>");
33707 break;
33708 case RT_ASM:
33709 gmsgid = G_("expected %<asm%>");
33710 break;
33711 case RT_TRY:
33712 gmsgid = G_("expected %<try%>");
33713 break;
33714 case RT_CATCH:
33715 gmsgid = G_("expected %<catch%>");
33716 break;
33717 case RT_THROW:
33718 gmsgid = G_("expected %<throw%>");
33719 break;
33720 case RT_AUTO:
33721 gmsgid = G_("expected %<auto%>");
33722 break;
33723 case RT_LABEL:
33724 gmsgid = G_("expected %<__label__%>");
33725 break;
33726 case RT_AT_TRY:
33727 gmsgid = G_("expected %<@try%>");
33728 break;
33729 case RT_AT_SYNCHRONIZED:
33730 gmsgid = G_("expected %<@synchronized%>");
33731 break;
33732 case RT_AT_THROW:
33733 gmsgid = G_("expected %<@throw%>");
33734 break;
33735 case RT_TRANSACTION_ATOMIC:
33736 gmsgid = G_("expected %<__transaction_atomic%>");
33737 break;
33738 case RT_TRANSACTION_RELAXED:
33739 gmsgid = G_("expected %<__transaction_relaxed%>");
33740 break;
33741 case RT_CO_YIELD:
33742 gmsgid = G_("expected %<co_yield%>");
33743 break;
33744 default:
33745 break;
33748 if (!gmsgid && !keyword)
33750 switch (token_desc)
33752 case RT_SEMICOLON:
33753 gmsgid = G_("expected %<;%>");
33754 break;
33755 case RT_OPEN_PAREN:
33756 gmsgid = G_("expected %<(%>");
33757 break;
33758 case RT_CLOSE_BRACE:
33759 gmsgid = G_("expected %<}%>");
33760 break;
33761 case RT_OPEN_BRACE:
33762 gmsgid = G_("expected %<{%>");
33763 break;
33764 case RT_CLOSE_SQUARE:
33765 gmsgid = G_("expected %<]%>");
33766 break;
33767 case RT_OPEN_SQUARE:
33768 gmsgid = G_("expected %<[%>");
33769 break;
33770 case RT_COMMA:
33771 gmsgid = G_("expected %<,%>");
33772 break;
33773 case RT_SCOPE:
33774 gmsgid = G_("expected %<::%>");
33775 break;
33776 case RT_LESS:
33777 gmsgid = G_("expected %<<%>");
33778 break;
33779 case RT_GREATER:
33780 gmsgid = G_("expected %<>%>");
33781 break;
33782 case RT_EQ:
33783 gmsgid = G_("expected %<=%>");
33784 break;
33785 case RT_ELLIPSIS:
33786 gmsgid = G_("expected %<...%>");
33787 break;
33788 case RT_MULT:
33789 gmsgid = G_("expected %<*%>");
33790 break;
33791 case RT_COMPL:
33792 gmsgid = G_("expected %<~%>");
33793 break;
33794 case RT_COLON:
33795 gmsgid = G_("expected %<:%>");
33796 break;
33797 case RT_COLON_SCOPE:
33798 gmsgid = G_("expected %<:%> or %<::%>");
33799 break;
33800 case RT_CLOSE_PAREN:
33801 gmsgid = G_("expected %<)%>");
33802 break;
33803 case RT_COMMA_CLOSE_PAREN:
33804 gmsgid = G_("expected %<,%> or %<)%>");
33805 break;
33806 case RT_PRAGMA_EOL:
33807 gmsgid = G_("expected end of line");
33808 break;
33809 case RT_NAME:
33810 gmsgid = G_("expected identifier");
33811 break;
33812 case RT_SELECT:
33813 gmsgid = G_("expected selection-statement");
33814 break;
33815 case RT_ITERATION:
33816 gmsgid = G_("expected iteration-statement");
33817 break;
33818 case RT_JUMP:
33819 gmsgid = G_("expected jump-statement");
33820 break;
33821 case RT_CLASS_KEY:
33822 gmsgid = G_("expected class-key");
33823 break;
33824 case RT_CLASS_TYPENAME_TEMPLATE:
33825 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
33826 break;
33827 default:
33828 gcc_unreachable ();
33832 if (gmsgid)
33833 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
33837 /* If the next token is of the indicated TYPE, consume it. Otherwise,
33838 issue an error message indicating that TOKEN_DESC was expected.
33840 Returns the token consumed, if the token had the appropriate type.
33841 Otherwise, returns NULL.
33843 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
33844 within any error as the location of an "opening" token matching
33845 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
33846 RT_CLOSE_PAREN). */
33848 static cp_token *
33849 cp_parser_require (cp_parser* parser,
33850 enum cpp_ttype type,
33851 required_token token_desc,
33852 location_t matching_location)
33854 if (cp_lexer_next_token_is (parser->lexer, type))
33855 return cp_lexer_consume_token (parser->lexer);
33856 else
33858 /* Output the MESSAGE -- unless we're parsing tentatively. */
33859 if (!cp_parser_simulate_error (parser))
33860 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
33861 matching_location);
33862 return NULL;
33866 /* Skip an entire parameter list from start to finish. The next token must
33867 be the initial "<" of the parameter list. Returns true on success and
33868 false otherwise. */
33870 static bool
33871 cp_parser_skip_entire_template_parameter_list (cp_parser* parser)
33873 /* Consume the "<" because cp_parser_skip_to_end_of_template_parameter_list
33874 requires it. */
33875 cp_lexer_consume_token (parser->lexer);
33876 return cp_parser_skip_to_end_of_template_parameter_list (parser);
33879 /* Ensure we are at the end of a template parameter list. If we are, return.
33880 If we are not, something has gone wrong, in which case issue an error and
33881 skip to end of the parameter list. */
33883 static void
33884 cp_parser_require_end_of_template_parameter_list (cp_parser* parser)
33886 /* Are we ready, yet? If not, issue error message. */
33887 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
33888 return;
33890 cp_parser_skip_to_end_of_template_parameter_list (parser);
33893 /* You should only call this function from inside a template parameter list
33894 (i.e. the current token should at least be the initial "<" of the
33895 parameter list). If you are skipping the entire list, it may be better to
33896 use cp_parser_skip_entire_template_parameter_list.
33898 Tokens are skipped until the final ">" is found, or if we see
33899 '{', '}', ';', or if we find an unbalanced ')' or ']'.
33901 Returns true if we successfully reached the end, and false if
33902 something unexpected happened (e.g. end of file). */
33904 static bool
33905 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
33907 /* Current level of '< ... >'. */
33908 unsigned level = 0;
33909 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
33910 unsigned nesting_depth = 0;
33912 /* Skip tokens until the desired token is found. */
33913 while (true)
33915 /* Peek at the next token. */
33916 switch (cp_lexer_peek_token (parser->lexer)->type)
33918 case CPP_LESS:
33919 if (!nesting_depth)
33920 ++level;
33921 break;
33923 case CPP_RSHIFT:
33924 if (cxx_dialect == cxx98)
33925 /* C++0x views the `>>' operator as two `>' tokens, but
33926 C++98 does not. */
33927 break;
33928 else if (!nesting_depth && level-- == 0)
33930 /* We've hit a `>>' where the first `>' closes the
33931 template argument list, and the second `>' is
33932 spurious. Just consume the `>>' and stop; we've
33933 already produced at least one error. */
33934 cp_lexer_consume_token (parser->lexer);
33935 return false;
33937 /* Fall through for C++0x, so we handle the second `>' in
33938 the `>>'. */
33939 gcc_fallthrough ();
33941 case CPP_GREATER:
33942 if (!nesting_depth && level-- == 0)
33944 /* We've reached the token we want, consume it and stop. */
33945 cp_lexer_consume_token (parser->lexer);
33946 return true;
33948 break;
33950 case CPP_OPEN_PAREN:
33951 case CPP_OPEN_SQUARE:
33952 ++nesting_depth;
33953 break;
33955 case CPP_CLOSE_PAREN:
33956 case CPP_CLOSE_SQUARE:
33957 if (nesting_depth-- == 0)
33958 return false;
33959 break;
33961 case CPP_EOF:
33962 case CPP_PRAGMA_EOL:
33963 case CPP_SEMICOLON:
33964 case CPP_OPEN_BRACE:
33965 case CPP_CLOSE_BRACE:
33966 /* The '>' was probably forgotten, don't look further. */
33967 return false;
33969 default:
33970 break;
33973 /* Consume this token. */
33974 cp_lexer_consume_token (parser->lexer);
33978 /* If the next token is the indicated keyword, consume it. Otherwise,
33979 issue an error message indicating that TOKEN_DESC was expected.
33981 Returns the token consumed, if the token had the appropriate type.
33982 Otherwise, returns NULL. */
33984 static cp_token *
33985 cp_parser_require_keyword (cp_parser* parser,
33986 enum rid keyword,
33987 required_token token_desc)
33989 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
33991 if (token && token->keyword != keyword)
33993 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
33994 UNKNOWN_LOCATION);
33995 return NULL;
33998 return token;
34001 /* Returns TRUE iff TOKEN is a token that can begin the body of a
34002 function-definition. */
34004 static bool
34005 cp_parser_token_starts_function_definition_p (cp_token* token)
34007 return (/* An ordinary function-body begins with an `{'. */
34008 token->type == CPP_OPEN_BRACE
34009 /* A ctor-initializer begins with a `:'. */
34010 || token->type == CPP_COLON
34011 /* A function-try-block begins with `try'. */
34012 || token->keyword == RID_TRY
34013 /* A function-transaction-block begins with `__transaction_atomic'
34014 or `__transaction_relaxed'. */
34015 || token->keyword == RID_TRANSACTION_ATOMIC
34016 || token->keyword == RID_TRANSACTION_RELAXED
34017 /* The named return value extension begins with `return'. */
34018 || token->keyword == RID_RETURN);
34021 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
34022 definition. */
34024 static bool
34025 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
34027 cp_token *token;
34029 token = cp_lexer_peek_token (parser->lexer);
34030 return (token->type == CPP_OPEN_BRACE
34031 || (token->type == CPP_COLON
34032 && !parser->colon_doesnt_start_class_def_p));
34035 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
34036 C++0x) ending a template-argument. */
34038 static bool
34039 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
34041 cp_token *token;
34043 token = cp_lexer_peek_token (parser->lexer);
34044 return (token->type == CPP_COMMA
34045 || token->type == CPP_GREATER
34046 || token->type == CPP_ELLIPSIS
34047 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)
34048 /* For better diagnostics, treat >>= like that too, that
34049 shouldn't appear non-nested in template arguments. */
34050 || token->type == CPP_RSHIFT_EQ);
34053 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
34054 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
34056 static bool
34057 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
34058 size_t n)
34060 cp_token *token;
34062 token = cp_lexer_peek_nth_token (parser->lexer, n);
34063 if (token->type == CPP_LESS)
34064 return true;
34065 /* Check for the sequence `<::' in the original code. It would be lexed as
34066 `[:', where `[' is a digraph, and there is no whitespace before
34067 `:'. */
34068 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
34070 cp_token *token2;
34071 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
34072 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
34073 return true;
34075 return false;
34078 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
34079 or none_type otherwise. */
34081 static enum tag_types
34082 cp_parser_token_is_class_key (cp_token* token)
34084 switch (token->keyword)
34086 case RID_CLASS:
34087 return class_type;
34088 case RID_STRUCT:
34089 return record_type;
34090 case RID_UNION:
34091 return union_type;
34093 default:
34094 return none_type;
34098 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
34099 or none_type otherwise or if the token is null. */
34101 static enum tag_types
34102 cp_parser_token_is_type_parameter_key (cp_token* token)
34104 if (!token)
34105 return none_type;
34107 switch (token->keyword)
34109 case RID_CLASS:
34110 return class_type;
34111 case RID_TYPENAME:
34112 return typename_type;
34114 default:
34115 return none_type;
34119 /* Diagnose redundant enum-keys. */
34121 static void
34122 cp_parser_maybe_warn_enum_key (cp_parser *parser, location_t key_loc,
34123 tree type, rid scoped_key)
34125 if (!warn_redundant_tags)
34126 return;
34128 tree type_decl = TYPE_MAIN_DECL (type);
34129 tree name = DECL_NAME (type_decl);
34130 /* Look up the NAME to see if it unambiguously refers to the TYPE. */
34131 push_deferring_access_checks (dk_no_check);
34132 tree decl = cp_parser_lookup_name_simple (parser, name, input_location);
34133 pop_deferring_access_checks ();
34135 /* The enum-key is redundant for uses of the TYPE that are not
34136 declarations and for which name lookup returns just the type
34137 itself. */
34138 if (decl != type_decl)
34139 return;
34141 if (scoped_key != RID_CLASS
34142 && scoped_key != RID_STRUCT
34143 && current_lang_name != lang_name_cplusplus
34144 && current_namespace == global_namespace)
34146 /* Avoid issuing the diagnostic for apparently redundant (unscoped)
34147 enum tag in shared C/C++ code in files (such as headers) included
34148 in the main source file. */
34149 const line_map_ordinary *map = NULL;
34150 linemap_resolve_location (line_table, key_loc,
34151 LRK_MACRO_DEFINITION_LOCATION,
34152 &map);
34153 if (!MAIN_FILE_P (map))
34154 return;
34157 gcc_rich_location richloc (key_loc);
34158 richloc.add_fixit_remove (key_loc);
34159 warning_at (&richloc, OPT_Wredundant_tags,
34160 "redundant enum-key %<enum%s%> in reference to %q#T",
34161 (scoped_key == RID_CLASS ? " class"
34162 : scoped_key == RID_STRUCT ? " struct" : ""), type);
34165 /* Describes the set of declarations of a struct, class, or class template
34166 or its specializations. Used for -Wmismatched-tags. */
34168 class class_decl_loc_t
34170 public:
34172 class_decl_loc_t ()
34173 : locvec (), idxdef (), def_class_key ()
34175 locvec.create (4);
34178 /* Constructs an object for a single declaration of a class with
34179 CLASS_KEY at the current location in the current function (or
34180 at another scope). KEY_REDUNDANT is true if the class-key may
34181 be omitted in the current context without an ambiguity with
34182 another symbol with the same name.
34183 DEF_P is true for a class declaration that is a definition.
34184 CURLOC is the associated location. */
34185 class_decl_loc_t (tag_types class_key, bool key_redundant, bool def_p,
34186 location_t curloc = input_location)
34187 : locvec (), idxdef (def_p ? 0 : UINT_MAX), def_class_key (class_key)
34189 locvec.create (4);
34190 class_key_loc_t ckl (current_function_decl, curloc, class_key,
34191 key_redundant);
34192 locvec.quick_push (ckl);
34195 /* Copy, assign, and destroy the object. Necessary because LOCVEC
34196 isn't safely copyable and assignable and doesn't release storage
34197 on its own. */
34198 class_decl_loc_t (const class_decl_loc_t &rhs)
34199 : locvec (rhs.locvec.copy ()), idxdef (rhs.idxdef),
34200 def_class_key (rhs.def_class_key)
34203 class_decl_loc_t& operator= (const class_decl_loc_t &rhs)
34205 if (this == &rhs)
34206 return *this;
34207 locvec.release ();
34208 locvec = rhs.locvec.copy ();
34209 idxdef = rhs.idxdef;
34210 def_class_key = rhs.def_class_key;
34211 return *this;
34214 ~class_decl_loc_t ()
34216 locvec.release ();
34219 /* Issues -Wmismatched-tags for a single class. */
34220 void diag_mismatched_tags (tree);
34222 /* Issues -Wmismatched-tags for all classes. */
34223 static void diag_mismatched_tags ();
34225 /* Adds TYPE_DECL to the collection of class decls and diagnoses
34226 redundant tags (if -Wredundant-tags is enabled). */
34227 static void add (cp_parser *, location_t, tag_types, tree, bool, bool);
34229 /* Either adds this decl to the collection of class decls
34230 or diagnoses it, whichever is appropriate. */
34231 void add_or_diag_mismatched_tag (tree, tag_types, bool, bool);
34233 private:
34235 tree function (unsigned i) const
34237 return locvec[i].func;
34240 location_t location (unsigned i) const
34242 return locvec[i].loc;
34245 bool key_redundant (unsigned i) const
34247 return locvec[i].key_redundant;
34250 tag_types class_key (unsigned i) const
34252 return locvec[i].class_key;
34255 /* True if a definition for the class has been seen. */
34256 bool def_p () const
34258 return idxdef < locvec.length ();
34261 /* The location of a single mention of a class type with the given
34262 class-key. */
34263 struct class_key_loc_t
34265 class_key_loc_t (tree func, location_t loc, tag_types key, bool redundant)
34266 : func (func), loc (loc), class_key (key), key_redundant (redundant)
34269 /* The function the type is mentioned in. */
34270 tree func;
34271 /* The exact location. */
34272 location_t loc;
34273 /* The class-key used in the mention of the type. */
34274 tag_types class_key;
34275 /* True when the class-key could be omitted at this location
34276 without an ambiguity with another symbol of the same name. */
34277 bool key_redundant;
34279 /* Avoid using auto_vec here since it's not safe to copy due to pr90904. */
34280 vec <class_key_loc_t> locvec;
34281 /* LOCVEC index of the definition or UINT_MAX if none exists. */
34282 unsigned idxdef;
34283 /* The class-key the class was last declared with or none_type when
34284 it has been declared with a mismatched key. */
34285 tag_types def_class_key;
34287 /* A mapping between a TYPE_DECL for a class and the class_decl_loc_t
34288 description above. */
34289 typedef hash_map<tree_decl_hash, class_decl_loc_t> class_to_loc_map_t;
34290 static class_to_loc_map_t class2loc;
34293 class_decl_loc_t::class_to_loc_map_t class_decl_loc_t::class2loc;
34295 /* Issue an error message if the CLASS_KEY does not match the TYPE.
34296 DEF_P is expected to be set for a definition of class TYPE. DECL_P
34297 is set for a declaration of class TYPE and clear for a reference to
34298 it that is not a declaration of it. */
34300 static void
34301 cp_parser_check_class_key (cp_parser *parser, location_t key_loc,
34302 tag_types class_key, tree type, bool def_p,
34303 bool decl_p)
34305 if (type == error_mark_node)
34306 return;
34308 bool seen_as_union = TREE_CODE (type) == UNION_TYPE;
34309 if (seen_as_union != (class_key == union_type))
34311 if (permerror (input_location, "%qs tag used in naming %q#T",
34312 class_key == union_type ? "union"
34313 : class_key == record_type ? "struct" : "class",
34314 type))
34315 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
34316 "%q#T was previously declared here", type);
34317 return;
34320 if (!warn_mismatched_tags && !warn_redundant_tags)
34321 return;
34323 /* Only consider the true class-keys below and ignore typename_type,
34324 etc. that are not C++ class-keys. */
34325 if (class_key != class_type
34326 && class_key != record_type
34327 && class_key != union_type)
34328 return;
34330 class_decl_loc_t::add (parser, key_loc, class_key, type, def_p, decl_p);
34333 /* Returns the template or specialization of one to which the RECORD_TYPE
34334 TYPE corresponds. */
34336 static tree
34337 specialization_of (tree type)
34339 tree ret = type;
34341 /* Determine the template or its partial specialization to which TYPE
34342 corresponds. */
34343 if (tree spec = most_specialized_partial_spec (type, tf_none))
34344 if (spec != error_mark_node)
34345 ret = TREE_TYPE (TREE_VALUE (spec));
34347 if (ret == type)
34348 ret = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (type);
34350 return TYPE_MAIN_DECL (ret);
34354 /* Adds the class TYPE to the collection of class decls and diagnoses
34355 redundant tags (if -Wredundant-tags is enabled).
34356 DEF_P is expected to be set for a definition of class TYPE. DECL_P
34357 is set for a (likely, based on syntactic context) declaration of class
34358 TYPE and clear for a reference to it that is not a declaration of it. */
34360 void
34361 class_decl_loc_t::add (cp_parser *parser, location_t key_loc,
34362 tag_types class_key, tree type, bool def_p, bool decl_p)
34364 tree type_decl = TYPE_MAIN_DECL (type);
34365 tree name = DECL_NAME (type_decl);
34366 /* Look up the NAME to see if it unambiguously refers to the TYPE
34367 and set KEY_REDUNDANT if so. */
34368 push_deferring_access_checks (dk_no_check);
34369 tree decl = cp_parser_lookup_name_simple (parser, name, input_location);
34370 pop_deferring_access_checks ();
34372 /* The class-key is redundant for uses of the CLASS_TYPE that are
34373 neither definitions of it nor declarations, and for which name
34374 lookup returns just the type itself. */
34375 bool key_redundant = (!def_p && !decl_p
34376 && (decl == type_decl
34377 || TREE_CODE (decl) == TEMPLATE_DECL
34378 || (CLASS_TYPE_P (type)
34379 && TYPE_BEING_DEFINED (type))));
34381 if (key_redundant
34382 && class_key != class_type
34383 && current_lang_name != lang_name_cplusplus
34384 && current_namespace == global_namespace)
34386 /* Avoid issuing the diagnostic for apparently redundant struct
34387 and union class-keys in shared C/C++ code in files (such as
34388 headers) included in the main source file. */
34389 const line_map_ordinary *map = NULL;
34390 linemap_resolve_location (line_table, key_loc,
34391 LRK_MACRO_DEFINITION_LOCATION,
34392 &map);
34393 if (!MAIN_FILE_P (map))
34394 key_redundant = false;
34397 /* Set if a declaration of TYPE has previously been seen or if it must
34398 exist in a precompiled header. */
34399 bool exist;
34400 class_decl_loc_t *rdl = &class2loc.get_or_insert (type_decl, &exist);
34401 if (!exist)
34403 tree type = TREE_TYPE (type_decl);
34404 if (def_p || !COMPLETE_TYPE_P (type))
34406 /* TYPE_DECL is the first declaration or definition of the type
34407 (outside precompiled headers -- see below). Just create
34408 a new entry for it and return unless it's a declaration
34409 involving a template that may need to be diagnosed by
34410 -Wredundant-tags. */
34411 *rdl = class_decl_loc_t (class_key, false, def_p);
34412 if (TREE_CODE (decl) != TEMPLATE_DECL)
34413 return;
34415 else
34417 /* TYPE was previously defined in some unknown precompiled header.
34418 Simply add a record of its definition at an unknown location and
34419 proceed below to add a reference to it at the current location.
34420 (Declarations in precompiled headers that are not definitions
34421 are ignored.) */
34422 tag_types def_key
34423 = CLASSTYPE_DECLARED_CLASS (type) ? class_type : record_type;
34424 location_t def_loc = DECL_SOURCE_LOCATION (type_decl);
34425 *rdl = class_decl_loc_t (def_key, false, true, def_loc);
34426 exist = true;
34430 /* A prior declaration of TYPE_DECL has been seen. */
34432 if (key_redundant)
34434 gcc_rich_location richloc (key_loc);
34435 richloc.add_fixit_remove (key_loc);
34436 warning_at (&richloc, OPT_Wredundant_tags,
34437 "redundant class-key %qs in reference to %q#T",
34438 class_key == union_type ? "union"
34439 : class_key == record_type ? "struct" : "class",
34440 type);
34443 if (!exist)
34444 /* Do nothing if this is the first declaration of the type. */
34445 return;
34447 if (rdl->idxdef != UINT_MAX && rdl->def_class_key == class_key)
34448 /* Do nothing if the class-key in this declaration matches
34449 the definition. */
34450 return;
34452 rdl->add_or_diag_mismatched_tag (type_decl, class_key, key_redundant,
34453 def_p);
34456 /* Either adds this DECL corresponding to the TYPE_DECL to the collection
34457 of class decls or diagnoses it, whichever is appropriate. */
34459 void
34460 class_decl_loc_t::add_or_diag_mismatched_tag (tree type_decl,
34461 tag_types class_key,
34462 bool redundant,
34463 bool def_p)
34465 /* Reset the CLASS_KEY associated with this type on mismatch.
34466 This is an optimization that lets the diagnostic code skip
34467 over classes that use the same class-key in all declarations. */
34468 if (def_class_key != class_key)
34469 def_class_key = none_type;
34471 /* Set IDXDEF to the index of the vector corresponding to
34472 the definition. */
34473 if (def_p)
34474 idxdef = locvec.length ();
34476 /* Append a record of this declaration to the vector. */
34477 class_key_loc_t ckl (current_function_decl, input_location, class_key,
34478 redundant);
34479 locvec.safe_push (ckl);
34481 if (idxdef == UINT_MAX)
34482 return;
34484 /* As a space optimization diagnose declarations of a class
34485 whose definition has been seen and purge the LOCVEC of
34486 all entries except the definition. */
34487 diag_mismatched_tags (type_decl);
34488 if (idxdef)
34490 class_decl_loc_t::class_key_loc_t ent = locvec[idxdef];
34491 locvec.release ();
34492 locvec.reserve (2);
34493 locvec.safe_push (ent);
34494 idxdef = 0;
34496 else
34497 /* Pop the entry pushed above for this declaration. */
34498 locvec.pop ();
34501 /* Issues -Wmismatched-tags for a single class. */
34503 void
34504 class_decl_loc_t::diag_mismatched_tags (tree type_decl)
34506 if (!warn_mismatched_tags)
34507 return;
34509 /* Number of uses of the class. */
34510 const unsigned ndecls = locvec.length ();
34512 /* The class (or template) declaration guiding the decisions about
34513 the diagnostic. For ordinary classes it's the same as THIS. For
34514 uses of instantiations of templates other than their declarations
34515 it points to the record for the declaration of the corresponding
34516 primary template or partial specialization. */
34517 class_decl_loc_t *cdlguide = this;
34519 tree type = TREE_TYPE (type_decl);
34520 if (CLASS_TYPE_P (type) && CLASSTYPE_IMPLICIT_INSTANTIATION (type))
34522 /* For implicit instantiations of a primary template look up
34523 the primary or partial specialization and use it as
34524 the expected class-key rather than using the class-key of
34525 the first reference to the instantiation. The primary must
34526 be (and inevitably is) at index zero. */
34527 tree spec = specialization_of (type);
34528 cdlguide = class2loc.get (spec);
34529 /* It's possible that we didn't find SPEC. Consider:
34531 template<typename T> struct A {
34532 template<typename U> struct W { };
34534 struct A<int>::W<int> w; // #1
34536 where while parsing A and #1 we've stashed
34537 A<T>
34538 A<T>::W<U>
34539 A<int>::W<int>
34540 into CLASS2LOC. If TYPE is A<int>::W<int>, specialization_of
34541 will yield A<int>::W<U> which may be in CLASS2LOC if we had
34542 an A<int> class specialization, but otherwise won't be in it.
34543 So try to look up A<T>::W<U>. */
34544 if (!cdlguide)
34546 spec = DECL_TEMPLATE_RESULT (most_general_template (spec));
34547 cdlguide = class2loc.get (spec);
34549 /* Now we really should have found something. */
34550 gcc_assert (cdlguide != NULL);
34552 /* Skip declarations that consistently use the same class-key. */
34553 else if (def_class_key != none_type)
34554 return;
34556 /* Set if a definition for the class has been seen. */
34557 const bool def_p = cdlguide->def_p ();
34559 /* The index of the declaration whose class-key this declaration
34560 is expected to match. It's either the class-key of the class
34561 definition if one exists or the first declaration otherwise. */
34562 const unsigned idxguide = def_p ? cdlguide->idxdef : 0;
34564 /* The class-key the class is expected to be declared with: it's
34565 either the key used in its definition or the first declaration
34566 if no definition has been provided.
34567 For implicit instantiations of a primary template it's
34568 the class-key used to declare the primary with. The primary
34569 must be at index zero. */
34570 const tag_types xpect_key = cdlguide->class_key (idxguide);
34572 unsigned idx = 0;
34573 /* Advance IDX to the first declaration that either is not
34574 a definition or that doesn't match the first declaration
34575 if no definition is provided. */
34576 while (class_key (idx) == xpect_key)
34577 if (++idx == ndecls)
34578 return;
34580 /* Save the current function before changing it below. */
34581 tree save_func = current_function_decl;
34582 /* Set the function declaration to print in diagnostic context. */
34583 current_function_decl = function (idx);
34585 const char *xmatchkstr = xpect_key == record_type ? "class" : "struct";
34586 const char *xpectkstr = xpect_key == record_type ? "struct" : "class";
34588 location_t loc = location (idx);
34589 bool key_redundant_p = key_redundant (idx);
34590 auto_diagnostic_group d;
34591 /* Issue a warning for the first mismatched declaration.
34592 Avoid using "%#qT" since the class-key for the same type will
34593 be the same regardless of which one was used in the declaraion. */
34594 if (warning_at (loc, OPT_Wmismatched_tags,
34595 "%qT declared with a mismatched class-key %qs",
34596 type_decl, xmatchkstr))
34598 /* Suggest how to avoid the warning for each instance since
34599 the guidance may be different depending on context. */
34600 inform (loc,
34601 (key_redundant_p
34602 ? G_("remove the class-key or replace it with %qs")
34603 : G_("replace the class-key with %qs")),
34604 xpectkstr);
34606 /* Also point to the first declaration or definition that guided
34607 the decision to issue the warning above. */
34608 inform (cdlguide->location (idxguide),
34609 (def_p
34610 ? G_("%qT defined as %qs here")
34611 : G_("%qT first declared as %qs here")),
34612 type_decl, xpectkstr);
34615 /* Issue warnings for the remaining inconsistent declarations. */
34616 for (unsigned i = idx + 1; i != ndecls; ++i)
34618 tag_types clskey = class_key (i);
34619 /* Skip over the declarations that match either the definition
34620 if one was provided or the first declaration. */
34621 if (clskey == xpect_key)
34622 continue;
34624 loc = location (i);
34625 key_redundant_p = key_redundant (i);
34626 /* Set the function declaration to print in diagnostic context. */
34627 current_function_decl = function (i);
34628 if (warning_at (loc, OPT_Wmismatched_tags,
34629 "%qT declared with a mismatched class-key %qs",
34630 type_decl, xmatchkstr))
34631 /* Suggest how to avoid the warning for each instance since
34632 the guidance may be different depending on context. */
34633 inform (loc,
34634 (key_redundant_p
34635 ? G_("remove the class-key or replace it with %qs")
34636 : G_("replace the class-key with %qs")),
34637 xpectkstr);
34640 /* Restore the current function in case it was replaced above. */
34641 current_function_decl = save_func;
34644 /* Issues -Wmismatched-tags for all classes. Called at the end
34645 of processing a translation unit, after declarations of all class
34646 types and their uses have been recorded. */
34648 void
34649 class_decl_loc_t::diag_mismatched_tags ()
34651 /* CLASS2LOC should be empty if both -Wmismatched-tags and
34652 -Wredundant-tags are disabled. */
34653 gcc_assert (warn_mismatched_tags
34654 || warn_redundant_tags
34655 || class2loc.is_empty ());
34657 /* Save the current function before changing on return. It should
34658 be null at this point. */
34659 temp_override<tree> cleanup (current_function_decl);
34661 if (warn_mismatched_tags)
34663 /* Iterate over the collected class/struct/template declarations. */
34664 typedef class_to_loc_map_t::iterator iter_t;
34665 for (iter_t it = class2loc.begin (); it != class2loc.end (); ++it)
34667 tree type_decl = (*it).first;
34668 class_decl_loc_t &recloc = (*it).second;
34669 recloc.diag_mismatched_tags (type_decl);
34673 class2loc.empty ();
34676 /* Issue an error message if DECL is redeclared with different
34677 access than its original declaration [class.access.spec/3].
34678 This applies to nested classes, nested class templates and
34679 enumerations [class.mem/1]. */
34681 static void
34682 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
34684 if (!decl
34685 || (!CLASS_TYPE_P (TREE_TYPE (decl))
34686 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
34687 return;
34689 if ((TREE_PRIVATE (decl)
34690 != (current_access_specifier == access_private_node))
34691 || (TREE_PROTECTED (decl)
34692 != (current_access_specifier == access_protected_node)))
34693 error_at (location, "%qD redeclared with different access", decl);
34696 /* Look for the `template' keyword, as a syntactic disambiguator.
34697 Return TRUE iff it is present, in which case it will be
34698 consumed. */
34700 static bool
34701 cp_parser_optional_template_keyword (cp_parser *parser)
34703 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
34705 /* In C++98 the `template' keyword can only be used within templates;
34706 outside templates the parser can always figure out what is a
34707 template and what is not. In C++11, per the resolution of DR 468,
34708 `template' is allowed in cases where it is not strictly necessary. */
34709 if (!processing_template_decl
34710 && pedantic && cxx_dialect == cxx98)
34712 cp_token *token = cp_lexer_peek_token (parser->lexer);
34713 pedwarn (token->location, OPT_Wpedantic,
34714 "in C++98 %<template%> (as a disambiguator) is only "
34715 "allowed within templates");
34716 /* If this part of the token stream is rescanned, the same
34717 error message would be generated. So, we purge the token
34718 from the stream. */
34719 cp_lexer_purge_token (parser->lexer);
34720 return false;
34722 else
34724 /* Consume the `template' keyword. */
34725 cp_lexer_consume_token (parser->lexer);
34726 return true;
34729 return false;
34732 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
34733 set PARSER->SCOPE, and perform other related actions. */
34735 static void
34736 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
34738 struct tree_check *check_value;
34740 /* Get the stored value. */
34741 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
34742 /* Set the scope from the stored value. */
34743 parser->scope = saved_checks_value (check_value);
34744 parser->qualifying_scope = check_value->qualifying_scope;
34745 parser->object_scope = parser->context->object_type;
34746 parser->context->object_type = NULL_TREE;
34749 /* Consume tokens up through a non-nested END token. Returns TRUE if we
34750 encounter the end of a block before what we were looking for. */
34752 static bool
34753 cp_parser_cache_group (cp_parser *parser,
34754 enum cpp_ttype end,
34755 unsigned depth)
34757 while (true)
34759 cp_token *token = cp_lexer_peek_token (parser->lexer);
34761 /* Abort a parenthesized expression if we encounter a semicolon. */
34762 if ((end == CPP_CLOSE_PAREN || depth == 0)
34763 && token->type == CPP_SEMICOLON)
34764 return true;
34765 /* If we've reached the end of the file, stop. */
34766 if (token->type == CPP_EOF
34767 || (end != CPP_PRAGMA_EOL
34768 && token->type == CPP_PRAGMA_EOL))
34769 return true;
34770 if (token->type == CPP_CLOSE_BRACE && depth == 0)
34771 /* We've hit the end of an enclosing block, so there's been some
34772 kind of syntax error. */
34773 return true;
34775 /* Consume the token. */
34776 cp_lexer_consume_token (parser->lexer);
34777 /* See if it starts a new group. */
34778 if (token->type == CPP_OPEN_BRACE)
34780 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
34781 /* In theory this should probably check end == '}', but
34782 cp_parser_save_member_function_body needs it to exit
34783 after either '}' or ')' when called with ')'. */
34784 if (depth == 0)
34785 return false;
34787 else if (token->type == CPP_OPEN_PAREN)
34789 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
34790 if (depth == 0 && end == CPP_CLOSE_PAREN)
34791 return false;
34793 else if (token->type == CPP_PRAGMA)
34794 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
34795 else if (token->type == end)
34796 return false;
34800 /* Like above, for caching a default argument or NSDMI. Both of these are
34801 terminated by a non-nested comma, but it can be unclear whether or not a
34802 comma is nested in a template argument list unless we do more parsing.
34803 In order to handle this ambiguity, when we encounter a ',' after a '<'
34804 we try to parse what follows as a parameter-declaration-list (in the
34805 case of a default argument) or a member-declarator (in the case of an
34806 NSDMI). If that succeeds, then we stop caching. */
34808 static tree
34809 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
34811 unsigned depth = 0;
34812 int maybe_template_id = 0;
34813 cp_token *first_token;
34814 cp_token *token;
34815 tree default_argument;
34817 /* Add tokens until we have processed the entire default
34818 argument. We add the range [first_token, token). */
34819 first_token = cp_lexer_peek_token (parser->lexer);
34820 if (first_token->type == CPP_OPEN_BRACE)
34822 /* For list-initialization, this is straightforward. */
34823 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
34824 token = cp_lexer_peek_token (parser->lexer);
34826 else while (true)
34828 bool done = false;
34830 /* Peek at the next token. */
34831 token = cp_lexer_peek_token (parser->lexer);
34832 /* What we do depends on what token we have. */
34833 switch (token->type)
34835 /* In valid code, a default argument must be
34836 immediately followed by a `,' `)', or `...'. */
34837 case CPP_COMMA:
34838 if (depth == 0 && maybe_template_id)
34840 /* If we've seen a '<', we might be in a
34841 template-argument-list. Until Core issue 325 is
34842 resolved, we don't know how this situation ought
34843 to be handled, so try to DTRT. We check whether
34844 what comes after the comma is a valid parameter
34845 declaration list. If it is, then the comma ends
34846 the default argument; otherwise the default
34847 argument continues. */
34848 bool error = false;
34849 cp_token *peek;
34851 /* Set ITALP so cp_parser_parameter_declaration_list
34852 doesn't decide to commit to this parse. */
34853 bool saved_italp = parser->in_template_argument_list_p;
34854 parser->in_template_argument_list_p = true;
34856 cp_parser_parse_tentatively (parser);
34858 if (nsdmi)
34860 /* Parse declarators until we reach a non-comma or
34861 somthing that cannot be an initializer.
34862 Just checking whether we're looking at a single
34863 declarator is insufficient. Consider:
34864 int var = tuple<T,U>::x;
34865 The template parameter 'U' looks exactly like a
34866 declarator. */
34869 int ctor_dtor_or_conv_p;
34870 cp_lexer_consume_token (parser->lexer);
34871 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
34872 CP_PARSER_FLAGS_NONE,
34873 &ctor_dtor_or_conv_p,
34874 /*parenthesized_p=*/NULL,
34875 /*member_p=*/true,
34876 /*friend_p=*/false,
34877 /*static_p=*/false);
34878 peek = cp_lexer_peek_token (parser->lexer);
34879 if (cp_parser_error_occurred (parser))
34880 break;
34882 while (peek->type == CPP_COMMA);
34883 /* If we met an '=' or ';' then the original comma
34884 was the end of the NSDMI. Otherwise assume
34885 we're still in the NSDMI. */
34886 error = (peek->type != CPP_EQ
34887 && peek->type != CPP_SEMICOLON);
34889 else
34891 cp_lexer_consume_token (parser->lexer);
34892 begin_scope (sk_function_parms, NULL_TREE);
34893 tree t = cp_parser_parameter_declaration_list
34894 (parser, CP_PARSER_FLAGS_NONE,
34895 /*pending_decls*/nullptr);
34896 if (t == error_mark_node)
34897 error = true;
34898 pop_bindings_and_leave_scope ();
34900 if (!cp_parser_error_occurred (parser) && !error)
34901 done = true;
34902 cp_parser_abort_tentative_parse (parser);
34904 parser->in_template_argument_list_p = saved_italp;
34905 break;
34907 /* FALLTHRU */
34908 case CPP_CLOSE_PAREN:
34909 case CPP_ELLIPSIS:
34910 /* If we run into a non-nested `;', `}', or `]',
34911 then the code is invalid -- but the default
34912 argument is certainly over. */
34913 case CPP_SEMICOLON:
34914 case CPP_CLOSE_BRACE:
34915 case CPP_CLOSE_SQUARE:
34916 if (depth == 0
34917 /* Handle correctly int n = sizeof ... ( p ); */
34918 && token->type != CPP_ELLIPSIS)
34919 done = true;
34920 /* Update DEPTH, if necessary. */
34921 else if (token->type == CPP_CLOSE_PAREN
34922 || token->type == CPP_CLOSE_BRACE
34923 || token->type == CPP_CLOSE_SQUARE)
34924 --depth;
34925 break;
34927 case CPP_OPEN_PAREN:
34928 case CPP_OPEN_SQUARE:
34929 case CPP_OPEN_BRACE:
34930 ++depth;
34931 break;
34933 case CPP_LESS:
34934 if (depth == 0)
34935 /* This might be the comparison operator, or it might
34936 start a template argument list. */
34937 ++maybe_template_id;
34938 break;
34940 case CPP_RSHIFT:
34941 if (cxx_dialect == cxx98)
34942 break;
34943 /* Fall through for C++0x, which treats the `>>'
34944 operator like two `>' tokens in certain
34945 cases. */
34946 gcc_fallthrough ();
34948 case CPP_GREATER:
34949 if (depth == 0)
34951 /* This might be an operator, or it might close a
34952 template argument list. But if a previous '<'
34953 started a template argument list, this will have
34954 closed it, so we can't be in one anymore. */
34955 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
34956 if (maybe_template_id < 0)
34957 maybe_template_id = 0;
34959 break;
34961 /* If we run out of tokens, issue an error message. */
34962 case CPP_EOF:
34963 case CPP_PRAGMA_EOL:
34964 error_at (token->location, "file ends in default argument");
34965 return error_mark_node;
34967 case CPP_NAME:
34968 case CPP_SCOPE:
34969 /* In these cases, we should look for template-ids.
34970 For example, if the default argument is
34971 `X<int, double>()', we need to do name lookup to
34972 figure out whether or not `X' is a template; if
34973 so, the `,' does not end the default argument.
34975 That is not yet done. */
34976 break;
34978 default:
34979 break;
34982 /* If we've reached the end, stop. */
34983 if (done)
34984 break;
34986 /* Add the token to the token block. */
34987 token = cp_lexer_consume_token (parser->lexer);
34990 /* Create a DEFERRED_PARSE to represent the unparsed default
34991 argument. */
34992 default_argument = make_node (DEFERRED_PARSE);
34993 DEFPARSE_TOKENS (default_argument)
34994 = cp_token_cache_new (first_token, token);
34995 DEFPARSE_INSTANTIATIONS (default_argument) = NULL;
34997 return default_argument;
35000 /* A location to use for diagnostics about an unparsed DEFERRED_PARSE. */
35002 location_t
35003 defparse_location (tree default_argument)
35005 cp_token_cache *tokens = DEFPARSE_TOKENS (default_argument);
35006 location_t start = tokens->first->location;
35007 location_t end = tokens->last->location;
35008 return make_location (start, start, end);
35011 /* Begin parsing tentatively. We always save tokens while parsing
35012 tentatively so that if the tentative parsing fails we can restore the
35013 tokens. */
35015 static void
35016 cp_parser_parse_tentatively (cp_parser* parser)
35018 /* Enter a new parsing context. */
35019 parser->context = cp_parser_context_new (parser->context);
35020 /* Begin saving tokens. */
35021 cp_lexer_save_tokens (parser->lexer);
35022 /* In order to avoid repetitive access control error messages,
35023 access checks are queued up until we are no longer parsing
35024 tentatively. */
35025 push_deferring_access_checks (dk_deferred);
35028 /* Commit to the currently active tentative parse. */
35030 static void
35031 cp_parser_commit_to_tentative_parse (cp_parser* parser)
35033 cp_parser_context *context;
35034 cp_lexer *lexer;
35036 /* Mark all of the levels as committed. */
35037 lexer = parser->lexer;
35038 for (context = parser->context; context->next; context = context->next)
35040 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
35041 break;
35042 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
35043 while (!cp_lexer_saving_tokens (lexer))
35044 lexer = lexer->next;
35045 cp_lexer_commit_tokens (lexer);
35049 /* Commit to the topmost currently active tentative parse.
35051 Note that this function shouldn't be called when there are
35052 irreversible side-effects while in a tentative state. For
35053 example, we shouldn't create a permanent entry in the symbol
35054 table, or issue an error message that might not apply if the
35055 tentative parse is aborted. */
35057 static void
35058 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
35060 cp_parser_context *context = parser->context;
35061 cp_lexer *lexer = parser->lexer;
35063 if (context)
35065 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
35066 return;
35067 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
35069 while (!cp_lexer_saving_tokens (lexer))
35070 lexer = lexer->next;
35071 cp_lexer_commit_tokens (lexer);
35075 /* Abort the currently active tentative parse. All consumed tokens
35076 will be rolled back, and no diagnostics will be issued. */
35078 static void
35079 cp_parser_abort_tentative_parse (cp_parser* parser)
35081 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
35082 || errorcount > 0);
35083 cp_parser_simulate_error (parser);
35084 /* Now, pretend that we want to see if the construct was
35085 successfully parsed. */
35086 cp_parser_parse_definitely (parser);
35089 /* Stop parsing tentatively. If a parse error has occurred, restore the
35090 token stream. Otherwise, commit to the tokens we have consumed.
35091 Returns true if no error occurred; false otherwise. */
35093 static bool
35094 cp_parser_parse_definitely (cp_parser* parser)
35096 bool error_occurred;
35097 cp_parser_context *context;
35099 /* Remember whether or not an error occurred, since we are about to
35100 destroy that information. */
35101 error_occurred = cp_parser_error_occurred (parser);
35102 /* Remove the topmost context from the stack. */
35103 context = parser->context;
35104 parser->context = context->next;
35105 /* If no parse errors occurred, commit to the tentative parse. */
35106 if (!error_occurred)
35108 /* Commit to the tokens read tentatively, unless that was
35109 already done. */
35110 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
35111 cp_lexer_commit_tokens (parser->lexer);
35113 pop_to_parent_deferring_access_checks ();
35115 /* Otherwise, if errors occurred, roll back our state so that things
35116 are just as they were before we began the tentative parse. */
35117 else
35119 cp_lexer_rollback_tokens (parser->lexer);
35120 pop_deferring_access_checks ();
35122 /* Add the context to the front of the free list. */
35123 context->next = cp_parser_context_free_list;
35124 cp_parser_context_free_list = context;
35126 return !error_occurred;
35129 /* Returns true if we are parsing tentatively and are not committed to
35130 this tentative parse. */
35132 static bool
35133 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
35135 return (cp_parser_parsing_tentatively (parser)
35136 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
35139 /* Returns nonzero iff an error has occurred during the most recent
35140 tentative parse. */
35142 static bool
35143 cp_parser_error_occurred (cp_parser* parser)
35145 return (cp_parser_parsing_tentatively (parser)
35146 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
35149 /* Returns nonzero if GNU extensions are allowed. */
35151 static bool
35152 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
35154 return parser->allow_gnu_extensions_p;
35157 /* Objective-C++ Productions */
35160 /* Parse an Objective-C expression, which feeds into a primary-expression
35161 above.
35163 objc-expression:
35164 objc-message-expression
35165 objc-string-literal
35166 objc-encode-expression
35167 objc-protocol-expression
35168 objc-selector-expression
35170 Returns a tree representation of the expression. */
35172 static cp_expr
35173 cp_parser_objc_expression (cp_parser* parser)
35175 /* Try to figure out what kind of declaration is present. */
35176 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
35178 switch (kwd->type)
35180 case CPP_OPEN_SQUARE:
35181 return cp_parser_objc_message_expression (parser);
35183 case CPP_OBJC_STRING:
35184 kwd = cp_lexer_consume_token (parser->lexer);
35185 return objc_build_string_object (kwd->u.value);
35187 case CPP_KEYWORD:
35188 switch (kwd->keyword)
35190 case RID_AT_ENCODE:
35191 return cp_parser_objc_encode_expression (parser);
35193 case RID_AT_PROTOCOL:
35194 return cp_parser_objc_protocol_expression (parser);
35196 case RID_AT_SELECTOR:
35197 return cp_parser_objc_selector_expression (parser);
35199 default:
35200 break;
35202 /* FALLTHRU */
35203 default:
35204 error_at (kwd->location,
35205 "misplaced %<@%D%> Objective-C++ construct",
35206 kwd->u.value);
35207 cp_parser_skip_to_end_of_block_or_statement (parser);
35210 return error_mark_node;
35213 /* Parse an Objective-C message expression.
35215 objc-message-expression:
35216 [ objc-message-receiver objc-message-args ]
35218 Returns a representation of an Objective-C message. */
35220 static tree
35221 cp_parser_objc_message_expression (cp_parser* parser)
35223 tree receiver, messageargs;
35225 parser->objective_c_message_context_p = true;
35226 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
35227 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
35228 receiver = cp_parser_objc_message_receiver (parser);
35229 messageargs = cp_parser_objc_message_args (parser);
35230 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
35231 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
35233 tree result = objc_build_message_expr (receiver, messageargs);
35235 /* Construct a location e.g.
35236 [self func1:5]
35237 ^~~~~~~~~~~~~~
35238 ranging from the '[' to the ']', with the caret at the start. */
35239 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
35240 protected_set_expr_location (result, combined_loc);
35242 parser->objective_c_message_context_p = false;
35243 return result;
35246 /* Parse an objc-message-receiver.
35248 objc-message-receiver:
35249 expression
35250 simple-type-specifier
35252 Returns a representation of the type or expression. */
35254 static tree
35255 cp_parser_objc_message_receiver (cp_parser* parser)
35257 tree rcv;
35259 /* An Objective-C message receiver may be either (1) a type
35260 or (2) an expression. */
35261 cp_parser_parse_tentatively (parser);
35262 rcv = cp_parser_expression (parser);
35264 /* If that worked out, fine. */
35265 if (cp_parser_parse_definitely (parser))
35266 return rcv;
35268 cp_parser_parse_tentatively (parser);
35269 rcv = cp_parser_simple_type_specifier (parser,
35270 /*decl_specs=*/NULL,
35271 CP_PARSER_FLAGS_NONE);
35273 if (cp_parser_parse_definitely (parser))
35274 return objc_get_class_reference (rcv);
35276 cp_parser_error (parser, "objective-c++ message receiver expected");
35277 return error_mark_node;
35280 /* Parse the arguments and selectors comprising an Objective-C message.
35282 objc-message-args:
35283 objc-selector
35284 objc-selector-args
35285 objc-selector-args , objc-comma-args
35287 objc-selector-args:
35288 objc-selector [opt] : assignment-expression
35289 objc-selector-args objc-selector [opt] : assignment-expression
35291 objc-comma-args:
35292 assignment-expression
35293 objc-comma-args , assignment-expression
35295 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
35296 selector arguments and TREE_VALUE containing a list of comma
35297 arguments. */
35299 static tree
35300 cp_parser_objc_message_args (cp_parser* parser)
35302 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
35303 bool maybe_unary_selector_p = true;
35304 cp_token *token = cp_lexer_peek_token (parser->lexer);
35306 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
35308 tree selector = NULL_TREE, arg;
35310 if (token->type != CPP_COLON)
35311 selector = cp_parser_objc_selector (parser);
35313 /* Detect if we have a unary selector. */
35314 if (maybe_unary_selector_p
35315 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
35316 return build_tree_list (selector, NULL_TREE);
35318 maybe_unary_selector_p = false;
35319 cp_parser_require (parser, CPP_COLON, RT_COLON);
35320 arg = cp_parser_assignment_expression (parser);
35322 sel_args
35323 = chainon (sel_args,
35324 build_tree_list (selector, arg));
35326 token = cp_lexer_peek_token (parser->lexer);
35329 /* Handle non-selector arguments, if any. */
35330 while (token->type == CPP_COMMA)
35332 tree arg;
35334 cp_lexer_consume_token (parser->lexer);
35335 arg = cp_parser_assignment_expression (parser);
35337 addl_args
35338 = chainon (addl_args,
35339 build_tree_list (NULL_TREE, arg));
35341 token = cp_lexer_peek_token (parser->lexer);
35344 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
35346 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
35347 return build_tree_list (error_mark_node, error_mark_node);
35350 return build_tree_list (sel_args, addl_args);
35353 /* Parse an Objective-C encode expression.
35355 objc-encode-expression:
35356 @encode objc-typename
35358 Returns an encoded representation of the type argument. */
35360 static cp_expr
35361 cp_parser_objc_encode_expression (cp_parser* parser)
35363 tree type;
35364 cp_token *token;
35365 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
35367 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
35368 matching_parens parens;
35369 parens.require_open (parser);
35370 token = cp_lexer_peek_token (parser->lexer);
35371 type = complete_type (cp_parser_type_id (parser));
35372 parens.require_close (parser);
35374 if (!type)
35376 error_at (token->location,
35377 "%<@encode%> must specify a type as an argument");
35378 return error_mark_node;
35381 /* This happens if we find @encode(T) (where T is a template
35382 typename or something dependent on a template typename) when
35383 parsing a template. In that case, we can't compile it
35384 immediately, but we rather create an AT_ENCODE_EXPR which will
35385 need to be instantiated when the template is used.
35387 if (dependent_type_p (type))
35389 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
35390 TREE_READONLY (value) = 1;
35391 return value;
35395 /* Build a location of the form:
35396 @encode(int)
35397 ^~~~~~~~~~~~
35398 with caret==start at the @ token, finishing at the close paren. */
35399 location_t combined_loc = make_location (start_loc, start_loc, parser->lexer);
35401 return cp_expr (objc_build_encode_expr (type), combined_loc);
35404 /* Parse an Objective-C @defs expression. */
35406 static tree
35407 cp_parser_objc_defs_expression (cp_parser *parser)
35409 tree name;
35411 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
35412 matching_parens parens;
35413 parens.require_open (parser);
35414 name = cp_parser_identifier (parser);
35415 parens.require_close (parser);
35417 return objc_get_class_ivars (name);
35420 /* Parse an Objective-C protocol expression.
35422 objc-protocol-expression:
35423 @protocol ( identifier )
35425 Returns a representation of the protocol expression. */
35427 static tree
35428 cp_parser_objc_protocol_expression (cp_parser* parser)
35430 tree proto;
35431 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
35433 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
35434 matching_parens parens;
35435 parens.require_open (parser);
35436 proto = cp_parser_identifier (parser);
35437 parens.require_close (parser);
35439 /* Build a location of the form:
35440 @protocol(prot)
35441 ^~~~~~~~~~~~~~~
35442 with caret==start at the @ token, finishing at the close paren. */
35443 location_t combined_loc = make_location (start_loc, start_loc, parser->lexer);
35444 tree result = objc_build_protocol_expr (proto);
35445 protected_set_expr_location (result, combined_loc);
35446 return result;
35449 /* Parse an Objective-C selector expression.
35451 objc-selector-expression:
35452 @selector ( objc-method-signature )
35454 objc-method-signature:
35455 objc-selector
35456 objc-selector-seq
35458 objc-selector-seq:
35459 objc-selector :
35460 objc-selector-seq objc-selector :
35462 Returns a representation of the method selector. */
35464 static tree
35465 cp_parser_objc_selector_expression (cp_parser* parser)
35467 tree sel_seq = NULL_TREE;
35468 bool maybe_unary_selector_p = true;
35469 cp_token *token;
35470 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35472 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
35473 matching_parens parens;
35474 parens.require_open (parser);
35475 token = cp_lexer_peek_token (parser->lexer);
35477 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
35478 || token->type == CPP_SCOPE)
35480 tree selector = NULL_TREE;
35482 if (token->type != CPP_COLON
35483 || token->type == CPP_SCOPE)
35484 selector = cp_parser_objc_selector (parser);
35486 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
35487 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
35489 /* Detect if we have a unary selector. */
35490 if (maybe_unary_selector_p)
35492 sel_seq = selector;
35493 goto finish_selector;
35495 else
35497 cp_parser_error (parser, "expected %<:%>");
35500 maybe_unary_selector_p = false;
35501 token = cp_lexer_consume_token (parser->lexer);
35503 if (token->type == CPP_SCOPE)
35505 sel_seq
35506 = chainon (sel_seq,
35507 build_tree_list (selector, NULL_TREE));
35508 sel_seq
35509 = chainon (sel_seq,
35510 build_tree_list (NULL_TREE, NULL_TREE));
35512 else
35513 sel_seq
35514 = chainon (sel_seq,
35515 build_tree_list (selector, NULL_TREE));
35517 token = cp_lexer_peek_token (parser->lexer);
35520 finish_selector:
35521 parens.require_close (parser);
35524 /* Build a location of the form:
35525 @selector(func)
35526 ^~~~~~~~~~~~~~~
35527 with caret==start at the @ token, finishing at the close paren. */
35528 location_t combined_loc = make_location (loc, loc, parser->lexer);
35529 tree result = objc_build_selector_expr (combined_loc, sel_seq);
35530 /* TODO: objc_build_selector_expr doesn't always honor the location. */
35531 protected_set_expr_location (result, combined_loc);
35532 return result;
35535 /* Parse a list of identifiers.
35537 objc-identifier-list:
35538 identifier
35539 objc-identifier-list , identifier
35541 Returns a TREE_LIST of identifier nodes. */
35543 static tree
35544 cp_parser_objc_identifier_list (cp_parser* parser)
35546 tree identifier;
35547 tree list;
35548 cp_token *sep;
35550 identifier = cp_parser_identifier (parser);
35551 if (identifier == error_mark_node)
35552 return error_mark_node;
35554 list = build_tree_list (NULL_TREE, identifier);
35555 sep = cp_lexer_peek_token (parser->lexer);
35557 while (sep->type == CPP_COMMA)
35559 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
35560 identifier = cp_parser_identifier (parser);
35561 if (identifier == error_mark_node)
35562 return list;
35564 list = chainon (list, build_tree_list (NULL_TREE,
35565 identifier));
35566 sep = cp_lexer_peek_token (parser->lexer);
35569 return list;
35572 /* Parse an Objective-C alias declaration.
35574 objc-alias-declaration:
35575 @compatibility_alias identifier identifier ;
35577 This function registers the alias mapping with the Objective-C front end.
35578 It returns nothing. */
35580 static void
35581 cp_parser_objc_alias_declaration (cp_parser* parser)
35583 tree alias, orig;
35585 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
35586 alias = cp_parser_identifier (parser);
35587 orig = cp_parser_identifier (parser);
35588 objc_declare_alias (alias, orig);
35589 cp_parser_consume_semicolon_at_end_of_statement (parser);
35592 /* Parse an Objective-C class forward-declaration.
35594 objc-class-declaration:
35595 @class objc-identifier-list ;
35597 The function registers the forward declarations with the Objective-C
35598 front end. It returns nothing. */
35600 static void
35601 cp_parser_objc_class_declaration (cp_parser* parser)
35603 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
35604 while (true)
35606 tree id;
35608 id = cp_parser_identifier (parser);
35609 if (id == error_mark_node)
35610 break;
35612 objc_declare_class (id);
35614 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
35615 cp_lexer_consume_token (parser->lexer);
35616 else
35617 break;
35619 cp_parser_consume_semicolon_at_end_of_statement (parser);
35622 /* Parse a list of Objective-C protocol references.
35624 objc-protocol-refs-opt:
35625 objc-protocol-refs [opt]
35627 objc-protocol-refs:
35628 < objc-identifier-list >
35630 Returns a TREE_LIST of identifiers, if any. */
35632 static tree
35633 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
35635 tree protorefs = NULL_TREE;
35637 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
35639 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
35640 protorefs = cp_parser_objc_identifier_list (parser);
35641 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
35644 return protorefs;
35647 /* Parse a Objective-C visibility specification. */
35649 static void
35650 cp_parser_objc_visibility_spec (cp_parser* parser)
35652 cp_token *vis = cp_lexer_peek_token (parser->lexer);
35654 switch (vis->keyword)
35656 case RID_AT_PRIVATE:
35657 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
35658 break;
35659 case RID_AT_PROTECTED:
35660 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
35661 break;
35662 case RID_AT_PUBLIC:
35663 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
35664 break;
35665 case RID_AT_PACKAGE:
35666 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
35667 break;
35668 default:
35669 return;
35672 /* Eat '@private'/'@protected'/'@public'. */
35673 cp_lexer_consume_token (parser->lexer);
35676 /* Parse an Objective-C method type. Return 'true' if it is a class
35677 (+) method, and 'false' if it is an instance (-) method. */
35679 static inline bool
35680 cp_parser_objc_method_type (cp_parser* parser)
35682 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
35683 return true;
35684 else
35685 return false;
35688 /* Parse an Objective-C protocol qualifier. */
35690 static tree
35691 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
35693 tree quals = NULL_TREE, node;
35694 cp_token *token = cp_lexer_peek_token (parser->lexer);
35696 node = token->u.value;
35698 while (node && identifier_p (node)
35699 && (node == ridpointers [(int) RID_IN]
35700 || node == ridpointers [(int) RID_OUT]
35701 || node == ridpointers [(int) RID_INOUT]
35702 || node == ridpointers [(int) RID_BYCOPY]
35703 || node == ridpointers [(int) RID_BYREF]
35704 || node == ridpointers [(int) RID_ONEWAY]))
35706 quals = tree_cons (NULL_TREE, node, quals);
35707 cp_lexer_consume_token (parser->lexer);
35708 token = cp_lexer_peek_token (parser->lexer);
35709 node = token->u.value;
35712 return quals;
35715 /* Parse an Objective-C typename. */
35717 static tree
35718 cp_parser_objc_typename (cp_parser* parser)
35720 tree type_name = NULL_TREE;
35722 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
35724 tree proto_quals, cp_type = NULL_TREE;
35726 matching_parens parens;
35727 parens.consume_open (parser); /* Eat '('. */
35728 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
35730 /* An ObjC type name may consist of just protocol qualifiers, in which
35731 case the type shall default to 'id'. */
35732 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
35734 cp_type = cp_parser_type_id (parser);
35736 /* If the type could not be parsed, an error has already
35737 been produced. For error recovery, behave as if it had
35738 not been specified, which will use the default type
35739 'id'. */
35740 if (cp_type == error_mark_node)
35742 cp_type = NULL_TREE;
35743 /* We need to skip to the closing parenthesis as
35744 cp_parser_type_id() does not seem to do it for
35745 us. */
35746 cp_parser_skip_to_closing_parenthesis (parser,
35747 /*recovering=*/true,
35748 /*or_comma=*/false,
35749 /*consume_paren=*/false);
35753 parens.require_close (parser);
35754 type_name = build_tree_list (proto_quals, cp_type);
35757 return type_name;
35760 /* Check to see if TYPE refers to an Objective-C selector name. */
35762 static bool
35763 cp_parser_objc_selector_p (enum cpp_ttype type)
35765 return (type == CPP_NAME || type == CPP_KEYWORD
35766 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
35767 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
35768 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
35769 || type == CPP_XOR || type == CPP_XOR_EQ);
35772 /* Parse an Objective-C selector. */
35774 static tree
35775 cp_parser_objc_selector (cp_parser* parser)
35777 cp_token *token = cp_lexer_consume_token (parser->lexer);
35779 if (!cp_parser_objc_selector_p (token->type))
35781 error_at (token->location, "invalid Objective-C++ selector name");
35782 return error_mark_node;
35785 /* C++ operator names are allowed to appear in ObjC selectors. */
35786 switch (token->type)
35788 case CPP_AND_AND: return get_identifier ("and");
35789 case CPP_AND_EQ: return get_identifier ("and_eq");
35790 case CPP_AND: return get_identifier ("bitand");
35791 case CPP_OR: return get_identifier ("bitor");
35792 case CPP_COMPL: return get_identifier ("compl");
35793 case CPP_NOT: return get_identifier ("not");
35794 case CPP_NOT_EQ: return get_identifier ("not_eq");
35795 case CPP_OR_OR: return get_identifier ("or");
35796 case CPP_OR_EQ: return get_identifier ("or_eq");
35797 case CPP_XOR: return get_identifier ("xor");
35798 case CPP_XOR_EQ: return get_identifier ("xor_eq");
35799 default: return token->u.value;
35803 /* Parse an Objective-C params list. */
35805 static tree
35806 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
35808 tree params = NULL_TREE;
35809 bool maybe_unary_selector_p = true;
35810 cp_token *token = cp_lexer_peek_token (parser->lexer);
35812 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
35814 tree selector = NULL_TREE, type_name, identifier;
35815 tree parm_attr = NULL_TREE;
35817 if (token->keyword == RID_ATTRIBUTE)
35818 break;
35820 if (token->type != CPP_COLON)
35821 selector = cp_parser_objc_selector (parser);
35823 /* Detect if we have a unary selector. */
35824 if (maybe_unary_selector_p
35825 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
35827 params = selector; /* Might be followed by attributes. */
35828 break;
35831 maybe_unary_selector_p = false;
35832 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
35834 /* Something went quite wrong. There should be a colon
35835 here, but there is not. Stop parsing parameters. */
35836 break;
35838 type_name = cp_parser_objc_typename (parser);
35839 /* New ObjC allows attributes on parameters too. */
35840 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
35841 parm_attr = cp_parser_attributes_opt (parser);
35842 identifier = cp_parser_identifier (parser);
35844 params
35845 = chainon (params,
35846 objc_build_keyword_decl (selector,
35847 type_name,
35848 identifier,
35849 parm_attr));
35851 token = cp_lexer_peek_token (parser->lexer);
35854 if (params == NULL_TREE)
35856 cp_parser_error (parser, "objective-c++ method declaration is expected");
35857 return error_mark_node;
35860 /* We allow tail attributes for the method. */
35861 if (token->keyword == RID_ATTRIBUTE)
35863 *attributes = cp_parser_attributes_opt (parser);
35864 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
35865 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35866 return params;
35867 cp_parser_error (parser,
35868 "method attributes must be specified at the end");
35869 return error_mark_node;
35872 if (params == NULL_TREE)
35874 cp_parser_error (parser, "objective-c++ method declaration is expected");
35875 return error_mark_node;
35877 return params;
35880 /* Parse the non-keyword Objective-C params. */
35882 static tree
35883 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
35884 tree* attributes)
35886 tree params = make_node (TREE_LIST);
35887 cp_token *token = cp_lexer_peek_token (parser->lexer);
35888 *ellipsisp = false; /* Initially, assume no ellipsis. */
35890 while (token->type == CPP_COMMA)
35892 cp_parameter_declarator *parmdecl;
35893 tree parm;
35895 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
35896 token = cp_lexer_peek_token (parser->lexer);
35898 if (token->type == CPP_ELLIPSIS)
35900 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
35901 *ellipsisp = true;
35902 token = cp_lexer_peek_token (parser->lexer);
35903 break;
35906 /* TODO: parse attributes for tail parameters. */
35907 parmdecl = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
35908 false, NULL);
35909 parm = grokdeclarator (parmdecl->declarator,
35910 &parmdecl->decl_specifiers,
35911 PARM, /*initialized=*/0,
35912 /*attrlist=*/NULL);
35914 chainon (params, build_tree_list (NULL_TREE, parm));
35915 token = cp_lexer_peek_token (parser->lexer);
35918 /* We allow tail attributes for the method. */
35919 if (token->keyword == RID_ATTRIBUTE)
35921 if (*attributes == NULL_TREE)
35923 *attributes = cp_parser_attributes_opt (parser);
35924 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
35925 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35926 return params;
35928 else
35929 /* We have an error, but parse the attributes, so that we can
35930 carry on. */
35931 *attributes = cp_parser_attributes_opt (parser);
35933 cp_parser_error (parser,
35934 "method attributes must be specified at the end");
35935 return error_mark_node;
35938 return params;
35941 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
35943 static void
35944 cp_parser_objc_interstitial_code (cp_parser* parser)
35946 cp_token *token = cp_lexer_peek_token (parser->lexer);
35948 /* If the next token is `extern' and the following token is a string
35949 literal, then we have a linkage specification. */
35950 if (token->keyword == RID_EXTERN
35951 && cp_parser_is_pure_string_literal
35952 (cp_lexer_peek_nth_token (parser->lexer, 2)))
35953 cp_parser_linkage_specification (parser, NULL_TREE);
35954 /* Handle #pragma, if any. */
35955 else if (token->type == CPP_PRAGMA)
35956 cp_parser_pragma (parser, pragma_objc_icode, NULL);
35957 /* Allow stray semicolons. */
35958 else if (token->type == CPP_SEMICOLON)
35959 cp_lexer_consume_token (parser->lexer);
35960 /* Mark methods as optional or required, when building protocols. */
35961 else if (token->keyword == RID_AT_OPTIONAL)
35963 cp_lexer_consume_token (parser->lexer);
35964 objc_set_method_opt (true);
35966 else if (token->keyword == RID_AT_REQUIRED)
35968 cp_lexer_consume_token (parser->lexer);
35969 objc_set_method_opt (false);
35971 else if (token->keyword == RID_NAMESPACE)
35972 cp_parser_namespace_definition (parser);
35973 /* Other stray characters must generate errors. */
35974 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
35976 cp_lexer_consume_token (parser->lexer);
35977 error ("stray %qs between Objective-C++ methods",
35978 token->type == CPP_OPEN_BRACE ? "{" : "}");
35980 /* Finally, try to parse a block-declaration, or a function-definition. */
35981 else
35982 cp_parser_block_declaration (parser, /*statement_p=*/false);
35985 /* Parse a method signature. */
35987 static tree
35988 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
35990 tree rettype, kwdparms, optparms;
35991 bool ellipsis = false;
35992 bool is_class_method;
35994 is_class_method = cp_parser_objc_method_type (parser);
35995 rettype = cp_parser_objc_typename (parser);
35996 *attributes = NULL_TREE;
35997 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
35998 if (kwdparms == error_mark_node)
35999 return error_mark_node;
36000 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
36001 if (optparms == error_mark_node)
36002 return error_mark_node;
36004 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
36007 static bool
36008 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
36010 tree tattr;
36011 cp_lexer_save_tokens (parser->lexer);
36012 tattr = cp_parser_attributes_opt (parser);
36013 gcc_assert (tattr) ;
36015 /* If the attributes are followed by a method introducer, this is not allowed.
36016 Dump the attributes and flag the situation. */
36017 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
36018 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
36019 return true;
36021 /* Otherwise, the attributes introduce some interstitial code, possibly so
36022 rewind to allow that check. */
36023 cp_lexer_rollback_tokens (parser->lexer);
36024 return false;
36027 /* Parse an Objective-C method prototype list. */
36029 static void
36030 cp_parser_objc_method_prototype_list (cp_parser* parser)
36032 cp_token *token = cp_lexer_peek_token (parser->lexer);
36034 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
36036 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
36038 tree attributes, sig;
36039 bool is_class_method;
36040 if (token->type == CPP_PLUS)
36041 is_class_method = true;
36042 else
36043 is_class_method = false;
36044 sig = cp_parser_objc_method_signature (parser, &attributes);
36045 if (sig == error_mark_node)
36047 cp_parser_skip_to_end_of_block_or_statement (parser);
36048 token = cp_lexer_peek_token (parser->lexer);
36049 continue;
36051 objc_add_method_declaration (is_class_method, sig, attributes);
36052 cp_parser_consume_semicolon_at_end_of_statement (parser);
36054 else if (token->keyword == RID_AT_PROPERTY)
36055 cp_parser_objc_at_property_declaration (parser);
36056 else if (token->keyword == RID_ATTRIBUTE
36057 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
36058 warning_at (cp_lexer_peek_token (parser->lexer)->location,
36059 OPT_Wattributes,
36060 "prefix attributes are ignored for methods");
36061 else
36062 /* Allow for interspersed non-ObjC++ code. */
36063 cp_parser_objc_interstitial_code (parser);
36065 token = cp_lexer_peek_token (parser->lexer);
36068 if (token->type != CPP_EOF)
36069 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
36070 else
36071 cp_parser_error (parser, "expected %<@end%>");
36073 objc_finish_interface ();
36076 /* Parse an Objective-C method definition list. */
36078 static void
36079 cp_parser_objc_method_definition_list (cp_parser* parser)
36081 for (;;)
36083 cp_token *token = cp_lexer_peek_token (parser->lexer);
36085 if (token->keyword == RID_AT_END)
36087 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
36088 break;
36090 else if (token->type == CPP_EOF)
36092 cp_parser_error (parser, "expected %<@end%>");
36093 break;
36095 else if (token->type == CPP_PLUS || token->type == CPP_MINUS)
36097 bool is_class_method = token->type == CPP_PLUS;
36099 push_deferring_access_checks (dk_deferred);
36100 tree attribute;
36101 tree sig = cp_parser_objc_method_signature (parser, &attribute);
36102 if (sig == error_mark_node)
36103 cp_parser_skip_to_end_of_block_or_statement (parser);
36104 else
36106 objc_start_method_definition (is_class_method, sig,
36107 attribute, NULL_TREE);
36109 /* For historical reasons, we accept an optional semicolon. */
36110 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
36111 cp_lexer_consume_token (parser->lexer);
36113 perform_deferred_access_checks (tf_warning_or_error);
36114 stop_deferring_access_checks ();
36115 tree meth
36116 = cp_parser_function_definition_after_declarator (parser, false);
36117 pop_deferring_access_checks ();
36118 objc_finish_method_definition (meth);
36121 /* The following case will be removed once @synthesize is
36122 completely implemented. */
36123 else if (token->keyword == RID_AT_PROPERTY)
36124 cp_parser_objc_at_property_declaration (parser);
36125 else if (token->keyword == RID_AT_SYNTHESIZE)
36126 cp_parser_objc_at_synthesize_declaration (parser);
36127 else if (token->keyword == RID_AT_DYNAMIC)
36128 cp_parser_objc_at_dynamic_declaration (parser);
36129 else if (token->keyword == RID_ATTRIBUTE
36130 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
36131 warning_at (token->location, OPT_Wattributes,
36132 "prefix attributes are ignored for methods");
36133 else
36134 /* Allow for interspersed non-ObjC++ code. */
36135 cp_parser_objc_interstitial_code (parser);
36138 objc_finish_implementation ();
36141 /* Parse Objective-C ivars. */
36143 static void
36144 cp_parser_objc_class_ivars (cp_parser* parser)
36146 cp_token *token = cp_lexer_peek_token (parser->lexer);
36148 if (token->type != CPP_OPEN_BRACE)
36149 return; /* No ivars specified. */
36151 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
36152 token = cp_lexer_peek_token (parser->lexer);
36154 while (token->type != CPP_CLOSE_BRACE
36155 && token->keyword != RID_AT_END && token->type != CPP_EOF)
36157 cp_decl_specifier_seq declspecs;
36158 int decl_class_or_enum_p;
36159 tree prefix_attributes;
36161 cp_parser_objc_visibility_spec (parser);
36163 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
36164 break;
36166 cp_parser_decl_specifier_seq (parser,
36167 CP_PARSER_FLAGS_OPTIONAL,
36168 &declspecs,
36169 &decl_class_or_enum_p);
36171 /* auto, register, static, extern, mutable. */
36172 if (declspecs.storage_class != sc_none)
36174 cp_parser_error (parser, "invalid type for instance variable");
36175 declspecs.storage_class = sc_none;
36178 /* thread_local. */
36179 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
36181 cp_parser_error (parser, "invalid type for instance variable");
36182 declspecs.locations[ds_thread] = 0;
36185 /* typedef. */
36186 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
36188 cp_parser_error (parser, "invalid type for instance variable");
36189 declspecs.locations[ds_typedef] = 0;
36192 prefix_attributes = declspecs.attributes;
36193 declspecs.attributes = NULL_TREE;
36195 /* Keep going until we hit the `;' at the end of the
36196 declaration. */
36197 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
36199 tree width = NULL_TREE, attributes, first_attribute, decl;
36200 cp_declarator *declarator = NULL;
36201 int ctor_dtor_or_conv_p;
36203 /* Check for a (possibly unnamed) bitfield declaration. */
36204 token = cp_lexer_peek_token (parser->lexer);
36205 if (token->type == CPP_COLON)
36206 goto eat_colon;
36208 if (token->type == CPP_NAME
36209 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
36210 == CPP_COLON))
36212 /* Get the name of the bitfield. */
36213 declarator = make_id_declarator (NULL_TREE,
36214 cp_parser_identifier (parser),
36215 sfk_none, token->location);
36217 eat_colon:
36218 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
36219 /* Get the width of the bitfield. */
36220 width
36221 = cp_parser_constant_expression (parser);
36223 else
36225 /* Parse the declarator. */
36226 declarator
36227 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
36228 CP_PARSER_FLAGS_NONE,
36229 &ctor_dtor_or_conv_p,
36230 /*parenthesized_p=*/NULL,
36231 /*member_p=*/false,
36232 /*friend_p=*/false,
36233 /*static_p=*/false);
36236 /* Look for attributes that apply to the ivar. */
36237 attributes = cp_parser_attributes_opt (parser);
36238 /* Remember which attributes are prefix attributes and
36239 which are not. */
36240 first_attribute = attributes;
36241 /* Combine the attributes. */
36242 attributes = attr_chainon (prefix_attributes, attributes);
36244 if (width)
36245 /* Create the bitfield declaration. */
36246 decl = grokbitfield (declarator, &declspecs,
36247 width, NULL_TREE, attributes);
36248 else
36249 decl = grokfield (declarator, &declspecs,
36250 NULL_TREE, /*init_const_expr_p=*/false,
36251 NULL_TREE, attributes);
36253 /* Add the instance variable. */
36254 if (decl != error_mark_node && decl != NULL_TREE)
36255 objc_add_instance_variable (decl);
36257 /* Reset PREFIX_ATTRIBUTES. */
36258 if (attributes != error_mark_node)
36260 while (attributes && TREE_CHAIN (attributes) != first_attribute)
36261 attributes = TREE_CHAIN (attributes);
36262 if (attributes)
36263 TREE_CHAIN (attributes) = NULL_TREE;
36266 token = cp_lexer_peek_token (parser->lexer);
36268 if (token->type == CPP_COMMA)
36270 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
36271 continue;
36273 break;
36276 cp_parser_consume_semicolon_at_end_of_statement (parser);
36277 token = cp_lexer_peek_token (parser->lexer);
36280 if (token->keyword == RID_AT_END)
36281 cp_parser_error (parser, "expected %<}%>");
36283 /* Do not consume the RID_AT_END, so it will be read again as terminating
36284 the @interface of @implementation. */
36285 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
36286 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
36288 /* For historical reasons, we accept an optional semicolon. */
36289 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
36290 cp_lexer_consume_token (parser->lexer);
36293 /* Parse an Objective-C protocol declaration. */
36295 static void
36296 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
36298 tree proto, protorefs;
36299 cp_token *tok;
36301 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
36302 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
36304 tok = cp_lexer_peek_token (parser->lexer);
36305 error_at (tok->location, "identifier expected after %<@protocol%>");
36306 cp_parser_consume_semicolon_at_end_of_statement (parser);
36307 return;
36310 /* See if we have a forward declaration or a definition. */
36311 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
36313 /* Try a forward declaration first. */
36314 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
36316 while (true)
36318 tree id;
36320 id = cp_parser_identifier (parser);
36321 if (id == error_mark_node)
36322 break;
36324 objc_declare_protocol (id, attributes);
36326 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36327 cp_lexer_consume_token (parser->lexer);
36328 else
36329 break;
36331 cp_parser_consume_semicolon_at_end_of_statement (parser);
36334 /* Ok, we got a full-fledged definition (or at least should). */
36335 else
36337 proto = cp_parser_identifier (parser);
36338 protorefs = cp_parser_objc_protocol_refs_opt (parser);
36339 objc_start_protocol (proto, protorefs, attributes);
36340 cp_parser_objc_method_prototype_list (parser);
36344 /* Parse an Objective-C superclass or category. */
36346 static void
36347 cp_parser_objc_superclass_or_category (cp_parser *parser,
36348 bool iface_p,
36349 tree *super,
36350 tree *categ, bool *is_class_extension)
36352 cp_token *next = cp_lexer_peek_token (parser->lexer);
36354 *super = *categ = NULL_TREE;
36355 *is_class_extension = false;
36356 if (next->type == CPP_COLON)
36358 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
36359 *super = cp_parser_identifier (parser);
36361 else if (next->type == CPP_OPEN_PAREN)
36363 matching_parens parens;
36364 parens.consume_open (parser); /* Eat '('. */
36366 /* If there is no category name, and this is an @interface, we
36367 have a class extension. */
36368 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
36370 *categ = NULL_TREE;
36371 *is_class_extension = true;
36373 else
36374 *categ = cp_parser_identifier (parser);
36376 parens.require_close (parser);
36380 /* Parse an Objective-C class interface. */
36382 static void
36383 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
36385 tree name, super, categ, protos;
36386 bool is_class_extension;
36388 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
36389 location_t nam_loc = cp_lexer_peek_token (parser->lexer)->location;
36390 name = cp_parser_identifier (parser);
36391 if (name == error_mark_node)
36393 /* It's hard to recover because even if valid @interface stuff
36394 is to follow, we can't compile it (or validate it) if we
36395 don't even know which class it refers to. Let's assume this
36396 was a stray '@interface' token in the stream and skip it.
36398 return;
36400 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
36401 &is_class_extension);
36402 protos = cp_parser_objc_protocol_refs_opt (parser);
36404 /* We have either a class or a category on our hands. */
36405 if (categ || is_class_extension)
36406 objc_start_category_interface (name, categ, protos, attributes);
36407 else
36409 objc_start_class_interface (name, nam_loc, super, protos, attributes);
36410 /* Handle instance variable declarations, if any. */
36411 cp_parser_objc_class_ivars (parser);
36412 objc_continue_interface ();
36415 cp_parser_objc_method_prototype_list (parser);
36418 /* Parse an Objective-C class implementation. */
36420 static void
36421 cp_parser_objc_class_implementation (cp_parser* parser)
36423 tree name, super, categ;
36424 bool is_class_extension;
36426 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
36427 name = cp_parser_identifier (parser);
36428 if (name == error_mark_node)
36430 /* It's hard to recover because even if valid @implementation
36431 stuff is to follow, we can't compile it (or validate it) if
36432 we don't even know which class it refers to. Let's assume
36433 this was a stray '@implementation' token in the stream and
36434 skip it.
36436 return;
36438 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
36439 &is_class_extension);
36441 /* We have either a class or a category on our hands. */
36442 if (categ)
36443 objc_start_category_implementation (name, categ);
36444 else
36446 objc_start_class_implementation (name, super);
36447 /* Handle instance variable declarations, if any. */
36448 cp_parser_objc_class_ivars (parser);
36449 objc_continue_implementation ();
36452 cp_parser_objc_method_definition_list (parser);
36455 /* Consume the @end token and finish off the implementation. */
36457 static void
36458 cp_parser_objc_end_implementation (cp_parser* parser)
36460 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
36461 objc_finish_implementation ();
36464 /* Parse an Objective-C declaration. */
36466 static void
36467 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
36469 /* Try to figure out what kind of declaration is present. */
36470 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
36472 if (attributes)
36473 switch (kwd->keyword)
36475 case RID_AT_ALIAS:
36476 case RID_AT_CLASS:
36477 case RID_AT_END:
36478 error_at (kwd->location, "attributes may not be specified before"
36479 " the %<@%D%> Objective-C++ keyword",
36480 kwd->u.value);
36481 attributes = NULL;
36482 break;
36483 case RID_AT_IMPLEMENTATION:
36484 warning_at (kwd->location, OPT_Wattributes,
36485 "prefix attributes are ignored before %<@%D%>",
36486 kwd->u.value);
36487 attributes = NULL;
36488 default:
36489 break;
36492 switch (kwd->keyword)
36494 case RID_AT_ALIAS:
36495 cp_parser_objc_alias_declaration (parser);
36496 break;
36497 case RID_AT_CLASS:
36498 cp_parser_objc_class_declaration (parser);
36499 break;
36500 case RID_AT_PROTOCOL:
36501 cp_parser_objc_protocol_declaration (parser, attributes);
36502 break;
36503 case RID_AT_INTERFACE:
36504 cp_parser_objc_class_interface (parser, attributes);
36505 break;
36506 case RID_AT_IMPLEMENTATION:
36507 cp_parser_objc_class_implementation (parser);
36508 break;
36509 case RID_AT_END:
36510 cp_parser_objc_end_implementation (parser);
36511 break;
36512 default:
36513 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
36514 kwd->u.value);
36515 cp_parser_skip_to_end_of_block_or_statement (parser);
36519 /* Parse an Objective-C try-catch-finally statement.
36521 objc-try-catch-finally-stmt:
36522 @try compound-statement objc-catch-clause-seq [opt]
36523 objc-finally-clause [opt]
36525 objc-catch-clause-seq:
36526 objc-catch-clause objc-catch-clause-seq [opt]
36528 objc-catch-clause:
36529 @catch ( objc-exception-declaration ) compound-statement
36531 objc-finally-clause:
36532 @finally compound-statement
36534 objc-exception-declaration:
36535 parameter-declaration
36536 '...'
36538 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
36540 Returns NULL_TREE.
36542 PS: This function is identical to c_parser_objc_try_catch_finally_statement
36543 for C. Keep them in sync. */
36545 static tree
36546 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
36548 location_t location;
36549 tree stmt;
36551 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
36552 location = cp_lexer_peek_token (parser->lexer)->location;
36553 objc_maybe_warn_exceptions (location);
36554 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
36555 node, lest it get absorbed into the surrounding block. */
36556 stmt = push_stmt_list ();
36557 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
36558 objc_begin_try_stmt (location, pop_stmt_list (stmt));
36560 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
36562 cp_parameter_declarator *parm;
36563 tree parameter_declaration = error_mark_node;
36564 bool seen_open_paren = false;
36565 matching_parens parens;
36567 cp_lexer_consume_token (parser->lexer);
36568 if (parens.require_open (parser))
36569 seen_open_paren = true;
36570 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
36572 /* We have "@catch (...)" (where the '...' are literally
36573 what is in the code). Skip the '...'.
36574 parameter_declaration is set to NULL_TREE, and
36575 objc_being_catch_clauses() knows that that means
36576 '...'. */
36577 cp_lexer_consume_token (parser->lexer);
36578 parameter_declaration = NULL_TREE;
36580 else
36582 /* We have "@catch (NSException *exception)" or something
36583 like that. Parse the parameter declaration. */
36584 parm = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
36585 false, NULL);
36586 if (parm == NULL)
36587 parameter_declaration = error_mark_node;
36588 else
36589 parameter_declaration = grokdeclarator (parm->declarator,
36590 &parm->decl_specifiers,
36591 PARM, /*initialized=*/0,
36592 /*attrlist=*/NULL);
36594 if (seen_open_paren)
36595 parens.require_close (parser);
36596 else
36598 /* If there was no open parenthesis, we are recovering from
36599 an error, and we are trying to figure out what mistake
36600 the user has made. */
36602 /* If there is an immediate closing parenthesis, the user
36603 probably forgot the opening one (ie, they typed "@catch
36604 NSException *e)". Parse the closing parenthesis and keep
36605 going. */
36606 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
36607 cp_lexer_consume_token (parser->lexer);
36609 /* If these is no immediate closing parenthesis, the user
36610 probably doesn't know that parenthesis are required at
36611 all (ie, they typed "@catch NSException *e"). So, just
36612 forget about the closing parenthesis and keep going. */
36614 objc_begin_catch_clause (parameter_declaration);
36615 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
36616 objc_finish_catch_clause ();
36618 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
36620 cp_lexer_consume_token (parser->lexer);
36621 location = cp_lexer_peek_token (parser->lexer)->location;
36622 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
36623 node, lest it get absorbed into the surrounding block. */
36624 stmt = push_stmt_list ();
36625 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
36626 objc_build_finally_clause (location, pop_stmt_list (stmt));
36629 return objc_finish_try_stmt ();
36632 /* Parse an Objective-C synchronized statement.
36634 objc-synchronized-stmt:
36635 @synchronized ( expression ) compound-statement
36637 Returns NULL_TREE. */
36639 static tree
36640 cp_parser_objc_synchronized_statement (cp_parser *parser)
36642 location_t location;
36643 tree lock, stmt;
36645 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
36647 location = cp_lexer_peek_token (parser->lexer)->location;
36648 objc_maybe_warn_exceptions (location);
36649 matching_parens parens;
36650 parens.require_open (parser);
36651 lock = cp_parser_expression (parser);
36652 parens.require_close (parser);
36654 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
36655 node, lest it get absorbed into the surrounding block. */
36656 stmt = push_stmt_list ();
36657 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
36659 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
36662 /* Parse an Objective-C throw statement.
36664 objc-throw-stmt:
36665 @throw assignment-expression [opt] ;
36667 Returns a constructed '@throw' statement. */
36669 static tree
36670 cp_parser_objc_throw_statement (cp_parser *parser)
36672 tree expr = NULL_TREE;
36673 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36675 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
36677 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
36678 expr = cp_parser_expression (parser);
36680 cp_parser_consume_semicolon_at_end_of_statement (parser);
36682 return objc_build_throw_stmt (loc, expr);
36685 /* Parse an Objective-C statement. */
36687 static tree
36688 cp_parser_objc_statement (cp_parser * parser)
36690 /* Try to figure out what kind of declaration is present. */
36691 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
36693 switch (kwd->keyword)
36695 case RID_AT_TRY:
36696 return cp_parser_objc_try_catch_finally_statement (parser);
36697 case RID_AT_SYNCHRONIZED:
36698 return cp_parser_objc_synchronized_statement (parser);
36699 case RID_AT_THROW:
36700 return cp_parser_objc_throw_statement (parser);
36701 default:
36702 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
36703 kwd->u.value);
36704 cp_parser_skip_to_end_of_block_or_statement (parser);
36707 return error_mark_node;
36710 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
36711 look ahead to see if an objc keyword follows the attributes. This
36712 is to detect the use of prefix attributes on ObjC @interface and
36713 @protocol. */
36715 static bool
36716 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
36718 cp_lexer_save_tokens (parser->lexer);
36719 tree addon = cp_parser_attributes_opt (parser);
36720 if (addon
36721 && OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
36723 cp_lexer_commit_tokens (parser->lexer);
36724 if (*attrib)
36725 TREE_CHAIN (*attrib) = addon;
36726 else
36727 *attrib = addon;
36728 return true;
36730 cp_lexer_rollback_tokens (parser->lexer);
36731 return false;
36734 /* This routine is a minimal replacement for
36735 c_parser_struct_declaration () used when parsing the list of
36736 types/names or ObjC++ properties. For example, when parsing the
36737 code
36739 @property (readonly) int a, b, c;
36741 this function is responsible for parsing "int a, int b, int c" and
36742 returning the declarations as CHAIN of DECLs.
36744 TODO: Share this code with cp_parser_objc_class_ivars. It's very
36745 similar parsing. */
36746 static tree
36747 cp_parser_objc_struct_declaration (cp_parser *parser)
36749 tree decls = NULL_TREE;
36750 cp_decl_specifier_seq declspecs;
36751 int decl_class_or_enum_p;
36752 tree prefix_attributes;
36754 cp_parser_decl_specifier_seq (parser,
36755 CP_PARSER_FLAGS_NONE,
36756 &declspecs,
36757 &decl_class_or_enum_p);
36759 if (declspecs.type == error_mark_node)
36760 return error_mark_node;
36762 /* auto, register, static, extern, mutable. */
36763 if (declspecs.storage_class != sc_none)
36765 cp_parser_error (parser, "invalid type for property");
36766 declspecs.storage_class = sc_none;
36769 /* thread_local. */
36770 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
36772 cp_parser_error (parser, "invalid type for property");
36773 declspecs.locations[ds_thread] = 0;
36776 /* typedef. */
36777 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
36779 cp_parser_error (parser, "invalid type for property");
36780 declspecs.locations[ds_typedef] = 0;
36783 prefix_attributes = declspecs.attributes;
36784 declspecs.attributes = NULL_TREE;
36786 /* Keep going until we hit the `;' at the end of the declaration. */
36787 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
36789 tree attributes, first_attribute, decl;
36790 cp_declarator *declarator;
36791 cp_token *token;
36793 /* Parse the declarator. */
36794 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
36795 CP_PARSER_FLAGS_NONE,
36796 NULL, NULL, false, false, false);
36798 /* Look for attributes that apply to the ivar. */
36799 attributes = cp_parser_attributes_opt (parser);
36800 /* Remember which attributes are prefix attributes and
36801 which are not. */
36802 first_attribute = attributes;
36803 /* Combine the attributes. */
36804 attributes = attr_chainon (prefix_attributes, attributes);
36806 decl = grokfield (declarator, &declspecs,
36807 NULL_TREE, /*init_const_expr_p=*/false,
36808 NULL_TREE, attributes);
36810 if (decl == error_mark_node || decl == NULL_TREE)
36811 return error_mark_node;
36813 /* Reset PREFIX_ATTRIBUTES. */
36814 if (attributes != error_mark_node)
36816 while (attributes && TREE_CHAIN (attributes) != first_attribute)
36817 attributes = TREE_CHAIN (attributes);
36818 if (attributes)
36819 TREE_CHAIN (attributes) = NULL_TREE;
36822 DECL_CHAIN (decl) = decls;
36823 decls = decl;
36825 token = cp_lexer_peek_token (parser->lexer);
36826 if (token->type == CPP_COMMA)
36828 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
36829 continue;
36831 else
36832 break;
36834 return decls;
36837 /* Parse an Objective-C @property declaration. The syntax is:
36839 objc-property-declaration:
36840 '@property' objc-property-attributes[opt] struct-declaration ;
36842 objc-property-attributes:
36843 '(' objc-property-attribute-list ')'
36845 objc-property-attribute-list:
36846 objc-property-attribute
36847 objc-property-attribute-list, objc-property-attribute
36849 objc-property-attribute
36850 'getter' = identifier
36851 'setter' = identifier
36852 'readonly'
36853 'readwrite'
36854 'assign'
36855 'retain'
36856 'copy'
36857 'nonatomic'
36859 For example:
36860 @property NSString *name;
36861 @property (readonly) id object;
36862 @property (retain, nonatomic, getter=getTheName) id name;
36863 @property int a, b, c;
36865 PS: This function is identical to
36866 c_parser_objc_at_property_declaration for C. Keep them in sync. */
36867 static void
36868 cp_parser_objc_at_property_declaration (cp_parser *parser)
36870 /* Parse the optional attribute list.
36872 A list of parsed, but not verified, attributes. */
36873 auto_delete_vec<property_attribute_info> prop_attr_list;
36874 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36876 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
36878 /* Parse the optional attribute list... */
36879 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36881 /* Eat the '('. */
36882 matching_parens parens;
36883 location_t attr_start = cp_lexer_peek_token (parser->lexer)->location;
36884 parens.consume_open (parser);
36885 bool syntax_error = false;
36887 /* Allow empty @property attribute lists, but with a warning. */
36888 location_t attr_end = cp_lexer_peek_token (parser->lexer)->location;
36889 location_t attr_comb;
36890 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
36892 attr_comb = make_location (attr_end, attr_start, attr_end);
36893 warning_at (attr_comb, OPT_Wattributes,
36894 "empty property attribute list");
36896 else
36897 while (true)
36899 cp_token *token = cp_lexer_peek_token (parser->lexer);
36900 attr_start = token->location;
36901 attr_end = get_finish (token->location);
36902 attr_comb = make_location (attr_start, attr_start, attr_end);
36904 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_COMMA)
36906 warning_at (attr_comb, OPT_Wattributes,
36907 "missing property attribute");
36908 if (token->type == CPP_CLOSE_PAREN)
36909 break;
36910 cp_lexer_consume_token (parser->lexer);
36911 continue;
36914 tree attr_name = NULL_TREE;
36915 if (identifier_p (token->u.value))
36916 attr_name = token->u.value;
36918 enum rid keyword;
36919 if (token->type == CPP_NAME)
36920 keyword = C_RID_CODE (token->u.value);
36921 else if (token->type == CPP_KEYWORD
36922 && token->keyword == RID_CLASS)
36923 /* Account for accepting the 'class' keyword in this context. */
36924 keyword = RID_CLASS;
36925 else
36926 keyword = RID_MAX; /* By definition, an unknown property. */
36927 cp_lexer_consume_token (parser->lexer);
36929 enum objc_property_attribute_kind prop_kind
36930 = objc_prop_attr_kind_for_rid (keyword);
36931 property_attribute_info *prop
36932 = new property_attribute_info (attr_name, attr_comb, prop_kind);
36933 prop_attr_list.safe_push (prop);
36935 tree meth_name;
36936 switch (prop->prop_kind)
36938 default: break;
36939 case OBJC_PROPERTY_ATTR_UNKNOWN:
36940 if (attr_name)
36941 error_at (attr_start, "unknown property attribute %qE",
36942 attr_name);
36943 else
36944 error_at (attr_start, "unknown property attribute");
36945 prop->parse_error = syntax_error = true;
36946 break;
36948 case OBJC_PROPERTY_ATTR_GETTER:
36949 case OBJC_PROPERTY_ATTR_SETTER:
36950 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
36952 attr_comb = make_location (attr_end, attr_start, attr_end);
36953 error_at (attr_comb, "expected %<=%> after Objective-C %qE",
36954 attr_name);
36955 prop->parse_error = syntax_error = true;
36956 break;
36959 token = cp_lexer_peek_token (parser->lexer);
36960 attr_end = token->location;
36961 cp_lexer_consume_token (parser->lexer); /* eat the = */
36963 if (!cp_parser_objc_selector_p
36964 (cp_lexer_peek_token (parser->lexer)->type))
36966 attr_comb = make_location (attr_end, attr_start, attr_end);
36967 error_at (attr_comb, "expected %qE selector name",
36968 attr_name);
36969 prop->parse_error = syntax_error = true;
36970 break;
36973 /* Get the end of the method name, and consume the name. */
36974 token = cp_lexer_peek_token (parser->lexer);
36975 attr_end = get_finish (token->location);
36976 /* Because method names may contain C++ keywords, we have a
36977 routine to fetch them (this also consumes the token). */
36978 meth_name = cp_parser_objc_selector (parser);
36980 if (prop->prop_kind == OBJC_PROPERTY_ATTR_SETTER)
36982 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
36984 attr_comb = make_location (attr_end, attr_start,
36985 attr_end);
36986 error_at (attr_comb, "setter method names must"
36987 " terminate with %<:%>");
36988 prop->parse_error = syntax_error = true;
36990 else
36992 attr_end = get_finish (cp_lexer_peek_token
36993 (parser->lexer)->location);
36994 cp_lexer_consume_token (parser->lexer);
36996 attr_comb = make_location (attr_start, attr_start,
36997 attr_end);
36999 else
37000 attr_comb = make_location (attr_start, attr_start,
37001 attr_end);
37002 prop->ident = meth_name;
37003 /* Updated location including all that was successfully
37004 parsed. */
37005 prop->prop_loc = attr_comb;
37006 break;
37009 /* If we see a comma here, then keep going - even if we already
37010 saw a syntax error. For simple mistakes e.g. (asign, getter=x)
37011 this makes a more useful output and avoid spurious warnings
37012 about missing attributes that are, in fact, specified after the
37013 one with the syntax error. */
37014 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37015 cp_lexer_consume_token (parser->lexer);
37016 else
37017 break;
37020 if (syntax_error || !parens.require_close (parser))
37021 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37022 /*or_comma=*/false,
37023 /*consume_paren=*/true);
37026 /* 'properties' is the list of properties that we read. Usually a
37027 single one, but maybe more (eg, in "@property int a, b, c;" there
37028 are three).
37029 TODO: Update this parsing so that it accepts (erroneous) bitfields so
37030 that we can issue a meaningful and consistent (between C/C++) error
37031 message from objc_add_property_declaration (). */
37032 tree properties = cp_parser_objc_struct_declaration (parser);
37034 if (properties == error_mark_node)
37035 cp_parser_skip_to_end_of_statement (parser);
37036 else if (properties == NULL_TREE)
37037 cp_parser_error (parser, "expected identifier");
37038 else
37040 /* Comma-separated properties are chained together in reverse order;
37041 add them one by one. */
37042 properties = nreverse (properties);
37043 for (; properties; properties = TREE_CHAIN (properties))
37044 objc_add_property_declaration (loc, copy_node (properties),
37045 prop_attr_list);
37048 cp_parser_consume_semicolon_at_end_of_statement (parser);
37051 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
37053 objc-synthesize-declaration:
37054 @synthesize objc-synthesize-identifier-list ;
37056 objc-synthesize-identifier-list:
37057 objc-synthesize-identifier
37058 objc-synthesize-identifier-list, objc-synthesize-identifier
37060 objc-synthesize-identifier
37061 identifier
37062 identifier = identifier
37064 For example:
37065 @synthesize MyProperty;
37066 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
37068 PS: This function is identical to c_parser_objc_at_synthesize_declaration
37069 for C. Keep them in sync.
37071 static void
37072 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
37074 tree list = NULL_TREE;
37075 location_t loc;
37076 loc = cp_lexer_peek_token (parser->lexer)->location;
37078 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
37079 while (true)
37081 tree property, ivar;
37082 property = cp_parser_identifier (parser);
37083 if (property == error_mark_node)
37085 cp_parser_consume_semicolon_at_end_of_statement (parser);
37086 return;
37088 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
37090 cp_lexer_consume_token (parser->lexer);
37091 ivar = cp_parser_identifier (parser);
37092 if (ivar == error_mark_node)
37094 cp_parser_consume_semicolon_at_end_of_statement (parser);
37095 return;
37098 else
37099 ivar = NULL_TREE;
37100 list = chainon (list, build_tree_list (ivar, property));
37101 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37102 cp_lexer_consume_token (parser->lexer);
37103 else
37104 break;
37106 cp_parser_consume_semicolon_at_end_of_statement (parser);
37107 objc_add_synthesize_declaration (loc, list);
37110 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
37112 objc-dynamic-declaration:
37113 @dynamic identifier-list ;
37115 For example:
37116 @dynamic MyProperty;
37117 @dynamic MyProperty, AnotherProperty;
37119 PS: This function is identical to c_parser_objc_at_dynamic_declaration
37120 for C. Keep them in sync.
37122 static void
37123 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
37125 tree list = NULL_TREE;
37126 location_t loc;
37127 loc = cp_lexer_peek_token (parser->lexer)->location;
37129 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
37130 while (true)
37132 tree property;
37133 property = cp_parser_identifier (parser);
37134 if (property == error_mark_node)
37136 cp_parser_consume_semicolon_at_end_of_statement (parser);
37137 return;
37139 list = chainon (list, build_tree_list (NULL, property));
37140 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37141 cp_lexer_consume_token (parser->lexer);
37142 else
37143 break;
37145 cp_parser_consume_semicolon_at_end_of_statement (parser);
37146 objc_add_dynamic_declaration (loc, list);
37150 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 / 4.5 / 5.0 parsing routines. */
37152 /* Returns name of the next clause.
37153 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
37154 the token is not consumed. Otherwise appropriate pragma_omp_clause is
37155 returned and the token is consumed. */
37157 static pragma_omp_clause
37158 cp_parser_omp_clause_name (cp_parser *parser)
37160 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
37162 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
37163 result = PRAGMA_OACC_CLAUSE_AUTO;
37164 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
37165 result = PRAGMA_OMP_CLAUSE_IF;
37166 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
37167 result = PRAGMA_OMP_CLAUSE_DEFAULT;
37168 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
37169 result = PRAGMA_OACC_CLAUSE_DELETE;
37170 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
37171 result = PRAGMA_OMP_CLAUSE_PRIVATE;
37172 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
37173 result = PRAGMA_OMP_CLAUSE_FOR;
37174 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37176 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37177 const char *p = IDENTIFIER_POINTER (id);
37179 switch (p[0])
37181 case 'a':
37182 if (!strcmp ("affinity", p))
37183 result = PRAGMA_OMP_CLAUSE_AFFINITY;
37184 else if (!strcmp ("aligned", p))
37185 result = PRAGMA_OMP_CLAUSE_ALIGNED;
37186 else if (!strcmp ("allocate", p))
37187 result = PRAGMA_OMP_CLAUSE_ALLOCATE;
37188 else if (!strcmp ("async", p))
37189 result = PRAGMA_OACC_CLAUSE_ASYNC;
37190 else if (!strcmp ("attach", p))
37191 result = PRAGMA_OACC_CLAUSE_ATTACH;
37192 break;
37193 case 'b':
37194 if (!strcmp ("bind", p))
37195 result = PRAGMA_OMP_CLAUSE_BIND;
37196 break;
37197 case 'c':
37198 if (!strcmp ("collapse", p))
37199 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
37200 else if (!strcmp ("copy", p))
37201 result = PRAGMA_OACC_CLAUSE_COPY;
37202 else if (!strcmp ("copyin", p))
37203 result = PRAGMA_OMP_CLAUSE_COPYIN;
37204 else if (!strcmp ("copyout", p))
37205 result = PRAGMA_OACC_CLAUSE_COPYOUT;
37206 else if (!strcmp ("copyprivate", p))
37207 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
37208 else if (!strcmp ("create", p))
37209 result = PRAGMA_OACC_CLAUSE_CREATE;
37210 break;
37211 case 'd':
37212 if (!strcmp ("defaultmap", p))
37213 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
37214 else if (!strcmp ("depend", p))
37215 result = PRAGMA_OMP_CLAUSE_DEPEND;
37216 else if (!strcmp ("detach", p))
37217 result = PRAGMA_OACC_CLAUSE_DETACH;
37218 else if (!strcmp ("device", p))
37219 result = PRAGMA_OMP_CLAUSE_DEVICE;
37220 else if (!strcmp ("deviceptr", p))
37221 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
37222 else if (!strcmp ("device_resident", p))
37223 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
37224 else if (!strcmp ("device_type", p))
37225 result = PRAGMA_OMP_CLAUSE_DEVICE_TYPE;
37226 else if (!strcmp ("dist_schedule", p))
37227 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
37228 else if (!strcmp ("doacross", p))
37229 result = PRAGMA_OMP_CLAUSE_DOACROSS;
37230 break;
37231 case 'e':
37232 if (!strcmp ("enter", p))
37233 result = PRAGMA_OMP_CLAUSE_ENTER;
37234 break;
37235 case 'f':
37236 if (!strcmp ("filter", p))
37237 result = PRAGMA_OMP_CLAUSE_FILTER;
37238 else if (!strcmp ("final", p))
37239 result = PRAGMA_OMP_CLAUSE_FINAL;
37240 else if (!strcmp ("finalize", p))
37241 result = PRAGMA_OACC_CLAUSE_FINALIZE;
37242 else if (!strcmp ("firstprivate", p))
37243 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
37244 else if (!strcmp ("from", p))
37245 result = PRAGMA_OMP_CLAUSE_FROM;
37246 break;
37247 case 'g':
37248 if (!strcmp ("gang", p))
37249 result = PRAGMA_OACC_CLAUSE_GANG;
37250 else if (!strcmp ("grainsize", p))
37251 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
37252 break;
37253 case 'h':
37254 if (!strcmp ("has_device_addr", p))
37255 result = PRAGMA_OMP_CLAUSE_HAS_DEVICE_ADDR;
37256 else if (!strcmp ("hint", p))
37257 result = PRAGMA_OMP_CLAUSE_HINT;
37258 else if (!strcmp ("host", p))
37259 result = PRAGMA_OACC_CLAUSE_HOST;
37260 break;
37261 case 'i':
37262 if (!strcmp ("if_present", p))
37263 result = PRAGMA_OACC_CLAUSE_IF_PRESENT;
37264 else if (!strcmp ("in_reduction", p))
37265 result = PRAGMA_OMP_CLAUSE_IN_REDUCTION;
37266 else if (!strcmp ("inbranch", p))
37267 result = PRAGMA_OMP_CLAUSE_INBRANCH;
37268 else if (!strcmp ("independent", p))
37269 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
37270 else if (!strcmp ("is_device_ptr", p))
37271 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
37272 break;
37273 case 'l':
37274 if (!strcmp ("lastprivate", p))
37275 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
37276 else if (!strcmp ("linear", p))
37277 result = PRAGMA_OMP_CLAUSE_LINEAR;
37278 else if (!strcmp ("link", p))
37279 result = PRAGMA_OMP_CLAUSE_LINK;
37280 break;
37281 case 'm':
37282 if (!strcmp ("map", p))
37283 result = PRAGMA_OMP_CLAUSE_MAP;
37284 else if (!strcmp ("mergeable", p))
37285 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
37286 break;
37287 case 'n':
37288 if (!strcmp ("no_create", p))
37289 result = PRAGMA_OACC_CLAUSE_NO_CREATE;
37290 else if (!strcmp ("nogroup", p))
37291 result = PRAGMA_OMP_CLAUSE_NOGROUP;
37292 else if (!strcmp ("nohost", p))
37293 result = PRAGMA_OACC_CLAUSE_NOHOST;
37294 else if (!strcmp ("nontemporal", p))
37295 result = PRAGMA_OMP_CLAUSE_NONTEMPORAL;
37296 else if (!strcmp ("notinbranch", p))
37297 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
37298 else if (!strcmp ("nowait", p))
37299 result = PRAGMA_OMP_CLAUSE_NOWAIT;
37300 else if (!strcmp ("num_gangs", p))
37301 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
37302 else if (!strcmp ("num_tasks", p))
37303 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
37304 else if (!strcmp ("num_teams", p))
37305 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
37306 else if (!strcmp ("num_threads", p))
37307 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
37308 else if (!strcmp ("num_workers", p))
37309 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
37310 break;
37311 case 'o':
37312 if (!strcmp ("ordered", p))
37313 result = PRAGMA_OMP_CLAUSE_ORDERED;
37314 else if (!strcmp ("order", p))
37315 result = PRAGMA_OMP_CLAUSE_ORDER;
37316 break;
37317 case 'p':
37318 if (!strcmp ("parallel", p))
37319 result = PRAGMA_OMP_CLAUSE_PARALLEL;
37320 else if (!strcmp ("present", p))
37321 result = PRAGMA_OACC_CLAUSE_PRESENT;
37322 else if (!strcmp ("present_or_copy", p)
37323 || !strcmp ("pcopy", p))
37324 result = PRAGMA_OACC_CLAUSE_COPY;
37325 else if (!strcmp ("present_or_copyin", p)
37326 || !strcmp ("pcopyin", p))
37327 result = PRAGMA_OACC_CLAUSE_COPYIN;
37328 else if (!strcmp ("present_or_copyout", p)
37329 || !strcmp ("pcopyout", p))
37330 result = PRAGMA_OACC_CLAUSE_COPYOUT;
37331 else if (!strcmp ("present_or_create", p)
37332 || !strcmp ("pcreate", p))
37333 result = PRAGMA_OACC_CLAUSE_CREATE;
37334 else if (!strcmp ("priority", p))
37335 result = PRAGMA_OMP_CLAUSE_PRIORITY;
37336 else if (!strcmp ("proc_bind", p))
37337 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
37338 break;
37339 case 'r':
37340 if (!strcmp ("reduction", p))
37341 result = PRAGMA_OMP_CLAUSE_REDUCTION;
37342 break;
37343 case 's':
37344 if (!strcmp ("safelen", p))
37345 result = PRAGMA_OMP_CLAUSE_SAFELEN;
37346 else if (!strcmp ("schedule", p))
37347 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
37348 else if (!strcmp ("sections", p))
37349 result = PRAGMA_OMP_CLAUSE_SECTIONS;
37350 else if (!strcmp ("self", p)) /* "self" is a synonym for "host". */
37351 result = PRAGMA_OACC_CLAUSE_HOST;
37352 else if (!strcmp ("seq", p))
37353 result = PRAGMA_OACC_CLAUSE_SEQ;
37354 else if (!strcmp ("shared", p))
37355 result = PRAGMA_OMP_CLAUSE_SHARED;
37356 else if (!strcmp ("simd", p))
37357 result = PRAGMA_OMP_CLAUSE_SIMD;
37358 else if (!strcmp ("simdlen", p))
37359 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
37360 break;
37361 case 't':
37362 if (!strcmp ("task_reduction", p))
37363 result = PRAGMA_OMP_CLAUSE_TASK_REDUCTION;
37364 else if (!strcmp ("taskgroup", p))
37365 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
37366 else if (!strcmp ("thread_limit", p))
37367 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
37368 else if (!strcmp ("threads", p))
37369 result = PRAGMA_OMP_CLAUSE_THREADS;
37370 else if (!strcmp ("tile", p))
37371 result = PRAGMA_OACC_CLAUSE_TILE;
37372 else if (!strcmp ("to", p))
37373 result = PRAGMA_OMP_CLAUSE_TO;
37374 break;
37375 case 'u':
37376 if (!strcmp ("uniform", p))
37377 result = PRAGMA_OMP_CLAUSE_UNIFORM;
37378 else if (!strcmp ("untied", p))
37379 result = PRAGMA_OMP_CLAUSE_UNTIED;
37380 else if (!strcmp ("use_device", p))
37381 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
37382 else if (!strcmp ("use_device_addr", p))
37383 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR;
37384 else if (!strcmp ("use_device_ptr", p))
37385 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
37386 break;
37387 case 'v':
37388 if (!strcmp ("vector", p))
37389 result = PRAGMA_OACC_CLAUSE_VECTOR;
37390 else if (!strcmp ("vector_length", p))
37391 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
37392 break;
37393 case 'w':
37394 if (!strcmp ("wait", p))
37395 result = PRAGMA_OACC_CLAUSE_WAIT;
37396 else if (!strcmp ("worker", p))
37397 result = PRAGMA_OACC_CLAUSE_WORKER;
37398 break;
37402 if (result != PRAGMA_OMP_CLAUSE_NONE)
37403 cp_lexer_consume_token (parser->lexer);
37405 return result;
37408 /* Validate that a clause of the given type does not already exist. */
37410 static void
37411 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
37412 const char *name, location_t location)
37414 if (omp_find_clause (clauses, code))
37415 error_at (location, "too many %qs clauses", name);
37418 /* OpenMP 2.5:
37419 variable-list:
37420 identifier
37421 variable-list , identifier
37423 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
37424 colon). An opening parenthesis will have been consumed by the caller.
37426 If KIND is nonzero, create the appropriate node and install the decl
37427 in OMP_CLAUSE_DECL and add the node to the head of the list.
37429 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
37430 return the list created.
37432 COLON can be NULL if only closing parenthesis should end the list,
37433 or pointer to bool which will receive false if the list is terminated
37434 by closing parenthesis or true if the list is terminated by colon.
37436 The optional ALLOW_DEREF argument is true if list items can use the deref
37437 (->) operator. */
37439 struct omp_dim
37441 tree low_bound, length;
37442 location_t loc;
37443 bool no_colon;
37444 omp_dim (tree lb, tree len, location_t lo, bool nc)
37445 : low_bound (lb), length (len), loc (lo), no_colon (nc) {}
37448 static tree
37449 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
37450 tree list, bool *colon,
37451 bool allow_deref = false)
37453 auto_vec<omp_dim> dims;
37454 bool array_section_p;
37455 cp_token *token;
37456 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37457 if (colon)
37459 parser->colon_corrects_to_scope_p = false;
37460 *colon = false;
37462 while (1)
37464 tree name, decl;
37466 if (kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
37467 cp_parser_parse_tentatively (parser);
37468 token = cp_lexer_peek_token (parser->lexer);
37469 if (kind != 0
37470 && cp_parser_is_keyword (token, RID_THIS))
37472 decl = finish_this_expr ();
37473 if (TREE_CODE (decl) == NON_LVALUE_EXPR
37474 || CONVERT_EXPR_P (decl))
37475 decl = TREE_OPERAND (decl, 0);
37476 cp_lexer_consume_token (parser->lexer);
37478 else if (cp_parser_is_keyword (token, RID_FUNCTION_NAME)
37479 || cp_parser_is_keyword (token, RID_PRETTY_FUNCTION_NAME)
37480 || cp_parser_is_keyword (token, RID_C99_FUNCTION_NAME))
37482 cp_id_kind idk;
37483 decl = cp_parser_primary_expression (parser, false, false, false,
37484 &idk);
37486 else if (kind == OMP_CLAUSE_DEPEND
37487 && cp_parser_is_keyword (token, RID_OMP_ALL_MEMORY)
37488 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
37489 || cp_lexer_nth_token_is (parser->lexer, 2,
37490 CPP_CLOSE_PAREN)))
37492 decl = ridpointers[RID_OMP_ALL_MEMORY];
37493 cp_lexer_consume_token (parser->lexer);
37495 else
37497 name = cp_parser_id_expression (parser, /*template_p=*/false,
37498 /*check_dependency_p=*/true,
37499 /*template_p=*/NULL,
37500 /*declarator_p=*/false,
37501 /*optional_p=*/false);
37502 if (name == error_mark_node)
37504 if ((kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
37505 && cp_parser_simulate_error (parser))
37506 goto depend_lvalue;
37507 goto skip_comma;
37510 if (identifier_p (name))
37511 decl = cp_parser_lookup_name_simple (parser, name, token->location);
37512 else
37513 decl = name;
37514 if (decl == error_mark_node)
37516 if ((kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
37517 && cp_parser_simulate_error (parser))
37518 goto depend_lvalue;
37519 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
37520 token->location);
37523 if (outer_automatic_var_p (decl))
37524 decl = process_outer_var_ref (decl, tf_warning_or_error);
37525 if (decl == error_mark_node)
37527 else if (kind != 0)
37529 switch (kind)
37531 case OMP_CLAUSE__CACHE_:
37532 /* The OpenACC cache directive explicitly only allows "array
37533 elements or subarrays". */
37534 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
37536 error_at (token->location, "expected %<[%>");
37537 decl = error_mark_node;
37538 break;
37540 /* FALLTHROUGH. */
37541 case OMP_CLAUSE_MAP:
37542 case OMP_CLAUSE_FROM:
37543 case OMP_CLAUSE_TO:
37544 start_component_ref:
37545 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT)
37546 || (allow_deref
37547 && cp_lexer_next_token_is (parser->lexer, CPP_DEREF)))
37549 cpp_ttype ttype
37550 = cp_lexer_next_token_is (parser->lexer, CPP_DOT)
37551 ? CPP_DOT : CPP_DEREF;
37552 location_t loc
37553 = cp_lexer_peek_token (parser->lexer)->location;
37554 cp_id_kind idk = CP_ID_KIND_NONE;
37555 cp_lexer_consume_token (parser->lexer);
37556 decl = convert_from_reference (decl);
37557 decl = (cp_parser_postfix_dot_deref_expression
37558 (parser, ttype, cp_expr (decl, token->location),
37559 false, &idk, loc));
37561 /* FALLTHROUGH. */
37562 case OMP_CLAUSE_AFFINITY:
37563 case OMP_CLAUSE_DEPEND:
37564 case OMP_CLAUSE_REDUCTION:
37565 case OMP_CLAUSE_IN_REDUCTION:
37566 case OMP_CLAUSE_TASK_REDUCTION:
37567 case OMP_CLAUSE_HAS_DEVICE_ADDR:
37568 array_section_p = false;
37569 dims.truncate (0);
37570 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
37572 location_t loc = UNKNOWN_LOCATION;
37573 tree low_bound = NULL_TREE, length = NULL_TREE;
37574 bool no_colon = false;
37576 parser->colon_corrects_to_scope_p = false;
37577 cp_lexer_consume_token (parser->lexer);
37578 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
37580 loc = cp_lexer_peek_token (parser->lexer)->location;
37581 low_bound = cp_parser_expression (parser);
37582 /* Later handling is not prepared to see through these. */
37583 gcc_checking_assert (!location_wrapper_p (low_bound));
37585 if (!colon)
37586 parser->colon_corrects_to_scope_p
37587 = saved_colon_corrects_to_scope_p;
37588 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
37590 length = integer_one_node;
37591 no_colon = true;
37593 else
37595 /* Look for `:'. */
37596 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37598 if ((kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
37599 && cp_parser_simulate_error (parser))
37600 goto depend_lvalue;
37601 goto skip_comma;
37603 if (kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
37604 cp_parser_commit_to_tentative_parse (parser);
37605 else
37606 array_section_p = true;
37607 if (!cp_lexer_next_token_is (parser->lexer,
37608 CPP_CLOSE_SQUARE))
37610 length = cp_parser_expression (parser);
37611 /* Later handling is not prepared to see through these. */
37612 gcc_checking_assert (!location_wrapper_p (length));
37615 /* Look for the closing `]'. */
37616 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
37617 RT_CLOSE_SQUARE))
37619 if ((kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
37620 && cp_parser_simulate_error (parser))
37621 goto depend_lvalue;
37622 goto skip_comma;
37625 dims.safe_push (omp_dim (low_bound, length, loc, no_colon));
37628 if ((kind == OMP_CLAUSE_MAP
37629 || kind == OMP_CLAUSE_FROM
37630 || kind == OMP_CLAUSE_TO)
37631 && !array_section_p
37632 && (cp_lexer_next_token_is (parser->lexer, CPP_DOT)
37633 || (allow_deref
37634 && cp_lexer_next_token_is (parser->lexer,
37635 CPP_DEREF))))
37637 for (unsigned i = 0; i < dims.length (); i++)
37639 gcc_assert (dims[i].length == integer_one_node);
37640 decl = build_array_ref (dims[i].loc,
37641 decl, dims[i].low_bound);
37643 goto start_component_ref;
37645 else
37646 for (unsigned i = 0; i < dims.length (); i++)
37647 decl = tree_cons (dims[i].low_bound, dims[i].length, decl);
37649 break;
37650 default:
37651 break;
37654 if (kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
37656 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
37657 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
37658 && cp_parser_simulate_error (parser))
37660 depend_lvalue:
37661 cp_parser_abort_tentative_parse (parser);
37662 decl = cp_parser_assignment_expression (parser, NULL,
37663 false, false);
37665 else
37666 cp_parser_parse_definitely (parser);
37669 tree u = build_omp_clause (token->location, kind);
37670 OMP_CLAUSE_DECL (u) = decl;
37671 OMP_CLAUSE_CHAIN (u) = list;
37672 list = u;
37674 else
37675 list = tree_cons (decl, NULL_TREE, list);
37677 get_comma:
37678 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
37679 break;
37680 cp_lexer_consume_token (parser->lexer);
37683 if (colon)
37684 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37686 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
37688 *colon = true;
37689 cp_parser_require (parser, CPP_COLON, RT_COLON);
37690 return list;
37693 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37695 int ending;
37697 /* Try to resync to an unnested comma. Copied from
37698 cp_parser_parenthesized_expression_list. */
37699 skip_comma:
37700 if (colon)
37701 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37702 ending = cp_parser_skip_to_closing_parenthesis (parser,
37703 /*recovering=*/true,
37704 /*or_comma=*/true,
37705 /*consume_paren=*/true);
37706 if (ending < 0)
37707 goto get_comma;
37710 return list;
37713 /* Similarly, but expect leading and trailing parenthesis. This is a very
37714 common case for omp clauses. */
37716 static tree
37717 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list,
37718 bool allow_deref = false)
37720 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37721 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL,
37722 allow_deref);
37723 return list;
37726 /* OpenACC 2.0:
37727 copy ( variable-list )
37728 copyin ( variable-list )
37729 copyout ( variable-list )
37730 create ( variable-list )
37731 delete ( variable-list )
37732 present ( variable-list )
37734 OpenACC 2.6:
37735 no_create ( variable-list )
37736 attach ( variable-list )
37737 detach ( variable-list ) */
37739 static tree
37740 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
37741 tree list)
37743 enum gomp_map_kind kind;
37744 switch (c_kind)
37746 case PRAGMA_OACC_CLAUSE_ATTACH:
37747 kind = GOMP_MAP_ATTACH;
37748 break;
37749 case PRAGMA_OACC_CLAUSE_COPY:
37750 kind = GOMP_MAP_TOFROM;
37751 break;
37752 case PRAGMA_OACC_CLAUSE_COPYIN:
37753 kind = GOMP_MAP_TO;
37754 break;
37755 case PRAGMA_OACC_CLAUSE_COPYOUT:
37756 kind = GOMP_MAP_FROM;
37757 break;
37758 case PRAGMA_OACC_CLAUSE_CREATE:
37759 kind = GOMP_MAP_ALLOC;
37760 break;
37761 case PRAGMA_OACC_CLAUSE_DELETE:
37762 kind = GOMP_MAP_RELEASE;
37763 break;
37764 case PRAGMA_OACC_CLAUSE_DETACH:
37765 kind = GOMP_MAP_DETACH;
37766 break;
37767 case PRAGMA_OACC_CLAUSE_DEVICE:
37768 kind = GOMP_MAP_FORCE_TO;
37769 break;
37770 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
37771 kind = GOMP_MAP_DEVICE_RESIDENT;
37772 break;
37773 case PRAGMA_OACC_CLAUSE_HOST:
37774 kind = GOMP_MAP_FORCE_FROM;
37775 break;
37776 case PRAGMA_OACC_CLAUSE_LINK:
37777 kind = GOMP_MAP_LINK;
37778 break;
37779 case PRAGMA_OACC_CLAUSE_NO_CREATE:
37780 kind = GOMP_MAP_IF_PRESENT;
37781 break;
37782 case PRAGMA_OACC_CLAUSE_PRESENT:
37783 kind = GOMP_MAP_FORCE_PRESENT;
37784 break;
37785 default:
37786 gcc_unreachable ();
37788 tree nl, c;
37789 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list, true);
37791 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
37792 OMP_CLAUSE_SET_MAP_KIND (c, kind);
37794 return nl;
37797 /* OpenACC 2.0:
37798 deviceptr ( variable-list ) */
37800 static tree
37801 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
37803 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37804 tree vars, t;
37806 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
37807 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
37808 variable-list must only allow for pointer variables. */
37809 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
37810 for (t = vars; t; t = TREE_CHAIN (t))
37812 tree v = TREE_PURPOSE (t);
37813 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
37814 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
37815 OMP_CLAUSE_DECL (u) = v;
37816 OMP_CLAUSE_CHAIN (u) = list;
37817 list = u;
37820 return list;
37823 /* OpenACC 2.5:
37824 auto
37825 finalize
37826 independent
37827 nohost
37828 seq */
37830 static tree
37831 cp_parser_oacc_simple_clause (location_t loc, enum omp_clause_code code,
37832 tree list)
37834 check_no_duplicate_clause (list, code, omp_clause_code_name[code], loc);
37836 tree c = build_omp_clause (loc, code);
37837 OMP_CLAUSE_CHAIN (c) = list;
37839 return c;
37842 /* OpenACC:
37843 num_gangs ( expression )
37844 num_workers ( expression )
37845 vector_length ( expression ) */
37847 static tree
37848 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
37849 const char *str, tree list)
37851 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37853 matching_parens parens;
37854 if (!parens.require_open (parser))
37855 return list;
37857 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
37859 if (t == error_mark_node
37860 || !parens.require_close (parser))
37862 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37863 /*or_comma=*/false,
37864 /*consume_paren=*/true);
37865 return list;
37868 check_no_duplicate_clause (list, code, str, loc);
37870 tree c = build_omp_clause (loc, code);
37871 OMP_CLAUSE_OPERAND (c, 0) = t;
37872 OMP_CLAUSE_CHAIN (c) = list;
37873 return c;
37876 /* OpenACC:
37878 gang [( gang-arg-list )]
37879 worker [( [num:] int-expr )]
37880 vector [( [length:] int-expr )]
37882 where gang-arg is one of:
37884 [num:] int-expr
37885 static: size-expr
37887 and size-expr may be:
37890 int-expr
37893 static tree
37894 cp_parser_oacc_shape_clause (cp_parser *parser, location_t loc,
37895 omp_clause_code kind,
37896 const char *str, tree list)
37898 const char *id = "num";
37899 cp_lexer *lexer = parser->lexer;
37900 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
37902 if (kind == OMP_CLAUSE_VECTOR)
37903 id = "length";
37905 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
37907 matching_parens parens;
37908 parens.consume_open (parser);
37912 cp_token *next = cp_lexer_peek_token (lexer);
37913 int idx = 0;
37915 /* Gang static argument. */
37916 if (kind == OMP_CLAUSE_GANG
37917 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
37919 cp_lexer_consume_token (lexer);
37921 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37922 goto cleanup_error;
37924 idx = 1;
37925 if (ops[idx] != NULL)
37927 cp_parser_error (parser, "too many %<static%> arguments");
37928 goto cleanup_error;
37931 /* Check for the '*' argument. */
37932 if (cp_lexer_next_token_is (lexer, CPP_MULT)
37933 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
37934 || cp_lexer_nth_token_is (parser->lexer, 2,
37935 CPP_CLOSE_PAREN)))
37937 cp_lexer_consume_token (lexer);
37938 ops[idx] = integer_minus_one_node;
37940 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
37942 cp_lexer_consume_token (lexer);
37943 continue;
37945 else break;
37948 /* Worker num: argument and vector length: arguments. */
37949 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
37950 && id_equal (next->u.value, id)
37951 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
37953 cp_lexer_consume_token (lexer); /* id */
37954 cp_lexer_consume_token (lexer); /* ':' */
37957 /* Now collect the actual argument. */
37958 if (ops[idx] != NULL_TREE)
37960 cp_parser_error (parser, "unexpected argument");
37961 goto cleanup_error;
37964 tree expr = cp_parser_assignment_expression (parser, NULL, false,
37965 false);
37966 if (expr == error_mark_node)
37967 goto cleanup_error;
37969 mark_exp_read (expr);
37970 ops[idx] = expr;
37972 if (kind == OMP_CLAUSE_GANG
37973 && cp_lexer_next_token_is (lexer, CPP_COMMA))
37975 cp_lexer_consume_token (lexer);
37976 continue;
37978 break;
37980 while (1);
37982 if (!parens.require_close (parser))
37983 goto cleanup_error;
37986 check_no_duplicate_clause (list, kind, str, loc);
37988 c = build_omp_clause (loc, kind);
37990 if (ops[1])
37991 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
37993 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
37994 OMP_CLAUSE_CHAIN (c) = list;
37996 return c;
37998 cleanup_error:
37999 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
38000 return list;
38003 /* OpenACC 2.0:
38004 tile ( size-expr-list ) */
38006 static tree
38007 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
38009 tree c, expr = error_mark_node;
38010 tree tile = NULL_TREE;
38012 /* Collapse and tile are mutually exclusive. (The spec doesn't say
38013 so, but the spec authors never considered such a case and have
38014 differing opinions on what it might mean, including 'not
38015 allowed'.) */
38016 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
38017 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
38018 clause_loc);
38020 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38021 return list;
38025 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
38026 return list;
38028 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
38029 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
38030 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
38032 cp_lexer_consume_token (parser->lexer);
38033 expr = integer_zero_node;
38035 else
38036 expr = cp_parser_constant_expression (parser);
38038 tile = tree_cons (NULL_TREE, expr, tile);
38040 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
38042 /* Consume the trailing ')'. */
38043 cp_lexer_consume_token (parser->lexer);
38045 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
38046 tile = nreverse (tile);
38047 OMP_CLAUSE_TILE_LIST (c) = tile;
38048 OMP_CLAUSE_CHAIN (c) = list;
38049 return c;
38052 /* OpenACC 2.0
38053 Parse wait clause or directive parameters. */
38055 static tree
38056 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
38058 vec<tree, va_gc> *args;
38059 tree t, args_tree;
38061 args = cp_parser_parenthesized_expression_list (parser, non_attr,
38062 /*cast_p=*/false,
38063 /*allow_expansion_p=*/true,
38064 /*non_constant_p=*/NULL);
38066 if (args == NULL || args->length () == 0)
38068 if (args != NULL)
38070 cp_parser_error (parser, "expected integer expression list");
38071 release_tree_vector (args);
38073 return list;
38076 args_tree = build_tree_list_vec (args);
38078 release_tree_vector (args);
38080 for (t = args_tree; t; t = TREE_CHAIN (t))
38082 tree targ = TREE_VALUE (t);
38084 if (targ != error_mark_node)
38086 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
38087 error ("%<wait%> expression must be integral");
38088 else
38090 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
38092 targ = mark_rvalue_use (targ);
38093 OMP_CLAUSE_DECL (c) = targ;
38094 OMP_CLAUSE_CHAIN (c) = list;
38095 list = c;
38100 return list;
38103 /* OpenACC:
38104 wait [( int-expr-list )] */
38106 static tree
38107 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
38109 location_t location = cp_lexer_peek_token (parser->lexer)->location;
38111 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
38112 list = cp_parser_oacc_wait_list (parser, location, list);
38113 else
38115 tree c = build_omp_clause (location, OMP_CLAUSE_WAIT);
38117 OMP_CLAUSE_DECL (c) = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
38118 OMP_CLAUSE_CHAIN (c) = list;
38119 list = c;
38122 return list;
38125 /* OpenMP 3.0:
38126 collapse ( constant-expression ) */
38128 static tree
38129 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
38131 tree c, num;
38132 location_t loc;
38133 HOST_WIDE_INT n;
38135 loc = cp_lexer_peek_token (parser->lexer)->location;
38136 matching_parens parens;
38137 if (!parens.require_open (parser))
38138 return list;
38140 num = cp_parser_constant_expression (parser);
38142 if (!parens.require_close (parser))
38143 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38144 /*or_comma=*/false,
38145 /*consume_paren=*/true);
38147 if (num == error_mark_node)
38148 return list;
38149 num = fold_non_dependent_expr (num);
38150 if (!tree_fits_shwi_p (num)
38151 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
38152 || (n = tree_to_shwi (num)) <= 0
38153 || (int) n != n)
38155 error_at (loc, "collapse argument needs positive constant integer expression");
38156 return list;
38159 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
38160 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
38161 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
38162 OMP_CLAUSE_CHAIN (c) = list;
38163 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
38165 return c;
38168 /* OpenMP 2.5:
38169 default ( none | shared )
38171 OpenMP 5.1:
38172 default ( private | firstprivate )
38174 OpenACC:
38175 default ( none | present ) */
38177 static tree
38178 cp_parser_omp_clause_default (cp_parser *parser, tree list,
38179 location_t location, bool is_oacc)
38181 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
38182 tree c;
38184 matching_parens parens;
38185 if (!parens.require_open (parser))
38186 return list;
38187 if (!is_oacc && cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
38189 kind = OMP_CLAUSE_DEFAULT_PRIVATE;
38190 cp_lexer_consume_token (parser->lexer);
38192 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38194 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38195 const char *p = IDENTIFIER_POINTER (id);
38197 switch (p[0])
38199 case 'n':
38200 if (strcmp ("none", p) != 0)
38201 goto invalid_kind;
38202 kind = OMP_CLAUSE_DEFAULT_NONE;
38203 break;
38205 case 'p':
38206 if (strcmp ("present", p) != 0 || !is_oacc)
38207 goto invalid_kind;
38208 kind = OMP_CLAUSE_DEFAULT_PRESENT;
38209 break;
38211 case 'f':
38212 if (strcmp ("firstprivate", p) != 0 || is_oacc)
38213 goto invalid_kind;
38214 kind = OMP_CLAUSE_DEFAULT_FIRSTPRIVATE;
38215 break;
38217 case 's':
38218 if (strcmp ("shared", p) != 0 || is_oacc)
38219 goto invalid_kind;
38220 kind = OMP_CLAUSE_DEFAULT_SHARED;
38221 break;
38223 default:
38224 goto invalid_kind;
38227 cp_lexer_consume_token (parser->lexer);
38229 else
38231 invalid_kind:
38232 if (is_oacc)
38233 cp_parser_error (parser, "expected %<none%> or %<present%>");
38234 else
38235 cp_parser_error (parser, "expected %<none%>, %<shared%>, "
38236 "%<private%> or %<firstprivate%>");
38239 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
38240 || !parens.require_close (parser))
38241 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38242 /*or_comma=*/false,
38243 /*consume_paren=*/true);
38245 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
38246 return list;
38248 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
38249 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
38250 OMP_CLAUSE_CHAIN (c) = list;
38251 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
38253 return c;
38256 /* OpenMP 3.1:
38257 final ( expression ) */
38259 static tree
38260 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
38262 tree t, c;
38264 matching_parens parens;
38265 if (!parens.require_open (parser))
38266 return list;
38268 t = cp_parser_assignment_expression (parser);
38270 if (t == error_mark_node
38271 || !parens.require_close (parser))
38272 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38273 /*or_comma=*/false,
38274 /*consume_paren=*/true);
38276 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
38278 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
38279 OMP_CLAUSE_FINAL_EXPR (c) = t;
38280 OMP_CLAUSE_CHAIN (c) = list;
38282 return c;
38285 /* OpenMP 2.5:
38286 if ( expression )
38288 OpenMP 4.5:
38289 if ( directive-name-modifier : expression )
38291 directive-name-modifier:
38292 parallel | task | taskloop | target data | target | target update
38293 | target enter data | target exit data
38295 OpenMP 5.0:
38296 directive-name-modifier:
38297 ... | simd | cancel */
38299 static tree
38300 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
38301 bool is_omp)
38303 tree t, c;
38304 enum tree_code if_modifier = ERROR_MARK;
38306 matching_parens parens;
38307 if (!parens.require_open (parser))
38308 return list;
38310 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38312 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38313 const char *p = IDENTIFIER_POINTER (id);
38314 int n = 2;
38316 if (strcmp ("cancel", p) == 0)
38317 if_modifier = VOID_CST;
38318 else if (strcmp ("parallel", p) == 0)
38319 if_modifier = OMP_PARALLEL;
38320 else if (strcmp ("simd", p) == 0)
38321 if_modifier = OMP_SIMD;
38322 else if (strcmp ("task", p) == 0)
38323 if_modifier = OMP_TASK;
38324 else if (strcmp ("taskloop", p) == 0)
38325 if_modifier = OMP_TASKLOOP;
38326 else if (strcmp ("target", p) == 0)
38328 if_modifier = OMP_TARGET;
38329 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
38331 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
38332 p = IDENTIFIER_POINTER (id);
38333 if (strcmp ("data", p) == 0)
38334 if_modifier = OMP_TARGET_DATA;
38335 else if (strcmp ("update", p) == 0)
38336 if_modifier = OMP_TARGET_UPDATE;
38337 else if (strcmp ("enter", p) == 0)
38338 if_modifier = OMP_TARGET_ENTER_DATA;
38339 else if (strcmp ("exit", p) == 0)
38340 if_modifier = OMP_TARGET_EXIT_DATA;
38341 if (if_modifier != OMP_TARGET)
38342 n = 3;
38343 else
38345 location_t loc
38346 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
38347 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
38348 "or %<exit%>");
38349 if_modifier = ERROR_MARK;
38351 if (if_modifier == OMP_TARGET_ENTER_DATA
38352 || if_modifier == OMP_TARGET_EXIT_DATA)
38354 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
38356 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
38357 p = IDENTIFIER_POINTER (id);
38358 if (strcmp ("data", p) == 0)
38359 n = 4;
38361 if (n != 4)
38363 location_t loc
38364 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
38365 error_at (loc, "expected %<data%>");
38366 if_modifier = ERROR_MARK;
38371 if (if_modifier != ERROR_MARK)
38373 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
38375 while (n-- > 0)
38376 cp_lexer_consume_token (parser->lexer);
38378 else
38380 if (n > 2)
38382 location_t loc
38383 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
38384 error_at (loc, "expected %<:%>");
38386 if_modifier = ERROR_MARK;
38391 t = cp_parser_assignment_expression (parser);
38393 if (t == error_mark_node
38394 || !parens.require_close (parser))
38395 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38396 /*or_comma=*/false,
38397 /*consume_paren=*/true);
38399 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
38400 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
38402 if (if_modifier != ERROR_MARK
38403 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
38405 const char *p = NULL;
38406 switch (if_modifier)
38408 case VOID_CST: p = "cancel"; break;
38409 case OMP_PARALLEL: p = "parallel"; break;
38410 case OMP_SIMD: p = "simd"; break;
38411 case OMP_TASK: p = "task"; break;
38412 case OMP_TASKLOOP: p = "taskloop"; break;
38413 case OMP_TARGET_DATA: p = "target data"; break;
38414 case OMP_TARGET: p = "target"; break;
38415 case OMP_TARGET_UPDATE: p = "target update"; break;
38416 case OMP_TARGET_ENTER_DATA: p = "target enter data"; break;
38417 case OMP_TARGET_EXIT_DATA: p = "target exit data"; break;
38418 default: gcc_unreachable ();
38420 error_at (location, "too many %<if%> clauses with %qs modifier",
38422 return list;
38424 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
38426 if (!is_omp)
38427 error_at (location, "too many %<if%> clauses");
38428 else
38429 error_at (location, "too many %<if%> clauses without modifier");
38430 return list;
38432 else if (if_modifier == ERROR_MARK
38433 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
38435 error_at (location, "if any %<if%> clause has modifier, then all "
38436 "%<if%> clauses have to use modifier");
38437 return list;
38441 c = build_omp_clause (location, OMP_CLAUSE_IF);
38442 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
38443 OMP_CLAUSE_IF_EXPR (c) = t;
38444 OMP_CLAUSE_CHAIN (c) = list;
38446 return c;
38449 /* OpenMP 3.1:
38450 mergeable */
38452 static tree
38453 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
38454 tree list, location_t location)
38456 tree c;
38458 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
38459 location);
38461 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
38462 OMP_CLAUSE_CHAIN (c) = list;
38463 return c;
38466 /* OpenMP 2.5:
38467 nowait */
38469 static tree
38470 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
38471 tree list, location_t location)
38473 tree c;
38475 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
38477 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
38478 OMP_CLAUSE_CHAIN (c) = list;
38479 return c;
38482 /* OpenMP 2.5:
38483 num_threads ( expression ) */
38485 static tree
38486 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
38487 location_t location)
38489 tree t, c;
38491 matching_parens parens;
38492 if (!parens.require_open (parser))
38493 return list;
38495 t = cp_parser_assignment_expression (parser);
38497 if (t == error_mark_node
38498 || !parens.require_close (parser))
38499 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38500 /*or_comma=*/false,
38501 /*consume_paren=*/true);
38503 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
38504 "num_threads", location);
38506 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
38507 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
38508 OMP_CLAUSE_CHAIN (c) = list;
38510 return c;
38513 /* OpenMP 4.5:
38514 num_tasks ( expression )
38516 OpenMP 5.1:
38517 num_tasks ( strict : expression ) */
38519 static tree
38520 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
38521 location_t location)
38523 tree t, c;
38525 matching_parens parens;
38526 if (!parens.require_open (parser))
38527 return list;
38529 bool strict = false;
38530 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
38531 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
38533 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38534 if (!strcmp (IDENTIFIER_POINTER (id), "strict"))
38536 strict = true;
38537 cp_lexer_consume_token (parser->lexer);
38538 cp_lexer_consume_token (parser->lexer);
38542 t = cp_parser_assignment_expression (parser);
38544 if (t == error_mark_node
38545 || !parens.require_close (parser))
38546 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38547 /*or_comma=*/false,
38548 /*consume_paren=*/true);
38550 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
38551 "num_tasks", location);
38553 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
38554 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
38555 OMP_CLAUSE_NUM_TASKS_STRICT (c) = strict;
38556 OMP_CLAUSE_CHAIN (c) = list;
38558 return c;
38561 /* OpenMP 4.5:
38562 grainsize ( expression )
38564 OpenMP 5.1:
38565 grainsize ( strict : expression ) */
38567 static tree
38568 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
38569 location_t location)
38571 tree t, c;
38573 matching_parens parens;
38574 if (!parens.require_open (parser))
38575 return list;
38577 bool strict = false;
38578 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
38579 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
38581 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38582 if (!strcmp (IDENTIFIER_POINTER (id), "strict"))
38584 strict = true;
38585 cp_lexer_consume_token (parser->lexer);
38586 cp_lexer_consume_token (parser->lexer);
38590 t = cp_parser_assignment_expression (parser);
38592 if (t == error_mark_node
38593 || !parens.require_close (parser))
38594 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38595 /*or_comma=*/false,
38596 /*consume_paren=*/true);
38598 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
38599 "grainsize", location);
38601 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
38602 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
38603 OMP_CLAUSE_GRAINSIZE_STRICT (c) = strict;
38604 OMP_CLAUSE_CHAIN (c) = list;
38606 return c;
38609 /* OpenMP 4.5:
38610 priority ( expression ) */
38612 static tree
38613 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
38614 location_t location)
38616 tree t, c;
38618 matching_parens parens;
38619 if (!parens.require_open (parser))
38620 return list;
38622 t = cp_parser_assignment_expression (parser);
38624 if (t == error_mark_node
38625 || !parens.require_close (parser))
38626 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38627 /*or_comma=*/false,
38628 /*consume_paren=*/true);
38630 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
38631 "priority", location);
38633 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
38634 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
38635 OMP_CLAUSE_CHAIN (c) = list;
38637 return c;
38640 /* OpenMP 4.5:
38641 hint ( expression ) */
38643 static tree
38644 cp_parser_omp_clause_hint (cp_parser *parser, tree list, location_t location)
38646 tree t, c;
38648 matching_parens parens;
38649 if (!parens.require_open (parser))
38650 return list;
38652 t = cp_parser_assignment_expression (parser);
38654 if (t != error_mark_node)
38656 t = fold_non_dependent_expr (t);
38657 if (!value_dependent_expression_p (t)
38658 && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
38659 || !tree_fits_shwi_p (t)
38660 || tree_int_cst_sgn (t) == -1))
38661 error_at (location, "expected constant integer expression with "
38662 "valid sync-hint value");
38664 if (t == error_mark_node
38665 || !parens.require_close (parser))
38666 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38667 /*or_comma=*/false,
38668 /*consume_paren=*/true);
38669 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
38671 c = build_omp_clause (location, OMP_CLAUSE_HINT);
38672 OMP_CLAUSE_HINT_EXPR (c) = t;
38673 OMP_CLAUSE_CHAIN (c) = list;
38675 return c;
38678 /* OpenMP 5.1:
38679 filter ( integer-expression ) */
38681 static tree
38682 cp_parser_omp_clause_filter (cp_parser *parser, tree list, location_t location)
38684 tree t, c;
38686 matching_parens parens;
38687 if (!parens.require_open (parser))
38688 return list;
38690 t = cp_parser_assignment_expression (parser);
38692 if (t == error_mark_node
38693 || !parens.require_close (parser))
38694 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38695 /*or_comma=*/false,
38696 /*consume_paren=*/true);
38697 check_no_duplicate_clause (list, OMP_CLAUSE_FILTER, "filter", location);
38699 c = build_omp_clause (location, OMP_CLAUSE_FILTER);
38700 OMP_CLAUSE_FILTER_EXPR (c) = t;
38701 OMP_CLAUSE_CHAIN (c) = list;
38703 return c;
38706 /* OpenMP 4.5:
38707 defaultmap ( tofrom : scalar )
38709 OpenMP 5.0:
38710 defaultmap ( implicit-behavior [ : variable-category ] ) */
38712 static tree
38713 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
38714 location_t location)
38716 tree c, id;
38717 const char *p;
38718 enum omp_clause_defaultmap_kind behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
38719 enum omp_clause_defaultmap_kind category
38720 = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED;
38722 matching_parens parens;
38723 if (!parens.require_open (parser))
38724 return list;
38726 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
38727 p = "default";
38728 else if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38730 invalid_behavior:
38731 cp_parser_error (parser, "expected %<alloc%>, %<to%>, %<from%>, "
38732 "%<tofrom%>, %<firstprivate%>, %<none%> "
38733 "or %<default%>");
38734 goto out_err;
38736 else
38738 id = cp_lexer_peek_token (parser->lexer)->u.value;
38739 p = IDENTIFIER_POINTER (id);
38742 switch (p[0])
38744 case 'a':
38745 if (strcmp ("alloc", p) == 0)
38746 behavior = OMP_CLAUSE_DEFAULTMAP_ALLOC;
38747 else
38748 goto invalid_behavior;
38749 break;
38751 case 'd':
38752 if (strcmp ("default", p) == 0)
38753 behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
38754 else
38755 goto invalid_behavior;
38756 break;
38758 case 'f':
38759 if (strcmp ("firstprivate", p) == 0)
38760 behavior = OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE;
38761 else if (strcmp ("from", p) == 0)
38762 behavior = OMP_CLAUSE_DEFAULTMAP_FROM;
38763 else
38764 goto invalid_behavior;
38765 break;
38767 case 'n':
38768 if (strcmp ("none", p) == 0)
38769 behavior = OMP_CLAUSE_DEFAULTMAP_NONE;
38770 else
38771 goto invalid_behavior;
38772 break;
38774 case 't':
38775 if (strcmp ("tofrom", p) == 0)
38776 behavior = OMP_CLAUSE_DEFAULTMAP_TOFROM;
38777 else if (strcmp ("to", p) == 0)
38778 behavior = OMP_CLAUSE_DEFAULTMAP_TO;
38779 else
38780 goto invalid_behavior;
38781 break;
38783 default:
38784 goto invalid_behavior;
38786 cp_lexer_consume_token (parser->lexer);
38788 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
38790 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
38791 goto out_err;
38793 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38795 invalid_category:
38796 cp_parser_error (parser, "expected %<scalar%>, %<aggregate%> or "
38797 "%<pointer%>");
38798 goto out_err;
38800 id = cp_lexer_peek_token (parser->lexer)->u.value;
38801 p = IDENTIFIER_POINTER (id);
38803 switch (p[0])
38805 case 'a':
38806 if (strcmp ("aggregate", p) == 0)
38807 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE;
38808 else
38809 goto invalid_category;
38810 break;
38812 case 'p':
38813 if (strcmp ("pointer", p) == 0)
38814 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER;
38815 else
38816 goto invalid_category;
38817 break;
38819 case 's':
38820 if (strcmp ("scalar", p) == 0)
38821 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR;
38822 else
38823 goto invalid_category;
38824 break;
38826 default:
38827 goto invalid_category;
38830 cp_lexer_consume_token (parser->lexer);
38832 if (!parens.require_close (parser))
38833 goto out_err;
38835 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
38836 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEFAULTMAP
38837 && (category == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
38838 || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c) == category
38839 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c)
38840 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)))
38842 enum omp_clause_defaultmap_kind cat = category;
38843 location_t loc = OMP_CLAUSE_LOCATION (c);
38844 if (cat == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)
38845 cat = OMP_CLAUSE_DEFAULTMAP_CATEGORY (c);
38846 p = NULL;
38847 switch (cat)
38849 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED:
38850 p = NULL;
38851 break;
38852 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE:
38853 p = "aggregate";
38854 break;
38855 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER:
38856 p = "pointer";
38857 break;
38858 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR:
38859 p = "scalar";
38860 break;
38861 default:
38862 gcc_unreachable ();
38864 if (p)
38865 error_at (loc, "too many %<defaultmap%> clauses with %qs category",
38867 else
38868 error_at (loc, "too many %<defaultmap%> clauses with unspecified "
38869 "category");
38870 break;
38873 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
38874 OMP_CLAUSE_DEFAULTMAP_SET_KIND (c, behavior, category);
38875 OMP_CLAUSE_CHAIN (c) = list;
38876 return c;
38878 out_err:
38879 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38880 /*or_comma=*/false,
38881 /*consume_paren=*/true);
38882 return list;
38885 /* OpenMP 5.0:
38886 order ( concurrent )
38888 OpenMP 5.1:
38889 order ( order-modifier : concurrent )
38891 order-modifier:
38892 reproducible
38893 unconstrained */
38895 static tree
38896 cp_parser_omp_clause_order (cp_parser *parser, tree list, location_t location)
38898 tree c, id;
38899 const char *p;
38900 bool unconstrained = false;
38901 bool reproducible = false;
38903 matching_parens parens;
38904 if (!parens.require_open (parser))
38905 return list;
38907 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
38908 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
38910 id = cp_lexer_peek_token (parser->lexer)->u.value;
38911 p = IDENTIFIER_POINTER (id);
38912 if (strcmp (p, "unconstrained") == 0)
38913 unconstrained = true;
38914 else if (strcmp (p, "reproducible") == 0)
38915 reproducible = true;
38916 else
38918 cp_parser_error (parser, "expected %<reproducible%> or "
38919 "%<unconstrained%>");
38920 goto out_err;
38922 cp_lexer_consume_token (parser->lexer);
38923 cp_lexer_consume_token (parser->lexer);
38925 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38927 cp_parser_error (parser, "expected %<concurrent%>");
38928 goto out_err;
38930 else
38932 id = cp_lexer_peek_token (parser->lexer)->u.value;
38933 p = IDENTIFIER_POINTER (id);
38935 if (strcmp (p, "concurrent") != 0)
38937 cp_parser_error (parser, "expected %<concurrent%>");
38938 goto out_err;
38940 cp_lexer_consume_token (parser->lexer);
38941 if (!parens.require_close (parser))
38942 goto out_err;
38944 check_no_duplicate_clause (list, OMP_CLAUSE_ORDER, "order", location);
38945 c = build_omp_clause (location, OMP_CLAUSE_ORDER);
38946 OMP_CLAUSE_ORDER_UNCONSTRAINED (c) = unconstrained;
38947 OMP_CLAUSE_ORDER_REPRODUCIBLE (c) = reproducible;
38948 OMP_CLAUSE_CHAIN (c) = list;
38949 return c;
38951 out_err:
38952 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38953 /*or_comma=*/false,
38954 /*consume_paren=*/true);
38955 return list;
38958 /* OpenMP 5.0:
38959 bind ( teams | parallel | thread ) */
38961 static tree
38962 cp_parser_omp_clause_bind (cp_parser *parser, tree list,
38963 location_t location)
38965 tree c;
38966 const char *p;
38967 enum omp_clause_bind_kind kind = OMP_CLAUSE_BIND_THREAD;
38969 matching_parens parens;
38970 if (!parens.require_open (parser))
38971 return list;
38973 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38975 invalid:
38976 cp_parser_error (parser,
38977 "expected %<teams%>, %<parallel%> or %<thread%>");
38978 goto out_err;
38980 else
38982 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38983 p = IDENTIFIER_POINTER (id);
38985 if (strcmp (p, "teams") == 0)
38986 kind = OMP_CLAUSE_BIND_TEAMS;
38987 else if (strcmp (p, "parallel") == 0)
38988 kind = OMP_CLAUSE_BIND_PARALLEL;
38989 else if (strcmp (p, "thread") != 0)
38990 goto invalid;
38991 cp_lexer_consume_token (parser->lexer);
38992 if (!parens.require_close (parser))
38993 goto out_err;
38995 /* check_no_duplicate_clause (list, OMP_CLAUSE_BIND, "bind", location); */
38996 c = build_omp_clause (location, OMP_CLAUSE_BIND);
38997 OMP_CLAUSE_BIND_KIND (c) = kind;
38998 OMP_CLAUSE_CHAIN (c) = list;
38999 return c;
39001 out_err:
39002 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39003 /*or_comma=*/false,
39004 /*consume_paren=*/true);
39005 return list;
39008 /* OpenMP 2.5:
39009 ordered
39011 OpenMP 4.5:
39012 ordered ( constant-expression ) */
39014 static tree
39015 cp_parser_omp_clause_ordered (cp_parser *parser,
39016 tree list, location_t location)
39018 tree c, num = NULL_TREE;
39019 HOST_WIDE_INT n;
39021 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
39022 "ordered", location);
39024 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
39026 matching_parens parens;
39027 parens.consume_open (parser);
39029 num = cp_parser_constant_expression (parser);
39031 if (!parens.require_close (parser))
39032 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39033 /*or_comma=*/false,
39034 /*consume_paren=*/true);
39036 if (num == error_mark_node)
39037 return list;
39038 num = fold_non_dependent_expr (num);
39039 if (!tree_fits_shwi_p (num)
39040 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
39041 || (n = tree_to_shwi (num)) <= 0
39042 || (int) n != n)
39044 error_at (location,
39045 "ordered argument needs positive constant integer "
39046 "expression");
39047 return list;
39051 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
39052 OMP_CLAUSE_ORDERED_EXPR (c) = num;
39053 OMP_CLAUSE_CHAIN (c) = list;
39054 return c;
39057 /* OpenMP 2.5:
39058 reduction ( reduction-operator : variable-list )
39060 reduction-operator:
39061 One of: + * - & ^ | && ||
39063 OpenMP 3.1:
39065 reduction-operator:
39066 One of: + * - & ^ | && || min max
39068 OpenMP 4.0:
39070 reduction-operator:
39071 One of: + * - & ^ | && ||
39072 id-expression
39074 OpenMP 5.0:
39075 reduction ( reduction-modifier, reduction-operator : variable-list )
39076 in_reduction ( reduction-operator : variable-list )
39077 task_reduction ( reduction-operator : variable-list ) */
39079 static tree
39080 cp_parser_omp_clause_reduction (cp_parser *parser, enum omp_clause_code kind,
39081 bool is_omp, tree list)
39083 enum tree_code code = ERROR_MARK;
39084 tree nlist, c, id = NULL_TREE;
39085 bool task = false;
39086 bool inscan = false;
39088 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
39089 return list;
39091 if (kind == OMP_CLAUSE_REDUCTION && is_omp)
39093 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT)
39094 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
39096 cp_lexer_consume_token (parser->lexer);
39097 cp_lexer_consume_token (parser->lexer);
39099 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
39100 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
39102 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39103 const char *p = IDENTIFIER_POINTER (id);
39104 if (strcmp (p, "task") == 0)
39105 task = true;
39106 else if (strcmp (p, "inscan") == 0)
39107 inscan = true;
39108 if (task || inscan)
39110 cp_lexer_consume_token (parser->lexer);
39111 cp_lexer_consume_token (parser->lexer);
39116 switch (cp_lexer_peek_token (parser->lexer)->type)
39118 case CPP_PLUS: code = PLUS_EXPR; break;
39119 case CPP_MULT: code = MULT_EXPR; break;
39120 case CPP_MINUS: code = MINUS_EXPR; break;
39121 case CPP_AND: code = BIT_AND_EXPR; break;
39122 case CPP_XOR: code = BIT_XOR_EXPR; break;
39123 case CPP_OR: code = BIT_IOR_EXPR; break;
39124 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
39125 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
39126 default: break;
39129 if (code != ERROR_MARK)
39130 cp_lexer_consume_token (parser->lexer);
39131 else
39133 bool saved_colon_corrects_to_scope_p;
39134 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
39135 parser->colon_corrects_to_scope_p = false;
39136 id = cp_parser_id_expression (parser, /*template_p=*/false,
39137 /*check_dependency_p=*/true,
39138 /*template_p=*/NULL,
39139 /*declarator_p=*/false,
39140 /*optional_p=*/false);
39141 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
39142 if (identifier_p (id))
39144 const char *p = IDENTIFIER_POINTER (id);
39146 if (strcmp (p, "min") == 0)
39147 code = MIN_EXPR;
39148 else if (strcmp (p, "max") == 0)
39149 code = MAX_EXPR;
39150 else if (id == ovl_op_identifier (false, PLUS_EXPR))
39151 code = PLUS_EXPR;
39152 else if (id == ovl_op_identifier (false, MULT_EXPR))
39153 code = MULT_EXPR;
39154 else if (id == ovl_op_identifier (false, MINUS_EXPR))
39155 code = MINUS_EXPR;
39156 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
39157 code = BIT_AND_EXPR;
39158 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
39159 code = BIT_IOR_EXPR;
39160 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
39161 code = BIT_XOR_EXPR;
39162 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
39163 code = TRUTH_ANDIF_EXPR;
39164 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
39165 code = TRUTH_ORIF_EXPR;
39166 id = omp_reduction_id (code, id, NULL_TREE);
39167 tree scope = parser->scope;
39168 if (scope)
39169 id = build_qualified_name (NULL_TREE, scope, id, false);
39170 parser->scope = NULL_TREE;
39171 parser->qualifying_scope = NULL_TREE;
39172 parser->object_scope = NULL_TREE;
39174 else
39176 error ("invalid reduction-identifier");
39177 resync_fail:
39178 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39179 /*or_comma=*/false,
39180 /*consume_paren=*/true);
39181 return list;
39185 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
39186 goto resync_fail;
39188 nlist = cp_parser_omp_var_list_no_open (parser, kind, list,
39189 NULL);
39190 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
39192 OMP_CLAUSE_REDUCTION_CODE (c) = code;
39193 if (task)
39194 OMP_CLAUSE_REDUCTION_TASK (c) = 1;
39195 else if (inscan)
39196 OMP_CLAUSE_REDUCTION_INSCAN (c) = 1;
39197 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
39200 return nlist;
39203 /* OpenMP 2.5:
39204 schedule ( schedule-kind )
39205 schedule ( schedule-kind , expression )
39207 schedule-kind:
39208 static | dynamic | guided | runtime | auto
39210 OpenMP 4.5:
39211 schedule ( schedule-modifier : schedule-kind )
39212 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
39214 schedule-modifier:
39215 simd
39216 monotonic
39217 nonmonotonic */
39219 static tree
39220 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
39222 tree c, t;
39223 int modifiers = 0, nmodifiers = 0;
39225 matching_parens parens;
39226 if (!parens.require_open (parser))
39227 return list;
39229 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
39231 location_t comma = UNKNOWN_LOCATION;
39232 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39234 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39235 const char *p = IDENTIFIER_POINTER (id);
39236 if (strcmp ("simd", p) == 0)
39237 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
39238 else if (strcmp ("monotonic", p) == 0)
39239 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
39240 else if (strcmp ("nonmonotonic", p) == 0)
39241 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
39242 else
39243 break;
39244 comma = UNKNOWN_LOCATION;
39245 cp_lexer_consume_token (parser->lexer);
39246 if (nmodifiers++ == 0
39247 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39249 comma = cp_lexer_peek_token (parser->lexer)->location;
39250 cp_lexer_consume_token (parser->lexer);
39252 else
39254 cp_parser_require (parser, CPP_COLON, RT_COLON);
39255 break;
39258 if (comma != UNKNOWN_LOCATION)
39259 error_at (comma, "expected %<:%>");
39261 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39263 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39264 const char *p = IDENTIFIER_POINTER (id);
39266 switch (p[0])
39268 case 'd':
39269 if (strcmp ("dynamic", p) != 0)
39270 goto invalid_kind;
39271 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
39272 break;
39274 case 'g':
39275 if (strcmp ("guided", p) != 0)
39276 goto invalid_kind;
39277 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
39278 break;
39280 case 'r':
39281 if (strcmp ("runtime", p) != 0)
39282 goto invalid_kind;
39283 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
39284 break;
39286 default:
39287 goto invalid_kind;
39290 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
39291 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
39292 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
39293 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
39294 else
39295 goto invalid_kind;
39296 cp_lexer_consume_token (parser->lexer);
39298 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
39299 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
39300 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
39301 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
39303 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
39304 "specified");
39305 modifiers = 0;
39308 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39310 cp_token *token;
39311 cp_lexer_consume_token (parser->lexer);
39313 token = cp_lexer_peek_token (parser->lexer);
39314 t = cp_parser_assignment_expression (parser);
39316 if (t == error_mark_node)
39317 goto resync_fail;
39318 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
39319 error_at (token->location, "schedule %<runtime%> does not take "
39320 "a %<chunk_size%> parameter");
39321 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
39322 error_at (token->location, "schedule %<auto%> does not take "
39323 "a %<chunk_size%> parameter");
39324 else
39325 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
39327 if (!parens.require_close (parser))
39328 goto resync_fail;
39330 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
39331 goto resync_fail;
39333 OMP_CLAUSE_SCHEDULE_KIND (c)
39334 = (enum omp_clause_schedule_kind)
39335 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
39337 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
39338 OMP_CLAUSE_CHAIN (c) = list;
39339 return c;
39341 invalid_kind:
39342 cp_parser_error (parser, "invalid schedule kind");
39343 resync_fail:
39344 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39345 /*or_comma=*/false,
39346 /*consume_paren=*/true);
39347 return list;
39350 /* OpenMP 3.0:
39351 untied */
39353 static tree
39354 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
39355 tree list, location_t location)
39357 tree c;
39359 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
39361 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
39362 OMP_CLAUSE_CHAIN (c) = list;
39363 return c;
39366 /* OpenMP 4.0:
39367 inbranch
39368 notinbranch */
39370 static tree
39371 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
39372 tree list, location_t location)
39374 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
39375 tree c = build_omp_clause (location, code);
39376 OMP_CLAUSE_CHAIN (c) = list;
39377 return c;
39380 /* OpenMP 4.0:
39381 parallel
39383 sections
39384 taskgroup */
39386 static tree
39387 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
39388 enum omp_clause_code code,
39389 tree list, location_t location)
39391 tree c = build_omp_clause (location, code);
39392 OMP_CLAUSE_CHAIN (c) = list;
39393 return c;
39396 /* OpenMP 4.5:
39397 nogroup */
39399 static tree
39400 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
39401 tree list, location_t location)
39403 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
39404 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
39405 OMP_CLAUSE_CHAIN (c) = list;
39406 return c;
39409 /* OpenMP 4.5:
39410 simd
39411 threads */
39413 static tree
39414 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
39415 enum omp_clause_code code,
39416 tree list, location_t location)
39418 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
39419 tree c = build_omp_clause (location, code);
39420 OMP_CLAUSE_CHAIN (c) = list;
39421 return c;
39424 /* OpenMP 4.0:
39425 num_teams ( expression )
39427 OpenMP 5.1:
39428 num_teams ( expression : expression ) */
39430 static tree
39431 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
39432 location_t location)
39434 tree upper, lower = NULL_TREE, c;
39436 matching_parens parens;
39437 if (!parens.require_open (parser))
39438 return list;
39440 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
39441 parser->colon_corrects_to_scope_p = false;
39442 upper = cp_parser_assignment_expression (parser);
39443 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
39445 if (upper != error_mark_node
39446 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
39448 lower = upper;
39449 cp_lexer_consume_token (parser->lexer);
39450 upper = cp_parser_assignment_expression (parser);
39453 if (upper == error_mark_node
39454 || !parens.require_close (parser))
39455 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39456 /*or_comma=*/false,
39457 /*consume_paren=*/true);
39459 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
39460 "num_teams", location);
39462 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
39463 OMP_CLAUSE_NUM_TEAMS_UPPER_EXPR (c) = upper;
39464 OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c) = lower;
39465 OMP_CLAUSE_CHAIN (c) = list;
39467 return c;
39470 /* OpenMP 4.0:
39471 thread_limit ( expression ) */
39473 static tree
39474 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
39475 location_t location)
39477 tree t, c;
39479 matching_parens parens;
39480 if (!parens.require_open (parser))
39481 return list;
39483 t = cp_parser_assignment_expression (parser);
39485 if (t == error_mark_node
39486 || !parens.require_close (parser))
39487 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39488 /*or_comma=*/false,
39489 /*consume_paren=*/true);
39491 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
39492 "thread_limit", location);
39494 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
39495 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
39496 OMP_CLAUSE_CHAIN (c) = list;
39498 return c;
39501 /* OpenMP 4.0:
39502 aligned ( variable-list )
39503 aligned ( variable-list : constant-expression ) */
39505 static tree
39506 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
39508 tree nlist, c, alignment = NULL_TREE;
39509 bool colon;
39511 matching_parens parens;
39512 if (!parens.require_open (parser))
39513 return list;
39515 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
39516 &colon);
39518 if (colon)
39520 alignment = cp_parser_constant_expression (parser);
39522 if (!parens.require_close (parser))
39523 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39524 /*or_comma=*/false,
39525 /*consume_paren=*/true);
39527 if (alignment == error_mark_node)
39528 alignment = NULL_TREE;
39531 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
39532 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
39534 return nlist;
39537 /* OpenMP 5.0:
39538 allocate ( variable-list )
39539 allocate ( expression : variable-list )
39541 OpenMP 5.1:
39542 allocate ( allocator-modifier : variable-list )
39543 allocate ( allocator-modifier , allocator-modifier : variable-list )
39545 allocator-modifier:
39546 allocator ( expression )
39547 align ( expression ) */
39549 static tree
39550 cp_parser_omp_clause_allocate (cp_parser *parser, tree list)
39552 tree nlist, c, allocator = NULL_TREE, align = NULL_TREE;
39553 bool colon, has_modifiers = false;
39555 matching_parens parens;
39556 if (!parens.require_open (parser))
39557 return list;
39559 cp_parser_parse_tentatively (parser);
39560 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
39561 parser->colon_corrects_to_scope_p = false;
39562 for (int mod = 0; mod < 2; mod++)
39563 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
39564 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
39566 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39567 const char *p = IDENTIFIER_POINTER (id);
39568 if (strcmp (p, "allocator") != 0 && strcmp (p, "align") != 0)
39569 break;
39570 cp_lexer_consume_token (parser->lexer);
39571 matching_parens parens2;
39572 if (!parens2.require_open (parser))
39573 break;
39574 if (strcmp (p, "allocator") == 0)
39576 if (allocator != NULL_TREE)
39577 break;
39578 allocator = cp_parser_assignment_expression (parser);
39580 else
39582 if (align != NULL_TREE)
39583 break;
39584 align = cp_parser_assignment_expression (parser);
39586 if (!parens2.require_close (parser))
39587 break;
39588 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
39590 has_modifiers = true;
39591 break;
39593 if (mod != 0 || cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
39594 break;
39595 cp_lexer_consume_token (parser->lexer);
39597 else
39598 break;
39599 if (!has_modifiers)
39601 cp_parser_abort_tentative_parse (parser);
39602 align = NULL_TREE;
39603 allocator = NULL_TREE;
39604 cp_parser_parse_tentatively (parser);
39605 allocator = cp_parser_assignment_expression (parser);
39607 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
39608 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
39610 cp_parser_parse_definitely (parser);
39611 cp_lexer_consume_token (parser->lexer);
39612 if (allocator == error_mark_node)
39613 allocator = NULL_TREE;
39614 if (align == error_mark_node)
39615 align = NULL_TREE;
39617 else
39619 cp_parser_abort_tentative_parse (parser);
39620 allocator = NULL_TREE;
39621 align = NULL_TREE;
39624 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALLOCATE, list,
39625 &colon);
39627 if (allocator || align)
39628 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
39630 OMP_CLAUSE_ALLOCATE_ALLOCATOR (c) = allocator;
39631 OMP_CLAUSE_ALLOCATE_ALIGN (c) = align;
39634 return nlist;
39637 /* OpenMP 2.5:
39638 lastprivate ( variable-list )
39640 OpenMP 5.0:
39641 lastprivate ( [ lastprivate-modifier : ] variable-list ) */
39643 static tree
39644 cp_parser_omp_clause_lastprivate (cp_parser *parser, tree list)
39646 bool conditional = false;
39648 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
39649 return list;
39651 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
39652 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
39654 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39655 const char *p = IDENTIFIER_POINTER (id);
39657 if (strcmp ("conditional", p) == 0)
39659 conditional = true;
39660 cp_lexer_consume_token (parser->lexer);
39661 cp_lexer_consume_token (parser->lexer);
39665 tree nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LASTPRIVATE,
39666 list, NULL);
39668 if (conditional)
39669 for (tree c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
39670 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 1;
39671 return nlist;
39674 /* OpenMP 4.0:
39675 linear ( variable-list )
39676 linear ( variable-list : expression )
39678 OpenMP 4.5:
39679 linear ( modifier ( variable-list ) )
39680 linear ( modifier ( variable-list ) : expression )
39682 modifier:
39685 uval
39687 OpenMP 5.2:
39688 linear ( variable-list : modifiers-list )
39690 modifiers:
39693 uval
39694 step ( expression ) */
39696 static tree
39697 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
39698 bool declare_simd)
39700 tree nlist, c, step = integer_one_node;
39701 bool colon;
39702 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
39703 bool old_linear_modifier = false;
39705 matching_parens parens;
39706 if (!parens.require_open (parser))
39707 return list;
39709 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39711 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39712 const char *p = IDENTIFIER_POINTER (id);
39714 if (strcmp ("ref", p) == 0)
39715 kind = OMP_CLAUSE_LINEAR_REF;
39716 else if (strcmp ("val", p) == 0)
39717 kind = OMP_CLAUSE_LINEAR_VAL;
39718 else if (strcmp ("uval", p) == 0)
39719 kind = OMP_CLAUSE_LINEAR_UVAL;
39720 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
39722 cp_lexer_consume_token (parser->lexer);
39723 old_linear_modifier = true;
39725 else
39726 kind = OMP_CLAUSE_LINEAR_DEFAULT;
39729 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
39730 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
39731 &colon);
39732 else
39734 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
39735 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
39736 if (colon)
39737 cp_parser_require (parser, CPP_COLON, RT_COLON);
39738 else if (!parens.require_close (parser))
39739 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39740 /*or_comma=*/false,
39741 /*consume_paren=*/true);
39744 if (colon)
39746 bool has_modifiers = false;
39747 if (kind == OMP_CLAUSE_LINEAR_DEFAULT
39748 && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39750 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39751 const char *p = IDENTIFIER_POINTER (id);
39752 size_t pos = 0;
39753 if (strcmp ("ref", p) == 0
39754 || strcmp ("val", p) == 0
39755 || strcmp ("uval", p) == 0)
39756 pos = 2;
39757 else if (strcmp ("step", p) == 0
39758 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
39760 pos = cp_parser_skip_balanced_tokens (parser, 2);
39761 if (pos == 2)
39762 pos = 0;
39764 if (pos != 0
39765 && (cp_lexer_nth_token_is (parser->lexer, pos, CPP_COMMA)
39766 || cp_lexer_nth_token_is (parser->lexer, pos,
39767 CPP_CLOSE_PAREN)))
39768 has_modifiers = true;
39771 step = NULL_TREE;
39772 if (has_modifiers)
39774 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39776 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39777 const char *p = IDENTIFIER_POINTER (id);
39778 enum omp_clause_linear_kind nkind = OMP_CLAUSE_LINEAR_DEFAULT;
39779 if (strcmp ("ref", p) == 0)
39780 nkind = OMP_CLAUSE_LINEAR_REF;
39781 else if (strcmp ("val", p) == 0)
39782 nkind = OMP_CLAUSE_LINEAR_VAL;
39783 else if (strcmp ("uval", p) == 0)
39784 nkind = OMP_CLAUSE_LINEAR_UVAL;
39785 if (nkind != OMP_CLAUSE_LINEAR_DEFAULT)
39787 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
39788 error_at (cp_lexer_peek_token (parser->lexer)->location,
39789 "multiple linear modifiers");
39790 kind = nkind;
39791 cp_lexer_consume_token (parser->lexer);
39793 else if (strcmp ("step", p) == 0)
39795 location_t step_loc
39796 = cp_lexer_peek_token (parser->lexer)->location;
39797 cp_lexer_consume_token (parser->lexer);
39798 matching_parens parens2;
39799 if (parens2.require_open (parser))
39801 if (step)
39802 error_at (step_loc, "multiple %<step%> modifiers");
39803 if (declare_simd
39804 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
39805 && cp_lexer_nth_token_is (parser->lexer, 2,
39806 CPP_CLOSE_PAREN))
39808 cp_token *token
39809 = cp_lexer_peek_token (parser->lexer);
39810 location_t tok_loc = token->location;
39811 cp_parser_parse_tentatively (parser);
39812 step = cp_parser_id_expression (parser, false, true,
39813 NULL, false, false);
39814 if (step != error_mark_node)
39815 step = cp_parser_lookup_name_simple (parser, step,
39816 tok_loc);
39817 if (step == error_mark_node)
39819 step = NULL_TREE;
39820 cp_parser_abort_tentative_parse (parser);
39822 else if (!cp_parser_parse_definitely (parser))
39823 step = NULL_TREE;
39825 if (!step)
39826 step = cp_parser_assignment_expression (parser);
39827 if (!parens2.require_close (parser))
39828 cp_parser_skip_to_closing_parenthesis (parser, true,
39829 false, true);
39831 else
39832 break;
39834 else
39835 break;
39836 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39838 cp_lexer_consume_token (parser->lexer);
39839 continue;
39841 break;
39843 if (!step)
39844 step = integer_one_node;
39846 else if (declare_simd
39847 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
39848 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
39850 cp_token *token = cp_lexer_peek_token (parser->lexer);
39851 cp_parser_parse_tentatively (parser);
39852 step = cp_parser_id_expression (parser, /*template_p=*/false,
39853 /*check_dependency_p=*/true,
39854 /*template_p=*/NULL,
39855 /*declarator_p=*/false,
39856 /*optional_p=*/false);
39857 if (step != error_mark_node)
39858 step = cp_parser_lookup_name_simple (parser, step, token->location);
39859 if (step == error_mark_node)
39861 step = NULL_TREE;
39862 cp_parser_abort_tentative_parse (parser);
39864 else if (!cp_parser_parse_definitely (parser))
39865 step = NULL_TREE;
39867 if (!step)
39868 step = cp_parser_assignment_expression (parser);
39870 if (!parens.require_close (parser))
39871 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39872 /*or_comma=*/false,
39873 /*consume_paren=*/true);
39875 if (step == error_mark_node)
39876 return list;
39879 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
39881 OMP_CLAUSE_LINEAR_STEP (c) = step;
39882 OMP_CLAUSE_LINEAR_KIND (c) = kind;
39883 OMP_CLAUSE_LINEAR_OLD_LINEAR_MODIFIER (c) = old_linear_modifier;
39886 return nlist;
39889 /* OpenMP 4.0:
39890 safelen ( constant-expression ) */
39892 static tree
39893 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
39894 location_t location)
39896 tree t, c;
39898 matching_parens parens;
39899 if (!parens.require_open (parser))
39900 return list;
39902 t = cp_parser_constant_expression (parser);
39904 if (t == error_mark_node
39905 || !parens.require_close (parser))
39906 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39907 /*or_comma=*/false,
39908 /*consume_paren=*/true);
39910 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
39912 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
39913 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
39914 OMP_CLAUSE_CHAIN (c) = list;
39916 return c;
39919 /* OpenMP 4.0:
39920 simdlen ( constant-expression ) */
39922 static tree
39923 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
39924 location_t location)
39926 tree t, c;
39928 matching_parens parens;
39929 if (!parens.require_open (parser))
39930 return list;
39932 t = cp_parser_constant_expression (parser);
39934 if (t == error_mark_node
39935 || !parens.require_close (parser))
39936 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39937 /*or_comma=*/false,
39938 /*consume_paren=*/true);
39940 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
39942 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
39943 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
39944 OMP_CLAUSE_CHAIN (c) = list;
39946 return c;
39949 /* OpenMP 4.5:
39950 vec:
39951 identifier [+/- integer]
39952 vec , identifier [+/- integer]
39955 static tree
39956 cp_parser_omp_clause_doacross_sink (cp_parser *parser, location_t clause_loc,
39957 tree list, bool depend_p)
39959 tree vec = NULL;
39961 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
39963 cp_parser_error (parser, "expected identifier");
39964 return list;
39967 if (!depend_p)
39969 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39970 if (strcmp (IDENTIFIER_POINTER (id), "omp_cur_iteration") == 0
39971 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_MINUS)
39972 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_NUMBER)
39973 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_PAREN))
39975 tree val = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
39976 if (integer_onep (val))
39978 cp_lexer_consume_token (parser->lexer);
39979 cp_lexer_consume_token (parser->lexer);
39980 cp_lexer_consume_token (parser->lexer);
39981 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DOACROSS);
39982 OMP_CLAUSE_DOACROSS_KIND (u) = OMP_CLAUSE_DOACROSS_SINK;
39983 OMP_CLAUSE_CHAIN (u) = list;
39984 return u;
39989 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39991 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
39992 tree t, identifier = cp_parser_identifier (parser);
39993 tree addend = NULL;
39995 if (identifier == error_mark_node)
39996 t = error_mark_node;
39997 else
39999 t = cp_parser_lookup_name_simple
40000 (parser, identifier,
40001 cp_lexer_peek_token (parser->lexer)->location);
40002 if (t == error_mark_node)
40003 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
40004 id_loc);
40007 bool neg = false;
40008 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
40009 neg = true;
40010 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
40012 addend = integer_zero_node;
40013 goto add_to_vector;
40015 cp_lexer_consume_token (parser->lexer);
40017 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
40019 cp_parser_error (parser, "expected integer");
40020 return list;
40023 addend = cp_lexer_peek_token (parser->lexer)->u.value;
40024 if (TREE_CODE (addend) != INTEGER_CST)
40026 cp_parser_error (parser, "expected integer");
40027 return list;
40029 cp_lexer_consume_token (parser->lexer);
40031 add_to_vector:
40032 if (t != error_mark_node)
40034 vec = tree_cons (addend, t, vec);
40035 if (neg)
40036 OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (vec) = 1;
40039 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
40040 || !cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
40041 break;
40043 cp_lexer_consume_token (parser->lexer);
40046 if (vec)
40048 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DOACROSS);
40049 OMP_CLAUSE_DOACROSS_KIND (u) = OMP_CLAUSE_DOACROSS_SINK;
40050 OMP_CLAUSE_DOACROSS_DEPEND (u) = depend_p;
40051 OMP_CLAUSE_DECL (u) = nreverse (vec);
40052 OMP_CLAUSE_CHAIN (u) = list;
40053 return u;
40055 return list;
40058 /* OpenMP 5.0:
40059 detach ( event-handle ) */
40061 static tree
40062 cp_parser_omp_clause_detach (cp_parser *parser, tree list)
40064 matching_parens parens;
40066 if (!parens.require_open (parser))
40067 return list;
40069 cp_token *token;
40070 tree name, decl;
40072 token = cp_lexer_peek_token (parser->lexer);
40073 name = cp_parser_id_expression (parser, /*template_p=*/false,
40074 /*check_dependency_p=*/true,
40075 /*template_p=*/NULL,
40076 /*declarator_p=*/false,
40077 /*optional_p=*/false);
40078 if (name == error_mark_node)
40079 decl = error_mark_node;
40080 else
40082 if (identifier_p (name))
40083 decl = cp_parser_lookup_name_simple (parser, name, token->location);
40084 else
40085 decl = name;
40086 if (decl == error_mark_node)
40087 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
40088 token->location);
40091 if (decl == error_mark_node
40092 || !parens.require_close (parser))
40093 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40094 /*or_comma=*/false,
40095 /*consume_paren=*/true);
40097 tree u = build_omp_clause (token->location, OMP_CLAUSE_DETACH);
40098 OMP_CLAUSE_DECL (u) = decl;
40099 OMP_CLAUSE_CHAIN (u) = list;
40101 return u;
40104 /* OpenMP 5.0:
40105 iterators ( iterators-definition )
40107 iterators-definition:
40108 iterator-specifier
40109 iterator-specifier , iterators-definition
40111 iterator-specifier:
40112 identifier = range-specification
40113 iterator-type identifier = range-specification
40115 range-specification:
40116 begin : end
40117 begin : end : step */
40119 static tree
40120 cp_parser_omp_iterators (cp_parser *parser)
40122 tree ret = NULL_TREE, *last = &ret;
40123 cp_lexer_consume_token (parser->lexer);
40125 matching_parens parens;
40126 if (!parens.require_open (parser))
40127 return error_mark_node;
40129 bool saved_colon_corrects_to_scope_p
40130 = parser->colon_corrects_to_scope_p;
40131 bool saved_colon_doesnt_start_class_def_p
40132 = parser->colon_doesnt_start_class_def_p;
40136 tree iter_type;
40137 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
40138 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ))
40139 iter_type = integer_type_node;
40140 else
40142 const char *saved_message
40143 = parser->type_definition_forbidden_message;
40144 parser->type_definition_forbidden_message
40145 = G_("types may not be defined in iterator type");
40147 iter_type = cp_parser_type_id (parser);
40149 parser->type_definition_forbidden_message = saved_message;
40152 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
40153 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
40155 cp_parser_error (parser, "expected identifier");
40156 break;
40159 tree id = cp_parser_identifier (parser);
40160 if (id == error_mark_node)
40161 break;
40163 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
40164 break;
40166 parser->colon_corrects_to_scope_p = false;
40167 parser->colon_doesnt_start_class_def_p = true;
40168 tree begin = cp_parser_assignment_expression (parser);
40170 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
40171 break;
40173 tree end = cp_parser_assignment_expression (parser);
40175 tree step = integer_one_node;
40176 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
40178 cp_lexer_consume_token (parser->lexer);
40179 step = cp_parser_assignment_expression (parser);
40182 tree iter_var = build_decl (loc, VAR_DECL, id, iter_type);
40183 DECL_ARTIFICIAL (iter_var) = 1;
40184 DECL_CONTEXT (iter_var) = current_function_decl;
40185 pushdecl (iter_var);
40187 *last = make_tree_vec (6);
40188 TREE_VEC_ELT (*last, 0) = iter_var;
40189 TREE_VEC_ELT (*last, 1) = begin;
40190 TREE_VEC_ELT (*last, 2) = end;
40191 TREE_VEC_ELT (*last, 3) = step;
40192 last = &TREE_CHAIN (*last);
40194 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
40196 cp_lexer_consume_token (parser->lexer);
40197 continue;
40199 break;
40201 while (1);
40203 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
40204 parser->colon_doesnt_start_class_def_p
40205 = saved_colon_doesnt_start_class_def_p;
40207 if (!parens.require_close (parser))
40208 cp_parser_skip_to_closing_parenthesis (parser,
40209 /*recovering=*/true,
40210 /*or_comma=*/false,
40211 /*consume_paren=*/true);
40213 return ret ? ret : error_mark_node;
40216 /* OpenMP 5.0:
40217 affinity ( [aff-modifier :] variable-list )
40218 aff-modifier:
40219 iterator ( iterators-definition ) */
40221 static tree
40222 cp_parser_omp_clause_affinity (cp_parser *parser, tree list)
40224 tree nlist, c, iterators = NULL_TREE;
40226 matching_parens parens;
40227 if (!parens.require_open (parser))
40228 return list;
40230 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40232 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40233 const char *p = IDENTIFIER_POINTER (id);
40234 bool parse_iter = ((strcmp ("iterator", p) == 0)
40235 && (cp_lexer_nth_token_is (parser->lexer, 2,
40236 CPP_OPEN_PAREN)));
40237 if (parse_iter)
40239 size_t n = cp_parser_skip_balanced_tokens (parser, 2);
40240 parse_iter = cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON);
40242 if (parse_iter)
40244 begin_scope (sk_omp, NULL);
40245 iterators = cp_parser_omp_iterators (parser);
40246 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
40248 if (iterators)
40249 poplevel (0, 1, 0);
40250 cp_parser_skip_to_closing_parenthesis (parser,
40251 /*recovering=*/true,
40252 /*or_comma=*/false,
40253 /*consume_paren=*/true);
40254 return list;
40258 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_AFFINITY,
40259 list, NULL);
40260 if (iterators)
40262 tree block = poplevel (1, 1, 0);
40263 if (iterators != error_mark_node)
40265 TREE_VEC_ELT (iterators, 5) = block;
40266 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
40267 OMP_CLAUSE_DECL (c) = build_tree_list (iterators,
40268 OMP_CLAUSE_DECL (c));
40271 return nlist;
40274 /* OpenMP 4.0:
40275 depend ( depend-kind : variable-list )
40277 depend-kind:
40278 in | out | inout
40280 OpenMP 4.5:
40281 depend ( source )
40283 depend ( sink : vec )
40285 OpenMP 5.0:
40286 depend ( depend-modifier , depend-kind: variable-list )
40288 depend-kind:
40289 in | out | inout | mutexinoutset | depobj
40291 depend-modifier:
40292 iterator ( iterators-definition ) */
40294 static tree
40295 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
40297 tree nlist, c, iterators = NULL_TREE;
40298 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_LAST;
40299 enum omp_clause_doacross_kind dkind = OMP_CLAUSE_DOACROSS_LAST;
40301 matching_parens parens;
40302 if (!parens.require_open (parser))
40303 return list;
40307 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
40308 goto invalid_kind;
40310 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40311 const char *p = IDENTIFIER_POINTER (id);
40313 if (strcmp ("iterator", p) == 0 && iterators == NULL_TREE)
40315 begin_scope (sk_omp, NULL);
40316 iterators = cp_parser_omp_iterators (parser);
40317 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
40318 continue;
40320 if (strcmp ("in", p) == 0)
40321 kind = OMP_CLAUSE_DEPEND_IN;
40322 else if (strcmp ("inout", p) == 0)
40323 kind = OMP_CLAUSE_DEPEND_INOUT;
40324 else if (strcmp ("inoutset", p) == 0)
40325 kind = OMP_CLAUSE_DEPEND_INOUTSET;
40326 else if (strcmp ("mutexinoutset", p) == 0)
40327 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
40328 else if (strcmp ("out", p) == 0)
40329 kind = OMP_CLAUSE_DEPEND_OUT;
40330 else if (strcmp ("depobj", p) == 0)
40331 kind = OMP_CLAUSE_DEPEND_DEPOBJ;
40332 else if (strcmp ("sink", p) == 0)
40333 dkind = OMP_CLAUSE_DOACROSS_SINK;
40334 else if (strcmp ("source", p) == 0)
40335 dkind = OMP_CLAUSE_DOACROSS_SOURCE;
40336 else
40337 goto invalid_kind;
40338 break;
40340 while (1);
40342 cp_lexer_consume_token (parser->lexer);
40344 if (iterators
40345 && (dkind == OMP_CLAUSE_DOACROSS_SOURCE
40346 || dkind == OMP_CLAUSE_DOACROSS_SINK))
40348 poplevel (0, 1, 0);
40349 error_at (loc, "%<iterator%> modifier incompatible with %qs",
40350 dkind == OMP_CLAUSE_DOACROSS_SOURCE ? "source" : "sink");
40351 iterators = NULL_TREE;
40354 if (dkind == OMP_CLAUSE_DOACROSS_SOURCE)
40356 c = build_omp_clause (loc, OMP_CLAUSE_DOACROSS);
40357 OMP_CLAUSE_DOACROSS_KIND (c) = dkind;
40358 OMP_CLAUSE_DOACROSS_DEPEND (c) = 1;
40359 OMP_CLAUSE_DECL (c) = NULL_TREE;
40360 OMP_CLAUSE_CHAIN (c) = list;
40361 if (!parens.require_close (parser))
40362 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40363 /*or_comma=*/false,
40364 /*consume_paren=*/true);
40365 return c;
40368 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
40369 goto resync_fail;
40371 if (dkind == OMP_CLAUSE_DOACROSS_SINK)
40373 nlist = cp_parser_omp_clause_doacross_sink (parser, loc, list, true);
40374 if (!parens.require_close (parser))
40375 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40376 /*or_comma=*/false,
40377 /*consume_paren=*/true);
40379 else
40381 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
40382 list, NULL);
40384 if (iterators)
40386 tree block = poplevel (1, 1, 0);
40387 if (iterators == error_mark_node)
40388 iterators = NULL_TREE;
40389 else
40390 TREE_VEC_ELT (iterators, 5) = block;
40393 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
40395 OMP_CLAUSE_DEPEND_KIND (c) = kind;
40396 if (iterators)
40397 OMP_CLAUSE_DECL (c)
40398 = build_tree_list (iterators, OMP_CLAUSE_DECL (c));
40401 return nlist;
40403 invalid_kind:
40404 cp_parser_error (parser, "invalid depend kind");
40405 resync_fail:
40406 if (iterators)
40407 poplevel (0, 1, 0);
40408 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40409 /*or_comma=*/false,
40410 /*consume_paren=*/true);
40411 return list;
40414 /* OpenMP 5.2:
40415 doacross ( source : )
40416 doacross ( source : omp_cur_iteration )
40418 doacross ( sink : vec )
40419 doacross ( sink : omp_cur_iteration - logical_iteration ) */
40421 static tree
40422 cp_parser_omp_clause_doacross (cp_parser *parser, tree list, location_t loc)
40424 tree nlist;
40425 enum omp_clause_doacross_kind kind = OMP_CLAUSE_DOACROSS_LAST;
40427 matching_parens parens;
40428 if (!parens.require_open (parser))
40429 return list;
40431 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
40433 invalid_kind:
40434 cp_parser_error (parser, "invalid doacross kind");
40435 resync_fail:
40436 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40437 /*or_comma=*/false,
40438 /*consume_paren=*/true);
40439 return list;
40442 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40443 const char *p = IDENTIFIER_POINTER (id);
40445 if (strcmp ("sink", p) == 0)
40446 kind = OMP_CLAUSE_DOACROSS_SINK;
40447 else if (strcmp ("source", p) == 0)
40448 kind = OMP_CLAUSE_DOACROSS_SOURCE;
40449 else
40450 goto invalid_kind;
40452 cp_lexer_consume_token (parser->lexer);
40454 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
40455 goto resync_fail;
40457 if (kind == OMP_CLAUSE_DOACROSS_SOURCE)
40459 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40461 id = cp_lexer_peek_token (parser->lexer)->u.value;
40462 p = IDENTIFIER_POINTER (id);
40463 if (strcmp (p, "omp_cur_iteration") == 0)
40464 cp_lexer_consume_token (parser->lexer);
40466 nlist = build_omp_clause (loc, OMP_CLAUSE_DOACROSS);
40467 OMP_CLAUSE_DOACROSS_KIND (nlist) = OMP_CLAUSE_DOACROSS_SOURCE;
40468 OMP_CLAUSE_DECL (nlist) = NULL_TREE;
40469 OMP_CLAUSE_CHAIN (nlist) = list;
40471 else
40472 nlist = cp_parser_omp_clause_doacross_sink (parser, loc, list, false);
40474 if (!parens.require_close (parser))
40475 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40476 /*or_comma=*/false,
40477 /*consume_paren=*/true);
40478 return nlist;
40481 /* OpenMP 4.0:
40482 map ( map-kind : variable-list )
40483 map ( variable-list )
40485 map-kind:
40486 alloc | to | from | tofrom
40488 OpenMP 4.5:
40489 map-kind:
40490 alloc | to | from | tofrom | release | delete
40492 map ( always [,] map-kind: variable-list )
40494 OpenMP 5.0:
40495 map ( [map-type-modifier[,] ...] map-kind: variable-list )
40497 map-type-modifier:
40498 always | close */
40500 static tree
40501 cp_parser_omp_clause_map (cp_parser *parser, tree list)
40503 tree nlist, c;
40504 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
40506 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
40507 return list;
40509 int pos = 1;
40510 int map_kind_pos = 0;
40511 while (cp_lexer_peek_nth_token (parser->lexer, pos)->type == CPP_NAME
40512 || cp_lexer_peek_nth_token (parser->lexer, pos)->keyword == RID_DELETE)
40514 if (cp_lexer_peek_nth_token (parser->lexer, pos + 1)->type == CPP_COLON)
40516 map_kind_pos = pos;
40517 break;
40520 if (cp_lexer_peek_nth_token (parser->lexer, pos + 1)->type == CPP_COMMA)
40521 pos++;
40522 pos++;
40525 bool always_modifier = false;
40526 bool close_modifier = false;
40527 for (int pos = 1; pos < map_kind_pos; ++pos)
40529 cp_token *tok = cp_lexer_peek_token (parser->lexer);
40530 if (tok->type == CPP_COMMA)
40532 cp_lexer_consume_token (parser->lexer);
40533 continue;
40536 const char *p = IDENTIFIER_POINTER (tok->u.value);
40537 if (strcmp ("always", p) == 0)
40539 if (always_modifier)
40541 cp_parser_error (parser, "too many %<always%> modifiers");
40542 cp_parser_skip_to_closing_parenthesis (parser,
40543 /*recovering=*/true,
40544 /*or_comma=*/false,
40545 /*consume_paren=*/true);
40546 return list;
40548 always_modifier = true;
40550 else if (strcmp ("close", p) == 0)
40552 if (close_modifier)
40554 cp_parser_error (parser, "too many %<close%> modifiers");
40555 cp_parser_skip_to_closing_parenthesis (parser,
40556 /*recovering=*/true,
40557 /*or_comma=*/false,
40558 /*consume_paren=*/true);
40559 return list;
40561 close_modifier = true;
40563 else
40565 cp_parser_error (parser, "%<#pragma omp target%> with "
40566 "modifier other than %<always%> or "
40567 "%<close%> on %<map%> clause");
40568 cp_parser_skip_to_closing_parenthesis (parser,
40569 /*recovering=*/true,
40570 /*or_comma=*/false,
40571 /*consume_paren=*/true);
40572 return list;
40575 cp_lexer_consume_token (parser->lexer);
40578 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
40579 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
40581 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40582 const char *p = IDENTIFIER_POINTER (id);
40584 if (strcmp ("alloc", p) == 0)
40585 kind = GOMP_MAP_ALLOC;
40586 else if (strcmp ("to", p) == 0)
40587 kind = always_modifier ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
40588 else if (strcmp ("from", p) == 0)
40589 kind = always_modifier ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
40590 else if (strcmp ("tofrom", p) == 0)
40591 kind = always_modifier ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
40592 else if (strcmp ("release", p) == 0)
40593 kind = GOMP_MAP_RELEASE;
40594 else
40596 cp_parser_error (parser, "invalid map kind");
40597 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40598 /*or_comma=*/false,
40599 /*consume_paren=*/true);
40600 return list;
40602 cp_lexer_consume_token (parser->lexer);
40603 cp_lexer_consume_token (parser->lexer);
40605 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
40606 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
40608 kind = GOMP_MAP_DELETE;
40609 cp_lexer_consume_token (parser->lexer);
40610 cp_lexer_consume_token (parser->lexer);
40613 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
40614 NULL, true);
40616 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
40617 OMP_CLAUSE_SET_MAP_KIND (c, kind);
40619 return nlist;
40622 /* OpenMP 4.0:
40623 device ( expression )
40625 OpenMP 5.0:
40626 device ( [device-modifier :] integer-expression )
40628 device-modifier:
40629 ancestor | device_num */
40631 static tree
40632 cp_parser_omp_clause_device (cp_parser *parser, tree list,
40633 location_t location)
40635 tree t, c;
40636 bool ancestor = false;
40638 matching_parens parens;
40639 if (!parens.require_open (parser))
40640 return list;
40642 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
40643 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
40645 cp_token *tok = cp_lexer_peek_token (parser->lexer);
40646 const char *p = IDENTIFIER_POINTER (tok->u.value);
40647 if (strcmp ("ancestor", p) == 0)
40649 ancestor = true;
40651 /* A requires directive with the reverse_offload clause must be
40652 specified. */
40653 if ((omp_requires_mask & OMP_REQUIRES_REVERSE_OFFLOAD) == 0)
40655 error_at (tok->location, "%<ancestor%> device modifier not "
40656 "preceded by %<requires%> directive "
40657 "with %<reverse_offload%> clause");
40658 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
40659 return list;
40662 else if (strcmp ("device_num", p) == 0)
40664 else
40666 error_at (tok->location, "expected %<ancestor%> or %<device_num%>");
40667 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
40668 return list;
40670 cp_lexer_consume_token (parser->lexer);
40671 cp_lexer_consume_token (parser->lexer);
40674 t = cp_parser_assignment_expression (parser);
40676 if (t == error_mark_node
40677 || !parens.require_close (parser))
40678 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40679 /*or_comma=*/false,
40680 /*consume_paren=*/true);
40682 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
40683 "device", location);
40685 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
40686 OMP_CLAUSE_DEVICE_ID (c) = t;
40687 OMP_CLAUSE_CHAIN (c) = list;
40688 OMP_CLAUSE_DEVICE_ANCESTOR (c) = ancestor;
40690 return c;
40693 /* OpenMP 4.0:
40694 dist_schedule ( static )
40695 dist_schedule ( static , expression ) */
40697 static tree
40698 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
40699 location_t location)
40701 tree c, t;
40703 matching_parens parens;
40704 if (!parens.require_open (parser))
40705 return list;
40707 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
40709 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
40710 goto invalid_kind;
40711 cp_lexer_consume_token (parser->lexer);
40713 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
40715 cp_lexer_consume_token (parser->lexer);
40717 t = cp_parser_assignment_expression (parser);
40719 if (t == error_mark_node)
40720 goto resync_fail;
40721 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
40723 if (!parens.require_close (parser))
40724 goto resync_fail;
40726 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
40727 goto resync_fail;
40729 /* check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE,
40730 "dist_schedule", location); */
40731 if (omp_find_clause (list, OMP_CLAUSE_DIST_SCHEDULE))
40732 warning_at (location, 0, "too many %qs clauses", "dist_schedule");
40733 OMP_CLAUSE_CHAIN (c) = list;
40734 return c;
40736 invalid_kind:
40737 cp_parser_error (parser, "invalid dist_schedule kind");
40738 resync_fail:
40739 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40740 /*or_comma=*/false,
40741 /*consume_paren=*/true);
40742 return list;
40745 /* OpenMP 4.0:
40746 proc_bind ( proc-bind-kind )
40748 proc-bind-kind:
40749 primary | master | close | spread
40750 where OpenMP 5.1 added 'primary' and deprecated the alias 'master'. */
40752 static tree
40753 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
40754 location_t location)
40756 tree c;
40757 enum omp_clause_proc_bind_kind kind;
40759 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
40760 return list;
40762 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40764 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40765 const char *p = IDENTIFIER_POINTER (id);
40767 if (strcmp ("primary", p) == 0)
40768 kind = OMP_CLAUSE_PROC_BIND_PRIMARY;
40769 else if (strcmp ("master", p) == 0)
40770 kind = OMP_CLAUSE_PROC_BIND_MASTER;
40771 else if (strcmp ("close", p) == 0)
40772 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
40773 else if (strcmp ("spread", p) == 0)
40774 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
40775 else
40776 goto invalid_kind;
40778 else
40779 goto invalid_kind;
40781 cp_lexer_consume_token (parser->lexer);
40782 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
40783 goto resync_fail;
40785 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
40786 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
40787 location);
40788 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
40789 OMP_CLAUSE_CHAIN (c) = list;
40790 return c;
40792 invalid_kind:
40793 cp_parser_error (parser, "invalid depend kind");
40794 resync_fail:
40795 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40796 /*or_comma=*/false,
40797 /*consume_paren=*/true);
40798 return list;
40801 /* OpenMP 5.0:
40802 device_type ( host | nohost | any ) */
40804 static tree
40805 cp_parser_omp_clause_device_type (cp_parser *parser, tree list,
40806 location_t location)
40808 tree c;
40809 enum omp_clause_device_type_kind kind;
40811 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
40812 return list;
40814 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40816 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40817 const char *p = IDENTIFIER_POINTER (id);
40819 if (strcmp ("host", p) == 0)
40820 kind = OMP_CLAUSE_DEVICE_TYPE_HOST;
40821 else if (strcmp ("nohost", p) == 0)
40822 kind = OMP_CLAUSE_DEVICE_TYPE_NOHOST;
40823 else if (strcmp ("any", p) == 0)
40824 kind = OMP_CLAUSE_DEVICE_TYPE_ANY;
40825 else
40826 goto invalid_kind;
40828 else
40829 goto invalid_kind;
40831 cp_lexer_consume_token (parser->lexer);
40832 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
40833 goto resync_fail;
40835 c = build_omp_clause (location, OMP_CLAUSE_DEVICE_TYPE);
40836 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE_TYPE, "device_type",
40837 location);
40838 OMP_CLAUSE_DEVICE_TYPE_KIND (c) = kind;
40839 OMP_CLAUSE_CHAIN (c) = list;
40840 return c;
40842 invalid_kind:
40843 cp_parser_error (parser, "invalid depend kind");
40844 resync_fail:
40845 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40846 /*or_comma=*/false,
40847 /*consume_paren=*/true);
40848 return list;
40851 /* OpenACC:
40852 async [( int-expr )] */
40854 static tree
40855 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
40857 tree c, t;
40858 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
40860 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
40862 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
40864 matching_parens parens;
40865 parens.consume_open (parser);
40867 t = cp_parser_assignment_expression (parser);
40868 if (t == error_mark_node
40869 || !parens.require_close (parser))
40870 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40871 /*or_comma=*/false,
40872 /*consume_paren=*/true);
40875 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
40877 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
40878 OMP_CLAUSE_ASYNC_EXPR (c) = t;
40879 OMP_CLAUSE_CHAIN (c) = list;
40880 list = c;
40882 return list;
40885 /* Parse all OpenACC clauses. The set clauses allowed by the directive
40886 is a bitmask in MASK. Return the list of clauses found. */
40888 static tree
40889 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
40890 const char *where, cp_token *pragma_tok,
40891 bool finish_p = true)
40893 tree clauses = NULL;
40894 bool first = true;
40896 /* Don't create location wrapper nodes within OpenACC clauses. */
40897 auto_suppress_location_wrappers sentinel;
40899 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
40901 location_t here;
40902 pragma_omp_clause c_kind;
40903 omp_clause_code code;
40904 const char *c_name;
40905 tree prev = clauses;
40907 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
40908 cp_lexer_consume_token (parser->lexer);
40910 here = cp_lexer_peek_token (parser->lexer)->location;
40911 c_kind = cp_parser_omp_clause_name (parser);
40913 switch (c_kind)
40915 case PRAGMA_OACC_CLAUSE_ASYNC:
40916 clauses = cp_parser_oacc_clause_async (parser, clauses);
40917 c_name = "async";
40918 break;
40919 case PRAGMA_OACC_CLAUSE_AUTO:
40920 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_AUTO,
40921 clauses);
40922 c_name = "auto";
40923 break;
40924 case PRAGMA_OACC_CLAUSE_ATTACH:
40925 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
40926 c_name = "attach";
40927 break;
40928 case PRAGMA_OACC_CLAUSE_COLLAPSE:
40929 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
40930 c_name = "collapse";
40931 break;
40932 case PRAGMA_OACC_CLAUSE_COPY:
40933 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
40934 c_name = "copy";
40935 break;
40936 case PRAGMA_OACC_CLAUSE_COPYIN:
40937 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
40938 c_name = "copyin";
40939 break;
40940 case PRAGMA_OACC_CLAUSE_COPYOUT:
40941 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
40942 c_name = "copyout";
40943 break;
40944 case PRAGMA_OACC_CLAUSE_CREATE:
40945 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
40946 c_name = "create";
40947 break;
40948 case PRAGMA_OACC_CLAUSE_DELETE:
40949 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
40950 c_name = "delete";
40951 break;
40952 case PRAGMA_OMP_CLAUSE_DEFAULT:
40953 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
40954 c_name = "default";
40955 break;
40956 case PRAGMA_OACC_CLAUSE_DETACH:
40957 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
40958 c_name = "detach";
40959 break;
40960 case PRAGMA_OACC_CLAUSE_DEVICE:
40961 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
40962 c_name = "device";
40963 break;
40964 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
40965 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
40966 c_name = "deviceptr";
40967 break;
40968 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
40969 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
40970 c_name = "device_resident";
40971 break;
40972 case PRAGMA_OACC_CLAUSE_FINALIZE:
40973 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_FINALIZE,
40974 clauses);
40975 c_name = "finalize";
40976 break;
40977 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
40978 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
40979 clauses);
40980 c_name = "firstprivate";
40981 break;
40982 case PRAGMA_OACC_CLAUSE_GANG:
40983 c_name = "gang";
40984 clauses = cp_parser_oacc_shape_clause (parser, here, OMP_CLAUSE_GANG,
40985 c_name, clauses);
40986 break;
40987 case PRAGMA_OACC_CLAUSE_HOST:
40988 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
40989 c_name = "host";
40990 break;
40991 case PRAGMA_OACC_CLAUSE_IF:
40992 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
40993 c_name = "if";
40994 break;
40995 case PRAGMA_OACC_CLAUSE_IF_PRESENT:
40996 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_IF_PRESENT,
40997 clauses);
40998 c_name = "if_present";
40999 break;
41000 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
41001 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_INDEPENDENT,
41002 clauses);
41003 c_name = "independent";
41004 break;
41005 case PRAGMA_OACC_CLAUSE_LINK:
41006 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41007 c_name = "link";
41008 break;
41009 case PRAGMA_OACC_CLAUSE_NO_CREATE:
41010 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41011 c_name = "no_create";
41012 break;
41013 case PRAGMA_OACC_CLAUSE_NOHOST:
41014 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_NOHOST,
41015 clauses);
41016 c_name = "nohost";
41017 break;
41018 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
41019 code = OMP_CLAUSE_NUM_GANGS;
41020 c_name = "num_gangs";
41021 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
41022 clauses);
41023 break;
41024 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
41025 c_name = "num_workers";
41026 code = OMP_CLAUSE_NUM_WORKERS;
41027 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
41028 clauses);
41029 break;
41030 case PRAGMA_OACC_CLAUSE_PRESENT:
41031 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
41032 c_name = "present";
41033 break;
41034 case PRAGMA_OACC_CLAUSE_PRIVATE:
41035 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
41036 clauses);
41037 c_name = "private";
41038 break;
41039 case PRAGMA_OACC_CLAUSE_REDUCTION:
41040 clauses
41041 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
41042 false, clauses);
41043 c_name = "reduction";
41044 break;
41045 case PRAGMA_OACC_CLAUSE_SEQ:
41046 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_SEQ,
41047 clauses);
41048 c_name = "seq";
41049 break;
41050 case PRAGMA_OACC_CLAUSE_TILE:
41051 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
41052 c_name = "tile";
41053 break;
41054 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
41055 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
41056 clauses);
41057 c_name = "use_device";
41058 break;
41059 case PRAGMA_OACC_CLAUSE_VECTOR:
41060 c_name = "vector";
41061 clauses = cp_parser_oacc_shape_clause (parser, here,
41062 OMP_CLAUSE_VECTOR,
41063 c_name, clauses);
41064 break;
41065 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
41066 c_name = "vector_length";
41067 code = OMP_CLAUSE_VECTOR_LENGTH;
41068 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
41069 clauses);
41070 break;
41071 case PRAGMA_OACC_CLAUSE_WAIT:
41072 clauses = cp_parser_oacc_clause_wait (parser, clauses);
41073 c_name = "wait";
41074 break;
41075 case PRAGMA_OACC_CLAUSE_WORKER:
41076 c_name = "worker";
41077 clauses = cp_parser_oacc_shape_clause (parser, here,
41078 OMP_CLAUSE_WORKER,
41079 c_name, clauses);
41080 break;
41081 default:
41082 cp_parser_error (parser, "expected %<#pragma acc%> clause");
41083 goto saw_error;
41086 first = false;
41088 if (((mask >> c_kind) & 1) == 0)
41090 /* Remove the invalid clause(s) from the list to avoid
41091 confusing the rest of the compiler. */
41092 clauses = prev;
41093 error_at (here, "%qs is not valid for %qs", c_name, where);
41097 saw_error:
41098 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41100 if (finish_p)
41101 return finish_omp_clauses (clauses, C_ORT_ACC);
41103 return clauses;
41106 /* Parse all OpenMP clauses. The set clauses allowed by the directive
41107 is a bitmask in MASK. Return the list of clauses found.
41108 FINISH_P set if finish_omp_clauses should be called.
41109 NESTED non-zero if clauses should be terminated by closing paren instead
41110 of end of pragma. If it is 2, additionally commas are required in between
41111 the clauses. */
41113 static tree
41114 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
41115 const char *where, cp_token *pragma_tok,
41116 bool finish_p = true, int nested = 0)
41118 tree clauses = NULL;
41119 bool first = true;
41120 cp_token *token = NULL;
41122 /* Don't create location wrapper nodes within OpenMP clauses. */
41123 auto_suppress_location_wrappers sentinel;
41125 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
41127 pragma_omp_clause c_kind;
41128 const char *c_name;
41129 tree prev = clauses;
41131 if (nested && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
41132 break;
41134 if (!first || nested != 2)
41136 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
41137 cp_lexer_consume_token (parser->lexer);
41138 else if (nested == 2)
41139 error_at (cp_lexer_peek_token (parser->lexer)->location,
41140 "clauses in %<simd%> trait should be separated "
41141 "by %<,%>");
41144 token = cp_lexer_peek_token (parser->lexer);
41145 c_kind = cp_parser_omp_clause_name (parser);
41147 switch (c_kind)
41149 case PRAGMA_OMP_CLAUSE_BIND:
41150 clauses = cp_parser_omp_clause_bind (parser, clauses,
41151 token->location);
41152 c_name = "bind";
41153 break;
41154 case PRAGMA_OMP_CLAUSE_COLLAPSE:
41155 clauses = cp_parser_omp_clause_collapse (parser, clauses,
41156 token->location);
41157 c_name = "collapse";
41158 break;
41159 case PRAGMA_OMP_CLAUSE_COPYIN:
41160 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
41161 c_name = "copyin";
41162 break;
41163 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
41164 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
41165 clauses);
41166 c_name = "copyprivate";
41167 break;
41168 case PRAGMA_OMP_CLAUSE_DEFAULT:
41169 clauses = cp_parser_omp_clause_default (parser, clauses,
41170 token->location, false);
41171 c_name = "default";
41172 break;
41173 case PRAGMA_OMP_CLAUSE_FILTER:
41174 clauses = cp_parser_omp_clause_filter (parser, clauses,
41175 token->location);
41176 c_name = "filter";
41177 break;
41178 case PRAGMA_OMP_CLAUSE_FINAL:
41179 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
41180 c_name = "final";
41181 break;
41182 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
41183 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
41184 clauses);
41185 c_name = "firstprivate";
41186 break;
41187 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
41188 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
41189 token->location);
41190 c_name = "grainsize";
41191 break;
41192 case PRAGMA_OMP_CLAUSE_HINT:
41193 clauses = cp_parser_omp_clause_hint (parser, clauses,
41194 token->location);
41195 c_name = "hint";
41196 break;
41197 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
41198 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
41199 token->location);
41200 c_name = "defaultmap";
41201 break;
41202 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
41203 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
41204 clauses);
41205 c_name = "use_device_ptr";
41206 break;
41207 case PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR:
41208 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_ADDR,
41209 clauses);
41210 c_name = "use_device_addr";
41211 break;
41212 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
41213 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
41214 clauses);
41215 c_name = "is_device_ptr";
41216 break;
41217 case PRAGMA_OMP_CLAUSE_HAS_DEVICE_ADDR:
41218 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_HAS_DEVICE_ADDR,
41219 clauses);
41220 c_name = "has_device_addr";
41221 break;
41222 case PRAGMA_OMP_CLAUSE_IF:
41223 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
41224 true);
41225 c_name = "if";
41226 break;
41227 case PRAGMA_OMP_CLAUSE_IN_REDUCTION:
41228 clauses
41229 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_IN_REDUCTION,
41230 true, clauses);
41231 c_name = "in_reduction";
41232 break;
41233 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
41234 clauses = cp_parser_omp_clause_lastprivate (parser, clauses);
41235 c_name = "lastprivate";
41236 break;
41237 case PRAGMA_OMP_CLAUSE_MERGEABLE:
41238 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
41239 token->location);
41240 c_name = "mergeable";
41241 break;
41242 case PRAGMA_OMP_CLAUSE_NOWAIT:
41243 clauses = cp_parser_omp_clause_nowait (parser, clauses,
41244 token->location);
41245 c_name = "nowait";
41246 break;
41247 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
41248 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
41249 token->location);
41250 c_name = "num_tasks";
41251 break;
41252 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
41253 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
41254 token->location);
41255 c_name = "num_threads";
41256 break;
41257 case PRAGMA_OMP_CLAUSE_ORDER:
41258 clauses = cp_parser_omp_clause_order (parser, clauses,
41259 token->location);
41260 c_name = "order";
41261 break;
41262 case PRAGMA_OMP_CLAUSE_ORDERED:
41263 clauses = cp_parser_omp_clause_ordered (parser, clauses,
41264 token->location);
41265 c_name = "ordered";
41266 break;
41267 case PRAGMA_OMP_CLAUSE_PRIORITY:
41268 clauses = cp_parser_omp_clause_priority (parser, clauses,
41269 token->location);
41270 c_name = "priority";
41271 break;
41272 case PRAGMA_OMP_CLAUSE_PRIVATE:
41273 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
41274 clauses);
41275 c_name = "private";
41276 break;
41277 case PRAGMA_OMP_CLAUSE_REDUCTION:
41278 clauses
41279 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
41280 true, clauses);
41281 c_name = "reduction";
41282 break;
41283 case PRAGMA_OMP_CLAUSE_SCHEDULE:
41284 clauses = cp_parser_omp_clause_schedule (parser, clauses,
41285 token->location);
41286 c_name = "schedule";
41287 break;
41288 case PRAGMA_OMP_CLAUSE_SHARED:
41289 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
41290 clauses);
41291 c_name = "shared";
41292 break;
41293 case PRAGMA_OMP_CLAUSE_TASK_REDUCTION:
41294 clauses
41295 = cp_parser_omp_clause_reduction (parser,
41296 OMP_CLAUSE_TASK_REDUCTION,
41297 true, clauses);
41298 c_name = "task_reduction";
41299 break;
41300 case PRAGMA_OMP_CLAUSE_UNTIED:
41301 clauses = cp_parser_omp_clause_untied (parser, clauses,
41302 token->location);
41303 c_name = "untied";
41304 break;
41305 case PRAGMA_OMP_CLAUSE_INBRANCH:
41306 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
41307 clauses, token->location);
41308 c_name = "inbranch";
41309 break;
41310 case PRAGMA_OMP_CLAUSE_NONTEMPORAL:
41311 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_NONTEMPORAL,
41312 clauses);
41313 c_name = "nontemporal";
41314 break;
41315 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
41316 clauses = cp_parser_omp_clause_branch (parser,
41317 OMP_CLAUSE_NOTINBRANCH,
41318 clauses, token->location);
41319 c_name = "notinbranch";
41320 break;
41321 case PRAGMA_OMP_CLAUSE_PARALLEL:
41322 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
41323 clauses, token->location);
41324 c_name = "parallel";
41325 if (!first)
41327 clause_not_first:
41328 error_at (token->location, "%qs must be the first clause of %qs",
41329 c_name, where);
41330 clauses = prev;
41332 break;
41333 case PRAGMA_OMP_CLAUSE_FOR:
41334 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
41335 clauses, token->location);
41336 c_name = "for";
41337 if (!first)
41338 goto clause_not_first;
41339 break;
41340 case PRAGMA_OMP_CLAUSE_SECTIONS:
41341 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
41342 clauses, token->location);
41343 c_name = "sections";
41344 if (!first)
41345 goto clause_not_first;
41346 break;
41347 case PRAGMA_OMP_CLAUSE_TASKGROUP:
41348 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
41349 clauses, token->location);
41350 c_name = "taskgroup";
41351 if (!first)
41352 goto clause_not_first;
41353 break;
41354 case PRAGMA_OMP_CLAUSE_LINK:
41355 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
41356 c_name = "link";
41357 break;
41358 case PRAGMA_OMP_CLAUSE_TO:
41359 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
41361 tree nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_ENTER,
41362 clauses);
41363 for (tree c = nl; c != clauses; c = OMP_CLAUSE_CHAIN (c))
41364 OMP_CLAUSE_ENTER_TO (c) = 1;
41365 clauses = nl;
41367 else
41368 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses,
41369 true);
41370 c_name = "to";
41371 break;
41372 case PRAGMA_OMP_CLAUSE_FROM:
41373 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses,
41374 true);
41375 c_name = "from";
41376 break;
41377 case PRAGMA_OMP_CLAUSE_UNIFORM:
41378 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
41379 clauses);
41380 c_name = "uniform";
41381 break;
41382 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
41383 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
41384 token->location);
41385 c_name = "num_teams";
41386 break;
41387 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
41388 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
41389 token->location);
41390 c_name = "thread_limit";
41391 break;
41392 case PRAGMA_OMP_CLAUSE_ALIGNED:
41393 clauses = cp_parser_omp_clause_aligned (parser, clauses);
41394 c_name = "aligned";
41395 break;
41396 case PRAGMA_OMP_CLAUSE_ALLOCATE:
41397 clauses = cp_parser_omp_clause_allocate (parser, clauses);
41398 c_name = "allocate";
41399 break;
41400 case PRAGMA_OMP_CLAUSE_LINEAR:
41402 bool declare_simd = false;
41403 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
41404 declare_simd = true;
41405 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
41407 c_name = "linear";
41408 break;
41409 case PRAGMA_OMP_CLAUSE_AFFINITY:
41410 clauses = cp_parser_omp_clause_affinity (parser, clauses);
41411 c_name = "affinity";
41412 break;
41413 case PRAGMA_OMP_CLAUSE_DEPEND:
41414 clauses = cp_parser_omp_clause_depend (parser, clauses,
41415 token->location);
41416 c_name = "depend";
41417 break;
41418 case PRAGMA_OMP_CLAUSE_DOACROSS:
41419 clauses = cp_parser_omp_clause_doacross (parser, clauses,
41420 token->location);
41421 c_name = "doacross";
41422 break;
41423 case PRAGMA_OMP_CLAUSE_DETACH:
41424 clauses = cp_parser_omp_clause_detach (parser, clauses);
41425 c_name = "detach";
41426 break;
41427 case PRAGMA_OMP_CLAUSE_MAP:
41428 clauses = cp_parser_omp_clause_map (parser, clauses);
41429 c_name = "map";
41430 break;
41431 case PRAGMA_OMP_CLAUSE_DEVICE:
41432 clauses = cp_parser_omp_clause_device (parser, clauses,
41433 token->location);
41434 c_name = "device";
41435 break;
41436 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
41437 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
41438 token->location);
41439 c_name = "dist_schedule";
41440 break;
41441 case PRAGMA_OMP_CLAUSE_PROC_BIND:
41442 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
41443 token->location);
41444 c_name = "proc_bind";
41445 break;
41446 case PRAGMA_OMP_CLAUSE_DEVICE_TYPE:
41447 clauses = cp_parser_omp_clause_device_type (parser, clauses,
41448 token->location);
41449 c_name = "device_type";
41450 break;
41451 case PRAGMA_OMP_CLAUSE_SAFELEN:
41452 clauses = cp_parser_omp_clause_safelen (parser, clauses,
41453 token->location);
41454 c_name = "safelen";
41455 break;
41456 case PRAGMA_OMP_CLAUSE_SIMDLEN:
41457 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
41458 token->location);
41459 c_name = "simdlen";
41460 break;
41461 case PRAGMA_OMP_CLAUSE_NOGROUP:
41462 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
41463 token->location);
41464 c_name = "nogroup";
41465 break;
41466 case PRAGMA_OMP_CLAUSE_THREADS:
41467 clauses
41468 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
41469 clauses, token->location);
41470 c_name = "threads";
41471 break;
41472 case PRAGMA_OMP_CLAUSE_SIMD:
41473 clauses
41474 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
41475 clauses, token->location);
41476 c_name = "simd";
41477 break;
41478 case PRAGMA_OMP_CLAUSE_ENTER:
41479 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_ENTER,
41480 clauses);
41481 c_name = "enter";
41482 break;
41483 default:
41484 cp_parser_error (parser, "expected %<#pragma omp%> clause");
41485 goto saw_error;
41488 first = false;
41490 if (((mask >> c_kind) & 1) == 0)
41492 /* Remove the invalid clause(s) from the list to avoid
41493 confusing the rest of the compiler. */
41494 clauses = prev;
41495 error_at (token->location, "%qs is not valid for %qs", c_name, where);
41498 saw_error:
41499 if (!nested)
41500 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41501 if (finish_p)
41503 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
41504 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
41505 else
41506 return finish_omp_clauses (clauses, C_ORT_OMP);
41508 return clauses;
41511 /* OpenMP 2.5:
41512 structured-block:
41513 statement
41515 In practice, we're also interested in adding the statement to an
41516 outer node. So it is convenient if we work around the fact that
41517 cp_parser_statement calls add_stmt. */
41519 static unsigned
41520 cp_parser_begin_omp_structured_block (cp_parser *parser)
41522 unsigned save = parser->in_statement;
41524 /* Only move the values to IN_OMP_BLOCK if they weren't false.
41525 This preserves the "not within loop or switch" style error messages
41526 for nonsense cases like
41527 void foo() {
41528 #pragma omp single
41529 break;
41532 if (parser->in_statement)
41533 parser->in_statement = IN_OMP_BLOCK;
41535 return save;
41538 static void
41539 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
41541 parser->in_statement = save;
41544 static tree
41545 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
41547 tree stmt = begin_omp_structured_block ();
41548 unsigned int save = cp_parser_begin_omp_structured_block (parser);
41550 parser->omp_attrs_forbidden_p = true;
41551 cp_parser_statement (parser, NULL_TREE, false, if_p);
41553 cp_parser_end_omp_structured_block (parser, save);
41554 return finish_omp_structured_block (stmt);
41557 /* OpenMP 5.x:
41558 # pragma omp allocate (list) clauses
41560 OpenMP 5.0 clause:
41561 allocator (omp_allocator_handle_t expression)
41563 OpenMP 5.1 additional clause:
41564 align (constant-expression)] */
41566 static void
41567 cp_parser_omp_allocate (cp_parser *parser, cp_token *pragma_tok)
41569 tree allocator = NULL_TREE;
41570 tree alignment = NULL_TREE;
41571 location_t loc = pragma_tok->location;
41572 tree nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_ALLOCATE, NULL_TREE);
41576 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
41577 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
41578 cp_lexer_consume_token (parser->lexer);
41580 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41581 break;
41582 matching_parens parens;
41583 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41584 const char *p = IDENTIFIER_POINTER (id);
41585 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
41586 cp_lexer_consume_token (parser->lexer);
41587 if (strcmp (p, "allocator") != 0 && strcmp (p, "align") != 0)
41589 error_at (cloc, "expected %<allocator%> or %<align%>");
41590 break;
41592 if (!parens.require_open (parser))
41593 break;
41594 tree expr = cp_parser_assignment_expression (parser);
41595 if (p[2] == 'i' && alignment)
41597 error_at (cloc, "too many %qs clauses", "align");
41598 break;
41600 else if (p[2] == 'i')
41602 if (expr != error_mark_node)
41603 alignment = expr;
41604 /* FIXME: Remove when adding check to semantics.cc; cf FIXME below. */
41605 if (alignment
41606 && !type_dependent_expression_p (alignment)
41607 && !INTEGRAL_TYPE_P (TREE_TYPE (alignment)))
41609 error_at (cloc, "%<align%> clause argument needs to be "
41610 "positive constant power of two integer "
41611 "expression");
41612 alignment = NULL_TREE;
41614 else if (alignment)
41616 alignment = mark_rvalue_use (alignment);
41617 if (!processing_template_decl)
41619 alignment = maybe_constant_value (alignment);
41620 if (TREE_CODE (alignment) != INTEGER_CST
41621 || !tree_fits_uhwi_p (alignment)
41622 || !integer_pow2p (alignment))
41624 error_at (cloc, "%<align%> clause argument needs to be "
41625 "positive constant power of two integer "
41626 "expression");
41627 alignment = NULL_TREE;
41632 else if (allocator)
41634 error_at (cloc, "too many %qs clauses", "allocator");
41635 break;
41637 else
41639 if (expr != error_mark_node)
41640 allocator = expr;
41642 parens.require_close (parser);
41643 } while (true);
41644 cp_parser_require_pragma_eol (parser, pragma_tok);
41646 if (allocator || alignment)
41647 for (tree c = nl; c != NULL_TREE; c = OMP_CLAUSE_CHAIN (c))
41649 OMP_CLAUSE_ALLOCATE_ALLOCATOR (c) = allocator;
41650 OMP_CLAUSE_ALLOCATE_ALIGN (c) = alignment;
41653 /* FIXME: When implementing properly, delete the align/allocate expr error
41654 check above and add one in semantics.cc (to properly handle templates).
41655 Base this on the allocator/align modifiers check for the 'allocate' clause
41656 in semantics.cc's finish_omp_clauses. */
41657 sorry_at (loc, "%<#pragma omp allocate%> not yet supported");
41660 /* OpenMP 2.5:
41661 # pragma omp atomic new-line
41662 expression-stmt
41664 expression-stmt:
41665 x binop= expr | x++ | ++x | x-- | --x
41666 binop:
41667 +, *, -, /, &, ^, |, <<, >>
41669 where x is an lvalue expression with scalar type.
41671 OpenMP 3.1:
41672 # pragma omp atomic new-line
41673 update-stmt
41675 # pragma omp atomic read new-line
41676 read-stmt
41678 # pragma omp atomic write new-line
41679 write-stmt
41681 # pragma omp atomic update new-line
41682 update-stmt
41684 # pragma omp atomic capture new-line
41685 capture-stmt
41687 # pragma omp atomic capture new-line
41688 capture-block
41690 read-stmt:
41691 v = x
41692 write-stmt:
41693 x = expr
41694 update-stmt:
41695 expression-stmt | x = x binop expr
41696 capture-stmt:
41697 v = expression-stmt
41698 capture-block:
41699 { v = x; update-stmt; } | { update-stmt; v = x; }
41701 OpenMP 4.0:
41702 update-stmt:
41703 expression-stmt | x = x binop expr | x = expr binop x
41704 capture-stmt:
41705 v = update-stmt
41706 capture-block:
41707 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
41709 OpenMP 5.1:
41710 # pragma omp atomic compare new-line
41711 conditional-update-atomic
41713 # pragma omp atomic compare capture new-line
41714 conditional-update-capture-atomic
41716 conditional-update-atomic:
41717 cond-expr-stmt | cond-update-stmt
41718 cond-expr-stmt:
41719 x = expr ordop x ? expr : x;
41720 x = x ordop expr ? expr : x;
41721 x = x == e ? d : x;
41722 cond-update-stmt:
41723 if (expr ordop x) { x = expr; }
41724 if (x ordop expr) { x = expr; }
41725 if (x == e) { x = d; }
41726 ordop:
41727 <, >
41728 conditional-update-capture-atomic:
41729 v = cond-expr-stmt
41730 { v = x; cond-expr-stmt }
41731 { cond-expr-stmt v = x; }
41732 { v = x; cond-update-stmt }
41733 { cond-update-stmt v = x; }
41734 if (x == e) { x = d; } else { v = x; }
41735 { r = x == e; if (r) { x = d; } }
41736 { r = x == e; if (r) { x = d; } else { v = x; } }
41738 where x, r and v are lvalue expressions with scalar type,
41739 expr, e and d are expressions with scalar type and e might be
41740 the same as v. */
41742 static void
41743 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok, bool openacc)
41745 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
41746 tree rhs1 = NULL_TREE, orig_lhs, r = NULL_TREE;
41747 location_t loc = pragma_tok->location;
41748 enum tree_code code = ERROR_MARK, opcode = NOP_EXPR;
41749 enum omp_memory_order memory_order = OMP_MEMORY_ORDER_UNSPECIFIED;
41750 bool structured_block = false;
41751 tree clauses = NULL_TREE;
41752 bool capture = false;
41753 bool compare = false;
41754 bool weak = false;
41755 enum omp_memory_order fail = OMP_MEMORY_ORDER_UNSPECIFIED;
41756 bool no_semicolon = false;
41757 bool extra_scope = false;
41759 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
41761 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
41762 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
41763 cp_lexer_consume_token (parser->lexer);
41765 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41767 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41768 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
41769 const char *p = IDENTIFIER_POINTER (id);
41770 enum tree_code new_code = ERROR_MARK;
41771 enum omp_memory_order new_memory_order
41772 = OMP_MEMORY_ORDER_UNSPECIFIED;
41773 bool new_capture = false;
41774 bool new_compare = false;
41775 bool new_weak = false;
41776 enum omp_memory_order new_fail = OMP_MEMORY_ORDER_UNSPECIFIED;
41778 if (!strcmp (p, "read"))
41779 new_code = OMP_ATOMIC_READ;
41780 else if (!strcmp (p, "write"))
41781 new_code = NOP_EXPR;
41782 else if (!strcmp (p, "update"))
41783 new_code = OMP_ATOMIC;
41784 else if (openacc && !strcmp (p, "capture"))
41785 new_code = OMP_ATOMIC_CAPTURE_NEW;
41786 else if (openacc)
41788 p = NULL;
41789 error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
41790 "or %<capture%> clause");
41792 else if (!strcmp (p, "capture"))
41793 new_capture = true;
41794 else if (!strcmp (p, "compare"))
41795 new_compare = true;
41796 else if (!strcmp (p, "weak"))
41797 new_weak = true;
41798 else if (!strcmp (p, "fail"))
41800 matching_parens parens;
41802 cp_lexer_consume_token (parser->lexer);
41803 if (!parens.require_open (parser))
41804 continue;
41806 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41808 id = cp_lexer_peek_token (parser->lexer)->u.value;
41809 const char *q = IDENTIFIER_POINTER (id);
41811 if (!strcmp (q, "seq_cst"))
41812 new_fail = OMP_MEMORY_ORDER_SEQ_CST;
41813 else if (!strcmp (q, "acquire"))
41814 new_fail = OMP_MEMORY_ORDER_ACQUIRE;
41815 else if (!strcmp (q, "relaxed"))
41816 new_fail = OMP_MEMORY_ORDER_RELAXED;
41819 if (new_fail != OMP_MEMORY_ORDER_UNSPECIFIED)
41821 cp_lexer_consume_token (parser->lexer);
41822 if (fail != OMP_MEMORY_ORDER_UNSPECIFIED)
41823 error_at (cloc, "too many %qs clauses", "fail");
41824 else
41825 fail = new_fail;
41827 else
41828 cp_parser_error (parser, "expected %<seq_cst%>, %<acquire%> "
41829 "or %<relaxed%>");
41830 if (new_fail == OMP_MEMORY_ORDER_UNSPECIFIED
41831 || !parens.require_close (parser))
41832 cp_parser_skip_to_closing_parenthesis (parser,
41833 /*recovering=*/true,
41834 /*or_comma=*/false,
41835 /*consume_paren=*/true);
41836 continue;
41838 else if (!strcmp (p, "seq_cst"))
41839 new_memory_order = OMP_MEMORY_ORDER_SEQ_CST;
41840 else if (!strcmp (p, "acq_rel"))
41841 new_memory_order = OMP_MEMORY_ORDER_ACQ_REL;
41842 else if (!strcmp (p, "release"))
41843 new_memory_order = OMP_MEMORY_ORDER_RELEASE;
41844 else if (!strcmp (p, "acquire"))
41845 new_memory_order = OMP_MEMORY_ORDER_ACQUIRE;
41846 else if (!strcmp (p, "relaxed"))
41847 new_memory_order = OMP_MEMORY_ORDER_RELAXED;
41848 else if (!strcmp (p, "hint"))
41850 cp_lexer_consume_token (parser->lexer);
41851 clauses = cp_parser_omp_clause_hint (parser, clauses, cloc);
41852 continue;
41854 else
41856 p = NULL;
41857 error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
41858 "%<capture%>, %<compare%>, %<weak%>, %<fail%>, "
41859 "%<seq_cst%>, %<acq_rel%>, %<release%>, "
41860 "%<relaxed%> or %<hint%> clause");
41862 if (p)
41864 if (new_code != ERROR_MARK)
41866 /* OpenACC permits 'update capture'. */
41867 if (openacc
41868 && code == OMP_ATOMIC
41869 && new_code == OMP_ATOMIC_CAPTURE_NEW)
41870 code = new_code;
41871 else if (code != ERROR_MARK)
41872 error_at (cloc, "too many atomic clauses");
41873 else
41874 code = new_code;
41876 else if (new_memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
41878 if (memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
41879 error_at (cloc, "too many memory order clauses");
41880 else
41881 memory_order = new_memory_order;
41883 else if (new_capture)
41885 if (capture)
41886 error_at (cloc, "too many %qs clauses", "capture");
41887 else
41888 capture = true;
41890 else if (new_compare)
41892 if (compare)
41893 error_at (cloc, "too many %qs clauses", "compare");
41894 else
41895 compare = true;
41897 else if (new_weak)
41899 if (weak)
41900 error_at (cloc, "too many %qs clauses", "weak");
41901 else
41902 weak = true;
41904 cp_lexer_consume_token (parser->lexer);
41905 continue;
41908 break;
41910 cp_parser_require_pragma_eol (parser, pragma_tok);
41912 if (code == ERROR_MARK)
41913 code = OMP_ATOMIC;
41914 if (capture)
41916 if (code != OMP_ATOMIC)
41917 error_at (loc, "%qs clause is incompatible with %<read%> or %<write%> "
41918 "clauses", "capture");
41919 else
41920 code = OMP_ATOMIC_CAPTURE_NEW;
41922 if (compare && code != OMP_ATOMIC && code != OMP_ATOMIC_CAPTURE_NEW)
41924 error_at (loc, "%qs clause is incompatible with %<read%> or %<write%> "
41925 "clauses", "compare");
41926 compare = false;
41928 if (fail != OMP_MEMORY_ORDER_UNSPECIFIED && !compare)
41930 error_at (loc, "%qs clause requires %qs clause", "fail", "compare");
41931 fail = OMP_MEMORY_ORDER_UNSPECIFIED;
41933 if (weak && !compare)
41935 error_at (loc, "%qs clause requires %qs clause", "weak", "compare");
41936 weak = false;
41938 if (openacc)
41939 memory_order = OMP_MEMORY_ORDER_RELAXED;
41940 else if (memory_order == OMP_MEMORY_ORDER_UNSPECIFIED)
41942 omp_requires_mask
41943 = (enum omp_requires) (omp_requires_mask
41944 | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED);
41945 switch ((enum omp_memory_order)
41946 (omp_requires_mask & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER))
41948 case OMP_MEMORY_ORDER_UNSPECIFIED:
41949 case OMP_MEMORY_ORDER_RELAXED:
41950 memory_order = OMP_MEMORY_ORDER_RELAXED;
41951 break;
41952 case OMP_MEMORY_ORDER_SEQ_CST:
41953 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
41954 break;
41955 case OMP_MEMORY_ORDER_ACQ_REL:
41956 switch (code)
41958 case OMP_ATOMIC_READ:
41959 memory_order = OMP_MEMORY_ORDER_ACQUIRE;
41960 break;
41961 case NOP_EXPR: /* atomic write */
41962 memory_order = OMP_MEMORY_ORDER_RELEASE;
41963 break;
41964 default:
41965 memory_order = OMP_MEMORY_ORDER_ACQ_REL;
41966 break;
41968 break;
41969 default:
41970 gcc_unreachable ();
41973 else
41974 switch (code)
41976 case OMP_ATOMIC_READ:
41977 if (memory_order == OMP_MEMORY_ORDER_RELEASE)
41979 error_at (loc, "%<#pragma omp atomic read%> incompatible with "
41980 "%<release%> clause");
41981 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
41983 else if (memory_order == OMP_MEMORY_ORDER_ACQ_REL)
41984 memory_order = OMP_MEMORY_ORDER_ACQUIRE;
41985 break;
41986 case NOP_EXPR: /* atomic write */
41987 if (memory_order == OMP_MEMORY_ORDER_ACQUIRE)
41989 error_at (loc, "%<#pragma omp atomic write%> incompatible with "
41990 "%<acquire%> clause");
41991 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
41993 else if (memory_order == OMP_MEMORY_ORDER_ACQ_REL)
41994 memory_order = OMP_MEMORY_ORDER_RELEASE;
41995 break;
41996 default:
41997 break;
41999 if (fail != OMP_MEMORY_ORDER_UNSPECIFIED)
42000 memory_order
42001 = (enum omp_memory_order) (memory_order
42002 | (fail << OMP_FAIL_MEMORY_ORDER_SHIFT));
42004 switch (code)
42006 case OMP_ATOMIC_READ:
42007 case NOP_EXPR: /* atomic write */
42008 v = cp_parser_unary_expression (parser);
42009 if (v == error_mark_node)
42010 goto saw_error;
42011 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
42012 goto saw_error;
42013 if (code == NOP_EXPR)
42014 lhs = cp_parser_expression (parser);
42015 else
42016 lhs = cp_parser_unary_expression (parser);
42017 if (lhs == error_mark_node)
42018 goto saw_error;
42019 if (code == NOP_EXPR)
42021 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
42022 opcode. */
42023 code = OMP_ATOMIC;
42024 rhs = lhs;
42025 lhs = v;
42026 v = NULL_TREE;
42028 goto done;
42029 case OMP_ATOMIC_CAPTURE_NEW:
42030 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
42032 cp_lexer_consume_token (parser->lexer);
42033 structured_block = true;
42035 else if (compare
42036 && cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
42037 break;
42038 else
42040 v = cp_parser_unary_expression (parser);
42041 if (v == error_mark_node)
42042 goto saw_error;
42043 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
42044 goto saw_error;
42045 if (compare
42046 && cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
42048 location_t eloc = cp_lexer_peek_token (parser->lexer)->location;
42049 error_at (eloc, "expected expression");
42050 goto saw_error;
42053 default:
42054 break;
42057 restart:
42058 if (compare && cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
42060 cp_lexer_consume_token (parser->lexer);
42062 matching_parens parens;
42063 if (!parens.require_open (parser))
42064 goto saw_error;
42065 location_t eloc = cp_lexer_peek_token (parser->lexer)->location;
42066 tree cmp_expr;
42067 if (r)
42068 cmp_expr = cp_parser_unary_expression (parser);
42069 else
42070 cmp_expr = cp_parser_binary_expression (parser, false, true,
42071 PREC_NOT_OPERATOR, NULL);
42072 if (!parens.require_close (parser))
42073 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
42074 if (cmp_expr == error_mark_node)
42075 goto saw_error;
42076 if (r)
42078 if (!cp_tree_equal (cmp_expr, r))
42079 goto bad_if;
42080 cmp_expr = rhs;
42081 rhs = NULL_TREE;
42082 gcc_assert (TREE_CODE (cmp_expr) == EQ_EXPR);
42084 if (TREE_CODE (cmp_expr) == EQ_EXPR)
42086 else if (!structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
42088 error_at (EXPR_LOC_OR_LOC (cmp_expr, eloc),
42089 "expected %<==%> comparison in %<if%> condition");
42090 goto saw_error;
42092 else if (TREE_CODE (cmp_expr) != GT_EXPR
42093 && TREE_CODE (cmp_expr) != LT_EXPR)
42095 error_at (EXPR_LOC_OR_LOC (cmp_expr, eloc),
42096 "expected %<==%>, %<<%> or %<>%> comparison in %<if%> "
42097 "condition");
42098 goto saw_error;
42100 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
42101 goto saw_error;
42103 extra_scope = true;
42104 eloc = cp_lexer_peek_token (parser->lexer)->location;
42105 lhs = cp_parser_unary_expression (parser);
42106 orig_lhs = lhs;
42107 if (lhs == error_mark_node)
42108 goto saw_error;
42109 if (!cp_lexer_next_token_is (parser->lexer, CPP_EQ))
42111 cp_parser_error (parser, "expected %<=%>");
42112 goto saw_error;
42114 cp_lexer_consume_token (parser->lexer);
42115 eloc = cp_lexer_peek_token (parser->lexer)->location;
42116 if (TREE_CODE (cmp_expr) == EQ_EXPR)
42117 rhs1 = cp_parser_expression (parser);
42118 else
42119 rhs1 = cp_parser_simple_cast_expression (parser);
42121 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
42122 goto saw_error;
42124 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
42125 goto saw_error;
42127 extra_scope = false;
42128 no_semicolon = true;
42130 if (cp_tree_equal (TREE_OPERAND (cmp_expr, 0), lhs))
42132 if (TREE_CODE (cmp_expr) == EQ_EXPR)
42134 opcode = COND_EXPR;
42135 rhs = TREE_OPERAND (cmp_expr, 1);
42137 else if (cp_tree_equal (TREE_OPERAND (cmp_expr, 1), rhs1))
42139 opcode = (TREE_CODE (cmp_expr) == GT_EXPR
42140 ? MIN_EXPR : MAX_EXPR);
42141 rhs = rhs1;
42142 rhs1 = TREE_OPERAND (cmp_expr, 0);
42144 else
42145 goto bad_if;
42147 else if (TREE_CODE (cmp_expr) == EQ_EXPR)
42148 goto bad_if;
42149 else if (cp_tree_equal (TREE_OPERAND (cmp_expr, 1), lhs)
42150 && cp_tree_equal (TREE_OPERAND (cmp_expr, 0), rhs1))
42152 opcode = (TREE_CODE (cmp_expr) == GT_EXPR
42153 ? MAX_EXPR : MIN_EXPR);
42154 rhs = rhs1;
42155 rhs1 = TREE_OPERAND (cmp_expr, 1);
42157 else
42159 bad_if:
42160 cp_parser_error (parser,
42161 "invalid form of %<#pragma omp atomic compare%>");
42162 goto saw_error;
42165 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
42167 if (code != OMP_ATOMIC_CAPTURE_NEW
42168 || (structured_block && r == NULL_TREE)
42169 || TREE_CODE (cmp_expr) != EQ_EXPR)
42171 eloc = cp_lexer_peek_token (parser->lexer)->location;
42172 error_at (eloc, "unexpected %<else%>");
42173 goto saw_error;
42176 cp_lexer_consume_token (parser->lexer);
42178 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
42179 goto saw_error;
42181 extra_scope = true;
42182 v = cp_parser_unary_expression (parser);
42183 if (v == error_mark_node)
42184 goto saw_error;
42185 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
42186 goto saw_error;
42188 tree expr = cp_parser_simple_cast_expression (parser);
42190 if (!cp_tree_equal (expr, lhs))
42191 goto bad_if;
42193 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
42194 goto saw_error;
42196 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
42197 goto saw_error;
42199 extra_scope = false;
42200 code = OMP_ATOMIC_CAPTURE_OLD;
42201 if (r == NULL_TREE)
42202 /* Signal to c_finish_omp_atomic that in
42203 if (x == e) { x = d; } else { v = x; }
42204 case the store to v should be conditional. */
42205 r = void_list_node;
42207 else if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
42209 cp_parser_error (parser, "expected %<else%>");
42210 goto saw_error;
42212 else if (code == OMP_ATOMIC_CAPTURE_NEW
42213 && r != NULL_TREE
42214 && v == NULL_TREE)
42215 code = OMP_ATOMIC;
42216 goto stmt_done;
42218 lhs = cp_parser_unary_expression (parser);
42219 orig_lhs = lhs;
42220 switch (TREE_CODE (lhs))
42222 case ERROR_MARK:
42223 goto saw_error;
42225 case POSTINCREMENT_EXPR:
42226 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
42227 code = OMP_ATOMIC_CAPTURE_OLD;
42228 /* FALLTHROUGH */
42229 case PREINCREMENT_EXPR:
42230 lhs = TREE_OPERAND (lhs, 0);
42231 opcode = PLUS_EXPR;
42232 rhs = integer_one_node;
42233 if (compare)
42234 goto invalid_compare;
42235 break;
42237 case POSTDECREMENT_EXPR:
42238 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
42239 code = OMP_ATOMIC_CAPTURE_OLD;
42240 /* FALLTHROUGH */
42241 case PREDECREMENT_EXPR:
42242 lhs = TREE_OPERAND (lhs, 0);
42243 opcode = MINUS_EXPR;
42244 rhs = integer_one_node;
42245 if (compare)
42246 goto invalid_compare;
42247 break;
42249 case COMPOUND_EXPR:
42250 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
42251 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
42252 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
42253 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
42254 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
42255 (TREE_OPERAND (lhs, 1), 0), 0)))
42256 == BOOLEAN_TYPE)
42257 /* Undo effects of boolean_increment for post {in,de}crement. */
42258 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
42259 /* FALLTHRU */
42260 case MODIFY_EXPR:
42261 if (TREE_CODE (lhs) == MODIFY_EXPR
42262 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
42264 /* Undo effects of boolean_increment. */
42265 if (integer_onep (TREE_OPERAND (lhs, 1)))
42267 /* This is pre or post increment. */
42268 rhs = TREE_OPERAND (lhs, 1);
42269 lhs = TREE_OPERAND (lhs, 0);
42270 opcode = NOP_EXPR;
42271 if (code == OMP_ATOMIC_CAPTURE_NEW
42272 && !structured_block
42273 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
42274 code = OMP_ATOMIC_CAPTURE_OLD;
42275 if (compare)
42276 goto invalid_compare;
42277 break;
42280 /* FALLTHRU */
42281 default:
42282 if (compare && !cp_lexer_next_token_is (parser->lexer, CPP_EQ))
42284 cp_parser_error (parser, "expected %<=%>");
42285 goto saw_error;
42287 switch (cp_lexer_peek_token (parser->lexer)->type)
42289 case CPP_MULT_EQ:
42290 opcode = MULT_EXPR;
42291 break;
42292 case CPP_DIV_EQ:
42293 opcode = TRUNC_DIV_EXPR;
42294 break;
42295 case CPP_PLUS_EQ:
42296 opcode = PLUS_EXPR;
42297 break;
42298 case CPP_MINUS_EQ:
42299 opcode = MINUS_EXPR;
42300 break;
42301 case CPP_LSHIFT_EQ:
42302 opcode = LSHIFT_EXPR;
42303 break;
42304 case CPP_RSHIFT_EQ:
42305 opcode = RSHIFT_EXPR;
42306 break;
42307 case CPP_AND_EQ:
42308 opcode = BIT_AND_EXPR;
42309 break;
42310 case CPP_OR_EQ:
42311 opcode = BIT_IOR_EXPR;
42312 break;
42313 case CPP_XOR_EQ:
42314 opcode = BIT_XOR_EXPR;
42315 break;
42316 case CPP_EQ:
42317 enum cp_parser_prec oprec;
42318 cp_token *token;
42319 cp_lexer_consume_token (parser->lexer);
42320 cp_parser_parse_tentatively (parser);
42321 rhs1 = cp_parser_simple_cast_expression (parser);
42322 if (rhs1 == error_mark_node)
42324 cp_parser_abort_tentative_parse (parser);
42325 cp_parser_simple_cast_expression (parser);
42326 goto saw_error;
42328 token = cp_lexer_peek_token (parser->lexer);
42329 if (token->type != CPP_SEMICOLON
42330 && (!compare || token->type != CPP_QUERY)
42331 && !cp_tree_equal (lhs, rhs1))
42333 cp_parser_abort_tentative_parse (parser);
42334 cp_parser_parse_tentatively (parser);
42335 rhs = cp_parser_binary_expression (parser, false, true,
42336 PREC_NOT_OPERATOR, NULL);
42337 if (rhs == error_mark_node)
42339 cp_parser_abort_tentative_parse (parser);
42340 cp_parser_binary_expression (parser, false, true,
42341 PREC_NOT_OPERATOR, NULL);
42342 goto saw_error;
42344 switch (TREE_CODE (rhs))
42346 case MULT_EXPR:
42347 case TRUNC_DIV_EXPR:
42348 case RDIV_EXPR:
42349 case PLUS_EXPR:
42350 case MINUS_EXPR:
42351 case LSHIFT_EXPR:
42352 case RSHIFT_EXPR:
42353 case BIT_AND_EXPR:
42354 case BIT_IOR_EXPR:
42355 case BIT_XOR_EXPR:
42356 if (compare)
42357 break;
42358 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
42360 if (cp_parser_parse_definitely (parser))
42362 opcode = TREE_CODE (rhs);
42363 rhs1 = TREE_OPERAND (rhs, 0);
42364 rhs = TREE_OPERAND (rhs, 1);
42365 goto stmt_done;
42367 else
42368 goto saw_error;
42370 break;
42371 case EQ_EXPR:
42372 if (!compare
42373 || code != OMP_ATOMIC_CAPTURE_NEW
42374 || !structured_block
42375 || v
42376 || r)
42377 break;
42378 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
42379 && cp_lexer_nth_token_is_keyword (parser->lexer,
42380 2, RID_IF))
42382 if (cp_parser_parse_definitely (parser))
42384 r = lhs;
42385 lhs = NULL_TREE;
42386 rhs1 = NULL_TREE;
42387 cp_lexer_consume_token (parser->lexer);
42388 goto restart;
42391 break;
42392 case GT_EXPR:
42393 case LT_EXPR:
42394 if (compare
42395 && cp_lexer_next_token_is (parser->lexer, CPP_QUERY)
42396 && cp_tree_equal (lhs, TREE_OPERAND (rhs, 1))
42397 && cp_parser_parse_definitely (parser))
42399 opcode = TREE_CODE (rhs);
42400 rhs1 = TREE_OPERAND (rhs, 0);
42401 rhs = TREE_OPERAND (rhs, 1);
42402 cond_expr:
42403 cp_lexer_consume_token (parser->lexer);
42404 bool saved_colon_corrects_to_scope_p
42405 = parser->colon_corrects_to_scope_p;
42406 parser->colon_corrects_to_scope_p = false;
42407 tree e1 = cp_parser_expression (parser);
42408 parser->colon_corrects_to_scope_p
42409 = saved_colon_corrects_to_scope_p;
42410 cp_parser_require (parser, CPP_COLON, RT_COLON);
42411 tree e2 = cp_parser_simple_cast_expression (parser);
42412 if (cp_tree_equal (lhs, e2))
42414 if (cp_tree_equal (lhs, rhs1))
42416 if (opcode == EQ_EXPR)
42418 opcode = COND_EXPR;
42419 rhs1 = e1;
42420 goto stmt_done;
42422 if (cp_tree_equal (rhs, e1))
42424 opcode
42425 = opcode == GT_EXPR ? MIN_EXPR : MAX_EXPR;
42426 rhs = e1;
42427 goto stmt_done;
42430 else
42432 gcc_assert (opcode != EQ_EXPR);
42433 if (cp_tree_equal (rhs1, e1))
42435 opcode
42436 = opcode == GT_EXPR ? MAX_EXPR : MIN_EXPR;
42437 rhs1 = rhs;
42438 rhs = e1;
42439 goto stmt_done;
42443 cp_parser_error (parser,
42444 "invalid form of "
42445 "%<#pragma omp atomic compare%>");
42446 goto saw_error;
42448 break;
42449 default:
42450 break;
42452 cp_parser_abort_tentative_parse (parser);
42453 if (structured_block
42454 && code == OMP_ATOMIC_CAPTURE_OLD
42455 && !compare)
42457 rhs = cp_parser_expression (parser);
42458 if (rhs == error_mark_node)
42459 goto saw_error;
42460 opcode = NOP_EXPR;
42461 rhs1 = NULL_TREE;
42462 goto stmt_done;
42464 cp_parser_error (parser,
42465 "invalid form of %<#pragma omp atomic%>");
42466 goto saw_error;
42468 if (!cp_parser_parse_definitely (parser))
42469 goto saw_error;
42470 switch (token->type)
42472 case CPP_SEMICOLON:
42473 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
42475 code = OMP_ATOMIC_CAPTURE_OLD;
42476 v = lhs;
42477 lhs = NULL_TREE;
42478 lhs1 = rhs1;
42479 rhs1 = NULL_TREE;
42480 cp_lexer_consume_token (parser->lexer);
42481 goto restart;
42483 else if (structured_block && !compare)
42485 opcode = NOP_EXPR;
42486 rhs = rhs1;
42487 rhs1 = NULL_TREE;
42488 goto stmt_done;
42490 cp_parser_error (parser,
42491 "invalid form of %<#pragma omp atomic%>");
42492 goto saw_error;
42493 case CPP_MULT:
42494 opcode = MULT_EXPR;
42495 break;
42496 case CPP_DIV:
42497 opcode = TRUNC_DIV_EXPR;
42498 break;
42499 case CPP_PLUS:
42500 opcode = PLUS_EXPR;
42501 break;
42502 case CPP_MINUS:
42503 opcode = MINUS_EXPR;
42504 break;
42505 case CPP_LSHIFT:
42506 opcode = LSHIFT_EXPR;
42507 break;
42508 case CPP_RSHIFT:
42509 opcode = RSHIFT_EXPR;
42510 break;
42511 case CPP_AND:
42512 opcode = BIT_AND_EXPR;
42513 break;
42514 case CPP_OR:
42515 opcode = BIT_IOR_EXPR;
42516 break;
42517 case CPP_XOR:
42518 opcode = BIT_XOR_EXPR;
42519 break;
42520 case CPP_EQ_EQ:
42521 opcode = EQ_EXPR;
42522 break;
42523 case CPP_GREATER:
42524 opcode = GT_EXPR;
42525 break;
42526 case CPP_LESS:
42527 opcode = LT_EXPR;
42528 break;
42529 default:
42530 cp_parser_error (parser,
42531 "invalid operator for %<#pragma omp atomic%>");
42532 goto saw_error;
42534 if (compare
42535 && TREE_CODE_CLASS (opcode) != tcc_comparison)
42537 cp_parser_error (parser,
42538 "invalid form of "
42539 "%<#pragma omp atomic compare%>");
42540 goto saw_error;
42542 oprec = TOKEN_PRECEDENCE (token);
42543 gcc_assert (oprec != PREC_NOT_OPERATOR);
42544 if (commutative_tree_code (opcode))
42545 oprec = (enum cp_parser_prec) (oprec - 1);
42546 cp_lexer_consume_token (parser->lexer);
42547 rhs = cp_parser_binary_expression (parser, false, false,
42548 oprec, NULL);
42549 if (rhs == error_mark_node)
42550 goto saw_error;
42551 if (compare)
42553 if (!cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
42555 cp_parser_error (parser,
42556 "invalid form of "
42557 "%<#pragma omp atomic compare%>");
42558 goto saw_error;
42560 goto cond_expr;
42562 goto stmt_done;
42563 default:
42564 cp_parser_error (parser,
42565 "invalid operator for %<#pragma omp atomic%>");
42566 goto saw_error;
42568 cp_lexer_consume_token (parser->lexer);
42570 rhs = cp_parser_expression (parser);
42571 if (rhs == error_mark_node)
42572 goto saw_error;
42573 break;
42575 stmt_done:
42576 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW && r == NULL_TREE)
42578 if (!no_semicolon
42579 && !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
42580 goto saw_error;
42581 no_semicolon = false;
42582 v = cp_parser_unary_expression (parser);
42583 if (v == error_mark_node)
42584 goto saw_error;
42585 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
42586 goto saw_error;
42587 lhs1 = cp_parser_unary_expression (parser);
42588 if (lhs1 == error_mark_node)
42589 goto saw_error;
42591 if (structured_block)
42593 if (!no_semicolon)
42594 cp_parser_consume_semicolon_at_end_of_statement (parser);
42595 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
42597 done:
42598 if (weak && opcode != COND_EXPR)
42600 error_at (loc, "%<weak%> clause requires atomic equality comparison");
42601 weak = false;
42603 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
42604 finish_omp_atomic (pragma_tok->location, code, opcode, lhs, rhs, v, lhs1,
42605 rhs1, r, clauses, memory_order, weak);
42606 if (!structured_block && !no_semicolon)
42607 cp_parser_consume_semicolon_at_end_of_statement (parser);
42608 return;
42610 invalid_compare:
42611 error ("invalid form of %<pragma omp atomic compare%>");
42612 /* FALLTHRU */
42613 saw_error:
42614 cp_parser_skip_to_end_of_block_or_statement (parser);
42615 if (extra_scope && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
42616 cp_lexer_consume_token (parser->lexer);
42617 if (structured_block)
42619 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
42620 cp_lexer_consume_token (parser->lexer);
42621 else if (code == OMP_ATOMIC_CAPTURE_NEW)
42623 cp_parser_skip_to_end_of_block_or_statement (parser);
42624 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
42625 cp_lexer_consume_token (parser->lexer);
42631 /* OpenMP 2.5:
42632 # pragma omp barrier new-line */
42634 static void
42635 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
42637 cp_parser_require_pragma_eol (parser, pragma_tok);
42638 finish_omp_barrier ();
42641 /* OpenMP 2.5:
42642 # pragma omp critical [(name)] new-line
42643 structured-block
42645 OpenMP 4.5:
42646 # pragma omp critical [(name) [hint(expression)]] new-line
42647 structured-block */
42649 #define OMP_CRITICAL_CLAUSE_MASK \
42650 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
42652 static tree
42653 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
42655 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
42657 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
42659 matching_parens parens;
42660 parens.consume_open (parser);
42662 name = cp_parser_identifier (parser);
42664 if (name == error_mark_node
42665 || !parens.require_close (parser))
42666 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
42667 /*or_comma=*/false,
42668 /*consume_paren=*/true);
42669 if (name == error_mark_node)
42670 name = NULL;
42672 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
42673 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
42674 cp_lexer_consume_token (parser->lexer);
42677 clauses = cp_parser_omp_all_clauses (parser, OMP_CRITICAL_CLAUSE_MASK,
42678 "#pragma omp critical", pragma_tok);
42680 stmt = cp_parser_omp_structured_block (parser, if_p);
42681 return c_finish_omp_critical (input_location, stmt, name, clauses);
42684 /* OpenMP 5.0:
42685 # pragma omp depobj ( depobj ) depobj-clause new-line
42687 depobj-clause:
42688 depend (dependence-type : locator)
42689 destroy
42690 update (dependence-type)
42692 dependence-type:
42695 inout
42696 mutexinout */
42698 static void
42699 cp_parser_omp_depobj (cp_parser *parser, cp_token *pragma_tok)
42701 location_t loc = pragma_tok->location;
42702 matching_parens parens;
42703 if (!parens.require_open (parser))
42705 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
42706 return;
42709 tree depobj = cp_parser_assignment_expression (parser);
42711 if (!parens.require_close (parser))
42712 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
42713 /*or_comma=*/false,
42714 /*consume_paren=*/true);
42716 tree clause = NULL_TREE;
42717 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INVALID;
42718 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
42719 cp_lexer_consume_token (parser->lexer);
42720 location_t c_loc = cp_lexer_peek_token (parser->lexer)->location;
42721 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42723 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
42724 const char *p = IDENTIFIER_POINTER (id);
42726 cp_lexer_consume_token (parser->lexer);
42727 if (!strcmp ("depend", p))
42729 /* Don't create location wrapper nodes within the depend clause. */
42730 auto_suppress_location_wrappers sentinel;
42731 clause = cp_parser_omp_clause_depend (parser, NULL_TREE, c_loc);
42732 if (clause)
42733 clause = finish_omp_clauses (clause, C_ORT_OMP);
42734 if (!clause)
42735 clause = error_mark_node;
42737 else if (!strcmp ("destroy", p))
42738 kind = OMP_CLAUSE_DEPEND_LAST;
42739 else if (!strcmp ("update", p))
42741 matching_parens c_parens;
42742 if (c_parens.require_open (parser))
42744 location_t c2_loc
42745 = cp_lexer_peek_token (parser->lexer)->location;
42746 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42748 tree id2 = cp_lexer_peek_token (parser->lexer)->u.value;
42749 const char *p2 = IDENTIFIER_POINTER (id2);
42751 cp_lexer_consume_token (parser->lexer);
42752 if (!strcmp ("in", p2))
42753 kind = OMP_CLAUSE_DEPEND_IN;
42754 else if (!strcmp ("out", p2))
42755 kind = OMP_CLAUSE_DEPEND_OUT;
42756 else if (!strcmp ("inout", p2))
42757 kind = OMP_CLAUSE_DEPEND_INOUT;
42758 else if (!strcmp ("mutexinoutset", p2))
42759 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
42760 else if (!strcmp ("inoutset", p2))
42761 kind = OMP_CLAUSE_DEPEND_INOUTSET;
42763 if (kind == OMP_CLAUSE_DEPEND_INVALID)
42765 clause = error_mark_node;
42766 error_at (c2_loc, "expected %<in%>, %<out%>, %<inout%>, "
42767 "%<mutexinoutset%> or %<inoutset%>");
42769 if (!c_parens.require_close (parser))
42770 cp_parser_skip_to_closing_parenthesis (parser,
42771 /*recovering=*/true,
42772 /*or_comma=*/false,
42773 /*consume_paren=*/true);
42775 else
42776 clause = error_mark_node;
42779 if (!clause && kind == OMP_CLAUSE_DEPEND_INVALID)
42781 clause = error_mark_node;
42782 error_at (c_loc, "expected %<depend%>, %<destroy%> or %<update%> clause");
42784 cp_parser_require_pragma_eol (parser, pragma_tok);
42786 finish_omp_depobj (loc, depobj, kind, clause);
42790 /* OpenMP 2.5:
42791 # pragma omp flush flush-vars[opt] new-line
42793 flush-vars:
42794 ( variable-list )
42796 OpenMP 5.0:
42797 # pragma omp flush memory-order-clause new-line */
42799 static void
42800 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
42802 enum memmodel mo = MEMMODEL_LAST;
42803 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
42804 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
42805 cp_lexer_consume_token (parser->lexer);
42806 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42808 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
42809 const char *p = IDENTIFIER_POINTER (id);
42810 if (!strcmp (p, "seq_cst"))
42811 mo = MEMMODEL_SEQ_CST;
42812 else if (!strcmp (p, "acq_rel"))
42813 mo = MEMMODEL_ACQ_REL;
42814 else if (!strcmp (p, "release"))
42815 mo = MEMMODEL_RELEASE;
42816 else if (!strcmp (p, "acquire"))
42817 mo = MEMMODEL_ACQUIRE;
42818 else
42819 error_at (cp_lexer_peek_token (parser->lexer)->location,
42820 "expected %<seq_cst%>, %<acq_rel%>, %<release%> or "
42821 "%<acquire%>");
42822 cp_lexer_consume_token (parser->lexer);
42824 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
42826 if (mo != MEMMODEL_LAST)
42827 error_at (cp_lexer_peek_token (parser->lexer)->location,
42828 "%<flush%> list specified together with memory order "
42829 "clause");
42830 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
42832 cp_parser_require_pragma_eol (parser, pragma_tok);
42834 finish_omp_flush (mo);
42837 /* Helper function, to parse omp for increment expression. */
42839 static tree
42840 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
42842 tree cond = cp_parser_binary_expression (parser, false, true,
42843 PREC_NOT_OPERATOR, NULL);
42844 if (cond == error_mark_node
42845 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
42847 cp_parser_skip_to_end_of_statement (parser);
42848 return error_mark_node;
42851 switch (TREE_CODE (cond))
42853 case GT_EXPR:
42854 case GE_EXPR:
42855 case LT_EXPR:
42856 case LE_EXPR:
42857 break;
42858 case NE_EXPR:
42859 if (code != OACC_LOOP)
42860 break;
42861 gcc_fallthrough ();
42862 default:
42863 return error_mark_node;
42866 /* If decl is an iterator, preserve LHS and RHS of the relational
42867 expr until finish_omp_for. */
42868 if (decl
42869 && (type_dependent_expression_p (decl)
42870 || CLASS_TYPE_P (TREE_TYPE (decl))))
42871 return cond;
42873 return build_x_binary_op (cp_expr_loc_or_input_loc (cond),
42874 TREE_CODE (cond),
42875 TREE_OPERAND (cond, 0), ERROR_MARK,
42876 TREE_OPERAND (cond, 1), ERROR_MARK,
42877 NULL_TREE, /*overload=*/NULL, tf_warning_or_error);
42880 /* Helper function, to parse omp for increment expression. */
42882 static tree
42883 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
42885 cp_token *token = cp_lexer_peek_token (parser->lexer);
42886 enum tree_code op;
42887 tree lhs, rhs;
42888 cp_id_kind idk;
42889 bool decl_first;
42891 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
42893 op = (token->type == CPP_PLUS_PLUS
42894 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
42895 cp_lexer_consume_token (parser->lexer);
42896 lhs = cp_parser_simple_cast_expression (parser);
42897 if (lhs != decl
42898 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
42899 return error_mark_node;
42900 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
42903 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
42904 if (lhs != decl
42905 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
42906 return error_mark_node;
42908 token = cp_lexer_peek_token (parser->lexer);
42909 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
42911 op = (token->type == CPP_PLUS_PLUS
42912 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
42913 cp_lexer_consume_token (parser->lexer);
42914 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
42917 op = cp_parser_assignment_operator_opt (parser);
42918 if (op == ERROR_MARK)
42919 return error_mark_node;
42921 if (op != NOP_EXPR)
42923 rhs = cp_parser_assignment_expression (parser);
42924 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
42925 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
42928 lhs = cp_parser_binary_expression (parser, false, false,
42929 PREC_ADDITIVE_EXPRESSION, NULL);
42930 token = cp_lexer_peek_token (parser->lexer);
42931 decl_first = (lhs == decl
42932 || (processing_template_decl && cp_tree_equal (lhs, decl)));
42933 if (decl_first)
42934 lhs = NULL_TREE;
42935 if (token->type != CPP_PLUS
42936 && token->type != CPP_MINUS)
42937 return error_mark_node;
42941 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
42942 cp_lexer_consume_token (parser->lexer);
42943 rhs = cp_parser_binary_expression (parser, false, false,
42944 PREC_ADDITIVE_EXPRESSION, NULL);
42945 token = cp_lexer_peek_token (parser->lexer);
42946 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
42948 if (lhs == NULL_TREE)
42950 if (op == PLUS_EXPR)
42951 lhs = rhs;
42952 else
42953 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
42954 NULL_TREE, tf_warning_or_error);
42956 else
42957 lhs = build_x_binary_op (input_location, op,
42958 lhs, ERROR_MARK,
42959 rhs, ERROR_MARK,
42960 NULL_TREE, NULL, tf_warning_or_error);
42963 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
42965 if (!decl_first)
42967 if ((rhs != decl
42968 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
42969 || op == MINUS_EXPR)
42970 return error_mark_node;
42971 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
42973 else
42974 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
42976 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
42979 /* Parse the initialization statement of an OpenMP for loop.
42981 Return true if the resulting construct should have an
42982 OMP_CLAUSE_PRIVATE added to it. */
42984 static tree
42985 cp_parser_omp_for_loop_init (cp_parser *parser,
42986 tree &this_pre_body,
42987 releasing_vec &for_block,
42988 tree &init,
42989 tree &orig_init,
42990 tree &decl,
42991 tree &real_decl)
42993 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
42994 return NULL_TREE;
42996 tree add_private_clause = NULL_TREE;
42998 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
43000 init-expr:
43001 var = lb
43002 integer-type var = lb
43003 random-access-iterator-type var = lb
43004 pointer-type var = lb
43006 cp_decl_specifier_seq type_specifiers;
43008 /* First, try to parse as an initialized declaration. See
43009 cp_parser_condition, from whence the bulk of this is copied. */
43011 cp_parser_parse_tentatively (parser);
43012 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
43013 /*is_declaration=*/true,
43014 /*is_trailing_return=*/false,
43015 &type_specifiers);
43016 if (cp_parser_parse_definitely (parser))
43018 /* If parsing a type specifier seq succeeded, then this
43019 MUST be a initialized declaration. */
43020 tree asm_specification, attributes;
43021 cp_declarator *declarator;
43023 declarator = cp_parser_declarator (parser,
43024 CP_PARSER_DECLARATOR_NAMED,
43025 CP_PARSER_FLAGS_NONE,
43026 /*ctor_dtor_or_conv_p=*/NULL,
43027 /*parenthesized_p=*/NULL,
43028 /*member_p=*/false,
43029 /*friend_p=*/false,
43030 /*static_p=*/false);
43031 attributes = cp_parser_attributes_opt (parser);
43032 asm_specification = cp_parser_asm_specification_opt (parser);
43034 if (declarator == cp_error_declarator)
43035 cp_parser_skip_to_end_of_statement (parser);
43037 else
43039 tree pushed_scope, auto_node;
43041 decl = start_decl (declarator, &type_specifiers,
43042 SD_INITIALIZED, attributes,
43043 /*prefix_attributes=*/NULL_TREE,
43044 &pushed_scope);
43046 auto_node = type_uses_auto (TREE_TYPE (decl));
43047 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
43049 if (cp_lexer_next_token_is (parser->lexer,
43050 CPP_OPEN_PAREN))
43051 error ("parenthesized initialization is not allowed in "
43052 "OpenMP %<for%> loop");
43053 else
43054 /* Trigger an error. */
43055 cp_parser_require (parser, CPP_EQ, RT_EQ);
43057 init = error_mark_node;
43058 cp_parser_skip_to_end_of_statement (parser);
43060 else if (CLASS_TYPE_P (TREE_TYPE (decl))
43061 || type_dependent_expression_p (decl)
43062 || auto_node)
43064 bool is_direct_init, is_non_constant_init;
43066 init = cp_parser_initializer (parser,
43067 &is_direct_init,
43068 &is_non_constant_init);
43070 if (auto_node)
43072 TREE_TYPE (decl)
43073 = do_auto_deduction (TREE_TYPE (decl), init,
43074 auto_node);
43076 if (!CLASS_TYPE_P (TREE_TYPE (decl))
43077 && !type_dependent_expression_p (decl))
43078 goto non_class;
43081 cp_finish_decl (decl, init, !is_non_constant_init,
43082 asm_specification,
43083 LOOKUP_ONLYCONVERTING);
43084 orig_init = init;
43085 if (CLASS_TYPE_P (TREE_TYPE (decl)))
43087 vec_safe_push (for_block, this_pre_body);
43088 init = NULL_TREE;
43090 else
43092 init = pop_stmt_list (this_pre_body);
43093 if (init && TREE_CODE (init) == STATEMENT_LIST)
43095 tree_stmt_iterator i = tsi_start (init);
43096 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
43097 while (!tsi_end_p (i))
43099 tree t = tsi_stmt (i);
43100 if (TREE_CODE (t) == DECL_EXPR
43101 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
43103 tsi_delink (&i);
43104 vec_safe_push (for_block, t);
43105 continue;
43107 break;
43109 if (tsi_one_before_end_p (i))
43111 tree t = tsi_stmt (i);
43112 tsi_delink (&i);
43113 free_stmt_list (init);
43114 init = t;
43118 this_pre_body = NULL_TREE;
43120 else
43122 /* Consume '='. */
43123 cp_lexer_consume_token (parser->lexer);
43124 init = cp_parser_assignment_expression (parser);
43126 non_class:
43127 if (TYPE_REF_P (TREE_TYPE (decl)))
43128 init = error_mark_node;
43129 else
43130 cp_finish_decl (decl, NULL_TREE,
43131 /*init_const_expr_p=*/false,
43132 asm_specification,
43133 LOOKUP_ONLYCONVERTING);
43136 if (pushed_scope)
43137 pop_scope (pushed_scope);
43140 else
43142 cp_id_kind idk;
43143 /* If parsing a type specifier sequence failed, then
43144 this MUST be a simple expression. */
43145 cp_parser_parse_tentatively (parser);
43146 decl = cp_parser_primary_expression (parser, false, false,
43147 false, &idk);
43148 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
43149 if (!cp_parser_error_occurred (parser)
43150 && decl
43151 && (TREE_CODE (decl) == COMPONENT_REF
43152 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
43154 cp_parser_abort_tentative_parse (parser);
43155 cp_parser_parse_tentatively (parser);
43156 cp_token *token = cp_lexer_peek_token (parser->lexer);
43157 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
43158 /*check_dependency_p=*/true,
43159 /*template_p=*/NULL,
43160 /*declarator_p=*/false,
43161 /*optional_p=*/false);
43162 if (name != error_mark_node
43163 && last_tok == cp_lexer_peek_token (parser->lexer))
43165 decl = cp_parser_lookup_name_simple (parser, name,
43166 token->location);
43167 if (TREE_CODE (decl) == FIELD_DECL)
43168 add_private_clause = omp_privatize_field (decl, false);
43170 cp_parser_abort_tentative_parse (parser);
43171 cp_parser_parse_tentatively (parser);
43172 decl = cp_parser_primary_expression (parser, false, false,
43173 false, &idk);
43175 if (!cp_parser_error_occurred (parser)
43176 && decl
43177 && DECL_P (decl)
43178 && CLASS_TYPE_P (TREE_TYPE (decl)))
43180 tree rhs;
43182 cp_parser_parse_definitely (parser);
43183 cp_parser_require (parser, CPP_EQ, RT_EQ);
43184 rhs = cp_parser_assignment_expression (parser);
43185 orig_init = rhs;
43186 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
43187 decl, NOP_EXPR,
43188 rhs, NULL_TREE,
43189 tf_warning_or_error));
43190 if (!add_private_clause)
43191 add_private_clause = decl;
43193 else
43195 decl = NULL;
43196 cp_parser_abort_tentative_parse (parser);
43197 init = cp_parser_expression (parser);
43198 if (init)
43200 if (TREE_CODE (init) == MODIFY_EXPR
43201 || TREE_CODE (init) == MODOP_EXPR)
43202 real_decl = TREE_OPERAND (init, 0);
43206 return add_private_clause;
43209 /* Helper for cp_parser_omp_for_loop, handle one range-for loop. */
43211 void
43212 cp_convert_omp_range_for (tree &this_pre_body, vec<tree, va_gc> *for_block,
43213 tree &decl, tree &orig_decl, tree &init,
43214 tree &orig_init, tree &cond, tree &incr)
43216 tree begin, end, range_temp_decl = NULL_TREE;
43217 tree iter_type, begin_expr, end_expr;
43218 bool clear_has_value_expr = false;
43220 if (processing_template_decl)
43222 if (check_for_bare_parameter_packs (init))
43223 init = error_mark_node;
43224 if (!type_dependent_expression_p (init)
43225 /* do_auto_deduction doesn't mess with template init-lists. */
43226 && !BRACE_ENCLOSED_INITIALIZER_P (init))
43228 tree d = decl;
43229 tree decomp_first_name = NULL_TREE;
43230 unsigned decomp_cnt = 0;
43231 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
43233 tree v = DECL_VALUE_EXPR (decl);
43234 if (TREE_CODE (v) == ARRAY_REF
43235 && VAR_P (TREE_OPERAND (v, 0))
43236 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
43238 d = TREE_OPERAND (v, 0);
43239 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
43240 decomp_first_name = decl;
43243 do_range_for_auto_deduction (d, init, decomp_first_name, decomp_cnt);
43245 cond = global_namespace;
43246 incr = NULL_TREE;
43247 orig_init = init;
43248 if (this_pre_body)
43249 this_pre_body = pop_stmt_list (this_pre_body);
43250 return;
43253 init = mark_lvalue_use (init);
43255 if (decl == error_mark_node || init == error_mark_node)
43256 /* If an error happened previously do nothing or else a lot of
43257 unhelpful errors would be issued. */
43258 begin_expr = end_expr = iter_type = error_mark_node;
43259 else
43261 tree range_temp;
43263 if (VAR_P (init)
43264 && array_of_runtime_bound_p (TREE_TYPE (init)))
43265 /* Can't bind a reference to an array of runtime bound. */
43266 range_temp = init;
43267 else
43269 range_temp = build_range_temp (init);
43270 DECL_NAME (range_temp) = NULL_TREE;
43271 pushdecl (range_temp);
43272 cp_finish_decl (range_temp, init,
43273 /*is_constant_init*/false, NULL_TREE,
43274 LOOKUP_ONLYCONVERTING);
43275 range_temp_decl = range_temp;
43276 range_temp = convert_from_reference (range_temp);
43278 iter_type = cp_parser_perform_range_for_lookup (range_temp,
43279 &begin_expr, &end_expr);
43282 tree end_iter_type = iter_type;
43283 if (cxx_dialect >= cxx17)
43284 end_iter_type = cv_unqualified (TREE_TYPE (end_expr));
43285 end = build_decl (input_location, VAR_DECL, NULL_TREE, end_iter_type);
43286 TREE_USED (end) = 1;
43287 DECL_ARTIFICIAL (end) = 1;
43288 pushdecl (end);
43289 cp_finish_decl (end, end_expr,
43290 /*is_constant_init*/false, NULL_TREE,
43291 LOOKUP_ONLYCONVERTING);
43293 /* The new for initialization statement. */
43294 begin = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
43295 TREE_USED (begin) = 1;
43296 DECL_ARTIFICIAL (begin) = 1;
43297 pushdecl (begin);
43298 orig_init = init;
43299 if (CLASS_TYPE_P (iter_type))
43300 init = NULL_TREE;
43301 else
43303 init = begin_expr;
43304 begin_expr = NULL_TREE;
43306 cp_finish_decl (begin, begin_expr,
43307 /*is_constant_init*/false, NULL_TREE,
43308 LOOKUP_ONLYCONVERTING);
43310 /* The new for condition. */
43311 if (CLASS_TYPE_P (iter_type))
43312 cond = build2 (NE_EXPR, boolean_type_node, begin, end);
43313 else
43314 cond = build_x_binary_op (input_location, NE_EXPR,
43315 begin, ERROR_MARK,
43316 end, ERROR_MARK,
43317 NULL_TREE, NULL, tf_warning_or_error);
43319 /* The new increment expression. */
43320 if (CLASS_TYPE_P (iter_type))
43321 incr = build2 (PREINCREMENT_EXPR, iter_type, begin, NULL_TREE);
43322 else
43323 incr = finish_unary_op_expr (input_location,
43324 PREINCREMENT_EXPR, begin,
43325 tf_warning_or_error);
43327 orig_decl = decl;
43328 decl = begin;
43329 if (for_block)
43331 vec_safe_push (for_block, this_pre_body);
43332 this_pre_body = NULL_TREE;
43335 tree decomp_first_name = NULL_TREE;
43336 unsigned decomp_cnt = 0;
43337 if (orig_decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (orig_decl))
43339 tree v = DECL_VALUE_EXPR (orig_decl);
43340 if (TREE_CODE (v) == ARRAY_REF
43341 && VAR_P (TREE_OPERAND (v, 0))
43342 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
43344 tree d = orig_decl;
43345 orig_decl = TREE_OPERAND (v, 0);
43346 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
43347 decomp_first_name = d;
43351 tree auto_node = type_uses_auto (TREE_TYPE (orig_decl));
43352 if (auto_node)
43354 tree t = build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
43355 NULL_TREE, tf_none);
43356 if (!error_operand_p (t))
43358 TREE_TYPE (orig_decl) = do_auto_deduction (TREE_TYPE (orig_decl),
43359 t, auto_node);
43360 if (decomp_first_name)
43362 ++processing_template_decl;
43363 cp_finish_decomp (orig_decl, decomp_first_name, decomp_cnt);
43364 --processing_template_decl;
43365 if (!processing_template_decl)
43366 clear_has_value_expr = true;
43371 tree v = make_tree_vec (decomp_cnt + 3);
43372 TREE_VEC_ELT (v, 0) = range_temp_decl;
43373 TREE_VEC_ELT (v, 1) = end;
43374 TREE_VEC_ELT (v, 2) = orig_decl;
43375 if (clear_has_value_expr)
43376 TREE_PUBLIC (v) = 1;
43377 for (unsigned i = 0; i < decomp_cnt; i++)
43379 if (clear_has_value_expr)
43381 /* If cp_finish_decomp was called with processing_template_decl
43382 temporarily set to 1, then decomp names will have deduced
43383 name but the DECL_VALUE_EXPR will be dependent. Hide those
43384 from folding of other loop initializers e.g. for warning
43385 purposes until cp_finish_omp_range_for. */
43386 gcc_checking_assert (DECL_HAS_VALUE_EXPR_P (decomp_first_name)
43387 || (TREE_TYPE (decomp_first_name)
43388 == error_mark_node));
43389 DECL_HAS_VALUE_EXPR_P (decomp_first_name) = 0;
43391 TREE_VEC_ELT (v, i + 3) = decomp_first_name;
43392 decomp_first_name = DECL_CHAIN (decomp_first_name);
43394 orig_decl = tree_cons (NULL_TREE, NULL_TREE, v);
43397 /* Helper for cp_parser_omp_for_loop, finalize part of range for
43398 inside of the collapsed body. */
43400 void
43401 cp_finish_omp_range_for (tree orig, tree begin)
43403 gcc_assert (TREE_CODE (orig) == TREE_LIST
43404 && TREE_CODE (TREE_CHAIN (orig)) == TREE_VEC);
43405 tree decl = TREE_VEC_ELT (TREE_CHAIN (orig), 2);
43406 tree decomp_first_name = NULL_TREE;
43407 unsigned int decomp_cnt = 0;
43409 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
43411 decomp_first_name = TREE_VEC_ELT (TREE_CHAIN (orig), 3);
43412 decomp_cnt = TREE_VEC_LENGTH (TREE_CHAIN (orig)) - 3;
43413 if (TREE_PUBLIC (TREE_CHAIN (orig)))
43415 /* Undo temporary clearing of DECL_HAS_VALUE_EXPR_P done
43416 by cp_convert_omp_range_for above. */
43417 TREE_PUBLIC (TREE_CHAIN (orig)) = 0;
43418 tree d = decomp_first_name;
43419 for (unsigned i = 0; i < decomp_cnt; i++)
43421 if (TREE_TYPE (d) != error_mark_node)
43422 DECL_HAS_VALUE_EXPR_P (d) = 1;
43423 d = DECL_CHAIN (d);
43426 cp_maybe_mangle_decomp (decl, decomp_first_name, decomp_cnt);
43429 /* The declaration is initialized with *__begin inside the loop body. */
43430 cp_finish_decl (decl,
43431 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
43432 NULL_TREE, tf_warning_or_error),
43433 /*is_constant_init*/false, NULL_TREE,
43434 LOOKUP_ONLYCONVERTING);
43435 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
43436 cp_finish_decomp (decl, decomp_first_name, decomp_cnt);
43439 /* Return true if next tokens contain a standard attribute that contains
43440 omp::directive (DIRECTIVE). */
43442 static bool
43443 cp_parser_omp_section_scan (cp_parser *parser, const char *directive,
43444 bool tentative)
43446 size_t n = cp_parser_skip_attributes_opt (parser, 1), i;
43447 if (n < 10)
43448 return false;
43449 for (i = 5; i < n - 4; i++)
43450 if (cp_lexer_nth_token_is (parser->lexer, i, CPP_NAME)
43451 && cp_lexer_nth_token_is (parser->lexer, i + 1, CPP_OPEN_PAREN)
43452 && cp_lexer_nth_token_is (parser->lexer, i + 2, CPP_NAME))
43454 tree first = cp_lexer_peek_nth_token (parser->lexer, i)->u.value;
43455 tree second = cp_lexer_peek_nth_token (parser->lexer, i + 2)->u.value;
43456 if (strcmp (IDENTIFIER_POINTER (first), "directive"))
43457 continue;
43458 if (strcmp (IDENTIFIER_POINTER (second), directive) == 0)
43459 break;
43461 if (i == n - 4)
43462 return false;
43463 cp_parser_parse_tentatively (parser);
43464 location_t first_loc = cp_lexer_peek_token (parser->lexer)->location;
43465 location_t last_loc
43466 = cp_lexer_peek_nth_token (parser->lexer, n - 1)->location;
43467 location_t middle_loc = UNKNOWN_LOCATION;
43468 tree std_attrs = cp_parser_std_attribute_spec_seq (parser);
43469 int cnt = 0;
43470 bool seen = false;
43471 for (tree attr = std_attrs; attr; attr = TREE_CHAIN (attr))
43472 if (get_attribute_namespace (attr) == omp_identifier
43473 && is_attribute_p ("directive", get_attribute_name (attr)))
43475 for (tree a = TREE_VALUE (attr); a; a = TREE_CHAIN (a))
43477 tree d = TREE_VALUE (a);
43478 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
43479 cp_token *first = DEFPARSE_TOKENS (d)->first;
43480 cnt++;
43481 if (first->type == CPP_NAME
43482 && strcmp (IDENTIFIER_POINTER (first->u.value),
43483 directive) == 0)
43485 seen = true;
43486 if (middle_loc == UNKNOWN_LOCATION)
43487 middle_loc = first->location;
43491 if (!seen || tentative)
43493 cp_parser_abort_tentative_parse (parser);
43494 return seen;
43496 if (cnt != 1 || TREE_CHAIN (std_attrs))
43498 error_at (make_location (first_loc, last_loc, middle_loc),
43499 "%<[[omp::directive(%s)]]%> must be the only specified "
43500 "attribute on a statement", directive);
43501 cp_parser_abort_tentative_parse (parser);
43502 return false;
43504 if (!cp_parser_parse_definitely (parser))
43505 return false;
43506 cp_parser_handle_statement_omp_attributes (parser, std_attrs);
43507 return true;
43510 /* Parse an OpenMP structured block sequence. KIND is the corresponding
43511 separating directive. */
43513 static tree
43514 cp_parser_omp_structured_block_sequence (cp_parser *parser,
43515 enum pragma_kind kind)
43517 tree stmt = begin_omp_structured_block ();
43518 unsigned int save = cp_parser_begin_omp_structured_block (parser);
43520 cp_parser_statement (parser, NULL_TREE, false, NULL);
43521 while (true)
43523 cp_token *token = cp_lexer_peek_token (parser->lexer);
43525 if (token->type == CPP_CLOSE_BRACE
43526 || token->type == CPP_EOF
43527 || token->type == CPP_PRAGMA_EOL
43528 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END)
43529 || (kind != PRAGMA_NONE
43530 && cp_parser_pragma_kind (token) == kind))
43531 break;
43533 if (kind != PRAGMA_NONE
43534 && cp_parser_omp_section_scan (parser,
43535 kind == PRAGMA_OMP_SCAN
43536 ? "scan" : "section", false))
43537 break;
43539 cp_parser_statement (parser, NULL_TREE, false, NULL);
43542 cp_parser_end_omp_structured_block (parser, save);
43543 return finish_omp_structured_block (stmt);
43547 /* OpenMP 5.0:
43549 scan-loop-body:
43550 { structured-block scan-directive structured-block } */
43552 static void
43553 cp_parser_omp_scan_loop_body (cp_parser *parser)
43555 tree substmt, clauses = NULL_TREE;
43556 bool found_scan = false;
43558 matching_braces braces;
43559 if (!braces.require_open (parser))
43560 return;
43562 cp_token *tok = cp_lexer_peek_token (parser->lexer);
43563 if (cp_parser_pragma_kind (tok) != PRAGMA_OMP_SCAN)
43564 substmt = cp_parser_omp_structured_block_sequence (parser, PRAGMA_OMP_SCAN);
43565 else
43567 warning_at (tok->location, 0, "%<#pragma omp scan%> with zero preceding "
43568 "executable statements");
43569 substmt = build_empty_stmt (tok->location);
43571 substmt = build2 (OMP_SCAN, void_type_node, substmt, NULL_TREE);
43572 add_stmt (substmt);
43574 tok = cp_lexer_peek_token (parser->lexer);
43575 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SCAN)
43577 enum omp_clause_code clause = OMP_CLAUSE_ERROR;
43578 found_scan = true;
43580 cp_lexer_consume_token (parser->lexer);
43582 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
43583 cp_lexer_consume_token (parser->lexer);
43585 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43587 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43588 const char *p = IDENTIFIER_POINTER (id);
43589 if (strcmp (p, "inclusive") == 0)
43590 clause = OMP_CLAUSE_INCLUSIVE;
43591 else if (strcmp (p, "exclusive") == 0)
43592 clause = OMP_CLAUSE_EXCLUSIVE;
43594 if (clause != OMP_CLAUSE_ERROR)
43596 cp_lexer_consume_token (parser->lexer);
43597 clauses = cp_parser_omp_var_list (parser, clause, NULL_TREE);
43599 else
43600 cp_parser_error (parser, "expected %<inclusive%> or "
43601 "%<exclusive%> clause");
43603 cp_parser_require_pragma_eol (parser, tok);
43605 else
43606 error ("expected %<#pragma omp scan%>");
43608 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
43609 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
43610 substmt = cp_parser_omp_structured_block_sequence (parser, PRAGMA_NONE);
43611 else
43613 if (found_scan)
43614 warning_at (tok->location, 0, "%<#pragma omp scan%> with zero "
43615 "succeeding executable statements");
43616 substmt = build_empty_stmt (tok->location);
43618 substmt = build2_loc (tok->location, OMP_SCAN, void_type_node, substmt,
43619 clauses);
43620 add_stmt (substmt);
43622 braces.require_close (parser);
43625 /* Parse the restricted form of the for statement allowed by OpenMP. */
43627 static tree
43628 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
43629 tree *cclauses, bool *if_p)
43631 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
43632 tree orig_decl;
43633 tree real_decl, initv, condv, incrv, declv, orig_declv;
43634 tree this_pre_body, cl, ordered_cl = NULL_TREE;
43635 location_t loc_first;
43636 bool collapse_err = false;
43637 int i, collapse = 1, ordered = 0, count, nbraces = 0;
43638 releasing_vec for_block;
43639 auto_vec<tree, 4> orig_inits;
43640 bool tiling = false;
43641 bool inscan = false;
43643 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
43644 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
43645 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
43646 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
43648 tiling = true;
43649 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
43651 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
43652 && OMP_CLAUSE_ORDERED_EXPR (cl))
43654 ordered_cl = cl;
43655 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
43657 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_REDUCTION
43658 && OMP_CLAUSE_REDUCTION_INSCAN (cl)
43659 && (code == OMP_SIMD || code == OMP_FOR))
43660 inscan = true;
43662 if (ordered && ordered < collapse)
43664 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
43665 "%<ordered%> clause parameter is less than %<collapse%>");
43666 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
43667 = build_int_cst (NULL_TREE, collapse);
43668 ordered = collapse;
43671 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
43672 count = ordered ? ordered : collapse;
43674 declv = make_tree_vec (count);
43675 initv = make_tree_vec (count);
43676 condv = make_tree_vec (count);
43677 incrv = make_tree_vec (count);
43678 orig_declv = NULL_TREE;
43680 loc_first = cp_lexer_peek_token (parser->lexer)->location;
43682 for (i = 0; i < count; i++)
43684 int bracecount = 0;
43685 tree add_private_clause = NULL_TREE;
43686 location_t loc;
43688 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
43690 if (!collapse_err)
43691 cp_parser_error (parser, "for statement expected");
43692 return NULL;
43694 loc = cp_lexer_consume_token (parser->lexer)->location;
43696 /* Don't create location wrapper nodes within an OpenMP "for"
43697 statement. */
43698 auto_suppress_location_wrappers sentinel;
43700 matching_parens parens;
43701 if (!parens.require_open (parser))
43702 return NULL;
43704 init = orig_init = decl = real_decl = orig_decl = NULL_TREE;
43705 this_pre_body = push_stmt_list ();
43707 if (code != OACC_LOOP && cxx_dialect >= cxx11)
43709 /* Save tokens so that we can put them back. */
43710 cp_lexer_save_tokens (parser->lexer);
43712 /* Look for ':' that is not nested in () or {}. */
43713 bool is_range_for
43714 = (cp_parser_skip_to_closing_parenthesis_1 (parser,
43715 /*recovering=*/false,
43716 CPP_COLON,
43717 /*consume_paren=*/
43718 false) == -1);
43720 /* Roll back the tokens we skipped. */
43721 cp_lexer_rollback_tokens (parser->lexer);
43723 if (is_range_for)
43725 bool saved_colon_corrects_to_scope_p
43726 = parser->colon_corrects_to_scope_p;
43728 /* A colon is used in range-based for. */
43729 parser->colon_corrects_to_scope_p = false;
43731 /* Parse the declaration. */
43732 cp_parser_simple_declaration (parser,
43733 /*function_definition_allowed_p=*/
43734 false, &decl);
43735 parser->colon_corrects_to_scope_p
43736 = saved_colon_corrects_to_scope_p;
43738 cp_parser_require (parser, CPP_COLON, RT_COLON);
43740 init = cp_parser_range_for (parser, NULL_TREE, NULL_TREE, decl,
43741 false, 0, true);
43743 cp_convert_omp_range_for (this_pre_body, for_block, decl,
43744 orig_decl, init, orig_init,
43745 cond, incr);
43746 if (this_pre_body)
43748 if (pre_body)
43750 tree t = pre_body;
43751 pre_body = push_stmt_list ();
43752 add_stmt (t);
43753 add_stmt (this_pre_body);
43754 pre_body = pop_stmt_list (pre_body);
43756 else
43757 pre_body = this_pre_body;
43760 if (ordered_cl)
43761 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
43762 "%<ordered%> clause with parameter on "
43763 "range-based %<for%> loop");
43765 goto parse_close_paren;
43769 add_private_clause
43770 = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
43771 init, orig_init, decl, real_decl);
43773 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
43774 if (this_pre_body)
43776 this_pre_body = pop_stmt_list (this_pre_body);
43777 if (pre_body)
43779 tree t = pre_body;
43780 pre_body = push_stmt_list ();
43781 add_stmt (t);
43782 add_stmt (this_pre_body);
43783 pre_body = pop_stmt_list (pre_body);
43785 else
43786 pre_body = this_pre_body;
43789 if (decl)
43790 real_decl = decl;
43791 if (cclauses != NULL
43792 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
43793 && real_decl != NULL_TREE
43794 && code != OMP_LOOP)
43796 tree *c;
43797 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
43798 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
43799 && OMP_CLAUSE_DECL (*c) == real_decl)
43801 error_at (loc, "iteration variable %qD"
43802 " should not be firstprivate", real_decl);
43803 *c = OMP_CLAUSE_CHAIN (*c);
43805 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
43806 && OMP_CLAUSE_DECL (*c) == real_decl)
43808 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
43809 tree l = *c;
43810 *c = OMP_CLAUSE_CHAIN (*c);
43811 if (code == OMP_SIMD)
43813 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
43814 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
43816 else
43818 OMP_CLAUSE_CHAIN (l) = clauses;
43819 clauses = l;
43821 add_private_clause = NULL_TREE;
43823 else
43825 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
43826 && OMP_CLAUSE_DECL (*c) == real_decl)
43827 add_private_clause = NULL_TREE;
43828 c = &OMP_CLAUSE_CHAIN (*c);
43832 if (add_private_clause)
43834 tree c;
43835 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
43837 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
43838 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
43839 && OMP_CLAUSE_DECL (c) == decl)
43840 break;
43841 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
43842 && OMP_CLAUSE_DECL (c) == decl)
43843 error_at (loc, "iteration variable %qD "
43844 "should not be firstprivate",
43845 decl);
43846 else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
43847 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
43848 && OMP_CLAUSE_DECL (c) == decl)
43849 error_at (loc, "iteration variable %qD should not be reduction",
43850 decl);
43852 if (c == NULL)
43854 if ((code == OMP_SIMD && collapse != 1) || code == OMP_LOOP)
43855 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
43856 else if (code != OMP_SIMD)
43857 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
43858 else
43859 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
43860 OMP_CLAUSE_DECL (c) = add_private_clause;
43861 c = finish_omp_clauses (c, C_ORT_OMP);
43862 if (c)
43864 OMP_CLAUSE_CHAIN (c) = clauses;
43865 clauses = c;
43866 /* For linear, signal that we need to fill up
43867 the so far unknown linear step. */
43868 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
43869 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
43874 cond = NULL;
43875 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
43876 cond = cp_parser_omp_for_cond (parser, decl, code);
43877 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
43879 incr = NULL;
43880 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
43882 /* If decl is an iterator, preserve the operator on decl
43883 until finish_omp_for. */
43884 if (real_decl
43885 && ((processing_template_decl
43886 && (TREE_TYPE (real_decl) == NULL_TREE
43887 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl))))
43888 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
43889 incr = cp_parser_omp_for_incr (parser, real_decl);
43890 else
43891 incr = cp_parser_expression (parser);
43892 protected_set_expr_location_if_unset (incr, input_location);
43895 parse_close_paren:
43896 if (!parens.require_close (parser))
43897 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
43898 /*or_comma=*/false,
43899 /*consume_paren=*/true);
43901 TREE_VEC_ELT (declv, i) = decl;
43902 TREE_VEC_ELT (initv, i) = init;
43903 TREE_VEC_ELT (condv, i) = cond;
43904 TREE_VEC_ELT (incrv, i) = incr;
43905 if (orig_init)
43907 orig_inits.safe_grow_cleared (i + 1, true);
43908 orig_inits[i] = orig_init;
43910 if (orig_decl)
43912 if (!orig_declv)
43913 orig_declv = copy_node (declv);
43914 TREE_VEC_ELT (orig_declv, i) = orig_decl;
43916 else if (orig_declv)
43917 TREE_VEC_ELT (orig_declv, i) = decl;
43919 if (i == count - 1)
43920 break;
43922 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
43923 in between the collapsed for loops to be still considered perfectly
43924 nested. Hopefully the final version clarifies this.
43925 For now handle (multiple) {'s and empty statements. */
43926 cp_parser_parse_tentatively (parser);
43927 for (;;)
43929 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
43930 break;
43931 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
43933 cp_lexer_consume_token (parser->lexer);
43934 bracecount++;
43936 else if (bracecount
43937 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
43938 cp_lexer_consume_token (parser->lexer);
43939 else
43941 loc = cp_lexer_peek_token (parser->lexer)->location;
43942 error_at (loc, "not enough for loops to collapse");
43943 collapse_err = true;
43944 cp_parser_abort_tentative_parse (parser);
43945 declv = NULL_TREE;
43946 break;
43950 if (declv)
43952 cp_parser_parse_definitely (parser);
43953 nbraces += bracecount;
43957 if (nbraces)
43958 if_p = NULL;
43960 /* Note that we saved the original contents of this flag when we entered
43961 the structured block, and so we don't need to re-save it here. */
43962 parser->in_statement = IN_OMP_FOR;
43964 /* Note that the grammar doesn't call for a structured block here,
43965 though the loop as a whole is a structured block. */
43966 if (orig_declv)
43968 body = begin_omp_structured_block ();
43969 for (i = 0; i < count; i++)
43970 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i))
43971 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
43972 TREE_VEC_ELT (declv, i));
43974 else
43975 body = push_stmt_list ();
43976 if (inscan)
43977 cp_parser_omp_scan_loop_body (parser);
43978 else
43979 cp_parser_statement (parser, NULL_TREE, false, if_p);
43980 if (orig_declv)
43981 body = finish_omp_structured_block (body);
43982 else
43983 body = pop_stmt_list (body);
43985 if (declv == NULL_TREE)
43986 ret = NULL_TREE;
43987 else
43988 ret = finish_omp_for (loc_first, code, declv, orig_declv, initv, condv,
43989 incrv, body, pre_body, &orig_inits, clauses);
43991 while (nbraces)
43993 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
43995 cp_lexer_consume_token (parser->lexer);
43996 nbraces--;
43998 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
43999 cp_lexer_consume_token (parser->lexer);
44000 else
44002 if (!collapse_err)
44004 error_at (cp_lexer_peek_token (parser->lexer)->location,
44005 "collapsed loops not perfectly nested");
44007 collapse_err = true;
44008 cp_parser_statement_seq_opt (parser, NULL);
44009 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
44010 break;
44014 while (!for_block->is_empty ())
44016 tree t = for_block->pop ();
44017 if (TREE_CODE (t) == STATEMENT_LIST)
44018 add_stmt (pop_stmt_list (t));
44019 else
44020 add_stmt (t);
44023 return ret;
44026 /* Helper function for OpenMP parsing, split clauses and call
44027 finish_omp_clauses on each of the set of clauses afterwards. */
44029 static void
44030 cp_omp_split_clauses (location_t loc, enum tree_code code,
44031 omp_clause_mask mask, tree clauses, tree *cclauses)
44033 int i;
44034 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
44035 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
44036 if (cclauses[i])
44037 cclauses[i] = finish_omp_clauses (cclauses[i],
44038 i == C_OMP_CLAUSE_SPLIT_TARGET
44039 ? C_ORT_OMP_TARGET : C_ORT_OMP);
44042 /* OpenMP 5.0:
44043 #pragma omp loop loop-clause[optseq] new-line
44044 for-loop */
44046 #define OMP_LOOP_CLAUSE_MASK \
44047 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
44048 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
44049 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
44050 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
44051 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_BIND) \
44052 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
44054 static tree
44055 cp_parser_omp_loop (cp_parser *parser, cp_token *pragma_tok,
44056 char *p_name, omp_clause_mask mask, tree *cclauses,
44057 bool *if_p)
44059 tree clauses, sb, ret;
44060 unsigned int save;
44061 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
44063 strcat (p_name, " loop");
44064 mask |= OMP_LOOP_CLAUSE_MASK;
44066 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
44067 cclauses == NULL);
44068 if (cclauses)
44070 cp_omp_split_clauses (loc, OMP_LOOP, mask, clauses, cclauses);
44071 clauses = cclauses[C_OMP_CLAUSE_SPLIT_LOOP];
44074 keep_next_level (true);
44075 sb = begin_omp_structured_block ();
44076 save = cp_parser_begin_omp_structured_block (parser);
44078 ret = cp_parser_omp_for_loop (parser, OMP_LOOP, clauses, cclauses, if_p);
44080 cp_parser_end_omp_structured_block (parser, save);
44081 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
44083 return ret;
44086 /* OpenMP 4.0:
44087 #pragma omp simd simd-clause[optseq] new-line
44088 for-loop */
44090 #define OMP_SIMD_CLAUSE_MASK \
44091 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
44092 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
44093 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
44094 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
44095 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
44096 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
44097 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
44098 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
44099 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
44100 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL) \
44101 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
44103 static tree
44104 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
44105 char *p_name, omp_clause_mask mask, tree *cclauses,
44106 bool *if_p)
44108 tree clauses, sb, ret;
44109 unsigned int save;
44110 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
44112 strcat (p_name, " simd");
44113 mask |= OMP_SIMD_CLAUSE_MASK;
44115 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
44116 cclauses == NULL);
44117 if (cclauses)
44119 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
44120 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
44123 keep_next_level (true);
44124 sb = begin_omp_structured_block ();
44125 save = cp_parser_begin_omp_structured_block (parser);
44127 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
44129 cp_parser_end_omp_structured_block (parser, save);
44130 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
44132 return ret;
44135 /* OpenMP 2.5:
44136 #pragma omp for for-clause[optseq] new-line
44137 for-loop
44139 OpenMP 4.0:
44140 #pragma omp for simd for-simd-clause[optseq] new-line
44141 for-loop */
44143 #define OMP_FOR_CLAUSE_MASK \
44144 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
44145 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
44146 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
44147 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
44148 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
44149 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
44150 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
44151 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
44152 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
44153 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
44154 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
44156 static tree
44157 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
44158 char *p_name, omp_clause_mask mask, tree *cclauses,
44159 bool *if_p)
44161 tree clauses, sb, ret;
44162 unsigned int save;
44163 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
44165 strcat (p_name, " for");
44166 mask |= OMP_FOR_CLAUSE_MASK;
44167 /* parallel for{, simd} disallows nowait clause, but for
44168 target {teams distribute ,}parallel for{, simd} it should be accepted. */
44169 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
44170 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
44171 /* Composite distribute parallel for{, simd} disallows ordered clause. */
44172 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
44173 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
44175 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
44177 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
44178 const char *p = IDENTIFIER_POINTER (id);
44180 if (strcmp (p, "simd") == 0)
44182 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
44183 if (cclauses == NULL)
44184 cclauses = cclauses_buf;
44186 cp_lexer_consume_token (parser->lexer);
44187 if (!flag_openmp) /* flag_openmp_simd */
44188 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
44189 cclauses, if_p);
44190 sb = begin_omp_structured_block ();
44191 save = cp_parser_begin_omp_structured_block (parser);
44192 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
44193 cclauses, if_p);
44194 cp_parser_end_omp_structured_block (parser, save);
44195 tree body = finish_omp_structured_block (sb);
44196 if (ret == NULL)
44197 return ret;
44198 ret = make_node (OMP_FOR);
44199 TREE_TYPE (ret) = void_type_node;
44200 OMP_FOR_BODY (ret) = body;
44201 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
44202 SET_EXPR_LOCATION (ret, loc);
44203 add_stmt (ret);
44204 return ret;
44207 if (!flag_openmp) /* flag_openmp_simd */
44209 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44210 return NULL_TREE;
44213 /* Composite distribute parallel for disallows linear clause. */
44214 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
44215 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
44217 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
44218 cclauses == NULL);
44219 if (cclauses)
44221 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
44222 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
44225 keep_next_level (true);
44226 sb = begin_omp_structured_block ();
44227 save = cp_parser_begin_omp_structured_block (parser);
44229 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
44231 cp_parser_end_omp_structured_block (parser, save);
44232 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
44234 return ret;
44237 static tree cp_parser_omp_taskloop (cp_parser *, cp_token *, char *,
44238 omp_clause_mask, tree *, bool *);
44240 /* OpenMP 2.5:
44241 # pragma omp master new-line
44242 structured-block */
44244 static tree
44245 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok,
44246 char *p_name, omp_clause_mask mask, tree *cclauses,
44247 bool *if_p)
44249 tree clauses, sb, ret;
44250 unsigned int save;
44251 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
44253 strcat (p_name, " master");
44255 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
44257 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
44258 const char *p = IDENTIFIER_POINTER (id);
44260 if (strcmp (p, "taskloop") == 0)
44262 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
44263 if (cclauses == NULL)
44264 cclauses = cclauses_buf;
44266 cp_lexer_consume_token (parser->lexer);
44267 if (!flag_openmp) /* flag_openmp_simd */
44268 return cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
44269 cclauses, if_p);
44270 sb = begin_omp_structured_block ();
44271 save = cp_parser_begin_omp_structured_block (parser);
44272 ret = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
44273 cclauses, if_p);
44274 cp_parser_end_omp_structured_block (parser, save);
44275 tree body = finish_omp_structured_block (sb);
44276 if (ret == NULL)
44277 return ret;
44278 ret = c_finish_omp_master (loc, body);
44279 OMP_MASTER_COMBINED (ret) = 1;
44280 return ret;
44283 if (!flag_openmp) /* flag_openmp_simd */
44285 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44286 return NULL_TREE;
44289 if (cclauses)
44291 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
44292 false);
44293 cp_omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
44295 else
44296 cp_parser_require_pragma_eol (parser, pragma_tok);
44298 return c_finish_omp_master (loc,
44299 cp_parser_omp_structured_block (parser, if_p));
44302 /* OpenMP 5.1:
44303 # pragma omp masked masked-clauses new-line
44304 structured-block */
44306 #define OMP_MASKED_CLAUSE_MASK \
44307 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FILTER)
44309 static tree
44310 cp_parser_omp_masked (cp_parser *parser, cp_token *pragma_tok,
44311 char *p_name, omp_clause_mask mask, tree *cclauses,
44312 bool *if_p)
44314 tree clauses, sb, ret;
44315 unsigned int save;
44316 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
44318 strcat (p_name, " masked");
44319 mask |= OMP_MASKED_CLAUSE_MASK;
44321 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
44323 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
44324 const char *p = IDENTIFIER_POINTER (id);
44326 if (strcmp (p, "taskloop") == 0)
44328 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
44329 if (cclauses == NULL)
44330 cclauses = cclauses_buf;
44332 cp_lexer_consume_token (parser->lexer);
44333 if (!flag_openmp) /* flag_openmp_simd */
44334 return cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
44335 cclauses, if_p);
44336 sb = begin_omp_structured_block ();
44337 save = cp_parser_begin_omp_structured_block (parser);
44338 ret = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
44339 cclauses, if_p);
44340 cp_parser_end_omp_structured_block (parser, save);
44341 tree body = finish_omp_structured_block (sb);
44342 if (ret == NULL)
44343 return ret;
44344 ret = c_finish_omp_masked (loc, body,
44345 cclauses[C_OMP_CLAUSE_SPLIT_MASKED]);
44346 OMP_MASKED_COMBINED (ret) = 1;
44347 return ret;
44350 if (!flag_openmp) /* flag_openmp_simd */
44352 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44353 return NULL_TREE;
44356 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
44357 cclauses == NULL);
44358 if (cclauses)
44360 cp_omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
44361 clauses = cclauses[C_OMP_CLAUSE_SPLIT_MASKED];
44364 return c_finish_omp_masked (loc,
44365 cp_parser_omp_structured_block (parser, if_p),
44366 clauses);
44369 /* OpenMP 2.5:
44370 # pragma omp ordered new-line
44371 structured-block
44373 OpenMP 4.5:
44374 # pragma omp ordered ordered-clauses new-line
44375 structured-block */
44377 #define OMP_ORDERED_CLAUSE_MASK \
44378 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
44379 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
44381 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
44382 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
44383 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DOACROSS))
44385 static bool
44386 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
44387 enum pragma_context context, bool *if_p)
44389 location_t loc = pragma_tok->location;
44390 int n = 1;
44392 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
44393 n = 2;
44395 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_NAME))
44397 tree id = cp_lexer_peek_nth_token (parser->lexer, n)->u.value;
44398 const char *p = IDENTIFIER_POINTER (id);
44400 if (strcmp (p, "depend") == 0 || strcmp (p, "doacross") == 0)
44402 if (!flag_openmp) /* flag_openmp_simd */
44404 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44405 return false;
44407 if (context == pragma_stmt)
44409 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
44410 "%qs clause may only be used in compound "
44411 "statements", p);
44412 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44413 return true;
44415 tree clauses
44416 = cp_parser_omp_all_clauses (parser,
44417 OMP_ORDERED_DEPEND_CLAUSE_MASK,
44418 "#pragma omp ordered", pragma_tok);
44419 c_finish_omp_ordered (loc, clauses, NULL_TREE);
44420 return false;
44424 tree clauses
44425 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
44426 "#pragma omp ordered", pragma_tok);
44428 if (!flag_openmp /* flag_openmp_simd */
44429 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
44430 return false;
44432 c_finish_omp_ordered (loc, clauses,
44433 cp_parser_omp_structured_block (parser, if_p));
44434 return true;
44437 /* OpenMP 2.5:
44439 section-scope:
44440 { section-sequence }
44442 section-sequence:
44443 section-directive[opt] structured-block
44444 section-sequence section-directive structured-block */
44446 static tree
44447 cp_parser_omp_sections_scope (cp_parser *parser)
44449 tree stmt, substmt;
44450 bool error_suppress = false;
44451 cp_token *tok;
44453 matching_braces braces;
44454 if (!braces.require_open (parser))
44455 return NULL_TREE;
44457 stmt = push_stmt_list ();
44459 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
44460 != PRAGMA_OMP_SECTION
44461 && !cp_parser_omp_section_scan (parser, "section", true))
44463 substmt = cp_parser_omp_structured_block_sequence (parser,
44464 PRAGMA_OMP_SECTION);
44465 substmt = build1 (OMP_SECTION, void_type_node, substmt);
44466 add_stmt (substmt);
44469 while (1)
44471 tok = cp_lexer_peek_token (parser->lexer);
44472 if (tok->type == CPP_CLOSE_BRACE)
44473 break;
44474 if (tok->type == CPP_EOF)
44475 break;
44477 if (cp_parser_omp_section_scan (parser, "section", false))
44478 tok = cp_lexer_peek_token (parser->lexer);
44479 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
44481 cp_lexer_consume_token (parser->lexer);
44482 cp_parser_require_pragma_eol (parser, tok);
44483 error_suppress = false;
44485 else if (!error_suppress)
44487 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
44488 error_suppress = true;
44491 substmt = cp_parser_omp_structured_block_sequence (parser,
44492 PRAGMA_OMP_SECTION);
44493 substmt = build1 (OMP_SECTION, void_type_node, substmt);
44494 add_stmt (substmt);
44496 braces.require_close (parser);
44498 substmt = pop_stmt_list (stmt);
44500 stmt = make_node (OMP_SECTIONS);
44501 TREE_TYPE (stmt) = void_type_node;
44502 OMP_SECTIONS_BODY (stmt) = substmt;
44504 add_stmt (stmt);
44505 return stmt;
44508 /* OpenMP 2.5:
44509 # pragma omp sections sections-clause[optseq] newline
44510 sections-scope */
44512 #define OMP_SECTIONS_CLAUSE_MASK \
44513 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
44514 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
44515 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
44516 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
44517 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
44518 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
44520 static tree
44521 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
44522 char *p_name, omp_clause_mask mask, tree *cclauses)
44524 tree clauses, ret;
44525 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
44527 strcat (p_name, " sections");
44528 mask |= OMP_SECTIONS_CLAUSE_MASK;
44529 if (cclauses)
44530 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
44532 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
44533 cclauses == NULL);
44534 if (cclauses)
44536 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
44537 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
44540 ret = cp_parser_omp_sections_scope (parser);
44541 if (ret)
44542 OMP_SECTIONS_CLAUSES (ret) = clauses;
44544 return ret;
44547 /* OpenMP 2.5:
44548 # pragma omp parallel parallel-clause[optseq] new-line
44549 structured-block
44550 # pragma omp parallel for parallel-for-clause[optseq] new-line
44551 structured-block
44552 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
44553 structured-block
44555 OpenMP 4.0:
44556 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
44557 structured-block */
44559 #define OMP_PARALLEL_CLAUSE_MASK \
44560 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
44561 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
44562 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
44563 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
44564 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
44565 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
44566 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
44567 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
44568 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
44569 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
44571 static tree
44572 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
44573 char *p_name, omp_clause_mask mask, tree *cclauses,
44574 bool *if_p)
44576 tree stmt, clauses, block;
44577 unsigned int save;
44578 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
44580 strcat (p_name, " parallel");
44581 mask |= OMP_PARALLEL_CLAUSE_MASK;
44582 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
44583 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
44584 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
44585 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
44587 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
44589 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
44590 if (cclauses == NULL)
44591 cclauses = cclauses_buf;
44593 cp_lexer_consume_token (parser->lexer);
44594 if (!flag_openmp) /* flag_openmp_simd */
44595 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
44596 if_p);
44597 block = begin_omp_parallel ();
44598 save = cp_parser_begin_omp_structured_block (parser);
44599 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
44600 if_p);
44601 cp_parser_end_omp_structured_block (parser, save);
44602 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
44603 block);
44604 if (ret == NULL_TREE)
44605 return ret;
44606 OMP_PARALLEL_COMBINED (stmt) = 1;
44607 return stmt;
44609 /* When combined with distribute, parallel has to be followed by for.
44610 #pragma omp target parallel is allowed though. */
44611 else if (cclauses
44612 && (mask & (OMP_CLAUSE_MASK_1
44613 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
44615 error_at (loc, "expected %<for%> after %qs", p_name);
44616 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44617 return NULL_TREE;
44619 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
44621 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
44622 const char *p = IDENTIFIER_POINTER (id);
44623 if (cclauses == NULL && strcmp (p, "masked") == 0)
44625 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
44626 cclauses = cclauses_buf;
44628 cp_lexer_consume_token (parser->lexer);
44629 if (!flag_openmp) /* flag_openmp_simd */
44630 return cp_parser_omp_masked (parser, pragma_tok, p_name, mask,
44631 cclauses, if_p);
44632 block = begin_omp_parallel ();
44633 save = cp_parser_begin_omp_structured_block (parser);
44634 tree ret = cp_parser_omp_masked (parser, pragma_tok, p_name, mask,
44635 cclauses, if_p);
44636 cp_parser_end_omp_structured_block (parser, save);
44637 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
44638 block);
44639 if (ret == NULL_TREE)
44640 return ret;
44641 /* masked does have just filter clause, but during gimplification
44642 isn't represented by a gimplification omp context, so for
44643 #pragma omp parallel masked don't set OMP_PARALLEL_COMBINED,
44644 so that
44645 #pragma omp parallel masked
44646 #pragma omp taskloop simd lastprivate (x)
44647 isn't confused with
44648 #pragma omp parallel masked taskloop simd lastprivate (x) */
44649 if (OMP_MASKED_COMBINED (ret))
44650 OMP_PARALLEL_COMBINED (stmt) = 1;
44651 return stmt;
44653 else if (cclauses == NULL && strcmp (p, "master") == 0)
44655 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
44656 cclauses = cclauses_buf;
44658 cp_lexer_consume_token (parser->lexer);
44659 if (!flag_openmp) /* flag_openmp_simd */
44660 return cp_parser_omp_master (parser, pragma_tok, p_name, mask,
44661 cclauses, if_p);
44662 block = begin_omp_parallel ();
44663 save = cp_parser_begin_omp_structured_block (parser);
44664 tree ret = cp_parser_omp_master (parser, pragma_tok, p_name, mask,
44665 cclauses, if_p);
44666 cp_parser_end_omp_structured_block (parser, save);
44667 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
44668 block);
44669 if (ret == NULL_TREE)
44670 return ret;
44671 /* master doesn't have any clauses and during gimplification
44672 isn't represented by a gimplification omp context, so for
44673 #pragma omp parallel master don't set OMP_PARALLEL_COMBINED,
44674 so that
44675 #pragma omp parallel master
44676 #pragma omp taskloop simd lastprivate (x)
44677 isn't confused with
44678 #pragma omp parallel master taskloop simd lastprivate (x) */
44679 if (OMP_MASTER_COMBINED (ret))
44680 OMP_PARALLEL_COMBINED (stmt) = 1;
44681 return stmt;
44683 else if (strcmp (p, "loop") == 0)
44685 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
44686 if (cclauses == NULL)
44687 cclauses = cclauses_buf;
44689 cp_lexer_consume_token (parser->lexer);
44690 if (!flag_openmp) /* flag_openmp_simd */
44691 return cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
44692 cclauses, if_p);
44693 block = begin_omp_parallel ();
44694 save = cp_parser_begin_omp_structured_block (parser);
44695 tree ret = cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
44696 cclauses, if_p);
44697 cp_parser_end_omp_structured_block (parser, save);
44698 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
44699 block);
44700 if (ret == NULL_TREE)
44701 return ret;
44702 OMP_PARALLEL_COMBINED (stmt) = 1;
44703 return stmt;
44705 else if (!flag_openmp) /* flag_openmp_simd */
44707 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44708 return NULL_TREE;
44710 else if (cclauses == NULL && strcmp (p, "sections") == 0)
44712 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
44713 cclauses = cclauses_buf;
44715 cp_lexer_consume_token (parser->lexer);
44716 block = begin_omp_parallel ();
44717 save = cp_parser_begin_omp_structured_block (parser);
44718 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
44719 cp_parser_end_omp_structured_block (parser, save);
44720 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
44721 block);
44722 OMP_PARALLEL_COMBINED (stmt) = 1;
44723 return stmt;
44726 else if (!flag_openmp) /* flag_openmp_simd */
44728 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44729 return NULL_TREE;
44732 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
44733 cclauses == NULL);
44734 if (cclauses)
44736 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
44737 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
44740 block = begin_omp_parallel ();
44741 save = cp_parser_begin_omp_structured_block (parser);
44742 parser->omp_attrs_forbidden_p = true;
44743 cp_parser_statement (parser, NULL_TREE, false, if_p);
44744 cp_parser_end_omp_structured_block (parser, save);
44745 stmt = finish_omp_parallel (clauses, block);
44746 return stmt;
44749 /* OpenMP 2.5:
44750 # pragma omp single single-clause[optseq] new-line
44751 structured-block */
44753 #define OMP_SINGLE_CLAUSE_MASK \
44754 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
44755 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
44756 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
44757 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
44758 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
44760 static tree
44761 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
44763 tree stmt = make_node (OMP_SINGLE);
44764 TREE_TYPE (stmt) = void_type_node;
44765 SET_EXPR_LOCATION (stmt, pragma_tok->location);
44767 OMP_SINGLE_CLAUSES (stmt)
44768 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
44769 "#pragma omp single", pragma_tok);
44770 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
44772 return add_stmt (stmt);
44775 /* OpenMP 5.1:
44776 # pragma omp scope scope-clause[optseq] new-line
44777 structured-block */
44779 #define OMP_SCOPE_CLAUSE_MASK \
44780 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
44781 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
44782 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
44783 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
44784 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
44786 static tree
44787 cp_parser_omp_scope (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
44789 tree stmt = make_node (OMP_SCOPE);
44790 TREE_TYPE (stmt) = void_type_node;
44791 SET_EXPR_LOCATION (stmt, pragma_tok->location);
44793 OMP_SCOPE_CLAUSES (stmt)
44794 = cp_parser_omp_all_clauses (parser, OMP_SCOPE_CLAUSE_MASK,
44795 "#pragma omp scope", pragma_tok);
44796 OMP_SCOPE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
44798 return add_stmt (stmt);
44801 /* OpenMP 3.0:
44802 # pragma omp task task-clause[optseq] new-line
44803 structured-block */
44805 #define OMP_TASK_CLAUSE_MASK \
44806 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
44807 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
44808 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
44809 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
44810 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
44811 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
44812 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
44813 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
44814 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
44815 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
44816 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
44817 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION) \
44818 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DETACH) \
44819 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_AFFINITY))
44821 static tree
44822 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
44824 tree clauses, block;
44825 unsigned int save;
44827 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
44828 "#pragma omp task", pragma_tok);
44829 block = begin_omp_task ();
44830 save = cp_parser_begin_omp_structured_block (parser);
44831 parser->omp_attrs_forbidden_p = true;
44832 cp_parser_statement (parser, NULL_TREE, false, if_p);
44833 cp_parser_end_omp_structured_block (parser, save);
44834 return finish_omp_task (clauses, block);
44837 /* OpenMP 3.0:
44838 # pragma omp taskwait new-line
44840 OpenMP 5.0:
44841 # pragma omp taskwait taskwait-clause[opt] new-line */
44843 #define OMP_TASKWAIT_CLAUSE_MASK \
44844 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
44845 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
44847 static void
44848 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
44850 tree clauses
44851 = cp_parser_omp_all_clauses (parser, OMP_TASKWAIT_CLAUSE_MASK,
44852 "#pragma omp taskwait", pragma_tok);
44854 if (clauses)
44856 tree stmt = make_node (OMP_TASK);
44857 TREE_TYPE (stmt) = void_node;
44858 OMP_TASK_CLAUSES (stmt) = clauses;
44859 OMP_TASK_BODY (stmt) = NULL_TREE;
44860 SET_EXPR_LOCATION (stmt, pragma_tok->location);
44861 add_stmt (stmt);
44863 else
44864 finish_omp_taskwait ();
44867 /* OpenMP 3.1:
44868 # pragma omp taskyield new-line */
44870 static void
44871 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
44873 cp_parser_require_pragma_eol (parser, pragma_tok);
44874 finish_omp_taskyield ();
44877 /* OpenMP 4.0:
44878 # pragma omp taskgroup new-line
44879 structured-block
44881 OpenMP 5.0:
44882 # pragma omp taskgroup taskgroup-clause[optseq] new-line */
44884 #define OMP_TASKGROUP_CLAUSE_MASK \
44885 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
44886 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
44888 static tree
44889 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
44891 tree clauses
44892 = cp_parser_omp_all_clauses (parser, OMP_TASKGROUP_CLAUSE_MASK,
44893 "#pragma omp taskgroup", pragma_tok);
44894 return c_finish_omp_taskgroup (input_location,
44895 cp_parser_omp_structured_block (parser,
44896 if_p),
44897 clauses);
44901 /* OpenMP 2.5:
44902 # pragma omp threadprivate (variable-list) */
44904 static void
44905 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
44907 tree vars;
44909 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
44910 cp_parser_require_pragma_eol (parser, pragma_tok);
44912 finish_omp_threadprivate (vars);
44915 /* OpenMP 4.0:
44916 # pragma omp cancel cancel-clause[optseq] new-line */
44918 #define OMP_CANCEL_CLAUSE_MASK \
44919 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
44920 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
44921 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
44922 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
44923 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
44925 static void
44926 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
44928 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
44929 "#pragma omp cancel", pragma_tok);
44930 finish_omp_cancel (clauses);
44933 /* OpenMP 4.0:
44934 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
44936 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
44937 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
44938 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
44939 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
44940 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
44942 static bool
44943 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
44944 enum pragma_context context)
44946 tree clauses;
44947 bool point_seen = false;
44949 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
44951 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
44952 const char *p = IDENTIFIER_POINTER (id);
44954 if (strcmp (p, "point") == 0)
44956 cp_lexer_consume_token (parser->lexer);
44957 point_seen = true;
44960 if (!point_seen)
44962 cp_parser_error (parser, "expected %<point%>");
44963 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44964 return false;
44967 if (context != pragma_compound)
44969 if (context == pragma_stmt)
44970 error_at (pragma_tok->location,
44971 "%<#pragma %s%> may only be used in compound statements",
44972 "omp cancellation point");
44973 else
44974 cp_parser_error (parser, "expected declaration specifiers");
44975 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44976 return true;
44979 clauses = cp_parser_omp_all_clauses (parser,
44980 OMP_CANCELLATION_POINT_CLAUSE_MASK,
44981 "#pragma omp cancellation point",
44982 pragma_tok);
44983 finish_omp_cancellation_point (clauses);
44984 return true;
44987 /* OpenMP 4.0:
44988 #pragma omp distribute distribute-clause[optseq] new-line
44989 for-loop */
44991 #define OMP_DISTRIBUTE_CLAUSE_MASK \
44992 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
44993 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
44994 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
44995 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
44996 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
44997 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
44998 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
45000 static tree
45001 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
45002 char *p_name, omp_clause_mask mask, tree *cclauses,
45003 bool *if_p)
45005 tree clauses, sb, ret;
45006 unsigned int save;
45007 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
45009 strcat (p_name, " distribute");
45010 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
45012 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45014 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
45015 const char *p = IDENTIFIER_POINTER (id);
45016 bool simd = false;
45017 bool parallel = false;
45019 if (strcmp (p, "simd") == 0)
45020 simd = true;
45021 else
45022 parallel = strcmp (p, "parallel") == 0;
45023 if (parallel || simd)
45025 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
45026 if (cclauses == NULL)
45027 cclauses = cclauses_buf;
45028 cp_lexer_consume_token (parser->lexer);
45029 if (!flag_openmp) /* flag_openmp_simd */
45031 if (simd)
45032 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
45033 cclauses, if_p);
45034 else
45035 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
45036 cclauses, if_p);
45038 sb = begin_omp_structured_block ();
45039 save = cp_parser_begin_omp_structured_block (parser);
45040 if (simd)
45041 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
45042 cclauses, if_p);
45043 else
45044 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
45045 cclauses, if_p);
45046 cp_parser_end_omp_structured_block (parser, save);
45047 tree body = finish_omp_structured_block (sb);
45048 if (ret == NULL)
45049 return ret;
45050 ret = make_node (OMP_DISTRIBUTE);
45051 TREE_TYPE (ret) = void_type_node;
45052 OMP_FOR_BODY (ret) = body;
45053 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
45054 SET_EXPR_LOCATION (ret, loc);
45055 add_stmt (ret);
45056 return ret;
45059 if (!flag_openmp) /* flag_openmp_simd */
45061 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45062 return NULL_TREE;
45065 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
45066 cclauses == NULL);
45067 if (cclauses)
45069 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
45070 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
45073 keep_next_level (true);
45074 sb = begin_omp_structured_block ();
45075 save = cp_parser_begin_omp_structured_block (parser);
45077 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
45079 cp_parser_end_omp_structured_block (parser, save);
45080 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
45082 return ret;
45085 /* OpenMP 4.0:
45086 # pragma omp teams teams-clause[optseq] new-line
45087 structured-block */
45089 #define OMP_TEAMS_CLAUSE_MASK \
45090 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
45091 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
45092 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
45093 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
45094 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
45095 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
45096 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
45097 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
45099 static tree
45100 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
45101 char *p_name, omp_clause_mask mask, tree *cclauses,
45102 bool *if_p)
45104 tree clauses, sb, ret;
45105 unsigned int save;
45106 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
45108 strcat (p_name, " teams");
45109 mask |= OMP_TEAMS_CLAUSE_MASK;
45111 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45113 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
45114 const char *p = IDENTIFIER_POINTER (id);
45115 if (strcmp (p, "distribute") == 0)
45117 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
45118 if (cclauses == NULL)
45119 cclauses = cclauses_buf;
45121 cp_lexer_consume_token (parser->lexer);
45122 if (!flag_openmp) /* flag_openmp_simd */
45123 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
45124 cclauses, if_p);
45125 keep_next_level (true);
45126 sb = begin_omp_structured_block ();
45127 save = cp_parser_begin_omp_structured_block (parser);
45128 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
45129 cclauses, if_p);
45130 cp_parser_end_omp_structured_block (parser, save);
45131 tree body = finish_omp_structured_block (sb);
45132 if (ret == NULL)
45133 return ret;
45134 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
45135 ret = make_node (OMP_TEAMS);
45136 TREE_TYPE (ret) = void_type_node;
45137 OMP_TEAMS_CLAUSES (ret) = clauses;
45138 OMP_TEAMS_BODY (ret) = body;
45139 OMP_TEAMS_COMBINED (ret) = 1;
45140 SET_EXPR_LOCATION (ret, loc);
45141 return add_stmt (ret);
45143 else if (strcmp (p, "loop") == 0)
45145 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
45146 if (cclauses == NULL)
45147 cclauses = cclauses_buf;
45149 cp_lexer_consume_token (parser->lexer);
45150 if (!flag_openmp) /* flag_openmp_simd */
45151 return cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
45152 cclauses, if_p);
45153 keep_next_level (true);
45154 sb = begin_omp_structured_block ();
45155 save = cp_parser_begin_omp_structured_block (parser);
45156 ret = cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
45157 cclauses, if_p);
45158 cp_parser_end_omp_structured_block (parser, save);
45159 tree body = finish_omp_structured_block (sb);
45160 if (ret == NULL)
45161 return ret;
45162 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
45163 ret = make_node (OMP_TEAMS);
45164 TREE_TYPE (ret) = void_type_node;
45165 OMP_TEAMS_CLAUSES (ret) = clauses;
45166 OMP_TEAMS_BODY (ret) = body;
45167 OMP_TEAMS_COMBINED (ret) = 1;
45168 SET_EXPR_LOCATION (ret, loc);
45169 return add_stmt (ret);
45172 if (!flag_openmp) /* flag_openmp_simd */
45174 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45175 return NULL_TREE;
45178 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
45179 cclauses == NULL);
45180 if (cclauses)
45182 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
45183 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
45186 tree stmt = make_node (OMP_TEAMS);
45187 TREE_TYPE (stmt) = void_type_node;
45188 OMP_TEAMS_CLAUSES (stmt) = clauses;
45189 keep_next_level (true);
45190 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
45191 SET_EXPR_LOCATION (stmt, loc);
45193 return add_stmt (stmt);
45196 /* OpenMP 4.0:
45197 # pragma omp target data target-data-clause[optseq] new-line
45198 structured-block */
45200 #define OMP_TARGET_DATA_CLAUSE_MASK \
45201 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
45202 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
45203 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
45204 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR) \
45205 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR))
45207 static tree
45208 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
45210 if (flag_openmp)
45211 omp_requires_mask
45212 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
45214 tree clauses
45215 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
45216 "#pragma omp target data", pragma_tok);
45217 c_omp_adjust_map_clauses (clauses, false);
45218 int map_seen = 0;
45219 for (tree *pc = &clauses; *pc;)
45221 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
45222 switch (OMP_CLAUSE_MAP_KIND (*pc))
45224 case GOMP_MAP_TO:
45225 case GOMP_MAP_ALWAYS_TO:
45226 case GOMP_MAP_FROM:
45227 case GOMP_MAP_ALWAYS_FROM:
45228 case GOMP_MAP_TOFROM:
45229 case GOMP_MAP_ALWAYS_TOFROM:
45230 case GOMP_MAP_ALLOC:
45231 map_seen = 3;
45232 break;
45233 case GOMP_MAP_FIRSTPRIVATE_POINTER:
45234 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
45235 case GOMP_MAP_ALWAYS_POINTER:
45236 case GOMP_MAP_ATTACH_DETACH:
45237 break;
45238 default:
45239 map_seen |= 1;
45240 error_at (OMP_CLAUSE_LOCATION (*pc),
45241 "%<#pragma omp target data%> with map-type other "
45242 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
45243 "on %<map%> clause");
45244 *pc = OMP_CLAUSE_CHAIN (*pc);
45245 continue;
45247 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_PTR
45248 || OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_ADDR)
45249 map_seen = 3;
45250 pc = &OMP_CLAUSE_CHAIN (*pc);
45253 if (map_seen != 3)
45255 if (map_seen == 0)
45256 error_at (pragma_tok->location,
45257 "%<#pragma omp target data%> must contain at least "
45258 "one %<map%>, %<use_device_ptr%> or %<use_device_addr%> "
45259 "clause");
45260 return NULL_TREE;
45263 tree stmt = make_node (OMP_TARGET_DATA);
45264 TREE_TYPE (stmt) = void_type_node;
45265 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
45267 keep_next_level (true);
45268 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
45270 SET_EXPR_LOCATION (stmt, pragma_tok->location);
45271 return add_stmt (stmt);
45274 /* OpenMP 4.5:
45275 # pragma omp target enter data target-enter-data-clause[optseq] new-line
45276 structured-block */
45278 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
45279 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
45280 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
45281 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
45282 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
45283 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
45285 static bool
45286 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
45287 enum pragma_context context)
45289 bool data_seen = false;
45290 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45292 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
45293 const char *p = IDENTIFIER_POINTER (id);
45295 if (strcmp (p, "data") == 0)
45297 cp_lexer_consume_token (parser->lexer);
45298 data_seen = true;
45301 if (!data_seen)
45303 cp_parser_error (parser, "expected %<data%>");
45304 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45305 return false;
45308 if (context == pragma_stmt)
45310 error_at (pragma_tok->location,
45311 "%<#pragma %s%> may only be used in compound statements",
45312 "omp target enter data");
45313 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45314 return true;
45317 if (flag_openmp)
45318 omp_requires_mask
45319 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
45321 tree clauses
45322 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
45323 "#pragma omp target enter data", pragma_tok);
45324 c_omp_adjust_map_clauses (clauses, false);
45325 int map_seen = 0;
45326 for (tree *pc = &clauses; *pc;)
45328 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
45329 switch (OMP_CLAUSE_MAP_KIND (*pc))
45331 case GOMP_MAP_TO:
45332 case GOMP_MAP_ALWAYS_TO:
45333 case GOMP_MAP_ALLOC:
45334 map_seen = 3;
45335 break;
45336 case GOMP_MAP_TOFROM:
45337 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_TO);
45338 map_seen = 3;
45339 break;
45340 case GOMP_MAP_ALWAYS_TOFROM:
45341 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_ALWAYS_TO);
45342 map_seen = 3;
45343 break;
45344 case GOMP_MAP_FIRSTPRIVATE_POINTER:
45345 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
45346 case GOMP_MAP_ALWAYS_POINTER:
45347 case GOMP_MAP_ATTACH_DETACH:
45348 break;
45349 default:
45350 map_seen |= 1;
45351 error_at (OMP_CLAUSE_LOCATION (*pc),
45352 "%<#pragma omp target enter data%> with map-type other "
45353 "than %<to%>, %<tofrom%> or %<alloc%> on %<map%> clause");
45354 *pc = OMP_CLAUSE_CHAIN (*pc);
45355 continue;
45357 pc = &OMP_CLAUSE_CHAIN (*pc);
45360 if (map_seen != 3)
45362 if (map_seen == 0)
45363 error_at (pragma_tok->location,
45364 "%<#pragma omp target enter data%> must contain at least "
45365 "one %<map%> clause");
45366 return true;
45369 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
45370 TREE_TYPE (stmt) = void_type_node;
45371 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
45372 SET_EXPR_LOCATION (stmt, pragma_tok->location);
45373 add_stmt (stmt);
45374 return true;
45377 /* OpenMP 4.5:
45378 # pragma omp target exit data target-enter-data-clause[optseq] new-line
45379 structured-block */
45381 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
45382 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
45383 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
45384 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
45385 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
45386 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
45388 static bool
45389 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
45390 enum pragma_context context)
45392 bool data_seen = false;
45393 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45395 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
45396 const char *p = IDENTIFIER_POINTER (id);
45398 if (strcmp (p, "data") == 0)
45400 cp_lexer_consume_token (parser->lexer);
45401 data_seen = true;
45404 if (!data_seen)
45406 cp_parser_error (parser, "expected %<data%>");
45407 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45408 return false;
45411 if (context == pragma_stmt)
45413 error_at (pragma_tok->location,
45414 "%<#pragma %s%> may only be used in compound statements",
45415 "omp target exit data");
45416 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45417 return true;
45420 if (flag_openmp)
45421 omp_requires_mask
45422 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
45424 tree clauses
45425 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
45426 "#pragma omp target exit data", pragma_tok);
45427 c_omp_adjust_map_clauses (clauses, false);
45428 int map_seen = 0;
45429 for (tree *pc = &clauses; *pc;)
45431 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
45432 switch (OMP_CLAUSE_MAP_KIND (*pc))
45434 case GOMP_MAP_FROM:
45435 case GOMP_MAP_ALWAYS_FROM:
45436 case GOMP_MAP_RELEASE:
45437 case GOMP_MAP_DELETE:
45438 map_seen = 3;
45439 break;
45440 case GOMP_MAP_TOFROM:
45441 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_FROM);
45442 map_seen = 3;
45443 break;
45444 case GOMP_MAP_ALWAYS_TOFROM:
45445 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_ALWAYS_FROM);
45446 map_seen = 3;
45447 break;
45448 case GOMP_MAP_FIRSTPRIVATE_POINTER:
45449 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
45450 case GOMP_MAP_ALWAYS_POINTER:
45451 case GOMP_MAP_ATTACH_DETACH:
45452 break;
45453 default:
45454 map_seen |= 1;
45455 error_at (OMP_CLAUSE_LOCATION (*pc),
45456 "%<#pragma omp target exit data%> with map-type other "
45457 "than %<from%>, %<tofrom%>, %<release%> or %<delete%> "
45458 "on %<map%> clause");
45459 *pc = OMP_CLAUSE_CHAIN (*pc);
45460 continue;
45462 pc = &OMP_CLAUSE_CHAIN (*pc);
45465 if (map_seen != 3)
45467 if (map_seen == 0)
45468 error_at (pragma_tok->location,
45469 "%<#pragma omp target exit data%> must contain at least "
45470 "one %<map%> clause");
45471 return true;
45474 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
45475 TREE_TYPE (stmt) = void_type_node;
45476 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
45477 SET_EXPR_LOCATION (stmt, pragma_tok->location);
45478 add_stmt (stmt);
45479 return true;
45482 /* OpenMP 4.0:
45483 # pragma omp target update target-update-clause[optseq] new-line */
45485 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
45486 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
45487 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
45488 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
45489 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
45490 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
45491 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
45493 static bool
45494 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
45495 enum pragma_context context)
45497 if (context == pragma_stmt)
45499 error_at (pragma_tok->location,
45500 "%<#pragma %s%> may only be used in compound statements",
45501 "omp target update");
45502 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45503 return true;
45506 tree clauses
45507 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
45508 "#pragma omp target update", pragma_tok);
45509 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
45510 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
45512 error_at (pragma_tok->location,
45513 "%<#pragma omp target update%> must contain at least one "
45514 "%<from%> or %<to%> clauses");
45515 return true;
45518 if (flag_openmp)
45519 omp_requires_mask
45520 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
45522 tree stmt = make_node (OMP_TARGET_UPDATE);
45523 TREE_TYPE (stmt) = void_type_node;
45524 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
45525 SET_EXPR_LOCATION (stmt, pragma_tok->location);
45526 add_stmt (stmt);
45527 return true;
45530 /* OpenMP 4.0:
45531 # pragma omp target target-clause[optseq] new-line
45532 structured-block */
45534 #define OMP_TARGET_CLAUSE_MASK \
45535 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
45536 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
45537 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
45538 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
45539 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
45540 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
45541 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
45542 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
45543 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
45544 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION) \
45545 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
45546 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR)\
45547 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HAS_DEVICE_ADDR))
45549 static bool
45550 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
45551 enum pragma_context context, bool *if_p)
45553 if (flag_openmp)
45554 omp_requires_mask
45555 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
45557 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45559 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
45560 const char *p = IDENTIFIER_POINTER (id);
45561 enum tree_code ccode = ERROR_MARK;
45563 if (strcmp (p, "teams") == 0)
45564 ccode = OMP_TEAMS;
45565 else if (strcmp (p, "parallel") == 0)
45566 ccode = OMP_PARALLEL;
45567 else if (strcmp (p, "simd") == 0)
45568 ccode = OMP_SIMD;
45569 if (ccode != ERROR_MARK)
45571 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
45572 char p_name[sizeof ("#pragma omp target teams distribute "
45573 "parallel for simd")];
45575 cp_lexer_consume_token (parser->lexer);
45576 strcpy (p_name, "#pragma omp target");
45577 if (!flag_openmp) /* flag_openmp_simd */
45579 tree stmt;
45580 switch (ccode)
45582 case OMP_TEAMS:
45583 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
45584 OMP_TARGET_CLAUSE_MASK,
45585 cclauses, if_p);
45586 break;
45587 case OMP_PARALLEL:
45588 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
45589 OMP_TARGET_CLAUSE_MASK,
45590 cclauses, if_p);
45591 break;
45592 case OMP_SIMD:
45593 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
45594 OMP_TARGET_CLAUSE_MASK,
45595 cclauses, if_p);
45596 break;
45597 default:
45598 gcc_unreachable ();
45600 return stmt != NULL_TREE;
45602 keep_next_level (true);
45603 tree sb = begin_omp_structured_block (), ret;
45604 unsigned save = cp_parser_begin_omp_structured_block (parser);
45605 switch (ccode)
45607 case OMP_TEAMS:
45608 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
45609 OMP_TARGET_CLAUSE_MASK, cclauses,
45610 if_p);
45611 break;
45612 case OMP_PARALLEL:
45613 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
45614 OMP_TARGET_CLAUSE_MASK, cclauses,
45615 if_p);
45616 break;
45617 case OMP_SIMD:
45618 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
45619 OMP_TARGET_CLAUSE_MASK, cclauses,
45620 if_p);
45621 break;
45622 default:
45623 gcc_unreachable ();
45625 cp_parser_end_omp_structured_block (parser, save);
45626 tree body = finish_omp_structured_block (sb);
45627 if (ret == NULL_TREE)
45628 return false;
45629 if (ccode == OMP_TEAMS && !processing_template_decl)
45630 /* For combined target teams, ensure the num_teams and
45631 thread_limit clause expressions are evaluated on the host,
45632 before entering the target construct. */
45633 for (tree c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
45634 c; c = OMP_CLAUSE_CHAIN (c))
45635 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
45636 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
45637 for (int i = 0;
45638 i <= (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS); ++i)
45639 if (OMP_CLAUSE_OPERAND (c, i)
45640 && TREE_CODE (OMP_CLAUSE_OPERAND (c, i)) != INTEGER_CST)
45642 tree expr = OMP_CLAUSE_OPERAND (c, i);
45643 expr = force_target_expr (TREE_TYPE (expr), expr,
45644 tf_none);
45645 if (expr == error_mark_node)
45646 continue;
45647 tree tmp = TARGET_EXPR_SLOT (expr);
45648 add_stmt (expr);
45649 OMP_CLAUSE_OPERAND (c, i) = expr;
45650 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
45651 OMP_CLAUSE_FIRSTPRIVATE);
45652 OMP_CLAUSE_DECL (tc) = tmp;
45653 OMP_CLAUSE_CHAIN (tc)
45654 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
45655 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
45657 c_omp_adjust_map_clauses (cclauses[C_OMP_CLAUSE_SPLIT_TARGET], true);
45658 finish_omp_target (pragma_tok->location,
45659 cclauses[C_OMP_CLAUSE_SPLIT_TARGET], body, true);
45660 return true;
45662 else if (!flag_openmp) /* flag_openmp_simd */
45664 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45665 return false;
45667 else if (strcmp (p, "data") == 0)
45669 cp_lexer_consume_token (parser->lexer);
45670 cp_parser_omp_target_data (parser, pragma_tok, if_p);
45671 return true;
45673 else if (strcmp (p, "enter") == 0)
45675 cp_lexer_consume_token (parser->lexer);
45676 return cp_parser_omp_target_enter_data (parser, pragma_tok, context);
45678 else if (strcmp (p, "exit") == 0)
45680 cp_lexer_consume_token (parser->lexer);
45681 return cp_parser_omp_target_exit_data (parser, pragma_tok, context);
45683 else if (strcmp (p, "update") == 0)
45685 cp_lexer_consume_token (parser->lexer);
45686 return cp_parser_omp_target_update (parser, pragma_tok, context);
45689 if (!flag_openmp) /* flag_openmp_simd */
45691 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45692 return false;
45695 tree clauses = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
45696 "#pragma omp target", pragma_tok,
45697 false);
45698 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
45699 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
45701 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
45702 OMP_CLAUSE_DECL (nc) = OMP_CLAUSE_DECL (c);
45703 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_ALWAYS_TOFROM);
45704 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (c);
45705 OMP_CLAUSE_CHAIN (c) = nc;
45707 clauses = finish_omp_clauses (clauses, C_ORT_OMP_TARGET);
45709 c_omp_adjust_map_clauses (clauses, true);
45710 keep_next_level (true);
45711 tree body = cp_parser_omp_structured_block (parser, if_p);
45713 finish_omp_target (pragma_tok->location, clauses, body, false);
45714 return true;
45717 /* OpenACC 2.0:
45718 # pragma acc cache (variable-list) new-line
45721 static tree
45722 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
45724 /* Don't create location wrapper nodes within 'OMP_CLAUSE__CACHE_'
45725 clauses. */
45726 auto_suppress_location_wrappers sentinel;
45728 tree stmt, clauses;
45730 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
45731 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
45733 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
45735 stmt = make_node (OACC_CACHE);
45736 TREE_TYPE (stmt) = void_type_node;
45737 OACC_CACHE_CLAUSES (stmt) = clauses;
45738 SET_EXPR_LOCATION (stmt, pragma_tok->location);
45739 add_stmt (stmt);
45741 return stmt;
45744 /* OpenACC 2.0:
45745 # pragma acc data oacc-data-clause[optseq] new-line
45746 structured-block */
45748 #define OACC_DATA_CLAUSE_MASK \
45749 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
45750 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
45751 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
45752 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
45753 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
45754 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DETACH) \
45755 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
45756 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
45757 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
45758 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
45760 static tree
45761 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
45763 tree stmt, clauses, block;
45764 unsigned int save;
45766 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
45767 "#pragma acc data", pragma_tok);
45769 block = begin_omp_parallel ();
45770 save = cp_parser_begin_omp_structured_block (parser);
45771 cp_parser_statement (parser, NULL_TREE, false, if_p);
45772 cp_parser_end_omp_structured_block (parser, save);
45773 stmt = finish_oacc_data (clauses, block);
45774 return stmt;
45777 /* OpenACC 2.0:
45778 # pragma acc host_data <clauses> new-line
45779 structured-block */
45781 #define OACC_HOST_DATA_CLAUSE_MASK \
45782 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) \
45783 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
45784 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) )
45786 static tree
45787 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
45789 tree stmt, clauses, block;
45790 unsigned int save;
45792 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
45793 "#pragma acc host_data", pragma_tok);
45795 block = begin_omp_parallel ();
45796 save = cp_parser_begin_omp_structured_block (parser);
45797 cp_parser_statement (parser, NULL_TREE, false, if_p);
45798 cp_parser_end_omp_structured_block (parser, save);
45799 stmt = finish_oacc_host_data (clauses, block);
45800 return stmt;
45803 /* OpenACC 2.0:
45804 # pragma acc declare oacc-data-clause[optseq] new-line
45807 #define OACC_DECLARE_CLAUSE_MASK \
45808 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
45809 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
45810 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
45811 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
45812 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
45813 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
45814 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
45815 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
45817 static tree
45818 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
45820 tree clauses, stmt;
45821 bool error = false;
45822 bool found_in_scope = global_bindings_p ();
45824 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
45825 "#pragma acc declare", pragma_tok, true);
45828 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
45830 error_at (pragma_tok->location,
45831 "no valid clauses specified in %<#pragma acc declare%>");
45832 return NULL_TREE;
45835 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
45837 location_t loc = OMP_CLAUSE_LOCATION (t);
45838 tree decl = OMP_CLAUSE_DECL (t);
45839 if (!DECL_P (decl))
45841 error_at (loc, "array section in %<#pragma acc declare%>");
45842 error = true;
45843 continue;
45845 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
45846 switch (OMP_CLAUSE_MAP_KIND (t))
45848 case GOMP_MAP_FIRSTPRIVATE_POINTER:
45849 case GOMP_MAP_ALLOC:
45850 case GOMP_MAP_TO:
45851 case GOMP_MAP_FORCE_DEVICEPTR:
45852 case GOMP_MAP_DEVICE_RESIDENT:
45853 break;
45855 case GOMP_MAP_LINK:
45856 if (!global_bindings_p ()
45857 && (TREE_STATIC (decl)
45858 || !DECL_EXTERNAL (decl)))
45860 error_at (loc,
45861 "%qD must be a global variable in "
45862 "%<#pragma acc declare link%>",
45863 decl);
45864 error = true;
45865 continue;
45867 break;
45869 default:
45870 if (global_bindings_p ())
45872 error_at (loc, "invalid OpenACC clause at file scope");
45873 error = true;
45874 continue;
45876 if (DECL_EXTERNAL (decl))
45878 error_at (loc,
45879 "invalid use of %<extern%> variable %qD "
45880 "in %<#pragma acc declare%>", decl);
45881 error = true;
45882 continue;
45884 else if (TREE_PUBLIC (decl))
45886 error_at (loc,
45887 "invalid use of %<global%> variable %qD "
45888 "in %<#pragma acc declare%>", decl);
45889 error = true;
45890 continue;
45892 break;
45895 if (!found_in_scope)
45896 /* This seems to ignore the existence of cleanup scopes?
45897 What is the meaning for local extern decls? The local
45898 extern is in this scope, but it is referring to a decl that
45899 is namespace scope. */
45900 for (tree d = current_binding_level->names; d; d = TREE_CHAIN (d))
45901 if (d == decl)
45903 found_in_scope = true;
45904 break;
45906 if (!found_in_scope)
45908 error_at (loc,
45909 "%qD must be a variable declared in the same scope as "
45910 "%<#pragma acc declare%>", decl);
45911 error = true;
45912 continue;
45915 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
45916 || lookup_attribute ("omp declare target link",
45917 DECL_ATTRIBUTES (decl)))
45919 error_at (loc, "variable %qD used more than once with "
45920 "%<#pragma acc declare%>", decl);
45921 error = true;
45922 continue;
45925 if (!error)
45927 tree id;
45929 if (DECL_LOCAL_DECL_P (decl))
45930 /* We need to mark the aliased decl, as that is the entity
45931 that is being referred to. This won't work for
45932 dependent variables, but it didn't work for them before
45933 DECL_LOCAL_DECL_P was a thing either. But then
45934 dependent local extern variable decls are as rare as
45935 hen's teeth. */
45936 if (auto alias = DECL_LOCAL_DECL_ALIAS (decl))
45937 if (alias != error_mark_node)
45938 decl = alias;
45940 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
45941 id = get_identifier ("omp declare target link");
45942 else
45943 id = get_identifier ("omp declare target");
45945 DECL_ATTRIBUTES (decl)
45946 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
45947 if (current_binding_level->kind == sk_namespace)
45949 symtab_node *node = symtab_node::get (decl);
45950 if (node != NULL)
45952 node->offloadable = 1;
45953 if (ENABLE_OFFLOADING)
45955 g->have_offload = true;
45956 if (is_a <varpool_node *> (node))
45957 vec_safe_push (offload_vars, decl);
45964 if (error || current_binding_level->kind == sk_namespace)
45965 return NULL_TREE;
45967 stmt = make_node (OACC_DECLARE);
45968 TREE_TYPE (stmt) = void_type_node;
45969 OACC_DECLARE_CLAUSES (stmt) = clauses;
45970 SET_EXPR_LOCATION (stmt, pragma_tok->location);
45972 add_stmt (stmt);
45974 return NULL_TREE;
45977 /* OpenACC 2.0:
45978 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
45982 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
45984 LOC is the location of the #pragma token.
45987 #define OACC_ENTER_DATA_CLAUSE_MASK \
45988 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
45989 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
45990 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
45991 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
45992 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
45993 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
45995 #define OACC_EXIT_DATA_CLAUSE_MASK \
45996 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
45997 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
45998 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
45999 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
46000 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DETACH) \
46001 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
46002 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
46004 static tree
46005 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
46006 bool enter)
46008 location_t loc = pragma_tok->location;
46009 tree stmt, clauses;
46010 const char *p = "";
46012 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46013 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
46015 if (strcmp (p, "data") != 0)
46017 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
46018 enter ? "enter" : "exit");
46019 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46020 return NULL_TREE;
46023 cp_lexer_consume_token (parser->lexer);
46025 if (enter)
46026 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
46027 "#pragma acc enter data", pragma_tok);
46028 else
46029 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
46030 "#pragma acc exit data", pragma_tok);
46032 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
46034 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
46035 enter ? "enter" : "exit");
46036 return NULL_TREE;
46039 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
46040 TREE_TYPE (stmt) = void_type_node;
46041 OMP_STANDALONE_CLAUSES (stmt) = clauses;
46042 SET_EXPR_LOCATION (stmt, loc);
46043 add_stmt (stmt);
46044 return stmt;
46047 /* OpenACC 2.0:
46048 # pragma acc loop oacc-loop-clause[optseq] new-line
46049 structured-block */
46051 #define OACC_LOOP_CLAUSE_MASK \
46052 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
46053 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
46054 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
46055 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
46056 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
46057 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
46058 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
46059 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
46060 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
46061 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
46063 static tree
46064 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
46065 omp_clause_mask mask, tree *cclauses, bool *if_p)
46067 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
46069 strcat (p_name, " loop");
46070 mask |= OACC_LOOP_CLAUSE_MASK;
46072 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
46073 cclauses == NULL);
46074 if (cclauses)
46076 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
46077 if (*cclauses)
46078 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
46079 if (clauses)
46080 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
46083 tree block = begin_omp_structured_block ();
46084 int save = cp_parser_begin_omp_structured_block (parser);
46085 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
46086 cp_parser_end_omp_structured_block (parser, save);
46087 add_stmt (finish_omp_structured_block (block));
46089 return stmt;
46092 /* OpenACC 2.0:
46093 # pragma acc kernels oacc-kernels-clause[optseq] new-line
46094 structured-block
46098 # pragma acc parallel oacc-parallel-clause[optseq] new-line
46099 structured-block
46101 OpenACC 2.6:
46103 # pragma acc serial oacc-serial-clause[optseq] new-line
46106 #define OACC_KERNELS_CLAUSE_MASK \
46107 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
46108 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
46109 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
46110 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
46111 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
46112 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
46113 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
46114 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
46115 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
46116 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
46117 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
46118 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
46119 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
46120 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
46121 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
46123 #define OACC_PARALLEL_CLAUSE_MASK \
46124 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
46125 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
46126 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
46127 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
46128 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
46129 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
46130 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
46131 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
46132 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
46133 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
46134 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
46135 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
46136 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
46137 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
46138 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
46139 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
46140 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
46141 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
46143 #define OACC_SERIAL_CLAUSE_MASK \
46144 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
46145 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
46146 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
46147 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
46148 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
46149 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
46150 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
46151 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
46152 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
46153 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
46154 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
46155 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
46156 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
46157 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
46158 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
46160 static tree
46161 cp_parser_oacc_compute (cp_parser *parser, cp_token *pragma_tok,
46162 char *p_name, bool *if_p)
46164 omp_clause_mask mask;
46165 enum tree_code code;
46166 switch (cp_parser_pragma_kind (pragma_tok))
46168 case PRAGMA_OACC_KERNELS:
46169 strcat (p_name, " kernels");
46170 mask = OACC_KERNELS_CLAUSE_MASK;
46171 code = OACC_KERNELS;
46172 break;
46173 case PRAGMA_OACC_PARALLEL:
46174 strcat (p_name, " parallel");
46175 mask = OACC_PARALLEL_CLAUSE_MASK;
46176 code = OACC_PARALLEL;
46177 break;
46178 case PRAGMA_OACC_SERIAL:
46179 strcat (p_name, " serial");
46180 mask = OACC_SERIAL_CLAUSE_MASK;
46181 code = OACC_SERIAL;
46182 break;
46183 default:
46184 gcc_unreachable ();
46187 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46189 const char *p
46190 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
46191 if (strcmp (p, "loop") == 0)
46193 cp_lexer_consume_token (parser->lexer);
46194 tree block = begin_omp_parallel ();
46195 tree clauses;
46196 tree stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask,
46197 &clauses, if_p);
46198 protected_set_expr_location (stmt, pragma_tok->location);
46199 return finish_omp_construct (code, block, clauses);
46203 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
46205 tree block = begin_omp_parallel ();
46206 unsigned int save = cp_parser_begin_omp_structured_block (parser);
46207 cp_parser_statement (parser, NULL_TREE, false, if_p);
46208 cp_parser_end_omp_structured_block (parser, save);
46209 return finish_omp_construct (code, block, clauses);
46212 /* OpenACC 2.0:
46213 # pragma acc update oacc-update-clause[optseq] new-line
46216 #define OACC_UPDATE_CLAUSE_MASK \
46217 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
46218 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
46219 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
46220 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
46221 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
46222 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
46224 static tree
46225 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
46227 tree stmt, clauses;
46229 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
46230 "#pragma acc update", pragma_tok);
46232 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
46234 error_at (pragma_tok->location,
46235 "%<#pragma acc update%> must contain at least one "
46236 "%<device%> or %<host%> or %<self%> clause");
46237 return NULL_TREE;
46240 stmt = make_node (OACC_UPDATE);
46241 TREE_TYPE (stmt) = void_type_node;
46242 OACC_UPDATE_CLAUSES (stmt) = clauses;
46243 SET_EXPR_LOCATION (stmt, pragma_tok->location);
46244 add_stmt (stmt);
46245 return stmt;
46248 /* OpenACC 2.0:
46249 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
46251 LOC is the location of the #pragma token.
46254 #define OACC_WAIT_CLAUSE_MASK \
46255 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
46257 static tree
46258 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
46260 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
46261 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
46263 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
46264 list = cp_parser_oacc_wait_list (parser, loc, list);
46266 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
46267 "#pragma acc wait", pragma_tok);
46269 stmt = c_finish_oacc_wait (loc, list, clauses);
46270 stmt = finish_expr_stmt (stmt);
46272 return stmt;
46275 /* OpenMP 4.0:
46276 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
46278 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
46279 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
46280 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
46281 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
46282 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
46283 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
46284 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
46286 static void
46287 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
46288 enum pragma_context context,
46289 bool variant_p)
46291 bool first_p = parser->omp_declare_simd == NULL;
46292 cp_omp_declare_simd_data data;
46293 if (first_p)
46295 data.error_seen = false;
46296 data.fndecl_seen = false;
46297 data.variant_p = variant_p;
46298 data.tokens = vNULL;
46299 data.attribs[0] = NULL;
46300 data.attribs[1] = NULL;
46301 data.loc = UNKNOWN_LOCATION;
46302 /* It is safe to take the address of a local variable; it will only be
46303 used while this scope is live. */
46304 parser->omp_declare_simd = &data;
46306 else if (parser->omp_declare_simd->variant_p != variant_p)
46308 error_at (pragma_tok->location,
46309 "%<#pragma omp declare %s%> followed by "
46310 "%<#pragma omp declare %s%>",
46311 parser->omp_declare_simd->variant_p ? "variant" : "simd",
46312 parser->omp_declare_simd->variant_p ? "simd" : "variant");
46313 parser->omp_declare_simd->error_seen = true;
46316 /* Store away all pragma tokens. */
46317 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
46318 cp_lexer_consume_token (parser->lexer);
46319 cp_parser_require_pragma_eol (parser, pragma_tok);
46320 struct cp_token_cache *cp
46321 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
46322 parser->omp_declare_simd->tokens.safe_push (cp);
46324 if (first_p)
46326 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
46327 cp_parser_pragma (parser, context, NULL);
46328 switch (context)
46330 case pragma_external:
46331 cp_parser_declaration (parser, NULL_TREE);
46332 break;
46333 case pragma_member:
46334 cp_parser_member_declaration (parser);
46335 break;
46336 case pragma_objc_icode:
46337 cp_parser_block_declaration (parser, /*statement_p=*/false);
46338 break;
46339 default:
46340 cp_parser_declaration_statement (parser);
46341 break;
46343 if (parser->omp_declare_simd
46344 && !parser->omp_declare_simd->error_seen
46345 && !parser->omp_declare_simd->fndecl_seen)
46346 error_at (pragma_tok->location,
46347 "%<#pragma omp declare %s%> not immediately followed by "
46348 "function declaration or definition",
46349 parser->omp_declare_simd->variant_p ? "variant" : "simd");
46350 data.tokens.release ();
46351 parser->omp_declare_simd = NULL;
46355 static const char *const omp_construct_selectors[] = {
46356 "simd", "target", "teams", "parallel", "for", NULL };
46357 static const char *const omp_device_selectors[] = {
46358 "kind", "isa", "arch", NULL };
46359 static const char *const omp_implementation_selectors[] = {
46360 "vendor", "extension", "atomic_default_mem_order", "unified_address",
46361 "unified_shared_memory", "dynamic_allocators", "reverse_offload", NULL };
46362 static const char *const omp_user_selectors[] = {
46363 "condition", NULL };
46365 /* OpenMP 5.0:
46367 trait-selector:
46368 trait-selector-name[([trait-score:]trait-property[,trait-property[,...]])]
46370 trait-score:
46371 score(score-expression) */
46373 static tree
46374 cp_parser_omp_context_selector (cp_parser *parser, tree set, bool has_parms_p)
46376 tree ret = NULL_TREE;
46379 tree selector;
46380 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
46381 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46382 selector = cp_lexer_peek_token (parser->lexer)->u.value;
46383 else
46385 cp_parser_error (parser, "expected trait selector name");
46386 return error_mark_node;
46389 tree properties = NULL_TREE;
46390 const char *const *selectors = NULL;
46391 bool allow_score = true;
46392 bool allow_user = false;
46393 int property_limit = 0;
46394 enum { CTX_PROPERTY_NONE, CTX_PROPERTY_USER, CTX_PROPERTY_NAME_LIST,
46395 CTX_PROPERTY_ID, CTX_PROPERTY_EXPR,
46396 CTX_PROPERTY_SIMD } property_kind = CTX_PROPERTY_NONE;
46397 switch (IDENTIFIER_POINTER (set)[0])
46399 case 'c': /* construct */
46400 selectors = omp_construct_selectors;
46401 allow_score = false;
46402 property_limit = 1;
46403 property_kind = CTX_PROPERTY_SIMD;
46404 break;
46405 case 'd': /* device */
46406 selectors = omp_device_selectors;
46407 allow_score = false;
46408 allow_user = true;
46409 property_limit = 3;
46410 property_kind = CTX_PROPERTY_NAME_LIST;
46411 break;
46412 case 'i': /* implementation */
46413 selectors = omp_implementation_selectors;
46414 allow_user = true;
46415 property_limit = 3;
46416 property_kind = CTX_PROPERTY_NAME_LIST;
46417 break;
46418 case 'u': /* user */
46419 selectors = omp_user_selectors;
46420 property_limit = 1;
46421 property_kind = CTX_PROPERTY_EXPR;
46422 break;
46423 default:
46424 gcc_unreachable ();
46426 for (int i = 0; ; i++)
46428 if (selectors[i] == NULL)
46430 if (allow_user)
46432 property_kind = CTX_PROPERTY_USER;
46433 break;
46435 else
46437 error ("selector %qs not allowed for context selector "
46438 "set %qs", IDENTIFIER_POINTER (selector),
46439 IDENTIFIER_POINTER (set));
46440 cp_lexer_consume_token (parser->lexer);
46441 return error_mark_node;
46444 if (i == property_limit)
46445 property_kind = CTX_PROPERTY_NONE;
46446 if (strcmp (selectors[i], IDENTIFIER_POINTER (selector)) == 0)
46447 break;
46449 if (property_kind == CTX_PROPERTY_NAME_LIST
46450 && IDENTIFIER_POINTER (set)[0] == 'i'
46451 && strcmp (IDENTIFIER_POINTER (selector),
46452 "atomic_default_mem_order") == 0)
46453 property_kind = CTX_PROPERTY_ID;
46455 cp_lexer_consume_token (parser->lexer);
46457 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
46459 if (property_kind == CTX_PROPERTY_NONE)
46461 error ("selector %qs does not accept any properties",
46462 IDENTIFIER_POINTER (selector));
46463 return error_mark_node;
46466 matching_parens parens;
46467 parens.consume_open (parser);
46469 cp_token *token = cp_lexer_peek_token (parser->lexer);
46470 if (allow_score
46471 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
46472 && strcmp (IDENTIFIER_POINTER (token->u.value), "score") == 0
46473 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
46475 cp_lexer_save_tokens (parser->lexer);
46476 cp_lexer_consume_token (parser->lexer);
46477 cp_lexer_consume_token (parser->lexer);
46478 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
46479 true)
46480 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
46482 cp_lexer_rollback_tokens (parser->lexer);
46483 cp_lexer_consume_token (parser->lexer);
46485 matching_parens parens2;
46486 parens2.require_open (parser);
46487 tree score = cp_parser_constant_expression (parser);
46488 if (!parens2.require_close (parser))
46489 cp_parser_skip_to_closing_parenthesis (parser, true,
46490 false, true);
46491 cp_parser_require (parser, CPP_COLON, RT_COLON);
46492 if (score != error_mark_node)
46494 score = fold_non_dependent_expr (score);
46495 if (value_dependent_expression_p (score))
46496 properties = tree_cons (get_identifier (" score"),
46497 score, properties);
46498 else if (!INTEGRAL_TYPE_P (TREE_TYPE (score))
46499 || TREE_CODE (score) != INTEGER_CST)
46500 error_at (token->location, "score argument must be "
46501 "constant integer expression");
46502 else if (tree_int_cst_sgn (score) < 0)
46503 error_at (token->location, "score argument must be "
46504 "non-negative");
46505 else
46506 properties = tree_cons (get_identifier (" score"),
46507 score, properties);
46510 else
46511 cp_lexer_rollback_tokens (parser->lexer);
46513 token = cp_lexer_peek_token (parser->lexer);
46516 switch (property_kind)
46518 tree t;
46519 case CTX_PROPERTY_USER:
46522 t = cp_parser_constant_expression (parser);
46523 if (t != error_mark_node)
46525 t = fold_non_dependent_expr (t);
46526 if (TREE_CODE (t) == STRING_CST)
46527 properties = tree_cons (NULL_TREE, t, properties);
46528 else if (!value_dependent_expression_p (t)
46529 && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
46530 || !tree_fits_shwi_p (t)))
46531 error_at (token->location, "property must be "
46532 "constant integer expression or string "
46533 "literal");
46534 else
46535 properties = tree_cons (NULL_TREE, t, properties);
46537 else
46538 return error_mark_node;
46540 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
46541 cp_lexer_consume_token (parser->lexer);
46542 else
46543 break;
46545 while (1);
46546 break;
46547 case CTX_PROPERTY_ID:
46548 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
46549 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46551 tree prop = cp_lexer_peek_token (parser->lexer)->u.value;
46552 cp_lexer_consume_token (parser->lexer);
46553 properties = tree_cons (prop, NULL_TREE, properties);
46555 else
46557 cp_parser_error (parser, "expected identifier");
46558 return error_mark_node;
46560 break;
46561 case CTX_PROPERTY_NAME_LIST:
46564 tree prop = NULL_TREE, value = NULL_TREE;
46565 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
46566 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46568 prop = cp_lexer_peek_token (parser->lexer)->u.value;
46569 cp_lexer_consume_token (parser->lexer);
46571 else if (cp_lexer_next_token_is (parser->lexer, CPP_STRING))
46572 value = cp_parser_string_literal (parser,
46573 /*translate=*/false,
46574 /*wide_ok=*/false);
46575 else
46577 cp_parser_error (parser, "expected identifier or "
46578 "string literal");
46579 return error_mark_node;
46582 properties = tree_cons (prop, value, properties);
46584 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
46585 cp_lexer_consume_token (parser->lexer);
46586 else
46587 break;
46589 while (1);
46590 break;
46591 case CTX_PROPERTY_EXPR:
46592 t = cp_parser_constant_expression (parser);
46593 if (t != error_mark_node)
46595 t = fold_non_dependent_expr (t);
46596 if (!value_dependent_expression_p (t)
46597 && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
46598 || !tree_fits_shwi_p (t)))
46599 error_at (token->location, "property must be "
46600 "constant integer expression");
46601 else
46602 properties = tree_cons (NULL_TREE, t, properties);
46604 else
46605 return error_mark_node;
46606 break;
46607 case CTX_PROPERTY_SIMD:
46608 if (!has_parms_p)
46610 error_at (token->location, "properties for %<simd%> "
46611 "selector may not be specified in "
46612 "%<metadirective%>");
46613 return error_mark_node;
46615 properties
46616 = cp_parser_omp_all_clauses (parser,
46617 OMP_DECLARE_SIMD_CLAUSE_MASK,
46618 "simd", NULL, true, 2);
46619 break;
46620 default:
46621 gcc_unreachable ();
46624 if (!parens.require_close (parser))
46625 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
46627 properties = nreverse (properties);
46629 else if (property_kind == CTX_PROPERTY_NAME_LIST
46630 || property_kind == CTX_PROPERTY_ID
46631 || property_kind == CTX_PROPERTY_EXPR)
46633 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
46634 return error_mark_node;
46637 ret = tree_cons (selector, properties, ret);
46639 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
46640 cp_lexer_consume_token (parser->lexer);
46641 else
46642 break;
46644 while (1);
46646 return nreverse (ret);
46649 /* OpenMP 5.0:
46651 trait-set-selector[,trait-set-selector[,...]]
46653 trait-set-selector:
46654 trait-set-selector-name = { trait-selector[, trait-selector[, ...]] }
46656 trait-set-selector-name:
46657 constructor
46658 device
46659 implementation
46660 user */
46662 static tree
46663 cp_parser_omp_context_selector_specification (cp_parser *parser,
46664 bool has_parms_p)
46666 tree ret = NULL_TREE;
46669 const char *setp = "";
46670 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46671 setp
46672 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
46673 switch (setp[0])
46675 case 'c':
46676 if (strcmp (setp, "construct") == 0)
46677 setp = NULL;
46678 break;
46679 case 'd':
46680 if (strcmp (setp, "device") == 0)
46681 setp = NULL;
46682 break;
46683 case 'i':
46684 if (strcmp (setp, "implementation") == 0)
46685 setp = NULL;
46686 break;
46687 case 'u':
46688 if (strcmp (setp, "user") == 0)
46689 setp = NULL;
46690 break;
46691 default:
46692 break;
46694 if (setp)
46696 cp_parser_error (parser, "expected %<construct%>, %<device%>, "
46697 "%<implementation%> or %<user%>");
46698 return error_mark_node;
46701 tree set = cp_lexer_peek_token (parser->lexer)->u.value;
46702 cp_lexer_consume_token (parser->lexer);
46704 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
46705 return error_mark_node;
46707 matching_braces braces;
46708 if (!braces.require_open (parser))
46709 return error_mark_node;
46711 tree selectors
46712 = cp_parser_omp_context_selector (parser, set, has_parms_p);
46713 if (selectors == error_mark_node)
46715 cp_parser_skip_to_closing_brace (parser);
46716 ret = error_mark_node;
46718 else if (ret != error_mark_node)
46719 ret = tree_cons (set, selectors, ret);
46721 braces.require_close (parser);
46723 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
46724 cp_lexer_consume_token (parser->lexer);
46725 else
46726 break;
46728 while (1);
46730 if (ret == error_mark_node)
46731 return ret;
46732 return nreverse (ret);
46735 /* Assumption clauses:
46736 OpenMP 5.1
46737 absent (directive-name-list)
46738 contains (directive-name-list)
46739 holds (expression)
46740 no_openmp
46741 no_openmp_routines
46742 no_parallelism */
46744 static void
46745 cp_parser_omp_assumption_clauses (cp_parser *parser, cp_token *pragma_tok,
46746 bool is_assume)
46748 bool no_openmp = false;
46749 bool no_openmp_routines = false;
46750 bool no_parallelism = false;
46751 bitmap_head absent_head, contains_head;
46753 bitmap_obstack_initialize (NULL);
46754 bitmap_initialize (&absent_head, &bitmap_default_obstack);
46755 bitmap_initialize (&contains_head, &bitmap_default_obstack);
46757 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
46758 error_at (cp_lexer_peek_token (parser->lexer)->location,
46759 "expected at least one assumption clause");
46761 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
46763 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
46764 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
46765 cp_lexer_consume_token (parser->lexer);
46767 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46768 break;
46770 const char *p
46771 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
46772 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
46774 if (!strcmp (p, "no_openmp"))
46776 cp_lexer_consume_token (parser->lexer);
46777 if (no_openmp)
46778 error_at (cloc, "too many %qs clauses", "no_openmp");
46779 no_openmp = true;
46781 else if (!strcmp (p, "no_openmp_routines"))
46783 cp_lexer_consume_token (parser->lexer);
46784 if (no_openmp_routines)
46785 error_at (cloc, "too many %qs clauses", "no_openmp_routines");
46786 no_openmp_routines = true;
46788 else if (!strcmp (p, "no_parallelism"))
46790 cp_lexer_consume_token (parser->lexer);
46791 if (no_parallelism)
46792 error_at (cloc, "too many %qs clauses", "no_parallelism");
46793 no_parallelism = true;
46795 else if (!strcmp (p, "holds"))
46797 cp_lexer_consume_token (parser->lexer);
46798 matching_parens parens;
46799 if (parens.require_open (parser))
46801 location_t eloc = cp_lexer_peek_token (parser->lexer)->location;
46802 tree t = cp_parser_assignment_expression (parser);
46803 if (!type_dependent_expression_p (t))
46804 t = contextual_conv_bool (t, tf_warning_or_error);
46805 if (is_assume && !error_operand_p (t))
46806 finish_expr_stmt (build_assume_call (eloc, t));
46807 if (!parens.require_close (parser))
46808 cp_parser_skip_to_closing_parenthesis (parser,
46809 /*recovering=*/true,
46810 /*or_comma=*/false,
46811 /*consume_paren=*/true);
46814 else if (!strcmp (p, "absent") || !strcmp (p, "contains"))
46816 cp_lexer_consume_token (parser->lexer);
46817 matching_parens parens;
46818 if (parens.require_open (parser))
46822 const char *directive[3] = {};
46823 int i;
46824 location_t dloc
46825 = cp_lexer_peek_token (parser->lexer)->location;
46826 for (i = 0; i < 3; i++)
46828 tree id;
46829 if (cp_lexer_nth_token_is (parser->lexer, i + 1, CPP_NAME))
46830 id = cp_lexer_peek_nth_token (parser->lexer,
46831 i + 1)->u.value;
46832 else if (cp_lexer_nth_token_is (parser->lexer, i + 1,
46833 CPP_KEYWORD))
46835 enum rid rid
46836 = cp_lexer_peek_nth_token (parser->lexer,
46837 i + 1)->keyword;
46838 id = ridpointers[rid];
46840 else
46841 break;
46842 directive[i] = IDENTIFIER_POINTER (id);
46844 if (i == 0)
46845 error_at (dloc, "expected directive name");
46846 else
46848 const struct c_omp_directive *dir
46849 = c_omp_categorize_directive (directive[0],
46850 directive[1],
46851 directive[2]);
46852 if (dir == NULL
46853 || dir->kind == C_OMP_DIR_DECLARATIVE
46854 || dir->kind == C_OMP_DIR_INFORMATIONAL
46855 || dir->id == PRAGMA_OMP_END
46856 || (!dir->second && directive[1])
46857 || (!dir->third && directive[2]))
46858 error_at (dloc, "unknown OpenMP directive name in "
46859 "%qs clause argument", p);
46860 else
46862 int id = dir - c_omp_directives;
46863 if (bitmap_bit_p (p[0] == 'a' ? &contains_head
46864 : &absent_head, id))
46865 error_at (dloc, "%<%s%s%s%s%s%> directive "
46866 "mentioned in both %<absent%> and "
46867 "%<contains%> clauses",
46868 directive[0],
46869 directive[1] ? " " : "",
46870 directive[1] ? directive[1] : "",
46871 directive[2] ? " " : "",
46872 directive[2] ? directive[2] : "");
46873 else if (!bitmap_set_bit (p[0] == 'a'
46874 ? &absent_head
46875 : &contains_head, id))
46876 error_at (dloc, "%<%s%s%s%s%s%> directive "
46877 "mentioned multiple times in %qs "
46878 "clauses",
46879 directive[0],
46880 directive[1] ? " " : "",
46881 directive[1] ? directive[1] : "",
46882 directive[2] ? " " : "",
46883 directive[2] ? directive[2] : "", p);
46885 for (; i; --i)
46886 cp_lexer_consume_token (parser->lexer);
46888 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
46889 cp_lexer_consume_token (parser->lexer);
46890 else
46891 break;
46893 while (1);
46894 if (!parens.require_close (parser))
46895 cp_parser_skip_to_closing_parenthesis (parser,
46896 /*recovering=*/true,
46897 /*or_comma=*/false,
46898 /*consume_paren=*/true);
46901 else if (startswith (p, "ext_"))
46903 warning_at (cloc, 0, "unknown assumption clause %qs", p);
46904 cp_lexer_consume_token (parser->lexer);
46905 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
46906 for (size_t n = cp_parser_skip_balanced_tokens (parser, 1) - 1;
46907 n; --n)
46908 cp_lexer_consume_token (parser->lexer);
46910 else
46912 cp_lexer_consume_token (parser->lexer);
46913 error_at (cloc, "expected assumption clause");
46914 break;
46917 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46920 /* OpenMP 5.1
46921 # pragma omp assume clauses[optseq] new-line */
46923 static void
46924 cp_parser_omp_assume (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
46926 cp_parser_omp_assumption_clauses (parser, pragma_tok, true);
46927 add_stmt (cp_parser_omp_structured_block (parser, if_p));
46930 /* OpenMP 5.1
46931 # pragma omp assumes clauses[optseq] new-line */
46933 static bool
46934 cp_parser_omp_assumes (cp_parser *parser, cp_token *pragma_tok)
46936 cp_parser_omp_assumption_clauses (parser, pragma_tok, false);
46937 return false;
46940 /* Finalize #pragma omp declare variant after a fndecl has been parsed, and put
46941 that into "omp declare variant base" attribute. */
46943 static tree
46944 cp_finish_omp_declare_variant (cp_parser *parser, cp_token *pragma_tok,
46945 tree attrs)
46947 matching_parens parens;
46948 if (!parens.require_open (parser))
46950 fail:
46951 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46952 return attrs;
46955 bool template_p;
46956 cp_id_kind idk = CP_ID_KIND_NONE;
46957 cp_token *varid_token = cp_lexer_peek_token (parser->lexer);
46958 cp_expr varid
46959 = cp_parser_id_expression (parser, /*template_keyword_p=*/false,
46960 /*check_dependency_p=*/true,
46961 /*template_p=*/&template_p,
46962 /*declarator_p=*/false,
46963 /*optional_p=*/false);
46964 parens.require_close (parser);
46966 tree variant;
46967 if (TREE_CODE (varid) == TEMPLATE_ID_EXPR
46968 || TREE_CODE (varid) == TYPE_DECL
46969 || varid == error_mark_node)
46970 variant = varid;
46971 else if (varid_token->type == CPP_NAME && varid_token->error_reported)
46972 variant = NULL_TREE;
46973 else
46975 tree ambiguous_decls;
46976 variant = cp_parser_lookup_name (parser, varid, none_type,
46977 template_p, /*is_namespace=*/false,
46978 /*check_dependency=*/true,
46979 &ambiguous_decls,
46980 varid.get_location ());
46981 if (ambiguous_decls)
46982 variant = NULL_TREE;
46984 if (variant == NULL_TREE)
46985 variant = error_mark_node;
46986 else if (TREE_CODE (variant) != SCOPE_REF)
46988 const char *error_msg;
46989 variant
46990 = finish_id_expression (varid, variant, parser->scope,
46991 &idk, false, true,
46992 &parser->non_integral_constant_expression_p,
46993 template_p, true, false, false, &error_msg,
46994 varid.get_location ());
46995 if (error_msg)
46996 cp_parser_error (parser, error_msg);
46998 location_t caret_loc = get_pure_location (varid.get_location ());
46999 location_t start_loc = get_start (varid_token->location);
47000 location_t finish_loc = get_finish (varid.get_location ());
47001 location_t varid_loc = make_location (caret_loc, start_loc, finish_loc);
47003 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
47004 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
47005 cp_lexer_consume_token (parser->lexer);
47007 const char *clause = "";
47008 location_t match_loc = cp_lexer_peek_token (parser->lexer)->location;
47009 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47010 clause = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
47011 if (strcmp (clause, "match"))
47013 cp_parser_error (parser, "expected %<match%>");
47014 goto fail;
47017 cp_lexer_consume_token (parser->lexer);
47019 if (!parens.require_open (parser))
47020 goto fail;
47022 tree ctx = cp_parser_omp_context_selector_specification (parser, true);
47023 if (ctx == error_mark_node)
47024 goto fail;
47025 ctx = omp_check_context_selector (match_loc, ctx);
47026 if (ctx != error_mark_node && variant != error_mark_node)
47028 tree match_loc_node = maybe_wrap_with_location (integer_zero_node,
47029 match_loc);
47030 tree loc_node = maybe_wrap_with_location (integer_zero_node, varid_loc);
47031 loc_node = tree_cons (match_loc_node,
47032 build_int_cst (integer_type_node, idk),
47033 build_tree_list (loc_node, integer_zero_node));
47034 attrs = tree_cons (get_identifier ("omp declare variant base"),
47035 tree_cons (variant, ctx, loc_node), attrs);
47036 if (processing_template_decl)
47037 ATTR_IS_DEPENDENT (attrs) = 1;
47040 parens.require_close (parser);
47041 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47042 return attrs;
47046 /* Finalize #pragma omp declare simd clauses after direct declarator has
47047 been parsed, and put that into "omp declare simd" attribute. */
47049 static tree
47050 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
47052 struct cp_token_cache *ce;
47053 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
47054 int i;
47056 if (!data->error_seen && data->fndecl_seen)
47058 error ("%<#pragma omp declare %s%> not immediately followed by "
47059 "a single function declaration or definition",
47060 data->variant_p ? "variant" : "simd");
47061 data->error_seen = true;
47063 if (data->error_seen)
47064 return attrs;
47066 FOR_EACH_VEC_ELT (data->tokens, i, ce)
47068 tree c, cl;
47070 cp_parser_push_lexer_for_tokens (parser, ce);
47071 parser->lexer->in_pragma = true;
47072 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
47073 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
47074 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
47075 const char *kind = IDENTIFIER_POINTER (id);
47076 cp_lexer_consume_token (parser->lexer);
47077 if (strcmp (kind, "simd") == 0)
47079 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
47080 "#pragma omp declare simd",
47081 pragma_tok);
47082 if (cl)
47083 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
47084 c = build_tree_list (get_identifier ("omp declare simd"), cl);
47085 TREE_CHAIN (c) = attrs;
47086 if (processing_template_decl)
47087 ATTR_IS_DEPENDENT (c) = 1;
47088 attrs = c;
47090 else
47092 gcc_assert (strcmp (kind, "variant") == 0);
47093 attrs
47094 = cp_finish_omp_declare_variant (parser, pragma_tok, attrs);
47096 cp_parser_pop_lexer (parser);
47099 cp_lexer *lexer = NULL;
47100 for (int i = 0; i < 2; i++)
47102 if (data->attribs[i] == NULL)
47103 continue;
47104 for (tree *pa = data->attribs[i]; *pa; )
47105 if (get_attribute_namespace (*pa) == omp_identifier
47106 && is_attribute_p ("directive", get_attribute_name (*pa)))
47108 for (tree a = TREE_VALUE (*pa); a; a = TREE_CHAIN (a))
47110 tree d = TREE_VALUE (a);
47111 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
47112 cp_token *first = DEFPARSE_TOKENS (d)->first;
47113 cp_token *last = DEFPARSE_TOKENS (d)->last;
47114 const char *directive[3] = {};
47115 for (int j = 0; j < 3; j++)
47117 tree id = NULL_TREE;
47118 if (first + j == last)
47119 break;
47120 if (first[j].type == CPP_NAME)
47121 id = first[j].u.value;
47122 else if (first[j].type == CPP_KEYWORD)
47123 id = ridpointers[(int) first[j].keyword];
47124 else
47125 break;
47126 directive[j] = IDENTIFIER_POINTER (id);
47128 const c_omp_directive *dir = NULL;
47129 if (directive[0])
47130 dir = c_omp_categorize_directive (directive[0], directive[1],
47131 directive[2]);
47132 if (dir == NULL)
47134 error_at (first->location,
47135 "unknown OpenMP directive name in "
47136 "%<omp::directive%> attribute argument");
47137 continue;
47139 if (dir->id != PRAGMA_OMP_DECLARE
47140 || (strcmp (directive[1], "simd") != 0
47141 && strcmp (directive[1], "variant") != 0))
47143 error_at (first->location,
47144 "OpenMP directive other than %<declare simd%> "
47145 "or %<declare variant%> appertains to a "
47146 "declaration");
47147 continue;
47150 if (parser->omp_attrs_forbidden_p)
47152 error_at (first->location,
47153 "mixing OpenMP directives with attribute and "
47154 "pragma syntax on the same statement");
47155 parser->omp_attrs_forbidden_p = false;
47158 if (!flag_openmp && strcmp (directive[1], "simd") != 0)
47159 continue;
47160 if (lexer == NULL)
47162 lexer = cp_lexer_alloc ();
47163 lexer->debugging_p = parser->lexer->debugging_p;
47165 vec_safe_reserve (lexer->buffer, (last - first) + 2);
47166 cp_token tok = {};
47167 tok.type = CPP_PRAGMA;
47168 tok.keyword = RID_MAX;
47169 tok.u.value = build_int_cst (NULL, PRAGMA_OMP_DECLARE);
47170 tok.location = first->location;
47171 lexer->buffer->quick_push (tok);
47172 while (++first < last)
47173 lexer->buffer->quick_push (*first);
47174 tok = {};
47175 tok.type = CPP_PRAGMA_EOL;
47176 tok.keyword = RID_MAX;
47177 tok.location = last->location;
47178 lexer->buffer->quick_push (tok);
47179 tok = {};
47180 tok.type = CPP_EOF;
47181 tok.keyword = RID_MAX;
47182 tok.location = last->location;
47183 lexer->buffer->quick_push (tok);
47184 lexer->next = parser->lexer;
47185 lexer->next_token = lexer->buffer->address ();
47186 lexer->last_token = lexer->next_token
47187 + lexer->buffer->length ()
47188 - 1;
47189 lexer->in_omp_attribute_pragma = true;
47190 parser->lexer = lexer;
47191 /* Move the current source position to that of the first token
47192 in the new lexer. */
47193 cp_lexer_set_source_position_from_token (lexer->next_token);
47195 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
47196 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
47197 const char *kind = IDENTIFIER_POINTER (id);
47198 cp_lexer_consume_token (parser->lexer);
47200 tree c, cl;
47201 if (strcmp (kind, "simd") == 0)
47203 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
47204 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
47205 cp_lexer_consume_token (parser->lexer);
47207 omp_clause_mask mask = OMP_DECLARE_SIMD_CLAUSE_MASK;
47208 cl = cp_parser_omp_all_clauses (parser, mask,
47209 "#pragma omp declare simd",
47210 pragma_tok);
47211 if (cl)
47212 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
47213 c = build_tree_list (get_identifier ("omp declare simd"),
47214 cl);
47215 TREE_CHAIN (c) = attrs;
47216 if (processing_template_decl)
47217 ATTR_IS_DEPENDENT (c) = 1;
47218 attrs = c;
47220 else
47222 gcc_assert (strcmp (kind, "variant") == 0);
47223 attrs
47224 = cp_finish_omp_declare_variant (parser, pragma_tok,
47225 attrs);
47227 gcc_assert (parser->lexer != lexer);
47228 vec_safe_truncate (lexer->buffer, 0);
47230 *pa = TREE_CHAIN (*pa);
47232 else
47233 pa = &TREE_CHAIN (*pa);
47235 if (lexer)
47236 cp_lexer_destroy (lexer);
47238 data->fndecl_seen = true;
47239 return attrs;
47242 /* Helper for cp_parser_omp_declare_target, handle one to or link clause
47243 on #pragma omp declare target. Return false if errors were reported. */
47245 static bool
47246 handle_omp_declare_target_clause (tree c, tree t, int device_type)
47248 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
47249 tree at2 = lookup_attribute ("omp declare target link", DECL_ATTRIBUTES (t));
47250 tree id;
47251 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
47253 id = get_identifier ("omp declare target link");
47254 std::swap (at1, at2);
47256 else
47257 id = get_identifier ("omp declare target");
47258 if (at2)
47260 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
47261 error_at (OMP_CLAUSE_LOCATION (c),
47262 "%qD specified both in declare target %<link%> and %qs"
47263 " clauses", t, OMP_CLAUSE_ENTER_TO (c) ? "to" : "enter");
47264 else
47265 error_at (OMP_CLAUSE_LOCATION (c),
47266 "%qD specified both in declare target %<link%> and "
47267 "%<to%> or %<enter%> clauses", t);
47268 return false;
47270 if (!at1)
47272 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
47273 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
47274 return true;
47276 symtab_node *node = symtab_node::get (t);
47277 if (node != NULL)
47279 node->offloadable = 1;
47280 if (ENABLE_OFFLOADING)
47282 g->have_offload = true;
47283 if (is_a <varpool_node *> (node))
47284 vec_safe_push (offload_vars, t);
47288 if (TREE_CODE (t) != FUNCTION_DECL)
47289 return true;
47290 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_HOST) != 0)
47292 tree at3 = lookup_attribute ("omp declare target host",
47293 DECL_ATTRIBUTES (t));
47294 if (at3 == NULL_TREE)
47296 id = get_identifier ("omp declare target host");
47297 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
47300 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_NOHOST) != 0)
47302 tree at3 = lookup_attribute ("omp declare target nohost",
47303 DECL_ATTRIBUTES (t));
47304 if (at3 == NULL_TREE)
47306 id = get_identifier ("omp declare target nohost");
47307 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
47310 return true;
47313 /* OpenMP 4.0:
47314 # pragma omp declare target new-line
47315 declarations and definitions
47316 # pragma omp end declare target new-line
47318 OpenMP 4.5:
47319 # pragma omp declare target ( extended-list ) new-line
47321 # pragma omp declare target declare-target-clauses[seq] new-line */
47323 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
47324 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
47325 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ENTER) \
47326 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK) \
47327 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE_TYPE))
47329 static void
47330 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
47332 tree clauses = NULL_TREE;
47333 int device_type = 0;
47334 bool only_device_type = true;
47335 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
47336 || (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
47337 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME)))
47338 clauses
47339 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
47340 "#pragma omp declare target", pragma_tok);
47341 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
47343 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_ENTER,
47344 clauses);
47345 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
47346 cp_parser_require_pragma_eol (parser, pragma_tok);
47348 else
47350 cp_omp_declare_target_attr a
47351 = { parser->lexer->in_omp_attribute_pragma, -1 };
47352 vec_safe_push (scope_chain->omp_declare_target_attribute, a);
47353 cp_parser_require_pragma_eol (parser, pragma_tok);
47354 return;
47356 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
47357 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE)
47358 device_type |= OMP_CLAUSE_DEVICE_TYPE_KIND (c);
47359 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
47361 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE)
47362 continue;
47363 tree t = OMP_CLAUSE_DECL (c);
47364 only_device_type = false;
47365 if (!handle_omp_declare_target_clause (c, t, device_type))
47366 continue;
47367 if (VAR_OR_FUNCTION_DECL_P (t)
47368 && DECL_LOCAL_DECL_P (t)
47369 && DECL_LANG_SPECIFIC (t)
47370 && DECL_LOCAL_DECL_ALIAS (t)
47371 && DECL_LOCAL_DECL_ALIAS (t) != error_mark_node)
47372 handle_omp_declare_target_clause (c, DECL_LOCAL_DECL_ALIAS (t),
47373 device_type);
47375 if (device_type && only_device_type)
47376 error_at (OMP_CLAUSE_LOCATION (clauses),
47377 "directive with only %<device_type%> clause");
47380 /* OpenMP 5.1
47381 # pragma omp begin assumes clauses[optseq] new-line
47383 # pragma omp begin declare target clauses[optseq] new-line */
47385 #define OMP_BEGIN_DECLARE_TARGET_CLAUSE_MASK \
47386 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE_TYPE)
47388 static void
47389 cp_parser_omp_begin (cp_parser *parser, cp_token *pragma_tok)
47391 const char *p = "";
47392 bool in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
47393 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47395 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
47396 p = IDENTIFIER_POINTER (id);
47398 if (strcmp (p, "declare") == 0)
47400 cp_lexer_consume_token (parser->lexer);
47401 p = "";
47402 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47404 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
47405 p = IDENTIFIER_POINTER (id);
47407 if (strcmp (p, "target") == 0)
47409 cp_lexer_consume_token (parser->lexer);
47410 tree clauses
47411 = cp_parser_omp_all_clauses (parser,
47412 OMP_BEGIN_DECLARE_TARGET_CLAUSE_MASK,
47413 "#pragma omp begin declare target",
47414 pragma_tok);
47415 int device_type = 0;
47416 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
47417 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE)
47418 device_type |= OMP_CLAUSE_DEVICE_TYPE_KIND (c);
47419 cp_omp_declare_target_attr a
47420 = { in_omp_attribute_pragma, device_type };
47421 vec_safe_push (scope_chain->omp_declare_target_attribute, a);
47423 else
47425 cp_parser_error (parser, "expected %<target%>");
47426 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47429 else if (strcmp (p, "assumes") == 0)
47431 cp_lexer_consume_token (parser->lexer);
47432 cp_parser_omp_assumption_clauses (parser, pragma_tok, false);
47433 cp_omp_begin_assumes_data a = { in_omp_attribute_pragma };
47434 vec_safe_push (scope_chain->omp_begin_assumes, a);
47436 else
47438 cp_parser_error (parser, "expected %<declare target%> or %<assumes%>");
47439 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47443 /* OpenMP 4.0:
47444 # pragma omp end declare target new-line
47446 OpenMP 5.1:
47447 # pragma omp end assumes new-line */
47449 static void
47450 cp_parser_omp_end (cp_parser *parser, cp_token *pragma_tok)
47452 const char *p = "";
47453 bool in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
47454 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47456 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
47457 p = IDENTIFIER_POINTER (id);
47459 if (strcmp (p, "declare") == 0)
47461 cp_lexer_consume_token (parser->lexer);
47462 p = "";
47463 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47465 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
47466 p = IDENTIFIER_POINTER (id);
47468 if (strcmp (p, "target") == 0)
47469 cp_lexer_consume_token (parser->lexer);
47470 else
47472 cp_parser_error (parser, "expected %<target%>");
47473 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47474 return;
47476 cp_parser_require_pragma_eol (parser, pragma_tok);
47477 if (!vec_safe_length (scope_chain->omp_declare_target_attribute))
47478 error_at (pragma_tok->location,
47479 "%<#pragma omp end declare target%> without corresponding "
47480 "%<#pragma omp declare target%> or "
47481 "%<#pragma omp begin declare target%>");
47482 else
47484 cp_omp_declare_target_attr
47485 a = scope_chain->omp_declare_target_attribute->pop ();
47486 if (a.attr_syntax != in_omp_attribute_pragma)
47488 if (a.attr_syntax)
47489 error_at (pragma_tok->location,
47490 "%qs in attribute syntax terminated "
47491 "with %qs in pragma syntax",
47492 a.device_type >= 0 ? "begin declare target"
47493 : "declare target",
47494 "end declare target");
47495 else
47496 error_at (pragma_tok->location,
47497 "%qs in pragma syntax terminated "
47498 "with %qs in attribute syntax",
47499 a.device_type >= 0 ? "begin declare target"
47500 : "declare target",
47501 "end declare target");
47505 else if (strcmp (p, "assumes") == 0)
47507 cp_lexer_consume_token (parser->lexer);
47508 cp_parser_require_pragma_eol (parser, pragma_tok);
47509 if (!vec_safe_length (scope_chain->omp_begin_assumes))
47510 error_at (pragma_tok->location,
47511 "%qs without corresponding %qs",
47512 "#pragma omp end assumes", "#pragma omp begin assumes");
47513 else
47515 cp_omp_begin_assumes_data
47516 a = scope_chain->omp_begin_assumes->pop ();
47517 if (a.attr_syntax != in_omp_attribute_pragma)
47519 if (a.attr_syntax)
47520 error_at (pragma_tok->location,
47521 "%qs in attribute syntax terminated "
47522 "with %qs in pragma syntax",
47523 "begin assumes", "end assumes");
47524 else
47525 error_at (pragma_tok->location,
47526 "%qs in pragma syntax terminated "
47527 "with %qs in attribute syntax",
47528 "begin assumes", "end assumes");
47532 else
47534 cp_parser_error (parser, "expected %<declare%> or %<assumes%>");
47535 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47536 return;
47540 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
47541 expression and optional initializer clause of
47542 #pragma omp declare reduction. We store the expression(s) as
47543 either 3, 6 or 7 special statements inside of the artificial function's
47544 body. The first two statements are DECL_EXPRs for the artificial
47545 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
47546 expression that uses those variables.
47547 If there was any INITIALIZER clause, this is followed by further statements,
47548 the fourth and fifth statements are DECL_EXPRs for the artificial
47549 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
47550 constructor variant (first token after open paren is not omp_priv),
47551 then the sixth statement is a statement with the function call expression
47552 that uses the OMP_PRIV and optionally OMP_ORIG variable.
47553 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
47554 to initialize the OMP_PRIV artificial variable and there is seventh
47555 statement, a DECL_EXPR of the OMP_PRIV statement again. */
47557 static bool
47558 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
47560 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
47561 gcc_assert (TYPE_REF_P (type));
47562 type = TREE_TYPE (type);
47563 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
47564 DECL_ARTIFICIAL (omp_out) = 1;
47565 pushdecl (omp_out);
47566 add_decl_expr (omp_out);
47567 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
47568 DECL_ARTIFICIAL (omp_in) = 1;
47569 pushdecl (omp_in);
47570 add_decl_expr (omp_in);
47571 tree combiner;
47572 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
47574 keep_next_level (true);
47575 tree block = begin_omp_structured_block ();
47576 combiner = cp_parser_expression (parser);
47577 finish_expr_stmt (combiner);
47578 block = finish_omp_structured_block (block);
47579 if (processing_template_decl)
47580 block = build_stmt (input_location, EXPR_STMT, block);
47581 add_stmt (block);
47583 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
47584 return false;
47586 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
47587 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
47588 cp_lexer_consume_token (parser->lexer);
47590 const char *p = "";
47591 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47593 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
47594 p = IDENTIFIER_POINTER (id);
47597 if (strcmp (p, "initializer") == 0)
47599 cp_lexer_consume_token (parser->lexer);
47600 matching_parens parens;
47601 if (!parens.require_open (parser))
47602 return false;
47604 p = "";
47605 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47607 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
47608 p = IDENTIFIER_POINTER (id);
47611 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
47612 DECL_ARTIFICIAL (omp_priv) = 1;
47613 pushdecl (omp_priv);
47614 add_decl_expr (omp_priv);
47615 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
47616 DECL_ARTIFICIAL (omp_orig) = 1;
47617 pushdecl (omp_orig);
47618 add_decl_expr (omp_orig);
47620 keep_next_level (true);
47621 block = begin_omp_structured_block ();
47623 bool ctor = false;
47624 if (strcmp (p, "omp_priv") == 0)
47626 bool is_direct_init, is_non_constant_init;
47627 ctor = true;
47628 cp_lexer_consume_token (parser->lexer);
47629 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
47630 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
47631 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
47632 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
47633 == CPP_CLOSE_PAREN
47634 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
47635 == CPP_CLOSE_PAREN))
47637 finish_omp_structured_block (block);
47638 error ("invalid initializer clause");
47639 return false;
47641 initializer = cp_parser_initializer (parser, &is_direct_init,
47642 &is_non_constant_init);
47643 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
47644 NULL_TREE, LOOKUP_ONLYCONVERTING);
47646 else
47648 cp_parser_parse_tentatively (parser);
47649 /* Don't create location wrapper nodes here. */
47650 auto_suppress_location_wrappers sentinel;
47651 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
47652 /*check_dependency_p=*/true,
47653 /*template_p=*/NULL,
47654 /*declarator_p=*/false,
47655 /*optional_p=*/false);
47656 vec<tree, va_gc> *args;
47657 if (fn_name == error_mark_node
47658 || cp_parser_error_occurred (parser)
47659 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
47660 || ((args = cp_parser_parenthesized_expression_list
47661 (parser, non_attr, /*cast_p=*/false,
47662 /*allow_expansion_p=*/true,
47663 /*non_constant_p=*/NULL)),
47664 cp_parser_error_occurred (parser)))
47666 finish_omp_structured_block (block);
47667 cp_parser_abort_tentative_parse (parser);
47668 cp_parser_error (parser, "expected id-expression (arguments)");
47669 return false;
47671 unsigned int i;
47672 tree arg;
47673 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
47674 if (arg == omp_priv
47675 || (TREE_CODE (arg) == ADDR_EXPR
47676 && TREE_OPERAND (arg, 0) == omp_priv))
47677 break;
47678 cp_parser_abort_tentative_parse (parser);
47679 if (arg == NULL_TREE)
47680 error ("one of the initializer call arguments should be %<omp_priv%>"
47681 " or %<&omp_priv%>");
47682 initializer = cp_parser_postfix_expression (parser, false, false, false,
47683 false, NULL);
47684 finish_expr_stmt (initializer);
47687 block = finish_omp_structured_block (block);
47688 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
47689 if (processing_template_decl)
47690 block = build_stmt (input_location, EXPR_STMT, block);
47691 add_stmt (block);
47693 if (ctor)
47694 add_decl_expr (omp_orig);
47696 if (!parens.require_close (parser))
47697 return false;
47700 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
47701 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
47702 UNKNOWN_LOCATION);
47704 return true;
47707 /* OpenMP 4.0
47708 #pragma omp declare reduction (reduction-id : typename-list : expression) \
47709 initializer-clause[opt] new-line
47711 initializer-clause:
47712 initializer (omp_priv initializer)
47713 initializer (function-name (argument-list)) */
47715 static void
47716 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
47717 enum pragma_context)
47719 auto_vec<tree> types;
47720 enum tree_code reduc_code = ERROR_MARK;
47721 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
47722 unsigned int i;
47723 cp_token *first_token;
47724 cp_token_cache *cp;
47725 int errs;
47726 void *p;
47728 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
47729 p = obstack_alloc (&declarator_obstack, 0);
47731 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
47732 goto fail;
47734 switch (cp_lexer_peek_token (parser->lexer)->type)
47736 case CPP_PLUS:
47737 reduc_code = PLUS_EXPR;
47738 break;
47739 case CPP_MULT:
47740 reduc_code = MULT_EXPR;
47741 break;
47742 case CPP_MINUS:
47743 reduc_code = MINUS_EXPR;
47744 break;
47745 case CPP_AND:
47746 reduc_code = BIT_AND_EXPR;
47747 break;
47748 case CPP_XOR:
47749 reduc_code = BIT_XOR_EXPR;
47750 break;
47751 case CPP_OR:
47752 reduc_code = BIT_IOR_EXPR;
47753 break;
47754 case CPP_AND_AND:
47755 reduc_code = TRUTH_ANDIF_EXPR;
47756 break;
47757 case CPP_OR_OR:
47758 reduc_code = TRUTH_ORIF_EXPR;
47759 break;
47760 case CPP_NAME:
47761 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
47762 break;
47763 default:
47764 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
47765 "%<|%>, %<&&%>, %<||%> or identifier");
47766 goto fail;
47769 if (reduc_code != ERROR_MARK)
47770 cp_lexer_consume_token (parser->lexer);
47772 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
47773 if (reduc_id == error_mark_node)
47774 goto fail;
47776 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
47777 goto fail;
47779 /* Types may not be defined in declare reduction type list. */
47780 const char *saved_message;
47781 saved_message = parser->type_definition_forbidden_message;
47782 parser->type_definition_forbidden_message
47783 = G_("types may not be defined in declare reduction type list");
47784 bool saved_colon_corrects_to_scope_p;
47785 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
47786 parser->colon_corrects_to_scope_p = false;
47787 bool saved_colon_doesnt_start_class_def_p;
47788 saved_colon_doesnt_start_class_def_p
47789 = parser->colon_doesnt_start_class_def_p;
47790 parser->colon_doesnt_start_class_def_p = true;
47792 while (true)
47794 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
47795 type = cp_parser_type_id (parser);
47796 if (type == error_mark_node)
47798 else if (ARITHMETIC_TYPE_P (type)
47799 && (orig_reduc_id == NULL_TREE
47800 || (TREE_CODE (type) != COMPLEX_TYPE
47801 && (id_equal (orig_reduc_id, "min")
47802 || id_equal (orig_reduc_id, "max")))))
47803 error_at (loc, "predeclared arithmetic type %qT in "
47804 "%<#pragma omp declare reduction%>", type);
47805 else if (FUNC_OR_METHOD_TYPE_P (type)
47806 || TREE_CODE (type) == ARRAY_TYPE)
47807 error_at (loc, "function or array type %qT in "
47808 "%<#pragma omp declare reduction%>", type);
47809 else if (TYPE_REF_P (type))
47810 error_at (loc, "reference type %qT in "
47811 "%<#pragma omp declare reduction%>", type);
47812 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
47813 error_at (loc, "%<const%>, %<volatile%> or %<__restrict%>-qualified "
47814 "type %qT in %<#pragma omp declare reduction%>", type);
47815 else
47816 types.safe_push (type);
47818 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
47819 cp_lexer_consume_token (parser->lexer);
47820 else
47821 break;
47824 /* Restore the saved message. */
47825 parser->type_definition_forbidden_message = saved_message;
47826 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
47827 parser->colon_doesnt_start_class_def_p
47828 = saved_colon_doesnt_start_class_def_p;
47830 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
47831 || types.is_empty ())
47833 fail:
47834 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47835 goto done;
47838 first_token = cp_lexer_peek_token (parser->lexer);
47839 cp = NULL;
47840 errs = errorcount;
47841 FOR_EACH_VEC_ELT (types, i, type)
47843 tree fntype
47844 = build_function_type_list (void_type_node,
47845 cp_build_reference_type (type, false),
47846 NULL_TREE);
47847 tree this_reduc_id = reduc_id;
47848 if (!dependent_type_p (type))
47849 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
47850 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
47851 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
47852 DECL_ARTIFICIAL (fndecl) = 1;
47853 DECL_EXTERNAL (fndecl) = 1;
47854 DECL_DECLARED_INLINE_P (fndecl) = 1;
47855 DECL_IGNORED_P (fndecl) = 1;
47856 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
47857 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
47858 DECL_ATTRIBUTES (fndecl)
47859 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
47860 DECL_ATTRIBUTES (fndecl));
47861 bool block_scope = false;
47862 if (current_function_decl)
47864 block_scope = true;
47865 DECL_CONTEXT (fndecl) = current_function_decl;
47866 DECL_LOCAL_DECL_P (fndecl) = true;
47869 if (processing_template_decl)
47870 fndecl = push_template_decl (fndecl);
47872 if (block_scope)
47874 if (!processing_template_decl)
47875 pushdecl (fndecl);
47877 else if (current_class_type)
47879 if (cp == NULL)
47881 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
47882 cp_lexer_consume_token (parser->lexer);
47883 cp = cp_token_cache_new (first_token,
47884 cp_lexer_peek_nth_token (parser->lexer,
47885 2));
47887 DECL_STATIC_FUNCTION_P (fndecl) = 1;
47888 finish_member_declaration (fndecl);
47889 DECL_PENDING_INLINE_INFO (fndecl) = cp;
47890 DECL_PENDING_INLINE_P (fndecl) = 1;
47891 vec_safe_push (unparsed_funs_with_definitions, fndecl);
47892 continue;
47894 else
47896 DECL_CONTEXT (fndecl) = current_namespace;
47897 tree d = pushdecl (fndecl);
47898 /* We should never meet a matched duplicate decl. */
47899 gcc_checking_assert (d == error_mark_node || d == fndecl);
47902 tree block = NULL_TREE;
47903 if (!block_scope)
47904 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
47905 else
47906 block = begin_omp_structured_block ();
47907 if (cp)
47909 cp_parser_push_lexer_for_tokens (parser, cp);
47910 parser->lexer->in_pragma = true;
47913 bool ok = cp_parser_omp_declare_reduction_exprs (fndecl, parser);
47915 if (cp)
47916 cp_parser_pop_lexer (parser);
47917 if (!block_scope)
47918 finish_function (/*inline_p=*/false);
47919 else
47921 DECL_CONTEXT (fndecl) = current_function_decl;
47922 if (DECL_TEMPLATE_INFO (fndecl))
47923 DECL_CONTEXT (DECL_TI_TEMPLATE (fndecl)) = current_function_decl;
47925 if (!ok)
47926 goto fail;
47928 if (block_scope)
47930 block = finish_omp_structured_block (block);
47931 if (TREE_CODE (block) == BIND_EXPR)
47932 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
47933 else if (TREE_CODE (block) == STATEMENT_LIST)
47934 DECL_SAVED_TREE (fndecl) = block;
47935 if (processing_template_decl)
47936 add_decl_expr (fndecl);
47939 cp_check_omp_declare_reduction (fndecl);
47940 if (cp == NULL && types.length () > 1)
47941 cp = cp_token_cache_new (first_token,
47942 cp_lexer_peek_nth_token (parser->lexer, 2));
47943 if (errs != errorcount)
47944 break;
47947 cp_parser_require_pragma_eol (parser, pragma_tok);
47949 done:
47950 /* Free any declarators allocated. */
47951 obstack_free (&declarator_obstack, p);
47954 /* OpenMP 4.0
47955 #pragma omp declare simd declare-simd-clauses[optseq] new-line
47956 #pragma omp declare reduction (reduction-id : typename-list : expression) \
47957 initializer-clause[opt] new-line
47958 #pragma omp declare target new-line
47960 OpenMP 5.0
47961 #pragma omp declare variant (identifier) match (context-selector) */
47963 static bool
47964 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
47965 enum pragma_context context)
47967 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47969 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
47970 const char *p = IDENTIFIER_POINTER (id);
47972 if (strcmp (p, "simd") == 0)
47974 cp_lexer_consume_token (parser->lexer);
47975 cp_parser_omp_declare_simd (parser, pragma_tok,
47976 context, false);
47977 return true;
47979 if (flag_openmp && strcmp (p, "variant") == 0)
47981 cp_lexer_consume_token (parser->lexer);
47982 cp_parser_omp_declare_simd (parser, pragma_tok,
47983 context, true);
47984 return true;
47986 cp_ensure_no_omp_declare_simd (parser);
47987 if (strcmp (p, "reduction") == 0)
47989 cp_lexer_consume_token (parser->lexer);
47990 cp_parser_omp_declare_reduction (parser, pragma_tok,
47991 context);
47992 return false;
47994 if (!flag_openmp) /* flag_openmp_simd */
47996 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47997 return false;
47999 if (strcmp (p, "target") == 0)
48001 cp_lexer_consume_token (parser->lexer);
48002 cp_parser_omp_declare_target (parser, pragma_tok);
48003 return false;
48006 cp_parser_error (parser, "expected %<simd%>, %<reduction%>, "
48007 "%<target%> or %<variant%>");
48008 cp_parser_require_pragma_eol (parser, pragma_tok);
48009 return false;
48012 /* OpenMP 5.0
48013 #pragma omp requires clauses[optseq] new-line */
48015 static bool
48016 cp_parser_omp_requires (cp_parser *parser, cp_token *pragma_tok)
48018 enum omp_requires new_req = (enum omp_requires) 0;
48020 location_t loc = pragma_tok->location;
48021 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
48023 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
48024 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
48025 cp_lexer_consume_token (parser->lexer);
48027 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
48029 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
48030 const char *p = IDENTIFIER_POINTER (id);
48031 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
48032 enum omp_requires this_req = (enum omp_requires) 0;
48034 if (!strcmp (p, "unified_address"))
48035 this_req = OMP_REQUIRES_UNIFIED_ADDRESS;
48036 else if (!strcmp (p, "unified_shared_memory"))
48037 this_req = OMP_REQUIRES_UNIFIED_SHARED_MEMORY;
48038 else if (!strcmp (p, "dynamic_allocators"))
48039 this_req = OMP_REQUIRES_DYNAMIC_ALLOCATORS;
48040 else if (!strcmp (p, "reverse_offload"))
48041 this_req = OMP_REQUIRES_REVERSE_OFFLOAD;
48042 else if (!strcmp (p, "atomic_default_mem_order"))
48044 cp_lexer_consume_token (parser->lexer);
48046 matching_parens parens;
48047 if (parens.require_open (parser))
48049 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
48051 id = cp_lexer_peek_token (parser->lexer)->u.value;
48052 p = IDENTIFIER_POINTER (id);
48054 if (!strcmp (p, "seq_cst"))
48055 this_req
48056 = (enum omp_requires) OMP_MEMORY_ORDER_SEQ_CST;
48057 else if (!strcmp (p, "relaxed"))
48058 this_req
48059 = (enum omp_requires) OMP_MEMORY_ORDER_RELAXED;
48060 else if (!strcmp (p, "acq_rel"))
48061 this_req
48062 = (enum omp_requires) OMP_MEMORY_ORDER_ACQ_REL;
48064 if (this_req == 0)
48066 error_at (cp_lexer_peek_token (parser->lexer)->location,
48067 "expected %<seq_cst%>, %<relaxed%> or "
48068 "%<acq_rel%>");
48069 switch (cp_lexer_peek_token (parser->lexer)->type)
48071 case CPP_EOF:
48072 case CPP_PRAGMA_EOL:
48073 case CPP_CLOSE_PAREN:
48074 break;
48075 default:
48076 if (cp_lexer_nth_token_is (parser->lexer, 2,
48077 CPP_CLOSE_PAREN))
48078 cp_lexer_consume_token (parser->lexer);
48079 break;
48082 else
48083 cp_lexer_consume_token (parser->lexer);
48085 if (!parens.require_close (parser))
48086 cp_parser_skip_to_closing_parenthesis (parser,
48087 /*recovering=*/true,
48088 /*or_comma=*/false,
48089 /*consume_paren=*/
48090 true);
48092 if (this_req == 0)
48094 cp_parser_require_pragma_eol (parser, pragma_tok);
48095 return false;
48098 p = NULL;
48100 else
48102 error_at (cloc, "expected %<unified_address%>, "
48103 "%<unified_shared_memory%>, "
48104 "%<dynamic_allocators%>, "
48105 "%<reverse_offload%> "
48106 "or %<atomic_default_mem_order%> clause");
48107 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
48108 return false;
48110 if (p)
48111 cp_lexer_consume_token (parser->lexer);
48112 if (this_req)
48114 if ((this_req & ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
48116 if ((this_req & new_req) != 0)
48117 error_at (cloc, "too many %qs clauses", p);
48118 if (this_req != OMP_REQUIRES_DYNAMIC_ALLOCATORS
48119 && (omp_requires_mask & OMP_REQUIRES_TARGET_USED) != 0)
48120 error_at (cloc, "%qs clause used lexically after first "
48121 "target construct or offloading API", p);
48123 else if ((new_req & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
48125 error_at (cloc, "too many %qs clauses",
48126 "atomic_default_mem_order");
48127 this_req = (enum omp_requires) 0;
48129 else if ((omp_requires_mask
48130 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
48132 error_at (cloc, "more than one %<atomic_default_mem_order%>"
48133 " clause in a single compilation unit");
48134 this_req
48135 = (enum omp_requires)
48136 (omp_requires_mask
48137 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER);
48139 else if ((omp_requires_mask
48140 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED) != 0)
48141 error_at (cloc, "%<atomic_default_mem_order%> clause used "
48142 "lexically after first %<atomic%> construct "
48143 "without memory order clause");
48144 new_req = (enum omp_requires) (new_req | this_req);
48145 omp_requires_mask
48146 = (enum omp_requires) (omp_requires_mask | this_req);
48147 continue;
48150 break;
48152 cp_parser_require_pragma_eol (parser, pragma_tok);
48154 if (new_req == 0)
48155 error_at (loc, "%<pragma omp requires%> requires at least one clause");
48156 return false;
48160 /* OpenMP 5.1:
48161 #pragma omp nothing new-line */
48163 static void
48164 cp_parser_omp_nothing (cp_parser *parser, cp_token *pragma_tok)
48166 cp_parser_require_pragma_eol (parser, pragma_tok);
48170 /* OpenMP 5.1
48171 #pragma omp error clauses[optseq] new-line */
48173 static bool
48174 cp_parser_omp_error (cp_parser *parser, cp_token *pragma_tok,
48175 enum pragma_context context)
48177 int at_compilation = -1;
48178 int severity_fatal = -1;
48179 tree message = NULL_TREE;
48180 bool bad = false;
48181 location_t loc = pragma_tok->location;
48183 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
48185 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
48186 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
48187 cp_lexer_consume_token (parser->lexer);
48189 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
48190 break;
48192 const char *p
48193 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
48194 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
48195 static const char *args[] = {
48196 "execution", "compilation", "warning", "fatal"
48198 int *v = NULL;
48199 int idx = 0, n = -1;
48200 tree m = NULL_TREE;
48202 if (!strcmp (p, "at"))
48203 v = &at_compilation;
48204 else if (!strcmp (p, "severity"))
48206 v = &severity_fatal;
48207 idx += 2;
48209 else if (strcmp (p, "message"))
48211 error_at (cloc,
48212 "expected %<at%>, %<severity%> or %<message%> clause");
48213 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
48214 return false;
48217 cp_lexer_consume_token (parser->lexer);
48219 matching_parens parens;
48220 if (parens.require_open (parser))
48222 if (v == NULL)
48224 m = cp_parser_assignment_expression (parser);
48225 if (type_dependent_expression_p (m))
48226 m = build1 (IMPLICIT_CONV_EXPR, const_string_type_node, m);
48227 else
48228 m = perform_implicit_conversion_flags (const_string_type_node, m,
48229 tf_warning_or_error,
48230 LOOKUP_NORMAL);
48232 else
48234 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
48236 tree val = cp_lexer_peek_token (parser->lexer)->u.value;
48237 const char *q = IDENTIFIER_POINTER (val);
48239 if (!strcmp (q, args[idx]))
48240 n = 0;
48241 else if (!strcmp (q, args[idx + 1]))
48242 n = 1;
48244 if (n == -1)
48246 error_at (cp_lexer_peek_token (parser->lexer)->location,
48247 "expected %qs or %qs", args[idx], args[idx + 1]);
48248 bad = true;
48249 switch (cp_lexer_peek_token (parser->lexer)->type)
48251 case CPP_EOF:
48252 case CPP_PRAGMA_EOL:
48253 case CPP_CLOSE_PAREN:
48254 break;
48255 default:
48256 if (cp_lexer_nth_token_is (parser->lexer, 2,
48257 CPP_CLOSE_PAREN))
48258 cp_lexer_consume_token (parser->lexer);
48259 break;
48262 else
48263 cp_lexer_consume_token (parser->lexer);
48266 if (!parens.require_close (parser))
48267 cp_parser_skip_to_closing_parenthesis (parser,
48268 /*recovering=*/true,
48269 /*or_comma=*/false,
48270 /*consume_paren=*/
48271 true);
48273 if (v == NULL)
48275 if (message)
48277 error_at (cloc, "too many %qs clauses", p);
48278 bad = true;
48280 else
48281 message = m;
48283 else if (n != -1)
48285 if (*v != -1)
48287 error_at (cloc, "too many %qs clauses", p);
48288 bad = true;
48290 else
48291 *v = n;
48294 else
48295 bad = true;
48297 cp_parser_require_pragma_eol (parser, pragma_tok);
48298 if (bad)
48299 return true;
48301 if (at_compilation == -1)
48302 at_compilation = 1;
48303 if (severity_fatal == -1)
48304 severity_fatal = 1;
48305 if (!at_compilation)
48307 if (context != pragma_compound)
48309 error_at (loc, "%<#pragma omp error%> with %<at(execution)%> clause "
48310 "may only be used in compound statements");
48311 return true;
48313 tree fndecl
48314 = builtin_decl_explicit (severity_fatal ? BUILT_IN_GOMP_ERROR
48315 : BUILT_IN_GOMP_WARNING);
48316 if (!message)
48317 message = build_zero_cst (const_string_type_node);
48318 tree stmt = build_call_expr_loc (loc, fndecl, 2, message,
48319 build_all_ones_cst (size_type_node));
48320 add_stmt (stmt);
48321 return true;
48324 if (in_discarded_stmt)
48325 return false;
48327 const char *msg = NULL;
48328 if (message)
48330 msg = c_getstr (fold_for_warn (message));
48331 if (msg == NULL)
48332 msg = _("<message unknown at compile time>");
48334 if (msg)
48335 emit_diagnostic (severity_fatal ? DK_ERROR : DK_WARNING, loc, 0,
48336 "%<pragma omp error%> encountered: %s", msg);
48337 else
48338 emit_diagnostic (severity_fatal ? DK_ERROR : DK_WARNING, loc, 0,
48339 "%<pragma omp error%> encountered");
48340 return false;
48343 /* OpenMP 4.5:
48344 #pragma omp taskloop taskloop-clause[optseq] new-line
48345 for-loop
48347 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
48348 for-loop */
48350 #define OMP_TASKLOOP_CLAUSE_MASK \
48351 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
48352 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
48353 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
48354 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
48355 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
48356 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
48357 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
48358 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
48359 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
48360 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
48361 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
48362 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
48363 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
48364 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
48365 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
48366 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
48367 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
48369 static tree
48370 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
48371 char *p_name, omp_clause_mask mask, tree *cclauses,
48372 bool *if_p)
48374 tree clauses, sb, ret;
48375 unsigned int save;
48376 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
48378 strcat (p_name, " taskloop");
48379 mask |= OMP_TASKLOOP_CLAUSE_MASK;
48380 /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
48381 clause. */
48382 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)) != 0)
48383 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION);
48385 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
48387 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
48388 const char *p = IDENTIFIER_POINTER (id);
48390 if (strcmp (p, "simd") == 0)
48392 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
48393 if (cclauses == NULL)
48394 cclauses = cclauses_buf;
48396 cp_lexer_consume_token (parser->lexer);
48397 if (!flag_openmp) /* flag_openmp_simd */
48398 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
48399 cclauses, if_p);
48400 sb = begin_omp_structured_block ();
48401 save = cp_parser_begin_omp_structured_block (parser);
48402 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
48403 cclauses, if_p);
48404 cp_parser_end_omp_structured_block (parser, save);
48405 tree body = finish_omp_structured_block (sb);
48406 if (ret == NULL)
48407 return ret;
48408 ret = make_node (OMP_TASKLOOP);
48409 TREE_TYPE (ret) = void_type_node;
48410 OMP_FOR_BODY (ret) = body;
48411 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
48412 SET_EXPR_LOCATION (ret, loc);
48413 add_stmt (ret);
48414 return ret;
48417 if (!flag_openmp) /* flag_openmp_simd */
48419 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
48420 return NULL_TREE;
48423 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
48424 cclauses == NULL);
48425 if (cclauses)
48427 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
48428 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
48431 keep_next_level (true);
48432 sb = begin_omp_structured_block ();
48433 save = cp_parser_begin_omp_structured_block (parser);
48435 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
48436 if_p);
48438 cp_parser_end_omp_structured_block (parser, save);
48439 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
48441 return ret;
48445 /* OpenACC 2.0:
48446 # pragma acc routine oacc-routine-clause[optseq] new-line
48447 function-definition
48449 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
48452 #define OACC_ROUTINE_CLAUSE_MASK \
48453 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
48454 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
48455 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
48456 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
48457 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NOHOST) )
48459 /* Parse the OpenACC routine pragma. This has an optional '( name )'
48460 component, which must resolve to a declared namespace-scope
48461 function. The clauses are either processed directly (for a named
48462 function), or defered until the immediatley following declaration
48463 is parsed. */
48465 static void
48466 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
48467 enum pragma_context context)
48469 gcc_checking_assert (context == pragma_external);
48470 /* The checking for "another pragma following this one" in the "no optional
48471 '( name )'" case makes sure that we dont re-enter. */
48472 gcc_checking_assert (parser->oacc_routine == NULL);
48474 cp_oacc_routine_data data;
48475 data.error_seen = false;
48476 data.fndecl_seen = false;
48477 data.tokens = vNULL;
48478 data.clauses = NULL_TREE;
48479 data.loc = pragma_tok->location;
48480 /* It is safe to take the address of a local variable; it will only be
48481 used while this scope is live. */
48482 parser->oacc_routine = &data;
48484 /* Look for optional '( name )'. */
48485 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
48487 matching_parens parens;
48488 parens.consume_open (parser); /* '(' */
48490 /* We parse the name as an id-expression. If it resolves to
48491 anything other than a non-overloaded function at namespace
48492 scope, it's an error. */
48493 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
48494 tree name = cp_parser_id_expression (parser,
48495 /*template_keyword_p=*/false,
48496 /*check_dependency_p=*/false,
48497 /*template_p=*/NULL,
48498 /*declarator_p=*/false,
48499 /*optional_p=*/false);
48500 tree decl = (identifier_p (name)
48501 ? cp_parser_lookup_name_simple (parser, name, name_loc)
48502 : name);
48503 if (name != error_mark_node && decl == error_mark_node)
48504 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
48506 if (decl == error_mark_node
48507 || !parens.require_close (parser))
48509 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
48510 parser->oacc_routine = NULL;
48511 return;
48514 data.clauses
48515 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
48516 "#pragma acc routine",
48517 cp_lexer_peek_token (parser->lexer));
48518 /* The clauses are in reverse order; fix that to make later diagnostic
48519 emission easier. */
48520 data.clauses = nreverse (data.clauses);
48522 if (decl && is_overloaded_fn (decl)
48523 && (TREE_CODE (decl) != FUNCTION_DECL
48524 || DECL_FUNCTION_TEMPLATE_P (decl)))
48526 error_at (name_loc,
48527 "%<#pragma acc routine%> names a set of overloads");
48528 parser->oacc_routine = NULL;
48529 return;
48532 /* Perhaps we should use the same rule as declarations in different
48533 namespaces? */
48534 if (!DECL_NAMESPACE_SCOPE_P (decl))
48536 error_at (name_loc,
48537 "%qD does not refer to a namespace scope function", decl);
48538 parser->oacc_routine = NULL;
48539 return;
48542 if (TREE_CODE (decl) != FUNCTION_DECL)
48544 error_at (name_loc, "%qD does not refer to a function", decl);
48545 parser->oacc_routine = NULL;
48546 return;
48549 cp_finalize_oacc_routine (parser, decl, false);
48550 parser->oacc_routine = NULL;
48552 else /* No optional '( name )'. */
48554 /* Store away all pragma tokens. */
48555 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
48556 cp_lexer_consume_token (parser->lexer);
48557 cp_parser_require_pragma_eol (parser, pragma_tok);
48558 struct cp_token_cache *cp
48559 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
48560 parser->oacc_routine->tokens.safe_push (cp);
48562 /* Emit a helpful diagnostic if there's another pragma following this
48563 one. */
48564 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
48566 cp_ensure_no_oacc_routine (parser);
48567 data.tokens.release ();
48568 /* ..., and then just keep going. */
48569 return;
48572 /* We only have to consider the pragma_external case here. */
48573 cp_parser_declaration (parser, NULL_TREE);
48574 if (parser->oacc_routine
48575 && !parser->oacc_routine->fndecl_seen)
48576 cp_ensure_no_oacc_routine (parser);
48577 else
48578 parser->oacc_routine = NULL;
48579 data.tokens.release ();
48583 /* Finalize #pragma acc routine clauses after direct declarator has
48584 been parsed. */
48586 static tree
48587 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
48589 struct cp_token_cache *ce;
48590 cp_oacc_routine_data *data = parser->oacc_routine;
48592 if (!data->error_seen && data->fndecl_seen)
48594 error_at (data->loc,
48595 "%<#pragma acc routine%> not immediately followed by "
48596 "a single function declaration or definition");
48597 data->error_seen = true;
48599 if (data->error_seen)
48600 return attrs;
48602 gcc_checking_assert (data->tokens.length () == 1);
48603 ce = data->tokens[0];
48605 cp_parser_push_lexer_for_tokens (parser, ce);
48606 parser->lexer->in_pragma = true;
48607 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
48609 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
48610 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
48611 parser->oacc_routine->clauses
48612 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
48613 "#pragma acc routine", pragma_tok);
48614 /* The clauses are in reverse order; fix that to make later diagnostic
48615 emission easier. */
48616 parser->oacc_routine->clauses = nreverse (parser->oacc_routine->clauses);
48617 cp_parser_pop_lexer (parser);
48618 /* Later, cp_finalize_oacc_routine will process the clauses. */
48619 parser->oacc_routine->fndecl_seen = true;
48621 return attrs;
48624 /* Apply any saved OpenACC routine clauses to a just-parsed
48625 declaration. */
48627 static void
48628 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
48630 if (UNLIKELY (parser->oacc_routine != NULL))
48632 /* Keep going if we're in error reporting mode. */
48633 if (parser->oacc_routine->error_seen
48634 || fndecl == error_mark_node)
48635 return;
48637 if (TREE_CODE (fndecl) != FUNCTION_DECL)
48639 if (parser->oacc_routine->fndecl_seen)
48641 error_at (parser->oacc_routine->loc,
48642 "%<#pragma acc routine%> not immediately followed by"
48643 " a single function declaration or definition");
48644 parser->oacc_routine = NULL;
48645 return;
48648 cp_ensure_no_oacc_routine (parser);
48649 return;
48652 int compatible
48653 = oacc_verify_routine_clauses (fndecl, &parser->oacc_routine->clauses,
48654 parser->oacc_routine->loc,
48655 "#pragma acc routine");
48656 if (compatible < 0)
48658 parser->oacc_routine = NULL;
48659 return;
48661 if (compatible > 0)
48664 else
48666 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
48668 error_at (parser->oacc_routine->loc,
48669 TREE_USED (fndecl)
48670 ? G_("%<#pragma acc routine%> must be applied before"
48671 " use")
48672 : G_("%<#pragma acc routine%> must be applied before"
48673 " definition"));
48674 parser->oacc_routine = NULL;
48675 return;
48678 /* Set the routine's level of parallelism. */
48679 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
48680 oacc_replace_fn_attrib (fndecl, dims);
48682 /* Add an "omp declare target" attribute. */
48683 DECL_ATTRIBUTES (fndecl)
48684 = tree_cons (get_identifier ("omp declare target"),
48685 parser->oacc_routine->clauses,
48686 DECL_ATTRIBUTES (fndecl));
48691 /* Main entry point to OpenMP statement pragmas. */
48693 static void
48694 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
48696 tree stmt;
48697 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
48698 omp_clause_mask mask (0);
48700 switch (cp_parser_pragma_kind (pragma_tok))
48702 case PRAGMA_OACC_ATOMIC:
48703 cp_parser_omp_atomic (parser, pragma_tok, true);
48704 return;
48705 case PRAGMA_OACC_CACHE:
48706 stmt = cp_parser_oacc_cache (parser, pragma_tok);
48707 break;
48708 case PRAGMA_OACC_DATA:
48709 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
48710 break;
48711 case PRAGMA_OACC_ENTER_DATA:
48712 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
48713 break;
48714 case PRAGMA_OACC_EXIT_DATA:
48715 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
48716 break;
48717 case PRAGMA_OACC_HOST_DATA:
48718 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
48719 break;
48720 case PRAGMA_OACC_KERNELS:
48721 case PRAGMA_OACC_PARALLEL:
48722 case PRAGMA_OACC_SERIAL:
48723 strcpy (p_name, "#pragma acc");
48724 stmt = cp_parser_oacc_compute (parser, pragma_tok, p_name, if_p);
48725 break;
48726 case PRAGMA_OACC_LOOP:
48727 strcpy (p_name, "#pragma acc");
48728 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
48729 if_p);
48730 break;
48731 case PRAGMA_OACC_UPDATE:
48732 stmt = cp_parser_oacc_update (parser, pragma_tok);
48733 break;
48734 case PRAGMA_OACC_WAIT:
48735 stmt = cp_parser_oacc_wait (parser, pragma_tok);
48736 break;
48737 case PRAGMA_OMP_ALLOCATE:
48738 cp_parser_omp_allocate (parser, pragma_tok);
48739 return;
48740 case PRAGMA_OMP_ATOMIC:
48741 cp_parser_omp_atomic (parser, pragma_tok, false);
48742 return;
48743 case PRAGMA_OMP_CRITICAL:
48744 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
48745 break;
48746 case PRAGMA_OMP_DISTRIBUTE:
48747 strcpy (p_name, "#pragma omp");
48748 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
48749 if_p);
48750 break;
48751 case PRAGMA_OMP_FOR:
48752 strcpy (p_name, "#pragma omp");
48753 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
48754 if_p);
48755 break;
48756 case PRAGMA_OMP_LOOP:
48757 strcpy (p_name, "#pragma omp");
48758 stmt = cp_parser_omp_loop (parser, pragma_tok, p_name, mask, NULL,
48759 if_p);
48760 break;
48761 case PRAGMA_OMP_MASKED:
48762 strcpy (p_name, "#pragma omp");
48763 stmt = cp_parser_omp_masked (parser, pragma_tok, p_name, mask, NULL,
48764 if_p);
48765 break;
48766 case PRAGMA_OMP_MASTER:
48767 strcpy (p_name, "#pragma omp");
48768 stmt = cp_parser_omp_master (parser, pragma_tok, p_name, mask, NULL,
48769 if_p);
48770 break;
48771 case PRAGMA_OMP_PARALLEL:
48772 strcpy (p_name, "#pragma omp");
48773 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
48774 if_p);
48775 break;
48776 case PRAGMA_OMP_SCOPE:
48777 stmt = cp_parser_omp_scope (parser, pragma_tok, if_p);
48778 break;
48779 case PRAGMA_OMP_SECTIONS:
48780 strcpy (p_name, "#pragma omp");
48781 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
48782 break;
48783 case PRAGMA_OMP_SIMD:
48784 strcpy (p_name, "#pragma omp");
48785 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
48786 if_p);
48787 break;
48788 case PRAGMA_OMP_SINGLE:
48789 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
48790 break;
48791 case PRAGMA_OMP_TASK:
48792 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
48793 break;
48794 case PRAGMA_OMP_TASKGROUP:
48795 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
48796 break;
48797 case PRAGMA_OMP_TASKLOOP:
48798 strcpy (p_name, "#pragma omp");
48799 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
48800 if_p);
48801 break;
48802 case PRAGMA_OMP_TEAMS:
48803 strcpy (p_name, "#pragma omp");
48804 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
48805 if_p);
48806 break;
48807 case PRAGMA_OMP_ASSUME:
48808 cp_parser_omp_assume (parser, pragma_tok, if_p);
48809 return;
48810 default:
48811 gcc_unreachable ();
48814 protected_set_expr_location (stmt, pragma_tok->location);
48817 /* Transactional Memory parsing routines. */
48819 /* Parse a transaction attribute.
48821 txn-attribute:
48822 attribute
48823 [ [ identifier ] ]
48825 We use this instead of cp_parser_attributes_opt for transactions to avoid
48826 the pedwarn in C++98 mode. */
48828 static tree
48829 cp_parser_txn_attribute_opt (cp_parser *parser)
48831 cp_token *token;
48832 tree attr_name, attr = NULL;
48834 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
48835 return cp_parser_attributes_opt (parser);
48837 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
48838 return NULL_TREE;
48839 cp_lexer_consume_token (parser->lexer);
48840 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
48841 goto error1;
48843 token = cp_lexer_peek_token (parser->lexer);
48844 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
48846 token = cp_lexer_consume_token (parser->lexer);
48848 attr_name = (token->type == CPP_KEYWORD
48849 /* For keywords, use the canonical spelling,
48850 not the parsed identifier. */
48851 ? ridpointers[(int) token->keyword]
48852 : token->u.value);
48853 attr = build_tree_list (attr_name, NULL_TREE);
48855 else
48856 cp_parser_error (parser, "expected identifier");
48858 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
48859 error1:
48860 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
48861 return attr;
48864 /* Parse a __transaction_atomic or __transaction_relaxed statement.
48866 transaction-statement:
48867 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
48868 compound-statement
48869 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
48872 static tree
48873 cp_parser_transaction (cp_parser *parser, cp_token *token)
48875 unsigned char old_in = parser->in_transaction;
48876 unsigned char this_in = 1, new_in;
48877 enum rid keyword = token->keyword;
48878 tree stmt, attrs, noex;
48880 cp_lexer_consume_token (parser->lexer);
48882 if (keyword == RID_TRANSACTION_RELAXED
48883 || keyword == RID_SYNCHRONIZED)
48884 this_in |= TM_STMT_ATTR_RELAXED;
48885 else
48887 attrs = cp_parser_txn_attribute_opt (parser);
48888 if (attrs)
48889 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
48892 /* Parse a noexcept specification. */
48893 if (keyword == RID_ATOMIC_NOEXCEPT)
48894 noex = boolean_true_node;
48895 else if (keyword == RID_ATOMIC_CANCEL)
48897 /* cancel-and-throw is unimplemented. */
48898 sorry ("%<atomic_cancel%>");
48899 noex = NULL_TREE;
48901 else
48902 noex = cp_parser_noexcept_specification_opt (parser,
48903 CP_PARSER_FLAGS_NONE,
48904 /*require_constexpr=*/true,
48905 /*consumed_expr=*/NULL,
48906 /*return_cond=*/true);
48908 /* Keep track if we're in the lexical scope of an outer transaction. */
48909 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
48911 stmt = begin_transaction_stmt (token->location, NULL, this_in);
48913 parser->in_transaction = new_in;
48914 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
48915 parser->in_transaction = old_in;
48917 finish_transaction_stmt (stmt, NULL, this_in, noex);
48919 return stmt;
48922 /* Parse a __transaction_atomic or __transaction_relaxed expression.
48924 transaction-expression:
48925 __transaction_atomic txn-noexcept-spec[opt] ( expression )
48926 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
48929 static tree
48930 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
48932 unsigned char old_in = parser->in_transaction;
48933 unsigned char this_in = 1;
48934 cp_token *token;
48935 tree expr, noex;
48936 bool noex_expr;
48937 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
48939 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
48940 || keyword == RID_TRANSACTION_RELAXED);
48942 if (!flag_tm)
48943 error_at (loc,
48944 keyword == RID_TRANSACTION_RELAXED
48945 ? G_("%<__transaction_relaxed%> without transactional memory "
48946 "support enabled")
48947 : G_("%<__transaction_atomic%> without transactional memory "
48948 "support enabled"));
48950 token = cp_parser_require_keyword (parser, keyword,
48951 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
48952 : RT_TRANSACTION_RELAXED));
48953 gcc_assert (token != NULL);
48955 if (keyword == RID_TRANSACTION_RELAXED)
48956 this_in |= TM_STMT_ATTR_RELAXED;
48958 /* Set this early. This might mean that we allow transaction_cancel in
48959 an expression that we find out later actually has to be a constexpr.
48960 However, we expect that cxx_constant_value will be able to deal with
48961 this; also, if the noexcept has no constexpr, then what we parse next
48962 really is a transaction's body. */
48963 parser->in_transaction = this_in;
48965 /* Parse a noexcept specification. */
48966 noex = cp_parser_noexcept_specification_opt (parser,
48967 CP_PARSER_FLAGS_NONE,
48968 /*require_constexpr=*/false,
48969 &noex_expr,
48970 /*return_cond=*/true);
48972 if (!noex || !noex_expr
48973 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
48975 matching_parens parens;
48976 parens.require_open (parser);
48978 expr = cp_parser_expression (parser);
48979 expr = finish_parenthesized_expr (expr);
48981 parens.require_close (parser);
48983 else
48985 /* The only expression that is available got parsed for the noexcept
48986 already. noexcept is true then. */
48987 expr = noex;
48988 noex = boolean_true_node;
48991 expr = build_transaction_expr (token->location, expr, this_in, noex);
48992 parser->in_transaction = old_in;
48994 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
48995 return error_mark_node;
48997 return (flag_tm ? expr : error_mark_node);
49000 /* Parse a function-transaction-block.
49002 function-transaction-block:
49003 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
49004 function-body
49005 __transaction_atomic txn-attribute[opt] function-try-block
49006 __transaction_relaxed ctor-initializer[opt] function-body
49007 __transaction_relaxed function-try-block
49010 static void
49011 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
49013 unsigned char old_in = parser->in_transaction;
49014 unsigned char new_in = 1;
49015 tree compound_stmt, stmt, attrs;
49016 cp_token *token;
49018 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
49019 || keyword == RID_TRANSACTION_RELAXED);
49020 token = cp_parser_require_keyword (parser, keyword,
49021 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
49022 : RT_TRANSACTION_RELAXED));
49023 gcc_assert (token != NULL);
49025 if (keyword == RID_TRANSACTION_RELAXED)
49026 new_in |= TM_STMT_ATTR_RELAXED;
49027 else
49029 attrs = cp_parser_txn_attribute_opt (parser);
49030 if (attrs)
49031 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
49034 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
49036 parser->in_transaction = new_in;
49038 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
49039 cp_parser_function_try_block (parser);
49040 else
49041 cp_parser_ctor_initializer_opt_and_function_body
49042 (parser, /*in_function_try_block=*/false);
49044 parser->in_transaction = old_in;
49046 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
49049 /* Parse a __transaction_cancel statement.
49051 cancel-statement:
49052 __transaction_cancel txn-attribute[opt] ;
49053 __transaction_cancel txn-attribute[opt] throw-expression ;
49055 ??? Cancel and throw is not yet implemented. */
49057 static tree
49058 cp_parser_transaction_cancel (cp_parser *parser)
49060 cp_token *token;
49061 bool is_outer = false;
49062 tree stmt, attrs;
49064 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
49065 RT_TRANSACTION_CANCEL);
49066 gcc_assert (token != NULL);
49068 attrs = cp_parser_txn_attribute_opt (parser);
49069 if (attrs)
49070 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
49072 /* ??? Parse cancel-and-throw here. */
49074 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
49076 if (!flag_tm)
49078 error_at (token->location, "%<__transaction_cancel%> without "
49079 "transactional memory support enabled");
49080 return error_mark_node;
49082 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
49084 error_at (token->location, "%<__transaction_cancel%> within a "
49085 "%<__transaction_relaxed%>");
49086 return error_mark_node;
49088 else if (is_outer)
49090 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
49091 && !is_tm_may_cancel_outer (current_function_decl))
49093 error_at (token->location, "outer %<__transaction_cancel%> not "
49094 "within outer %<__transaction_atomic%>");
49095 error_at (token->location,
49096 " or a %<transaction_may_cancel_outer%> function");
49097 return error_mark_node;
49100 else if (parser->in_transaction == 0)
49102 error_at (token->location, "%<__transaction_cancel%> not within "
49103 "%<__transaction_atomic%>");
49104 return error_mark_node;
49107 stmt = build_tm_abort_call (token->location, is_outer);
49108 add_stmt (stmt);
49110 return stmt;
49114 /* Special handling for the first token or line in the file. The first
49115 thing in the file might be #pragma GCC pch_preprocess, which loads a
49116 PCH file, which is a GC collection point. So we need to handle this
49117 first pragma without benefit of an existing lexer structure.
49119 Always returns one token to the caller in *FIRST_TOKEN. This is
49120 either the true first token of the file, or the first token after
49121 the initial pragma. */
49123 static void
49124 cp_parser_initial_pragma (cp_token *first_token)
49126 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
49127 return;
49129 cp_lexer_get_preprocessor_token (0, first_token);
49131 tree name = NULL;
49132 if (first_token->type == CPP_STRING)
49134 name = first_token->u.value;
49136 cp_lexer_get_preprocessor_token (0, first_token);
49139 /* Skip to the end of the pragma. */
49140 if (first_token->type != CPP_PRAGMA_EOL)
49142 error_at (first_token->location,
49143 "malformed %<#pragma GCC pch_preprocess%>");
49145 cp_lexer_get_preprocessor_token (0, first_token);
49146 while (first_token->type != CPP_PRAGMA_EOL);
49149 /* Now actually load the PCH file. */
49150 if (name)
49151 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
49153 /* Read one more token to return to our caller. We have to do this
49154 after reading the PCH file in, since its pointers have to be
49155 live. */
49156 cp_lexer_get_preprocessor_token (0, first_token);
49159 /* Parse a pragma GCC ivdep. */
49161 static bool
49162 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
49164 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
49165 return true;
49168 /* Parse a pragma GCC unroll. */
49170 static unsigned short
49171 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
49173 location_t location = cp_lexer_peek_token (parser->lexer)->location;
49174 tree expr = cp_parser_constant_expression (parser);
49175 unsigned short unroll;
49176 expr = maybe_constant_value (expr);
49177 HOST_WIDE_INT lunroll = 0;
49178 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
49179 || TREE_CODE (expr) != INTEGER_CST
49180 || (lunroll = tree_to_shwi (expr)) < 0
49181 || lunroll >= USHRT_MAX)
49183 error_at (location, "%<#pragma GCC unroll%> requires an"
49184 " assignment-expression that evaluates to a non-negative"
49185 " integral constant less than %u", USHRT_MAX);
49186 unroll = 0;
49188 else
49190 unroll = (unsigned short)lunroll;
49191 if (unroll == 0)
49192 unroll = 1;
49194 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
49195 return unroll;
49198 /* Normal parsing of a pragma token. Here we can (and must) use the
49199 regular lexer. */
49201 static bool
49202 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
49204 cp_token *pragma_tok;
49205 unsigned int id;
49206 tree stmt;
49207 bool ret = false;
49209 pragma_tok = cp_lexer_consume_token (parser->lexer);
49210 gcc_assert (pragma_tok->type == CPP_PRAGMA);
49211 parser->lexer->in_pragma = true;
49213 id = cp_parser_pragma_kind (pragma_tok);
49214 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
49215 cp_ensure_no_omp_declare_simd (parser);
49216 switch (id)
49218 case PRAGMA_GCC_PCH_PREPROCESS:
49219 error_at (pragma_tok->location,
49220 "%<#pragma GCC pch_preprocess%> must be first");
49221 break;
49223 case PRAGMA_OMP_BARRIER:
49224 switch (context)
49226 case pragma_compound:
49227 cp_parser_omp_barrier (parser, pragma_tok);
49228 return false;
49229 case pragma_stmt:
49230 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
49231 "used in compound statements", "omp barrier");
49232 ret = true;
49233 break;
49234 default:
49235 goto bad_stmt;
49237 break;
49239 case PRAGMA_OMP_DEPOBJ:
49240 switch (context)
49242 case pragma_compound:
49243 cp_parser_omp_depobj (parser, pragma_tok);
49244 return false;
49245 case pragma_stmt:
49246 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
49247 "used in compound statements", "omp depobj");
49248 ret = true;
49249 break;
49250 default:
49251 goto bad_stmt;
49253 break;
49255 case PRAGMA_OMP_FLUSH:
49256 switch (context)
49258 case pragma_compound:
49259 cp_parser_omp_flush (parser, pragma_tok);
49260 return false;
49261 case pragma_stmt:
49262 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
49263 "used in compound statements", "omp flush");
49264 ret = true;
49265 break;
49266 default:
49267 goto bad_stmt;
49269 break;
49271 case PRAGMA_OMP_TASKWAIT:
49272 switch (context)
49274 case pragma_compound:
49275 cp_parser_omp_taskwait (parser, pragma_tok);
49276 return false;
49277 case pragma_stmt:
49278 error_at (pragma_tok->location,
49279 "%<#pragma %s%> may only be used in compound statements",
49280 "omp taskwait");
49281 ret = true;
49282 break;
49283 default:
49284 goto bad_stmt;
49286 break;
49288 case PRAGMA_OMP_TASKYIELD:
49289 switch (context)
49291 case pragma_compound:
49292 cp_parser_omp_taskyield (parser, pragma_tok);
49293 return false;
49294 case pragma_stmt:
49295 error_at (pragma_tok->location,
49296 "%<#pragma %s%> may only be used in compound statements",
49297 "omp taskyield");
49298 ret = true;
49299 break;
49300 default:
49301 goto bad_stmt;
49303 break;
49305 case PRAGMA_OMP_CANCEL:
49306 switch (context)
49308 case pragma_compound:
49309 cp_parser_omp_cancel (parser, pragma_tok);
49310 return false;
49311 case pragma_stmt:
49312 error_at (pragma_tok->location,
49313 "%<#pragma %s%> may only be used in compound statements",
49314 "omp cancel");
49315 ret = true;
49316 break;
49317 default:
49318 goto bad_stmt;
49320 break;
49322 case PRAGMA_OMP_CANCELLATION_POINT:
49323 return cp_parser_omp_cancellation_point (parser, pragma_tok, context);
49325 case PRAGMA_OMP_THREADPRIVATE:
49326 cp_parser_omp_threadprivate (parser, pragma_tok);
49327 return false;
49329 case PRAGMA_OMP_DECLARE:
49330 return cp_parser_omp_declare (parser, pragma_tok, context);
49332 case PRAGMA_OACC_DECLARE:
49333 cp_parser_oacc_declare (parser, pragma_tok);
49334 return false;
49336 case PRAGMA_OACC_ENTER_DATA:
49337 if (context == pragma_stmt)
49339 error_at (pragma_tok->location,
49340 "%<#pragma %s%> may only be used in compound statements",
49341 "acc enter data");
49342 ret = true;
49343 break;
49345 else if (context != pragma_compound)
49346 goto bad_stmt;
49347 cp_parser_omp_construct (parser, pragma_tok, if_p);
49348 return true;
49350 case PRAGMA_OACC_EXIT_DATA:
49351 if (context == pragma_stmt)
49353 error_at (pragma_tok->location,
49354 "%<#pragma %s%> may only be used in compound statements",
49355 "acc exit data");
49356 ret = true;
49357 break;
49359 else if (context != pragma_compound)
49360 goto bad_stmt;
49361 cp_parser_omp_construct (parser, pragma_tok, if_p);
49362 return true;
49364 case PRAGMA_OACC_ROUTINE:
49365 if (context != pragma_external)
49367 error_at (pragma_tok->location,
49368 "%<#pragma acc routine%> must be at file scope");
49369 ret = true;
49370 break;
49372 cp_parser_oacc_routine (parser, pragma_tok, context);
49373 return false;
49375 case PRAGMA_OACC_UPDATE:
49376 if (context == pragma_stmt)
49378 error_at (pragma_tok->location,
49379 "%<#pragma %s%> may only be used in compound statements",
49380 "acc update");
49381 ret = true;
49382 break;
49384 else if (context != pragma_compound)
49385 goto bad_stmt;
49386 cp_parser_omp_construct (parser, pragma_tok, if_p);
49387 return true;
49389 case PRAGMA_OACC_WAIT:
49390 if (context == pragma_stmt)
49392 error_at (pragma_tok->location,
49393 "%<#pragma %s%> may only be used in compound statements",
49394 "acc wait");
49395 ret = true;
49396 break;
49398 else if (context != pragma_compound)
49399 goto bad_stmt;
49400 cp_parser_omp_construct (parser, pragma_tok, if_p);
49401 return true;
49402 case PRAGMA_OMP_ALLOCATE:
49403 cp_parser_omp_allocate (parser, pragma_tok);
49404 return false;
49405 case PRAGMA_OACC_ATOMIC:
49406 case PRAGMA_OACC_CACHE:
49407 case PRAGMA_OACC_DATA:
49408 case PRAGMA_OACC_HOST_DATA:
49409 case PRAGMA_OACC_KERNELS:
49410 case PRAGMA_OACC_LOOP:
49411 case PRAGMA_OACC_PARALLEL:
49412 case PRAGMA_OACC_SERIAL:
49413 case PRAGMA_OMP_ASSUME:
49414 case PRAGMA_OMP_ATOMIC:
49415 case PRAGMA_OMP_CRITICAL:
49416 case PRAGMA_OMP_DISTRIBUTE:
49417 case PRAGMA_OMP_FOR:
49418 case PRAGMA_OMP_LOOP:
49419 case PRAGMA_OMP_MASKED:
49420 case PRAGMA_OMP_MASTER:
49421 case PRAGMA_OMP_PARALLEL:
49422 case PRAGMA_OMP_SCOPE:
49423 case PRAGMA_OMP_SECTIONS:
49424 case PRAGMA_OMP_SIMD:
49425 case PRAGMA_OMP_SINGLE:
49426 case PRAGMA_OMP_TASK:
49427 case PRAGMA_OMP_TASKGROUP:
49428 case PRAGMA_OMP_TASKLOOP:
49429 case PRAGMA_OMP_TEAMS:
49430 if (context != pragma_stmt && context != pragma_compound)
49431 goto bad_stmt;
49432 stmt = push_omp_privatization_clauses (false);
49433 cp_parser_omp_construct (parser, pragma_tok, if_p);
49434 pop_omp_privatization_clauses (stmt);
49435 return true;
49437 case PRAGMA_OMP_REQUIRES:
49438 if (context != pragma_external)
49440 error_at (pragma_tok->location,
49441 "%<#pragma omp requires%> may only be used at file or "
49442 "namespace scope");
49443 ret = true;
49444 break;
49446 return cp_parser_omp_requires (parser, pragma_tok);
49448 case PRAGMA_OMP_ASSUMES:
49449 if (context != pragma_external)
49451 error_at (pragma_tok->location,
49452 "%<#pragma omp assumes%> may only be used at file or "
49453 "namespace scope");
49454 ret = true;
49455 break;
49457 return cp_parser_omp_assumes (parser, pragma_tok);
49459 case PRAGMA_OMP_NOTHING:
49460 cp_parser_omp_nothing (parser, pragma_tok);
49461 return false;
49463 case PRAGMA_OMP_ERROR:
49464 return cp_parser_omp_error (parser, pragma_tok, context);
49466 case PRAGMA_OMP_ORDERED:
49467 if (context != pragma_stmt && context != pragma_compound)
49468 goto bad_stmt;
49469 stmt = push_omp_privatization_clauses (false);
49470 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
49471 pop_omp_privatization_clauses (stmt);
49472 return ret;
49474 case PRAGMA_OMP_TARGET:
49475 if (context != pragma_stmt && context != pragma_compound)
49476 goto bad_stmt;
49477 stmt = push_omp_privatization_clauses (false);
49478 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
49479 pop_omp_privatization_clauses (stmt);
49480 return ret;
49482 case PRAGMA_OMP_BEGIN:
49483 cp_parser_omp_begin (parser, pragma_tok);
49484 return false;
49486 case PRAGMA_OMP_END:
49487 cp_parser_omp_end (parser, pragma_tok);
49488 return false;
49490 case PRAGMA_OMP_SCAN:
49491 error_at (pragma_tok->location,
49492 "%<#pragma omp scan%> may only be used in "
49493 "a loop construct with %<inscan%> %<reduction%> clause");
49494 break;
49496 case PRAGMA_OMP_SECTION:
49497 error_at (pragma_tok->location,
49498 "%<#pragma omp section%> may only be used in "
49499 "%<#pragma omp sections%> construct");
49500 break;
49502 case PRAGMA_IVDEP:
49504 if (context == pragma_external)
49506 error_at (pragma_tok->location,
49507 "%<#pragma GCC ivdep%> must be inside a function");
49508 break;
49510 const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
49511 unsigned short unroll;
49512 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
49513 if (tok->type == CPP_PRAGMA
49514 && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
49516 tok = cp_lexer_consume_token (parser->lexer);
49517 unroll = cp_parser_pragma_unroll (parser, tok);
49518 tok = cp_lexer_peek_token (the_parser->lexer);
49520 else
49521 unroll = 0;
49522 if (tok->type != CPP_KEYWORD
49523 || (tok->keyword != RID_FOR
49524 && tok->keyword != RID_WHILE
49525 && tok->keyword != RID_DO))
49527 cp_parser_error (parser, "for, while or do statement expected");
49528 return false;
49530 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
49531 return true;
49534 case PRAGMA_UNROLL:
49536 if (context == pragma_external)
49538 error_at (pragma_tok->location,
49539 "%<#pragma GCC unroll%> must be inside a function");
49540 break;
49542 const unsigned short unroll
49543 = cp_parser_pragma_unroll (parser, pragma_tok);
49544 bool ivdep;
49545 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
49546 if (tok->type == CPP_PRAGMA
49547 && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
49549 tok = cp_lexer_consume_token (parser->lexer);
49550 ivdep = cp_parser_pragma_ivdep (parser, tok);
49551 tok = cp_lexer_peek_token (the_parser->lexer);
49553 else
49554 ivdep = false;
49555 if (tok->type != CPP_KEYWORD
49556 || (tok->keyword != RID_FOR
49557 && tok->keyword != RID_WHILE
49558 && tok->keyword != RID_DO))
49560 cp_parser_error (parser, "for, while or do statement expected");
49561 return false;
49563 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
49564 return true;
49567 default:
49568 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
49569 c_invoke_pragma_handler (id);
49570 break;
49572 bad_stmt:
49573 cp_parser_error (parser, "expected declaration specifiers");
49574 break;
49577 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
49578 return ret;
49581 /* The interface the pragma parsers have to the lexer. */
49583 enum cpp_ttype
49584 pragma_lex (tree *value, location_t *loc)
49586 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
49587 enum cpp_ttype ret = tok->type;
49589 *value = tok->u.value;
49590 if (loc)
49591 *loc = tok->location;
49593 if (ret == CPP_PRAGMA_EOL)
49594 ret = CPP_EOF;
49595 else if (ret == CPP_STRING)
49596 *value = cp_parser_string_literal (the_parser, /*translate=*/false,
49597 /*wide_ok=*/false);
49598 else
49600 if (ret == CPP_KEYWORD)
49601 ret = CPP_NAME;
49602 cp_lexer_consume_token (the_parser->lexer);
49605 return ret;
49609 /* External interface. */
49611 /* Parse one entire translation unit. */
49613 void
49614 c_parse_file (void)
49616 static bool already_called = false;
49618 if (already_called)
49619 fatal_error (input_location,
49620 "multi-source compilation not implemented for C++");
49621 already_called = true;
49623 /* cp_lexer_new_main is called before doing any GC allocation
49624 because tokenization might load a PCH file. */
49625 cp_lexer_new_main ();
49627 cp_parser_translation_unit (the_parser);
49628 class_decl_loc_t::diag_mismatched_tags ();
49630 the_parser = NULL;
49632 finish_translation_unit ();
49635 /* Create an identifier for a generic parameter type (a synthesized
49636 template parameter implied by `auto' or a concept identifier). */
49638 static GTY(()) int generic_parm_count;
49639 static tree
49640 make_generic_type_name ()
49642 char buf[32];
49643 sprintf (buf, "auto:%d", ++generic_parm_count);
49644 return get_identifier (buf);
49647 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
49648 (creating a new template parameter list if necessary). Returns the newly
49649 created template type parm. */
49651 static tree
49652 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
49654 /* A requires-clause is not a function and cannot have placeholders. */
49655 if (current_binding_level->requires_expression)
49657 error ("placeholder type not allowed in this context");
49658 return error_mark_node;
49661 gcc_assert (current_binding_level->kind == sk_function_parms);
49663 /* We are either continuing a function template that already contains implicit
49664 template parameters, creating a new fully-implicit function template, or
49665 extending an existing explicit function template with implicit template
49666 parameters. */
49668 cp_binding_level *const entry_scope = current_binding_level;
49670 bool become_template = false;
49671 cp_binding_level *parent_scope = 0;
49673 if (parser->implicit_template_scope)
49675 gcc_assert (parser->implicit_template_parms);
49677 current_binding_level = parser->implicit_template_scope;
49679 else
49681 /* Roll back to the existing template parameter scope (in the case of
49682 extending an explicit function template) or introduce a new template
49683 parameter scope ahead of the function parameter scope (or class scope
49684 in the case of out-of-line member definitions). The function scope is
49685 added back after template parameter synthesis below. */
49687 cp_binding_level *scope = entry_scope;
49689 while (scope->kind == sk_function_parms)
49691 parent_scope = scope;
49692 scope = scope->level_chain;
49694 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
49696 /* If not defining a class, then any class scope is a scope level in
49697 an out-of-line member definition. In this case simply wind back
49698 beyond the first such scope to inject the template parameter list.
49699 Otherwise wind back to the class being defined. The latter can
49700 occur in class member friend declarations such as:
49702 class A {
49703 void foo (auto);
49705 class B {
49706 friend void A::foo (auto);
49709 The template parameter list synthesized for the friend declaration
49710 must be injected in the scope of 'B'. This can also occur in
49711 erroneous cases such as:
49713 struct A {
49714 struct B {
49715 void foo (auto);
49717 void B::foo (auto) {}
49720 Here the attempted definition of 'B::foo' within 'A' is ill-formed
49721 but, nevertheless, the template parameter list synthesized for the
49722 declarator should be injected into the scope of 'A' as if the
49723 ill-formed template was specified explicitly. */
49725 while (scope->kind == sk_class && !scope->defining_class_p)
49727 parent_scope = scope;
49728 scope = scope->level_chain;
49732 current_binding_level = scope;
49734 if (scope->kind != sk_template_parms
49735 || !function_being_declared_is_template_p (parser))
49737 /* Introduce a new template parameter list for implicit template
49738 parameters. */
49740 become_template = true;
49742 parser->implicit_template_scope
49743 = begin_scope (sk_template_parms, NULL);
49745 ++processing_template_decl;
49747 parser->fully_implicit_function_template_p = true;
49748 ++parser->num_template_parameter_lists;
49750 else
49752 /* Synthesize implicit template parameters at the end of the explicit
49753 template parameter list. */
49755 gcc_assert (current_template_parms);
49757 parser->implicit_template_scope = scope;
49759 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
49760 parser->implicit_template_parms
49761 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
49765 /* Synthesize a new template parameter and track the current template
49766 parameter chain with implicit_template_parms. */
49768 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
49769 tree synth_id = make_generic_type_name ();
49770 bool non_type = false;
49772 /* Synthesize the type template parameter. */
49773 gcc_assert(!proto || TREE_CODE (proto) == TYPE_DECL);
49774 tree synth_tmpl_parm = finish_template_type_parm (class_type_node, synth_id);
49776 if (become_template)
49777 current_template_parms = tree_cons (size_int (current_template_depth + 1),
49778 NULL_TREE, current_template_parms);
49780 /* Attach the constraint to the parm before processing. */
49781 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
49782 TREE_TYPE (node) = constr;
49783 tree new_parm
49784 = process_template_parm (parser->implicit_template_parms,
49785 input_location,
49786 node,
49787 /*non_type=*/non_type,
49788 /*param_pack=*/false);
49789 // Process_template_parm returns the list of parms, and
49790 // parser->implicit_template_parms holds the final node of the parm
49791 // list. We really want to manipulate the newly appended element.
49792 gcc_checking_assert (!parser->implicit_template_parms
49793 || parser->implicit_template_parms == new_parm);
49794 if (parser->implicit_template_parms)
49795 new_parm = TREE_CHAIN (new_parm);
49796 gcc_checking_assert (!TREE_CHAIN (new_parm));
49798 // Record the last implicit parm node
49799 parser->implicit_template_parms = new_parm;
49801 /* Mark the synthetic declaration "virtual". This is used when
49802 comparing template-heads to determine if whether an abbreviated
49803 function template is equivalent to an explicit template.
49805 Note that DECL_ARTIFICIAL is used elsewhere for template
49806 parameters. */
49807 if (TREE_VALUE (new_parm) != error_mark_node)
49808 DECL_VIRTUAL_P (TREE_VALUE (new_parm)) = true;
49810 tree new_decl = get_local_decls ();
49811 if (non_type)
49812 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
49813 new_decl = DECL_INITIAL (new_decl);
49815 /* If creating a fully implicit function template, start the new implicit
49816 template parameter list with this synthesized type, otherwise grow the
49817 current template parameter list. */
49819 if (become_template)
49821 parent_scope->level_chain = current_binding_level;
49823 tree new_parms = make_tree_vec (1);
49824 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
49825 TREE_VALUE (current_template_parms) = new_parms;
49827 else
49829 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
49830 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
49831 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
49832 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
49835 /* If the new parameter was constrained, we need to add that to the
49836 constraints in the template parameter list. */
49837 if (tree req = TEMPLATE_PARM_CONSTRAINTS (new_parm))
49839 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
49840 reqs = combine_constraint_expressions (reqs, req);
49841 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
49844 current_binding_level = entry_scope;
49846 return new_decl;
49849 /* Finish the declaration of a fully implicit function template. Such a
49850 template has no explicit template parameter list so has not been through the
49851 normal template head and tail processing. synthesize_implicit_template_parm
49852 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
49853 provided if the declaration is a class member such that its template
49854 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
49855 form is returned. Otherwise NULL_TREE is returned. */
49857 static tree
49858 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
49860 gcc_assert (parser->fully_implicit_function_template_p);
49862 if (member_decl_opt && member_decl_opt != error_mark_node
49863 && DECL_VIRTUAL_P (member_decl_opt))
49865 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
49866 "implicit templates may not be %<virtual%>");
49867 DECL_VIRTUAL_P (member_decl_opt) = false;
49870 if (member_decl_opt)
49871 member_decl_opt = finish_member_template_decl (member_decl_opt);
49872 end_template_decl ();
49874 parser->fully_implicit_function_template_p = false;
49875 parser->implicit_template_parms = 0;
49876 parser->implicit_template_scope = 0;
49877 --parser->num_template_parameter_lists;
49879 return member_decl_opt;
49882 /* Like finish_fully_implicit_template, but to be used in error
49883 recovery, rearranging scopes so that we restore the state we had
49884 before synthesize_implicit_template_parm inserted the implement
49885 template parms scope. */
49887 static void
49888 abort_fully_implicit_template (cp_parser *parser)
49890 cp_binding_level *return_to_scope = current_binding_level;
49892 if (parser->implicit_template_scope
49893 && return_to_scope != parser->implicit_template_scope)
49895 cp_binding_level *child = return_to_scope;
49896 for (cp_binding_level *scope = child->level_chain;
49897 scope != parser->implicit_template_scope;
49898 scope = child->level_chain)
49899 child = scope;
49900 child->level_chain = parser->implicit_template_scope->level_chain;
49901 parser->implicit_template_scope->level_chain = return_to_scope;
49902 current_binding_level = parser->implicit_template_scope;
49904 else
49905 return_to_scope = return_to_scope->level_chain;
49907 finish_fully_implicit_template (parser, NULL);
49909 gcc_assert (current_binding_level == return_to_scope);
49912 /* Helper function for diagnostics that have complained about things
49913 being used with 'extern "C"' linkage.
49915 Attempt to issue a note showing where the 'extern "C"' linkage began. */
49917 void
49918 maybe_show_extern_c_location (void)
49920 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
49921 inform (the_parser->innermost_linkage_specification_location,
49922 "%<extern \"C\"%> linkage started here");
49925 #include "gt-cp-parser.h"